FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/parseutils.h"
30 #include "avformat.h"
31 #include "internal.h"
32 
33 typedef struct VivoContext {
34  int version;
35 
36  int type;
37  int sequence;
38  int length;
39 
40  uint8_t text[1024 + 1];
41 } VivoContext;
42 
43 static int vivo_probe(AVProbeData *p)
44 {
45  const unsigned char *buf = p->buf;
46  unsigned c, length = 0;
47 
48  // stream must start with packet of type 0 and sequence number 0
49  if (*buf++ != 0)
50  return 0;
51 
52  // read at most 2 bytes of coded length
53  c = *buf++;
54  length = c & 0x7F;
55  if (c & 0x80) {
56  c = *buf++;
57  length = (length << 7) | (c & 0x7F);
58  }
59  if (c & 0x80 || length > 1024 || length < 21)
60  return 0;
61 
62  if (memcmp(buf, "\r\nVersion:Vivo/", 15))
63  return 0;
64  buf += 15;
65 
66  if (*buf < '0' && *buf > '2')
67  return 0;
68 
69  return AVPROBE_SCORE_MAX;
70 }
71 
73 {
74  VivoContext *vivo = s->priv_data;
75  AVIOContext *pb = s->pb;
76  unsigned c, get_length = 0;
77 
78  if (avio_feof(pb))
79  return AVERROR_EOF;
80 
81  c = avio_r8(pb);
82  if (c == 0x82) {
83  get_length = 1;
84  c = avio_r8(pb);
85  }
86 
87  vivo->type = c >> 4;
88  vivo->sequence = c & 0xF;
89 
90  switch (vivo->type) {
91  case 0: get_length = 1; break;
92  case 1: vivo->length = 128; break;
93  case 2: get_length = 1; break;
94  case 3: vivo->length = 40; break;
95  case 4: vivo->length = 24; break;
96  default:
97  av_log(s, AV_LOG_ERROR, "unknown packet type %d\n", vivo->type);
98  return AVERROR_INVALIDDATA;
99  }
100 
101  if (get_length) {
102  c = avio_r8(pb);
103  vivo->length = c & 0x7F;
104  if (c & 0x80) {
105  c = avio_r8(pb);
106  vivo->length = (vivo->length << 7) | (c & 0x7F);
107 
108  if (c & 0x80) {
109  av_log(s, AV_LOG_ERROR, "coded length is more than two bytes\n");
110  return AVERROR_INVALIDDATA;
111  }
112  }
113  }
114 
115  return 0;
116 }
117 
119 {
120  VivoContext *vivo = s->priv_data;
121  AVRational fps = { 1, 25};
122  AVStream *ast, *vst;
123  unsigned char *line, *line_end, *key, *value;
124  long value_int;
125  int ret, value_used;
126  int64_t duration = 0;
127  char *end_value;
128 
129  vst = avformat_new_stream(s, NULL);
130  ast = avformat_new_stream(s, NULL);
131  if (!ast || !vst)
132  return AVERROR(ENOMEM);
133 
134  ast->codec->sample_rate = 8000;
135 
136  while (1) {
137  if ((ret = vivo_get_packet_header(s)) < 0)
138  return ret;
139 
140  // done reading all text header packets?
141  if (vivo->sequence || vivo->type)
142  break;
143 
144  if (vivo->length <= 1024) {
145  avio_read(s->pb, vivo->text, vivo->length);
146  vivo->text[vivo->length] = 0;
147  } else {
148  av_log(s, AV_LOG_WARNING, "too big header, skipping\n");
149  avio_skip(s->pb, vivo->length);
150  continue;
151  }
152 
153  line = vivo->text;
154  while (*line) {
155  line_end = strstr(line, "\r\n");
156  if (!line_end)
157  break;
158 
159  *line_end = 0;
160  key = line;
161  line = line_end + 2; // skip \r\n
162 
163  if (line_end == key) // skip blank lines
164  continue;
165 
166  value = strchr(key, ':');
167  if (!value) {
168  av_log(s, AV_LOG_WARNING, "missing colon in key:value pair '%s'\n",
169  value);
170  continue;
171  }
172 
173  *value++ = 0;
174 
175  av_log(s, AV_LOG_DEBUG, "header: '%s' = '%s'\n", key, value);
176 
177  value_int = strtol(value, &end_value, 10);
178  value_used = 0;
179  if (*end_value == 0) { // valid integer
180  av_log(s, AV_LOG_DEBUG, "got a valid integer (%ld)\n", value_int);
181  value_used = 1;
182  if (!strcmp(key, "Duration")) {
183  duration = value_int;
184  } else if (!strcmp(key, "Width")) {
185  vst->codec->width = value_int;
186  } else if (!strcmp(key, "Height")) {
187  vst->codec->height = value_int;
188  } else if (!strcmp(key, "TimeUnitNumerator")) {
189  fps.num = value_int / 1000;
190  } else if (!strcmp(key, "TimeUnitDenominator")) {
191  fps.den = value_int;
192  } else if (!strcmp(key, "SamplingFrequency")) {
193  ast->codec->sample_rate = value_int;
194  } else if (!strcmp(key, "NominalBitrate")) {
195  } else if (!strcmp(key, "Length")) {
196  // size of file
197  } else {
198  value_used = 0;
199  }
200  }
201 
202  if (!strcmp(key, "Version")) {
203  if (sscanf(value, "Vivo/%d.", &vivo->version) != 1)
204  return AVERROR_INVALIDDATA;
205  value_used = 1;
206  } else if (!strcmp(key, "FPS")) {
207  AVRational tmp;
208 
209  value_used = 1;
210  if (!av_parse_ratio(&tmp, value, 10000, AV_LOG_WARNING, s))
211  fps = av_inv_q(tmp);
212  }
213 
214  if (!value_used)
215  av_dict_set(&s->metadata, key, value, 0);
216  }
217  }
218 
219  avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
220  avpriv_set_pts_info(vst, 64, fps.num, fps.den);
221  if (duration)
222  s->duration = av_rescale(duration, 1000, 1);
223 
224  vst->start_time = 0;
225  vst->codec->codec_tag = 0;
227 
228  if (vivo->version == 1) {
231  ast->codec->bits_per_coded_sample = 8;
232  ast->codec->block_align = 24;
233  ast->codec->bit_rate = 6400;
234  }
235 
236  ast->start_time = 0;
237  ast->codec->codec_tag = 0;
239  ast->codec->channels = 1;
240 
241  return 0;
242 }
243 
245 {
246  VivoContext *vivo = s->priv_data;
247  AVIOContext *pb = s->pb;
248  unsigned old_sequence = vivo->sequence, old_type = vivo->type;
249  int stream_index, ret = 0;
250 
251 restart:
252 
253  if (avio_feof(pb))
254  return AVERROR_EOF;
255 
256  switch (vivo->type) {
257  case 0:
258  avio_skip(pb, vivo->length);
259  if ((ret = vivo_get_packet_header(s)) < 0)
260  return ret;
261  goto restart;
262  case 1:
263  case 2: // video
264  stream_index = 0;
265  break;
266  case 3:
267  case 4: // audio
268  stream_index = 1;
269  break;
270  default:
271  av_log(s, AV_LOG_ERROR, "unknown packet type %d\n", vivo->type);
272  return AVERROR_INVALIDDATA;
273  }
274 
275  if ((ret = av_get_packet(pb, pkt, vivo->length)) < 0)
276  goto fail;
277 
278  // get next packet header
279  if ((ret = vivo_get_packet_header(s)) < 0)
280  goto fail;
281 
282  while (vivo->sequence == old_sequence &&
283  (((vivo->type - 1) >> 1) == ((old_type - 1) >> 1))) {
284  if (avio_feof(pb)) {
285  ret = AVERROR_EOF;
286  break;
287  }
288 
289  if ((ret = av_append_packet(pb, pkt, vivo->length)) < 0)
290  break;
291 
292  // get next packet header
293  if ((ret = vivo_get_packet_header(s)) < 0)
294  break;
295  }
296 
297  pkt->stream_index = stream_index;
298 
299 fail:
300  if (ret < 0)
301  av_free_packet(pkt);
302  return ret;
303 }
304 
306  .name = "vivo",
307  .long_name = NULL_IF_CONFIG_SMALL("Vivo"),
308  .priv_data_size = sizeof(VivoContext),
312  .extensions = "viv",
313 };
int av_parse_ratio(AVRational *q, const char *str, int max, int log_offset, void *log_ctx)
Parse str and store the parsed ratio in q.
Definition: parseutils.c:44
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:280
int sequence
Definition: vivo.c:37
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
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:4083
int length
Definition: vivo.c:38
int num
numerator
Definition: rational.h:44
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:277
static AVPacket pkt
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2299
static int vivo_read_header(AVFormatContext *s)
Definition: vivo.c:118
Format I/O context.
Definition: avformat.h:1273
uint8_t
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3749
AVInputFormat ff_vivo_demuxer
Definition: vivo.c:305
#define AVERROR_EOF
End of file.
Definition: error.h:55
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:244
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2996
static int64_t duration
Definition: ffplay.c:326
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:538
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:254
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1485
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
Definition: graph2dot.c:48
GLsizei GLsizei * length
Definition: opengl_enc.c:115
#define fail()
Definition: checkasm.h:57
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:529
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
static int vivo_get_packet_header(AVFormatContext *s)
Definition: vivo.c:72
int bit_rate
the average bitrate
Definition: avcodec.h:1567
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:127
uint8_t text[1024+1]
Definition: vivo.c:40
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1681
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:623
Stream structure.
Definition: avformat.h:842
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
enum AVMediaType codec_type
Definition: avcodec.h:1510
enum AVCodecID codec_id
Definition: avcodec.h:1519
static int vivo_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: vivo.c:244
int sample_rate
samples per second
Definition: avcodec.h:2262
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1534
void * buf
Definition: avisynth_c.h:553
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:69
rational number numerator/denominator
Definition: rational.h:43
int version
Definition: vivo.c:34
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
misc parsing utilities
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:133
static int vivo_probe(AVProbeData *p)
Definition: vivo.c:43
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:894
static double c[64]
int den
denominator
Definition: rational.h:45
int type
Definition: vivo.c:36
int channels
number of audio channels
Definition: avcodec.h:2263
void * priv_data
Format private data.
Definition: avformat.h:1301
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1368
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:628
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:301
int stream_index
Definition: avcodec.h:1425
This structure stores compressed data.
Definition: avcodec.h:1400