FFmpeg
sierravmd.c
Go to the documentation of this file.
1 /*
2  * Sierra VMD Format Demuxer
3  * Copyright (c) 2004 The FFmpeg project
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  * Sierra VMD file demuxer
25  * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
26  * for more information on the Sierra VMD file format, visit:
27  * http://www.pcisys.net/~melanson/codecs/
28  */
29 
31 #include "libavutil/intreadwrite.h"
32 #include "avformat.h"
33 #include "internal.h"
34 #include "avio_internal.h"
35 
36 #define VMD_HEADER_SIZE 0x0330
37 #define BYTES_PER_FRAME_RECORD 16
38 
39 typedef struct vmd_frame {
41  unsigned int frame_size;
42  int64_t frame_offset;
43  int64_t pts;
45 } vmd_frame;
46 
47 typedef struct VmdDemuxContext {
50 
51  unsigned int frame_count;
52  unsigned int frames_per_block;
54  unsigned int current_frame;
55  int is_indeo3;
56 
59  int skiphdr;
60 
61  unsigned char vmd_header[VMD_HEADER_SIZE];
63 
64 static int vmd_probe(const AVProbeData *p)
65 {
66  int w, h, sample_rate;
67  if (p->buf_size < 806)
68  return 0;
69  /* check if the first 2 bytes of the file contain the appropriate size
70  * of a VMD header chunk */
71  if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
72  return 0;
73  w = AV_RL16(&p->buf[12]);
74  h = AV_RL16(&p->buf[14]);
75  sample_rate = AV_RL16(&p->buf[804]);
76  if ((!w || w > 2048 || !h || h > 2048) &&
77  sample_rate != 22050)
78  return 0;
79 
80  /* only return half certainty since this check is a bit sketchy */
82 }
83 
85 {
86  VmdDemuxContext *vmd = s->priv_data;
87  AVIOContext *pb = s->pb;
88  AVStream *st = NULL, *vst = NULL;
89  unsigned int toc_offset;
90  unsigned char *raw_frame_table;
91  int raw_frame_table_size;
92  int64_t current_offset;
93  int i, j, ret;
94  int width, height;
95  unsigned int total_frames;
96  int64_t current_audio_pts = 0;
97  unsigned char chunk[BYTES_PER_FRAME_RECORD];
98  int num, den;
99  int sound_buffers;
100 
101  /* fetch the main header, including the 2 header length bytes */
102  avio_seek(pb, 0, SEEK_SET);
104  return AVERROR(EIO);
105 
106  width = AV_RL16(&vmd->vmd_header[12]);
107  height = AV_RL16(&vmd->vmd_header[14]);
108  if (width && height) {
109  if(vmd->vmd_header[24] == 'i' && vmd->vmd_header[25] == 'v' && vmd->vmd_header[26] == '3') {
110  vmd->is_indeo3 = 1;
111  } else {
112  vmd->is_indeo3 = 0;
113  }
114  /* start up the decoders */
115  vst = avformat_new_stream(s, NULL);
116  if (!vst)
117  return AVERROR(ENOMEM);
118  avpriv_set_pts_info(vst, 33, 1, 10);
119  vmd->video_stream_index = vst->index;
120  vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
121  vst->codecpar->codec_id = vmd->is_indeo3 ? AV_CODEC_ID_INDEO3 : AV_CODEC_ID_VMDVIDEO;
122  vst->codecpar->codec_tag = 0; /* no fourcc */
123  vst->codecpar->width = width;
124  vst->codecpar->height = height;
125  if(vmd->is_indeo3 && vst->codecpar->width > 320){
126  vst->codecpar->width >>= 1;
127  vst->codecpar->height >>= 1;
128  }
129  if ((ret = ff_alloc_extradata(vst->codecpar, VMD_HEADER_SIZE)) < 0)
130  return ret;
131  memcpy(vst->codecpar->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
132  }
133 
134  /* if sample rate is 0, assume no audio */
135  vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
136  if (vmd->sample_rate) {
137  st = avformat_new_stream(s, NULL);
138  if (!st)
139  return AVERROR(ENOMEM);
140  vmd->audio_stream_index = st->index;
143  st->codecpar->codec_tag = 0; /* no fourcc */
144  st->codecpar->sample_rate = vmd->sample_rate;
145  st->codecpar->block_align = AV_RL16(&vmd->vmd_header[806]);
146  if (st->codecpar->block_align & 0x8000) {
148  st->codecpar->block_align = -(st->codecpar->block_align - 0x10000);
149  } else {
151  }
152  if (vmd->vmd_header[811] & 0x80) {
153  st->codecpar->channels = 2;
155  } else if (vmd->vmd_header[811] & 0x2) {
156  /* Shivers 2 stereo audio */
157  /* Frame length is for 1 channel */
158  st->codecpar->channels = 2;
160  st->codecpar->block_align = st->codecpar->block_align << 1;
161  } else {
162  st->codecpar->channels = 1;
164  }
165  st->codecpar->bit_rate = st->codecpar->sample_rate *
167 
168  /* calculate pts */
169  num = st->codecpar->block_align;
170  den = st->codecpar->sample_rate * st->codecpar->channels;
171  av_reduce(&num, &den, num, den, (1UL<<31)-1);
172  if (vst)
173  avpriv_set_pts_info(vst, 33, num, den);
174  avpriv_set_pts_info(st, 33, num, den);
175  }
176  if (!s->nb_streams)
177  return AVERROR_INVALIDDATA;
178 
179  toc_offset = AV_RL32(&vmd->vmd_header[812]);
180  vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
181  vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
182  avio_seek(pb, toc_offset, SEEK_SET);
183 
184  raw_frame_table = NULL;
185  vmd->frame_table = NULL;
186  sound_buffers = AV_RL16(&vmd->vmd_header[808]);
187  raw_frame_table_size = vmd->frame_count * 6;
188  raw_frame_table = av_malloc(raw_frame_table_size);
189  vmd->frame_table = av_malloc_array(vmd->frame_count * vmd->frames_per_block + sound_buffers, sizeof(vmd_frame));
190  if (!raw_frame_table || !vmd->frame_table) {
191  ret = AVERROR(ENOMEM);
192  goto error;
193  }
194  if (avio_read(pb, raw_frame_table, raw_frame_table_size) !=
195  raw_frame_table_size) {
196  ret = AVERROR(EIO);
197  goto error;
198  }
199 
200  total_frames = 0;
201  for (i = 0; i < vmd->frame_count; i++) {
202 
203  current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
204 
205  /* handle each entry in index block */
206  for (j = 0; j < vmd->frames_per_block; j++) {
207  int type;
208  uint32_t size;
209 
210  if ((ret = avio_read(pb, chunk, BYTES_PER_FRAME_RECORD)) != BYTES_PER_FRAME_RECORD) {
211  av_log(s, AV_LOG_ERROR, "Failed to read frame record\n");
212  if (ret >= 0)
213  ret = AVERROR_INVALIDDATA;
214  goto error;
215  }
216  type = chunk[0];
217  size = AV_RL32(&chunk[2]);
218  if (size > INT_MAX / 2) {
219  av_log(s, AV_LOG_ERROR, "Invalid frame size\n");
220  ret = AVERROR_INVALIDDATA;
221  goto error;
222  }
223  if(!size && type != 1)
224  continue;
225  switch(type) {
226  case 1: /* Audio Chunk */
227  if (!st) break;
228  /* first audio chunk contains several audio buffers */
229  vmd->frame_table[total_frames].frame_offset = current_offset;
230  vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
231  vmd->frame_table[total_frames].frame_size = size;
232  memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
233  vmd->frame_table[total_frames].pts = current_audio_pts;
234  total_frames++;
235  if(!current_audio_pts)
236  current_audio_pts += sound_buffers - 1;
237  else
238  current_audio_pts++;
239  break;
240  case 2: /* Video Chunk */
241  if (!vst)
242  break;
243  vmd->frame_table[total_frames].frame_offset = current_offset;
244  vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
245  vmd->frame_table[total_frames].frame_size = size;
246  memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
247  vmd->frame_table[total_frames].pts = i;
248  total_frames++;
249  break;
250  }
251  current_offset += size;
252  }
253  }
254 
255  av_free(raw_frame_table);
256 
257  vmd->current_frame = 0;
258  vmd->frame_count = total_frames;
259 
260  return 0;
261 
262 error:
263  av_freep(&raw_frame_table);
264  av_freep(&vmd->frame_table);
265  return ret;
266 }
267 
269  AVPacket *pkt)
270 {
271  VmdDemuxContext *vmd = s->priv_data;
272  AVIOContext *pb = s->pb;
273  int ret = 0;
274  vmd_frame *frame;
275 
276  if (vmd->current_frame >= vmd->frame_count)
277  return AVERROR_EOF;
278 
279  frame = &vmd->frame_table[vmd->current_frame];
280  /* position the stream (will probably be there already) */
281  avio_seek(pb, frame->frame_offset, SEEK_SET);
282 
283  if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
284  return AVERROR(EIO);
285  ret = av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD);
286  if (ret < 0)
287  return ret;
288  pkt->pos= avio_tell(pb);
289  memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
290  if(vmd->is_indeo3 && frame->frame_record[0] == 0x02)
291  ret = avio_read(pb, pkt->data, frame->frame_size);
292  else
293  ret = avio_read(pb, pkt->data + BYTES_PER_FRAME_RECORD,
294  frame->frame_size);
295 
296  if (ret != frame->frame_size) {
297  ret = AVERROR(EIO);
298  }
299  pkt->stream_index = frame->stream_index;
300  pkt->pts = frame->pts;
301  av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
302  (frame->frame_record[0] == 0x02) ? "video" : "audio",
304  pkt->pts);
305 
306  vmd->current_frame++;
307 
308  return ret;
309 }
310 
312 {
313  VmdDemuxContext *vmd = s->priv_data;
314 
315  av_freep(&vmd->frame_table);
316 
317  return 0;
318 }
319 
321  .name = "vmd",
322  .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD"),
323  .priv_data_size = sizeof(VmdDemuxContext),
328 };
unsigned int frame_count
Definition: sierravmd.c:51
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
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:4999
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int index
stream index in AVFormatContext
Definition: avformat.h:885
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
GLint GLenum type
Definition: opengl_enc.c:104
int64_t audio_sample_counter
Definition: sierravmd.c:58
#define AV_CH_LAYOUT_STEREO
AVPacket * pkt
Definition: movenc.c:59
static void error(const char *err)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:91
unsigned int frames_per_block
Definition: sierravmd.c:52
unsigned int current_frame
Definition: sierravmd.c:54
Format I/O context.
Definition: avformat.h:1247
static int vmd_read_close(AVFormatContext *s)
Definition: sierravmd.c:311
int audio_stream_index
Definition: sierravmd.c:49
int ffio_limit(AVIOContext *s, int size)
Definition: utils.c:244
#define av_malloc(s)
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4553
int stream_index
Definition: sierravmd.c:40
#define height
uint8_t * data
Definition: packet.h:369
static int vmd_probe(const AVProbeData *p)
Definition: sierravmd.c:64
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
ptrdiff_t size
Definition: opengl_enc.c:100
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:117
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1303
int block_align
Audio only.
Definition: codec_par.h:177
audio channel layout utility functions
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0...
Definition: utils.c:3368
#define width
AVInputFormat ff_vmd_demuxer
Definition: sierravmd.c:320
uint8_t w
Definition: llviddspenc.c:39
#define BYTES_PER_FRAME_RECORD
Definition: sierravmd.c:37
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
Stream structure.
Definition: avformat.h:884
static int vmd_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: sierravmd.c:268
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
int video_stream_index
Definition: sierravmd.c:48
sample_rate
AVIOContext * pb
I/O context.
Definition: avformat.h:1289
vmd_frame * frame_table
Definition: sierravmd.c:53
static int vmd_read_header(AVFormatContext *s)
Definition: sierravmd.c:84
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:451
int64_t pts
Definition: sierravmd.c:43
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
unsigned char vmd_header[VMD_HEADER_SIZE]
Definition: sierravmd.c:61
int64_t frame_offset
Definition: sierravmd.c:42
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
int sample_rate
Audio only.
Definition: codec_par.h:170
Main libavformat public API header.
unsigned char frame_record[BYTES_PER_FRAME_RECORD]
Definition: sierravmd.c:44
#define av_free(p)
void * priv_data
Format private data.
Definition: avformat.h:1275
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
int channels
Audio only.
Definition: codec_par.h:166
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:654
unsigned int frame_size
Definition: sierravmd.c:41
#define VMD_HEADER_SIZE
Definition: sierravmd.c:36
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1049
#define av_malloc_array(a, b)
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
int stream_index
Definition: packet.h:371
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
#define AV_CH_LAYOUT_MONO
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:91
This structure stores compressed data.
Definition: packet.h:346
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int i
Definition: input.c:407