Browse Source

Updated VGMStream to r1050-2834-gc40d364e

master
Christopher Snowhill 4 months ago
parent
commit
9dd4f68049
15 changed files with 2418 additions and 2279 deletions
  1. +2
    -2
      Frameworks/vgmstream/vgmstream/src/coding/coding.h
  2. +446
    -423
      Frameworks/vgmstream/vgmstream/src/coding/psx_decoder.c
  3. +75
    -60
      Frameworks/vgmstream/vgmstream/src/layout/blocked_awc.c
  4. +15
    -0
      Frameworks/vgmstream/vgmstream/src/meta/awb.c
  5. +349
    -349
      Frameworks/vgmstream/vgmstream/src/meta/awc.c
  6. +1
    -0
      Frameworks/vgmstream/vgmstream/src/meta/meta.h
  7. +39
    -1
      Frameworks/vgmstream/vgmstream/src/meta/nub.c
  8. +30
    -2
      Frameworks/vgmstream/vgmstream/src/meta/riff.c
  9. +2
    -1
      Frameworks/vgmstream/vgmstream/src/meta/sgxd.c
  10. +373
    -373
      Frameworks/vgmstream/vgmstream/src/meta/ta_aac.c
  11. +1
    -1
      Frameworks/vgmstream/vgmstream/src/meta/ubi_hx.c
  12. +12
    -0
      Frameworks/vgmstream/vgmstream/src/meta/ubi_sb.c
  13. +744
    -740
      Frameworks/vgmstream/vgmstream/src/meta/wwise.c
  14. +326
    -324
      Frameworks/vgmstream/vgmstream/src/meta/xvag.c
  15. +3
    -3
      Frameworks/vgmstream/vgmstream/src/vgmstream.c

+ 2
- 2
Frameworks/vgmstream/vgmstream/src/coding/coding.h View File

@@ -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);


+ 446
- 423
Frameworks/vgmstream/vgmstream/src/coding/psx_decoder.c View File

@@ -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;
}

+ 75
- 60
Frameworks/vgmstream/vgmstream/src/layout/blocked_awc.c View File

@@ -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;
}

+ 15
- 0
Frameworks/vgmstream/vgmstream/src/meta/awb.c View File

@@ -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);


+ 349
- 349
Frameworks/vgmstream/vgmstream/src/meta/awc.c View File

@@ -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;
}

+ 1
- 0
Frameworks/vgmstream/vgmstream/src/meta/meta.h View File

@@ -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);



+ 39
- 1
Frameworks/vgmstream/vgmstream/src/meta/nub.c View File

@@ -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;


+ 30
- 2
Frameworks/vgmstream/vgmstream/src/meta/riff.c View File

@@ -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;


+ 2
- 1
Frameworks/vgmstream/vgmstream/src/meta/sgxd.c View File

@@ -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;



+ 373
- 373
Frameworks/vgmstream/vgmstream/src/meta/ta_aac.c View File

@@ -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;