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