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