3 * Copyright (c) 2003 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * - add 2, 4 and 16 bit depth support
23 * - use filters when generating a png (better compression)
31 #define PNG_COLOR_MASK_PALETTE 1
32 #define PNG_COLOR_MASK_COLOR 2
33 #define PNG_COLOR_MASK_ALPHA 4
35 #define PNG_COLOR_TYPE_GRAY 0
36 #define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
37 #define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
38 #define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
39 #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
41 #define PNG_FILTER_VALUE_NONE 0
42 #define PNG_FILTER_VALUE_SUB 1
43 #define PNG_FILTER_VALUE_UP 2
44 #define PNG_FILTER_VALUE_AVG 3
45 #define PNG_FILTER_VALUE_PAETH 4
47 #define PNG_IHDR 0x0001
48 #define PNG_IDAT 0x0002
49 #define PNG_ALLIMAGE 0x0004
50 #define PNG_PLTE 0x0008
54 #define IOBUF_SIZE 4096
56 typedef struct PNGContext {
58 uint8_t *bytestream_start;
59 uint8_t *bytestream_end;
75 uint32_t palette[256];
80 int crow_size; /* compressed row size (include filter type) */
81 int row_size; /* decompressed row size */
82 int pass_row_size; /* decompress row size of the current pass */
85 uint8_t buf[IOBUF_SIZE];
88 static unsigned int get32(uint8_t **b){
90 return ((*b)[-4]<<24) + ((*b)[-3]<<16) + ((*b)[-2]<<8) + (*b)[-1];
93 static void put32(uint8_t **b, unsigned int v){
100 static const uint8_t pngsig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
102 /* Mask to determine which y pixels are valid in a pass */
103 static const uint8_t png_pass_ymask[NB_PASSES] = {
104 0x80, 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55,
107 /* Mask to determine which y pixels can be written in a pass */
108 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
109 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
112 /* minimum x value */
113 static const uint8_t png_pass_xmin[NB_PASSES] = {
117 /* x shift to get row width */
118 static const uint8_t png_pass_xshift[NB_PASSES] = {
122 /* Mask to determine which pixels are valid in a pass */
123 static const uint8_t png_pass_mask[NB_PASSES] = {
124 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff
127 /* Mask to determine which pixels to overwrite while displaying */
128 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
129 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
132 static int png_probe(AVProbeData *pd)
134 if (pd->buf_size >= 8 &&
135 memcmp(pd->buf, pngsig, 8) == 0)
136 return AVPROBE_SCORE_MAX;
141 static void *png_zalloc(void *opaque, unsigned int items, unsigned int size)
143 return av_malloc(items * size);
146 static void png_zfree(void *opaque, void *ptr)
151 static int png_get_nb_channels(int color_type)
155 if ((color_type & (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)) ==
156 PNG_COLOR_MASK_COLOR)
158 if (color_type & PNG_COLOR_MASK_ALPHA)
163 /* compute the row size of an interleaved pass */
164 static int png_pass_row_size(int pass, int bits_per_pixel, int width)
166 int shift, xmin, pass_width;
168 xmin = png_pass_xmin[pass];
171 shift = png_pass_xshift[pass];
172 pass_width = (width - xmin + (1 << shift) - 1) >> shift;
173 return (pass_width * bits_per_pixel + 7) >> 3;
176 /* NOTE: we try to construct a good looking image at each pass. width
177 is the original image width. We also do pixel format convertion at
179 static void png_put_interlaced_row(uint8_t *dst, int width,
180 int bits_per_pixel, int pass,
181 int color_type, const uint8_t *src)
183 int x, mask, dsp_mask, j, src_x, b, bpp;
187 mask = png_pass_mask[pass];
188 dsp_mask = png_pass_dsp_mask[pass];
189 switch(bits_per_pixel) {
191 /* we must intialize the line to zero before writing to it */
193 memset(dst, 0, (width + 7) >> 3);
195 for(x = 0; x < width; x++) {
197 if ((dsp_mask << j) & 0x80) {
198 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
199 dst[x >> 3] |= b << (7 - j);
201 if ((mask << j) & 0x80)
206 bpp = bits_per_pixel >> 3;
209 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
210 for(x = 0; x < width; x++) {
212 if ((dsp_mask << j) & 0x80) {
213 *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
216 if ((mask << j) & 0x80)
220 for(x = 0; x < width; x++) {
222 if ((dsp_mask << j) & 0x80) {
226 if ((mask << j) & 0x80)
234 static void png_get_interlaced_row(uint8_t *dst, int row_size,
235 int bits_per_pixel, int pass,
236 const uint8_t *src, int width)
238 int x, mask, dst_x, j, b, bpp;
242 mask = png_pass_mask[pass];
243 switch(bits_per_pixel) {
245 memset(dst, 0, row_size);
247 for(x = 0; x < width; x++) {
249 if ((mask << j) & 0x80) {
250 b = (src[x >> 3] >> (7 - j)) & 1;
251 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
257 bpp = bits_per_pixel >> 3;
260 for(x = 0; x < width; x++) {
262 if ((mask << j) & 0x80) {
273 /* NOTE: 'dst' can be equal to 'last' */
274 static void png_filter_row(uint8_t *dst, int filter_type,
275 uint8_t *src, uint8_t *last, int size, int bpp)
279 switch(filter_type) {
280 case PNG_FILTER_VALUE_NONE:
281 memcpy(dst, src, size);
283 case PNG_FILTER_VALUE_SUB:
284 for(i = 0; i < bpp; i++) {
287 for(i = bpp; i < size; i++) {
292 case PNG_FILTER_VALUE_UP:
293 for(i = 0; i < size; i++) {
298 case PNG_FILTER_VALUE_AVG:
299 for(i = 0; i < bpp; i++) {
303 for(i = bpp; i < size; i++) {
304 p = ((dst[i - bpp] + last[i]) >> 1);
308 case PNG_FILTER_VALUE_PAETH:
309 for(i = 0; i < bpp; i++) {
313 for(i = bpp; i < size; i++) {
314 int a, b, c, pa, pb, pc;
327 if (pa <= pb && pa <= pc)
339 static void convert_from_rgba32(uint8_t *dst, const uint8_t *src, int width)
346 for(j = 0; j < width; j++) {
347 v = ((uint32_t *)src)[j];
356 static void convert_to_rgba32(uint8_t *dst, const uint8_t *src, int width)
359 unsigned int r, g, b, a;
361 for(j = 0;j < width; j++) {
366 *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
372 /* process exactly one decompressed row */
373 static void png_handle_row(PNGContext *s)
375 uint8_t *ptr, *last_row;
378 if (!s->interlace_type) {
379 ptr = s->image_buf + s->image_linesize * s->y;
380 /* need to swap bytes correctly for RGB_ALPHA */
381 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
382 png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
383 s->last_row, s->row_size, s->bpp);
384 memcpy(s->last_row, s->tmp_row, s->row_size);
385 convert_to_rgba32(ptr, s->tmp_row, s->width);
387 /* in normal case, we avoid one copy */
389 last_row = s->last_row;
391 last_row = ptr - s->image_linesize;
393 png_filter_row(ptr, s->crow_buf[0], s->crow_buf + 1,
394 last_row, s->row_size, s->bpp);
397 if (s->y == s->height) {
398 s->state |= PNG_ALLIMAGE;
403 ptr = s->image_buf + s->image_linesize * s->y;
404 if ((png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
405 /* if we already read one row, it is time to stop to
406 wait for the next one */
409 png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
410 s->last_row, s->pass_row_size, s->bpp);
411 memcpy(s->last_row, s->tmp_row, s->pass_row_size);
414 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
415 /* NOTE: rgba32 is handled directly in png_put_interlaced_row */
416 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
417 s->color_type, s->last_row);
420 if (s->y == s->height) {
422 if (s->pass == NB_PASSES - 1) {
423 s->state |= PNG_ALLIMAGE;
428 s->pass_row_size = png_pass_row_size(s->pass,
431 s->crow_size = s->pass_row_size + 1;
432 if (s->pass_row_size != 0)
434 /* skip pass if empty row */
443 static int png_decode_idat(PNGContext *s, int length)
446 s->zstream.avail_in = length;
447 s->zstream.next_in = s->bytestream;
448 s->bytestream += length;
450 if(s->bytestream > s->bytestream_end)
453 /* decode one line if possible */
454 while (s->zstream.avail_in > 0) {
455 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
456 if (ret != Z_OK && ret != Z_STREAM_END) {
459 if (s->zstream.avail_out == 0) {
460 if (!(s->state & PNG_ALLIMAGE)) {
463 s->zstream.avail_out = s->crow_size;
464 s->zstream.next_out = s->crow_buf;
470 static int decode_frame(AVCodecContext *avctx,
471 void *data, int *data_size,
472 uint8_t *buf, int buf_size)
474 PNGContext * const s = avctx->priv_data;
475 AVFrame *picture = data;
476 AVFrame * const p= (AVFrame*)&s->picture;
477 uint32_t tag, length;
480 /* special case for last picture */
487 s->bytestream_end= buf + buf_size;
489 /* check signature */
490 if (memcmp(s->bytestream, pngsig, 8) != 0)
495 // memset(s, 0, sizeof(PNGContext));
497 s->zstream.zalloc = png_zalloc;
498 s->zstream.zfree = png_zfree;
499 s->zstream.opaque = NULL;
500 ret = inflateInit(&s->zstream);
504 if (s->bytestream >= s->bytestream_end)
506 length = get32(&s->bytestream);
507 if (length > 0x7fffffff)
509 tag = bswap_32(get32(&s->bytestream));
511 printf("png: tag=%c%c%c%c length=%u\n",
514 ((tag >> 16) & 0xff),
515 ((tag >> 24) & 0xff), length);
518 case MKTAG('I', 'H', 'D', 'R'):
521 s->width = get32(&s->bytestream);
522 s->height = get32(&s->bytestream);
523 s->bit_depth = *s->bytestream++;
524 s->color_type = *s->bytestream++;
525 s->compression_type = *s->bytestream++;
526 s->filter_type = *s->bytestream++;
527 s->interlace_type = *s->bytestream++;
528 crc = get32(&s->bytestream);
529 s->state |= PNG_IHDR;
531 printf("width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
532 s->width, s->height, s->bit_depth, s->color_type,
533 s->compression_type, s->filter_type, s->interlace_type);
536 case MKTAG('I', 'D', 'A', 'T'):
537 if (!(s->state & PNG_IHDR))
539 if (!(s->state & PNG_IDAT)) {
540 /* init image info */
541 avctx->width = s->width;
542 avctx->height = s->height;
544 s->channels = png_get_nb_channels(s->color_type);
545 s->bits_per_pixel = s->bit_depth * s->channels;
546 s->bpp = (s->bits_per_pixel + 7) >> 3;
547 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
549 if (s->bit_depth == 8 &&
550 s->color_type == PNG_COLOR_TYPE_RGB) {
551 avctx->pix_fmt = PIX_FMT_RGB24;
552 } else if (s->bit_depth == 8 &&
553 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
554 avctx->pix_fmt = PIX_FMT_RGBA32;
555 } else if (s->bit_depth == 8 &&
556 s->color_type == PNG_COLOR_TYPE_GRAY) {
557 avctx->pix_fmt = PIX_FMT_GRAY8;
558 } else if (s->bit_depth == 1 &&
559 s->color_type == PNG_COLOR_TYPE_GRAY) {
560 avctx->pix_fmt = PIX_FMT_MONOBLACK;
561 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
562 avctx->pix_fmt = PIX_FMT_PAL8;
567 avctx->release_buffer(avctx, p);
570 if(avctx->get_buffer(avctx, p) < 0){
571 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
574 p->pict_type= FF_I_TYPE;
576 p->interlaced_frame = !!s->interlace_type;
578 /* compute the compressed row size */
579 if (!s->interlace_type) {
580 s->crow_size = s->row_size + 1;
583 s->pass_row_size = png_pass_row_size(s->pass,
586 s->crow_size = s->pass_row_size + 1;
589 printf("row_size=%d crow_size =%d\n",
590 s->row_size, s->crow_size);
592 s->image_buf = p->data[0];
593 s->image_linesize = p->linesize[0];
594 /* copy the palette if needed */
595 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
596 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
597 /* empty row is used if differencing to the first row */
598 s->last_row = av_mallocz(s->row_size);
601 if (s->interlace_type ||
602 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
603 s->tmp_row = av_malloc(s->row_size);
608 s->crow_buf = av_malloc(s->row_size + 1);
611 s->zstream.avail_out = s->crow_size;
612 s->zstream.next_out = s->crow_buf;
614 s->state |= PNG_IDAT;
615 if (png_decode_idat(s, length) < 0)
618 crc = get32(&s->bytestream);
620 case MKTAG('P', 'L', 'T', 'E'):
624 if ((length % 3) != 0 || length > 256 * 3)
626 /* read the palette */
629 r = *s->bytestream++;
630 g = *s->bytestream++;
631 b = *s->bytestream++;
632 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
635 s->palette[i] = (0xff << 24);
637 s->state |= PNG_PLTE;
638 crc = get32(&s->bytestream);
641 case MKTAG('t', 'R', 'N', 'S'):
645 /* read the transparency. XXX: Only palette mode supported */
646 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
648 !(s->state & PNG_PLTE))
650 for(i=0;i<length;i++) {
651 v = *s->bytestream++;
652 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
654 crc = get32(&s->bytestream);
657 case MKTAG('I', 'E', 'N', 'D'):
658 if (!(s->state & PNG_ALLIMAGE))
660 crc = get32(&s->bytestream);
665 s->bytestream += length + 4;
670 *picture= *(AVFrame*)&s->picture;
671 *data_size = sizeof(AVPicture);
673 ret = s->bytestream - s->bytestream_start;
675 inflateEnd(&s->zstream);
676 av_freep(&s->crow_buf);
677 av_freep(&s->last_row);
678 av_freep(&s->tmp_row);
685 static void png_write_chunk(uint8_t **f, uint32_t tag,
686 const uint8_t *buf, int length)
692 crc = crc32(0, Z_NULL, 0);
694 tagbuf[1] = tag >> 8;
695 tagbuf[2] = tag >> 16;
696 tagbuf[3] = tag >> 24;
697 crc = crc32(crc, tagbuf, 4);
698 put32(f, bswap_32(tag));
700 crc = crc32(crc, buf, length);
701 memcpy(*f, buf, length);
707 /* XXX: use avcodec generic function ? */
708 static void to_be32(uint8_t *p, uint32_t v)
716 /* XXX: do filtering */
717 static int png_write_row(PNGContext *s, const uint8_t *data, int size)
721 s->zstream.avail_in = size;
722 s->zstream.next_in = (uint8_t *)data;
723 while (s->zstream.avail_in > 0) {
724 ret = deflate(&s->zstream, Z_NO_FLUSH);
727 if (s->zstream.avail_out == 0) {
728 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
729 s->zstream.avail_out = IOBUF_SIZE;
730 s->zstream.next_out = s->buf;
736 static int common_init(AVCodecContext *avctx){
737 PNGContext *s = avctx->priv_data;
739 avcodec_get_frame_defaults((AVFrame*)&s->picture);
740 avctx->coded_frame= (AVFrame*)&s->picture;
746 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
747 PNGContext *s = avctx->priv_data;
748 AVFrame *pict = data;
749 AVFrame * const p= (AVFrame*)&s->picture;
750 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
751 int bits_per_pixel, pass_row_size;
753 uint8_t *crow_buf = NULL;
754 uint8_t *tmp_buf = NULL;
757 p->pict_type= FF_I_TYPE;
762 s->bytestream_end= buf+buf_size;
764 is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
765 switch(avctx->pix_fmt) {
768 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
772 color_type = PNG_COLOR_TYPE_RGB;
776 color_type = PNG_COLOR_TYPE_GRAY;
778 case PIX_FMT_MONOBLACK:
780 color_type = PNG_COLOR_TYPE_GRAY;
784 color_type = PNG_COLOR_TYPE_PALETTE;
789 bits_per_pixel = png_get_nb_channels(color_type) * bit_depth;
790 row_size = (avctx->width * bits_per_pixel + 7) >> 3;
792 s->zstream.zalloc = png_zalloc;
793 s->zstream.zfree = png_zfree;
794 s->zstream.opaque = NULL;
795 ret = deflateInit2(&s->zstream, Z_DEFAULT_COMPRESSION,
796 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
799 crow_buf = av_malloc(row_size + 1);
802 if (is_progressive) {
803 tmp_buf = av_malloc(row_size + 1);
808 /* write png header */
809 memcpy(s->bytestream, pngsig, 8);
812 to_be32(s->buf, avctx->width);
813 to_be32(s->buf + 4, avctx->height);
814 s->buf[8] = bit_depth;
815 s->buf[9] = color_type;
816 s->buf[10] = 0; /* compression type */
817 s->buf[11] = 0; /* filter type */
818 s->buf[12] = is_progressive; /* interlace type */
820 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
822 /* put the palette if needed */
823 if (color_type == PNG_COLOR_TYPE_PALETTE) {
824 int has_alpha, alpha, i;
829 palette = (uint32_t *)p->data[1];
831 alpha_ptr = s->buf + 256 * 3;
833 for(i = 0; i < 256; i++) {
838 *alpha_ptr++ = alpha;
844 png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
846 png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
850 /* now put each row */
851 s->zstream.avail_out = IOBUF_SIZE;
852 s->zstream.next_out = s->buf;
853 if (is_progressive) {
857 for(pass = 0; pass < NB_PASSES; pass++) {
858 /* NOTE: a pass is completely omited if no pixels would be
860 pass_row_size = png_pass_row_size(pass, bits_per_pixel, avctx->width);
861 if (pass_row_size > 0) {
862 for(y = 0; y < avctx->height; y++) {
863 if ((png_pass_ymask[pass] << (y & 7)) & 0x80) {
864 ptr = p->data[0] + y * p->linesize[0];
865 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
866 convert_from_rgba32(tmp_buf, ptr, avctx->width);
871 png_get_interlaced_row(crow_buf + 1, pass_row_size,
872 bits_per_pixel, pass,
874 crow_buf[0] = PNG_FILTER_VALUE_NONE;
875 png_write_row(s, crow_buf, pass_row_size + 1);
881 for(y = 0; y < avctx->height; y++) {
882 ptr = p->data[0] + y * p->linesize[0];
883 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
884 convert_from_rgba32(crow_buf + 1, ptr, avctx->width);
886 memcpy(crow_buf + 1, ptr, row_size);
887 crow_buf[0] = PNG_FILTER_VALUE_NONE;
888 png_write_row(s, crow_buf, row_size + 1);
891 /* compress last bytes */
893 ret = deflate(&s->zstream, Z_FINISH);
894 if (ret == Z_OK || ret == Z_STREAM_END) {
895 len = IOBUF_SIZE - s->zstream.avail_out;
897 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
899 s->zstream.avail_out = IOBUF_SIZE;
900 s->zstream.next_out = s->buf;
901 if (ret == Z_STREAM_END)
907 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
909 ret = s->bytestream - s->bytestream_start;
913 deflateEnd(&s->zstream);
920 AVCodec png_decoder = {
929 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
933 AVCodec png_encoder = {
941 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, -1},