More correct demuxing and timestamp setting fot Sierra VMD

Originally committed as revision 5148 to svn://svn.ffmpeg.org/ffmpeg/trunk
This commit is contained in:
Kostya Shishkov 2006-03-11 04:27:58 +00:00
parent 79d15fd4db
commit d82da3a880
1 changed files with 63 additions and 57 deletions

View File

@ -49,6 +49,7 @@ typedef struct VmdDemuxContext {
unsigned int audio_channels; unsigned int audio_channels;
unsigned int frame_count; unsigned int frame_count;
unsigned int frames_per_block;
vmd_frame_t *frame_table; vmd_frame_t *frame_table;
unsigned int current_frame; unsigned int current_frame;
@ -56,6 +57,7 @@ typedef struct VmdDemuxContext {
int64_t audio_sample_counter; int64_t audio_sample_counter;
int audio_frame_divisor; int audio_frame_divisor;
int audio_block_align; int audio_block_align;
int skiphdr;
unsigned char vmd_header[VMD_HEADER_SIZE]; unsigned char vmd_header[VMD_HEADER_SIZE];
} VmdDemuxContext; } VmdDemuxContext;
@ -87,6 +89,10 @@ static int vmd_calculate_audio_duration(unsigned char *audio_chunk,
if (audio_chunk_size < 16) if (audio_chunk_size < 16)
return 0; return 0;
if (audio_chunk_size == block_align + 16)
return block_align;
if (audio_chunk_size == block_align + 17)
return block_align;
sound_flags = LE_32(p); sound_flags = LE_32(p);
p += 4; p += 4;
@ -96,6 +102,7 @@ static int vmd_calculate_audio_duration(unsigned char *audio_chunk,
p += block_align; p += block_align;
sound_flags >>= 1; sound_flags >>= 1;
} }
av_log(NULL,0,"Got %i samples for size %i map %08X\n", total_samples, audio_chunk_size, LE_32(audio_chunk));
return total_samples; return total_samples;
} }
@ -109,12 +116,13 @@ static int vmd_read_header(AVFormatContext *s,
unsigned int toc_offset; unsigned int toc_offset;
unsigned char *raw_frame_table; unsigned char *raw_frame_table;
int raw_frame_table_size; int raw_frame_table_size;
unsigned char *current_frame_record;
offset_t current_offset; offset_t current_offset;
int i; int i, j;
unsigned int total_frames; unsigned int total_frames;
int64_t video_pts_inc; int64_t video_pts_inc = 0;
int64_t current_video_pts = 0; int64_t current_video_pts = 0;
unsigned char chunk[BYTES_PER_FRAME_RECORD];
int lastframe = 0;
/* fetch the main header, including the 2 header length bytes */ /* fetch the main header, including the 2 header length bytes */
url_fseek(pb, 0, SEEK_SET); url_fseek(pb, 0, SEEK_SET);
@ -136,6 +144,8 @@ static int vmd_read_header(AVFormatContext *s,
st->codec->codec_tag = 0; /* no fourcc */ st->codec->codec_tag = 0; /* no fourcc */
st->codec->width = LE_16(&vmd->vmd_header[12]); st->codec->width = LE_16(&vmd->vmd_header[12]);
st->codec->height = LE_16(&vmd->vmd_header[14]); st->codec->height = LE_16(&vmd->vmd_header[14]);
st->codec->time_base.num = 1;
st->codec->time_base.den = 10;
st->codec->extradata_size = VMD_HEADER_SIZE; st->codec->extradata_size = VMD_HEADER_SIZE;
st->codec->extradata = av_mallocz(VMD_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE); st->codec->extradata = av_mallocz(VMD_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
memcpy(st->codec->extradata, vmd->vmd_header, VMD_HEADER_SIZE); memcpy(st->codec->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
@ -150,48 +160,43 @@ static int vmd_read_header(AVFormatContext *s,
vmd->audio_stream_index = st->index; vmd->audio_stream_index = st->index;
st->codec->codec_type = CODEC_TYPE_AUDIO; st->codec->codec_type = CODEC_TYPE_AUDIO;
st->codec->codec_id = CODEC_ID_VMDAUDIO; st->codec->codec_id = CODEC_ID_VMDAUDIO;
st->codec->codec_tag = 0; /* no codec tag */ st->codec->codec_tag = 0; /* no fourcc */
st->codec->channels = (vmd->vmd_header[811] & 0x80) ? 2 : 1; st->codec->channels = vmd->audio_channels = (vmd->vmd_header[811] & 0x80) ? 2 : 1;
st->codec->sample_rate = vmd->sample_rate; st->codec->sample_rate = vmd->sample_rate;
st->codec->block_align = vmd->audio_block_align = st->codec->block_align = vmd->audio_block_align =
LE_16(&vmd->vmd_header[806]); LE_16(&vmd->vmd_header[806]);
if (st->codec->block_align & 0x8000) { if (st->codec->block_align & 0x8000) {
st->codec->bits_per_sample = 16; st->codec->bits_per_sample = 16;
st->codec->block_align = -(st->codec->block_align - 0x10000); st->codec->block_align = -(st->codec->block_align - 0x10000);
} else vmd->audio_block_align = -(vmd->audio_block_align - 0x10000);
st->codec->bits_per_sample = 16; } else {
// st->codec->bits_per_sample = 8; st->codec->bits_per_sample = 8;
}
st->codec->bit_rate = st->codec->sample_rate * st->codec->bit_rate = st->codec->sample_rate *
st->codec->bits_per_sample * st->codec->channels; st->codec->bits_per_sample * st->codec->channels;
/* for calculating pts */ /* for calculating pts */
vmd->audio_frame_divisor = st->codec->bits_per_sample / 8 / vmd->audio_frame_divisor = st->codec->channels;
st->codec->channels;
video_pts_inc = 90000; video_pts_inc = 90000;
video_pts_inc *= st->codec->block_align; video_pts_inc *= st->codec->block_align;
video_pts_inc /= st->codec->sample_rate; video_pts_inc /= st->codec->sample_rate;
video_pts_inc /= st->codec->channels;
} else { } else {
/* if no audio, assume 10 frames/second */ /* if no audio, assume 10 frames/second */
video_pts_inc = 90000 / 10; video_pts_inc = 90000 / 10;
} }
/* skip over the offset table and load the table of contents; don't
* care about the offset table since demuxer will calculate those
* independently */
toc_offset = LE_32(&vmd->vmd_header[812]); toc_offset = LE_32(&vmd->vmd_header[812]);
vmd->frame_count = LE_16(&vmd->vmd_header[6]); vmd->frame_count = LE_16(&vmd->vmd_header[6]);
url_fseek(pb, toc_offset + vmd->frame_count * 6, SEEK_SET); vmd->frames_per_block = LE_16(&vmd->vmd_header[18]);
url_fseek(pb, toc_offset, SEEK_SET);
/* each on-disk VMD frame has an audio part and a video part; demuxer
* accounts them separately */
if(vmd->sample_rate)
vmd->frame_count *= 2;
raw_frame_table = NULL; raw_frame_table = NULL;
vmd->frame_table = NULL; vmd->frame_table = NULL;
raw_frame_table_size = vmd->frame_count * BYTES_PER_FRAME_RECORD; raw_frame_table_size = vmd->frame_count * 6;
raw_frame_table = av_malloc(raw_frame_table_size); raw_frame_table = av_malloc(raw_frame_table_size);
vmd->frame_table = av_malloc(vmd->frame_count * sizeof(vmd_frame_t)); vmd->frame_table = av_malloc(vmd->frame_count * vmd->frames_per_block * sizeof(vmd_frame_t));
if (!raw_frame_table || !vmd->frame_table) { if (!raw_frame_table || !vmd->frame_table) {
av_free(raw_frame_table); av_free(raw_frame_table);
av_free(vmd->frame_table); av_free(vmd->frame_table);
@ -204,50 +209,51 @@ static int vmd_read_header(AVFormatContext *s,
return AVERROR_IO; return AVERROR_IO;
} }
current_offset = LE_32(&vmd->vmd_header[20]); total_frames = 0;
current_frame_record = raw_frame_table; for (i = 0; i < vmd->frame_count; i++) {
total_frames = vmd->frame_count;
i = 0;
while (total_frames--) {
/* if the frame size is 0, do not count the frame and bring the current_offset = LE_32(&raw_frame_table[6 * i + 2]);
* total frame count down */
// note, we limit the size to 1Gb to ensure that we dont end up overflowing the size integer used to allocate the memory
vmd->frame_table[i].frame_size = LE_32(&current_frame_record[2]) & 0x3FFFFFFF;
/* this logic is present so that 0-length audio chunks are not /* handle each entry in index block */
* accounted */ for (j = 0; j < vmd->frames_per_block; j++) {
if (!vmd->frame_table[i].frame_size) { int type;
vmd->frame_count--; /* one less frame to count */ uint32_t size;
current_frame_record += BYTES_PER_FRAME_RECORD;
continue; get_buffer(pb, chunk, BYTES_PER_FRAME_RECORD);
type = chunk[0];
size = LE_32(&chunk[2]);
if(!size)
continue;
switch(type) {
case 1: /* Audio Chunk */
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);
total_frames++;
break;
case 2: /* Video Chunk */
vmd->frame_table[total_frames].frame_offset = current_offset;
vmd->frame_table[total_frames].frame_size = size;
vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
vmd->frame_table[total_frames].pts = current_video_pts;
if (lastframe) {
vmd->frame_table[lastframe].pts = current_video_pts - video_pts_inc;
}
lastframe = total_frames;
total_frames++;
break;
}
current_offset += size;
} }
current_video_pts += video_pts_inc;
if (current_frame_record[0] == 0x02)
vmd->frame_table[i].stream_index = vmd->video_stream_index;
else
vmd->frame_table[i].stream_index = vmd->audio_stream_index;
vmd->frame_table[i].frame_offset = current_offset;
current_offset += vmd->frame_table[i].frame_size;
memcpy(vmd->frame_table[i].frame_record, current_frame_record,
BYTES_PER_FRAME_RECORD);
/* figure out the pts for this frame */
if (current_frame_record[0] == 0x02) {
vmd->frame_table[i].pts = current_video_pts;
current_video_pts += video_pts_inc;
} else if (current_frame_record[0] == 0x01) {
/* figure out the pts during the dispatch phase */
vmd->frame_table[i].pts = 0;
}
current_frame_record += BYTES_PER_FRAME_RECORD;
i++;
} }
av_free(raw_frame_table); av_free(raw_frame_table);
vmd->current_frame = 0; vmd->current_frame = 0;
vmd->frame_count = total_frames;
return 0; return 0;
} }
@ -285,7 +291,7 @@ static int vmd_read_packet(AVFormatContext *s,
pkt->pts = vmd->audio_sample_counter; pkt->pts = vmd->audio_sample_counter;
pkt->pts *= 90000; pkt->pts *= 90000;
pkt->pts /= vmd->sample_rate; pkt->pts /= vmd->sample_rate;
// pkt->pts /= vmd->audio_frame_divisor; pkt->pts /= vmd->audio_channels;
vmd->audio_sample_counter += vmd_calculate_audio_duration( vmd->audio_sample_counter += vmd_calculate_audio_duration(
pkt->data, pkt->size, vmd->audio_block_align); pkt->data, pkt->size, vmd->audio_block_align);