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