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 y pixels can be written in a pass */
71 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
72 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
75 /* Mask to determine which pixels to overwrite while displaying */
76 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
77 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
80 /* NOTE: we try to construct a good looking image at each pass. width
81 is the original image width. We also do pixel format conversion at
83 static void png_put_interlaced_row(uint8_t *dst, int width,
84 int bits_per_pixel, int pass,
85 int color_type, const uint8_t *src)
87 int x, mask, dsp_mask, j, src_x, b, bpp;
91 mask = ff_png_pass_mask[pass];
92 dsp_mask = png_pass_dsp_mask[pass];
93 switch(bits_per_pixel) {
96 for(x = 0; x < width; x++) {
98 if ((dsp_mask << j) & 0x80) {
99 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
100 dst[x >> 3] &= 0xFF7F>>j;
101 dst[x >> 3] |= b << (7 - j);
103 if ((mask << j) & 0x80)
109 for(x = 0; x < width; x++) {
112 if ((dsp_mask << j) & 0x80) {
113 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
114 dst[x >> 2] &= 0xFF3F>>j2;
115 dst[x >> 2] |= b << (6 - j2);
117 if ((mask << j) & 0x80)
123 for(x = 0; x < width; x++) {
126 if ((dsp_mask << j) & 0x80) {
127 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
128 dst[x >> 1] &= 0xFF0F>>j2;
129 dst[x >> 1] |= b << (4 - j2);
131 if ((mask << j) & 0x80)
136 bpp = bits_per_pixel >> 3;
139 for(x = 0; x < width; x++) {
141 if ((dsp_mask << j) & 0x80) {
145 if ((mask << j) & 0x80)
152 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
155 for(i = 0; i < w; i++) {
156 int a, b, c, p, pa, pb, pc;
169 if (pa <= pb && pa <= pc)
179 #define UNROLL1(bpp, op) {\
181 if(bpp >= 2) g = dst[1];\
182 if(bpp >= 3) b = dst[2];\
183 if(bpp >= 4) a = dst[3];\
184 for(; i < size; i+=bpp) {\
185 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
186 if(bpp == 1) continue;\
187 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
188 if(bpp == 2) continue;\
189 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
190 if(bpp == 3) continue;\
191 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
195 #define UNROLL_FILTER(op)\
196 if(bpp == 1) UNROLL1(1, op)\
197 else if(bpp == 2) UNROLL1(2, op)\
198 else if(bpp == 3) UNROLL1(3, op)\
199 else if(bpp == 4) UNROLL1(4, op)\
201 for (; i < size; i += bpp) {\
203 for (j = 0; j < bpp; j++)\
204 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
208 /* NOTE: 'dst' can be equal to 'last' */
209 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
210 uint8_t *src, uint8_t *last, int size, int bpp)
212 int i, p, r, g, b, a;
214 switch(filter_type) {
215 case PNG_FILTER_VALUE_NONE:
216 memcpy(dst, src, size);
218 case PNG_FILTER_VALUE_SUB:
219 for(i = 0; i < bpp; i++) {
224 for(; i < size; i+=bpp) {
225 int s = *(int*)(src+i);
226 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
230 #define OP_SUB(x,s,l) x+s
231 UNROLL_FILTER(OP_SUB);
234 case PNG_FILTER_VALUE_UP:
235 dsp->add_bytes_l2(dst, src, last, size);
237 case PNG_FILTER_VALUE_AVG:
238 for(i = 0; i < bpp; i++) {
242 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
243 UNROLL_FILTER(OP_AVG);
245 case PNG_FILTER_VALUE_PAETH:
246 for(i = 0; i < bpp; i++) {
250 if(bpp > 2 && size > 4) {
251 // would write off the end of the array if we let it process the last pixel with bpp=3
252 int w = bpp==4 ? size : size-3;
253 dsp->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
256 ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
261 /* This used to be called "deloco" in FFmpeg
262 * and is actually an inverse reversible colorspace transformation */
263 #define YUV2RGB(NAME, TYPE) \
264 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
267 for (i = 0; i < size; i += 3 + alpha) { \
274 YUV2RGB(rgb8, uint8_t)
275 YUV2RGB(rgb16, uint16_t)
277 /* process exactly one decompressed row */
278 static void png_handle_row(PNGDecContext *s)
280 uint8_t *ptr, *last_row;
283 if (!s->interlace_type) {
284 ptr = s->image_buf + s->image_linesize * s->y;
286 last_row = s->last_row;
288 last_row = ptr - s->image_linesize;
290 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
291 last_row, s->row_size, s->bpp);
292 /* loco lags by 1 row so that it doesn't interfere with top prediction */
293 if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
294 if (s->bit_depth == 16) {
295 deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
296 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
298 deloco_rgb8(ptr - s->image_linesize, s->row_size,
299 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
303 if (s->y == s->height) {
304 s->state |= PNG_ALLIMAGE;
305 if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
306 if (s->bit_depth == 16) {
307 deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
308 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
310 deloco_rgb8(ptr, s->row_size,
311 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
318 ptr = s->image_buf + s->image_linesize * s->y;
319 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
320 /* if we already read one row, it is time to stop to
321 wait for the next one */
324 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
325 s->last_row, s->pass_row_size, s->bpp);
326 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
329 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
330 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
331 s->color_type, s->last_row);
334 if (s->y == s->height) {
335 memset(s->last_row, 0, s->row_size);
337 if (s->pass == NB_PASSES - 1) {
338 s->state |= PNG_ALLIMAGE;
343 s->pass_row_size = ff_png_pass_row_size(s->pass,
346 s->crow_size = s->pass_row_size + 1;
347 if (s->pass_row_size != 0)
349 /* skip pass if empty row */
358 static int png_decode_idat(PNGDecContext *s, int length)
361 s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
362 s->zstream.next_in = s->gb.buffer;
363 bytestream2_skip(&s->gb, length);
365 /* decode one line if possible */
366 while (s->zstream.avail_in > 0) {
367 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
368 if (ret != Z_OK && ret != Z_STREAM_END) {
371 if (s->zstream.avail_out == 0) {
372 if (!(s->state & PNG_ALLIMAGE)) {
375 s->zstream.avail_out = s->crow_size;
376 s->zstream.next_out = s->crow_buf;
382 static int decode_frame(AVCodecContext *avctx,
383 void *data, int *data_size,
386 const uint8_t *buf = avpkt->data;
387 int buf_size = avpkt->size;
388 PNGDecContext * const s = avctx->priv_data;
389 AVFrame *picture = data;
391 uint8_t *crow_buf_base = NULL;
392 uint32_t tag, length;
395 FFSWAP(AVFrame *, s->current_picture, s->last_picture);
396 avctx->coded_frame= s->current_picture;
397 p = s->current_picture;
399 /* check signature */
401 memcmp(buf, ff_pngsig, 8) != 0 &&
402 memcmp(buf, ff_mngsig, 8) != 0) {
403 av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
407 bytestream2_init(&s->gb, buf + 8, buf_size - 8);
410 // memset(s, 0, sizeof(PNGDecContext));
412 s->zstream.zalloc = ff_png_zalloc;
413 s->zstream.zfree = ff_png_zfree;
414 s->zstream.opaque = NULL;
415 ret = inflateInit(&s->zstream);
419 if (bytestream2_get_bytes_left(&s->gb) <= 0)
422 length = bytestream2_get_be32(&s->gb);
423 if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb))
425 tag = bytestream2_get_le32(&s->gb);
426 if (avctx->debug & FF_DEBUG_STARTCODE)
427 av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
430 ((tag >> 16) & 0xff),
431 ((tag >> 24) & 0xff), length);
433 case MKTAG('I', 'H', 'D', 'R'):
436 s->width = bytestream2_get_be32(&s->gb);
437 s->height = bytestream2_get_be32(&s->gb);
438 if(av_image_check_size(s->width, s->height, 0, avctx)){
439 s->width= s->height= 0;
442 s->bit_depth = bytestream2_get_byte(&s->gb);
443 s->color_type = bytestream2_get_byte(&s->gb);
444 s->compression_type = bytestream2_get_byte(&s->gb);
445 s->filter_type = bytestream2_get_byte(&s->gb);
446 s->interlace_type = bytestream2_get_byte(&s->gb);
447 bytestream2_skip(&s->gb, 4); /* crc */
448 s->state |= PNG_IHDR;
449 if (avctx->debug & FF_DEBUG_PICT_INFO)
450 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",
451 s->width, s->height, s->bit_depth, s->color_type,
452 s->compression_type, s->filter_type, s->interlace_type);
454 case MKTAG('I', 'D', 'A', 'T'):
455 if (!(s->state & PNG_IHDR))
457 if (!(s->state & PNG_IDAT)) {
458 /* init image info */
459 avctx->width = s->width;
460 avctx->height = s->height;
462 s->channels = ff_png_get_nb_channels(s->color_type);
463 s->bits_per_pixel = s->bit_depth * s->channels;
464 s->bpp = (s->bits_per_pixel + 7) >> 3;
465 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
467 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
468 s->color_type == PNG_COLOR_TYPE_RGB) {
469 avctx->pix_fmt = PIX_FMT_RGB24;
470 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
471 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
472 avctx->pix_fmt = PIX_FMT_RGBA;
473 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
474 s->color_type == PNG_COLOR_TYPE_GRAY) {
475 avctx->pix_fmt = PIX_FMT_GRAY8;
476 } else if (s->bit_depth == 16 &&
477 s->color_type == PNG_COLOR_TYPE_GRAY) {
478 avctx->pix_fmt = PIX_FMT_GRAY16BE;
479 } else if (s->bit_depth == 16 &&
480 s->color_type == PNG_COLOR_TYPE_RGB) {
481 avctx->pix_fmt = PIX_FMT_RGB48BE;
482 } else if (s->bit_depth == 16 &&
483 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
484 avctx->pix_fmt = PIX_FMT_RGBA64BE;
485 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
486 avctx->pix_fmt = PIX_FMT_PAL8;
487 } else if (s->bit_depth == 1) {
488 avctx->pix_fmt = PIX_FMT_MONOBLACK;
489 } else if (s->bit_depth == 8 &&
490 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
491 avctx->pix_fmt = PIX_FMT_GRAY8A;
493 av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
494 "and color type %d\n",
495 s->bit_depth, s->color_type);
499 avctx->release_buffer(avctx, p);
502 if(avctx->get_buffer(avctx, p) < 0){
503 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
506 p->pict_type= AV_PICTURE_TYPE_I;
508 p->interlaced_frame = !!s->interlace_type;
510 /* compute the compressed row size */
511 if (!s->interlace_type) {
512 s->crow_size = s->row_size + 1;
515 s->pass_row_size = ff_png_pass_row_size(s->pass,
518 s->crow_size = s->pass_row_size + 1;
520 av_dlog(avctx, "row_size=%d crow_size =%d\n",
521 s->row_size, s->crow_size);
522 s->image_buf = p->data[0];
523 s->image_linesize = p->linesize[0];
524 /* copy the palette if needed */
525 if (avctx->pix_fmt == PIX_FMT_PAL8)
526 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
527 /* empty row is used if differencing to the first row */
528 s->last_row = av_mallocz(s->row_size);
531 if (s->interlace_type ||
532 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
533 s->tmp_row = av_malloc(s->row_size);
538 crow_buf_base = av_malloc(s->row_size + 16);
542 /* we want crow_buf+1 to be 16-byte aligned */
543 s->crow_buf = crow_buf_base + 15;
544 s->zstream.avail_out = s->crow_size;
545 s->zstream.next_out = s->crow_buf;
547 s->state |= PNG_IDAT;
548 if (png_decode_idat(s, length) < 0)
550 bytestream2_skip(&s->gb, 4); /* crc */
552 case MKTAG('P', 'L', 'T', 'E'):
556 if ((length % 3) != 0 || length > 256 * 3)
558 /* read the palette */
561 r = bytestream2_get_byte(&s->gb);
562 g = bytestream2_get_byte(&s->gb);
563 b = bytestream2_get_byte(&s->gb);
564 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
567 s->palette[i] = (0xff << 24);
569 s->state |= PNG_PLTE;
570 bytestream2_skip(&s->gb, 4); /* crc */
573 case MKTAG('t', 'R', 'N', 'S'):
577 /* read the transparency. XXX: Only palette mode supported */
578 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
580 !(s->state & PNG_PLTE))
582 for(i=0;i<length;i++) {
583 v = bytestream2_get_byte(&s->gb);
584 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
586 bytestream2_skip(&s->gb, 4); /* crc */
589 case MKTAG('I', 'E', 'N', 'D'):
590 if (!(s->state & PNG_ALLIMAGE))
592 bytestream2_skip(&s->gb, 4); /* crc */
597 bytestream2_skip(&s->gb, length + 4);
603 if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
605 uint8_t *pd = s->current_picture->data[0];
606 for(j=0; j < s->height; j++) {
607 for(i=s->width/8-1; i>=0; i--) {
609 pd[8*i+6]= (pd[i]>>1)&1;
610 pd[8*i+5]= (pd[i]>>2)&1;
611 pd[8*i+4]= (pd[i]>>3)&1;
612 pd[8*i+3]= (pd[i]>>4)&1;
613 pd[8*i+2]= (pd[i]>>5)&1;
614 pd[8*i+1]= (pd[i]>>6)&1;
617 pd += s->image_linesize;
620 if(s->bits_per_pixel == 2){
622 uint8_t *pd = s->current_picture->data[0];
623 for(j=0; j < s->height; j++) {
624 if (s->color_type == PNG_COLOR_TYPE_PALETTE){
625 for(i=s->width/4-1; i>=0; i--) {
627 pd[4*i+2]= (pd[i]>>2)&3;
628 pd[4*i+1]= (pd[i]>>4)&3;
632 for(i=s->width/4-1; i>=0; i--) {
633 pd[4*i+3]= ( pd[i] &3)*0x55;
634 pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
635 pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
636 pd[4*i+0]= ( pd[i]>>6 )*0x55;
639 pd += s->image_linesize;
642 if(s->bits_per_pixel == 4){
644 uint8_t *pd = s->current_picture->data[0];
645 for(j=0; j < s->height; j++) {
646 if (s->color_type == PNG_COLOR_TYPE_PALETTE){
647 for(i=s->width/2-1; i>=0; i--) {
652 for(i=s->width/2-1; i>=0; i--) {
653 pd[2*i+1]= (pd[i]&15)*0x11;
654 pd[2*i+0]= (pd[i]>>4)*0x11;
657 pd += s->image_linesize;
661 /* handle p-frames only if a predecessor frame is available */
662 if(s->last_picture->data[0] != NULL) {
663 if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
665 uint8_t *pd = s->current_picture->data[0];
666 uint8_t *pd_last = s->last_picture->data[0];
668 for(j=0; j < s->height; j++) {
669 for(i=0; i < s->width * s->bpp; i++) {
672 pd += s->image_linesize;
673 pd_last += s->image_linesize;
678 *picture= *s->current_picture;
679 *data_size = sizeof(AVFrame);
681 ret = bytestream2_tell(&s->gb);
683 inflateEnd(&s->zstream);
684 av_free(crow_buf_base);
686 av_freep(&s->last_row);
687 av_freep(&s->tmp_row);
694 static av_cold int png_dec_init(AVCodecContext *avctx)
696 PNGDecContext *s = avctx->priv_data;
698 s->current_picture = &s->picture1;
699 s->last_picture = &s->picture2;
700 avcodec_get_frame_defaults(&s->picture1);
701 avcodec_get_frame_defaults(&s->picture2);
703 ff_pngdsp_init(&s->dsp);
708 static av_cold int png_dec_end(AVCodecContext *avctx)
710 PNGDecContext *s = avctx->priv_data;
712 if (s->picture1.data[0])
713 avctx->release_buffer(avctx, &s->picture1);
714 if (s->picture2.data[0])
715 avctx->release_buffer(avctx, &s->picture2);
720 AVCodec ff_png_decoder = {
722 .type = AVMEDIA_TYPE_VIDEO,
724 .priv_data_size = sizeof(PNGDecContext),
725 .init = png_dec_init,
726 .close = png_dec_end,
727 .decode = decode_frame,
728 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
729 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),