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"
46 PNG_ALLIMAGE = 1 << 1,
49 typedef struct PNGDecContext {
51 AVCodecContext *avctx;
54 ThreadFrame previous_picture;
55 ThreadFrame last_picture;
58 enum PNGHeaderState hdr_state;
59 enum PNGImageState pic_state;
63 int x_offset, y_offset;
64 int last_x_offset, last_y_offset;
65 uint8_t dispose_op, blend_op;
66 uint8_t last_dispose_op;
76 uint8_t transparent_color_be[6];
80 uint32_t palette[256];
83 unsigned int last_row_size;
85 unsigned int tmp_row_size;
89 int crow_size; /* compressed row size (include filter type) */
90 int row_size; /* decompressed row size */
91 int pass_row_size; /* decompress row size of the current pass */
96 /* Mask to determine which pixels are valid in a pass */
97 static const uint8_t png_pass_mask[NB_PASSES] = {
98 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
101 /* Mask to determine which y pixels can be written in a pass */
102 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
103 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
106 /* Mask to determine which pixels to overwrite while displaying */
107 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
108 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
111 /* NOTE: we try to construct a good looking image at each pass. width
112 * is the original image width. We also do pixel format conversion at
114 static void png_put_interlaced_row(uint8_t *dst, int width,
115 int bits_per_pixel, int pass,
116 int color_type, const uint8_t *src)
118 int x, mask, dsp_mask, j, src_x, b, bpp;
122 mask = png_pass_mask[pass];
123 dsp_mask = png_pass_dsp_mask[pass];
125 switch (bits_per_pixel) {
128 for (x = 0; x < width; x++) {
130 if ((dsp_mask << j) & 0x80) {
131 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
132 dst[x >> 3] &= 0xFF7F>>j;
133 dst[x >> 3] |= b << (7 - j);
135 if ((mask << j) & 0x80)
141 for (x = 0; x < width; x++) {
142 int j2 = 2 * (x & 3);
144 if ((dsp_mask << j) & 0x80) {
145 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
146 dst[x >> 2] &= 0xFF3F>>j2;
147 dst[x >> 2] |= b << (6 - j2);
149 if ((mask << j) & 0x80)
155 for (x = 0; x < width; x++) {
158 if ((dsp_mask << j) & 0x80) {
159 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
160 dst[x >> 1] &= 0xFF0F>>j2;
161 dst[x >> 1] |= b << (4 - j2);
163 if ((mask << j) & 0x80)
168 bpp = bits_per_pixel >> 3;
171 for (x = 0; x < width; x++) {
173 if ((dsp_mask << j) & 0x80) {
177 if ((mask << j) & 0x80)
184 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
188 for (i = 0; i < w; i++) {
189 int a, b, c, p, pa, pb, pc;
202 if (pa <= pb && pa <= pc)
212 #define UNROLL1(bpp, op) \
221 for (; i <= size - bpp; i += bpp) { \
222 dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
225 dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
228 dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
231 dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
235 #define UNROLL_FILTER(op) \
238 } else if (bpp == 2) { \
240 } else if (bpp == 3) { \
242 } else if (bpp == 4) { \
245 for (; i < size; i++) { \
246 dst[i] = op(dst[i - bpp], src[i], last[i]); \
249 /* NOTE: 'dst' can be equal to 'last' */
250 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
251 uint8_t *src, uint8_t *last, int size, int bpp)
253 int i, p, r, g, b, a;
255 switch (filter_type) {
256 case PNG_FILTER_VALUE_NONE:
257 memcpy(dst, src, size);
259 case PNG_FILTER_VALUE_SUB:
260 for (i = 0; i < bpp; i++)
264 for (; i < size; i += bpp) {
265 unsigned s = *(int *)(src + i);
266 p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
267 *(int *)(dst + i) = p;
270 #define OP_SUB(x, s, l) ((x) + (s))
271 UNROLL_FILTER(OP_SUB);
274 case PNG_FILTER_VALUE_UP:
275 dsp->add_bytes_l2(dst, src, last, size);
277 case PNG_FILTER_VALUE_AVG:
278 for (i = 0; i < bpp; i++) {
282 #define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
283 UNROLL_FILTER(OP_AVG);
285 case PNG_FILTER_VALUE_PAETH:
286 for (i = 0; i < bpp; i++) {
290 if (bpp > 2 && size > 4) {
291 /* would write off the end of the array if we let it process
292 * the last pixel with bpp=3 */
293 int w = (bpp & 3) ? size - 3 : size;
296 dsp->add_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
300 ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
305 /* This used to be called "deloco" in FFmpeg
306 * and is actually an inverse reversible colorspace transformation */
307 #define YUV2RGB(NAME, TYPE) \
308 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
311 for (i = 0; i < size; i += 3 + alpha) { \
312 int g = dst [i + 1]; \
318 YUV2RGB(rgb8, uint8_t)
319 YUV2RGB(rgb16, uint16_t)
321 /* process exactly one decompressed row */
322 static void png_handle_row(PNGDecContext *s)
324 uint8_t *ptr, *last_row;
327 if (!s->interlace_type) {
328 ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
330 last_row = s->last_row;
332 last_row = ptr - s->image_linesize;
334 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
335 last_row, s->row_size, s->bpp);
336 /* loco lags by 1 row so that it doesn't interfere with top prediction */
337 if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
338 if (s->bit_depth == 16) {
339 deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
340 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
342 deloco_rgb8(ptr - s->image_linesize, s->row_size,
343 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
347 if (s->y == s->cur_h) {
348 s->pic_state |= PNG_ALLIMAGE;
349 if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
350 if (s->bit_depth == 16) {
351 deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
352 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
354 deloco_rgb8(ptr, s->row_size,
355 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
362 ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
363 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
364 /* if we already read one row, it is time to stop to
365 * wait for the next one */
368 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
369 s->last_row, s->pass_row_size, s->bpp);
370 FFSWAP(uint8_t *, s->last_row, s->tmp_row);
371 FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size);
374 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
375 png_put_interlaced_row(ptr, s->cur_w, s->bits_per_pixel, s->pass,
376 s->color_type, s->last_row);
379 if (s->y == s->cur_h) {
380 memset(s->last_row, 0, s->row_size);
382 if (s->pass == NB_PASSES - 1) {
383 s->pic_state |= PNG_ALLIMAGE;
388 s->pass_row_size = ff_png_pass_row_size(s->pass,
391 s->crow_size = s->pass_row_size + 1;
392 if (s->pass_row_size != 0)
394 /* skip pass if empty row */
403 static int png_decode_idat(PNGDecContext *s, int length)
406 s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
407 s->zstream.next_in = (unsigned char *)s->gb.buffer;
408 bytestream2_skip(&s->gb, length);
410 /* decode one line if possible */
411 while (s->zstream.avail_in > 0) {
412 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
413 if (ret != Z_OK && ret != Z_STREAM_END) {
414 av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
415 return AVERROR_EXTERNAL;
417 if (s->zstream.avail_out == 0) {
418 if (!(s->pic_state & PNG_ALLIMAGE)) {
421 s->zstream.avail_out = s->crow_size;
422 s->zstream.next_out = s->crow_buf;
424 if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
425 av_log(NULL, AV_LOG_WARNING,
426 "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
433 static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
434 const uint8_t *data_end)
441 zstream.zalloc = ff_png_zalloc;
442 zstream.zfree = ff_png_zfree;
443 zstream.opaque = NULL;
444 if (inflateInit(&zstream) != Z_OK)
445 return AVERROR_EXTERNAL;
446 zstream.next_in = (unsigned char *)data;
447 zstream.avail_in = data_end - data;
448 av_bprint_init(bp, 0, -1);
450 while (zstream.avail_in > 0) {
451 av_bprint_get_buffer(bp, 2, &buf, &buf_size);
453 ret = AVERROR(ENOMEM);
456 zstream.next_out = buf;
457 zstream.avail_out = buf_size - 1;
458 ret = inflate(&zstream, Z_PARTIAL_FLUSH);
459 if (ret != Z_OK && ret != Z_STREAM_END) {
460 ret = AVERROR_EXTERNAL;
463 bp->len += zstream.next_out - buf;
464 if (ret == Z_STREAM_END)
467 inflateEnd(&zstream);
468 bp->str[bp->len] = 0;
472 inflateEnd(&zstream);
473 av_bprint_finalize(bp, NULL);
477 static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
482 for (i = 0; i < size_in; i++)
483 extra += in[i] >= 0x80;
484 if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
486 q = out = av_malloc(size_in + extra + 1);
489 for (i = 0; i < size_in; i++) {
491 *(q++) = 0xC0 | (in[i] >> 6);
492 *(q++) = 0x80 | (in[i] & 0x3F);
501 static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed,
505 const uint8_t *data = s->gb.buffer;
506 const uint8_t *data_end = data + length;
507 const uint8_t *keyword = data;
508 const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
509 uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
514 return AVERROR_INVALIDDATA;
515 data = keyword_end + 1;
518 if (data == data_end)
519 return AVERROR_INVALIDDATA;
522 return AVERROR_INVALIDDATA;
523 if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
526 av_bprint_finalize(&bp, (char **)&text);
528 return AVERROR(ENOMEM);
530 text = (uint8_t *)data;
531 text_len = data_end - text;
534 kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
535 txt_utf8 = iso88591_to_utf8(text, text_len);
538 if (!(kw_utf8 && txt_utf8)) {
541 return AVERROR(ENOMEM);
544 av_dict_set(dict, kw_utf8, txt_utf8,
545 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
549 static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s,
553 return AVERROR_INVALIDDATA;
555 if (s->pic_state & PNG_IDAT) {
556 av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");
557 return AVERROR_INVALIDDATA;
560 if (s->hdr_state & PNG_IHDR) {
561 av_log(avctx, AV_LOG_ERROR, "Multiple IHDR\n");
562 return AVERROR_INVALIDDATA;
565 s->width = s->cur_w = bytestream2_get_be32(&s->gb);
566 s->height = s->cur_h = bytestream2_get_be32(&s->gb);
567 if (av_image_check_size(s->width, s->height, 0, avctx)) {
568 s->cur_w = s->cur_h = s->width = s->height = 0;
569 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
570 return AVERROR_INVALIDDATA;
572 s->bit_depth = bytestream2_get_byte(&s->gb);
573 if (s->bit_depth != 1 && s->bit_depth != 2 && s->bit_depth != 4 &&
574 s->bit_depth != 8 && s->bit_depth != 16) {
575 av_log(avctx, AV_LOG_ERROR, "Invalid bit depth\n");
578 s->color_type = bytestream2_get_byte(&s->gb);
579 s->compression_type = bytestream2_get_byte(&s->gb);
580 s->filter_type = bytestream2_get_byte(&s->gb);
581 s->interlace_type = bytestream2_get_byte(&s->gb);
582 bytestream2_skip(&s->gb, 4); /* crc */
583 s->hdr_state |= PNG_IHDR;
584 if (avctx->debug & FF_DEBUG_PICT_INFO)
585 av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
586 "compression_type=%d filter_type=%d interlace_type=%d\n",
587 s->width, s->height, s->bit_depth, s->color_type,
588 s->compression_type, s->filter_type, s->interlace_type);
592 s->cur_w = s->cur_h = s->width = s->height = 0;
594 return AVERROR_INVALIDDATA;
597 static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s)
599 if (s->pic_state & PNG_IDAT) {
600 av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
601 return AVERROR_INVALIDDATA;
603 avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
604 avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
605 if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
606 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
607 bytestream2_skip(&s->gb, 1); /* unit specifier */
608 bytestream2_skip(&s->gb, 4); /* crc */
613 static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s,
614 uint32_t length, AVFrame *p)
617 size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
619 if (!(s->hdr_state & PNG_IHDR)) {
620 av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
621 return AVERROR_INVALIDDATA;
623 if (!(s->pic_state & PNG_IDAT)) {
624 /* init image info */
625 avctx->width = s->width;
626 avctx->height = s->height;
628 s->channels = ff_png_get_nb_channels(s->color_type);
629 s->bits_per_pixel = s->bit_depth * s->channels;
630 s->bpp = (s->bits_per_pixel + 7) >> 3;
631 s->row_size = (s->cur_w * s->bits_per_pixel + 7) >> 3;
633 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
634 s->color_type == PNG_COLOR_TYPE_RGB) {
635 avctx->pix_fmt = AV_PIX_FMT_RGB24;
636 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
637 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
638 avctx->pix_fmt = AV_PIX_FMT_RGBA;
639 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
640 s->color_type == PNG_COLOR_TYPE_GRAY) {
641 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
642 } else if (s->bit_depth == 16 &&
643 s->color_type == PNG_COLOR_TYPE_GRAY) {
644 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
645 } else if (s->bit_depth == 16 &&
646 s->color_type == PNG_COLOR_TYPE_RGB) {
647 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
648 } else if (s->bit_depth == 16 &&
649 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
650 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
651 } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
652 s->color_type == PNG_COLOR_TYPE_PALETTE) {
653 avctx->pix_fmt = AV_PIX_FMT_PAL8;
654 } else if (s->bit_depth == 1 && s->bits_per_pixel == 1 && avctx->codec_id != AV_CODEC_ID_APNG) {
655 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
656 } else if (s->bit_depth == 8 &&
657 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
658 avctx->pix_fmt = AV_PIX_FMT_YA8;
659 } else if (s->bit_depth == 16 &&
660 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
661 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
663 av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
664 "and color type %d\n",
665 s->bit_depth, s->color_type);
666 return AVERROR_INVALIDDATA;
669 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
670 switch (avctx->pix_fmt) {
671 case AV_PIX_FMT_RGB24:
672 avctx->pix_fmt = AV_PIX_FMT_RGBA;
675 case AV_PIX_FMT_RGB48BE:
676 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
679 case AV_PIX_FMT_GRAY8:
680 avctx->pix_fmt = AV_PIX_FMT_YA8;
683 case AV_PIX_FMT_GRAY16BE:
684 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
688 avpriv_request_sample(avctx, "bit depth %d "
689 "and color type %d with TRNS",
690 s->bit_depth, s->color_type);
691 return AVERROR_INVALIDDATA;
694 s->bpp += byte_depth;
697 if ((ret = ff_thread_get_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
699 if (avctx->codec_id == AV_CODEC_ID_APNG && s->last_dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
700 ff_thread_release_buffer(avctx, &s->previous_picture);
701 if ((ret = ff_thread_get_buffer(avctx, &s->previous_picture, AV_GET_BUFFER_FLAG_REF)) < 0)
704 p->pict_type = AV_PICTURE_TYPE_I;
706 p->interlaced_frame = !!s->interlace_type;
708 ff_thread_finish_setup(avctx);
710 /* compute the compressed row size */
711 if (!s->interlace_type) {
712 s->crow_size = s->row_size + 1;
715 s->pass_row_size = ff_png_pass_row_size(s->pass,
718 s->crow_size = s->pass_row_size + 1;
720 ff_dlog(avctx, "row_size=%d crow_size =%d\n",
721 s->row_size, s->crow_size);
722 s->image_buf = p->data[0];
723 s->image_linesize = p->linesize[0];
724 /* copy the palette if needed */
725 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
726 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
727 /* empty row is used if differencing to the first row */
728 av_fast_padded_mallocz(&s->last_row, &s->last_row_size, s->row_size);
730 return AVERROR_INVALIDDATA;
731 if (s->interlace_type ||
732 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
733 av_fast_padded_malloc(&s->tmp_row, &s->tmp_row_size, s->row_size);
735 return AVERROR_INVALIDDATA;
738 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
740 return AVERROR(ENOMEM);
742 /* we want crow_buf+1 to be 16-byte aligned */
743 s->crow_buf = s->buffer + 15;
744 s->zstream.avail_out = s->crow_size;
745 s->zstream.next_out = s->crow_buf;
748 s->pic_state |= PNG_IDAT;
750 /* set image to non-transparent bpp while decompressing */
751 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
752 s->bpp -= byte_depth;
754 ret = png_decode_idat(s, length);
756 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
757 s->bpp += byte_depth;
762 bytestream2_skip(&s->gb, 4); /* crc */
767 static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s,
772 if ((length % 3) != 0 || length > 256 * 3)
773 return AVERROR_INVALIDDATA;
774 /* read the palette */
776 for (i = 0; i < n; i++) {
777 r = bytestream2_get_byte(&s->gb);
778 g = bytestream2_get_byte(&s->gb);
779 b = bytestream2_get_byte(&s->gb);
780 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
783 s->palette[i] = (0xFFU << 24);
784 s->hdr_state |= PNG_PLTE;
785 bytestream2_skip(&s->gb, 4); /* crc */
790 static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,
795 if (!(s->hdr_state & PNG_IHDR)) {
796 av_log(avctx, AV_LOG_ERROR, "trns before IHDR\n");
797 return AVERROR_INVALIDDATA;
800 if (s->pic_state & PNG_IDAT) {
801 av_log(avctx, AV_LOG_ERROR, "trns after IDAT\n");
802 return AVERROR_INVALIDDATA;
805 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
806 if (length > 256 || !(s->hdr_state & PNG_PLTE))
807 return AVERROR_INVALIDDATA;
809 for (i = 0; i < length; i++) {
810 unsigned v = bytestream2_get_byte(&s->gb);
811 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
813 } else if (s->color_type == PNG_COLOR_TYPE_GRAY || s->color_type == PNG_COLOR_TYPE_RGB) {
814 if ((s->color_type == PNG_COLOR_TYPE_GRAY && length != 2) ||
815 (s->color_type == PNG_COLOR_TYPE_RGB && length != 6) ||
817 return AVERROR_INVALIDDATA;
819 for (i = 0; i < length / 2; i++) {
820 /* only use the least significant bits */
821 v = av_mod_uintp2(bytestream2_get_be16(&s->gb), s->bit_depth);
823 if (s->bit_depth > 8)
824 AV_WB16(&s->transparent_color_be[2 * i], v);
826 s->transparent_color_be[i] = v;
829 return AVERROR_INVALIDDATA;
832 bytestream2_skip(&s->gb, 4); /* crc */
838 static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
840 if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
842 uint8_t *pd = p->data[0];
843 for (j = 0; j < s->height; j++) {
845 for (k = 7; k >= 1; k--)
846 if ((s->width&7) >= k)
847 pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
848 for (i--; i >= 0; i--) {
849 pd[8*i + 7]= pd[i] & 1;
850 pd[8*i + 6]= (pd[i]>>1) & 1;
851 pd[8*i + 5]= (pd[i]>>2) & 1;
852 pd[8*i + 4]= (pd[i]>>3) & 1;
853 pd[8*i + 3]= (pd[i]>>4) & 1;
854 pd[8*i + 2]= (pd[i]>>5) & 1;
855 pd[8*i + 1]= (pd[i]>>6) & 1;
856 pd[8*i + 0]= pd[i]>>7;
858 pd += s->image_linesize;
860 } else if (s->bits_per_pixel == 2) {
862 uint8_t *pd = p->data[0];
863 for (j = 0; j < s->height; j++) {
865 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
866 if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
867 if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
868 if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
869 for (i--; i >= 0; i--) {
870 pd[4*i + 3]= pd[i] & 3;
871 pd[4*i + 2]= (pd[i]>>2) & 3;
872 pd[4*i + 1]= (pd[i]>>4) & 3;
873 pd[4*i + 0]= pd[i]>>6;
876 if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
877 if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
878 if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
879 for (i--; i >= 0; i--) {
880 pd[4*i + 3]= ( pd[i] & 3)*0x55;
881 pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
882 pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
883 pd[4*i + 0]= ( pd[i]>>6 )*0x55;
886 pd += s->image_linesize;
888 } else if (s->bits_per_pixel == 4) {
890 uint8_t *pd = p->data[0];
891 for (j = 0; j < s->height; j++) {
893 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
894 if (s->width&1) pd[2*i+0]= pd[i]>>4;
895 for (i--; i >= 0; i--) {
896 pd[2*i + 1] = pd[i] & 15;
897 pd[2*i + 0] = pd[i] >> 4;
900 if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
901 for (i--; i >= 0; i--) {
902 pd[2*i + 1] = (pd[i] & 15) * 0x11;
903 pd[2*i + 0] = (pd[i] >> 4) * 0x11;
906 pd += s->image_linesize;
911 static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s,
914 uint32_t sequence_number;
915 int cur_w, cur_h, x_offset, y_offset, dispose_op, blend_op;
918 return AVERROR_INVALIDDATA;
920 if (!(s->hdr_state & PNG_IHDR)) {
921 av_log(avctx, AV_LOG_ERROR, "fctl before IHDR\n");
922 return AVERROR_INVALIDDATA;
925 s->last_w = s->cur_w;
926 s->last_h = s->cur_h;
927 s->last_x_offset = s->x_offset;
928 s->last_y_offset = s->y_offset;
929 s->last_dispose_op = s->dispose_op;
931 sequence_number = bytestream2_get_be32(&s->gb);
932 cur_w = bytestream2_get_be32(&s->gb);
933 cur_h = bytestream2_get_be32(&s->gb);
934 x_offset = bytestream2_get_be32(&s->gb);
935 y_offset = bytestream2_get_be32(&s->gb);
936 bytestream2_skip(&s->gb, 4); /* delay_num (2), delay_den (2) */
937 dispose_op = bytestream2_get_byte(&s->gb);
938 blend_op = bytestream2_get_byte(&s->gb);
939 bytestream2_skip(&s->gb, 4); /* crc */
941 if (sequence_number == 0 &&
942 (cur_w != s->width ||
943 cur_h != s->height ||
946 cur_w <= 0 || cur_h <= 0 ||
947 x_offset < 0 || y_offset < 0 ||
948 cur_w > s->width - x_offset|| cur_h > s->height - y_offset)
949 return AVERROR_INVALIDDATA;
951 if (blend_op != APNG_BLEND_OP_OVER && blend_op != APNG_BLEND_OP_SOURCE) {
952 av_log(avctx, AV_LOG_ERROR, "Invalid blend_op %d\n", blend_op);
953 return AVERROR_INVALIDDATA;
956 if ((sequence_number == 0 || !s->previous_picture.f->data[0]) &&
957 dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
958 // No previous frame to revert to for the first frame
959 // Spec says to just treat it as a APNG_DISPOSE_OP_BACKGROUND
960 dispose_op = APNG_DISPOSE_OP_BACKGROUND;
963 if (blend_op == APNG_BLEND_OP_OVER && !s->has_trns && (
964 avctx->pix_fmt == AV_PIX_FMT_RGB24 ||
965 avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
966 avctx->pix_fmt == AV_PIX_FMT_PAL8 ||
967 avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
968 avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
969 avctx->pix_fmt == AV_PIX_FMT_MONOBLACK
971 // APNG_BLEND_OP_OVER is the same as APNG_BLEND_OP_SOURCE when there is no alpha channel
972 blend_op = APNG_BLEND_OP_SOURCE;
977 s->x_offset = x_offset;
978 s->y_offset = y_offset;
979 s->dispose_op = dispose_op;
980 s->blend_op = blend_op;
985 static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
988 uint8_t *pd = p->data[0];
989 uint8_t *pd_last = s->last_picture.f->data[0];
990 int ls = FFMIN(av_image_get_linesize(p->format, s->width, 0), s->width * s->bpp);
992 ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
993 for (j = 0; j < s->height; j++) {
994 for (i = 0; i < ls; i++)
996 pd += s->image_linesize;
997 pd_last += s->image_linesize;
1001 // divide by 255 and round to nearest
1002 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
1003 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
1005 static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s,
1011 if (s->blend_op == APNG_BLEND_OP_OVER &&
1012 avctx->pix_fmt != AV_PIX_FMT_RGBA &&
1013 avctx->pix_fmt != AV_PIX_FMT_GRAY8A &&
1014 avctx->pix_fmt != AV_PIX_FMT_PAL8) {
1015 avpriv_request_sample(avctx, "Blending with pixel format %s",
1016 av_get_pix_fmt_name(avctx->pix_fmt));
1017 return AVERROR_PATCHWELCOME;
1020 buffer = av_malloc_array(s->image_linesize, s->height);
1022 return AVERROR(ENOMEM);
1025 // Do the disposal operation specified by the last frame on the frame
1026 if (s->last_dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
1027 ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
1028 memcpy(buffer, s->last_picture.f->data[0], s->image_linesize * s->height);
1030 if (s->last_dispose_op == APNG_DISPOSE_OP_BACKGROUND)
1031 for (y = s->last_y_offset; y < s->last_y_offset + s->last_h; ++y)
1032 memset(buffer + s->image_linesize * y + s->bpp * s->last_x_offset, 0, s->bpp * s->last_w);
1034 memcpy(s->previous_picture.f->data[0], buffer, s->image_linesize * s->height);
1035 ff_thread_report_progress(&s->previous_picture, INT_MAX, 0);
1037 ff_thread_await_progress(&s->previous_picture, INT_MAX, 0);
1038 memcpy(buffer, s->previous_picture.f->data[0], s->image_linesize * s->height);
1042 if (s->blend_op == APNG_BLEND_OP_SOURCE) {
1043 for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
1044 size_t row_start = s->image_linesize * y + s->bpp * s->x_offset;
1045 memcpy(buffer + row_start, p->data[0] + row_start, s->bpp * s->cur_w);
1047 } else { // APNG_BLEND_OP_OVER
1048 for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
1049 uint8_t *foreground = p->data[0] + s->image_linesize * y + s->bpp * s->x_offset;
1050 uint8_t *background = buffer + s->image_linesize * y + s->bpp * s->x_offset;
1051 for (x = s->x_offset; x < s->x_offset + s->cur_w; ++x, foreground += s->bpp, background += s->bpp) {
1053 uint8_t foreground_alpha, background_alpha, output_alpha;
1056 // Since we might be blending alpha onto alpha, we use the following equations:
1057 // output_alpha = foreground_alpha + (1 - foreground_alpha) * background_alpha
1058 // output = (foreground_alpha * foreground + (1 - foreground_alpha) * background_alpha * background) / output_alpha
1060 switch (avctx->pix_fmt) {
1061 case AV_PIX_FMT_RGBA:
1062 foreground_alpha = foreground[3];
1063 background_alpha = background[3];
1066 case AV_PIX_FMT_GRAY8A:
1067 foreground_alpha = foreground[1];
1068 background_alpha = background[1];
1071 case AV_PIX_FMT_PAL8:
1072 foreground_alpha = s->palette[foreground[0]] >> 24;
1073 background_alpha = s->palette[background[0]] >> 24;
1077 if (foreground_alpha == 0)
1080 if (foreground_alpha == 255) {
1081 memcpy(background, foreground, s->bpp);
1085 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1086 // TODO: Alpha blending with PAL8 will likely need the entire image converted over to RGBA first
1087 avpriv_request_sample(avctx, "Alpha blending palette samples");
1088 background[0] = foreground[0];
1092 output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);
1094 av_assert0(s->bpp <= 10);
1096 for (b = 0; b < s->bpp - 1; ++b) {
1097 if (output_alpha == 0) {
1099 } else if (background_alpha == 255) {
1100 output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);
1102 output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);
1105 output[b] = output_alpha;
1106 memcpy(background, output, s->bpp);
1111 // Copy blended buffer into the frame and free
1112 memcpy(p->data[0], buffer, s->image_linesize * s->height);
1118 static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
1119 AVFrame *p, AVPacket *avpkt)
1121 AVDictionary **metadatap = NULL;
1122 uint32_t tag, length;
1123 int decode_next_dat = 0;
1127 length = bytestream2_get_bytes_left(&s->gb);
1130 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1131 avctx->skip_frame == AVDISCARD_ALL) {
1135 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && length == 0) {
1136 if (!(s->pic_state & PNG_IDAT))
1141 av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length);
1142 if ( s->pic_state & PNG_ALLIMAGE
1143 && avctx->strict_std_compliance <= FF_COMPLIANCE_NORMAL)
1145 ret = AVERROR_INVALIDDATA;
1149 length = bytestream2_get_be32(&s->gb);
1150 if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
1151 av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
1152 ret = AVERROR_INVALIDDATA;
1155 tag = bytestream2_get_le32(&s->gb);
1156 if (avctx->debug & FF_DEBUG_STARTCODE)
1157 av_log(avctx, AV_LOG_DEBUG, "png: tag=%s length=%u\n",
1158 av_fourcc2str(tag), length);
1160 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1161 avctx->skip_frame == AVDISCARD_ALL) {
1163 case MKTAG('I', 'H', 'D', 'R'):
1164 case MKTAG('p', 'H', 'Y', 's'):
1165 case MKTAG('t', 'E', 'X', 't'):
1166 case MKTAG('I', 'D', 'A', 'T'):
1167 case MKTAG('t', 'R', 'N', 'S'):
1174 metadatap = avpriv_frame_get_metadatap(p);
1176 case MKTAG('I', 'H', 'D', 'R'):
1177 if ((ret = decode_ihdr_chunk(avctx, s, length)) < 0)
1180 case MKTAG('p', 'H', 'Y', 's'):
1181 if ((ret = decode_phys_chunk(avctx, s)) < 0)
1184 case MKTAG('f', 'c', 'T', 'L'):
1185 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1187 if ((ret = decode_fctl_chunk(avctx, s, length)) < 0)
1189 decode_next_dat = 1;
1191 case MKTAG('f', 'd', 'A', 'T'):
1192 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1194 if (!decode_next_dat) {
1195 ret = AVERROR_INVALIDDATA;
1198 bytestream2_get_be32(&s->gb);
1201 case MKTAG('I', 'D', 'A', 'T'):
1202 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
1204 if ((ret = decode_idat_chunk(avctx, s, length, p)) < 0)
1207 case MKTAG('P', 'L', 'T', 'E'):
1208 if (decode_plte_chunk(avctx, s, length) < 0)
1211 case MKTAG('t', 'R', 'N', 'S'):
1212 if (decode_trns_chunk(avctx, s, length) < 0)
1215 case MKTAG('t', 'E', 'X', 't'):
1216 if (decode_text_chunk(s, length, 0, metadatap) < 0)
1217 av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
1218 bytestream2_skip(&s->gb, length + 4);
1220 case MKTAG('z', 'T', 'X', 't'):
1221 if (decode_text_chunk(s, length, 1, metadatap) < 0)
1222 av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
1223 bytestream2_skip(&s->gb, length + 4);
1225 case MKTAG('s', 'T', 'E', 'R'): {
1226 int mode = bytestream2_get_byte(&s->gb);
1227 AVStereo3D *stereo3d = av_stereo3d_create_side_data(p);
1231 if (mode == 0 || mode == 1) {
1232 stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
1233 stereo3d->flags = mode ? 0 : AV_STEREO3D_FLAG_INVERT;
1235 av_log(avctx, AV_LOG_WARNING,
1236 "Unknown value in sTER chunk (%d)\n", mode);
1238 bytestream2_skip(&s->gb, 4); /* crc */
1241 case MKTAG('I', 'E', 'N', 'D'):
1242 if (!(s->pic_state & PNG_ALLIMAGE))
1243 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
1244 if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT))) {
1245 ret = AVERROR_INVALIDDATA;
1248 bytestream2_skip(&s->gb, 4); /* crc */
1253 bytestream2_skip(&s->gb, length + 4);
1259 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1260 avctx->skip_frame == AVDISCARD_ALL) {
1264 if (s->bits_per_pixel <= 4)
1265 handle_small_bpp(s, p);
1267 /* apply transparency if needed */
1268 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
1269 size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
1270 size_t raw_bpp = s->bpp - byte_depth;
1273 av_assert0(s->bit_depth > 1);
1275 for (y = 0; y < s->height; ++y) {
1276 uint8_t *row = &s->image_buf[s->image_linesize * y];
1278 /* since we're updating in-place, we have to go from right to left */
1279 for (x = s->width; x > 0; --x) {
1280 uint8_t *pixel = &row[s->bpp * (x - 1)];
1281 memmove(pixel, &row[raw_bpp * (x - 1)], raw_bpp);
1283 if (!memcmp(pixel, s->transparent_color_be, raw_bpp)) {
1284 memset(&pixel[raw_bpp], 0, byte_depth);
1286 memset(&pixel[raw_bpp], 0xff, byte_depth);
1292 /* handle P-frames only if a predecessor frame is available */
1293 if (s->last_picture.f->data[0]) {
1294 if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
1295 && s->last_picture.f->width == p->width
1296 && s->last_picture.f->height== p->height
1297 && s->last_picture.f->format== p->format
1299 if (CONFIG_PNG_DECODER && avctx->codec_id != AV_CODEC_ID_APNG)
1300 handle_p_frame_png(s, p);
1301 else if (CONFIG_APNG_DECODER &&
1302 avctx->codec_id == AV_CODEC_ID_APNG &&
1303 (ret = handle_p_frame_apng(avctx, s, p)) < 0)
1307 ff_thread_report_progress(&s->picture, INT_MAX, 0);
1308 ff_thread_report_progress(&s->previous_picture, INT_MAX, 0);
1313 ff_thread_report_progress(&s->picture, INT_MAX, 0);
1314 ff_thread_report_progress(&s->previous_picture, INT_MAX, 0);
1318 #if CONFIG_PNG_DECODER
1319 static int decode_frame_png(AVCodecContext *avctx,
1320 void *data, int *got_frame,
1323 PNGDecContext *const s = avctx->priv_data;
1324 const uint8_t *buf = avpkt->data;
1325 int buf_size = avpkt->size;
1330 ff_thread_release_buffer(avctx, &s->last_picture);
1331 FFSWAP(ThreadFrame, s->picture, s->last_picture);
1334 bytestream2_init(&s->gb, buf, buf_size);
1336 /* check signature */
1337 sig = bytestream2_get_be64(&s->gb);
1338 if (sig != PNGSIG &&
1340 av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature 0x%08"PRIX64".\n", sig);
1341 return AVERROR_INVALIDDATA;
1344 s->y = s->has_trns = 0;
1349 s->zstream.zalloc = ff_png_zalloc;
1350 s->zstream.zfree = ff_png_zfree;
1351 s->zstream.opaque = NULL;
1352 ret = inflateInit(&s->zstream);
1354 av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1355 return AVERROR_EXTERNAL;
1358 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1361 if (avctx->skip_frame == AVDISCARD_ALL) {
1363 ret = bytestream2_tell(&s->gb);
1367 if ((ret = av_frame_ref(data, s->picture.f)) < 0)
1372 ret = bytestream2_tell(&s->gb);
1374 inflateEnd(&s->zstream);
1380 #if CONFIG_APNG_DECODER
1381 static int decode_frame_apng(AVCodecContext *avctx,
1382 void *data, int *got_frame,
1385 PNGDecContext *const s = avctx->priv_data;
1389 ff_thread_release_buffer(avctx, &s->last_picture);
1390 FFSWAP(ThreadFrame, s->picture, s->last_picture);
1393 if (!(s->hdr_state & PNG_IHDR)) {
1394 if (!avctx->extradata_size)
1395 return AVERROR_INVALIDDATA;
1397 /* only init fields, there is no zlib use in extradata */
1398 s->zstream.zalloc = ff_png_zalloc;
1399 s->zstream.zfree = ff_png_zfree;
1401 bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
1402 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1406 /* reset state for a new frame */
1407 if ((ret = inflateInit(&s->zstream)) != Z_OK) {
1408 av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1409 ret = AVERROR_EXTERNAL;
1414 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1415 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1418 if (!(s->pic_state & PNG_ALLIMAGE))
1419 av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
1420 if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT))) {
1421 ret = AVERROR_INVALIDDATA;
1424 if ((ret = av_frame_ref(data, s->picture.f)) < 0)
1428 ret = bytestream2_tell(&s->gb);
1431 inflateEnd(&s->zstream);
1437 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1439 PNGDecContext *psrc = src->priv_data;
1440 PNGDecContext *pdst = dst->priv_data;
1446 ff_thread_release_buffer(dst, &pdst->picture);
1447 if (psrc->picture.f->data[0] &&
1448 (ret = ff_thread_ref_frame(&pdst->picture, &psrc->picture)) < 0)
1450 if (CONFIG_APNG_DECODER && dst->codec_id == AV_CODEC_ID_APNG) {
1451 pdst->width = psrc->width;
1452 pdst->height = psrc->height;
1453 pdst->bit_depth = psrc->bit_depth;
1454 pdst->color_type = psrc->color_type;
1455 pdst->compression_type = psrc->compression_type;
1456 pdst->interlace_type = psrc->interlace_type;
1457 pdst->filter_type = psrc->filter_type;
1458 pdst->cur_w = psrc->cur_w;
1459 pdst->cur_h = psrc->cur_h;
1460 pdst->x_offset = psrc->x_offset;
1461 pdst->y_offset = psrc->y_offset;
1462 pdst->has_trns = psrc->has_trns;
1463 memcpy(pdst->transparent_color_be, psrc->transparent_color_be, sizeof(pdst->transparent_color_be));
1465 pdst->dispose_op = psrc->dispose_op;
1467 memcpy(pdst->palette, psrc->palette, sizeof(pdst->palette));
1469 pdst->hdr_state |= psrc->hdr_state;
1471 ff_thread_release_buffer(dst, &pdst->last_picture);
1472 if (psrc->last_picture.f->data[0] &&
1473 (ret = ff_thread_ref_frame(&pdst->last_picture, &psrc->last_picture)) < 0)
1476 ff_thread_release_buffer(dst, &pdst->previous_picture);
1477 if (psrc->previous_picture.f->data[0] &&
1478 (ret = ff_thread_ref_frame(&pdst->previous_picture, &psrc->previous_picture)) < 0)
1486 static av_cold int png_dec_init(AVCodecContext *avctx)
1488 PNGDecContext *s = avctx->priv_data;
1490 avctx->color_range = AVCOL_RANGE_JPEG;
1493 s->previous_picture.f = av_frame_alloc();
1494 s->last_picture.f = av_frame_alloc();
1495 s->picture.f = av_frame_alloc();
1496 if (!s->previous_picture.f || !s->last_picture.f || !s->picture.f) {
1497 av_frame_free(&s->previous_picture.f);
1498 av_frame_free(&s->last_picture.f);
1499 av_frame_free(&s->picture.f);
1500 return AVERROR(ENOMEM);
1503 if (!avctx->internal->is_copy) {
1504 avctx->internal->allocate_progress = 1;
1505 ff_pngdsp_init(&s->dsp);
1511 static av_cold int png_dec_end(AVCodecContext *avctx)
1513 PNGDecContext *s = avctx->priv_data;
1515 ff_thread_release_buffer(avctx, &s->previous_picture);
1516 av_frame_free(&s->previous_picture.f);
1517 ff_thread_release_buffer(avctx, &s->last_picture);
1518 av_frame_free(&s->last_picture.f);
1519 ff_thread_release_buffer(avctx, &s->picture);
1520 av_frame_free(&s->picture.f);
1521 av_freep(&s->buffer);
1523 av_freep(&s->last_row);
1524 s->last_row_size = 0;
1525 av_freep(&s->tmp_row);
1526 s->tmp_row_size = 0;
1531 #if CONFIG_APNG_DECODER
1532 AVCodec ff_apng_decoder = {
1534 .long_name = NULL_IF_CONFIG_SMALL("APNG (Animated Portable Network Graphics) image"),
1535 .type = AVMEDIA_TYPE_VIDEO,
1536 .id = AV_CODEC_ID_APNG,
1537 .priv_data_size = sizeof(PNGDecContext),
1538 .init = png_dec_init,
1539 .close = png_dec_end,
1540 .decode = decode_frame_apng,
1541 .init_thread_copy = ONLY_IF_THREADS_ENABLED(png_dec_init),
1542 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1543 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
1544 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1548 #if CONFIG_PNG_DECODER
1549 AVCodec ff_png_decoder = {
1551 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
1552 .type = AVMEDIA_TYPE_VIDEO,
1553 .id = AV_CODEC_ID_PNG,
1554 .priv_data_size = sizeof(PNGDecContext),
1555 .init = png_dec_init,
1556 .close = png_dec_end,
1557 .decode = decode_frame_png,
1558 .init_thread_copy = ONLY_IF_THREADS_ENABLED(png_dec_init),
1559 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1560 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
1561 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_INIT_THREADSAFE,