comparison gif.c @ 4125:f6f67a8bdd09 libavcodec

change gif muxer to simple gif encoder
author bcoudurier
date Thu, 02 Nov 2006 23:13:34 +0000
parents
children bff60ecc02f9
comparison
equal deleted inserted replaced
4124:22de53ac033a 4125:f6f67a8bdd09
1 /*
2 * GIF encoder.
3 * Copyright (c) 2000 Fabrice Bellard.
4 * Copyright (c) 2002 Francois Revol.
5 * Copyright (c) 2006 Baptiste Coudurier.
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /*
25 * First version by Francois Revol revol@free.fr
26 *
27 * Features and limitations:
28 * - currently no compression is performed,
29 * in fact the size of the data is 9/8 the size of the image in 8bpp
30 * - uses only a global standard palette
31 * - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
32 *
33 * Reference documents:
34 * http://www.goice.co.jp/member/mo/formats/gif.html
35 * http://astronomy.swin.edu.au/pbourke/dataformats/gif/
36 * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
37 *
38 * this url claims to have an LZW algorithm not covered by Unisys patent:
39 * http://www.msg.net/utility/whirlgif/gifencod.html
40 * could help reduce the size of the files _a lot_...
41 * some sites mentions an RLE type compression also.
42 */
43
44 #include "avcodec.h"
45 #include "bytestream.h"
46 #include "bitstream.h"
47
48 /* bitstream minipacket size */
49 #define GIF_CHUNKS 100
50
51 /* slows down the decoding (and some browsers don't like it) */
52 /* update on the 'some browsers don't like it issue from above: this was probably due to missing 'Data Sub-block Terminator' (byte 19) in the app_header */
53 #define GIF_ADD_APP_HEADER // required to enable looping of animated gif
54
55 typedef struct {
56 unsigned char r;
57 unsigned char g;
58 unsigned char b;
59 } rgb_triplet;
60
61 /* we use the standard 216 color palette */
62
63 /* this script was used to create the palette:
64 * for r in 00 33 66 99 cc ff; do for g in 00 33 66 99 cc ff; do echo -n " "; for b in 00 33 66 99 cc ff; do
65 * echo -n "{ 0x$r, 0x$g, 0x$b }, "; done; echo ""; done; done
66 */
67
68 static const rgb_triplet gif_clut[216] = {
69 { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x33 }, { 0x00, 0x00, 0x66 }, { 0x00, 0x00, 0x99 }, { 0x00, 0x00, 0xcc }, { 0x00, 0x00, 0xff },
70 { 0x00, 0x33, 0x00 }, { 0x00, 0x33, 0x33 }, { 0x00, 0x33, 0x66 }, { 0x00, 0x33, 0x99 }, { 0x00, 0x33, 0xcc }, { 0x00, 0x33, 0xff },
71 { 0x00, 0x66, 0x00 }, { 0x00, 0x66, 0x33 }, { 0x00, 0x66, 0x66 }, { 0x00, 0x66, 0x99 }, { 0x00, 0x66, 0xcc }, { 0x00, 0x66, 0xff },
72 { 0x00, 0x99, 0x00 }, { 0x00, 0x99, 0x33 }, { 0x00, 0x99, 0x66 }, { 0x00, 0x99, 0x99 }, { 0x00, 0x99, 0xcc }, { 0x00, 0x99, 0xff },
73 { 0x00, 0xcc, 0x00 }, { 0x00, 0xcc, 0x33 }, { 0x00, 0xcc, 0x66 }, { 0x00, 0xcc, 0x99 }, { 0x00, 0xcc, 0xcc }, { 0x00, 0xcc, 0xff },
74 { 0x00, 0xff, 0x00 }, { 0x00, 0xff, 0x33 }, { 0x00, 0xff, 0x66 }, { 0x00, 0xff, 0x99 }, { 0x00, 0xff, 0xcc }, { 0x00, 0xff, 0xff },
75 { 0x33, 0x00, 0x00 }, { 0x33, 0x00, 0x33 }, { 0x33, 0x00, 0x66 }, { 0x33, 0x00, 0x99 }, { 0x33, 0x00, 0xcc }, { 0x33, 0x00, 0xff },
76 { 0x33, 0x33, 0x00 }, { 0x33, 0x33, 0x33 }, { 0x33, 0x33, 0x66 }, { 0x33, 0x33, 0x99 }, { 0x33, 0x33, 0xcc }, { 0x33, 0x33, 0xff },
77 { 0x33, 0x66, 0x00 }, { 0x33, 0x66, 0x33 }, { 0x33, 0x66, 0x66 }, { 0x33, 0x66, 0x99 }, { 0x33, 0x66, 0xcc }, { 0x33, 0x66, 0xff },
78 { 0x33, 0x99, 0x00 }, { 0x33, 0x99, 0x33 }, { 0x33, 0x99, 0x66 }, { 0x33, 0x99, 0x99 }, { 0x33, 0x99, 0xcc }, { 0x33, 0x99, 0xff },
79 { 0x33, 0xcc, 0x00 }, { 0x33, 0xcc, 0x33 }, { 0x33, 0xcc, 0x66 }, { 0x33, 0xcc, 0x99 }, { 0x33, 0xcc, 0xcc }, { 0x33, 0xcc, 0xff },
80 { 0x33, 0xff, 0x00 }, { 0x33, 0xff, 0x33 }, { 0x33, 0xff, 0x66 }, { 0x33, 0xff, 0x99 }, { 0x33, 0xff, 0xcc }, { 0x33, 0xff, 0xff },
81 { 0x66, 0x00, 0x00 }, { 0x66, 0x00, 0x33 }, { 0x66, 0x00, 0x66 }, { 0x66, 0x00, 0x99 }, { 0x66, 0x00, 0xcc }, { 0x66, 0x00, 0xff },
82 { 0x66, 0x33, 0x00 }, { 0x66, 0x33, 0x33 }, { 0x66, 0x33, 0x66 }, { 0x66, 0x33, 0x99 }, { 0x66, 0x33, 0xcc }, { 0x66, 0x33, 0xff },
83 { 0x66, 0x66, 0x00 }, { 0x66, 0x66, 0x33 }, { 0x66, 0x66, 0x66 }, { 0x66, 0x66, 0x99 }, { 0x66, 0x66, 0xcc }, { 0x66, 0x66, 0xff },
84 { 0x66, 0x99, 0x00 }, { 0x66, 0x99, 0x33 }, { 0x66, 0x99, 0x66 }, { 0x66, 0x99, 0x99 }, { 0x66, 0x99, 0xcc }, { 0x66, 0x99, 0xff },
85 { 0x66, 0xcc, 0x00 }, { 0x66, 0xcc, 0x33 }, { 0x66, 0xcc, 0x66 }, { 0x66, 0xcc, 0x99 }, { 0x66, 0xcc, 0xcc }, { 0x66, 0xcc, 0xff },
86 { 0x66, 0xff, 0x00 }, { 0x66, 0xff, 0x33 }, { 0x66, 0xff, 0x66 }, { 0x66, 0xff, 0x99 }, { 0x66, 0xff, 0xcc }, { 0x66, 0xff, 0xff },
87 { 0x99, 0x00, 0x00 }, { 0x99, 0x00, 0x33 }, { 0x99, 0x00, 0x66 }, { 0x99, 0x00, 0x99 }, { 0x99, 0x00, 0xcc }, { 0x99, 0x00, 0xff },
88 { 0x99, 0x33, 0x00 }, { 0x99, 0x33, 0x33 }, { 0x99, 0x33, 0x66 }, { 0x99, 0x33, 0x99 }, { 0x99, 0x33, 0xcc }, { 0x99, 0x33, 0xff },
89 { 0x99, 0x66, 0x00 }, { 0x99, 0x66, 0x33 }, { 0x99, 0x66, 0x66 }, { 0x99, 0x66, 0x99 }, { 0x99, 0x66, 0xcc }, { 0x99, 0x66, 0xff },
90 { 0x99, 0x99, 0x00 }, { 0x99, 0x99, 0x33 }, { 0x99, 0x99, 0x66 }, { 0x99, 0x99, 0x99 }, { 0x99, 0x99, 0xcc }, { 0x99, 0x99, 0xff },
91 { 0x99, 0xcc, 0x00 }, { 0x99, 0xcc, 0x33 }, { 0x99, 0xcc, 0x66 }, { 0x99, 0xcc, 0x99 }, { 0x99, 0xcc, 0xcc }, { 0x99, 0xcc, 0xff },
92 { 0x99, 0xff, 0x00 }, { 0x99, 0xff, 0x33 }, { 0x99, 0xff, 0x66 }, { 0x99, 0xff, 0x99 }, { 0x99, 0xff, 0xcc }, { 0x99, 0xff, 0xff },
93 { 0xcc, 0x00, 0x00 }, { 0xcc, 0x00, 0x33 }, { 0xcc, 0x00, 0x66 }, { 0xcc, 0x00, 0x99 }, { 0xcc, 0x00, 0xcc }, { 0xcc, 0x00, 0xff },
94 { 0xcc, 0x33, 0x00 }, { 0xcc, 0x33, 0x33 }, { 0xcc, 0x33, 0x66 }, { 0xcc, 0x33, 0x99 }, { 0xcc, 0x33, 0xcc }, { 0xcc, 0x33, 0xff },
95 { 0xcc, 0x66, 0x00 }, { 0xcc, 0x66, 0x33 }, { 0xcc, 0x66, 0x66 }, { 0xcc, 0x66, 0x99 }, { 0xcc, 0x66, 0xcc }, { 0xcc, 0x66, 0xff },
96 { 0xcc, 0x99, 0x00 }, { 0xcc, 0x99, 0x33 }, { 0xcc, 0x99, 0x66 }, { 0xcc, 0x99, 0x99 }, { 0xcc, 0x99, 0xcc }, { 0xcc, 0x99, 0xff },
97 { 0xcc, 0xcc, 0x00 }, { 0xcc, 0xcc, 0x33 }, { 0xcc, 0xcc, 0x66 }, { 0xcc, 0xcc, 0x99 }, { 0xcc, 0xcc, 0xcc }, { 0xcc, 0xcc, 0xff },
98 { 0xcc, 0xff, 0x00 }, { 0xcc, 0xff, 0x33 }, { 0xcc, 0xff, 0x66 }, { 0xcc, 0xff, 0x99 }, { 0xcc, 0xff, 0xcc }, { 0xcc, 0xff, 0xff },
99 { 0xff, 0x00, 0x00 }, { 0xff, 0x00, 0x33 }, { 0xff, 0x00, 0x66 }, { 0xff, 0x00, 0x99 }, { 0xff, 0x00, 0xcc }, { 0xff, 0x00, 0xff },
100 { 0xff, 0x33, 0x00 }, { 0xff, 0x33, 0x33 }, { 0xff, 0x33, 0x66 }, { 0xff, 0x33, 0x99 }, { 0xff, 0x33, 0xcc }, { 0xff, 0x33, 0xff },
101 { 0xff, 0x66, 0x00 }, { 0xff, 0x66, 0x33 }, { 0xff, 0x66, 0x66 }, { 0xff, 0x66, 0x99 }, { 0xff, 0x66, 0xcc }, { 0xff, 0x66, 0xff },
102 { 0xff, 0x99, 0x00 }, { 0xff, 0x99, 0x33 }, { 0xff, 0x99, 0x66 }, { 0xff, 0x99, 0x99 }, { 0xff, 0x99, 0xcc }, { 0xff, 0x99, 0xff },
103 { 0xff, 0xcc, 0x00 }, { 0xff, 0xcc, 0x33 }, { 0xff, 0xcc, 0x66 }, { 0xff, 0xcc, 0x99 }, { 0xff, 0xcc, 0xcc }, { 0xff, 0xcc, 0xff },
104 { 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0x33 }, { 0xff, 0xff, 0x66 }, { 0xff, 0xff, 0x99 }, { 0xff, 0xff, 0xcc }, { 0xff, 0xff, 0xff },
105 };
106
107 /* The GIF format uses reversed order for bitstreams... */
108 /* at least they don't use PDP_ENDIAN :) */
109 /* so we 'extend' PutBitContext. hmmm, OOP :) */
110 /* seems this thing changed slightly since I wrote it... */
111
112 #ifdef ALT_BITSTREAM_WRITER
113 # error no ALT_BITSTREAM_WRITER support for now
114 #endif
115
116 static void gif_put_bits_rev(PutBitContext *s, int n, unsigned int value)
117 {
118 unsigned int bit_buf;
119 int bit_cnt;
120
121 // printf("put_bits=%d %x\n", n, value);
122 assert(n == 32 || value < (1U << n));
123
124 bit_buf = s->bit_buf;
125 bit_cnt = 32 - s->bit_left; /* XXX:lazyness... was = s->bit_cnt; */
126
127 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
128 /* XXX: optimize */
129 if (n < (32-bit_cnt)) {
130 bit_buf |= value << (bit_cnt);
131 bit_cnt+=n;
132 } else {
133 bit_buf |= value << (bit_cnt);
134
135 *s->buf_ptr = bit_buf & 0xff;
136 s->buf_ptr[1] = (bit_buf >> 8) & 0xff;
137 s->buf_ptr[2] = (bit_buf >> 16) & 0xff;
138 s->buf_ptr[3] = (bit_buf >> 24) & 0xff;
139
140 //printf("bitbuf = %08x\n", bit_buf);
141 s->buf_ptr+=4;
142 if (s->buf_ptr >= s->buf_end)
143 puts("bit buffer overflow !!"); // should never happen ! who got rid of the callback ???
144 // flush_buffer_rev(s);
145 bit_cnt=bit_cnt + n - 32;
146 if (bit_cnt == 0) {
147 bit_buf = 0;
148 } else {
149 bit_buf = value >> (n - bit_cnt);
150 }
151 }
152
153 s->bit_buf = bit_buf;
154 s->bit_left = 32 - bit_cnt;
155 }
156
157 /* pad the end of the output stream with zeros */
158 static void gif_flush_put_bits_rev(PutBitContext *s)
159 {
160 while (s->bit_left < 32) {
161 /* XXX: should test end of buffer */
162 *s->buf_ptr++=s->bit_buf & 0xff;
163 s->bit_buf>>=8;
164 s->bit_left+=8;
165 }
166 // flush_buffer_rev(s);
167 s->bit_left=32;
168 s->bit_buf=0;
169 }
170
171 /* !RevPutBitContext */
172
173 /* GIF header */
174 static int gif_image_write_header(uint8_t **bytestream,
175 int width, int height, int loop_count,
176 uint32_t *palette)
177 {
178 int i;
179 unsigned int v;
180
181 bytestream_put_buffer(bytestream, "GIF", 3);
182 bytestream_put_buffer(bytestream, "89a", 3);
183 bytestream_put_le16(bytestream, width);
184 bytestream_put_le16(bytestream, height);
185
186 bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
187 bytestream_put_byte(bytestream, 0x1f); /* background color index */
188 bytestream_put_byte(bytestream, 0); /* aspect ratio */
189
190 /* the global palette */
191 if (!palette) {
192 bytestream_put_buffer(bytestream, (const unsigned char *)gif_clut, 216*3);
193 for(i=0;i<((256-216)*3);i++)
194 bytestream_put_byte(bytestream, 0);
195 } else {
196 for(i=0;i<256;i++) {
197 v = palette[i];
198 bytestream_put_byte(bytestream, (v >> 16) & 0xff);
199 bytestream_put_byte(bytestream, (v >> 8) & 0xff);
200 bytestream_put_byte(bytestream, (v) & 0xff);
201 }
202 }
203
204 /* update: this is the 'NETSCAPE EXTENSION' that allows for looped animated gif
205 see http://members.aol.com/royalef/gifabout.htm#net-extension
206
207 byte 1 : 33 (hex 0x21) GIF Extension code
208 byte 2 : 255 (hex 0xFF) Application Extension Label
209 byte 3 : 11 (hex (0x0B) Length of Application Block
210 (eleven bytes of data to follow)
211 bytes 4 to 11 : "NETSCAPE"
212 bytes 12 to 14 : "2.0"
213 byte 15 : 3 (hex 0x03) Length of Data Sub-Block
214 (three bytes of data to follow)
215 byte 16 : 1 (hex 0x01)
216 bytes 17 to 18 : 0 to 65535, an unsigned integer in
217 lo-hi byte format. This indicate the
218 number of iterations the loop should
219 be executed.
220 bytes 19 : 0 (hex 0x00) a Data Sub-block Terminator
221 */
222
223 /* application extension header */
224 #ifdef GIF_ADD_APP_HEADER
225 if (loop_count >= 0 && loop_count <= 65535) {
226 bytestream_put_byte(bytestream, 0x21);
227 bytestream_put_byte(bytestream, 0xff);
228 bytestream_put_byte(bytestream, 0x0b);
229 bytestream_put_buffer(bytestream, "NETSCAPE2.0", 11); // bytes 4 to 14
230 bytestream_put_byte(bytestream, 0x03); // byte 15
231 bytestream_put_byte(bytestream, 0x01); // byte 16
232 bytestream_put_le16(bytestream, (uint16_t)loop_count);
233 bytestream_put_byte(bytestream, 0x00); // byte 19
234 }
235 #endif
236 return 0;
237 }
238
239 /* this is maybe slow, but allows for extensions */
240 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
241 {
242 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
243 }
244
245
246 static int gif_image_write_image(uint8_t **bytestream,
247 int x1, int y1, int width, int height,
248 const uint8_t *buf, int linesize, int pix_fmt)
249 {
250 PutBitContext p;
251 uint8_t buffer[200]; /* 100 * 9 / 8 = 113 */
252 int i, left, w, v;
253 const uint8_t *ptr;
254 /* image block */
255
256 bytestream_put_byte(bytestream, 0x2c);
257 bytestream_put_le16(bytestream, x1);
258 bytestream_put_le16(bytestream, y1);
259 bytestream_put_le16(bytestream, width);
260 bytestream_put_le16(bytestream, height);
261 bytestream_put_byte(bytestream, 0x00); /* flags */
262 /* no local clut */
263
264 bytestream_put_byte(bytestream, 0x08);
265
266 left= width * height;
267
268 init_put_bits(&p, buffer, 130);
269
270 /*
271 * the thing here is the bitstream is written as little packets, with a size byte before
272 * but it's still the same bitstream between packets (no flush !)
273 */
274 ptr = buf;
275 w = width;
276 while(left>0) {
277
278 gif_put_bits_rev(&p, 9, 0x0100); /* clear code */
279
280 for(i=(left<GIF_CHUNKS)?left:GIF_CHUNKS;i;i--) {
281 if (pix_fmt == PIX_FMT_RGB24) {
282 v = gif_clut_index(ptr[0], ptr[1], ptr[2]);
283 ptr+=3;
284 } else {
285 v = *ptr++;
286 }
287 gif_put_bits_rev(&p, 9, v);
288 if (--w == 0) {
289 w = width;
290 buf += linesize;
291 ptr = buf;
292 }
293 }
294
295 if(left<=GIF_CHUNKS) {
296 gif_put_bits_rev(&p, 9, 0x101); /* end of stream */
297 gif_flush_put_bits_rev(&p);
298 }
299 if(pbBufPtr(&p) - p.buf > 0) {
300 bytestream_put_byte(bytestream, pbBufPtr(&p) - p.buf); /* byte count of the packet */
301 bytestream_put_buffer(bytestream, p.buf, pbBufPtr(&p) - p.buf); /* the actual buffer */
302 p.buf_ptr = p.buf; /* dequeue the bytes off the bitstream */
303 }
304 left-=GIF_CHUNKS;
305 }
306 bytestream_put_byte(bytestream, 0x00); /* end of image block */
307 bytestream_put_byte(bytestream, 0x3b);
308 return 0;
309 }
310
311 typedef struct {
312 int64_t time, file_time;
313 uint8_t buffer[100]; /* data chunks */
314 AVFrame picture;
315 } GIFContext;
316
317 static int gif_encode_init(AVCodecContext *avctx)
318 {
319 GIFContext *s = avctx->priv_data;
320
321 avctx->coded_frame = &s->picture;
322 return 0;
323 }
324
325 /* better than nothing gif encoder */
326 static int gif_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data)
327 {
328 GIFContext *s = avctx->priv_data;
329 AVFrame *pict = data;
330 AVFrame *const p = (AVFrame *)&s->picture;
331 uint8_t *outbuf_ptr = outbuf;
332
333 *p = *pict;
334 p->pict_type = FF_I_TYPE;
335 p->key_frame = 1;
336 gif_image_write_header(&outbuf_ptr, avctx->width, avctx->height, -1, (uint32_t *)pict->data[1]);
337 gif_image_write_image(&outbuf_ptr, 0, 0, avctx->width, avctx->height, pict->data[0], pict->linesize[0], PIX_FMT_PAL8);
338 return outbuf_ptr - outbuf;
339 }
340
341 AVCodec gif_encoder = {
342 "gif",
343 CODEC_TYPE_VIDEO,
344 CODEC_ID_GIF,
345 sizeof(GIFContext),
346 gif_encode_init,
347 gif_encode_frame,
348 NULL, //encode_end,
349 .pix_fmts= (enum PixelFormat[]){PIX_FMT_PAL8, -1},
350 };