From 770343b25579a38a2bae4790d08d2b2c6161ddf9 Mon Sep 17 00:00:00 2001 From: bnnm Date: Sat, 15 Dec 2018 12:37:01 +0100 Subject: [PATCH] Add .nus3audio [Super Smash Bros. Ultimate (Switch)] --- src/formats.c | 1 + src/libvgmstream.vcproj | 4 ++ src/libvgmstream.vcxproj | 1 + src/libvgmstream.vcxproj.filters | 3 + src/meta/meta.h | 2 + src/meta/nus3audio.c | 119 +++++++++++++++++++++++++++++++ src/vgmstream.c | 1 + 7 files changed, 131 insertions(+) create mode 100644 src/meta/nus3audio.c diff --git a/src/formats.c b/src/formats.c index 9688ab3b..9db2151d 100644 --- a/src/formats.c +++ b/src/formats.c @@ -263,6 +263,7 @@ static const char* extension_list[] = { "nop", "nps", "npsf", //fake extension/header id for .nps (in bigfiles) + "nus3audio", "nus3bank", "nwa", "nwav", diff --git a/src/libvgmstream.vcproj b/src/libvgmstream.vcproj index 85614108..e43284f2 100644 --- a/src/libvgmstream.vcproj +++ b/src/libvgmstream.vcproj @@ -846,6 +846,10 @@ RelativePath=".\meta\nub_xma.c" > + + diff --git a/src/libvgmstream.vcxproj b/src/libvgmstream.vcxproj index f3f99bbe..ca301847 100644 --- a/src/libvgmstream.vcxproj +++ b/src/libvgmstream.vcxproj @@ -319,6 +319,7 @@ + diff --git a/src/libvgmstream.vcxproj.filters b/src/libvgmstream.vcxproj.filters index f8ab0189..f36e72ff 100644 --- a/src/libvgmstream.vcxproj.filters +++ b/src/libvgmstream.vcxproj.filters @@ -520,6 +520,9 @@ meta\Source Files + + meta\Source Files + meta\Source Files diff --git a/src/meta/meta.h b/src/meta/meta.h index 287af92a..a068fc96 100644 --- a/src/meta/meta.h +++ b/src/meta/meta.h @@ -819,4 +819,6 @@ VGMSTREAM * init_vgmstream_zsnd(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_ogg_opus(STREAMFILE * streamFile); +VGMSTREAM * init_vgmstream_nus3audio(STREAMFILE * streamFile); + #endif /*_META_H*/ diff --git a/src/meta/nus3audio.c b/src/meta/nus3audio.c new file mode 100644 index 00000000..79826bc3 --- /dev/null +++ b/src/meta/nus3audio.c @@ -0,0 +1,119 @@ +#include "meta.h" +#include "../coding/coding.h" + +typedef enum { IDSP, OPUS, } nus3audio_codec; + +/* .nus3audio - Namco's newest newest audio container [Super Smash Bros. Ultimate (Switch)] */ +VGMSTREAM * init_vgmstream_nus3audio(STREAMFILE *streamFile) { + VGMSTREAM *vgmstream = NULL; + STREAMFILE *temp_streamFile = NULL; + off_t subfile_offset = 0, name_offset = 0; + size_t subfile_size = 0; + nus3audio_codec codec; + const char* fake_ext = NULL; + int total_subsongs, target_subsong = streamFile->stream_index; + + + /* checks */ + if (!check_extensions(streamFile, "nus3audio")) + goto fail; + if (read_32bitBE(0x00,streamFile) != 0x4E555333) /* "NUS3" */ + goto fail; + if (read_32bitLE(0x04,streamFile) + 0x08 != get_streamfile_size(streamFile)) + goto fail; + if (read_32bitBE(0x08,streamFile) != 0x41554449) /* "AUDI" */ + goto fail; + + + /* parse existing chunks */ + { + off_t offset = 0x0c; + size_t file_size = get_streamfile_size(streamFile); + uint32_t codec_id = 0; + + total_subsongs = 0; + + while (offset < file_size) { + uint32_t chunk_id = (uint32_t)read_32bitBE(offset+0x00, streamFile); + size_t chunk_size = (size_t)read_32bitLE(offset+0x04, streamFile); + + switch(chunk_id) { + case 0x494E4458: /* "INDX": audio index */ + total_subsongs = read_32bitLE(offset+0x08 + 0x00,streamFile); + if (target_subsong == 0) target_subsong = 1; + if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail; + break; + + case 0x4E4D4F46: /* "NMOF": name offsets (absolute, inside TNNM) */ + name_offset = read_32bitLE(offset+0x08 + 0x04*(target_subsong-1),streamFile); + break; + + case 0x41444F46: /* "ADOF": audio offsets (absolute, inside PACK) */ + subfile_offset = read_32bitLE(offset+0x08 + 0x08*(target_subsong-1) + 0x00,streamFile); + subfile_size = read_32bitLE(offset+0x08 + 0x08*(target_subsong-1) + 0x04,streamFile); + break; + + case 0x544E4944: /* "TNID": tone ids? */ + case 0x544E4E4D: /* "TNNM": tone names */ + case 0x4A554E4B: /* "JUNK": padding */ + case 0x5041434B: /* "PACK": main data */ + default: + break; + } + + offset += 0x08 + chunk_size; + } + + if (total_subsongs == 0 || subfile_offset == 0 || subfile_size == 0) { + VGM_LOG("NUS3AUDIO: subfile not found\n"); + goto fail; + } + + codec_id = read_32bitBE(subfile_offset, streamFile); + switch(codec_id) { + case 0x49445350: /* "IDSP" */ + codec = IDSP; + fake_ext = "idsp"; + break; + case 0x4F505553: /* "OPUS" */ + codec = OPUS; + fake_ext = "opus"; + break; + default: + VGM_LOG("NUS3AUDIO: unknown codec %x\n", codec_id); + goto fail; + } + } + + + temp_streamFile = setup_subfile_streamfile(streamFile, subfile_offset,subfile_size, fake_ext); + if (!temp_streamFile) goto fail; + + /* init the VGMSTREAM */ + switch(codec) { + case IDSP: + vgmstream = init_vgmstream_idsp_nus3(temp_streamFile); + if (!vgmstream) goto fail; + break; + case OPUS: + vgmstream = init_vgmstream_opus_nus3(temp_streamFile); + if (!vgmstream) goto fail; + break; + default: + goto fail; + } + + vgmstream->num_streams = total_subsongs; + if (name_offset) /* null-terminated */ + read_string(vgmstream->stream_name,STREAM_NAME_SIZE, name_offset,streamFile); + + close_streamfile(temp_streamFile); + return vgmstream; + +fail: + close_streamfile(temp_streamFile); + close_vgmstream(vgmstream); + return NULL; +} + + diff --git a/src/vgmstream.c b/src/vgmstream.c index 1023e894..47aba049 100644 --- a/src/vgmstream.c +++ b/src/vgmstream.c @@ -456,6 +456,7 @@ VGMSTREAM * (*init_vgmstream_functions[])(STREAMFILE *streamFile) = { init_vgmstream_opus_opusx, init_vgmstream_dsp_adpcmx, init_vgmstream_ogg_opus, + init_vgmstream_nus3audio, /* lowest priority metas (should go after all metas, and TXTH should go before raw formats) */ init_vgmstream_txth, /* proper parsers should supersede TXTH, once added */