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