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