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
32 #include "bytestream.h"
36 /* The GIF format uses reversed order for bitstreams... */
37 /* at least they don't use PDP_ENDIAN :) */
38 #define BITSTREAM_WRITER_LE
49 static int gif_image_write_image(AVCodecContext *avctx,
50 uint8_t **bytestream, uint8_t *end,
51 const uint32_t *palette,
52 const uint8_t *buf, int linesize)
54 GIFContext *s = avctx->priv_data;
55 int len = 0, height = avctx->height, width = avctx->width, y;
56 int x_start = 0, y_start = 0;
59 /* Mark one colour as transparent if the input palette contains at least
60 * one colour that is more than 50% transparent. */
62 unsigned i, smallest_alpha = 0xFF, alpha_component = 0;
63 for (i = 0; i < AVPALETTE_COUNT; i++) {
64 const uint32_t v = palette[i];
65 if (v >> 24 < smallest_alpha) {
66 smallest_alpha = v >> 24;
70 if (smallest_alpha < 128) {
71 bytestream_put_byte(bytestream, 0x21); /* Extension Introducer */
72 bytestream_put_byte(bytestream, 0xf9); /* Graphic Control Label */
73 bytestream_put_byte(bytestream, 0x04); /* block length */
74 bytestream_put_byte(bytestream, 0x01); /* Transparent Color Flag */
75 bytestream_put_le16(bytestream, 0x00); /* no delay */
76 bytestream_put_byte(bytestream, alpha_component);
77 bytestream_put_byte(bytestream, 0x00);
82 // TODO support with palette change
83 if (s->last_frame && !palette) {
84 const uint8_t *ref = s->last_frame->data[0];
85 const int ref_linesize = s->last_frame->linesize[0];
86 int x_end = avctx->width - 1,
87 y_end = avctx->height - 1;
89 /* skip common lines */
90 while (y_start < height) {
91 if (memcmp(ref + y_start*ref_linesize, buf + y_start*linesize, width))
95 while (y_end > y_start) {
96 if (memcmp(ref + y_end*ref_linesize, buf + y_end*linesize, width))
100 height = y_end + 1 - y_start;
102 /* skip common columns */
103 while (x_start < width) {
105 for (y = y_start; y < y_end; y++) {
106 if (ref[y*ref_linesize + x_start] != buf[y*linesize + x_start]) {
115 while (x_end > x_start) {
117 for (y = y_start; y < y_end; y++) {
118 if (ref[y*ref_linesize + x_end] != buf[y*linesize + x_end]) {
127 width = x_end + 1 - x_start;
129 av_log(avctx, AV_LOG_DEBUG,"%dx%d image at pos (%d;%d) [area:%dx%d]\n",
130 width, height, x_start, y_start, avctx->width, avctx->height);
134 bytestream_put_byte(bytestream, 0x2c);
135 bytestream_put_le16(bytestream, x_start);
136 bytestream_put_le16(bytestream, y_start);
137 bytestream_put_le16(bytestream, width);
138 bytestream_put_le16(bytestream, height);
141 bytestream_put_byte(bytestream, 0x00); /* flags */
144 bytestream_put_byte(bytestream, 1<<7 | 0x7); /* flags */
145 for (i = 0; i < AVPALETTE_COUNT; i++) {
146 const uint32_t v = palette[i];
147 bytestream_put_be24(bytestream, v);
151 bytestream_put_byte(bytestream, 0x08);
153 ff_lzw_encode_init(s->lzw, s->buf, width * height,
154 12, FF_LZW_GIF, put_bits);
156 ptr = buf + y_start*linesize + x_start;
157 for (y = 0; y < height; y++) {
158 len += ff_lzw_encode(s->lzw, ptr, width);
161 len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
165 int size = FFMIN(255, len);
166 bytestream_put_byte(bytestream, size);
167 if (end - *bytestream < size)
169 bytestream_put_buffer(bytestream, ptr, size);
173 bytestream_put_byte(bytestream, 0x00); /* end of image block */
177 static av_cold int gif_encode_init(AVCodecContext *avctx)
179 GIFContext *s = avctx->priv_data;
181 if (avctx->width > 65535 || avctx->height > 65535) {
182 av_log(avctx, AV_LOG_ERROR, "GIF does not support resolutions above 65535x65535\n");
183 return AVERROR(EINVAL);
186 avctx->coded_frame = &s->picture;
187 s->lzw = av_mallocz(ff_lzw_encode_state_size);
188 s->buf = av_malloc(avctx->width*avctx->height*2);
189 if (!s->buf || !s->lzw)
190 return AVERROR(ENOMEM);
194 /* better than nothing gif encoder */
195 static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
196 const AVFrame *pict, int *got_packet)
198 GIFContext *s = avctx->priv_data;
199 AVFrame *const p = &s->picture;
200 uint8_t *outbuf_ptr, *end;
201 const uint32_t *palette = NULL;
204 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0)
206 outbuf_ptr = pkt->data;
207 end = pkt->data + pkt->size;
210 p->pict_type = AV_PICTURE_TYPE_I;
213 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
214 palette = (uint32_t*)p->data[1];
216 gif_image_write_image(avctx, &outbuf_ptr, end, palette, pict->data[0], pict->linesize[0]);
217 if (!s->last_frame) {
218 s->last_frame = av_frame_alloc();
220 return AVERROR(ENOMEM);
222 av_frame_unref(s->last_frame);
223 ret = av_frame_ref(s->last_frame, (AVFrame*)pict);
227 pkt->size = outbuf_ptr - pkt->data;
228 pkt->flags |= AV_PKT_FLAG_KEY;
234 static int gif_encode_close(AVCodecContext *avctx)
236 GIFContext *s = avctx->priv_data;
240 av_frame_free(&s->last_frame);
244 AVCodec ff_gif_encoder = {
246 .type = AVMEDIA_TYPE_VIDEO,
247 .id = AV_CODEC_ID_GIF,
248 .priv_data_size = sizeof(GIFContext),
249 .init = gif_encode_init,
250 .encode2 = gif_encode_frame,
251 .close = gif_encode_close,
252 .pix_fmts = (const enum AVPixelFormat[]){
253 AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
254 AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE
256 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),