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"
29 #include "bytestream.h"
38 typedef struct PNGDecContext {
40 AVCodecContext *avctx;
43 ThreadFrame previous_picture;
44 ThreadFrame last_picture;
51 int x_offset, y_offset;
52 int last_x_offset, last_y_offset;
53 uint8_t dispose_op, blend_op;
54 uint8_t last_dispose_op;
64 uint8_t transparent_color_be[6];
68 uint32_t palette[256];
71 unsigned int last_row_size;
73 unsigned int tmp_row_size;
77 int crow_size; /* compressed row size (include filter type) */
78 int row_size; /* decompressed row size */
79 int pass_row_size; /* decompress row size of the current pass */
84 /* Mask to determine which pixels are valid in a pass */
85 static const uint8_t png_pass_mask[NB_PASSES] = {
86 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
89 /* Mask to determine which y pixels can be written in a pass */
90 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
91 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
94 /* Mask to determine which pixels to overwrite while displaying */
95 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
96 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
99 /* NOTE: we try to construct a good looking image at each pass. width
100 * is the original image width. We also do pixel format conversion at
102 static void png_put_interlaced_row(uint8_t *dst, int width,
103 int bits_per_pixel, int pass,
104 int color_type, const uint8_t *src)
106 int x, mask, dsp_mask, j, src_x, b, bpp;
110 mask = png_pass_mask[pass];
111 dsp_mask = png_pass_dsp_mask[pass];
113 switch (bits_per_pixel) {
116 for (x = 0; x < width; x++) {
118 if ((dsp_mask << j) & 0x80) {
119 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
120 dst[x >> 3] &= 0xFF7F>>j;
121 dst[x >> 3] |= b << (7 - j);
123 if ((mask << j) & 0x80)
129 for (x = 0; x < width; x++) {
130 int j2 = 2 * (x & 3);
132 if ((dsp_mask << j) & 0x80) {
133 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
134 dst[x >> 2] &= 0xFF3F>>j2;
135 dst[x >> 2] |= b << (6 - j2);
137 if ((mask << j) & 0x80)
143 for (x = 0; x < width; x++) {
146 if ((dsp_mask << j) & 0x80) {
147 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
148 dst[x >> 1] &= 0xFF0F>>j2;
149 dst[x >> 1] |= b << (4 - j2);
151 if ((mask << j) & 0x80)
156 bpp = bits_per_pixel >> 3;
159 for (x = 0; x < width; x++) {
161 if ((dsp_mask << j) & 0x80) {
165 if ((mask << j) & 0x80)
172 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
176 for (i = 0; i < w; i++) {
177 int a, b, c, p, pa, pb, pc;
190 if (pa <= pb && pa <= pc)
200 #define UNROLL1(bpp, op) \
209 for (; i <= size - bpp; i += bpp) { \
210 dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
213 dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
216 dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
219 dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
223 #define UNROLL_FILTER(op) \
226 } else if (bpp == 2) { \
228 } else if (bpp == 3) { \
230 } else if (bpp == 4) { \
233 for (; i < size; i++) { \
234 dst[i] = op(dst[i - bpp], src[i], last[i]); \
237 /* NOTE: 'dst' can be equal to 'last' */
238 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
239 uint8_t *src, uint8_t *last, int size, int bpp)
241 int i, p, r, g, b, a;
243 switch (filter_type) {
244 case PNG_FILTER_VALUE_NONE:
245 memcpy(dst, src, size);
247 case PNG_FILTER_VALUE_SUB:
248 for (i = 0; i < bpp; i++)
252 for (; i < size; i += bpp) {
253 unsigned s = *(int *)(src + i);
254 p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
255 *(int *)(dst + i) = p;
258 #define OP_SUB(x, s, l) ((x) + (s))
259 UNROLL_FILTER(OP_SUB);
262 case PNG_FILTER_VALUE_UP:
263 dsp->add_bytes_l2(dst, src, last, size);
265 case PNG_FILTER_VALUE_AVG:
266 for (i = 0; i < bpp; i++) {
270 #define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
271 UNROLL_FILTER(OP_AVG);
273 case PNG_FILTER_VALUE_PAETH:
274 for (i = 0; i < bpp; i++) {
278 if (bpp > 2 && size > 4) {
279 /* would write off the end of the array if we let it process
280 * the last pixel with bpp=3 */
281 int w = (bpp & 3) ? size - 3 : size;
284 dsp->add_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
288 ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
293 /* This used to be called "deloco" in FFmpeg
294 * and is actually an inverse reversible colorspace transformation */
295 #define YUV2RGB(NAME, TYPE) \
296 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
299 for (i = 0; i < size; i += 3 + alpha) { \
300 int g = dst [i + 1]; \
306 YUV2RGB(rgb8, uint8_t)
307 YUV2RGB(rgb16, uint16_t)
309 /* process exactly one decompressed row */
310 static void png_handle_row(PNGDecContext *s)
312 uint8_t *ptr, *last_row;
315 if (!s->interlace_type) {
316 ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
318 last_row = s->last_row;
320 last_row = ptr - s->image_linesize;
322 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
323 last_row, s->row_size, s->bpp);
324 /* loco lags by 1 row so that it doesn't interfere with top prediction */
325 if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
326 if (s->bit_depth == 16) {
327 deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
328 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
330 deloco_rgb8(ptr - s->image_linesize, s->row_size,
331 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
335 if (s->y == s->cur_h) {
336 s->state |= PNG_ALLIMAGE;
337 if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
338 if (s->bit_depth == 16) {
339 deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
340 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
342 deloco_rgb8(ptr, s->row_size,
343 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
350 ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
351 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
352 /* if we already read one row, it is time to stop to
353 * wait for the next one */
356 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
357 s->last_row, s->pass_row_size, s->bpp);
358 FFSWAP(uint8_t *, s->last_row, s->tmp_row);
359 FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size);
362 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
363 png_put_interlaced_row(ptr, s->cur_w, s->bits_per_pixel, s->pass,
364 s->color_type, s->last_row);
367 if (s->y == s->cur_h) {
368 memset(s->last_row, 0, s->row_size);
370 if (s->pass == NB_PASSES - 1) {
371 s->state |= PNG_ALLIMAGE;
376 s->pass_row_size = ff_png_pass_row_size(s->pass,
379 s->crow_size = s->pass_row_size + 1;
380 if (s->pass_row_size != 0)
382 /* skip pass if empty row */
391 static int png_decode_idat(PNGDecContext *s, int length)
394 s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
395 s->zstream.next_in = (unsigned char *)s->gb.buffer;
396 bytestream2_skip(&s->gb, length);
398 /* decode one line if possible */
399 while (s->zstream.avail_in > 0) {
400 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
401 if (ret != Z_OK && ret != Z_STREAM_END) {
402 av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
403 return AVERROR_EXTERNAL;
405 if (s->zstream.avail_out == 0) {
406 if (!(s->state & PNG_ALLIMAGE)) {
409 s->zstream.avail_out = s->crow_size;
410 s->zstream.next_out = s->crow_buf;
412 if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
413 av_log(NULL, AV_LOG_WARNING,
414 "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
421 static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
422 const uint8_t *data_end)
429 zstream.zalloc = ff_png_zalloc;
430 zstream.zfree = ff_png_zfree;
431 zstream.opaque = NULL;
432 if (inflateInit(&zstream) != Z_OK)
433 return AVERROR_EXTERNAL;
434 zstream.next_in = (unsigned char *)data;
435 zstream.avail_in = data_end - data;
436 av_bprint_init(bp, 0, -1);
438 while (zstream.avail_in > 0) {
439 av_bprint_get_buffer(bp, 1, &buf, &buf_size);
441 ret = AVERROR(ENOMEM);
444 zstream.next_out = buf;
445 zstream.avail_out = buf_size;
446 ret = inflate(&zstream, Z_PARTIAL_FLUSH);
447 if (ret != Z_OK && ret != Z_STREAM_END) {
448 ret = AVERROR_EXTERNAL;
451 bp->len += zstream.next_out - buf;
452 if (ret == Z_STREAM_END)
455 inflateEnd(&zstream);
456 bp->str[bp->len] = 0;
460 inflateEnd(&zstream);
461 av_bprint_finalize(bp, NULL);
465 static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
470 for (i = 0; i < size_in; i++)
471 extra += in[i] >= 0x80;
472 if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
474 q = out = av_malloc(size_in + extra + 1);
477 for (i = 0; i < size_in; i++) {
479 *(q++) = 0xC0 | (in[i] >> 6);
480 *(q++) = 0x80 | (in[i] & 0x3F);
489 static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed,
493 const uint8_t *data = s->gb.buffer;
494 const uint8_t *data_end = data + length;
495 const uint8_t *keyword = data;
496 const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
497 uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
502 return AVERROR_INVALIDDATA;
503 data = keyword_end + 1;
506 if (data == data_end)
507 return AVERROR_INVALIDDATA;
510 return AVERROR_INVALIDDATA;
511 if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
514 av_bprint_finalize(&bp, (char **)&text);
516 return AVERROR(ENOMEM);
518 text = (uint8_t *)data;
519 text_len = data_end - text;
522 kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
523 txt_utf8 = iso88591_to_utf8(text, text_len);
526 if (!(kw_utf8 && txt_utf8)) {
529 return AVERROR(ENOMEM);
532 av_dict_set(dict, kw_utf8, txt_utf8,
533 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
537 static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s,
541 return AVERROR_INVALIDDATA;
543 if (s->state & PNG_IDAT) {
544 av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");
545 return AVERROR_INVALIDDATA;
548 if (s->state & PNG_IHDR) {
549 av_log(avctx, AV_LOG_ERROR, "Multiple IHDR\n");
550 return AVERROR_INVALIDDATA;
553 s->width = s->cur_w = bytestream2_get_be32(&s->gb);
554 s->height = s->cur_h = bytestream2_get_be32(&s->gb);
555 if (av_image_check_size(s->width, s->height, 0, avctx)) {
556 s->cur_w = s->cur_h = s->width = s->height = 0;
557 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
558 return AVERROR_INVALIDDATA;
560 s->bit_depth = bytestream2_get_byte(&s->gb);
561 s->color_type = bytestream2_get_byte(&s->gb);
562 s->compression_type = bytestream2_get_byte(&s->gb);
563 s->filter_type = bytestream2_get_byte(&s->gb);
564 s->interlace_type = bytestream2_get_byte(&s->gb);
565 bytestream2_skip(&s->gb, 4); /* crc */
566 s->state |= PNG_IHDR;
567 if (avctx->debug & FF_DEBUG_PICT_INFO)
568 av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
569 "compression_type=%d filter_type=%d interlace_type=%d\n",
570 s->width, s->height, s->bit_depth, s->color_type,
571 s->compression_type, s->filter_type, s->interlace_type);
576 static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s)
578 if (s->state & PNG_IDAT) {
579 av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
580 return AVERROR_INVALIDDATA;
582 avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
583 avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
584 if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
585 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
586 bytestream2_skip(&s->gb, 1); /* unit specifier */
587 bytestream2_skip(&s->gb, 4); /* crc */
592 static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s,
593 uint32_t length, AVFrame *p)
596 size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
598 if (!(s->state & PNG_IHDR)) {
599 av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
600 return AVERROR_INVALIDDATA;
602 if (!(s->state & PNG_IDAT)) {
603 /* init image info */
604 avctx->width = s->width;
605 avctx->height = s->height;
607 s->channels = ff_png_get_nb_channels(s->color_type);
608 s->bits_per_pixel = s->bit_depth * s->channels;
609 s->bpp = (s->bits_per_pixel + 7) >> 3;
610 s->row_size = (s->cur_w * s->bits_per_pixel + 7) >> 3;
612 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
613 s->color_type == PNG_COLOR_TYPE_RGB) {
614 avctx->pix_fmt = AV_PIX_FMT_RGB24;
615 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
616 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
617 avctx->pix_fmt = AV_PIX_FMT_RGBA;
618 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
619 s->color_type == PNG_COLOR_TYPE_GRAY) {
620 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
621 } else if (s->bit_depth == 16 &&
622 s->color_type == PNG_COLOR_TYPE_GRAY) {
623 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
624 } else if (s->bit_depth == 16 &&
625 s->color_type == PNG_COLOR_TYPE_RGB) {
626 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
627 } else if (s->bit_depth == 16 &&
628 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
629 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
630 } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
631 s->color_type == PNG_COLOR_TYPE_PALETTE) {
632 avctx->pix_fmt = AV_PIX_FMT_PAL8;
633 } else if (s->bit_depth == 1 && s->bits_per_pixel == 1 && avctx->codec_id != AV_CODEC_ID_APNG) {
634 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
635 } else if (s->bit_depth == 8 &&
636 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
637 avctx->pix_fmt = AV_PIX_FMT_YA8;
638 } else if (s->bit_depth == 16 &&
639 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
640 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
642 av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
643 "and color type %d\n",
644 s->bit_depth, s->color_type);
645 return AVERROR_INVALIDDATA;
648 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
649 switch (avctx->pix_fmt) {
650 case AV_PIX_FMT_RGB24:
651 avctx->pix_fmt = AV_PIX_FMT_RGBA;
654 case AV_PIX_FMT_RGB48BE:
655 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
658 case AV_PIX_FMT_GRAY8:
659 avctx->pix_fmt = AV_PIX_FMT_YA8;
662 case AV_PIX_FMT_GRAY16BE:
663 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
670 s->bpp += byte_depth;
673 if ((ret = ff_thread_get_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
675 if (avctx->codec_id == AV_CODEC_ID_APNG && s->last_dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
676 ff_thread_release_buffer(avctx, &s->previous_picture);
677 if ((ret = ff_thread_get_buffer(avctx, &s->previous_picture, AV_GET_BUFFER_FLAG_REF)) < 0)
680 ff_thread_finish_setup(avctx);
682 p->pict_type = AV_PICTURE_TYPE_I;
684 p->interlaced_frame = !!s->interlace_type;
686 /* compute the compressed row size */
687 if (!s->interlace_type) {
688 s->crow_size = s->row_size + 1;
691 s->pass_row_size = ff_png_pass_row_size(s->pass,
694 s->crow_size = s->pass_row_size + 1;
696 ff_dlog(avctx, "row_size=%d crow_size =%d\n",
697 s->row_size, s->crow_size);
698 s->image_buf = p->data[0];
699 s->image_linesize = p->linesize[0];
700 /* copy the palette if needed */
701 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
702 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
703 /* empty row is used if differencing to the first row */
704 av_fast_padded_mallocz(&s->last_row, &s->last_row_size, s->row_size);
706 return AVERROR_INVALIDDATA;
707 if (s->interlace_type ||
708 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
709 av_fast_padded_malloc(&s->tmp_row, &s->tmp_row_size, s->row_size);
711 return AVERROR_INVALIDDATA;
714 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
716 return AVERROR(ENOMEM);
718 /* we want crow_buf+1 to be 16-byte aligned */
719 s->crow_buf = s->buffer + 15;
720 s->zstream.avail_out = s->crow_size;
721 s->zstream.next_out = s->crow_buf;
724 s->state |= PNG_IDAT;
726 /* set image to non-transparent bpp while decompressing */
727 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
728 s->bpp -= byte_depth;
730 ret = png_decode_idat(s, length);
732 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
733 s->bpp += byte_depth;
738 bytestream2_skip(&s->gb, 4); /* crc */
743 static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s,
748 if ((length % 3) != 0 || length > 256 * 3)
749 return AVERROR_INVALIDDATA;
750 /* read the palette */
752 for (i = 0; i < n; i++) {
753 r = bytestream2_get_byte(&s->gb);
754 g = bytestream2_get_byte(&s->gb);
755 b = bytestream2_get_byte(&s->gb);
756 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
759 s->palette[i] = (0xFFU << 24);
760 s->state |= PNG_PLTE;
761 bytestream2_skip(&s->gb, 4); /* crc */
766 static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,
771 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
772 if (length > 256 || !(s->state & PNG_PLTE))
773 return AVERROR_INVALIDDATA;
775 for (i = 0; i < length; i++) {
776 v = bytestream2_get_byte(&s->gb);
777 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
779 } else if (s->color_type == PNG_COLOR_TYPE_GRAY || s->color_type == PNG_COLOR_TYPE_RGB) {
780 if ((s->color_type == PNG_COLOR_TYPE_GRAY && length != 2) ||
781 (s->color_type == PNG_COLOR_TYPE_RGB && length != 6))
782 return AVERROR_INVALIDDATA;
784 for (i = 0; i < length / 2; i++) {
785 /* only use the least significant bits */
786 v = bytestream2_get_be16(&s->gb) & ((1 << s->bit_depth) - 1);
788 if (s->bit_depth > 8)
789 AV_WB16(&s->transparent_color_be[2 * i], v);
791 s->transparent_color_be[i] = v;
794 return AVERROR_INVALIDDATA;
797 bytestream2_skip(&s->gb, 4); /* crc */
803 static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
805 if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
807 uint8_t *pd = p->data[0];
808 for (j = 0; j < s->height; j++) {
810 for (k = 7; k >= 1; k--)
811 if ((s->width&7) >= k)
812 pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
813 for (i--; i >= 0; i--) {
814 pd[8*i + 7]= pd[i] & 1;
815 pd[8*i + 6]= (pd[i]>>1) & 1;
816 pd[8*i + 5]= (pd[i]>>2) & 1;
817 pd[8*i + 4]= (pd[i]>>3) & 1;
818 pd[8*i + 3]= (pd[i]>>4) & 1;
819 pd[8*i + 2]= (pd[i]>>5) & 1;
820 pd[8*i + 1]= (pd[i]>>6) & 1;
821 pd[8*i + 0]= pd[i]>>7;
823 pd += s->image_linesize;
825 } else if (s->bits_per_pixel == 2) {
827 uint8_t *pd = p->data[0];
828 for (j = 0; j < s->height; j++) {
830 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
831 if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
832 if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
833 if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
834 for (i--; i >= 0; i--) {
835 pd[4*i + 3]= pd[i] & 3;
836 pd[4*i + 2]= (pd[i]>>2) & 3;
837 pd[4*i + 1]= (pd[i]>>4) & 3;
838 pd[4*i + 0]= pd[i]>>6;
841 if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
842 if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
843 if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
844 for (i--; i >= 0; i--) {
845 pd[4*i + 3]= ( pd[i] & 3)*0x55;
846 pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
847 pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
848 pd[4*i + 0]= ( pd[i]>>6 )*0x55;
851 pd += s->image_linesize;
853 } else if (s->bits_per_pixel == 4) {
855 uint8_t *pd = p->data[0];
856 for (j = 0; j < s->height; j++) {
858 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
859 if (s->width&1) pd[2*i+0]= pd[i]>>4;
860 for (i--; i >= 0; i--) {
861 pd[2*i + 1] = pd[i] & 15;
862 pd[2*i + 0] = pd[i] >> 4;
865 if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
866 for (i--; i >= 0; i--) {
867 pd[2*i + 1] = (pd[i] & 15) * 0x11;
868 pd[2*i + 0] = (pd[i] >> 4) * 0x11;
871 pd += s->image_linesize;
876 static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s,
879 uint32_t sequence_number;
880 int cur_w, cur_h, x_offset, y_offset, dispose_op, blend_op;
883 return AVERROR_INVALIDDATA;
885 if (!(s->state & PNG_IHDR)) {
886 av_log(avctx, AV_LOG_ERROR, "fctl before IHDR\n");
887 return AVERROR_INVALIDDATA;
890 s->last_w = s->cur_w;
891 s->last_h = s->cur_h;
892 s->last_x_offset = s->x_offset;
893 s->last_y_offset = s->y_offset;
894 s->last_dispose_op = s->dispose_op;
896 sequence_number = bytestream2_get_be32(&s->gb);
897 cur_w = bytestream2_get_be32(&s->gb);
898 cur_h = bytestream2_get_be32(&s->gb);
899 x_offset = bytestream2_get_be32(&s->gb);
900 y_offset = bytestream2_get_be32(&s->gb);
901 bytestream2_skip(&s->gb, 4); /* delay_num (2), delay_den (2) */
902 dispose_op = bytestream2_get_byte(&s->gb);
903 blend_op = bytestream2_get_byte(&s->gb);
904 bytestream2_skip(&s->gb, 4); /* crc */
906 if (sequence_number == 0 &&
907 (cur_w != s->width ||
908 cur_h != s->height ||
911 cur_w <= 0 || cur_h <= 0 ||
912 x_offset < 0 || y_offset < 0 ||
913 cur_w > s->width - x_offset|| cur_h > s->height - y_offset)
914 return AVERROR_INVALIDDATA;
916 if (blend_op != APNG_BLEND_OP_OVER && blend_op != APNG_BLEND_OP_SOURCE) {
917 av_log(avctx, AV_LOG_ERROR, "Invalid blend_op %d\n", blend_op);
918 return AVERROR_INVALIDDATA;
921 if (sequence_number == 0 && dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
922 // No previous frame to revert to for the first frame
923 // Spec says to just treat it as a APNG_DISPOSE_OP_BACKGROUND
924 dispose_op = APNG_DISPOSE_OP_BACKGROUND;
927 if (blend_op == APNG_BLEND_OP_OVER && !s->has_trns && (
928 avctx->pix_fmt == AV_PIX_FMT_RGB24 ||
929 avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
930 avctx->pix_fmt == AV_PIX_FMT_PAL8 ||
931 avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
932 avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
933 avctx->pix_fmt == AV_PIX_FMT_MONOBLACK
935 // APNG_BLEND_OP_OVER is the same as APNG_BLEND_OP_SOURCE when there is no alpha channel
936 blend_op = APNG_BLEND_OP_SOURCE;
941 s->x_offset = x_offset;
942 s->y_offset = y_offset;
943 s->dispose_op = dispose_op;
944 s->blend_op = blend_op;
949 static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
952 uint8_t *pd = p->data[0];
953 uint8_t *pd_last = s->last_picture.f->data[0];
954 int ls = FFMIN(av_image_get_linesize(p->format, s->width, 0), s->width * s->bpp);
956 ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
957 for (j = 0; j < s->height; j++) {
958 for (i = 0; i < ls; i++)
960 pd += s->image_linesize;
961 pd_last += s->image_linesize;
965 // divide by 255 and round to nearest
966 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
967 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
969 static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s,
975 if (s->blend_op == APNG_BLEND_OP_OVER &&
976 avctx->pix_fmt != AV_PIX_FMT_RGBA &&
977 avctx->pix_fmt != AV_PIX_FMT_GRAY8A &&
978 avctx->pix_fmt != AV_PIX_FMT_PAL8) {
979 avpriv_request_sample(avctx, "Blending with pixel format %s",
980 av_get_pix_fmt_name(avctx->pix_fmt));
981 return AVERROR_PATCHWELCOME;
984 buffer = av_malloc_array(s->image_linesize, s->height);
986 return AVERROR(ENOMEM);
989 // Do the disposal operation specified by the last frame on the frame
990 if (s->last_dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
991 ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
992 memcpy(buffer, s->last_picture.f->data[0], s->image_linesize * s->height);
994 if (s->last_dispose_op == APNG_DISPOSE_OP_BACKGROUND)
995 for (y = s->last_y_offset; y < s->last_y_offset + s->last_h; ++y)
996 memset(buffer + s->image_linesize * y + s->bpp * s->last_x_offset, 0, s->bpp * s->last_w);
998 memcpy(s->previous_picture.f->data[0], buffer, s->image_linesize * s->height);
999 ff_thread_report_progress(&s->previous_picture, INT_MAX, 0);
1001 ff_thread_await_progress(&s->previous_picture, INT_MAX, 0);
1002 memcpy(buffer, s->previous_picture.f->data[0], s->image_linesize * s->height);
1006 if (s->blend_op == APNG_BLEND_OP_SOURCE) {
1007 for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
1008 size_t row_start = s->image_linesize * y + s->bpp * s->x_offset;
1009 memcpy(buffer + row_start, p->data[0] + row_start, s->bpp * s->cur_w);
1011 } else { // APNG_BLEND_OP_OVER
1012 for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
1013 uint8_t *foreground = p->data[0] + s->image_linesize * y + s->bpp * s->x_offset;
1014 uint8_t *background = buffer + s->image_linesize * y + s->bpp * s->x_offset;
1015 for (x = s->x_offset; x < s->x_offset + s->cur_w; ++x, foreground += s->bpp, background += s->bpp) {
1017 uint8_t foreground_alpha, background_alpha, output_alpha;
1020 // Since we might be blending alpha onto alpha, we use the following equations:
1021 // output_alpha = foreground_alpha + (1 - foreground_alpha) * background_alpha
1022 // output = (foreground_alpha * foreground + (1 - foreground_alpha) * background_alpha * background) / output_alpha
1024 switch (avctx->pix_fmt) {
1025 case AV_PIX_FMT_RGBA:
1026 foreground_alpha = foreground[3];
1027 background_alpha = background[3];
1030 case AV_PIX_FMT_GRAY8A:
1031 foreground_alpha = foreground[1];
1032 background_alpha = background[1];
1035 case AV_PIX_FMT_PAL8:
1036 foreground_alpha = s->palette[foreground[0]] >> 24;
1037 background_alpha = s->palette[background[0]] >> 24;
1041 if (foreground_alpha == 0)
1044 if (foreground_alpha == 255) {
1045 memcpy(background, foreground, s->bpp);
1049 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1050 // TODO: Alpha blending with PAL8 will likely need the entire image converted over to RGBA first
1051 avpriv_request_sample(avctx, "Alpha blending palette samples");
1052 background[0] = foreground[0];
1056 output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);
1058 for (b = 0; b < s->bpp - 1; ++b) {
1059 if (output_alpha == 0) {
1061 } else if (background_alpha == 255) {
1062 output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);
1064 output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);
1067 output[b] = output_alpha;
1068 memcpy(background, output, s->bpp);
1073 // Copy blended buffer into the frame and free
1074 memcpy(p->data[0], buffer, s->image_linesize * s->height);
1080 static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
1081 AVFrame *p, AVPacket *avpkt)
1083 AVDictionary *metadata = NULL;
1084 uint32_t tag, length;
1085 int decode_next_dat = 0;
1089 length = bytestream2_get_bytes_left(&s->gb);
1092 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1093 avctx->skip_frame == AVDISCARD_ALL) {
1094 av_frame_set_metadata(p, metadata);
1098 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && length == 0) {
1099 if (!(s->state & PNG_IDAT))
1104 av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length);
1105 if ( s->state & PNG_ALLIMAGE
1106 && avctx->strict_std_compliance <= FF_COMPLIANCE_NORMAL)
1108 ret = AVERROR_INVALIDDATA;
1112 length = bytestream2_get_be32(&s->gb);
1113 if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
1114 av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
1115 ret = AVERROR_INVALIDDATA;
1118 tag = bytestream2_get_le32(&s->gb);
1119 if (avctx->debug & FF_DEBUG_STARTCODE)
1120 av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
1122 ((tag >> 8) & 0xff),
1123 ((tag >> 16) & 0xff),
1124 ((tag >> 24) & 0xff), length);
1126 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1127 avctx->skip_frame == AVDISCARD_ALL) {
1129 case MKTAG('I', 'H', 'D', 'R'):
1130 case MKTAG('p', 'H', 'Y', 's'):
1131 case MKTAG('t', 'E', 'X', 't'):
1132 case MKTAG('I', 'D', 'A', 'T'):
1140 case MKTAG('I', 'H', 'D', 'R'):
1141 if ((ret = decode_ihdr_chunk(avctx, s, length)) < 0)
1144 case MKTAG('p', 'H', 'Y', 's'):
1145 if ((ret = decode_phys_chunk(avctx, s)) < 0)
1148 case MKTAG('f', 'c', 'T', 'L'):
1149 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1151 if ((ret = decode_fctl_chunk(avctx, s, length)) < 0)
1153 decode_next_dat = 1;
1155 case MKTAG('f', 'd', 'A', 'T'):
1156 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1158 if (!decode_next_dat) {
1159 ret = AVERROR_INVALIDDATA;
1162 bytestream2_get_be32(&s->gb);
1165 case MKTAG('I', 'D', 'A', 'T'):
1166 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
1168 if ((ret = decode_idat_chunk(avctx, s, length, p)) < 0)
1171 case MKTAG('P', 'L', 'T', 'E'):
1172 if (decode_plte_chunk(avctx, s, length) < 0)
1175 case MKTAG('t', 'R', 'N', 'S'):
1176 if (decode_trns_chunk(avctx, s, length) < 0)
1179 case MKTAG('t', 'E', 'X', 't'):
1180 if (decode_text_chunk(s, length, 0, &metadata) < 0)
1181 av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
1182 bytestream2_skip(&s->gb, length + 4);
1184 case MKTAG('z', 'T', 'X', 't'):
1185 if (decode_text_chunk(s, length, 1, &metadata) < 0)
1186 av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
1187 bytestream2_skip(&s->gb, length + 4);
1189 case MKTAG('s', 'T', 'E', 'R'): {
1190 AVStereo3D *stereo3d = av_stereo3d_create_side_data(p);
1193 } else if (*s->gb.buffer == 0) {
1194 stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
1195 stereo3d->flags = AV_STEREO3D_FLAG_INVERT;
1196 } else if (*s->gb.buffer == 1) {
1197 stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
1199 av_log(avctx, AV_LOG_WARNING, "Broken sTER chunk - unknown value\n");
1201 bytestream2_skip(&s->gb, length + 4);
1204 case MKTAG('I', 'E', 'N', 'D'):
1205 if (!(s->state & PNG_ALLIMAGE))
1206 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
1207 if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
1208 ret = AVERROR_INVALIDDATA;
1211 bytestream2_skip(&s->gb, 4); /* crc */
1216 bytestream2_skip(&s->gb, length + 4);
1221 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1222 avctx->skip_frame == AVDISCARD_ALL) {
1223 av_frame_set_metadata(p, metadata);
1227 if (s->bits_per_pixel <= 4)
1228 handle_small_bpp(s, p);
1230 /* apply transparency if needed */
1231 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
1232 size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
1233 size_t raw_bpp = s->bpp - byte_depth;
1236 for (y = 0; y < s->height; ++y) {
1237 uint8_t *row = &s->image_buf[s->image_linesize * y];
1239 /* since we're updating in-place, we have to go from right to left */
1240 for (x = s->width; x > 0; --x) {
1241 uint8_t *pixel = &row[s->bpp * (x - 1)];
1242 memmove(pixel, &row[raw_bpp * (x - 1)], raw_bpp);
1244 if (!memcmp(pixel, s->transparent_color_be, raw_bpp)) {
1245 memset(&pixel[raw_bpp], 0, byte_depth);
1247 memset(&pixel[raw_bpp], 0xff, byte_depth);
1253 /* handle p-frames only if a predecessor frame is available */
1254 if (s->last_picture.f->data[0]) {
1255 if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
1256 && s->last_picture.f->width == p->width
1257 && s->last_picture.f->height== p->height
1258 && s->last_picture.f->format== p->format
1260 if (CONFIG_PNG_DECODER && avctx->codec_id != AV_CODEC_ID_APNG)
1261 handle_p_frame_png(s, p);
1262 else if (CONFIG_APNG_DECODER &&
1263 avctx->codec_id == AV_CODEC_ID_APNG &&
1264 (ret = handle_p_frame_apng(avctx, s, p)) < 0)
1268 ff_thread_report_progress(&s->picture, INT_MAX, 0);
1269 ff_thread_report_progress(&s->previous_picture, INT_MAX, 0);
1271 av_frame_set_metadata(p, metadata);
1276 av_dict_free(&metadata);
1277 ff_thread_report_progress(&s->picture, INT_MAX, 0);
1278 ff_thread_report_progress(&s->previous_picture, INT_MAX, 0);
1282 #if CONFIG_PNG_DECODER
1283 static int decode_frame_png(AVCodecContext *avctx,
1284 void *data, int *got_frame,
1287 PNGDecContext *const s = avctx->priv_data;
1288 const uint8_t *buf = avpkt->data;
1289 int buf_size = avpkt->size;
1294 ff_thread_release_buffer(avctx, &s->last_picture);
1295 FFSWAP(ThreadFrame, s->picture, s->last_picture);
1298 bytestream2_init(&s->gb, buf, buf_size);
1300 /* check signature */
1301 sig = bytestream2_get_be64(&s->gb);
1302 if (sig != PNGSIG &&
1304 av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature 0x%08"PRIX64".\n", sig);
1305 return AVERROR_INVALIDDATA;
1308 s->y = s->state = s->has_trns = 0;
1311 s->zstream.zalloc = ff_png_zalloc;
1312 s->zstream.zfree = ff_png_zfree;
1313 s->zstream.opaque = NULL;
1314 ret = inflateInit(&s->zstream);
1316 av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1317 return AVERROR_EXTERNAL;
1320 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1323 if (avctx->skip_frame == AVDISCARD_ALL) {
1325 ret = bytestream2_tell(&s->gb);
1329 if ((ret = av_frame_ref(data, s->picture.f)) < 0)
1334 ret = bytestream2_tell(&s->gb);
1336 inflateEnd(&s->zstream);
1342 #if CONFIG_APNG_DECODER
1343 static int decode_frame_apng(AVCodecContext *avctx,
1344 void *data, int *got_frame,
1347 PNGDecContext *const s = avctx->priv_data;
1351 ff_thread_release_buffer(avctx, &s->last_picture);
1352 FFSWAP(ThreadFrame, s->picture, s->last_picture);
1355 if (!(s->state & PNG_IHDR)) {
1356 if (!avctx->extradata_size)
1357 return AVERROR_INVALIDDATA;
1359 /* only init fields, there is no zlib use in extradata */
1360 s->zstream.zalloc = ff_png_zalloc;
1361 s->zstream.zfree = ff_png_zfree;
1363 bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
1364 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1368 /* reset state for a new frame */
1369 if ((ret = inflateInit(&s->zstream)) != Z_OK) {
1370 av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1371 ret = AVERROR_EXTERNAL;
1375 s->state &= ~(PNG_IDAT | PNG_ALLIMAGE);
1376 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1377 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1380 if (!(s->state & PNG_ALLIMAGE))
1381 av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
1382 if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
1383 ret = AVERROR_INVALIDDATA;
1386 if ((ret = av_frame_ref(data, s->picture.f)) < 0)
1390 ret = bytestream2_tell(&s->gb);
1393 inflateEnd(&s->zstream);
1399 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1401 PNGDecContext *psrc = src->priv_data;
1402 PNGDecContext *pdst = dst->priv_data;
1408 ff_thread_release_buffer(dst, &pdst->picture);
1409 if (psrc->picture.f->data[0] &&
1410 (ret = ff_thread_ref_frame(&pdst->picture, &psrc->picture)) < 0)
1412 if (CONFIG_APNG_DECODER && dst->codec_id == AV_CODEC_ID_APNG) {
1413 pdst->width = psrc->width;
1414 pdst->height = psrc->height;
1415 pdst->bit_depth = psrc->bit_depth;
1416 pdst->color_type = psrc->color_type;
1417 pdst->compression_type = psrc->compression_type;
1418 pdst->interlace_type = psrc->interlace_type;
1419 pdst->filter_type = psrc->filter_type;
1420 pdst->cur_w = psrc->cur_w;
1421 pdst->cur_h = psrc->cur_h;
1422 pdst->x_offset = psrc->x_offset;
1423 pdst->y_offset = psrc->y_offset;
1424 pdst->has_trns = psrc->has_trns;
1425 memcpy(pdst->transparent_color_be, psrc->transparent_color_be, sizeof(pdst->transparent_color_be));
1427 pdst->dispose_op = psrc->dispose_op;
1429 memcpy(pdst->palette, psrc->palette, sizeof(pdst->palette));
1431 pdst->state |= psrc->state & (PNG_IHDR | PNG_PLTE);
1433 ff_thread_release_buffer(dst, &pdst->last_picture);
1434 if (psrc->last_picture.f->data[0] &&
1435 (ret = ff_thread_ref_frame(&pdst->last_picture, &psrc->last_picture)) < 0)
1438 ff_thread_release_buffer(dst, &pdst->previous_picture);
1439 if (psrc->previous_picture.f->data[0] &&
1440 (ret = ff_thread_ref_frame(&pdst->previous_picture, &psrc->previous_picture)) < 0)
1448 static av_cold int png_dec_init(AVCodecContext *avctx)
1450 PNGDecContext *s = avctx->priv_data;
1452 avctx->color_range = AVCOL_RANGE_JPEG;
1455 s->previous_picture.f = av_frame_alloc();
1456 s->last_picture.f = av_frame_alloc();
1457 s->picture.f = av_frame_alloc();
1458 if (!s->previous_picture.f || !s->last_picture.f || !s->picture.f) {
1459 av_frame_free(&s->previous_picture.f);
1460 av_frame_free(&s->last_picture.f);
1461 av_frame_free(&s->picture.f);
1462 return AVERROR(ENOMEM);
1465 if (!avctx->internal->is_copy) {
1466 avctx->internal->allocate_progress = 1;
1467 ff_pngdsp_init(&s->dsp);
1473 static av_cold int png_dec_end(AVCodecContext *avctx)
1475 PNGDecContext *s = avctx->priv_data;
1477 ff_thread_release_buffer(avctx, &s->previous_picture);
1478 av_frame_free(&s->previous_picture.f);
1479 ff_thread_release_buffer(avctx, &s->last_picture);
1480 av_frame_free(&s->last_picture.f);
1481 ff_thread_release_buffer(avctx, &s->picture);
1482 av_frame_free(&s->picture.f);
1483 av_freep(&s->buffer);
1485 av_freep(&s->last_row);
1486 s->last_row_size = 0;
1487 av_freep(&s->tmp_row);
1488 s->tmp_row_size = 0;
1493 #if CONFIG_APNG_DECODER
1494 AVCodec ff_apng_decoder = {
1496 .long_name = NULL_IF_CONFIG_SMALL("APNG (Animated Portable Network Graphics) image"),
1497 .type = AVMEDIA_TYPE_VIDEO,
1498 .id = AV_CODEC_ID_APNG,
1499 .priv_data_size = sizeof(PNGDecContext),
1500 .init = png_dec_init,
1501 .close = png_dec_end,
1502 .decode = decode_frame_apng,
1503 .init_thread_copy = ONLY_IF_THREADS_ENABLED(png_dec_init),
1504 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1505 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
1509 #if CONFIG_PNG_DECODER
1510 AVCodec ff_png_decoder = {
1512 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
1513 .type = AVMEDIA_TYPE_VIDEO,
1514 .id = AV_CODEC_ID_PNG,
1515 .priv_data_size = sizeof(PNGDecContext),
1516 .init = png_dec_init,
1517 .close = png_dec_end,
1518 .decode = decode_frame_png,
1519 .init_thread_copy = ONLY_IF_THREADS_ENABLED(png_dec_init),
1520 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1521 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,