Add SEAD .mab/sab names and cleanup

This commit is contained in:
bnnm 2019-09-15 14:57:55 +02:00
parent f694f7d379
commit b1f03eaa6d
5 changed files with 579 additions and 210 deletions

View File

@ -316,6 +316,10 @@
RelativePath=".\meta\sqex_scd_streamfile.h"
>
</File>
<File
RelativePath=".\meta\sqex_sead_streamfile.h"
>
</File>
<File
RelativePath=".\meta\txth_streamfile.h"
>

View File

@ -121,6 +121,7 @@
<ClInclude Include="meta\opus_interleave_streamfile.h" />
<ClInclude Include="meta\sfh_streamfile.h" />
<ClInclude Include="meta\sqex_scd_streamfile.h" />
<ClInclude Include="meta\sqex_sead_streamfile.h" />
<ClInclude Include="meta\txth_streamfile.h" />
<ClInclude Include="meta\ubi_bao_streamfile.h" />
<ClInclude Include="meta\ubi_sb_streamfile.h" />

View File

@ -134,6 +134,9 @@
<ClInclude Include="meta\sqex_scd_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\sqex_sead_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\txth_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>

View File

@ -1,186 +1,137 @@
#include "meta.h"
#include "../coding/coding.h"
#include "sqex_sead_streamfile.h"
static STREAMFILE* setup_sead_hca_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size, int encryption, size_t header_size, size_t key_start);
typedef struct {
int big_endian;
int version;
int is_sab;
int is_mab;
int total_subsongs;
int target_subsong;
uint16_t wave_id;
int loop_flag;
int channel_count;
int codec;
int sample_rate;
int loop_start;
int loop_end;
off_t meta_offset;
off_t extradata_offset;
size_t extradata_size;
size_t stream_size;
size_t special_size;
off_t descriptor_offset;
size_t descriptor_size;
off_t filename_offset;
size_t filename_size;
off_t cuename_offset;
size_t cuename_size;
off_t modename_offset;
size_t modename_size;
off_t instname_offset;
size_t instname_size;
off_t sndname_offset;
size_t sndname_size;
off_t sections_offset;
off_t snd_offset;
off_t trk_offset;
off_t musc_offset;
off_t inst_offset;
off_t mtrl_offset;
char readable_name[STREAM_NAME_SIZE];
} sead_header;
static int parse_sead(sead_header *sead, STREAMFILE *sf);
/* SABF/MABF - Square Enix's "sead" audio games [Dragon Quest Builders (PS3), Dissidia Opera Omnia (mobile), FF XV (PS4)] */
VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile) {
VGMSTREAM * vgmstream = NULL;
off_t start_offset, tables_offset, mtrl_offset, meta_offset, extradata_offset; //, info_offset, name_offset = 0;
size_t stream_size, descriptor_size, extradata_size, special_size; //, name_size = 0;
int loop_flag = 0, channel_count, codec, sample_rate, loop_start, loop_end;
int is_sab = 0, is_mab = 0;
int total_subsongs, target_subsong = streamFile->stream_index;
sead_header sead = {0};
off_t start_offset;
int target_subsong = streamFile->stream_index;
int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL;
int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL;
/* check extensions (.sab: sound/bgm, .mab: music, .sbin: Dissidia Opera Omnia .sab) */
if ( !check_extensions(streamFile,"sab,mab,sbin"))
/* checks */
/* .sab: sound/bgm
* .mab: music
* .sbin: Dissidia Opera Omnia .sab */
if (!check_extensions(streamFile,"sab,mab,sbin"))
goto fail;
/** main header **/
if (read_32bitBE(0x00,streamFile) == 0x73616266) { /* "sabf" */
is_sab = 1;
sead.is_sab = 1;
} else if (read_32bitBE(0x00,streamFile) == 0x6D616266) { /* "mabf" */
is_mab = 1;
sead.is_mab = 1;
} else {
/* there are other SEAD files with other chunks but similar formats too */
goto fail;
}
//if (read_8bit(0x04,streamFile) != 0x02) /* version? */
// goto fail;
/* 0x04(1): version? (usually 0x02, rarely 0x01, ex FF XV title) */
/* 0x05(1): 0x00/01? */
/* 0x06(2): version? (usually 0x10, rarely 0x20) */
if (read_16bitBE(0x06,streamFile) < 0x100) { /* use some value as no apparent flag */
sead.big_endian = guess_endianness16bit(0x06,streamFile); /* use some value as no apparent flag */
if (sead.big_endian) {
read_32bit = read_32bitBE;
read_16bit = read_16bitBE;
} else {
read_32bit = read_32bitLE;
read_16bit = read_16bitLE;
}
/* 0x08(1): version 0x04?, 0x0a(2): ? */
descriptor_size = read_8bit(0x09,streamFile);
if (read_32bit(0x0c,streamFile) != get_streamfile_size(streamFile))
sead.target_subsong = target_subsong;
if (!parse_sead(&sead, streamFile))
goto fail;
/* 0x10(n): file descriptor ("BGM", "Music", "SE", etc, long names are ok), padded */
tables_offset = 0x10 + (descriptor_size + 0x01); /* string null seems counted for padding */
if (tables_offset % 0x10)
tables_offset += 0x10 - (tables_offset % 0x10);
/** offset tables **/
if (is_sab) {
if (read_32bitBE(tables_offset+0x00,streamFile) != 0x736E6420) goto fail; /* "snd " (info) */
if (read_32bitBE(tables_offset+0x10,streamFile) != 0x73657120) goto fail; /* "seq " (unknown) */
if (read_32bitBE(tables_offset+0x20,streamFile) != 0x74726B20) goto fail; /* "trk " (unknown) */
if (read_32bitBE(tables_offset+0x30,streamFile) != 0x6D74726C) goto fail; /* "mtrl" (headers/streams) */
//info_offset = read_32bit(tables_offset+0x08,streamFile);
//seq_offset = read_32bit(tables_offset+0x18,streamFile);
//trk_offset = read_32bit(tables_offset+0x28,streamFile);
mtrl_offset = read_32bit(tables_offset+0x38,streamFile);
}
else if (is_mab) {
if (read_32bitBE(tables_offset+0x00,streamFile) != 0x6D757363) goto fail; /* "musc" (info) */
if (read_32bitBE(tables_offset+0x10,streamFile) != 0x696E7374) goto fail; /* "inst" (unknown) */
if (read_32bitBE(tables_offset+0x20,streamFile) != 0x6D74726C) goto fail; /* "mtrl" (headers/streams) */
//info_offset = read_32bit(tables_offset+0x08,streamFile);
//inst_offset = read_32bit(tables_offset+0x18,streamFile);
mtrl_offset = read_32bit(tables_offset+0x28,streamFile);
}
else {
goto fail;
}
/* each section starts with:
* 0x00(2): 0x00/01?, 0x02: size? (0x10), 0x04(2): entries, 0x06+: padded to 0x10
* 0x10+0x04*entry: offset from section start, also padded to 0x10 at the end */
/* find meta_offset in mtrl and total subsongs */
{
int i;
int entries = read_16bit(mtrl_offset+0x04,streamFile);
off_t entries_offset = mtrl_offset + 0x10;
if (target_subsong == 0) target_subsong = 1;
total_subsongs = 0;
meta_offset = 0;
/* manually find subsongs as entries can be dummy (ex. sfx banks in Dissidia Opera Omnia) */
for (i = 0; i < entries; i++) {
off_t entry_offset = mtrl_offset + read_32bit(entries_offset + i*0x04,streamFile);
if (read_8bit(entry_offset+0x05,streamFile) == 0)
continue; /* codec 0 when dummy */
total_subsongs++;
if (!meta_offset && total_subsongs == target_subsong)
meta_offset = entry_offset;
}
if (meta_offset == 0) goto fail;
/* SAB can contain 0 entries too */
}
/** stream header **/
/* 0x00(2): 0x00/01? */
/* 0x02(2): base entry size? (0x20) */
channel_count = read_8bit(meta_offset+0x04,streamFile);
codec = read_8bit(meta_offset+0x05,streamFile);
//entry_id = read_16bit(meta_offset+0x06,streamFile);
sample_rate = read_32bit(meta_offset+0x08,streamFile);
loop_start = read_32bit(meta_offset+0x0c,streamFile); /* in samples but usually ignored */
loop_end = read_32bit(meta_offset+0x10,streamFile);
extradata_size = read_32bit(meta_offset+0x14,streamFile); /* including subfile header, can be 0 */
stream_size = read_32bit(meta_offset+0x18,streamFile); /* not including subfile header */
special_size = read_32bit(meta_offset+0x1c,streamFile);
loop_flag = (loop_end > 0);
extradata_offset = meta_offset + 0x20;
/** info section (get stream name) **/
//if (is_sab) { //todo load name based on entry id
/* "snd " */
/* 0x08(2): file number within descriptor */
/* 0x1a(2): base_entry size (-0x10?) */
//name_size = read_32bit(snd_offset+0x20,streamFile);
//name_offset = snd_offset+0x70;
/* 0x24(4): unique id? (referenced in "seq" section) */
//}
//else if (is_mab) {
/* "musc" */
//looks like a "music cue" section, pointing to one subsection per "material".
// ex. one cue may point to 3 named subsongs/sections.
// some common header info from all materials is repeated (ex. sample rate), while other
// (loops, maybe proper num_samples) are listed per material but don't always match thei header
//}
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
vgmstream = allocate_vgmstream(sead.channel_count, sead.loop_flag);
if (!vgmstream) goto fail;
vgmstream->sample_rate = sample_rate;
vgmstream->num_streams = total_subsongs;
vgmstream->stream_size = stream_size;
vgmstream->meta_type = is_sab ? meta_SQEX_SAB : meta_SQEX_MAB;
vgmstream->meta_type = sead.is_sab ? meta_SQEX_SAB : meta_SQEX_MAB;
vgmstream->sample_rate = sead.sample_rate;
vgmstream->num_streams = sead.total_subsongs;
vgmstream->stream_size = sead.stream_size;
strcpy(vgmstream->stream_name, sead.readable_name);
switch(codec) {
switch(sead.codec) {
case 0x01: { /* PCM [Chrono Trigger sfx (PC)] */
start_offset = extradata_offset + extradata_size;
start_offset = sead.extradata_offset + sead.extradata_size;
vgmstream->coding_type = coding_PCM16LE;
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = 0x02;
vgmstream->num_samples = pcm_bytes_to_samples(stream_size, vgmstream->channels, 16);
vgmstream->loop_start_sample = loop_start;
vgmstream->loop_end_sample = loop_end;
vgmstream->num_samples = pcm_bytes_to_samples(sead.stream_size, vgmstream->channels, 16);
vgmstream->loop_start_sample = sead.loop_start;
vgmstream->loop_end_sample = sead.loop_end;
break;
}
case 0x02: { /* MSADPCM [Dragon Quest Builders (Vita) sfx] */
start_offset = extradata_offset + extradata_size;
start_offset = sead.extradata_offset + sead.extradata_size;
/* 0x00 (2): null?, 0x02(2): entry size? */
vgmstream->coding_type = coding_MSADPCM;
vgmstream->layout_type = layout_none;
vgmstream->interleave_block_size = read_16bit(extradata_offset+0x04,streamFile);
vgmstream->interleave_block_size = read_16bit(sead.extradata_offset+0x04,streamFile);
/* much like AKBs, there are slightly different loop values here, probably more accurate
* (if no loop, loop_end doubles as num_samples) */
vgmstream->num_samples = msadpcm_bytes_to_samples(stream_size, vgmstream->interleave_block_size, vgmstream->channels);
vgmstream->loop_start_sample = read_32bit(extradata_offset+0x08, streamFile); //loop_start
vgmstream->loop_end_sample = read_32bit(extradata_offset+0x0c, streamFile); //loop_end
vgmstream->num_samples = msadpcm_bytes_to_samples(sead.stream_size, vgmstream->interleave_block_size, vgmstream->channels);
vgmstream->loop_start_sample = read_32bit(sead.extradata_offset+0x08, streamFile); //loop_start
vgmstream->loop_end_sample = read_32bit(sead.extradata_offset+0x0c, streamFile); //loop_end
break;
}
@ -188,17 +139,18 @@ VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile) {
case 0x03: { /* OGG [Final Fantasy XV Benchmark sfx (PC)] */
VGMSTREAM *ogg_vgmstream = NULL;
ogg_vorbis_meta_info_t ovmi = {0};
off_t subfile_offset = extradata_offset + extradata_size;
off_t subfile_offset = sead.extradata_offset + sead.extradata_size;
ovmi.meta_type = vgmstream->meta_type;
ovmi.total_subsongs = total_subsongs;
ovmi.stream_size = stream_size;
ovmi.total_subsongs = sead.total_subsongs;
ovmi.stream_size = sead.stream_size;
/* post header has some kind of repeated values, config/table? */
ogg_vgmstream = init_vgmstream_ogg_vorbis_callbacks(streamFile, NULL, subfile_offset, &ovmi);
if (ogg_vgmstream) {
ogg_vgmstream->num_streams = vgmstream->num_streams;
ogg_vgmstream->stream_size = vgmstream->stream_size;
strcpy(ogg_vgmstream->stream_name, vgmstream->stream_name);
close_vgmstream(vgmstream);
return ogg_vgmstream;
@ -215,21 +167,21 @@ VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile) {
case 0x04: { /* ATRAC9 [Dragon Quest Builders (Vita), Final Fantaxy XV (PS4)] */
atrac9_config cfg = {0};
start_offset = extradata_offset + extradata_size;
start_offset = sead.extradata_offset + sead.extradata_size;
/* post header has various typical ATRAC9 values */
cfg.channels = vgmstream->channels;
cfg.config_data = read_32bit(extradata_offset+0x0c,streamFile);
cfg.encoder_delay = read_32bit(extradata_offset+0x18,streamFile);
cfg.config_data = read_32bit(sead.extradata_offset+0x0c,streamFile);
cfg.encoder_delay = read_32bit(sead.extradata_offset+0x18,streamFile);
vgmstream->codec_data = init_atrac9(&cfg);
if (!vgmstream->codec_data) goto fail;
vgmstream->coding_type = coding_ATRAC9;
vgmstream->layout_type = layout_none;
vgmstream->sample_rate = read_32bit(extradata_offset+0x1c,streamFile); /* SAB's sample rate can be different but it's ignored */
vgmstream->num_samples = read_32bit(extradata_offset+0x10,streamFile); /* loop values above are also weird and ignored */
vgmstream->loop_start_sample = read_32bit(extradata_offset+0x20, streamFile) - (loop_flag ? cfg.encoder_delay : 0); //loop_start
vgmstream->loop_end_sample = read_32bit(extradata_offset+0x24, streamFile) - (loop_flag ? cfg.encoder_delay : 0); //loop_end
vgmstream->sample_rate = read_32bit(sead.extradata_offset+0x1c,streamFile); /* SAB's sample rate can be different but it's ignored */
vgmstream->num_samples = read_32bit(sead.extradata_offset+0x10,streamFile); /* loop values above are also weird and ignored */
vgmstream->loop_start_sample = read_32bit(sead.extradata_offset+0x20, streamFile) - (sead.loop_flag ? cfg.encoder_delay : 0); //loop_start
vgmstream->loop_end_sample = read_32bit(sead.extradata_offset+0x24, streamFile) - (sead.loop_flag ? cfg.encoder_delay : 0); //loop_end
break;
}
#endif
@ -239,7 +191,7 @@ VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile) {
mpeg_codec_data *mpeg_data = NULL;
mpeg_custom_config cfg = {0};
start_offset = extradata_offset + extradata_size;
start_offset = sead.extradata_offset + sead.extradata_size;
/* post header is a proper MSF, but sample rate/loops are ignored in favor of SAB's */
mpeg_data = init_mpeg_custom(streamFile, start_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_STANDARD, &cfg);
@ -247,9 +199,9 @@ VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile) {
vgmstream->codec_data = mpeg_data;
vgmstream->layout_type = layout_none;
vgmstream->num_samples = mpeg_bytes_to_samples(stream_size, mpeg_data);
vgmstream->loop_start_sample = loop_start;
vgmstream->loop_end_sample = loop_end;
vgmstream->num_samples = mpeg_bytes_to_samples(sead.stream_size, mpeg_data);
vgmstream->loop_start_sample = sead.loop_start;
vgmstream->loop_end_sample = sead.loop_end;
break;
}
#endif
@ -258,16 +210,16 @@ VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile) {
//todo there is no easy way to use the HCA decoder; try subfile hack for now
VGMSTREAM *temp_vgmstream = NULL;
STREAMFILE *temp_streamFile = NULL;
off_t subfile_offset = extradata_offset + 0x10;
size_t subfile_size = stream_size + extradata_size - 0x10;
off_t subfile_offset = sead.extradata_offset + 0x10;
size_t subfile_size = sead.stream_size + sead.extradata_size - 0x10;
/* post header: values from the HCA header, in file endianness + HCA header */
size_t key_start = special_size & 0xff;
size_t header_size = read_16bit(extradata_offset+0x02, streamFile);
int encryption = read_16bit(extradata_offset+0x0c, streamFile); //maybe 8bit?
size_t key_start = sead.special_size & 0xff;
size_t header_size = read_16bit(sead.extradata_offset+0x02, streamFile);
int encryption = read_16bit(sead.extradata_offset+0x0c, streamFile); //maybe 8bit?
/* encryption type 0x01 found in Final Fantasy XII TZA (PS4/PC) */
temp_streamFile = setup_sead_hca_streamfile(streamFile, subfile_offset, subfile_size, encryption, header_size, key_start);
temp_streamFile = setup_sqex_sead_streamfile(streamFile, subfile_offset, subfile_size, encryption, header_size, key_start);
if (!temp_streamFile) goto fail;
temp_vgmstream = init_vgmstream_hca(temp_streamFile);
@ -276,6 +228,7 @@ VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile) {
temp_vgmstream->num_streams = vgmstream->num_streams;
temp_vgmstream->stream_size = vgmstream->stream_size;
temp_vgmstream->meta_type = vgmstream->meta_type;
strcpy(temp_vgmstream->stream_name, vgmstream->stream_name);
close_streamfile(temp_streamFile);
close_vgmstream(vgmstream);
@ -289,10 +242,12 @@ VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile) {
case 0x00: /* dummy entry */
default:
VGM_LOG("SQEX SEAD: unknown codec %x\n", codec);
VGM_LOG("SQEX SEAD: unknown codec %x\n", sead.codec);
goto fail;
}
strcpy(vgmstream->stream_name, sead.readable_name);
/* open the file for reading */
if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) )
goto fail;
@ -304,79 +259,399 @@ fail:
}
typedef struct {
size_t header_size;
size_t key_start;
} sead_decryption_data;
static void build_readable_name(char * buf, size_t buf_size, sead_header *sead, STREAMFILE *sf) {
/* Encrypted HCA */
static size_t sead_decryption_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, sead_decryption_data* data) {
/* Found in FFXII_TZA.exe (same key in SCD Ogg V3) */
static const uint8_t encryption_key[0x100] = {
0x3A,0x32,0x32,0x32,0x03,0x7E,0x12,0xF7,0xB2,0xE2,0xA2,0x67,0x32,0x32,0x22,0x32, // 00-0F
0x32,0x52,0x16,0x1B,0x3C,0xA1,0x54,0x7B,0x1B,0x97,0xA6,0x93,0x1A,0x4B,0xAA,0xA6, // 10-1F
0x7A,0x7B,0x1B,0x97,0xA6,0xF7,0x02,0xBB,0xAA,0xA6,0xBB,0xF7,0x2A,0x51,0xBE,0x03, // 20-2F
0xF4,0x2A,0x51,0xBE,0x03,0xF4,0x2A,0x51,0xBE,0x12,0x06,0x56,0x27,0x32,0x32,0x36, // 30-3F
0x32,0xB2,0x1A,0x3B,0xBC,0x91,0xD4,0x7B,0x58,0xFC,0x0B,0x55,0x2A,0x15,0xBC,0x40, // 40-4F
0x92,0x0B,0x5B,0x7C,0x0A,0x95,0x12,0x35,0xB8,0x63,0xD2,0x0B,0x3B,0xF0,0xC7,0x14, // 50-5F
0x51,0x5C,0x94,0x86,0x94,0x59,0x5C,0xFC,0x1B,0x17,0x3A,0x3F,0x6B,0x37,0x32,0x32, // 60-6F
0x30,0x32,0x72,0x7A,0x13,0xB7,0x26,0x60,0x7A,0x13,0xB7,0x26,0x50,0xBA,0x13,0xB4, // 70-7F
0x2A,0x50,0xBA,0x13,0xB5,0x2E,0x40,0xFA,0x13,0x95,0xAE,0x40,0x38,0x18,0x9A,0x92, // 80-8F
0xB0,0x38,0x00,0xFA,0x12,0xB1,0x7E,0x00,0xDB,0x96,0xA1,0x7C,0x08,0xDB,0x9A,0x91, // 90-9F
0xBC,0x08,0xD8,0x1A,0x86,0xE2,0x70,0x39,0x1F,0x86,0xE0,0x78,0x7E,0x03,0xE7,0x64, // A0-AF
0x51,0x9C,0x8F,0x34,0x6F,0x4E,0x41,0xFC,0x0B,0xD5,0xAE,0x41,0xFC,0x0B,0xD5,0xAE, // B0-BF
0x41,0xFC,0x3B,0x70,0x71,0x64,0x33,0x32,0x12,0x32,0x32,0x36,0x70,0x34,0x2B,0x56, // C0-CF
0x22,0x70,0x3A,0x13,0xB7,0x26,0x60,0xBA,0x1B,0x94,0xAA,0x40,0x38,0x00,0xFA,0xB2, // D0-DF
0xE2,0xA2,0x67,0x32,0x32,0x12,0x32,0xB2,0x32,0x32,0x32,0x32,0x75,0xA3,0x26,0x7B, // E0-EF
0x83,0x26,0xF9,0x83,0x2E,0xFF,0xE3,0x16,0x7D,0xC0,0x1E,0x63,0x21,0x07,0xE3,0x01, // F0-FF
};
size_t bytes_read;
off_t encrypted_offset = data->header_size;
int i;
if (sead->is_sab) {
char descriptor[255], name[255];
bytes_read = streamfile->read(streamfile, dest, offset, length);
if (sead->descriptor_size > 255 || sead->sndname_size > 255) goto fail;
/* decrypt data (xor) */
if (offset >= encrypted_offset) {
for (i = 0; i < bytes_read; i++) {
dest[i] ^= encryption_key[(data->key_start + (offset - encrypted_offset) + i) % 0x100];
read_string(descriptor,sead->descriptor_size+1,sead->descriptor_offset, sf);
read_string(name,sead->sndname_size+1,sead->sndname_offset, sf);
snprintf(buf,buf_size, "%s/%s", descriptor, name);
}
else {
char descriptor[255], name[255], mode[255];
if (sead->descriptor_size > 255 || sead->filename_size > 255 || sead->cuename_size > 255 || sead->modename_size > 255) goto fail;
read_string(descriptor,sead->descriptor_size+1,sead->descriptor_offset, sf);
//read_string(filename,sead->filename_size+1,sead->filename_offset, sf); /* same as filename, not too interesting */
if (sead->cuename_offset)
read_string(name,sead->cuename_size+1,sead->cuename_offset, sf);
else if (sead->instname_offset)
read_string(name,sead->instname_size+1,sead->instname_offset, sf);
else
strcpy(name, "?");
read_string(mode,sead->modename_size+1,sead->modename_offset, sf);
/* default mode in most files, not very interesting */
if (strcmp(mode, "Mode") == 0 || strcmp(mode, "Mode0") == 0)
snprintf(buf,buf_size, "%s/%s", descriptor, name);
else
snprintf(buf,buf_size, "%s/%s/%s", descriptor, name, mode);
}
return;
fail:
VGM_LOG("SEAD: bad name found\n");
}
static void parse_sead_mab_name(sead_header *sead, STREAMFILE *sf) {
int32_t (*read_32bit)(off_t,STREAMFILE*) = sead->big_endian ? read_32bitBE : read_32bitLE;
int16_t (*read_16bit)(off_t,STREAMFILE*) = sead->big_endian ? read_16bitBE : read_16bitLE;
int i, entries, cue, mode, cue_count, mode_count;
off_t entry_offset, cue_offset, mode_offset, name_offset, table_offset;
size_t name_size;
//int wave, wave_count; off_t wave_offset, subtable_offset; uint16_t wave_id;
int name = 0;
/* find which name corresponds to our song (mabf can have N subsongs
* and X cues + Y modes and also Z instruments, one of which should reference it) */
//todo exact name matching unknown, assumes subsong N = name N
/* parse "musc" (music cue?) */
entries = read_16bit(sead->musc_offset + 0x04, sf);
for (i = 0; i < entries; i++) {
entry_offset = sead->musc_offset + read_32bit(sead->musc_offset + 0x10 + i*0x04, sf);
/* 0x00: config? */
sead->filename_offset = entry_offset + read_16bit(entry_offset + 0x02, sf);
cue_count = read_8bit(entry_offset + 0x04, sf);
mode_count = read_8bit(entry_offset + 0x05, sf);
/* 0x06: some low number? */
/* 0x07: always 0x80? (apparently not an offset/size) */
/* 0x08: id? */
/* 0x0a: 0? */
/* 0x44: sample rate */
/* others: unknown/null */
sead->filename_size = read_8bit(entry_offset + 0x48, sf);
/* table points to all cue offsets first then all modes offsets */
table_offset = align_size_to_block(sead->filename_offset + sead->filename_size + 0x01, 0x10);
/* cue name (ex. "bgm_007_take2" / "bgm_007s" / etc subsongs) */
for (cue = 0; cue < cue_count; cue++) {
cue_offset = sead->musc_offset + 0x20 + read_32bit(table_offset + cue*0x04, sf);
/* 0x00: id? */
name_offset = cue_offset + read_16bit(cue_offset + 0x02, sf);
name_size = read_8bit(cue_offset + 0x04, sf);
//wave_count = read_8bit(cue_offset + 0x05, sf);
/* 0x06: ? */
/* 0x0c: num samples */
/* 0x10: loop start */
/* 0x14: loop end */
/* 0x18: flag? */
/* others: ? */
name++;
if (name == sead->target_subsong || cue_count == 1) {
sead->cuename_offset = name_offset;
sead->cuename_size = name_size;
break;
}
#if 0 //this works for some games like KH3 but not others like FFXII
/* subtable: first N wave refs + ? unk refs (rarely more than 1 each) */
subtable_offset = align_size_to_block(name_offset + name_size + 1, 0x10);
for (wave = 0; wave < wave_count; wave++) {
wave_offset = cue_offset + read_32bit(subtable_offset + wave*0x04, sf);
/* 0x00: config? */
/* 0x02: entry size */
wave_id = read_16bit(wave_offset + 0x04, sf);
/* 0x06: null? */
/* 0x08: null? */
/* 0x0c: some id/config? */
if (wave_id == sead->wave_id) {
sead->cuename_offset = name_offset;
sead->cuename_size = name_size;
break;
}
}
if (sead->cuename_offset)
break;
#endif
}
/* mode name (ex. almost always "Mode" and only 1 entry, rarely "Water" / "Restaurant" / etc)
* no idea how modes are referenced (perhaps manually with in-game events)
* so just a quick hack, only found multiple in FFXV's bgm_gardina */
if (mode_count == sead->total_subsongs)
mode = sead->target_subsong - 1;
else
mode = 0;
{ //for (mode = 0; mode < mode_count; mode++) {
mode_offset = sead->musc_offset + 0x20 + read_32bit(table_offset + cue_count*0x04 + mode*0x04, sf);
/* 0x00: id? */
name_offset = mode_offset + read_16bit(mode_offset + 0x02, sf);
/* 0x04: mode id */
name_size = read_8bit(mode_offset + 0x06, sf);
/* 0x08: offset? */
/* others: floats and stuff */
sead->modename_offset = name_offset;
sead->modename_size = name_size;
}
}
return bytes_read;
/* parse "inst" (instruments) */
entries = read_16bit(sead->inst_offset + 0x04, sf);
for (i = 0; i < entries; i++) {
entry_offset = sead->inst_offset + read_32bit(sead->inst_offset + 0x10 + i*0x04, sf);
/* 0x00: id? */
/* 0x02: base size? */
/* 0x05: count? */
//wave_count = read_8bit(entry_offset + 0x06, sf);
/* 0x0c: num samples */
/* 0x10: loop start */
/* 0x14: loop end */
/* 0x18: flag? */
/* others: ? */
/* no apparent fields and inst is very rare (ex. KH3 tut) */
name_offset = entry_offset + 0x30;
name_size = 0x0F;
name++;
if (name == sead->target_subsong) {
sead->instname_offset = name_offset;
sead->instname_size = name_size;
break;
}
#if 0 //not actually tested
if (wave_count != 1) break; /* ? */
/* subtable: N wave refs? */
subtable_offset = align_size_to_block(name_offset + name_size + 1, 0x10);
for (wave = 0; wave < wave_count; wave++) {
wave_offset = subtable_offset + read_32bit(subtable_offset + wave*0x04, sf);
/* 0x00: config? */
/* 0x02: entry size? */
wave_id = read_16bit(wave_offset + 0x04, sf);
/* 0x06: ? */
/* 0x08: id/crc? */
/* 0x0c: ? */
/* 0x10: sample rate */
/* others: null? */
if (wave_id == sead->wave_id) {
sead->instname_offset = name_offset;
sead->instname_size = name_size;
break;
}
}
if (sead->instname_offset)
break;
#endif
}
}
static STREAMFILE* setup_sead_hca_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size, int encryption, size_t header_size, size_t key_start) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
static void parse_sead_sab_name(sead_header *sead, STREAMFILE *sf) {
int32_t (*read_32bit)(off_t,STREAMFILE*) = sead->big_endian ? read_32bitBE : read_32bitLE;
int16_t (*read_16bit)(off_t,STREAMFILE*) = sead->big_endian ? read_16bitBE : read_16bitLE;
int i, entries, snd_id, wave_id, snd_found = 0;
size_t size;
off_t entry_offset;
/* setup subfile */
new_streamFile = open_wrap_streamfile(streamFile);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_clamp_streamfile(temp_streamFile, subfile_offset,subfile_size);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
//todo looks mostly correct for many subsongs but in rare cases wave_ids aren't referenced
// or maybe id needs another jump (seq?) (ex. DQB se_break_soil, FFXV aircraftzeroone)
if (encryption) {
sead_decryption_data io_data = {0};
size_t io_data_size = sizeof(sead_decryption_data);
/* parse "trk" (track info) */
entries = read_16bit(sead->trk_offset + 0x04, sf);
for (i = 0; i < entries; i++) {
entry_offset = sead->trk_offset + read_32bit(sead->trk_offset + 0x10 + i*0x04, sf);
io_data.header_size = header_size;
io_data.key_start = key_start;
/* 0x00: type/count? */
size = read_16bit(entry_offset + 0x02, sf); /* bigger if 'type=03' */
/* 0x04: trk id? */
/* 0x04: some id? */
new_streamFile = open_io_streamfile(temp_streamFile, &io_data,io_data_size, sead_decryption_read,NULL);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
if (size > 0x10) {
snd_id = read_8bit(entry_offset + 0x10, sf);
wave_id = read_16bit(entry_offset + 0x11, sf);
}
else {
snd_id = read_16bit(entry_offset + 0x08, sf);
wave_id = read_16bit(entry_offset + 0x0a, sf);
}
if (wave_id == sead->wave_id) {
snd_found = 1;
break;
}
}
new_streamFile = open_fakename_streamfile(temp_streamFile, NULL,"hca");
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
if (!snd_found) {
if (sead->total_subsongs == 1) {
snd_id = 0; /* meh */
VGM_LOG("SEAD: snd_id not found, using first\n");
} else {
return;
}
}
return temp_streamFile;
fail:
close_streamfile(temp_streamFile);
return NULL;
/* parse "snd " (sound info) */
{
off_t entry_offset = sead->snd_offset + read_32bit(sead->snd_offset + 0x10 + snd_id*0x04, sf);
/* 0x00: config? */
sead->sndname_offset = entry_offset + read_16bit(entry_offset + 0x02, sf);
/* 0x04: count of ? */
/* 0x05: count of ? (0 if no sound exist in file) */
/* 0x06: some low number? */
/* 0x07: always 0x80? (apparently not an offset/size) */
/* 0x08: snd id */
/* 0x0a: 0? */
/* 0x0c: 1.0? */
/* 0x1a: header size? */
/* 0x1c: 30.0? * */
/* 0x24: crc/id? */
/* 0x46: header size? */
/* 0x4c: header size? */
if (sead->version == 1) {
sead->sndname_offset -= 0x10;
sead->sndname_size = read_8bit(entry_offset + 0x08, sf);
}
else {
sead->sndname_size = read_8bit(entry_offset + 0x23, sf);
}
/* 0x24: unique id? (referenced in "seq" section?) */
/* others: probably sound config like pan/volume (has floats and stuff) */
}
}
static int parse_sead(sead_header *sead, STREAMFILE *sf) {
int32_t (*read_32bit)(off_t,STREAMFILE*) = sead->big_endian ? read_32bitBE : read_32bitLE;
int16_t (*read_16bit)(off_t,STREAMFILE*) = sead->big_endian ? read_16bitBE : read_16bitLE;
/** base header **/
sead->version = read_8bit(0x04, sf); /* usually 0x02, rarely 0x01 (ex FF XV early songs) */
/* 0x05(1): 0/1? */
/* 0x06(2): ? (usually 0x10, rarely 0x20) */
/* 0x08(1): 3/4? */
sead->descriptor_size = read_8bit(0x09, sf);
/* 0x0a(2): ? */
if (read_32bit(0x0c, sf) != get_streamfile_size(sf))
goto fail;
if (sead->descriptor_size == 0) /* not set when version == 1 */
sead->descriptor_size = 0x0f;
sead->descriptor_offset = 0x10; /* file descriptor ("BGM", "Music2", "SE", etc, long names are ok) */
sead->sections_offset = sead->descriptor_offset + (sead->descriptor_size + 0x01); /* string null matters for padding */
sead->sections_offset = align_size_to_block(sead->sections_offset, 0x10);
/** offsets to sections **/
if (sead->is_sab) {
if (read_32bitBE(sead->sections_offset + 0x00, sf) != 0x736E6420) goto fail; /* "snd " (sonds) */
if (read_32bitBE(sead->sections_offset + 0x10, sf) != 0x73657120) goto fail; /* "seq " (unknown) */
if (read_32bitBE(sead->sections_offset + 0x20, sf) != 0x74726B20) goto fail; /* "trk " (unknown) */
if (read_32bitBE(sead->sections_offset + 0x30, sf) != 0x6D74726C) goto fail; /* "mtrl" (headers/streams) */
sead->snd_offset = read_32bit(sead->sections_offset + 0x08, sf);
//sead->seq_offset = read_32bit(sead->sections_offset + 0x18, sf);
sead->trk_offset = read_32bit(sead->sections_offset + 0x28, sf);
sead->mtrl_offset = read_32bit(sead->sections_offset + 0x38, sf);
}
else if (sead->is_mab) {
if (read_32bitBE(sead->sections_offset + 0x00, sf) != 0x6D757363) goto fail; /* "musc" (cues) */
if (read_32bitBE(sead->sections_offset + 0x10, sf) != 0x696E7374) goto fail; /* "inst" (instruments) */
if (read_32bitBE(sead->sections_offset + 0x20, sf) != 0x6D74726C) goto fail; /* "mtrl" (headers/streams) */
sead->musc_offset = read_32bit(sead->sections_offset + 0x08, sf);
sead->inst_offset = read_32bit(sead->sections_offset + 0x18, sf);
sead->mtrl_offset = read_32bit(sead->sections_offset + 0x28, sf);
}
else {
goto fail;
}
/* section format at offset:
* 0x00(2): 0/1?
* 0x02(2): header size? (always 0x10)
* 0x04(2): entries
* 0x06(+): padded to 0x10
* 0x10 + 0x04*entry: offset to entry from table start (also padded to 0x10 at the end) */
/* find meta_offset in "mtrl" and total subsongs */
{
int i, entries;
entries = read_16bit(sead->mtrl_offset+0x04, sf);
if (sead->target_subsong == 0) sead->target_subsong = 1;
sead->total_subsongs = 0;
sead->meta_offset = 0;
/* manually find subsongs as entries can be dummy (ex. sfx banks in Dissidia Opera Omnia) */
for (i = 0; i < entries; i++) {
off_t entry_offset = sead->mtrl_offset + read_32bit(sead->mtrl_offset + 0x10 + i*0x04, sf);
if (read_8bit(entry_offset + 0x05, sf) == 0) {
continue; /* codec 0 when dummy (see stream header) */
}
sead->total_subsongs++;
if (!sead->meta_offset && sead->total_subsongs == sead->target_subsong) {
sead->meta_offset = entry_offset;
}
}
if (sead->meta_offset == 0) goto fail;
/* SAB can contain 0 entries too */
}
/** stream header **/
/* 0x00(2): 0x00/01? */
/* 0x02(2): base entry size? (0x20) */
sead->channel_count = read_8bit(sead->meta_offset + 0x04, sf);
sead->codec = read_8bit(sead->meta_offset + 0x05, sf);
sead->wave_id = read_16bit(sead->meta_offset + 0x06, sf); /* 0..N */
sead->sample_rate = read_32bit(sead->meta_offset + 0x08, sf);
sead->loop_start = read_32bit(sead->meta_offset + 0x0c, sf); /* in samples but usually ignored */
sead->loop_end = read_32bit(sead->meta_offset + 0x10, sf);
sead->extradata_size = read_32bit(sead->meta_offset + 0x14, sf); /* including subfile header, can be 0 */
sead->stream_size = read_32bit(sead->meta_offset + 0x18, sf); /* not including subfile header */
sead->special_size = read_32bit(sead->meta_offset + 0x1c, sf);
sead->loop_flag = (sead->loop_end > 0);
sead->extradata_offset = sead->meta_offset + 0x20;
/** info section (get stream name) **/
if (sead->is_sab) {
parse_sead_sab_name(sead, sf);
}
else if (sead->is_mab) {
parse_sead_mab_name(sead, sf);
}
build_readable_name(sead->readable_name, sizeof(sead->readable_name), sead, sf);
return 1;
fail:
return 0;
}

View File

@ -0,0 +1,86 @@
#ifndef _SQEX_SEAD_STREAMFILE_H_
#define _SQEX_SEAD_STREAMFILE_H_
#include "../streamfile.h"
static STREAMFILE* setup_sqex_sead_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size, int encryption, size_t header_size, size_t key_start);
typedef struct {
size_t header_size;
size_t key_start;
} sqex_sead_decryption_data;
static size_t sqex_sead_decryption_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, sqex_sead_decryption_data* data) {
/* Found in FFXII_TZA.exe (same key in SCD Ogg V3) */
static const uint8_t encryption_key[0x100] = {
0x3A,0x32,0x32,0x32,0x03,0x7E,0x12,0xF7,0xB2,0xE2,0xA2,0x67,0x32,0x32,0x22,0x32, // 00-0F
0x32,0x52,0x16,0x1B,0x3C,0xA1,0x54,0x7B,0x1B,0x97,0xA6,0x93,0x1A,0x4B,0xAA,0xA6, // 10-1F
0x7A,0x7B,0x1B,0x97,0xA6,0xF7,0x02,0xBB,0xAA,0xA6,0xBB,0xF7,0x2A,0x51,0xBE,0x03, // 20-2F
0xF4,0x2A,0x51,0xBE,0x03,0xF4,0x2A,0x51,0xBE,0x12,0x06,0x56,0x27,0x32,0x32,0x36, // 30-3F
0x32,0xB2,0x1A,0x3B,0xBC,0x91,0xD4,0x7B,0x58,0xFC,0x0B,0x55,0x2A,0x15,0xBC,0x40, // 40-4F
0x92,0x0B,0x5B,0x7C,0x0A,0x95,0x12,0x35,0xB8,0x63,0xD2,0x0B,0x3B,0xF0,0xC7,0x14, // 50-5F
0x51,0x5C,0x94,0x86,0x94,0x59,0x5C,0xFC,0x1B,0x17,0x3A,0x3F,0x6B,0x37,0x32,0x32, // 60-6F
0x30,0x32,0x72,0x7A,0x13,0xB7,0x26,0x60,0x7A,0x13,0xB7,0x26,0x50,0xBA,0x13,0xB4, // 70-7F
0x2A,0x50,0xBA,0x13,0xB5,0x2E,0x40,0xFA,0x13,0x95,0xAE,0x40,0x38,0x18,0x9A,0x92, // 80-8F
0xB0,0x38,0x00,0xFA,0x12,0xB1,0x7E,0x00,0xDB,0x96,0xA1,0x7C,0x08,0xDB,0x9A,0x91, // 90-9F
0xBC,0x08,0xD8,0x1A,0x86,0xE2,0x70,0x39,0x1F,0x86,0xE0,0x78,0x7E,0x03,0xE7,0x64, // A0-AF
0x51,0x9C,0x8F,0x34,0x6F,0x4E,0x41,0xFC,0x0B,0xD5,0xAE,0x41,0xFC,0x0B,0xD5,0xAE, // B0-BF
0x41,0xFC,0x3B,0x70,0x71,0x64,0x33,0x32,0x12,0x32,0x32,0x36,0x70,0x34,0x2B,0x56, // C0-CF
0x22,0x70,0x3A,0x13,0xB7,0x26,0x60,0xBA,0x1B,0x94,0xAA,0x40,0x38,0x00,0xFA,0xB2, // D0-DF
0xE2,0xA2,0x67,0x32,0x32,0x12,0x32,0xB2,0x32,0x32,0x32,0x32,0x75,0xA3,0x26,0x7B, // E0-EF
0x83,0x26,0xF9,0x83,0x2E,0xFF,0xE3,0x16,0x7D,0xC0,0x1E,0x63,0x21,0x07,0xE3,0x01, // F0-FF
};
size_t bytes_read;
off_t encrypted_offset = data->header_size;
int i;
bytes_read = streamfile->read(streamfile, dest, offset, length);
/* decrypt data (xor) */
if (offset >= encrypted_offset) {
for (i = 0; i < bytes_read; i++) {
dest[i] ^= encryption_key[(data->key_start + (offset - encrypted_offset) + i) % 0x100];
}
}
return bytes_read;
}
/* decrypts subfile if neccessary */
static STREAMFILE* setup_sqex_sead_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size, int encryption, size_t header_size, size_t key_start) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
/* setup subfile */
new_streamFile = open_wrap_streamfile(streamFile);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_clamp_streamfile(temp_streamFile, subfile_offset,subfile_size);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
if (encryption) {
sqex_sead_decryption_data io_data = {0};
size_t io_data_size = sizeof(sqex_sead_decryption_data);
io_data.header_size = header_size;
io_data.key_start = key_start;
new_streamFile = open_io_streamfile(temp_streamFile, &io_data,io_data_size, sqex_sead_decryption_read,NULL);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
}
new_streamFile = open_fakename_streamfile(temp_streamFile, NULL,"hca");
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
return temp_streamFile;
fail:
close_streamfile(temp_streamFile);
return NULL;
}
#endif /* _SQEX_SEAD_STREAMFILE_H_ */