FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tiff.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 462 1531 30.2%
Functions: 14 31 45.2%
Branches: 298 1281 23.3%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2006 Konstantin Shishkov
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * TIFF image decoder
24 * @author Konstantin Shishkov
25 */
26
27 #include "config.h"
28 #if CONFIG_ZLIB
29 #include <zlib.h>
30 #endif
31 #if CONFIG_LZMA
32 #define LZMA_API_STATIC
33 #include <lzma.h>
34 #endif
35
36 #include <float.h>
37
38 #include "libavutil/attributes.h"
39 #include "libavutil/error.h"
40 #include "libavutil/intreadwrite.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/reverse.h"
43 #include "avcodec.h"
44 #include "bytestream.h"
45 #include "codec_internal.h"
46 #include "decode.h"
47 #include "faxcompr.h"
48 #include "lzw.h"
49 #include "tiff.h"
50 #include "tiff_data.h"
51 #include "mjpegdec.h"
52 #include "thread.h"
53 #include "get_bits.h"
54
55 typedef struct TiffContext {
56 AVClass *class;
57 AVCodecContext *avctx;
58 GetByteContext gb;
59
60 /* JPEG decoding for DNG */
61 AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
62 AVPacket *jpkt; // encoded JPEG tile
63 AVFrame *jpgframe; // decoded JPEG tile
64
65 int get_subimage;
66 uint16_t get_page;
67 int get_thumbnail;
68
69 enum TiffType tiff_type;
70 int width, height;
71 unsigned int bpp, bppcount;
72 uint32_t palette[256];
73 int palette_is_set;
74 int le;
75 enum TiffCompr compr;
76 enum TiffPhotometric photometric;
77 int planar;
78 int subsampling[2];
79 int fax_opts;
80 int predictor;
81 int fill_order;
82 uint32_t res[4];
83 int is_thumbnail;
84 unsigned last_tag;
85
86 int is_bayer;
87 int use_color_matrix;
88 uint8_t pattern[4];
89
90 float analog_balance[4];
91 float as_shot_neutral[4];
92 float as_shot_white[4];
93 float color_matrix[3][4];
94 float camera_calibration[4][4];
95 float premultiply[4];
96 float black_level[4];
97
98 unsigned white_level;
99 uint16_t dng_lut[65536];
100
101 uint32_t sub_ifd;
102 uint16_t cur_page;
103
104 int strips, rps, sstype;
105 int sot;
106 int stripsizesoff, stripsize, stripoff, strippos;
107 LZWState *lzw;
108
109 /* Tile support */
110 int is_tiled;
111 int tile_byte_counts_offset, tile_offsets_offset;
112 int tile_width, tile_length;
113
114 int is_jpeg;
115
116 uint8_t *deinvert_buf;
117 int deinvert_buf_size;
118 uint8_t *yuv_line;
119 unsigned int yuv_line_size;
120
121 int geotag_count;
122 TiffGeoTag *geotags;
123 } TiffContext;
124
125 static const float d65_white[3] = { 0.950456f, 1.f, 1.088754f };
126
127 static void tiff_set_type(TiffContext *s, enum TiffType tiff_type) {
128 if (s->tiff_type < tiff_type) // Prioritize higher-valued entries
129 s->tiff_type = tiff_type;
130 }
131
132 54 static void free_geotags(TiffContext *const s)
133 {
134 int i;
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 for (i = 0; i < s->geotag_count; i++) {
136 if (s->geotags[i].val)
137 av_freep(&s->geotags[i].val);
138 }
139 54 av_freep(&s->geotags);
140 54 s->geotag_count = 0;
141 54 }
142
143 #define RET_GEOKEY(TYPE, array, element)\
144 if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
145 key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(tiff_##array##_name_type_map))\
146 return tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
147
148 static const char *get_geokey_name(int key)
149 {
150 RET_GEOKEY(VERT, vert, name);
151 RET_GEOKEY(PROJ, proj, name);
152 RET_GEOKEY(GEOG, geog, name);
153 RET_GEOKEY(CONF, conf, name);
154
155 return NULL;
156 }
157
158 static int get_geokey_type(int key)
159 {
160 RET_GEOKEY(VERT, vert, type);
161 RET_GEOKEY(PROJ, proj, type);
162 RET_GEOKEY(GEOG, geog, type);
163 RET_GEOKEY(CONF, conf, type);
164
165 return AVERROR_INVALIDDATA;
166 }
167
168 static int cmp_id_key(const void *id, const void *k)
169 {
170 return *(const int*)id - ((const TiffGeoTagKeyName*)k)->key;
171 }
172
173 static const char *search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
174 {
175 TiffGeoTagKeyName *r = bsearch(&id, keys, n, sizeof(keys[0]), cmp_id_key);
176 if(r)
177 return r->name;
178
179 return NULL;
180 }
181
182 static char *get_geokey_val(int key, int val)
183 {
184 char *ap;
185
186 if (val == TIFF_GEO_KEY_UNDEFINED)
187 return av_strdup("undefined");
188 if (val == TIFF_GEO_KEY_USER_DEFINED)
189 return av_strdup("User-Defined");
190
191 #define RET_GEOKEY_VAL(TYPE, array)\
192 if (val >= TIFF_##TYPE##_OFFSET &&\
193 val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(tiff_##array##_codes))\
194 return av_strdup(tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]);
195
196 switch (key) {
197 case TIFF_GT_MODEL_TYPE_GEOKEY:
198 RET_GEOKEY_VAL(GT_MODEL_TYPE, gt_model_type);
199 break;
200 case TIFF_GT_RASTER_TYPE_GEOKEY:
201 RET_GEOKEY_VAL(GT_RASTER_TYPE, gt_raster_type);
202 break;
203 case TIFF_GEOG_LINEAR_UNITS_GEOKEY:
204 case TIFF_PROJ_LINEAR_UNITS_GEOKEY:
205 case TIFF_VERTICAL_UNITS_GEOKEY:
206 RET_GEOKEY_VAL(LINEAR_UNIT, linear_unit);
207 break;
208 case TIFF_GEOG_ANGULAR_UNITS_GEOKEY:
209 case TIFF_GEOG_AZIMUTH_UNITS_GEOKEY:
210 RET_GEOKEY_VAL(ANGULAR_UNIT, angular_unit);
211 break;
212 case TIFF_GEOGRAPHIC_TYPE_GEOKEY:
213 RET_GEOKEY_VAL(GCS_TYPE, gcs_type);
214 RET_GEOKEY_VAL(GCSE_TYPE, gcse_type);
215 break;
216 case TIFF_GEOG_GEODETIC_DATUM_GEOKEY:
217 RET_GEOKEY_VAL(GEODETIC_DATUM, geodetic_datum);
218 RET_GEOKEY_VAL(GEODETIC_DATUM_E, geodetic_datum_e);
219 break;
220 case TIFF_GEOG_ELLIPSOID_GEOKEY:
221 RET_GEOKEY_VAL(ELLIPSOID, ellipsoid);
222 break;
223 case TIFF_GEOG_PRIME_MERIDIAN_GEOKEY:
224 RET_GEOKEY_VAL(PRIME_MERIDIAN, prime_meridian);
225 break;
226 case TIFF_PROJECTED_CS_TYPE_GEOKEY:
227 ap = av_strdup(search_keyval(tiff_proj_cs_type_codes, FF_ARRAY_ELEMS(tiff_proj_cs_type_codes), val));
228 if(ap) return ap;
229 break;
230 case TIFF_PROJECTION_GEOKEY:
231 ap = av_strdup(search_keyval(tiff_projection_codes, FF_ARRAY_ELEMS(tiff_projection_codes), val));
232 if(ap) return ap;
233 break;
234 case TIFF_PROJ_COORD_TRANS_GEOKEY:
235 RET_GEOKEY_VAL(COORD_TRANS, coord_trans);
236 break;
237 case TIFF_VERTICAL_CS_TYPE_GEOKEY:
238 RET_GEOKEY_VAL(VERT_CS, vert_cs);
239 RET_GEOKEY_VAL(ORTHO_VERT_CS, ortho_vert_cs);
240 break;
241
242 }
243
244 ap = av_malloc(14);
245 if (ap)
246 snprintf(ap, 14, "Unknown-%d", val);
247 return ap;
248 }
249
250 static char *doubles2str(double *dp, int count, const char *sep)
251 {
252 int i;
253 char *ap, *ap0;
254 uint64_t component_len;
255 if (!sep) sep = ", ";
256 component_len = 24LL + strlen(sep);
257 if (count >= (INT_MAX - 1)/component_len)
258 return NULL;
259 ap = av_malloc(component_len * count + 1);
260 if (!ap)
261 return NULL;
262 ap0 = ap;
263 ap[0] = '\0';
264 for (i = 0; i < count; i++) {
265 unsigned l = snprintf(ap, component_len, "%.15g%s", dp[i], sep);
266 if(l >= component_len) {
267 av_free(ap0);
268 return NULL;
269 }
270 ap += l;
271 }
272 ap0[strlen(ap0) - strlen(sep)] = '\0';
273 return ap0;
274 }
275
276 26 static int add_metadata(int count, int type,
277 const char *name, const char *sep, TiffContext *s, AVFrame *frame)
278 {
279
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
26 switch(type) {
280 case TIFF_DOUBLE: return ff_tadd_doubles_metadata(count, name, sep, &s->gb, s->le, &frame->metadata);
281 12 case TIFF_SHORT : return ff_tadd_shorts_metadata(count, name, sep, &s->gb, s->le, 0, &frame->metadata);
282 14 case TIFF_STRING: return ff_tadd_string_metadata(count, name, &s->gb, s->le, &frame->metadata);
283 default : return AVERROR_INVALIDDATA;
284 };
285 }
286
287 /**
288 * Map stored raw sensor values into linear reference values (see: DNG Specification - Chapter 5)
289 */
290 static uint16_t av_always_inline dng_process_color16(uint16_t value,
291 const uint16_t *lut,
292 float black_level,
293 float scale_factor)
294 {
295 float value_norm;
296
297 // Lookup table lookup
298 value = lut[value];
299
300 // Black level subtraction
301 // Color scaling
302 value_norm = ((float)value - black_level) * scale_factor;
303
304 value = av_clip_uint16(lrintf(value_norm));
305
306 return value;
307 }
308
309 static uint16_t av_always_inline dng_process_color8(uint16_t value,
310 const uint16_t *lut,
311 float black_level,
312 float scale_factor)
313 {
314 return dng_process_color16(value, lut, black_level, scale_factor) >> 8;
315 }
316
317 static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
318 const uint8_t *src, int src_stride, int width, int height,
319 int is_single_comp, int is_u16, int odd_line)
320 {
321 float scale_factor[4];
322 int line, col;
323
324 if (s->is_bayer) {
325 for (int i = 0; i < 4; i++)
326 scale_factor[i] = s->premultiply[s->pattern[i]] * 65535.f / (s->white_level - s->black_level[i]);
327 } else {
328 for (int i = 0; i < 4; i++)
329 scale_factor[i] = s->premultiply[ i ] * 65535.f / (s->white_level - s->black_level[i]);
330 }
331
332 if (is_single_comp) {
333 if (!is_u16)
334 return; /* <= 8bpp unsupported */
335
336 /* Image is double the width and half the height we need, each row comprises 2 rows of the output
337 (split vertically in the middle). */
338 for (line = 0; line < height / 2; line++) {
339 uint16_t *dst_u16 = (uint16_t *)dst;
340 const uint16_t *src_u16 = (const uint16_t *)src;
341
342 /* Blit first half of input row row to initial row of output */
343 for (col = 0; col < width; col++)
344 *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level[col&1], scale_factor[col&1]);
345
346 /* Advance the destination pointer by a row (source pointer remains in the same place) */
347 dst += dst_stride * sizeof(uint16_t);
348 dst_u16 = (uint16_t *)dst;
349
350 /* Blit second half of input row row to next row of output */
351 for (col = 0; col < width; col++)
352 *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level[(col&1) + 2], scale_factor[(col&1) + 2]);
353
354 dst += dst_stride * sizeof(uint16_t);
355 src += src_stride * sizeof(uint16_t);
356 }
357 } else {
358 /* Input and output image are the same size and the MJpeg decoder has done per-component
359 deinterleaving, so blitting here is straightforward. */
360 if (is_u16) {
361 for (line = 0; line < height; line++) {
362 uint16_t *dst_u16 = (uint16_t *)dst;
363 const uint16_t *src_u16 = (const uint16_t *)src;
364
365 for (col = 0; col < width; col++)
366 *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut,
367 s->black_level[(col&1) + 2 * ((line&1) + odd_line)],
368 scale_factor[(col&1) + 2 * ((line&1) + odd_line)]);
369
370 dst += dst_stride * sizeof(uint16_t);
371 src += src_stride * sizeof(uint16_t);
372 }
373 } else {
374 for (line = 0; line < height; line++) {
375 uint8_t *dst_u8 = dst;
376 const uint8_t *src_u8 = src;
377
378 for (col = 0; col < width; col++)
379 *dst_u8++ = dng_process_color8(*src_u8++, s->dng_lut,
380 s->black_level[(col&1) + 2 * ((line&1) + odd_line)],
381 scale_factor[(col&1) + 2 * ((line&1) + odd_line)]);
382
383 dst += dst_stride;
384 src += src_stride;
385 }
386 }
387 }
388 }
389
390 36484 static void av_always_inline horizontal_fill(TiffContext *s,
391 unsigned int bpp, uint8_t* dst,
392 int usePtr, const uint8_t *src,
393 uint8_t c, int width, int offset)
394 {
395
1/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 36484 times.
36484 switch (bpp) {
396 case 1:
397 while (--width >= 0) {
398 dst[(width+offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
399 dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
400 dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
401 dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
402 dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
403 dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
404 dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
405 dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
406 }
407 break;
408 case 2:
409 while (--width >= 0) {
410 dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
411 dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
412 dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
413 dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
414 }
415 break;
416 case 4:
417 while (--width >= 0) {
418 dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
419 dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
420 }
421 break;
422 case 10:
423 case 12:
424 case 14: {
425 uint16_t *dst16 = (uint16_t *)dst;
426 int is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
427 uint8_t shift = is_dng ? 0 : 16 - bpp;
428 GetBitContext gb;
429
430 init_get_bits8(&gb, src, width);
431 for (int i = 0; i < s->width; i++) {
432 dst16[i] = get_bits(&gb, bpp) << shift;
433 }
434 }
435 break;
436 36484 default:
437
2/2
✓ Branch 0 taken 34369 times.
✓ Branch 1 taken 2115 times.
36484 if (usePtr) {
438 34369 memcpy(dst + offset, src, width);
439 } else {
440 2115 memset(dst + offset, c, width);
441 }
442 }
443 36484 }
444
445 18 static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
446 {
447 int i;
448
449 18 av_fast_padded_malloc(&s->deinvert_buf, &s->deinvert_buf_size, size);
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!s->deinvert_buf)
451 return AVERROR(ENOMEM);
452
2/2
✓ Branch 0 taken 251119 times.
✓ Branch 1 taken 18 times.
251137 for (i = 0; i < size; i++)
453 251119 s->deinvert_buf[i] = ff_reverse[src[i]];
454
455 18 return 0;
456 }
457
458 static void unpack_gray(TiffContext *s, AVFrame *p,
459 const uint8_t *src, int lnum, int width, int bpp)
460 {
461 GetBitContext gb;
462 uint16_t *dst = (uint16_t *)(p->data[0] + lnum * p->linesize[0]);
463
464 init_get_bits8(&gb, src, width);
465
466 for (int i = 0; i < s->width; i++) {
467 dst[i] = get_bits(&gb, bpp);
468 }
469 }
470
471 static void unpack_yuv(TiffContext *s, AVFrame *p,
472 const uint8_t *src, int lnum)
473 {
474 int i, j, k;
475 int w = (s->width - 1) / s->subsampling[0] + 1;
476 uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
477 uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
478 if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) {
479 for (i = 0; i < w; i++) {
480 for (j = 0; j < s->subsampling[1]; j++)
481 for (k = 0; k < s->subsampling[0]; k++)
482 p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
483 FFMIN(i * s->subsampling[0] + k, s->width-1)] = *src++;
484 *pu++ = *src++;
485 *pv++ = *src++;
486 }
487 }else{
488 for (i = 0; i < w; i++) {
489 for (j = 0; j < s->subsampling[1]; j++)
490 for (k = 0; k < s->subsampling[0]; k++)
491 p->data[0][(lnum + j) * p->linesize[0] +
492 i * s->subsampling[0] + k] = *src++;
493 *pu++ = *src++;
494 *pv++ = *src++;
495 }
496 }
497 }
498
499 #if CONFIG_ZLIB
500 2 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
501 int size)
502 {
503 2 z_stream zstream = { 0 };
504 int zret;
505
506 2 zstream.next_in = src;
507 2 zstream.avail_in = size;
508 2 zstream.next_out = dst;
509 2 zstream.avail_out = *len;
510 2 zret = inflateInit(&zstream);
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (zret != Z_OK) {
512 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
513 return zret;
514 }
515 2 zret = inflate(&zstream, Z_SYNC_FLUSH);
516 2 inflateEnd(&zstream);
517 2 *len = zstream.total_out;
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 return zret == Z_STREAM_END ? Z_OK : zret;
519 }
520
521 2 static int tiff_unpack_zlib(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
522 const uint8_t *src, int size, int width, int lines,
523 int strip_start, int is_yuv)
524 {
525 uint8_t *zbuf;
526 unsigned long outlen;
527 int ret, line;
528 2 outlen = width * lines;
529 2 zbuf = av_malloc(outlen);
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!zbuf)
531 return AVERROR(ENOMEM);
532
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->fill_order) {
533 if ((ret = deinvert_buffer(s, src, size)) < 0) {
534 av_free(zbuf);
535 return ret;
536 }
537 src = s->deinvert_buf;
538 }
539 2 ret = tiff_uncompress(zbuf, &outlen, src, size);
540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret != Z_OK) {
541 av_log(s->avctx, AV_LOG_ERROR,
542 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
543 (unsigned long)width * lines, ret);
544 av_free(zbuf);
545 return AVERROR_UNKNOWN;
546 }
547 2 src = zbuf;
548
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (line = 0; line < lines; line++) {
549
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16 if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
550 horizontal_fill(s, s->bpp, dst, 1, src, 0, width, 0);
551 } else {
552 16 memcpy(dst, src, width);
553 }
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (is_yuv) {
555 unpack_yuv(s, p, dst, strip_start + line);
556 line += s->subsampling[1] - 1;
557 }
558 16 dst += stride;
559 16 src += width;
560 }
561 2 av_free(zbuf);
562 2 return 0;
563 }
564 #endif
565
566 #if CONFIG_LZMA
567 static int tiff_uncompress_lzma(uint8_t *dst, uint64_t *len, const uint8_t *src,
568 int size)
569 {
570 lzma_stream stream = LZMA_STREAM_INIT;
571 lzma_ret ret;
572
573 stream.next_in = src;
574 stream.avail_in = size;
575 stream.next_out = dst;
576 stream.avail_out = *len;
577 ret = lzma_stream_decoder(&stream, UINT64_MAX, 0);
578 if (ret != LZMA_OK) {
579 av_log(NULL, AV_LOG_ERROR, "LZMA init error: %d\n", ret);
580 return ret;
581 }
582 ret = lzma_code(&stream, LZMA_RUN);
583 lzma_end(&stream);
584 *len = stream.total_out;
585 return ret == LZMA_STREAM_END ? LZMA_OK : ret;
586 }
587
588 static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
589 const uint8_t *src, int size, int width, int lines,
590 int strip_start, int is_yuv)
591 {
592 uint64_t outlen = width * (uint64_t)lines;
593 int ret, line;
594 uint8_t *buf = av_malloc(outlen);
595 if (!buf)
596 return AVERROR(ENOMEM);
597 if (s->fill_order) {
598 if ((ret = deinvert_buffer(s, src, size)) < 0) {
599 av_free(buf);
600 return ret;
601 }
602 src = s->deinvert_buf;
603 }
604 ret = tiff_uncompress_lzma(buf, &outlen, src, size);
605 if (ret != LZMA_OK) {
606 av_log(s->avctx, AV_LOG_ERROR,
607 "Uncompressing failed (%"PRIu64" of %"PRIu64") with error %d\n", outlen,
608 (uint64_t)width * lines, ret);
609 av_free(buf);
610 return AVERROR_UNKNOWN;
611 }
612 src = buf;
613 for (line = 0; line < lines; line++) {
614 if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
615 horizontal_fill(s, s->bpp, dst, 1, src, 0, width, 0);
616 } else {
617 memcpy(dst, src, width);
618 }
619 if (is_yuv) {
620 unpack_yuv(s, p, dst, strip_start + line);
621 line += s->subsampling[1] - 1;
622 }
623 dst += stride;
624 src += width;
625 }
626 av_free(buf);
627 return 0;
628 }
629 #endif
630
631 18 static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
632 const uint8_t *src, int size, int width, int lines)
633 {
634 int line;
635 int ret;
636
637
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (s->fill_order) {
638
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = deinvert_buffer(s, src, size)) < 0)
639 return ret;
640 18 src = s->deinvert_buf;
641 }
642 18 ret = ff_ccitt_unpack(s->avctx, src, size, dst, lines, stride,
643 s->compr, s->fax_opts);
644
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
645 for (line = 0; line < lines; line++) {
646 horizontal_fill(s, s->bpp, dst, 1, dst, 0, width, 0);
647 dst += stride;
648 }
649 18 return ret;
650 }
651
652 static int dng_decode_jpeg(AVCodecContext *avctx, AVFrame *frame,
653 int tile_byte_count, int dst_x, int dst_y, int w, int h)
654 {
655 TiffContext *s = avctx->priv_data;
656 uint8_t *dst_data, *src_data;
657 uint32_t dst_offset; /* offset from dst buffer in pixels */
658 int is_single_comp, is_u16, pixel_size;
659 int ret;
660
661 if (tile_byte_count < 0 || tile_byte_count > bytestream2_get_bytes_left(&s->gb))
662 return AVERROR_INVALIDDATA;
663
664 /* Prepare a packet and send to the MJPEG decoder */
665 av_packet_unref(s->jpkt);
666 s->jpkt->data = (uint8_t*)s->gb.buffer;
667 s->jpkt->size = tile_byte_count;
668
669 if (s->is_bayer) {
670 MJpegDecodeContext *mjpegdecctx = s->avctx_mjpeg->priv_data;
671 /* We have to set this information here, there is no way to know if a given JPEG is a DNG-embedded
672 image or not from its own data (and we need that information when decoding it). */
673 mjpegdecctx->bayer = 1;
674 }
675
676 ret = avcodec_send_packet(s->avctx_mjpeg, s->jpkt);
677 if (ret < 0) {
678 av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
679 return ret;
680 }
681
682 ret = avcodec_receive_frame(s->avctx_mjpeg, s->jpgframe);
683 if (ret < 0) {
684 av_log(avctx, AV_LOG_ERROR, "JPEG decoding error: %s.\n", av_err2str(ret));
685
686 /* Normally skip, error if explode */
687 if (avctx->err_recognition & AV_EF_EXPLODE)
688 return AVERROR_INVALIDDATA;
689 else
690 return 0;
691 }
692
693 is_u16 = (s->bpp > 8);
694
695 /* Copy the outputted tile's pixels from 'jpgframe' to 'frame' (final buffer) */
696
697 if (s->jpgframe->width != s->avctx_mjpeg->width ||
698 s->jpgframe->height != s->avctx_mjpeg->height ||
699 s->jpgframe->format != s->avctx_mjpeg->pix_fmt)
700 return AVERROR_INVALIDDATA;
701
702 /* See dng_blit for explanation */
703 if (s->avctx_mjpeg->width == w * 2 &&
704 s->avctx_mjpeg->height == h / 2 &&
705 s->avctx_mjpeg->pix_fmt == AV_PIX_FMT_GRAY16LE) {
706 is_single_comp = 1;
707 } else if (s->avctx_mjpeg->width >= w &&
708 s->avctx_mjpeg->height >= h &&
709 s->avctx_mjpeg->pix_fmt == (is_u16 ? AV_PIX_FMT_GRAY16 : AV_PIX_FMT_GRAY8)
710 ) {
711 is_single_comp = 0;
712 } else
713 return AVERROR_INVALIDDATA;
714
715 pixel_size = (is_u16 ? sizeof(uint16_t) : sizeof(uint8_t));
716
717 if (is_single_comp && !is_u16) {
718 av_log(s->avctx, AV_LOG_ERROR, "DNGs with bpp <= 8 and 1 component are unsupported\n");
719 av_frame_unref(s->jpgframe);
720 return AVERROR_PATCHWELCOME;
721 }
722
723 dst_offset = dst_x + frame->linesize[0] * dst_y / pixel_size;
724 dst_data = frame->data[0] + dst_offset * pixel_size;
725 src_data = s->jpgframe->data[0];
726
727 dng_blit(s,
728 dst_data,
729 frame->linesize[0] / pixel_size,
730 src_data,
731 s->jpgframe->linesize[0] / pixel_size,
732 w,
733 h,
734 is_single_comp,
735 is_u16, 0);
736
737 av_frame_unref(s->jpgframe);
738
739 return 0;
740 }
741
742 579 static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
743 const uint8_t *src, int size, int strip_start, int lines)
744 {
745 PutByteContext pb;
746 int c, line, pixels, code, ret;
747 579 const uint8_t *ssrc = src;
748 579 int width = ((s->width * s->bpp) + 7) >> 3;
749 579 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(p->format);
750 1176 int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
751
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 561 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
579 (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
752 desc->nb_components >= 3;
753 int is_dng;
754
755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
579 if (s->planar)
756 width /= s->bppcount;
757
758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
579 if (size <= 0)
759 return AVERROR_INVALIDDATA;
760
761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
579 if (is_yuv) {
762 int bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
763 s->subsampling[0] * s->subsampling[1] + 7) >> 3;
764 av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
765 if (s->yuv_line == NULL) {
766 av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
767 return AVERROR(ENOMEM);
768 }
769 dst = s->yuv_line;
770 stride = 0;
771
772 width = (s->width - 1) / s->subsampling[0] + 1;
773 width = width * s->subsampling[0] * s->subsampling[1] + 2*width;
774 av_assert0(width <= bytes_per_row);
775 av_assert0(s->bpp == 24);
776 }
777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
579 if (s->is_bayer) {
778 av_assert0(width == (s->bpp * s->width + 7) >> 3);
779 }
780
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
579 av_assert0(!(s->is_bayer && is_yuv));
781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
579 if (p->format == AV_PIX_FMT_GRAY12) {
782 av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, width);
783 if (s->yuv_line == NULL) {
784 av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
785 return AVERROR(ENOMEM);
786 }
787 dst = s->yuv_line;
788 stride = 0;
789 }
790
791
3/4
✓ Branch 0 taken 579 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 577 times.
579 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
792 #if CONFIG_ZLIB
793 2 return tiff_unpack_zlib(s, p, dst, stride, src, size, width, lines,
794 strip_start, is_yuv);
795 #else
796 av_log(s->avctx, AV_LOG_ERROR,
797 "zlib support not enabled, "
798 "deflate compression not supported\n");
799 return AVERROR(ENOSYS);
800 #endif
801 }
802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 577 times.
577 if (s->compr == TIFF_LZMA) {
803 #if CONFIG_LZMA
804 return tiff_unpack_lzma(s, p, dst, stride, src, size, width, lines,
805 strip_start, is_yuv);
806 #else
807 av_log(s->avctx, AV_LOG_ERROR,
808 "LZMA support not enabled\n");
809 return AVERROR(ENOSYS);
810 #endif
811 }
812
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 575 times.
577 if (s->compr == TIFF_LZW) {
813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->fill_order) {
814 if ((ret = deinvert_buffer(s, src, size)) < 0)
815 return ret;
816 ssrc = src = s->deinvert_buf;
817 }
818
2/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2 if (size > 1 && !src[0] && (src[1]&1)) {
819 av_log(s->avctx, AV_LOG_ERROR, "Old style LZW is unsupported\n");
820 }
821
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
822 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
823 return ret;
824 }
825
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (line = 0; line < lines; line++) {
826 16 pixels = ff_lzw_decode(s->lzw, dst, width);
827
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (pixels < width) {
828 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
829 pixels, width);
830 return AVERROR_INVALIDDATA;
831 }
832
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16 if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
833 horizontal_fill(s, s->bpp, dst, 1, dst, 0, width, 0);
834
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (is_yuv) {
835 unpack_yuv(s, p, dst, strip_start + line);
836 line += s->subsampling[1] - 1;
837
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (p->format == AV_PIX_FMT_GRAY12) {
838 unpack_gray(s, p, dst, strip_start + line, width, s->bpp);
839 }
840 16 dst += stride;
841 }
842 2 return 0;
843 }
844
1/2
✓ Branch 0 taken 575 times.
✗ Branch 1 not taken.
575 if (s->compr == TIFF_CCITT_RLE ||
845
2/2
✓ Branch 0 taken 557 times.
✓ Branch 1 taken 18 times.
575 s->compr == TIFF_G3 ||
846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 557 times.
557 s->compr == TIFF_G4) {
847
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if (is_yuv || p->format == AV_PIX_FMT_GRAY12)
848 return AVERROR_INVALIDDATA;
849
850 18 return tiff_unpack_fax(s, dst, stride, src, size, width, lines);
851 }
852
853 557 bytestream2_init(&s->gb, src, size);
854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 557 times.
557 bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * lines));
855
856
2/4
✓ Branch 0 taken 557 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 557 times.
557 is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
857
858 /* Decode JPEG-encoded DNGs with strips */
859
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 557 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
557 if (s->compr == TIFF_NEWJPEG && is_dng) {
860 if (s->strips > 1) {
861 av_log(s->avctx, AV_LOG_ERROR, "More than one DNG JPEG strips unsupported\n");
862 return AVERROR_PATCHWELCOME;
863 }
864 if (!s->is_bayer)
865 return AVERROR_PATCHWELCOME;
866 if ((ret = dng_decode_jpeg(s->avctx, p, s->stripsize, 0, 0, s->width, s->height)) < 0)
867 return ret;
868 return 0;
869 }
870
871
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 557 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
557 if (is_dng && stride == 0)
872 return AVERROR_INVALIDDATA;
873
874
2/2
✓ Branch 0 taken 3872 times.
✓ Branch 1 taken 557 times.
4429 for (line = 0; line < lines; line++) {
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3872 times.
3872 if (src - ssrc > size) {
876 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
877 return AVERROR_INVALIDDATA;
878 }
879
880
2/4
✓ Branch 1 taken 3872 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3872 times.
✗ Branch 5 not taken.
3872 if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
881 break;
882 3872 bytestream2_seek_p(&pb, stride * line, SEEK_SET);
883
2/3
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 3744 times.
✗ Branch 2 not taken.
3872 switch (s->compr) {
884 128 case TIFF_RAW:
885
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
128 if (ssrc + size - src < width)
886 return AVERROR_INVALIDDATA;
887
888
1/2
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
128 if (!s->fill_order) {
889
2/4
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 128 times.
128 horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 || s->is_bayer),
890 dst, 1, src, 0, width, 0);
891 } else {
892 int i;
893 for (i = 0; i < width; i++)
894 dst[i] = ff_reverse[src[i]];
895 }
896
897 /* Color processing for DNG images with uncompressed strips (non-tiled) */
898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
128 if (is_dng) {
899 int is_u16, pixel_size_bytes, pixel_size_bits, elements;
900
901 is_u16 = (s->bpp / s->bppcount > 8);
902 pixel_size_bits = (is_u16 ? 16 : 8);
903 pixel_size_bytes = (is_u16 ? sizeof(uint16_t) : sizeof(uint8_t));
904
905 elements = width / pixel_size_bytes * pixel_size_bits / s->bpp * s->bppcount; // need to account for [1, 16] bpp
906 av_assert0 (elements * pixel_size_bytes <= FFABS(stride));
907 dng_blit(s,
908 dst,
909 0, // no stride, only 1 line
910 dst,
911 0, // no stride, only 1 line
912 elements,
913 1,
914 0, // single-component variation is only preset in JPEG-encoded DNGs
915 is_u16,
916 (line + strip_start)&1);
917 }
918
919 128 src += width;
920 128 break;
921 3744 case TIFF_PACKBITS:
922
2/2
✓ Branch 0 taken 36356 times.
✓ Branch 1 taken 3744 times.
40100 for (pixels = 0; pixels < width;) {
923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36356 times.
36356 if (ssrc + size - src < 2) {
924 av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
925 return AVERROR_INVALIDDATA;
926 }
927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36356 times.
36356 code = s->fill_order ? (int8_t) ff_reverse[*src++]: (int8_t) *src++;
928
2/2
✓ Branch 0 taken 34241 times.
✓ Branch 1 taken 2115 times.
36356 if (code >= 0) {
929 34241 code++;
930
1/2
✓ Branch 0 taken 34241 times.
✗ Branch 1 not taken.
34241 if (pixels + code > width ||
931
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34241 times.
34241 ssrc + size - src < code) {
932 av_log(s->avctx, AV_LOG_ERROR,
933 "Copy went out of bounds\n");
934 return AVERROR_INVALIDDATA;
935 }
936 34241 horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
937 dst, 1, src, 0, code, pixels);
938 34241 src += code;
939 34241 pixels += code;
940
1/2
✓ Branch 0 taken 2115 times.
✗ Branch 1 not taken.
2115 } else if (code != -128) { // -127..-1
941 2115 code = (-code) + 1;
942
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2115 times.
2115 if (pixels + code > width) {
943 av_log(s->avctx, AV_LOG_ERROR,
944 "Run went out of bounds\n");
945 return AVERROR_INVALIDDATA;
946 }
947 2115 c = *src++;
948 2115 horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
949 dst, 0, NULL, c, code, pixels);
950 2115 pixels += code;
951 }
952 }
953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3744 times.
3744 if (s->fill_order) {
954 int i;
955 for (i = 0; i < width; i++)
956 dst[i] = ff_reverse[dst[i]];
957 }
958 3744 break;
959 }
960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3872 times.
3872 if (is_yuv) {
961 unpack_yuv(s, p, dst, strip_start + line);
962 line += s->subsampling[1] - 1;
963
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3872 times.
3872 } else if (p->format == AV_PIX_FMT_GRAY12) {
964 unpack_gray(s, p, dst, strip_start + line, width, s->bpp);
965 }
966 3872 dst += stride;
967 }
968 557 return 0;
969 }
970
971 static int dng_decode_tiles(AVCodecContext *avctx, AVFrame *frame,
972 const AVPacket *avpkt)
973 {
974 TiffContext *s = avctx->priv_data;
975 int tile_idx;
976 int tile_offset_offset, tile_offset;
977 int tile_byte_count_offset, tile_byte_count;
978 int tile_count_x, tile_count_y;
979 int tile_width, tile_length;
980 int has_width_leftover, has_height_leftover;
981 int tile_x = 0, tile_y = 0;
982 int pos_x = 0, pos_y = 0;
983 int ret;
984
985 if (s->tile_width <= 0 || s->tile_length <= 0)
986 return AVERROR_INVALIDDATA;
987
988 has_width_leftover = (s->width % s->tile_width != 0);
989 has_height_leftover = (s->height % s->tile_length != 0);
990
991 /* Calculate tile counts (round up) */
992 tile_count_x = (s->width + s->tile_width - 1) / s->tile_width;
993 tile_count_y = (s->height + s->tile_length - 1) / s->tile_length;
994
995 /* Iterate over the number of tiles */
996 for (tile_idx = 0; tile_idx < tile_count_x * tile_count_y; tile_idx++) {
997 tile_x = tile_idx % tile_count_x;
998 tile_y = tile_idx / tile_count_x;
999
1000 if (has_width_leftover && tile_x == tile_count_x - 1) // If on the right-most tile
1001 tile_width = s->width % s->tile_width;
1002 else
1003 tile_width = s->tile_width;
1004
1005 if (has_height_leftover && tile_y == tile_count_y - 1) // If on the bottom-most tile
1006 tile_length = s->height % s->tile_length;
1007 else
1008 tile_length = s->tile_length;
1009
1010 /* Read tile offset */
1011 tile_offset_offset = s->tile_offsets_offset + tile_idx * sizeof(int);
1012 bytestream2_seek(&s->gb, tile_offset_offset, SEEK_SET);
1013 tile_offset = ff_tget_long(&s->gb, s->le);
1014
1015 /* Read tile byte size */
1016 tile_byte_count_offset = s->tile_byte_counts_offset + tile_idx * sizeof(int);
1017 bytestream2_seek(&s->gb, tile_byte_count_offset, SEEK_SET);
1018 tile_byte_count = ff_tget_long(&s->gb, s->le);
1019
1020 /* Seek to tile data */
1021 bytestream2_seek(&s->gb, tile_offset, SEEK_SET);
1022
1023 /* Decode JPEG tile and copy it in the reference frame */
1024 ret = dng_decode_jpeg(avctx, frame, tile_byte_count, pos_x, pos_y, tile_width, tile_length);
1025
1026 if (ret < 0)
1027 return ret;
1028
1029 /* Advance current positions */
1030 pos_x += tile_width;
1031 if (tile_x == tile_count_x - 1) { // If on the right edge
1032 pos_x = 0;
1033 pos_y += tile_length;
1034 }
1035 }
1036
1037 /* Frame is ready to be output */
1038 frame->pict_type = AV_PICTURE_TYPE_I;
1039 frame->flags |= AV_FRAME_FLAG_KEY;
1040
1041 return avpkt->size;
1042 }
1043
1044 33 static int init_image(TiffContext *s, AVFrame *frame)
1045 {
1046 int ret;
1047 33 int create_gray_palette = 0;
1048
1049 // make sure there is no aliasing in the following switch
1050
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 if (s->bpp > 128 || s->bppcount >= 10) {
1051 av_log(s->avctx, AV_LOG_ERROR,
1052 "Unsupported image parameters: bpp=%d, bppcount=%d\n",
1053 s->bpp, s->bppcount);
1054 return AVERROR_INVALIDDATA;
1055 }
1056
1057
4/23
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 17 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 6 times.
✓ Branch 19 taken 6 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
33 switch (s->planar * 10000 + s->bpp * 10 + s->bppcount + s->is_bayer * 100000) {
1058 4 case 11:
1059
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!s->palette_is_set) {
1060 4 s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
1061 4 break;
1062 }
1063 case 21:
1064 case 41:
1065 s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
1066 if (!s->palette_is_set) {
1067 create_gray_palette = 1;
1068 }
1069 break;
1070 case 81:
1071 s->avctx->pix_fmt = s->palette_is_set ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
1072 break;
1073 case 121:
1074 s->avctx->pix_fmt = AV_PIX_FMT_GRAY12;
1075 break;
1076 case 100081:
1077 switch (AV_RL32(s->pattern)) {
1078 case 0x02010100:
1079 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB8;
1080 break;
1081 case 0x00010102:
1082 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR8;
1083 break;
1084 case 0x01000201:
1085 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG8;
1086 break;
1087 case 0x01020001:
1088 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG8;
1089 break;
1090 default:
1091 av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n",
1092 AV_RL32(s->pattern));
1093 return AVERROR_PATCHWELCOME;
1094 }
1095 break;
1096 case 100101:
1097 case 100121:
1098 case 100141:
1099 case 100161:
1100 switch (AV_RL32(s->pattern)) {
1101 case 0x02010100:
1102 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB16;
1103 break;
1104 case 0x00010102:
1105 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR16;
1106 break;
1107 case 0x01000201:
1108 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG16;
1109 break;
1110 case 0x01020001:
1111 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG16;
1112 break;
1113 default:
1114 av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n",
1115 AV_RL32(s->pattern));
1116 return AVERROR_PATCHWELCOME;
1117 }
1118 break;
1119 17 case 243:
1120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1121 if (s->subsampling[0] == 1 && s->subsampling[1] == 1) {
1122 s->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
1123 } else if (s->subsampling[0] == 2 && s->subsampling[1] == 1) {
1124 s->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
1125 } else if (s->subsampling[0] == 4 && s->subsampling[1] == 1) {
1126 s->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
1127 } else if (s->subsampling[0] == 1 && s->subsampling[1] == 2) {
1128 s->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
1129 } else if (s->subsampling[0] == 2 && s->subsampling[1] == 2) {
1130 s->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1131 } else if (s->subsampling[0] == 4 && s->subsampling[1] == 4) {
1132 s->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
1133 } else {
1134 av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr subsampling\n");
1135 return AVERROR_PATCHWELCOME;
1136 }
1137 } else
1138 17 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
1139 17 break;
1140 case 161:
1141 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
1142 break;
1143 case 162:
1144 s->avctx->pix_fmt = AV_PIX_FMT_YA8;
1145 break;
1146 case 322:
1147 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YA16LE : AV_PIX_FMT_YA16BE;
1148 break;
1149 case 324:
1150 s->avctx->pix_fmt = s->photometric == TIFF_PHOTOMETRIC_SEPARATED ? AV_PIX_FMT_RGB0 : AV_PIX_FMT_RGBA;
1151 break;
1152 case 405:
1153 if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED)
1154 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
1155 else {
1156 av_log(s->avctx, AV_LOG_ERROR,
1157 "bpp=40 without PHOTOMETRIC_SEPARATED is unsupported\n");
1158 return AVERROR_PATCHWELCOME;
1159 }
1160 break;
1161 case 483:
1162 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
1163 break;
1164 case 644:
1165 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGBA64BE;
1166 break;
1167 case 10243:
1168 s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
1169 break;
1170 case 10324:
1171 s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
1172 break;
1173 case 10483:
1174 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRP16LE : AV_PIX_FMT_GBRP16BE;
1175 break;
1176 case 10644:
1177 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAP16LE : AV_PIX_FMT_GBRAP16BE;
1178 break;
1179 6 case 963:
1180
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBF32LE : AV_PIX_FMT_RGBF32BE;
1181 6 break;
1182 6 case 1284:
1183
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBAF32LE : AV_PIX_FMT_RGBAF32BE;
1184 6 break;
1185 case 10963:
1186 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRPF32LE : AV_PIX_FMT_GBRPF32BE;
1187 break;
1188 case 11284:
1189 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAPF32LE : AV_PIX_FMT_GBRAPF32BE;
1190 break;
1191 default:
1192 av_log(s->avctx, AV_LOG_ERROR,
1193 "This format is not supported (bpp=%d, bppcount=%d)\n",
1194 s->bpp, s->bppcount);
1195 return AVERROR_INVALIDDATA;
1196 }
1197
1198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1199 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1200 if((desc->flags & AV_PIX_FMT_FLAG_RGB) ||
1201 !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
1202 desc->nb_components < 3) {
1203 av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr variant\n");
1204 return AVERROR_INVALIDDATA;
1205 }
1206 }
1207
1208
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
33 if (s->width != s->avctx->width || s->height != s->avctx->height) {
1209 11 ret = ff_set_dimensions(s->avctx, s->width, s->height);
1210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ret < 0)
1211 return ret;
1212 }
1213
1214
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 22 times.
33 if (s->avctx->skip_frame >= AVDISCARD_ALL)
1215 11 return 0;
1216
1217
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 if ((ret = ff_thread_get_buffer(s->avctx, frame, 0)) < 0)
1218 return ret;
1219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1220 if (!create_gray_palette)
1221 memcpy(frame->data[1], s->palette, sizeof(s->palette));
1222 else {
1223 /* make default grayscale pal */
1224 int i;
1225 uint32_t *pal = (uint32_t *)frame->data[1];
1226 for (i = 0; i < 1<<s->bpp; i++)
1227 pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
1228 }
1229 }
1230 22 return 1;
1231 }
1232
1233 42 static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
1234 {
1235
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
42 int offset = tag == TIFF_YRES ? 2 : 0;
1236 42 s->res[offset++] = num;
1237 42 s->res[offset] = den;
1238
5/8
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
42 if (s->res[0] && s->res[1] && s->res[2] && s->res[3]) {
1239 33 uint64_t num = s->res[2] * (uint64_t)s->res[1];
1240 33 uint64_t den = s->res[0] * (uint64_t)s->res[3];
1241
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 if (num > INT64_MAX || den > INT64_MAX) {
1242 num = num >> 1;
1243 den = den >> 1;
1244 }
1245 33 av_reduce(&s->avctx->sample_aspect_ratio.num, &s->avctx->sample_aspect_ratio.den,
1246 num, den, INT32_MAX);
1247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (!s->avctx->sample_aspect_ratio.den)
1248 s->avctx->sample_aspect_ratio = (AVRational) {0, 1};
1249 }
1250 42 }
1251
1252 519 static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
1253 {
1254 AVFrameSideData *sd;
1255 GetByteContext gb_temp;
1256 519 unsigned tag, type, count, off, value = 0, value2 = 1; // value2 is a denominator so init. to 1
1257 int i, start;
1258 int pos;
1259 int ret;
1260 double *dp;
1261
1262 519 ret = ff_tread_tag(&s->gb, s->le, &tag, &type, &count, &start);
1263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
519 if (ret < 0) {
1264 goto end;
1265 }
1266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
519 if (tag <= s->last_tag)
1267 return AVERROR_INVALIDDATA;
1268
1269 // We ignore TIFF_STRIP_SIZE as it is sometimes in the logic but wrong order around TIFF_STRIP_OFFS
1270
2/2
✓ Branch 0 taken 486 times.
✓ Branch 1 taken 33 times.
519 if (tag != TIFF_STRIP_SIZE)
1271 486 s->last_tag = tag;
1272
1273 519 off = bytestream2_tell(&s->gb);
1274
2/2
✓ Branch 0 taken 374 times.
✓ Branch 1 taken 145 times.
519 if (count == 1) {
1275
2/4
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
374 switch (type) {
1276 332 case TIFF_BYTE:
1277 case TIFF_SHORT:
1278 case TIFF_LONG:
1279 332 value = ff_tget(&s->gb, type, s->le);
1280 332 break;
1281 42 case TIFF_RATIONAL:
1282 42 value = ff_tget(&s->gb, TIFF_LONG, s->le);
1283 42 value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!value2) {
1285 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator in rational\n");
1286 value2 = 1;
1287 }
1288
1289 42 break;
1290 case TIFF_STRING:
1291 if (count <= 4) {
1292 break;
1293 }
1294 default:
1295 value = UINT_MAX;
1296 }
1297 }
1298
1299
20/54
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 33 times.
✓ Branch 4 taken 33 times.
✓ Branch 5 taken 33 times.
✓ Branch 6 taken 33 times.
✓ Branch 7 taken 33 times.
✓ Branch 8 taken 33 times.
✓ Branch 9 taken 42 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 8 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 33 times.
✓ Branch 22 taken 16 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 18 times.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✓ Branch 37 taken 2 times.
✓ Branch 38 taken 6 times.
✗ Branch 39 not taken.
✓ Branch 40 taken 4 times.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✓ Branch 44 taken 12 times.
✓ Branch 45 taken 2 times.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✓ Branch 53 taken 97 times.
519 switch (tag) {
1300 15 case TIFF_SUBFILE:
1301 15 s->is_thumbnail = (value != 0);
1302 15 break;
1303 33 case TIFF_WIDTH:
1304 33 s->width = value;
1305 33 break;
1306 33 case TIFF_HEIGHT:
1307 33 s->height = value;
1308 33 break;
1309 33 case TIFF_BPP:
1310
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 if (count > 5 || count <= 0) {
1311 av_log(s->avctx, AV_LOG_ERROR,
1312 "This format is not supported (bpp=%d, %d components)\n",
1313 value, count);
1314 return AVERROR_INVALIDDATA;
1315 }
1316 33 s->bppcount = count;
1317
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 29 times.
33 if (count == 1)
1318 4 s->bpp = value;
1319 else {
1320
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 switch (type) {
1321 29 case TIFF_BYTE:
1322 case TIFF_SHORT:
1323 case TIFF_LONG:
1324 29 s->bpp = 0;
1325
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
29 if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
1326 return AVERROR_INVALIDDATA;
1327
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 29 times.
122 for (i = 0; i < count; i++)
1328 93 s->bpp += ff_tget(&s->gb, type, s->le);
1329 29 break;
1330 default:
1331 s->bpp = -1;
1332 }
1333 }
1334 33 break;
1335 33 case TIFF_SAMPLES_PER_PIXEL:
1336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (count != 1) {
1337 av_log(s->avctx, AV_LOG_ERROR,
1338 "Samples per pixel requires a single value, many provided\n");
1339 return AVERROR_INVALIDDATA;
1340 }
1341
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 if (value > 5 || value <= 0) {
1342 av_log(s->avctx, AV_LOG_ERROR,
1343 "Invalid samples per pixel %d\n", value);
1344 return AVERROR_INVALIDDATA;
1345 }
1346
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 29 times.
33 if (s->bppcount == 1)
1347 4 s->bpp *= value;
1348 33 s->bppcount = value;
1349 33 break;
1350 33 case TIFF_COMPR:
1351 33 s->compr = value;
1352 33 av_log(s->avctx, AV_LOG_DEBUG, "compression: %d\n", s->compr);
1353 33 s->predictor = 0;
1354
3/6
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
33 switch (s->compr) {
1355 25 case TIFF_RAW:
1356 case TIFF_PACKBITS:
1357 case TIFF_LZW:
1358 case TIFF_CCITT_RLE:
1359 25 break;
1360 4 case TIFF_G3:
1361 case TIFF_G4:
1362 4 s->fax_opts = 0;
1363 4 break;
1364 4 case TIFF_DEFLATE:
1365 case TIFF_ADOBE_DEFLATE:
1366 #if CONFIG_ZLIB
1367 4 break;
1368 #else
1369 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
1370 return AVERROR(ENOSYS);
1371 #endif
1372 case TIFF_JPEG:
1373 case TIFF_NEWJPEG:
1374 s->is_jpeg = 1;
1375 break;
1376 case TIFF_LZMA:
1377 #if CONFIG_LZMA
1378 break;
1379 #else
1380 av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n");
1381 return AVERROR(ENOSYS);
1382 #endif
1383 default:
1384 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
1385 s->compr);
1386 return AVERROR_INVALIDDATA;
1387 }
1388 33 break;
1389 33 case TIFF_ROWSPERSTRIP:
1390
4/6
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
33 if (!value || (type == TIFF_LONG && value == UINT_MAX))
1391 value = s->height;
1392 33 s->rps = FFMIN(value, s->height);
1393 33 break;
1394 33 case TIFF_STRIP_OFFS:
1395
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 19 times.
33 if (count == 1) {
1396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (value > INT_MAX) {
1397 av_log(s->avctx, AV_LOG_ERROR,
1398 "strippos %u too large\n", value);
1399 return AVERROR_INVALIDDATA;
1400 }
1401 14 s->strippos = 0;
1402 14 s->stripoff = value;
1403 } else
1404 19 s->strippos = off;
1405 33 s->strips = count;
1406
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 31 times.
33 if (s->strips == s->bppcount)
1407 2 s->rps = s->height;
1408 33 s->sot = type;
1409 33 break;
1410 33 case TIFF_STRIP_SIZE:
1411
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 19 times.
33 if (count == 1) {
1412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (value > INT_MAX) {
1413 av_log(s->avctx, AV_LOG_ERROR,
1414 "stripsize %u too large\n", value);
1415 return AVERROR_INVALIDDATA;
1416 }
1417 14 s->stripsizesoff = 0;
1418 14 s->stripsize = value;
1419 14 s->strips = 1;
1420 } else {
1421 19 s->stripsizesoff = off;
1422 }
1423 33 s->strips = count;
1424 33 s->sstype = type;
1425 33 break;
1426 42 case TIFF_XRES:
1427 case TIFF_YRES:
1428 42 set_sar(s, tag, value, value2);
1429 42 break;
1430 case TIFF_TILE_OFFSETS:
1431 s->tile_offsets_offset = off;
1432 s->is_tiled = 1;
1433 break;
1434 case TIFF_TILE_BYTE_COUNTS:
1435 s->tile_byte_counts_offset = off;
1436 break;
1437 case TIFF_TILE_LENGTH:
1438 s->tile_length = value;
1439 break;
1440 case TIFF_TILE_WIDTH:
1441 s->tile_width = value;
1442 break;
1443 8 case TIFF_PREDICTOR:
1444 8 s->predictor = value;
1445 8 break;
1446 case TIFF_SUB_IFDS:
1447 if (count == 1)
1448 s->sub_ifd = value;
1449 else if (count > 1)
1450 s->sub_ifd = ff_tget(&s->gb, TIFF_LONG, s->le); /** Only get the first SubIFD */
1451 break;
1452 case TIFF_GRAY_RESPONSE_CURVE:
1453 case DNG_LINEARIZATION_TABLE:
1454 if (count < 1 || count > FF_ARRAY_ELEMS(s->dng_lut))
1455 return AVERROR_INVALIDDATA;
1456 for (int i = 0; i < count; i++)
1457 s->dng_lut[i] = ff_tget(&s->gb, type, s->le);
1458 s->white_level = s->dng_lut[count-1];
1459 break;
1460 case DNG_BLACK_LEVEL:
1461 if (count > FF_ARRAY_ELEMS(s->black_level))
1462 return AVERROR_INVALIDDATA;
1463 s->black_level[0] = value / (float)value2;
1464 for (int i = 0; i < count && count > 1; i++) {
1465 if (type == TIFF_RATIONAL) {
1466 value = ff_tget(&s->gb, TIFF_LONG, s->le);
1467 value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1468 if (!value2) {
1469 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1470 value2 = 1;
1471 }
1472
1473 s->black_level[i] = value / (float)value2;
1474 } else if (type == TIFF_SRATIONAL) {
1475 int value = ff_tget(&s->gb, TIFF_LONG, s->le);
1476 int value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1477 if (!value2) {
1478 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1479 value2 = 1;
1480 }
1481
1482 s->black_level[i] = value / (float)value2;
1483 } else {
1484 s->black_level[i] = ff_tget(&s->gb, type, s->le);
1485 }
1486 }
1487 for (int i = count; i < 4 && count > 0; i++)
1488 s->black_level[i] = s->black_level[count - 1];
1489 break;
1490 case DNG_WHITE_LEVEL:
1491 s->white_level = value;
1492 break;
1493 case TIFF_CFA_PATTERN_DIM:
1494 if (count != 2 || (ff_tget(&s->gb, type, s->le) != 2 &&
1495 ff_tget(&s->gb, type, s->le) != 2)) {
1496 av_log(s->avctx, AV_LOG_ERROR, "CFA Pattern dimensions are not 2x2\n");
1497 return AVERROR_INVALIDDATA;
1498 }
1499 break;
1500 case TIFF_CFA_PATTERN:
1501 s->is_bayer = 1;
1502 s->pattern[0] = ff_tget(&s->gb, type, s->le);
1503 s->pattern[1] = ff_tget(&s->gb, type, s->le);
1504 s->pattern[2] = ff_tget(&s->gb, type, s->le);
1505 s->pattern[3] = ff_tget(&s->gb, type, s->le);
1506 break;
1507
1/3
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
33 case TIFF_PHOTOMETRIC:
1508 switch (value) {
1509 33 case TIFF_PHOTOMETRIC_WHITE_IS_ZERO:
1510 case TIFF_PHOTOMETRIC_BLACK_IS_ZERO:
1511 case TIFF_PHOTOMETRIC_RGB:
1512 case TIFF_PHOTOMETRIC_PALETTE:
1513 case TIFF_PHOTOMETRIC_SEPARATED:
1514 case TIFF_PHOTOMETRIC_YCBCR:
1515 case TIFF_PHOTOMETRIC_CFA:
1516 case TIFF_PHOTOMETRIC_LINEAR_RAW: // Used by DNG images
1517 33 s->photometric = value;
1518 33 break;
1519 case TIFF_PHOTOMETRIC_ALPHA_MASK:
1520 case TIFF_PHOTOMETRIC_CIE_LAB:
1521 case TIFF_PHOTOMETRIC_ICC_LAB:
1522 case TIFF_PHOTOMETRIC_ITU_LAB:
1523 case TIFF_PHOTOMETRIC_LOG_L:
1524 case TIFF_PHOTOMETRIC_LOG_LUV:
1525 avpriv_report_missing_feature(s->avctx,
1526 "PhotometricInterpretation 0x%04X",
1527 value);
1528 return AVERROR_PATCHWELCOME;
1529 default:
1530 av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
1531 "unknown\n", value);
1532 return AVERROR_INVALIDDATA;
1533 }
1534 33 break;
1535 16 case TIFF_FILL_ORDER:
1536
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 if (value < 1 || value > 2) {
1537 av_log(s->avctx, AV_LOG_ERROR,
1538 "Unknown FillOrder value %d, trying default one\n", value);
1539 value = 1;
1540 }
1541 16 s->fill_order = value - 1;
1542 16 break;
1543 case TIFF_PAL: {
1544 GetByteContext pal_gb[3];
1545 off = type_sizes[type];
1546 if (count / 3 > 256 ||
1547 bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
1548 return AVERROR_INVALIDDATA;
1549
1550 pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
1551 bytestream2_skip(&pal_gb[1], count / 3 * off);
1552 bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
1553
1554 off = (type_sizes[type] - 1) << 3;
1555 if (off > 31U) {
1556 av_log(s->avctx, AV_LOG_ERROR, "palette shift %d is out of range\n", off);
1557 return AVERROR_INVALIDDATA;
1558 }
1559
1560 for (i = 0; i < count / 3; i++) {
1561 uint32_t p = 0xFF000000;
1562 p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16;
1563 p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8;
1564 p |= ff_tget(&pal_gb[2], type, s->le) >> off;
1565 s->palette[i] = p;
1566 }
1567 s->palette_is_set = 1;
1568 break;
1569 }
1570 18 case TIFF_PLANAR:
1571 18 s->planar = value == 2;
1572 18 break;
1573 case TIFF_YCBCR_SUBSAMPLING:
1574 if (count != 2) {
1575 av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n");
1576 return AVERROR_INVALIDDATA;
1577 }
1578 for (i = 0; i < count; i++) {
1579 s->subsampling[i] = ff_tget(&s->gb, type, s->le);
1580 if (s->subsampling[i] <= 0) {
1581 av_log(s->avctx, AV_LOG_ERROR, "subsampling %d is invalid\n", s->subsampling[i]);
1582 s->subsampling[i] = 1;
1583 return AVERROR_INVALIDDATA;
1584 }
1585 }
1586 break;
1587 case TIFF_T4OPTIONS:
1588 if (s->compr == TIFF_G3)
1589 s->fax_opts = value;
1590 break;
1591 case TIFF_T6OPTIONS:
1592 if (s->compr == TIFF_G4)
1593 s->fax_opts = value;
1594 break;
1595 #define ADD_METADATA(count, name, sep)\
1596 if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1597 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1598 goto end;\
1599 }
1600 case TIFF_MODEL_PIXEL_SCALE:
1601 ADD_METADATA(count, "ModelPixelScaleTag", NULL);
1602 break;
1603 case TIFF_MODEL_TRANSFORMATION:
1604 ADD_METADATA(count, "ModelTransformationTag", NULL);
1605 break;
1606 case TIFF_MODEL_TIEPOINT:
1607 ADD_METADATA(count, "ModelTiepointTag", NULL);
1608 break;
1609 case TIFF_GEO_KEY_DIRECTORY:
1610 if (s->geotag_count) {
1611 avpriv_request_sample(s->avctx, "Multiple geo key directories");
1612 return AVERROR_INVALIDDATA;
1613 }
1614 ADD_METADATA(1, "GeoTIFF_Version", NULL);
1615 ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
1616 s->geotag_count = ff_tget_short(&s->gb, s->le);
1617 if (s->geotag_count > count / 4 - 1) {
1618 s->geotag_count = count / 4 - 1;
1619 av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
1620 }
1621 if ( bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4
1622 || s->geotag_count == 0) {
1623 s->geotag_count = 0;
1624 return -1;
1625 }
1626 s->geotags = av_calloc(s->geotag_count, sizeof(*s->geotags));
1627 if (!s->geotags) {
1628 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1629 s->geotag_count = 0;
1630 goto end;
1631 }
1632 for (i = 0; i < s->geotag_count; i++) {
1633 s->geotags[i].key = ff_tget_short(&s->gb, s->le);
1634 s->geotags[i].type = ff_tget_short(&s->gb, s->le);
1635 s->geotags[i].count = ff_tget_short(&s->gb, s->le);
1636
1637 if (!s->geotags[i].type)
1638 s->geotags[i].val = get_geokey_val(s->geotags[i].key, ff_tget_short(&s->gb, s->le));
1639 else
1640 s->geotags[i].offset = ff_tget_short(&s->gb, s->le);
1641 }
1642 break;
1643 case TIFF_GEO_DOUBLE_PARAMS:
1644 if (count >= INT_MAX / sizeof(int64_t))
1645 return AVERROR_INVALIDDATA;
1646 if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
1647 return AVERROR_INVALIDDATA;
1648 dp = av_malloc_array(count, sizeof(double));
1649 if (!dp) {
1650 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1651 goto end;
1652 }
1653 for (i = 0; i < count; i++)
1654 dp[i] = ff_tget_double(&s->gb, s->le);
1655 for (i = 0; i < s->geotag_count; i++) {
1656 if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
1657 if (s->geotags[i].count == 0
1658 || s->geotags[i].offset + s->geotags[i].count > count) {
1659 av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1660 } else if (s->geotags[i].val) {
1661 av_log(s->avctx, AV_LOG_WARNING, "Duplicate GeoTIFF key %d\n", s->geotags[i].key);
1662 } else {
1663 char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
1664 if (!ap) {
1665 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1666 av_freep(&dp);
1667 return AVERROR(ENOMEM);
1668 }
1669 s->geotags[i].val = ap;
1670 }
1671 }
1672 }
1673 av_freep(&dp);
1674 break;
1675 case TIFF_GEO_ASCII_PARAMS:
1676 pos = bytestream2_tell(&s->gb);
1677 for (i = 0; i < s->geotag_count; i++) {
1678 if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
1679 if (s->geotags[i].count == 0
1680 || s->geotags[i].offset + s->geotags[i].count > count) {
1681 av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1682 } else {
1683 char *ap;
1684
1685 bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
1686 if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
1687 return AVERROR_INVALIDDATA;
1688 if (s->geotags[i].val)
1689 return AVERROR_INVALIDDATA;
1690 ap = av_malloc(s->geotags[i].count);
1691 if (!ap) {
1692 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1693 return AVERROR(ENOMEM);
1694 }
1695 bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
1696 ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
1697 s->geotags[i].val = ap;
1698 }
1699 }
1700 }
1701 break;
1702 case TIFF_ICC_PROFILE:
1703 gb_temp = s->gb;
1704 bytestream2_seek(&gb_temp, off, SEEK_SET);
1705
1706 if (bytestream2_get_bytes_left(&gb_temp) < count)
1707 return AVERROR_INVALIDDATA;
1708
1709 sd = av_frame_new_side_data(frame, AV_FRAME_DATA_ICC_PROFILE, count);
1710 if (!sd)
1711 return AVERROR(ENOMEM);
1712
1713 bytestream2_get_bufferu(&gb_temp, sd->data, count);
1714 break;
1715 case TIFF_ARTIST:
1716 ADD_METADATA(count, "artist", NULL);
1717 break;
1718 case TIFF_COPYRIGHT:
1719 ADD_METADATA(count, "copyright", NULL);
1720 break;
1721 2 case TIFF_DATE:
1722
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 ADD_METADATA(count, "date", NULL);
1723 2 break;
1724 6 case TIFF_DOCUMENT_NAME:
1725
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 ADD_METADATA(count, "document_name", NULL);
1726 6 break;
1727 case TIFF_HOST_COMPUTER:
1728 ADD_METADATA(count, "computer", NULL);
1729 break;
1730 4 case TIFF_IMAGE_DESCRIPTION:
1731
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 ADD_METADATA(count, "description", NULL);
1732 4 break;
1733 case TIFF_MAKE:
1734 ADD_METADATA(count, "make", NULL);
1735 break;
1736 case TIFF_MODEL:
1737 ADD_METADATA(count, "model", NULL);
1738 break;
1739 case TIFF_PAGE_NAME:
1740 ADD_METADATA(count, "page_name", NULL);
1741 break;
1742 12 case TIFF_PAGE_NUMBER:
1743
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 ADD_METADATA(count, "page_number", " / ");
1744 // need to seek back to re-read the page number
1745 12 bytestream2_seek(&s->gb, -count * sizeof(uint16_t), SEEK_CUR);
1746 // read the page number
1747 12 s->cur_page = ff_tget(&s->gb, TIFF_SHORT, s->le);
1748 // get back to where we were before the previous seek
1749 12 bytestream2_seek(&s->gb, count * sizeof(uint16_t) - sizeof(uint16_t), SEEK_CUR);
1750 12 break;
1751 2 case TIFF_SOFTWARE_NAME:
1752
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 ADD_METADATA(count, "software", NULL);
1753 2 break;
1754 case DNG_VERSION:
1755 if (count == 4) {
1756 unsigned int ver[4];
1757 ver[0] = ff_tget(&s->gb, type, s->le);
1758 ver[1] = ff_tget(&s->gb, type, s->le);
1759 ver[2] = ff_tget(&s->gb, type, s->le);
1760 ver[3] = ff_tget(&s->gb, type, s->le);
1761
1762 av_log(s->avctx, AV_LOG_DEBUG, "DNG file, version %u.%u.%u.%u\n",
1763 ver[0], ver[1], ver[2], ver[3]);
1764
1765 tiff_set_type(s, TIFF_TYPE_DNG);
1766 }
1767 break;
1768 case DNG_ANALOG_BALANCE:
1769 if (type != TIFF_RATIONAL)
1770 break;
1771
1772 for (int i = 0; i < 3; i++) {
1773 value = ff_tget(&s->gb, TIFF_LONG, s->le);
1774 value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1775 if (!value2) {
1776 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1777 value2 = 1;
1778 }
1779
1780 s->analog_balance[i] = value / (float)value2;
1781 }
1782 break;
1783 case DNG_AS_SHOT_NEUTRAL:
1784 if (type != TIFF_RATIONAL)
1785 break;
1786
1787 for (int i = 0; i < 3; i++) {
1788 value = ff_tget(&s->gb, TIFF_LONG, s->le);
1789 value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1790 if (!value2) {
1791 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1792 value2 = 1;
1793 }
1794
1795 s->as_shot_neutral[i] = value / (float)value2;
1796 }
1797 break;
1798 case DNG_AS_SHOT_WHITE_XY:
1799 if (type != TIFF_RATIONAL)
1800 break;
1801
1802 for (int i = 0; i < 2; i++) {
1803 value = ff_tget(&s->gb, TIFF_LONG, s->le);
1804 value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1805 if (!value2) {
1806 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1807 value2 = 1;
1808 }
1809
1810 s->as_shot_white[i] = value / (float)value2;
1811 }
1812 s->as_shot_white[2] = 1.f - s->as_shot_white[0] - s->as_shot_white[1];
1813 for (int i = 0; i < 3; i++) {
1814 s->as_shot_white[i] /= d65_white[i];
1815 }
1816 break;
1817 case DNG_COLOR_MATRIX1:
1818 case DNG_COLOR_MATRIX2:
1819 for (int i = 0; i < 3; i++) {
1820 for (int j = 0; j < 3; j++) {
1821 int value = ff_tget(&s->gb, TIFF_LONG, s->le);
1822 int value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1823 if (!value2) {
1824 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1825 value2 = 1;
1826 }
1827 s->color_matrix[i][j] = value / (float)value2;
1828 }
1829 s->use_color_matrix = 1;
1830 }
1831 break;
1832 case DNG_CAMERA_CALIBRATION1:
1833 case DNG_CAMERA_CALIBRATION2:
1834 for (int i = 0; i < 3; i++) {
1835 for (int j = 0; j < 3; j++) {
1836 int value = ff_tget(&s->gb, TIFF_LONG, s->le);
1837 int value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1838 if (!value2) {
1839 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1840 value2 = 1;
1841 }
1842 s->camera_calibration[i][j] = value / (float)value2;
1843 }
1844 }
1845 break;
1846 case CINEMADNG_TIME_CODES:
1847 case CINEMADNG_FRAME_RATE:
1848 case CINEMADNG_T_STOP:
1849 case CINEMADNG_REEL_NAME:
1850 case CINEMADNG_CAMERA_LABEL:
1851 tiff_set_type(s, TIFF_TYPE_CINEMADNG);
1852 break;
1853 97 default:
1854
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1855 av_log(s->avctx, AV_LOG_ERROR,
1856 "Unknown or unsupported tag %d/0x%0X\n",
1857 tag, tag);
1858 return AVERROR_INVALIDDATA;
1859 }
1860 }
1861 97 end:
1862
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
519 if (s->bpp > 128U) {
1863 av_log(s->avctx, AV_LOG_ERROR,
1864 "This format is not supported (bpp=%d, %d components)\n",
1865 s->bpp, count);
1866 s->bpp = 0;
1867 return AVERROR_INVALIDDATA;
1868 }
1869 519 bytestream2_seek(&s->gb, start, SEEK_SET);
1870 519 return 0;
1871 }
1872
1873 static const float xyz2rgb[3][3] = {
1874 { 0.412453f, 0.357580f, 0.180423f },
1875 { 0.212671f, 0.715160f, 0.072169f },
1876 { 0.019334f, 0.119193f, 0.950227f },
1877 };
1878
1879 static void camera_xyz_coeff(TiffContext *s,
1880 float rgb2cam[3][4],
1881 double cam2xyz[4][3])
1882 {
1883 double cam2rgb[4][3], num;
1884 int i, j, k;
1885
1886 for (i = 0; i < 3; i++) {
1887 for (j = 0; j < 3; j++) {
1888 cam2rgb[i][j] = 0.;
1889 for (k = 0; k < 3; k++)
1890 cam2rgb[i][j] += cam2xyz[i][k] * xyz2rgb[k][j];
1891 }
1892 }
1893
1894 for (i = 0; i < 3; i++) {
1895 for (num = j = 0; j < 3; j++)
1896 num += cam2rgb[i][j];
1897 if (!num)
1898 num = 1;
1899 for (j = 0; j < 3; j++)
1900 cam2rgb[i][j] /= num;
1901 s->premultiply[i] = 1.f / num;
1902 }
1903 }
1904
1905 33 static int decode_frame(AVCodecContext *avctx, AVFrame *p,
1906 int *got_frame, AVPacket *avpkt)
1907 {
1908 33 TiffContext *const s = avctx->priv_data;
1909 33 unsigned off, last_off = 0;
1910 int le, ret, plane, planes;
1911 int i, j, entries, stride;
1912 unsigned soff, ssize;
1913 uint8_t *dst;
1914 GetByteContext stripsizes;
1915 GetByteContext stripdata;
1916 int retry_for_subifd, retry_for_page;
1917 int is_dng;
1918 int has_tile_bits, has_strip_bits;
1919
1920 33 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1921
1922 // parse image header
1923
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
1924 av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
1925 return ret;
1926
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1927 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1928 return AVERROR_INVALIDDATA;
1929 }
1930 33 s->le = le;
1931 // TIFF_BPP is not a required tag and defaults to 1
1932
1933 33 s->tiff_type = TIFF_TYPE_TIFF;
1934 33 s->use_color_matrix = 0;
1935 33 again:
1936 33 s->is_thumbnail = 0;
1937 33 s->bppcount = s->bpp = 1;
1938 33 s->photometric = TIFF_PHOTOMETRIC_NONE;
1939 33 s->compr = TIFF_RAW;
1940 33 s->fill_order = 0;
1941 33 s->white_level = 0;
1942 33 s->is_bayer = 0;
1943 33 s->is_tiled = 0;
1944 33 s->is_jpeg = 0;
1945 33 s->cur_page = 0;
1946 33 s->last_tag = 0;
1947
1948
2/2
✓ Branch 0 taken 2162688 times.
✓ Branch 1 taken 33 times.
2162721 for (i = 0; i < 65536; i++)
1949 2162688 s->dng_lut[i] = i;
1950
1951
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
165 for (i = 0; i < FF_ARRAY_ELEMS(s->black_level); i++)
1952 132 s->black_level[i] = 0.f;
1953
1954
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
165 for (i = 0; i < FF_ARRAY_ELEMS(s->as_shot_neutral); i++)
1955 132 s->as_shot_neutral[i] = 0.f;
1956
1957
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
165 for (i = 0; i < FF_ARRAY_ELEMS(s->as_shot_white); i++)
1958 132 s->as_shot_white[i] = 1.f;
1959
1960
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
165 for (i = 0; i < FF_ARRAY_ELEMS(s->analog_balance); i++)
1961 132 s->analog_balance[i] = 1.f;
1962
1963
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
165 for (i = 0; i < FF_ARRAY_ELEMS(s->premultiply); i++)
1964 132 s->premultiply[i] = 1.f;
1965
1966
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
165 for (i = 0; i < 4; i++)
1967
2/2
✓ Branch 0 taken 528 times.
✓ Branch 1 taken 132 times.
660 for (j = 0; j < 4; j++)
1968
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 396 times.
528 s->camera_calibration[i][j] = i == j;
1969
1970 33 free_geotags(s);
1971
1972 // Reset these offsets so we can tell if they were set this frame
1973 33 s->stripsizesoff = s->strippos = 0;
1974 /* parse image file directory */
1975 33 bytestream2_seek(&s->gb, off, SEEK_SET);
1976 33 entries = ff_tget_short(&s->gb, le);
1977
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
1978 return AVERROR_INVALIDDATA;
1979
2/2
✓ Branch 0 taken 519 times.
✓ Branch 1 taken 33 times.
552 for (i = 0; i < entries; i++) {
1980
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 519 times.
519 if ((ret = tiff_decode_tag(s, p)) < 0)
1981 return ret;
1982 }
1983
1984
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
33 if (s->get_thumbnail && !s->is_thumbnail) {
1985 av_log(avctx, AV_LOG_INFO, "No embedded thumbnail present\n");
1986 return AVERROR_EOF;
1987 }
1988
1989 /** whether we should process this IFD's SubIFD */
1990
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
33 retry_for_subifd = s->sub_ifd && (s->get_subimage || (!s->get_thumbnail && s->is_thumbnail));
1991 /** whether we should process this multi-page IFD's next page */
1992
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
33 retry_for_page = s->get_page && s->cur_page + 1 < s->get_page; // get_page is 1-indexed
1993
1994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (retry_for_page) {
1995 // set offset to the next IFD
1996 off = ff_tget_long(&s->gb, le);
1997
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 } else if (retry_for_subifd) {
1998 // set offset to the SubIFD
1999 off = s->sub_ifd;
2000 }
2001
2002
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 if (retry_for_subifd || retry_for_page) {
2003 if (!off) {
2004 av_log(avctx, AV_LOG_ERROR, "Requested entry not found\n");
2005 return AVERROR_INVALIDDATA;
2006 }
2007 if (off <= last_off) {
2008 avpriv_request_sample(s->avctx, "non increasing IFD offset");
2009 return AVERROR_INVALIDDATA;
2010 }
2011 last_off = off;
2012 if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
2013 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
2014 return AVERROR_INVALIDDATA;
2015 }
2016 s->sub_ifd = 0;
2017 goto again;
2018 }
2019
2020 /* At this point we've decided on which (Sub)IFD to process */
2021
2022
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
2023
2024
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 for (i = 0; i<s->geotag_count; i++) {
2025 const char *keyname = get_geokey_name(s->geotags[i].key);
2026 if (!keyname) {
2027 av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
2028 continue;
2029 }
2030 if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
2031 av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
2032 continue;
2033 }
2034 ret = av_dict_set(&p->metadata, keyname, s->geotags[i].val, 0);
2035 if (ret<0) {
2036 av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
2037 return ret;
2038 }
2039 }
2040
2041
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (is_dng) {
2042 double cam2xyz[4][3];
2043 float cmatrix[3][4];
2044 float pmin = FLT_MAX;
2045 int bps;
2046
2047 for (i = 0; i < 3; i++) {
2048 for (j = 0; j < 3; j++)
2049 s->camera_calibration[i][j] *= s->analog_balance[i];
2050 }
2051
2052 if (!s->use_color_matrix) {
2053 for (i = 0; i < 3; i++) {
2054 if (s->camera_calibration[i][i])
2055 s->premultiply[i] /= s->camera_calibration[i][i];
2056 }
2057 } else {
2058 for (int c = 0; c < 3; c++) {
2059 for (i = 0; i < 3; i++) {
2060 cam2xyz[c][i] = 0.;
2061 for (j = 0; j < 3; j++)
2062 cam2xyz[c][i] += s->camera_calibration[c][j] * s->color_matrix[j][i] * s->as_shot_white[i];
2063 }
2064 }
2065
2066 camera_xyz_coeff(s, cmatrix, cam2xyz);
2067 }
2068
2069 for (int c = 0; c < 3; c++)
2070 pmin = fminf(pmin, s->premultiply[c]);
2071
2072 for (int c = 0;