2
0

sample_cvt.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. static inline ALshort DecodeMuLaw(ALmulaw val)
  159. { return muLawDecompressionTable[val]; }
  160. static ALmulaw EncodeMuLaw(ALshort val)
  161. {
  162. ALint mant, exp, sign;
  163. sign = (val>>8) & 0x80;
  164. if(sign)
  165. {
  166. /* -32768 doesn't properly negate on a short; it results in itself.
  167. * So clamp to -32767 */
  168. val = maxi(val, -32767);
  169. val = -val;
  170. }
  171. val = mini(val, muLawClip);
  172. val += muLawBias;
  173. exp = muLawCompressTable[(val>>7) & 0xff];
  174. mant = (val >> (exp+3)) & 0x0f;
  175. return ~(sign | (exp<<4) | mant);
  176. }
  177. static inline ALshort DecodeALaw(ALalaw val)
  178. { return aLawDecompressionTable[val]; }
  179. static ALalaw EncodeALaw(ALshort val)
  180. {
  181. ALint mant, exp, sign;
  182. sign = ((~val) >> 8) & 0x80;
  183. if(!sign)
  184. {
  185. val = maxi(val, -32767);
  186. val = -val;
  187. }
  188. val = mini(val, aLawClip);
  189. if(val >= 256)
  190. {
  191. exp = aLawCompressTable[(val>>8) & 0x7f];
  192. mant = (val >> (exp+3)) & 0x0f;
  193. }
  194. else
  195. {
  196. exp = 0;
  197. mant = val >> 4;
  198. }
  199. return ((exp<<4) | mant) ^ (sign^0x55);
  200. }
  201. static void DecodeIMA4Block(ALshort *dst, const ALima4 *src, ALint numchans, ALsizei align)
  202. {
  203. ALint sample[MAX_INPUT_CHANNELS], index[MAX_INPUT_CHANNELS];
  204. ALuint code[MAX_INPUT_CHANNELS];
  205. ALsizei j,k,c;
  206. for(c = 0;c < numchans;c++)
  207. {
  208. sample[c] = *(src++);
  209. sample[c] |= *(src++) << 8;
  210. sample[c] = (sample[c]^0x8000) - 32768;
  211. index[c] = *(src++);
  212. index[c] |= *(src++) << 8;
  213. index[c] = (index[c]^0x8000) - 32768;
  214. index[c] = clampi(index[c], 0, 88);
  215. dst[c] = sample[c];
  216. }
  217. for(j = 1;j < align;j += 8)
  218. {
  219. for(c = 0;c < numchans;c++)
  220. {
  221. code[c] = *(src++);
  222. code[c] |= *(src++) << 8;
  223. code[c] |= *(src++) << 16;
  224. code[c] |= *(src++) << 24;
  225. }
  226. for(k = 0;k < 8;k++)
  227. {
  228. for(c = 0;c < numchans;c++)
  229. {
  230. int nibble = code[c]&0xf;
  231. code[c] >>= 4;
  232. sample[c] += IMA4Codeword[nibble] * IMAStep_size[index[c]] / 8;
  233. sample[c] = clampi(sample[c], -32768, 32767);
  234. index[c] += IMA4Index_adjust[nibble];
  235. index[c] = clampi(index[c], 0, 88);
  236. dst[(j+k)*numchans + c] = sample[c];
  237. }
  238. }
  239. }
  240. }
  241. static void EncodeIMA4Block(ALima4 *dst, const ALshort *src, ALint *sample, ALint *index, ALint numchans, ALsizei align)
  242. {
  243. ALsizei j,k,c;
  244. for(c = 0;c < numchans;c++)
  245. {
  246. int diff = src[c] - sample[c];
  247. int step = IMAStep_size[index[c]];
  248. int nibble;
  249. nibble = 0;
  250. if(diff < 0)
  251. {
  252. nibble = 0x8;
  253. diff = -diff;
  254. }
  255. diff = mini(step*2, diff);
  256. nibble |= (diff*8/step - 1) / 2;
  257. sample[c] += IMA4Codeword[nibble] * step / 8;
  258. sample[c] = clampi(sample[c], -32768, 32767);
  259. index[c] += IMA4Index_adjust[nibble];
  260. index[c] = clampi(index[c], 0, 88);
  261. *(dst++) = sample[c] & 0xff;
  262. *(dst++) = (sample[c]>>8) & 0xff;
  263. *(dst++) = index[c] & 0xff;
  264. *(dst++) = (index[c]>>8) & 0xff;
  265. }
  266. for(j = 1;j < align;j += 8)
  267. {
  268. for(c = 0;c < numchans;c++)
  269. {
  270. for(k = 0;k < 8;k++)
  271. {
  272. int diff = src[(j+k)*numchans + c] - sample[c];
  273. int step = IMAStep_size[index[c]];
  274. int nibble;
  275. nibble = 0;
  276. if(diff < 0)
  277. {
  278. nibble = 0x8;
  279. diff = -diff;
  280. }
  281. diff = mini(step*2, diff);
  282. nibble |= (diff*8/step - 1) / 2;
  283. sample[c] += IMA4Codeword[nibble] * step / 8;
  284. sample[c] = clampi(sample[c], -32768, 32767);
  285. index[c] += IMA4Index_adjust[nibble];
  286. index[c] = clampi(index[c], 0, 88);
  287. if(!(k&1)) *dst = nibble;
  288. else *(dst++) |= nibble<<4;
  289. }
  290. }
  291. }
  292. }
  293. static void DecodeMSADPCMBlock(ALshort *dst, const ALmsadpcm *src, ALint numchans, ALsizei align)
  294. {
  295. ALubyte blockpred[MAX_INPUT_CHANNELS];
  296. ALint delta[MAX_INPUT_CHANNELS];
  297. ALshort samples[MAX_INPUT_CHANNELS][2];
  298. ALint i, j;
  299. for(i = 0;i < numchans;i++)
  300. {
  301. blockpred[i] = *(src++);
  302. blockpred[i] = minu(blockpred[i], 6);
  303. }
  304. for(i = 0;i < numchans;i++)
  305. {
  306. delta[i] = *(src++);
  307. delta[i] |= *(src++) << 8;
  308. delta[i] = (delta[i]^0x8000) - 0x8000;
  309. }
  310. for(i = 0;i < numchans;i++)
  311. {
  312. samples[i][0] = *(src++);
  313. samples[i][0] |= *(src++) << 8;
  314. samples[i][0] = (samples[i][0]^0x8000) - 0x8000;
  315. }
  316. for(i = 0;i < numchans;i++)
  317. {
  318. samples[i][1] = *(src++);
  319. samples[i][1] |= *(src++) << 8;
  320. samples[i][1] = (samples[i][1]^0x8000) - 0x8000;
  321. }
  322. /* Second sample is written first. */
  323. for(i = 0;i < numchans;i++)
  324. *(dst++) = samples[i][1];
  325. for(i = 0;i < numchans;i++)
  326. *(dst++) = samples[i][0];
  327. for(j = 2;j < align;j++)
  328. {
  329. for(i = 0;i < numchans;i++)
  330. {
  331. const ALint num = (j*numchans) + i;
  332. ALint nibble, pred;
  333. /* Read the nibble (first is in the upper bits). */
  334. if(!(num&1))
  335. nibble = (*src>>4)&0x0f;
  336. else
  337. nibble = (*(src++))&0x0f;
  338. pred = (samples[i][0]*MSADPCMAdaptionCoeff[blockpred[i]][0] +
  339. samples[i][1]*MSADPCMAdaptionCoeff[blockpred[i]][1]) / 256;
  340. pred += ((nibble^0x08) - 0x08) * delta[i];
  341. pred = clampi(pred, -32768, 32767);
  342. samples[i][1] = samples[i][0];
  343. samples[i][0] = pred;
  344. delta[i] = (MSADPCMAdaption[nibble] * delta[i]) / 256;
  345. delta[i] = maxi(16, delta[i]);
  346. *(dst++) = pred;
  347. }
  348. }
  349. }
  350. /* NOTE: This encoder is pretty dumb/simplistic. Some kind of pre-processing
  351. * that tries to find the optimal block predictors would be nice, at least. A
  352. * multi-pass method that can generate better deltas would be good, too. */
  353. static void EncodeMSADPCMBlock(ALmsadpcm *dst, const ALshort *src, ALint *sample, ALint numchans, ALsizei align)
  354. {
  355. ALubyte blockpred[MAX_INPUT_CHANNELS];
  356. ALint delta[MAX_INPUT_CHANNELS];
  357. ALshort samples[MAX_INPUT_CHANNELS][2];
  358. ALint i, j;
  359. /* Block predictor */
  360. for(i = 0;i < numchans;i++)
  361. {
  362. /* FIXME: Calculate something better. */
  363. blockpred[i] = 0;
  364. *(dst++) = blockpred[i];
  365. }
  366. /* Initial delta */
  367. for(i = 0;i < numchans;i++)
  368. {
  369. delta[i] = 16;
  370. *(dst++) = (delta[i] ) & 0xff;
  371. *(dst++) = (delta[i]>>8) & 0xff;
  372. }
  373. /* Initial sample 1 */
  374. for(i = 0;i < numchans;i++)
  375. {
  376. samples[i][0] = src[1*numchans + i];
  377. *(dst++) = (samples[i][0] ) & 0xff;
  378. *(dst++) = (samples[i][0]>>8) & 0xff;
  379. }
  380. /* Initial sample 2 */
  381. for(i = 0;i < numchans;i++)
  382. {
  383. samples[i][1] = src[i];
  384. *(dst++) = (samples[i][1] ) & 0xff;
  385. *(dst++) = (samples[i][1]>>8) & 0xff;
  386. }
  387. for(j = 2;j < align;j++)
  388. {
  389. for(i = 0;i < numchans;i++)
  390. {
  391. const ALint num = (j*numchans) + i;
  392. ALint nibble = 0;
  393. ALint bias;
  394. sample[i] = (samples[i][0]*MSADPCMAdaptionCoeff[blockpred[i]][0] +
  395. samples[i][1]*MSADPCMAdaptionCoeff[blockpred[i]][1]) / 256;
  396. nibble = src[num] - sample[i];
  397. if(nibble >= 0)
  398. bias = delta[i] / 2;
  399. else
  400. bias = -delta[i] / 2;
  401. nibble = (nibble + bias) / delta[i];
  402. nibble = clampi(nibble, -8, 7)&0x0f;
  403. sample[i] += ((nibble^0x08)-0x08) * delta[i];
  404. sample[i] = clampi(sample[i], -32768, 32767);
  405. samples[i][1] = samples[i][0];
  406. samples[i][0] = sample[i];
  407. delta[i] = (MSADPCMAdaption[nibble] * delta[i]) / 256;
  408. delta[i] = maxi(16, delta[i]);
  409. if(!(num&1))
  410. *dst = nibble << 4;
  411. else
  412. {
  413. *dst |= nibble;
  414. dst++;
  415. }
  416. }
  417. }
  418. }
  419. /* Define same-type pass-through sample conversion functions (excludes ADPCM,
  420. * which are block-based). */
  421. #define DECL_TEMPLATE(T) \
  422. static inline T Conv_##T##_##T(T val) { return val; }
  423. DECL_TEMPLATE(ALbyte);
  424. DECL_TEMPLATE(ALubyte);
  425. DECL_TEMPLATE(ALshort);
  426. DECL_TEMPLATE(ALushort);
  427. DECL_TEMPLATE(ALint);
  428. DECL_TEMPLATE(ALuint);
  429. DECL_TEMPLATE(ALalaw);
  430. DECL_TEMPLATE(ALmulaw);
  431. /* Slightly special handling for floats and doubles (converts NaN to 0, and
  432. * allows float<->double pass-through).
  433. */
  434. static inline ALfloat Conv_ALfloat_ALfloat(ALfloat val)
  435. { return (val==val) ? val : 0.0f; }
  436. static inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
  437. { return (val==val) ? (ALfloat)val : 0.0f; }
  438. static inline ALdouble Conv_ALdouble_ALfloat(ALfloat val)
  439. { return (val==val) ? (ALdouble)val : 0.0; }
  440. static inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
  441. { return (val==val) ? val : 0.0; }
  442. #undef DECL_TEMPLATE
  443. /* Define alternate-sign functions. */
  444. #define DECL_TEMPLATE(T1, T2, O) \
  445. static inline T1 Conv_##T1##_##T2(T2 val) { return (T1)val - O; } \
  446. static inline T2 Conv_##T2##_##T1(T1 val) { return (T2)val + O; }
  447. DECL_TEMPLATE(ALbyte, ALubyte, 128);
  448. DECL_TEMPLATE(ALshort, ALushort, 32768);
  449. DECL_TEMPLATE(ALint, ALuint, 2147483648u);
  450. #undef DECL_TEMPLATE
  451. /* Define int-type to int-type functions */
  452. #define DECL_TEMPLATE(T, ST, UT, SH) \
  453. static inline T Conv_##T##_##ST(ST val){ return val >> SH; } \
  454. static inline T Conv_##T##_##UT(UT val){ return Conv_##ST##_##UT(val) >> SH; }\
  455. static inline ST Conv_##ST##_##T(T val){ return val << SH; } \
  456. static inline UT Conv_##UT##_##T(T val){ return Conv_##UT##_##ST(val << SH); }
  457. #define DECL_TEMPLATE2(T1, T2, SH) \
  458. DECL_TEMPLATE(AL##T1, AL##T2, ALu##T2, SH) \
  459. DECL_TEMPLATE(ALu##T1, ALu##T2, AL##T2, SH)
  460. DECL_TEMPLATE2(byte, short, 8)
  461. DECL_TEMPLATE2(short, int, 16)
  462. DECL_TEMPLATE2(byte, int, 24)
  463. #undef DECL_TEMPLATE2
  464. #undef DECL_TEMPLATE
  465. /* Define int-type to fp functions */
  466. #define DECL_TEMPLATE(T, ST, UT, OP) \
  467. static inline T Conv_##T##_##ST(ST val) { return (T)val * OP; } \
  468. static inline T Conv_##T##_##UT(UT val) { return (T)Conv_##ST##_##UT(val) * OP; }
  469. #define DECL_TEMPLATE2(T1, T2, OP) \
  470. DECL_TEMPLATE(T1, AL##T2, ALu##T2, OP)
  471. DECL_TEMPLATE2(ALfloat, byte, (1.0f/128.0f))
  472. DECL_TEMPLATE2(ALdouble, byte, (1.0/128.0))
  473. DECL_TEMPLATE2(ALfloat, short, (1.0f/32768.0f))
  474. DECL_TEMPLATE2(ALdouble, short, (1.0/32768.0))
  475. DECL_TEMPLATE2(ALdouble, int, (1.0/2147483648.0))
  476. /* Special handling for int32 to float32, since it would overflow. */
  477. static inline ALfloat Conv_ALfloat_ALint(ALint val)
  478. { return (ALfloat)(val>>7) * (1.0f/16777216.0f); }
  479. static inline ALfloat Conv_ALfloat_ALuint(ALuint val)
  480. { return (ALfloat)(Conv_ALint_ALuint(val)>>7) * (1.0f/16777216.0f); }
  481. #undef DECL_TEMPLATE2
  482. #undef DECL_TEMPLATE
  483. /* Define fp to int-type functions */
  484. #define DECL_TEMPLATE(FT, T, smin, smax) \
  485. static inline AL##T Conv_AL##T##_##FT(FT val) \
  486. { \
  487. val *= (FT)smax + 1; \
  488. if(val >= (FT)smax) return smax; \
  489. if(val <= (FT)smin) return smin; \
  490. return (AL##T)val; \
  491. } \
  492. static inline ALu##T Conv_ALu##T##_##FT(FT val) \
  493. { return Conv_ALu##T##_AL##T(Conv_AL##T##_##FT(val)); }
  494. DECL_TEMPLATE(ALfloat, byte, -128, 127)
  495. DECL_TEMPLATE(ALdouble, byte, -128, 127)
  496. DECL_TEMPLATE(ALfloat, short, -32768, 32767)
  497. DECL_TEMPLATE(ALdouble, short, -32768, 32767)
  498. DECL_TEMPLATE(ALdouble, int, -2147483647-1, 2147483647)
  499. /* Special handling for float32 to int32, since it would overflow. */
  500. static inline ALint Conv_ALint_ALfloat(ALfloat val)
  501. {
  502. val *= 16777216.0f;
  503. if(val >= 16777215.0f) return 0x7fffff80/*16777215 << 7*/;
  504. if(val <= -16777216.0f) return 0x80000000/*-16777216 << 7*/;
  505. return (ALint)val << 7;
  506. }
  507. static inline ALuint Conv_ALuint_ALfloat(ALfloat val)
  508. { return Conv_ALuint_ALint(Conv_ALint_ALfloat(val)); }
  509. #undef DECL_TEMPLATE
  510. /* Define muLaw and aLaw functions (goes through short functions). */
  511. #define DECL_TEMPLATE(T) \
  512. static inline ALmulaw Conv_ALmulaw_##T(T val) \
  513. { return EncodeMuLaw(Conv_ALshort_##T(val)); } \
  514. static inline T Conv_##T##_ALmulaw(ALmulaw val) \
  515. { return Conv_##T##_ALshort(DecodeMuLaw(val)); } \
  516. \
  517. static inline ALalaw Conv_ALalaw_##T(T val) \
  518. { return EncodeALaw(Conv_ALshort_##T(val)); } \
  519. static inline T Conv_##T##_ALalaw(ALalaw val) \
  520. { return Conv_##T##_ALshort(DecodeALaw(val)); }
  521. DECL_TEMPLATE(ALbyte)
  522. DECL_TEMPLATE(ALubyte)
  523. DECL_TEMPLATE(ALshort)
  524. DECL_TEMPLATE(ALushort)
  525. DECL_TEMPLATE(ALint)
  526. DECL_TEMPLATE(ALuint)
  527. DECL_TEMPLATE(ALfloat)
  528. DECL_TEMPLATE(ALdouble)
  529. #undef DECL_TEMPLATE
  530. /* Define muLaw <-> aLaw functions. */
  531. static inline ALalaw Conv_ALalaw_ALmulaw(ALmulaw val)
  532. { return EncodeALaw(DecodeMuLaw(val)); }
  533. static inline ALmulaw Conv_ALmulaw_ALalaw(ALalaw val)
  534. { return EncodeMuLaw(DecodeALaw(val)); }
  535. #define DECL_TEMPLATE(T1, T2) \
  536. static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint numchans, \
  537. ALuint len, ALsizei UNUSED(align)) \
  538. { \
  539. ALuint i, j; \
  540. for(i = 0;i < len;i++) \
  541. { \
  542. for(j = 0;j < numchans;j++) \
  543. *(dst++) = Conv_##T1##_##T2(*(src++)); \
  544. } \
  545. }
  546. #define DECL_TEMPLATE2(T) \
  547. DECL_TEMPLATE(T, ALbyte) \
  548. DECL_TEMPLATE(T, ALubyte) \
  549. DECL_TEMPLATE(T, ALshort) \
  550. DECL_TEMPLATE(T, ALushort) \
  551. DECL_TEMPLATE(T, ALint) \
  552. DECL_TEMPLATE(T, ALuint) \
  553. DECL_TEMPLATE(T, ALfloat) \
  554. DECL_TEMPLATE(T, ALdouble) \
  555. DECL_TEMPLATE(T, ALmulaw) \
  556. DECL_TEMPLATE(T, ALalaw)
  557. DECL_TEMPLATE2(ALbyte)
  558. DECL_TEMPLATE2(ALubyte)
  559. DECL_TEMPLATE2(ALshort)
  560. DECL_TEMPLATE2(ALushort)
  561. DECL_TEMPLATE2(ALint)
  562. DECL_TEMPLATE2(ALuint)
  563. DECL_TEMPLATE2(ALfloat)
  564. DECL_TEMPLATE2(ALdouble)
  565. DECL_TEMPLATE2(ALmulaw)
  566. DECL_TEMPLATE2(ALalaw)
  567. #undef DECL_TEMPLATE2
  568. #undef DECL_TEMPLATE
  569. #define DECL_TEMPLATE(T) \
  570. static void Convert_##T##_ALima4(T *dst, const ALima4 *src, ALuint numchans, \
  571. ALuint len, ALuint align) \
  572. { \
  573. ALsizei byte_align = ((align-1)/2 + 4) * numchans; \
  574. DECL_VLA(ALshort, tmp, align*numchans); \
  575. ALuint i, j, k; \
  576. \
  577. assert(align > 0 && (len%align) == 0); \
  578. for(i = 0;i < len;i += align) \
  579. { \
  580. DecodeIMA4Block(tmp, src, numchans, align); \
  581. src += byte_align; \
  582. \
  583. for(j = 0;j < align;j++) \
  584. { \
  585. for(k = 0;k < numchans;k++) \
  586. *(dst++) = Conv_##T##_ALshort(tmp[j*numchans + k]); \
  587. } \
  588. } \
  589. }
  590. DECL_TEMPLATE(ALbyte)
  591. DECL_TEMPLATE(ALubyte)
  592. static void Convert_ALshort_ALima4(ALshort *dst, const ALima4 *src, ALuint numchans,
  593. ALuint len, ALuint align)
  594. {
  595. ALsizei byte_align = ((align-1)/2 + 4) * numchans;
  596. ALuint i;
  597. assert(align > 0 && (len%align) == 0);
  598. for(i = 0;i < len;i += align)
  599. {
  600. DecodeIMA4Block(dst, src, numchans, align);
  601. src += byte_align;
  602. dst += align*numchans;
  603. }
  604. }
  605. DECL_TEMPLATE(ALushort)
  606. DECL_TEMPLATE(ALint)
  607. DECL_TEMPLATE(ALuint)
  608. DECL_TEMPLATE(ALfloat)
  609. DECL_TEMPLATE(ALdouble)
  610. DECL_TEMPLATE(ALmulaw)
  611. DECL_TEMPLATE(ALalaw)
  612. #undef DECL_TEMPLATE
  613. #define DECL_TEMPLATE(T) \
  614. static void Convert_ALima4_##T(ALima4 *dst, const T *src, ALuint numchans, \
  615. ALuint len, ALuint align) \
  616. { \
  617. ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
  618. ALint index[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
  619. ALsizei byte_align = ((align-1)/2 + 4) * numchans; \
  620. DECL_VLA(ALshort, tmp, align*numchans); \
  621. ALuint i, j, k; \
  622. \
  623. assert(align > 0 && (len%align) == 0); \
  624. for(i = 0;i < len;i += align) \
  625. { \
  626. for(j = 0;j < align;j++) \
  627. { \
  628. for(k = 0;k < numchans;k++) \
  629. tmp[j*numchans + k] = Conv_ALshort_##T(*(src++)); \
  630. } \
  631. EncodeIMA4Block(dst, tmp, sample, index, numchans, align); \
  632. dst += byte_align; \
  633. } \
  634. }
  635. DECL_TEMPLATE(ALbyte)
  636. DECL_TEMPLATE(ALubyte)
  637. static void Convert_ALima4_ALshort(ALima4 *dst, const ALshort *src,
  638. ALuint numchans, ALuint len, ALuint align)
  639. {
  640. ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
  641. ALint index[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
  642. ALsizei byte_align = ((align-1)/2 + 4) * numchans;
  643. ALuint i;
  644. assert(align > 0 && (len%align) == 0);
  645. for(i = 0;i < len;i += align)
  646. {
  647. EncodeIMA4Block(dst, src, sample, index, numchans, align);
  648. src += align*numchans;
  649. dst += byte_align;
  650. }
  651. }
  652. DECL_TEMPLATE(ALushort)
  653. DECL_TEMPLATE(ALint)
  654. DECL_TEMPLATE(ALuint)
  655. DECL_TEMPLATE(ALfloat)
  656. DECL_TEMPLATE(ALdouble)
  657. DECL_TEMPLATE(ALmulaw)
  658. DECL_TEMPLATE(ALalaw)
  659. #undef DECL_TEMPLATE
  660. #define DECL_TEMPLATE(T) \
  661. static void Convert_##T##_ALmsadpcm(T *dst, const ALmsadpcm *src, \
  662. ALuint numchans, ALuint len, \
  663. ALuint align) \
  664. { \
  665. ALsizei byte_align = ((align-2)/2 + 7) * numchans; \
  666. DECL_VLA(ALshort, tmp, align*numchans); \
  667. ALuint i, j, k; \
  668. \
  669. assert(align > 1 && (len%align) == 0); \
  670. for(i = 0;i < len;i += align) \
  671. { \
  672. DecodeMSADPCMBlock(tmp, src, numchans, align); \
  673. src += byte_align; \
  674. \
  675. for(j = 0;j < align;j++) \
  676. { \
  677. for(k = 0;k < numchans;k++) \
  678. *(dst++) = Conv_##T##_ALshort(tmp[j*numchans + k]); \
  679. } \
  680. } \
  681. }
  682. DECL_TEMPLATE(ALbyte)
  683. DECL_TEMPLATE(ALubyte)
  684. static void Convert_ALshort_ALmsadpcm(ALshort *dst, const ALmsadpcm *src,
  685. ALuint numchans, ALuint len,
  686. ALuint align)
  687. {
  688. ALsizei byte_align = ((align-2)/2 + 7) * numchans;
  689. ALuint i;
  690. assert(align > 1 && (len%align) == 0);
  691. for(i = 0;i < len;i += align)
  692. {
  693. DecodeMSADPCMBlock(dst, src, numchans, align);
  694. src += byte_align;
  695. dst += align*numchans;
  696. }
  697. }
  698. DECL_TEMPLATE(ALushort)
  699. DECL_TEMPLATE(ALint)
  700. DECL_TEMPLATE(ALuint)
  701. DECL_TEMPLATE(ALfloat)
  702. DECL_TEMPLATE(ALdouble)
  703. DECL_TEMPLATE(ALmulaw)
  704. DECL_TEMPLATE(ALalaw)
  705. #undef DECL_TEMPLATE
  706. #define DECL_TEMPLATE(T) \
  707. static void Convert_ALmsadpcm_##T(ALmsadpcm *dst, const T *src, \
  708. ALuint numchans, ALuint len, ALuint align) \
  709. { \
  710. ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
  711. ALsizei byte_align = ((align-2)/2 + 7) * numchans; \
  712. DECL_VLA(ALshort, tmp, align*numchans); \
  713. ALuint i, j, k; \
  714. \
  715. assert(align > 1 && (len%align) == 0); \
  716. for(i = 0;i < len;i += align) \
  717. { \
  718. for(j = 0;j < align;j++) \
  719. { \
  720. for(k = 0;k < numchans;k++) \
  721. tmp[j*numchans + k] = Conv_ALshort_##T(*(src++)); \
  722. } \
  723. EncodeMSADPCMBlock(dst, tmp, sample, numchans, align); \
  724. dst += byte_align; \
  725. } \
  726. }
  727. DECL_TEMPLATE(ALbyte)
  728. DECL_TEMPLATE(ALubyte)
  729. static void Convert_ALmsadpcm_ALshort(ALmsadpcm *dst, const ALshort *src,
  730. ALuint numchans, ALuint len, ALuint align)
  731. {
  732. ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0};
  733. ALsizei byte_align = ((align-2)/2 + 7) * numchans;
  734. ALuint i;
  735. assert(align > 1 && (len%align) == 0);
  736. for(i = 0;i < len;i += align)
  737. {
  738. EncodeMSADPCMBlock(dst, src, sample, numchans, align);
  739. src += align*numchans;
  740. dst += byte_align;
  741. }
  742. }
  743. DECL_TEMPLATE(ALushort)
  744. DECL_TEMPLATE(ALint)
  745. DECL_TEMPLATE(ALuint)
  746. DECL_TEMPLATE(ALfloat)
  747. DECL_TEMPLATE(ALdouble)
  748. DECL_TEMPLATE(ALmulaw)
  749. DECL_TEMPLATE(ALalaw)
  750. #undef DECL_TEMPLATE
  751. /* NOTE: We don't store compressed samples internally, so these conversions
  752. * should never happen. */
  753. static void Convert_ALima4_ALima4(ALima4* UNUSED(dst), const ALima4* UNUSED(src),
  754. ALuint UNUSED(numchans), ALuint UNUSED(len),
  755. ALuint UNUSED(align))
  756. {
  757. ERR("Unexpected IMA4-to-IMA4 conversion!\n");
  758. }
  759. static void Convert_ALmsadpcm_ALmsadpcm(ALmsadpcm* UNUSED(dst), const ALmsadpcm* UNUSED(src),
  760. ALuint UNUSED(numchans), ALuint UNUSED(len),
  761. ALuint UNUSED(align))
  762. {
  763. ERR("Unexpected MSADPCM-to-MSADPCM conversion!\n");
  764. }
  765. static void Convert_ALmsadpcm_ALima4(ALmsadpcm* UNUSED(dst), const ALima4* UNUSED(src),
  766. ALuint UNUSED(numchans), ALuint UNUSED(len),
  767. ALuint UNUSED(align))
  768. {
  769. ERR("Unexpected IMA4-to-MSADPCM conversion!\n");
  770. }
  771. static void Convert_ALima4_ALmsadpcm(ALima4* UNUSED(dst), const ALmsadpcm* UNUSED(src),
  772. ALuint UNUSED(numchans), ALuint UNUSED(len),
  773. ALuint UNUSED(align))
  774. {
  775. ERR("Unexpected MSADPCM-to-IMA4 conversion!\n");
  776. }
  777. #define DECL_TEMPLATE(T) \
  778. static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
  779. ALsizei numchans, ALsizei len, ALsizei align) \
  780. { \
  781. switch(srcType) \
  782. { \
  783. case UserFmtByte: \
  784. Convert_##T##_ALbyte(dst, src, numchans, len, align); \
  785. break; \
  786. case UserFmtUByte: \
  787. Convert_##T##_ALubyte(dst, src, numchans, len, align); \
  788. break; \
  789. case UserFmtShort: \
  790. Convert_##T##_ALshort(dst, src, numchans, len, align); \
  791. break; \
  792. case UserFmtUShort: \
  793. Convert_##T##_ALushort(dst, src, numchans, len, align); \
  794. break; \
  795. case UserFmtInt: \
  796. Convert_##T##_ALint(dst, src, numchans, len, align); \
  797. break; \
  798. case UserFmtUInt: \
  799. Convert_##T##_ALuint(dst, src, numchans, len, align); \
  800. break; \
  801. case UserFmtFloat: \
  802. Convert_##T##_ALfloat(dst, src, numchans, len, align); \
  803. break; \
  804. case UserFmtDouble: \
  805. Convert_##T##_ALdouble(dst, src, numchans, len, align); \
  806. break; \
  807. case UserFmtMulaw: \
  808. Convert_##T##_ALmulaw(dst, src, numchans, len, align); \
  809. break; \
  810. case UserFmtAlaw: \
  811. Convert_##T##_ALalaw(dst, src, numchans, len, align); \
  812. break; \
  813. case UserFmtIMA4: \
  814. Convert_##T##_ALima4(dst, src, numchans, len, align); \
  815. break; \
  816. case UserFmtMSADPCM: \
  817. Convert_##T##_ALmsadpcm(dst, src, numchans, len, align); \
  818. break; \
  819. } \
  820. }
  821. DECL_TEMPLATE(ALbyte)
  822. DECL_TEMPLATE(ALubyte)
  823. DECL_TEMPLATE(ALshort)
  824. DECL_TEMPLATE(ALushort)
  825. DECL_TEMPLATE(ALint)
  826. DECL_TEMPLATE(ALuint)
  827. DECL_TEMPLATE(ALfloat)
  828. DECL_TEMPLATE(ALdouble)
  829. DECL_TEMPLATE(ALmulaw)
  830. DECL_TEMPLATE(ALalaw)
  831. DECL_TEMPLATE(ALima4)
  832. DECL_TEMPLATE(ALmsadpcm)
  833. #undef DECL_TEMPLATE
  834. void ConvertData(ALvoid *dst, enum UserFmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei numchans, ALsizei len, ALsizei align)
  835. {
  836. switch(dstType)
  837. {
  838. case UserFmtByte:
  839. Convert_ALbyte(dst, src, srcType, numchans, len, align);
  840. break;
  841. case UserFmtUByte:
  842. Convert_ALubyte(dst, src, srcType, numchans, len, align);
  843. break;
  844. case UserFmtShort:
  845. Convert_ALshort(dst, src, srcType, numchans, len, align);
  846. break;
  847. case UserFmtUShort:
  848. Convert_ALushort(dst, src, srcType, numchans, len, align);
  849. break;
  850. case UserFmtInt:
  851. Convert_ALint(dst, src, srcType, numchans, len, align);
  852. break;
  853. case UserFmtUInt:
  854. Convert_ALuint(dst, src, srcType, numchans, len, align);
  855. break;
  856. case UserFmtFloat:
  857. Convert_ALfloat(dst, src, srcType, numchans, len, align);
  858. break;
  859. case UserFmtDouble:
  860. Convert_ALdouble(dst, src, srcType, numchans, len, align);
  861. break;
  862. case UserFmtMulaw:
  863. Convert_ALmulaw(dst, src, srcType, numchans, len, align);
  864. break;
  865. case UserFmtAlaw:
  866. Convert_ALalaw(dst, src, srcType, numchans, len, align);
  867. break;
  868. case UserFmtIMA4:
  869. Convert_ALima4(dst, src, srcType, numchans, len, align);
  870. break;
  871. case UserFmtMSADPCM:
  872. Convert_ALmsadpcm(dst, src, srcType, numchans, len, align);
  873. break;
  874. }
  875. }