FFmpeg
 All Data Structures 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 "avcodec.h"
28 #include "bytestream.h"
29 #include "config.h"
30 #if CONFIG_ZLIB
31 #include <zlib.h>
32 #endif
33 #include "lzw.h"
34 #include "tiff.h"
35 #include "tiff_data.h"
36 #include "faxcompr.h"
37 #include "internal.h"
38 #include "mathops.h"
39 #include "libavutil/attributes.h"
40 #include "libavutil/intreadwrite.h"
41 #include "libavutil/imgutils.h"
42 #include "libavutil/avstring.h"
43 
44 typedef struct TiffContext {
48 
49  int width, height;
50  unsigned int bpp, bppcount;
51  uint32_t palette[256];
53  int le;
55  int invert;
56  int fax_opts;
57  int predictor;
59 
60  int strips, rps, sstype;
61  int sot;
64 
67 
70 } TiffContext;
71 
72 static unsigned tget_short(GetByteContext *gb, int le)
73 {
74  unsigned v = le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
75  return v;
76 }
77 
78 static unsigned tget_long(GetByteContext *gb, int le)
79 {
80  unsigned v = le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
81  return v;
82 }
83 
84 static double tget_double(GetByteContext *gb, int le)
85 {
86  av_alias64 i = { .u64 = le ? bytestream2_get_le64(gb) : bytestream2_get_be64(gb)};
87  return i.f64;
88 }
89 
90 static unsigned tget(GetByteContext *gb, int type, int le)
91 {
92  switch (type) {
93  case TIFF_BYTE : return bytestream2_get_byte(gb);
94  case TIFF_SHORT: return tget_short(gb, le);
95  case TIFF_LONG : return tget_long(gb, le);
96  default : return UINT_MAX;
97  }
98 }
99 
100 static void free_geotags(TiffContext *const s)
101 {
102  int i;
103  for (i = 0; i < s->geotag_count; i++) {
104  if (s->geotags[i].val)
105  av_freep(&s->geotags[i].val);
106  }
107  av_freep(&s->geotags);
108 }
109 
110 #define RET_GEOKEY(TYPE, array, element)\
111  if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
112  key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_name_type_map))\
113  return ff_tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
114 
115 static const char *get_geokey_name(int key)
116 {
117  RET_GEOKEY(VERT, vert, name);
118  RET_GEOKEY(PROJ, proj, name);
119  RET_GEOKEY(GEOG, geog, name);
120  RET_GEOKEY(CONF, conf, name);
121 
122  return NULL;
123 }
124 
125 static int get_geokey_type(int key)
126 {
127  RET_GEOKEY(VERT, vert, type);
128  RET_GEOKEY(PROJ, proj, type);
129  RET_GEOKEY(GEOG, geog, type);
130  RET_GEOKEY(CONF, conf, type);
131 
132  return AVERROR_INVALIDDATA;
133 }
134 
135 static int cmp_id_key(const void *id, const void *k)
136 {
137  return *(const int*)id - ((const TiffGeoTagKeyName*)k)->key;
138 }
139 
140 static const char *search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
141 {
142  return ((TiffGeoTagKeyName*)bsearch(&id, keys, n, sizeof(keys[0]), cmp_id_key))->name;
143 }
144 
145 static char *get_geokey_val(int key, int val)
146 {
147  char *ap;
148 
149  if (val == TIFF_GEO_KEY_UNDEFINED)
150  return av_strdup("undefined");
151  if (val == TIFF_GEO_KEY_USER_DEFINED)
152  return av_strdup("User-Defined");
153 
154 #define RET_GEOKEY_VAL(TYPE, array)\
155  if (val >= TIFF_##TYPE##_OFFSET &&\
156  val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_codes))\
157  return av_strdup(ff_tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]);
158 
159  switch (key) {
161  RET_GEOKEY_VAL(GT_MODEL_TYPE, gt_model_type);
162  break;
164  RET_GEOKEY_VAL(GT_RASTER_TYPE, gt_raster_type);
165  break;
169  RET_GEOKEY_VAL(LINEAR_UNIT, linear_unit);
170  break;
173  RET_GEOKEY_VAL(ANGULAR_UNIT, angular_unit);
174  break;
176  RET_GEOKEY_VAL(GCS_TYPE, gcs_type);
177  RET_GEOKEY_VAL(GCSE_TYPE, gcse_type);
178  break;
180  RET_GEOKEY_VAL(GEODETIC_DATUM, geodetic_datum);
181  RET_GEOKEY_VAL(GEODETIC_DATUM_E, geodetic_datum_e);
182  break;
184  RET_GEOKEY_VAL(ELLIPSOID, ellipsoid);
185  break;
187  RET_GEOKEY_VAL(PRIME_MERIDIAN, prime_meridian);
188  break;
191  break;
194  break;
196  RET_GEOKEY_VAL(COORD_TRANS, coord_trans);
197  break;
199  RET_GEOKEY_VAL(VERT_CS, vert_cs);
200  RET_GEOKEY_VAL(ORTHO_VERT_CS, ortho_vert_cs);
201  break;
202 
203  }
204 
205  ap = av_malloc(14);
206  if (ap)
207  snprintf(ap, 14, "Unknown-%d", val);
208  return ap;
209 }
210 
211 static char *doubles2str(double *dp, int count, const char *sep)
212 {
213  int i;
214  char *ap, *ap0;
215  uint64_t component_len;
216  if (!sep) sep = ", ";
217  component_len = 15LL + strlen(sep);
218  if (count >= (INT_MAX - 1)/component_len)
219  return NULL;
220  ap = av_malloc(component_len * count + 1);
221  if (!ap)
222  return NULL;
223  ap0 = ap;
224  ap[0] = '\0';
225  for (i = 0; i < count; i++) {
226  unsigned l = snprintf(ap, component_len, "%f%s", dp[i], sep);
227  if(l >= component_len) {
228  av_free(ap0);
229  return NULL;
230  }
231  ap += l;
232  }
233  ap0[strlen(ap0) - strlen(sep)] = '\0';
234  return ap0;
235 }
236 
237 static char *shorts2str(int16_t *sp, int count, const char *sep)
238 {
239  int i;
240  char *ap, *ap0;
241  uint64_t component_len;
242  if (!sep) sep = ", ";
243  component_len = 7LL + strlen(sep);
244  if (count >= (INT_MAX - 1)/component_len)
245  return NULL;
246  ap = av_malloc(component_len * count + 1);
247  if (!ap)
248  return NULL;
249  ap0 = ap;
250  ap[0] = '\0';
251  for (i = 0; i < count; i++) {
252  unsigned l = snprintf(ap, component_len, "%d%s", sp[i], sep);
253  if (l >= component_len) {
254  av_free(ap0);
255  return NULL;
256  }
257  ap += l;
258  }
259  ap0[strlen(ap0) - strlen(sep)] = '\0';
260  return ap0;
261 }
262 
263 static int add_doubles_metadata(int count,
264  const char *name, const char *sep,
265  TiffContext *s)
266 {
267  char *ap;
268  int i;
269  double *dp;
270 
271  if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
272  return AVERROR_INVALIDDATA;
273  if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
274  return AVERROR_INVALIDDATA;
275 
276  dp = av_malloc(count * sizeof(double));
277  if (!dp)
278  return AVERROR(ENOMEM);
279 
280  for (i = 0; i < count; i++)
281  dp[i] = tget_double(&s->gb, s->le);
282  ap = doubles2str(dp, count, sep);
283  av_freep(&dp);
284  if (!ap)
285  return AVERROR(ENOMEM);
287  return 0;
288 }
289 
290 static int add_shorts_metadata(int count, const char *name,
291  const char *sep, TiffContext *s)
292 {
293  char *ap;
294  int i;
295  int16_t *sp;
296 
297  if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
298  return AVERROR_INVALIDDATA;
299  if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int16_t))
300  return AVERROR_INVALIDDATA;
301 
302  sp = av_malloc(count * sizeof(int16_t));
303  if (!sp)
304  return AVERROR(ENOMEM);
305 
306  for (i = 0; i < count; i++)
307  sp[i] = tget_short(&s->gb, s->le);
308  ap = shorts2str(sp, count, sep);
309  av_freep(&sp);
310  if (!ap)
311  return AVERROR(ENOMEM);
313  return 0;
314 }
315 
316 static int add_string_metadata(int count, const char *name,
317  TiffContext *s)
318 {
319  char *value;
320 
321  if (bytestream2_get_bytes_left(&s->gb) < count)
322  return AVERROR_INVALIDDATA;
323 
324  value = av_malloc(count + 1);
325  if (!value)
326  return AVERROR(ENOMEM);
327 
328  bytestream2_get_bufferu(&s->gb, value, count);
329  value[count] = 0;
330 
332  return 0;
333 }
334 
335 static int add_metadata(int count, int type,
336  const char *name, const char *sep, TiffContext *s)
337 {
338  switch(type) {
339  case TIFF_DOUBLE: return add_doubles_metadata(count, name, sep, s);
340  case TIFF_SHORT : return add_shorts_metadata(count, name, sep, s);
341  case TIFF_STRING: return add_string_metadata(count, name, s);
342  default : return AVERROR_INVALIDDATA;
343  };
344 }
345 
346 #if CONFIG_ZLIB
347 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
348  int size)
349 {
350  z_stream zstream = { 0 };
351  int zret;
352 
353  zstream.next_in = (uint8_t *)src;
354  zstream.avail_in = size;
355  zstream.next_out = dst;
356  zstream.avail_out = *len;
357  zret = inflateInit(&zstream);
358  if (zret != Z_OK) {
359  av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
360  return zret;
361  }
362  zret = inflate(&zstream, Z_SYNC_FLUSH);
363  inflateEnd(&zstream);
364  *len = zstream.total_out;
365  return zret == Z_STREAM_END ? Z_OK : zret;
366 }
367 #endif
368 
369 static void av_always_inline horizontal_fill(unsigned int bpp, uint8_t* dst,
370  int usePtr, const uint8_t *src,
371  uint8_t c, int width, int offset)
372 {
373  switch (bpp) {
374  case 1:
375  while (--width >= 0) {
376  dst[(width+offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
377  dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
378  dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
379  dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
380  dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
381  dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
382  dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
383  dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
384  }
385  break;
386  case 2:
387  while (--width >= 0) {
388  dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
389  dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
390  dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
391  dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
392  }
393  break;
394  case 4:
395  while (--width >= 0) {
396  dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
397  dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
398  }
399  break;
400  default:
401  if (usePtr) {
402  memcpy(dst + offset, src, width);
403  } else {
404  memset(dst + offset, c, width);
405  }
406  }
407 }
408 
409 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
410  const uint8_t *src, int size, int lines)
411 {
412  int c, line, pixels, code;
413  const uint8_t *ssrc = src;
414  int width = ((s->width * s->bpp) + 7) >> 3;
415 
416  if (size <= 0)
417  return AVERROR_INVALIDDATA;
418 
419 #if CONFIG_ZLIB
420  if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
421  uint8_t *src2 = NULL, *zbuf;
422  unsigned long outlen;
423  int i, ret;
424  outlen = width * lines;
425  zbuf = av_malloc(outlen);
426  if (!zbuf)
427  return AVERROR(ENOMEM);
428  if (s->fill_order) {
429  src2 = av_malloc((unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE);
430  if (!src2) {
431  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
432  av_free(zbuf);
433  return AVERROR(ENOMEM);
434  }
435  for (i = 0; i < size; i++)
436  src2[i] = ff_reverse[src[i]];
437  memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
438  src = src2;
439  }
440  ret = tiff_uncompress(zbuf, &outlen, src, size);
441  if (ret != Z_OK) {
443  "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
444  (unsigned long)width * lines, ret);
445  av_free(src2);
446  av_free(zbuf);
447  return -1;
448  }
449  src = zbuf;
450  for (line = 0; line < lines; line++) {
451  if(s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8){
452  horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
453  }else{
454  memcpy(dst, src, width);
455  }
456  dst += stride;
457  src += width;
458  }
459  av_free(src2);
460  av_free(zbuf);
461  return 0;
462  }
463 #endif
464  if (s->compr == TIFF_LZW) {
465  if (s->fill_order) {
466  int i;
468  if (!s->deinvert_buf)
469  return AVERROR(ENOMEM);
470  for (i = 0; i < size; i++)
471  s->deinvert_buf[i] = ff_reverse[src[i]];
472  src = s->deinvert_buf;
473  ssrc = src;
474  }
475  if (size > 1 && !src[0] && (src[1]&1)) {
476  av_log(s->avctx, AV_LOG_ERROR, "Old style LZW is unsupported\n");
477  }
478  if (ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0) {
479  av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
480  return -1;
481  }
482  }
483  if (s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3
484  || s->compr == TIFF_G4) {
485  int i, ret = 0;
486  uint8_t *src2 = av_malloc((unsigned)size +
488 
489  if (!src2) {
491  "Error allocating temporary buffer\n");
492  return AVERROR(ENOMEM);
493  }
494  if (s->fax_opts & 2) {
496  "Uncompressed fax mode is not supported (yet)\n");
497  av_free(src2);
498  return -1;
499  }
500  if (!s->fill_order) {
501  memcpy(src2, src, size);
502  } else {
503  for (i = 0; i < size; i++)
504  src2[i] = ff_reverse[src[i]];
505  }
506  memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
507  switch (s->compr) {
508  case TIFF_CCITT_RLE:
509  case TIFF_G3:
510  case TIFF_G4:
511  ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
512  s->compr, s->fax_opts);
513  break;
514  }
515  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
516  for (line = 0; line < lines; line++) {
517  horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
518  dst += stride;
519  }
520  av_free(src2);
521  return ret;
522  }
523  for (line = 0; line < lines; line++) {
524  if (src - ssrc > size) {
525  av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
526  return -1;
527  }
528  switch (s->compr) {
529  case TIFF_RAW:
530  if (ssrc + size - src < width)
531  return AVERROR_INVALIDDATA;
532  if (!s->fill_order) {
534  dst, 1, src, 0, width, 0);
535  } else {
536  int i;
537  for (i = 0; i < width; i++)
538  dst[i] = ff_reverse[src[i]];
539  }
540  src += width;
541  break;
542  case TIFF_PACKBITS:
543  for (pixels = 0; pixels < width;) {
544  if (ssrc + size - src < 2) {
545  av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
546  return AVERROR_INVALIDDATA;
547  }
548  code = (int8_t) * src++;
549  if (code >= 0) {
550  code++;
551  if (pixels + code > width) {
553  "Copy went out of bounds\n");
554  return -1;
555  }
556  if (ssrc + size - src < code) {
557  av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
558  return AVERROR_INVALIDDATA;
559  }
561  dst, 1, src, 0, code, pixels);
562  src += code;
563  pixels += code;
564  } else if (code != -128) { // -127..-1
565  code = (-code) + 1;
566  if (pixels + code > width) {
568  "Run went out of bounds\n");
569  return -1;
570  }
571  c = *src++;
573  dst, 0, NULL, c, code, pixels);
574  pixels += code;
575  }
576  }
577  break;
578  case TIFF_LZW:
579  pixels = ff_lzw_decode(s->lzw, dst, width);
580  if (pixels < width) {
581  av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
582  pixels, width);
583  return -1;
584  }
585  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
586  horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
587  break;
588  }
589  dst += stride;
590  }
591  return 0;
592 }
593 
594 static int init_image(TiffContext *s)
595 {
596  int i, ret;
597  uint32_t *pal;
598 
599  switch (s->bpp * 10 + s->bppcount) {
600  case 11:
601  if (!s->palette_is_set) {
603  break;
604  }
605  case 21:
606  case 41:
607  case 81:
609  break;
610  case 243:
612  break;
613  case 161:
615  break;
616  case 162:
618  break;
619  case 324:
621  break;
622  case 483:
624  break;
625  case 644:
627  break;
628  default:
630  "This format is not supported (bpp=%d, bppcount=%d)\n",
631  s->bpp, s->bppcount);
632  return AVERROR_INVALIDDATA;
633  }
634  if (s->width != s->avctx->width || s->height != s->avctx->height) {
635  if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
636  return ret;
638  }
639  if (s->picture.data[0])
640  s->avctx->release_buffer(s->avctx, &s->picture);
641  if ((ret = ff_get_buffer(s->avctx, &s->picture)) < 0) {
642  av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
643  return ret;
644  }
645  if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
646  if (s->palette_is_set) {
647  memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
648  } else {
649  /* make default grayscale pal */
650  pal = (uint32_t *) s->picture.data[1];
651  for (i = 0; i < 1<<s->bpp; i++)
652  pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
653  }
654  }
655  return 0;
656 }
657 
659 {
660  unsigned tag, type, count, off, value = 0;
661  int i, j, k, pos, start;
662  int ret;
663  uint32_t *pal;
664  double *dp;
665 
666  tag = tget_short(&s->gb, s->le);
667  type = tget_short(&s->gb, s->le);
668  count = tget_long(&s->gb, s->le);
669  off = tget_long(&s->gb, s->le);
670  start = bytestream2_tell(&s->gb);
671 
672  if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
673  av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
674  type);
675  return 0;
676  }
677 
678  if (count == 1) {
679  switch (type) {
680  case TIFF_BYTE:
681  case TIFF_SHORT:
682  bytestream2_seek(&s->gb, -4, SEEK_CUR);
683  value = tget(&s->gb, type, s->le);
684  break;
685  case TIFF_LONG:
686  value = off;
687  break;
688  case TIFF_STRING:
689  if (count <= 4) {
690  bytestream2_seek(&s->gb, -4, SEEK_CUR);
691  break;
692  }
693  default:
694  value = UINT_MAX;
695  bytestream2_seek(&s->gb, off, SEEK_SET);
696  }
697  } else {
698  if (count <= 4 && type_sizes[type] * count <= 4) {
699  bytestream2_seek(&s->gb, -4, SEEK_CUR);
700  } else {
701  bytestream2_seek(&s->gb, off, SEEK_SET);
702  }
703  }
704 
705  switch (tag) {
706  case TIFF_WIDTH:
707  s->width = value;
708  break;
709  case TIFF_HEIGHT:
710  s->height = value;
711  break;
712  case TIFF_BPP:
713  s->bppcount = count;
714  if (count > 4) {
716  "This format is not supported (bpp=%d, %d components)\n",
717  s->bpp, count);
718  return -1;
719  }
720  if (count == 1)
721  s->bpp = value;
722  else {
723  switch (type) {
724  case TIFF_BYTE:
725  s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
726  ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
727  break;
728  case TIFF_SHORT:
729  case TIFF_LONG:
730  s->bpp = 0;
731  if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
732  return -1;
733  for (i = 0; i < count; i++)
734  s->bpp += tget(&s->gb, type, s->le);
735  break;
736  default:
737  s->bpp = -1;
738  }
739  }
740  break;
742  if (count != 1) {
744  "Samples per pixel requires a single value, many provided\n");
745  return AVERROR_INVALIDDATA;
746  }
747  if (s->bppcount == 1)
748  s->bpp *= value;
749  s->bppcount = value;
750  break;
751  case TIFF_COMPR:
752  s->compr = value;
753  s->predictor = 0;
754  switch (s->compr) {
755  case TIFF_RAW:
756  case TIFF_PACKBITS:
757  case TIFF_LZW:
758  case TIFF_CCITT_RLE:
759  break;
760  case TIFF_G3:
761  case TIFF_G4:
762  s->fax_opts = 0;
763  break;
764  case TIFF_DEFLATE:
765  case TIFF_ADOBE_DEFLATE:
766 #if CONFIG_ZLIB
767  break;
768 #else
769  av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
770  return -1;
771 #endif
772  case TIFF_JPEG:
773  case TIFF_NEWJPEG:
775  "JPEG compression is not supported\n");
776  return -1;
777  default:
778  av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
779  s->compr);
780  return -1;
781  }
782  break;
783  case TIFF_ROWSPERSTRIP:
784  if (type == TIFF_LONG && value == UINT_MAX)
785  value = s->height;
786  if (value < 1) {
788  "Incorrect value of rows per strip\n");
789  return -1;
790  }
791  s->rps = value;
792  break;
793  case TIFF_STRIP_OFFS:
794  if (count == 1) {
795  s->strippos = 0;
796  s->stripoff = value;
797  } else
798  s->strippos = off;
799  s->strips = count;
800  if (s->strips == 1)
801  s->rps = s->height;
802  s->sot = type;
803  if (s->strippos > bytestream2_size(&s->gb)) {
805  "Tag referencing position outside the image\n");
806  return -1;
807  }
808  break;
809  case TIFF_STRIP_SIZE:
810  if (count == 1) {
811  s->stripsizesoff = 0;
812  s->stripsize = value;
813  s->strips = 1;
814  } else {
815  s->stripsizesoff = off;
816  }
817  s->strips = count;
818  s->sstype = type;
819  if (s->stripsizesoff > bytestream2_size(&s->gb)) {
821  "Tag referencing position outside the image\n");
822  return -1;
823  }
824  break;
826  case TIFF_TILE_LENGTH:
827  case TIFF_TILE_OFFSETS:
828  case TIFF_TILE_WIDTH:
829  av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");
830  return AVERROR_PATCHWELCOME;
831  break;
832  case TIFF_PREDICTOR:
833  s->predictor = value;
834  break;
835  case TIFF_INVERT:
836  switch (value) {
837  case 0:
838  s->invert = 1;
839  break;
840  case 1:
841  s->invert = 0;
842  break;
843  case 2:
844  case 3:
845  break;
846  default:
847  av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
848  value);
849  return -1;
850  }
851  break;
852  case TIFF_FILL_ORDER:
853  if (value < 1 || value > 2) {
855  "Unknown FillOrder value %d, trying default one\n", value);
856  value = 1;
857  }
858  s->fill_order = value - 1;
859  break;
860  case TIFF_PAL:
861  pal = (uint32_t *) s->palette;
862  off = type_sizes[type];
863  if (count / 3 > 256 || bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
864  return -1;
865  off = (type_sizes[type] - 1) << 3;
866  for (k = 2; k >= 0; k--) {
867  for (i = 0; i < count / 3; i++) {
868  if (k == 2)
869  pal[i] = 0xFFU << 24;
870  j = (tget(&s->gb, type, s->le) >> off) << (k * 8);
871  pal[i] |= j;
872  }
873  }
874  s->palette_is_set = 1;
875  break;
876  case TIFF_PLANAR:
877  if (value == 2) {
878  av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
879  return -1;
880  }
881  break;
882  case TIFF_T4OPTIONS:
883  if (s->compr == TIFF_G3)
884  s->fax_opts = value;
885  break;
886  case TIFF_T6OPTIONS:
887  if (s->compr == TIFF_G4)
888  s->fax_opts = value;
889  break;
890 #define ADD_METADATA(count, name, sep)\
891  if ((ret = add_metadata(count, type, name, sep, s)) < 0) {\
892  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
893  return ret;\
894  }
896  ADD_METADATA(count, "ModelPixelScaleTag", NULL);
897  break;
899  ADD_METADATA(count, "ModelTransformationTag", NULL);
900  break;
901  case TIFF_MODEL_TIEPOINT:
902  ADD_METADATA(count, "ModelTiepointTag", NULL);
903  break;
905  ADD_METADATA(1, "GeoTIFF_Version", NULL);
906  ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
907  s->geotag_count = tget_short(&s->gb, s->le);
908  if (s->geotag_count > count / 4 - 1) {
909  s->geotag_count = count / 4 - 1;
910  av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
911  }
912  if (bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4)
913  return -1;
914  s->geotags = av_mallocz(sizeof(TiffGeoTag) * s->geotag_count);
915  if (!s->geotags) {
916  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
917  return AVERROR(ENOMEM);
918  }
919  for (i = 0; i < s->geotag_count; i++) {
920  s->geotags[i].key = tget_short(&s->gb, s->le);
921  s->geotags[i].type = tget_short(&s->gb, s->le);
922  s->geotags[i].count = tget_short(&s->gb, s->le);
923 
924  if (!s->geotags[i].type)
925  s->geotags[i].val = get_geokey_val(s->geotags[i].key, tget_short(&s->gb, s->le));
926  else
927  s->geotags[i].offset = tget_short(&s->gb, s->le);
928  }
929  break;
931  if (count >= INT_MAX / sizeof(int64_t))
932  return AVERROR_INVALIDDATA;
933  if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
934  return AVERROR_INVALIDDATA;
935  dp = av_malloc(count * sizeof(double));
936  if (!dp) {
937  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
938  return AVERROR(ENOMEM);
939  }
940  for (i = 0; i < count; i++)
941  dp[i] = tget_double(&s->gb, s->le);
942  for (i = 0; i < s->geotag_count; i++) {
943  if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
944  if (s->geotags[i].count == 0
945  || s->geotags[i].offset + s->geotags[i].count > count) {
946  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
947  } else {
948  char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
949  if (!ap) {
950  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
951  av_freep(&dp);
952  return AVERROR(ENOMEM);
953  }
954  s->geotags[i].val = ap;
955  }
956  }
957  }
958  av_freep(&dp);
959  break;
961  pos = bytestream2_tell(&s->gb);
962  for (i = 0; i < s->geotag_count; i++) {
963  if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
964  if (s->geotags[i].count == 0
965  || s->geotags[i].offset + s->geotags[i].count > count) {
966  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
967  } else {
968  char *ap;
969 
970  bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
971  if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
972  return -1;
973  ap = av_malloc(s->geotags[i].count);
974  if (!ap) {
975  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
976  return AVERROR(ENOMEM);
977  }
978  bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
979  ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
980  s->geotags[i].val = ap;
981  }
982  }
983  }
984  break;
985  case TIFF_ARTIST:
986  ADD_METADATA(count, "artist", NULL);
987  break;
988  case TIFF_COPYRIGHT:
989  ADD_METADATA(count, "copyright", NULL);
990  break;
991  case TIFF_DATE:
992  ADD_METADATA(count, "date", NULL);
993  break;
994  case TIFF_DOCUMENT_NAME:
995  ADD_METADATA(count, "document_name", NULL);
996  break;
997  case TIFF_HOST_COMPUTER:
998  ADD_METADATA(count, "computer", NULL);
999  break;
1001  ADD_METADATA(count, "description", NULL);
1002  break;
1003  case TIFF_MAKE:
1004  ADD_METADATA(count, "make", NULL);
1005  break;
1006  case TIFF_MODEL:
1007  ADD_METADATA(count, "model", NULL);
1008  break;
1009  case TIFF_PAGE_NAME:
1010  ADD_METADATA(count, "page_name", NULL);
1011  break;
1012  case TIFF_PAGE_NUMBER:
1013  ADD_METADATA(count, "page_number", " / ");
1014  break;
1015  case TIFF_SOFTWARE_NAME:
1016  ADD_METADATA(count, "software", NULL);
1017  break;
1018  default:
1019  av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",
1020  tag, tag);
1021  }
1022  bytestream2_seek(&s->gb, start, SEEK_SET);
1023  return 0;
1024 }
1025 
1026 static int decode_frame(AVCodecContext *avctx,
1027  void *data, int *got_frame, AVPacket *avpkt)
1028 {
1029  TiffContext *const s = avctx->priv_data;
1030  AVFrame *picture = data;
1031  AVFrame *const p = &s->picture;
1032  unsigned off;
1033  int id, le, ret;
1034  int i, j, entries;
1035  int stride;
1036  unsigned soff, ssize;
1037  uint8_t *dst;
1038  GetByteContext stripsizes;
1039  GetByteContext stripdata;
1040 
1041  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1042 
1043  //parse image header
1044  if (avpkt->size < 8)
1045  return AVERROR_INVALIDDATA;
1046  id = bytestream2_get_le16u(&s->gb);
1047  if (id == 0x4949)
1048  le = 1;
1049  else if (id == 0x4D4D)
1050  le = 0;
1051  else {
1052  av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
1053  return -1;
1054  }
1055  s->le = le;
1056  // TIFF_BPP is not a required tag and defaults to 1
1057  s->bppcount = s->bpp = 1;
1058  s->invert = 0;
1059  s->compr = TIFF_RAW;
1060  s->fill_order = 0;
1061  free_geotags(s);
1062  /* metadata has been destroyed from lavc internals, that pointer is not
1063  * valid anymore */
1064  s->picture.metadata = NULL;
1065 
1066  // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
1067  // that further identifies the file as a TIFF file"
1068  if (tget_short(&s->gb, le) != 42) {
1069  av_log(avctx, AV_LOG_ERROR,
1070  "The answer to life, universe and everything is not correct!\n");
1071  return -1;
1072  }
1073  // Reset these offsets so we can tell if they were set this frame
1074  s->stripsizesoff = s->strippos = 0;
1075  /* parse image file directory */
1076  off = tget_long(&s->gb, le);
1077  if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1078  av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1079  return AVERROR_INVALIDDATA;
1080  }
1081  bytestream2_seek(&s->gb, off, SEEK_SET);
1082  entries = tget_short(&s->gb, le);
1083  if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
1084  return AVERROR_INVALIDDATA;
1085  for (i = 0; i < entries; i++) {
1086  if (tiff_decode_tag(s) < 0)
1087  return -1;
1088  }
1089 
1090  for (i = 0; i<s->geotag_count; i++) {
1091  const char *keyname = get_geokey_name(s->geotags[i].key);
1092  if (!keyname) {
1093  av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
1094  continue;
1095  }
1096  if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
1097  av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
1098  continue;
1099  }
1100  ret = av_dict_set(&s->picture.metadata, keyname, s->geotags[i].val, 0);
1101  if (ret<0) {
1102  av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
1103  return ret;
1104  }
1105  }
1106 
1107  if (!s->strippos && !s->stripoff) {
1108  av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
1109  return -1;
1110  }
1111  /* now we have the data and may start decoding */
1112  if ((ret = init_image(s)) < 0)
1113  return ret;
1114 
1115  if (s->strips == 1 && !s->stripsize) {
1116  av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
1117  s->stripsize = avpkt->size - s->stripoff;
1118  }
1119  stride = p->linesize[0];
1120  dst = p->data[0];
1121 
1122  if (s->stripsizesoff) {
1123  if (s->stripsizesoff >= (unsigned)avpkt->size)
1124  return AVERROR_INVALIDDATA;
1125  bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff, avpkt->size - s->stripsizesoff);
1126  }
1127  if (s->strippos) {
1128  if (s->strippos >= (unsigned)avpkt->size)
1129  return AVERROR_INVALIDDATA;
1130  bytestream2_init(&stripdata, avpkt->data + s->strippos, avpkt->size - s->strippos);
1131  }
1132 
1133  if (s->rps <= 0) {
1134  av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
1135  return AVERROR_INVALIDDATA;
1136  }
1137 
1138  for (i = 0; i < s->height; i += s->rps) {
1139  if (s->stripsizesoff)
1140  ssize = tget(&stripsizes, s->sstype, s->le);
1141  else
1142  ssize = s->stripsize;
1143 
1144  if (s->strippos)
1145  soff = tget(&stripdata, s->sot, s->le);
1146  else
1147  soff = s->stripoff;
1148 
1149  if (soff > avpkt->size || ssize > avpkt->size - soff) {
1150  av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
1151  return -1;
1152  }
1153  if (tiff_unpack_strip(s, dst, stride, avpkt->data + soff, ssize,
1154  FFMIN(s->rps, s->height - i)) < 0)
1155  break;
1156  dst += s->rps * stride;
1157  }
1158  if (s->predictor == 2) {
1159  dst = p->data[0];
1160  soff = s->bpp >> 3;
1161  ssize = s->width * soff;
1162  if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
1164  for (i = 0; i < s->height; i++) {
1165  for (j = soff; j < ssize; j += 2)
1166  AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
1167  dst += stride;
1168  }
1169  } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1171  for (i = 0; i < s->height; i++) {
1172  for (j = soff; j < ssize; j += 2)
1173  AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
1174  dst += stride;
1175  }
1176  } else {
1177  for (i = 0; i < s->height; i++) {
1178  for (j = soff; j < ssize; j++)
1179  dst[j] += dst[j - soff];
1180  dst += stride;
1181  }
1182  }
1183  }
1184 
1185  if (s->invert) {
1186  dst = s->picture.data[0];
1187  for (i = 0; i < s->height; i++) {
1188  for (j = 0; j < s->picture.linesize[0]; j++)
1189  dst[j] = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];
1190  dst += s->picture.linesize[0];
1191  }
1192  }
1193  *picture = s->picture;
1194  *got_frame = 1;
1195 
1196  return avpkt->size;
1197 }
1198 
1200 {
1201  TiffContext *s = avctx->priv_data;
1202 
1203  s->width = 0;
1204  s->height = 0;
1205  s->avctx = avctx;
1207  avctx->coded_frame = &s->picture;
1208  ff_lzw_decode_open(&s->lzw);
1210 
1211  return 0;
1212 }
1213 
1214 static av_cold int tiff_end(AVCodecContext *avctx)
1215 {
1216  TiffContext *const s = avctx->priv_data;
1217 
1218  free_geotags(s);
1219 
1220  ff_lzw_decode_close(&s->lzw);
1221  av_freep(&s->deinvert_buf);
1222  if (s->picture.data[0])
1223  avctx->release_buffer(avctx, &s->picture);
1224  return 0;
1225 }
1226 
1228  .name = "tiff",
1229  .type = AVMEDIA_TYPE_VIDEO,
1230  .id = AV_CODEC_ID_TIFF,
1231  .priv_data_size = sizeof(TiffContext),
1232  .init = tiff_init,
1233  .close = tiff_end,
1234  .decode = decode_frame,
1235  .capabilities = CODEC_CAP_DR1,
1236  .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
1237 };