FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "avcodec.h"
41 #include "bytestream.h"
42 #include "faxcompr.h"
43 #include "internal.h"
44 #include "lzw.h"
45 #include "mathops.h"
46 #include "tiff.h"
47 #include "tiff_data.h"
48 #include "thread.h"
49 
50 typedef struct TiffContext {
53 
54  int width, height;
55  unsigned int bpp, bppcount;
56  uint32_t palette[256];
58  int le;
61  int planar;
62  int subsampling[2];
63  int fax_opts;
64  int predictor;
66  uint32_t res[4];
67 
68  int strips, rps, sstype;
69  int sot;
72 
76  unsigned int yuv_line_size;
77 
80 } TiffContext;
81 
82 static void free_geotags(TiffContext *const s)
83 {
84  int i;
85  for (i = 0; i < s->geotag_count; i++) {
86  if (s->geotags[i].val)
87  av_freep(&s->geotags[i].val);
88  }
89  av_freep(&s->geotags);
90  s->geotag_count = 0;
91 }
92 
93 #define RET_GEOKEY(TYPE, array, element)\
94  if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
95  key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_name_type_map))\
96  return ff_tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
97 
98 static const char *get_geokey_name(int key)
99 {
100  RET_GEOKEY(VERT, vert, name);
101  RET_GEOKEY(PROJ, proj, name);
102  RET_GEOKEY(GEOG, geog, name);
103  RET_GEOKEY(CONF, conf, name);
104 
105  return NULL;
106 }
107 
108 static int get_geokey_type(int key)
109 {
110  RET_GEOKEY(VERT, vert, type);
111  RET_GEOKEY(PROJ, proj, type);
112  RET_GEOKEY(GEOG, geog, type);
113  RET_GEOKEY(CONF, conf, type);
114 
115  return AVERROR_INVALIDDATA;
116 }
117 
118 static int cmp_id_key(const void *id, const void *k)
119 {
120  return *(const int*)id - ((const TiffGeoTagKeyName*)k)->key;
121 }
122 
123 static const char *search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
124 {
125  TiffGeoTagKeyName *r = bsearch(&id, keys, n, sizeof(keys[0]), cmp_id_key);
126  if(r)
127  return r->name;
128 
129  return NULL;
130 }
131 
132 static char *get_geokey_val(int key, int val)
133 {
134  char *ap;
135 
136  if (val == TIFF_GEO_KEY_UNDEFINED)
137  return av_strdup("undefined");
138  if (val == TIFF_GEO_KEY_USER_DEFINED)
139  return av_strdup("User-Defined");
140 
141 #define RET_GEOKEY_VAL(TYPE, array)\
142  if (val >= TIFF_##TYPE##_OFFSET &&\
143  val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_codes))\
144  return av_strdup(ff_tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]);
145 
146  switch (key) {
148  RET_GEOKEY_VAL(GT_MODEL_TYPE, gt_model_type);
149  break;
151  RET_GEOKEY_VAL(GT_RASTER_TYPE, gt_raster_type);
152  break;
156  RET_GEOKEY_VAL(LINEAR_UNIT, linear_unit);
157  break;
160  RET_GEOKEY_VAL(ANGULAR_UNIT, angular_unit);
161  break;
163  RET_GEOKEY_VAL(GCS_TYPE, gcs_type);
164  RET_GEOKEY_VAL(GCSE_TYPE, gcse_type);
165  break;
167  RET_GEOKEY_VAL(GEODETIC_DATUM, geodetic_datum);
168  RET_GEOKEY_VAL(GEODETIC_DATUM_E, geodetic_datum_e);
169  break;
171  RET_GEOKEY_VAL(ELLIPSOID, ellipsoid);
172  break;
174  RET_GEOKEY_VAL(PRIME_MERIDIAN, prime_meridian);
175  break;
178  if(ap) return ap;
179  break;
182  if(ap) return ap;
183  break;
185  RET_GEOKEY_VAL(COORD_TRANS, coord_trans);
186  break;
188  RET_GEOKEY_VAL(VERT_CS, vert_cs);
189  RET_GEOKEY_VAL(ORTHO_VERT_CS, ortho_vert_cs);
190  break;
191 
192  }
193 
194  ap = av_malloc(14);
195  if (ap)
196  snprintf(ap, 14, "Unknown-%d", val);
197  return ap;
198 }
199 
200 static char *doubles2str(double *dp, int count, const char *sep)
201 {
202  int i;
203  char *ap, *ap0;
204  uint64_t component_len;
205  if (!sep) sep = ", ";
206  component_len = 24LL + strlen(sep);
207  if (count >= (INT_MAX - 1)/component_len)
208  return NULL;
209  ap = av_malloc(component_len * count + 1);
210  if (!ap)
211  return NULL;
212  ap0 = ap;
213  ap[0] = '\0';
214  for (i = 0; i < count; i++) {
215  unsigned l = snprintf(ap, component_len, "%.15g%s", dp[i], sep);
216  if(l >= component_len) {
217  av_free(ap0);
218  return NULL;
219  }
220  ap += l;
221  }
222  ap0[strlen(ap0) - strlen(sep)] = '\0';
223  return ap0;
224 }
225 
226 static int add_metadata(int count, int type,
227  const char *name, const char *sep, TiffContext *s, AVFrame *frame)
228 {
229  switch(type) {
230  case TIFF_DOUBLE: return ff_tadd_doubles_metadata(count, name, sep, &s->gb, s->le, avpriv_frame_get_metadatap(frame));
231  case TIFF_SHORT : return ff_tadd_shorts_metadata(count, name, sep, &s->gb, s->le, 0, avpriv_frame_get_metadatap(frame));
232  case TIFF_STRING: return ff_tadd_string_metadata(count, name, &s->gb, s->le, avpriv_frame_get_metadatap(frame));
233  default : return AVERROR_INVALIDDATA;
234  };
235 }
236 
237 static void av_always_inline horizontal_fill(unsigned int bpp, uint8_t* dst,
238  int usePtr, const uint8_t *src,
239  uint8_t c, int width, int offset)
240 {
241  switch (bpp) {
242  case 1:
243  while (--width >= 0) {
244  dst[(width+offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
245  dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
246  dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
247  dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
248  dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
249  dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
250  dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
251  dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
252  }
253  break;
254  case 2:
255  while (--width >= 0) {
256  dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
257  dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
258  dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
259  dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
260  }
261  break;
262  case 4:
263  while (--width >= 0) {
264  dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
265  dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
266  }
267  break;
268  default:
269  if (usePtr) {
270  memcpy(dst + offset, src, width);
271  } else {
272  memset(dst + offset, c, width);
273  }
274  }
275 }
276 
277 static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
278 {
279  int i;
280 
282  if (!s->deinvert_buf)
283  return AVERROR(ENOMEM);
284  for (i = 0; i < size; i++)
285  s->deinvert_buf[i] = ff_reverse[src[i]];
286 
287  return 0;
288 }
289 
290 static void unpack_yuv(TiffContext *s, AVFrame *p,
291  const uint8_t *src, int lnum)
292 {
293  int i, j, k;
294  int w = (s->width - 1) / s->subsampling[0] + 1;
295  uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
296  uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
297  if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) {
298  for (i = 0; i < w; i++) {
299  for (j = 0; j < s->subsampling[1]; j++)
300  for (k = 0; k < s->subsampling[0]; k++)
301  p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
302  FFMIN(i * s->subsampling[0] + k, s->width-1)] = *src++;
303  *pu++ = *src++;
304  *pv++ = *src++;
305  }
306  }else{
307  for (i = 0; i < w; i++) {
308  for (j = 0; j < s->subsampling[1]; j++)
309  for (k = 0; k < s->subsampling[0]; k++)
310  p->data[0][(lnum + j) * p->linesize[0] +
311  i * s->subsampling[0] + k] = *src++;
312  *pu++ = *src++;
313  *pv++ = *src++;
314  }
315  }
316 }
317 
318 #if CONFIG_ZLIB
319 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
320  int size)
321 {
322  z_stream zstream = { 0 };
323  int zret;
324 
325  zstream.next_in = (uint8_t *)src;
326  zstream.avail_in = size;
327  zstream.next_out = dst;
328  zstream.avail_out = *len;
329  zret = inflateInit(&zstream);
330  if (zret != Z_OK) {
331  av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
332  return zret;
333  }
334  zret = inflate(&zstream, Z_SYNC_FLUSH);
335  inflateEnd(&zstream);
336  *len = zstream.total_out;
337  return zret == Z_STREAM_END ? Z_OK : zret;
338 }
339 
340 static int tiff_unpack_zlib(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
341  const uint8_t *src, int size, int width, int lines,
342  int strip_start, int is_yuv)
343 {
344  uint8_t *zbuf;
345  unsigned long outlen;
346  int ret, line;
347  outlen = width * lines;
348  zbuf = av_malloc(outlen);
349  if (!zbuf)
350  return AVERROR(ENOMEM);
351  if (s->fill_order) {
352  if ((ret = deinvert_buffer(s, src, size)) < 0) {
353  av_free(zbuf);
354  return ret;
355  }
356  src = s->deinvert_buf;
357  }
358  ret = tiff_uncompress(zbuf, &outlen, src, size);
359  if (ret != Z_OK) {
361  "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
362  (unsigned long)width * lines, ret);
363  av_free(zbuf);
364  return AVERROR_UNKNOWN;
365  }
366  src = zbuf;
367  for (line = 0; line < lines; line++) {
368  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
369  horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
370  } else {
371  memcpy(dst, src, width);
372  }
373  if (is_yuv) {
374  unpack_yuv(s, p, dst, strip_start + line);
375  line += s->subsampling[1] - 1;
376  }
377  dst += stride;
378  src += width;
379  }
380  av_free(zbuf);
381  return 0;
382 }
383 #endif
384 
385 #if CONFIG_LZMA
386 static int tiff_uncompress_lzma(uint8_t *dst, uint64_t *len, const uint8_t *src,
387  int size)
388 {
389  lzma_stream stream = LZMA_STREAM_INIT;
390  lzma_ret ret;
391 
392  stream.next_in = (uint8_t *)src;
393  stream.avail_in = size;
394  stream.next_out = dst;
395  stream.avail_out = *len;
396  ret = lzma_stream_decoder(&stream, UINT64_MAX, 0);
397  if (ret != LZMA_OK) {
398  av_log(NULL, AV_LOG_ERROR, "LZMA init error: %d\n", ret);
399  return ret;
400  }
401  ret = lzma_code(&stream, LZMA_RUN);
402  lzma_end(&stream);
403  *len = stream.total_out;
404  return ret == LZMA_STREAM_END ? LZMA_OK : ret;
405 }
406 
407 static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
408  const uint8_t *src, int size, int width, int lines,
409  int strip_start, int is_yuv)
410 {
411  uint64_t outlen = width * lines;
412  int ret, line;
413  uint8_t *buf = av_malloc(outlen);
414  if (!buf)
415  return AVERROR(ENOMEM);
416  if (s->fill_order) {
417  if ((ret = deinvert_buffer(s, src, size)) < 0) {
418  av_free(buf);
419  return ret;
420  }
421  src = s->deinvert_buf;
422  }
423  ret = tiff_uncompress_lzma(buf, &outlen, src, size);
424  if (ret != LZMA_OK) {
426  "Uncompressing failed (%"PRIu64" of %"PRIu64") with error %d\n", outlen,
427  (uint64_t)width * lines, ret);
428  av_free(buf);
429  return AVERROR_UNKNOWN;
430  }
431  src = buf;
432  for (line = 0; line < lines; line++) {
433  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
434  horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
435  } else {
436  memcpy(dst, src, width);
437  }
438  if (is_yuv) {
439  unpack_yuv(s, p, dst, strip_start + line);
440  line += s->subsampling[1] - 1;
441  }
442  dst += stride;
443  src += width;
444  }
445  av_free(buf);
446  return 0;
447 }
448 #endif
449 
450 static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
451  const uint8_t *src, int size, int width, int lines)
452 {
453  int i, ret = 0;
454  int line;
455  uint8_t *src2 = av_malloc((unsigned)size +
457 
458  if (!src2) {
460  "Error allocating temporary buffer\n");
461  return AVERROR(ENOMEM);
462  }
463 
464  if (!s->fill_order) {
465  memcpy(src2, src, size);
466  } else {
467  for (i = 0; i < size; i++)
468  src2[i] = ff_reverse[src[i]];
469  }
470  memset(src2 + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
471  ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
472  s->compr, s->fax_opts);
473  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
474  for (line = 0; line < lines; line++) {
475  horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
476  dst += stride;
477  }
478  av_free(src2);
479  return ret;
480 }
481 
482 static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
483  const uint8_t *src, int size, int strip_start, int lines)
484 {
485  PutByteContext pb;
486  int c, line, pixels, code, ret;
487  const uint8_t *ssrc = src;
488  int width = ((s->width * s->bpp) + 7) >> 3;
490  int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
491  (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
492  desc->nb_components >= 3;
493 
494  if (s->planar)
495  width /= s->bppcount;
496 
497  if (size <= 0)
498  return AVERROR_INVALIDDATA;
499 
500  if (is_yuv) {
501  int bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
502  s->subsampling[0] * s->subsampling[1] + 7) >> 3;
503  av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
504  if (s->yuv_line == NULL) {
505  av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
506  return AVERROR(ENOMEM);
507  }
508  dst = s->yuv_line;
509  stride = 0;
510 
511  width = (s->width - 1) / s->subsampling[0] + 1;
512  width = width * s->subsampling[0] * s->subsampling[1] + 2*width;
513  av_assert0(width <= bytes_per_row);
514  av_assert0(s->bpp == 24);
515  }
516 
517  if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
518 #if CONFIG_ZLIB
519  return tiff_unpack_zlib(s, p, dst, stride, src, size, width, lines,
520  strip_start, is_yuv);
521 #else
523  "zlib support not enabled, "
524  "deflate compression not supported\n");
525  return AVERROR(ENOSYS);
526 #endif
527  }
528  if (s->compr == TIFF_LZMA) {
529 #if CONFIG_LZMA
530  return tiff_unpack_lzma(s, p, dst, stride, src, size, width, lines,
531  strip_start, is_yuv);
532 #else
534  "LZMA support not enabled\n");
535  return AVERROR(ENOSYS);
536 #endif
537  }
538  if (s->compr == TIFF_LZW) {
539  if (s->fill_order) {
540  if ((ret = deinvert_buffer(s, src, size)) < 0)
541  return ret;
542  ssrc = src = s->deinvert_buf;
543  }
544  if (size > 1 && !src[0] && (src[1]&1)) {
545  av_log(s->avctx, AV_LOG_ERROR, "Old style LZW is unsupported\n");
546  }
547  if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
548  av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
549  return ret;
550  }
551  for (line = 0; line < lines; line++) {
552  pixels = ff_lzw_decode(s->lzw, dst, width);
553  if (pixels < width) {
554  av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
555  pixels, width);
556  return AVERROR_INVALIDDATA;
557  }
558  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
559  horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
560  if (is_yuv) {
561  unpack_yuv(s, p, dst, strip_start + line);
562  line += s->subsampling[1] - 1;
563  }
564  dst += stride;
565  }
566  return 0;
567  }
568  if (s->compr == TIFF_CCITT_RLE ||
569  s->compr == TIFF_G3 ||
570  s->compr == TIFF_G4) {
571  if (is_yuv)
572  return AVERROR_INVALIDDATA;
573 
574  return tiff_unpack_fax(s, dst, stride, src, size, width, lines);
575  }
576 
577  bytestream2_init(&s->gb, src, size);
578  bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * lines));
579 
580  for (line = 0; line < lines; line++) {
581  if (src - ssrc > size) {
582  av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
583  return AVERROR_INVALIDDATA;
584  }
585 
586  if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
587  break;
588  bytestream2_seek_p(&pb, stride * line, SEEK_SET);
589  switch (s->compr) {
590  case TIFF_RAW:
591  if (ssrc + size - src < width)
592  return AVERROR_INVALIDDATA;
593 
594  if (!s->fill_order) {
596  dst, 1, src, 0, width, 0);
597  } else {
598  int i;
599  for (i = 0; i < width; i++)
600  dst[i] = ff_reverse[src[i]];
601  }
602  src += width;
603  break;
604  case TIFF_PACKBITS:
605  for (pixels = 0; pixels < width;) {
606  if (ssrc + size - src < 2) {
607  av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
608  return AVERROR_INVALIDDATA;
609  }
610  code = s->fill_order ? (int8_t) ff_reverse[*src++]: (int8_t) *src++;
611  if (code >= 0) {
612  code++;
613  if (pixels + code > width ||
614  ssrc + size - src < code) {
616  "Copy went out of bounds\n");
617  return AVERROR_INVALIDDATA;
618  }
620  dst, 1, src, 0, code, pixels);
621  src += code;
622  pixels += code;
623  } else if (code != -128) { // -127..-1
624  code = (-code) + 1;
625  if (pixels + code > width) {
627  "Run went out of bounds\n");
628  return AVERROR_INVALIDDATA;
629  }
630  c = *src++;
632  dst, 0, NULL, c, code, pixels);
633  pixels += code;
634  }
635  }
636  if (s->fill_order) {
637  int i;
638  for (i = 0; i < width; i++)
639  dst[i] = ff_reverse[dst[i]];
640  }
641  break;
642  }
643  if (is_yuv) {
644  unpack_yuv(s, p, dst, strip_start + line);
645  line += s->subsampling[1] - 1;
646  }
647  dst += stride;
648  }
649  return 0;
650 }
651 
653 {
654  int ret;
655  int create_gray_palette = 0;
656 
657  // make sure there is no aliasing in the following switch
658  if (s->bpp >= 100 || s->bppcount >= 10) {
660  "Unsupported image parameters: bpp=%d, bppcount=%d\n",
661  s->bpp, s->bppcount);
662  return AVERROR_INVALIDDATA;
663  }
664 
665  switch (s->planar * 1000 + s->bpp * 10 + s->bppcount) {
666  case 11:
667  if (!s->palette_is_set) {
669  break;
670  }
671  case 21:
672  case 41:
674  if (!s->palette_is_set) {
675  create_gray_palette = 1;
676  }
677  break;
678  case 81:
680  break;
681  case 243:
683  if (s->subsampling[0] == 1 && s->subsampling[1] == 1) {
685  } else if (s->subsampling[0] == 2 && s->subsampling[1] == 1) {
687  } else if (s->subsampling[0] == 4 && s->subsampling[1] == 1) {
689  } else if (s->subsampling[0] == 1 && s->subsampling[1] == 2) {
691  } else if (s->subsampling[0] == 2 && s->subsampling[1] == 2) {
693  } else if (s->subsampling[0] == 4 && s->subsampling[1] == 4) {
695  } else {
696  av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr subsampling\n");
697  return AVERROR_PATCHWELCOME;
698  }
699  } else
701  break;
702  case 161:
704  break;
705  case 162:
707  break;
708  case 322:
710  break;
711  case 324:
713  break;
714  case 483:
716  break;
717  case 644:
719  break;
720  case 1243:
722  break;
723  case 1324:
725  break;
726  case 1483:
728  break;
729  case 1644:
731  break;
732  default:
734  "This format is not supported (bpp=%d, bppcount=%d)\n",
735  s->bpp, s->bppcount);
736  return AVERROR_INVALIDDATA;
737  }
738 
741  if((desc->flags & AV_PIX_FMT_FLAG_RGB) ||
742  !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
743  desc->nb_components < 3) {
744  av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr variant\n");
745  return AVERROR_INVALIDDATA;
746  }
747  }
748 
749  if (s->width != s->avctx->width || s->height != s->avctx->height) {
750  ret = ff_set_dimensions(s->avctx, s->width, s->height);
751  if (ret < 0)
752  return ret;
753  }
754  if ((ret = ff_thread_get_buffer(s->avctx, frame, 0)) < 0)
755  return ret;
756  if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
757  if (!create_gray_palette)
758  memcpy(frame->f->data[1], s->palette, sizeof(s->palette));
759  else {
760  /* make default grayscale pal */
761  int i;
762  uint32_t *pal = (uint32_t *)frame->f->data[1];
763  for (i = 0; i < 1<<s->bpp; i++)
764  pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
765  }
766  }
767  return 0;
768 }
769 
770 static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
771 {
772  int offset = tag == TIFF_YRES ? 2 : 0;
773  s->res[offset++] = num;
774  s->res[offset] = den;
775  if (s->res[0] && s->res[1] && s->res[2] && s->res[3])
777  s->res[2] * (uint64_t)s->res[1], s->res[0] * (uint64_t)s->res[3], INT32_MAX);
778 }
779 
781 {
782  unsigned tag, type, count, off, value = 0, value2 = 0;
783  int i, start;
784  int pos;
785  int ret;
786  double *dp;
787 
788  ret = ff_tread_tag(&s->gb, s->le, &tag, &type, &count, &start);
789  if (ret < 0) {
790  goto end;
791  }
792 
793  off = bytestream2_tell(&s->gb);
794  if (count == 1) {
795  switch (type) {
796  case TIFF_BYTE:
797  case TIFF_SHORT:
798  case TIFF_LONG:
799  value = ff_tget(&s->gb, type, s->le);
800  break;
801  case TIFF_RATIONAL:
802  value = ff_tget(&s->gb, TIFF_LONG, s->le);
803  value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
804  break;
805  case TIFF_STRING:
806  if (count <= 4) {
807  break;
808  }
809  default:
810  value = UINT_MAX;
811  }
812  }
813 
814  switch (tag) {
815  case TIFF_WIDTH:
816  s->width = value;
817  break;
818  case TIFF_HEIGHT:
819  s->height = value;
820  break;
821  case TIFF_BPP:
822  if (count > 4U) {
824  "This format is not supported (bpp=%d, %d components)\n",
825  value, count);
826  return AVERROR_INVALIDDATA;
827  }
828  s->bppcount = count;
829  if (count == 1)
830  s->bpp = value;
831  else {
832  switch (type) {
833  case TIFF_BYTE:
834  case TIFF_SHORT:
835  case TIFF_LONG:
836  s->bpp = 0;
837  if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
838  return AVERROR_INVALIDDATA;
839  for (i = 0; i < count; i++)
840  s->bpp += ff_tget(&s->gb, type, s->le);
841  break;
842  default:
843  s->bpp = -1;
844  }
845  }
846  break;
848  if (count != 1) {
850  "Samples per pixel requires a single value, many provided\n");
851  return AVERROR_INVALIDDATA;
852  }
853  if (value > 4U) {
855  "Samples per pixel %d is too large\n", value);
856  return AVERROR_INVALIDDATA;
857  }
858  if (s->bppcount == 1)
859  s->bpp *= value;
860  s->bppcount = value;
861  break;
862  case TIFF_COMPR:
863  s->compr = value;
864  av_log(s->avctx, AV_LOG_DEBUG, "compression: %d\n", s->compr);
865  s->predictor = 0;
866  switch (s->compr) {
867  case TIFF_RAW:
868  case TIFF_PACKBITS:
869  case TIFF_LZW:
870  case TIFF_CCITT_RLE:
871  break;
872  case TIFF_G3:
873  case TIFF_G4:
874  s->fax_opts = 0;
875  break;
876  case TIFF_DEFLATE:
877  case TIFF_ADOBE_DEFLATE:
878 #if CONFIG_ZLIB
879  break;
880 #else
881  av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
882  return AVERROR(ENOSYS);
883 #endif
884  case TIFF_JPEG:
885  case TIFF_NEWJPEG:
886  avpriv_report_missing_feature(s->avctx, "JPEG compression");
887  return AVERROR_PATCHWELCOME;
888  case TIFF_LZMA:
889 #if CONFIG_LZMA
890  break;
891 #else
892  av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n");
893  return AVERROR(ENOSYS);
894 #endif
895  default:
896  av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
897  s->compr);
898  return AVERROR_INVALIDDATA;
899  }
900  break;
901  case TIFF_ROWSPERSTRIP:
902  if (!value || (type == TIFF_LONG && value == UINT_MAX))
903  value = s->height;
904  s->rps = FFMIN(value, s->height);
905  break;
906  case TIFF_STRIP_OFFS:
907  if (count == 1) {
908  s->strippos = 0;
909  s->stripoff = value;
910  } else
911  s->strippos = off;
912  s->strips = count;
913  if (s->strips == 1)
914  s->rps = s->height;
915  s->sot = type;
916  break;
917  case TIFF_STRIP_SIZE:
918  if (count == 1) {
919  s->stripsizesoff = 0;
920  s->stripsize = value;
921  s->strips = 1;
922  } else {
923  s->stripsizesoff = off;
924  }
925  s->strips = count;
926  s->sstype = type;
927  break;
928  case TIFF_XRES:
929  case TIFF_YRES:
930  set_sar(s, tag, value, value2);
931  break;
933  case TIFF_TILE_LENGTH:
934  case TIFF_TILE_OFFSETS:
935  case TIFF_TILE_WIDTH:
936  av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");
937  return AVERROR_PATCHWELCOME;
938  break;
939  case TIFF_PREDICTOR:
940  s->predictor = value;
941  break;
942  case TIFF_PHOTOMETRIC:
943  switch (value) {
949  s->photometric = value;
950  break;
961  "PhotometricInterpretation 0x%04X",
962  value);
963  return AVERROR_PATCHWELCOME;
964  default:
965  av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
966  "unknown\n", value);
967  return AVERROR_INVALIDDATA;
968  }
969  break;
970  case TIFF_FILL_ORDER:
971  if (value < 1 || value > 2) {
973  "Unknown FillOrder value %d, trying default one\n", value);
974  value = 1;
975  }
976  s->fill_order = value - 1;
977  break;
978  case TIFF_PAL: {
979  GetByteContext pal_gb[3];
980  off = type_sizes[type];
981  if (count / 3 > 256 ||
982  bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
983  return AVERROR_INVALIDDATA;
984 
985  pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
986  bytestream2_skip(&pal_gb[1], count / 3 * off);
987  bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
988 
989  off = (type_sizes[type] - 1) << 3;
990  for (i = 0; i < count / 3; i++) {
991  uint32_t p = 0xFF000000;
992  p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16;
993  p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8;
994  p |= ff_tget(&pal_gb[2], type, s->le) >> off;
995  s->palette[i] = p;
996  }
997  s->palette_is_set = 1;
998  break;
999  }
1000  case TIFF_PLANAR:
1001  s->planar = value == 2;
1002  break;
1004  if (count != 2) {
1005  av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n");
1006  return AVERROR_INVALIDDATA;
1007  }
1008  for (i = 0; i < count; i++) {
1009  s->subsampling[i] = ff_tget(&s->gb, type, s->le);
1010  if (s->subsampling[i] <= 0) {
1011  av_log(s->avctx, AV_LOG_ERROR, "subsampling %d is invalid\n", s->subsampling[i]);
1012  return AVERROR_INVALIDDATA;
1013  }
1014  }
1015  break;
1016  case TIFF_T4OPTIONS:
1017  if (s->compr == TIFF_G3)
1018  s->fax_opts = value;
1019  break;
1020  case TIFF_T6OPTIONS:
1021  if (s->compr == TIFF_G4)
1022  s->fax_opts = value;
1023  break;
1024 #define ADD_METADATA(count, name, sep)\
1025  if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1026  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1027  goto end;\
1028  }
1030  ADD_METADATA(count, "ModelPixelScaleTag", NULL);
1031  break;
1033  ADD_METADATA(count, "ModelTransformationTag", NULL);
1034  break;
1035  case TIFF_MODEL_TIEPOINT:
1036  ADD_METADATA(count, "ModelTiepointTag", NULL);
1037  break;
1039  ADD_METADATA(1, "GeoTIFF_Version", NULL);
1040  ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
1041  s->geotag_count = ff_tget_short(&s->gb, s->le);
1042  if (s->geotag_count > count / 4 - 1) {
1043  s->geotag_count = count / 4 - 1;
1044  av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
1045  }
1046  if (bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4) {
1047  s->geotag_count = 0;
1048  return -1;
1049  }
1050  s->geotags = av_mallocz_array(s->geotag_count, sizeof(TiffGeoTag));
1051  if (!s->geotags) {
1052  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1053  s->geotag_count = 0;
1054  goto end;
1055  }
1056  for (i = 0; i < s->geotag_count; i++) {
1057  s->geotags[i].key = ff_tget_short(&s->gb, s->le);
1058  s->geotags[i].type = ff_tget_short(&s->gb, s->le);
1059  s->geotags[i].count = ff_tget_short(&s->gb, s->le);
1060 
1061  if (!s->geotags[i].type)
1062  s->geotags[i].val = get_geokey_val(s->geotags[i].key, ff_tget_short(&s->gb, s->le));
1063  else
1064  s->geotags[i].offset = ff_tget_short(&s->gb, s->le);
1065  }
1066  break;
1068  if (count >= INT_MAX / sizeof(int64_t))
1069  return AVERROR_INVALIDDATA;
1070  if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
1071  return AVERROR_INVALIDDATA;
1072  dp = av_malloc_array(count, sizeof(double));
1073  if (!dp) {
1074  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1075  goto end;
1076  }
1077  for (i = 0; i < count; i++)
1078  dp[i] = ff_tget_double(&s->gb, s->le);
1079  for (i = 0; i < s->geotag_count; i++) {
1080  if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
1081  if (s->geotags[i].count == 0
1082  || s->geotags[i].offset + s->geotags[i].count > count) {
1083  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1084  } else {
1085  char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
1086  if (!ap) {
1087  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1088  av_freep(&dp);
1089  return AVERROR(ENOMEM);
1090  }
1091  s->geotags[i].val = ap;
1092  }
1093  }
1094  }
1095  av_freep(&dp);
1096  break;
1097  case TIFF_GEO_ASCII_PARAMS:
1098  pos = bytestream2_tell(&s->gb);
1099  for (i = 0; i < s->geotag_count; i++) {
1100  if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
1101  if (s->geotags[i].count == 0
1102  || s->geotags[i].offset + s->geotags[i].count > count) {
1103  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1104  } else {
1105  char *ap;
1106 
1107  bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
1108  if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
1109  return AVERROR_INVALIDDATA;
1110  ap = av_malloc(s->geotags[i].count);
1111  if (!ap) {
1112  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1113  return AVERROR(ENOMEM);
1114  }
1115  bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
1116  ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
1117  s->geotags[i].val = ap;
1118  }
1119  }
1120  }
1121  break;
1122  case TIFF_ARTIST:
1123  ADD_METADATA(count, "artist", NULL);
1124  break;
1125  case TIFF_COPYRIGHT:
1126  ADD_METADATA(count, "copyright", NULL);
1127  break;
1128  case TIFF_DATE:
1129  ADD_METADATA(count, "date", NULL);
1130  break;
1131  case TIFF_DOCUMENT_NAME:
1132  ADD_METADATA(count, "document_name", NULL);
1133  break;
1134  case TIFF_HOST_COMPUTER:
1135  ADD_METADATA(count, "computer", NULL);
1136  break;
1138  ADD_METADATA(count, "description", NULL);
1139  break;
1140  case TIFF_MAKE:
1141  ADD_METADATA(count, "make", NULL);
1142  break;
1143  case TIFF_MODEL:
1144  ADD_METADATA(count, "model", NULL);
1145  break;
1146  case TIFF_PAGE_NAME:
1147  ADD_METADATA(count, "page_name", NULL);
1148  break;
1149  case TIFF_PAGE_NUMBER:
1150  ADD_METADATA(count, "page_number", " / ");
1151  break;
1152  case TIFF_SOFTWARE_NAME:
1153  ADD_METADATA(count, "software", NULL);
1154  break;
1155  default:
1156  if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1158  "Unknown or unsupported tag %d/0X%0X\n",
1159  tag, tag);
1160  return AVERROR_INVALIDDATA;
1161  }
1162  }
1163 end:
1164  if (s->bpp > 64U) {
1166  "This format is not supported (bpp=%d, %d components)\n",
1167  s->bpp, count);
1168  s->bpp = 0;
1169  return AVERROR_INVALIDDATA;
1170  }
1171  bytestream2_seek(&s->gb, start, SEEK_SET);
1172  return 0;
1173 }
1174 
1175 static int decode_frame(AVCodecContext *avctx,
1176  void *data, int *got_frame, AVPacket *avpkt)
1177 {
1178  TiffContext *const s = avctx->priv_data;
1179  AVFrame *const p = data;
1180  ThreadFrame frame = { .f = data };
1181  unsigned off;
1182  int le, ret, plane, planes;
1183  int i, j, entries, stride;
1184  unsigned soff, ssize;
1185  uint8_t *dst;
1186  GetByteContext stripsizes;
1187  GetByteContext stripdata;
1188 
1189  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1190 
1191  // parse image header
1192  if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
1193  av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
1194  return ret;
1195  } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1196  av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1197  return AVERROR_INVALIDDATA;
1198  }
1199  s->le = le;
1200  // TIFF_BPP is not a required tag and defaults to 1
1201  s->bppcount = s->bpp = 1;
1203  s->compr = TIFF_RAW;
1204  s->fill_order = 0;
1205  free_geotags(s);
1206 
1207  // Reset these offsets so we can tell if they were set this frame
1208  s->stripsizesoff = s->strippos = 0;
1209  /* parse image file directory */
1210  bytestream2_seek(&s->gb, off, SEEK_SET);
1211  entries = ff_tget_short(&s->gb, le);
1212  if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
1213  return AVERROR_INVALIDDATA;
1214  for (i = 0; i < entries; i++) {
1215  if ((ret = tiff_decode_tag(s, p)) < 0)
1216  return ret;
1217  }
1218 
1219  for (i = 0; i<s->geotag_count; i++) {
1220  const char *keyname = get_geokey_name(s->geotags[i].key);
1221  if (!keyname) {
1222  av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
1223  continue;
1224  }
1225  if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
1226  av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
1227  continue;
1228  }
1229  ret = av_dict_set(avpriv_frame_get_metadatap(p), keyname, s->geotags[i].val, 0);
1230  if (ret<0) {
1231  av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
1232  return ret;
1233  }
1234  }
1235 
1236  if (!s->strippos && !s->stripoff) {
1237  av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
1238  return AVERROR_INVALIDDATA;
1239  }
1240  /* now we have the data and may start decoding */
1241  if ((ret = init_image(s, &frame)) < 0)
1242  return ret;
1243 
1244  if (s->strips == 1 && !s->stripsize) {
1245  av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
1246  s->stripsize = avpkt->size - s->stripoff;
1247  }
1248 
1249  if (s->stripsizesoff) {
1250  if (s->stripsizesoff >= (unsigned)avpkt->size)
1251  return AVERROR_INVALIDDATA;
1252  bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
1253  avpkt->size - s->stripsizesoff);
1254  }
1255  if (s->strippos) {
1256  if (s->strippos >= (unsigned)avpkt->size)
1257  return AVERROR_INVALIDDATA;
1258  bytestream2_init(&stripdata, avpkt->data + s->strippos,
1259  avpkt->size - s->strippos);
1260  }
1261 
1262  if (s->rps <= 0 || s->rps % s->subsampling[1]) {
1263  av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
1264  return AVERROR_INVALIDDATA;
1265  }
1266 
1267  planes = s->planar ? s->bppcount : 1;
1268  for (plane = 0; plane < planes; plane++) {
1269  stride = p->linesize[plane];
1270  dst = p->data[plane];
1271  for (i = 0; i < s->height; i += s->rps) {
1272  if (s->stripsizesoff)
1273  ssize = ff_tget(&stripsizes, s->sstype, le);
1274  else
1275  ssize = s->stripsize;
1276 
1277  if (s->strippos)
1278  soff = ff_tget(&stripdata, s->sot, le);
1279  else
1280  soff = s->stripoff;
1281 
1282  if (soff > avpkt->size || ssize > avpkt->size - soff) {
1283  av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
1284  return AVERROR_INVALIDDATA;
1285  }
1286  if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,
1287  FFMIN(s->rps, s->height - i))) < 0) {
1288  if (avctx->err_recognition & AV_EF_EXPLODE)
1289  return ret;
1290  break;
1291  }
1292  dst += s->rps * stride;
1293  }
1294  if (s->predictor == 2) {
1295  if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1296  av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");
1297  return AVERROR_PATCHWELCOME;
1298  }
1299  dst = p->data[plane];
1300  soff = s->bpp >> 3;
1301  if (s->planar)
1302  soff = FFMAX(soff / s->bppcount, 1);
1303  ssize = s->width * soff;
1304  if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
1307  s->avctx->pix_fmt == AV_PIX_FMT_YA16LE ||
1310  for (i = 0; i < s->height; i++) {
1311  for (j = soff; j < ssize; j += 2)
1312  AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
1313  dst += stride;
1314  }
1315  } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1318  s->avctx->pix_fmt == AV_PIX_FMT_YA16BE ||
1321  for (i = 0; i < s->height; i++) {
1322  for (j = soff; j < ssize; j += 2)
1323  AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
1324  dst += stride;
1325  }
1326  } else {
1327  for (i = 0; i < s->height; i++) {
1328  for (j = soff; j < ssize; j++)
1329  dst[j] += dst[j - soff];
1330  dst += stride;
1331  }
1332  }
1333  }
1334 
1336  dst = p->data[plane];
1337  for (i = 0; i < s->height; i++) {
1338  for (j = 0; j < stride; j++)
1339  dst[j] = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];
1340  dst += stride;
1341  }
1342  }
1343  }
1344 
1345  if (s->planar && s->bppcount > 2) {
1346  FFSWAP(uint8_t*, p->data[0], p->data[2]);
1347  FFSWAP(int, p->linesize[0], p->linesize[2]);
1348  FFSWAP(uint8_t*, p->data[0], p->data[1]);
1349  FFSWAP(int, p->linesize[0], p->linesize[1]);
1350  }
1351 
1352  *got_frame = 1;
1353 
1354  return avpkt->size;
1355 }
1356 
1358 {
1359  TiffContext *s = avctx->priv_data;
1360 
1361  s->width = 0;
1362  s->height = 0;
1363  s->subsampling[0] =
1364  s->subsampling[1] = 1;
1365  s->avctx = avctx;
1366  ff_lzw_decode_open(&s->lzw);
1368 
1369  return 0;
1370 }
1371 
1372 static av_cold int tiff_end(AVCodecContext *avctx)
1373 {
1374  TiffContext *const s = avctx->priv_data;
1375 
1376  free_geotags(s);
1377 
1378  ff_lzw_decode_close(&s->lzw);
1379  av_freep(&s->deinvert_buf);
1380  return 0;
1381 }
1382 
1384  .name = "tiff",
1385  .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
1386  .type = AVMEDIA_TYPE_VIDEO,
1387  .id = AV_CODEC_ID_TIFF,
1388  .priv_data_size = sizeof(TiffContext),
1389  .init = tiff_init,
1390  .close = tiff_end,
1391  .decode = decode_frame,
1393  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1394 };
Definition: tiff.h:54
int plane
Definition: avisynth_c.h:422
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:166
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
Definition: tiff.h:89
int offset
Definition: tiff.h:178
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
const TiffGeoTagKeyName ff_tiff_projection_codes[]
Definition: tiff_data.c:1497
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2266
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
TiffPhotometric
Definition: tiff.h:150
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int fill_order
Definition: tiff.c:65
unsigned int bpp
Definition: tiff.c:55
8 bits gray, 8 bits alpha
Definition: pixfmt.h:154
int geotag_count
Definition: tiff.c:78
uint32_t res[4]
Definition: tiff.c:66
Definition: tiff.h:53
int sstype
Definition: tiff.c:68
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
misc image utilities
#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:36
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:64
Definition: tiff.h:99
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:210
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 *const name
Definition: tiff.h:184
const char * desc
Definition: nvenc.c:101
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
Definition: tiff.h:47
TIFF tables.
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:180
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1602
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:143
av_cold void ff_lzw_decode_close(LZWState **p)
Definition: lzw.c:115
static const char * search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
Definition: tiff.c:123
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:2087
av_cold void ff_lzw_decode_open(LZWState **p)
Definition: lzw.c:110
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1904
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
static void free_geotags(TiffContext *const s)
Definition: tiff.c:82
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:120
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
unsigned int yuv_line_size
Definition: tiff.c:76
AVCodec.
Definition: avcodec.h:3600
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
Definition: tiff.h:93
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:273
Definition: tiff.h:92
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: tiff.c:1175
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:482
static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
Definition: tiff.c:780
static const uint8_t type_sizes[14]
sizes of various TIFF field types (string size = 100)
Definition: tiff_common.h:54
int deinvert_buf_size
Definition: tiff.c:74
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:230
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
char * val
Definition: tiff.h:179
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
#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:111
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
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:217
static av_cold int tiff_init(AVCodecContext *avctx)
Definition: tiff.c:1357
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1601
planar GBR 4:4:4 48bpp, big-endian
Definition: pixfmt.h:186
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord)
Definition: vf_neighbor.c:129
uint32_t tag
Definition: movenc.c:1382
static int add_metadata(int count, int type, const char *name, const char *sep, TiffContext *s, AVFrame *frame)
Definition: tiff.c:226
int stripoff
Definition: tiff.c:70
Definition: tiff.h:94
ptrdiff_t size
Definition: opengl_enc.c:101
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:71
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
#define av_log(a,...)
Definition: tiff.h:68
int planar
Definition: tiff.c:61
#define U(x)
Definition: vp56_arith.h:37
Definition: lzw.c:46
int height
Definition: tiff.c:54
#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:226
enum TiffGeoTagKey key
Definition: tiff.h:175
int sot
Definition: tiff.c:69
#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:176
const uint8_t ff_reverse[256]
Definition: reverse.c:23
static av_cold int tiff_end(AVCodecContext *avctx)
Definition: tiff.c:1372
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:111
static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
Definition: tiff.c:277
#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
const char * name
Name of the codec implementation.
Definition: avcodec.h:3607
static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
Definition: tiff.c:770
int width
Definition: tiff.c:54
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:450
int strips
Definition: tiff.c:68
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
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
uint8_t * yuv_line
Definition: tiff.c:75
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:226
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1022
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
int predictor
Definition: tiff.c:64
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:215
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
enum TiffPhotometric photometric
Definition: tiff.c:60
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:70
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2964
#define FFMIN(a, b)
Definition: common.h:96
int le
Definition: tiff.c:58
#define width
int width
picture width / height.
Definition: avcodec.h:1863
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
int rps
Definition: tiff.c:68
static void unpack_yuv(TiffContext *s, AVFrame *p, const uint8_t *src, int lnum)
Definition: tiff.c:290
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2975
int n
Definition: avisynth_c.h:684
#define src
Definition: vp9dsp.c:530
#define FF_ARRAY_ELEMS(a)
uint8_t le
Definition: crc.c:295
#define TIFF_GEO_KEY_UNDEFINED
Definition: tiff_data.h:47
int palette_is_set
Definition: tiff.c:57
#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:248
uint32_t palette[256]
Definition: tiff.c:56
Definition: tiff.h:41
static const char * get_geokey_name(int key)
Definition: tiff.c:98
TiffCompr
list of TIFF compression types
Definition: tiff.h:88
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:267
Libavcodec external API header.
AVDictionary ** avpriv_frame_get_metadatap(AVFrame *frame)
Definition: frame.c:46
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
enum TiffCompr compr
Definition: tiff.c:59
unsigned int bppcount
Definition: tiff.c:55
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.
main external API structure.
Definition: avcodec.h:1676
Definition: tiff.h:91
void * buf
Definition: avisynth_c.h:690
GLint GLenum type
Definition: opengl_enc.c:105
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:51
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:68
Y , 16bpp, big-endian.
Definition: pixfmt.h:98
int subsampling[2]
Definition: tiff.c:62
#define snprintf
Definition: snprintf.h:34
static int get_geokey_type(int key)
Definition: tiff.c:108
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:70
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:198
int count
Definition: tiff.h:177
enum TiffTags type
Definition: tiff.h:176
LZW decoding routines.
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:72
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
int stripsizesoff
Definition: tiff.c:70
Y , 8bpp.
Definition: pixfmt.h:70
uint8_t * deinvert_buf
Definition: tiff.c:73
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:282
static char * get_geokey_val(int key, int val)
Definition: tiff.c:132
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:229
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:110
static void av_always_inline horizontal_fill(unsigned int bpp, uint8_t *dst, int usePtr, const uint8_t *src, uint8_t c, int width, int offset)
Definition: tiff.c:237
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
static char * doubles2str(double *dp, int count, const char *sep)
Definition: tiff.c:200
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:69
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:128
#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:734
void * priv_data
Definition: avcodec.h:1718
#define av_free(p)
int pixels
Definition: avisynth_c.h:429
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
Y , 16bpp, little-endian.
Definition: pixfmt.h:99
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:227
int fax_opts
Definition: tiff.c:63
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:93
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)
static int init_image(TiffContext *s, ThreadFrame *frame)
Definition: tiff.c:652
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
void INT64 start
Definition: avisynth_c.h:690
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2035
AVCodec ff_tiff_decoder
Definition: tiff.c:1383
#define av_always_inline
Definition: attributes.h:39
planar GBR 4:4:4 48bpp, little-endian
Definition: pixfmt.h:187
#define av_malloc_array(a, b)
#define FFSWAP(type, a, b)
Definition: common.h:99
GetByteContext gb
Definition: tiff.c:52
#define stride
static int cmp_id_key(const void *id, const void *k)
Definition: tiff.c:118
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:79
planar GBRA 4:4:4:4 64bpp, little-endian
Definition: pixfmt.h:231
#define ADD_METADATA(count, name, sep)
Definition: tiff.h:64
This structure stores compressed data.
Definition: avcodec.h:1578
#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:959
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:218
const char * name
Definition: opengl_enc.c:103