3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/avassert.h"
25 #include "libavutil/bprint.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/stereo3d.h"
30 #include "bytestream.h"
39 typedef struct PNGDecContext {
41 AVCodecContext *avctx;
44 ThreadFrame previous_picture;
45 ThreadFrame last_picture;
52 int x_offset, y_offset;
53 int last_x_offset, last_y_offset;
54 uint8_t dispose_op, blend_op;
55 uint8_t last_dispose_op;
65 uint8_t transparent_color_be[6];
69 uint32_t palette[256];
72 unsigned int last_row_size;
74 unsigned int tmp_row_size;
78 int crow_size; /* compressed row size (include filter type) */
79 int row_size; /* decompressed row size */
80 int pass_row_size; /* decompress row size of the current pass */
85 /* Mask to determine which pixels are valid in a pass */
86 static const uint8_t png_pass_mask[NB_PASSES] = {
87 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
90 /* Mask to determine which y pixels can be written in a pass */
91 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
92 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
95 /* Mask to determine which pixels to overwrite while displaying */
96 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
97 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
100 /* NOTE: we try to construct a good looking image at each pass. width
101 * is the original image width. We also do pixel format conversion at
103 static void png_put_interlaced_row(uint8_t *dst, int width,
104 int bits_per_pixel, int pass,
105 int color_type, const uint8_t *src)
107 int x, mask, dsp_mask, j, src_x, b, bpp;
111 mask = png_pass_mask[pass];
112 dsp_mask = png_pass_dsp_mask[pass];
114 switch (bits_per_pixel) {
117 for (x = 0; x < width; x++) {
119 if ((dsp_mask << j) & 0x80) {
120 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
121 dst[x >> 3] &= 0xFF7F>>j;
122 dst[x >> 3] |= b << (7 - j);
124 if ((mask << j) & 0x80)
130 for (x = 0; x < width; x++) {
131 int j2 = 2 * (x & 3);
133 if ((dsp_mask << j) & 0x80) {
134 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
135 dst[x >> 2] &= 0xFF3F>>j2;
136 dst[x >> 2] |= b << (6 - j2);
138 if ((mask << j) & 0x80)
144 for (x = 0; x < width; x++) {
147 if ((dsp_mask << j) & 0x80) {
148 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
149 dst[x >> 1] &= 0xFF0F>>j2;
150 dst[x >> 1] |= b << (4 - j2);
152 if ((mask << j) & 0x80)
157 bpp = bits_per_pixel >> 3;
160 for (x = 0; x < width; x++) {
162 if ((dsp_mask << j) & 0x80) {
166 if ((mask << j) & 0x80)
173 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
177 for (i = 0; i < w; i++) {
178 int a, b, c, p, pa, pb, pc;
191 if (pa <= pb && pa <= pc)
201 #define UNROLL1(bpp, op) \
210 for (; i <= size - bpp; i += bpp) { \
211 dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
214 dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
217 dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
220 dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
224 #define UNROLL_FILTER(op) \
227 } else if (bpp == 2) { \
229 } else if (bpp == 3) { \
231 } else if (bpp == 4) { \
234 for (; i < size; i++) { \
235 dst[i] = op(dst[i - bpp], src[i], last[i]); \
238 /* NOTE: 'dst' can be equal to 'last' */
239 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
240 uint8_t *src, uint8_t *last, int size, int bpp)
242 int i, p, r, g, b, a;
244 switch (filter_type) {
245 case PNG_FILTER_VALUE_NONE:
246 memcpy(dst, src, size);
248 case PNG_FILTER_VALUE_SUB:
249 for (i = 0; i < bpp; i++)
253 for (; i < size; i += bpp) {
254 unsigned s = *(int *)(src + i);
255 p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
256 *(int *)(dst + i) = p;
259 #define OP_SUB(x, s, l) ((x) + (s))
260 UNROLL_FILTER(OP_SUB);
263 case PNG_FILTER_VALUE_UP:
264 dsp->add_bytes_l2(dst, src, last, size);
266 case PNG_FILTER_VALUE_AVG:
267 for (i = 0; i < bpp; i++) {
271 #define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
272 UNROLL_FILTER(OP_AVG);
274 case PNG_FILTER_VALUE_PAETH:
275 for (i = 0; i < bpp; i++) {
279 if (bpp > 2 && size > 4) {
280 /* would write off the end of the array if we let it process
281 * the last pixel with bpp=3 */
282 int w = (bpp & 3) ? size - 3 : size;
285 dsp->add_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
289 ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
294 /* This used to be called "deloco" in FFmpeg
295 * and is actually an inverse reversible colorspace transformation */
296 #define YUV2RGB(NAME, TYPE) \
297 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
300 for (i = 0; i < size; i += 3 + alpha) { \
301 int g = dst [i + 1]; \
307 YUV2RGB(rgb8, uint8_t)
308 YUV2RGB(rgb16, uint16_t)
310 /* process exactly one decompressed row */
311 static void png_handle_row(PNGDecContext *s)
313 uint8_t *ptr, *last_row;
316 if (!s->interlace_type) {
317 ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
319 last_row = s->last_row;
321 last_row = ptr - s->image_linesize;
323 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
324 last_row, s->row_size, s->bpp);
325 /* loco lags by 1 row so that it doesn't interfere with top prediction */
326 if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
327 if (s->bit_depth == 16) {
328 deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
329 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
331 deloco_rgb8(ptr - s->image_linesize, s->row_size,
332 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
336 if (s->y == s->cur_h) {
337 s->state |= PNG_ALLIMAGE;
338 if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
339 if (s->bit_depth == 16) {
340 deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
341 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
343 deloco_rgb8(ptr, s->row_size,
344 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
351 ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
352 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
353 /* if we already read one row, it is time to stop to
354 * wait for the next one */
357 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
358 s->last_row, s->pass_row_size, s->bpp);
359 FFSWAP(uint8_t *, s->last_row, s->tmp_row);
360 FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size);
363 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
364 png_put_interlaced_row(ptr, s->cur_w, s->bits_per_pixel, s->pass,
365 s->color_type, s->last_row);
368 if (s->y == s->cur_h) {
369 memset(s->last_row, 0, s->row_size);
371 if (s->pass == NB_PASSES - 1) {
372 s->state |= PNG_ALLIMAGE;
377 s->pass_row_size = ff_png_pass_row_size(s->pass,
380 s->crow_size = s->pass_row_size + 1;
381 if (s->pass_row_size != 0)
383 /* skip pass if empty row */
392 static int png_decode_idat(PNGDecContext *s, int length)
395 s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
396 s->zstream.next_in = (unsigned char *)s->gb.buffer;
397 bytestream2_skip(&s->gb, length);
399 /* decode one line if possible */
400 while (s->zstream.avail_in > 0) {
401 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
402 if (ret != Z_OK && ret != Z_STREAM_END) {
403 av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
404 return AVERROR_EXTERNAL;
406 if (s->zstream.avail_out == 0) {
407 if (!(s->state & PNG_ALLIMAGE)) {
410 s->zstream.avail_out = s->crow_size;
411 s->zstream.next_out = s->crow_buf;
413 if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
414 av_log(NULL, AV_LOG_WARNING,
415 "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
422 static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
423 const uint8_t *data_end)
430 zstream.zalloc = ff_png_zalloc;
431 zstream.zfree = ff_png_zfree;
432 zstream.opaque = NULL;
433 if (inflateInit(&zstream) != Z_OK)
434 return AVERROR_EXTERNAL;
435 zstream.next_in = (unsigned char *)data;
436 zstream.avail_in = data_end - data;
437 av_bprint_init(bp, 0, -1);
439 while (zstream.avail_in > 0) {
440 av_bprint_get_buffer(bp, 2, &buf, &buf_size);
442 ret = AVERROR(ENOMEM);
445 zstream.next_out = buf;
446 zstream.avail_out = buf_size - 1;
447 ret = inflate(&zstream, Z_PARTIAL_FLUSH);
448 if (ret != Z_OK && ret != Z_STREAM_END) {
449 ret = AVERROR_EXTERNAL;
452 bp->len += zstream.next_out - buf;
453 if (ret == Z_STREAM_END)
456 inflateEnd(&zstream);
457 bp->str[bp->len] = 0;
461 inflateEnd(&zstream);
462 av_bprint_finalize(bp, NULL);
466 static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
471 for (i = 0; i < size_in; i++)
472 extra += in[i] >= 0x80;
473 if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
475 q = out = av_malloc(size_in + extra + 1);
478 for (i = 0; i < size_in; i++) {
480 *(q++) = 0xC0 | (in[i] >> 6);
481 *(q++) = 0x80 | (in[i] & 0x3F);
490 static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed,
494 const uint8_t *data = s->gb.buffer;
495 const uint8_t *data_end = data + length;
496 const uint8_t *keyword = data;
497 const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
498 uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
503 return AVERROR_INVALIDDATA;
504 data = keyword_end + 1;
507 if (data == data_end)
508 return AVERROR_INVALIDDATA;
511 return AVERROR_INVALIDDATA;
512 if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
515 av_bprint_finalize(&bp, (char **)&text);
517 return AVERROR(ENOMEM);
519 text = (uint8_t *)data;
520 text_len = data_end - text;
523 kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
524 txt_utf8 = iso88591_to_utf8(text, text_len);
527 if (!(kw_utf8 && txt_utf8)) {
530 return AVERROR(ENOMEM);
533 av_dict_set(dict, kw_utf8, txt_utf8,
534 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
538 static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s,
542 return AVERROR_INVALIDDATA;
544 if (s->state & PNG_IDAT) {
545 av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");
546 return AVERROR_INVALIDDATA;
549 if (s->state & PNG_IHDR) {
550 av_log(avctx, AV_LOG_ERROR, "Multiple IHDR\n");
551 return AVERROR_INVALIDDATA;
554 s->width = s->cur_w = bytestream2_get_be32(&s->gb);
555 s->height = s->cur_h = bytestream2_get_be32(&s->gb);
556 if (av_image_check_size(s->width, s->height, 0, avctx)) {
557 s->cur_w = s->cur_h = s->width = s->height = 0;
558 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
559 return AVERROR_INVALIDDATA;
561 s->bit_depth = bytestream2_get_byte(&s->gb);
562 if (s->bit_depth != 1 && s->bit_depth != 2 && s->bit_depth != 4 &&
563 s->bit_depth != 8 && s->bit_depth != 16) {
564 av_log(avctx, AV_LOG_ERROR, "Invalid bit depth\n");
567 s->color_type = bytestream2_get_byte(&s->gb);
568 s->compression_type = bytestream2_get_byte(&s->gb);
569 s->filter_type = bytestream2_get_byte(&s->gb);
570 s->interlace_type = bytestream2_get_byte(&s->gb);
571 bytestream2_skip(&s->gb, 4); /* crc */
572 s->state |= PNG_IHDR;
573 if (avctx->debug & FF_DEBUG_PICT_INFO)
574 av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
575 "compression_type=%d filter_type=%d interlace_type=%d\n",
576 s->width, s->height, s->bit_depth, s->color_type,
577 s->compression_type, s->filter_type, s->interlace_type);
581 s->cur_w = s->cur_h = s->width = s->height = 0;
583 return AVERROR_INVALIDDATA;
586 static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s)
588 if (s->state & PNG_IDAT) {
589 av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
590 return AVERROR_INVALIDDATA;
592 avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
593 avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
594 if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
595 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
596 bytestream2_skip(&s->gb, 1); /* unit specifier */
597 bytestream2_skip(&s->gb, 4); /* crc */
602 static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s,
603 uint32_t length, AVFrame *p)
606 size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
608 if (!(s->state & PNG_IHDR)) {
609 av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
610 return AVERROR_INVALIDDATA;
612 if (!(s->state & PNG_IDAT)) {
613 /* init image info */
614 avctx->width = s->width;
615 avctx->height = s->height;
617 s->channels = ff_png_get_nb_channels(s->color_type);
618 s->bits_per_pixel = s->bit_depth * s->channels;
619 s->bpp = (s->bits_per_pixel + 7) >> 3;
620 s->row_size = (s->cur_w * s->bits_per_pixel + 7) >> 3;
622 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
623 s->color_type == PNG_COLOR_TYPE_RGB) {
624 avctx->pix_fmt = AV_PIX_FMT_RGB24;
625 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
626 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
627 avctx->pix_fmt = AV_PIX_FMT_RGBA;
628 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
629 s->color_type == PNG_COLOR_TYPE_GRAY) {
630 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
631 } else if (s->bit_depth == 16 &&
632 s->color_type == PNG_COLOR_TYPE_GRAY) {
633 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
634 } else if (s->bit_depth == 16 &&
635 s->color_type == PNG_COLOR_TYPE_RGB) {
636 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
637 } else if (s->bit_depth == 16 &&
638 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
639 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
640 } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
641 s->color_type == PNG_COLOR_TYPE_PALETTE) {
642 avctx->pix_fmt = AV_PIX_FMT_PAL8;
643 } else if (s->bit_depth == 1 && s->bits_per_pixel == 1 && avctx->codec_id != AV_CODEC_ID_APNG) {
644 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
645 } else if (s->bit_depth == 8 &&
646 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
647 avctx->pix_fmt = AV_PIX_FMT_YA8;
648 } else if (s->bit_depth == 16 &&
649 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
650 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
652 av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
653 "and color type %d\n",
654 s->bit_depth, s->color_type);
655 return AVERROR_INVALIDDATA;
658 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
659 switch (avctx->pix_fmt) {
660 case AV_PIX_FMT_RGB24:
661 avctx->pix_fmt = AV_PIX_FMT_RGBA;
664 case AV_PIX_FMT_RGB48BE:
665 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
668 case AV_PIX_FMT_GRAY8:
669 avctx->pix_fmt = AV_PIX_FMT_YA8;
672 case AV_PIX_FMT_GRAY16BE:
673 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
677 avpriv_request_sample(avctx, "bit depth %d "
678 "and color type %d with TRNS",
679 s->bit_depth, s->color_type);
680 return AVERROR_INVALIDDATA;
683 s->bpp += byte_depth;
686 if ((ret = ff_thread_get_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
688 if (avctx->codec_id == AV_CODEC_ID_APNG && s->last_dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
689 ff_thread_release_buffer(avctx, &s->previous_picture);
690 if ((ret = ff_thread_get_buffer(avctx, &s->previous_picture, AV_GET_BUFFER_FLAG_REF)) < 0)
693 ff_thread_finish_setup(avctx);
695 p->pict_type = AV_PICTURE_TYPE_I;
697 p->interlaced_frame = !!s->interlace_type;
699 /* compute the compressed row size */
700 if (!s->interlace_type) {
701 s->crow_size = s->row_size + 1;
704 s->pass_row_size = ff_png_pass_row_size(s->pass,
707 s->crow_size = s->pass_row_size + 1;
709 ff_dlog(avctx, "row_size=%d crow_size =%d\n",
710 s->row_size, s->crow_size);
711 s->image_buf = p->data[0];
712 s->image_linesize = p->linesize[0];
713 /* copy the palette if needed */
714 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
715 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
716 /* empty row is used if differencing to the first row */
717 av_fast_padded_mallocz(&s->last_row, &s->last_row_size, s->row_size);
719 return AVERROR_INVALIDDATA;
720 if (s->interlace_type ||
721 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
722 av_fast_padded_malloc(&s->tmp_row, &s->tmp_row_size, s->row_size);
724 return AVERROR_INVALIDDATA;
727 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
729 return AVERROR(ENOMEM);
731 /* we want crow_buf+1 to be 16-byte aligned */
732 s->crow_buf = s->buffer + 15;
733 s->zstream.avail_out = s->crow_size;
734 s->zstream.next_out = s->crow_buf;
737 s->state |= PNG_IDAT;
739 /* set image to non-transparent bpp while decompressing */
740 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
741 s->bpp -= byte_depth;
743 ret = png_decode_idat(s, length);
745 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
746 s->bpp += byte_depth;
751 bytestream2_skip(&s->gb, 4); /* crc */
756 static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s,
761 if ((length % 3) != 0 || length > 256 * 3)
762 return AVERROR_INVALIDDATA;
763 /* read the palette */
765 for (i = 0; i < n; i++) {
766 r = bytestream2_get_byte(&s->gb);
767 g = bytestream2_get_byte(&s->gb);
768 b = bytestream2_get_byte(&s->gb);
769 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
772 s->palette[i] = (0xFFU << 24);
773 s->state |= PNG_PLTE;
774 bytestream2_skip(&s->gb, 4); /* crc */
779 static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,
784 if (!(s->state & PNG_IHDR)) {
785 av_log(avctx, AV_LOG_ERROR, "trns before IHDR\n");
786 return AVERROR_INVALIDDATA;
789 if (s->state & PNG_IDAT) {
790 av_log(avctx, AV_LOG_ERROR, "trns after IDAT\n");
791 return AVERROR_INVALIDDATA;
794 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
795 if (length > 256 || !(s->state & PNG_PLTE))
796 return AVERROR_INVALIDDATA;
798 for (i = 0; i < length; i++) {
799 unsigned v = bytestream2_get_byte(&s->gb);
800 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
802 } else if (s->color_type == PNG_COLOR_TYPE_GRAY || s->color_type == PNG_COLOR_TYPE_RGB) {
803 if ((s->color_type == PNG_COLOR_TYPE_GRAY && length != 2) ||
804 (s->color_type == PNG_COLOR_TYPE_RGB && length != 6) ||
806 return AVERROR_INVALIDDATA;
808 for (i = 0; i < length / 2; i++) {
809 /* only use the least significant bits */
810 v = av_mod_uintp2(bytestream2_get_be16(&s->gb), s->bit_depth);
812 if (s->bit_depth > 8)
813 AV_WB16(&s->transparent_color_be[2 * i], v);
815 s->transparent_color_be[i] = v;
818 return AVERROR_INVALIDDATA;
821 bytestream2_skip(&s->gb, 4); /* crc */
827 static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
829 if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
831 uint8_t *pd = p->data[0];
832 for (j = 0; j < s->height; j++) {
834 for (k = 7; k >= 1; k--)
835 if ((s->width&7) >= k)
836 pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
837 for (i--; i >= 0; i--) {
838 pd[8*i + 7]= pd[i] & 1;
839 pd[8*i + 6]= (pd[i]>>1) & 1;
840 pd[8*i + 5]= (pd[i]>>2) & 1;
841 pd[8*i + 4]= (pd[i]>>3) & 1;
842 pd[8*i + 3]= (pd[i]>>4) & 1;
843 pd[8*i + 2]= (pd[i]>>5) & 1;
844 pd[8*i + 1]= (pd[i]>>6) & 1;
845 pd[8*i + 0]= pd[i]>>7;
847 pd += s->image_linesize;
849 } else if (s->bits_per_pixel == 2) {
851 uint8_t *pd = p->data[0];
852 for (j = 0; j < s->height; j++) {
854 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
855 if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
856 if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
857 if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
858 for (i--; i >= 0; i--) {
859 pd[4*i + 3]= pd[i] & 3;
860 pd[4*i + 2]= (pd[i]>>2) & 3;
861 pd[4*i + 1]= (pd[i]>>4) & 3;
862 pd[4*i + 0]= pd[i]>>6;
865 if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
866 if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
867 if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
868 for (i--; i >= 0; i--) {
869 pd[4*i + 3]= ( pd[i] & 3)*0x55;
870 pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
871 pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
872 pd[4*i + 0]= ( pd[i]>>6 )*0x55;
875 pd += s->image_linesize;
877 } else if (s->bits_per_pixel == 4) {
879 uint8_t *pd = p->data[0];
880 for (j = 0; j < s->height; j++) {
882 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
883 if (s->width&1) pd[2*i+0]= pd[i]>>4;
884 for (i--; i >= 0; i--) {
885 pd[2*i + 1] = pd[i] & 15;
886 pd[2*i + 0] = pd[i] >> 4;
889 if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
890 for (i--; i >= 0; i--) {
891 pd[2*i + 1] = (pd[i] & 15) * 0x11;
892 pd[2*i + 0] = (pd[i] >> 4) * 0x11;
895 pd += s->image_linesize;
900 static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s,
903 uint32_t sequence_number;
904 int cur_w, cur_h, x_offset, y_offset, dispose_op, blend_op;
907 return AVERROR_INVALIDDATA;
909 if (!(s->state & PNG_IHDR)) {
910 av_log(avctx, AV_LOG_ERROR, "fctl before IHDR\n");
911 return AVERROR_INVALIDDATA;
914 s->last_w = s->cur_w;
915 s->last_h = s->cur_h;
916 s->last_x_offset = s->x_offset;
917 s->last_y_offset = s->y_offset;
918 s->last_dispose_op = s->dispose_op;
920 sequence_number = bytestream2_get_be32(&s->gb);
921 cur_w = bytestream2_get_be32(&s->gb);
922 cur_h = bytestream2_get_be32(&s->gb);
923 x_offset = bytestream2_get_be32(&s->gb);
924 y_offset = bytestream2_get_be32(&s->gb);
925 bytestream2_skip(&s->gb, 4); /* delay_num (2), delay_den (2) */
926 dispose_op = bytestream2_get_byte(&s->gb);
927 blend_op = bytestream2_get_byte(&s->gb);
928 bytestream2_skip(&s->gb, 4); /* crc */
930 if (sequence_number == 0 &&
931 (cur_w != s->width ||
932 cur_h != s->height ||
935 cur_w <= 0 || cur_h <= 0 ||
936 x_offset < 0 || y_offset < 0 ||
937 cur_w > s->width - x_offset|| cur_h > s->height - y_offset)
938 return AVERROR_INVALIDDATA;
940 if (blend_op != APNG_BLEND_OP_OVER && blend_op != APNG_BLEND_OP_SOURCE) {
941 av_log(avctx, AV_LOG_ERROR, "Invalid blend_op %d\n", blend_op);
942 return AVERROR_INVALIDDATA;
945 if ((sequence_number == 0 || !s->previous_picture.f->data[0]) &&
946 dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
947 // No previous frame to revert to for the first frame
948 // Spec says to just treat it as a APNG_DISPOSE_OP_BACKGROUND
949 dispose_op = APNG_DISPOSE_OP_BACKGROUND;
952 if (blend_op == APNG_BLEND_OP_OVER && !s->has_trns && (
953 avctx->pix_fmt == AV_PIX_FMT_RGB24 ||
954 avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
955 avctx->pix_fmt == AV_PIX_FMT_PAL8 ||
956 avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
957 avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
958 avctx->pix_fmt == AV_PIX_FMT_MONOBLACK
960 // APNG_BLEND_OP_OVER is the same as APNG_BLEND_OP_SOURCE when there is no alpha channel
961 blend_op = APNG_BLEND_OP_SOURCE;
966 s->x_offset = x_offset;
967 s->y_offset = y_offset;
968 s->dispose_op = dispose_op;
969 s->blend_op = blend_op;
974 static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
977 uint8_t *pd = p->data[0];
978 uint8_t *pd_last = s->last_picture.f->data[0];
979 int ls = FFMIN(av_image_get_linesize(p->format, s->width, 0), s->width * s->bpp);
981 ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
982 for (j = 0; j < s->height; j++) {
983 for (i = 0; i < ls; i++)
985 pd += s->image_linesize;
986 pd_last += s->image_linesize;
990 // divide by 255 and round to nearest
991 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
992 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
994 static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s,
1000 if (s->blend_op == APNG_BLEND_OP_OVER &&
1001 avctx->pix_fmt != AV_PIX_FMT_RGBA &&
1002 avctx->pix_fmt != AV_PIX_FMT_GRAY8A &&
1003 avctx->pix_fmt != AV_PIX_FMT_PAL8) {
1004 avpriv_request_sample(avctx, "Blending with pixel format %s",
1005 av_get_pix_fmt_name(avctx->pix_fmt));
1006 return AVERROR_PATCHWELCOME;
1009 buffer = av_malloc_array(s->image_linesize, s->height);
1011 return AVERROR(ENOMEM);
1014 // Do the disposal operation specified by the last frame on the frame
1015 if (s->last_dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
1016 ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
1017 memcpy(buffer, s->last_picture.f->data[0], s->image_linesize * s->height);
1019 if (s->last_dispose_op == APNG_DISPOSE_OP_BACKGROUND)
1020 for (y = s->last_y_offset; y < s->last_y_offset + s->last_h; ++y)
1021 memset(buffer + s->image_linesize * y + s->bpp * s->last_x_offset, 0, s->bpp * s->last_w);
1023 memcpy(s->previous_picture.f->data[0], buffer, s->image_linesize * s->height);
1024 ff_thread_report_progress(&s->previous_picture, INT_MAX, 0);
1026 ff_thread_await_progress(&s->previous_picture, INT_MAX, 0);
1027 memcpy(buffer, s->previous_picture.f->data[0], s->image_linesize * s->height);
1031 if (s->blend_op == APNG_BLEND_OP_SOURCE) {
1032 for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
1033 size_t row_start = s->image_linesize * y + s->bpp * s->x_offset;
1034 memcpy(buffer + row_start, p->data[0] + row_start, s->bpp * s->cur_w);
1036 } else { // APNG_BLEND_OP_OVER
1037 for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
1038 uint8_t *foreground = p->data[0] + s->image_linesize * y + s->bpp * s->x_offset;
1039 uint8_t *background = buffer + s->image_linesize * y + s->bpp * s->x_offset;
1040 for (x = s->x_offset; x < s->x_offset + s->cur_w; ++x, foreground += s->bpp, background += s->bpp) {
1042 uint8_t foreground_alpha, background_alpha, output_alpha;
1045 // Since we might be blending alpha onto alpha, we use the following equations:
1046 // output_alpha = foreground_alpha + (1 - foreground_alpha) * background_alpha
1047 // output = (foreground_alpha * foreground + (1 - foreground_alpha) * background_alpha * background) / output_alpha
1049 switch (avctx->pix_fmt) {
1050 case AV_PIX_FMT_RGBA:
1051 foreground_alpha = foreground[3];
1052 background_alpha = background[3];
1055 case AV_PIX_FMT_GRAY8A:
1056 foreground_alpha = foreground[1];
1057 background_alpha = background[1];
1060 case AV_PIX_FMT_PAL8:
1061 foreground_alpha = s->palette[foreground[0]] >> 24;
1062 background_alpha = s->palette[background[0]] >> 24;
1066 if (foreground_alpha == 0)
1069 if (foreground_alpha == 255) {
1070 memcpy(background, foreground, s->bpp);
1074 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1075 // TODO: Alpha blending with PAL8 will likely need the entire image converted over to RGBA first
1076 avpriv_request_sample(avctx, "Alpha blending palette samples");
1077 background[0] = foreground[0];
1081 output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);
1083 av_assert0(s->bpp <= 10);
1085 for (b = 0; b < s->bpp - 1; ++b) {
1086 if (output_alpha == 0) {
1088 } else if (background_alpha == 255) {
1089 output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);
1091 output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);
1094 output[b] = output_alpha;
1095 memcpy(background, output, s->bpp);
1100 // Copy blended buffer into the frame and free
1101 memcpy(p->data[0], buffer, s->image_linesize * s->height);
1107 static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
1108 AVFrame *p, AVPacket *avpkt)
1110 AVDictionary **metadatap = NULL;
1111 uint32_t tag, length;
1112 int decode_next_dat = 0;
1116 length = bytestream2_get_bytes_left(&s->gb);
1119 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1120 avctx->skip_frame == AVDISCARD_ALL) {
1124 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && length == 0) {
1125 if (!(s->state & PNG_IDAT))
1130 av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length);
1131 if ( s->state & PNG_ALLIMAGE
1132 && avctx->strict_std_compliance <= FF_COMPLIANCE_NORMAL)
1134 ret = AVERROR_INVALIDDATA;
1138 length = bytestream2_get_be32(&s->gb);
1139 if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
1140 av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
1141 ret = AVERROR_INVALIDDATA;
1144 tag = bytestream2_get_le32(&s->gb);
1145 if (avctx->debug & FF_DEBUG_STARTCODE)
1146 av_log(avctx, AV_LOG_DEBUG, "png: tag=%s length=%u\n",
1147 av_fourcc2str(tag), length);
1149 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1150 avctx->skip_frame == AVDISCARD_ALL) {
1152 case MKTAG('I', 'H', 'D', 'R'):
1153 case MKTAG('p', 'H', 'Y', 's'):
1154 case MKTAG('t', 'E', 'X', 't'):
1155 case MKTAG('I', 'D', 'A', 'T'):
1156 case MKTAG('t', 'R', 'N', 'S'):
1163 metadatap = avpriv_frame_get_metadatap(p);
1165 case MKTAG('I', 'H', 'D', 'R'):
1166 if ((ret = decode_ihdr_chunk(avctx, s, length)) < 0)
1169 case MKTAG('p', 'H', 'Y', 's'):
1170 if ((ret = decode_phys_chunk(avctx, s)) < 0)
1173 case MKTAG('f', 'c', 'T', 'L'):
1174 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1176 if ((ret = decode_fctl_chunk(avctx, s, length)) < 0)
1178 decode_next_dat = 1;
1180 case MKTAG('f', 'd', 'A', 'T'):
1181 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1183 if (!decode_next_dat) {
1184 ret = AVERROR_INVALIDDATA;
1187 bytestream2_get_be32(&s->gb);
1190 case MKTAG('I', 'D', 'A', 'T'):
1191 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
1193 if ((ret = decode_idat_chunk(avctx, s, length, p)) < 0)
1196 case MKTAG('P', 'L', 'T', 'E'):
1197 if (decode_plte_chunk(avctx, s, length) < 0)
1200 case MKTAG('t', 'R', 'N', 'S'):
1201 if (decode_trns_chunk(avctx, s, length) < 0)
1204 case MKTAG('t', 'E', 'X', 't'):
1205 if (decode_text_chunk(s, length, 0, metadatap) < 0)
1206 av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
1207 bytestream2_skip(&s->gb, length + 4);
1209 case MKTAG('z', 'T', 'X', 't'):
1210 if (decode_text_chunk(s, length, 1, metadatap) < 0)
1211 av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
1212 bytestream2_skip(&s->gb, length + 4);
1214 case MKTAG('s', 'T', 'E', 'R'): {
1215 int mode = bytestream2_get_byte(&s->gb);
1216 AVStereo3D *stereo3d = av_stereo3d_create_side_data(p);
1220 if (mode == 0 || mode == 1) {
1221 stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
1222 stereo3d->flags = mode ? 0 : AV_STEREO3D_FLAG_INVERT;
1224 av_log(avctx, AV_LOG_WARNING,
1225 "Unknown value in sTER chunk (%d)\n", mode);
1227 bytestream2_skip(&s->gb, 4); /* crc */
1230 case MKTAG('I', 'E', 'N', 'D'):
1231 if (!(s->state & PNG_ALLIMAGE))
1232 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
1233 if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
1234 ret = AVERROR_INVALIDDATA;
1237 bytestream2_skip(&s->gb, 4); /* crc */
1242 bytestream2_skip(&s->gb, length + 4);
1248 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1249 avctx->skip_frame == AVDISCARD_ALL) {
1253 if (s->bits_per_pixel <= 4)
1254 handle_small_bpp(s, p);
1256 /* apply transparency if needed */
1257 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
1258 size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
1259 size_t raw_bpp = s->bpp - byte_depth;
1262 av_assert0(s->bit_depth > 1);
1264 for (y = 0; y < s->height; ++y) {
1265 uint8_t *row = &s->image_buf[s->image_linesize * y];
1267 /* since we're updating in-place, we have to go from right to left */
1268 for (x = s->width; x > 0; --x) {
1269 uint8_t *pixel = &row[s->bpp * (x - 1)];
1270 memmove(pixel, &row[raw_bpp * (x - 1)], raw_bpp);
1272 if (!memcmp(pixel, s->transparent_color_be, raw_bpp)) {
1273 memset(&pixel[raw_bpp], 0, byte_depth);
1275 memset(&pixel[raw_bpp], 0xff, byte_depth);
1281 /* handle P-frames only if a predecessor frame is available */
1282 if (s->last_picture.f->data[0]) {
1283 if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
1284 && s->last_picture.f->width == p->width
1285 && s->last_picture.f->height== p->height
1286 && s->last_picture.f->format== p->format
1288 if (CONFIG_PNG_DECODER && avctx->codec_id != AV_CODEC_ID_APNG)
1289 handle_p_frame_png(s, p);
1290 else if (CONFIG_APNG_DECODER &&
1291 avctx->codec_id == AV_CODEC_ID_APNG &&
1292 (ret = handle_p_frame_apng(avctx, s, p)) < 0)
1296 ff_thread_report_progress(&s->picture, INT_MAX, 0);
1297 ff_thread_report_progress(&s->previous_picture, INT_MAX, 0);
1302 ff_thread_report_progress(&s->picture, INT_MAX, 0);
1303 ff_thread_report_progress(&s->previous_picture, INT_MAX, 0);
1307 #if CONFIG_PNG_DECODER
1308 static int decode_frame_png(AVCodecContext *avctx,
1309 void *data, int *got_frame,
1312 PNGDecContext *const s = avctx->priv_data;
1313 const uint8_t *buf = avpkt->data;
1314 int buf_size = avpkt->size;
1319 ff_thread_release_buffer(avctx, &s->last_picture);
1320 FFSWAP(ThreadFrame, s->picture, s->last_picture);
1323 bytestream2_init(&s->gb, buf, buf_size);
1325 /* check signature */
1326 sig = bytestream2_get_be64(&s->gb);
1327 if (sig != PNGSIG &&
1329 av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature 0x%08"PRIX64".\n", sig);
1330 return AVERROR_INVALIDDATA;
1333 s->y = s->state = s->has_trns = 0;
1336 s->zstream.zalloc = ff_png_zalloc;
1337 s->zstream.zfree = ff_png_zfree;
1338 s->zstream.opaque = NULL;
1339 ret = inflateInit(&s->zstream);
1341 av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1342 return AVERROR_EXTERNAL;
1345 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1348 if (avctx->skip_frame == AVDISCARD_ALL) {
1350 ret = bytestream2_tell(&s->gb);
1354 if ((ret = av_frame_ref(data, s->picture.f)) < 0)
1359 ret = bytestream2_tell(&s->gb);
1361 inflateEnd(&s->zstream);
1367 #if CONFIG_APNG_DECODER
1368 static int decode_frame_apng(AVCodecContext *avctx,
1369 void *data, int *got_frame,
1372 PNGDecContext *const s = avctx->priv_data;
1376 ff_thread_release_buffer(avctx, &s->last_picture);
1377 FFSWAP(ThreadFrame, s->picture, s->last_picture);
1380 if (!(s->state & PNG_IHDR)) {
1381 if (!avctx->extradata_size)
1382 return AVERROR_INVALIDDATA;
1384 /* only init fields, there is no zlib use in extradata */
1385 s->zstream.zalloc = ff_png_zalloc;
1386 s->zstream.zfree = ff_png_zfree;
1388 bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
1389 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1393 /* reset state for a new frame */
1394 if ((ret = inflateInit(&s->zstream)) != Z_OK) {
1395 av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1396 ret = AVERROR_EXTERNAL;
1400 s->state &= ~(PNG_IDAT | PNG_ALLIMAGE);
1401 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1402 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1405 if (!(s->state & PNG_ALLIMAGE))
1406 av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
1407 if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
1408 ret = AVERROR_INVALIDDATA;
1411 if ((ret = av_frame_ref(data, s->picture.f)) < 0)
1415 ret = bytestream2_tell(&s->gb);
1418 inflateEnd(&s->zstream);
1424 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1426 PNGDecContext *psrc = src->priv_data;
1427 PNGDecContext *pdst = dst->priv_data;
1433 ff_thread_release_buffer(dst, &pdst->picture);
1434 if (psrc->picture.f->data[0] &&
1435 (ret = ff_thread_ref_frame(&pdst->picture, &psrc->picture)) < 0)
1437 if (CONFIG_APNG_DECODER && dst->codec_id == AV_CODEC_ID_APNG) {
1438 pdst->width = psrc->width;
1439 pdst->height = psrc->height;
1440 pdst->bit_depth = psrc->bit_depth;
1441 pdst->color_type = psrc->color_type;
1442 pdst->compression_type = psrc->compression_type;
1443 pdst->interlace_type = psrc->interlace_type;
1444 pdst->filter_type = psrc->filter_type;
1445 pdst->cur_w = psrc->cur_w;
1446 pdst->cur_h = psrc->cur_h;
1447 pdst->x_offset = psrc->x_offset;
1448 pdst->y_offset = psrc->y_offset;
1449 pdst->has_trns = psrc->has_trns;
1450 memcpy(pdst->transparent_color_be, psrc->transparent_color_be, sizeof(pdst->transparent_color_be));
1452 pdst->dispose_op = psrc->dispose_op;
1454 memcpy(pdst->palette, psrc->palette, sizeof(pdst->palette));
1456 pdst->state |= psrc->state & (PNG_IHDR | PNG_PLTE);
1458 ff_thread_release_buffer(dst, &pdst->last_picture);
1459 if (psrc->last_picture.f->data[0] &&
1460 (ret = ff_thread_ref_frame(&pdst->last_picture, &psrc->last_picture)) < 0)
1463 ff_thread_release_buffer(dst, &pdst->previous_picture);
1464 if (psrc->previous_picture.f->data[0] &&
1465 (ret = ff_thread_ref_frame(&pdst->previous_picture, &psrc->previous_picture)) < 0)
1473 static av_cold int png_dec_init(AVCodecContext *avctx)
1475 PNGDecContext *s = avctx->priv_data;
1477 avctx->color_range = AVCOL_RANGE_JPEG;
1480 s->previous_picture.f = av_frame_alloc();
1481 s->last_picture.f = av_frame_alloc();
1482 s->picture.f = av_frame_alloc();
1483 if (!s->previous_picture.f || !s->last_picture.f || !s->picture.f) {
1484 av_frame_free(&s->previous_picture.f);
1485 av_frame_free(&s->last_picture.f);
1486 av_frame_free(&s->picture.f);
1487 return AVERROR(ENOMEM);
1490 if (!avctx->internal->is_copy) {
1491 avctx->internal->allocate_progress = 1;
1492 ff_pngdsp_init(&s->dsp);
1498 static av_cold int png_dec_end(AVCodecContext *avctx)
1500 PNGDecContext *s = avctx->priv_data;
1502 ff_thread_release_buffer(avctx, &s->previous_picture);
1503 av_frame_free(&s->previous_picture.f);
1504 ff_thread_release_buffer(avctx, &s->last_picture);
1505 av_frame_free(&s->last_picture.f);
1506 ff_thread_release_buffer(avctx, &s->picture);
1507 av_frame_free(&s->picture.f);
1508 av_freep(&s->buffer);
1510 av_freep(&s->last_row);
1511 s->last_row_size = 0;
1512 av_freep(&s->tmp_row);
1513 s->tmp_row_size = 0;
1518 #if CONFIG_APNG_DECODER
1519 AVCodec ff_apng_decoder = {
1521 .long_name = NULL_IF_CONFIG_SMALL("APNG (Animated Portable Network Graphics) image"),
1522 .type = AVMEDIA_TYPE_VIDEO,
1523 .id = AV_CODEC_ID_APNG,
1524 .priv_data_size = sizeof(PNGDecContext),
1525 .init = png_dec_init,
1526 .close = png_dec_end,
1527 .decode = decode_frame_apng,
1528 .init_thread_copy = ONLY_IF_THREADS_ENABLED(png_dec_init),
1529 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1530 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
1531 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1535 #if CONFIG_PNG_DECODER
1536 AVCodec ff_png_decoder = {
1538 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
1539 .type = AVMEDIA_TYPE_VIDEO,
1540 .id = AV_CODEC_ID_PNG,
1541 .priv_data_size = sizeof(PNGDecContext),
1542 .init = png_dec_init,
1543 .close = png_dec_end,
1544 .decode = decode_frame_png,
1545 .init_thread_copy = ONLY_IF_THREADS_ENABLED(png_dec_init),
1546 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1547 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
1548 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_INIT_THREADSAFE,