2004-01-02 06:47:02 +02:00
|
|
|
/*
|
|
|
|
* Sierra VMD Format Demuxer
|
|
|
|
* Copyright (c) 2004 The ffmpeg Project
|
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2004-01-02 06:47:02 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2004-01-02 06:47:02 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2004-01-02 06:47:02 +02: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 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-01-02 06:47:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file sierravmd.c
|
|
|
|
* Sierra VMD file demuxer
|
|
|
|
* by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
|
2004-03-14 06:08:11 +02:00
|
|
|
* for more information on the Sierra VMD file format, visit:
|
|
|
|
* http://www.pcisys.net/~melanson/codecs/
|
2004-01-02 06:47:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avformat.h"
|
|
|
|
|
|
|
|
#define VMD_HEADER_SIZE 0x0330
|
|
|
|
#define BYTES_PER_FRAME_RECORD 16
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int stream_index;
|
2008-10-03 13:16:29 +03:00
|
|
|
int64_t frame_offset;
|
2004-01-02 06:47:02 +02:00
|
|
|
unsigned int frame_size;
|
|
|
|
int64_t pts;
|
|
|
|
int keyframe;
|
|
|
|
unsigned char frame_record[BYTES_PER_FRAME_RECORD];
|
2008-12-14 05:29:33 +02:00
|
|
|
} vmd_frame;
|
2004-01-02 06:47:02 +02:00
|
|
|
|
|
|
|
typedef struct VmdDemuxContext {
|
|
|
|
int video_stream_index;
|
|
|
|
int audio_stream_index;
|
|
|
|
|
|
|
|
unsigned int frame_count;
|
2006-03-11 06:27:58 +02:00
|
|
|
unsigned int frames_per_block;
|
2008-12-14 05:29:33 +02:00
|
|
|
vmd_frame *frame_table;
|
2004-01-02 06:47:02 +02:00
|
|
|
unsigned int current_frame;
|
2008-12-27 19:34:00 +02:00
|
|
|
int is_indeo3;
|
2004-01-02 06:47:02 +02:00
|
|
|
|
2004-03-14 06:08:11 +02:00
|
|
|
int sample_rate;
|
|
|
|
int64_t audio_sample_counter;
|
2006-03-11 06:27:58 +02:00
|
|
|
int skiphdr;
|
2004-03-14 06:08:11 +02:00
|
|
|
|
2004-01-02 06:47:02 +02:00
|
|
|
unsigned char vmd_header[VMD_HEADER_SIZE];
|
|
|
|
} VmdDemuxContext;
|
|
|
|
|
|
|
|
static int vmd_probe(AVProbeData *p)
|
|
|
|
{
|
|
|
|
/* check if the first 2 bytes of the file contain the appropriate size
|
|
|
|
* of a VMD header chunk */
|
2007-01-20 00:12:59 +02:00
|
|
|
if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
|
2004-01-02 06:47:02 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* 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 23:24:16 +03:00
|
|
|
VmdDemuxContext *vmd = s->priv_data;
|
2007-11-21 09:41:00 +02:00
|
|
|
ByteIOContext *pb = s->pb;
|
2008-02-04 14:13:09 +02:00
|
|
|
AVStream *st = NULL, *vst;
|
2004-01-02 06:47:02 +02:00
|
|
|
unsigned int toc_offset;
|
|
|
|
unsigned char *raw_frame_table;
|
|
|
|
int raw_frame_table_size;
|
2008-10-03 13:16:29 +03:00
|
|
|
int64_t current_offset;
|
2006-03-11 06:27:58 +02:00
|
|
|
int i, j;
|
2004-01-02 06:47:02 +02:00
|
|
|
unsigned int total_frames;
|
2006-11-13 07:18:19 +02:00
|
|
|
int64_t pts_inc = 1;
|
|
|
|
int64_t current_video_pts = 0, current_audio_pts = 0;
|
2006-03-11 06:27:58 +02:00
|
|
|
unsigned char chunk[BYTES_PER_FRAME_RECORD];
|
2006-11-13 07:18:19 +02:00
|
|
|
int num, den;
|
2006-11-19 07:30:43 +02:00
|
|
|
int sound_buffers;
|
2004-01-02 06:47:02 +02: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 18:23:32 +03:00
|
|
|
return AVERROR(EIO);
|
2004-01-02 06:47:02 +02:00
|
|
|
|
2008-12-27 19:34:00 +02: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 18:22:42 +02:00
|
|
|
vmd->is_indeo3 = 0;
|
2004-01-02 06:47:02 +02:00
|
|
|
/* start up the decoders */
|
2006-11-13 07:18:19 +02:00
|
|
|
vst = av_new_stream(s, 0);
|
|
|
|
if (!vst)
|
2007-07-19 18:21:30 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2006-11-13 07:18:19 +02:00
|
|
|
av_set_pts_info(vst, 33, 1, 10);
|
|
|
|
vmd->video_stream_index = vst->index;
|
|
|
|
vst->codec->codec_type = CODEC_TYPE_VIDEO;
|
2008-12-27 19:34:00 +02:00
|
|
|
vst->codec->codec_id = vmd->is_indeo3 ? CODEC_ID_INDEO3 : CODEC_ID_VMDVIDEO;
|
2006-11-13 07:18:19 +02:00
|
|
|
vst->codec->codec_tag = 0; /* no fourcc */
|
2007-01-20 00:12:59 +02:00
|
|
|
vst->codec->width = AV_RL16(&vmd->vmd_header[12]);
|
|
|
|
vst->codec->height = AV_RL16(&vmd->vmd_header[14]);
|
2008-12-27 19:34:00 +02:00
|
|
|
if(vmd->is_indeo3 && vst->codec->width > 320){
|
|
|
|
vst->codec->width >>= 1;
|
|
|
|
vst->codec->height >>= 1;
|
|
|
|
}
|
2006-11-13 07:18:19 +02: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 06:47:02 +02:00
|
|
|
|
|
|
|
/* if sample rate is 0, assume no audio */
|
2007-01-20 00:12:59 +02:00
|
|
|
vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
|
2004-03-14 06:08:11 +02:00
|
|
|
if (vmd->sample_rate) {
|
2004-01-02 06:47:02 +02:00
|
|
|
st = av_new_stream(s, 0);
|
|
|
|
if (!st)
|
2007-07-19 18:21:30 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2004-01-02 06:47:02 +02:00
|
|
|
vmd->audio_stream_index = st->index;
|
2005-07-18 01:24:36 +03:00
|
|
|
st->codec->codec_type = CODEC_TYPE_AUDIO;
|
|
|
|
st->codec->codec_id = CODEC_ID_VMDAUDIO;
|
2006-03-11 06:27:58 +02:00
|
|
|
st->codec->codec_tag = 0; /* no fourcc */
|
2006-11-13 07:18:19 +02:00
|
|
|
st->codec->channels = (vmd->vmd_header[811] & 0x80) ? 2 : 1;
|
2005-07-18 01:24:36 +03:00
|
|
|
st->codec->sample_rate = vmd->sample_rate;
|
2007-01-20 00:12:59 +02:00
|
|
|
st->codec->block_align = AV_RL16(&vmd->vmd_header[806]);
|
2005-07-18 01:24:36 +03:00
|
|
|
if (st->codec->block_align & 0x8000) {
|
2008-09-08 17:24:59 +03:00
|
|
|
st->codec->bits_per_coded_sample = 16;
|
2005-07-18 01:24:36 +03:00
|
|
|
st->codec->block_align = -(st->codec->block_align - 0x10000);
|
2006-03-11 06:27:58 +02:00
|
|
|
} else {
|
2008-09-08 17:24:59 +03:00
|
|
|
st->codec->bits_per_coded_sample = 8;
|
2006-03-11 06:27:58 +02:00
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
st->codec->bit_rate = st->codec->sample_rate *
|
2008-09-08 17:24:59 +03:00
|
|
|
st->codec->bits_per_coded_sample * st->codec->channels;
|
2004-03-14 06:08:11 +02:00
|
|
|
|
2006-11-13 07:18:19 +02: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);
|
|
|
|
pts_inc = num;
|
2004-01-02 06:47:02 +02:00
|
|
|
}
|
|
|
|
|
2007-01-20 00:12:59 +02: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 06:27:58 +02:00
|
|
|
url_fseek(pb, toc_offset, SEEK_SET);
|
2004-01-02 06:47:02 +02:00
|
|
|
|
|
|
|
raw_frame_table = NULL;
|
|
|
|
vmd->frame_table = NULL;
|
2007-01-20 00:12:59 +02:00
|
|
|
sound_buffers = AV_RL16(&vmd->vmd_header[808]);
|
2006-03-11 06:27:58 +02:00
|
|
|
raw_frame_table_size = vmd->frame_count * 6;
|
2008-12-14 05:29:33 +02:00
|
|
|
if(vmd->frame_count * vmd->frames_per_block >= UINT_MAX / sizeof(vmd_frame)){
|
2006-05-13 14:37:56 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2008-05-29 01:26:35 +03:00
|
|
|
raw_frame_table = av_malloc(raw_frame_table_size);
|
2008-12-14 05:29:33 +02:00
|
|
|
vmd->frame_table = av_malloc((vmd->frame_count * vmd->frames_per_block + sound_buffers) * sizeof(vmd_frame));
|
2004-01-02 06:47:02 +02:00
|
|
|
if (!raw_frame_table || !vmd->frame_table) {
|
|
|
|
av_free(raw_frame_table);
|
|
|
|
av_free(vmd->frame_table);
|
2007-07-19 18:21:30 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2004-01-02 06:47:02 +02:00
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
if (get_buffer(pb, raw_frame_table, raw_frame_table_size) !=
|
2004-01-02 06:47:02 +02:00
|
|
|
raw_frame_table_size) {
|
|
|
|
av_free(raw_frame_table);
|
|
|
|
av_free(vmd->frame_table);
|
2007-07-19 18:23:32 +03:00
|
|
|
return AVERROR(EIO);
|
2004-01-02 06:47:02 +02:00
|
|
|
}
|
|
|
|
|
2006-03-11 06:27:58 +02:00
|
|
|
total_frames = 0;
|
|
|
|
for (i = 0; i < vmd->frame_count; i++) {
|
|
|
|
|
2007-01-20 00:12:59 +02:00
|
|
|
current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
|
2006-03-11 06:27:58 +02: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-20 00:12:59 +02:00
|
|
|
size = AV_RL32(&chunk[2]);
|
2006-03-11 06:27:58 +02:00
|
|
|
if(!size)
|
|
|
|
continue;
|
|
|
|
switch(type) {
|
|
|
|
case 1: /* Audio Chunk */
|
2008-02-04 14:13:09 +02:00
|
|
|
if (!st) break;
|
2006-11-19 07:30:43 +02:00
|
|
|
/* first audio chunk contains several audio buffers */
|
|
|
|
if(current_audio_pts){
|
2006-11-19 07:32:22 +02: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++;
|
|
|
|
current_audio_pts += pts_inc;
|
2006-11-19 07:30:43 +02:00
|
|
|
}else{
|
|
|
|
uint32_t flags;
|
|
|
|
int k;
|
|
|
|
int noff;
|
|
|
|
int64_t pos;
|
|
|
|
|
|
|
|
pos = url_ftell(pb);
|
|
|
|
url_fseek(pb, current_offset, SEEK_SET);
|
|
|
|
flags = get_le32(pb);
|
|
|
|
noff = 4;
|
|
|
|
url_fseek(pb, pos, SEEK_SET);
|
|
|
|
av_log(s, AV_LOG_DEBUG, "Sound mapping = %08X (%i bufs)\n", flags, sound_buffers);
|
|
|
|
for(k = 0; k < sound_buffers - 1; k++){
|
|
|
|
if(flags & 1) { /* silent block */
|
|
|
|
vmd->frame_table[total_frames].frame_size = 0;
|
|
|
|
}else{
|
|
|
|
vmd->frame_table[total_frames].frame_size = st->codec->block_align + (st->codec->block_align & 1);
|
|
|
|
}
|
|
|
|
noff += vmd->frame_table[total_frames].frame_size;
|
|
|
|
vmd->frame_table[total_frames].frame_offset = current_offset + noff;
|
|
|
|
vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
|
|
|
|
memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
|
|
|
|
vmd->frame_table[total_frames].pts = current_audio_pts;
|
|
|
|
total_frames++;
|
|
|
|
current_audio_pts += pts_inc;
|
|
|
|
flags >>= 1;
|
|
|
|
}
|
|
|
|
}
|
2006-03-11 06:27:58 +02: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 07:18:19 +02:00
|
|
|
vmd->frame_table[total_frames].frame_size = size;
|
2006-03-11 06:27:58 +02:00
|
|
|
memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
|
|
|
|
vmd->frame_table[total_frames].pts = current_video_pts;
|
|
|
|
total_frames++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current_offset += size;
|
2004-01-02 06:47:02 +02:00
|
|
|
}
|
2006-11-13 07:18:19 +02:00
|
|
|
current_video_pts += pts_inc;
|
2004-01-02 06:47:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
av_free(raw_frame_table);
|
|
|
|
|
|
|
|
vmd->current_frame = 0;
|
2006-03-11 06:27:58 +02:00
|
|
|
vmd->frame_count = total_frames;
|
2004-01-02 06:47:02 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vmd_read_packet(AVFormatContext *s,
|
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
VmdDemuxContext *vmd = s->priv_data;
|
2007-11-21 09:41:00 +02:00
|
|
|
ByteIOContext *pb = s->pb;
|
2004-01-02 06:47:02 +02:00
|
|
|
int ret = 0;
|
2008-12-14 05:29:33 +02:00
|
|
|
vmd_frame *frame;
|
2004-01-02 06:47:02 +02:00
|
|
|
|
|
|
|
if (vmd->current_frame >= vmd->frame_count)
|
2007-07-19 18:23:32 +03:00
|
|
|
return AVERROR(EIO);
|
2004-01-02 06:47:02 +02: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 18:21:30 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2005-05-26 23:17:12 +03:00
|
|
|
pkt->pos= url_ftell(pb);
|
2004-01-02 06:47:02 +02:00
|
|
|
memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
|
2008-12-27 19:34:00 +02: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 06:47:02 +02:00
|
|
|
|
2004-03-14 06:08:11 +02:00
|
|
|
if (ret != frame->frame_size) {
|
|
|
|
av_free_packet(pkt);
|
2007-07-19 18:23:32 +03:00
|
|
|
ret = AVERROR(EIO);
|
2004-03-14 06:08:11 +02:00
|
|
|
}
|
2004-01-02 06:47:02 +02:00
|
|
|
pkt->stream_index = frame->stream_index;
|
2006-11-13 07:18:19 +02:00
|
|
|
pkt->pts = frame->pts;
|
2007-08-11 01:40:11 +03:00
|
|
|
av_log(NULL, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
|
2006-11-01 23:09:14 +02:00
|
|
|
(frame->frame_record[0] == 0x02) ? "video" : "audio",
|
|
|
|
frame->frame_size + BYTES_PER_FRAME_RECORD,
|
2006-11-13 07:18:19 +02:00
|
|
|
pkt->pts);
|
2004-01-02 06:47:02 +02:00
|
|
|
|
|
|
|
vmd->current_frame++;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vmd_read_close(AVFormatContext *s)
|
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
VmdDemuxContext *vmd = s->priv_data;
|
2004-01-02 06:47:02 +02:00
|
|
|
|
|
|
|
av_free(vmd->frame_table);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-11 00:14:37 +03:00
|
|
|
AVInputFormat vmd_demuxer = {
|
2004-01-02 06:47:02 +02:00
|
|
|
"vmd",
|
2008-06-03 19:20:54 +03:00
|
|
|
NULL_IF_CONFIG_SMALL("Sierra VMD format"),
|
2004-01-02 06:47:02 +02:00
|
|
|
sizeof(VmdDemuxContext),
|
|
|
|
vmd_probe,
|
|
|
|
vmd_read_header,
|
|
|
|
vmd_read_packet,
|
|
|
|
vmd_read_close,
|
|
|
|
};
|