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 * (uint64_t)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]) {
776  uint64_t num = s->res[2] * (uint64_t)s->res[1];
777  uint64_t den = s->res[0] * (uint64_t)s->res[3];
778  if (num > INT64_MAX || den > INT64_MAX) {
779  num = num >> 1;
780  den = den >> 1;
781  }
783  num, den, INT32_MAX);
784  if (!s->avctx->sample_aspect_ratio.den)
785  s->avctx->sample_aspect_ratio = (AVRational) {0, 1};
786  }
787 }
788 
790 {
791  unsigned tag, type, count, off, value = 0, value2 = 0;
792  int i, start;
793  int pos;
794  int ret;
795  double *dp;
796 
797  ret = ff_tread_tag(&s->gb, s->le, &tag, &type, &count, &start);
798  if (ret < 0) {
799  goto end;
800  }
801 
802  off = bytestream2_tell(&s->gb);
803  if (count == 1) {
804  switch (type) {
805  case TIFF_BYTE:
806  case TIFF_SHORT:
807  case TIFF_LONG:
808  value = ff_tget(&s->gb, type, s->le);
809  break;
810  case TIFF_RATIONAL:
811  value = ff_tget(&s->gb, TIFF_LONG, s->le);
812  value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
813  break;
814  case TIFF_STRING:
815  if (count <= 4) {
816  break;
817  }
818  default:
819  value = UINT_MAX;
820  }
821  }
822 
823  switch (tag) {
824  case TIFF_WIDTH:
825  s->width = value;
826  break;
827  case TIFF_HEIGHT:
828  s->height = value;
829  break;
830  case TIFF_BPP:
831  if (count > 4U) {
833  "This format is not supported (bpp=%d, %d components)\n",
834  value, count);
835  return AVERROR_INVALIDDATA;
836  }
837  s->bppcount = count;
838  if (count == 1)
839  s->bpp = value;
840  else {
841  switch (type) {
842  case TIFF_BYTE:
843  case TIFF_SHORT:
844  case TIFF_LONG:
845  s->bpp = 0;
846  if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
847  return AVERROR_INVALIDDATA;
848  for (i = 0; i < count; i++)
849  s->bpp += ff_tget(&s->gb, type, s->le);
850  break;
851  default:
852  s->bpp = -1;
853  }
854  }
855  break;
857  if (count != 1) {
859  "Samples per pixel requires a single value, many provided\n");
860  return AVERROR_INVALIDDATA;
861  }
862  if (value > 4U) {
864  "Samples per pixel %d is too large\n", value);
865  return AVERROR_INVALIDDATA;
866  }
867  if (s->bppcount == 1)
868  s->bpp *= value;
869  s->bppcount = value;
870  break;
871  case TIFF_COMPR:
872  s->compr = value;
873  av_log(s->avctx, AV_LOG_DEBUG, "compression: %d\n", s->compr);
874  s->predictor = 0;
875  switch (s->compr) {
876  case TIFF_RAW:
877  case TIFF_PACKBITS:
878  case TIFF_LZW:
879  case TIFF_CCITT_RLE:
880  break;
881  case TIFF_G3:
882  case TIFF_G4:
883  s->fax_opts = 0;
884  break;
885  case TIFF_DEFLATE:
886  case TIFF_ADOBE_DEFLATE:
887 #if CONFIG_ZLIB
888  break;
889 #else
890  av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
891  return AVERROR(ENOSYS);
892 #endif
893  case TIFF_JPEG:
894  case TIFF_NEWJPEG:
895  avpriv_report_missing_feature(s->avctx, "JPEG compression");
896  return AVERROR_PATCHWELCOME;
897  case TIFF_LZMA:
898 #if CONFIG_LZMA
899  break;
900 #else
901  av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n");
902  return AVERROR(ENOSYS);
903 #endif
904  default:
905  av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
906  s->compr);
907  return AVERROR_INVALIDDATA;
908  }
909  break;
910  case TIFF_ROWSPERSTRIP:
911  if (!value || (type == TIFF_LONG && value == UINT_MAX))
912  value = s->height;
913  s->rps = FFMIN(value, s->height);
914  break;
915  case TIFF_STRIP_OFFS:
916  if (count == 1) {
917  if (value > INT_MAX) {
919  "strippos %u too large\n", value);
920  return AVERROR_INVALIDDATA;
921  }
922  s->strippos = 0;
923  s->stripoff = value;
924  } else
925  s->strippos = off;
926  s->strips = count;
927  if (s->strips == 1)
928  s->rps = s->height;
929  s->sot = type;
930  break;
931  case TIFF_STRIP_SIZE:
932  if (count == 1) {
933  if (value > INT_MAX) {
935  "stripsize %u too large\n", value);
936  return AVERROR_INVALIDDATA;
937  }
938  s->stripsizesoff = 0;
939  s->stripsize = value;
940  s->strips = 1;
941  } else {
942  s->stripsizesoff = off;
943  }
944  s->strips = count;
945  s->sstype = type;
946  break;
947  case TIFF_XRES:
948  case TIFF_YRES:
949  set_sar(s, tag, value, value2);
950  break;
952  case TIFF_TILE_LENGTH:
953  case TIFF_TILE_OFFSETS:
954  case TIFF_TILE_WIDTH:
955  av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");
956  return AVERROR_PATCHWELCOME;
957  break;
958  case TIFF_PREDICTOR:
959  s->predictor = value;
960  break;
961  case TIFF_PHOTOMETRIC:
962  switch (value) {
968  s->photometric = value;
969  break;
980  "PhotometricInterpretation 0x%04X",
981  value);
982  return AVERROR_PATCHWELCOME;
983  default:
984  av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
985  "unknown\n", value);
986  return AVERROR_INVALIDDATA;
987  }
988  break;
989  case TIFF_FILL_ORDER:
990  if (value < 1 || value > 2) {
992  "Unknown FillOrder value %d, trying default one\n", value);
993  value = 1;
994  }
995  s->fill_order = value - 1;
996  break;
997  case TIFF_PAL: {
998  GetByteContext pal_gb[3];
999  off = type_sizes[type];
1000  if (count / 3 > 256 ||
1001  bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
1002  return AVERROR_INVALIDDATA;
1003 
1004  pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
1005  bytestream2_skip(&pal_gb[1], count / 3 * off);
1006  bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
1007 
1008  off = (type_sizes[type] - 1) << 3;
1009  if (off > 31U) {
1010  av_log(s->avctx, AV_LOG_ERROR, "palette shift %d is out of range\n", off);
1011  return AVERROR_INVALIDDATA;
1012  }
1013 
1014  for (i = 0; i < count / 3; i++) {
1015  uint32_t p = 0xFF000000;
1016  p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16;
1017  p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8;
1018  p |= ff_tget(&pal_gb[2], type, s->le) >> off;
1019  s->palette[i] = p;
1020  }
1021  s->palette_is_set = 1;
1022  break;
1023  }
1024  case TIFF_PLANAR:
1025  s->planar = value == 2;
1026  break;
1028  if (count != 2) {
1029  av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n");
1030  return AVERROR_INVALIDDATA;
1031  }
1032  for (i = 0; i < count; i++) {
1033  s->subsampling[i] = ff_tget(&s->gb, type, s->le);
1034  if (s->subsampling[i] <= 0) {
1035  av_log(s->avctx, AV_LOG_ERROR, "subsampling %d is invalid\n", s->subsampling[i]);
1036  s->subsampling[i] = 1;
1037  return AVERROR_INVALIDDATA;
1038  }
1039  }
1040  break;
1041  case TIFF_T4OPTIONS:
1042  if (s->compr == TIFF_G3)
1043  s->fax_opts = value;
1044  break;
1045  case TIFF_T6OPTIONS:
1046  if (s->compr == TIFF_G4)
1047  s->fax_opts = value;
1048  break;
1049 #define ADD_METADATA(count, name, sep)\
1050  if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1051  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1052  goto end;\
1053  }
1055  ADD_METADATA(count, "ModelPixelScaleTag", NULL);
1056  break;
1058  ADD_METADATA(count, "ModelTransformationTag", NULL);
1059  break;
1060  case TIFF_MODEL_TIEPOINT:
1061  ADD_METADATA(count, "ModelTiepointTag", NULL);
1062  break;
1064  if (s->geotag_count) {
1065  avpriv_request_sample(s->avctx, "Multiple geo key directories\n");
1066  return AVERROR_INVALIDDATA;
1067  }
1068  ADD_METADATA(1, "GeoTIFF_Version", NULL);
1069  ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
1070  s->geotag_count = ff_tget_short(&s->gb, s->le);
1071  if (s->geotag_count > count / 4 - 1) {
1072  s->geotag_count = count / 4 - 1;
1073  av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
1074  }
1075  if ( bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4
1076  || s->geotag_count == 0) {
1077  s->geotag_count = 0;
1078  return -1;
1079  }
1080  s->geotags = av_mallocz_array(s->geotag_count, sizeof(TiffGeoTag));
1081  if (!s->geotags) {
1082  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1083  s->geotag_count = 0;
1084  goto end;
1085  }
1086  for (i = 0; i < s->geotag_count; i++) {
1087  s->geotags[i].key = ff_tget_short(&s->gb, s->le);
1088  s->geotags[i].type = ff_tget_short(&s->gb, s->le);
1089  s->geotags[i].count = ff_tget_short(&s->gb, s->le);
1090 
1091  if (!s->geotags[i].type)
1092  s->geotags[i].val = get_geokey_val(s->geotags[i].key, ff_tget_short(&s->gb, s->le));
1093  else
1094  s->geotags[i].offset = ff_tget_short(&s->gb, s->le);
1095  }
1096  break;
1098  if (count >= INT_MAX / sizeof(int64_t))
1099  return AVERROR_INVALIDDATA;
1100  if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
1101  return AVERROR_INVALIDDATA;
1102  dp = av_malloc_array(count, sizeof(double));
1103  if (!dp) {
1104  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1105  goto end;
1106  }
1107  for (i = 0; i < count; i++)
1108  dp[i] = ff_tget_double(&s->gb, s->le);
1109  for (i = 0; i < s->geotag_count; i++) {
1110  if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
1111  if (s->geotags[i].count == 0
1112  || s->geotags[i].offset + s->geotags[i].count > count) {
1113  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1114  } else if (s->geotags[i].val) {
1115  av_log(s->avctx, AV_LOG_WARNING, "Duplicate GeoTIFF key %d\n", s->geotags[i].key);
1116  } else {
1117  char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
1118  if (!ap) {
1119  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1120  av_freep(&dp);
1121  return AVERROR(ENOMEM);
1122  }
1123  s->geotags[i].val = ap;
1124  }
1125  }
1126  }
1127  av_freep(&dp);
1128  break;
1129  case TIFF_GEO_ASCII_PARAMS:
1130  pos = bytestream2_tell(&s->gb);
1131  for (i = 0; i < s->geotag_count; i++) {
1132  if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
1133  if (s->geotags[i].count == 0
1134  || s->geotags[i].offset + s->geotags[i].count > count) {
1135  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1136  } else {
1137  char *ap;
1138 
1139  bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
1140  if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
1141  return AVERROR_INVALIDDATA;
1142  if (s->geotags[i].val)
1143  return AVERROR_INVALIDDATA;
1144  ap = av_malloc(s->geotags[i].count);
1145  if (!ap) {
1146  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1147  return AVERROR(ENOMEM);
1148  }
1149  bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
1150  ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
1151  s->geotags[i].val = ap;
1152  }
1153  }
1154  }
1155  break;
1156  case TIFF_ARTIST:
1157  ADD_METADATA(count, "artist", NULL);
1158  break;
1159  case TIFF_COPYRIGHT:
1160  ADD_METADATA(count, "copyright", NULL);
1161  break;
1162  case TIFF_DATE:
1163  ADD_METADATA(count, "date", NULL);
1164  break;
1165  case TIFF_DOCUMENT_NAME:
1166  ADD_METADATA(count, "document_name", NULL);
1167  break;
1168  case TIFF_HOST_COMPUTER:
1169  ADD_METADATA(count, "computer", NULL);
1170  break;
1172  ADD_METADATA(count, "description", NULL);
1173  break;
1174  case TIFF_MAKE:
1175  ADD_METADATA(count, "make", NULL);
1176  break;
1177  case TIFF_MODEL:
1178  ADD_METADATA(count, "model", NULL);
1179  break;
1180  case TIFF_PAGE_NAME:
1181  ADD_METADATA(count, "page_name", NULL);
1182  break;
1183  case TIFF_PAGE_NUMBER:
1184  ADD_METADATA(count, "page_number", " / ");
1185  break;
1186  case TIFF_SOFTWARE_NAME:
1187  ADD_METADATA(count, "software", NULL);
1188  break;
1189  default:
1190  if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1192  "Unknown or unsupported tag %d/0X%0X\n",
1193  tag, tag);
1194  return AVERROR_INVALIDDATA;
1195  }
1196  }
1197 end:
1198  if (s->bpp > 64U) {
1200  "This format is not supported (bpp=%d, %d components)\n",
1201  s->bpp, count);
1202  s->bpp = 0;
1203  return AVERROR_INVALIDDATA;
1204  }
1205  bytestream2_seek(&s->gb, start, SEEK_SET);
1206  return 0;
1207 }
1208 
1209 static int decode_frame(AVCodecContext *avctx,
1210  void *data, int *got_frame, AVPacket *avpkt)
1211 {
1212  TiffContext *const s = avctx->priv_data;
1213  AVFrame *const p = data;
1214  ThreadFrame frame = { .f = data };
1215  unsigned off;
1216  int le, ret, plane, planes;
1217  int i, j, entries, stride;
1218  unsigned soff, ssize;
1219  uint8_t *dst;
1220  GetByteContext stripsizes;
1221  GetByteContext stripdata;
1222 
1223  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1224 
1225  // parse image header
1226  if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
1227  av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
1228  return ret;
1229  } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1230  av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1231  return AVERROR_INVALIDDATA;
1232  }
1233  s->le = le;
1234  // TIFF_BPP is not a required tag and defaults to 1
1235  s->bppcount = s->bpp = 1;
1237  s->compr = TIFF_RAW;
1238  s->fill_order = 0;
1239  free_geotags(s);
1240 
1241  // Reset these offsets so we can tell if they were set this frame
1242  s->stripsizesoff = s->strippos = 0;
1243  /* parse image file directory */
1244  bytestream2_seek(&s->gb, off, SEEK_SET);
1245  entries = ff_tget_short(&s->gb, le);
1246  if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
1247  return AVERROR_INVALIDDATA;
1248  for (i = 0; i < entries; i++) {
1249  if ((ret = tiff_decode_tag(s, p)) < 0)
1250  return ret;
1251  }
1252 
1253  for (i = 0; i<s->geotag_count; i++) {
1254  const char *keyname = get_geokey_name(s->geotags[i].key);
1255  if (!keyname) {
1256  av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
1257  continue;
1258  }
1259  if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
1260  av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
1261  continue;
1262  }
1263  ret = av_dict_set(avpriv_frame_get_metadatap(p), keyname, s->geotags[i].val, 0);
1264  if (ret<0) {
1265  av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
1266  return ret;
1267  }
1268  }
1269 
1270  if (!s->strippos && !s->stripoff) {
1271  av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
1272  return AVERROR_INVALIDDATA;
1273  }
1274  /* now we have the data and may start decoding */
1275  if ((ret = init_image(s, &frame)) < 0)
1276  return ret;
1277 
1278  if (s->strips == 1 && !s->stripsize) {
1279  av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
1280  s->stripsize = avpkt->size - s->stripoff;
1281  }
1282 
1283  if (s->stripsizesoff) {
1284  if (s->stripsizesoff >= (unsigned)avpkt->size)
1285  return AVERROR_INVALIDDATA;
1286  bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
1287  avpkt->size - s->stripsizesoff);
1288  }
1289  if (s->strippos) {
1290  if (s->strippos >= (unsigned)avpkt->size)
1291  return AVERROR_INVALIDDATA;
1292  bytestream2_init(&stripdata, avpkt->data + s->strippos,
1293  avpkt->size - s->strippos);
1294  }
1295 
1296  if (s->rps <= 0 || s->rps % s->subsampling[1]) {
1297  av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
1298  return AVERROR_INVALIDDATA;
1299  }
1300 
1301  planes = s->planar ? s->bppcount : 1;
1302  for (plane = 0; plane < planes; plane++) {
1303  stride = p->linesize[plane];
1304  dst = p->data[plane];
1305  for (i = 0; i < s->height; i += s->rps) {
1306  if (i)
1307  dst += s->rps * stride;
1308  if (s->stripsizesoff)
1309  ssize = ff_tget(&stripsizes, s->sstype, le);
1310  else
1311  ssize = s->stripsize;
1312 
1313  if (s->strippos)
1314  soff = ff_tget(&stripdata, s->sot, le);
1315  else
1316  soff = s->stripoff;
1317 
1318  if (soff > avpkt->size || ssize > avpkt->size - soff) {
1319  av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
1320  return AVERROR_INVALIDDATA;
1321  }
1322  if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,
1323  FFMIN(s->rps, s->height - i))) < 0) {
1324  if (avctx->err_recognition & AV_EF_EXPLODE)
1325  return ret;
1326  break;
1327  }
1328  }
1329  if (s->predictor == 2) {
1330  if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1331  av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");
1332  return AVERROR_PATCHWELCOME;
1333  }
1334  dst = p->data[plane];
1335  soff = s->bpp >> 3;
1336  if (s->planar)
1337  soff = FFMAX(soff / s->bppcount, 1);
1338  ssize = s->width * soff;
1339  if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
1342  s->avctx->pix_fmt == AV_PIX_FMT_YA16LE ||
1345  for (i = 0; i < s->height; i++) {
1346  for (j = soff; j < ssize; j += 2)
1347  AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
1348  dst += stride;
1349  }
1350  } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1353  s->avctx->pix_fmt == AV_PIX_FMT_YA16BE ||
1356  for (i = 0; i < s->height; i++) {
1357  for (j = soff; j < ssize; j += 2)
1358  AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
1359  dst += stride;
1360  }
1361  } else {
1362  for (i = 0; i < s->height; i++) {
1363  for (j = soff; j < ssize; j++)
1364  dst[j] += dst[j - soff];
1365  dst += stride;
1366  }
1367  }
1368  }
1369 
1371  dst = p->data[plane];
1372  for (i = 0; i < s->height; i++) {
1373  for (j = 0; j < stride; j++)
1374  dst[j] = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];
1375  dst += stride;
1376  }
1377  }
1378  }
1379 
1380  if (s->planar && s->bppcount > 2) {
1381  FFSWAP(uint8_t*, p->data[0], p->data[2]);
1382  FFSWAP(int, p->linesize[0], p->linesize[2]);
1383  FFSWAP(uint8_t*, p->data[0], p->data[1]);
1384  FFSWAP(int, p->linesize[0], p->linesize[1]);
1385  }
1386 
1387  *got_frame = 1;
1388 
1389  return avpkt->size;
1390 }
1391 
1393 {
1394  TiffContext *s = avctx->priv_data;
1395 
1396  s->width = 0;
1397  s->height = 0;
1398  s->subsampling[0] =
1399  s->subsampling[1] = 1;
1400  s->avctx = avctx;
1401  ff_lzw_decode_open(&s->lzw);
1403 
1404  return 0;
1405 }
1406 
1407 static av_cold int tiff_end(AVCodecContext *avctx)
1408 {
1409  TiffContext *const s = avctx->priv_data;
1410 
1411  free_geotags(s);
1412 
1413  ff_lzw_decode_close(&s->lzw);
1414  av_freep(&s->deinvert_buf);
1415  return 0;
1416 }
1417 
1419  .name = "tiff",
1420  .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
1421  .type = AVMEDIA_TYPE_VIDEO,
1422  .id = AV_CODEC_ID_TIFF,
1423  .priv_data_size = sizeof(TiffContext),
1424  .init = tiff_init,
1425  .close = tiff_end,
1426  .decode = decode_frame,
1428  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1429 };
Definition: tiff.h:54
int plane
Definition: avisynth_c.h:422
int ff_lzw_decode(LZWState *p, uint8_t *buf, int len)
Decode given number of bytes NOTE: the algorithm here is inspired from the LZW GIF decoder written by...
Definition: lzw.c:166
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
Definition: tiff.h:89
int offset
Definition: tiff.h:178
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
const TiffGeoTagKeyName ff_tiff_projection_codes[]
Definition: tiff_data.c:1497
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2333
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
TiffPhotometric
Definition: tiff.h:150
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int fill_order
Definition: tiff.c:65
unsigned int bpp
Definition: tiff.c:55
8 bits gray, 8 bits alpha
Definition: pixfmt.h:154
int geotag_count
Definition: tiff.c:78
uint32_t res[4]
Definition: tiff.c:66
Definition: tiff.h:53
int sstype
Definition: tiff.c:68
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static int init_thread_copy(AVCodecContext *avctx)
Definition: tta.c:392
AVFrame * f
Definition: thread.h:36
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:64
Definition: tiff.h:99
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:210
int ff_tadd_doubles_metadata(int count, const char *name, const char *sep, GetByteContext *gb, int le, AVDictionary **metadata)
Adds count doubles converted to a string into the metadata dictionary.
Definition: tiff_common.c:147
const char *const name
Definition: tiff.h:184
const char * desc
Definition: nvenc.c:60
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
const uint8_t ff_reverse[256]
Definition: reverse.c:23
Definition: tiff.h:47
TIFF tables.
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:180
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1658
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:2143
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:1960
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
static void free_geotags(TiffContext *const s)
Definition: tiff.c:82
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:120
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
#define src
Definition: vp8dsp.c:254
unsigned int yuv_line_size
Definition: tiff.c:76
AVCodec.
Definition: avcodec.h:3681
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:1209
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:789
static const uint8_t type_sizes[14]
sizes of various TIFF field types (string size = 100)
Definition: tiff_common.h:54
int deinvert_buf_size
Definition: tiff.c:74
av_cold void ff_ccitt_unpack_init(void)
initialize unpacker code
Definition: faxcompr.c:99
planar GBRA 4:4:4:4 64bpp, big-endian
Definition: pixfmt.h:230
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
char * val
Definition: tiff.h:179
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
#define TIFF_GEO_KEY_USER_DEFINED
Definition: tiff_data.h:48
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as lit...
Definition: pixfmt.h:111
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
Multithreading support functions.
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: pixfmt.h:217
static av_cold int tiff_init(AVCodecContext *avctx)
Definition: tiff.c:1392
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1657
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:1413
static int add_metadata(int count, int type, const char *name, const char *sep, TiffContext *s, AVFrame *frame)
Definition: tiff.c:226
int stripoff
Definition: tiff.c:70
Definition: tiff.h:94
ptrdiff_t size
Definition: opengl_enc.c:101
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
LZWState * lzw
Definition: tiff.c:71
#define AV_WB16(p, v)
Definition: intreadwrite.h:410
#define av_log(a,...)
Definition: tiff.h:68
int planar
Definition: tiff.c:61
#define U(x)
Definition: vp56_arith.h:37
Definition: lzw.c:46
int height
Definition: tiff.c:54
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
16 bits gray, 16 bits alpha (big-endian)
Definition: pixfmt.h:226
enum TiffGeoTagKey key
Definition: tiff.h:175
int sot
Definition: tiff.c:69
#define AVERROR(e)
Definition: error.h:43
TIFF data tables.
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:148
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
#define pv
Definition: regdef.h:60
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
static av_cold int tiff_end(AVCodecContext *avctx)
Definition: tiff.c:1407
unsigned ff_tget_short(GetByteContext *gb, int le)
Reads a short from the bytestream using given endianness.
Definition: tiff_common.c:43
const char * r
Definition: vf_curves.c:111
static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
Definition: tiff.c:277
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
unsigned ff_tget(GetByteContext *gb, int type, int le)
Reads a byte from the bytestream using given endianness.
Definition: tiff_common.c:62
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
Definition: graph2dot.c:48
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
Definition: tiff.c:770
int width
Definition: tiff.c:54
static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride, const uint8_t *src, int size, int width, int lines)
Definition: tiff.c:450
int strips
Definition: tiff.c:68
int ff_ccitt_unpack(AVCodecContext *avctx, const uint8_t *src, int srcsize, uint8_t *dst, int height, int stride, enum TiffCompr compr, int opts)
unpack data compressed with CCITT Group 3 1/2-D or Group 4 method
Definition: faxcompr.c:381
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
uint8_t * yuv_line
Definition: tiff.c:75
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1057
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
int predictor
Definition: tiff.c:64
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:218
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:3020
#define FFMIN(a, b)
Definition: common.h:96
int le
Definition: tiff.c:58
#define width
int width
picture width / height.
Definition: avcodec.h:1919
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:3031
int n
Definition: avisynth_c.h:684
#define FF_ARRAY_ELEMS(a)
uint8_t le
Definition: crc.c:295
#define TIFF_GEO_KEY_UNDEFINED
Definition: tiff_data.h:47
int palette_is_set
Definition: tiff.c:57
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static av_always_inline int bytestream2_seek_p(PutByteContext *p, int offset, int whence)
Definition: bytestream.h:232
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:251
uint32_t palette[256]
Definition: tiff.c:56
Definition: tiff.h:41
static const char * get_geokey_name(int key)
Definition: tiff.c:98
TiffCompr
list of TIFF compression types
Definition: tiff.h:88
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:237
Libavcodec external API header.
AVDictionary ** avpriv_frame_get_metadatap(AVFrame *frame)
Definition: frame.c:51
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:218
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:1732
Definition: tiff.h:91
void * buf
Definition: avisynth_c.h:690
GLint GLenum type
Definition: opengl_enc.c:105
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
AVCodecContext * avctx
Definition: tiff.c:51
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:68
Y , 16bpp, big-endian.
Definition: pixfmt.h:98
Rational number (pair of numerator and denominator).
Definition: rational.h:58
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:201
int count
Definition: tiff.h:177
enum TiffTags type
Definition: tiff.h:176
LZW decoding routines.
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:72
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
int stripsizesoff
Definition: tiff.c:70
Y , 8bpp.
Definition: pixfmt.h:70
uint8_t * deinvert_buf
Definition: tiff.c:73
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:282
static char * get_geokey_val(int key, int val)
Definition: tiff.c:132
static av_always_inline unsigned int bytestream2_get_eof(PutByteContext *p)
Definition: bytestream.h:328
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:229
static double c[64]
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:110
static void av_always_inline horizontal_fill(unsigned int bpp, uint8_t *dst, int usePtr, const uint8_t *src, uint8_t c, int width, int offset)
Definition: tiff.c:237
#define AV_WL16(p, v)
Definition: intreadwrite.h:417
static char * doubles2str(double *dp, int count, const char *sep)
Definition: tiff.c:200
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:69
int den
Denominator.
Definition: rational.h:60
int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
Initialize LZW decoder.
Definition: lzw.c:128
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define RET_GEOKEY_VAL(TYPE, array)
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:769
void * priv_data
Definition: avcodec.h:1774
#define av_free(p)
int pixels
Definition: avisynth_c.h:429
int ff_tadd_string_metadata(int count, const char *name, GetByteContext *gb, int le, AVDictionary **metadata)
Adds a string of count characters into the metadata dictionary.
Definition: tiff_common.c:241
int len
Y , 16bpp, little-endian.
Definition: pixfmt.h:99
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:208
16 bits gray, 16 bits alpha (little-endian)
Definition: pixfmt.h:227
int fax_opts
Definition: tiff.c:63
int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type, unsigned *count, int *next)
Reads the first 3 fields of a TIFF tag, which are the tag id, the tag type and the count of values fo...
Definition: tiff_common.c:286
#define RET_GEOKEY(TYPE, array, element)
Definition: tiff.c:93
int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
Decodes a TIFF header from the input bytestream and sets the endianness in *le and the offset to the ...
Definition: tiff_common.c:261
#define av_freep(p)
static int init_image(TiffContext *s, ThreadFrame *frame)
Definition: tiff.c:652
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
void INT64 start
Definition: avisynth_c.h:690
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2257
AVCodec ff_tiff_decoder
Definition: tiff.c:1418
#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:1634
#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:994
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