FFmpeg
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"
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 static 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, "%f", &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 {
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;
253  ast->codecpar->ch_layout.nb_channels = 1;
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 
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 
319  .p.name = "vivo",
320  .p.long_name = NULL_IF_CONFIG_SMALL("Vivo"),
321  .p.extensions = "viv",
322  .priv_data_size = sizeof(VivoContext),
326 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
VivoContext
Definition: vivo.c:35
AV_CODEC_ID_SIREN
@ AV_CODEC_ID_SIREN
Definition: codec_id.h:538
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
int64_t
long long int64_t
Definition: coverity.c:34
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:327
VivoContext::duration
int duration
Definition: vivo.c:41
VivoContext::version
int version
Definition: vivo.c:36
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
vivo_get_packet_header
static int vivo_get_packet_header(AVFormatContext *s)
Definition: vivo.c:76
avpriv_set_pts_info
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:867
VivoContext::type
int type
Definition: vivo.c:38
AVRational::num
int num
Numerator.
Definition: rational.h:59
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
duration
int64_t duration
Definition: movenc.c:65
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
vivo_probe
static int vivo_probe(const AVProbeData *p)
Definition: vivo.c:46
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
vivo_read_packet
static int vivo_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: vivo.c:258
key
const char * key
Definition: hwcontext_opencl.c:189
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
vivo_read_header
static int vivo_read_header(AVFormatContext *s)
Definition: vivo.c:122
av_sscanf
int av_sscanf(const char *string, const char *format,...)
See libc sscanf manual for more information.
Definition: avsscanf.c:961
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
VivoContext::length
int length
Definition: vivo.c:40
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AV_CODEC_ID_G723_1
@ AV_CODEC_ID_G723_1
Definition: codec_id.h:498
parseutils.h
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
VivoContext::text
uint8_t text[1024+1]
Definition: vivo.c:43
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AV_CODEC_ID_H263
@ AV_CODEC_ID_H263
Definition: codec_id.h:56
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
line
Definition: graph2dot.c:48
AVCodecParameters::height
int height
Definition: codec_par.h:135
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
av_d2q
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
demux.h
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
av_get_packet
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:91
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
av_append_packet
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:107
avformat.h
AVRational::den
int den
Denominator.
Definition: rational.h:60
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
AVPacket::stream_index
int stream_index
Definition: packet.h:541
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
av_dict_set
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:88
FFInputFormat
Definition: demux.h:42
VivoContext::sequence
int sequence
Definition: vivo.c:39
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:797
avstring.h
line
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common like for each output line the vertical scaler pulls lines from a ring buffer When the ring buffer does not contain the wanted line
Definition: swscale.txt:40
ff_vivo_demuxer
const FFInputFormat ff_vivo_demuxer
Definition: vivo.c:318
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346