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 * - add 2, 4 and 16 bit depth support
25 * - use filters when generating a png (better compression)
33 #define PNG_COLOR_MASK_PALETTE 1
34 #define PNG_COLOR_MASK_COLOR 2
35 #define PNG_COLOR_MASK_ALPHA 4
37 #define PNG_COLOR_TYPE_GRAY 0
38 #define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
39 #define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
40 #define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
41 #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
43 #define PNG_FILTER_VALUE_NONE 0
44 #define PNG_FILTER_VALUE_SUB 1
45 #define PNG_FILTER_VALUE_UP 2
46 #define PNG_FILTER_VALUE_AVG 3
47 #define PNG_FILTER_VALUE_PAETH 4
49 #define PNG_IHDR 0x0001
50 #define PNG_IDAT 0x0002
51 #define PNG_ALLIMAGE 0x0004
52 #define PNG_PLTE 0x0008
56 #define IOBUF_SIZE 4096
58 typedef struct PNGDecodeState {
72 uint32_t palette[256];
77 int crow_size; /* compressed row size (include filter type) */
78 int row_size; /* decompressed row size */
79 int pass_row_size; /* decompress row size of the current pass */
84 static const uint8_t pngsig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
86 /* Mask to determine which y pixels are valid in a pass */
87 static const uint8_t png_pass_ymask[NB_PASSES] = {
88 0x80, 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55,
91 /* Mask to determine which y pixels can be written in a pass */
92 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
93 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
97 static const uint8_t png_pass_xmin[NB_PASSES] = {
101 /* x shift to get row width */
102 static const uint8_t png_pass_xshift[NB_PASSES] = {
106 /* Mask to determine which pixels are valid in a pass */
107 static const uint8_t png_pass_mask[NB_PASSES] = {
108 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff
111 /* Mask to determine which pixels to overwrite while displaying */
112 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
113 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
116 static int png_probe(AVProbeData *pd)
118 if (pd->buf_size >= 8 &&
119 memcmp(pd->buf, pngsig, 8) == 0)
120 return AVPROBE_SCORE_MAX;
125 static void *png_zalloc(void *opaque, unsigned int items, unsigned int size)
127 return av_malloc(items * size);
130 static void png_zfree(void *opaque, void *ptr)
135 static int png_get_nb_channels(int color_type)
139 if ((color_type & (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)) ==
140 PNG_COLOR_MASK_COLOR)
142 if (color_type & PNG_COLOR_MASK_ALPHA)
147 /* compute the row size of an interleaved pass */
148 static int png_pass_row_size(int pass, int bits_per_pixel, int width)
150 int shift, xmin, pass_width;
152 xmin = png_pass_xmin[pass];
155 shift = png_pass_xshift[pass];
156 pass_width = (width - xmin + (1 << shift) - 1) >> shift;
157 return (pass_width * bits_per_pixel + 7) >> 3;
160 /* NOTE: we try to construct a good looking image at each pass. width
161 is the original image width. We also do pixel format convertion at
163 static void png_put_interlaced_row(uint8_t *dst, int width,
164 int bits_per_pixel, int pass,
165 int color_type, const uint8_t *src)
167 int x, mask, dsp_mask, j, src_x, b, bpp;
171 mask = png_pass_mask[pass];
172 dsp_mask = png_pass_dsp_mask[pass];
173 switch(bits_per_pixel) {
175 /* we must intialize the line to zero before writing to it */
177 memset(dst, 0, (width + 7) >> 3);
179 for(x = 0; x < width; x++) {
181 if ((dsp_mask << j) & 0x80) {
182 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
183 dst[x >> 3] |= b << (7 - j);
185 if ((mask << j) & 0x80)
190 bpp = bits_per_pixel >> 3;
193 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
194 for(x = 0; x < width; x++) {
196 if ((dsp_mask << j) & 0x80) {
197 *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
200 if ((mask << j) & 0x80)
204 for(x = 0; x < width; x++) {
206 if ((dsp_mask << j) & 0x80) {
210 if ((mask << j) & 0x80)
218 static void png_get_interlaced_row(uint8_t *dst, int row_size,
219 int bits_per_pixel, int pass,
220 const uint8_t *src, int width)
222 int x, mask, dst_x, j, b, bpp;
226 mask = png_pass_mask[pass];
227 switch(bits_per_pixel) {
229 memset(dst, 0, row_size);
231 for(x = 0; x < width; x++) {
233 if ((mask << j) & 0x80) {
234 b = (src[x >> 3] >> (7 - j)) & 1;
235 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
241 bpp = bits_per_pixel >> 3;
244 for(x = 0; x < width; x++) {
246 if ((mask << j) & 0x80) {
257 /* NOTE: 'dst' can be equal to 'last' */
258 static void png_filter_row(uint8_t *dst, int filter_type,
259 uint8_t *src, uint8_t *last, int size, int bpp)
263 switch(filter_type) {
264 case PNG_FILTER_VALUE_NONE:
265 memcpy(dst, src, size);
267 case PNG_FILTER_VALUE_SUB:
268 for(i = 0; i < bpp; i++) {
271 for(i = bpp; i < size; i++) {
276 case PNG_FILTER_VALUE_UP:
277 for(i = 0; i < size; i++) {
282 case PNG_FILTER_VALUE_AVG:
283 for(i = 0; i < bpp; i++) {
287 for(i = bpp; i < size; i++) {
288 p = ((dst[i - bpp] + last[i]) >> 1);
292 case PNG_FILTER_VALUE_PAETH:
293 for(i = 0; i < bpp; i++) {
297 for(i = bpp; i < size; i++) {
298 int a, b, c, pa, pb, pc;
311 if (pa <= pb && pa <= pc)
323 static void convert_from_rgba32(uint8_t *dst, const uint8_t *src, int width)
330 for(j = 0; j < width; j++) {
331 v = ((const uint32_t *)src)[j];
340 static void convert_to_rgba32(uint8_t *dst, const uint8_t *src, int width)
343 unsigned int r, g, b, a;
345 for(j = 0;j < width; j++) {
350 *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
356 /* process exactly one decompressed row */
357 static void png_handle_row(PNGDecodeState *s)
359 uint8_t *ptr, *last_row;
362 if (!s->interlace_type) {
363 ptr = s->image_buf + s->image_linesize * s->y;
364 /* need to swap bytes correctly for RGB_ALPHA */
365 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
366 png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
367 s->last_row, s->row_size, s->bpp);
368 memcpy(s->last_row, s->tmp_row, s->row_size);
369 convert_to_rgba32(ptr, s->tmp_row, s->width);
371 /* in normal case, we avoid one copy */
373 last_row = s->last_row;
375 last_row = ptr - s->image_linesize;
377 png_filter_row(ptr, s->crow_buf[0], s->crow_buf + 1,
378 last_row, s->row_size, s->bpp);
381 if (s->y == s->height) {
382 s->state |= PNG_ALLIMAGE;
387 ptr = s->image_buf + s->image_linesize * s->y;
388 if ((png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
389 /* if we already read one row, it is time to stop to
390 wait for the next one */
393 png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
394 s->last_row, s->pass_row_size, s->bpp);
395 memcpy(s->last_row, s->tmp_row, s->pass_row_size);
398 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
399 /* NOTE: rgba32 is handled directly in png_put_interlaced_row */
400 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
401 s->color_type, s->last_row);
404 if (s->y == s->height) {
406 if (s->pass == NB_PASSES - 1) {
407 s->state |= PNG_ALLIMAGE;
412 s->pass_row_size = png_pass_row_size(s->pass,
415 s->crow_size = s->pass_row_size + 1;
416 if (s->pass_row_size != 0)
418 /* skip pass if empty row */
427 static int png_decode_idat(PNGDecodeState *s, ByteIOContext *f, int length)
429 uint8_t buf[IOBUF_SIZE];
433 /* read the buffer */
434 buf_size = IOBUF_SIZE;
435 if (buf_size > length)
437 ret = get_buffer(f, buf, buf_size);
440 s->zstream.avail_in = buf_size;
441 s->zstream.next_in = buf;
442 /* decode one line if possible */
443 while (s->zstream.avail_in > 0) {
444 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
445 if (ret != Z_OK && ret != Z_STREAM_END) {
448 if (s->zstream.avail_out == 0) {
449 if (!(s->state & PNG_ALLIMAGE)) {
452 s->zstream.avail_out = s->crow_size;
453 s->zstream.next_out = s->crow_buf;
461 static int png_read(ByteIOContext *f,
462 int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
464 AVImageInfo info1, *info = &info1;
465 PNGDecodeState s1, *s = &s1;
466 uint32_t tag, length;
470 /* check signature */
471 ret = get_buffer(f, buf, 8);
474 if (memcmp(buf, pngsig, 8) != 0)
476 memset(s, 0, sizeof(PNGDecodeState));
478 s->zstream.zalloc = png_zalloc;
479 s->zstream.zfree = png_zfree;
480 s->zstream.opaque = NULL;
481 ret = inflateInit(&s->zstream);
487 length = get_be32(f);
488 if (length > 0x7fffffff)
492 printf("png: tag=%c%c%c%c length=%u\n",
495 ((tag >> 16) & 0xff),
496 ((tag >> 24) & 0xff), length);
499 case MKTAG('I', 'H', 'D', 'R'):
502 s->width = get_be32(f);
503 s->height = get_be32(f);
504 s->bit_depth = get_byte(f);
505 s->color_type = get_byte(f);
506 s->compression_type = get_byte(f);
507 s->filter_type = get_byte(f);
508 s->interlace_type = get_byte(f);
510 s->state |= PNG_IHDR;
512 printf("width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
513 s->width, s->height, s->bit_depth, s->color_type,
514 s->compression_type, s->filter_type, s->interlace_type);
517 case MKTAG('I', 'D', 'A', 'T'):
518 if (!(s->state & PNG_IHDR))
520 if (!(s->state & PNG_IDAT)) {
521 /* init image info */
522 info->width = s->width;
523 info->height = s->height;
524 info->interleaved = (s->interlace_type != 0);
526 s->channels = png_get_nb_channels(s->color_type);
527 s->bits_per_pixel = s->bit_depth * s->channels;
528 s->bpp = (s->bits_per_pixel + 7) >> 3;
529 s->row_size = (info->width * s->bits_per_pixel + 7) >> 3;
531 if (s->bit_depth == 8 &&
532 s->color_type == PNG_COLOR_TYPE_RGB) {
533 info->pix_fmt = PIX_FMT_RGB24;
534 } else if (s->bit_depth == 8 &&
535 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
536 info->pix_fmt = PIX_FMT_RGBA32;
537 } else if (s->bit_depth == 8 &&
538 s->color_type == PNG_COLOR_TYPE_GRAY) {
539 info->pix_fmt = PIX_FMT_GRAY8;
540 } else if (s->bit_depth == 1 &&
541 s->color_type == PNG_COLOR_TYPE_GRAY) {
542 info->pix_fmt = PIX_FMT_MONOBLACK;
543 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
544 info->pix_fmt = PIX_FMT_PAL8;
548 ret = alloc_cb(opaque, info);
552 /* compute the compressed row size */
553 if (!s->interlace_type) {
554 s->crow_size = s->row_size + 1;
557 s->pass_row_size = png_pass_row_size(s->pass,
560 s->crow_size = s->pass_row_size + 1;
563 printf("row_size=%d crow_size =%d\n",
564 s->row_size, s->crow_size);
566 s->image_buf = info->pict.data[0];
567 s->image_linesize = info->pict.linesize[0];
568 /* copy the palette if needed */
569 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
570 memcpy(info->pict.data[1], s->palette, 256 * sizeof(uint32_t));
571 /* empty row is used if differencing to the first row */
572 s->last_row = av_mallocz(s->row_size);
575 if (s->interlace_type ||
576 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
577 s->tmp_row = av_malloc(s->row_size);
582 s->crow_buf = av_malloc(s->row_size + 1);
585 s->zstream.avail_out = s->crow_size;
586 s->zstream.next_out = s->crow_buf;
588 s->state |= PNG_IDAT;
589 if (png_decode_idat(s, f, length) < 0)
594 case MKTAG('P', 'L', 'T', 'E'):
598 if ((length % 3) != 0 || length > 256 * 3)
600 /* read the palette */
606 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
609 s->palette[i] = (0xff << 24);
611 s->state |= PNG_PLTE;
615 case MKTAG('t', 'R', 'N', 'S'):
619 /* read the transparency. XXX: Only palette mode supported */
620 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
622 !(s->state & PNG_PLTE))
624 for(i=0;i<length;i++) {
626 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
631 case MKTAG('I', 'E', 'N', 'D'):
632 if (!(s->state & PNG_ALLIMAGE))
639 url_fskip(f, length + 4);
646 inflateEnd(&s->zstream);
647 av_free(s->crow_buf);
648 av_free(s->last_row);
656 static void png_write_chunk(ByteIOContext *f, uint32_t tag,
657 const uint8_t *buf, int length)
663 crc = crc32(0, Z_NULL, 0);
665 tagbuf[1] = tag >> 8;
666 tagbuf[2] = tag >> 16;
667 tagbuf[3] = tag >> 24;
668 crc = crc32(crc, tagbuf, 4);
671 crc = crc32(crc, buf, length);
672 put_buffer(f, buf, length);
677 /* XXX: use avcodec generic function ? */
678 static void to_be32(uint8_t *p, uint32_t v)
686 typedef struct PNGEncodeState {
689 uint8_t buf[IOBUF_SIZE];
693 /* XXX: do filtering */
694 static int png_write_row(PNGEncodeState *s, const uint8_t *data, int size)
698 s->zstream.avail_in = size;
699 s->zstream.next_in = (uint8_t *)data;
700 while (s->zstream.avail_in > 0) {
701 ret = deflate(&s->zstream, Z_NO_FLUSH);
704 if (s->zstream.avail_out == 0) {
705 png_write_chunk(s->f, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
706 s->zstream.avail_out = IOBUF_SIZE;
707 s->zstream.next_out = s->buf;
713 static int png_write(ByteIOContext *f, AVImageInfo *info)
715 PNGEncodeState s1, *s = &s1;
716 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
717 int bits_per_pixel, pass_row_size;
719 uint8_t *crow_buf = NULL;
720 uint8_t *tmp_buf = NULL;
723 is_progressive = info->interleaved;
724 switch(info->pix_fmt) {
727 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
731 color_type = PNG_COLOR_TYPE_RGB;
735 color_type = PNG_COLOR_TYPE_GRAY;
737 case PIX_FMT_MONOBLACK:
739 color_type = PNG_COLOR_TYPE_GRAY;
743 color_type = PNG_COLOR_TYPE_PALETTE;
748 bits_per_pixel = png_get_nb_channels(color_type) * bit_depth;
749 row_size = (info->width * bits_per_pixel + 7) >> 3;
751 s->zstream.zalloc = png_zalloc;
752 s->zstream.zfree = png_zfree;
753 s->zstream.opaque = NULL;
754 ret = deflateInit2(&s->zstream, Z_DEFAULT_COMPRESSION,
755 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
758 crow_buf = av_malloc(row_size + 1);
761 if (is_progressive) {
762 tmp_buf = av_malloc(row_size + 1);
767 /* write png header */
768 put_buffer(f, pngsig, 8);
770 to_be32(s->buf, info->width);
771 to_be32(s->buf + 4, info->height);
772 s->buf[8] = bit_depth;
773 s->buf[9] = color_type;
774 s->buf[10] = 0; /* compression type */
775 s->buf[11] = 0; /* filter type */
776 s->buf[12] = is_progressive; /* interlace type */
778 png_write_chunk(f, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
780 /* put the palette if needed */
781 if (color_type == PNG_COLOR_TYPE_PALETTE) {
782 int has_alpha, alpha, i;
787 palette = (uint32_t *)info->pict.data[1];
789 alpha_ptr = s->buf + 256 * 3;
791 for(i = 0; i < 256; i++) {
796 *alpha_ptr++ = alpha;
802 png_write_chunk(f, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
804 png_write_chunk(f, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
808 /* now put each row */
809 s->zstream.avail_out = IOBUF_SIZE;
810 s->zstream.next_out = s->buf;
811 if (is_progressive) {
815 for(pass = 0; pass < NB_PASSES; pass++) {
816 /* NOTE: a pass is completely omited if no pixels would be
818 pass_row_size = png_pass_row_size(pass, bits_per_pixel, info->width);
819 if (pass_row_size > 0) {
820 for(y = 0; y < info->height; y++) {
821 if ((png_pass_ymask[pass] << (y & 7)) & 0x80) {
822 ptr = info->pict.data[0] + y * info->pict.linesize[0];
823 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
824 convert_from_rgba32(tmp_buf, ptr, info->width);
829 png_get_interlaced_row(crow_buf + 1, pass_row_size,
830 bits_per_pixel, pass,
832 crow_buf[0] = PNG_FILTER_VALUE_NONE;
833 png_write_row(s, crow_buf, pass_row_size + 1);
839 for(y = 0; y < info->height; y++) {
840 ptr = info->pict.data[0] + y * info->pict.linesize[0];
841 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
842 convert_from_rgba32(crow_buf + 1, ptr, info->width);
844 memcpy(crow_buf + 1, ptr, row_size);
845 crow_buf[0] = PNG_FILTER_VALUE_NONE;
846 png_write_row(s, crow_buf, row_size + 1);
849 /* compress last bytes */
851 ret = deflate(&s->zstream, Z_FINISH);
852 if (ret == Z_OK || ret == Z_STREAM_END) {
853 len = IOBUF_SIZE - s->zstream.avail_out;
855 png_write_chunk(f, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
857 s->zstream.avail_out = IOBUF_SIZE;
858 s->zstream.next_out = s->buf;
859 if (ret == Z_STREAM_END)
865 png_write_chunk(f, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
872 deflateEnd(&s->zstream);
879 AVImageFormat png_image_format = {
884 (1 << PIX_FMT_RGBA32) | (1 << PIX_FMT_RGB24) | (1 << PIX_FMT_GRAY8) |
885 (1 << PIX_FMT_MONOBLACK) | (1 << PIX_FMT_PAL8),