From 9dd4f6804977f81a2b1af8cfcc97af4d1f228117 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Sun, 1 Mar 2020 18:36:55 -0800 Subject: [PATCH] Updated VGMStream to r1050-2834-gc40d364e --- .../vgmstream/vgmstream/src/coding/coding.h | 4 +- .../vgmstream/src/coding/psx_decoder.c | 869 +++++----- .../vgmstream/src/layout/blocked_awc.c | 135 +- Frameworks/vgmstream/vgmstream/src/meta/awb.c | 15 + Frameworks/vgmstream/vgmstream/src/meta/awc.c | 698 ++++---- .../vgmstream/vgmstream/src/meta/meta.h | 1 + Frameworks/vgmstream/vgmstream/src/meta/nub.c | 40 +- .../vgmstream/vgmstream/src/meta/riff.c | 32 +- .../vgmstream/vgmstream/src/meta/sgxd.c | 3 +- .../vgmstream/vgmstream/src/meta/ta_aac.c | 746 ++++----- .../vgmstream/vgmstream/src/meta/ubi_hx.c | 2 +- .../vgmstream/vgmstream/src/meta/ubi_sb.c | 12 + .../vgmstream/vgmstream/src/meta/wwise.c | 1484 +++++++++-------- .../vgmstream/vgmstream/src/meta/xvag.c | 650 ++++---- .../vgmstream/vgmstream/src/vgmstream.c | 6 +- 15 files changed, 2418 insertions(+), 2279 deletions(-) diff --git a/Frameworks/vgmstream/vgmstream/src/coding/coding.h b/Frameworks/vgmstream/vgmstream/src/coding/coding.h index db56e1dcf..5a4c00fc5 100644 --- a/Frameworks/vgmstream/vgmstream/src/coding/coding.h +++ b/Frameworks/vgmstream/vgmstream/src/coding/coding.h @@ -79,8 +79,8 @@ void decode_pcmfloat(VGMSTREAMCHANNEL * stream, sample_t * outbuf, int channelsp size_t pcm_bytes_to_samples(size_t bytes, int channels, int bits_per_sample); /* psx_decoder */ -void decode_psx(VGMSTREAMCHANNEL * stream, sample_t * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int is_badflags); -void decode_psx_configurable(VGMSTREAMCHANNEL * stream, sample_t * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int frame_size); +void decode_psx(VGMSTREAMCHANNEL* stream, sample_t* outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int is_badflags, int config); +void decode_psx_configurable(VGMSTREAMCHANNEL* stream, sample_t* outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int frame_size, int config); void decode_psx_pivotal(VGMSTREAMCHANNEL * stream, sample_t * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int frame_size); int ps_find_loop_offsets(STREAMFILE *streamFile, off_t start_offset, size_t data_size, int channels, size_t interleave, int32_t * out_loop_start, int32_t * out_loop_end); int ps_find_loop_offsets_full(STREAMFILE *streamFile, off_t start_offset, size_t data_size, int channels, size_t interleave, int32_t * out_loop_start, int32_t * out_loop_end); diff --git a/Frameworks/vgmstream/vgmstream/src/coding/psx_decoder.c b/Frameworks/vgmstream/vgmstream/src/coding/psx_decoder.c index 3b5b2b7ec..d70f06127 100644 --- a/Frameworks/vgmstream/vgmstream/src/coding/psx_decoder.c +++ b/Frameworks/vgmstream/vgmstream/src/coding/psx_decoder.c @@ -1,423 +1,446 @@ -#include "coding.h" - - -/* PS-ADPCM table, defined as rational numbers (as in the spec) */ -static const float ps_adpcm_coefs_f[5][2] = { - { 0.0 , 0.0 }, //{ 0.0 , 0.0 }, - { 0.9375 , 0.0 }, //{ 60.0 / 64.0 , 0.0 }, - { 1.796875 , -0.8125 }, //{ 115.0 / 64.0 , -52.0 / 64.0 }, - { 1.53125 , -0.859375 }, //{ 98.0 / 64.0 , -55.0 / 64.0 }, - { 1.90625 , -0.9375 }, //{ 122.0 / 64.0 , -60.0 / 64.0 }, -}; - -/* PS-ADPCM table, defined as spec_coef*64 (for int implementations) */ -static const int ps_adpcm_coefs_i[5][2] = { - { 0 , 0 }, - { 60 , 0 }, - { 115 , -52 }, - { 98 , -55 }, - { 122 , -60 }, -#if 0 - /* extended table from PPSSPP (PSP emu), found by tests (unused?) */ - { 0 , 0 }, - { 0 , 0 }, - { 52 , 0 }, - { 55 , -2 }, - { 60 ,-125 }, - { 0 , 0 }, - { 0 , -91 }, - { 0 , 0 }, - { 2 ,-216 }, - { 125 , -6 }, - { 0 ,-151 }, -#endif -}; - - -/* Decodes Sony's PS-ADPCM (sometimes called SPU-ADPCM or VAG, just "ADPCM" in the SDK docs). - * Very similar to XA ADPCM (see xa_decoder for extended info). - * - * Some official PC tools decode using float coefs (from the spec), as does this code, but - * consoles/games/libs would vary (PS1 could do it in hardware using BRR/XA's logic, FMOD/PS3 - * may use int math in software, etc). There are inaudible rounding diffs between implementations. - */ - -/* standard PS-ADPCM (float math version) */ -void decode_psx(VGMSTREAMCHANNEL * stream, sample_t * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int is_badflags) { - uint8_t frame[0x10] = {0}; - off_t frame_offset; - int i, frames_in, sample_count = 0; - size_t bytes_per_frame, samples_per_frame; - uint8_t coef_index, shift_factor, flag; - int32_t hist1 = stream->adpcm_history1_32; - int32_t hist2 = stream->adpcm_history2_32; - - - /* external interleave (fixed size), mono */ - bytes_per_frame = 0x10; - samples_per_frame = (bytes_per_frame - 0x02) * 2; /* always 28 */ - frames_in = first_sample / samples_per_frame; - first_sample = first_sample % samples_per_frame; - - /* parse frame header */ - frame_offset = stream->offset + bytes_per_frame * frames_in; - read_streamfile(frame, frame_offset, bytes_per_frame, stream->streamfile); /* ignore EOF errors */ - coef_index = (frame[0] >> 4) & 0xf; - shift_factor = (frame[0] >> 0) & 0xf; - flag = frame[1]; /* only lower nibble needed */ - - VGM_ASSERT_ONCE(coef_index > 5 || shift_factor > 12, "PS-ADPCM: incorrect coefs/shift at %x\n", (uint32_t)frame_offset); - if (coef_index > 5) /* needed by inFamous (PS3) (maybe it's supposed to use more filters?) */ - coef_index = 0; /* upper filters aren't used in PS1/PS2, maybe in PSP/PS3? */ - if (shift_factor > 12) - shift_factor = 9; /* supposedly, from Nocash PSX docs */ - - if (is_badflags) /* some games store garbage or extra internal logic in the flags, must be ignored */ - flag = 0; - VGM_ASSERT_ONCE(flag > 7,"PS-ADPCM: unknown flag at %x\n", (uint32_t)frame_offset); /* meta should use PSX-badflags */ - - - /* decode nibbles */ - for (i = first_sample; i < first_sample + samples_to_do; i++) { - int32_t sample = 0; - - if (flag < 0x07) { /* with flag 0x07 decoded sample must be 0 */ - uint8_t nibbles = frame[0x02 + i/2]; - - sample = i&1 ? /* low nibble first */ - (nibbles >> 4) & 0x0f : - (nibbles >> 0) & 0x0f; - sample = (int16_t)((sample << 12) & 0xf000) >> shift_factor; /* 16b sign extend + scale */ - sample = (int32_t)(sample + ps_adpcm_coefs_f[coef_index][0]*hist1 + ps_adpcm_coefs_f[coef_index][1]*hist2); - sample = clamp16(sample); - } - - outbuf[sample_count] = sample; - sample_count += channelspacing; - - hist2 = hist1; - hist1 = sample; - } - - stream->adpcm_history1_32 = hist1; - stream->adpcm_history2_32 = hist2; -} - - -/* PS-ADPCM with configurable frame size and no flag (int math version). - * Found in some PC/PS3 games (FF XI in sizes 0x3/0x5/0x9/0x41, Afrika in size 0x4, Blur/James Bond in size 0x33, etc). - * - * Uses int math to decode, which seems more likely (based on FF XI PC's code in Moogle Toolbox). */ -void decode_psx_configurable(VGMSTREAMCHANNEL * stream, sample_t * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int frame_size) { - uint8_t frame[0x50] = {0}; - off_t frame_offset; - int i, frames_in, sample_count = 0; - size_t bytes_per_frame, samples_per_frame; - uint8_t coef_index, shift_factor; - int32_t hist1 = stream->adpcm_history1_32; - int32_t hist2 = stream->adpcm_history2_32; - - - /* external interleave (variable size), mono */ - bytes_per_frame = frame_size; - samples_per_frame = (bytes_per_frame - 0x01) * 2; - frames_in = first_sample / samples_per_frame; - first_sample = first_sample % samples_per_frame; - - /* parse frame header */ - frame_offset = stream->offset + bytes_per_frame * frames_in; - read_streamfile(frame, frame_offset, bytes_per_frame, stream->streamfile); /* ignore EOF errors */ - coef_index = (frame[0] >> 4) & 0xf; - shift_factor = (frame[0] >> 0) & 0xf; - - VGM_ASSERT_ONCE(coef_index > 5 || shift_factor > 12, "PS-ADPCM: incorrect coefs/shift at %x\n", (uint32_t)frame_offset); - if (coef_index > 5) /* needed by Afrika (PS3) (maybe it's supposed to use more filters?) */ - coef_index = 0; /* upper filters aren't used in PS1/PS2, maybe in PSP/PS3? */ - if (shift_factor > 12) - shift_factor = 9; /* supposedly, from Nocash PSX docs */ - - - /* decode nibbles */ - for (i = first_sample; i < first_sample + samples_to_do; i++) { - int32_t sample = 0; - uint8_t nibbles = frame[0x01 + i/2]; - - sample = i&1 ? /* low nibble first */ - (nibbles >> 4) & 0x0f : - (nibbles >> 0) & 0x0f; - sample = (int16_t)((sample << 12) & 0xf000) >> shift_factor; /* 16b sign extend + scale */ - sample = sample + ((ps_adpcm_coefs_i[coef_index][0]*hist1 + ps_adpcm_coefs_i[coef_index][1]*hist2) >> 6); - sample = clamp16(sample); - - outbuf[sample_count] = sample; - sample_count += channelspacing; - - hist2 = hist1; - hist1 = sample; - } - - stream->adpcm_history1_32 = hist1; - stream->adpcm_history2_32 = hist2; -} - -/* PS-ADPCM from Pivotal games, exactly like psx_cfg but with float math (reverse engineered from the exe) */ -void decode_psx_pivotal(VGMSTREAMCHANNEL * stream, sample_t * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int frame_size) { - uint8_t frame[0x50] = {0}; - off_t frame_offset; - int i, frames_in, sample_count = 0; - size_t bytes_per_frame, samples_per_frame; - uint8_t coef_index, shift_factor; - int32_t hist1 = stream->adpcm_history1_32; - int32_t hist2 = stream->adpcm_history2_32; - float scale; - - - /* external interleave (variable size), mono */ - bytes_per_frame = frame_size; - samples_per_frame = (bytes_per_frame - 0x01) * 2; - frames_in = first_sample / samples_per_frame; - first_sample = first_sample % samples_per_frame; - - /* parse frame header */ - frame_offset = stream->offset + bytes_per_frame * frames_in; - read_streamfile(frame, frame_offset, bytes_per_frame, stream->streamfile); /* ignore EOF errors */ - coef_index = (frame[0] >> 4) & 0xf; - shift_factor = (frame[0] >> 0) & 0xf; - - VGM_ASSERT_ONCE(coef_index > 5 || shift_factor > 12, "PS-ADPCM-piv: incorrect coefs/shift\n"); - if (coef_index > 5) /* just in case */ - coef_index = 5; - if (shift_factor > 12) /* same */ - shift_factor = 12; - - scale = (float)(1.0 / (double)(1 << shift_factor)); - - - /* decode nibbles */ - for (i = first_sample; i < first_sample + samples_to_do; i++) { - int32_t sample = 0; - uint8_t nibbles = frame[0x01 + i/2]; - - sample = !(i&1) ? /* low nibble first */ - (nibbles >> 0) & 0x0f : - (nibbles >> 4) & 0x0f; - sample = (int16_t)((sample << 12) & 0xf000); /* 16b sign extend + default scale */ - sample = sample*scale + ps_adpcm_coefs_f[coef_index][0]*hist1 + ps_adpcm_coefs_f[coef_index][1]*hist2; /* actually substracts negative coefs but whatevs */ - - outbuf[sample_count] = clamp16(sample); - sample_count += channelspacing; - - hist2 = hist1; - hist1 = sample; /* not clamped but no difference */ - } - - stream->adpcm_history1_32 = hist1; - stream->adpcm_history2_32 = hist2; -} - - -/* Find loop samples in PS-ADPCM data and return if the file loops. - * - * PS-ADPCM/VAG has optional bit flags that control looping in the SPU. - * Possible combinations (as usually defined in Sony's docs): - * - 0x0 (0000): Normal decode - * - 0x1 (0001): End marker (last frame) - * - 0x2 (0010): Loop region (marks files that *may* have loop flags somewhere) - * - 0x3 (0011): Loop end (jump to loop address) - * - 0x4 (0100): Start marker - * - 0x5 (0101): Same as 0x07? Extremely rare [Blood Omen: Legacy of Kain (PS1)] - * - 0x6 (0110): Loop start (save loop address) - * - 0x7 (0111): End marker and don't decode - * - 0x8+(1NNN): Not valid - */ -static int ps_find_loop_offsets_internal(STREAMFILE *sf, off_t start_offset, size_t data_size, int channels, size_t interleave, int32_t * p_loop_start, int32_t * p_loop_end, int config) { - int num_samples = 0, loop_start = 0, loop_end = 0; - int loop_start_found = 0, loop_end_found = 0; - off_t offset = start_offset; - off_t max_offset = start_offset + data_size; - size_t interleave_consumed = 0; - int detect_full_loops = config & 1; - - - if (data_size == 0 || channels == 0 || (channels > 1 && interleave == 0)) - return 0; - - while (offset < max_offset) { - uint8_t flag = read_u8(offset+0x01, sf) & 0x0F; /* lower nibble only (for HEVAG) */ - - /* theoretically possible and would use last 0x06 */ - VGM_ASSERT_ONCE(loop_start_found && flag == 0x06, "PS LOOPS: multiple loop start found at %x\n", (uint32_t)offset); - - if (flag == 0x06 && !loop_start_found) { - loop_start = num_samples; /* loop start before this frame */ - loop_start_found = 1; - } - - if (flag == 0x03 && !loop_end) { - loop_end = num_samples + 28; /* loop end after this frame */ - loop_end_found = 1; - - /* ignore strange case in Commandos (PS2), has many loop starts and ends */ - if (channels == 1 - && offset + 0x10 < max_offset - && (read_u8(offset + 0x11, sf) & 0x0F) == 0x06) { - loop_end = 0; - loop_end_found = 0; - } - - if (loop_start_found && loop_end_found) - break; - } - - /* hack for some games that don't have loop points but do full loops, - * if there is a "partial" 0x07 end flag pretend it wants to loop - * (sometimes this will loop non-looping tracks, and won't loop all repeating files) - * seems only used in Ratchet & Clank series and Ecco the Dolphin */ - if (flag == 0x01 && detect_full_loops) { - static const uint8_t eof[0x10] = {0xFF,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; - uint8_t buf[0x10]; - uint8_t hdr = read_u8(offset + 0x00, sf); - - int read = read_streamfile(buf, offset+0x10, sizeof(buf), sf); - if (read > 0 - && buf[0] != 0x00 /* ignore blank frame */ - && buf[0] != 0x0c /* ignore silent frame */ - && buf[0] != 0x3c /* ignore some L-R tracks with different end flags */ - ) { - - /* assume full loop with repeated frame header and null frame */ - if (hdr == buf[0] && memcmp(buf+1, eof+1, sizeof(buf) - 1) == 0) { - loop_start = 28; /* skip first frame as it's null in PS-ADPCM */ - loop_end = num_samples + 28; /* loop end after this frame */ - loop_start_found = 1; - loop_end_found = 1; - //;VGM_LOG("PS LOOPS: full loop found\n"); - break; - } - } - } - - - num_samples += 28; - offset += 0x10; - - /* skip other channels */ - interleave_consumed += 0x10; - if (interleave_consumed == interleave) { - interleave_consumed = 0; - offset += interleave*(channels - 1); - } - } - - VGM_ASSERT(loop_start_found && !loop_end_found, "PS LOOPS: found loop start but not loop end\n"); - VGM_ASSERT(loop_end_found && !loop_start_found, "PS LOOPS: found loop end but not loop start\n"); - //;VGM_LOG("PS LOOPS: start=%i, end=%i\n", loop_start, loop_end); - - /* From Sony's docs: if only loop_end is set loop back to "phoneme region start", but in practice doesn't */ - if (loop_start_found && loop_end_found) { - *p_loop_start = loop_start; - *p_loop_end = loop_end; - return 1; - } - - return 0; /* no loop */ -} - -int ps_find_loop_offsets(STREAMFILE *sf, off_t start_offset, size_t data_size, int channels, size_t interleave, int32_t *p_loop_start, int32_t *p_loop_end) { - return ps_find_loop_offsets_internal(sf, start_offset, data_size, channels, interleave, p_loop_start, p_loop_end, 0); -} - -int ps_find_loop_offsets_full(STREAMFILE *sf, off_t start_offset, size_t data_size, int channels, size_t interleave, int32_t *p_loop_start, int32_t *p_loop_end) { - return ps_find_loop_offsets_internal(sf, start_offset, data_size, channels, interleave, p_loop_start, p_loop_end, 1); -} - -size_t ps_find_padding(STREAMFILE *streamFile, off_t start_offset, size_t data_size, int channels, size_t interleave, int discard_empty) { - off_t min_offset, offset; - size_t frame_size = 0x10; - size_t padding_size = 0; - size_t interleave_consumed = 0; - - - if (data_size == 0 || channels == 0 || (channels > 0 && interleave == 0)) - return 0; - - offset = start_offset + data_size; - - /* in rare cases (ex. Gitaroo Man) channels have inconsistent empty padding, use first as guide */ - offset = offset - interleave * (channels - 1); - - /* some files have padding spanning multiple interleave blocks */ - min_offset = start_offset; //offset - interleave; - - while (offset > min_offset) { - uint32_t f1,f2,f3,f4; - uint8_t flag; - int is_empty = 0; - - offset -= frame_size; - - f1 = read_32bitBE(offset+0x00,streamFile); - f2 = read_32bitBE(offset+0x04,streamFile); - f3 = read_32bitBE(offset+0x08,streamFile); - f4 = read_32bitBE(offset+0x0c,streamFile); - flag = (f1 >> 16) & 0xFF; - - if (f1 == 0 && f2 == 0 && f3 == 0 && f4 == 0) - is_empty = 1; - - if (!is_empty && discard_empty) { - if (flag == 0x07 || flag == 0x77) - is_empty = 1; /* 'discard frame' flag */ - else if ((f1 & 0xFF00FFFF) == 0 && f2 == 0 && f3 == 0 && f4 == 0) - is_empty = 1; /* silent with flags (typical for looping files) */ - else if ((f1 & 0xFF00FFFF) == 0x0C000000 && f2 == 0 && f3 == 0 && f4 == 0) - is_empty = 1; /* silent (maybe shouldn't ignore flag 0x03?) */ - else if ((f1 & 0x0000FFFF) == 0x00007777 && f2 == 0x77777777 && f3 ==0x77777777 && f4 == 0x77777777) - is_empty = 1; /* silent-ish */ - } - - if (!is_empty) - break; - - padding_size += frame_size * channels; - - /* skip other channels */ - interleave_consumed += 0x10; - if (interleave_consumed == interleave) { - interleave_consumed = 0; - offset -= interleave*(channels - 1); - } - } - - return padding_size; -} - - -size_t ps_bytes_to_samples(size_t bytes, int channels) { - if (channels <= 0) return 0; - return bytes / channels / 0x10 * 28; -} - -size_t ps_cfg_bytes_to_samples(size_t bytes, size_t frame_size, int channels) { - int samples_per_frame = (frame_size - 0x01) * 2; - return bytes / channels / frame_size * samples_per_frame; -} - -/* test PS-ADPCM frames for correctness */ -int ps_check_format(STREAMFILE *streamFile, off_t offset, size_t max) { - off_t max_offset = offset + max; - if (max_offset > get_streamfile_size(streamFile)) - max_offset = get_streamfile_size(streamFile); - - while (offset < max_offset) { - uint8_t predictor = (read_8bit(offset+0x00,streamFile) >> 4) & 0x0f; - uint8_t flags = read_8bit(offset+0x01,streamFile); - - if (predictor > 5 || flags > 7) { - return 0; - } - offset += 0x10; - } - - return 1; -} +#include "coding.h" + + +/* PS-ADPCM table, defined as rational numbers (as in the spec) */ +static const float ps_adpcm_coefs_f[16][2] = { + { 0.0 , 0.0 }, //{ 0.0 , 0.0 }, + { 0.9375 , 0.0 }, //{ 60.0 / 64.0 , 0.0 }, + { 1.796875 , -0.8125 }, //{ 115.0 / 64.0 , -52.0 / 64.0 }, + { 1.53125 , -0.859375 }, //{ 98.0 / 64.0 , -55.0 / 64.0 }, + { 1.90625 , -0.9375 }, //{ 122.0 / 64.0 , -60.0 / 64.0 }, + /* extended table used in few PS3 games, found in ELFs */ + { 0.46875 , -0.0 }, //{ 30.0 / 64.0 , -0.0 / 64.0 }, + { 0.8984375 , -0.40625 }, //{ 57.5 / 64.0 , -26.0 / 64.0 }, + { 0.765625 , -0.4296875 }, //{ 49.0 / 64.0 , -27.5 / 64.0 }, + { 0.953125 , -0.46875 }, //{ 61.0 / 64.0 , -30.0 / 64.0 }, + { 0.234375 , -0.0 }, //{ 15.0 / 64.0 , -0.0 / 64.0 }, + { 0.44921875, -0.203125 }, //{ 28.75/ 64.0 , -13.0 / 64.0 }, + { 0.3828125 , -0.21484375}, //{ 24.5 / 64.0 , -13.75/ 64.0 }, + { 0.4765625 , -0.234375 }, //{ 30.5 / 64.0 , -15.0 / 64.0 }, + { 0.5 , -0.9375 }, //{ 32.0 / 64.0 , -60.0 / 64.0 }, + { 0.234375 , -0.9375 }, //{ 15.0 / 64.0 , -60.0 / 64.0 }, + { 0.109375 , -0.9375 }, //{ 7.0 / 64.0 , -60.0 / 64.0 }, +}; + +/* PS-ADPCM table, defined as spec_coef*64 (for int implementations) */ +static const int ps_adpcm_coefs_i[5][2] = { + { 0 , 0 }, + { 60 , 0 }, + { 115 , -52 }, + { 98 , -55 }, + { 122 , -60 }, +#if 0 + /* extended table from PPSSPP (PSP emu), found by tests (unused?) */ + { 0 , 0 }, + { 0 , 0 }, + { 52 , 0 }, + { 55 , -2 }, + { 60 ,-125 }, + { 0 , 0 }, + { 0 , -91 }, + { 0 , 0 }, + { 2 ,-216 }, + { 125 , -6 }, + { 0 ,-151 }, +#endif +}; + + +/* Decodes Sony's PS-ADPCM (sometimes called SPU-ADPCM or VAG, just "ADPCM" in the SDK docs). + * Very similar to XA ADPCM (see xa_decoder for extended info). + * + * Some official PC tools decode using float coefs (from the spec), as does this code, but + * consoles/games/libs would vary (PS1 could do it in hardware using BRR/XA's logic, FMOD may + * depend on platform, PS3 games use floats, etc). There are rounding diffs between implementations. + */ + +/* standard PS-ADPCM (float math version) */ +void decode_psx(VGMSTREAMCHANNEL* stream, sample_t* outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int is_badflags, int config) { + uint8_t frame[0x10] = {0}; + off_t frame_offset; + int i, frames_in, sample_count = 0; + size_t bytes_per_frame, samples_per_frame; + uint8_t coef_index, shift_factor, flag; + int32_t hist1 = stream->adpcm_history1_32; + int32_t hist2 = stream->adpcm_history2_32; + int extended_mode = (config == 1); + + + /* external interleave (fixed size), mono */ + bytes_per_frame = 0x10; + samples_per_frame = (bytes_per_frame - 0x02) * 2; /* always 28 */ + frames_in = first_sample / samples_per_frame; + first_sample = first_sample % samples_per_frame; + + /* parse frame header */ + frame_offset = stream->offset + bytes_per_frame * frames_in; + read_streamfile(frame, frame_offset, bytes_per_frame, stream->streamfile); /* ignore EOF errors */ + coef_index = (frame[0] >> 4) & 0xf; + shift_factor = (frame[0] >> 0) & 0xf; + flag = frame[1]; /* only lower nibble needed */ + + /* upper filters only used in few PS3 games, normally 0 */ + if (!extended_mode) { + VGM_ASSERT_ONCE(coef_index > 5 || shift_factor > 12, "PS-ADPCM: incorrect coefs/shift at %x\n", (uint32_t)frame_offset); + if (coef_index > 5) + coef_index = 0; + if (shift_factor > 12) + shift_factor = 9; /* supposedly, from Nocash PSX docs */ + } + + if (is_badflags) /* some games store garbage or extra internal logic in the flags, must be ignored */ + flag = 0; + VGM_ASSERT_ONCE(flag > 7,"PS-ADPCM: unknown flag at %x\n", (uint32_t)frame_offset); /* meta should use PSX-badflags */ + + + /* decode nibbles */ + for (i = first_sample; i < first_sample + samples_to_do; i++) { + int32_t sample = 0; + + if (flag < 0x07) { /* with flag 0x07 decoded sample must be 0 */ + uint8_t nibbles = frame[0x02 + i/2]; + + sample = i&1 ? /* low nibble first */ + (nibbles >> 4) & 0x0f : + (nibbles >> 0) & 0x0f; + sample = (int16_t)((sample << 12) & 0xf000) >> shift_factor; /* 16b sign extend + scale */ + sample = (int32_t)(sample + ps_adpcm_coefs_f[coef_index][0]*hist1 + ps_adpcm_coefs_f[coef_index][1]*hist2); + sample = clamp16(sample); + } + + outbuf[sample_count] = sample; + sample_count += channelspacing; + + hist2 = hist1; + hist1 = sample; + } + + stream->adpcm_history1_32 = hist1; + stream->adpcm_history2_32 = hist2; +} + + +/* PS-ADPCM with configurable frame size and no flag (int math version). + * Found in some PC/PS3 games (FF XI in sizes 0x3/0x5/0x9/0x41, Afrika in size 0x4, Blur/James Bond in size 0x33, etc). + * + * Uses int/float math depending on config (PC/other code may be int, PS3 float). */ +void decode_psx_configurable(VGMSTREAMCHANNEL* stream, sample_t* outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int frame_size, int config) { + uint8_t frame[0x50] = {0}; + off_t frame_offset; + int i, frames_in, sample_count = 0; + size_t bytes_per_frame, samples_per_frame; + uint8_t coef_index, shift_factor; + int32_t hist1 = stream->adpcm_history1_32; + int32_t hist2 = stream->adpcm_history2_32; + int extended_mode = (config == 1); + int float_mode = (config == 1); + + + /* external interleave (variable size), mono */ + bytes_per_frame = frame_size; + samples_per_frame = (bytes_per_frame - 0x01) * 2; + frames_in = first_sample / samples_per_frame; + first_sample = first_sample % samples_per_frame; + + /* parse frame header */ + frame_offset = stream->offset + bytes_per_frame * frames_in; + read_streamfile(frame, frame_offset, bytes_per_frame, stream->streamfile); /* ignore EOF errors */ + coef_index = (frame[0] >> 4) & 0xf; + shift_factor = (frame[0] >> 0) & 0xf; + + /* upper filters only used in few PS3 games, normally 0 */ + if (!extended_mode) { + VGM_ASSERT_ONCE(coef_index > 5 || shift_factor > 12, "PS-ADPCM: incorrect coefs/shift at %x\n", (uint32_t)frame_offset); + if (coef_index > 5) + coef_index = 0; + if (shift_factor > 12) + shift_factor = 9; /* supposedly, from Nocash PSX docs */ + } + + + /* decode nibbles */ + for (i = first_sample; i < first_sample + samples_to_do; i++) { + int32_t sample = 0; + uint8_t nibbles = frame[0x01 + i/2]; + + sample = i&1 ? /* low nibble first */ + (nibbles >> 4) & 0x0f : + (nibbles >> 0) & 0x0f; + sample = (int16_t)((sample << 12) & 0xf000) >> shift_factor; /* 16b sign extend + scale */ + sample = float_mode ? + (int32_t)(sample + ps_adpcm_coefs_f[coef_index][0]*hist1 + ps_adpcm_coefs_f[coef_index][1]*hist2) : + sample + ((ps_adpcm_coefs_i[coef_index][0]*hist1 + ps_adpcm_coefs_i[coef_index][1]*hist2) >> 6); + sample = clamp16(sample); + + outbuf[sample_count] = sample; + sample_count += channelspacing; + + hist2 = hist1; + hist1 = sample; + } + + stream->adpcm_history1_32 = hist1; + stream->adpcm_history2_32 = hist2; +} + +/* PS-ADPCM from Pivotal games, exactly like psx_cfg but with float math (reverse engineered from the exe) */ +void decode_psx_pivotal(VGMSTREAMCHANNEL * stream, sample_t * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int frame_size) { + uint8_t frame[0x50] = {0}; + off_t frame_offset; + int i, frames_in, sample_count = 0; + size_t bytes_per_frame, samples_per_frame; + uint8_t coef_index, shift_factor; + int32_t hist1 = stream->adpcm_history1_32; + int32_t hist2 = stream->adpcm_history2_32; + float scale; + + + /* external interleave (variable size), mono */ + bytes_per_frame = frame_size; + samples_per_frame = (bytes_per_frame - 0x01) * 2; + frames_in = first_sample / samples_per_frame; + first_sample = first_sample % samples_per_frame; + + /* parse frame header */ + frame_offset = stream->offset + bytes_per_frame * frames_in; + read_streamfile(frame, frame_offset, bytes_per_frame, stream->streamfile); /* ignore EOF errors */ + coef_index = (frame[0] >> 4) & 0xf; + shift_factor = (frame[0] >> 0) & 0xf; + + VGM_ASSERT_ONCE(coef_index > 5 || shift_factor > 12, "PS-ADPCM-piv: incorrect coefs/shift\n"); + if (coef_index > 5) /* just in case */ + coef_index = 5; + if (shift_factor > 12) /* same */ + shift_factor = 12; + + scale = (float)(1.0 / (double)(1 << shift_factor)); + + + /* decode nibbles */ + for (i = first_sample; i < first_sample + samples_to_do; i++) { + int32_t sample = 0; + uint8_t nibbles = frame[0x01 + i/2]; + + sample = !(i&1) ? /* low nibble first */ + (nibbles >> 0) & 0x0f : + (nibbles >> 4) & 0x0f; + sample = (int16_t)((sample << 12) & 0xf000); /* 16b sign extend + default scale */ + sample = sample*scale + ps_adpcm_coefs_f[coef_index][0]*hist1 + ps_adpcm_coefs_f[coef_index][1]*hist2; /* actually substracts negative coefs but whatevs */ + + outbuf[sample_count] = clamp16(sample); + sample_count += channelspacing; + + hist2 = hist1; + hist1 = sample; /* not clamped but no difference */ + } + + stream->adpcm_history1_32 = hist1; + stream->adpcm_history2_32 = hist2; +} + + +/* Find loop samples in PS-ADPCM data and return if the file loops. + * + * PS-ADPCM/VAG has optional bit flags that control looping in the SPU. + * Possible combinations (as usually defined in Sony's docs): + * - 0x0 (0000): Normal decode + * - 0x1 (0001): End marker (last frame) + * - 0x2 (0010): Loop region (marks files that *may* have loop flags somewhere) + * - 0x3 (0011): Loop end (jump to loop address) + * - 0x4 (0100): Start marker + * - 0x5 (0101): Same as 0x07? Extremely rare [Blood Omen: Legacy of Kain (PS1)] + * - 0x6 (0110): Loop start (save loop address) + * - 0x7 (0111): End marker and don't decode + * - 0x8+(1NNN): Not valid + */ +static int ps_find_loop_offsets_internal(STREAMFILE *sf, off_t start_offset, size_t data_size, int channels, size_t interleave, int32_t * p_loop_start, int32_t * p_loop_end, int config) { + int num_samples = 0, loop_start = 0, loop_end = 0; + int loop_start_found = 0, loop_end_found = 0; + off_t offset = start_offset; + off_t max_offset = start_offset + data_size; + size_t interleave_consumed = 0; + int detect_full_loops = config & 1; + + + if (data_size == 0 || channels == 0 || (channels > 1 && interleave == 0)) + return 0; + + while (offset < max_offset) { + uint8_t flag = read_u8(offset+0x01, sf) & 0x0F; /* lower nibble only (for HEVAG) */ + + /* theoretically possible and would use last 0x06 */ + VGM_ASSERT_ONCE(loop_start_found && flag == 0x06, "PS LOOPS: multiple loop start found at %x\n", (uint32_t)offset); + + if (flag == 0x06 && !loop_start_found) { + loop_start = num_samples; /* loop start before this frame */ + loop_start_found = 1; + } + + if (flag == 0x03 && !loop_end) { + loop_end = num_samples + 28; /* loop end after this frame */ + loop_end_found = 1; + + /* ignore strange case in Commandos (PS2), has many loop starts and ends */ + if (channels == 1 + && offset + 0x10 < max_offset + && (read_u8(offset + 0x11, sf) & 0x0F) == 0x06) { + loop_end = 0; + loop_end_found = 0; + } + + if (loop_start_found && loop_end_found) + break; + } + + /* hack for some games that don't have loop points but do full loops, + * if there is a "partial" 0x07 end flag pretend it wants to loop + * (sometimes this will loop non-looping tracks, and won't loop all repeating files) + * seems only used in Ratchet & Clank series and Ecco the Dolphin */ + if (flag == 0x01 && detect_full_loops) { + static const uint8_t eof[0x10] = {0xFF,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; + uint8_t buf[0x10]; + uint8_t hdr = read_u8(offset + 0x00, sf); + + int read = read_streamfile(buf, offset+0x10, sizeof(buf), sf); + if (read > 0 + && buf[0] != 0x00 /* ignore blank frame */ + && buf[0] != 0x0c /* ignore silent frame */ + && buf[0] != 0x3c /* ignore some L-R tracks with different end flags */ + ) { + + /* assume full loop with repeated frame header and null frame */ + if (hdr == buf[0] && memcmp(buf+1, eof+1, sizeof(buf) - 1) == 0) { + loop_start = 28; /* skip first frame as it's null in PS-ADPCM */ + loop_end = num_samples + 28; /* loop end after this frame */ + loop_start_found = 1; + loop_end_found = 1; + //;VGM_LOG("PS LOOPS: full loop found\n"); + break; + } + } + } + + + num_samples += 28; + offset += 0x10; + + /* skip other channels */ + interleave_consumed += 0x10; + if (interleave_consumed == interleave) { + interleave_consumed = 0; + offset += interleave*(channels - 1); + } + } + + VGM_ASSERT(loop_start_found && !loop_end_found, "PS LOOPS: found loop start but not loop end\n"); + VGM_ASSERT(loop_end_found && !loop_start_found, "PS LOOPS: found loop end but not loop start\n"); + //;VGM_LOG("PS LOOPS: start=%i, end=%i\n", loop_start, loop_end); + + /* From Sony's docs: if only loop_end is set loop back to "phoneme region start", but in practice doesn't */ + if (loop_start_found && loop_end_found) { + *p_loop_start = loop_start; + *p_loop_end = loop_end; + return 1; + } + + return 0; /* no loop */ +} + +int ps_find_loop_offsets(STREAMFILE *sf, off_t start_offset, size_t data_size, int channels, size_t interleave, int32_t *p_loop_start, int32_t *p_loop_end) { + return ps_find_loop_offsets_internal(sf, start_offset, data_size, channels, interleave, p_loop_start, p_loop_end, 0); +} + +int ps_find_loop_offsets_full(STREAMFILE *sf, off_t start_offset, size_t data_size, int channels, size_t interleave, int32_t *p_loop_start, int32_t *p_loop_end) { + return ps_find_loop_offsets_internal(sf, start_offset, data_size, channels, interleave, p_loop_start, p_loop_end, 1); +} + +size_t ps_find_padding(STREAMFILE *streamFile, off_t start_offset, size_t data_size, int channels, size_t interleave, int discard_empty) { + off_t min_offset, offset; + size_t frame_size = 0x10; + size_t padding_size = 0; + size_t interleave_consumed = 0; + + + if (data_size == 0 || channels == 0 || (channels > 0 && interleave == 0)) + return 0; + + offset = start_offset + data_size; + + /* in rare cases (ex. Gitaroo Man) channels have inconsistent empty padding, use first as guide */ + offset = offset - interleave * (channels - 1); + + /* some files have padding spanning multiple interleave blocks */ + min_offset = start_offset; //offset - interleave; + + while (offset > min_offset) { + uint32_t f1,f2,f3,f4; + uint8_t flag; + int is_empty = 0; + + offset -= frame_size; + + f1 = read_32bitBE(offset+0x00,streamFile); + f2 = read_32bitBE(offset+0x04,streamFile); + f3 = read_32bitBE(offset+0x08,streamFile); + f4 = read_32bitBE(offset+0x0c,streamFile); + flag = (f1 >> 16) & 0xFF; + + if (f1 == 0 && f2 == 0 && f3 == 0 && f4 == 0) + is_empty = 1; + + if (!is_empty && discard_empty) { + if (flag == 0x07 || flag == 0x77) + is_empty = 1; /* 'discard frame' flag */ + else if ((f1 & 0xFF00FFFF) == 0 && f2 == 0 && f3 == 0 && f4 == 0) + is_empty = 1; /* silent with flags (typical for looping files) */ + else if ((f1 & 0xFF00FFFF) == 0x0C000000 && f2 == 0 && f3 == 0 && f4 == 0) + is_empty = 1; /* silent (maybe shouldn't ignore flag 0x03?) */ + else if ((f1 & 0x0000FFFF) == 0x00007777 && f2 == 0x77777777 && f3 ==0x77777777 && f4 == 0x77777777) + is_empty = 1; /* silent-ish */ + } + + if (!is_empty) + break; + + padding_size += frame_size * channels; + + /* skip other channels */ + interleave_consumed += 0x10; + if (interleave_consumed == interleave) { + interleave_consumed = 0; + offset -= interleave*(channels - 1); + } + } + + return padding_size; +} + + +size_t ps_bytes_to_samples(size_t bytes, int channels) { + if (channels <= 0) return 0; + return bytes / channels / 0x10 * 28; +} + +size_t ps_cfg_bytes_to_samples(size_t bytes, size_t frame_size, int channels) { + int samples_per_frame = (frame_size - 0x01) * 2; + return bytes / channels / frame_size * samples_per_frame; +} + +/* test PS-ADPCM frames for correctness */ +int ps_check_format(STREAMFILE *streamFile, off_t offset, size_t max) { + off_t max_offset = offset + max; + if (max_offset > get_streamfile_size(streamFile)) + max_offset = get_streamfile_size(streamFile); + + while (offset < max_offset) { + uint8_t predictor = (read_8bit(offset+0x00,streamFile) >> 4) & 0x0f; + uint8_t flags = read_8bit(offset+0x01,streamFile); + + if (predictor > 5 || flags > 7) { + return 0; + } + offset += 0x10; + } + + return 1; +} diff --git a/Frameworks/vgmstream/vgmstream/src/layout/blocked_awc.c b/Frameworks/vgmstream/vgmstream/src/layout/blocked_awc.c index 763f046d3..3504e1d78 100644 --- a/Frameworks/vgmstream/vgmstream/src/layout/blocked_awc.c +++ b/Frameworks/vgmstream/vgmstream/src/layout/blocked_awc.c @@ -1,60 +1,75 @@ -#include "layout.h" -#include "../coding/coding.h" -#include "../layout/layout.h" -#include "../vgmstream.h" - - -static size_t get_block_header_size(STREAMFILE *streamFile, off_t offset, int channels, int big_endian); - -/* AWC music chunks */ -void block_update_awc(off_t block_offset, VGMSTREAM * vgmstream) { - STREAMFILE* streamFile = vgmstream->ch[0].streamfile; - int32_t (*read_32bit)(off_t,STREAMFILE*) = vgmstream->codec_endian ? read_32bitBE : read_32bitLE; - size_t header_size, entries, block_size, block_samples; - int i; - - /* assumed only AWC_IMA enters here, MPEG/XMA2 need special parsing as blocked layout is too limited */ - - entries = read_32bit(block_offset + 0x18*0 + 0x04, streamFile); /* assumed same for all channels */ - block_samples = entries * (0x800-4)*2; - block_size = vgmstream->full_block_size; - - vgmstream->current_block_offset = block_offset; - vgmstream->next_block_offset = block_offset + block_size; - vgmstream->current_block_samples = block_samples; - - /* starts with a header block */ - /* for each channel - * 0x00: start entry within channel (ie. entries * ch) - * 0x04: entries - * 0x08: samples to discard in the beginning of this block (MPEG only?) - * 0x0c: samples in channel (for MPEG/XMA2 can vary between channels) - * 0x10: MPEG only: close to number of frames but varies a bit? - * 0x14: MPEG only: channel usable data size (not counting padding) - * for each channel - * 32b * entries = global samples per frame in each block (for MPEG probably per full frame) - */ - - header_size = get_block_header_size(streamFile, block_offset, vgmstream->channels, vgmstream->codec_endian); - for (i = 0; i < vgmstream->channels; i++) { - vgmstream->ch[i].offset = block_offset + header_size + 0x800*entries*i; - } - -} - -static size_t get_block_header_size(STREAMFILE *streamFile, off_t offset, int channels, int big_endian) { - size_t header_size = 0; - int i; - int entries = channels; - int32_t (*read_32bit)(off_t,STREAMFILE*) = big_endian ? read_32bitBE : read_32bitLE; - - for (i = 0; i < entries; i++) { - header_size += 0x18; - header_size += read_32bit(offset + 0x18*i + 0x04, streamFile) * 0x04; /* entries in the table */ - } - - if (header_size % 0x800) /* padded */ - header_size += 0x800 - (header_size % 0x800); - - return header_size; -} +#include "layout.h" +#include "../coding/coding.h" +#include "../layout/layout.h" +#include "../vgmstream.h" + + +static size_t get_channel_header_size(STREAMFILE* sf, off_t offset, int channels, int big_endian); +static size_t get_block_header_size(STREAMFILE* sf, off_t offset, size_t channel_header_size, int channels, int big_endian); + +/* AWC music chunks */ +void block_update_awc(off_t block_offset, VGMSTREAM * vgmstream) { + STREAMFILE* sf = vgmstream->ch[0].streamfile; + int32_t (*read_32bit)(off_t,STREAMFILE*) = vgmstream->codec_endian ? read_32bitBE : read_32bitLE; + size_t header_size, entries, block_size, block_samples; + size_t channel_header_size; + int i; + + /* assumed only AWC_IMA enters here, MPEG/XMA2 need special parsing as blocked layout is too limited */ + entries = read_32bit(block_offset + 0x04, sf); /* se first channel, assume all are the same */ + //block_samples = entries * (0x800-4)*2; //todo use + block_samples = read_32bit(block_offset + 0x0c, sf); + block_size = vgmstream->full_block_size; + + vgmstream->current_block_offset = block_offset; + vgmstream->next_block_offset = block_offset + block_size; + vgmstream->current_block_samples = block_samples; + + /* starts with a header block */ + /* for each channel + * 0x00: start entry within channel (ie. entries * ch) but may be off by +1/+2 + * 0x04: entries + * 0x08: samples to discard in the beginning of this block (MPEG only?) + * 0x0c: samples in channel (for MPEG/XMA2 can vary between channels) + * (next fields don't exist in later versions for IMA) + * 0x10: (MPEG only, empty otherwise) close to number of frames but varies a bit? + * 0x14: (MPEG only, empty otherwise) channel usable data size (not counting padding) + * for each channel + * 32b * entries = global samples per frame in each block (for MPEG probably per full frame) + */ + + channel_header_size = get_channel_header_size(sf, block_offset, vgmstream->channels, vgmstream->codec_endian); + header_size = get_block_header_size(sf, block_offset, channel_header_size, vgmstream->channels, vgmstream->codec_endian); + for (i = 0; i < vgmstream->channels; i++) { + vgmstream->ch[i].offset = block_offset + header_size + 0x800*entries*i; + VGM_ASSERT(entries != read_32bit(block_offset + channel_header_size*i + 0x04, sf), "AWC: variable number of entries found at %lx\n", block_offset); + } + +} + +static size_t get_channel_header_size(STREAMFILE* sf, off_t offset, int channels, int big_endian) { + int32_t (*read_32bit)(off_t,STREAMFILE*) = big_endian ? read_32bitBE : read_32bitLE; + + /* later games have an smaller channel header, try to detect using + * an empty field not in IMA */ + if (read_32bit(offset + 0x14, sf) == 0x00) + return 0x18; + return 0x10; +} + +static size_t get_block_header_size(STREAMFILE* sf, off_t offset, size_t channel_header_size, int channels, int big_endian) { + size_t header_size = 0; + int i; + int entries = channels; + int32_t (*read_32bit)(off_t,STREAMFILE*) = big_endian ? read_32bitBE : read_32bitLE; + + for (i = 0; i < entries; i++) { + header_size += channel_header_size; + header_size += read_32bit(offset + channel_header_size*i + 0x04, sf) * 0x04; /* entries in the table */ + } + + if (header_size % 0x800) /* padded */ + header_size += 0x800 - (header_size % 0x800); + + return header_size; +} diff --git a/Frameworks/vgmstream/vgmstream/src/meta/awb.c b/Frameworks/vgmstream/vgmstream/src/meta/awb.c index 9d21b09f6..ede63671b 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/awb.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/awb.c @@ -209,6 +209,21 @@ static void load_awb_name(STREAMFILE *streamFile, STREAMFILE *acbFile, VGMSTREAM } } + /* try (name)_(name)_R001.awb + (name).acb [Sengoku Basara Battle Party (Mobile)] */ + if (!acbFile) { + char *cmp = "_R001"; + get_streamfile_basename(streamFile, filename, sizeof(filename)); + len_name = strlen(filename); + len_cmp = strlen(cmp); + + if (len_name > len_cmp && strcmp(filename + len_name - len_cmp, cmp) == 0) { + filename[(len_name - len_cmp) / 2] = '\0'; + strcat(filename, ".acb"); + VGM_LOG("%s\n", filename); + acbFile = open_streamfile_by_filename(streamFile, filename); + } + } + /* probably loaded */ load_acb_wave_name(acbFile, vgmstream, waveid, is_memory); diff --git a/Frameworks/vgmstream/vgmstream/src/meta/awc.c b/Frameworks/vgmstream/vgmstream/src/meta/awc.c index bde1a21df..9aeb39621 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/awc.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/awc.c @@ -1,349 +1,349 @@ -#include "meta.h" -#include "../coding/coding.h" -#include "../layout/layout.h" -#include "awc_xma_streamfile.h" - -typedef struct { - int big_endian; - int is_encrypted; - int is_music; - - int total_subsongs; - - int channel_count; - int sample_rate; - int codec; - int num_samples; - - int block_chunk; - - off_t stream_offset; - size_t stream_size; - -} awc_header; - -static int parse_awc_header(STREAMFILE* streamFile, awc_header* awc); - - -/* AWC - from RAGE (Rockstar Advanced Game Engine) audio (Red Dead Redemption, Max Payne 3, GTA5) */ -VGMSTREAM * init_vgmstream_awc(STREAMFILE *streamFile) { - VGMSTREAM * vgmstream = NULL; - awc_header awc = {0}; - - /* check extension */ - if (!check_extensions(streamFile,"awc")) - goto fail; - - /* check header */ - if (!parse_awc_header(streamFile, &awc)) - goto fail; - - - /* build the VGMSTREAM */ - vgmstream = allocate_vgmstream(awc.channel_count, 0); - if (!vgmstream) goto fail; - - vgmstream->sample_rate = awc.sample_rate; - vgmstream->num_samples = awc.num_samples; - vgmstream->num_streams = awc.total_subsongs; - vgmstream->stream_size = awc.stream_size; - vgmstream->meta_type = meta_AWC; - - - switch(awc.codec) { - case 0x01: /* PCM (PC/PS3) [sfx, rarely] */ - if (awc.is_music) goto fail; /* blocked_awc needs to be prepared */ - vgmstream->coding_type = awc.big_endian ? coding_PCM16BE : coding_PCM16LE; - vgmstream->layout_type = layout_interleave; - vgmstream->interleave_block_size = 0x02; - break; - - case 0x04: /* IMA (PC) */ - vgmstream->coding_type = coding_AWC_IMA; - vgmstream->layout_type = awc.is_music ? layout_blocked_awc : layout_none; - vgmstream->full_block_size = awc.block_chunk; - vgmstream->codec_endian = awc.big_endian; - break; - -#ifdef VGM_USE_FFMPEG - case 0x05: { /* XMA2 (X360) */ - uint8_t buf[0x100]; - size_t bytes, block_size, block_count, substream_size; - off_t substream_offset; - - if (awc.is_music) { - /* 1ch XMAs in blocks, we'll use layered layout + custom IO to get multi-FFmpegs working */ - int i; - layered_layout_data * data = NULL; - - /* init layout */ - data = init_layout_layered(awc.channel_count); - if (!data) goto fail; - vgmstream->layout_data = data; - vgmstream->layout_type = layout_layered; - vgmstream->coding_type = coding_FFmpeg; - - /* open each layer subfile */ - for (i = 0; i < awc.channel_count; i++) { - STREAMFILE* temp_streamFile; - int layer_channels = 1; - - /* build the layer VGMSTREAM */ - data->layers[i] = allocate_vgmstream(layer_channels, 0); - if (!data->layers[i]) goto fail; - - data->layers[i]->sample_rate = awc.sample_rate; - data->layers[i]->meta_type = meta_AWC; - data->layers[i]->coding_type = coding_FFmpeg; - data->layers[i]->layout_type = layout_none; - data->layers[i]->num_samples = awc.num_samples; - - /* setup custom IO streamfile, pass to FFmpeg and hope it's fooled */ - temp_streamFile = setup_awc_xma_streamfile(streamFile, awc.stream_offset, awc.stream_size, awc.block_chunk, awc.channel_count, i); - if (!temp_streamFile) goto fail; - - substream_offset = 0; /* where FFmpeg thinks data starts, which our custom streamFile will clamp */ - substream_size = get_streamfile_size(temp_streamFile); /* data of one XMA substream without blocks */ - block_size = 0x8000; /* no idea */ - block_count = substream_size / block_size; /* not accurate but not needed */ - - bytes = ffmpeg_make_riff_xma2(buf, 0x100, awc.num_samples, substream_size, layer_channels, awc.sample_rate, block_count, block_size); - data->layers[i]->codec_data = init_ffmpeg_header_offset(temp_streamFile, buf,bytes, substream_offset,substream_size); - - xma_fix_raw_samples(data->layers[i], temp_streamFile, substream_offset,substream_size, 0, 0,0); /* samples are ok? */ - - close_streamfile(temp_streamFile); - if (!data->layers[i]->codec_data) goto fail; - } - - /* setup layered VGMSTREAMs */ - if (!setup_layout_layered(data)) - goto fail; - } - else { - /* regular XMA for sfx */ - block_size = 0x8000; /* no idea */ - block_count = awc.stream_size / block_size; /* not accurate but not needed */ - - bytes = ffmpeg_make_riff_xma2(buf, 0x100, awc.num_samples, awc.stream_size, awc.channel_count, awc.sample_rate, block_count, block_size); - vgmstream->codec_data = init_ffmpeg_header_offset(streamFile, buf,bytes, awc.stream_offset,awc.stream_size); - if (!vgmstream->codec_data) goto fail; - vgmstream->coding_type = coding_FFmpeg; - vgmstream->layout_type = layout_none; - - xma_fix_raw_samples(vgmstream, streamFile, awc.stream_offset,awc.stream_size, 0, 0,0); /* samples are ok? */ - } - - break; - } - - -#endif -#ifdef VGM_USE_MPEG - case 0x07: { /* MPEG (PS3) */ - mpeg_custom_config cfg = {0}; - - cfg.chunk_size = awc.block_chunk; - cfg.big_endian = awc.big_endian; - - vgmstream->codec_data = init_mpeg_custom(streamFile, awc.stream_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_AWC, &cfg); - if (!vgmstream->codec_data) goto fail; - vgmstream->layout_type = layout_none; - - break; - } -#endif - - default: - VGM_LOG("AWC: unknown codec 0x%02x\n", awc.codec); - goto fail; - } - - - if (!vgmstream_open_stream(vgmstream,streamFile,awc.stream_offset)) - goto fail; - return vgmstream; - -fail: - close_vgmstream(vgmstream); - return NULL; -} - - -/* Parse Rockstar's AWC header (much info from LibertyV: https://github.com/koolkdev/libertyv). - * Made of entries for N streams, each with a number of tags pointing to chunks (header, data, events, etc). */ -static int parse_awc_header(STREAMFILE* streamFile, awc_header* awc) { - int64_t (*read_64bit)(off_t,STREAMFILE*) = NULL; - int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL; - int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL; - int i, ch, entries; - uint32_t flags, info_header, tag_count = 0, tags_skip = 0; - off_t off; - int target_subsong = streamFile->stream_index; - - - /* check header */ - if (read_32bitBE(0x00,streamFile) != 0x41444154 && /* "ADAT" (LE) */ - read_32bitBE(0x00,streamFile) != 0x54414441) /* "TADA" (BE) */ - goto fail; - - awc->big_endian = read_32bitBE(0x00,streamFile) == 0x54414441; - if (awc->big_endian) { - read_64bit = read_64bitBE; - read_32bit = read_32bitBE; - read_16bit = read_16bitBE; - } else { - read_64bit = read_64bitLE; - read_32bit = read_32bitLE; - read_16bit = read_16bitLE; - } - - - flags = read_32bit(0x04,streamFile); - entries = read_32bit(0x08,streamFile); - //header_size = read_32bit(0x0c,streamFile); /* after to stream id/tags, not including chunks */ - - off = 0x10; - - if ((flags & 0xFF00FFFF) != 0xFF000001 || (flags & 0x00F00000)) { - VGM_LOG("AWC: unknown flags 0x%08x\n", flags); - goto fail; - } - - if (flags & 0x00010000) /* some kind of mini offset table */ - off += 0x2 * entries; - //if (flags % 0x00020000) /* seems to indicate chunks are not ordered (ie. header may go after data) */ - // ... - //if (flags % 0x00040000) /* music/multichannel flag? (GTA5, not seen in RDR) */ - // awc->is_music = 1; - if (flags & 0x00080000) /* encrypted data chunk (most of GTA5 PC) */ - awc->is_encrypted = 1; - - if (awc->is_encrypted) { - VGM_LOG("AWC: encrypted data found\n"); - goto fail; - } - - /* Music when the first id is 0 (base/fake entry with info for all channels), sfx pack otherwise. - * sfx = N single streams, music = N-1 interleaved mono channels (even for MP3/XMA). - * Music seems layered (N-1/2 stereo pairs), maybe set with events? */ - awc->is_music = (read_32bit(off + 0x00,streamFile) & 0x1FFFFFFF) == 0x00000000; - if (awc->is_music) { /* all streams except id 0 is a channel */ - awc->total_subsongs = 1; - target_subsong = 1; /* we only need id 0, though channels may have its own tags/chunks */ - } - else { /* each stream is a single sound */ - awc->total_subsongs = entries; - if (target_subsong == 0) target_subsong = 1; - if (target_subsong < 0 || target_subsong > awc->total_subsongs || awc->total_subsongs < 1) goto fail; - } - - - /* get stream base info */ - for (i = 0; i < entries; i++) { - info_header = read_32bit(off + 0x04*i, streamFile); - tag_count = (info_header >> 29) & 0x7; /* 3b */ - //id = (info_header >> 0) & 0x1FFFFFFF; /* 29b */ - if (target_subsong-1 == i) - break; - tags_skip += tag_count; /* tags to skip to reach target's tags, in the next header */ - } - off += 0x04*entries; - off += 0x08*tags_skip; - - /* get stream tags */ - for (i = 0; i < tag_count; i++) { - uint64_t tag_header; - uint8_t tag; - size_t size; - off_t offset; - - tag_header = (uint64_t)read_64bit(off + 0x08*i,streamFile); - tag = (uint8_t)((tag_header >> 56) & 0xFF); /* 8b */ - size = (size_t)((tag_header >> 28) & 0x0FFFFFFF); /* 28b */ - offset = (off_t)((tag_header >> 0) & 0x0FFFFFFF); /* 28b */ - - /* Tags are apparently part of a hash derived from a word ("data", "format", etc). - * If music + 1ch, the header and data chunks can repeat for no reason (sometimes not even pointed). */ - switch(tag) { - case 0x55: /* data */ - awc->stream_offset = offset; - awc->stream_size = size; - break; - - case 0x48: /* music header */ - if (!awc->is_music) { - VGM_LOG("AWC: music header found in sfx\n"); - goto fail; - } - - /* 0x00(32): unknown (some count?) */ - awc->block_chunk = read_32bit(offset + 0x04,streamFile); - awc->channel_count = read_32bit(offset + 0x08,streamFile); - - if (awc->channel_count != entries - 1) { /* not counting id-0 */ - VGM_LOG("AWC: number of music channels doesn't match entries\n"); - goto fail; - } - - for (ch = 0; ch < awc->channel_count; ch++) { - int num_samples, sample_rate, codec; - /* 0x00(32): stream id (not always in the header entries order) */ - /* 0x08(16): headroom?, 0x0d(8): round size?, 0x0e(16): unknown (zero?) */ - num_samples = read_32bit(offset + 0x0c + 0x10*ch + 0x04,streamFile); - sample_rate = (uint16_t)read_16bit(offset + 0x0c + 0x10*ch + 0x0a,streamFile); - codec = read_8bit(offset + 0x0c + 0x10*ch + 0x0c, streamFile); - - /* validate as all channels should repeat this (when channels is even and > 2 seems - * it's stereo pairs, and num_samples can vary slightly but no matter) */ - if ((awc->num_samples && !(awc->num_samples >= num_samples - 10 && awc->num_samples <= num_samples + 10)) || - (awc->sample_rate && awc->sample_rate != sample_rate) || - (awc->codec && awc->codec != codec)) { - VGM_LOG("AWC: found header diffs in channel %i, ns=%i vs %i, sr=%i vs %i, c=%i vs %i\n", - ch, awc->num_samples, num_samples, awc->sample_rate, sample_rate, awc->codec, codec); - goto fail; - } - - awc->num_samples = num_samples; - awc->sample_rate = sample_rate; - awc->codec = codec; - } - break; - - case 0xFA: /* sfx header */ - if (awc->is_music) { - VGM_LOG("AWC: sfx header found in music\n"); - goto fail; - } - /* 0x04(32): -1?, 0x0a(16x4): unknown x4, 0x12: null? */ - awc->num_samples = read_32bit(offset + 0x00,streamFile); - awc->sample_rate = (uint16_t)read_16bit(offset + 0x08,streamFile); - awc->codec = read_8bit(offset + 0x13, streamFile); - awc->channel_count = 1; - break; - - case 0xA3: /* block-to-sample table (32b x number of blocks w/ num_samples at the start of each block) */ - case 0xBD: /* events (32bx4): type_hash, params_hash, timestamp_ms, flags */ - default: /* 0x5C=animation/RSC?, 0x68=midi?, 0x36/0x2B/0x5A/0xD9=? */ - //VGM_LOG("AWC: ignoring unknown tag 0x%02x\n", tag); - break; - } - } - - if (!awc->stream_offset) { - VGM_LOG("AWC: stream offset not found\n"); - goto fail; - } - - /* If music, data is divided into blocks of block_chunk size with padding. - * Each block has a header/seek table and interleaved data for all channels */ - if (awc->is_music && read_32bit(awc->stream_offset, streamFile) != 0) { - VGM_LOG("AWC: music found, but block doesn't start with seek table at %x\n", (uint32_t)awc->stream_offset); - goto fail; - } - - - return 1; -fail: - return 0; -} +#include "meta.h" +#include "../coding/coding.h" +#include "../layout/layout.h" +#include "awc_xma_streamfile.h" + +typedef struct { + int big_endian; + int is_encrypted; + int is_music; + + int total_subsongs; + + int channel_count; + int sample_rate; + int codec; + int num_samples; + + int block_chunk; + + off_t stream_offset; + size_t stream_size; + +} awc_header; + +static int parse_awc_header(STREAMFILE* streamFile, awc_header* awc); + + +/* AWC - from RAGE (Rockstar Advanced Game Engine) audio (Red Dead Redemption, Max Payne 3, GTA5) */ +VGMSTREAM * init_vgmstream_awc(STREAMFILE *streamFile) { + VGMSTREAM * vgmstream = NULL; + awc_header awc = {0}; + + /* check extension */ + if (!check_extensions(streamFile,"awc")) + goto fail; + + /* check header */ + if (!parse_awc_header(streamFile, &awc)) + goto fail; + + + /* build the VGMSTREAM */ + vgmstream = allocate_vgmstream(awc.channel_count, 0); + if (!vgmstream) goto fail; + + vgmstream->sample_rate = awc.sample_rate; + vgmstream->num_samples = awc.num_samples; + vgmstream->num_streams = awc.total_subsongs; + vgmstream->stream_size = awc.stream_size; + vgmstream->meta_type = meta_AWC; + + + switch(awc.codec) { + case 0x01: /* PCM (PC/PS3) [sfx, rarely] */ + if (awc.is_music) goto fail; /* blocked_awc needs to be prepared */ + vgmstream->coding_type = awc.big_endian ? coding_PCM16BE : coding_PCM16LE; + vgmstream->layout_type = layout_interleave; + vgmstream->interleave_block_size = 0x02; + break; + + case 0x04: /* IMA (PC) */ + vgmstream->coding_type = coding_AWC_IMA; + vgmstream->layout_type = awc.is_music ? layout_blocked_awc : layout_none; + vgmstream->full_block_size = awc.block_chunk; + vgmstream->codec_endian = awc.big_endian; + break; + +#ifdef VGM_USE_FFMPEG + case 0x05: { /* XMA2 (X360) */ + uint8_t buf[0x100]; + size_t bytes, block_size, block_count, substream_size; + off_t substream_offset; + + if (awc.is_music) { + /* 1ch XMAs in blocks, we'll use layered layout + custom IO to get multi-FFmpegs working */ + int i; + layered_layout_data * data = NULL; + + /* init layout */ + data = init_layout_layered(awc.channel_count); + if (!data) goto fail; + vgmstream->layout_data = data; + vgmstream->layout_type = layout_layered; + vgmstream->coding_type = coding_FFmpeg; + + /* open each layer subfile */ + for (i = 0; i < awc.channel_count; i++) { + STREAMFILE* temp_streamFile; + int layer_channels = 1; + + /* build the layer VGMSTREAM */ + data->layers[i] = allocate_vgmstream(layer_channels, 0); + if (!data->layers[i]) goto fail; + + data->layers[i]->sample_rate = awc.sample_rate; + data->layers[i]->meta_type = meta_AWC; + data->layers[i]->coding_type = coding_FFmpeg; + data->layers[i]->layout_type = layout_none; + data->layers[i]->num_samples = awc.num_samples; + + /* setup custom IO streamfile, pass to FFmpeg and hope it's fooled */ + temp_streamFile = setup_awc_xma_streamfile(streamFile, awc.stream_offset, awc.stream_size, awc.block_chunk, awc.channel_count, i); + if (!temp_streamFile) goto fail; + + substream_offset = 0; /* where FFmpeg thinks data starts, which our custom streamFile will clamp */ + substream_size = get_streamfile_size(temp_streamFile); /* data of one XMA substream without blocks */ + block_size = 0x8000; /* no idea */ + block_count = substream_size / block_size; /* not accurate but not needed */ + + bytes = ffmpeg_make_riff_xma2(buf, 0x100, awc.num_samples, substream_size, layer_channels, awc.sample_rate, block_count, block_size); + data->layers[i]->codec_data = init_ffmpeg_header_offset(temp_streamFile, buf,bytes, substream_offset,substream_size); + + xma_fix_raw_samples(data->layers[i], temp_streamFile, substream_offset,substream_size, 0, 0,0); /* samples are ok? */ + + close_streamfile(temp_streamFile); + if (!data->layers[i]->codec_data) goto fail; + } + + /* setup layered VGMSTREAMs */ + if (!setup_layout_layered(data)) + goto fail; + } + else { + /* regular XMA for sfx */ + block_size = 0x8000; /* no idea */ + block_count = awc.stream_size / block_size; /* not accurate but not needed */ + + bytes = ffmpeg_make_riff_xma2(buf, 0x100, awc.num_samples, awc.stream_size, awc.channel_count, awc.sample_rate, block_count, block_size); + vgmstream->codec_data = init_ffmpeg_header_offset(streamFile, buf,bytes, awc.stream_offset,awc.stream_size); + if (!vgmstream->codec_data) goto fail; + vgmstream->coding_type = coding_FFmpeg; + vgmstream->layout_type = layout_none; + + xma_fix_raw_samples(vgmstream, streamFile, awc.stream_offset,awc.stream_size, 0, 0,0); /* samples are ok? */ + } + + break; + } + + +#endif +#ifdef VGM_USE_MPEG + case 0x07: { /* MPEG (PS3) */ + mpeg_custom_config cfg = {0}; + + cfg.chunk_size = awc.block_chunk; + cfg.big_endian = awc.big_endian; + + vgmstream->codec_data = init_mpeg_custom(streamFile, awc.stream_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_AWC, &cfg); + if (!vgmstream->codec_data) goto fail; + vgmstream->layout_type = layout_none; + + break; + } +#endif + + default: + VGM_LOG("AWC: unknown codec 0x%02x\n", awc.codec); + goto fail; + } + + + if (!vgmstream_open_stream(vgmstream,streamFile,awc.stream_offset)) + goto fail; + return vgmstream; + +fail: + close_vgmstream(vgmstream); + return NULL; +} + + +/* Parse Rockstar's AWC header (much info from LibertyV: https://github.com/koolkdev/libertyv). + * Made of entries for N streams, each with a number of tags pointing to chunks (header, data, events, etc). */ +static int parse_awc_header(STREAMFILE* streamFile, awc_header* awc) { + int64_t (*read_64bit)(off_t,STREAMFILE*) = NULL; + int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL; + int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL; + int i, ch, entries; + uint32_t flags, info_header, tag_count = 0, tags_skip = 0; + off_t off; + int target_subsong = streamFile->stream_index; + + + /* check header */ + if (read_32bitBE(0x00,streamFile) != 0x41444154 && /* "ADAT" (LE) */ + read_32bitBE(0x00,streamFile) != 0x54414441) /* "TADA" (BE) */ + goto fail; + + awc->big_endian = read_32bitBE(0x00,streamFile) == 0x54414441; + if (awc->big_endian) { + read_64bit = read_64bitBE; + read_32bit = read_32bitBE; + read_16bit = read_16bitBE; + } else { + read_64bit = read_64bitLE; + read_32bit = read_32bitLE; + read_16bit = read_16bitLE; + } + + + flags = read_32bit(0x04,streamFile); + entries = read_32bit(0x08,streamFile); + //header_size = read_32bit(0x0c,streamFile); /* after to stream id/tags, not including chunks */ + + off = 0x10; + + if ((flags & 0xFF00FFFF) != 0xFF000001 || (flags & 0x00F00000)) { + VGM_LOG("AWC: unknown flags 0x%08x\n", flags); + goto fail; + } + + if (flags & 0x00010000) /* some kind of mini offset table */ + off += 0x2 * entries; + //if (flags % 0x00020000) /* seems to indicate chunks are not ordered (ie. header may go after data) */ + // ... + //if (flags % 0x00040000) /* music/multichannel flag? (GTA5, not seen in RDR) */ + // awc->is_music = 1; + if (flags & 0x00080000) /* encrypted data chunk (most of GTA5 PC) */ + awc->is_encrypted = 1; + + if (awc->is_encrypted) { + VGM_LOG("AWC: encrypted data found\n"); + goto fail; + } + + /* Music when the first id is 0 (base/fake entry with info for all channels), sfx pack otherwise. + * sfx = N single streams, music = N-1 interleaved mono channels (even for MP3/XMA). + * Music seems layered (N-1/2 stereo pairs), maybe set with events? */ + awc->is_music = (read_32bit(off + 0x00,streamFile) & 0x1FFFFFFF) == 0x00000000; + if (awc->is_music) { /* all streams except id 0 is a channel */ + awc->total_subsongs = 1; + target_subsong = 1; /* we only need id 0, though channels may have its own tags/chunks */ + } + else { /* each stream is a single sound */ + awc->total_subsongs = entries; + if (target_subsong == 0) target_subsong = 1; + if (target_subsong < 0 || target_subsong > awc->total_subsongs || awc->total_subsongs < 1) goto fail; + } + + + /* get stream base info */ + for (i = 0; i < entries; i++) { + info_header = read_32bit(off + 0x04*i, streamFile); + tag_count = (info_header >> 29) & 0x7; /* 3b */ + //id = (info_header >> 0) & 0x1FFFFFFF; /* 29b */ + if (target_subsong-1 == i) + break; + tags_skip += tag_count; /* tags to skip to reach target's tags, in the next header */ + } + off += 0x04*entries; + off += 0x08*tags_skip; + + /* get stream tags */ + for (i = 0; i < tag_count; i++) { + uint64_t tag_header; + uint8_t tag; + size_t size; + off_t offset; + + tag_header = (uint64_t)read_64bit(off + 0x08*i,streamFile); + tag = (uint8_t)((tag_header >> 56) & 0xFF); /* 8b */ + size = (size_t)((tag_header >> 28) & 0x0FFFFFFF); /* 28b */ + offset = (off_t)((tag_header >> 0) & 0x0FFFFFFF); /* 28b */ + + /* Tags are apparently part of a hash derived from a word ("data", "format", etc). + * If music + 1ch, the header and data chunks can repeat for no reason (sometimes not even pointed). */ + switch(tag) { + case 0x55: /* data */ + awc->stream_offset = offset; + awc->stream_size = size; + break; + + case 0x48: /* music header */ + if (!awc->is_music) { + VGM_LOG("AWC: music header found in sfx\n"); + goto fail; + } + + /* 0x00(32): unknown (some count?) */ + awc->block_chunk = read_32bit(offset + 0x04,streamFile); + awc->channel_count = read_32bit(offset + 0x08,streamFile); + + if (awc->channel_count != entries - 1) { /* not counting id-0 */ + VGM_LOG("AWC: number of music channels doesn't match entries\n"); + goto fail; + } + + for (ch = 0; ch < awc->channel_count; ch++) { + int num_samples, sample_rate, codec; + /* 0x00(32): stream id (not always in the header entries order) */ + /* 0x08(16): headroom?, 0x0d(8): round size?, 0x0e(16): unknown (zero?) */ + num_samples = read_32bit(offset + 0x0c + 0x10*ch + 0x04,streamFile); + sample_rate = (uint16_t)read_16bit(offset + 0x0c + 0x10*ch + 0x0a,streamFile); + codec = read_8bit(offset + 0x0c + 0x10*ch + 0x0c, streamFile); + + /* validate as all channels should repeat this (when channels is even and > 2 seems + * it's stereo pairs, and num_samples can vary slightly but no matter) */ + if ((awc->num_samples && !(awc->num_samples >= num_samples - 10 && awc->num_samples <= num_samples + 10)) || + (awc->sample_rate && awc->sample_rate != sample_rate) || + (awc->codec && awc->codec != codec)) { + VGM_LOG("AWC: found header diffs in channel %i, ns=%i vs %i, sr=%i vs %i, c=%i vs %i\n", + ch, awc->num_samples, num_samples, awc->sample_rate, sample_rate, awc->codec, codec); + //goto fail; //todo some Max Payne 3 cutscene channels have huge sample diffs + } + + awc->num_samples = num_samples; + awc->sample_rate = sample_rate; + awc->codec = codec; + } + break; + + case 0xFA: /* sfx header */ + if (awc->is_music) { + VGM_LOG("AWC: sfx header found in music\n"); + goto fail; + } + /* 0x04(32): -1?, 0x0a(16x4): unknown x4, 0x12: null? */ + awc->num_samples = read_32bit(offset + 0x00,streamFile); + awc->sample_rate = (uint16_t)read_16bit(offset + 0x08,streamFile); + awc->codec = read_8bit(offset + 0x13, streamFile); + awc->channel_count = 1; + break; + + case 0xA3: /* block-to-sample table (32b x number of blocks w/ num_samples at the start of each block) */ + case 0xBD: /* events (32bx4): type_hash, params_hash, timestamp_ms, flags */ + default: /* 0x5C=animation/RSC?, 0x68=midi?, 0x36/0x2B/0x5A/0xD9=? */ + //VGM_LOG("AWC: ignoring unknown tag 0x%02x\n", tag); + break; + } + } + + if (!awc->stream_offset) { + VGM_LOG("AWC: stream offset not found\n"); + goto fail; + } + + /* If music, data is divided into blocks of block_chunk size with padding. + * Each block has a header/seek table and interleaved data for all channels */ + if (awc->is_music && read_32bit(awc->stream_offset, streamFile) != 0) { + VGM_LOG("AWC: music found, but block doesn't start with seek table at %x\n", (uint32_t)awc->stream_offset); + goto fail; + } + + + return 1; +fail: + return 0; +} diff --git a/Frameworks/vgmstream/vgmstream/src/meta/meta.h b/Frameworks/vgmstream/vgmstream/src/meta/meta.h index 39ab32c6d..bb619bc10 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/meta.h +++ b/Frameworks/vgmstream/vgmstream/src/meta/meta.h @@ -860,6 +860,7 @@ VGMSTREAM * init_vgmstream_nub_wav(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_nub_vag(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_nub_at3(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_nub_xma(STREAMFILE *streamFile); +VGMSTREAM * init_vgmstream_nub_dsp(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_nub_idsp(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_nub_is14(STREAMFILE * streamFile); diff --git a/Frameworks/vgmstream/vgmstream/src/meta/nub.c b/Frameworks/vgmstream/vgmstream/src/meta/nub.c index 136c9b9fd..77bf09163 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/nub.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/nub.c @@ -120,6 +120,11 @@ VGMSTREAM * init_vgmstream_nub(STREAMFILE *streamFile) { init_vgmstream_function = init_vgmstream_nub_xma; break; + case 0x05: /* "dsp\0" */ + fake_ext = "dsp"; + init_vgmstream_function = init_vgmstream_nub_dsp; + break; + case 0x06: /* "idsp" */ fake_ext = "idsp"; init_vgmstream_function = init_vgmstream_nub_idsp; @@ -130,7 +135,6 @@ VGMSTREAM * init_vgmstream_nub(STREAMFILE *streamFile) { init_vgmstream_function = init_vgmstream_nub_is14; break; - case 0x05: default: VGM_LOG("NUB: unknown codec %x\n", codec); goto fail; @@ -512,6 +516,40 @@ fail: return NULL; } +/* .nub dsp - from Namco NUB archives [Taiko no Tatsujin Wii Chou Goukanban (Wii)] */ +VGMSTREAM * init_vgmstream_nub_dsp(STREAMFILE *streamFile) { + VGMSTREAM *vgmstream = NULL; + STREAMFILE *temp_sf = NULL; + off_t header_offset, stream_offset; + size_t header_size, stream_size; + + + /* checks */ + if (!check_extensions(streamFile,"dsp")) + goto fail; + if (read_32bitBE(0x00,streamFile) != 0x64737000) /* "dsp\0" */ + goto fail; + + /* paste header+data together and pass to meta, which has loop info too */ + header_offset = 0xBC; + stream_size = read_32bitBE(0x14, streamFile); + header_size = read_32bitBE(0x1c, streamFile); + stream_offset = align_size_to_block(header_offset + header_size, 0x10); + + temp_sf = setup_nub_streamfile(streamFile, header_offset, header_size, stream_offset, stream_size, "dsp"); + if (!temp_sf) goto fail; + + vgmstream = init_vgmstream_ngc_dsp_std(temp_sf); + if (!vgmstream) goto fail; + + close_streamfile(temp_sf); + return vgmstream; +fail: + close_streamfile(temp_sf); + close_vgmstream(vgmstream); + return NULL; +} + /* .nub idsp - from Namco NUB archives [Soul Calibur Legends (Wii), Sky Crawlers: Innocent Aces (Wii)] */ VGMSTREAM * init_vgmstream_nub_idsp(STREAMFILE *streamFile) { VGMSTREAM *vgmstream = NULL; diff --git a/Frameworks/vgmstream/vgmstream/src/meta/riff.c b/Frameworks/vgmstream/vgmstream/src/meta/riff.c index 183d39f1b..efd6e9d86 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/riff.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/riff.c @@ -221,7 +221,7 @@ static int read_fmt(int big_endian, STREAMFILE * streamFile, off_t current_chunk goto fail; #endif - case 0x270: /* ATRAC3 */ + case 0x0270: /* ATRAC3 */ #ifdef VGM_USE_FFMPEG fmt->coding_type = coding_FFmpeg; fmt->is_at3 = 1; @@ -312,6 +312,7 @@ VGMSTREAM * init_vgmstream_riff(STREAMFILE *streamFile) { int32_t loop_start_wsmp = -1, loop_end_wsmp = -1; int32_t loop_start_smpl = -1, loop_end_smpl = -1; int32_t loop_start_cue = -1; + int32_t loop_start_nxbf = -1; int FormatChunkFound = 0, DataChunkFound = 0, JunkFound = 0; @@ -340,7 +341,7 @@ VGMSTREAM * init_vgmstream_riff(STREAMFILE *streamFile) { * .wvx: Godzilla - Destroy All Monsters Melee (Xbox) * .str: Harry Potter and the Philosopher's Stone (Xbox) * .at3: standard ATRAC3 - * .rws: Climax games (Silent Hill Origins PSP, Oblivion PSP) ATRAC3 + * .rws: Climax ATRAC3 [Silent Hill Origins (PSP), Oblivion (PSP)] * .aud: EA Replay ATRAC3 * .at9: standard ATRAC9 * .saf: Whacked! (Xbox) @@ -377,6 +378,8 @@ VGMSTREAM * init_vgmstream_riff(STREAMFILE *streamFile) { riff_size -= 0x04; /* [Halo 2 (PC)] (possibly bad extractor? 'Gravemind Tool') */ else if (riff_size == file_size && codec == 0x0300) riff_size -= 0x08; /* [Chrono Ma:gia (Android)] */ + else if (riff_size >= file_size && read_32bitBE(0x24,streamFile) == 0x4E584246) /* "NXBF" */ + riff_size = file_size - 0x08; /* [R:Racing Evolution (Xbox)] */ } /* check for truncated RIFF */ @@ -506,6 +509,23 @@ VGMSTREAM * init_vgmstream_riff(STREAMFILE *streamFile) { } break; + case 0x4E584246: /* "NXBF" (Namco NUS v1) [R:Racing Evolution (Xbox)] */ + /* 0x00: "NXBF" again */ + /* 0x04: always 0x1000? */ + /* 0x08: data size */ + /* 0x0c: channels */ + /* 0x10: null */ + loop_start_nxbf = read_32bitLE(current_chunk + 0x08 + 0x14, streamFile); + /* 0x18: sample rate */ + /* 0x1c: volume? */ + /* 0x20: type/flags? */ + /* 0x24: codec? */ + /* 0x28: null */ + /* 0x2c: null */ + /* 0x30: type/flags? */ + loop_flag = (loop_start_nxbf >= 0); + break; + case 0x4A554E4B: /* "JUNK" */ JunkFound = 1; break; @@ -778,6 +798,14 @@ VGMSTREAM * init_vgmstream_riff(STREAMFILE *streamFile) { vgmstream->loop_start_sample = loop_start_cue; vgmstream->loop_end_sample = vgmstream->num_samples; } + else if (loop_start_nxbf != -1) { + switch (fmt.coding_type) { + case coding_PCM16LE: + vgmstream->loop_start_sample = pcm_bytes_to_samples(loop_start_nxbf, vgmstream->channels, 16); + vgmstream->loop_end_sample = vgmstream->num_samples; + break; + } + } } if (mwv) { vgmstream->meta_type = meta_RIFF_WAVE_MWV; diff --git a/Frameworks/vgmstream/vgmstream/src/meta/sgxd.c b/Frameworks/vgmstream/vgmstream/src/meta/sgxd.c index ab86c0a1e..b107b22b3 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/sgxd.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/sgxd.c @@ -153,10 +153,11 @@ VGMSTREAM * init_vgmstream_sgxd(STREAMFILE *streamFile) { break; } #endif - case 0x05: /* Short PS-ADPCM [Afrika (PS3)] */ + case 0x05: /* Short PS-ADPCM [Afrika (PS3), LocoRoco Cocoreccho! (PS3)] */ vgmstream->coding_type = coding_PSX_cfg; vgmstream->layout_type = layout_interleave; vgmstream->interleave_block_size = 0x4; + vgmstream->codec_config = 1; /* needs extended table */ break; diff --git a/Frameworks/vgmstream/vgmstream/src/meta/ta_aac.c b/Frameworks/vgmstream/vgmstream/src/meta/ta_aac.c index 59388f20d..05dd29a4f 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/ta_aac.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/ta_aac.c @@ -1,373 +1,373 @@ -#include "meta.h" -#include "../coding/coding.h" - -/* AAC - tri-Ace (Aska engine) Audio Container */ - -/* Xbox 360 Variants (Star Ocean 4, End of Eternity, Infinite Undiscovery) */ -VGMSTREAM * init_vgmstream_ta_aac_x360(STREAMFILE *streamFile) { - VGMSTREAM * vgmstream = NULL; - off_t start_offset; - int loop_flag, channel_count; - size_t sampleRate, numSamples, startSample, dataSize, blockSize, blockCount; // A mess - - /* check extension, case insensitive */ - /* .aac: expected, .laac/ace: for players to avoid hijacking MP4/AAC */ - if ( !check_extensions(streamFile,"aac,laac,ace")) - goto fail; - - if (read_32bitBE(0x00,streamFile) != 0x41414320) /* "AAC " */ - goto fail; - - /* Ok, let's check what's behind door number 1 */ - if (read_32bitBE(0x1000, streamFile) == 0x41534320) /* "ASC " */ - { - loop_flag = read_32bitBE(0x1118, streamFile); - - /*Funky Channel Count Checking */ - if (read_32bitBE(0x1184, streamFile) == 0x7374726D) - channel_count = 6; - else if (read_32bitBE(0x1154, streamFile) == 0x7374726D) - channel_count = 4; - else - channel_count = read_8bit(0x1134, streamFile); - - sampleRate = read_32bitBE(0x10F4, streamFile); - numSamples = read_32bitBE(0x10FC, streamFile); - startSample = read_32bitBE(0x10F8, streamFile); - dataSize = read_32bitBE(0x10F0, streamFile); - blockSize = read_32bitBE(0x1100, streamFile); - blockCount = read_32bitBE(0x110C, streamFile); - } - else if (read_32bitBE(0x1000, streamFile) == 0x57415645) /* "WAVE" */ - { - loop_flag = read_32bitBE(0x1048, streamFile); - - /*Funky Channel Count Checking */ - if (read_32bitBE(0x10B0, streamFile) == 0x7374726D) - channel_count = 6; - else if (read_32bitBE(0x1080, streamFile) == 0x7374726D) - channel_count = 4; - else - channel_count = read_8bit(0x1060, streamFile); - - sampleRate = read_32bitBE(0x1024, streamFile); - numSamples = read_32bitBE(0x102C, streamFile); - startSample = read_32bitBE(0x1028, streamFile); - dataSize = read_32bitBE(0x1020, streamFile); - blockSize = read_32bitBE(0x1030, streamFile); - blockCount = read_32bitBE(0x103C, streamFile); - } - else if (read_32bitBE(0x1000, streamFile) == 0x00000000) /* some like to be special */ - { - loop_flag = read_32bitBE(0x6048, streamFile); - - /*Funky Channel Count Checking */ - if (read_32bitBE(0x60B0, streamFile) == 0x7374726D) - channel_count = 6; - else if (read_32bitBE(0x6080, streamFile) == 0x7374726D) - channel_count = 4; - else - channel_count = read_8bit(0x6060, streamFile); - - sampleRate = read_32bitBE(0x6024, streamFile); - numSamples = read_32bitBE(0x602C, streamFile); - startSample = read_32bitBE(0x6028, streamFile); - dataSize = read_32bitBE(0x6020, streamFile); - blockSize = read_32bitBE(0x6030, streamFile); - blockCount = read_32bitBE(0x603C, streamFile); - } - else - goto fail; //cuz I don't know if there are other variants - - /* build the VGMSTREAM */ - vgmstream = allocate_vgmstream(channel_count,loop_flag); - if (!vgmstream) goto fail; - - if (read_32bitBE(0x1000, streamFile) == 0x00000000) - start_offset = 0x7000; - else - start_offset = 0x2000; - - vgmstream->sample_rate = sampleRate; - vgmstream->channels = channel_count; - vgmstream->num_samples = numSamples; - if (loop_flag) { - vgmstream->loop_start_sample = startSample; - vgmstream->loop_end_sample = vgmstream->num_samples; - } - vgmstream->meta_type = meta_TA_AAC_X360; - -#ifdef VGM_USE_FFMPEG - { - ffmpeg_codec_data *ffmpeg_data = NULL; - uint8_t buf[100]; - size_t bytes, datasize, block_size, block_count; - - block_count = blockCount; - block_size = blockSize; - datasize = dataSize; - - bytes = ffmpeg_make_riff_xma2(buf,100, vgmstream->num_samples, datasize, vgmstream->channels, vgmstream->sample_rate, block_count, block_size); - ffmpeg_data = init_ffmpeg_header_offset(streamFile, buf,bytes, start_offset,datasize); - if ( !ffmpeg_data ) goto fail; - vgmstream->codec_data = ffmpeg_data; - vgmstream->coding_type = coding_FFmpeg; - vgmstream->layout_type = layout_none; - - xma_fix_raw_samples(vgmstream, streamFile, start_offset, datasize, 0, 1,1); - if (loop_flag) { /* reapply adjusted samples */ - vgmstream->loop_end_sample = vgmstream->num_samples; - } - - } -#else - goto fail; -#endif - - /* open the file for reading */ - if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) ) - goto fail; - return vgmstream; - -fail: - close_vgmstream(vgmstream); - return NULL; -} - -/* PlayStation 3 Variants (Star Ocean International, Resonance of Fate) */ -VGMSTREAM * init_vgmstream_ta_aac_ps3(STREAMFILE *streamFile) { - VGMSTREAM * vgmstream = NULL; - off_t start_offset; - int loop_flag, channel_count; - uint32_t data_size, loop_start, loop_end, codec_id, asc_chunk; - - /* check extension, case insensitive */ - /* .aac: expected, .laac/ace: for players to avoid hijacking MP4/AAC */ - if (!check_extensions(streamFile, "aac,laac,ace")) - goto fail; - - if (read_32bitBE(0x00, streamFile) != 0x41414320) /* "AAC " */ - goto fail; - - /* Find the ASC chunk, That's where the goodies are */ - asc_chunk = read_32bitBE(0x40, streamFile); - if (read_32bitBE(asc_chunk, streamFile) != 0x41534320) /* "ASC " */ - goto fail; - - if (read_32bitBE(asc_chunk+0x104, streamFile) != 0xFFFFFFFF) - loop_flag = 1; - else - loop_flag = 0; - - channel_count = read_32bitBE(asc_chunk + 0xF4, streamFile); - codec_id = read_32bitBE(asc_chunk + 0xF0, streamFile); - - /* build the VGMSTREAM */ - vgmstream = allocate_vgmstream(channel_count, loop_flag); - if (!vgmstream) goto fail; - - /* ASC header */ - start_offset = asc_chunk + 0x110; - vgmstream->sample_rate = read_32bitBE(asc_chunk + 0xFC, streamFile); - vgmstream->channels = channel_count; - vgmstream->meta_type = meta_TA_AAC_PS3; - data_size = read_32bitBE(asc_chunk + 0xF8, streamFile); - loop_start = read_32bitBE(asc_chunk + 0x104, streamFile); - loop_end = read_32bitBE(asc_chunk + 0x108, streamFile); - -#ifdef VGM_USE_FFMPEG - { - int block_align, encoder_delay; - - block_align = (codec_id == 4 ? 0x60 : (codec_id == 5 ? 0x98 : 0xC0)) * vgmstream->channels; - encoder_delay = 1024 + 69; /* approximate, gets good loops */ - vgmstream->num_samples = atrac3_bytes_to_samples(data_size, block_align) - encoder_delay; - - vgmstream->codec_data = init_ffmpeg_atrac3_raw(streamFile, start_offset,data_size, vgmstream->num_samples,vgmstream->channels,vgmstream->sample_rate, block_align, encoder_delay); - if (!vgmstream->codec_data) goto fail; - vgmstream->coding_type = coding_FFmpeg; - vgmstream->layout_type = layout_none; - - /* set offset samples (offset 0 jumps to sample 0 > pre-applied delay, and offset end loops after sample end > adjusted delay) */ - vgmstream->loop_start_sample = atrac3_bytes_to_samples(loop_start, block_align); // - encoder_delay - vgmstream->loop_end_sample = atrac3_bytes_to_samples(loop_end, block_align) - encoder_delay; - } -#endif - - /* open the file for reading */ - if (!vgmstream_open_stream(vgmstream, streamFile, start_offset)) - goto fail; - return vgmstream; - -fail: - close_vgmstream(vgmstream); - return NULL; -} - -/* Android/iOS Variants (Star Ocean Anamnesis (APK v1.9.2), Heaven x Inferno (iOS)) */ -VGMSTREAM * init_vgmstream_ta_aac_mobile_vorbis(STREAMFILE *streamFile) { -#ifdef VGM_USE_VORBIS - off_t start_offset; - int8_t codec_id; - - /* check extension, case insensitive */ - /* .aac: expected, .laac/ace: for players to avoid hijacking MP4/AAC */ - if (!check_extensions(streamFile, "aac,laac,ace")) - goto fail; - - if (read_32bitLE(0x00, streamFile) != 0x41414320) /* "AAC " */ - goto fail; - - if (read_32bitLE(0xf0, streamFile) != 0x57415645) /* "WAVE" */ - goto fail; - - codec_id = read_8bit(0x104, streamFile); - if (codec_id == 0xe) /* Vorbis */ - { - ogg_vorbis_meta_info_t ovmi = {0}; - VGMSTREAM * result = NULL; - - ovmi.meta_type = meta_TA_AAC_MOBILE; - ovmi.loop_start = read_32bitLE(0x140, streamFile); - ovmi.loop_end = read_32bitLE(0x144, streamFile); - ovmi.loop_flag = ovmi.loop_end > ovmi.loop_start; - ovmi.loop_end_found = ovmi.loop_flag; - - start_offset = read_32bitLE(0x120, streamFile); - result = init_vgmstream_ogg_vorbis_callbacks(streamFile, NULL, start_offset, &ovmi); - - if (result != NULL) { - return result; - } - } - -fail: - /* clean up anything we may have opened */ -#endif - return NULL; -} - -/* Android/iOS Variants, before they switched to Vorbis (Star Ocean Anamnesis (Android), Heaven x Inferno (iOS)) */ -VGMSTREAM * init_vgmstream_ta_aac_mobile(STREAMFILE *streamFile) { - VGMSTREAM * vgmstream = NULL; - off_t start_offset; - int channel_count, loop_flag, codec; - size_t data_size; - - - /* check extension, case insensitive */ - /* .aac: expected, .laac: for players to avoid hijacking MP4/AAC */ - if (!check_extensions(streamFile, "aac,laac")) - goto fail; - - if (read_32bitLE(0x00, streamFile) != 0x41414320) /* "AAC " */ - goto fail; - - if (read_32bitLE(0xf0, streamFile) != 0x57415645) /* "WAVE" */ - goto fail; - - codec = read_8bit(0x104, streamFile); - channel_count = read_8bit(0x105, streamFile); - /* 0x106: 0x01?, 0x107: 0x10? */ - data_size = read_32bitLE(0x10c, streamFile); /* usable data only, cuts last frame */ - start_offset = read_32bitLE(0x120, streamFile); - /* 0x124: full data size */ - loop_flag = (read_32bitLE(0x134, streamFile) > 0); - - - /* build the VGMSTREAM */ - vgmstream = allocate_vgmstream(channel_count, loop_flag); - if (!vgmstream) goto fail; - - vgmstream->sample_rate = read_32bitLE(0x108, streamFile); - vgmstream->meta_type = meta_TA_AAC_MOBILE; - - switch(codec) { - case 0x0d: - if (read_32bitLE(0x144, streamFile) != 0x40) goto fail; /* frame size */ - if (read_32bitLE(0x148, streamFile) != (0x40-0x04*channel_count)*2 / channel_count) goto fail; /* frame samples */ - if (channel_count > 2) goto fail; /* unknown data layout */ - - vgmstream->coding_type = coding_ASKA; - vgmstream->layout_type = layout_none; - - vgmstream->num_samples = aska_bytes_to_samples(data_size, channel_count); - vgmstream->loop_start_sample = aska_bytes_to_samples(read_32bitLE(0x130, streamFile), channel_count); - vgmstream->loop_end_sample = aska_bytes_to_samples(read_32bitLE(0x134, streamFile), channel_count); - break; - - default: - goto fail; - } - - if (!vgmstream_open_stream(vgmstream,streamFile,start_offset)) - goto fail; - return vgmstream; - -fail: - close_vgmstream(vgmstream); - return NULL; -} - -/* Vita variants [Judas Code (Vita)] */ -VGMSTREAM * init_vgmstream_ta_aac_vita(STREAMFILE *streamFile) { - VGMSTREAM * vgmstream = NULL; - off_t start_offset; - int channel_count, loop_flag; - - - /* check extension, case insensitive */ - /* .aac: expected, .laac: for players to avoid hijacking MP4/AAC */ - if (!check_extensions(streamFile, "aac,laac")) - goto fail; - - if (read_32bitLE(0x00, streamFile) != 0x41414320) /* "AAC " */ - goto fail; - if (read_32bitLE(0x14, streamFile) != 0x56495441) /* "VITA" */ - goto fail; - if (read_32bitLE(0x10d0, streamFile) != 0x57415645) /* "WAVE" */ - goto fail; - - /* there is a bunch of chunks but we simplify */ - - /* 0x10E4: codec 0x08? */ - channel_count = read_8bit(0x10E5, streamFile); - start_offset = read_32bitLE(0x1100, streamFile); - loop_flag = (read_32bitLE(0x1114, streamFile) > 0); - - - /* build the VGMSTREAM */ - vgmstream = allocate_vgmstream(channel_count, loop_flag); - if (!vgmstream) goto fail; - - vgmstream->sample_rate = read_32bitLE(0x10e8, streamFile); - vgmstream->meta_type = meta_TA_AAC_VITA; - -#ifdef VGM_USE_ATRAC9 - { - atrac9_config cfg = {0}; - - cfg.channels = vgmstream->channels; - cfg.encoder_delay = read_32bitLE(0x1124,streamFile); - cfg.config_data = read_32bitBE(0x1128,streamFile); - - vgmstream->codec_data = init_atrac9(&cfg); - if (!vgmstream->codec_data) goto fail; - vgmstream->coding_type = coding_ATRAC9; - vgmstream->layout_type = layout_none; - - vgmstream->num_samples = atrac9_bytes_to_samples(read_32bitLE(0x10EC, streamFile), vgmstream->codec_data); - vgmstream->num_samples -= cfg.encoder_delay; - vgmstream->loop_start_sample = atrac9_bytes_to_samples(read_32bitLE(0x1110, streamFile), vgmstream->codec_data); - vgmstream->loop_end_sample = atrac9_bytes_to_samples(read_32bitLE(0x1114, streamFile), vgmstream->codec_data); - } -#endif - - if (!vgmstream_open_stream(vgmstream,streamFile,start_offset)) - goto fail; - return vgmstream; - -fail: - close_vgmstream(vgmstream); - return NULL; -} +#include "meta.h" +#include "../coding/coding.h" + +/* AAC - tri-Ace (Aska engine) Audio Container */ + +/* Xbox 360 Variants (Star Ocean 4, End of Eternity, Infinite Undiscovery) */ +VGMSTREAM * init_vgmstream_ta_aac_x360(STREAMFILE *streamFile) { + VGMSTREAM * vgmstream = NULL; + off_t start_offset; + int loop_flag, channel_count; + size_t sampleRate, numSamples, startSample, dataSize, blockSize, blockCount; // A mess + + /* check extension, case insensitive */ + /* .aac: expected, .laac/ace: for players to avoid hijacking MP4/AAC */ + if ( !check_extensions(streamFile,"aac,laac,ace")) + goto fail; + + if (read_32bitBE(0x00,streamFile) != 0x41414320) /* "AAC " */ + goto fail; + + /* Ok, let's check what's behind door number 1 */ + if (read_32bitBE(0x1000, streamFile) == 0x41534320) /* "ASC " */ + { + loop_flag = read_32bitBE(0x1118, streamFile); + + /*Funky Channel Count Checking */ + if (read_32bitBE(0x1184, streamFile) == 0x7374726D) + channel_count = 6; + else if (read_32bitBE(0x1154, streamFile) == 0x7374726D) + channel_count = 4; + else + channel_count = read_8bit(0x1134, streamFile); + + sampleRate = read_32bitBE(0x10F4, streamFile); + numSamples = read_32bitBE(0x10FC, streamFile); + startSample = read_32bitBE(0x10F8, streamFile); + dataSize = read_32bitBE(0x10F0, streamFile); + blockSize = read_32bitBE(0x1100, streamFile); + blockCount = read_32bitBE(0x110C, streamFile); + } + else if (read_32bitBE(0x1000, streamFile) == 0x57415645) /* "WAVE" */ + { + loop_flag = read_32bitBE(0x1048, streamFile); + + /*Funky Channel Count Checking */ + if (read_32bitBE(0x10B0, streamFile) == 0x7374726D) + channel_count = 6; + else if (read_32bitBE(0x1080, streamFile) == 0x7374726D) + channel_count = 4; + else + channel_count = read_8bit(0x1060, streamFile); + + sampleRate = read_32bitBE(0x1024, streamFile); + numSamples = read_32bitBE(0x102C, streamFile); + startSample = read_32bitBE(0x1028, streamFile); + dataSize = read_32bitBE(0x1020, streamFile); + blockSize = read_32bitBE(0x1030, streamFile); + blockCount = read_32bitBE(0x103C, streamFile); + } + else if (read_32bitBE(0x1000, streamFile) == 0x00000000) /* some like to be special */ + { + loop_flag = read_32bitBE(0x6048, streamFile); + + /*Funky Channel Count Checking */ + if (read_32bitBE(0x60B0, streamFile) == 0x7374726D) + channel_count = 6; + else if (read_32bitBE(0x6080, streamFile) == 0x7374726D) + channel_count = 4; + else + channel_count = read_8bit(0x6060, streamFile); + + sampleRate = read_32bitBE(0x6024, streamFile); + numSamples = read_32bitBE(0x602C, streamFile); + startSample = read_32bitBE(0x6028, streamFile); + dataSize = read_32bitBE(0x6020, streamFile); + blockSize = read_32bitBE(0x6030, streamFile); + blockCount = read_32bitBE(0x603C, streamFile); + } + else + goto fail; //cuz I don't know if there are other variants + + /* build the VGMSTREAM */ + vgmstream = allocate_vgmstream(channel_count,loop_flag); + if (!vgmstream) goto fail; + + if (read_32bitBE(0x1000, streamFile) == 0x00000000) + start_offset = 0x7000; + else + start_offset = 0x2000; + + vgmstream->sample_rate = sampleRate; + vgmstream->channels = channel_count; + vgmstream->num_samples = numSamples; + if (loop_flag) { + vgmstream->loop_start_sample = startSample; + vgmstream->loop_end_sample = vgmstream->num_samples; + } + vgmstream->meta_type = meta_TA_AAC_X360; + +#ifdef VGM_USE_FFMPEG + { + ffmpeg_codec_data *ffmpeg_data = NULL; + uint8_t buf[100]; + size_t bytes, datasize, block_size, block_count; + + block_count = blockCount; + block_size = blockSize; + datasize = dataSize; + + bytes = ffmpeg_make_riff_xma2(buf,100, vgmstream->num_samples, datasize, vgmstream->channels, vgmstream->sample_rate, block_count, block_size); + ffmpeg_data = init_ffmpeg_header_offset(streamFile, buf,bytes, start_offset,datasize); + if ( !ffmpeg_data ) goto fail; + vgmstream->codec_data = ffmpeg_data; + vgmstream->coding_type = coding_FFmpeg; + vgmstream->layout_type = layout_none; + + xma_fix_raw_samples(vgmstream, streamFile, start_offset, datasize, 0, 1,1); + if (loop_flag) { /* reapply adjusted samples */ + vgmstream->loop_end_sample = vgmstream->num_samples; + } + + } +#else + goto fail; +#endif + + /* open the file for reading */ + if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) ) + goto fail; + return vgmstream; + +fail: + close_vgmstream(vgmstream); + return NULL; +} + +/* PlayStation 3 Variants (Star Ocean International, Resonance of Fate) */ +VGMSTREAM * init_vgmstream_ta_aac_ps3(STREAMFILE *streamFile) { + VGMSTREAM * vgmstream = NULL; + off_t start_offset; + int loop_flag, channel_count; + uint32_t data_size, loop_start, loop_end, codec_id, asc_chunk; + + /* check extension, case insensitive */ + /* .aac: expected, .laac/ace: for players to avoid hijacking MP4/AAC */ + if (!check_extensions(streamFile, "aac,laac,ace")) + goto fail; + + if (read_32bitBE(0x00, streamFile) != 0x41414320) /* "AAC " */ + goto fail; + + /* Find the ASC chunk, That's where the goodies are */ + asc_chunk = read_32bitBE(0x40, streamFile); + if (read_32bitBE(asc_chunk, streamFile) != 0x41534320) /* "ASC " */ + goto fail; + + if (read_32bitBE(asc_chunk+0x104, streamFile) != 0xFFFFFFFF) + loop_flag = 1; + else + loop_flag = 0; + + channel_count = read_32bitBE(asc_chunk + 0xF4, streamFile); + codec_id = read_32bitBE(asc_chunk + 0xF0, streamFile); + + /* build the VGMSTREAM */ + vgmstream = allocate_vgmstream(channel_count, loop_flag); + if (!vgmstream) goto fail; + + /* ASC header */ + start_offset = asc_chunk + 0x110; + vgmstream->sample_rate = read_32bitBE(asc_chunk + 0xFC, streamFile); + vgmstream->channels = channel_count; + vgmstream->meta_type = meta_TA_AAC_PS3; + data_size = read_32bitBE(asc_chunk + 0xF8, streamFile); + loop_start = read_32bitBE(asc_chunk + 0x104, streamFile); + loop_end = read_32bitBE(asc_chunk + 0x108, streamFile); + +#ifdef VGM_USE_FFMPEG + { + int block_align, encoder_delay; + + block_align = (codec_id == 4 ? 0x60 : (codec_id == 5 ? 0x98 : 0xC0)) * vgmstream->channels; + encoder_delay = 1024 + 69; /* approximate, gets good loops */ + vgmstream->num_samples = atrac3_bytes_to_samples(data_size, block_align) - encoder_delay; + + vgmstream->codec_data = init_ffmpeg_atrac3_raw(streamFile, start_offset,data_size, vgmstream->num_samples,vgmstream->channels,vgmstream->sample_rate, block_align, encoder_delay); + if (!vgmstream->codec_data) goto fail; + vgmstream->coding_type = coding_FFmpeg; + vgmstream->layout_type = layout_none; + + /* set offset samples (offset 0 jumps to sample 0 > pre-applied delay, and offset end loops after sample end > adjusted delay) */ + vgmstream->loop_start_sample = atrac3_bytes_to_samples(loop_start, block_align); // - encoder_delay + vgmstream->loop_end_sample = atrac3_bytes_to_samples(loop_end, block_align) - encoder_delay; + } +#endif + + /* open the file for reading */ + if (!vgmstream_open_stream(vgmstream, streamFile, start_offset)) + goto fail; + return vgmstream; + +fail: + close_vgmstream(vgmstream); + return NULL; +} + +/* Android/iOS Variants (Star Ocean Anamnesis (APK v1.9.2), Heaven x Inferno (iOS)) */ +VGMSTREAM * init_vgmstream_ta_aac_mobile_vorbis(STREAMFILE *streamFile) { +#ifdef VGM_USE_VORBIS + off_t start_offset; + int8_t codec_id; + + /* check extension, case insensitive */ + /* .aac: expected, .laac/ace: for players to avoid hijacking MP4/AAC */ + if (!check_extensions(streamFile, "aac,laac,ace")) + goto fail; + + if (read_32bitLE(0x00, streamFile) != 0x41414320) /* "AAC " */ + goto fail; + + if (read_32bitLE(0xf0, streamFile) != 0x57415645) /* "WAVE" */ + goto fail; + + codec_id = read_8bit(0x104, streamFile); + if (codec_id == 0xe) /* Vorbis */ + { + ogg_vorbis_meta_info_t ovmi = {0}; + VGMSTREAM * result = NULL; + + ovmi.meta_type = meta_TA_AAC_MOBILE; + ovmi.loop_start = read_32bitLE(0x140, streamFile); + ovmi.loop_end = read_32bitLE(0x144, streamFile); + ovmi.loop_flag = ovmi.loop_end > ovmi.loop_start; + ovmi.loop_end_found = ovmi.loop_flag; + + start_offset = read_32bitLE(0x120, streamFile); + result = init_vgmstream_ogg_vorbis_callbacks(streamFile, NULL, start_offset, &ovmi); + + if (result != NULL) { + return result; + } + } + +fail: + /* clean up anything we may have opened */ +#endif + return NULL; +} + +/* Android/iOS Variants, before they switched to Vorbis (Star Ocean Anamnesis (Android), Heaven x Inferno (iOS)) */ +VGMSTREAM * init_vgmstream_ta_aac_mobile(STREAMFILE *streamFile) { + VGMSTREAM * vgmstream = NULL; + off_t start_offset; + int channel_count, loop_flag, codec; + size_t data_size; + + + /* check extension, case insensitive */ + /* .aac: expected, .laac: for players to avoid hijacking MP4/AAC */ + if (!check_extensions(streamFile, "aac,laac")) + goto fail; + + if (read_32bitLE(0x00, streamFile) != 0x41414320) /* "AAC " */ + goto fail; + + if (read_32bitLE(0xf0, streamFile) != 0x57415645) /* "WAVE" */ + goto fail; + + codec = read_8bit(0x104, streamFile); + channel_count = read_8bit(0x105, streamFile); + /* 0x106: 0x01?, 0x107: 0x10? */ + data_size = read_32bitLE(0x10c, streamFile); /* usable data only, cuts last frame */ + start_offset = read_32bitLE(0x120, streamFile); + /* 0x124: full data size */ + loop_flag = (read_32bitLE(0x134, streamFile) > 0); + + + /* build the VGMSTREAM */ + vgmstream = allocate_vgmstream(channel_count, loop_flag); + if (!vgmstream) goto fail; + + vgmstream->sample_rate = read_32bitLE(0x108, streamFile); + vgmstream->meta_type = meta_TA_AAC_MOBILE; + + switch(codec) { + case 0x0d: + if (read_32bitLE(0x144, streamFile) != 0x40) goto fail; /* frame size */ + /* 0x148 or 0x150 (later games): frame samples */ + if (channel_count > 2) goto fail; /* unknown data layout */ + + vgmstream->coding_type = coding_ASKA; + vgmstream->layout_type = layout_none; + + vgmstream->num_samples = aska_bytes_to_samples(data_size, channel_count); + vgmstream->loop_start_sample = aska_bytes_to_samples(read_32bitLE(0x130, streamFile), channel_count); + vgmstream->loop_end_sample = aska_bytes_to_samples(read_32bitLE(0x134, streamFile), channel_count); + break; + + default: + goto fail; + } + + if (!vgmstream_open_stream(vgmstream,streamFile,start_offset)) + goto fail; + return vgmstream; + +fail: + close_vgmstream(vgmstream); + return NULL; +} + +/* Vita variants [Judas Code (Vita)] */ +VGMSTREAM * init_vgmstream_ta_aac_vita(STREAMFILE *streamFile) { + VGMSTREAM * vgmstream = NULL; + off_t start_offset; + int channel_count, loop_flag; + + + /* check extension, case insensitive */ + /* .aac: expected, .laac: for players to avoid hijacking MP4/AAC */ + if (!check_extensions(streamFile, "aac,laac")) + goto fail; + + if (read_32bitLE(0x00, streamFile) != 0x41414320) /* "AAC " */ + goto fail; + if (read_32bitLE(0x14, streamFile) != 0x56495441) /* "VITA" */ + goto fail; + if (read_32bitLE(0x10d0, streamFile) != 0x57415645) /* "WAVE" */ + goto fail; + + /* there is a bunch of chunks but we simplify */ + + /* 0x10E4: codec 0x08? */ + channel_count = read_8bit(0x10E5, streamFile); + start_offset = read_32bitLE(0x1100, streamFile); + loop_flag = (read_32bitLE(0x1114, streamFile) > 0); + + + /* build the VGMSTREAM */ + vgmstream = allocate_vgmstream(channel_count, loop_flag); + if (!vgmstream) goto fail; + + vgmstream->sample_rate = read_32bitLE(0x10e8, streamFile); + vgmstream->meta_type = meta_TA_AAC_VITA; + +#ifdef VGM_USE_ATRAC9 + { + atrac9_config cfg = {0}; + + cfg.channels = vgmstream->channels; + cfg.encoder_delay = read_32bitLE(0x1124,streamFile); + cfg.config_data = read_32bitBE(0x1128,streamFile); + + vgmstream->codec_data = init_atrac9(&cfg); + if (!vgmstream->codec_data) goto fail; + vgmstream->coding_type = coding_ATRAC9; + vgmstream->layout_type = layout_none; + + vgmstream->num_samples = atrac9_bytes_to_samples(read_32bitLE(0x10EC, streamFile), vgmstream->codec_data); + vgmstream->num_samples -= cfg.encoder_delay; + vgmstream->loop_start_sample = atrac9_bytes_to_samples(read_32bitLE(0x1110, streamFile), vgmstream->codec_data); + vgmstream->loop_end_sample = atrac9_bytes_to_samples(read_32bitLE(0x1114, streamFile), vgmstream->codec_data); + } +#endif + + if (!vgmstream_open_stream(vgmstream,streamFile,start_offset)) + goto fail; + return vgmstream; + +fail: + close_vgmstream(vgmstream); + return NULL; +} diff --git a/Frameworks/vgmstream/vgmstream/src/meta/ubi_hx.c b/Frameworks/vgmstream/vgmstream/src/meta/ubi_hx.c index 37446233c..627292c5b 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/ubi_hx.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/ubi_hx.c @@ -622,7 +622,7 @@ static VGMSTREAM * init_vgmstream_ubi_hx_header(ubi_hx_header *hx, STREAMFILE *s switch(hx->codec) { case PCM: - vgmstream->coding_type = coding_PCM16LE; + vgmstream->coding_type = hx->big_endian ? coding_PCM16BE : coding_PCM16LE; vgmstream->layout_type = layout_interleave; vgmstream->interleave_block_size = 0x02; diff --git a/Frameworks/vgmstream/vgmstream/src/meta/ubi_sb.c b/Frameworks/vgmstream/vgmstream/src/meta/ubi_sb.c index 699f6031a..5b3b66b0d 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/ubi_sb.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/ubi_sb.c @@ -2966,12 +2966,14 @@ static int config_sb_version(ubi_sb_header * sb, STREAMFILE *streamFile) { } /* TMNT (2007)(X360)-bank 0x00190002 */ + /* My Word Coach (2007)(Wii)-bank 0x00190002 */ /* Prince of Persia: Rival Swords (2007)(Wii)-bank 0x00190003 */ /* Rainbow Six Vegas (2007)(PS3)-bank 0x00190005 */ /* Surf's Up (2007)(PS3)-bank 0x00190005 */ /* Surf's Up (2007)(X360)-bank 0x00190005 */ /* Splinter Cell: Double Agent (2007)(PS3)-map 0x00190005 */ if ((sb->version == 0x00190002 && sb->platform == UBI_X360) || + (sb->version == 0x00190002 && sb->platform == UBI_WII) || (sb->version == 0x00190003 && sb->platform == UBI_WII) || (sb->version == 0x00190005 && sb->platform == UBI_PS3) || (sb->version == 0x00190005 && sb->platform == UBI_X360)) { @@ -3009,6 +3011,16 @@ static int config_sb_version(ubi_sb_header * sb, STREAMFILE *streamFile) { return 1; } + /* Cranium Kabookii (2007)(Wii)-bank 0x001a0003 */ + if (sb->version == 0x001a0003 && sb->platform == UBI_WII) { + config_sb_entry(sb, 0x6c, 0x78); + + config_sb_audio_fs(sb, 0x2c, 0x30, 0x34); + config_sb_audio_he(sb, 0x40, 0x44, 0x4c, 0x54, 0x5c, 0x60); + + return 1; + } + /* Rainbow Six Vegas 2 (2008)(PS3)-bank */ /* Rainbow Six Vegas 2 (2008)(X360)-bank */ if ((sb->version == 0x001C0000 && sb->platform == UBI_PS3) || diff --git a/Frameworks/vgmstream/vgmstream/src/meta/wwise.c b/Frameworks/vgmstream/vgmstream/src/meta/wwise.c index 04393ebc5..687066105 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/wwise.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/wwise.c @@ -1,740 +1,744 @@ -#include "meta.h" -#include "../util.h" -#include "../coding/coding.h" - - -/* Wwise uses a custom RIFF/RIFX header, non-standard enough that it's parsed it here. - * There is some repetition from other metas, but not enough to bother me. - * - * Some info: https://www.audiokinetic.com/en/library/edge/ - */ -typedef enum { PCM, IMA, VORBIS, DSP, XMA2, XWMA, AAC, HEVAG, ATRAC9, OPUSNX, OPUS, PTADPCM } wwise_codec; -typedef struct { - int big_endian; - size_t file_size; - int truncated; - - /* chunks references */ - off_t fmt_offset; - size_t fmt_size; - off_t data_offset; - size_t data_size; - off_t chunk_offset; - - /* standard fmt stuff */ - wwise_codec codec; - int format; - int channels; - int sample_rate; - int block_align; - int average_bps; - int bits_per_sample; - uint32_t channel_layout; - size_t extra_size; - - int32_t num_samples; - int loop_flag; - int32_t loop_start_sample; - int32_t loop_end_sample; -} wwise_header; - - -/* Wwise - Audiokinetic Wwise (Wave Works Interactive Sound Engine) middleware */ -VGMSTREAM * init_vgmstream_wwise(STREAMFILE *streamFile) { - VGMSTREAM * vgmstream = NULL; - wwise_header ww = {0}; - off_t start_offset, first_offset = 0xc; - int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL; - int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL; - - /* basic checks */ - /* .wem (Wwise Encoded Media) is "newer Wwise", used after the 2011.2 SDK (~july) - * .wav (ex. Shadowrun X360) and .ogg (ex. KOF XII X360), .xma (ex. Tron Evolution X360) are used in older Wwise */ - if (!check_extensions(streamFile,"wem,wav,lwav,ogg,logg,xma")) goto fail; - - if ((read_32bitBE(0x00,streamFile) != 0x52494646) && /* "RIFF" (LE) */ - (read_32bitBE(0x00,streamFile) != 0x52494658)) /* "RIFX" (BE) */ - goto fail; - if ((read_32bitBE(0x08,streamFile) != 0x57415645) && /* "WAVE" */ - (read_32bitBE(0x08,streamFile) != 0x58574D41)) /* "XWMA" */ - goto fail; - - - ww.big_endian = read_32bitBE(0x00,streamFile) == 0x52494658;/* RIFX */ - if (ww.big_endian) { /* Wwise honors machine's endianness (PC=RIFF, X360=RIFX --unlike XMA) */ - read_32bit = read_32bitBE; - read_16bit = read_16bitBE; - } else { - read_32bit = read_32bitLE; - read_16bit = read_16bitLE; - } - - ww.file_size = streamFile->get_size(streamFile); - -#if 0 - /* Wwise's RIFF size is often wonky, seemingly depending on codec: - * - PCM, IMA/PTADPCM, VORBIS, AAC, OPUSNX/OPUS: correct - * - DSP, XWMA, ATRAC9: almost always slightly smaller (around 0x50) - * - HEVAG: very off - * - XMA2: exact file size - * - some RIFX have LE size - * (later we'll validate "data" which fortunately is correct) - */ - if (read_32bit(0x04,streamFile)+0x04+0x04 != ww.file_size) { - VGM_LOG("WWISE: bad riff size (real=0x%x vs riff=0x%x)\n", read_32bit(0x04,streamFile)+0x04+0x04, ww.file_size); - goto fail; - } -#endif - - /* ignore LyN RIFF */ - { - off_t fact_offset; - size_t fact_size; - - if (find_chunk(streamFile, 0x66616374,first_offset,0, &fact_offset,&fact_size, 0, 0)) { /* "fact" */ - if (fact_size == 0x10 && read_32bitBE(fact_offset+0x04, streamFile) == 0x4C794E20) /* "LyN " */ - goto fail; /* parsed elsewhere */ - /* Wwise doesn't use "fact", though */ - } - } - - - /* parse format (roughly spec-compliant but some massaging is needed) */ - { - off_t loop_offset; - size_t loop_size; - - /* find basic chunks */ - if (!find_chunk(streamFile, 0x666d7420,first_offset,0, &ww.fmt_offset,&ww.fmt_size, ww.big_endian, 0)) goto fail; /*"fmt "*/ - if (!find_chunk(streamFile, 0x64617461,first_offset,0, &ww.data_offset,&ww.data_size, ww.big_endian, 0)) goto fail; /*"data"*/ - - /* base fmt */ - if (ww.fmt_size < 0x12) goto fail; - ww.format = (uint16_t)read_16bit(ww.fmt_offset+0x00,streamFile); - - if (ww.format == 0x0165) { /* pseudo-XMA2WAVEFORMAT (always "fmt"+"XMA2", unlike .xma that may only have "XMA2") */ - if (!find_chunk(streamFile, 0x584D4132,first_offset,0, &ww.chunk_offset,NULL, ww.big_endian, 0)) - goto fail; - xma2_parse_xma2_chunk(streamFile, ww.chunk_offset,&ww.channels,&ww.sample_rate, &ww.loop_flag, &ww.num_samples, &ww.loop_start_sample, &ww.loop_end_sample); - } - else { /* pseudo-WAVEFORMATEX */ - ww.channels = read_16bit(ww.fmt_offset+0x02,streamFile); - ww.sample_rate = read_32bit(ww.fmt_offset+0x04,streamFile); - ww.average_bps = read_32bit(ww.fmt_offset+0x08,streamFile);/* bytes per sec */ - ww.block_align = (uint16_t)read_16bit(ww.fmt_offset+0x0c,streamFile); - ww.bits_per_sample = (uint16_t)read_16bit(ww.fmt_offset+0x0e,streamFile); - if (ww.fmt_size > 0x10 && ww.format != 0x0165 && ww.format != 0x0166) /* ignore XMAWAVEFORMAT */ - ww.extra_size = (uint16_t)read_16bit(ww.fmt_offset+0x10,streamFile); - if (ww.extra_size >= 0x06) { /* always present (actual RIFFs only have it in WAVEFORMATEXTENSIBLE) */ - /* mostly WAVEFORMATEXTENSIBLE's bitmask (see AkSpeakerConfig.h) */ - ww.channel_layout = read_32bit(ww.fmt_offset+0x14,streamFile); - /* latest games have a pseudo-format instead to handle more cases: - * - 8b: uNumChannels - * - 4b: eConfigType (0=none, 1=standard, 2=ambisonic) - * - 19b: uChannelMask */ - if ((ww.channel_layout & 0xFF) == ww.channels) { - ww.channel_layout = (ww.channel_layout >> 12); - } - } - } - - /* find loop info */ - if (ww.format == 0x0166) { /* XMA2WAVEFORMATEX */ - ww.chunk_offset = ww.fmt_offset; - xma2_parse_fmt_chunk_extra(streamFile, ww.chunk_offset, &ww.loop_flag, &ww.num_samples, &ww.loop_start_sample, &ww.loop_end_sample, ww.big_endian); - } - else if (find_chunk(streamFile, 0x736D706C,first_offset,0, &loop_offset,&loop_size, ww.big_endian, 0)) { /*"smpl", common */ - if (loop_size >= 0x34 - && read_32bit(loop_offset+0x1c, streamFile)==1 /*loop count*/ - && read_32bit(loop_offset+0x24+4, streamFile)==0) { - ww.loop_flag = 1; - ww.loop_start_sample = read_32bit(loop_offset+0x24+0x8, streamFile); - ww.loop_end_sample = read_32bit(loop_offset+0x24+0xc,streamFile); - //todo fix repeat looping - } - } - //else if (find_chunk(streamFile, 0x4C495354,first_offset,0, &loop_offset,&loop_size, ww.big_endian, 0)) { /*"LIST", common */ - // /* usually contains "cue"s with sample positions for events (ex. Platinum Games) but no real looping info */ - //} - - /* other Wwise specific: */ - //"JUNK": optional padding for aligment (0-size JUNK exists too) - //"akd ": seem to store extra info for Wwise editor (wave peaks/loudness/HDR envelope?) - } - - /* format to codec */ - switch(ww.format) { - case 0x0001: ww.codec = PCM; break; /* older Wwise */ - case 0x0002: ww.codec = IMA; break; /* newer Wwise (conflicts with MSADPCM, probably means "platform's ADPCM") */ - //case 0x0011: ww.codec = IMA; break; /* older Wwise (used?) */ - case 0x0069: ww.codec = IMA; break; /* older Wwise (Spiderman Web of Shadows X360, LotR Conquest PC) */ - case 0x0161: ww.codec = XWMA; break; /* WMAv2 */ - case 0x0162: ww.codec = XWMA; break; /* WMAPro */ - case 0x0165: ww.codec = XMA2; break; /* always with the "XMA2" chunk, Wwise doesn't use XMA1 */ - case 0x0166: ww.codec = XMA2; break; - case 0xAAC0: ww.codec = AAC; break; - case 0xFFF0: ww.codec = DSP; break; - case 0xFFFB: ww.codec = HEVAG; break; - case 0xFFFC: ww.codec = ATRAC9; break; - case 0xFFFE: ww.codec = PCM; break; /* "PCM for Wwise Authoring" */ - case 0xFFFF: ww.codec = VORBIS; break; - case 0x3039: ww.codec = OPUSNX; break; /* later renamed from "OPUS" */ - case 0x3040: ww.codec = OPUS; break; -#if 0 - case 0x8311: ww.codec = PTADPCM; break; -#endif - default: - goto fail; - } - - /* identify system's ADPCM */ - if (ww.format == 0x0002) { - if (ww.extra_size == 0x0c + ww.channels * 0x2e) { - /* newer Wwise DSP with coefs [Epic Mickey 2 (Wii), Batman Arkham Origins Blackgate (3DS)] */ - ww.codec = DSP; - } else if (ww.extra_size == 0x0a && find_chunk(streamFile, 0x57696948, first_offset,0, NULL,NULL, ww.big_endian, 0)) { /* WiiH */ - /* few older Wwise DSP with num_samples in extra_size [Tony Hawk: Shred (Wii)] */ - ww.codec = DSP; - } else if (ww.block_align == 0x104 * ww.channels) { - ww.codec = PTADPCM; /* Bayonetta 2 (Switch) */ - } - } - - - /* Some Wwise files (ex. Oddworld PSV, Bayonetta 2 WiiU, often in BGM.bnk) are truncated mirrors of another file. - * They come in RAM banks, prefetch to play the beginning while the rest of the real stream loads. - * We'll add basic support to avoid complaints of this or that .wem not playing */ - if (ww.data_offset + ww.data_size > ww.file_size) { - //VGM_LOG("WWISE: truncated data size (prefetch): (real=0x%x > riff=0x%x)\n", ww.data_size, ww.file_size); - - /* catch wrong rips as truncated tracks' file_size should be much smaller than data_size */ - if (ww.data_offset + ww.data_size - ww.file_size < 0x5000) { - VGM_LOG("WWISE: wrong expected data_size\n"); - goto fail; - } - - if (ww.codec == IMA || ww.codec == VORBIS || ww.codec == XMA2 || ww.codec == OPUSNX) - ww.truncated = 1; /* only seen those, probably all exist */ - else - goto fail; - } - - - start_offset = ww.data_offset; - - /* build the VGMSTREAM */ - vgmstream = allocate_vgmstream(ww.channels,ww.loop_flag); - if (!vgmstream) goto fail; - - vgmstream->sample_rate = ww.sample_rate; - vgmstream->loop_start_sample = ww.loop_start_sample; - vgmstream->loop_end_sample = ww.loop_end_sample; - vgmstream->channel_layout = ww.channel_layout; - vgmstream->meta_type = meta_WWISE_RIFF; - - switch(ww.codec) { - case PCM: /* common */ - /* normally riff.c has priority but it's needed when .wem is used */ - if (ww.fmt_size != 0x10 && ww.fmt_size != 0x18 && ww.fmt_size != 0x28) goto fail; /* old, new/Limbo (PC) */ - if (ww.bits_per_sample != 16) goto fail; - - vgmstream->coding_type = (ww.big_endian ? coding_PCM16BE : coding_PCM16LE); - vgmstream->layout_type = ww.channels > 1 ? layout_interleave : layout_none; - vgmstream->interleave_block_size = 0x02; - - vgmstream->num_samples = pcm_bytes_to_samples(ww.data_size, ww.channels, ww.bits_per_sample); - break; - - case IMA: /* common */ - /* slightly modified XBOX-IMA */ - /* Wwise reuses common codec ids (ex. 0x0002 MSADPCM) for IMA so this parser should go AFTER riff.c avoid misdetection */ - - if (ww.fmt_size != 0x28 && ww.fmt_size != 0x18) goto fail; /* old, new */ - if (ww.bits_per_sample != 4) goto fail; - if (ww.block_align != 0x24 * ww.channels) goto fail; - - vgmstream->coding_type = coding_WWISE_IMA; - vgmstream->layout_type = layout_interleave; - vgmstream->interleave_block_size = ww.block_align / ww.channels; - vgmstream->codec_endian = ww.big_endian; - - /* enough to get real samples */ - if (ww.truncated) { - ww.data_size = ww.file_size - ww.data_offset; - } - - vgmstream->num_samples = xbox_ima_bytes_to_samples(ww.data_size, ww.channels); - break; - -#ifdef VGM_USE_VORBIS - case VORBIS: { /* common */ - /* Wwise uses custom Vorbis, which changed over time (config must be detected to pass to the decoder). */ - off_t vorb_offset, data_offsets, block_offsets; - size_t vorb_size, setup_offset, audio_offset; - vorbis_custom_config cfg = {0}; - - cfg.channels = ww.channels; - cfg.sample_rate = ww.sample_rate; - cfg.big_endian = ww.big_endian; - - if (ww.block_align != 0 || ww.bits_per_sample != 0) goto fail; /* always 0 for Worbis */ - - /* autodetect format (fields are mostly common, see the end of the file) */ - if (find_chunk(streamFile, 0x766F7262,first_offset,0, &vorb_offset,&vorb_size, ww.big_endian, 0)) { /*"vorb"*/ - /* older Wwise (~<2012) */ - - switch(vorb_size) { - case 0x2C: /* earliest (~2009), [UFC Undisputed 2009 (PS3), some EVE Online Apocrypha (PC)?] */ - case 0x28: /* early (~2009) [The Lord of the Rings: Conquest (PC)] */ - data_offsets = 0x18; - block_offsets = 0; /* no need, full headers are present */ - cfg.header_type = WWV_TYPE_8; - cfg.packet_type = WWV_STANDARD; - cfg.setup_type = WWV_HEADER_TRIAD; - break; - - case 0x34: /* common (2010~2011) */ - case 0x32: /* very rare (mid 2011) [Saints Row the 3rd (PC)] */ - data_offsets = 0x18; - block_offsets = 0x30; - cfg.header_type = WWV_TYPE_6; - cfg.packet_type = WWV_STANDARD; - cfg.setup_type = WWV_EXTERNAL_CODEBOOKS; /* setup_type will be corrected later */ - break; - - case 0x2a: /* uncommon (mid 2011) [inFamous 2 (PS3), Captain America: Super Soldier (X360)] */ - data_offsets = 0x10; - block_offsets = 0x28; - cfg.header_type = WWV_TYPE_2; - cfg.packet_type = WWV_MODIFIED; - cfg.setup_type = WWV_EXTERNAL_CODEBOOKS; - break; - - default: - VGM_LOG("WWISE: unknown vorb size 0x%x\n", vorb_size); - goto fail; - } - - vgmstream->num_samples = read_32bit(vorb_offset + 0x00, streamFile); - setup_offset = read_32bit(vorb_offset + data_offsets + 0x00, streamFile); /* within data (0 = no seek table) */ - audio_offset = read_32bit(vorb_offset + data_offsets + 0x04, streamFile); /* within data */ - if (block_offsets) { - cfg.blocksize_1_exp = read_8bit(vorb_offset + block_offsets + 0x00, streamFile); /* small */ - cfg.blocksize_0_exp = read_8bit(vorb_offset + block_offsets + 0x01, streamFile); /* big */ - } - ww.data_size -= audio_offset; - - - /* detect normal packets */ - if (vorb_size == 0x2a) { - /* almost all blocksizes are 0x08+0x0B except a few with 0x0a+0x0a [Captain America: Super Soldier (X360) voices/sfx] */ - if (cfg.blocksize_0_exp == cfg.blocksize_1_exp) - cfg.packet_type = WWV_STANDARD; - } - - /* detect setup type: - * - full inline: ~2009, ex. The King of Fighters XII (X360), The Saboteur (PC) - * - trimmed inline: ~2010, ex. Army of Two: 40 days (X360) some multiplayer files - * - external: ~2010, ex. Assassin's Creed Brotherhood (X360), Dead Nation (X360) */ - if (vorb_size == 0x34) { - size_t setup_size = (uint16_t)read_16bit(start_offset + setup_offset, streamFile); - uint32_t id = (uint32_t)read_32bitBE(start_offset + setup_offset + 0x06, streamFile); - - /* if the setup after header starts with "(data)BCV" it's an inline codebook) */ - if ((id & 0x00FFFFFF) == 0x00424356) { /* 0"BCV" */ - cfg.setup_type = WWV_FULL_SETUP; - } - /* if the setup is suspiciously big it's probably trimmed inline codebooks */ - else if (setup_size > 0x200) { /* an external setup it's ~0x100 max + some threshold */ - cfg.setup_type = WWV_INLINE_CODEBOOKS; - } - } - - vgmstream->codec_data = init_vorbis_custom(streamFile, start_offset + setup_offset, VORBIS_WWISE, &cfg); - if (!vgmstream->codec_data) goto fail; - } - else { - /* newer Wwise (>2012) */ - off_t extra_offset = ww.fmt_offset + 0x18; /* after flag + channels */ - int is_wem = check_extensions(streamFile,"wem"); - - switch(ww.extra_size) { - case 0x30: - data_offsets = 0x10; - block_offsets = 0x28; - cfg.header_type = WWV_TYPE_2; - cfg.packet_type = WWV_MODIFIED; - - /* setup not detectable by header, so we'll try both; hopefully libvorbis will reject wrong codebooks - * - standard: early (<2012), ex. The King of Fighters XIII (X360)-2011/11, .ogg (cbs are from aoTuV, too) - * - aoTuV603: later (>2012), ex. Sonic & All-Stars Racing Transformed (PC)-2012/11, .wem */ - cfg.setup_type = is_wem ? WWV_AOTUV603_CODEBOOKS : WWV_EXTERNAL_CODEBOOKS; /* aoTuV came along .wem */ - break; - - //case 0x2a: /* Rocksmith 2011 (X360)? */ - //non mod packets? TYPE_06? (possibly detectable by checking setup's granule, should be 0) - default: - VGM_LOG("WWISE: unknown extra size 0x%x\n", vorb_size); - goto fail; - } - - vgmstream->num_samples = read_32bit(extra_offset + 0x00, streamFile); - setup_offset = read_32bit(extra_offset + data_offsets + 0x00, streamFile); /* within data */ - audio_offset = read_32bit(extra_offset + data_offsets + 0x04, streamFile); /* within data */ - cfg.blocksize_1_exp = read_8bit(extra_offset + block_offsets + 0x00, streamFile); /* small */ - cfg.blocksize_0_exp = read_8bit(extra_offset + block_offsets + 0x01, streamFile); /* big */ - ww.data_size -= audio_offset; - - /* detect normal packets */ - if (ww.extra_size == 0x30) { - /* almost all blocksizes are 0x08+0x0B except some with 0x09+0x09 [Oddworld New 'n' Tasty! (PSV)] */ - if (cfg.blocksize_0_exp == cfg.blocksize_1_exp) - cfg.packet_type = WWV_STANDARD; - } - - /* try with the selected codebooks */ - vgmstream->codec_data = init_vorbis_custom(streamFile, start_offset + setup_offset, VORBIS_WWISE, &cfg); - if (!vgmstream->codec_data) { - /* codebooks failed: try again with the other type */ - cfg.setup_type = is_wem ? WWV_EXTERNAL_CODEBOOKS : WWV_AOTUV603_CODEBOOKS; - vgmstream->codec_data = init_vorbis_custom(streamFile, start_offset + setup_offset, VORBIS_WWISE, &cfg); - if (!vgmstream->codec_data) goto fail; - } - } - vgmstream->layout_type = layout_none; - vgmstream->coding_type = coding_VORBIS_custom; - vgmstream->codec_endian = ww.big_endian; - - start_offset += audio_offset; - - /* Vorbis is VBR so this is very approximate percent, meh */ - if (ww.truncated) { - vgmstream->num_samples = (int32_t)(vgmstream->num_samples * - (double)(ww.file_size - start_offset) / (double)ww.data_size); - } - - break; - } -#endif - - case DSP: { /* Wii/3DS/WiiU */ - off_t wiih_offset; - size_t wiih_size; - int i; - - //if (ww.fmt_size != 0x28 && ww.fmt_size != ?) goto fail; /* old, new */ - if (ww.bits_per_sample != 4) goto fail; - - vgmstream->coding_type = coding_NGC_DSP; - vgmstream->layout_type = layout_interleave; - vgmstream->interleave_block_size = 0x08; /* ww.block_align = 0x8 in older Wwise, samples per block in newer Wwise */ - - /* find coef position */ - if (find_chunk(streamFile, 0x57696948,first_offset,0, &wiih_offset,&wiih_size, ww.big_endian, 0)) { /*"WiiH", older Wwise */ - vgmstream->num_samples = dsp_bytes_to_samples(ww.data_size, ww.channels); - if (wiih_size != 0x2e * ww.channels) goto fail; - } - else if (ww.extra_size == 0x0c + ww.channels * 0x2e) { /* newer Wwise */ - vgmstream->num_samples = read_32bit(ww.fmt_offset + 0x18, streamFile); - wiih_offset = ww.fmt_offset + 0x1c; - wiih_size = 0x2e * ww.channels; - } - else { - goto fail; - } - - /* for some reason all(?) DSP .wem do full loops (even mono/jingles/etc) but - * several tracks do loop like this, so disable it for short-ish tracks */ - if (ww.loop_flag && vgmstream->loop_start_sample == 0 && - vgmstream->loop_end_sample < 20*ww.sample_rate) { /* in seconds */ - vgmstream->loop_flag = 0; - } - - - - /* get coefs and default history */ - dsp_read_coefs(vgmstream,streamFile,wiih_offset, 0x2e, ww.big_endian); - for (i=0; i < ww.channels; i++) { - vgmstream->ch[i].adpcm_history1_16 = read_16bitBE(wiih_offset + i * 0x2e + 0x24,streamFile); - vgmstream->ch[i].adpcm_history2_16 = read_16bitBE(wiih_offset + i * 0x2e + 0x26,streamFile); - } - - break; - } - -#ifdef VGM_USE_FFMPEG - case XMA2: { /* X360/XBone */ - uint8_t buf[0x100]; - int bytes; - off_t xma2_offset; - size_t xma2_size; - - /* endian check should be enough */ - //if (ww.fmt_size != ...) goto fail; /* XMA1 0x20, XMA2old: 0x34, XMA2new: 0x40, XMA2 Guitar Hero Live/padded: 0x64, etc */ - if (!ww.big_endian) goto fail; /* must be Wwise (real XMA are LE and parsed elsewhere) */ - - if (find_chunk(streamFile, 0x584D4132,first_offset,0, &xma2_offset,&xma2_size, ww.big_endian, 0)) { /*"XMA2"*/ /* older Wwise */ - bytes = ffmpeg_make_riff_xma2_from_xma2_chunk(buf,0x100, xma2_offset, xma2_size, ww.data_size, streamFile); - } else { /* newer Wwise */ - bytes = ffmpeg_make_riff_xma_from_fmt_chunk(buf,0x100, ww.fmt_offset, ww.fmt_size, ww.data_size, streamFile, ww.big_endian); - } - - vgmstream->codec_data = init_ffmpeg_header_offset(streamFile, buf,bytes, ww.data_offset,ww.data_size); - if ( !vgmstream->codec_data ) goto fail; - vgmstream->coding_type = coding_FFmpeg; - vgmstream->layout_type = layout_none; - - vgmstream->num_samples = ww.num_samples; /* set while parsing XMAWAVEFORMATs */ - - /* Wwise loops are always pre-adjusted (old or new) and only num_samples is off */ - xma_fix_raw_samples(vgmstream, streamFile, ww.data_offset,ww.data_size, ww.chunk_offset, 1,0); - - /* "XMAc": rare Wwise extension, XMA2 physical loop regions (loop_start_b, loop_end_b, loop_subframe_data) - * Can appear even in the file doesn't loop, maybe it's meant to be the playable physical region */ - //VGM_ASSERT(find_chunk(streamFile, 0x584D4163,first_offset,0, NULL,NULL, ww.big_endian, 0), "WWISE: XMAc chunk found\n"); - /* other chunks: "seek", regular XMA2 seek table */ - - /* XMA is VBR so this is very approximate percent, meh */ - if (ww.truncated) { - vgmstream->num_samples = (int32_t)(vgmstream->num_samples * - (double)(ww.file_size - start_offset) / (double)ww.data_size); - } - - break; - } - - case XWMA: { /* X360 */ - ffmpeg_codec_data *ffmpeg_data = NULL; - uint8_t buf[0x100]; - int bytes; - - if (ww.fmt_size != 0x18) goto fail; - if (!ww.big_endian) goto fail; /* must be from Wwise X360 (PC LE XWMA is parsed elsewhere) */ - - bytes = ffmpeg_make_riff_xwma(buf,0x100, ww.format, ww.data_size, vgmstream->channels, vgmstream->sample_rate, ww.average_bps, ww.block_align); - ffmpeg_data = init_ffmpeg_header_offset(streamFile, buf,bytes, ww.data_offset,ww.data_size); - if ( !ffmpeg_data ) goto fail; - vgmstream->codec_data = ffmpeg_data; - vgmstream->coding_type = coding_FFmpeg; - vgmstream->layout_type = layout_none; - - - /* manually find total samples, why don't they put this in the header is beyond me */ - { - ms_sample_data msd = {0}; - - msd.channels = ww.channels; - msd.data_offset = ww.data_offset; - msd.data_size = ww.data_size; - - if (ww.format == 0x0162) - wmapro_get_samples(&msd, streamFile, ww.block_align, ww.sample_rate,0x00E0); - else - wma_get_samples(&msd, streamFile, ww.block_align, ww.sample_rate,0x001F); - - vgmstream->num_samples = msd.num_samples; - if (!vgmstream->num_samples) - vgmstream->num_samples = (int32_t)ffmpeg_data->totalSamples; /* very wrong, from avg-br */ - //num_samples seem to be found in the last "seek" table entry too, as: entry / channels / 2 - } - - break; - } - - case AAC: { /* iOS/Mac */ - ffmpeg_codec_data * ffmpeg_data = NULL; - - if (ww.fmt_size != 0x24) goto fail; - if (ww.block_align != 0 || ww.bits_per_sample != 0) goto fail; - - /* extra: size 0x12, unknown values */ - - ffmpeg_data = init_ffmpeg_offset(streamFile, ww.data_offset,ww.data_size); - if (!ffmpeg_data) goto fail; - vgmstream->codec_data = ffmpeg_data; - vgmstream->coding_type = coding_FFmpeg; - vgmstream->layout_type = layout_none; - - vgmstream->num_samples = (int32_t)ffmpeg_data->totalSamples; - break; - } - - case OPUSNX: { /* Switch */ - size_t skip; - - /* values up to 0x14 seem fixed and similar to HEVAG's (block_align 0x02/04, bits_per_sample 0x10) */ - if (ww.fmt_size == 0x28) { - size_t seek_size; - - vgmstream->num_samples += read_32bit(ww.fmt_offset + 0x18, streamFile); - /* 0x1c: null? 0x20: data_size without seek_size */ - seek_size = read_32bit(ww.fmt_offset + 0x24, streamFile); - - start_offset += seek_size; - ww.data_size -= seek_size; - } - else { - goto fail; - } - - skip = switch_opus_get_encoder_delay(start_offset, streamFile); /* should be 120 */ - - /* OPUS is VBR so this is very approximate percent, meh */ - if (ww.truncated) { - vgmstream->num_samples = (int32_t)(vgmstream->num_samples * - (double)(ww.file_size - start_offset) / (double)ww.data_size); - ww.data_size = ww.file_size - start_offset; - } - - vgmstream->codec_data = init_ffmpeg_switch_opus(streamFile, start_offset,ww.data_size, vgmstream->channels, skip, vgmstream->sample_rate); - if (!vgmstream->codec_data) goto fail; - vgmstream->coding_type = coding_FFmpeg; - vgmstream->layout_type = layout_none; - break; - } - - case OPUS: { /* PC/mobile/etc, rare (most games still use Vorbis) [Girl Cafe Gun (Mobile)] */ - if (ww.block_align != 0 || ww.bits_per_sample != 0) goto fail; - - /* extra: size 0x12 */ - vgmstream->num_samples = read_32bit(ww.fmt_offset + 0x18, streamFile); - /* 0x1c: stream size without OggS? */ - /* 0x20: full samples (without encoder delay) */ - - vgmstream->codec_data = init_ffmpeg_offset(streamFile, ww.data_offset,ww.data_size); - if (!vgmstream->codec_data) goto fail; - vgmstream->coding_type = coding_FFmpeg; - vgmstream->layout_type = layout_none; - break; - } - -#endif - case HEVAG: /* PSV */ - /* changed values, another bizarre Wwise quirk */ - //ww.block_align /* unknown (1ch=2, 2ch=4) */ - //ww.bits_per_sample; /* unknown (0x10) */ - //if (ww.bits_per_sample != 4) goto fail; - - if (ww.fmt_size != 0x18) goto fail; - if (ww.big_endian) goto fail; - - /* extra_data: size 0x06, @0x00: samples per block (0x1c), @0x04: channel config */ - - vgmstream->coding_type = coding_HEVAG; - vgmstream->layout_type = layout_interleave; - vgmstream->interleave_block_size = 0x10; - - vgmstream->num_samples = ps_bytes_to_samples(ww.data_size, ww.channels); - break; - -#ifdef VGM_USE_ATRAC9 - case ATRAC9: { /* PSV/PS4 */ - atrac9_config cfg = {0}; - - if (ww.fmt_size != 0x24) goto fail; - if (ww.extra_size != 0x12) goto fail; - - cfg.channels = vgmstream->channels; - cfg.config_data = read_32bitBE(ww.fmt_offset+0x18,streamFile); - cfg.encoder_delay = read_32bit(ww.fmt_offset+0x20,streamFile); - - vgmstream->codec_data = init_atrac9(&cfg); - if (!vgmstream->codec_data) goto fail; - vgmstream->coding_type = coding_ATRAC9; - vgmstream->layout_type = layout_none; - - vgmstream->num_samples = read_32bit(ww.fmt_offset+0x1c,streamFile); - break; - } -#endif - case PTADPCM: /* substitutes IMA as default ADPCM codec */ - if (ww.bits_per_sample != 4) goto fail; - if (ww.block_align != 0x24 * ww.channels && ww.block_align != 0x104 * ww.channels) goto fail; - - vgmstream->coding_type = coding_PTADPCM; - vgmstream->layout_type = layout_interleave; - vgmstream->interleave_block_size = ww.block_align / ww.channels; - //vgmstream->codec_endian = ww.big_endian; //? - - vgmstream->num_samples = ptadpcm_bytes_to_samples(ww.data_size, ww.channels, vgmstream->interleave_block_size); - break; - - default: - goto fail; - } - - - - if ( !vgmstream_open_stream(vgmstream,streamFile,start_offset) ) - goto fail; - return vgmstream; - -fail: - close_vgmstream(vgmstream); - return NULL; -} - - - -/* VORBIS FORMAT RESEARCH */ -/* -- old format -"fmt" size 0x28, extra size 0x16 / size 0x18, extra size 0x06 -0x12 (2): flag? (00,10,18): not related to seek table, codebook type, chunk count, looping -0x14 (4): channel config -0x18-24 (16): ? (fixed: 0x01000000 00001000 800000AA 00389B71) [removed when extra size is 0x06] - -"vorb" size 0x34 -0x00 (4): num_samples -0x04 (4): skip samples? -0x08 (4): ? (small if loop, 0 otherwise) -0x0c (4): data start offset after seek table+setup, or loop start when "smpl" is present -0x10 (4): ? (small, 0..~0x400) -0x14 (4): approximate data size without seek table? (almost setup+packets) -0x18 (4): setup_offset within data (0 = no seek table) -0x1c (4): audio_offset within data -0x20 (2): biggest packet size (not including header)? -0x22 (2): ? (small, N..~0x100) uLastGranuleExtra? -0x24 (4): ? (mid, 0~0x5000) dwDecodeAllocSize? -0x28 (4): ? (mid, 0~0x5000) dwDecodeX64AllocSize? -0x2c (4): parent bank/event id? uHashCodebook? (shared by several .wem a game, but not all need to share it) -0x30 (1): blocksize_1_exp (small) -0x31 (1): blocksize_0_exp (large) -0x32 (2): empty - -"vorb" size 0x28 / 0x2c / 0x2a -0x00 (4): num_samples -0x04 (4): data start offset after seek table+setup, or loop start when "smpl" is present -0x08 (4): data end offset after seek table (setup+packets), or loop end when "smpl" is present -0x0c (2): ? (small, 0..~0x400) [(4) when size is 0x2C] -0x10 (4): setup_offset within data (0 = no seek table) -0x14 (4): audio_offset within data -0x18 (2): biggest packet size (not including header)? -0x1a (2): ? (small, N..~0x100) uLastGranuleExtra? [(4) when size is 0x2C] -0x1c (4): ? (mid, 0~0x5000) dwDecodeAllocSize? -0x20 (4): ? (mid, 0~0x5000) dwDecodeX64AllocSize? -0x24 (4): parent bank/event id? uHashCodebook? (shared by several .wem a game, but not all need to share it) -0x28 (1): blocksize_1_exp (small) [removed when size is 0x28] -0x29 (1): blocksize_0_exp (large) [removed when size is 0x28] - -- new format: -"fmt" size 0x42, extra size 0x30 -0x12 (2): flag? (00,10,18): not related to seek table, codebook type, chunk count, looping, etc -0x14 (4): channel config -0x18 (4): num_samples -0x1c (4): data start offset after seek table+setup, or loop start when "smpl" is present -0x20 (4): data end offset after seek table (setup+packets), or loop end when "smpl" is present -0x24 (2): ?1 (small, 0..~0x400) -0x26 (2): ?2 (small, N..~0x100): not related to seek table, codebook type, chunk count, looping, packet size, samples, etc -0x28 (4): setup offset within data (0 = no seek table) -0x2c (4): audio offset within data -0x30 (2): biggest packet size (not including header) -0x32 (2): (small, 0..~0x100) uLastGranuleExtra? -0x34 (4): ? (mid, 0~0x5000) dwDecodeAllocSize? -0x38 (4): ? (mid, 0~0x5000) dwDecodeX64AllocSize? -0x40 (1): blocksize_1_exp (small) -0x41 (1): blocksize_0_exp (large) - -Wwise encoder options, unknown fields above may be reflect these: - https://www.audiokinetic.com/library/edge/?source=Help&id=vorbis_encoder_parameters -*/ +#include "meta.h" +#include "../util.h" +#include "../coding/coding.h" + + +/* Wwise uses a custom RIFF/RIFX header, non-standard enough that it's parsed it here. + * There is some repetition from other metas, but not enough to bother me. + * + * Some info: https://www.audiokinetic.com/en/library/edge/ + */ +typedef enum { PCM, IMA, VORBIS, DSP, XMA2, XWMA, AAC, HEVAG, ATRAC9, OPUSNX, OPUS, PTADPCM } wwise_codec; +typedef struct { + int big_endian; + size_t file_size; + int truncated; + + /* chunks references */ + off_t fmt_offset; + size_t fmt_size; + off_t data_offset; + size_t data_size; + off_t chunk_offset; + + /* standard fmt stuff */ + wwise_codec codec; + int format; + int channels; + int sample_rate; + int block_align; + int average_bps; + int bits_per_sample; + uint32_t channel_layout; + size_t extra_size; + + int32_t num_samples; + int loop_flag; + int32_t loop_start_sample; + int32_t loop_end_sample; +} wwise_header; + + +/* Wwise - Audiokinetic Wwise (Wave Works Interactive Sound Engine) middleware */ +VGMSTREAM * init_vgmstream_wwise(STREAMFILE *streamFile) { + VGMSTREAM * vgmstream = NULL; + wwise_header ww = {0}; + off_t start_offset, first_offset = 0xc; + int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL; + int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL; + + /* basic checks */ + /* .wem (Wwise Encoded Media) is "newer Wwise", used after the 2011.2 SDK (~july) + * .wav (ex. Shadowrun X360) and .ogg (ex. KOF XII X360), .xma (ex. Tron Evolution X360) are used in older Wwise */ + if (!check_extensions(streamFile,"wem,wav,lwav,ogg,logg,xma")) goto fail; + + if ((read_32bitBE(0x00,streamFile) != 0x52494646) && /* "RIFF" (LE) */ + (read_32bitBE(0x00,streamFile) != 0x52494658)) /* "RIFX" (BE) */ + goto fail; + if ((read_32bitBE(0x08,streamFile) != 0x57415645) && /* "WAVE" */ + (read_32bitBE(0x08,streamFile) != 0x58574D41)) /* "XWMA" */ + goto fail; + + + ww.big_endian = read_32bitBE(0x00,streamFile) == 0x52494658;/* RIFX */ + if (ww.big_endian) { /* Wwise honors machine's endianness (PC=RIFF, X360=RIFX --unlike XMA) */ + read_32bit = read_32bitBE; + read_16bit = read_16bitBE; + } else { + read_32bit = read_32bitLE; + read_16bit = read_16bitLE; + } + + ww.file_size = streamFile->get_size(streamFile); + +#if 0 + /* Wwise's RIFF size is often wonky, seemingly depending on codec: + * - PCM, IMA/PTADPCM, VORBIS, AAC, OPUSNX/OPUS: correct + * - DSP, XWMA, ATRAC9: almost always slightly smaller (around 0x50) + * - HEVAG: very off + * - XMA2: exact file size + * - some RIFX have LE size + * (later we'll validate "data" which fortunately is correct) + */ + if (read_32bit(0x04,streamFile)+0x04+0x04 != ww.file_size) { + VGM_LOG("WWISE: bad riff size (real=0x%x vs riff=0x%x)\n", read_32bit(0x04,streamFile)+0x04+0x04, ww.file_size); + goto fail; + } +#endif + + /* ignore LyN RIFF */ + { + off_t fact_offset; + size_t fact_size; + + if (find_chunk(streamFile, 0x66616374,first_offset,0, &fact_offset,&fact_size, 0, 0)) { /* "fact" */ + if (fact_size == 0x10 && read_32bitBE(fact_offset+0x04, streamFile) == 0x4C794E20) /* "LyN " */ + goto fail; /* parsed elsewhere */ + /* Wwise doesn't use "fact", though */ + } + } + + + /* parse format (roughly spec-compliant but some massaging is needed) */ + { + off_t loop_offset; + size_t loop_size; + + /* find basic chunks */ + if (!find_chunk(streamFile, 0x666d7420,first_offset,0, &ww.fmt_offset,&ww.fmt_size, ww.big_endian, 0)) goto fail; /*"fmt "*/ + if (!find_chunk(streamFile, 0x64617461,first_offset,0, &ww.data_offset,&ww.data_size, ww.big_endian, 0)) goto fail; /*"data"*/ + + /* base fmt */ + if (ww.fmt_size < 0x12) goto fail; + ww.format = (uint16_t)read_16bit(ww.fmt_offset+0x00,streamFile); + + if (ww.format == 0x0165) { /* pseudo-XMA2WAVEFORMAT (always "fmt"+"XMA2", unlike .xma that may only have "XMA2") */ + if (!find_chunk(streamFile, 0x584D4132,first_offset,0, &ww.chunk_offset,NULL, ww.big_endian, 0)) + goto fail; + xma2_parse_xma2_chunk(streamFile, ww.chunk_offset,&ww.channels,&ww.sample_rate, &ww.loop_flag, &ww.num_samples, &ww.loop_start_sample, &ww.loop_end_sample); + } + else { /* pseudo-WAVEFORMATEX */ + ww.channels = read_16bit(ww.fmt_offset+0x02,streamFile); + ww.sample_rate = read_32bit(ww.fmt_offset+0x04,streamFile); + ww.average_bps = read_32bit(ww.fmt_offset+0x08,streamFile);/* bytes per sec */ + ww.block_align = (uint16_t)read_16bit(ww.fmt_offset+0x0c,streamFile); + ww.bits_per_sample = (uint16_t)read_16bit(ww.fmt_offset+0x0e,streamFile); + if (ww.fmt_size > 0x10 && ww.format != 0x0165 && ww.format != 0x0166) /* ignore XMAWAVEFORMAT */ + ww.extra_size = (uint16_t)read_16bit(ww.fmt_offset+0x10,streamFile); + if (ww.extra_size >= 0x06) { /* always present (actual RIFFs only have it in WAVEFORMATEXTENSIBLE) */ + /* mostly WAVEFORMATEXTENSIBLE's bitmask (see AkSpeakerConfig.h) */ + ww.channel_layout = read_32bit(ww.fmt_offset+0x14,streamFile); + /* latest games have a pseudo-format instead to handle more cases: + * - 8b: uNumChannels + * - 4b: eConfigType (0=none, 1=standard, 2=ambisonic) + * - 19b: uChannelMask */ + if ((ww.channel_layout & 0xFF) == ww.channels) { + ww.channel_layout = (ww.channel_layout >> 12); + } + } + } + + /* find loop info */ + if (ww.format == 0x0166) { /* XMA2WAVEFORMATEX */ + ww.chunk_offset = ww.fmt_offset; + xma2_parse_fmt_chunk_extra(streamFile, ww.chunk_offset, &ww.loop_flag, &ww.num_samples, &ww.loop_start_sample, &ww.loop_end_sample, ww.big_endian); + } + else if (find_chunk(streamFile, 0x736D706C,first_offset,0, &loop_offset,&loop_size, ww.big_endian, 0)) { /*"smpl", common */ + if (loop_size >= 0x34 + && read_32bit(loop_offset+0x1c, streamFile)==1 /*loop count*/ + && read_32bit(loop_offset+0x24+4, streamFile)==0) { + ww.loop_flag = 1; + ww.loop_start_sample = read_32bit(loop_offset+0x24+0x8, streamFile); + ww.loop_end_sample = read_32bit(loop_offset+0x24+0xc,streamFile); + //todo fix repeat looping + } + } + //else if (find_chunk(streamFile, 0x4C495354,first_offset,0, &loop_offset,&loop_size, ww.big_endian, 0)) { /*"LIST", common */ + // /* usually contains "cue"s with sample positions for events (ex. Platinum Games) but no real looping info */ + //} + + /* other Wwise specific: */ + //"JUNK": optional padding for aligment (0-size JUNK exists too) + //"akd ": seem to store extra info for Wwise editor (wave peaks/loudness/HDR envelope?) + } + + /* format to codec */ + switch(ww.format) { + case 0x0001: ww.codec = PCM; break; /* older Wwise */ + case 0x0002: ww.codec = IMA; break; /* newer Wwise (conflicts with MSADPCM, probably means "platform's ADPCM") */ + //case 0x0011: ww.codec = IMA; break; /* older Wwise (used?) */ + case 0x0069: ww.codec = IMA; break; /* older Wwise (Spiderman Web of Shadows X360, LotR Conquest PC) */ + case 0x0161: ww.codec = XWMA; break; /* WMAv2 */ + case 0x0162: ww.codec = XWMA; break; /* WMAPro */ + case 0x0165: ww.codec = XMA2; break; /* always with the "XMA2" chunk, Wwise doesn't use XMA1 */ + case 0x0166: ww.codec = XMA2; break; + case 0xAAC0: ww.codec = AAC; break; + case 0xFFF0: ww.codec = DSP; break; + case 0xFFFB: ww.codec = HEVAG; break; + case 0xFFFC: ww.codec = ATRAC9; break; + case 0xFFFE: ww.codec = PCM; break; /* "PCM for Wwise Authoring" */ + case 0xFFFF: ww.codec = VORBIS; break; + case 0x3039: ww.codec = OPUSNX; break; /* later renamed from "OPUS" */ + case 0x3040: ww.codec = OPUS; break; +#if 0 + case 0x8311: ww.codec = PTADPCM; break; +#endif + default: + goto fail; + } + + /* identify system's ADPCM */ + if (ww.format == 0x0002) { + if (ww.extra_size == 0x0c + ww.channels * 0x2e) { + /* newer Wwise DSP with coefs [Epic Mickey 2 (Wii), Batman Arkham Origins Blackgate (3DS)] */ + ww.codec = DSP; + } else if (ww.extra_size == 0x0a && find_chunk(streamFile, 0x57696948, first_offset,0, NULL,NULL, ww.big_endian, 0)) { /* WiiH */ + /* few older Wwise DSP with num_samples in extra_size [Tony Hawk: Shred (Wii)] */ + ww.codec = DSP; + } else if (ww.block_align == 0x104 * ww.channels) { + ww.codec = PTADPCM; /* Bayonetta 2 (Switch) */ + } + } + + + /* Some Wwise files (ex. Oddworld PSV, Bayonetta 2 WiiU, often in BGM.bnk) are truncated mirrors of another file. + * They come in RAM banks, prefetch to play the beginning while the rest of the real stream loads. + * We'll add basic support to avoid complaints of this or that .wem not playing */ + if (ww.data_offset + ww.data_size > ww.file_size) { + //VGM_LOG("WWISE: truncated data size (prefetch): (real=0x%x > riff=0x%x)\n", ww.data_size, ww.file_size); + + /* catch wrong rips as truncated tracks' file_size should be much smaller than data_size */ + if (ww.data_offset + ww.data_size - ww.file_size < 0x5000) { + VGM_LOG("WWISE: wrong expected data_size\n"); + goto fail; + } + + if (ww.codec == PCM || ww.codec == IMA || ww.codec == VORBIS || ww.codec == XMA2 || ww.codec == OPUSNX) + ww.truncated = 1; /* only seen those, probably all exist */ + else + goto fail; + } + + + start_offset = ww.data_offset; + + /* build the VGMSTREAM */ + vgmstream = allocate_vgmstream(ww.channels,ww.loop_flag); + if (!vgmstream) goto fail; + + vgmstream->sample_rate = ww.sample_rate; + vgmstream->loop_start_sample = ww.loop_start_sample; + vgmstream->loop_end_sample = ww.loop_end_sample; + vgmstream->channel_layout = ww.channel_layout; + vgmstream->meta_type = meta_WWISE_RIFF; + + switch(ww.codec) { + case PCM: /* common */ + /* normally riff.c has priority but it's needed when .wem is used */ + if (ww.fmt_size != 0x10 && ww.fmt_size != 0x18 && ww.fmt_size != 0x28) goto fail; /* old, new/Limbo (PC) */ + if (ww.bits_per_sample != 16) goto fail; + + vgmstream->coding_type = (ww.big_endian ? coding_PCM16BE : coding_PCM16LE); + vgmstream->layout_type = ww.channels > 1 ? layout_interleave : layout_none; + vgmstream->interleave_block_size = 0x02; + + if (ww.truncated) { + ww.data_size = ww.file_size - ww.data_offset; + } + + vgmstream->num_samples = pcm_bytes_to_samples(ww.data_size, ww.channels, ww.bits_per_sample); + break; + + case IMA: /* common */ + /* slightly modified XBOX-IMA */ + /* Wwise reuses common codec ids (ex. 0x0002 MSADPCM) for IMA so this parser should go AFTER riff.c avoid misdetection */ + + if (ww.fmt_size != 0x28 && ww.fmt_size != 0x18) goto fail; /* old, new */ + if (ww.bits_per_sample != 4) goto fail; + if (ww.block_align != 0x24 * ww.channels) goto fail; + + vgmstream->coding_type = coding_WWISE_IMA; + vgmstream->layout_type = layout_interleave; + vgmstream->interleave_block_size = ww.block_align / ww.channels; + vgmstream->codec_endian = ww.big_endian; + + /* enough to get real samples */ + if (ww.truncated) { + ww.data_size = ww.file_size - ww.data_offset; + } + + vgmstream->num_samples = xbox_ima_bytes_to_samples(ww.data_size, ww.channels); + break; + +#ifdef VGM_USE_VORBIS + case VORBIS: { /* common */ + /* Wwise uses custom Vorbis, which changed over time (config must be detected to pass to the decoder). */ + off_t vorb_offset, data_offsets, block_offsets; + size_t vorb_size, setup_offset, audio_offset; + vorbis_custom_config cfg = {0}; + + cfg.channels = ww.channels; + cfg.sample_rate = ww.sample_rate; + cfg.big_endian = ww.big_endian; + + if (ww.block_align != 0 || ww.bits_per_sample != 0) goto fail; /* always 0 for Worbis */ + + /* autodetect format (fields are mostly common, see the end of the file) */ + if (find_chunk(streamFile, 0x766F7262,first_offset,0, &vorb_offset,&vorb_size, ww.big_endian, 0)) { /*"vorb"*/ + /* older Wwise (~<2012) */ + + switch(vorb_size) { + case 0x2C: /* earliest (~2009), [UFC Undisputed 2009 (PS3), some EVE Online Apocrypha (PC)?] */ + case 0x28: /* early (~2009) [The Lord of the Rings: Conquest (PC)] */ + data_offsets = 0x18; + block_offsets = 0; /* no need, full headers are present */ + cfg.header_type = WWV_TYPE_8; + cfg.packet_type = WWV_STANDARD; + cfg.setup_type = WWV_HEADER_TRIAD; + break; + + case 0x34: /* common (2010~2011) */ + case 0x32: /* very rare (mid 2011) [Saints Row the 3rd (PC)] */ + data_offsets = 0x18; + block_offsets = 0x30; + cfg.header_type = WWV_TYPE_6; + cfg.packet_type = WWV_STANDARD; + cfg.setup_type = WWV_EXTERNAL_CODEBOOKS; /* setup_type will be corrected later */ + break; + + case 0x2a: /* uncommon (mid 2011) [inFamous 2 (PS3), Captain America: Super Soldier (X360)] */ + data_offsets = 0x10; + block_offsets = 0x28; + cfg.header_type = WWV_TYPE_2; + cfg.packet_type = WWV_MODIFIED; + cfg.setup_type = WWV_EXTERNAL_CODEBOOKS; + break; + + default: + VGM_LOG("WWISE: unknown vorb size 0x%x\n", vorb_size); + goto fail; + } + + vgmstream->num_samples = read_32bit(vorb_offset + 0x00, streamFile); + setup_offset = read_32bit(vorb_offset + data_offsets + 0x00, streamFile); /* within data (0 = no seek table) */ + audio_offset = read_32bit(vorb_offset + data_offsets + 0x04, streamFile); /* within data */ + if (block_offsets) { + cfg.blocksize_1_exp = read_8bit(vorb_offset + block_offsets + 0x00, streamFile); /* small */ + cfg.blocksize_0_exp = read_8bit(vorb_offset + block_offsets + 0x01, streamFile); /* big */ + } + ww.data_size -= audio_offset; + + + /* detect normal packets */ + if (vorb_size == 0x2a) { + /* almost all blocksizes are 0x08+0x0B except a few with 0x0a+0x0a [Captain America: Super Soldier (X360) voices/sfx] */ + if (cfg.blocksize_0_exp == cfg.blocksize_1_exp) + cfg.packet_type = WWV_STANDARD; + } + + /* detect setup type: + * - full inline: ~2009, ex. The King of Fighters XII (X360), The Saboteur (PC) + * - trimmed inline: ~2010, ex. Army of Two: 40 days (X360) some multiplayer files + * - external: ~2010, ex. Assassin's Creed Brotherhood (X360), Dead Nation (X360) */ + if (vorb_size == 0x34) { + size_t setup_size = (uint16_t)read_16bit(start_offset + setup_offset, streamFile); + uint32_t id = (uint32_t)read_32bitBE(start_offset + setup_offset + 0x06, streamFile); + + /* if the setup after header starts with "(data)BCV" it's an inline codebook) */ + if ((id & 0x00FFFFFF) == 0x00424356) { /* 0"BCV" */ + cfg.setup_type = WWV_FULL_SETUP; + } + /* if the setup is suspiciously big it's probably trimmed inline codebooks */ + else if (setup_size > 0x200) { /* an external setup it's ~0x100 max + some threshold */ + cfg.setup_type = WWV_INLINE_CODEBOOKS; + } + } + + vgmstream->codec_data = init_vorbis_custom(streamFile, start_offset + setup_offset, VORBIS_WWISE, &cfg); + if (!vgmstream->codec_data) goto fail; + } + else { + /* newer Wwise (>2012) */ + off_t extra_offset = ww.fmt_offset + 0x18; /* after flag + channels */ + int is_wem = check_extensions(streamFile,"wem"); + + switch(ww.extra_size) { + case 0x30: + data_offsets = 0x10; + block_offsets = 0x28; + cfg.header_type = WWV_TYPE_2; + cfg.packet_type = WWV_MODIFIED; + + /* setup not detectable by header, so we'll try both; hopefully libvorbis will reject wrong codebooks + * - standard: early (<2012), ex. The King of Fighters XIII (X360)-2011/11, .ogg (cbs are from aoTuV, too) + * - aoTuV603: later (>2012), ex. Sonic & All-Stars Racing Transformed (PC)-2012/11, .wem */ + cfg.setup_type = is_wem ? WWV_AOTUV603_CODEBOOKS : WWV_EXTERNAL_CODEBOOKS; /* aoTuV came along .wem */ + break; + + //case 0x2a: /* Rocksmith 2011 (X360)? */ + //non mod packets? TYPE_06? (possibly detectable by checking setup's granule, should be 0) + default: + VGM_LOG("WWISE: unknown extra size 0x%x\n", vorb_size); + goto fail; + } + + vgmstream->num_samples = read_32bit(extra_offset + 0x00, streamFile); + setup_offset = read_32bit(extra_offset + data_offsets + 0x00, streamFile); /* within data */ + audio_offset = read_32bit(extra_offset + data_offsets + 0x04, streamFile); /* within data */ + cfg.blocksize_1_exp = read_8bit(extra_offset + block_offsets + 0x00, streamFile); /* small */ + cfg.blocksize_0_exp = read_8bit(extra_offset + block_offsets + 0x01, streamFile); /* big */ + ww.data_size -= audio_offset; + + /* detect normal packets */ + if (ww.extra_size == 0x30) { + /* almost all blocksizes are 0x08+0x0B except some with 0x09+0x09 [Oddworld New 'n' Tasty! (PSV)] */ + if (cfg.blocksize_0_exp == cfg.blocksize_1_exp) + cfg.packet_type = WWV_STANDARD; + } + + /* try with the selected codebooks */ + vgmstream->codec_data = init_vorbis_custom(streamFile, start_offset + setup_offset, VORBIS_WWISE, &cfg); + if (!vgmstream->codec_data) { + /* codebooks failed: try again with the other type */ + cfg.setup_type = is_wem ? WWV_EXTERNAL_CODEBOOKS : WWV_AOTUV603_CODEBOOKS; + vgmstream->codec_data = init_vorbis_custom(streamFile, start_offset + setup_offset, VORBIS_WWISE, &cfg); + if (!vgmstream->codec_data) goto fail; + } + } + vgmstream->layout_type = layout_none; + vgmstream->coding_type = coding_VORBIS_custom; + vgmstream->codec_endian = ww.big_endian; + + start_offset += audio_offset; + + /* Vorbis is VBR so this is very approximate percent, meh */ + if (ww.truncated) { + vgmstream->num_samples = (int32_t)(vgmstream->num_samples * + (double)(ww.file_size - start_offset) / (double)ww.data_size); + } + + break; + } +#endif + + case DSP: { /* Wii/3DS/WiiU */ + off_t wiih_offset; + size_t wiih_size; + int i; + + //if (ww.fmt_size != 0x28 && ww.fmt_size != ?) goto fail; /* old, new */ + if (ww.bits_per_sample != 4) goto fail; + + vgmstream->coding_type = coding_NGC_DSP; + vgmstream->layout_type = layout_interleave; + vgmstream->interleave_block_size = 0x08; /* ww.block_align = 0x8 in older Wwise, samples per block in newer Wwise */ + + /* find coef position */ + if (find_chunk(streamFile, 0x57696948,first_offset,0, &wiih_offset,&wiih_size, ww.big_endian, 0)) { /*"WiiH", older Wwise */ + vgmstream->num_samples = dsp_bytes_to_samples(ww.data_size, ww.channels); + if (wiih_size != 0x2e * ww.channels) goto fail; + } + else if (ww.extra_size == 0x0c + ww.channels * 0x2e) { /* newer Wwise */ + vgmstream->num_samples = read_32bit(ww.fmt_offset + 0x18, streamFile); + wiih_offset = ww.fmt_offset + 0x1c; + wiih_size = 0x2e * ww.channels; + } + else { + goto fail; + } + + /* for some reason all(?) DSP .wem do full loops (even mono/jingles/etc) but + * several tracks do loop like this, so disable it for short-ish tracks */ + if (ww.loop_flag && vgmstream->loop_start_sample == 0 && + vgmstream->loop_end_sample < 20*ww.sample_rate) { /* in seconds */ + vgmstream->loop_flag = 0; + } + + + + /* get coefs and default history */ + dsp_read_coefs(vgmstream,streamFile,wiih_offset, 0x2e, ww.big_endian); + for (i=0; i < ww.channels; i++) { + vgmstream->ch[i].adpcm_history1_16 = read_16bitBE(wiih_offset + i * 0x2e + 0x24,streamFile); + vgmstream->ch[i].adpcm_history2_16 = read_16bitBE(wiih_offset + i * 0x2e + 0x26,streamFile); + } + + break; + } + +#ifdef VGM_USE_FFMPEG + case XMA2: { /* X360/XBone */ + uint8_t buf[0x100]; + int bytes; + off_t xma2_offset; + size_t xma2_size; + + /* endian check should be enough */ + //if (ww.fmt_size != ...) goto fail; /* XMA1 0x20, XMA2old: 0x34, XMA2new: 0x40, XMA2 Guitar Hero Live/padded: 0x64, etc */ + if (!ww.big_endian) goto fail; /* must be Wwise (real XMA are LE and parsed elsewhere) */ + + if (find_chunk(streamFile, 0x584D4132,first_offset,0, &xma2_offset,&xma2_size, ww.big_endian, 0)) { /*"XMA2"*/ /* older Wwise */ + bytes = ffmpeg_make_riff_xma2_from_xma2_chunk(buf,0x100, xma2_offset, xma2_size, ww.data_size, streamFile); + } else { /* newer Wwise */ + bytes = ffmpeg_make_riff_xma_from_fmt_chunk(buf,0x100, ww.fmt_offset, ww.fmt_size, ww.data_size, streamFile, ww.big_endian); + } + + vgmstream->codec_data = init_ffmpeg_header_offset(streamFile, buf,bytes, ww.data_offset,ww.data_size); + if ( !vgmstream->codec_data ) goto fail; + vgmstream->coding_type = coding_FFmpeg; + vgmstream->layout_type = layout_none; + + vgmstream->num_samples = ww.num_samples; /* set while parsing XMAWAVEFORMATs */ + + /* Wwise loops are always pre-adjusted (old or new) and only num_samples is off */ + xma_fix_raw_samples(vgmstream, streamFile, ww.data_offset,ww.data_size, ww.chunk_offset, 1,0); + + /* "XMAc": rare Wwise extension, XMA2 physical loop regions (loop_start_b, loop_end_b, loop_subframe_data) + * Can appear even in the file doesn't loop, maybe it's meant to be the playable physical region */ + //VGM_ASSERT(find_chunk(streamFile, 0x584D4163,first_offset,0, NULL,NULL, ww.big_endian, 0), "WWISE: XMAc chunk found\n"); + /* other chunks: "seek", regular XMA2 seek table */ + + /* XMA is VBR so this is very approximate percent, meh */ + if (ww.truncated) { + vgmstream->num_samples = (int32_t)(vgmstream->num_samples * + (double)(ww.file_size - start_offset) / (double)ww.data_size); + } + + break; + } + + case XWMA: { /* X360 */ + ffmpeg_codec_data *ffmpeg_data = NULL; + uint8_t buf[0x100]; + int bytes; + + if (ww.fmt_size != 0x18) goto fail; + if (!ww.big_endian) goto fail; /* must be from Wwise X360 (PC LE XWMA is parsed elsewhere) */ + + bytes = ffmpeg_make_riff_xwma(buf,0x100, ww.format, ww.data_size, vgmstream->channels, vgmstream->sample_rate, ww.average_bps, ww.block_align); + ffmpeg_data = init_ffmpeg_header_offset(streamFile, buf,bytes, ww.data_offset,ww.data_size); + if ( !ffmpeg_data ) goto fail; + vgmstream->codec_data = ffmpeg_data; + vgmstream->coding_type = coding_FFmpeg; + vgmstream->layout_type = layout_none; + + + /* manually find total samples, why don't they put this in the header is beyond me */ + { + ms_sample_data msd = {0}; + + msd.channels = ww.channels; + msd.data_offset = ww.data_offset; + msd.data_size = ww.data_size; + + if (ww.format == 0x0162) + wmapro_get_samples(&msd, streamFile, ww.block_align, ww.sample_rate,0x00E0); + else + wma_get_samples(&msd, streamFile, ww.block_align, ww.sample_rate,0x001F); + + vgmstream->num_samples = msd.num_samples; + if (!vgmstream->num_samples) + vgmstream->num_samples = (int32_t)ffmpeg_data->totalSamples; /* very wrong, from avg-br */ + //num_samples seem to be found in the last "seek" table entry too, as: entry / channels / 2 + } + + break; + } + + case AAC: { /* iOS/Mac */ + ffmpeg_codec_data * ffmpeg_data = NULL; + + if (ww.fmt_size != 0x24) goto fail; + if (ww.block_align != 0 || ww.bits_per_sample != 0) goto fail; + + /* extra: size 0x12, unknown values */ + + ffmpeg_data = init_ffmpeg_offset(streamFile, ww.data_offset,ww.data_size); + if (!ffmpeg_data) goto fail; + vgmstream->codec_data = ffmpeg_data; + vgmstream->coding_type = coding_FFmpeg; + vgmstream->layout_type = layout_none; + + vgmstream->num_samples = (int32_t)ffmpeg_data->totalSamples; + break; + } + + case OPUSNX: { /* Switch */ + size_t skip; + + /* values up to 0x14 seem fixed and similar to HEVAG's (block_align 0x02/04, bits_per_sample 0x10) */ + if (ww.fmt_size == 0x28) { + size_t seek_size; + + vgmstream->num_samples += read_32bit(ww.fmt_offset + 0x18, streamFile); + /* 0x1c: null? 0x20: data_size without seek_size */ + seek_size = read_32bit(ww.fmt_offset + 0x24, streamFile); + + start_offset += seek_size; + ww.data_size -= seek_size; + } + else { + goto fail; + } + + skip = switch_opus_get_encoder_delay(start_offset, streamFile); /* should be 120 */ + + /* OPUS is VBR so this is very approximate percent, meh */ + if (ww.truncated) { + vgmstream->num_samples = (int32_t)(vgmstream->num_samples * + (double)(ww.file_size - start_offset) / (double)ww.data_size); + ww.data_size = ww.file_size - start_offset; + } + + vgmstream->codec_data = init_ffmpeg_switch_opus(streamFile, start_offset,ww.data_size, vgmstream->channels, skip, vgmstream->sample_rate); + if (!vgmstream->codec_data) goto fail; + vgmstream->coding_type = coding_FFmpeg; + vgmstream->layout_type = layout_none; + break; + } + + case OPUS: { /* PC/mobile/etc, rare (most games still use Vorbis) [Girl Cafe Gun (Mobile)] */ + if (ww.block_align != 0 || ww.bits_per_sample != 0) goto fail; + + /* extra: size 0x12 */ + vgmstream->num_samples = read_32bit(ww.fmt_offset + 0x18, streamFile); + /* 0x1c: stream size without OggS? */ + /* 0x20: full samples (without encoder delay) */ + + vgmstream->codec_data = init_ffmpeg_offset(streamFile, ww.data_offset,ww.data_size); + if (!vgmstream->codec_data) goto fail; + vgmstream->coding_type = coding_FFmpeg; + vgmstream->layout_type = layout_none; + break; + } + +#endif + case HEVAG: /* PSV */ + /* changed values, another bizarre Wwise quirk */ + //ww.block_align /* unknown (1ch=2, 2ch=4) */ + //ww.bits_per_sample; /* unknown (0x10) */ + //if (ww.bits_per_sample != 4) goto fail; + + if (ww.fmt_size != 0x18) goto fail; + if (ww.big_endian) goto fail; + + /* extra_data: size 0x06, @0x00: samples per block (0x1c), @0x04: channel config */ + + vgmstream->coding_type = coding_HEVAG; + vgmstream->layout_type = layout_interleave; + vgmstream->interleave_block_size = 0x10; + + vgmstream->num_samples = ps_bytes_to_samples(ww.data_size, ww.channels); + break; + +#ifdef VGM_USE_ATRAC9 + case ATRAC9: { /* PSV/PS4 */ + atrac9_config cfg = {0}; + + if (ww.fmt_size != 0x24) goto fail; + if (ww.extra_size != 0x12) goto fail; + + cfg.channels = vgmstream->channels; + cfg.config_data = read_32bitBE(ww.fmt_offset+0x18,streamFile); + cfg.encoder_delay = read_32bit(ww.fmt_offset+0x20,streamFile); + + vgmstream->codec_data = init_atrac9(&cfg); + if (!vgmstream->codec_data) goto fail; + vgmstream->coding_type = coding_ATRAC9; + vgmstream->layout_type = layout_none; + + vgmstream->num_samples = read_32bit(ww.fmt_offset+0x1c,streamFile); + break; + } +#endif + case PTADPCM: /* substitutes IMA as default ADPCM codec */ + if (ww.bits_per_sample != 4) goto fail; + if (ww.block_align != 0x24 * ww.channels && ww.block_align != 0x104 * ww.channels) goto fail; + + vgmstream->coding_type = coding_PTADPCM; + vgmstream->layout_type = layout_interleave; + vgmstream->interleave_block_size = ww.block_align / ww.channels; + //vgmstream->codec_endian = ww.big_endian; //? + + vgmstream->num_samples = ptadpcm_bytes_to_samples(ww.data_size, ww.channels, vgmstream->interleave_block_size); + break; + + default: + goto fail; + } + + + + if ( !vgmstream_open_stream(vgmstream,streamFile,start_offset) ) + goto fail; + return vgmstream; + +fail: + close_vgmstream(vgmstream); + return NULL; +} + + + +/* VORBIS FORMAT RESEARCH */ +/* +- old format +"fmt" size 0x28, extra size 0x16 / size 0x18, extra size 0x06 +0x12 (2): flag? (00,10,18): not related to seek table, codebook type, chunk count, looping +0x14 (4): channel config +0x18-24 (16): ? (fixed: 0x01000000 00001000 800000AA 00389B71) [removed when extra size is 0x06] + +"vorb" size 0x34 +0x00 (4): num_samples +0x04 (4): skip samples? +0x08 (4): ? (small if loop, 0 otherwise) +0x0c (4): data start offset after seek table+setup, or loop start when "smpl" is present +0x10 (4): ? (small, 0..~0x400) +0x14 (4): approximate data size without seek table? (almost setup+packets) +0x18 (4): setup_offset within data (0 = no seek table) +0x1c (4): audio_offset within data +0x20 (2): biggest packet size (not including header)? +0x22 (2): ? (small, N..~0x100) uLastGranuleExtra? +0x24 (4): ? (mid, 0~0x5000) dwDecodeAllocSize? +0x28 (4): ? (mid, 0~0x5000) dwDecodeX64AllocSize? +0x2c (4): parent bank/event id? uHashCodebook? (shared by several .wem a game, but not all need to share it) +0x30 (1): blocksize_1_exp (small) +0x31 (1): blocksize_0_exp (large) +0x32 (2): empty + +"vorb" size 0x28 / 0x2c / 0x2a +0x00 (4): num_samples +0x04 (4): data start offset after seek table+setup, or loop start when "smpl" is present +0x08 (4): data end offset after seek table (setup+packets), or loop end when "smpl" is present +0x0c (2): ? (small, 0..~0x400) [(4) when size is 0x2C] +0x10 (4): setup_offset within data (0 = no seek table) +0x14 (4): audio_offset within data +0x18 (2): biggest packet size (not including header)? +0x1a (2): ? (small, N..~0x100) uLastGranuleExtra? [(4) when size is 0x2C] +0x1c (4): ? (mid, 0~0x5000) dwDecodeAllocSize? +0x20 (4): ? (mid, 0~0x5000) dwDecodeX64AllocSize? +0x24 (4): parent bank/event id? uHashCodebook? (shared by several .wem a game, but not all need to share it) +0x28 (1): blocksize_1_exp (small) [removed when size is 0x28] +0x29 (1): blocksize_0_exp (large) [removed when size is 0x28] + +- new format: +"fmt" size 0x42, extra size 0x30 +0x12 (2): flag? (00,10,18): not related to seek table, codebook type, chunk count, looping, etc +0x14 (4): channel config +0x18 (4): num_samples +0x1c (4): data start offset after seek table+setup, or loop start when "smpl" is present +0x20 (4): data end offset after seek table (setup+packets), or loop end when "smpl" is present +0x24 (2): ?1 (small, 0..~0x400) +0x26 (2): ?2 (small, N..~0x100): not related to seek table, codebook type, chunk count, looping, packet size, samples, etc +0x28 (4): setup offset within data (0 = no seek table) +0x2c (4): audio offset within data +0x30 (2): biggest packet size (not including header) +0x32 (2): (small, 0..~0x100) uLastGranuleExtra? +0x34 (4): ? (mid, 0~0x5000) dwDecodeAllocSize? +0x38 (4): ? (mid, 0~0x5000) dwDecodeX64AllocSize? +0x40 (1): blocksize_1_exp (small) +0x41 (1): blocksize_0_exp (large) + +Wwise encoder options, unknown fields above may be reflect these: + https://www.audiokinetic.com/library/edge/?source=Help&id=vorbis_encoder_parameters +*/ diff --git a/Frameworks/vgmstream/vgmstream/src/meta/xvag.c b/Frameworks/vgmstream/vgmstream/src/meta/xvag.c index 4cf0247f9..7bf942902 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/xvag.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/xvag.c @@ -1,324 +1,326 @@ -#include "meta.h" -#include "../coding/coding.h" -#include "../layout/layout.h" -#include "xvag_streamfile.h" - - -typedef struct { - int big_endian; - int channels; - int sample_rate; - int codec; - - int factor; - - int loop_flag; - int num_samples; - int loop_start; - int loop_end; - - int subsongs; - int layers; - - size_t data_size; - off_t stream_offset; -} xvag_header; - -static int init_xvag_atrac9(STREAMFILE *streamFile, VGMSTREAM* vgmstream, xvag_header * xvag, off_t chunk_offset); -static layered_layout_data* build_layered_xvag(STREAMFILE *streamFile, xvag_header * xvag, off_t chunk_offset, off_t start_offset); - -/* XVAG - Sony's Scream Tool/Stream Creator format (God of War III, Ratchet & Clank Future, The Last of Us, Uncharted) */ -VGMSTREAM * init_vgmstream_xvag(STREAMFILE *streamFile) { - VGMSTREAM * vgmstream = NULL; - STREAMFILE* temp_streamFile = NULL; - xvag_header xvag = {0}; - int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL; - off_t start_offset, chunk_offset, first_offset = 0x20; - size_t chunk_size; - int total_subsongs = 0, target_subsong = streamFile->stream_index; - - - /* checks */ - /* .xvag: standard - * (extensionless): The Last Of Us (PS3) speech files */ - if (!check_extensions(streamFile,"xvag,")) - goto fail; - if (read_32bitBE(0x00,streamFile) != 0x58564147) /* "XVAG" */ - goto fail; - - /* endian flag (XVAGs of the same game can use BE or LE, usually when reusing from other platforms) */ - xvag.big_endian = read_8bit(0x08,streamFile) & 0x01; - if (xvag.big_endian) { - read_32bit = read_32bitBE; - } else { - read_32bit = read_32bitLE; - } - - start_offset = read_32bit(0x04,streamFile); - /* 0x08: flags? (&0x01=big endian, 0x02=?, 0x06=full RIFF AT9?) - * 0x09: flags2? (0x00/0x01/0x04, speaker mode?) - * 0x0a: always 0? - * 0x0b: version-flag? (0x5f/0x60/0x61/0x62/etc) */ - - - /* "fmat": base format (always first) */ - if (!find_chunk(streamFile, 0x666D6174,first_offset,0, &chunk_offset,&chunk_size, xvag.big_endian, 1)) /*"fmat"*/ - goto fail; - xvag.channels = read_32bit(chunk_offset+0x00,streamFile); - xvag.codec = read_32bit(chunk_offset+0x04,streamFile); - xvag.num_samples = read_32bit(chunk_offset+0x08,streamFile); - /* 0x0c: samples again? */ - VGM_ASSERT(xvag.num_samples != read_32bit(chunk_offset+0x0c,streamFile), "XVAG: num_samples values don't match\n"); - - xvag.factor = read_32bit(chunk_offset+0x10,streamFile); /* for interleave */ - xvag.sample_rate = read_32bit(chunk_offset+0x14,streamFile); - xvag.data_size = read_32bit(chunk_offset+0x18,streamFile); /* not always accurate */ - - /* extra data, seen in versions 0x61+ */ - if (chunk_size > 0x1c) { - /* number of interleaved subsongs */ - xvag.subsongs = read_32bit(chunk_offset+0x1c,streamFile); - /* number of interleaved layers (layers * channels_per_layer = channels) */ - xvag.layers = read_32bit(chunk_offset+0x20,streamFile); - } - else { - xvag.subsongs = 1; - xvag.layers = 1; - } - - total_subsongs = xvag.subsongs; - if (target_subsong == 0) target_subsong = 1; - if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail; - - - /* other chunks: */ - /* "cpan": pan/volume per channel */ - /* "cues": cue/labels (rare) */ - /* "md5 ": hash (rare) */ - /* "0000": end chunk before start_offset */ - - /* XVAG has no looping, but some PS3 PS-ADPCM seems to do full loops (without data flags) */ - if (xvag.codec == 0x06 && xvag.subsongs == 1) { - size_t file_size = get_streamfile_size(streamFile); - /* simply test if last frame is not empty = may loop */ - xvag.loop_flag = (read_8bit(file_size - 0x01, streamFile) != 0); - xvag.loop_start = 0; - xvag.loop_end = ps_bytes_to_samples(file_size - start_offset, xvag.channels); - } - - /* May use 'MP3 Surround' for multichannel [Twisted Metal (PS3), The Last of Us (PS4) test file] - * It's a mutant MP3 that decodes as 2ch but output can be routed to 6ch somehow, if manually - * activated. Fraunhofer IIS's MP3sPlayer can do it, as can PS3 (fw v2.40+) but no others seems to. - * So simply play as 2ch, they sound ok with slightly wider feel. No XVAG/MP3 flag exists to detect, - * can be found in v0x60 (without layers/subsongs) and v0x61 (with them set as 1) */ - if (xvag.codec == 0x08 && xvag.channels == 6 && xvag.layers == 1) { - xvag.channels = 2; - } - - - /* build the VGMSTREAM */ - vgmstream = allocate_vgmstream(xvag.channels,xvag.loop_flag); - if (!vgmstream) goto fail; - - vgmstream->meta_type = meta_XVAG; - vgmstream->sample_rate = xvag.sample_rate; - vgmstream->num_samples = xvag.num_samples; - if (xvag.loop_flag) { - vgmstream->loop_start_sample = xvag.loop_start; - vgmstream->loop_end_sample = xvag.loop_end; - } - vgmstream->num_streams = total_subsongs; - vgmstream->stream_size = (xvag.data_size / total_subsongs); - - switch (xvag.codec) { - case 0x06: /* VAG (PS-ADPCM): God of War III (PS3), Uncharted 1/2 (PS3), Ratchet and Clank Future (PS3) */ - case 0x07: /* SVAG? (PS-ADPCM with extended table?): inFamous 1 (PS3) */ - if (xvag.subsongs > 1 && xvag.layers > 1) goto fail; - if (xvag.layers > 1 && xvag.layers != xvag.channels) goto fail; - if (xvag.subsongs > 1 && xvag.channels > 1) goto fail; /* unknown layout */ - - vgmstream->coding_type = coding_PSX; - - if (xvag.subsongs > 1) { /* God of War 3 (PS4) */ - vgmstream->layout_type = layout_blocked_xvag_subsong; - vgmstream->interleave_block_size = 0x10; - vgmstream->full_block_size = 0x10 * xvag.factor * xvag.subsongs; - vgmstream->current_block_size = 0x10 * xvag.factor; - start_offset += vgmstream->current_block_size * (target_subsong-1); - } - else { - vgmstream->layout_type = layout_interleave; - vgmstream->interleave_block_size = 0x10 * xvag.factor; /* usually 1, bigger in GoW3 PS4 */ - } - break; - -#ifdef VGM_USE_MPEG - case 0x08: { /* MPEG: The Last of Us (PS3), Uncharted 3 (PS3), Medieval Moves (PS3) */ - mpeg_custom_config cfg = {0}; - - /* often 2ch per MPEG and rarely 1ch (GoW3 PS4) */ - if (xvag.layers > 1 && !(xvag.layers*1 == vgmstream->channels || xvag.layers*2 == vgmstream->channels)) goto fail; - - /* "mpin": mpeg info */ - if (!find_chunk(streamFile, 0x6D70696E,first_offset,0, &chunk_offset,NULL, xvag.big_endian, 1)) /*"mpin"*/ - goto fail; - - /* all layers/subsongs share the same config; not very useful but for posterity: - * - 0x00: mpeg version - * - 0x04: mpeg layer - * - 0x08: bit rate - * - 0x0c: sample rate - * - 0x10: some version? (0x01-0x03)? - * - 0x14: channels per stream? - * - 0x18: channels per stream or total channels? - * - 0x1c: fixed frame size (always CBR) - * - 0x20: encoder delay (usually but not always 1201) - * - 0x24: number of samples - * - 0x28: some size? - * - 0x2c: ? (0x02) - * - 0x30: ? (0x00, 0x80) - * - 0x34: data size - * (rest is padding) - * */ - cfg.chunk_size = read_32bit(chunk_offset+0x1c,streamFile); - cfg.skip_samples = read_32bit(chunk_offset+0x20,streamFile); - cfg.interleave = cfg.chunk_size * xvag.factor; - - vgmstream->codec_data = init_mpeg_custom(streamFile, start_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_XVAG, &cfg); - if (!vgmstream->codec_data) goto fail; - vgmstream->layout_type = layout_none; - - /* interleaved subsongs, rarely [Sly Cooper: Thieves in Time (PS3)] */ - if (xvag.subsongs > 1) { - temp_streamFile = setup_xvag_streamfile(streamFile, start_offset, cfg.interleave,cfg.chunk_size, (target_subsong-1), total_subsongs); - if (!temp_streamFile) goto fail; - start_offset = 0; - } - - break; - } -#endif - -#ifdef VGM_USE_ATRAC9 - case 0x09: { /* ATRAC9: Sly Cooper and the Thievius Raccoonus (Vita), The Last of Us Remastered (PS4) */ - if (xvag.subsongs > 1 && xvag.layers > 1) goto fail; - - /* "a9in": ATRAC9 info */ - /* 0x00: frame size, 0x04: samples per frame, 0x0c: fact num_samples (no change), 0x10: encoder delay1 */ - if (!find_chunk(streamFile, 0x6139696E,first_offset,0, &chunk_offset,NULL, xvag.big_endian, 1)) /*"a9in"*/ - goto fail; - - if (xvag.layers > 1) { - /* some Vita/PS4 multichannel [flower (Vita), Uncharted Collection (PS4)]. PS4 ATRAC9 also - * does single-stream >2ch, but this can do configs ATRAC9 can't, like 5ch/14ch/etc */ - vgmstream->layout_data = build_layered_xvag(streamFile, &xvag, chunk_offset, start_offset); - if (!vgmstream->layout_data) goto fail; - vgmstream->coding_type = coding_ATRAC9; - vgmstream->layout_type = layout_layered; - - break; - } - else { - /* interleaved subsongs (section layers) */ - size_t frame_size = read_32bit(chunk_offset+0x00,streamFile); - - if (!init_xvag_atrac9(streamFile, vgmstream, &xvag, chunk_offset)) - goto fail; - temp_streamFile = setup_xvag_streamfile(streamFile, start_offset, frame_size*xvag.factor,frame_size, (target_subsong-1), total_subsongs); - if (!temp_streamFile) goto fail; - start_offset = 0; - } - - break; - } -#endif - - default: - goto fail; - } - - - if (!vgmstream_open_stream(vgmstream,temp_streamFile ? temp_streamFile : streamFile,start_offset)) - goto fail; - close_streamfile(temp_streamFile); - return vgmstream; - -fail: - close_streamfile(temp_streamFile); - close_vgmstream(vgmstream); - return NULL; -} - -#ifdef VGM_USE_ATRAC9 -static int init_xvag_atrac9(STREAMFILE *streamFile, VGMSTREAM* vgmstream, xvag_header * xvag, off_t chunk_offset) { - int32_t (*read_32bit)(off_t,STREAMFILE*) = xvag->big_endian ? read_32bitBE : read_32bitLE; - atrac9_config cfg = {0}; - - cfg.channels = vgmstream->channels; - cfg.config_data = read_32bitBE(chunk_offset+0x08,streamFile); - cfg.encoder_delay = read_32bit(chunk_offset+0x14,streamFile); - - vgmstream->codec_data = init_atrac9(&cfg); - if (!vgmstream->codec_data) goto fail; - vgmstream->coding_type = coding_ATRAC9; - vgmstream->layout_type = layout_none; - - return 1; -fail: - return 0; -} -#endif - -static layered_layout_data* build_layered_xvag(STREAMFILE *streamFile, xvag_header * xvag, off_t chunk_offset, off_t start_offset) { - layered_layout_data* data = NULL; - STREAMFILE* temp_streamFile = NULL; - int32_t (*read_32bit)(off_t,STREAMFILE*) = xvag->big_endian ? read_32bitBE : read_32bitLE; - int i, layers = xvag->layers; - - - /* init layout */ - data = init_layout_layered(layers); - if (!data) goto fail; - - /* interleaves frames per substreams */ - for (i = 0; i < layers; i++) { - int layer_channels = xvag->channels / layers; /* all streams must be equal (XVAG limitation) */ - - /* build the layer VGMSTREAM */ - data->layers[i] = allocate_vgmstream(layer_channels, xvag->loop_flag); - if (!data->layers[i]) goto fail; - - data->layers[i]->sample_rate = xvag->sample_rate; - data->layers[i]->num_samples = xvag->num_samples; - - switch(xvag->codec) { -#ifdef VGM_USE_ATRAC9 - case 0x09: { - size_t frame_size = read_32bit(chunk_offset+0x00,streamFile); - - if (!init_xvag_atrac9(streamFile, data->layers[i], xvag, chunk_offset)) - goto fail; - temp_streamFile = setup_xvag_streamfile(streamFile, start_offset, frame_size*xvag->factor,frame_size, i, layers); - if (!temp_streamFile) goto fail; - break; - } -#endif - default: - goto fail; - } - - if ( !vgmstream_open_stream(data->layers[i], temp_streamFile, 0x00) ) - goto fail; - close_streamfile(temp_streamFile); - } - - /* setup layered VGMSTREAMs */ - if (!setup_layout_layered(data)) - goto fail; - return data; - -fail: - close_streamfile(temp_streamFile); - free_layout_layered(data); - return NULL; -} +#include "meta.h" +#include "../coding/coding.h" +#include "../layout/layout.h" +#include "xvag_streamfile.h" + + +typedef struct { + int big_endian; + int channels; + int sample_rate; + int codec; + + int factor; + + int loop_flag; + int num_samples; + int loop_start; + int loop_end; + + int subsongs; + int layers; + + size_t data_size; + off_t stream_offset; +} xvag_header; + +static int init_xvag_atrac9(STREAMFILE *streamFile, VGMSTREAM* vgmstream, xvag_header * xvag, off_t chunk_offset); +static layered_layout_data* build_layered_xvag(STREAMFILE *streamFile, xvag_header * xvag, off_t chunk_offset, off_t start_offset); + +/* XVAG - Sony's Scream Tool/Stream Creator format (God of War III, Ratchet & Clank Future, The Last of Us, Uncharted) */ +VGMSTREAM * init_vgmstream_xvag(STREAMFILE *streamFile) { + VGMSTREAM * vgmstream = NULL; + STREAMFILE* temp_streamFile = NULL; + xvag_header xvag = {0}; + int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL; + off_t start_offset, chunk_offset, first_offset = 0x20; + size_t chunk_size; + int total_subsongs = 0, target_subsong = streamFile->stream_index; + + + /* checks */ + /* .xvag: standard + * (extensionless): The Last Of Us (PS3) speech files */ + if (!check_extensions(streamFile,"xvag,")) + goto fail; + if (read_32bitBE(0x00,streamFile) != 0x58564147) /* "XVAG" */ + goto fail; + + /* endian flag (XVAGs of the same game can use BE or LE, usually when reusing from other platforms) */ + xvag.big_endian = read_8bit(0x08,streamFile) & 0x01; + if (xvag.big_endian) { + read_32bit = read_32bitBE; + } else { + read_32bit = read_32bitLE; + } + + start_offset = read_32bit(0x04,streamFile); + /* 0x08: flags? (&0x01=big endian, 0x02=?, 0x06=full RIFF AT9?) + * 0x09: flags2? (0x00/0x01/0x04, speaker mode?) + * 0x0a: always 0? + * 0x0b: version-flag? (0x5f/0x60/0x61/0x62/etc) */ + + + /* "fmat": base format (always first) */ + if (!find_chunk(streamFile, 0x666D6174,first_offset,0, &chunk_offset,&chunk_size, xvag.big_endian, 1)) /*"fmat"*/ + goto fail; + xvag.channels = read_32bit(chunk_offset+0x00,streamFile); + xvag.codec = read_32bit(chunk_offset+0x04,streamFile); + xvag.num_samples = read_32bit(chunk_offset+0x08,streamFile); + /* 0x0c: samples again? */ + VGM_ASSERT(xvag.num_samples != read_32bit(chunk_offset+0x0c,streamFile), "XVAG: num_samples values don't match\n"); + + xvag.factor = read_32bit(chunk_offset+0x10,streamFile); /* for interleave */ + xvag.sample_rate = read_32bit(chunk_offset+0x14,streamFile); + xvag.data_size = read_32bit(chunk_offset+0x18,streamFile); /* not always accurate */ + + /* extra data, seen in versions 0x61+ */ + if (chunk_size > 0x1c) { + /* number of interleaved subsongs */ + xvag.subsongs = read_32bit(chunk_offset+0x1c,streamFile); + /* number of interleaved layers (layers * channels_per_layer = channels) */ + xvag.layers = read_32bit(chunk_offset+0x20,streamFile); + } + else { + xvag.subsongs = 1; + xvag.layers = 1; + } + + total_subsongs = xvag.subsongs; + if (target_subsong == 0) target_subsong = 1; + if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail; + + + /* other chunks: */ + /* "cpan": pan/volume per channel */ + /* "cues": cue/labels (rare) */ + /* "md5 ": hash (rare) */ + /* "0000": end chunk before start_offset */ + + /* XVAG has no looping, but some PS3 PS-ADPCM seems to do full loops (without data flags) */ + if (xvag.codec == 0x06 && xvag.subsongs == 1) { + size_t file_size = get_streamfile_size(streamFile); + /* simply test if last frame is not empty = may loop */ + xvag.loop_flag = (read_8bit(file_size - 0x01, streamFile) != 0); + xvag.loop_start = 0; + xvag.loop_end = ps_bytes_to_samples(file_size - start_offset, xvag.channels); + } + + /* May use 'MP3 Surround' for multichannel [Twisted Metal (PS3), The Last of Us (PS4) test file] + * It's a mutant MP3 that decodes as 2ch but output can be routed to 6ch somehow, if manually + * activated. Fraunhofer IIS's MP3sPlayer can do it, as can PS3 (fw v2.40+) but no others seems to. + * So simply play as 2ch, they sound ok with slightly wider feel. No XVAG/MP3 flag exists to detect, + * can be found in v0x60 (without layers/subsongs) and v0x61 (with them set as 1) */ + if (xvag.codec == 0x08 && xvag.channels == 6 && xvag.layers == 1) { + xvag.channels = 2; + } + + + /* build the VGMSTREAM */ + vgmstream = allocate_vgmstream(xvag.channels,xvag.loop_flag); + if (!vgmstream) goto fail; + + vgmstream->meta_type = meta_XVAG; + vgmstream->sample_rate = xvag.sample_rate; + vgmstream->num_samples = xvag.num_samples; + if (xvag.loop_flag) { + vgmstream->loop_start_sample = xvag.loop_start; + vgmstream->loop_end_sample = xvag.loop_end; + } + vgmstream->num_streams = total_subsongs; + vgmstream->stream_size = (xvag.data_size / total_subsongs); + + switch (xvag.codec) { + case 0x06: /* VAG (PS-ADPCM): God of War III (PS3), Uncharted 1/2 (PS3), Ratchet and Clank Future (PS3) */ + case 0x07: /* SVAG? (PS-ADPCM with extended table): inFamous 1 (PS3) */ + if (xvag.subsongs > 1 && xvag.layers > 1) goto fail; + if (xvag.layers > 1 && xvag.layers != xvag.channels) goto fail; + if (xvag.subsongs > 1 && xvag.channels > 1) goto fail; /* unknown layout */ + + vgmstream->coding_type = coding_PSX; + if (xvag.codec == 0x07) + vgmstream->codec_config = 1; /* needs extended table */ + + if (xvag.subsongs > 1) { /* God of War 3 (PS4) */ + vgmstream->layout_type = layout_blocked_xvag_subsong; + vgmstream->interleave_block_size = 0x10; + vgmstream->full_block_size = 0x10 * xvag.factor * xvag.subsongs; + vgmstream->current_block_size = 0x10 * xvag.factor; + start_offset += vgmstream->current_block_size * (target_subsong-1); + } + else { + vgmstream->layout_type = layout_interleave; + vgmstream->interleave_block_size = 0x10 * xvag.factor; /* usually 1, bigger in GoW3 PS4 */ + } + break; + +#ifdef VGM_USE_MPEG + case 0x08: { /* MPEG: The Last of Us (PS3), Uncharted 3 (PS3), Medieval Moves (PS3) */ + mpeg_custom_config cfg = {0}; + + /* often 2ch per MPEG and rarely 1ch (GoW3 PS4) */ + if (xvag.layers > 1 && !(xvag.layers*1 == vgmstream->channels || xvag.layers*2 == vgmstream->channels)) goto fail; + + /* "mpin": mpeg info */ + if (!find_chunk(streamFile, 0x6D70696E,first_offset,0, &chunk_offset,NULL, xvag.big_endian, 1)) /*"mpin"*/ + goto fail; + + /* all layers/subsongs share the same config; not very useful but for posterity: + * - 0x00: mpeg version + * - 0x04: mpeg layer + * - 0x08: bit rate + * - 0x0c: sample rate + * - 0x10: some version? (0x01-0x03)? + * - 0x14: channels per stream? + * - 0x18: channels per stream or total channels? + * - 0x1c: fixed frame size (always CBR) + * - 0x20: encoder delay (usually but not always 1201) + * - 0x24: number of samples + * - 0x28: some size? + * - 0x2c: ? (0x02) + * - 0x30: ? (0x00, 0x80) + * - 0x34: data size + * (rest is padding) + * */ + cfg.chunk_size = read_32bit(chunk_offset+0x1c,streamFile); + cfg.skip_samples = read_32bit(chunk_offset+0x20,streamFile); + cfg.interleave = cfg.chunk_size * xvag.factor; + + vgmstream->codec_data = init_mpeg_custom(streamFile, start_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_XVAG, &cfg); + if (!vgmstream->codec_data) goto fail; + vgmstream->layout_type = layout_none; + + /* interleaved subsongs, rarely [Sly Cooper: Thieves in Time (PS3)] */ + if (xvag.subsongs > 1) { + temp_streamFile = setup_xvag_streamfile(streamFile, start_offset, cfg.interleave,cfg.chunk_size, (target_subsong-1), total_subsongs); + if (!temp_streamFile) goto fail; + start_offset = 0; + } + + break; + } +#endif + +#ifdef VGM_USE_ATRAC9 + case 0x09: { /* ATRAC9: Sly Cooper and the Thievius Raccoonus (Vita), The Last of Us Remastered (PS4) */ + if (xvag.subsongs > 1 && xvag.layers > 1) goto fail; + + /* "a9in": ATRAC9 info */ + /* 0x00: frame size, 0x04: samples per frame, 0x0c: fact num_samples (no change), 0x10: encoder delay1 */ + if (!find_chunk(streamFile, 0x6139696E,first_offset,0, &chunk_offset,NULL, xvag.big_endian, 1)) /*"a9in"*/ + goto fail; + + if (xvag.layers > 1) { + /* some Vita/PS4 multichannel [flower (Vita), Uncharted Collection (PS4)]. PS4 ATRAC9 also + * does single-stream >2ch, but this can do configs ATRAC9 can't, like 5ch/14ch/etc */ + vgmstream->layout_data = build_layered_xvag(streamFile, &xvag, chunk_offset, start_offset); + if (!vgmstream->layout_data) goto fail; + vgmstream->coding_type = coding_ATRAC9; + vgmstream->layout_type = layout_layered; + + break; + } + else { + /* interleaved subsongs (section layers) */ + size_t frame_size = read_32bit(chunk_offset+0x00,streamFile); + + if (!init_xvag_atrac9(streamFile, vgmstream, &xvag, chunk_offset)) + goto fail; + temp_streamFile = setup_xvag_streamfile(streamFile, start_offset, frame_size*xvag.factor,frame_size, (target_subsong-1), total_subsongs); + if (!temp_streamFile) goto fail; + start_offset = 0; + } + + break; + } +#endif + + default: + goto fail; + } + + + if (!vgmstream_open_stream(vgmstream,temp_streamFile ? temp_streamFile : streamFile,start_offset)) + goto fail; + close_streamfile(temp_streamFile); + return vgmstream; + +fail: + close_streamfile(temp_streamFile); + close_vgmstream(vgmstream); + return NULL; +} + +#ifdef VGM_USE_ATRAC9 +static int init_xvag_atrac9(STREAMFILE *streamFile, VGMSTREAM* vgmstream, xvag_header * xvag, off_t chunk_offset) { + int32_t (*read_32bit)(off_t,STREAMFILE*) = xvag->big_endian ? read_32bitBE : read_32bitLE; + atrac9_config cfg = {0}; + + cfg.channels = vgmstream->channels; + cfg.config_data = read_32bitBE(chunk_offset+0x08,streamFile); + cfg.encoder_delay = read_32bit(chunk_offset+0x14,streamFile); + + vgmstream->codec_data = init_atrac9(&cfg); + if (!vgmstream->codec_data) goto fail; + vgmstream->coding_type = coding_ATRAC9; + vgmstream->layout_type = layout_none; + + return 1; +fail: + return 0; +} +#endif + +static layered_layout_data* build_layered_xvag(STREAMFILE *streamFile, xvag_header * xvag, off_t chunk_offset, off_t start_offset) { + layered_layout_data* data = NULL; + STREAMFILE* temp_streamFile = NULL; + int32_t (*read_32bit)(off_t,STREAMFILE*) = xvag->big_endian ? read_32bitBE : read_32bitLE; + int i, layers = xvag->layers; + + + /* init layout */ + data = init_layout_layered(layers); + if (!data) goto fail; + + /* interleaves frames per substreams */ + for (i = 0; i < layers; i++) { + int layer_channels = xvag->channels / layers; /* all streams must be equal (XVAG limitation) */ + + /* build the layer VGMSTREAM */ + data->layers[i] = allocate_vgmstream(layer_channels, xvag->loop_flag); + if (!data->layers[i]) goto fail; + + data->layers[i]->sample_rate = xvag->sample_rate; + data->layers[i]->num_samples = xvag->num_samples; + + switch(xvag->codec) { +#ifdef VGM_USE_ATRAC9 + case 0x09: { + size_t frame_size = read_32bit(chunk_offset+0x00,streamFile); + + if (!init_xvag_atrac9(streamFile, data->layers[i], xvag, chunk_offset)) + goto fail; + temp_streamFile = setup_xvag_streamfile(streamFile, start_offset, frame_size*xvag->factor,frame_size, i, layers); + if (!temp_streamFile) goto fail; + break; + } +#endif + default: + goto fail; + } + + if ( !vgmstream_open_stream(data->layers[i], temp_streamFile, 0x00) ) + goto fail; + close_streamfile(temp_streamFile); + } + + /* setup layered VGMSTREAMs */ + if (!setup_layout_layered(data)) + goto fail; + return data; + +fail: + close_streamfile(temp_streamFile); + free_layout_layered(data); + return NULL; +} diff --git a/Frameworks/vgmstream/vgmstream/src/vgmstream.c b/Frameworks/vgmstream/vgmstream/src/vgmstream.c index e8634ccff..62ad41da5 100644 --- a/Frameworks/vgmstream/vgmstream/src/vgmstream.c +++ b/Frameworks/vgmstream/vgmstream/src/vgmstream.c @@ -1694,19 +1694,19 @@ void decode_vgmstream(VGMSTREAM * vgmstream, int samples_written, int samples_to case coding_PSX: for (ch = 0; ch < vgmstream->channels; ch++) { decode_psx(&vgmstream->ch[ch],buffer+samples_written*vgmstream->channels+ch, - vgmstream->channels,vgmstream->samples_into_block,samples_to_do, 0); + vgmstream->channels,vgmstream->samples_into_block,samples_to_do, 0, vgmstream->codec_config); } break; case coding_PSX_badflags: for (ch = 0; ch < vgmstream->channels; ch++) { decode_psx(&vgmstream->ch[ch],buffer+samples_written*vgmstream->channels+ch, - vgmstream->channels,vgmstream->samples_into_block,samples_to_do, 1); + vgmstream->channels,vgmstream->samples_into_block,samples_to_do, 1, vgmstream->codec_config); } break; case coding_PSX_cfg: for (ch = 0; ch < vgmstream->channels; ch++) { decode_psx_configurable(&vgmstream->ch[ch],buffer+samples_written*vgmstream->channels+ch, - vgmstream->channels,vgmstream->samples_into_block,samples_to_do, vgmstream->interleave_block_size); + vgmstream->channels,vgmstream->samples_into_block,samples_to_do, vgmstream->interleave_block_size, vgmstream->codec_config); } break; case coding_PSX_pivotal: