FFmpeg
tiffenc.c
Go to the documentation of this file.
1 /*
2  * TIFF image encoder
3  * Copyright (c) 2007 Bartlomiej Wolowiec
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * TIFF image encoder
25  * @author Bartlomiej Wolowiec
26  */
27 
28 #include "config.h"
29 #if CONFIG_ZLIB
30 #include <zlib.h>
31 #endif
32 
33 #include "libavutil/imgutils.h"
34 #include "libavutil/log.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
37 #include "avcodec.h"
38 #include "bytestream.h"
39 #include "internal.h"
40 #include "lzw.h"
41 #include "put_bits.h"
42 #include "rle.h"
43 #include "tiff.h"
44 
45 #define TIFF_MAX_ENTRY 32
46 
47 /** sizes of various TIFF field types (string size = 1)*/
48 static const uint8_t type_sizes2[14] = {
49  0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4
50 };
51 
52 typedef struct TiffEncoderContext {
53  AVClass *class; ///< for private options
55 
56  int width; ///< picture width
57  int height; ///< picture height
58  unsigned int bpp; ///< bits per pixel
59  int compr; ///< compression level
60  int bpp_tab_size; ///< bpp_tab size
61  enum TiffPhotometric photometric_interpretation; ///< photometric interpretation
62  int strips; ///< number of strips
63  uint32_t *strip_sizes;
64  unsigned int strip_sizes_size;
65  uint32_t *strip_offsets;
66  unsigned int strip_offsets_size;
68  unsigned int yuv_line_size;
69  int rps; ///< row per strip
70  uint8_t entries[TIFF_MAX_ENTRY * 12]; ///< entries in header
71  int num_entries; ///< number of entries
72  uint8_t **buf; ///< actual position in buffer
73  uint8_t *buf_start; ///< pointer to first byte in buffer
74  int buf_size; ///< buffer size
75  uint16_t subsampling[2]; ///< YUV subsampling factors
76  struct LZWEncodeState *lzws; ///< LZW encode state
77  uint32_t dpi; ///< image resolution in DPI
79 
80 /**
81  * Check free space in buffer.
82  *
83  * @param s Tiff context
84  * @param need Needed bytes
85  * @return 0 - ok, 1 - no free space
86  */
87 static inline int check_size(TiffEncoderContext *s, uint64_t need)
88 {
89  if (s->buf_size < *s->buf - s->buf_start + need) {
90  *s->buf = s->buf_start + s->buf_size + 1;
91  av_log(s->avctx, AV_LOG_ERROR, "Buffer is too small\n");
92  return 1;
93  }
94  return 0;
95 }
96 
97 /**
98  * Put n values to buffer.
99  *
100  * @param p pointer to pointer to output buffer
101  * @param n number of values
102  * @param val pointer to values
103  * @param type type of values
104  * @param flip = 0 - normal copy, >0 - flip
105  */
106 static void tnput(uint8_t **p, int n, const uint8_t *val, enum TiffTypes type,
107  int flip)
108 {
109  int i;
110 #if HAVE_BIGENDIAN
111  flip ^= ((int[]) { 0, 0, 0, 1, 3, 3 })[type];
112 #endif
113  for (i = 0; i < n * type_sizes2[type]; i++)
114  *(*p)++ = val[i ^ flip];
115 }
116 
117 /**
118  * Add entry to directory in tiff header.
119  *
120  * @param s Tiff context
121  * @param tag tag that identifies the entry
122  * @param type entry type
123  * @param count the number of values
124  * @param ptr_val pointer to values
125  */
127  enum TiffTypes type, int count, const void *ptr_val)
128 {
129  uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
130 
131  av_assert0(s->num_entries < TIFF_MAX_ENTRY);
132 
133  bytestream_put_le16(&entries_ptr, tag);
134  bytestream_put_le16(&entries_ptr, type);
135  bytestream_put_le32(&entries_ptr, count);
136 
137  if (type_sizes[type] * (int64_t)count <= 4) {
138  tnput(&entries_ptr, count, ptr_val, type, 0);
139  } else {
140  bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start);
141  if (check_size(s, count * (int64_t)type_sizes2[type]))
142  return AVERROR_INVALIDDATA;
143  tnput(s->buf, count, ptr_val, type, 0);
144  }
145 
146  s->num_entries++;
147  return 0;
148 }
149 
151  enum TiffTags tag, enum TiffTypes type, int val)
152 {
153  uint16_t w = val;
154  uint32_t dw = val;
155  return add_entry(s, tag, type, 1,
156  type == TIFF_SHORT ? (void *)&w : (void *)&dw);
157 }
158 
159 /**
160  * Encode one strip in tiff file.
161  *
162  * @param s Tiff context
163  * @param src input buffer
164  * @param dst output buffer
165  * @param n size of input buffer
166  * @param compr compression method
167  * @return number of output bytes. If an output error is encountered, a negative
168  * value corresponding to an AVERROR error code is returned.
169  */
170 static int encode_strip(TiffEncoderContext *s, const int8_t *src,
171  uint8_t *dst, int n, int compr)
172 {
173  switch (compr) {
174 #if CONFIG_ZLIB
175  case TIFF_DEFLATE:
176  case TIFF_ADOBE_DEFLATE:
177  {
178  unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
179  if (compress(dst, &zlen, src, n) != Z_OK) {
180  av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
181  return AVERROR_EXTERNAL;
182  }
183  return zlen;
184  }
185 #endif
186  case TIFF_RAW:
187  if (check_size(s, n))
188  return AVERROR(EINVAL);
189  memcpy(dst, src, n);
190  return n;
191  case TIFF_PACKBITS:
192  return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start),
193  src, 1, n, 2, 0xff, -1, 0);
194  case TIFF_LZW:
195  return ff_lzw_encode(s->lzws, src, n);
196  default:
197  av_log(s->avctx, AV_LOG_ERROR, "Unsupported compression method: %d\n",
198  compr);
199  return AVERROR(EINVAL);
200  }
201 }
202 
203 static void pack_yuv(TiffEncoderContext *s, const AVFrame *p,
204  uint8_t *dst, int lnum)
205 {
206  int i, j, k;
207  int w = (s->width - 1) / s->subsampling[0] + 1;
208  uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
209  uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
210  if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) {
211  for (i = 0; i < w; i++) {
212  for (j = 0; j < s->subsampling[1]; j++)
213  for (k = 0; k < s->subsampling[0]; k++)
214  *dst++ = p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
215  FFMIN(i * s->subsampling[0] + k, s->width-1)];
216  *dst++ = *pu++;
217  *dst++ = *pv++;
218  }
219  }else{
220  for (i = 0; i < w; i++) {
221  for (j = 0; j < s->subsampling[1]; j++)
222  for (k = 0; k < s->subsampling[0]; k++)
223  *dst++ = p->data[0][(lnum + j) * p->linesize[0] +
224  i * s->subsampling[0] + k];
225  *dst++ = *pu++;
226  *dst++ = *pv++;
227  }
228  }
229 }
230 
231 #define ADD_ENTRY(s, tag, type, count, ptr_val) \
232  do { \
233  ret = add_entry(s, tag, type, count, ptr_val); \
234  if (ret < 0) \
235  goto fail; \
236  } while (0)
237 
238 #define ADD_ENTRY1(s, tag, type, val) \
239  do { \
240  ret = add_entry1(s, tag, type, val); \
241  if (ret < 0) \
242  goto fail; \
243  } while (0)
244 
246  const AVFrame *pict, int *got_packet)
247 {
249  TiffEncoderContext *s = avctx->priv_data;
250  const AVFrame *const p = pict;
251  int i;
252  uint8_t *ptr;
253  uint8_t *offset;
254  uint32_t strips;
255  int bytes_per_row;
256  uint32_t res[2] = { s->dpi, 1 }; // image resolution (72/1)
257  uint16_t bpp_tab[4];
258  int ret = 0;
259  int is_yuv = 0, alpha = 0;
260  int shift_h, shift_v;
261  int packet_size;
262 
263  s->width = avctx->width;
264  s->height = avctx->height;
265  s->subsampling[0] = 1;
266  s->subsampling[1] = 1;
267 
268  if (!desc)
269  return AVERROR(EINVAL);
270 
271  avctx->bits_per_coded_sample =
272  s->bpp = av_get_bits_per_pixel(desc);
273  s->bpp_tab_size = desc->nb_components;
274 
275  switch (avctx->pix_fmt) {
276  case AV_PIX_FMT_RGBA64LE:
277  case AV_PIX_FMT_RGBA:
278  alpha = 1;
279  case AV_PIX_FMT_RGB48LE:
280  case AV_PIX_FMT_RGB24:
281  s->photometric_interpretation = TIFF_PHOTOMETRIC_RGB;
282  break;
283  case AV_PIX_FMT_GRAY8:
284  avctx->bits_per_coded_sample = 0x28;
285  case AV_PIX_FMT_GRAY8A:
286  case AV_PIX_FMT_YA16LE:
287  alpha = avctx->pix_fmt == AV_PIX_FMT_GRAY8A || avctx->pix_fmt == AV_PIX_FMT_YA16LE;
288  case AV_PIX_FMT_GRAY16LE:
290  s->photometric_interpretation = TIFF_PHOTOMETRIC_BLACK_IS_ZERO;
291  break;
292  case AV_PIX_FMT_PAL8:
293  s->photometric_interpretation = TIFF_PHOTOMETRIC_PALETTE;
294  break;
296  s->photometric_interpretation = TIFF_PHOTOMETRIC_WHITE_IS_ZERO;
297  break;
298  case AV_PIX_FMT_YUV420P:
299  case AV_PIX_FMT_YUV422P:
300  case AV_PIX_FMT_YUV440P:
301  case AV_PIX_FMT_YUV444P:
302  case AV_PIX_FMT_YUV410P:
303  case AV_PIX_FMT_YUV411P:
304  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &shift_h, &shift_v);
305  s->photometric_interpretation = TIFF_PHOTOMETRIC_YCBCR;
306  s->subsampling[0] = 1 << shift_h;
307  s->subsampling[1] = 1 << shift_v;
308  is_yuv = 1;
309  break;
310  default:
311  av_log(s->avctx, AV_LOG_ERROR,
312  "This colors format is not supported\n");
313  return AVERROR(EINVAL);
314  }
315 
316  for (i = 0; i < s->bpp_tab_size; i++)
317  bpp_tab[i] = desc->comp[i].depth;
318 
319  if (s->compr == TIFF_DEFLATE ||
320  s->compr == TIFF_ADOBE_DEFLATE ||
321  s->compr == TIFF_LZW)
322  // best choice for DEFLATE
323  s->rps = s->height;
324  else
325  // suggest size of strip
326  s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1);
327  // round rps up
328  s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1];
329 
330  strips = (s->height - 1) / s->rps + 1;
331 
332  bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
333  s->subsampling[0] * s->subsampling[1] + 7) >> 3;
334  packet_size = avctx->height * bytes_per_row * 2 +
335  avctx->height * 4 + AV_INPUT_BUFFER_MIN_SIZE;
336 
337  if ((ret = ff_alloc_packet2(avctx, pkt, packet_size, 0)) < 0)
338  return ret;
339  ptr = pkt->data;
340  s->buf_start = pkt->data;
341  s->buf = &ptr;
342  s->buf_size = pkt->size;
343 
344  if (check_size(s, 8)) {
345  ret = AVERROR(EINVAL);
346  goto fail;
347  }
348 
349  // write header
350  bytestream_put_le16(&ptr, 0x4949);
351  bytestream_put_le16(&ptr, 42);
352 
353  offset = ptr;
354  bytestream_put_le32(&ptr, 0);
355 
356  if (strips > INT_MAX / FFMAX(sizeof(s->strip_sizes[0]), sizeof(s->strip_offsets[0]))) {
357  ret = AVERROR(ENOMEM);
358  goto fail;
359  }
360  av_fast_padded_mallocz(&s->strip_sizes , &s->strip_sizes_size , sizeof(s->strip_sizes [0]) * strips);
361  av_fast_padded_mallocz(&s->strip_offsets, &s->strip_offsets_size, sizeof(s->strip_offsets[0]) * strips);
362 
363  if (!s->strip_sizes || !s->strip_offsets) {
364  ret = AVERROR(ENOMEM);
365  goto fail;
366  }
367 
368  if (is_yuv) {
369  av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
370  if (s->yuv_line == NULL) {
371  av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
372  ret = AVERROR(ENOMEM);
373  goto fail;
374  }
375  }
376 
377 #if CONFIG_ZLIB
378  if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
379  uint8_t *zbuf;
380  int zlen, zn;
381  int j;
382 
383  zlen = bytes_per_row * s->rps;
384  zbuf = av_malloc(zlen);
385  if (!zbuf) {
386  ret = AVERROR(ENOMEM);
387  goto fail;
388  }
389  s->strip_offsets[0] = ptr - pkt->data;
390  zn = 0;
391  for (j = 0; j < s->rps; j++) {
392  if (is_yuv) {
393  pack_yuv(s, p, s->yuv_line, j);
394  memcpy(zbuf + zn, s->yuv_line, bytes_per_row);
395  j += s->subsampling[1] - 1;
396  } else
397  memcpy(zbuf + j * bytes_per_row,
398  p->data[0] + j * p->linesize[0], bytes_per_row);
399  zn += bytes_per_row;
400  }
401  ret = encode_strip(s, zbuf, ptr, zn, s->compr);
402  av_free(zbuf);
403  if (ret < 0) {
404  av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
405  goto fail;
406  }
407  ptr += ret;
408  s->strip_sizes[0] = ptr - pkt->data - s->strip_offsets[0];
409  } else
410 #endif
411  {
412  if (s->compr == TIFF_LZW) {
414  if (!s->lzws) {
415  ret = AVERROR(ENOMEM);
416  goto fail;
417  }
418  }
419  for (i = 0; i < s->height; i++) {
420  if (s->strip_sizes[i / s->rps] == 0) {
421  if (s->compr == TIFF_LZW) {
422  ff_lzw_encode_init(s->lzws, ptr,
423  s->buf_size - (*s->buf - s->buf_start),
424  12, FF_LZW_TIFF, put_bits);
425  }
426  s->strip_offsets[i / s->rps] = ptr - pkt->data;
427  }
428  if (is_yuv) {
429  pack_yuv(s, p, s->yuv_line, i);
430  ret = encode_strip(s, s->yuv_line, ptr, bytes_per_row, s->compr);
431  i += s->subsampling[1] - 1;
432  } else
433  ret = encode_strip(s, p->data[0] + i * p->linesize[0],
434  ptr, bytes_per_row, s->compr);
435  if (ret < 0) {
436  av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
437  goto fail;
438  }
439  s->strip_sizes[i / s->rps] += ret;
440  ptr += ret;
441  if (s->compr == TIFF_LZW &&
442  (i == s->height - 1 || i % s->rps == s->rps - 1)) {
444  s->strip_sizes[(i / s->rps)] += ret;
445  ptr += ret;
446  }
447  }
448  if (s->compr == TIFF_LZW)
449  av_freep(&s->lzws);
450  }
451 
452  s->num_entries = 0;
453 
455  ADD_ENTRY1(s, TIFF_WIDTH, TIFF_LONG, s->width);
456  ADD_ENTRY1(s, TIFF_HEIGHT, TIFF_LONG, s->height);
457 
458  if (s->bpp_tab_size)
459  ADD_ENTRY(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
460 
461  ADD_ENTRY1(s, TIFF_COMPR, TIFF_SHORT, s->compr);
462  ADD_ENTRY1(s, TIFF_PHOTOMETRIC, TIFF_SHORT, s->photometric_interpretation);
463  ADD_ENTRY(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, s->strip_offsets);
464 
465  if (s->bpp_tab_size)
466  ADD_ENTRY1(s, TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, s->bpp_tab_size);
467 
469  ADD_ENTRY(s, TIFF_STRIP_SIZE, TIFF_LONG, strips, s->strip_sizes);
470  ADD_ENTRY(s, TIFF_XRES, TIFF_RATIONAL, 1, res);
471  if (avctx->sample_aspect_ratio.num > 0 &&
472  avctx->sample_aspect_ratio.den > 0) {
473  AVRational y = av_mul_q(av_make_q(s->dpi, 1),
474  avctx->sample_aspect_ratio);
475  res[0] = y.num;
476  res[1] = y.den;
477  }
478  ADD_ENTRY(s, TIFF_YRES, TIFF_RATIONAL, 1, res);
480 
481  if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
483  strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
484 
485  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
486  uint16_t pal[256 * 3];
487  for (i = 0; i < 256; i++) {
488  uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
489  pal[i] = ((rgb >> 16) & 0xff) * 257;
490  pal[i + 256] = ((rgb >> 8) & 0xff) * 257;
491  pal[i + 512] = (rgb & 0xff) * 257;
492  }
493  ADD_ENTRY(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
494  }
495  if (alpha)
497  if (is_yuv) {
498  /** according to CCIR Recommendation 601.1 */
499  uint32_t refbw[12] = { 15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1 };
500  ADD_ENTRY(s, TIFF_YCBCR_SUBSAMPLING, TIFF_SHORT, 2, s->subsampling);
504  }
505  // write offset to dir
506  bytestream_put_le32(&offset, ptr - pkt->data);
507 
508  if (check_size(s, 6 + s->num_entries * 12)) {
509  ret = AVERROR(EINVAL);
510  goto fail;
511  }
512  bytestream_put_le16(&ptr, s->num_entries); // write tag count
513  bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
514  bytestream_put_le32(&ptr, 0);
515 
516  pkt->size = ptr - pkt->data;
518  *got_packet = 1;
519 
520 fail:
521  return ret < 0 ? ret : 0;
522 }
523 
525 {
526  TiffEncoderContext *s = avctx->priv_data;
527 
528 #if !CONFIG_ZLIB
529  if (s->compr == TIFF_DEFLATE) {
530  av_log(avctx, AV_LOG_ERROR,
531  "Deflate compression needs zlib compiled in\n");
532  return AVERROR(ENOSYS);
533  }
534 #endif
535 
536 #if FF_API_CODED_FRAME
539  avctx->coded_frame->key_frame = 1;
541 #endif
542  s->avctx = avctx;
543 
544  return 0;
545 }
546 
548 {
549  TiffEncoderContext *s = avctx->priv_data;
550 
551  av_freep(&s->strip_sizes);
552  av_freep(&s->strip_offsets);
553  av_freep(&s->yuv_line);
554 
555  return 0;
556 }
557 
558 #define OFFSET(x) offsetof(TiffEncoderContext, x)
559 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
560 static const AVOption options[] = {
561  {"dpi", "set the image resolution (in dpi)", OFFSET(dpi), AV_OPT_TYPE_INT, {.i64 = 72}, 1, 0x10000, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_ENCODING_PARAM},
562  { "compression_algo", NULL, OFFSET(compr), AV_OPT_TYPE_INT, { .i64 = TIFF_PACKBITS }, TIFF_RAW, TIFF_DEFLATE, VE, "compression_algo" },
563  { "packbits", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TIFF_PACKBITS }, 0, 0, VE, "compression_algo" },
564  { "raw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TIFF_RAW }, 0, 0, VE, "compression_algo" },
565  { "lzw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TIFF_LZW }, 0, 0, VE, "compression_algo" },
566  { "deflate", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TIFF_DEFLATE }, 0, 0, VE, "compression_algo" },
567  { NULL },
568 };
569 
570 static const AVClass tiffenc_class = {
571  .class_name = "TIFF encoder",
572  .item_name = av_default_item_name,
573  .option = options,
574  .version = LIBAVUTIL_VERSION_INT,
575 };
576 
578  .name = "tiff",
579  .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
580  .type = AVMEDIA_TYPE_VIDEO,
581  .id = AV_CODEC_ID_TIFF,
582  .priv_data_size = sizeof(TiffEncoderContext),
583  .init = encode_init,
584  .close = encode_close,
586  .encode2 = encode_frame,
587  .pix_fmts = (const enum AVPixelFormat[]) {
595  },
596  .priv_class = &tiffenc_class,
597 };
AV_CODEC_CAP_INTRA_ONLY
#define AV_CODEC_CAP_INTRA_ONLY
Codec is intra only.
Definition: avcodec.h:1067
encode_close
static av_cold int encode_close(AVCodecContext *avctx)
Definition: tiffenc.c:547
AVCodec
AVCodec.
Definition: avcodec.h:3481
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
TiffTypes
TiffTypes
data type identifiers for TIFF tags
Definition: tiff_common.h:37
check_size
static int check_size(TiffEncoderContext *s, uint64_t need)
Check free space in buffer.
Definition: tiffenc.c:87
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
ff_lzw_encode_init
void ff_lzw_encode_init(struct LZWEncodeState *s, uint8_t *outbuf, int outsize, int maxbits, enum FF_LZW_MODES mode, void(*lzw_put_bits)(struct PutBitContext *, int, unsigned int))
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
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
encode_strip
static int encode_strip(TiffEncoderContext *s, const int8_t *src, uint8_t *dst, int n, int compr)
Encode one strip in tiff file.
Definition: tiffenc.c:170
AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:279
LIBAVCODEC_IDENT
#define LIBAVCODEC_IDENT
Definition: version.h:42
rle.h
TiffEncoderContext::entries
uint8_t entries[TIFF_MAX_ENTRY *12]
entries in header
Definition: tiffenc.c:70
n
int n
Definition: avisynth_c.h:760
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2522
TiffEncoderContext::bpp_tab_size
int bpp_tab_size
bpp_tab size
Definition: tiffenc.c:60
TIFF_YCBCR_POSITIONING
@ TIFF_YCBCR_POSITIONING
Definition: tiff.h:87
TiffEncoderContext::strip_sizes_size
unsigned int strip_sizes_size
Definition: tiffenc.c:64
TiffEncoderContext::width
int width
picture width
Definition: tiffenc.c:56
count
void INT64 INT64 count
Definition: avisynth_c.h:767
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
pixdesc.h
w
uint8_t w
Definition: llviddspenc.c:38
TIFF_ADOBE_DEFLATE
@ TIFF_ADOBE_DEFLATE
Definition: tiff.h:125
LZWEncodeState
LZW encode state.
Definition: lzwenc.c:50
internal.h
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AVOption
AVOption.
Definition: opt.h:246
TIFF_LONG
@ TIFF_LONG
Definition: tiff_common.h:41
ff_tiff_encoder
AVCodec ff_tiff_encoder
Definition: tiffenc.c:577
TiffTags
TiffTags
abridged list of TIFF and TIFF/EP tags
Definition: tiff.h:47
AV_PIX_FMT_MONOWHITE
@ AV_PIX_FMT_MONOWHITE
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:75
av_get_bits_per_pixel
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
Definition: pixdesc.c:2474
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
TIFF_ROWSPERSTRIP
@ TIFF_ROWSPERSTRIP
Definition: tiff.h:61
TiffEncoderContext::avctx
AVCodecContext * avctx
Definition: tiffenc.c:54
TIFF_RAW
@ TIFF_RAW
Definition: tiff.h:118
TiffEncoderContext::subsampling
uint16_t subsampling[2]
YUV subsampling factors.
Definition: tiffenc.c:75
TIFF_PHOTOMETRIC_WHITE_IS_ZERO
@ TIFF_PHOTOMETRIC_WHITE_IS_ZERO
Definition: tiff.h:182
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1509
TIFF_PACKBITS
@ TIFF_PACKBITS
Definition: tiff.h:126
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
TiffEncoderContext::yuv_line_size
unsigned int yuv_line_size
Definition: tiffenc.c:68
add_entry1
static int add_entry1(TiffEncoderContext *s, enum TiffTags tag, enum TiffTypes type, int val)
Definition: tiffenc.c:150
fail
#define fail()
Definition: checkasm.h:120
TIFF_YCBCR_SUBSAMPLING
@ TIFF_YCBCR_SUBSAMPLING
Definition: tiff.h:86
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:373
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1645
av_pix_fmt_get_chroma_sub_sample
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2550
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
OFFSET
#define OFFSET(x)
Definition: tiffenc.c:558
TiffEncoderContext::num_entries
int num_entries
number of entries
Definition: tiffenc.c:71
TIFF_SOFTWARE_NAME
@ TIFF_SOFTWARE_NAME
Definition: tiff.h:73
FF_LZW_TIFF
@ FF_LZW_TIFF
Definition: lzw.h:39
AVRational::num
int num
Numerator.
Definition: rational.h:59
src
#define src
Definition: vp8dsp.c:254
TIFF_SHORT
@ TIFF_SHORT
Definition: tiff_common.h:40
ADD_ENTRY1
#define ADD_ENTRY1(s, tag, type, val)
Definition: tiffenc.c:238
VE
#define VE
Definition: tiffenc.c:559
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:84
TIFF_SUBFILE
@ TIFF_SUBFILE
Definition: tiff.h:48
TIFF_RES_UNIT
@ TIFF_RES_UNIT
Definition: tiff.h:71
type_sizes2
static const uint8_t type_sizes2[14]
sizes of various TIFF field types (string size = 1)
Definition: tiffenc.c:48
s
#define s(width, name)
Definition: cbs_vp9.c:257
TiffEncoderContext::strips
int strips
number of strips
Definition: tiffenc.c:62
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:276
TIFF_STRIP_SIZE
@ TIFF_STRIP_SIZE
Definition: tiff.h:62
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
TIFF_STRING
@ TIFF_STRING
Definition: tiff_common.h:39
AV_INPUT_BUFFER_MIN_SIZE
#define AV_INPUT_BUFFER_MIN_SIZE
Definition: avcodec.h:797
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
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
if
if(ret)
Definition: filter_design.txt:179
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1037
need
must be printed separately If there s no standard function for printing the type you need
Definition: tablegen.txt:45
AV_PIX_FMT_GRAY8A
@ AV_PIX_FMT_GRAY8A
alias for AV_PIX_FMT_YA8
Definition: pixfmt.h:146
ADD_ENTRY
#define ADD_ENTRY(s, tag, type, count, ptr_val)
Definition: tiffenc.c:231
TIFF_LZW
@ TIFF_LZW
Definition: tiff.h:122
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
TIFF_PHOTOMETRIC_YCBCR
@ TIFF_PHOTOMETRIC_YCBCR
Definition: tiff.h:188
TiffEncoderContext::buf
uint8_t ** buf
actual position in buffer
Definition: tiffenc.c:72
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVCHROMA_LOC_TOPLEFT
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:545
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:213
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:185
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
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:206
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
TiffEncoderContext
Definition: tiffenc.c:52
TiffEncoderContext::dpi
uint32_t dpi
image resolution in DPI
Definition: tiffenc.c:77
TIFF_PAL
@ TIFF_PAL
Definition: tiff.h:78
TiffEncoderContext::strip_offsets
uint32_t * strip_offsets
Definition: tiffenc.c:65
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
TIFF_SAMPLES_PER_PIXEL
@ TIFF_SAMPLES_PER_PIXEL
Definition: tiff.h:60
TiffEncoderContext::lzws
struct LZWEncodeState * lzws
LZW encode state.
Definition: tiffenc.c:76
TIFF_WIDTH
@ TIFF_WIDTH
Definition: tiff.h:49
desc
const char * desc
Definition: nvenc.c:68
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:378
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
flip
static void flip(AVCodecContext *avctx, AVFrame *frame)
Definition: rawdec.c:136
AVPacket::size
int size
Definition: avcodec.h:1478
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:188
TIFF_EXTRASAMPLES
@ TIFF_EXTRASAMPLES
Definition: tiff.h:84
add_entry
static int add_entry(TiffEncoderContext *s, enum TiffTags tag, enum TiffTypes type, int count, const void *ptr_val)
Add entry to directory in tiff header.
Definition: tiffenc.c:126
lzw.h
LZW decoding routines.
FFMAX
#define FFMAX(a, b)
Definition: common.h:94
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: tiffenc.c:245
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
TiffEncoderContext::strip_sizes
uint32_t * strip_sizes
Definition: tiffenc.c:63
TiffEncoderContext::buf_size
int buf_size
buffer size
Definition: tiffenc.c:74
val
const char const char void * val
Definition: avisynth_c.h:863
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
TiffEncoderContext::compr
int compr
compression level
Definition: tiffenc.c:59
tiffenc_class
static const AVClass tiffenc_class
Definition: tiffenc.c:570
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
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
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1483
TIFF_COMPR
@ TIFF_COMPR
Definition: tiff.h:52
TIFF_HEIGHT
@ TIFF_HEIGHT
Definition: tiff.h:50
tnput
static void tnput(uint8_t **p, int n, const uint8_t *val, enum TiffTypes type, int flip)
Put n values to buffer.
Definition: tiffenc.c:106
TiffEncoderContext::photometric_interpretation
enum TiffPhotometric photometric_interpretation
photometric interpretation
Definition: tiffenc.c:61
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2789
bytestream_put_buffer
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:368
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
TIFF_PHOTOMETRIC_BLACK_IS_ZERO
@ TIFF_PHOTOMETRIC_BLACK_IS_ZERO
Definition: tiff.h:183
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:70
uint8_t
uint8_t
Definition: audio_convert.c:194
AVCodec::name
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2207
AVCodecContext::height
int height
Definition: avcodec.h:1738
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1775
TIFF_STRIP_OFFS
@ TIFF_STRIP_OFFS
Definition: tiff.h:59
TIFF_MAX_ENTRY
#define TIFF_MAX_ENTRY
Definition: tiffenc.c:45
avcodec.h
pv
#define pv
Definition: regdef.h:60
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:1496
ret
ret
Definition: filter_design.txt:187
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:72
TIFF_BPP
@ TIFF_BPP
Definition: tiff.h:51
ff_rle_encode
int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr, int bpp, int w, int add_rep, int xor_rep, int add_raw, int xor_raw)
RLE compress the row, with maximum size of out_size.
Definition: rle.c:52
TIFF_PHOTOMETRIC
@ TIFF_PHOTOMETRIC
Definition: tiff.h:53
TiffPhotometric
TiffPhotometric
list of TIFF, TIFF/AP and DNG PhotometricInterpretation (TIFF_PHOTOMETRIC) values
Definition: tiff.h:180
AVCodecContext::coded_frame
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2815
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
TiffEncoderContext::bpp
unsigned int bpp
bits per pixel
Definition: tiffenc.c:58
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
options
static const AVOption options[]
Definition: tiffenc.c:560
TIFF_RATIONAL
@ TIFF_RATIONAL
Definition: tiff_common.h:42
TiffEncoderContext::yuv_line
uint8_t * yuv_line
Definition: tiffenc.c:67
AVRational::den
int den
Denominator.
Definition: rational.h:60
TiffEncoderContext::strip_offsets_size
unsigned int strip_offsets_size
Definition: tiffenc.c:66
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
config.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
av_fast_padded_mallocz
void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_padded_malloc except that buffer will always be 0-initialized after call.
Definition: utils.c:82
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
TIFF_YRES
@ TIFF_YRES
Definition: tiff.h:64
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
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
ff_lzw_encode_flush
int ff_lzw_encode_flush(struct LZWEncodeState *s, void(*lzw_flush_put_bits)(struct PutBitContext *))
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
TiffEncoderContext::height
int height
picture height
Definition: tiffenc.c:57
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
TiffEncoderContext::rps
int rps
row per strip
Definition: tiffenc.c:69
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:908
pack_yuv
static void pack_yuv(TiffEncoderContext *s, const AVFrame *p, uint8_t *dst, int lnum)
Definition: tiffenc.c:203
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
TIFF_DEFLATE
@ TIFF_DEFLATE
Definition: tiff.h:127
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
TIFF_PHOTOMETRIC_RGB
@ TIFF_PHOTOMETRIC_RGB
Definition: tiff.h:184
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1592
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
TiffEncoderContext::buf_start
uint8_t * buf_start
pointer to first byte in buffer
Definition: tiffenc.c:73
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
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
ff_lzw_encode_state_size
const int ff_lzw_encode_state_size
Definition: lzwenc.c:67
TIFF_XRES
@ TIFF_XRES
Definition: tiff.h:63
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:1738
bytestream.h
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
TIFF_REFERENCE_BW
@ TIFF_REFERENCE_BW
Definition: tiff.h:88
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
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:59
encode_init
static av_cold int encode_init(AVCodecContext *avctx)
Definition: tiffenc.c:524
AV_CODEC_ID_TIFF
@ AV_CODEC_ID_TIFF
Definition: avcodec.h:314
type_sizes
static const uint8_t type_sizes[14]
sizes of various TIFF field types (string size = 100)
Definition: tiff_common.h:54
ff_lzw_encode
int ff_lzw_encode(struct LZWEncodeState *s, const uint8_t *inbuf, int insize)
LZW main compress function.
Definition: lzwenc.c:227
ff_alloc_packet2
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
put_bits.h
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:232
AVCodecContext::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:1944