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