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