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