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