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