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);
505 if (s->bytestream >= s->bytestream_end)
507 length = get32(&s->bytestream);
508 if (length > 0x7fffffff)
510 tag32 = get32(&s->bytestream);
511 tag = bswap_32(tag32);
513 printf("png: tag=%c%c%c%c length=%u\n",
516 ((tag >> 16) & 0xff),
517 ((tag >> 24) & 0xff), length);
520 case MKTAG('I', 'H', 'D', 'R'):
523 s->width = get32(&s->bytestream);
524 s->height = get32(&s->bytestream);
525 s->bit_depth = *s->bytestream++;
526 s->color_type = *s->bytestream++;
527 s->compression_type = *s->bytestream++;
528 s->filter_type = *s->bytestream++;
529 s->interlace_type = *s->bytestream++;
530 crc = get32(&s->bytestream);
531 s->state |= PNG_IHDR;
533 printf("width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
534 s->width, s->height, s->bit_depth, s->color_type,
535 s->compression_type, s->filter_type, s->interlace_type);
538 case MKTAG('I', 'D', 'A', 'T'):
539 if (!(s->state & PNG_IHDR))
541 if (!(s->state & PNG_IDAT)) {
542 /* init image info */
543 avctx->width = s->width;
544 avctx->height = s->height;
546 s->channels = png_get_nb_channels(s->color_type);
547 s->bits_per_pixel = s->bit_depth * s->channels;
548 s->bpp = (s->bits_per_pixel + 7) >> 3;
549 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
551 if (s->bit_depth == 8 &&
552 s->color_type == PNG_COLOR_TYPE_RGB) {
553 avctx->pix_fmt = PIX_FMT_RGB24;
554 } else if (s->bit_depth == 8 &&
555 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
556 avctx->pix_fmt = PIX_FMT_RGBA32;
557 } else if (s->bit_depth == 8 &&
558 s->color_type == PNG_COLOR_TYPE_GRAY) {
559 avctx->pix_fmt = PIX_FMT_GRAY8;
560 } else if (s->bit_depth == 1 &&
561 s->color_type == PNG_COLOR_TYPE_GRAY) {
562 avctx->pix_fmt = PIX_FMT_MONOBLACK;
563 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
564 avctx->pix_fmt = PIX_FMT_PAL8;
569 avctx->release_buffer(avctx, p);
572 if(avctx->get_buffer(avctx, p) < 0){
573 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
576 p->pict_type= FF_I_TYPE;
578 p->interlaced_frame = !!s->interlace_type;
580 /* compute the compressed row size */
581 if (!s->interlace_type) {
582 s->crow_size = s->row_size + 1;
585 s->pass_row_size = png_pass_row_size(s->pass,
588 s->crow_size = s->pass_row_size + 1;
591 printf("row_size=%d crow_size =%d\n",
592 s->row_size, s->crow_size);
594 s->image_buf = p->data[0];
595 s->image_linesize = p->linesize[0];
596 /* copy the palette if needed */
597 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
598 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
599 /* empty row is used if differencing to the first row */
600 s->last_row = av_mallocz(s->row_size);
603 if (s->interlace_type ||
604 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
605 s->tmp_row = av_malloc(s->row_size);
610 s->crow_buf = av_malloc(s->row_size + 1);
613 s->zstream.avail_out = s->crow_size;
614 s->zstream.next_out = s->crow_buf;
616 s->state |= PNG_IDAT;
617 if (png_decode_idat(s, length) < 0)
620 crc = get32(&s->bytestream);
622 case MKTAG('P', 'L', 'T', 'E'):
626 if ((length % 3) != 0 || length > 256 * 3)
628 /* read the palette */
631 r = *s->bytestream++;
632 g = *s->bytestream++;
633 b = *s->bytestream++;
634 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
637 s->palette[i] = (0xff << 24);
639 s->state |= PNG_PLTE;
640 crc = get32(&s->bytestream);
643 case MKTAG('t', 'R', 'N', 'S'):
647 /* read the transparency. XXX: Only palette mode supported */
648 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
650 !(s->state & PNG_PLTE))
652 for(i=0;i<length;i++) {
653 v = *s->bytestream++;
654 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
656 crc = get32(&s->bytestream);
659 case MKTAG('I', 'E', 'N', 'D'):
660 if (!(s->state & PNG_ALLIMAGE))
662 crc = get32(&s->bytestream);
667 s->bytestream += length + 4;
672 *picture= *(AVFrame*)&s->picture;
673 *data_size = sizeof(AVPicture);
675 ret = s->bytestream - s->bytestream_start;
677 inflateEnd(&s->zstream);
678 av_freep(&s->crow_buf);
679 av_freep(&s->last_row);
680 av_freep(&s->tmp_row);
687 static void png_write_chunk(uint8_t **f, uint32_t tag,
688 const uint8_t *buf, int length)
694 crc = crc32(0, Z_NULL, 0);
696 tagbuf[1] = tag >> 8;
697 tagbuf[2] = tag >> 16;
698 tagbuf[3] = tag >> 24;
699 crc = crc32(crc, tagbuf, 4);
700 put32(f, bswap_32(tag));
702 crc = crc32(crc, buf, length);
703 memcpy(*f, buf, length);
709 /* XXX: use avcodec generic function ? */
710 static void to_be32(uint8_t *p, uint32_t v)
718 /* XXX: do filtering */
719 static int png_write_row(PNGContext *s, const uint8_t *data, int size)
723 s->zstream.avail_in = size;
724 s->zstream.next_in = (uint8_t *)data;
725 while (s->zstream.avail_in > 0) {
726 ret = deflate(&s->zstream, Z_NO_FLUSH);
729 if (s->zstream.avail_out == 0) {
730 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
731 s->zstream.avail_out = IOBUF_SIZE;
732 s->zstream.next_out = s->buf;
738 static int common_init(AVCodecContext *avctx){
739 PNGContext *s = avctx->priv_data;
741 avcodec_get_frame_defaults((AVFrame*)&s->picture);
742 avctx->coded_frame= (AVFrame*)&s->picture;
748 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
749 PNGContext *s = avctx->priv_data;
750 AVFrame *pict = data;
751 AVFrame * const p= (AVFrame*)&s->picture;
752 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
753 int bits_per_pixel, pass_row_size;
755 uint8_t *crow_buf = NULL;
756 uint8_t *tmp_buf = NULL;
759 p->pict_type= FF_I_TYPE;
764 s->bytestream_end= buf+buf_size;
766 is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
767 switch(avctx->pix_fmt) {
770 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
774 color_type = PNG_COLOR_TYPE_RGB;
778 color_type = PNG_COLOR_TYPE_GRAY;
780 case PIX_FMT_MONOBLACK:
782 color_type = PNG_COLOR_TYPE_GRAY;
786 color_type = PNG_COLOR_TYPE_PALETTE;
791 bits_per_pixel = png_get_nb_channels(color_type) * bit_depth;
792 row_size = (avctx->width * bits_per_pixel + 7) >> 3;
794 s->zstream.zalloc = png_zalloc;
795 s->zstream.zfree = png_zfree;
796 s->zstream.opaque = NULL;
797 ret = deflateInit2(&s->zstream, Z_DEFAULT_COMPRESSION,
798 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
801 crow_buf = av_malloc(row_size + 1);
804 if (is_progressive) {
805 tmp_buf = av_malloc(row_size + 1);
810 /* write png header */
811 memcpy(s->bytestream, pngsig, 8);
814 to_be32(s->buf, avctx->width);
815 to_be32(s->buf + 4, avctx->height);
816 s->buf[8] = bit_depth;
817 s->buf[9] = color_type;
818 s->buf[10] = 0; /* compression type */
819 s->buf[11] = 0; /* filter type */
820 s->buf[12] = is_progressive; /* interlace type */
822 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
824 /* put the palette if needed */
825 if (color_type == PNG_COLOR_TYPE_PALETTE) {
826 int has_alpha, alpha, i;
831 palette = (uint32_t *)p->data[1];
833 alpha_ptr = s->buf + 256 * 3;
835 for(i = 0; i < 256; i++) {
840 *alpha_ptr++ = alpha;
846 png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
848 png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
852 /* now put each row */
853 s->zstream.avail_out = IOBUF_SIZE;
854 s->zstream.next_out = s->buf;
855 if (is_progressive) {
859 for(pass = 0; pass < NB_PASSES; pass++) {
860 /* NOTE: a pass is completely omited if no pixels would be
862 pass_row_size = png_pass_row_size(pass, bits_per_pixel, avctx->width);
863 if (pass_row_size > 0) {
864 for(y = 0; y < avctx->height; y++) {
865 if ((png_pass_ymask[pass] << (y & 7)) & 0x80) {
866 ptr = p->data[0] + y * p->linesize[0];
867 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
868 convert_from_rgba32(tmp_buf, ptr, avctx->width);
873 png_get_interlaced_row(crow_buf + 1, pass_row_size,
874 bits_per_pixel, pass,
876 crow_buf[0] = PNG_FILTER_VALUE_NONE;
877 png_write_row(s, crow_buf, pass_row_size + 1);
883 for(y = 0; y < avctx->height; y++) {
884 ptr = p->data[0] + y * p->linesize[0];
885 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
886 convert_from_rgba32(crow_buf + 1, ptr, avctx->width);
888 memcpy(crow_buf + 1, ptr, row_size);
889 crow_buf[0] = PNG_FILTER_VALUE_NONE;
890 png_write_row(s, crow_buf, row_size + 1);
893 /* compress last bytes */
895 ret = deflate(&s->zstream, Z_FINISH);
896 if (ret == Z_OK || ret == Z_STREAM_END) {
897 len = IOBUF_SIZE - s->zstream.avail_out;
899 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
901 s->zstream.avail_out = IOBUF_SIZE;
902 s->zstream.next_out = s->buf;
903 if (ret == Z_STREAM_END)
909 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
911 ret = s->bytestream - s->bytestream_start;
915 deflateEnd(&s->zstream);
922 AVCodec png_decoder = {
931 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
935 AVCodec png_encoder = {
943 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, -1},