Mercurial > libavcodec.hg
annotate pngenc.c @ 6383:7ba06222bda7 libavcodec
Disabling all SSE* code for old gcc to avoid alignment issues.
author | michael |
---|---|
date | Thu, 21 Feb 2008 00:06:07 +0000 |
parents | af7e6436d8bd |
children | 2d7afa1bc573 |
rev | line source |
---|---|
2342 | 1 /* |
2 * PNG image format | |
3 * Copyright (c) 2003 Fabrice Bellard. | |
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 | 8 * modify it under the terms of the GNU Lesser General Public |
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 | 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 | 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. | |
16 * | |
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 | 20 */ |
21 #include "avcodec.h" | |
5067 | 22 #include "bytestream.h" |
5337 | 23 #include "png.h" |
2342 | 24 |
25 /* TODO: | |
26 * - add 2, 4 and 16 bit depth support | |
27 * - use filters when generating a png (better compression) | |
28 */ | |
29 | |
30 #include <zlib.h> | |
31 | |
32 //#define DEBUG | |
33 | |
5339 | 34 #define IOBUF_SIZE 4096 |
35 | |
36 typedef struct PNGEncContext { | |
37 uint8_t *bytestream; | |
38 uint8_t *bytestream_start; | |
39 uint8_t *bytestream_end; | |
40 AVFrame picture; | |
41 | |
42 z_stream zstream; | |
43 uint8_t buf[IOBUF_SIZE]; | |
44 } PNGEncContext; | |
45 | |
2967 | 46 static void png_get_interlaced_row(uint8_t *dst, int row_size, |
47 int bits_per_pixel, int pass, | |
2342 | 48 const uint8_t *src, int width) |
49 { | |
50 int x, mask, dst_x, j, b, bpp; | |
51 uint8_t *d; | |
52 const uint8_t *s; | |
53 | |
5337 | 54 mask = ff_png_pass_mask[pass]; |
2342 | 55 switch(bits_per_pixel) { |
56 case 1: | |
57 memset(dst, 0, row_size); | |
58 dst_x = 0; | |
59 for(x = 0; x < width; x++) { | |
60 j = (x & 7); | |
61 if ((mask << j) & 0x80) { | |
62 b = (src[x >> 3] >> (7 - j)) & 1; | |
63 dst[dst_x >> 3] |= b << (7 - (dst_x & 7)); | |
64 dst_x++; | |
65 } | |
66 } | |
67 break; | |
68 default: | |
69 bpp = bits_per_pixel >> 3; | |
70 d = dst; | |
71 s = src; | |
72 for(x = 0; x < width; x++) { | |
73 j = x & 7; | |
74 if ((mask << j) & 0x80) { | |
75 memcpy(d, s, bpp); | |
76 d += bpp; | |
77 } | |
78 s += bpp; | |
79 } | |
80 break; | |
81 } | |
82 } | |
83 | |
4515 | 84 static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width) |
2342 | 85 { |
86 uint8_t *d; | |
87 int j; | |
88 unsigned int v; | |
2967 | 89 |
2342 | 90 d = dst; |
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 | 93 d[0] = v >> 16; |
94 d[1] = v >> 8; | |
95 d[2] = v; | |
96 d[3] = v >> 24; | |
97 d += 4; | |
98 } | |
99 } | |
100 | |
101 static void png_write_chunk(uint8_t **f, uint32_t tag, | |
102 const uint8_t *buf, int length) | |
103 { | |
104 uint32_t crc; | |
105 uint8_t tagbuf[4]; | |
106 | |
5067 | 107 bytestream_put_be32(f, length); |
2342 | 108 crc = crc32(0, Z_NULL, 0); |
5089 | 109 AV_WL32(tagbuf, tag); |
2342 | 110 crc = crc32(crc, tagbuf, 4); |
5067 | 111 bytestream_put_be32(f, bswap_32(tag)); |
2342 | 112 if (length > 0) { |
113 crc = crc32(crc, buf, length); | |
114 memcpy(*f, buf, length); | |
115 *f += length; | |
116 } | |
5067 | 117 bytestream_put_be32(f, crc); |
2342 | 118 } |
119 | |
120 /* XXX: do filtering */ | |
5339 | 121 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size) |
2342 | 122 { |
123 int ret; | |
124 | |
125 s->zstream.avail_in = size; | |
126 s->zstream.next_in = (uint8_t *)data; | |
127 while (s->zstream.avail_in > 0) { | |
128 ret = deflate(&s->zstream, Z_NO_FLUSH); | |
129 if (ret != Z_OK) | |
130 return -1; | |
131 if (s->zstream.avail_out == 0) { | |
2422 | 132 if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100) |
133 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE); | |
2342 | 134 s->zstream.avail_out = IOBUF_SIZE; |
135 s->zstream.next_out = s->buf; | |
136 } | |
137 } | |
138 return 0; | |
139 } | |
140 | |
141 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ | |
5339 | 142 PNGEncContext *s = avctx->priv_data; |
2342 | 143 AVFrame *pict = data; |
144 AVFrame * const p= (AVFrame*)&s->picture; | |
145 int bit_depth, color_type, y, len, row_size, ret, is_progressive; | |
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 | 148 uint8_t *ptr; |
149 uint8_t *crow_buf = NULL; | |
150 uint8_t *tmp_buf = NULL; | |
151 | |
152 *p = *pict; | |
153 p->pict_type= FF_I_TYPE; | |
154 p->key_frame= 1; | |
2967 | 155 |
2342 | 156 s->bytestream_start= |
157 s->bytestream= buf; | |
158 s->bytestream_end= buf+buf_size; | |
159 | |
160 is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT); | |
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 | 163 bit_depth = 8; |
164 color_type = PNG_COLOR_TYPE_RGB_ALPHA; | |
165 break; | |
166 case PIX_FMT_RGB24: | |
167 bit_depth = 8; | |
168 color_type = PNG_COLOR_TYPE_RGB; | |
169 break; | |
170 case PIX_FMT_GRAY8: | |
171 bit_depth = 8; | |
172 color_type = PNG_COLOR_TYPE_GRAY; | |
173 break; | |
174 case PIX_FMT_MONOBLACK: | |
175 bit_depth = 1; | |
176 color_type = PNG_COLOR_TYPE_GRAY; | |
177 break; | |
178 case PIX_FMT_PAL8: | |
179 bit_depth = 8; | |
180 color_type = PNG_COLOR_TYPE_PALETTE; | |
181 break; | |
182 default: | |
183 return -1; | |
184 } | |
5337 | 185 bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth; |
2342 | 186 row_size = (avctx->width * bits_per_pixel + 7) >> 3; |
187 | |
5337 | 188 s->zstream.zalloc = ff_png_zalloc; |
189 s->zstream.zfree = ff_png_zfree; | |
2342 | 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 | 195 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY); |
196 if (ret != Z_OK) | |
197 return -1; | |
198 crow_buf = av_malloc(row_size + 1); | |
199 if (!crow_buf) | |
200 goto fail; | |
201 if (is_progressive) { | |
202 tmp_buf = av_malloc(row_size + 1); | |
203 if (!tmp_buf) | |
204 goto fail; | |
205 } | |
206 | |
207 /* write png header */ | |
5337 | 208 memcpy(s->bytestream, ff_pngsig, 8); |
2342 | 209 s->bytestream += 8; |
2967 | 210 |
5067 | 211 AV_WB32(s->buf, avctx->width); |
212 AV_WB32(s->buf + 4, avctx->height); | |
2342 | 213 s->buf[8] = bit_depth; |
214 s->buf[9] = color_type; | |
215 s->buf[10] = 0; /* compression type */ | |
216 s->buf[11] = 0; /* filter type */ | |
217 s->buf[12] = is_progressive; /* interlace type */ | |
2967 | 218 |
2342 | 219 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13); |
220 | |
221 /* put the palette if needed */ | |
222 if (color_type == PNG_COLOR_TYPE_PALETTE) { | |
223 int has_alpha, alpha, i; | |
224 unsigned int v; | |
225 uint32_t *palette; | |
226 uint8_t *alpha_ptr; | |
2967 | 227 |
2342 | 228 palette = (uint32_t *)p->data[1]; |
229 ptr = s->buf; | |
230 alpha_ptr = s->buf + 256 * 3; | |
231 has_alpha = 0; | |
232 for(i = 0; i < 256; i++) { | |
233 v = palette[i]; | |
234 alpha = v >> 24; | |
4018 | 235 if (alpha && alpha != 0xff) |
2342 | 236 has_alpha = 1; |
237 *alpha_ptr++ = alpha; | |
5089 | 238 bytestream_put_be24(&ptr, v); |
2342 | 239 } |
240 png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3); | |
241 if (has_alpha) { | |
242 png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256); | |
243 } | |
244 } | |
245 | |
246 /* now put each row */ | |
247 s->zstream.avail_out = IOBUF_SIZE; | |
248 s->zstream.next_out = s->buf; | |
249 if (is_progressive) { | |
250 uint8_t *ptr1; | |
251 int pass; | |
252 | |
253 for(pass = 0; pass < NB_PASSES; pass++) { | |
254 /* NOTE: a pass is completely omited if no pixels would be | |
255 output */ | |
5337 | 256 pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width); |
2342 | 257 if (pass_row_size > 0) { |
258 for(y = 0; y < avctx->height; y++) { | |
5337 | 259 if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) { |
2342 | 260 ptr = p->data[0] + y * p->linesize[0]; |
261 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) { | |
4515 | 262 convert_from_rgb32(tmp_buf, ptr, avctx->width); |
2342 | 263 ptr1 = tmp_buf; |
264 } else { | |
265 ptr1 = ptr; | |
266 } | |
2967 | 267 png_get_interlaced_row(crow_buf + 1, pass_row_size, |
268 bits_per_pixel, pass, | |
2342 | 269 ptr1, avctx->width); |
270 crow_buf[0] = PNG_FILTER_VALUE_NONE; | |
271 png_write_row(s, crow_buf, pass_row_size + 1); | |
272 } | |
273 } | |
274 } | |
275 } | |
276 } else { | |
277 for(y = 0; y < avctx->height; y++) { | |
278 ptr = p->data[0] + y * p->linesize[0]; | |
279 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) | |
4515 | 280 convert_from_rgb32(crow_buf + 1, ptr, avctx->width); |
2342 | 281 else |
282 memcpy(crow_buf + 1, ptr, row_size); | |
283 crow_buf[0] = PNG_FILTER_VALUE_NONE; | |
284 png_write_row(s, crow_buf, row_size + 1); | |
285 } | |
286 } | |
287 /* compress last bytes */ | |
288 for(;;) { | |
289 ret = deflate(&s->zstream, Z_FINISH); | |
290 if (ret == Z_OK || ret == Z_STREAM_END) { | |
291 len = IOBUF_SIZE - s->zstream.avail_out; | |
2422 | 292 if (len > 0 && s->bytestream_end - s->bytestream > len + 100) { |
2342 | 293 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len); |
294 } | |
295 s->zstream.avail_out = IOBUF_SIZE; | |
296 s->zstream.next_out = s->buf; | |
297 if (ret == Z_STREAM_END) | |
298 break; | |
299 } else { | |
300 goto fail; | |
301 } | |
302 } | |
303 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0); | |
304 | |
305 ret = s->bytestream - s->bytestream_start; | |
306 the_end: | |
307 av_free(crow_buf); | |
308 av_free(tmp_buf); | |
309 deflateEnd(&s->zstream); | |
310 return ret; | |
311 fail: | |
312 ret = -1; | |
313 goto the_end; | |
314 } | |
315 | |
5339 | 316 static int png_enc_init(AVCodecContext *avctx){ |
317 PNGEncContext *s = avctx->priv_data; | |
318 | |
319 avcodec_get_frame_defaults((AVFrame*)&s->picture); | |
320 avctx->coded_frame= (AVFrame*)&s->picture; | |
321 | |
322 return 0; | |
323 } | |
324 | |
2342 | 325 AVCodec png_encoder = { |
326 "png", | |
327 CODEC_TYPE_VIDEO, | |
328 CODEC_ID_PNG, | |
5339 | 329 sizeof(PNGEncContext), |
330 png_enc_init, | |
2342 | 331 encode_frame, |
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 | 334 }; |