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 {
41  int64_t frame_offset;
42  unsigned int frame_size;
43  int64_t pts;
44  int keyframe;
45  unsigned char frame_record[BYTES_PER_FRAME_RECORD];
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, 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->codec->codec_type = AVMEDIA_TYPE_VIDEO;
121  vst->codec->codec_id = vmd->is_indeo3 ? AV_CODEC_ID_INDEO3 : AV_CODEC_ID_VMDVIDEO;
122  vst->codec->codec_tag = 0; /* no fourcc */
123  vst->codec->width = width;
124  vst->codec->height = height;
125  if(vmd->is_indeo3 && vst->codec->width > 320){
126  vst->codec->width >>= 1;
127  vst->codec->height >>= 1;
128  }
129  vst->codec->extradata_size = VMD_HEADER_SIZE;
130  vst->codec->extradata = av_mallocz(VMD_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
131  memcpy(vst->codec->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->codec->codec_tag = 0; /* no fourcc */
144  if (vmd->vmd_header[811] & 0x80) {
145  st->codec->channels = 2;
147  } else {
148  st->codec->channels = 1;
150  }
151  st->codec->sample_rate = vmd->sample_rate;
152  st->codec->block_align = AV_RL16(&vmd->vmd_header[806]);
153  if (st->codec->block_align & 0x8000) {
154  st->codec->bits_per_coded_sample = 16;
155  st->codec->block_align = -(st->codec->block_align - 0x10000);
156  } else {
157  st->codec->bits_per_coded_sample = 8;
158  }
159  st->codec->bit_rate = st->codec->sample_rate *
161 
162  /* calculate pts */
163  num = st->codec->block_align;
164  den = st->codec->sample_rate * st->codec->channels;
165  av_reduce(&num, &den, num, den, (1UL<<31)-1);
166  if (vst)
167  avpriv_set_pts_info(vst, 33, num, den);
168  avpriv_set_pts_info(st, 33, num, den);
169  }
170 
171  toc_offset = AV_RL32(&vmd->vmd_header[812]);
172  vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
173  vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
174  avio_seek(pb, toc_offset, SEEK_SET);
175 
176  raw_frame_table = NULL;
177  vmd->frame_table = NULL;
178  sound_buffers = AV_RL16(&vmd->vmd_header[808]);
179  raw_frame_table_size = vmd->frame_count * 6;
180  if(vmd->frame_count * vmd->frames_per_block >= UINT_MAX / sizeof(vmd_frame) - sound_buffers){
181  av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
182  return -1;
183  }
184  raw_frame_table = av_malloc(raw_frame_table_size);
185  vmd->frame_table = av_malloc((vmd->frame_count * vmd->frames_per_block + sound_buffers) * sizeof(vmd_frame));
186  if (!raw_frame_table || !vmd->frame_table) {
187  av_free(raw_frame_table);
188  av_free(vmd->frame_table);
189  return AVERROR(ENOMEM);
190  }
191  if (avio_read(pb, raw_frame_table, raw_frame_table_size) !=
192  raw_frame_table_size) {
193  av_free(raw_frame_table);
194  av_free(vmd->frame_table);
195  return AVERROR(EIO);
196  }
197 
198  total_frames = 0;
199  for (i = 0; i < vmd->frame_count; i++) {
200 
201  current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
202 
203  /* handle each entry in index block */
204  for (j = 0; j < vmd->frames_per_block; j++) {
205  int type;
206  uint32_t size;
207 
208  avio_read(pb, chunk, BYTES_PER_FRAME_RECORD);
209  type = chunk[0];
210  size = AV_RL32(&chunk[2]);
211  if(!size && type != 1)
212  continue;
213  switch(type) {
214  case 1: /* Audio Chunk */
215  if (!st) break;
216  /* first audio chunk contains several audio buffers */
217  vmd->frame_table[total_frames].frame_offset = current_offset;
218  vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
219  vmd->frame_table[total_frames].frame_size = size;
220  memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
221  vmd->frame_table[total_frames].pts = current_audio_pts;
222  total_frames++;
223  if(!current_audio_pts)
224  current_audio_pts += sound_buffers - 1;
225  else
226  current_audio_pts++;
227  break;
228  case 2: /* Video Chunk */
229  vmd->frame_table[total_frames].frame_offset = current_offset;
230  vmd->frame_table[total_frames].stream_index = vmd->video_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 = i;
234  total_frames++;
235  break;
236  }
237  current_offset += size;
238  }
239  }
240 
241  av_free(raw_frame_table);
242 
243  vmd->current_frame = 0;
244  vmd->frame_count = total_frames;
245 
246  return 0;
247 }
248 
250  AVPacket *pkt)
251 {
252  VmdDemuxContext *vmd = s->priv_data;
253  AVIOContext *pb = s->pb;
254  int ret = 0;
255  vmd_frame *frame;
256 
257  if (vmd->current_frame >= vmd->frame_count)
258  return AVERROR_EOF;
259 
260  frame = &vmd->frame_table[vmd->current_frame];
261  /* position the stream (will probably be there already) */
262  avio_seek(pb, frame->frame_offset, SEEK_SET);
263 
264  if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
265  return AVERROR(EIO);
267  return AVERROR(ENOMEM);
268  pkt->pos= avio_tell(pb);
269  memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
270  if(vmd->is_indeo3 && frame->frame_record[0] == 0x02)
271  ret = avio_read(pb, pkt->data, frame->frame_size);
272  else
273  ret = avio_read(pb, pkt->data + BYTES_PER_FRAME_RECORD,
274  frame->frame_size);
275 
276  if (ret != frame->frame_size) {
277  av_free_packet(pkt);
278  ret = AVERROR(EIO);
279  }
280  pkt->stream_index = frame->stream_index;
281  pkt->pts = frame->pts;
282  av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
283  (frame->frame_record[0] == 0x02) ? "video" : "audio",
285  pkt->pts);
286 
287  vmd->current_frame++;
288 
289  return ret;
290 }
291 
293 {
294  VmdDemuxContext *vmd = s->priv_data;
295 
296  av_free(vmd->frame_table);
297 
298  return 0;
299 }
300 
302  .name = "vmd",
303  .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD"),
304  .priv_data_size = sizeof(VmdDemuxContext),
309 };