History

avcodec/tiff: support multiple black levels

Paul B Mahol committed 4 years ago

Commit 0ca73861 parent 14524451 file · +40 −23Browse files

Unified · Split

libavcodec/tiff.c+40−23
@@ -93,8 +93,8 @@ typedef struct TiffContext { · 92 unmodified lines
9393 float color_matrix[3][4];
9494 float camera_calibration[4][4];
9595 float premultiply[4];
96+ float black_level[4];
9697
97− unsigned black_level;
9898 unsigned white_level;
9999 uint16_t dng_lut[65536];
100100
@@ -290,7 +290,7 @@ static int add_metadata(int count, int type, · 189 unmodified lines
290290 */
291291 static uint16_t av_always_inline dng_process_color16(uint16_t value,
292292 const uint16_t *lut,
293− uint16_t black_level,
293+ float black_level,
294294 float scale_factor)
295295 {
296296 float value_norm;
@@ -299,10 +299,8 @@ static uint16_t av_always_inline dng_process_color16(uint16_t value, · 2 unmodified lines
299299 value = lut[value];
300300
301301 // Black level subtraction
302− value = av_clip_uint16((unsigned)value - black_level);
303−
304302 // Color scaling
305− value_norm = (float)value * scale_factor;
303+ value_norm = ((float)value - black_level) * scale_factor;
306304
307305 value = av_clip_uint16(lrintf(value_norm));
308306
@@ -311,7 +309,7 @@ static uint16_t av_always_inline dng_process_color16(uint16_t value, · 2 unmodified lines
311309
312310 static uint16_t av_always_inline dng_process_color8(uint16_t value,
313311 const uint16_t *lut,
314− uint16_t black_level,
312+ float black_level,
315313 float scale_factor)
316314 {
317315 return dng_process_color16(value, lut, black_level, scale_factor) >> 8;
@@ -326,10 +324,10 @@ static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int dst_stri · 8 unmodified lines
326324
327325 if (s->is_bayer) {
328326 for (int i = 0; i < 4; i++)
329− scale_factor[i] = s->premultiply[s->pattern[i]] * 65535.f / (s->white_level - s->black_level);
327+ scale_factor[i] = s->premultiply[s->pattern[i]] * 65535.f / (s->white_level - s->black_level[i]);
330328 } else {
331329 for (int i = 0; i < 4; i++)
332− scale_factor[i] = 65535.f * s->premultiply[i] / (s->white_level - s->black_level);
330+ scale_factor[i] = 65535.f * s->premultiply[i] / (s->white_level - s->black_level[i]);
333331 }
334332
335333 if (is_single_comp) {
@@ -344,7 +342,7 @@ static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int dst_stri · 8 unmodified lines
344342
345343 /* Blit first half of input row row to initial row of output */
346344 for (col = 0; col < width; col++)
347− *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level, scale_factor[col&1]);
345+ *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level[col&1], scale_factor[col&1]);
348346
349347 /* Advance the destination pointer by a row (source pointer remains in the same place) */
350348 dst += dst_stride * sizeof(uint16_t);
@@ -352,7 +350,7 @@ static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int dst_stri · 1 unmodified line
352350
353351 /* Blit second half of input row row to next row of output */
354352 for (col = 0; col < width; col++)
355− *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level, scale_factor[(col&1) + 2]);
353+ *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level[(col&1) + 2], scale_factor[(col&1) + 2]);
356354
357355 dst += dst_stride * sizeof(uint16_t);
358356 src += src_stride * sizeof(uint16_t);
@@ -366,7 +364,9 @@ static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int dst_stri · 7 unmodified lines
366364 uint16_t *src_u16 = (uint16_t *)src;
367365
368366 for (col = 0; col < width; col++)
369− *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level, scale_factor[(col&1) + 2 * ((line&1) + odd_line)]);
367+ *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut,
368+ s->black_level[(col&1) + 2 * ((line&1) + odd_line)],
369+ scale_factor[(col&1) + 2 * ((line&1) + odd_line)]);
370370
371371 dst += dst_stride * sizeof(uint16_t);
372372 src += src_stride * sizeof(uint16_t);
@@ -377,7 +377,9 @@ static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int dst_stri · 4 unmodified lines
377377 const uint8_t *src_u8 = src;
378378
379379 for (col = 0; col < width; col++)
380− *dst_u8++ = dng_process_color8(*src_u8++, s->dng_lut, s->black_level, scale_factor[(col&1) + 2 * ((line&1) + odd_line)]);
380+ *dst_u8++ = dng_process_color8(*src_u8++, s->dng_lut,
381+ s->black_level[(col&1) + 2 * ((line&1) + odd_line)],
382+ scale_factor[(col&1) + 2 * ((line&1) + odd_line)]);
381383
382384 dst += dst_stride;
383385 src += src_stride;
@@ -1458,22 +1460,34 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame) · 1074 unmodified lines
14581460 s->white_level = s->dng_lut[count-1];
14591461 break;
14601462 case DNG_BLACK_LEVEL:
1461− if (count > 1) { /* Use the first value in the pattern (assume they're all the same) */
1463+ if (count > FF_ARRAY_ELEMS(s->black_level))
1464+ return AVERROR_INVALIDDATA;
1465+ s->black_level[0] = value / (float)value2;
1466+ for (int i = 0; i < count && count > 1; i++) {
14621467 if (type == TIFF_RATIONAL) {
14631468 value = ff_tget(&s->gb, TIFF_LONG, s->le);
14641469 value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
14651470 if (!value2) {
1466− av_log(s->avctx, AV_LOG_WARNING, "Invalid black level denominator\n");
1471+ av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
14671472 value2 = 1;
14681473 }
14691474
1470− s->black_level = value / value2;
1471− } else
1472− s->black_level = ff_tget(&s->gb, type, s->le);
1473− av_log(s->avctx, AV_LOG_WARNING, "Assuming black level pattern values are identical\n");
1474− } else {
1475− s->black_level = value / value2;
1475+ s->black_level[i] = value / (float)value2;
1476+ } else if (type == TIFF_SRATIONAL) {
1477+ int value = ff_tget(&s->gb, TIFF_LONG, s->le);
1478+ int value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1479+ if (!value2) {
1480+ av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1481+ value2 = 1;
1482+ }
1483+
1484+ s->black_level[i] = value / (float)value2;
1485+ } else {
1486+ s->black_level[i] = ff_tget(&s->gb, type, s->le);
1487+ }
14761488 }
1489+ for (int i = count; i < 4 && count > 0; i++)
1490+ s->black_level[i] = s->black_level[count - 1];
14771491 break;
14781492 case DNG_WHITE_LEVEL:
14791493 s->white_level = value;
@@ -1939,6 +1953,9 @@ again: · 459 unmodified lines
19391953 for (i = 0; i < 65536; i++)
19401954 s->dng_lut[i] = i;
19411955
1956+ for (i = 0; i < FF_ARRAY_ELEMS(s->black_level); i++)
1957+ s->black_level[i] = 0.f;
1958+
19421959 for (i = 0; i < FF_ARRAY_ELEMS(s->as_shot_neutral); i++)
19431960 s->as_shot_neutral[i] = 0.f;
19441961
@@ -2067,9 +2084,9 @@ again: · 122 unmodified lines
20672084 if (s->white_level == 0)
20682085 s->white_level = (1LL << bps) - 1; /* Default value as per the spec */
20692086
2070− if (s->white_level <= s->black_level) {
2071− av_log(avctx, AV_LOG_ERROR, "BlackLevel (%"PRId32") must be less than WhiteLevel (%"PRId32")\n",
2072− s->black_level, s->white_level);
2087+ if (s->white_level <= s->black_level[0]) {
2088+ av_log(avctx, AV_LOG_ERROR, "BlackLevel (%g) must be less than WhiteLevel (%"PRId32")\n",
2089+ s->black_level[0], s->white_level);
20732090 return AVERROR_INVALIDDATA;
20742091 }
20752092

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t