Working brstm decoder with seemingly perfect looping.

git-svn-id: https://vgmstream.svn.sourceforge.net/svnroot/vgmstream@12 51a99a44-fe44-0410-b1ba-c3e57ba2b86b
This commit is contained in:
halleyscometsw 2008-02-04 15:20:20 +00:00
parent 903422dabd
commit 8e80ba4680
8 changed files with 234 additions and 16 deletions

124
src/fmt/brstm.c Normal file
View File

@ -0,0 +1,124 @@
#include "brstm.h"
#include "../util.h"
VGMSTREAM * init_vgmstream_brstm(const char * const filename) {
VGMSTREAM * vgmstream = NULL;
STREAMFILE * infile = NULL;
coding_t coding_type;
off_t head_offset;
size_t head_length;
off_t coef_offset;
off_t coef_offset1;
off_t coef_offset2;
int codec_number;
int channel_count;
int loop_flag;
off_t start_offset;
/* check extension, case insensitive */
if (strcasecmp("brstm",filename_extension(filename))) goto fail;
/* try to open the file for header reading */
infile = open_streamfile(filename);
if (!infile) goto fail;
/* check header */
if ((uint32_t)read_32bitBE(0,infile)!=0x5253544D ||
(uint32_t)read_32bitBE(4,infile)!=0xFEFF0100)
goto fail;
/* get head offset, check */
head_offset = read_32bitBE(0x10,infile);
if ((uint32_t)read_32bitBE(head_offset,infile)!=0x48454144)
goto fail;
head_length = read_32bitBE(0x14,infile);
/* check type details */
codec_number = read_8bit(head_offset+0x20,infile);
loop_flag = read_8bit(head_offset+0x21,infile);
channel_count = read_8bit(head_offset+0x22,infile);
switch (codec_number) {
case 0:
coding_type = coding_PCM8;
break;
case 1:
coding_type = coding_PCM16BE;
break;
case 2:
coding_type = coding_NGC_DSP;
break;
default:
goto fail;
}
/* TODO: only mono and stereo supported */
if (channel_count < 1 || channel_count > 2) goto fail;
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
if (!vgmstream) goto fail;
/* fill in the vital statistics */
vgmstream->num_samples = read_32bitBE(head_offset+0x2c,infile);
vgmstream->sample_rate = read_16bitBE(head_offset+0x24,infile);
/* channels and loop flag are set by allocate_vgmstream */
vgmstream->loop_start_sample = read_32bitBE(head_offset+0x28,infile);
vgmstream->loop_end_sample = vgmstream->num_samples;
vgmstream->coding_type = coding_type;
vgmstream->layout_type = layout_interleave_shortblock;
vgmstream->meta_type = meta_RSTM;
vgmstream->interleave_block_size = read_32bitBE(head_offset+0x38,infile);
vgmstream->interleave_smallblock_size = read_32bitBE(head_offset+0x48,infile);
coef_offset1=read_32bitBE(head_offset+0x1c,infile);
coef_offset2=read_32bitBE(head_offset+0x10+coef_offset1,infile);
coef_offset=coef_offset2+0x10;
{
int i;
for (i=0;i<16;i++) {
vgmstream->ch[0].adpcm_coef[i]=read_16bitBE(head_offset+coef_offset+i*2,infile);
}
if (vgmstream->channels==2) {
for (i=0;i<16;i++) {
vgmstream->ch[1].adpcm_coef[i]=read_16bitBE(head_offset+coef_offset+0x38+i*2,infile);
}
}
}
start_offset = read_32bitBE(head_offset+0x30,infile);
close_streamfile(infile); infile=NULL;
/* open the file for reading by each channel */
{
int i;
for (i=0;i<channel_count;i++) {
vgmstream->ch[i].streamfile = open_streamfile_buffer(filename,
vgmstream->interleave_block_size>0?
vgmstream->interleave_block_size:
0x1000);
if (!vgmstream->ch[i].streamfile) goto fail;
vgmstream->ch[i].channel_start_offset=
vgmstream->ch[i].offset=
start_offset + i*vgmstream->interleave_block_size;
}
}
return vgmstream;
/* clean up anything we may have opened */
fail:
if (infile) close_streamfile(infile);
if (vgmstream) close_vgmstream(vgmstream);
return NULL;
}

