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;
60 // TODO support with palette change
61 if (s->last_frame && !palette) {
62 const uint8_t *ref = s->last_frame->data[0];
63 const int ref_linesize = s->last_frame->linesize[0];
64 int x_end = avctx->width - 1,
65 y_end = avctx->height - 1;
67 /* skip common lines */
68 while (y_start < y_end) {
69 if (memcmp(ref + y_start*ref_linesize, buf + y_start*linesize, width))
73 while (y_end > y_start) {
74 if (memcmp(ref + y_end*ref_linesize, buf + y_end*linesize, width))
78 height = y_end + 1 - y_start;
80 /* skip common columns */
81 while (x_start < x_end) {
83 for (y = y_start; y < y_end; y++) {
84 if (ref[y*ref_linesize + x_start] != buf[y*linesize + x_start]) {
93 while (x_end > x_start) {
95 for (y = y_start; y < y_end; y++) {
96 if (ref[y*ref_linesize + x_end] != buf[y*linesize + x_end]) {
105 width = x_end + 1 - x_start;
107 av_log(avctx, AV_LOG_DEBUG,"%dx%d image at pos (%d;%d) [area:%dx%d]\n",
108 width, height, x_start, y_start, avctx->width, avctx->height);
112 bytestream_put_byte(bytestream, 0x2c);
113 bytestream_put_le16(bytestream, x_start);
114 bytestream_put_le16(bytestream, y_start);
115 bytestream_put_le16(bytestream, width);
116 bytestream_put_le16(bytestream, height);
119 bytestream_put_byte(bytestream, 0x00); /* flags */
122 bytestream_put_byte(bytestream, 1<<7 | 0x7); /* flags */
123 for (i = 0; i < AVPALETTE_COUNT; i++) {
124 const uint32_t v = palette[i];
125 bytestream_put_be24(bytestream, v);
129 bytestream_put_byte(bytestream, 0x08);
131 ff_lzw_encode_init(s->lzw, s->buf, width * height,
132 12, FF_LZW_GIF, put_bits);
134 ptr = buf + y_start*linesize + x_start;
135 for (y = 0; y < height; y++) {
136 len += ff_lzw_encode(s->lzw, ptr, width);
139 len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
143 int size = FFMIN(255, len);
144 bytestream_put_byte(bytestream, size);
145 if (end - *bytestream < size)
147 bytestream_put_buffer(bytestream, ptr, size);
151 bytestream_put_byte(bytestream, 0x00); /* end of image block */
155 static av_cold int gif_encode_init(AVCodecContext *avctx)
157 GIFContext *s = avctx->priv_data;
159 if (avctx->width > 65535 || avctx->height > 65535) {
160 av_log(avctx, AV_LOG_ERROR, "GIF does not support resolutions above 65535x65535\n");
161 return AVERROR(EINVAL);
164 avctx->coded_frame = &s->picture;
165 s->lzw = av_mallocz(ff_lzw_encode_state_size);
166 s->buf = av_malloc(avctx->width*avctx->height*2);
167 if (!s->buf || !s->lzw)
168 return AVERROR(ENOMEM);
172 /* better than nothing gif encoder */
173 static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
174 const AVFrame *pict, int *got_packet)
176 GIFContext *s = avctx->priv_data;
177 AVFrame *const p = &s->picture;
178 uint8_t *outbuf_ptr, *end;
179 const uint32_t *palette = NULL;
182 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0)
184 outbuf_ptr = pkt->data;
185 end = pkt->data + pkt->size;
188 p->pict_type = AV_PICTURE_TYPE_I;
191 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
192 uint8_t *pal_exdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
194 return AVERROR(ENOMEM);
195 memcpy(pal_exdata, p->data[1], AVPALETTE_SIZE);
196 palette = (uint32_t*)p->data[1];
199 gif_image_write_image(avctx, &outbuf_ptr, end, palette, pict->data[0], pict->linesize[0]);
200 if (!s->last_frame) {
201 s->last_frame = av_frame_alloc();
203 return AVERROR(ENOMEM);
205 av_frame_unref(s->last_frame);
206 ret = av_frame_ref(s->last_frame, (AVFrame*)pict);
210 pkt->size = outbuf_ptr - pkt->data;
211 pkt->flags |= AV_PKT_FLAG_KEY;
217 static int gif_encode_close(AVCodecContext *avctx)
219 GIFContext *s = avctx->priv_data;
223 av_frame_free(&s->last_frame);
227 AVCodec ff_gif_encoder = {
229 .type = AVMEDIA_TYPE_VIDEO,
230 .id = AV_CODEC_ID_GIF,
231 .priv_data_size = sizeof(GIFContext),
232 .init = gif_encode_init,
233 .encode2 = gif_encode_frame,
234 .close = gif_encode_close,
235 .pix_fmts = (const enum AVPixelFormat[]){
236 AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
237 AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE
239 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),