FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/vivo.c
Date: 2026-09-25 07:50:45
Exec Total Coverage
Lines: 15 181 8.3%
Functions: 1 4 25.0%
Branches: 11 114 9.6%

Line Branch Exec Source
1 /*
2 * Vivo stream demuxer
3 * Copyright (c) 2009 Daniel Verkamp <daniel at drv.nu>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * @brief Vivo stream demuxer
25 * @author Daniel Verkamp <daniel at drv.nu>
26 * @sa http://wiki.multimedia.cx/index.php?title=Vivo
27 */
28
29 #include "libavutil/avstring.h"
30 #include "libavutil/parseutils.h"
31 #include "avformat.h"
32 #include "demux.h"
33 #include "internal.h"
34
35 typedef struct VivoContext {
36 int version;
37
38 int type;
39 int sequence;
40 int length;
41 int duration;
42
43 uint8_t text[1024 + 1];
44 } VivoContext;
45
46 8064 static int vivo_probe(const AVProbeData *p)
47 {
48 8064 const unsigned char *buf = p->buf;
49 8064 unsigned c, length = 0;
50
51 // stream must start with packet of type 0 and sequence number 0
52
2/2
✓ Branch 0 taken 6476 times.
✓ Branch 1 taken 1588 times.
8064 if (*buf++ != 0)
53 6476 return 0;
54
55 // read at most 2 bytes of coded length
56 1588 c = *buf++;
57 1588 length = c & 0x7F;
58
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1581 times.
1588 if (c & 0x80) {
59 7 c = *buf++;
60 7 length = (length << 7) | (c & 0x7F);
61 }
62
6/6
✓ Branch 0 taken 1584 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1581 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1577 times.
✓ Branch 5 taken 4 times.
1588 if (c & 0x80 || length > 1024 || length < 21)
63 1584 return 0;
64
65 4 buf += 2;
66
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (memcmp(buf, "Version:Vivo/", 13))
67 4 return 0;
68 ✗ buf += 13;
69
70 ✗ if (*buf < '0' || *buf > '2')
71 ✗ return 0;
72
73 ✗ return AVPROBE_SCORE_MAX;
74 }
75
76 ✗ static int vivo_get_packet_header(AVFormatContext *s)
77 {
78 ✗ VivoContext *vivo = s->priv_data;
79 ✗ AVIOContext *pb = s->pb;
80 ✗ unsigned c, get_length = 0;
81
82 ✗ if (avio_feof(pb))
83 ✗ return AVERROR_EOF;
84
85 ✗ c = avio_r8(pb);
86 ✗ if (c == 0x82) {
87 ✗ get_length = 1;
88 ✗ c = avio_r8(pb);
89 }
90
91 ✗ vivo->type = c >> 4;
92 ✗ vivo->sequence = c & 0xF;
93
94 ✗ switch (vivo->type) {
95 ✗ case 0: get_length = 1; break;
96 ✗ case 1: vivo->length = 128; break;
97 ✗ case 2: get_length = 1; break;
98 ✗ case 3: vivo->length = 40; break;
99 ✗ case 4: vivo->length = 24; break;
100 ✗ default:
101 ✗ av_log(s, AV_LOG_ERROR, "unknown packet type %d\n", vivo->type);
102 ✗ return AVERROR_INVALIDDATA;
103 }
104
105 ✗ if (get_length) {
106 ✗ c = avio_r8(pb);
107 ✗ vivo->length = c & 0x7F;
108 ✗ if (c & 0x80) {
109 ✗ c = avio_r8(pb);
110 ✗ vivo->length = (vivo->length << 7) | (c & 0x7F);
111
112 ✗ if (c & 0x80) {
113 ✗ av_log(s, AV_LOG_ERROR, "coded length is more than two bytes\n");
114 ✗ return AVERROR_INVALIDDATA;
115 }
116 }
117 }
118
119 ✗ return 0;
120 }
121
122 ✗ static int vivo_read_header(AVFormatContext *s)
123 {
124 ✗ VivoContext *vivo = s->priv_data;
125 ✗ AVRational fps = { 0 };
126 AVStream *ast, *vst;
127 unsigned char *line, *line_end, *key, *value;
128 long value_int;
129 int ret, value_used;
130 ✗ int64_t duration = 0;
131 char *end_value;
132
133 ✗ vst = avformat_new_stream(s, NULL);
134 ✗ ast = avformat_new_stream(s, NULL);
135 ✗ if (!ast || !vst)
136 ✗ return AVERROR(ENOMEM);
137
138 ✗ ast->codecpar->sample_rate = 8000;
139
140 while (1) {
141 ✗ if ((ret = vivo_get_packet_header(s)) < 0)
142 ✗ return ret;
143
144 // done reading all text header packets?
145 ✗ if (vivo->sequence || vivo->type)
146 break;
147
148 ✗ if (vivo->length <= 1024) {
149 ✗ avio_read(s->pb, vivo->text, vivo->length);
150 ✗ vivo->text[vivo->length] = 0;
151 } else {
152 ✗ av_log(s, AV_LOG_WARNING, "too big header, skipping\n");
153 ✗ avio_skip(s->pb, vivo->length);
154 ✗ continue;
155 }
156
157 ✗ line = vivo->text;
158 ✗ while (*line) {
159 ✗ line_end = strstr(line, "\r\n");
160 ✗ if (!line_end)
161 ✗ break;
162
163 ✗ *line_end = 0;
164 ✗ key = line;
165 ✗ line = line_end + 2; // skip \r\n
166
167 ✗ if (line_end == key) // skip blank lines
168 ✗ continue;
169
170 ✗ value = strchr(key, ':');
171 ✗ if (!value) {
172 ✗ av_log(s, AV_LOG_WARNING, "missing colon in key:value pair '%s'\n",
173 key);
174 ✗ continue;
175 }
176
177 ✗ *value++ = 0;
178
179 ✗ av_log(s, AV_LOG_DEBUG, "header: '%s' = '%s'\n", key, value);
180
181 ✗ value_int = strtol(value, &end_value, 10);
182 ✗ value_used = 0;
183 ✗ if (*end_value == 0) { // valid integer
184 ✗ av_log(s, AV_LOG_DEBUG, "got a valid integer (%ld)\n", value_int);
185 ✗ value_used = 1;
186 ✗ if (!strcmp(key, "Duration")) {
187 ✗ duration = value_int;
188 ✗ } else if (!strcmp(key, "Width")) {
189 ✗ vst->codecpar->width = value_int;
190 ✗ } else if (!strcmp(key, "Height")) {
191 ✗ vst->codecpar->height = value_int;
192 ✗ } else if (!strcmp(key, "TimeUnitNumerator")) {
193 ✗ fps.num = value_int / 1000;
194 ✗ } else if (!strcmp(key, "TimeUnitDenominator")) {
195 ✗ fps.den = value_int;
196 ✗ } else if (!strcmp(key, "SamplingFrequency")) {
197 ✗ ast->codecpar->sample_rate = value_int;
198 ✗ } else if (!strcmp(key, "NominalBitrate")) {
199 ✗ } else if (!strcmp(key, "Length")) {
200 // size of file
201 } else {
202 ✗ value_used = 0;
203 }
204 }
205
206 ✗ if (!strcmp(key, "Version")) {
207 ✗ if (sscanf(value, "Vivo/%d.", &vivo->version) != 1)
208 ✗ return AVERROR_INVALIDDATA;
209 ✗ value_used = 1;
210 ✗ } else if (!strcmp(key, "FPS")) {
211 double d;
212 ✗ if (av_sscanf(value, "%lf", &d) != 1)
213 ✗ return AVERROR_INVALIDDATA;
214
215 ✗ value_used = 1;
216 ✗ if (!fps.num && !fps.den)
217 ✗ fps = av_inv_q(av_d2q(d, 10000));
218 }
219
220 ✗ if (!value_used)
221 ✗ av_dict_set(&s->metadata, key, value, 0);
222 }
223 }
224 ✗ if (!fps.num || !fps.den)
225 ✗ fps = (AVRational){ 1, 25 };
226
227 ✗ avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
228 ✗ avpriv_set_pts_info(vst, 64, fps.num, fps.den);
229 ✗ if (duration)
230 ✗ s->duration = av_rescale(duration, 1000, 1);
231
232 ✗ vst->start_time = 0;
233 ✗ vst->codecpar->codec_tag = 0;
234 ✗ vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
235
236 ✗ if (vivo->version == 1) {
237 ✗ vst->codecpar->codec_id = AV_CODEC_ID_H263;
238 ✗ ast->codecpar->codec_id = AV_CODEC_ID_G723_1;
239 ✗ ast->codecpar->bits_per_coded_sample = 8;
240 ✗ ast->codecpar->block_align = 24;
241 ✗ ast->codecpar->bit_rate = 6400;
242 } else {
243 ✗ ast->codecpar->codec_id = AV_CODEC_ID_SIREN;
244 ✗ ast->codecpar->bits_per_coded_sample = 16;
245 ✗ ast->codecpar->block_align = 40;
246 ✗ ast->codecpar->bit_rate = 6400;
247 ✗ vivo->duration = 320;
248 }
249
250 ✗ ast->start_time = 0;
251 ✗ ast->codecpar->codec_tag = 0;
252 ✗ ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
253 ✗ ast->codecpar->ch_layout.nb_channels = 1;
254
255 ✗ return 0;
256 }
257
258 ✗ static int vivo_read_packet(AVFormatContext *s, AVPacket *pkt)
259 {
260 ✗ VivoContext *vivo = s->priv_data;
261 ✗ AVIOContext *pb = s->pb;
262 ✗ unsigned old_sequence = vivo->sequence, old_type = vivo->type;
263 ✗ int stream_index, duration, ret = 0;
264
265 ✗ restart:
266
267 ✗ if (avio_feof(pb))
268 ✗ return AVERROR_EOF;
269
270 ✗ switch (vivo->type) {
271 ✗ case 0:
272 ✗ avio_skip(pb, vivo->length);
273 ✗ if ((ret = vivo_get_packet_header(s)) < 0)
274 ✗ return ret;
275 ✗ goto restart;
276 ✗ case 1:
277 case 2: // video
278 ✗ stream_index = 0;
279 ✗ duration = 1;
280 ✗ break;
281 ✗ case 3:
282 case 4: // audio
283 ✗ stream_index = 1;
284 ✗ duration = vivo->duration;
285 ✗ break;
286 ✗ default:
287 ✗ av_log(s, AV_LOG_ERROR, "unknown packet type %d\n", vivo->type);
288 ✗ return AVERROR_INVALIDDATA;
289 }
290
291 ✗ if ((ret = av_get_packet(pb, pkt, vivo->length)) < 0)
292 ✗ return ret;
293
294 // get next packet header
295 ✗ if ((ret = vivo_get_packet_header(s)) < 0)
296 ✗ return ret;
297
298 ✗ while (vivo->sequence == old_sequence &&
299 ✗ (((vivo->type - 1) >> 1) == ((old_type - 1) >> 1))) {
300 ✗ if (avio_feof(pb)) {
301 ✗ return AVERROR_EOF;
302 }
303
304 ✗ if ((ret = av_append_packet(pb, pkt, vivo->length)) < 0)
305 ✗ return ret;
306
307 // get next packet header
308 ✗ if ((ret = vivo_get_packet_header(s)) < 0)
309 ✗ return ret;
310 }
311
312 ✗ pkt->stream_index = stream_index;
313 ✗ pkt->duration = duration;
314
315 ✗ return ret;
316 }
317
318 const FFInputFormat ff_vivo_demuxer = {
319 .p.name = "vivo",
320 .p.long_name = NULL_IF_CONFIG_SMALL("Vivo"),
321 .p.extensions = "viv",
322 .priv_data_size = sizeof(VivoContext),
323 .read_probe = vivo_probe,
324 .read_header = vivo_read_header,
325 .read_packet = vivo_read_packet,
326 };
327