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 #include "libavutil/opt.h"
32 #include "libavutil/imgutils.h"
34 #include "bytestream.h"
39 #define BITSTREAM_WRITER_LE
49 uint32_t palette[AVPALETTE_COUNT]; ///< local reference palette for !pal8
50 uint8_t *tmpl; ///< temporary line buffer
58 static int pick_palette_entry(const uint8_t *buf, int linesize, int w, int h)
60 int histogram[AVPALETTE_COUNT] = {0};
63 for (y = 0; y < h; y++) {
64 for (x = 0; x < w; x++)
68 for (i = 0; i < FF_ARRAY_ELEMS(histogram); i++)
74 static int gif_image_write_image(AVCodecContext *avctx,
75 uint8_t **bytestream, uint8_t *end,
76 const uint32_t *palette,
77 const uint8_t *buf, const int linesize,
80 GIFContext *s = avctx->priv_data;
81 int len = 0, height = avctx->height, width = avctx->width, x, y;
82 int x_start = 0, y_start = 0, trans = -1;
86 // TODO support with palette change
87 if ((s->flags & GF_OFFSETTING) && s->last_frame && !palette) {
88 const uint8_t *ref = s->last_frame->data[0];
89 const int ref_linesize = s->last_frame->linesize[0];
90 int x_end = avctx->width - 1,
91 y_end = avctx->height - 1;
93 /* skip common lines */
94 while (y_start < y_end) {
95 if (memcmp(ref + y_start*ref_linesize, buf + y_start*linesize, width))
99 while (y_end > y_start) {
100 if (memcmp(ref + y_end*ref_linesize, buf + y_end*linesize, width))
104 height = y_end + 1 - y_start;
106 /* skip common columns */
107 while (x_start < x_end) {
109 for (y = y_start; y < y_end; y++) {
110 if (ref[y*ref_linesize + x_start] != buf[y*linesize + x_start]) {
119 while (x_end > x_start) {
121 for (y = y_start; y < y_end; y++) {
122 if (ref[y*ref_linesize + x_end] != buf[y*linesize + x_end]) {
131 width = x_end + 1 - x_start;
133 av_log(avctx, AV_LOG_DEBUG,"%dx%d image at pos (%d;%d) [area:%dx%d]\n",
134 width, height, x_start, y_start, avctx->width, avctx->height);
138 bytestream_put_byte(bytestream, GIF_IMAGE_SEPARATOR);
139 bytestream_put_le16(bytestream, x_start);
140 bytestream_put_le16(bytestream, y_start);
141 bytestream_put_le16(bytestream, width);
142 bytestream_put_le16(bytestream, height);
145 bytestream_put_byte(bytestream, 0x00); /* flags */
148 bytestream_put_byte(bytestream, 1<<7 | 0x7); /* flags */
149 for (i = 0; i < AVPALETTE_COUNT; i++) {
150 const uint32_t v = palette[i];
151 bytestream_put_be24(bytestream, v);
155 /* TODO: support with palette change (pal8) */
156 if ((s->flags & GF_TRANSDIFF) && s->last_frame && !palette) {
157 trans = pick_palette_entry(buf + y_start*linesize + x_start,
158 linesize, width, height);
159 if (trans < 0) { // TODO, patch welcome
160 av_log(avctx, AV_LOG_DEBUG, "No available color, can not use transparency\n");
162 uint8_t *pal_exdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
164 return AVERROR(ENOMEM);
165 memcpy(pal_exdata, s->palette, AVPALETTE_SIZE);
166 pal_exdata[trans*4 + 3*!HAVE_BIGENDIAN] = 0x00;
170 bytestream_put_byte(bytestream, 0x08);
172 ff_lzw_encode_init(s->lzw, s->buf, width * height,
173 12, FF_LZW_GIF, put_bits);
175 ptr = buf + y_start*linesize + x_start;
177 const int ref_linesize = s->last_frame->linesize[0];
178 const uint8_t *ref = s->last_frame->data[0] + y_start*ref_linesize + x_start;
180 for (y = 0; y < height; y++) {
181 memcpy(s->tmpl, ptr, width);
182 for (x = 0; x < width; x++)
183 if (ref[x] == ptr[x])
185 len += ff_lzw_encode(s->lzw, s->tmpl, width);
190 for (y = 0; y < height; y++) {
191 len += ff_lzw_encode(s->lzw, ptr, width);
195 len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
199 int size = FFMIN(255, len);
200 bytestream_put_byte(bytestream, size);
201 if (end - *bytestream < size)
203 bytestream_put_buffer(bytestream, ptr, size);
207 bytestream_put_byte(bytestream, 0x00); /* end of image block */
211 static av_cold int gif_encode_init(AVCodecContext *avctx)
213 GIFContext *s = avctx->priv_data;
215 if (avctx->width > 65535 || avctx->height > 65535) {
216 av_log(avctx, AV_LOG_ERROR, "GIF does not support resolutions above 65535x65535\n");
217 return AVERROR(EINVAL);
220 avctx->coded_frame = &s->picture;
221 s->lzw = av_mallocz(ff_lzw_encode_state_size);
222 s->buf = av_malloc(avctx->width*avctx->height*2);
223 s->tmpl = av_malloc(avctx->width);
224 if (!s->tmpl || !s->buf || !s->lzw)
225 return AVERROR(ENOMEM);
227 if (avpriv_set_systematic_pal2(s->palette, avctx->pix_fmt) < 0)
228 av_assert0(avctx->pix_fmt == AV_PIX_FMT_PAL8);
233 static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
234 const AVFrame *pict, int *got_packet)
236 GIFContext *s = avctx->priv_data;
237 AVFrame *const p = &s->picture;
238 uint8_t *outbuf_ptr, *end;
239 const uint32_t *palette = NULL;
242 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0)
244 outbuf_ptr = pkt->data;
245 end = pkt->data + pkt->size;
248 p->pict_type = AV_PICTURE_TYPE_I;
251 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
252 uint8_t *pal_exdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
254 return AVERROR(ENOMEM);
255 memcpy(pal_exdata, p->data[1], AVPALETTE_SIZE);
256 palette = (uint32_t*)p->data[1];
259 gif_image_write_image(avctx, &outbuf_ptr, end, palette,
260 pict->data[0], pict->linesize[0], pkt);
261 if (!s->last_frame) {
262 s->last_frame = av_frame_alloc();
264 return AVERROR(ENOMEM);
266 av_frame_unref(s->last_frame);
267 ret = av_frame_ref(s->last_frame, (AVFrame*)pict);
271 pkt->size = outbuf_ptr - pkt->data;
272 pkt->flags |= AV_PKT_FLAG_KEY;
278 static int gif_encode_close(AVCodecContext *avctx)
280 GIFContext *s = avctx->priv_data;
284 av_frame_free(&s->last_frame);
289 #define OFFSET(x) offsetof(GIFContext, x)
290 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
291 static const AVOption gif_options[] = {
292 { "gifflags", "set GIF flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = GF_OFFSETTING|GF_TRANSDIFF}, 0, INT_MAX, FLAGS, "flags" },
293 { "offsetting", "enable picture offsetting", 0, AV_OPT_TYPE_CONST, {.i64=GF_OFFSETTING}, INT_MIN, INT_MAX, FLAGS, "flags" },
294 { "transdiff", "enable transparency detection between frames", 0, AV_OPT_TYPE_CONST, {.i64=GF_TRANSDIFF}, INT_MIN, INT_MAX, FLAGS, "flags" },
298 static const AVClass gif_class = {
299 .class_name = "GIF encoder",
300 .item_name = av_default_item_name,
301 .option = gif_options,
302 .version = LIBAVUTIL_VERSION_INT,
305 AVCodec ff_gif_encoder = {
307 .type = AVMEDIA_TYPE_VIDEO,
308 .id = AV_CODEC_ID_GIF,
309 .priv_data_size = sizeof(GIFContext),
310 .init = gif_encode_init,
311 .encode2 = gif_encode_frame,
312 .close = gif_encode_close,
313 .pix_fmts = (const enum AVPixelFormat[]){
314 AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
315 AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE
317 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
318 .priv_class = &gif_class,