From 2ef0663d79769f080e8477a88e9fd5ef5d5966ee Mon Sep 17 00:00:00 2001 From: bnnm Date: Sat, 27 Jan 2018 22:34:48 +0100 Subject: [PATCH] Add SQEX SAB/MAB (MPEG/ATRAC9/HCA/MSADPCM) [DQ Builders, FF XV (PS4)] --- src/formats.c | 4 + src/libvgmstream.vcproj | 4 + src/libvgmstream.vcxproj | 1 + src/libvgmstream.vcxproj.filters | 3 + src/meta/meta.h | 2 + src/meta/sqex_sead.c | 271 +++++++++++++++++++++++++++++++ src/vgmstream.c | 1 + src/vgmstream.h | 2 + 8 files changed, 288 insertions(+) create mode 100644 src/meta/sqex_sead.c diff --git a/src/formats.c b/src/formats.c index 1fd59b6a..004c724e 100644 --- a/src/formats.c +++ b/src/formats.c @@ -175,6 +175,7 @@ static const char* extension_list[] = { "lstm", //fake extension, for STMs "lwav", //fake extension, for WAVs + "mab", "matx", "mc3", "mca", @@ -265,6 +266,7 @@ static const char* extension_list[] = { "sb5", "sb6", "sb7", + "sbin", "sc", "scd", "sck", @@ -951,6 +953,8 @@ static const meta_info meta_info_list[] = { {meta_OGG_ISD, "Ogg Vorbis (ISD)"}, {meta_KMA9, "Koei Tecmo KMA9 header"}, {meta_XWC, "Starbreeze XWC header"}, + {meta_SQEX_SAB, "Square-Enix SAB header"}, + {meta_SQEX_MAB, "Square-Enix MAB header"}, #ifdef VGM_USE_MP4V2 {meta_MP4, "AAC header"}, diff --git a/src/libvgmstream.vcproj b/src/libvgmstream.vcproj index ffd8300f..6257180d 100644 --- a/src/libvgmstream.vcproj +++ b/src/libvgmstream.vcproj @@ -1198,6 +1198,10 @@ RelativePath=".\meta\sqex_scd.c" > + + diff --git a/src/libvgmstream.vcxproj b/src/libvgmstream.vcxproj index 564e1937..1941b101 100644 --- a/src/libvgmstream.vcxproj +++ b/src/libvgmstream.vcxproj @@ -162,6 +162,7 @@ + diff --git a/src/libvgmstream.vcxproj.filters b/src/libvgmstream.vcxproj.filters index 8c3c5dc1..c344eb98 100644 --- a/src/libvgmstream.vcxproj.filters +++ b/src/libvgmstream.vcxproj.filters @@ -1129,6 +1129,9 @@ meta\Source Files + + meta\Source Files + meta\Source Files diff --git a/src/meta/meta.h b/src/meta/meta.h index 4b70c35f..8088dc7c 100644 --- a/src/meta/meta.h +++ b/src/meta/meta.h @@ -704,4 +704,6 @@ VGMSTREAM * init_vgmstream_atsl3(STREAMFILE *streamFile); VGMSTREAM * init_vgmstream_sps_n1(STREAMFILE *streamFile); VGMSTREAM * init_vgmstream_atx(STREAMFILE *streamFile); + +VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile); #endif /*_META_H*/ diff --git a/src/meta/sqex_sead.c b/src/meta/sqex_sead.c new file mode 100644 index 00000000..4e324953 --- /dev/null +++ b/src/meta/sqex_sead.c @@ -0,0 +1,271 @@ +#include "meta.h" +#include "../coding/coding.h" + + +static STREAMFILE* setup_sead_hca_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size); + +/* 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, mtrl_offset, meta_offset, post_meta_offset; //, info_offset, name_offset = 0; + size_t stream_size, subheader_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; + + 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")) + goto fail; + + + /** main header **/ + if (read_32bitBE(0x00,streamFile) == 0x73616266) { /* "sabf" */ + is_sab = 1; + } else if (read_32bitBE(0x00,streamFile) == 0x6D616266) { /* "mabf" */ + is_mab = 1; + } else { + 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): chunk size? (usually 0x10, rarely 0x20) */ + if (read_16bitBE(0x06,streamFile) < 0x100) { /* use size as no apparent flag */ + read_32bit = read_32bitBE; + read_16bit = read_16bitBE; + } else { + read_32bit = read_32bitLE; + read_16bit = read_16bitLE; + } + /* 0x08(1): ?, 0x09(1): ?, 0x0a(2): ? */ + if (read_32bit(0x0c,streamFile) != get_streamfile_size(streamFile)) + goto fail; + /* 0x10(10): file descriptor ("BGM", "Music", "SE", etc) */ + + + /** offset tables **/ + if (is_sab) { + if (read_32bitBE(0x20,streamFile) != 0x736E6420) goto fail; /* "snd " (info) */ + if (read_32bitBE(0x30,streamFile) != 0x73657120) goto fail; /* "seq " (unknown) */ + if (read_32bitBE(0x40,streamFile) != 0x74726B20) goto fail; /* "trk " (unknown) */ + if (read_32bitBE(0x50,streamFile) != 0x6D74726C) goto fail; /* "mtrl" (headers/streams) */ + //info_offset = read_32bit(0x28,streamFile); + //seq_offset = read_32bit(0x38,streamFile); + //trk_offset = read_32bit(0x48,streamFile); + mtrl_offset = read_32bit(0x58,streamFile); + } + else if (is_mab) { + if (read_32bitBE(0x20,streamFile) != 0x6D757363) goto fail; /* "musc" (info) */ + if (read_32bitBE(0x30,streamFile) != 0x696E7374) goto fail; /* "inst" (unknown) */ + if (read_32bitBE(0x40,streamFile) != 0x6D74726C) goto fail; /* "mtrl" (headers/streams) */ + //info_offset = read_32bit(0x28,streamFile); + //inst_offset = read_32bit(0x38,streamFile); + mtrl_offset = read_32bit(0x48,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); + subheader_size = read_32bit(meta_offset+0x14,streamFile); /* including subfile header */ + stream_size = read_32bit(meta_offset+0x18,streamFile); /* not including subfile header */ + /* 0x1c: null? */ + + loop_flag = (loop_end > 0); + post_meta_offset = meta_offset + 0x20; + + + /** info section (get stream name) **/ + //if (is_sab) { //todo load name based on entry id + /* "snd ": unknown flags/sizes and name */ + /* 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": unknown flags sizes and names, another format */ + //} + + + /* build the VGMSTREAM */ + vgmstream = allocate_vgmstream(channel_count,loop_flag); + if (!vgmstream) goto fail; + + vgmstream->sample_rate = sample_rate; + vgmstream->num_streams = total_subsongs; + vgmstream->meta_type = is_sab ? meta_SQEX_SAB : meta_SQEX_MAB; + + switch(codec) { + + case 0x02: { /* MSADPCM [Dragon Quest Builders (Vita) sfx] */ + start_offset = post_meta_offset + subheader_size; + + /* 0x00 (2): null?, 0x02(2): entry size? */ + vgmstream->coding_type = coding_MSADPCM; + vgmstream->layout_type = layout_none; + vgmstream->interleave_block_size = read_16bit(post_meta_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(post_meta_offset+0x08, streamFile); //loop_start + vgmstream->loop_end_sample = read_32bit(post_meta_offset+0x0c, streamFile); //loop_end + break; + } + +#ifdef VGM_USE_ATRAC9 + case 0x04: { /* ATRAC9 [Dragon Quest Builders (Vita), Final Fantaxy XV (PS4)] */ + atrac9_config cfg = {0}; + + start_offset = post_meta_offset + subheader_size; + /* post header has various typical ATRAC9 values */ + cfg.channels = vgmstream->channels; + cfg.config_data = read_32bit(post_meta_offset+0x0c,streamFile); + cfg.encoder_delay = read_32bit(post_meta_offset+0x18,streamFile); +VGM_LOG("1\n"); + vgmstream->codec_data = init_atrac9(&cfg); + if (!vgmstream->codec_data) goto fail; + vgmstream->coding_type = coding_ATRAC9; + vgmstream->layout_type = layout_none; +VGM_LOG("2\n"); + vgmstream->sample_rate = read_32bit(post_meta_offset+0x1c,streamFile); /* SAB's sample rate can be different but it's ignored */ + vgmstream->num_samples = read_32bit(post_meta_offset+0x10,streamFile); /* loop values above are also weird and ignored */ + vgmstream->loop_start_sample = read_32bit(post_meta_offset+0x20, streamFile) - (loop_flag ? cfg.encoder_delay : 0); //loop_start + vgmstream->loop_end_sample = read_32bit(post_meta_offset+0x24, streamFile) - (loop_flag ? cfg.encoder_delay : 0); //loop_end + break; + } +#endif + +#ifdef VGM_USE_MPEG + case 0x06: { /* MSF subfile (MPEG mode) [Dragon Quest Builders (PS3)] */ + mpeg_codec_data *mpeg_data = NULL; + mpeg_custom_config cfg = {0}; + + start_offset = post_meta_offset + subheader_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); + if (!mpeg_data) goto fail; + 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; + break; + } +#endif + + case 0x07: { /* HCA subfile [Dissidia Opera Omnia (Mobile), Final Fantaxy XV (PS4)] */ + //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 = post_meta_offset + 0x10; + size_t subfile_size = stream_size + subheader_size - 0x10; + /* post header has 0x10 unknown + HCA header */ + + + temp_streamFile = setup_sead_hca_streamfile(streamFile, subfile_offset, subfile_size); + if (!temp_streamFile) goto fail; + + temp_vgmstream = init_vgmstream_hca(temp_streamFile); + if (temp_vgmstream) { + /* loops can be slightly different (~1000 samples) but probably HCA's are more accurate */ + temp_vgmstream->num_streams = vgmstream->num_streams; + temp_vgmstream->meta_type = vgmstream->meta_type; + + close_streamfile(temp_streamFile); + close_vgmstream(vgmstream); + return temp_vgmstream; + } + else { + close_streamfile(temp_streamFile); + goto fail; + } + } + + case 0x00: /* dummy entry */ + default: + VGM_LOG("SQEX SEAD: unknown codec %x\n", codec); + goto fail; + } + + /* open the file for reading */ + if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) ) + goto fail; + return vgmstream; + +fail: + close_vgmstream(vgmstream); + return NULL; +} + +static STREAMFILE* setup_sead_hca_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size) { + 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; + + 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; +} diff --git a/src/vgmstream.c b/src/vgmstream.c index c5a2bf59..f7969294 100644 --- a/src/vgmstream.c +++ b/src/vgmstream.c @@ -379,6 +379,7 @@ VGMSTREAM * (*init_vgmstream_functions[])(STREAMFILE *streamFile) = { init_vgmstream_atsl3, init_vgmstream_sps_n1, init_vgmstream_atx, + init_vgmstream_sqex_sead, init_vgmstream_txth, /* should go at the end (lower priority) */ #ifdef VGM_USE_FFMPEG diff --git a/src/vgmstream.h b/src/vgmstream.h index 9b611f88..5a441927 100644 --- a/src/vgmstream.h +++ b/src/vgmstream.h @@ -657,6 +657,8 @@ typedef enum { meta_OGG_ISD, /* Ogg Vorbis with encryption (Azure Striker Gunvolt PC) */ meta_KMA9, /* Koei Tecmo [Nobunaga no Yabou - Souzou (Vita)] */ meta_XWC, /* Starbreeze games */ + meta_SQEX_SAB, /* Square-Enix newest middleware (sound) */ + meta_SQEX_MAB, /* Square-Enix newest middleware (music) */ #ifdef VGM_USE_MP4V2 meta_MP4, /* AAC (iOS) */