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) {
420 av_log(avctx, AV_LOG_ERROR, "No bytes left\n");
424 length = bytestream2_get_be32(&s->gb);
425 if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
426 av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
429 tag = bytestream2_get_le32(&s->gb);
430 if (avctx->debug & FF_DEBUG_STARTCODE)
431 av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
434 ((tag >> 16) & 0xff),
435 ((tag >> 24) & 0xff), length);
437 case MKTAG('I', 'H', 'D', 'R'):
440 s->width = bytestream2_get_be32(&s->gb);
441 s->height = bytestream2_get_be32(&s->gb);
442 if(av_image_check_size(s->width, s->height, 0, avctx)){
443 s->width= s->height= 0;
444 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
447 s->bit_depth = bytestream2_get_byte(&s->gb);
448 s->color_type = bytestream2_get_byte(&s->gb);
449 s->compression_type = bytestream2_get_byte(&s->gb);
450 s->filter_type = bytestream2_get_byte(&s->gb);
451 s->interlace_type = bytestream2_get_byte(&s->gb);
452 bytestream2_skip(&s->gb, 4); /* crc */
453 s->state |= PNG_IHDR;
454 if (avctx->debug & FF_DEBUG_PICT_INFO)
455 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",
456 s->width, s->height, s->bit_depth, s->color_type,
457 s->compression_type, s->filter_type, s->interlace_type);
459 case MKTAG('I', 'D', 'A', 'T'):
460 if (!(s->state & PNG_IHDR)) {
461 av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
464 if (!(s->state & PNG_IDAT)) {
465 /* init image info */
466 avctx->width = s->width;
467 avctx->height = s->height;
469 s->channels = ff_png_get_nb_channels(s->color_type);
470 s->bits_per_pixel = s->bit_depth * s->channels;
471 s->bpp = (s->bits_per_pixel + 7) >> 3;
472 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
474 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
475 s->color_type == PNG_COLOR_TYPE_RGB) {
476 avctx->pix_fmt = PIX_FMT_RGB24;
477 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
478 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
479 avctx->pix_fmt = PIX_FMT_RGBA;
480 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
481 s->color_type == PNG_COLOR_TYPE_GRAY) {
482 avctx->pix_fmt = PIX_FMT_GRAY8;
483 } else if (s->bit_depth == 16 &&
484 s->color_type == PNG_COLOR_TYPE_GRAY) {
485 avctx->pix_fmt = PIX_FMT_GRAY16BE;
486 } else if (s->bit_depth == 16 &&
487 s->color_type == PNG_COLOR_TYPE_RGB) {
488 avctx->pix_fmt = PIX_FMT_RGB48BE;
489 } else if (s->bit_depth == 16 &&
490 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
491 avctx->pix_fmt = PIX_FMT_RGBA64BE;
492 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
493 avctx->pix_fmt = PIX_FMT_PAL8;
494 } else if (s->bit_depth == 1) {
495 avctx->pix_fmt = PIX_FMT_MONOBLACK;
496 } else if (s->bit_depth == 8 &&
497 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
498 avctx->pix_fmt = PIX_FMT_GRAY8A;
500 av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
501 "and color type %d\n",
502 s->bit_depth, s->color_type);
506 avctx->release_buffer(avctx, p);
509 if(avctx->get_buffer(avctx, p) < 0){
510 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
513 p->pict_type= AV_PICTURE_TYPE_I;
515 p->interlaced_frame = !!s->interlace_type;
517 /* compute the compressed row size */
518 if (!s->interlace_type) {
519 s->crow_size = s->row_size + 1;
522 s->pass_row_size = ff_png_pass_row_size(s->pass,
525 s->crow_size = s->pass_row_size + 1;
527 av_dlog(avctx, "row_size=%d crow_size =%d\n",
528 s->row_size, s->crow_size);
529 s->image_buf = p->data[0];
530 s->image_linesize = p->linesize[0];
531 /* copy the palette if needed */
532 if (avctx->pix_fmt == PIX_FMT_PAL8)
533 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
534 /* empty row is used if differencing to the first row */
535 s->last_row = av_mallocz(s->row_size);
538 if (s->interlace_type ||
539 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
540 s->tmp_row = av_malloc(s->row_size);
545 crow_buf_base = av_malloc(s->row_size + 16);
549 /* we want crow_buf+1 to be 16-byte aligned */
550 s->crow_buf = crow_buf_base + 15;
551 s->zstream.avail_out = s->crow_size;
552 s->zstream.next_out = s->crow_buf;
554 s->state |= PNG_IDAT;
555 if (png_decode_idat(s, length) < 0)
557 bytestream2_skip(&s->gb, 4); /* crc */
559 case MKTAG('P', 'L', 'T', 'E'):
563 if ((length % 3) != 0 || length > 256 * 3)
565 /* read the palette */
568 r = bytestream2_get_byte(&s->gb);
569 g = bytestream2_get_byte(&s->gb);
570 b = bytestream2_get_byte(&s->gb);
571 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
574 s->palette[i] = (0xff << 24);
576 s->state |= PNG_PLTE;
577 bytestream2_skip(&s->gb, 4); /* crc */
580 case MKTAG('t', 'R', 'N', 'S'):
584 /* read the transparency. XXX: Only palette mode supported */
585 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
587 !(s->state & PNG_PLTE))
589 for(i=0;i<length;i++) {
590 v = bytestream2_get_byte(&s->gb);
591 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
593 bytestream2_skip(&s->gb, 4); /* crc */
596 case MKTAG('I', 'E', 'N', 'D'):
597 if (!(s->state & PNG_ALLIMAGE))
598 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
599 if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
602 bytestream2_skip(&s->gb, 4); /* crc */
607 bytestream2_skip(&s->gb, length + 4);
613 if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
615 uint8_t *pd = s->current_picture->data[0];
616 for(j=0; j < s->height; j++) {
617 for(i=s->width/8-1; i>=0; i--) {
619 pd[8*i+6]= (pd[i]>>1)&1;
620 pd[8*i+5]= (pd[i]>>2)&1;
621 pd[8*i+4]= (pd[i]>>3)&1;
622 pd[8*i+3]= (pd[i]>>4)&1;
623 pd[8*i+2]= (pd[i]>>5)&1;
624 pd[8*i+1]= (pd[i]>>6)&1;
627 pd += s->image_linesize;
630 if(s->bits_per_pixel == 2){
632 uint8_t *pd = s->current_picture->data[0];
633 for(j=0; j < s->height; j++) {
634 if (s->color_type == PNG_COLOR_TYPE_PALETTE){
635 for(i=s->width/4-1; i>=0; i--) {
637 pd[4*i+2]= (pd[i]>>2)&3;
638 pd[4*i+1]= (pd[i]>>4)&3;
642 for(i=s->width/4-1; i>=0; i--) {
643 pd[4*i+3]= ( pd[i] &3)*0x55;
644 pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
645 pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
646 pd[4*i+0]= ( pd[i]>>6 )*0x55;
649 pd += s->image_linesize;
652 if(s->bits_per_pixel == 4){
654 uint8_t *pd = s->current_picture->data[0];
655 for(j=0; j < s->height; j++) {
656 if (s->color_type == PNG_COLOR_TYPE_PALETTE){
657 for(i=s->width/2-1; i>=0; i--) {
662 for(i=s->width/2-1; i>=0; i--) {
663 pd[2*i+1]= (pd[i]&15)*0x11;
664 pd[2*i+0]= (pd[i]>>4)*0x11;
667 pd += s->image_linesize;
671 /* handle p-frames only if a predecessor frame is available */
672 if(s->last_picture->data[0] != NULL) {
673 if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
675 uint8_t *pd = s->current_picture->data[0];
676 uint8_t *pd_last = s->last_picture->data[0];
678 for(j=0; j < s->height; j++) {
679 for(i=0; i < s->width * s->bpp; i++) {
682 pd += s->image_linesize;
683 pd_last += s->image_linesize;
688 *picture= *s->current_picture;
689 *data_size = sizeof(AVFrame);
691 ret = bytestream2_tell(&s->gb);
693 inflateEnd(&s->zstream);
694 av_free(crow_buf_base);
696 av_freep(&s->last_row);
697 av_freep(&s->tmp_row);
704 static av_cold int png_dec_init(AVCodecContext *avctx)
706 PNGDecContext *s = avctx->priv_data;
708 s->current_picture = &s->picture1;
709 s->last_picture = &s->picture2;
710 avcodec_get_frame_defaults(&s->picture1);
711 avcodec_get_frame_defaults(&s->picture2);
713 ff_pngdsp_init(&s->dsp);
718 static av_cold int png_dec_end(AVCodecContext *avctx)
720 PNGDecContext *s = avctx->priv_data;
722 if (s->picture1.data[0])
723 avctx->release_buffer(avctx, &s->picture1);
724 if (s->picture2.data[0])
725 avctx->release_buffer(avctx, &s->picture2);
730 AVCodec ff_png_decoder = {
732 .type = AVMEDIA_TYPE_VIDEO,
734 .priv_data_size = sizeof(PNGDecContext),
735 .init = png_dec_init,
736 .close = png_dec_end,
737 .decode = decode_frame,
738 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
739 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),