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
93 float color_matrix[3][4];93 float color_matrix[3][4];
94 float camera_calibration[4][4];94 float camera_calibration[4][4];
95 float premultiply[4];95 float premultiply[4];
96 float black_level[4];
96 97
97 unsigned black_level;
98 unsigned white_level;98 unsigned white_level;
99 uint16_t dng_lut[65536];99 uint16_t dng_lut[65536];
100 100
@@ -290,7 +290,7 @@ static int add_metadata(int count, int type, · 189 unmodified lines
290 */290 */
291static uint16_t av_always_inline dng_process_color16(uint16_t value,291static uint16_t av_always_inline dng_process_color16(uint16_t value,
292 const uint16_t *lut,292 const uint16_t *lut,
293 uint16_t black_level,293 float black_level,
294 float scale_factor)294 float scale_factor)
295{295{
296 float value_norm;296 float value_norm;
@@ -299,10 +299,8 @@ static uint16_t av_always_inline dng_process_color16(uint16_t value, · 2 unmodified lines
299 value = lut[value];299 value = lut[value];
300 300
301 // Black level subtraction301 // Black level subtraction
302 value = av_clip_uint16((unsigned)value - black_level);
303
304 // Color scaling302 // Color scaling
305 value_norm = (float)value * scale_factor;303 value_norm = ((float)value - black_level) * scale_factor;
306 304
307 value = av_clip_uint16(lrintf(value_norm));305 value = av_clip_uint16(lrintf(value_norm));
308 306
@@ -311,7 +309,7 @@ static uint16_t av_always_inline dng_process_color16(uint16_t value, · 2 unmodified lines
311 309
312static uint16_t av_always_inline dng_process_color8(uint16_t value,310static uint16_t av_always_inline dng_process_color8(uint16_t value,
313 const uint16_t *lut,311 const uint16_t *lut,
314 uint16_t black_level,312 float black_level,
315 float scale_factor)313 float scale_factor)
316{314{
317 return dng_process_color16(value, lut, black_level, scale_factor) >> 8;315 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
326 324
327 if (s->is_bayer) {325 if (s->is_bayer) {
328 for (int i = 0; i < 4; i++)326 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]);
330 } else {328 } else {
331 for (int i = 0; i < 4; i++)329 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]);
333 }331 }
334 332
335 if (is_single_comp) {333 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
344 342
345 /* Blit first half of input row row to initial row of output */343 /* Blit first half of input row row to initial row of output */
346 for (col = 0; col < width; col++)344 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]);
348 346
349 /* Advance the destination pointer by a row (source pointer remains in the same place) */347 /* Advance the destination pointer by a row (source pointer remains in the same place) */
350 dst += dst_stride * sizeof(uint16_t);348 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
352 350
353 /* Blit second half of input row row to next row of output */351 /* Blit second half of input row row to next row of output */
354 for (col = 0; col < width; col++)352 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]);
356 354
357 dst += dst_stride * sizeof(uint16_t);355 dst += dst_stride * sizeof(uint16_t);
358 src += src_stride * sizeof(uint16_t);356 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
366 uint16_t *src_u16 = (uint16_t *)src;364 uint16_t *src_u16 = (uint16_t *)src;
367 365
368 for (col = 0; col < width; col++)366 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)]);
370 370
371 dst += dst_stride * sizeof(uint16_t);371 dst += dst_stride * sizeof(uint16_t);
372 src += src_stride * sizeof(uint16_t);372 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
377 const uint8_t *src_u8 = src;377 const uint8_t *src_u8 = src;
378 378
379 for (col = 0; col < width; col++)379 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)]);
381 383
382 dst += dst_stride;384 dst += dst_stride;
383 src += src_stride;385 src += src_stride;
@@ -1458,22 +1460,34 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame) · 1074 unmodified lines
1458 s->white_level = s->dng_lut[count-1];1460 s->white_level = s->dng_lut[count-1];
1459 break;1461 break;
1460 case DNG_BLACK_LEVEL:1462 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++) {
1462 if (type == TIFF_RATIONAL) {1467 if (type == TIFF_RATIONAL) {
1463 value = ff_tget(&s->gb, TIFF_LONG, s->le);1468 value = ff_tget(&s->gb, TIFF_LONG, s->le);
1464 value2 = ff_tget(&s->gb, TIFF_LONG, s->le);1469 value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1465 if (!value2) {1470 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");
1467 value2 = 1;1472 value2 = 1;
1468 }1473 }
1469 1474
1470 s->black_level = value / value2;1475 s->black_level[i] = value / (float)value2;
1471 } else1476 } else if (type == TIFF_SRATIONAL) {
1472 s->black_level = ff_tget(&s->gb, type, s->le);1477 int value = ff_tget(&s->gb, TIFF_LONG, s->le);
1473 av_log(s->avctx, AV_LOG_WARNING, "Assuming black level pattern values are identical\n");1478 int value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1474 } else {1479 if (!value2) {
1475 s->black_level = value / 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 }
1476 }1488 }
1489 for (int i = count; i < 4 && count > 0; i++)
1490 s->black_level[i] = s->black_level[count - 1];
1477 break;1491 break;
1478 case DNG_WHITE_LEVEL:1492 case DNG_WHITE_LEVEL:
1479 s->white_level = value;1493 s->white_level = value;
@@ -1939,6 +1953,9 @@ again: · 459 unmodified lines
1939 for (i = 0; i < 65536; i++)1953 for (i = 0; i < 65536; i++)
1940 s->dng_lut[i] = i;1954 s->dng_lut[i] = i;
1941 1955
1956 for (i = 0; i < FF_ARRAY_ELEMS(s->black_level); i++)
1957 s->black_level[i] = 0.f;
1958
1942 for (i = 0; i < FF_ARRAY_ELEMS(s->as_shot_neutral); i++)1959 for (i = 0; i < FF_ARRAY_ELEMS(s->as_shot_neutral); i++)
1943 s->as_shot_neutral[i] = 0.f;1960 s->as_shot_neutral[i] = 0.f;
1944 1961
@@ -2067,9 +2084,9 @@ again: · 122 unmodified lines
2067 if (s->white_level == 0)2084 if (s->white_level == 0)
2068 s->white_level = (1LL << bps) - 1; /* Default value as per the spec */2085 s->white_level = (1LL << bps) - 1; /* Default value as per the spec */
2069 2086
2070 if (s->white_level <= s->black_level) {2087 if (s->white_level <= s->black_level[0]) {
2071 av_log(avctx, AV_LOG_ERROR, "BlackLevel (%"PRId32") must be less than WhiteLevel (%"PRId32")\n",2088 av_log(avctx, AV_LOG_ERROR, "BlackLevel (%g) must be less than WhiteLevel (%"PRId32")\n",
2072 s->black_level, s->white_level);2089 s->black_level[0], s->white_level);
2073 return AVERROR_INVALIDDATA;2090 return AVERROR_INVALIDDATA;
2074 }2091 }
2075 2092

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