FFmpeg
Loading...
Searching...
No Matches
vivo.c
Go to the documentation of this file.
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"
31#include "avformat.h"
32#include "demux.h"
33#include "internal.h"
34
35typedef struct VivoContext {
37
38 int type;
40 int length;
42
43 uint8_t text[1024 + 1];
45
46static int vivo_probe(const AVProbeData *p)
47{
48 const unsigned char *buf = p->buf;
49 unsigned c, length = 0;
50
51 // stream must start with packet of type 0 and sequence number 0
52 if (*buf++ != 0)
53 return 0;
54
55 // read at most 2 bytes of coded length
56 c = *buf++;
57 length = c & 0x7F;
58 if (c & 0x80) {
59 c = *buf++;
60 length = (length << 7) | (c & 0x7F);
61 }
62 if (c & 0x80 || length > 1024 || length < 21)
63 return 0;
64
65 buf += 2;
66 if (memcmp(buf, "Version:Vivo/", 13))
67 return 0;
68 buf += 13;
69
70 if (*buf < '0' || *buf > '2')
71 return 0;
72
73 return AVPROBE_SCORE_MAX;
74}
75
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
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;
235
236 if (vivo->version == 1) {
240 ast->codecpar->block_align = 24;
241 ast->codecpar->bit_rate = 6400;
242 } else {
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;
254
255 return 0;
256}
257
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
265restart:
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
319 .p.name = "vivo",
320 .p.long_name = NULL_IF_CONFIG_SMALL("Vivo"),
321 .p.extensions = "viv",
322 .priv_data_size = sizeof(VivoContext),
326};
const FFInputFormat ff_vivo_demuxer
Definition vivo.c:318
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
Read data and append it to the current content of the AVPacket.
Definition utils.c:114
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
int av_sscanf(const char *string, const char *format,...)
Definition avsscanf.c:962
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
double value
Definition eval.c:102
const char * key
static int64_t duration
Definition ffplay.c:330
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_G723_1
Definition codec_id.h:505
@ AV_CODEC_ID_SIREN
Definition codec_id.h:545
@ AV_CODEC_ID_H263
Definition codec_id.h:54
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition rational.c:110
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
misc parsing utilities
int nb_channels
Number of channels in this layout.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
int sequence
Definition vivo.c:39
uint8_t text[1024+1]
Definition vivo.c:43
int duration
Definition vivo.c:41
int type
Definition vivo.c:38
int length
Definition vivo.c:40
int version
Definition vivo.c:36
#define av_log(a,...)
static int vivo_read_header(AVFormatContext *s)
Definition vivo.c:122
static int vivo_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition vivo.c:258
static int vivo_probe(const AVProbeData *p)
Definition vivo.c:46
static int vivo_get_packet_header(AVFormatContext *s)
Definition vivo.c:76
static double c[64]