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