vgmstream/src/coding/ima_decoder.c
halleyscometsw 39e4e02984 STRM's IMA ADPCM added
git-svn-id: https://vgmstream.svn.sourceforge.net/svnroot/vgmstream@21 51a99a44-fe44-0410-b1ba-c3e57ba2b86b
2008-02-05 09:21:20 +00:00

66 lines
2.0 KiB
C

#include "../util.h"
#include "ima_decoder.h"
const int32_t ADPCMTable[89] =
{
7, 8, 9, 10, 11, 12, 13, 14,
16, 17, 19, 21, 23, 25, 28, 31,
34, 37, 41, 45, 50, 55, 60, 66,
73, 80, 88, 97, 107, 118, 130, 143,
157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658,
724, 796, 876, 963, 1060, 1166, 1282, 1411,
1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
32767
};
const int IMA_IndexTable[16] =
{
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8
};
void decode_nds_ima(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do) {
int i=first_sample;
int32_t sample_count;
int32_t hist1 = stream->adpcm_history1_16;
int step_index = stream->adpcm_step_index;
if (first_sample==0) {
step_index = read_16bitLE(stream->offset+2,stream->streamfile);
hist1 = read_16bitLE(stream->offset,stream->streamfile);
}
for (i=first_sample,sample_count=0; i<first_sample+samples_to_do; i++,sample_count+=channelspacing) {
int sample_nibble =
(read_8bit(stream->offset+4+i/2,stream->streamfile) >> (i&1?4:0))&0xf;
int delta;
int step = ADPCMTable[step_index];
delta = step >> 3;
if (sample_nibble & 1) delta += step >> 2;
if (sample_nibble & 2) delta += step >> 1;
if (sample_nibble & 4) delta += step;
if (sample_nibble & 8)
outbuf[sample_count] = clamp16(hist1 - delta);
else
outbuf[sample_count] = clamp16(hist1 + delta);
step_index += IMA_IndexTable[sample_nibble];
if (step_index < 0) step_index=0;
if (step_index > 88) step_index=88;
hist1 = outbuf[sample_count];
}
stream->adpcm_history1_16 = hist1;
stream->adpcm_step_index = step_index;
}