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 {
39 AVFrame picture1, picture2;
40 AVFrame *current_picture, *last_picture;
55 uint32_t palette[256];
60 int crow_size; /* compressed row size (include filter type) */
61 int row_size; /* decompressed row size */
62 int pass_row_size; /* decompress row size of the current pass */
67 /* Mask to determine which y pixels can be written in a pass */
68 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
69 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
72 /* Mask to determine which pixels to overwrite while displaying */
73 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
74 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
77 /* NOTE: we try to construct a good looking image at each pass. width
78 is the original image width. We also do pixel format conversion at
80 static void png_put_interlaced_row(uint8_t *dst, int width,
81 int bits_per_pixel, int pass,
82 int color_type, const uint8_t *src)
84 int x, mask, dsp_mask, j, src_x, b, bpp;
88 mask = ff_png_pass_mask[pass];
89 dsp_mask = png_pass_dsp_mask[pass];
90 switch(bits_per_pixel) {
92 /* we must initialize the line to zero before writing to it */
94 memset(dst, 0, (width + 7) >> 3);
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] |= b << (7 - j);
102 if ((mask << j) & 0x80)
107 bpp = bits_per_pixel >> 3;
110 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
111 for(x = 0; x < width; x++) {
113 if ((dsp_mask << j) & 0x80) {
114 *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
117 if ((mask << j) & 0x80)
121 for(x = 0; x < width; x++) {
123 if ((dsp_mask << j) & 0x80) {
127 if ((mask << j) & 0x80)
135 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
138 for(i = 0; i < w; i++) {
139 int a, b, c, p, pa, pb, pc;
152 if (pa <= pb && pa <= pc)
162 #define UNROLL1(bpp, op) {\
164 if(bpp >= 2) g = dst[1];\
165 if(bpp >= 3) b = dst[2];\
166 if(bpp >= 4) a = dst[3];\
167 for(; i < size; i+=bpp) {\
168 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
169 if(bpp == 1) continue;\
170 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
171 if(bpp == 2) continue;\
172 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
173 if(bpp == 3) continue;\
174 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
178 #define UNROLL_FILTER(op)\
179 if(bpp == 1) UNROLL1(1, op)\
180 else if(bpp == 2) UNROLL1(2, op)\
181 else if(bpp == 3) UNROLL1(3, op)\
182 else if(bpp == 4) UNROLL1(4, op)\
184 for (; i < size; i += bpp) {\
186 for (j = 0; j < bpp; j++)\
187 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
191 /* NOTE: 'dst' can be equal to 'last' */
192 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
193 uint8_t *src, uint8_t *last, int size, int bpp)
195 int i, p, r, g, b, a;
197 switch(filter_type) {
198 case PNG_FILTER_VALUE_NONE:
199 memcpy(dst, src, size);
201 case PNG_FILTER_VALUE_SUB:
202 for(i = 0; i < bpp; i++) {
207 for(; i < size; i+=bpp) {
208 int s = *(int*)(src+i);
209 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
213 #define OP_SUB(x,s,l) x+s
214 UNROLL_FILTER(OP_SUB);
217 case PNG_FILTER_VALUE_UP:
218 dsp->add_bytes_l2(dst, src, last, size);
220 case PNG_FILTER_VALUE_AVG:
221 for(i = 0; i < bpp; i++) {
225 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
226 UNROLL_FILTER(OP_AVG);
228 case PNG_FILTER_VALUE_PAETH:
229 for(i = 0; i < bpp; i++) {
233 if(bpp > 1 && size > 4) {
234 // would write off the end of the array if we let it process the last pixel with bpp=3
235 int w = bpp==4 ? size : size-3;
236 dsp->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
239 ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
244 static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
247 unsigned int r, g, b, a;
249 for(j = 0;j < width; j++) {
258 *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
264 static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
267 convert_to_rgb32_loco(dst, src, width, 1);
269 convert_to_rgb32_loco(dst, src, width, 0);
272 static void deloco_rgb24(uint8_t *dst, int size)
275 for(i=0; i<size; i+=3) {
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;
290 /* need to swap bytes correctly for RGB_ALPHA */
291 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
292 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
293 s->last_row, s->row_size, s->bpp);
294 convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
295 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
297 /* in normal case, we avoid one copy */
299 last_row = s->last_row;
301 last_row = ptr - s->image_linesize;
303 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
304 last_row, s->row_size, s->bpp);
306 /* loco lags by 1 row so that it doesn't interfere with top prediction */
307 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
308 s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
309 deloco_rgb24(ptr - s->image_linesize, s->row_size);
311 if (s->y == s->height) {
312 s->state |= PNG_ALLIMAGE;
313 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
314 s->color_type == PNG_COLOR_TYPE_RGB)
315 deloco_rgb24(ptr, s->row_size);
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 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
333 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
334 s->color_type, s->last_row);
337 if (s->y == s->height) {
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 = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
364 s->zstream.next_in = s->gb.buffer;
365 bytestream2_skip(&s->gb, length);
367 /* decode one line if possible */
368 while (s->zstream.avail_in > 0) {
369 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
370 if (ret != Z_OK && ret != Z_STREAM_END) {
373 if (s->zstream.avail_out == 0) {
374 if (!(s->state & PNG_ALLIMAGE)) {
377 s->zstream.avail_out = s->crow_size;
378 s->zstream.next_out = s->crow_buf;
384 static int decode_frame(AVCodecContext *avctx,
385 void *data, int *data_size,
388 const uint8_t *buf = avpkt->data;
389 int buf_size = avpkt->size;
390 PNGDecContext * const s = avctx->priv_data;
391 AVFrame *picture = data;
393 uint8_t *crow_buf_base = NULL;
394 uint32_t tag, length;
397 FFSWAP(AVFrame *, s->current_picture, s->last_picture);
398 avctx->coded_frame= s->current_picture;
399 p = s->current_picture;
401 /* check signature */
403 memcmp(buf, ff_pngsig, 8) != 0 &&
404 memcmp(buf, ff_mngsig, 8) != 0)
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)
421 length = bytestream2_get_be32(&s->gb);
422 if (length > 0x7fffffff)
424 tag = bytestream2_get_le32(&s->gb);
425 av_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
428 ((tag >> 16) & 0xff),
429 ((tag >> 24) & 0xff), length);
431 case MKTAG('I', 'H', 'D', 'R'):
434 s->width = bytestream2_get_be32(&s->gb);
435 s->height = bytestream2_get_be32(&s->gb);
436 if(av_image_check_size(s->width, s->height, 0, avctx)){
437 s->width= s->height= 0;
440 s->bit_depth = bytestream2_get_byte(&s->gb);
441 s->color_type = bytestream2_get_byte(&s->gb);
442 s->compression_type = bytestream2_get_byte(&s->gb);
443 s->filter_type = bytestream2_get_byte(&s->gb);
444 s->interlace_type = bytestream2_get_byte(&s->gb);
445 bytestream2_skip(&s->gb, 4); /* crc */
446 s->state |= PNG_IHDR;
447 av_dlog(avctx, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
448 s->width, s->height, s->bit_depth, s->color_type,
449 s->compression_type, s->filter_type, s->interlace_type);
451 case MKTAG('I', 'D', 'A', 'T'):
452 if (!(s->state & PNG_IHDR))
454 if (!(s->state & PNG_IDAT)) {
455 /* init image info */
456 avctx->width = s->width;
457 avctx->height = s->height;
459 s->channels = ff_png_get_nb_channels(s->color_type);
460 s->bits_per_pixel = s->bit_depth * s->channels;
461 s->bpp = (s->bits_per_pixel + 7) >> 3;
462 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
464 if (s->bit_depth == 8 &&
465 s->color_type == PNG_COLOR_TYPE_RGB) {
466 avctx->pix_fmt = AV_PIX_FMT_RGB24;
467 } else if (s->bit_depth == 8 &&
468 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
469 avctx->pix_fmt = AV_PIX_FMT_RGB32;
470 } else if (s->bit_depth == 8 &&
471 s->color_type == PNG_COLOR_TYPE_GRAY) {
472 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
473 } else if (s->bit_depth == 16 &&
474 s->color_type == PNG_COLOR_TYPE_GRAY) {
475 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
476 } else if (s->bit_depth == 16 &&
477 s->color_type == PNG_COLOR_TYPE_RGB) {
478 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
479 } else if (s->bit_depth == 1 &&
480 s->color_type == PNG_COLOR_TYPE_GRAY) {
481 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
482 } else if (s->bit_depth == 8 &&
483 s->color_type == PNG_COLOR_TYPE_PALETTE) {
484 avctx->pix_fmt = AV_PIX_FMT_PAL8;
485 } else if (s->bit_depth == 8 &&
486 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
487 avctx->pix_fmt = AV_PIX_FMT_Y400A;
492 avctx->release_buffer(avctx, p);
495 if(avctx->get_buffer(avctx, p) < 0){
496 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
499 p->pict_type= AV_PICTURE_TYPE_I;
501 p->interlaced_frame = !!s->interlace_type;
503 /* compute the compressed row size */
504 if (!s->interlace_type) {
505 s->crow_size = s->row_size + 1;
508 s->pass_row_size = ff_png_pass_row_size(s->pass,
511 s->crow_size = s->pass_row_size + 1;
513 av_dlog(avctx, "row_size=%d crow_size =%d\n",
514 s->row_size, s->crow_size);
515 s->image_buf = p->data[0];
516 s->image_linesize = p->linesize[0];
517 /* copy the palette if needed */
518 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
519 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
520 /* empty row is used if differencing to the first row */
521 s->last_row = av_mallocz(s->row_size);
524 if (s->interlace_type ||
525 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
526 s->tmp_row = av_malloc(s->row_size);
531 crow_buf_base = av_malloc(s->row_size + 16);
535 /* we want crow_buf+1 to be 16-byte aligned */
536 s->crow_buf = crow_buf_base + 15;
537 s->zstream.avail_out = s->crow_size;
538 s->zstream.next_out = s->crow_buf;
540 s->state |= PNG_IDAT;
541 if (png_decode_idat(s, length) < 0)
543 bytestream2_skip(&s->gb, 4); /* crc */
545 case MKTAG('P', 'L', 'T', 'E'):
549 if ((length % 3) != 0 || length > 256 * 3)
551 /* read the palette */
554 r = bytestream2_get_byte(&s->gb);
555 g = bytestream2_get_byte(&s->gb);
556 b = bytestream2_get_byte(&s->gb);
557 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
560 s->palette[i] = (0xff << 24);
562 s->state |= PNG_PLTE;
563 bytestream2_skip(&s->gb, 4); /* crc */
566 case MKTAG('t', 'R', 'N', 'S'):
570 /* read the transparency. XXX: Only palette mode supported */
571 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
573 !(s->state & PNG_PLTE))
575 for(i=0;i<length;i++) {
576 v = bytestream2_get_byte(&s->gb);
577 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
579 bytestream2_skip(&s->gb, 4); /* crc */
582 case MKTAG('I', 'E', 'N', 'D'):
583 if (!(s->state & PNG_ALLIMAGE))
585 bytestream2_skip(&s->gb, 4); /* crc */
590 bytestream2_skip(&s->gb, length + 4);
595 /* handle p-frames only if a predecessor frame is available */
596 if(s->last_picture->data[0] != NULL) {
597 if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
599 uint8_t *pd = s->current_picture->data[0];
600 uint8_t *pd_last = s->last_picture->data[0];
602 for(j=0; j < s->height; j++) {
603 for(i=0; i < s->width * s->bpp; i++) {
606 pd += s->image_linesize;
607 pd_last += s->image_linesize;
612 *picture= *s->current_picture;
613 *data_size = sizeof(AVFrame);
615 ret = bytestream2_tell(&s->gb);
617 inflateEnd(&s->zstream);
618 av_free(crow_buf_base);
620 av_freep(&s->last_row);
621 av_freep(&s->tmp_row);
628 static av_cold int png_dec_init(AVCodecContext *avctx){
629 PNGDecContext *s = avctx->priv_data;
631 s->current_picture = &s->picture1;
632 s->last_picture = &s->picture2;
633 avcodec_get_frame_defaults(&s->picture1);
634 avcodec_get_frame_defaults(&s->picture2);
635 ff_pngdsp_init(&s->dsp);
640 static av_cold int png_dec_end(AVCodecContext *avctx)
642 PNGDecContext *s = avctx->priv_data;
644 if (s->picture1.data[0])
645 avctx->release_buffer(avctx, &s->picture1);
646 if (s->picture2.data[0])
647 avctx->release_buffer(avctx, &s->picture2);
652 AVCodec ff_png_decoder = {
654 .type = AVMEDIA_TYPE_VIDEO,
655 .id = AV_CODEC_ID_PNG,
656 .priv_data_size = sizeof(PNGDecContext),
657 .init = png_dec_init,
658 .close = png_dec_end,
659 .decode = decode_frame,
660 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
661 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),