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