2008-05-06 05:35:37 +02:00
|
|
|
#include "meta.h"
|
2008-05-06 06:01:05 +02:00
|
|
|
#include "../coding/coding.h"
|
2008-05-06 03:01:06 +02:00
|
|
|
#include "../util.h"
|
|
|
|
|
2009-01-20 02:03:33 +01:00
|
|
|
static off_t read_rwav(off_t offset, int *version, off_t *start_offset, off_t *info_chunkp, STREAMFILE *streamFile)
|
2008-12-20 23:57:54 +01:00
|
|
|
{
|
2009-01-20 02:03:33 +01:00
|
|
|
off_t info_chunk;
|
|
|
|
off_t data_chunk;
|
2008-12-20 23:57:54 +01:00
|
|
|
off_t wave_offset;
|
2009-01-20 02:03:33 +01:00
|
|
|
|
|
|
|
if ((uint32_t)read_32bitBE(offset,streamFile)!=0x52574156) /* "RWAV" */
|
|
|
|
goto fail;
|
|
|
|
if ((uint32_t)read_32bitBE(offset+4,streamFile)!=0xFEFF0102) /* version 2 */
|
2008-12-20 23:57:54 +01:00
|
|
|
goto fail;
|
|
|
|
|
2009-01-20 02:03:33 +01:00
|
|
|
info_chunk = offset+read_32bitBE(offset+0x10,streamFile);
|
|
|
|
if ((uint32_t)read_32bitBE(info_chunk,streamFile)!=0x494e464f) /* "INFO" */
|
|
|
|
goto fail;
|
|
|
|
data_chunk = offset+read_32bitBE(offset+0x18,streamFile);
|
|
|
|
if ((uint32_t)read_32bitBE(data_chunk,streamFile)!=0x44415441) /* "DATA" */
|
|
|
|
goto fail;
|
2008-12-20 23:57:54 +01:00
|
|
|
|
2009-01-20 02:03:33 +01:00
|
|
|
*start_offset = data_chunk + 8;
|
|
|
|
*info_chunkp = info_chunk + 8;
|
|
|
|
*version = 2;
|
|
|
|
wave_offset = info_chunk - 8;
|
2008-12-20 23:57:54 +01:00
|
|
|
|
2009-01-20 02:03:33 +01:00
|
|
|
return wave_offset;
|
|
|
|
fail:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static off_t read_rwar(off_t offset, int *version, off_t *start_offset, off_t *info_chunk, STREAMFILE *streamFile)
|
|
|
|
{
|
|
|
|
off_t wave_offset;
|
|
|
|
if ((uint32_t)read_32bitBE(offset,streamFile)!=0x52574152) /* "RWAR" */
|
|
|
|
goto fail;
|
|
|
|
if ((uint32_t)read_32bitBE(offset+4,streamFile)!=0xFEFF0100) /* version 0 */
|
|
|
|
goto fail;
|
2008-12-20 23:57:54 +01:00
|
|
|
|
2009-01-20 02:03:33 +01:00
|
|
|
wave_offset = read_rwav(offset+0x60,version,start_offset,info_chunk,streamFile);
|
|
|
|
*version = 0;
|
2008-12-20 23:57:54 +01:00
|
|
|
return wave_offset;
|
2009-01-20 02:03:33 +01:00
|
|
|
|
2008-12-20 23:57:54 +01:00
|
|
|
fail:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-05-06 03:01:06 +02:00
|
|
|
/* RWSD is quite similar to BRSTM, but can contain several streams.
|
2008-05-06 04:58:29 +02:00
|
|
|
* Still, some games use it for single streams. We only support the
|
2008-05-06 03:01:06 +02:00
|
|
|
* single stream form here */
|
2008-05-20 17:18:38 +02:00
|
|
|
VGMSTREAM * init_vgmstream_rwsd(STREAMFILE *streamFile) {
|
2008-05-06 03:01:06 +02:00
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2008-05-20 17:18:38 +02:00
|
|
|
char filename[260];
|
2008-05-06 03:01:06 +02:00
|
|
|
|
|
|
|
coding_t coding_type;
|
|
|
|
|
2009-01-20 02:03:33 +01:00
|
|
|
off_t info_chunk;
|
2008-05-06 04:58:29 +02:00
|
|
|
off_t wave_offset;
|
|
|
|
size_t wave_length;
|
2008-05-06 03:01:06 +02:00
|
|
|
int codec_number;
|
|
|
|
int channel_count;
|
|
|
|
int loop_flag;
|
2008-12-20 23:57:54 +01:00
|
|
|
int rwar = 0;
|
2009-01-20 02:03:33 +01:00
|
|
|
int rwav = 0;
|
2008-12-20 06:17:24 +01:00
|
|
|
int version = -1;
|
2008-05-06 03:01:06 +02:00
|
|
|
|
2008-12-28 07:33:14 +01:00
|
|
|
off_t start_offset = 0;
|
2008-05-06 04:58:29 +02:00
|
|
|
size_t stream_size;
|
2008-05-06 03:01:06 +02:00
|
|
|
|
|
|
|
/* check extension, case insensitive */
|
2008-05-20 17:18:38 +02:00
|
|
|
streamFile->get_name(streamFile,filename,sizeof(filename));
|
2008-12-20 23:57:54 +01:00
|
|
|
if (strcasecmp("rwsd",filename_extension(filename)))
|
|
|
|
{
|
|
|
|
if (strcasecmp("rwar",filename_extension(filename)))
|
2009-01-20 02:03:33 +01:00
|
|
|
{
|
|
|
|
if (strcasecmp("rwav",filename_extension(filename)))
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rwav = 1;
|
|
|
|
}
|
|
|
|
}
|
2008-12-20 23:57:54 +01:00
|
|
|
else
|
2009-01-20 02:03:33 +01:00
|
|
|
{
|
2008-12-20 23:57:54 +01:00
|
|
|
rwar = 1;
|
2009-01-20 02:03:33 +01:00
|
|
|
}
|
2008-12-20 23:57:54 +01:00
|
|
|
}
|
2008-05-06 03:01:06 +02:00
|
|
|
|
|
|
|
/* check header */
|
2008-12-20 23:57:54 +01:00
|
|
|
if (rwar)
|
2008-12-20 06:17:24 +01:00
|
|
|
{
|
2009-01-20 02:03:33 +01:00
|
|
|
wave_offset = read_rwar(0,&version,&start_offset,&info_chunk,streamFile);
|
|
|
|
if (wave_offset < 0) goto fail;
|
|
|
|
}
|
|
|
|
else if (rwav)
|
|
|
|
{
|
|
|
|
wave_offset = read_rwav(0,&version,&start_offset,&info_chunk,streamFile);
|
2008-12-20 23:57:54 +01:00
|
|
|
if (wave_offset < 0) goto fail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((uint32_t)read_32bitBE(0,streamFile)!=0x52575344) /* "RWSD" */
|
|
|
|
goto fail;
|
2008-12-20 06:17:24 +01:00
|
|
|
|
2008-12-20 23:57:54 +01:00
|
|
|
switch (read_32bitBE(4,streamFile))
|
|
|
|
{
|
|
|
|
case 0xFEFF0102:
|
|
|
|
/* ideally we would look through the chunk list for a WAVE chunk,
|
|
|
|
* but it's always in the same order */
|
|
|
|
/* get WAVE offset, check */
|
|
|
|
wave_offset = read_32bitBE(0x18,streamFile);
|
|
|
|
if ((uint32_t)read_32bitBE(wave_offset,streamFile)!=0x57415645) /* "WAVE" */
|
|
|
|
goto fail;
|
|
|
|
/* get WAVE size, check */
|
|
|
|
wave_length = read_32bitBE(0x1c,streamFile);
|
|
|
|
if (read_32bitBE(wave_offset+4,streamFile)!=wave_length)
|
|
|
|
goto fail;
|
2008-12-20 06:17:24 +01:00
|
|
|
|
2008-12-20 23:57:54 +01:00
|
|
|
/* check wave count */
|
|
|
|
if (read_32bitBE(wave_offset+8,streamFile) != 1)
|
|
|
|
goto fail; /* only support 1 */
|
2008-12-20 06:17:24 +01:00
|
|
|
|
2008-12-20 23:57:54 +01:00
|
|
|
version = 2;
|
2008-12-20 06:17:24 +01:00
|
|
|
|
2008-12-20 23:57:54 +01:00
|
|
|
break;
|
|
|
|
case 0xFEFF0103:
|
2009-01-20 02:03:33 +01:00
|
|
|
wave_offset = read_rwar(0xe0,&version,&start_offset,&info_chunk,streamFile);
|
2008-12-20 23:57:54 +01:00
|
|
|
if (wave_offset < 0) goto fail;
|
|
|
|
|
|
|
|
rwar = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
2008-05-06 04:58:29 +02:00
|
|
|
|
2008-12-20 06:17:24 +01:00
|
|
|
}
|
2008-05-06 03:01:06 +02:00
|
|
|
|
2008-05-06 04:58:29 +02:00
|
|
|
/* get type details */
|
2008-05-20 17:18:38 +02:00
|
|
|
codec_number = read_8bit(wave_offset+0x10,streamFile);
|
|
|
|
loop_flag = read_8bit(wave_offset+0x11,streamFile);
|
|
|
|
channel_count = read_8bit(wave_offset+0x12,streamFile);
|
2008-05-06 03:01:06 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel_count < 1) goto fail;
|
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
/* fill in the vital statistics */
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->num_samples = dsp_nibbles_to_samples(read_32bitBE(wave_offset+0x1c,streamFile));
|
|
|
|
vgmstream->sample_rate = (uint16_t)read_16bitBE(wave_offset+0x14,streamFile);
|
2008-05-06 03:01:06 +02:00
|
|
|
/* channels and loop flag are set by allocate_vgmstream */
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->loop_start_sample = dsp_nibbles_to_samples(read_32bitBE(wave_offset+0x18,streamFile));
|
2008-05-06 03:01:06 +02:00
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
|
|
|
|
|
|
|
vgmstream->coding_type = coding_type;
|
2008-05-06 04:58:29 +02:00
|
|
|
vgmstream->layout_type = layout_none;
|
2008-05-06 03:01:06 +02:00
|
|
|
|
2008-12-20 23:57:54 +01:00
|
|
|
if (rwar)
|
|
|
|
vgmstream->meta_type = meta_RWAR;
|
2009-01-20 02:03:33 +01:00
|
|
|
else if (rwav)
|
|
|
|
vgmstream->meta_type = meta_RWAV;
|
2008-12-20 23:57:54 +01:00
|
|
|
else
|
|
|
|
vgmstream->meta_type = meta_RWSD;
|
2008-05-06 03:01:06 +02:00
|
|
|
|
|
|
|
if (vgmstream->coding_type == coding_NGC_DSP) {
|
|
|
|
off_t coef_offset;
|
|
|
|
int i,j;
|
|
|
|
|
|
|
|
for (j=0;j<vgmstream->channels;j++) {
|
2009-01-20 02:03:33 +01:00
|
|
|
if (rwar || rwav)
|
|
|
|
{
|
|
|
|
/* This is pretty nasty, so an explaination is in order.
|
|
|
|
* At 0x10 in the info_chunk is the offset of a table with
|
|
|
|
* one entry per channel. Each entry in this table is itself
|
|
|
|
* an offset to a set of information for the channel. The
|
|
|
|
* first element in the set is the offset into DATA of the
|
|
|
|
* channel. The stream_size read far below just happens to
|
|
|
|
* hit on this properly for stereo. The second element is the
|
|
|
|
* offset of the coefficient table for the channel. */
|
|
|
|
coef_offset = info_chunk +
|
|
|
|
read_32bitBE(info_chunk +
|
|
|
|
read_32bitBE(info_chunk+
|
|
|
|
read_32bitBE(info_chunk+0x10,streamFile)+j*4,
|
|
|
|
streamFile) + 4, streamFile);
|
|
|
|
} else {
|
|
|
|
coef_offset=wave_offset+0x6c+j*0x30;
|
|
|
|
}
|
2008-05-06 03:01:06 +02:00
|
|
|
for (i=0;i<16;i++) {
|
2009-01-20 02:03:33 +01:00
|
|
|
vgmstream->ch[j].adpcm_coef[i]=read_16bitBE(coef_offset+i*2,streamFile);
|
2008-05-06 03:01:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 02:03:33 +01:00
|
|
|
if (rwar || rwav)
|
2008-12-20 23:57:54 +01:00
|
|
|
{
|
2009-01-20 02:03:33 +01:00
|
|
|
/* */
|
2008-12-20 23:57:54 +01:00
|
|
|
}
|
2008-12-20 06:17:24 +01:00
|
|
|
else
|
2008-12-20 23:57:54 +01:00
|
|
|
{
|
|
|
|
if (version == 2)
|
|
|
|
start_offset = read_32bitBE(8,streamFile);
|
|
|
|
}
|
2008-05-20 17:18:38 +02:00
|
|
|
stream_size = read_32bitBE(wave_offset+0x50,streamFile);
|
2008-05-06 03:01:06 +02:00
|
|
|
|
|
|
|
/* open the file for reading by each channel */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0;i<channel_count;i++) {
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->ch[i].streamfile = streamFile->open(streamFile,filename,
|
2008-05-06 03:01:06 +02:00
|
|
|
0x1000);
|
|
|
|
|
|
|
|
if (!vgmstream->ch[i].streamfile) goto fail;
|
|
|
|
|
|
|
|
vgmstream->ch[i].channel_start_offset=
|
|
|
|
vgmstream->ch[i].offset=
|
2008-05-06 04:58:29 +02:00
|
|
|
start_offset + i*stream_size;
|
2008-05-06 03:01:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
/* clean up anything we may have opened */
|
|
|
|
fail:
|
|
|
|
if (vgmstream) close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|