8
src/fmt/brstm.h Normal file
View File

@ -0,0 +1,8 @@
#include "../vgmstream.h"
#ifndef _BRSTM_H
#define _BRSTM_H
VGMSTREAM * init_vgmstream_brstm(const char * const filename);
#endif

8
src/fmt/gcdsp.h Normal file
View File

@ -0,0 +1,8 @@
#include "../vgmstream.h"
#ifndef _GCDSP_H
#define _GCDSP_H
void decode_gcdsp(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do);
#endif

37
src/fmt/gcdsp_decoder.c Normal file
View File

@ -0,0 +1,37 @@
#include "gcdsp.h"
#include "../util.h"
void decode_gcdsp(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do) {
int i=first_sample;
int32_t sample_count;
int framesin = first_sample/14;
int8_t header = read_8bit(framesin*8+stream->offset,stream->streamfile);
int32_t scale = 1 << (header & 0xf);
int coef_index = (header >> 4) & 0xf;
int32_t hist1 = stream->adpcm_history1_16;
int32_t hist2 = stream->adpcm_history2_16;
int coef1 = stream->adpcm_coef[coef_index*2];
int coef2 = stream->adpcm_coef[coef_index*2+1];
first_sample = first_sample%14;
for (i=first_sample,sample_count=0; i<first_sample+samples_to_do; i++,sample_count+=channelspacing) {
int sample_byte = read_8bit(framesin*8+stream->offset+1+i/2,stream->streamfile);
outbuf[sample_count] = clamp16((
(((i&1?
get_low_nibble_signed(sample_byte):
get_high_nibble_signed(sample_byte)
) * scale)<<11) + 1024 +
(coef1 * hist1 + coef2 * hist2))>>11
);
hist2 = hist1;
hist1 = outbuf[sample_count];
}
stream->adpcm_history1_16 = hist1;
stream->adpcm_history2_16 = hist2;
}

View File

