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"
34 #include "avio_internal.h"
36 #define VMD_HEADER_SIZE 0x0330
37 #define BYTES_PER_FRAME_RECORD 16
39 typedef struct vmd_frame {
42 unsigned int frame_size;
45 unsigned char frame_record[BYTES_PER_FRAME_RECORD];
48 typedef struct VmdDemuxContext {
49 int video_stream_index;
50 int audio_stream_index;
52 unsigned int frame_count;
53 unsigned int frames_per_block;
54 vmd_frame *frame_table;
55 unsigned int current_frame;
59 int64_t audio_sample_counter;
62 unsigned char vmd_header[VMD_HEADER_SIZE];
65 static int vmd_probe(AVProbeData *p)
67 int w, h, sample_rate;
68 if (p->buf_size < 806)
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)
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) &&
81 /* only return half certainty since this check is a bit sketchy */
82 return AVPROBE_SCORE_EXTENSION;
85 static int vmd_read_header(AVFormatContext *s)
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;
96 unsigned int total_frames;
97 int64_t current_audio_pts = 0;
98 unsigned char chunk[BYTES_PER_FRAME_RECORD];
102 /* fetch the main header, including the 2 header length bytes */
103 avio_seek(pb, 0, SEEK_SET);
104 if (avio_read(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE)
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') {
115 /* start up the decoders */
116 vst = avformat_new_stream(s, NULL);
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;
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);
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);
140 return AVERROR(ENOMEM);
141 vmd->audio_stream_index = st->index;
142 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
143 st->codecpar->codec_id = AV_CODEC_ID_VMDAUDIO;
144 st->codecpar->codec_tag = 0; /* no fourcc */
145 if (vmd->vmd_header[811] & 0x80) {
146 st->codecpar->channels = 2;
147 st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
149 st->codecpar->channels = 1;
150 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
152 st->codecpar->sample_rate = vmd->sample_rate;
153 st->codecpar->block_align = AV_RL16(&vmd->vmd_header[806]);
154 if (st->codecpar->block_align & 0x8000) {
155 st->codecpar->bits_per_coded_sample = 16;
156 st->codecpar->block_align = -(st->codecpar->block_align - 0x10000);
158 st->codecpar->bits_per_coded_sample = 8;
160 st->codecpar->bit_rate = st->codecpar->sample_rate *
161 st->codecpar->bits_per_coded_sample * st->codecpar->channels;
164 num = st->codecpar->block_align;
165 den = st->codecpar->sample_rate * st->codecpar->channels;
166 av_reduce(&num, &den, num, den, (1UL<<31)-1);
168 avpriv_set_pts_info(vst, 33, num, den);
169 avpriv_set_pts_info(st, 33, num, den);
172 toc_offset = AV_RL32(&vmd->vmd_header[812]);
173 vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
174 vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
175 avio_seek(pb, toc_offset, SEEK_SET);
177 raw_frame_table = NULL;
178 vmd->frame_table = NULL;
179 sound_buffers = AV_RL16(&vmd->vmd_header[808]);
180 raw_frame_table_size = vmd->frame_count * 6;
181 if(vmd->frame_count * vmd->frames_per_block >= UINT_MAX / sizeof(vmd_frame) - sound_buffers){
182 av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
185 raw_frame_table = av_malloc(raw_frame_table_size);
186 vmd->frame_table = av_malloc_array(vmd->frame_count * vmd->frames_per_block + sound_buffers, sizeof(vmd_frame));
187 if (!raw_frame_table || !vmd->frame_table) {
188 ret = AVERROR(ENOMEM);
191 if (avio_read(pb, raw_frame_table, raw_frame_table_size) !=
192 raw_frame_table_size) {
198 for (i = 0; i < vmd->frame_count; i++) {
200 current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
202 /* handle each entry in index block */
203 for (j = 0; j < vmd->frames_per_block; j++) {
207 if ((ret = avio_read(pb, chunk, BYTES_PER_FRAME_RECORD)) != BYTES_PER_FRAME_RECORD) {
208 av_log(s, AV_LOG_ERROR, "Failed to read frame record\n");
210 ret = AVERROR_INVALIDDATA;
214 size = AV_RL32(&chunk[2]);
215 if (size > INT_MAX / 2) {
216 av_log(s, AV_LOG_ERROR, "Invalid frame size\n");
217 ret = AVERROR_INVALIDDATA;
220 if(!size && type != 1)
223 case 1: /* Audio Chunk */
225 /* first audio chunk contains several audio buffers */
226 vmd->frame_table[total_frames].frame_offset = current_offset;
227 vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
228 vmd->frame_table[total_frames].frame_size = size;
229 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
230 vmd->frame_table[total_frames].pts = current_audio_pts;
232 if(!current_audio_pts)
233 current_audio_pts += sound_buffers - 1;
237 case 2: /* Video Chunk */
238 vmd->frame_table[total_frames].frame_offset = current_offset;
239 vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
240 vmd->frame_table[total_frames].frame_size = size;
241 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
242 vmd->frame_table[total_frames].pts = i;
246 current_offset += size;
250 av_free(raw_frame_table);
252 vmd->current_frame = 0;
253 vmd->frame_count = total_frames;
258 av_freep(&raw_frame_table);
259 av_freep(&vmd->frame_table);
263 static int vmd_read_packet(AVFormatContext *s,
266 VmdDemuxContext *vmd = s->priv_data;
267 AVIOContext *pb = s->pb;
271 if (vmd->current_frame >= vmd->frame_count)
274 frame = &vmd->frame_table[vmd->current_frame];
275 /* position the stream (will probably be there already) */
276 avio_seek(pb, frame->frame_offset, SEEK_SET);
278 if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
280 if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
281 return AVERROR(ENOMEM);
282 pkt->pos= avio_tell(pb);
283 memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
284 if(vmd->is_indeo3 && frame->frame_record[0] == 0x02)
285 ret = avio_read(pb, pkt->data, frame->frame_size);
287 ret = avio_read(pb, pkt->data + BYTES_PER_FRAME_RECORD,
290 if (ret != frame->frame_size) {
291 av_packet_unref(pkt);
294 pkt->stream_index = frame->stream_index;
295 pkt->pts = frame->pts;
296 av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
297 (frame->frame_record[0] == 0x02) ? "video" : "audio",
298 frame->frame_size + BYTES_PER_FRAME_RECORD,
301 vmd->current_frame++;
306 static int vmd_read_close(AVFormatContext *s)
308 VmdDemuxContext *vmd = s->priv_data;
310 av_freep(&vmd->frame_table);
315 AVInputFormat ff_vmd_demuxer = {
317 .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD"),
318 .priv_data_size = sizeof(VmdDemuxContext),
319 .read_probe = vmd_probe,
320 .read_header = vmd_read_header,
321 .read_packet = vmd_read_packet,
322 .read_close = vmd_read_close,