3 * Copyright (c) 2000 Fabrice Bellard
4 * Copyright (c) 2002 Francois Revol
5 * Copyright (c) 2006 Baptiste Coudurier
7 * first version by Francois Revol <revol@free.fr>
9 * This file is part of FFmpeg.
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * Features and limitations:
28 * - uses only a global standard palette
29 * - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
31 * Reference documents:
32 * http://www.goice.co.jp/member/mo/formats/gif.html
33 * http://astronomy.swin.edu.au/pbourke/dataformats/gif/
34 * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
38 #include "bytestream.h"
42 /* The GIF format uses reversed order for bitstreams... */
43 /* at least they don't use PDP_ENDIAN :) */
44 #define BITSTREAM_WRITER_LE
55 static int gif_image_write_header(AVCodecContext *avctx,
56 uint8_t **bytestream, uint32_t *palette)
59 unsigned int v, smallest_alpha = 0xFF, alpha_component = 0;
61 bytestream_put_buffer(bytestream, "GIF", 3);
62 bytestream_put_buffer(bytestream, "89a", 3);
63 bytestream_put_le16(bytestream, avctx->width);
64 bytestream_put_le16(bytestream, avctx->height);
66 bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
67 bytestream_put_byte(bytestream, 0x1f); /* background color index */
68 bytestream_put_byte(bytestream, 0); /* aspect ratio */
70 /* the global palette */
73 bytestream_put_be24(bytestream, v);
74 if (v >> 24 < smallest_alpha) {
75 smallest_alpha = v >> 24;
80 if (smallest_alpha < 128) {
81 bytestream_put_byte(bytestream, 0x21); /* Extension Introducer */
82 bytestream_put_byte(bytestream, 0xf9); /* Graphic Control Label */
83 bytestream_put_byte(bytestream, 0x04); /* block length */
84 bytestream_put_byte(bytestream, 0x01); /* Transparent Color Flag */
85 bytestream_put_le16(bytestream, 0x00); /* no delay */
86 bytestream_put_byte(bytestream, alpha_component);
87 bytestream_put_byte(bytestream, 0x00);
93 static int gif_image_write_image(AVCodecContext *avctx,
94 uint8_t **bytestream, uint8_t *end,
95 const uint8_t *buf, int linesize)
97 GIFContext *s = avctx->priv_data;
102 bytestream_put_byte(bytestream, 0x2c);
103 bytestream_put_le16(bytestream, 0);
104 bytestream_put_le16(bytestream, 0);
105 bytestream_put_le16(bytestream, avctx->width);
106 bytestream_put_le16(bytestream, avctx->height);
107 bytestream_put_byte(bytestream, 0x00); /* flags */
110 bytestream_put_byte(bytestream, 0x08);
112 ff_lzw_encode_init(s->lzw, s->buf, avctx->width*avctx->height,
113 12, FF_LZW_GIF, put_bits);
116 for (height = avctx->height; height--;) {
117 len += ff_lzw_encode(s->lzw, ptr, avctx->width);
120 len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
124 int size = FFMIN(255, len);
125 bytestream_put_byte(bytestream, size);
126 if (end - *bytestream < size)
128 bytestream_put_buffer(bytestream, ptr, size);
132 bytestream_put_byte(bytestream, 0x00); /* end of image block */
133 bytestream_put_byte(bytestream, 0x3b);
137 static av_cold int gif_encode_init(AVCodecContext *avctx)
139 GIFContext *s = avctx->priv_data;
141 if (avctx->width > 65535 || avctx->height > 65535) {
142 av_log(avctx, AV_LOG_ERROR, "GIF does not support resolutions above 65535x65535\n");
146 avctx->coded_frame = &s->picture;
147 s->lzw = av_mallocz(ff_lzw_encode_state_size);
149 return AVERROR(ENOMEM);
150 s->buf = av_malloc(avctx->width*avctx->height*2);
152 return AVERROR(ENOMEM);
156 /* better than nothing gif encoder */
157 static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
158 const AVFrame *pict, int *got_packet)
160 GIFContext *s = avctx->priv_data;
161 AVFrame *const p = &s->picture;
162 uint8_t *outbuf_ptr, *end;
165 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0)
167 outbuf_ptr = pkt->data;
168 end = pkt->data + pkt->size;
171 p->pict_type = AV_PICTURE_TYPE_I;
173 gif_image_write_header(avctx, &outbuf_ptr, (uint32_t *)pict->data[1]);
174 gif_image_write_image(avctx, &outbuf_ptr, end, pict->data[0], pict->linesize[0]);
176 pkt->size = outbuf_ptr - pkt->data;
177 pkt->flags |= AV_PKT_FLAG_KEY;
183 static int gif_encode_close(AVCodecContext *avctx)
185 GIFContext *s = avctx->priv_data;
192 AVCodec ff_gif_encoder = {
194 .type = AVMEDIA_TYPE_VIDEO,
195 .id = AV_CODEC_ID_GIF,
196 .priv_data_size = sizeof(GIFContext),
197 .init = gif_encode_init,
198 .encode2 = gif_encode_frame,
199 .close = gif_encode_close,
200 .pix_fmts = (const enum AVPixelFormat[]){
201 AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
202 AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE
204 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),