FFmpeg
pngenc.c
Go to the documentation of this file.
1 /*
2  * PNG image format
3  * Copyright (c) 2003 Fabrice Bellard
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 #include "avcodec.h"
23 #include "codec_internal.h"
24 #include "encode.h"
25 #include "exif_internal.h"
26 #include "bytestream.h"
27 #include "lossless_videoencdsp.h"
28 #include "png.h"
29 #include "apng.h"
30 #include "zlib_wrapper.h"
31 
32 #include "libavutil/avassert.h"
33 #include "libavutil/buffer.h"
34 #include "libavutil/crc.h"
35 #include "libavutil/csp.h"
36 #include "libavutil/libm.h"
38 #include "libavutil/mem.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/rational.h"
41 #include "libavutil/stereo3d.h"
42 
43 #include <zlib.h>
44 
45 #define IOBUF_SIZE 4096
46 
47 typedef struct APNGFctlChunk {
48  uint32_t sequence_number;
49  uint32_t width, height;
50  uint32_t x_offset, y_offset;
51  uint16_t delay_num, delay_den;
52  uint8_t dispose_op, blend_op;
54 
55 typedef struct PNGEncContext {
56  AVClass *class;
58 
59  uint8_t *bytestream;
60  uint8_t *bytestream_start;
61  uint8_t *bytestream_end;
62 
64 
66  uint8_t buf[IOBUF_SIZE];
67  int dpi; ///< Physical pixel density, in dots per inch, if set
68  int dpm; ///< Physical pixel density, in dots per meter, if set
69 
71  int bit_depth;
74 
75  // APNG
76  uint32_t palette_checksum; // Used to ensure a single unique palette
77  uint32_t sequence_number;
79  uint8_t *extra_data;
81 
88 
89 static void png_get_interlaced_row(uint8_t *dst, int row_size,
90  int bits_per_pixel, int pass,
91  const uint8_t *src, int width)
92 {
93  int x, mask, dst_x, j, b, bpp;
94  uint8_t *d;
95  const uint8_t *s;
96  static const int masks[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
97 
98  mask = masks[pass];
99  switch (bits_per_pixel) {
100  case 1:
101  memset(dst, 0, row_size);
102  dst_x = 0;
103  for (x = 0; x < width; x++) {
104  j = (x & 7);
105  if ((mask << j) & 0x80) {
106  b = (src[x >> 3] >> (7 - j)) & 1;
107  dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
108  dst_x++;
109  }
110  }
111  break;
112  default:
113  bpp = bits_per_pixel >> 3;
114  d = dst;
115  s = src;
116  for (x = 0; x < width; x++) {
117  j = x & 7;
118  if ((mask << j) & 0x80) {
119  memcpy(d, s, bpp);
120  d += bpp;
121  }
122  s += bpp;
123  }
124  break;
125  }
126 }
127 
128 static void sub_png_paeth_prediction(uint8_t *dst, const uint8_t *src, const uint8_t *top,
129  int w, int bpp)
130 {
131  int i;
132  for (i = 0; i < w; i++) {
133  int a, b, c, p, pa, pb, pc;
134 
135  a = src[i - bpp];
136  b = top[i];
137  c = top[i - bpp];
138 
139  p = b - c;
140  pc = a - c;
141 
142  pa = abs(p);
143  pb = abs(pc);
144  pc = abs(p + pc);
145 
146  if (pa <= pb && pa <= pc)
147  p = a;
148  else if (pb <= pc)
149  p = b;
150  else
151  p = c;
152  dst[i] = src[i] - p;
153  }
154 }
155 
156 static void sub_left_prediction(PNGEncContext *c, uint8_t *dst, const uint8_t *src, int bpp, int size)
157 {
158  const uint8_t *src1 = src + bpp;
159  const uint8_t *src2 = src;
160  int x, unaligned_w;
161 
162  memcpy(dst, src, bpp);
163  dst += bpp;
164  size -= bpp;
165  unaligned_w = FFMIN(32 - bpp, size);
166  for (x = 0; x < unaligned_w; x++)
167  *dst++ = *src1++ - *src2++;
168  size -= unaligned_w;
169  c->llvidencdsp.diff_bytes(dst, src1, src2, size);
170 }
171 
172 static void png_filter_row(PNGEncContext *c, uint8_t *dst, int filter_type,
173  const uint8_t *src, const uint8_t *top, int size, int bpp)
174 {
175  int i;
176 
177  switch (filter_type) {
179  memcpy(dst, src, size);
180  break;
182  sub_left_prediction(c, dst, src, bpp, size);
183  break;
184  case PNG_FILTER_VALUE_UP:
185  c->llvidencdsp.diff_bytes(dst, src, top, size);
186  break;
188  for (i = 0; i < bpp; i++)
189  dst[i] = src[i] - (top[i] >> 1);
190  for (; i < size; i++)
191  dst[i] = src[i] - ((src[i - bpp] + top[i]) >> 1);
192  break;
194  for (i = 0; i < bpp; i++)
195  dst[i] = src[i] - top[i];
196  sub_png_paeth_prediction(dst + i, src + i, top + i, size - i, bpp);
197  break;
198  }
199 }
200 
201 static uint8_t *png_choose_filter(PNGEncContext *s, uint8_t *dst,
202  const uint8_t *src, const uint8_t *top, int size, int bpp)
203 {
204  int pred = s->filter_type;
205  av_assert0(bpp || !pred);
206  if (!top && pred)
208  if (pred == PNG_FILTER_VALUE_MIXED) {
209  int i;
210  int cost, bcost = INT_MAX;
211  uint8_t *buf1 = dst, *buf2 = dst + size + 16;
212  for (pred = 0; pred < 5; pred++) {
213  png_filter_row(s, buf1 + 1, pred, src, top, size, bpp);
214  buf1[0] = pred;
215  cost = 0;
216  for (i = 0; i <= size; i++)
217  cost += abs((int8_t) buf1[i]);
218  if (cost < bcost) {
219  bcost = cost;
220  FFSWAP(uint8_t *, buf1, buf2);
221  }
222  }
223  return buf2;
224  } else {
225  png_filter_row(s, dst + 1, pred, src, top, size, bpp);
226  dst[0] = pred;
227  return dst;
228  }
229 }
230 
231 static void png_write_chunk(uint8_t **f, uint32_t tag,
232  const uint8_t *buf, int length)
233 {
234  const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
235  uint32_t crc = ~0U;
236  uint8_t tagbuf[4];
237 
238  bytestream_put_be32(f, length);
239  AV_WL32(tagbuf, tag);
240  crc = av_crc(crc_table, crc, tagbuf, 4);
241  bytestream_put_be32(f, av_bswap32(tag));
242  if (length > 0) {
243  crc = av_crc(crc_table, crc, buf, length);
244  if (*f != buf)
245  memcpy(*f, buf, length);
246  *f += length;
247  }
248  bytestream_put_be32(f, ~crc);
249 }
250 
252  const uint8_t *buf, int length)
253 {
254  PNGEncContext *s = avctx->priv_data;
255  const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
256  uint32_t crc = ~0U;
257 
258  if (avctx->codec_id == AV_CODEC_ID_PNG || avctx->frame_num == 0) {
259  png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), buf, length);
260  return;
261  }
262 
263  bytestream_put_be32(&s->bytestream, length + 4);
264 
265  bytestream_put_be32(&s->bytestream, MKBETAG('f', 'd', 'A', 'T'));
266  bytestream_put_be32(&s->bytestream, s->sequence_number);
267  crc = av_crc(crc_table, crc, s->bytestream - 8, 8);
268 
269  crc = av_crc(crc_table, crc, buf, length);
270  memcpy(s->bytestream, buf, length);
271  s->bytestream += length;
272 
273  bytestream_put_be32(&s->bytestream, ~crc);
274 
275  ++s->sequence_number;
276 }
277 
278 /* XXX: do filtering */
279 static int png_write_row(AVCodecContext *avctx, const uint8_t *data, int size)
280 {
281  PNGEncContext *s = avctx->priv_data;
282  z_stream *const zstream = &s->zstream.zstream;
283  int ret;
284 
285  zstream->avail_in = size;
286  zstream->next_in = data;
287  while (zstream->avail_in > 0) {
288  ret = deflate(zstream, Z_NO_FLUSH);
289  if (ret != Z_OK)
290  return -1;
291  if (zstream->avail_out == 0) {
292  if (s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
293  png_write_image_data(avctx, s->buf, IOBUF_SIZE);
294  zstream->avail_out = IOBUF_SIZE;
295  zstream->next_out = s->buf;
296  }
297  }
298  return 0;
299 }
300 
301 #define PNG_LRINT(d, divisor) lrint((d) * (divisor))
302 #define PNG_Q2D(q, divisor) PNG_LRINT(av_q2d(q), (divisor))
303 #define AV_WB32_PNG_D(buf, q) AV_WB32(buf, PNG_Q2D(q, 100000))
304 static int png_get_chrm(enum AVColorPrimaries prim, uint8_t *buf)
305 {
307  if (!desc)
308  return 0;
309 
310  AV_WB32_PNG_D(buf, desc->wp.x);
311  AV_WB32_PNG_D(buf + 4, desc->wp.y);
312  AV_WB32_PNG_D(buf + 8, desc->prim.r.x);
313  AV_WB32_PNG_D(buf + 12, desc->prim.r.y);
314  AV_WB32_PNG_D(buf + 16, desc->prim.g.x);
315  AV_WB32_PNG_D(buf + 20, desc->prim.g.y);
316  AV_WB32_PNG_D(buf + 24, desc->prim.b.x);
317  AV_WB32_PNG_D(buf + 28, desc->prim.b.y);
318 
319  return 1;
320 }
321 
322 static int png_get_gama(enum AVColorTransferCharacteristic trc, uint8_t *buf)
323 {
324  double gamma = av_csp_approximate_trc_gamma(trc);
325  if (gamma <= 1e-6)
326  return 0;
327 
328  AV_WB32(buf, PNG_LRINT(1.0 / gamma, 100000));
329  return 1;
330 }
331 
333 {
334  z_stream *const zstream = &s->zstream.zstream;
335  const AVDictionaryEntry *entry;
336  const char *name;
337  uint8_t *start, *buf;
338  int ret;
339 
340  if (!sd || !sd->size)
341  return 0;
342  zstream->next_in = sd->data;
343  zstream->avail_in = sd->size;
344 
345  /* write the chunk contents first */
346  start = s->bytestream + 8; /* make room for iCCP tag + length */
347  buf = start;
348 
349  /* profile description */
350  entry = av_dict_get(sd->metadata, "name", NULL, 0);
351  name = (entry && entry->value[0]) ? entry->value : "icc";
352  for (int i = 0;; i++) {
353  char c = (i == 79) ? 0 : name[i];
354  bytestream_put_byte(&buf, c);
355  if (!c)
356  break;
357  }
358 
359  /* compression method and profile data */
360  bytestream_put_byte(&buf, 0);
361  zstream->next_out = buf;
362  zstream->avail_out = s->bytestream_end - buf;
363  ret = deflate(zstream, Z_FINISH);
364  deflateReset(zstream);
365  if (ret != Z_STREAM_END)
366  return AVERROR_EXTERNAL;
367 
368  /* rewind to the start and write the chunk header/crc */
369  png_write_chunk(&s->bytestream, MKTAG('i', 'C', 'C', 'P'), start,
370  zstream->next_out - start);
371  return 0;
372 }
373 
374 static int encode_headers(AVCodecContext *avctx, const AVFrame *pict)
375 {
376  AVFrameSideData *side_data;
377  PNGEncContext *s = avctx->priv_data;
378  AVBufferRef *exif_data = NULL;
379  int ret;
380 
381  /* write png header */
382  AV_WB32(s->buf, avctx->width);
383  AV_WB32(s->buf + 4, avctx->height);
384  s->buf[8] = s->bit_depth;
385  s->buf[9] = s->color_type;
386  s->buf[10] = 0; /* compression type */
387  s->buf[11] = 0; /* filter type */
388  s->buf[12] = s->is_progressive; /* interlace type */
389  png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
390 
391  /* write physical information */
392  if (s->dpm) {
393  AV_WB32(s->buf, s->dpm);
394  AV_WB32(s->buf + 4, s->dpm);
395  s->buf[8] = 1; /* unit specifier is meter */
396  } else {
397  AV_WB32(s->buf, avctx->sample_aspect_ratio.num);
398  AV_WB32(s->buf + 4, avctx->sample_aspect_ratio.den);
399  s->buf[8] = 0; /* unit specifier is unknown */
400  }
401  png_write_chunk(&s->bytestream, MKTAG('p', 'H', 'Y', 's'), s->buf, 9);
402 
403  /* write stereoscopic information */
405  if (side_data) {
406  AVStereo3D *stereo3d = (AVStereo3D *)side_data->data;
407  switch (stereo3d->type) {
409  s->buf[0] = ((stereo3d->flags & AV_STEREO3D_FLAG_INVERT) == 0) ? 1 : 0;
410  png_write_chunk(&s->bytestream, MKTAG('s', 'T', 'E', 'R'), s->buf, 1);
411  break;
412  case AV_STEREO3D_2D:
413  break;
414  default:
415  av_log(avctx, AV_LOG_WARNING, "Only side-by-side stereo3d flag can be defined within sTER chunk\n");
416  break;
417  }
418  }
419 
420  ret = ff_exif_get_buffer(avctx, pict, &exif_data, AV_EXIF_TIFF_HEADER);
421  if (exif_data) {
422  // png_write_chunk accepts an int, not a size_t, so we have to check overflow
423  if (exif_data->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
424  // that's a very big exif chunk, probably a bug
425  av_log(avctx, AV_LOG_ERROR, "extremely large EXIF buffer detected, not writing\n");
426  else
427  png_write_chunk(&s->bytestream, MKTAG('e','X','I','f'), exif_data->data, exif_data->size);
428  av_buffer_unref(&exif_data);
429  } else if (ret < 0) {
430  av_log(avctx, AV_LOG_WARNING, "unable to attach EXIF metadata: %s\n", av_err2str(ret));
431  }
432 
434  if ((ret = png_write_iccp(s, side_data)))
435  return ret;
436 
437  /* write colorspace information */
438  if (pict->color_primaries == AVCOL_PRI_BT709 &&
440  s->buf[0] = 1; /* rendering intent, relative colorimetric by default */
441  png_write_chunk(&s->bytestream, MKTAG('s', 'R', 'G', 'B'), s->buf, 1);
442  } else if (pict->color_trc != AVCOL_TRC_UNSPECIFIED && !side_data) {
443  /*
444  * Avoid writing cICP if the transfer is unknown. Known primaries
445  * with unknown transfer can be handled by cHRM.
446  *
447  * We also avoid writing cICP if an ICC Profile is present, because
448  * the standard requires that cICP overrides iCCP.
449  *
450  * These values match H.273 so no translation is needed.
451  */
452  s->buf[0] = pict->color_primaries;
453  s->buf[1] = pict->color_trc;
454  s->buf[2] = 0; /* colorspace = RGB */
455  s->buf[3] = pict->color_range == AVCOL_RANGE_MPEG ? 0 : 1;
456  png_write_chunk(&s->bytestream, MKTAG('c', 'I', 'C', 'P'), s->buf, 4);
457  }
458 
460  if (side_data) {
461  AVContentLightMetadata *clli = (AVContentLightMetadata *) side_data->data;
462  AV_WB32(s->buf, clli->MaxCLL * 10000);
463  AV_WB32(s->buf + 4, clli->MaxFALL * 10000);
464  png_write_chunk(&s->bytestream, MKTAG('c', 'L', 'L', 'I'), s->buf, 8);
465  }
466 
468  if (side_data) {
470  if (mdcv->has_luminance && mdcv->has_primaries) {
471  for (int i = 0; i < 3; i++) {
472  AV_WB16(s->buf + 2*i, PNG_Q2D(mdcv->display_primaries[i][0], 50000));
473  AV_WB16(s->buf + 2*i + 2, PNG_Q2D(mdcv->display_primaries[i][1], 50000));
474  }
475  AV_WB16(s->buf + 12, PNG_Q2D(mdcv->white_point[0], 50000));
476  AV_WB16(s->buf + 14, PNG_Q2D(mdcv->white_point[1], 50000));
477  AV_WB32(s->buf + 16, PNG_Q2D(mdcv->max_luminance, 10000));
478  AV_WB32(s->buf + 20, PNG_Q2D(mdcv->min_luminance, 10000));
479  png_write_chunk(&s->bytestream, MKTAG('m', 'D', 'C', 'V'), s->buf, 24);
480  }
481  }
482 
483  if (png_get_chrm(pict->color_primaries, s->buf))
484  png_write_chunk(&s->bytestream, MKTAG('c', 'H', 'R', 'M'), s->buf, 32);
485  if (png_get_gama(pict->color_trc, s->buf))
486  png_write_chunk(&s->bytestream, MKTAG('g', 'A', 'M', 'A'), s->buf, 4);
487 
488  if (avctx->bits_per_raw_sample > 0 &&
489  avctx->bits_per_raw_sample < (s->color_type & PNG_COLOR_MASK_PALETTE ? 8 : s->bit_depth)) {
490  int len = s->color_type & PNG_COLOR_MASK_PALETTE ? 3 : ff_png_get_nb_channels(s->color_type);
491  memset(s->buf, avctx->bits_per_raw_sample, len);
492  png_write_chunk(&s->bytestream, MKTAG('s', 'B', 'I', 'T'), s->buf, len);
493  }
494 
495  /* put the palette if needed, must be after colorspace information */
496  if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
497  int has_alpha, alpha, i;
498  unsigned int v;
499  uint32_t *palette;
500  uint8_t *ptr, *alpha_ptr;
501 
502  palette = (uint32_t *)pict->data[1];
503  ptr = s->buf;
504  alpha_ptr = s->buf + 256 * 3;
505  has_alpha = 0;
506  for (i = 0; i < 256; i++) {
507  v = palette[i];
508  alpha = v >> 24;
509  if (alpha != 0xff)
510  has_alpha = 1;
511  *alpha_ptr++ = alpha;
512  bytestream_put_be24(&ptr, v);
513  }
514  png_write_chunk(&s->bytestream,
515  MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
516  if (has_alpha) {
517  png_write_chunk(&s->bytestream,
518  MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
519  }
520  }
521 
522  return 0;
523 }
524 
525 static int encode_frame(AVCodecContext *avctx, const AVFrame *pict)
526 {
527  PNGEncContext *s = avctx->priv_data;
528  z_stream *const zstream = &s->zstream.zstream;
529  const AVFrame *const p = pict;
530  int y, len, ret;
531  int row_size, pass_row_size;
532  uint8_t *crow_buf, *crow;
533  uint8_t *crow_base = NULL;
534  uint8_t *progressive_buf = NULL;
535  uint8_t *top_buf = NULL;
536 
537  row_size = (pict->width * s->bits_per_pixel + 7) >> 3;
538 
539  crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
540  if (!crow_base) {
541  ret = AVERROR(ENOMEM);
542  goto the_end;
543  }
544  // pixel data should be aligned, but there's a control byte before it
545  crow_buf = crow_base + 15;
546  if (s->is_progressive) {
547  progressive_buf = av_malloc(row_size + 1);
548  top_buf = av_malloc(row_size + 1);
549  if (!progressive_buf || !top_buf) {
550  ret = AVERROR(ENOMEM);
551  goto the_end;
552  }
553  }
554 
555  /* put each row */
556  zstream->avail_out = IOBUF_SIZE;
557  zstream->next_out = s->buf;
558  if (s->is_progressive) {
559  int pass;
560 
561  for (pass = 0; pass < NB_PASSES; pass++) {
562  /* NOTE: a pass is completely omitted if no pixels would be
563  * output */
564  pass_row_size = ff_png_pass_row_size(pass, s->bits_per_pixel, pict->width);
565  if (pass_row_size > 0) {
566  uint8_t *top = NULL;
567  for (y = 0; y < pict->height; y++)
568  if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
569  const uint8_t *ptr = p->data[0] + y * p->linesize[0];
570  FFSWAP(uint8_t *, progressive_buf, top_buf);
571  png_get_interlaced_row(progressive_buf, pass_row_size,
572  s->bits_per_pixel, pass,
573  ptr, pict->width);
574  crow = png_choose_filter(s, crow_buf, progressive_buf,
575  top, pass_row_size, s->bits_per_pixel >> 3);
576  png_write_row(avctx, crow, pass_row_size + 1);
577  top = progressive_buf;
578  }
579  }
580  }
581  } else {
582  const uint8_t *top = NULL;
583  for (y = 0; y < pict->height; y++) {
584  const uint8_t *ptr = p->data[0] + y * p->linesize[0];
585  crow = png_choose_filter(s, crow_buf, ptr, top,
586  row_size, s->bits_per_pixel >> 3);
587  png_write_row(avctx, crow, row_size + 1);
588  top = ptr;
589  }
590  }
591  /* compress last bytes */
592  for (;;) {
593  ret = deflate(zstream, Z_FINISH);
594  if (ret == Z_OK || ret == Z_STREAM_END) {
595  len = IOBUF_SIZE - zstream->avail_out;
596  if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
597  png_write_image_data(avctx, s->buf, len);
598  }
599  zstream->avail_out = IOBUF_SIZE;
600  zstream->next_out = s->buf;
601  if (ret == Z_STREAM_END)
602  break;
603  } else {
604  ret = -1;
605  goto the_end;
606  }
607  }
608 
609  ret = 0;
610 
611 the_end:
612  av_freep(&crow_base);
613  av_freep(&progressive_buf);
614  av_freep(&top_buf);
615  deflateReset(zstream);
616  return ret;
617 }
618 
619 static int add_icc_profile_size(AVCodecContext *avctx, const AVFrame *pict,
620  uint64_t *max_packet_size)
621 {
622  PNGEncContext *s = avctx->priv_data;
623  const AVFrameSideData *sd;
624  const int hdr_size = 128;
625  uint64_t new_pkt_size;
626  uLong bound;
627 
628  if (!pict)
629  return 0;
631  if (!sd || !sd->size)
632  return 0;
633  if (sd->size != (uLong) sd->size)
634  return AVERROR_INVALIDDATA;
635 
636  bound = deflateBound(&s->zstream.zstream, sd->size);
637  if (bound > INT32_MAX - hdr_size)
638  return AVERROR_INVALIDDATA;
639 
640  new_pkt_size = *max_packet_size + bound + hdr_size;
641  if (new_pkt_size < *max_packet_size)
642  return AVERROR_INVALIDDATA;
643  *max_packet_size = new_pkt_size;
644  return 0;
645 }
646 
647 static int encode_png(AVCodecContext *avctx, AVPacket *pkt,
648  const AVFrame *pict, int *got_packet)
649 {
650  PNGEncContext *s = avctx->priv_data;
651  int ret;
652  int enc_row_size;
653  uint64_t max_packet_size;
654 
655  enc_row_size = deflateBound(&s->zstream.zstream,
656  (avctx->width * s->bits_per_pixel + 7) >> 3);
657  max_packet_size =
658  FF_INPUT_BUFFER_MIN_SIZE + // headers
659  avctx->height * (
660  enc_row_size +
661  12 * (((int64_t)enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) // IDAT * ceil(enc_row_size / IOBUF_SIZE)
662  );
663  if ((ret = add_icc_profile_size(avctx, pict, &max_packet_size)))
664  return ret;
665  ret = ff_alloc_packet(avctx, pkt, max_packet_size);
666  if (ret < 0)
667  return ret;
668 
669  s->bytestream_start =
670  s->bytestream = pkt->data;
671  s->bytestream_end = pkt->data + pkt->size;
672 
673  AV_WB64(s->bytestream, PNGSIG);
674  s->bytestream += 8;
675 
676  ret = encode_headers(avctx, pict);
677  if (ret < 0)
678  return ret;
679 
680  ret = encode_frame(avctx, pict);
681  if (ret < 0)
682  return ret;
683 
684  png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
685 
686  pkt->size = s->bytestream - s->bytestream_start;
688  *got_packet = 1;
689 
690  return 0;
691 }
692 
694  APNGFctlChunk *fctl_chunk, uint8_t bpp)
695 {
696  // output: background, input: foreground
697  // output the image such that when blended with the background, will produce the foreground
698 
699  unsigned int x, y;
700  unsigned int leftmost_x = input->width;
701  unsigned int rightmost_x = 0;
702  unsigned int topmost_y = input->height;
703  unsigned int bottommost_y = 0;
704  const uint8_t *input_data = input->data[0];
705  uint8_t *output_data = output->data[0];
706  ptrdiff_t input_linesize = input->linesize[0];
707  ptrdiff_t output_linesize = output->linesize[0];
708 
709  // Find bounding box of changes
710  for (y = 0; y < input->height; ++y) {
711  for (x = 0; x < input->width; ++x) {
712  if (!memcmp(input_data + bpp * x, output_data + bpp * x, bpp))
713  continue;
714 
715  if (x < leftmost_x)
716  leftmost_x = x;
717  if (x >= rightmost_x)
718  rightmost_x = x + 1;
719  if (y < topmost_y)
720  topmost_y = y;
721  if (y >= bottommost_y)
722  bottommost_y = y + 1;
723  }
724 
725  input_data += input_linesize;
726  output_data += output_linesize;
727  }
728 
729  if (leftmost_x == input->width && rightmost_x == 0) {
730  // Empty frame
731  // APNG does not support empty frames, so we make it a 1x1 frame
732  leftmost_x = topmost_y = 0;
733  rightmost_x = bottommost_y = 1;
734  }
735 
736  // Do actual inverse blending
737  if (fctl_chunk->blend_op == APNG_BLEND_OP_SOURCE) {
738  output_data = output->data[0];
739  for (y = topmost_y; y < bottommost_y; ++y) {
740  memcpy(output_data,
741  input->data[0] + input_linesize * y + bpp * leftmost_x,
742  bpp * (rightmost_x - leftmost_x));
743  output_data += output_linesize;
744  }
745  } else { // APNG_BLEND_OP_OVER
746  size_t transparent_palette_index;
747  uint32_t *palette;
748 
749  switch (input->format) {
750  case AV_PIX_FMT_RGBA64BE:
751  case AV_PIX_FMT_YA16BE:
752  case AV_PIX_FMT_RGBA:
753  case AV_PIX_FMT_GRAY8A:
754  break;
755 
756  case AV_PIX_FMT_PAL8:
757  palette = (uint32_t*)input->data[1];
758  for (transparent_palette_index = 0; transparent_palette_index < 256; ++transparent_palette_index)
759  if (palette[transparent_palette_index] >> 24 == 0)
760  break;
761  break;
762 
763  default:
764  // No alpha, so blending not possible
765  return -1;
766  }
767 
768  for (y = topmost_y; y < bottommost_y; ++y) {
769  const uint8_t *foreground = input->data[0] + input_linesize * y + bpp * leftmost_x;
770  uint8_t *background = output->data[0] + output_linesize * y + bpp * leftmost_x;
771  output_data = output->data[0] + output_linesize * (y - topmost_y);
772  for (x = leftmost_x; x < rightmost_x; ++x, foreground += bpp, background += bpp, output_data += bpp) {
773  if (!memcmp(foreground, background, bpp)) {
774  if (input->format == AV_PIX_FMT_PAL8) {
775  if (transparent_palette_index == 256) {
776  // Need fully transparent colour, but none exists
777  return -1;
778  }
779 
780  *output_data = transparent_palette_index;
781  } else {
782  memset(output_data, 0, bpp);
783  }
784  continue;
785  }
786 
787  // Check for special alpha values, since full inverse
788  // alpha-on-alpha blending is rarely possible, and when
789  // possible, doesn't compress much better than
790  // APNG_BLEND_OP_SOURCE blending
791  switch (input->format) {
792  case AV_PIX_FMT_RGBA64BE:
793  if (((uint16_t*)foreground)[3] == 0xffff ||
794  ((uint16_t*)background)[3] == 0)
795  break;
796  return -1;
797 
798  case AV_PIX_FMT_YA16BE:
799  if (((uint16_t*)foreground)[1] == 0xffff ||
800  ((uint16_t*)background)[1] == 0)
801  break;
802  return -1;
803 
804  case AV_PIX_FMT_RGBA:
805  if (foreground[3] == 0xff || background[3] == 0)
806  break;
807  return -1;
808 
809  case AV_PIX_FMT_GRAY8A:
810  if (foreground[1] == 0xff || background[1] == 0)
811  break;
812  return -1;
813 
814  case AV_PIX_FMT_PAL8:
815  if (palette[*foreground] >> 24 == 0xff ||
816  palette[*background] >> 24 == 0)
817  break;
818  return -1;
819  }
820 
821  memmove(output_data, foreground, bpp);
822  }
823  }
824  }
825 
826  output->width = rightmost_x - leftmost_x;
827  output->height = bottommost_y - topmost_y;
828  fctl_chunk->width = output->width;
829  fctl_chunk->height = output->height;
830  fctl_chunk->x_offset = leftmost_x;
831  fctl_chunk->y_offset = topmost_y;
832 
833  return 0;
834 }
835 
836 static int apng_encode_frame(AVCodecContext *avctx, const AVFrame *pict,
837  APNGFctlChunk *best_fctl_chunk, APNGFctlChunk *best_last_fctl_chunk)
838 {
839  PNGEncContext *s = avctx->priv_data;
840  int ret;
841  unsigned int y;
842  AVFrame* diffFrame;
843  uint8_t bpp = (s->bits_per_pixel + 7) >> 3;
844  uint8_t *original_bytestream, *original_bytestream_end;
845  uint8_t *temp_bytestream = 0, *temp_bytestream_end;
846  uint32_t best_sequence_number;
847  uint8_t *best_bytestream;
848  size_t best_bytestream_size = SIZE_MAX;
849  APNGFctlChunk last_fctl_chunk = *best_last_fctl_chunk;
850  APNGFctlChunk fctl_chunk = *best_fctl_chunk;
851 
852  if (avctx->frame_num == 0) {
853  best_fctl_chunk->width = pict->width;
854  best_fctl_chunk->height = pict->height;
855  best_fctl_chunk->x_offset = 0;
856  best_fctl_chunk->y_offset = 0;
857  best_fctl_chunk->blend_op = APNG_BLEND_OP_SOURCE;
858  return encode_frame(avctx, pict);
859  }
860 
861  diffFrame = av_frame_alloc();
862  if (!diffFrame)
863  return AVERROR(ENOMEM);
864 
865  diffFrame->format = pict->format;
866  diffFrame->width = pict->width;
867  diffFrame->height = pict->height;
868  if ((ret = av_frame_get_buffer(diffFrame, 0)) < 0)
869  goto fail;
870 
871  original_bytestream = s->bytestream;
872  original_bytestream_end = s->bytestream_end;
873 
874  temp_bytestream = av_malloc(original_bytestream_end - original_bytestream);
875  if (!temp_bytestream) {
876  ret = AVERROR(ENOMEM);
877  goto fail;
878  }
879  temp_bytestream_end = temp_bytestream + (original_bytestream_end - original_bytestream);
880 
881  for (last_fctl_chunk.dispose_op = 0; last_fctl_chunk.dispose_op < 3; ++last_fctl_chunk.dispose_op) {
882  // 0: APNG_DISPOSE_OP_NONE
883  // 1: APNG_DISPOSE_OP_BACKGROUND
884  // 2: APNG_DISPOSE_OP_PREVIOUS
885 
886  for (fctl_chunk.blend_op = 0; fctl_chunk.blend_op < 2; ++fctl_chunk.blend_op) {
887  // 0: APNG_BLEND_OP_SOURCE
888  // 1: APNG_BLEND_OP_OVER
889 
890  uint32_t original_sequence_number = s->sequence_number, sequence_number;
891  uint8_t *bytestream_start = s->bytestream;
892  size_t bytestream_size;
893 
894  // Do disposal
895  if (last_fctl_chunk.dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
896  diffFrame->width = pict->width;
897  diffFrame->height = pict->height;
898  ret = av_frame_copy(diffFrame, s->last_frame);
899  if (ret < 0)
900  goto fail;
901 
902  if (last_fctl_chunk.dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
903  for (y = last_fctl_chunk.y_offset; y < last_fctl_chunk.y_offset + last_fctl_chunk.height; ++y) {
904  size_t row_start = diffFrame->linesize[0] * y + bpp * last_fctl_chunk.x_offset;
905  memset(diffFrame->data[0] + row_start, 0, bpp * last_fctl_chunk.width);
906  }
907  }
908  } else {
909  if (!s->prev_frame)
910  continue;
911 
912  diffFrame->width = pict->width;
913  diffFrame->height = pict->height;
914  ret = av_frame_copy(diffFrame, s->prev_frame);
915  if (ret < 0)
916  goto fail;
917  }
918 
919  // Do inverse blending
920  if (apng_do_inverse_blend(diffFrame, pict, &fctl_chunk, bpp) < 0)
921  continue;
922 
923  // Do encoding
924  ret = encode_frame(avctx, diffFrame);
925  sequence_number = s->sequence_number;
926  s->sequence_number = original_sequence_number;
927  bytestream_size = s->bytestream - bytestream_start;
928  s->bytestream = bytestream_start;
929  if (ret < 0)
930  goto fail;
931 
932  if (bytestream_size < best_bytestream_size) {
933  *best_fctl_chunk = fctl_chunk;
934  *best_last_fctl_chunk = last_fctl_chunk;
935 
936  best_sequence_number = sequence_number;
937  best_bytestream = s->bytestream;
938  best_bytestream_size = bytestream_size;
939 
940  if (best_bytestream == original_bytestream) {
941  s->bytestream = temp_bytestream;
942  s->bytestream_end = temp_bytestream_end;
943  } else {
944  s->bytestream = original_bytestream;
945  s->bytestream_end = original_bytestream_end;
946  }
947  }
948  }
949  }
950 
951  s->sequence_number = best_sequence_number;
952  s->bytestream = original_bytestream + best_bytestream_size;
953  s->bytestream_end = original_bytestream_end;
954  if (best_bytestream != original_bytestream)
955  memcpy(original_bytestream, best_bytestream, best_bytestream_size);
956 
957  ret = 0;
958 
959 fail:
960  av_freep(&temp_bytestream);
961  av_frame_free(&diffFrame);
962  return ret;
963 }
964 
966  const AVFrame *pict, int *got_packet)
967 {
968  PNGEncContext *s = avctx->priv_data;
969  int ret;
970  int enc_row_size;
971  uint64_t max_packet_size;
972  APNGFctlChunk fctl_chunk = {0};
973 
974  if (pict && s->color_type == PNG_COLOR_TYPE_PALETTE) {
975  uint32_t checksum = ~av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), ~0U, pict->data[1], 256 * sizeof(uint32_t));
976 
977  if (avctx->frame_num == 0) {
978  s->palette_checksum = checksum;
979  } else if (checksum != s->palette_checksum) {
980  av_log(avctx, AV_LOG_ERROR,
981  "Input contains more than one unique palette. APNG does not support multiple palettes.\n");
982  return -1;
983  }
984  }
985 
986  enc_row_size = deflateBound(&s->zstream.zstream,
987  (avctx->width * s->bits_per_pixel + 7) >> 3);
988  max_packet_size =
989  FF_INPUT_BUFFER_MIN_SIZE + // headers
990  avctx->height * (
991  enc_row_size +
992  (4 + 12) * (((int64_t)enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) // fdAT * ceil(enc_row_size / IOBUF_SIZE)
993  );
994  if ((ret = add_icc_profile_size(avctx, pict, &max_packet_size)))
995  return ret;
996  if (max_packet_size > INT_MAX)
997  return AVERROR(ENOMEM);
998 
999  if (avctx->frame_num == 0) {
1000  if (!pict)
1001  return AVERROR(EINVAL);
1002 
1003  s->bytestream = s->extra_data = av_malloc(FF_INPUT_BUFFER_MIN_SIZE);
1004  if (!s->extra_data)
1005  return AVERROR(ENOMEM);
1006 
1007  ret = encode_headers(avctx, pict);
1008  if (ret < 0)
1009  return ret;
1010 
1011  s->extra_data_size = s->bytestream - s->extra_data;
1012 
1013  s->last_frame_packet = av_malloc(max_packet_size);
1014  if (!s->last_frame_packet)
1015  return AVERROR(ENOMEM);
1016  } else if (s->last_frame) {
1017  ret = ff_get_encode_buffer(avctx, pkt, s->last_frame_packet_size, 0);
1018  if (ret < 0)
1019  return ret;
1020 
1021  memcpy(pkt->data, s->last_frame_packet, s->last_frame_packet_size);
1022  pkt->pts = s->last_frame->pts;
1023  pkt->duration = s->last_frame->duration;
1024 
1025  ret = ff_encode_reordered_opaque(avctx, pkt, s->last_frame);
1026  if (ret < 0)
1027  return ret;
1028  }
1029 
1030  if (pict) {
1031  s->bytestream_start =
1032  s->bytestream = s->last_frame_packet;
1033  s->bytestream_end = s->bytestream + max_packet_size;
1034 
1035  // We're encoding the frame first, so we have to do a bit of shuffling around
1036  // to have the image data write to the correct place in the buffer
1037  fctl_chunk.sequence_number = s->sequence_number;
1038  ++s->sequence_number;
1039  s->bytestream += APNG_FCTL_CHUNK_SIZE + 12;
1040 
1041  ret = apng_encode_frame(avctx, pict, &fctl_chunk, &s->last_frame_fctl);
1042  if (ret < 0)
1043  return ret;
1044 
1045  fctl_chunk.delay_num = 0; // delay filled in during muxing
1046  fctl_chunk.delay_den = 0;
1047  } else {
1048  s->last_frame_fctl.dispose_op = APNG_DISPOSE_OP_NONE;
1049  }
1050 
1051  if (s->last_frame) {
1052  uint8_t* last_fctl_chunk_start = pkt->data;
1053  uint8_t buf[APNG_FCTL_CHUNK_SIZE];
1054  if (!s->extra_data_updated) {
1055  uint8_t *side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, s->extra_data_size);
1056  if (!side_data)
1057  return AVERROR(ENOMEM);
1058  memcpy(side_data, s->extra_data, s->extra_data_size);
1059  s->extra_data_updated = 1;
1060  }
1061 
1062  AV_WB32(buf + 0, s->last_frame_fctl.sequence_number);
1063  AV_WB32(buf + 4, s->last_frame_fctl.width);
1064  AV_WB32(buf + 8, s->last_frame_fctl.height);
1065  AV_WB32(buf + 12, s->last_frame_fctl.x_offset);
1066  AV_WB32(buf + 16, s->last_frame_fctl.y_offset);
1067  AV_WB16(buf + 20, s->last_frame_fctl.delay_num);
1068  AV_WB16(buf + 22, s->last_frame_fctl.delay_den);
1069  buf[24] = s->last_frame_fctl.dispose_op;
1070  buf[25] = s->last_frame_fctl.blend_op;
1071  png_write_chunk(&last_fctl_chunk_start, MKTAG('f', 'c', 'T', 'L'), buf, sizeof(buf));
1072 
1073  *got_packet = 1;
1074  }
1075 
1076  if (pict) {
1077  if (!s->last_frame) {
1078  s->last_frame = av_frame_alloc();
1079  if (!s->last_frame)
1080  return AVERROR(ENOMEM);
1081  } else if (s->last_frame_fctl.dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
1082  if (!s->prev_frame) {
1083  s->prev_frame = av_frame_alloc();
1084  if (!s->prev_frame)
1085  return AVERROR(ENOMEM);
1086 
1087  s->prev_frame->format = pict->format;
1088  s->prev_frame->width = pict->width;
1089  s->prev_frame->height = pict->height;
1090  if ((ret = av_frame_get_buffer(s->prev_frame, 0)) < 0)
1091  return ret;
1092  }
1093 
1094  // Do disposal, but not blending
1095  av_frame_copy(s->prev_frame, s->last_frame);
1096  if (s->last_frame_fctl.dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
1097  uint32_t y;
1098  uint8_t bpp = (s->bits_per_pixel + 7) >> 3;
1099  for (y = s->last_frame_fctl.y_offset; y < s->last_frame_fctl.y_offset + s->last_frame_fctl.height; ++y) {
1100  size_t row_start = s->prev_frame->linesize[0] * y + bpp * s->last_frame_fctl.x_offset;
1101  memset(s->prev_frame->data[0] + row_start, 0, bpp * s->last_frame_fctl.width);
1102  }
1103  }
1104  }
1105 
1106  ret = av_frame_replace(s->last_frame, pict);
1107  if (ret < 0)
1108  return ret;
1109 
1110  s->last_frame_fctl = fctl_chunk;
1111  s->last_frame_packet_size = s->bytestream - s->bytestream_start;
1112  } else {
1113  av_frame_free(&s->last_frame);
1114  }
1115 
1116  return 0;
1117 }
1118 
1120 {
1121  PNGEncContext *s = avctx->priv_data;
1122  int compression_level;
1123 
1124  switch (avctx->pix_fmt) {
1125  case AV_PIX_FMT_RGBA:
1126  avctx->bits_per_coded_sample = 32;
1127  break;
1128  case AV_PIX_FMT_RGB24:
1129  avctx->bits_per_coded_sample = 24;
1130  break;
1131  case AV_PIX_FMT_GRAY8:
1132  avctx->bits_per_coded_sample = 0x28;
1133  break;
1134  case AV_PIX_FMT_MONOBLACK:
1135  avctx->bits_per_coded_sample = 1;
1136  break;
1137  case AV_PIX_FMT_PAL8:
1138  avctx->bits_per_coded_sample = 8;
1139  }
1140 
1141  ff_llvidencdsp_init(&s->llvidencdsp);
1142 
1143  if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
1144  s->filter_type = PNG_FILTER_VALUE_NONE;
1145 
1146  if (s->dpi && s->dpm) {
1147  av_log(avctx, AV_LOG_ERROR, "Only one of 'dpi' or 'dpm' options should be set\n");
1148  return AVERROR(EINVAL);
1149  } else if (s->dpi) {
1150  s->dpm = s->dpi * 10000 / 254;
1151  }
1152 
1153  s->is_progressive = !!(avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT);
1154  switch (avctx->pix_fmt) {
1155  case AV_PIX_FMT_RGBA64BE:
1156  s->bit_depth = 16;
1157  s->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1158  break;
1159  case AV_PIX_FMT_RGB48BE:
1160  s->bit_depth = 16;
1161  s->color_type = PNG_COLOR_TYPE_RGB;
1162  break;
1163  case AV_PIX_FMT_RGBA:
1164  s->bit_depth = 8;
1165  s->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1166  break;
1167  case AV_PIX_FMT_RGB24:
1168  s->bit_depth = 8;
1169  s->color_type = PNG_COLOR_TYPE_RGB;
1170  break;
1171  case AV_PIX_FMT_GRAY16BE:
1172  s->bit_depth = 16;
1173  s->color_type = PNG_COLOR_TYPE_GRAY;
1174  break;
1175  case AV_PIX_FMT_GRAY8:
1176  s->bit_depth = 8;
1177  s->color_type = PNG_COLOR_TYPE_GRAY;
1178  break;
1179  case AV_PIX_FMT_GRAY8A:
1180  s->bit_depth = 8;
1181  s->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
1182  break;
1183  case AV_PIX_FMT_YA16BE:
1184  s->bit_depth = 16;
1185  s->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
1186  break;
1187  case AV_PIX_FMT_MONOBLACK:
1188  s->bit_depth = 1;
1189  s->color_type = PNG_COLOR_TYPE_GRAY;
1190  break;
1191  case AV_PIX_FMT_PAL8:
1192  s->bit_depth = 8;
1193  s->color_type = PNG_COLOR_TYPE_PALETTE;
1194  break;
1195  default:
1196  return -1;
1197  }
1198  s->bits_per_pixel = ff_png_get_nb_channels(s->color_type) * s->bit_depth;
1199 
1200  compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
1201  ? Z_DEFAULT_COMPRESSION
1202  : av_clip(avctx->compression_level, 0, 9);
1203  return ff_deflate_init(&s->zstream, compression_level, avctx);
1204 }
1205 
1207 {
1208  PNGEncContext *s = avctx->priv_data;
1209 
1210  ff_deflate_end(&s->zstream);
1211  av_frame_free(&s->last_frame);
1212  av_frame_free(&s->prev_frame);
1213  av_freep(&s->last_frame_packet);
1214  av_freep(&s->extra_data);
1215  s->extra_data_size = 0;
1216  return 0;
1217 }
1218 
1219 #define OFFSET(x) offsetof(PNGEncContext, x)
1220 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1221 static const AVOption options[] = {
1222  {"dpi", "Set image resolution (in dots per inch)", OFFSET(dpi), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 0x10000, VE},
1223  {"dpm", "Set image resolution (in dots per meter)", OFFSET(dpm), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 0x10000, VE},
1224  { "pred", "Prediction method", OFFSET(filter_type), AV_OPT_TYPE_INT, { .i64 = PNG_FILTER_VALUE_PAETH }, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED, VE, .unit = "pred" },
1225  { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_NONE }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1226  { "sub", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_SUB }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1227  { "up", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_UP }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1228  { "avg", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_AVG }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1229  { "paeth", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_PAETH }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1230  { "mixed", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_MIXED }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1231  { NULL},
1232 };
1233 
1234 static const AVClass pngenc_class = {
1235  .class_name = "(A)PNG encoder",
1236  .item_name = av_default_item_name,
1237  .option = options,
1238  .version = LIBAVUTIL_VERSION_INT,
1239 };
1240 
1242  .p.name = "png",
1243  CODEC_LONG_NAME("PNG (Portable Network Graphics) image"),
1244  .p.type = AVMEDIA_TYPE_VIDEO,
1245  .p.id = AV_CODEC_ID_PNG,
1246  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1248  .priv_data_size = sizeof(PNGEncContext),
1249  .init = png_enc_init,
1250  .close = png_enc_close,
1258  .alpha_modes = (const enum AVAlphaMode[]) {
1260  },
1261  .p.priv_class = &pngenc_class,
1262  .caps_internal = FF_CODEC_CAP_ICC_PROFILES,
1263 };
1264 
1266  .p.name = "apng",
1267  CODEC_LONG_NAME("APNG (Animated Portable Network Graphics) image"),
1268  .p.type = AVMEDIA_TYPE_VIDEO,
1269  .p.id = AV_CODEC_ID_APNG,
1270  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1272  .priv_data_size = sizeof(PNGEncContext),
1273  .init = png_enc_init,
1274  .close = png_enc_close,
1281  .alpha_modes = (const enum AVAlphaMode[]) {
1283  },
1284  .p.priv_class = &pngenc_class,
1285  .caps_internal = FF_CODEC_CAP_ICC_PROFILES,
1286 };
AVFrame::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: frame.h:682
ff_encode_reordered_opaque
int ff_encode_reordered_opaque(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame)
Propagate user opaque values from the frame to avctx/pkt as needed.
Definition: encode.c:220
AVMasteringDisplayMetadata::has_primaries
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
Definition: mastering_display_metadata.h:62
CODEC_PIXFMTS
#define CODEC_PIXFMTS(...)
Definition: codec_internal.h:391
encode_frame
static int encode_frame(AVCodecContext *avctx, const AVFrame *pict)
Definition: pngenc.c:525
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AVFrame::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:678
AVMasteringDisplayMetadata::max_luminance
AVRational max_luminance
Max luminance of mastering display (cd/m^2).
Definition: mastering_display_metadata.h:57
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
entry
#define entry
Definition: aom_film_grain_template.c:66
av_clip
#define av_clip
Definition: common.h:100
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
AVALPHA_MODE_STRAIGHT
@ AVALPHA_MODE_STRAIGHT
Alpha channel is independent of color values.
Definition: pixfmt.h:803
PNGEncContext::buf
uint8_t buf[IOBUF_SIZE]
Definition: pngenc.c:66
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:422
AVColorTransferCharacteristic
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:661
libm.h
ff_png_encoder
const FFCodec ff_png_encoder
Definition: pngenc.c:1241
av_frame_get_buffer
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:206
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:659
AVColorPrimariesDesc
Struct that contains both white point location and primaries location, providing the complete descrip...
Definition: csp.h:78
AVCRC
uint32_t AVCRC
Definition: crc.h:46
png_get_chrm
static int png_get_chrm(enum AVColorPrimaries prim, uint8_t *buf)
Definition: pngenc.c:304
AV_PKT_DATA_NEW_EXTRADATA
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:56
APNG_FCTL_CHUNK_SIZE
#define APNG_FCTL_CHUNK_SIZE
Definition: apng.h:42
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
ff_png_get_nb_channels
int ff_png_get_nb_channels(int color_type)
Definition: png.c:41
PNGEncContext::bits_per_pixel
int bits_per_pixel
Definition: pngenc.c:73
AVMasteringDisplayMetadata::display_primaries
AVRational display_primaries[3][2]
CIE 1931 xy chromaticity coords of color primaries (r, g, b order).
Definition: mastering_display_metadata.h:42
src1
const pixel * src1
Definition: h264pred_template.c:420
AVMasteringDisplayMetadata::has_luminance
int has_luminance
Flag indicating whether the luminance (min_ and max_) have been set.
Definition: mastering_display_metadata.h:67
rational.h
PNGEncContext::last_frame
AVFrame * last_frame
Definition: pngenc.c:83
int64_t
long long int64_t
Definition: coverity.c:34
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:226
AVFrame::color_primaries
enum AVColorPrimaries color_primaries
Definition: frame.h:680
mask
int mask
Definition: mediacodecdec_common.c:154
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
apng_encode_frame
static int apng_encode_frame(AVCodecContext *avctx, const AVFrame *pict, APNGFctlChunk *best_fctl_chunk, APNGFctlChunk *best_last_fctl_chunk)
Definition: pngenc.c:836
AVContentLightMetadata::MaxCLL
unsigned MaxCLL
Max content light level (cd/m^2).
Definition: mastering_display_metadata.h:111
APNGFctlChunk::delay_num
uint16_t delay_num
Definition: pngenc.c:51
test::height
int height
Definition: vc1dsp.c:40
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
AV_PIX_FMT_RGBA64BE
@ AV_PIX_FMT_RGBA64BE
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: pixfmt.h:202
AVFrame::width
int width
Definition: frame.h:499
PNG_FILTER_VALUE_MIXED
#define PNG_FILTER_VALUE_MIXED
Definition: png.h:45
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:558
AVOption
AVOption.
Definition: opt.h:429
encode.h
b
#define b
Definition: input.c:42
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:664
data
const char data[16]
Definition: mxf.c:149
png_write_row
static int png_write_row(AVCodecContext *avctx, const uint8_t *data, int size)
Definition: pngenc.c:279
FFCodec
Definition: codec_internal.h:127
output_data
static int output_data(MLPDecodeContext *m, unsigned int substr, AVFrame *frame, int *got_frame_ptr)
Write the audio data into the output buffer.
Definition: mlpdec.c:1108
PNGEncContext::dpm
int dpm
Physical pixel density, in dots per meter, if set.
Definition: pngenc.c:68
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:576
png_get_gama
static int png_get_gama(enum AVColorTransferCharacteristic trc, uint8_t *buf)
Definition: pngenc.c:322
PNGEncContext::last_frame_packet
uint8_t * last_frame_packet
Definition: pngenc.c:85
AVColorPrimaries
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:636
ff_deflate_end
void ff_deflate_end(FFZStream *zstream)
Wrapper around deflateEnd().
AV_CODEC_ID_APNG
@ AV_CODEC_ID_APNG
Definition: codec_id.h:269
FF_COMPRESSION_DEFAULT
#define FF_COMPRESSION_DEFAULT
Definition: avcodec.h:1224
APNG_DISPOSE_OP_BACKGROUND
@ APNG_DISPOSE_OP_BACKGROUND
Definition: apng.h:32
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:613
FF_INPUT_BUFFER_MIN_SIZE
#define FF_INPUT_BUFFER_MIN_SIZE
Used by some encoders as upper bound for the length of headers.
Definition: encode.h:33
AV_WB64
#define AV_WB64(p, v)
Definition: intreadwrite.h:429
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:448
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
NB_PASSES
#define NB_PASSES
Definition: png.h:47
AVContentLightMetadata
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
Definition: mastering_display_metadata.h:107
crc.h
ff_apng_encoder
const FFCodec ff_apng_encoder
Definition: pngenc.c:1265
sub_png_paeth_prediction
static void sub_png_paeth_prediction(uint8_t *dst, const uint8_t *src, const uint8_t *top, int w, int bpp)
Definition: pngenc.c:128
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:104
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:135
AV_STEREO3D_SIDEBYSIDE
@ AV_STEREO3D_SIDEBYSIDE
Views are next to each other.
Definition: stereo3d.h:64
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
PNGEncContext::prev_frame
AVFrame * prev_frame
Definition: pngenc.c:82
AVCOL_TRC_IEC61966_2_1
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:675
ff_png_pass_row_size
int ff_png_pass_row_size(int pass, int bits_per_pixel, int width)
Definition: png.c:54
fail
#define fail()
Definition: checkasm.h:200
AV_STEREO3D_2D
@ AV_STEREO3D_2D
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:52
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:488
APNGFctlChunk::blend_op
uint8_t blend_op
Definition: pngenc.c:52
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:358
AVRational::num
int num
Numerator.
Definition: rational.h:59
encode_png
static int encode_png(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: pngenc.c:647
PNG_COLOR_TYPE_RGB_ALPHA
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:36
AV_CODEC_FLAG_INTERLACED_DCT
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:310
png_filter_row
static void png_filter_row(PNGEncContext *c, uint8_t *dst, int filter_type, const uint8_t *src, const uint8_t *top, int size, int bpp)
Definition: pngenc.c:172
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:52
avassert.h
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
zlib_wrapper.h
AVFrameSideData::size
size_t size
Definition: frame.h:285
av_cold
#define av_cold
Definition: attributes.h:90
encode_apng
static int encode_apng(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: pngenc.c:965
av_dict_get
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:60
PNGEncContext::bytestream_end
uint8_t * bytestream_end
Definition: pngenc.c:61
stereo3d.h
AVMasteringDisplayMetadata::white_point
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
Definition: mastering_display_metadata.h:47
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_csp_primaries_desc_from_id
const AVColorPrimariesDesc * av_csp_primaries_desc_from_id(enum AVColorPrimaries prm)
Retrieves a complete gamut description from an enum constant describing the color primaries.
Definition: csp.c:90
png_write_chunk
static void png_write_chunk(uint8_t **f, uint32_t tag, const uint8_t *buf, int length)
Definition: pngenc.c:231
APNG_BLEND_OP_SOURCE
@ APNG_BLEND_OP_SOURCE
Definition: apng.h:37
PNG_COLOR_TYPE_RGB
#define PNG_COLOR_TYPE_RGB
Definition: png.h:35
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:144
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1553
PNG_Q2D
#define PNG_Q2D(q, divisor)
Definition: pngenc.c:302
png_write_image_data
static void png_write_image_data(AVCodecContext *avctx, const uint8_t *buf, int length)
Definition: pngenc.c:251
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:331
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:441
AVStereo3D::flags
int flags
Additional information about the frame packing.
Definition: stereo3d.h:212
AV_CODEC_ID_PNG
@ AV_CODEC_ID_PNG
Definition: codec_id.h:113
if
if(ret)
Definition: filter_design.txt:179
PNGEncContext
Definition: pngenc.c:55
APNGFctlChunk::y_offset
uint32_t y_offset
Definition: pngenc.c:50
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:95
AV_PIX_FMT_GRAY8A
@ AV_PIX_FMT_GRAY8A
alias for AV_PIX_FMT_YA8
Definition: pixfmt.h:143
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
APNGFctlChunk::delay_den
uint16_t delay_den
Definition: pngenc.c:51
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
exif_internal.h
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
AV_EXIF_TIFF_HEADER
@ AV_EXIF_TIFF_HEADER
The TIFF header starts with 0x49492a00, or 0x4d4d002a.
Definition: exif.h:63
apng.h
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:401
IOBUF_SIZE
#define IOBUF_SIZE
Definition: pngenc.c:45
AV_PIX_FMT_MONOBLACK
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:83
AVCOL_PRI_BT709
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition: pixfmt.h:638
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:241
apng_do_inverse_blend
static int apng_do_inverse_blend(AVFrame *output, const AVFrame *input, APNGFctlChunk *fctl_chunk, uint8_t bpp)
Definition: pngenc.c:693
APNGFctlChunk::width
uint32_t width
Definition: pngenc.c:49
png_enc_close
static av_cold int png_enc_close(AVCodecContext *avctx)
Definition: pngenc.c:1206
AV_FRAME_DATA_ICC_PROFILE
@ AV_FRAME_DATA_ICC_PROFILE
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
Definition: frame.h:144
APNG_DISPOSE_OP_PREVIOUS
@ APNG_DISPOSE_OP_PREVIOUS
Definition: apng.h:33
PNG_COLOR_TYPE_GRAY
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:33
options
Definition: swscale.c:43
deflate
static void deflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
Definition: vf_neighbor.c:161
PNGEncContext::filter_type
int filter_type
Definition: pngenc.c:63
AV_FRAME_DATA_MASTERING_DISPLAY_METADATA
@ AV_FRAME_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata associated with a video frame.
Definition: frame.h:120
abs
#define abs(x)
Definition: cuda_runtime.h:35
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
PNGEncContext::extra_data_updated
int extra_data_updated
Definition: pngenc.c:78
APNGFctlChunk
Definition: pngenc.c:47
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
ff_png_pass_ymask
const uint8_t ff_png_pass_ymask[NB_PASSES]
Definition: png.c:27
ff_llvidencdsp_init
av_cold void ff_llvidencdsp_init(LLVidEncDSPContext *c)
Definition: lossless_videoencdsp.c:100
add_icc_profile_size
static int add_icc_profile_size(AVCodecContext *avctx, const AVFrame *pict, uint64_t *max_packet_size)
Definition: pngenc.c:619
APNGFctlChunk::sequence_number
uint32_t sequence_number
Definition: pngenc.c:48
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:415
PNGEncContext::zstream
FFZStream zstream
Definition: pngenc.c:65
AVAlphaMode
AVAlphaMode
Correlation between the alpha channel and color values.
Definition: pixfmt.h:800
test::width
int width
Definition: vc1dsp.c:39
PNG_FILTER_VALUE_NONE
#define PNG_FILTER_VALUE_NONE
Definition: png.h:40
f
f
Definition: af_crystalizer.c:122
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:75
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:559
codec_internal.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
av_frame_copy
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:711
av_bswap32
#define av_bswap32
Definition: bswap.h:47
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:122
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:424
AV_PIX_FMT_YA16BE
@ AV_PIX_FMT_YA16BE
16 bits gray, 16 bits alpha (big-endian)
Definition: pixfmt.h:209
PNGEncContext::last_frame_packet_size
size_t last_frame_packet_size
Definition: pngenc.c:86
PNG_FILTER_VALUE_AVG
#define PNG_FILTER_VALUE_AVG
Definition: png.h:43
size
int size
Definition: twinvq_data.h:10344
av_csp_approximate_trc_gamma
double av_csp_approximate_trc_gamma(enum AVColorTransferCharacteristic trc)
Determine a suitable 'gamma' value to match the supplied AVColorTransferCharacteristic.
Definition: csp.c:149
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
PNGEncContext::llvidencdsp
LLVidEncDSPContext llvidencdsp
Definition: pngenc.c:57
APNG_DISPOSE_OP_NONE
@ APNG_DISPOSE_OP_NONE
Definition: apng.h:31
AVFrameSideData::data
uint8_t * data
Definition: frame.h:284
PNG_FILTER_VALUE_PAETH
#define PNG_FILTER_VALUE_PAETH
Definition: png.h:44
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:514
PNGEncContext::extra_data
uint8_t * extra_data
Definition: pngenc.c:79
png_choose_filter
static uint8_t * png_choose_filter(PNGEncContext *s, uint8_t *dst, const uint8_t *src, const uint8_t *top, int size, int bpp)
Definition: pngenc.c:201
buffer.h
PNG_FILTER_VALUE_UP
#define PNG_FILTER_VALUE_UP
Definition: png.h:42
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
csp.h
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
OFFSET
#define OFFSET(x)
Definition: pngenc.c:1219
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:564
AV_STEREO3D_FLAG_INVERT
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:194
input
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 input
Definition: filter_design.txt:172
PNGSIG
#define PNGSIG
Definition: png.h:49
AVBufferRef::size
size_t size
Size of data in bytes.
Definition: buffer.h:94
lossless_videoencdsp.h
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1546
PNG_FILTER_VALUE_SUB
#define PNG_FILTER_VALUE_SUB
Definition: png.h:41
AV_FRAME_DATA_CONTENT_LIGHT_LEVEL
@ AV_FRAME_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
Definition: frame.h:137
AV_PIX_FMT_RGB48BE
@ AV_PIX_FMT_RGB48BE
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:109
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:551
options
static const AVOption options[]
Definition: pngenc.c:1221
src2
const pixel * src2
Definition: h264pred_template.c:421
AV_FRAME_DATA_STEREO3D
@ AV_FRAME_DATA_STEREO3D
Stereoscopic 3d metadata.
Definition: frame.h:64
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
AVMasteringDisplayMetadata
Mastering display metadata capable of representing the color volume of the display used to master the...
Definition: mastering_display_metadata.h:38
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::height
int height
Definition: avcodec.h:592
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:631
LLVidEncDSPContext
Definition: lossless_videoencdsp.h:25
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:750
FF_CODEC_CAP_ICC_PROFILES
#define FF_CODEC_CAP_ICC_PROFILES
Codec supports embedded ICC profiles (AV_FRAME_DATA_ICC_PROFILE).
Definition: codec_internal.h:81
sub_left_prediction
static void sub_left_prediction(PNGEncContext *c, uint8_t *dst, const uint8_t *src, int bpp, int size)
Definition: pngenc.c:156
PNGEncContext::color_type
int color_type
Definition: pngenc.c:72
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:1878
bound
static double bound(const double threshold, const double val)
Definition: af_dynaudnorm.c:413
tag
uint32_t tag
Definition: movenc.c:1957
ret
ret
Definition: filter_design.txt:187
pred
static const float pred[4]
Definition: siprdata.h:259
PNGEncContext::extra_data_size
int extra_data_size
Definition: pngenc.c:80
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
AVALPHA_MODE_UNSPECIFIED
@ AVALPHA_MODE_UNSPECIFIED
Unknown alpha handling, or no alpha channel.
Definition: pixfmt.h:801
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:81
AVStereo3D::type
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:207
PNGEncContext::bit_depth
int bit_depth
Definition: pngenc.c:71
PNG_LRINT
#define PNG_LRINT(d, divisor)
Definition: pngenc.c:301
PNGEncContext::bytestream_start
uint8_t * bytestream_start
Definition: pngenc.c:60
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
U
#define U(x)
Definition: vpx_arith.h:37
av_frame_replace
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition: frame.c:376
AVCodecContext
main external API structure.
Definition: avcodec.h:431
AVFrame::height
int height
Definition: frame.h:499
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition: packet.c:232
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:106
av_crc
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:392
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
png_get_interlaced_row
static void png_get_interlaced_row(uint8_t *dst, int row_size, int bits_per_pixel, int pass, const uint8_t *src, int width)
Definition: pngenc.c:89
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
PNG_COLOR_MASK_PALETTE
#define PNG_COLOR_MASK_PALETTE
Definition: png.h:29
AVMasteringDisplayMetadata::min_luminance
AVRational min_luminance
Min luminance of mastering display (cd/m^2).
Definition: mastering_display_metadata.h:52
AV_WB32_PNG_D
#define AV_WB32_PNG_D(buf, q)
Definition: pngenc.c:303
AV_CRC_32_IEEE_LE
@ AV_CRC_32_IEEE_LE
Definition: crc.h:53
PNGEncContext::last_frame_fctl
APNGFctlChunk last_frame_fctl
Definition: pngenc.c:84
desc
const char * desc
Definition: libsvtav1.c:79
PNGEncContext::dpi
int dpi
Physical pixel density, in dots per inch, if set.
Definition: pngenc.c:67
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
FFZStream
Definition: zlib_wrapper.h:27
mem.h
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
mastering_display_metadata.h
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:282
png_enc_init
static av_cold int png_enc_init(AVCodecContext *avctx)
Definition: pngenc.c:1119
AVDictionaryEntry
Definition: dict.h:90
png_write_iccp
static int png_write_iccp(PNGEncContext *s, const AVFrameSideData *sd)
Definition: pngenc.c:332
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:535
AVContentLightMetadata::MaxFALL
unsigned MaxFALL
Max average light level per frame (cd/m^2).
Definition: mastering_display_metadata.h:116
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
png.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
ff_exif_get_buffer
int ff_exif_get_buffer(void *logctx, const AVFrame *frame, AVBufferRef **buffer_ptr, enum AVExifHeaderMode header_mode)
Gets all relevant side data, collects it into an IFD, and writes it into the corresponding buffer poi...
Definition: exif.c:1355
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:592
bytestream.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:472
PNG_COLOR_TYPE_GRAY_ALPHA
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition: png.h:37
AVFrameSideData::metadata
AVDictionary * metadata
Definition: frame.h:286
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
APNGFctlChunk::height
uint32_t height
Definition: pngenc.c:49
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
AVStereo3D
Stereo 3D type: this structure describes how two videos are packed within a single video surface,...
Definition: stereo3d.h:203
width
#define width
Definition: dsp.h:89
input_data
static void input_data(MLPEncodeContext *ctx, MLPSubstream *s, uint8_t **const samples, int nb_samples)
Wrapper function for inputting data in two different bit-depths.
Definition: mlpenc.c:1224
PNGEncContext::bytestream
uint8_t * bytestream
Definition: pngenc.c:59
PNGEncContext::is_progressive
int is_progressive
Definition: pngenc.c:70
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
VE
#define VE
Definition: pngenc.c:1220
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:62
encode_headers
static int encode_headers(AVCodecContext *avctx, const AVFrame *pict)
Definition: pngenc.c:374
APNGFctlChunk::dispose_op
uint8_t dispose_op
Definition: pngenc.c:52
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:616
PNGEncContext::palette_checksum
uint32_t palette_checksum
Definition: pngenc.c:76
PNG_COLOR_TYPE_PALETTE
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:34
src
#define src
Definition: vp8dsp.c:248
APNGFctlChunk::x_offset
uint32_t x_offset
Definition: pngenc.c:50
ff_deflate_init
int ff_deflate_init(FFZStream *zstream, int level, void *logctx)
Wrapper around deflateInit().
PNGEncContext::sequence_number
uint32_t sequence_number
Definition: pngenc.c:77
AVCodecContext::compression_level
int compression_level
Definition: avcodec.h:1223
pngenc_class
static const AVClass pngenc_class
Definition: pngenc.c:1234