sample_cvt.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. #include "config.h"
  2. #include "sample_cvt.h"
  3. #ifdef HAVE_ALLOCA_H
  4. #include <alloca.h>
  5. #endif
  6. #ifdef HAVE_MALLOC_H
  7. #include <malloc.h>
  8. #endif
  9. #include "AL/al.h"
  10. #include "alu.h"
  11. #include "alBuffer.h"
  12. /* IMA ADPCM Stepsize table */
  13. static const int IMAStep_size[89] = {
  14. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19,
  15. 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
  16. 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
  17. 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
  18. 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
  19. 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660,
  20. 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,
  21. 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794,
  22. 32767
  23. };
  24. /* IMA4 ADPCM Codeword decode table */
  25. static const int IMA4Codeword[16] = {
  26. 1, 3, 5, 7, 9, 11, 13, 15,
  27. -1,-3,-5,-7,-9,-11,-13,-15,
  28. };
  29. /* IMA4 ADPCM Step index adjust decode table */
  30. static const int IMA4Index_adjust[16] = {
  31. -1,-1,-1,-1, 2, 4, 6, 8,
  32. -1,-1,-1,-1, 2, 4, 6, 8
  33. };
  34. /* MSADPCM Adaption table */
  35. static const int MSADPCMAdaption[16] = {
  36. 230, 230, 230, 230, 307, 409, 512, 614,
  37. 768, 614, 512, 409, 307, 230, 230, 230
  38. };
  39. /* MSADPCM Adaption Coefficient tables */
  40. static const int MSADPCMAdaptionCoeff[7][2] = {
  41. { 256, 0 },
  42. { 512, -256 },
  43. { 0, 0 },
  44. { 192, 64 },
  45. { 240, 0 },
  46. { 460, -208 },
  47. { 392, -232 }
  48. };
  49. /* A quick'n'dirty lookup table to decode a muLaw-encoded byte sample into a
  50. * signed 16-bit sample */
  51. static const ALshort muLawDecompressionTable[256] = {
  52. -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
  53. -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
  54. -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
  55. -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
  56. -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
  57. -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
  58. -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
  59. -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
  60. -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
  61. -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
  62. -876, -844, -812, -780, -748, -716, -684, -652,
  63. -620, -588, -556, -524, -492, -460, -428, -396,
  64. -372, -356, -340, -324, -308, -292, -276, -260,
  65. -244, -228, -212, -196, -180, -164, -148, -132,
  66. -120, -112, -104, -96, -88, -80, -72, -64,
  67. -56, -48, -40, -32, -24, -16, -8, 0,
  68. 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
  69. 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
  70. 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
  71. 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
  72. 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
  73. 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
  74. 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
  75. 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
  76. 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
  77. 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
  78. 876, 844, 812, 780, 748, 716, 684, 652,
  79. 620, 588, 556, 524, 492, 460, 428, 396,
  80. 372, 356, 340, 324, 308, 292, 276, 260,
  81. 244, 228, 212, 196, 180, 164, 148, 132,
  82. 120, 112, 104, 96, 88, 80, 72, 64,
  83. 56, 48, 40, 32, 24, 16, 8, 0
  84. };
  85. /* Values used when encoding a muLaw sample */
  86. static const int muLawBias = 0x84;
  87. static const int muLawClip = 32635;
  88. static const char muLawCompressTable[256] = {
  89. 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
  90. 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  91. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  92. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  93. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  94. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  95. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  96. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  97. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  98. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  99. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  100. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  101. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  102. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  103. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  104. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
  105. };
  106. /* A quick'n'dirty lookup table to decode an aLaw-encoded byte sample into a
  107. * signed 16-bit sample */
  108. static const ALshort aLawDecompressionTable[256] = {
  109. -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
  110. -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
  111. -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
  112. -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
  113. -22016,-20992,-24064,-23040,-17920,-16896,-19968,-18944,
  114. -30208,-29184,-32256,-31232,-26112,-25088,-28160,-27136,
  115. -11008,-10496,-12032,-11520, -8960, -8448, -9984, -9472,
  116. -15104,-14592,-16128,-15616,-13056,-12544,-14080,-13568,
  117. -344, -328, -376, -360, -280, -264, -312, -296,
  118. -472, -456, -504, -488, -408, -392, -440, -424,
  119. -88, -72, -120, -104, -24, -8, -56, -40,
  120. -216, -200, -248, -232, -152, -136, -184, -168,
  121. -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
  122. -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
  123. -688, -656, -752, -720, -560, -528, -624, -592,
  124. -944, -912, -1008, -976, -816, -784, -880, -848,
  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. };
  142. /* Values used when encoding an aLaw sample */
  143. static const int aLawClip = 32635;
  144. static const char aLawCompressTable[128] = {
  145. 1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
  146. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  147. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  148. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  149. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  150. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  151. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  152. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
  153. };
  154. typedef ALubyte ALmulaw;
  155. typedef ALubyte ALalaw;
  156. typedef ALubyte ALima4;
  157. typedef ALubyte ALmsadpcm;
  158. typedef struct {
  159. ALbyte b[3];
  160. } ALbyte3;
  161. static_assert(sizeof(ALbyte3)==sizeof(ALbyte[3]), "ALbyte3 size is not 3");
  162. typedef struct {
  163. ALubyte b[3];
  164. } ALubyte3;
  165. static_assert(sizeof(ALubyte3)==sizeof(ALubyte[3]), "ALubyte3 size is not 3");
  166. static inline ALshort DecodeMuLaw(ALmulaw val)
  167. { return muLawDecompressionTable[val]; }
  168. static ALmulaw EncodeMuLaw(ALshort val)
  169. {
  170. ALint mant, exp, sign;
  171. sign = (val>>8) & 0x80;
  172. if(sign)
  173. {
  174. /* -32768 doesn't properly negate on a short; it results in itself.
  175. * So clamp to -32767 */
  176. val = maxi(val, -32767);
  177. val = -val;
  178. }
  179. val = mini(val, muLawClip);
  180. val += muLawBias;
  181. exp = muLawCompressTable[(val>>7) & 0xff];
  182. mant = (val >> (exp+3)) & 0x0f;
  183. return ~(sign | (exp<<4) | mant);
  184. }
  185. static inline ALshort DecodeALaw(ALalaw val)
  186. { return aLawDecompressionTable[val]; }
  187. static ALalaw EncodeALaw(ALshort val)
  188. {
  189. ALint mant, exp, sign;
  190. sign = ((~val) >> 8) & 0x80;
  191. if(!sign)
  192. {
  193. val = maxi(val, -32767);
  194. val = -val;
  195. }
  196. val = mini(val, aLawClip);
  197. if(val >= 256)
  198. {
  199. exp = aLawCompressTable[(val>>8) & 0x7f];
  200. mant = (val >> (exp+3)) & 0x0f;
  201. }
  202. else
  203. {
  204. exp = 0;
  205. mant = val >> 4;
  206. }
  207. return ((exp<<4) | mant) ^ (sign^0x55);
  208. }
  209. static void DecodeIMA4Block(ALshort *dst, const ALima4 *src, ALint numchans, ALsizei align)
  210. {
  211. ALint sample[MAX_INPUT_CHANNELS], index[MAX_INPUT_CHANNELS];
  212. ALuint code[MAX_INPUT_CHANNELS];
  213. ALsizei j,k,c;
  214. for(c = 0;c < numchans;c++)
  215. {
  216. sample[c] = *(src++);
  217. sample[c] |= *(src++) << 8;
  218. sample[c] = (sample[c]^0x8000) - 32768;
  219. index[c] = *(src++);
  220. index[c] |= *(src++) << 8;
  221. index[c] = (index[c]^0x8000) - 32768;
  222. index[c] = clampi(index[c], 0, 88);
  223. dst[c] = sample[c];
  224. }
  225. for(j = 1;j < align;j += 8)
  226. {
  227. for(c = 0;c < numchans;c++)
  228. {
  229. code[c] = *(src++);
  230. code[c] |= *(src++) << 8;
  231. code[c] |= *(src++) << 16;
  232. code[c] |= *(src++) << 24;
  233. }
  234. for(k = 0;k < 8;k++)
  235. {
  236. for(c = 0;c < numchans;c++)
  237. {
  238. int nibble = code[c]&0xf;
  239. code[c] >>= 4;
  240. sample[c] += IMA4Codeword[nibble] * IMAStep_size[index[c]] / 8;
  241. sample[c] = clampi(sample[c], -32768, 32767);
  242. index[c] += IMA4Index_adjust[nibble];
  243. index[c] = clampi(index[c], 0, 88);
  244. dst[(j+k)*numchans + c] = sample[c];
  245. }
  246. }
  247. }
  248. }
  249. static void EncodeIMA4Block(ALima4 *dst, const ALshort *src, ALint *sample, ALint *index, ALint numchans, ALsizei align)
  250. {
  251. ALsizei j,k,c;
  252. for(c = 0;c < numchans;c++)
  253. {
  254. int diff = src[c] - sample[c];
  255. int step = IMAStep_size[index[c]];
  256. int nibble;
  257. nibble = 0;
  258. if(diff < 0)
  259. {
  260. nibble = 0x8;
  261. diff = -diff;
  262. }
  263. diff = mini(step*2, diff);
  264. nibble |= (diff*8/step - 1) / 2;
  265. sample[c] += IMA4Codeword[nibble] * step / 8;
  266. sample[c] = clampi(sample[c], -32768, 32767);
  267. index[c] += IMA4Index_adjust[nibble];
  268. index[c] = clampi(index[c], 0, 88);
  269. *(dst++) = sample[c] & 0xff;
  270. *(dst++) = (sample[c]>>8) & 0xff;
  271. *(dst++) = index[c] & 0xff;
  272. *(dst++) = (index[c]>>8) & 0xff;
  273. }
  274. for(j = 1;j < align;j += 8)
  275. {
  276. for(c = 0;c < numchans;c++)
  277. {
  278. for(k = 0;k < 8;k++)
  279. {
  280. int diff = src[(j+k)*numchans + c] - sample[c];
  281. int step = IMAStep_size[index[c]];
  282. int nibble;
  283. nibble = 0;
  284. if(diff < 0)
  285. {
  286. nibble = 0x8;
  287. diff = -diff;
  288. }
  289. diff = mini(step*2, diff);
  290. nibble |= (diff*8/step - 1) / 2;
  291. sample[c] += IMA4Codeword[nibble] * step / 8;
  292. sample[c] = clampi(sample[c], -32768, 32767);
  293. index[c] += IMA4Index_adjust[nibble];
  294. index[c] = clampi(index[c], 0, 88);
  295. if(!(k&1)) *dst = nibble;
  296. else *(dst++) |= nibble<<4;
  297. }
  298. }
  299. }
  300. }
  301. static void DecodeMSADPCMBlock(ALshort *dst, const ALmsadpcm *src, ALint numchans, ALsizei align)
  302. {
  303. ALubyte blockpred[MAX_INPUT_CHANNELS];
  304. ALint delta[MAX_INPUT_CHANNELS];
  305. ALshort samples[MAX_INPUT_CHANNELS][2];
  306. ALint i, j;
  307. for(i = 0;i < numchans;i++)
  308. {
  309. blockpred[i] = *(src++);
  310. blockpred[i] = minu(blockpred[i], 6);
  311. }
  312. for(i = 0;i < numchans;i++)
  313. {
  314. delta[i] = *(src++);
  315. delta[i] |= *(src++) << 8;
  316. delta[i] = (delta[i]^0x8000) - 0x8000;
  317. }
  318. for(i = 0;i < numchans;i++)
  319. {
  320. samples[i][0] = *(src++);
  321. samples[i][0] |= *(src++) << 8;
  322. samples[i][0] = (samples[i][0]^0x8000) - 0x8000;
  323. }
  324. for(i = 0;i < numchans;i++)
  325. {
  326. samples[i][1] = *(src++);
  327. samples[i][1] |= *(src++) << 8;
  328. samples[i][1] = (samples[i][1]^0x8000) - 0x8000;
  329. }
  330. /* Second sample is written first. */
  331. for(i = 0;i < numchans;i++)
  332. *(dst++) = samples[i][1];
  333. for(i = 0;i < numchans;i++)
  334. *(dst++) = samples[i][0];
  335. for(j = 2;j < align;j++)
  336. {
  337. for(i = 0;i < numchans;i++)
  338. {
  339. const ALint num = (j*numchans) + i;
  340. ALint nibble, pred;
  341. /* Read the nibble (first is in the upper bits). */
  342. if(!(num&1))
  343. nibble = (*src>>4)&0x0f;
  344. else
  345. nibble = (*(src++))&0x0f;
  346. pred = (samples[i][0]*MSADPCMAdaptionCoeff[blockpred[i]][0] +
  347. samples[i][1]*MSADPCMAdaptionCoeff[blockpred[i]][1]) / 256;
  348. pred += ((nibble^0x08) - 0x08) * delta[i];
  349. pred = clampi(pred, -32768, 32767);
  350. samples[i][1] = samples[i][0];
  351. samples[i][0] = pred;
  352. delta[i] = (MSADPCMAdaption[nibble] * delta[i]) / 256;
  353. delta[i] = maxi(16, delta[i]);
  354. *(dst++) = pred;
  355. }
  356. }
  357. }
  358. /* NOTE: This encoder is pretty dumb/simplistic. Some kind of pre-processing
  359. * that tries to find the optimal block predictors would be nice, at least. A
  360. * multi-pass method that can generate better deltas would be good, too. */
  361. static void EncodeMSADPCMBlock(ALmsadpcm *dst, const ALshort *src, ALint *sample, ALint numchans, ALsizei align)
  362. {
  363. ALubyte blockpred[MAX_INPUT_CHANNELS];
  364. ALint delta[MAX_INPUT_CHANNELS];
  365. ALshort samples[MAX_INPUT_CHANNELS][2];
  366. ALint i, j;
  367. /* Block predictor */
  368. for(i = 0;i < numchans;i++)
  369. {
  370. /* FIXME: Calculate something better. */
  371. blockpred[i] = 0;
  372. *(dst++) = blockpred[i];
  373. }
  374. /* Initial delta */
  375. for(i = 0;i < numchans;i++)
  376. {
  377. delta[i] = 16;
  378. *(dst++) = (delta[i] ) & 0xff;
  379. *(dst++) = (delta[i]>>8) & 0xff;
  380. }
  381. /* Initial sample 1 */
  382. for(i = 0;i < numchans;i++)
  383. {
  384. samples[i][0] = src[1*numchans + i];
  385. *(dst++) = (samples[i][0] ) & 0xff;
  386. *(dst++) = (samples[i][0]>>8) & 0xff;
  387. }
  388. /* Initial sample 2 */
  389. for(i = 0;i < numchans;i++)
  390. {
  391. samples[i][1] = src[i];
  392. *(dst++) = (samples[i][1] ) & 0xff;
  393. *(dst++) = (samples[i][1]>>8) & 0xff;
  394. }
  395. for(j = 2;j < align;j++)
  396. {
  397. for(i = 0;i < numchans;i++)
  398. {
  399. const ALint num = (j*numchans) + i;
  400. ALint nibble = 0;
  401. ALint bias;
  402. sample[i] = (samples[i][0]*MSADPCMAdaptionCoeff[blockpred[i]][0] +
  403. samples[i][1]*MSADPCMAdaptionCoeff[blockpred[i]][1]) / 256;
  404. nibble = src[num] - sample[i];
  405. if(nibble >= 0)
  406. bias = delta[i] / 2;
  407. else
  408. bias = -delta[i] / 2;
  409. nibble = (nibble + bias) / delta[i];
  410. nibble = clampi(nibble, -8, 7)&0x0f;
  411. sample[i] += ((nibble^0x08)-0x08) * delta[i];
  412. sample[i] = clampi(sample[i], -32768, 32767);
  413. samples[i][1] = samples[i][0];
  414. samples[i][0] = sample[i];
  415. delta[i] = (MSADPCMAdaption[nibble] * delta[i]) / 256;
  416. delta[i] = maxi(16, delta[i]);
  417. if(!(num&1))
  418. *dst = nibble << 4;
  419. else
  420. {
  421. *dst |= nibble;
  422. dst++;
  423. }
  424. }
  425. }
  426. }
  427. static inline ALint DecodeByte3(ALbyte3 val)
  428. {
  429. if(IS_LITTLE_ENDIAN)
  430. return (val.b[2]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[0]);
  431. return (val.b[0]<<16) | (((ALubyte)val.b[1])<<8) | ((ALubyte)val.b[2]);
  432. }
  433. static inline ALbyte3 EncodeByte3(ALint val)
  434. {
  435. if(IS_LITTLE_ENDIAN)
  436. {
  437. ALbyte3 ret = {{ val, val>>8, val>>16 }};
  438. return ret;
  439. }
  440. else
  441. {
  442. ALbyte3 ret = {{ val>>16, val>>8, val }};
  443. return ret;
  444. }
  445. }
  446. static inline ALint DecodeUByte3(ALubyte3 val)
  447. {
  448. if(IS_LITTLE_ENDIAN)
  449. return (val.b[2]<<16) | (val.b[1]<<8) | (val.b[0]);
  450. return (val.b[0]<<16) | (val.b[1]<<8) | val.b[2];
  451. }
  452. static inline ALubyte3 EncodeUByte3(ALint val)
  453. {
  454. if(IS_LITTLE_ENDIAN)
  455. {
  456. ALubyte3 ret = {{ val, val>>8, val>>16 }};
  457. return ret;
  458. }
  459. else
  460. {
  461. ALubyte3 ret = {{ val>>16, val>>8, val }};
  462. return ret;
  463. }
  464. }
  465. /* Define same-type pass-through sample conversion functions (excludes ADPCM,
  466. * which are block-based). */
  467. #define DECL_TEMPLATE(T) \
  468. static inline T Conv_##T##_##T(T val) { return val; }
  469. DECL_TEMPLATE(ALbyte);
  470. DECL_TEMPLATE(ALubyte);
  471. DECL_TEMPLATE(ALshort);
  472. DECL_TEMPLATE(ALushort);
  473. DECL_TEMPLATE(ALint);
  474. DECL_TEMPLATE(ALuint);
  475. DECL_TEMPLATE(ALbyte3);
  476. DECL_TEMPLATE(ALubyte3);
  477. DECL_TEMPLATE(ALalaw);
  478. DECL_TEMPLATE(ALmulaw);
  479. /* Slightly special handling for floats and doubles (converts NaN to 0, and
  480. * allows float<->double pass-through).
  481. */
  482. static inline ALfloat Conv_ALfloat_ALfloat(ALfloat val)
  483. { return (val==val) ? val : 0.0f; }
  484. static inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
  485. { return (val==val) ? (ALfloat)val : 0.0f; }
  486. static inline ALdouble Conv_ALdouble_ALfloat(ALfloat val)
  487. { return (val==val) ? (ALdouble)val : 0.0; }
  488. static inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
  489. { return (val==val) ? val : 0.0; }
  490. #undef DECL_TEMPLATE
  491. /* Define alternate-sign functions. */
  492. #define DECL_TEMPLATE(T1, T2, O) \
  493. static inline T1 Conv_##T1##_##T2(T2 val) { return (T1)val - O; } \
  494. static inline T2 Conv_##T2##_##T1(T1 val) { return (T2)val + O; }
  495. DECL_TEMPLATE(ALbyte, ALubyte, 128);
  496. DECL_TEMPLATE(ALshort, ALushort, 32768);
  497. DECL_TEMPLATE(ALint, ALuint, 2147483648u);
  498. #undef DECL_TEMPLATE
  499. /* Define int-type to int-type functions */
  500. #define DECL_TEMPLATE(T, ST, UT, SH) \
  501. static inline T Conv_##T##_##ST(ST val){ return val >> SH; } \
  502. static inline T Conv_##T##_##UT(UT val){ return Conv_##ST##_##UT(val) >> SH; }\
  503. static inline ST Conv_##ST##_##T(T val){ return val << SH; } \
  504. static inline UT Conv_##UT##_##T(T val){ return Conv_##UT##_##ST(val << SH); }
  505. #define DECL_TEMPLATE2(T1, T2, SH) \
  506. DECL_TEMPLATE(AL##T1, AL##T2, ALu##T2, SH) \
  507. DECL_TEMPLATE(ALu##T1, ALu##T2, AL##T2, SH)
  508. DECL_TEMPLATE2(byte, short, 8)
  509. DECL_TEMPLATE2(short, int, 16)
  510. DECL_TEMPLATE2(byte, int, 24)
  511. #undef DECL_TEMPLATE2
  512. #undef DECL_TEMPLATE
  513. /* Define int-type to fp functions */
  514. #define DECL_TEMPLATE(T, ST, UT, OP) \
  515. static inline T Conv_##T##_##ST(ST val) { return (T)val * OP; } \
  516. static inline T Conv_##T##_##UT(UT val) { return (T)Conv_##ST##_##UT(val) * OP; }
  517. #define DECL_TEMPLATE2(T1, T2, OP) \
  518. DECL_TEMPLATE(T1, AL##T2, ALu##T2, OP)
  519. DECL_TEMPLATE2(ALfloat, byte, (1.0f/127.0f))
  520. DECL_TEMPLATE2(ALdouble, byte, (1.0/127.0))
  521. DECL_TEMPLATE2(ALfloat, short, (1.0f/32767.0f))
  522. DECL_TEMPLATE2(ALdouble, short, (1.0/32767.0))
  523. DECL_TEMPLATE2(ALdouble, int, (1.0/2147483647.0))
  524. /* Special handling for int32 to float32, since it would overflow. */
  525. static inline ALfloat Conv_ALfloat_ALint(ALint val)
  526. { return (ALfloat)(val>>7) * (1.0f/16777215.0f); }
  527. static inline ALfloat Conv_ALfloat_ALuint(ALuint val)
  528. { return (ALfloat)(Conv_ALint_ALuint(val)>>7) * (1.0f/16777215.0f); }
  529. #undef DECL_TEMPLATE2
  530. #undef DECL_TEMPLATE
  531. /* Define fp to int-type functions */
  532. #define DECL_TEMPLATE(FT, T, smin, smax) \
  533. static inline AL##T Conv_AL##T##_##FT(FT val) \
  534. { \
  535. if(val > 1.0f) return smax; \
  536. if(val < -1.0f) return smin; \
  537. return (AL##T)(val * (FT)smax); \
  538. } \
  539. static inline ALu##T Conv_ALu##T##_##FT(FT val) \
  540. { return Conv_ALu##T##_AL##T(Conv_AL##T##_##FT(val)); }
  541. DECL_TEMPLATE(ALfloat, byte, -128, 127)
  542. DECL_TEMPLATE(ALdouble, byte, -128, 127)
  543. DECL_TEMPLATE(ALfloat, short, -32768, 32767)
  544. DECL_TEMPLATE(ALdouble, short, -32768, 32767)
  545. DECL_TEMPLATE(ALdouble, int, -2147483647-1, 2147483647)
  546. /* Special handling for float32 to int32, since it would overflow. */
  547. static inline ALint Conv_ALint_ALfloat(ALfloat val)
  548. {
  549. if(val > 1.0f) return 2147483647;
  550. if(val < -1.0f) return -2147483647-1;
  551. return (ALint)(val * 16777215.0f) << 7;
  552. }
  553. static inline ALuint Conv_ALuint_ALfloat(ALfloat val)
  554. { return Conv_ALuint_ALint(Conv_ALint_ALfloat(val)); }
  555. #undef DECL_TEMPLATE
  556. /* Define byte3 and ubyte3 functions (goes through int and uint functions). */
  557. #define DECL_TEMPLATE(T) \
  558. static inline ALbyte3 Conv_ALbyte3_##T(T val) \
  559. { return EncodeByte3(Conv_ALint_##T(val)>>8); } \
  560. static inline T Conv_##T##_ALbyte3(ALbyte3 val) \
  561. { return Conv_##T##_ALint(DecodeByte3(val)<<8); } \
  562. \
  563. static inline ALubyte3 Conv_ALubyte3_##T(T val) \
  564. { return EncodeUByte3(Conv_ALuint_##T(val)>>8); } \
  565. static inline T Conv_##T##_ALubyte3(ALubyte3 val) \
  566. { return Conv_##T##_ALuint(DecodeUByte3(val)<<8); }
  567. DECL_TEMPLATE(ALbyte)
  568. DECL_TEMPLATE(ALubyte)
  569. DECL_TEMPLATE(ALshort)
  570. DECL_TEMPLATE(ALushort)
  571. DECL_TEMPLATE(ALint)
  572. DECL_TEMPLATE(ALuint)
  573. DECL_TEMPLATE(ALfloat)
  574. DECL_TEMPLATE(ALdouble)
  575. #undef DECL_TEMPLATE
  576. /* Define byte3 <-> ubyte3 functions. */
  577. static inline ALbyte3 Conv_ALbyte3_ALubyte3(ALubyte3 val)
  578. { return EncodeByte3(DecodeUByte3(val)-8388608); }
  579. static inline ALubyte3 Conv_ALubyte3_ALbyte3(ALbyte3 val)
  580. { return EncodeUByte3(DecodeByte3(val)+8388608); }
  581. /* Define muLaw and aLaw functions (goes through short functions). */
  582. #define DECL_TEMPLATE(T) \
  583. static inline ALmulaw Conv_ALmulaw_##T(T val) \
  584. { return EncodeMuLaw(Conv_ALshort_##T(val)); } \
  585. static inline T Conv_##T##_ALmulaw(ALmulaw val) \
  586. { return Conv_##T##_ALshort(DecodeMuLaw(val)); } \
  587. \
  588. static inline ALalaw Conv_ALalaw_##T(T val) \
  589. { return EncodeALaw(Conv_ALshort_##T(val)); } \
  590. static inline T Conv_##T##_ALalaw(ALalaw val) \
  591. { return Conv_##T##_ALshort(DecodeALaw(val)); }
  592. DECL_TEMPLATE(ALbyte)
  593. DECL_TEMPLATE(ALubyte)
  594. DECL_TEMPLATE(ALshort)
  595. DECL_TEMPLATE(ALushort)
  596. DECL_TEMPLATE(ALint)
  597. DECL_TEMPLATE(ALuint)
  598. DECL_TEMPLATE(ALfloat)
  599. DECL_TEMPLATE(ALdouble)
  600. DECL_TEMPLATE(ALbyte3)
  601. DECL_TEMPLATE(ALubyte3)
  602. #undef DECL_TEMPLATE
  603. /* Define muLaw <-> aLaw functions. */
  604. static inline ALalaw Conv_ALalaw_ALmulaw(ALmulaw val)
  605. { return EncodeALaw(DecodeMuLaw(val)); }
  606. static inline ALmulaw Conv_ALmulaw_ALalaw(ALalaw val)
  607. { return EncodeMuLaw(DecodeALaw(val)); }
  608. #define DECL_TEMPLATE(T1, T2) \
  609. static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint numchans, \
  610. ALuint len, ALsizei UNUSED(align)) \
  611. { \
  612. ALuint i, j; \
  613. for(i = 0;i < len;i++) \
  614. { \
  615. for(j = 0;j < numchans;j++) \
  616. *(dst++) = Conv_##T1##_##T2(*(src++)); \
  617. } \
  618. }
  619. #define DECL_TEMPLATE2(T) \
  620. DECL_TEMPLATE(T, ALbyte) \
  621. DECL_TEMPLATE(T, ALubyte) \
  622. DECL_TEMPLATE(T, ALshort) \
  623. DECL_TEMPLATE(T, ALushort) \
  624. DECL_TEMPLATE(T, ALint) \
  625. DECL_TEMPLATE(T, ALuint) \
  626. DECL_TEMPLATE(T, ALfloat) \
  627. DECL_TEMPLATE(T, ALdouble) \
  628. DECL_TEMPLATE(T, ALmulaw) \
  629. DECL_TEMPLATE(T, ALalaw) \
  630. DECL_TEMPLATE(T, ALbyte3) \
  631. DECL_TEMPLATE(T, ALubyte3)
  632. DECL_TEMPLATE2(ALbyte)
  633. DECL_TEMPLATE2(ALubyte)
  634. DECL_TEMPLATE2(ALshort)
  635. DECL_TEMPLATE2(ALushort)
  636. DECL_TEMPLATE2(ALint)
  637. DECL_TEMPLATE2(ALuint)
  638. DECL_TEMPLATE2(ALfloat)
  639. DECL_TEMPLATE2(ALdouble)
  640. DECL_TEMPLATE2(ALmulaw)
  641. DECL_TEMPLATE2(ALalaw)
  642. DECL_TEMPLATE2(ALbyte3)
  643. DECL_TEMPLATE2(ALubyte3)
  644. #undef DECL_TEMPLATE2
  645. #undef DECL_TEMPLATE
  646. #define DECL_TEMPLATE(T) \
  647. static void Convert_##T##_ALima4(T *dst, const ALima4 *src, ALuint numchans, \
  648. ALuint len, ALuint align) \
  649. { \
  650. ALsizei byte_align = ((align-1)/2 + 4) * numchans; \
  651. DECL_VLA(ALshort, tmp, align*numchans); \
  652. ALuint i, j, k; \
  653. \
  654. assert(align > 0 && (len%align) == 0); \
  655. for(i = 0;i < len;i += align) \
  656. { \
  657. DecodeIMA4Block(tmp, src, numchans, align); \
  658. src += byte_align; \
  659. \
  660. for(j = 0;j < align;j++) \
  661. { \
  662. for(k = 0;k < numchans;k++) \
  663. *(dst++) = Conv_##T##_ALshort(tmp[j*numchans + k]); \
  664. } \
  665. } \
  666. }
  667. DECL_TEMPLATE(ALbyte)
  668. DECL_TEMPLATE(ALubyte)
  669. static void Convert_ALshort_ALima4(ALshort *dst, const ALima4 *src, ALuint numchans,
  670. ALuint len, ALuint align)
  671. {
  672. ALsizei byte_align = ((align-1)/2 + 4) * numchans;
  673. ALuint i;
  674. assert(align > 0 && (len%align) == 0);
  675. for(i = 0;i < len;i += align)
  676. {
  677. DecodeIMA4Block(dst, src, numchans, align);
  678. src += byte_align;
  679. dst += align*numchans;
  680. }
  681. }
  682. DECL_TEMPLATE(ALushort)
  683. DECL_TEMPLATE(ALint)
  684. DECL_TEMPLATE(ALuint)
  685. DECL_TEMPLATE(ALfloat)
  686. DECL_TEMPLATE(ALdouble)
  687. DECL_TEMPLATE(ALmulaw)
  688. DECL_TEMPLATE(ALalaw)
  689. DECL_TEMPLATE(ALbyte3)
  690. DECL_TEMPLATE(ALubyte3)
  691. #undef DECL_TEMPLATE
  692. #define DECL_TEMPLATE(T) \
  693. static void Convert_ALima4_##T(ALima4 *dst, const T *src, ALuint numchans, \
  694. ALuint len, ALuint align) \
  695. { \
  696. ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
  697. ALint index[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
  698. ALsizei byte_align = ((align-1)/2 + 4) * numchans; \
  699. DECL_VLA(ALshort, tmp, align*numchans); \
  700. ALuint i, j, k; \
  701. \
  702. assert(align > 0 && (len%align) == 0); \
  703. for(i = 0;i < len;i += align) \
  704. { \
  705. for(j = 0;j < align;j++) \
  706. { \
  707. for(k = 0;k < numchans;k++) \
  708. tmp[j*numchans + k] = Conv_ALshort_##T(*(src++)); \
  709. } \
  710. EncodeIMA4Block(dst, tmp, sample, index, numchans, align); \
  711. dst += byte_align; \
  712. } \
  713. }
  714. DECL_TEMPLATE(ALbyte)
  715. DECL_TEMPLATE(ALubyte)
  716. static void Convert_ALima4_ALshort(ALima4 *dst, const ALshort *src,
  717. ALuint numchans, ALuint len, ALuint align)
  718. {
  719. ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
  720. ALint index[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
  721. ALsizei byte_align = ((align-1)/2 + 4) * numchans;
  722. ALuint i;
  723. assert(align > 0 && (len%align) == 0);
  724. for(i = 0;i < len;i += align)
  725. {
  726. EncodeIMA4Block(dst, src, sample, index, numchans, align);
  727. src += align*numchans;
  728. dst += byte_align;
  729. }
  730. }
  731. DECL_TEMPLATE(ALushort)
  732. DECL_TEMPLATE(ALint)
  733. DECL_TEMPLATE(ALuint)
  734. DECL_TEMPLATE(ALfloat)
  735. DECL_TEMPLATE(ALdouble)
  736. DECL_TEMPLATE(ALmulaw)
  737. DECL_TEMPLATE(ALalaw)
  738. DECL_TEMPLATE(ALbyte3)
  739. DECL_TEMPLATE(ALubyte3)
  740. #undef DECL_TEMPLATE
  741. #define DECL_TEMPLATE(T) \
  742. static void Convert_##T##_ALmsadpcm(T *dst, const ALmsadpcm *src, \
  743. ALuint numchans, ALuint len, \
  744. ALuint align) \
  745. { \
  746. ALsizei byte_align = ((align-2)/2 + 7) * numchans; \
  747. DECL_VLA(ALshort, tmp, align*numchans); \
  748. ALuint i, j, k; \
  749. \
  750. assert(align > 1 && (len%align) == 0); \
  751. for(i = 0;i < len;i += align) \
  752. { \
  753. DecodeMSADPCMBlock(tmp, src, numchans, align); \
  754. src += byte_align; \
  755. \
  756. for(j = 0;j < align;j++) \
  757. { \
  758. for(k = 0;k < numchans;k++) \
  759. *(dst++) = Conv_##T##_ALshort(tmp[j*numchans + k]); \
  760. } \
  761. } \
  762. }
  763. DECL_TEMPLATE(ALbyte)
  764. DECL_TEMPLATE(ALubyte)
  765. static void Convert_ALshort_ALmsadpcm(ALshort *dst, const ALmsadpcm *src,
  766. ALuint numchans, ALuint len,
  767. ALuint align)
  768. {
  769. ALsizei byte_align = ((align-2)/2 + 7) * numchans;
  770. ALuint i;
  771. assert(align > 1 && (len%align) == 0);
  772. for(i = 0;i < len;i += align)
  773. {
  774. DecodeMSADPCMBlock(dst, src, numchans, align);
  775. src += byte_align;
  776. dst += align*numchans;
  777. }
  778. }
  779. DECL_TEMPLATE(ALushort)
  780. DECL_TEMPLATE(ALint)
  781. DECL_TEMPLATE(ALuint)
  782. DECL_TEMPLATE(ALfloat)
  783. DECL_TEMPLATE(ALdouble)
  784. DECL_TEMPLATE(ALmulaw)
  785. DECL_TEMPLATE(ALalaw)
  786. DECL_TEMPLATE(ALbyte3)
  787. DECL_TEMPLATE(ALubyte3)
  788. #undef DECL_TEMPLATE
  789. #define DECL_TEMPLATE(T) \
  790. static void Convert_ALmsadpcm_##T(ALmsadpcm *dst, const T *src, \
  791. ALuint numchans, ALuint len, ALuint align) \
  792. { \
  793. ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
  794. ALsizei byte_align = ((align-2)/2 + 7) * numchans; \
  795. DECL_VLA(ALshort, tmp, align*numchans); \
  796. ALuint i, j, k; \
  797. \
  798. assert(align > 1 && (len%align) == 0); \
  799. for(i = 0;i < len;i += align) \
  800. { \
  801. for(j = 0;j < align;j++) \
  802. { \
  803. for(k = 0;k < numchans;k++) \
  804. tmp[j*numchans + k] = Conv_ALshort_##T(*(src++)); \
  805. } \
  806. EncodeMSADPCMBlock(dst, tmp, sample, numchans, align); \
  807. dst += byte_align; \
  808. } \
  809. }
  810. DECL_TEMPLATE(ALbyte)
  811. DECL_TEMPLATE(ALubyte)
  812. static void Convert_ALmsadpcm_ALshort(ALmsadpcm *dst, const ALshort *src,
  813. ALuint numchans, ALuint len, ALuint align)
  814. {
  815. ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
  816. ALsizei byte_align = ((align-2)/2 + 7) * numchans;
  817. ALuint i;
  818. assert(align > 1 && (len%align) == 0);
  819. for(i = 0;i < len;i += align)
  820. {
  821. EncodeMSADPCMBlock(dst, src, sample, numchans, align);
  822. src += align*numchans;
  823. dst += byte_align;
  824. }
  825. }
  826. DECL_TEMPLATE(ALushort)
  827. DECL_TEMPLATE(ALint)
  828. DECL_TEMPLATE(ALuint)
  829. DECL_TEMPLATE(ALfloat)
  830. DECL_TEMPLATE(ALdouble)
  831. DECL_TEMPLATE(ALmulaw)
  832. DECL_TEMPLATE(ALalaw)
  833. DECL_TEMPLATE(ALbyte3)
  834. DECL_TEMPLATE(ALubyte3)
  835. #undef DECL_TEMPLATE
  836. /* NOTE: We don't store compressed samples internally, so these conversions
  837. * should never happen. */
  838. static void Convert_ALima4_ALima4(ALima4* UNUSED(dst), const ALima4* UNUSED(src),
  839. ALuint UNUSED(numchans), ALuint UNUSED(len),
  840. ALuint UNUSED(align))
  841. {
  842. ERR("Unexpected IMA4-to-IMA4 conversion!\n");
  843. }
  844. static void Convert_ALmsadpcm_ALmsadpcm(ALmsadpcm* UNUSED(dst), const ALmsadpcm* UNUSED(src),
  845. ALuint UNUSED(numchans), ALuint UNUSED(len),
  846. ALuint UNUSED(align))
  847. {
  848. ERR("Unexpected MSADPCM-to-MSADPCM conversion!\n");
  849. }
  850. static void Convert_ALmsadpcm_ALima4(ALmsadpcm* UNUSED(dst), const ALima4* UNUSED(src),
  851. ALuint UNUSED(numchans), ALuint UNUSED(len),
  852. ALuint UNUSED(align))
  853. {
  854. ERR("Unexpected IMA4-to-MSADPCM conversion!\n");
  855. }
  856. static void Convert_ALima4_ALmsadpcm(ALima4* UNUSED(dst), const ALmsadpcm* UNUSED(src),
  857. ALuint UNUSED(numchans), ALuint UNUSED(len),
  858. ALuint UNUSED(align))
  859. {
  860. ERR("Unexpected MSADPCM-to-IMA4 conversion!\n");
  861. }
  862. #define DECL_TEMPLATE(T) \
  863. static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
  864. ALsizei numchans, ALsizei len, ALsizei align) \
  865. { \
  866. switch(srcType) \
  867. { \
  868. case UserFmtByte: \
  869. Convert_##T##_ALbyte(dst, src, numchans, len, align); \
  870. break; \
  871. case UserFmtUByte: \
  872. Convert_##T##_ALubyte(dst, src, numchans, len, align); \
  873. break; \
  874. case UserFmtShort: \
  875. Convert_##T##_ALshort(dst, src, numchans, len, align); \
  876. break; \
  877. case UserFmtUShort: \
  878. Convert_##T##_ALushort(dst, src, numchans, len, align); \
  879. break; \
  880. case UserFmtInt: \
  881. Convert_##T##_ALint(dst, src, numchans, len, align); \
  882. break; \
  883. case UserFmtUInt: \
  884. Convert_##T##_ALuint(dst, src, numchans, len, align); \
  885. break; \
  886. case UserFmtFloat: \
  887. Convert_##T##_ALfloat(dst, src, numchans, len, align); \
  888. break; \
  889. case UserFmtDouble: \
  890. Convert_##T##_ALdouble(dst, src, numchans, len, align); \
  891. break; \
  892. case UserFmtMulaw: \
  893. Convert_##T##_ALmulaw(dst, src, numchans, len, align); \
  894. break; \
  895. case UserFmtAlaw: \
  896. Convert_##T##_ALalaw(dst, src, numchans, len, align); \
  897. break; \
  898. case UserFmtIMA4: \
  899. Convert_##T##_ALima4(dst, src, numchans, len, align); \
  900. break; \
  901. case UserFmtMSADPCM: \
  902. Convert_##T##_ALmsadpcm(dst, src, numchans, len, align); \
  903. break; \
  904. case UserFmtByte3: \
  905. Convert_##T##_ALbyte3(dst, src, numchans, len, align); \
  906. break; \
  907. case UserFmtUByte3: \
  908. Convert_##T##_ALubyte3(dst, src, numchans, len, align); \
  909. break; \
  910. } \
  911. }
  912. DECL_TEMPLATE(ALbyte)
  913. DECL_TEMPLATE(ALubyte)
  914. DECL_TEMPLATE(ALshort)
  915. DECL_TEMPLATE(ALushort)
  916. DECL_TEMPLATE(ALint)
  917. DECL_TEMPLATE(ALuint)
  918. DECL_TEMPLATE(ALfloat)
  919. DECL_TEMPLATE(ALdouble)
  920. DECL_TEMPLATE(ALmulaw)
  921. DECL_TEMPLATE(ALalaw)
  922. DECL_TEMPLATE(ALima4)
  923. DECL_TEMPLATE(ALmsadpcm)
  924. DECL_TEMPLATE(ALbyte3)
  925. DECL_TEMPLATE(ALubyte3)
  926. #undef DECL_TEMPLATE
  927. void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei numchans, ALsizei len, ALsizei align)
  928. {
  929. switch(dstType)
  930. {
  931. case UserFmtByte:
  932. Convert_ALbyte(dst, src, srcType, numchans, len, align);
  933. break;
  934. case UserFmtUByte:
  935. Convert_ALubyte(dst, src, srcType, numchans, len, align);
  936. break;
  937. case UserFmtShort:
  938. Convert_ALshort(dst, src, srcType, numchans, len, align);
  939. break;
  940. case UserFmtUShort:
  941. Convert_ALushort(dst, src, srcType, numchans, len, align);
  942. break;
  943. case UserFmtInt:
  944. Convert_ALint(dst, src, srcType, numchans, len, align);
  945. break;
  946. case UserFmtUInt:
  947. Convert_ALuint(dst, src, srcType, numchans, len, align);
  948. break;
  949. case UserFmtFloat:
  950. Convert_ALfloat(dst, src, srcType, numchans, len, align);
  951. break;
  952. case UserFmtDouble:
  953. Convert_ALdouble(dst, src, srcType, numchans, len, align);
  954. break;
  955. case UserFmtMulaw:
  956. Convert_ALmulaw(dst, src, srcType, numchans, len, align);
  957. break;
  958. case UserFmtAlaw:
  959. Convert_ALalaw(dst, src, srcType, numchans, len, align);
  960. break;
  961. case UserFmtIMA4:
  962. Convert_ALima4(dst, src, srcType, numchans, len, align);
  963. break;
  964. case UserFmtMSADPCM:
  965. Convert_ALmsadpcm(dst, src, srcType, numchans, len, align);
  966. break;
  967. case UserFmtByte3:
  968. Convert_ALbyte3(dst, src, srcType, numchans, len, align);
  969. break;
  970. case UserFmtUByte3:
  971. Convert_ALubyte3(dst, src, srcType, numchans, len, align);
  972. break;
  973. }
  974. }