FFmpeg
pngdec.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 //#define DEBUG
23 
24 #include "config_components.h"
25 
26 #include "libavutil/avassert.h"
27 #include "libavutil/bprint.h"
28 #include "libavutil/crc.h"
29 #include "libavutil/csp.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/intreadwrite.h"
33 #include "libavutil/pixfmt.h"
34 #include "libavutil/rational.h"
35 #include "libavutil/stereo3d.h"
36 
37 #include "avcodec.h"
38 #include "bytestream.h"
39 #include "codec_internal.h"
40 #include "decode.h"
41 #include "apng.h"
42 #include "png.h"
43 #include "pngdsp.h"
44 #include "thread.h"
45 #include "threadframe.h"
46 #include "zlib_wrapper.h"
47 
48 #include <zlib.h>
49 
51  PNG_IHDR = 1 << 0,
52  PNG_PLTE = 1 << 1,
53 };
54 
56  PNG_IDAT = 1 << 0,
57  PNG_ALLIMAGE = 1 << 1,
58 };
59 
60 typedef struct PNGDecContext {
63 
67 
69 
70  uint8_t iccp_name[82];
71  uint8_t *iccp_data;
72  size_t iccp_data_len;
73 
75 
76  int have_chrm;
77  uint32_t white_point[2];
78  uint32_t display_primaries[3][2];
79  int gamma;
80  int have_srgb;
81  int have_cicp;
85  int have_clli;
86  uint32_t clli_max;
87  uint32_t clli_avg;
88  int have_mdvc;
89  uint16_t mdvc_primaries[3][2];
90  uint16_t mdvc_white_point[2];
91  uint32_t mdvc_max_lum;
92  uint32_t mdvc_min_lum;
93 
96  int width, height;
97  int cur_w, cur_h;
99  uint8_t dispose_op, blend_op;
105  int channels;
107  int bpp;
108  int has_trns;
111 
112  uint32_t palette[256];
113  uint8_t *crow_buf;
114  uint8_t *last_row;
115  unsigned int last_row_size;
116  uint8_t *tmp_row;
117  unsigned int tmp_row_size;
118  uint8_t *buffer;
120  int pass;
121  int crow_size; /* compressed row size (include filter type) */
122  int row_size; /* decompressed row size */
123  int pass_row_size; /* decompress row size of the current pass */
124  int y;
126 } PNGDecContext;
127 
128 /* Mask to determine which pixels are valid in a pass */
129 static const uint8_t png_pass_mask[NB_PASSES] = {
130  0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
131 };
132 
133 /* Mask to determine which y pixels can be written in a pass */
134 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
135  0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
136 };
137 
138 /* Mask to determine which pixels to overwrite while displaying */
139 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
140  0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
141 };
142 
143 /* NOTE: we try to construct a good looking image at each pass. width
144  * is the original image width. We also do pixel format conversion at
145  * this stage */
146 static void png_put_interlaced_row(uint8_t *dst, int width,
147  int bits_per_pixel, int pass,
148  int color_type, const uint8_t *src)
149 {
150  int x, mask, dsp_mask, j, src_x, b, bpp;
151  uint8_t *d;
152  const uint8_t *s;
153 
154  mask = png_pass_mask[pass];
155  dsp_mask = png_pass_dsp_mask[pass];
156 
157  switch (bits_per_pixel) {
158  case 1:
159  src_x = 0;
160  for (x = 0; x < width; x++) {
161  j = (x & 7);
162  if ((dsp_mask << j) & 0x80) {
163  b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
164  dst[x >> 3] &= 0xFF7F>>j;
165  dst[x >> 3] |= b << (7 - j);
166  }
167  if ((mask << j) & 0x80)
168  src_x++;
169  }
170  break;
171  case 2:
172  src_x = 0;
173  for (x = 0; x < width; x++) {
174  int j2 = 2 * (x & 3);
175  j = (x & 7);
176  if ((dsp_mask << j) & 0x80) {
177  b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
178  dst[x >> 2] &= 0xFF3F>>j2;
179  dst[x >> 2] |= b << (6 - j2);
180  }
181  if ((mask << j) & 0x80)
182  src_x++;
183  }
184  break;
185  case 4:
186  src_x = 0;
187  for (x = 0; x < width; x++) {
188  int j2 = 4*(x&1);
189  j = (x & 7);
190  if ((dsp_mask << j) & 0x80) {
191  b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
192  dst[x >> 1] &= 0xFF0F>>j2;
193  dst[x >> 1] |= b << (4 - j2);
194  }
195  if ((mask << j) & 0x80)
196  src_x++;
197  }
198  break;
199  default:
200  bpp = bits_per_pixel >> 3;
201  d = dst;
202  s = src;
203  for (x = 0; x < width; x++) {
204  j = x & 7;
205  if ((dsp_mask << j) & 0x80) {
206  memcpy(d, s, bpp);
207  }
208  d += bpp;
209  if ((mask << j) & 0x80)
210  s += bpp;
211  }
212  break;
213  }
214 }
215 
216 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
217  int w, int bpp)
218 {
219  int i;
220  for (i = 0; i < w; i++) {
221  int a, b, c, p, pa, pb, pc;
222 
223  a = dst[i - bpp];
224  b = top[i];
225  c = top[i - bpp];
226 
227  p = b - c;
228  pc = a - c;
229 
230  pa = abs(p);
231  pb = abs(pc);
232  pc = abs(p + pc);
233 
234  if (pa <= pb && pa <= pc)
235  p = a;
236  else if (pb <= pc)
237  p = b;
238  else
239  p = c;
240  dst[i] = p + src[i];
241  }
242 }
243 
244 #define UNROLL1(bpp, op) \
245  { \
246  r = dst[0]; \
247  if (bpp >= 2) \
248  g = dst[1]; \
249  if (bpp >= 3) \
250  b = dst[2]; \
251  if (bpp >= 4) \
252  a = dst[3]; \
253  for (; i <= size - bpp; i += bpp) { \
254  dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
255  if (bpp == 1) \
256  continue; \
257  dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
258  if (bpp == 2) \
259  continue; \
260  dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
261  if (bpp == 3) \
262  continue; \
263  dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
264  } \
265  }
266 
267 #define UNROLL_FILTER(op) \
268  if (bpp == 1) { \
269  UNROLL1(1, op) \
270  } else if (bpp == 2) { \
271  UNROLL1(2, op) \
272  } else if (bpp == 3) { \
273  UNROLL1(3, op) \
274  } else if (bpp == 4) { \
275  UNROLL1(4, op) \
276  } \
277  for (; i < size; i++) { \
278  dst[i] = op(dst[i - bpp], src[i], last[i]); \
279  }
280 
281 /* NOTE: 'dst' can be equal to 'last' */
282 void ff_png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
283  uint8_t *src, uint8_t *last, int size, int bpp)
284 {
285  int i, p, r, g, b, a;
286 
287  switch (filter_type) {
289  memcpy(dst, src, size);
290  break;
292  for (i = 0; i < bpp; i++)
293  dst[i] = src[i];
294  if (bpp == 4) {
295  p = *(int *)dst;
296  for (; i < size; i += bpp) {
297  unsigned s = *(int *)(src + i);
298  p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
299  *(int *)(dst + i) = p;
300  }
301  } else {
302 #define OP_SUB(x, s, l) ((x) + (s))
304  }
305  break;
306  case PNG_FILTER_VALUE_UP:
307  dsp->add_bytes_l2(dst, src, last, size);
308  break;
310  for (i = 0; i < bpp; i++) {
311  p = (last[i] >> 1);
312  dst[i] = p + src[i];
313  }
314 #define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
316  break;
318  for (i = 0; i < bpp; i++) {
319  p = last[i];
320  dst[i] = p + src[i];
321  }
322  if (bpp > 2 && size > 4) {
323  /* would write off the end of the array if we let it process
324  * the last pixel with bpp=3 */
325  int w = (bpp & 3) ? size - 3 : size;
326 
327  if (w > i) {
328  dsp->add_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
329  i = w;
330  }
331  }
332  ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
333  break;
334  }
335 }
336 
337 /* This used to be called "deloco" in FFmpeg
338  * and is actually an inverse reversible colorspace transformation */
339 #define YUV2RGB(NAME, TYPE) \
340 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
341 { \
342  int i; \
343  for (i = 0; i < size - 2; i += 3 + alpha) { \
344  int g = dst [i + 1]; \
345  dst[i + 0] += g; \
346  dst[i + 2] += g; \
347  } \
348 }
349 
350 YUV2RGB(rgb8, uint8_t)
351 YUV2RGB(rgb16, uint16_t)
352 
354 {
355  if (s->interlace_type) {
356  return 100 - 100 * s->pass / (NB_PASSES - 1);
357  } else {
358  return 100 - 100 * s->y / s->cur_h;
359  }
360 }
361 
362 /* process exactly one decompressed row */
363 static void png_handle_row(PNGDecContext *s, uint8_t *dst, ptrdiff_t dst_stride)
364 {
365  uint8_t *ptr, *last_row;
366  int got_line;
367 
368  if (!s->interlace_type) {
369  ptr = dst + dst_stride * (s->y + s->y_offset) + s->x_offset * s->bpp;
370  if (s->y == 0)
371  last_row = s->last_row;
372  else
373  last_row = ptr - dst_stride;
374 
375  ff_png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
376  last_row, s->row_size, s->bpp);
377  /* loco lags by 1 row so that it doesn't interfere with top prediction */
378  if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
379  if (s->bit_depth == 16) {
380  deloco_rgb16((uint16_t *)(ptr - dst_stride), s->row_size / 2,
381  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
382  } else {
383  deloco_rgb8(ptr - dst_stride, s->row_size,
384  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
385  }
386  }
387  s->y++;
388  if (s->y == s->cur_h) {
389  s->pic_state |= PNG_ALLIMAGE;
390  if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
391  if (s->bit_depth == 16) {
392  deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
393  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
394  } else {
395  deloco_rgb8(ptr, s->row_size,
396  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
397  }
398  }
399  }
400  } else {
401  got_line = 0;
402  for (;;) {
403  ptr = dst + dst_stride * (s->y + s->y_offset) + s->x_offset * s->bpp;
404  if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
405  /* if we already read one row, it is time to stop to
406  * wait for the next one */
407  if (got_line)
408  break;
409  ff_png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
410  s->last_row, s->pass_row_size, s->bpp);
411  FFSWAP(uint8_t *, s->last_row, s->tmp_row);
412  FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size);
413  got_line = 1;
414  }
415  if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
416  png_put_interlaced_row(ptr, s->cur_w, s->bits_per_pixel, s->pass,
417  s->color_type, s->last_row);
418  }
419  s->y++;
420  if (s->y == s->cur_h) {
421  memset(s->last_row, 0, s->row_size);
422  for (;;) {
423  if (s->pass == NB_PASSES - 1) {
424  s->pic_state |= PNG_ALLIMAGE;
425  goto the_end;
426  } else {
427  s->pass++;
428  s->y = 0;
429  s->pass_row_size = ff_png_pass_row_size(s->pass,
430  s->bits_per_pixel,
431  s->cur_w);
432  s->crow_size = s->pass_row_size + 1;
433  if (s->pass_row_size != 0)
434  break;
435  /* skip pass if empty row */
436  }
437  }
438  }
439  }
440 the_end:;
441  }
442 }
443 
445  uint8_t *dst, ptrdiff_t dst_stride)
446 {
447  z_stream *const zstream = &s->zstream.zstream;
448  int ret;
449  zstream->avail_in = bytestream2_get_bytes_left(gb);
450  zstream->next_in = gb->buffer;
451 
452  /* decode one line if possible */
453  while (zstream->avail_in > 0) {
454  ret = inflate(zstream, Z_PARTIAL_FLUSH);
455  if (ret != Z_OK && ret != Z_STREAM_END) {
456  av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
457  return AVERROR_EXTERNAL;
458  }
459  if (zstream->avail_out == 0) {
460  if (!(s->pic_state & PNG_ALLIMAGE)) {
461  png_handle_row(s, dst, dst_stride);
462  }
463  zstream->avail_out = s->crow_size;
464  zstream->next_out = s->crow_buf;
465  }
466  if (ret == Z_STREAM_END && zstream->avail_in > 0) {
467  av_log(s->avctx, AV_LOG_WARNING,
468  "%d undecompressed bytes left in buffer\n", zstream->avail_in);
469  return 0;
470  }
471  }
472  return 0;
473 }
474 
475 static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
476  const uint8_t *data_end, void *logctx)
477 {
478  FFZStream z;
479  z_stream *const zstream = &z.zstream;
480  unsigned char *buf;
481  unsigned buf_size;
482  int ret = ff_inflate_init(&z, logctx);
483  if (ret < 0)
484  return ret;
485 
486  zstream->next_in = data;
487  zstream->avail_in = data_end - data;
489 
490  while (zstream->avail_in > 0) {
491  av_bprint_get_buffer(bp, 2, &buf, &buf_size);
492  if (buf_size < 2) {
493  ret = AVERROR(ENOMEM);
494  goto fail;
495  }
496  zstream->next_out = buf;
497  zstream->avail_out = buf_size - 1;
498  ret = inflate(zstream, Z_PARTIAL_FLUSH);
499  if (ret != Z_OK && ret != Z_STREAM_END) {
501  goto fail;
502  }
503  bp->len += zstream->next_out - buf;
504  if (ret == Z_STREAM_END)
505  break;
506  }
507  ff_inflate_end(&z);
508  bp->str[bp->len] = 0;
509  return 0;
510 
511 fail:
512  ff_inflate_end(&z);
514  return ret;
515 }
516 
517 static char *iso88591_to_utf8(const char *in, size_t size_in)
518 {
519  size_t extra = 0, i;
520  char *out, *q;
521 
522  for (i = 0; i < size_in; i++)
523  extra += !!(in[i] & 0x80);
524  if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
525  return NULL;
526  q = out = av_malloc(size_in + extra + 1);
527  if (!out)
528  return NULL;
529  for (i = 0; i < size_in; i++) {
530  if (in[i] & 0x80) {
531  *(q++) = 0xC0 | (in[i] >> 6);
532  *(q++) = 0x80 | (in[i] & 0x3F);
533  } else {
534  *(q++) = in[i];
535  }
536  }
537  *(q++) = 0;
538  return out;
539 }
540 
541 static int decode_text_chunk(PNGDecContext *s, GetByteContext *gb, int compressed)
542 {
543  int ret, method;
544  const uint8_t *data = gb->buffer;
545  const uint8_t *data_end = gb->buffer_end;
546  const char *keyword = data;
547  const char *keyword_end = memchr(keyword, 0, data_end - data);
548  char *kw_utf8 = NULL, *txt_utf8 = NULL;
549  const char *text;
550  unsigned text_len;
551  AVBPrint bp;
552 
553  if (!keyword_end)
554  return AVERROR_INVALIDDATA;
555  data = keyword_end + 1;
556 
557  if (compressed) {
558  if (data == data_end)
559  return AVERROR_INVALIDDATA;
560  method = *(data++);
561  if (method)
562  return AVERROR_INVALIDDATA;
563  if ((ret = decode_zbuf(&bp, data, data_end, s->avctx)) < 0)
564  return ret;
565  text = bp.str;
566  text_len = bp.len;
567  } else {
568  text = data;
569  text_len = data_end - data;
570  }
571 
572  txt_utf8 = iso88591_to_utf8(text, text_len);
573  if (compressed)
574  av_bprint_finalize(&bp, NULL);
575  if (!txt_utf8)
576  return AVERROR(ENOMEM);
577  kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
578  if (!kw_utf8) {
579  av_free(txt_utf8);
580  return AVERROR(ENOMEM);
581  }
582 
583  av_dict_set(&s->frame_metadata, kw_utf8, txt_utf8,
585  return 0;
586 }
587 
589  GetByteContext *gb)
590 {
591  if (bytestream2_get_bytes_left(gb) != 13)
592  return AVERROR_INVALIDDATA;
593 
594  if (s->pic_state & PNG_IDAT) {
595  av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");
596  return AVERROR_INVALIDDATA;
597  }
598 
599  if (s->hdr_state & PNG_IHDR) {
600  av_log(avctx, AV_LOG_ERROR, "Multiple IHDR\n");
601  return AVERROR_INVALIDDATA;
602  }
603 
604  s->width = s->cur_w = bytestream2_get_be32(gb);
605  s->height = s->cur_h = bytestream2_get_be32(gb);
606  if (av_image_check_size(s->width, s->height, 0, avctx)) {
607  s->cur_w = s->cur_h = s->width = s->height = 0;
608  av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
609  return AVERROR_INVALIDDATA;
610  }
611  s->bit_depth = bytestream2_get_byte(gb);
612  if (s->bit_depth != 1 && s->bit_depth != 2 && s->bit_depth != 4 &&
613  s->bit_depth != 8 && s->bit_depth != 16) {
614  av_log(avctx, AV_LOG_ERROR, "Invalid bit depth\n");
615  goto error;
616  }
617  s->color_type = bytestream2_get_byte(gb);
618  s->compression_type = bytestream2_get_byte(gb);
619  if (s->compression_type) {
620  av_log(avctx, AV_LOG_ERROR, "Invalid compression method %d\n", s->compression_type);
621  goto error;
622  }
623  s->filter_type = bytestream2_get_byte(gb);
624  s->interlace_type = bytestream2_get_byte(gb);
625  s->hdr_state |= PNG_IHDR;
626  if (avctx->debug & FF_DEBUG_PICT_INFO)
627  av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
628  "compression_type=%d filter_type=%d interlace_type=%d\n",
629  s->width, s->height, s->bit_depth, s->color_type,
630  s->compression_type, s->filter_type, s->interlace_type);
631 
632  return 0;
633 error:
634  s->cur_w = s->cur_h = s->width = s->height = 0;
635  s->bit_depth = 8;
636  return AVERROR_INVALIDDATA;
637 }
638 
640  GetByteContext *gb)
641 {
642  if (s->pic_state & PNG_IDAT) {
643  av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
644  return AVERROR_INVALIDDATA;
645  }
646  avctx->sample_aspect_ratio.num = bytestream2_get_be32(gb);
647  avctx->sample_aspect_ratio.den = bytestream2_get_be32(gb);
648  if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
649  avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
650  bytestream2_skip(gb, 1); /* unit specifier */
651 
652  return 0;
653 }
654 
655 /*
656  * This populates AVCodecContext fields so it must be called before
657  * ff_thread_finish_setup() to avoid a race condition with respect to the
658  * generic copying of avctx fields.
659  */
661 {
662  PNGDecContext *s = avctx->priv_data;
663  int ret;
664 
665  if (s->have_cicp) {
666  if (s->cicp_primaries >= AVCOL_PRI_NB)
667  av_log(avctx, AV_LOG_WARNING, "unrecognized cICP primaries\n");
668  else
669  avctx->color_primaries = frame->color_primaries = s->cicp_primaries;
670  if (s->cicp_trc >= AVCOL_TRC_NB)
671  av_log(avctx, AV_LOG_WARNING, "unrecognized cICP transfer\n");
672  else
673  avctx->color_trc = frame->color_trc = s->cicp_trc;
674  if (s->cicp_range == 0) {
675  av_log(avctx, AV_LOG_WARNING, "tv-range cICP tag found. Colors may be wrong\n");
677  } else if (s->cicp_range != 1) {
678  /* we already printed a warning when parsing the cICP chunk */
680  }
681  } else if (s->iccp_data) {
682  AVFrameSideData *sd;
684  s->iccp_data_len, &sd);
685  if (ret < 0)
686  return ret;
687  if (sd) {
688  memcpy(sd->data, s->iccp_data, s->iccp_data_len);
689  av_dict_set(&sd->metadata, "name", s->iccp_name, 0);
690  }
691  } else if (s->have_srgb) {
694  } else if (s->have_chrm) {
696  enum AVColorPrimaries prim;
697  desc.wp.x = av_make_q(s->white_point[0], 100000);
698  desc.wp.y = av_make_q(s->white_point[1], 100000);
699  desc.prim.r.x = av_make_q(s->display_primaries[0][0], 100000);
700  desc.prim.r.y = av_make_q(s->display_primaries[0][1], 100000);
701  desc.prim.g.x = av_make_q(s->display_primaries[1][0], 100000);
702  desc.prim.g.y = av_make_q(s->display_primaries[1][1], 100000);
703  desc.prim.b.x = av_make_q(s->display_primaries[2][0], 100000);
704  desc.prim.b.y = av_make_q(s->display_primaries[2][1], 100000);
706  if (prim != AVCOL_PRI_UNSPECIFIED)
707  avctx->color_primaries = frame->color_primaries = prim;
708  else
709  av_log(avctx, AV_LOG_WARNING, "unknown cHRM primaries\n");
710  }
711 
712  /* these chunks override gAMA */
713  if (s->iccp_data || s->have_srgb || s->have_cicp) {
714  av_dict_set(&s->frame_metadata, "gamma", NULL, 0);
715  } else if (s->gamma) {
716  /*
717  * These values are 100000/2.2, 100000/2.8, 100000/2.6, and
718  * 100000/1.0 respectively. 45455, 35714, and 38462, and 100000.
719  * There's a 0.001 gamma tolerance here in case of floating
720  * point issues when the PNG was written.
721  *
722  * None of the other enums have a pure gamma curve so it makes
723  * sense to leave those to sRGB and cICP.
724  */
725  if (s->gamma > 45355 && s->gamma < 45555)
727  else if (s->gamma > 35614 && s->gamma < 35814)
729  else if (s->gamma > 38362 && s->gamma < 38562)
731  else if (s->gamma > 99900 && s->gamma < 100100)
733  }
734 
735  /* PNG only supports RGB */
737  if (!s->have_cicp || s->cicp_range == 1)
739 
740  /*
741  * tRNS sets alpha depth to full, so we ignore sBIT if set.
742  * As a result we must wait until now to set
743  * avctx->bits_per_raw_sample in case tRNS appears after sBIT
744  */
745  if (!s->has_trns && s->significant_bits > 0)
746  avctx->bits_per_raw_sample = s->significant_bits;
747 
748  if (s->have_clli) {
750 
751  ret = ff_decode_content_light_new(avctx, frame, &clli);
752  if (ret < 0)
753  return ret;
754 
755  if (clli) {
756  /*
757  * 0.0001 divisor value
758  * see: https://www.w3.org/TR/png-3/#cLLi-chunk
759  */
760  clli->MaxCLL = s->clli_max / 10000;
761  clli->MaxFALL = s->clli_avg / 10000;
762  }
763  }
764 
765  if (s->have_mdvc) {
767 
768  ret = ff_decode_mastering_display_new(avctx, frame, &mdvc);
769  if (ret < 0)
770  return ret;
771 
772  if (mdvc) {
773  mdvc->has_primaries = 1;
774  for (int i = 0; i < 3; i++) {
775  mdvc->display_primaries[i][0] = av_make_q(s->mdvc_primaries[i][0], 50000);
776  mdvc->display_primaries[i][1] = av_make_q(s->mdvc_primaries[i][1], 50000);
777  }
778  mdvc->white_point[0] = av_make_q(s->mdvc_white_point[0], 50000);
779  mdvc->white_point[1] = av_make_q(s->mdvc_white_point[1], 50000);
780  mdvc->has_luminance = 1;
781  mdvc->max_luminance = av_make_q(s->mdvc_max_lum, 10000);
782  mdvc->min_luminance = av_make_q(s->mdvc_min_lum, 10000);
783  }
784  }
785 
786  return 0;
787 }
788 
790  GetByteContext *gb, AVFrame *p)
791 {
792  int ret;
793  size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
794 
795  if (!p)
796  return AVERROR_INVALIDDATA;
797  if (!(s->hdr_state & PNG_IHDR)) {
798  av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
799  return AVERROR_INVALIDDATA;
800  }
801  if (!(s->pic_state & PNG_IDAT)) {
802  /* init image info */
803  ret = ff_set_dimensions(avctx, s->width, s->height);
804  if (ret < 0)
805  return ret;
806 
807  s->channels = ff_png_get_nb_channels(s->color_type);
808  s->bits_per_pixel = s->bit_depth * s->channels;
809  s->bpp = (s->bits_per_pixel + 7) >> 3;
810  s->row_size = (s->cur_w * s->bits_per_pixel + 7) >> 3;
811 
812  if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
813  s->color_type == PNG_COLOR_TYPE_RGB) {
814  avctx->pix_fmt = AV_PIX_FMT_RGB24;
815  } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
816  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
817  avctx->pix_fmt = AV_PIX_FMT_RGBA;
818  } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
819  s->color_type == PNG_COLOR_TYPE_GRAY) {
820  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
821  } else if (s->bit_depth == 16 &&
822  s->color_type == PNG_COLOR_TYPE_GRAY) {
823  avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
824  } else if (s->bit_depth == 16 &&
825  s->color_type == PNG_COLOR_TYPE_RGB) {
826  avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
827  } else if (s->bit_depth == 16 &&
828  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
829  avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
830  } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
831  s->color_type == PNG_COLOR_TYPE_PALETTE) {
833  } else if (s->bit_depth == 1 && s->bits_per_pixel == 1 && avctx->codec_id != AV_CODEC_ID_APNG) {
834  avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
835  } else if (s->bit_depth == 8 &&
836  s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
837  avctx->pix_fmt = AV_PIX_FMT_YA8;
838  } else if (s->bit_depth == 16 &&
839  s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
840  avctx->pix_fmt = AV_PIX_FMT_YA16BE;
841  } else {
843  "Bit depth %d color type %d",
844  s->bit_depth, s->color_type);
845  return AVERROR_PATCHWELCOME;
846  }
847 
848  if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
849  switch (avctx->pix_fmt) {
850  case AV_PIX_FMT_RGB24:
851  avctx->pix_fmt = AV_PIX_FMT_RGBA;
852  break;
853 
854  case AV_PIX_FMT_RGB48BE:
855  avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
856  break;
857 
858  case AV_PIX_FMT_GRAY8:
859  avctx->pix_fmt = AV_PIX_FMT_YA8;
860  break;
861 
862  case AV_PIX_FMT_GRAY16BE:
863  avctx->pix_fmt = AV_PIX_FMT_YA16BE;
864  break;
865 
866  default:
867  avpriv_request_sample(avctx, "bit depth %d "
868  "and color type %d with TRNS",
869  s->bit_depth, s->color_type);
870  return AVERROR_INVALIDDATA;
871  }
872 
873  s->bpp += byte_depth;
874  }
875 
876  ff_thread_release_ext_buffer(&s->picture);
877  if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
878  /* We only need a buffer for the current picture. */
879  ret = ff_thread_get_buffer(avctx, p, 0);
880  if (ret < 0)
881  return ret;
882  } else if (s->dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
883  /* We need a buffer for the current picture as well as
884  * a buffer for the reference to retain. */
885  ret = ff_thread_get_ext_buffer(avctx, &s->picture,
887  if (ret < 0)
888  return ret;
889  ret = ff_thread_get_buffer(avctx, p, 0);
890  if (ret < 0)
891  return ret;
892  } else {
893  /* The picture output this time and the reference to retain coincide. */
894  if ((ret = ff_thread_get_ext_buffer(avctx, &s->picture,
896  return ret;
897  ret = av_frame_ref(p, s->picture.f);
898  if (ret < 0)
899  return ret;
900  }
901 
903  p->flags |= AV_FRAME_FLAG_KEY;
904  p->flags |= AV_FRAME_FLAG_INTERLACED * !!s->interlace_type;
905 
906  if ((ret = populate_avctx_color_fields(avctx, p)) < 0)
907  return ret;
908  ff_thread_finish_setup(avctx);
909 
910  /* compute the compressed row size */
911  if (!s->interlace_type) {
912  s->crow_size = s->row_size + 1;
913  } else {
914  s->pass = 0;
915  s->pass_row_size = ff_png_pass_row_size(s->pass,
916  s->bits_per_pixel,
917  s->cur_w);
918  s->crow_size = s->pass_row_size + 1;
919  }
920  ff_dlog(avctx, "row_size=%d crow_size =%d\n",
921  s->row_size, s->crow_size);
922 
923  /* copy the palette if needed */
924  if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
925  memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
926  /* empty row is used if differencing to the first row */
927  av_fast_padded_mallocz(&s->last_row, &s->last_row_size, s->row_size);
928  if (!s->last_row)
929  return AVERROR_INVALIDDATA;
930  if (s->interlace_type ||
931  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
932  av_fast_padded_malloc(&s->tmp_row, &s->tmp_row_size, s->row_size);
933  if (!s->tmp_row)
934  return AVERROR_INVALIDDATA;
935  }
936  /* compressed row */
937  av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
938  if (!s->buffer)
939  return AVERROR(ENOMEM);
940 
941  /* we want crow_buf+1 to be 16-byte aligned */
942  s->crow_buf = s->buffer + 15;
943  s->zstream.zstream.avail_out = s->crow_size;
944  s->zstream.zstream.next_out = s->crow_buf;
945  }
946 
947  s->pic_state |= PNG_IDAT;
948 
949  /* set image to non-transparent bpp while decompressing */
950  if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
951  s->bpp -= byte_depth;
952 
953  ret = png_decode_idat(s, gb, p->data[0], p->linesize[0]);
954 
955  if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
956  s->bpp += byte_depth;
957 
958  if (ret < 0)
959  return ret;
960 
961  return 0;
962 }
963 
965  GetByteContext *gb)
966 {
967  int length = bytestream2_get_bytes_left(gb);
968  int n, i, r, g, b;
969 
970  if ((length % 3) != 0 || length > 256 * 3)
971  return AVERROR_INVALIDDATA;
972  /* read the palette */
973  n = length / 3;
974  for (i = 0; i < n; i++) {
975  r = bytestream2_get_byte(gb);
976  g = bytestream2_get_byte(gb);
977  b = bytestream2_get_byte(gb);
978  s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
979  }
980  for (; i < 256; i++)
981  s->palette[i] = (0xFFU << 24);
982  s->hdr_state |= PNG_PLTE;
983 
984  return 0;
985 }
986 
988  GetByteContext *gb)
989 {
990  int length = bytestream2_get_bytes_left(gb);
991  int v, i;
992 
993  if (!(s->hdr_state & PNG_IHDR)) {
994  av_log(avctx, AV_LOG_ERROR, "trns before IHDR\n");
995  return AVERROR_INVALIDDATA;
996  }
997 
998  if (s->pic_state & PNG_IDAT) {
999  av_log(avctx, AV_LOG_ERROR, "trns after IDAT\n");
1000  return AVERROR_INVALIDDATA;
1001  }
1002 
1003  if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
1004  if (length > 256 || !(s->hdr_state & PNG_PLTE))
1005  return AVERROR_INVALIDDATA;
1006 
1007  for (i = 0; i < length; i++) {
1008  unsigned v = bytestream2_get_byte(gb);
1009  s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
1010  }
1011  } else if (s->color_type == PNG_COLOR_TYPE_GRAY || s->color_type == PNG_COLOR_TYPE_RGB) {
1012  if ((s->color_type == PNG_COLOR_TYPE_GRAY && length != 2) ||
1013  (s->color_type == PNG_COLOR_TYPE_RGB && length != 6) ||
1014  s->bit_depth == 1)
1015  return AVERROR_INVALIDDATA;
1016 
1017  for (i = 0; i < length / 2; i++) {
1018  /* only use the least significant bits */
1019  v = av_mod_uintp2(bytestream2_get_be16(gb), s->bit_depth);
1020 
1021  if (s->bit_depth > 8)
1022  AV_WB16(&s->transparent_color_be[2 * i], v);
1023  else
1024  s->transparent_color_be[i] = v;
1025  }
1026  } else {
1027  return AVERROR_INVALIDDATA;
1028  }
1029 
1030  s->has_trns = 1;
1031 
1032  return 0;
1033 }
1034 
1036 {
1037  int ret, cnt = 0;
1038  AVBPrint bp;
1039 
1040  while ((s->iccp_name[cnt++] = bytestream2_get_byte(gb)) && cnt < 81);
1041  if (cnt > 80) {
1042  av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid name!\n");
1044  goto fail;
1045  }
1046 
1047  if (bytestream2_get_byte(gb) != 0) {
1048  av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid compression!\n");
1050  goto fail;
1051  }
1052 
1053  if ((ret = decode_zbuf(&bp, gb->buffer, gb->buffer_end, s->avctx)) < 0)
1054  return ret;
1055 
1056  av_freep(&s->iccp_data);
1057  ret = av_bprint_finalize(&bp, (char **)&s->iccp_data);
1058  if (ret < 0)
1059  return ret;
1060  s->iccp_data_len = bp.len;
1061 
1062  return 0;
1063 fail:
1064  s->iccp_name[0] = 0;
1065  return ret;
1066 }
1067 
1069  GetByteContext *gb)
1070 {
1071  int bits = 0;
1072  int channels;
1073 
1074  if (!(s->hdr_state & PNG_IHDR)) {
1075  av_log(avctx, AV_LOG_ERROR, "sBIT before IHDR\n");
1076  return AVERROR_INVALIDDATA;
1077  }
1078 
1079  if (s->pic_state & PNG_IDAT) {
1080  av_log(avctx, AV_LOG_ERROR, "sBIT after IDAT\n");
1081  return AVERROR_INVALIDDATA;
1082  }
1083 
1084  channels = ff_png_get_nb_channels(s->color_type);
1085 
1087  return AVERROR_INVALIDDATA;
1088 
1089  for (int i = 0; i < channels; i++) {
1090  int b = bytestream2_get_byteu(gb);
1091  bits = FFMAX(b, bits);
1092  }
1093 
1094  if (bits < 0 || bits > s->bit_depth) {
1095  av_log(avctx, AV_LOG_ERROR, "Invalid significant bits: %d\n", bits);
1096  return AVERROR_INVALIDDATA;
1097  }
1098  s->significant_bits = bits;
1099 
1100  return 0;
1101 }
1102 
1104 {
1105  if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
1106  int i, j, k;
1107  uint8_t *pd = p->data[0];
1108  for (j = 0; j < s->height; j++) {
1109  i = s->width / 8;
1110  for (k = 7; k >= 1; k--)
1111  if ((s->width&7) >= k)
1112  pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
1113  for (i--; i >= 0; i--) {
1114  pd[8*i + 7]= pd[i] & 1;
1115  pd[8*i + 6]= (pd[i]>>1) & 1;
1116  pd[8*i + 5]= (pd[i]>>2) & 1;
1117  pd[8*i + 4]= (pd[i]>>3) & 1;
1118  pd[8*i + 3]= (pd[i]>>4) & 1;
1119  pd[8*i + 2]= (pd[i]>>5) & 1;
1120  pd[8*i + 1]= (pd[i]>>6) & 1;
1121  pd[8*i + 0]= pd[i]>>7;
1122  }
1123  pd += p->linesize[0];
1124  }
1125  } else if (s->bits_per_pixel == 2) {
1126  int i, j;
1127  uint8_t *pd = p->data[0];
1128  for (j = 0; j < s->height; j++) {
1129  i = s->width / 4;
1130  if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
1131  if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
1132  if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
1133  if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
1134  for (i--; i >= 0; i--) {
1135  pd[4*i + 3]= pd[i] & 3;
1136  pd[4*i + 2]= (pd[i]>>2) & 3;
1137  pd[4*i + 1]= (pd[i]>>4) & 3;
1138  pd[4*i + 0]= pd[i]>>6;
1139  }
1140  } else {
1141  if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
1142  if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
1143  if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
1144  for (i--; i >= 0; i--) {
1145  pd[4*i + 3]= ( pd[i] & 3)*0x55;
1146  pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
1147  pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
1148  pd[4*i + 0]= ( pd[i]>>6 )*0x55;
1149  }
1150  }
1151  pd += p->linesize[0];
1152  }
1153  } else if (s->bits_per_pixel == 4) {
1154  int i, j;
1155  uint8_t *pd = p->data[0];
1156  for (j = 0; j < s->height; j++) {
1157  i = s->width/2;
1158  if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
1159  if (s->width&1) pd[2*i+0]= pd[i]>>4;
1160  for (i--; i >= 0; i--) {
1161  pd[2*i + 1] = pd[i] & 15;
1162  pd[2*i + 0] = pd[i] >> 4;
1163  }
1164  } else {
1165  if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
1166  for (i--; i >= 0; i--) {
1167  pd[2*i + 1] = (pd[i] & 15) * 0x11;
1168  pd[2*i + 0] = (pd[i] >> 4) * 0x11;
1169  }
1170  }
1171  pd += p->linesize[0];
1172  }
1173  }
1174 }
1175 
1177  GetByteContext *gb)
1178 {
1179  uint32_t sequence_number;
1180  int cur_w, cur_h, x_offset, y_offset, dispose_op, blend_op;
1181 
1183  return AVERROR_INVALIDDATA;
1184 
1185  if (!(s->hdr_state & PNG_IHDR)) {
1186  av_log(avctx, AV_LOG_ERROR, "fctl before IHDR\n");
1187  return AVERROR_INVALIDDATA;
1188  }
1189 
1190  if (s->pic_state & PNG_IDAT) {
1191  av_log(avctx, AV_LOG_ERROR, "fctl after IDAT\n");
1192  return AVERROR_INVALIDDATA;
1193  }
1194 
1195  sequence_number = bytestream2_get_be32(gb);
1196  cur_w = bytestream2_get_be32(gb);
1197  cur_h = bytestream2_get_be32(gb);
1198  x_offset = bytestream2_get_be32(gb);
1199  y_offset = bytestream2_get_be32(gb);
1200  bytestream2_skip(gb, 4); /* delay_num (2), delay_den (2) */
1201  dispose_op = bytestream2_get_byte(gb);
1202  blend_op = bytestream2_get_byte(gb);
1203 
1204  if (sequence_number == 0 &&
1205  (cur_w != s->width ||
1206  cur_h != s->height ||
1207  x_offset != 0 ||
1208  y_offset != 0) ||
1209  cur_w <= 0 || cur_h <= 0 ||
1210  x_offset < 0 || y_offset < 0 ||
1211  cur_w > s->width - x_offset|| cur_h > s->height - y_offset)
1212  return AVERROR_INVALIDDATA;
1213 
1214  if (blend_op != APNG_BLEND_OP_OVER && blend_op != APNG_BLEND_OP_SOURCE) {
1215  av_log(avctx, AV_LOG_ERROR, "Invalid blend_op %d\n", blend_op);
1216  return AVERROR_INVALIDDATA;
1217  }
1218 
1219  if ((sequence_number == 0 || !s->last_picture.f->data[0]) &&
1220  dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
1221  // No previous frame to revert to for the first frame
1222  // Spec says to just treat it as a APNG_DISPOSE_OP_BACKGROUND
1223  dispose_op = APNG_DISPOSE_OP_BACKGROUND;
1224  }
1225 
1226  if (blend_op == APNG_BLEND_OP_OVER && !s->has_trns && (
1227  avctx->pix_fmt == AV_PIX_FMT_RGB24 ||
1228  avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1229  avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
1230  avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
1231  avctx->pix_fmt == AV_PIX_FMT_MONOBLACK
1232  )) {
1233  // APNG_BLEND_OP_OVER is the same as APNG_BLEND_OP_SOURCE when there is no alpha channel
1234  blend_op = APNG_BLEND_OP_SOURCE;
1235  }
1236 
1237  s->cur_w = cur_w;
1238  s->cur_h = cur_h;
1239  s->x_offset = x_offset;
1240  s->y_offset = y_offset;
1241  s->dispose_op = dispose_op;
1242  s->blend_op = blend_op;
1243 
1244  return 0;
1245 }
1246 
1248 {
1249  int i, j;
1250  uint8_t *pd = p->data[0];
1251  uint8_t *pd_last = s->last_picture.f->data[0];
1252  int ls = av_image_get_linesize(p->format, s->width, 0);
1253 
1254  ls = FFMIN(ls, s->width * s->bpp);
1255 
1256  ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
1257  for (j = 0; j < s->height; j++) {
1258  for (i = 0; i < ls; i++)
1259  pd[i] += pd_last[i];
1260  pd += p->linesize[0];
1261  pd_last += s->last_picture.f->linesize[0];
1262  }
1263 }
1264 
1265 // divide by 255 and round to nearest
1266 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
1267 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
1268 
1270  AVFrame *p)
1271 {
1272  uint8_t *dst = p->data[0];
1273  ptrdiff_t dst_stride = p->linesize[0];
1274  const uint8_t *src = s->last_picture.f->data[0];
1275  ptrdiff_t src_stride = s->last_picture.f->linesize[0];
1276  const int bpp = s->color_type == PNG_COLOR_TYPE_PALETTE ? 4 : s->bpp;
1277 
1278  size_t x, y;
1279 
1280  if (s->blend_op == APNG_BLEND_OP_OVER &&
1281  avctx->pix_fmt != AV_PIX_FMT_RGBA &&
1282  avctx->pix_fmt != AV_PIX_FMT_GRAY8A) {
1283  avpriv_request_sample(avctx, "Blending with pixel format %s",
1284  av_get_pix_fmt_name(avctx->pix_fmt));
1285  return AVERROR_PATCHWELCOME;
1286  }
1287 
1288  ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
1289 
1290  // copy unchanged rectangles from the last frame
1291  for (y = 0; y < s->y_offset; y++)
1292  memcpy(dst + y * dst_stride, src + y * src_stride, p->width * bpp);
1293  for (y = s->y_offset; y < s->y_offset + s->cur_h; y++) {
1294  memcpy(dst + y * dst_stride, src + y * src_stride, s->x_offset * bpp);
1295  memcpy(dst + y * dst_stride + (s->x_offset + s->cur_w) * bpp,
1296  src + y * src_stride + (s->x_offset + s->cur_w) * bpp,
1297  (p->width - s->cur_w - s->x_offset) * bpp);
1298  }
1299  for (y = s->y_offset + s->cur_h; y < p->height; y++)
1300  memcpy(dst + y * dst_stride, src + y * src_stride, p->width * bpp);
1301 
1302  if (s->blend_op == APNG_BLEND_OP_OVER) {
1303  // Perform blending
1304  for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
1305  uint8_t *foreground = dst + dst_stride * y + bpp * s->x_offset;
1306  const uint8_t *background = src + src_stride * y + bpp * s->x_offset;
1307  for (x = s->x_offset; x < s->x_offset + s->cur_w; ++x, foreground += bpp, background += bpp) {
1308  size_t b;
1309  uint8_t foreground_alpha, background_alpha, output_alpha;
1310  uint8_t output[10];
1311 
1312  // Since we might be blending alpha onto alpha, we use the following equations:
1313  // output_alpha = foreground_alpha + (1 - foreground_alpha) * background_alpha
1314  // output = (foreground_alpha * foreground + (1 - foreground_alpha) * background_alpha * background) / output_alpha
1315 
1316  switch (avctx->pix_fmt) {
1317  case AV_PIX_FMT_RGBA:
1318  foreground_alpha = foreground[3];
1319  background_alpha = background[3];
1320  break;
1321 
1322  case AV_PIX_FMT_GRAY8A:
1323  foreground_alpha = foreground[1];
1324  background_alpha = background[1];
1325  break;
1326  }
1327 
1328  if (foreground_alpha == 255)
1329  continue;
1330 
1331  if (foreground_alpha == 0) {
1332  memcpy(foreground, background, bpp);
1333  continue;
1334  }
1335 
1336  output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);
1337 
1338  av_assert0(bpp <= 10);
1339 
1340  for (b = 0; b < bpp - 1; ++b) {
1341  if (output_alpha == 0) {
1342  output[b] = 0;
1343  } else if (background_alpha == 255) {
1344  output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);
1345  } else {
1346  output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);
1347  }
1348  }
1349  output[b] = output_alpha;
1350  memcpy(foreground, output, bpp);
1351  }
1352  }
1353  }
1354 
1355  return 0;
1356 }
1357 
1359 {
1360  // need to reset a rectangle to black
1361  av_unused int ret = av_frame_copy(s->picture.f, p);
1362  const int bpp = s->color_type == PNG_COLOR_TYPE_PALETTE ? 4 : s->bpp;
1363  const ptrdiff_t dst_stride = s->picture.f->linesize[0];
1364  uint8_t *dst = s->picture.f->data[0] + s->y_offset * dst_stride + bpp * s->x_offset;
1365 
1366  av_assert1(ret >= 0);
1367 
1368  for (size_t y = 0; y < s->cur_h; y++) {
1369  memset(dst, 0, bpp * s->cur_w);
1370  dst += dst_stride;
1371  }
1372 }
1373 
1375  AVFrame *p, const AVPacket *avpkt)
1376 {
1377  const AVCRC *crc_tab = av_crc_get_table(AV_CRC_32_IEEE_LE);
1378  uint32_t tag, length;
1379  int decode_next_dat = 0;
1380  int i, ret;
1381 
1382  for (;;) {
1383  GetByteContext gb_chunk;
1384 
1385  length = bytestream2_get_bytes_left(&s->gb);
1386  if (length <= 0) {
1387 
1388  if (avctx->codec_id == AV_CODEC_ID_PNG &&
1389  avctx->skip_frame == AVDISCARD_ALL) {
1390  return 0;
1391  }
1392 
1393  if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && length == 0) {
1394  if (!(s->pic_state & PNG_IDAT))
1395  return 0;
1396  else
1397  goto exit_loop;
1398  }
1399  av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length);
1400  if ( s->pic_state & PNG_ALLIMAGE
1402  goto exit_loop;
1404  goto fail;
1405  }
1406 
1407  length = bytestream2_get_be32(&s->gb);
1408  if (length > 0x7fffffff || length + 8 > bytestream2_get_bytes_left(&s->gb)) {
1409  av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
1411  goto fail;
1412  }
1413  if (avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_IGNORE_ERR)) {
1414  uint32_t crc_sig = AV_RB32(s->gb.buffer + length + 4);
1415  uint32_t crc_cal = ~av_crc(crc_tab, UINT32_MAX, s->gb.buffer, length + 4);
1416  if (crc_sig ^ crc_cal) {
1417  av_log(avctx, AV_LOG_ERROR, "CRC mismatch in chunk");
1418  if (avctx->err_recognition & AV_EF_EXPLODE) {
1419  av_log(avctx, AV_LOG_ERROR, ", quitting\n");
1421  goto fail;
1422  }
1423  av_log(avctx, AV_LOG_ERROR, ", skipping\n");
1424  bytestream2_skip(&s->gb, length + 8); /* tag */
1425  continue;
1426  }
1427  }
1428  tag = bytestream2_get_le32(&s->gb);
1429  if (avctx->debug & FF_DEBUG_STARTCODE)
1430  av_log(avctx, AV_LOG_DEBUG, "png: tag=%s length=%u\n",
1431  av_fourcc2str(tag), length);
1432 
1433  bytestream2_init(&gb_chunk, s->gb.buffer, length);
1434  bytestream2_skip(&s->gb, length + 4);
1435 
1436  if (avctx->codec_id == AV_CODEC_ID_PNG &&
1437  avctx->skip_frame == AVDISCARD_ALL) {
1438  switch(tag) {
1439  case MKTAG('I', 'H', 'D', 'R'):
1440  case MKTAG('p', 'H', 'Y', 's'):
1441  case MKTAG('t', 'E', 'X', 't'):
1442  case MKTAG('I', 'D', 'A', 'T'):
1443  case MKTAG('t', 'R', 'N', 'S'):
1444  case MKTAG('s', 'R', 'G', 'B'):
1445  case MKTAG('c', 'I', 'C', 'P'):
1446  case MKTAG('c', 'H', 'R', 'M'):
1447  case MKTAG('g', 'A', 'M', 'A'):
1448  break;
1449  default:
1450  continue;
1451  }
1452  }
1453 
1454  switch (tag) {
1455  case MKTAG('I', 'H', 'D', 'R'):
1456  if ((ret = decode_ihdr_chunk(avctx, s, &gb_chunk)) < 0)
1457  goto fail;
1458  break;
1459  case MKTAG('p', 'H', 'Y', 's'):
1460  if ((ret = decode_phys_chunk(avctx, s, &gb_chunk)) < 0)
1461  goto fail;
1462  break;
1463  case MKTAG('f', 'c', 'T', 'L'):
1464  if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1465  continue;
1466  if ((ret = decode_fctl_chunk(avctx, s, &gb_chunk)) < 0)
1467  goto fail;
1468  decode_next_dat = 1;
1469  break;
1470  case MKTAG('f', 'd', 'A', 'T'):
1471  if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1472  continue;
1473  if (!decode_next_dat || bytestream2_get_bytes_left(&gb_chunk) < 4) {
1475  goto fail;
1476  }
1477  bytestream2_get_be32(&gb_chunk);
1478  /* fallthrough */
1479  case MKTAG('I', 'D', 'A', 'T'):
1480  if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
1481  continue;
1482  if ((ret = decode_idat_chunk(avctx, s, &gb_chunk, p)) < 0)
1483  goto fail;
1484  break;
1485  case MKTAG('P', 'L', 'T', 'E'):
1486  decode_plte_chunk(avctx, s, &gb_chunk);
1487  break;
1488  case MKTAG('t', 'R', 'N', 'S'):
1489  decode_trns_chunk(avctx, s, &gb_chunk);
1490  break;
1491  case MKTAG('t', 'E', 'X', 't'):
1492  if (decode_text_chunk(s, &gb_chunk, 0) < 0)
1493  av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
1494  break;
1495  case MKTAG('z', 'T', 'X', 't'):
1496  if (decode_text_chunk(s, &gb_chunk, 1) < 0)
1497  av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
1498  break;
1499  case MKTAG('s', 'T', 'E', 'R'): {
1500  int mode = bytestream2_get_byte(&gb_chunk);
1501 
1502  if (mode == 0 || mode == 1) {
1503  s->stereo_mode = mode;
1504  } else {
1505  av_log(avctx, AV_LOG_WARNING,
1506  "Unknown value in sTER chunk (%d)\n", mode);
1507  }
1508  break;
1509  }
1510  case MKTAG('c', 'I', 'C', 'P'):
1511  s->cicp_primaries = bytestream2_get_byte(&gb_chunk);
1512  s->cicp_trc = bytestream2_get_byte(&gb_chunk);
1513  if (bytestream2_get_byte(&gb_chunk) != 0)
1514  av_log(avctx, AV_LOG_WARNING, "nonzero cICP matrix\n");
1515  s->cicp_range = bytestream2_get_byte(&gb_chunk);
1516  if (s->cicp_range != 0 && s->cicp_range != 1)
1517  av_log(avctx, AV_LOG_WARNING, "invalid cICP range: %d\n", s->cicp_range);
1518  s->have_cicp = 1;
1519  break;
1520  case MKTAG('s', 'R', 'G', 'B'):
1521  /* skip rendering intent byte */
1522  bytestream2_skip(&gb_chunk, 1);
1523  s->have_srgb = 1;
1524  break;
1525  case MKTAG('i', 'C', 'C', 'P'): {
1526  if ((ret = decode_iccp_chunk(s, &gb_chunk)) < 0)
1527  goto fail;
1528  break;
1529  }
1530  case MKTAG('c', 'H', 'R', 'M'): {
1531  s->have_chrm = 1;
1532 
1533  s->white_point[0] = bytestream2_get_be32(&gb_chunk);
1534  s->white_point[1] = bytestream2_get_be32(&gb_chunk);
1535 
1536  /* RGB Primaries */
1537  for (i = 0; i < 3; i++) {
1538  s->display_primaries[i][0] = bytestream2_get_be32(&gb_chunk);
1539  s->display_primaries[i][1] = bytestream2_get_be32(&gb_chunk);
1540  }
1541 
1542  break;
1543  }
1544  case MKTAG('s', 'B', 'I', 'T'):
1545  if ((ret = decode_sbit_chunk(avctx, s, &gb_chunk)) < 0)
1546  goto fail;
1547  break;
1548  case MKTAG('g', 'A', 'M', 'A'): {
1549  AVBPrint bp;
1550  char *gamma_str;
1551  s->gamma = bytestream2_get_be32(&gb_chunk);
1552 
1554  av_bprintf(&bp, "%i/%i", s->gamma, 100000);
1555  ret = av_bprint_finalize(&bp, &gamma_str);
1556  if (ret < 0)
1557  return ret;
1558 
1559  av_dict_set(&s->frame_metadata, "gamma", gamma_str, AV_DICT_DONT_STRDUP_VAL);
1560 
1561  break;
1562  }
1563  case MKTAG('c', 'L', 'L', 'i'):
1564  if (bytestream2_get_bytes_left(&gb_chunk) != 8) {
1565  av_log(avctx, AV_LOG_WARNING, "Invalid cLLi chunk size: %d\n", bytestream2_get_bytes_left(&gb_chunk));
1566  break;
1567  }
1568  s->have_clli = 1;
1569  s->clli_max = bytestream2_get_be32u(&gb_chunk);
1570  s->clli_avg = bytestream2_get_be32u(&gb_chunk);
1571  break;
1572  case MKTAG('m', 'D', 'V', 'c'):
1573  if (bytestream2_get_bytes_left(&gb_chunk) != 24) {
1574  av_log(avctx, AV_LOG_WARNING, "Invalid mDVc chunk size: %d\n", bytestream2_get_bytes_left(&gb_chunk));
1575  break;
1576  }
1577  s->have_mdvc = 1;
1578  for (int i = 0; i < 3; i++) {
1579  s->mdvc_primaries[i][0] = bytestream2_get_be16u(&gb_chunk);
1580  s->mdvc_primaries[i][1] = bytestream2_get_be16u(&gb_chunk);
1581  }
1582  s->mdvc_white_point[0] = bytestream2_get_be16u(&gb_chunk);
1583  s->mdvc_white_point[1] = bytestream2_get_be16u(&gb_chunk);
1584  s->mdvc_max_lum = bytestream2_get_be32u(&gb_chunk);
1585  s->mdvc_min_lum = bytestream2_get_be32u(&gb_chunk);
1586  break;
1587  case MKTAG('I', 'E', 'N', 'D'):
1588  if (!(s->pic_state & PNG_ALLIMAGE))
1589  av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
1590  if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT))) {
1592  goto fail;
1593  }
1594  goto exit_loop;
1595  }
1596  }
1597 exit_loop:
1598 
1599  if (!p)
1600  return AVERROR_INVALIDDATA;
1601 
1602  if (avctx->codec_id == AV_CODEC_ID_PNG &&
1603  avctx->skip_frame == AVDISCARD_ALL) {
1604  return 0;
1605  }
1606 
1609  goto fail;
1610  }
1611 
1612  if (s->bits_per_pixel <= 4)
1613  handle_small_bpp(s, p);
1614 
1615  if (s->color_type == PNG_COLOR_TYPE_PALETTE && avctx->codec_id == AV_CODEC_ID_APNG) {
1616  for (int y = 0; y < s->height; y++) {
1617  uint8_t *row = &p->data[0][p->linesize[0] * y];
1618 
1619  for (int x = s->width - 1; x >= 0; x--) {
1620  const uint8_t idx = row[x];
1621 
1622  row[4*x+2] = s->palette[idx] & 0xFF;
1623  row[4*x+1] = (s->palette[idx] >> 8 ) & 0xFF;
1624  row[4*x+0] = (s->palette[idx] >> 16) & 0xFF;
1625  row[4*x+3] = s->palette[idx] >> 24;
1626  }
1627  }
1628  }
1629 
1630  /* apply transparency if needed */
1631  if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
1632  size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
1633  size_t raw_bpp = s->bpp - byte_depth;
1634  ptrdiff_t x, y;
1635 
1636  av_assert0(s->bit_depth > 1);
1637 
1638  for (y = 0; y < s->height; ++y) {
1639  uint8_t *row = &p->data[0][p->linesize[0] * y];
1640 
1641  if (s->bpp == 2 && byte_depth == 1) {
1642  uint8_t *pixel = &row[2 * s->width - 1];
1643  uint8_t *rowp = &row[1 * s->width - 1];
1644  int tcolor = s->transparent_color_be[0];
1645  for (x = s->width; x > 0; --x) {
1646  *pixel-- = *rowp == tcolor ? 0 : 0xff;
1647  *pixel-- = *rowp--;
1648  }
1649  } else if (s->bpp == 4 && byte_depth == 1) {
1650  uint8_t *pixel = &row[4 * s->width - 1];
1651  uint8_t *rowp = &row[3 * s->width - 1];
1652  int tcolor = AV_RL24(s->transparent_color_be);
1653  for (x = s->width; x > 0; --x) {
1654  *pixel-- = AV_RL24(rowp-2) == tcolor ? 0 : 0xff;
1655  *pixel-- = *rowp--;
1656  *pixel-- = *rowp--;
1657  *pixel-- = *rowp--;
1658  }
1659  } else {
1660  /* since we're updating in-place, we have to go from right to left */
1661  for (x = s->width; x > 0; --x) {
1662  uint8_t *pixel = &row[s->bpp * (x - 1)];
1663  memmove(pixel, &row[raw_bpp * (x - 1)], raw_bpp);
1664 
1665  if (!memcmp(pixel, s->transparent_color_be, raw_bpp)) {
1666  memset(&pixel[raw_bpp], 0, byte_depth);
1667  } else {
1668  memset(&pixel[raw_bpp], 0xff, byte_depth);
1669  }
1670  }
1671  }
1672  }
1673  }
1674 
1675  /* handle P-frames only if a predecessor frame is available */
1676  if (s->last_picture.f->data[0]) {
1677  if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
1678  && s->last_picture.f->width == p->width
1679  && s->last_picture.f->height== p->height
1680  && s->last_picture.f->format== p->format
1681  ) {
1682  if (CONFIG_PNG_DECODER && avctx->codec_id != AV_CODEC_ID_APNG)
1683  handle_p_frame_png(s, p);
1684  else if (CONFIG_APNG_DECODER &&
1685  avctx->codec_id == AV_CODEC_ID_APNG &&
1686  (ret = handle_p_frame_apng(avctx, s, p)) < 0)
1687  goto fail;
1688  }
1689  }
1690  if (CONFIG_APNG_DECODER && s->dispose_op == APNG_DISPOSE_OP_BACKGROUND)
1692 
1693  ff_thread_report_progress(&s->picture, INT_MAX, 0);
1694 
1695  return 0;
1696 
1697 fail:
1698  ff_thread_report_progress(&s->picture, INT_MAX, 0);
1699  return ret;
1700 }
1701 
1703 {
1704  av_freep(&s->iccp_data);
1705  s->iccp_data_len = 0;
1706  s->iccp_name[0] = 0;
1707 
1708  s->stereo_mode = -1;
1709 
1710  s->have_chrm = 0;
1711  s->have_srgb = 0;
1712  s->have_cicp = 0;
1713 
1714  av_dict_free(&s->frame_metadata);
1715 }
1716 
1718 {
1719  int ret;
1720 
1721  if (s->stereo_mode >= 0) {
1723  if (!stereo3d) {
1724  ret = AVERROR(ENOMEM);
1725  goto fail;
1726  }
1727 
1728  stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
1729  stereo3d->flags = s->stereo_mode ? 0 : AV_STEREO3D_FLAG_INVERT;
1730  }
1731 
1732  FFSWAP(AVDictionary*, f->metadata, s->frame_metadata);
1733 
1734  return 0;
1735 fail:
1736  av_frame_unref(f);
1737  return ret;
1738 }
1739 
1740 #if CONFIG_PNG_DECODER
1741 static int decode_frame_png(AVCodecContext *avctx, AVFrame *p,
1742  int *got_frame, AVPacket *avpkt)
1743 {
1744  PNGDecContext *const s = avctx->priv_data;
1745  const uint8_t *buf = avpkt->data;
1746  int buf_size = avpkt->size;
1747  int64_t sig;
1748  int ret;
1749 
1751 
1752  bytestream2_init(&s->gb, buf, buf_size);
1753 
1754  /* check signature */
1755  sig = bytestream2_get_be64(&s->gb);
1756  if (sig != PNGSIG &&
1757  sig != MNGSIG) {
1758  av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature 0x%08"PRIX64".\n", sig);
1759  return AVERROR_INVALIDDATA;
1760  }
1761 
1762  s->y = s->has_trns = 0;
1763  s->hdr_state = 0;
1764  s->pic_state = 0;
1765 
1766  /* Reset z_stream */
1767  ret = inflateReset(&s->zstream.zstream);
1768  if (ret != Z_OK)
1769  return AVERROR_EXTERNAL;
1770 
1771  if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1772  goto the_end;
1773 
1774  if (avctx->skip_frame == AVDISCARD_ALL) {
1775  *got_frame = 0;
1776  ret = bytestream2_tell(&s->gb);
1777  goto the_end;
1778  }
1779 
1780  ret = output_frame(s, p);
1781  if (ret < 0)
1782  goto the_end;
1783 
1784  if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
1785  ff_thread_release_ext_buffer(&s->last_picture);
1786  FFSWAP(ThreadFrame, s->picture, s->last_picture);
1787  }
1788 
1789  *got_frame = 1;
1790 
1791  ret = bytestream2_tell(&s->gb);
1792 the_end:
1793  s->crow_buf = NULL;
1794  return ret;
1795 }
1796 #endif
1797 
1798 #if CONFIG_APNG_DECODER
1799 static int decode_frame_apng(AVCodecContext *avctx, AVFrame *p,
1800  int *got_frame, AVPacket *avpkt)
1801 {
1802  PNGDecContext *const s = avctx->priv_data;
1803  int ret;
1804 
1806 
1807  if (!(s->hdr_state & PNG_IHDR)) {
1808  if (!avctx->extradata_size)
1809  return AVERROR_INVALIDDATA;
1810 
1811  if ((ret = inflateReset(&s->zstream.zstream)) != Z_OK)
1812  return AVERROR_EXTERNAL;
1813  bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
1814  if ((ret = decode_frame_common(avctx, s, NULL, avpkt)) < 0)
1815  return ret;
1816  }
1817 
1818  /* reset state for a new frame */
1819  if ((ret = inflateReset(&s->zstream.zstream)) != Z_OK)
1820  return AVERROR_EXTERNAL;
1821  s->y = 0;
1822  s->pic_state = 0;
1823  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1824  if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1825  return ret;
1826 
1827  if (!(s->pic_state & PNG_ALLIMAGE))
1828  av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
1829  if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT)))
1830  return AVERROR_INVALIDDATA;
1831 
1832  ret = output_frame(s, p);
1833  if (ret < 0)
1834  return ret;
1835 
1836  if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
1837  if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
1838  ff_thread_release_ext_buffer(&s->picture);
1839  } else {
1840  ff_thread_release_ext_buffer(&s->last_picture);
1841  FFSWAP(ThreadFrame, s->picture, s->last_picture);
1842  }
1843  }
1844 
1845  *got_frame = 1;
1846  return bytestream2_tell(&s->gb);
1847 }
1848 #endif
1849 
1850 #if HAVE_THREADS
1851 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1852 {
1853  PNGDecContext *psrc = src->priv_data;
1854  PNGDecContext *pdst = dst->priv_data;
1855  ThreadFrame *src_frame = NULL;
1856  int ret;
1857 
1858  if (dst == src)
1859  return 0;
1860 
1861  if (CONFIG_APNG_DECODER && dst->codec_id == AV_CODEC_ID_APNG) {
1862 
1863  pdst->width = psrc->width;
1864  pdst->height = psrc->height;
1865  pdst->bit_depth = psrc->bit_depth;
1866  pdst->color_type = psrc->color_type;
1867  pdst->compression_type = psrc->compression_type;
1868  pdst->interlace_type = psrc->interlace_type;
1869  pdst->filter_type = psrc->filter_type;
1870  pdst->has_trns = psrc->has_trns;
1871  memcpy(pdst->transparent_color_be, psrc->transparent_color_be, sizeof(pdst->transparent_color_be));
1872 
1873  memcpy(pdst->palette, psrc->palette, sizeof(pdst->palette));
1874 
1875  pdst->hdr_state |= psrc->hdr_state;
1876  }
1877 
1878  src_frame = psrc->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?
1879  &psrc->last_picture : &psrc->picture;
1880 
1882  if (src_frame && src_frame->f->data[0]) {
1883  ret = ff_thread_ref_frame(&pdst->last_picture, src_frame);
1884  if (ret < 0)
1885  return ret;
1886  }
1887 
1888  return 0;
1889 }
1890 #endif
1891 
1893 {
1894  PNGDecContext *s = avctx->priv_data;
1895 
1896  s->avctx = avctx;
1897  s->last_picture.f = av_frame_alloc();
1898  s->picture.f = av_frame_alloc();
1899  if (!s->last_picture.f || !s->picture.f)
1900  return AVERROR(ENOMEM);
1901 
1902  ff_pngdsp_init(&s->dsp);
1903 
1904  return ff_inflate_init(&s->zstream, avctx);
1905 }
1906 
1908 {
1909  PNGDecContext *s = avctx->priv_data;
1910 
1911  ff_thread_release_ext_buffer(&s->last_picture);
1912  av_frame_free(&s->last_picture.f);
1913  ff_thread_release_ext_buffer(&s->picture);
1914  av_frame_free(&s->picture.f);
1915  av_freep(&s->buffer);
1916  s->buffer_size = 0;
1917  av_freep(&s->last_row);
1918  s->last_row_size = 0;
1919  av_freep(&s->tmp_row);
1920  s->tmp_row_size = 0;
1921 
1922  av_freep(&s->iccp_data);
1923  av_dict_free(&s->frame_metadata);
1924  ff_inflate_end(&s->zstream);
1925 
1926  return 0;
1927 }
1928 
1929 #if CONFIG_APNG_DECODER
1930 const FFCodec ff_apng_decoder = {
1931  .p.name = "apng",
1932  CODEC_LONG_NAME("APNG (Animated Portable Network Graphics) image"),
1933  .p.type = AVMEDIA_TYPE_VIDEO,
1934  .p.id = AV_CODEC_ID_APNG,
1935  .priv_data_size = sizeof(PNGDecContext),
1936  .init = png_dec_init,
1937  .close = png_dec_end,
1938  FF_CODEC_DECODE_CB(decode_frame_apng),
1940  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1941  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1944 };
1945 #endif
1946 
1947 #if CONFIG_PNG_DECODER
1948 const FFCodec ff_png_decoder = {
1949  .p.name = "png",
1950  CODEC_LONG_NAME("PNG (Portable Network Graphics) image"),
1951  .p.type = AVMEDIA_TYPE_VIDEO,
1952  .p.id = AV_CODEC_ID_PNG,
1953  .priv_data_size = sizeof(PNGDecContext),
1954  .init = png_dec_init,
1955  .close = png_dec_end,
1956  FF_CODEC_DECODE_CB(decode_frame_png),
1958  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1959  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
1962 };
1963 #endif
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
PNG_PLTE
@ PNG_PLTE
Definition: pngdec.c:52
PNGDSPContext
Definition: pngdsp.h:27
AVFrame::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: frame.h:623
AVMasteringDisplayMetadata::has_primaries
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
Definition: mastering_display_metadata.h:62
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:619
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
AVMasteringDisplayMetadata::max_luminance
AVRational max_luminance
Max luminance of mastering display (cd/m^2).
Definition: mastering_display_metadata.h:57
PNGDecContext::clli_max
uint32_t clli_max
Definition: pngdec.c:86
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: defs.h:51
clear_frame_metadata
static void clear_frame_metadata(PNGDecContext *s)
Definition: pngdec.c:1702
ff_add_png_paeth_prediction
void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
Definition: pngdec.c:216
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
PNGDecContext::have_srgb
int have_srgb
Definition: pngdec.c:80
PNGDecContext::cicp_range
enum AVColorRange cicp_range
Definition: pngdec.c:84
r
const char * r
Definition: vf_curves.c:126
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
AV_PIX_FMT_YA8
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition: pixfmt.h:140
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:685
AVColorTransferCharacteristic
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:580
out
FILE * out
Definition: movenc.c:54
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
GetByteContext
Definition: bytestream.h:33
PNG_ALLIMAGE
@ PNG_ALLIMAGE
Definition: pngdec.c:57
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
PNGDecContext::last_row_size
unsigned int last_row_size
Definition: pngdec.c:115
APNG_FCTL_CHUNK_SIZE
#define APNG_FCTL_CHUNK_SIZE
Definition: apng.h:42
decode_phys_chunk
static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb)
Definition: pngdec.c:639
AVCOL_TRC_LINEAR
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition: pixfmt.h:589
PNGDecContext::bit_depth
int bit_depth
Definition: pngdec.c:100
ff_png_get_nb_channels
int ff_png_get_nb_channels(int color_type)
Definition: png.c:41
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
PNGDSPContext::add_paeth_prediction
void(* add_paeth_prediction)(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
Definition: pngdsp.h:33
AVMasteringDisplayMetadata::has_luminance
int has_luminance
Flag indicating whether the luminance (min_ and max_) have been set.
Definition: mastering_display_metadata.h:67
AVCodecContext::err_recognition
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1420
rational.h
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
PNGDecContext::crow_size
int crow_size
Definition: pngdec.c:121
AVFrame::color_primaries
enum AVColorPrimaries color_primaries
Definition: frame.h:621
av_unused
#define av_unused
Definition: attributes.h:131
av_mod_uintp2
#define av_mod_uintp2
Definition: common.h:125
decode_text_chunk
static int decode_text_chunk(PNGDecContext *s, GetByteContext *gb, int compressed)
Definition: pngdec.c:541
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:88
APNG_BLEND_OP_OVER
@ APNG_BLEND_OP_OVER
Definition: apng.h:38
AVContentLightMetadata::MaxCLL
unsigned MaxCLL
Max content light level (cd/m^2).
Definition: mastering_display_metadata.h:102
AVFrame::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: frame.h:630
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVCOL_TRC_NB
@ AVCOL_TRC_NB
Not part of ABI.
Definition: pixfmt.h:602
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
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:678
AVFrame::width
int width
Definition: frame.h:412
w
uint8_t w
Definition: llviddspenc.c:38
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:683
AVPacket::data
uint8_t * data
Definition: packet.h:522
PNGDecContext::have_clli
int have_clli
Definition: pngdec.c:85
b
#define b
Definition: input.c:41
data
const char data[16]
Definition: mxf.c:148
PNGDecContext::mdvc_max_lum
uint32_t mdvc_max_lum
Definition: pngdec.c:91
FFCodec
Definition: codec_internal.h:127
PNGDecContext::row_size
int row_size
Definition: pngdec.c:122
FFZStream::zstream
z_stream zstream
Definition: zlib_wrapper.h:28
AVCOL_SPC_RGB
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition: pixfmt.h:610
PNGDecContext::width
int width
Definition: pngdec.c:96
AVDictionary
Definition: dict.c:34
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:612
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVColorPrimaries
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:555
AV_CODEC_ID_APNG
@ AV_CODEC_ID_APNG
Definition: codec_id.h:265
PNGDecContext::tmp_row_size
unsigned int tmp_row_size
Definition: pngdec.c:117
PNGDecContext::color_type
int color_type
Definition: pngdec.c:101
PNGDecContext::cur_h
int cur_h
Definition: pngdec.c:97
ff_set_dimensions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:94
PNGDecContext::bits_per_pixel
int bits_per_pixel
Definition: pngdec.c:106
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:577
thread.h
ff_thread_await_progress
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before ff_thread_await_progress() has been called on them. reget_buffer() and buffer age optimizations no longer work. *The contents of buffers must not be written to after ff_thread_report_progress() has been called on them. This includes draw_edges(). Porting codecs to frame threading
PNGDecContext::gamma
int gamma
Definition: pngdec.c:79
ThreadFrame::f
AVFrame * f
Definition: threadframe.h:28
FF_DEBUG_PICT_INFO
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:1397
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
ff_thread_get_buffer
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have so the codec calls ff_thread_report set FF_CODEC_CAP_ALLOCATE_PROGRESS in FFCodec caps_internal and use ff_thread_get_buffer() to allocate frames. Otherwise decode directly into the user-supplied frames. Call ff_thread_report_progress() after some part of the current picture has decoded. A good place to put this is where draw_horiz_band() is called - add this if it isn 't called anywhere
NB_PASSES
#define NB_PASSES
Definition: png.h:47
PNGDecContext::blend_op
uint8_t blend_op
Definition: pngdec.c:99
AVContentLightMetadata
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
Definition: mastering_display_metadata.h:98
crc.h
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
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
FAST_DIV255
#define FAST_DIV255(x)
Definition: pngdec.c:1267
PNGImageState
PNGImageState
Definition: pngdec.c:55
PNG_FILTER_TYPE_LOCO
#define PNG_FILTER_TYPE_LOCO
Definition: png.h:39
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
PNGDecContext::have_mdvc
int have_mdvc
Definition: pngdec.c:88
AVCOL_TRC_IEC61966_2_1
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:594
PNGDecContext::pass_row_size
int pass_row_size
Definition: pngdec.c:123
ff_png_pass_row_size
int ff_png_pass_row_size(int pass, int bits_per_pixel, int width)
Definition: png.c:54
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1819
fail
#define fail()
Definition: checkasm.h:179
inflate
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
Definition: vf_neighbor.c:194
png_dec_end
static av_cold int png_dec_end(AVCodecContext *avctx)
Definition: pngdec.c:1907
OP_AVG
#define OP_AVG(x, s, l)
AVCOL_TRC_GAMMA28
@ AVCOL_TRC_GAMMA28
also ITU-R BT470BG
Definition: pixfmt.h:586
PNGDecContext::pic_state
enum PNGImageState pic_state
Definition: pngdec.c:95
decode_idat_chunk
static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb, AVFrame *p)
Definition: pngdec.c:789
png_pass_dsp_ymask
static const uint8_t png_pass_dsp_ymask[NB_PASSES]
Definition: pngdec.c:134
AVRational::num
int num
Numerator.
Definition: rational.h:59
PNG_COLOR_TYPE_RGB_ALPHA
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:36
PNGDecContext::crow_buf
uint8_t * crow_buf
Definition: pngdec.c:113
AVCOL_TRC_GAMMA22
@ AVCOL_TRC_GAMMA22
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:585
AV_DICT_DONT_STRDUP_VAL
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:79
PNGDecContext::last_row
uint8_t * last_row
Definition: pngdec.c:114
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:76
PNGDecContext::height
int height
Definition: pngdec.c:96
avassert.h
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:671
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
pngdsp.h
zlib_wrapper.h
av_cold
#define av_cold
Definition: attributes.h:90
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:591
ff_thread_report_progress
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
Definition: pthread_frame.c:573
PNGDecContext::have_cicp
int have_cicp
Definition: pngdec.c:81
YUV2RGB
#define YUV2RGB(NAME, TYPE)
Definition: pngdec.c:339
mask
static const uint16_t mask[17]
Definition: lzw.c:38
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:524
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
stereo3d.h
AVMasteringDisplayMetadata::white_point
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
Definition: mastering_display_metadata.h:47
intreadwrite.h
PNGDecContext::tmp_row
uint8_t * tmp_row
Definition: pngdec.c:116
s
#define s(width, name)
Definition: cbs_vp9.c:198
PNGDecContext::clli_avg
uint32_t clli_avg
Definition: pngdec.c:87
AVCOL_PRI_NB
@ AVCOL_PRI_NB
Not part of ABI.
Definition: pixfmt.h:573
ff_apng_decoder
const FFCodec ff_apng_decoder
PNGDecContext::y_offset
int y_offset
Definition: pngdec.c:98
PNGDecContext::palette
uint32_t palette[256]
Definition: pngdec.c:112
g
const char * g
Definition: vf_curves.c:127
AV_GET_BUFFER_FLAG_REF
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:425
APNG_DISPOSE_OP_PREVIOUS
@ APNG_DISPOSE_OP_PREVIOUS
Definition: apng.h:33
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
PNG_COLOR_TYPE_RGB
#define PNG_COLOR_TYPE_RGB
Definition: png.h:35
ff_png_decoder
const FFCodec ff_png_decoder
bits
uint8_t bits
Definition: vp3data.h:128
AV_EF_IGNORE_ERR
#define AV_EF_IGNORE_ERR
ignore errors and continue
Definition: defs.h:53
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
PNGDecContext::hdr_state
enum PNGHeaderState hdr_state
Definition: pngdec.c:94
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1574
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
PNGDecContext::iccp_data
uint8_t * iccp_data
Definition: pngdec.c:71
channels
channels
Definition: aptx.h:31
decode.h
percent_missing
static int percent_missing(PNGDecContext *s)
Definition: pngdec.c:353
av_csp_primaries_id_from_desc
enum AVColorPrimaries av_csp_primaries_id_from_desc(const AVColorPrimariesDesc *prm)
Detects which enum AVColorPrimaries constant corresponds to the given complete gamut description.
Definition: csp.c:110
APNG_DISPOSE_OP_BACKGROUND
@ APNG_DISPOSE_OP_BACKGROUND
Definition: apng.h:32
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:558
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
frame
static AVFrame * frame
Definition: demux_decode.c:54
ff_decode_mastering_display_new
int ff_decode_mastering_display_new(const AVCodecContext *avctx, AVFrame *frame, AVMasteringDisplayMetadata **mdm)
Wrapper around av_mastering_display_metadata_create_side_data(), which rejects side data overridden b...
Definition: decode.c:1861
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:455
ff_thread_ref_frame
int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
Definition: utils.c:850
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
PNGDecContext
Definition: pngdec.c:60
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:110
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:219
threadframe.h
AV_PIX_FMT_GRAY8A
@ AV_PIX_FMT_GRAY8A
alias for AV_PIX_FMT_YA8
Definition: pixfmt.h:143
PNGDecContext::cicp_primaries
enum AVColorPrimaries cicp_primaries
Definition: pngdec.c:82
NULL
#define NULL
Definition: coverity.c:32
PNGDecContext::filter_type
int filter_type
Definition: pngdec.c:104
png_decode_idat
static int png_decode_idat(PNGDecContext *s, GetByteContext *gb, uint8_t *dst, ptrdiff_t dst_stride)
Definition: pngdec.c:444
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
png_dec_init
static av_cold int png_dec_init(AVCodecContext *avctx)
Definition: pngdec.c:1892
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:695
apng.h
pixel
uint8_t pixel
Definition: tiny_ssim.c:41
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:403
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
PNGDecContext::mdvc_min_lum
uint32_t mdvc_min_lum
Definition: pngdec.c:92
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:557
PNGDecContext::channels
int channels
Definition: pngdec.c:105
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
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
ff_thread_release_ext_buffer
void ff_thread_release_ext_buffer(ThreadFrame *f)
Unref a ThreadFrame.
Definition: pthread_frame.c:996
PNG_COLOR_TYPE_GRAY
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:33
abs
#define abs(x)
Definition: cuda_runtime.h:35
UPDATE_THREAD_CONTEXT
#define UPDATE_THREAD_CONTEXT(func)
Definition: codec_internal.h:281
decode_sbit_chunk
static int decode_sbit_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb)
Definition: pngdec.c:1068
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
AV_EF_CRCCHECK
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
Definition: defs.h:48
apng_reset_background
static void apng_reset_background(PNGDecContext *s, const AVFrame *p)
Definition: pngdec.c:1358
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:649
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
output_frame
static int output_frame(PNGDecContext *s, AVFrame *f)
Definition: pngdec.c:1717
png_pass_mask
static const uint8_t png_pass_mask[NB_PASSES]
Definition: pngdec.c:129
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
bytestream2_tell
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
PNGDecContext::pass
int pass
Definition: pngdec.c:120
PNGDecContext::iccp_name
uint8_t iccp_name[82]
Definition: pngdec.c:70
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
handle_small_bpp
static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
Definition: pngdec.c:1103
PNG_FILTER_VALUE_NONE
#define PNG_FILTER_VALUE_NONE
Definition: png.h:40
f
f
Definition: af_crystalizer.c:121
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:442
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
PNGDecContext::gb
GetByteContext gb
Definition: pngdec.c:64
AVPacket::size
int size
Definition: packet.h:523
handle_p_frame_png
static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
Definition: pngdec.c:1247
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:312
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:240
codec_internal.h
PNGDecContext::white_point
uint32_t white_point[2]
Definition: pngdec.c:77
av_frame_copy
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:769
PNGDecContext::picture
ThreadFrame picture
Definition: pngdec.c:66
png_put_interlaced_row
static void png_put_interlaced_row(uint8_t *dst, int width, int bits_per_pixel, int pass, int color_type, const uint8_t *src)
Definition: pngdec.c:146
AV_PIX_FMT_YA16BE
@ AV_PIX_FMT_YA16BE
16 bits gray, 16 bits alpha (big-endian)
Definition: pixfmt.h:209
PNG_FILTER_VALUE_AVG
#define PNG_FILTER_VALUE_AVG
Definition: png.h:43
size
int size
Definition: twinvq_data.h:10344
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
FF_CODEC_CAP_ALLOCATE_PROGRESS
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
Definition: codec_internal.h:69
ff_frame_new_side_data
int ff_frame_new_side_data(const AVCodecContext *avctx, AVFrame *frame, enum AVFrameSideDataType type, size_t size, AVFrameSideData **psd)
Wrapper around av_frame_new_side_data, which rejects side data overridden by the demuxer.
Definition: decode.c:1818
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
Definition: codec_internal.h:54
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
AVFrameSideData::data
uint8_t * data
Definition: frame.h:248
PNG_FILTER_VALUE_PAETH
#define PNG_FILTER_VALUE_PAETH
Definition: png.h:44
AV_RL24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:93
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
PNGDecContext::buffer
uint8_t * buffer
Definition: pngdec.c:118
PNGDecContext::zstream
FFZStream zstream
Definition: pngdec.c:125
PNG_FILTER_VALUE_UP
#define PNG_FILTER_VALUE_UP
Definition: png.h:42
FF_COMPLIANCE_NORMAL
#define FF_COMPLIANCE_NORMAL
Definition: defs.h:60
height
#define height
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
PNGDecContext::dispose_op
uint8_t dispose_op
Definition: pngdec.c:99
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
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:528
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:223
decode_trns_chunk
static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb)
Definition: pngdec.c:987
AV_STEREO3D_FLAG_INVERT
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:164
PNGSIG
#define PNGSIG
Definition: png.h:49
PNGDecContext::buffer_size
int buffer_size
Definition: pngdec.c:119
populate_avctx_color_fields
static int populate_avctx_color_fields(AVCodecContext *avctx, AVFrame *frame)
Definition: pngdec.c:660
FF_THREAD_FRAME
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1593
PNGDecContext::stereo_mode
int stereo_mode
Definition: pngdec.c:74
ff_pngdsp_init
av_cold void ff_pngdsp_init(PNGDSPContext *dsp)
Definition: pngdsp.c:43
av_image_get_linesize
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane.
Definition: imgutils.c:76
PNGDecContext::frame_metadata
AVDictionary * frame_metadata
Definition: pngdec.c:68
PNGDecContext::significant_bits
int significant_bits
Definition: pngdec.c:110
PNGDSPContext::add_bytes_l2
void(* add_bytes_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
Definition: pngdsp.h:28
PNG_FILTER_VALUE_SUB
#define PNG_FILTER_VALUE_SUB
Definition: png.h:41
bprint.h
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
PNGDecContext::mdvc_primaries
uint16_t mdvc_primaries[3][2]
Definition: pngdec.c:89
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
PNGDecContext::x_offset
int x_offset
Definition: pngdec.c:98
PNGDecContext::display_primaries
uint32_t display_primaries[3][2]
Definition: pngdec.c:78
av_bprint_get_buffer
void av_bprint_get_buffer(AVBPrint *buf, unsigned size, unsigned char **mem, unsigned *actual_size)
Allocate bytes in the buffer for external use.
Definition: bprint.c:223
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
png_handle_row
static void png_handle_row(PNGDecContext *s, uint8_t *dst, ptrdiff_t dst_stride)
Definition: pngdec.c:363
av_fast_padded_malloc
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:52
FF_DEBUG_STARTCODE
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:1404
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
decode_fctl_chunk
static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb)
Definition: pngdec.c:1176
decode_plte_chunk
static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb)
Definition: pngdec.c:964
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:534
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
update_thread_context
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have update_thread_context() run it in the next thread. Add AV_CODEC_CAP_FRAME_THREADS to the codec capabilities. There will be very little speed gain at this point but it should work. If there are inter-frame dependencies
AVMasteringDisplayMetadata
Mastering display metadata capable of representing the color volume of the display used to master the...
Definition: mastering_display_metadata.h:38
ff_inflate_end
void ff_inflate_end(FFZStream *zstream)
Wrapper around inflateEnd().
GetByteContext::buffer_end
const uint8_t * buffer_end
Definition: bytestream.h:34
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
ff_thread_get_ext_buffer
int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around ff_get_buffer() for frame-multithreaded codecs.
Definition: pthread_frame.c:968
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:599
PNGDecContext::avctx
AVCodecContext * avctx
Definition: pngdec.c:62
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:666
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
avcodec.h
PNGHeaderState
PNGHeaderState
Definition: pngdec.c:50
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
tag
uint32_t tag
Definition: movenc.c:1786
ret
ret
Definition: filter_design.txt:187
pixfmt.h
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
PNGDecContext::iccp_data_len
size_t iccp_data_len
Definition: pngdec.c:72
AVCodecContext::strict_std_compliance
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:1379
AVStereo3D::type
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:177
PNGDecContext::last_picture
ThreadFrame last_picture
Definition: pngdec.c:65
ff_decode_content_light_new
int ff_decode_content_light_new(const AVCodecContext *avctx, AVFrame *frame, AVContentLightMetadata **clm)
Wrapper around av_content_light_metadata_create_side_data(), which rejects side data overridden by th...
Definition: decode.c:1873
av_bprintf
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:99
ff_thread_finish_setup
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call ff_thread_finish_setup() afterwards. If some code can 't be moved
PNGDecContext::compression_type
int compression_type
Definition: pngdec.c:102
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
U
#define U(x)
Definition: vpx_arith.h:37
iso88591_to_utf8
static char * iso88591_to_utf8(const char *in, size_t size_in)
Definition: pngdec.c:517
PNG_IHDR
@ PNG_IHDR
Definition: pngdec.c:51
ff_png_filter_row
void ff_png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type, uint8_t *src, uint8_t *last, int size, int bpp)
Definition: pngdec.c:282
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVFrame::height
int height
Definition: frame.h:412
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1601
ThreadFrame
Definition: threadframe.h:27
decode_frame_common
static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, AVFrame *p, const AVPacket *avpkt)
Definition: pngdec.c:1374
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
UNROLL_FILTER
#define UNROLL_FILTER(op)
Definition: pngdec.c:267
AVRational::den
int den
Denominator.
Definition: rational.h:60
mode
mode
Definition: ebur128.h:83
PNGDecContext::transparent_color_be
uint8_t transparent_color_be[6]
Definition: pngdec.c:109
av_fast_padded_mallocz
void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_padded_malloc except that buffer will always be 0-initialized after call.
Definition: utils.c:65
PNGDecContext::have_chrm
int have_chrm
Definition: pngdec.c:76
png_pass_dsp_mask
static const uint8_t png_pass_dsp_mask[NB_PASSES]
Definition: pngdec.c:139
AVCodecContext::discard_damaged_percentage
int discard_damaged_percentage
The percentage of damaged samples to discard a frame.
Definition: avcodec.h:1969
AVMasteringDisplayMetadata::min_luminance
AVRational min_luminance
Min luminance of mastering display (cd/m^2).
Definition: mastering_display_metadata.h:52
AVCodecContext::debug
int debug
debug
Definition: avcodec.h:1396
PNG_IDAT
@ PNG_IDAT
Definition: pngdec.c:56
AV_CRC_32_IEEE_LE
@ AV_CRC_32_IEEE_LE
Definition: crc.h:53
decode_iccp_chunk
static int decode_iccp_chunk(PNGDecContext *s, GetByteContext *gb)
Definition: pngdec.c:1035
desc
const char * desc
Definition: libsvtav1.c:73
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FFZStream
Definition: zlib_wrapper.h:27
PNGDecContext::mdvc_white_point
uint16_t mdvc_white_point[2]
Definition: pngdec.c:90
mastering_display_metadata.h
decode_ihdr_chunk
static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb)
Definition: pngdec.c:588
PNGDecContext::dsp
PNGDSPContext dsp
Definition: pngdec.c:61
av_stereo3d_create_side_data
AVStereo3D * av_stereo3d_create_side_data(AVFrame *frame)
Allocate a complete AVFrameSideData and add it to the frame.
Definition: stereo3d.c:34
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:246
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:470
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
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
ff_inflate_init
int ff_inflate_init(FFZStream *zstream, void *logctx)
Wrapper around inflateInit().
d
d
Definition: ffmpeg_filter.c:425
bytestream.h
APNG_BLEND_OP_SOURCE
@ APNG_BLEND_OP_SOURCE
Definition: apng.h:37
imgutils.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
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
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVCOL_TRC_SMPTE428
@ AVCOL_TRC_SMPTE428
SMPTE ST 428-1.
Definition: pixfmt.h:599
MNGSIG
#define MNGSIG
Definition: png.h:50
PNGDecContext::cicp_trc
enum AVColorTransferCharacteristic cicp_trc
Definition: pngdec.c:83
PNGDecContext::interlace_type
int interlace_type
Definition: pngdec.c:103
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
handle_p_frame_apng
static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s, AVFrame *p)
Definition: pngdec.c:1269
PNGDecContext::y
int y
Definition: pngdec.c:124
av_image_check_size
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:318
OP_SUB
#define OP_SUB(x, s, l)
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:648
decode_zbuf
static int decode_zbuf(AVBPrint *bp, const uint8_t *data, const uint8_t *data_end, void *logctx)
Definition: pngdec.c:475
PNGDecContext::cur_w
int cur_w
Definition: pngdec.c:97
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:642
PNG_COLOR_TYPE_PALETTE
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:34
AV_DICT_DONT_STRDUP_KEY
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function.
Definition: dict.h:77
PNGDecContext::bpp
int bpp
Definition: pngdec.c:107
av_get_pix_fmt_name
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2882
PNGDecContext::has_trns
int has_trns
Definition: pngdec.c:108
av_fourcc2str
#define av_fourcc2str(fourcc)
Definition: avutil.h:345