Merge pull request #1012 from bnnm/rpgmvo-dsp

- Add RIFF .rpgmvo [Omori (PC)]
- Add PCM24 .wav [Omori (PC)]
- Improve DSP validations
This commit is contained in:
bnnm 2021-11-12 18:53:57 +01:00 committed by GitHub
commit 8a41ed1dab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 366 additions and 252 deletions

View File

@ -121,7 +121,7 @@ VGMSTREAM* init_vgmstream_bkhd(STREAMFILE* sf) {
subfile_size = read_u32(offset + 0x08, sf);
}
;VGM_LOG("BKHD: %lx, %x\n", subfile_offset, subfile_size);
//;VGM_LOG("BKHD: %lx, %x\n", subfile_offset, subfile_size);
/* detect format */
if (subfile_offset <= 0 || subfile_size <= 0) {

View File

@ -16,98 +16,176 @@ static void load_key(ogg_vorbis_io_config_data* cfg, const uint8_t* key, size_t
memcpy(cfg->key, key, size);
}
/* parser for various encrypted games */
VGMSTREAM* init_vgmstream_encrypted(STREAMFILE* sf) {
VGMSTREAM* vgmstream = NULL;
STREAMFILE* temp_sf = NULL;
ogg_vorbis_io_config_data cfg = {0};
typedef struct {
uint32_t id;
uint8_t keybuf[0x100];
size_t key_size;
off_t start;
VGMSTREAM* vgmstream;
STREAMFILE* temp_sf;
STREAMFILE* new_sf;
} encrypted_t;
/* checks */
id = read_u32be(0x00, sf);
/* The Pirate's Fate (PC) */
static VGMSTREAM* init_vgmstream_encrypted_ogg_tpf(STREAMFILE* sf) {
ogg_vorbis_io_config_data cfg = {0};
encrypted_t e = {0};
if (check_extensions(sf,"ogg,logg")) {
/* The Pirate's Fate (PC) */
if (id == 0x454C513C) { /* "OggS" xored */
load_key(&cfg, tpf_key, sizeof(tpf_key));
}
else {
goto fail;
}
e.id = read_u32be(0x00, sf);
if (e.id != 0x454C513C) /* "OggS" xored */
goto fail;
temp_sf = setup_ogg_vorbis_streamfile(sf, &cfg);
if (!temp_sf) goto fail;
if (!check_extensions(sf,"ogg,logg"))
goto fail;
vgmstream = init_vgmstream_ogg_vorbis(temp_sf);
close_streamfile(temp_sf);
return vgmstream;
}
load_key(&cfg, tpf_key, sizeof(tpf_key));
if (check_extensions(sf,"mp3")) {
/* The Pirate's Fate (PC) */
if ((id & 0xFFFFFF00) == 0x436F0500) { /* "ID3\0" xored */
load_key(&cfg, tpf_key, sizeof(tpf_key));
}
else {
goto fail;
}
temp_sf = setup_ogg_vorbis_streamfile(sf, &cfg);
if (!temp_sf) goto fail;
#ifdef VGM_USE_FFMPEG //TODO: allow MP3 without FFmpeg
vgmstream = init_vgmstream_ffmpeg(temp_sf);
#endif
close_streamfile(temp_sf);
return vgmstream;
}
if (check_extensions(sf,"wav,lwav")) {
/* The Pirate's Fate (PC) */
if (id == 0x58627029) { /* "RIFF" xored */
load_key(&cfg, tpf_key, sizeof(tpf_key));
}
else {
goto fail;
}
temp_sf = setup_ogg_vorbis_streamfile(sf, &cfg);
if (!temp_sf) goto fail;
vgmstream = init_vgmstream_riff(temp_sf);
close_streamfile(temp_sf);
return vgmstream;
}
if (check_extensions(sf,"bgm,mse,koe")) {
/* Studio Ring games [Nanami to Konomi no Oshiete ABC (PC), Oyatsu no Jikan (PC)]
* .bgm: BGM, .mse: SE, .koe: Voice */
uint8_t keybuf[0x100];
size_t key_size;
off_t start;
if (id != get_id32be("RIFF"))
goto fail;
/* Standard RIFF xor'd past "data", sometimes including extra chunks like JUNK or smpl.
* If .bgm is added to riff.c this needs to be reworked so detection goes first, or bgm+bgmkey is
* rejected in riff.c (most files are rejected due to the xor'd extra chunks though). */
key_size = read_key_file(keybuf, sizeof(keybuf), sf);
if (key_size <= 0) goto fail;
if (!find_chunk_le(sf, get_id32be("data"), 0x0c, 0, &start, NULL))
goto fail;
temp_sf = setup_bgm_streamfile(sf, start, keybuf, key_size);
if (!temp_sf) goto fail;
vgmstream = init_vgmstream_riff(temp_sf);
close_streamfile(temp_sf);
return vgmstream;
}
e.temp_sf = setup_ogg_vorbis_streamfile(sf, &cfg);
if (!e.temp_sf) goto fail;
e.vgmstream = init_vgmstream_ogg_vorbis(e.temp_sf);
close_streamfile(e.temp_sf);
return e.vgmstream;
fail:
return NULL;
}
/* The Pirate's Fate (PC) */
static VGMSTREAM* init_vgmstream_encrypted_mp3_tpf(STREAMFILE* sf) {
ogg_vorbis_io_config_data cfg = {0};
encrypted_t e = {0};
e.id = read_u32be(0x00, sf);
if ((e.id & 0xFFFFFF00) != 0x436F0500) /* "ID3\0" xored */
goto fail;
if (!check_extensions(sf,"mp3"))
goto fail;
load_key(&cfg, tpf_key, sizeof(tpf_key));
e.temp_sf = setup_ogg_vorbis_streamfile(sf, &cfg);
if (!e.temp_sf) goto fail;
#ifdef VGM_USE_FFMPEG //TODO: allow MP3 without FFmpeg
e.vgmstream = init_vgmstream_ffmpeg(e.temp_sf);
#endif
close_streamfile(e.temp_sf);
return e.vgmstream;
fail:
return NULL;
}
/* Studio Ring games [Nanami to Konomi no Oshiete ABC (PC), Oyatsu no Jikan (PC)] */
static VGMSTREAM* init_vgmstream_encrypted_riff(STREAMFILE* sf) {
encrypted_t e = {0};
e.id = read_u32be(0x00, sf);
if (e.id != get_id32be("RIFF"))
goto fail;
/* .bgm: BGM, .mse: SE, .koe: Voice */
if (!check_extensions(sf,"bgm,mse,koe"))
goto fail;
/* Standard RIFF xor'd past "data", sometimes including extra chunks like JUNK or smpl.
* If .bgm/etc is added to riff.c this needs to be reworked so detection goes first, or bgm+bgmkey is
* rejected in riff.c (most files are rejected due to the xor'd extra chunks though). */
e.key_size = read_key_file(e.keybuf, sizeof(e.keybuf), sf);
if (e.key_size <= 0) goto fail;
if (!find_chunk_le(sf, get_id32be("data"), 0x0c, 0, &e.start, NULL))
goto fail;
e.temp_sf = setup_bgm_streamfile(sf, e.start, e.keybuf, e.key_size);
if (!e.temp_sf) goto fail;
e.vgmstream = init_vgmstream_riff(e.temp_sf);
close_streamfile(e.temp_sf);
return e.vgmstream;
fail:
return NULL;
}
/* RPGMVO / Omori (PC) */
static VGMSTREAM* init_vgmstream_encrypted_rpgmvo_riff(STREAMFILE* sf) {
ogg_vorbis_io_config_data cfg = {0};
encrypted_t e = {0};
uint32_t xor;
uint32_t riff_size;
if (!is_id64be(0x00, sf, "RPGMV\0\0\0"))
goto fail;
if (!check_extensions(sf,"rpgmvo"))
goto fail;
/* 0x08: version? */
/* Ogg .rpgmvo has per-game key, so this is probably the same. Reversing key is simple though,
* calc suspected key and pass to RIFF to validate (format is the same as Ogg) */
riff_size = get_streamfile_size(sf) - 0x10;
xor = read_u32be(0x10, sf);
xor ^= get_id32be("RIFF");
put_u32be(e.keybuf + 0x00, xor);
xor = read_u32le(0x14, sf);
xor ^= riff_size - 0x08;
put_u32le(e.keybuf + 0x04, xor);
xor = read_u32be(0x18, sf);
xor ^= get_id32be("WAVE");
put_u32be(e.keybuf + 0x08, xor);
xor = read_u32be(0x1c, sf);
xor ^= get_id32be("fmt ");
put_u32be(e.keybuf + 0x0c, xor);
e.key_size = 0x10;
load_key(&cfg, e.keybuf, e.key_size);
cfg.start = 0x10;
cfg.max_offset = 0x10;
e.temp_sf = setup_ogg_vorbis_streamfile(sf, &cfg);
if (!e.temp_sf) goto fail;
e.new_sf = setup_subfile_streamfile(e.temp_sf, 0x10, riff_size, "wav");
if (!e.new_sf) goto fail;
dump_streamfile(e.new_sf, 0);
e.vgmstream = init_vgmstream_riff(e.new_sf);
close_streamfile(e.temp_sf);
close_streamfile(e.new_sf);
return e.vgmstream;
fail:
close_streamfile(e.temp_sf);
close_streamfile(e.new_sf);
return NULL;
}
/* parser for various encrypted games */
VGMSTREAM* init_vgmstream_encrypted(STREAMFILE* sf) {
VGMSTREAM* v = NULL;
v = init_vgmstream_encrypted_ogg_tpf(sf);
if (v) return v;
v = init_vgmstream_encrypted_mp3_tpf(sf);
if (v) return v;
v = init_vgmstream_encrypted_riff(sf);
if (v) return v;
v = init_vgmstream_encrypted_rpgmvo_riff(sf);
if (v) return v;
return NULL;
}

View File

@ -40,14 +40,13 @@ static void bruteforce_hca_key_bin_type(STREAMFILE* sf, hca_codec_data* hca_data
hca_keytest_t hk = {0};
hk.subkey = subkey;
*p_keycode = 0;
/* load whole file in memory for performance (exes with keys shouldn't be too big) */
sf_keys = open_streamfile_by_filename(sf, "keys.bin");
if (!sf_keys) return;
VGM_LOG("HCA BF: test keys.bin (type %i)\n", type);
*p_keycode = 0;
keys_size = get_streamfile_size(sf_keys);
@ -110,16 +109,14 @@ done:
static void bruteforce_hca_key_bin(STREAMFILE* sf, hca_codec_data* hca_data, unsigned long long* p_keycode, uint16_t subkey) {
bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_64LE_1);
/*
bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_32LE_1);
bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_64BE_1);
bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_32BE_1);
//bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_32LE_1);
//bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_64BE_1);
//bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_32BE_1);
bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_64LE_4);
bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_32LE_4);
bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_64BE_4);
bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_32BE_4);
*/
//bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_64LE_4);
//bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_32LE_4);
//bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_64BE_4);
//bruteforce_hca_key_bin_type(sf, hca_data, p_keycode, subkey, HBF_TYPE_32BE_4);
}
@ -137,14 +134,13 @@ static void bruteforce_hca_key_txt(STREAMFILE* sf, hca_codec_data* hca_data, uns
hca_keytest_t hk = {0};
hk.subkey = subkey;
*p_keycode = 0;
/* load whole file in memory for performance (exes with keys shouldn't be too big) */
sf_keys = open_streamfile_by_filename(sf, "keys.txt");
if (!sf_keys) return;
VGM_LOG("HCA BF: test keys.txt\n");
*p_keycode = 0;
keys_size = get_streamfile_size(sf_keys);
@ -198,14 +194,13 @@ static void bruteforce_hca_key_num(STREAMFILE* sf, hca_codec_data* hca_data, uns
hca_keytest_t hk = {0};
hk.subkey = subkey;
*p_keycode = 0;
/* load whole file in memory for performance (exes with keys shouldn't be too big) */
sf_keys = open_streamfile_by_filename(sf, "keys.num");
if (!sf_keys) return;
VGM_LOG("HCA BF: test keys.num\n");
*p_keycode = 0;
keys_size = get_streamfile_size(sf_keys);

View File

@ -8,12 +8,12 @@
struct dsp_header {
uint32_t sample_count; /* 0x00 */
uint32_t nibble_count; /* 0x04 (includes frame headers) */
uint32_t sample_rate; /* 0x08 (generally 32/48kz but games like Wario World set 32028hz to adjust for GC's rate) */
uint32_t sample_rate; /* 0x08 (generally 22/32/44/48kz but games like Wario World set 32028hz to adjust for GC's rate) */
uint16_t loop_flag; /* 0x0c */
uint16_t format; /* 0x0e (always 0 for ADPCM) */
uint32_t loop_start_offset; /* 0x10 */
uint32_t loop_end_offset; /* 0x14 */
uint32_t ca; /* 0x18 (always 0) */
uint32_t initial_offset; /* 0x18 ("ca", in nibbles, should be 2) */
int16_t coef[16]; /* 0x1c (eight pairs) */
uint16_t gain; /* 0x3c (always 0 for ADPCM) */
uint16_t initial_ps; /* 0x3e (predictor/scale in frame header) */
@ -22,12 +22,13 @@ struct dsp_header {
uint16_t loop_ps; /* 0x44 (predictor/scale in loop frame header) */
int16_t loop_hist1; /* 0x46 */
int16_t loop_hist2; /* 0x48 */
int16_t channel_count; /* 0x4a (DSPADPCM.exe ~v2.7 extension) */
int16_t channels; /* 0x4a (DSPADPCM.exe ~v2.7 extension) */
int16_t block_size; /* 0x4c */
/* padding/reserved up to 0x60, DSPADPCM.exe from GC adds garbage here (uninitialized MSVC memory?) */
/* padding/reserved up to 0x60, DSPADPCM.exe from GC adds garbage here (uninitialized MSVC memory?)
* [ex. Batallion Wars (GC), Timesplitters 2 (GC)], 0xcccc...cccc with DSPADPCMD */
};
/* read the above struct; returns nonzero on failure */
/* read and do basic validations to the above struct */
static int read_dsp_header_endian(struct dsp_header *header, off_t offset, STREAMFILE* sf, int big_endian) {
uint32_t (*get_u32)(const uint8_t*) = big_endian ? get_u32be : get_u32le;
uint16_t (*get_u16)(const uint8_t*) = big_endian ? get_u16be : get_u16le;
@ -36,31 +37,68 @@ static int read_dsp_header_endian(struct dsp_header *header, off_t offset, STREA
uint8_t buf[0x60];
if (offset > get_streamfile_size(sf))
return 1;
goto fail;
if (read_streamfile(buf, offset, 0x60, sf) != 0x60)
return 1;
header->sample_count = get_u32(buf+0x00);
header->nibble_count = get_u32(buf+0x04);
header->sample_rate = get_u32(buf+0x08);
header->loop_flag = get_u16(buf+0x0c);
header->format = get_u16(buf+0x0e);
header->loop_start_offset = get_u32(buf+0x10);
header->loop_end_offset = get_u32(buf+0x14);
header->ca = get_u32(buf+0x18);
for (i=0; i < 16; i++)
header->coef[i] = get_s16(buf+0x1c+i*0x02);
header->gain = get_u16(buf+0x3c);
header->initial_ps = get_u16(buf+0x3e);
header->initial_hist1 = get_s16(buf+0x40);
header->initial_hist2 = get_s16(buf+0x42);
header->loop_ps = get_u16(buf+0x44);
header->loop_hist1 = get_s16(buf+0x46);
header->loop_hist2 = get_s16(buf+0x48);
header->channel_count = get_s16(buf+0x4a);
header->block_size = get_s16(buf+0x4c);
goto fail;
/* Since header is rather basic add some extra checks. Some validations like samples vs nibbles
* and loop offsets/expected values should be external, since there are buggy variations */
/* base */
header->sample_count = get_u32(buf+0x00);
if (header->sample_count > 0x10000000)
goto fail; /* unlikely to be that big, should catch fourccs */
header->nibble_count = get_u32(buf+0x04);
if (header->nibble_count > 0x10000000)
goto fail;
header->sample_rate = get_u32(buf+0x08);
if (header->sample_rate < 8000 || header->sample_rate > 48000)
goto fail; /* validated later but fail faster (unsure of min) */
/* context */
header->loop_flag = get_u16(buf+0x0c);
if (header->loop_flag != 0 && header->loop_flag != 1)
goto fail;
header->format = get_u16(buf+0x0e);
if (header->format != 0)
goto fail;
header->loop_start_offset = get_u32(buf+0x10);
header->loop_end_offset = get_u32(buf+0x14);
header->initial_offset = get_u32(buf+0x18);
if (header->initial_offset != 2 && header->initial_offset != 0)
goto fail; /* Dr. Muto uses 0 */
for (i = 0; i < 16; i++)
header->coef[i] = get_s16(buf+0x1c + i*0x02);
header->gain = get_u16(buf+0x3c);
if (header->gain != 0)
goto fail;
/* decoder state */
header->initial_ps = get_u16(buf+0x3e);
header->initial_hist1 = get_s16(buf+0x40);
header->initial_hist2 = get_s16(buf+0x42);
header->loop_ps = get_u16(buf+0x44);
header->loop_hist1 = get_s16(buf+0x46);
header->loop_hist2 = get_s16(buf+0x48);
/* reserved, may contain garbage */
header->channels = get_s16(buf+0x4a);
header->block_size = get_s16(buf+0x4c);
if (header->channels > 64) /* arbitrary max */
header->channels = 0;
if (header->block_size > 0x100000)
header->block_size = 0;
return 1;
fail:
return 0;
}
static int read_dsp_header(struct dsp_header *header, off_t offset, STREAMFILE* file) {
static int read_dsp_header_be(struct dsp_header *header, off_t offset, STREAMFILE* file) {
return read_dsp_header_endian(header, offset, file, 1);
}
static int read_dsp_header_le(struct dsp_header *header, off_t offset, STREAMFILE* file) {
@ -115,7 +153,7 @@ static VGMSTREAM* init_vgmstream_dsp_common(STREAMFILE* sf, dsp_meta* dspm) {
/* load standard DSP header per channel */
{
for (i = 0; i < dspm->channels; i++) {
if (read_dsp_header_endian(&ch_header[i], dspm->header_offset + i*dspm->header_spacing, sf, !dspm->little_endian)) {
if (!read_dsp_header_endian(&ch_header[i], dspm->header_offset + i*dspm->header_spacing, sf, !dspm->little_endian)) {
//;VGM_LOG("DSP: bad header\n");
goto fail;
}
@ -130,16 +168,6 @@ static VGMSTREAM* init_vgmstream_dsp_common(STREAMFILE* sf, dsp_meta* dspm) {
}
}
/* check type==0 and gain==0 */
{
for (i = 0; i < dspm->channels; i++) {
if (ch_header[i].format || ch_header[i].gain) {
//;VGM_LOG("DSP: bad type/gain\n");
goto fail;
}
}
}
/* check for agreement between channels */
if (!dspm->ignore_header_agreement) {
for (i = 0; i < dspm->channels - 1; i++) {
@ -269,25 +297,23 @@ VGMSTREAM* init_vgmstream_ngc_dsp_std(STREAMFILE* sf) {
struct dsp_header header;
const size_t header_size = 0x60;
off_t start_offset;
int i, channel_count;
int i, channels;
/* checks */
if (!read_dsp_header_be(&header, 0x00, sf))
goto fail;
/* .dsp: standard
* .adp: Dr. Muto/Battalion Wars (GC) mono files
* (extensionless): Tony Hawk's Downhill Jam (Wii) */
if (!check_extensions(sf, "dsp,adp,"))
goto fail;
if (read_dsp_header(&header, 0x00, sf))
goto fail;
channel_count = 1;
channels = 1;
start_offset = header_size;
if (header.initial_ps != (uint8_t)read_8bit(start_offset,sf))
goto fail; /* check initial predictor/scale */
if (header.format || header.gain)
goto fail; /* check type==0 and gain==0 */
if (header.initial_ps != read_u8(start_offset,sf))
goto fail;
/* Check for a matching second header. If we find one and it checks
* out thoroughly, we're probably not dealing with a genuine mono DSP.
@ -299,7 +325,7 @@ VGMSTREAM* init_vgmstream_ngc_dsp_std(STREAMFILE* sf) {
struct dsp_header header2;
/* ignore headers one after another */
ko = read_dsp_header(&header2, header_size, sf);
ko = !read_dsp_header_be(&header2, header_size, sf);
if (!ko &&
header.sample_count == header2.sample_count &&
header.nibble_count == header2.nibble_count &&
@ -310,7 +336,7 @@ VGMSTREAM* init_vgmstream_ngc_dsp_std(STREAMFILE* sf) {
/* ignore headers after interleave [Ultimate Board Collection (Wii)] */
ko = read_dsp_header(&header2, 0x10000, sf);
ko = !read_dsp_header_be(&header2, 0x10000, sf);
if (!ko &&
header.sample_count == header2.sample_count &&
header.nibble_count == header2.nibble_count &&
@ -324,7 +350,7 @@ VGMSTREAM* init_vgmstream_ngc_dsp_std(STREAMFILE* sf) {
off_t loop_off;
/* check loop predictor/scale */
loop_off = header.loop_start_offset/16*8;
if (header.loop_ps != (uint8_t)read_8bit(start_offset+loop_off,sf)) {
if (header.loop_ps != read_u8(start_offset+loop_off,sf)) {
/* rarely won't match (ex ESPN 2002), not sure if header or calc problem, but doesn't seem to matter
* (there may be a "click" when looping, or loop values may be too big and loop disabled anyway) */
VGM_LOG("DSP (std): bad loop_predictor\n");
@ -335,7 +361,7 @@ VGMSTREAM* init_vgmstream_ngc_dsp_std(STREAMFILE* sf) {
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,header.loop_flag);
vgmstream = allocate_vgmstream(channels, header.loop_flag);
if (!vgmstream) goto fail;
vgmstream->sample_rate = header.sample_rate;
@ -373,23 +399,21 @@ VGMSTREAM* init_vgmstream_ngc_dsp_std_le(STREAMFILE* sf) {
struct dsp_header header;
const size_t header_size = 0x60;
off_t start_offset;
int i, channel_count;
int i, channels;
/* checks */
/* .adpcm: LEGO Worlds */
if (!check_extensions(sf, "adpcm"))
goto fail;
if (read_dsp_header_le(&header, 0x00, sf))
if (!read_dsp_header_le(&header, 0x00, sf))
goto fail;
channel_count = 1;
channels = 1;
start_offset = header_size;
if (header.initial_ps != (uint8_t)read_8bit(start_offset,sf))
goto fail; /* check initial predictor/scale */
if (header.format || header.gain)
goto fail; /* check type==0 and gain==0 */
if (header.initial_ps != read_u8(start_offset,sf))
goto fail;
/* Check for a matching second header. If we find one and it checks
* out thoroughly, we're probably not dealing with a genuine mono DSP.
@ -397,9 +421,12 @@ VGMSTREAM* init_vgmstream_ngc_dsp_std_le(STREAMFILE* sf) {
* predictor/scale check if the first byte is 0 */
{
struct dsp_header header2;
read_dsp_header_le(&header2, header_size, sf);
int ko;
if (header.sample_count == header2.sample_count &&
ko = !read_dsp_header_le(&header2, header_size, sf);
if (!ko &&
header.sample_count == header2.sample_count &&
header.nibble_count == header2.nibble_count &&
header.sample_rate == header2.sample_rate &&
header.loop_flag == header2.loop_flag) {
@ -411,24 +438,20 @@ VGMSTREAM* init_vgmstream_ngc_dsp_std_le(STREAMFILE* sf) {
off_t loop_off;
/* check loop predictor/scale */
loop_off = header.loop_start_offset/16*8;
if (header.loop_ps != (uint8_t)read_8bit(start_offset+loop_off,sf)) {
/* rarely won't match (ex ESPN 2002), not sure if header or calc problem, but doesn't seem to matter
* (there may be a "click" when looping, or loop values may be too big and loop disabled anyway) */
VGM_LOG("DSP (std): bad loop_predictor\n");
//header.loop_flag = 0;
//goto fail;
if (header.loop_ps != read_u8(start_offset+loop_off,sf)) {
goto fail;
}
}
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,header.loop_flag);
vgmstream = allocate_vgmstream(channels, header.loop_flag);
if (!vgmstream) goto fail;
vgmstream->sample_rate = header.sample_rate;
vgmstream->num_samples = header.sample_count;
vgmstream->loop_start_sample = dsp_nibbles_to_samples(header.loop_start_offset);
vgmstream->loop_end_sample = dsp_nibbles_to_samples(header.loop_end_offset)+1;
vgmstream->loop_end_sample = dsp_nibbles_to_samples(header.loop_end_offset) + 1;
if (vgmstream->loop_end_sample > vgmstream->num_samples) /* don't know why, but it does happen */
vgmstream->loop_end_sample = vgmstream->num_samples;
@ -460,29 +483,28 @@ VGMSTREAM* init_vgmstream_ngc_mdsp_std(STREAMFILE* sf) {
struct dsp_header header;
const size_t header_size = 0x60;
off_t start_offset;
int i, c, channel_count;
int i, c, channels;
/* checks */
if (!read_dsp_header_be(&header, 0x00, sf))
goto fail;
if (!check_extensions(sf, "dsp,mdsp"))
goto fail;
if (read_dsp_header(&header, 0x00, sf))
goto fail;
channels = header.channels==0 ? 1 : header.channels;
start_offset = header_size * channels;
channel_count = header.channel_count==0 ? 1 : header.channel_count;
start_offset = header_size * channel_count;
if (header.initial_ps != read_u8(start_offset, sf))
goto fail;
/* named .dsp and no channels? likely another interleaved dsp */
if (check_extensions(sf,"dsp") && header.channel_count == 0)
if (check_extensions(sf,"dsp") && header.channels == 0)
goto fail;
if (header.initial_ps != (uint8_t)read_8bit(start_offset, sf))
goto fail; /* check initial predictor/scale */
if (header.format || header.gain)
goto fail; /* check type==0 and gain==0 */
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count, header.loop_flag);
vgmstream = allocate_vgmstream(channels, header.loop_flag);
if (!vgmstream) goto fail;
vgmstream->sample_rate = header.sample_rate;
@ -494,13 +516,14 @@ VGMSTREAM* init_vgmstream_ngc_mdsp_std(STREAMFILE* sf) {
vgmstream->meta_type = meta_DSP_STD;
vgmstream->coding_type = coding_NGC_DSP;
vgmstream->layout_type = channel_count == 1 ? layout_none : layout_interleave;
vgmstream->layout_type = channels == 1 ? layout_none : layout_interleave;
vgmstream->interleave_block_size = header.block_size * 8;
if (vgmstream->interleave_block_size)
vgmstream->interleave_last_block_size = (header.nibble_count / 2 % vgmstream->interleave_block_size + 7) / 8 * 8;
for (i = 0; i < channel_count; i++) {
if (read_dsp_header(&header, header_size * i, sf)) goto fail;
for (i = 0; i < channels; i++) {
if (!read_dsp_header_be(&header, header_size * i, sf))
goto fail;
/* adpcm coeffs/history */
for (c = 0; c < 16; c++)
@ -525,21 +548,23 @@ VGMSTREAM* init_vgmstream_ngc_dsp_stm(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
/* .lstm/dsp: renamed to avoid hijacking Scream Tracker 2 Modules */
if (read_u16be(0x00, sf) != 0x0200)
goto fail;
/* .lstm/dsp: renamed to avoid hijacking Scream Tracker 2 Modules (not needed) */
if (!check_extensions(sf, "stm,lstm,dsp"))
goto fail;
if (read_16bitBE(0x00, sf) != 0x0200)
goto fail;
/* 0x02(2): sample rate, 0x08+: channel sizes/loop offsets? */
/* 0x02: sample rate
* 0x08+: channel sizes/loop offsets? */
dspm.channels = read_32bitBE(0x04, sf);
dspm.channels = read_u32be(0x04, sf);
dspm.max_channels = 2;
dspm.fix_looping = 1;
dspm.header_offset = 0x40;
dspm.header_spacing = 0x60;
dspm.start_offset = 0x100;
dspm.interleave = (read_32bitBE(0x08, sf) + 0x20) / 0x20 * 0x20; /* strange rounding, but works */
dspm.interleave = (read_u32be(0x08, sf) + 0x20) / 0x20 * 0x20; /* strange rounding, but works */
dspm.meta_type = meta_DSP_STM;
return init_vgmstream_dsp_common(sf, &dspm);
@ -619,9 +644,10 @@ VGMSTREAM* init_vgmstream_idsp_namco(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (!check_extensions(sf, "idsp"))
if (!is_id32be(0x00,sf, "IDSP"))
goto fail;
if (read_32bitBE(0x00,sf) != 0x49445350) /* "IDSP" */
if (!check_extensions(sf, "idsp"))
goto fail;
dspm.max_channels = 8;
@ -653,9 +679,10 @@ VGMSTREAM* init_vgmstream_sadb(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (!check_extensions(sf, "sad"))
if (!is_id32be(0x00,sf, "sadb"))
goto fail;
if (read_32bitBE(0x00,sf) != 0x73616462) /* "sadb" */
if (!check_extensions(sf, "sad"))
goto fail;
dspm.channels = read_8bit(0x32, sf);
@ -679,17 +706,17 @@ VGMSTREAM* init_vgmstream_idsp_tt(STREAMFILE* sf) {
int version_main, version_sub;
/* checks */
if (!is_id32be(0x00,sf, "IDSP"))
goto fail;
/* .gcm: standard
* .idsp: header id?
* .wua: Lego Dimensions (Wii U) */
if (!check_extensions(sf, "gcm,idsp,wua"))
goto fail;
if (read_32bitBE(0x00,sf) != 0x49445350) /* "IDSP" */
goto fail;
version_main = read_32bitBE(0x04, sf);
version_sub = read_32bitBE(0x08, sf); /* extra check since there are other IDSPs */
version_main = read_u32be(0x04, sf);
version_sub = read_u32be(0x08, sf); /* extra check since there are other IDSPs */
if (version_main == 0x01 && version_sub == 0xc8) {
/* Transformers: The Game (Wii) */
dspm.channels = 2;
@ -711,7 +738,7 @@ VGMSTREAM* init_vgmstream_idsp_tt(STREAMFILE* sf) {
else if (version_main == 0x03 && version_sub == 0x12c) {
/* Lego The Lord of the Rings (Wii) */
/* Lego Dimensions (Wii U) */
dspm.channels = read_32bitBE(0x10, sf);
dspm.channels = read_u32be(0x10, sf);
dspm.max_channels = 2;
dspm.header_offset = 0x20;
/* 0x14+: "I_AM_PADDING" */
@ -722,7 +749,7 @@ VGMSTREAM* init_vgmstream_idsp_tt(STREAMFILE* sf) {
dspm.header_spacing = 0x60;
dspm.start_offset = dspm.header_offset + 0x60 * dspm.channels;
dspm.interleave = read_32bitBE(0x0c, sf);
dspm.interleave = read_u32be(0x0c, sf);
dspm.meta_type = meta_IDSP_TT;
return init_vgmstream_dsp_common(sf, &dspm);
@ -735,9 +762,9 @@ VGMSTREAM* init_vgmstream_idsp_nl(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (!check_extensions(sf, "idsp"))
if (!is_id32be(0x00,sf, "IDSP"))
goto fail;
if (read_32bitBE(0x00,sf) != 0x49445350) /* "IDSP" */
if (!check_extensions(sf, "idsp"))
goto fail;
dspm.channels = 2;
@ -773,16 +800,18 @@ VGMSTREAM* init_vgmstream_wii_wsd(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (read_u32be(0x00,sf) != 0x20)
goto fail;
if (!check_extensions(sf, "wsd"))
goto fail;
if (read_32bitBE(0x08,sf) != read_32bitBE(0x0c,sf)) /* channel sizes */
if (read_u32be(0x08,sf) != read_u32be(0x0c,sf)) /* channel sizes */
goto fail;
dspm.channels = 2;
dspm.max_channels = 2;
dspm.header_offset = read_32bitBE(0x00,sf);
dspm.header_spacing = read_32bitBE(0x04,sf) - dspm.header_offset;
dspm.header_offset = read_u32be(0x00,sf);
dspm.header_spacing = read_u32be(0x04,sf) - dspm.header_offset;
dspm.start_offset = dspm.header_offset + 0x60;
dspm.interleave = dspm.header_spacing;
@ -792,7 +821,7 @@ fail:
return NULL;
}
/* .ddsp - full interleaved dsp [The Sims 2 - Pets (Wii)] */
/* .ddsp - full interleaved dsp [The Sims 2: Pets (Wii)] */
VGMSTREAM* init_vgmstream_dsp_ddsp(STREAMFILE* sf) {
dsp_meta dspm = {0};
@ -819,9 +848,9 @@ VGMSTREAM* init_vgmstream_wii_was(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (!check_extensions(sf, "was,dsp,isws"))
if (!is_id32be(0x00,sf, "iSWS"))
goto fail;
if (read_32bitBE(0x00,sf) != 0x69535753) /* "iSWS" */
if (!check_extensions(sf, "was,dsp,isws"))
goto fail;
dspm.channels = read_32bitBE(0x08,sf);
@ -888,12 +917,12 @@ VGMSTREAM* init_vgmstream_dsp_ndp(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (!is_id32be(0x00,sf, "NDP\0"))
goto fail;
/* .nds: standard
* .ndp: header id */
if (!check_extensions(sf, "nds,ndp"))
goto fail;
if (!is_id32be(0x00,sf, "NDP\0"))
goto fail;
if (read_u32le(0x08,sf) + 0x18 != get_streamfile_size(sf))
goto fail;
/* 0x0c: sample rate */
@ -946,18 +975,19 @@ VGMSTREAM* init_vgmstream_ngc_dsp_aaap(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (!is_id32be(0x00,sf, "AAAp"))
goto fail;
if (!check_extensions(sf, "dsp"))
goto fail;
if (read_32bitBE(0x00,sf) != 0x41414170) /* "AAAp" */
goto fail;
dspm.channels = read_16bitBE(0x06,sf);
dspm.interleave = read_u16be(0x04,sf);
dspm.channels = read_u16be(0x06,sf);
dspm.max_channels = 2;
dspm.header_offset = 0x08;
dspm.header_spacing = 0x60;
dspm.start_offset = dspm.header_offset + dspm.channels*dspm.header_spacing;
dspm.interleave = (uint16_t)read_16bitBE(0x04,sf);
dspm.start_offset = dspm.header_offset + dspm.channels * dspm.header_spacing;
dspm.meta_type = meta_NGC_DSP_AAAP;
return init_vgmstream_dsp_common(sf, &dspm);
@ -970,15 +1000,15 @@ VGMSTREAM* init_vgmstream_dsp_dspw(STREAMFILE* sf) {
dsp_meta dspm = {0};
size_t data_size;
/* check extension */
if (!check_extensions(sf, "dspw"))
/* checks */
if (!is_id32be(0x00,sf, "DSPW"))
goto fail;
if (read_32bitBE(0x00,sf) != 0x44535057) /* "DSPW" */
if (!check_extensions(sf, "dspw"))
goto fail;
/* ignore time marker */
data_size = read_32bitBE(0x08, sf);
if (read_32bitBE(data_size - 0x10, sf) == 0x74494D45) /* "tIME" */
if (is_id32be(data_size - 0x10, sf, "tIME"))
data_size -= 0x10; /* (ignore, 2 ints in YYYYMMDD hhmmss00) */
/* some files have a mrkr section with multiple loop regions added at the end (variable size) */
@ -1016,10 +1046,12 @@ VGMSTREAM* init_vgmstream_ngc_dsp_iadp(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
/* .adp: actual extension, .iadp: header id */
if (!check_extensions(sf, "adp,iadp"))
if (!is_id32be(0x00,sf, "iadp"))
goto fail;
if (read_32bitBE(0x00,sf) != 0x69616470) /* "iadp" */
/* .adp: actual extension
* .iadp: header id */
if (!check_extensions(sf, "adp,iadp"))
goto fail;
dspm.channels = read_32bitBE(0x04,sf);
@ -1068,11 +1100,13 @@ VGMSTREAM* init_vgmstream_dsp_switch_audio(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
/* .switch_audio: possibly UE4 class name rather than extension, .dsp: assumed */
/* .switch_audio: possibly UE4 class name rather than extension
* .dsp: assumed */
if (!check_extensions(sf, "switch_audio,dsp"))
goto fail;
/* manual double header test */
//todo improve to read after first header
if (read_32bitLE(0x00, sf) == read_32bitLE(get_streamfile_size(sf) / 2, sf))
dspm.channels = 2;
else
@ -1096,13 +1130,14 @@ VGMSTREAM* init_vgmstream_dsp_sps_n1(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (read_u32be(0x00,sf) != 0x08000000) /* file type (see other N1 SPS) */
goto fail;
/* .vag: Penny-Punching Princess (Switch)
* .nlsd: Ys VIII (Switch) */
if (!check_extensions(sf, "vag,nlsd"))
goto fail;
if (read_32bitBE(0x00,sf) != 0x08000000) /* file type (see other N1 SPS) */
goto fail;
if ((uint16_t)read_16bitLE(0x08,sf) != read_32bitLE(0x24,sf)) /* header has various repeated values */
if (read_u16le(0x08,sf) != read_u32le(0x24,sf)) /* header has various repeated values */
goto fail;
dspm.channels = 1;
@ -1133,7 +1168,7 @@ VGMSTREAM* init_vgmstream_dsp_itl_ch(STREAMFILE* sf) {
dspm.header_offset = 0x00;
dspm.header_spacing = 0x60;
dspm.start_offset = dspm.header_offset + dspm.header_spacing*dspm.channels;
dspm.start_offset = dspm.header_offset + dspm.header_spacing * dspm.channels;
dspm.interleave = 0x23C0;
dspm.fix_looping = 1;
@ -1149,16 +1184,17 @@ VGMSTREAM* init_vgmstream_dsp_adpy(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (!check_extensions(sf, "adpcmx"))
if (!is_id32be(0x00,sf, "ADPY"))
goto fail;
if (read_32bitBE(0x00,sf) != 0x41445059) /* "ADPY" */
if (!check_extensions(sf, "adpcmx"))
goto fail;
/* 0x04(2): 1? */
/* 0x08: some size? */
/* 0x0c: null */
dspm.channels = read_16bitLE(0x06,sf);
dspm.channels = read_u16le(0x06,sf);
dspm.max_channels = 2;
dspm.little_endian = 1;
@ -1178,9 +1214,10 @@ VGMSTREAM* init_vgmstream_dsp_adpx(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (!check_extensions(sf, "adpcmx"))
if (!is_id32be(0x00,sf, "ADPX"))
goto fail;
if (read_32bitBE(0x00,sf) != 0x41445058) /* "ADPX" */
if (!check_extensions(sf, "adpcmx"))
goto fail;
/* from 0x04 *6 are probably channel sizes, so max would be 6ch; this assumes 2ch */
@ -1216,6 +1253,7 @@ VGMSTREAM* init_vgmstream_dsp_ds2(STREAMFILE* sf) {
read_32bitBE(0x58,sf) == 0 &&
read_32bitBE(0x5c,sf) != 0))
goto fail;
file_size = get_streamfile_size(sf);
channel_offset = read_32bitBE(0x5c,sf); /* absolute offset to 2nd channel */
if (channel_offset < file_size / 2 || channel_offset > file_size) /* just to make sure */
@ -1272,11 +1310,12 @@ VGMSTREAM* init_vgmstream_dsp_sqex(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (!check_extensions(sf, "wav,lwav"))
goto fail;
if (read_u32be(0x00,sf) != 0x00000000)
goto fail;
if (!check_extensions(sf, "wav,lwav"))
goto fail;
dspm.channels = read_u32le(0x04,sf);
dspm.header_offset = read_u32le(0x08,sf);
/* 0x0c: channel size */
@ -1303,12 +1342,11 @@ VGMSTREAM* init_vgmstream_dsp_wiivoice(STREAMFILE* sf) {
/* also see g1l.c for WiiBGM weirder variation */
/* checks */
if (!is_id64be(0x00,sf, "WiiVoice"))
goto fail;
/* .dsp: assumed */
if (!check_extensions(sf, "dsp"))
goto fail;
if (read_u32be(0x00,sf) != 0x57696956 && /* "WiiV" */
read_u32be(0x04,sf) != 0x6F696365) /* "oice" */
goto fail;
dspm.channels = 1;
dspm.max_channels = 1;
@ -1331,9 +1369,9 @@ VGMSTREAM* init_vgmstream_dsp_wiiadpcm(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (!check_extensions(sf, "adpcm"))
if (!is_id64be(0x00,sf, "WIIADPCM"))
goto fail;
if (!is_id32be(0x00,sf, "WIIA") && !is_id32be(0x00,sf, "DPCM"))
if (!check_extensions(sf, "adpcm"))
goto fail;
dspm.interleave = read_u32be(0x08,sf); /* interleave offset */
@ -1361,11 +1399,12 @@ VGMSTREAM* init_vgmstream_dsp_cwac(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (!is_id32be(0x00,sf, "CWAC"))
goto fail;
/* .dsp: assumed */
if (!check_extensions(sf, "dsp"))
goto fail;
if (!is_id32be(0x00,sf, "CWAC"))
goto fail;
dspm.channels = read_u16be(0x04,sf);
dspm.header_offset = read_u32be(0x08,sf);
@ -1388,10 +1427,10 @@ VGMSTREAM* init_vgmstream_idsp_tose(STREAMFILE* sf) {
uint32_t blocks;
/* checks */
if (!check_extensions(sf, "idsp"))
goto fail;
if (read_u32be(0x00,sf) != 0)
goto fail;
if (!check_extensions(sf, "idsp"))
goto fail;
dspm.max_channels = 4; /* mainly stereo */
@ -1419,11 +1458,12 @@ VGMSTREAM* init_vgmstream_dsp_kwa(STREAMFILE* sf) {
dsp_meta dspm = {0};
/* checks */
if (read_u32be(0x00,sf) != 3)
goto fail;
/* .dsp: assumed */
if (!check_extensions(sf, "kwa"))
goto fail;
if (read_u32be(0x00,sf) != 3)
goto fail;
dspm.max_channels = 4;

View File

@ -131,6 +131,9 @@ static int read_fmt(int big_endian, STREAMFILE* sf, off_t offset, riff_fmt_chunk
}
}
if (!fmt->channels)
goto fail;
switch (fmt->codec) {
case 0x00: /* Yamaha AICA ADPCM [Headhunter (DC), Bomber hehhe (DC), Rayman 2 (DC)] (unofficial) */
if (fmt->bps != 4) goto fail;
@ -142,17 +145,19 @@ static int read_fmt(int big_endian, STREAMFILE* sf, off_t offset, riff_fmt_chunk
case 0x01: /* PCM */
switch (fmt->bps) {
case 24: /* Omori (PC) */
fmt->coding_type = coding_PCM24LE;
break;
case 16:
fmt->coding_type = big_endian ? coding_PCM16BE : coding_PCM16LE;
fmt->interleave = 0x02;
break;
case 8:
fmt->coding_type = coding_PCM8_U;
fmt->interleave = 0x01;
break;
default:
goto fail;
}
fmt->interleave = fmt->block_size / fmt->channels;
break;
case 0x02: /* MSADPCM */
@ -666,12 +671,10 @@ VGMSTREAM* init_vgmstream_riff(STREAMFILE* sf) {
/* samples, codec init (after setting coding to ensure proper close on failure) */
switch (fmt.coding_type) {
case coding_PCM24LE:
case coding_PCM16LE:
vgmstream->num_samples = pcm_bytes_to_samples(data_size, fmt.channels, 16);
break;
case coding_PCM8_U:
vgmstream->num_samples = pcm_bytes_to_samples(data_size, vgmstream->channels, 8);
vgmstream->num_samples = pcm_bytes_to_samples(data_size, fmt.channels, fmt.bps);
break;
case coding_L5_555:
@ -1068,10 +1071,8 @@ VGMSTREAM* init_vgmstream_rifx(STREAMFILE* sf) {
/* init, samples */
switch (fmt.coding_type) {
case coding_PCM16BE:
vgmstream->num_samples = pcm_bytes_to_samples(data_size, vgmstream->channels, 16);
break;
case coding_PCM8_U:
vgmstream->num_samples = pcm_bytes_to_samples(data_size, vgmstream->channels, 8);
vgmstream->num_samples = pcm_bytes_to_samples(data_size, vgmstream->channels, fmt.bps);
break;
default:
goto fail;