summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/libflash/adpcm.cc
blob: a4bc4356a6bfbc1808231d412e450782869869ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

#include "swf.h"

#ifdef RCSID
static char *rcsid = "$Id$";
#endif

// This file has been rearranged from the code posted
// on news:forums.macromedia.com by Jonathan Gay.
// Courtesy of Macromedia

//
// ADPCM tables
//

static const int indexTable2[2] = {
    -1, 2,
};

// Is this ok?
static const int indexTable3[4] = {
    -1, -1, 2, 4,
};

static const int indexTable4[8] = {
    -1, -1, -1, -1, 2, 4, 6, 8,
};

static const int indexTable5[16] = {
 -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16,
};

static const int* indexTables[] = {
 indexTable2,
 indexTable3,
 indexTable4,
 indexTable5
};

static const int stepsizeTable[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
};

long
Adpcm::GetBits(int n)
{
	if ( bitPos < n ) FillBuffer();

	assert(bitPos >= n);

	long v = ((unsigned long)bitBuf << (32-bitPos)) >> (32-n);
	bitPos -= n;

	return v;
}

long
Adpcm::GetSBits(int n)
{
	if ( bitPos < n ) FillBuffer();

	assert(bitPos >= n);

	long v = ((long)bitBuf << (32-bitPos)) >> (32-n);
	bitPos -= n;

	return v;
}

//
// The Decompressor
//

// Constructor
Adpcm::Adpcm(unsigned char *buffer, long isStereo)
{
	stereo = isStereo;
	src = buffer;

	nBits = 0; // flag that it is not inited
	nSamples = 0;

	bitPos = 0;
	bitBuf = 0;
}

void
Adpcm::FillBuffer()
{
	while ( bitPos <= 24 /*&& srcSize > 0*/ ) {
		bitBuf = (bitBuf<<8) | *src++;
		bitPos += 8;
	}
}

void
Adpcm::Decompress(short *dst, long n)
{
	if ( nBits == 0 ) {
		// Get the compression header
		nBits = (int)GetBits(2)+2;
	}

	const int* indexTable = indexTables[nBits-2];
	int k0 = 1 << (nBits-2);
	int signmask = 1 << (nBits-1);

	if ( !stereo ) {
		// Optimize for mono
		long		vp = valpred[0]; // maybe these can get into registers...
		int		ind = index[0];
		long		ns = nSamples;

		while ( n-- > 0 ) {
			ns++;

			if ( (ns & 0xFFF) == 1 ) {
				// Get a new block header
				*dst++ = (short)(vp = GetSBits(16));

				ind = (int)GetBits(6); // The first sample in a block does not have a delta
			} else {
				// Process a delta value
				int delta = (int)GetBits(nBits);

				// Compute difference and new predicted value
				// Computes 'vpdiff = (delta+0.5)*step/4'
				int step = stepsizeTable[ind];
				long vpdiff = 0;
				int k = k0;

				do {
					if ( delta & k )
					vpdiff += step;
					step >>= 1;
					k >>= 1;
				} while ( k );

				vpdiff += step; // add 0.5

				if ( delta & signmask ) // the sign bit
					vp -= vpdiff;
				else
					vp += vpdiff;

				// Find new index value
				ind += indexTable[delta&(~signmask)];

				if ( ind < 0 )
					ind = 0;
				else if ( ind > 88 )
					ind = 88;

				// clamp output value
				if ( vp != (short)vp )
					vp = vp < 0 ? -32768 : 32767;

				/* Step 7 - Output value */
				*dst++ = (short)vp;
			}
		}

		valpred[0] = vp;
		index[0] = ind;
		nSamples = ns;

	} else {
		int sn = stereo ? 2 : 1;

		// Stereo
		while ( n-- > 0 ) {

			nSamples++;

			if ( (nSamples & 0xFFF) == 1 ) {
				// Get a new block header
				for ( int i = 0; i < sn; i++ ) {

					*dst++ = (short)(valpred[i] = GetSBits(16));

					// The first sample in a block does not have a delta
					index[i] = (int)GetBits(6);
				}
			} else {
				// Process a delta value
				for ( int i = 0; i < sn; i++ ) {
					int delta = (int)GetBits(nBits);

					// Compute difference and new predicted value
					// Computes 'vpdiff = (delta+0.5)*step/4'

					int step = stepsizeTable[index[i]];
					long vpdiff = 0;
					int k = k0;

					do {
						if ( delta & k ) vpdiff += step;
						step >>= 1;
						k >>= 1;
					} while ( k );
					vpdiff += step; // add 0.5


					if ( delta & signmask ) // the sign bit
						valpred[i] -= vpdiff;
					else
						valpred[i] += vpdiff;

					// Find new index value
					index[i] += indexTable[delta&(~signmask)];

					if ( index[i] < 0 )
						index[i] = 0;
					else if ( index[i] > 88 )
						index[i] = 88;

					// clamp output value
					if ( valpred[i] != (short)valpred[i] )
						valpred[i] = valpred[i] < 0 ? -32768 : 32767;

					/* Step 7 - Output value */
					*dst++ = (short)valpred[i];
				}
			}
		}
	}
}