FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/gif.c
Date: 2023-12-07 21:54:23
Exec Total Coverage
Lines: 74 91 81.3%
Functions: 5 5 100.0%
Branches: 34 64 53.1%

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