FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tiff.c
Date: 2025-09-01 20:07:09
Exec Total Coverage
Lines: 469 1549 30.3%
Functions: 14 31 45.2%
Branches: 302 1295 23.3%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2006 Konstantin Shishkov
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * TIFF image decoder
24 * @author Konstantin Shishkov
25 */
26
27 #include "config.h"
28 #if CONFIG_ZLIB
29 #include <zlib.h>
30 #endif
31 #if CONFIG_LZMA
32 #define LZMA_API_STATIC
33 #include <lzma.h>
34 #endif
35
36 #include <float.h>
37
38 #include "libavutil/attributes.h"
39 #include "libavutil/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 54 static void free_geotags(TiffContext *const s)
140 {
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 for (int i = 0; i < s->geotag_count; i++)
142 av_freep(&s->geotags[i].val);
143 54 av_freep(&s->geotags);
144 54 s->geotag_count = 0;
145 54 }
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 36484 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 36484 times.
36484 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 36484 default:
437
2/2
✓ Branch 0 taken 34369 times.
✓ Branch 1 taken 2115 times.
36484 if (usePtr) {
438 34369 memcpy(dst + offset, src, width);
439 } else {
440 2115 memset(dst + offset, c, width);
441 }
442 }
443 36484 }
444
445 18 static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
446 {
447 int i;
448
449 18 av_fast_padded_malloc(&s->deinvert_buf, &s->deinvert_buf_size, size);
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!s->deinvert_buf)
451 return AVERROR(ENOMEM);
452
2/2
✓ Branch 0 taken 251119 times.
✓ Branch 1 taken 18 times.
251137 for (i = 0; i < size; i++)
453 251119 s->deinvert_buf[i] = ff_reverse[src[i]];
454
455 18 return 0;
456 }
457
458 static void unpack_gray(TiffContext *s, AVFrame *p,
459 const uint8_t *src, int lnum, int width, int bpp)
460 {
461 GetBitContext gb;
462 uint16_t *dst = (uint16_t *)(p->data[0] + lnum * p->linesize[0]);
463
464 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 static void unpack_yuv(TiffContext *s, AVFrame *p,
473 const uint8_t *src, int lnum)
474 {
475 int i, j, k;
476 int w = (s->width - 1) / s->subsampling[0] + 1;
477 uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
478 uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
479 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 for (i = 0; i < w; i++) {
490 for (j = 0; j < s->subsampling[1]; j++)
491 for (k = 0; k < s->subsampling[0]; k++)
492 p->data[0][(lnum + j) * p->linesize[0] +
493 i * s->subsampling[0] + k] = *src++;
494 *pu++ = *src++;
495 *pv++ = *src++;
496 }
497 }
498 }
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 579 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 579 const uint8_t *ssrc = src;
749 579 int width = ((s->width * s->bpp) + 7) >> 3;
750 579 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(p->format);
751 1176 int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
752
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 561 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
579 (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
753 desc->nb_components >= 3;
754 int is_dng;
755
756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
579 if (s->planar)
757 width /= s->bppcount;
758
759
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
579 if (size <= 0)
760 return AVERROR_INVALIDDATA;
761
762
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
579 if (is_yuv) {
763 int bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
764 s->subsampling[0] * s->subsampling[1] + 7) >> 3;
765 av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
766 if (s->yuv_line == NULL) {
767 av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
768 return AVERROR(ENOMEM);
769 }
770 dst = s->yuv_line;
771 stride = 0;
772
773 width = (s->width - 1) / s->subsampling[0] + 1;
774 width = width * s->subsampling[0] * s->subsampling[1] + 2*width;
775 av_assert0(width <= bytes_per_row);
776 av_assert0(s->bpp == 24);
777 }
778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
579 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 579 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
579 av_assert0(!(s->is_bayer && is_yuv));
782
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
579 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 579 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 577 times.
579 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
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 577 times.
577 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 575 times.
577 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 575 times.
✗ Branch 1 not taken.
575 if (s->compr == TIFF_CCITT_RLE ||
846
2/2
✓ Branch 0 taken 557 times.
✓ Branch 1 taken 18 times.
575 s->compr == TIFF_G3 ||
847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 557 times.
557 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 557 bytestream2_init(&s->gb, src, size);
855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 557 times.
557 bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * lines));
856
857
2/4
✓ Branch 0 taken 557 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 557 times.
557 is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
858
859 /* Decode JPEG-encoded DNGs with strips */
860
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 557 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
557 if (s->compr == TIFF_NEWJPEG && is_dng) {
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 557 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
557 if (is_dng && stride == 0)
873 return AVERROR_INVALIDDATA;
874
875
2/2
✓ Branch 0 taken 3872 times.
✓ Branch 1 taken 557 times.
4429 for (line = 0; line < lines; line++) {
876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3872 times.
3872 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 3872 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3872 times.
✗ Branch 5 not taken.
3872 if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
882 break;
883 3872 bytestream2_seek_p(&pb, stride * line, SEEK_SET);
884
2/3
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 3744 times.
✗ Branch 2 not taken.
3872 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 3744 case TIFF_PACKBITS:
923
2/2
✓ Branch 0 taken 36356 times.
✓ Branch 1 taken 3744 times.
40100 for (pixels = 0; pixels < width;) {
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36356 times.
36356 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 36356 times.
36356 code = s->fill_order ? (int8_t) ff_reverse[*src++]: (int8_t) *src++;
929
2/2
✓ Branch 0 taken 34241 times.
✓ Branch 1 taken 2115 times.
36356 if (code >= 0) {
930 34241 code++;
931
1/2
✓ Branch 0 taken 34241 times.
✗ Branch 1 not taken.
34241 if (pixels + code > width ||
932
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34241 times.
34241 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 34241 horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
938 dst, 1, src, 0, code, pixels);
939 34241 src += code;
940 34241 pixels += code;
941
1/2
✓ Branch 0 taken 2115 times.
✗ Branch 1 not taken.
2115 } else if (code != -128) { // -127..-1
942 2115 code = (-code) + 1;
943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2115 times.
2115 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 2115 c = *src++;
949 2115 horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
950 dst, 0, NULL, c, code, pixels);
951 2115 pixels += code;
952 }
953 }
954
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3744 times.
3744 if (s->fill_order) {
955 int i;
956 for (i = 0; i < width; i++)
957 dst[i] = ff_reverse[dst[i]];
958 }
959 3744 break;
960 }
961
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3872 times.
3872 if (is_yuv) {
962 unpack_yuv(s, p, dst, strip_start + line);
963 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 3872 dst += stride;
968 }
969 557 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 33 static int init_image(TiffContext *s, AVFrame *frame)
1046 {
1047 int ret;
1048 33 int create_gray_palette = 0;
1049
1050 // make sure there is no aliasing in the following switch
1051
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 if (s->bpp > 128 || s->bppcount >= 10) {
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 17 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 6 times.
✓ Branch 19 taken 6 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
33 switch (s->planar * 10000 + s->bpp * 10 + s->bppcount + s->is_bayer * 100000) {
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 case 21:
1065 case 41:
1066 s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
1067 if (!s->palette_is_set) {
1068 create_gray_palette = 1;
1069 }
1070 break;
1071 case 81:
1072 s->avctx->pix_fmt = s->palette_is_set ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
1073 break;
1074 case 121:
1075 s->avctx->pix_fmt = AV_PIX_FMT_GRAY12;
1076 break;
1077 case 100081:
1078 switch (AV_RL32(s->pattern)) {
1079 case 0x02010100:
1080 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB8;
1081 break;
1082 case 0x00010102:
1083 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR8;
1084 break;
1085 case 0x01000201:
1086 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG8;
1087 break;
1088 case 0x01020001:
1089 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG8;
1090 break;
1091 default:
1092 av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n",
1093 AV_RL32(s->pattern));
1094 return AVERROR_PATCHWELCOME;
1095 }
1096 break;
1097 case 100101:
1098 case 100121:
1099 case 100141:
1100 case 100161:
1101 switch (AV_RL32(s->pattern)) {
1102 case 0x02010100:
1103 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB16;
1104 break;
1105 case 0x00010102:
1106 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR16;
1107 break;
1108 case 0x01000201:
1109 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG16;
1110 break;
1111 case 0x01020001:
1112 s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG16;
1113 break;
1114 default:
1115 av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n",
1116 AV_RL32(s->pattern));
1117 return AVERROR_PATCHWELCOME;
1118 }
1119 break;
1120 17 case 243:
1121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1122 if (s->subsampling[0] == 1 && s->subsampling[1] == 1) {
1123 s->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
1124 } else if (s->subsampling[0] == 2 && s->subsampling[1] == 1) {
1125 s->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
1126 } else if (s->subsampling[0] == 4 && s->subsampling[1] == 1) {
1127 s->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
1128 } else if (s->subsampling[0] == 1 && s->subsampling[1] == 2) {
1129 s->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
1130 } else if (s->subsampling[0] == 2 && s->subsampling[1] == 2) {
1131 s->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1132 } else if (s->subsampling[0] == 4 && s->subsampling[1] == 4) {
1133 s->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
1134 } else {
1135 av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr subsampling\n");
1136 return AVERROR_PATCHWELCOME;
1137 }
1138 } else
1139 17 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
1140 17 break;
1141 case 161:
1142 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
1143 break;
1144 case 162:
1145 s->avctx->pix_fmt = AV_PIX_FMT_YA8;
1146 break;
1147 case 322:
1148 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YA16LE : AV_PIX_FMT_YA16BE;
1149 break;
1150 case 324:
1151 s->avctx->pix_fmt = s->photometric == TIFF_PHOTOMETRIC_SEPARATED ? AV_PIX_FMT_RGB0 : AV_PIX_FMT_RGBA;
1152 break;
1153 case 405:
1154 if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED)
1155 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
1156 else {
1157 av_log(s->avctx, AV_LOG_ERROR,
1158 "bpp=40 without PHOTOMETRIC_SEPARATED is unsupported\n");
1159 return AVERROR_PATCHWELCOME;
1160 }
1161 break;
1162 case 483:
1163 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
1164 break;
1165 case 644:
1166 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGBA64BE;
1167 break;
1168 case 10243:
1169 s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
1170 break;
1171 case 10324:
1172 s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
1173 break;
1174 case 10483:
1175 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRP16LE : AV_PIX_FMT_GBRP16BE;
1176 break;
1177 case 10644:
1178 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAP16LE : AV_PIX_FMT_GBRAP16BE;
1179 break;
1180 6 case 963:
1181
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;
1182 6 break;
1183 6 case 1284:
1184
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;
1185 6 break;
1186 case 10963:
1187 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRPF32LE : AV_PIX_FMT_GBRPF32BE;
1188 break;
1189 case 11284:
1190 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAPF32LE : AV_PIX_FMT_GBRAPF32BE;
1191 break;
1192 default:
1193 av_log(s->avctx, AV_LOG_ERROR,
1194 "This format is not supported (bpp=%d, bppcount=%d)\n",
1195 s->bpp, s->bppcount);
1196 return AVERROR_INVALIDDATA;
1197 }
1198
1199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1200 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1201 if((desc->flags & AV_PIX_FMT_FLAG_RGB) ||
1202 !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
1203 desc->nb_components < 3) {
1204 av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr variant\n");
1205 return AVERROR_INVALIDDATA;
1206 }
1207 }
1208
1209
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
33 if (s->width != s->avctx->width || s->height != s->avctx->height) {
1210 11 ret = ff_set_dimensions(s->avctx, s->width, s->height);
1211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ret < 0)
1212 return ret;
1213 }
1214
1215
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 22 times.
33 if (s->avctx->skip_frame >= AVDISCARD_ALL)
1216 11 return 0;
1217
1218
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 if ((ret = ff_thread_get_buffer(s->avctx, frame, 0)) < 0)
1219 return ret;
1220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1221 if (!create_gray_palette)
1222 memcpy(frame->data[1], s->palette, sizeof(s->palette));
1223 else {
1224 /* make default grayscale pal */
1225 int i;
1226 uint32_t *pal = (uint32_t *)frame->data[1];
1227 for (i = 0; i < 1<<s->bpp; i++)
1228 pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
1229 }
1230 }
1231 22 return 1;
1232 }
1233
1234 42 static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
1235 {
1236
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
42 int offset = tag == TIFF_YRES ? 2 : 0;
1237 42 s->res[offset++] = num;
1238 42 s->res[offset] = den;
1239
5/8
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
42 if (s->res[0] && s->res[1] && s->res[2] && s->res[3]) {
1240 33 uint64_t num = s->res[2] * (uint64_t)s->res[1];
1241 33 uint64_t den = s->res[0] * (uint64_t)s->res[3];
1242
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 if (num > INT64_MAX || den > INT64_MAX) {
1243 num = num >> 1;
1244 den = den >> 1;
1245 }
1246 33 av_reduce(&s->avctx->sample_aspect_ratio.num, &s->avctx->sample_aspect_ratio.den,
1247 num, den, INT32_MAX);
1248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (!s->avctx->sample_aspect_ratio.den)
1249 s->avctx->sample_aspect_ratio = (AVRational) {0, 1};
1250 }
1251 42 }
1252
1253 519 static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
1254 {
1255 AVFrameSideData *sd;
1256 GetByteContext gb_temp;
1257 519 unsigned tag, type, count, off, value = 0, value2 = 1; // value2 is a denominator so init. to 1
1258 int i, start;
1259 int pos;
1260 int ret;
1261 double *dp;
1262
1263 519 ret = ff_tread_tag(&s->gb, s->le, &tag, &type, &count, &start);
1264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
519 if (ret < 0) {
1265 goto end;
1266 }
1267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
519 if (tag <= s->last_tag)
1268 return AVERROR_INVALIDDATA;
1269
1270 // We ignore TIFF_STRIP_SIZE as it is sometimes in the logic but wrong order around TIFF_STRIP_OFFS
1271
2/2
✓ Branch 0 taken 486 times.
✓ Branch 1 taken 33 times.
519 if (tag != TIFF_STRIP_SIZE)
1272 486 s->last_tag = tag;
1273
1274 519 off = bytestream2_tell(&s->gb);
1275
2/2
✓ Branch 0 taken 374 times.
✓ Branch 1 taken 145 times.
519 if (count == 1) {
1276
2/4
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
374 switch (type) {
1277 332 case AV_TIFF_BYTE:
1278 case AV_TIFF_SHORT:
1279 case AV_TIFF_LONG:
1280 332 value = ff_tget(&s->gb, type, s->le);
1281 332 break;
1282 42 case AV_TIFF_RATIONAL:
1283 42 value = ff_tget_long(&s->gb, s->le);
1284 42 value2 = ff_tget_long(&s->gb, s->le);
1285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!value2) {
1286 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator in rational\n");
1287 value2 = 1;
1288 }
1289
1290 42 break;
1291 case AV_TIFF_STRING:
1292 if (count <= 4) {
1293 break;
1294 }
1295 default:
1296 value = UINT_MAX;
1297 }
1298 }
1299
1300
20/54
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 33 times.
✓ Branch 4 taken 33 times.
✓ Branch 5 taken 33 times.
✓ Branch 6 taken 33 times.
✓ Branch 7 taken 33 times.
✓ Branch 8 taken 33 times.
✓ Branch 9 taken 42 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 8 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 33 times.
✓ Branch 22 taken 16 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 18 times.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✓ Branch 37 taken 2 times.
✓ Branch 38 taken 6 times.
✗ Branch 39 not taken.
✓ Branch 40 taken 4 times.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✓ Branch 44 taken 12 times.
✓ Branch 45 taken 2 times.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✓ Branch 53 taken 97 times.
519 switch (tag) {
1301 15 case TIFF_SUBFILE:
1302 15 s->is_thumbnail = (value != 0);
1303 15 break;
1304 33 case TIFF_WIDTH:
1305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (value > INT_MAX)
1306 return AVERROR_INVALIDDATA;
1307 33 s->width = value;
1308 33 break;
1309 33 case TIFF_HEIGHT:
1310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (value > INT_MAX)
1311 return AVERROR_INVALIDDATA;
1312 33 s->height = value;
1313 33 break;
1314 33 case TIFF_BPP:
1315
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 if (count > 5 || count <= 0) {
1316 av_log(s->avctx, AV_LOG_ERROR,
1317 "This format is not supported (bpp=%d, %d components)\n",
1318 value, count);
1319 return AVERROR_INVALIDDATA;
1320 }
1321 33 s->bppcount = count;
1322
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 29 times.
33 if (count == 1)
1323 4 s->bpp = value;
1324 else {
1325
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 switch (type) {
1326 29 case AV_TIFF_BYTE:
1327 case AV_TIFF_SHORT:
1328 case AV_TIFF_LONG:
1329 29 s->bpp = 0;
1330
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
29 if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
1331 return AVERROR_INVALIDDATA;
1332
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 29 times.
122 for (i = 0; i < count; i++)
1333 93 s->bpp += ff_tget(&s->gb, type, s->le);
1334 29 break;
1335 default:
1336 s->bpp = -1;
1337 }
1338 }
1339 33 break;
1340 33 case TIFF_SAMPLES_PER_PIXEL:
1341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (count != 1) {
1342 av_log(s->avctx, AV_LOG_ERROR,
1343 "Samples per pixel requires a single value, many provided\n");
1344 return AVERROR_INVALIDDATA;
1345 }
1346
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 if (value > 5 || value <= 0) {
1347 av_log(s->avctx, AV_LOG_ERROR,
1348 "Invalid samples per pixel %d\n", value);
1349 return AVERROR_INVALIDDATA;
1350 }
1351
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 29 times.
33 if (s->bppcount == 1)
1352 4 s->bpp *= value;
1353 33 s->bppcount = value;
1354 33 break;
1355 33 case TIFF_COMPR:
1356 33 s->compr = value;
1357 33 av_log(s->avctx, AV_LOG_DEBUG, "compression: %d\n", s->compr);
1358 33 s->predictor = 0;
1359
3/6
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
33 switch (s->compr) {
1360 25 case TIFF_RAW:
1361 case TIFF_PACKBITS:
1362 case TIFF_LZW:
1363 case TIFF_CCITT_RLE:
1364 25 break;
1365 4 case TIFF_G3:
1366 case TIFF_G4:
1367 4 s->fax_opts = 0;
1368 4 break;
1369 4 case TIFF_DEFLATE:
1370 case TIFF_ADOBE_DEFLATE:
1371 #if CONFIG_ZLIB
1372 4 break;
1373 #else
1374 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
1375 return AVERROR(ENOSYS);
1376 #endif
1377 case TIFF_JPEG:
1378 case TIFF_NEWJPEG:
1379 s->is_jpeg = 1;
1380 break;
1381 case TIFF_LZMA:
1382 #if CONFIG_LZMA
1383 break;
1384 #else
1385 av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n");
1386 return AVERROR(ENOSYS);
1387 #endif
1388 default:
1389 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
1390 s->compr);
1391 return AVERROR_INVALIDDATA;
1392 }
1393 33 break;
1394 33 case TIFF_ROWSPERSTRIP:
1395
4/6
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
33 if (!value || (type == AV_TIFF_LONG && value == UINT_MAX))
1396 value = s->height;
1397 33 s->rps = FFMIN(value, s->height);
1398 33 break;
1399 33 case TIFF_STRIP_OFFS:
1400
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 19 times.
33 if (count == 1) {
1401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (value > INT_MAX) {
1402 av_log(s->avctx, AV_LOG_ERROR,
1403 "strippos %u too large\n", value);
1404 return AVERROR_INVALIDDATA;
1405 }
1406 14 s->strippos = 0;
1407 14 s->stripoff = value;
1408 } else
1409 19 s->strippos = off;
1410 33 s->strips = count;
1411
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 31 times.
33 if (s->strips == s->bppcount)
1412 2 s->rps = s->height;
1413 33 s->sot = type;
1414 33 break;
1415 33 case TIFF_STRIP_SIZE:
1416
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 19 times.
33 if (count == 1) {
1417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (value > INT_MAX) {
1418 av_log(s->avctx, AV_LOG_ERROR,
1419 "stripsize %u too large\n", value);
1420 return AVERROR_INVALIDDATA;
1421 }
1422 14 s->stripsizesoff = 0;
1423 14 s->stripsize = value;
1424 14 s->strips = 1;
1425 } else {
1426 19 s->stripsizesoff = off;
1427 }
1428 33 s->strips = count;
1429 33 s->sstype = type;
1430 33 break;
1431 42 case TIFF_XRES:
1432 case TIFF_YRES:
1433 42 set_sar(s, tag, value, value2);
1434 42 break;
1435 case TIFF_TILE_OFFSETS:
1436 s->tile_offsets_offset = off;
1437 s->is_tiled = 1;
1438 break;
1439 case TIFF_TILE_BYTE_COUNTS:
1440 s->tile_byte_counts_offset = off;
1441 break;
1442 case TIFF_TILE_LENGTH:
1443 if (value > INT_MAX)
1444 return AVERROR_INVALIDDATA;
1445 s->tile_length = value;
1446 break;
1447 case TIFF_TILE_WIDTH:
1448 if (value > INT_MAX)
1449 return AVERROR_INVALIDDATA;
1450 s->tile_width = value;
1451 break;
1452 8 case TIFF_PREDICTOR:
1453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (value > INT_MAX)
1454 return AVERROR_INVALIDDATA;
1455 8 s->predictor = value;
1456 8 break;
1457 case TIFF_SUB_IFDS:
1458 if (count == 1)
1459 s->sub_ifd = value;
1460 else if (count > 1)
1461 s->sub_ifd = ff_tget_long(&s->gb, s->le); /** Only get the first SubIFD */
1462 break;
1463 case TIFF_GRAY_RESPONSE_CURVE:
1464 case DNG_LINEARIZATION_TABLE:
1465 if (count < 1 || count > FF_ARRAY_ELEMS(s->dng_lut))
1466 return AVERROR_INVALIDDATA;
1467 for (int i = 0; i < count; i++)
1468 s->dng_lut[i] = ff_tget(&s->gb, type, s->le);
1469 s->white_level = s->dng_lut[count-1];
1470 break;
1471 case DNG_BLACK_LEVEL:
1472 if (count > FF_ARRAY_ELEMS(s->black_level))
1473 return AVERROR_INVALIDDATA;
1474 s->black_level[0] = value / (float)value2;
1475 for (int i = 0; i < count && count > 1; i++) {
1476 if (type == AV_TIFF_RATIONAL) {
1477 value = ff_tget_long(&s->gb, s->le);
1478 value2 = ff_tget_long(&s->gb, s->le);
1479 if (!value2) {
1480 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1481 value2 = 1;
1482 }
1483
1484 s->black_level[i] = value / (float)value2;
1485 } else if (type == AV_TIFF_SRATIONAL) {
1486 int value = ff_tget_long(&s->gb, s->le);
1487 int value2 = ff_tget_long(&s->gb, s->le);
1488 if (!value2) {
1489 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1490 value2 = 1;
1491 }
1492
1493 s->black_level[i] = value / (float)value2;
1494 } else {
1495 s->black_level[i] = ff_tget(&s->gb, type, s->le);
1496 }
1497 }
1498 for (int i = count; i < 4 && count > 0; i++)
1499 s->black_level[i] = s->black_level[count - 1];
1500 break;
1501 case DNG_WHITE_LEVEL:
1502 s->white_level = value;
1503 break;
1504 case TIFF_CFA_PATTERN_DIM:
1505 if (count != 2 || (ff_tget(&s->gb, type, s->le) != 2 &&
1506 ff_tget(&s->gb, type, s->le) != 2)) {
1507 av_log(s->avctx, AV_LOG_ERROR, "CFA Pattern dimensions are not 2x2\n");
1508 return AVERROR_INVALIDDATA;
1509 }
1510 break;
1511 case TIFF_CFA_PATTERN:
1512 s->is_bayer = 1;
1513 s->pattern[0] = ff_tget(&s->gb, type, s->le);
1514 s->pattern[1] = ff_tget(&s->gb, type, s->le);
1515 s->pattern[2] = ff_tget(&s->gb, type, s->le);
1516 s->pattern[3] = ff_tget(&s->gb, type, s->le);
1517 break;
1518 33 case TIFF_PHOTOMETRIC:
1519
1/3
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
33 switch (value) {
1520 33 case TIFF_PHOTOMETRIC_WHITE_IS_ZERO:
1521 case TIFF_PHOTOMETRIC_BLACK_IS_ZERO:
1522 case TIFF_PHOTOMETRIC_RGB:
1523 case TIFF_PHOTOMETRIC_PALETTE:
1524 case TIFF_PHOTOMETRIC_SEPARATED:
1525 case TIFF_PHOTOMETRIC_YCBCR:
1526 case TIFF_PHOTOMETRIC_CFA:
1527 case TIFF_PHOTOMETRIC_LINEAR_RAW: // Used by DNG images
1528 33 s->photometric = value;
1529 33 break;
1530 case TIFF_PHOTOMETRIC_ALPHA_MASK:
1531 case TIFF_PHOTOMETRIC_CIE_LAB:
1532 case TIFF_PHOTOMETRIC_ICC_LAB:
1533 case TIFF_PHOTOMETRIC_ITU_LAB:
1534 case TIFF_PHOTOMETRIC_LOG_L:
1535 case TIFF_PHOTOMETRIC_LOG_LUV:
1536 avpriv_report_missing_feature(s->avctx,
1537 "PhotometricInterpretation 0x%04X",
1538 value);
1539 return AVERROR_PATCHWELCOME;
1540 default:
1541 av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
1542 "unknown\n", value);
1543 return AVERROR_INVALIDDATA;
1544 }
1545 33 break;
1546 16 case TIFF_FILL_ORDER:
1547
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) {
1548 av_log(s->avctx, AV_LOG_ERROR,
1549 "Unknown FillOrder value %d, trying default one\n", value);
1550 value = 1;
1551 }
1552 16 s->fill_order = value - 1;
1553 16 break;
1554 case TIFF_PAL: {
1555 GetByteContext pal_gb[3];
1556 off = type_sizes[type];
1557 if (count / 3 > 256 ||
1558 bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
1559 return AVERROR_INVALIDDATA;
1560
1561 pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
1562 bytestream2_skip(&pal_gb[1], count / 3 * off);
1563 bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
1564
1565 off = (type_sizes[type] - 1) << 3;
1566 if (off > 31U) {
1567 av_log(s->avctx, AV_LOG_ERROR, "palette shift %d is out of range\n", off);
1568 return AVERROR_INVALIDDATA;
1569 }
1570
1571 for (i = 0; i < count / 3; i++) {
1572 uint32_t p = 0xFF000000;
1573 p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16;
1574 p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8;
1575 p |= ff_tget(&pal_gb[2], type, s->le) >> off;
1576 s->palette[i] = p;
1577 }
1578 s->palette_is_set = 1;
1579 break;
1580 }
1581 18 case TIFF_PLANAR:
1582 18 s->planar = value == 2;
1583 18 break;
1584 case TIFF_YCBCR_SUBSAMPLING:
1585 if (count != 2) {
1586 av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n");
1587 return AVERROR_INVALIDDATA;
1588 }
1589 for (i = 0; i < count; i++) {
1590 s->subsampling[i] = ff_tget(&s->gb, type, s->le);
1591 if (s->subsampling[i] <= 0) {
1592 av_log(s->avctx, AV_LOG_ERROR, "subsampling %d is invalid\n", s->subsampling[i]);
1593 s->subsampling[i] = 1;
1594 return AVERROR_INVALIDDATA;
1595 }
1596 }
1597 break;
1598 case TIFF_T4OPTIONS:
1599 if (s->compr == TIFF_G3) {
1600 if (value > INT_MAX)
1601 return AVERROR_INVALIDDATA;
1602 s->fax_opts = value;
1603 }
1604 break;
1605 case TIFF_T6OPTIONS:
1606 if (s->compr == TIFF_G4) {
1607 if (value > INT_MAX)
1608 return AVERROR_INVALIDDATA;
1609 s->fax_opts = value;
1610 }
1611 break;
1612 #define ADD_METADATA(count, name, sep)\
1613 if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1614 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1615 goto end;\
1616 }
1617 case TIFF_MODEL_PIXEL_SCALE:
1618 ADD_METADATA(count, "ModelPixelScaleTag", NULL);
1619 break;
1620 case TIFF_MODEL_TRANSFORMATION:
1621 ADD_METADATA(count, "ModelTransformationTag", NULL);
1622 break;
1623 case TIFF_MODEL_TIEPOINT:
1624 ADD_METADATA(count, "ModelTiepointTag", NULL);
1625 break;
1626 case TIFF_GEO_KEY_DIRECTORY:
1627 if (s->geotag_count) {
1628 avpriv_request_sample(s->avctx, "Multiple geo key directories");
1629 return AVERROR_INVALIDDATA;
1630 }
1631 ADD_METADATA(1, "GeoTIFF_Version", NULL);
1632 ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
1633 s->geotag_count = ff_tget_short(&s->gb, s->le);
1634 if (s->geotag_count > count / 4 - 1) {
1635 s->geotag_count = count / 4 - 1;
1636 av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
1637 }
1638 if ( bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4
1639 || s->geotag_count == 0) {
1640 s->geotag_count = 0;
1641 return -1;
1642 }
1643 s->geotags = av_calloc(s->geotag_count, sizeof(*s->geotags));
1644 if (!s->geotags) {
1645 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1646 s->geotag_count = 0;
1647 goto end;
1648 }
1649 for (i = 0; i < s->geotag_count; i++) {
1650 unsigned val;
1651 s->geotags[i].key = ff_tget_short(&s->gb, s->le);
1652 s->geotags[i].type = ff_tget_short(&s->gb, s->le);
1653 s->geotags[i].count = ff_tget_short(&s->gb, s->le);
1654 val = ff_tget_short(&s->gb, s->le);
1655
1656 if (!s->geotags[i].type) {
1657 const char *str = get_geokey_val(s->geotags[i].key, val);
1658
1659 s->geotags[i].val = str ? av_strdup(str) : av_asprintf("Unknown-%u", val);
1660 if (!s->geotags[i].val)
1661 return AVERROR(ENOMEM);
1662 } else
1663 s->geotags[i].offset = val;
1664 }
1665 break;
1666 case TIFF_GEO_DOUBLE_PARAMS:
1667 if (count >= INT_MAX / sizeof(int64_t))
1668 return AVERROR_INVALIDDATA;
1669 if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
1670 return AVERROR_INVALIDDATA;
1671 dp = av_malloc_array(count, sizeof(double));
1672 if (!dp) {
1673 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1674 goto end;
1675 }
1676 for (i = 0; i < count; i++)
1677 dp[i] = ff_tget_double(&s->gb, s->le);
1678 for (i = 0; i < s->geotag_count; i++) {
1679 if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
1680 if (s->geotags[i].count == 0
1681 || s->geotags[i].offset + s->geotags[i].count > count) {
1682 av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1683 } else if (s->geotags[i].val) {
1684 av_log(s->avctx, AV_LOG_WARNING, "Duplicate GeoTIFF key %d\n", s->geotags[i].key);
1685 } else {
1686 char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
1687 if (!ap) {
1688 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1689 av_freep(&dp);
1690 return AVERROR(ENOMEM);
1691 }
1692 s->geotags[i].val = ap;
1693 }
1694 }
1695 }
1696 av_freep(&dp);
1697 break;
1698 case TIFF_GEO_ASCII_PARAMS:
1699 pos = bytestream2_tell(&s->gb);
1700 for (i = 0; i < s->geotag_count; i++) {
1701 if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
1702 if (s->geotags[i].count == 0
1703 || s->geotags[i].offset + s->geotags[i].count > count) {
1704 av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1705 } else {
1706 char *ap;
1707
1708 bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
1709 if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
1710 return AVERROR_INVALIDDATA;
1711 if (s->geotags[i].val)
1712 return AVERROR_INVALIDDATA;
1713 ap = av_malloc(s->geotags[i].count);
1714 if (!ap) {
1715 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1716 return AVERROR(ENOMEM);
1717 }
1718 bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
1719 ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
1720 s->geotags[i].val = ap;
1721 }
1722 }
1723 }
1724 break;
1725 case TIFF_ICC_PROFILE:
1726 gb_temp = s->gb;
1727 bytestream2_seek(&gb_temp, off, SEEK_SET);
1728
1729 if (bytestream2_get_bytes_left(&gb_temp) < count)
1730 return AVERROR_INVALIDDATA;
1731
1732 ret = ff_frame_new_side_data(s->avctx, frame, AV_FRAME_DATA_ICC_PROFILE, count, &sd);
1733 if (ret < 0)
1734 return ret;
1735 if (sd)
1736 bytestream2_get_bufferu(&gb_temp, sd->data, count);
1737 break;
1738 case TIFF_ARTIST:
1739 ADD_METADATA(count, "artist", NULL);
1740 break;
1741 case TIFF_COPYRIGHT:
1742 ADD_METADATA(count, "copyright", NULL);
1743 break;
1744 2 case TIFF_DATE:
1745
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 ADD_METADATA(count, "date", NULL);
1746 2 break;
1747 6 case TIFF_DOCUMENT_NAME:
1748
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 ADD_METADATA(count, "document_name", NULL);
1749 6 break;
1750 case TIFF_HOST_COMPUTER:
1751 ADD_METADATA(count, "computer", NULL);
1752 break;
1753 4 case TIFF_IMAGE_DESCRIPTION:
1754
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 ADD_METADATA(count, "description", NULL);
1755 4 break;
1756 case TIFF_MAKE:
1757 ADD_METADATA(count, "make", NULL);
1758 break;
1759 case TIFF_MODEL:
1760 ADD_METADATA(count, "model", NULL);
1761 break;
1762 case TIFF_PAGE_NAME:
1763 ADD_METADATA(count, "page_name", NULL);
1764 break;
1765 12 case TIFF_PAGE_NUMBER:
1766
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 ADD_METADATA(count, "page_number", " / ");
1767 // need to seek back to re-read the page number
1768 12 bytestream2_seek(&s->gb, -count * sizeof(uint16_t), SEEK_CUR);
1769 // read the page number
1770 12 s->cur_page = ff_tget_short(&s->gb, s->le);
1771 // get back to where we were before the previous seek
1772 12 bytestream2_seek(&s->gb, count * sizeof(uint16_t) - sizeof(uint16_t), SEEK_CUR);
1773 12 break;
1774 2 case TIFF_SOFTWARE_NAME:
1775
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 ADD_METADATA(count, "software", NULL);
1776 2 break;
1777 case DNG_VERSION:
1778 if (count == 4) {
1779 unsigned int ver[4];
1780 ver[0] = ff_tget(&s->gb, type, s->le);
1781 ver[1] = ff_tget(&s->gb, type, s->le);
1782 ver[2] = ff_tget(&s->gb, type, s->le);
1783 ver[3] = ff_tget(&s->gb, type, s->le);
1784
1785 av_log(s->avctx, AV_LOG_DEBUG, "DNG file, version %u.%u.%u.%u\n",
1786 ver[0], ver[1], ver[2], ver[3]);
1787
1788 tiff_set_type(s, TIFF_TYPE_DNG);
1789 }
1790 break;
1791 case DNG_ANALOG_BALANCE:
1792 if (type != AV_TIFF_RATIONAL)
1793 break;
1794
1795 for (int i = 0; i < 3; i++) {
1796 value = ff_tget_long(&s->gb, s->le);
1797 value2 = ff_tget_long(&s->gb, s->le);
1798 if (!value2) {
1799 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1800 value2 = 1;
1801 }
1802
1803 s->analog_balance[i] = value / (float)value2;
1804 }
1805 break;
1806 case DNG_AS_SHOT_NEUTRAL:
1807 if (type != AV_TIFF_RATIONAL)
1808 break;
1809
1810 for (int i = 0; i < 3; i++) {
1811 value = ff_tget_long(&s->gb, s->le);
1812 value2 = ff_tget_long(&s->gb, s->le);
1813 if (!value2) {
1814 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1815 value2 = 1;
1816 }
1817
1818 s->as_shot_neutral[i] = value / (float)value2;
1819 }
1820 break;
1821 case DNG_AS_SHOT_WHITE_XY:
1822 if (type != AV_TIFF_RATIONAL)
1823 break;
1824
1825 for (int i = 0; i < 2; i++) {
1826 value = ff_tget_long(&s->gb, s->le);
1827 value2 = ff_tget_long(&s->gb, s->le);
1828 if (!value2) {
1829 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1830 value2 = 1;
1831 }
1832
1833 s->as_shot_white[i] = value / (float)value2;
1834 }
1835 s->as_shot_white[2] = 1.f - s->as_shot_white[0] - s->as_shot_white[1];
1836 for (int i = 0; i < 3; i++) {
1837 s->as_shot_white[i] /= d65_white[i];
1838 }
1839 break;
1840 case DNG_COLOR_MATRIX1:
1841 case DNG_COLOR_MATRIX2:
1842 for (int i = 0; i < 3; i++) {
1843 for (int j = 0; j < 3; j++) {
1844 int value = ff_tget_long(&s->gb, s->le);
1845 int value2 = ff_tget_long(&s->gb, s->le);
1846 if (!value2) {
1847 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1848 value2 = 1;
1849 }
1850 s->color_matrix[i][j] = value / (float)value2;
1851 }
1852 s->use_color_matrix = 1;
1853 }
1854 break;
1855 case DNG_CAMERA_CALIBRATION1:
1856 case DNG_CAMERA_CALIBRATION2:
1857 for (int i = 0; i < 3; i++) {
1858 for (int j = 0; j < 3; j++) {
1859 int value = ff_tget_long(&s->gb, s->le);
1860 int value2 = ff_tget_long(&s->gb, s->le);
1861 if (!value2) {
1862 av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n");
1863 value2 = 1;
1864 }
1865 s->camera_calibration[i][j] = value / (float)value2;
1866 }
1867 }
1868 break;
1869 case CINEMADNG_TIME_CODES:
1870 case CINEMADNG_FRAME_RATE:
1871 case CINEMADNG_T_STOP:
1872 case CINEMADNG_REEL_NAME:
1873 case CINEMADNG_CAMERA_LABEL:
1874 tiff_set_type(s, TIFF_TYPE_CINEMADNG);
1875 break;
1876 97 default:
1877
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1878 av_log(s->avctx, AV_LOG_ERROR,
1879 "Unknown or unsupported tag %d/0x%0X\n",
1880 tag, tag);
1881 return AVERROR_INVALIDDATA;
1882 }
1883 }
1884 97 end:
1885
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
519 if (s->bpp > 128U) {
1886 av_log(s->avctx, AV_LOG_ERROR,
1887 "This format is not supported (bpp=%d, %d components)\n",
1888 s->bpp, count);
1889 s->bpp = 0;
1890 return AVERROR_INVALIDDATA;
1891 }
1892 519 bytestream2_seek(&s->gb, start, SEEK_SET);
1893 519 return 0;
1894 }
1895
1896 static const float xyz2rgb[3][3] = {
1897 { 0.412453f, 0.357580f, 0.180423f },
1898 { 0.212671f, 0.715160f, 0.072169f },
1899 { 0.019334f, 0.119193f, 0.950227f },
1900 };
1901
1902 static void camera_xyz_coeff(TiffContext *s,
1903 float rgb2cam[3][4],
1904 double cam2xyz[4][3])
1905 {
1906 double cam2rgb[4][3], num;
1907 int i, j, k;
1908
1909 for (i = 0; i < 3; i++) {
1910 for (j = 0; j < 3; j++) {
1911 cam2rgb[i][j] = 0.;
1912 for (k = 0; k < 3; k++)
1913 cam2rgb[i][j] += cam2xyz[i][k] * xyz2rgb[k][j];
1914 }
1915 }
1916
1917 for (i = 0; i < 3; i++) {
1918 for (num = j = 0; j < 3; j++)
1919 num += cam2rgb[i][j];
1920 if (!num)
1921 num = 1;
1922 for (j = 0; j < 3; j++)
1923 cam2rgb[i][j] /= num;
1924 s->premultiply[i] = 1.f / num;
1925 }
1926 }
1927
1928 33 static int decode_frame(AVCodecContext *avctx, AVFrame *p,
1929 int *got_frame, AVPacket *avpkt)
1930 {
1931 33 TiffContext *const s = avctx->priv_data;
1932 33 unsigned off, last_off = 0;
1933 int le, ret, plane, planes;
1934 int i, j, entries, stride;
1935 unsigned soff, ssize;
1936 uint8_t *dst;
1937 GetByteContext stripsizes;
1938 GetByteContext stripdata;
1939 int retry_for_subifd, retry_for_page;
1940 int is_dng;
1941 int has_tile_bits, has_strip_bits;
1942
1943 33 av_exif_free(&s->exif_meta);
1944 /* this will not parse the image data */
1945 33 ret = av_exif_parse_buffer(avctx, avpkt->data, avpkt->size, &s->exif_meta, AV_EXIF_TIFF_HEADER);
1946
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0)
1947 av_log(avctx, AV_LOG_ERROR, "could not parse EXIF data: %s\n", av_err2str(ret));
1948
1949 33 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1950
1951 // parse image header
1952
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
1953 av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
1954 return ret;
1955
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1956 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1957 return AVERROR_INVALIDDATA;
1958 }
1959 33 s->le = le;
1960 // TIFF_BPP is not a required tag and defaults to 1
1961
1962 33 s->tiff_type = TIFF_TYPE_TIFF;
1963 33 s->use_color_matrix = 0;
1964 33 again:
1965 33 s->is_thumbnail = 0;
1966 33 s->bppcount = s->bpp = 1;
1967 33 s->photometric = TIFF_PHOTOMETRIC_NONE;
1968 33 s->compr = TIFF_RAW;
1969 33 s->fill_order = 0;
1970 33 s->white_level = 0;
1971 33 s->is_bayer = 0;
1972 33 s->is_tiled = 0;
1973 33 s->is_jpeg = 0;
1974 33 s->cur_page = 0;
1975 33 s->last_tag = 0;
1976
1977
2/2
✓ Branch 0 taken 2162688 times.
✓ Branch 1 taken 33 times.
2162721 for (i = 0; i < 65536; i++)
1978 2162688 s->dng_lut[i] = i;
1979
1980
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
165 for (i = 0; i < FF_ARRAY_ELEMS(s->black_level); i++)
1981 132 s->black_level[i] = 0.f;
1982
1983
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
165 for (i = 0; i < FF_ARRAY_ELEMS(s->as_shot_neutral); i++)
1984 132 s->as_shot_neutral[i] = 0.f;
1985
1986
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
165 for (i = 0; i < FF_ARRAY_ELEMS(s->as_shot_white); i++)
1987 132 s->as_shot_white[i] = 1.f;
1988
1989
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
165 for (i = 0; i < FF_ARRAY_ELEMS(s->analog_balance); i++)
1990 132 s->analog_balance[i] = 1.f;
1991
1992
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
165 for (i = 0; i < FF_ARRAY_ELEMS(s->premultiply); i++)
1993 132 s->premultiply[i] = 1.f;
1994
1995
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
165 for (i = 0; i < 4; i++)
1996
2/2
✓ Branch 0 taken 528 times.
✓ Branch 1 taken 132 times.
660 for (j = 0; j < 4; j++)
1997
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 396 times.
528 s->camera_calibration[i][j] = i == j;
1998
1999 33 free_geotags(s);
2000
2001 // Reset these offsets so we can tell if they were set this frame
2002 33 s->stripsizesoff = s->strippos = 0;
2003 /* parse image file directory */
2004 33 bytestream2_seek(&s->gb, off, SEEK_SET);
2005 33 entries = ff_tget_short(&s->gb, le);
2006
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
2007 return AVERROR_INVALIDDATA;
2008
2/2
✓ Branch 0 taken 519 times.
✓ Branch 1 taken 33 times.
552 for (i = 0; i < entries; i++) {
2009
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 519 times.
519 if ((ret = tiff_decode_tag(s, p)) < 0)
2010 return ret;
2011 }
2012
2013
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
33 if (s->get_thumbnail && !s->is_thumbnail) {
2014 av_log(avctx, AV_LOG_INFO, "No embedded thumbnail present\n");
2015 return AVERROR_EOF;
2016 }
2017
2018 /** whether we should process this IFD's SubIFD */
2019
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
33 retry_for_subifd = s->sub_ifd && (s->get_subimage || (!s->get_thumbnail && s->is_thumbnail));
2020 /** whether we should process this multi-page IFD's next page */
2021
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
33 retry_for_page = s->get_page && s->cur_page + 1 < s->get_page; // get_page is 1-indexed
2022
2023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (retry_for_page) {
2024 // set offset to the next IFD
2025 off = ff_tget_long(&s->gb, le);
2026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 } else if (retry_for_subifd) {
2027 // set offset to the SubIFD
2028 off = s->sub_ifd;
2029 }
2030
2031
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 if (retry_for_subifd || retry_for_page) {
2032 if (!off) {
2033 av_log(avctx, AV_LOG_ERROR, "Requested entry not found\n");
2034 return AVERROR_INVALIDDATA;
2035 }
2036 if (off <= last_off) {
2037 avpriv_request_sample(s->avctx, "non increasing IFD offset");
2038 return AVERROR_INVALIDDATA;
2039 }
2040 last_off = off;
2041 if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
2042 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
2043 return AVERROR_INVALIDDATA;
2044 }
2045 s->sub_ifd = 0;
2046 goto again;
2047 }
2048
2049 /* At this point we've decided on which (Sub)IFD to process */
2050
2051
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
2052
2053
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 for (i = 0; i<s->geotag_count; i++) {
2054 const char *keyname = get_geokey_name(s->geotags[i].key);
2055 if (!keyname) {
2056 av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
2057 continue;
2058 }
2059 if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
2060 av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
2061 continue;
2062 }
2063 ret = av_dict_set(&p->metadata, keyname, s->geotags[i].val, AV_DICT_DONT_STRDUP_VAL);
2064 s->geotags[i].val = NULL;
2065 if (ret<0) {
2066 av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
2067 return ret;
2068 }
2069 }
2070
2071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (is_dng) {
2072 double cam2xyz[4][3];
2073 float cmatrix[3][4];
2074 float pmin = FLT_MAX;
2075 int bps;
2076
2077 for (i = 0; i < 3; i++) {
2078 for (j = 0; j < 3; j++)
2079 s->camera_calibration[i][j] *= s->analog_balance[i];
2080 }
2081
2082 if (!s->use_color_matrix) {
2083 for (i = 0; i < 3; i++) {
2084 if (s->camera_calibration[i][i])
2085 s->premultiply[i] /= s->camera_calibration[i][i];
2086 }
2087 } else {
2088 for (int c = 0; c < 3; c++) {
2089 for (i = 0; i < 3; i++) {
2090 cam2xyz[c][i] = 0.;
2091 for (j = 0; j < 3; j++)
2092 cam2xyz[c][i] += s->camera_calibration[c][j] * s->color_matrix[j][i] * s->as_shot_white[i];
2093 }
2094 }
2095
2096 camera_xyz_coeff(s, cmatrix, cam2xyz);
2097 }
2098
2099 for (int c = 0; c < 3; c++)
2100 pmin = fminf(pmin, s->premultiply[c]);
2101
2102 for (int c = 0; c < 3; c++)
2103 s->premultiply[c] /= pmin;
2104
2105 if (s->bpp % s->bppcount)
2106 return AVERROR_INVALIDDATA;
2107 bps = s->bpp / s->bppcount;
2108 if (bps < 8 || bps > 32)
2109 return AVERROR_INVALIDDATA;
2110
2111 if (s->white_level == 0)
2112 s->white_level = (1LL << bps) - 1; /* Default value as per the spec */
2113
2114 if (s->white_level <= s->black_level[0]) {
2115 av_log(avctx, AV_LOG_ERROR, "BlackLevel (%g) must be less than WhiteLevel (%"PRId32")\n",
2116 s->black_level[0], s->white_level);
2117 return AVERROR_INVALIDDATA;
2118 }
2119
2120 if (s->planar)
2121 return AVERROR_PATCHWELCOME;
2122 }
2123
2124
4/6
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 14 times.
33 if (!s->is_tiled && !s->strippos && !s->stripoff) {
2125 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
2126 return AVERROR_INVALIDDATA;
2127 }
2128
2129
5/10
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 33 times.
33 has_tile_bits = s->is_tiled || s->tile_byte_counts_offset || s->tile_offsets_offset || s->tile_width || s->tile_length;
2130
3/16
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 19 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.
33 has_strip_bits = s->strippos || s->strips || s->stripoff || s->rps || s->sot || s->sstype || s->stripsize || s->stripsizesoff;
2131
2132
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
33 if (has_tile_bits && has_strip_bits) {
2133 int tiled_dng = s->is_tiled && is_dng;
2134 av_log(avctx, tiled_dng ? AV_LOG_WARNING : AV_LOG_ERROR, "Tiled TIFF is not allowed to strip\n");
2135 if (!tiled_dng)
2136 return AVERROR_INVALIDDATA;
2137 }
2138
2139 /* now we have the data and may start decoding */
2140
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 22 times.
33 if ((ret = init_image(s, p)) <= 0)
2141 11 return ret;
2142
2143
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
22 if (!s->is_tiled || has_strip_bits) {
2144
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
22 if (s->strips == 1 && !s->stripsize) {
2145 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
2146 s->stripsize = avpkt->size - s->stripoff;
2147 }
2148
2149
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 7 times.
22 if (s->stripsizesoff) {
2150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (s->stripsizesoff >= (unsigned)avpkt->size)
2151 return AVERROR_INVALIDDATA;
2152 15 bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
2153 15 avpkt->size - s->stripsizesoff);
2154 }
2155
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 7 times.
22 if (s->strippos) {
2156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (s->strippos >= (unsigned)avpkt->size)
2157 return AVERROR_INVALIDDATA;
2158 15 bytestream2_init(&stripdata, avpkt->data + s->strippos,
2159 15 avpkt->size - s->strippos);
2160 }
2161
2162
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
22 if (s->rps <= 0 || s->rps % s->subsampling[1]) {
2163 av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
2164 return AVERROR_INVALIDDATA;
2165 }
2166 }
2167
2168
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (s->photometric == TIFF_PHOTOMETRIC_LINEAR_RAW ||
2169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 s->photometric == TIFF_PHOTOMETRIC_CFA) {
2170 p->color_trc = AVCOL_TRC_LINEAR;
2171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 } else if (s->photometric == TIFF_PHOTOMETRIC_BLACK_IS_ZERO) {
2172 p->color_trc = AVCOL_TRC_GAMMA22;
2173 }
2174
2175 /* Handle DNG images with JPEG-compressed tiles */
2176
2177
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
22 if (is_dng && s->is_tiled) {
2178 if (!s->is_jpeg) {
2179 avpriv_report_missing_feature(avctx, "DNG uncompressed tiled images");
2180 return AVERROR_PATCHWELCOME;
2181 } else if (!s->is_bayer) {
2182 avpriv_report_missing_feature(avctx, "DNG JPG-compressed tiled non-bayer-encoded images");
2183 return AVERROR_PATCHWELCOME;
2184 } else {
2185 if ((ret = dng_decode_tiles(avctx, p, avpkt)) > 0)
2186 *got_frame = 1;
2187 return ret;
2188 }
2189 }
2190
2191 /* Handle TIFF images and DNG images with uncompressed strips (non-tiled) */
2192
2193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 planes = s->planar ? s->bppcount : 1;
2194
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 22 times.
44 for (plane = 0; plane < planes; plane++) {
2195 22 uint8_t *five_planes = NULL;
2196 22 int remaining = avpkt->size;
2197 int decoded_height;
2198 22 stride = p->linesize[plane];
2199 22 dst = p->data[plane];
2200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED &&
2201 s->avctx->pix_fmt == AV_PIX_FMT_RGBA) {
2202 stride = stride * 5 / 4;
2203 five_planes =
2204 dst = av_malloc(stride * s->height);
2205 if (!dst)
2206 return AVERROR(ENOMEM);
2207 }
2208
2/2
✓ Branch 0 taken 579 times.
✓ Branch 1 taken 22 times.
601 for (i = 0; i < s->height; i += s->rps) {
2209
2/2
✓ Branch 0 taken 557 times.
✓ Branch 1 taken 22 times.
579 if (i)
2210 557 dst += s->rps * stride;
2211
2/2
✓ Branch 0 taken 572 times.
✓ Branch 1 taken 7 times.
579 if (s->stripsizesoff)
2212 572 ssize = ff_tget(&stripsizes, s->sstype, le);
2213 else
2214 7 ssize = s->stripsize;
2215
2216
2/2
✓ Branch 0 taken 572 times.
✓ Branch 1 taken 7 times.
579 if (s->strippos)
2217 572 soff = ff_tget(&stripdata, s->sot, le);
2218 else
2219 7 soff = s->stripoff;
2220
2221
3/6
✓ Branch 0 taken 579 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 579 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 579 times.
579 if (soff > avpkt->size || ssize > avpkt->size - soff || ssize > remaining) {
2222 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
2223 av_freep(&five_planes);
2224 return AVERROR_INVALIDDATA;
2225 }
2226 579 remaining -= ssize;
2227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
579 if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,
2228 579 FFMIN(s->rps, s->height - i))) < 0) {
2229 if (avctx->err_recognition & AV_EF_EXPLODE) {
2230 av_freep(&five_planes);
2231 return ret;
2232 }
2233 break;
2234 }
2235 }
2236 22 decoded_height = FFMIN(i, s->height);
2237
2238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (s->predictor == 2) {
2239 if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
2240 av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");
2241 return AVERROR_PATCHWELCOME;
2242 }
2243 dst = five_planes ? five_planes : p->data[plane];
2244 soff = s->bpp >> 3;
2245 if (s->planar)
2246 soff = FFMAX(soff / s->bppcount, 1);
2247 ssize = s->width * soff;
2248 if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
2249 s->avctx->pix_fmt == AV_PIX_FMT_RGBA64LE ||
2250 s->avctx->pix_fmt == AV_PIX_FMT_GRAY16LE ||
2251 s->avctx->pix_fmt == AV_PIX_FMT_YA16LE ||
2252 s->avctx->pix_fmt == AV_PIX_FMT_GBRP16LE ||
2253 s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16LE) {
2254 for (i = 0; i < decoded_height; i++) {
2255 for (j = soff; j < ssize; j += 2)
2256 AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
2257 dst += stride;
2258 }
2259 } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
2260 s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE ||
2261 s->avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
2262 s->avctx->pix_fmt == AV_PIX_FMT_YA16BE ||
2263 s->avctx->pix_fmt == AV_PIX_FMT_GBRP16BE ||
2264 s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16BE) {
2265 for (i = 0; i < decoded_height; i++) {
2266 for (j = soff; j < ssize; j += 2)
2267 AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
2268 dst += stride;
2269 }
2270 } else {
2271 for (i = 0; i < decoded_height; i++) {
2272 for (j = soff; j < ssize; j++)
2273 dst[j] += dst[j - soff];
2274 dst += stride;
2275 }
2276 }
2277 }
2278
2279 /* Floating point predictor
2280 TIFF Technical Note 3 http://chriscox.org/TIFFTN3d1.pdf */
2281
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 18 times.
22 if (s->predictor == 3) {
2282 4 int channels = s->bppcount;
2283 int group_size;
2284 uint8_t *tmpbuf;
2285 int bpc;
2286
2287
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 dst = five_planes ? five_planes : p->data[plane];
2288 4 soff = s->bpp >> 3;
2289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->planar) {
2290 soff = FFMAX(soff / s->bppcount, 1);
2291 channels = 1;
2292 }
2293 4 ssize = s->width * soff;
2294 4 bpc = FFMAX(soff / s->bppcount, 1); /* Bytes per component */
2295 4 group_size = s->width * channels;
2296
2297 4 tmpbuf = av_malloc(ssize);
2298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!tmpbuf) {
2299 av_free(five_planes);
2300 return AVERROR(ENOMEM);
2301 }
2302
2303
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (s->avctx->pix_fmt == AV_PIX_FMT_RGBF32LE ||
2304
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 s->avctx->pix_fmt == AV_PIX_FMT_RGBAF32LE) {
2305
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 4 times.
36 for (i = 0; i < decoded_height; i++) {
2306 /* Copy first sample byte for each channel */
2307
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 32 times.
144 for (j = 0; j < channels; j++)
2308 112 tmpbuf[j] = dst[j];
2309
2310 /* Decode horizontal differences */
2311
2/2
✓ Branch 0 taken 3472 times.
✓ Branch 1 taken 32 times.
3504 for (j = channels; j < ssize; j++)
2312 3472 tmpbuf[j] = dst[j] + tmpbuf[j-channels];
2313
2314 /* Combine shuffled bytes from their separate groups. Each
2315 byte of every floating point value in a row of pixels is
2316 split and combined into separate groups. A group of all
2317 the sign/exponents bytes in the row and groups for each
2318 of the upper, mid, and lower mantissa bytes in the row. */
2319
2/2
✓ Branch 0 taken 896 times.
✓ Branch 1 taken 32 times.
928 for (j = 0; j < group_size; j++) {
2320
2/2
✓ Branch 0 taken 3584 times.
✓ Branch 1 taken 896 times.
4480 for (int k = 0; k < bpc; k++) {
2321 3584 dst[bpc * j + k] = tmpbuf[(bpc - k - 1) * group_size + j];
2322 }
2323 }
2324 32 dst += stride;
2325 }
2326 } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGBF32BE ||
2327 s->avctx->pix_fmt == AV_PIX_FMT_RGBAF32BE) {
2328 /* Same as LE only the shuffle at the end is reversed */
2329 for (i = 0; i < decoded_height; i++) {
2330 for (j = 0; j < channels; j++)
2331 tmpbuf[j] = dst[j];
2332
2333 for (j = channels; j < ssize; j++)
2334 tmpbuf[j] = dst[j] + tmpbuf[j-channels];
2335
2336 for (j = 0; j < group_size; j++) {
2337 for (int k = 0; k < bpc; k++) {
2338 dst[bpc * j + k] = tmpbuf[k * group_size + j];
2339 }
2340 }
2341 dst += stride;
2342 }
2343 } else {
2344 av_log(s->avctx, AV_LOG_ERROR, "unsupported floating point pixel format\n");
2345 }
2346 4 av_free(tmpbuf);
2347 }
2348
2349
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
22 if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {
2350
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);
2351 2 dst = p->data[plane];
2352
2/2
✓ Branch 0 taken 6496 times.
✓ Branch 1 taken 2 times.
6498 for (i = 0; i < s->height; i++) {
2353
2/2
✓ Branch 0 taken 2078720 times.
✓ Branch 1 taken 6496 times.
2085216 for (j = 0; j < stride; j++)
2354 2078720 dst[j] = c - dst[j];
2355 6496 dst += stride;
2356 }
2357 }
2358
2359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED &&
2360 (s->avctx->pix_fmt == AV_PIX_FMT_RGB0 || s->avctx->pix_fmt == AV_PIX_FMT_RGBA)) {
2361 int x = s->avctx->pix_fmt == AV_PIX_FMT_RGB0 ? 4 : 5;
2362 uint8_t *src = five_planes ? five_planes : p->data[plane];
2363 dst = p->data[plane];
2364 for (i = 0; i < s->height; i++) {
2365 for (j = 0; j < s->width; j++) {
2366 int k = 255 - src[x * j + 3];
2367 int r = (255 - src[x * j ]) * k;
2368 int g = (255 - src[x * j + 1]) * k;
2369 int b = (255 - src[x * j + 2]) * k;
2370 dst[4 * j ] = r * 257 >> 16;
2371 dst[4 * j + 1] = g * 257 >> 16;
2372 dst[4 * j + 2] = b * 257 >> 16;
2373 dst[4 * j + 3] = s->avctx->pix_fmt == AV_PIX_FMT_RGBA ? src[x * j + 4] : 255;
2374 }
2375 src += stride;
2376 dst += p->linesize[plane];
2377 }
2378 av_freep(&five_planes);
2379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 } else if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED &&
2380 s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE) {
2381 dst = p->data[plane];
2382 for (i = 0; i < s->height; i++) {
2383 for (j = 0; j < s->width; j++) {
2384 uint64_t k = 65535 - AV_RB16(dst + 8 * j + 6);
2385 uint64_t r = (65535 - AV_RB16(dst + 8 * j )) * k;
2386 uint64_t g = (65535 - AV_RB16(dst + 8 * j + 2)) * k;
2387 uint64_t b = (65535 - AV_RB16(dst + 8 * j + 4)) * k;
2388 AV_WB16(dst + 8 * j , r * 65537 >> 32);
2389 AV_WB16(dst + 8 * j + 2, g * 65537 >> 32);
2390 AV_WB16(dst + 8 * j + 4, b * 65537 >> 32);
2391 AV_WB16(dst + 8 * j + 6, 65535);
2392 }
2393 dst += p->linesize[plane];
2394 }
2395 }
2396 }
2397
2398
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
22 if (s->planar && s->bppcount > 2) {
2399 FFSWAP(uint8_t*, p->data[0], p->data[2]);
2400 FFSWAP(int, p->linesize[0], p->linesize[2]);
2401 FFSWAP(uint8_t*, p->data[0], p->data[1]);
2402 FFSWAP(int, p->linesize[0], p->linesize[1]);
2403 }
2404
2405
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 22 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.
22 if (s->is_bayer && s->white_level && s->bpp == 16 && !is_dng) {
2406 uint16_t *dst = (uint16_t *)p->data[0];
2407 for (i = 0; i < s->height; i++) {
2408 for (j = 0; j < s->width; j++)
2409 dst[j] = FFMIN((dst[j] / (float)s->white_level) * 65535, 65535);
2410 dst += stride / 2;
2411 }
2412 }
2413
2414 22 ret = ff_decode_exif_attach_ifd(avctx, p, &s->exif_meta);
2415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
2416 av_log(avctx, AV_LOG_ERROR, "error attaching EXIF ifd: %s\n", av_err2str(ret));
2417
2418 22 *got_frame = 1;
2419
2420 22 return avpkt->size;
2421 }
2422
2423 21 static av_cold int tiff_init(AVCodecContext *avctx)
2424 {
2425 21 TiffContext *s = avctx->priv_data;
2426 int ret;
2427
2428 21 s->width = 0;
2429 21 s->height = 0;
2430 21 s->subsampling[0] =
2431 21 s->subsampling[1] = 1;
2432 21 s->avctx = avctx;
2433 21 ff_lzw_decode_open(&s->lzw);
2434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!s->lzw)
2435 return AVERROR(ENOMEM);
2436 21 ff_ccitt_unpack_init();
2437
2438 /* Allocate JPEG frame */
2439 21 s->jpgframe = av_frame_alloc();
2440 21 s->jpkt = av_packet_alloc();
2441
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
21 if (!s->jpgframe || !s->jpkt)
2442 return AVERROR(ENOMEM);
2443
2444 /* Prepare everything needed for JPEG decoding */
2445 EXTERN const FFCodec ff_mjpeg_decoder;
2446 21 s->avctx_mjpeg = avcodec_alloc_context3(&ff_mjpeg_decoder.p);
2447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!s->avctx_mjpeg)
2448 return AVERROR(ENOMEM);
2449 21 s->avctx_mjpeg->flags = avctx->flags;
2450 21 s->avctx_mjpeg->flags2 = avctx->flags2;
2451 21 s->avctx_mjpeg->idct_algo = avctx->idct_algo;
2452 21 s->avctx_mjpeg->max_pixels = avctx->max_pixels;
2453 21 ret = avcodec_open2(s->avctx_mjpeg, NULL, NULL);
2454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0) {
2455 return ret;
2456 }
2457
2458 21 return 0;
2459 }
2460
2461 21 static av_cold int tiff_end(AVCodecContext *avctx)
2462 {
2463 21 TiffContext *const s = avctx->priv_data;
2464
2465 21 free_geotags(s);
2466 21 av_exif_free(&s->exif_meta);
2467
2468 21 ff_lzw_decode_close(&s->lzw);
2469 21 av_freep(&s->deinvert_buf);
2470 21 s->deinvert_buf_size = 0;
2471 21 av_freep(&s->yuv_line);
2472 21 s->yuv_line_size = 0;
2473 21 av_frame_free(&s->jpgframe);
2474 21 av_packet_free(&s->jpkt);
2475 21 avcodec_free_context(&s->avctx_mjpeg);
2476 21 return 0;
2477 }
2478
2479 #define OFFSET(x) offsetof(TiffContext, x)
2480 static const AVOption tiff_options[] = {
2481 { "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 },
2482 { "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 },
2483 { "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 },
2484 { NULL },
2485 };
2486
2487 static const AVClass tiff_decoder_class = {
2488 .class_name = "TIFF decoder",
2489 .item_name = av_default_item_name,
2490 .option = tiff_options,
2491 .version = LIBAVUTIL_VERSION_INT,
2492 };
2493
2494 const FFCodec ff_tiff_decoder = {
2495 .p.name = "tiff",
2496 CODEC_LONG_NAME("TIFF image"),
2497 .p.type = AVMEDIA_TYPE_VIDEO,
2498 .p.id = AV_CODEC_ID_TIFF,
2499 .priv_data_size = sizeof(TiffContext),
2500 .init = tiff_init,
2501 .close = tiff_end,
2502 FF_CODEC_DECODE_CB(decode_frame),
2503 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
2504 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_ICC_PROFILES |
2505 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2506 .p.priv_class = &tiff_decoder_class,
2507 };
2508