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
00043
00044 #include "avcodec.h"
00045 #include "bytestream.h"
00046
00047
00048
00049 #define BITSTREAM_WRITER_LE
00050
00051 #include "bitstream.h"
00052
00053
00054 #define GIF_CHUNKS 100
00055
00056
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);
00070 bytestream_put_byte(bytestream, 0x1f);
00071 bytestream_put_byte(bytestream, 0);
00072
00073
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];
00088 int i, left, w;
00089 const uint8_t *ptr;
00090
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);
00098
00099
00100 bytestream_put_byte(bytestream, 0x08);
00101
00102 left= width * height;
00103
00104 init_put_bits(&p, buffer, 130);
00105
00106
00107
00108
00109
00110 ptr = buf;
00111 w = width;
00112 while(left>0) {
00113
00114 put_bits(&p, 9, 0x0100);
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);
00127 flush_put_bits(&p);
00128 }
00129 if(pbBufPtr(&p) - p.buf > 0) {
00130 bytestream_put_byte(bytestream, pbBufPtr(&p) - p.buf);
00131 bytestream_put_buffer(bytestream, p.buf, pbBufPtr(&p) - p.buf);
00132 p.buf_ptr = p.buf;
00133 }
00134 left-=GIF_CHUNKS;
00135 }
00136 bytestream_put_byte(bytestream, 0x00);
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
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,
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 };