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