annotate pngenc.c @ 5875:5a61e8e2f65c libavcodec

Remove libvorbis Vorbis decoding support. Our native decoder is complete and has no known bugs, any remaining issues will hopefully be uncovered now.
author diego
date Sun, 04 Nov 2007 12:55:32 +0000
parents af7e6436d8bd
children 2d7afa1bc573
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
1 /*
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
2 * PNG image format
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
3 * Copyright (c) 2003 Fabrice Bellard.
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
4 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3777
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3777
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3777
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3777
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3777
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
15 * Lesser General Public License for more details.
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
16 *
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3777
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2967
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
20 */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
21 #include "avcodec.h"
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
22 #include "bytestream.h"
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
23 #include "png.h"
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
24
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
25 /* TODO:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
26 * - add 2, 4 and 16 bit depth support
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
27 * - use filters when generating a png (better compression)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
28 */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
29
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
30 #include <zlib.h>
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
31
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
32 //#define DEBUG
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
33
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
34 #define IOBUF_SIZE 4096
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
35
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
36 typedef struct PNGEncContext {
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
37 uint8_t *bytestream;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
38 uint8_t *bytestream_start;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
39 uint8_t *bytestream_end;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
40 AVFrame picture;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
41
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
42 z_stream zstream;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
43 uint8_t buf[IOBUF_SIZE];
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
44 } PNGEncContext;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
45
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
46 static void png_get_interlaced_row(uint8_t *dst, int row_size,
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
47 int bits_per_pixel, int pass,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
48 const uint8_t *src, int width)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
49 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
50 int x, mask, dst_x, j, b, bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
51 uint8_t *d;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
52 const uint8_t *s;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
53
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
54 mask = ff_png_pass_mask[pass];
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
55 switch(bits_per_pixel) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
56 case 1:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
57 memset(dst, 0, row_size);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
58 dst_x = 0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
59 for(x = 0; x < width; x++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
60 j = (x & 7);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
61 if ((mask << j) & 0x80) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
62 b = (src[x >> 3] >> (7 - j)) & 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
63 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
64 dst_x++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
65 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
66 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
67 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
68 default:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
69 bpp = bits_per_pixel >> 3;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
70 d = dst;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
71 s = src;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
72 for(x = 0; x < width; x++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
73 j = x & 7;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
74 if ((mask << j) & 0x80) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
75 memcpy(d, s, bpp);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
76 d += bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
77 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
78 s += bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
79 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
80 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
81 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
82 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
83
4515
a2b14c6fccfd consistency renaming: rgba32 --> rgb32
diego
parents: 4494
diff changeset
84 static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
85 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
86 uint8_t *d;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
87 int j;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
88 unsigned int v;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
89
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
90 d = dst;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
91 for(j = 0; j < width; j++) {
3347
82277c821113 Add const to (mostly) char* and make some functions static, which aren't used
diego
parents: 3177
diff changeset
92 v = ((const uint32_t *)src)[j];
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
93 d[0] = v >> 16;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
94 d[1] = v >> 8;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
95 d[2] = v;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
96 d[3] = v >> 24;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
97 d += 4;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
98 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
99 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
100
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
101 static void png_write_chunk(uint8_t **f, uint32_t tag,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
102 const uint8_t *buf, int length)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
103 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
104 uint32_t crc;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
105 uint8_t tagbuf[4];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
106
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
107 bytestream_put_be32(f, length);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
108 crc = crc32(0, Z_NULL, 0);
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 5067
diff changeset
109 AV_WL32(tagbuf, tag);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
110 crc = crc32(crc, tagbuf, 4);
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
111 bytestream_put_be32(f, bswap_32(tag));
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
112 if (length > 0) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
113 crc = crc32(crc, buf, length);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
114 memcpy(*f, buf, length);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
115 *f += length;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
116 }
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
117 bytestream_put_be32(f, crc);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
118 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
119
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
120 /* XXX: do filtering */
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
121 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
122 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
123 int ret;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
124
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
125 s->zstream.avail_in = size;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
126 s->zstream.next_in = (uint8_t *)data;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
127 while (s->zstream.avail_in > 0) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
128 ret = deflate(&s->zstream, Z_NO_FLUSH);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
129 if (ret != Z_OK)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
130 return -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
131 if (s->zstream.avail_out == 0) {
2422
18b8b2dcc037 various security fixes and precautionary checks
michael
parents: 2347
diff changeset
132 if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
18b8b2dcc037 various security fixes and precautionary checks
michael
parents: 2347
diff changeset
133 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
134 s->zstream.avail_out = IOBUF_SIZE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
135 s->zstream.next_out = s->buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
136 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
137 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
138 return 0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
139 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
140
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
141 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
142 PNGEncContext *s = avctx->priv_data;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
143 AVFrame *pict = data;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
144 AVFrame * const p= (AVFrame*)&s->picture;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
145 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
146 int bits_per_pixel, pass_row_size;
5804
af7e6436d8bd Allow to override zlib compression level in PNG encoder via avctx->compression_level
reimar
parents: 5339
diff changeset
147 int compression_level;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
148 uint8_t *ptr;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
149 uint8_t *crow_buf = NULL;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
150 uint8_t *tmp_buf = NULL;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
151
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
152 *p = *pict;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
153 p->pict_type= FF_I_TYPE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
154 p->key_frame= 1;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
155
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
156 s->bytestream_start=
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
157 s->bytestream= buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
158 s->bytestream_end= buf+buf_size;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
159
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
160 is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
161 switch(avctx->pix_fmt) {
4494
ce643a22f049 Replace deprecated PIX_FMT names by the newer variants.
diego
parents: 4379
diff changeset
162 case PIX_FMT_RGB32:
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
163 bit_depth = 8;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
164 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
165 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
166 case PIX_FMT_RGB24:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
167 bit_depth = 8;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
168 color_type = PNG_COLOR_TYPE_RGB;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
169 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
170 case PIX_FMT_GRAY8:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
171 bit_depth = 8;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
172 color_type = PNG_COLOR_TYPE_GRAY;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
173 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
174 case PIX_FMT_MONOBLACK:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
175 bit_depth = 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
176 color_type = PNG_COLOR_TYPE_GRAY;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
177 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
178 case PIX_FMT_PAL8:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
179 bit_depth = 8;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
180 color_type = PNG_COLOR_TYPE_PALETTE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
181 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
182 default:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
183 return -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
184 }
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
185 bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
186 row_size = (avctx->width * bits_per_pixel + 7) >> 3;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
187
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
188 s->zstream.zalloc = ff_png_zalloc;
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
189 s->zstream.zfree = ff_png_zfree;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
190 s->zstream.opaque = NULL;
5804
af7e6436d8bd Allow to override zlib compression level in PNG encoder via avctx->compression_level
reimar
parents: 5339
diff changeset
191 compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
af7e6436d8bd Allow to override zlib compression level in PNG encoder via avctx->compression_level
reimar
parents: 5339
diff changeset
192 Z_DEFAULT_COMPRESSION :
af7e6436d8bd Allow to override zlib compression level in PNG encoder via avctx->compression_level
reimar
parents: 5339
diff changeset
193 av_clip(avctx->compression_level, 0, 9);
af7e6436d8bd Allow to override zlib compression level in PNG encoder via avctx->compression_level
reimar
parents: 5339
diff changeset
194 ret = deflateInit2(&s->zstream, compression_level,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
195 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
196 if (ret != Z_OK)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
197 return -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
198 crow_buf = av_malloc(row_size + 1);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
199 if (!crow_buf)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
200 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
201 if (is_progressive) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
202 tmp_buf = av_malloc(row_size + 1);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
203 if (!tmp_buf)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
204 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
205 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
206
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
207 /* write png header */
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
208 memcpy(s->bytestream, ff_pngsig, 8);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
209 s->bytestream += 8;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
210
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
211 AV_WB32(s->buf, avctx->width);
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
212 AV_WB32(s->buf + 4, avctx->height);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
213 s->buf[8] = bit_depth;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
214 s->buf[9] = color_type;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
215 s->buf[10] = 0; /* compression type */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
216 s->buf[11] = 0; /* filter type */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
217 s->buf[12] = is_progressive; /* interlace type */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
218
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
219 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
220
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
221 /* put the palette if needed */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
222 if (color_type == PNG_COLOR_TYPE_PALETTE) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
223 int has_alpha, alpha, i;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
224 unsigned int v;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
225 uint32_t *palette;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
226 uint8_t *alpha_ptr;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
227
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
228 palette = (uint32_t *)p->data[1];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
229 ptr = s->buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
230 alpha_ptr = s->buf + 256 * 3;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
231 has_alpha = 0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
232 for(i = 0; i < 256; i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
233 v = palette[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
234 alpha = v >> 24;
4018
9c8ab288ebf6 Make PNG produce correct 8-bit pictures
kostya
parents: 3947
diff changeset
235 if (alpha && alpha != 0xff)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
236 has_alpha = 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
237 *alpha_ptr++ = alpha;
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 5067
diff changeset
238 bytestream_put_be24(&ptr, v);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
239 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
240 png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
241 if (has_alpha) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
242 png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
243 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
244 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
245
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
246 /* now put each row */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
247 s->zstream.avail_out = IOBUF_SIZE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
248 s->zstream.next_out = s->buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
249 if (is_progressive) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
250 uint8_t *ptr1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
251 int pass;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
252
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
253 for(pass = 0; pass < NB_PASSES; pass++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
254 /* NOTE: a pass is completely omited if no pixels would be
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
255 output */
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
256 pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
257 if (pass_row_size > 0) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
258 for(y = 0; y < avctx->height; y++) {
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
259 if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
260 ptr = p->data[0] + y * p->linesize[0];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
261 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
4515
a2b14c6fccfd consistency renaming: rgba32 --> rgb32
diego
parents: 4494
diff changeset
262 convert_from_rgb32(tmp_buf, ptr, avctx->width);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
263 ptr1 = tmp_buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
264 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
265 ptr1 = ptr;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
266 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
267 png_get_interlaced_row(crow_buf + 1, pass_row_size,
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
268 bits_per_pixel, pass,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
269 ptr1, avctx->width);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
270 crow_buf[0] = PNG_FILTER_VALUE_NONE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
271 png_write_row(s, crow_buf, pass_row_size + 1);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
272 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
273 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
274 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
275 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
276 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
277 for(y = 0; y < avctx->height; y++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
278 ptr = p->data[0] + y * p->linesize[0];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
279 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4515
a2b14c6fccfd consistency renaming: rgba32 --> rgb32
diego
parents: 4494
diff changeset
280 convert_from_rgb32(crow_buf + 1, ptr, avctx->width);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
281 else
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
282 memcpy(crow_buf + 1, ptr, row_size);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
283 crow_buf[0] = PNG_FILTER_VALUE_NONE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
284 png_write_row(s, crow_buf, row_size + 1);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
285 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
286 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
287 /* compress last bytes */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
288 for(;;) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
289 ret = deflate(&s->zstream, Z_FINISH);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
290 if (ret == Z_OK || ret == Z_STREAM_END) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
291 len = IOBUF_SIZE - s->zstream.avail_out;
2422
18b8b2dcc037 various security fixes and precautionary checks
michael
parents: 2347
diff changeset
292 if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
293 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
294 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
295 s->zstream.avail_out = IOBUF_SIZE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
296 s->zstream.next_out = s->buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
297 if (ret == Z_STREAM_END)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
298 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
299 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
300 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
301 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
302 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
303 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
304
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
305 ret = s->bytestream - s->bytestream_start;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
306 the_end:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
307 av_free(crow_buf);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
308 av_free(tmp_buf);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
309 deflateEnd(&s->zstream);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
310 return ret;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
311 fail:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
312 ret = -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
313 goto the_end;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
314 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
315
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
316 static int png_enc_init(AVCodecContext *avctx){
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
317 PNGEncContext *s = avctx->priv_data;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
318
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
319 avcodec_get_frame_defaults((AVFrame*)&s->picture);
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
320 avctx->coded_frame= (AVFrame*)&s->picture;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
321
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
322 return 0;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
323 }
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
324
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
325 AVCodec png_encoder = {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
326 "png",
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
327 CODEC_TYPE_VIDEO,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
328 CODEC_ID_PNG,
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
329 sizeof(PNGEncContext),
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
330 png_enc_init,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
331 encode_frame,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
332 NULL, //encode_end,
4494
ce643a22f049 Replace deprecated PIX_FMT names by the newer variants.
diego
parents: 4379
diff changeset
333 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, -1},
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
334 };