@ -1,21 +1,36 @@
#include "interleave.h"
#include "adx.h"
#include "gcdsp.h"
void render_vgmstream_interleave(sample * buffer, int32_t sample_count, VGMSTREAM * vgmstream) {
int samples_written=0;
int frame_size;
int samples_per_frame;
int samples_this_block;
switch (vgmstream->coding_type) {
case coding_CRI_ADX:
frame_size=18;
samples_per_frame=32;
break;
case coding_NGC_DSP:
frame_size=8;
samples_per_frame=14;
break;
}
samples_this_block = vgmstream->interleave_block_size / frame_size * samples_per_frame;
if (vgmstream->layout_type == layout_interleave_shortblock &&
vgmstream->current_sample - vgmstream->samples_into_block + samples_this_block> vgmstream->num_samples) {
samples_this_block = vgmstream->interleave_smallblock_size / frame_size * samples_per_frame;
}
while (samples_written<sample_count) {
int samples_to_do;
int chan;
int samples_this_block;
int samples_left_this_block;
switch (vgmstream->coding_type) {
case coding_CRI_ADX:
samples_this_block = vgmstream->interleave_block_size / 18 * 32;
break;
}
/*samples_this_block -= vgmstream->samples_into_block;*/
samples_left_this_block = samples_this_block - vgmstream->samples_into_block;
samples_to_do = samples_left_this_block;
@ -29,6 +44,7 @@ void render_vgmstream_interleave(sample * buffer, int32_t sample_count, VGMSTREA
/*
switch (vgmstream->coding_type) {
case coding_CRI_ADX:
case coding_NGC_DSP:
{
int i;
for (i=0;i<vgmstream->channels;i++) {
@ -45,6 +61,8 @@ void render_vgmstream_interleave(sample * buffer, int32_t sample_count, VGMSTREA
memcpy(vgmstream->ch,vgmstream->loop_ch,sizeof(VGMSTREAMCHANNEL)*vgmstream->channels);
vgmstream->current_sample=vgmstream->loop_sample;
vgmstream->samples_into_block=vgmstream->loop_samples_into_block;
samples_this_block = vgmstream->interleave_block_size / frame_size * samples_per_frame;
continue; /* recalculate stuff */
}
@ -73,6 +91,8 @@ void render_vgmstream_interleave(sample * buffer, int32_t sample_count, VGMSTREA
}
if ((vgmstream->samples_into_block%samples_per_frame)+samples_to_do>samples_per_frame) samples_to_do=samples_per_frame-(vgmstream->samples_into_block%samples_per_frame);
if (samples_written+samples_to_do > sample_count)
samples_to_do=sample_count-samples_written;
@ -85,16 +105,33 @@ void render_vgmstream_interleave(sample * buffer, int32_t sample_count, VGMSTREA
}
break;
}
vgmstream->samples_into_block+=samples_to_do;
if (vgmstream->samples_into_block==samples_this_block) {
for (chan=0;chan<vgmstream->channels;chan++)
vgmstream->ch[chan].offset+=vgmstream->interleave_block_size*vgmstream->channels;
vgmstream->samples_into_block=0;
case coding_NGC_DSP:
for (chan=0;chan<vgmstream->channels;chan++) {
decode_gcdsp(&vgmstream->ch[chan],buffer+samples_written*vgmstream->channels+chan,
vgmstream->channels,vgmstream->samples_into_block,
samples_to_do);
}
break;
}
samples_written += samples_to_do;
vgmstream->current_sample += samples_to_do;
vgmstream->samples_into_block+=samples_to_do;
if (vgmstream->samples_into_block==samples_this_block) {
if (vgmstream->layout_type == layout_interleave_shortblock &&
vgmstream->current_sample + samples_this_block > vgmstream->num_samples) {
samples_this_block = vgmstream->interleave_smallblock_size / frame_size * samples_per_frame;
for (chan=0;chan<vgmstream->channels;chan++)
vgmstream->ch[chan].offset+=vgmstream->interleave_block_size*(vgmstream->channels-chan)+vgmstream->interleave_smallblock_size*chan;
} else {
for (chan=0;chan<vgmstream->channels;chan++)
vgmstream->ch[chan].offset+=vgmstream->interleave_block_size*vgmstream->channels;
}
vgmstream->samples_into_block=0;
}
}
}

View File

@ -1,14 +1,16 @@
#include "vgmstream.h"
#include "fmt/adx.h"
#include "fmt/brstm.h"
#include "fmt/interleave.h"
/*
* List of functions that will recognize files. These should correspond pretty
* directly to the metadata types
*/
#define INIT_VGMSTREAM_FCNS 1
#define INIT_VGMSTREAM_FCNS 2
VGMSTREAM * (*init_vgmstream_fcns[INIT_VGMSTREAM_FCNS])(const char * const) = {
init_vgmstream_adx,
init_vgmstream_brstm,
};
/* format detection and VGMSTREAM setup */
@ -100,6 +102,7 @@ int32_t get_vgmstream_play_samples(double looptimes, double fadetime, VGMSTREAM
void render_vgmstream(sample * buffer, int32_t sample_count, VGMSTREAM * vgmstream) {
switch (vgmstream->layout_type) {
case layout_interleave:
case layout_interleave_shortblock:
render_vgmstream_interleave(buffer,sample_count,vgmstream);
break;
}

View File

@ -1,6 +1,6 @@
CFLAGS=-lm -O3
test: test.c ../src/streamfile.c ../src/vgmstream.c ../src/util.c ../src/fmt/adx_header.c ../src/fmt/adx_decoder.c ../src/fmt/interleave.c
test: test.c ../src/streamfile.c ../src/vgmstream.c ../src/util.c ../src/fmt/adx_header.c ../src/fmt/adx_decoder.c ../src/fmt/gcdsp_decoder.c ../src/fmt/brstm.c ../src/fmt/interleave.c
clean:
rm test

View File

@ -71,6 +71,7 @@ int main(int argc, char ** argv) {
}
}
printf("decoding %s\n",argv[optind]);
printf("sample rate %d Hz\n",s->sample_rate);
printf("channels: %d\n",s->channels);
if (s->loop_flag) {