From 2d3376e47362a39be2540e65766bd714196ec6b2 Mon Sep 17 00:00:00 2001 From: halleyscometsw Date: Tue, 5 Feb 2008 07:38:00 +0000 Subject: [PATCH] NDS STRM format support (only decoding PCM for now) git-svn-id: https://vgmstream.svn.sourceforge.net/svnroot/vgmstream@20 51a99a44-fe44-0410-b1ba-c3e57ba2b86b --- src/coding/pcm_decoder.c | 2 +- src/meta/brstm.c | 30 ++++++----- src/meta/nds_strm.c | 107 +++++++++++++++++++++++++++++++++++++++ src/meta/nds_strm.h | 8 +++ src/vgmstream.c | 7 ++- test/Makefile | 2 +- 6 files changed, 140 insertions(+), 16 deletions(-) create mode 100644 src/meta/nds_strm.c create mode 100644 src/meta/nds_strm.h diff --git a/src/coding/pcm_decoder.c b/src/coding/pcm_decoder.c index f5c837d3..d03c1a63 100644 --- a/src/coding/pcm_decoder.c +++ b/src/coding/pcm_decoder.c @@ -24,6 +24,6 @@ void decode_pcm8(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t sample_count; for (i=first_sample,sample_count=0; ioffset+i,stream->streamfile); + outbuf[sample_count]=read_8bit(stream->offset+i,stream->streamfile)*0x100; } } diff --git a/src/meta/brstm.c b/src/meta/brstm.c index 43e701c0..181bc903 100644 --- a/src/meta/brstm.c +++ b/src/meta/brstm.c @@ -9,9 +9,6 @@ VGMSTREAM * init_vgmstream_brstm(const char * const filename) { 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; @@ -26,13 +23,13 @@ VGMSTREAM * init_vgmstream_brstm(const char * const filename) { if (!infile) goto fail; /* check header */ - if ((uint32_t)read_32bitBE(0,infile)!=0x5253544D || + if ((uint32_t)read_32bitBE(0,infile)!=0x5253544D || /* "RSTM" */ (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) + if ((uint32_t)read_32bitBE(head_offset,infile)!=0x48454144) /* "HEAD" */ goto fail; head_length = read_32bitBE(0x14,infile); @@ -80,12 +77,16 @@ VGMSTREAM * init_vgmstream_brstm(const char * const filename) { 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; - - { + if (vgmstream->coding_type == coding_NGC_DSP) { + off_t coef_offset; + off_t coef_offset1; + off_t coef_offset2; int i; + + coef_offset1=read_32bitBE(head_offset+0x1c,infile); + coef_offset2=read_32bitBE(head_offset+0x10+coef_offset1,infile); + coef_offset=coef_offset2+0x10; + for (i=0;i<16;i++) { vgmstream->ch[0].adpcm_coef[i]=read_16bitBE(head_offset+coef_offset+i*2,infile); } @@ -105,10 +106,13 @@ VGMSTREAM * init_vgmstream_brstm(const char * const filename) { { int i; for (i=0;ich[i].streamfile = open_streamfile_buffer(filename, - vgmstream->interleave_block_size>0? - vgmstream->interleave_block_size: + if (vgmstream->layout_type==layout_interleave_shortblock) + vgmstream->ch[i].streamfile = open_streamfile_buffer(filename, + vgmstream->interleave_block_size); + else + vgmstream->ch[i].streamfile = open_streamfile_buffer(filename, 0x1000); + if (!vgmstream->ch[i].streamfile) goto fail; vgmstream->ch[i].channel_start_offset= diff --git a/src/meta/nds_strm.c b/src/meta/nds_strm.c new file mode 100644 index 00000000..6a397c66 --- /dev/null +++ b/src/meta/nds_strm.c @@ -0,0 +1,107 @@ +#include "nds_strm.h" +#include "../util.h" + +VGMSTREAM * init_vgmstream_nds_strm(const char * const filename) { + VGMSTREAM * vgmstream = NULL; + STREAMFILE * infile = NULL; + + coding_t coding_type; + + int codec_number; + int channel_count; + int loop_flag; + + off_t start_offset; + + /* check extension, case insensitive */ + if (strcasecmp("strm",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)!=0x5354524D || + (uint32_t)read_32bitBE(4,infile)!=0xFFFE0001) + goto fail; + + /* check for HEAD section */ + if ((uint32_t)read_32bitBE(0x10,infile)!=0x48454144 || /* "HEAD" */ + (uint32_t)read_32bitLE(0x14,infile)!=0x50) /* 0x50-sized head is all I've seen */ + goto fail; + + /* check type details */ + codec_number = read_8bit(0x18,infile); + loop_flag = read_8bit(0x19,infile); + channel_count = read_8bit(0x1a,infile); + + switch (codec_number) { + case 0: + coding_type = coding_PCM8; + break; + case 1: + coding_type = coding_PCM16LE; + break; + case 2: + coding_type = coding_NDS_IMA; + 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_32bitLE(0x24,infile); + vgmstream->sample_rate = read_16bitLE(0x1c,infile); + /* channels and loop flag are set by allocate_vgmstream */ + vgmstream->loop_start_sample = read_32bitLE(0x20,infile); + vgmstream->loop_end_sample = vgmstream->num_samples; + + vgmstream->coding_type = coding_type; + vgmstream->meta_type = meta_STRM; + + vgmstream->interleave_block_size = read_32bitLE(0x30,infile); + vgmstream->interleave_smallblock_size = read_32bitLE(0x38,infile); + + if (channel_count==1 || coding_type==coding_PCM8 || coding_type==coding_PCM16LE) + vgmstream->layout_type = layout_none; + else + vgmstream->layout_type = layout_interleave_shortblock; + + start_offset = read_32bitLE(0x28,infile); + + close_streamfile(infile); infile=NULL; + + /* open the file for reading by each channel */ + { + int i; + for (i=0;ilayout_type==layout_interleave_shortblock) + vgmstream->ch[i].streamfile = open_streamfile_buffer(filename, + vgmstream->interleave_block_size); + else + vgmstream->ch[i].streamfile = open_streamfile_buffer(filename, + 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; +} diff --git a/src/meta/nds_strm.h b/src/meta/nds_strm.h new file mode 100644 index 00000000..76e82d68 --- /dev/null +++ b/src/meta/nds_strm.h @@ -0,0 +1,8 @@ +#include "../vgmstream.h" + +#ifndef _NDS_STRM_H +#define _NDS_STRM_H + +VGMSTREAM * init_vgmstream_nds_strm(const char * const filename); + +#endif diff --git a/src/vgmstream.c b/src/vgmstream.c index aace7401..2f2c8dfb 100644 --- a/src/vgmstream.c +++ b/src/vgmstream.c @@ -1,6 +1,7 @@ #include "vgmstream.h" #include "meta/adx_header.h" #include "meta/brstm.h" +#include "meta/nds_strm.h" #include "layout/interleave.h" #include "layout/nolayout.h" #include "coding/adx_decoder.h" @@ -11,10 +12,11 @@ * List of functions that will recognize files. These should correspond pretty * directly to the metadata types */ -#define INIT_VGMSTREAM_FCNS 2 +#define INIT_VGMSTREAM_FCNS 3 VGMSTREAM * (*init_vgmstream_fcns[INIT_VGMSTREAM_FCNS])(const char * const) = { init_vgmstream_adx, init_vgmstream_brstm, + init_vgmstream_nds_strm, }; /* format detection and VGMSTREAM setup */ @@ -264,6 +266,9 @@ void describe_vgmstream(VGMSTREAM * vgmstream) { case meta_RSTM: printf("RSTM header"); break; + case meta_STRM: + printf("NDS STRM header"); + break; case meta_ADX_03: printf("ADX header type 03"); break; diff --git a/test/Makefile b/test/Makefile index 8d49b646..ebb979e7 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,6 @@ CFLAGS=-lm -O3 -test: test.c ../src/streamfile.c ../src/vgmstream.c ../src/util.c ../src/meta/adx_header.c ../src/coding/adx_decoder.c ../src/coding/gcdsp_decoder.c ../src/meta/brstm.c ../src/layout/interleave.c ../src/layout/nolayout.c ../src/coding/pcm_decoder.c +test: test.c ../src/streamfile.c ../src/vgmstream.c ../src/util.c ../src/meta/adx_header.c ../src/coding/adx_decoder.c ../src/coding/gcdsp_decoder.c ../src/meta/brstm.c ../src/layout/interleave.c ../src/layout/nolayout.c ../src/coding/pcm_decoder.c ../src/meta/nds_strm.c clean: rm test