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, 0xff, 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 for(x = 0; x < width; x++) {
106 if ((dsp_mask << j) & 0x80) {
110 if ((mask << j) & 0x80)
117 // 0x7f7f7f7f or 0x7f7f7f7f7f7f7f7f or whatever, depending on the cpu's native arithmetic size
118 #define pb_7f (~0UL/255 * 0x7f)
119 #define pb_80 (~0UL/255 * 0x80)
121 static void add_bytes_l2_c(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
124 for(i=0; i<=w-sizeof(long); i+=sizeof(long)){
125 long a = *(long*)(src1+i);
126 long b = *(long*)(src2+i);
127 *(long*)(dst+i) = ((a&pb_7f) + (b&pb_7f)) ^ ((a^b)&pb_80);
130 dst[i] = src1[i]+src2[i];
133 static void add_paeth_prediction_c(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
136 for(i = 0; i < w; i++) {
137 int a, b, c, p, pa, pb, pc;
150 if (pa <= pb && pa <= pc)
160 #define UNROLL1(bpp, op) {\
162 if(bpp >= 2) g = dst[1];\
163 if(bpp >= 3) b = dst[2];\
164 if(bpp >= 4) a = dst[3];\
165 for(; i < size; i+=bpp) {\
166 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
167 if(bpp == 1) continue;\
168 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
169 if(bpp == 2) continue;\
170 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
171 if(bpp == 3) continue;\
172 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
176 #define UNROLL_FILTER(op)\
177 if(bpp == 1) UNROLL1(1, op)\
178 else if(bpp == 2) UNROLL1(2, op)\
179 else if(bpp == 3) UNROLL1(3, op)\
180 else if(bpp == 4) UNROLL1(4, op)\
182 for (; i < size; i += bpp) {\
184 for (j = 0; j < bpp; j++)\
185 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
189 /* NOTE: 'dst' can be equal to 'last' */
190 static void png_filter_row(PNGDecContext *s, uint8_t *dst, int filter_type,
191 uint8_t *src, uint8_t *last, int size, int bpp)
193 int i, p, r, g, b, a;
195 switch(filter_type) {
196 case PNG_FILTER_VALUE_NONE:
197 memcpy(dst, src, size);
199 case PNG_FILTER_VALUE_SUB:
200 for(i = 0; i < bpp; i++) {
205 for(; i < size; i+=bpp) {
206 int s = *(int*)(src+i);
207 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
211 #define OP_SUB(x,s,l) x+s
212 UNROLL_FILTER(OP_SUB);
215 case PNG_FILTER_VALUE_UP:
216 s->add_bytes_l2(dst, src, last, size);
218 case PNG_FILTER_VALUE_AVG:
219 for(i = 0; i < bpp; i++) {
223 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
224 UNROLL_FILTER(OP_AVG);
226 case PNG_FILTER_VALUE_PAETH:
227 for(i = 0; i < bpp; i++) {
231 if(bpp > 1 && size > 4) {
232 // would write off the end of the array if we let it process the last pixel with bpp=3
233 int w = bpp==4 ? size : size-3;
234 s->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
237 add_paeth_prediction_c(dst+i, src+i, last+i, size-i, bpp);
242 static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
245 unsigned int r, g, b, a;
247 for(j = 0;j < width; j++) {
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 memcpy(dst, src, width * 4);
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, 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, 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, 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 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) {
338 memset(s->last_row, 0, s->row_size);
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 = length;
365 s->zstream.next_in = s->bytestream;
366 s->bytestream += length;
368 if(s->bytestream > s->bytestream_end)
371 /* decode one line if possible */
372 while (s->zstream.avail_in > 0) {
373 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
374 if (ret != Z_OK && ret != Z_STREAM_END) {
377 if (s->zstream.avail_out == 0) {
378 if (!(s->state & PNG_ALLIMAGE)) {
381 s->zstream.avail_out = s->crow_size;
382 s->zstream.next_out = s->crow_buf;
388 static int decode_frame(AVCodecContext *avctx,
389 void *data, int *data_size,
392 const uint8_t *buf = avpkt->data;
393 int buf_size = avpkt->size;
394 PNGDecContext * const s = avctx->priv_data;
395 AVFrame *picture = data;
397 uint8_t *crow_buf_base = NULL;
398 uint32_t tag, length;
401 FFSWAP(AVFrame *, s->current_picture, s->last_picture);
402 avctx->coded_frame= s->current_picture;
403 p = s->current_picture;
407 s->bytestream_end= buf + buf_size;
409 /* check signature */
410 if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
411 memcmp(s->bytestream, ff_mngsig, 8) != 0)
416 // memset(s, 0, sizeof(PNGDecContext));
418 s->zstream.zalloc = ff_png_zalloc;
419 s->zstream.zfree = ff_png_zfree;
420 s->zstream.opaque = NULL;
421 ret = inflateInit(&s->zstream);
426 if (s->bytestream >= s->bytestream_end)
428 length = bytestream_get_be32(&s->bytestream);
429 if (length > 0x7fffffff)
431 tag32 = bytestream_get_be32(&s->bytestream);
432 tag = av_bswap32(tag32);
433 if (avctx->debug & FF_DEBUG_STARTCODE)
434 av_log(avctx, AV_LOG_DEBUG, "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 if (avctx->debug & FF_DEBUG_PICT_INFO)
457 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",
458 s->width, s->height, s->bit_depth, s->color_type,
459 s->compression_type, s->filter_type, s->interlace_type);
461 case MKTAG('I', 'D', 'A', 'T'):
462 if (!(s->state & PNG_IHDR))
464 if (!(s->state & PNG_IDAT)) {
465 /* init image info */
466 avctx->width = s->width;
467 avctx->height = s->height;
469 s->channels = ff_png_get_nb_channels(s->color_type);
470 s->bits_per_pixel = s->bit_depth * s->channels;
471 s->bpp = (s->bits_per_pixel + 7) >> 3;
472 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
474 if (s->bit_depth == 8 &&
475 s->color_type == PNG_COLOR_TYPE_RGB) {
476 avctx->pix_fmt = PIX_FMT_RGB24;
477 } else if (s->bit_depth == 8 &&
478 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
479 avctx->pix_fmt = PIX_FMT_RGBA;
480 } else if (s->bit_depth == 8 &&
481 s->color_type == PNG_COLOR_TYPE_GRAY) {
482 avctx->pix_fmt = PIX_FMT_GRAY8;
483 } else if (s->bit_depth == 16 &&
484 s->color_type == PNG_COLOR_TYPE_GRAY) {
485 avctx->pix_fmt = PIX_FMT_GRAY16BE;
486 } else if (s->bit_depth == 16 &&
487 s->color_type == PNG_COLOR_TYPE_RGB) {
488 avctx->pix_fmt = PIX_FMT_RGB48BE;
489 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
490 avctx->pix_fmt = PIX_FMT_PAL8;
491 } else if (s->bit_depth == 1) {
492 avctx->pix_fmt = PIX_FMT_MONOBLACK;
493 } else if (s->bit_depth == 8 &&
494 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
495 avctx->pix_fmt = PIX_FMT_GRAY8A;
497 av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
498 "and color type %d\n",
499 s->bit_depth, s->color_type);
503 avctx->release_buffer(avctx, p);
506 if(avctx->get_buffer(avctx, p) < 0){
507 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
510 p->pict_type= AV_PICTURE_TYPE_I;
512 p->interlaced_frame = !!s->interlace_type;
514 /* compute the compressed row size */
515 if (!s->interlace_type) {
516 s->crow_size = s->row_size + 1;
519 s->pass_row_size = ff_png_pass_row_size(s->pass,
522 s->crow_size = s->pass_row_size + 1;
524 av_dlog(avctx, "row_size=%d crow_size =%d\n",
525 s->row_size, s->crow_size);
526 s->image_buf = p->data[0];
527 s->image_linesize = p->linesize[0];
528 /* copy the palette if needed */
529 if (avctx->pix_fmt == PIX_FMT_PAL8)
530 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
531 /* empty row is used if differencing to the first row */
532 s->last_row = av_mallocz(s->row_size);
535 if (s->interlace_type ||
536 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
537 s->tmp_row = av_malloc(s->row_size);
542 crow_buf_base = av_malloc(s->row_size + 16);
546 /* we want crow_buf+1 to be 16-byte aligned */
547 s->crow_buf = crow_buf_base + 15;
548 s->zstream.avail_out = s->crow_size;
549 s->zstream.next_out = s->crow_buf;
551 s->state |= PNG_IDAT;
552 if (png_decode_idat(s, length) < 0)
554 s->bytestream += 4; /* crc */
556 case MKTAG('P', 'L', 'T', 'E'):
560 if ((length % 3) != 0 || length > 256 * 3)
562 /* read the palette */
565 r = *s->bytestream++;
566 g = *s->bytestream++;
567 b = *s->bytestream++;
568 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
571 s->palette[i] = (0xff << 24);
573 s->state |= PNG_PLTE;
574 s->bytestream += 4; /* crc */
577 case MKTAG('t', 'R', 'N', 'S'):
581 /* read the transparency. XXX: Only palette mode supported */
582 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
584 !(s->state & PNG_PLTE))
586 for(i=0;i<length;i++) {
587 v = *s->bytestream++;
588 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
590 s->bytestream += 4; /* crc */
593 case MKTAG('I', 'E', 'N', 'D'):
594 if (!(s->state & PNG_ALLIMAGE))
596 s->bytestream += 4; /* crc */
601 s->bytestream += length + 4;
607 if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
609 uint8_t *pd = s->current_picture->data[0];
610 for(j=0; j < s->height; j++) {
611 for(i=s->width/8-1; i>=0; i--) {
613 pd[8*i+6]= (pd[i]>>1)&1;
614 pd[8*i+5]= (pd[i]>>2)&1;
615 pd[8*i+4]= (pd[i]>>3)&1;
616 pd[8*i+3]= (pd[i]>>4)&1;
617 pd[8*i+2]= (pd[i]>>5)&1;
618 pd[8*i+1]= (pd[i]>>6)&1;
621 pd += s->image_linesize;
624 if(s->bits_per_pixel == 2){
626 uint8_t *pd = s->current_picture->data[0];
627 for(j=0; j < s->height; j++) {
628 for(i=s->width/4-1; i>=0; i--) {
630 pd[4*i+2]= (pd[i]>>2)&3;
631 pd[4*i+1]= (pd[i]>>4)&3;
634 pd += s->image_linesize;
637 if(s->bits_per_pixel == 4){
639 uint8_t *pd = s->current_picture->data[0];
640 for(j=0; j < s->height; j++) {
641 for(i=s->width/2-1; i>=0; i--) {
645 pd += s->image_linesize;
649 /* handle p-frames only if a predecessor frame is available */
650 if(s->last_picture->data[0] != NULL) {
651 if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
653 uint8_t *pd = s->current_picture->data[0];
654 uint8_t *pd_last = s->last_picture->data[0];
656 for(j=0; j < s->height; j++) {
657 for(i=0; i < s->width * s->bpp; i++) {
660 pd += s->image_linesize;
661 pd_last += s->image_linesize;
666 *picture= *s->current_picture;
667 *data_size = sizeof(AVFrame);
669 ret = s->bytestream - s->bytestream_start;
671 inflateEnd(&s->zstream);
672 av_free(crow_buf_base);
674 av_freep(&s->last_row);
675 av_freep(&s->tmp_row);
682 static av_cold int png_dec_init(AVCodecContext *avctx)
684 PNGDecContext *s = avctx->priv_data;
686 s->current_picture = &s->picture1;
687 s->last_picture = &s->picture2;
688 avcodec_get_frame_defaults(&s->picture1);
689 avcodec_get_frame_defaults(&s->picture2);
695 if (!s->add_paeth_prediction)
696 s->add_paeth_prediction = add_paeth_prediction_c;
697 if (!s->add_bytes_l2)
698 s->add_bytes_l2 = add_bytes_l2_c;
703 static av_cold int png_dec_end(AVCodecContext *avctx)
705 PNGDecContext *s = avctx->priv_data;
707 if (s->picture1.data[0])
708 avctx->release_buffer(avctx, &s->picture1);
709 if (s->picture2.data[0])
710 avctx->release_buffer(avctx, &s->picture2);
715 AVCodec ff_png_decoder = {
717 .type = AVMEDIA_TYPE_VIDEO,
719 .priv_data_size = sizeof(PNGDecContext),
720 .init = png_dec_init,
721 .close = png_dec_end,
722 .decode = decode_frame,
723 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
724 .long_name = NULL_IF_CONFIG_SMALL("PNG image"),