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)
32 #define PNG_COLOR_MASK_PALETTE 1
33 #define PNG_COLOR_MASK_COLOR 2
34 #define PNG_COLOR_MASK_ALPHA 4
36 #define PNG_COLOR_TYPE_GRAY 0
37 #define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
38 #define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
39 #define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
40 #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
42 #define PNG_FILTER_VALUE_NONE 0
43 #define PNG_FILTER_VALUE_SUB 1
44 #define PNG_FILTER_VALUE_UP 2
45 #define PNG_FILTER_VALUE_AVG 3
46 #define PNG_FILTER_VALUE_PAETH 4
48 #define PNG_IHDR 0x0001
49 #define PNG_IDAT 0x0002
50 #define PNG_ALLIMAGE 0x0004
51 #define PNG_PLTE 0x0008
55 #define IOBUF_SIZE 4096
57 typedef struct PNGDecodeState
{
71 uint32_t palette
[256];
76 int crow_size
; /* compressed row size (include filter type) */
77 int row_size
; /* decompressed row size */
78 int pass_row_size
; /* decompress row size of the current pass */
83 static const uint8_t pngsig
[8] = {137, 80, 78, 71, 13, 10, 26, 10};
85 /* Mask to determine which y pixels are valid in a pass */
86 static const uint8_t png_pass_ymask
[NB_PASSES
] = {
87 0x80, 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55,
90 /* Mask to determine which y pixels can be written in a pass */
91 static const uint8_t png_pass_dsp_ymask
[NB_PASSES
] = {
92 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
96 static const uint8_t png_pass_xmin
[NB_PASSES
] = {
100 /* x shift to get row width */
101 static const uint8_t png_pass_xshift
[NB_PASSES
] = {
105 /* Mask to determine which pixels are valid in a pass */
106 static const uint8_t png_pass_mask
[NB_PASSES
] = {
107 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff
110 /* Mask to determine which pixels to overwrite while displaying */
111 static const uint8_t png_pass_dsp_mask
[NB_PASSES
] = {
112 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
115 static int png_probe(AVProbeData
*pd
)
117 if (pd
->buf_size
>= 8 &&
118 memcmp(pd
->buf
, pngsig
, 8) == 0)
119 return AVPROBE_SCORE_MAX
;
124 static void *png_zalloc(void *opaque
, unsigned int items
, unsigned int size
)
126 return av_malloc(items
* size
);
129 static void png_zfree(void *opaque
, void *ptr
)
134 static int png_get_nb_channels(int color_type
)
138 if ((color_type
& (PNG_COLOR_MASK_COLOR
| PNG_COLOR_MASK_PALETTE
)) ==
139 PNG_COLOR_MASK_COLOR
)
141 if (color_type
& PNG_COLOR_MASK_ALPHA
)
146 /* compute the row size of an interleaved pass */
147 static int png_pass_row_size(int pass
, int bits_per_pixel
, int width
)
149 int shift
, xmin
, pass_width
;
151 xmin
= png_pass_xmin
[pass
];
154 shift
= png_pass_xshift
[pass
];
155 pass_width
= (width
- xmin
+ (1 << shift
) - 1) >> shift
;
156 return (pass_width
* bits_per_pixel
+ 7) >> 3;
159 /* NOTE: we try to construct a good looking image at each pass. width
160 is the original image width. We also do pixel format convertion at
162 static void png_put_interlaced_row(uint8_t *dst
, int width
,
163 int bits_per_pixel
, int pass
,
164 int color_type
, const uint8_t *src
)
166 int x
, mask
, dsp_mask
, j
, src_x
, b
, bpp
;
170 mask
= png_pass_mask
[pass
];
171 dsp_mask
= png_pass_dsp_mask
[pass
];
172 switch(bits_per_pixel
) {
174 /* we must intialize the line to zero before writing to it */
176 memset(dst
, 0, (width
+ 7) >> 3);
178 for(x
= 0; x
< width
; x
++) {
180 if ((dsp_mask
<< j
) & 0x80) {
181 b
= (src
[src_x
>> 3] >> (7 - (src_x
& 7))) & 1;
182 dst
[x
>> 3] |= b
<< (7 - j
);
184 if ((mask
<< j
) & 0x80)
189 bpp
= bits_per_pixel
>> 3;
192 if (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
193 for(x
= 0; x
< width
; x
++) {
195 if ((dsp_mask
<< j
) & 0x80) {
196 *(uint32_t *)d
= (s
[3] << 24) | (s
[0] << 16) | (s
[1] << 8) | s
[2];
199 if ((mask
<< j
) & 0x80)
203 for(x
= 0; x
< width
; x
++) {
205 if ((dsp_mask
<< j
) & 0x80) {
209 if ((mask
<< j
) & 0x80)
217 static void png_get_interlaced_row(uint8_t *dst
, int row_size
,
218 int bits_per_pixel
, int pass
,
219 const uint8_t *src
, int width
)
221 int x
, mask
, dst_x
, j
, b
, bpp
;
225 mask
= png_pass_mask
[pass
];
226 switch(bits_per_pixel
) {
228 memset(dst
, 0, row_size
);
230 for(x
= 0; x
< width
; x
++) {
232 if ((mask
<< j
) & 0x80) {
233 b
= (src
[x
>> 3] >> (7 - j
)) & 1;
234 dst
[dst_x
>> 3] |= b
<< (7 - (dst_x
& 7));
240 bpp
= bits_per_pixel
>> 3;
243 for(x
= 0; x
< width
; x
++) {
245 if ((mask
<< j
) & 0x80) {
256 /* NOTE: 'dst' can be equal to 'last' */
257 static void png_filter_row(uint8_t *dst
, int filter_type
,
258 uint8_t *src
, uint8_t *last
, int size
, int bpp
)
262 switch(filter_type
) {
263 case PNG_FILTER_VALUE_NONE
:
264 memcpy(dst
, src
, size
);
266 case PNG_FILTER_VALUE_SUB
:
267 for(i
= 0; i
< bpp
; i
++) {
270 for(i
= bpp
; i
< size
; i
++) {
275 case PNG_FILTER_VALUE_UP
:
276 for(i
= 0; i
< size
; i
++) {
281 case PNG_FILTER_VALUE_AVG
:
282 for(i
= 0; i
< bpp
; i
++) {
286 for(i
= bpp
; i
< size
; i
++) {
287 p
= ((dst
[i
- bpp
] + last
[i
]) >> 1);
291 case PNG_FILTER_VALUE_PAETH
:
292 for(i
= 0; i
< bpp
; i
++) {
296 for(i
= bpp
; i
< size
; i
++) {
297 int a
, b
, c
, pa
, pb
, pc
;
310 if (pa
<= pb
&& pa
<= pc
)
322 static void convert_from_rgba32(uint8_t *dst
, const uint8_t *src
, int width
)
329 for(j
= 0; j
< width
; j
++) {
330 v
= ((const uint32_t *)src
)[j
];
339 static void convert_to_rgba32(uint8_t *dst
, const uint8_t *src
, int width
)
342 unsigned int r
, g
, b
, a
;
344 for(j
= 0;j
< width
; j
++) {
349 *(uint32_t *)dst
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
355 /* process exactly one decompressed row */
356 static void png_handle_row(PNGDecodeState
*s
)
358 uint8_t *ptr
, *last_row
;
361 if (!s
->interlace_type
) {
362 ptr
= s
->image_buf
+ s
->image_linesize
* s
->y
;
363 /* need to swap bytes correctly for RGB_ALPHA */
364 if (s
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
365 png_filter_row(s
->tmp_row
, s
->crow_buf
[0], s
->crow_buf
+ 1,
366 s
->last_row
, s
->row_size
, s
->bpp
);
367 memcpy(s
->last_row
, s
->tmp_row
, s
->row_size
);
368 convert_to_rgba32(ptr
, s
->tmp_row
, s
->width
);
370 /* in normal case, we avoid one copy */
372 last_row
= s
->last_row
;
374 last_row
= ptr
- s
->image_linesize
;
376 png_filter_row(ptr
, s
->crow_buf
[0], s
->crow_buf
+ 1,
377 last_row
, s
->row_size
, s
->bpp
);
380 if (s
->y
== s
->height
) {
381 s
->state
|= PNG_ALLIMAGE
;
386 ptr
= s
->image_buf
+ s
->image_linesize
* s
->y
;
387 if ((png_pass_ymask
[s
->pass
] << (s
->y
& 7)) & 0x80) {
388 /* if we already read one row, it is time to stop to
389 wait for the next one */
392 png_filter_row(s
->tmp_row
, s
->crow_buf
[0], s
->crow_buf
+ 1,
393 s
->last_row
, s
->pass_row_size
, s
->bpp
);
394 memcpy(s
->last_row
, s
->tmp_row
, s
->pass_row_size
);
397 if ((png_pass_dsp_ymask
[s
->pass
] << (s
->y
& 7)) & 0x80) {
398 /* NOTE: rgba32 is handled directly in png_put_interlaced_row */
399 png_put_interlaced_row(ptr
, s
->width
, s
->bits_per_pixel
, s
->pass
,
400 s
->color_type
, s
->last_row
);
403 if (s
->y
== s
->height
) {
405 if (s
->pass
== NB_PASSES
- 1) {
406 s
->state
|= PNG_ALLIMAGE
;
411 s
->pass_row_size
= png_pass_row_size(s
->pass
,
414 s
->crow_size
= s
->pass_row_size
+ 1;
415 if (s
->pass_row_size
!= 0)
417 /* skip pass if empty row */
426 static int png_decode_idat(PNGDecodeState
*s
, ByteIOContext
*f
, int length
)
428 uint8_t buf
[IOBUF_SIZE
];
432 /* read the buffer */
433 buf_size
= IOBUF_SIZE
;
434 if (buf_size
> length
)
436 ret
= get_buffer(f
, buf
, buf_size
);
439 s
->zstream
.avail_in
= buf_size
;
440 s
->zstream
.next_in
= buf
;
441 /* decode one line if possible */
442 while (s
->zstream
.avail_in
> 0) {
443 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
444 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
447 if (s
->zstream
.avail_out
== 0) {
448 if (!(s
->state
& PNG_ALLIMAGE
)) {
451 s
->zstream
.avail_out
= s
->crow_size
;
452 s
->zstream
.next_out
= s
->crow_buf
;
460 static int png_read(ByteIOContext
*f
,
461 int (*alloc_cb
)(void *opaque
, AVImageInfo
*info
), void *opaque
)
463 AVImageInfo info1
, *info
= &info1
;
464 PNGDecodeState s1
, *s
= &s1
;
465 uint32_t tag
, length
;
469 /* check signature */
470 ret
= get_buffer(f
, buf
, 8);
473 if (memcmp(buf
, pngsig
, 8) != 0)
475 memset(s
, 0, sizeof(PNGDecodeState
));
477 s
->zstream
.zalloc
= png_zalloc
;
478 s
->zstream
.zfree
= png_zfree
;
479 s
->zstream
.opaque
= NULL
;
480 ret
= inflateInit(&s
->zstream
);
486 length
= get_be32(f
);
487 if (length
> 0x7fffffff)
491 printf("png: tag=%c%c%c%c length=%u\n",
494 ((tag
>> 16) & 0xff),
495 ((tag
>> 24) & 0xff), length
);
498 case MKTAG('I', 'H', 'D', 'R'):
501 s
->width
= get_be32(f
);
502 s
->height
= get_be32(f
);
503 s
->bit_depth
= get_byte(f
);
504 s
->color_type
= get_byte(f
);
505 s
->compression_type
= get_byte(f
);
506 s
->filter_type
= get_byte(f
);
507 s
->interlace_type
= get_byte(f
);
509 s
->state
|= PNG_IHDR
;
511 printf("width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
512 s
->width
, s
->height
, s
->bit_depth
, s
->color_type
,
513 s
->compression_type
, s
->filter_type
, s
->interlace_type
);
516 case MKTAG('I', 'D', 'A', 'T'):
517 if (!(s
->state
& PNG_IHDR
))
519 if (!(s
->state
& PNG_IDAT
)) {
520 /* init image info */
521 info
->width
= s
->width
;
522 info
->height
= s
->height
;
523 info
->interleaved
= (s
->interlace_type
!= 0);
525 s
->channels
= png_get_nb_channels(s
->color_type
);
526 s
->bits_per_pixel
= s
->bit_depth
* s
->channels
;
527 s
->bpp
= (s
->bits_per_pixel
+ 7) >> 3;
528 s
->row_size
= (info
->width
* s
->bits_per_pixel
+ 7) >> 3;
530 if (s
->bit_depth
== 8 &&
531 s
->color_type
== PNG_COLOR_TYPE_RGB
) {
532 info
->pix_fmt
= PIX_FMT_RGB24
;
533 } else if (s
->bit_depth
== 8 &&
534 s
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
535 info
->pix_fmt
= PIX_FMT_RGBA32
;
536 } else if (s
->bit_depth
== 8 &&
537 s
->color_type
== PNG_COLOR_TYPE_GRAY
) {
538 info
->pix_fmt
= PIX_FMT_GRAY8
;
539 } else if (s
->bit_depth
== 1 &&
540 s
->color_type
== PNG_COLOR_TYPE_GRAY
) {
541 info
->pix_fmt
= PIX_FMT_MONOBLACK
;
542 } else if (s
->color_type
== PNG_COLOR_TYPE_PALETTE
) {
543 info
->pix_fmt
= PIX_FMT_PAL8
;
547 ret
= alloc_cb(opaque
, info
);
551 /* compute the compressed row size */
552 if (!s
->interlace_type
) {
553 s
->crow_size
= s
->row_size
+ 1;
556 s
->pass_row_size
= png_pass_row_size(s
->pass
,
559 s
->crow_size
= s
->pass_row_size
+ 1;
562 printf("row_size=%d crow_size =%d\n",
563 s
->row_size
, s
->crow_size
);
565 s
->image_buf
= info
->pict
.data
[0];
566 s
->image_linesize
= info
->pict
.linesize
[0];
567 /* copy the palette if needed */
568 if (s
->color_type
== PNG_COLOR_TYPE_PALETTE
)
569 memcpy(info
->pict
.data
[1], s
->palette
, 256 * sizeof(uint32_t));
570 /* empty row is used if differencing to the first row */
571 s
->last_row
= av_mallocz(s
->row_size
);
574 if (s
->interlace_type
||
575 s
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
576 s
->tmp_row
= av_malloc(s
->row_size
);
581 s
->crow_buf
= av_malloc(s
->row_size
+ 1);
584 s
->zstream
.avail_out
= s
->crow_size
;
585 s
->zstream
.next_out
= s
->crow_buf
;
587 s
->state
|= PNG_IDAT
;
588 if (png_decode_idat(s
, f
, length
) < 0)
593 case MKTAG('P', 'L', 'T', 'E'):
597 if ((length
% 3) != 0 || length
> 256 * 3)
599 /* read the palette */
605 s
->palette
[i
] = (0xff << 24) | (r
<< 16) | (g
<< 8) | b
;
608 s
->palette
[i
] = (0xff << 24);
610 s
->state
|= PNG_PLTE
;
614 case MKTAG('t', 'R', 'N', 'S'):
618 /* read the transparency. XXX: Only palette mode supported */
619 if (s
->color_type
!= PNG_COLOR_TYPE_PALETTE
||
621 !(s
->state
& PNG_PLTE
))
623 for(i
=0;i
<length
;i
++) {
625 s
->palette
[i
] = (s
->palette
[i
] & 0x00ffffff) | (v
<< 24);
630 case MKTAG('I', 'E', 'N', 'D'):
631 if (!(s
->state
& PNG_ALLIMAGE
))
638 url_fskip(f
, length
+ 4);
645 inflateEnd(&s
->zstream
);
646 av_free(s
->crow_buf
);
647 av_free(s
->last_row
);
655 static void png_write_chunk(ByteIOContext
*f
, uint32_t tag
,
656 const uint8_t *buf
, int length
)
662 crc
= crc32(0, Z_NULL
, 0);
664 tagbuf
[1] = tag
>> 8;
665 tagbuf
[2] = tag
>> 16;
666 tagbuf
[3] = tag
>> 24;
667 crc
= crc32(crc
, tagbuf
, 4);
670 crc
= crc32(crc
, buf
, length
);
671 put_buffer(f
, buf
, length
);
676 /* XXX: use avcodec generic function ? */
677 static void to_be32(uint8_t *p
, uint32_t v
)
685 typedef struct PNGEncodeState
{
688 uint8_t buf
[IOBUF_SIZE
];
692 /* XXX: do filtering */
693 static int png_write_row(PNGEncodeState
*s
, const uint8_t *data
, int size
)
697 s
->zstream
.avail_in
= size
;
698 s
->zstream
.next_in
= (uint8_t *)data
;
699 while (s
->zstream
.avail_in
> 0) {
700 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
703 if (s
->zstream
.avail_out
== 0) {
704 png_write_chunk(s
->f
, MKTAG('I', 'D', 'A', 'T'), s
->buf
, IOBUF_SIZE
);
705 s
->zstream
.avail_out
= IOBUF_SIZE
;
706 s
->zstream
.next_out
= s
->buf
;
712 static int png_write(ByteIOContext
*f
, AVImageInfo
*info
)
714 PNGEncodeState s1
, *s
= &s1
;
715 int bit_depth
, color_type
, y
, len
, row_size
, ret
, is_progressive
;
716 int bits_per_pixel
, pass_row_size
;
718 uint8_t *crow_buf
= NULL
;
719 uint8_t *tmp_buf
= NULL
;
722 is_progressive
= info
->interleaved
;
723 switch(info
->pix_fmt
) {
726 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
730 color_type
= PNG_COLOR_TYPE_RGB
;
734 color_type
= PNG_COLOR_TYPE_GRAY
;
736 case PIX_FMT_MONOBLACK
:
738 color_type
= PNG_COLOR_TYPE_GRAY
;
742 color_type
= PNG_COLOR_TYPE_PALETTE
;
747 bits_per_pixel
= png_get_nb_channels(color_type
) * bit_depth
;
748 row_size
= (info
->width
* bits_per_pixel
+ 7) >> 3;
750 s
->zstream
.zalloc
= png_zalloc
;
751 s
->zstream
.zfree
= png_zfree
;
752 s
->zstream
.opaque
= NULL
;
753 ret
= deflateInit2(&s
->zstream
, Z_DEFAULT_COMPRESSION
,
754 Z_DEFLATED
, 15, 8, Z_DEFAULT_STRATEGY
);
757 crow_buf
= av_malloc(row_size
+ 1);
760 if (is_progressive
) {
761 tmp_buf
= av_malloc(row_size
+ 1);
766 /* write png header */
767 put_buffer(f
, pngsig
, 8);
769 to_be32(s
->buf
, info
->width
);
770 to_be32(s
->buf
+ 4, info
->height
);
771 s
->buf
[8] = bit_depth
;
772 s
->buf
[9] = color_type
;
773 s
->buf
[10] = 0; /* compression type */
774 s
->buf
[11] = 0; /* filter type */
775 s
->buf
[12] = is_progressive
; /* interlace type */
777 png_write_chunk(f
, MKTAG('I', 'H', 'D', 'R'), s
->buf
, 13);
779 /* put the palette if needed */
780 if (color_type
== PNG_COLOR_TYPE_PALETTE
) {
781 int has_alpha
, alpha
, i
;
786 palette
= (uint32_t *)info
->pict
.data
[1];
788 alpha_ptr
= s
->buf
+ 256 * 3;
790 for(i
= 0; i
< 256; i
++) {
795 *alpha_ptr
++ = alpha
;
801 png_write_chunk(f
, MKTAG('P', 'L', 'T', 'E'), s
->buf
, 256 * 3);
803 png_write_chunk(f
, MKTAG('t', 'R', 'N', 'S'), s
->buf
+ 256 * 3, 256);
807 /* now put each row */
808 s
->zstream
.avail_out
= IOBUF_SIZE
;
809 s
->zstream
.next_out
= s
->buf
;
810 if (is_progressive
) {
814 for(pass
= 0; pass
< NB_PASSES
; pass
++) {
815 /* NOTE: a pass is completely omited if no pixels would be
817 pass_row_size
= png_pass_row_size(pass
, bits_per_pixel
, info
->width
);
818 if (pass_row_size
> 0) {
819 for(y
= 0; y
< info
->height
; y
++) {
820 if ((png_pass_ymask
[pass
] << (y
& 7)) & 0x80) {
821 ptr
= info
->pict
.data
[0] + y
* info
->pict
.linesize
[0];
822 if (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
823 convert_from_rgba32(tmp_buf
, ptr
, info
->width
);
828 png_get_interlaced_row(crow_buf
+ 1, pass_row_size
,
829 bits_per_pixel
, pass
,
831 crow_buf
[0] = PNG_FILTER_VALUE_NONE
;
832 png_write_row(s
, crow_buf
, pass_row_size
+ 1);
838 for(y
= 0; y
< info
->height
; y
++) {
839 ptr
= info
->pict
.data
[0] + y
* info
->pict
.linesize
[0];
840 if (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
841 convert_from_rgba32(crow_buf
+ 1, ptr
, info
->width
);
843 memcpy(crow_buf
+ 1, ptr
, row_size
);
844 crow_buf
[0] = PNG_FILTER_VALUE_NONE
;
845 png_write_row(s
, crow_buf
, row_size
+ 1);
848 /* compress last bytes */
850 ret
= deflate(&s
->zstream
, Z_FINISH
);
851 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
852 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
854 png_write_chunk(f
, MKTAG('I', 'D', 'A', 'T'), s
->buf
, len
);
856 s
->zstream
.avail_out
= IOBUF_SIZE
;
857 s
->zstream
.next_out
= s
->buf
;
858 if (ret
== Z_STREAM_END
)
864 png_write_chunk(f
, MKTAG('I', 'E', 'N', 'D'), NULL
, 0);
871 deflateEnd(&s
->zstream
);
878 AVImageFormat png_image_format
= {
883 (1 << PIX_FMT_RGBA32
) | (1 << PIX_FMT_RGB24
) | (1 << PIX_FMT_GRAY8
) |
884 (1 << PIX_FMT_MONOBLACK
) | (1 << PIX_FMT_PAL8
),