Line | Branch | Exec | Source |
---|---|---|---|
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 "mux.h" | ||
27 | #include "libavutil/imgutils.h" | ||
28 | #include "libavutil/log.h" | ||
29 | #include "libavutil/opt.h" | ||
30 | #include "libavcodec/bytestream.h" | ||
31 | #include "libavcodec/gif.h" | ||
32 | |||
33 | typedef struct GIFContext { | ||
34 | AVClass *class; | ||
35 | int loop; | ||
36 | int last_delay; | ||
37 | int duration; | ||
38 | int64_t last_pos; | ||
39 | int have_end; | ||
40 | AVPacket *prev_pkt; | ||
41 | } GIFContext; | ||
42 | |||
43 | 1 | static av_cold int gif_init(AVFormatContext *s) | |
44 | { | ||
45 | 1 | avpriv_set_pts_info(s->streams[0], 64, 1, 100); | |
46 | |||
47 | 1 | return 0; | |
48 | } | ||
49 | |||
50 | 25 | static int gif_parse_packet(AVFormatContext *s, const uint8_t *data, int size) | |
51 | { | ||
52 | GetByteContext gb; | ||
53 | int x; | ||
54 | |||
55 | 25 | bytestream2_init(&gb, data, size); | |
56 | |||
57 |
1/2✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
|
25 | while (bytestream2_get_bytes_left(&gb) > 0) { |
58 | 25 | x = bytestream2_get_byte(&gb); | |
59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (x != GIF_EXTENSION_INTRODUCER) |
60 | ✗ | return 0; | |
61 | |||
62 | 25 | x = bytestream2_get_byte(&gb); | |
63 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
25 | while (x != GIF_GCE_EXT_LABEL && bytestream2_get_bytes_left(&gb) > 0) { |
64 | ✗ | int block_size = bytestream2_get_byte(&gb); | |
65 | ✗ | if (!block_size) | |
66 | ✗ | break; | |
67 | ✗ | bytestream2_skip(&gb, block_size); | |
68 | } | ||
69 | |||
70 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | if (x == GIF_GCE_EXT_LABEL) |
71 | 25 | return bytestream2_tell(&gb) + 2; | |
72 | } | ||
73 | |||
74 | ✗ | return 0; | |
75 | } | ||
76 | |||
77 | 25 | static int gif_get_delay(GIFContext *gif, AVPacket *prev, AVPacket *new) | |
78 | { | ||
79 |
3/4✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
25 | if (new && new->pts != AV_NOPTS_VALUE) |
80 | 24 | gif->duration = av_clip_uint16(new->pts - prev->pts); | |
81 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | else if (!new && gif->last_delay >= 0) |
82 | ✗ | gif->duration = gif->last_delay; | |
83 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | else if (prev->duration) |
84 | 1 | gif->duration = prev->duration; | |
85 | |||
86 | 25 | return gif->duration; | |
87 | } | ||
88 | |||
89 | 26 | static int gif_write_packet(AVFormatContext *s, AVPacket *new_pkt) | |
90 | { | ||
91 | 26 | GIFContext *gif = s->priv_data; | |
92 | 26 | AVIOContext *pb = s->pb; | |
93 | 26 | AVPacket *pkt = gif->prev_pkt; | |
94 | |||
95 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 25 times.
|
26 | if (!gif->prev_pkt) { |
96 | 1 | gif->prev_pkt = av_packet_alloc(); | |
97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!gif->prev_pkt) |
98 | ✗ | return AVERROR(ENOMEM); | |
99 | 1 | return av_packet_ref(gif->prev_pkt, new_pkt); | |
100 | } | ||
101 | |||
102 | 25 | gif->last_pos = avio_tell(pb); | |
103 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | if (pkt->size > 0) |
104 | 25 | gif->have_end = pkt->data[pkt->size - 1] == GIF_TRAILER; | |
105 | |||
106 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 24 times.
|
25 | if (!gif->last_pos) { |
107 | int delay_pos; | ||
108 | 1 | int off = 13; | |
109 | |||
110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (pkt->size < 13) |
111 | ✗ | return AVERROR(EINVAL); | |
112 | |||
113 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (pkt->data[10] & 0x80) |
114 | 1 | off += 3 * (1 << ((pkt->data[10] & 0x07) + 1)); | |
115 | |||
116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (pkt->size < off + 2) |
117 | ✗ | return AVERROR(EINVAL); | |
118 | |||
119 | 1 | avio_write(pb, pkt->data, off); | |
120 | |||
121 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (pkt->data[off] == GIF_EXTENSION_INTRODUCER && pkt->data[off + 1] == 0xff) |
122 | ✗ | off += 19; | |
123 | |||
124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (pkt->size <= off) |
125 | ✗ | return AVERROR(EINVAL); | |
126 | |||
127 | /* "NETSCAPE EXTENSION" for looped animation GIF */ | ||
128 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (gif->loop >= 0) { |
129 | 1 | avio_w8(pb, GIF_EXTENSION_INTRODUCER); /* GIF Extension code */ | |
130 | 1 | avio_w8(pb, GIF_APP_EXT_LABEL); /* Application Extension Label */ | |
131 | 1 | avio_w8(pb, 0x0b); /* Length of Application Block */ | |
132 | 1 | avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1); | |
133 | 1 | avio_w8(pb, 0x03); /* Length of Data Sub-Block */ | |
134 | 1 | avio_w8(pb, 0x01); | |
135 | 1 | avio_wl16(pb, (uint16_t)gif->loop); | |
136 | 1 | avio_w8(pb, 0x00); /* Data Sub-block Terminator */ | |
137 | } | ||
138 | |||
139 | 1 | delay_pos = gif_parse_packet(s, pkt->data + off, pkt->size - off); | |
140 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (delay_pos > 0 && delay_pos < pkt->size - off - 2) { |
141 | 1 | avio_write(pb, pkt->data + off, delay_pos); | |
142 | 1 | avio_wl16(pb, gif_get_delay(gif, pkt, new_pkt)); | |
143 | 1 | avio_write(pb, pkt->data + off + delay_pos + 2, pkt->size - off - delay_pos - 2); | |
144 | } else { | ||
145 | ✗ | avio_write(pb, pkt->data + off, pkt->size - off); | |
146 | } | ||
147 | } else { | ||
148 | 24 | int delay_pos = gif_parse_packet(s, pkt->data, pkt->size); | |
149 | |||
150 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
24 | if (delay_pos > 0 && delay_pos < pkt->size - 2) { |
151 | 24 | avio_write(pb, pkt->data, delay_pos); | |
152 | 24 | avio_wl16(pb, gif_get_delay(gif, pkt, new_pkt)); | |
153 | 24 | avio_write(pb, pkt->data + delay_pos + 2, pkt->size - delay_pos - 2); | |
154 | } else { | ||
155 | ✗ | avio_write(pb, pkt->data, pkt->size); | |
156 | } | ||
157 | } | ||
158 | |||
159 | 25 | av_packet_unref(gif->prev_pkt); | |
160 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 times.
|
25 | if (new_pkt) |
161 | 24 | return av_packet_ref(gif->prev_pkt, new_pkt); | |
162 | |||
163 | 1 | return 0; | |
164 | } | ||
165 | |||
166 | 1 | static int gif_write_trailer(AVFormatContext *s) | |
167 | { | ||
168 | 1 | GIFContext *gif = s->priv_data; | |
169 | 1 | AVIOContext *pb = s->pb; | |
170 | |||
171 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!gif->prev_pkt) |
172 | ✗ | return AVERROR(EINVAL); | |
173 | |||
174 | 1 | gif_write_packet(s, NULL); | |
175 | |||
176 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!gif->have_end) |
177 | 1 | avio_w8(pb, GIF_TRAILER); | |
178 | 1 | av_packet_free(&gif->prev_pkt); | |
179 | |||
180 | 1 | return 0; | |
181 | } | ||
182 | |||
183 | #define OFFSET(x) offsetof(GIFContext, x) | ||
184 | #define ENC AV_OPT_FLAG_ENCODING_PARAM | ||
185 | static const AVOption options[] = { | ||
186 | { "loop", "Number of times to loop the output: -1 - no loop, 0 - infinite loop", OFFSET(loop), | ||
187 | AV_OPT_TYPE_INT, { .i64 = 0 }, -1, 65535, ENC }, | ||
188 | { "final_delay", "Force delay (in centiseconds) after the last frame", OFFSET(last_delay), | ||
189 | AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 65535, ENC }, | ||
190 | { NULL }, | ||
191 | }; | ||
192 | |||
193 | static const AVClass gif_muxer_class = { | ||
194 | .class_name = "GIF muxer", | ||
195 | .item_name = av_default_item_name, | ||
196 | .version = LIBAVUTIL_VERSION_INT, | ||
197 | .option = options, | ||
198 | }; | ||
199 | |||
200 | const FFOutputFormat ff_gif_muxer = { | ||
201 | .p.name = "gif", | ||
202 | .p.long_name = NULL_IF_CONFIG_SMALL("CompuServe Graphics Interchange Format (GIF)"), | ||
203 | .p.mime_type = "image/gif", | ||
204 | .p.extensions = "gif", | ||
205 | .priv_data_size = sizeof(GIFContext), | ||
206 | .p.audio_codec = AV_CODEC_ID_NONE, | ||
207 | .p.video_codec = AV_CODEC_ID_GIF, | ||
208 | .p.subtitle_codec = AV_CODEC_ID_NONE, | ||
209 | .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH | | ||
210 | FF_OFMT_FLAG_ONLY_DEFAULT_CODECS, | ||
211 | .init = gif_init, | ||
212 | .write_packet = gif_write_packet, | ||
213 | .write_trailer = gif_write_trailer, | ||
214 | .p.priv_class = &gif_muxer_class, | ||
215 | .p.flags = AVFMT_VARIABLE_FPS, | ||
216 | }; | ||
217 |