FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tiff.c
Date: 2022-07-04 19:11:22
Exec Total Coverage
Lines: 368 1349 27.3%
Branches: 232 1118 20.8%

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