mirror of
https://github.com/vgmstream/vgmstream.git
synced 2025-02-18 03:26:57 +01:00
Tweak PS-ADPCM decoding [inFamous (PS3)]
- Code now more verbose, but easier to understand (by me, at least) - Unify normal and badflags code - Fix hist1 not being properly clamped, though not noticeable - Fix inFamous (PS3) which seemingly wasn't using the extended table but hitting some compiler weirdness? (failed in foobar and worked in test) - Use int math for PSX-cfg, which should be minimally more accurate
This commit is contained in:
parent
1ec9463235
commit
af70e95877
@ -73,13 +73,14 @@ void decode_pcmfloat(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspac
|
||||
size_t pcm_bytes_to_samples(size_t bytes, int channels, int bits_per_sample);
|
||||
|
||||
/* psx_decoder */
|
||||
void decode_psx(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do);
|
||||
void decode_psx_badflags(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do);
|
||||
void decode_psx(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int is_badflags);
|
||||
void decode_psx_configurable(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int frame_size);
|
||||
void decode_hevag(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do);
|
||||
size_t ps_bytes_to_samples(size_t bytes, int channels);
|
||||
size_t ps_cfg_bytes_to_samples(size_t bytes, size_t frame_size, int channels);
|
||||
|
||||
/* psv_decoder */
|
||||
void decode_hevag(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do);
|
||||
|
||||
/* xa_decoder */
|
||||
void decode_xa(VGMSTREAM * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int channel);
|
||||
size_t xa_bytes_to_samples(size_t bytes, int channels, int is_blocked);
|
||||
|
@ -1,40 +1,24 @@
|
||||
#include <math.h>
|
||||
#include "coding.h"
|
||||
#include "../util.h"
|
||||
|
||||
/* for some algos, maybe closer to the real thing */
|
||||
#define VAG_USE_INTEGER_TABLE 0
|
||||
|
||||
/* PS ADPCM table (precalculated divs) */
|
||||
static const double VAG_f[16][2] = {
|
||||
/* PS-ADPCM table, defined as rational numbers (as in the spec) */
|
||||
static const double ps_adpcm_coefs_f[5][2] = {
|
||||
{ 0.0 , 0.0 },
|
||||
{ 60.0 / 64.0 , 0.0 },
|
||||
{ 115.0 / 64.0 , -52.0 / 64.0 },
|
||||
{ 98.0 / 64.0 , -55.0 / 64.0 },
|
||||
{ 122.0 / 64.0 , -60.0 / 64.0 },
|
||||
/* extended table from PPSSPP (PSP emu), found by tests
|
||||
* (only seen in inFamous PS3, very rare, possibly "SVAG" or "VAG-HE") */
|
||||
{ 0.0 , 0.0 },
|
||||
{ 0.0 , 0.0 },
|
||||
{ 52.0 / 64.0 , 0.0 },
|
||||
{ 55.0 / 64.0 , -2.0 / 64.0 },
|
||||
{ 60.0 / 64.0 ,-125.0 / 64.0 },
|
||||
{ 0.0 , 0.0 },
|
||||
{ 0.0 , -91.0 / 64.0 },
|
||||
{ 0.0 , 0.0 },
|
||||
{ 2.0 / 64.0 ,-216.0 / 64.0 },
|
||||
{ 125.0 / 64.0 , -6.0 / 64.0 },
|
||||
{ 0.0 ,-151.0 / 64.0 },
|
||||
};
|
||||
#if VAG_USE_INTEGER_TABLE
|
||||
/* PS ADPCM table */
|
||||
static const int8_t VAG_coefs[5][2] = {
|
||||
|
||||
/* 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 },
|
||||
/* extended */
|
||||
#if 0
|
||||
/* extended table from PPSSPP (PSP emu), found by tests (unused?) */
|
||||
{ 0 , 0 },
|
||||
{ 0 , 0 },
|
||||
{ 52 , 0 },
|
||||
@ -46,163 +30,79 @@ static const int8_t VAG_coefs[5][2] = {
|
||||
{ 2 ,-216 },
|
||||
{ 125 , -6 },
|
||||
{ 0 ,-151 },
|
||||
};
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Sony's PS ADPCM (sometimes called VAG), decodes 16 bytes into 28 samples.
|
||||
* The first 2 bytes are a header (shift, predictor, optional flag).
|
||||
* All variants are the same with minor differences.
|
||||
/* 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).
|
||||
*
|
||||
* Flags:
|
||||
* 0x0: Nothing
|
||||
* 0x1: End marker + decode
|
||||
* 0x2: Loop region
|
||||
* 0x3: Loop end
|
||||
* 0x4: Start marker
|
||||
* 0x5: ?
|
||||
* 0x6: Loop start
|
||||
* 0x7: End marker + don't decode
|
||||
* 0x8+ Not valid
|
||||
* 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.
|
||||
*
|
||||
* Optional bit flag combinations in the header control the SPU:
|
||||
* 0x0 (0000): Nothing
|
||||
* 0x1 (0001): End marker + decode
|
||||
* 0x2 (0010): Loop region
|
||||
* 0x3 (0011): Loop end
|
||||
* 0x4 (0100): Start marker
|
||||
* 0x6 (0110): Loop start
|
||||
* 0x7 (0111): End marker + don't decode
|
||||
* 0x5/8+ (1NNN): Not valid
|
||||
*/
|
||||
|
||||
/* default */
|
||||
void decode_psx(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do) {
|
||||
|
||||
int predict_nr, shift_factor, sample;
|
||||
int32_t hist1=stream->adpcm_history1_32;
|
||||
int32_t hist2=stream->adpcm_history2_32;
|
||||
|
||||
short scale;
|
||||
int i;
|
||||
int32_t sample_count;
|
||||
uint8_t flag;
|
||||
|
||||
int framesin = first_sample/28;
|
||||
|
||||
predict_nr = read_8bit(stream->offset+framesin*16,stream->streamfile) >> 4;
|
||||
shift_factor = read_8bit(stream->offset+framesin*16,stream->streamfile) & 0xf;
|
||||
flag = read_8bit(stream->offset+framesin*16+1,stream->streamfile); /* only lower nibble needed */
|
||||
|
||||
first_sample = first_sample % 28;
|
||||
|
||||
for (i=first_sample,sample_count=0; i<first_sample+samples_to_do; i++,sample_count+=channelspacing) {
|
||||
|
||||
sample=0;
|
||||
|
||||
if(flag<0x07) {
|
||||
|
||||
short sample_byte = (short)read_8bit(stream->offset+(framesin*16)+2+i/2,stream->streamfile);
|
||||
|
||||
scale = ((i&1 ? /* odd/even byte */
|
||||
sample_byte >> 4 :
|
||||
sample_byte & 0x0f)<<12);
|
||||
|
||||
sample=(int)((scale >> shift_factor)+hist1*VAG_f[predict_nr][0]+hist2*VAG_f[predict_nr][1]);
|
||||
}
|
||||
|
||||
outbuf[sample_count] = clamp16(sample);
|
||||
hist2=hist1;
|
||||
hist1=sample;
|
||||
}
|
||||
stream->adpcm_history1_32=hist1;
|
||||
stream->adpcm_history2_32=hist2;
|
||||
}
|
||||
|
||||
/* some games have garbage (?) in their flags, this decoder just ignores that byte */
|
||||
void decode_psx_badflags(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do) {
|
||||
|
||||
int predict_nr, shift_factor, sample;
|
||||
int32_t hist1=stream->adpcm_history1_32;
|
||||
int32_t hist2=stream->adpcm_history2_32;
|
||||
|
||||
short scale;
|
||||
int i;
|
||||
int32_t sample_count;
|
||||
|
||||
int framesin = first_sample/28;
|
||||
|
||||
predict_nr = read_8bit(stream->offset+framesin*16,stream->streamfile) >> 4;
|
||||
shift_factor = read_8bit(stream->offset+framesin*16,stream->streamfile) & 0xf;
|
||||
first_sample = first_sample % 28;
|
||||
|
||||
for (i=first_sample,sample_count=0; i<first_sample+samples_to_do; i++,sample_count+=channelspacing) {
|
||||
short sample_byte = (short)read_8bit(stream->offset+(framesin*16)+2+i/2,stream->streamfile);
|
||||
|
||||
scale = ((i&1 ?
|
||||
sample_byte >> 4 :
|
||||
sample_byte & 0x0f)<<12);
|
||||
|
||||
sample=(int)((scale >> shift_factor)+hist1*VAG_f[predict_nr][0]+hist2*VAG_f[predict_nr][1]);
|
||||
|
||||
outbuf[sample_count] = clamp16(sample);
|
||||
hist2=hist1;
|
||||
hist1=sample;
|
||||
}
|
||||
stream->adpcm_history1_32=hist1;
|
||||
stream->adpcm_history2_32=hist2;
|
||||
}
|
||||
|
||||
|
||||
/* configurable frame size, with no flag
|
||||
* Found in PS3 Afrika (SGXD type 5) in size 4, FF XI in sizes 3/5/9/41, Blur and James Bond in size 33. */
|
||||
void decode_psx_configurable(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int frame_size) {
|
||||
uint8_t predict_nr, shift, byte;
|
||||
int16_t scale = 0;
|
||||
|
||||
int32_t sample;
|
||||
/* standard PS-ADPCM (float math version) */
|
||||
void decode_psx(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int is_badflags) {
|
||||
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 i, sample_count, bytes_per_frame, samples_per_frame;
|
||||
const int header_size = 1;
|
||||
int framesin;
|
||||
|
||||
bytes_per_frame = frame_size - header_size;
|
||||
samples_per_frame = bytes_per_frame * 2;
|
||||
|
||||
framesin = first_sample / samples_per_frame;
|
||||
|
||||
/* 1 byte header: predictor = 1st, shift = 2nd */
|
||||
byte = (uint8_t)read_8bit(stream->offset+framesin*frame_size+0,stream->streamfile);
|
||||
predict_nr = byte >> 4;
|
||||
shift = byte & 0x0f;
|
||||
|
||||
/* 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;
|
||||
|
||||
if (first_sample & 1) { /* if restarting on a high nibble, read byte first */
|
||||
byte = (uint8_t)read_8bit(stream->offset+(framesin*frame_size)+header_size+first_sample/2,stream->streamfile);
|
||||
}
|
||||
/* parse frame header */
|
||||
frame_offset = stream->offset + bytes_per_frame*frames_in;
|
||||
coef_index = ((uint8_t)read_8bit(frame_offset+0x00,stream->streamfile) >> 4) & 0xf;
|
||||
shift_factor = ((uint8_t)read_8bit(frame_offset+0x00,stream->streamfile) >> 0) & 0xf;
|
||||
flag = (uint8_t)read_8bit(frame_offset+0x01,stream->streamfile); /* only lower nibble needed */
|
||||
|
||||
for (i = first_sample, sample_count = 0; i < first_sample + samples_to_do; i++, sample_count += channelspacing) {
|
||||
sample = 0;
|
||||
VGM_ASSERT_ONCE(coef_index > 5 || shift_factor > 12, "PS-ADPCM: incorrect coefs/shift at %lx\n", 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 (predict_nr < 5) {
|
||||
if (!(i&1)) { /* low nibble first */
|
||||
byte = (uint8_t)read_8bit(stream->offset+(framesin*frame_size)+header_size+i/2,stream->streamfile);
|
||||
scale = (byte & 0x0f);
|
||||
} else { /* high nibble last */
|
||||
scale = byte >> 4;
|
||||
}
|
||||
scale = scale << 12; /* shift + sign extend (only if scale is int16_t) */
|
||||
/*if (scale > 7) {
|
||||
scale = scale - 16;
|
||||
}*/
|
||||
#if VAG_USE_INTEGER_TABLE
|
||||
sample = (scale >> shift) +
|
||||
(hist1 * VAG_coefs[predict_nr][0] +
|
||||
hist2 * VAG_coefs[predict_nr][1] ) / 64;
|
||||
#else
|
||||
sample = (int)( (scale >> shift) +
|
||||
(hist1 * VAG_f[predict_nr][0] +
|
||||
hist2 * VAG_f[predict_nr][1]) );
|
||||
#endif
|
||||
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 %lx\n", frame_offset); /* meta should set PSX-badflags */
|
||||
|
||||
/* decode nibbles */
|
||||
for (i = first_sample; i < first_sample + samples_to_do; i++) {
|
||||
int32_t new_sample = 0;
|
||||
|
||||
if (flag < 0x07) { /* with flag 0x07 decoded sample must be 0 */
|
||||
uint8_t nibbles = (uint8_t)read_8bit(frame_offset+0x02+i/2,stream->streamfile);
|
||||
|
||||
new_sample = i&1 ? /* low nibble first */
|
||||
(nibbles >> 4) & 0x0f :
|
||||
(nibbles >> 0) & 0x0f;
|
||||
new_sample = (int16_t)((new_sample << 12) & 0xf000) >> shift_factor; /* 16b sign extend + scale */
|
||||
new_sample = (int)(new_sample + ps_adpcm_coefs_f[coef_index][0]*hist1 + ps_adpcm_coefs_f[coef_index][1]*hist2);
|
||||
new_sample = clamp16(new_sample);
|
||||
}
|
||||
|
||||
outbuf[sample_count] = clamp16(sample);
|
||||
outbuf[sample_count] = new_sample;
|
||||
sample_count += channelspacing;
|
||||
|
||||
hist2 = hist1;
|
||||
hist1 = sample;
|
||||
hist1 = new_sample;
|
||||
}
|
||||
|
||||
stream->adpcm_history1_32 = hist1;
|
||||
@ -210,6 +110,58 @@ void decode_psx_configurable(VGMSTREAMCHANNEL * stream, sample * outbuf, int cha
|
||||
}
|
||||
|
||||
|
||||
/* PS-ADPCM with configurable frame size and no flag (int math version).
|
||||
* Found in some PC/PS3 games (FF XI in sizes 3/5/9/41, Afrika in size 4, Blur/James Bond in size 33, 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 * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int frame_size) {
|
||||
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; /* 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;
|
||||
coef_index = ((uint8_t)read_8bit(frame_offset+0x00,stream->streamfile) >> 4) & 0xf;
|
||||
shift_factor = ((uint8_t)read_8bit(frame_offset+0x00,stream->streamfile) >> 0) & 0xf;
|
||||
|
||||
VGM_ASSERT_ONCE(coef_index > 5 || shift_factor > 12, "PS-ADPCM: incorrect coefs/shift at %lx\n", 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 new_sample = 0;
|
||||
uint8_t nibbles = (uint8_t)read_8bit(frame_offset+0x01+i/2,stream->streamfile);
|
||||
|
||||
new_sample = i&1 ? /* low nibble first */
|
||||
(nibbles >> 4) & 0x0f :
|
||||
(nibbles >> 0) & 0x0f;
|
||||
new_sample = (int16_t)((new_sample << 12) & 0xf000) >> shift_factor; /* 16b sign extend + scale */
|
||||
new_sample = new_sample + ((ps_adpcm_coefs_i[coef_index][0]*hist1 + ps_adpcm_coefs_i[coef_index][1]*hist2) >> 6);
|
||||
new_sample = clamp16(new_sample);
|
||||
|
||||
outbuf[sample_count] = new_sample;
|
||||
sample_count += channelspacing;
|
||||
|
||||
hist2 = hist1;
|
||||
hist1 = new_sample;
|
||||
}
|
||||
|
||||
stream->adpcm_history1_32 = hist1;
|
||||
stream->adpcm_history2_32 = hist2;
|
||||
}
|
||||
|
||||
size_t ps_bytes_to_samples(size_t bytes, int channels) {
|
||||
return bytes / channels / 0x10 * 28;
|
||||
}
|
||||
|
@ -1535,21 +1535,14 @@ void decode_vgmstream(VGMSTREAM * vgmstream, int samples_written, int samples_to
|
||||
for (chan=0;chan<vgmstream->channels;chan++) {
|
||||
decode_psx(&vgmstream->ch[chan],buffer+samples_written*vgmstream->channels+chan,
|
||||
vgmstream->channels,vgmstream->samples_into_block,
|
||||
samples_to_do);
|
||||
samples_to_do, 0);
|
||||
}
|
||||
break;
|
||||
case coding_PSX_badflags:
|
||||
for (chan=0;chan<vgmstream->channels;chan++) {
|
||||
decode_psx_badflags(&vgmstream->ch[chan],buffer+samples_written*vgmstream->channels+chan,
|
||||
decode_psx(&vgmstream->ch[chan],buffer+samples_written*vgmstream->channels+chan,
|
||||
vgmstream->channels,vgmstream->samples_into_block,
|
||||
samples_to_do);
|
||||
}
|
||||
break;
|
||||
case coding_HEVAG:
|
||||
for (chan=0;chan<vgmstream->channels;chan++) {
|
||||
decode_hevag(&vgmstream->ch[chan],buffer+samples_written*vgmstream->channels+chan,
|
||||
vgmstream->channels,vgmstream->samples_into_block,
|
||||
samples_to_do);
|
||||
samples_to_do, 1);
|
||||
}
|
||||
break;
|
||||
case coding_PSX_cfg:
|
||||
@ -1559,6 +1552,13 @@ void decode_vgmstream(VGMSTREAM * vgmstream, int samples_written, int samples_to
|
||||
samples_to_do, vgmstream->interleave_block_size);
|
||||
}
|
||||
break;
|
||||
case coding_HEVAG:
|
||||
for (chan=0;chan<vgmstream->channels;chan++) {
|
||||
decode_hevag(&vgmstream->ch[chan],buffer+samples_written*vgmstream->channels+chan,
|
||||
vgmstream->channels,vgmstream->samples_into_block,
|
||||
samples_to_do);
|
||||
}
|
||||
break;
|
||||
case coding_XA:
|
||||
for (chan=0;chan<vgmstream->channels;chan++) {
|
||||
decode_xa(vgmstream,buffer+samples_written*vgmstream->channels+chan,
|
||||
|
Loading…
x
Reference in New Issue
Block a user