Fix for NULL JNIEnv
[openal-soft.git] / OpenAL32 / sample_cvt.c
blobaff3de8342b8af69daa4a63d8030ddfe5d499316
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;
177 typedef struct {
178 ALbyte b[3];
179 } ALbyte3;
180 static_assert(sizeof(ALbyte3)==sizeof(ALbyte[3]), "ALbyte3 size is not 3");
181 typedef struct {
182 ALubyte b[3];
183 } ALubyte3;
184 static_assert(sizeof(ALubyte3)==sizeof(ALubyte[3]), "ALubyte3 size is not 3");
186 static inline ALshort DecodeMuLaw(ALmulaw val)
187 { return muLawDecompressionTable[val]; }
189 static ALmulaw EncodeMuLaw(ALshort val)
191 ALint mant, exp, sign;
193 sign = (val>>8) & 0x80;
194 if(sign)
196 /* -32768 doesn't properly negate on a short; it results in itself.
197 * So clamp to -32767 */
198 val = maxi(val, -32767);
199 val = -val;
202 val = mini(val, muLawClip);
203 val += muLawBias;
205 exp = muLawCompressTable[(val>>7) & 0xff];
206 mant = (val >> (exp+3)) & 0x0f;
208 return ~(sign | (exp<<4) | mant);
211 static inline ALshort DecodeALaw(ALalaw val)
212 { return aLawDecompressionTable[val]; }
214 static ALalaw EncodeALaw(ALshort val)
216 ALint mant, exp, sign;
218 sign = ((~val) >> 8) & 0x80;
219 if(!sign)
221 val = maxi(val, -32767);
222 val = -val;
224 val = mini(val, aLawClip);
226 if(val >= 256)
228 exp = aLawCompressTable[(val>>8) & 0x7f];
229 mant = (val >> (exp+3)) & 0x0f;
231 else
233 exp = 0;
234 mant = val >> 4;
237 return ((exp<<4) | mant) ^ (sign^0x55);
240 static void DecodeIMA4Block(ALshort *dst, const ALima4 *src, ALint numchans, ALsizei align)
242 ALint sample[MAX_INPUT_CHANNELS], index[MAX_INPUT_CHANNELS];
243 ALuint code[MAX_INPUT_CHANNELS];
244 ALsizei j,k,c;
246 for(c = 0;c < numchans;c++)
248 sample[c] = *(src++);
249 sample[c] |= *(src++) << 8;
250 sample[c] = (sample[c]^0x8000) - 32768;
251 index[c] = *(src++);
252 index[c] |= *(src++) << 8;
253 index[c] = (index[c]^0x8000) - 32768;
255 index[c] = clampi(index[c], 0, 88);
257 dst[c] = sample[c];
260 for(j = 1;j < align;j += 8)
262 for(c = 0;c < numchans;c++)
264 code[c] = *(src++);
265 code[c] |= *(src++) << 8;
266 code[c] |= *(src++) << 16;
267 code[c] |= *(src++) << 24;
270 for(k = 0;k < 8;k++)
272 for(c = 0;c < numchans;c++)
274 int nibble = code[c]&0xf;
275 code[c] >>= 4;
277 sample[c] += IMA4Codeword[nibble] * IMAStep_size[index[c]] / 8;
278 sample[c] = clampi(sample[c], -32768, 32767);
280 index[c] += IMA4Index_adjust[nibble];
281 index[c] = clampi(index[c], 0, 88);
283 dst[(j+k)*numchans + c] = sample[c];
289 static void EncodeIMA4Block(ALima4 *dst, const ALshort *src, ALint *sample, ALint *index, ALint numchans, ALsizei align)
291 ALsizei j,k,c;
293 for(c = 0;c < numchans;c++)
295 int diff = src[c] - sample[c];
296 int step = IMAStep_size[index[c]];
297 int nibble;
299 nibble = 0;
300 if(diff < 0)
302 nibble = 0x8;
303 diff = -diff;
306 diff = mini(step*2, diff);
307 nibble |= (diff*8/step - 1) / 2;
309 sample[c] += IMA4Codeword[nibble] * step / 8;
310 sample[c] = clampi(sample[c], -32768, 32767);
312 index[c] += IMA4Index_adjust[nibble];
313 index[c] = clampi(index[c], 0, 88);
315 *(dst++) = sample[c] & 0xff;
316 *(dst++) = (sample[c]>>8) & 0xff;
317 *(dst++) = index[c] & 0xff;
318 *(dst++) = (index[c]>>8) & 0xff;
321 for(j = 1;j < align;j += 8)
323 for(c = 0;c < numchans;c++)
325 for(k = 0;k < 8;k++)
327 int diff = src[(j+k)*numchans + c] - sample[c];
328 int step = IMAStep_size[index[c]];
329 int nibble;
331 nibble = 0;
332 if(diff < 0)
334 nibble = 0x8;
335 diff = -diff;
338 diff = mini(step*2, diff);
339 nibble |= (diff*8/step - 1) / 2;
341 sample[c] += IMA4Codeword[nibble] * step / 8;
342 sample[c] = clampi(sample[c], -32768, 32767);
344 index[c] += IMA4Index_adjust[nibble];
345 index[c] = clampi(index[c], 0, 88);
347 if(!(k&1)) *dst = nibble;
348 else *(dst++) |= nibble<<4;
355 static void DecodeMSADPCMBlock(ALshort *dst, const ALmsadpcm *src, ALint numchans, ALsizei align)
357 ALubyte blockpred[MAX_INPUT_CHANNELS];
358 ALint delta[MAX_INPUT_CHANNELS];
359 ALshort samples[MAX_INPUT_CHANNELS][2];
360 ALint i, j;
362 for(i = 0;i < numchans;i++)
364 blockpred[i] = *(src++);
365 blockpred[i] = minu(blockpred[i], 6);
367 for(i = 0;i < numchans;i++)
369 delta[i] = *(src++);
370 delta[i] |= *(src++) << 8;
371 delta[i] = (delta[i]^0x8000) - 0x8000;
373 for(i = 0;i < numchans;i++)
375 samples[i][0] = *(src++);
376 samples[i][0] |= *(src++) << 8;
377 samples[i][0] = (samples[i][0]^0x8000) - 0x8000;
379 for(i = 0;i < numchans;i++)
381 samples[i][1] = *(src++);
382 samples[i][1] |= *(src++) << 8;
383 samples[i][1] = (samples[i][1]^0x8000) - 0x8000;
386 /* Second sample is written first. */
387 for(i = 0;i < numchans;i++)
388 *(dst++) = samples[i][1];
389 for(i = 0;i < numchans;i++)
390 *(dst++) = samples[i][0];
392 for(j = 2;j < align;j++)
394 for(i = 0;i < numchans;i++)
396 const ALint num = (j*numchans) + i;
397 ALint nibble, pred;
399 /* Read the nibble (first is in the upper bits). */
400 if(!(num&1))
401 nibble = (*src>>4)&0x0f;
402 else
403 nibble = (*(src++))&0x0f;
405 pred = (samples[i][0]*MSADPCMAdaptionCoeff[blockpred[i]][0] +
406 samples[i][1]*MSADPCMAdaptionCoeff[blockpred[i]][1]) / 256;
407 pred += ((nibble^0x08) - 0x08) * delta[i];
408 pred = clampi(pred, -32768, 32767);
410 samples[i][1] = samples[i][0];
411 samples[i][0] = pred;
413 delta[i] = (MSADPCMAdaption[nibble] * delta[i]) / 256;
414 delta[i] = maxi(16, delta[i]);
416 *(dst++) = pred;
421 /* NOTE: This encoder is pretty dumb/simplistic. Some kind of pre-processing
422 * that tries to find the optimal block predictors would be nice, at least. A
423 * multi-pass method that can generate better deltas would be good, too. */
424 static void EncodeMSADPCMBlock(ALmsadpcm *dst, const ALshort *src, ALint *sample, ALint numchans, ALsizei align)
426 ALubyte blockpred[MAX_INPUT_CHANNELS];
427 ALint delta[MAX_INPUT_CHANNELS];
428 ALshort samples[MAX_INPUT_CHANNELS][2];
429 ALint i, j;
431 /* Block predictor */
432 for(i = 0;i < numchans;i++)
434 /* FIXME: Calculate something better. */
435 blockpred[i] = 0;
436 *(dst++) = blockpred[i];
438 /* Initial delta */
439 for(i = 0;i < numchans;i++)
441 delta[i] = 16;
442 *(dst++) = (delta[i] ) & 0xff;
443 *(dst++) = (delta[i]>>8) & 0xff;
445 /* Initial sample 1 */
446 for(i = 0;i < numchans;i++)
448 samples[i][0] = src[1*numchans + i];
449 *(dst++) = (samples[i][0] ) & 0xff;
450 *(dst++) = (samples[i][0]>>8) & 0xff;
452 /* Initial sample 2 */
453 for(i = 0;i < numchans;i++)
455 samples[i][1] = src[i];
456 *(dst++) = (samples[i][1] ) & 0xff;
457 *(dst++) = (samples[i][1]>>8) & 0xff;
460 for(j = 2;j < align;j++)
462 for(i = 0;i < numchans;i++)
464 const ALint num = (j*numchans) + i;
465 ALint nibble = 0;
466 ALint bias;
468 sample[i] = (samples[i][0]*MSADPCMAdaptionCoeff[blockpred[i]][0] +
469 samples[i][1]*MSADPCMAdaptionCoeff[blockpred[i]][1]) / 256;
471 nibble = src[num] - sample[i];
472 if(nibble >= 0)
473 bias = delta[i] / 2;
474 else
475 bias = -delta[i] / 2;
477 nibble = (nibble + bias) / delta[i];
478 nibble = clampi(nibble, -8, 7)&0x0f;
480 sample[i] += ((nibble^0x08)-0x08) * delta[i];
481 sample[i] = clampi(sample[i], -32768, 32767);
483 samples[i][1] = samples[i][0];
484 samples[i][0] = sample[i];
486 delta[i] = (MSADPCMAdaption[nibble] * delta[i]) / 256;
487 delta[i] = maxi(16, delta[i]);
489 if(!(num&1))
490 *dst = nibble << 4;
491 else
493 *dst |= nibble;
494 dst++;
501 static inline ALint DecodeByte3(ALbyte3 val)
503 if(IS_LITTLE_ENDIAN)
504 return (val.b[2]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[0]);
505 return (val.b[0]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[2]);
508 static inline ALbyte3 EncodeByte3(ALint val)
510 if(IS_LITTLE_ENDIAN)
512 ALbyte3 ret = {{ val, val>>8, val>>16 }};
513 return ret;
515 else
517 ALbyte3 ret = {{ val>>16, val>>8, val }};
518 return ret;
522 static inline ALint DecodeUByte3(ALubyte3 val)
524 if(IS_LITTLE_ENDIAN)
525 return (val.b[2]<<16) | (val.b[1]<<8) | (val.b[0]);
526 return (val.b[0]<<16) | (val.b[1]<<8) | val.b[2];
529 static inline ALubyte3 EncodeUByte3(ALint val)
531 if(IS_LITTLE_ENDIAN)
533 ALubyte3 ret = {{ val, val>>8, val>>16 }};
534 return ret;
536 else
538 ALubyte3 ret = {{ val>>16, val>>8, val }};
539 return ret;
544 /* Define same-type pass-through sample conversion functions (excludes ADPCM,
545 * which are block-based). */
546 #define DECL_TEMPLATE(T) \
547 static inline T Conv_##T##_##T(T val) { return val; }
549 DECL_TEMPLATE(ALbyte);
550 DECL_TEMPLATE(ALubyte);
551 DECL_TEMPLATE(ALshort);
552 DECL_TEMPLATE(ALushort);
553 DECL_TEMPLATE(ALint);
554 DECL_TEMPLATE(ALuint);
555 DECL_TEMPLATE(ALbyte3);
556 DECL_TEMPLATE(ALubyte3);
557 DECL_TEMPLATE(ALalaw);
558 DECL_TEMPLATE(ALmulaw);
560 /* Slightly special handling for floats and doubles (converts NaN to 0, and
561 * allows float<->double pass-through).
563 static inline ALfloat Conv_ALfloat_ALfloat(ALfloat val)
564 { return (val==val) ? val : 0.0f; }
565 static inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
566 { return (val==val) ? (ALfloat)val : 0.0f; }
567 static inline ALdouble Conv_ALdouble_ALfloat(ALfloat val)
568 { return (val==val) ? (ALdouble)val : 0.0; }
569 static inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
570 { return (val==val) ? val : 0.0; }
572 #undef DECL_TEMPLATE
574 /* Define alternate-sign functions. */
575 #define DECL_TEMPLATE(T1, T2, O) \
576 static inline T1 Conv_##T1##_##T2(T2 val) { return (T1)val - O; } \
577 static inline T2 Conv_##T2##_##T1(T1 val) { return (T2)val + O; }
579 DECL_TEMPLATE(ALbyte, ALubyte, 128);
580 DECL_TEMPLATE(ALshort, ALushort, 32768);
581 DECL_TEMPLATE(ALint, ALuint, 2147483648u);
583 #undef DECL_TEMPLATE
585 /* Define int-type to int-type functions */
586 #define DECL_TEMPLATE(T, ST, UT, SH) \
587 static inline T Conv_##T##_##ST(ST val){ return val >> SH; } \
588 static inline T Conv_##T##_##UT(UT val){ return Conv_##ST##_##UT(val) >> SH; }\
589 static inline ST Conv_##ST##_##T(T val){ return val << SH; } \
590 static inline UT Conv_##UT##_##T(T val){ return Conv_##UT##_##ST(val << SH); }
592 #define DECL_TEMPLATE2(T1, T2, SH) \
593 DECL_TEMPLATE(AL##T1, AL##T2, ALu##T2, SH) \
594 DECL_TEMPLATE(ALu##T1, ALu##T2, AL##T2, SH)
596 DECL_TEMPLATE2(byte, short, 8)
597 DECL_TEMPLATE2(short, int, 16)
598 DECL_TEMPLATE2(byte, int, 24)
600 #undef DECL_TEMPLATE2
601 #undef DECL_TEMPLATE
603 /* Define int-type to fp functions */
604 #define DECL_TEMPLATE(T, ST, UT, OP) \
605 static inline T Conv_##T##_##ST(ST val) { return (T)val * OP; } \
606 static inline T Conv_##T##_##UT(UT val) { return (T)Conv_##ST##_##UT(val) * OP; }
608 #define DECL_TEMPLATE2(T1, T2, OP) \
609 DECL_TEMPLATE(T1, AL##T2, ALu##T2, OP)
611 DECL_TEMPLATE2(ALfloat, byte, (1.0f/127.0f))
612 DECL_TEMPLATE2(ALdouble, byte, (1.0/127.0))
613 DECL_TEMPLATE2(ALfloat, short, (1.0f/32767.0f))
614 DECL_TEMPLATE2(ALdouble, short, (1.0/32767.0))
615 DECL_TEMPLATE2(ALdouble, int, (1.0/2147483647.0))
617 /* Special handling for int32 to float32, since it would overflow. */
618 static inline ALfloat Conv_ALfloat_ALint(ALint val)
619 { return (ALfloat)(val>>7) * (1.0f/16777215.0f); }
620 static inline ALfloat Conv_ALfloat_ALuint(ALuint val)
621 { return (ALfloat)(Conv_ALint_ALuint(val)>>7) * (1.0f/16777215.0f); }
623 #undef DECL_TEMPLATE2
624 #undef DECL_TEMPLATE
626 /* Define fp to int-type functions */
627 #define DECL_TEMPLATE(FT, T, smin, smax) \
628 static inline AL##T Conv_AL##T##_##FT(FT val) \
630 if(val > 1.0f) return smax; \
631 if(val < -1.0f) return smin; \
632 return (AL##T)(val * (FT)smax); \
634 static inline ALu##T Conv_ALu##T##_##FT(FT val) \
635 { return Conv_ALu##T##_AL##T(Conv_AL##T##_##FT(val)); }
637 DECL_TEMPLATE(ALfloat, byte, -128, 127)
638 DECL_TEMPLATE(ALdouble, byte, -128, 127)
639 DECL_TEMPLATE(ALfloat, short, -32768, 32767)
640 DECL_TEMPLATE(ALdouble, short, -32768, 32767)
641 DECL_TEMPLATE(ALdouble, int, -2147483647-1, 2147483647)
643 /* Special handling for float32 to int32, since it would overflow. */
644 static inline ALint Conv_ALint_ALfloat(ALfloat val)
646 if(val > 1.0f) return 2147483647;
647 if(val < -1.0f) return -2147483647-1;
648 return (ALint)(val * 16777215.0f) << 7;
650 static inline ALuint Conv_ALuint_ALfloat(ALfloat val)
651 { return Conv_ALuint_ALint(Conv_ALint_ALfloat(val)); }
653 #undef DECL_TEMPLATE
655 /* Define byte3 and ubyte3 functions (goes through int and uint functions). */
656 #define DECL_TEMPLATE(T) \
657 static inline ALbyte3 Conv_ALbyte3_##T(T val) \
658 { return EncodeByte3(Conv_ALint_##T(val)>>8); } \
659 static inline T Conv_##T##_ALbyte3(ALbyte3 val) \
660 { return Conv_##T##_ALint(DecodeByte3(val)<<8); } \
662 static inline ALubyte3 Conv_ALubyte3_##T(T val) \
663 { return EncodeUByte3(Conv_ALuint_##T(val)>>8); } \
664 static inline T Conv_##T##_ALubyte3(ALubyte3 val) \
665 { return Conv_##T##_ALuint(DecodeUByte3(val)<<8); }
667 DECL_TEMPLATE(ALbyte)
668 DECL_TEMPLATE(ALubyte)
669 DECL_TEMPLATE(ALshort)
670 DECL_TEMPLATE(ALushort)
671 DECL_TEMPLATE(ALint)
672 DECL_TEMPLATE(ALuint)
673 DECL_TEMPLATE(ALfloat)
674 DECL_TEMPLATE(ALdouble)
676 #undef DECL_TEMPLATE
678 /* Define byte3 <-> ubyte3 functions. */
679 static inline ALbyte3 Conv_ALbyte3_ALubyte3(ALubyte3 val)
680 { return EncodeByte3(DecodeUByte3(val)-8388608); }
681 static inline ALubyte3 Conv_ALubyte3_ALbyte3(ALbyte3 val)
682 { return EncodeUByte3(DecodeByte3(val)+8388608); }
684 /* Define muLaw and aLaw functions (goes through short functions). */
685 #define DECL_TEMPLATE(T) \
686 static inline ALmulaw Conv_ALmulaw_##T(T val) \
687 { return EncodeMuLaw(Conv_ALshort_##T(val)); } \
688 static inline T Conv_##T##_ALmulaw(ALmulaw val) \
689 { return Conv_##T##_ALshort(DecodeMuLaw(val)); } \
691 static inline ALalaw Conv_ALalaw_##T(T val) \
692 { return EncodeALaw(Conv_ALshort_##T(val)); } \
693 static inline T Conv_##T##_ALalaw(ALalaw val) \
694 { return Conv_##T##_ALshort(DecodeALaw(val)); }
696 DECL_TEMPLATE(ALbyte)
697 DECL_TEMPLATE(ALubyte)
698 DECL_TEMPLATE(ALshort)
699 DECL_TEMPLATE(ALushort)
700 DECL_TEMPLATE(ALint)
701 DECL_TEMPLATE(ALuint)
702 DECL_TEMPLATE(ALfloat)
703 DECL_TEMPLATE(ALdouble)
704 DECL_TEMPLATE(ALbyte3)
705 DECL_TEMPLATE(ALubyte3)
707 #undef DECL_TEMPLATE
709 /* Define muLaw <-> aLaw functions. */
710 static inline ALalaw Conv_ALalaw_ALmulaw(ALmulaw val)
711 { return EncodeALaw(DecodeMuLaw(val)); }
712 static inline ALmulaw Conv_ALmulaw_ALalaw(ALalaw val)
713 { return EncodeMuLaw(DecodeALaw(val)); }
716 #define DECL_TEMPLATE(T1, T2) \
717 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint numchans, \
718 ALuint len, ALsizei UNUSED(align)) \
720 ALuint i, j; \
721 for(i = 0;i < len;i++) \
723 for(j = 0;j < numchans;j++) \
724 *(dst++) = Conv_##T1##_##T2(*(src++)); \
728 #define DECL_TEMPLATE2(T) \
729 DECL_TEMPLATE(T, ALbyte) \
730 DECL_TEMPLATE(T, ALubyte) \
731 DECL_TEMPLATE(T, ALshort) \
732 DECL_TEMPLATE(T, ALushort) \
733 DECL_TEMPLATE(T, ALint) \
734 DECL_TEMPLATE(T, ALuint) \
735 DECL_TEMPLATE(T, ALfloat) \
736 DECL_TEMPLATE(T, ALdouble) \
737 DECL_TEMPLATE(T, ALmulaw) \
738 DECL_TEMPLATE(T, ALalaw) \
739 DECL_TEMPLATE(T, ALbyte3) \
740 DECL_TEMPLATE(T, ALubyte3)
742 DECL_TEMPLATE2(ALbyte)
743 DECL_TEMPLATE2(ALubyte)
744 DECL_TEMPLATE2(ALshort)
745 DECL_TEMPLATE2(ALushort)
746 DECL_TEMPLATE2(ALint)
747 DECL_TEMPLATE2(ALuint)
748 DECL_TEMPLATE2(ALfloat)
749 DECL_TEMPLATE2(ALdouble)
750 DECL_TEMPLATE2(ALmulaw)
751 DECL_TEMPLATE2(ALalaw)
752 DECL_TEMPLATE2(ALbyte3)
753 DECL_TEMPLATE2(ALubyte3)
755 #undef DECL_TEMPLATE2
756 #undef DECL_TEMPLATE
758 #define DECL_TEMPLATE(T) \
759 static void Convert_##T##_ALima4(T *dst, const ALima4 *src, ALuint numchans, \
760 ALuint len, ALuint align) \
762 ALsizei byte_align = ((align-1)/2 + 4) * numchans; \
763 DECL_VLA(ALshort, tmp, align*numchans); \
764 ALuint i, j, k; \
766 assert(align > 0 && (len%align) == 0); \
767 for(i = 0;i < len;i += align) \
769 DecodeIMA4Block(tmp, src, numchans, align); \
770 src += byte_align; \
772 for(j = 0;j < align;j++) \
774 for(k = 0;k < numchans;k++) \
775 *(dst++) = Conv_##T##_ALshort(tmp[j*numchans + k]); \
780 DECL_TEMPLATE(ALbyte)
781 DECL_TEMPLATE(ALubyte)
782 static void Convert_ALshort_ALima4(ALshort *dst, const ALima4 *src, ALuint numchans,
783 ALuint len, ALuint align)
785 ALsizei byte_align = ((align-1)/2 + 4) * numchans;
786 ALuint i;
788 assert(align > 0 && (len%align) == 0);
789 for(i = 0;i < len;i += align)
791 DecodeIMA4Block(dst, src, numchans, align);
792 src += byte_align;
793 dst += align*numchans;
796 DECL_TEMPLATE(ALushort)
797 DECL_TEMPLATE(ALint)
798 DECL_TEMPLATE(ALuint)
799 DECL_TEMPLATE(ALfloat)
800 DECL_TEMPLATE(ALdouble)
801 DECL_TEMPLATE(ALmulaw)
802 DECL_TEMPLATE(ALalaw)
803 DECL_TEMPLATE(ALbyte3)
804 DECL_TEMPLATE(ALubyte3)
806 #undef DECL_TEMPLATE
808 #define DECL_TEMPLATE(T) \
809 static void Convert_ALima4_##T(ALima4 *dst, const T *src, ALuint numchans, \
810 ALuint len, ALuint align) \
812 ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
813 ALint index[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
814 ALsizei byte_align = ((align-1)/2 + 4) * numchans; \
815 DECL_VLA(ALshort, tmp, align*numchans); \
816 ALuint i, j, k; \
818 assert(align > 0 && (len%align) == 0); \
819 for(i = 0;i < len;i += align) \
821 for(j = 0;j < align;j++) \
823 for(k = 0;k < numchans;k++) \
824 tmp[j*numchans + k] = Conv_ALshort_##T(*(src++)); \
826 EncodeIMA4Block(dst, tmp, sample, index, numchans, align); \
827 dst += byte_align; \
831 DECL_TEMPLATE(ALbyte)
832 DECL_TEMPLATE(ALubyte)
833 static void Convert_ALima4_ALshort(ALima4 *dst, const ALshort *src,
834 ALuint numchans, ALuint len, ALuint align)
836 ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
837 ALint index[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
838 ALsizei byte_align = ((align-1)/2 + 4) * numchans;
839 ALuint i;
841 assert(align > 0 && (len%align) == 0);
842 for(i = 0;i < len;i += align)
844 EncodeIMA4Block(dst, src, sample, index, numchans, align);
845 src += align*numchans;
846 dst += byte_align;
849 DECL_TEMPLATE(ALushort)
850 DECL_TEMPLATE(ALint)
851 DECL_TEMPLATE(ALuint)
852 DECL_TEMPLATE(ALfloat)
853 DECL_TEMPLATE(ALdouble)
854 DECL_TEMPLATE(ALmulaw)
855 DECL_TEMPLATE(ALalaw)
856 DECL_TEMPLATE(ALbyte3)
857 DECL_TEMPLATE(ALubyte3)
859 #undef DECL_TEMPLATE
862 #define DECL_TEMPLATE(T) \
863 static void Convert_##T##_ALmsadpcm(T *dst, const ALmsadpcm *src, \
864 ALuint numchans, ALuint len, \
865 ALuint align) \
867 ALsizei byte_align = ((align-2)/2 + 7) * numchans; \
868 DECL_VLA(ALshort, tmp, align*numchans); \
869 ALuint i, j, k; \
871 assert(align > 1 && (len%align) == 0); \
872 for(i = 0;i < len;i += align) \
874 DecodeMSADPCMBlock(tmp, src, numchans, align); \
875 src += byte_align; \
877 for(j = 0;j < align;j++) \
879 for(k = 0;k < numchans;k++) \
880 *(dst++) = Conv_##T##_ALshort(tmp[j*numchans + k]); \
885 DECL_TEMPLATE(ALbyte)
886 DECL_TEMPLATE(ALubyte)
887 static void Convert_ALshort_ALmsadpcm(ALshort *dst, const ALmsadpcm *src,
888 ALuint numchans, ALuint len,
889 ALuint align)
891 ALsizei byte_align = ((align-2)/2 + 7) * numchans;
892 ALuint i;
894 assert(align > 1 && (len%align) == 0);
895 for(i = 0;i < len;i += align)
897 DecodeMSADPCMBlock(dst, src, numchans, align);
898 src += byte_align;
899 dst += align*numchans;
902 DECL_TEMPLATE(ALushort)
903 DECL_TEMPLATE(ALint)
904 DECL_TEMPLATE(ALuint)
905 DECL_TEMPLATE(ALfloat)
906 DECL_TEMPLATE(ALdouble)
907 DECL_TEMPLATE(ALmulaw)
908 DECL_TEMPLATE(ALalaw)
909 DECL_TEMPLATE(ALbyte3)
910 DECL_TEMPLATE(ALubyte3)
912 #undef DECL_TEMPLATE
914 #define DECL_TEMPLATE(T) \
915 static void Convert_ALmsadpcm_##T(ALmsadpcm *dst, const T *src, \
916 ALuint numchans, ALuint len, ALuint align) \
918 ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
919 ALsizei byte_align = ((align-2)/2 + 7) * numchans; \
920 DECL_VLA(ALshort, tmp, align*numchans); \
921 ALuint i, j, k; \
923 assert(align > 1 && (len%align) == 0); \
924 for(i = 0;i < len;i += align) \
926 for(j = 0;j < align;j++) \
928 for(k = 0;k < numchans;k++) \
929 tmp[j*numchans + k] = Conv_ALshort_##T(*(src++)); \
931 EncodeMSADPCMBlock(dst, tmp, sample, numchans, align); \
932 dst += byte_align; \
936 DECL_TEMPLATE(ALbyte)
937 DECL_TEMPLATE(ALubyte)
938 static void Convert_ALmsadpcm_ALshort(ALmsadpcm *dst, const ALshort *src,
939 ALuint numchans, ALuint len, ALuint align)
941 ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
942 ALsizei byte_align = ((align-2)/2 + 7) * numchans;
943 ALuint i;
945 assert(align > 1 && (len%align) == 0);
946 for(i = 0;i < len;i += align)
948 EncodeMSADPCMBlock(dst, src, sample, numchans, align);
949 src += align*numchans;
950 dst += byte_align;
953 DECL_TEMPLATE(ALushort)
954 DECL_TEMPLATE(ALint)
955 DECL_TEMPLATE(ALuint)
956 DECL_TEMPLATE(ALfloat)
957 DECL_TEMPLATE(ALdouble)
958 DECL_TEMPLATE(ALmulaw)
959 DECL_TEMPLATE(ALalaw)
960 DECL_TEMPLATE(ALbyte3)
961 DECL_TEMPLATE(ALubyte3)
963 #undef DECL_TEMPLATE
965 /* NOTE: We don't store compressed samples internally, so these conversions
966 * should never happen. */
967 static void Convert_ALima4_ALima4(ALima4* UNUSED(dst), const ALima4* UNUSED(src),
968 ALuint UNUSED(numchans), ALuint UNUSED(len),
969 ALuint UNUSED(align))
971 ERR("Unexpected IMA4-to-IMA4 conversion!\n");
974 static void Convert_ALmsadpcm_ALmsadpcm(ALmsadpcm* UNUSED(dst), const ALmsadpcm* UNUSED(src),
975 ALuint UNUSED(numchans), ALuint UNUSED(len),
976 ALuint UNUSED(align))
978 ERR("Unexpected MSADPCM-to-MSADPCM conversion!\n");
981 static void Convert_ALmsadpcm_ALima4(ALmsadpcm* UNUSED(dst), const ALima4* UNUSED(src),
982 ALuint UNUSED(numchans), ALuint UNUSED(len),
983 ALuint UNUSED(align))
985 ERR("Unexpected IMA4-to-MSADPCM conversion!\n");
988 static void Convert_ALima4_ALmsadpcm(ALima4* UNUSED(dst), const ALmsadpcm* UNUSED(src),
989 ALuint UNUSED(numchans), ALuint UNUSED(len),
990 ALuint UNUSED(align))
992 ERR("Unexpected MSADPCM-to-IMA4 conversion!\n");
996 #define DECL_TEMPLATE(T) \
997 static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
998 ALsizei numchans, ALsizei len, ALsizei align) \
1000 switch(srcType) \
1002 case UserFmtByte: \
1003 Convert_##T##_ALbyte(dst, src, numchans, len, align); \
1004 break; \
1005 case UserFmtUByte: \
1006 Convert_##T##_ALubyte(dst, src, numchans, len, align); \
1007 break; \
1008 case UserFmtShort: \
1009 Convert_##T##_ALshort(dst, src, numchans, len, align); \
1010 break; \
1011 case UserFmtUShort: \
1012 Convert_##T##_ALushort(dst, src, numchans, len, align); \
1013 break; \
1014 case UserFmtInt: \
1015 Convert_##T##_ALint(dst, src, numchans, len, align); \
1016 break; \
1017 case UserFmtUInt: \
1018 Convert_##T##_ALuint(dst, src, numchans, len, align); \
1019 break; \
1020 case UserFmtFloat: \
1021 Convert_##T##_ALfloat(dst, src, numchans, len, align); \
1022 break; \
1023 case UserFmtDouble: \
1024 Convert_##T##_ALdouble(dst, src, numchans, len, align); \
1025 break; \
1026 case UserFmtMulaw: \
1027 Convert_##T##_ALmulaw(dst, src, numchans, len, align); \
1028 break; \
1029 case UserFmtAlaw: \
1030 Convert_##T##_ALalaw(dst, src, numchans, len, align); \
1031 break; \
1032 case UserFmtIMA4: \
1033 Convert_##T##_ALima4(dst, src, numchans, len, align); \
1034 break; \
1035 case UserFmtMSADPCM: \
1036 Convert_##T##_ALmsadpcm(dst, src, numchans, len, align); \
1037 break; \
1038 case UserFmtByte3: \
1039 Convert_##T##_ALbyte3(dst, src, numchans, len, align); \
1040 break; \
1041 case UserFmtUByte3: \
1042 Convert_##T##_ALubyte3(dst, src, numchans, len, align); \
1043 break; \
1047 DECL_TEMPLATE(ALbyte)
1048 DECL_TEMPLATE(ALubyte)
1049 DECL_TEMPLATE(ALshort)
1050 DECL_TEMPLATE(ALushort)
1051 DECL_TEMPLATE(ALint)
1052 DECL_TEMPLATE(ALuint)
1053 DECL_TEMPLATE(ALfloat)
1054 DECL_TEMPLATE(ALdouble)
1055 DECL_TEMPLATE(ALmulaw)
1056 DECL_TEMPLATE(ALalaw)
1057 DECL_TEMPLATE(ALima4)
1058 DECL_TEMPLATE(ALmsadpcm)
1059 DECL_TEMPLATE(ALbyte3)
1060 DECL_TEMPLATE(ALubyte3)
1062 #undef DECL_TEMPLATE
1065 void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei numchans, ALsizei len, ALsizei align)
1067 switch(dstType)
1069 case UserFmtByte:
1070 Convert_ALbyte(dst, src, srcType, numchans, len, align);
1071 break;
1072 case UserFmtUByte:
1073 Convert_ALubyte(dst, src, srcType, numchans, len, align);
1074 break;
1075 case UserFmtShort:
1076 Convert_ALshort(dst, src, srcType, numchans, len, align);
1077 break;
1078 case UserFmtUShort:
1079 Convert_ALushort(dst, src, srcType, numchans, len, align);
1080 break;
1081 case UserFmtInt:
1082 Convert_ALint(dst, src, srcType, numchans, len, align);
1083 break;
1084 case UserFmtUInt:
1085 Convert_ALuint(dst, src, srcType, numchans, len, align);
1086 break;
1087 case UserFmtFloat:
1088 Convert_ALfloat(dst, src, srcType, numchans, len, align);
1089 break;
1090 case UserFmtDouble:
1091 Convert_ALdouble(dst, src, srcType, numchans, len, align);
1092 break;
1093 case UserFmtMulaw:
1094 Convert_ALmulaw(dst, src, srcType, numchans, len, align);
1095 break;
1096 case UserFmtAlaw:
1097 Convert_ALalaw(dst, src, srcType, numchans, len, align);
1098 break;
1099 case UserFmtIMA4:
1100 Convert_ALima4(dst, src, srcType, numchans, len, align);
1101 break;
1102 case UserFmtMSADPCM:
1103 Convert_ALmsadpcm(dst, src, srcType, numchans, len, align);
1104 break;
1105 case UserFmtByte3:
1106 Convert_ALbyte3(dst, src, srcType, numchans, len, align);
1107 break;
1108 case UserFmtUByte3:
1109 Convert_ALubyte3(dst, src, srcType, numchans, len, align);
1110 break;