diff --git a/src/coding/psx_decoder.c b/src/coding/psx_decoder.c index 3b5b2b7e..6182f3ae 100644 --- a/src/coding/psx_decoder.c +++ b/src/coding/psx_decoder.c @@ -1,423 +1,423 @@ -#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[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; +} diff --git a/src/meta/xvag.c b/src/meta/xvag.c index 4cf0247f..76b01ce3 100644 --- a/src/meta/xvag.c +++ b/src/meta/xvag.c @@ -1,324 +1,324 @@ -#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.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; +}