3 * Copyright (c) 2000 Fabrice Bellard
5 * first version by Francois Revol <revol@free.fr>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/avassert.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/log.h"
29 #include "libavutil/opt.h"
31 /* XXX: random value that shouldn't be taken into effect if there is no
32 * transparent color in the palette (the transparency bit will be set to 0) */
33 #define DEFAULT_TRANSPARENCY_INDEX 0x1f
35 static int get_palette_transparency_index(const uint32_t *palette)
37 int transparent_color_index = -1;
38 unsigned i, smallest_alpha = 0xff;
43 for (i = 0; i < AVPALETTE_COUNT; i++) {
44 const uint32_t v = palette[i];
45 if (v >> 24 < smallest_alpha) {
46 smallest_alpha = v >> 24;
47 transparent_color_index = i;
50 return smallest_alpha < 128 ? transparent_color_index : -1;
53 static int gif_image_write_header(AVIOContext *pb, AVStream *st,
54 int loop_count, uint32_t *palette)
58 const AVRational sar = st->sample_aspect_ratio;
60 if (sar.num > 0 && sar.den > 0) {
61 aspect = sar.num * 64LL / sar.den - 15;
62 if (aspect < 0 || aspect > 255)
66 avio_write(pb, "GIF", 3);
67 avio_write(pb, "89a", 3);
68 avio_wl16(pb, st->codecpar->width);
69 avio_wl16(pb, st->codecpar->height);
72 const int bcid = get_palette_transparency_index(palette);
74 avio_w8(pb, 0xf7); /* flags: global clut, 256 entries */
75 avio_w8(pb, bcid < 0 ? DEFAULT_TRANSPARENCY_INDEX : bcid); /* background color index */
77 for (i = 0; i < 256; i++) {
78 const uint32_t v = palette[i] & 0xffffff;
82 avio_w8(pb, 0); /* flags */
83 avio_w8(pb, 0); /* background color index */
88 if (loop_count >= 0 ) {
89 /* "NETSCAPE EXTENSION" for looped animation GIF */
90 avio_w8(pb, 0x21); /* GIF Extension code */
91 avio_w8(pb, 0xff); /* Application Extension Label */
92 avio_w8(pb, 0x0b); /* Length of Application Block */
93 avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1);
94 avio_w8(pb, 0x03); /* Length of Data Sub-Block */
96 avio_wl16(pb, (uint16_t)loop_count);
97 avio_w8(pb, 0x00); /* Data Sub-block Terminator */
104 typedef struct GIFContext {
112 static int gif_write_header(AVFormatContext *s)
114 GIFContext *gif = s->priv_data;
115 AVCodecParameters *video_par;
116 uint32_t palette[AVPALETTE_COUNT];
118 if (s->nb_streams != 1 ||
119 s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ||
120 s->streams[0]->codecpar->codec_id != AV_CODEC_ID_GIF) {
121 av_log(s, AV_LOG_ERROR,
122 "GIF muxer supports only a single video GIF stream.\n");
123 return AVERROR(EINVAL);
126 video_par = s->streams[0]->codecpar;
128 avpriv_set_pts_info(s->streams[0], 64, 1, 100);
129 if (avpriv_set_systematic_pal2(palette, video_par->format) < 0) {
130 av_assert0(video_par->format == AV_PIX_FMT_PAL8);
131 /* delay header writing: we wait for the first palette to put it
134 gif_image_write_header(s->pb, s->streams[0], gif->loop, palette);
140 static int flush_packet(AVFormatContext *s, AVPacket *new)
142 GIFContext *gif = s->priv_data;
144 AVIOContext *pb = s->pb;
145 const uint32_t *palette;
146 AVPacket *pkt = gif->prev_pkt;
151 /* Mark one colour as transparent if the input palette contains at least
152 * one colour that is more than 50% transparent. */
153 palette = (uint32_t*)av_packet_get_side_data(pkt, AV_PKT_DATA_PALETTE, &size);
154 if (palette && size != AVPALETTE_SIZE) {
155 av_log(s, AV_LOG_ERROR, "Invalid palette extradata\n");
156 return AVERROR_INVALIDDATA;
158 bcid = get_palette_transparency_index(palette);
160 if (new && new->pts != AV_NOPTS_VALUE)
161 gif->duration = av_clip_uint16(new->pts - gif->prev_pkt->pts);
162 else if (!new && gif->last_delay >= 0)
163 gif->duration = gif->last_delay;
165 /* graphic control extension block */
168 avio_w8(pb, 0x04); /* block size */
169 avio_w8(pb, 1<<2 | (bcid >= 0));
170 avio_wl16(pb, gif->duration);
171 avio_w8(pb, bcid < 0 ? DEFAULT_TRANSPARENCY_INDEX : bcid);
174 avio_write(pb, pkt->data, pkt->size);
176 av_packet_unref(gif->prev_pkt);
178 av_packet_ref(gif->prev_pkt, new);
183 static int gif_write_packet(AVFormatContext *s, AVPacket *pkt)
185 GIFContext *gif = s->priv_data;
186 AVStream *video_st = s->streams[0];
188 if (!gif->prev_pkt) {
189 gif->prev_pkt = av_packet_alloc();
191 return AVERROR(ENOMEM);
193 /* Write the first palette as global palette */
194 if (video_st->codecpar->format == AV_PIX_FMT_PAL8) {
196 void *palette = av_packet_get_side_data(pkt, AV_PKT_DATA_PALETTE, &size);
199 av_log(s, AV_LOG_ERROR, "PAL8 packet is missing palette in extradata\n");
200 return AVERROR_INVALIDDATA;
202 if (size != AVPALETTE_SIZE) {
203 av_log(s, AV_LOG_ERROR, "Invalid palette extradata\n");
204 return AVERROR_INVALIDDATA;
206 gif_image_write_header(s->pb, video_st, gif->loop, palette);
209 return av_packet_ref(gif->prev_pkt, pkt);
211 return flush_packet(s, pkt);
214 static int gif_write_trailer(AVFormatContext *s)
216 GIFContext *gif = s->priv_data;
217 AVIOContext *pb = s->pb;
219 flush_packet(s, NULL);
220 av_freep(&gif->prev_pkt);
226 #define OFFSET(x) offsetof(GIFContext, x)
227 #define ENC AV_OPT_FLAG_ENCODING_PARAM
228 static const AVOption options[] = {
229 { "loop", "Number of times to loop the output: -1 - no loop, 0 - infinite loop", OFFSET(loop),
230 AV_OPT_TYPE_INT, { .i64 = 0 }, -1, 65535, ENC },
231 { "final_delay", "Force delay (in centiseconds) after the last frame", OFFSET(last_delay),
232 AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 65535, ENC },
236 static const AVClass gif_muxer_class = {
237 .class_name = "GIF muxer",
238 .item_name = av_default_item_name,
239 .version = LIBAVUTIL_VERSION_INT,
243 AVOutputFormat ff_gif_muxer = {
245 .long_name = NULL_IF_CONFIG_SMALL("GIF Animation"),
246 .mime_type = "image/gif",
248 .priv_data_size = sizeof(GIFContext),
249 .audio_codec = AV_CODEC_ID_NONE,
250 .video_codec = AV_CODEC_ID_GIF,
251 .write_header = gif_write_header,
252 .write_packet = gif_write_packet,
253 .write_trailer = gif_write_trailer,
254 .priv_class = &gif_muxer_class,
255 .flags = AVFMT_VARIABLE_FPS,