3 * Copyright (c) 2000 Fabrice Bellard
4 * Copyright (c) 2002 Francois Revol
5 * Copyright (c) 2006 Baptiste Coudurier
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * First version by Francois Revol revol@free.fr
27 * Features and limitations:
28 * - currently no compression is performed,
29 * in fact the size of the data is 9/8 the size of the image in 8bpp
30 * - uses only a global standard palette
31 * - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
33 * Reference documents:
34 * http://www.goice.co.jp/member/mo/formats/gif.html
35 * http://astronomy.swin.edu.au/pbourke/dataformats/gif/
36 * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
38 * this url claims to have an LZW algorithm not covered by Unisys patent:
39 * http://www.msg.net/utility/whirlgif/gifencod.html
40 * could help reduce the size of the files _a lot_...
41 * some sites mentions an RLE type compression also.
45 #include "bytestream.h"
48 /* The GIF format uses reversed order for bitstreams... */
49 /* at least they don't use PDP_ENDIAN :) */
50 #define BITSTREAM_WRITER_LE
61 static int gif_image_write_header(AVCodecContext *avctx,
62 uint8_t **bytestream, uint32_t *palette)
65 unsigned int v, smallest_alpha = 0xFF, alpha_component = 0;
67 bytestream_put_buffer(bytestream, "GIF", 3);
68 bytestream_put_buffer(bytestream, "89a", 3);
69 bytestream_put_le16(bytestream, avctx->width);
70 bytestream_put_le16(bytestream, avctx->height);
72 bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
73 bytestream_put_byte(bytestream, 0x1f); /* background color index */
74 bytestream_put_byte(bytestream, 0); /* aspect ratio */
76 /* the global palette */
79 bytestream_put_be24(bytestream, v);
80 if (v >> 24 < smallest_alpha) {
81 smallest_alpha = v >> 24;
86 if (smallest_alpha < 128) {
87 bytestream_put_byte(bytestream, 0x21); /* Extension Introducer */
88 bytestream_put_byte(bytestream, 0xf9); /* Graphic Control Label */
89 bytestream_put_byte(bytestream, 0x04); /* block length */
90 bytestream_put_byte(bytestream, 0x01); /* Transparent Color Flag */
91 bytestream_put_le16(bytestream, 0x00); /* no delay */
92 bytestream_put_byte(bytestream, alpha_component);
93 bytestream_put_byte(bytestream, 0x00);
99 static int gif_image_write_image(AVCodecContext *avctx,
100 uint8_t **bytestream, uint8_t *end,
101 const uint8_t *buf, int linesize)
103 GIFContext *s = avctx->priv_data;
108 bytestream_put_byte(bytestream, 0x2c);
109 bytestream_put_le16(bytestream, 0);
110 bytestream_put_le16(bytestream, 0);
111 bytestream_put_le16(bytestream, avctx->width);
112 bytestream_put_le16(bytestream, avctx->height);
113 bytestream_put_byte(bytestream, 0x00); /* flags */
116 bytestream_put_byte(bytestream, 0x08);
118 ff_lzw_encode_init(s->lzw, s->buf, avctx->width*avctx->height,
119 12, FF_LZW_GIF, put_bits);
122 for (height = avctx->height; height--;) {
123 len += ff_lzw_encode(s->lzw, ptr, avctx->width);
126 len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
130 int size = FFMIN(255, len);
131 bytestream_put_byte(bytestream, size);
132 if (end - *bytestream < size)
134 bytestream_put_buffer(bytestream, ptr, size);
138 bytestream_put_byte(bytestream, 0x00); /* end of image block */
139 bytestream_put_byte(bytestream, 0x3b);
143 static av_cold int gif_encode_init(AVCodecContext *avctx)
145 GIFContext *s = avctx->priv_data;
147 avctx->coded_frame = &s->picture;
148 s->lzw = av_mallocz(ff_lzw_encode_state_size);
150 return AVERROR(ENOMEM);
151 s->buf = av_malloc(avctx->width*avctx->height*2);
153 return AVERROR(ENOMEM);
157 /* better than nothing gif encoder */
158 static int gif_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data)
160 GIFContext *s = avctx->priv_data;
161 AVFrame *pict = data;
162 AVFrame *const p = (AVFrame *)&s->picture;
163 uint8_t *outbuf_ptr = outbuf;
164 uint8_t *end = outbuf + buf_size;
167 p->pict_type = AV_PICTURE_TYPE_I;
169 gif_image_write_header(avctx, &outbuf_ptr, (uint32_t *)pict->data[1]);
170 gif_image_write_image(avctx, &outbuf_ptr, end, pict->data[0], pict->linesize[0]);
171 return outbuf_ptr - outbuf;
174 static int gif_encode_close(AVCodecContext *avctx)
176 GIFContext *s = avctx->priv_data;
183 AVCodec ff_gif_encoder = {
185 .type = AVMEDIA_TYPE_VIDEO,
187 .priv_data_size = sizeof(GIFContext),
188 .init = gif_encode_init,
189 .encode = gif_encode_frame,
190 .close = gif_encode_close,
191 .pix_fmts= (const 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},
192 .long_name= NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),