00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "avformat.h"
00043 #include "libavutil/log.h"
00044 #include "libavutil/opt.h"
00045
00046
00047
00048 #define BITSTREAM_WRITER_LE
00049
00050 #include "libavcodec/put_bits.h"
00051
00052
00053 #define GIF_CHUNKS 100
00054
00055
00056
00057
00058
00059 #define GIF_ADD_APP_HEADER // required to enable looping of animated gif
00060
00061 typedef struct {
00062 unsigned char r;
00063 unsigned char g;
00064 unsigned char b;
00065 } rgb_triplet;
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 static const rgb_triplet gif_clut[216] = {
00082 { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x33 }, { 0x00, 0x00, 0x66 }, { 0x00, 0x00, 0x99 }, { 0x00, 0x00, 0xcc }, { 0x00, 0x00, 0xff },
00083 { 0x00, 0x33, 0x00 }, { 0x00, 0x33, 0x33 }, { 0x00, 0x33, 0x66 }, { 0x00, 0x33, 0x99 }, { 0x00, 0x33, 0xcc }, { 0x00, 0x33, 0xff },
00084 { 0x00, 0x66, 0x00 }, { 0x00, 0x66, 0x33 }, { 0x00, 0x66, 0x66 }, { 0x00, 0x66, 0x99 }, { 0x00, 0x66, 0xcc }, { 0x00, 0x66, 0xff },
00085 { 0x00, 0x99, 0x00 }, { 0x00, 0x99, 0x33 }, { 0x00, 0x99, 0x66 }, { 0x00, 0x99, 0x99 }, { 0x00, 0x99, 0xcc }, { 0x00, 0x99, 0xff },
00086 { 0x00, 0xcc, 0x00 }, { 0x00, 0xcc, 0x33 }, { 0x00, 0xcc, 0x66 }, { 0x00, 0xcc, 0x99 }, { 0x00, 0xcc, 0xcc }, { 0x00, 0xcc, 0xff },
00087 { 0x00, 0xff, 0x00 }, { 0x00, 0xff, 0x33 }, { 0x00, 0xff, 0x66 }, { 0x00, 0xff, 0x99 }, { 0x00, 0xff, 0xcc }, { 0x00, 0xff, 0xff },
00088 { 0x33, 0x00, 0x00 }, { 0x33, 0x00, 0x33 }, { 0x33, 0x00, 0x66 }, { 0x33, 0x00, 0x99 }, { 0x33, 0x00, 0xcc }, { 0x33, 0x00, 0xff },
00089 { 0x33, 0x33, 0x00 }, { 0x33, 0x33, 0x33 }, { 0x33, 0x33, 0x66 }, { 0x33, 0x33, 0x99 }, { 0x33, 0x33, 0xcc }, { 0x33, 0x33, 0xff },
00090 { 0x33, 0x66, 0x00 }, { 0x33, 0x66, 0x33 }, { 0x33, 0x66, 0x66 }, { 0x33, 0x66, 0x99 }, { 0x33, 0x66, 0xcc }, { 0x33, 0x66, 0xff },
00091 { 0x33, 0x99, 0x00 }, { 0x33, 0x99, 0x33 }, { 0x33, 0x99, 0x66 }, { 0x33, 0x99, 0x99 }, { 0x33, 0x99, 0xcc }, { 0x33, 0x99, 0xff },
00092 { 0x33, 0xcc, 0x00 }, { 0x33, 0xcc, 0x33 }, { 0x33, 0xcc, 0x66 }, { 0x33, 0xcc, 0x99 }, { 0x33, 0xcc, 0xcc }, { 0x33, 0xcc, 0xff },
00093 { 0x33, 0xff, 0x00 }, { 0x33, 0xff, 0x33 }, { 0x33, 0xff, 0x66 }, { 0x33, 0xff, 0x99 }, { 0x33, 0xff, 0xcc }, { 0x33, 0xff, 0xff },
00094 { 0x66, 0x00, 0x00 }, { 0x66, 0x00, 0x33 }, { 0x66, 0x00, 0x66 }, { 0x66, 0x00, 0x99 }, { 0x66, 0x00, 0xcc }, { 0x66, 0x00, 0xff },
00095 { 0x66, 0x33, 0x00 }, { 0x66, 0x33, 0x33 }, { 0x66, 0x33, 0x66 }, { 0x66, 0x33, 0x99 }, { 0x66, 0x33, 0xcc }, { 0x66, 0x33, 0xff },
00096 { 0x66, 0x66, 0x00 }, { 0x66, 0x66, 0x33 }, { 0x66, 0x66, 0x66 }, { 0x66, 0x66, 0x99 }, { 0x66, 0x66, 0xcc }, { 0x66, 0x66, 0xff },
00097 { 0x66, 0x99, 0x00 }, { 0x66, 0x99, 0x33 }, { 0x66, 0x99, 0x66 }, { 0x66, 0x99, 0x99 }, { 0x66, 0x99, 0xcc }, { 0x66, 0x99, 0xff },
00098 { 0x66, 0xcc, 0x00 }, { 0x66, 0xcc, 0x33 }, { 0x66, 0xcc, 0x66 }, { 0x66, 0xcc, 0x99 }, { 0x66, 0xcc, 0xcc }, { 0x66, 0xcc, 0xff },
00099 { 0x66, 0xff, 0x00 }, { 0x66, 0xff, 0x33 }, { 0x66, 0xff, 0x66 }, { 0x66, 0xff, 0x99 }, { 0x66, 0xff, 0xcc }, { 0x66, 0xff, 0xff },
00100 { 0x99, 0x00, 0x00 }, { 0x99, 0x00, 0x33 }, { 0x99, 0x00, 0x66 }, { 0x99, 0x00, 0x99 }, { 0x99, 0x00, 0xcc }, { 0x99, 0x00, 0xff },
00101 { 0x99, 0x33, 0x00 }, { 0x99, 0x33, 0x33 }, { 0x99, 0x33, 0x66 }, { 0x99, 0x33, 0x99 }, { 0x99, 0x33, 0xcc }, { 0x99, 0x33, 0xff },
00102 { 0x99, 0x66, 0x00 }, { 0x99, 0x66, 0x33 }, { 0x99, 0x66, 0x66 }, { 0x99, 0x66, 0x99 }, { 0x99, 0x66, 0xcc }, { 0x99, 0x66, 0xff },
00103 { 0x99, 0x99, 0x00 }, { 0x99, 0x99, 0x33 }, { 0x99, 0x99, 0x66 }, { 0x99, 0x99, 0x99 }, { 0x99, 0x99, 0xcc }, { 0x99, 0x99, 0xff },
00104 { 0x99, 0xcc, 0x00 }, { 0x99, 0xcc, 0x33 }, { 0x99, 0xcc, 0x66 }, { 0x99, 0xcc, 0x99 }, { 0x99, 0xcc, 0xcc }, { 0x99, 0xcc, 0xff },
00105 { 0x99, 0xff, 0x00 }, { 0x99, 0xff, 0x33 }, { 0x99, 0xff, 0x66 }, { 0x99, 0xff, 0x99 }, { 0x99, 0xff, 0xcc }, { 0x99, 0xff, 0xff },
00106 { 0xcc, 0x00, 0x00 }, { 0xcc, 0x00, 0x33 }, { 0xcc, 0x00, 0x66 }, { 0xcc, 0x00, 0x99 }, { 0xcc, 0x00, 0xcc }, { 0xcc, 0x00, 0xff },
00107 { 0xcc, 0x33, 0x00 }, { 0xcc, 0x33, 0x33 }, { 0xcc, 0x33, 0x66 }, { 0xcc, 0x33, 0x99 }, { 0xcc, 0x33, 0xcc }, { 0xcc, 0x33, 0xff },
00108 { 0xcc, 0x66, 0x00 }, { 0xcc, 0x66, 0x33 }, { 0xcc, 0x66, 0x66 }, { 0xcc, 0x66, 0x99 }, { 0xcc, 0x66, 0xcc }, { 0xcc, 0x66, 0xff },
00109 { 0xcc, 0x99, 0x00 }, { 0xcc, 0x99, 0x33 }, { 0xcc, 0x99, 0x66 }, { 0xcc, 0x99, 0x99 }, { 0xcc, 0x99, 0xcc }, { 0xcc, 0x99, 0xff },
00110 { 0xcc, 0xcc, 0x00 }, { 0xcc, 0xcc, 0x33 }, { 0xcc, 0xcc, 0x66 }, { 0xcc, 0xcc, 0x99 }, { 0xcc, 0xcc, 0xcc }, { 0xcc, 0xcc, 0xff },
00111 { 0xcc, 0xff, 0x00 }, { 0xcc, 0xff, 0x33 }, { 0xcc, 0xff, 0x66 }, { 0xcc, 0xff, 0x99 }, { 0xcc, 0xff, 0xcc }, { 0xcc, 0xff, 0xff },
00112 { 0xff, 0x00, 0x00 }, { 0xff, 0x00, 0x33 }, { 0xff, 0x00, 0x66 }, { 0xff, 0x00, 0x99 }, { 0xff, 0x00, 0xcc }, { 0xff, 0x00, 0xff },
00113 { 0xff, 0x33, 0x00 }, { 0xff, 0x33, 0x33 }, { 0xff, 0x33, 0x66 }, { 0xff, 0x33, 0x99 }, { 0xff, 0x33, 0xcc }, { 0xff, 0x33, 0xff },
00114 { 0xff, 0x66, 0x00 }, { 0xff, 0x66, 0x33 }, { 0xff, 0x66, 0x66 }, { 0xff, 0x66, 0x99 }, { 0xff, 0x66, 0xcc }, { 0xff, 0x66, 0xff },
00115 { 0xff, 0x99, 0x00 }, { 0xff, 0x99, 0x33 }, { 0xff, 0x99, 0x66 }, { 0xff, 0x99, 0x99 }, { 0xff, 0x99, 0xcc }, { 0xff, 0x99, 0xff },
00116 { 0xff, 0xcc, 0x00 }, { 0xff, 0xcc, 0x33 }, { 0xff, 0xcc, 0x66 }, { 0xff, 0xcc, 0x99 }, { 0xff, 0xcc, 0xcc }, { 0xff, 0xcc, 0xff },
00117 { 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0x33 }, { 0xff, 0xff, 0x66 }, { 0xff, 0xff, 0x99 }, { 0xff, 0xff, 0xcc }, { 0xff, 0xff, 0xff },
00118 };
00119
00120
00121 static int gif_image_write_header(AVIOContext *pb, int width, int height,
00122 int loop_count, uint32_t *palette)
00123 {
00124 int i;
00125 unsigned int v;
00126
00127 avio_write(pb, "GIF", 3);
00128 avio_write(pb, "89a", 3);
00129 avio_wl16(pb, width);
00130 avio_wl16(pb, height);
00131
00132 avio_w8(pb, 0xf7);
00133 avio_w8(pb, 0x1f);
00134 avio_w8(pb, 0);
00135
00136
00137 if (!palette) {
00138 avio_write(pb, (const unsigned char *)gif_clut, 216 * 3);
00139 for (i = 0; i < ((256 - 216) * 3); i++)
00140 avio_w8(pb, 0);
00141 } else {
00142 for (i = 0; i < 256; i++) {
00143 v = palette[i];
00144 avio_w8(pb, (v >> 16) & 0xff);
00145 avio_w8(pb, (v >> 8) & 0xff);
00146 avio_w8(pb, (v) & 0xff);
00147 }
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 #ifdef GIF_ADD_APP_HEADER
00171 if (loop_count >= 0 && loop_count <= 65535) {
00172 avio_w8(pb, 0x21);
00173 avio_w8(pb, 0xff);
00174 avio_w8(pb, 0x0b);
00175
00176 avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1);
00177 avio_w8(pb, 0x03);
00178 avio_w8(pb, 0x01);
00179 avio_wl16(pb, (uint16_t)loop_count);
00180 avio_w8(pb, 0x00);
00181 }
00182 #endif
00183 return 0;
00184 }
00185
00186
00187 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
00188 {
00189 return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
00190 }
00191
00192 static int gif_image_write_image(AVIOContext *pb,
00193 int x1, int y1, int width, int height,
00194 const uint8_t *buf, int linesize, int pix_fmt)
00195 {
00196 PutBitContext p;
00197 uint8_t buffer[200];
00198 int i, left, w, v;
00199 const uint8_t *ptr;
00200
00201
00202 avio_w8(pb, 0x2c);
00203 avio_wl16(pb, x1);
00204 avio_wl16(pb, y1);
00205 avio_wl16(pb, width);
00206 avio_wl16(pb, height);
00207 avio_w8(pb, 0x00);
00208
00209
00210 avio_w8(pb, 0x08);
00211
00212 left = width * height;
00213
00214 init_put_bits(&p, buffer, 130);
00215
00216
00217
00218
00219
00220 ptr = buf;
00221 w = width;
00222 while (left > 0) {
00223 put_bits(&p, 9, 0x0100);
00224
00225 for (i = (left < GIF_CHUNKS) ? left : GIF_CHUNKS; i; i--) {
00226 if (pix_fmt == PIX_FMT_RGB24) {
00227 v = gif_clut_index(ptr[0], ptr[1], ptr[2]);
00228 ptr += 3;
00229 } else {
00230 v = *ptr++;
00231 }
00232 put_bits(&p, 9, v);
00233 if (--w == 0) {
00234 w = width;
00235 buf += linesize;
00236 ptr = buf;
00237 }
00238 }
00239
00240 if (left <= GIF_CHUNKS) {
00241 put_bits(&p, 9, 0x101);
00242 flush_put_bits(&p);
00243 }
00244 if (put_bits_ptr(&p) - p.buf > 0) {
00245 avio_w8(pb, put_bits_ptr(&p) - p.buf);
00246 avio_write(pb, p.buf, put_bits_ptr(&p) - p.buf);
00247 p.buf_ptr = p.buf;
00248 }
00249 left -= GIF_CHUNKS;
00250 }
00251 avio_w8(pb, 0x00);
00252
00253 return 0;
00254 }
00255
00256 typedef struct {
00257 AVClass *class;
00258 int64_t time, file_time;
00259 uint8_t buffer[100];
00260 int loop;
00261 } GIFContext;
00262
00263 static int gif_write_header(AVFormatContext *s)
00264 {
00265 GIFContext *gif = s->priv_data;
00266 AVIOContext *pb = s->pb;
00267 AVCodecContext *enc, *video_enc;
00268 int i, width, height ;
00269
00270
00271
00272
00273
00274 gif->time = 0;
00275 gif->file_time = 0;
00276
00277 video_enc = NULL;
00278 for (i = 0; i < s->nb_streams; i++) {
00279 enc = s->streams[i]->codec;
00280 if (enc->codec_type != AVMEDIA_TYPE_AUDIO)
00281 video_enc = enc;
00282 }
00283
00284 if (!video_enc) {
00285 av_free(gif);
00286 return -1;
00287 } else {
00288 width = video_enc->width;
00289 height = video_enc->height;
00290
00291 }
00292
00293 if (video_enc->pix_fmt != PIX_FMT_RGB24) {
00294 av_log(s, AV_LOG_ERROR,
00295 "ERROR: gif only handles the rgb24 pixel format. Use -pix_fmt rgb24.\n");
00296 return AVERROR(EIO);
00297 }
00298
00299 gif_image_write_header(pb, width, height, gif->loop, NULL);
00300
00301 avio_flush(s->pb);
00302 return 0;
00303 }
00304
00305 static int gif_write_video(AVFormatContext *s, AVCodecContext *enc,
00306 const uint8_t *buf, int size)
00307 {
00308 AVIOContext *pb = s->pb;
00309 int jiffies;
00310
00311
00312 avio_w8(pb, 0x21);
00313 avio_w8(pb, 0xf9);
00314 avio_w8(pb, 0x04);
00315 avio_w8(pb, 0x04);
00316
00317
00318
00319
00320
00321
00322 jiffies = (70 * enc->time_base.num / enc->time_base.den) - 1;
00323
00324 avio_wl16(pb, jiffies);
00325
00326 avio_w8(pb, 0x1f);
00327 avio_w8(pb, 0x00);
00328
00329 gif_image_write_image(pb, 0, 0, enc->width, enc->height,
00330 buf, enc->width * 3, PIX_FMT_RGB24);
00331
00332 avio_flush(s->pb);
00333 return 0;
00334 }
00335
00336 static int gif_write_packet(AVFormatContext *s, AVPacket *pkt)
00337 {
00338 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
00339 if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
00340 return 0;
00341 else
00342 return gif_write_video(s, codec, pkt->data, pkt->size);
00343 }
00344
00345 static int gif_write_trailer(AVFormatContext *s)
00346 {
00347 AVIOContext *pb = s->pb;
00348
00349 avio_w8(pb, 0x3b);
00350
00351 return 0;
00352 }
00353
00354 #define OFFSET(x) offsetof(GIFContext, x)
00355 #define ENC AV_OPT_FLAG_ENCODING_PARAM
00356 static const AVOption options[] = {
00357 { "loop", "Number of times to loop the output.", OFFSET(loop),
00358 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 65535, ENC },
00359 { NULL },
00360 };
00361
00362 static const AVClass gif_muxer_class = {
00363 .class_name = "GIF muxer",
00364 .item_name = av_default_item_name,
00365 .version = LIBAVUTIL_VERSION_INT,
00366 .option = options,
00367 };
00368
00369 AVOutputFormat ff_gif_muxer = {
00370 .name = "gif",
00371 .long_name = NULL_IF_CONFIG_SMALL("GIF Animation"),
00372 .mime_type = "image/gif",
00373 .extensions = "gif",
00374 .priv_data_size = sizeof(GIFContext),
00375 .audio_codec = AV_CODEC_ID_NONE,
00376 .video_codec = AV_CODEC_ID_RAWVIDEO,
00377 .write_header = gif_write_header,
00378 .write_packet = gif_write_packet,
00379 .write_trailer = gif_write_trailer,
00380 .priv_class = &gif_muxer_class,
00381 };