3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/imgutils.h"
23 #include "bytestream.h"
28 * - add 2, 4 and 16 bit depth support
35 typedef struct PNGDecContext {
38 const uint8_t *bytestream;
39 const uint8_t *bytestream_start;
40 const uint8_t *bytestream_end;
41 AVFrame picture1, picture2;
42 AVFrame *current_picture, *last_picture;
57 uint32_t palette[256];
62 int crow_size; /* compressed row size (include filter type) */
63 int row_size; /* decompressed row size */
64 int pass_row_size; /* decompress row size of the current pass */
69 /* Mask to determine which y pixels can be written in a pass */
70 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
71 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
74 /* Mask to determine which pixels to overwrite while displaying */
75 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
76 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
79 /* NOTE: we try to construct a good looking image at each pass. width
80 is the original image width. We also do pixel format conversion at
82 static void png_put_interlaced_row(uint8_t *dst, int width,
83 int bits_per_pixel, int pass,
84 int color_type, const uint8_t *src)
86 int x, mask, dsp_mask, j, src_x, b, bpp;
90 mask = ff_png_pass_mask[pass];
91 dsp_mask = png_pass_dsp_mask[pass];
92 switch(bits_per_pixel) {
94 /* we must initialize the line to zero before writing to it */
96 memset(dst, 0, (width + 7) >> 3);
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] |= b << (7 - j);
104 if ((mask << j) & 0x80)
109 bpp = bits_per_pixel >> 3;
112 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
113 for(x = 0; x < width; x++) {
115 if ((dsp_mask << j) & 0x80) {
116 *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
119 if ((mask << j) & 0x80)
123 for(x = 0; x < width; x++) {
125 if ((dsp_mask << j) & 0x80) {
129 if ((mask << j) & 0x80)
137 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
140 for(i = 0; i < w; i++) {
141 int a, b, c, p, pa, pb, pc;
154 if (pa <= pb && pa <= pc)
164 #define UNROLL1(bpp, op) {\
166 if(bpp >= 2) g = dst[1];\
167 if(bpp >= 3) b = dst[2];\
168 if(bpp >= 4) a = dst[3];\
169 for(; i < size; i+=bpp) {\
170 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
171 if(bpp == 1) continue;\
172 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
173 if(bpp == 2) continue;\
174 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
175 if(bpp == 3) continue;\
176 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
180 #define UNROLL_FILTER(op)\
181 if(bpp == 1) UNROLL1(1, op)\
182 else if(bpp == 2) UNROLL1(2, op)\
183 else if(bpp == 3) UNROLL1(3, op)\
184 else if(bpp == 4) UNROLL1(4, op)\
186 for (; i < size; i += bpp) {\
188 for (j = 0; j < bpp; j++)\
189 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
193 /* NOTE: 'dst' can be equal to 'last' */
194 static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
195 uint8_t *src, uint8_t *last, int size, int bpp)
197 int i, p, r, g, b, a;
199 switch(filter_type) {
200 case PNG_FILTER_VALUE_NONE:
201 memcpy(dst, src, size);
203 case PNG_FILTER_VALUE_SUB:
204 for(i = 0; i < bpp; i++) {
209 for(; i < size; i+=bpp) {
210 int s = *(int*)(src+i);
211 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
215 #define OP_SUB(x,s,l) x+s
216 UNROLL_FILTER(OP_SUB);
219 case PNG_FILTER_VALUE_UP:
220 dsp->add_bytes_l2(dst, src, last, size);
222 case PNG_FILTER_VALUE_AVG:
223 for(i = 0; i < bpp; i++) {
227 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
228 UNROLL_FILTER(OP_AVG);
230 case PNG_FILTER_VALUE_PAETH:
231 for(i = 0; i < bpp; i++) {
235 if(bpp > 1 && size > 4) {
236 // would write off the end of the array if we let it process the last pixel with bpp=3
237 int w = bpp==4 ? size : size-3;
238 dsp->add_png_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
241 ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
246 static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
249 unsigned int r, g, b, a;
251 for(j = 0;j < width; j++) {
260 *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
266 static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
269 convert_to_rgb32_loco(dst, src, width, 1);
271 convert_to_rgb32_loco(dst, src, width, 0);
274 static void deloco_rgb24(uint8_t *dst, int size)
277 for(i=0; i<size; i+=3) {
284 /* process exactly one decompressed row */
285 static void png_handle_row(PNGDecContext *s)
287 uint8_t *ptr, *last_row;
290 if (!s->interlace_type) {
291 ptr = s->image_buf + s->image_linesize * s->y;
292 /* need to swap bytes correctly for RGB_ALPHA */
293 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
294 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
295 s->last_row, s->row_size, s->bpp);
296 convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
297 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
299 /* in normal case, we avoid one copy */
301 last_row = s->last_row;
303 last_row = ptr - s->image_linesize;
305 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
306 last_row, s->row_size, s->bpp);
308 /* loco lags by 1 row so that it doesn't interfere with top prediction */
309 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
310 s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
311 deloco_rgb24(ptr - s->image_linesize, s->row_size);
313 if (s->y == s->height) {
314 s->state |= PNG_ALLIMAGE;
315 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
316 s->color_type == PNG_COLOR_TYPE_RGB)
317 deloco_rgb24(ptr, s->row_size);
322 ptr = s->image_buf + s->image_linesize * s->y;
323 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
324 /* if we already read one row, it is time to stop to
325 wait for the next one */
328 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
329 s->last_row, s->pass_row_size, s->bpp);
330 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
333 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
334 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
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) {
341 if (s->pass == NB_PASSES - 1) {
342 s->state |= PNG_ALLIMAGE;
347 s->pass_row_size = ff_png_pass_row_size(s->pass,
350 s->crow_size = s->pass_row_size + 1;
351 if (s->pass_row_size != 0)
353 /* skip pass if empty row */
362 static int png_decode_idat(PNGDecContext *s, int length)
365 s->zstream.avail_in = length;
366 s->zstream.next_in = s->bytestream;
367 s->bytestream += length;
369 if(s->bytestream > s->bytestream_end)
372 /* decode one line if possible */
373 while (s->zstream.avail_in > 0) {
374 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
375 if (ret != Z_OK && ret != Z_STREAM_END) {
378 if (s->zstream.avail_out == 0) {
379 if (!(s->state & PNG_ALLIMAGE)) {
382 s->zstream.avail_out = s->crow_size;
383 s->zstream.next_out = s->crow_buf;
389 static int decode_frame(AVCodecContext *avctx,
390 void *data, int *data_size,
393 const uint8_t *buf = avpkt->data;
394 int buf_size = avpkt->size;
395 PNGDecContext * const s = avctx->priv_data;
396 AVFrame *picture = data;
398 uint8_t *crow_buf_base = NULL;
399 uint32_t tag, length;
402 FFSWAP(AVFrame *, s->current_picture, s->last_picture);
403 avctx->coded_frame= s->current_picture;
404 p = s->current_picture;
408 s->bytestream_end= buf + buf_size;
410 /* check signature */
411 if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
412 memcmp(s->bytestream, ff_mngsig, 8) != 0)
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);
427 if (s->bytestream >= s->bytestream_end)
429 length = bytestream_get_be32(&s->bytestream);
430 if (length > 0x7fffffff)
432 tag32 = bytestream_get_be32(&s->bytestream);
433 tag = av_bswap32(tag32);
434 av_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
437 ((tag >> 16) & 0xff),
438 ((tag >> 24) & 0xff), length);
440 case MKTAG('I', 'H', 'D', 'R'):
443 s->width = bytestream_get_be32(&s->bytestream);
444 s->height = bytestream_get_be32(&s->bytestream);
445 if(av_image_check_size(s->width, s->height, 0, avctx)){
446 s->width= s->height= 0;
449 s->bit_depth = *s->bytestream++;
450 s->color_type = *s->bytestream++;
451 s->compression_type = *s->bytestream++;
452 s->filter_type = *s->bytestream++;
453 s->interlace_type = *s->bytestream++;
454 s->bytestream += 4; /* crc */
455 s->state |= PNG_IHDR;
456 av_dlog(avctx, "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 == 8 &&
474 s->color_type == PNG_COLOR_TYPE_RGB) {
475 avctx->pix_fmt = PIX_FMT_RGB24;
476 } else if (s->bit_depth == 8 &&
477 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
478 avctx->pix_fmt = PIX_FMT_RGB32;
479 } else if (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 == 1 &&
489 s->color_type == PNG_COLOR_TYPE_GRAY) {
490 avctx->pix_fmt = PIX_FMT_MONOBLACK;
491 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
492 avctx->pix_fmt = PIX_FMT_PAL8;
493 } else if (s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
494 avctx->pix_fmt = PIX_FMT_Y400A;
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 (s->color_type == PNG_COLOR_TYPE_PALETTE)
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 s->bytestream += 4; /* crc */
552 case MKTAG('P', 'L', 'T', 'E'):
556 if ((length % 3) != 0 || length > 256 * 3)
558 /* read the palette */
561 r = *s->bytestream++;
562 g = *s->bytestream++;
563 b = *s->bytestream++;
564 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
567 s->palette[i] = (0xff << 24);
569 s->state |= PNG_PLTE;
570 s->bytestream += 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 = *s->bytestream++;
584 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
586 s->bytestream += 4; /* crc */
589 case MKTAG('I', 'E', 'N', 'D'):
590 if (!(s->state & PNG_ALLIMAGE))
592 s->bytestream += 4; /* crc */
597 s->bytestream += length + 4;
602 /* handle p-frames only if a predecessor frame is available */
603 if(s->last_picture->data[0] != NULL) {
604 if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
606 uint8_t *pd = s->current_picture->data[0];
607 uint8_t *pd_last = s->last_picture->data[0];
609 for(j=0; j < s->height; j++) {
610 for(i=0; i < s->width * s->bpp; i++) {
613 pd += s->image_linesize;
614 pd_last += s->image_linesize;
619 *picture= *s->current_picture;
620 *data_size = sizeof(AVFrame);
622 ret = s->bytestream - s->bytestream_start;
624 inflateEnd(&s->zstream);
625 av_free(crow_buf_base);
627 av_freep(&s->last_row);
628 av_freep(&s->tmp_row);
635 static av_cold int png_dec_init(AVCodecContext *avctx){
636 PNGDecContext *s = avctx->priv_data;
638 s->current_picture = &s->picture1;
639 s->last_picture = &s->picture2;
640 avcodec_get_frame_defaults(&s->picture1);
641 avcodec_get_frame_defaults(&s->picture2);
642 dsputil_init(&s->dsp, avctx);
647 static av_cold int png_dec_end(AVCodecContext *avctx)
649 PNGDecContext *s = avctx->priv_data;
651 if (s->picture1.data[0])
652 avctx->release_buffer(avctx, &s->picture1);
653 if (s->picture2.data[0])
654 avctx->release_buffer(avctx, &s->picture2);
659 AVCodec ff_png_decoder = {
661 .type = AVMEDIA_TYPE_VIDEO,
663 .priv_data_size = sizeof(PNGDecContext),
664 .init = png_dec_init,
665 .close = png_dec_end,
666 .decode = decode_frame,
667 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
668 .long_name = NULL_IF_CONFIG_SMALL("PNG image"),