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 */
56 if (loop_count >= 0 ) {
57 /* "NETSCAPE EXTENSION" for looped animation GIF */
58 avio_w8(pb, 0x21); /* GIF Extension code */
59 avio_w8(pb, 0xff); /* Application Extension Label */
60 avio_w8(pb, 0x0b); /* Length of Application Block */
61 avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1);
62 avio_w8(pb, 0x03); /* Length of Data Sub-Block */
64 avio_wl16(pb, (uint16_t)loop_count);
65 avio_w8(pb, 0x00); /* Data Sub-block Terminator */
79 static int gif_write_header(AVFormatContext *s)
81 GIFContext *gif = s->priv_data;
82 AVIOContext *pb = s->pb;
83 AVCodecContext *video_enc;
85 uint32_t palette[AVPALETTE_COUNT];
87 if (s->nb_streams != 1 ||
88 s->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
89 s->streams[0]->codec->codec_id != AV_CODEC_ID_GIF) {
90 av_log(s, AV_LOG_ERROR,
91 "GIF muxer supports only a single video GIF stream.\n");
92 return AVERROR(EINVAL);
95 video_enc = s->streams[0]->codec;
96 width = video_enc->width;
97 height = video_enc->height;
99 avpriv_set_pts_info(s->streams[0], 64, 1, 100);
100 if (avpriv_set_systematic_pal2(palette, video_enc->pix_fmt) < 0) {
101 av_assert0(video_enc->pix_fmt == AV_PIX_FMT_PAL8);
102 gif_image_write_header(pb, width, height, gif->loop, NULL);
104 gif_image_write_header(pb, width, height, gif->loop, palette);
111 static int flush_packet(AVFormatContext *s, AVPacket *new)
113 GIFContext *gif = s->priv_data;
115 AVIOContext *pb = s->pb;
116 uint8_t flags = 0x4, transparent_color_index = 0x1f;
117 const uint32_t *palette;
118 AVPacket *pkt = gif->prev_pkt;
123 /* Mark one colour as transparent if the input palette contains at least
124 * one colour that is more than 50% transparent. */
125 palette = (uint32_t*)av_packet_get_side_data(pkt, AV_PKT_DATA_PALETTE, &size);
126 if (palette && size != AVPALETTE_SIZE) {
127 av_log(s, AV_LOG_ERROR, "Invalid palette extradata\n");
128 return AVERROR_INVALIDDATA;
131 unsigned i, smallest_alpha = 0xff;
133 for (i = 0; i < AVPALETTE_COUNT; i++) {
134 const uint32_t v = palette[i];
135 if (v >> 24 < smallest_alpha) {
136 smallest_alpha = v >> 24;
137 transparent_color_index = i;
140 if (smallest_alpha < 128)
141 flags |= 0x1; /* Transparent Color Flag */
144 if (new && new->pts != AV_NOPTS_VALUE)
145 gif->duration = av_clip_uint16(new->pts - gif->prev_pkt->pts);
146 else if (!new && gif->last_delay >= 0)
147 gif->duration = gif->last_delay;
149 /* graphic control extension block */
152 avio_w8(pb, 0x04); /* block size */
154 avio_wl16(pb, gif->duration);
155 avio_w8(pb, transparent_color_index);
158 avio_write(pb, pkt->data, pkt->size);
160 av_free_packet(gif->prev_pkt);
162 av_copy_packet(gif->prev_pkt, new);
167 static int gif_write_packet(AVFormatContext *s, AVPacket *pkt)
169 GIFContext *gif = s->priv_data;
171 if (!gif->prev_pkt) {
172 gif->prev_pkt = av_malloc(sizeof(*gif->prev_pkt));
174 return AVERROR(ENOMEM);
175 return av_copy_packet(gif->prev_pkt, pkt);
177 return flush_packet(s, pkt);
180 static int gif_write_trailer(AVFormatContext *s)
182 GIFContext *gif = s->priv_data;
183 AVIOContext *pb = s->pb;
185 flush_packet(s, NULL);
186 av_freep(&gif->prev_pkt);
192 #define OFFSET(x) offsetof(GIFContext, x)
193 #define ENC AV_OPT_FLAG_ENCODING_PARAM
194 static const AVOption options[] = {
195 { "loop", "Number of times to loop the output: -1 - no loop, 0 - infinite loop", OFFSET(loop),
196 AV_OPT_TYPE_INT, { .i64 = 0 }, -1, 65535, ENC },
197 { "final_delay", "Force delay (in centiseconds) after the last frame", OFFSET(last_delay),
198 AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 65535, ENC },
202 static const AVClass gif_muxer_class = {
203 .class_name = "GIF muxer",
204 .item_name = av_default_item_name,
205 .version = LIBAVUTIL_VERSION_INT,
209 AVOutputFormat ff_gif_muxer = {
211 .long_name = NULL_IF_CONFIG_SMALL("GIF Animation"),
212 .mime_type = "image/gif",
214 .priv_data_size = sizeof(GIFContext),
215 .audio_codec = AV_CODEC_ID_NONE,
216 .video_codec = AV_CODEC_ID_GIF,
217 .write_header = gif_write_header,
218 .write_packet = gif_write_packet,
219 .write_trailer = gif_write_trailer,
220 .priv_class = &gif_muxer_class,
221 .flags = AVFMT_VARIABLE_FPS,