Forward Sample_ALuint to Sample_ALint
[openal-soft.git] / OpenAL32 / sample_cvt.c
blob89c2a408c6b72a7a7c18e2bdbe400fd6efedcd44
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, ALuint numchans, \
636 ALuint len, ALsizei UNUSED(align)) \
638 ALuint i, j; \
639 for(i = 0;i < len;i++) \
641 for(j = 0;j < numchans;j++) \
642 *(dst++) = Conv_##T1##_##T2(*(src++)); \
646 #define DECL_TEMPLATE2(T) \
647 DECL_TEMPLATE(T, ALbyte) \
648 DECL_TEMPLATE(T, ALubyte) \
649 DECL_TEMPLATE(T, ALshort) \
650 DECL_TEMPLATE(T, ALushort) \
651 DECL_TEMPLATE(T, ALint) \
652 DECL_TEMPLATE(T, ALuint) \
653 DECL_TEMPLATE(T, ALfloat) \
654 DECL_TEMPLATE(T, ALdouble) \
655 DECL_TEMPLATE(T, ALmulaw) \
656 DECL_TEMPLATE(T, ALalaw)
658 DECL_TEMPLATE2(ALbyte)
659 DECL_TEMPLATE2(ALubyte)
660 DECL_TEMPLATE2(ALshort)
661 DECL_TEMPLATE2(ALushort)
662 DECL_TEMPLATE2(ALint)
663 DECL_TEMPLATE2(ALuint)
664 DECL_TEMPLATE2(ALfloat)
665 DECL_TEMPLATE2(ALdouble)
666 DECL_TEMPLATE2(ALmulaw)
667 DECL_TEMPLATE2(ALalaw)
669 #undef DECL_TEMPLATE2
670 #undef DECL_TEMPLATE
672 #define DECL_TEMPLATE(T) \
673 static void Convert_##T##_ALima4(T *dst, const ALima4 *src, ALuint numchans, \
674 ALuint len, ALuint align) \
676 ALsizei byte_align = ((align-1)/2 + 4) * numchans; \
677 DECL_VLA(ALshort, tmp, align*numchans); \
678 ALuint i, j, k; \
680 assert(align > 0 && (len%align) == 0); \
681 for(i = 0;i < len;i += align) \
683 DecodeIMA4Block(tmp, src, numchans, align); \
684 src += byte_align; \
686 for(j = 0;j < align;j++) \
688 for(k = 0;k < numchans;k++) \
689 *(dst++) = Conv_##T##_ALshort(tmp[j*numchans + k]); \
694 DECL_TEMPLATE(ALbyte)
695 DECL_TEMPLATE(ALubyte)
696 static void Convert_ALshort_ALima4(ALshort *dst, const ALima4 *src, ALuint numchans,
697 ALuint len, ALuint align)
699 ALsizei byte_align = ((align-1)/2 + 4) * numchans;
700 ALuint i;
702 assert(align > 0 && (len%align) == 0);
703 for(i = 0;i < len;i += align)
705 DecodeIMA4Block(dst, src, numchans, align);
706 src += byte_align;
707 dst += align*numchans;
710 DECL_TEMPLATE(ALushort)
711 DECL_TEMPLATE(ALint)
712 DECL_TEMPLATE(ALuint)
713 DECL_TEMPLATE(ALfloat)
714 DECL_TEMPLATE(ALdouble)
715 DECL_TEMPLATE(ALmulaw)
716 DECL_TEMPLATE(ALalaw)
718 #undef DECL_TEMPLATE
720 #define DECL_TEMPLATE(T) \
721 static void Convert_ALima4_##T(ALima4 *dst, const T *src, ALuint numchans, \
722 ALuint len, ALuint align) \
724 ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
725 ALint index[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
726 ALsizei byte_align = ((align-1)/2 + 4) * numchans; \
727 DECL_VLA(ALshort, tmp, align*numchans); \
728 ALuint i, j, k; \
730 assert(align > 0 && (len%align) == 0); \
731 for(i = 0;i < len;i += align) \
733 for(j = 0;j < align;j++) \
735 for(k = 0;k < numchans;k++) \
736 tmp[j*numchans + k] = Conv_ALshort_##T(*(src++)); \
738 EncodeIMA4Block(dst, tmp, sample, index, numchans, align); \
739 dst += byte_align; \
743 DECL_TEMPLATE(ALbyte)
744 DECL_TEMPLATE(ALubyte)
745 static void Convert_ALima4_ALshort(ALima4 *dst, const ALshort *src,
746 ALuint numchans, ALuint len, ALuint align)
748 ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
749 ALint index[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
750 ALsizei byte_align = ((align-1)/2 + 4) * numchans;
751 ALuint i;
753 assert(align > 0 && (len%align) == 0);
754 for(i = 0;i < len;i += align)
756 EncodeIMA4Block(dst, src, sample, index, numchans, align);
757 src += align*numchans;
758 dst += byte_align;
761 DECL_TEMPLATE(ALushort)
762 DECL_TEMPLATE(ALint)
763 DECL_TEMPLATE(ALuint)
764 DECL_TEMPLATE(ALfloat)
765 DECL_TEMPLATE(ALdouble)
766 DECL_TEMPLATE(ALmulaw)
767 DECL_TEMPLATE(ALalaw)
769 #undef DECL_TEMPLATE
772 #define DECL_TEMPLATE(T) \
773 static void Convert_##T##_ALmsadpcm(T *dst, const ALmsadpcm *src, \
774 ALuint numchans, ALuint len, \
775 ALuint align) \
777 ALsizei byte_align = ((align-2)/2 + 7) * numchans; \
778 DECL_VLA(ALshort, tmp, align*numchans); \
779 ALuint i, j, k; \
781 assert(align > 1 && (len%align) == 0); \
782 for(i = 0;i < len;i += align) \
784 DecodeMSADPCMBlock(tmp, src, numchans, align); \
785 src += byte_align; \
787 for(j = 0;j < align;j++) \
789 for(k = 0;k < numchans;k++) \
790 *(dst++) = Conv_##T##_ALshort(tmp[j*numchans + k]); \
795 DECL_TEMPLATE(ALbyte)
796 DECL_TEMPLATE(ALubyte)
797 static void Convert_ALshort_ALmsadpcm(ALshort *dst, const ALmsadpcm *src,
798 ALuint numchans, ALuint len,
799 ALuint align)
801 ALsizei byte_align = ((align-2)/2 + 7) * numchans;
802 ALuint i;
804 assert(align > 1 && (len%align) == 0);
805 for(i = 0;i < len;i += align)
807 DecodeMSADPCMBlock(dst, src, numchans, align);
808 src += byte_align;
809 dst += align*numchans;
812 DECL_TEMPLATE(ALushort)
813 DECL_TEMPLATE(ALint)
814 DECL_TEMPLATE(ALuint)
815 DECL_TEMPLATE(ALfloat)
816 DECL_TEMPLATE(ALdouble)
817 DECL_TEMPLATE(ALmulaw)
818 DECL_TEMPLATE(ALalaw)
820 #undef DECL_TEMPLATE
822 #define DECL_TEMPLATE(T) \
823 static void Convert_ALmsadpcm_##T(ALmsadpcm *dst, const T *src, \
824 ALuint numchans, ALuint len, ALuint align) \
826 ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
827 ALsizei byte_align = ((align-2)/2 + 7) * numchans; \
828 DECL_VLA(ALshort, tmp, align*numchans); \
829 ALuint i, j, k; \
831 assert(align > 1 && (len%align) == 0); \
832 for(i = 0;i < len;i += align) \
834 for(j = 0;j < align;j++) \
836 for(k = 0;k < numchans;k++) \
837 tmp[j*numchans + k] = Conv_ALshort_##T(*(src++)); \
839 EncodeMSADPCMBlock(dst, tmp, sample, numchans, align); \
840 dst += byte_align; \
844 DECL_TEMPLATE(ALbyte)
845 DECL_TEMPLATE(ALubyte)
846 static void Convert_ALmsadpcm_ALshort(ALmsadpcm *dst, const ALshort *src,
847 ALuint numchans, ALuint len, ALuint align)
849 ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
850 ALsizei byte_align = ((align-2)/2 + 7) * numchans;
851 ALuint i;
853 assert(align > 1 && (len%align) == 0);
854 for(i = 0;i < len;i += align)
856 EncodeMSADPCMBlock(dst, src, sample, numchans, align);
857 src += align*numchans;
858 dst += byte_align;
861 DECL_TEMPLATE(ALushort)
862 DECL_TEMPLATE(ALint)
863 DECL_TEMPLATE(ALuint)
864 DECL_TEMPLATE(ALfloat)
865 DECL_TEMPLATE(ALdouble)
866 DECL_TEMPLATE(ALmulaw)
867 DECL_TEMPLATE(ALalaw)
869 #undef DECL_TEMPLATE
871 /* NOTE: We don't store compressed samples internally, so these conversions
872 * should never happen. */
873 static void Convert_ALima4_ALima4(ALima4* UNUSED(dst), const ALima4* UNUSED(src),
874 ALuint UNUSED(numchans), ALuint UNUSED(len),
875 ALuint UNUSED(align))
877 ERR("Unexpected IMA4-to-IMA4 conversion!\n");
880 static void Convert_ALmsadpcm_ALmsadpcm(ALmsadpcm* UNUSED(dst), const ALmsadpcm* UNUSED(src),
881 ALuint UNUSED(numchans), ALuint UNUSED(len),
882 ALuint UNUSED(align))
884 ERR("Unexpected MSADPCM-to-MSADPCM conversion!\n");
887 static void Convert_ALmsadpcm_ALima4(ALmsadpcm* UNUSED(dst), const ALima4* UNUSED(src),
888 ALuint UNUSED(numchans), ALuint UNUSED(len),
889 ALuint UNUSED(align))
891 ERR("Unexpected IMA4-to-MSADPCM conversion!\n");
894 static void Convert_ALima4_ALmsadpcm(ALima4* UNUSED(dst), const ALmsadpcm* UNUSED(src),
895 ALuint UNUSED(numchans), ALuint UNUSED(len),
896 ALuint UNUSED(align))
898 ERR("Unexpected MSADPCM-to-IMA4 conversion!\n");
902 #define DECL_TEMPLATE(T) \
903 static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
904 ALsizei numchans, ALsizei len, ALsizei align) \
906 switch(srcType) \
908 case UserFmtByte: \
909 Convert_##T##_ALbyte(dst, src, numchans, len, align); \
910 break; \
911 case UserFmtUByte: \
912 Convert_##T##_ALubyte(dst, src, numchans, len, align); \
913 break; \
914 case UserFmtShort: \
915 Convert_##T##_ALshort(dst, src, numchans, len, align); \
916 break; \
917 case UserFmtUShort: \
918 Convert_##T##_ALushort(dst, src, numchans, len, align); \
919 break; \
920 case UserFmtInt: \
921 Convert_##T##_ALint(dst, src, numchans, len, align); \
922 break; \
923 case UserFmtUInt: \
924 Convert_##T##_ALuint(dst, src, numchans, len, align); \
925 break; \
926 case UserFmtFloat: \
927 Convert_##T##_ALfloat(dst, src, numchans, len, align); \
928 break; \
929 case UserFmtDouble: \
930 Convert_##T##_ALdouble(dst, src, numchans, len, align); \
931 break; \
932 case UserFmtMulaw: \
933 Convert_##T##_ALmulaw(dst, src, numchans, len, align); \
934 break; \
935 case UserFmtAlaw: \
936 Convert_##T##_ALalaw(dst, src, numchans, len, align); \
937 break; \
938 case UserFmtIMA4: \
939 Convert_##T##_ALima4(dst, src, numchans, len, align); \
940 break; \
941 case UserFmtMSADPCM: \
942 Convert_##T##_ALmsadpcm(dst, src, numchans, len, align); \
943 break; \
947 DECL_TEMPLATE(ALbyte)
948 DECL_TEMPLATE(ALubyte)
949 DECL_TEMPLATE(ALshort)
950 DECL_TEMPLATE(ALushort)
951 DECL_TEMPLATE(ALint)
952 DECL_TEMPLATE(ALuint)
953 DECL_TEMPLATE(ALfloat)
954 DECL_TEMPLATE(ALdouble)
955 DECL_TEMPLATE(ALmulaw)
956 DECL_TEMPLATE(ALalaw)
957 DECL_TEMPLATE(ALima4)
958 DECL_TEMPLATE(ALmsadpcm)
960 #undef DECL_TEMPLATE
963 void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src, 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;