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  s->predictor = 0;
865  switch (s->compr) {
866  case TIFF_RAW:
867  case TIFF_PACKBITS:
868  case TIFF_LZW:
869  case TIFF_CCITT_RLE:
870  break;
871  case TIFF_G3:
872  case TIFF_G4:
873  s->fax_opts = 0;
874  break;
875  case TIFF_DEFLATE:
876  case TIFF_ADOBE_DEFLATE:
877 #if CONFIG_ZLIB
878  break;
879 #else
880  av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
881  return AVERROR(ENOSYS);
882 #endif
883  case TIFF_JPEG:
884  case TIFF_NEWJPEG:
885  avpriv_report_missing_feature(s->avctx, "JPEG compression");
886  return AVERROR_PATCHWELCOME;
887  case TIFF_LZMA:
888 #if CONFIG_LZMA
889  break;
890 #else
891  av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n");
892  return AVERROR(ENOSYS);
893 #endif
894  default:
895  av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
896  s->compr);
897  return AVERROR_INVALIDDATA;
898  }
899  break;
900  case TIFF_ROWSPERSTRIP:
901  if (!value || (type == TIFF_LONG && value == UINT_MAX))
902  value = s->height;
903  s->rps = FFMIN(value, s->height);
904  break;
905  case TIFF_STRIP_OFFS:
906  if (count == 1) {
907  s->strippos = 0;
908  s->stripoff = value;
909  } else
910  s->strippos = off;
911  s->strips = count;
912  if (s->strips == 1)
913  s->rps = s->height;
914  s->sot = type;
915  break;
916  case TIFF_STRIP_SIZE:
917  if (count == 1) {
918  s->stripsizesoff = 0;
919  s->stripsize = value;
920  s->strips = 1;
921  } else {
922  s->stripsizesoff = off;
923  }
924  s->strips = count;
925  s->sstype = type;
926  break;
927  case TIFF_XRES:
928  case TIFF_YRES:
929  set_sar(s, tag, value, value2);
930  break;
932  case TIFF_TILE_LENGTH:
933  case TIFF_TILE_OFFSETS:
934  case TIFF_TILE_WIDTH:
935  av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");
936  return AVERROR_PATCHWELCOME;
937  break;
938  case TIFF_PREDICTOR:
939  s->predictor = value;
940  break;
941  case TIFF_PHOTOMETRIC:
942  switch (value) {
948  s->photometric = value;
949  break;
960  "PhotometricInterpretation 0x%04X",
961  value);
962  return AVERROR_PATCHWELCOME;
963  default:
964  av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
965  "unknown\n", value);
966  return AVERROR_INVALIDDATA;
967  }
968  break;
969  case TIFF_FILL_ORDER:
970  if (value < 1 || value > 2) {
972  "Unknown FillOrder value %d, trying default one\n", value);
973  value = 1;
974  }
975  s->fill_order = value - 1;
976  break;
977  case TIFF_PAL: {
978  GetByteContext pal_gb[3];
979  off = type_sizes[type];
980  if (count / 3 > 256 ||
981  bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
982  return AVERROR_INVALIDDATA;
983 
984  pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
985  bytestream2_skip(&pal_gb[1], count / 3 * off);
986  bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
987 
988  off = (type_sizes[type] - 1) << 3;
989  for (i = 0; i < count / 3; i++) {
990  uint32_t p = 0xFF000000;
991  p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16;
992  p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8;
993  p |= ff_tget(&pal_gb[2], type, s->le) >> off;
994  s->palette[i] = p;
995  }
996  s->palette_is_set = 1;
997  break;
998  }
999  case TIFF_PLANAR:
1000  s->planar = value == 2;
1001  break;
1003  if (count != 2) {
1004  av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n");
1005  return AVERROR_INVALIDDATA;
1006  }
1007  for (i = 0; i < count; i++) {
1008  s->subsampling[i] = ff_tget(&s->gb, type, s->le);
1009  if (s->subsampling[i] <= 0) {
1010  av_log(s->avctx, AV_LOG_ERROR, "subsampling %d is invalid\n", s->subsampling[i]);
1011  return AVERROR_INVALIDDATA;
1012  }
1013  }
1014  break;
1015  case TIFF_T4OPTIONS:
1016  if (s->compr == TIFF_G3)
1017  s->fax_opts = value;
1018  break;
1019  case TIFF_T6OPTIONS:
1020  if (s->compr == TIFF_G4)
1021  s->fax_opts = value;
1022  break;
1023 #define ADD_METADATA(count, name, sep)\
1024  if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1025  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1026  goto end;\
1027  }
1029  ADD_METADATA(count, "ModelPixelScaleTag", NULL);
1030  break;
1032  ADD_METADATA(count, "ModelTransformationTag", NULL);
1033  break;
1034  case TIFF_MODEL_TIEPOINT:
1035  ADD_METADATA(count, "ModelTiepointTag", NULL);
1036  break;
1038  ADD_METADATA(1, "GeoTIFF_Version", NULL);
1039  ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
1040  s->geotag_count = ff_tget_short(&s->gb, s->le);
1041  if (s->geotag_count > count / 4 - 1) {
1042  s->geotag_count = count / 4 - 1;
1043  av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
1044  }
1045  if (bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4) {
1046  s->geotag_count = 0;
1047  return -1;
1048  }
1049  s->geotags = av_mallocz_array(s->geotag_count, sizeof(TiffGeoTag));
1050  if (!s->geotags) {
1051  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1052  s->geotag_count = 0;
1053  goto end;
1054  }
1055  for (i = 0; i < s->geotag_count; i++) {
1056  s->geotags[i].key = ff_tget_short(&s->gb, s->le);
1057  s->geotags[i].type = ff_tget_short(&s->gb, s->le);
1058  s->geotags[i].count = ff_tget_short(&s->gb, s->le);
1059 
1060  if (!s->geotags[i].type)
1061  s->geotags[i].val = get_geokey_val(s->geotags[i].key, ff_tget_short(&s->gb, s->le));
1062  else
1063  s->geotags[i].offset = ff_tget_short(&s->gb, s->le);
1064  }
1065  break;
1067  if (count >= INT_MAX / sizeof(int64_t))
1068  return AVERROR_INVALIDDATA;
1069  if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
1070  return AVERROR_INVALIDDATA;
1071  dp = av_malloc_array(count, sizeof(double));
1072  if (!dp) {
1073  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1074  goto end;
1075  }
1076  for (i = 0; i < count; i++)
1077  dp[i] = ff_tget_double(&s->gb, s->le);
1078  for (i = 0; i < s->geotag_count; i++) {
1079  if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
1080  if (s->geotags[i].count == 0
1081  || s->geotags[i].offset + s->geotags[i].count > count) {
1082  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1083  } else {
1084  char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
1085  if (!ap) {
1086  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1087  av_freep(&dp);
1088  return AVERROR(ENOMEM);
1089  }
1090  s->geotags[i].val = ap;
1091  }
1092  }
1093  }
1094  av_freep(&dp);
1095  break;
1096  case TIFF_GEO_ASCII_PARAMS:
1097  pos = bytestream2_tell(&s->gb);
1098  for (i = 0; i < s->geotag_count; i++) {
1099  if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
1100  if (s->geotags[i].count == 0
1101  || s->geotags[i].offset + s->geotags[i].count > count) {
1102  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1103  } else {
1104  char *ap;
1105 
1106  bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
1107  if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
1108  return AVERROR_INVALIDDATA;
1109  ap = av_malloc(s->geotags[i].count);
1110  if (!ap) {
1111  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1112  return AVERROR(ENOMEM);
1113  }
1114  bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
1115  ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
1116  s->geotags[i].val = ap;
1117  }
1118  }
1119  }
1120  break;
1121  case TIFF_ARTIST:
1122  ADD_METADATA(count, "artist", NULL);
1123  break;
1124  case TIFF_COPYRIGHT:
1125  ADD_METADATA(count, "copyright", NULL);
1126  break;
1127  case TIFF_DATE:
1128  ADD_METADATA(count, "date", NULL);
1129  break;
1130  case TIFF_DOCUMENT_NAME:
1131  ADD_METADATA(count, "document_name", NULL);
1132  break;
1133  case TIFF_HOST_COMPUTER:
1134  ADD_METADATA(count, "computer", NULL);
1135  break;
1137  ADD_METADATA(count, "description", NULL);
1138  break;
1139  case TIFF_MAKE:
1140  ADD_METADATA(count, "make", NULL);
1141  break;
1142  case TIFF_MODEL:
1143  ADD_METADATA(count, "model", NULL);
1144  break;
1145  case TIFF_PAGE_NAME:
1146  ADD_METADATA(count, "page_name", NULL);
1147  break;
1148  case TIFF_PAGE_NUMBER:
1149  ADD_METADATA(count, "page_number", " / ");
1150  break;
1151  case TIFF_SOFTWARE_NAME:
1152  ADD_METADATA(count, "software", NULL);
1153  break;
1154  default:
1155  if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1157  "Unknown or unsupported tag %d/0X%0X\n",
1158  tag, tag);
1159  return AVERROR_INVALIDDATA;
1160  }
1161  }
1162 end:
1163  if (s->bpp > 64U) {
1165  "This format is not supported (bpp=%d, %d components)\n",
1166  s->bpp, count);
1167  s->bpp = 0;
1168  return AVERROR_INVALIDDATA;
1169  }
1170  bytestream2_seek(&s->gb, start, SEEK_SET);
1171  return 0;
1172 }
1173 
1174 static int decode_frame(AVCodecContext *avctx,
1175  void *data, int *got_frame, AVPacket *avpkt)
1176 {
1177  TiffContext *const s = avctx->priv_data;
1178  AVFrame *const p = data;
1179  ThreadFrame frame = { .f = data };
1180  unsigned off;
1181  int le, ret, plane, planes;
1182  int i, j, entries, stride;
1183  unsigned soff, ssize;
1184  uint8_t *dst;
1185  GetByteContext stripsizes;
1186  GetByteContext stripdata;
1187 
1188  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1189 
1190  // parse image header
1191  if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
1192  av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
1193  return ret;
1194  } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1195  av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1196  return AVERROR_INVALIDDATA;
1197  }
1198  s->le = le;
1199  // TIFF_BPP is not a required tag and defaults to 1
1200  s->bppcount = s->bpp = 1;
1202  s->compr = TIFF_RAW;
1203  s->fill_order = 0;
1204  free_geotags(s);
1205 
1206  // Reset these offsets so we can tell if they were set this frame
1207  s->stripsizesoff = s->strippos = 0;
1208  /* parse image file directory */
1209  bytestream2_seek(&s->gb, off, SEEK_SET);
1210  entries = ff_tget_short(&s->gb, le);
1211  if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
1212  return AVERROR_INVALIDDATA;
1213  for (i = 0; i < entries; i++) {
1214  if ((ret = tiff_decode_tag(s, p)) < 0)
1215  return ret;
1216  }
1217 
1218  for (i = 0; i<s->geotag_count; i++) {
1219  const char *keyname = get_geokey_name(s->geotags[i].key);
1220  if (!keyname) {
1221  av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
1222  continue;
1223  }
1224  if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
1225  av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
1226  continue;
1227  }
1228  ret = av_dict_set(avpriv_frame_get_metadatap(p), keyname, s->geotags[i].val, 0);
1229  if (ret<0) {
1230  av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
1231  return ret;
1232  }
1233  }
1234 
1235  if (!s->strippos && !s->stripoff) {
1236  av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
1237  return AVERROR_INVALIDDATA;
1238  }
1239  /* now we have the data and may start decoding */
1240  if ((ret = init_image(s, &frame)) < 0)
1241  return ret;
1242 
1243  if (s->strips == 1 && !s->stripsize) {
1244  av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
1245  s->stripsize = avpkt->size - s->stripoff;
1246  }
1247 
1248  if (s->stripsizesoff) {
1249  if (s->stripsizesoff >= (unsigned)avpkt->size)
1250  return AVERROR_INVALIDDATA;
1251  bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
1252  avpkt->size - s->stripsizesoff);
1253  }
1254  if (s->strippos) {
1255  if (s->strippos >= (unsigned)avpkt->size)
1256  return AVERROR_INVALIDDATA;
1257  bytestream2_init(&stripdata, avpkt->data + s->strippos,
1258  avpkt->size - s->strippos);
1259  }
1260 
1261  if (s->rps <= 0 || s->rps % s->subsampling[1]) {
1262  av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
1263  return AVERROR_INVALIDDATA;
1264  }
1265 
1266  planes = s->planar ? s->bppcount : 1;
1267  for (plane = 0; plane < planes; plane++) {
1268  stride = p->linesize[plane];
1269  dst = p->data[plane];
1270  for (i = 0; i < s->height; i += s->rps) {
1271  if (s->stripsizesoff)
1272  ssize = ff_tget(&stripsizes, s->sstype, le);
1273  else
1274  ssize = s->stripsize;
1275 
1276  if (s->strippos)
1277  soff = ff_tget(&stripdata, s->sot, le);
1278  else
1279  soff = s->stripoff;
1280 
1281  if (soff > avpkt->size || ssize > avpkt->size - soff) {
1282  av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
1283  return AVERROR_INVALIDDATA;
1284  }
1285  if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,
1286  FFMIN(s->rps, s->height - i))) < 0) {
1287  if (avctx->err_recognition & AV_EF_EXPLODE)
1288  return ret;
1289  break;
1290  }
1291  dst += s->rps * stride;
1292  }
1293  if (s->predictor == 2) {
1294  if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1295  av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");
1296  return AVERROR_PATCHWELCOME;
1297  }
1298  dst = p->data[plane];
1299  soff = s->bpp >> 3;
1300  if (s->planar)
1301  soff = FFMAX(soff / s->bppcount, 1);
1302  ssize = s->width * soff;
1303  if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
1306  s->avctx->pix_fmt == AV_PIX_FMT_YA16LE ||
1309  for (i = 0; i < s->height; i++) {
1310  for (j = soff; j < ssize; j += 2)
1311  AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
1312  dst += stride;
1313  }
1314  } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1317  s->avctx->pix_fmt == AV_PIX_FMT_YA16BE ||
1320  for (i = 0; i < s->height; i++) {
1321  for (j = soff; j < ssize; j += 2)
1322  AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
1323  dst += stride;
1324  }
1325  } else {
1326  for (i = 0; i < s->height; i++) {
1327  for (j = soff; j < ssize; j++)
1328  dst[j] += dst[j - soff];
1329  dst += stride;
1330  }
1331  }
1332  }
1333 
1335  dst = p->data[plane];
1336  for (i = 0; i < s->height; i++) {
1337  for (j = 0; j < stride; j++)
1338  dst[j] = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];
1339  dst += stride;
1340  }
1341  }
1342  }
1343 
1344  if (s->planar && s->bppcount > 2) {
1345  FFSWAP(uint8_t*, p->data[0], p->data[2]);
1346  FFSWAP(int, p->linesize[0], p->linesize[2]);
1347  FFSWAP(uint8_t*, p->data[0], p->data[1]);
1348  FFSWAP(int, p->linesize[0], p->linesize[1]);
1349  }
1350 
1351  *got_frame = 1;
1352 
1353  return avpkt->size;
1354 }
1355 
1357 {
1358  TiffContext *s = avctx->priv_data;
1359 
1360  s->width = 0;
1361  s->height = 0;
1362  s->subsampling[0] =
1363  s->subsampling[1] = 1;
1364  s->avctx = avctx;
1365  ff_lzw_decode_open(&s->lzw);
1367 
1368  return 0;
1369 }
1370 
1371 static av_cold int tiff_end(AVCodecContext *avctx)
1372 {
1373  TiffContext *const s = avctx->priv_data;
1374 
1375  free_geotags(s);
1376 
1377  ff_lzw_decode_close(&s->lzw);
1378  av_freep(&s->deinvert_buf);
1379  return 0;
1380 }
1381 
1383  .name = "tiff",
1384  .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
1385  .type = AVMEDIA_TYPE_VIDEO,
1386  .id = AV_CODEC_ID_TIFF,
1387  .priv_data_size = sizeof(TiffContext),
1388  .init = tiff_init,
1389  .close = tiff_end,
1390  .decode = decode_frame,
1392  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1393 };
Definition: tiff.h:54
int plane
Definition: avisynth_c.h:291
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:634
Definition: tiff.h:89
int offset
Definition: tiff.h:178
const char * s
Definition: avisynth_c.h:631
#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:2157
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
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
8bit gray, 8bit alpha
Definition: pixfmt.h:155
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:68
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:390
AVFrame * f
Definition: thread.h:36
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:65
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:209
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
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:181
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1468
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:1935
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:1752
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:119
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:3392
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:1174
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 upacker 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 bit with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:74
#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:112
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:1356
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1467
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:1348
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
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
16bit gray, 16bit 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:1371
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:107
static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
Definition: tiff.c:277
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:3399
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
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:919
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:95
int predictor
Definition: tiff.c:64
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:215
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
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:2811
#define FFMIN(a, b)
Definition: common.h:96
int le
Definition: tiff.c:58
int width
picture width / height.
Definition: avcodec.h:1711
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:2822
int n
Definition: avisynth_c.h:547
#define src
Definition: vp9dsp.c:530
#define FF_ARRAY_ELEMS(a)
uint8_t le
Definition: crc.c:294
#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:242
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
Libavcodec external API header.
AVDictionary ** avpriv_frame_get_metadatap(AVFrame *frame)
Definition: frame.c:47
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
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:1532
Definition: tiff.h:91
void * buf
Definition: avisynth_c.h:553
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:69
AVCodecContext * avctx
Definition: tiff.c:51
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:69
Y , 16bpp, big-endian.
Definition: pixfmt.h:99
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:192
int count
Definition: tiff.h:177
enum TiffTags type
Definition: tiff.h:176
LZW decoding routines.
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:572
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:73
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:63
int stripsizesoff
Definition: tiff.c:70
Y , 8bpp.
Definition: pixfmt.h:71
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:111
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:70
int den
denominator
Definition: rational.h:45
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:635
void * priv_data
Definition: avcodec.h:1574
#define av_free(p)
int pixels
Definition: avisynth_c.h:298
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:100
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:208
16bit gray, 16bit 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
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
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:101
void INT64 start
Definition: avisynth_c.h:553
AVCodec ff_tiff_decoder
Definition: tiff.c:1382
#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:1444
#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:856
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
static int width