FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tiff.c
Date: 2026-09-25 11:37:27
Exec Total Coverage
Lines: 525 1561 33.6%
Functions: 15 31 48.4%
Branches: 343 1303 26.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/attributes_internal.h"
40 #include "libavutil/avstring.h"
41 #include "libavutil/error.h"
42 #include "libavutil/intreadwrite.h"
43 #include "libavutil/mem.h"
44 #include "libavutil/opt.h"
45 #include "libavutil/reverse.h"
46 #include "avcodec.h"
47 #include "bytestream.h"
48 #include "codec_internal.h"
49 #include "decode.h"
50 #include "exif_internal.h"
51 #include "faxcompr.h"
52 #include "lzw.h"
53 #include "tiff.h"
54 #include "tiff_common.h"
55 #include "tiff_data.h"
56 #include "mjpegdec.h"
57 #include "thread.h"
58 #include "get_bits.h"
59
60 typedef struct TiffContext {
61 AVClass *class;
62 AVCodecContext *avctx;
63 GetByteContext gb;
64
65 /* JPEG decoding for DNG */
66 AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
67 AVPacket *jpkt; // encoded JPEG tile
68 AVFrame *jpgframe; // decoded JPEG tile
69
70 int get_subimage;
71 uint16_t get_page;
72 int get_thumbnail;
73
74 enum TiffType tiff_type;
75 int width, height;
76 unsigned int bpp, bppcount;
77 uint32_t palette[256];
78 int palette_is_set;
79 int le;
80 enum TiffCompr compr;
81 enum TiffPhotometric photometric;
82 int planar;
83 int subsampling[2];
84 int fax_opts;
85 int predictor;
86 int fill_order;
87 uint32_t res[4];
88 int is_thumbnail;
89 unsigned last_tag;
90
91 int is_bayer;
92 int use_color_matrix;
93 uint8_t pattern[4];
94
95 float analog_balance[4];
96 float as_shot_neutral[4];
97 float as_shot_white[4];
98 float color_matrix[3][4];
99 float camera_calibration[4][4];
100 float premultiply[4];
101 float black_level[4];
102
103 unsigned white_level;
104 uint16_t dng_lut[65536];
105
106 uint32_t sub_ifd;
107 uint16_t cur_page;
108
109 int strips, rps, sstype;
110 int sot;
111 int stripsizesoff, stripsize, stripoff, strippos;
112 LZWState *lzw;
113
114 /* Tile support */
115 int is_tiled;
116 int tile_byte_counts_offset, tile_offsets_offset;
117 int tile_width, tile_length;
118
119 int is_jpeg;
120
121 uint8_t *deinvert_buf;
122 int deinvert_buf_size;
123 uint8_t *yuv_line;
124 unsigned int yuv_line_size;
125
126 int geotag_count;
127 TiffGeoTag *geotags;
128
129 AVExifMetadata exif_meta;
130 } TiffContext;
131
132 static const float d65_white[3] = { 0.950456f, 1.f, 1.088754f };
133
134 ✗ static void tiff_set_type(TiffContext *s, enum TiffType tiff_type) {
135 ✗ if (s->tiff_type < tiff_type) // Prioritize higher-valued entries
136 ✗ s->tiff_type = tiff_type;
137 ✗ }
138
139 80 static void free_geotags(TiffContext *const s)
140 {
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 for (int i = 0; i < s->geotag_count; i++)
142 ✗ av_freep(&s->geotags[i].val);
143 80 av_freep(&s->geotags);
144 80 s->geotag_count = 0;
145 80 }
146
147 ✗ static const char *get_geokey_name(int key)
148 {
149 #define RET_GEOKEY_STR(TYPE, array)\
150 if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
151 key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(tiff_##array##_name_type_map))\
152 return tiff_##array##_name_type_string + tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].offset;
153
154 ✗ RET_GEOKEY_STR(VERT, vert);
155 ✗ RET_GEOKEY_STR(PROJ, proj);
156 ✗ RET_GEOKEY_STR(GEOG, geog);
157 ✗ RET_GEOKEY_STR(CONF, conf);
158
159 ✗ return NULL;
160 }
161
162 ✗ static int get_geokey_type(int key)
163 {
164 #define RET_GEOKEY_TYPE(TYPE, array)\
165 if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
166 key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(tiff_##array##_name_type_map))\
167 return tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].type;
168 ✗ RET_GEOKEY_TYPE(VERT, vert);
169 ✗ RET_GEOKEY_TYPE(PROJ, proj);
170 ✗ RET_GEOKEY_TYPE(GEOG, geog);
171 ✗ RET_GEOKEY_TYPE(CONF, conf);
172
173 ✗ return AVERROR_INVALIDDATA;
174 }
175
176 ✗ static int cmp_id_key(const void *id, const void *k)
177 {
178 ✗ return *(const int*)id - ((const TiffGeoTagKeyName*)k)->key;
179 }
180
181 ✗ static const char *search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
182 {
183 ✗ const TiffGeoTagKeyName *r = bsearch(&id, keys, n, sizeof(keys[0]), cmp_id_key);
184 ✗ if(r)
185 ✗ return r->name;
186
187 ✗ return NULL;
188 }
189
190 ✗ static const char *get_geokey_val(int key, uint16_t val)
191 {
192 ✗ if (val == TIFF_GEO_KEY_UNDEFINED)
193 ✗ return "undefined";
194 ✗ if (val == TIFF_GEO_KEY_USER_DEFINED)
195 ✗ return "User-Defined";
196
197 #define RET_GEOKEY_VAL(TYPE, array)\
198 if (val >= TIFF_##TYPE##_OFFSET &&\
199 val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(tiff_##array##_codes))\
200 return tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET];
201
202 ✗ switch (key) {
203 ✗ case TIFF_GT_MODEL_TYPE_GEOKEY:
204 ✗ RET_GEOKEY_VAL(GT_MODEL_TYPE, gt_model_type);
205 ✗ break;
206 ✗ case TIFF_GT_RASTER_TYPE_GEOKEY:
207 ✗ RET_GEOKEY_VAL(GT_RASTER_TYPE, gt_raster_type);
208 ✗ break;
209 ✗ case TIFF_GEOG_LINEAR_UNITS_GEOKEY:
210 case TIFF_PROJ_LINEAR_UNITS_GEOKEY:
211 case TIFF_VERTICAL_UNITS_GEOKEY:
212 ✗ RET_GEOKEY_VAL(LINEAR_UNIT, linear_unit);
213 ✗ break;
214 ✗ case TIFF_GEOG_ANGULAR_UNITS_GEOKEY:
215 case TIFF_GEOG_AZIMUTH_UNITS_GEOKEY:
216 ✗ RET_GEOKEY_VAL(ANGULAR_UNIT, angular_unit);
217 ✗ break;
218 ✗ case TIFF_GEOGRAPHIC_TYPE_GEOKEY:
219 ✗ RET_GEOKEY_VAL(GCS_TYPE, gcs_type);
220 ✗ RET_GEOKEY_VAL(GCSE_TYPE, gcse_type);
221 ✗ break;
222 ✗ case TIFF_GEOG_GEODETIC_DATUM_GEOKEY:
223 ✗ RET_GEOKEY_VAL(GEODETIC_DATUM, geodetic_datum);
224 ✗ RET_GEOKEY_VAL(GEODETIC_DATUM_E, geodetic_datum_e);
225 ✗ break;
226 ✗ case TIFF_GEOG_ELLIPSOID_GEOKEY:
227 ✗ RET_GEOKEY_VAL(ELLIPSOID, ellipsoid);
228 ✗ break;
229 ✗ case TIFF_GEOG_PRIME_MERIDIAN_GEOKEY:
230 ✗ RET_GEOKEY_VAL(PRIME_MERIDIAN, prime_meridian);
231 ✗ break;
232 ✗ case TIFF_PROJECTED_CS_TYPE_GEOKEY:
233 ✗ return search_keyval(tiff_proj_cs_type_codes, FF_ARRAY_ELEMS(tiff_proj_cs_type_codes), val);
234 ✗ case TIFF_PROJECTION_GEOKEY:
235 ✗ return search_keyval(tiff_projection_codes, FF_ARRAY_ELEMS(tiff_projection_codes), val);
236 ✗ case TIFF_PROJ_COORD_TRANS_GEOKEY:
237 ✗ RET_GEOKEY_VAL(COORD_TRANS, coord_trans);
238 ✗ break;
239 ✗ case TIFF_VERTICAL_CS_TYPE_GEOKEY:
240 ✗ RET_GEOKEY_VAL(VERT_CS, vert_cs);
241 ✗ RET_GEOKEY_VAL(ORTHO_VERT_CS, ortho_vert_cs);
242 ✗ break;
243
244 }
245
246 ✗ return NULL;
247 }
248
249 ✗ static char *doubles2str(double *dp, int count, const char *sep)
250 {
251 int i;
252 char *ap, *ap0;
253 uint64_t component_len;
254 ✗ if (!sep) sep = ", ";
255 ✗ component_len = 24LL + strlen(sep);
256 ✗ if (count >= (INT_MAX - 1)/component_len)
257 ✗ return NULL;
258 ✗ ap = av_malloc(component_len * count + 1);
259 ✗ if (!ap)
260 ✗ return NULL;
261 ✗ ap0 = ap;
262 ✗ ap[0] = '\0';
263 ✗ for (i = 0; i < count; i++) {
264 ✗ unsigned l = snprintf(ap, component_len, "%.15g%s", dp[i], sep);
265 ✗ if(l >= component_len) {
266 ✗ av_free(ap0);
267 ✗ return NULL;
268 }
269 ✗ ap += l;
270 }
271 ✗ ap0[strlen(ap0) - strlen(sep)] = '\0';
272 ✗ return ap0;
273 }
274
275 26 static int add_metadata(int count, int type,
276 const char *name, const char *sep, TiffContext *s, AVFrame *frame)
277 {
278
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
26 switch(type) {
279 ✗ case AV_TIFF_DOUBLE: return ff_tadd_doubles_metadata(count, name, sep, &s->gb, s->le, &frame->metadata);
280 12 case AV_TIFF_SHORT : return ff_tadd_shorts_metadata(count, name, sep, &s->gb, s->le, 0, &frame->metadata);
281 14 case AV_TIFF_STRING: return ff_tadd_string_metadata(count, name, &s->gb, s->le, &frame->metadata);
282 ✗ default : return AVERROR_INVALIDDATA;
283 };
284 }
285
286 /**
287 * Map stored raw sensor values into linear reference values (see: DNG Specification - Chapter 5)
288 */
289 ✗ static uint16_t av_always_inline dng_process_color16(uint16_t value,
290 const uint16_t *lut,
291 float black_level,
292 float scale_factor)
293 {
294 float value_norm;
295
296 // Lookup table lookup
297 ✗ value = lut[value];
298
299 // Black level subtraction
300 // Color scaling
301 ✗ value_norm = ((float)value - black_level) * scale_factor;
302
303 ✗ value = av_clip_uint16(lrintf(value_norm));
304
305 ✗ return value;
306 }
307
308 ✗ static uint16_t av_always_inline dng_process_color8(uint16_t value,
309 const uint16_t *lut,
310 float black_level,
311 float scale_factor)
312 {
313 ✗ return dng_process_color16(value, lut, black_level, scale_factor) >> 8;
314 }
315
316 ✗ static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
317 const uint8_t *src, int src_stride, int width, int height,
318 int is_single_comp, int is_u16, int odd_line)
319 {
320 float scale_factor[4];
321 int line, col;
322
323 ✗ if (s->is_bayer) {
324 ✗ for (int i = 0; i < 4; i++)
325 ✗ scale_factor[i] = s->premultiply[s->pattern[i]] * 65535.f / (s->white_level - s->black_level[i]);
326 } else {
327 ✗ for (int i = 0; i < 4; i++)
328 ✗ scale_factor[i] = s->premultiply[ i ] * 65535.f / (s->white_level - s->black_level[i]);
329 }
330
331 ✗ if (is_single_comp) {
332 ✗ if (!is_u16)
333 ✗ return; /* <= 8bpp unsupported */
334
335 /* Image is double the width and half the height we need, each row comprises 2 rows of the output
336 (split vertically in the middle). */
337 ✗ for (line = 0; line < height / 2; line++) {
338 ✗ uint16_t *dst_u16 = (uint16_t *)dst;
339 ✗ const uint16_t *src_u16 = (const uint16_t *)src;
340
341 /* Blit first half of input row row to initial row of output */
342 ✗ for (col = 0; col < width; col++)
343 ✗ *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level[col&1], scale_factor[col&1]);
344
345 /* Advance the destination pointer by a row (source pointer remains in the same place) */
346 ✗ dst += dst_stride * sizeof(uint16_t);
347 ✗ dst_u16 = (uint16_t *)dst;
348
349 /* Blit second half of input row row to next row of output */
350 ✗ for (col = 0; col < width; col++)
351 ✗ *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level[(col&1) + 2], scale_factor[(col&1) + 2]);
352
353 ✗ dst += dst_stride * sizeof(uint16_t);
354 ✗ src += src_stride * sizeof(uint16_t);
355 }
356 } else {
357 /* Input and output image are the same size and the MJpeg decoder has done per-component
358 deinterleaving, so blitting here is straightforward. */
359 ✗ if (is_u16) {
360 ✗ for (line = 0; line < height; line++) {
361 ✗ uint16_t *dst_u16 = (uint16_t *)dst;
362 ✗ const uint16_t *src_u16 = (const uint16_t *)src;
363
364 ✗ for (col = 0; col < width; col++)
365 ✗ *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut,
366 ✗ s->black_level[(col&1) + 2 * ((line&1) + odd_line)],
367 ✗ scale_factor[(col&1) + 2 * ((line&1) + odd_line)]);
368
369 ✗ dst += dst_stride * sizeof(uint16_t);
370 ✗ src += src_stride * sizeof(uint16_t);
371 }
372 } else {
373 ✗ for (line = 0; line < height; line++) {
374 ✗ uint8_t *dst_u8 = dst;
375 ✗ const uint8_t *src_u8 = src;
376
377 ✗ for (col = 0; col < width; col++)
378 ✗ *dst_u8++ = dng_process_color8(*src_u8++, s->dng_lut,
379 ✗ s->black_level[(col&1) + 2 * ((line&1) + odd_line)],
380 ✗ scale_factor[(col&1) + 2 * ((line&1) + odd_line)]);
381
382 ✗ dst += dst_stride;
383 ✗ src += src_stride;
384 }
385 }
386 }
387 }
388
389 43290 static void av_always_inline horizontal_fill(TiffContext *s,
390 unsigned int bpp, uint8_t* dst,
391 int usePtr, const uint8_t *src,
392 uint8_t c, int width, int offset)
393 {
394
1/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 43290 times.
43290 switch (bpp) {
395 ✗ case 1:
396 ✗ while (--width >= 0) {
397 ✗ dst[(width+offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
398 ✗ dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
399 ✗ dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
400 ✗ dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
401 ✗ dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
402 ✗ dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
403 ✗ dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
404 ✗ dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
405 }
406 ✗ break;
407 ✗ case 2:
408 ✗ while (--width >= 0) {
409 ✗ dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
410 ✗ dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
411 ✗ dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
412 ✗ dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
413 }
414 ✗ break;
415 ✗ case 4:
416 ✗ while (--width >= 0) {
417 ✗ dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
418 ✗ dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
419 }
420 ✗ break;
421 ✗ case 10:
422 case 12:
423 case 14: {
424 ✗ uint16_t *dst16 = (uint16_t *)dst;
425 ✗ int is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
426 ✗ uint8_t shift = is_dng ? 0 : 16 - bpp;
427 GetBitContext gb;
428
429 ✗ av_unused int ret = init_get_bits8(&gb, src, width);
430 av_assert1(ret >= 0);
431 ✗ for (int i = 0; i < s->width; i++) {
432 ✗ dst16[i] = get_bits(&gb, bpp) << shift;
433 }
434 }
435 ✗ break;
436 43290 default:
437
2/2
✓ Branch 0 taken 38731 times.
✓ Branch 1 taken 4559 times.
43290 if (usePtr) {
438 38731 memcpy(dst + offset, src, width);
439 } else {
440 4559 memset(dst + offset, c, width);
441 }
442 }
443 43290 }
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 ✗ av_unused int ret = init_get_bits8(&gb, src, width);
465 av_assert1(ret >= 0);
466
467 ✗ for (int i = 0; i < s->width; i++) {
468 ✗ dst[i] = get_bits(&gb, bpp);
469 }
470 ✗ }
471
472 500 static void unpack_yuv(TiffContext *s, AVFrame *p,
473 const uint8_t *src, int lnum)
474 {
475 int i, j, k;
476 500 int w = (s->width - 1) / s->subsampling[0] + 1;
477 500 uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
478 500 uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
479
2/4
✓ Branch 0 taken 500 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 500 times.
500 if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) {
480 ✗ for (i = 0; i < w; i++) {
481 ✗ for (j = 0; j < s->subsampling[1]; j++)
482 ✗ for (k = 0; k < s->subsampling[0]; k++)
483 ✗ p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
484 ✗ FFMIN(i * s->subsampling[0] + k, s->width-1)] = *src++;
485 ✗ *pu++ = *src++;
486 ✗ *pv++ = *src++;
487 }
488 }else{
489
2/2
✓ Branch 0 taken 60000 times.
✓ Branch 1 taken 500 times.
60500 for (i = 0; i < w; i++) {
490
2/2
✓ Branch 0 taken 120000 times.
✓ Branch 1 taken 60000 times.
180000 for (j = 0; j < s->subsampling[1]; j++)
491
2/2
✓ Branch 0 taken 240000 times.
✓ Branch 1 taken 120000 times.
360000 for (k = 0; k < s->subsampling[0]; k++)
492 240000 p->data[0][(lnum + j) * p->linesize[0] +
493 240000 i * s->subsampling[0] + k] = *src++;
494 60000 *pu++ = *src++;
495 60000 *pv++ = *src++;
496 }
497 }
498 500 }
499
500 #if CONFIG_ZLIB
501 2 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
502 int size)
503 {
504 2 z_stream zstream = { 0 };
505 int zret;
506
507 2 zstream.next_in = src;
508 2 zstream.avail_in = size;
509 2 zstream.next_out = dst;
510 2 zstream.avail_out = *len;
511 2 zret = inflateInit(&zstream);
512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (zret != Z_OK) {
513 ✗ av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
514 ✗ return zret;
515 }
516 2 zret = inflate(&zstream, Z_SYNC_FLUSH);
517 2 inflateEnd(&zstream);
518 2 *len = zstream.total_out;
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 return zret == Z_STREAM_END ? Z_OK : zret;
520 }
521
522 2 static int tiff_unpack_zlib(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
523 const uint8_t *src, int size, int width, int lines,
524 int strip_start, int is_yuv)
525 {
526 uint8_t *zbuf;
527 unsigned long outlen;
528 int ret, line;
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 int rows = is_yuv ? (lines + s->subsampling[1] - 1) / s->subsampling[1] : lines;
530 2 outlen = width * lines;
531 2 zbuf = av_malloc(outlen);
532
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!zbuf)
533 ✗ return AVERROR(ENOMEM);
534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->fill_order) {
535 ✗ if ((ret = deinvert_buffer(s, src, size)) < 0) {
536 ✗ av_free(zbuf);
537 ✗ return ret;
538 }
539 ✗ src = s->deinvert_buf;
540 }
541 2 ret = tiff_uncompress(zbuf, &outlen, src, size);
542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret != Z_OK) {
543 ✗ av_log(s->avctx, AV_LOG_ERROR,
544 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
545 ✗ (unsigned long)width * lines, ret);
546 ✗ av_free(zbuf);
547 ✗ return AVERROR_UNKNOWN;
548 }
549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (outlen < (unsigned long)width * rows) {
550 ✗ av_log(s->avctx, AV_LOG_ERROR, "Deflated %lu bytes, but %lu are needed\n",
551 ✗ outlen, (unsigned long)width * rows);
552 ✗ av_free(zbuf);
553 ✗ return AVERROR_INVALIDDATA;
554 }
555 2 src = zbuf;
556
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (line = 0; line < lines; line++) {
557
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) {
558 ✗ horizontal_fill(s, s->bpp, dst, 1, src, 0, width, 0);
559 } else {
560 16 memcpy(dst, src, width);
561 }
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (is_yuv) {
563 ✗ unpack_yuv(s, p, dst, strip_start + line);
564 ✗ line += s->subsampling[1] - 1;
565 }
566 16 dst += stride;
567 16 src += width;
568 }
569 2 av_free(zbuf);
570 2 return 0;
571 }
572 #endif
573
574 #if CONFIG_LZMA
575 ✗ static int tiff_uncompress_lzma(uint8_t *dst, uint64_t *len, const uint8_t *src,
576 int size)
577 {
578 ✗ lzma_stream stream = LZMA_STREAM_INIT;
579 lzma_ret ret;
580
581 ✗ stream.next_in = src;
582 ✗ stream.avail_in = size;
583 ✗ stream.next_out = dst;
584 ✗ stream.avail_out = *len;
585 ✗ ret = lzma_stream_decoder(&stream, UINT64_MAX, 0);
586 ✗ if (ret != LZMA_OK) {
587 ✗ av_log(NULL, AV_LOG_ERROR, "LZMA init error: %d\n", ret);
588 ✗ return ret;
589 }
590 ✗ ret = lzma_code(&stream, LZMA_RUN);
591 ✗ lzma_end(&stream);
592 ✗ *len = stream.total_out;
593 ✗ return ret == LZMA_STREAM_END ? LZMA_OK : ret;
594 }
595
596 ✗ static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
597 const uint8_t *src, int size, int width, int lines,
598 int strip_start, int is_yuv)
599 {
600 ✗ uint64_t outlen = width * (uint64_t)lines;
601 int ret, line;
602 ✗ int rows = is_yuv ? (lines + s->subsampling[1] - 1) / s->subsampling[1] : lines;
603 ✗ uint8_t *buf = av_malloc(outlen);
604 ✗ if (!buf)
605 ✗ return AVERROR(ENOMEM);
606 ✗ if (s->fill_order) {
607 ✗ if ((ret = deinvert_buffer(s, src, size)) < 0) {
608 ✗ av_free(buf);
609 ✗ return ret;
610 }
611 ✗ src = s->deinvert_buf;
612 }
613 ✗ ret = tiff_uncompress_lzma(buf, &outlen, src, size);
614 ✗ if (ret != LZMA_OK) {
615 ✗ av_log(s->avctx, AV_LOG_ERROR,
616 "Uncompressing failed (%"PRIu64" of %"PRIu64") with error %d\n", outlen,
617 ✗ (uint64_t)width * lines, ret);
618 ✗ av_free(buf);
619 ✗ return AVERROR_UNKNOWN;
620 }
621 ✗ if (outlen < (uint64_t)width * rows) {
622 ✗ av_log(s->avctx, AV_LOG_ERROR, "Uncompressed %"PRIu64" bytes, but %"PRIu64" are needed\n",
623 ✗ outlen, (uint64_t)width * rows);
624 ✗ av_free(buf);
625 ✗ return AVERROR_INVALIDDATA;
626 }
627 ✗ src = buf;
628 ✗ for (line = 0; line < lines; line++) {
629 ✗ if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
630 ✗ horizontal_fill(s, s->bpp, dst, 1, src, 0, width, 0);
631 } else {
632 ✗ memcpy(dst, src, width);
633 }
634 ✗ if (is_yuv) {
635 ✗ unpack_yuv(s, p, dst, strip_start + line);
636 ✗ line += s->subsampling[1] - 1;
637 }
638 ✗ dst += stride;
639 ✗ src += width;
640 }
641 ✗ av_free(buf);
642 ✗ return 0;
643 }
644 #endif
645
646 18 static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
647 const uint8_t *src, int size, int width, int lines)
648 {
649 int line;
650 int ret;
651
652
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (s->fill_order) {
653
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = deinvert_buffer(s, src, size)) < 0)
654 ✗ return ret;
655 18 src = s->deinvert_buf;
656 }
657 18 ret = ff_ccitt_unpack(s->avctx, src, size, dst, lines, stride,
658 s->compr, s->fax_opts);
659
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)
660 ✗ for (line = 0; line < lines; line++) {
661 ✗ horizontal_fill(s, s->bpp, dst, 1, dst, 0, width, 0);
662 ✗ dst += stride;
663 }
664 18 return ret;
665 }
666
667 ✗ static int dng_decode_jpeg(AVCodecContext *avctx, AVFrame *frame,
668 int tile_byte_count, int dst_x, int dst_y, int w, int h)
669 {
670 ✗ TiffContext *s = avctx->priv_data;
671 uint8_t *dst_data, *src_data;
672 uint32_t dst_offset; /* offset from dst buffer in pixels */
673 int is_single_comp, is_u16, pixel_size;
674 int ret;
675
676 ✗ if (tile_byte_count < 0 || tile_byte_count > bytestream2_get_bytes_left(&s->gb))
677 ✗ return AVERROR_INVALIDDATA;
678
679 /* Prepare a packet and send to the MJPEG decoder */
680 ✗ av_packet_unref(s->jpkt);
681 ✗ s->jpkt->data = (uint8_t*)s->gb.buffer;
682 ✗ s->jpkt->size = tile_byte_count;
683
684 ✗ if (s->is_bayer) {
685 ✗ MJpegDecodeContext *mjpegdecctx = s->avctx_mjpeg->priv_data;
686 /* We have to set this information here, there is no way to know if a given JPEG is a DNG-embedded
687 image or not from its own data (and we need that information when decoding it). */
688 ✗ mjpegdecctx->bayer = 1;
689 }
690
691 ✗ ret = avcodec_send_packet(s->avctx_mjpeg, s->jpkt);
692 ✗ if (ret < 0) {
693 ✗ av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
694 ✗ return ret;
695 }
696
697 ✗ ret = avcodec_receive_frame(s->avctx_mjpeg, s->jpgframe);
698 ✗ if (ret < 0) {
699 ✗ av_log(avctx, AV_LOG_ERROR, "JPEG decoding error: %s.\n", av_err2str(ret));
700
701 /* Normally skip, error if explode */
702 ✗ if (avctx->err_recognition & AV_EF_EXPLODE)
703 ✗ return AVERROR_INVALIDDATA;
704 else
705 ✗ return 0;
706 }
707
708 ✗ is_u16 = (s->bpp > 8);
709
710 /* Copy the outputted tile's pixels from 'jpgframe' to 'frame' (final buffer) */
711
712 ✗ if (s->jpgframe->width != s->avctx_mjpeg->width ||
713 ✗ s->jpgframe->height != s->avctx_mjpeg->height ||
714 ✗ s->jpgframe->format != s->avctx_mjpeg->pix_fmt)
715 ✗ return AVERROR_INVALIDDATA;
716
717 /* See dng_blit for explanation */
718 ✗ if (s->avctx_mjpeg->width == w * 2 &&
719 ✗ s->avctx_mjpeg->height == h / 2 &&
720 ✗ s->avctx_mjpeg->pix_fmt == AV_PIX_FMT_GRAY16LE) {
721 ✗ is_single_comp = 1;
722 ✗ } else if (s->avctx_mjpeg->width >= w &&
723 ✗ s->avctx_mjpeg->height >= h &&
724 ✗ s->avctx_mjpeg->pix_fmt == (is_u16 ? AV_PIX_FMT_GRAY16 : AV_PIX_FMT_GRAY8)
725 ) {
726 ✗ is_single_comp = 0;
727 } else
728 ✗ return AVERROR_INVALIDDATA;
729
730 ✗ pixel_size = (is_u16 ? sizeof(uint16_t) : sizeof(uint8_t));
731
732 ✗ if (is_single_comp && !is_u16) {
733 ✗ av_log(s->avctx, AV_LOG_ERROR, "DNGs with bpp <= 8 and 1 component are unsupported\n");
734 ✗ av_frame_unref(s->jpgframe);
735 ✗ return AVERROR_PATCHWELCOME;
736 }
737
738 ✗ dst_offset = dst_x + frame->linesize[0] * dst_y / pixel_size;
739 ✗ dst_data = frame->data[0] + dst_offset * pixel_size;
740 ✗ src_data = s->jpgframe->data[0];
741
742 ✗ dng_blit(s,
743 dst_data,
744 ✗ frame->linesize[0] / pixel_size,
745 src_data,
746 ✗ s->jpgframe->linesize[0] / pixel_size,
747 w,
748 h,
749 is_single_comp,
750 is_u16, 0);
751
752 ✗ av_frame_unref(s->jpgframe);
753
754 ✗ return 0;
755 }
756
757 627 static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
758 const uint8_t *src, int size, int strip_start, int lines)
759 {
760 PutByteContext pb;
761 int c, line, pixels, code, ret;
762 627 const uint8_t *ssrc = src;
763 627 int width = ((s->width * s->bpp) + 7) >> 3;
764 627 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(p->format);
765 1318 int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
766
4/4
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 563 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 18 times.
673 (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
767
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 desc->nb_components >= 3;
768 int is_dng;
769
770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 627 times.
627 if (s->planar)
771 ✗ width /= s->bppcount;
772
773
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 627 times.
627 if (size <= 0)
774 ✗ return AVERROR_INVALIDDATA;
775
776
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 581 times.
627 if (is_yuv) {
777 46 int bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
778 46 s->subsampling[0] * s->subsampling[1] + 7) >> 3;
779 46 av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
780
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (s->yuv_line == NULL) {
781 ✗ av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
782 ✗ return AVERROR(ENOMEM);
783 }
784 46 dst = s->yuv_line;
785 46 stride = 0;
786
787 46 width = (s->width - 1) / s->subsampling[0] + 1;
788 46 width = width * s->subsampling[0] * s->subsampling[1] + 2*width;
789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 av_assert0(width <= bytes_per_row);
790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 av_assert0(s->bpp == 24);
791 }
792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 627 times.
627 if (s->is_bayer) {
793 ✗ av_assert0(width == (s->bpp * s->width + 7) >> 3);
794 }
795
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 627 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
627 av_assert0(!(s->is_bayer && is_yuv));
796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 627 times.
627 if (p->format == AV_PIX_FMT_GRAY12) {
797 ✗ av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, width);
798 ✗ if (s->yuv_line == NULL) {
799 ✗ av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
800 ✗ return AVERROR(ENOMEM);
801 }
802 ✗ dst = s->yuv_line;
803 ✗ stride = 0;
804 }
805
806
3/4
✓ Branch 0 taken 627 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 625 times.
627 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
807 #if CONFIG_ZLIB
808 2 return tiff_unpack_zlib(s, p, dst, stride, src, size, width, lines,
809 strip_start, is_yuv);
810 #else
811 av_log(s->avctx, AV_LOG_ERROR,
812 "zlib support not enabled, "
813 "deflate compression not supported\n");
814 return AVERROR(ENOSYS);
815 #endif
816 }
817
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 625 times.
625 if (s->compr == TIFF_LZMA) {
818 #if CONFIG_LZMA
819 ✗ return tiff_unpack_lzma(s, p, dst, stride, src, size, width, lines,
820 strip_start, is_yuv);
821 #else
822 av_log(s->avctx, AV_LOG_ERROR,
823 "LZMA support not enabled\n");
824 return AVERROR(ENOSYS);
825 #endif
826 }
827
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 623 times.
625 if (s->compr == TIFF_LZW) {
828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->fill_order) {
829 ✗ if ((ret = deinvert_buffer(s, src, size)) < 0)
830 ✗ return ret;
831 ✗ ssrc = src = s->deinvert_buf;
832 }
833
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)) {
834 ✗ av_log(s->avctx, AV_LOG_ERROR, "Old style LZW is unsupported\n");
835 }
836
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) {
837 ✗ av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
838 ✗ return ret;
839 }
840
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (line = 0; line < lines; line++) {
841 16 pixels = ff_lzw_decode(s->lzw, dst, width);
842
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (pixels < width) {
843 ✗ av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
844 pixels, width);
845 ✗ return AVERROR_INVALIDDATA;
846 }
847
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)
848 ✗ horizontal_fill(s, s->bpp, dst, 1, dst, 0, width, 0);
849
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (is_yuv) {
850 ✗ unpack_yuv(s, p, dst, strip_start + line);
851 ✗ line += s->subsampling[1] - 1;
852
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (p->format == AV_PIX_FMT_GRAY12) {
853 ✗ unpack_gray(s, p, dst, strip_start + line, width, s->bpp);
854 }
855 16 dst += stride;
856 }
857 2 return 0;
858 }
859
1/2
✓ Branch 0 taken 623 times.
✗ Branch 1 not taken.
623 if (s->compr == TIFF_CCITT_RLE ||
860
2/2
✓ Branch 0 taken 605 times.
✓ Branch 1 taken 18 times.
623 s->compr == TIFF_G3 ||
861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 605 times.
605 s->compr == TIFF_G4) {
862
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)
863 ✗ return AVERROR_INVALIDDATA;
864
865 18 return tiff_unpack_fax(s, dst, stride, src, size, width, lines);
866 }
867
868 605 bytestream2_init(&s->gb, src, size);
869
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 559 times.
605 bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * lines));
870
871
2/4
✓ Branch 0 taken 605 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 605 times.
605 is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
872
873 /* Decode JPEG-encoded DNGs with strips */
874
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 605 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
605 if (s->compr == TIFF_NEWJPEG && is_dng) {
875 ✗ if (s->strips > 1) {
876 ✗ av_log(s->avctx, AV_LOG_ERROR, "More than one DNG JPEG strips unsupported\n");
877 ✗ return AVERROR_PATCHWELCOME;
878 }
879 ✗ if (!s->is_bayer)
880 ✗ return AVERROR_PATCHWELCOME;
881 ✗ if ((ret = dng_decode_jpeg(s->avctx, p, s->stripsize, 0, 0, s->width, s->height)) < 0)
882 ✗ return ret;
883 ✗ return 0;
884 }
885
886
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 605 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
605 if (is_dng && stride == 0)
887 ✗ return AVERROR_INVALIDDATA;
888
889
2/2
✓ Branch 0 taken 4374 times.
✓ Branch 1 taken 605 times.
4979 for (line = 0; line < lines; line++) {
890
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4374 times.
4374 if (src - ssrc > size) {
891 ✗ av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
892 ✗ return AVERROR_INVALIDDATA;
893 }
894
895
2/4
✓ Branch 1 taken 4374 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4374 times.
✗ Branch 5 not taken.
4374 if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
896 break;
897 4374 bytestream2_seek_p(&pb, stride * line, SEEK_SET);
898
2/3
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 4244 times.
✗ Branch 2 not taken.
4374 switch (s->compr) {
899 130 case TIFF_RAW:
900
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 if (ssrc + size - src < width)
901 ✗ return AVERROR_INVALIDDATA;
902
903
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 if (!s->fill_order) {
904
2/4
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 130 times.
130 horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 || s->is_bayer),
905 dst, 1, src, 0, width, 0);
906 } else {
907 int i;
908 ✗ for (i = 0; i < width; i++)
909 ✗ dst[i] = ff_reverse[src[i]];
910 }
911
912 /* Color processing for DNG images with uncompressed strips (non-tiled) */
913
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 if (is_dng) {
914 int is_u16, pixel_size_bytes, pixel_size_bits, elements;
915
916 ✗ is_u16 = (s->bpp / s->bppcount > 8);
917 ✗ pixel_size_bits = (is_u16 ? 16 : 8);
918 ✗ pixel_size_bytes = (is_u16 ? sizeof(uint16_t) : sizeof(uint8_t));
919
920 ✗ elements = width / pixel_size_bytes * pixel_size_bits / s->bpp * s->bppcount; // need to account for [1, 16] bpp
921 ✗ av_assert0 (elements * pixel_size_bytes <= FFABS(stride));
922 ✗ dng_blit(s,
923 dst,
924 0, // no stride, only 1 line
925 dst,
926 0, // no stride, only 1 line
927 elements,
928 1,
929 0, // single-component variation is only preset in JPEG-encoded DNGs
930 is_u16,
931 ✗ (line + strip_start)&1);
932 }
933
934 130 src += width;
935 130 break;
936 4244 case TIFF_PACKBITS:
937
2/2
✓ Branch 0 taken 43160 times.
✓ Branch 1 taken 4244 times.
47404 for (pixels = 0; pixels < width;) {
938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43160 times.
43160 if (ssrc + size - src < 2) {
939 ✗ av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
940 ✗ return AVERROR_INVALIDDATA;
941 }
942
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43160 times.
43160 code = s->fill_order ? (int8_t) ff_reverse[*src++]: (int8_t) *src++;
943
2/2
✓ Branch 0 taken 38601 times.
✓ Branch 1 taken 4559 times.
43160 if (code >= 0) {
944 38601 code++;
945
1/2
✓ Branch 0 taken 38601 times.
✗ Branch 1 not taken.
38601 if (pixels + code > width ||
946
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38601 times.
38601 ssrc + size - src < code) {
947 ✗ av_log(s->avctx, AV_LOG_ERROR,
948 "Copy went out of bounds\n");
949 ✗ return AVERROR_INVALIDDATA;
950 }
951 38601 horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
952 dst, 1, src, 0, code, pixels);
953 38601 src += code;
954 38601 pixels += code;
955
1/2
✓ Branch 0 taken 4559 times.
✗ Branch 1 not taken.
4559 } else if (code != -128) { // -127..-1
956 4559 code = (-code) + 1;
957
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4559 times.
4559 if (pixels + code > width) {
958 ✗ av_log(s->avctx, AV_LOG_ERROR,
959 "Run went out of bounds\n");
960 ✗ return AVERROR_INVALIDDATA;
961 }
962 4559 c = *src++;
963 4559 horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
964 dst, 0, NULL, c, code, pixels);
965 4559 pixels += code;
966 }
967 }
968
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4244 times.
4244 if (s->fill_order) {
969 int i;
970 ✗ for (i = 0; i < width; i++)
971 ✗ dst[i] = ff_reverse[dst[i]];
972 }
973 4244 break;
974 }
975
2/2
✓ Branch 0 taken 500 times.
✓ Branch 1 taken 3874 times.
4374 if (is_yuv) {
976 500 unpack_yuv(s, p, dst, strip_start + line);
977 500 line += s->subsampling[1] - 1;
978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3874 times.
3874 } else if (p->format == AV_PIX_FMT_GRAY12) {
979 ✗ unpack_gray(s, p, dst, strip_start + line, width, s->bpp);
980 }
981 4374 dst += stride;
982 }
983 605 return 0;
984 }
985
986 ✗ static int dng_decode_tiles(AVCodecContext *avctx, AVFrame *frame,
987 const AVPacket *avpkt)
988 {
989 ✗ TiffContext *s = avctx->priv_data;
990 int tile_idx;
991 int tile_offset_offset, tile_offset;
992 int tile_byte_count_offset, tile_byte_count;
993 int tile_count_x, tile_count_y;
994 int tile_width, tile_length;
995 int has_width_leftover, has_height_leftover;
996 ✗ int tile_x = 0, tile_y = 0;
997 ✗ int pos_x = 0, pos_y = 0;
998 int ret;
999
1000 ✗ if (s->tile_width <= 0 || s->tile_length <= 0)
1001 ✗ return AVERROR_INVALIDDATA;
1002
1003 ✗ has_width_leftover = (s->width % s->tile_width != 0);
1004 ✗ has_height_leftover = (s->height % s->tile_length != 0);
1005
1006 /* Calculate tile counts (round up) */
1007 ✗ tile_count_x = (s->width + s->tile_width - 1) / s->tile_width;
1008 ✗ tile_count_y = (s->height + s->tile_length - 1) / s->tile_length;
1009
1010 /* Iterate over the number of tiles */
1011 ✗ for (tile_idx = 0; tile_idx < tile_count_x * tile_count_y; tile_idx++) {
1012 ✗ tile_x = tile_idx % tile_count_x;
1013 ✗ tile_y = tile_idx / tile_count_x;
1014
1015 ✗ if (has_width_leftover && tile_x == tile_count_x - 1) // If on the right-most tile
1016 ✗ tile_width = s->width % s->tile_width;
1017 else
1018 ✗ tile_width = s->tile_width;
1019
1020 ✗ if (has_height_leftover && tile_y == tile_count_y - 1) // If on the bottom-most tile
1021 ✗ tile_length = s->height % s->tile_length;
1022 else
1023 ✗ tile_length = s->tile_length;
1024
1025 /* Read tile offset */
1026 ✗ tile_offset_offset = s->tile_offsets_offset + tile_idx * sizeof(int);
1027 ✗ bytestream2_seek(&s->gb, tile_offset_offset, SEEK_SET);
1028 ✗ tile_offset = ff_tget_long(&s->gb, s->le);
1029
1030 /* Read tile byte size */
1031 ✗ tile_byte_count_offset = s->tile_byte_counts_offset + tile_idx * sizeof(int);
1032 ✗ bytestream2_seek(&s->gb, tile_byte_count_offset, SEEK_SET);
1033 ✗ tile_byte_count = ff_tget_long(&s->gb, s->le);
1034
1035 /* Seek to tile data */
1036 ✗ bytestream2_seek(&s->gb, tile_offset, SEEK_SET);
1037
1038 /* Decode JPEG tile and copy it in the reference frame */
1039 ✗ ret = dng_decode_jpeg(avctx, frame, tile_byte_count, pos_x, pos_y, tile_width, tile_length);
1040
1041 ✗ if (ret < 0)
1042 ✗ return ret;
1043
1044 /* Advance current positions */
1045 ✗ pos_x += tile_width;
1046 ✗ if (tile_x == tile_count_x - 1) { // If on the right edge
1047 ✗ pos_x = 0;
1048 ✗ pos_y += tile_length;
1049 }
1050 }
1051
1052 /* Frame is ready to be output */
1053 ✗ frame->pict_type = AV_PICTURE_TYPE_I;
1054 ✗ frame->flags |= AV_FRAME_FLAG_KEY;
1055
1056 ✗ return avpkt->size;
1057 }
1058
1059 45 static int init_image(TiffContext *s, AVFrame *frame)
1060 {
1061 int ret;
1062 45 int create_gray_palette = 0;
1063
1064 // make sure there is no aliasing in the following switch
1065
2/4
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 45 times.
45 if (s->bpp > 128 || s->bppcount >= 10) {
1066 ✗ av_log(s->avctx, AV_LOG_ERROR,
1067 "Unsupported image parameters: bpp=%d, bppcount=%d\n",
1068 s->bpp, s->bppcount);
1069 ✗ return AVERROR_INVALIDDATA;
1070 }
1071
1072
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 29 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.
45 switch (s->planar * 10000 + s->bpp * 10 + s->bppcount + s->is_bayer * 100000) {
1073 4 case 11:
1074
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!s->palette_is_set) {
1075 4 s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
1076 4 break;
1077 }
1078 av_fallthrough;
1079 case 21:
1080 case 41:
1081 ✗ s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
1082 ✗ if (!s->palette_is_set) {
1083 ✗ create_gray_palette = 1;
1084 }
1085 ✗ break;
1086 ✗ case 81:
1087 ✗ s->avctx->pix_fmt = s->palette_is_set ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
1088 ✗ break;
1089 ✗ case 121:
1090 ✗ s->avctx->pix_fmt = AV_PIX_FMT_GRAY12;
1091 ✗ break;
1092 ✗ case 100081:
1093 ✗ switch (AV_RL32(s->pattern)) {
1094 ✗ case 0x02010100:
1095 ✗ s->avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB8;
1096 ✗ break;
1097 ✗ case 0x00010102:
1098 ✗ s->avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR8;
1099 ✗ break;
1100 ✗ case 0x01000201:
1101 ✗ s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG8;
1102 ✗ break;
1103 ✗ case 0x01020001:
1104 ✗ s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG8;
1105 ✗ break;
1106 ✗ default:
1107 ✗ av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n",
1108 ✗ AV_RL32(s->pattern));
1109 ✗ return AVERROR_PATCHWELCOME;
1110 }
1111 ✗ break;
1112 ✗ case 100101:
1113 case 100121:
1114 case 100141:
1115 case 100161:
1116 ✗ switch (AV_RL32(s->pattern)) {
1117 ✗ case 0x02010100:
1118 ✗ s->avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB16;
1119 ✗ break;
1120 ✗ case 0x00010102:
1121 ✗ s->avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR16;
1122 ✗ break;
1123 ✗ case 0x01000201:
1124 ✗ s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG16;
1125 ✗ break;
1126 ✗ case 0x01020001:
1127 ✗ s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG16;
1128 ✗ break;
1129 ✗ default:
1130 ✗ av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n",
1131 ✗ AV_RL32(s->pattern));
1132 ✗ return AVERROR_PATCHWELCOME;
1133 }
1134 ✗ break;
1135 29 case 243:
1136
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 21 times.
29 if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1137
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8 if (s->subsampling[0] == 1 && s->subsampling[1] == 1) {
1138 ✗ s->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
1139
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 } else if (s->subsampling[0] == 2 && s->subsampling[1] == 1) {
1140 ✗ s->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
1141
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8 } else if (s->subsampling[0] == 4 && s->subsampling[1] == 1) {
1142 ✗ s->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
1143
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8 } else if (s->subsampling[0] == 1 && s->subsampling[1] == 2) {
1144 ✗ s->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
1145
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 } else if (s->subsampling[0] == 2 && s->subsampling[1] == 2) {
1146 8 s->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1147 ✗ } else if (s->subsampling[0] == 4 && s->subsampling[1] == 4) {
1148 ✗ s->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
1149 } else {
1150 ✗ av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr subsampling\n");
1151 ✗ return AVERROR_PATCHWELCOME;
1152 }
1153 } else
1154 21 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
1155 29 break;
1156 ✗ case 161:
1157 ✗ s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
1158 ✗ break;
1159 ✗ case 162:
1160 ✗ s->avctx->pix_fmt = AV_PIX_FMT_YA8;
1161 ✗ break;
1162 ✗ case 322:
1163 ✗ s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YA16LE : AV_PIX_FMT_YA16BE;
1164 ✗ break;
1165 ✗ case 324:
1166 ✗ s->avctx->pix_fmt = s->photometric == TIFF_PHOTOMETRIC_SEPARATED ? AV_PIX_FMT_RGB0 : AV_PIX_FMT_RGBA;
1167 ✗ break;
1168 ✗ case 405:
1169 ✗ if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED)
1170 ✗ s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
1171 else {
1172 ✗ av_log(s->avctx, AV_LOG_ERROR,
1173 "bpp=40 without PHOTOMETRIC_SEPARATED is unsupported\n");
1174 ✗ return AVERROR_PATCHWELCOME;
1175 }
1176 ✗ break;
1177 ✗ case 483:
1178 ✗ s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
1179 ✗ break;
1180 ✗ case 644:
1181 ✗ s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGBA64BE;
1182 ✗ break;
1183 ✗ case 10243:
1184 ✗ s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
1185 ✗ break;
1186 ✗ case 10324:
1187 ✗ s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
1188 ✗ break;
1189 ✗ case 10483:
1190 ✗ s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRP16LE : AV_PIX_FMT_GBRP16BE;
1191 ✗ break;
1192 ✗ case 10644:
1193 ✗ s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAP16LE : AV_PIX_FMT_GBRAP16BE;
1194 ✗ break;
1195 6 case 963:
1196
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;
1197 6 break;
1198 6 case 1284:
1199
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;
1200 6 break;
1201 ✗ case 10963:
1202 ✗ s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRPF32LE : AV_PIX_FMT_GBRPF32BE;
1203 ✗ break;
1204 ✗ case 11284:
1205 ✗ s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAPF32LE : AV_PIX_FMT_GBRAPF32BE;
1206 ✗ break;
1207 ✗ default:
1208 ✗ av_log(s->avctx, AV_LOG_ERROR,
1209 "This format is not supported (bpp=%d, bppcount=%d)\n",
1210 s->bpp, s->bppcount);
1211 ✗ return AVERROR_INVALIDDATA;
1212 }
1213
1214
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 37 times.
45 if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1215 8 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1216
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if((desc->flags & AV_PIX_FMT_FLAG_RGB) ||
1217
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
1218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 desc->nb_components < 3) {
1219 ✗ av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr variant\n");
1220 ✗ return AVERROR_INVALIDDATA;
1221 }
1222 }
1223
1224
3/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
45 if (s->width != s->avctx->width || s->height != s->avctx->height) {
1225 17 ret = ff_set_dimensions(s->avctx, s->width, s->height);
1226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret < 0)
1227 ✗ return ret;
1228 }
1229
1230
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 28 times.
45 if (s->avctx->skip_frame >= AVDISCARD_ALL)
1231 17 return 0;
1232
1233
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if ((ret = ff_thread_get_buffer(s->avctx, frame, 0)) < 0)
1234 ✗ return ret;
1235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1236 ✗ if (!create_gray_palette)
1237 ✗ memcpy(frame->data[1], s->palette, sizeof(s->palette));
1238 else {
1239 /* make default grayscale pal */
1240 int i;
1241 ✗ uint32_t *pal = (uint32_t *)frame->data[1];
1242 ✗ for (i = 0; i < 1<<s->bpp; i++)
1243 ✗ pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
1244 }
1245 }
1246 28 return 1;
1247 }
1248
1249 66 static void set_sar(TiffContext *s, unsigned tag, unsigned numerator, unsigned denumerator)
1250 {
1251
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 33 times.
66 int offset = tag == TIFF_YRES ? 2 : 0;
1252 66 s->res[offset++] = numerator;
1253 66 s->res[offset] = denumerator;
1254
5/8
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45 times.
✓ Branch 5 taken 21 times.
✓ Branch 6 taken 45 times.
✗ Branch 7 not taken.
66 if (s->res[0] && s->res[1] && s->res[2] && s->res[3]) {
1255 45 uint64_t num = s->res[2] * (uint64_t)s->res[1];
1256 45 uint64_t den = s->res[0] * (uint64_t)s->res[3];
1257
2/4
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 45 times.
45 if (num > INT64_MAX || den > INT64_MAX) {
1258 ✗ num = num >> 1;
1259 ✗ den = den >> 1;
1260 }
1261 45 av_reduce(&s->avctx->sample_aspect_ratio.num, &s->avctx->sample_aspect_ratio.den,
1262 num, den, INT32_MAX);
1263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (!s->avctx->sample_aspect_ratio.den)
1264 ✗ s->avctx->sample_aspect_ratio = (AVRational) {0, 1};
1265 }
1266 66 }
1267
1268 699 static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
1269 {
1270 AVFrameSideData *sd;
1271 GetByteContext gb_temp;
1272 699 unsigned tag, type, count, off, value = 0, value2 = 1; // value2 is a denominator so init. to 1
1273 int start;
1274 int pos;
1275 int ret;
1276 double *dp;
1277
1278 699 ret = ff_tread_tag(&s->gb, s->le, &tag, &type, &count, &start);
1279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 699 times.
699 if (ret < 0) {
1280 ✗ goto end;
1281 }
1282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 699 times.
699 if (tag <= s->last_tag)
1283 ✗ return AVERROR_INVALIDDATA;
1284
1285 // We ignore TIFF_STRIP_SIZE as it is sometimes in the logic but wrong order around TIFF_STRIP_OFFS
1286
2/2
✓ Branch 0 taken 654 times.
✓ Branch 1 taken 45 times.
699 if (tag != TIFF_STRIP_SIZE)
1287 654 s->last_tag = tag;
1288
1289 699 off = bytestream2_tell(&s->gb);
1290
2/2
✓ Branch 0 taken 506 times.
✓ Branch 1 taken 193 times.
699 if (count == 1) {
1291
2/4
✓ Branch 0 taken 440 times.
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
506 switch (type) {
1292 440 case AV_TIFF_BYTE:
1293 case AV_TIFF_SHORT:
1294 case AV_TIFF_LONG:
1295 440 value = ff_tget(&s->gb, type, s->le);
1296 440 break;
1297 66 case AV_TIFF_RATIONAL:
1298 66 value = ff_tget_long(&s->gb, s->le);
1299 66 value2 = ff_tget_long(&s->gb, s->le);
1300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!value2) {
1301 ✗ av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator in rational\n");
1302 ✗ value2 = 1;
1303 }
1304
1305 66 break;
1306 ✗ case AV_TIFF_STRING:
1307 ✗ if (count <= 4) {
1308 ✗ break;
1309 }
1310 av_fallthrough;
1311 default:
1312 ✗ value = UINT_MAX;
1313 }
1314 }
1315
1316
22/54
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 45 times.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 45 times.
✓ Branch 5 taken 45 times.
✓ Branch 6 taken 45 times.
✓ Branch 7 taken 45 times.
✓ Branch 8 taken 45 times.
✓ Branch 9 taken 66 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 45 times.
✓ Branch 22 taken 16 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 18 times.
✓ Branch 25 taken 8 times.
✗ 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 taken 4 times.
✗ 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 121 times.
699 switch (tag) {
1317 27 case TIFF_SUBFILE:
1318 27 s->is_thumbnail = (value != 0);
1319 27 break;
1320 45 case TIFF_WIDTH:
1321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (value > INT_MAX)
1322 ✗ return AVERROR_INVALIDDATA;
1323 45 s->width = value;
1324 45 break;
1325 45 case TIFF_HEIGHT:
1326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (value > INT_MAX)
1327 ✗ return AVERROR_INVALIDDATA;
1328 45 s->height = value;
1329 45 break;
1330 45 case TIFF_BPP:
1331
2/4
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 45 times.
45 if (count > 5 || count <= 0) {
1332 ✗ av_log(s->avctx, AV_LOG_ERROR,
1333 "This format is not supported (bpp=%d, %d components)\n",
1334 value, count);
1335 ✗ return AVERROR_INVALIDDATA;
1336 }
1337 45 s->bppcount = count;
1338
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 41 times.
45 if (count == 1)
1339 4 s->bpp = value;
1340 else {
1341
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 switch (type) {
1342 41 case AV_TIFF_BYTE:
1343 case AV_TIFF_SHORT:
1344 case AV_TIFF_LONG:
1345 41 s->bpp = 0;
1346
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
41 if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
1347 ✗ return AVERROR_INVALIDDATA;
1348
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 41 times.
170 for (int i = 0; i < count; i++)
1349 129 s->bpp += ff_tget(&s->gb, type, s->le);
1350 41 break;
1351 ✗ default:
1352 ✗ s->bpp = -1;
1353 }
1354 }
1355 45 break;
1356 45 case TIFF_SAMPLES_PER_PIXEL:
1357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (count != 1) {
1358 ✗ av_log(s->avctx, AV_LOG_ERROR,
1359 "Samples per pixel requires a single value, many provided\n");
1360 ✗ return AVERROR_INVALIDDATA;
1361 }
1362
2/4
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 45 times.
45 if (value > 5 || value <= 0) {
1363 ✗ av_log(s->avctx, AV_LOG_ERROR,
1364 "Invalid samples per pixel %d\n", value);
1365 ✗ return AVERROR_INVALIDDATA;
1366 }
1367
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 41 times.
45 if (s->bppcount == 1)
1368 4 s->bpp *= value;
1369 45 s->bppcount = value;
1370 45 break;
1371 45 case TIFF_COMPR:
1372 45 s->compr = value;
1373 45 av_log(s->avctx, AV_LOG_DEBUG, "compression: %d\n", s->compr);
1374 45 s->predictor = 0;
1375
3/6
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
45 switch (s->compr) {
1376 37 case TIFF_RAW:
1377 case TIFF_PACKBITS:
1378 case TIFF_LZW:
1379 case TIFF_CCITT_RLE:
1380 37 break;
1381 4 case TIFF_G3:
1382 case TIFF_G4:
1383 4 s->fax_opts = 0;
1384 4 break;
1385 4 case TIFF_DEFLATE:
1386 case TIFF_ADOBE_DEFLATE:
1387 #if CONFIG_ZLIB
1388 4 break;
1389 #else
1390 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
1391 return AVERROR(ENOSYS);
1392 #endif
1393 ✗ case TIFF_JPEG:
1394 case TIFF_NEWJPEG:
1395 ✗ s->is_jpeg = 1;
1396 ✗ break;
1397 ✗ case TIFF_LZMA:
1398 #if CONFIG_LZMA
1399 ✗ break;
1400 #else
1401 av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n");
1402 return AVERROR(ENOSYS);
1403 #endif
1404 ✗ default:
1405 ✗ av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
1406 ✗ s->compr);
1407 ✗ return AVERROR_INVALIDDATA;
1408 }
1409 45 break;
1410 45 case TIFF_ROWSPERSTRIP:
1411
4/6
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 31 times.
45 if (!value || (type == AV_TIFF_LONG && value == UINT_MAX))
1412 ✗ value = s->height;
1413 45 s->rps = FFMIN(value, s->height);
1414 45 break;
1415 45 case TIFF_STRIP_OFFS:
1416
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 27 times.
45 if (count == 1) {
1417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (value > INT_MAX) {
1418 ✗ av_log(s->avctx, AV_LOG_ERROR,
1419 "strippos %u too large\n", value);
1420 ✗ return AVERROR_INVALIDDATA;
1421 }
1422 18 s->strippos = 0;
1423 18 s->stripoff = value;
1424 } else
1425 27 s->strippos = off;
1426 45 s->strips = count;
1427
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 43 times.
45 if (s->strips == s->bppcount)
1428 2 s->rps = s->height;
1429 45 s->sot = type;
1430 45 break;
1431 45 case TIFF_STRIP_SIZE:
1432
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 27 times.
45 if (count == 1) {
1433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (value > INT_MAX) {
1434 ✗ av_log(s->avctx, AV_LOG_ERROR,
1435 "stripsize %u too large\n", value);
1436 ✗ return AVERROR_INVALIDDATA;
1437 }
1438 18 s->stripsizesoff = 0;
1439 18 s->stripsize = value;
1440 18 s->strips = 1;
1441 } else {
1442 27 s->stripsizesoff = off;
1443 }
1444 45 s->strips = count;
1445 45 s->sstype = type;
1446 45 break;
1447 66 case TIFF_XRES:
1448 case TIFF_YRES:
1449 66 set_sar(s, tag, value, value2);
1450 66 break;
1451 ✗ case TIFF_TILE_OFFSETS:
1452 ✗ s->tile_offsets_offset = off;
1453 ✗ s->is_tiled = 1;
1454 ✗ break;
1455 ✗ case TIFF_TILE_BYTE_COUNTS:
1456 ✗ s->tile_byte_counts_offset = off;
1457 ✗ break;
1458 ✗ case TIFF_TILE_LENGTH:
1459 ✗ if (value > INT_MAX)
1460 ✗ return AVERROR_INVALIDDATA;
1461 ✗ s->tile_length = value;
1462 ✗ break;
1463 ✗ case TIFF_TILE_WIDTH:
1464 ✗ if (value > INT_MAX)
1465 ✗ return AVERROR_INVALIDDATA;
1466 ✗ s->tile_width = value;
1467 ✗ break;
1468 8 case TIFF_PREDICTOR:
1469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (value > INT_MAX)
1470 ✗ return AVERROR_INVALIDDATA;
1471 8 s->predictor = value;
1472 8 break;
1473 ✗ case TIFF_SUB_IFDS:
1474 ✗ if (count == 1)
1475 ✗ s->sub_ifd = value;
1476 ✗ else if (count > 1)
1477 ✗ s->sub_ifd = ff_tget_long(&s->gb, s->le); /** Only get the first SubIFD */
1478 ✗ break;
1479 ✗ case TIFF_GRAY_RESPONSE_CURVE:
1480 case DNG_LINEARIZATION_TABLE:
1481 ✗ if (count < 1 || count > FF_ARRAY_ELEMS(s->dng_lut))
1482 ✗ return AVERROR_INVALIDDATA;
1483 ✗ for (int i = 0; i < count; i++)
1484 ✗ s->dng_lut[i] = ff_tget(&s->gb, type, s->le);
1485 ✗ s->white_level = s->dng_lut[count-1];
1486 ✗ break;
1487 ✗ case DNG_BLACK_LEVEL:
1488 ✗ if (count > FF_ARRAY_ELEMS(s->black_level))
1489 ✗ return AVERROR_INVALIDDATA;
1490 ✗ s->black_level[0] = value / (float)value2;
1491 ✗ for (int i = 0; i < count && count > 1; i++) {
1492 ✗ if (type == AV_TIFF_RATIONAL) {
1493 ✗ value = ff_tget_long(&s->gb, s->le);
1494 ✗ value2 = ff_tget_long(&s->gb, s->le);
1495 ✗ if (!value2) {
1496 ✗ av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1497 ✗ value2 = 1;
1498 }
1499
1500 ✗ s->black_level[i] = value / (float)value2;
1501 ✗ } else if (type == AV_TIFF_SRATIONAL) {
1502 ✗ int val = ff_tget_long(&s->gb, s->le);
1503 ✗ int val2 = ff_tget_long(&s->gb, s->le);
1504 ✗ if (!val2) {
1505 ✗ av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1506 ✗ val2 = 1;
1507 }
1508
1509 ✗ s->black_level[i] = val / (float)val2;
1510 } else {
1511 ✗ s->black_level[i] = ff_tget(&s->gb, type, s->le);
1512 }
1513 }
1514 ✗ for (int i = count; i < 4 && count > 0; i++)
1515 ✗ s->black_level[i] = s->black_level[count - 1];
1516 ✗ break;
1517 ✗ case DNG_WHITE_LEVEL:
1518 ✗ s->white_level = value;
1519 ✗ break;
1520 ✗ case TIFF_CFA_PATTERN_DIM:
1521 ✗ if (count != 2 || (ff_tget(&s->gb, type, s->le) != 2 &&
1522 ✗ ff_tget(&s->gb, type, s->le) != 2)) {
1523 ✗ av_log(s->avctx, AV_LOG_ERROR, "CFA Pattern dimensions are not 2x2\n");
1524 ✗ return AVERROR_INVALIDDATA;
1525 }
1526 ✗ break;
1527 ✗ case TIFF_CFA_PATTERN:
1528 ✗ s->is_bayer = 1;
1529 ✗ s->pattern[0] = ff_tget(&s->gb, type, s->le);
1530 ✗ s->pattern[1] = ff_tget(&s->gb, type, s->le);
1531 ✗ s->pattern[2] = ff_tget(&s->gb, type, s->le);
1532 ✗ s->pattern[3] = ff_tget(&s->gb, type, s->le);
1533 ✗ break;
1534 45 case TIFF_PHOTOMETRIC:
1535
1/3
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
45 switch (value) {
1536 45 case TIFF_PHOTOMETRIC_WHITE_IS_ZERO:
1537 case TIFF_PHOTOMETRIC_BLACK_IS_ZERO:
1538 case TIFF_PHOTOMETRIC_RGB:
1539 case TIFF_PHOTOMETRIC_PALETTE:
1540 case TIFF_PHOTOMETRIC_SEPARATED:
1541 case TIFF_PHOTOMETRIC_YCBCR:
1542 case TIFF_PHOTOMETRIC_CFA:
1543 case TIFF_PHOTOMETRIC_LINEAR_RAW: // Used by DNG images
1544 45 s->photometric = value;
1545 45 break;
1546 ✗ case TIFF_PHOTOMETRIC_ALPHA_MASK:
1547 case TIFF_PHOTOMETRIC_CIE_LAB:
1548 case TIFF_PHOTOMETRIC_ICC_LAB:
1549 case TIFF_PHOTOMETRIC_ITU_LAB:
1550 case TIFF_PHOTOMETRIC_LOG_L:
1551 case TIFF_PHOTOMETRIC_LOG_LUV:
1552 ✗ avpriv_report_missing_feature(s->avctx,
1553 "PhotometricInterpretation 0x%04X",
1554 value);
1555 ✗ return AVERROR_PATCHWELCOME;
1556 ✗ default:
1557 ✗ av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
1558 "unknown\n", value);
1559 ✗ return AVERROR_INVALIDDATA;
1560 }
1561 45 break;
1562 16 case TIFF_FILL_ORDER:
1563
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) {
1564 ✗ av_log(s->avctx, AV_LOG_ERROR,
1565 "Unknown FillOrder value %d, trying default one\n", value);
1566 ✗ value = 1;
1567 }
1568 16 s->fill_order = value - 1;
1569 16 break;
1570 ✗ case TIFF_PAL: {
1571 GetByteContext pal_gb[3];
1572 ✗ off = type_sizes[type];
1573 ✗ if (count / 3 > 256 ||
1574 ✗ bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
1575 ✗ return AVERROR_INVALIDDATA;
1576
1577 ✗ pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
1578 ✗ bytestream2_skip(&pal_gb[1], count / 3 * off);
1579 ✗ bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
1580
1581 ✗ off = (type_sizes[type] - 1) << 3;
1582 ✗ if (off > 31U) {
1583 ✗ av_log(s->avctx, AV_LOG_ERROR, "palette shift %d is out of range\n", off);
1584 ✗ return AVERROR_INVALIDDATA;
1585 }
1586
1587 ✗ for (unsigned i = 0; i < count / 3; i++) {
1588 ✗ uint32_t p = 0xFF000000;
1589 ✗ p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16;
1590 ✗ p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8;
1591 ✗ p |= ff_tget(&pal_gb[2], type, s->le) >> off;
1592 ✗ s->palette[i] = p;
1593 }
1594 ✗ s->palette_is_set = 1;
1595 ✗ break;
1596 }
1597 18 case TIFF_PLANAR:
1598 18 s->planar = value == 2;
1599 18 break;
1600 8 case TIFF_YCBCR_SUBSAMPLING:
1601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (count != 2) {
1602 ✗ av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n");
1603 ✗ return AVERROR_INVALIDDATA;
1604 }
1605
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (unsigned i = 0; i < count; i++) {
1606 16 s->subsampling[i] = ff_tget(&s->gb, type, s->le);
1607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (s->subsampling[i] <= 0) {
1608 ✗ av_log(s->avctx, AV_LOG_ERROR, "subsampling %d is invalid\n", s->subsampling[i]);
1609 ✗ s->subsampling[i] = 1;
1610 ✗ return AVERROR_INVALIDDATA;
1611 }
1612 }
1613 8 break;
1614 ✗ case TIFF_T4OPTIONS:
1615 ✗ if (s->compr == TIFF_G3) {
1616 ✗ if (value > INT_MAX)
1617 ✗ return AVERROR_INVALIDDATA;
1618 ✗ s->fax_opts = value;
1619 }
1620 ✗ break;
1621 ✗ case TIFF_T6OPTIONS:
1622 ✗ if (s->compr == TIFF_G4) {
1623 ✗ if (value > INT_MAX)
1624 ✗ return AVERROR_INVALIDDATA;
1625 ✗ s->fax_opts = value;
1626 }
1627 ✗ break;
1628 #define ADD_METADATA(count, name, sep)\
1629 if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1630 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1631 goto end;\
1632 }
1633 ✗ case TIFF_MODEL_PIXEL_SCALE:
1634 ✗ ADD_METADATA(count, "ModelPixelScaleTag", NULL);
1635 ✗ break;
1636 ✗ case TIFF_MODEL_TRANSFORMATION:
1637 ✗ ADD_METADATA(count, "ModelTransformationTag", NULL);
1638 ✗ break;
1639 ✗ case TIFF_MODEL_TIEPOINT:
1640 ✗ ADD_METADATA(count, "ModelTiepointTag", NULL);
1641 ✗ break;
1642 ✗ case TIFF_GEO_KEY_DIRECTORY:
1643 ✗ if (s->geotag_count) {
1644 ✗ avpriv_request_sample(s->avctx, "Multiple geo key directories");
1645 ✗ return AVERROR_INVALIDDATA;
1646 }
1647 ✗ ADD_METADATA(1, "GeoTIFF_Version", NULL);
1648 ✗ ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
1649 ✗ s->geotag_count = ff_tget_short(&s->gb, s->le);
1650 ✗ if (s->geotag_count > count / 4 - 1) {
1651 ✗ s->geotag_count = count / 4 - 1;
1652 ✗ av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
1653 }
1654 ✗ if ( bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4
1655 ✗ || s->geotag_count == 0) {
1656 ✗ s->geotag_count = 0;
1657 ✗ return -1;
1658 }
1659 ✗ s->geotags = av_calloc(s->geotag_count, sizeof(*s->geotags));
1660 ✗ if (!s->geotags) {
1661 ✗ av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1662 ✗ s->geotag_count = 0;
1663 ✗ goto end;
1664 }
1665 ✗ for (int i = 0; i < s->geotag_count; i++) {
1666 unsigned val;
1667 ✗ s->geotags[i].key = ff_tget_short(&s->gb, s->le);
1668 ✗ s->geotags[i].type = ff_tget_short(&s->gb, s->le);
1669 ✗ s->geotags[i].count = ff_tget_short(&s->gb, s->le);
1670 ✗ val = ff_tget_short(&s->gb, s->le);
1671
1672 ✗ if (!s->geotags[i].type) {
1673 ✗ const char *str = get_geokey_val(s->geotags[i].key, val);
1674
1675 ✗ s->geotags[i].val = str ? av_strdup(str) : av_asprintf("Unknown-%u", val);
1676 ✗ if (!s->geotags[i].val)
1677 ✗ return AVERROR(ENOMEM);
1678 } else
1679 ✗ s->geotags[i].offset = val;
1680 }
1681 ✗ break;
1682 ✗ case TIFF_GEO_DOUBLE_PARAMS:
1683 ✗ if (count >= INT_MAX / sizeof(int64_t))
1684 ✗ return AVERROR_INVALIDDATA;
1685 ✗ if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
1686 ✗ return AVERROR_INVALIDDATA;
1687 ✗ dp = av_malloc_array(count, sizeof(double));
1688 ✗ if (!dp) {
1689 ✗ av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1690 ✗ goto end;
1691 }
1692 ✗ for (unsigned i = 0; i < count; i++)
1693 ✗ dp[i] = ff_tget_double(&s->gb, s->le);
1694 ✗ for (int i = 0; i < s->geotag_count; i++) {
1695 ✗ if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
1696 ✗ if (s->geotags[i].count == 0
1697 ✗ || s->geotags[i].offset + s->geotags[i].count > count) {
1698 ✗ av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1699 ✗ } else if (s->geotags[i].val) {
1700 ✗ av_log(s->avctx, AV_LOG_WARNING, "Duplicate GeoTIFF key %d\n", s->geotags[i].key);
1701 } else {
1702 ✗ char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
1703 ✗ if (!ap) {
1704 ✗ av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1705 ✗ av_freep(&dp);
1706 ✗ return AVERROR(ENOMEM);
1707 }
1708 ✗ s->geotags[i].val = ap;
1709 }
1710 }
1711 }
1712 ✗ av_freep(&dp);
1713 ✗ break;
1714 ✗ case TIFF_GEO_ASCII_PARAMS:
1715 ✗ pos = bytestream2_tell(&s->gb);
1716 ✗ for (int i = 0; i < s->geotag_count; i++) {
1717 ✗ if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
1718 ✗ if (s->geotags[i].count == 0
1719 ✗ || s->geotags[i].offset + s->geotags[i].count > count) {
1720 ✗ av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1721 } else {
1722 char *ap;
1723
1724 ✗ bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
1725 ✗ if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
1726 ✗ return AVERROR_INVALIDDATA;
1727 ✗ if (s->geotags[i].val)
1728 ✗ return AVERROR_INVALIDDATA;
1729 ✗ ap = av_malloc(s->geotags[i].count);
1730 ✗ if (!ap) {
1731 ✗ av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1732 ✗ return AVERROR(ENOMEM);
1733 }
1734 ✗ bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
1735 ✗ ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
1736 ✗ s->geotags[i].val = ap;
1737 }
1738 }
1739 }
1740 ✗ break;
1741 4 case TIFF_ICC_PROFILE:
1742 4 gb_temp = s->gb;
1743 4 bytestream2_seek(&gb_temp, off, SEEK_SET);
1744
1745
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (bytestream2_get_bytes_left(&gb_temp) < count)
1746 ✗ return AVERROR_INVALIDDATA;
1747
1748 4 ret = ff_frame_new_side_data(s->avctx, frame, AV_FRAME_DATA_ICC_PROFILE, count, &sd);
1749
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
1750 ✗ return ret;
1751
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (sd)
1752 4 bytestream2_get_bufferu(&gb_temp, sd->data, count);
1753 4 break;
1754 ✗ case TIFF_ARTIST:
1755 ✗ ADD_METADATA(count, "artist", NULL);
1756 ✗ break;
1757 ✗ case TIFF_COPYRIGHT:
1758 ✗ ADD_METADATA(count, "copyright", NULL);
1759 ✗ break;
1760 2 case TIFF_DATE:
1761
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 ADD_METADATA(count, "date", NULL);
1762 2 break;
1763 6 case TIFF_DOCUMENT_NAME:
1764
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 ADD_METADATA(count, "document_name", NULL);
1765 6 break;
1766 ✗ case TIFF_HOST_COMPUTER:
1767 ✗ ADD_METADATA(count, "computer", NULL);
1768 ✗ break;
1769 4 case TIFF_IMAGE_DESCRIPTION:
1770
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 ADD_METADATA(count, "description", NULL);
1771 4 break;
1772 ✗ case TIFF_MAKE:
1773 ✗ ADD_METADATA(count, "make", NULL);
1774 ✗ break;
1775 ✗ case TIFF_MODEL:
1776 ✗ ADD_METADATA(count, "model", NULL);
1777 ✗ break;
1778 ✗ case TIFF_PAGE_NAME:
1779 ✗ ADD_METADATA(count, "page_name", NULL);
1780 ✗ break;
1781 12 case TIFF_PAGE_NUMBER:
1782
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 ADD_METADATA(count, "page_number", " / ");
1783 // need to seek back to re-read the page number
1784 12 bytestream2_seek(&s->gb, -count * sizeof(uint16_t), SEEK_CUR);
1785 // read the page number
1786 12 s->cur_page = ff_tget_short(&s->gb, s->le);
1787 // get back to where we were before the previous seek
1788 12 bytestream2_seek(&s->gb, count * sizeof(uint16_t) - sizeof(uint16_t), SEEK_CUR);
1789 12 break;
1790 2 case TIFF_SOFTWARE_NAME:
1791
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 ADD_METADATA(count, "software", NULL);
1792 2 break;
1793 ✗ case DNG_VERSION:
1794 ✗ if (count == 4) {
1795 unsigned int ver[4];
1796 ✗ ver[0] = ff_tget(&s->gb, type, s->le);
1797 ✗ ver[1] = ff_tget(&s->gb, type, s->le);
1798 ✗ ver[2] = ff_tget(&s->gb, type, s->le);
1799 ✗ ver[3] = ff_tget(&s->gb, type, s->le);
1800
1801 ✗ av_log(s->avctx, AV_LOG_DEBUG, "DNG file, version %u.%u.%u.%u\n",
1802 ver[0], ver[1], ver[2], ver[3]);
1803
1804 ✗ tiff_set_type(s, TIFF_TYPE_DNG);
1805 }
1806 ✗ break;
1807 ✗ case DNG_ANALOG_BALANCE:
1808 ✗ if (type != AV_TIFF_RATIONAL)
1809 ✗ break;
1810
1811 ✗ for (int i = 0; i < 3; i++) {
1812 ✗ value = ff_tget_long(&s->gb, s->le);
1813 ✗ value2 = ff_tget_long(&s->gb, s->le);
1814 ✗ if (!value2) {
1815 ✗ av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1816 ✗ value2 = 1;
1817 }
1818
1819 ✗ s->analog_balance[i] = value / (float)value2;
1820 }
1821 ✗ break;
1822 ✗ case DNG_AS_SHOT_NEUTRAL:
1823 ✗ if (type != AV_TIFF_RATIONAL)
1824 ✗ break;
1825
1826 ✗ for (int i = 0; i < 3; i++) {
1827 ✗ value = ff_tget_long(&s->gb, s->le);
1828 ✗ value2 = ff_tget_long(&s->gb, s->le);
1829 ✗ if (!value2) {
1830 ✗ av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1831 ✗ value2 = 1;
1832 }
1833
1834 ✗ s->as_shot_neutral[i] = value / (float)value2;
1835 }
1836 ✗ break;
1837 ✗ case DNG_AS_SHOT_WHITE_XY:
1838 ✗ if (type != AV_TIFF_RATIONAL)
1839 ✗ break;
1840
1841 ✗ for (int i = 0; i < 2; i++) {
1842 ✗ value = ff_tget_long(&s->gb, s->le);
1843 ✗ value2 = ff_tget_long(&s->gb, s->le);
1844 ✗ if (!value2) {
1845 ✗ av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1846 ✗ value2 = 1;
1847 }
1848
1849 ✗ s->as_shot_white[i] = value / (float)value2;
1850 }
1851 ✗ s->as_shot_white[2] = 1.f - s->as_shot_white[0] - s->as_shot_white[1];
1852 ✗ for (int i = 0; i < 3; i++) {
1853 ✗ s->as_shot_white[i] /= d65_white[i];
1854 }
1855 ✗ break;
1856 ✗ case DNG_COLOR_MATRIX1:
1857 case DNG_COLOR_MATRIX2:
1858 ✗ for (int i = 0; i < 3; i++) {
1859 ✗ for (int j = 0; j < 3; j++) {
1860 ✗ int val = ff_tget_long(&s->gb, s->le);
1861 ✗ int val2 = ff_tget_long(&s->gb, s->le);
1862 ✗ if (!val2) {
1863 ✗ av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1864 ✗ val2 = 1;
1865 }
1866 ✗ s->color_matrix[i][j] = val / (float)val2;
1867 }
1868 ✗ s->use_color_matrix = 1;
1869 }
1870 ✗ break;
1871 ✗ case DNG_CAMERA_CALIBRATION1:
1872 case DNG_CAMERA_CALIBRATION2:
1873 ✗ for (int i = 0; i < 3; i++) {
1874 ✗ for (int j = 0; j < 3; j++) {
1875 ✗ int val = ff_tget_long(&s->gb, s->le);
1876 ✗ int val2 = ff_tget_long(&s->gb, s->le);
1877 ✗ if (!val2) {
1878 ✗ av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1879 ✗ val2 = 1;
1880 }
1881 ✗ s->camera_calibration[i][j] = val / (float)val2;
1882 }
1883 }
1884 ✗ break;
1885 ✗ case CINEMADNG_TIME_CODES:
1886 case CINEMADNG_FRAME_RATE:
1887 case CINEMADNG_T_STOP:
1888 case CINEMADNG_REEL_NAME:
1889 case CINEMADNG_CAMERA_LABEL:
1890 ✗ tiff_set_type(s, TIFF_TYPE_CINEMADNG);
1891 ✗ break;
1892 121 default:
1893
1/2
✓ Branch 0 taken 121 times.
✗ Branch 1 not taken.
121 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1894 ✗ av_log(s->avctx, AV_LOG_ERROR,
1895 "Unknown or unsupported tag %d/0x%0X\n",
1896 tag, tag);
1897 ✗ return AVERROR_INVALIDDATA;
1898 }
1899 }
1900 121 end:
1901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 699 times.
699 if (s->bpp > 128U) {
1902 ✗ av_log(s->avctx, AV_LOG_ERROR,
1903 "This format is not supported (bpp=%d, %d components)\n",
1904 s->bpp, count);
1905 ✗ s->bpp = 0;
1906 ✗ return AVERROR_INVALIDDATA;
1907 }
1908 699 bytestream2_seek(&s->gb, start, SEEK_SET);
1909 699 return 0;
1910 }
1911
1912 static const float xyz2rgb[3][3] = {
1913 { 0.412453f, 0.357580f, 0.180423f },
1914 { 0.212671f, 0.715160f, 0.072169f },
1915 { 0.019334f, 0.119193f, 0.950227f },
1916 };
1917
1918 ✗ static void camera_xyz_coeff(TiffContext *s,
1919 float rgb2cam[3][4],
1920 double cam2xyz[4][3])
1921 {
1922 double cam2rgb[4][3], num;
1923 int i, j, k;
1924
1925 ✗ for (i = 0; i < 3; i++) {
1926 ✗ for (j = 0; j < 3; j++) {
1927 ✗ cam2rgb[i][j] = 0.;
1928 ✗ for (k = 0; k < 3; k++)
1929 ✗ cam2rgb[i][j] += cam2xyz[i][k] * xyz2rgb[k][j];
1930 }
1931 }
1932
1933 ✗ for (i = 0; i < 3; i++) {
1934 ✗ for (num = j = 0; j < 3; j++)
1935 ✗ num += cam2rgb[i][j];
1936 ✗ if (!num)
1937 ✗ num = 1;
1938 ✗ for (j = 0; j < 3; j++)
1939 ✗ cam2rgb[i][j] /= num;
1940 ✗ s->premultiply[i] = 1.f / num;
1941 }
1942 ✗ }
1943
1944 46 static int decode_frame(AVCodecContext *avctx, AVFrame *p,
1945 int *got_frame, AVPacket *avpkt)
1946 {
1947 46 TiffContext *const s = avctx->priv_data;
1948 46 unsigned off, last_off = 0;
1949 int le, ret, plane, planes;
1950 int i, j, entries, stride;
1951 unsigned soff, ssize;
1952 GetByteContext stripsizes;
1953 GetByteContext stripdata;
1954 int retry_for_subifd, retry_for_page;
1955 int is_dng;
1956 int has_tile_bits, has_strip_bits;
1957
1958 46 av_exif_free(&s->exif_meta);
1959 /* this will not parse the image data */
1960 46 ret = av_exif_parse_buffer(avctx, avpkt->data, avpkt->size, &s->exif_meta, AV_EXIF_TIFF_HEADER);
1961
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 45 times.
46 if (ret < 0)
1962 1 av_log(avctx, AV_LOG_ERROR, "could not parse EXIF data: %s\n", av_err2str(ret));
1963
1964 46 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1965
1966 // parse image header
1967
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
1968 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
1969 ✗ return ret;
1970
3/4
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 45 times.
46 } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1971 1 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1972 1 return AVERROR_INVALIDDATA;
1973 }
1974 45 s->le = le;
1975 // TIFF_BPP is not a required tag and defaults to 1
1976
1977 45 s->tiff_type = TIFF_TYPE_TIFF;
1978 45 s->use_color_matrix = 0;
1979 45 again:
1980 45 s->is_thumbnail = 0;
1981 45 s->bppcount = s->bpp = 1;
1982 45 s->photometric = TIFF_PHOTOMETRIC_NONE;
1983 45 s->compr = TIFF_RAW;
1984 45 s->fill_order = 0;
1985 45 s->white_level = 0;
1986 45 s->is_bayer = 0;
1987 45 s->is_tiled = 0;
1988 45 s->is_jpeg = 0;
1989 45 s->cur_page = 0;
1990 45 s->last_tag = 0;
1991
1992
2/2
✓ Branch 0 taken 2949120 times.
✓ Branch 1 taken 45 times.
2949165 for (i = 0; i < 65536; i++)
1993 2949120 s->dng_lut[i] = i;
1994
1995
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 45 times.
225 for (i = 0; i < FF_ARRAY_ELEMS(s->black_level); i++)
1996 180 s->black_level[i] = 0.f;
1997
1998
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 45 times.
225 for (i = 0; i < FF_ARRAY_ELEMS(s->as_shot_neutral); i++)
1999 180 s->as_shot_neutral[i] = 0.f;
2000
2001
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 45 times.
225 for (i = 0; i < FF_ARRAY_ELEMS(s->as_shot_white); i++)
2002 180 s->as_shot_white[i] = 1.f;
2003
2004
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 45 times.
225 for (i = 0; i < FF_ARRAY_ELEMS(s->analog_balance); i++)
2005 180 s->analog_balance[i] = 1.f;
2006
2007
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 45 times.
225 for (i = 0; i < FF_ARRAY_ELEMS(s->premultiply); i++)
2008 180 s->premultiply[i] = 1.f;
2009
2010
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 45 times.
225 for (i = 0; i < 4; i++)
2011
2/2
✓ Branch 0 taken 720 times.
✓ Branch 1 taken 180 times.
900 for (j = 0; j < 4; j++)
2012
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 540 times.
720 s->camera_calibration[i][j] = i == j;
2013
2014 45 free_geotags(s);
2015
2016 // Reset these offsets so we can tell if they were set this frame
2017 45 s->stripsizesoff = s->strippos = 0;
2018 /* parse image file directory */
2019 45 bytestream2_seek(&s->gb, off, SEEK_SET);
2020 45 entries = ff_tget_short(&s->gb, le);
2021
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
45 if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
2022 ✗ return AVERROR_INVALIDDATA;
2023
2/2
✓ Branch 0 taken 699 times.
✓ Branch 1 taken 45 times.
744 for (i = 0; i < entries; i++) {
2024
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 699 times.
699 if ((ret = tiff_decode_tag(s, p)) < 0)
2025 ✗ return ret;
2026 }
2027
2028
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
45 if (s->get_thumbnail && !s->is_thumbnail) {
2029 ✗ av_log(avctx, AV_LOG_INFO, "No embedded thumbnail present\n");
2030 ✗ return 0;
2031 }
2032
2033 /** whether we should process this IFD's SubIFD */
2034
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 45 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.
45 retry_for_subifd = s->sub_ifd && (s->get_subimage || (!s->get_thumbnail && s->is_thumbnail));
2035 /** whether we should process this multi-page IFD's next page */
2036
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
45 retry_for_page = s->get_page && s->cur_page + 1 < s->get_page; // get_page is 1-indexed
2037
2038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (retry_for_page) {
2039 // set offset to the next IFD
2040 ✗ off = ff_tget_long(&s->gb, le);
2041
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 } else if (retry_for_subifd) {
2042 // set offset to the SubIFD
2043 ✗ off = s->sub_ifd;
2044 }
2045
2046
2/4
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 45 times.
45 if (retry_for_subifd || retry_for_page) {
2047 ✗ if (!off) {
2048 ✗ av_log(avctx, AV_LOG_ERROR, "Requested entry not found\n");
2049 ✗ return AVERROR_INVALIDDATA;
2050 }
2051 ✗ if (off <= last_off) {
2052 ✗ avpriv_request_sample(s->avctx, "non increasing IFD offset");
2053 ✗ return AVERROR_INVALIDDATA;
2054 }
2055 ✗ last_off = off;
2056 ✗ if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
2057 ✗ av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
2058 ✗ return AVERROR_INVALIDDATA;
2059 }
2060 ✗ s->sub_ifd = 0;
2061 ✗ goto again;
2062 }
2063
2064 /* At this point we've decided on which (Sub)IFD to process */
2065
2066
2/4
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 45 times.
45 is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
2067
2068
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 for (i = 0; i<s->geotag_count; i++) {
2069 ✗ const char *keyname = get_geokey_name(s->geotags[i].key);
2070 ✗ if (!keyname) {
2071 ✗ av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
2072 ✗ continue;
2073 }
2074 ✗ if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
2075 ✗ av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
2076 ✗ continue;
2077 }
2078 ✗ ret = av_dict_set(&p->metadata, keyname, s->geotags[i].val, AV_DICT_DONT_STRDUP_VAL);
2079 ✗ s->geotags[i].val = NULL;
2080 ✗ if (ret<0) {
2081 ✗ av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
2082 ✗ return ret;
2083 }
2084 }
2085
2086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (is_dng) {
2087 double cam2xyz[4][3];
2088 float cmatrix[3][4];
2089 ✗ float pmin = FLT_MAX;
2090 int bps;
2091
2092 ✗ for (i = 0; i < 3; i++) {
2093 ✗ for (j = 0; j < 3; j++)
2094 ✗ s->camera_calibration[i][j] *= s->analog_balance[i];
2095 }
2096
2097 ✗ if (!s->use_color_matrix) {
2098 ✗ for (i = 0; i < 3; i++) {
2099 ✗ if (s->camera_calibration[i][i])
2100 ✗ s->premultiply[i] /= s->camera_calibration[i][i];
2101 }
2102 } else {
2103 ✗ for (int c = 0; c < 3; c++) {
2104 ✗ for (i = 0; i < 3; i++) {
2105 ✗ cam2xyz[c][i] = 0.;
2106 ✗ for (j = 0; j < 3; j++)
2107 ✗ cam2xyz[c][i] += s->camera_calibration[c][j] * s->color_matrix[j][i] * s->as_shot_white[i];
2108 }
2109 }
2110
2111 ✗ camera_xyz_coeff(s, cmatrix, cam2xyz);
2112 }
2113
2114 ✗ for (int c = 0; c < 3; c++)
2115 ✗ pmin = fminf(pmin, s->premultiply[c]);
2116
2117 ✗ for (int c = 0; c < 3; c++)
2118 ✗ s->premultiply[c] /= pmin;
2119
2120 ✗ if (s->bpp % s->bppcount)
2121 ✗ return AVERROR_INVALIDDATA;
2122 ✗ bps = s->bpp / s->bppcount;
2123 ✗ if (bps < 8 || bps > 32)
2124 ✗ return AVERROR_INVALIDDATA;
2125
2126 ✗ if (s->white_level == 0)
2127 ✗ s->white_level = (1LL << bps) - 1; /* Default value as per the spec */
2128
2129 ✗ if (s->white_level <= s->black_level[0]) {
2130 ✗ av_log(avctx, AV_LOG_ERROR, "BlackLevel (%g) must be less than WhiteLevel (%"PRId32")\n",
2131 ✗ s->black_level[0], s->white_level);
2132 ✗ return AVERROR_INVALIDDATA;
2133 }
2134
2135 ✗ if (s->planar)
2136 ✗ return AVERROR_PATCHWELCOME;
2137 }
2138
2139
4/6
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 27 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
45 if (!s->is_tiled && !s->strippos && !s->stripoff) {
2140 ✗ av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
2141 ✗ return AVERROR_INVALIDDATA;
2142 }
2143
2144
5/10
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 45 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 45 times.
45 has_tile_bits = s->is_tiled || s->tile_byte_counts_offset || s->tile_offsets_offset || s->tile_width || s->tile_length;
2145
3/16
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ 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.
45 has_strip_bits = s->strippos || s->strips || s->stripoff || s->rps || s->sot || s->sstype || s->stripsize || s->stripsizesoff;
2146
2147
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
45 if (has_tile_bits && has_strip_bits) {
2148 ✗ int tiled_dng = s->is_tiled && is_dng;
2149 ✗ av_log(avctx, tiled_dng ? AV_LOG_WARNING : AV_LOG_ERROR, "Tiled TIFF is not allowed to strip\n");
2150 ✗ if (!tiled_dng)
2151 ✗ return AVERROR_INVALIDDATA;
2152 }
2153
2154 /* now we have the data and may start decoding */
2155
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 28 times.
45 if ((ret = init_image(s, p)) <= 0)
2156 17 return ret;
2157
2158
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
28 if (!s->is_tiled || has_strip_bits) {
2159
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
28 if (s->strips == 1 && !s->stripsize) {
2160 ✗ av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
2161 ✗ s->stripsize = avpkt->size - s->stripoff;
2162 }
2163
2164
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 9 times.
28 if (s->stripsizesoff) {
2165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (s->stripsizesoff >= (unsigned)avpkt->size)
2166 ✗ return AVERROR_INVALIDDATA;
2167 19 bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
2168 19 avpkt->size - s->stripsizesoff);
2169 }
2170
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 9 times.
28 if (s->strippos) {
2171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (s->strippos >= (unsigned)avpkt->size)
2172 ✗ return AVERROR_INVALIDDATA;
2173 19 bytestream2_init(&stripdata, avpkt->data + s->strippos,
2174 19 avpkt->size - s->strippos);
2175 }
2176
2177
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
28 if (s->rps <= 0 || s->rps % s->subsampling[1]) {
2178 ✗ av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
2179 ✗ return AVERROR_INVALIDDATA;
2180 }
2181 }
2182
2183
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (s->photometric == TIFF_PHOTOMETRIC_LINEAR_RAW ||
2184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 s->photometric == TIFF_PHOTOMETRIC_CFA) {
2185 ✗ p->color_trc = AVCOL_TRC_LINEAR;
2186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 } else if (s->photometric == TIFF_PHOTOMETRIC_BLACK_IS_ZERO) {
2187 ✗ p->color_trc = AVCOL_TRC_GAMMA22;
2188 }
2189
2190 /* Handle DNG images with JPEG-compressed tiles */
2191
2192
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
28 if (is_dng && s->is_tiled) {
2193 ✗ if (!s->is_jpeg) {
2194 ✗ avpriv_report_missing_feature(avctx, "DNG uncompressed tiled images");
2195 ✗ return AVERROR_PATCHWELCOME;
2196 ✗ } else if (!s->is_bayer) {
2197 ✗ avpriv_report_missing_feature(avctx, "DNG JPG-compressed tiled non-bayer-encoded images");
2198 ✗ return AVERROR_PATCHWELCOME;
2199 } else {
2200 ✗ if ((ret = dng_decode_tiles(avctx, p, avpkt)) > 0)
2201 ✗ *got_frame = 1;
2202 ✗ return ret;
2203 }
2204 }
2205
2206 /* Handle TIFF images and DNG images with uncompressed strips (non-tiled) */
2207
2208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 planes = s->planar ? s->bppcount : 1;
2209
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
56 for (plane = 0; plane < planes; plane++) {
2210 28 uint8_t *five_planes = NULL;
2211 28 int remaining = avpkt->size;
2212 int decoded_height;
2213 28 uint8_t *dst = p->data[plane];
2214 28 stride = p->linesize[plane];
2215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED &&
2216 ✗ s->avctx->pix_fmt == AV_PIX_FMT_RGBA) {
2217 ✗ stride = stride * 5 / 4;
2218 ✗ five_planes =
2219 ✗ dst = av_malloc(stride * s->height);
2220 ✗ if (!dst)
2221 ✗ return AVERROR(ENOMEM);
2222 }
2223
2/2
✓ Branch 0 taken 627 times.
✓ Branch 1 taken 28 times.
655 for (i = 0; i < s->height; i += s->rps) {
2224
2/2
✓ Branch 0 taken 599 times.
✓ Branch 1 taken 28 times.
627 if (i)
2225 599 dst += s->rps * stride;
2226
2/2
✓ Branch 0 taken 618 times.
✓ Branch 1 taken 9 times.
627 if (s->stripsizesoff)
2227 618 ssize = ff_tget(&stripsizes, s->sstype, le);
2228 else
2229 9 ssize = s->stripsize;
2230
2231
2/2
✓ Branch 0 taken 618 times.
✓ Branch 1 taken 9 times.
627 if (s->strippos)
2232 618 soff = ff_tget(&stripdata, s->sot, le);
2233 else
2234 9 soff = s->stripoff;
2235
2236
3/6
✓ Branch 0 taken 627 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 627 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 627 times.
627 if (soff > avpkt->size || ssize > avpkt->size - soff || ssize > remaining) {
2237 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
2238 ✗ av_freep(&five_planes);
2239 ✗ return AVERROR_INVALIDDATA;
2240 }
2241 627 remaining -= ssize;
2242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 627 times.
627 if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,
2243 627 FFMIN(s->rps, s->height - i))) < 0) {
2244 ✗ if (avctx->err_recognition & AV_EF_EXPLODE) {
2245 ✗ av_freep(&five_planes);
2246 ✗ return ret;
2247 }
2248 ✗ break;
2249 }
2250 }
2251 28 decoded_height = FFMIN(i, s->height);
2252
2253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (s->predictor == 2) {
2254 ✗ if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
2255 ✗ av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");
2256 ✗ return AVERROR_PATCHWELCOME;
2257 }
2258 ✗ dst = five_planes ? five_planes : p->data[plane];
2259 ✗ soff = s->bpp >> 3;
2260 ✗ if (s->planar)
2261 ✗ soff = FFMAX(soff / s->bppcount, 1);
2262 ✗ ssize = s->width * soff;
2263 ✗ if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
2264 ✗ s->avctx->pix_fmt == AV_PIX_FMT_RGBA64LE ||
2265 ✗ s->avctx->pix_fmt == AV_PIX_FMT_GRAY16LE ||
2266 ✗ s->avctx->pix_fmt == AV_PIX_FMT_YA16LE ||
2267 ✗ s->avctx->pix_fmt == AV_PIX_FMT_GBRP16LE ||
2268 ✗ s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16LE) {
2269 ✗ for (i = 0; i < decoded_height; i++) {
2270 ✗ for (j = soff; j < ssize; j += 2)
2271 ✗ AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
2272 ✗ dst += stride;
2273 }
2274 ✗ } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
2275 ✗ s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE ||
2276 ✗ s->avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
2277 ✗ s->avctx->pix_fmt == AV_PIX_FMT_YA16BE ||
2278 ✗ s->avctx->pix_fmt == AV_PIX_FMT_GBRP16BE ||
2279 ✗ s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16BE) {
2280 ✗ for (i = 0; i < decoded_height; i++) {
2281 ✗ for (j = soff; j < ssize; j += 2)
2282 ✗ AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
2283 ✗ dst += stride;
2284 }
2285 } else {
2286 ✗ for (i = 0; i < decoded_height; i++) {
2287 ✗ for (j = soff; j < ssize; j++)
2288 ✗ dst[j] += dst[j - soff];
2289 ✗ dst += stride;
2290 }
2291 }
2292 }
2293
2294 /* Floating point predictor
2295 TIFF Technical Note 3 http://chriscox.org/TIFFTN3d1.pdf */
2296
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
28 if (s->predictor == 3) {
2297 4 int channels = s->bppcount;
2298 int group_size;
2299 uint8_t *tmpbuf;
2300 int bpc;
2301
2302
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 dst = five_planes ? five_planes : p->data[plane];
2303 4 soff = s->bpp >> 3;
2304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->planar) {
2305 ✗ soff = FFMAX(soff / s->bppcount, 1);
2306 ✗ channels = 1;
2307 }
2308 4 ssize = s->width * soff;
2309 4 bpc = FFMAX(soff / s->bppcount, 1); /* Bytes per component */
2310 4 group_size = s->width * channels;
2311
2312 4 tmpbuf = av_malloc(ssize);
2313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!tmpbuf) {
2314 ✗ av_free(five_planes);
2315 ✗ return AVERROR(ENOMEM);
2316 }
2317
2318
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (s->avctx->pix_fmt == AV_PIX_FMT_RGBF32LE ||
2319
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 s->avctx->pix_fmt == AV_PIX_FMT_RGBAF32LE) {
2320
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 4 times.
36 for (i = 0; i < decoded_height; i++) {
2321 /* Copy first sample byte for each channel */
2322
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 32 times.
144 for (j = 0; j < channels; j++)
2323 112 tmpbuf[j] = dst[j];
2324
2325 /* Decode horizontal differences */
2326
2/2
✓ Branch 0 taken 3472 times.
✓ Branch 1 taken 32 times.
3504 for (j = channels; j < ssize; j++)
2327 3472 tmpbuf[j] = dst[j] + tmpbuf[j-channels];
2328
2329 /* Combine shuffled bytes from their separate groups. Each
2330 byte of every floating point value in a row of pixels is
2331 split and combined into separate groups. A group of all
2332 the sign/exponents bytes in the row and groups for each
2333 of the upper, mid, and lower mantissa bytes in the row. */
2334
2/2
✓ Branch 0 taken 896 times.
✓ Branch 1 taken 32 times.
928 for (j = 0; j < group_size; j++) {
2335
2/2
✓ Branch 0 taken 3584 times.
✓ Branch 1 taken 896 times.
4480 for (int k = 0; k < bpc; k++) {
2336 3584 dst[bpc * j + k] = tmpbuf[(bpc - k - 1) * group_size + j];
2337 }
2338 }
2339 32 dst += stride;
2340 }
2341 ✗ } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGBF32BE ||
2342 ✗ s->avctx->pix_fmt == AV_PIX_FMT_RGBAF32BE) {
2343 /* Same as LE only the shuffle at the end is reversed */
2344 ✗ for (i = 0; i < decoded_height; i++) {
2345 ✗ for (j = 0; j < channels; j++)
2346 ✗ tmpbuf[j] = dst[j];
2347
2348 ✗ for (j = channels; j < ssize; j++)
2349 ✗ tmpbuf[j] = dst[j] + tmpbuf[j-channels];
2350
2351 ✗ for (j = 0; j < group_size; j++) {
2352 ✗ for (int k = 0; k < bpc; k++) {
2353 ✗ dst[bpc * j + k] = tmpbuf[k * group_size + j];
2354 }
2355 }
2356 ✗ dst += stride;
2357 }
2358 } else {
2359 ✗ av_log(s->avctx, AV_LOG_ERROR, "unsupported floating point pixel format\n");
2360 }
2361 4 av_free(tmpbuf);
2362 }
2363
2364
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 26 times.
28 if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {
2365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 int c = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255);
2366 2 dst = p->data[plane];
2367
2/2
✓ Branch 0 taken 6496 times.
✓ Branch 1 taken 2 times.
6498 for (i = 0; i < s->height; i++) {
2368
2/2
✓ Branch 0 taken 2078720 times.
✓ Branch 1 taken 6496 times.
2085216 for (j = 0; j < stride; j++)
2369 2078720 dst[j] = c - dst[j];
2370 6496 dst += stride;
2371 }
2372 }
2373
2374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED &&
2375 ✗ (s->avctx->pix_fmt == AV_PIX_FMT_RGB0 || s->avctx->pix_fmt == AV_PIX_FMT_RGBA)) {
2376 ✗ int x = s->avctx->pix_fmt == AV_PIX_FMT_RGB0 ? 4 : 5;
2377 ✗ uint8_t *src = five_planes ? five_planes : p->data[plane];
2378 ✗ dst = p->data[plane];
2379 ✗ for (i = 0; i < s->height; i++) {
2380 ✗ for (j = 0; j < s->width; j++) {
2381 ✗ int k = 255 - src[x * j + 3];
2382 ✗ int r = (255 - src[x * j ]) * k;
2383 ✗ int g = (255 - src[x * j + 1]) * k;
2384 ✗ int b = (255 - src[x * j + 2]) * k;
2385 ✗ dst[4 * j ] = r * 257 >> 16;
2386 ✗ dst[4 * j + 1] = g * 257 >> 16;
2387 ✗ dst[4 * j + 2] = b * 257 >> 16;
2388 ✗ dst[4 * j + 3] = s->avctx->pix_fmt == AV_PIX_FMT_RGBA ? src[x * j + 4] : 255;
2389 }
2390 ✗ src += stride;
2391 ✗ dst += p->linesize[plane];
2392 }
2393 ✗ av_freep(&five_planes);
2394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 } else if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED &&
2395 ✗ s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE) {
2396 ✗ dst = p->data[plane];
2397 ✗ for (i = 0; i < s->height; i++) {
2398 ✗ for (j = 0; j < s->width; j++) {
2399 ✗ uint64_t k = 65535 - AV_RB16(dst + 8 * j + 6);
2400 ✗ uint64_t r = (65535 - AV_RB16(dst + 8 * j )) * k;
2401 ✗ uint64_t g = (65535 - AV_RB16(dst + 8 * j + 2)) * k;
2402 ✗ uint64_t b = (65535 - AV_RB16(dst + 8 * j + 4)) * k;
2403 ✗ AV_WB16(dst + 8 * j , r * 65537 >> 32);
2404 ✗ AV_WB16(dst + 8 * j + 2, g * 65537 >> 32);
2405 ✗ AV_WB16(dst + 8 * j + 4, b * 65537 >> 32);
2406 ✗ AV_WB16(dst + 8 * j + 6, 65535);
2407 }
2408 ✗ dst += p->linesize[plane];
2409 }
2410 }
2411 }
2412
2413
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
28 if (s->planar && s->bppcount > 2) {
2414 ✗ FFSWAP(uint8_t*, p->data[0], p->data[2]);
2415 ✗ FFSWAP(int, p->linesize[0], p->linesize[2]);
2416 ✗ FFSWAP(uint8_t*, p->data[0], p->data[1]);
2417 ✗ FFSWAP(int, p->linesize[0], p->linesize[1]);
2418 }
2419
2420
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 28 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.
28 if (s->is_bayer && s->white_level && s->bpp == 16 && !is_dng) {
2421 ✗ uint16_t *dst = (uint16_t *)p->data[0];
2422 ✗ for (i = 0; i < s->height; i++) {
2423 ✗ for (j = 0; j < s->width; j++)
2424 ✗ dst[j] = FFMIN((dst[j] / (float)s->white_level) * 65535, 65535);
2425 ✗ dst += stride / 2;
2426 }
2427 }
2428
2429 28 ret = ff_decode_exif_attach_ifd(avctx, p, &s->exif_meta);
2430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (ret < 0)
2431 ✗ av_log(avctx, AV_LOG_ERROR, "error attaching EXIF ifd: %s\n", av_err2str(ret));
2432
2433 28 *got_frame = 1;
2434
2435 28 return avpkt->size;
2436 }
2437
2438 35 static av_cold int tiff_init(AVCodecContext *avctx)
2439 {
2440 35 TiffContext *s = avctx->priv_data;
2441 int ret;
2442
2443 35 s->width = 0;
2444 35 s->height = 0;
2445 35 s->subsampling[0] =
2446 35 s->subsampling[1] = 1;
2447 35 s->avctx = avctx;
2448 35 ff_lzw_decode_open(&s->lzw);
2449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (!s->lzw)
2450 ✗ return AVERROR(ENOMEM);
2451 35 ff_ccitt_unpack_init();
2452
2453 /* Allocate JPEG frame */
2454 35 s->jpgframe = av_frame_alloc();
2455 35 s->jpkt = av_packet_alloc();
2456
2/4
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35 times.
35 if (!s->jpgframe || !s->jpkt)
2457 ✗ return AVERROR(ENOMEM);
2458
2459 /* Prepare everything needed for JPEG decoding */
2460 EXTERN const FFCodec ff_mjpeg_decoder;
2461 35 s->avctx_mjpeg = avcodec_alloc_context3(&ff_mjpeg_decoder.p);
2462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (!s->avctx_mjpeg)
2463 ✗ return AVERROR(ENOMEM);
2464 35 s->avctx_mjpeg->flags = avctx->flags;
2465 35 s->avctx_mjpeg->flags2 = avctx->flags2;
2466 35 s->avctx_mjpeg->idct_algo = avctx->idct_algo;
2467 35 s->avctx_mjpeg->max_pixels = avctx->max_pixels;
2468 35 ret = avcodec_open2(s->avctx_mjpeg, NULL, NULL);
2469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (ret < 0) {
2470 ✗ return ret;
2471 }
2472
2473 35 return 0;
2474 }
2475
2476 35 static av_cold int tiff_end(AVCodecContext *avctx)
2477 {
2478 35 TiffContext *const s = avctx->priv_data;
2479
2480 35 free_geotags(s);
2481 35 av_exif_free(&s->exif_meta);
2482
2483 35 ff_lzw_decode_close(&s->lzw);
2484 35 av_freep(&s->deinvert_buf);
2485 35 s->deinvert_buf_size = 0;
2486 35 av_freep(&s->yuv_line);
2487 35 s->yuv_line_size = 0;
2488 35 av_frame_free(&s->jpgframe);
2489 35 av_packet_free(&s->jpkt);
2490 35 avcodec_free_context(&s->avctx_mjpeg);
2491 35 return 0;
2492 }
2493
2494 #define OFFSET(x) offsetof(TiffContext, x)
2495 static const AVOption tiff_options[] = {
2496 { "subimage", "decode subimage instead if available", OFFSET(get_subimage), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
2497 { "thumbnail", "decode embedded thumbnail subimage instead if available", OFFSET(get_thumbnail), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
2498 { "page", "page number of multi-page image to decode (starting from 1)", OFFSET(get_page), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
2499 { NULL },
2500 };
2501
2502 static const AVClass tiff_decoder_class = {
2503 .class_name = "TIFF decoder",
2504 .item_name = av_default_item_name,
2505 .option = tiff_options,
2506 .version = LIBAVUTIL_VERSION_INT,
2507 };
2508
2509 const FFCodec ff_tiff_decoder = {
2510 .p.name = "tiff",
2511 CODEC_LONG_NAME("TIFF image"),
2512 .p.type = AVMEDIA_TYPE_VIDEO,
2513 .p.id = AV_CODEC_ID_TIFF,
2514 .priv_data_size = sizeof(TiffContext),
2515 .init = tiff_init,
2516 .close = tiff_end,
2517 FF_CODEC_DECODE_CB(decode_frame),
2518 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
2519 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_ICC_PROFILES |
2520 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2521 .p.priv_class = &tiff_decoder_class,
2522 };
2523