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;
78  unsigned int fax_buffer_size;
79 
82 } TiffContext;
83 
84 static void free_geotags(TiffContext *const s)
85 {
86  int i;
87  for (i = 0; i < s->geotag_count; i++) {
88  if (s->geotags[i].val)
89  av_freep(&s->geotags[i].val);
90  }
91  av_freep(&s->geotags);
92  s->geotag_count = 0;
93 }
94 
95 #define RET_GEOKEY(TYPE, array, element)\
96  if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
97  key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_name_type_map))\
98  return ff_tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
99 
100 static const char *get_geokey_name(int key)
101 {
102  RET_GEOKEY(VERT, vert, name);
103  RET_GEOKEY(PROJ, proj, name);
104  RET_GEOKEY(GEOG, geog, name);
105  RET_GEOKEY(CONF, conf, name);
106 
107  return NULL;
108 }
109 
110 static int get_geokey_type(int key)
111 {
112  RET_GEOKEY(VERT, vert, type);
113  RET_GEOKEY(PROJ, proj, type);
114  RET_GEOKEY(GEOG, geog, type);
115  RET_GEOKEY(CONF, conf, type);
116 
117  return AVERROR_INVALIDDATA;
118 }
119 
120 static int cmp_id_key(const void *id, const void *k)
121 {
122  return *(const int*)id - ((const TiffGeoTagKeyName*)k)->key;
123 }
124 
125 static const char *search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
126 {
127  TiffGeoTagKeyName *r = bsearch(&id, keys, n, sizeof(keys[0]), cmp_id_key);
128  if(r)
129  return r->name;
130 
131  return NULL;
132 }
133 
134 static char *get_geokey_val(int key, int val)
135 {
136  char *ap;
137 
138  if (val == TIFF_GEO_KEY_UNDEFINED)
139  return av_strdup("undefined");
140  if (val == TIFF_GEO_KEY_USER_DEFINED)
141  return av_strdup("User-Defined");
142 
143 #define RET_GEOKEY_VAL(TYPE, array)\
144  if (val >= TIFF_##TYPE##_OFFSET &&\
145  val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_codes))\
146  return av_strdup(ff_tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]);
147 
148  switch (key) {
150  RET_GEOKEY_VAL(GT_MODEL_TYPE, gt_model_type);
151  break;
153  RET_GEOKEY_VAL(GT_RASTER_TYPE, gt_raster_type);
154  break;
158  RET_GEOKEY_VAL(LINEAR_UNIT, linear_unit);
159  break;
162  RET_GEOKEY_VAL(ANGULAR_UNIT, angular_unit);
163  break;
165  RET_GEOKEY_VAL(GCS_TYPE, gcs_type);
166  RET_GEOKEY_VAL(GCSE_TYPE, gcse_type);
167  break;
169  RET_GEOKEY_VAL(GEODETIC_DATUM, geodetic_datum);
170  RET_GEOKEY_VAL(GEODETIC_DATUM_E, geodetic_datum_e);
171  break;
173  RET_GEOKEY_VAL(ELLIPSOID, ellipsoid);
174  break;
176  RET_GEOKEY_VAL(PRIME_MERIDIAN, prime_meridian);
177  break;
180  if(ap) return ap;
181  break;
184  if(ap) return ap;
185  break;
187  RET_GEOKEY_VAL(COORD_TRANS, coord_trans);
188  break;
190  RET_GEOKEY_VAL(VERT_CS, vert_cs);
191  RET_GEOKEY_VAL(ORTHO_VERT_CS, ortho_vert_cs);
192  break;
193 
194  }
195 
196  ap = av_malloc(14);
197  if (ap)
198  snprintf(ap, 14, "Unknown-%d", val);
199  return ap;
200 }
201 
202 static char *doubles2str(double *dp, int count, const char *sep)
203 {
204  int i;
205  char *ap, *ap0;
206  uint64_t component_len;
207  if (!sep) sep = ", ";
208  component_len = 24LL + strlen(sep);
209  if (count >= (INT_MAX - 1)/component_len)
210  return NULL;
211  ap = av_malloc(component_len * count + 1);
212  if (!ap)
213  return NULL;
214  ap0 = ap;
215  ap[0] = '\0';
216  for (i = 0; i < count; i++) {
217  unsigned l = snprintf(ap, component_len, "%.15g%s", dp[i], sep);
218  if(l >= component_len) {
219  av_free(ap0);
220  return NULL;
221  }
222  ap += l;
223  }
224  ap0[strlen(ap0) - strlen(sep)] = '\0';
225  return ap0;
226 }
227 
228 static int add_metadata(int count, int type,
229  const char *name, const char *sep, TiffContext *s, AVFrame *frame)
230 {
231  switch(type) {
232  case TIFF_DOUBLE: return ff_tadd_doubles_metadata(count, name, sep, &s->gb, s->le, &frame->metadata);
233  case TIFF_SHORT : return ff_tadd_shorts_metadata(count, name, sep, &s->gb, s->le, 0, &frame->metadata);
234  case TIFF_STRING: return ff_tadd_string_metadata(count, name, &s->gb, s->le, &frame->metadata);
235  default : return AVERROR_INVALIDDATA;
236  };
237 }
238 
239 static void av_always_inline horizontal_fill(unsigned int bpp, uint8_t* dst,
240  int usePtr, const uint8_t *src,
241  uint8_t c, int width, int offset)
242 {
243  switch (bpp) {
244  case 1:
245  while (--width >= 0) {
246  dst[(width+offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
247  dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
248  dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
249  dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
250  dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
251  dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
252  dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
253  dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
254  }
255  break;
256  case 2:
257  while (--width >= 0) {
258  dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
259  dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
260  dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
261  dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
262  }
263  break;
264  case 4:
265  while (--width >= 0) {
266  dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
267  dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
268  }
269  break;
270  default:
271  if (usePtr) {
272  memcpy(dst + offset, src, width);
273  } else {
274  memset(dst + offset, c, width);
275  }
276  }
277 }
278 
279 static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
280 {
281  int i;
282 
284  if (!s->deinvert_buf)
285  return AVERROR(ENOMEM);
286  for (i = 0; i < size; i++)
287  s->deinvert_buf[i] = ff_reverse[src[i]];
288 
289  return 0;
290 }
291 
292 static void unpack_yuv(TiffContext *s, AVFrame *p,
293  const uint8_t *src, int lnum)
294 {
295  int i, j, k;
296  int w = (s->width - 1) / s->subsampling[0] + 1;
297  uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
298  uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
299  if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) {
300  for (i = 0; i < w; i++) {
301  for (j = 0; j < s->subsampling[1]; j++)
302  for (k = 0; k < s->subsampling[0]; k++)
303  p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
304  FFMIN(i * s->subsampling[0] + k, s->width-1)] = *src++;
305  *pu++ = *src++;
306  *pv++ = *src++;
307  }
308  }else{
309  for (i = 0; i < w; i++) {
310  for (j = 0; j < s->subsampling[1]; j++)
311  for (k = 0; k < s->subsampling[0]; k++)
312  p->data[0][(lnum + j) * p->linesize[0] +
313  i * s->subsampling[0] + k] = *src++;
314  *pu++ = *src++;
315  *pv++ = *src++;
316  }
317  }
318 }
319 
320 #if CONFIG_ZLIB
321 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
322  int size)
323 {
324  z_stream zstream = { 0 };
325  int zret;
326 
327  zstream.next_in = (uint8_t *)src;
328  zstream.avail_in = size;
329  zstream.next_out = dst;
330  zstream.avail_out = *len;
331  zret = inflateInit(&zstream);
332  if (zret != Z_OK) {
333  av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
334  return zret;
335  }
336  zret = inflate(&zstream, Z_SYNC_FLUSH);
337  inflateEnd(&zstream);
338  *len = zstream.total_out;
339  return zret == Z_STREAM_END ? Z_OK : zret;
340 }
341 
342 static int tiff_unpack_zlib(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
343  const uint8_t *src, int size, int width, int lines,
344  int strip_start, int is_yuv)
345 {
346  uint8_t *zbuf;
347  unsigned long outlen;
348  int ret, line;
349  outlen = width * lines;
350  zbuf = av_malloc(outlen);
351  if (!zbuf)
352  return AVERROR(ENOMEM);
353  if (s->fill_order) {
354  if ((ret = deinvert_buffer(s, src, size)) < 0) {
355  av_free(zbuf);
356  return ret;
357  }
358  src = s->deinvert_buf;
359  }
360  ret = tiff_uncompress(zbuf, &outlen, src, size);
361  if (ret != Z_OK) {
363  "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
364  (unsigned long)width * lines, ret);
365  av_free(zbuf);
366  return AVERROR_UNKNOWN;
367  }
368  src = zbuf;
369  for (line = 0; line < lines; line++) {
370  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
371  horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
372  } else {
373  memcpy(dst, src, width);
374  }
375  if (is_yuv) {
376  unpack_yuv(s, p, dst, strip_start + line);
377  line += s->subsampling[1] - 1;
378  }
379  dst += stride;
380  src += width;
381  }
382  av_free(zbuf);
383  return 0;
384 }
385 #endif
386 
387 #if CONFIG_LZMA
388 static int tiff_uncompress_lzma(uint8_t *dst, uint64_t *len, const uint8_t *src,
389  int size)
390 {
391  lzma_stream stream = LZMA_STREAM_INIT;
392  lzma_ret ret;
393 
394  stream.next_in = (uint8_t *)src;
395  stream.avail_in = size;
396  stream.next_out = dst;
397  stream.avail_out = *len;
398  ret = lzma_stream_decoder(&stream, UINT64_MAX, 0);
399  if (ret != LZMA_OK) {
400  av_log(NULL, AV_LOG_ERROR, "LZMA init error: %d\n", ret);
401  return ret;
402  }
403  ret = lzma_code(&stream, LZMA_RUN);
404  lzma_end(&stream);
405  *len = stream.total_out;
406  return ret == LZMA_STREAM_END ? LZMA_OK : ret;
407 }
408 
409 static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
410  const uint8_t *src, int size, int width, int lines,
411  int strip_start, int is_yuv)
412 {
413  uint64_t outlen = width * (uint64_t)lines;
414  int ret, line;
415  uint8_t *buf = av_malloc(outlen);
416  if (!buf)
417  return AVERROR(ENOMEM);
418  if (s->fill_order) {
419  if ((ret = deinvert_buffer(s, src, size)) < 0) {
420  av_free(buf);
421  return ret;
422  }
423  src = s->deinvert_buf;
424  }
425  ret = tiff_uncompress_lzma(buf, &outlen, src, size);
426  if (ret != LZMA_OK) {
428  "Uncompressing failed (%"PRIu64" of %"PRIu64") with error %d\n", outlen,
429  (uint64_t)width * lines, ret);
430  av_free(buf);
431  return AVERROR_UNKNOWN;
432  }
433  src = buf;
434  for (line = 0; line < lines; line++) {
435  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
436  horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
437  } else {
438  memcpy(dst, src, width);
439  }
440  if (is_yuv) {
441  unpack_yuv(s, p, dst, strip_start + line);
442  line += s->subsampling[1] - 1;
443  }
444  dst += stride;
445  src += width;
446  }
447  av_free(buf);
448  return 0;
449 }
450 #endif
451 
452 static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
453  const uint8_t *src, int size, int width, int lines)
454 {
455  int i, ret = 0;
456  int line;
457  uint8_t *src2;
458 
460  src2 = s->fax_buffer;
461 
462  if (!src2) {
464  "Error allocating temporary buffer\n");
465  return AVERROR(ENOMEM);
466  }
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, AV_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  return ret;
483 }
484 
485 static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
486  const uint8_t *src, int size, int strip_start, int lines)
487 {
488  PutByteContext pb;
489  int c, line, pixels, code, ret;
490  const uint8_t *ssrc = src;
491  int width = ((s->width * s->bpp) + 7) >> 3;
493  int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
494  (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
495  desc->nb_components >= 3;
496 
497  if (s->planar)
498  width /= s->bppcount;
499 
500  if (size <= 0)
501  return AVERROR_INVALIDDATA;
502 
503  if (is_yuv) {
504  int bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
505  s->subsampling[0] * s->subsampling[1] + 7) >> 3;
506  av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
507  if (s->yuv_line == NULL) {
508  av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
509  return AVERROR(ENOMEM);
510  }
511  dst = s->yuv_line;
512  stride = 0;
513 
514  width = (s->width - 1) / s->subsampling[0] + 1;
515  width = width * s->subsampling[0] * s->subsampling[1] + 2*width;
516  av_assert0(width <= bytes_per_row);
517  av_assert0(s->bpp == 24);
518  }
519 
520  if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
521 #if CONFIG_ZLIB
522  return tiff_unpack_zlib(s, p, dst, stride, src, size, width, lines,
523  strip_start, is_yuv);
524 #else
526  "zlib support not enabled, "
527  "deflate compression not supported\n");
528  return AVERROR(ENOSYS);
529 #endif
530  }
531  if (s->compr == TIFF_LZMA) {
532 #if CONFIG_LZMA
533  return tiff_unpack_lzma(s, p, dst, stride, src, size, width, lines,
534  strip_start, is_yuv);
535 #else
537  "LZMA support not enabled\n");
538  return AVERROR(ENOSYS);
539 #endif
540  }
541  if (s->compr == TIFF_LZW) {
542  if (s->fill_order) {
543  if ((ret = deinvert_buffer(s, src, size)) < 0)
544  return ret;
545  ssrc = src = s->deinvert_buf;
546  }
547  if (size > 1 && !src[0] && (src[1]&1)) {
548  av_log(s->avctx, AV_LOG_ERROR, "Old style LZW is unsupported\n");
549  }
550  if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
551  av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
552  return ret;
553  }
554  for (line = 0; line < lines; line++) {
555  pixels = ff_lzw_decode(s->lzw, dst, width);
556  if (pixels < width) {
557  av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
558  pixels, width);
559  return AVERROR_INVALIDDATA;
560  }
561  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
562  horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
563  if (is_yuv) {
564  unpack_yuv(s, p, dst, strip_start + line);
565  line += s->subsampling[1] - 1;
566  }
567  dst += stride;
568  }
569  return 0;
570  }
571  if (s->compr == TIFF_CCITT_RLE ||
572  s->compr == TIFF_G3 ||
573  s->compr == TIFF_G4) {
574  if (is_yuv)
575  return AVERROR_INVALIDDATA;
576 
577  return tiff_unpack_fax(s, dst, stride, src, size, width, lines);
578  }
579 
580  bytestream2_init(&s->gb, src, size);
581  bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * lines));
582 
583  for (line = 0; line < lines; line++) {
584  if (src - ssrc > size) {
585  av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
586  return AVERROR_INVALIDDATA;
587  }
588 
589  if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
590  break;
591  bytestream2_seek_p(&pb, stride * line, SEEK_SET);
592  switch (s->compr) {
593  case TIFF_RAW:
594  if (ssrc + size - src < width)
595  return AVERROR_INVALIDDATA;
596 
597  if (!s->fill_order) {
599  dst, 1, src, 0, width, 0);
600  } else {
601  int i;
602  for (i = 0; i < width; i++)
603  dst[i] = ff_reverse[src[i]];
604  }
605  src += width;
606  break;
607  case TIFF_PACKBITS:
608  for (pixels = 0; pixels < width;) {
609  if (ssrc + size - src < 2) {
610  av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
611  return AVERROR_INVALIDDATA;
612  }
613  code = s->fill_order ? (int8_t) ff_reverse[*src++]: (int8_t) *src++;
614  if (code >= 0) {
615  code++;
616  if (pixels + code > width ||
617  ssrc + size - src < code) {
619  "Copy went out of bounds\n");
620  return AVERROR_INVALIDDATA;
621  }
623  dst, 1, src, 0, code, pixels);
624  src += code;
625  pixels += code;
626  } else if (code != -128) { // -127..-1
627  code = (-code) + 1;
628  if (pixels + code > width) {
630  "Run went out of bounds\n");
631  return AVERROR_INVALIDDATA;
632  }
633  c = *src++;
635  dst, 0, NULL, c, code, pixels);
636  pixels += code;
637  }
638  }
639  if (s->fill_order) {
640  int i;
641  for (i = 0; i < width; i++)
642  dst[i] = ff_reverse[dst[i]];
643  }
644  break;
645  }
646  if (is_yuv) {
647  unpack_yuv(s, p, dst, strip_start + line);
648  line += s->subsampling[1] - 1;
649  }
650  dst += stride;
651  }
652  return 0;
653 }
654 
656 {
657  int ret;
658  int create_gray_palette = 0;
659 
660  // make sure there is no aliasing in the following switch
661  if (s->bpp >= 100 || s->bppcount >= 10) {
663  "Unsupported image parameters: bpp=%d, bppcount=%d\n",
664  s->bpp, s->bppcount);
665  return AVERROR_INVALIDDATA;
666  }
667 
668  switch (s->planar * 1000 + s->bpp * 10 + s->bppcount) {
669  case 11:
670  if (!s->palette_is_set) {
672  break;
673  }
674  case 21:
675  case 41:
677  if (!s->palette_is_set) {
678  create_gray_palette = 1;
679  }
680  break;
681  case 81:
683  break;
684  case 243:
686  if (s->subsampling[0] == 1 && s->subsampling[1] == 1) {
688  } else if (s->subsampling[0] == 2 && s->subsampling[1] == 1) {
690  } else if (s->subsampling[0] == 4 && s->subsampling[1] == 1) {
692  } else if (s->subsampling[0] == 1 && s->subsampling[1] == 2) {
694  } else if (s->subsampling[0] == 2 && s->subsampling[1] == 2) {
696  } else if (s->subsampling[0] == 4 && s->subsampling[1] == 4) {
698  } else {
699  av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr subsampling\n");
700  return AVERROR_PATCHWELCOME;
701  }
702  } else
704  break;
705  case 161:
707  break;
708  case 162:
710  break;
711  case 322:
713  break;
714  case 324:
716  break;
717  case 483:
719  break;
720  case 644:
722  break;
723  case 1243:
725  break;
726  case 1324:
728  break;
729  case 1483:
731  break;
732  case 1644:
734  break;
735  default:
737  "This format is not supported (bpp=%d, bppcount=%d)\n",
738  s->bpp, s->bppcount);
739  return AVERROR_INVALIDDATA;
740  }
741 
744  if((desc->flags & AV_PIX_FMT_FLAG_RGB) ||
745  !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
746  desc->nb_components < 3) {
747  av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr variant\n");
748  return AVERROR_INVALIDDATA;
749  }
750  }
751 
752  if (s->width != s->avctx->width || s->height != s->avctx->height) {
753  ret = ff_set_dimensions(s->avctx, s->width, s->height);
754  if (ret < 0)
755  return ret;
756  }
757  if ((ret = ff_thread_get_buffer(s->avctx, frame, 0)) < 0)
758  return ret;
759  if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
760  if (!create_gray_palette)
761  memcpy(frame->f->data[1], s->palette, sizeof(s->palette));
762  else {
763  /* make default grayscale pal */
764  int i;
765  uint32_t *pal = (uint32_t *)frame->f->data[1];
766  for (i = 0; i < 1<<s->bpp; i++)
767  pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
768  }
769  }
770  return 0;
771 }
772 
773 static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
774 {
775  int offset = tag == TIFF_YRES ? 2 : 0;
776  s->res[offset++] = num;
777  s->res[offset] = den;
778  if (s->res[0] && s->res[1] && s->res[2] && s->res[3]) {
779  uint64_t num = s->res[2] * (uint64_t)s->res[1];
780  uint64_t den = s->res[0] * (uint64_t)s->res[3];
781  if (num > INT64_MAX || den > INT64_MAX) {
782  num = num >> 1;
783  den = den >> 1;
784  }
786  num, den, INT32_MAX);
787  if (!s->avctx->sample_aspect_ratio.den)
788  s->avctx->sample_aspect_ratio = (AVRational) {0, 1};
789  }
790 }
791 
793 {
794  unsigned tag, type, count, off, value = 0, value2 = 0;
795  int i, start;
796  int pos;
797  int ret;
798  double *dp;
799 
800  ret = ff_tread_tag(&s->gb, s->le, &tag, &type, &count, &start);
801  if (ret < 0) {
802  goto end;
803  }
804 
805  off = bytestream2_tell(&s->gb);
806  if (count == 1) {
807  switch (type) {
808  case TIFF_BYTE:
809  case TIFF_SHORT:
810  case TIFF_LONG:
811  value = ff_tget(&s->gb, type, s->le);
812  break;
813  case TIFF_RATIONAL:
814  value = ff_tget(&s->gb, TIFF_LONG, s->le);
815  value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
816  break;
817  case TIFF_STRING:
818  if (count <= 4) {
819  break;
820  }
821  default:
822  value = UINT_MAX;
823  }
824  }
825 
826  switch (tag) {
827  case TIFF_WIDTH:
828  s->width = value;
829  break;
830  case TIFF_HEIGHT:
831  s->height = value;
832  break;
833  case TIFF_BPP:
834  if (count > 4U) {
836  "This format is not supported (bpp=%d, %d components)\n",
837  value, count);
838  return AVERROR_INVALIDDATA;
839  }
840  s->bppcount = count;
841  if (count == 1)
842  s->bpp = value;
843  else {
844  switch (type) {
845  case TIFF_BYTE:
846  case TIFF_SHORT:
847  case TIFF_LONG:
848  s->bpp = 0;
849  if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
850  return AVERROR_INVALIDDATA;
851  for (i = 0; i < count; i++)
852  s->bpp += ff_tget(&s->gb, type, s->le);
853  break;
854  default:
855  s->bpp = -1;
856  }
857  }
858  break;
860  if (count != 1) {
862  "Samples per pixel requires a single value, many provided\n");
863  return AVERROR_INVALIDDATA;
864  }
865  if (value > 4U) {
867  "Samples per pixel %d is too large\n", value);
868  return AVERROR_INVALIDDATA;
869  }
870  if (s->bppcount == 1)
871  s->bpp *= value;
872  s->bppcount = value;
873  break;
874  case TIFF_COMPR:
875  s->compr = value;
876  av_log(s->avctx, AV_LOG_DEBUG, "compression: %d\n", s->compr);
877  s->predictor = 0;
878  switch (s->compr) {
879  case TIFF_RAW:
880  case TIFF_PACKBITS:
881  case TIFF_LZW:
882  case TIFF_CCITT_RLE:
883  break;
884  case TIFF_G3:
885  case TIFF_G4:
886  s->fax_opts = 0;
887  break;
888  case TIFF_DEFLATE:
889  case TIFF_ADOBE_DEFLATE:
890 #if CONFIG_ZLIB
891  break;
892 #else
893  av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
894  return AVERROR(ENOSYS);
895 #endif
896  case TIFF_JPEG:
897  case TIFF_NEWJPEG:
898  avpriv_report_missing_feature(s->avctx, "JPEG compression");
899  return AVERROR_PATCHWELCOME;
900  case TIFF_LZMA:
901 #if CONFIG_LZMA
902  break;
903 #else
904  av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n");
905  return AVERROR(ENOSYS);
906 #endif
907  default:
908  av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
909  s->compr);
910  return AVERROR_INVALIDDATA;
911  }
912  break;
913  case TIFF_ROWSPERSTRIP:
914  if (!value || (type == TIFF_LONG && value == UINT_MAX))
915  value = s->height;
916  s->rps = FFMIN(value, s->height);
917  break;
918  case TIFF_STRIP_OFFS:
919  if (count == 1) {
920  if (value > INT_MAX) {
922  "strippos %u too large\n", value);
923  return AVERROR_INVALIDDATA;
924  }
925  s->strippos = 0;
926  s->stripoff = value;
927  } else
928  s->strippos = off;
929  s->strips = count;
930  if (s->strips == 1)
931  s->rps = s->height;
932  s->sot = type;
933  break;
934  case TIFF_STRIP_SIZE:
935  if (count == 1) {
936  if (value > INT_MAX) {
938  "stripsize %u too large\n", value);
939  return AVERROR_INVALIDDATA;
940  }
941  s->stripsizesoff = 0;
942  s->stripsize = value;
943  s->strips = 1;
944  } else {
945  s->stripsizesoff = off;
946  }
947  s->strips = count;
948  s->sstype = type;
949  break;
950  case TIFF_XRES:
951  case TIFF_YRES:
952  set_sar(s, tag, value, value2);
953  break;
955  case TIFF_TILE_LENGTH:
956  case TIFF_TILE_OFFSETS:
957  case TIFF_TILE_WIDTH:
958  av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");
959  return AVERROR_PATCHWELCOME;
960  break;
961  case TIFF_PREDICTOR:
962  s->predictor = value;
963  break;
964  case TIFF_PHOTOMETRIC:
965  switch (value) {
971  s->photometric = value;
972  break;
983  "PhotometricInterpretation 0x%04X",
984  value);
985  return AVERROR_PATCHWELCOME;
986  default:
987  av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
988  "unknown\n", value);
989  return AVERROR_INVALIDDATA;
990  }
991  break;
992  case TIFF_FILL_ORDER:
993  if (value < 1 || value > 2) {
995  "Unknown FillOrder value %d, trying default one\n", value);
996  value = 1;
997  }
998  s->fill_order = value - 1;
999  break;
1000  case TIFF_PAL: {
1001  GetByteContext pal_gb[3];
1002  off = type_sizes[type];
1003  if (count / 3 > 256 ||
1004  bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
1005  return AVERROR_INVALIDDATA;
1006 
1007  pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
1008  bytestream2_skip(&pal_gb[1], count / 3 * off);
1009  bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
1010 
1011  off = (type_sizes[type] - 1) << 3;
1012  if (off > 31U) {
1013  av_log(s->avctx, AV_LOG_ERROR, "palette shift %d is out of range\n", off);
1014  return AVERROR_INVALIDDATA;
1015  }
1016 
1017  for (i = 0; i < count / 3; i++) {
1018  uint32_t p = 0xFF000000;
1019  p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16;
1020  p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8;
1021  p |= ff_tget(&pal_gb[2], type, s->le) >> off;
1022  s->palette[i] = p;
1023  }
1024  s->palette_is_set = 1;
1025  break;
1026  }
1027  case TIFF_PLANAR:
1028  s->planar = value == 2;
1029  break;
1031  if (count != 2) {
1032  av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n");
1033  return AVERROR_INVALIDDATA;
1034  }
1035  for (i = 0; i < count; i++) {
1036  s->subsampling[i] = ff_tget(&s->gb, type, s->le);
1037  if (s->subsampling[i] <= 0) {
1038  av_log(s->avctx, AV_LOG_ERROR, "subsampling %d is invalid\n", s->subsampling[i]);
1039  s->subsampling[i] = 1;
1040  return AVERROR_INVALIDDATA;
1041  }
1042  }
1043  break;
1044  case TIFF_T4OPTIONS:
1045  if (s->compr == TIFF_G3)
1046  s->fax_opts = value;
1047  break;
1048  case TIFF_T6OPTIONS:
1049  if (s->compr == TIFF_G4)
1050  s->fax_opts = value;
1051  break;
1052 #define ADD_METADATA(count, name, sep)\
1053  if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1054  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1055  goto end;\
1056  }
1058  ADD_METADATA(count, "ModelPixelScaleTag", NULL);
1059  break;
1061  ADD_METADATA(count, "ModelTransformationTag", NULL);
1062  break;
1063  case TIFF_MODEL_TIEPOINT:
1064  ADD_METADATA(count, "ModelTiepointTag", NULL);
1065  break;
1067  if (s->geotag_count) {
1068  avpriv_request_sample(s->avctx, "Multiple geo key directories\n");
1069  return AVERROR_INVALIDDATA;
1070  }
1071  ADD_METADATA(1, "GeoTIFF_Version", NULL);
1072  ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
1073  s->geotag_count = ff_tget_short(&s->gb, s->le);
1074  if (s->geotag_count > count / 4 - 1) {
1075  s->geotag_count = count / 4 - 1;
1076  av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
1077  }
1078  if ( bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4
1079  || s->geotag_count == 0) {
1080  s->geotag_count = 0;
1081  return -1;
1082  }
1083  s->geotags = av_mallocz_array(s->geotag_count, sizeof(TiffGeoTag));
1084  if (!s->geotags) {
1085  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1086  s->geotag_count = 0;
1087  goto end;
1088  }
1089  for (i = 0; i < s->geotag_count; i++) {
1090  s->geotags[i].key = ff_tget_short(&s->gb, s->le);
1091  s->geotags[i].type = ff_tget_short(&s->gb, s->le);
1092  s->geotags[i].count = ff_tget_short(&s->gb, s->le);
1093 
1094  if (!s->geotags[i].type)
1095  s->geotags[i].val = get_geokey_val(s->geotags[i].key, ff_tget_short(&s->gb, s->le));
1096  else
1097  s->geotags[i].offset = ff_tget_short(&s->gb, s->le);
1098  }
1099  break;
1101  if (count >= INT_MAX / sizeof(int64_t))
1102  return AVERROR_INVALIDDATA;
1103  if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
1104  return AVERROR_INVALIDDATA;
1105  dp = av_malloc_array(count, sizeof(double));
1106  if (!dp) {
1107  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1108  goto end;
1109  }
1110  for (i = 0; i < count; i++)
1111  dp[i] = ff_tget_double(&s->gb, s->le);
1112  for (i = 0; i < s->geotag_count; i++) {
1113  if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
1114  if (s->geotags[i].count == 0
1115  || s->geotags[i].offset + s->geotags[i].count > count) {
1116  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1117  } else if (s->geotags[i].val) {
1118  av_log(s->avctx, AV_LOG_WARNING, "Duplicate GeoTIFF key %d\n", s->geotags[i].key);
1119  } else {
1120  char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
1121  if (!ap) {
1122  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1123  av_freep(&dp);
1124  return AVERROR(ENOMEM);
1125  }
1126  s->geotags[i].val = ap;
1127  }
1128  }
1129  }
1130  av_freep(&dp);
1131  break;
1132  case TIFF_GEO_ASCII_PARAMS:
1133  pos = bytestream2_tell(&s->gb);
1134  for (i = 0; i < s->geotag_count; i++) {
1135  if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
1136  if (s->geotags[i].count == 0
1137  || s->geotags[i].offset + s->geotags[i].count > count) {
1138  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1139  } else {
1140  char *ap;
1141 
1142  bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
1143  if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
1144  return AVERROR_INVALIDDATA;
1145  if (s->geotags[i].val)
1146  return AVERROR_INVALIDDATA;
1147  ap = av_malloc(s->geotags[i].count);
1148  if (!ap) {
1149  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1150  return AVERROR(ENOMEM);
1151  }
1152  bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
1153  ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
1154  s->geotags[i].val = ap;
1155  }
1156  }
1157  }
1158  break;
1159  case TIFF_ARTIST:
1160  ADD_METADATA(count, "artist", NULL);
1161  break;
1162  case TIFF_COPYRIGHT:
1163  ADD_METADATA(count, "copyright", NULL);
1164  break;
1165  case TIFF_DATE:
1166  ADD_METADATA(count, "date", NULL);
1167  break;
1168  case TIFF_DOCUMENT_NAME:
1169  ADD_METADATA(count, "document_name", NULL);
1170  break;
1171  case TIFF_HOST_COMPUTER:
1172  ADD_METADATA(count, "computer", NULL);
1173  break;
1175  ADD_METADATA(count, "description", NULL);
1176  break;
1177  case TIFF_MAKE:
1178  ADD_METADATA(count, "make", NULL);
1179  break;
1180  case TIFF_MODEL:
1181  ADD_METADATA(count, "model", NULL);
1182  break;
1183  case TIFF_PAGE_NAME:
1184  ADD_METADATA(count, "page_name", NULL);
1185  break;
1186  case TIFF_PAGE_NUMBER:
1187  ADD_METADATA(count, "page_number", " / ");
1188  break;
1189  case TIFF_SOFTWARE_NAME:
1190  ADD_METADATA(count, "software", NULL);
1191  break;
1192  default:
1193  if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1195  "Unknown or unsupported tag %d/0X%0X\n",
1196  tag, tag);
1197  return AVERROR_INVALIDDATA;
1198  }
1199  }
1200 end:
1201  if (s->bpp > 64U) {
1203  "This format is not supported (bpp=%d, %d components)\n",
1204  s->bpp, count);
1205  s->bpp = 0;
1206  return AVERROR_INVALIDDATA;
1207  }
1208  bytestream2_seek(&s->gb, start, SEEK_SET);
1209  return 0;
1210 }
1211 
1212 static int decode_frame(AVCodecContext *avctx,
1213  void *data, int *got_frame, AVPacket *avpkt)
1214 {
1215  TiffContext *const s = avctx->priv_data;
1216  AVFrame *const p = data;
1217  ThreadFrame frame = { .f = data };
1218  unsigned off;
1219  int le, ret, plane, planes;
1220  int i, j, entries, stride;
1221  unsigned soff, ssize;
1222  uint8_t *dst;
1223  GetByteContext stripsizes;
1224  GetByteContext stripdata;
1225 
1226  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1227 
1228  // parse image header
1229  if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
1230  av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
1231  return ret;
1232  } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1233  av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1234  return AVERROR_INVALIDDATA;
1235  }
1236  s->le = le;
1237  // TIFF_BPP is not a required tag and defaults to 1
1238  s->bppcount = s->bpp = 1;
1240  s->compr = TIFF_RAW;
1241  s->fill_order = 0;
1242  free_geotags(s);
1243 
1244  // Reset these offsets so we can tell if they were set this frame
1245  s->stripsizesoff = s->strippos = 0;
1246  /* parse image file directory */
1247  bytestream2_seek(&s->gb, off, SEEK_SET);
1248  entries = ff_tget_short(&s->gb, le);
1249  if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
1250  return AVERROR_INVALIDDATA;
1251  for (i = 0; i < entries; i++) {
1252  if ((ret = tiff_decode_tag(s, p)) < 0)
1253  return ret;
1254  }
1255 
1256  for (i = 0; i<s->geotag_count; i++) {
1257  const char *keyname = get_geokey_name(s->geotags[i].key);
1258  if (!keyname) {
1259  av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
1260  continue;
1261  }
1262  if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
1263  av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
1264  continue;
1265  }
1266  ret = av_dict_set(&p->metadata, keyname, s->geotags[i].val, 0);
1267  if (ret<0) {
1268  av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
1269  return ret;
1270  }
1271  }
1272 
1273  if (!s->strippos && !s->stripoff) {
1274  av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
1275  return AVERROR_INVALIDDATA;
1276  }
1277  /* now we have the data and may start decoding */
1278  if ((ret = init_image(s, &frame)) < 0)
1279  return ret;
1280 
1281  if (s->strips == 1 && !s->stripsize) {
1282  av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
1283  s->stripsize = avpkt->size - s->stripoff;
1284  }
1285 
1286  if (s->stripsizesoff) {
1287  if (s->stripsizesoff >= (unsigned)avpkt->size)
1288  return AVERROR_INVALIDDATA;
1289  bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
1290  avpkt->size - s->stripsizesoff);
1291  }
1292  if (s->strippos) {
1293  if (s->strippos >= (unsigned)avpkt->size)
1294  return AVERROR_INVALIDDATA;
1295  bytestream2_init(&stripdata, avpkt->data + s->strippos,
1296  avpkt->size - s->strippos);
1297  }
1298 
1299  if (s->rps <= 0 || s->rps % s->subsampling[1]) {
1300  av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
1301  return AVERROR_INVALIDDATA;
1302  }
1303 
1304  planes = s->planar ? s->bppcount : 1;
1305  for (plane = 0; plane < planes; plane++) {
1306  stride = p->linesize[plane];
1307  dst = p->data[plane];
1308  for (i = 0; i < s->height; i += s->rps) {
1309  if (i)
1310  dst += s->rps * stride;
1311  if (s->stripsizesoff)
1312  ssize = ff_tget(&stripsizes, s->sstype, le);
1313  else
1314  ssize = s->stripsize;
1315 
1316  if (s->strippos)
1317  soff = ff_tget(&stripdata, s->sot, le);
1318  else
1319  soff = s->stripoff;
1320 
1321  if (soff > avpkt->size || ssize > avpkt->size - soff) {
1322  av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
1323  return AVERROR_INVALIDDATA;
1324  }
1325  if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,
1326  FFMIN(s->rps, s->height - i))) < 0) {
1327  if (avctx->err_recognition & AV_EF_EXPLODE)
1328  return ret;
1329  break;
1330  }
1331  }
1332  if (s->predictor == 2) {
1333  if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1334  av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");
1335  return AVERROR_PATCHWELCOME;
1336  }
1337  dst = p->data[plane];
1338  soff = s->bpp >> 3;
1339  if (s->planar)
1340  soff = FFMAX(soff / s->bppcount, 1);
1341  ssize = s->width * soff;
1342  if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
1345  s->avctx->pix_fmt == AV_PIX_FMT_YA16LE ||
1348  for (i = 0; i < s->height; i++) {
1349  for (j = soff; j < ssize; j += 2)
1350  AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
1351  dst += stride;
1352  }
1353  } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1356  s->avctx->pix_fmt == AV_PIX_FMT_YA16BE ||
1359  for (i = 0; i < s->height; i++) {
1360  for (j = soff; j < ssize; j += 2)
1361  AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
1362  dst += stride;
1363  }
1364  } else {
1365  for (i = 0; i < s->height; i++) {
1366  for (j = soff; j < ssize; j++)
1367  dst[j] += dst[j - soff];
1368  dst += stride;
1369  }
1370  }
1371  }
1372 
1374  int c = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255);
1375  dst = p->data[plane];
1376  for (i = 0; i < s->height; i++) {
1377  for (j = 0; j < stride; j++)
1378  dst[j] = c - dst[j];
1379  dst += stride;
1380  }
1381  }
1382  }
1383 
1384  if (s->planar && s->bppcount > 2) {
1385  FFSWAP(uint8_t*, p->data[0], p->data[2]);
1386  FFSWAP(int, p->linesize[0], p->linesize[2]);
1387  FFSWAP(uint8_t*, p->data[0], p->data[1]);
1388  FFSWAP(int, p->linesize[0], p->linesize[1]);
1389  }
1390 
1391  *got_frame = 1;
1392 
1393  return avpkt->size;
1394 }
1395 
1397 {
1398  TiffContext *s = avctx->priv_data;
1399 
1400  s->width = 0;
1401  s->height = 0;
1402  s->subsampling[0] =
1403  s->subsampling[1] = 1;
1404  s->avctx = avctx;
1405  ff_lzw_decode_open(&s->lzw);
1407 
1408  return 0;
1409 }
1410 
1411 static av_cold int tiff_end(AVCodecContext *avctx)
1412 {
1413  TiffContext *const s = avctx->priv_data;
1414 
1415  free_geotags(s);
1416 
1417  ff_lzw_decode_close(&s->lzw);
1418  av_freep(&s->deinvert_buf);
1419  s->deinvert_buf_size = 0;
1420  av_freep(&s->fax_buffer);
1421  s->fax_buffer_size = 0;
1422  return 0;
1423 }
1424 
1426  .name = "tiff",
1427  .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
1428  .type = AVMEDIA_TYPE_VIDEO,
1429  .id = AV_CODEC_ID_TIFF,
1430  .priv_data_size = sizeof(TiffContext),
1431  .init = tiff_init,
1432  .close = tiff_end,
1433  .decode = decode_frame,
1435  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1436 };
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:2419
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
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:80
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:211
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:1680
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:125
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:2172
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:1989
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:84
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:121
uint8_t * fax_buffer
Definition: tiff.c:77
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:3739
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 void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
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:1212
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:485
static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
Definition: tiff.c:792
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:1396
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1679
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:1409
static int add_metadata(int count, int type, const char *name, const char *sep, TiffContext *s, AVFrame *frame)
Definition: tiff.c:228
int stripoff
Definition: tiff.c:70
AVDictionary * metadata
metadata.
Definition: frame.h:488
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
unsigned int fax_buffer_size
Definition: tiff.c:78
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:1411
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:279
#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
uint16_t width
Definition: gdv.c:47
const char * name
Name of the codec implementation.
Definition: avcodec.h:3746
static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
Definition: tiff.c:773
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:452
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:1065
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:3050
#define FFMIN(a, b)
Definition: common.h:96
int le
Definition: tiff.c:58
int width
picture width / height.
Definition: avcodec.h:1948
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:292
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:3061
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:274
uint32_t palette[256]
Definition: tiff.c:56
Definition: tiff.h:41
static const char * get_geokey_name(int key)
Definition: tiff.c:100
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.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
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:1761
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:110
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:215
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:279
static char * get_geokey_val(int key, int val)
Definition: tiff.c:134
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:239
#define AV_WL16(p, v)
Definition: intreadwrite.h:417
static char * doubles2str(double *dp, int count, const char *sep)
Definition: tiff.c:202
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:777
void * priv_data
Definition: avcodec.h:1803
#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:95
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:655
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
AVCodec ff_tiff_decoder
Definition: tiff.c:1425
#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:120
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:81
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:1656
#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:1002
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