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/bprint.h"
25 #include "libavutil/imgutils.h"
27 #include "bytestream.h"
36 typedef struct PNGDecContext {
38 AVCodecContext *avctx;
41 ThreadFrame previous_picture;
42 ThreadFrame last_picture;
48 int x_offset, y_offset;
49 uint8_t dispose_op, blend_op;
62 uint32_t palette[256];
65 unsigned int last_row_size;
67 unsigned int tmp_row_size;
71 int crow_size; /* compressed row size (include filter type) */
72 int row_size; /* decompressed row size */
73 int pass_row_size; /* decompress row size of the current pass */
78 /* Mask to determine which pixels are valid in a pass */
79 static const uint8_t png_pass_mask[NB_PASSES] = {
80 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
83 /* Mask to determine which y pixels can be written in a pass */
84 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
85 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
88 /* Mask to determine which pixels to overwrite while displaying */
89 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
90 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
93 /* NOTE: we try to construct a good looking image at each pass. width
94 * is the original image width. We also do pixel format conversion at
96 static void png_put_interlaced_row(uint8_t *dst, int width,
97 int bits_per_pixel, int pass,
98 int color_type, const uint8_t *src)
100 int x, mask, dsp_mask, j, src_x, b, bpp;
104 mask = png_pass_mask[pass];
105 dsp_mask = png_pass_dsp_mask[pass];
107 switch (bits_per_pixel) {
110 for (x = 0; x < width; x++) {
112 if ((dsp_mask << j) & 0x80) {
113 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
114 dst[x >> 3] &= 0xFF7F>>j;
115 dst[x >> 3] |= b << (7 - j);
117 if ((mask << j) & 0x80)
123 for (x = 0; x < width; x++) {
124 int j2 = 2 * (x & 3);
126 if ((dsp_mask << j) & 0x80) {
127 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
128 dst[x >> 2] &= 0xFF3F>>j2;
129 dst[x >> 2] |= b << (6 - j2);
131 if ((mask << j) & 0x80)
137 for (x = 0; x < width; x++) {
140 if ((dsp_mask << j) & 0x80) {
141 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
142 dst[x >> 1] &= 0xFF0F>>j2;
143 dst[x >> 1] |= b << (4 - j2);
145 if ((mask << j) & 0x80)
150 bpp = bits_per_pixel >> 3;
153 for (x = 0; x < width; x++) {
155 if ((dsp_mask << j) & 0x80) {
159 if ((mask << j) & 0x80)
166 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
170 for (i = 0; i < w; i++) {
171 int a, b, c, p, pa, pb, pc;
184 if (pa <= pb && pa <= pc)
194 #define UNROLL1(bpp, op) \
203 for (; i <= size - bpp; i += bpp) { \
204 dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
207 dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
210 dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
213 dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
217 #define UNROLL_FILTER(op) \
220 } else if (bpp == 2) { \
222 } else if (bpp == 3) { \
224 } else if (bpp == 4) { \
227 for (; i < size; i++) { \
228 dst[i] = op(dst[i - bpp], src[i], last[i]); \
231 /* NOTE: 'dst' can be equal to 'last' */
232 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
233 uint8_t *src, uint8_t *last, int size, int bpp)
235 int i, p, r, g, b, a;
237 switch (filter_type) {
238 case PNG_FILTER_VALUE_NONE:
239 memcpy(dst, src, size);
241 case PNG_FILTER_VALUE_SUB:
242 for (i = 0; i < bpp; i++)
246 for (; i < size; i += bpp) {
247 unsigned s = *(int *)(src + i);
248 p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
249 *(int *)(dst + i) = p;
252 #define OP_SUB(x, s, l) ((x) + (s))
253 UNROLL_FILTER(OP_SUB);
256 case PNG_FILTER_VALUE_UP:
257 dsp->add_bytes_l2(dst, src, last, size);
259 case PNG_FILTER_VALUE_AVG:
260 for (i = 0; i < bpp; i++) {
264 #define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
265 UNROLL_FILTER(OP_AVG);
267 case PNG_FILTER_VALUE_PAETH:
268 for (i = 0; i < bpp; i++) {
272 if (bpp > 2 && size > 4) {
273 /* would write off the end of the array if we let it process
274 * the last pixel with bpp=3 */
275 int w = bpp == 4 ? size : size - 3;
277 dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp);
281 ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
286 /* This used to be called "deloco" in FFmpeg
287 * and is actually an inverse reversible colorspace transformation */
288 #define YUV2RGB(NAME, TYPE) \
289 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
292 for (i = 0; i < size; i += 3 + alpha) { \
293 int g = dst [i + 1]; \
299 YUV2RGB(rgb8, uint8_t)
300 YUV2RGB(rgb16, uint16_t)
302 /* process exactly one decompressed row */
303 static void png_handle_row(PNGDecContext *s)
305 uint8_t *ptr, *last_row;
308 if (!s->interlace_type) {
309 ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
311 last_row = s->last_row;
313 last_row = ptr - s->image_linesize;
315 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
316 last_row, s->row_size, s->bpp);
317 /* loco lags by 1 row so that it doesn't interfere with top prediction */
318 if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
319 if (s->bit_depth == 16) {
320 deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
321 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
323 deloco_rgb8(ptr - s->image_linesize, s->row_size,
324 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
328 if (s->y == s->cur_h) {
329 s->state |= PNG_ALLIMAGE;
330 if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
331 if (s->bit_depth == 16) {
332 deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
333 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
335 deloco_rgb8(ptr, s->row_size,
336 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
343 ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
344 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
345 /* if we already read one row, it is time to stop to
346 * wait for the next one */
349 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
350 s->last_row, s->pass_row_size, s->bpp);
351 FFSWAP(uint8_t *, s->last_row, s->tmp_row);
352 FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size);
355 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
356 png_put_interlaced_row(ptr, s->cur_w, s->bits_per_pixel, s->pass,
357 s->color_type, s->last_row);
360 if (s->y == s->cur_h) {
361 memset(s->last_row, 0, s->row_size);
363 if (s->pass == NB_PASSES - 1) {
364 s->state |= PNG_ALLIMAGE;
369 s->pass_row_size = ff_png_pass_row_size(s->pass,
372 s->crow_size = s->pass_row_size + 1;
373 if (s->pass_row_size != 0)
375 /* skip pass if empty row */
384 static int png_decode_idat(PNGDecContext *s, int length)
387 s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
388 s->zstream.next_in = (unsigned char *)s->gb.buffer;
389 bytestream2_skip(&s->gb, length);
391 /* decode one line if possible */
392 while (s->zstream.avail_in > 0) {
393 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
394 if (ret != Z_OK && ret != Z_STREAM_END) {
395 av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
396 return AVERROR_EXTERNAL;
398 if (s->zstream.avail_out == 0) {
399 if (!(s->state & PNG_ALLIMAGE)) {
402 s->zstream.avail_out = s->crow_size;
403 s->zstream.next_out = s->crow_buf;
405 if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
406 av_log(NULL, AV_LOG_WARNING,
407 "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
414 static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
415 const uint8_t *data_end)
422 zstream.zalloc = ff_png_zalloc;
423 zstream.zfree = ff_png_zfree;
424 zstream.opaque = NULL;
425 if (inflateInit(&zstream) != Z_OK)
426 return AVERROR_EXTERNAL;
427 zstream.next_in = (unsigned char *)data;
428 zstream.avail_in = data_end - data;
429 av_bprint_init(bp, 0, -1);
431 while (zstream.avail_in > 0) {
432 av_bprint_get_buffer(bp, 1, &buf, &buf_size);
434 ret = AVERROR(ENOMEM);
437 zstream.next_out = buf;
438 zstream.avail_out = buf_size;
439 ret = inflate(&zstream, Z_PARTIAL_FLUSH);
440 if (ret != Z_OK && ret != Z_STREAM_END) {
441 ret = AVERROR_EXTERNAL;
444 bp->len += zstream.next_out - buf;
445 if (ret == Z_STREAM_END)
448 inflateEnd(&zstream);
449 bp->str[bp->len] = 0;
453 inflateEnd(&zstream);
454 av_bprint_finalize(bp, NULL);
458 static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
463 for (i = 0; i < size_in; i++)
464 extra += in[i] >= 0x80;
465 if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
467 q = out = av_malloc(size_in + extra + 1);
470 for (i = 0; i < size_in; i++) {
472 *(q++) = 0xC0 | (in[i] >> 6);
473 *(q++) = 0x80 | (in[i] & 0x3F);
482 static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed,
486 const uint8_t *data = s->gb.buffer;
487 const uint8_t *data_end = data + length;
488 const uint8_t *keyword = data;
489 const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
490 uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
495 return AVERROR_INVALIDDATA;
496 data = keyword_end + 1;
499 if (data == data_end)
500 return AVERROR_INVALIDDATA;
503 return AVERROR_INVALIDDATA;
504 if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
507 av_bprint_finalize(&bp, (char **)&text);
509 return AVERROR(ENOMEM);
511 text = (uint8_t *)data;
512 text_len = data_end - text;
515 kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
516 txt_utf8 = iso88591_to_utf8(text, text_len);
519 if (!(kw_utf8 && txt_utf8)) {
522 return AVERROR(ENOMEM);
525 av_dict_set(dict, kw_utf8, txt_utf8,
526 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
530 static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s,
534 return AVERROR_INVALIDDATA;
536 if (s->state & PNG_IDAT) {
537 av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");
538 return AVERROR_INVALIDDATA;
541 s->width = s->cur_w = bytestream2_get_be32(&s->gb);
542 s->height = s->cur_h = bytestream2_get_be32(&s->gb);
543 if (av_image_check_size(s->width, s->height, 0, avctx)) {
544 s->width = s->height = 0;
545 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
546 return AVERROR_INVALIDDATA;
548 s->bit_depth = bytestream2_get_byte(&s->gb);
549 s->color_type = bytestream2_get_byte(&s->gb);
550 s->compression_type = bytestream2_get_byte(&s->gb);
551 s->filter_type = bytestream2_get_byte(&s->gb);
552 s->interlace_type = bytestream2_get_byte(&s->gb);
553 bytestream2_skip(&s->gb, 4); /* crc */
554 s->state |= PNG_IHDR;
555 if (avctx->debug & FF_DEBUG_PICT_INFO)
556 av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
557 "compression_type=%d filter_type=%d interlace_type=%d\n",
558 s->width, s->height, s->bit_depth, s->color_type,
559 s->compression_type, s->filter_type, s->interlace_type);
564 static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s)
566 if (s->state & PNG_IDAT) {
567 av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
568 return AVERROR_INVALIDDATA;
570 avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
571 avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
572 if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
573 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
574 bytestream2_skip(&s->gb, 1); /* unit specifier */
575 bytestream2_skip(&s->gb, 4); /* crc */
580 static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s,
581 uint32_t length, AVFrame *p)
585 if (!(s->state & PNG_IHDR)) {
586 av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
587 return AVERROR_INVALIDDATA;
589 if (!(s->state & PNG_IDAT)) {
590 /* init image info */
591 avctx->width = s->width;
592 avctx->height = s->height;
594 s->channels = ff_png_get_nb_channels(s->color_type);
595 s->bits_per_pixel = s->bit_depth * s->channels;
596 s->bpp = (s->bits_per_pixel + 7) >> 3;
597 s->row_size = (s->cur_w * s->bits_per_pixel + 7) >> 3;
599 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
600 s->color_type == PNG_COLOR_TYPE_RGB) {
601 avctx->pix_fmt = AV_PIX_FMT_RGB24;
602 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
603 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
604 avctx->pix_fmt = AV_PIX_FMT_RGBA;
605 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
606 s->color_type == PNG_COLOR_TYPE_GRAY) {
607 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
608 } else if (s->bit_depth == 16 &&
609 s->color_type == PNG_COLOR_TYPE_GRAY) {
610 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
611 } else if (s->bit_depth == 16 &&
612 s->color_type == PNG_COLOR_TYPE_RGB) {
613 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
614 } else if (s->bit_depth == 16 &&
615 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
616 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
617 } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
618 s->color_type == PNG_COLOR_TYPE_PALETTE) {
619 avctx->pix_fmt = AV_PIX_FMT_PAL8;
620 } else if (s->bit_depth == 1 && s->bits_per_pixel == 1) {
621 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
622 } else if (s->bit_depth == 8 &&
623 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
624 avctx->pix_fmt = AV_PIX_FMT_YA8;
625 } else if (s->bit_depth == 16 &&
626 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
627 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
629 av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
630 "and color type %d\n",
631 s->bit_depth, s->color_type);
632 return AVERROR_INVALIDDATA;
635 if ((ret = ff_thread_get_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
637 ff_thread_finish_setup(avctx);
639 p->pict_type = AV_PICTURE_TYPE_I;
641 p->interlaced_frame = !!s->interlace_type;
643 /* compute the compressed row size */
644 if (!s->interlace_type) {
645 s->crow_size = s->row_size + 1;
648 s->pass_row_size = ff_png_pass_row_size(s->pass,
651 s->crow_size = s->pass_row_size + 1;
653 av_dlog(avctx, "row_size=%d crow_size =%d\n",
654 s->row_size, s->crow_size);
655 s->image_buf = p->data[0];
656 s->image_linesize = p->linesize[0];
657 /* copy the palette if needed */
658 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
659 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
660 /* empty row is used if differencing to the first row */
661 av_fast_padded_mallocz(&s->last_row, &s->last_row_size, s->row_size);
663 return AVERROR_INVALIDDATA;
664 if (s->interlace_type ||
665 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
666 av_fast_padded_malloc(&s->tmp_row, &s->tmp_row_size, s->row_size);
668 return AVERROR_INVALIDDATA;
671 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
673 return AVERROR(ENOMEM);
675 /* we want crow_buf+1 to be 16-byte aligned */
676 s->crow_buf = s->buffer + 15;
677 s->zstream.avail_out = s->crow_size;
678 s->zstream.next_out = s->crow_buf;
680 s->state |= PNG_IDAT;
681 if ((ret = png_decode_idat(s, length)) < 0)
683 bytestream2_skip(&s->gb, 4); /* crc */
688 static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s,
693 if ((length % 3) != 0 || length > 256 * 3)
694 return AVERROR_INVALIDDATA;
695 /* read the palette */
697 for (i = 0; i < n; i++) {
698 r = bytestream2_get_byte(&s->gb);
699 g = bytestream2_get_byte(&s->gb);
700 b = bytestream2_get_byte(&s->gb);
701 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
704 s->palette[i] = (0xFFU << 24);
705 s->state |= PNG_PLTE;
706 bytestream2_skip(&s->gb, 4); /* crc */
711 static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,
716 /* read the transparency. XXX: Only palette mode supported */
717 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
719 !(s->state & PNG_PLTE))
720 return AVERROR_INVALIDDATA;
721 for (i = 0; i < length; i++) {
722 v = bytestream2_get_byte(&s->gb);
723 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
725 bytestream2_skip(&s->gb, 4); /* crc */
730 static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
732 if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
734 uint8_t *pd = p->data[0];
735 for (j = 0; j < s->height; j++) {
737 for (k = 7; k >= 1; k--)
738 if ((s->width&7) >= k)
739 pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
740 for (i--; i >= 0; i--) {
741 pd[8*i + 7]= pd[i] & 1;
742 pd[8*i + 6]= (pd[i]>>1) & 1;
743 pd[8*i + 5]= (pd[i]>>2) & 1;
744 pd[8*i + 4]= (pd[i]>>3) & 1;
745 pd[8*i + 3]= (pd[i]>>4) & 1;
746 pd[8*i + 2]= (pd[i]>>5) & 1;
747 pd[8*i + 1]= (pd[i]>>6) & 1;
748 pd[8*i + 0]= pd[i]>>7;
750 pd += s->image_linesize;
752 } else if (s->bits_per_pixel == 2) {
754 uint8_t *pd = p->data[0];
755 for (j = 0; j < s->height; j++) {
757 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
758 if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
759 if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
760 if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
761 for (i--; i >= 0; i--) {
762 pd[4*i + 3]= pd[i] & 3;
763 pd[4*i + 2]= (pd[i]>>2) & 3;
764 pd[4*i + 1]= (pd[i]>>4) & 3;
765 pd[4*i + 0]= pd[i]>>6;
768 if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
769 if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
770 if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
771 for (i--; i >= 0; i--) {
772 pd[4*i + 3]= ( pd[i] & 3)*0x55;
773 pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
774 pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
775 pd[4*i + 0]= ( pd[i]>>6 )*0x55;
778 pd += s->image_linesize;
780 } else if (s->bits_per_pixel == 4) {
782 uint8_t *pd = p->data[0];
783 for (j = 0; j < s->height; j++) {
785 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
786 if (s->width&1) pd[2*i+0]= pd[i]>>4;
787 for (i--; i >= 0; i--) {
788 pd[2*i + 1] = pd[i] & 15;
789 pd[2*i + 0] = pd[i] >> 4;
792 if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
793 for (i--; i >= 0; i--) {
794 pd[2*i + 1] = (pd[i] & 15) * 0x11;
795 pd[2*i + 0] = (pd[i] >> 4) * 0x11;
798 pd += s->image_linesize;
803 static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s,
806 uint32_t sequence_number;
809 return AVERROR_INVALIDDATA;
811 sequence_number = bytestream2_get_be32(&s->gb);
812 s->cur_w = bytestream2_get_be32(&s->gb);
813 s->cur_h = bytestream2_get_be32(&s->gb);
814 s->x_offset = bytestream2_get_be32(&s->gb);
815 s->y_offset = bytestream2_get_be32(&s->gb);
816 bytestream2_skip(&s->gb, 4); /* delay_num (2), delay_den (2) */
817 s->dispose_op = bytestream2_get_byte(&s->gb);
818 s->blend_op = bytestream2_get_byte(&s->gb);
819 bytestream2_skip(&s->gb, 4); /* crc */
821 if (sequence_number == 0 &&
822 (s->cur_w != s->width ||
823 s->cur_h != s->height ||
826 s->cur_w <= 0 || s->cur_h <= 0 ||
827 s->x_offset < 0 || s->y_offset < 0 ||
828 s->cur_w > s->width - s->x_offset|| s->cur_h > s->height - s->y_offset)
829 return AVERROR_INVALIDDATA;
831 /* always (re)start with a clean frame */
832 if (sequence_number == 0) {
833 s->dispose_op = APNG_DISPOSE_OP_BACKGROUND;
837 if (s->frame_id == 1 && s->dispose_op == APNG_DISPOSE_OP_PREVIOUS)
838 /* previous for the second frame is the first frame */
839 s->dispose_op = APNG_DISPOSE_OP_NONE;
845 static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
848 uint8_t *pd = p->data[0];
849 uint8_t *pd_last = s->last_picture.f->data[0];
850 int ls = FFMIN(av_image_get_linesize(p->format, s->width, 0), s->width * s->bpp);
852 ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
853 for (j = 0; j < s->height; j++) {
854 for (i = 0; i < ls; i++)
856 pd += s->image_linesize;
857 pd_last += s->image_linesize;
861 // divide by 255 and round to nearest
862 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
863 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
865 static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s,
869 uint8_t *pd = p->data[0];
870 uint8_t *pd_last = s->last_picture.f->data[0];
871 uint8_t *pd_last_region = s->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?
872 s->previous_picture.f->data[0] : s->last_picture.f->data[0];
873 int ls = FFMIN(av_image_get_linesize(p->format, s->width, 0), s->width * s->bpp);
878 if (s->blend_op == APNG_BLEND_OP_OVER &&
879 avctx->pix_fmt != AV_PIX_FMT_RGBA && avctx->pix_fmt != AV_PIX_FMT_ARGB) {
880 avpriv_request_sample(avctx, "Blending with pixel format %s",
881 av_get_pix_fmt_name(avctx->pix_fmt));
882 return AVERROR_PATCHWELCOME;
885 ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
886 if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS)
887 ff_thread_await_progress(&s->previous_picture, INT_MAX, 0);
889 for (j = 0; j < s->y_offset; j++) {
890 memcpy(pd, pd_last, ls);
891 pd += s->image_linesize;
892 pd_last += s->image_linesize;
895 if (s->dispose_op != APNG_DISPOSE_OP_BACKGROUND && s->blend_op == APNG_BLEND_OP_OVER) {
896 uint8_t ri, gi, bi, ai;
898 pd_last_region += s->y_offset * s->image_linesize;
899 if (avctx->pix_fmt == AV_PIX_FMT_RGBA) {
911 for (j = s->y_offset; j < s->y_offset + s->cur_h; j++) {
912 i = s->x_offset * s->bpp;
914 memcpy(pd, pd_last, i);
915 for (; i < (s->x_offset + s->cur_w) * s->bpp; i += s->bpp) {
916 uint8_t alpha = pd[i+ai];
918 /* output = alpha * foreground + (1-alpha) * background */
921 pd[i+ri] = pd_last_region[i+ri];
922 pd[i+gi] = pd_last_region[i+gi];
923 pd[i+bi] = pd_last_region[i+bi];
929 pd[i+ri] = FAST_DIV255(alpha * pd[i+ri] + (255 - alpha) * pd_last_region[i+ri]);
930 pd[i+gi] = FAST_DIV255(alpha * pd[i+gi] + (255 - alpha) * pd_last_region[i+gi]);
931 pd[i+bi] = FAST_DIV255(alpha * pd[i+bi] + (255 - alpha) * pd_last_region[i+bi]);
937 memcpy(pd+i, pd_last+i, ls - i);
938 pd += s->image_linesize;
939 pd_last += s->image_linesize;
940 pd_last_region += s->image_linesize;
943 for (j = s->y_offset; j < s->y_offset + s->cur_h; j++) {
944 int end_offset = (s->x_offset + s->cur_w) * s->bpp;
945 int end_len = ls - end_offset;
947 memcpy(pd, pd_last, s->x_offset * s->bpp);
949 memcpy(pd+end_offset, pd_last+end_offset, end_len);
950 pd += s->image_linesize;
951 pd_last += s->image_linesize;
955 for (j = s->y_offset + s->cur_h; j < s->height; j++) {
956 memcpy(pd, pd_last, ls);
957 pd += s->image_linesize;
958 pd_last += s->image_linesize;
964 static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
965 AVFrame *p, AVPacket *avpkt)
967 AVDictionary *metadata = NULL;
968 uint32_t tag, length;
969 int decode_next_dat = 0;
970 int ret = AVERROR_INVALIDDATA;
974 length = bytestream2_get_bytes_left(&s->gb);
976 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && length == 0) {
977 if (!(s->state & PNG_IDAT))
982 av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length);
983 if ( s->state & PNG_ALLIMAGE
984 && avctx->strict_std_compliance <= FF_COMPLIANCE_NORMAL)
989 length = bytestream2_get_be32(&s->gb);
990 if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
991 av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
994 tag = bytestream2_get_le32(&s->gb);
995 if (avctx->debug & FF_DEBUG_STARTCODE)
996 av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
999 ((tag >> 16) & 0xff),
1000 ((tag >> 24) & 0xff), length);
1002 case MKTAG('I', 'H', 'D', 'R'):
1003 if (decode_ihdr_chunk(avctx, s, length) < 0)
1006 case MKTAG('p', 'H', 'Y', 's'):
1007 if (decode_phys_chunk(avctx, s) < 0)
1010 case MKTAG('f', 'c', 'T', 'L'):
1011 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1013 if ((ret = decode_fctl_chunk(avctx, s, length)) < 0)
1015 decode_next_dat = 1;
1017 case MKTAG('f', 'd', 'A', 'T'):
1018 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1020 if (!decode_next_dat)
1022 bytestream2_get_be32(&s->gb);
1025 case MKTAG('I', 'D', 'A', 'T'):
1026 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
1028 if (decode_idat_chunk(avctx, s, length, p) < 0)
1031 case MKTAG('P', 'L', 'T', 'E'):
1032 if (decode_plte_chunk(avctx, s, length) < 0)
1035 case MKTAG('t', 'R', 'N', 'S'):
1036 if (decode_trns_chunk(avctx, s, length) < 0)
1039 case MKTAG('t', 'E', 'X', 't'):
1040 if (decode_text_chunk(s, length, 0, &metadata) < 0)
1041 av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
1042 bytestream2_skip(&s->gb, length + 4);
1044 case MKTAG('z', 'T', 'X', 't'):
1045 if (decode_text_chunk(s, length, 1, &metadata) < 0)
1046 av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
1047 bytestream2_skip(&s->gb, length + 4);
1049 case MKTAG('I', 'E', 'N', 'D'):
1050 if (!(s->state & PNG_ALLIMAGE))
1051 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
1052 if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
1055 bytestream2_skip(&s->gb, 4); /* crc */
1060 bytestream2_skip(&s->gb, length + 4);
1066 if (s->bits_per_pixel <= 4)
1067 handle_small_bpp(s, p);
1069 /* handle p-frames only if a predecessor frame is available */
1070 ref = s->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?
1071 s->previous_picture.f : s->last_picture.f;
1073 if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
1074 && ref->width == p->width
1075 && ref->height== p->height
1076 && ref->format== p->format
1078 if (CONFIG_PNG_DECODER && avctx->codec_id != AV_CODEC_ID_APNG)
1079 handle_p_frame_png(s, p);
1080 else if (CONFIG_APNG_DECODER &&
1081 avctx->codec_id == AV_CODEC_ID_APNG &&
1082 (ret = handle_p_frame_apng(avctx, s, p)) < 0)
1086 ff_thread_report_progress(&s->picture, INT_MAX, 0);
1088 av_frame_set_metadata(p, metadata);
1093 av_dict_free(&metadata);
1094 ff_thread_report_progress(&s->picture, INT_MAX, 0);
1098 #if CONFIG_PNG_DECODER
1099 static int decode_frame_png(AVCodecContext *avctx,
1100 void *data, int *got_frame,
1103 PNGDecContext *const s = avctx->priv_data;
1104 const uint8_t *buf = avpkt->data;
1105 int buf_size = avpkt->size;
1110 ff_thread_release_buffer(avctx, &s->last_picture);
1111 FFSWAP(ThreadFrame, s->picture, s->last_picture);
1114 bytestream2_init(&s->gb, buf, buf_size);
1116 /* check signature */
1117 sig = bytestream2_get_be64(&s->gb);
1118 if (sig != PNGSIG &&
1120 av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
1121 return AVERROR_INVALIDDATA;
1124 s->y = s->state = 0;
1127 s->zstream.zalloc = ff_png_zalloc;
1128 s->zstream.zfree = ff_png_zfree;
1129 s->zstream.opaque = NULL;
1130 ret = inflateInit(&s->zstream);
1132 av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1133 return AVERROR_EXTERNAL;
1136 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1139 if ((ret = av_frame_ref(data, s->picture.f)) < 0)
1144 ret = bytestream2_tell(&s->gb);
1146 inflateEnd(&s->zstream);
1152 #if CONFIG_APNG_DECODER
1153 static int decode_frame_apng(AVCodecContext *avctx,
1154 void *data, int *got_frame,
1157 PNGDecContext *const s = avctx->priv_data;
1162 ff_thread_release_buffer(avctx, &s->previous_picture);
1163 tmp = s->previous_picture;
1164 s->previous_picture = s->last_picture;
1165 s->last_picture = s->picture;
1169 if (!(s->state & PNG_IHDR)) {
1170 if (!avctx->extradata_size)
1171 return AVERROR_INVALIDDATA;
1173 /* only init fields, there is no zlib use in extradata */
1174 s->zstream.zalloc = ff_png_zalloc;
1175 s->zstream.zfree = ff_png_zfree;
1177 bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
1178 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1182 /* reset state for a new frame */
1183 if ((ret = inflateInit(&s->zstream)) != Z_OK) {
1184 av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1185 ret = AVERROR_EXTERNAL;
1189 s->state &= ~(PNG_IDAT | PNG_ALLIMAGE);
1190 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1191 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1194 if (!(s->state & PNG_ALLIMAGE))
1195 av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
1196 if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
1197 ret = AVERROR_INVALIDDATA;
1200 if ((ret = av_frame_ref(data, s->picture.f)) < 0)
1204 ret = bytestream2_tell(&s->gb);
1207 inflateEnd(&s->zstream);
1212 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1214 PNGDecContext *psrc = src->priv_data;
1215 PNGDecContext *pdst = dst->priv_data;
1221 pdst->frame_id = psrc->frame_id;
1223 ff_thread_release_buffer(dst, &pdst->picture);
1224 if (psrc->picture.f->data[0] &&
1225 (ret = ff_thread_ref_frame(&pdst->picture, &psrc->picture)) < 0)
1227 if (CONFIG_APNG_DECODER && dst->codec_id == AV_CODEC_ID_APNG) {
1228 ff_thread_release_buffer(dst, &pdst->last_picture);
1229 if (psrc->last_picture.f->data[0])
1230 return ff_thread_ref_frame(&pdst->last_picture, &psrc->last_picture);
1236 static av_cold int png_dec_init(AVCodecContext *avctx)
1238 PNGDecContext *s = avctx->priv_data;
1241 s->previous_picture.f = av_frame_alloc();
1242 s->last_picture.f = av_frame_alloc();
1243 s->picture.f = av_frame_alloc();
1244 if (!s->previous_picture.f || !s->last_picture.f || !s->picture.f) {
1245 av_frame_free(&s->previous_picture.f);
1246 av_frame_free(&s->last_picture.f);
1247 av_frame_free(&s->picture.f);
1248 return AVERROR(ENOMEM);
1251 if (!avctx->internal->is_copy) {
1252 avctx->internal->allocate_progress = 1;
1253 ff_pngdsp_init(&s->dsp);
1259 static av_cold int png_dec_end(AVCodecContext *avctx)
1261 PNGDecContext *s = avctx->priv_data;
1263 ff_thread_release_buffer(avctx, &s->previous_picture);
1264 av_frame_free(&s->previous_picture.f);
1265 ff_thread_release_buffer(avctx, &s->last_picture);
1266 av_frame_free(&s->last_picture.f);
1267 ff_thread_release_buffer(avctx, &s->picture);
1268 av_frame_free(&s->picture.f);
1269 av_freep(&s->buffer);
1271 av_freep(&s->last_row);
1272 s->last_row_size = 0;
1273 av_freep(&s->tmp_row);
1274 s->tmp_row_size = 0;
1279 #if CONFIG_APNG_DECODER
1280 AVCodec ff_apng_decoder = {
1282 .long_name = NULL_IF_CONFIG_SMALL("APNG (Animated Portable Network Graphics) image"),
1283 .type = AVMEDIA_TYPE_VIDEO,
1284 .id = AV_CODEC_ID_APNG,
1285 .priv_data_size = sizeof(PNGDecContext),
1286 .init = png_dec_init,
1287 .close = png_dec_end,
1288 .decode = decode_frame_apng,
1289 .init_thread_copy = ONLY_IF_THREADS_ENABLED(png_dec_init),
1290 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1291 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
1295 #if CONFIG_PNG_DECODER
1296 AVCodec ff_png_decoder = {
1298 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
1299 .type = AVMEDIA_TYPE_VIDEO,
1300 .id = AV_CODEC_ID_PNG,
1301 .priv_data_size = sizeof(PNGDecContext),
1302 .init = png_dec_init,
1303 .close = png_dec_end,
1304 .decode = decode_frame_png,
1305 .init_thread_copy = ONLY_IF_THREADS_ENABLED(png_dec_init),
1306 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1307 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS /*| CODEC_CAP_DRAW_HORIZ_BAND*/,