FFmpeg  4.4.6
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/error.h"
39 #include "libavutil/intreadwrite.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/opt.h"
42 #include "avcodec.h"
43 #include "bytestream.h"
44 #include "faxcompr.h"
45 #include "internal.h"
46 #include "lzw.h"
47 #include "mathops.h"
48 #include "tiff.h"
49 #include "tiff_data.h"
50 #include "mjpegdec.h"
51 #include "thread.h"
52 #include "get_bits.h"
53 
54 typedef struct TiffContext {
55  AVClass *class;
58 
59  /* JPEG decoding for DNG */
60  AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
61  AVPacket *jpkt; // encoded JPEG tile
62  AVFrame *jpgframe; // decoded JPEG tile
63 
65  uint16_t get_page;
67 
68  enum TiffType tiff_type;
69  int width, height;
70  unsigned int bpp, bppcount;
71  uint32_t palette[256];
73  int le;
74  enum TiffCompr compr;
76  int planar;
77  int subsampling[2];
78  int fax_opts;
79  int predictor;
81  uint32_t res[4];
83  unsigned last_tag;
84 
85  int is_bayer;
87  unsigned black_level;
88  unsigned white_level;
89  uint16_t dng_lut[65536];
90 
91  uint32_t sub_ifd;
92  uint16_t cur_page;
93 
94  int strips, rps, sstype;
95  int sot;
98 
99  /* Tile support */
100  int is_tiled;
103 
104  int is_jpeg;
105 
109  unsigned int yuv_line_size;
111  unsigned int fax_buffer_size;
112 
115 } TiffContext;
116 
117 static void tiff_set_type(TiffContext *s, enum TiffType tiff_type) {
118  if (s->tiff_type < tiff_type) // Prioritize higher-valued entries
119  s->tiff_type = tiff_type;
120 }
121 
122 static void free_geotags(TiffContext *const s)
123 {
124  int i;
125  for (i = 0; i < s->geotag_count; i++) {
126  if (s->geotags[i].val)
127  av_freep(&s->geotags[i].val);
128  }
129  av_freep(&s->geotags);
130  s->geotag_count = 0;
131 }
132 
133 #define RET_GEOKEY(TYPE, array, element)\
134  if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
135  key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(tiff_##array##_name_type_map))\
136  return tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
137 
138 static const char *get_geokey_name(int key)
139 {
140  RET_GEOKEY(VERT, vert, name);
141  RET_GEOKEY(PROJ, proj, name);
142  RET_GEOKEY(GEOG, geog, name);
143  RET_GEOKEY(CONF, conf, name);
144 
145  return NULL;
146 }
147 
148 static int get_geokey_type(int key)
149 {
150  RET_GEOKEY(VERT, vert, type);
151  RET_GEOKEY(PROJ, proj, type);
152  RET_GEOKEY(GEOG, geog, type);
153  RET_GEOKEY(CONF, conf, type);
154 
155  return AVERROR_INVALIDDATA;
156 }
157 
158 static int cmp_id_key(const void *id, const void *k)
159 {
160  return *(const int*)id - ((const TiffGeoTagKeyName*)k)->key;
161 }
162 
163 static const char *search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
164 {
165  TiffGeoTagKeyName *r = bsearch(&id, keys, n, sizeof(keys[0]), cmp_id_key);
166  if(r)
167  return r->name;
168 
169  return NULL;
170 }
171 
172 static char *get_geokey_val(int key, int val)
173 {
174  char *ap;
175 
177  return av_strdup("undefined");
179  return av_strdup("User-Defined");
180 
181 #define RET_GEOKEY_VAL(TYPE, array)\
182  if (val >= TIFF_##TYPE##_OFFSET &&\
183  val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(tiff_##array##_codes))\
184  return av_strdup(tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]);
185 
186  switch (key) {
188  RET_GEOKEY_VAL(GT_MODEL_TYPE, gt_model_type);
189  break;
191  RET_GEOKEY_VAL(GT_RASTER_TYPE, gt_raster_type);
192  break;
196  RET_GEOKEY_VAL(LINEAR_UNIT, linear_unit);
197  break;
200  RET_GEOKEY_VAL(ANGULAR_UNIT, angular_unit);
201  break;
203  RET_GEOKEY_VAL(GCS_TYPE, gcs_type);
204  RET_GEOKEY_VAL(GCSE_TYPE, gcse_type);
205  break;
207  RET_GEOKEY_VAL(GEODETIC_DATUM, geodetic_datum);
208  RET_GEOKEY_VAL(GEODETIC_DATUM_E, geodetic_datum_e);
209  break;
211  RET_GEOKEY_VAL(ELLIPSOID, ellipsoid);
212  break;
214  RET_GEOKEY_VAL(PRIME_MERIDIAN, prime_meridian);
215  break;
218  if(ap) return ap;
219  break;
222  if(ap) return ap;
223  break;
225  RET_GEOKEY_VAL(COORD_TRANS, coord_trans);
226  break;
228  RET_GEOKEY_VAL(VERT_CS, vert_cs);
229  RET_GEOKEY_VAL(ORTHO_VERT_CS, ortho_vert_cs);
230  break;
231 
232  }
233 
234  ap = av_malloc(14);
235  if (ap)
236  snprintf(ap, 14, "Unknown-%d", val);
237  return ap;
238 }
239 
240 static char *doubles2str(double *dp, int count, const char *sep)
241 {
242  int i;
243  char *ap, *ap0;
244  uint64_t component_len;
245  if (!sep) sep = ", ";
246  component_len = 24LL + strlen(sep);
247  if (count >= (INT_MAX - 1)/component_len)
248  return NULL;
249  ap = av_malloc(component_len * count + 1);
250  if (!ap)
251  return NULL;
252  ap0 = ap;
253  ap[0] = '\0';
254  for (i = 0; i < count; i++) {
255  unsigned l = snprintf(ap, component_len, "%.15g%s", dp[i], sep);
256  if(l >= component_len) {
257  av_free(ap0);
258  return NULL;
259  }
260  ap += l;
261  }
262  ap0[strlen(ap0) - strlen(sep)] = '\0';
263  return ap0;
264 }
265 
266 static int add_metadata(int count, int type,
267  const char *name, const char *sep, TiffContext *s, AVFrame *frame)
268 {
269  switch(type) {
270  case TIFF_DOUBLE: return ff_tadd_doubles_metadata(count, name, sep, &s->gb, s->le, &frame->metadata);
271  case TIFF_SHORT : return ff_tadd_shorts_metadata(count, name, sep, &s->gb, s->le, 0, &frame->metadata);
272  case TIFF_STRING: return ff_tadd_string_metadata(count, name, &s->gb, s->le, &frame->metadata);
273  default : return AVERROR_INVALIDDATA;
274  };
275 }
276 
277 /**
278  * Map stored raw sensor values into linear reference values (see: DNG Specification - Chapter 5)
279  */
280 static uint16_t av_always_inline dng_process_color16(uint16_t value,
281  const uint16_t *lut,
282  uint16_t black_level,
283  float scale_factor)
284 {
285  float value_norm;
286 
287  // Lookup table lookup
288  if (lut)
289  value = lut[value];
290 
291  // Black level subtraction
292  value = av_clip_uint16_c((unsigned)value - black_level);
293 
294  // Color scaling
295  value_norm = (float)value * scale_factor;
296 
297  value = av_clip_uint16_c(value_norm * 65535);
298 
299  return value;
300 }
301 
302 static uint16_t av_always_inline dng_process_color8(uint16_t value,
303  const uint16_t *lut,
304  uint16_t black_level,
305  float scale_factor)
306 {
307  return dng_process_color16(value, lut, black_level, scale_factor) >> 8;
308 }
309 
310 static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
311  const uint8_t *src, int src_stride, int width, int height,
312  int is_single_comp, int is_u16)
313 {
314  int line, col;
315  float scale_factor;
316 
317  scale_factor = 1.0f / (s->white_level - s->black_level);
318 
319  if (is_single_comp) {
320  if (!is_u16)
321  return; /* <= 8bpp unsupported */
322 
323  /* Image is double the width and half the height we need, each row comprises 2 rows of the output
324  (split vertically in the middle). */
325  for (line = 0; line < height / 2; line++) {
326  uint16_t *dst_u16 = (uint16_t *)dst;
327  uint16_t *src_u16 = (uint16_t *)src;
328 
329  /* Blit first half of input row row to initial row of output */
330  for (col = 0; col < width; col++)
331  *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level, scale_factor);
332 
333  /* Advance the destination pointer by a row (source pointer remains in the same place) */
334  dst += dst_stride * sizeof(uint16_t);
335  dst_u16 = (uint16_t *)dst;
336 
337  /* Blit second half of input row row to next row of output */
338  for (col = 0; col < width; col++)
339  *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level, scale_factor);
340 
341  dst += dst_stride * sizeof(uint16_t);
342  src += src_stride * sizeof(uint16_t);
343  }
344  } else {
345  /* Input and output image are the same size and the MJpeg decoder has done per-component
346  deinterleaving, so blitting here is straightforward. */
347  if (is_u16) {
348  for (line = 0; line < height; line++) {
349  uint16_t *dst_u16 = (uint16_t *)dst;
350  uint16_t *src_u16 = (uint16_t *)src;
351 
352  for (col = 0; col < width; col++)
353  *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level, scale_factor);
354 
355  dst += dst_stride * sizeof(uint16_t);
356  src += src_stride * sizeof(uint16_t);
357  }
358  } else {
359  for (line = 0; line < height; line++) {
360  uint8_t *dst_u8 = dst;
361  const uint8_t *src_u8 = src;
362 
363  for (col = 0; col < width; col++)
364  *dst_u8++ = dng_process_color8(*src_u8++, s->dng_lut, s->black_level, scale_factor);
365 
366  dst += dst_stride;
367  src += src_stride;
368  }
369  }
370  }
371 }
372 
374  unsigned int bpp, uint8_t* dst,
375  int usePtr, const uint8_t *src,
376  uint8_t c, int width, int offset)
377 {
378  switch (bpp) {
379  case 1:
380  while (--width >= 0) {
381  dst[(width+offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
382  dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
383  dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
384  dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
385  dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
386  dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
387  dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
388  dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
389  }
390  break;
391  case 2:
392  while (--width >= 0) {
393  dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
394  dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
395  dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
396  dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
397  }
398  break;
399  case 4:
400  while (--width >= 0) {
401  dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
402  dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
403  }
404  break;
405  case 10:
406  case 12:
407  case 14: {
408  uint16_t *dst16 = (uint16_t *)dst;
409  int is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
410  uint8_t shift = is_dng ? 0 : 16 - bpp;
411  GetBitContext gb;
412 
413  int ret = init_get_bits8(&gb, src, width);
414  av_assert1(ret >= 0);
415  for (int i = 0; i < s->width; i++) {
416  dst16[i] = get_bits(&gb, bpp) << shift;
417  }
418  }
419  break;
420  default:
421  if (usePtr) {
422  memcpy(dst + offset, src, width);
423  } else {
424  memset(dst + offset, c, width);
425  }
426  }
427 }
428 
429 static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
430 {
431  int i;
432 
433  av_fast_padded_malloc(&s->deinvert_buf, &s->deinvert_buf_size, size);
434  if (!s->deinvert_buf)
435  return AVERROR(ENOMEM);
436  for (i = 0; i < size; i++)
437  s->deinvert_buf[i] = ff_reverse[src[i]];
438 
439  return 0;
440 }
441 
442 static void unpack_gray(TiffContext *s, AVFrame *p,
443  const uint8_t *src, int lnum, int width, int bpp)
444 {
445  GetBitContext gb;
446  uint16_t *dst = (uint16_t *)(p->data[0] + lnum * p->linesize[0]);
447 
448  int ret = init_get_bits8(&gb, src, width);
449  av_assert1(ret >= 0);
450 
451  for (int i = 0; i < s->width; i++) {
452  dst[i] = get_bits(&gb, bpp);
453  }
454 }
455 
456 static void unpack_yuv(TiffContext *s, AVFrame *p,
457  const uint8_t *src, int lnum)
458 {
459  int i, j, k;
460  int w = (s->width - 1) / s->subsampling[0] + 1;
461  uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
462  uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
463  if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) {
464  for (i = 0; i < w; i++) {
465  for (j = 0; j < s->subsampling[1]; j++)
466  for (k = 0; k < s->subsampling[0]; k++)
467  p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
468  FFMIN(i * s->subsampling[0] + k, s->width-1)] = *src++;
469  *pu++ = *src++;
470  *pv++ = *src++;
471  }
472  }else{
473  for (i = 0; i < w; i++) {
474  for (j = 0; j < s->subsampling[1]; j++)
475  for (k = 0; k < s->subsampling[0]; k++)
476  p->data[0][(lnum + j) * p->linesize[0] +
477  i * s->subsampling[0] + k] = *src++;
478  *pu++ = *src++;
479  *pv++ = *src++;
480  }
481  }
482 }
483 
484 #if CONFIG_ZLIB
485 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
486  int size)
487 {
488  z_stream zstream = { 0 };
489  int zret;
490 
491  zstream.next_in = src;
492  zstream.avail_in = size;
493  zstream.next_out = dst;
494  zstream.avail_out = *len;
495  zret = inflateInit(&zstream);
496  if (zret != Z_OK) {
497  av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
498  return zret;
499  }
500  zret = inflate(&zstream, Z_SYNC_FLUSH);
501  inflateEnd(&zstream);
502  *len = zstream.total_out;
503  return zret == Z_STREAM_END ? Z_OK : zret;
504 }
505 
506 static int tiff_unpack_zlib(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
507  const uint8_t *src, int size, int width, int lines,
508  int strip_start, int is_yuv)
509 {
510  uint8_t *zbuf;
511  unsigned long outlen;
512  int ret, line;
513  outlen = width * lines;
514  zbuf = av_malloc(outlen);
515  if (!zbuf)
516  return AVERROR(ENOMEM);
517  if (s->fill_order) {
518  if ((ret = deinvert_buffer(s, src, size)) < 0) {
519  av_free(zbuf);
520  return ret;
521  }
522  src = s->deinvert_buf;
523  }
524  ret = tiff_uncompress(zbuf, &outlen, src, size);
525  if (ret != Z_OK) {
526  av_log(s->avctx, AV_LOG_ERROR,
527  "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
528  (unsigned long)width * lines, ret);
529  av_free(zbuf);
530  return AVERROR_UNKNOWN;
531  }
532  src = zbuf;
533  for (line = 0; line < lines; line++) {
534  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
535  horizontal_fill(s, s->bpp, dst, 1, src, 0, width, 0);
536  } else {
537  memcpy(dst, src, width);
538  }
539  if (is_yuv) {
540  unpack_yuv(s, p, dst, strip_start + line);
541  line += s->subsampling[1] - 1;
542  }
543  dst += stride;
544  src += width;
545  }
546  av_free(zbuf);
547  return 0;
548 }
549 #endif
550 
551 #if CONFIG_LZMA
552 static int tiff_uncompress_lzma(uint8_t *dst, uint64_t *len, const uint8_t *src,
553  int size)
554 {
555  lzma_stream stream = LZMA_STREAM_INIT;
556  lzma_ret ret;
557 
558  stream.next_in = (uint8_t *)src;
559  stream.avail_in = size;
560  stream.next_out = dst;
561  stream.avail_out = *len;
562  ret = lzma_stream_decoder(&stream, UINT64_MAX, 0);
563  if (ret != LZMA_OK) {
564  av_log(NULL, AV_LOG_ERROR, "LZMA init error: %d\n", ret);
565  return ret;
566  }
567  ret = lzma_code(&stream, LZMA_RUN);
568  lzma_end(&stream);
569  *len = stream.total_out;
570  return ret == LZMA_STREAM_END ? LZMA_OK : ret;
571 }
572 
573 static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
574  const uint8_t *src, int size, int width, int lines,
575  int strip_start, int is_yuv)
576 {
577  uint64_t outlen = width * (uint64_t)lines;
578  int ret, line;
579  uint8_t *buf = av_malloc(outlen);
580  if (!buf)
581  return AVERROR(ENOMEM);
582  if (s->fill_order) {
583  if ((ret = deinvert_buffer(s, src, size)) < 0) {
584  av_free(buf);
585  return ret;
586  }
587  src = s->deinvert_buf;
588  }
589  ret = tiff_uncompress_lzma(buf, &outlen, src, size);
590  if (ret != LZMA_OK) {
591  av_log(s->avctx, AV_LOG_ERROR,
592  "Uncompressing failed (%"PRIu64" of %"PRIu64") with error %d\n", outlen,
593  (uint64_t)width * lines, ret);
594  av_free(buf);
595  return AVERROR_UNKNOWN;
596  }
597  src = buf;
598  for (line = 0; line < lines; line++) {
599  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
600  horizontal_fill(s, s->bpp, dst, 1, src, 0, width, 0);
601  } else {
602  memcpy(dst, src, width);
603  }
604  if (is_yuv) {
605  unpack_yuv(s, p, dst, strip_start + line);
606  line += s->subsampling[1] - 1;
607  }
608  dst += stride;
609  src += width;
610  }
611  av_free(buf);
612  return 0;
613 }
614 #endif
615 
616 static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
617  const uint8_t *src, int size, int width, int lines)
618 {
619  int i, ret = 0;
620  int line;
621  uint8_t *src2;
622 
623  av_fast_padded_malloc(&s->fax_buffer, &s->fax_buffer_size, size);
624  src2 = s->fax_buffer;
625 
626  if (!src2) {
627  av_log(s->avctx, AV_LOG_ERROR,
628  "Error allocating temporary buffer\n");
629  return AVERROR(ENOMEM);
630  }
631 
632  if (!s->fill_order) {
633  memcpy(src2, src, size);
634  } else {
635  for (i = 0; i < size; i++)
636  src2[i] = ff_reverse[src[i]];
637  }
638  memset(src2 + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
639  ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
640  s->compr, s->fax_opts);
641  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
642  for (line = 0; line < lines; line++) {
643  horizontal_fill(s, s->bpp, dst, 1, dst, 0, width, 0);
644  dst += stride;
645  }
646  return ret;
647 }
648 
650  int tile_byte_count, int dst_x, int dst_y, int w, int h)
651 {
652  TiffContext *s = avctx->priv_data;
653  uint8_t *dst_data, *src_data;
654  uint32_t dst_offset; /* offset from dst buffer in pixels */
655  int is_single_comp, is_u16, pixel_size;
656  int ret;
657 
658  if (tile_byte_count < 0 || tile_byte_count > bytestream2_get_bytes_left(&s->gb))
659  return AVERROR_INVALIDDATA;
660 
661  /* Prepare a packet and send to the MJPEG decoder */
662  av_packet_unref(s->jpkt);
663  s->jpkt->data = (uint8_t*)s->gb.buffer;
664  s->jpkt->size = tile_byte_count;
665 
666  if (s->is_bayer) {
667  MJpegDecodeContext *mjpegdecctx = s->avctx_mjpeg->priv_data;
668  /* We have to set this information here, there is no way to know if a given JPEG is a DNG-embedded
669  image or not from its own data (and we need that information when decoding it). */
670  mjpegdecctx->bayer = 1;
671  }
672 
673  ret = avcodec_send_packet(s->avctx_mjpeg, s->jpkt);
674  if (ret < 0) {
675  av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
676  return ret;
677  }
678 
679  ret = avcodec_receive_frame(s->avctx_mjpeg, s->jpgframe);
680  if (ret < 0) {
681  av_log(avctx, AV_LOG_ERROR, "JPEG decoding error: %s.\n", av_err2str(ret));
682 
683  /* Normally skip, error if explode */
684  if (avctx->err_recognition & AV_EF_EXPLODE)
685  return AVERROR_INVALIDDATA;
686  else
687  return 0;
688  }
689 
690  is_u16 = (s->bpp > 8);
691 
692  /* Copy the outputted tile's pixels from 'jpgframe' to 'frame' (final buffer) */
693 
694  if (s->jpgframe->width != s->avctx_mjpeg->width ||
695  s->jpgframe->height != s->avctx_mjpeg->height ||
696  s->jpgframe->format != s->avctx_mjpeg->pix_fmt)
697  return AVERROR_INVALIDDATA;
698 
699  /* See dng_blit for explanation */
700  if (s->avctx_mjpeg->width == w * 2 &&
701  s->avctx_mjpeg->height == h / 2 &&
702  s->avctx_mjpeg->pix_fmt == AV_PIX_FMT_GRAY16LE) {
703  is_single_comp = 1;
704  } else if (s->avctx_mjpeg->width >= w &&
705  s->avctx_mjpeg->height >= h &&
706  s->avctx_mjpeg->pix_fmt == (is_u16 ? AV_PIX_FMT_GRAY16 : AV_PIX_FMT_GRAY8)
707  ) {
708  is_single_comp = 0;
709  } else
710  return AVERROR_INVALIDDATA;
711 
712  pixel_size = (is_u16 ? sizeof(uint16_t) : sizeof(uint8_t));
713 
714  if (is_single_comp && !is_u16) {
715  av_log(s->avctx, AV_LOG_ERROR, "DNGs with bpp <= 8 and 1 component are unsupported\n");
716  av_frame_unref(s->jpgframe);
717  return AVERROR_PATCHWELCOME;
718  }
719 
720  dst_offset = dst_x + frame->linesize[0] * dst_y / pixel_size;
721  dst_data = frame->data[0] + dst_offset * pixel_size;
722  src_data = s->jpgframe->data[0];
723 
724  dng_blit(s,
725  dst_data,
726  frame->linesize[0] / pixel_size,
727  src_data,
728  s->jpgframe->linesize[0] / pixel_size,
729  w,
730  h,
731  is_single_comp,
732  is_u16);
733 
734  av_frame_unref(s->jpgframe);
735 
736  return 0;
737 }
738 
740  const uint8_t *src, int size, int strip_start, int lines)
741 {
742  PutByteContext pb;
743  int c, line, pixels, code, ret;
744  const uint8_t *ssrc = src;
745  int width = ((s->width * s->bpp) + 7) >> 3;
747  int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
748  (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
749  desc->nb_components >= 3;
750  int is_dng;
751 
752  if (s->planar)
753  width /= s->bppcount;
754 
755  if (size <= 0)
756  return AVERROR_INVALIDDATA;
757 
758  if (is_yuv) {
759  int bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
760  s->subsampling[0] * s->subsampling[1] + 7) >> 3;
761  av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
762  if (s->yuv_line == NULL) {
763  av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
764  return AVERROR(ENOMEM);
765  }
766  dst = s->yuv_line;
767  stride = 0;
768 
769  width = (s->width - 1) / s->subsampling[0] + 1;
770  width = width * s->subsampling[0] * s->subsampling[1] + 2*width;
771  av_assert0(width <= bytes_per_row);
772  av_assert0(s->bpp == 24);
773  }
774  if (s->is_bayer) {
775  av_assert0(width == (s->bpp * s->width + 7) >> 3);
776  }
777  av_assert0(!(s->is_bayer && is_yuv));
778  if (p->format == AV_PIX_FMT_GRAY12) {
779  av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, width);
780  if (s->yuv_line == NULL) {
781  av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
782  return AVERROR(ENOMEM);
783  }
784  dst = s->yuv_line;
785  stride = 0;
786  }
787 
788  if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
789 #if CONFIG_ZLIB
790  return tiff_unpack_zlib(s, p, dst, stride, src, size, width, lines,
791  strip_start, is_yuv);
792 #else
793  av_log(s->avctx, AV_LOG_ERROR,
794  "zlib support not enabled, "
795  "deflate compression not supported\n");
796  return AVERROR(ENOSYS);
797 #endif
798  }
799  if (s->compr == TIFF_LZMA) {
800 #if CONFIG_LZMA
801  return tiff_unpack_lzma(s, p, dst, stride, src, size, width, lines,
802  strip_start, is_yuv);
803 #else
804  av_log(s->avctx, AV_LOG_ERROR,
805  "LZMA support not enabled\n");
806  return AVERROR(ENOSYS);
807 #endif
808  }
809  if (s->compr == TIFF_LZW) {
810  if (s->fill_order) {
811  if ((ret = deinvert_buffer(s, src, size)) < 0)
812  return ret;
813  ssrc = src = s->deinvert_buf;
814  }
815  if (size > 1 && !src[0] && (src[1]&1)) {
816  av_log(s->avctx, AV_LOG_ERROR, "Old style LZW is unsupported\n");
817  }
818  if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
819  av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
820  return ret;
821  }
822  for (line = 0; line < lines; line++) {
823  pixels = ff_lzw_decode(s->lzw, dst, width);
824  if (pixels < width) {
825  av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
826  pixels, width);
827  return AVERROR_INVALIDDATA;
828  }
829  if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
830  horizontal_fill(s, s->bpp, dst, 1, dst, 0, width, 0);
831  if (is_yuv) {
832  unpack_yuv(s, p, dst, strip_start + line);
833  line += s->subsampling[1] - 1;
834  } else if (p->format == AV_PIX_FMT_GRAY12) {
835  unpack_gray(s, p, dst, strip_start + line, width, s->bpp);
836  }
837  dst += stride;
838  }
839  return 0;
840  }
841  if (s->compr == TIFF_CCITT_RLE ||
842  s->compr == TIFF_G3 ||
843  s->compr == TIFF_G4) {
844  if (is_yuv || p->format == AV_PIX_FMT_GRAY12)
845  return AVERROR_INVALIDDATA;
846 
847  return tiff_unpack_fax(s, dst, stride, src, size, width, lines);
848  }
849 
850  bytestream2_init(&s->gb, src, size);
851  bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * lines));
852 
853  is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
854 
855  /* Decode JPEG-encoded DNGs with strips */
856  if (s->compr == TIFF_NEWJPEG && is_dng) {
857  if (s->strips > 1) {
858  av_log(s->avctx, AV_LOG_ERROR, "More than one DNG JPEG strips unsupported\n");
859  return AVERROR_PATCHWELCOME;
860  }
861  if (!s->is_bayer)
862  return AVERROR_PATCHWELCOME;
863  if ((ret = dng_decode_jpeg(s->avctx, p, s->stripsize, 0, 0, s->width, s->height)) < 0)
864  return ret;
865  return 0;
866  }
867 
868  if (is_dng && stride == 0)
869  return AVERROR_INVALIDDATA;
870 
871  for (line = 0; line < lines; line++) {
872  if (src - ssrc > size) {
873  av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
874  return AVERROR_INVALIDDATA;
875  }
876 
877  if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
878  break;
879  bytestream2_seek_p(&pb, stride * line, SEEK_SET);
880  switch (s->compr) {
881  case TIFF_RAW:
882  if (ssrc + size - src < width)
883  return AVERROR_INVALIDDATA;
884 
885  if (!s->fill_order) {
886  horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 || s->is_bayer),
887  dst, 1, src, 0, width, 0);
888  } else {
889  int i;
890  for (i = 0; i < width; i++)
891  dst[i] = ff_reverse[src[i]];
892  }
893 
894  /* Color processing for DNG images with uncompressed strips (non-tiled) */
895  if (is_dng) {
896  int is_u16, pixel_size_bytes, pixel_size_bits, elements;
897 
898  is_u16 = (s->bpp / s->bppcount > 8);
899  pixel_size_bits = (is_u16 ? 16 : 8);
900  pixel_size_bytes = (is_u16 ? sizeof(uint16_t) : sizeof(uint8_t));
901 
902  elements = width / pixel_size_bytes * pixel_size_bits / s->bpp * s->bppcount; // need to account for [1, 16] bpp
903  av_assert0 (elements * pixel_size_bytes <= FFABS(stride));
904  dng_blit(s,
905  dst,
906  0, // no stride, only 1 line
907  dst,
908  0, // no stride, only 1 line
909  elements,
910  1,
911  0, // single-component variation is only preset in JPEG-encoded DNGs
912  is_u16);
913  }
914 
915  src += width;
916  break;
917  case TIFF_PACKBITS:
918  for (pixels = 0; pixels < width;) {
919  if (ssrc + size - src < 2) {
920  av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
921  return AVERROR_INVALIDDATA;
922  }
923  code = s->fill_order ? (int8_t) ff_reverse[*src++]: (int8_t) *src++;
924  if (code >= 0) {
925  code++;
926  if (pixels + code > width ||
927  ssrc + size - src < code) {
928  av_log(s->avctx, AV_LOG_ERROR,
929  "Copy went out of bounds\n");
930  return AVERROR_INVALIDDATA;
931  }
932  horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
933  dst, 1, src, 0, code, pixels);
934  src += code;
935  pixels += code;
936  } else if (code != -128) { // -127..-1
937  code = (-code) + 1;
938  if (pixels + code > width) {
939  av_log(s->avctx, AV_LOG_ERROR,
940  "Run went out of bounds\n");
941  return AVERROR_INVALIDDATA;
942  }
943  c = *src++;
944  horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
945  dst, 0, NULL, c, code, pixels);
946  pixels += code;
947  }
948  }
949  if (s->fill_order) {
950  int i;
951  for (i = 0; i < width; i++)
952  dst[i] = ff_reverse[dst[i]];
953  }
954  break;
955  }
956  if (is_yuv) {
957  unpack_yuv(s, p, dst, strip_start + line);
958  line += s->subsampling[1] - 1;
959  } else if (p->format == AV_PIX_FMT_GRAY12) {
960  unpack_gray(s, p, dst, strip_start + line, width, s->bpp);
961  }
962  dst += stride;
963  }
964  return 0;
965 }
966 
968  const AVPacket *avpkt)
969 {
970  TiffContext *s = avctx->priv_data;
971  int tile_idx;
972  int tile_offset_offset, tile_offset;
973  int tile_byte_count_offset, tile_byte_count;
974  int tile_count_x, tile_count_y;
975  int tile_width, tile_length;
976  int has_width_leftover, has_height_leftover;
977  int tile_x = 0, tile_y = 0;
978  int pos_x = 0, pos_y = 0;
979  int ret;
980 
981  if (s->tile_width <= 0 || s->tile_length <= 0)
982  return AVERROR_INVALIDDATA;
983 
984  has_width_leftover = (s->width % s->tile_width != 0);
985  has_height_leftover = (s->height % s->tile_length != 0);
986 
987  /* Calculate tile counts (round up) */
988  tile_count_x = (s->width + s->tile_width - 1) / s->tile_width;
989  tile_count_y = (s->height + s->tile_length - 1) / s->tile_length;
990 
991  /* Iterate over the number of tiles */
992  for (tile_idx = 0; tile_idx < tile_count_x * tile_count_y; tile_idx++) {
993  tile_x = tile_idx % tile_count_x;
994  tile_y = tile_idx / tile_count_x;
995 
996  if (has_width_leftover && tile_x == tile_count_x - 1) // If on the right-most tile
997  tile_width = s->width % s->tile_width;
998  else
999  tile_width = s->tile_width;
1000 
1001  if (has_height_leftover && tile_y == tile_count_y - 1) // If on the bottom-most tile
1002  tile_length = s->height % s->tile_length;
1003  else
1004  tile_length = s->tile_length;
1005 
1006  /* Read tile offset */
1007  tile_offset_offset = s->tile_offsets_offset + tile_idx * sizeof(int);
1008  bytestream2_seek(&s->gb, tile_offset_offset, SEEK_SET);
1009  tile_offset = ff_tget_long(&s->gb, s->le);
1010 
1011  /* Read tile byte size */
1012  tile_byte_count_offset = s->tile_byte_counts_offset + tile_idx * sizeof(int);
1013  bytestream2_seek(&s->gb, tile_byte_count_offset, SEEK_SET);
1014  tile_byte_count = ff_tget_long(&s->gb, s->le);
1015 
1016  /* Seek to tile data */
1017  bytestream2_seek(&s->gb, tile_offset, SEEK_SET);
1018 
1019  /* Decode JPEG tile and copy it in the reference frame */
1020  ret = dng_decode_jpeg(avctx, frame, tile_byte_count, pos_x, pos_y, tile_width, tile_length);
1021 
1022  if (ret < 0)
1023  return ret;
1024 
1025  /* Advance current positions */
1026  pos_x += tile_width;
1027  if (tile_x == tile_count_x - 1) { // If on the right edge
1028  pos_x = 0;
1029  pos_y += tile_length;
1030  }
1031  }
1032 
1033  /* Frame is ready to be output */
1035  frame->key_frame = 1;
1036 
1037  return avpkt->size;
1038 }
1039 
1041 {
1042  int ret;
1043  int create_gray_palette = 0;
1044 
1045  // make sure there is no aliasing in the following switch
1046  if (s->bpp >= 100 || s->bppcount >= 10) {
1047  av_log(s->avctx, AV_LOG_ERROR,
1048  "Unsupported image parameters: bpp=%d, bppcount=%d\n",
1049  s->bpp, s->bppcount);
1050  return AVERROR_INVALIDDATA;
1051  }
1052 
1053  switch (s->planar * 1000 + s->bpp * 10 + s->bppcount + s->is_bayer * 10000) {
1054  case 11:
1055  if (!s->palette_is_set) {
1056  s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
1057  break;
1058  }
1059  case 21:
1060  case 41:
1061  s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
1062  if (!s->palette_is_set) {
1063  create_gray_palette = 1;
1064  }
1065  break;
1066  case 81:
1067  s->avctx->pix_fmt = s->palette_is_set ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
1068  break;
1069  case 121:
1070  s->avctx->pix_fmt = AV_PIX_FMT_GRAY12;
1071  break;
1072  case 10081:
1073  switch (AV_RL32(s->pattern)) {
1074  case 0x02010100:
1075  s->avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB8;
1076  break;
1077  case 0x00010102:
1078  s->avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR8;
1079  break;
1080  case 0x01000201:
1081  s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG8;
1082  break;
1083  case 0x01020001:
1084  s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG8;
1085  break;
1086  default:
1087  av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n",
1088  AV_RL32(s->pattern));
1089  return AVERROR_PATCHWELCOME;
1090  }
1091  break;
1092  case 10101:
1093  case 10121:
1094  case 10141:
1095  case 10161:
1096  switch (AV_RL32(s->pattern)) {
1097  case 0x02010100:
1098  s->avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB16;
1099  break;
1100  case 0x00010102:
1101  s->avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR16;
1102  break;
1103  case 0x01000201:
1104  s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG16;
1105  break;
1106  case 0x01020001:
1107  s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG16;
1108  break;
1109  default:
1110  av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n",
1111  AV_RL32(s->pattern));
1112  return AVERROR_PATCHWELCOME;
1113  }
1114  break;
1115  case 243:
1116  if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1117  if (s->subsampling[0] == 1 && s->subsampling[1] == 1) {
1118  s->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
1119  } else if (s->subsampling[0] == 2 && s->subsampling[1] == 1) {
1120  s->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
1121  } else if (s->subsampling[0] == 4 && s->subsampling[1] == 1) {
1122  s->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
1123  } else if (s->subsampling[0] == 1 && s->subsampling[1] == 2) {
1124  s->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
1125  } else if (s->subsampling[0] == 2 && s->subsampling[1] == 2) {
1126  s->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1127  } else if (s->subsampling[0] == 4 && s->subsampling[1] == 4) {
1128  s->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
1129  } else {
1130  av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr subsampling\n");
1131  return AVERROR_PATCHWELCOME;
1132  }
1133  } else
1134  s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
1135  break;
1136  case 161:
1137  s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
1138  break;
1139  case 162:
1140  s->avctx->pix_fmt = AV_PIX_FMT_YA8;
1141  break;
1142  case 322:
1143  s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YA16LE : AV_PIX_FMT_YA16BE;
1144  break;
1145  case 324:
1146  s->avctx->pix_fmt = s->photometric == TIFF_PHOTOMETRIC_SEPARATED ? AV_PIX_FMT_RGB0 : AV_PIX_FMT_RGBA;
1147  break;
1148  case 405:
1149  if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED)
1150  s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
1151  else {
1152  av_log(s->avctx, AV_LOG_ERROR,
1153  "bpp=40 without PHOTOMETRIC_SEPARATED is unsupported\n");
1154  return AVERROR_PATCHWELCOME;
1155  }
1156  break;
1157  case 483:
1158  s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
1159  break;
1160  case 644:
1161  s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGBA64BE;
1162  break;
1163  case 1243:
1164  s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
1165  break;
1166  case 1324:
1167  s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
1168  break;
1169  case 1483:
1170  s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRP16LE : AV_PIX_FMT_GBRP16BE;
1171  break;
1172  case 1644:
1173  s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAP16LE : AV_PIX_FMT_GBRAP16BE;
1174  break;
1175  default:
1176  av_log(s->avctx, AV_LOG_ERROR,
1177  "This format is not supported (bpp=%d, bppcount=%d)\n",
1178  s->bpp, s->bppcount);
1179  return AVERROR_INVALIDDATA;
1180  }
1181 
1182  if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1183  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1184  if((desc->flags & AV_PIX_FMT_FLAG_RGB) ||
1185  !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
1186  desc->nb_components < 3) {
1187  av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr variant\n");
1188  return AVERROR_INVALIDDATA;
1189  }
1190  }
1191 
1192  if (s->width != s->avctx->width || s->height != s->avctx->height) {
1193  ret = ff_set_dimensions(s->avctx, s->width, s->height);
1194  if (ret < 0)
1195  return ret;
1196  }
1197  if ((ret = ff_thread_get_buffer(s->avctx, frame, 0)) < 0)
1198  return ret;
1199  if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1200  if (!create_gray_palette)
1201  memcpy(frame->f->data[1], s->palette, sizeof(s->palette));
1202  else {
1203  /* make default grayscale pal */
1204  int i;
1205  uint32_t *pal = (uint32_t *)frame->f->data[1];
1206  for (i = 0; i < 1<<s->bpp; i++)
1207  pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
1208  }
1209  }
1210  return 0;
1211 }
1212 
1213 static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
1214 {
1215  int offset = tag == TIFF_YRES ? 2 : 0;
1216  s->res[offset++] = num;
1217  s->res[offset] = den;
1218  if (s->res[0] && s->res[1] && s->res[2] && s->res[3]) {
1219  uint64_t num = s->res[2] * (uint64_t)s->res[1];
1220  uint64_t den = s->res[0] * (uint64_t)s->res[3];
1221  if (num > INT64_MAX || den > INT64_MAX) {
1222  num = num >> 1;
1223  den = den >> 1;
1224  }
1225  av_reduce(&s->avctx->sample_aspect_ratio.num, &s->avctx->sample_aspect_ratio.den,
1226  num, den, INT32_MAX);
1227  if (!s->avctx->sample_aspect_ratio.den)
1228  s->avctx->sample_aspect_ratio = (AVRational) {0, 1};
1229  }
1230 }
1231 
1233 {
1234  AVFrameSideData *sd;
1235  GetByteContext gb_temp;
1236  unsigned tag, type, count, off, value = 0, value2 = 1; // value2 is a denominator so init. to 1
1237  int i, start;
1238  int pos;
1239  int ret;
1240  double *dp;
1241 
1242  ret = ff_tread_tag(&s->gb, s->le, &tag, &type, &count, &start);
1243  if (ret < 0) {
1244  goto end;
1245  }
1246  if (tag <= s->last_tag)
1247  return AVERROR_INVALIDDATA;
1248 
1249  // We ignore TIFF_STRIP_SIZE as it is sometimes in the logic but wrong order around TIFF_STRIP_OFFS
1250  if (tag != TIFF_STRIP_SIZE)
1251  s->last_tag = tag;
1252 
1253  off = bytestream2_tell(&s->gb);
1254  if (count == 1) {
1255  switch (type) {
1256  case TIFF_BYTE:
1257  case TIFF_SHORT:
1258  case TIFF_LONG:
1259  value = ff_tget(&s->gb, type, s->le);
1260  break;
1261  case TIFF_RATIONAL:
1262  value = ff_tget(&s->gb, TIFF_LONG, s->le);
1263  value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1264  if (!value2) {
1265  av_log(s->avctx, AV_LOG_ERROR, "Invalid denominator in rational\n");
1266  return AVERROR_INVALIDDATA;
1267  }
1268 
1269  break;
1270  case TIFF_STRING:
1271  if (count <= 4) {
1272  break;
1273  }
1274  default:
1275  value = UINT_MAX;
1276  }
1277  }
1278 
1279  switch (tag) {
1280  case TIFF_SUBFILE:
1281  s->is_thumbnail = (value != 0);
1282  break;
1283  case TIFF_WIDTH:
1284  if (value > INT_MAX)
1285  return AVERROR_INVALIDDATA;
1286  s->width = value;
1287  break;
1288  case TIFF_HEIGHT:
1289  if (value > INT_MAX)
1290  return AVERROR_INVALIDDATA;
1291  s->height = value;
1292  break;
1293  case TIFF_BPP:
1294  if (count > 5 || count <= 0) {
1295  av_log(s->avctx, AV_LOG_ERROR,
1296  "This format is not supported (bpp=%d, %d components)\n",
1297  value, count);
1298  return AVERROR_INVALIDDATA;
1299  }
1300  s->bppcount = count;
1301  if (count == 1)
1302  s->bpp = value;
1303  else {
1304  switch (type) {
1305  case TIFF_BYTE:
1306  case TIFF_SHORT:
1307  case TIFF_LONG:
1308  s->bpp = 0;
1309  if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
1310  return AVERROR_INVALIDDATA;
1311  for (i = 0; i < count; i++)
1312  s->bpp += ff_tget(&s->gb, type, s->le);
1313  break;
1314  default:
1315  s->bpp = -1;
1316  }
1317  }
1318  break;
1320  if (count != 1) {
1321  av_log(s->avctx, AV_LOG_ERROR,
1322  "Samples per pixel requires a single value, many provided\n");
1323  return AVERROR_INVALIDDATA;
1324  }
1325  if (value > 5 || value <= 0) {
1326  av_log(s->avctx, AV_LOG_ERROR,
1327  "Invalid samples per pixel %d\n", value);
1328  return AVERROR_INVALIDDATA;
1329  }
1330  if (s->bppcount == 1)
1331  s->bpp *= value;
1332  s->bppcount = value;
1333  break;
1334  case TIFF_COMPR:
1335  s->compr = value;
1336  av_log(s->avctx, AV_LOG_DEBUG, "compression: %d\n", s->compr);
1337  s->predictor = 0;
1338  switch (s->compr) {
1339  case TIFF_RAW:
1340  case TIFF_PACKBITS:
1341  case TIFF_LZW:
1342  case TIFF_CCITT_RLE:
1343  break;
1344  case TIFF_G3:
1345  case TIFF_G4:
1346  s->fax_opts = 0;
1347  break;
1348  case TIFF_DEFLATE:
1349  case TIFF_ADOBE_DEFLATE:
1350 #if CONFIG_ZLIB
1351  break;
1352 #else
1353  av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
1354  return AVERROR(ENOSYS);
1355 #endif
1356  case TIFF_JPEG:
1357  case TIFF_NEWJPEG:
1358  s->is_jpeg = 1;
1359  break;
1360  case TIFF_LZMA:
1361 #if CONFIG_LZMA
1362  break;
1363 #else
1364  av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n");
1365  return AVERROR(ENOSYS);
1366 #endif
1367  default:
1368  av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
1369  s->compr);
1370  return AVERROR_INVALIDDATA;
1371  }
1372  break;
1373  case TIFF_ROWSPERSTRIP:
1374  if (!value || (type == TIFF_LONG && value == UINT_MAX))
1375  value = s->height;
1376  s->rps = FFMIN(value, s->height);
1377  break;
1378  case TIFF_STRIP_OFFS:
1379  if (count == 1) {
1380  if (value > INT_MAX) {
1381  av_log(s->avctx, AV_LOG_ERROR,
1382  "strippos %u too large\n", value);
1383  return AVERROR_INVALIDDATA;
1384  }
1385  s->strippos = 0;
1386  s->stripoff = value;
1387  } else
1388  s->strippos = off;
1389  s->strips = count;
1390  if (s->strips == 1)
1391  s->rps = s->height;
1392  s->sot = type;
1393  break;
1394  case TIFF_STRIP_SIZE:
1395  if (count == 1) {
1396  if (value > INT_MAX) {
1397  av_log(s->avctx, AV_LOG_ERROR,
1398  "stripsize %u too large\n", value);
1399  return AVERROR_INVALIDDATA;
1400  }
1401  s->stripsizesoff = 0;
1402  s->stripsize = value;
1403  s->strips = 1;
1404  } else {
1405  s->stripsizesoff = off;
1406  }
1407  s->strips = count;
1408  s->sstype = type;
1409  break;
1410  case TIFF_XRES:
1411  case TIFF_YRES:
1412  set_sar(s, tag, value, value2);
1413  break;
1414  case TIFF_TILE_OFFSETS:
1415  s->tile_offsets_offset = off;
1416  s->is_tiled = 1;
1417  break;
1418  case TIFF_TILE_BYTE_COUNTS:
1419  s->tile_byte_counts_offset = off;
1420  break;
1421  case TIFF_TILE_LENGTH:
1422  if (value > INT_MAX)
1423  return AVERROR_INVALIDDATA;
1424  s->tile_length = value;
1425  break;
1426  case TIFF_TILE_WIDTH:
1427  if (value > INT_MAX)
1428  return AVERROR_INVALIDDATA;
1429  s->tile_width = value;
1430  break;
1431  case TIFF_PREDICTOR:
1432  if (value > INT_MAX)
1433  return AVERROR_INVALIDDATA;
1434  s->predictor = value;
1435  break;
1436  case TIFF_SUB_IFDS:
1437  if (count == 1)
1438  s->sub_ifd = value;
1439  else if (count > 1)
1440  s->sub_ifd = ff_tget(&s->gb, TIFF_LONG, s->le); /** Only get the first SubIFD */
1441  break;
1443  if (count < 1 || count > FF_ARRAY_ELEMS(s->dng_lut))
1444  return AVERROR_INVALIDDATA;
1445  for (int i = 0; i < count; i++)
1446  s->dng_lut[i] = ff_tget(&s->gb, type, s->le);
1447  break;
1448  case DNG_BLACK_LEVEL:
1449  if (count > 1) { /* Use the first value in the pattern (assume they're all the same) */
1450  if (type == TIFF_RATIONAL) {
1451  value = ff_tget(&s->gb, TIFF_LONG, s->le);
1452  value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1453  if (!value2) {
1454  av_log(s->avctx, AV_LOG_ERROR, "Invalid black level denominator\n");
1455  return AVERROR_INVALIDDATA;
1456  }
1457 
1458  s->black_level = value / value2;
1459  } else
1460  s->black_level = ff_tget(&s->gb, type, s->le);
1461  av_log(s->avctx, AV_LOG_WARNING, "Assuming black level pattern values are identical\n");
1462  } else {
1463  s->black_level = value / value2;
1464  }
1465  break;
1466  case DNG_WHITE_LEVEL:
1467  s->white_level = value;
1468  break;
1469  case TIFF_CFA_PATTERN_DIM:
1470  if (count != 2 || (ff_tget(&s->gb, type, s->le) != 2 &&
1471  ff_tget(&s->gb, type, s->le) != 2)) {
1472  av_log(s->avctx, AV_LOG_ERROR, "CFA Pattern dimensions are not 2x2\n");
1473  return AVERROR_INVALIDDATA;
1474  }
1475  break;
1476  case TIFF_CFA_PATTERN:
1477  s->is_bayer = 1;
1478  s->pattern[0] = ff_tget(&s->gb, type, s->le);
1479  s->pattern[1] = ff_tget(&s->gb, type, s->le);
1480  s->pattern[2] = ff_tget(&s->gb, type, s->le);
1481  s->pattern[3] = ff_tget(&s->gb, type, s->le);
1482  break;
1483  case TIFF_PHOTOMETRIC:
1484  switch (value) {
1487  case TIFF_PHOTOMETRIC_RGB:
1491  case TIFF_PHOTOMETRIC_CFA:
1492  case TIFF_PHOTOMETRIC_LINEAR_RAW: // Used by DNG images
1493  s->photometric = value;
1494  break;
1502  "PhotometricInterpretation 0x%04X",
1503  value);
1504  return AVERROR_PATCHWELCOME;
1505  default:
1506  av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
1507  "unknown\n", value);
1508  return AVERROR_INVALIDDATA;
1509  }
1510  break;
1511  case TIFF_FILL_ORDER:
1512  if (value < 1 || value > 2) {
1513  av_log(s->avctx, AV_LOG_ERROR,
1514  "Unknown FillOrder value %d, trying default one\n", value);
1515  value = 1;
1516  }
1517  s->fill_order = value - 1;
1518  break;
1519  case TIFF_PAL: {
1520  GetByteContext pal_gb[3];
1521  off = type_sizes[type];
1522  if (count / 3 > 256 ||
1523  bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
1524  return AVERROR_INVALIDDATA;
1525 
1526  pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
1527  bytestream2_skip(&pal_gb[1], count / 3 * off);
1528  bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
1529 
1530  off = (type_sizes[type] - 1) << 3;
1531  if (off > 31U) {
1532  av_log(s->avctx, AV_LOG_ERROR, "palette shift %d is out of range\n", off);
1533  return AVERROR_INVALIDDATA;
1534  }
1535 
1536  for (i = 0; i < count / 3; i++) {
1537  uint32_t p = 0xFF000000;
1538  p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16;
1539  p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8;
1540  p |= ff_tget(&pal_gb[2], type, s->le) >> off;
1541  s->palette[i] = p;
1542  }
1543  s->palette_is_set = 1;
1544  break;
1545  }
1546  case TIFF_PLANAR:
1547  s->planar = value == 2;
1548  break;
1550  if (count != 2) {
1551  av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n");
1552  return AVERROR_INVALIDDATA;
1553  }
1554  for (i = 0; i < count; i++) {
1555  s->subsampling[i] = ff_tget(&s->gb, type, s->le);
1556  if (s->subsampling[i] <= 0) {
1557  av_log(s->avctx, AV_LOG_ERROR, "subsampling %d is invalid\n", s->subsampling[i]);
1558  s->subsampling[i] = 1;
1559  return AVERROR_INVALIDDATA;
1560  }
1561  }
1562  break;
1563  case TIFF_T4OPTIONS:
1564  if (s->compr == TIFF_G3) {
1565  if (value > INT_MAX)
1566  return AVERROR_INVALIDDATA;
1567  s->fax_opts = value;
1568  }
1569  break;
1570  case TIFF_T6OPTIONS:
1571  if (s->compr == TIFF_G4) {
1572  if (value > INT_MAX)
1573  return AVERROR_INVALIDDATA;
1574  s->fax_opts = value;
1575  }
1576  break;
1577 #define ADD_METADATA(count, name, sep)\
1578  if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1579  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1580  goto end;\
1581  }
1583  ADD_METADATA(count, "ModelPixelScaleTag", NULL);
1584  break;
1586  ADD_METADATA(count, "ModelTransformationTag", NULL);
1587  break;
1588  case TIFF_MODEL_TIEPOINT:
1589  ADD_METADATA(count, "ModelTiepointTag", NULL);
1590  break;
1592  if (s->geotag_count) {
1593  avpriv_request_sample(s->avctx, "Multiple geo key directories");
1594  return AVERROR_INVALIDDATA;
1595  }
1596  ADD_METADATA(1, "GeoTIFF_Version", NULL);
1597  ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
1598  s->geotag_count = ff_tget_short(&s->gb, s->le);
1599  if (s->geotag_count > count / 4 - 1) {
1600  s->geotag_count = count / 4 - 1;
1601  av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
1602  }
1603  if ( bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4
1604  || s->geotag_count == 0) {
1605  s->geotag_count = 0;
1606  return -1;
1607  }
1608  s->geotags = av_mallocz_array(s->geotag_count, sizeof(TiffGeoTag));
1609  if (!s->geotags) {
1610  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1611  s->geotag_count = 0;
1612  goto end;
1613  }
1614  for (i = 0; i < s->geotag_count; i++) {
1615  s->geotags[i].key = ff_tget_short(&s->gb, s->le);
1616  s->geotags[i].type = ff_tget_short(&s->gb, s->le);
1617  s->geotags[i].count = ff_tget_short(&s->gb, s->le);
1618 
1619  if (!s->geotags[i].type)
1620  s->geotags[i].val = get_geokey_val(s->geotags[i].key, ff_tget_short(&s->gb, s->le));
1621  else
1622  s->geotags[i].offset = ff_tget_short(&s->gb, s->le);
1623  }
1624  break;
1626  if (count >= INT_MAX / sizeof(int64_t))
1627  return AVERROR_INVALIDDATA;
1628  if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
1629  return AVERROR_INVALIDDATA;
1630  dp = av_malloc_array(count, sizeof(double));
1631  if (!dp) {
1632  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1633  goto end;
1634  }
1635  for (i = 0; i < count; i++)
1636  dp[i] = ff_tget_double(&s->gb, s->le);
1637  for (i = 0; i < s->geotag_count; i++) {
1638  if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
1639  if (s->geotags[i].count == 0
1640  || s->geotags[i].offset + s->geotags[i].count > count) {
1641  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1642  } else if (s->geotags[i].val) {
1643  av_log(s->avctx, AV_LOG_WARNING, "Duplicate GeoTIFF key %d\n", s->geotags[i].key);
1644  } else {
1645  char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
1646  if (!ap) {
1647  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1648  av_freep(&dp);
1649  return AVERROR(ENOMEM);
1650  }
1651  s->geotags[i].val = ap;
1652  }
1653  }
1654  }
1655  av_freep(&dp);
1656  break;
1657  case TIFF_GEO_ASCII_PARAMS:
1658  pos = bytestream2_tell(&s->gb);
1659  for (i = 0; i < s->geotag_count; i++) {
1660  if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
1661  if (s->geotags[i].count == 0
1662  || s->geotags[i].offset + s->geotags[i].count > count) {
1663  av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1664  } else {
1665  char *ap;
1666 
1667  bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
1668  if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
1669  return AVERROR_INVALIDDATA;
1670  if (s->geotags[i].val)
1671  return AVERROR_INVALIDDATA;
1672  ap = av_malloc(s->geotags[i].count);
1673  if (!ap) {
1674  av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1675  return AVERROR(ENOMEM);
1676  }
1677  bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
1678  ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
1679  s->geotags[i].val = ap;
1680  }
1681  }
1682  }
1683  break;
1684  case TIFF_ICC_PROFILE:
1685  gb_temp = s->gb;
1686  bytestream2_seek(&gb_temp, SEEK_SET, off);
1687 
1688  if (bytestream2_get_bytes_left(&gb_temp) < count)
1689  return AVERROR_INVALIDDATA;
1690 
1692  if (!sd)
1693  return AVERROR(ENOMEM);
1694 
1695  bytestream2_get_bufferu(&gb_temp, sd->data, count);
1696  break;
1697  case TIFF_ARTIST:
1698  ADD_METADATA(count, "artist", NULL);
1699  break;
1700  case TIFF_COPYRIGHT:
1701  ADD_METADATA(count, "copyright", NULL);
1702  break;
1703  case TIFF_DATE:
1704  ADD_METADATA(count, "date", NULL);
1705  break;
1706  case TIFF_DOCUMENT_NAME:
1707  ADD_METADATA(count, "document_name", NULL);
1708  break;
1709  case TIFF_HOST_COMPUTER:
1710  ADD_METADATA(count, "computer", NULL);
1711  break;
1713  ADD_METADATA(count, "description", NULL);
1714  break;
1715  case TIFF_MAKE:
1716  ADD_METADATA(count, "make", NULL);
1717  break;
1718  case TIFF_MODEL:
1719  ADD_METADATA(count, "model", NULL);
1720  break;
1721  case TIFF_PAGE_NAME:
1722  ADD_METADATA(count, "page_name", NULL);
1723  break;
1724  case TIFF_PAGE_NUMBER:
1725  ADD_METADATA(count, "page_number", " / ");
1726  // need to seek back to re-read the page number
1727  bytestream2_seek(&s->gb, -count * sizeof(uint16_t), SEEK_CUR);
1728  // read the page number
1729  s->cur_page = ff_tget(&s->gb, TIFF_SHORT, s->le);
1730  // get back to where we were before the previous seek
1731  bytestream2_seek(&s->gb, count * sizeof(uint16_t) - sizeof(uint16_t), SEEK_CUR);
1732  break;
1733  case TIFF_SOFTWARE_NAME:
1734  ADD_METADATA(count, "software", NULL);
1735  break;
1736  case DNG_VERSION:
1737  if (count == 4) {
1738  unsigned int ver[4];
1739  ver[0] = ff_tget(&s->gb, type, s->le);
1740  ver[1] = ff_tget(&s->gb, type, s->le);
1741  ver[2] = ff_tget(&s->gb, type, s->le);
1742  ver[3] = ff_tget(&s->gb, type, s->le);
1743 
1744  av_log(s->avctx, AV_LOG_DEBUG, "DNG file, version %u.%u.%u.%u\n",
1745  ver[0], ver[1], ver[2], ver[3]);
1746 
1748  }
1749  break;
1750  case CINEMADNG_TIME_CODES:
1751  case CINEMADNG_FRAME_RATE:
1752  case CINEMADNG_T_STOP:
1753  case CINEMADNG_REEL_NAME:
1756  break;
1757  default:
1758  if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1759  av_log(s->avctx, AV_LOG_ERROR,
1760  "Unknown or unsupported tag %d/0x%0X\n",
1761  tag, tag);
1762  return AVERROR_INVALIDDATA;
1763  }
1764  }
1765 end:
1766  if (s->bpp > 64U) {
1767  av_log(s->avctx, AV_LOG_ERROR,
1768  "This format is not supported (bpp=%d, %d components)\n",
1769  s->bpp, count);
1770  s->bpp = 0;
1771  return AVERROR_INVALIDDATA;
1772  }
1773  bytestream2_seek(&s->gb, start, SEEK_SET);
1774  return 0;
1775 }
1776 
1777 static int decode_frame(AVCodecContext *avctx,
1778  void *data, int *got_frame, AVPacket *avpkt)
1779 {
1780  TiffContext *const s = avctx->priv_data;
1781  AVFrame *const p = data;
1782  ThreadFrame frame = { .f = data };
1783  unsigned off, last_off = 0;
1784  int le, ret, plane, planes;
1785  int i, j, entries, stride;
1786  unsigned soff, ssize;
1787  uint8_t *dst;
1788  GetByteContext stripsizes;
1789  GetByteContext stripdata;
1790  int retry_for_subifd, retry_for_page;
1791  int is_dng;
1792  int has_tile_bits, has_strip_bits;
1793 
1794  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1795 
1796  // parse image header
1797  if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
1798  av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
1799  return ret;
1800  } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1801  av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1802  return AVERROR_INVALIDDATA;
1803  }
1804  s->le = le;
1805  // TIFF_BPP is not a required tag and defaults to 1
1806 
1807  s->tiff_type = TIFF_TYPE_TIFF;
1808 again:
1809  s->is_thumbnail = 0;
1810  s->bppcount = s->bpp = 1;
1811  s->photometric = TIFF_PHOTOMETRIC_NONE;
1812  s->compr = TIFF_RAW;
1813  s->fill_order = 0;
1814  s->white_level = 0;
1815  s->is_bayer = 0;
1816  s->is_tiled = 0;
1817  s->is_jpeg = 0;
1818  s->cur_page = 0;
1819  s->last_tag = 0;
1820 
1821  for (i = 0; i < 65536; i++)
1822  s->dng_lut[i] = i;
1823 
1824  free_geotags(s);
1825 
1826  // Reset these offsets so we can tell if they were set this frame
1827  s->stripsizesoff = s->strippos = 0;
1828  /* parse image file directory */
1829  bytestream2_seek(&s->gb, off, SEEK_SET);
1830  entries = ff_tget_short(&s->gb, le);
1831  if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
1832  return AVERROR_INVALIDDATA;
1833  for (i = 0; i < entries; i++) {
1834  if ((ret = tiff_decode_tag(s, p)) < 0)
1835  return ret;
1836  }
1837 
1838  if (s->get_thumbnail && !s->is_thumbnail) {
1839  av_log(avctx, AV_LOG_INFO, "No embedded thumbnail present\n");
1840  return AVERROR_EOF;
1841  }
1842 
1843  /** whether we should process this IFD's SubIFD */
1844  retry_for_subifd = s->sub_ifd && (s->get_subimage || (!s->get_thumbnail && s->is_thumbnail));
1845  /** whether we should process this multi-page IFD's next page */
1846  retry_for_page = s->get_page && s->cur_page + 1 < s->get_page; // get_page is 1-indexed
1847 
1848  if (retry_for_page) {
1849  // set offset to the next IFD
1850  off = ff_tget_long(&s->gb, le);
1851  } else if (retry_for_subifd) {
1852  // set offset to the SubIFD
1853  off = s->sub_ifd;
1854  }
1855 
1856  if (retry_for_subifd || retry_for_page) {
1857  if (!off) {
1858  av_log(avctx, AV_LOG_ERROR, "Requested entry not found\n");
1859  return AVERROR_INVALIDDATA;
1860  }
1861  if (off <= last_off) {
1862  avpriv_request_sample(s->avctx, "non increasing IFD offset");
1863  return AVERROR_INVALIDDATA;
1864  }
1865  last_off = off;
1866  if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1867  av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1868  return AVERROR_INVALIDDATA;
1869  }
1870  s->sub_ifd = 0;
1871  goto again;
1872  }
1873 
1874  /* At this point we've decided on which (Sub)IFD to process */
1875 
1876  is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
1877 
1878  for (i = 0; i<s->geotag_count; i++) {
1879  const char *keyname = get_geokey_name(s->geotags[i].key);
1880  if (!keyname) {
1881  av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
1882  continue;
1883  }
1884  if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
1885  av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
1886  continue;
1887  }
1888  ret = av_dict_set(&p->metadata, keyname, s->geotags[i].val, 0);
1889  if (ret<0) {
1890  av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
1891  return ret;
1892  }
1893  }
1894 
1895  if (is_dng) {
1896  int bps;
1897 
1898  if (s->bpp % s->bppcount)
1899  return AVERROR_INVALIDDATA;
1900  bps = s->bpp / s->bppcount;
1901  if (bps < 8 || bps > 32)
1902  return AVERROR_INVALIDDATA;
1903 
1904  if (s->white_level == 0)
1905  s->white_level = (1LL << bps) - 1; /* Default value as per the spec */
1906 
1907  if (s->white_level <= s->black_level) {
1908  av_log(avctx, AV_LOG_ERROR, "BlackLevel (%"PRId32") must be less than WhiteLevel (%"PRId32")\n",
1909  s->black_level, s->white_level);
1910  return AVERROR_INVALIDDATA;
1911  }
1912 
1913  if (s->planar)
1914  return AVERROR_PATCHWELCOME;
1915  }
1916 
1917  if (!s->is_tiled && !s->strippos && !s->stripoff) {
1918  av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
1919  return AVERROR_INVALIDDATA;
1920  }
1921 
1922  has_tile_bits = s->is_tiled || s->tile_byte_counts_offset || s->tile_offsets_offset || s->tile_width || s->tile_length;
1923  has_strip_bits = s->strippos || s->strips || s->stripoff || s->rps || s->sot || s->sstype || s->stripsize || s->stripsizesoff;
1924 
1925  if (has_tile_bits && has_strip_bits) {
1926  int tiled_dng = s->is_tiled && is_dng;
1927  av_log(avctx, tiled_dng ? AV_LOG_WARNING : AV_LOG_ERROR, "Tiled TIFF is not allowed to strip\n");
1928  if (!tiled_dng)
1929  return AVERROR_INVALIDDATA;
1930  }
1931 
1932  /* now we have the data and may start decoding */
1933  if ((ret = init_image(s, &frame)) < 0)
1934  return ret;
1935 
1936  if (!s->is_tiled || has_strip_bits) {
1937  if (s->strips == 1 && !s->stripsize) {
1938  av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
1939  s->stripsize = avpkt->size - s->stripoff;
1940  }
1941 
1942  if (s->stripsizesoff) {
1943  if (s->stripsizesoff >= (unsigned)avpkt->size)
1944  return AVERROR_INVALIDDATA;
1945  bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
1946  avpkt->size - s->stripsizesoff);
1947  }
1948  if (s->strippos) {
1949  if (s->strippos >= (unsigned)avpkt->size)
1950  return AVERROR_INVALIDDATA;
1951  bytestream2_init(&stripdata, avpkt->data + s->strippos,
1952  avpkt->size - s->strippos);
1953  }
1954 
1955  if (s->rps <= 0 || s->rps % s->subsampling[1]) {
1956  av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
1957  return AVERROR_INVALIDDATA;
1958  }
1959  }
1960 
1961  if (s->photometric == TIFF_PHOTOMETRIC_LINEAR_RAW ||
1962  s->photometric == TIFF_PHOTOMETRIC_CFA) {
1964  } else if (s->photometric == TIFF_PHOTOMETRIC_BLACK_IS_ZERO) {
1966  }
1967 
1968  /* Handle DNG images with JPEG-compressed tiles */
1969 
1970  if (is_dng && s->is_tiled) {
1971  if (!s->is_jpeg) {
1972  avpriv_report_missing_feature(avctx, "DNG uncompressed tiled images");
1973  return AVERROR_PATCHWELCOME;
1974  } else if (!s->is_bayer) {
1975  avpriv_report_missing_feature(avctx, "DNG JPG-compressed tiled non-bayer-encoded images");
1976  return AVERROR_PATCHWELCOME;
1977  } else {
1978  if ((ret = dng_decode_tiles(avctx, (AVFrame*)data, avpkt)) > 0)
1979  *got_frame = 1;
1980  return ret;
1981  }
1982  }
1983 
1984  /* Handle TIFF images and DNG images with uncompressed strips (non-tiled) */
1985 
1986  planes = s->planar ? s->bppcount : 1;
1987  for (plane = 0; plane < planes; plane++) {
1988  uint8_t *five_planes = NULL;
1989  int remaining = avpkt->size;
1990  int decoded_height;
1991  stride = p->linesize[plane];
1992  dst = p->data[plane];
1993  if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED &&
1994  s->avctx->pix_fmt == AV_PIX_FMT_RGBA) {
1995  stride = stride * 5 / 4;
1996  five_planes =
1997  dst = av_malloc(stride * s->height);
1998  if (!dst)
1999  return AVERROR(ENOMEM);
2000  }
2001  for (i = 0; i < s->height; i += s->rps) {
2002  if (i)
2003  dst += s->rps * stride;
2004  if (s->stripsizesoff)
2005  ssize = ff_tget(&stripsizes, s->sstype, le);
2006  else
2007  ssize = s->stripsize;
2008 
2009  if (s->strippos)
2010  soff = ff_tget(&stripdata, s->sot, le);
2011  else
2012  soff = s->stripoff;
2013 
2014  if (soff > avpkt->size || ssize > avpkt->size - soff || ssize > remaining) {
2015  av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
2016  av_freep(&five_planes);
2017  return AVERROR_INVALIDDATA;
2018  }
2019  remaining -= ssize;
2020  if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,
2021  FFMIN(s->rps, s->height - i))) < 0) {
2022  if (avctx->err_recognition & AV_EF_EXPLODE) {
2023  av_freep(&five_planes);
2024  return ret;
2025  }
2026  break;
2027  }
2028  }
2029  decoded_height = FFMIN(i, s->height);
2030 
2031  if (s->predictor == 2) {
2032  if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
2033  av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");
2034  return AVERROR_PATCHWELCOME;
2035  }
2036  dst = five_planes ? five_planes : p->data[plane];
2037  soff = s->bpp >> 3;
2038  if (s->planar)
2039  soff = FFMAX(soff / s->bppcount, 1);
2040  ssize = s->width * soff;
2041  if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
2042  s->avctx->pix_fmt == AV_PIX_FMT_RGBA64LE ||
2043  s->avctx->pix_fmt == AV_PIX_FMT_GRAY16LE ||
2044  s->avctx->pix_fmt == AV_PIX_FMT_YA16LE ||
2045  s->avctx->pix_fmt == AV_PIX_FMT_GBRP16LE ||
2046  s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16LE) {
2047  for (i = 0; i < decoded_height; i++) {
2048  for (j = soff; j < ssize; j += 2)
2049  AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
2050  dst += stride;
2051  }
2052  } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
2053  s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE ||
2054  s->avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
2055  s->avctx->pix_fmt == AV_PIX_FMT_YA16BE ||
2056  s->avctx->pix_fmt == AV_PIX_FMT_GBRP16BE ||
2057  s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16BE) {
2058  for (i = 0; i < decoded_height; i++) {
2059  for (j = soff; j < ssize; j += 2)
2060  AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
2061  dst += stride;
2062  }
2063  } else {
2064  for (i = 0; i < decoded_height; i++) {
2065  for (j = soff; j < ssize; j++)
2066  dst[j] += dst[j - soff];
2067  dst += stride;
2068  }
2069  }
2070  }
2071 
2072  if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {
2073  int c = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255);
2074  dst = p->data[plane];
2075  for (i = 0; i < s->height; i++) {
2076  for (j = 0; j < stride; j++)
2077  dst[j] = c - dst[j];
2078  dst += stride;
2079  }
2080  }
2081 
2082  if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED &&
2083  (s->avctx->pix_fmt == AV_PIX_FMT_RGB0 || s->avctx->pix_fmt == AV_PIX_FMT_RGBA)) {
2084  int x = s->avctx->pix_fmt == AV_PIX_FMT_RGB0 ? 4 : 5;
2085  uint8_t *src = five_planes ? five_planes : p->data[plane];
2086  dst = p->data[plane];
2087  for (i = 0; i < s->height; i++) {
2088  for (j = 0; j < s->width; j++) {
2089  int k = 255 - src[x * j + 3];
2090  int r = (255 - src[x * j ]) * k;
2091  int g = (255 - src[x * j + 1]) * k;
2092  int b = (255 - src[x * j + 2]) * k;
2093  dst[4 * j ] = r * 257 >> 16;
2094  dst[4 * j + 1] = g * 257 >> 16;
2095  dst[4 * j + 2] = b * 257 >> 16;
2096  dst[4 * j + 3] = s->avctx->pix_fmt == AV_PIX_FMT_RGBA ? src[x * j + 4] : 255;
2097  }
2098  src += stride;
2099  dst += p->linesize[plane];
2100  }
2101  av_freep(&five_planes);
2102  } else if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED &&
2103  s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE) {
2104  dst = p->data[plane];
2105  for (i = 0; i < s->height; i++) {
2106  for (j = 0; j < s->width; j++) {
2107  uint64_t k = 65535 - AV_RB16(dst + 8 * j + 6);
2108  uint64_t r = (65535 - AV_RB16(dst + 8 * j )) * k;
2109  uint64_t g = (65535 - AV_RB16(dst + 8 * j + 2)) * k;
2110  uint64_t b = (65535 - AV_RB16(dst + 8 * j + 4)) * k;
2111  AV_WB16(dst + 8 * j , r * 65537 >> 32);
2112  AV_WB16(dst + 8 * j + 2, g * 65537 >> 32);
2113  AV_WB16(dst + 8 * j + 4, b * 65537 >> 32);
2114  AV_WB16(dst + 8 * j + 6, 65535);
2115  }
2116  dst += p->linesize[plane];
2117  }
2118  }
2119  }
2120 
2121  if (s->planar && s->bppcount > 2) {
2122  FFSWAP(uint8_t*, p->data[0], p->data[2]);
2123  FFSWAP(int, p->linesize[0], p->linesize[2]);
2124  FFSWAP(uint8_t*, p->data[0], p->data[1]);
2125  FFSWAP(int, p->linesize[0], p->linesize[1]);
2126  }
2127 
2128  if (s->is_bayer && s->white_level && s->bpp == 16 && !is_dng) {
2129  uint16_t *dst = (uint16_t *)p->data[0];
2130  for (i = 0; i < s->height; i++) {
2131  for (j = 0; j < s->width; j++)
2132  dst[j] = FFMIN((dst[j] / (float)s->white_level) * 65535, 65535);
2133  dst += stride / 2;
2134  }
2135  }
2136 
2137  *got_frame = 1;
2138 
2139  return avpkt->size;
2140 }
2141 
2143 {
2144  TiffContext *s = avctx->priv_data;
2145  const AVCodec *codec;
2146  int ret;
2147 
2148  s->width = 0;
2149  s->height = 0;
2150  s->subsampling[0] =
2151  s->subsampling[1] = 1;
2152  s->avctx = avctx;
2153  ff_lzw_decode_open(&s->lzw);
2154  if (!s->lzw)
2155  return AVERROR(ENOMEM);
2157 
2158  /* Allocate JPEG frame */
2159  s->jpgframe = av_frame_alloc();
2160  s->jpkt = av_packet_alloc();
2161  if (!s->jpgframe || !s->jpkt)
2162  return AVERROR(ENOMEM);
2163 
2164  /* Prepare everything needed for JPEG decoding */
2166  if (!codec)
2167  return AVERROR_BUG;
2168  s->avctx_mjpeg = avcodec_alloc_context3(codec);
2169  if (!s->avctx_mjpeg)
2170  return AVERROR(ENOMEM);
2171  s->avctx_mjpeg->flags = avctx->flags;
2172  s->avctx_mjpeg->flags2 = avctx->flags2;
2173  s->avctx_mjpeg->dct_algo = avctx->dct_algo;
2174  s->avctx_mjpeg->idct_algo = avctx->idct_algo;
2175  s->avctx_mjpeg->max_pixels = avctx->max_pixels;
2176  ret = avcodec_open2(s->avctx_mjpeg, codec, NULL);
2177  if (ret < 0) {
2178  return ret;
2179  }
2180 
2181  return 0;
2182 }
2183 
2184 static av_cold int tiff_end(AVCodecContext *avctx)
2185 {
2186  TiffContext *const s = avctx->priv_data;
2187 
2188  free_geotags(s);
2189 
2190  ff_lzw_decode_close(&s->lzw);
2191  av_freep(&s->deinvert_buf);
2192  s->deinvert_buf_size = 0;
2193  av_freep(&s->yuv_line);
2194  s->yuv_line_size = 0;
2195  av_freep(&s->fax_buffer);
2196  s->fax_buffer_size = 0;
2197  av_frame_free(&s->jpgframe);
2198  av_packet_free(&s->jpkt);
2199  avcodec_free_context(&s->avctx_mjpeg);
2200  return 0;
2201 }
2202 
2203 #define OFFSET(x) offsetof(TiffContext, x)
2204 static const AVOption tiff_options[] = {
2205  { "subimage", "decode subimage instead if available", OFFSET(get_subimage), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
2206  { "thumbnail", "decode embedded thumbnail subimage instead if available", OFFSET(get_thumbnail), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
2207  { "page", "page number of multi-page image to decode (starting from 1)", OFFSET(get_page), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
2208  { NULL },
2209 };
2210 
2211 static const AVClass tiff_decoder_class = {
2212  .class_name = "TIFF decoder",
2213  .item_name = av_default_item_name,
2214  .option = tiff_options,
2215  .version = LIBAVUTIL_VERSION_INT,
2216 };
2217 
2219  .name = "tiff",
2220  .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
2221  .type = AVMEDIA_TYPE_VIDEO,
2222  .id = AV_CODEC_ID_TIFF,
2223  .priv_data_size = sizeof(TiffContext),
2224  .init = tiff_init,
2225  .close = tiff_end,
2226  .decode = decode_frame,
2227  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
2229  .priv_class = &tiff_decoder_class,
2230 };
static double val(void *priv, double ch)
Definition: aeval.c:76
Macro definitions for various function/variable attributes.
#define av_always_inline
Definition: attributes.h:45
#define av_cold
Definition: attributes.h:88
uint8_t
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:1660
#define AV_RL16
Definition: intreadwrite.h:42
#define AV_RB16
Definition: intreadwrite.h:53
#define AV_RL32
Definition: intreadwrite.h:146
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:147
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:277
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:212
static av_always_inline int bytestream2_seek_p(PutByteContext *p, int offset, int whence)
Definition: bytestream.h:236
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
static av_always_inline unsigned int bytestream2_get_eof(PutByteContext *p)
Definition: bytestream.h:332
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFSWAP(type, a, b)
Definition: common.h:108
#define FFMIN(a, b)
Definition: common.h:105
static av_always_inline av_const uint16_t av_clip_uint16_c(int a)
Clip a signed integer value into the 0-65535 range.
Definition: common.h:254
#define FFMAX(a, b)
Definition: common.h:103
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static AVFrame * frame
error code definitions
double value
Definition: eval.c:98
av_cold void ff_ccitt_unpack_init(void)
initialize unpacker code
Definition: faxcompr.c:122
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:396
CCITT Fax Group 3 and 4 decompression.
int
bitstream reader API header.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:144
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:946
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:108
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:173
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition: options.c:188
@ AV_CODEC_ID_TIFF
Definition: codec_id.h:145
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:56
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder.
Definition: decode.c:652
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:589
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition: avcodec.h:215
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:50
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
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
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
#define AVERROR(e)
Definition: error.h:43
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, buffer_size_t size)
Add a new side data to a frame.
Definition: frame.c:726
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
@ AV_FRAME_DATA_ICC_PROFILE
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
Definition: frame.h:143
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
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
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:253
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
for(j=16;j >0;--j)
cl_device_type type
const char * key
misc image utilities
int i
Definition: input.c:407
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
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:84
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
const uint8_t ff_reverse[256]
Definition: reverse.c:23
const char * desc
Definition: libsvtav1.c:79
uint8_t w
Definition: llviddspenc.c:39
static const struct @322 planes[]
av_cold void ff_lzw_decode_close(LZWState **p)
Definition: lzw.c:118
int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
Initialize LZW decoder.
Definition: lzw.c:131
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:169
av_cold void ff_lzw_decode_open(LZWState **p)
Definition: lzw.c:113
LZW decoding routines.
@ FF_LZW_TIFF
Definition: lzw.h:39
int stride
Definition: mace.c:144
MJPEG decoder.
uint32_t tag
Definition: movenc.c:1611
unsigned bps
Definition: movenc.c:1612
const char data[16]
Definition: mxf.c:142
AVOptions.
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:279
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:281
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:148
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:144
#define AV_PIX_FMT_BAYER_GRBG16
Definition: pixfmt.h:426
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:381
#define AV_PIX_FMT_BAYER_BGGR16
Definition: pixfmt.h:423
#define AV_PIX_FMT_BAYER_RGGB16
Definition: pixfmt.h:424
@ AV_PIX_FMT_BAYER_GBRG8
bayer, GBGB..(odd line), RGRG..(even line), 8-bit samples
Definition: pixfmt.h:262
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
@ AV_PIX_FMT_YA16BE
16 bits gray, 16 bits alpha (big-endian)
Definition: pixfmt.h:212
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:76
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_YA16LE
16 bits gray, 16 bits alpha (little-endian)
Definition: pixfmt.h:213
@ AV_PIX_FMT_RGB48BE
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:102
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
@ AV_PIX_FMT_RGBA64BE
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:205
@ AV_PIX_FMT_RGBA64LE
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:206
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
@ AV_PIX_FMT_GBRAP16BE
planar GBRA 4:4:4:4 64bpp, big-endian
Definition: pixfmt.h:216
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_BAYER_GRBG8
bayer, GRGR..(odd line), BGBG..(even line), 8-bit samples
Definition: pixfmt.h:263
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
@ AV_PIX_FMT_GBRP16BE
planar GBR 4:4:4 48bpp, big-endian
Definition: pixfmt.h:174
@ AV_PIX_FMT_RGB48LE
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:103
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:238
@ AV_PIX_FMT_GRAY16LE
Y , 16bpp, little-endian.
Definition: pixfmt.h:98
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
@ AV_PIX_FMT_GBRAP16LE
planar GBRA 4:4:4:4 64bpp, little-endian
Definition: pixfmt.h:217
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition: pixfmt.h:143
@ AV_PIX_FMT_BAYER_RGGB8
bayer, RGRG..(odd line), GBGB..(even line), 8-bit samples
Definition: pixfmt.h:261
@ AV_PIX_FMT_GBRP16LE
planar GBR 4:4:4 48bpp, little-endian
Definition: pixfmt.h:175
@ AV_PIX_FMT_BAYER_BGGR8
bayer, BGBG..(odd line), GRGR..(even line), 8-bit samples
Definition: pixfmt.h:260
#define AV_PIX_FMT_BAYER_GBRG16
Definition: pixfmt.h:425
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:383
@ AVCOL_TRC_GAMMA22
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:488
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition: pixfmt.h:492
FF_ENABLE_DEPRECATION_WARNINGS int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
const char * name
Definition: qsvenc.c:46
#define pv
Definition: regdef.h:60
static const ElemCat * elements[ELEMENT_COUNT]
Definition: signature.h:566
#define FF_ARRAY_ELEMS(a)
#define snprintf
Definition: snprintf.h:34
static int shift(int a, int b)
Definition: sonic.c:82
const uint8_t * code
Definition: spdifenc.c:413
unsigned int pos
Definition: spdifenc.c:412
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
main external API structure.
Definition: avcodec.h:536
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:623
int64_t max_pixels
The number of pixels per image to maximally accept.
Definition: avcodec.h:2252
int dct_algo
DCT algorithm, see FF_DCT_* below.
Definition: avcodec.h:1710
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:1723
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:616
void * priv_data
Definition: avcodec.h:563
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1649
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
Structure to hold side data for an AVFrame.
Definition: frame.h:220
uint8_t * data
Definition: frame.h:222
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
AVDictionary * metadata
metadata.
Definition: frame.h:604
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVColorTransferCharacteristic color_trc
Definition: frame.h:566
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:391
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
Rational number (pair of numerator and denominator).
Definition: rational.h:58
Definition: lzw.c:46
GetByteContext gb
Definition: tiff.c:57
int le
Definition: tiff.c:73
int planar
Definition: tiff.c:76
int stripsizesoff
Definition: tiff.c:96
int stripsize
Definition: tiff.c:96
AVCodecContext * avctx_mjpeg
Definition: tiff.c:60
AVCodecContext * avctx
Definition: tiff.c:56
unsigned int yuv_line_size
Definition: tiff.c:109
uint32_t res[4]
Definition: tiff.c:81
int tile_width
Definition: tiff.c:102
int fax_opts
Definition: tiff.c:78
enum TiffType tiff_type
Definition: tiff.c:68
int is_thumbnail
Definition: tiff.c:82
int palette_is_set
Definition: tiff.c:72
unsigned last_tag
Definition: tiff.c:83
int subsampling[2]
Definition: tiff.c:77
unsigned int fax_buffer_size
Definition: tiff.c:111
unsigned white_level
Definition: tiff.c:88
int height
Definition: tiff.c:69
uint16_t dng_lut[65536]
Definition: tiff.c:89
int predictor
Definition: tiff.c:79
int tile_length
Definition: tiff.c:102
unsigned int bppcount
Definition: tiff.c:70
uint32_t palette[256]
Definition: tiff.c:71
int strippos
Definition: tiff.c:96
int tile_offsets_offset
Definition: tiff.c:101
AVPacket * jpkt
Definition: tiff.c:61
TiffGeoTag * geotags
Definition: tiff.c:114
int fill_order
Definition: tiff.c:80
uint8_t * yuv_line
Definition: tiff.c:108
AVFrame * jpgframe
Definition: tiff.c:62
uint16_t cur_page
Definition: tiff.c:92
int is_bayer
Definition: tiff.c:85
unsigned black_level
Definition: tiff.c:87
uint8_t pattern[4]
Definition: tiff.c:86
uint32_t sub_ifd
Definition: tiff.c:91
int width
Definition: tiff.c:69
LZWState * lzw
Definition: tiff.c:97
int sstype
Definition: tiff.c:94
int get_subimage
Definition: tiff.c:64
int deinvert_buf_size
Definition: tiff.c:107
int is_tiled
Definition: tiff.c:100
uint8_t * deinvert_buf
Definition: tiff.c:106
int geotag_count
Definition: tiff.c:113
int strips
Definition: tiff.c:94
int stripoff
Definition: tiff.c:96
enum TiffCompr compr
Definition: tiff.c:74
enum TiffPhotometric photometric
Definition: tiff.c:75
int get_thumbnail
Definition: tiff.c:66
int sot
Definition: tiff.c:95
int is_jpeg
Definition: tiff.c:104
int rps
Definition: tiff.c:94
int tile_byte_counts_offset
Definition: tiff.c:101
unsigned int bpp
Definition: tiff.c:70
uint16_t get_page
Definition: tiff.c:65
uint8_t * fax_buffer
Definition: tiff.c:110
Definition: graph2dot.c:48
#define av_free(p)
#define av_malloc_array(a, b)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
#define height
#define width
#define ADD_METADATA(count, name, sep)
static const AVOption tiff_options[]
Definition: tiff.c:2204
static int get_geokey_type(int key)
Definition: tiff.c:148
static void unpack_yuv(TiffContext *s, AVFrame *p, const uint8_t *src, int lnum)
Definition: tiff.c:456
static uint16_t av_always_inline dng_process_color8(uint16_t value, const uint16_t *lut, uint16_t black_level, float scale_factor)
Definition: tiff.c:302
static uint16_t av_always_inline dng_process_color16(uint16_t value, const uint16_t *lut, uint16_t black_level, float scale_factor)
Map stored raw sensor values into linear reference values (see: DNG Specification - Chapter 5)
Definition: tiff.c:280
static int init_image(TiffContext *s, ThreadFrame *frame)
Definition: tiff.c:1040
static av_cold int tiff_end(AVCodecContext *avctx)
Definition: tiff.c:2184
#define RET_GEOKEY(TYPE, array, element)
Definition: tiff.c:133
static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
Definition: tiff.c:1232
static char * doubles2str(double *dp, int count, const char *sep)
Definition: tiff.c:240
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:739
static const char * search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
Definition: tiff.c:163
static char * get_geokey_val(int key, int val)
Definition: tiff.c:172
static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
Definition: tiff.c:1213
static int cmp_id_key(const void *id, const void *k)
Definition: tiff.c:158
static int add_metadata(int count, int type, const char *name, const char *sep, TiffContext *s, AVFrame *frame)
Definition: tiff.c:266
static av_cold int tiff_init(AVCodecContext *avctx)
Definition: tiff.c:2142
static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int dst_stride, const uint8_t *src, int src_stride, int width, int height, int is_single_comp, int is_u16)
Definition: tiff.c:310
static int dng_decode_tiles(AVCodecContext *avctx, AVFrame *frame, const AVPacket *avpkt)
Definition: tiff.c:967
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:616
static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
Definition: tiff.c:429
static const char * get_geokey_name(int key)
Definition: tiff.c:138
static void unpack_gray(TiffContext *s, AVFrame *p, const uint8_t *src, int lnum, int width, int bpp)
Definition: tiff.c:442
static const AVClass tiff_decoder_class
Definition: tiff.c:2211
static void free_geotags(TiffContext *const s)
Definition: tiff.c:122
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: tiff.c:1777
static int dng_decode_jpeg(AVCodecContext *avctx, AVFrame *frame, int tile_byte_count, int dst_x, int dst_y, int w, int h)
Definition: tiff.c:649
#define RET_GEOKEY_VAL(TYPE, array)
#define OFFSET(x)
Definition: tiff.c:2203
static void av_always_inline horizontal_fill(TiffContext *s, unsigned int bpp, uint8_t *dst, int usePtr, const uint8_t *src, uint8_t c, int width, int offset)
Definition: tiff.c:373
static void tiff_set_type(TiffContext *s, enum TiffType tiff_type)
Definition: tiff.c:117
AVCodec ff_tiff_decoder
Definition: tiff.c:2218
TIFF constants & data structures.
TiffCompr
list of TIFF, TIFF/EP and DNG compression types
Definition: tiff.h:120
@ TIFF_G4
Definition: tiff.h:124
@ TIFF_LZMA
Definition: tiff.h:131
@ TIFF_ADOBE_DEFLATE
Definition: tiff.h:128
@ TIFF_NEWJPEG
Definition: tiff.h:127
@ TIFF_RAW
Definition: tiff.h:121
@ TIFF_LZW
Definition: tiff.h:125
@ TIFF_JPEG
Definition: tiff.h:126
@ TIFF_DEFLATE
Definition: tiff.h:130
@ TIFF_PACKBITS
Definition: tiff.h:129
@ TIFF_CCITT_RLE
Definition: tiff.h:122
@ TIFF_G3
Definition: tiff.h:123
@ TIFF_TILE_LENGTH
Definition: tiff.h:80
@ TIFF_DOCUMENT_NAME
Definition: tiff.h:55
@ TIFF_ICC_PROFILE
Definition: tiff.h:95
@ TIFF_WIDTH
Definition: tiff.h:49
@ TIFF_GEO_DOUBLE_PARAMS
Definition: tiff.h:97
@ TIFF_HOST_COMPUTER
Definition: tiff.h:76
@ TIFF_MODEL_TRANSFORMATION
Definition: tiff.h:94
@ TIFF_ARTIST
Definition: tiff.h:75
@ TIFF_PLANAR
Definition: tiff.h:65
@ TIFF_COMPR
Definition: tiff.h:52
@ TIFF_PAL
Definition: tiff.h:78
@ TIFF_PAGE_NUMBER
Definition: tiff.h:72
@ TIFF_GEO_ASCII_PARAMS
Definition: tiff.h:98
@ TIFF_ROWSPERSTRIP
Definition: tiff.h:61
@ TIFF_TILE_WIDTH
Definition: tiff.h:79
@ TIFF_PHOTOMETRIC
Definition: tiff.h:53
@ TIFF_T6OPTIONS
Definition: tiff.h:70
@ TIFF_YCBCR_SUBSAMPLING
Definition: tiff.h:86
@ TIFF_SAMPLES_PER_PIXEL
Definition: tiff.h:60
@ TIFF_STRIP_SIZE
Definition: tiff.h:62
@ TIFF_MAKE
Definition: tiff.h:57
@ TIFF_CFA_PATTERN
Definition: tiff.h:90
@ TIFF_DATE
Definition: tiff.h:74
@ TIFF_MODEL_TIEPOINT
Definition: tiff.h:92
@ TIFF_COPYRIGHT
Definition: tiff.h:91
@ TIFF_TILE_BYTE_COUNTS
Definition: tiff.h:82
@ TIFF_YRES
Definition: tiff.h:64
@ TIFF_CFA_PATTERN_DIM
Definition: tiff.h:89
@ TIFF_GEO_KEY_DIRECTORY
Definition: tiff.h:96
@ TIFF_SUB_IFDS
Definition: tiff.h:83
@ TIFF_XRES
Definition: tiff.h:63
@ TIFF_HEIGHT
Definition: tiff.h:50
@ TIFF_SOFTWARE_NAME
Definition: tiff.h:73
@ TIFF_SUBFILE
Definition: tiff.h:48
@ TIFF_T4OPTIONS
Definition: tiff.h:69
@ TIFF_TILE_OFFSETS
Definition: tiff.h:81
@ TIFF_PREDICTOR
Definition: tiff.h:77
@ TIFF_IMAGE_DESCRIPTION
Definition: tiff.h:56
@ TIFF_BPP
Definition: tiff.h:51
@ TIFF_MODEL_PIXEL_SCALE
Definition: tiff.h:93
@ TIFF_FILL_ORDER
Definition: tiff.h:54
@ TIFF_MODEL
Definition: tiff.h:58
@ TIFF_PAGE_NAME
Definition: tiff.h:66
@ TIFF_STRIP_OFFS
Definition: tiff.h:59
@ TIFF_VERTICAL_CS_TYPE_GEOKEY
Definition: tiff.h:176
@ TIFF_GEOG_GEODETIC_DATUM_GEOKEY
Definition: tiff.h:140
@ TIFF_GEOG_ELLIPSOID_GEOKEY
Definition: tiff.h:146
@ TIFF_GEOG_ANGULAR_UNITS_GEOKEY
Definition: tiff.h:144
@ TIFF_GT_MODEL_TYPE_GEOKEY
Definition: tiff.h:135
@ TIFF_PROJECTION_GEOKEY
Definition: tiff.h:154
@ TIFF_PROJ_LINEAR_UNITS_GEOKEY
Definition: tiff.h:156
@ TIFF_GT_RASTER_TYPE_GEOKEY
Definition: tiff.h:136
@ TIFF_GEOG_PRIME_MERIDIAN_GEOKEY
Definition: tiff.h:141
@ TIFF_GEOG_AZIMUTH_UNITS_GEOKEY
Definition: tiff.h:150
@ TIFF_GEOG_LINEAR_UNITS_GEOKEY
Definition: tiff.h:142
@ TIFF_GEOGRAPHIC_TYPE_GEOKEY
Definition: tiff.h:138
@ TIFF_PROJECTED_CS_TYPE_GEOKEY
Definition: tiff.h:152
@ TIFF_VERTICAL_UNITS_GEOKEY
Definition: tiff.h:179
@ TIFF_PROJ_COORD_TRANS_GEOKEY
Definition: tiff.h:155
@ CINEMADNG_REEL_NAME
Definition: tiff.h:115
@ CINEMADNG_TIME_CODES
Definition: tiff.h:112
@ CINEMADNG_CAMERA_LABEL
Definition: tiff.h:116
@ CINEMADNG_FRAME_RATE
Definition: tiff.h:113
@ CINEMADNG_T_STOP
Definition: tiff.h:114
TiffPhotometric
list of TIFF, TIFF/AP and DNG PhotometricInterpretation (TIFF_PHOTOMETRIC) values
Definition: tiff.h:183
@ TIFF_PHOTOMETRIC_ICC_LAB
Definition: tiff.h:193
@ TIFF_PHOTOMETRIC_SEPARATED
Definition: tiff.h:190
@ TIFF_PHOTOMETRIC_LINEAR_RAW
Definition: tiff.h:198
@ TIFF_PHOTOMETRIC_RGB
Definition: tiff.h:187
@ TIFF_PHOTOMETRIC_CIE_LAB
Definition: tiff.h:192
@ TIFF_PHOTOMETRIC_YCBCR
Definition: tiff.h:191
@ TIFF_PHOTOMETRIC_LOG_L
Definition: tiff.h:196
@ TIFF_PHOTOMETRIC_PALETTE
Definition: tiff.h:188
@ TIFF_PHOTOMETRIC_WHITE_IS_ZERO
Definition: tiff.h:185
@ TIFF_PHOTOMETRIC_LOG_LUV
Definition: tiff.h:197
@ TIFF_PHOTOMETRIC_ALPHA_MASK
Definition: tiff.h:189
@ TIFF_PHOTOMETRIC_CFA
Definition: tiff.h:195
@ TIFF_PHOTOMETRIC_NONE
Definition: tiff.h:184
@ TIFF_PHOTOMETRIC_BLACK_IS_ZERO
Definition: tiff.h:186
@ TIFF_PHOTOMETRIC_ITU_LAB
Definition: tiff.h:194
@ DNG_VERSION
Definition: tiff.h:103
@ DNG_WHITE_LEVEL
Definition: tiff.h:107
@ DNG_LINEARIZATION_TABLE
Definition: tiff.h:105
@ DNG_BLACK_LEVEL
Definition: tiff.h:106
TiffType
TIFF types in ascenting priority (last in the list is highest)
Definition: tiff.h:37
@ TIFF_TYPE_CINEMADNG
Digital Negative (DNG) image part of an CinemaDNG image sequence.
Definition: tiff.h:43
@ TIFF_TYPE_TIFF
TIFF image based on the TIFF 6.0 or TIFF/EP (ISO 12234-2) specifications.
Definition: tiff.h:39
@ TIFF_TYPE_DNG
Digital Negative (DNG) image.
Definition: tiff.h:41
unsigned ff_tget(GetByteContext *gb, int type, int le)
Reads a byte from the bytestream using given endianness.
Definition: tiff_common.c:62
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 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 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
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
unsigned ff_tget_short(GetByteContext *gb, int le)
Reads a short from the bytestream using given endianness.
Definition: tiff_common.c:43
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
double ff_tget_double(GetByteContext *gb, int le)
Reads a double from the bytestream using given endianness.
Definition: tiff_common.c:55
unsigned ff_tget_long(GetByteContext *gb, int le)
Reads a long from the bytestream using given endianness.
Definition: tiff_common.c:49
@ TIFF_BYTE
Definition: tiff_common.h:38
@ TIFF_SHORT
Definition: tiff_common.h:40
@ TIFF_LONG
Definition: tiff_common.h:41
@ TIFF_STRING
Definition: tiff_common.h:39
@ TIFF_DOUBLE
Definition: tiff_common.h:49
@ TIFF_RATIONAL
Definition: tiff_common.h:42
static const uint8_t type_sizes[14]
sizes of various TIFF field types (string size = 100)
Definition: tiff_common.h:54
TIFF data tables.
static const TiffGeoTagKeyName tiff_projection_codes[]
Definition: tiff_data.h:1517
#define TIFF_GEO_KEY_USER_DEFINED
Definition: tiff_data.h:97
static const TiffGeoTagKeyName tiff_proj_cs_type_codes[]
Definition: tiff_data.h:536
#define TIFF_GEO_KEY_UNDEFINED
Definition: tiff_data.h:96
int size
const char * b
Definition: vf_curves.c:118
const char * g
Definition: vf_curves.c:117
const char * r
Definition: vf_curves.c:116
if(ret< 0)
Definition: vf_mcdeint.c:282
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
Definition: vf_neighbor.c:198
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
int len
static double c[64]