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/imgutils.h"
26 #include "bytestream.h"
31 * - add 16 bit depth support
38 typedef struct PNGDecContext {
42 AVFrame picture1, picture2;
43 AVFrame *current_picture, *last_picture;
58 uint32_t palette[256];
63 int crow_size; /* compressed row size (include filter type) */
64 int row_size; /* decompressed row size */
65 int pass_row_size; /* decompress row size of the current pass */
70 /* Mask to determine which pixels are valid in a pass */
71 static const uint8_t png_pass_mask[NB_PASSES] = {
72 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
75 /* Mask to determine which y pixels can be written in a pass */
76 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
77 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
80 /* Mask to determine which pixels to overwrite while displaying */
81 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
82 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
85 /* NOTE: we try to construct a good looking image at each pass. width
86 is the original image width. We also do pixel format conversion at
88 static void png_put_interlaced_row(uint8_t *dst, int width,
89 int bits_per_pixel, int pass,
90 int color_type, const uint8_t *src)
92 int x, mask, dsp_mask, j, src_x, b, bpp;
96 mask = png_pass_mask[pass];
97 dsp_mask = png_pass_dsp_mask[pass];
98 switch(bits_per_pixel) {
101 for(x = 0; x < width; x++) {
103 if ((dsp_mask << j) & 0x80) {
104 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
105 dst[x >> 3] &= 0xFF7F>>j;
106 dst[x >> 3] |= b << (7 - j);
108 if ((mask << j) & 0x80)
114 for(x = 0; x < width; x++) {
117 if ((dsp_mask << j) & 0x80) {
118 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
119 dst[x >> 2] &= 0xFF3F>>j2;
120 dst[x >> 2] |= b << (6 - j2);
122 if ((mask << j) & 0x80)
128 for(x = 0; x < width; x++) {
131 if ((dsp_mask << j) & 0x80) {
132 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
133 dst[x >> 1] &= 0xFF0F>>j2;
134 dst[x >> 1] |= b << (4 - j2);
136 if ((mask << j) & 0x80)
141 bpp = bits_per_pixel >> 3;
144 for(x = 0; x < width; x++) {
146 if ((dsp_mask << j) & 0x80) {
150 if ((mask << j) & 0x80)
157 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
160 for(i = 0; i < w; i++) {
161 int a, b, c, p, pa, pb, pc;
174 if (pa <= pb && pa <= pc)
184 #define UNROLL1(bpp, op) {\
186 if(bpp >= 2) g = dst[1];\
187 if(bpp >= 3) b = dst[2];\
188 if(bpp >= 4) a = dst[3];\
189 for(; i < size; i+=bpp) {\
190 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
191 if(bpp == 1) continue;\
192 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
193 if(bpp == 2) continue;\
194 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
195 if(bpp == 3) continue;\
196 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
200 #define UNROLL_FILTER(op)\
201 if(bpp == 1) UNROLL1(1, op)\
202 else if(bpp == 2) UNROLL1(2, op)\
203 else if(bpp == 3) UNROLL1(3, op)\
204 else if(bpp == 4) UNROLL1(4, op)\
206 for (; i < size; i += bpp) {\
208 for (j = 0; j < bpp; j++)\
209 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
213 /* NOTE: 'dst' can be equal to 'last' */
214 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
215 uint8_t *src, uint8_t *last, int size, int bpp)
217 int i, p, r, g, b, a;
219 switch(filter_type) {
220 case PNG_FILTER_VALUE_NONE:
221 memcpy(dst, src, size);
223 case PNG_FILTER_VALUE_SUB:
224 for(i = 0; i < bpp; i++) {
229 for(; i < size; i+=bpp) {
230 int s = *(int*)(src+i);
231 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
235 #define OP_SUB(x,s,l) x+s
236 UNROLL_FILTER(OP_SUB);
239 case PNG_FILTER_VALUE_UP:
240 dsp->add_bytes_l2(dst, src, last, size);
242 case PNG_FILTER_VALUE_AVG:
243 for(i = 0; i < bpp; i++) {
247 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
248 UNROLL_FILTER(OP_AVG);
250 case PNG_FILTER_VALUE_PAETH:
251 for(i = 0; i < bpp; i++) {
255 if(bpp > 2 && size > 4) {
256 // would write off the end of the array if we let it process the last pixel with bpp=3
257 int w = bpp==4 ? size : size-3;
258 dsp->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
261 ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
266 /* This used to be called "deloco" in FFmpeg
267 * and is actually an inverse reversible colorspace transformation */
268 #define YUV2RGB(NAME, TYPE) \
269 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
272 for (i = 0; i < size; i += 3 + alpha) { \
279 YUV2RGB(rgb8, uint8_t)
280 YUV2RGB(rgb16, uint16_t)
282 /* process exactly one decompressed row */
283 static void png_handle_row(PNGDecContext *s)
285 uint8_t *ptr, *last_row;
288 if (!s->interlace_type) {
289 ptr = s->image_buf + s->image_linesize * s->y;
291 last_row = s->last_row;
293 last_row = ptr - s->image_linesize;
295 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
296 last_row, s->row_size, s->bpp);
297 /* loco lags by 1 row so that it doesn't interfere with top prediction */
298 if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
299 if (s->bit_depth == 16) {
300 deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
301 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
303 deloco_rgb8(ptr - s->image_linesize, s->row_size,
304 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
308 if (s->y == s->height) {
309 s->state |= PNG_ALLIMAGE;
310 if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
311 if (s->bit_depth == 16) {
312 deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
313 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
315 deloco_rgb8(ptr, s->row_size,
316 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
323 ptr = s->image_buf + s->image_linesize * s->y;
324 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
325 /* if we already read one row, it is time to stop to
326 wait for the next one */
329 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
330 s->last_row, s->pass_row_size, s->bpp);
331 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
334 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
335 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
336 s->color_type, s->last_row);
339 if (s->y == s->height) {
340 memset(s->last_row, 0, s->row_size);
342 if (s->pass == NB_PASSES - 1) {
343 s->state |= PNG_ALLIMAGE;
348 s->pass_row_size = ff_png_pass_row_size(s->pass,
351 s->crow_size = s->pass_row_size + 1;
352 if (s->pass_row_size != 0)
354 /* skip pass if empty row */
363 static int png_decode_idat(PNGDecContext *s, int length)
366 s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
367 s->zstream.next_in = (unsigned char *)s->gb.buffer;
368 bytestream2_skip(&s->gb, length);
370 /* decode one line if possible */
371 while (s->zstream.avail_in > 0) {
372 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
373 if (ret != Z_OK && ret != Z_STREAM_END) {
376 if (s->zstream.avail_out == 0) {
377 if (!(s->state & PNG_ALLIMAGE)) {
380 s->zstream.avail_out = s->crow_size;
381 s->zstream.next_out = s->crow_buf;
387 static int decode_frame(AVCodecContext *avctx,
388 void *data, int *data_size,
391 const uint8_t *buf = avpkt->data;
392 int buf_size = avpkt->size;
393 PNGDecContext * const s = avctx->priv_data;
394 AVFrame *picture = data;
396 uint8_t *crow_buf_base = NULL;
397 uint32_t tag, length;
401 FFSWAP(AVFrame *, s->current_picture, s->last_picture);
402 avctx->coded_frame= s->current_picture;
403 p = s->current_picture;
405 bytestream2_init(&s->gb, buf, buf_size);
407 /* check signature */
408 sig = bytestream2_get_be64(&s->gb);
411 av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
417 // memset(s, 0, sizeof(PNGDecContext));
419 s->zstream.zalloc = ff_png_zalloc;
420 s->zstream.zfree = ff_png_zfree;
421 s->zstream.opaque = NULL;
422 ret = inflateInit(&s->zstream);
426 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
427 av_log(avctx, AV_LOG_ERROR, "No bytes left\n");
431 length = bytestream2_get_be32(&s->gb);
432 if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
433 av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
436 tag = bytestream2_get_le32(&s->gb);
437 if (avctx->debug & FF_DEBUG_STARTCODE)
438 av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
441 ((tag >> 16) & 0xff),
442 ((tag >> 24) & 0xff), length);
444 case MKTAG('I', 'H', 'D', 'R'):
447 s->width = bytestream2_get_be32(&s->gb);
448 s->height = bytestream2_get_be32(&s->gb);
449 if(av_image_check_size(s->width, s->height, 0, avctx)){
450 s->width= s->height= 0;
451 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
454 s->bit_depth = bytestream2_get_byte(&s->gb);
455 s->color_type = bytestream2_get_byte(&s->gb);
456 s->compression_type = bytestream2_get_byte(&s->gb);
457 s->filter_type = bytestream2_get_byte(&s->gb);
458 s->interlace_type = bytestream2_get_byte(&s->gb);
459 bytestream2_skip(&s->gb, 4); /* crc */
460 s->state |= PNG_IHDR;
461 if (avctx->debug & FF_DEBUG_PICT_INFO)
462 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",
463 s->width, s->height, s->bit_depth, s->color_type,
464 s->compression_type, s->filter_type, s->interlace_type);
466 case MKTAG('I', 'D', 'A', 'T'):
467 if (!(s->state & PNG_IHDR)) {
468 av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
471 if (!(s->state & PNG_IDAT)) {
472 /* init image info */
473 avctx->width = s->width;
474 avctx->height = s->height;
476 s->channels = ff_png_get_nb_channels(s->color_type);
477 s->bits_per_pixel = s->bit_depth * s->channels;
478 s->bpp = (s->bits_per_pixel + 7) >> 3;
479 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
481 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
482 s->color_type == PNG_COLOR_TYPE_RGB) {
483 avctx->pix_fmt = PIX_FMT_RGB24;
484 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
485 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
486 avctx->pix_fmt = PIX_FMT_RGBA;
487 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
488 s->color_type == PNG_COLOR_TYPE_GRAY) {
489 avctx->pix_fmt = PIX_FMT_GRAY8;
490 } else if (s->bit_depth == 16 &&
491 s->color_type == PNG_COLOR_TYPE_GRAY) {
492 avctx->pix_fmt = PIX_FMT_GRAY16BE;
493 } else if (s->bit_depth == 16 &&
494 s->color_type == PNG_COLOR_TYPE_RGB) {
495 avctx->pix_fmt = PIX_FMT_RGB48BE;
496 } else if (s->bit_depth == 16 &&
497 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
498 avctx->pix_fmt = PIX_FMT_RGBA64BE;
499 } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
500 s->color_type == PNG_COLOR_TYPE_PALETTE) {
501 avctx->pix_fmt = PIX_FMT_PAL8;
502 } else if (s->bit_depth == 1) {
503 avctx->pix_fmt = PIX_FMT_MONOBLACK;
504 } else if (s->bit_depth == 8 &&
505 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
506 avctx->pix_fmt = PIX_FMT_Y400A;
508 av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
509 "and color type %d\n",
510 s->bit_depth, s->color_type);
514 avctx->release_buffer(avctx, p);
517 if(avctx->get_buffer(avctx, p) < 0){
518 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
521 p->pict_type= AV_PICTURE_TYPE_I;
523 p->interlaced_frame = !!s->interlace_type;
525 /* compute the compressed row size */
526 if (!s->interlace_type) {
527 s->crow_size = s->row_size + 1;
530 s->pass_row_size = ff_png_pass_row_size(s->pass,
533 s->crow_size = s->pass_row_size + 1;
535 av_dlog(avctx, "row_size=%d crow_size =%d\n",
536 s->row_size, s->crow_size);
537 s->image_buf = p->data[0];
538 s->image_linesize = p->linesize[0];
539 /* copy the palette if needed */
540 if (avctx->pix_fmt == PIX_FMT_PAL8)
541 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
542 /* empty row is used if differencing to the first row */
543 s->last_row = av_mallocz(s->row_size);
546 if (s->interlace_type ||
547 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
548 s->tmp_row = av_malloc(s->row_size);
553 crow_buf_base = av_malloc(s->row_size + 16);
557 /* we want crow_buf+1 to be 16-byte aligned */
558 s->crow_buf = crow_buf_base + 15;
559 s->zstream.avail_out = s->crow_size;
560 s->zstream.next_out = s->crow_buf;
562 s->state |= PNG_IDAT;
563 if (png_decode_idat(s, length) < 0)
565 bytestream2_skip(&s->gb, 4); /* crc */
567 case MKTAG('P', 'L', 'T', 'E'):
571 if ((length % 3) != 0 || length > 256 * 3)
573 /* read the palette */
576 r = bytestream2_get_byte(&s->gb);
577 g = bytestream2_get_byte(&s->gb);
578 b = bytestream2_get_byte(&s->gb);
579 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
582 s->palette[i] = (0xff << 24);
584 s->state |= PNG_PLTE;
585 bytestream2_skip(&s->gb, 4); /* crc */
588 case MKTAG('t', 'R', 'N', 'S'):
592 /* read the transparency. XXX: Only palette mode supported */
593 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
595 !(s->state & PNG_PLTE))
597 for(i=0;i<length;i++) {
598 v = bytestream2_get_byte(&s->gb);
599 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
601 bytestream2_skip(&s->gb, 4); /* crc */
604 case MKTAG('I', 'E', 'N', 'D'):
605 if (!(s->state & PNG_ALLIMAGE))
606 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
607 if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
610 bytestream2_skip(&s->gb, 4); /* crc */
615 bytestream2_skip(&s->gb, length + 4);
621 if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
623 uint8_t *pd = s->current_picture->data[0];
624 for(j=0; j < s->height; j++) {
625 for(i=s->width/8-1; i>=0; i--) {
627 pd[8*i+6]= (pd[i]>>1)&1;
628 pd[8*i+5]= (pd[i]>>2)&1;
629 pd[8*i+4]= (pd[i]>>3)&1;
630 pd[8*i+3]= (pd[i]>>4)&1;
631 pd[8*i+2]= (pd[i]>>5)&1;
632 pd[8*i+1]= (pd[i]>>6)&1;
635 pd += s->image_linesize;
638 if(s->bits_per_pixel == 2){
640 uint8_t *pd = s->current_picture->data[0];
641 for(j=0; j < s->height; j++) {
642 if (s->color_type == PNG_COLOR_TYPE_PALETTE){
643 for(i=s->width/4-1; i>=0; i--) {
645 pd[4*i+2]= (pd[i]>>2)&3;
646 pd[4*i+1]= (pd[i]>>4)&3;
650 for(i=s->width/4-1; i>=0; i--) {
651 pd[4*i+3]= ( pd[i] &3)*0x55;
652 pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
653 pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
654 pd[4*i+0]= ( pd[i]>>6 )*0x55;
657 pd += s->image_linesize;
660 if(s->bits_per_pixel == 4){
662 uint8_t *pd = s->current_picture->data[0];
663 for(j=0; j < s->height; j++) {
664 if (s->color_type == PNG_COLOR_TYPE_PALETTE){
665 for(i=s->width/2-1; i>=0; i--) {
670 for(i=s->width/2-1; i>=0; i--) {
671 pd[2*i+1]= (pd[i]&15)*0x11;
672 pd[2*i+0]= (pd[i]>>4)*0x11;
675 pd += s->image_linesize;
679 /* handle p-frames only if a predecessor frame is available */
680 if(s->last_picture->data[0] != NULL) {
681 if( !(avpkt->flags & AV_PKT_FLAG_KEY)
682 && s->last_picture->width == s->current_picture->width
683 && s->last_picture->height== s->current_picture->height
686 uint8_t *pd = s->current_picture->data[0];
687 uint8_t *pd_last = s->last_picture->data[0];
689 for(j=0; j < s->height; j++) {
690 for(i=0; i < s->width * s->bpp; i++) {
693 pd += s->image_linesize;
694 pd_last += s->image_linesize;
699 *picture= *s->current_picture;
700 *data_size = sizeof(AVFrame);
702 ret = bytestream2_tell(&s->gb);
704 inflateEnd(&s->zstream);
705 av_free(crow_buf_base);
707 av_freep(&s->last_row);
708 av_freep(&s->tmp_row);
715 static av_cold int png_dec_init(AVCodecContext *avctx)
717 PNGDecContext *s = avctx->priv_data;
719 s->current_picture = &s->picture1;
720 s->last_picture = &s->picture2;
721 avcodec_get_frame_defaults(&s->picture1);
722 avcodec_get_frame_defaults(&s->picture2);
724 ff_pngdsp_init(&s->dsp);
729 static av_cold int png_dec_end(AVCodecContext *avctx)
731 PNGDecContext *s = avctx->priv_data;
733 if (s->picture1.data[0])
734 avctx->release_buffer(avctx, &s->picture1);
735 if (s->picture2.data[0])
736 avctx->release_buffer(avctx, &s->picture2);
741 AVCodec ff_png_decoder = {
743 .type = AVMEDIA_TYPE_VIDEO,
745 .priv_data_size = sizeof(PNGDecContext),
746 .init = png_dec_init,
747 .close = png_dec_end,
748 .decode = decode_frame,
749 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
750 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),