2008-06-02 19:58:08 +02:00
|
|
|
#include "coding.h"
|
|
|
|
#include "../util.h"
|
|
|
|
|
2017-08-05 17:59:35 +02:00
|
|
|
/* AKA "EA ADPCM", evolved from CDXA. Inconsistently called EA XA/EA-XA/EAXA.
|
|
|
|
* Some variations contain ADPCM hist header per block, but it's handled in ea_block.c */
|
2017-07-01 23:02:24 +02:00
|
|
|
|
2017-07-21 21:03:51 +02:00
|
|
|
/*
|
|
|
|
* Another way to get coefs in EAXA v2, with no diffs (no idea which table is actually used in games):
|
|
|
|
* coef1 = EA_XA_TABLE2[(((frame_info >> 4) & 0x0F) << 1) + 0];
|
|
|
|
* coef2 = EA_XA_TABLE2[(((frame_info >> 4) & 0x0F) << 1) + 1];
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
static const int32_t EA_XA_TABLE2[28] = {
|
|
|
|
0, 0, 240, 0,
|
|
|
|
460, -208, 392, -220,
|
|
|
|
0, 0, 240, 0,
|
|
|
|
460, 0, 392, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
-208, -1, -220, -1,
|
|
|
|
0, 0, 0, 0x3F70
|
2017-07-01 23:02:24 +02:00
|
|
|
};
|
2017-07-21 21:03:51 +02:00
|
|
|
*/
|
2017-07-01 23:02:24 +02:00
|
|
|
|
2017-07-21 21:03:51 +02:00
|
|
|
static const int EA_XA_TABLE[20] = {
|
2017-07-08 00:27:36 +02:00
|
|
|
0, 240, 460, 392,
|
|
|
|
0, 0, -208, -220,
|
|
|
|
0, 1, 3, 4,
|
|
|
|
7, 8, 10, 11,
|
|
|
|
0, -1, -3, -4
|
2017-07-01 23:02:24 +02:00
|
|
|
};
|
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
/* EA XA v2 (always mono); like ea_xa_int but with "PCM samples" flag and doesn't add 128 on expand or clamp (pre-adjusted by the encoder?) */
|
2019-01-01 19:02:58 +01:00
|
|
|
void decode_ea_xa_v2(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int channel) {
|
2008-06-02 19:58:08 +02:00
|
|
|
uint8_t frame_info;
|
2017-07-08 00:27:36 +02:00
|
|
|
int32_t coef1, coef2;
|
2017-12-03 01:37:56 +01:00
|
|
|
int i, sample_count, shift;
|
2017-07-01 23:02:24 +02:00
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
int pcm_frame_size = 0x01 + 2*0x02 + 28*0x02;
|
|
|
|
int xa_frame_size = 0x0f;
|
|
|
|
int frame_samples = 28;
|
|
|
|
first_sample = first_sample % frame_samples;
|
2017-07-01 23:02:24 +02:00
|
|
|
|
|
|
|
/* header */
|
2017-12-03 01:37:56 +01:00
|
|
|
frame_info = read_8bit(stream->offset,stream->streamfile);
|
2017-07-01 23:02:24 +02:00
|
|
|
|
2017-07-21 19:19:58 +02:00
|
|
|
if (frame_info == 0xEE) { /* PCM frame (used in later revisions), samples always BE */
|
2017-12-03 01:37:56 +01:00
|
|
|
stream->adpcm_history1_32 = read_16bitBE(stream->offset + 0x01 + 0x00,stream->streamfile);
|
|
|
|
stream->adpcm_history2_32 = read_16bitBE(stream->offset + 0x01 + 0x02,stream->streamfile);
|
2017-07-01 23:02:24 +02:00
|
|
|
|
|
|
|
for (i=first_sample,sample_count=0; i<first_sample+samples_to_do; i++,sample_count+=channelspacing) {
|
2017-12-03 01:37:56 +01:00
|
|
|
outbuf[sample_count] = read_16bitBE(stream->offset + 0x01 + 2*0x02 + i*0x02,stream->streamfile);
|
2017-07-01 23:02:24 +02:00
|
|
|
}
|
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
/* only increment offset on complete frame */
|
|
|
|
if (i == frame_samples)
|
|
|
|
stream->offset += pcm_frame_size;
|
|
|
|
}
|
|
|
|
else { /* ADPCM frame */
|
2017-07-21 21:03:51 +02:00
|
|
|
coef1 = EA_XA_TABLE[(frame_info >> 4) + 0];
|
|
|
|
coef2 = EA_XA_TABLE[(frame_info >> 4) + 4];
|
2017-07-01 23:02:24 +02:00
|
|
|
shift = (frame_info & 0x0F) + 8;
|
|
|
|
|
|
|
|
for (i=first_sample,sample_count=0; i<first_sample+samples_to_do; i++,sample_count+=channelspacing) {
|
2017-07-21 21:03:51 +02:00
|
|
|
uint8_t sample_byte, sample_nibble;
|
2017-12-03 01:37:56 +01:00
|
|
|
int32_t new_sample;
|
|
|
|
off_t byte_offset = (stream->offset + 0x01 + i/2);
|
|
|
|
int nibble_shift = (!(i&1)) ? 4 : 0; /* high nibble first */
|
2017-07-21 21:03:51 +02:00
|
|
|
|
|
|
|
sample_byte = (uint8_t)read_8bit(byte_offset,stream->streamfile);
|
2017-12-03 01:37:56 +01:00
|
|
|
sample_nibble = (sample_byte >> nibble_shift) & 0x0F;
|
|
|
|
new_sample = (sample_nibble << 28) >> shift; /* sign extend to 32b and shift */
|
|
|
|
new_sample = (new_sample + coef1 * stream->adpcm_history1_32 + coef2 * stream->adpcm_history2_32) >> 8;
|
|
|
|
new_sample = clamp16(new_sample);
|
2017-07-01 23:02:24 +02:00
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
outbuf[sample_count] = new_sample;
|
2017-07-01 23:02:24 +02:00
|
|
|
stream->adpcm_history2_32 = stream->adpcm_history1_32;
|
2017-12-03 01:37:56 +01:00
|
|
|
stream->adpcm_history1_32 = new_sample;
|
2017-07-01 23:02:24 +02:00
|
|
|
}
|
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
/* only increment offset on complete frame */
|
|
|
|
if (i == frame_samples)
|
|
|
|
stream->offset += xa_frame_size;
|
2017-07-01 23:02:24 +02:00
|
|
|
}
|
2008-06-02 19:58:08 +02:00
|
|
|
}
|
2008-07-14 21:21:45 +02:00
|
|
|
|
2019-01-01 19:02:58 +01:00
|
|
|
#if 0
|
|
|
|
/* later PC games use float math, though in the end sounds basically the same (decompiled from various exes) */
|
|
|
|
static const double XA_K0[16] = { 0.0, 0.9375, 1.796875, 1.53125 };
|
|
|
|
static const double XA_K1[16] = { 0.0, 0.0, -0.8125, -0.859375 };
|
|
|
|
/* code uses look-up table but it's be equivalent to:
|
|
|
|
* (double)((nibble << 28) >> (shift + 8) >> 8) or (double)(signed_nibble << (12 - shift)) */
|
|
|
|
static const uint32_t FLOAT_TABLE_INT[256] = {
|
|
|
|
0x00000000,0x45800000,0x46000000,0x46400000,0x46800000,0x46A00000,0x46C00000,0x46E00000,
|
|
|
|
0xC7000000,0xC6E00000,0xC6C00000,0xC6A00000,0xC6800000,0xC6400000,0xC6000000,0xC5800000,
|
|
|
|
0x00000000,0x45000000,0x45800000,0x45C00000,0x46000000,0x46200000,0x46400000,0x46600000,
|
|
|
|
0xC6800000,0xC6600000,0xC6400000,0xC6200000,0xC6000000,0xC5C00000,0xC5800000,0xC5000000,
|
|
|
|
0x00000000,0x44800000,0x45000000,0x45400000,0x45800000,0x45A00000,0x45C00000,0x45E00000,
|
|
|
|
0xC6000000,0xC5E00000,0xC5C00000,0xC5A00000,0xC5800000,0xC5400000,0xC5000000,0xC4800000,
|
|
|
|
0x00000000,0x44000000,0x44800000,0x44C00000,0x45000000,0x45200000,0x45400000,0x45600000,
|
|
|
|
0xC5800000,0xC5600000,0xC5400000,0xC5200000,0xC5000000,0xC4C00000,0xC4800000,0xC4000000,
|
|
|
|
0x00000000,0x43800000,0x44000000,0x44400000,0x44800000,0x44A00000,0x44C00000,0x44E00000,
|
|
|
|
0xC5000000,0xC4E00000,0xC4C00000,0xC4A00000,0xC4800000,0xC4400000,0xC4000000,0xC3800000,
|
|
|
|
0x00000000,0x43000000,0x43800000,0x43C00000,0x44000000,0x44200000,0x44400000,0x44600000,
|
|
|
|
0xC4800000,0xC4600000,0xC4400000,0xC4200000,0xC4000000,0xC3C00000,0xC3800000,0xC3000000,
|
|
|
|
0x00000000,0x42800000,0x43000000,0x43400000,0x43800000,0x43A00000,0x43C00000,0x43E00000,
|
|
|
|
0xC4000000,0xC3E00000,0xC3C00000,0xC3A00000,0xC3800000,0xC3400000,0xC3000000,0xC2800000,
|
|
|
|
0x00000000,0x42000000,0x42800000,0x42C00000,0x43000000,0x43200000,0x43400000,0x43600000,
|
|
|
|
0xC3800000,0xC3600000,0xC3400000,0xC3200000,0xC3000000,0xC2C00000,0xC2800000,0xC2000000,
|
|
|
|
0x00000000,0x41800000,0x42000000,0x42400000,0x42800000,0x42A00000,0x42C00000,0x42E00000,
|
|
|
|
0xC3000000,0xC2E00000,0xC2C00000,0xC2A00000,0xC2800000,0xC2400000,0xC2000000,0xC1800000,
|
|
|
|
0x00000000,0x41000000,0x41800000,0x41C00000,0x42000000,0x42200000,0x42400000,0x42600000,
|
|
|
|
0xC2800000,0xC2600000,0xC2400000,0xC2200000,0xC2000000,0xC1C00000,0xC1800000,0xC1000000,
|
|
|
|
0x00000000,0x40800000,0x41000000,0x41400000,0x41800000,0x41A00000,0x41C00000,0x41E00000,
|
|
|
|
0xC2000000,0xC1E00000,0xC1C00000,0xC1A00000,0xC1800000,0xC1400000,0xC1000000,0xC0800000,
|
|
|
|
0x00000000,0x40000000,0x40800000,0x40C00000,0x41000000,0x41200000,0x41400000,0x41600000,
|
|
|
|
0xC1800000,0xC1600000,0xC1400000,0xC1200000,0xC1000000,0xC0C00000,0xC0800000,0xC0000000,
|
|
|
|
0x00000000,0x3F800000,0x40000000,0x40400000,0x40800000,0x40A00000,0x40C00000,0x40E00000,
|
|
|
|
0xC1000000,0xC0E00000,0xC0C00000,0xC0A00000,0xC0800000,0xC0400000,0xC0000000,0xBF800000,
|
|
|
|
0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
|
|
|
|
0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
|
|
|
|
0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
|
|
|
|
0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
|
|
|
|
0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
|
|
|
|
0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
|
|
|
|
};
|
|
|
|
static const float *FLOAT_TABLE = (const float *)FLOAT_TABLE_INT;
|
|
|
|
|
|
|
|
void decode_ea_xa_v2(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int channel) {
|
|
|
|
uint8_t frame_info;
|
|
|
|
int i, sample_count, shift;
|
|
|
|
|
|
|
|
int pcm_frame_size = 0x01 + 2*0x02 + 28*0x02;
|
|
|
|
int xa_frame_size = 0x0f;
|
|
|
|
int frame_samples = 28;
|
|
|
|
first_sample = first_sample % frame_samples;
|
|
|
|
|
|
|
|
/* header */
|
|
|
|
frame_info = read_8bit(stream->offset,stream->streamfile);
|
|
|
|
|
|
|
|
if (frame_info == 0xEE) { /* PCM frame (used in later revisions), samples always BE */
|
|
|
|
stream->adpcm_history1_double = read_16bitBE(stream->offset + 0x01 + 0x00,stream->streamfile);
|
|
|
|
stream->adpcm_history2_double = read_16bitBE(stream->offset + 0x01 + 0x02,stream->streamfile);
|
|
|
|
|
|
|
|
for (i=first_sample,sample_count=0; i<first_sample+samples_to_do; i++,sample_count+=channelspacing) {
|
|
|
|
outbuf[sample_count] = read_16bitBE(stream->offset + 0x01 + 2*0x02 + i*0x02,stream->streamfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only increment offset on complete frame */
|
|
|
|
if (i == frame_samples)
|
|
|
|
stream->offset += pcm_frame_size;
|
|
|
|
}
|
|
|
|
else { /* ADPCM frame */
|
|
|
|
double coef1, coef2, hist1, hist2, new_sample;
|
|
|
|
|
|
|
|
coef1 = XA_K0[(frame_info >> 4)];
|
|
|
|
coef2 = XA_K1[(frame_info >> 4)];
|
|
|
|
shift = (frame_info & 0x0F) + 8;// << 4;
|
|
|
|
hist1 = stream->adpcm_history1_double;
|
|
|
|
hist2 = stream->adpcm_history2_double;
|
|
|
|
|
|
|
|
for (i=first_sample,sample_count=0; i<first_sample+samples_to_do; i++,sample_count+=channelspacing) {
|
|
|
|
uint8_t sample_byte, sample_nibble;
|
|
|
|
off_t byte_offset = (stream->offset + 0x01 + i/2);
|
|
|
|
int nibble_shift = (!(i&1)) ? 4 : 0; /* high nibble first */
|
|
|
|
|
|
|
|
sample_byte = (uint8_t)read_8bit(byte_offset,stream->streamfile);
|
|
|
|
sample_nibble = (sample_byte >> nibble_shift) & 0x0F;
|
|
|
|
new_sample = (double)FLOAT_TABLE[sample_nibble + shift];
|
|
|
|
new_sample = new_sample + coef1 * hist1 + coef2 * hist2;
|
|
|
|
|
|
|
|
outbuf[sample_count] = clamp16((int)new_sample);
|
|
|
|
hist2 = hist1;
|
|
|
|
hist1 = new_sample;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->adpcm_history1_double = hist1;
|
|
|
|
stream->adpcm_history2_double = hist2;
|
|
|
|
|
|
|
|
/* only increment offset on complete frame */
|
|
|
|
if (i == frame_samples)
|
|
|
|
stream->offset += xa_frame_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-08-05 17:59:35 +02:00
|
|
|
/* EA XA v1 stereo */
|
2017-07-21 19:19:58 +02:00
|
|
|
void decode_ea_xa(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do,int channel) {
|
2008-07-14 21:21:45 +02:00
|
|
|
uint8_t frame_info;
|
2017-07-08 00:27:36 +02:00
|
|
|
int32_t coef1, coef2;
|
|
|
|
int i, sample_count, shift;
|
|
|
|
int hn = (channel==0); /* high nibble marker for stereo subinterleave, ch0/L=high nibble, ch1/R=low nibble */
|
2008-07-14 21:21:45 +02:00
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
int frame_size = 0x1e;
|
|
|
|
int frame_samples = 28;
|
|
|
|
first_sample = first_sample % frame_samples;
|
2008-07-14 21:21:45 +02:00
|
|
|
|
2017-07-08 00:27:36 +02:00
|
|
|
/* header (coefs ch0+ch1 + shift ch0+ch1) */
|
2017-12-03 01:37:56 +01:00
|
|
|
frame_info = read_8bit(stream->offset+0x00,stream->streamfile);
|
2017-07-21 21:03:51 +02:00
|
|
|
coef1 = EA_XA_TABLE[(hn ? frame_info >> 4 : frame_info & 0x0F) + 0];
|
|
|
|
coef2 = EA_XA_TABLE[(hn ? frame_info >> 4 : frame_info & 0x0F) + 4];
|
2017-07-21 20:37:25 +02:00
|
|
|
shift = (frame_info & 0x0F) + 8;
|
2008-07-14 21:21:45 +02:00
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
frame_info = read_8bit(stream->offset+0x01,stream->streamfile);
|
2017-07-08 00:27:36 +02:00
|
|
|
shift = (hn ? frame_info >> 4 : frame_info & 0x0F) + 8;
|
2008-07-14 21:21:45 +02:00
|
|
|
|
2017-07-08 00:27:36 +02:00
|
|
|
/* samples */
|
2017-07-01 23:02:24 +02:00
|
|
|
for (i=first_sample,sample_count=0; i<first_sample+samples_to_do; i++,sample_count+=channelspacing) {
|
2017-07-08 00:27:36 +02:00
|
|
|
uint8_t sample_byte, sample_nibble;
|
2017-12-03 01:37:56 +01:00
|
|
|
int32_t new_sample;
|
|
|
|
off_t byte_offset = (stream->offset + 0x02 + i);
|
|
|
|
int nibble_shift = (hn ? 4 : 0); /* high nibble first */
|
2008-07-14 21:21:45 +02:00
|
|
|
|
2017-07-08 00:27:36 +02:00
|
|
|
sample_byte = (uint8_t)read_8bit(byte_offset,stream->streamfile);
|
2017-12-03 01:37:56 +01:00
|
|
|
sample_nibble = (sample_byte >> nibble_shift) & 0x0F;
|
|
|
|
new_sample = (sample_nibble << 28) >> shift; /* sign extend to 32b and shift */
|
|
|
|
new_sample = (new_sample + coef1 * stream->adpcm_history1_32 + coef2 * stream->adpcm_history2_32 + 128) >> 8;
|
|
|
|
new_sample = clamp16(new_sample);
|
2009-09-01 23:28:55 +02:00
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
outbuf[sample_count] = new_sample;
|
2017-07-01 23:02:24 +02:00
|
|
|
stream->adpcm_history2_32 = stream->adpcm_history1_32;
|
2017-12-03 01:37:56 +01:00
|
|
|
stream->adpcm_history1_32 = new_sample;
|
2017-07-01 23:02:24 +02:00
|
|
|
}
|
2008-07-14 21:21:45 +02:00
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
/* only increment offset on complete frame */
|
|
|
|
if (i == frame_samples)
|
|
|
|
stream->offset += frame_size;
|
2008-07-14 21:21:45 +02:00
|
|
|
}
|
|
|
|
|
2017-07-21 20:37:25 +02:00
|
|
|
/* EA-XA v1 mono/interleave */
|
2017-07-21 19:19:58 +02:00
|
|
|
void decode_ea_xa_int(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do,int channel) {
|
2017-07-08 00:27:36 +02:00
|
|
|
uint8_t frame_info;
|
|
|
|
int32_t coef1, coef2;
|
|
|
|
int i, sample_count, shift;
|
2009-09-01 23:28:55 +02:00
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
int frame_size = 0x0f;
|
|
|
|
int frame_samples = 28;
|
|
|
|
first_sample = first_sample % frame_samples;
|
2017-07-08 00:27:36 +02:00
|
|
|
|
|
|
|
/* header (coefs+shift ch0) */
|
2017-12-03 01:37:56 +01:00
|
|
|
frame_info = read_8bit(stream->offset,stream->streamfile);
|
2017-07-21 21:03:51 +02:00
|
|
|
coef1 = EA_XA_TABLE[(frame_info >> 4) + 0];
|
|
|
|
coef2 = EA_XA_TABLE[(frame_info >> 4) + 4];
|
2017-07-08 00:27:36 +02:00
|
|
|
shift = (frame_info & 0x0F) + 8;
|
|
|
|
|
|
|
|
/* samples */
|
|
|
|
for (i=first_sample,sample_count=0; i<first_sample+samples_to_do; i++,sample_count+=channelspacing) {
|
|
|
|
uint8_t sample_byte, sample_nibble;
|
2017-12-03 01:37:56 +01:00
|
|
|
int32_t new_sample;
|
|
|
|
off_t byte_offset = (stream->offset + 0x01 + i/2);
|
|
|
|
int nibble_shift = (!(i&1)) ? 4 : 0; /* high nibble first */
|
2017-07-01 23:02:24 +02:00
|
|
|
|
2017-07-08 00:27:36 +02:00
|
|
|
sample_byte = (uint8_t)read_8bit(byte_offset,stream->streamfile);
|
2017-12-03 01:37:56 +01:00
|
|
|
sample_nibble = (sample_byte >> nibble_shift) & 0x0F;
|
|
|
|
new_sample = (sample_nibble << 28) >> shift; /* sign extend to 32b and shift */
|
|
|
|
new_sample = (new_sample + coef1 * stream->adpcm_history1_32 + coef2 * stream->adpcm_history2_32 + 128) >> 8;
|
|
|
|
new_sample = clamp16(new_sample);
|
2017-07-01 23:02:24 +02:00
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
outbuf[sample_count] = new_sample;
|
2017-07-08 00:27:36 +02:00
|
|
|
stream->adpcm_history2_32 = stream->adpcm_history1_32;
|
2017-12-03 01:37:56 +01:00
|
|
|
stream->adpcm_history1_32 = new_sample;
|
2017-07-08 00:27:36 +02:00
|
|
|
}
|
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
/* only increment offset on complete frame */
|
|
|
|
if (i == frame_samples)
|
|
|
|
stream->offset += frame_size;
|
2017-07-08 00:27:36 +02:00
|
|
|
}
|
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
/* Maxis EA-XA v1 (mono+stereo) with byte-interleave layout in stereo mode */
|
2017-07-21 19:19:58 +02:00
|
|
|
void decode_maxis_xa(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int channel) {
|
2009-09-01 23:28:55 +02:00
|
|
|
uint8_t frame_info;
|
2017-07-08 00:27:36 +02:00
|
|
|
int32_t coef1, coef2;
|
|
|
|
int i, sample_count, shift;
|
2017-07-01 23:02:24 +02:00
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
int frame_size = 0x0f * channelspacing; /* varies in mono/stereo */
|
|
|
|
int frame_samples = 28;
|
|
|
|
first_sample = first_sample % frame_samples;
|
2017-07-01 23:02:24 +02:00
|
|
|
|
2017-07-08 00:27:36 +02:00
|
|
|
/* header (coefs+shift ch0 + coefs+shift ch1) */
|
2017-12-03 01:37:56 +01:00
|
|
|
frame_info = read_8bit(stream->offset + channel,stream->streamfile);
|
2017-07-21 21:03:51 +02:00
|
|
|
coef1 = EA_XA_TABLE[(frame_info >> 4) + 0];
|
|
|
|
coef2 = EA_XA_TABLE[(frame_info >> 4) + 4];
|
2017-07-21 20:37:25 +02:00
|
|
|
shift = (frame_info & 0x0F) + 8;
|
2017-07-01 23:02:24 +02:00
|
|
|
|
2017-07-08 00:27:36 +02:00
|
|
|
/* samples */
|
2017-07-01 23:02:24 +02:00
|
|
|
for (i=first_sample,sample_count=0; i<first_sample+samples_to_do; i++,sample_count+=channelspacing) {
|
2017-07-08 00:27:36 +02:00
|
|
|
uint8_t sample_byte, sample_nibble;
|
2017-12-03 01:37:56 +01:00
|
|
|
int32_t new_sample;
|
|
|
|
off_t byte_offset = (stream->offset + 0x01*channelspacing + (channelspacing == 2 ? i/2 + channel + (i/2)*0x01 : i/2));
|
|
|
|
int nibble_shift = (!(i&1)) ? 4 : 0; /* high nibble first */
|
2017-07-01 23:02:24 +02:00
|
|
|
|
2017-07-08 00:27:36 +02:00
|
|
|
sample_byte = (uint8_t)read_8bit(byte_offset,stream->streamfile);
|
2017-12-03 01:37:56 +01:00
|
|
|
sample_nibble = (sample_byte >> nibble_shift) & 0x0F;
|
|
|
|
new_sample = (sample_nibble << 28) >> shift; /* sign extend to 32b and shift */
|
|
|
|
new_sample = (new_sample + coef1 * stream->adpcm_history1_32 + coef2 * stream->adpcm_history2_32 + 128) >> 8;
|
|
|
|
new_sample = clamp16(new_sample);
|
2017-07-01 23:02:24 +02:00
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
outbuf[sample_count] = new_sample;
|
2017-07-01 23:02:24 +02:00
|
|
|
stream->adpcm_history2_32 = stream->adpcm_history1_32;
|
2017-12-03 01:37:56 +01:00
|
|
|
stream->adpcm_history1_32 = new_sample;
|
2017-07-01 23:02:24 +02:00
|
|
|
}
|
|
|
|
|
2017-12-03 01:37:56 +01:00
|
|
|
/* only increment offset on complete frame */
|
|
|
|
if (i == frame_samples)
|
|
|
|
stream->offset += frame_size;
|
2009-09-01 23:28:55 +02:00
|
|
|
}
|
2020-06-09 20:32:18 +02:00
|
|
|
|
|
|
|
int32_t ea_xa_bytes_to_samples(size_t bytes, int channels) {
|
|
|
|
if (channels <= 0) return 0;
|
|
|
|
return bytes / channels / 0x0f * 28;
|
|
|
|
}
|