Use ALsizei for some count/length variables
[openal-soft.git] / OpenAL32 / sample_cvt.c
blob58cad523580810993c0c7b1e64093c49ad3e5ee2
2 #include "config.h"
4 #include "sample_cvt.h"
6 #ifdef HAVE_ALLOCA_H
7 #include <alloca.h>
8 #endif
9 #ifdef HAVE_MALLOC_H
10 #include <malloc.h>
11 #endif
13 #include "AL/al.h"
14 #include "alu.h"
15 #include "alBuffer.h"
18 /* IMA ADPCM Stepsize table */
19 static const int IMAStep_size[89] = {
20 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19,
21 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
22 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
23 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
24 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
25 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660,
26 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,
27 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794,
28 32767
31 /* IMA4 ADPCM Codeword decode table */
32 static const int IMA4Codeword[16] = {
33 1, 3, 5, 7, 9, 11, 13, 15,
34 -1,-3,-5,-7,-9,-11,-13,-15,
37 /* IMA4 ADPCM Step index adjust decode table */
38 static const int IMA4Index_adjust[16] = {
39 -1,-1,-1,-1, 2, 4, 6, 8,
40 -1,-1,-1,-1, 2, 4, 6, 8
44 /* MSADPCM Adaption table */
45 static const int MSADPCMAdaption[16] = {
46 230, 230, 230, 230, 307, 409, 512, 614,
47 768, 614, 512, 409, 307, 230, 230, 230
50 /* MSADPCM Adaption Coefficient tables */
51 static const int MSADPCMAdaptionCoeff[7][2] = {
52 { 256, 0 },
53 { 512, -256 },
54 { 0, 0 },
55 { 192, 64 },
56 { 240, 0 },
57 { 460, -208 },
58 { 392, -232 }
62 /* A quick'n'dirty lookup table to decode a muLaw-encoded byte sample into a
63 * signed 16-bit sample */
64 static const ALshort muLawDecompressionTable[256] = {
65 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
66 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
67 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
68 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
69 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
70 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
71 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
72 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
73 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
74 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
75 -876, -844, -812, -780, -748, -716, -684, -652,
76 -620, -588, -556, -524, -492, -460, -428, -396,
77 -372, -356, -340, -324, -308, -292, -276, -260,
78 -244, -228, -212, -196, -180, -164, -148, -132,
79 -120, -112, -104, -96, -88, -80, -72, -64,
80 -56, -48, -40, -32, -24, -16, -8, 0,
81 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
82 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
83 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
84 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
85 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
86 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
87 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
88 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
89 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
90 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
91 876, 844, 812, 780, 748, 716, 684, 652,
92 620, 588, 556, 524, 492, 460, 428, 396,
93 372, 356, 340, 324, 308, 292, 276, 260,
94 244, 228, 212, 196, 180, 164, 148, 132,
95 120, 112, 104, 96, 88, 80, 72, 64,
96 56, 48, 40, 32, 24, 16, 8, 0
99 /* Values used when encoding a muLaw sample */
100 static const int muLawBias = 0x84;
101 static const int muLawClip = 32635;
102 static const char muLawCompressTable[256] = {
103 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
104 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
105 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
106 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
107 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
108 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
109 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
110 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
111 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
112 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
113 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
114 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
115 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
116 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
117 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
118 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
122 /* A quick'n'dirty lookup table to decode an aLaw-encoded byte sample into a
123 * signed 16-bit sample */
124 static const ALshort aLawDecompressionTable[256] = {
125 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
126 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
127 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
128 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
129 -22016,-20992,-24064,-23040,-17920,-16896,-19968,-18944,
130 -30208,-29184,-32256,-31232,-26112,-25088,-28160,-27136,
131 -11008,-10496,-12032,-11520, -8960, -8448, -9984, -9472,
132 -15104,-14592,-16128,-15616,-13056,-12544,-14080,-13568,
133 -344, -328, -376, -360, -280, -264, -312, -296,
134 -472, -456, -504, -488, -408, -392, -440, -424,
135 -88, -72, -120, -104, -24, -8, -56, -40,
136 -216, -200, -248, -232, -152, -136, -184, -168,
137 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
138 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
139 -688, -656, -752, -720, -560, -528, -624, -592,
140 -944, -912, -1008, -976, -816, -784, -880, -848,
141 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
142 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
143 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
144 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
145 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
146 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
147 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
148 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
149 344, 328, 376, 360, 280, 264, 312, 296,
150 472, 456, 504, 488, 408, 392, 440, 424,
151 88, 72, 120, 104, 24, 8, 56, 40,
152 216, 200, 248, 232, 152, 136, 184, 168,
153 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
154 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
155 688, 656, 752, 720, 560, 528, 624, 592,
156 944, 912, 1008, 976, 816, 784, 880, 848
159 /* Values used when encoding an aLaw sample */
160 static const int aLawClip = 32635;
161 static const char aLawCompressTable[128] = {
162 1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
163 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
164 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
165 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
166 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
167 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
168 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
169 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
173 typedef ALubyte ALmulaw;
174 typedef ALubyte ALalaw;
175 typedef ALubyte ALima4;
176 typedef ALubyte ALmsadpcm;
178 static inline ALshort DecodeMuLaw(ALmulaw val)
179 { return muLawDecompressionTable[val]; }
181 static ALmulaw EncodeMuLaw(ALshort val)
183 ALint mant, exp, sign;
185 sign = (val>>8) & 0x80;
186 if(sign)
188 /* -32768 doesn't properly negate on a short; it results in itself.
189 * So clamp to -32767 */
190 val = maxi(val, -32767);
191 val = -val;
194 val = mini(val, muLawClip);
195 val += muLawBias;
197 exp = muLawCompressTable[(val>>7) & 0xff];
198 mant = (val >> (exp+3)) & 0x0f;
200 return ~(sign | (exp<<4) | mant);
203 static inline ALshort DecodeALaw(ALalaw val)
204 { return aLawDecompressionTable[val]; }
206 static ALalaw EncodeALaw(ALshort val)
208 ALint mant, exp, sign;
210 sign = ((~val) >> 8) & 0x80;
211 if(!sign)
213 val = maxi(val, -32767);
214 val = -val;
216 val = mini(val, aLawClip);
218 if(val >= 256)
220 exp = aLawCompressTable[(val>>8) & 0x7f];
221 mant = (val >> (exp+3)) & 0x0f;
223 else
225 exp = 0;
226 mant = val >> 4;
229 return ((exp<<4) | mant) ^ (sign^0x55);
232 static void DecodeIMA4Block(ALshort *dst, const ALima4 *src, ALint numchans, ALsizei align)
234 ALint sample[MAX_INPUT_CHANNELS], index[MAX_INPUT_CHANNELS];
235 ALuint code[MAX_INPUT_CHANNELS];
236 ALsizei j,k,c;
238 for(c = 0;c < numchans;c++)
240 sample[c] = *(src++);
241 sample[c] |= *(src++) << 8;
242 sample[c] = (sample[c]^0x8000) - 32768;
243 index[c] = *(src++);
244 index[c] |= *(src++) << 8;
245 index[c] = (index[c]^0x8000) - 32768;
247 index[c] = clampi(index[c], 0, 88);
249 dst[c] = sample[c];
252 for(j = 1;j < align;j += 8)
254 for(c = 0;c < numchans;c++)
256 code[c] = *(src++);
257 code[c] |= *(src++) << 8;
258 code[c] |= *(src++) << 16;
259 code[c] |= *(src++) << 24;
262 for(k = 0;k < 8;k++)
264 for(c = 0;c < numchans;c++)
266 int nibble = code[c]&0xf;
267 code[c] >>= 4;
269 sample[c] += IMA4Codeword[nibble] * IMAStep_size[index[c]] / 8;
270 sample[c] = clampi(sample[c], -32768, 32767);
272 index[c] += IMA4Index_adjust[nibble];
273 index[c] = clampi(index[c], 0, 88);
275 dst[(j+k)*numchans + c] = sample[c];
281 static void EncodeIMA4Block(ALima4 *dst, const ALshort *src, ALint *sample, ALint *index, ALint numchans, ALsizei align)
283 ALsizei j,k,c;
285 for(c = 0;c < numchans;c++)
287 int diff = src[c] - sample[c];
288 int step = IMAStep_size[index[c]];
289 int nibble;
291 nibble = 0;
292 if(diff < 0)
294 nibble = 0x8;
295 diff = -diff;
298 diff = mini(step*2, diff);
299 nibble |= (diff*8/step - 1) / 2;
301 sample[c] += IMA4Codeword[nibble] * step / 8;
302 sample[c] = clampi(sample[c], -32768, 32767);
304 index[c] += IMA4Index_adjust[nibble];
305 index[c] = clampi(index[c], 0, 88);
307 *(dst++) = sample[c] & 0xff;
308 *(dst++) = (sample[c]>>8) & 0xff;
309 *(dst++) = index[c] & 0xff;
310 *(dst++) = (index[c]>>8) & 0xff;
313 for(j = 1;j < align;j += 8)
315 for(c = 0;c < numchans;c++)
317 for(k = 0;k < 8;k++)
319 int diff = src[(j+k)*numchans + c] - sample[c];
320 int step = IMAStep_size[index[c]];
321 int nibble;
323 nibble = 0;
324 if(diff < 0)
326 nibble = 0x8;
327 diff = -diff;
330 diff = mini(step*2, diff);
331 nibble |= (diff*8/step - 1) / 2;
333 sample[c] += IMA4Codeword[nibble] * step / 8;
334 sample[c] = clampi(sample[c], -32768, 32767);
336 index[c] += IMA4Index_adjust[nibble];
337 index[c] = clampi(index[c], 0, 88);
339 if(!(k&1)) *dst = nibble;
340 else *(dst++) |= nibble<<4;
347 static void DecodeMSADPCMBlock(ALshort *dst, const ALmsadpcm *src, ALint numchans, ALsizei align)
349 ALubyte blockpred[MAX_INPUT_CHANNELS];
350 ALint delta[MAX_INPUT_CHANNELS];
351 ALshort samples[MAX_INPUT_CHANNELS][2];
352 ALint i, j;
354 for(i = 0;i < numchans;i++)
356 blockpred[i] = *(src++);
357 blockpred[i] = minu(blockpred[i], 6);
359 for(i = 0;i < numchans;i++)
361 delta[i] = *(src++);
362 delta[i] |= *(src++) << 8;
363 delta[i] = (delta[i]^0x8000) - 0x8000;
365 for(i = 0;i < numchans;i++)
367 samples[i][0] = *(src++);
368 samples[i][0] |= *(src++) << 8;
369 samples[i][0] = (samples[i][0]^0x8000) - 0x8000;
371 for(i = 0;i < numchans;i++)
373 samples[i][1] = *(src++);
374 samples[i][1] |= *(src++) << 8;
375 samples[i][1] = (samples[i][1]^0x8000) - 0x8000;
378 /* Second sample is written first. */
379 for(i = 0;i < numchans;i++)
380 *(dst++) = samples[i][1];
381 for(i = 0;i < numchans;i++)
382 *(dst++) = samples[i][0];
384 for(j = 2;j < align;j++)
386 for(i = 0;i < numchans;i++)
388 const ALint num = (j*numchans) + i;
389 ALint nibble, pred;
391 /* Read the nibble (first is in the upper bits). */
392 if(!(num&1))
393 nibble = (*src>>4)&0x0f;
394 else
395 nibble = (*(src++))&0x0f;
397 pred = (samples[i][0]*MSADPCMAdaptionCoeff[blockpred[i]][0] +
398 samples[i][1]*MSADPCMAdaptionCoeff[blockpred[i]][1]) / 256;
399 pred += ((nibble^0x08) - 0x08) * delta[i];
400 pred = clampi(pred, -32768, 32767);
402 samples[i][1] = samples[i][0];
403 samples[i][0] = pred;
405 delta[i] = (MSADPCMAdaption[nibble] * delta[i]) / 256;
406 delta[i] = maxi(16, delta[i]);
408 *(dst++) = pred;
413 /* NOTE: This encoder is pretty dumb/simplistic. Some kind of pre-processing
414 * that tries to find the optimal block predictors would be nice, at least. A
415 * multi-pass method that can generate better deltas would be good, too. */
416 static void EncodeMSADPCMBlock(ALmsadpcm *dst, const ALshort *src, ALint *sample, ALint numchans, ALsizei align)
418 ALubyte blockpred[MAX_INPUT_CHANNELS];
419 ALint delta[MAX_INPUT_CHANNELS];
420 ALshort samples[MAX_INPUT_CHANNELS][2];
421 ALint i, j;
423 /* Block predictor */
424 for(i = 0;i < numchans;i++)
426 /* FIXME: Calculate something better. */
427 blockpred[i] = 0;
428 *(dst++) = blockpred[i];
430 /* Initial delta */
431 for(i = 0;i < numchans;i++)
433 delta[i] = 16;
434 *(dst++) = (delta[i] ) & 0xff;
435 *(dst++) = (delta[i]>>8) & 0xff;
437 /* Initial sample 1 */
438 for(i = 0;i < numchans;i++)
440 samples[i][0] = src[1*numchans + i];
441 *(dst++) = (samples[i][0] ) & 0xff;
442 *(dst++) = (samples[i][0]>>8) & 0xff;
444 /* Initial sample 2 */
445 for(i = 0;i < numchans;i++)
447 samples[i][1] = src[i];
448 *(dst++) = (samples[i][1] ) & 0xff;
449 *(dst++) = (samples[i][1]>>8) & 0xff;
452 for(j = 2;j < align;j++)
454 for(i = 0;i < numchans;i++)
456 const ALint num = (j*numchans) + i;
457 ALint nibble = 0;
458 ALint bias;
460 sample[i] = (samples[i][0]*MSADPCMAdaptionCoeff[blockpred[i]][0] +
461 samples[i][1]*MSADPCMAdaptionCoeff[blockpred[i]][1]) / 256;
463 nibble = src[num] - sample[i];
464 if(nibble >= 0)
465 bias = delta[i] / 2;
466 else
467 bias = -delta[i] / 2;
469 nibble = (nibble + bias) / delta[i];
470 nibble = clampi(nibble, -8, 7)&0x0f;
472 sample[i] += ((nibble^0x08)-0x08) * delta[i];
473 sample[i] = clampi(sample[i], -32768, 32767);
475 samples[i][1] = samples[i][0];
476 samples[i][0] = sample[i];
478 delta[i] = (MSADPCMAdaption[nibble] * delta[i]) / 256;
479 delta[i] = maxi(16, delta[i]);
481 if(!(num&1))
482 *dst = nibble << 4;
483 else
485 *dst |= nibble;
486 dst++;
493 /* Define same-type pass-through sample conversion functions (excludes ADPCM,
494 * which are block-based). */
495 #define DECL_TEMPLATE(T) \
496 static inline T Conv_##T##_##T(T val) { return val; }
498 DECL_TEMPLATE(ALbyte);
499 DECL_TEMPLATE(ALubyte);
500 DECL_TEMPLATE(ALshort);
501 DECL_TEMPLATE(ALushort);
502 DECL_TEMPLATE(ALint);
503 DECL_TEMPLATE(ALuint);
504 DECL_TEMPLATE(ALalaw);
505 DECL_TEMPLATE(ALmulaw);
507 /* Slightly special handling for floats and doubles (converts NaN to 0, and
508 * allows float<->double pass-through).
510 static inline ALfloat Conv_ALfloat_ALfloat(ALfloat val)
511 { return (val==val) ? val : 0.0f; }
512 static inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
513 { return (val==val) ? (ALfloat)val : 0.0f; }
514 static inline ALdouble Conv_ALdouble_ALfloat(ALfloat val)
515 { return (val==val) ? (ALdouble)val : 0.0; }
516 static inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
517 { return (val==val) ? val : 0.0; }
519 #undef DECL_TEMPLATE
521 /* Define alternate-sign functions. */
522 #define DECL_TEMPLATE(T1, T2, O) \
523 static inline T1 Conv_##T1##_##T2(T2 val) { return (T1)val - O; } \
524 static inline T2 Conv_##T2##_##T1(T1 val) { return (T2)val + O; }
526 DECL_TEMPLATE(ALbyte, ALubyte, 128);
527 DECL_TEMPLATE(ALshort, ALushort, 32768);
528 DECL_TEMPLATE(ALint, ALuint, 2147483648u);
530 #undef DECL_TEMPLATE
532 /* Define int-type to int-type functions */
533 #define DECL_TEMPLATE(T, ST, UT, SH) \
534 static inline T Conv_##T##_##ST(ST val){ return val >> SH; } \
535 static inline T Conv_##T##_##UT(UT val){ return Conv_##ST##_##UT(val) >> SH; }\
536 static inline ST Conv_##ST##_##T(T val){ return val << SH; } \
537 static inline UT Conv_##UT##_##T(T val){ return Conv_##UT##_##ST(val << SH); }
539 #define DECL_TEMPLATE2(T1, T2, SH) \
540 DECL_TEMPLATE(AL##T1, AL##T2, ALu##T2, SH) \
541 DECL_TEMPLATE(ALu##T1, ALu##T2, AL##T2, SH)
543 DECL_TEMPLATE2(byte, short, 8)
544 DECL_TEMPLATE2(short, int, 16)
545 DECL_TEMPLATE2(byte, int, 24)
547 #undef DECL_TEMPLATE2
548 #undef DECL_TEMPLATE
550 /* Define int-type to fp functions */
551 #define DECL_TEMPLATE(T, ST, UT, OP) \
552 static inline T Conv_##T##_##ST(ST val) { return (T)val * OP; } \
553 static inline T Conv_##T##_##UT(UT val) { return (T)Conv_##ST##_##UT(val) * OP; }
555 #define DECL_TEMPLATE2(T1, T2, OP) \
556 DECL_TEMPLATE(T1, AL##T2, ALu##T2, OP)
558 DECL_TEMPLATE2(ALfloat, byte, (1.0f/128.0f))
559 DECL_TEMPLATE2(ALdouble, byte, (1.0/128.0))
560 DECL_TEMPLATE2(ALfloat, short, (1.0f/32768.0f))
561 DECL_TEMPLATE2(ALdouble, short, (1.0/32768.0))
562 DECL_TEMPLATE2(ALdouble, int, (1.0/2147483648.0))
564 /* Special handling for int32 to float32, since it would overflow. */
565 static inline ALfloat Conv_ALfloat_ALint(ALint val)
566 { return (ALfloat)(val>>7) * (1.0f/16777216.0f); }
567 static inline ALfloat Conv_ALfloat_ALuint(ALuint val)
568 { return (ALfloat)(Conv_ALint_ALuint(val)>>7) * (1.0f/16777216.0f); }
570 #undef DECL_TEMPLATE2
571 #undef DECL_TEMPLATE
573 /* Define fp to int-type functions */
574 #define DECL_TEMPLATE(FT, T, smin, smax) \
575 static inline AL##T Conv_AL##T##_##FT(FT val) \
577 val *= (FT)smax + 1; \
578 if(val >= (FT)smax) return smax; \
579 if(val <= (FT)smin) return smin; \
580 return (AL##T)val; \
582 static inline ALu##T Conv_ALu##T##_##FT(FT val) \
583 { return Conv_ALu##T##_AL##T(Conv_AL##T##_##FT(val)); }
585 DECL_TEMPLATE(ALfloat, byte, -128, 127)
586 DECL_TEMPLATE(ALdouble, byte, -128, 127)
587 DECL_TEMPLATE(ALfloat, short, -32768, 32767)
588 DECL_TEMPLATE(ALdouble, short, -32768, 32767)
589 DECL_TEMPLATE(ALdouble, int, -2147483647-1, 2147483647)
591 /* Special handling for float32 to int32, since it would overflow. */
592 static inline ALint Conv_ALint_ALfloat(ALfloat val)
594 val *= 16777216.0f;
595 if(val >= 16777215.0f) return 0x7fffff80/*16777215 << 7*/;
596 if(val <= -16777216.0f) return 0x80000000/*-16777216 << 7*/;
597 return (ALint)val << 7;
599 static inline ALuint Conv_ALuint_ALfloat(ALfloat val)
600 { return Conv_ALuint_ALint(Conv_ALint_ALfloat(val)); }
602 #undef DECL_TEMPLATE
604 /* Define muLaw and aLaw functions (goes through short functions). */
605 #define DECL_TEMPLATE(T) \
606 static inline ALmulaw Conv_ALmulaw_##T(T val) \
607 { return EncodeMuLaw(Conv_ALshort_##T(val)); } \
608 static inline T Conv_##T##_ALmulaw(ALmulaw val) \
609 { return Conv_##T##_ALshort(DecodeMuLaw(val)); } \
611 static inline ALalaw Conv_ALalaw_##T(T val) \
612 { return EncodeALaw(Conv_ALshort_##T(val)); } \
613 static inline T Conv_##T##_ALalaw(ALalaw val) \
614 { return Conv_##T##_ALshort(DecodeALaw(val)); }
616 DECL_TEMPLATE(ALbyte)
617 DECL_TEMPLATE(ALubyte)
618 DECL_TEMPLATE(ALshort)
619 DECL_TEMPLATE(ALushort)
620 DECL_TEMPLATE(ALint)
621 DECL_TEMPLATE(ALuint)
622 DECL_TEMPLATE(ALfloat)
623 DECL_TEMPLATE(ALdouble)
625 #undef DECL_TEMPLATE
627 /* Define muLaw <-> aLaw functions. */
628 static inline ALalaw Conv_ALalaw_ALmulaw(ALmulaw val)
629 { return EncodeALaw(DecodeMuLaw(val)); }
630 static inline ALmulaw Conv_ALmulaw_ALalaw(ALalaw val)
631 { return EncodeMuLaw(DecodeALaw(val)); }
634 #define DECL_TEMPLATE(T1, T2) \
635 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALsizei numchans, \
636 ALsizei len, ALsizei UNUSED(align)) \
638 ALsizei i; \
639 len *= numchans; \
640 for(i = 0;i < len;i++) \
641 *(dst++) = Conv_##T1##_##T2(*(src++)); \
644 #define DECL_TEMPLATE2(T) \
645 DECL_TEMPLATE(T, ALbyte) \
646 DECL_TEMPLATE(T, ALubyte) \
647 DECL_TEMPLATE(T, ALshort) \
648 DECL_TEMPLATE(T, ALushort) \
649 DECL_TEMPLATE(T, ALint) \
650 DECL_TEMPLATE(T, ALuint) \
651 DECL_TEMPLATE(T, ALfloat) \
652 DECL_TEMPLATE(T, ALdouble) \
653 DECL_TEMPLATE(T, ALmulaw) \
654 DECL_TEMPLATE(T, ALalaw)
656 DECL_TEMPLATE2(ALbyte)
657 DECL_TEMPLATE2(ALubyte)
658 DECL_TEMPLATE2(ALshort)
659 DECL_TEMPLATE2(ALushort)
660 DECL_TEMPLATE2(ALint)
661 DECL_TEMPLATE2(ALuint)
662 DECL_TEMPLATE2(ALfloat)
663 DECL_TEMPLATE2(ALdouble)
664 DECL_TEMPLATE2(ALmulaw)
665 DECL_TEMPLATE2(ALalaw)
667 #undef DECL_TEMPLATE2
668 #undef DECL_TEMPLATE
670 #define DECL_TEMPLATE(T) \
671 static void Convert_##T##_ALima4(T *dst, const ALima4 *src, ALsizei numchans, \
672 ALsizei len, ALsizei align) \
674 ALsizei byte_align = ((align-1)/2 + 4) * numchans; \
675 DECL_VLA(ALshort, tmp, align*numchans); \
676 ALsizei i, j, k; \
678 assert(align > 0 && (len%align) == 0); \
679 for(i = 0;i < len;i += align) \
681 DecodeIMA4Block(tmp, src, numchans, align); \
682 src += byte_align; \
684 for(j = 0;j < align;j++) \
686 for(k = 0;k < numchans;k++) \
687 *(dst++) = Conv_##T##_ALshort(tmp[j*numchans + k]); \
692 DECL_TEMPLATE(ALbyte)
693 DECL_TEMPLATE(ALubyte)
694 static void Convert_ALshort_ALima4(ALshort *dst, const ALima4 *src, ALsizei numchans,
695 ALsizei len, ALsizei align)
697 ALsizei byte_align = ((align-1)/2 + 4) * numchans;
698 ALsizei i;
700 assert(align > 0 && (len%align) == 0);
701 for(i = 0;i < len;i += align)
703 DecodeIMA4Block(dst, src, numchans, align);
704 src += byte_align;
705 dst += align*numchans;
708 DECL_TEMPLATE(ALushort)
709 DECL_TEMPLATE(ALint)
710 DECL_TEMPLATE(ALuint)
711 DECL_TEMPLATE(ALfloat)
712 DECL_TEMPLATE(ALdouble)
713 DECL_TEMPLATE(ALmulaw)
714 DECL_TEMPLATE(ALalaw)
716 #undef DECL_TEMPLATE
718 #define DECL_TEMPLATE(T) \
719 static void Convert_ALima4_##T(ALima4 *dst, const T *src, ALsizei numchans, \
720 ALsizei len, ALsizei align) \
722 ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
723 ALint index[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
724 ALsizei byte_align = ((align-1)/2 + 4) * numchans; \
725 DECL_VLA(ALshort, tmp, align*numchans); \
726 ALsizei i, j, k; \
728 assert(align > 0 && (len%align) == 0); \
729 for(i = 0;i < len;i += align) \
731 for(j = 0;j < align;j++) \
733 for(k = 0;k < numchans;k++) \
734 tmp[j*numchans + k] = Conv_ALshort_##T(*(src++)); \
736 EncodeIMA4Block(dst, tmp, sample, index, numchans, align); \
737 dst += byte_align; \
741 DECL_TEMPLATE(ALbyte)
742 DECL_TEMPLATE(ALubyte)
743 static void Convert_ALima4_ALshort(ALima4 *dst, const ALshort *src,
744 ALsizei numchans, ALsizei len, ALsizei align)
746 ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
747 ALint index[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
748 ALsizei byte_align = ((align-1)/2 + 4) * numchans;
749 ALsizei i;
751 assert(align > 0 && (len%align) == 0);
752 for(i = 0;i < len;i += align)
754 EncodeIMA4Block(dst, src, sample, index, numchans, align);
755 src += align*numchans;
756 dst += byte_align;
759 DECL_TEMPLATE(ALushort)
760 DECL_TEMPLATE(ALint)
761 DECL_TEMPLATE(ALuint)
762 DECL_TEMPLATE(ALfloat)
763 DECL_TEMPLATE(ALdouble)
764 DECL_TEMPLATE(ALmulaw)
765 DECL_TEMPLATE(ALalaw)
767 #undef DECL_TEMPLATE
770 #define DECL_TEMPLATE(T) \
771 static void Convert_##T##_ALmsadpcm(T *dst, const ALmsadpcm *src, \
772 ALsizei numchans, ALsizei len, \
773 ALsizei align) \
775 ALsizei byte_align = ((align-2)/2 + 7) * numchans; \
776 DECL_VLA(ALshort, tmp, align*numchans); \
777 ALsizei i, j, k; \
779 assert(align > 1 && (len%align) == 0); \
780 for(i = 0;i < len;i += align) \
782 DecodeMSADPCMBlock(tmp, src, numchans, align); \
783 src += byte_align; \
785 for(j = 0;j < align;j++) \
787 for(k = 0;k < numchans;k++) \
788 *(dst++) = Conv_##T##_ALshort(tmp[j*numchans + k]); \
793 DECL_TEMPLATE(ALbyte)
794 DECL_TEMPLATE(ALubyte)
795 static void Convert_ALshort_ALmsadpcm(ALshort *dst, const ALmsadpcm *src,
796 ALsizei numchans, ALsizei len,
797 ALsizei align)
799 ALsizei byte_align = ((align-2)/2 + 7) * numchans;
800 ALsizei i;
802 assert(align > 1 && (len%align) == 0);
803 for(i = 0;i < len;i += align)
805 DecodeMSADPCMBlock(dst, src, numchans, align);
806 src += byte_align;
807 dst += align*numchans;
810 DECL_TEMPLATE(ALushort)
811 DECL_TEMPLATE(ALint)
812 DECL_TEMPLATE(ALuint)
813 DECL_TEMPLATE(ALfloat)
814 DECL_TEMPLATE(ALdouble)
815 DECL_TEMPLATE(ALmulaw)
816 DECL_TEMPLATE(ALalaw)
818 #undef DECL_TEMPLATE
820 #define DECL_TEMPLATE(T) \
821 static void Convert_ALmsadpcm_##T(ALmsadpcm *dst, const T *src, \
822 ALsizei numchans, ALsizei len, \
823 ALsizei align) \
825 ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
826 ALsizei byte_align = ((align-2)/2 + 7) * numchans; \
827 DECL_VLA(ALshort, tmp, align*numchans); \
828 ALsizei i, j, k; \
830 assert(align > 1 && (len%align) == 0); \
831 for(i = 0;i < len;i += align) \
833 for(j = 0;j < align;j++) \
835 for(k = 0;k < numchans;k++) \
836 tmp[j*numchans + k] = Conv_ALshort_##T(*(src++)); \
838 EncodeMSADPCMBlock(dst, tmp, sample, numchans, align); \
839 dst += byte_align; \
843 DECL_TEMPLATE(ALbyte)
844 DECL_TEMPLATE(ALubyte)
845 static void Convert_ALmsadpcm_ALshort(ALmsadpcm *dst, const ALshort *src,
846 ALsizei numchans, ALsizei len, ALsizei align)
848 ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
849 ALsizei byte_align = ((align-2)/2 + 7) * numchans;
850 ALsizei i;
852 assert(align > 1 && (len%align) == 0);
853 for(i = 0;i < len;i += align)
855 EncodeMSADPCMBlock(dst, src, sample, numchans, align);
856 src += align*numchans;
857 dst += byte_align;
860 DECL_TEMPLATE(ALushort)
861 DECL_TEMPLATE(ALint)
862 DECL_TEMPLATE(ALuint)
863 DECL_TEMPLATE(ALfloat)
864 DECL_TEMPLATE(ALdouble)
865 DECL_TEMPLATE(ALmulaw)
866 DECL_TEMPLATE(ALalaw)
868 #undef DECL_TEMPLATE
870 /* NOTE: We don't store compressed samples internally, so these conversions
871 * should never happen. */
872 static void Convert_ALima4_ALima4(ALima4* UNUSED(dst), const ALima4* UNUSED(src),
873 ALsizei UNUSED(numchans), ALsizei UNUSED(len),
874 ALsizei UNUSED(align))
876 ERR("Unexpected IMA4-to-IMA4 conversion!\n");
879 static void Convert_ALmsadpcm_ALmsadpcm(ALmsadpcm* UNUSED(dst), const ALmsadpcm* UNUSED(src),
880 ALsizei UNUSED(numchans), ALsizei UNUSED(len),
881 ALsizei UNUSED(align))
883 ERR("Unexpected MSADPCM-to-MSADPCM conversion!\n");
886 static void Convert_ALmsadpcm_ALima4(ALmsadpcm* UNUSED(dst), const ALima4* UNUSED(src),
887 ALsizei UNUSED(numchans), ALsizei UNUSED(len),
888 ALsizei UNUSED(align))
890 ERR("Unexpected IMA4-to-MSADPCM conversion!\n");
893 static void Convert_ALima4_ALmsadpcm(ALima4* UNUSED(dst), const ALmsadpcm* UNUSED(src),
894 ALsizei UNUSED(numchans), ALsizei UNUSED(len),
895 ALsizei UNUSED(align))
897 ERR("Unexpected MSADPCM-to-IMA4 conversion!\n");
901 #define DECL_TEMPLATE(T) \
902 static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
903 ALsizei numchans, ALsizei len, ALsizei align) \
905 switch(srcType) \
907 case UserFmtByte: \
908 Convert_##T##_ALbyte(dst, src, numchans, len, align); \
909 break; \
910 case UserFmtUByte: \
911 Convert_##T##_ALubyte(dst, src, numchans, len, align); \
912 break; \
913 case UserFmtShort: \
914 Convert_##T##_ALshort(dst, src, numchans, len, align); \
915 break; \
916 case UserFmtUShort: \
917 Convert_##T##_ALushort(dst, src, numchans, len, align); \
918 break; \
919 case UserFmtInt: \
920 Convert_##T##_ALint(dst, src, numchans, len, align); \
921 break; \
922 case UserFmtUInt: \
923 Convert_##T##_ALuint(dst, src, numchans, len, align); \
924 break; \
925 case UserFmtFloat: \
926 Convert_##T##_ALfloat(dst, src, numchans, len, align); \
927 break; \
928 case UserFmtDouble: \
929 Convert_##T##_ALdouble(dst, src, numchans, len, align); \
930 break; \
931 case UserFmtMulaw: \
932 Convert_##T##_ALmulaw(dst, src, numchans, len, align); \
933 break; \
934 case UserFmtAlaw: \
935 Convert_##T##_ALalaw(dst, src, numchans, len, align); \
936 break; \
937 case UserFmtIMA4: \
938 Convert_##T##_ALima4(dst, src, numchans, len, align); \
939 break; \
940 case UserFmtMSADPCM: \
941 Convert_##T##_ALmsadpcm(dst, src, numchans, len, align); \
942 break; \
946 DECL_TEMPLATE(ALbyte)
947 DECL_TEMPLATE(ALubyte)
948 DECL_TEMPLATE(ALshort)
949 DECL_TEMPLATE(ALushort)
950 DECL_TEMPLATE(ALint)
951 DECL_TEMPLATE(ALuint)
952 DECL_TEMPLATE(ALfloat)
953 DECL_TEMPLATE(ALdouble)
954 DECL_TEMPLATE(ALmulaw)
955 DECL_TEMPLATE(ALalaw)
956 DECL_TEMPLATE(ALima4)
957 DECL_TEMPLATE(ALmsadpcm)
959 #undef DECL_TEMPLATE
962 void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src,
963 enum UserFmtType srcType, ALsizei numchans, ALsizei len, ALsizei align)
965 switch(dstType)
967 case UserFmtByte:
968 Convert_ALbyte(dst, src, srcType, numchans, len, align);
969 break;
970 case UserFmtUByte:
971 Convert_ALubyte(dst, src, srcType, numchans, len, align);
972 break;
973 case UserFmtShort:
974 Convert_ALshort(dst, src, srcType, numchans, len, align);
975 break;
976 case UserFmtUShort:
977 Convert_ALushort(dst, src, srcType, numchans, len, align);
978 break;
979 case UserFmtInt:
980 Convert_ALint(dst, src, srcType, numchans, len, align);
981 break;
982 case UserFmtUInt:
983 Convert_ALuint(dst, src, srcType, numchans, len, align);
984 break;
985 case UserFmtFloat:
986 Convert_ALfloat(dst, src, srcType, numchans, len, align);
987 break;
988 case UserFmtDouble:
989 Convert_ALdouble(dst, src, srcType, numchans, len, align);
990 break;
991 case UserFmtMulaw:
992 Convert_ALmulaw(dst, src, srcType, numchans, len, align);
993 break;
994 case UserFmtAlaw:
995 Convert_ALalaw(dst, src, srcType, numchans, len, align);
996 break;
997 case UserFmtIMA4:
998 Convert_ALima4(dst, src, srcType, numchans, len, align);
999 break;
1000 case UserFmtMSADPCM:
1001 Convert_ALmsadpcm(dst, src, srcType, numchans, len, align);
1002 break;