Hi, I have been trying to decode audio files from a video game and was wondering what decoding algorithm it was using. At first glance it looks like a variation of the IMA ADPCM 4-bit to 16-bit decoder since it is using the same exact step size table and index table, but the algorithm does a bit more...
It is reading 4 bytes when decoding a mono file and 6 bytes when decoding a stereo file, for each block, which looks like every block has a little descriptor at its beginning.
Here is the code for decoding a mono channel, to make it simple.
int adpcmDecodeMonoBlock(char *inBuffer, short *outBuffer, int sizeBlock)
{
/* looks like each first 4 bytes of a block contain a ADPCM description */
int sample = *inBuffer;
char index = inBuffer[2];
char *v6 = inBuffer + 3;
int step = step_table[index];
int i = 0;
for (; i < sizeBlock; i++)
{
int delta;
if (i & 1) delta = (*v6++ >> 4) & 0xF; else delta = *v6 & 0xF;
/* compute index */
index += index_table[delta]; /* delta is 4-bit, hence the 16-sized index array */
if (index < 0) index = 0;
else if (index > 88) index = 88;
int s = 0;
if ((delta & 7) & 4) s = 4 * step;
if ((delta & 7) & 2) s += 2 * step;
if ((delta & 7) & 1) s += step;
if (delta & 8) sample -= s >> 2; else sample += s >> 2;
/* keep in 'short' type boundaries */
if (sample > 32767) sample = 32767;
else if (sample < -32768) sample = -32768;
step = step_table[index];
*outBuffer++ = sample;
}
return i; /* return the number of samples read */
}
So my question is, which variant of ADPCM is it?
That looks like the QuickTime variant.
Thank you for answering, but I do not think so. The stereo version of the game's algorithm decodes channels in an interleaved way, which is not the case of the QuickTime ADPCM (source here (https://wiki.multimedia.cx/index.php/Apple_QuickTime_IMA_ADPCM)).
In other words, the game's algorithm decodes left and right channel samples in every block (2 channels included in 1 block), unlike the QuickTime ADPCM variant which decodes one channel per block (2 channels = 2 separate consecutive blocks).