2 * Sierra VMD Format Demuxer
3 * Copyright (c) 2004 The FFmpeg project
5 * This file is part of FFmpeg.
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.
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.
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
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/
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/mem.h"
34 #include "avio_internal.h"
37 #include "avio_internal.h"
39 #define VMD_HEADER_SIZE 0x0330
40 #define BYTES_PER_FRAME_RECORD 16
42 typedef struct vmd_frame
{
44 unsigned int frame_size
;
47 unsigned char frame_record
[BYTES_PER_FRAME_RECORD
];
50 typedef struct VmdDemuxContext
{
51 int video_stream_index
;
52 int audio_stream_index
;
54 unsigned int frame_count
;
55 unsigned int frames_per_block
;
56 vmd_frame
*frame_table
;
57 unsigned int current_frame
;
61 int64_t audio_sample_counter
;
64 unsigned char vmd_header
[VMD_HEADER_SIZE
];
67 static int vmd_probe(const AVProbeData
*p
)
69 int w
, h
, sample_rate
;
70 if (p
->buf_size
< 806)
72 /* check if the first 2 bytes of the file contain the appropriate size
73 * of a VMD header chunk */
74 if (AV_RL16(&p
->buf
[0]) != VMD_HEADER_SIZE
- 2)
76 w
= AV_RL16(&p
->buf
[12]);
77 h
= AV_RL16(&p
->buf
[14]);
78 sample_rate
= AV_RL16(&p
->buf
[804]);
79 if ((!w
|| w
> 2048 || !h
|| h
> 2048) &&
83 /* only return half certainty since this check is a bit sketchy */
84 return AVPROBE_SCORE_EXTENSION
;
87 static int vmd_read_header(AVFormatContext
*s
)
89 VmdDemuxContext
*vmd
= s
->priv_data
;
90 AVIOContext
*pb
= s
->pb
;
91 AVStream
*st
= NULL
, *vst
= NULL
;
92 unsigned int toc_offset
;
93 unsigned char *raw_frame_table
;
94 int raw_frame_table_size
;
95 int64_t current_offset
;
98 unsigned int total_frames
;
99 int64_t current_audio_pts
= 0;
100 unsigned char chunk
[BYTES_PER_FRAME_RECORD
];
104 /* fetch the main header, including the 2 header length bytes */
105 avio_seek(pb
, 0, SEEK_SET
);
106 if ((ret
= ffio_read_size(pb
, vmd
->vmd_header
, VMD_HEADER_SIZE
) < 0))
109 width
= AV_RL16(&vmd
->vmd_header
[12]);
110 height
= AV_RL16(&vmd
->vmd_header
[14]);
111 if (width
&& height
) {
112 if(vmd
->vmd_header
[24] == 'i' && vmd
->vmd_header
[25] == 'v' && vmd
->vmd_header
[26] == '3') {
117 /* start up the decoders */
118 vst
= avformat_new_stream(s
, NULL
);
120 return AVERROR(ENOMEM
);
121 avpriv_set_pts_info(vst
, 33, 1, 10);
122 vmd
->video_stream_index
= vst
->index
;
123 vst
->codecpar
->codec_type
= AVMEDIA_TYPE_VIDEO
;
124 vst
->codecpar
->codec_id
= vmd
->is_indeo3
? AV_CODEC_ID_INDEO3
: AV_CODEC_ID_VMDVIDEO
;
125 vst
->codecpar
->codec_tag
= 0; /* no fourcc */
126 vst
->codecpar
->width
= width
;
127 vst
->codecpar
->height
= height
;
128 if(vmd
->is_indeo3
&& vst
->codecpar
->width
> 320){
129 vst
->codecpar
->width
>>= 1;
130 vst
->codecpar
->height
>>= 1;
132 if ((ret
= ff_alloc_extradata(vst
->codecpar
, VMD_HEADER_SIZE
)) < 0)
134 memcpy(vst
->codecpar
->extradata
, vmd
->vmd_header
, VMD_HEADER_SIZE
);
137 /* if sample rate is 0, assume no audio */
138 vmd
->sample_rate
= AV_RL16(&vmd
->vmd_header
[804]);
139 if (vmd
->sample_rate
) {
141 st
= avformat_new_stream(s
, NULL
);
143 return AVERROR(ENOMEM
);
144 vmd
->audio_stream_index
= st
->index
;
145 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
146 st
->codecpar
->codec_id
= AV_CODEC_ID_VMDAUDIO
;
147 st
->codecpar
->codec_tag
= 0; /* no fourcc */
148 st
->codecpar
->sample_rate
= vmd
->sample_rate
;
149 st
->codecpar
->block_align
= AV_RL16(&vmd
->vmd_header
[806]);
150 if (st
->codecpar
->block_align
& 0x8000) {
151 st
->codecpar
->bits_per_coded_sample
= 16;
152 st
->codecpar
->block_align
= -(st
->codecpar
->block_align
- 0x10000);
154 st
->codecpar
->bits_per_coded_sample
= 8;
156 if (vmd
->vmd_header
[811] & 0x80) {
158 } else if (vmd
->vmd_header
[811] & 0x2) {
159 /* Shivers 2 stereo audio */
160 /* Frame length is for 1 channel */
162 st
->codecpar
->block_align
= st
->codecpar
->block_align
<< 1;
166 av_channel_layout_default(&st
->codecpar
->ch_layout
, channels
);
167 st
->codecpar
->bit_rate
= st
->codecpar
->sample_rate
*
168 st
->codecpar
->bits_per_coded_sample
* channels
;
171 num
= st
->codecpar
->block_align
;
172 den
= st
->codecpar
->sample_rate
* channels
;
173 av_reduce(&num
, &den
, num
, den
, (1UL<<31)-1);
175 avpriv_set_pts_info(vst
, 33, num
, den
);
176 avpriv_set_pts_info(st
, 33, num
, den
);
179 return AVERROR_INVALIDDATA
;
181 toc_offset
= AV_RL32(&vmd
->vmd_header
[812]);
182 vmd
->frame_count
= AV_RL16(&vmd
->vmd_header
[6]);
183 vmd
->frames_per_block
= AV_RL16(&vmd
->vmd_header
[18]);
184 avio_seek(pb
, toc_offset
, SEEK_SET
);
186 raw_frame_table
= NULL
;
187 vmd
->frame_table
= NULL
;
188 sound_buffers
= AV_RL16(&vmd
->vmd_header
[808]);
189 raw_frame_table_size
= vmd
->frame_count
* 6;
190 raw_frame_table
= av_malloc(raw_frame_table_size
);
191 vmd
->frame_table
= av_malloc_array(vmd
->frame_count
* vmd
->frames_per_block
+ sound_buffers
, sizeof(vmd_frame
));
192 if (!raw_frame_table
|| !vmd
->frame_table
) {
193 ret
= AVERROR(ENOMEM
);
196 if ((ret
= ffio_read_size(pb
, raw_frame_table
, raw_frame_table_size
)) < 0)
200 for (i
= 0; i
< vmd
->frame_count
; i
++) {
202 current_offset
= AV_RL32(&raw_frame_table
[6 * i
+ 2]);
204 /* handle each entry in index block */
205 for (j
= 0; j
< vmd
->frames_per_block
; j
++) {
209 if ((ret
= avio_read(pb
, chunk
, BYTES_PER_FRAME_RECORD
)) != BYTES_PER_FRAME_RECORD
) {
210 av_log(s
, AV_LOG_ERROR
, "Failed to read frame record\n");
212 ret
= AVERROR_INVALIDDATA
;
216 size
= AV_RL32(&chunk
[2]);
217 if (size
> INT_MAX
/ 2) {
218 av_log(s
, AV_LOG_ERROR
, "Invalid frame size\n");
219 ret
= AVERROR_INVALIDDATA
;
222 if(!size
&& type
!= 1)
225 case 1: /* Audio Chunk */
227 /* first audio chunk contains several audio buffers */
228 vmd
->frame_table
[total_frames
].frame_offset
= current_offset
;
229 vmd
->frame_table
[total_frames
].stream_index
= vmd
->audio_stream_index
;
230 vmd
->frame_table
[total_frames
].frame_size
= size
;
231 memcpy(vmd
->frame_table
[total_frames
].frame_record
, chunk
, BYTES_PER_FRAME_RECORD
);
232 vmd
->frame_table
[total_frames
].pts
= current_audio_pts
;
234 if(!current_audio_pts
)
235 current_audio_pts
+= sound_buffers
- 1;
239 case 2: /* Video Chunk */
242 vmd
->frame_table
[total_frames
].frame_offset
= current_offset
;
243 vmd
->frame_table
[total_frames
].stream_index
= vmd
->video_stream_index
;
244 vmd
->frame_table
[total_frames
].frame_size
= size
;
245 memcpy(vmd
->frame_table
[total_frames
].frame_record
, chunk
, BYTES_PER_FRAME_RECORD
);
246 vmd
->frame_table
[total_frames
].pts
= i
;
250 current_offset
+= size
;
255 vmd
->current_frame
= 0;
256 vmd
->frame_count
= total_frames
;
260 av_freep(&raw_frame_table
);
264 static int vmd_read_packet(AVFormatContext
*s
,
267 VmdDemuxContext
*vmd
= s
->priv_data
;
268 AVIOContext
*pb
= s
->pb
;
272 if (vmd
->current_frame
>= vmd
->frame_count
)
275 frame
= &vmd
->frame_table
[vmd
->current_frame
];
276 /* position the stream (will probably be there already) */
277 avio_seek(pb
, frame
->frame_offset
, SEEK_SET
);
279 if(ffio_limit(pb
, frame
->frame_size
) != frame
->frame_size
)
280 return AVERROR_INVALIDDATA
;
281 ret
= av_new_packet(pkt
, frame
->frame_size
+ BYTES_PER_FRAME_RECORD
);
284 pkt
->pos
= avio_tell(pb
);
285 memcpy(pkt
->data
, frame
->frame_record
, BYTES_PER_FRAME_RECORD
);
286 if(vmd
->is_indeo3
&& frame
->frame_record
[0] == 0x02)
287 ret
= ffio_read_size(pb
, pkt
->data
, frame
->frame_size
);
289 ret
= ffio_read_size(pb
, pkt
->data
+ BYTES_PER_FRAME_RECORD
,
292 pkt
->stream_index
= frame
->stream_index
;
293 pkt
->pts
= frame
->pts
;
294 av_log(s
, AV_LOG_DEBUG
, " dispatching %s frame with %d bytes and pts %"PRId64
"\n",
295 (frame
->frame_record
[0] == 0x02) ? "video" : "audio",
296 frame
->frame_size
+ BYTES_PER_FRAME_RECORD
,
299 vmd
->current_frame
++;
304 static int vmd_read_close(AVFormatContext
*s
)
306 VmdDemuxContext
*vmd
= s
->priv_data
;
308 av_freep(&vmd
->frame_table
);
313 const FFInputFormat ff_vmd_demuxer
= {
315 .p
.long_name
= NULL_IF_CONFIG_SMALL("Sierra VMD"),
316 .priv_data_size
= sizeof(VmdDemuxContext
),
317 .flags_internal
= FF_INFMT_FLAG_INIT_CLEANUP
,
318 .read_probe
= vmd_probe
,
319 .read_header
= vmd_read_header
,
320 .read_packet
= vmd_read_packet
,
321 .read_close
= vmd_read_close
,