53
53
#define GIF_CHUNKS 100
55
55
/* slows down the decoding (and some browsers don't like it) */
56
/* 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 */
56
/* update on the 'some browsers don't like it issue from above:
57
* this was probably due to missing 'Data Sub-block Terminator'
58
* (byte 19) in the app_header */
57
59
#define GIF_ADD_APP_HEADER // required to enable looping of animated gif
65
67
/* we use the standard 216 color palette */
67
69
/* this script was used to create the palette:
68
* 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
69
* echo -n "{ 0x$r, 0x$g, 0x$b }, "; done; echo ""; done; done
70
* for r in 00 33 66 99 cc ff; do
71
* for g in 00 33 66 99 cc ff; do
73
* for b in 00 33 66 99 cc ff; do
74
* echo -n "{ 0x$r, 0x$g, 0x$b }, "
72
81
static const rgb_triplet gif_clut[216] = {
112
static int gif_image_write_header(AVIOContext *pb,
113
int width, int height, int loop_count,
121
static int gif_image_write_header(AVIOContext *pb, int width, int height,
122
int loop_count, uint32_t *palette)
128
136
/* the global palette */
130
avio_write(pb, (const unsigned char *)gif_clut, 216*3);
131
for(i=0;i<((256-216)*3);i++)
138
avio_write(pb, (const unsigned char *)gif_clut, 216 * 3);
139
for (i = 0; i < ((256 - 216) * 3); i++)
142
for (i = 0; i < 256; i++) {
136
144
avio_w8(pb, (v >> 16) & 0xff);
137
avio_w8(pb, (v >> 8) & 0xff);
138
avio_w8(pb, (v) & 0xff);
145
avio_w8(pb, (v >> 8) & 0xff);
146
avio_w8(pb, (v) & 0xff);
142
/* update: this is the 'NETSCAPE EXTENSION' that allows for looped animated gif
143
see http://members.aol.com/royalef/gifabout.htm#net-extension
145
byte 1 : 33 (hex 0x21) GIF Extension code
146
byte 2 : 255 (hex 0xFF) Application Extension Label
147
byte 3 : 11 (hex (0x0B) Length of Application Block
148
(eleven bytes of data to follow)
149
bytes 4 to 11 : "NETSCAPE"
150
bytes 12 to 14 : "2.0"
151
byte 15 : 3 (hex 0x03) Length of Data Sub-Block
152
(three bytes of data to follow)
153
byte 16 : 1 (hex 0x01)
154
bytes 17 to 18 : 0 to 65535, an unsigned integer in
155
lo-hi byte format. This indicate the
156
number of iterations the loop should
158
bytes 19 : 0 (hex 0x00) a Data Sub-block Terminator
150
/* update: this is the 'NETSCAPE EXTENSION' that allows for looped animated
151
* GIF, see http://members.aol.com/royalef/gifabout.htm#net-extension
153
* byte 1 : 33 (hex 0x21) GIF Extension code
154
* byte 2 : 255 (hex 0xFF) Application Extension Label
155
* byte 3 : 11 (hex (0x0B) Length of Application Block
156
* (eleven bytes of data to follow)
157
* bytes 4 to 11 : "NETSCAPE"
158
* bytes 12 to 14 : "2.0"
159
* byte 15 : 3 (hex 0x03) Length of Data Sub-Block
160
* (three bytes of data to follow)
161
* byte 16 : 1 (hex 0x01)
162
* bytes 17 to 18 : 0 to 65535, an unsigned integer in
163
* lo-hi byte format. This indicate the
164
* number of iterations the loop should
166
* bytes 19 : 0 (hex 0x00) a Data Sub-block Terminator
161
169
/* application extension header */
162
170
#ifdef GIF_ADD_APP_HEADER
163
171
if (loop_count >= 0 && loop_count <= 65535) {
167
avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1); // bytes 4 to 14
176
avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1);
168
177
avio_w8(pb, 0x03); // byte 15
169
178
avio_w8(pb, 0x01); // byte 16
170
179
avio_wl16(pb, (uint16_t)loop_count);
180
189
return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
184
192
static int gif_image_write_image(AVIOContext *pb,
185
193
int x1, int y1, int width, int height,
186
194
const uint8_t *buf, int linesize, int pix_fmt)
202
210
avio_w8(pb, 0x08);
204
left= width * height;
212
left = width * height;
206
214
init_put_bits(&p, buffer, 130);
209
* the thing here is the bitstream is written as little packets, with a size byte before
210
* but it's still the same bitstream between packets (no flush !)
217
* the thing here is the bitstream is written as little packets, with a size
218
* byte before but it's still the same bitstream between packets (no flush !)
216
223
put_bits(&p, 9, 0x0100); /* clear code */
218
for(i=(left<GIF_CHUNKS)?left:GIF_CHUNKS;i;i--) {
219
if (pix_fmt == PIX_FMT_RGB24) {
220
v = gif_clut_index(ptr[0], ptr[1], ptr[2]);
225
for (i = (left < GIF_CHUNKS) ? left : GIF_CHUNKS; i; i--) {
226
if (pix_fmt == AV_PIX_FMT_RGB24) {
227
v = gif_clut_index(ptr[0], ptr[1], ptr[2]);
225
232
put_bits(&p, 9, v);
233
if(left<=GIF_CHUNKS) {
240
if (left <= GIF_CHUNKS) {
234
241
put_bits(&p, 9, 0x101); /* end of stream */
235
242
flush_put_bits(&p);
237
if(put_bits_ptr(&p) - p.buf > 0) {
244
if (put_bits_ptr(&p) - p.buf > 0) {
238
245
avio_w8(pb, put_bits_ptr(&p) - p.buf); /* byte count of the packet */
239
246
avio_write(pb, p.buf, put_bits_ptr(&p) - p.buf); /* the actual buffer */
240
247
p.buf_ptr = p.buf; /* dequeue the bytes off the bitstream */
244
251
avio_w8(pb, 0x00); /* end of image block */
261
268
int i, width, height /*, rate*/;
263
270
/* XXX: do we reject audio streams or just ignore them ?
264
if(s->nb_streams > 1)
271
* if (s->nb_streams > 1)
268
275
gif->file_time = 0;
270
277
video_enc = NULL;
271
for(i=0;i<s->nb_streams;i++) {
278
for (i = 0; i < s->nb_streams; i++) {
272
279
enc = s->streams[i]->codec;
273
280
if (enc->codec_type != AVMEDIA_TYPE_AUDIO)
281
width = video_enc->width;
288
width = video_enc->width;
282
289
height = video_enc->height;
283
290
// rate = video_enc->time_base.den;
286
if (video_enc->pix_fmt != PIX_FMT_RGB24) {
287
av_log(s, AV_LOG_ERROR, "ERROR: gif only handles the rgb24 pixel format. Use -pix_fmt rgb24.\n");
293
if (video_enc->pix_fmt != AV_PIX_FMT_RGB24) {
294
av_log(s, AV_LOG_ERROR,
295
"ERROR: gif only handles the rgb24 pixel format. Use -pix_fmt rgb24.\n");
288
296
return AVERROR(EIO);
291
#if FF_API_LOOP_OUTPUT
293
gif->loop = s->loop_output;
296
299
gif_image_write_header(pb, width, height, gif->loop, NULL);
298
301
avio_flush(s->pb);
302
static int gif_write_video(AVFormatContext *s,
303
AVCodecContext *enc, const uint8_t *buf, int size)
305
static int gif_write_video(AVFormatContext *s, AVCodecContext *enc,
306
const uint8_t *buf, int size)
305
308
AVIOContext *pb = s->pb;
324
327
avio_w8(pb, 0x00);
326
329
gif_image_write_image(pb, 0, 0, enc->width, enc->height,
327
buf, enc->width * 3, PIX_FMT_RGB24);
330
buf, enc->width * 3, AV_PIX_FMT_RGB24);
329
332
avio_flush(s->pb);
344
347
AVIOContext *pb = s->pb;
346
349
avio_w8(pb, 0x3b);
351
354
#define OFFSET(x) offsetof(GIFContext, x)
352
355
#define ENC AV_OPT_FLAG_ENCODING_PARAM
353
356
static const AVOption options[] = {
354
{ "loop", "Number of times to loop the output.", OFFSET(loop), AV_OPT_TYPE_INT, {0}, 0, 65535, ENC },
357
{ "loop", "Number of times to loop the output.", OFFSET(loop),
358
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 65535, ENC },
365
369
AVOutputFormat ff_gif_muxer = {
367
.long_name = NULL_IF_CONFIG_SMALL("GIF Animation"),
368
.mime_type = "image/gif",
370
.priv_data_size = sizeof(GIFContext),
371
.audio_codec = CODEC_ID_NONE,
372
.video_codec = CODEC_ID_RAWVIDEO,
373
.write_header = gif_write_header,
374
.write_packet = gif_write_packet,
375
.write_trailer = gif_write_trailer,
376
.priv_class = &gif_muxer_class,
371
.long_name = NULL_IF_CONFIG_SMALL("GIF Animation"),
372
.mime_type = "image/gif",
374
.priv_data_size = sizeof(GIFContext),
375
.audio_codec = AV_CODEC_ID_NONE,
376
.video_codec = AV_CODEC_ID_RAWVIDEO,
377
.write_header = gif_write_header,
378
.write_packet = gif_write_packet,
379
.write_trailer = gif_write_trailer,
380
.priv_class = &gif_muxer_class,