2010-03-21 05:23:18 +01:00
|
|
|
#include "meta.h"
|
2017-10-14 12:42:44 +02:00
|
|
|
#include "../coding/coding.h"
|
2010-03-21 05:23:18 +01:00
|
|
|
#include "../util.h"
|
2020-02-09 10:15:17 +01:00
|
|
|
#include "bnsf_keys.h"
|
2010-03-21 05:23:18 +01:00
|
|
|
|
2020-02-09 10:15:17 +01:00
|
|
|
|
|
|
|
static void find_bnsf_key(g7221_codec_data *data, off_t start, STREAMFILE *sf, uint8_t *best_key);
|
|
|
|
|
|
|
|
/* BNSF - Bandai Namco Sound Format/File [Tales of Graces (Wii), Tales of Berseria (PS4)] */
|
2010-03-21 05:23:18 +01:00
|
|
|
VGMSTREAM * init_vgmstream_bnsf(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2017-10-14 12:42:44 +02:00
|
|
|
off_t start_offset = 0, first_offset = 0x0C;
|
2018-09-22 16:11:39 +02:00
|
|
|
int loop_flag = 0, channel_count = 0, sample_rate;
|
|
|
|
int num_samples, loop_start = 0, loop_end = 0, loop_adjust, block_samples;
|
2020-01-12 20:35:18 +01:00
|
|
|
uint32_t codec, flags = 0;
|
2018-09-22 16:11:39 +02:00
|
|
|
size_t bnsf_size, sdat_size, block_size;
|
2017-10-14 12:42:44 +02:00
|
|
|
off_t loop_chunk = 0, sfmt_chunk, sdat_chunk;
|
2010-03-21 05:23:18 +01:00
|
|
|
|
2017-10-14 12:42:44 +02:00
|
|
|
|
2018-09-22 16:11:39 +02:00
|
|
|
/* checks */
|
2017-10-14 12:42:44 +02:00
|
|
|
if (!check_extensions(streamFile,"bnsf"))
|
|
|
|
goto fail;
|
|
|
|
if (read_32bitBE(0,streamFile) != 0x424E5346) /* "BNSF" */
|
2010-03-21 05:23:18 +01:00
|
|
|
goto fail;
|
|
|
|
|
2020-02-09 10:15:17 +01:00
|
|
|
bnsf_size = read_32bitBE(0x04,streamFile);
|
2017-10-14 12:42:44 +02:00
|
|
|
codec = read_32bitBE(0x08,streamFile);
|
|
|
|
|
2020-02-09 10:15:17 +01:00
|
|
|
if (bnsf_size + (codec == 0x49533232 ? 0x00 : 0x08) != get_streamfile_size(streamFile)) /* IS22 uses full size */
|
2017-10-14 12:42:44 +02:00
|
|
|
goto fail;
|
2010-03-21 05:23:18 +01:00
|
|
|
|
2017-10-14 12:42:44 +02:00
|
|
|
if (!find_chunk_be(streamFile, 0x73666d74,first_offset,0, &sfmt_chunk,NULL)) /* "sfmt" */
|
|
|
|
goto fail;
|
|
|
|
if (!find_chunk_be(streamFile, 0x73646174,first_offset,0, &sdat_chunk,&sdat_size)) /* "sdat" */
|
|
|
|
goto fail;
|
|
|
|
if ( find_chunk_be(streamFile, 0x6C6F6F70,first_offset,0, &loop_chunk,NULL)) { /* "loop" */
|
|
|
|
loop_flag = 1;
|
2020-01-12 20:35:18 +01:00
|
|
|
loop_start = read_32bitBE(loop_chunk+0x00,streamFile); /* block-aligned */
|
2018-09-22 16:11:39 +02:00
|
|
|
loop_end = read_32bitBE(loop_chunk+0x04,streamFile) + 1;
|
2010-03-21 05:23:18 +01:00
|
|
|
}
|
|
|
|
|
2020-01-12 20:35:18 +01:00
|
|
|
flags = read_16bitBE(sfmt_chunk+0x00,streamFile);
|
2017-10-14 12:42:44 +02:00
|
|
|
channel_count = read_16bitBE(sfmt_chunk+0x02,streamFile);
|
2018-09-22 16:11:39 +02:00
|
|
|
sample_rate = read_32bitBE(sfmt_chunk+0x04,streamFile);
|
|
|
|
num_samples = read_32bitBE(sfmt_chunk+0x08,streamFile);
|
2020-02-09 10:15:17 +01:00
|
|
|
loop_adjust = read_32bitBE(sfmt_chunk+0x0c,streamFile); /* 0 when no loop */
|
2018-09-22 16:11:39 +02:00
|
|
|
block_size = read_16bitBE(sfmt_chunk+0x10,streamFile);
|
|
|
|
block_samples = read_16bitBE(sfmt_chunk+0x12,streamFile);
|
2020-02-09 10:15:17 +01:00
|
|
|
//max_samples = sdat_size / block_size * block_samples;
|
2010-03-21 05:23:18 +01:00
|
|
|
|
2017-10-14 12:42:44 +02:00
|
|
|
start_offset = sdat_chunk;
|
2010-03-21 05:23:18 +01:00
|
|
|
|
2020-02-09 10:15:17 +01:00
|
|
|
if (loop_adjust >= block_samples) /* decoder can't handle this */
|
2020-01-12 20:35:18 +01:00
|
|
|
goto fail;
|
2018-09-22 16:11:39 +02:00
|
|
|
|
2010-03-21 05:23:18 +01:00
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
2020-02-09 10:15:17 +01:00
|
|
|
vgmstream = allocate_vgmstream(channel_count, loop_flag);
|
2010-03-21 05:23:18 +01:00
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
vgmstream->sample_rate = sample_rate;
|
2017-10-14 12:42:44 +02:00
|
|
|
vgmstream->num_samples = num_samples;
|
2018-09-22 16:11:39 +02:00
|
|
|
vgmstream->loop_start_sample = loop_start + loop_adjust;
|
2017-10-14 12:42:44 +02:00
|
|
|
vgmstream->loop_end_sample = loop_end;
|
2010-03-21 05:23:18 +01:00
|
|
|
|
2017-10-14 12:42:44 +02:00
|
|
|
vgmstream->meta_type = meta_BNSF;
|
2010-03-24 21:57:55 +01:00
|
|
|
vgmstream->layout_type = layout_interleave;
|
2020-02-09 10:15:17 +01:00
|
|
|
vgmstream->interleave_block_size = block_size / channel_count;
|
2010-03-21 05:23:18 +01:00
|
|
|
|
2017-10-14 12:42:44 +02:00
|
|
|
switch (codec) {
|
2010-03-23 21:57:12 +01:00
|
|
|
#ifdef VGM_USE_G7221
|
2020-01-12 20:35:18 +01:00
|
|
|
case 0x49533134: /* "IS14" (interleaved Siren14) */
|
2017-10-14 12:42:44 +02:00
|
|
|
vgmstream->coding_type = coding_G7221C;
|
|
|
|
vgmstream->codec_data = init_g7221(vgmstream->channels, vgmstream->interleave_block_size);
|
|
|
|
if (!vgmstream->codec_data) goto fail;
|
2010-03-23 21:57:12 +01:00
|
|
|
|
2020-02-09 10:15:17 +01:00
|
|
|
/* get decryption key in .bnsfkey file or list, for later games' voices
|
|
|
|
* [The Idolm@ster 2 (PS3/X360), Tales of Zestiria (PS3/PC)] */
|
|
|
|
if (flags != 0) { /* only known value is 0x02 though */
|
|
|
|
size_t keysize;
|
|
|
|
uint8_t key[24] = {0}; /* keystring 0-padded to 192-bit */
|
|
|
|
|
|
|
|
keysize = read_key_file(key, sizeof(key), streamFile);
|
|
|
|
if (keysize <= 0 || keysize > sizeof(key)) {
|
|
|
|
find_bnsf_key(vgmstream->codec_data, start_offset, streamFile, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_key_g7221(vgmstream->codec_data, key);
|
|
|
|
}
|
|
|
|
|
2017-10-14 12:42:44 +02:00
|
|
|
break;
|
|
|
|
#endif
|
2015-01-25 06:08:25 +01:00
|
|
|
#ifdef VGM_USE_G719
|
2020-01-12 20:35:18 +01:00
|
|
|
case 0x49533232: /* "IS22" (interleaved Siren22) */
|
2020-02-09 10:15:17 +01:00
|
|
|
|
|
|
|
/* same encryption as IS14 but not seen */
|
|
|
|
if (flags != 0)
|
|
|
|
goto fail;
|
|
|
|
|
2017-10-14 12:42:44 +02:00
|
|
|
vgmstream->coding_type = coding_G719;
|
|
|
|
vgmstream->codec_data = init_g719(vgmstream->channels, vgmstream->interleave_block_size);
|
|
|
|
if (!vgmstream->codec_data) goto fail;
|
|
|
|
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2015-01-25 06:08:25 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2010-03-21 05:23:18 +01:00
|
|
|
|
|
|
|
|
2017-10-14 12:42:44 +02:00
|
|
|
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
|
|
|
|
goto fail;
|
2010-03-21 05:23:18 +01:00
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2017-10-14 12:42:44 +02:00
|
|
|
close_vgmstream(vgmstream);
|
2010-03-21 05:23:18 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-02-09 10:15:17 +01:00
|
|
|
|
|
|
|
static void find_bnsf_key(g7221_codec_data* data, off_t start, STREAMFILE* sf, uint8_t* best_key) {
|
|
|
|
const size_t keys_length = sizeof(s14key_list) / sizeof(bnsfkey_info);
|
|
|
|
int score, best_score = -1;
|
|
|
|
int i;
|
|
|
|
uint8_t tmpkey[24];
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < keys_length; i++) {
|
|
|
|
const char* key = s14key_list[i].key;
|
|
|
|
int keylen = strlen(key);
|
|
|
|
|
|
|
|
if (keylen > sizeof(tmpkey))
|
|
|
|
continue;
|
|
|
|
memcpy(tmpkey, key, keylen);
|
|
|
|
memset(tmpkey + keylen, 0, sizeof(tmpkey) - keylen);
|
|
|
|
|
|
|
|
//;VGM_LOG("BNSF: test key=%.24s\n", tmpkey);
|
|
|
|
set_key_g7221(data, tmpkey);
|
|
|
|
|
|
|
|
score = test_key_g7221(data, start, sf);
|
|
|
|
if (score < 0) continue;
|
|
|
|
|
|
|
|
if (best_score <= 0 || (score < best_score && score > 0)) {
|
|
|
|
best_score = score;
|
|
|
|
memcpy(best_key, key, keylen);
|
|
|
|
memset(best_key + keylen, 0, sizeof(tmpkey) - keylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (best_score == 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
VGM_ASSERT(best_score > 0, "BNSF: best key=%.24s (score=%i)\n", best_key, best_score);
|
|
|
|
VGM_ASSERT(best_score < 0, "BNSF: key not found\n"); /* defaults to all 0s */
|
|
|
|
}
|