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 static int gif_image_write_header(AVIOContext *pb, int width, int height,
32 int loop_count, uint32_t *palette)
36 avio_write(pb, "GIF", 3);
37 avio_write(pb, "89a", 3);
39 avio_wl16(pb, height);
42 avio_w8(pb, 0xf7); /* flags: global clut, 256 entries */
43 avio_w8(pb, 0x1f); /* background color index */
44 avio_w8(pb, 0); /* aspect ratio */
45 for (i = 0; i < 256; i++) {
46 const uint32_t v = palette[i] & 0xffffff;
50 avio_w8(pb, 0); /* flags */
51 avio_w8(pb, 0); /* background color index */
52 avio_w8(pb, 0); /* aspect ratio */
55 /* "NETSCAPE EXTENSION" for looped animation GIF */
56 avio_w8(pb, 0x21); /* GIF Extension code */
57 avio_w8(pb, 0xff); /* Application Extension Label */
58 avio_w8(pb, 0x0b); /* Length of Application Block */
59 avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1);
60 avio_w8(pb, 0x03); /* Length of Data Sub-Block */
62 avio_wl16(pb, (uint16_t)loop_count);
63 avio_w8(pb, 0x00); /* Data Sub-block Terminator */
76 static int gif_write_header(AVFormatContext *s)
78 GIFContext *gif = s->priv_data;
79 AVIOContext *pb = s->pb;
80 AVCodecContext *video_enc;
82 uint32_t palette[AVPALETTE_COUNT];
84 if (s->nb_streams != 1 ||
85 s->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
86 s->streams[0]->codec->codec_id != AV_CODEC_ID_GIF) {
87 av_log(s, AV_LOG_ERROR,
88 "GIF muxer supports only a single video GIF stream.\n");
89 return AVERROR(EINVAL);
92 video_enc = s->streams[0]->codec;
93 width = video_enc->width;
94 height = video_enc->height;
96 avpriv_set_pts_info(s->streams[0], 64, 1, 100);
97 if (avpriv_set_systematic_pal2(palette, video_enc->pix_fmt) < 0) {
98 av_assert0(video_enc->pix_fmt == AV_PIX_FMT_PAL8);
99 gif_image_write_header(pb, width, height, gif->loop, NULL);
101 gif_image_write_header(pb, width, height, gif->loop, palette);
108 static int flush_packet(AVFormatContext *s, AVPacket *new)
110 GIFContext *gif = s->priv_data;
112 AVIOContext *pb = s->pb;
113 uint8_t flags = 0x4, transparent_color_index = 0x1f;
114 const uint32_t *palette;
115 AVPacket *pkt = gif->prev_pkt;
120 /* Mark one colour as transparent if the input palette contains at least
121 * one colour that is more than 50% transparent. */
122 palette = (uint32_t*)av_packet_get_side_data(pkt, AV_PKT_DATA_PALETTE, &size);
123 if (palette && size != AVPALETTE_SIZE) {
124 av_log(s, AV_LOG_ERROR, "Invalid palette extradata\n");
125 return AVERROR_INVALIDDATA;
128 unsigned i, smallest_alpha = 0xff;
130 for (i = 0; i < AVPALETTE_COUNT; i++) {
131 const uint32_t v = palette[i];
132 if (v >> 24 < smallest_alpha) {
133 smallest_alpha = v >> 24;
134 transparent_color_index = i;
137 if (smallest_alpha < 128)
138 flags |= 0x1; /* Transparent Color Flag */
141 if (new && new->pts != AV_NOPTS_VALUE)
142 gif->duration = av_clip_uint16(new->pts - gif->prev_pkt->pts);
143 else if (!new && gif->last_delay >= 0)
144 gif->duration = gif->last_delay;
146 /* graphic control extension block */
149 avio_w8(pb, 0x04); /* block size */
151 avio_wl16(pb, gif->duration);
152 avio_w8(pb, transparent_color_index);
155 avio_write(pb, pkt->data, pkt->size);
157 av_free_packet(gif->prev_pkt);
159 av_copy_packet(gif->prev_pkt, new);
164 static int gif_write_packet(AVFormatContext *s, AVPacket *pkt)
166 GIFContext *gif = s->priv_data;
168 if (!gif->prev_pkt) {
169 gif->prev_pkt = av_malloc(sizeof(*gif->prev_pkt));
171 return AVERROR(ENOMEM);
172 return av_copy_packet(gif->prev_pkt, pkt);
174 return flush_packet(s, pkt);
177 static int gif_write_trailer(AVFormatContext *s)
179 GIFContext *gif = s->priv_data;
180 AVIOContext *pb = s->pb;
182 flush_packet(s, NULL);
183 av_freep(&gif->prev_pkt);
189 #define OFFSET(x) offsetof(GIFContext, x)
190 #define ENC AV_OPT_FLAG_ENCODING_PARAM
191 static const AVOption options[] = {
192 { "loop", "Number of times to loop the output.", OFFSET(loop),
193 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 65535, ENC },
194 { "final_delay", "Force delay (in ms) after the last frame", OFFSET(last_delay),
195 AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 65535, ENC },
199 static const AVClass gif_muxer_class = {
200 .class_name = "GIF muxer",
201 .item_name = av_default_item_name,
202 .version = LIBAVUTIL_VERSION_INT,
206 AVOutputFormat ff_gif_muxer = {
208 .long_name = NULL_IF_CONFIG_SMALL("GIF Animation"),
209 .mime_type = "image/gif",
211 .priv_data_size = sizeof(GIFContext),
212 .audio_codec = AV_CODEC_ID_NONE,
213 .video_codec = AV_CODEC_ID_GIF,
214 .write_header = gif_write_header,
215 .write_packet = gif_write_packet,
216 .write_trailer = gif_write_trailer,
217 .priv_class = &gif_muxer_class,
218 .flags = AVFMT_VARIABLE_FPS,