FFmpeg  4.2.2
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/intreadwrite.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/opt.h"
41 #include "avcodec.h"
42 #include "bytestream.h"
43 #include "faxcompr.h"
44 #include "internal.h"
45 #include "lzw.h"
46 #include "mathops.h"
47 #include "tiff.h"
48 #include "tiff_data.h"
49 #include "thread.h"
50 #include "get_bits.h"
51 
52 typedef struct TiffContext {
53  AVClass *class;
56 
58  uint16_t get_page;
60 
62  int width, height;
63  unsigned int bpp, bppcount;
64  uint32_t palette[256];
66  int le;
69  int planar;
70  int subsampling[2];
71  int fax_opts;
72  int predictor;
74  uint32_t res[4];
76 
77  int is_bayer;
79  unsigned white_level;
80 
81  uint32_t sub_ifd;
82  uint16_t cur_page;
83 
84  int strips, rps, sstype;
85  int sot;
88 
92  unsigned int yuv_line_size;
94  unsigned int fax_buffer_size;
95 
98 } TiffContext;
99 
101  if (s->tiff_type < tiff_type) // Prioritize higher-valued entries
102  s->tiff_type = tiff_type;
103 }
104 
105 static void free_geotags(TiffContext *const s)
106 {
107  int i;
108  for (i = 0; i < s->geotag_count; i++) {
109  if (s->geotags[i].val)
110  av_freep(&s->geotags[i].val);
111  }
112  av_freep(&s->geotags);
113  s->geotag_count = 0;
114 }
115 
116 #define RET_GEOKEY(TYPE, array, element)\
117  if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
118  key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_name_type_map))\
119  return ff_tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
120 
121 static const char *get_geokey_name(int key)
122 {
123  RET_GEOKEY(VERT, vert, name);
124  RET_GEOKEY(PROJ, proj, name);
125  RET_GEOKEY(GEOG, geog, name);
126  RET_GEOKEY(CONF, conf, name);
127 
128  return NULL;
129 }
130 
131 static int get_geokey_type(int key)
132 {
133  RET_GEOKEY(VERT, vert, type);
134  RET_GEOKEY(PROJ, proj, type);
135  RET_GEOKEY(GEOG, geog, type);
136  RET_GEOKEY(CONF, conf, type);
137 
138  return AVERROR_INVALIDDATA;
139 }
140 
141 static int cmp_id_key(const void *id, const void *k)
142 {
143  return *(const int*)id - ((const TiffGeoTagKeyName*)k)->key;
144 }
145 
146 static const char *search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
147 {
148  TiffGeoTagKeyName *r = bsearch(&id, keys, n, sizeof(keys[0]), cmp_id_key);
149  if(r)
150  return r->name;
151 
152  return NULL;
153 }
154 
155 static char *get_geokey_val(int key, int val)
156 {
157  char *ap;
158 
159  if (val == TIFF_GEO_KEY_UNDEFINED)
160  return av_strdup("undefined");
161  if (val == TIFF_GEO_KEY_USER_DEFINED)
162  return av_strdup("User-Defined");
163 
164 #define RET_GEOKEY_VAL(TYPE, array)\
165  if (val >= TIFF_##TYPE##_OFFSET &&\
166  val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_codes))\
167  return av_strdup(ff_tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]);
168 
169  switch (key) {
171  RET_GEOKEY_VAL(GT_MODEL_TYPE, gt_model_type);
172  break;
174  RET_GEOKEY_VAL(GT_RASTER_TYPE, gt_raster_type);
175  break;
179  RET_GEOKEY_VAL(LINEAR_UNIT, linear_unit);
180  break;
183  RET_GEOKEY_VAL(ANGULAR_UNIT, angular_unit);
184  break;
186  RET_GEOKEY_VAL(GCS_TYPE, gcs_type);
187  RET_GEOKEY_VAL(GCSE_TYPE, gcse_type);
188  break;
190  RET_GEOKEY_VAL(GEODETIC_DATUM, geodetic_datum);
191  RET_GEOKEY_VAL(GEODETIC_DATUM_E, geodetic_datum_e);
192  break;
194  RET_GEOKEY_VAL(ELLIPSOID, ellipsoid);
195  break;
197  RET_GEOKEY_VAL(PRIME_MERIDIAN, prime_meridian);
198  break;
201  if(ap) return ap;
202  break;
205  if(ap) return ap;
206  break;
208  RET_GEOKEY_VAL(COORD_TRANS, coord_trans);
209  break;
211  RET_GEOKEY_VAL(VERT_CS, vert_cs);
212  RET_GEOKEY_VAL(ORTHO_VERT_CS, ortho_vert_cs);
213  break;
214 
215  }
216 
217  ap = av_malloc(14);
218  if (ap)
219  snprintf(ap, 14, "Unknown-%d", val);
220  return ap;
221 }
222 
223 static char *doubles2str(double *dp, int count, const char *sep)
224 {
225  int i;
226  char *ap, *ap0;
227  uint64_t component_len;
228  if (!sep) sep = ", ";
229  component_len = 24LL + strlen(sep);
230  if (count >= (INT_MAX - 1)/component_len)
231  return NULL;
232  ap = av_malloc(component_len * count + 1);
233  if (!ap)
234  return NULL;
235  ap0 = ap;
236  ap[0] = '\0';
237  for (i = 0; i < count; i++) {
238  unsigned l = snprintf(ap, component_len, "%.15g%s", dp[i], sep);
239  if(l >= component_len) {
240  av_free(ap0);
241  return NULL;
242  }
243  ap += l;
244  }
245  ap0[strlen(ap0) - strlen(sep)] = '\0';
246  return ap0;
247 }
248 
249 static int add_metadata(int count, int type,
250  const char *name, const char *sep, TiffContext *s, AVFrame *frame)
251 {
252  switch(type) {
253  case TIFF_DOUBLE: return ff_tadd_doubles_metadata(count, name, sep, &s->gb, s->le, &frame->metadata);
254  case TIFF_SHORT : return ff_tadd_shorts_metadata(count, name, sep, &s->gb, s->le, 0, &frame->metadata);
255  case TIFF_STRING: return ff_tadd_string_metadata(count, name, &s->gb, s->le, &frame->metadata);
256  default : return AVERROR_INVALIDDATA;
257  };
258 }
259 
261  unsigned int bpp, uint8_t* dst,
262  int usePtr, const uint8_t *src,
263  uint8_t c, int width, int offset)
264 {
265  switch (bpp) {
266  case 1:
267  while (--width >= 0) {
268  dst[(width+offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
269  dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
270  dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
271  dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
272  dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
273  dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
274  dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
275  dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
276  }
277  break;
278  case 2:
279  while (--width >= 0) {
280  dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
281  dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
282  dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
283  dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
284  }
285  break;
286  case 4:
287  while (--width >= 0) {
288  dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
289  dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
290  }
291  break;
292  case 12: {
293  uint16_t *dst16 = (uint16_t *)dst;
295  init_get_bits8(&gb, src, width);
296  for (int i = 0; i < s->width; i++) {
297  dst16[i] = get_bits(&gb, 12) << 4;
298  }
299  }
300  break;
301  default:
302  if (usePtr) {
303  memcpy(dst + offset, src, width);
304  } else {
305  memset(dst + offset, c, width);
306  }
307  }
308 }
309 
310 static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
311 {
312  int i;
313 
315  if (!s->deinvert_buf)
316  return AVERROR(ENOMEM);
317  for (i = 0; i < size; i++)
318  s->deinvert_buf[i] = ff_reverse[src[i]];
319 
320  return 0;
321 }
322 
323 static void unpack_gray(TiffContext *s, AVFrame *p,
324  const uint8_t *src, int lnum, int width, int bpp)
325 {
327  uint16_t *dst = (uint16_t *)(p->data[0] + lnum * p->linesize[0]);
328 
329  init_get_bits8(&gb, src, width);
330 
331  for (int i = 0; i < s->width; i++) {
332  dst[i] = get_bits(&gb, bpp);
333  }
334 }
335 
336 static void unpack_yuv(TiffContext *s, AVFrame *p,
337  const uint8_t *src, int lnum)
338 {
339  int i, j, k;
340  int w = (s->width - 1) / s->subsampling[0] + 1;
341  uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
342  uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
343  if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) {
344  for (i = 0; i < w; i++) {
345  for (j = 0; j < s->subsampling[1]; j++)
346  for (k = 0; k < s->subsampling[0]; k++)
347  p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
348  FFMIN(i * s->subsampling[0] + k, s->width-1)] = *src++;
349  *pu++ = *src++;
350  *pv++ = *src++;
351  }
352  }else{
353  for (i = 0; i < w; i++) {
354  for (j = 0; j < s->subsampling[1]; j++)
355  for (k = 0; k < s->subsampling[0]; k++)
356  p->data[0][(lnum + j) * p->linesize[0] +
357  i * s->subsampling[0] + k] = *src++;
358  *pu++ = *src++;
359  *pv++ = *src++;
360  }
361  }
362 }
363 
364 #if CONFIG_ZLIB
365 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
366  int size)
367 {
368  z_stream zstream = { 0 };
369  int zret;
370 
371  zstream.next_in = (uint8_t *)src;
372  zstream.avail_in = size;
373  zstream.next_out = dst;
374  zstream.avail_out = *len;
375  zret = inflateInit(&zstream);
376  if (zret != Z_OK) {
377  av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
378  return zret;
379  }
380  zret = inflate(&zstream, Z_SYNC_FLUSH);
381  inflateEnd(&zstream);
382  *len = zstream.total_out;
383  return zret == Z_STREAM_END ? Z_OK : zret;
384 }
385 
386 static int tiff_unpack_zlib(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
387  const uint8_t *src, int size, int width, int lines,
388  int strip_start, int is_yuv)
389 {
390  uint8_t *zbuf;
391  unsigned long outlen;
392  int ret, line;
393  outlen = width * lines;
394  zbuf = av_malloc(outlen);
395  if (!zbuf)
396  return AVERROR(ENOMEM);
397  if (s->fill_order) {
398  if ((ret = deinvert_buffer(s, src, size)) < 0) {
399  av_free(zbuf);
400  return ret;
401  }
402  src = s->deinvert_buf;
403  }
404  ret = tiff_uncompress(zbuf, &outlen, src, size);
405  if (ret != Z_OK) {
407  "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
408  (unsigned long)width * lines, ret);
409  av_free(zbuf);
410  return AVERROR_UNKNOWN;
411  }
412  src = zbuf;
413  for (line = 0; line < lines; line++) {
414  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
415  horizontal_fill(s, s->bpp, dst, 1, src, 0, width, 0);
416  } else {
417  memcpy(dst, src, width);
418  }
419  if (is_yuv) {
420  unpack_yuv(s, p, dst, strip_start + line);
421  line += s->subsampling[1] - 1;
422  }
423  dst += stride;
424  src += width;
425  }
426  av_free(zbuf);
427  return 0;
428 }
429 #endif
430 
431 #if CONFIG_LZMA
432 static int tiff_uncompress_lzma(uint8_t *dst, uint64_t *len, const uint8_t *src,
433  int size)
434 {
435  lzma_stream stream = LZMA_STREAM_INIT;
436  lzma_ret ret;
437 
438  stream.next_in = (uint8_t *)src;
439  stream.avail_in = size;
440  stream.next_out = dst;
441  stream.avail_out = *len;
442  ret = lzma_stream_decoder(&stream, UINT64_MAX, 0);
443  if (ret != LZMA_OK) {
444  av_log(NULL, AV_LOG_ERROR, "LZMA init error: %d\n", ret);
445  return ret;
446  }
447  ret = lzma_code(&stream, LZMA_RUN);
448  lzma_end(&stream);
449  *len = stream.total_out;
450  return ret == LZMA_STREAM_END ? LZMA_OK : ret;
451 }
452 
453 static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
454  const uint8_t *src, int size, int width, int lines,
455  int strip_start, int is_yuv)
456 {
457  uint64_t outlen = width * (uint64_t)lines;
458  int ret, line;
459  uint8_t *buf = av_malloc(outlen);
460  if (!buf)
461  return AVERROR(ENOMEM);
462  if (s->fill_order) {
463  if ((ret = deinvert_buffer(s, src, size)) < 0) {
464  av_free(buf);
465  return ret;
466  }
467  src = s->deinvert_buf;
468  }
469  ret = tiff_uncompress_lzma(buf, &outlen, src, size);
470  if (ret != LZMA_OK) {
472  "Uncompressing failed (%"PRIu64" of %"PRIu64") with error %d\n", outlen,
473  (uint64_t)width * lines, ret);
474  av_free(buf);
475  return AVERROR_UNKNOWN;
476  }
477  src = buf;
478  for (line = 0; line < lines; line++) {
479  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
480  horizontal_fill(s, s->bpp, dst, 1, src, 0, width, 0);
481  } else {
482  memcpy(dst, src, width);
483  }
484  if (is_yuv) {
485  unpack_yuv(s, p, dst, strip_start + line);
486  line += s->subsampling[1] - 1;
487  }
488  dst += stride;
489  src += width;
490  }
491  av_free(buf);
492  return 0;
493 }
494 #endif
495 
496 static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
497  const uint8_t *src, int size, int width, int lines)
498 {
499  int i, ret = 0;
500  int line;
501  uint8_t *src2;
502 
504  src2 = s->fax_buffer;
505 
506  if (!src2) {
508  "Error allocating temporary buffer\n");
509  return AVERROR(ENOMEM);
510  }
511 
512  if (!s->fill_order) {
513  memcpy(src2, src, size);
514  } else {
515  for (i = 0; i < size; i++)
516  src2[i] = ff_reverse[src[i]];
517  }
518  memset(src2 + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
519  ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
520  s->compr, s->fax_opts);
521  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
522  for (line = 0; line < lines; line++) {
523  horizontal_fill(s, s->bpp, dst, 1, dst, 0, width, 0);
524  dst += stride;
525  }
526  return ret;
527 }
528 
529 static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
530  const uint8_t *src, int size, int strip_start, int lines)
531 {
532  PutByteContext pb;
533  int c, line, pixels, code, ret;
534  const uint8_t *ssrc = src;
535  int width = ((s->width * s->bpp) + 7) >> 3;
537  int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
538  (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
539  desc->nb_components >= 3;
540 
541  if (s->planar)
542  width /= s->bppcount;
543 
544  if (size <= 0)
545  return AVERROR_INVALIDDATA;
546 
547  if (is_yuv) {
548  int bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
549  s->subsampling[0] * s->subsampling[1] + 7) >> 3;
550  av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
551  if (s->yuv_line == NULL) {
552  av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
553  return AVERROR(ENOMEM);
554  }
555  dst = s->yuv_line;
556  stride = 0;
557 
558  width = (s->width - 1) / s->subsampling[0] + 1;
559  width = width * s->subsampling[0] * s->subsampling[1] + 2*width;
560  av_assert0(width <= bytes_per_row);
561  av_assert0(s->bpp == 24);
562  }
563  if (s->is_bayer) {
564  width = (s->bpp * s->width + 7) >> 3;
565  }
566  if (p->format == AV_PIX_FMT_GRAY12) {
568  if (s->yuv_line == NULL) {
569  av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
570  return AVERROR(ENOMEM);
571  }
572  dst = s->yuv_line;
573  stride = 0;
574  }
575 
576  if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
577 #if CONFIG_ZLIB
578  return tiff_unpack_zlib(s, p, dst, stride, src, size, width, lines,
579  strip_start, is_yuv);
580 #else
582  "zlib support not enabled, "
583  "deflate compression not supported\n");
584  return AVERROR(ENOSYS);
585 #endif
586  }
587  if (s->compr == TIFF_LZMA) {
588 #if CONFIG_LZMA
589  return tiff_unpack_lzma(s, p, dst, stride, src, size, width, lines,
590  strip_start, is_yuv);
591 #else
593  "LZMA support not enabled\n");
594  return AVERROR(ENOSYS);
595 #endif
596  }
597  if (s->compr == TIFF_LZW) {
598  if (s->fill_order) {
599  if ((ret = deinvert_buffer(s, src, size)) < 0)
600  return ret;
601  ssrc = src = s->deinvert_buf;
602  }
603  if (size > 1 && !src[0] && (src[1]&1)) {
604  av_log(s->avctx, AV_LOG_ERROR, "Old style LZW is unsupported\n");
605  }
606  if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
607  av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
608  return ret;
609  }
610  for (line = 0; line < lines; line++) {
611  pixels = ff_lzw_decode(s->lzw, dst, width);
612  if (pixels < width) {
613  av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
614  pixels, width);
615  return AVERROR_INVALIDDATA;
616  }
617  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
618  horizontal_fill(s, s->bpp, dst, 1, dst, 0, width, 0);
619  if (is_yuv) {
620  unpack_yuv(s, p, dst, strip_start + line);
621  line += s->subsampling[1] - 1;
622  } else if (p->format == AV_PIX_FMT_GRAY12) {
623  unpack_gray(s, p, dst, strip_start + line, width, s->bpp);
624  }
625  dst += stride;
626  }
627  return 0;
628  }
629  if (s->compr == TIFF_CCITT_RLE ||
630  s->compr == TIFF_G3 ||
631  s->compr == TIFF_G4) {
632  if (is_yuv || p->format == AV_PIX_FMT_GRAY12)
633  return AVERROR_INVALIDDATA;
634 
635  return tiff_unpack_fax(s, dst, stride, src, size, width, lines);
636  }
637 
638  bytestream2_init(&s->gb, src, size);
639  bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * lines));
640 
641  for (line = 0; line < lines; line++) {
642  if (src - ssrc > size) {
643  av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
644  return AVERROR_INVALIDDATA;
645  }
646 
647  if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
648  break;
649  bytestream2_seek_p(&pb, stride * line, SEEK_SET);
650  switch (s->compr) {
651  case TIFF_RAW:
652  if (ssrc + size - src < width)
653  return AVERROR_INVALIDDATA;
654 
655  if (!s->fill_order) {
656  horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 || s->is_bayer),
657  dst, 1, src, 0, width, 0);
658  } else {
659  int i;
660  for (i = 0; i < width; i++)
661  dst[i] = ff_reverse[src[i]];
662  }
663  src += width;
664  break;
665  case TIFF_PACKBITS:
666  for (pixels = 0; pixels < width;) {
667  if (ssrc + size - src < 2) {
668  av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
669  return AVERROR_INVALIDDATA;
670  }
671  code = s->fill_order ? (int8_t) ff_reverse[*src++]: (int8_t) *src++;
672  if (code >= 0) {
673  code++;
674  if (pixels + code > width ||
675  ssrc + size - src < code) {
677  "Copy went out of bounds\n");
678  return AVERROR_INVALIDDATA;
679  }
681  dst, 1, src, 0, code, pixels);
682  src += code;
683  pixels += code;
684  } else if (code != -128) { // -127..-1
685  code = (-code) + 1;
686  if (pixels + code > width) {
688  "Run went out of bounds\n");
689  return AVERROR_INVALIDDATA;
690  }
691  c = *src++;
693  dst, 0, NULL, c, code, pixels);
694  pixels += code;
695  }
696  }
697  if (s->fill_order) {
698  int i;
699  for (i = 0; i < width; i++)
700  dst[i] = ff_reverse[dst[i]];
701  }
702  break;
703  }
704  if (is_yuv) {
705  unpack_yuv(s, p, dst, strip_start + line);
706  line += s->subsampling[1] - 1;
707  } else if (p->format == AV_PIX_FMT_GRAY12) {
708  unpack_gray(s, p, dst, strip_start + line, width, s->bpp);
709  }
710  dst += stride;
711  }
712  return 0;
713 }
714 
716 {
717  int ret;
718  int create_gray_palette = 0;
719 
720  // make sure there is no aliasing in the following switch
721  if (s->bpp >= 100 || s->bppcount >= 10) {
723  "Unsupported image parameters: bpp=%d, bppcount=%d\n",
724  s->bpp, s->bppcount);
725  return AVERROR_INVALIDDATA;
726  }
727 
728  switch (s->planar * 1000 + s->bpp * 10 + s->bppcount + s->is_bayer * 10000) {
729  case 11:
730  if (!s->palette_is_set) {
732  break;
733  }
734  case 21:
735  case 41:
737  if (!s->palette_is_set) {
738  create_gray_palette = 1;
739  }
740  break;
741  case 81:
743  break;
744  case 121:
746  break;
747  case 10081:
748  switch (AV_RL32(s->pattern)) {
749  case 0x02010100:
751  break;
752  case 0x00010102:
754  break;
755  case 0x01000201:
757  break;
758  case 0x01020001:
760  break;
761  default:
762  av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n",
763  AV_RL32(s->pattern));
764  return AVERROR_PATCHWELCOME;
765  }
766  break;
767  case 10121:
768  switch (AV_RL32(s->pattern)) {
769  case 0x02010100:
771  break;
772  case 0x00010102:
774  break;
775  case 0x01000201:
777  break;
778  case 0x01020001:
780  break;
781  default:
782  av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n",
783  AV_RL32(s->pattern));
784  return AVERROR_PATCHWELCOME;
785  }
786  break;
787  case 10161:
788  switch (AV_RL32(s->pattern)) {
789  case 0x02010100:
791  break;
792  case 0x00010102:
794  break;
795  case 0x01000201:
797  break;
798  case 0x01020001:
800  break;
801  default:
802  av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n",
803  AV_RL32(s->pattern));
804  return AVERROR_PATCHWELCOME;
805  }
806  break;
807  case 243:
809  if (s->subsampling[0] == 1 && s->subsampling[1] == 1) {
811  } else if (s->subsampling[0] == 2 && s->subsampling[1] == 1) {
813  } else if (s->subsampling[0] == 4 && s->subsampling[1] == 1) {
815  } else if (s->subsampling[0] == 1 && s->subsampling[1] == 2) {
817  } else if (s->subsampling[0] == 2 && s->subsampling[1] == 2) {
819  } else if (s->subsampling[0] == 4 && s->subsampling[1] == 4) {
821  } else {
822  av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr subsampling\n");
823  return AVERROR_PATCHWELCOME;
824  }
825  } else
827  break;
828  case 161:
830  break;
831  case 162:
833  break;
834  case 322:
836  break;
837  case 324:
839  break;
840  case 405:
843  else {
845  "bpp=40 without PHOTOMETRIC_SEPARATED is unsupported\n");
846  return AVERROR_PATCHWELCOME;
847  }
848  break;
849  case 483:
851  break;
852  case 644:
854  break;
855  case 1243:
857  break;
858  case 1324:
860  break;
861  case 1483:
863  break;
864  case 1644:
866  break;
867  default:
869  "This format is not supported (bpp=%d, bppcount=%d)\n",
870  s->bpp, s->bppcount);
871  return AVERROR_INVALIDDATA;
872  }
873 
876  if((desc->flags & AV_PIX_FMT_FLAG_RGB) ||
877  !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
878  desc->nb_components < 3) {
879  av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr variant\n");
880  return AVERROR_INVALIDDATA;
881  }
882  }
883 
884  if (s->width != s->avctx->width || s->height != s->avctx->height) {
885  ret = ff_set_dimensions(s->avctx, s->width, s->height);
886  if (ret < 0)
887  return ret;
888  }
889  if ((ret = ff_thread_get_buffer(s->avctx, frame, 0)) < 0)
890  return ret;
891  if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
892  if (!create_gray_palette)
893  memcpy(frame->f->data[1], s->palette, sizeof(s->palette));
894  else {
895  /* make default grayscale pal */
896  int i;
897  uint32_t *pal = (uint32_t *)frame->f->data[1];
898  for (i = 0; i < 1<<s->bpp; i++)
899  pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
900  }
901  }
902  return 0;
903 }
904 
905 static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
906 {
907  int offset = tag == TIFF_YRES ? 2 : 0;
908  s->res[offset++] = num;
909  s->res[offset] = den;
910  if (s->res[0] && s->res[1] && s->res[2] && s->res[3]) {
911  uint64_t num = s->res[2] * (uint64_t)s->res[1];
912  uint64_t den = s->res[0] * (uint64_t)s->res[3];
913  if (num > INT64_MAX || den > INT64_MAX) {
914  num = num >> 1;
915  den = den >> 1;
916  }
918  num, den, INT32_MAX);
919  if (!s->avctx->sample_aspect_ratio.den)
920  s->avctx->sample_aspect_ratio = (AVRational) {0, 1};
921  }
922 }
923 
925 {
926  unsigned tag, type, count, off, value = 0, value2 = 0;
927  int i, start;
928  int pos;
929  int ret;
930  double *dp;
931 
932  ret = ff_tread_tag(&s->gb, s->le, &tag, &type, &count, &start);
933  if (ret < 0) {
934  goto end;
935  }
936 
937  off = bytestream2_tell(&s->gb);
938  if (count == 1) {
939  switch (type) {
940  case TIFF_BYTE:
941  case TIFF_SHORT:
942  case TIFF_LONG:
943  value = ff_tget(&s->gb, type, s->le);
944  break;
945  case TIFF_RATIONAL:
946  value = ff_tget(&s->gb, TIFF_LONG, s->le);
947  value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
948  break;
949  case TIFF_STRING:
950  if (count <= 4) {
951  break;
952  }
953  default:
954  value = UINT_MAX;
955  }
956  }
957 
958  switch (tag) {
959  case TIFF_SUBFILE:
960  s->is_thumbnail = (value != 0);
961  case TIFF_WIDTH:
962  s->width = value;
963  break;
964  case TIFF_HEIGHT:
965  s->height = value;
966  break;
967  case TIFF_BPP:
968  if (count > 5U) {
970  "This format is not supported (bpp=%d, %d components)\n",
971  value, count);
972  return AVERROR_INVALIDDATA;
973  }
974  s->bppcount = count;
975  if (count == 1)
976  s->bpp = value;
977  else {
978  switch (type) {
979  case TIFF_BYTE:
980  case TIFF_SHORT:
981  case TIFF_LONG:
982  s->bpp = 0;
983  if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
984  return AVERROR_INVALIDDATA;
985  for (i = 0; i < count; i++)
986  s->bpp += ff_tget(&s->gb, type, s->le);
987  break;
988  default:
989  s->bpp = -1;
990  }
991  }
992  break;
994  if (count != 1) {
996  "Samples per pixel requires a single value, many provided\n");
997  return AVERROR_INVALIDDATA;
998  }
999  if (value > 5U) {
1001  "Samples per pixel %d is too large\n", value);
1002  return AVERROR_INVALIDDATA;
1003  }
1004  if (s->bppcount == 1)
1005  s->bpp *= value;
1006  s->bppcount = value;
1007  break;
1008  case TIFF_COMPR:
1009  s->compr = value;
1010  av_log(s->avctx, AV_LOG_DEBUG, "compression: %d\n", s->compr);
1011  s->predictor = 0;
1012  switch (s->compr) {
1013  case TIFF_RAW:
1014  case TIFF_PACKBITS:
1015  case TIFF_LZW:
1016  case TIFF_CCITT_RLE:
1017  break;
1018  case TIFF_G3:
1019  case TIFF_G4:
1020  s->fax_opts = 0;
1021  break;
1022  case TIFF_DEFLATE:
1023  case TIFF_ADOBE_DEFLATE:
1024 #if CONFIG_ZLIB
1025  break;
1026 #else
1027  av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
1028  return AVERROR(ENOSYS);
1029 #endif
1030  case TIFF_JPEG:
1031  case TIFF_NEWJPEG:
1032  avpriv_report_missing_feature(s->avctx, "JPEG compression");
1033  return AVERROR_PATCHWELCOME;
1034  case TIFF_LZMA:
1035 #if CONFIG_LZMA
1036  break;
1037 #else
1038  av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n");
1039  return AVERROR(ENOSYS);
1040 #endif
1041  default:
1042  av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
1043  s->compr);
1044  return AVERROR_INVALIDDATA;
1045  }
1046  break;
1047  case TIFF_ROWSPERSTRIP:
1048  if (!value || (type == TIFF_LONG && value == UINT_MAX))
1049  value = s->height;
1050  s->rps = FFMIN(value, s->height);
1051  break;
1052  case TIFF_STRIP_OFFS:
1053  if (count == 1) {
1054  if (value > INT_MAX) {
1056  "strippos %u too large\n", value);
1057  return AVERROR_INVALIDDATA;
1058  }
1059  s->strippos = 0;
1060  s->stripoff = value;
1061  } else
1062  s->strippos = off;
1063  s->strips = count;
1064  if (s->strips == 1)
1065  s->rps = s->height;
1066  s->sot = type;
1067  break;
1068  case TIFF_STRIP_SIZE:
1069  if (count == 1) {
1070  if (value > INT_MAX) {
1072  "stripsize %u too large\n", value);
1073  return AVERROR_INVALIDDATA;
1074  }
1075  s->stripsizesoff = 0;
1076  s->stripsize = value;
1077  s->strips = 1;
1078  } else {
1079  s->stripsizesoff = off;
1080  }
1081  s->strips = count;
1082  s->sstype = type;
1083  break;
1084  case TIFF_XRES:
1085  case TIFF_YRES:
1086  set_sar(s, tag, value, value2);
1087  break;
1088  case TIFF_TILE_BYTE_COUNTS:
1089  case TIFF_TILE_LENGTH:
1090  case TIFF_TILE_OFFSETS:
1091  case TIFF_TILE_WIDTH:
1092  av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");
1093  return AVERROR_PATCHWELCOME;
1094  break;
1095  case TIFF_PREDICTOR:
1096  s->predictor = value;
1097  break;
1098  case TIFF_SUB_IFDS:
1099  if (count == 1)
1100  s->sub_ifd = value;
1101  else if (count > 1)
1102  s->sub_ifd = ff_tget(&s->gb, TIFF_LONG, s->le); /** Only get the first SubIFD */
1103  break;
1104  case DNG_WHITE_LEVEL:
1105  s->white_level = value;
1106  break;
1107  case TIFF_CFA_PATTERN_DIM:
1108  if (count != 2 || (ff_tget(&s->gb, type, s->le) != 2 &&
1109  ff_tget(&s->gb, type, s->le) != 2)) {
1110  av_log(s->avctx, AV_LOG_ERROR, "CFA Pattern dimensions are not 2x2\n");
1111  return AVERROR_INVALIDDATA;
1112  }
1113  break;
1114  case TIFF_CFA_PATTERN:
1115  s->is_bayer = 1;
1116  s->pattern[0] = ff_tget(&s->gb, type, s->le);
1117  s->pattern[1] = ff_tget(&s->gb, type, s->le);
1118  s->pattern[2] = ff_tget(&s->gb, type, s->le);
1119  s->pattern[3] = ff_tget(&s->gb, type, s->le);
1120  break;
1121  case TIFF_PHOTOMETRIC:
1122  switch (value) {
1125  case TIFF_PHOTOMETRIC_RGB:
1129  case TIFF_PHOTOMETRIC_CFA:
1130  s->photometric = value;
1131  break;
1140  "PhotometricInterpretation 0x%04X",
1141  value);
1142  return AVERROR_PATCHWELCOME;
1143  default:
1144  av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
1145  "unknown\n", value);
1146  return AVERROR_INVALIDDATA;
1147  }
1148  break;
1149  case TIFF_FILL_ORDER:
1150  if (value < 1 || value > 2) {
1152  "Unknown FillOrder value %d, trying default one\n", value);
1153  value = 1;
1154  }
1155  s->fill_order = value - 1;
1156  break;
1157  case TIFF_PAL: {
1158  GetByteContext pal_gb[3];
1159  off = type_sizes[type];
1160  if (count / 3 > 256 ||
1161  bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
1162  return AVERROR_INVALIDDATA;
1163 
1164  pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
1165  bytestream2_skip(&pal_gb[1], count / 3 * off);
1166  bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
1167 
1168  off = (type_sizes[type] - 1) << 3;
1169  if (off > 31U) {
1170  av_log(s->avctx, AV_LOG_ERROR, "palette shift %d is out of range\n", off);
1171  return AVERROR_INVALIDDATA;
1172  }
1173 
1174  for (i = 0; i < count / 3; i++) {
1175  uint32_t p = 0xFF000000;
1176  p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16;
1177  p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8;
1178  p |= ff_tget(&pal_gb[2], type, s->le) >> off;
1179  s->palette[i] = p;
1180  }
1181  s->palette_is_set = 1;
1182  break;
1183  }
1184  case TIFF_PLANAR:
1185  s->planar = value == 2;
1186  break;
1188  if (count != 2) {
1189  av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n");
1190  return AVERROR_INVALIDDATA;
1191  }
1192  for (i = 0; i < count; i++) {
1193  s->subsampling[i] = ff_tget(&s->gb, type, s->le);
1194  if (s->subsampling[i] <= 0) {
1195  av_log(s->avctx, AV_LOG_ERROR, "subsampling %d is invalid\n", s->subsampling[i]);
1196  s->subsampling[i] = 1;
1197  return AVERROR_INVALIDDATA;
1198  }
1199  }
1200  break;
1201  case TIFF_T4OPTIONS:
1202  if (s->compr == TIFF_G3)
1203  s->fax_opts = value;
1204  break;
1205  case TIFF_T6OPTIONS:
1206  if (s->compr == TIFF_G4)
1207  s->fax_opts = value;
1208  break;
1209 #define ADD_METADATA(count, name, sep)\
1210  if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1211  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1212  goto end;\
1213  }
1215  ADD_METADATA(count, "ModelPixelScaleTag", NULL);
1216  break;
1218  ADD_METADATA(count, "ModelTransformationTag", NULL);
1219  break;
1220  case TIFF_MODEL_TIEPOINT:
1221  ADD_METADATA(count, "ModelTiepointTag", NULL);
1222  break;
1224  if (s->geotag_count) {
1225  avpriv_request_sample(s->avctx, "Multiple geo key directories\n");
1226  return AVERROR_INVALIDDATA;
1227  }
1228  ADD_METADATA(1, "GeoTIFF_Version", NULL);
1229  ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
1230  s->geotag_count = ff_tget_short(&s->gb, s->le);
1231  if (s->geotag_count > count / 4 - 1) {
1232  s->geotag_count = count / 4 - 1;
1233  av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
1234  }
1235  if ( bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4
1236  || s->geotag_count == 0) {
1237  s->geotag_count = 0;
1238  return -1;
1239  }
1240  s->geotags = av_mallocz_array(s->geotag_count, sizeof(TiffGeoTag));
1241  if (!s->geotags) {
1242  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1243  s->geotag_count = 0;
1244  goto end;
1245  }
1246  for (i = 0; i < s->geotag_count; i++) {
1247  s->geotags[i].key = ff_tget_short(&s->gb, s->le);
1248  s->geotags[i].type = ff_tget_short(&s->gb, s->le);
1249  s->geotags[i].count = ff_tget_short(&s->gb, s->le);
1250 
1251  if (!s->geotags[i].type)
1252  s->geotags[i].val = get_geokey_val(s->geotags[i].key, ff_tget_short(&s->gb, s->le));
1253  else
1254  s->geotags[i].offset = ff_tget_short(&s->gb, s->le);
1255  }
1256  break;
1258  if (count >= INT_MAX / sizeof(int64_t))
1259  return AVERROR_INVALIDDATA;
1260  if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
1261  return AVERROR_INVALIDDATA;
1262  dp = av_malloc_array(count, sizeof(double));
1263  if (!dp) {
1264  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1265  goto end;
1266  }
1267  for (i = 0; i < count; i++)
1268  dp[i] = ff_tget_double(&s->gb, s->le);
1269  for (i = 0; i < s->geotag_count; i++) {
1270  if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
1271  if (s->geotags[i].count == 0
1272  || s->geotags[i].offset + s->geotags[i].count > count) {
1273  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1274  } else if (s->geotags[i].val) {
1275  av_log(s->avctx, AV_LOG_WARNING, "Duplicate GeoTIFF key %d\n", s->geotags[i].key);
1276  } else {
1277  char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
1278  if (!ap) {
1279  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1280  av_freep(&dp);
1281  return AVERROR(ENOMEM);
1282  }
1283  s->geotags[i].val = ap;
1284  }
1285  }
1286  }
1287  av_freep(&dp);
1288  break;
1289  case TIFF_GEO_ASCII_PARAMS:
1290  pos = bytestream2_tell(&s->gb);
1291  for (i = 0; i < s->geotag_count; i++) {
1292  if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
1293  if (s->geotags[i].count == 0
1294  || s->geotags[i].offset + s->geotags[i].count > count) {
1295  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1296  } else {
1297  char *ap;
1298 
1299  bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
1300  if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
1301  return AVERROR_INVALIDDATA;
1302  if (s->geotags[i].val)
1303  return AVERROR_INVALIDDATA;
1304  ap = av_malloc(s->geotags[i].count);
1305  if (!ap) {
1306  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1307  return AVERROR(ENOMEM);
1308  }
1309  bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
1310  ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
1311  s->geotags[i].val = ap;
1312  }
1313  }
1314  }
1315  break;
1316  case TIFF_ARTIST:
1317  ADD_METADATA(count, "artist", NULL);
1318  break;
1319  case TIFF_COPYRIGHT:
1320  ADD_METADATA(count, "copyright", NULL);
1321  break;
1322  case TIFF_DATE:
1323  ADD_METADATA(count, "date", NULL);
1324  break;
1325  case TIFF_DOCUMENT_NAME:
1326  ADD_METADATA(count, "document_name", NULL);
1327  break;
1328  case TIFF_HOST_COMPUTER:
1329  ADD_METADATA(count, "computer", NULL);
1330  break;
1332  ADD_METADATA(count, "description", NULL);
1333  break;
1334  case TIFF_MAKE:
1335  ADD_METADATA(count, "make", NULL);
1336  break;
1337  case TIFF_MODEL:
1338  ADD_METADATA(count, "model", NULL);
1339  break;
1340  case TIFF_PAGE_NAME:
1341  ADD_METADATA(count, "page_name", NULL);
1342  break;
1343  case TIFF_PAGE_NUMBER:
1344  ADD_METADATA(count, "page_number", " / ");
1345  // need to seek back to re-read the page number
1346  bytestream2_seek(&s->gb, -count * sizeof(uint16_t), SEEK_CUR);
1347  // read the page number
1348  s->cur_page = ff_tget(&s->gb, TIFF_SHORT, s->le);
1349  // get back to where we were before the previous seek
1350  bytestream2_seek(&s->gb, count * sizeof(uint16_t) - sizeof(uint16_t), SEEK_CUR);
1351  break;
1352  case TIFF_SOFTWARE_NAME:
1353  ADD_METADATA(count, "software", NULL);
1354  break;
1355  case DNG_VERSION:
1356  if (count == 4) {
1357  unsigned int ver[4];
1358  ver[0] = ff_tget(&s->gb, type, s->le);
1359  ver[1] = ff_tget(&s->gb, type, s->le);
1360  ver[2] = ff_tget(&s->gb, type, s->le);
1361  ver[3] = ff_tget(&s->gb, type, s->le);
1362 
1363  av_log(s->avctx, AV_LOG_DEBUG, "DNG file, version %u.%u.%u.%u\n",
1364  ver[0], ver[1], ver[2], ver[3]);
1365 
1367  }
1368  break;
1369  case CINEMADNG_TIME_CODES:
1370  case CINEMADNG_FRAME_RATE:
1371  case CINEMADNG_T_STOP:
1372  case CINEMADNG_REEL_NAME:
1375  break;
1376  default:
1377  if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1379  "Unknown or unsupported tag %d/0x%0X\n",
1380  tag, tag);
1381  return AVERROR_INVALIDDATA;
1382  }
1383  }
1384 end:
1385  if (s->bpp > 64U) {
1387  "This format is not supported (bpp=%d, %d components)\n",
1388  s->bpp, count);
1389  s->bpp = 0;
1390  return AVERROR_INVALIDDATA;
1391  }
1392  bytestream2_seek(&s->gb, start, SEEK_SET);
1393  return 0;
1394 }
1395 
1397  void *data, int *got_frame, AVPacket *avpkt)
1398 {
1399  TiffContext *const s = avctx->priv_data;
1400  AVFrame *const p = data;
1401  ThreadFrame frame = { .f = data };
1402  unsigned off, last_off;
1403  int le, ret, plane, planes;
1404  int i, j, entries, stride;
1405  unsigned soff, ssize;
1406  uint8_t *dst;
1407  GetByteContext stripsizes;
1408  GetByteContext stripdata;
1409  int retry_for_subifd, retry_for_page;
1410 
1411  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1412 
1413  // parse image header
1414  if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
1415  av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
1416  return ret;
1417  } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1418  av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1419  return AVERROR_INVALIDDATA;
1420  }
1421  s->le = le;
1422  // TIFF_BPP is not a required tag and defaults to 1
1423 again:
1424  s->is_thumbnail = 0;
1425  s->bppcount = s->bpp = 1;
1427  s->compr = TIFF_RAW;
1428  s->fill_order = 0;
1429  s->white_level = 0;
1430  s->is_bayer = 0;
1431  s->cur_page = 0;
1433  free_geotags(s);
1434 
1435  // Reset these offsets so we can tell if they were set this frame
1436  s->stripsizesoff = s->strippos = 0;
1437  /* parse image file directory */
1438  bytestream2_seek(&s->gb, off, SEEK_SET);
1439  entries = ff_tget_short(&s->gb, le);
1440  if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
1441  return AVERROR_INVALIDDATA;
1442  for (i = 0; i < entries; i++) {
1443  if ((ret = tiff_decode_tag(s, p)) < 0)
1444  return ret;
1445  }
1446 
1447  if (s->get_thumbnail && !s->is_thumbnail) {
1448  av_log(avctx, AV_LOG_INFO, "No embedded thumbnail present\n");
1449  return AVERROR_EOF;
1450  }
1451 
1452  /** whether we should process this IFD's SubIFD */
1453  retry_for_subifd = s->sub_ifd && (s->get_subimage || (!s->get_thumbnail && s->is_thumbnail));
1454  /** whether we should process this multi-page IFD's next page */
1455  retry_for_page = s->get_page && s->cur_page + 1 < s->get_page; // get_page is 1-indexed
1456 
1457  last_off = off;
1458  if (retry_for_page) {
1459  // set offset to the next IFD
1460  off = ff_tget_long(&s->gb, le);
1461  } else if (retry_for_subifd) {
1462  // set offset to the SubIFD
1463  off = s->sub_ifd;
1464  }
1465 
1466  if (retry_for_subifd || retry_for_page) {
1467  if (!off) {
1468  av_log(avctx, AV_LOG_ERROR, "Requested entry not found\n");
1469  return AVERROR_INVALIDDATA;
1470  }
1471  if (off <= last_off) {
1472  avpriv_request_sample(s->avctx, "non increasing IFD offset\n");
1473  return AVERROR_INVALIDDATA;
1474  }
1475  if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1476  av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1477  return AVERROR_INVALIDDATA;
1478  }
1479  s->sub_ifd = 0;
1480  goto again;
1481  }
1482 
1483  for (i = 0; i<s->geotag_count; i++) {
1484  const char *keyname = get_geokey_name(s->geotags[i].key);
1485  if (!keyname) {
1486  av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
1487  continue;
1488  }
1489  if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
1490  av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
1491  continue;
1492  }
1493  ret = av_dict_set(&p->metadata, keyname, s->geotags[i].val, 0);
1494  if (ret<0) {
1495  av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
1496  return ret;
1497  }
1498  }
1499 
1500  if (!s->strippos && !s->stripoff) {
1501  av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
1502  return AVERROR_INVALIDDATA;
1503  }
1504  /* now we have the data and may start decoding */
1505  if ((ret = init_image(s, &frame)) < 0)
1506  return ret;
1507 
1508  if (s->strips == 1 && !s->stripsize) {
1509  av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
1510  s->stripsize = avpkt->size - s->stripoff;
1511  }
1512 
1513  if (s->stripsizesoff) {
1514  if (s->stripsizesoff >= (unsigned)avpkt->size)
1515  return AVERROR_INVALIDDATA;
1516  bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
1517  avpkt->size - s->stripsizesoff);
1518  }
1519  if (s->strippos) {
1520  if (s->strippos >= (unsigned)avpkt->size)
1521  return AVERROR_INVALIDDATA;
1522  bytestream2_init(&stripdata, avpkt->data + s->strippos,
1523  avpkt->size - s->strippos);
1524  }
1525 
1526  if (s->rps <= 0 || s->rps % s->subsampling[1]) {
1527  av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
1528  return AVERROR_INVALIDDATA;
1529  }
1530 
1531  planes = s->planar ? s->bppcount : 1;
1532  for (plane = 0; plane < planes; plane++) {
1533  uint8_t *five_planes = NULL;
1534  int remaining = avpkt->size;
1535  int decoded_height;
1536  stride = p->linesize[plane];
1537  dst = p->data[plane];
1539  s->avctx->pix_fmt == AV_PIX_FMT_RGBA) {
1540  stride = stride * 5 / 4;
1541  five_planes =
1542  dst = av_malloc(stride * s->height);
1543  if (!dst)
1544  return AVERROR(ENOMEM);
1545  }
1546  for (i = 0; i < s->height; i += s->rps) {
1547  if (i)
1548  dst += s->rps * stride;
1549  if (s->stripsizesoff)
1550  ssize = ff_tget(&stripsizes, s->sstype, le);
1551  else
1552  ssize = s->stripsize;
1553 
1554  if (s->strippos)
1555  soff = ff_tget(&stripdata, s->sot, le);
1556  else
1557  soff = s->stripoff;
1558 
1559  if (soff > avpkt->size || ssize > avpkt->size - soff || ssize > remaining) {
1560  av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
1561  av_freep(&five_planes);
1562  return AVERROR_INVALIDDATA;
1563  }
1564  remaining -= ssize;
1565  if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,
1566  FFMIN(s->rps, s->height - i))) < 0) {
1567  if (avctx->err_recognition & AV_EF_EXPLODE) {
1568  av_freep(&five_planes);
1569  return ret;
1570  }
1571  break;
1572  }
1573  }
1574  decoded_height = FFMIN(i, s->height);
1575 
1576  if (s->predictor == 2) {
1577  if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1578  av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");
1579  return AVERROR_PATCHWELCOME;
1580  }
1581  dst = five_planes ? five_planes : p->data[plane];
1582  soff = s->bpp >> 3;
1583  if (s->planar)
1584  soff = FFMAX(soff / s->bppcount, 1);
1585  ssize = s->width * soff;
1586  if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
1589  s->avctx->pix_fmt == AV_PIX_FMT_YA16LE ||
1592  for (i = 0; i < decoded_height; i++) {
1593  for (j = soff; j < ssize; j += 2)
1594  AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
1595  dst += stride;
1596  }
1597  } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1600  s->avctx->pix_fmt == AV_PIX_FMT_YA16BE ||
1603  for (i = 0; i < decoded_height; i++) {
1604  for (j = soff; j < ssize; j += 2)
1605  AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
1606  dst += stride;
1607  }
1608  } else {
1609  for (i = 0; i < decoded_height; i++) {
1610  for (j = soff; j < ssize; j++)
1611  dst[j] += dst[j - soff];
1612  dst += stride;
1613  }
1614  }
1615  }
1616 
1618  int c = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255);
1619  dst = p->data[plane];
1620  for (i = 0; i < s->height; i++) {
1621  for (j = 0; j < stride; j++)
1622  dst[j] = c - dst[j];
1623  dst += stride;
1624  }
1625  }
1626 
1629  int x = s->avctx->pix_fmt == AV_PIX_FMT_RGB0 ? 4 : 5;
1630  uint8_t *src = five_planes ? five_planes : p->data[plane];
1631  dst = p->data[plane];
1632  for (i = 0; i < s->height; i++) {
1633  for (j = 0; j < s->width; j++) {
1634  int k = 255 - src[x * j + 3];
1635  int r = (255 - src[x * j ]) * k;
1636  int g = (255 - src[x * j + 1]) * k;
1637  int b = (255 - src[x * j + 2]) * k;
1638  dst[4 * j ] = r * 257 >> 16;
1639  dst[4 * j + 1] = g * 257 >> 16;
1640  dst[4 * j + 2] = b * 257 >> 16;
1641  dst[4 * j + 3] = s->avctx->pix_fmt == AV_PIX_FMT_RGBA ? src[x * j + 4] : 255;
1642  }
1643  src += stride;
1644  dst += p->linesize[plane];
1645  }
1646  av_freep(&five_planes);
1647  } else if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED &&
1649  dst = p->data[plane];
1650  for (i = 0; i < s->height; i++) {
1651  for (j = 0; j < s->width; j++) {
1652  uint64_t k = 65535 - AV_RB16(dst + 8 * j + 6);
1653  uint64_t r = (65535 - AV_RB16(dst + 8 * j )) * k;
1654  uint64_t g = (65535 - AV_RB16(dst + 8 * j + 2)) * k;
1655  uint64_t b = (65535 - AV_RB16(dst + 8 * j + 4)) * k;
1656  AV_WB16(dst + 8 * j , r * 65537 >> 32);
1657  AV_WB16(dst + 8 * j + 2, g * 65537 >> 32);
1658  AV_WB16(dst + 8 * j + 4, b * 65537 >> 32);
1659  AV_WB16(dst + 8 * j + 6, 65535);
1660  }
1661  dst += p->linesize[plane];
1662  }
1663  }
1664  }
1665 
1666  if (s->planar && s->bppcount > 2) {
1667  FFSWAP(uint8_t*, p->data[0], p->data[2]);
1668  FFSWAP(int, p->linesize[0], p->linesize[2]);
1669  FFSWAP(uint8_t*, p->data[0], p->data[1]);
1670  FFSWAP(int, p->linesize[0], p->linesize[1]);
1671  }
1672 
1673  if (s->is_bayer && s->white_level && s->bpp == 16) {
1674  uint16_t *dst = (uint16_t *)p->data[0];
1675  for (i = 0; i < s->height; i++) {
1676  for (j = 0; j < s->width; j++)
1677  dst[j] = FFMIN((dst[j] / (float)s->white_level) * 65535, 65535);
1678  dst += stride / 2;
1679  }
1680  }
1681 
1682  *got_frame = 1;
1683 
1684  return avpkt->size;
1685 }
1686 
1688 {
1689  TiffContext *s = avctx->priv_data;
1690 
1691  s->width = 0;
1692  s->height = 0;
1693  s->subsampling[0] =
1694  s->subsampling[1] = 1;
1695  s->avctx = avctx;
1696  ff_lzw_decode_open(&s->lzw);
1697  if (!s->lzw)
1698  return AVERROR(ENOMEM);
1700 
1701  return 0;
1702 }
1703 
1705 {
1706  TiffContext *const s = avctx->priv_data;
1707 
1708  free_geotags(s);
1709 
1710  ff_lzw_decode_close(&s->lzw);
1711  av_freep(&s->deinvert_buf);
1712  s->deinvert_buf_size = 0;
1713  av_freep(&s->yuv_line);
1714  s->yuv_line_size = 0;
1715  av_freep(&s->fax_buffer);
1716  s->fax_buffer_size = 0;
1717  return 0;
1718 }
1719 
1720 #define OFFSET(x) offsetof(TiffContext, x)
1721 static const AVOption tiff_options[] = {
1722  { "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 },
1723  { "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 },
1724  { "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 },
1725  { NULL },
1726 };
1727 
1728 static const AVClass tiff_decoder_class = {
1729  .class_name = "TIFF decoder",
1730  .item_name = av_default_item_name,
1731  .option = tiff_options,
1732  .version = LIBAVUTIL_VERSION_INT,
1733 };
1734 
1736  .name = "tiff",
1737  .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
1738  .type = AVMEDIA_TYPE_VIDEO,
1739  .id = AV_CODEC_ID_TIFF,
1740  .priv_data_size = sizeof(TiffContext),
1741  .init = tiff_init,
1742  .close = tiff_end,
1743  .decode = decode_frame,
1745  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1746  .priv_class = &tiff_decoder_class,
1747 };
const char * name
Definition: avisynth_c.h:867
Definition: tiff.h:64
int plane
Definition: avisynth_c.h:384
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
const char const char void * val
Definition: avisynth_c.h:863
Definition: tiff.h:118
static const struct @314 planes[]
int offset
Definition: tiff.h:208
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
const TiffGeoTagKeyName ff_tiff_projection_codes[]
Definition: tiff_data.c:1497
int size
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2522
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
TiffPhotometric
list of TIFF, TIFF/AP and DNG PhotometricInterpretation (TIFF_PHOTOMETRIC) values ...
Definition: tiff.h:180
AVOption.
Definition: opt.h:246
int fill_order
Definition: tiff.c:73
unsigned int bpp
Definition: tiff.c:63
8 bits gray, 8 bits alpha
Definition: pixfmt.h:143
int geotag_count
Definition: tiff.c:96
uint32_t res[4]
Definition: tiff.c:74
Definition: tiff.h:63
int sstype
Definition: tiff.c:84
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
static int init_thread_copy(AVCodecContext *avctx)
Definition: tta.c:392
AVFrame * f
Definition: thread.h:35
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
uint8_t pattern[4]
Definition: tiff.c:78
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:214
const char * desc
Definition: nvenc.c:68
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:79
int size
Definition: avcodec.h:1478
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:191
bayer, GRGR..(odd line), BGBG..(even line), 16-bit samples, little-endian */
Definition: pixfmt.h:270
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:146
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:1944
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:1775
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:105
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:93
bayer, BGBG..(odd line), GRGR..(even line), 8-bit samples */
Definition: pixfmt.h:260
enum TiffType tiff_type
Definition: tiff.c:61
#define src
Definition: vp8dsp.c:254
unsigned int yuv_line_size
Definition: tiff.c:92
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3481
Definition: tiff.h:122
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:273
Definition: tiff.h:121
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: tiff.c:1396
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:529
static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
Definition: tiff.c:924
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:369
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:90
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:57
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:238
char * val
Definition: tiff.h:209
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
bayer, GBGB..(odd line), RGRG..(even line), 16-bit samples, little-endian */
Definition: pixfmt.h:268
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
bayer, GBGB..(odd line), RGRG..(even line), 16-bit samples, big-endian */
Definition: pixfmt.h:269
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
static av_cold int tiff_init(AVCodecContext *avctx)
Definition: tiff.c:1687
int is_thumbnail
Definition: tiff.c:75
static AVFrame * frame
const char data[16]
Definition: mxf.c:91
int get_thumbnail
Definition: tiff.c:59
uint8_t * data
Definition: avcodec.h:1477
planar GBR 4:4:4 48bpp, big-endian
Definition: pixfmt.h:174
uint32_t tag
Definition: movenc.c:1496
static int add_metadata(int count, int type, const char *name, const char *sep, TiffContext *s, AVFrame *frame)
Definition: tiff.c:249
int stripoff
Definition: tiff.c:86
#define AVERROR_EOF
End of file.
Definition: error.h:55
bitstream reader API header.
AVDictionary * metadata
metadata.
Definition: frame.h:581
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:87
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
bayer, BGBG..(odd line), GRGR..(even line), 16-bit samples, little-endian */
Definition: pixfmt.h:264
#define av_log(a,...)
Definition: tiff.h:78
int planar
Definition: tiff.c:69
#define U(x)
Definition: vp56_arith.h:37
Definition: lzw.c:46
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
int height
Definition: tiff.c:62
#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
unsigned int fax_buffer_size
Definition: tiff.c:94
enum TiffGeoTagKey key
Definition: tiff.h:205
int sot
Definition: tiff.c:85
#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
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
static av_cold int tiff_end(AVCodecContext *avctx)
Definition: tiff.c:1704
unsigned ff_tget_short(GetByteContext *gb, int le)
Reads a short from the bytestream using given endianness.
Definition: tiff_common.c:43
const char * r
Definition: vf_curves.c:114
static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
Definition: tiff.c:310
#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
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
Definition: graph2dot.c:48
uint16_t get_page
Definition: tiff.c:58
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
Definition: tiff.c:905
int width
Definition: tiff.c:62
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:496
int strips
Definition: tiff.c:84
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:381
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:94
uint8_t * yuv_line
Definition: tiff.c:91
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1037
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:72
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:225
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
enum TiffPhotometric photometric
Definition: tiff.c:68
const TiffGeoTagKeyName ff_tiff_proj_cs_type_codes[]
Definition: tiff_data.c:516
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:83
int stripsize
Definition: tiff.c:86
static const AVOption tiff_options[]
Definition: tiff.c:1721
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2694
#define FFMIN(a, b)
Definition: common.h:96
uint32_t sub_ifd
Definition: tiff.c:81
int le
Definition: tiff.c:66
int width
picture width / height.
Definition: avcodec.h:1738
uint8_t w
Definition: llviddspenc.c:38
int rps
Definition: tiff.c:84
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:336
static void tiff_set_type(TiffContext *s, enum TiffType tiff_type)
Definition: tiff.c:100
#define s(width, name)
Definition: cbs_vp9.c:257
int is_bayer
Definition: tiff.c:77
#define AV_RL32
Definition: intreadwrite.h:146
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2705
int n
Definition: avisynth_c.h:760
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:260
if(ret< 0)
Definition: vf_mcdeint.c:279
#define FF_ARRAY_ELEMS(a)
#define TIFF_GEO_KEY_UNDEFINED
Definition: tiff_data.h:47
int palette_is_set
Definition: tiff.c:65
#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:368
uint32_t palette[256]
Definition: tiff.c:64
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:121
TiffCompr
list of TIFF, TIFF/EP and DNG compression types
Definition: tiff.h:117
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:251
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
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:67
unsigned int bppcount
Definition: tiff.c:63
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
bayer, RGRG..(odd line), GBGB..(even line), 16-bit samples, big-endian */
Definition: pixfmt.h:267
main external API structure.
Definition: avcodec.h:1565
bayer, GRGR..(odd line), BGBG..(even line), 16-bit samples, big-endian */
Definition: pixfmt.h:271
Definition: tiff.h:120
void * buf
Definition: avisynth_c.h:766
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
AVCodecContext * avctx
Definition: tiff.c:54
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
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:197
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int subsampling[2]
Definition: tiff.c:70
#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:82
bayer, BGBG..(odd line), GRGR..(even line), 16-bit samples, big-endian */
Definition: pixfmt.h:265
static int get_geokey_type(int key)
Definition: tiff.c:131
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 strippos
Definition: tiff.c:86
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
int count
Definition: tiff.h:207
bayer, RGRG..(odd line), GBGB..(even line), 16-bit samples, little-endian */
Definition: pixfmt.h:266
enum TiffTags type
Definition: tiff.h:206
LZW decoding routines.
#define OFFSET(x)
Definition: tiff.c:1720
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:76
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
int stripsizesoff
Definition: tiff.c:86
Y , 8bpp.
Definition: pixfmt.h:74
uint8_t * deinvert_buf
Definition: tiff.c:89
common internal api header.
static char * get_geokey_val(int key, int val)
Definition: tiff.c:155
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:223
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
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
#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:790
void * priv_data
Definition: avcodec.h:1592
#define av_free(p)
int pixels
Definition: avisynth_c.h:390
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
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 fax_opts
Definition: tiff.c:71
static const AVClass tiff_decoder_class
Definition: tiff.c:1728
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:116
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
#define av_freep(p)
void INT64 INT64 count
Definition: avisynth_c.h:766
static int init_image(TiffContext *s, ThreadFrame *frame)
Definition: tiff.c:715
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
void INT64 start
Definition: avisynth_c.h:766
AVCodec ff_tiff_decoder
Definition: tiff.c:1735
#define av_always_inline
Definition: attributes.h:39
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:55
static int cmp_id_key(const void *id, const void *k)
Definition: tiff.c:141
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:97
planar GBRA 4:4:4:4 64bpp, little-endian
Definition: pixfmt.h:217
#define ADD_METADATA(count, name, sep)
Definition: tiff.h:74
This structure stores compressed data.
Definition: avcodec.h:1454
#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: avcodec.h:981
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:323
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:191