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