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"
33 * - add 16 bit depth support
40 typedef struct PNGDecContext {
42 AVCodecContext *avctx;
45 AVFrame picture1, picture2;
46 AVFrame *current_picture, *last_picture;
61 uint32_t palette[256];
66 int crow_size; /* compressed row size (include filter type) */
67 int row_size; /* decompressed row size */
68 int pass_row_size; /* decompress row size of the current pass */
73 /* Mask to determine which pixels are valid in a pass */
74 static const uint8_t png_pass_mask[NB_PASSES] = {
75 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
78 /* Mask to determine which y pixels can be written in a pass */
79 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
80 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
83 /* Mask to determine which pixels to overwrite while displaying */
84 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
85 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
88 /* NOTE: we try to construct a good looking image at each pass. width
89 is the original image width. We also do pixel format conversion at
91 static void png_put_interlaced_row(uint8_t *dst, int width,
92 int bits_per_pixel, int pass,
93 int color_type, const uint8_t *src)
95 int x, mask, dsp_mask, j, src_x, b, bpp;
99 mask = png_pass_mask[pass];
100 dsp_mask = png_pass_dsp_mask[pass];
101 switch(bits_per_pixel) {
104 for(x = 0; x < width; x++) {
106 if ((dsp_mask << j) & 0x80) {
107 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
108 dst[x >> 3] &= 0xFF7F>>j;
109 dst[x >> 3] |= b << (7 - j);
111 if ((mask << j) & 0x80)
117 for(x = 0; x < width; x++) {
120 if ((dsp_mask << j) & 0x80) {
121 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
122 dst[x >> 2] &= 0xFF3F>>j2;
123 dst[x >> 2] |= b << (6 - j2);
125 if ((mask << j) & 0x80)
131 for(x = 0; x < width; x++) {
134 if ((dsp_mask << j) & 0x80) {
135 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
136 dst[x >> 1] &= 0xFF0F>>j2;
137 dst[x >> 1] |= b << (4 - j2);
139 if ((mask << j) & 0x80)
144 bpp = bits_per_pixel >> 3;
147 for(x = 0; x < width; x++) {
149 if ((dsp_mask << j) & 0x80) {
153 if ((mask << j) & 0x80)
160 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
163 for(i = 0; i < w; i++) {
164 int a, b, c, p, pa, pb, pc;
177 if (pa <= pb && pa <= pc)
187 #define UNROLL1(bpp, op) {\
189 if(bpp >= 2) g = dst[1];\
190 if(bpp >= 3) b = dst[2];\
191 if(bpp >= 4) a = dst[3];\
192 for(; i < size; i+=bpp) {\
193 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
194 if(bpp == 1) continue;\
195 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
196 if(bpp == 2) continue;\
197 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
198 if(bpp == 3) continue;\
199 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
203 #define UNROLL_FILTER(op)\
204 if(bpp == 1) UNROLL1(1, op)\
205 else if(bpp == 2) UNROLL1(2, op)\
206 else if(bpp == 3) UNROLL1(3, op)\
207 else if(bpp == 4) UNROLL1(4, op)\
209 for (; i < size; i += bpp) {\
211 for (j = 0; j < bpp; j++)\
212 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
216 /* NOTE: 'dst' can be equal to 'last' */
217 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
218 uint8_t *src, uint8_t *last, int size, int bpp)
220 int i, p, r, g, b, a;
222 switch(filter_type) {
223 case PNG_FILTER_VALUE_NONE:
224 memcpy(dst, src, size);
226 case PNG_FILTER_VALUE_SUB:
227 for(i = 0; i < bpp; i++) {
232 for(; i < size; i+=bpp) {
233 int s = *(int*)(src+i);
234 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
238 #define OP_SUB(x,s,l) x+s
239 UNROLL_FILTER(OP_SUB);
242 case PNG_FILTER_VALUE_UP:
243 dsp->add_bytes_l2(dst, src, last, size);
245 case PNG_FILTER_VALUE_AVG:
246 for(i = 0; i < bpp; i++) {
250 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
251 UNROLL_FILTER(OP_AVG);
253 case PNG_FILTER_VALUE_PAETH:
254 for(i = 0; i < bpp; i++) {
258 if(bpp > 2 && size > 4) {
259 // would write off the end of the array if we let it process the last pixel with bpp=3
260 int w = bpp==4 ? size : size-3;
261 dsp->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
264 ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
269 /* This used to be called "deloco" in FFmpeg
270 * and is actually an inverse reversible colorspace transformation */
271 #define YUV2RGB(NAME, TYPE) \
272 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
275 for (i = 0; i < size; i += 3 + alpha) { \
282 YUV2RGB(rgb8, uint8_t)
283 YUV2RGB(rgb16, uint16_t)
285 /* process exactly one decompressed row */
286 static void png_handle_row(PNGDecContext *s)
288 uint8_t *ptr, *last_row;
291 if (!s->interlace_type) {
292 ptr = s->image_buf + s->image_linesize * s->y;
294 last_row = s->last_row;
296 last_row = ptr - s->image_linesize;
298 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
299 last_row, s->row_size, s->bpp);
300 /* loco lags by 1 row so that it doesn't interfere with top prediction */
301 if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
302 if (s->bit_depth == 16) {
303 deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
304 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
306 deloco_rgb8(ptr - s->image_linesize, s->row_size,
307 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
311 if (s->y == s->height) {
312 s->state |= PNG_ALLIMAGE;
313 if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
314 if (s->bit_depth == 16) {
315 deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
316 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
318 deloco_rgb8(ptr, s->row_size,
319 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
326 ptr = s->image_buf + s->image_linesize * s->y;
327 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
328 /* if we already read one row, it is time to stop to
329 wait for the next one */
332 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
333 s->last_row, s->pass_row_size, s->bpp);
334 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
337 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
338 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
339 s->color_type, s->last_row);
342 if (s->y == s->height) {
343 memset(s->last_row, 0, s->row_size);
345 if (s->pass == NB_PASSES - 1) {
346 s->state |= PNG_ALLIMAGE;
351 s->pass_row_size = ff_png_pass_row_size(s->pass,
354 s->crow_size = s->pass_row_size + 1;
355 if (s->pass_row_size != 0)
357 /* skip pass if empty row */
366 static int png_decode_idat(PNGDecContext *s, int length)
369 s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
370 s->zstream.next_in = (unsigned char *)s->gb.buffer;
371 bytestream2_skip(&s->gb, length);
373 /* decode one line if possible */
374 while (s->zstream.avail_in > 0) {
375 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
376 if (ret != Z_OK && ret != Z_STREAM_END) {
377 av_log(s->avctx, AV_LOG_ERROR, "inflate returned %d\n", ret);
380 if (s->zstream.avail_out == 0) {
381 if (!(s->state & PNG_ALLIMAGE)) {
384 s->zstream.avail_out = s->crow_size;
385 s->zstream.next_out = s->crow_buf;
391 static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
392 const uint8_t *data_end)
399 zstream.zalloc = ff_png_zalloc;
400 zstream.zfree = ff_png_zfree;
401 zstream.opaque = NULL;
402 if (inflateInit(&zstream) != Z_OK)
403 return AVERROR_EXTERNAL;
404 zstream.next_in = (unsigned char *)data;
405 zstream.avail_in = data_end - data;
406 av_bprint_init(bp, 0, -1);
408 while (zstream.avail_in > 0) {
409 av_bprint_get_buffer(bp, 1, &buf, &buf_size);
411 ret = AVERROR(ENOMEM);
414 zstream.next_out = buf;
415 zstream.avail_out = buf_size;
416 ret = inflate(&zstream, Z_PARTIAL_FLUSH);
417 if (ret != Z_OK && ret != Z_STREAM_END) {
418 ret = AVERROR_EXTERNAL;
421 bp->len += zstream.next_out - buf;
422 if (ret == Z_STREAM_END)
425 inflateEnd(&zstream);
426 bp->str[bp->len] = 0;
430 inflateEnd(&zstream);
431 av_bprint_finalize(bp, NULL);
435 static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
440 for (i = 0; i < size_in; i++)
441 extra += in[i] >= 0x80;
442 if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
444 q = out = av_malloc(size_in + extra + 1);
447 for (i = 0; i < size_in; i++) {
449 *(q++) = 0xC0 | (in[i] >> 6);
450 *(q++) = 0x80 | (in[i] & 0x3F);
459 static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed,
463 const uint8_t *data = s->gb.buffer;
464 const uint8_t *data_end = data + length;
465 const uint8_t *keyword = data;
466 const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
467 uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
472 return AVERROR_INVALIDDATA;
473 data = keyword_end + 1;
476 if (data == data_end)
477 return AVERROR_INVALIDDATA;
480 return AVERROR_INVALIDDATA;
481 if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
484 av_bprint_finalize(&bp, (char **)&text);
486 return AVERROR(ENOMEM);
488 text = (uint8_t *)data;
489 text_len = data_end - text;
492 kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
493 txt_utf8 = iso88591_to_utf8(text, text_len);
496 if (!(kw_utf8 && txt_utf8)) {
499 return AVERROR(ENOMEM);
502 av_dict_set(dict, kw_utf8, txt_utf8,
503 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
507 static int decode_frame(AVCodecContext *avctx,
508 void *data, int *got_frame,
511 const uint8_t *buf = avpkt->data;
512 int buf_size = avpkt->size;
513 PNGDecContext * const s = avctx->priv_data;
514 AVFrame *picture = data;
516 AVDictionary *metadata = NULL;
517 uint8_t *crow_buf_base = NULL;
518 uint32_t tag, length;
522 FFSWAP(AVFrame *, s->current_picture, s->last_picture);
523 avctx->coded_frame= s->current_picture;
524 p = s->current_picture;
526 bytestream2_init(&s->gb, buf, buf_size);
528 /* check signature */
529 sig = bytestream2_get_be64(&s->gb);
532 av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
538 // memset(s, 0, sizeof(PNGDecContext));
540 s->zstream.zalloc = ff_png_zalloc;
541 s->zstream.zfree = ff_png_zfree;
542 s->zstream.opaque = NULL;
543 ret = inflateInit(&s->zstream);
545 av_log(avctx, AV_LOG_ERROR, "inflateInit returned %d\n", ret);
549 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
550 av_log(avctx, AV_LOG_ERROR, "No bytes left\n");
554 length = bytestream2_get_be32(&s->gb);
555 if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
556 av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
559 tag = bytestream2_get_le32(&s->gb);
560 if (avctx->debug & FF_DEBUG_STARTCODE)
561 av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
564 ((tag >> 16) & 0xff),
565 ((tag >> 24) & 0xff), length);
567 case MKTAG('I', 'H', 'D', 'R'):
570 s->width = bytestream2_get_be32(&s->gb);
571 s->height = bytestream2_get_be32(&s->gb);
572 if(av_image_check_size(s->width, s->height, 0, avctx)){
573 s->width= s->height= 0;
574 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
577 s->bit_depth = bytestream2_get_byte(&s->gb);
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->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 compression_type=%d filter_type=%d interlace_type=%d\n",
586 s->width, s->height, s->bit_depth, s->color_type,
587 s->compression_type, s->filter_type, s->interlace_type);
589 case MKTAG('p', 'H', 'Y', 's'):
590 if (s->state & PNG_IDAT) {
591 av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
594 avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
595 avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
596 if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
597 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
598 bytestream2_skip(&s->gb, 1); /* unit specifier */
599 bytestream2_skip(&s->gb, 4); /* crc */
601 case MKTAG('I', 'D', 'A', 'T'):
602 if (!(s->state & PNG_IHDR)) {
603 av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
606 if (!(s->state & PNG_IDAT)) {
607 /* init image info */
608 avctx->width = s->width;
609 avctx->height = s->height;
611 s->channels = ff_png_get_nb_channels(s->color_type);
612 s->bits_per_pixel = s->bit_depth * s->channels;
613 s->bpp = (s->bits_per_pixel + 7) >> 3;
614 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
616 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
617 s->color_type == PNG_COLOR_TYPE_RGB) {
618 avctx->pix_fmt = AV_PIX_FMT_RGB24;
619 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
620 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
621 avctx->pix_fmt = AV_PIX_FMT_RGBA;
622 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
623 s->color_type == PNG_COLOR_TYPE_GRAY) {
624 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
625 } else if (s->bit_depth == 16 &&
626 s->color_type == PNG_COLOR_TYPE_GRAY) {
627 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
628 } else if (s->bit_depth == 16 &&
629 s->color_type == PNG_COLOR_TYPE_RGB) {
630 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
631 } else if (s->bit_depth == 16 &&
632 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
633 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
634 } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
635 s->color_type == PNG_COLOR_TYPE_PALETTE) {
636 avctx->pix_fmt = AV_PIX_FMT_PAL8;
637 } else if (s->bit_depth == 1) {
638 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
639 } else if (s->bit_depth == 8 &&
640 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
641 avctx->pix_fmt = AV_PIX_FMT_Y400A;
643 av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
644 "and color type %d\n",
645 s->bit_depth, s->color_type);
649 avctx->release_buffer(avctx, p);
652 if(ff_get_buffer(avctx, p) < 0){
653 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
656 p->pict_type= AV_PICTURE_TYPE_I;
658 p->interlaced_frame = !!s->interlace_type;
660 /* compute the compressed row size */
661 if (!s->interlace_type) {
662 s->crow_size = s->row_size + 1;
665 s->pass_row_size = ff_png_pass_row_size(s->pass,
668 s->crow_size = s->pass_row_size + 1;
670 av_dlog(avctx, "row_size=%d crow_size =%d\n",
671 s->row_size, s->crow_size);
672 s->image_buf = p->data[0];
673 s->image_linesize = p->linesize[0];
674 /* copy the palette if needed */
675 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
676 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
677 /* empty row is used if differencing to the first row */
678 s->last_row = av_mallocz(s->row_size);
681 if (s->interlace_type ||
682 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
683 s->tmp_row = av_malloc(s->row_size);
688 crow_buf_base = av_malloc(s->row_size + 16);
692 /* we want crow_buf+1 to be 16-byte aligned */
693 s->crow_buf = crow_buf_base + 15;
694 s->zstream.avail_out = s->crow_size;
695 s->zstream.next_out = s->crow_buf;
697 s->state |= PNG_IDAT;
698 if (png_decode_idat(s, length) < 0)
700 bytestream2_skip(&s->gb, 4); /* crc */
702 case MKTAG('P', 'L', 'T', 'E'):
706 if ((length % 3) != 0 || length > 256 * 3)
708 /* read the palette */
711 r = bytestream2_get_byte(&s->gb);
712 g = bytestream2_get_byte(&s->gb);
713 b = bytestream2_get_byte(&s->gb);
714 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
717 s->palette[i] = (0xFFU << 24);
719 s->state |= PNG_PLTE;
720 bytestream2_skip(&s->gb, 4); /* crc */
723 case MKTAG('t', 'R', 'N', 'S'):
727 /* read the transparency. XXX: Only palette mode supported */
728 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
730 !(s->state & PNG_PLTE))
732 for(i=0;i<length;i++) {
733 v = bytestream2_get_byte(&s->gb);
734 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
736 bytestream2_skip(&s->gb, 4); /* crc */
739 case MKTAG('t', 'E', 'X', 't'):
740 if (decode_text_chunk(s, length, 0, &metadata) < 0)
741 av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
742 bytestream2_skip(&s->gb, length + 4);
744 case MKTAG('z', 'T', 'X', 't'):
745 if (decode_text_chunk(s, length, 1, &metadata) < 0)
746 av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
747 bytestream2_skip(&s->gb, length + 4);
749 case MKTAG('I', 'E', 'N', 'D'):
750 if (!(s->state & PNG_ALLIMAGE))
751 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
752 if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
755 bytestream2_skip(&s->gb, 4); /* crc */
760 bytestream2_skip(&s->gb, length + 4);
766 if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
768 uint8_t *pd = s->current_picture->data[0];
769 for(j=0; j < s->height; j++) {
770 for(i=s->width/8-1; i>=0; i--) {
772 pd[8*i+6]= (pd[i]>>1)&1;
773 pd[8*i+5]= (pd[i]>>2)&1;
774 pd[8*i+4]= (pd[i]>>3)&1;
775 pd[8*i+3]= (pd[i]>>4)&1;
776 pd[8*i+2]= (pd[i]>>5)&1;
777 pd[8*i+1]= (pd[i]>>6)&1;
780 pd += s->image_linesize;
783 if(s->bits_per_pixel == 2){
785 uint8_t *pd = s->current_picture->data[0];
786 for(j=0; j < s->height; j++) {
788 if (s->color_type == PNG_COLOR_TYPE_PALETTE){
789 if ((s->width&3) >= 3) pd[4*i+2]= (pd[i]>>2)&3;
790 if ((s->width&3) >= 2) pd[4*i+1]= (pd[i]>>4)&3;
791 if ((s->width&3) >= 1) pd[4*i+0]= pd[i]>>6;
792 for(i--; i>=0; i--) {
794 pd[4*i+2]= (pd[i]>>2)&3;
795 pd[4*i+1]= (pd[i]>>4)&3;
799 if ((s->width&3) >= 3) pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
800 if ((s->width&3) >= 2) pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
801 if ((s->width&3) >= 1) pd[4*i+0]= ( pd[i]>>6 )*0x55;
802 for(i--; i>=0; i--) {
803 pd[4*i+3]= ( pd[i] &3)*0x55;
804 pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
805 pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
806 pd[4*i+0]= ( pd[i]>>6 )*0x55;
809 pd += s->image_linesize;
812 if(s->bits_per_pixel == 4){
814 uint8_t *pd = s->current_picture->data[0];
815 for(j=0; j < s->height; j++) {
817 if (s->color_type == PNG_COLOR_TYPE_PALETTE){
818 if (s->width&1) pd[2*i+0]= pd[i]>>4;
819 for(i--; i>=0; i--) {
824 if (s->width&1) pd[2*i+0]= (pd[i]>>4)*0x11;
825 for(i--; i>=0; i--) {
826 pd[2*i+1]= (pd[i]&15)*0x11;
827 pd[2*i+0]= (pd[i]>>4)*0x11;
830 pd += s->image_linesize;
834 /* handle p-frames only if a predecessor frame is available */
835 if(s->last_picture->data[0] != NULL) {
836 if( !(avpkt->flags & AV_PKT_FLAG_KEY)
837 && s->last_picture->width == s->current_picture->width
838 && s->last_picture->height== s->current_picture->height
839 && s->last_picture->format== s->current_picture->format
842 uint8_t *pd = s->current_picture->data[0];
843 uint8_t *pd_last = s->last_picture->data[0];
845 for(j=0; j < s->height; j++) {
846 for(i=0; i < s->width * s->bpp; i++) {
849 pd += s->image_linesize;
850 pd_last += s->image_linesize;
855 s->current_picture->metadata = metadata;
857 *picture= *s->current_picture;
860 ret = bytestream2_tell(&s->gb);
862 inflateEnd(&s->zstream);
863 av_free(crow_buf_base);
865 av_freep(&s->last_row);
866 av_freep(&s->tmp_row);
869 av_dict_free(&metadata);
874 static av_cold int png_dec_init(AVCodecContext *avctx)
876 PNGDecContext *s = avctx->priv_data;
878 s->current_picture = &s->picture1;
879 s->last_picture = &s->picture2;
880 avcodec_get_frame_defaults(&s->picture1);
881 avcodec_get_frame_defaults(&s->picture2);
883 ff_pngdsp_init(&s->dsp);
890 static av_cold int png_dec_end(AVCodecContext *avctx)
892 PNGDecContext *s = avctx->priv_data;
894 if (s->picture1.data[0])
895 avctx->release_buffer(avctx, &s->picture1);
896 if (s->picture2.data[0])
897 avctx->release_buffer(avctx, &s->picture2);
902 AVCodec ff_png_decoder = {
904 .type = AVMEDIA_TYPE_VIDEO,
905 .id = AV_CODEC_ID_PNG,
906 .priv_data_size = sizeof(PNGDecContext),
907 .init = png_dec_init,
908 .close = png_dec_end,
909 .decode = decode_frame,
910 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
911 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),