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

libavcodec/gif.c

Go to the documentation of this file.
00001 /*
00002  * GIF encoder.
00003  * Copyright (c) 2000 Fabrice Bellard
00004  * Copyright (c) 2002 Francois Revol
00005  * Copyright (c) 2006 Baptiste Coudurier
00006  *
00007  * This file is part of FFmpeg.
00008  *
00009  * FFmpeg is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  * FFmpeg is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with FFmpeg; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00022  */
00023 
00024 /*
00025  * First version by Francois Revol revol@free.fr
00026  *
00027  * Features and limitations:
00028  * - currently no compression is performed,
00029  *   in fact the size of the data is 9/8 the size of the image in 8bpp
00030  * - uses only a global standard palette
00031  * - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
00032  *
00033  * Reference documents:
00034  * http://www.goice.co.jp/member/mo/formats/gif.html
00035  * http://astronomy.swin.edu.au/pbourke/dataformats/gif/
00036  * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
00037  *
00038  * this url claims to have an LZW algorithm not covered by Unisys patent:
00039  * http://www.msg.net/utility/whirlgif/gifencod.html
00040  * could help reduce the size of the files _a lot_...
00041  * some sites mentions an RLE type compression also.
00042  */
00043 
00044 #include "avcodec.h"
00045 #include "bytestream.h"
00046 
00047 /* The GIF format uses reversed order for bitstreams... */
00048 /* at least they don't use PDP_ENDIAN :) */
00049 #define BITSTREAM_WRITER_LE
00050 
00051 #include "bitstream.h"
00052 
00053 /* bitstream minipacket size */
00054 #define GIF_CHUNKS 100
00055 
00056 /* GIF header */
00057 static int gif_image_write_header(uint8_t **bytestream,
00058                                   int width, int height,
00059                                   uint32_t *palette)
00060 {
00061     int i;
00062     unsigned int v;
00063 
00064     bytestream_put_buffer(bytestream, "GIF", 3);
00065     bytestream_put_buffer(bytestream, "89a", 3);
00066     bytestream_put_le16(bytestream, width);
00067     bytestream_put_le16(bytestream, height);
00068 
00069     bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
00070     bytestream_put_byte(bytestream, 0x1f); /* background color index */
00071     bytestream_put_byte(bytestream, 0); /* aspect ratio */
00072 
00073     /* the global palette */
00074     for(i=0;i<256;i++) {
00075         v = palette[i];
00076         bytestream_put_be24(bytestream, v);
00077     }
00078 
00079     return 0;
00080 }
00081 
00082 static int gif_image_write_image(uint8_t **bytestream,
00083                                  int x1, int y1, int width, int height,
00084                                  const uint8_t *buf, int linesize, int pix_fmt)
00085 {
00086     PutBitContext p;
00087     uint8_t buffer[200]; /* 100 * 9 / 8 = 113 */
00088     int i, left, w;
00089     const uint8_t *ptr;
00090     /* image block */
00091 
00092     bytestream_put_byte(bytestream, 0x2c);
00093     bytestream_put_le16(bytestream, x1);
00094     bytestream_put_le16(bytestream, y1);
00095     bytestream_put_le16(bytestream, width);
00096     bytestream_put_le16(bytestream, height);
00097     bytestream_put_byte(bytestream, 0x00); /* flags */
00098     /* no local clut */
00099 
00100     bytestream_put_byte(bytestream, 0x08);
00101 
00102     left= width * height;
00103 
00104     init_put_bits(&p, buffer, 130);
00105 
00106 /*
00107  * the thing here is the bitstream is written as little packets, with a size byte before
00108  * but it's still the same bitstream between packets (no flush !)
00109  */
00110     ptr = buf;
00111     w = width;
00112     while(left>0) {
00113 
00114         put_bits(&p, 9, 0x0100); /* clear code */
00115 
00116         for(i=(left<GIF_CHUNKS)?left:GIF_CHUNKS;i;i--) {
00117             put_bits(&p, 9, *ptr++);
00118             if (--w == 0) {
00119                 w = width;
00120                 buf += linesize;
00121                 ptr = buf;
00122             }
00123         }
00124 
00125         if(left<=GIF_CHUNKS) {
00126             put_bits(&p, 9, 0x101); /* end of stream */
00127             flush_put_bits(&p);
00128         }
00129         if(pbBufPtr(&p) - p.buf > 0) {
00130             bytestream_put_byte(bytestream, pbBufPtr(&p) - p.buf); /* byte count of the packet */
00131             bytestream_put_buffer(bytestream, p.buf, pbBufPtr(&p) - p.buf); /* the actual buffer */
00132             p.buf_ptr = p.buf; /* dequeue the bytes off the bitstream */
00133         }
00134         left-=GIF_CHUNKS;
00135     }
00136     bytestream_put_byte(bytestream, 0x00); /* end of image block */
00137     bytestream_put_byte(bytestream, 0x3b);
00138     return 0;
00139 }
00140 
00141 typedef struct {
00142     AVFrame picture;
00143 } GIFContext;
00144 
00145 static av_cold int gif_encode_init(AVCodecContext *avctx)
00146 {
00147     GIFContext *s = avctx->priv_data;
00148 
00149     avctx->coded_frame = &s->picture;
00150     return 0;
00151 }
00152 
00153 /* better than nothing gif encoder */
00154 static int gif_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data)
00155 {
00156     GIFContext *s = avctx->priv_data;
00157     AVFrame *pict = data;
00158     AVFrame *const p = (AVFrame *)&s->picture;
00159     uint8_t *outbuf_ptr = outbuf;
00160 
00161     *p = *pict;
00162     p->pict_type = FF_I_TYPE;
00163     p->key_frame = 1;
00164     gif_image_write_header(&outbuf_ptr, avctx->width, avctx->height, (uint32_t *)pict->data[1]);
00165     gif_image_write_image(&outbuf_ptr, 0, 0, avctx->width, avctx->height, pict->data[0], pict->linesize[0], PIX_FMT_PAL8);
00166     return outbuf_ptr - outbuf;
00167 }
00168 
00169 AVCodec gif_encoder = {
00170     "gif",
00171     CODEC_TYPE_VIDEO,
00172     CODEC_ID_GIF,
00173     sizeof(GIFContext),
00174     gif_encode_init,
00175     gif_encode_frame,
00176     NULL, //encode_end,
00177     .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8, PIX_FMT_NONE},
00178     .long_name= NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
00179 };

Generated on Sat Feb 16 2013 09:23:12 for ffmpeg by  doxygen 1.7.1