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"
30 * - add 16 bit depth support
35 /* Mask to determine which y pixels can be written in a pass */
36 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
37 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
40 /* Mask to determine which pixels to overwrite while displaying */
41 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
42 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
45 /* NOTE: we try to construct a good looking image at each pass. width
46 is the original image width. We also do pixel format conversion at
48 static void png_put_interlaced_row(uint8_t *dst, int width,
49 int bits_per_pixel, int pass,
50 int color_type, const uint8_t *src)
52 int x, mask, dsp_mask, j, src_x, b, bpp;
56 mask = ff_png_pass_mask[pass];
57 dsp_mask = png_pass_dsp_mask[pass];
58 switch(bits_per_pixel) {
61 for(x = 0; x < width; x++) {
63 if ((dsp_mask << j) & 0x80) {
64 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
65 dst[x >> 3] &= 0xFF7F>>j;
66 dst[x >> 3] |= b << (7 - j);
68 if ((mask << j) & 0x80)
74 for(x = 0; x < width; x++) {
77 if ((dsp_mask << j) & 0x80) {
78 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
79 dst[x >> 2] &= 0xFF3F>>j2;
80 dst[x >> 2] |= b << (6 - j2);
82 if ((mask << j) & 0x80)
88 for(x = 0; x < width; x++) {
91 if ((dsp_mask << j) & 0x80) {
92 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
93 dst[x >> 1] &= 0xFF0F>>j2;
94 dst[x >> 1] |= b << (4 - j2);
96 if ((mask << j) & 0x80)
101 bpp = bits_per_pixel >> 3;
104 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
105 for(x = 0; x < width; x++) {
107 if ((dsp_mask << j) & 0x80) {
108 *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
111 if ((mask << j) & 0x80)
115 for(x = 0; x < width; x++) {
117 if ((dsp_mask << j) & 0x80) {
121 if ((mask << j) & 0x80)
129 // 0x7f7f7f7f or 0x7f7f7f7f7f7f7f7f or whatever, depending on the cpu's native arithmetic size
130 #define pb_7f (~0UL/255 * 0x7f)
131 #define pb_80 (~0UL/255 * 0x80)
133 static void add_bytes_l2_c(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
136 for(i=0; i<=w-sizeof(long); i+=sizeof(long)){
137 long a = *(long*)(src1+i);
138 long b = *(long*)(src2+i);
139 *(long*)(dst+i) = ((a&pb_7f) + (b&pb_7f)) ^ ((a^b)&pb_80);
142 dst[i] = src1[i]+src2[i];
145 static void add_paeth_prediction_c(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
148 for(i = 0; i < w; i++) {
149 int a, b, c, p, pa, pb, pc;
162 if (pa <= pb && pa <= pc)
172 #define UNROLL1(bpp, op) {\
174 if(bpp >= 2) g = dst[1];\
175 if(bpp >= 3) b = dst[2];\
176 if(bpp >= 4) a = dst[3];\
177 for(; i < size; i+=bpp) {\
178 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
179 if(bpp == 1) continue;\
180 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
181 if(bpp == 2) continue;\
182 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
183 if(bpp == 3) continue;\
184 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
188 #define UNROLL_FILTER(op)\
189 if(bpp == 1) UNROLL1(1, op)\
190 else if(bpp == 2) UNROLL1(2, op)\
191 else if(bpp == 3) UNROLL1(3, op)\
192 else if(bpp == 4) UNROLL1(4, op)\
194 for (; i < size; i += bpp) {\
196 for (j = 0; j < bpp; j++)\
197 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
201 /* NOTE: 'dst' can be equal to 'last' */
202 static void png_filter_row(PNGDecContext *s, uint8_t *dst, int filter_type,
203 uint8_t *src, uint8_t *last, int size, int bpp)
205 int i, p, r, g, b, a;
207 switch(filter_type) {
208 case PNG_FILTER_VALUE_NONE:
209 memcpy(dst, src, size);
211 case PNG_FILTER_VALUE_SUB:
212 for(i = 0; i < bpp; i++) {
217 for(; i < size; i+=bpp) {
218 int s = *(int*)(src+i);
219 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
223 #define OP_SUB(x,s,l) x+s
224 UNROLL_FILTER(OP_SUB);
227 case PNG_FILTER_VALUE_UP:
228 s->add_bytes_l2(dst, src, last, size);
230 case PNG_FILTER_VALUE_AVG:
231 for(i = 0; i < bpp; i++) {
235 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
236 UNROLL_FILTER(OP_AVG);
238 case PNG_FILTER_VALUE_PAETH:
239 for(i = 0; i < bpp; i++) {
243 if(bpp > 1 && size > 4) {
244 // would write off the end of the array if we let it process the last pixel with bpp=3
245 int w = bpp==4 ? size : size-3;
246 s->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
249 add_paeth_prediction_c(dst+i, src+i, last+i, size-i, bpp);
254 static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
257 unsigned int r, g, b, a;
259 for(j = 0;j < width; j++) {
268 *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
274 static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
277 convert_to_rgb32_loco(dst, src, width, 1);
279 convert_to_rgb32_loco(dst, src, width, 0);
282 static void deloco_rgb24(uint8_t *dst, int size)
285 for(i=0; i<size; i+=3) {
292 /* process exactly one decompressed row */
293 static void png_handle_row(PNGDecContext *s)
295 uint8_t *ptr, *last_row;
298 if (!s->interlace_type) {
299 ptr = s->image_buf + s->image_linesize * s->y;
300 /* need to swap bytes correctly for RGB_ALPHA */
301 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
302 png_filter_row(s, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
303 s->last_row, s->row_size, s->bpp);
304 convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
305 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
307 /* in normal case, we avoid one copy */
309 last_row = s->last_row;
311 last_row = ptr - s->image_linesize;
313 png_filter_row(s, ptr, s->crow_buf[0], s->crow_buf + 1,
314 last_row, s->row_size, s->bpp);
316 /* loco lags by 1 row so that it doesn't interfere with top prediction */
317 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
318 s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
319 deloco_rgb24(ptr - s->image_linesize, s->row_size);
321 if (s->y == s->height) {
322 s->state |= PNG_ALLIMAGE;
323 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
324 s->color_type == PNG_COLOR_TYPE_RGB)
325 deloco_rgb24(ptr, s->row_size);
330 ptr = s->image_buf + s->image_linesize * s->y;
331 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
332 /* if we already read one row, it is time to stop to
333 wait for the next one */
336 png_filter_row(s, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
337 s->last_row, s->pass_row_size, s->bpp);
338 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
341 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
342 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
343 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
344 s->color_type, s->last_row);
347 if (s->y == s->height) {
348 memset(s->last_row, 0, s->row_size);
350 if (s->pass == NB_PASSES - 1) {
351 s->state |= PNG_ALLIMAGE;
356 s->pass_row_size = ff_png_pass_row_size(s->pass,
359 s->crow_size = s->pass_row_size + 1;
360 if (s->pass_row_size != 0)
362 /* skip pass if empty row */
371 static int png_decode_idat(PNGDecContext *s, int length)
374 s->zstream.avail_in = length;
375 s->zstream.next_in = s->bytestream;
376 s->bytestream += length;
378 if(s->bytestream > s->bytestream_end)
381 /* decode one line if possible */
382 while (s->zstream.avail_in > 0) {
383 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
384 if (ret != Z_OK && ret != Z_STREAM_END) {
387 if (s->zstream.avail_out == 0) {
388 if (!(s->state & PNG_ALLIMAGE)) {
391 s->zstream.avail_out = s->crow_size;
392 s->zstream.next_out = s->crow_buf;
398 static int decode_frame(AVCodecContext *avctx,
399 void *data, int *data_size,
402 const uint8_t *buf = avpkt->data;
403 int buf_size = avpkt->size;
404 PNGDecContext * const s = avctx->priv_data;
405 AVFrame *picture = data;
407 uint8_t *crow_buf_base = NULL;
408 uint32_t tag, length;
411 FFSWAP(AVFrame *, s->current_picture, s->last_picture);
412 avctx->coded_frame= s->current_picture;
413 p = s->current_picture;
417 s->bytestream_end= buf + buf_size;
419 /* check signature */
420 if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
421 memcmp(s->bytestream, ff_mngsig, 8) != 0)
426 // memset(s, 0, sizeof(PNGDecContext));
428 s->zstream.zalloc = ff_png_zalloc;
429 s->zstream.zfree = ff_png_zfree;
430 s->zstream.opaque = NULL;
431 ret = inflateInit(&s->zstream);
436 if (s->bytestream >= s->bytestream_end)
438 length = bytestream_get_be32(&s->bytestream);
439 if (length > 0x7fffffff)
441 tag32 = bytestream_get_be32(&s->bytestream);
442 tag = av_bswap32(tag32);
443 av_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
446 ((tag >> 16) & 0xff),
447 ((tag >> 24) & 0xff), length);
449 case MKTAG('I', 'H', 'D', 'R'):
452 s->width = bytestream_get_be32(&s->bytestream);
453 s->height = bytestream_get_be32(&s->bytestream);
454 if(av_image_check_size(s->width, s->height, 0, avctx)){
455 s->width= s->height= 0;
458 s->bit_depth = *s->bytestream++;
459 s->color_type = *s->bytestream++;
460 s->compression_type = *s->bytestream++;
461 s->filter_type = *s->bytestream++;
462 s->interlace_type = *s->bytestream++;
463 s->bytestream += 4; /* crc */
464 s->state |= PNG_IHDR;
465 av_dlog(avctx, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
466 s->width, s->height, s->bit_depth, s->color_type,
467 s->compression_type, s->filter_type, s->interlace_type);
469 case MKTAG('I', 'D', 'A', 'T'):
470 if (!(s->state & PNG_IHDR))
472 if (!(s->state & PNG_IDAT)) {
473 /* init image info */
474 avctx->width = s->width;
475 avctx->height = s->height;
477 s->channels = ff_png_get_nb_channels(s->color_type);
478 s->bits_per_pixel = s->bit_depth * s->channels;
479 s->bpp = (s->bits_per_pixel + 7) >> 3;
480 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
482 if (s->bit_depth == 8 &&
483 s->color_type == PNG_COLOR_TYPE_RGB) {
484 avctx->pix_fmt = PIX_FMT_RGB24;
485 } else if (s->bit_depth == 8 &&
486 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
487 avctx->pix_fmt = PIX_FMT_RGB32;
488 } else if (s->bit_depth == 8 &&
489 s->color_type == PNG_COLOR_TYPE_GRAY) {
490 avctx->pix_fmt = PIX_FMT_GRAY8;
491 } else if (s->bit_depth == 16 &&
492 s->color_type == PNG_COLOR_TYPE_GRAY) {
493 avctx->pix_fmt = PIX_FMT_GRAY16BE;
494 } else if (s->bit_depth == 16 &&
495 s->color_type == PNG_COLOR_TYPE_RGB) {
496 avctx->pix_fmt = PIX_FMT_RGB48BE;
497 } else if (s->bit_depth == 1) {
498 avctx->pix_fmt = PIX_FMT_MONOBLACK;
499 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
500 avctx->pix_fmt = PIX_FMT_PAL8;
501 } else if (s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
502 avctx->pix_fmt = PIX_FMT_GRAY8A;
507 avctx->release_buffer(avctx, p);
510 if(avctx->get_buffer(avctx, p) < 0){
511 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
514 p->pict_type= AV_PICTURE_TYPE_I;
516 p->interlaced_frame = !!s->interlace_type;
518 /* compute the compressed row size */
519 if (!s->interlace_type) {
520 s->crow_size = s->row_size + 1;
523 s->pass_row_size = ff_png_pass_row_size(s->pass,
526 s->crow_size = s->pass_row_size + 1;
528 av_dlog(avctx, "row_size=%d crow_size =%d\n",
529 s->row_size, s->crow_size);
530 s->image_buf = p->data[0];
531 s->image_linesize = p->linesize[0];
532 /* copy the palette if needed */
533 if (avctx->pix_fmt == PIX_FMT_PAL8)
534 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
535 /* empty row is used if differencing to the first row */
536 s->last_row = av_mallocz(s->row_size);
539 if (s->interlace_type ||
540 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
541 s->tmp_row = av_malloc(s->row_size);
546 crow_buf_base = av_malloc(s->row_size + 16);
550 /* we want crow_buf+1 to be 16-byte aligned */
551 s->crow_buf = crow_buf_base + 15;
552 s->zstream.avail_out = s->crow_size;
553 s->zstream.next_out = s->crow_buf;
555 s->state |= PNG_IDAT;
556 if (png_decode_idat(s, length) < 0)
558 s->bytestream += 4; /* crc */
560 case MKTAG('P', 'L', 'T', 'E'):
564 if ((length % 3) != 0 || length > 256 * 3)
566 /* read the palette */
569 r = *s->bytestream++;
570 g = *s->bytestream++;
571 b = *s->bytestream++;
572 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
575 s->palette[i] = (0xff << 24);
577 s->state |= PNG_PLTE;
578 s->bytestream += 4; /* crc */
581 case MKTAG('t', 'R', 'N', 'S'):
585 /* read the transparency. XXX: Only palette mode supported */
586 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
588 !(s->state & PNG_PLTE))
590 for(i=0;i<length;i++) {
591 v = *s->bytestream++;
592 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
594 s->bytestream += 4; /* crc */
597 case MKTAG('I', 'E', 'N', 'D'):
598 if (!(s->state & PNG_ALLIMAGE))
600 s->bytestream += 4; /* crc */
605 s->bytestream += length + 4;
611 if(s->bits_per_pixel == 2){
613 uint8_t *pd = s->current_picture->data[0];
614 for(j=0; j < s->height; j++) {
615 for(i=s->width/4-1; i>=0; i--) {
617 pd[4*i+2]= (pd[i]>>2)&3;
618 pd[4*i+1]= (pd[i]>>4)&3;
621 pd += s->image_linesize;
624 if(s->bits_per_pixel == 4){
626 uint8_t *pd = s->current_picture->data[0];
627 for(j=0; j < s->height; j++) {
628 for(i=s->width/2-1; i>=0; i--) {
632 pd += s->image_linesize;
636 /* handle p-frames only if a predecessor frame is available */
637 if(s->last_picture->data[0] != NULL) {
638 if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
640 uint8_t *pd = s->current_picture->data[0];
641 uint8_t *pd_last = s->last_picture->data[0];
643 for(j=0; j < s->height; j++) {
644 for(i=0; i < s->width * s->bpp; i++) {
647 pd += s->image_linesize;
648 pd_last += s->image_linesize;
653 *picture= *s->current_picture;
654 *data_size = sizeof(AVFrame);
656 ret = s->bytestream - s->bytestream_start;
658 inflateEnd(&s->zstream);
659 av_free(crow_buf_base);
661 av_freep(&s->last_row);
662 av_freep(&s->tmp_row);
669 static av_cold int png_dec_init(AVCodecContext *avctx)
671 PNGDecContext *s = avctx->priv_data;
673 s->current_picture = &s->picture1;
674 s->last_picture = &s->picture2;
675 avcodec_get_frame_defaults(&s->picture1);
676 avcodec_get_frame_defaults(&s->picture2);
682 if (!s->add_paeth_prediction)
683 s->add_paeth_prediction = add_paeth_prediction_c;
684 if (!s->add_bytes_l2)
685 s->add_bytes_l2 = add_bytes_l2_c;
690 static av_cold int png_dec_end(AVCodecContext *avctx)
692 PNGDecContext *s = avctx->priv_data;
694 if (s->picture1.data[0])
695 avctx->release_buffer(avctx, &s->picture1);
696 if (s->picture2.data[0])
697 avctx->release_buffer(avctx, &s->picture2);
702 AVCodec ff_png_decoder = {
704 .type = AVMEDIA_TYPE_VIDEO,
706 .priv_data_size = sizeof(PNGDecContext),
707 .init = png_dec_init,
708 .close = png_dec_end,
709 .decode = decode_frame,
710 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
711 .long_name = NULL_IF_CONFIG_SMALL("PNG image"),