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