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 {
41 const uint8_t *bytestream;
42 const uint8_t *bytestream_start;
43 const uint8_t *bytestream_end;
44 AVFrame picture1, picture2;
45 AVFrame *current_picture, *last_picture;
60 uint32_t palette[256];
65 int crow_size; /* compressed row size (include filter type) */
66 int row_size; /* decompressed row size */
67 int pass_row_size; /* decompress row size of the current pass */
72 /* Mask to determine which y pixels can be written in a pass */
73 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
74 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
77 /* Mask to determine which pixels to overwrite while displaying */
78 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
79 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
82 /* NOTE: we try to construct a good looking image at each pass. width
83 is the original image width. We also do pixel format conversion at
85 static void png_put_interlaced_row(uint8_t *dst, int width,
86 int bits_per_pixel, int pass,
87 int color_type, const uint8_t *src)
89 int x, mask, dsp_mask, j, src_x, b, bpp;
93 mask = ff_png_pass_mask[pass];
94 dsp_mask = png_pass_dsp_mask[pass];
95 switch(bits_per_pixel) {
98 for(x = 0; x < width; x++) {
100 if ((dsp_mask << j) & 0x80) {
101 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
102 dst[x >> 3] &= 0xFF7F>>j;
103 dst[x >> 3] |= b << (7 - j);
105 if ((mask << j) & 0x80)
111 for(x = 0; x < width; x++) {
114 if ((dsp_mask << j) & 0x80) {
115 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
116 dst[x >> 2] &= 0xFF3F>>j2;
117 dst[x >> 2] |= b << (6 - j2);
119 if ((mask << j) & 0x80)
125 for(x = 0; x < width; x++) {
128 if ((dsp_mask << j) & 0x80) {
129 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
130 dst[x >> 1] &= 0xFF0F>>j2;
131 dst[x >> 1] |= b << (4 - j2);
133 if ((mask << j) & 0x80)
138 bpp = bits_per_pixel >> 3;
141 for(x = 0; x < width; x++) {
143 if ((dsp_mask << j) & 0x80) {
147 if ((mask << j) & 0x80)
154 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
157 for(i = 0; i < w; i++) {
158 int a, b, c, p, pa, pb, pc;
171 if (pa <= pb && pa <= pc)
181 #define UNROLL1(bpp, op) {\
183 if(bpp >= 2) g = dst[1];\
184 if(bpp >= 3) b = dst[2];\
185 if(bpp >= 4) a = dst[3];\
186 for(; i < size; i+=bpp) {\
187 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
188 if(bpp == 1) continue;\
189 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
190 if(bpp == 2) continue;\
191 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
192 if(bpp == 3) continue;\
193 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
197 #define UNROLL_FILTER(op)\
198 if(bpp == 1) UNROLL1(1, op)\
199 else if(bpp == 2) UNROLL1(2, op)\
200 else if(bpp == 3) UNROLL1(3, op)\
201 else if(bpp == 4) UNROLL1(4, op)\
203 for (; i < size; i += bpp) {\
205 for (j = 0; j < bpp; j++)\
206 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
210 /* NOTE: 'dst' can be equal to 'last' */
211 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
212 uint8_t *src, uint8_t *last, int size, int bpp)
214 int i, p, r, g, b, a;
216 switch(filter_type) {
217 case PNG_FILTER_VALUE_NONE:
218 memcpy(dst, src, size);
220 case PNG_FILTER_VALUE_SUB:
221 for(i = 0; i < bpp; i++) {
226 for(; i < size; i+=bpp) {
227 int s = *(int*)(src+i);
228 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
232 #define OP_SUB(x,s,l) x+s
233 UNROLL_FILTER(OP_SUB);
236 case PNG_FILTER_VALUE_UP:
237 dsp->add_bytes_l2(dst, src, last, size);
239 case PNG_FILTER_VALUE_AVG:
240 for(i = 0; i < bpp; i++) {
244 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
245 UNROLL_FILTER(OP_AVG);
247 case PNG_FILTER_VALUE_PAETH:
248 for(i = 0; i < bpp; i++) {
252 if(bpp > 2 && size > 4) {
253 // would write off the end of the array if we let it process the last pixel with bpp=3
254 int w = bpp==4 ? size : size-3;
255 dsp->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
258 ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
263 /* This used to be called "deloco" in FFmpeg
264 * and is actually an inverse reversible colorspace transformation */
265 #define YUV2RGB(NAME, TYPE) \
266 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
269 for (i = 0; i < size; i += 3 + alpha) { \
276 YUV2RGB(rgb8, uint8_t)
277 YUV2RGB(rgb16, uint16_t)
279 /* process exactly one decompressed row */
280 static void png_handle_row(PNGDecContext *s)
282 uint8_t *ptr, *last_row;
285 if (!s->interlace_type) {
286 ptr = s->image_buf + s->image_linesize * s->y;
288 last_row = s->last_row;
290 last_row = ptr - s->image_linesize;
292 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
293 last_row, s->row_size, s->bpp);
294 /* loco lags by 1 row so that it doesn't interfere with top prediction */
295 if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
296 if (s->bit_depth == 16) {
297 deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
298 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
300 deloco_rgb8(ptr - s->image_linesize, s->row_size,
301 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
305 if (s->y == s->height) {
306 s->state |= PNG_ALLIMAGE;
307 if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
308 if (s->bit_depth == 16) {
309 deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
310 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
312 deloco_rgb8(ptr, s->row_size,
313 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
320 ptr = s->image_buf + s->image_linesize * s->y;
321 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
322 /* if we already read one row, it is time to stop to
323 wait for the next one */
326 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
327 s->last_row, s->pass_row_size, s->bpp);
328 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
331 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
332 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
333 s->color_type, s->last_row);
336 if (s->y == s->height) {
337 memset(s->last_row, 0, s->row_size);
339 if (s->pass == NB_PASSES - 1) {
340 s->state |= PNG_ALLIMAGE;
345 s->pass_row_size = ff_png_pass_row_size(s->pass,
348 s->crow_size = s->pass_row_size + 1;
349 if (s->pass_row_size != 0)
351 /* skip pass if empty row */
360 static int png_decode_idat(PNGDecContext *s, int length)
363 s->zstream.avail_in = length;
364 s->zstream.next_in = s->bytestream;
365 s->bytestream += length;
367 if(s->bytestream > s->bytestream_end)
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;
400 FFSWAP(AVFrame *, s->current_picture, s->last_picture);
401 avctx->coded_frame= s->current_picture;
402 p = s->current_picture;
406 s->bytestream_end= buf + buf_size;
408 /* check signature */
409 if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
410 memcmp(s->bytestream, ff_mngsig, 8) != 0)
415 // memset(s, 0, sizeof(PNGDecContext));
417 s->zstream.zalloc = ff_png_zalloc;
418 s->zstream.zfree = ff_png_zfree;
419 s->zstream.opaque = NULL;
420 ret = inflateInit(&s->zstream);
425 if (s->bytestream >= s->bytestream_end)
427 length = bytestream_get_be32(&s->bytestream);
428 if (length > 0x7fffffff)
430 tag32 = bytestream_get_be32(&s->bytestream);
431 tag = av_bswap32(tag32);
432 if (avctx->debug & FF_DEBUG_STARTCODE)
433 av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
436 ((tag >> 16) & 0xff),
437 ((tag >> 24) & 0xff), length);
439 case MKTAG('I', 'H', 'D', 'R'):
442 s->width = bytestream_get_be32(&s->bytestream);
443 s->height = bytestream_get_be32(&s->bytestream);
444 if(av_image_check_size(s->width, s->height, 0, avctx)){
445 s->width= s->height= 0;
448 s->bit_depth = *s->bytestream++;
449 s->color_type = *s->bytestream++;
450 s->compression_type = *s->bytestream++;
451 s->filter_type = *s->bytestream++;
452 s->interlace_type = *s->bytestream++;
453 s->bytestream += 4; /* crc */
454 s->state |= PNG_IHDR;
455 if (avctx->debug & FF_DEBUG_PICT_INFO)
456 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",
457 s->width, s->height, s->bit_depth, s->color_type,
458 s->compression_type, s->filter_type, s->interlace_type);
460 case MKTAG('I', 'D', 'A', 'T'):
461 if (!(s->state & PNG_IHDR))
463 if (!(s->state & PNG_IDAT)) {
464 /* init image info */
465 avctx->width = s->width;
466 avctx->height = s->height;
468 s->channels = ff_png_get_nb_channels(s->color_type);
469 s->bits_per_pixel = s->bit_depth * s->channels;
470 s->bpp = (s->bits_per_pixel + 7) >> 3;
471 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
473 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
474 s->color_type == PNG_COLOR_TYPE_RGB) {
475 avctx->pix_fmt = PIX_FMT_RGB24;
476 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
477 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
478 avctx->pix_fmt = PIX_FMT_RGBA;
479 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
480 s->color_type == PNG_COLOR_TYPE_GRAY) {
481 avctx->pix_fmt = PIX_FMT_GRAY8;
482 } else if (s->bit_depth == 16 &&
483 s->color_type == PNG_COLOR_TYPE_GRAY) {
484 avctx->pix_fmt = PIX_FMT_GRAY16BE;
485 } else if (s->bit_depth == 16 &&
486 s->color_type == PNG_COLOR_TYPE_RGB) {
487 avctx->pix_fmt = PIX_FMT_RGB48BE;
488 } else if (s->bit_depth == 16 &&
489 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
490 avctx->pix_fmt = PIX_FMT_RGBA64BE;
491 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
492 avctx->pix_fmt = PIX_FMT_PAL8;
493 } else if (s->bit_depth == 1) {
494 avctx->pix_fmt = PIX_FMT_MONOBLACK;
495 } else if (s->bit_depth == 8 &&
496 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
497 avctx->pix_fmt = PIX_FMT_GRAY8A;
499 av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
500 "and color type %d\n",
501 s->bit_depth, s->color_type);
505 avctx->release_buffer(avctx, p);
508 if(avctx->get_buffer(avctx, p) < 0){
509 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
512 p->pict_type= AV_PICTURE_TYPE_I;
514 p->interlaced_frame = !!s->interlace_type;
516 /* compute the compressed row size */
517 if (!s->interlace_type) {
518 s->crow_size = s->row_size + 1;
521 s->pass_row_size = ff_png_pass_row_size(s->pass,
524 s->crow_size = s->pass_row_size + 1;
526 av_dlog(avctx, "row_size=%d crow_size =%d\n",
527 s->row_size, s->crow_size);
528 s->image_buf = p->data[0];
529 s->image_linesize = p->linesize[0];
530 /* copy the palette if needed */
531 if (avctx->pix_fmt == PIX_FMT_PAL8)
532 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
533 /* empty row is used if differencing to the first row */
534 s->last_row = av_mallocz(s->row_size);
537 if (s->interlace_type ||
538 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
539 s->tmp_row = av_malloc(s->row_size);
544 crow_buf_base = av_malloc(s->row_size + 16);
548 /* we want crow_buf+1 to be 16-byte aligned */
549 s->crow_buf = crow_buf_base + 15;
550 s->zstream.avail_out = s->crow_size;
551 s->zstream.next_out = s->crow_buf;
553 s->state |= PNG_IDAT;
554 if (png_decode_idat(s, length) < 0)
556 s->bytestream += 4; /* crc */
558 case MKTAG('P', 'L', 'T', 'E'):
562 if ((length % 3) != 0 || length > 256 * 3)
564 /* read the palette */
567 r = *s->bytestream++;
568 g = *s->bytestream++;
569 b = *s->bytestream++;
570 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
573 s->palette[i] = (0xff << 24);
575 s->state |= PNG_PLTE;
576 s->bytestream += 4; /* crc */
579 case MKTAG('t', 'R', 'N', 'S'):
583 /* read the transparency. XXX: Only palette mode supported */
584 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
586 !(s->state & PNG_PLTE))
588 for(i=0;i<length;i++) {
589 v = *s->bytestream++;
590 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
592 s->bytestream += 4; /* crc */
595 case MKTAG('I', 'E', 'N', 'D'):
596 if (!(s->state & PNG_ALLIMAGE))
598 s->bytestream += 4; /* crc */
603 s->bytestream += length + 4;
609 if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
611 uint8_t *pd = s->current_picture->data[0];
612 for(j=0; j < s->height; j++) {
613 for(i=s->width/8-1; i>=0; i--) {
615 pd[8*i+6]= (pd[i]>>1)&1;
616 pd[8*i+5]= (pd[i]>>2)&1;
617 pd[8*i+4]= (pd[i]>>3)&1;
618 pd[8*i+3]= (pd[i]>>4)&1;
619 pd[8*i+2]= (pd[i]>>5)&1;
620 pd[8*i+1]= (pd[i]>>6)&1;
623 pd += s->image_linesize;
626 if(s->bits_per_pixel == 2){
628 uint8_t *pd = s->current_picture->data[0];
629 for(j=0; j < s->height; j++) {
630 if (s->color_type == PNG_COLOR_TYPE_PALETTE){
631 for(i=s->width/4-1; i>=0; i--) {
633 pd[4*i+2]= (pd[i]>>2)&3;
634 pd[4*i+1]= (pd[i]>>4)&3;
638 for(i=s->width/4-1; i>=0; i--) {
639 pd[4*i+3]= ( pd[i] &3)*0x55;
640 pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
641 pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
642 pd[4*i+0]= ( pd[i]>>6 )*0x55;
645 pd += s->image_linesize;
648 if(s->bits_per_pixel == 4){
650 uint8_t *pd = s->current_picture->data[0];
651 for(j=0; j < s->height; j++) {
652 if (s->color_type == PNG_COLOR_TYPE_PALETTE){
653 for(i=s->width/2-1; i>=0; i--) {
658 for(i=s->width/2-1; i>=0; i--) {
659 pd[2*i+1]= (pd[i]&15)*0x11;
660 pd[2*i+0]= (pd[i]>>4)*0x11;
663 pd += s->image_linesize;
667 /* handle p-frames only if a predecessor frame is available */
668 if(s->last_picture->data[0] != NULL) {
669 if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
671 uint8_t *pd = s->current_picture->data[0];
672 uint8_t *pd_last = s->last_picture->data[0];
674 for(j=0; j < s->height; j++) {
675 for(i=0; i < s->width * s->bpp; i++) {
678 pd += s->image_linesize;
679 pd_last += s->image_linesize;
684 *picture= *s->current_picture;
685 *data_size = sizeof(AVFrame);
687 ret = s->bytestream - s->bytestream_start;
689 inflateEnd(&s->zstream);
690 av_free(crow_buf_base);
692 av_freep(&s->last_row);
693 av_freep(&s->tmp_row);
700 static av_cold int png_dec_init(AVCodecContext *avctx)
702 PNGDecContext *s = avctx->priv_data;
704 s->current_picture = &s->picture1;
705 s->last_picture = &s->picture2;
706 avcodec_get_frame_defaults(&s->picture1);
707 avcodec_get_frame_defaults(&s->picture2);
709 ff_pngdsp_init(&s->dsp);
714 static av_cold int png_dec_end(AVCodecContext *avctx)
716 PNGDecContext *s = avctx->priv_data;
718 if (s->picture1.data[0])
719 avctx->release_buffer(avctx, &s->picture1);
720 if (s->picture2.data[0])
721 avctx->release_buffer(avctx, &s->picture2);
726 AVCodec ff_png_decoder = {
728 .type = AVMEDIA_TYPE_VIDEO,
730 .priv_data_size = sizeof(PNGDecContext),
731 .init = png_dec_init,
732 .close = png_dec_end,
733 .decode = decode_frame,
734 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
735 .long_name = NULL_IF_CONFIG_SMALL("PNG image"),