FFmpeg  4.4.9
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 {
42  unsigned int frame_size;
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(const 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  /* the header of a file with a video stream carries the 6bit VGA
81  * palette that vmdvideo_decode_init() loads from offset 28 */
82  if (w && h)
83  for (int i = 28; i < 28 + 3 * 256; i++)
84  if (p->buf[i] > 0x3F)
85  return 0;
86 
87  /* only return half certainty since this check is a bit sketchy */
89 }
90 
92 {
93  VmdDemuxContext *vmd = s->priv_data;
94  AVIOContext *pb = s->pb;
95  AVStream *st = NULL, *vst = NULL;
96  unsigned int toc_offset;
97  unsigned char *raw_frame_table;
98  int raw_frame_table_size;
99  int64_t current_offset;
100  int i, j, ret;
101  int width, height;
102  unsigned int total_frames;
103  int64_t current_audio_pts = 0;
104  unsigned char chunk[BYTES_PER_FRAME_RECORD];
105  int num, den;
106  int sound_buffers;
107 
108  /* fetch the main header, including the 2 header length bytes */
109  avio_seek(pb, 0, SEEK_SET);
111  return AVERROR(EIO);
112 
113  width = AV_RL16(&vmd->vmd_header[12]);
114  height = AV_RL16(&vmd->vmd_header[14]);
115  if (width && height) {
116  if(vmd->vmd_header[24] == 'i' && vmd->vmd_header[25] == 'v' && vmd->vmd_header[26] == '3') {
117  vmd->is_indeo3 = 1;
118  } else {
119  vmd->is_indeo3 = 0;
120  }
121  /* start up the decoders */
122  vst = avformat_new_stream(s, NULL);
123  if (!vst)
124  return AVERROR(ENOMEM);
125  avpriv_set_pts_info(vst, 33, 1, 10);
126  vmd->video_stream_index = vst->index;
127  vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
128  vst->codecpar->codec_id = vmd->is_indeo3 ? AV_CODEC_ID_INDEO3 : AV_CODEC_ID_VMDVIDEO;
129  vst->codecpar->codec_tag = 0; /* no fourcc */
130  vst->codecpar->width = width;
131  vst->codecpar->height = height;
132  if(vmd->is_indeo3 && vst->codecpar->width > 320){
133  vst->codecpar->width >>= 1;
134  vst->codecpar->height >>= 1;
135  }
136  if ((ret = ff_alloc_extradata(vst->codecpar, VMD_HEADER_SIZE)) < 0)
137  return ret;
138  memcpy(vst->codecpar->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
139  }
140 
141  /* if sample rate is 0, assume no audio */
142  vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
143  if (vmd->sample_rate) {
144  st = avformat_new_stream(s, NULL);
145  if (!st)
146  return AVERROR(ENOMEM);
147  vmd->audio_stream_index = st->index;
150  st->codecpar->codec_tag = 0; /* no fourcc */
151  st->codecpar->sample_rate = vmd->sample_rate;
152  st->codecpar->block_align = AV_RL16(&vmd->vmd_header[806]);
153  if (st->codecpar->block_align & 0x8000) {
155  st->codecpar->block_align = -(st->codecpar->block_align - 0x10000);
156  } else {
158  }
159  if (vmd->vmd_header[811] & 0x80) {
160  st->codecpar->channels = 2;
162  } else if (vmd->vmd_header[811] & 0x2) {
163  /* Shivers 2 stereo audio */
164  /* Frame length is for 1 channel */
165  st->codecpar->channels = 2;
167  st->codecpar->block_align = st->codecpar->block_align << 1;
168  } else {
169  st->codecpar->channels = 1;
171  }
172  st->codecpar->bit_rate = st->codecpar->sample_rate *
174 
175  /* calculate pts */
176  num = st->codecpar->block_align;
177  den = st->codecpar->sample_rate * st->codecpar->channels;
178  av_reduce(&num, &den, num, den, (1UL<<31)-1);
179  if (vst)
180  avpriv_set_pts_info(vst, 33, num, den);
181  avpriv_set_pts_info(st, 33, num, den);
182  }
183  if (!s->nb_streams)
184  return AVERROR_INVALIDDATA;
185 
186  toc_offset = AV_RL32(&vmd->vmd_header[812]);
187  vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
188  vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
189  avio_seek(pb, toc_offset, SEEK_SET);
190 
191  raw_frame_table = NULL;
192  vmd->frame_table = NULL;
193  sound_buffers = AV_RL16(&vmd->vmd_header[808]);
194  raw_frame_table_size = vmd->frame_count * 6;
195  if(vmd->frame_count * vmd->frames_per_block >= UINT_MAX / sizeof(vmd_frame) - sound_buffers){
196  av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
197  return -1;
198  }
199  raw_frame_table = av_malloc(raw_frame_table_size);
200  vmd->frame_table = av_malloc_array(vmd->frame_count * vmd->frames_per_block + sound_buffers, sizeof(vmd_frame));
201  if (!raw_frame_table || !vmd->frame_table) {
202  ret = AVERROR(ENOMEM);
203  goto error;
204  }
205  if (avio_read(pb, raw_frame_table, raw_frame_table_size) !=
206  raw_frame_table_size) {
207  ret = AVERROR(EIO);
208  goto error;
209  }
210 
211  total_frames = 0;
212  for (i = 0; i < vmd->frame_count; i++) {
213 
214  current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
215 
216  /* handle each entry in index block */
217  for (j = 0; j < vmd->frames_per_block; j++) {
218  int type;
219  uint32_t size;
220 
221  if ((ret = avio_read(pb, chunk, BYTES_PER_FRAME_RECORD)) != BYTES_PER_FRAME_RECORD) {
222  av_log(s, AV_LOG_ERROR, "Failed to read frame record\n");
223  if (ret >= 0)
224  ret = AVERROR_INVALIDDATA;
225  goto error;
226  }
227  type = chunk[0];
228  size = AV_RL32(&chunk[2]);
229  if (size > INT_MAX / 2) {
230  av_log(s, AV_LOG_ERROR, "Invalid frame size\n");
231  ret = AVERROR_INVALIDDATA;
232  goto error;
233  }
234  if(!size && type != 1)
235  continue;
236  switch(type) {
237  case 1: /* Audio Chunk */
238  if (!st) break;
239  /* first audio chunk contains several audio buffers */
240  vmd->frame_table[total_frames].frame_offset = current_offset;
241  vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
242  vmd->frame_table[total_frames].frame_size = size;
243  memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
244  vmd->frame_table[total_frames].pts = current_audio_pts;
245  total_frames++;
246  if(!current_audio_pts)
247  current_audio_pts += sound_buffers - 1;
248  else
249  current_audio_pts++;
250  break;
251  case 2: /* Video Chunk */
252  if (!vst)
253  break;
254  vmd->frame_table[total_frames].frame_offset = current_offset;
255  vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
256  vmd->frame_table[total_frames].frame_size = size;
257  memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
258  vmd->frame_table[total_frames].pts = i;
259  total_frames++;
260  break;
261  }
262  current_offset += size;
263  }
264  }
265 
266  av_free(raw_frame_table);
267 
268  vmd->current_frame = 0;
269  vmd->frame_count = total_frames;
270 
271  return 0;
272 
273 error:
274  av_freep(&raw_frame_table);
275  av_freep(&vmd->frame_table);
276  return ret;
277 }
278 
280  AVPacket *pkt)
281 {
282  VmdDemuxContext *vmd = s->priv_data;
283  AVIOContext *pb = s->pb;
284  int ret = 0, record_size;
285  vmd_frame *frame;
286 
287  if (vmd->current_frame >= vmd->frame_count)
288  return AVERROR_EOF;
289 
290  frame = &vmd->frame_table[vmd->current_frame];
291  /* position the stream (will probably be there already) */
292  avio_seek(pb, frame->frame_offset, SEEK_SET);
293 
294  if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
295  return AVERROR(EIO);
296  record_size = vmd->is_indeo3 && frame->frame_record[0] == 0x02 ? 0 : BYTES_PER_FRAME_RECORD;
297  ret = av_new_packet(pkt, frame->frame_size + record_size);
298  if (ret < 0)
299  return ret;
300  pkt->pos= avio_tell(pb);
301  memcpy(pkt->data, frame->frame_record, record_size);
302  ret = avio_read(pb, pkt->data + record_size, frame->frame_size);
303 
304  if (ret != frame->frame_size) {
305  ret = AVERROR(EIO);
306  }
307  pkt->stream_index = frame->stream_index;
308  pkt->pts = frame->pts;
309  av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
310  (frame->frame_record[0] == 0x02) ? "video" : "audio",
311  frame->frame_size + BYTES_PER_FRAME_RECORD,
312  pkt->pts);
313 
314  vmd->current_frame++;
315 
316  return ret;
317 }
318 
320 {
321  VmdDemuxContext *vmd = s->priv_data;
322 
323  av_freep(&vmd->frame_table);
324 
325  return 0;
326 }
327 
329  .name = "vmd",
330  .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD"),
331  .priv_data_size = sizeof(VmdDemuxContext),
336 };
Main libavformat public API header.
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:451
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:645
int ffio_limit(AVIOContext *s, int size)
Definition: utils.c:244
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AV_RL16
Definition: intreadwrite.h:42
#define AV_RL32
Definition: intreadwrite.h:146
#define s(width, name)
Definition: cbs_vp9.c:257
audio channel layout utility functions
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static AVFrame * frame
sample_rate
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:555
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_STEREO
@ AV_CODEC_ID_VMDAUDIO
Definition: codec_id.h:435
@ AV_CODEC_ID_INDEO3
Definition: codec_id.h:77
@ AV_CODEC_ID_VMDVIDEO
Definition: codec_id.h:101
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
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4508
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
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
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
cl_device_type type
int i
Definition: input.c:407
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:4944
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:3314
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
uint8_t w
Definition: llviddspenc.c:39
static int vmd_probe(const AVProbeData *p)
Definition: sierravmd.c:65
AVInputFormat ff_vmd_demuxer
Definition: sierravmd.c:328
static int vmd_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: sierravmd.c:279
#define VMD_HEADER_SIZE
Definition: sierravmd.c:36
#define BYTES_PER_FRAME_RECORD
Definition: sierravmd.c:37
static int vmd_read_close(AVFormatContext *s)
Definition: sierravmd.c:319
static int vmd_read_header(AVFormatContext *s)
Definition: sierravmd.c:91
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
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
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
int block_align
Audio only.
Definition: codec_par.h:177
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
Bytestream IO Context.
Definition: avio.h:161
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
uint8_t * data
Definition: packet.h:369
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
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
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int index
stream index in AVFormatContext
Definition: avformat.h:874
unsigned int frame_count
Definition: sierravmd.c:52
int video_stream_index
Definition: sierravmd.c:49
int audio_stream_index
Definition: sierravmd.c:50
int64_t audio_sample_counter
Definition: sierravmd.c:59
unsigned int current_frame
Definition: sierravmd.c:55
unsigned int frames_per_block
Definition: sierravmd.c:53
unsigned char vmd_header[VMD_HEADER_SIZE]
Definition: sierravmd.c:62
vmd_frame * frame_table
Definition: sierravmd.c:54
int64_t pts
Definition: sierravmd.c:43
int64_t frame_offset
Definition: sierravmd.c:41
unsigned int frame_size
Definition: sierravmd.c:42
unsigned char frame_record[BYTES_PER_FRAME_RECORD]
Definition: sierravmd.c:45
int stream_index
Definition: sierravmd.c:40
int keyframe
Definition: sierravmd.c:44
#define av_free(p)
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
static void error(const char *err)
AVPacket * pkt
Definition: movenc.c:59
#define height
#define width
int size