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