2 * Copyright (c) 2000 Fabrice Bellard
3 * Copyright (c) 2002 Francois Revol
4 * Copyright (c) 2006 Baptiste Coudurier
6 * first version by Francois Revol <revol@free.fr>
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 * @see http://www.w3.org/Graphics/GIF/spec-gif89a.txt
31 #define BITSTREAM_WRITER_LE
32 #include "libavutil/opt.h"
33 #include "libavutil/imgutils.h"
35 #include "bytestream.h"
42 typedef struct GIFContext {
48 uint32_t palette[AVPALETTE_COUNT]; ///< local reference palette for !pal8
50 int transparent_index;
52 uint8_t *tmpl; ///< temporary line buffer
60 static int pick_palette_entry(const uint8_t *buf, int linesize, int w, int h)
62 int histogram[AVPALETTE_COUNT] = {0};
65 for (y = 0; y < h; y++) {
66 for (x = 0; x < w; x++)
70 for (i = 0; i < FF_ARRAY_ELEMS(histogram); i++)
76 static int gif_image_write_image(AVCodecContext *avctx,
77 uint8_t **bytestream, uint8_t *end,
78 const uint32_t *palette,
79 const uint8_t *buf, const int linesize,
82 GIFContext *s = avctx->priv_data;
83 int len = 0, height = avctx->height, width = avctx->width, x, y;
84 int x_start = 0, y_start = 0, trans = s->transparent_index;
85 int honor_transparency = (s->flags & GF_TRANSDIFF) && s->last_frame;
89 if ((s->flags & GF_OFFSETTING) && s->last_frame && !palette) {
90 const uint8_t *ref = s->last_frame->data[0];
91 const int ref_linesize = s->last_frame->linesize[0];
92 int x_end = avctx->width - 1,
93 y_end = avctx->height - 1;
95 /* skip common lines */
96 while (y_start < y_end) {
97 if (memcmp(ref + y_start*ref_linesize, buf + y_start*linesize, width))
101 while (y_end > y_start) {
102 if (memcmp(ref + y_end*ref_linesize, buf + y_end*linesize, width))
106 height = y_end + 1 - y_start;
108 /* skip common columns */
109 while (x_start < x_end) {
111 for (y = y_start; y <= y_end; y++) {
112 if (ref[y*ref_linesize + x_start] != buf[y*linesize + x_start]) {
121 while (x_end > x_start) {
123 for (y = y_start; y <= y_end; y++) {
124 if (ref[y*ref_linesize + x_end] != buf[y*linesize + x_end]) {
133 width = x_end + 1 - x_start;
135 av_log(avctx, AV_LOG_DEBUG,"%dx%d image at pos (%d;%d) [area:%dx%d]\n",
136 width, height, x_start, y_start, avctx->width, avctx->height);
140 bytestream_put_byte(bytestream, GIF_IMAGE_SEPARATOR);
141 bytestream_put_le16(bytestream, x_start);
142 bytestream_put_le16(bytestream, y_start);
143 bytestream_put_le16(bytestream, width);
144 bytestream_put_le16(bytestream, height);
147 bytestream_put_byte(bytestream, 0x00); /* flags */
150 bytestream_put_byte(bytestream, 1<<7 | 0x7); /* flags */
151 for (i = 0; i < AVPALETTE_COUNT; i++) {
152 const uint32_t v = palette[i];
153 bytestream_put_be24(bytestream, v);
157 if (honor_transparency && trans < 0) {
158 trans = pick_palette_entry(buf + y_start*linesize + x_start,
159 linesize, width, height);
160 if (trans < 0) { // TODO, patch welcome
161 av_log(avctx, AV_LOG_DEBUG, "No available color, can not use transparency\n");
163 uint8_t *pal_exdata = s->pal_exdata;
165 pal_exdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
167 return AVERROR(ENOMEM);
168 memcpy(pal_exdata, s->palette, AVPALETTE_SIZE);
169 pal_exdata[trans*4 + 3*!HAVE_BIGENDIAN] = 0x00;
173 honor_transparency = 0;
175 bytestream_put_byte(bytestream, 0x08);
177 ff_lzw_encode_init(s->lzw, s->buf, 2 * width * height,
178 12, FF_LZW_GIF, put_bits);
180 ptr = buf + y_start*linesize + x_start;
181 if (honor_transparency) {
182 const int ref_linesize = s->last_frame->linesize[0];
183 const uint8_t *ref = s->last_frame->data[0] + y_start*ref_linesize + x_start;
185 for (y = 0; y < height; y++) {
186 memcpy(s->tmpl, ptr, width);
187 for (x = 0; x < width; x++)
188 if (ref[x] == ptr[x])
190 len += ff_lzw_encode(s->lzw, s->tmpl, width);
195 for (y = 0; y < height; y++) {
196 len += ff_lzw_encode(s->lzw, ptr, width);
200 len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
204 int size = FFMIN(255, len);
205 bytestream_put_byte(bytestream, size);
206 if (end - *bytestream < size)
208 bytestream_put_buffer(bytestream, ptr, size);
212 bytestream_put_byte(bytestream, 0x00); /* end of image block */
216 static av_cold int gif_encode_init(AVCodecContext *avctx)
218 GIFContext *s = avctx->priv_data;
220 if (avctx->width > 65535 || avctx->height > 65535) {
221 av_log(avctx, AV_LOG_ERROR, "GIF does not support resolutions above 65535x65535\n");
222 return AVERROR(EINVAL);
225 avctx->coded_frame = av_frame_alloc();
226 if (!avctx->coded_frame)
227 return AVERROR(ENOMEM);
229 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
230 avctx->coded_frame->key_frame = 1;
232 s->transparent_index = -1;
234 s->lzw = av_mallocz(ff_lzw_encode_state_size);
235 s->buf = av_malloc(avctx->width*avctx->height*2);
236 s->tmpl = av_malloc(avctx->width);
237 if (!s->tmpl || !s->buf || !s->lzw)
238 return AVERROR(ENOMEM);
240 if (avpriv_set_systematic_pal2(s->palette, avctx->pix_fmt) < 0)
241 av_assert0(avctx->pix_fmt == AV_PIX_FMT_PAL8);
246 /* FIXME: duplicated with lavc */
247 static int get_palette_transparency_index(const uint32_t *palette)
249 int transparent_color_index = -1;
250 unsigned i, smallest_alpha = 0xff;
255 for (i = 0; i < AVPALETTE_COUNT; i++) {
256 const uint32_t v = palette[i];
257 if (v >> 24 < smallest_alpha) {
258 smallest_alpha = v >> 24;
259 transparent_color_index = i;
262 return smallest_alpha < 128 ? transparent_color_index : -1;
265 static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
266 const AVFrame *pict, int *got_packet)
268 GIFContext *s = avctx->priv_data;
269 uint8_t *outbuf_ptr, *end;
270 const uint32_t *palette = NULL;
273 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0)
275 outbuf_ptr = pkt->data;
276 end = pkt->data + pkt->size;
278 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
279 uint8_t *pal_exdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
281 return AVERROR(ENOMEM);
282 memcpy(pal_exdata, pict->data[1], AVPALETTE_SIZE);
283 palette = (uint32_t*)pict->data[1];
285 s->pal_exdata = pal_exdata;
287 /* The first palette with PAL8 will be used as generic palette by the
288 * muxer so we don't need to write it locally in the packet. We store
289 * it as a reference here in case it changes later. */
290 if (!s->palette_loaded) {
291 memcpy(s->palette, palette, AVPALETTE_SIZE);
292 s->transparent_index = get_palette_transparency_index(palette);
293 s->palette_loaded = 1;
295 } else if (!memcmp(s->palette, palette, AVPALETTE_SIZE)) {
300 gif_image_write_image(avctx, &outbuf_ptr, end, palette,
301 pict->data[0], pict->linesize[0], pkt);
302 if (!s->last_frame) {
303 s->last_frame = av_frame_alloc();
305 return AVERROR(ENOMEM);
307 av_frame_unref(s->last_frame);
308 ret = av_frame_ref(s->last_frame, (AVFrame*)pict);
312 pkt->size = outbuf_ptr - pkt->data;
313 pkt->flags |= AV_PKT_FLAG_KEY;
319 static int gif_encode_close(AVCodecContext *avctx)
321 GIFContext *s = avctx->priv_data;
323 av_frame_free(&avctx->coded_frame);
327 av_frame_free(&s->last_frame);
332 #define OFFSET(x) offsetof(GIFContext, x)
333 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
334 static const AVOption gif_options[] = {
335 { "gifflags", "set GIF flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = GF_OFFSETTING|GF_TRANSDIFF}, 0, INT_MAX, FLAGS, "flags" },
336 { "offsetting", "enable picture offsetting", 0, AV_OPT_TYPE_CONST, {.i64=GF_OFFSETTING}, INT_MIN, INT_MAX, FLAGS, "flags" },
337 { "transdiff", "enable transparency detection between frames", 0, AV_OPT_TYPE_CONST, {.i64=GF_TRANSDIFF}, INT_MIN, INT_MAX, FLAGS, "flags" },
341 static const AVClass gif_class = {
342 .class_name = "GIF encoder",
343 .item_name = av_default_item_name,
344 .option = gif_options,
345 .version = LIBAVUTIL_VERSION_INT,
348 AVCodec ff_gif_encoder = {
350 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
351 .type = AVMEDIA_TYPE_VIDEO,
352 .id = AV_CODEC_ID_GIF,
353 .priv_data_size = sizeof(GIFContext),
354 .init = gif_encode_init,
355 .encode2 = gif_encode_frame,
356 .close = gif_encode_close,
357 .pix_fmts = (const enum AVPixelFormat[]){
358 AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
359 AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE
361 .priv_class = &gif_class,