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