• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/gif.c

Go to the documentation of this file.
00001 /*
00002  * Animated GIF muxer
00003  * Copyright (c) 2000 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 /*
00023  * First version by Francois Revol revol@free.fr
00024  *
00025  * Features and limitations:
00026  * - currently no compression is performed,
00027  *   in fact the size of the data is 9/8 the size of the image in 8bpp
00028  * - uses only a global standard palette
00029  * - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
00030  *
00031  * Reference documents:
00032  * http://www.goice.co.jp/member/mo/formats/gif.html
00033  * http://astronomy.swin.edu.au/pbourke/dataformats/gif/
00034  * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
00035  *
00036  * this url claims to have an LZW algorithm not covered by Unisys patent:
00037  * http://www.msg.net/utility/whirlgif/gifencod.html
00038  * could help reduce the size of the files _a lot_...
00039  * some sites mentions an RLE type compression also.
00040  */
00041 
00042 #include "avformat.h"
00043 
00044 /* The GIF format uses reversed order for bitstreams... */
00045 /* at least they don't use PDP_ENDIAN :) */
00046 #define BITSTREAM_WRITER_LE
00047 
00048 #include "libavcodec/put_bits.h"
00049 
00050 /* bitstream minipacket size */
00051 #define GIF_CHUNKS 100
00052 
00053 /* slows down the decoding (and some browsers don't like it) */
00054 /* 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 */
00055 #define GIF_ADD_APP_HEADER // required to enable looping of animated gif
00056 
00057 typedef struct {
00058     unsigned char r;
00059     unsigned char g;
00060     unsigned char b;
00061 } rgb_triplet;
00062 
00063 /* we use the standard 216 color palette */
00064 
00065 /* this script was used to create the palette:
00066  * 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
00067  *   echo -n "{ 0x$r, 0x$g, 0x$b }, "; done; echo ""; done; done
00068  */
00069 
00070 static const rgb_triplet gif_clut[216] = {
00071     { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x33 }, { 0x00, 0x00, 0x66 }, { 0x00, 0x00, 0x99 }, { 0x00, 0x00, 0xcc }, { 0x00, 0x00, 0xff },
00072     { 0x00, 0x33, 0x00 }, { 0x00, 0x33, 0x33 }, { 0x00, 0x33, 0x66 }, { 0x00, 0x33, 0x99 }, { 0x00, 0x33, 0xcc }, { 0x00, 0x33, 0xff },
00073     { 0x00, 0x66, 0x00 }, { 0x00, 0x66, 0x33 }, { 0x00, 0x66, 0x66 }, { 0x00, 0x66, 0x99 }, { 0x00, 0x66, 0xcc }, { 0x00, 0x66, 0xff },
00074     { 0x00, 0x99, 0x00 }, { 0x00, 0x99, 0x33 }, { 0x00, 0x99, 0x66 }, { 0x00, 0x99, 0x99 }, { 0x00, 0x99, 0xcc }, { 0x00, 0x99, 0xff },
00075     { 0x00, 0xcc, 0x00 }, { 0x00, 0xcc, 0x33 }, { 0x00, 0xcc, 0x66 }, { 0x00, 0xcc, 0x99 }, { 0x00, 0xcc, 0xcc }, { 0x00, 0xcc, 0xff },
00076     { 0x00, 0xff, 0x00 }, { 0x00, 0xff, 0x33 }, { 0x00, 0xff, 0x66 }, { 0x00, 0xff, 0x99 }, { 0x00, 0xff, 0xcc }, { 0x00, 0xff, 0xff },
00077     { 0x33, 0x00, 0x00 }, { 0x33, 0x00, 0x33 }, { 0x33, 0x00, 0x66 }, { 0x33, 0x00, 0x99 }, { 0x33, 0x00, 0xcc }, { 0x33, 0x00, 0xff },
00078     { 0x33, 0x33, 0x00 }, { 0x33, 0x33, 0x33 }, { 0x33, 0x33, 0x66 }, { 0x33, 0x33, 0x99 }, { 0x33, 0x33, 0xcc }, { 0x33, 0x33, 0xff },
00079     { 0x33, 0x66, 0x00 }, { 0x33, 0x66, 0x33 }, { 0x33, 0x66, 0x66 }, { 0x33, 0x66, 0x99 }, { 0x33, 0x66, 0xcc }, { 0x33, 0x66, 0xff },
00080     { 0x33, 0x99, 0x00 }, { 0x33, 0x99, 0x33 }, { 0x33, 0x99, 0x66 }, { 0x33, 0x99, 0x99 }, { 0x33, 0x99, 0xcc }, { 0x33, 0x99, 0xff },
00081     { 0x33, 0xcc, 0x00 }, { 0x33, 0xcc, 0x33 }, { 0x33, 0xcc, 0x66 }, { 0x33, 0xcc, 0x99 }, { 0x33, 0xcc, 0xcc }, { 0x33, 0xcc, 0xff },
00082     { 0x33, 0xff, 0x00 }, { 0x33, 0xff, 0x33 }, { 0x33, 0xff, 0x66 }, { 0x33, 0xff, 0x99 }, { 0x33, 0xff, 0xcc }, { 0x33, 0xff, 0xff },
00083     { 0x66, 0x00, 0x00 }, { 0x66, 0x00, 0x33 }, { 0x66, 0x00, 0x66 }, { 0x66, 0x00, 0x99 }, { 0x66, 0x00, 0xcc }, { 0x66, 0x00, 0xff },
00084     { 0x66, 0x33, 0x00 }, { 0x66, 0x33, 0x33 }, { 0x66, 0x33, 0x66 }, { 0x66, 0x33, 0x99 }, { 0x66, 0x33, 0xcc }, { 0x66, 0x33, 0xff },
00085     { 0x66, 0x66, 0x00 }, { 0x66, 0x66, 0x33 }, { 0x66, 0x66, 0x66 }, { 0x66, 0x66, 0x99 }, { 0x66, 0x66, 0xcc }, { 0x66, 0x66, 0xff },
00086     { 0x66, 0x99, 0x00 }, { 0x66, 0x99, 0x33 }, { 0x66, 0x99, 0x66 }, { 0x66, 0x99, 0x99 }, { 0x66, 0x99, 0xcc }, { 0x66, 0x99, 0xff },
00087     { 0x66, 0xcc, 0x00 }, { 0x66, 0xcc, 0x33 }, { 0x66, 0xcc, 0x66 }, { 0x66, 0xcc, 0x99 }, { 0x66, 0xcc, 0xcc }, { 0x66, 0xcc, 0xff },
00088     { 0x66, 0xff, 0x00 }, { 0x66, 0xff, 0x33 }, { 0x66, 0xff, 0x66 }, { 0x66, 0xff, 0x99 }, { 0x66, 0xff, 0xcc }, { 0x66, 0xff, 0xff },
00089     { 0x99, 0x00, 0x00 }, { 0x99, 0x00, 0x33 }, { 0x99, 0x00, 0x66 }, { 0x99, 0x00, 0x99 }, { 0x99, 0x00, 0xcc }, { 0x99, 0x00, 0xff },
00090     { 0x99, 0x33, 0x00 }, { 0x99, 0x33, 0x33 }, { 0x99, 0x33, 0x66 }, { 0x99, 0x33, 0x99 }, { 0x99, 0x33, 0xcc }, { 0x99, 0x33, 0xff },
00091     { 0x99, 0x66, 0x00 }, { 0x99, 0x66, 0x33 }, { 0x99, 0x66, 0x66 }, { 0x99, 0x66, 0x99 }, { 0x99, 0x66, 0xcc }, { 0x99, 0x66, 0xff },
00092     { 0x99, 0x99, 0x00 }, { 0x99, 0x99, 0x33 }, { 0x99, 0x99, 0x66 }, { 0x99, 0x99, 0x99 }, { 0x99, 0x99, 0xcc }, { 0x99, 0x99, 0xff },
00093     { 0x99, 0xcc, 0x00 }, { 0x99, 0xcc, 0x33 }, { 0x99, 0xcc, 0x66 }, { 0x99, 0xcc, 0x99 }, { 0x99, 0xcc, 0xcc }, { 0x99, 0xcc, 0xff },
00094     { 0x99, 0xff, 0x00 }, { 0x99, 0xff, 0x33 }, { 0x99, 0xff, 0x66 }, { 0x99, 0xff, 0x99 }, { 0x99, 0xff, 0xcc }, { 0x99, 0xff, 0xff },
00095     { 0xcc, 0x00, 0x00 }, { 0xcc, 0x00, 0x33 }, { 0xcc, 0x00, 0x66 }, { 0xcc, 0x00, 0x99 }, { 0xcc, 0x00, 0xcc }, { 0xcc, 0x00, 0xff },
00096     { 0xcc, 0x33, 0x00 }, { 0xcc, 0x33, 0x33 }, { 0xcc, 0x33, 0x66 }, { 0xcc, 0x33, 0x99 }, { 0xcc, 0x33, 0xcc }, { 0xcc, 0x33, 0xff },
00097     { 0xcc, 0x66, 0x00 }, { 0xcc, 0x66, 0x33 }, { 0xcc, 0x66, 0x66 }, { 0xcc, 0x66, 0x99 }, { 0xcc, 0x66, 0xcc }, { 0xcc, 0x66, 0xff },
00098     { 0xcc, 0x99, 0x00 }, { 0xcc, 0x99, 0x33 }, { 0xcc, 0x99, 0x66 }, { 0xcc, 0x99, 0x99 }, { 0xcc, 0x99, 0xcc }, { 0xcc, 0x99, 0xff },
00099     { 0xcc, 0xcc, 0x00 }, { 0xcc, 0xcc, 0x33 }, { 0xcc, 0xcc, 0x66 }, { 0xcc, 0xcc, 0x99 }, { 0xcc, 0xcc, 0xcc }, { 0xcc, 0xcc, 0xff },
00100     { 0xcc, 0xff, 0x00 }, { 0xcc, 0xff, 0x33 }, { 0xcc, 0xff, 0x66 }, { 0xcc, 0xff, 0x99 }, { 0xcc, 0xff, 0xcc }, { 0xcc, 0xff, 0xff },
00101     { 0xff, 0x00, 0x00 }, { 0xff, 0x00, 0x33 }, { 0xff, 0x00, 0x66 }, { 0xff, 0x00, 0x99 }, { 0xff, 0x00, 0xcc }, { 0xff, 0x00, 0xff },
00102     { 0xff, 0x33, 0x00 }, { 0xff, 0x33, 0x33 }, { 0xff, 0x33, 0x66 }, { 0xff, 0x33, 0x99 }, { 0xff, 0x33, 0xcc }, { 0xff, 0x33, 0xff },
00103     { 0xff, 0x66, 0x00 }, { 0xff, 0x66, 0x33 }, { 0xff, 0x66, 0x66 }, { 0xff, 0x66, 0x99 }, { 0xff, 0x66, 0xcc }, { 0xff, 0x66, 0xff },
00104     { 0xff, 0x99, 0x00 }, { 0xff, 0x99, 0x33 }, { 0xff, 0x99, 0x66 }, { 0xff, 0x99, 0x99 }, { 0xff, 0x99, 0xcc }, { 0xff, 0x99, 0xff },
00105     { 0xff, 0xcc, 0x00 }, { 0xff, 0xcc, 0x33 }, { 0xff, 0xcc, 0x66 }, { 0xff, 0xcc, 0x99 }, { 0xff, 0xcc, 0xcc }, { 0xff, 0xcc, 0xff },
00106     { 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0x33 }, { 0xff, 0xff, 0x66 }, { 0xff, 0xff, 0x99 }, { 0xff, 0xff, 0xcc }, { 0xff, 0xff, 0xff },
00107 };
00108 
00109 /* GIF header */
00110 static int gif_image_write_header(ByteIOContext *pb,
00111                                   int width, int height, int loop_count,
00112                                   uint32_t *palette)
00113 {
00114     int i;
00115     unsigned int v;
00116 
00117     put_tag(pb, "GIF");
00118     put_tag(pb, "89a");
00119     put_le16(pb, width);
00120     put_le16(pb, height);
00121 
00122     put_byte(pb, 0xf7); /* flags: global clut, 256 entries */
00123     put_byte(pb, 0x1f); /* background color index */
00124     put_byte(pb, 0); /* aspect ratio */
00125 
00126     /* the global palette */
00127     if (!palette) {
00128         put_buffer(pb, (const unsigned char *)gif_clut, 216*3);
00129         for(i=0;i<((256-216)*3);i++)
00130             put_byte(pb, 0);
00131     } else {
00132         for(i=0;i<256;i++) {
00133             v = palette[i];
00134             put_byte(pb, (v >> 16) & 0xff);
00135             put_byte(pb, (v >> 8) & 0xff);
00136             put_byte(pb, (v) & 0xff);
00137         }
00138     }
00139 
00140         /*        update: this is the 'NETSCAPE EXTENSION' that allows for looped animated gif
00141                 see http://members.aol.com/royalef/gifabout.htm#net-extension
00142 
00143                 byte   1       : 33 (hex 0x21) GIF Extension code
00144                 byte   2       : 255 (hex 0xFF) Application Extension Label
00145                 byte   3       : 11 (hex (0x0B) Length of Application Block
00146                                          (eleven bytes of data to follow)
00147                 bytes  4 to 11 : "NETSCAPE"
00148                 bytes 12 to 14 : "2.0"
00149                 byte  15       : 3 (hex 0x03) Length of Data Sub-Block
00150                                          (three bytes of data to follow)
00151                 byte  16       : 1 (hex 0x01)
00152                 bytes 17 to 18 : 0 to 65535, an unsigned integer in
00153                                          lo-hi byte format. This indicate the
00154                                          number of iterations the loop should
00155                                          be executed.
00156                 bytes 19       : 0 (hex 0x00) a Data Sub-block Terminator
00157         */
00158 
00159     /* application extension header */
00160 #ifdef GIF_ADD_APP_HEADER
00161     if (loop_count >= 0 && loop_count <= 65535) {
00162     put_byte(pb, 0x21);
00163     put_byte(pb, 0xff);
00164     put_byte(pb, 0x0b);
00165         put_tag(pb, "NETSCAPE2.0");  // bytes 4 to 14
00166         put_byte(pb, 0x03); // byte 15
00167         put_byte(pb, 0x01); // byte 16
00168         put_le16(pb, (uint16_t)loop_count);
00169         put_byte(pb, 0x00); // byte 19
00170     }
00171 #endif
00172     return 0;
00173 }
00174 
00175 /* this is maybe slow, but allows for extensions */
00176 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
00177 {
00178     return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
00179 }
00180 
00181 
00182 static int gif_image_write_image(ByteIOContext *pb,
00183                                  int x1, int y1, int width, int height,
00184                                  const uint8_t *buf, int linesize, int pix_fmt)
00185 {
00186     PutBitContext p;
00187     uint8_t buffer[200]; /* 100 * 9 / 8 = 113 */
00188     int i, left, w, v;
00189     const uint8_t *ptr;
00190     /* image block */
00191 
00192     put_byte(pb, 0x2c);
00193     put_le16(pb, x1);
00194     put_le16(pb, y1);
00195     put_le16(pb, width);
00196     put_le16(pb, height);
00197     put_byte(pb, 0x00); /* flags */
00198     /* no local clut */
00199 
00200     put_byte(pb, 0x08);
00201 
00202     left= width * height;
00203 
00204     init_put_bits(&p, buffer, 130);
00205 
00206 /*
00207  * the thing here is the bitstream is written as little packets, with a size byte before
00208  * but it's still the same bitstream between packets (no flush !)
00209  */
00210     ptr = buf;
00211     w = width;
00212     while(left>0) {
00213 
00214         put_bits(&p, 9, 0x0100); /* clear code */
00215 
00216         for(i=(left<GIF_CHUNKS)?left:GIF_CHUNKS;i;i--) {
00217             if (pix_fmt == PIX_FMT_RGB24) {
00218                 v = gif_clut_index(ptr[0], ptr[1], ptr[2]);
00219                 ptr+=3;
00220             } else {
00221                 v = *ptr++;
00222             }
00223             put_bits(&p, 9, v);
00224             if (--w == 0) {
00225                 w = width;
00226                 buf += linesize;
00227                 ptr = buf;
00228             }
00229         }
00230 
00231         if(left<=GIF_CHUNKS) {
00232             put_bits(&p, 9, 0x101); /* end of stream */
00233             flush_put_bits(&p);
00234         }
00235         if(put_bits_ptr(&p) - p.buf > 0) {
00236             put_byte(pb, put_bits_ptr(&p) - p.buf); /* byte count of the packet */
00237             put_buffer(pb, p.buf, put_bits_ptr(&p) - p.buf); /* the actual buffer */
00238             p.buf_ptr = p.buf; /* dequeue the bytes off the bitstream */
00239         }
00240         left-=GIF_CHUNKS;
00241     }
00242     put_byte(pb, 0x00); /* end of image block */
00243 
00244     return 0;
00245 }
00246 
00247 typedef struct {
00248     int64_t time, file_time;
00249     uint8_t buffer[100]; /* data chunks */
00250 } GIFContext;
00251 
00252 static int gif_write_header(AVFormatContext *s)
00253 {
00254     GIFContext *gif = s->priv_data;
00255     ByteIOContext *pb = s->pb;
00256     AVCodecContext *enc, *video_enc;
00257     int i, width, height, loop_count /*, rate*/;
00258 
00259 /* XXX: do we reject audio streams or just ignore them ?
00260     if(s->nb_streams > 1)
00261         return -1;
00262 */
00263     gif->time = 0;
00264     gif->file_time = 0;
00265 
00266     video_enc = NULL;
00267     for(i=0;i<s->nb_streams;i++) {
00268         enc = s->streams[i]->codec;
00269         if (enc->codec_type != AVMEDIA_TYPE_AUDIO)
00270             video_enc = enc;
00271     }
00272 
00273     if (!video_enc) {
00274         av_free(gif);
00275         return -1;
00276     } else {
00277         width = video_enc->width;
00278         height = video_enc->height;
00279         loop_count = s->loop_output;
00280 //        rate = video_enc->time_base.den;
00281     }
00282 
00283     if (video_enc->pix_fmt != PIX_FMT_RGB24) {
00284         av_log(s, AV_LOG_ERROR, "ERROR: gif only handles the rgb24 pixel format. Use -pix_fmt rgb24.\n");
00285         return AVERROR(EIO);
00286     }
00287 
00288     gif_image_write_header(pb, width, height, loop_count, NULL);
00289 
00290     put_flush_packet(s->pb);
00291     return 0;
00292 }
00293 
00294 static int gif_write_video(AVFormatContext *s,
00295                            AVCodecContext *enc, const uint8_t *buf, int size)
00296 {
00297     ByteIOContext *pb = s->pb;
00298     GIFContext *gif = s->priv_data;
00299     int jiffies;
00300     int64_t delay;
00301 
00302     /* graphic control extension block */
00303     put_byte(pb, 0x21);
00304     put_byte(pb, 0xf9);
00305     put_byte(pb, 0x04); /* block size */
00306     put_byte(pb, 0x04); /* flags */
00307 
00308     /* 1 jiffy is 1/70 s */
00309     /* the delay_time field indicates the number of jiffies - 1 */
00310     delay = gif->file_time - gif->time;
00311 
00312     /* XXX: should use delay, in order to be more accurate */
00313     /* instead of using the same rounded value each time */
00314     /* XXX: don't even remember if I really use it for now */
00315     jiffies = (70*enc->time_base.num/enc->time_base.den) - 1;
00316 
00317     put_le16(pb, jiffies);
00318 
00319     put_byte(pb, 0x1f); /* transparent color index */
00320     put_byte(pb, 0x00);
00321 
00322     gif_image_write_image(pb, 0, 0, enc->width, enc->height,
00323                           buf, enc->width * 3, PIX_FMT_RGB24);
00324 
00325     put_flush_packet(s->pb);
00326     return 0;
00327 }
00328 
00329 static int gif_write_packet(AVFormatContext *s, AVPacket *pkt)
00330 {
00331     AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
00332     if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
00333         return 0; /* just ignore audio */
00334     else
00335         return gif_write_video(s, codec, pkt->data, pkt->size);
00336 }
00337 
00338 static int gif_write_trailer(AVFormatContext *s)
00339 {
00340     ByteIOContext *pb = s->pb;
00341 
00342     put_byte(pb, 0x3b);
00343     put_flush_packet(s->pb);
00344     return 0;
00345 }
00346 
00347 AVOutputFormat gif_muxer = {
00348     "gif",
00349     NULL_IF_CONFIG_SMALL("GIF Animation"),
00350     "image/gif",
00351     "gif",
00352     sizeof(GIFContext),
00353     CODEC_ID_NONE,
00354     CODEC_ID_RAWVIDEO,
00355     gif_write_header,
00356     gif_write_packet,
00357     gif_write_trailer,
00358 };

Generated on Fri Sep 16 2011 17:17:36 for FFmpeg by  doxygen 1.7.1