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  if (s->fax_opts & 2) {
464  avpriv_request_sample(s->avctx, "Uncompressed fax mode");
465  av_free(src2);
466  return AVERROR_PATCHWELCOME;
467  }
468  if (!s->fill_order) {
469  memcpy(src2, src, size);
470  } else {
471  for (i = 0; i < size; i++)
472  src2[i] = ff_reverse[src[i]];
473  }
474  memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
475  ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
476  s->compr, s->fax_opts);
477  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
478  for (line = 0; line < lines; line++) {
479  horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
480  dst += stride;
481  }
482  av_free(src2);
483  return ret;
484 }
485 
486 static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
487  const uint8_t *src, int size, int strip_start, int lines)
488 {
489  PutByteContext pb;
490  int c, line, pixels, code, ret;
491  const uint8_t *ssrc = src;
492  int width = ((s->width * s->bpp) + 7) >> 3;
494  int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
495  (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
496  desc->nb_components >= 3;
497 
498  if (s->planar)
499  width /= s->bppcount;
500 
501  if (size <= 0)
502  return AVERROR_INVALIDDATA;
503 
504  if (is_yuv) {
505  int bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
506  s->subsampling[0] * s->subsampling[1] + 7) >> 3;
507  av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
508  if (s->yuv_line == NULL) {
509  av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
510  return AVERROR(ENOMEM);
511  }
512  dst = s->yuv_line;
513  stride = 0;
514 
515  width = (s->width - 1) / s->subsampling[0] + 1;
516  width = width * s->subsampling[0] * s->subsampling[1] + 2*width;
517  av_assert0(width <= bytes_per_row);
518  av_assert0(s->bpp == 24);
519  }
520 
521  if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
522 #if CONFIG_ZLIB
523  return tiff_unpack_zlib(s, p, dst, stride, src, size, width, lines,
524  strip_start, is_yuv);
525 #else
527  "zlib support not enabled, "
528  "deflate compression not supported\n");
529  return AVERROR(ENOSYS);
530 #endif
531  }
532  if (s->compr == TIFF_LZMA) {
533 #if CONFIG_LZMA
534  return tiff_unpack_lzma(s, p, dst, stride, src, size, width, lines,
535  strip_start, is_yuv);
536 #else
538  "LZMA support not enabled\n");
539  return AVERROR(ENOSYS);
540 #endif
541  }
542  if (s->compr == TIFF_LZW) {
543  if (s->fill_order) {
544  if ((ret = deinvert_buffer(s, src, size)) < 0)
545  return ret;
546  ssrc = src = s->deinvert_buf;
547  }
548  if (size > 1 && !src[0] && (src[1]&1)) {
549  av_log(s->avctx, AV_LOG_ERROR, "Old style LZW is unsupported\n");
550  }
551  if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
552  av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
553  return ret;
554  }
555  for (line = 0; line < lines; line++) {
556  pixels = ff_lzw_decode(s->lzw, dst, width);
557  if (pixels < width) {
558  av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
559  pixels, width);
560  return AVERROR_INVALIDDATA;
561  }
562  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
563  horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
564  if (is_yuv) {
565  unpack_yuv(s, p, dst, strip_start + line);
566  line += s->subsampling[1] - 1;
567  }
568  dst += stride;
569  }
570  return 0;
571  }
572  if (s->compr == TIFF_CCITT_RLE ||
573  s->compr == TIFF_G3 ||
574  s->compr == TIFF_G4) {
575  if (is_yuv)
576  return AVERROR_INVALIDDATA;
577 
578  return tiff_unpack_fax(s, dst, stride, src, size, width, lines);
579  }
580 
581  bytestream2_init(&s->gb, src, size);
582  bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * lines));
583 
584  for (line = 0; line < lines; line++) {
585  if (src - ssrc > size) {
586  av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
587  return AVERROR_INVALIDDATA;
588  }
589 
590  if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
591  break;
592  bytestream2_seek_p(&pb, stride * line, SEEK_SET);
593  switch (s->compr) {
594  case TIFF_RAW:
595  if (ssrc + size - src < width)
596  return AVERROR_INVALIDDATA;
597 
598  if (!s->fill_order) {
600  dst, 1, src, 0, width, 0);
601  } else {
602  int i;
603  for (i = 0; i < width; i++)
604  dst[i] = ff_reverse[src[i]];
605  }
606  src += width;
607  break;
608  case TIFF_PACKBITS:
609  for (pixels = 0; pixels < width;) {
610  if (ssrc + size - src < 2) {
611  av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
612  return AVERROR_INVALIDDATA;
613  }
614  code = s->fill_order ? (int8_t) ff_reverse[*src++]: (int8_t) *src++;
615  if (code >= 0) {
616  code++;
617  if (pixels + code > width ||
618  ssrc + size - src < code) {
620  "Copy went out of bounds\n");
621  return AVERROR_INVALIDDATA;
622  }
624  dst, 1, src, 0, code, pixels);
625  src += code;
626  pixels += code;
627  } else if (code != -128) { // -127..-1
628  code = (-code) + 1;
629  if (pixels + code > width) {
631  "Run went out of bounds\n");
632  return AVERROR_INVALIDDATA;
633  }
634  c = *src++;
636  dst, 0, NULL, c, code, pixels);
637  pixels += code;
638  }
639  }
640  if (s->fill_order) {
641  int i;
642  for (i = 0; i < width; i++)
643  dst[i] = ff_reverse[dst[i]];
644  }
645  break;
646  }
647  if (is_yuv) {
648  unpack_yuv(s, p, dst, strip_start + line);
649  line += s->subsampling[1] - 1;
650  }
651  dst += stride;
652  }
653  return 0;
654 }
655 
657 {
658  int ret;
659  int create_gray_palette = 0;
660 
661  // make sure there is no aliasing in the following switch
662  if (s->bpp >= 100 || s->bppcount >= 10) {
664  "Unsupported image parameters: bpp=%d, bppcount=%d\n",
665  s->bpp, s->bppcount);
666  return AVERROR_INVALIDDATA;
667  }
668 
669  switch (s->planar * 1000 + s->bpp * 10 + s->bppcount) {
670  case 11:
671  if (!s->palette_is_set) {
673  break;
674  }
675  case 21:
676  case 41:
678  if (!s->palette_is_set) {
679  create_gray_palette = 1;
680  }
681  break;
682  case 81:
684  break;
685  case 243:
687  if (s->subsampling[0] == 1 && s->subsampling[1] == 1) {
689  } else if (s->subsampling[0] == 2 && s->subsampling[1] == 1) {
691  } else if (s->subsampling[0] == 4 && s->subsampling[1] == 1) {
693  } else if (s->subsampling[0] == 1 && s->subsampling[1] == 2) {
695  } else if (s->subsampling[0] == 2 && s->subsampling[1] == 2) {
697  } else if (s->subsampling[0] == 4 && s->subsampling[1] == 4) {
699  } else {
700  av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr subsampling\n");
701  return AVERROR_PATCHWELCOME;
702  }
703  } else
705  break;
706  case 161:
708  break;
709  case 162:
711  break;
712  case 322:
714  break;
715  case 324:
717  break;
718  case 483:
720  break;
721  case 644:
723  break;
724  case 1243:
726  break;
727  case 1324:
729  break;
730  case 1483:
732  break;
733  case 1644:
735  break;
736  default:
738  "This format is not supported (bpp=%d, bppcount=%d)\n",
739  s->bpp, s->bppcount);
740  return AVERROR_INVALIDDATA;
741  }
742 
745  if((desc->flags & AV_PIX_FMT_FLAG_RGB) ||
746  !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
747  desc->nb_components < 3) {
748  av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr variant\n");
749  return AVERROR_INVALIDDATA;
750  }
751  }
752 
753  if (s->width != s->avctx->width || s->height != s->avctx->height) {
754  ret = ff_set_dimensions(s->avctx, s->width, s->height);
755  if (ret < 0)
756  return ret;
757  }
758  if ((ret = ff_thread_get_buffer(s->avctx, frame, 0)) < 0)
759  return ret;
760  if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
761  if (!create_gray_palette)
762  memcpy(frame->f->data[1], s->palette, sizeof(s->palette));
763  else {
764  /* make default grayscale pal */
765  int i;
766  uint32_t *pal = (uint32_t *)frame->f->data[1];
767  for (i = 0; i < 1<<s->bpp; i++)
768  pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
769  }
770  }
771  return 0;
772 }
773 
774 static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
775 {
776  int offset = tag == TIFF_YRES ? 2 : 0;
777  s->res[offset++] = num;
778  s->res[offset] = den;
779  if (s->res[0] && s->res[1] && s->res[2] && s->res[3])
781  s->res[2] * (uint64_t)s->res[1], s->res[0] * (uint64_t)s->res[3], INT32_MAX);
782 }
783 
785 {
786  unsigned tag, type, count, off, value = 0, value2 = 0;
787  int i, start;
788  int pos;
789  int ret;
790  double *dp;
791 
792  ret = ff_tread_tag(&s->gb, s->le, &tag, &type, &count, &start);
793  if (ret < 0) {
794  goto end;
795  }
796 
797  off = bytestream2_tell(&s->gb);
798  if (count == 1) {
799  switch (type) {
800  case TIFF_BYTE:
801  case TIFF_SHORT:
802  case TIFF_LONG:
803  value = ff_tget(&s->gb, type, s->le);
804  break;
805  case TIFF_RATIONAL:
806  value = ff_tget(&s->gb, TIFF_LONG, s->le);
807  value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
808  break;
809  case TIFF_STRING:
810  if (count <= 4) {
811  break;
812  }
813  default:
814  value = UINT_MAX;
815  }
816  }
817 
818  switch (tag) {
819  case TIFF_WIDTH:
820  s->width = value;
821  break;
822  case TIFF_HEIGHT:
823  s->height = value;
824  break;
825  case TIFF_BPP:
826  if (count > 4U) {
828  "This format is not supported (bpp=%d, %d components)\n",
829  value, count);
830  return AVERROR_INVALIDDATA;
831  }
832  s->bppcount = count;
833  if (count == 1)
834  s->bpp = value;
835  else {
836  switch (type) {
837  case TIFF_BYTE:
838  case TIFF_SHORT:
839  case TIFF_LONG:
840  s->bpp = 0;
841  if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
842  return AVERROR_INVALIDDATA;
843  for (i = 0; i < count; i++)
844  s->bpp += ff_tget(&s->gb, type, s->le);
845  break;
846  default:
847  s->bpp = -1;
848  }
849  }
850  break;
852  if (count != 1) {
854  "Samples per pixel requires a single value, many provided\n");
855  return AVERROR_INVALIDDATA;
856  }
857  if (value > 4U) {
859  "Samples per pixel %d is too large\n", value);
860  return AVERROR_INVALIDDATA;
861  }
862  if (s->bppcount == 1)
863  s->bpp *= value;
864  s->bppcount = value;
865  break;
866  case TIFF_COMPR:
867  s->compr = value;
868  s->predictor = 0;
869  switch (s->compr) {
870  case TIFF_RAW:
871  case TIFF_PACKBITS:
872  case TIFF_LZW:
873  case TIFF_CCITT_RLE:
874  break;
875  case TIFF_G3:
876  case TIFF_G4:
877  s->fax_opts = 0;
878  break;
879  case TIFF_DEFLATE:
880  case TIFF_ADOBE_DEFLATE:
881 #if CONFIG_ZLIB
882  break;
883 #else
884  av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
885  return AVERROR(ENOSYS);
886 #endif
887  case TIFF_JPEG:
888  case TIFF_NEWJPEG:
889  avpriv_report_missing_feature(s->avctx, "JPEG compression");
890  return AVERROR_PATCHWELCOME;
891  case TIFF_LZMA:
892 #if CONFIG_LZMA
893  break;
894 #else
895  av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n");
896  return AVERROR(ENOSYS);
897 #endif
898  default:
899  av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
900  s->compr);
901  return AVERROR_INVALIDDATA;
902  }
903  break;
904  case TIFF_ROWSPERSTRIP:
905  if (!value || (type == TIFF_LONG && value == UINT_MAX))
906  value = s->height;
907  s->rps = FFMIN(value, s->height);
908  break;
909  case TIFF_STRIP_OFFS:
910  if (count == 1) {
911  s->strippos = 0;
912  s->stripoff = value;
913  } else
914  s->strippos = off;
915  s->strips = count;
916  if (s->strips == 1)
917  s->rps = s->height;
918  s->sot = type;
919  break;
920  case TIFF_STRIP_SIZE:
921  if (count == 1) {
922  s->stripsizesoff = 0;
923  s->stripsize = value;
924  s->strips = 1;
925  } else {
926  s->stripsizesoff = off;
927  }
928  s->strips = count;
929  s->sstype = type;
930  break;
931  case TIFF_XRES:
932  case TIFF_YRES:
933  set_sar(s, tag, value, value2);
934  break;
936  case TIFF_TILE_LENGTH:
937  case TIFF_TILE_OFFSETS:
938  case TIFF_TILE_WIDTH:
939  av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");
940  return AVERROR_PATCHWELCOME;
941  break;
942  case TIFF_PREDICTOR:
943  s->predictor = value;
944  break;
945  case TIFF_PHOTOMETRIC:
946  switch (value) {
952  s->photometric = value;
953  break;
964  "PhotometricInterpretation 0x%04X",
965  value);
966  return AVERROR_PATCHWELCOME;
967  default:
968  av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
969  "unknown\n", value);
970  return AVERROR_INVALIDDATA;
971  }
972  break;
973  case TIFF_FILL_ORDER:
974  if (value < 1 || value > 2) {
976  "Unknown FillOrder value %d, trying default one\n", value);
977  value = 1;
978  }
979  s->fill_order = value - 1;
980  break;
981  case TIFF_PAL: {
982  GetByteContext pal_gb[3];
983  off = type_sizes[type];
984  if (count / 3 > 256 ||
985  bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
986  return AVERROR_INVALIDDATA;
987 
988  pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
989  bytestream2_skip(&pal_gb[1], count / 3 * off);
990  bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
991 
992  off = (type_sizes[type] - 1) << 3;
993  for (i = 0; i < count / 3; i++) {
994  uint32_t p = 0xFF000000;
995  p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16;
996  p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8;
997  p |= ff_tget(&pal_gb[2], type, s->le) >> off;
998  s->palette[i] = p;
999  }
1000  s->palette_is_set = 1;
1001  break;
1002  }
1003  case TIFF_PLANAR:
1004  s->planar = value == 2;
1005  break;
1007  if (count != 2) {
1008  av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n");
1009  return AVERROR_INVALIDDATA;
1010  }
1011  for (i = 0; i < count; i++)
1012  s->subsampling[i] = ff_tget(&s->gb, type, s->le);
1013  break;
1014  case TIFF_T4OPTIONS:
1015  if (s->compr == TIFF_G3)
1016  s->fax_opts = value;
1017  break;
1018  case TIFF_T6OPTIONS:
1019  if (s->compr == TIFF_G4)
1020  s->fax_opts = value;
1021  break;
1022 #define ADD_METADATA(count, name, sep)\
1023  if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1024  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1025  goto end;\
1026  }
1028  ADD_METADATA(count, "ModelPixelScaleTag", NULL);
1029  break;
1031  ADD_METADATA(count, "ModelTransformationTag", NULL);
1032  break;
1033  case TIFF_MODEL_TIEPOINT:
1034  ADD_METADATA(count, "ModelTiepointTag", NULL);
1035  break;
1037  ADD_METADATA(1, "GeoTIFF_Version", NULL);
1038  ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
1039  s->geotag_count = ff_tget_short(&s->gb, s->le);
1040  if (s->geotag_count > count / 4 - 1) {
1041  s->geotag_count = count / 4 - 1;
1042  av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
1043  }
1044  if (bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4) {
1045  s->geotag_count = 0;
1046  return -1;
1047  }
1048  s->geotags = av_mallocz_array(s->geotag_count, sizeof(TiffGeoTag));
1049  if (!s->geotags) {
1050  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1051  s->geotag_count = 0;
1052  goto end;
1053  }
1054  for (i = 0; i < s->geotag_count; i++) {
1055  s->geotags[i].key = ff_tget_short(&s->gb, s->le);
1056  s->geotags[i].type = ff_tget_short(&s->gb, s->le);
1057  s->geotags[i].count = ff_tget_short(&s->gb, s->le);
1058 
1059  if (!s->geotags[i].type)
1060  s->geotags[i].val = get_geokey_val(s->geotags[i].key, ff_tget_short(&s->gb, s->le));
1061  else
1062  s->geotags[i].offset = ff_tget_short(&s->gb, s->le);
1063  }
1064  break;
1066  if (count >= INT_MAX / sizeof(int64_t))
1067  return AVERROR_INVALIDDATA;
1068  if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
1069  return AVERROR_INVALIDDATA;
1070  dp = av_malloc_array(count, sizeof(double));
1071  if (!dp) {
1072  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1073  goto end;
1074  }
1075  for (i = 0; i < count; i++)
1076  dp[i] = ff_tget_double(&s->gb, s->le);
1077  for (i = 0; i < s->geotag_count; i++) {
1078  if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
1079  if (s->geotags[i].count == 0
1080  || s->geotags[i].offset + s->geotags[i].count > count) {
1081  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1082  } else {
1083  char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
1084  if (!ap) {
1085  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1086  av_freep(&dp);
1087  return AVERROR(ENOMEM);
1088  }
1089  s->geotags[i].val = ap;
1090  }
1091  }
1092  }
1093  av_freep(&dp);
1094  break;
1095  case TIFF_GEO_ASCII_PARAMS:
1096  pos = bytestream2_tell(&s->gb);
1097  for (i = 0; i < s->geotag_count; i++) {
1098  if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
1099  if (s->geotags[i].count == 0
1100  || s->geotags[i].offset + s->geotags[i].count > count) {
1101  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1102  } else {
1103  char *ap;
1104 
1105  bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
1106  if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
1107  return AVERROR_INVALIDDATA;
1108  ap = av_malloc(s->geotags[i].count);
1109  if (!ap) {
1110  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1111  return AVERROR(ENOMEM);
1112  }
1113  bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
1114  ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
1115  s->geotags[i].val = ap;
1116  }
1117  }
1118  }
1119  break;
1120  case TIFF_ARTIST:
1121  ADD_METADATA(count, "artist", NULL);
1122  break;
1123  case TIFF_COPYRIGHT:
1124  ADD_METADATA(count, "copyright", NULL);
1125  break;
1126  case TIFF_DATE:
1127  ADD_METADATA(count, "date", NULL);
1128  break;
1129  case TIFF_DOCUMENT_NAME:
1130  ADD_METADATA(count, "document_name", NULL);
1131  break;
1132  case TIFF_HOST_COMPUTER:
1133  ADD_METADATA(count, "computer", NULL);
1134  break;
1136  ADD_METADATA(count, "description", NULL);
1137  break;
1138  case TIFF_MAKE:
1139  ADD_METADATA(count, "make", NULL);
1140  break;
1141  case TIFF_MODEL:
1142  ADD_METADATA(count, "model", NULL);
1143  break;
1144  case TIFF_PAGE_NAME:
1145  ADD_METADATA(count, "page_name", NULL);
1146  break;
1147  case TIFF_PAGE_NUMBER:
1148  ADD_METADATA(count, "page_number", " / ");
1149  break;
1150  case TIFF_SOFTWARE_NAME:
1151  ADD_METADATA(count, "software", NULL);
1152  break;
1153  default:
1154  if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1156  "Unknown or unsupported tag %d/0X%0X\n",
1157  tag, tag);
1158  return AVERROR_INVALIDDATA;
1159  }
1160  }
1161 end:
1162  if (s->bpp > 64U) {
1164  "This format is not supported (bpp=%d, %d components)\n",
1165  s->bpp, count);
1166  s->bpp = 0;
1167  return AVERROR_INVALIDDATA;
1168  }
1169  bytestream2_seek(&s->gb, start, SEEK_SET);
1170  return 0;
1171 }
1172 
1173 static int decode_frame(AVCodecContext *avctx,
1174  void *data, int *got_frame, AVPacket *avpkt)
1175 {
1176  TiffContext *const s = avctx->priv_data;
1177  AVFrame *const p = data;
1178  ThreadFrame frame = { .f = data };
1179  unsigned off;
1180  int le, ret, plane, planes;
1181  int i, j, entries, stride;
1182  unsigned soff, ssize;
1183  uint8_t *dst;
1184  GetByteContext stripsizes;
1185  GetByteContext stripdata;
1186 
1187  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1188 
1189  // parse image header
1190  if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
1191  av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
1192  return ret;
1193  } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1194  av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1195  return AVERROR_INVALIDDATA;
1196  }
1197  s->le = le;
1198  // TIFF_BPP is not a required tag and defaults to 1
1199  s->bppcount = s->bpp = 1;
1201  s->compr = TIFF_RAW;
1202  s->fill_order = 0;
1203  free_geotags(s);
1204 
1205  // Reset these offsets so we can tell if they were set this frame
1206  s->stripsizesoff = s->strippos = 0;
1207  /* parse image file directory */
1208  bytestream2_seek(&s->gb, off, SEEK_SET);
1209  entries = ff_tget_short(&s->gb, le);
1210  if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
1211  return AVERROR_INVALIDDATA;
1212  for (i = 0; i < entries; i++) {
1213  if ((ret = tiff_decode_tag(s, p)) < 0)
1214  return ret;
1215  }
1216 
1217  for (i = 0; i<s->geotag_count; i++) {
1218  const char *keyname = get_geokey_name(s->geotags[i].key);
1219  if (!keyname) {
1220  av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
1221  continue;
1222  }
1223  if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
1224  av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
1225  continue;
1226  }
1227  ret = av_dict_set(avpriv_frame_get_metadatap(p), keyname, s->geotags[i].val, 0);
1228  if (ret<0) {
1229  av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
1230  return ret;
1231  }
1232  }
1233 
1234  if (!s->strippos && !s->stripoff) {
1235  av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
1236  return AVERROR_INVALIDDATA;
1237  }
1238  /* now we have the data and may start decoding */
1239  if ((ret = init_image(s, &frame)) < 0)
1240  return ret;
1241 
1242  if (s->strips == 1 && !s->stripsize) {
1243  av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
1244  s->stripsize = avpkt->size - s->stripoff;
1245  }
1246 
1247  if (s->stripsizesoff) {
1248  if (s->stripsizesoff >= (unsigned)avpkt->size)
1249  return AVERROR_INVALIDDATA;
1250  bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
1251  avpkt->size - s->stripsizesoff);
1252  }
1253  if (s->strippos) {
1254  if (s->strippos >= (unsigned)avpkt->size)
1255  return AVERROR_INVALIDDATA;
1256  bytestream2_init(&stripdata, avpkt->data + s->strippos,
1257  avpkt->size - s->strippos);
1258  }
1259 
1260  if (s->rps <= 0) {
1261  av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
1262  return AVERROR_INVALIDDATA;
1263  }
1264 
1265  planes = s->planar ? s->bppcount : 1;
1266  for (plane = 0; plane < planes; plane++) {
1267  stride = p->linesize[plane];
1268  dst = p->data[plane];
1269  for (i = 0; i < s->height; i += s->rps) {
1270  if (s->stripsizesoff)
1271  ssize = ff_tget(&stripsizes, s->sstype, le);
1272  else
1273  ssize = s->stripsize;
1274 
1275  if (s->strippos)
1276  soff = ff_tget(&stripdata, s->sot, le);
1277  else
1278  soff = s->stripoff;
1279 
1280  if (soff > avpkt->size || ssize > avpkt->size - soff) {
1281  av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
1282  return AVERROR_INVALIDDATA;
1283  }
1284  if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,
1285  FFMIN(s->rps, s->height - i))) < 0) {
1286  if (avctx->err_recognition & AV_EF_EXPLODE)
1287  return ret;
1288  break;
1289  }
1290  dst += s->rps * stride;
1291  }
1292  if (s->predictor == 2) {
1293  if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1294  av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");
1295  return AVERROR_PATCHWELCOME;
1296  }
1297  dst = p->data[plane];
1298  soff = s->bpp >> 3;
1299  if (s->planar)
1300  soff = FFMAX(soff / s->bppcount, 1);
1301  ssize = s->width * soff;
1302  if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
1305  s->avctx->pix_fmt == AV_PIX_FMT_YA16LE ||
1308  for (i = 0; i < s->height; i++) {
1309  for (j = soff; j < ssize; j += 2)
1310  AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
1311  dst += stride;
1312  }
1313  } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1316  s->avctx->pix_fmt == AV_PIX_FMT_YA16BE ||
1319  for (i = 0; i < s->height; i++) {
1320  for (j = soff; j < ssize; j += 2)
1321  AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
1322  dst += stride;
1323  }
1324  } else {
1325  for (i = 0; i < s->height; i++) {
1326  for (j = soff; j < ssize; j++)
1327  dst[j] += dst[j - soff];
1328  dst += stride;
1329  }
1330  }
1331  }
1332 
1334  dst = p->data[plane];
1335  for (i = 0; i < s->height; i++) {
1336  for (j = 0; j < stride; j++)
1337  dst[j] = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];
1338  dst += stride;
1339  }
1340  }
1341  }
1342 
1343  if (s->planar && s->bppcount > 2) {
1344  FFSWAP(uint8_t*, p->data[0], p->data[2]);
1345  FFSWAP(int, p->linesize[0], p->linesize[2]);
1346  FFSWAP(uint8_t*, p->data[0], p->data[1]);
1347  FFSWAP(int, p->linesize[0], p->linesize[1]);
1348  }
1349 
1350  *got_frame = 1;
1351 
1352  return avpkt->size;
1353 }
1354 
1356 {
1357  TiffContext *s = avctx->priv_data;
1358 
1359  s->width = 0;
1360  s->height = 0;
1361  s->subsampling[0] =
1362  s->subsampling[1] = 1;
1363  s->avctx = avctx;
1364  ff_lzw_decode_open(&s->lzw);
1366 
1367  return 0;
1368 }
1369 
1370 static av_cold int tiff_end(AVCodecContext *avctx)
1371 {
1372  TiffContext *const s = avctx->priv_data;
1373 
1374  free_geotags(s);
1375 
1376  ff_lzw_decode_close(&s->lzw);
1377  av_freep(&s->deinvert_buf);
1378  return 0;
1379 }
1380 
1382  .name = "tiff",
1383  .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
1384  .type = AVMEDIA_TYPE_VIDEO,
1385  .id = AV_CODEC_ID_TIFF,
1386  .priv_data_size = sizeof(TiffContext),
1387  .init = tiff_init,
1388  .close = tiff_end,
1389  .decode = decode_frame,
1391  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
1392 };
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:165
#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:2090
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
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:143
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
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:229
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:176
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1163
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:141
av_cold void ff_lzw_decode_close(LZWState **p)
Definition: lzw.c:114
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:1623
av_cold void ff_lzw_decode_open(LZWState **p)
Definition: lzw.c:109
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:131
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 FF_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:139
#define FF_ARRAY_ELEMS(a)
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:85
unsigned int yuv_line_size
Definition: tiff.c:76
AVCodec.
Definition: avcodec.h:3181
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:85
Definition: tiff.h:93
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:271
Definition: tiff.h:92
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: tiff.c:1173
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:486
static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
Definition: tiff.c:784
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:288
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
if()
Definition: avfilter.c:975
uint8_t
#define av_cold
Definition: attributes.h:74
#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:67
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:259
static av_cold int tiff_init(AVCodecContext *avctx)
Definition: tiff.c:1355
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:789
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1162
planar GBR 4:4:4 48bpp, big-endian
Definition: pixfmt.h:181
uint32_t tag
Definition: movenc.c:1333
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:234
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2623
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:131
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:162
#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:175
static av_cold int tiff_end(AVCodecContext *avctx)
Definition: tiff.c:1370
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:152
Definition: graph2dot.c:48
const char * name
Name of the codec implementation.
Definition: avcodec.h:3188
static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
Definition: tiff.c:774
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:279
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:64
Libavcodec external API header.
uint8_t * yuv_line
Definition: tiff.c:75
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:214
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
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:630
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:71
int stripsize
Definition: tiff.c:70
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2612
#define FFMIN(a, b)
Definition: common.h:66
int le
Definition: tiff.c:58
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1414
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
int n
Definition: avisynth_c.h:547
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:230
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:186
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
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
AVS_Value src
Definition: avisynth_c.h:482
AVDictionary ** avpriv_frame_get_metadatap(AVFrame *frame)
Definition: frame.c:47
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:265
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
enum TiffCompr compr
Definition: tiff.c:59
unsigned int bppcount
Definition: tiff.c:55
uint8_t flags
Definition: pixdesc.h:90
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
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:1241
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:182
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:522
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.
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:866
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:326
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:287
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:127
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define RET_GEOKEY_VAL(TYPE, array)
void * priv_data
Definition: avcodec.h:1283
#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:206
16bit gray, 16bit alpha (little-endian)
Definition: pixfmt.h:235
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:228
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:656
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
static int init_thread_copy(AVCodecContext *avctx)
Definition: alac.c:640
AVCodec ff_tiff_decoder
Definition: tiff.c:1381
#define av_always_inline
Definition: attributes.h:37
planar GBR 4:4:4 48bpp, little-endian
Definition: pixfmt.h:182
const uint8_t ff_reverse[256]
Definition: mathtables.c:74
#define av_malloc_array(a, b)
#define FFSWAP(type, a, b)
Definition: common.h:69
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:289
#define ADD_METADATA(count, name, sep)
Definition: tiff.h:64
This structure stores compressed data.
Definition: avcodec.h:1139
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:127
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:260
const char * name
Definition: opengl_enc.c:103
static int width