2004-01-02 05:47:02 +01:00
|
|
|
/*
|
|
|
|
* Sierra VMD Format Demuxer
|
|
|
|
* Copyright (c) 2004 The ffmpeg Project
|
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2004-01-02 05:47:02 +01:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 17:30:46 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2004-01-02 05:47:02 +01:00
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2004-01-02 05:47:02 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 17:30:46 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-12 23:43:26 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-01-02 05:47:02 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2004-01-02 05:47:02 +01:00
|
|
|
* Sierra VMD file demuxer
|
|
|
|
* by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
|
2004-03-14 05:08:11 +01:00
|
|
|
* for more information on the Sierra VMD file format, visit:
|
|
|
|
* http://www.pcisys.net/~melanson/codecs/
|
2004-01-02 05:47:02 +01:00
|
|
|
*/
|
|
|
|
|
2009-01-11 23:19:48 +01:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2004-01-02 05:47:02 +01:00
|
|
|
#include "avformat.h"
|
|
|
|
|
|
|
|
#define VMD_HEADER_SIZE 0x0330
|
|
|
|
#define BYTES_PER_FRAME_RECORD 16
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int stream_index;
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t frame_offset;
|
2004-01-02 05:47:02 +01:00
|
|
|
unsigned int frame_size;
|
|
|
|
int64_t pts;
|
|
|
|
int keyframe;
|
|
|
|
unsigned char frame_record[BYTES_PER_FRAME_RECORD];
|
2008-12-14 04:29:33 +01:00
|
|
|
} vmd_frame;
|
2004-01-02 05:47:02 +01:00
|
|
|
|
|
|
|
typedef struct VmdDemuxContext {
|
|
|
|
int video_stream_index;
|
|
|
|
int audio_stream_index;
|
|
|
|
|
|
|
|
unsigned int frame_count;
|
2006-03-11 05:27:58 +01:00
|
|
|
unsigned int frames_per_block;
|
2008-12-14 04:29:33 +01:00
|
|
|
vmd_frame *frame_table;
|
2004-01-02 05:47:02 +01:00
|
|
|
unsigned int current_frame;
|
2008-12-27 18:34:00 +01:00
|
|
|
int is_indeo3;
|
2004-01-02 05:47:02 +01:00
|
|
|
|
2004-03-14 05:08:11 +01:00
|
|
|
int sample_rate;
|
|
|
|
int64_t audio_sample_counter;
|
2006-03-11 05:27:58 +01:00
|
|
|
int skiphdr;
|
2004-03-14 05:08:11 +01:00
|
|
|
|
2004-01-02 05:47:02 +01:00
|
|
|
unsigned char vmd_header[VMD_HEADER_SIZE];
|
|
|
|
} VmdDemuxContext;
|
|
|
|
|
|
|
|
static int vmd_probe(AVProbeData *p)
|
|
|
|
{
|
2009-09-15 11:16:29 +02:00
|
|
|
int w, h;
|
|
|
|
if (p->buf_size < 16)
|
|
|
|
return 0;
|
2004-01-02 05:47:02 +01:00
|
|
|
/* check if the first 2 bytes of the file contain the appropriate size
|
|
|
|
* of a VMD header chunk */
|
2007-01-19 23:12:59 +01:00
|
|
|
if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
|
2004-01-02 05:47:02 +01:00
|
|
|
return 0;
|
2009-09-15 11:16:29 +02:00
|
|
|
w = AV_RL16(&p->buf[12]);
|
|
|
|
h = AV_RL16(&p->buf[14]);
|
|
|
|
if (!w || w > 2048 || !h || h > 2048)
|
|
|
|
return 0;
|
2004-01-02 05:47:02 +01:00
|
|
|
|
|
|
|
/* only return half certainty since this check is a bit sketchy */
|
|
|
|
return AVPROBE_SCORE_MAX / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vmd_read_header(AVFormatContext *s,
|
|
|
|
AVFormatParameters *ap)
|
|
|
|
{
|
2007-04-08 22:24:16 +02:00
|
|
|
VmdDemuxContext *vmd = s->priv_data;
|
2007-11-21 08:41:00 +01:00
|
|
|
ByteIOContext *pb = s->pb;
|
2008-02-04 13:13:09 +01:00
|
|
|
AVStream *st = NULL, *vst;
|
2004-01-02 05:47:02 +01:00
|
|
|
unsigned int toc_offset;
|
|
|
|
unsigned char *raw_frame_table;
|
|
|
|
int raw_frame_table_size;
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t current_offset;
|
2006-03-11 05:27:58 +01:00
|
|
|
int i, j;
|
2004-01-02 05:47:02 +01:00
|
|
|
unsigned int total_frames;
|
2009-01-21 14:27:39 +01:00
|
|
|
int64_t current_audio_pts = 0;
|
2006-03-11 05:27:58 +01:00
|
|
|
unsigned char chunk[BYTES_PER_FRAME_RECORD];
|
2006-11-13 06:18:19 +01:00
|
|
|
int num, den;
|
2006-11-19 06:30:43 +01:00
|
|
|
int sound_buffers;
|
2004-01-02 05:47:02 +01:00
|
|
|
|
|
|
|
/* fetch the main header, including the 2 header length bytes */
|
|
|
|
url_fseek(pb, 0, SEEK_SET);
|
|
|
|
if (get_buffer(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE)
|
2007-07-19 17:23:32 +02:00
|
|
|
return AVERROR(EIO);
|
2004-01-02 05:47:02 +01:00
|
|
|
|
2008-12-27 18:34:00 +01:00
|
|
|
if(vmd->vmd_header[16] == 'i' && vmd->vmd_header[17] == 'v' && vmd->vmd_header[18] == '3')
|
|
|
|
vmd->is_indeo3 = 1;
|
|
|
|
else
|
2008-12-28 17:22:42 +01:00
|
|
|
vmd->is_indeo3 = 0;
|
2004-01-02 05:47:02 +01:00
|
|
|
/* start up the decoders */
|
2006-11-13 06:18:19 +01:00
|
|
|
vst = av_new_stream(s, 0);
|
|
|
|
if (!vst)
|
2007-07-19 17:21:30 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2006-11-13 06:18:19 +01:00
|
|
|
av_set_pts_info(vst, 33, 1, 10);
|
|
|
|
vmd->video_stream_index = vst->index;
|
2010-03-31 01:30:55 +02:00
|
|
|
vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
2008-12-27 18:34:00 +01:00
|
|
|
vst->codec->codec_id = vmd->is_indeo3 ? CODEC_ID_INDEO3 : CODEC_ID_VMDVIDEO;
|
2006-11-13 06:18:19 +01:00
|
|
|
vst->codec->codec_tag = 0; /* no fourcc */
|
2007-01-19 23:12:59 +01:00
|
|
|
vst->codec->width = AV_RL16(&vmd->vmd_header[12]);
|
|
|
|
vst->codec->height = AV_RL16(&vmd->vmd_header[14]);
|
2008-12-27 18:34:00 +01:00
|
|
|
if(vmd->is_indeo3 && vst->codec->width > 320){
|
|
|
|
vst->codec->width >>= 1;
|
|
|
|
vst->codec->height >>= 1;
|
|
|
|
}
|
2006-11-13 06:18:19 +01:00
|
|
|
vst->codec->extradata_size = VMD_HEADER_SIZE;
|
|
|
|
vst->codec->extradata = av_mallocz(VMD_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
memcpy(vst->codec->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
|
2004-01-02 05:47:02 +01:00
|
|
|
|
|
|
|
/* if sample rate is 0, assume no audio */
|
2007-01-19 23:12:59 +01:00
|
|
|
vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
|
2004-03-14 05:08:11 +01:00
|
|
|
if (vmd->sample_rate) {
|
2004-01-02 05:47:02 +01:00
|
|
|
st = av_new_stream(s, 0);
|
|
|
|
if (!st)
|
2007-07-19 17:21:30 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2004-01-02 05:47:02 +01:00
|
|
|
vmd->audio_stream_index = st->index;
|
2010-03-31 01:30:55 +02:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
|
2005-07-18 00:24:36 +02:00
|
|
|
st->codec->codec_id = CODEC_ID_VMDAUDIO;
|
2006-03-11 05:27:58 +01:00
|
|
|
st->codec->codec_tag = 0; /* no fourcc */
|
2006-11-13 06:18:19 +01:00
|
|
|
st->codec->channels = (vmd->vmd_header[811] & 0x80) ? 2 : 1;
|
2005-07-18 00:24:36 +02:00
|
|
|
st->codec->sample_rate = vmd->sample_rate;
|
2007-01-19 23:12:59 +01:00
|
|
|
st->codec->block_align = AV_RL16(&vmd->vmd_header[806]);
|
2005-07-18 00:24:36 +02:00
|
|
|
if (st->codec->block_align & 0x8000) {
|
2008-09-08 16:24:59 +02:00
|
|
|
st->codec->bits_per_coded_sample = 16;
|
2005-07-18 00:24:36 +02:00
|
|
|
st->codec->block_align = -(st->codec->block_align - 0x10000);
|
2006-03-11 05:27:58 +01:00
|
|
|
} else {
|
2008-09-08 16:24:59 +02:00
|
|
|
st->codec->bits_per_coded_sample = 8;
|
2006-03-11 05:27:58 +01:00
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
st->codec->bit_rate = st->codec->sample_rate *
|
2008-09-08 16:24:59 +02:00
|
|
|
st->codec->bits_per_coded_sample * st->codec->channels;
|
2004-03-14 05:08:11 +01:00
|
|
|
|
2006-11-13 06:18:19 +01:00
|
|
|
/* calculate pts */
|
|
|
|
num = st->codec->block_align;
|
|
|
|
den = st->codec->sample_rate * st->codec->channels;
|
|
|
|
av_reduce(&den, &num, den, num, (1UL<<31)-1);
|
|
|
|
av_set_pts_info(vst, 33, num, den);
|
|
|
|
av_set_pts_info(st, 33, num, den);
|
2004-01-02 05:47:02 +01:00
|
|
|
}
|
|
|
|
|
2007-01-19 23:12:59 +01:00
|
|
|
toc_offset = AV_RL32(&vmd->vmd_header[812]);
|
|
|
|
vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
|
|
|
|
vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
|
2006-03-11 05:27:58 +01:00
|
|
|
url_fseek(pb, toc_offset, SEEK_SET);
|
2004-01-02 05:47:02 +01:00
|
|
|
|
|
|
|
raw_frame_table = NULL;
|
|
|
|
vmd->frame_table = NULL;
|
2007-01-19 23:12:59 +01:00
|
|
|
sound_buffers = AV_RL16(&vmd->vmd_header[808]);
|
2006-03-11 05:27:58 +01:00
|
|
|
raw_frame_table_size = vmd->frame_count * 6;
|
2009-09-16 17:12:23 +02:00
|
|
|
if(vmd->frame_count * vmd->frames_per_block >= UINT_MAX / sizeof(vmd_frame) - sound_buffers){
|
2006-05-13 13:37:56 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2008-05-29 00:26:35 +02:00
|
|
|
raw_frame_table = av_malloc(raw_frame_table_size);
|
2008-12-14 04:29:33 +01:00
|
|
|
vmd->frame_table = av_malloc((vmd->frame_count * vmd->frames_per_block + sound_buffers) * sizeof(vmd_frame));
|
2004-01-02 05:47:02 +01:00
|
|
|
if (!raw_frame_table || !vmd->frame_table) {
|
|
|
|
av_free(raw_frame_table);
|
|
|
|
av_free(vmd->frame_table);
|
2007-07-19 17:21:30 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2004-01-02 05:47:02 +01:00
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
if (get_buffer(pb, raw_frame_table, raw_frame_table_size) !=
|
2004-01-02 05:47:02 +01:00
|
|
|
raw_frame_table_size) {
|
|
|
|
av_free(raw_frame_table);
|
|
|
|
av_free(vmd->frame_table);
|
2007-07-19 17:23:32 +02:00
|
|
|
return AVERROR(EIO);
|
2004-01-02 05:47:02 +01:00
|
|
|
}
|
|
|
|
|
2006-03-11 05:27:58 +01:00
|
|
|
total_frames = 0;
|
|
|
|
for (i = 0; i < vmd->frame_count; i++) {
|
|
|
|
|
2007-01-19 23:12:59 +01:00
|
|
|
current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
|
2006-03-11 05:27:58 +01:00
|
|
|
|
|
|
|
/* handle each entry in index block */
|
|
|
|
for (j = 0; j < vmd->frames_per_block; j++) {
|
|
|
|
int type;
|
|
|
|
uint32_t size;
|
|
|
|
|
|
|
|
get_buffer(pb, chunk, BYTES_PER_FRAME_RECORD);
|
|
|
|
type = chunk[0];
|
2007-01-19 23:12:59 +01:00
|
|
|
size = AV_RL32(&chunk[2]);
|
2009-01-21 14:26:01 +01:00
|
|
|
if(!size && type != 1)
|
2006-03-11 05:27:58 +01:00
|
|
|
continue;
|
|
|
|
switch(type) {
|
|
|
|
case 1: /* Audio Chunk */
|
2008-02-04 13:13:09 +01:00
|
|
|
if (!st) break;
|
2006-11-19 06:30:43 +01:00
|
|
|
/* first audio chunk contains several audio buffers */
|
2009-01-21 14:25:14 +01:00
|
|
|
vmd->frame_table[total_frames].frame_offset = current_offset;
|
|
|
|
vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
|
|
|
|
vmd->frame_table[total_frames].frame_size = size;
|
|
|
|
memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
|
|
|
|
vmd->frame_table[total_frames].pts = current_audio_pts;
|
|
|
|
total_frames++;
|
2009-01-21 14:27:39 +01:00
|
|
|
if(!current_audio_pts)
|
|
|
|
current_audio_pts += sound_buffers;
|
|
|
|
else
|
|
|
|
current_audio_pts++;
|
2006-03-11 05:27:58 +01:00
|
|
|
break;
|
|
|
|
case 2: /* Video Chunk */
|
|
|
|
vmd->frame_table[total_frames].frame_offset = current_offset;
|
|
|
|
vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
|
2006-11-13 06:18:19 +01:00
|
|
|
vmd->frame_table[total_frames].frame_size = size;
|
2006-03-11 05:27:58 +01:00
|
|
|
memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
|
2009-01-21 14:27:39 +01:00
|
|
|
vmd->frame_table[total_frames].pts = i;
|
2006-03-11 05:27:58 +01:00
|
|
|
total_frames++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current_offset += size;
|
2004-01-02 05:47:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
av_free(raw_frame_table);
|
|
|
|
|
|
|
|
vmd->current_frame = 0;
|
2006-03-11 05:27:58 +01:00
|
|
|
vmd->frame_count = total_frames;
|
2004-01-02 05:47:02 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vmd_read_packet(AVFormatContext *s,
|
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
2007-04-08 22:24:16 +02:00
|
|
|
VmdDemuxContext *vmd = s->priv_data;
|
2007-11-21 08:41:00 +01:00
|
|
|
ByteIOContext *pb = s->pb;
|
2004-01-02 05:47:02 +01:00
|
|
|
int ret = 0;
|
2008-12-14 04:29:33 +01:00
|
|
|
vmd_frame *frame;
|
2004-01-02 05:47:02 +01:00
|
|
|
|
|
|
|
if (vmd->current_frame >= vmd->frame_count)
|
2007-07-19 17:23:32 +02:00
|
|
|
return AVERROR(EIO);
|
2004-01-02 05:47:02 +01:00
|
|
|
|
|
|
|
frame = &vmd->frame_table[vmd->current_frame];
|
|
|
|
/* position the stream (will probably be there already) */
|
|
|
|
url_fseek(pb, frame->frame_offset, SEEK_SET);
|
|
|
|
|
|
|
|
if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
|
2007-07-19 17:21:30 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2005-05-26 22:17:12 +02:00
|
|
|
pkt->pos= url_ftell(pb);
|
2004-01-02 05:47:02 +01:00
|
|
|
memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
|
2008-12-27 18:34:00 +01:00
|
|
|
if(vmd->is_indeo3)
|
|
|
|
ret = get_buffer(pb, pkt->data, frame->frame_size);
|
|
|
|
else
|
|
|
|
ret = get_buffer(pb, pkt->data + BYTES_PER_FRAME_RECORD,
|
|
|
|
frame->frame_size);
|
2004-01-02 05:47:02 +01:00
|
|
|
|
2004-03-14 05:08:11 +01:00
|
|
|
if (ret != frame->frame_size) {
|
|
|
|
av_free_packet(pkt);
|
2007-07-19 17:23:32 +02:00
|
|
|
ret = AVERROR(EIO);
|
2004-03-14 05:08:11 +01:00
|
|
|
}
|
2004-01-02 05:47:02 +01:00
|
|
|
pkt->stream_index = frame->stream_index;
|
2006-11-13 06:18:19 +01:00
|
|
|
pkt->pts = frame->pts;
|
2009-02-16 15:45:05 +01:00
|
|
|
av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
|
2006-11-01 22:09:14 +01:00
|
|
|
(frame->frame_record[0] == 0x02) ? "video" : "audio",
|
|
|
|
frame->frame_size + BYTES_PER_FRAME_RECORD,
|
2006-11-13 06:18:19 +01:00
|
|
|
pkt->pts);
|
2004-01-02 05:47:02 +01:00
|
|
|
|
|
|
|
vmd->current_frame++;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vmd_read_close(AVFormatContext *s)
|
|
|
|
{
|
2007-04-08 22:24:16 +02:00
|
|
|
VmdDemuxContext *vmd = s->priv_data;
|
2004-01-02 05:47:02 +01:00
|
|
|
|
|
|
|
av_free(vmd->frame_table);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 23:03:28 +01:00
|
|
|
AVInputFormat ff_vmd_demuxer = {
|
2004-01-02 05:47:02 +01:00
|
|
|
"vmd",
|
2008-06-03 18:20:54 +02:00
|
|
|
NULL_IF_CONFIG_SMALL("Sierra VMD format"),
|
2004-01-02 05:47:02 +01:00
|
|
|
sizeof(VmdDemuxContext),
|
|
|
|
vmd_probe,
|
|
|
|
vmd_read_header,
|
|
|
|
vmd_read_packet,
|
|
|
|
vmd_read_close,
|
|
|
|
};
|