3 * Copyright (c) 2009 Daniel Verkamp <daniel at drv.nu>
5 * This file is part of FFmpeg.
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.
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.
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
24 * @brief Vivo stream demuxer
25 * @author Daniel Verkamp <daniel at drv.nu>
26 * @sa http://wiki.multimedia.cx/index.php?title=Vivo
29 #include "libavutil/parseutils.h"
33 typedef struct VivoContext {
41 uint8_t text[1024 + 1];
44 static int vivo_probe(const AVProbeData *p)
46 const unsigned char *buf = p->buf;
47 unsigned c, length = 0;
49 // stream must start with packet of type 0 and sequence number 0
53 // read at most 2 bytes of coded length
58 length = (length << 7) | (c & 0x7F);
60 if (c & 0x80 || length > 1024 || length < 21)
64 if (memcmp(buf, "Version:Vivo/", 13))
68 if (*buf < '0' || *buf > '2')
71 return AVPROBE_SCORE_MAX;
74 static int vivo_get_packet_header(AVFormatContext *s)
76 VivoContext *vivo = s->priv_data;
77 AVIOContext *pb = s->pb;
78 unsigned c, get_length = 0;
90 vivo->sequence = c & 0xF;
93 case 0: get_length = 1; break;
94 case 1: vivo->length = 128; break;
95 case 2: get_length = 1; break;
96 case 3: vivo->length = 40; break;
97 case 4: vivo->length = 24; break;
99 av_log(s, AV_LOG_ERROR, "unknown packet type %d\n", vivo->type);
100 return AVERROR_INVALIDDATA;
105 vivo->length = c & 0x7F;
108 vivo->length = (vivo->length << 7) | (c & 0x7F);
111 av_log(s, AV_LOG_ERROR, "coded length is more than two bytes\n");
112 return AVERROR_INVALIDDATA;
120 static int vivo_read_header(AVFormatContext *s)
122 VivoContext *vivo = s->priv_data;
123 AVRational fps = { 1, 25};
125 unsigned char *line, *line_end, *key, *value;
128 int64_t duration = 0;
131 vst = avformat_new_stream(s, NULL);
132 ast = avformat_new_stream(s, NULL);
134 return AVERROR(ENOMEM);
136 ast->codecpar->sample_rate = 8000;
139 if ((ret = vivo_get_packet_header(s)) < 0)
142 // done reading all text header packets?
143 if (vivo->sequence || vivo->type)
146 if (vivo->length <= 1024) {
147 avio_read(s->pb, vivo->text, vivo->length);
148 vivo->text[vivo->length] = 0;
150 av_log(s, AV_LOG_WARNING, "too big header, skipping\n");
151 avio_skip(s->pb, vivo->length);
157 line_end = strstr(line, "\r\n");
163 line = line_end + 2; // skip \r\n
165 if (line_end == key) // skip blank lines
168 value = strchr(key, ':');
170 av_log(s, AV_LOG_WARNING, "missing colon in key:value pair '%s'\n",
177 av_log(s, AV_LOG_DEBUG, "header: '%s' = '%s'\n", key, value);
179 value_int = strtol(value, &end_value, 10);
181 if (*end_value == 0) { // valid integer
182 av_log(s, AV_LOG_DEBUG, "got a valid integer (%ld)\n", value_int);
184 if (!strcmp(key, "Duration")) {
185 duration = value_int;
186 } else if (!strcmp(key, "Width")) {
187 vst->codecpar->width = value_int;
188 } else if (!strcmp(key, "Height")) {
189 vst->codecpar->height = value_int;
190 } else if (!strcmp(key, "TimeUnitNumerator")) {
191 fps.num = value_int / 1000;
192 } else if (!strcmp(key, "TimeUnitDenominator")) {
194 } else if (!strcmp(key, "SamplingFrequency")) {
195 ast->codecpar->sample_rate = value_int;
196 } else if (!strcmp(key, "NominalBitrate")) {
197 } else if (!strcmp(key, "Length")) {
204 if (!strcmp(key, "Version")) {
205 if (sscanf(value, "Vivo/%d.", &vivo->version) != 1)
206 return AVERROR_INVALIDDATA;
208 } else if (!strcmp(key, "FPS")) {
212 if (!av_parse_ratio(&tmp, value, 10000, AV_LOG_WARNING, s))
217 av_dict_set(&s->metadata, key, value, 0);
221 avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
222 avpriv_set_pts_info(vst, 64, fps.num, fps.den);
224 s->duration = av_rescale(duration, 1000, 1);
227 vst->codecpar->codec_tag = 0;
228 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
230 if (vivo->version == 1) {
231 vst->codecpar->codec_id = AV_CODEC_ID_H263;
232 ast->codecpar->codec_id = AV_CODEC_ID_G723_1;
233 ast->codecpar->bits_per_coded_sample = 8;
234 ast->codecpar->block_align = 24;
235 ast->codecpar->bit_rate = 6400;
237 ast->codecpar->codec_id = AV_CODEC_ID_SIREN;
238 ast->codecpar->bits_per_coded_sample = 16;
239 ast->codecpar->block_align = 40;
240 ast->codecpar->bit_rate = 6400;
241 vivo->duration = 320;
245 ast->codecpar->codec_tag = 0;
246 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
247 ast->codecpar->channels = 1;
252 static int vivo_read_packet(AVFormatContext *s, AVPacket *pkt)
254 VivoContext *vivo = s->priv_data;
255 AVIOContext *pb = s->pb;
256 unsigned old_sequence = vivo->sequence, old_type = vivo->type;
257 int stream_index, duration, ret = 0;
264 switch (vivo->type) {
266 avio_skip(pb, vivo->length);
267 if ((ret = vivo_get_packet_header(s)) < 0)
278 duration = vivo->duration;
281 av_log(s, AV_LOG_ERROR, "unknown packet type %d\n", vivo->type);
282 return AVERROR_INVALIDDATA;
285 if ((ret = av_get_packet(pb, pkt, vivo->length)) < 0)
288 // get next packet header
289 if ((ret = vivo_get_packet_header(s)) < 0)
292 while (vivo->sequence == old_sequence &&
293 (((vivo->type - 1) >> 1) == ((old_type - 1) >> 1))) {
298 if ((ret = av_append_packet(pb, pkt, vivo->length)) < 0)
301 // get next packet header
302 if ((ret = vivo_get_packet_header(s)) < 0)
306 pkt->stream_index = stream_index;
307 pkt->duration = duration;
312 AVInputFormat ff_vivo_demuxer = {
314 .long_name = NULL_IF_CONFIG_SMALL("Vivo"),
315 .priv_data_size = sizeof(VivoContext),
316 .read_probe = vivo_probe,
317 .read_header = vivo_read_header,
318 .read_packet = vivo_read_packet,