FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tiff.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 511 1549 33.0%
Functions: 15 31 48.4%
Branches: 335 1295 25.9%

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