FFmpeg  4.1.11
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 "libavutil/avassert.h"
25 #include "libavutil/bprint.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/stereo3d.h"
29 
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "internal.h"
33 #include "apng.h"
34 #include "png.h"
35 #include "pngdsp.h"
36 #include "thread.h"
37 
38 #include <zlib.h>
39 
41  PNG_IHDR = 1 << 0,
42  PNG_PLTE = 1 << 1,
43 };
44 
46  PNG_IDAT = 1 << 0,
47  PNG_ALLIMAGE = 1 << 1,
48 };
49 
50 typedef struct PNGDecContext {
53 
58 
61  int width, height;
62  int cur_w, cur_h;
63  int last_w, last_h;
68  int bit_depth;
73  int channels;
75  int bpp;
76  int has_trns;
78 
81  uint32_t palette[256];
84  unsigned int last_row_size;
86  unsigned int tmp_row_size;
89  int pass;
90  int crow_size; /* compressed row size (include filter type) */
91  int row_size; /* decompressed row size */
92  int pass_row_size; /* decompress row size of the current pass */
93  int y;
94  z_stream zstream;
96 
97 /* Mask to determine which pixels are valid in a pass */
98 static const uint8_t png_pass_mask[NB_PASSES] = {
99  0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
100 };
101 
102 /* Mask to determine which y pixels can be written in a pass */
104  0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
105 };
106 
107 /* Mask to determine which pixels to overwrite while displaying */
109  0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
110 };
111 
112 /* NOTE: we try to construct a good looking image at each pass. width
113  * is the original image width. We also do pixel format conversion at
114  * this stage */
115 static void png_put_interlaced_row(uint8_t *dst, int width,
116  int bits_per_pixel, int pass,
117  int color_type, const uint8_t *src)
118 {
119  int x, mask, dsp_mask, j, src_x, b, bpp;
120  uint8_t *d;
121  const uint8_t *s;
122 
123  mask = png_pass_mask[pass];
124  dsp_mask = png_pass_dsp_mask[pass];
125 
126  switch (bits_per_pixel) {
127  case 1:
128  src_x = 0;
129  for (x = 0; x < width; x++) {
130  j = (x & 7);
131  if ((dsp_mask << j) & 0x80) {
132  b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
133  dst[x >> 3] &= 0xFF7F>>j;
134  dst[x >> 3] |= b << (7 - j);
135  }
136  if ((mask << j) & 0x80)
137  src_x++;
138  }
139  break;
140  case 2:
141  src_x = 0;
142  for (x = 0; x < width; x++) {
143  int j2 = 2 * (x & 3);
144  j = (x & 7);
145  if ((dsp_mask << j) & 0x80) {
146  b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
147  dst[x >> 2] &= 0xFF3F>>j2;
148  dst[x >> 2] |= b << (6 - j2);
149  }
150  if ((mask << j) & 0x80)
151  src_x++;
152  }
153  break;
154  case 4:
155  src_x = 0;
156  for (x = 0; x < width; x++) {
157  int j2 = 4*(x&1);
158  j = (x & 7);
159  if ((dsp_mask << j) & 0x80) {
160  b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
161  dst[x >> 1] &= 0xFF0F>>j2;
162  dst[x >> 1] |= b << (4 - j2);
163  }
164  if ((mask << j) & 0x80)
165  src_x++;
166  }
167  break;
168  default:
169  bpp = bits_per_pixel >> 3;
170  d = dst;
171  s = src;
172  for (x = 0; x < width; x++) {
173  j = x & 7;
174  if ((dsp_mask << j) & 0x80) {
175  memcpy(d, s, bpp);
176  }
177  d += bpp;
178  if ((mask << j) & 0x80)
179  s += bpp;
180  }
181  break;
182  }
183 }
184 
186  int w, int bpp)
187 {
188  int i;
189  for (i = 0; i < w; i++) {
190  int a, b, c, p, pa, pb, pc;
191 
192  a = dst[i - bpp];
193  b = top[i];
194  c = top[i - bpp];
195 
196  p = b - c;
197  pc = a - c;
198 
199  pa = abs(p);
200  pb = abs(pc);
201  pc = abs(p + pc);
202 
203  if (pa <= pb && pa <= pc)
204  p = a;
205  else if (pb <= pc)
206  p = b;
207  else
208  p = c;
209  dst[i] = p + src[i];
210  }
211 }
212 
213 #define UNROLL1(bpp, op) \
214  { \
215  r = dst[0]; \
216  if (bpp >= 2) \
217  g = dst[1]; \
218  if (bpp >= 3) \
219  b = dst[2]; \
220  if (bpp >= 4) \
221  a = dst[3]; \
222  for (; i <= size - bpp; i += bpp) { \
223  dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
224  if (bpp == 1) \
225  continue; \
226  dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
227  if (bpp == 2) \
228  continue; \
229  dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
230  if (bpp == 3) \
231  continue; \
232  dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
233  } \
234  }
235 
236 #define UNROLL_FILTER(op) \
237  if (bpp == 1) { \
238  UNROLL1(1, op) \
239  } else if (bpp == 2) { \
240  UNROLL1(2, op) \
241  } else if (bpp == 3) { \
242  UNROLL1(3, op) \
243  } else if (bpp == 4) { \
244  UNROLL1(4, op) \
245  } \
246  for (; i < size; i++) { \
247  dst[i] = op(dst[i - bpp], src[i], last[i]); \
248  }
249 
250 /* NOTE: 'dst' can be equal to 'last' */
252  uint8_t *src, uint8_t *last, int size, int bpp)
253 {
254  int i, p, r, g, b, a;
255 
256  switch (filter_type) {
258  memcpy(dst, src, size);
259  break;
261  for (i = 0; i < bpp; i++)
262  dst[i] = src[i];
263  if (bpp == 4) {
264  p = *(int *)dst;
265  for (; i < size; i += bpp) {
266  unsigned s = *(int *)(src + i);
267  p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
268  *(int *)(dst + i) = p;
269  }
270  } else {
271 #define OP_SUB(x, s, l) ((x) + (s))
273  }
274  break;
275  case PNG_FILTER_VALUE_UP:
276  dsp->add_bytes_l2(dst, src, last, size);
277  break;
279  for (i = 0; i < bpp; i++) {
280  p = (last[i] >> 1);
281  dst[i] = p + src[i];
282  }
283 #define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
285  break;
287  for (i = 0; i < bpp; i++) {
288  p = last[i];
289  dst[i] = p + src[i];
290  }
291  if (bpp > 2 && size > 4) {
292  /* would write off the end of the array if we let it process
293  * the last pixel with bpp=3 */
294  int w = (bpp & 3) ? size - 3 : size;
295 
296  if (w > i) {
297  dsp->add_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
298  i = w;
299  }
300  }
301  ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
302  break;
303  }
304 }
305 
306 /* This used to be called "deloco" in FFmpeg
307  * and is actually an inverse reversible colorspace transformation */
308 #define YUV2RGB(NAME, TYPE) \
309 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
310 { \
311  int i; \
312  for (i = 0; i < size - 2; i += 3 + alpha) { \
313  int g = dst [i + 1]; \
314  dst[i + 0] += g; \
315  dst[i + 2] += g; \
316  } \
317 }
318 
319 YUV2RGB(rgb8, uint8_t)
320 YUV2RGB(rgb16, uint16_t)
321 
322 /* process exactly one decompressed row */
324 {
325  uint8_t *ptr, *last_row;
326  int got_line;
327 
328  if (!s->interlace_type) {
329  ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
330  if (s->y == 0)
331  last_row = s->last_row;
332  else
333  last_row = ptr - s->image_linesize;
334 
335  png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
336  last_row, s->row_size, s->bpp);
337  /* loco lags by 1 row so that it doesn't interfere with top prediction */
338  if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
339  if (s->bit_depth == 16) {
340  deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
341  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
342  } else {
343  deloco_rgb8(ptr - s->image_linesize, s->row_size,
344  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
345  }
346  }
347  s->y++;
348  if (s->y == s->cur_h) {
349  s->pic_state |= PNG_ALLIMAGE;
350  if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
351  if (s->bit_depth == 16) {
352  deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
353  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
354  } else {
355  deloco_rgb8(ptr, s->row_size,
356  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
357  }
358  }
359  }
360  } else {
361  got_line = 0;
362  for (;;) {
363  ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
364  if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
365  /* if we already read one row, it is time to stop to
366  * wait for the next one */
367  if (got_line)
368  break;
369  png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
370  s->last_row, s->pass_row_size, s->bpp);
371  FFSWAP(uint8_t *, s->last_row, s->tmp_row);
372  FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size);
373  got_line = 1;
374  }
375  if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
376  png_put_interlaced_row(ptr, s->cur_w, s->bits_per_pixel, s->pass,
377  s->color_type, s->last_row);
378  }
379  s->y++;
380  if (s->y == s->cur_h) {
381  memset(s->last_row, 0, s->row_size);
382  for (;;) {
383  if (s->pass == NB_PASSES - 1) {
384  s->pic_state |= PNG_ALLIMAGE;
385  goto the_end;
386  } else {
387  s->pass++;
388  s->y = 0;
389  s->pass_row_size = ff_png_pass_row_size(s->pass,
390  s->bits_per_pixel,
391  s->cur_w);
392  s->crow_size = s->pass_row_size + 1;
393  if (s->pass_row_size != 0)
394  break;
395  /* skip pass if empty row */
396  }
397  }
398  }
399  }
400 the_end:;
401  }
402 }
403 
405 {
406  int ret;
407  s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
408  s->zstream.next_in = (unsigned char *)s->gb.buffer;
409  bytestream2_skip(&s->gb, length);
410 
411  /* decode one line if possible */
412  while (s->zstream.avail_in > 0) {
413  ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
414  if (ret != Z_OK && ret != Z_STREAM_END) {
415  av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
416  return AVERROR_EXTERNAL;
417  }
418  if (s->zstream.avail_out == 0) {
419  if (!(s->pic_state & PNG_ALLIMAGE)) {
420  png_handle_row(s);
421  }
422  s->zstream.avail_out = s->crow_size;
423  s->zstream.next_out = s->crow_buf;
424  }
425  if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
427  "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
428  return 0;
429  }
430  }
431  return 0;
432 }
433 
434 static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
435  const uint8_t *data_end)
436 {
437  z_stream zstream;
438  unsigned char *buf;
439  unsigned buf_size;
440  int ret;
441 
442  zstream.zalloc = ff_png_zalloc;
443  zstream.zfree = ff_png_zfree;
444  zstream.opaque = NULL;
445  if (inflateInit(&zstream) != Z_OK)
446  return AVERROR_EXTERNAL;
447  zstream.next_in = (unsigned char *)data;
448  zstream.avail_in = data_end - data;
450 
451  while (zstream.avail_in > 0) {
452  av_bprint_get_buffer(bp, 2, &buf, &buf_size);
453  if (buf_size < 2) {
454  ret = AVERROR(ENOMEM);
455  goto fail;
456  }
457  zstream.next_out = buf;
458  zstream.avail_out = buf_size - 1;
459  ret = inflate(&zstream, Z_PARTIAL_FLUSH);
460  if (ret != Z_OK && ret != Z_STREAM_END) {
461  ret = AVERROR_EXTERNAL;
462  goto fail;
463  }
464  bp->len += zstream.next_out - buf;
465  if (ret == Z_STREAM_END)
466  break;
467  }
468  inflateEnd(&zstream);
469  bp->str[bp->len] = 0;
470  return 0;
471 
472 fail:
473  inflateEnd(&zstream);
475  return ret;
476 }
477 
478 static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
479 {
480  size_t extra = 0, i;
481  uint8_t *out, *q;
482 
483  for (i = 0; i < size_in; i++)
484  extra += in[i] >= 0x80;
485  if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
486  return NULL;
487  q = out = av_malloc(size_in + extra + 1);
488  if (!out)
489  return NULL;
490  for (i = 0; i < size_in; i++) {
491  if (in[i] >= 0x80) {
492  *(q++) = 0xC0 | (in[i] >> 6);
493  *(q++) = 0x80 | (in[i] & 0x3F);
494  } else {
495  *(q++) = in[i];
496  }
497  }
498  *(q++) = 0;
499  return out;
500 }
501 
502 static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed,
503  AVDictionary **dict)
504 {
505  int ret, method;
506  const uint8_t *data = s->gb.buffer;
507  const uint8_t *data_end = data + length;
508  const uint8_t *keyword = data;
509  const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
510  uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
511  unsigned text_len;
512  AVBPrint bp;
513 
514  if (!keyword_end)
515  return AVERROR_INVALIDDATA;
516  data = keyword_end + 1;
517 
518  if (compressed) {
519  if (data == data_end)
520  return AVERROR_INVALIDDATA;
521  method = *(data++);
522  if (method)
523  return AVERROR_INVALIDDATA;
524  if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
525  return ret;
526  text_len = bp.len;
527  ret = av_bprint_finalize(&bp, (char **)&text);
528  if (ret < 0)
529  return ret;
530  } else {
531  text = (uint8_t *)data;
532  text_len = data_end - text;
533  }
534 
535  kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
536  txt_utf8 = iso88591_to_utf8(text, text_len);
537  if (text != data)
538  av_free(text);
539  if (!(kw_utf8 && txt_utf8)) {
540  av_free(kw_utf8);
541  av_free(txt_utf8);
542  return AVERROR(ENOMEM);
543  }
544 
545  av_dict_set(dict, kw_utf8, txt_utf8,
547  return 0;
548 }
549 
551  uint32_t length)
552 {
553  if (length != 13)
554  return AVERROR_INVALIDDATA;
555 
556  if (s->pic_state & PNG_IDAT) {
557  av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");
558  return AVERROR_INVALIDDATA;
559  }
560 
561  if (s->hdr_state & PNG_IHDR) {
562  av_log(avctx, AV_LOG_ERROR, "Multiple IHDR\n");
563  return AVERROR_INVALIDDATA;
564  }
565 
566  s->width = s->cur_w = bytestream2_get_be32(&s->gb);
567  s->height = s->cur_h = bytestream2_get_be32(&s->gb);
568  if (av_image_check_size(s->width, s->height, 0, avctx)) {
569  s->cur_w = s->cur_h = s->width = s->height = 0;
570  av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
571  return AVERROR_INVALIDDATA;
572  }
573  s->bit_depth = bytestream2_get_byte(&s->gb);
574  if (s->bit_depth != 1 && s->bit_depth != 2 && s->bit_depth != 4 &&
575  s->bit_depth != 8 && s->bit_depth != 16) {
576  av_log(avctx, AV_LOG_ERROR, "Invalid bit depth\n");
577  goto error;
578  }
579  s->color_type = bytestream2_get_byte(&s->gb);
580  s->compression_type = bytestream2_get_byte(&s->gb);
581  if (s->compression_type) {
582  av_log(avctx, AV_LOG_ERROR, "Invalid compression method %d\n", s->compression_type);
583  goto error;
584  }
585  s->filter_type = bytestream2_get_byte(&s->gb);
586  s->interlace_type = bytestream2_get_byte(&s->gb);
587  bytestream2_skip(&s->gb, 4); /* crc */
588  s->hdr_state |= PNG_IHDR;
589  if (avctx->debug & FF_DEBUG_PICT_INFO)
590  av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
591  "compression_type=%d filter_type=%d interlace_type=%d\n",
592  s->width, s->height, s->bit_depth, s->color_type,
594 
595  return 0;
596 error:
597  s->cur_w = s->cur_h = s->width = s->height = 0;
598  s->bit_depth = 8;
599  return AVERROR_INVALIDDATA;
600 }
601 
603 {
604  if (s->pic_state & PNG_IDAT) {
605  av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
606  return AVERROR_INVALIDDATA;
607  }
608  avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
609  avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
610  if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
611  avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
612  bytestream2_skip(&s->gb, 1); /* unit specifier */
613  bytestream2_skip(&s->gb, 4); /* crc */
614 
615  return 0;
616 }
617 
619  uint32_t length, AVFrame *p)
620 {
621  int ret;
622  size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
623 
624  if (!p)
625  return AVERROR_INVALIDDATA;
626  if (!(s->hdr_state & PNG_IHDR)) {
627  av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
628  return AVERROR_INVALIDDATA;
629  }
630  if (!(s->pic_state & PNG_IDAT)) {
631  /* init image info */
632  ret = ff_set_dimensions(avctx, s->width, s->height);
633  if (ret < 0)
634  return ret;
635 
637  s->bits_per_pixel = s->bit_depth * s->channels;
638  s->bpp = (s->bits_per_pixel + 7) >> 3;
639  s->row_size = (s->cur_w * s->bits_per_pixel + 7) >> 3;
640 
641  if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
643  avctx->pix_fmt = AV_PIX_FMT_RGB24;
644  } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
646  avctx->pix_fmt = AV_PIX_FMT_RGBA;
647  } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
649  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
650  } else if (s->bit_depth == 16 &&
652  avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
653  } else if (s->bit_depth == 16 &&
655  avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
656  } else if (s->bit_depth == 16 &&
658  avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
659  } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
661  avctx->pix_fmt = AV_PIX_FMT_PAL8;
662  } else if (s->bit_depth == 1 && s->bits_per_pixel == 1 && avctx->codec_id != AV_CODEC_ID_APNG) {
663  avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
664  } else if (s->bit_depth == 8 &&
666  avctx->pix_fmt = AV_PIX_FMT_YA8;
667  } else if (s->bit_depth == 16 &&
669  avctx->pix_fmt = AV_PIX_FMT_YA16BE;
670  } else {
672  "Bit depth %d color type %d",
673  s->bit_depth, s->color_type);
674  return AVERROR_PATCHWELCOME;
675  }
676 
677  if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
678  switch (avctx->pix_fmt) {
679  case AV_PIX_FMT_RGB24:
680  avctx->pix_fmt = AV_PIX_FMT_RGBA;
681  break;
682 
683  case AV_PIX_FMT_RGB48BE:
684  avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
685  break;
686 
687  case AV_PIX_FMT_GRAY8:
688  avctx->pix_fmt = AV_PIX_FMT_YA8;
689  break;
690 
691  case AV_PIX_FMT_GRAY16BE:
692  avctx->pix_fmt = AV_PIX_FMT_YA16BE;
693  break;
694 
695  default:
696  avpriv_request_sample(avctx, "bit depth %d "
697  "and color type %d with TRNS",
698  s->bit_depth, s->color_type);
699  return AVERROR_INVALIDDATA;
700  }
701 
702  s->bpp += byte_depth;
703  }
704 
705  if ((ret = ff_thread_get_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
706  return ret;
709  if ((ret = ff_thread_get_buffer(avctx, &s->previous_picture, AV_GET_BUFFER_FLAG_REF)) < 0)
710  return ret;
711  }
713  p->key_frame = 1;
715 
716  ff_thread_finish_setup(avctx);
717 
718  /* compute the compressed row size */
719  if (!s->interlace_type) {
720  s->crow_size = s->row_size + 1;
721  } else {
722  s->pass = 0;
724  s->bits_per_pixel,
725  s->cur_w);
726  s->crow_size = s->pass_row_size + 1;
727  }
728  ff_dlog(avctx, "row_size=%d crow_size =%d\n",
729  s->row_size, s->crow_size);
730  s->image_buf = p->data[0];
731  s->image_linesize = p->linesize[0];
732  /* copy the palette if needed */
733  if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
734  memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
735  /* empty row is used if differencing to the first row */
737  if (!s->last_row)
738  return AVERROR_INVALIDDATA;
739  if (s->interlace_type ||
742  if (!s->tmp_row)
743  return AVERROR_INVALIDDATA;
744  }
745  /* compressed row */
747  if (!s->buffer)
748  return AVERROR(ENOMEM);
749 
750  /* we want crow_buf+1 to be 16-byte aligned */
751  s->crow_buf = s->buffer + 15;
752  s->zstream.avail_out = s->crow_size;
753  s->zstream.next_out = s->crow_buf;
754  }
755 
756  s->pic_state |= PNG_IDAT;
757 
758  /* set image to non-transparent bpp while decompressing */
760  s->bpp -= byte_depth;
761 
762  ret = png_decode_idat(s, length);
763 
765  s->bpp += byte_depth;
766 
767  if (ret < 0)
768  return ret;
769 
770  bytestream2_skip(&s->gb, 4); /* crc */
771 
772  return 0;
773 }
774 
776  uint32_t length)
777 {
778  int n, i, r, g, b;
779 
780  if ((length % 3) != 0 || length > 256 * 3)
781  return AVERROR_INVALIDDATA;
782  /* read the palette */
783  n = length / 3;
784  for (i = 0; i < n; i++) {
785  r = bytestream2_get_byte(&s->gb);
786  g = bytestream2_get_byte(&s->gb);
787  b = bytestream2_get_byte(&s->gb);
788  s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
789  }
790  for (; i < 256; i++)
791  s->palette[i] = (0xFFU << 24);
792  s->hdr_state |= PNG_PLTE;
793  bytestream2_skip(&s->gb, 4); /* crc */
794 
795  return 0;
796 }
797 
799  uint32_t length)
800 {
801  int v, i;
802 
803  if (!(s->hdr_state & PNG_IHDR)) {
804  av_log(avctx, AV_LOG_ERROR, "trns before IHDR\n");
805  return AVERROR_INVALIDDATA;
806  }
807 
808  if (s->pic_state & PNG_IDAT) {
809  av_log(avctx, AV_LOG_ERROR, "trns after IDAT\n");
810  return AVERROR_INVALIDDATA;
811  }
812 
814  if (length > 256 || !(s->hdr_state & PNG_PLTE))
815  return AVERROR_INVALIDDATA;
816 
817  for (i = 0; i < length; i++) {
818  unsigned v = bytestream2_get_byte(&s->gb);
819  s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
820  }
821  } else if (s->color_type == PNG_COLOR_TYPE_GRAY || s->color_type == PNG_COLOR_TYPE_RGB) {
822  if ((s->color_type == PNG_COLOR_TYPE_GRAY && length != 2) ||
823  (s->color_type == PNG_COLOR_TYPE_RGB && length != 6) ||
824  s->bit_depth == 1)
825  return AVERROR_INVALIDDATA;
826 
827  for (i = 0; i < length / 2; i++) {
828  /* only use the least significant bits */
829  v = av_mod_uintp2(bytestream2_get_be16(&s->gb), s->bit_depth);
830 
831  if (s->bit_depth > 8)
832  AV_WB16(&s->transparent_color_be[2 * i], v);
833  else
834  s->transparent_color_be[i] = v;
835  }
836  } else {
837  return AVERROR_INVALIDDATA;
838  }
839 
840  bytestream2_skip(&s->gb, 4); /* crc */
841  s->has_trns = 1;
842 
843  return 0;
844 }
845 
847 {
848  int ret, cnt = 0;
849  uint8_t *data, profile_name[82];
850  AVBPrint bp;
851  AVFrameSideData *sd;
852 
853  while ((profile_name[cnt++] = bytestream2_get_byte(&s->gb)) && cnt < 81);
854  if (cnt > 80) {
855  av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid name!\n");
856  return AVERROR_INVALIDDATA;
857  }
858 
859  length = FFMAX(length - cnt, 0);
860 
861  if (bytestream2_get_byte(&s->gb) != 0) {
862  av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid compression!\n");
863  return AVERROR_INVALIDDATA;
864  }
865 
866  length = FFMAX(length - 1, 0);
867 
868  if ((ret = decode_zbuf(&bp, s->gb.buffer, s->gb.buffer + length)) < 0)
869  return ret;
870 
871  ret = av_bprint_finalize(&bp, (char **)&data);
872  if (ret < 0)
873  return ret;
874 
876  if (!sd) {
877  av_free(data);
878  return AVERROR(ENOMEM);
879  }
880 
881  av_dict_set(&sd->metadata, "name", profile_name, 0);
882  memcpy(sd->data, data, bp.len);
883  av_free(data);
884 
885  /* ICC compressed data and CRC */
886  bytestream2_skip(&s->gb, length + 4);
887 
888  return 0;
889 }
890 
892 {
893  if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
894  int i, j, k;
895  uint8_t *pd = p->data[0];
896  for (j = 0; j < s->height; j++) {
897  i = s->width / 8;
898  for (k = 7; k >= 1; k--)
899  if ((s->width&7) >= k)
900  pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
901  for (i--; i >= 0; i--) {
902  pd[8*i + 7]= pd[i] & 1;
903  pd[8*i + 6]= (pd[i]>>1) & 1;
904  pd[8*i + 5]= (pd[i]>>2) & 1;
905  pd[8*i + 4]= (pd[i]>>3) & 1;
906  pd[8*i + 3]= (pd[i]>>4) & 1;
907  pd[8*i + 2]= (pd[i]>>5) & 1;
908  pd[8*i + 1]= (pd[i]>>6) & 1;
909  pd[8*i + 0]= pd[i]>>7;
910  }
911  pd += s->image_linesize;
912  }
913  } else if (s->bits_per_pixel == 2) {
914  int i, j;
915  uint8_t *pd = p->data[0];
916  for (j = 0; j < s->height; j++) {
917  i = s->width / 4;
919  if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
920  if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
921  if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
922  for (i--; i >= 0; i--) {
923  pd[4*i + 3]= pd[i] & 3;
924  pd[4*i + 2]= (pd[i]>>2) & 3;
925  pd[4*i + 1]= (pd[i]>>4) & 3;
926  pd[4*i + 0]= pd[i]>>6;
927  }
928  } else {
929  if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
930  if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
931  if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
932  for (i--; i >= 0; i--) {
933  pd[4*i + 3]= ( pd[i] & 3)*0x55;
934  pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
935  pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
936  pd[4*i + 0]= ( pd[i]>>6 )*0x55;
937  }
938  }
939  pd += s->image_linesize;
940  }
941  } else if (s->bits_per_pixel == 4) {
942  int i, j;
943  uint8_t *pd = p->data[0];
944  for (j = 0; j < s->height; j++) {
945  i = s->width/2;
947  if (s->width&1) pd[2*i+0]= pd[i]>>4;
948  for (i--; i >= 0; i--) {
949  pd[2*i + 1] = pd[i] & 15;
950  pd[2*i + 0] = pd[i] >> 4;
951  }
952  } else {
953  if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
954  for (i--; i >= 0; i--) {
955  pd[2*i + 1] = (pd[i] & 15) * 0x11;
956  pd[2*i + 0] = (pd[i] >> 4) * 0x11;
957  }
958  }
959  pd += s->image_linesize;
960  }
961  }
962 }
963 
965  uint32_t length)
966 {
967  uint32_t sequence_number;
969 
970  if (length != 26)
971  return AVERROR_INVALIDDATA;
972 
973  if (!(s->hdr_state & PNG_IHDR)) {
974  av_log(avctx, AV_LOG_ERROR, "fctl before IHDR\n");
975  return AVERROR_INVALIDDATA;
976  }
977 
978  if (s->pic_state & PNG_IDAT) {
979  av_log(avctx, AV_LOG_ERROR, "fctl after IDAT\n");
980  return AVERROR_INVALIDDATA;
981  }
982 
983  s->last_w = s->cur_w;
984  s->last_h = s->cur_h;
985  s->last_x_offset = s->x_offset;
986  s->last_y_offset = s->y_offset;
987  s->last_dispose_op = s->dispose_op;
988 
989  sequence_number = bytestream2_get_be32(&s->gb);
990  cur_w = bytestream2_get_be32(&s->gb);
991  cur_h = bytestream2_get_be32(&s->gb);
992  x_offset = bytestream2_get_be32(&s->gb);
993  y_offset = bytestream2_get_be32(&s->gb);
994  bytestream2_skip(&s->gb, 4); /* delay_num (2), delay_den (2) */
995  dispose_op = bytestream2_get_byte(&s->gb);
996  blend_op = bytestream2_get_byte(&s->gb);
997  bytestream2_skip(&s->gb, 4); /* crc */
998 
999  if (sequence_number == 0 &&
1000  (cur_w != s->width ||
1001  cur_h != s->height ||
1002  x_offset != 0 ||
1003  y_offset != 0) ||
1004  cur_w <= 0 || cur_h <= 0 ||
1005  x_offset < 0 || y_offset < 0 ||
1006  cur_w > s->width - x_offset|| cur_h > s->height - y_offset)
1007  return AVERROR_INVALIDDATA;
1008 
1009  if (blend_op != APNG_BLEND_OP_OVER && blend_op != APNG_BLEND_OP_SOURCE) {
1010  av_log(avctx, AV_LOG_ERROR, "Invalid blend_op %d\n", blend_op);
1011  return AVERROR_INVALIDDATA;
1012  }
1013 
1014  if ((sequence_number == 0 || !s->previous_picture.f->data[0]) &&
1015  dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
1016  // No previous frame to revert to for the first frame
1017  // Spec says to just treat it as a APNG_DISPOSE_OP_BACKGROUND
1018  dispose_op = APNG_DISPOSE_OP_BACKGROUND;
1019  }
1020 
1021  if (blend_op == APNG_BLEND_OP_OVER && !s->has_trns && (
1022  avctx->pix_fmt == AV_PIX_FMT_RGB24 ||
1023  avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1024  avctx->pix_fmt == AV_PIX_FMT_PAL8 ||
1025  avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
1026  avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
1027  avctx->pix_fmt == AV_PIX_FMT_MONOBLACK
1028  )) {
1029  // APNG_BLEND_OP_OVER is the same as APNG_BLEND_OP_SOURCE when there is no alpha channel
1030  blend_op = APNG_BLEND_OP_SOURCE;
1031  }
1032 
1033  s->cur_w = cur_w;
1034  s->cur_h = cur_h;
1035  s->x_offset = x_offset;
1036  s->y_offset = y_offset;
1037  s->dispose_op = dispose_op;
1038  s->blend_op = blend_op;
1039 
1040  return 0;
1041 }
1042 
1044 {
1045  int i, j;
1046  uint8_t *pd = p->data[0];
1047  uint8_t *pd_last = s->last_picture.f->data[0];
1048  int ls = FFMIN(av_image_get_linesize(p->format, s->width, 0), s->width * s->bpp);
1049 
1050  ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
1051  for (j = 0; j < s->height; j++) {
1052  for (i = 0; i < ls; i++)
1053  pd[i] += pd_last[i];
1054  pd += s->image_linesize;
1055  pd_last += s->image_linesize;
1056  }
1057 }
1058 
1059 // divide by 255 and round to nearest
1060 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
1061 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
1062 
1064  AVFrame *p)
1065 {
1066  size_t x, y;
1067  uint8_t *buffer;
1068 
1069  if (s->blend_op == APNG_BLEND_OP_OVER &&
1070  avctx->pix_fmt != AV_PIX_FMT_RGBA &&
1071  avctx->pix_fmt != AV_PIX_FMT_GRAY8A &&
1072  avctx->pix_fmt != AV_PIX_FMT_PAL8) {
1073  avpriv_request_sample(avctx, "Blending with pixel format %s",
1074  av_get_pix_fmt_name(avctx->pix_fmt));
1075  return AVERROR_PATCHWELCOME;
1076  }
1077 
1078  buffer = av_malloc_array(s->image_linesize, s->height);
1079  if (!buffer)
1080  return AVERROR(ENOMEM);
1081 
1082 
1083  // Do the disposal operation specified by the last frame on the frame
1085  ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
1086  memcpy(buffer, s->last_picture.f->data[0], s->image_linesize * s->height);
1087 
1089  for (y = s->last_y_offset; y < s->last_y_offset + s->last_h; ++y)
1090  memset(buffer + s->image_linesize * y + s->bpp * s->last_x_offset, 0, s->bpp * s->last_w);
1091 
1092  memcpy(s->previous_picture.f->data[0], buffer, s->image_linesize * s->height);
1094  } else {
1095  ff_thread_await_progress(&s->previous_picture, INT_MAX, 0);
1096  memcpy(buffer, s->previous_picture.f->data[0], s->image_linesize * s->height);
1097  }
1098 
1099  // Perform blending
1100  if (s->blend_op == APNG_BLEND_OP_SOURCE) {
1101  for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
1102  size_t row_start = s->image_linesize * y + s->bpp * s->x_offset;
1103  memcpy(buffer + row_start, p->data[0] + row_start, s->bpp * s->cur_w);
1104  }
1105  } else { // APNG_BLEND_OP_OVER
1106  for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
1107  uint8_t *foreground = p->data[0] + s->image_linesize * y + s->bpp * s->x_offset;
1108  uint8_t *background = buffer + s->image_linesize * y + s->bpp * s->x_offset;
1109  for (x = s->x_offset; x < s->x_offset + s->cur_w; ++x, foreground += s->bpp, background += s->bpp) {
1110  size_t b;
1111  uint8_t foreground_alpha, background_alpha, output_alpha;
1112  uint8_t output[10];
1113 
1114  // Since we might be blending alpha onto alpha, we use the following equations:
1115  // output_alpha = foreground_alpha + (1 - foreground_alpha) * background_alpha
1116  // output = (foreground_alpha * foreground + (1 - foreground_alpha) * background_alpha * background) / output_alpha
1117 
1118  switch (avctx->pix_fmt) {
1119  case AV_PIX_FMT_RGBA:
1120  foreground_alpha = foreground[3];
1121  background_alpha = background[3];
1122  break;
1123 
1124  case AV_PIX_FMT_GRAY8A:
1125  foreground_alpha = foreground[1];
1126  background_alpha = background[1];
1127  break;
1128 
1129  case AV_PIX_FMT_PAL8:
1130  foreground_alpha = s->palette[foreground[0]] >> 24;
1131  background_alpha = s->palette[background[0]] >> 24;
1132  break;
1133  }
1134 
1135  if (foreground_alpha == 0)
1136  continue;
1137 
1138  if (foreground_alpha == 255) {
1139  memcpy(background, foreground, s->bpp);
1140  continue;
1141  }
1142 
1143  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1144  // TODO: Alpha blending with PAL8 will likely need the entire image converted over to RGBA first
1145  avpriv_request_sample(avctx, "Alpha blending palette samples");
1146  background[0] = foreground[0];
1147  continue;
1148  }
1149 
1150  output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);
1151 
1152  av_assert0(s->bpp <= 10);
1153 
1154  for (b = 0; b < s->bpp - 1; ++b) {
1155  if (output_alpha == 0) {
1156  output[b] = 0;
1157  } else if (background_alpha == 255) {
1158  output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);
1159  } else {
1160  output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);
1161  }
1162  }
1163  output[b] = output_alpha;
1164  memcpy(background, output, s->bpp);
1165  }
1166  }
1167  }
1168 
1169  // Copy blended buffer into the frame and free
1170  memcpy(p->data[0], buffer, s->image_linesize * s->height);
1171  av_free(buffer);
1172 
1173  return 0;
1174 }
1175 
1177  AVFrame *p, AVPacket *avpkt)
1178 {
1179  AVDictionary **metadatap = NULL;
1180  uint32_t tag, length;
1181  int decode_next_dat = 0;
1182  int i, ret;
1183 
1184  for (;;) {
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;
1203  ret = AVERROR_INVALIDDATA;
1204  goto fail;
1205  }
1206 
1207  length = bytestream2_get_be32(&s->gb);
1208  if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
1209  av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
1210  ret = AVERROR_INVALIDDATA;
1211  goto fail;
1212  }
1213  tag = bytestream2_get_le32(&s->gb);
1214  if (avctx->debug & FF_DEBUG_STARTCODE)
1215  av_log(avctx, AV_LOG_DEBUG, "png: tag=%s length=%u\n",
1216  av_fourcc2str(tag), length);
1217 
1218  if (avctx->codec_id == AV_CODEC_ID_PNG &&
1219  avctx->skip_frame == AVDISCARD_ALL) {
1220  switch(tag) {
1221  case MKTAG('I', 'H', 'D', 'R'):
1222  case MKTAG('p', 'H', 'Y', 's'):
1223  case MKTAG('t', 'E', 'X', 't'):
1224  case MKTAG('I', 'D', 'A', 'T'):
1225  case MKTAG('t', 'R', 'N', 'S'):
1226  break;
1227  default:
1228  goto skip_tag;
1229  }
1230  }
1231 
1232  metadatap = &p->metadata;
1233  switch (tag) {
1234  case MKTAG('I', 'H', 'D', 'R'):
1235  if ((ret = decode_ihdr_chunk(avctx, s, length)) < 0)
1236  goto fail;
1237  break;
1238  case MKTAG('p', 'H', 'Y', 's'):
1239  if ((ret = decode_phys_chunk(avctx, s)) < 0)
1240  goto fail;
1241  break;
1242  case MKTAG('f', 'c', 'T', 'L'):
1243  if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1244  goto skip_tag;
1245  if ((ret = decode_fctl_chunk(avctx, s, length)) < 0)
1246  goto fail;
1247  decode_next_dat = 1;
1248  break;
1249  case MKTAG('f', 'd', 'A', 'T'):
1250  if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1251  goto skip_tag;
1252  if (!decode_next_dat || length < 4) {
1253  ret = AVERROR_INVALIDDATA;
1254  goto fail;
1255  }
1256  bytestream2_get_be32(&s->gb);
1257  length -= 4;
1258  /* fallthrough */
1259  case MKTAG('I', 'D', 'A', 'T'):
1260  if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
1261  goto skip_tag;
1262  if ((ret = decode_idat_chunk(avctx, s, length, p)) < 0)
1263  goto fail;
1264  break;
1265  case MKTAG('P', 'L', 'T', 'E'):
1266  if (decode_plte_chunk(avctx, s, length) < 0)
1267  goto skip_tag;
1268  break;
1269  case MKTAG('t', 'R', 'N', 'S'):
1270  if (decode_trns_chunk(avctx, s, length) < 0)
1271  goto skip_tag;
1272  break;
1273  case MKTAG('t', 'E', 'X', 't'):
1274  if (decode_text_chunk(s, length, 0, metadatap) < 0)
1275  av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
1276  bytestream2_skip(&s->gb, length + 4);
1277  break;
1278  case MKTAG('z', 'T', 'X', 't'):
1279  if (decode_text_chunk(s, length, 1, metadatap) < 0)
1280  av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
1281  bytestream2_skip(&s->gb, length + 4);
1282  break;
1283  case MKTAG('s', 'T', 'E', 'R'): {
1284  int mode = bytestream2_get_byte(&s->gb);
1286  if (!stereo3d)
1287  goto fail;
1288 
1289  if (mode == 0 || mode == 1) {
1290  stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
1291  stereo3d->flags = mode ? 0 : AV_STEREO3D_FLAG_INVERT;
1292  } else {
1293  av_log(avctx, AV_LOG_WARNING,
1294  "Unknown value in sTER chunk (%d)\n", mode);
1295  }
1296  bytestream2_skip(&s->gb, 4); /* crc */
1297  break;
1298  }
1299  case MKTAG('i', 'C', 'C', 'P'): {
1300  if (!p)
1301  return AVERROR_INVALIDDATA;
1302  if ((ret = decode_iccp_chunk(s, length, p)) < 0)
1303  goto fail;
1304  break;
1305  }
1306  case MKTAG('c', 'H', 'R', 'M'): {
1308  if (!mdm) {
1309  ret = AVERROR(ENOMEM);
1310  goto fail;
1311  }
1312 
1313  mdm->white_point[0] = av_make_q(bytestream2_get_be32(&s->gb), 100000);
1314  mdm->white_point[1] = av_make_q(bytestream2_get_be32(&s->gb), 100000);
1315 
1316  /* RGB Primaries */
1317  for (i = 0; i < 3; i++) {
1318  mdm->display_primaries[i][0] = av_make_q(bytestream2_get_be32(&s->gb), 100000);
1319  mdm->display_primaries[i][1] = av_make_q(bytestream2_get_be32(&s->gb), 100000);
1320  }
1321 
1322  mdm->has_primaries = 1;
1323  bytestream2_skip(&s->gb, 4); /* crc */
1324  break;
1325  }
1326  case MKTAG('g', 'A', 'M', 'A'): {
1327  AVBPrint bp;
1328  char *gamma_str;
1329  int num = bytestream2_get_be32(&s->gb);
1330 
1332  av_bprintf(&bp, "%i/%i", num, 100000);
1333  ret = av_bprint_finalize(&bp, &gamma_str);
1334  if (ret < 0)
1335  return ret;
1336 
1337  av_dict_set(&p->metadata, "gamma", gamma_str, AV_DICT_DONT_STRDUP_VAL);
1338 
1339  bytestream2_skip(&s->gb, 4); /* crc */
1340  break;
1341  }
1342  case MKTAG('I', 'E', 'N', 'D'):
1343  if (!(s->pic_state & PNG_ALLIMAGE))
1344  av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
1345  if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT))) {
1346  ret = AVERROR_INVALIDDATA;
1347  goto fail;
1348  }
1349  bytestream2_skip(&s->gb, 4); /* crc */
1350  goto exit_loop;
1351  default:
1352  /* skip tag */
1353 skip_tag:
1354  bytestream2_skip(&s->gb, length + 4);
1355  break;
1356  }
1357  }
1358 exit_loop:
1359 
1360  if (!p)
1361  return AVERROR_INVALIDDATA;
1362 
1363  if (avctx->codec_id == AV_CODEC_ID_PNG &&
1364  avctx->skip_frame == AVDISCARD_ALL) {
1365  return 0;
1366  }
1367 
1368  if (s->bits_per_pixel <= 4)
1369  handle_small_bpp(s, p);
1370 
1371  /* apply transparency if needed */
1372  if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
1373  size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
1374  size_t raw_bpp = s->bpp - byte_depth;
1375  unsigned x, y;
1376 
1377  av_assert0(s->bit_depth > 1);
1378 
1379  for (y = 0; y < s->height; ++y) {
1380  uint8_t *row = &s->image_buf[s->image_linesize * y];
1381 
1382  /* since we're updating in-place, we have to go from right to left */
1383  for (x = s->width; x > 0; --x) {
1384  uint8_t *pixel = &row[s->bpp * (x - 1)];
1385  memmove(pixel, &row[raw_bpp * (x - 1)], raw_bpp);
1386 
1387  if (!memcmp(pixel, s->transparent_color_be, raw_bpp)) {
1388  memset(&pixel[raw_bpp], 0, byte_depth);
1389  } else {
1390  memset(&pixel[raw_bpp], 0xff, byte_depth);
1391  }
1392  }
1393  }
1394  }
1395 
1396  /* handle P-frames only if a predecessor frame is available */
1397  if (s->last_picture.f->data[0]) {
1398  if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
1399  && s->last_picture.f->width == p->width
1400  && s->last_picture.f->height== p->height
1401  && s->last_picture.f->format== p->format
1402  ) {
1403  if (CONFIG_PNG_DECODER && avctx->codec_id != AV_CODEC_ID_APNG)
1404  handle_p_frame_png(s, p);
1405  else if (CONFIG_APNG_DECODER &&
1406  s->previous_picture.f->width == p->width &&
1407  s->previous_picture.f->height== p->height &&
1408  s->previous_picture.f->format== p->format &&
1409  avctx->codec_id == AV_CODEC_ID_APNG &&
1410  (ret = handle_p_frame_apng(avctx, s, p)) < 0)
1411  goto fail;
1412  }
1413  }
1414  ff_thread_report_progress(&s->picture, INT_MAX, 0);
1416 
1417  return 0;
1418 
1419 fail:
1420  ff_thread_report_progress(&s->picture, INT_MAX, 0);
1422  return ret;
1423 }
1424 
1425 #if CONFIG_PNG_DECODER
1426 static int decode_frame_png(AVCodecContext *avctx,
1427  void *data, int *got_frame,
1428  AVPacket *avpkt)
1429 {
1430  PNGDecContext *const s = avctx->priv_data;
1431  const uint8_t *buf = avpkt->data;
1432  int buf_size = avpkt->size;
1433  AVFrame *p;
1434  int64_t sig;
1435  int ret;
1436 
1439  p = s->picture.f;
1440 
1441  bytestream2_init(&s->gb, buf, buf_size);
1442 
1443  /* check signature */
1444  sig = bytestream2_get_be64(&s->gb);
1445  if (sig != PNGSIG &&
1446  sig != MNGSIG) {
1447  av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature 0x%08"PRIX64".\n", sig);
1448  return AVERROR_INVALIDDATA;
1449  }
1450 
1451  s->y = s->has_trns = 0;
1452  s->hdr_state = 0;
1453  s->pic_state = 0;
1454 
1455  /* init the zlib */
1456  s->zstream.zalloc = ff_png_zalloc;
1457  s->zstream.zfree = ff_png_zfree;
1458  s->zstream.opaque = NULL;
1459  ret = inflateInit(&s->zstream);
1460  if (ret != Z_OK) {
1461  av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1462  return AVERROR_EXTERNAL;
1463  }
1464 
1465  if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1466  goto the_end;
1467 
1468  if (avctx->skip_frame == AVDISCARD_ALL) {
1469  *got_frame = 0;
1470  ret = bytestream2_tell(&s->gb);
1471  goto the_end;
1472  }
1473 
1474  if ((ret = av_frame_ref(data, s->picture.f)) < 0)
1475  goto the_end;
1476 
1477  *got_frame = 1;
1478 
1479  ret = bytestream2_tell(&s->gb);
1480 the_end:
1481  inflateEnd(&s->zstream);
1482  s->crow_buf = NULL;
1483  return ret;
1484 }
1485 #endif
1486 
1487 #if CONFIG_APNG_DECODER
1488 static int decode_frame_apng(AVCodecContext *avctx,
1489  void *data, int *got_frame,
1490  AVPacket *avpkt)
1491 {
1492  PNGDecContext *const s = avctx->priv_data;
1493  int ret;
1494  AVFrame *p;
1495 
1498  p = s->picture.f;
1499 
1500  if (!(s->hdr_state & PNG_IHDR)) {
1501  if (!avctx->extradata_size)
1502  return AVERROR_INVALIDDATA;
1503 
1504  /* only init fields, there is no zlib use in extradata */
1505  s->zstream.zalloc = ff_png_zalloc;
1506  s->zstream.zfree = ff_png_zfree;
1507 
1508  bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
1509  if ((ret = decode_frame_common(avctx, s, NULL, avpkt)) < 0)
1510  goto end;
1511  }
1512 
1513  /* reset state for a new frame */
1514  if ((ret = inflateInit(&s->zstream)) != Z_OK) {
1515  av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1516  ret = AVERROR_EXTERNAL;
1517  goto end;
1518  }
1519  s->y = 0;
1520  s->pic_state = 0;
1521  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1522  if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1523  goto end;
1524 
1525  if (!(s->pic_state & PNG_ALLIMAGE))
1526  av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
1527  if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT))) {
1528  ret = AVERROR_INVALIDDATA;
1529  goto end;
1530  }
1531  if ((ret = av_frame_ref(data, s->picture.f)) < 0)
1532  goto end;
1533 
1534  *got_frame = 1;
1535  ret = bytestream2_tell(&s->gb);
1536 
1537 end:
1538  inflateEnd(&s->zstream);
1539  return ret;
1540 }
1541 #endif
1542 
1543 #if HAVE_THREADS
1544 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1545 {
1546  PNGDecContext *psrc = src->priv_data;
1547  PNGDecContext *pdst = dst->priv_data;
1548  int ret;
1549 
1550  if (dst == src)
1551  return 0;
1552 
1553  ff_thread_release_buffer(dst, &pdst->picture);
1554  if (psrc->picture.f->data[0] &&
1555  (ret = ff_thread_ref_frame(&pdst->picture, &psrc->picture)) < 0)
1556  return ret;
1558  pdst->width = psrc->width;
1559  pdst->height = psrc->height;
1560  pdst->bit_depth = psrc->bit_depth;
1561  pdst->color_type = psrc->color_type;
1562  pdst->compression_type = psrc->compression_type;
1563  pdst->interlace_type = psrc->interlace_type;
1564  pdst->filter_type = psrc->filter_type;
1565  pdst->cur_w = psrc->cur_w;
1566  pdst->cur_h = psrc->cur_h;
1567  pdst->x_offset = psrc->x_offset;
1568  pdst->y_offset = psrc->y_offset;
1569  pdst->has_trns = psrc->has_trns;
1570  memcpy(pdst->transparent_color_be, psrc->transparent_color_be, sizeof(pdst->transparent_color_be));
1571 
1572  pdst->dispose_op = psrc->dispose_op;
1573 
1574  memcpy(pdst->palette, psrc->palette, sizeof(pdst->palette));
1575 
1576  pdst->hdr_state |= psrc->hdr_state;
1577 
1579  if (psrc->last_picture.f->data[0] &&
1580  (ret = ff_thread_ref_frame(&pdst->last_picture, &psrc->last_picture)) < 0)
1581  return ret;
1582 
1584  if (psrc->previous_picture.f->data[0] &&
1585  (ret = ff_thread_ref_frame(&pdst->previous_picture, &psrc->previous_picture)) < 0)
1586  return ret;
1587  }
1588 
1589  return 0;
1590 }
1591 #endif
1592 
1594 {
1595  PNGDecContext *s = avctx->priv_data;
1596 
1597  avctx->color_range = AVCOL_RANGE_JPEG;
1598 
1599  s->avctx = avctx;
1601  s->last_picture.f = av_frame_alloc();
1602  s->picture.f = av_frame_alloc();
1603  if (!s->previous_picture.f || !s->last_picture.f || !s->picture.f) {
1606  av_frame_free(&s->picture.f);
1607  return AVERROR(ENOMEM);
1608  }
1609 
1610  if (!avctx->internal->is_copy) {
1611  avctx->internal->allocate_progress = 1;
1612  ff_pngdsp_init(&s->dsp);
1613  }
1614 
1615  return 0;
1616 }
1617 
1619 {
1620  PNGDecContext *s = avctx->priv_data;
1621 
1626  ff_thread_release_buffer(avctx, &s->picture);
1627  av_frame_free(&s->picture.f);
1628  av_freep(&s->buffer);
1629  s->buffer_size = 0;
1630  av_freep(&s->last_row);
1631  s->last_row_size = 0;
1632  av_freep(&s->tmp_row);
1633  s->tmp_row_size = 0;
1634 
1635  return 0;
1636 }
1637 
1638 #if CONFIG_APNG_DECODER
1640  .name = "apng",
1641  .long_name = NULL_IF_CONFIG_SMALL("APNG (Animated Portable Network Graphics) image"),
1642  .type = AVMEDIA_TYPE_VIDEO,
1643  .id = AV_CODEC_ID_APNG,
1644  .priv_data_size = sizeof(PNGDecContext),
1645  .init = png_dec_init,
1646  .close = png_dec_end,
1647  .decode = decode_frame_apng,
1649  .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1650  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
1651  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1652 };
1653 #endif
1654 
1655 #if CONFIG_PNG_DECODER
1657  .name = "png",
1658  .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
1659  .type = AVMEDIA_TYPE_VIDEO,
1660  .id = AV_CODEC_ID_PNG,
1661  .priv_data_size = sizeof(PNGDecContext),
1662  .init = png_dec_init,
1663  .close = png_dec_end,
1664  .decode = decode_frame_png,
1666  .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1667  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
1669 };
1670 #endif
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:167
static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s, uint32_t length, AVFrame *p)
Definition: pngdec.c:618
static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s, uint32_t length)
Definition: pngdec.c:964
#define PNG_FILTER_VALUE_AVG
Definition: png.h:41
static void png_handle_row(PNGDecContext *s)
Definition: pngdec.c:323
ThreadFrame previous_picture
Definition: pngdec.c:55
#define NULL
Definition: coverity.c:32
int last_y_offset
Definition: pngdec.c:65
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
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
int width
Definition: pngdec.c:61
unsigned int tmp_row_size
Definition: pngdec.c:86
8 bits gray, 8 bits alpha
Definition: pixfmt.h:143
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static int init_thread_copy(AVCodecContext *avctx)
Definition: tta.c:399
AVFrame * f
Definition: thread.h:35
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
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:104
const char * g
Definition: vf_curves.c:115
int pass_row_size
Definition: pngdec.c:92
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVDictionary * metadata
Definition: frame.h:192
uint8_t * tmp_row
Definition: pngdec.c:85
#define avpriv_request_sample(...)
PNGHeaderState
Definition: pngdec.c:40
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2164
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
int num
Numerator.
Definition: rational.h:59
static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed, AVDictionary **dict)
Definition: pngdec.c:502
int size
Definition: avcodec.h:1446
const char * b
Definition: vf_curves.c:116
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:1912
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1743
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
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:70
enum PNGImageState pic_state
Definition: pngdec.c:60
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
discard all
Definition: avcodec.h:803
Views are next to each other.
Definition: stereo3d.h:67
#define PNG_COLOR_TYPE_RGB
Definition: png.h:33
void(* add_bytes_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
Definition: pngdsp.h:28
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition: png.h:35
#define src
Definition: vp8dsp.c:254
AVCodec.
Definition: avcodec.h:3424
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:32
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
int filter_type
Definition: pngdec.c:72
void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
Definition: pngdec.c:185
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that&#39;s been allocated with av_malloc() or another memory allocation function...
Definition: dict.h:73
#define PNG_FILTER_VALUE_PAETH
Definition: png.h:42
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:2991
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
int y_offset
Definition: pngdec.c:64
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:34
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
Stereo 3D type: this structure describes how two videos are packed within a single video surface...
Definition: stereo3d.h:176
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:2615
#define f(width, name)
Definition: cbs_vp9.c:255
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
Multithreading support functions.
AVCodec ff_apng_decoder
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:205
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:443
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1634
static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s)
Definition: pngdec.c:602
const char data[16]
Definition: mxf.c:91
Structure to hold side data for an AVFrame.
Definition: frame.h:188
uint8_t * data
Definition: avcodec.h:1445
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord)
Definition: vf_neighbor.c:189
const uint8_t * buffer
Definition: bytestream.h:34
uint32_t tag
Definition: movenc.c:1478
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:1867
#define ff_dlog(a,...)
AVDictionary * metadata
metadata.
Definition: frame.h:513
static int decode_iccp_chunk(PNGDecContext *s, int length, AVFrame *f)
Definition: pngdec.c:846
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:373
unsigned int last_row_size
Definition: pngdec.c:84
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
int cur_h
Definition: pngdec.c:62
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1477
static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s, uint32_t length)
Definition: pngdec.c:775
#define U(x)
Definition: vp56_arith.h:37
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
int width
Definition: frame.h:284
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static const uint8_t png_pass_dsp_mask[NB_PASSES]
Definition: pngdec.c:108
int flags
Additional information about the frame packing.
Definition: stereo3d.h:185
16 bits gray, 16 bits alpha (big-endian)
Definition: pixfmt.h:212
#define AV_BPRINT_SIZE_UNLIMITED
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, AVFrame *p, AVPacket *avpkt)
Definition: pngdec.c:1176
static const uint16_t mask[17]
Definition: lzw.c:38
#define OP_SUB(x, s, l)
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
Definition: internal.h:136
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
Definition: pngdec.c:1043
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
#define CONFIG_APNG_DECODER
Definition: config.h:712
uint8_t * crow_buf
Definition: pngdec.c:82
const char * r
Definition: vf_curves.c:114
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int pass
Definition: pngdec.c:89
int ff_png_get_nb_channels(int color_type)
Definition: png.c:49
ThreadFrame picture
Definition: pngdec.c:57
int height
Definition: pngdec.c:61
#define av_fourcc2str(fourcc)
Definition: avutil.h:348
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
#define PNGSIG
Definition: png.h:47
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: avcodec.h:3431
int bits_per_pixel
Definition: pngdec.c:74
GetByteContext gb
Definition: pngdec.c:54
#define FFMAX(a, b)
Definition: common.h:94
#define NB_PASSES
Definition: png.h:45
#define fail()
Definition: checkasm.h:117
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1024
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
uint8_t blend_op
Definition: pngdec.c:66
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:227
AVStereo3D * av_stereo3d_create_side_data(AVFrame *frame)
Allocate a complete AVFrameSideData and add it to the frame.
Definition: stereo3d.c:33
z_stream zstream
Definition: pngdec.c:94
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:282
AVMasteringDisplayMetadata * av_mastering_display_metadata_create_side_data(AVFrame *frame)
Allocate a complete AVMasteringDisplayMetadata and add it to the frame.
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:309
alias for AV_PIX_FMT_YA8
Definition: pixfmt.h:146
#define FFMIN(a, b)
Definition: common.h:96
#define PNG_FILTER_VALUE_SUB
Definition: png.h:39
uint32_t palette[256]
Definition: pngdec.c:81
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:31
static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type, uint8_t *src, uint8_t *last, int size, int bpp)
Definition: pngdec.c:251
uint8_t w
Definition: llviddspenc.c:38
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
#define s(width, name)
Definition: cbs_vp9.c:257
uint8_t * last_row
Definition: pngdec.c:83
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
Definition: frame.h:143
#define AV_RL32
Definition: intreadwrite.h:146
int n
Definition: avisynth_c.h:684
AVCodecContext * avctx
Definition: pngdec.c:52
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
if(ret< 0)
Definition: vf_mcdeint.c:279
av_cold void ff_pngdsp_init(PNGDSPContext *dsp)
Definition: pngdsp.c:43
static int decode_zbuf(AVBPrint *bp, const uint8_t *data, const uint8_t *data_end)
Definition: pngdec.c:434
static void error(const char *err)
int channels
Definition: pngdec.c:73
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:512
static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s, uint32_t length)
Definition: pngdec.c:550
static uint8_t * iso88591_to_utf8(const uint8_t *in, size_t size_in)
Definition: pngdec.c:478
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:299
static av_cold int png_dec_init(AVCodecContext *avctx)
Definition: pngdec.c:1593
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:180
Libavcodec external API header.
enum PNGHeaderState hdr_state
Definition: pngdec.c:59
int buffer_size
Definition: pngdec.c:88
static int skip_tag(AVIOContext *in, int32_t tag_name)
Definition: ismindex.c:132
enum AVCodecID codec_id
Definition: avcodec.h:1543
#define PNG_FILTER_VALUE_UP
Definition: png.h:40
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
#define PNG_FILTER_TYPE_LOCO
Definition: png.h:37
uint8_t last_dispose_op
Definition: pngdec.c:67
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
int debug
debug
Definition: avcodec.h:2614
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1533
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
Definition: avcodec.h:1558
uint8_t * data
Definition: frame.h:190
int interlace_type
Definition: pngdec.c:71
PNGImageState
Definition: pngdec.c:45
void * buf
Definition: avisynth_c.h:690
const uint8_t ff_png_pass_ymask[NB_PASSES]
Definition: png.c:25
int image_linesize
Definition: pngdec.c:80
int extradata_size
Definition: avcodec.h:1635
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
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, int size)
Add a new side data to a frame.
Definition: frame.c:722
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2595
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
Rational number (pair of numerator and denominator).
Definition: rational.h:58
Mastering display metadata capable of representing the color volume of the display used to master the...
int cur_w
Definition: pngdec.c:62
#define CONFIG_PNG_DECODER
Definition: config.h:861
uint8_t transparent_color_be[6]
Definition: pngdec.c:77
#define OP_AVG(x, s, l)
uint8_t * image_buf
Definition: pngdec.c:79
int allocate_progress
Whether to allocate progress for frame threading.
Definition: internal.h:151
uint8_t dispose_op
Definition: pngdec.c:66
AVRational display_primaries[3][2]
CIE 1931 xy chromaticity coords of color primaries (r, g, b order).
uint8_t pixel
Definition: tiny_ssim.c:42
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int last_x_offset
Definition: pngdec.c:65
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
#define FAST_DIV255(x)
Definition: pngdec.c:1061
static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s, AVFrame *p)
Definition: pngdec.c:1063
#define YUV2RGB(NAME, TYPE)
Definition: pngdec.c:308
static const uint8_t png_pass_mask[NB_PASSES]
Definition: pngdec.c:98
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:76
Y , 8bpp.
Definition: pixfmt.h:74
static av_cold int png_dec_end(AVCodecContext *avctx)
Definition: pngdec.c:1618
void(* add_paeth_prediction)(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
Definition: pngdsp.h:33
common internal api header.
static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
Definition: pngdec.c:891
#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: internal.h:60
#define PNG_FILTER_VALUE_NONE
Definition: png.h:38
static double c[64]
static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s, uint32_t length)
Definition: pngdec.c:798
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
int last_w
Definition: pngdec.c:63
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:82
static const uint8_t png_pass_dsp_ymask[NB_PASSES]
Definition: pngdec.c:103
Stereoscopic video.
int den
Denominator.
Definition: rational.h:60
void ff_png_zfree(void *opaque, void *ptr)
Definition: png.c:44
void * priv_data
Definition: avcodec.h:1560
static int png_decode_idat(PNGDecContext *s, int length)
Definition: pngdec.c:404
uint8_t * buffer
Definition: pngdec.c:87
#define av_free(p)
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:2628
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1568
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:304
int row_size
Definition: pngdec.c:91
APNG common header.
PNGDSPContext dsp
Definition: pngdec.c:51
int compression_type
Definition: pngdec.c:70
int last_h
Definition: pngdec.c:63
int ff_png_pass_row_size(int pass, int bits_per_pixel, int width)
Definition: png.c:62
int height
Definition: frame.h:284
FILE * out
Definition: movenc.c:54
int bit_depth
Definition: pngdec.c:68
#define av_freep(p)
int color_type
Definition: pngdec.c:69
ThreadFrame last_picture
Definition: pngdec.c:56
#define av_malloc_array(a, b)
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:115
#define FFSWAP(type, a, b)
Definition: common.h:99
const char int length
Definition: avisynth_c.h:768
int crow_size
Definition: pngdec.c:90
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:2362
int x_offset
Definition: pngdec.c:64
#define MKTAG(a, b, c, d)
Definition: common.h:366
void * ff_png_zalloc(void *opaque, unsigned int items, unsigned int size)
Definition: png.c:39
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
This structure stores compressed data.
Definition: avcodec.h:1422
int has_trns
Definition: pngdec.c:76
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1144
mode
Use these values in ebur128_init (or&#39;ed).
Definition: ebur128.h:83
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:968
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:2592
AVCodec ff_png_decoder
#define UNROLL_FILTER(op)
Definition: pngdec.c:236
#define MNGSIG
Definition: png.h:48