blob: 91cd40db5c135930421a3784d0ddb2c6c4342a1b
1 | /* |
2 | * Animated GIF muxer |
3 | * Copyright (c) 2000 Fabrice Bellard |
4 | * |
5 | * first version by Francois Revol <revol@free.fr> |
6 | * |
7 | * This file is part of FFmpeg. |
8 | * |
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. |
13 | * |
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. |
18 | * |
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 |
22 | */ |
23 | |
24 | #include "avformat.h" |
25 | #include "internal.h" |
26 | #include "libavutil/avassert.h" |
27 | #include "libavutil/imgutils.h" |
28 | #include "libavutil/log.h" |
29 | #include "libavutil/opt.h" |
30 | |
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 |
34 | |
35 | static int get_palette_transparency_index(const uint32_t *palette) |
36 | { |
37 | int transparent_color_index = -1; |
38 | unsigned i, smallest_alpha = 0xff; |
39 | |
40 | if (!palette) |
41 | return -1; |
42 | |
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; |
48 | } |
49 | } |
50 | return smallest_alpha < 128 ? transparent_color_index : -1; |
51 | } |
52 | |
53 | static int gif_image_write_header(AVIOContext *pb, AVStream *st, |
54 | int loop_count, uint32_t *palette) |
55 | { |
56 | int i; |
57 | int64_t aspect = 0; |
58 | const AVRational sar = st->sample_aspect_ratio; |
59 | |
60 | if (sar.num > 0 && sar.den > 0) { |
61 | aspect = sar.num * 64LL / sar.den - 15; |
62 | if (aspect < 0 || aspect > 255) |
63 | aspect = 0; |
64 | } |
65 | |
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); |
70 | |
71 | if (palette) { |
72 | const int bcid = get_palette_transparency_index(palette); |
73 | |
74 | avio_w8(pb, 0xf7); /* flags: global clut, 256 entries */ |
75 | avio_w8(pb, bcid < 0 ? DEFAULT_TRANSPARENCY_INDEX : bcid); /* background color index */ |
76 | avio_w8(pb, aspect); |
77 | for (i = 0; i < 256; i++) { |
78 | const uint32_t v = palette[i] & 0xffffff; |
79 | avio_wb24(pb, v); |
80 | } |
81 | } else { |
82 | avio_w8(pb, 0); /* flags */ |
83 | avio_w8(pb, 0); /* background color index */ |
84 | avio_w8(pb, aspect); |
85 | } |
86 | |
87 | |
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 */ |
95 | avio_w8(pb, 0x01); |
96 | avio_wl16(pb, (uint16_t)loop_count); |
97 | avio_w8(pb, 0x00); /* Data Sub-block Terminator */ |
98 | } |
99 | |
100 | avio_flush(pb); |
101 | return 0; |
102 | } |
103 | |
104 | typedef struct GIFContext { |
105 | AVClass *class; |
106 | int loop; |
107 | int last_delay; |
108 | AVPacket *prev_pkt; |
109 | int duration; |
110 | } GIFContext; |
111 | |
112 | static int gif_write_header(AVFormatContext *s) |
113 | { |
114 | GIFContext *gif = s->priv_data; |
115 | AVCodecParameters *video_par; |
116 | uint32_t palette[AVPALETTE_COUNT]; |
117 | |
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); |
124 | } |
125 | |
126 | video_par = s->streams[0]->codecpar; |
127 | |
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 |
132 | * globally */ |
133 | } else { |
134 | gif_image_write_header(s->pb, s->streams[0], gif->loop, palette); |
135 | } |
136 | |
137 | return 0; |
138 | } |
139 | |
140 | static int flush_packet(AVFormatContext *s, AVPacket *new) |
141 | { |
142 | GIFContext *gif = s->priv_data; |
143 | int size, bcid; |
144 | AVIOContext *pb = s->pb; |
145 | const uint32_t *palette; |
146 | AVPacket *pkt = gif->prev_pkt; |
147 | |
148 | if (!pkt) |
149 | return 0; |
150 | |
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; |
157 | } |
158 | bcid = get_palette_transparency_index(palette); |
159 | |
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; |
164 | |
165 | /* graphic control extension block */ |
166 | avio_w8(pb, 0x21); |
167 | avio_w8(pb, 0xf9); |
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); |
172 | avio_w8(pb, 0x00); |
173 | |
174 | avio_write(pb, pkt->data, pkt->size); |
175 | |
176 | av_packet_unref(gif->prev_pkt); |
177 | if (new) |
178 | av_copy_packet(gif->prev_pkt, new); |
179 | |
180 | return 0; |
181 | } |
182 | |
183 | static int gif_write_packet(AVFormatContext *s, AVPacket *pkt) |
184 | { |
185 | GIFContext *gif = s->priv_data; |
186 | AVStream *video_st = s->streams[0]; |
187 | |
188 | if (!gif->prev_pkt) { |
189 | gif->prev_pkt = av_malloc(sizeof(*gif->prev_pkt)); |
190 | if (!gif->prev_pkt) |
191 | return AVERROR(ENOMEM); |
192 | |
193 | /* Write the first palette as global palette */ |
194 | if (video_st->codecpar->format == AV_PIX_FMT_PAL8) { |
195 | int size; |
196 | void *palette = av_packet_get_side_data(pkt, AV_PKT_DATA_PALETTE, &size); |
197 | |
198 | if (!palette) { |
199 | av_log(s, AV_LOG_ERROR, "PAL8 packet is missing palette in extradata\n"); |
200 | return AVERROR_INVALIDDATA; |
201 | } |
202 | if (size != AVPALETTE_SIZE) { |
203 | av_log(s, AV_LOG_ERROR, "Invalid palette extradata\n"); |
204 | return AVERROR_INVALIDDATA; |
205 | } |
206 | gif_image_write_header(s->pb, video_st, gif->loop, palette); |
207 | } |
208 | |
209 | return av_copy_packet(gif->prev_pkt, pkt); |
210 | } |
211 | return flush_packet(s, pkt); |
212 | } |
213 | |
214 | static int gif_write_trailer(AVFormatContext *s) |
215 | { |
216 | GIFContext *gif = s->priv_data; |
217 | AVIOContext *pb = s->pb; |
218 | |
219 | flush_packet(s, NULL); |
220 | av_freep(&gif->prev_pkt); |
221 | avio_w8(pb, 0x3b); |
222 | |
223 | return 0; |
224 | } |
225 | |
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 }, |
233 | { NULL }, |
234 | }; |
235 | |
236 | static const AVClass gif_muxer_class = { |
237 | .class_name = "GIF muxer", |
238 | .item_name = av_default_item_name, |
239 | .version = LIBAVUTIL_VERSION_INT, |
240 | .option = options, |
241 | }; |
242 | |
243 | AVOutputFormat ff_gif_muxer = { |
244 | .name = "gif", |
245 | .long_name = NULL_IF_CONFIG_SMALL("GIF Animation"), |
246 | .mime_type = "image/gif", |
247 | .extensions = "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, |
256 | }; |
257 |