FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  int64_t frame_offset;
42  unsigned int frame_size;
43  int64_t pts;
44  int keyframe;
46 } vmd_frame;
47 
48 typedef struct VmdDemuxContext {
51 
52  unsigned int frame_count;
53  unsigned int frames_per_block;
55  unsigned int current_frame;
56  int is_indeo3;
57 
60  int skiphdr;
61 
62  unsigned char vmd_header[VMD_HEADER_SIZE];
64 
65 static int vmd_probe(AVProbeData *p)
66 {
67  int w, h, sample_rate;
68  if (p->buf_size < 806)
69  return 0;
70  /* check if the first 2 bytes of the file contain the appropriate size
71  * of a VMD header chunk */
72  if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
73  return 0;
74  w = AV_RL16(&p->buf[12]);
75  h = AV_RL16(&p->buf[14]);
76  sample_rate = AV_RL16(&p->buf[804]);
77  if ((!w || w > 2048 || !h || h > 2048) &&
78  sample_rate != 22050)
79  return 0;
80 
81  /* only return half certainty since this check is a bit sketchy */
83 }
84 
86 {
87  VmdDemuxContext *vmd = s->priv_data;
88  AVIOContext *pb = s->pb;
89  AVStream *st = NULL, *vst = NULL;
90  unsigned int toc_offset;
91  unsigned char *raw_frame_table;
92  int raw_frame_table_size;
93  int64_t current_offset;
94  int i, j, ret;
95  int width, height;
96  unsigned int total_frames;
97  int64_t current_audio_pts = 0;
98  unsigned char chunk[BYTES_PER_FRAME_RECORD];
99  int num, den;
100  int sound_buffers;
101 
102  /* fetch the main header, including the 2 header length bytes */
103  avio_seek(pb, 0, SEEK_SET);
105  return AVERROR(EIO);
106 
107  width = AV_RL16(&vmd->vmd_header[12]);
108  height = AV_RL16(&vmd->vmd_header[14]);
109  if (width && height) {
110  if(vmd->vmd_header[24] == 'i' && vmd->vmd_header[25] == 'v' && vmd->vmd_header[26] == '3') {
111  vmd->is_indeo3 = 1;
112  } else {
113  vmd->is_indeo3 = 0;
114  }
115  /* start up the decoders */
116  vst = avformat_new_stream(s, NULL);
117  if (!vst)
118  return AVERROR(ENOMEM);
119  avpriv_set_pts_info(vst, 33, 1, 10);
120  vmd->video_stream_index = vst->index;
121  vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
122  vst->codecpar->codec_id = vmd->is_indeo3 ? AV_CODEC_ID_INDEO3 : AV_CODEC_ID_VMDVIDEO;
123  vst->codecpar->codec_tag = 0; /* no fourcc */
124  vst->codecpar->width = width;
125  vst->codecpar->height = height;
126  if(vmd->is_indeo3 && vst->codecpar->width > 320){
127  vst->codecpar->width >>= 1;
128  vst->codecpar->height >>= 1;
129  }
130  if (ff_alloc_extradata(vst->codecpar, VMD_HEADER_SIZE))
131  return AVERROR(ENOMEM);
132  memcpy(vst->codecpar->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
133  }
134 
135  /* if sample rate is 0, assume no audio */
136  vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
137  if (vmd->sample_rate) {
138  st = avformat_new_stream(s, NULL);
139  if (!st)
140  return AVERROR(ENOMEM);
141  vmd->audio_stream_index = st->index;
144  st->codecpar->codec_tag = 0; /* no fourcc */
145  st->codecpar->sample_rate = vmd->sample_rate;
146  st->codecpar->block_align = AV_RL16(&vmd->vmd_header[806]);
147  if (st->codecpar->block_align & 0x8000) {
149  st->codecpar->block_align = -(st->codecpar->block_align - 0x10000);
150  } else {
152  }
153  if (vmd->vmd_header[811] & 0x80) {
154  st->codecpar->channels = 2;
156  } else if (vmd->vmd_header[811] & 0x2) {
157  /* Shivers 2 stereo audio */
158  /* Frame length is for 1 channel */
159  st->codecpar->channels = 2;
161  st->codecpar->block_align = st->codecpar->block_align << 1;
162  } else {
163  st->codecpar->channels = 1;
165  }
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 * st->codecpar->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 
178  toc_offset = AV_RL32(&vmd->vmd_header[812]);
179  vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
180  vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
181  avio_seek(pb, toc_offset, SEEK_SET);
182 
183  raw_frame_table = NULL;
184  vmd->frame_table = NULL;
185  sound_buffers = AV_RL16(&vmd->vmd_header[808]);
186  raw_frame_table_size = vmd->frame_count * 6;
187  if(vmd->frame_count * vmd->frames_per_block >= UINT_MAX / sizeof(vmd_frame) - sound_buffers){
188  av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
189  return -1;
190  }
191  raw_frame_table = av_malloc(raw_frame_table_size);
192  vmd->frame_table = av_malloc_array(vmd->frame_count * vmd->frames_per_block + sound_buffers, sizeof(vmd_frame));
193  if (!raw_frame_table || !vmd->frame_table) {
194  ret = AVERROR(ENOMEM);
195  goto error;
196  }
197  if (avio_read(pb, raw_frame_table, raw_frame_table_size) !=
198  raw_frame_table_size) {
199  ret = AVERROR(EIO);
200  goto error;
201  }
202 
203  total_frames = 0;
204  for (i = 0; i < vmd->frame_count; i++) {
205 
206  current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
207 
208  /* handle each entry in index block */
209  for (j = 0; j < vmd->frames_per_block; j++) {
210  int type;
211  uint32_t size;
212 
213  if ((ret = avio_read(pb, chunk, BYTES_PER_FRAME_RECORD)) != BYTES_PER_FRAME_RECORD) {
214  av_log(s, AV_LOG_ERROR, "Failed to read frame record\n");
215  if (ret >= 0)
216  ret = AVERROR_INVALIDDATA;
217  goto error;
218  }
219  type = chunk[0];
220  size = AV_RL32(&chunk[2]);
221  if (size > INT_MAX / 2) {
222  av_log(s, AV_LOG_ERROR, "Invalid frame size\n");
223  ret = AVERROR_INVALIDDATA;
224  goto error;
225  }
226  if(!size && type != 1)
227  continue;
228  switch(type) {
229  case 1: /* Audio Chunk */
230  if (!st) break;
231  /* first audio chunk contains several audio buffers */
232  vmd->frame_table[total_frames].frame_offset = current_offset;
233  vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
234  vmd->frame_table[total_frames].frame_size = size;
235  memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
236  vmd->frame_table[total_frames].pts = current_audio_pts;
237  total_frames++;
238  if(!current_audio_pts)
239  current_audio_pts += sound_buffers - 1;
240  else
241  current_audio_pts++;
242  break;
243  case 2: /* Video Chunk */
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  av_free(raw_frame_table);
257 
258  vmd->current_frame = 0;
259  vmd->frame_count = total_frames;
260 
261  return 0;
262 
263 error:
264  av_freep(&raw_frame_table);
265  av_freep(&vmd->frame_table);
266  return ret;
267 }
268 
270  AVPacket *pkt)
271 {
272  VmdDemuxContext *vmd = s->priv_data;
273  AVIOContext *pb = s->pb;
274  int ret = 0;
275  vmd_frame *frame;
276 
277  if (vmd->current_frame >= vmd->frame_count)
278  return AVERROR_EOF;
279 
280  frame = &vmd->frame_table[vmd->current_frame];
281  /* position the stream (will probably be there already) */
282  avio_seek(pb, frame->frame_offset, SEEK_SET);
283 
284  if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
285  return AVERROR(EIO);
287  return AVERROR(ENOMEM);
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  av_packet_unref(pkt);
298  ret = AVERROR(EIO);
299  }
300  pkt->stream_index = frame->stream_index;
301  pkt->pts = frame->pts;
302  av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
303  (frame->frame_record[0] == 0x02) ? "video" : "audio",
305  pkt->pts);
306 
307  vmd->current_frame++;
308 
309  return ret;
310 }
311 
313 {
314  VmdDemuxContext *vmd = s->priv_data;
315 
316  av_freep(&vmd->frame_table);
317 
318  return 0;
319 }
320 
322  .name = "vmd",
323  .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD"),
324  .priv_data_size = sizeof(VmdDemuxContext),
329 };
unsigned int frame_count
Definition: sierravmd.c:52
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:155
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1677
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:4601
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4066
int index
stream index in AVFormatContext
Definition: avformat.h:890
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
int64_t audio_sample_counter
Definition: sierravmd.c:59
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
unsigned int frames_per_block
Definition: sierravmd.c:53
unsigned int current_frame
Definition: sierravmd.c:55
Format I/O context.
Definition: avformat.h:1349
static int vmd_read_close(AVFormatContext *s)
Definition: sierravmd.c:312
int audio_stream_index
Definition: sierravmd.c:50
int ffio_limit(AVIOContext *s, int size)
Definition: utils.c:226
int keyframe
Definition: sierravmd.c:44
#define av_malloc(s)
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4231
static AVFrame * frame
int stream_index
Definition: sierravmd.c:40
#define height
uint8_t * data
Definition: avcodec.h:1657
#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:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:525
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: avcodec.h:4168
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:616
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:4095
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#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:179
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4062
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:464
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
int block_align
Audio only.
Definition: avcodec.h:4183
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:3213
#define width
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
AVInputFormat ff_vmd_demuxer
Definition: sierravmd.c:321
#define BYTES_PER_FRAME_RECORD
Definition: sierravmd.c:37
static void error(const char *err)
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:510
Stream structure.
Definition: avformat.h:889
static int vmd_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: sierravmd.c:269
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
int video_stream_index
Definition: sierravmd.c:49
sample_rate
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
vmd_frame * frame_table
Definition: sierravmd.c:54
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:589
static int vmd_read_header(AVFormatContext *s)
Definition: sierravmd.c:85
GLint GLenum type
Definition: opengl_enc.c:105
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:471
int64_t pts
Definition: sierravmd.c:43
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
unsigned char vmd_header[VMD_HEADER_SIZE]
Definition: sierravmd.c:62
int64_t frame_offset
Definition: sierravmd.c:41
int sample_rate
Audio only.
Definition: avcodec.h:4176
Main libavformat public API header.
unsigned char frame_record[BYTES_PER_FRAME_RECORD]
Definition: sierravmd.c:45
#define av_free(p)
void * priv_data
Format private data.
Definition: avformat.h:1377
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:4108
int channels
Audio only.
Definition: avcodec.h:4172
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
unsigned int frame_size
Definition: sierravmd.c:42
#define VMD_HEADER_SIZE
Definition: sierravmd.c:36
AVCodecParameters * codecpar
Definition: avformat.h:1252
#define av_malloc_array(a, b)
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:4070
static int vmd_probe(AVProbeData *p)
Definition: sierravmd.c:65
int stream_index
Definition: avcodec.h:1659
#define AV_CH_LAYOUT_MONO
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1634
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1650