]> git.sesse.net Git - ffmpeg/blob - libavcodec/gif.c
gif: remove outdated comments.
[ffmpeg] / libavcodec / gif.c
1 /*
2  * Copyright (c) 2000 Fabrice Bellard
3  * Copyright (c) 2002 Francois Revol
4  * Copyright (c) 2006 Baptiste Coudurier
5  *
6  * first version by Francois Revol <revol@free.fr>
7  *
8  * This file is part of FFmpeg.
9  *
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.
14  *
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.
19  *
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
23  */
24
25 /**
26  * @file
27  * GIF encoder
28  * @see http://www.w3.org/Graphics/GIF/spec-gif89a.txt
29  */
30
31 #include "avcodec.h"
32 #include "bytestream.h"
33 #include "internal.h"
34 #include "lzw.h"
35
36 /* The GIF format uses reversed order for bitstreams... */
37 /* at least they don't use PDP_ENDIAN :) */
38 #define BITSTREAM_WRITER_LE
39
40 #include "put_bits.h"
41
42 typedef struct {
43     AVFrame picture;
44     LZWState *lzw;
45     uint8_t *buf;
46     AVFrame *last_frame;
47 } GIFContext;
48
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)
53 {
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;
57     const uint8_t *ptr;
58
59     /* Mark one colour as transparent if the input palette contains at least
60      * one colour that is more than 50% transparent. */
61     if (palette) {
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;
67                 alpha_component = i;
68             }
69         }
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);
78         }
79     }
80
81     /* Crop image */
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;
88
89         /* skip common lines */
90         while (y_start < height) {
91             if (memcmp(ref + y_start*ref_linesize, buf + y_start*linesize, width))
92                 break;
93             y_start++;
94         }
95         while (y_end > y_start) {
96             if (memcmp(ref + y_end*ref_linesize, buf + y_end*linesize, width))
97                 break;
98             y_end--;
99         }
100         height = y_end + 1 - y_start;
101
102         /* skip common columns */
103         while (x_start < width) {
104             int same_column = 1;
105             for (y = y_start; y < y_end; y++) {
106                 if (ref[y*ref_linesize + x_start] != buf[y*linesize + x_start]) {
107                     same_column = 0;
108                     break;
109                 }
110             }
111             if (!same_column)
112                 break;
113             x_start++;
114         }
115         while (x_end > x_start) {
116             int same_column = 1;
117             for (y = y_start; y < y_end; y++) {
118                 if (ref[y*ref_linesize + x_end] != buf[y*linesize + x_end]) {
119                     same_column = 0;
120                     break;
121                 }
122             }
123             if (!same_column)
124                 break;
125             x_end--;
126         }
127         width = x_end + 1 - x_start;
128
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);
131     }
132
133     /* image block */
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);
139
140     if (!palette) {
141         bytestream_put_byte(bytestream, 0x00); /* flags */
142     } else {
143         unsigned i;
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);
148         }
149     }
150
151     bytestream_put_byte(bytestream, 0x08);
152
153     ff_lzw_encode_init(s->lzw, s->buf, width * height,
154                        12, FF_LZW_GIF, put_bits);
155
156     ptr = buf + y_start*linesize + x_start;
157     for (y = 0; y < height; y++) {
158         len += ff_lzw_encode(s->lzw, ptr, width);
159         ptr += linesize;
160     }
161     len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
162
163     ptr = s->buf;
164     while (len > 0) {
165         int size = FFMIN(255, len);
166         bytestream_put_byte(bytestream, size);
167         if (end - *bytestream < size)
168             return -1;
169         bytestream_put_buffer(bytestream, ptr, size);
170         ptr += size;
171         len -= size;
172     }
173     bytestream_put_byte(bytestream, 0x00); /* end of image block */
174     return 0;
175 }
176
177 static av_cold int gif_encode_init(AVCodecContext *avctx)
178 {
179     GIFContext *s = avctx->priv_data;
180
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);
184     }
185
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);
191     return 0;
192 }
193
194 /* better than nothing gif encoder */
195 static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
196                             const AVFrame *pict, int *got_packet)
197 {
198     GIFContext *s = avctx->priv_data;
199     AVFrame *const p = &s->picture;
200     uint8_t *outbuf_ptr, *end;
201     const uint32_t *palette = NULL;
202     int ret;
203
204     if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0)
205         return ret;
206     outbuf_ptr = pkt->data;
207     end        = pkt->data + pkt->size;
208
209     *p = *pict;
210     p->pict_type = AV_PICTURE_TYPE_I;
211     p->key_frame = 1;
212
213     if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
214         palette = (uint32_t*)p->data[1];
215
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();
219         if (!s->last_frame)
220             return AVERROR(ENOMEM);
221     }
222     av_frame_unref(s->last_frame);
223     ret = av_frame_ref(s->last_frame, (AVFrame*)pict);
224     if (ret < 0)
225         return ret;
226
227     pkt->size   = outbuf_ptr - pkt->data;
228     pkt->flags |= AV_PKT_FLAG_KEY;
229     *got_packet = 1;
230
231     return 0;
232 }
233
234 static int gif_encode_close(AVCodecContext *avctx)
235 {
236     GIFContext *s = avctx->priv_data;
237
238     av_freep(&s->lzw);
239     av_freep(&s->buf);
240     av_frame_free(&s->last_frame);
241     return 0;
242 }
243
244 AVCodec ff_gif_encoder = {
245     .name           = "gif",
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
255     },
256     .long_name      = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
257 };