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"
29 * - add 2, 4 and 16 bit depth support
34 typedef struct PNGDecContext {
53 uint32_t palette[256];
58 int crow_size; /* compressed row size (include filter type) */
59 int row_size; /* decompressed row size */
60 int pass_row_size; /* decompress row size of the current pass */
65 /* Mask to determine which y pixels can be written in a pass */
66 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
67 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
70 /* Mask to determine which pixels to overwrite while displaying */
71 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
72 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
75 /* NOTE: we try to construct a good looking image at each pass. width
76 is the original image width. We also do pixel format conversion at
78 static void png_put_interlaced_row(uint8_t *dst, int width,
79 int bits_per_pixel, int pass,
80 int color_type, const uint8_t *src)
82 int x, mask, dsp_mask, j, src_x, b, bpp;
86 mask = ff_png_pass_mask[pass];
87 dsp_mask = png_pass_dsp_mask[pass];
89 switch (bits_per_pixel) {
91 /* we must initialize the line to zero before writing to it */
93 memset(dst, 0, (width + 7) >> 3);
95 for (x = 0; x < width; x++) {
97 if ((dsp_mask << j) & 0x80) {
98 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
99 dst[x >> 3] |= b << (7 - j);
101 if ((mask << j) & 0x80)
106 bpp = bits_per_pixel >> 3;
109 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
110 for (x = 0; x < width; x++) {
112 if ((dsp_mask << j) & 0x80) {
113 *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
116 if ((mask << j) & 0x80)
120 for(x = 0; x < width; x++) {
122 if ((dsp_mask << j) & 0x80) {
126 if ((mask << j) & 0x80)
134 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
137 for (i = 0; i < w; i++) {
138 int a, b, c, p, pa, pb, pc;
151 if (pa <= pb && pa <= pc)
161 #define UNROLL1(bpp, op) {\
163 if(bpp >= 2) g = dst[1];\
164 if(bpp >= 3) b = dst[2];\
165 if(bpp >= 4) a = dst[3];\
166 for(; i < size; i+=bpp) {\
167 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
168 if(bpp == 1) continue;\
169 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
170 if(bpp == 2) continue;\
171 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
172 if(bpp == 3) continue;\
173 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
177 #define UNROLL_FILTER(op)\
178 if(bpp == 1) UNROLL1(1, op)\
179 else if(bpp == 2) UNROLL1(2, op)\
180 else if(bpp == 3) UNROLL1(3, op)\
181 else if(bpp == 4) UNROLL1(4, op)\
183 for (; i < size; i += bpp) {\
185 for (j = 0; j < bpp; j++)\
186 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
190 /* NOTE: 'dst' can be equal to 'last' */
191 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
192 uint8_t *src, uint8_t *last, int size, int bpp)
194 int i, p, r, g, b, a;
196 switch (filter_type) {
197 case PNG_FILTER_VALUE_NONE:
198 memcpy(dst, src, size);
200 case PNG_FILTER_VALUE_SUB:
201 for (i = 0; i < bpp; i++) {
206 for (; i < size; i += bpp) {
207 int s = *(int*)(src + i);
208 p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
209 *(int*)(dst + i) = p;
212 #define OP_SUB(x,s,l) x+s
213 UNROLL_FILTER(OP_SUB);
216 case PNG_FILTER_VALUE_UP:
217 dsp->add_bytes_l2(dst, src, last, size);
219 case PNG_FILTER_VALUE_AVG:
220 for (i = 0; i < bpp; i++) {
224 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
225 UNROLL_FILTER(OP_AVG);
227 case PNG_FILTER_VALUE_PAETH:
228 for (i = 0; i < bpp; i++) {
232 if (bpp > 1 && size > 4) {
233 // would write off the end of the array if we let it process the last pixel with bpp=3
234 int w = bpp == 4 ? size : size - 3;
235 dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp);
238 ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
243 static av_always_inline void convert_to_rgb32_loco(uint8_t *dst,
248 unsigned int r, g, b, a;
250 for (j = 0; j < width; j++) {
259 *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
265 static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
268 convert_to_rgb32_loco(dst, src, width, 1);
270 convert_to_rgb32_loco(dst, src, width, 0);
273 static void deloco_rgb24(uint8_t *dst, int size)
276 for (i = 0; i < size; i += 3) {
283 /* process exactly one decompressed row */
284 static void png_handle_row(PNGDecContext *s)
286 uint8_t *ptr, *last_row;
289 if (!s->interlace_type) {
290 ptr = s->image_buf + s->image_linesize * s->y;
291 /* need to swap bytes correctly for RGB_ALPHA */
292 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
293 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
294 s->last_row, s->row_size, s->bpp);
295 convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
296 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
298 /* in normal case, we avoid one copy */
300 last_row = s->last_row;
302 last_row = ptr - s->image_linesize;
304 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
305 last_row, s->row_size, s->bpp);
307 /* loco lags by 1 row so that it doesn't interfere with top prediction */
308 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
309 s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
310 deloco_rgb24(ptr - s->image_linesize, s->row_size);
312 if (s->y == s->height) {
313 s->state |= PNG_ALLIMAGE;
314 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
315 s->color_type == PNG_COLOR_TYPE_RGB)
316 deloco_rgb24(ptr, s->row_size);
321 ptr = s->image_buf + s->image_linesize * s->y;
322 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
323 /* if we already read one row, it is time to stop to
324 wait for the next one */
327 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
328 s->last_row, s->pass_row_size, s->bpp);
329 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
332 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
333 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
334 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
335 s->color_type, s->last_row);
338 if (s->y == s->height) {
340 if (s->pass == NB_PASSES - 1) {
341 s->state |= PNG_ALLIMAGE;
346 s->pass_row_size = ff_png_pass_row_size(s->pass,
349 s->crow_size = s->pass_row_size + 1;
350 if (s->pass_row_size != 0)
352 /* skip pass if empty row */
361 static int png_decode_idat(PNGDecContext *s, int length)
364 s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
365 s->zstream.next_in = s->gb.buffer;
366 bytestream2_skip(&s->gb, length);
368 /* decode one line if possible */
369 while (s->zstream.avail_in > 0) {
370 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
371 if (ret != Z_OK && ret != Z_STREAM_END) {
374 if (s->zstream.avail_out == 0) {
375 if (!(s->state & PNG_ALLIMAGE)) {
378 s->zstream.avail_out = s->crow_size;
379 s->zstream.next_out = s->crow_buf;
385 static int decode_frame(AVCodecContext *avctx,
386 void *data, int *got_frame,
389 PNGDecContext * const s = avctx->priv_data;
390 const uint8_t *buf = avpkt->data;
391 int buf_size = avpkt->size;
393 uint8_t *crow_buf_base = NULL;
394 uint32_t tag, length;
397 /* check signature */
399 memcmp(buf, ff_pngsig, 8) != 0 &&
400 memcmp(buf, ff_mngsig, 8) != 0)
403 bytestream2_init(&s->gb, buf + 8, buf_size - 8);
407 s->zstream.zalloc = ff_png_zalloc;
408 s->zstream.zfree = ff_png_zfree;
409 s->zstream.opaque = NULL;
410 ret = inflateInit(&s->zstream);
414 if (bytestream2_get_bytes_left(&s->gb) <= 0)
416 length = bytestream2_get_be32(&s->gb);
417 if (length > 0x7fffffff)
419 tag = bytestream2_get_le32(&s->gb);
420 av_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
423 ((tag >> 16) & 0xff),
424 ((tag >> 24) & 0xff), length);
426 case MKTAG('I', 'H', 'D', 'R'):
429 s->width = bytestream2_get_be32(&s->gb);
430 s->height = bytestream2_get_be32(&s->gb);
431 if (av_image_check_size(s->width, s->height, 0, avctx)) {
432 s->width = s->height = 0;
435 s->bit_depth = bytestream2_get_byte(&s->gb);
436 s->color_type = bytestream2_get_byte(&s->gb);
437 s->compression_type = bytestream2_get_byte(&s->gb);
438 s->filter_type = bytestream2_get_byte(&s->gb);
439 s->interlace_type = bytestream2_get_byte(&s->gb);
440 bytestream2_skip(&s->gb, 4); /* crc */
441 s->state |= PNG_IHDR;
442 av_dlog(avctx, "width=%d height=%d depth=%d color_type=%d "
443 "compression_type=%d filter_type=%d interlace_type=%d\n",
444 s->width, s->height, s->bit_depth, s->color_type,
445 s->compression_type, s->filter_type, s->interlace_type);
447 case MKTAG('I', 'D', 'A', 'T'):
448 if (!(s->state & PNG_IHDR))
450 if (!(s->state & PNG_IDAT)) {
451 /* init image info */
452 avctx->width = s->width;
453 avctx->height = s->height;
455 s->channels = ff_png_get_nb_channels(s->color_type);
456 s->bits_per_pixel = s->bit_depth * s->channels;
457 s->bpp = (s->bits_per_pixel + 7) >> 3;
458 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
460 if (s->bit_depth == 8 &&
461 s->color_type == PNG_COLOR_TYPE_RGB) {
462 avctx->pix_fmt = AV_PIX_FMT_RGB24;
463 } else if (s->bit_depth == 8 &&
464 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
465 avctx->pix_fmt = AV_PIX_FMT_RGB32;
466 } else if (s->bit_depth == 8 &&
467 s->color_type == PNG_COLOR_TYPE_GRAY) {
468 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
469 } else if (s->bit_depth == 16 &&
470 s->color_type == PNG_COLOR_TYPE_GRAY) {
471 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
472 } else if (s->bit_depth == 16 &&
473 s->color_type == PNG_COLOR_TYPE_RGB) {
474 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
475 } else if (s->bit_depth == 1 &&
476 s->color_type == PNG_COLOR_TYPE_GRAY) {
477 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
478 } else if (s->bit_depth == 8 &&
479 s->color_type == PNG_COLOR_TYPE_PALETTE) {
480 avctx->pix_fmt = AV_PIX_FMT_PAL8;
481 } else if (s->bit_depth == 8 &&
482 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
483 avctx->pix_fmt = AV_PIX_FMT_Y400A;
488 if (ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF) < 0) {
489 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
492 p->pict_type = AV_PICTURE_TYPE_I;
494 p->interlaced_frame = !!s->interlace_type;
496 /* compute the compressed row size */
497 if (!s->interlace_type) {
498 s->crow_size = s->row_size + 1;
501 s->pass_row_size = ff_png_pass_row_size(s->pass,
504 s->crow_size = s->pass_row_size + 1;
506 av_dlog(avctx, "row_size=%d crow_size =%d\n",
507 s->row_size, s->crow_size);
508 s->image_buf = p->data[0];
509 s->image_linesize = p->linesize[0];
510 /* copy the palette if needed */
511 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
512 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
513 /* empty row is used if differencing to the first row */
514 s->last_row = av_mallocz(s->row_size);
517 if (s->interlace_type ||
518 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
519 s->tmp_row = av_malloc(s->row_size);
524 crow_buf_base = av_malloc(s->row_size + 16);
528 /* we want crow_buf+1 to be 16-byte aligned */
529 s->crow_buf = crow_buf_base + 15;
530 s->zstream.avail_out = s->crow_size;
531 s->zstream.next_out = s->crow_buf;
533 s->state |= PNG_IDAT;
534 if (png_decode_idat(s, length) < 0)
536 bytestream2_skip(&s->gb, 4); /* crc */
538 case MKTAG('P', 'L', 'T', 'E'):
542 if ((length % 3) != 0 || length > 256 * 3)
544 /* read the palette */
546 for (i = 0; i < n; i++) {
547 r = bytestream2_get_byte(&s->gb);
548 g = bytestream2_get_byte(&s->gb);
549 b = bytestream2_get_byte(&s->gb);
550 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
552 for (; i < 256; i++) {
553 s->palette[i] = (0xff << 24);
555 s->state |= PNG_PLTE;
556 bytestream2_skip(&s->gb, 4); /* crc */
559 case MKTAG('t', 'R', 'N', 'S'):
563 /* read the transparency. XXX: Only palette mode supported */
564 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
566 !(s->state & PNG_PLTE))
568 for (i = 0; i < length; i++) {
569 v = bytestream2_get_byte(&s->gb);
570 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
572 bytestream2_skip(&s->gb, 4); /* crc */
575 case MKTAG('I', 'E', 'N', 'D'):
576 if (!(s->state & PNG_ALLIMAGE))
578 bytestream2_skip(&s->gb, 4); /* crc */
583 bytestream2_skip(&s->gb, length + 4);
588 /* handle p-frames only if a predecessor frame is available */
589 if (s->prev->data[0]) {
590 if (!(avpkt->flags & AV_PKT_FLAG_KEY)) {
592 uint8_t *pd = p->data[0];
593 uint8_t *pd_last = s->prev->data[0];
595 for (j = 0; j < s->height; j++) {
596 for (i = 0; i < s->width * s->bpp; i++) {
599 pd += s->image_linesize;
600 pd_last += s->image_linesize;
605 av_frame_unref(s->prev);
606 if ((ret = av_frame_ref(s->prev, p)) < 0)
611 ret = bytestream2_tell(&s->gb);
613 inflateEnd(&s->zstream);
614 av_free(crow_buf_base);
616 av_freep(&s->last_row);
617 av_freep(&s->tmp_row);
624 static av_cold int png_dec_init(AVCodecContext *avctx)
626 PNGDecContext *s = avctx->priv_data;
628 s->prev = av_frame_alloc();
630 return AVERROR(ENOMEM);
632 ff_pngdsp_init(&s->dsp);
637 static av_cold int png_dec_end(AVCodecContext *avctx)
639 PNGDecContext *s = avctx->priv_data;
641 av_frame_free(&s->prev);
646 AVCodec ff_png_decoder = {
648 .type = AVMEDIA_TYPE_VIDEO,
649 .id = AV_CODEC_ID_PNG,
650 .priv_data_size = sizeof(PNGDecContext),
651 .init = png_dec_init,
652 .close = png_dec_end,
653 .decode = decode_frame,
654 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
655 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),