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 "internal.h"
33 
34 typedef struct VivoContext {
35  int version;
36 
37  int type;
38  int sequence;
39  int length;
40 
41  uint8_t text[1024 + 1];
42 } VivoContext;
43 
44 static int vivo_probe(const AVProbeData *p)
45 {
46  const unsigned char *buf = p->buf;
47  unsigned c, length = 0;
48 
49  // stream must start with packet of type 0 and sequence number 0
50  if (*buf++ != 0)
51  return 0;
52 
53  // read at most 2 bytes of coded length
54  c = *buf++;
55  length = c & 0x7F;
56  if (c & 0x80) {
57  c = *buf++;
58  length = (length << 7) | (c & 0x7F);
59  }
60  if (c & 0x80 || length > 1024 || length < 21)
61  return 0;
62 
63  if (memcmp(buf, "\r\nVersion:Vivo/", 15))
64  return 0;
65  buf += 15;
66 
67  if (*buf < '0' || *buf > '2')
68  return 0;
69 
70  return AVPROBE_SCORE_MAX;
71 }
72 
74 {
75  VivoContext *vivo = s->priv_data;
76  AVIOContext *pb = s->pb;
77  unsigned c, get_length = 0;
78 
79  if (avio_feof(pb))
80  return AVERROR_EOF;
81 
82  c = avio_r8(pb);
83  if (c == 0x82) {
84  get_length = 1;
85  c = avio_r8(pb);
86  }
87 
88  vivo->type = c >> 4;
89  vivo->sequence = c & 0xF;
90 
91  switch (vivo->type) {
92  case 0: get_length = 1; break;
93  case 1: vivo->length = 128; break;
94  case 2: get_length = 1; break;
95  case 3: vivo->length = 40; break;
96  case 4: vivo->length = 24; break;
97  default:
98  av_log(s, AV_LOG_ERROR, "unknown packet type %d\n", vivo->type);
99  return AVERROR_INVALIDDATA;
100  }
101 
102  if (get_length) {
103  c = avio_r8(pb);
104  vivo->length = c & 0x7F;
105  if (c & 0x80) {
106  c = avio_r8(pb);
107  vivo->length = (vivo->length << 7) | (c & 0x7F);
108 
109  if (c & 0x80) {
110  av_log(s, AV_LOG_ERROR, "coded length is more than two bytes\n");
111  return AVERROR_INVALIDDATA;
112  }
113  }
114  }
115 
116  return 0;
117 }
118 
120 {
121  VivoContext *vivo = s->priv_data;
122  AVRational fps = { 0 };
123  AVStream *ast, *vst;
124  unsigned char *line, *line_end, *key, *value;
125  long value_int;
126  int ret, value_used;
127  int64_t duration = 0;
128  char *end_value;
129 
130  vst = avformat_new_stream(s, NULL);
131  ast = avformat_new_stream(s, NULL);
132  if (!ast || !vst)
133  return AVERROR(ENOMEM);
134 
135  ast->codecpar->sample_rate = 8000;
136 
137  while (1) {
138  if ((ret = vivo_get_packet_header(s)) < 0)
139  return ret;
140 
141  // done reading all text header packets?
142  if (vivo->sequence || vivo->type)
143  break;
144 
145  if (vivo->length <= 1024) {
146  avio_read(s->pb, vivo->text, vivo->length);
147  vivo->text[vivo->length] = 0;
148  } else {
149  av_log(s, AV_LOG_WARNING, "too big header, skipping\n");
150  avio_skip(s->pb, vivo->length);
151  continue;
152  }
153 
154  line = vivo->text;
155  while (*line) {
156  line_end = strstr(line, "\r\n");
157  if (!line_end)
158  break;
159 
160  *line_end = 0;
161  key = line;
162  line = line_end + 2; // skip \r\n
163 
164  if (line_end == key) // skip blank lines
165  continue;
166 
167  value = strchr(key, ':');
168  if (!value) {
169  av_log(s, AV_LOG_WARNING, "missing colon in key:value pair '%s'\n",
170  key);
171  continue;
172  }
173 
174  *value++ = 0;
175 
176  av_log(s, AV_LOG_DEBUG, "header: '%s' = '%s'\n", key, value);
177 
178  value_int = strtol(value, &end_value, 10);
179  value_used = 0;
180  if (*end_value == 0) { // valid integer
181  av_log(s, AV_LOG_DEBUG, "got a valid integer (%ld)\n", value_int);
182  value_used = 1;
183  if (!strcmp(key, "Duration")) {
184  duration = value_int;
185  } else if (!strcmp(key, "Width")) {
186  vst->codecpar->width = value_int;
187  } else if (!strcmp(key, "Height")) {
188  vst->codecpar->height = value_int;
189  } else if (!strcmp(key, "TimeUnitNumerator")) {
190  fps.num = value_int / 1000;
191  } else if (!strcmp(key, "TimeUnitDenominator")) {
192  fps.den = value_int;
193  } else if (!strcmp(key, "SamplingFrequency")) {
194  ast->codecpar->sample_rate = value_int;
195  } else if (!strcmp(key, "NominalBitrate")) {
196  } else if (!strcmp(key, "Length")) {
197  // size of file
198  } else {
199  value_used = 0;
200  }
201  }
202 
203  if (!strcmp(key, "Version")) {
204  if (sscanf(value, "Vivo/%d.", &vivo->version) != 1)
205  return AVERROR_INVALIDDATA;
206  value_used = 1;
207  } else if (!strcmp(key, "FPS")) {
208  double d;
209  if (av_sscanf(value, "%f", &d) != 1)
210  return AVERROR_INVALIDDATA;
211 
212  value_used = 1;
213  if (!fps.num && !fps.den)
214  fps = av_inv_q(av_d2q(d, 10000));
215  }
216 
217  if (!value_used)
218  av_dict_set(&s->metadata, key, value, 0);
219  }
220  }
221  if (!fps.num || !fps.den)
222  fps = (AVRational){ 1, 25 };
223 
224  avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
225  avpriv_set_pts_info(vst, 64, fps.num, fps.den);
226  if (duration)
227  s->duration = av_rescale(duration, 1000, 1);
228 
229  vst->start_time = 0;
230  vst->codecpar->codec_tag = 0;
232 
233  if (vivo->version == 1) {
237  ast->codecpar->block_align = 24;
238  ast->codecpar->bit_rate = 6400;
239  }
240 
241  ast->start_time = 0;
242  ast->codecpar->codec_tag = 0;
244  ast->codecpar->channels = 1;
245 
246  return 0;
247 }
248 
250 {
251  VivoContext *vivo = s->priv_data;
252  AVIOContext *pb = s->pb;
253  unsigned old_sequence = vivo->sequence, old_type = vivo->type;
254  int stream_index, ret = 0;
255 
256 restart:
257 
258  if (avio_feof(pb))
259  return AVERROR_EOF;
260 
261  switch (vivo->type) {
262  case 0:
263  avio_skip(pb, vivo->length);
264  if ((ret = vivo_get_packet_header(s)) < 0)
265  return ret;
266  goto restart;
267  case 1:
268  case 2: // video
269  stream_index = 0;
270  break;
271  case 3:
272  case 4: // audio
273  stream_index = 1;
274  break;
275  default:
276  av_log(s, AV_LOG_ERROR, "unknown packet type %d\n", vivo->type);
277  return AVERROR_INVALIDDATA;
278  }
279 
280  if ((ret = av_get_packet(pb, pkt, vivo->length)) < 0)
281  goto fail;
282 
283  // get next packet header
284  if ((ret = vivo_get_packet_header(s)) < 0)
285  goto fail;
286 
287  while (vivo->sequence == old_sequence &&
288  (((vivo->type - 1) >> 1) == ((old_type - 1) >> 1))) {
289  if (avio_feof(pb)) {
290  ret = AVERROR_EOF;
291  break;
292  }
293 
294  if ((ret = av_append_packet(pb, pkt, vivo->length)) < 0)
295  break;
296 
297  // get next packet header
298  if ((ret = vivo_get_packet_header(s)) < 0)
299  break;
300  }
301 
302  pkt->stream_index = stream_index;
303 
304 fail:
305  if (ret < 0)
307  return ret;
308 }
309 
311  .name = "vivo",
312  .long_name = NULL_IF_CONFIG_SMALL("Vivo"),
313  .priv_data_size = sizeof(VivoContext),
317  .extensions = "viv",
318 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
VivoContext
Definition: vivo.c:34
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
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4480
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3953
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3961
VivoContext::version
int version
Definition: vivo.c:35
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
vivo_get_packet_header
static int vivo_get_packet_header(AVFormatContext *s)
Definition: vivo.c:73
AVCodecParameters::channels
int channels
Audio only.
Definition: avcodec.h:4063
VivoContext::type
int type
Definition: vivo.c:37
fail
#define fail()
Definition: checkasm.h:120
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
buf
void * buf
Definition: avisynth_c.h:766
AVInputFormat
Definition: avformat.h:640
duration
int64_t duration
Definition: movenc.c:63
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: avcodec.h:4023
vivo_probe
static int vivo_probe(const AVProbeData *p)
Definition: vivo.c:44
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
vivo_read_packet
static int vivo_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: vivo.c:249
key
const char * key
Definition: hwcontext_opencl.c:168
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
vivo_read_header
static int vivo_read_header(AVFormatContext *s)
Definition: vivo.c:119
av_sscanf
int av_sscanf(const char *string, const char *format,...)
See libc sscanf manual for more information.
Definition: avsscanf.c:962
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
VivoContext::length
int length
Definition: vivo.c:39
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AV_CODEC_ID_G723_1
@ AV_CODEC_ID_G723_1
Definition: avcodec.h:616
parseutils.h
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
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: avcodec.h:4067
VivoContext::text
uint8_t text[1024+1]
Definition: vivo.c:41
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
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:188
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4910
AV_CODEC_ID_H263
@ AV_CODEC_ID_H263
Definition: avcodec.h:222
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:638
line
Definition: graph2dot.c:48
ff_vivo_demuxer
AVInputFormat ff_vivo_demuxer
Definition: vivo.c:310
AVCodecParameters::height
int height
Definition: avcodec.h:4024
AVCodecParameters::block_align
int block_align
Audio only.
Definition: avcodec.h:4074
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
uint8_t
uint8_t
Definition: audio_convert.c:194
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
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:313
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:870
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:323
avformat.h
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
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:647
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1479
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3999
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
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:70
VivoContext::sequence
int sequence
Definition: vivo.c:38
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3986
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
length
const char int length
Definition: avisynth_c.h:860
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:909
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
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:358