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/
32 #define VMD_HEADER_SIZE 0x0330
33 #define BYTES_PER_FRAME_RECORD 16
37 offset_t frame_offset
;
38 unsigned int frame_size
;
41 unsigned char frame_record
[BYTES_PER_FRAME_RECORD
];
44 typedef struct VmdDemuxContext
{
45 int video_stream_index
;
46 int audio_stream_index
;
48 unsigned int frame_count
;
49 unsigned int frames_per_block
;
50 vmd_frame_t
*frame_table
;
51 unsigned int current_frame
;
54 int64_t audio_sample_counter
;
57 unsigned char vmd_header
[VMD_HEADER_SIZE
];
60 static int vmd_probe(AVProbeData
*p
)
62 /* check if the first 2 bytes of the file contain the appropriate size
63 * of a VMD header chunk */
64 if (AV_RL16(&p
->buf
[0]) != VMD_HEADER_SIZE
- 2)
67 /* only return half certainty since this check is a bit sketchy */
68 return AVPROBE_SCORE_MAX
/ 2;
71 static int vmd_read_header(AVFormatContext
*s
,
72 AVFormatParameters
*ap
)
74 VmdDemuxContext
*vmd
= s
->priv_data
;
75 ByteIOContext
*pb
= s
->pb
;
76 AVStream
*st
= NULL
, *vst
;
77 unsigned int toc_offset
;
78 unsigned char *raw_frame_table
;
79 int raw_frame_table_size
;
80 offset_t current_offset
;
82 unsigned int total_frames
;
84 int64_t current_video_pts
= 0, current_audio_pts
= 0;
85 unsigned char chunk
[BYTES_PER_FRAME_RECORD
];
89 /* fetch the main header, including the 2 header length bytes */
90 url_fseek(pb
, 0, SEEK_SET
);
91 if (get_buffer(pb
, vmd
->vmd_header
, VMD_HEADER_SIZE
) != VMD_HEADER_SIZE
)
94 /* start up the decoders */
95 vst
= av_new_stream(s
, 0);
97 return AVERROR(ENOMEM
);
98 av_set_pts_info(vst
, 33, 1, 10);
99 vmd
->video_stream_index
= vst
->index
;
100 vst
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
101 vst
->codec
->codec_id
= CODEC_ID_VMDVIDEO
;
102 vst
->codec
->codec_tag
= 0; /* no fourcc */
103 vst
->codec
->width
= AV_RL16(&vmd
->vmd_header
[12]);
104 vst
->codec
->height
= AV_RL16(&vmd
->vmd_header
[14]);
105 vst
->codec
->extradata_size
= VMD_HEADER_SIZE
;
106 vst
->codec
->extradata
= av_mallocz(VMD_HEADER_SIZE
+ FF_INPUT_BUFFER_PADDING_SIZE
);
107 memcpy(vst
->codec
->extradata
, vmd
->vmd_header
, VMD_HEADER_SIZE
);
109 /* if sample rate is 0, assume no audio */
110 vmd
->sample_rate
= AV_RL16(&vmd
->vmd_header
[804]);
111 if (vmd
->sample_rate
) {
112 st
= av_new_stream(s
, 0);
114 return AVERROR(ENOMEM
);
115 vmd
->audio_stream_index
= st
->index
;
116 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
117 st
->codec
->codec_id
= CODEC_ID_VMDAUDIO
;
118 st
->codec
->codec_tag
= 0; /* no fourcc */
119 st
->codec
->channels
= (vmd
->vmd_header
[811] & 0x80) ? 2 : 1;
120 st
->codec
->sample_rate
= vmd
->sample_rate
;
121 st
->codec
->block_align
= AV_RL16(&vmd
->vmd_header
[806]);
122 if (st
->codec
->block_align
& 0x8000) {
123 st
->codec
->bits_per_sample
= 16;
124 st
->codec
->block_align
= -(st
->codec
->block_align
- 0x10000);
126 st
->codec
->bits_per_sample
= 8;
128 st
->codec
->bit_rate
= st
->codec
->sample_rate
*
129 st
->codec
->bits_per_sample
* st
->codec
->channels
;
132 num
= st
->codec
->block_align
;
133 den
= st
->codec
->sample_rate
* st
->codec
->channels
;
134 av_reduce(&den
, &num
, den
, num
, (1UL<<31)-1);
135 av_set_pts_info(vst
, 33, num
, den
);
136 av_set_pts_info(st
, 33, num
, den
);
140 toc_offset
= AV_RL32(&vmd
->vmd_header
[812]);
141 vmd
->frame_count
= AV_RL16(&vmd
->vmd_header
[6]);
142 vmd
->frames_per_block
= AV_RL16(&vmd
->vmd_header
[18]);
143 url_fseek(pb
, toc_offset
, SEEK_SET
);
145 raw_frame_table
= NULL
;
146 vmd
->frame_table
= NULL
;
147 sound_buffers
= AV_RL16(&vmd
->vmd_header
[808]);
148 raw_frame_table_size
= vmd
->frame_count
* 6;
149 if(vmd
->frame_count
* vmd
->frames_per_block
>= UINT_MAX
/ sizeof(vmd_frame_t
)){
150 av_log(s
, AV_LOG_ERROR
, "vmd->frame_count * vmd->frames_per_block too large\n");
153 raw_frame_table
= av_malloc(raw_frame_table_size
);
154 vmd
->frame_table
= av_malloc((vmd
->frame_count
* vmd
->frames_per_block
+ sound_buffers
) * sizeof(vmd_frame_t
));
155 if (!raw_frame_table
|| !vmd
->frame_table
) {
156 av_free(raw_frame_table
);
157 av_free(vmd
->frame_table
);
158 return AVERROR(ENOMEM
);
160 if (get_buffer(pb
, raw_frame_table
, raw_frame_table_size
) !=
161 raw_frame_table_size
) {
162 av_free(raw_frame_table
);
163 av_free(vmd
->frame_table
);
168 for (i
= 0; i
< vmd
->frame_count
; i
++) {
170 current_offset
= AV_RL32(&raw_frame_table
[6 * i
+ 2]);
172 /* handle each entry in index block */
173 for (j
= 0; j
< vmd
->frames_per_block
; j
++) {
177 get_buffer(pb
, chunk
, BYTES_PER_FRAME_RECORD
);
179 size
= AV_RL32(&chunk
[2]);
183 case 1: /* Audio Chunk */
185 /* first audio chunk contains several audio buffers */
186 if(current_audio_pts
){
187 vmd
->frame_table
[total_frames
].frame_offset
= current_offset
;
188 vmd
->frame_table
[total_frames
].stream_index
= vmd
->audio_stream_index
;
189 vmd
->frame_table
[total_frames
].frame_size
= size
;
190 memcpy(vmd
->frame_table
[total_frames
].frame_record
, chunk
, BYTES_PER_FRAME_RECORD
);
191 vmd
->frame_table
[total_frames
].pts
= current_audio_pts
;
193 current_audio_pts
+= pts_inc
;
201 url_fseek(pb
, current_offset
, SEEK_SET
);
202 flags
= get_le32(pb
);
204 url_fseek(pb
, pos
, SEEK_SET
);
205 av_log(s
, AV_LOG_DEBUG
, "Sound mapping = %08X (%i bufs)\n", flags
, sound_buffers
);
206 for(k
= 0; k
< sound_buffers
- 1; k
++){
207 if(flags
& 1) { /* silent block */
208 vmd
->frame_table
[total_frames
].frame_size
= 0;
210 vmd
->frame_table
[total_frames
].frame_size
= st
->codec
->block_align
+ (st
->codec
->block_align
& 1);
212 noff
+= vmd
->frame_table
[total_frames
].frame_size
;
213 vmd
->frame_table
[total_frames
].frame_offset
= current_offset
+ noff
;
214 vmd
->frame_table
[total_frames
].stream_index
= vmd
->audio_stream_index
;
215 memcpy(vmd
->frame_table
[total_frames
].frame_record
, chunk
, BYTES_PER_FRAME_RECORD
);
216 vmd
->frame_table
[total_frames
].pts
= current_audio_pts
;
218 current_audio_pts
+= pts_inc
;
223 case 2: /* Video Chunk */
224 vmd
->frame_table
[total_frames
].frame_offset
= current_offset
;
225 vmd
->frame_table
[total_frames
].stream_index
= vmd
->video_stream_index
;
226 vmd
->frame_table
[total_frames
].frame_size
= size
;
227 memcpy(vmd
->frame_table
[total_frames
].frame_record
, chunk
, BYTES_PER_FRAME_RECORD
);
228 vmd
->frame_table
[total_frames
].pts
= current_video_pts
;
232 current_offset
+= size
;
234 current_video_pts
+= pts_inc
;
237 av_free(raw_frame_table
);
239 vmd
->current_frame
= 0;
240 vmd
->frame_count
= total_frames
;
245 static int vmd_read_packet(AVFormatContext
*s
,
248 VmdDemuxContext
*vmd
= s
->priv_data
;
249 ByteIOContext
*pb
= s
->pb
;
253 if (vmd
->current_frame
>= vmd
->frame_count
)
256 frame
= &vmd
->frame_table
[vmd
->current_frame
];
257 /* position the stream (will probably be there already) */
258 url_fseek(pb
, frame
->frame_offset
, SEEK_SET
);
260 if (av_new_packet(pkt
, frame
->frame_size
+ BYTES_PER_FRAME_RECORD
))
261 return AVERROR(ENOMEM
);
262 pkt
->pos
= url_ftell(pb
);
263 memcpy(pkt
->data
, frame
->frame_record
, BYTES_PER_FRAME_RECORD
);
264 ret
= get_buffer(pb
, pkt
->data
+ BYTES_PER_FRAME_RECORD
,
267 if (ret
!= frame
->frame_size
) {
271 pkt
->stream_index
= frame
->stream_index
;
272 pkt
->pts
= frame
->pts
;
273 av_log(NULL
, AV_LOG_DEBUG
, " dispatching %s frame with %d bytes and pts %"PRId64
"\n",
274 (frame
->frame_record
[0] == 0x02) ? "video" : "audio",
275 frame
->frame_size
+ BYTES_PER_FRAME_RECORD
,
278 vmd
->current_frame
++;
283 static int vmd_read_close(AVFormatContext
*s
)
285 VmdDemuxContext
*vmd
= s
->priv_data
;
287 av_free(vmd
->frame_table
);
292 AVInputFormat vmd_demuxer
= {
294 NULL_IF_CONFIG_SMALL("Sierra VMD format"),
295 sizeof(VmdDemuxContext
),