2008-05-06 05:35:37 +02:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../coding/coding.h"
|
2008-04-02 18:11:53 +02:00
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
/* .dsp w/ Cstr header, seen in Star Fox Assault and Donkey Konga */
|
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
VGMSTREAM * init_vgmstream_Cstr(STREAMFILE *streamFile) {
|
2008-04-02 18:11:53 +02:00
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2013-05-27 05:55:50 +02:00
|
|
|
char filename[PATH_LIMIT];
|
2008-04-02 18:11:53 +02:00
|
|
|
|
|
|
|
int loop_flag;
|
|
|
|
off_t start_offset;
|
|
|
|
off_t first_data;
|
|
|
|
off_t loop_offset;
|
|
|
|
size_t interleave;
|
|
|
|
int loop_adjust;
|
2008-05-19 04:38:57 +02:00
|
|
|
int double_loop_end = 0;
|
2008-04-02 18:11:53 +02:00
|
|
|
|
|
|
|
/* check extension, case insensitive */
|
2008-05-20 17:18:38 +02:00
|
|
|
streamFile->get_name(streamFile,filename,sizeof(filename));
|
2008-04-02 18:11:53 +02:00
|
|
|
if (strcasecmp("dsp",filename_extension(filename))) goto fail;
|
|
|
|
|
|
|
|
/* check header */
|
2008-05-20 17:18:38 +02:00
|
|
|
if ((uint32_t)read_32bitBE(0,streamFile)!=0x43737472) /* "Cstr" */
|
2008-04-02 18:11:53 +02:00
|
|
|
goto fail;
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"header ok\n");
|
|
|
|
#endif
|
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_8bit(0x1b,streamFile)==1) {
|
2008-05-17 00:57:20 +02:00
|
|
|
/* mono version, much simpler to handle */
|
|
|
|
/* Only seen in R Racing Evolution radio sfx */
|
|
|
|
|
|
|
|
start_offset = 0x80;
|
2008-05-20 17:18:38 +02:00
|
|
|
loop_flag = read_16bitBE(0x2c,streamFile);
|
2008-05-17 00:57:20 +02:00
|
|
|
|
|
|
|
/* check initial predictor/scale */
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_16bitBE(0x5e,streamFile) != (uint8_t)read_8bit(start_offset,streamFile))
|
2008-05-17 00:57:20 +02:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* check type==0 and gain==0 */
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_16bitBE(0x2e,streamFile) || read_16bitBE(0x5c,streamFile))
|
2008-05-17 00:57:20 +02:00
|
|
|
goto fail;
|
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
loop_offset = start_offset+read_32bitBE(0x10,streamFile);
|
2008-05-17 00:57:20 +02:00
|
|
|
if (loop_flag) {
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_16bitBE(0x64,streamFile) != (uint8_t)read_8bit(loop_offset,streamFile)) goto fail;
|
2008-05-17 00:57:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
|
|
|
|
vgmstream = allocate_vgmstream(1,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
/* fill in the vital statistics */
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->sample_rate = read_32bitBE(0x28,streamFile);
|
|
|
|
vgmstream->num_samples = read_32bitBE(0x20,streamFile);
|
2008-05-17 00:57:20 +02:00
|
|
|
|
|
|
|
if (loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = dsp_nibbles_to_samples(
|
2008-05-20 17:18:38 +02:00
|
|
|
read_32bitBE(0x30,streamFile));
|
2008-05-17 00:57:20 +02:00
|
|
|
vgmstream->loop_end_sample = dsp_nibbles_to_samples(
|
2008-05-20 17:18:38 +02:00
|
|
|
read_32bitBE(0x34,streamFile))+1;
|
2008-05-17 00:57:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vgmstream->coding_type = coding_NGC_DSP;
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
vgmstream->meta_type = meta_DSP_CSTR;
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0;i<16;i++)
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->ch[0].adpcm_coef[i]=read_16bitBE(0x3c+i*2,streamFile);
|
2008-05-17 00:57:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* open the file for reading by each channel */
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->ch[0].streamfile = streamFile->open(streamFile,filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2008-05-17 00:57:20 +02:00
|
|
|
|
|
|
|
if (!vgmstream->ch[0].streamfile) goto fail;
|
|
|
|
|
|
|
|
vgmstream->ch[0].channel_start_offset=
|
|
|
|
vgmstream->ch[0].offset=
|
|
|
|
start_offset;
|
|
|
|
|
|
|
|
return vgmstream;
|
2008-05-19 04:38:57 +02:00
|
|
|
} /* end mono */
|
2008-05-17 00:57:20 +02:00
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
interleave = read_16bitBE(0x06,streamFile);
|
2008-04-02 18:11:53 +02:00
|
|
|
start_offset = 0xe0;
|
2008-05-20 17:18:38 +02:00
|
|
|
first_data = start_offset+read_32bitBE(0x0c,streamFile);
|
|
|
|
loop_flag = read_16bitBE(0x2c,streamFile);
|
2008-04-02 18:11:53 +02:00
|
|
|
|
|
|
|
if (!loop_flag) {
|
|
|
|
/* Nonlooped tracks seem to follow no discernable pattern
|
|
|
|
* with where they actually start.
|
|
|
|
* But! with the magic of initial p/s redundancy, we can guess.
|
|
|
|
*/
|
|
|
|
while (first_data<start_offset+0x800 &&
|
2008-05-20 17:18:38 +02:00
|
|
|
(read_16bitBE(0x5e,streamFile) != (uint8_t)read_8bit(first_data,streamFile) ||
|
|
|
|
read_16bitBE(0xbe,streamFile) != (uint8_t)read_8bit(first_data+interleave,streamFile)))
|
2008-04-02 18:11:53 +02:00
|
|
|
first_data+=8;
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"guessed first_data at %#x\n",first_data);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check initial predictor/scale */
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_16bitBE(0x5e,streamFile) != (uint8_t)read_8bit(first_data,streamFile))
|
2008-04-02 18:11:53 +02:00
|
|
|
goto fail;
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_16bitBE(0xbe,streamFile) != (uint8_t)read_8bit(first_data+interleave,streamFile))
|
2008-04-02 18:11:53 +02:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"p/s ok\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* check type==0 and gain==0 */
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_16bitBE(0x2e,streamFile) || read_16bitBE(0x5c,streamFile))
|
2008-04-02 18:11:53 +02:00
|
|
|
goto fail;
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_16bitBE(0x8e,streamFile) || read_16bitBE(0xbc,streamFile))
|
2008-04-02 18:11:53 +02:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"type & gain ok\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* check for loop flag agreement */
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_16bitBE(0x2c,streamFile) != read_16bitBE(0x8c,streamFile))
|
2008-04-02 18:11:53 +02:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"loop flags agree\n");
|
|
|
|
#endif
|
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
loop_offset = start_offset+read_32bitBE(0x10,streamFile)*2;
|
2008-04-02 18:11:53 +02:00
|
|
|
if (loop_flag) {
|
|
|
|
int loops_ok=0;
|
|
|
|
/* check loop predictor/scale */
|
|
|
|
/* some fuzz allowed */
|
|
|
|
for (loop_adjust=0;loop_adjust>=-0x10;loop_adjust-=8) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"looking for loop p/s at %#x,%#x\n",loop_offset-interleave+loop_adjust,loop_offset+loop_adjust);
|
|
|
|
#endif
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_16bitBE(0x64,streamFile) == (uint8_t)read_8bit(loop_offset-interleave+loop_adjust,streamFile) &&
|
|
|
|
read_16bitBE(0xc4,streamFile) == (uint8_t)read_8bit(loop_offset+loop_adjust,streamFile)) {
|
2008-04-02 18:11:53 +02:00
|
|
|
loops_ok=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!loops_ok)
|
|
|
|
for (loop_adjust=interleave;loop_adjust<=interleave+0x10;loop_adjust+=8) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"looking for loop p/s at %#x,%#x\n",loop_offset-interleave+loop_adjust,loop_offset+loop_adjust);
|
|
|
|
#endif
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_16bitBE(0x64,streamFile) == (uint8_t)read_8bit(loop_offset-interleave+loop_adjust,streamFile) &&
|
|
|
|
read_16bitBE(0xc4,streamFile) == (uint8_t)read_8bit(loop_offset+loop_adjust,streamFile)) {
|
2008-04-02 18:11:53 +02:00
|
|
|
loops_ok=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!loops_ok) goto fail;
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"loop p/s ok (with %#4x adjust)\n",loop_adjust);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* check for agreement */
|
|
|
|
/* loop end (channel 1 & 2 headers) */
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_32bitBE(0x34,streamFile) != read_32bitBE(0x94,streamFile))
|
2008-04-02 18:11:53 +02:00
|
|
|
goto fail;
|
2008-05-19 04:38:57 +02:00
|
|
|
|
|
|
|
/* Mr. Driller oddity */
|
2008-05-20 17:18:38 +02:00
|
|
|
if (dsp_nibbles_to_samples(read_32bitBE(0x34,streamFile)*2)+1 <= read_32bitBE(0x20,streamFile)) {
|
2008-05-19 04:38:57 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"loop end <= half total samples, should be doubled\n");
|
|
|
|
#endif
|
|
|
|
double_loop_end = 1;
|
|
|
|
}
|
|
|
|
|
2008-04-02 18:11:53 +02:00
|
|
|
/* loop start (Cstr header and channel 1 header) */
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_32bitBE(0x30,streamFile) != read_32bitBE(0x10,streamFile)
|
2008-04-02 18:11:53 +02:00
|
|
|
#if 0
|
|
|
|
/* this particular glitch only true for SFA, though it
|
|
|
|
* seems like something similar happens in Donkey Konga */
|
|
|
|
/* loop start (Cstr, channel 1 & 2 headers) */
|
2008-05-20 17:18:38 +02:00
|
|
|
|| (read_32bitBE(0x0c,streamFile)+read_32bitLE(0x30,streamFile)) !=
|
|
|
|
read_32bitBE(0x90,streamFile)
|
2008-04-02 18:11:53 +02:00
|
|
|
#endif
|
|
|
|
)
|
|
|
|
/* alternatively (Donkey Konga) the header loop is 0x0c+0x10 */
|
|
|
|
if (
|
|
|
|
/* loop start (Cstr header and channel 1 header) */
|
2008-05-20 17:18:38 +02:00
|
|
|
read_32bitBE(0x30,streamFile) != read_32bitBE(0x10,streamFile)+
|
|
|
|
read_32bitBE(0x0c,streamFile))
|
2008-04-02 18:11:53 +02:00
|
|
|
/* further alternatively (Donkey Konga), if we loop back to
|
|
|
|
* the very first frame 0x30 might be 0x00000002 (which
|
|
|
|
* is a *valid* std dsp loop start, imagine that) while 0x10
|
|
|
|
* is 0x00000000 */
|
2008-05-20 17:18:38 +02:00
|
|
|
if (!(read_32bitBE(0x30,streamFile) == 2 &&
|
|
|
|
read_32bitBE(0x10,streamFile) == 0))
|
2008-05-19 04:38:57 +02:00
|
|
|
/* lest there be too few alternatives, in Mr. Driller we
|
|
|
|
* find that [0x30] + [0x0c] + 8 = [0x10]*2 */
|
|
|
|
if (!(double_loop_end &&
|
2008-05-20 17:18:38 +02:00
|
|
|
read_32bitBE(0x30,streamFile) +
|
|
|
|
read_32bitBE(0x0c,streamFile) + 8 ==
|
2017-04-22 09:55:33 +02:00
|
|
|
read_32bitBE(0x10,streamFile)*2)) {
|
|
|
|
/* for Mr.Driller Drill Land, no idea about the above but seems to play and loop ok */
|
|
|
|
VGM_LOG("Cstr: bad loop check ignored\n");
|
|
|
|
//goto fail;
|
|
|
|
}
|
2008-04-02 18:11:53 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"loop points agree\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* assure that sample counts, sample rates agree */
|
|
|
|
if (
|
|
|
|
/* sample count (channel 1 & 2 headers) */
|
2008-05-20 17:18:38 +02:00
|
|
|
read_32bitBE(0x20,streamFile) != read_32bitBE(0x80,streamFile) ||
|
2008-04-02 18:11:53 +02:00
|
|
|
/* sample rate (channel 1 & 2 headers) */
|
2008-05-20 17:18:38 +02:00
|
|
|
read_32bitBE(0x28,streamFile) != read_32bitBE(0x88,streamFile) ||
|
2008-04-02 18:11:53 +02:00
|
|
|
/* sample count (Cstr header and channel 1 header) */
|
2008-05-20 17:18:38 +02:00
|
|
|
read_32bitLE(0x14,streamFile) != read_32bitBE(0x20,streamFile) ||
|
2008-04-02 18:11:53 +02:00
|
|
|
/* sample rate (Cstr header and channel 1 header) */
|
2008-05-20 17:18:38 +02:00
|
|
|
(uint16_t)read_16bitLE(0x18,streamFile) != read_32bitBE(0x28,streamFile))
|
2008-04-02 18:11:53 +02:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
|
|
|
|
vgmstream = allocate_vgmstream(2,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
/* fill in the vital statistics */
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->sample_rate = read_32bitBE(0x28,streamFile);
|
2008-04-02 18:11:53 +02:00
|
|
|
/* This is a slight hack to counteract their hack.
|
|
|
|
* All the data is ofset by first_data so that the loop
|
|
|
|
* point occurs at a block boundary. However, I always begin decoding
|
|
|
|
* right after the header, as that is the start of the first block and
|
|
|
|
* my interleave code relies on starting at the beginning of a block.
|
|
|
|
* So we decode a few silent samples at the beginning, and here we make up
|
|
|
|
* for it by lengthening the track by that much.
|
|
|
|
*/
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->num_samples = read_32bitBE(0x20,streamFile) +
|
2008-04-02 18:11:53 +02:00
|
|
|
(first_data-start_offset)/8*14;
|
|
|
|
|
|
|
|
if (loop_flag) {
|
|
|
|
off_t loop_start_bytes = loop_offset-start_offset-interleave;
|
|
|
|
vgmstream->loop_start_sample = dsp_nibbles_to_samples((loop_start_bytes/(2*interleave)*interleave+loop_start_bytes%(interleave*2))*2);
|
|
|
|
/*dsp_nibbles_to_samples(loop_start_bytes);*/
|
2008-05-20 17:18:38 +02:00
|
|
|
/*dsp_nibbles_to_samples(read_32bitBE(0x30,streamFile)*2-inter);*/
|
2008-04-02 18:11:53 +02:00
|
|
|
vgmstream->loop_end_sample = dsp_nibbles_to_samples(
|
2008-05-20 17:18:38 +02:00
|
|
|
read_32bitBE(0x34,streamFile))+1;
|
2008-05-19 04:38:57 +02:00
|
|
|
|
|
|
|
if (double_loop_end)
|
|
|
|
vgmstream->loop_end_sample =
|
2008-05-20 17:18:38 +02:00
|
|
|
dsp_nibbles_to_samples(read_32bitBE(0x34,streamFile)*2)+1;
|
2008-05-19 04:38:57 +02:00
|
|
|
|
2008-04-02 18:11:53 +02:00
|
|
|
if (vgmstream->loop_end_sample > vgmstream->num_samples) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"loop_end_sample > num_samples, adjusting\n");
|
|
|
|
#endif
|
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vgmstream->coding_type = coding_NGC_DSP;
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = interleave;
|
|
|
|
vgmstream->meta_type = meta_DSP_CSTR;
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0;i<16;i++)
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->ch[0].adpcm_coef[i]=read_16bitBE(0x3c+i*2,streamFile);
|
2008-04-02 18:11:53 +02:00
|
|
|
for (i=0;i<16;i++)
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->ch[1].adpcm_coef[i]=read_16bitBE(0x9c+i*2,streamFile);
|
2008-04-02 18:11:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* open the file for reading by each channel */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0;i<2;i++) {
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->ch[i].streamfile = streamFile->open(streamFile,filename,interleave);
|
2008-04-02 18:11:53 +02:00
|
|
|
|
|
|
|
if (!vgmstream->ch[i].streamfile) goto fail;
|
|
|
|
|
|
|
|
vgmstream->ch[i].channel_start_offset=
|
|
|
|
vgmstream->ch[i].offset=
|
|
|
|
start_offset+interleave*i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
/* clean up anything we may have opened */
|
|
|
|
fail:
|
|
|
|
if (vgmstream) close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|