]> git.sesse.net Git - ffmpeg/blob - libavcodec/gif.c
Merge commit 'a7f46586bf47174b5fa00a905b767b1781ec8b72'
[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     /* Crop image */
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;
66
67         /* skip common lines */
68         while (y_start < y_end) {
69             if (memcmp(ref + y_start*ref_linesize, buf + y_start*linesize, width))
70                 break;
71             y_start++;
72         }
73         while (y_end > y_start) {
74             if (memcmp(ref + y_end*ref_linesize, buf + y_end*linesize, width))
75                 break;
76             y_end--;
77         }
78         height = y_end + 1 - y_start;
79
80         /* skip common columns */
81         while (x_start < x_end) {
82             int same_column = 1;
83             for (y = y_start; y < y_end; y++) {
84                 if (ref[y*ref_linesize + x_start] != buf[y*linesize + x_start]) {
85                     same_column = 0;
86                     break;
87                 }
88             }
89             if (!same_column)
90                 break;
91             x_start++;
92         }
93         while (x_end > x_start) {
94             int same_column = 1;
95             for (y = y_start; y < y_end; y++) {
96                 if (ref[y*ref_linesize + x_end] != buf[y*linesize + x_end]) {
97                     same_column = 0;
98                     break;
99                 }
100             }
101             if (!same_column)
102                 break;
103             x_end--;
104         }
105         width = x_end + 1 - x_start;
106
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);
109     }
110
111     /* image block */
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);
117
118     if (!palette) {
119         bytestream_put_byte(bytestream, 0x00); /* flags */
120     } else {
121         unsigned i;
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);
126         }
127     }
128
129     bytestream_put_byte(bytestream, 0x08);
130
131     ff_lzw_encode_init(s->lzw, s->buf, width * height,
132                        12, FF_LZW_GIF, put_bits);
133
134     ptr = buf + y_start*linesize + x_start;
135     for (y = 0; y < height; y++) {
136         len += ff_lzw_encode(s->lzw, ptr, width);
137         ptr += linesize;
138     }
139     len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
140
141     ptr = s->buf;
142     while (len > 0) {
143         int size = FFMIN(255, len);
144         bytestream_put_byte(bytestream, size);
145         if (end - *bytestream < size)
146             return -1;
147         bytestream_put_buffer(bytestream, ptr, size);
148         ptr += size;
149         len -= size;
150     }
151     bytestream_put_byte(bytestream, 0x00); /* end of image block */
152     return 0;
153 }
154
155 static av_cold int gif_encode_init(AVCodecContext *avctx)
156 {
157     GIFContext *s = avctx->priv_data;
158
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);
162     }
163
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);
169     return 0;
170 }
171
172 /* better than nothing gif encoder */
173 static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
174                             const AVFrame *pict, int *got_packet)
175 {
176     GIFContext *s = avctx->priv_data;
177     AVFrame *const p = &s->picture;
178     uint8_t *outbuf_ptr, *end;
179     const uint32_t *palette = NULL;
180     int ret;
181
182     if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0)
183         return ret;
184     outbuf_ptr = pkt->data;
185     end        = pkt->data + pkt->size;
186
187     *p = *pict;
188     p->pict_type = AV_PICTURE_TYPE_I;
189     p->key_frame = 1;
190
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);
193         if (!pal_exdata)
194             return AVERROR(ENOMEM);
195         memcpy(pal_exdata, p->data[1], AVPALETTE_SIZE);
196         palette = (uint32_t*)p->data[1];
197     }
198
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();
202         if (!s->last_frame)
203             return AVERROR(ENOMEM);
204     }
205     av_frame_unref(s->last_frame);
206     ret = av_frame_ref(s->last_frame, (AVFrame*)pict);
207     if (ret < 0)
208         return ret;
209
210     pkt->size   = outbuf_ptr - pkt->data;
211     pkt->flags |= AV_PKT_FLAG_KEY;
212     *got_packet = 1;
213
214     return 0;
215 }
216
217 static int gif_encode_close(AVCodecContext *avctx)
218 {
219     GIFContext *s = avctx->priv_data;
220
221     av_freep(&s->lzw);
222     av_freep(&s->buf);
223     av_frame_free(&s->last_frame);
224     return 0;
225 }
226
227 AVCodec ff_gif_encoder = {
228     .name           = "gif",
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
238     },
239     .long_name      = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
240 };