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 "libavutil/mem.h"
33 #include "avformat.h"
34 #include "demux.h"
35 #include "internal.h"
36 #include "avio_internal.h"
37 
38 #define VMD_HEADER_SIZE 0x0330
39 #define BYTES_PER_FRAME_RECORD 16
40 
41 typedef struct vmd_frame {
43  unsigned int frame_size;
47 } vmd_frame;
48 
49 typedef struct VmdDemuxContext {
52 
53  unsigned int frame_count;
54  unsigned int frames_per_block;
56  unsigned int current_frame;
57  int is_indeo3;
58 
61  int skiphdr;
62 
63  unsigned char vmd_header[VMD_HEADER_SIZE];
65 
66 static int vmd_probe(const AVProbeData *p)
67 {
68  int w, h, sample_rate;
69  if (p->buf_size < 806)
70  return 0;
71  /* check if the first 2 bytes of the file contain the appropriate size
72  * of a VMD header chunk */
73  if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
74  return 0;
75  w = AV_RL16(&p->buf[12]);
76  h = AV_RL16(&p->buf[14]);
77  sample_rate = AV_RL16(&p->buf[804]);
78  if ((!w || w > 2048 || !h || h > 2048) &&
79  sample_rate != 22050)
80  return 0;
81 
82  /* only return half certainty since this check is a bit sketchy */
84 }
85 
87 {
88  VmdDemuxContext *vmd = s->priv_data;
89  AVIOContext *pb = s->pb;
90  AVStream *st = NULL, *vst = NULL;
91  unsigned int toc_offset;
92  unsigned char *raw_frame_table;
93  int raw_frame_table_size;
94  int64_t current_offset;
95  int i, j, ret;
96  int width, height;
97  unsigned int total_frames;
98  int64_t current_audio_pts = 0;
99  unsigned char chunk[BYTES_PER_FRAME_RECORD];
100  int num, den;
101  int sound_buffers;
102 
103  /* fetch the main header, including the 2 header length bytes */
104  avio_seek(pb, 0, SEEK_SET);
106  return AVERROR(EIO);
107 
108  width = AV_RL16(&vmd->vmd_header[12]);
109  height = AV_RL16(&vmd->vmd_header[14]);
110  if (width && height) {
111  if(vmd->vmd_header[24] == 'i' && vmd->vmd_header[25] == 'v' && vmd->vmd_header[26] == '3') {
112  vmd->is_indeo3 = 1;
113  } else {
114  vmd->is_indeo3 = 0;
115  }
116  /* start up the decoders */
117  vst = avformat_new_stream(s, NULL);
118  if (!vst)
119  return AVERROR(ENOMEM);
120  avpriv_set_pts_info(vst, 33, 1, 10);
121  vmd->video_stream_index = vst->index;
122  vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
123  vst->codecpar->codec_id = vmd->is_indeo3 ? AV_CODEC_ID_INDEO3 : AV_CODEC_ID_VMDVIDEO;
124  vst->codecpar->codec_tag = 0; /* no fourcc */
125  vst->codecpar->width = width;
126  vst->codecpar->height = height;
127  if(vmd->is_indeo3 && vst->codecpar->width > 320){
128  vst->codecpar->width >>= 1;
129  vst->codecpar->height >>= 1;
130  }
131  if ((ret = ff_alloc_extradata(vst->codecpar, VMD_HEADER_SIZE)) < 0)
132  return ret;
133  memcpy(vst->codecpar->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
134  }
135 
136  /* if sample rate is 0, assume no audio */
137  vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
138  if (vmd->sample_rate) {
139  int channels;
140  st = avformat_new_stream(s, NULL);
141  if (!st)
142  return AVERROR(ENOMEM);
143  vmd->audio_stream_index = st->index;
146  st->codecpar->codec_tag = 0; /* no fourcc */
147  st->codecpar->sample_rate = vmd->sample_rate;
148  st->codecpar->block_align = AV_RL16(&vmd->vmd_header[806]);
149  if (st->codecpar->block_align & 0x8000) {
151  st->codecpar->block_align = -(st->codecpar->block_align - 0x10000);
152  } else {
154  }
155  if (vmd->vmd_header[811] & 0x80) {
156  channels = 2;
157  } else if (vmd->vmd_header[811] & 0x2) {
158  /* Shivers 2 stereo audio */
159  /* Frame length is for 1 channel */
160  channels = 2;
161  st->codecpar->block_align = st->codecpar->block_align << 1;
162  } else {
163  channels = 1;
164  }
166  st->codecpar->bit_rate = st->codecpar->sample_rate *
168 
169  /* calculate pts */
170  num = st->codecpar->block_align;
171  den = st->codecpar->sample_rate * channels;
172  av_reduce(&num, &den, num, den, (1UL<<31)-1);
173  if (vst)
174  avpriv_set_pts_info(vst, 33, num, den);
175  avpriv_set_pts_info(st, 33, num, den);
176  }
177  if (!s->nb_streams)
178  return AVERROR_INVALIDDATA;
179 
180  toc_offset = AV_RL32(&vmd->vmd_header[812]);
181  vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
182  vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
183  avio_seek(pb, toc_offset, SEEK_SET);
184 
185  raw_frame_table = NULL;
186  vmd->frame_table = NULL;
187  sound_buffers = AV_RL16(&vmd->vmd_header[808]);
188  raw_frame_table_size = vmd->frame_count * 6;
189  raw_frame_table = av_malloc(raw_frame_table_size);
190  vmd->frame_table = av_malloc_array(vmd->frame_count * vmd->frames_per_block + sound_buffers, sizeof(vmd_frame));
191  if (!raw_frame_table || !vmd->frame_table) {
192  ret = AVERROR(ENOMEM);
193  goto error;
194  }
195  if (avio_read(pb, raw_frame_table, raw_frame_table_size) !=
196  raw_frame_table_size) {
197  ret = AVERROR(EIO);
198  goto error;
199  }
200 
201  total_frames = 0;
202  for (i = 0; i < vmd->frame_count; i++) {
203 
204  current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
205 
206  /* handle each entry in index block */
207  for (j = 0; j < vmd->frames_per_block; j++) {
208  int type;
209  uint32_t size;
210 
212  av_log(s, AV_LOG_ERROR, "Failed to read frame record\n");
213  if (ret >= 0)
215  goto error;
216  }
217  type = chunk[0];
218  size = AV_RL32(&chunk[2]);
219  if (size > INT_MAX / 2) {
220  av_log(s, AV_LOG_ERROR, "Invalid frame size\n");
222  goto error;
223  }
224  if(!size && type != 1)
225  continue;
226  switch(type) {
227  case 1: /* Audio Chunk */
228  if (!st) break;
229  /* first audio chunk contains several audio buffers */
230  vmd->frame_table[total_frames].frame_offset = current_offset;
231  vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
232  vmd->frame_table[total_frames].frame_size = size;
233  memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
234  vmd->frame_table[total_frames].pts = current_audio_pts;
235  total_frames++;
236  if(!current_audio_pts)
237  current_audio_pts += sound_buffers - 1;
238  else
239  current_audio_pts++;
240  break;
241  case 2: /* Video Chunk */
242  if (!vst)
243  break;
244  vmd->frame_table[total_frames].frame_offset = current_offset;
245  vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
246  vmd->frame_table[total_frames].frame_size = size;
247  memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
248  vmd->frame_table[total_frames].pts = i;
249  total_frames++;
250  break;
251  }
252  current_offset += size;
253  }
254  }
255 
256 
257  vmd->current_frame = 0;
258  vmd->frame_count = total_frames;
259 
260  ret = 0;
261 error:
262  av_freep(&raw_frame_table);
263  return ret;
264 }
265 
267  AVPacket *pkt)
268 {
269  VmdDemuxContext *vmd = s->priv_data;
270  AVIOContext *pb = s->pb;
271  int ret = 0;
272  vmd_frame *frame;
273 
274  if (vmd->current_frame >= vmd->frame_count)
275  return AVERROR_EOF;
276 
277  frame = &vmd->frame_table[vmd->current_frame];
278  /* position the stream (will probably be there already) */
279  avio_seek(pb, frame->frame_offset, SEEK_SET);
280 
281  if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
282  return AVERROR(EIO);
284  if (ret < 0)
285  return ret;
286  pkt->pos= avio_tell(pb);
287  memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
288  if(vmd->is_indeo3 && frame->frame_record[0] == 0x02)
289  ret = avio_read(pb, pkt->data, frame->frame_size);
290  else
292  frame->frame_size);
293 
294  if (ret != frame->frame_size) {
295  ret = AVERROR(EIO);
296  }
297  pkt->stream_index = frame->stream_index;
298  pkt->pts = frame->pts;
299  av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
300  (frame->frame_record[0] == 0x02) ? "video" : "audio",
301  frame->frame_size + BYTES_PER_FRAME_RECORD,
302  pkt->pts);
303 
304  vmd->current_frame++;
305 
306  return ret;
307 }
308 
310 {
311  VmdDemuxContext *vmd = s->priv_data;
312 
313  av_freep(&vmd->frame_table);
314 
315  return 0;
316 }
317 
319  .p.name = "vmd",
320  .p.long_name = NULL_IF_CONFIG_SMALL("Sierra VMD"),
321  .priv_data_size = sizeof(VmdDemuxContext),
322  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
327 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
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
VMD_HEADER_SIZE
#define VMD_HEADER_SIZE
Definition: sierravmd.c:38
vmd_frame
Definition: sierravmd.c:41
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
vmd_read_close
static int vmd_read_close(AVFormatContext *s)
Definition: sierravmd.c:309
w
uint8_t w
Definition: llviddspenc.c:38
AV_CODEC_ID_INDEO3
@ AV_CODEC_ID_INDEO3
Definition: codec_id.h:80
AVPacket::data
uint8_t * data
Definition: packet.h:539
VmdDemuxContext::frame_count
unsigned int frame_count
Definition: sierravmd.c:53
VmdDemuxContext::frames_per_block
unsigned int frames_per_block
Definition: sierravmd.c:54
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
ff_vmd_demuxer
const FFInputFormat ff_vmd_demuxer
Definition: sierravmd.c:318
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
vmd_frame::stream_index
int stream_index
Definition: sierravmd.c:42
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
type
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 type
Definition: writing_filters.txt:86
av_reduce
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
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
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
VmdDemuxContext::is_indeo3
int is_indeo3
Definition: sierravmd.c:57
VmdDemuxContext::current_frame
unsigned int current_frame
Definition: sierravmd.c:56
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: packet.c:98
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
AV_CODEC_ID_VMDAUDIO
@ AV_CODEC_ID_VMDAUDIO
Definition: codec_id.h:457
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
channels
channels
Definition: aptx.h:31
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
VmdDemuxContext::frame_table
vmd_frame * frame_table
Definition: sierravmd.c:55
vmd_probe
static int vmd_probe(const AVProbeData *p)
Definition: sierravmd.c:66
VmdDemuxContext::vmd_header
unsigned char vmd_header[VMD_HEADER_SIZE]
Definition: sierravmd.c:63
FF_INFMT_FLAG_INIT_CLEANUP
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: demux.h:35
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
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
NULL
#define NULL
Definition: coverity.c:32
VmdDemuxContext::skiphdr
int skiphdr
Definition: sierravmd.c:61
VmdDemuxContext::video_stream_index
int video_stream_index
Definition: sierravmd.c:50
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
AV_CODEC_ID_VMDVIDEO
@ AV_CODEC_ID_VMDVIDEO
Definition: codec_id.h:104
VmdDemuxContext::sample_rate
int sample_rate
Definition: sierravmd.c:59
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:461
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
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
height
#define height
Definition: dsp.h:85
vmd_read_header
static int vmd_read_header(AVFormatContext *s)
Definition: sierravmd.c:86
size
int size
Definition: twinvq_data.h:10344
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
ffio_limit
int ffio_limit(AVIOContext *s, int size)
Definition: aviobuf.c:1061
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:837
vmd_frame::pts
int64_t pts
Definition: sierravmd.c:45
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
avio_internal.h
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
vmd_read_packet
static int vmd_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: sierravmd.c:266
demux.h
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
frame
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
Definition: filter_design.txt:264
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
VmdDemuxContext
Definition: sierravmd.c:49
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:754
channel_layout.h
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
BYTES_PER_FRAME_RECORD
#define BYTES_PER_FRAME_RECORD
Definition: sierravmd.c:39
VmdDemuxContext::audio_stream_index
int audio_stream_index
Definition: sierravmd.c:51
AVPacket::stream_index
int stream_index
Definition: packet.h:541
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
vmd_frame::frame_size
unsigned int frame_size
Definition: sierravmd.c:43
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
mem.h
VmdDemuxContext::audio_sample_counter
int64_t audio_sample_counter
Definition: sierravmd.c:60
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_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
FFInputFormat
Definition: demux.h:42
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
h
h
Definition: vp9dsp_template.c:2070
vmd_frame::frame_offset
int64_t frame_offset
Definition: sierravmd.c:44
width
#define width
Definition: dsp.h:85
vmd_frame::frame_record
unsigned char frame_record[BYTES_PER_FRAME_RECORD]
Definition: sierravmd.c:46
ff_alloc_extradata
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:227