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 "codec_internal.h"
40 #include "encode.h"
41 #include "lzw.h"
42 #include "put_bits.h"
43 #include "rle.h"
44 #include "tiff.h"
45 #include "version.h"
46 
47 #define TIFF_MAX_ENTRY 32
48 
49 /** sizes of various TIFF field types (string size = 1)*/
50 static const uint8_t type_sizes2[14] = {
51  0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4
52 };
53 
54 typedef struct TiffEncoderContext {
55  AVClass *class; ///< for private options
57 
58  int width; ///< picture width
59  int height; ///< picture height
60  unsigned int bpp; ///< bits per pixel
61  int compr; ///< compression level
62  int bpp_tab_size; ///< bpp_tab size
63  enum TiffPhotometric photometric_interpretation; ///< photometric interpretation
64  int strips; ///< number of strips
65  uint32_t *strip_sizes;
66  unsigned int strip_sizes_size;
67  uint32_t *strip_offsets;
68  unsigned int strip_offsets_size;
69  uint8_t *yuv_line;
70  unsigned int yuv_line_size;
71  int rps; ///< row per strip
72  uint8_t entries[TIFF_MAX_ENTRY * 12]; ///< entries in header
73  int num_entries; ///< number of entries
74  uint8_t **buf; ///< actual position in buffer
75  uint8_t *buf_start; ///< pointer to first byte in buffer
76  int buf_size; ///< buffer size
77  uint16_t subsampling[2]; ///< YUV subsampling factors
78  struct LZWEncodeState *lzws; ///< LZW encode state
79  uint32_t dpi; ///< image resolution in DPI
81 
82 /**
83  * Check free space in buffer.
84  *
85  * @param s Tiff context
86  * @param need Needed bytes
87  * @return 0 - ok, 1 - no free space
88  */
89 static inline int check_size(TiffEncoderContext *s, uint64_t need)
90 {
91  if (s->buf_size < *s->buf - s->buf_start + need) {
92  *s->buf = s->buf_start + s->buf_size + 1;
93  av_log(s->avctx, AV_LOG_ERROR, "Buffer is too small\n");
94  return 1;
95  }
96  return 0;
97 }
98 
99 /**
100  * Put n values to buffer.
101  *
102  * @param p pointer to pointer to output buffer
103  * @param n number of values
104  * @param val pointer to values
105  * @param type type of values
106  * @param flip = 0 - normal copy, >0 - flip
107  */
108 static void tnput(uint8_t **p, int n, const uint8_t *val, enum TiffTypes type,
109  int flip)
110 {
111  int i;
112 #if HAVE_BIGENDIAN
113  flip ^= ((int[]) { 0, 0, 0, 1, 3, 3 })[type];
114 #endif
115  for (i = 0; i < n * type_sizes2[type]; i++)
116  *(*p)++ = val[i ^ flip];
117 }
118 
119 /**
120  * Add entry to directory in tiff header.
121  *
122  * @param s Tiff context
123  * @param tag tag that identifies the entry
124  * @param type entry type
125  * @param count the number of values
126  * @param ptr_val pointer to values
127  */
129  enum TiffTypes type, int count, const void *ptr_val)
130 {
131  uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
132 
133  av_assert0(s->num_entries < TIFF_MAX_ENTRY);
134 
135  bytestream_put_le16(&entries_ptr, tag);
136  bytestream_put_le16(&entries_ptr, type);
137  bytestream_put_le32(&entries_ptr, count);
138 
139  if (type_sizes[type] * (int64_t)count <= 4) {
140  tnput(&entries_ptr, count, ptr_val, type, 0);
141  } else {
142  bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start);
143  if (check_size(s, count * (int64_t)type_sizes2[type]))
144  return AVERROR_INVALIDDATA;
145  tnput(s->buf, count, ptr_val, type, 0);
146  }
147 
148  s->num_entries++;
149  return 0;
150 }
151 
153  enum TiffTags tag, enum TiffTypes type, int val)
154 {
155  uint16_t w = val;
156  uint32_t dw = val;
157  return add_entry(s, tag, type, 1,
158  type == TIFF_SHORT ? (void *)&w : (void *)&dw);
159 }
160 
161 /**
162  * Encode one strip in tiff file.
163  *
164  * @param s Tiff context
165  * @param src input buffer
166  * @param dst output buffer
167  * @param n size of input buffer
168  * @param compr compression method
169  * @return number of output bytes. If an output error is encountered, a negative
170  * value corresponding to an AVERROR error code is returned.
171  */
172 static int encode_strip(TiffEncoderContext *s, const int8_t *src,
173  uint8_t *dst, int n, int compr)
174 {
175  switch (compr) {
176 #if CONFIG_ZLIB
177  case TIFF_DEFLATE:
178  case TIFF_ADOBE_DEFLATE:
179  {
180  unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
181  if (compress(dst, &zlen, src, n) != Z_OK) {
182  av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
183  return AVERROR_EXTERNAL;
184  }
185  return zlen;
186  }
187 #endif
188  case TIFF_RAW:
189  if (check_size(s, n))
190  return AVERROR(EINVAL);
191  memcpy(dst, src, n);
192  return n;
193  case TIFF_PACKBITS:
194  return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start),
195  src, 1, n, 2, 0xff, -1, 0);
196  case TIFF_LZW:
197  return ff_lzw_encode(s->lzws, src, n);
198  default:
199  av_log(s->avctx, AV_LOG_ERROR, "Unsupported compression method: %d\n",
200  compr);
201  return AVERROR(EINVAL);
202  }
203 }
204 
205 static void pack_yuv(TiffEncoderContext *s, const AVFrame *p,
206  uint8_t *dst, int lnum)
207 {
208  int i, j, k;
209  int w = (s->width - 1) / s->subsampling[0] + 1;
210  uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
211  uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
212  if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) {
213  for (i = 0; i < w; i++) {
214  for (j = 0; j < s->subsampling[1]; j++)
215  for (k = 0; k < s->subsampling[0]; k++)
216  *dst++ = p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
217  FFMIN(i * s->subsampling[0] + k, s->width-1)];
218  *dst++ = *pu++;
219  *dst++ = *pv++;
220  }
221  }else{
222  for (i = 0; i < w; i++) {
223  for (j = 0; j < s->subsampling[1]; j++)
224  for (k = 0; k < s->subsampling[0]; k++)
225  *dst++ = p->data[0][(lnum + j) * p->linesize[0] +
226  i * s->subsampling[0] + k];
227  *dst++ = *pu++;
228  *dst++ = *pv++;
229  }
230  }
231 }
232 
233 #define ADD_ENTRY(s, tag, type, count, ptr_val) \
234  do { \
235  ret = add_entry(s, tag, type, count, ptr_val); \
236  if (ret < 0) \
237  goto fail; \
238  } while (0)
239 
240 #define ADD_ENTRY1(s, tag, type, val) \
241  do { \
242  ret = add_entry1(s, tag, type, val); \
243  if (ret < 0) \
244  goto fail; \
245  } while (0)
246 
248  const AVFrame *pict, int *got_packet)
249 {
251  TiffEncoderContext *s = avctx->priv_data;
252  const AVFrame *const p = pict;
253  int i;
254  uint8_t *ptr;
255  uint8_t *offset;
256  uint32_t strips;
257  int bytes_per_row;
258  uint32_t res[2] = { s->dpi, 1 }; // image resolution (72/1)
259  uint16_t bpp_tab[4];
260  int ret = 0;
261  int is_yuv = 0, alpha = 0;
262  int shift_h, shift_v;
263  int packet_size;
264 
265  s->width = avctx->width;
266  s->height = avctx->height;
267  s->subsampling[0] = 1;
268  s->subsampling[1] = 1;
269 
270  if (!desc)
271  return AVERROR(EINVAL);
272 
273  avctx->bits_per_coded_sample =
274  s->bpp = av_get_bits_per_pixel(desc);
275  s->bpp_tab_size = desc->nb_components;
276 
277  switch (avctx->pix_fmt) {
278  case AV_PIX_FMT_RGBA64LE:
279  case AV_PIX_FMT_RGBA:
280  alpha = 1;
281  case AV_PIX_FMT_RGB48LE:
282  case AV_PIX_FMT_RGB24:
283  s->photometric_interpretation = TIFF_PHOTOMETRIC_RGB;
284  break;
285  case AV_PIX_FMT_GRAY8:
286  avctx->bits_per_coded_sample = 0x28;
287  case AV_PIX_FMT_GRAY8A:
288  case AV_PIX_FMT_YA16LE:
289  alpha = avctx->pix_fmt == AV_PIX_FMT_GRAY8A || avctx->pix_fmt == AV_PIX_FMT_YA16LE;
290  case AV_PIX_FMT_GRAY16LE:
292  s->photometric_interpretation = TIFF_PHOTOMETRIC_BLACK_IS_ZERO;
293  break;
294  case AV_PIX_FMT_PAL8:
295  s->photometric_interpretation = TIFF_PHOTOMETRIC_PALETTE;
296  break;
298  s->photometric_interpretation = TIFF_PHOTOMETRIC_WHITE_IS_ZERO;
299  break;
300  case AV_PIX_FMT_YUV420P:
301  case AV_PIX_FMT_YUV422P:
302  case AV_PIX_FMT_YUV440P:
303  case AV_PIX_FMT_YUV444P:
304  case AV_PIX_FMT_YUV410P:
305  case AV_PIX_FMT_YUV411P:
306  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &shift_h, &shift_v);
307  s->photometric_interpretation = TIFF_PHOTOMETRIC_YCBCR;
308  s->subsampling[0] = 1 << shift_h;
309  s->subsampling[1] = 1 << shift_v;
310  is_yuv = 1;
311  break;
312  default:
313  av_log(s->avctx, AV_LOG_ERROR,
314  "This colors format is not supported\n");
315  return AVERROR(EINVAL);
316  }
317 
318  for (i = 0; i < s->bpp_tab_size; i++)
319  bpp_tab[i] = desc->comp[i].depth;
320 
321  if (s->compr == TIFF_DEFLATE ||
322  s->compr == TIFF_ADOBE_DEFLATE ||
323  s->compr == TIFF_LZW)
324  // best choice for DEFLATE
325  s->rps = s->height;
326  else
327  // suggest size of strip
328  s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1);
329  // round rps up
330  s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1];
331 
332  strips = (s->height - 1) / s->rps + 1;
333 
334  bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
335  s->subsampling[0] * s->subsampling[1] + 7) >> 3;
336  packet_size = avctx->height * bytes_per_row * 2 +
337  avctx->height * 4 + AV_INPUT_BUFFER_MIN_SIZE;
338 
339  if ((ret = ff_alloc_packet(avctx, pkt, packet_size)) < 0)
340  return ret;
341  ptr = pkt->data;
342  s->buf_start = pkt->data;
343  s->buf = &ptr;
344  s->buf_size = pkt->size;
345 
346  if (check_size(s, 8)) {
347  ret = AVERROR(EINVAL);
348  goto fail;
349  }
350 
351  // write header
352  bytestream_put_le16(&ptr, 0x4949);
353  bytestream_put_le16(&ptr, 42);
354 
355  offset = ptr;
356  bytestream_put_le32(&ptr, 0);
357 
358  if (strips > INT_MAX / FFMAX(sizeof(s->strip_sizes[0]), sizeof(s->strip_offsets[0]))) {
359  ret = AVERROR(ENOMEM);
360  goto fail;
361  }
362  av_fast_padded_mallocz(&s->strip_sizes , &s->strip_sizes_size , sizeof(s->strip_sizes [0]) * strips);
363  av_fast_padded_mallocz(&s->strip_offsets, &s->strip_offsets_size, sizeof(s->strip_offsets[0]) * strips);
364 
365  if (!s->strip_sizes || !s->strip_offsets) {
366  ret = AVERROR(ENOMEM);
367  goto fail;
368  }
369 
370  if (is_yuv) {
371  av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
372  if (s->yuv_line == NULL) {
373  av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
374  ret = AVERROR(ENOMEM);
375  goto fail;
376  }
377  }
378 
379 #if CONFIG_ZLIB
380  if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
381  uint8_t *zbuf;
382  int zlen, zn;
383  int j;
384 
385  zlen = bytes_per_row * s->rps;
386  zbuf = av_malloc(zlen);
387  if (!zbuf) {
388  ret = AVERROR(ENOMEM);
389  goto fail;
390  }
391  s->strip_offsets[0] = ptr - pkt->data;
392  zn = 0;
393  for (j = 0; j < s->rps; j++) {
394  if (is_yuv) {
395  pack_yuv(s, p, s->yuv_line, j);
396  memcpy(zbuf + zn, s->yuv_line, bytes_per_row);
397  j += s->subsampling[1] - 1;
398  } else
399  memcpy(zbuf + j * bytes_per_row,
400  p->data[0] + j * p->linesize[0], bytes_per_row);
401  zn += bytes_per_row;
402  }
403  ret = encode_strip(s, zbuf, ptr, zn, s->compr);
404  av_free(zbuf);
405  if (ret < 0) {
406  av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
407  goto fail;
408  }
409  ptr += ret;
410  s->strip_sizes[0] = ptr - pkt->data - s->strip_offsets[0];
411  } else
412 #endif
413  {
414  if (s->compr == TIFF_LZW) {
416  if (!s->lzws) {
417  ret = AVERROR(ENOMEM);
418  goto fail;
419  }
420  }
421  for (i = 0; i < s->height; i++) {
422  if (s->strip_sizes[i / s->rps] == 0) {
423  if (s->compr == TIFF_LZW) {
424  ff_lzw_encode_init(s->lzws, ptr,
425  s->buf_size - (*s->buf - s->buf_start),
426  12, FF_LZW_TIFF, 0);
427  }
428  s->strip_offsets[i / s->rps] = ptr - pkt->data;
429  }
430  if (is_yuv) {
431  pack_yuv(s, p, s->yuv_line, i);
432  ret = encode_strip(s, s->yuv_line, ptr, bytes_per_row, s->compr);
433  i += s->subsampling[1] - 1;
434  } else
435  ret = encode_strip(s, p->data[0] + i * p->linesize[0],
436  ptr, bytes_per_row, s->compr);
437  if (ret < 0) {
438  av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
439  goto fail;
440  }
441  s->strip_sizes[i / s->rps] += ret;
442  ptr += ret;
443  if (s->compr == TIFF_LZW &&
444  (i == s->height - 1 || i % s->rps == s->rps - 1)) {
445  ret = ff_lzw_encode_flush(s->lzws);
446  s->strip_sizes[(i / s->rps)] += ret;
447  ptr += ret;
448  }
449  }
450  if (s->compr == TIFF_LZW)
451  av_freep(&s->lzws);
452  }
453 
454  s->num_entries = 0;
455 
457  ADD_ENTRY1(s, TIFF_WIDTH, TIFF_LONG, s->width);
458  ADD_ENTRY1(s, TIFF_HEIGHT, TIFF_LONG, s->height);
459 
460  if (s->bpp_tab_size)
461  ADD_ENTRY(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
462 
463  ADD_ENTRY1(s, TIFF_COMPR, TIFF_SHORT, s->compr);
464  ADD_ENTRY1(s, TIFF_PHOTOMETRIC, TIFF_SHORT, s->photometric_interpretation);
465  ADD_ENTRY(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, s->strip_offsets);
466 
467  if (s->bpp_tab_size)
468  ADD_ENTRY1(s, TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, s->bpp_tab_size);
469 
471  ADD_ENTRY(s, TIFF_STRIP_SIZE, TIFF_LONG, strips, s->strip_sizes);
472  ADD_ENTRY(s, TIFF_XRES, TIFF_RATIONAL, 1, res);
473  if (avctx->sample_aspect_ratio.num > 0 &&
474  avctx->sample_aspect_ratio.den > 0) {
475  AVRational y = av_mul_q(av_make_q(s->dpi, 1),
476  avctx->sample_aspect_ratio);
477  res[0] = y.num;
478  res[1] = y.den;
479  }
480  ADD_ENTRY(s, TIFF_YRES, TIFF_RATIONAL, 1, res);
482 
483  if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
485  strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
486 
487  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
488  uint16_t pal[256 * 3];
489  for (i = 0; i < 256; i++) {
490  uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
491  pal[i] = ((rgb >> 16) & 0xff) * 257;
492  pal[i + 256] = ((rgb >> 8) & 0xff) * 257;
493  pal[i + 512] = (rgb & 0xff) * 257;
494  }
495  ADD_ENTRY(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
496  }
497  if (alpha)
499  if (is_yuv) {
500  /** according to CCIR Recommendation 601.1 */
501  uint32_t refbw[12] = { 15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1 };
502  ADD_ENTRY(s, TIFF_YCBCR_SUBSAMPLING, TIFF_SHORT, 2, s->subsampling);
506  }
507  // write offset to dir
508  bytestream_put_le32(&offset, ptr - pkt->data);
509 
510  if (check_size(s, 6 + s->num_entries * 12)) {
511  ret = AVERROR(EINVAL);
512  goto fail;
513  }
514  bytestream_put_le16(&ptr, s->num_entries); // write tag count
515  bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
516  bytestream_put_le32(&ptr, 0);
517 
518  pkt->size = ptr - pkt->data;
519  *got_packet = 1;
520 
521 fail:
522  return ret < 0 ? ret : 0;
523 }
524 
526 {
527  TiffEncoderContext *s = avctx->priv_data;
528 
529 #if !CONFIG_ZLIB
530  if (s->compr == TIFF_DEFLATE) {
531  av_log(avctx, AV_LOG_ERROR,
532  "Deflate compression needs zlib compiled in\n");
533  return AVERROR(ENOSYS);
534  }
535 #endif
536 
537  s->avctx = avctx;
538 
539  return 0;
540 }
541 
543 {
544  TiffEncoderContext *s = avctx->priv_data;
545 
546  av_freep(&s->strip_sizes);
547  av_freep(&s->strip_offsets);
548  av_freep(&s->yuv_line);
549 
550  return 0;
551 }
552 
553 #define OFFSET(x) offsetof(TiffEncoderContext, x)
554 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
555 static const AVOption options[] = {
556  {"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},
557  { "compression_algo", NULL, OFFSET(compr), AV_OPT_TYPE_INT, { .i64 = TIFF_PACKBITS }, TIFF_RAW, TIFF_DEFLATE, VE, "compression_algo" },
558  { "packbits", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TIFF_PACKBITS }, 0, 0, VE, "compression_algo" },
559  { "raw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TIFF_RAW }, 0, 0, VE, "compression_algo" },
560  { "lzw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TIFF_LZW }, 0, 0, VE, "compression_algo" },
561  { "deflate", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TIFF_DEFLATE }, 0, 0, VE, "compression_algo" },
562  { NULL },
563 };
564 
565 static const AVClass tiffenc_class = {
566  .class_name = "TIFF encoder",
567  .item_name = av_default_item_name,
568  .option = options,
569  .version = LIBAVUTIL_VERSION_INT,
570 };
571 
573  .p.name = "tiff",
574  .p.long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
575  .p.type = AVMEDIA_TYPE_VIDEO,
576  .p.id = AV_CODEC_ID_TIFF,
577  .priv_data_size = sizeof(TiffEncoderContext),
578  .init = encode_init,
579  .close = encode_close,
580  .p.capabilities = AV_CODEC_CAP_FRAME_THREADS,
582  .p.pix_fmts = (const enum AVPixelFormat[]) {
590  },
591  .p.priv_class = &tiffenc_class,
592  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
593 };
encode_close
static av_cold int encode_close(AVCodecContext *avctx)
Definition: tiffenc.c:542
TiffTypes
TiffTypes
data type identifiers for TIFF tags
Definition: tiff_common.h:36
check_size
static int check_size(TiffEncoderContext *s, uint64_t need)
Check free space in buffer.
Definition: tiffenc.c:89
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
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:172
AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:284
LIBAVCODEC_IDENT
#define LIBAVCODEC_IDENT
Definition: version.h:43
rle.h
TiffEncoderContext::entries
uint8_t entries[TIFF_MAX_ENTRY *12]
entries in header
Definition: tiffenc.c:72
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2662
TiffEncoderContext::bpp_tab_size
int bpp_tab_size
bpp_tab size
Definition: tiffenc.c:62
TIFF_YCBCR_POSITIONING
@ TIFF_YCBCR_POSITIONING
Definition: tiff.h:87
TiffEncoderContext::strip_sizes_size
unsigned int strip_sizes_size
Definition: tiffenc.c:66
TiffEncoderContext::width
int width
picture width
Definition: tiffenc.c:58
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
pixdesc.h
w
uint8_t w
Definition: llviddspenc.c:38
TIFF_ADOBE_DEFLATE
@ TIFF_ADOBE_DEFLATE
Definition: tiff.h:128
LZWEncodeState
LZW encode state.
Definition: lzwenc.c:51
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVOption
AVOption.
Definition: opt.h:251
TIFF_LONG
@ TIFF_LONG
Definition: tiff_common.h:40
encode.h
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
ff_lzw_encode_init
void ff_lzw_encode_init(struct LZWEncodeState *s, uint8_t *outbuf, int outsize, int maxbits, enum FF_LZW_MODES mode, int little_endian)
Initialize LZW encoder.
Definition: lzwenc.c:206
FFCodec
Definition: codec_internal.h:112
version.h
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:2614
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
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
TiffEncoderContext::avctx
AVCodecContext * avctx
Definition: tiffenc.c:56
TIFF_RAW
@ TIFF_RAW
Definition: tiff.h:121
TiffEncoderContext::subsampling
uint16_t subsampling[2]
YUV subsampling factors.
Definition: tiffenc.c:77
TIFF_PHOTOMETRIC_WHITE_IS_ZERO
@ TIFF_PHOTOMETRIC_WHITE_IS_ZERO
Definition: tiff.h:185
TIFF_PACKBITS
@ TIFF_PACKBITS
Definition: tiff.h:129
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:346
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
init
static int init
Definition: av_tx.c:47
TiffEncoderContext::yuv_line_size
unsigned int yuv_line_size
Definition: tiffenc.c:70
rgb
Definition: rpzaenc.c:59
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
add_entry1
static int add_entry1(TiffEncoderContext *s, enum TiffTags tag, enum TiffTypes type, int val)
Definition: tiffenc.c:152
fail
#define fail()
Definition: checkasm.h:131
TIFF_YCBCR_SUBSAMPLING
@ TIFF_YCBCR_SUBSAMPLING
Definition: tiff.h:86
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:469
val
static double val(void *priv, double ch)
Definition: aeval.c:77
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:2690
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:553
TiffEncoderContext::num_entries
int num_entries
number of entries
Definition: tiffenc.c:73
TIFF_SOFTWARE_NAME
@ TIFF_SOFTWARE_NAME
Definition: tiff.h:73
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:263
FF_LZW_TIFF
@ FF_LZW_TIFF
Definition: lzw.h:39
AVRational::num
int num
Numerator.
Definition: rational.h:59
TIFF_SHORT
@ TIFF_SHORT
Definition: tiff_common.h:39
ADD_ENTRY1
#define ADD_ENTRY1(s, tag, type, val)
Definition: tiffenc.c:240
VE
#define VE
Definition: tiffenc.c:554
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
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:50
s
#define s(width, name)
Definition: cbs_vp9.c:256
TiffEncoderContext::strips
int strips
number of strips
Definition: tiffenc.c:64
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:281
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
TIFF_STRING
@ TIFF_STRING
Definition: tiff_common.h:38
AV_INPUT_BUFFER_MIN_SIZE
#define AV_INPUT_BUFFER_MIN_SIZE
Definition: avcodec.h:191
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: codec.h:113
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:136
ADD_ENTRY
#define ADD_ENTRY(s, tag, type, count, ptr_val)
Definition: tiffenc.c:233
TIFF_LZW
@ TIFF_LZW
Definition: tiff.h:125
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
TIFF_PHOTOMETRIC_YCBCR
@ TIFF_PHOTOMETRIC_YCBCR
Definition: tiff.h:191
TiffEncoderContext::buf
uint8_t ** buf
actual position in buffer
Definition: tiffenc.c:74
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:622
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
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AV_PIX_FMT_RGBA64LE
@ AV_PIX_FMT_RGBA64LE
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: pixfmt.h:196
TiffEncoderContext
Definition: tiffenc.c:54
TiffEncoderContext::dpi
uint32_t dpi
image resolution in DPI
Definition: tiffenc.c:79
TIFF_PAL
@ TIFF_PAL
Definition: tiff.h:78
TiffEncoderContext::strip_offsets
uint32_t * strip_offsets
Definition: tiffenc.c:67
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:78
TIFF_WIDTH
@ TIFF_WIDTH
Definition: tiff.h:49
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:132
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
TIFF_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:128
codec_internal.h
lzw.h
LZW decoding routines.
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: tiffenc.c:247
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:65
TiffEncoderContext::buf_size
int buf_size
buffer size
Definition: tiffenc.c:76
TiffEncoderContext::compr
int compr
compression level
Definition: tiffenc.c:61
tiffenc_class
static const AVClass tiffenc_class
Definition: tiffenc.c:565
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
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
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:108
TiffEncoderContext::photometric_interpretation
enum TiffPhotometric photometric_interpretation
photometric interpretation
Definition: tiffenc.c:63
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1441
bytestream_put_buffer
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:372
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
TIFF_PHOTOMETRIC_BLACK_IS_ZERO
@ TIFF_PHOTOMETRIC_BLACK_IS_ZERO
Definition: tiff.h:186
av_fast_padded_malloc
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:48
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
ff_lzw_encode_flush
int ff_lzw_encode_flush(struct LZWEncodeState *s)
Write end code and flush bitstream.
Definition: lzwenc.c:263
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:980
AVCodecContext::height
int height
Definition: avcodec.h:562
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:599
TIFF_STRIP_OFFS
@ TIFF_STRIP_OFFS
Definition: tiff.h:59
TIFF_MAX_ENTRY
#define TIFF_MAX_ENTRY
Definition: tiffenc.c:47
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:1646
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:71
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:53
ff_tiff_encoder
const FFCodec ff_tiff_encoder
Definition: tiffenc.c:572
TIFF_PHOTOMETRIC
@ TIFF_PHOTOMETRIC
Definition: tiff.h:53
TiffPhotometric
TiffPhotometric
list of TIFF, TIFF/AP and DNG PhotometricInterpretation (TIFF_PHOTOMETRIC) values
Definition: tiff.h:183
AVCodecContext
main external API structure.
Definition: avcodec.h:389
TiffEncoderContext::bpp
unsigned int bpp
bits per pixel
Definition: tiffenc.c:60
options
static const AVOption options[]
Definition: tiffenc.c:555
TIFF_RATIONAL
@ TIFF_RATIONAL
Definition: tiff_common.h:41
TiffEncoderContext::yuv_line
uint8_t * yuv_line
Definition: tiffenc.c:69
AVRational::den
int den
Denominator.
Definition: rational.h:60
TiffEncoderContext::strip_offsets_size
unsigned int strip_offsets_size
Definition: tiffenc.c:68
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
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:61
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
desc
const char * desc
Definition: libsvtav1.c:83
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
TiffEncoderContext::height
int height
picture height
Definition: tiffenc.c:59
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:71
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:278
pack_yuv
static void pack_yuv(TiffEncoderContext *s, const AVFrame *p, uint8_t *dst, int lnum)
Definition: tiffenc.c:205
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
TIFF_DEFLATE
@ TIFF_DEFLATE
Definition: tiff.h:130
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
TIFF_PHOTOMETRIC_RGB
@ TIFF_PHOTOMETRIC_RGB
Definition: tiff.h:187
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
AVPacket
This structure stores compressed data.
Definition: packet.h:351
TiffEncoderContext::buf_start
uint8_t * buf_start
pointer to first byte in buffer
Definition: tiffenc.c:75
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
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:68
TIFF_XRES
@ TIFF_XRES
Definition: tiff.h:63
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:562
bytestream.h
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:370
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:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
encode_init
static av_cold int encode_init(AVCodecContext *avctx)
Definition: tiffenc.c:525
AV_CODEC_ID_TIFF
@ AV_CODEC_ID_TIFF
Definition: codec_id.h:146
type_sizes
static const uint8_t type_sizes[14]
sizes of various TIFF field types (string size = 100)
Definition: tiff_common.h:53
ff_lzw_encode
int ff_lzw_encode(struct LZWEncodeState *s, const uint8_t *inbuf, int insize)
LZW main compress function.
Definition: lzwenc.c:230
put_bits.h
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:35
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:759