FFmpeg  4.4.9
pngdec.c
Go to the documentation of this file.
1 /*
2  * PNG image format
3  * Copyright (c) 2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 //#define DEBUG
23 
24 #include "libavutil/avassert.h"
25 #include "libavutil/bprint.h"
26 #include "libavutil/crc.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/stereo3d.h"
31 
32 #include "avcodec.h"
33 #include "bytestream.h"
34 #include "internal.h"
35 #include "apng.h"
36 #include "png.h"
37 #include "pngdsp.h"
38 #include "thread.h"
39 
40 #include <zlib.h>
41 
43  PNG_IHDR = 1 << 0,
44  PNG_PLTE = 1 << 1,
45 };
46 
48  PNG_IDAT = 1 << 0,
49  PNG_ALLIMAGE = 1 << 1,
50 };
51 
52 typedef struct PNGDecContext {
55 
59 
61 
64  size_t iccp_data_len;
65 
67 
68  int have_chrm;
69  uint32_t white_point[2];
70  uint32_t display_primaries[3][2];
71 
74  int width, height;
75  int cur_w, cur_h;
76  int last_w, last_h;
81  int bit_depth;
86  int channels;
88  int bpp;
89  int has_trns;
91 
94  uint32_t palette[256];
97  unsigned int last_row_size;
99  unsigned int tmp_row_size;
102  int pass;
103  int crow_size; /* compressed row size (include filter type) */
104  int row_size; /* decompressed row size */
105  int pass_row_size; /* decompress row size of the current pass */
106  int y;
107  /* sub-8-bit samples of an APNG frame are decoded packed at the start of
108  * the frame row and only moved to the frame position once unpacked */
110  z_stream zstream;
111 } PNGDecContext;
112 
113 /* Mask to determine which pixels are valid in a pass */
114 static const uint8_t png_pass_mask[NB_PASSES] = {
115  0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
116 };
117 
118 /* Mask to determine which y pixels can be written in a pass */
120  0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
121 };
122 
123 /* Mask to determine which pixels to overwrite while displaying */
125  0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
126 };
127 
128 /* NOTE: we try to construct a good looking image at each pass. width
129  * is the original image width. We also do pixel format conversion at
130  * this stage */
131 static void png_put_interlaced_row(uint8_t *dst, int width,
132  int bits_per_pixel, int pass,
133  int color_type, const uint8_t *src)
134 {
135  int x, mask, dsp_mask, j, src_x, b, bpp;
136  uint8_t *d;
137  const uint8_t *s;
138 
140  dsp_mask = png_pass_dsp_mask[pass];
141 
142  switch (bits_per_pixel) {
143  case 1:
144  src_x = 0;
145  for (x = 0; x < width; x++) {
146  j = (x & 7);
147  if ((dsp_mask << j) & 0x80) {
148  b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
149  dst[x >> 3] &= 0xFF7F>>j;
150  dst[x >> 3] |= b << (7 - j);
151  }
152  if ((mask << j) & 0x80)
153  src_x++;
154  }
155  break;
156  case 2:
157  src_x = 0;
158  for (x = 0; x < width; x++) {
159  int j2 = 2 * (x & 3);
160  j = (x & 7);
161  if ((dsp_mask << j) & 0x80) {
162  b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
163  dst[x >> 2] &= 0xFF3F>>j2;
164  dst[x >> 2] |= b << (6 - j2);
165  }
166  if ((mask << j) & 0x80)
167  src_x++;
168  }
169  break;
170  case 4:
171  src_x = 0;
172  for (x = 0; x < width; x++) {
173  int j2 = 4*(x&1);
174  j = (x & 7);
175  if ((dsp_mask << j) & 0x80) {
176  b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
177  dst[x >> 1] &= 0xFF0F>>j2;
178  dst[x >> 1] |= b << (4 - j2);
179  }
180  if ((mask << j) & 0x80)
181  src_x++;
182  }
183  break;
184  default:
185  bpp = bits_per_pixel >> 3;
186  d = dst;
187  s = src;
188  for (x = 0; x < width; x++) {
189  j = x & 7;
190  if ((dsp_mask << j) & 0x80) {
191  memcpy(d, s, bpp);
192  }
193  d += bpp;
194  if ((mask << j) & 0x80)
195  s += bpp;
196  }
197  break;
198  }
199 }
200 
202  int w, int bpp)
203 {
204  int i;
205  for (i = 0; i < w; i++) {
206  int a, b, c, p, pa, pb, pc;
207 
208  a = dst[i - bpp];
209  b = top[i];
210  c = top[i - bpp];
211 
212  p = b - c;
213  pc = a - c;
214 
215  pa = abs(p);
216  pb = abs(pc);
217  pc = abs(p + pc);
218 
219  if (pa <= pb && pa <= pc)
220  p = a;
221  else if (pb <= pc)
222  p = b;
223  else
224  p = c;
225  dst[i] = p + src[i];
226  }
227 }
228 
229 #define UNROLL1(bpp, op) \
230  { \
231  r = dst[0]; \
232  if (bpp >= 2) \
233  g = dst[1]; \
234  if (bpp >= 3) \
235  b = dst[2]; \
236  if (bpp >= 4) \
237  a = dst[3]; \
238  for (; i <= size - bpp; i += bpp) { \
239  dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
240  if (bpp == 1) \
241  continue; \
242  dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
243  if (bpp == 2) \
244  continue; \
245  dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
246  if (bpp == 3) \
247  continue; \
248  dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
249  } \
250  }
251 
252 #define UNROLL_FILTER(op) \
253  if (bpp == 1) { \
254  UNROLL1(1, op) \
255  } else if (bpp == 2) { \
256  UNROLL1(2, op) \
257  } else if (bpp == 3) { \
258  UNROLL1(3, op) \
259  } else if (bpp == 4) { \
260  UNROLL1(4, op) \
261  } \
262  for (; i < size; i++) { \
263  dst[i] = op(dst[i - bpp], src[i], last[i]); \
264  }
265 
266 /* NOTE: 'dst' can be equal to 'last' */
267 void ff_png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
268  uint8_t *src, uint8_t *last, int size, int bpp)
269 {
270  int i, p, r, g, b, a;
271 
272  switch (filter_type) {
274  memcpy(dst, src, size);
275  break;
277  for (i = 0; i < bpp; i++)
278  dst[i] = src[i];
279  if (bpp == 4) {
280  p = *(int *)dst;
281  for (; i < size; i += bpp) {
282  unsigned s = *(int *)(src + i);
283  p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
284  *(int *)(dst + i) = p;
285  }
286  } else {
287 #define OP_SUB(x, s, l) ((x) + (s))
289  }
290  break;
291  case PNG_FILTER_VALUE_UP:
292  dsp->add_bytes_l2(dst, src, last, size);
293  break;
295  for (i = 0; i < bpp; i++) {
296  p = (last[i] >> 1);
297  dst[i] = p + src[i];
298  }
299 #define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
301  break;
303  for (i = 0; i < bpp; i++) {
304  p = last[i];
305  dst[i] = p + src[i];
306  }
307  if (bpp > 2 && size > 4) {
308  /* would write off the end of the array if we let it process
309  * the last pixel with bpp=3 */
310  int w = (bpp & 3) ? size - 3 : size;
311 
312  if (w > i) {
313  dsp->add_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
314  i = w;
315  }
316  }
317  ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
318  break;
319  }
320 }
321 
322 /* This used to be called "deloco" in FFmpeg
323  * and is actually an inverse reversible colorspace transformation */
324 #define YUV2RGB(NAME, TYPE) \
325 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
326 { \
327  int i; \
328  for (i = 0; i < size - 2; i += 3 + alpha) { \
329  int g = dst [i + 1]; \
330  dst[i + 0] += g; \
331  dst[i + 2] += g; \
332  } \
333 }
334 
335 YUV2RGB(rgb8, uint8_t)
336 YUV2RGB(rgb16, uint16_t)
337 
339 {
340  if (s->interlace_type) {
341  return 100 - 100 * s->pass / (NB_PASSES - 1);
342  } else {
343  return 100 - 100 * s->y / s->cur_h;
344  }
345 }
346 
347 /* process exactly one decompressed row */
348 static void png_handle_row(PNGDecContext *s, uint8_t *dst, ptrdiff_t dst_stride)
349 {
350  uint8_t *ptr, *last_row;
351  int got_line;
352 
353  if (!s->interlace_type) {
354  ptr = dst + dst_stride * (s->y + s->y_offset) +
355  (s->subframe_packed ? 0 : s->x_offset * s->bpp);
356  if (s->y == 0)
357  last_row = s->last_row;
358  else
359  last_row = ptr - dst_stride;
360 
361  ff_png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
362  last_row, s->row_size, s->bpp);
363  /* loco lags by 1 row so that it doesn't interfere with top prediction */
364  if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
365  if (s->bit_depth == 16) {
366  deloco_rgb16((uint16_t *)(ptr - dst_stride), s->row_size / 2,
367  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
368  } else {
369  deloco_rgb8(ptr - dst_stride, s->row_size,
370  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
371  }
372  }
373  s->y++;
374  if (s->y == s->cur_h) {
375  s->pic_state |= PNG_ALLIMAGE;
376  if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
377  if (s->bit_depth == 16) {
378  deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
379  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
380  } else {
381  deloco_rgb8(ptr, s->row_size,
382  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
383  }
384  }
385  }
386  } else {
387  got_line = 0;
388  for (;;) {
389  ptr = dst + dst_stride * (s->y + s->y_offset) + s->x_offset * s->bpp;
390  if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
391  /* if we already read one row, it is time to stop to
392  * wait for the next one */
393  if (got_line)
394  break;
395  ff_png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
396  s->last_row, s->pass_row_size, s->bpp);
397  FFSWAP(uint8_t *, s->last_row, s->tmp_row);
398  FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size);
399  got_line = 1;
400  }
401  if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
402  png_put_interlaced_row(ptr, s->cur_w, s->bits_per_pixel, s->pass,
403  s->color_type, s->last_row);
404  }
405  s->y++;
406  if (s->y == s->cur_h) {
407  memset(s->last_row, 0, s->row_size);
408  for (;;) {
409  if (s->pass == NB_PASSES - 1) {
410  s->pic_state |= PNG_ALLIMAGE;
411  goto the_end;
412  } else {
413  s->pass++;
414  s->y = 0;
415  s->pass_row_size = ff_png_pass_row_size(s->pass,
416  s->bits_per_pixel,
417  s->cur_w);
418  s->crow_size = s->pass_row_size + 1;
419  if (s->pass_row_size != 0)
420  break;
421  /* skip pass if empty row */
422  }
423  }
424  }
425  }
426 the_end:;
427  }
428 }
429 
430 static int png_decode_idat(PNGDecContext *s, int length,
431  uint8_t *dst, ptrdiff_t dst_stride)
432 {
433  int ret;
434  s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
435  s->zstream.next_in = s->gb.buffer;
436  bytestream2_skip(&s->gb, length);
437 
438  /* decode one line if possible */
439  while (s->zstream.avail_in > 0) {
440  ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
441  if (ret != Z_OK && ret != Z_STREAM_END) {
442  av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
443  return AVERROR_EXTERNAL;
444  }
445  if (s->zstream.avail_out == 0) {
446  if (!(s->pic_state & PNG_ALLIMAGE)) {
447  png_handle_row(s, dst, dst_stride);
448  }
449  s->zstream.avail_out = s->crow_size;
450  s->zstream.next_out = s->crow_buf;
451  }
452  if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
453  av_log(s->avctx, AV_LOG_WARNING,
454  "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
455  return 0;
456  }
457  }
458  return 0;
459 }
460 
461 static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
462  const uint8_t *data_end)
463 {
464  z_stream zstream;
465  unsigned char *buf;
466  unsigned buf_size;
467  int ret;
468 
469  zstream.zalloc = ff_png_zalloc;
470  zstream.zfree = ff_png_zfree;
471  zstream.opaque = NULL;
472  if (inflateInit(&zstream) != Z_OK)
473  return AVERROR_EXTERNAL;
474  zstream.next_in = data;
475  zstream.avail_in = data_end - data;
477 
478  while (zstream.avail_in > 0) {
479  av_bprint_get_buffer(bp, 2, &buf, &buf_size);
480  if (buf_size < 2) {
481  ret = AVERROR(ENOMEM);
482  goto fail;
483  }
484  zstream.next_out = buf;
485  zstream.avail_out = buf_size - 1;
486  ret = inflate(&zstream, Z_PARTIAL_FLUSH);
487  if (ret != Z_OK && ret != Z_STREAM_END) {
488  ret = AVERROR_EXTERNAL;
489  goto fail;
490  }
491  bp->len += zstream.next_out - buf;
492  if (ret == Z_STREAM_END)
493  break;
494  }
495  inflateEnd(&zstream);
496  bp->str[bp->len] = 0;
497  return 0;
498 
499 fail:
500  inflateEnd(&zstream);
502  return ret;
503 }
504 
505 static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
506 {
507  size_t extra = 0, i;
508  uint8_t *out, *q;
509 
510  for (i = 0; i < size_in; i++)
511  extra += in[i] >= 0x80;
512  if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
513  return NULL;
514  q = out = av_malloc(size_in + extra + 1);
515  if (!out)
516  return NULL;
517  for (i = 0; i < size_in; i++) {
518  if (in[i] >= 0x80) {
519  *(q++) = 0xC0 | (in[i] >> 6);
520  *(q++) = 0x80 | (in[i] & 0x3F);
521  } else {
522  *(q++) = in[i];
523  }
524  }
525  *(q++) = 0;
526  return out;
527 }
528 
529 static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed)
530 {
531  int ret, method;
532  const uint8_t *data = s->gb.buffer;
533  const uint8_t *data_end = data + length;
534  const uint8_t *keyword = data;
535  const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
536  uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
537  unsigned text_len;
538  AVBPrint bp;
539 
540  if (!keyword_end)
541  return AVERROR_INVALIDDATA;
542  data = keyword_end + 1;
543 
544  if (compressed) {
545  if (data == data_end)
546  return AVERROR_INVALIDDATA;
547  method = *(data++);
548  if (method)
549  return AVERROR_INVALIDDATA;
550  if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
551  return ret;
552  text_len = bp.len;
553  ret = av_bprint_finalize(&bp, (char **)&text);
554  if (ret < 0)
555  return ret;
556  } else {
557  text = (uint8_t *)data;
558  text_len = data_end - text;
559  }
560 
561  kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
562  txt_utf8 = iso88591_to_utf8(text, text_len);
563  if (text != data)
564  av_free(text);
565  if (!(kw_utf8 && txt_utf8)) {
566  av_free(kw_utf8);
567  av_free(txt_utf8);
568  return AVERROR(ENOMEM);
569  }
570 
571  av_dict_set(&s->frame_metadata, kw_utf8, txt_utf8,
573  return 0;
574 }
575 
577  uint32_t length)
578 {
579  if (length != 13)
580  return AVERROR_INVALIDDATA;
581 
582  if (s->pic_state & PNG_IDAT) {
583  av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");
584  return AVERROR_INVALIDDATA;
585  }
586 
587  if (s->hdr_state & PNG_IHDR) {
588  av_log(avctx, AV_LOG_ERROR, "Multiple IHDR\n");
589  return AVERROR_INVALIDDATA;
590  }
591 
592  s->width = s->cur_w = bytestream2_get_be32(&s->gb);
593  s->height = s->cur_h = bytestream2_get_be32(&s->gb);
594  if (av_image_check_size(s->width, s->height, 0, avctx)) {
595  s->cur_w = s->cur_h = s->width = s->height = 0;
596  av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
597  return AVERROR_INVALIDDATA;
598  }
599  s->bit_depth = bytestream2_get_byte(&s->gb);
600  if (s->bit_depth != 1 && s->bit_depth != 2 && s->bit_depth != 4 &&
601  s->bit_depth != 8 && s->bit_depth != 16) {
602  av_log(avctx, AV_LOG_ERROR, "Invalid bit depth\n");
603  goto error;
604  }
605  s->color_type = bytestream2_get_byte(&s->gb);
606  s->compression_type = bytestream2_get_byte(&s->gb);
607  if (s->compression_type) {
608  av_log(avctx, AV_LOG_ERROR, "Invalid compression method %d\n", s->compression_type);
609  goto error;
610  }
611  s->filter_type = bytestream2_get_byte(&s->gb);
612  s->interlace_type = bytestream2_get_byte(&s->gb);
613  bytestream2_skip(&s->gb, 4); /* crc */
614  s->hdr_state |= PNG_IHDR;
615  if (avctx->debug & FF_DEBUG_PICT_INFO)
616  av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
617  "compression_type=%d filter_type=%d interlace_type=%d\n",
618  s->width, s->height, s->bit_depth, s->color_type,
619  s->compression_type, s->filter_type, s->interlace_type);
620 
621  return 0;
622 error:
623  s->cur_w = s->cur_h = s->width = s->height = 0;
624  s->bit_depth = 8;
625  return AVERROR_INVALIDDATA;
626 }
627 
629 {
630  if (s->pic_state & PNG_IDAT) {
631  av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
632  return AVERROR_INVALIDDATA;
633  }
634  avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
635  avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
636  if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
637  avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
638  bytestream2_skip(&s->gb, 1); /* unit specifier */
639  bytestream2_skip(&s->gb, 4); /* crc */
640 
641  return 0;
642 }
643 
645  uint32_t length, AVFrame *p)
646 {
647  int ret;
648  size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
649 
650  if (!p)
651  return AVERROR_INVALIDDATA;
652  if (!(s->hdr_state & PNG_IHDR)) {
653  av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
654  return AVERROR_INVALIDDATA;
655  }
656  if (!(s->pic_state & PNG_IDAT)) {
657  /* init image info */
658  ret = ff_set_dimensions(avctx, s->width, s->height);
659  if (ret < 0)
660  return ret;
661 
662  s->channels = ff_png_get_nb_channels(s->color_type);
663  s->bits_per_pixel = s->bit_depth * s->channels;
664  s->bpp = (s->bits_per_pixel + 7) >> 3;
665  s->row_size = (s->cur_w * s->bits_per_pixel + 7) >> 3;
666 
667  /* Packed rows of sub-8-bit frames cannot be placed at the frame
668  * offset, as that one is a pixel and not a byte offset. */
669  s->subframe_packed = CONFIG_APNG_DECODER &&
670  avctx->codec_id == AV_CODEC_ID_APNG &&
671  s->bits_per_pixel <= 4 && !s->interlace_type;
672 
673  if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
674  s->color_type == PNG_COLOR_TYPE_RGB) {
675  avctx->pix_fmt = AV_PIX_FMT_RGB24;
676  } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
677  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
678  avctx->pix_fmt = AV_PIX_FMT_RGBA;
679  } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
680  s->color_type == PNG_COLOR_TYPE_GRAY) {
681  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
682  } else if (s->bit_depth == 16 &&
683  s->color_type == PNG_COLOR_TYPE_GRAY) {
684  avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
685  } else if (s->bit_depth == 16 &&
686  s->color_type == PNG_COLOR_TYPE_RGB) {
687  avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
688  } else if (s->bit_depth == 16 &&
689  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
690  avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
691  } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
692  s->color_type == PNG_COLOR_TYPE_PALETTE) {
693  avctx->pix_fmt = AV_PIX_FMT_PAL8;
694  } else if (s->bit_depth == 1 && s->bits_per_pixel == 1 && avctx->codec_id != AV_CODEC_ID_APNG) {
695  avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
696  } else if (s->bit_depth == 8 &&
697  s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
698  avctx->pix_fmt = AV_PIX_FMT_YA8;
699  } else if (s->bit_depth == 16 &&
700  s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
701  avctx->pix_fmt = AV_PIX_FMT_YA16BE;
702  } else {
704  "Bit depth %d color type %d",
705  s->bit_depth, s->color_type);
706  return AVERROR_PATCHWELCOME;
707  }
708 
709  if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
710  switch (avctx->pix_fmt) {
711  case AV_PIX_FMT_RGB24:
712  avctx->pix_fmt = AV_PIX_FMT_RGBA;
713  break;
714 
715  case AV_PIX_FMT_RGB48BE:
716  avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
717  break;
718 
719  case AV_PIX_FMT_GRAY8:
720  avctx->pix_fmt = AV_PIX_FMT_YA8;
721  break;
722 
723  case AV_PIX_FMT_GRAY16BE:
724  avctx->pix_fmt = AV_PIX_FMT_YA16BE;
725  break;
726 
727  default:
728  avpriv_request_sample(avctx, "bit depth %d "
729  "and color type %d with TRNS",
730  s->bit_depth, s->color_type);
731  return AVERROR_INVALIDDATA;
732  }
733 
734  s->bpp += byte_depth;
735  }
736 
737  ff_thread_release_buffer(avctx, &s->picture);
738  if ((ret = ff_thread_get_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
739  return ret;
740 
742  p->key_frame = 1;
743  p->interlaced_frame = !!s->interlace_type;
744 
745  ff_thread_finish_setup(avctx);
746 
747  /* compute the compressed row size */
748  if (!s->interlace_type) {
749  s->crow_size = s->row_size + 1;
750  } else {
751  s->pass = 0;
752  s->pass_row_size = ff_png_pass_row_size(s->pass,
753  s->bits_per_pixel,
754  s->cur_w);
755  s->crow_size = s->pass_row_size + 1;
756  }
757  ff_dlog(avctx, "row_size=%d crow_size =%d\n",
758  s->row_size, s->crow_size);
759 
760  /* copy the palette if needed */
761  if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
762  memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
763  /* empty row is used if differencing to the first row */
764  av_fast_padded_mallocz(&s->last_row, &s->last_row_size, s->row_size);
765  if (!s->last_row)
766  return AVERROR_INVALIDDATA;
767  if (s->interlace_type ||
768  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
769  av_fast_padded_malloc(&s->tmp_row, &s->tmp_row_size, s->row_size);
770  if (!s->tmp_row)
771  return AVERROR_INVALIDDATA;
772  }
773  /* compressed row */
774  av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
775  if (!s->buffer)
776  return AVERROR(ENOMEM);
777 
778  /* we want crow_buf+1 to be 16-byte aligned */
779  s->crow_buf = s->buffer + 15;
780  s->zstream.avail_out = s->crow_size;
781  s->zstream.next_out = s->crow_buf;
782  }
783 
784  s->pic_state |= PNG_IDAT;
785 
786  /* set image to non-transparent bpp while decompressing */
787  if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
788  s->bpp -= byte_depth;
789 
790  ret = png_decode_idat(s, length, p->data[0], p->linesize[0]);
791 
792  if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
793  s->bpp += byte_depth;
794 
795  if (ret < 0)
796  return ret;
797 
798  bytestream2_skip(&s->gb, 4); /* crc */
799 
800  return 0;
801 }
802 
804  uint32_t length)
805 {
806  int n, i, r, g, b;
807 
808  if ((length % 3) != 0 || length > 256 * 3)
809  return AVERROR_INVALIDDATA;
810  /* read the palette */
811  n = length / 3;
812  for (i = 0; i < n; i++) {
813  r = bytestream2_get_byte(&s->gb);
814  g = bytestream2_get_byte(&s->gb);
815  b = bytestream2_get_byte(&s->gb);
816  s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
817  }
818  for (; i < 256; i++)
819  s->palette[i] = (0xFFU << 24);
820  s->hdr_state |= PNG_PLTE;
821  bytestream2_skip(&s->gb, 4); /* crc */
822 
823  return 0;
824 }
825 
827  uint32_t length)
828 {
829  int v, i;
830 
831  if (!(s->hdr_state & PNG_IHDR)) {
832  av_log(avctx, AV_LOG_ERROR, "trns before IHDR\n");
833  return AVERROR_INVALIDDATA;
834  }
835 
836  if (s->pic_state & PNG_IDAT) {
837  av_log(avctx, AV_LOG_ERROR, "trns after IDAT\n");
838  return AVERROR_INVALIDDATA;
839  }
840 
841  if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
842  if (length > 256 || !(s->hdr_state & PNG_PLTE))
843  return AVERROR_INVALIDDATA;
844 
845  for (i = 0; i < length; i++) {
846  unsigned v = bytestream2_get_byte(&s->gb);
847  s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
848  }
849  } else if (s->color_type == PNG_COLOR_TYPE_GRAY || s->color_type == PNG_COLOR_TYPE_RGB) {
850  if ((s->color_type == PNG_COLOR_TYPE_GRAY && length != 2) ||
851  (s->color_type == PNG_COLOR_TYPE_RGB && length != 6) ||
852  s->bit_depth == 1)
853  return AVERROR_INVALIDDATA;
854 
855  for (i = 0; i < length / 2; i++) {
856  /* only use the least significant bits */
857  v = av_mod_uintp2(bytestream2_get_be16(&s->gb), s->bit_depth);
858 
859  if (s->bit_depth > 8)
860  AV_WB16(&s->transparent_color_be[2 * i], v);
861  else
862  s->transparent_color_be[i] = v;
863  }
864  } else {
865  return AVERROR_INVALIDDATA;
866  }
867 
868  bytestream2_skip(&s->gb, 4); /* crc */
869  s->has_trns = 1;
870 
871  return 0;
872 }
873 
874 static int decode_iccp_chunk(PNGDecContext *s, int length)
875 {
876  int ret, cnt = 0;
877  AVBPrint bp;
878 
879  while ((s->iccp_name[cnt++] = bytestream2_get_byte(&s->gb)) && cnt < 81);
880  if (cnt > 80) {
881  av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid name!\n");
882  ret = AVERROR_INVALIDDATA;
883  goto fail;
884  }
885 
886  length = FFMAX(length - cnt, 0);
887 
888  if (bytestream2_get_byte(&s->gb) != 0) {
889  av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid compression!\n");
890  ret = AVERROR_INVALIDDATA;
891  goto fail;
892  }
893 
894  length = FFMAX(length - 1, 0);
895 
896  if ((ret = decode_zbuf(&bp, s->gb.buffer, s->gb.buffer + length)) < 0)
897  return ret;
898 
899  av_freep(&s->iccp_data);
900  ret = av_bprint_finalize(&bp, (char **)&s->iccp_data);
901  if (ret < 0)
902  return ret;
903  s->iccp_data_len = bp.len;
904 
905  /* ICC compressed data and CRC */
906  bytestream2_skip(&s->gb, length + 4);
907 
908  return 0;
909 fail:
910  s->iccp_name[0] = 0;
911  return ret;
912 }
913 
914 /* expand one row of packed sub-8-bit samples to one byte per sample, in place */
916 {
917  if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
918  int i, k;
919 
920  i = width / 8;
921  for (k = 7; k >= 1; k--)
922  if ((width&7) >= k)
923  pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
924  for (i--; i >= 0; i--) {
925  pd[8*i + 7]= pd[i] & 1;
926  pd[8*i + 6]= (pd[i]>>1) & 1;
927  pd[8*i + 5]= (pd[i]>>2) & 1;
928  pd[8*i + 4]= (pd[i]>>3) & 1;
929  pd[8*i + 3]= (pd[i]>>4) & 1;
930  pd[8*i + 2]= (pd[i]>>5) & 1;
931  pd[8*i + 1]= (pd[i]>>6) & 1;
932  pd[8*i + 0]= pd[i]>>7;
933  }
934  } else if (s->bits_per_pixel == 2) {
935  int i;
936 
937  i = width / 4;
938  if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
939  if ((width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
940  if ((width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
941  if ((width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
942  for (i--; i >= 0; i--) {
943  pd[4*i + 3]= pd[i] & 3;
944  pd[4*i + 2]= (pd[i]>>2) & 3;
945  pd[4*i + 1]= (pd[i]>>4) & 3;
946  pd[4*i + 0]= pd[i]>>6;
947  }
948  } else {
949  if ((width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
950  if ((width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
951  if ((width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
952  for (i--; i >= 0; i--) {
953  pd[4*i + 3]= ( pd[i] & 3)*0x55;
954  pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
955  pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
956  pd[4*i + 0]= ( pd[i]>>6 )*0x55;
957  }
958  }
959  } else if (s->bits_per_pixel == 4) {
960  int i;
961 
962  i = width/2;
963  if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
964  if (width&1) pd[2*i+0]= pd[i]>>4;
965  for (i--; i >= 0; i--) {
966  pd[2*i + 1] = pd[i] & 15;
967  pd[2*i + 0] = pd[i] >> 4;
968  }
969  } else {
970  if (width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
971  for (i--; i >= 0; i--) {
972  pd[2*i + 1] = (pd[i] & 15) * 0x11;
973  pd[2*i + 0] = (pd[i] >> 4) * 0x11;
974  }
975  }
976  }
977 }
978 
980 {
981  uint8_t *pd = p->data[0];
982 
983  for (int j = 0; j < s->height; j++) {
984  unpack_small_row(s, pd, s->width);
985  pd += p->linesize[0];
986  }
987 }
988 
989 /* The offsets of an APNG frame are pixel offsets, so they cannot be applied
990  * to the packed rows: those are decoded at the start of the frame row and
991  * moved to their position within the frame once unpacked. */
993 {
994  /* one byte per sample once unpacked, that is one palette index or one
995  * gray sample per pixel */
996  const int bpp = s->channels;
997  uint8_t *pd = p->data[0] + s->y_offset * p->linesize[0];
998 
999  for (int j = 0; j < s->cur_h; j++) {
1000  unpack_small_row(s, pd, s->cur_w);
1001  if (s->x_offset)
1002  memmove(pd + s->x_offset * bpp, pd, s->cur_w * bpp);
1003  pd += p->linesize[0];
1004  }
1005 }
1006 
1008  uint32_t length)
1009 {
1010  uint32_t sequence_number;
1011  int cur_w, cur_h, x_offset, y_offset, dispose_op, blend_op;
1012 
1013  if (length != 26)
1014  return AVERROR_INVALIDDATA;
1015 
1016  if (!(s->hdr_state & PNG_IHDR)) {
1017  av_log(avctx, AV_LOG_ERROR, "fctl before IHDR\n");
1018  return AVERROR_INVALIDDATA;
1019  }
1020 
1021  if (s->pic_state & PNG_IDAT) {
1022  av_log(avctx, AV_LOG_ERROR, "fctl after IDAT\n");
1023  return AVERROR_INVALIDDATA;
1024  }
1025 
1026  s->last_w = s->cur_w;
1027  s->last_h = s->cur_h;
1028  s->last_x_offset = s->x_offset;
1029  s->last_y_offset = s->y_offset;
1030  s->last_dispose_op = s->dispose_op;
1031 
1032  sequence_number = bytestream2_get_be32(&s->gb);
1033  cur_w = bytestream2_get_be32(&s->gb);
1034  cur_h = bytestream2_get_be32(&s->gb);
1035  x_offset = bytestream2_get_be32(&s->gb);
1036  y_offset = bytestream2_get_be32(&s->gb);
1037  bytestream2_skip(&s->gb, 4); /* delay_num (2), delay_den (2) */
1038  dispose_op = bytestream2_get_byte(&s->gb);
1039  blend_op = bytestream2_get_byte(&s->gb);
1040  bytestream2_skip(&s->gb, 4); /* crc */
1041 
1042  if (sequence_number == 0 &&
1043  (cur_w != s->width ||
1044  cur_h != s->height ||
1045  x_offset != 0 ||
1046  y_offset != 0) ||
1047  cur_w <= 0 || cur_h <= 0 ||
1048  x_offset < 0 || y_offset < 0 ||
1049  cur_w > s->width - x_offset|| cur_h > s->height - y_offset)
1050  return AVERROR_INVALIDDATA;
1051 
1052  if (blend_op != APNG_BLEND_OP_OVER && blend_op != APNG_BLEND_OP_SOURCE) {
1053  av_log(avctx, AV_LOG_ERROR, "Invalid blend_op %d\n", blend_op);
1054  return AVERROR_INVALIDDATA;
1055  }
1056 
1057  if ((sequence_number == 0 || !s->last_picture.f) &&
1058  dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
1059  // No previous frame to revert to for the first frame
1060  // Spec says to just treat it as a APNG_DISPOSE_OP_BACKGROUND
1061  dispose_op = APNG_DISPOSE_OP_BACKGROUND;
1062  }
1063 
1064  if (blend_op == APNG_BLEND_OP_OVER && !s->has_trns && (
1065  avctx->pix_fmt == AV_PIX_FMT_RGB24 ||
1066  avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1067  avctx->pix_fmt == AV_PIX_FMT_PAL8 ||
1068  avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
1069  avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
1070  avctx->pix_fmt == AV_PIX_FMT_MONOBLACK
1071  )) {
1072  // APNG_BLEND_OP_OVER is the same as APNG_BLEND_OP_SOURCE when there is no alpha channel
1073  blend_op = APNG_BLEND_OP_SOURCE;
1074  }
1075 
1076  s->cur_w = cur_w;
1077  s->cur_h = cur_h;
1078  s->x_offset = x_offset;
1079  s->y_offset = y_offset;
1080  s->dispose_op = dispose_op;
1081  s->blend_op = blend_op;
1082 
1083  return 0;
1084 }
1085 
1087 {
1088  int i, j;
1089  uint8_t *pd = p->data[0];
1090  uint8_t *pd_last = s->last_picture.f->data[0];
1091  int ls = FFMIN(av_image_get_linesize(p->format, s->width, 0), s->width * s->bpp);
1092 
1093  ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
1094  for (j = 0; j < s->height; j++) {
1095  for (i = 0; i < ls; i++)
1096  pd[i] += pd_last[i];
1097  pd += p->linesize[0];
1098  pd_last += s->last_picture.f->linesize[0];
1099  }
1100 }
1101 
1102 // divide by 255 and round to nearest
1103 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
1104 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
1105 
1107  AVFrame *p)
1108 {
1109  uint8_t *dst = p->data[0];
1110  ptrdiff_t dst_stride = p->linesize[0];
1111  const uint8_t *src = s->last_picture.f->data[0];
1112  ptrdiff_t src_stride = s->last_picture.f->linesize[0];
1113 
1114  size_t x, y;
1115 
1116  if (s->blend_op == APNG_BLEND_OP_OVER &&
1117  avctx->pix_fmt != AV_PIX_FMT_RGBA &&
1118  avctx->pix_fmt != AV_PIX_FMT_GRAY8A &&
1119  avctx->pix_fmt != AV_PIX_FMT_PAL8) {
1120  avpriv_request_sample(avctx, "Blending with pixel format %s",
1121  av_get_pix_fmt_name(avctx->pix_fmt));
1122  return AVERROR_PATCHWELCOME;
1123  }
1124 
1125  ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
1126 
1127  // need to reset a rectangle to background:
1128  if (s->last_dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
1129  av_fast_malloc(&s->background_buf, &s->background_buf_allocated,
1130  src_stride * p->height);
1131  if (!s->background_buf)
1132  return AVERROR(ENOMEM);
1133 
1134  memcpy(s->background_buf, src, src_stride * p->height);
1135 
1136  for (y = s->last_y_offset; y < s->last_y_offset + s->last_h; y++) {
1137  memset(s->background_buf + src_stride * y +
1138  s->bpp * s->last_x_offset, 0, s->bpp * s->last_w);
1139  }
1140 
1141  src = s->background_buf;
1142  }
1143 
1144  // copy unchanged rectangles from the last frame
1145  for (y = 0; y < s->y_offset; y++)
1146  memcpy(dst + y * dst_stride, src + y * src_stride, p->width * s->bpp);
1147  for (y = s->y_offset; y < s->y_offset + s->cur_h; y++) {
1148  memcpy(dst + y * dst_stride, src + y * src_stride, s->x_offset * s->bpp);
1149  memcpy(dst + y * dst_stride + (s->x_offset + s->cur_w) * s->bpp,
1150  src + y * src_stride + (s->x_offset + s->cur_w) * s->bpp,
1151  (p->width - s->cur_w - s->x_offset) * s->bpp);
1152  }
1153  for (y = s->y_offset + s->cur_h; y < p->height; y++)
1154  memcpy(dst + y * dst_stride, src + y * src_stride, p->width * s->bpp);
1155 
1156  if (s->blend_op == APNG_BLEND_OP_OVER) {
1157  // Perform blending
1158  for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
1159  uint8_t *foreground = dst + dst_stride * y + s->bpp * s->x_offset;
1160  const uint8_t *background = src + src_stride * y + s->bpp * s->x_offset;
1161  for (x = s->x_offset; x < s->x_offset + s->cur_w; ++x, foreground += s->bpp, background += s->bpp) {
1162  size_t b;
1163  uint8_t foreground_alpha, background_alpha, output_alpha;
1164  uint8_t output[10];
1165 
1166  // Since we might be blending alpha onto alpha, we use the following equations:
1167  // output_alpha = foreground_alpha + (1 - foreground_alpha) * background_alpha
1168  // output = (foreground_alpha * foreground + (1 - foreground_alpha) * background_alpha * background) / output_alpha
1169 
1170  switch (avctx->pix_fmt) {
1171  case AV_PIX_FMT_RGBA:
1172  foreground_alpha = foreground[3];
1173  background_alpha = background[3];
1174  break;
1175 
1176  case AV_PIX_FMT_GRAY8A:
1177  foreground_alpha = foreground[1];
1178  background_alpha = background[1];
1179  break;
1180 
1181  case AV_PIX_FMT_PAL8:
1182  foreground_alpha = s->palette[foreground[0]] >> 24;
1183  background_alpha = s->palette[background[0]] >> 24;
1184  break;
1185  }
1186 
1187  if (foreground_alpha == 255)
1188  continue;
1189 
1190  if (foreground_alpha == 0) {
1191  memcpy(foreground, background, s->bpp);
1192  continue;
1193  }
1194 
1195  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1196  // TODO: Alpha blending with PAL8 will likely need the entire image converted over to RGBA first
1197  avpriv_request_sample(avctx, "Alpha blending palette samples");
1198  continue;
1199  }
1200 
1201  output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);
1202 
1203  av_assert0(s->bpp <= 10);
1204 
1205  for (b = 0; b < s->bpp - 1; ++b) {
1206  if (output_alpha == 0) {
1207  output[b] = 0;
1208  } else if (background_alpha == 255) {
1209  output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);
1210  } else {
1211  output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);
1212  }
1213  }
1214  output[b] = output_alpha;
1215  memcpy(foreground, output, s->bpp);
1216  }
1217  }
1218  }
1219 
1220  return 0;
1221 }
1222 
1224  AVFrame *p, const AVPacket *avpkt)
1225 {
1226  const AVCRC *crc_tab = av_crc_get_table(AV_CRC_32_IEEE_LE);
1227  uint32_t tag, length;
1228  int decode_next_dat = 0;
1229  int i, ret;
1230 
1231  for (;;) {
1232  length = bytestream2_get_bytes_left(&s->gb);
1233  if (length <= 0) {
1234 
1235  if (avctx->codec_id == AV_CODEC_ID_PNG &&
1236  avctx->skip_frame == AVDISCARD_ALL) {
1237  return 0;
1238  }
1239 
1240  if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && length == 0) {
1241  if (!(s->pic_state & PNG_IDAT))
1242  return 0;
1243  else
1244  goto exit_loop;
1245  }
1246  av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length);
1247  if ( s->pic_state & PNG_ALLIMAGE
1249  goto exit_loop;
1250  ret = AVERROR_INVALIDDATA;
1251  goto fail;
1252  }
1253 
1254  length = bytestream2_get_be32(&s->gb);
1255  if (length > 0x7fffffff || length + 8 > bytestream2_get_bytes_left(&s->gb)) {
1256  av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
1257  ret = AVERROR_INVALIDDATA;
1258  goto fail;
1259  }
1260  if (avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_IGNORE_ERR)) {
1261  uint32_t crc_sig = AV_RB32(s->gb.buffer + length + 4);
1262  uint32_t crc_cal = ~av_crc(crc_tab, UINT32_MAX, s->gb.buffer, length + 4);
1263  if (crc_sig ^ crc_cal) {
1264  av_log(avctx, AV_LOG_ERROR, "CRC mismatch in chunk");
1265  if (avctx->err_recognition & AV_EF_EXPLODE) {
1266  av_log(avctx, AV_LOG_ERROR, ", quitting\n");
1267  ret = AVERROR_INVALIDDATA;
1268  goto fail;
1269  }
1270  av_log(avctx, AV_LOG_ERROR, ", skipping\n");
1271  bytestream2_skip(&s->gb, 4); /* tag */
1272  goto skip_tag;
1273  }
1274  }
1275  tag = bytestream2_get_le32(&s->gb);
1276  if (avctx->debug & FF_DEBUG_STARTCODE)
1277  av_log(avctx, AV_LOG_DEBUG, "png: tag=%s length=%u\n",
1278  av_fourcc2str(tag), length);
1279 
1280  if (avctx->codec_id == AV_CODEC_ID_PNG &&
1281  avctx->skip_frame == AVDISCARD_ALL) {
1282  switch(tag) {
1283  case MKTAG('I', 'H', 'D', 'R'):
1284  case MKTAG('p', 'H', 'Y', 's'):
1285  case MKTAG('t', 'E', 'X', 't'):
1286  case MKTAG('I', 'D', 'A', 'T'):
1287  case MKTAG('t', 'R', 'N', 'S'):
1288  break;
1289  default:
1290  goto skip_tag;
1291  }
1292  }
1293 
1294  switch (tag) {
1295  case MKTAG('I', 'H', 'D', 'R'):
1296  if ((ret = decode_ihdr_chunk(avctx, s, length)) < 0)
1297  goto fail;
1298  break;
1299  case MKTAG('p', 'H', 'Y', 's'):
1300  if ((ret = decode_phys_chunk(avctx, s)) < 0)
1301  goto fail;
1302  break;
1303  case MKTAG('f', 'c', 'T', 'L'):
1304  if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1305  goto skip_tag;
1306  if ((ret = decode_fctl_chunk(avctx, s, length)) < 0)
1307  goto fail;
1308  decode_next_dat = 1;
1309  break;
1310  case MKTAG('f', 'd', 'A', 'T'):
1311  if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1312  goto skip_tag;
1313  if (!decode_next_dat || length < 4) {
1314  ret = AVERROR_INVALIDDATA;
1315  goto fail;
1316  }
1317  bytestream2_get_be32(&s->gb);
1318  length -= 4;
1319  /* fallthrough */
1320  case MKTAG('I', 'D', 'A', 'T'):
1321  if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
1322  goto skip_tag;
1323  if ((ret = decode_idat_chunk(avctx, s, length, p)) < 0)
1324  goto fail;
1325  break;
1326  case MKTAG('P', 'L', 'T', 'E'):
1327  if (decode_plte_chunk(avctx, s, length) < 0)
1328  goto skip_tag;
1329  break;
1330  case MKTAG('t', 'R', 'N', 'S'):
1331  if (decode_trns_chunk(avctx, s, length) < 0)
1332  goto skip_tag;
1333  break;
1334  case MKTAG('t', 'E', 'X', 't'):
1335  if (decode_text_chunk(s, length, 0) < 0)
1336  av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
1337  bytestream2_skip(&s->gb, length + 4);
1338  break;
1339  case MKTAG('z', 'T', 'X', 't'):
1340  if (decode_text_chunk(s, length, 1) < 0)
1341  av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
1342  bytestream2_skip(&s->gb, length + 4);
1343  break;
1344  case MKTAG('s', 'T', 'E', 'R'): {
1345  int mode = bytestream2_get_byte(&s->gb);
1346 
1347  if (mode == 0 || mode == 1) {
1348  s->stereo_mode = mode;
1349  } else {
1350  av_log(avctx, AV_LOG_WARNING,
1351  "Unknown value in sTER chunk (%d)\n", mode);
1352  }
1353  bytestream2_skip(&s->gb, 4); /* crc */
1354  break;
1355  }
1356  case MKTAG('i', 'C', 'C', 'P'): {
1357  if ((ret = decode_iccp_chunk(s, length)) < 0)
1358  goto fail;
1359  break;
1360  }
1361  case MKTAG('c', 'H', 'R', 'M'): {
1362  s->have_chrm = 1;
1363 
1364  s->white_point[0] = bytestream2_get_be32(&s->gb);
1365  s->white_point[1] = bytestream2_get_be32(&s->gb);
1366 
1367  /* RGB Primaries */
1368  for (i = 0; i < 3; i++) {
1369  s->display_primaries[i][0] = bytestream2_get_be32(&s->gb);
1370  s->display_primaries[i][1] = bytestream2_get_be32(&s->gb);
1371  }
1372 
1373  bytestream2_skip(&s->gb, 4); /* crc */
1374  break;
1375  }
1376  case MKTAG('g', 'A', 'M', 'A'): {
1377  AVBPrint bp;
1378  char *gamma_str;
1379  int num = bytestream2_get_be32(&s->gb);
1380 
1382  av_bprintf(&bp, "%i/%i", num, 100000);
1383  ret = av_bprint_finalize(&bp, &gamma_str);
1384  if (ret < 0)
1385  return ret;
1386 
1387  av_dict_set(&s->frame_metadata, "gamma", gamma_str, AV_DICT_DONT_STRDUP_VAL);
1388 
1389  bytestream2_skip(&s->gb, 4); /* crc */
1390  break;
1391  }
1392  case MKTAG('I', 'E', 'N', 'D'):
1393  if (!(s->pic_state & PNG_ALLIMAGE))
1394  av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
1395  if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT))) {
1396  ret = AVERROR_INVALIDDATA;
1397  goto fail;
1398  }
1399  bytestream2_skip(&s->gb, 4); /* crc */
1400  goto exit_loop;
1401  default:
1402  /* skip tag */
1403 skip_tag:
1404  bytestream2_skip(&s->gb, length + 4);
1405  break;
1406  }
1407  }
1408 exit_loop:
1409 
1410  if (!p)
1411  return AVERROR_INVALIDDATA;
1412 
1413  if (avctx->codec_id == AV_CODEC_ID_PNG &&
1414  avctx->skip_frame == AVDISCARD_ALL) {
1415  return 0;
1416  }
1417 
1419  return AVERROR_INVALIDDATA;
1420 
1421  if (s->bits_per_pixel <= 4) {
1422  if (CONFIG_APNG_DECODER && s->subframe_packed)
1424  else
1425  handle_small_bpp(s, p);
1426  }
1427 
1428  /* apply transparency if needed */
1429  if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
1430  size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
1431  size_t raw_bpp = s->bpp - byte_depth;
1432  ptrdiff_t x, y;
1433 
1434  av_assert0(s->bit_depth > 1);
1435 
1436  for (y = 0; y < s->height; ++y) {
1437  uint8_t *row = &p->data[0][p->linesize[0] * y];
1438 
1439  if (s->bpp == 2 && byte_depth == 1) {
1440  uint8_t *pixel = &row[2 * s->width - 1];
1441  uint8_t *rowp = &row[1 * s->width - 1];
1442  int tcolor = s->transparent_color_be[0];
1443  for (x = s->width; x > 0; --x) {
1444  *pixel-- = *rowp == tcolor ? 0 : 0xff;
1445  *pixel-- = *rowp--;
1446  }
1447  } else if (s->bpp == 4 && byte_depth == 1) {
1448  uint8_t *pixel = &row[4 * s->width - 1];
1449  uint8_t *rowp = &row[3 * s->width - 1];
1450  int tcolor = AV_RL24(s->transparent_color_be);
1451  for (x = s->width; x > 0; --x) {
1452  *pixel-- = AV_RL24(rowp-2) == tcolor ? 0 : 0xff;
1453  *pixel-- = *rowp--;
1454  *pixel-- = *rowp--;
1455  *pixel-- = *rowp--;
1456  }
1457  } else {
1458  /* since we're updating in-place, we have to go from right to left */
1459  for (x = s->width; x > 0; --x) {
1460  uint8_t *pixel = &row[s->bpp * (x - 1)];
1461  memmove(pixel, &row[raw_bpp * (x - 1)], raw_bpp);
1462 
1463  if (!memcmp(pixel, s->transparent_color_be, raw_bpp)) {
1464  memset(&pixel[raw_bpp], 0, byte_depth);
1465  } else {
1466  memset(&pixel[raw_bpp], 0xff, byte_depth);
1467  }
1468  }
1469  }
1470  }
1471  }
1472 
1473  /* handle P-frames only if a predecessor frame is available */
1474  if (s->last_picture.f->data[0]) {
1475  if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
1476  && s->last_picture.f->width == p->width
1477  && s->last_picture.f->height== p->height
1478  && s->last_picture.f->format== p->format
1479  ) {
1480  if (CONFIG_PNG_DECODER && avctx->codec_id != AV_CODEC_ID_APNG)
1481  handle_p_frame_png(s, p);
1482  else if (CONFIG_APNG_DECODER &&
1483  avctx->codec_id == AV_CODEC_ID_APNG &&
1484  (ret = handle_p_frame_apng(avctx, s, p)) < 0)
1485  goto fail;
1486  }
1487  }
1488  ff_thread_report_progress(&s->picture, INT_MAX, 0);
1489 
1490  return 0;
1491 
1492 fail:
1493  ff_thread_report_progress(&s->picture, INT_MAX, 0);
1494  return ret;
1495 }
1496 
1498 {
1499  av_freep(&s->iccp_data);
1500  s->iccp_data_len = 0;
1501  s->iccp_name[0] = 0;
1502 
1503  s->stereo_mode = -1;
1504 
1505  s->have_chrm = 0;
1506 
1507  av_dict_free(&s->frame_metadata);
1508 }
1509 
1511  const AVFrame *src)
1512 {
1513  int ret;
1514 
1515  ret = av_frame_ref(f, src);
1516  if (ret < 0)
1517  return ret;
1518 
1519  if (s->iccp_data) {
1521  if (!sd) {
1522  ret = AVERROR(ENOMEM);
1523  goto fail;
1524  }
1525  memcpy(sd->data, s->iccp_data, s->iccp_data_len);
1526 
1527  av_dict_set(&sd->metadata, "name", s->iccp_name, 0);
1528  }
1529 
1530  if (s->stereo_mode >= 0) {
1532  if (!stereo3d) {
1533  ret = AVERROR(ENOMEM);
1534  goto fail;
1535  }
1536 
1537  stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
1538  stereo3d->flags = s->stereo_mode ? 0 : AV_STEREO3D_FLAG_INVERT;
1539  }
1540 
1541  if (s->have_chrm) {
1543  if (!mdm) {
1544  ret = AVERROR(ENOMEM);
1545  goto fail;
1546  }
1547 
1548  mdm->white_point[0] = av_make_q(s->white_point[0], 100000);
1549  mdm->white_point[1] = av_make_q(s->white_point[1], 100000);
1550 
1551  /* RGB Primaries */
1552  for (int i = 0; i < 3; i++) {
1553  mdm->display_primaries[i][0] = av_make_q(s->display_primaries[i][0], 100000);
1554  mdm->display_primaries[i][1] = av_make_q(s->display_primaries[i][1], 100000);
1555  }
1556 
1557  mdm->has_primaries = 1;
1558  }
1559 
1560  FFSWAP(AVDictionary*, f->metadata, s->frame_metadata);
1561 
1562  return 0;
1563 fail:
1564  av_frame_unref(f);
1565  return ret;
1566 }
1567 
1568 #if CONFIG_PNG_DECODER
1569 static int decode_frame_png(AVCodecContext *avctx,
1570  void *data, int *got_frame,
1571  AVPacket *avpkt)
1572 {
1573  PNGDecContext *const s = avctx->priv_data;
1574  const uint8_t *buf = avpkt->data;
1575  int buf_size = avpkt->size;
1576  AVFrame *dst_frame = data;
1577  AVFrame *p = s->picture.f;
1578  int64_t sig;
1579  int ret;
1580 
1582 
1583  bytestream2_init(&s->gb, buf, buf_size);
1584 
1585  /* check signature */
1586  sig = bytestream2_get_be64(&s->gb);
1587  if (sig != PNGSIG &&
1588  sig != MNGSIG) {
1589  av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature 0x%08"PRIX64".\n", sig);
1590  return AVERROR_INVALIDDATA;
1591  }
1592 
1593  s->y = s->has_trns = 0;
1594  s->hdr_state = 0;
1595  s->pic_state = 0;
1596 
1597  /* init the zlib */
1598  s->zstream.zalloc = ff_png_zalloc;
1599  s->zstream.zfree = ff_png_zfree;
1600  s->zstream.opaque = NULL;
1601  ret = inflateInit(&s->zstream);
1602  if (ret != Z_OK) {
1603  av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1604  return AVERROR_EXTERNAL;
1605  }
1606 
1607  if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1608  goto the_end;
1609 
1610  if (avctx->skip_frame == AVDISCARD_ALL) {
1611  *got_frame = 0;
1612  ret = bytestream2_tell(&s->gb);
1613  goto the_end;
1614  }
1615 
1616  ret = output_frame(s, dst_frame, s->picture.f);
1617  if (ret < 0)
1618  goto the_end;
1619 
1620  if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
1621  ff_thread_release_buffer(avctx, &s->last_picture);
1622  FFSWAP(ThreadFrame, s->picture, s->last_picture);
1623  }
1624 
1625  *got_frame = 1;
1626 
1627  ret = bytestream2_tell(&s->gb);
1628 the_end:
1629  inflateEnd(&s->zstream);
1630  s->crow_buf = NULL;
1631  return ret;
1632 }
1633 #endif
1634 
1635 #if CONFIG_APNG_DECODER
1636 static int decode_frame_apng(AVCodecContext *avctx,
1637  void *data, int *got_frame,
1638  AVPacket *avpkt)
1639 {
1640  PNGDecContext *const s = avctx->priv_data;
1641  AVFrame *dst_frame = data;
1642  int ret;
1643  AVFrame *p = s->picture.f;
1644 
1646 
1647  if (!(s->hdr_state & PNG_IHDR)) {
1648  if (!avctx->extradata_size)
1649  return AVERROR_INVALIDDATA;
1650 
1651  /* only init fields, there is no zlib use in extradata */
1652  s->zstream.zalloc = ff_png_zalloc;
1653  s->zstream.zfree = ff_png_zfree;
1654 
1655  bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
1656  if ((ret = decode_frame_common(avctx, s, NULL, avpkt)) < 0)
1657  goto end;
1658  }
1659 
1660  /* reset state for a new frame */
1661  if ((ret = inflateInit(&s->zstream)) != Z_OK) {
1662  av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1663  ret = AVERROR_EXTERNAL;
1664  goto end;
1665  }
1666  s->y = 0;
1667  s->pic_state = 0;
1668  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1669  if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1670  goto end;
1671 
1672  if (!(s->pic_state & PNG_ALLIMAGE))
1673  av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
1674  if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT))) {
1675  ret = AVERROR_INVALIDDATA;
1676  goto end;
1677  }
1678 
1679  ret = output_frame(s, dst_frame, s->picture.f);
1680  if (ret < 0)
1681  goto end;
1682 
1683  if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
1684  if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
1685  ff_thread_release_buffer(avctx, &s->picture);
1686  } else {
1687  ff_thread_release_buffer(avctx, &s->last_picture);
1688  FFSWAP(ThreadFrame, s->picture, s->last_picture);
1689  }
1690  }
1691 
1692  *got_frame = 1;
1693  ret = bytestream2_tell(&s->gb);
1694 
1695 end:
1696  inflateEnd(&s->zstream);
1697  return ret;
1698 }
1699 #endif
1700 
1701 #if HAVE_THREADS
1702 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1703 {
1704  PNGDecContext *psrc = src->priv_data;
1705  PNGDecContext *pdst = dst->priv_data;
1706  ThreadFrame *src_frame = NULL;
1707  int ret;
1708 
1709  if (dst == src)
1710  return 0;
1711 
1713 
1714  pdst->width = psrc->width;
1715  pdst->height = psrc->height;
1716  pdst->bit_depth = psrc->bit_depth;
1717  pdst->color_type = psrc->color_type;
1718  pdst->compression_type = psrc->compression_type;
1719  pdst->interlace_type = psrc->interlace_type;
1720  pdst->filter_type = psrc->filter_type;
1721  pdst->cur_w = psrc->cur_w;
1722  pdst->cur_h = psrc->cur_h;
1723  pdst->x_offset = psrc->x_offset;
1724  pdst->y_offset = psrc->y_offset;
1725  pdst->has_trns = psrc->has_trns;
1726  memcpy(pdst->transparent_color_be, psrc->transparent_color_be, sizeof(pdst->transparent_color_be));
1727 
1728  pdst->dispose_op = psrc->dispose_op;
1729 
1730  memcpy(pdst->palette, psrc->palette, sizeof(pdst->palette));
1731 
1732  pdst->hdr_state |= psrc->hdr_state;
1733  }
1734 
1735  src_frame = psrc->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?
1736  &psrc->last_picture : &psrc->picture;
1737 
1739  if (src_frame && src_frame->f->data[0]) {
1740  ret = ff_thread_ref_frame(&pdst->last_picture, src_frame);
1741  if (ret < 0)
1742  return ret;
1743  }
1744 
1745  return 0;
1746 }
1747 #endif
1748 
1750 {
1751  PNGDecContext *s = avctx->priv_data;
1752 
1753  avctx->color_range = AVCOL_RANGE_JPEG;
1754 
1755  s->avctx = avctx;
1756  s->last_picture.f = av_frame_alloc();
1757  s->picture.f = av_frame_alloc();
1758  if (!s->last_picture.f || !s->picture.f) {
1759  av_frame_free(&s->last_picture.f);
1760  av_frame_free(&s->picture.f);
1761  return AVERROR(ENOMEM);
1762  }
1763 
1764  ff_pngdsp_init(&s->dsp);
1765 
1766  return 0;
1767 }
1768 
1770 {
1771  PNGDecContext *s = avctx->priv_data;
1772 
1773  ff_thread_release_buffer(avctx, &s->last_picture);
1774  av_frame_free(&s->last_picture.f);
1775  ff_thread_release_buffer(avctx, &s->picture);
1776  av_frame_free(&s->picture.f);
1777  av_freep(&s->buffer);
1778  s->buffer_size = 0;
1779  av_freep(&s->last_row);
1780  s->last_row_size = 0;
1781  av_freep(&s->tmp_row);
1782  s->tmp_row_size = 0;
1783  av_freep(&s->background_buf);
1784 
1785  av_freep(&s->iccp_data);
1786  av_dict_free(&s->frame_metadata);
1787 
1788  return 0;
1789 }
1790 
1791 #if CONFIG_APNG_DECODER
1793  .name = "apng",
1794  .long_name = NULL_IF_CONFIG_SMALL("APNG (Animated Portable Network Graphics) image"),
1795  .type = AVMEDIA_TYPE_VIDEO,
1796  .id = AV_CODEC_ID_APNG,
1797  .priv_data_size = sizeof(PNGDecContext),
1798  .init = png_dec_init,
1799  .close = png_dec_end,
1800  .decode = decode_frame_apng,
1801  .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1802  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
1803  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1805 };
1806 #endif
1807 
1808 #if CONFIG_PNG_DECODER
1810  .name = "png",
1811  .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
1812  .type = AVMEDIA_TYPE_VIDEO,
1813  .id = AV_CODEC_ID_PNG,
1814  .priv_data_size = sizeof(PNGDecContext),
1815  .init = png_dec_init,
1816  .close = png_dec_end,
1817  .decode = decode_frame_png,
1818  .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1819  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
1822 };
1823 #endif
AVCodec ff_apng_decoder
AVCodec ff_png_decoder
APNG common header.
@ APNG_DISPOSE_OP_BACKGROUND
Definition: apng.h:32
@ APNG_DISPOSE_OP_PREVIOUS
Definition: apng.h:33
@ APNG_BLEND_OP_OVER
Definition: apng.h:38
@ APNG_BLEND_OP_SOURCE
Definition: apng.h:37
#define av_cold
Definition: attributes.h:88
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:1635
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1788
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
Definition: avcodec.h:1657
#define AV_EF_IGNORE_ERR
ignore errors and continue
Definition: avcodec.h:1662
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:1628
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:1660
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:1608
#define AV_RB32
Definition: intreadwrite.h:130
#define AV_RL24
Definition: intreadwrite.h:78
#define AV_RL32
Definition: intreadwrite.h:146
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
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:228
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:245
#define AV_BPRINT_SIZE_UNLIMITED
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
#define s(width, name)
Definition: cbs_vp9.c:257
#define f(width, name)
Definition: cbs_vp9.c:255
#define fail()
Definition: checkasm.h:133
#define FFSWAP(type, a, b)
Definition: common.h:108
#define FFMIN(a, b)
Definition: common.h:105
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define av_mod_uintp2
Definition: common.h:149
#define FFMAX(a, b)
Definition: common.h:103
#define CONFIG_PNG_DECODER
Definition: config.h:919
#define CONFIG_APNG_DECODER
Definition: config.h:753
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
Public header for CRC hash function implementation.
#define abs(x)
Definition: cuda_runtime.h:35
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:514
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:108
@ AV_CODEC_ID_PNG
Definition: codec_id.h:110
@ AV_CODEC_ID_APNG
Definition: codec_id.h:264
@ AVDISCARD_ALL
discard all
Definition: avcodec.h:236
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:50
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:62
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
uint32_t AVCRC
Definition: crc.h:47
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:394
@ AV_CRC_32_IEEE_LE
Definition: crc.h:54
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:199
#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:74
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
#define 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:72
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
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:443
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, buffer_size_t size)
Add a new side data to a frame.
Definition: frame.c:726
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
@ AV_FRAME_DATA_ICC_PROFILE
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
Definition: frame.h:143
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:502
#define av_fourcc2str(fourcc)
Definition: avutil.h:348
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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:320
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane.
Definition: imgutils.c:76
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:167
AVStereo3D * av_stereo3d_create_side_data(AVFrame *frame)
Allocate a complete AVFrameSideData and add it to the frame.
Definition: stereo3d.c:33
@ AV_STEREO3D_SIDEBYSIDE
Views are next to each other.
Definition: stereo3d.h:67
misc image utilities
int i
Definition: input.c:407
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
static int skip_tag(AVIOContext *in, int32_t tag_name)
Definition: ismindex.c:132
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
Definition: internal.h:76
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
Definition: internal.h:61
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
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:84
int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
Definition: utils.c:960
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:156
Stereoscopic video.
uint8_t w
Definition: llviddspenc.c:39
static const uint16_t mask[17]
Definition: lzw.c:38
AVMasteringDisplayMetadata * av_mastering_display_metadata_create_side_data(AVFrame *frame)
Allocate a complete AVMasteringDisplayMetadata and add it to the frame.
uint32_t tag
Definition: movenc.c:1611
const char data[16]
Definition: mxf.c:142
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:2489
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:586
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
@ AV_PIX_FMT_YA16BE
16 bits gray, 16 bits alpha (big-endian)
Definition: pixfmt.h:212
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:76
@ AV_PIX_FMT_GRAY8A
alias for AV_PIX_FMT_YA8
Definition: pixfmt.h:146
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_RGB48BE
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:102
@ AV_PIX_FMT_RGBA64BE
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:205
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition: pixfmt.h:143
const uint8_t ff_png_pass_ymask[NB_PASSES]
Definition: png.c:25
int ff_png_pass_row_size(int pass, int bits_per_pixel, int width)
Definition: png.c:62
int ff_png_get_nb_channels(int color_type)
Definition: png.c:49
void * ff_png_zalloc(void *opaque, unsigned int items, unsigned int size)
Definition: png.c:39
void ff_png_zfree(void *opaque, void *ptr)
Definition: png.c:44
#define PNG_COLOR_TYPE_RGB
Definition: png.h:35
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:36
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition: png.h:37
#define MNGSIG
Definition: png.h:50
#define PNG_FILTER_VALUE_AVG
Definition: png.h:43
#define PNGSIG
Definition: png.h:49
#define PNG_FILTER_VALUE_UP
Definition: png.h:42
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:33
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:34
#define PNG_FILTER_VALUE_PAETH
Definition: png.h:44
#define PNG_FILTER_VALUE_NONE
Definition: png.h:40
#define PNG_FILTER_TYPE_LOCO
Definition: png.h:39
#define PNG_FILTER_VALUE_SUB
Definition: png.h:41
#define NB_PASSES
Definition: png.h:47
static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s, uint32_t length)
Definition: pngdec.c:576
static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
Definition: pngdec.c:979
static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, AVFrame *p, const AVPacket *avpkt)
Definition: pngdec.c:1223
#define OP_AVG(x, s, l)
#define UNROLL_FILTER(op)
Definition: pngdec.c:252
#define FAST_DIV255(x)
Definition: pngdec.c:1104
static int decode_zbuf(AVBPrint *bp, const uint8_t *data, const uint8_t *data_end)
Definition: pngdec.c:461
static const uint8_t png_pass_mask[NB_PASSES]
Definition: pngdec.c:114
void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
Definition: pngdec.c:201
static void unpack_small_row(PNGDecContext *s, uint8_t *pd, int width)
Definition: pngdec.c:915
#define OP_SUB(x, s, l)
static int output_frame(PNGDecContext *s, AVFrame *f, const AVFrame *src)
Definition: pngdec.c:1510
static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s, uint32_t length)
Definition: pngdec.c:1007
PNGHeaderState
Definition: pngdec.c:42
@ PNG_IHDR
Definition: pngdec.c:43
@ PNG_PLTE
Definition: pngdec.c:44
static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed)
Definition: pngdec.c:529
static uint8_t * iso88591_to_utf8(const uint8_t *in, size_t size_in)
Definition: pngdec.c:505
static av_cold int png_dec_end(AVCodecContext *avctx)
Definition: pngdec.c:1769
void ff_png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type, uint8_t *src, uint8_t *last, int size, int bpp)
Definition: pngdec.c:267
static void png_handle_row(PNGDecContext *s, uint8_t *dst, ptrdiff_t dst_stride)
Definition: pngdec.c:348
static const uint8_t png_pass_dsp_ymask[NB_PASSES]
Definition: pngdec.c:119
static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s)
Definition: pngdec.c:628
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:131
static av_cold int png_dec_init(AVCodecContext *avctx)
Definition: pngdec.c:1749
static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
Definition: pngdec.c:1086
PNGImageState
Definition: pngdec.c:47
@ PNG_ALLIMAGE
Definition: pngdec.c:49
@ PNG_IDAT
Definition: pngdec.c:48
static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s, uint32_t length, AVFrame *p)
Definition: pngdec.c:644
static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s, uint32_t length)
Definition: pngdec.c:826
static int percent_missing(PNGDecContext *s)
Definition: pngdec.c:338
static void clear_frame_metadata(PNGDecContext *s)
Definition: pngdec.c:1497
static void handle_small_bpp_apng(PNGDecContext *s, AVFrame *p)
Definition: pngdec.c:992
#define YUV2RGB(NAME, TYPE)
Definition: pngdec.c:324
static int png_decode_idat(PNGDecContext *s, int length, uint8_t *dst, ptrdiff_t dst_stride)
Definition: pngdec.c:430
static const uint8_t png_pass_dsp_mask[NB_PASSES]
Definition: pngdec.c:124
static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s, uint32_t length)
Definition: pngdec.c:803
static int decode_iccp_chunk(PNGDecContext *s, int length)
Definition: pngdec.c:874
static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s, AVFrame *p)
Definition: pngdec.c:1106
av_cold void ff_pngdsp_init(PNGDSPContext *dsp)
Definition: pngdsp.c:43
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
FF_ENABLE_DEPRECATION_WARNINGS int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
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...
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int debug
debug
Definition: avcodec.h:1627
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1171
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:561
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:1605
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:915
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1796
int discard_damaged_percentage
The percentage of damaged samples to discard a frame.
Definition: avcodec.h:2332
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:637
enum AVCodecID codec_id
Definition: avcodec.h:546
int extradata_size
Definition: avcodec.h:638
void * priv_data
Definition: avcodec.h:563
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:2010
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1649
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
Structure to hold side data for an AVFrame.
Definition: frame.h:220
AVDictionary * metadata
Definition: frame.h:228
uint8_t * data
Definition: frame.h:222
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
int height
Definition: frame.h:376
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:465
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:391
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
Mastering display metadata capable of representing the color volume of the display used to master the...
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
AVRational display_primaries[3][2]
CIE 1931 xy chromaticity coords of color primaries (r, g, b order).
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
This structure stores compressed data.
Definition: packet.h:346
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
Stereo 3D type: this structure describes how two videos are packed within a single video surface,...
Definition: stereo3d.h:176
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:180
int flags
Additional information about the frame packing.
Definition: stereo3d.h:185
void(* add_paeth_prediction)(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
Definition: pngdsp.h:33
void(* add_bytes_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
Definition: pngdsp.h:28
int cur_w
Definition: pngdec.c:75
uint32_t white_point[2]
Definition: pngdec.c:69
int crow_size
Definition: pngdec.c:103
uint8_t * crow_buf
Definition: pngdec.c:95
int buffer_size
Definition: pngdec.c:101
uint8_t dispose_op
Definition: pngdec.c:79
AVDictionary * frame_metadata
Definition: pngdec.c:60
unsigned background_buf_allocated
Definition: pngdec.c:93
int has_trns
Definition: pngdec.c:89
uint8_t transparent_color_be[6]
Definition: pngdec.c:90
int have_chrm
Definition: pngdec.c:68
int subframe_packed
Definition: pngdec.c:109
uint8_t * iccp_data
Definition: pngdec.c:63
int height
Definition: pngdec.c:74
uint8_t * buffer
Definition: pngdec.c:100
unsigned int tmp_row_size
Definition: pngdec.c:99
z_stream zstream
Definition: pngdec.c:110
uint8_t last_dispose_op
Definition: pngdec.c:80
ThreadFrame last_picture
Definition: pngdec.c:57
uint8_t * last_row
Definition: pngdec.c:96
uint32_t palette[256]
Definition: pngdec.c:94
int last_h
Definition: pngdec.c:76
int row_size
Definition: pngdec.c:104
int last_y_offset
Definition: pngdec.c:78
int interlace_type
Definition: pngdec.c:84
int filter_type
Definition: pngdec.c:85
enum PNGImageState pic_state
Definition: pngdec.c:73
uint8_t iccp_name[82]
Definition: pngdec.c:62
uint32_t display_primaries[3][2]
Definition: pngdec.c:70
uint8_t * background_buf
Definition: pngdec.c:92
int channels
Definition: pngdec.c:86
int bit_depth
Definition: pngdec.c:81
int cur_h
Definition: pngdec.c:75
ThreadFrame picture
Definition: pngdec.c:58
uint8_t * tmp_row
Definition: pngdec.c:98
int x_offset
Definition: pngdec.c:77
int compression_type
Definition: pngdec.c:83
int pass_row_size
Definition: pngdec.c:105
uint8_t blend_op
Definition: pngdec.c:79
int bits_per_pixel
Definition: pngdec.c:87
int last_w
Definition: pngdec.c:76
enum PNGHeaderState hdr_state
Definition: pngdec.c:72
unsigned int last_row_size
Definition: pngdec.c:97
AVCodecContext * avctx
Definition: pngdec.c:54
size_t iccp_data_len
Definition: pngdec.c:64
int color_type
Definition: pngdec.c:82
PNGDSPContext dsp
Definition: pngdec.c:53
int width
Definition: pngdec.c:74
int stereo_mode
Definition: pngdec.c:66
int y_offset
Definition: pngdec.c:77
int last_x_offset
Definition: pngdec.c:78
GetByteContext gb
Definition: pngdec.c:56
AVFrame * f
Definition: thread.h:35
#define av_free(p)
#define ff_dlog(a,...)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
static void error(const char *err)
#define src
Definition: vp8dsp.c:255
FILE * out
Definition: movenc.c:54
#define height
#define width
uint8_t pixel
Definition: tiny_ssim.c:42
int size
#define pass
Definition: tx_template.c:347
const char * b
Definition: vf_curves.c:119
const char * g
Definition: vf_curves.c:118
const char * r
Definition: vf_curves.c:117
if(ret< 0)
Definition: vf_mcdeint.c:282
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
Definition: vf_neighbor.c:199
static double c[64]