dsp_audio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Audio support data for mISDN_dsp.
  3. *
  4. * Copyright 2002/2003 by Andreas Eversberg (jolly@eversberg.eu)
  5. * Rewritten by Peter
  6. *
  7. * This software may be used and distributed according to the terms
  8. * of the GNU General Public License, incorporated herein by reference.
  9. *
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/mISDNif.h>
  13. #include <linux/mISDNdsp.h>
  14. #include <linux/export.h>
  15. #include "core.h"
  16. #include "dsp.h"
  17. /* ulaw[unsigned char] -> signed 16-bit */
  18. s32 dsp_audio_ulaw_to_s32[256];
  19. /* alaw[unsigned char] -> signed 16-bit */
  20. s32 dsp_audio_alaw_to_s32[256];
  21. s32 *dsp_audio_law_to_s32;
  22. EXPORT_SYMBOL(dsp_audio_law_to_s32);
  23. /* signed 16-bit -> law */
  24. u8 dsp_audio_s16_to_law[65536];
  25. EXPORT_SYMBOL(dsp_audio_s16_to_law);
  26. /* alaw -> ulaw */
  27. u8 dsp_audio_alaw_to_ulaw[256];
  28. /* ulaw -> alaw */
  29. static u8 dsp_audio_ulaw_to_alaw[256];
  30. u8 dsp_silence;
  31. /*****************************************************
  32. * generate table for conversion of s16 to alaw/ulaw *
  33. *****************************************************/
  34. #define AMI_MASK 0x55
  35. static inline unsigned char linear2alaw(short int linear)
  36. {
  37. int mask;
  38. int seg;
  39. int pcm_val;
  40. static int seg_end[8] = {
  41. 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
  42. };
  43. pcm_val = linear;
  44. if (pcm_val >= 0) {
  45. /* Sign (7th) bit = 1 */
  46. mask = AMI_MASK | 0x80;
  47. } else {
  48. /* Sign bit = 0 */
  49. mask = AMI_MASK;
  50. pcm_val = -pcm_val;
  51. }
  52. /* Convert the scaled magnitude to segment number. */
  53. for (seg = 0; seg < 8; seg++) {
  54. if (pcm_val <= seg_end[seg])
  55. break;
  56. }
  57. /* Combine the sign, segment, and quantization bits. */
  58. return ((seg << 4) |
  59. ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask;
  60. }
  61. static inline short int alaw2linear(unsigned char alaw)
  62. {
  63. int i;
  64. int seg;
  65. alaw ^= AMI_MASK;
  66. i = ((alaw & 0x0F) << 4) + 8 /* rounding error */;
  67. seg = (((int) alaw & 0x70) >> 4);
  68. if (seg)
  69. i = (i + 0x100) << (seg - 1);
  70. return (short int) ((alaw & 0x80) ? i : -i);
  71. }
  72. static inline short int ulaw2linear(unsigned char ulaw)
  73. {
  74. short mu, e, f, y;
  75. static short etab[] = {0, 132, 396, 924, 1980, 4092, 8316, 16764};
  76. mu = 255 - ulaw;
  77. e = (mu & 0x70) / 16;
  78. f = mu & 0x0f;
  79. y = f * (1 << (e + 3));
  80. y += etab[e];
  81. if (mu & 0x80)
  82. y = -y;
  83. return y;
  84. }
  85. #define BIAS 0x84 /*!< define the add-in bias for 16 bit samples */
  86. static unsigned char linear2ulaw(short sample)
  87. {
  88. static int exp_lut[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. int sign, exponent, mantissa;
  106. unsigned char ulawbyte;
  107. /* Get the sample into sign-magnitude. */
  108. sign = (sample >> 8) & 0x80; /* set aside the sign */
  109. if (sign != 0)
  110. sample = -sample; /* get magnitude */
  111. /* Convert from 16 bit linear to ulaw. */
  112. sample = sample + BIAS;
  113. exponent = exp_lut[(sample >> 7) & 0xFF];
  114. mantissa = (sample >> (exponent + 3)) & 0x0F;
  115. ulawbyte = ~(sign | (exponent << 4) | mantissa);
  116. return ulawbyte;
  117. }
  118. static int reverse_bits(int i)
  119. {
  120. int z, j;
  121. z = 0;
  122. for (j = 0; j < 8; j++) {
  123. if ((i & (1 << j)) != 0)
  124. z |= 1 << (7 - j);
  125. }
  126. return z;
  127. }
  128. void dsp_audio_generate_law_tables(void)
  129. {
  130. int i;
  131. for (i = 0; i < 256; i++)
  132. dsp_audio_alaw_to_s32[i] = alaw2linear(reverse_bits(i));
  133. for (i = 0; i < 256; i++)
  134. dsp_audio_ulaw_to_s32[i] = ulaw2linear(reverse_bits(i));
  135. for (i = 0; i < 256; i++) {
  136. dsp_audio_alaw_to_ulaw[i] =
  137. linear2ulaw(dsp_audio_alaw_to_s32[i]);
  138. dsp_audio_ulaw_to_alaw[i] =
  139. linear2alaw(dsp_audio_ulaw_to_s32[i]);
  140. }
  141. }
  142. void
  143. dsp_audio_generate_s2law_table(void)
  144. {
  145. int i;
  146. if (dsp_options & DSP_OPT_ULAW) {
  147. /* generating ulaw-table */
  148. for (i = -32768; i < 32768; i++) {
  149. dsp_audio_s16_to_law[i & 0xffff] =
  150. reverse_bits(linear2ulaw(i));
  151. }
  152. } else {
  153. /* generating alaw-table */
  154. for (i = -32768; i < 32768; i++) {
  155. dsp_audio_s16_to_law[i & 0xffff] =
  156. reverse_bits(linear2alaw(i));
  157. }
  158. }
  159. }
  160. /*
  161. * the seven bit sample is the number of every second alaw-sample ordered by
  162. * aplitude. 0x00 is negative, 0x7f is positive amplitude.
  163. */
  164. u8 dsp_audio_seven2law[128];
  165. u8 dsp_audio_law2seven[256];
  166. /********************************************************************
  167. * generate table for conversion law from/to 7-bit alaw-like sample *
  168. ********************************************************************/
  169. void
  170. dsp_audio_generate_seven(void)
  171. {
  172. int i, j, k;
  173. u8 spl;
  174. u8 sorted_alaw[256];
  175. /* generate alaw table, sorted by the linear value */
  176. for (i = 0; i < 256; i++) {
  177. j = 0;
  178. for (k = 0; k < 256; k++) {
  179. if (dsp_audio_alaw_to_s32[k]
  180. < dsp_audio_alaw_to_s32[i])
  181. j++;
  182. }
  183. sorted_alaw[j] = i;
  184. }
  185. /* generate tabels */
  186. for (i = 0; i < 256; i++) {
  187. /* spl is the source: the law-sample (converted to alaw) */
  188. spl = i;
  189. if (dsp_options & DSP_OPT_ULAW)
  190. spl = dsp_audio_ulaw_to_alaw[i];
  191. /* find the 7-bit-sample */
  192. for (j = 0; j < 256; j++) {
  193. if (sorted_alaw[j] == spl)
  194. break;
  195. }
  196. /* write 7-bit audio value */
  197. dsp_audio_law2seven[i] = j >> 1;
  198. }
  199. for (i = 0; i < 128; i++) {
  200. spl = sorted_alaw[i << 1];
  201. if (dsp_options & DSP_OPT_ULAW)
  202. spl = dsp_audio_alaw_to_ulaw[spl];
  203. dsp_audio_seven2law[i] = spl;
  204. }
  205. }
  206. /* mix 2*law -> law */
  207. u8 dsp_audio_mix_law[65536];
  208. /******************************************************
  209. * generate mix table to mix two law samples into one *
  210. ******************************************************/
  211. void
  212. dsp_audio_generate_mix_table(void)
  213. {
  214. int i, j;
  215. s32 sample;
  216. i = 0;
  217. while (i < 256) {
  218. j = 0;
  219. while (j < 256) {
  220. sample = dsp_audio_law_to_s32[i];
  221. sample += dsp_audio_law_to_s32[j];
  222. if (sample > 32767)
  223. sample = 32767;
  224. if (sample < -32768)
  225. sample = -32768;
  226. dsp_audio_mix_law[(i << 8) | j] =
  227. dsp_audio_s16_to_law[sample & 0xffff];
  228. j++;
  229. }
  230. i++;
  231. }
  232. }
  233. /*************************************
  234. * generate different volume changes *
  235. *************************************/
  236. static u8 dsp_audio_reduce8[256];
  237. static u8 dsp_audio_reduce7[256];
  238. static u8 dsp_audio_reduce6[256];
  239. static u8 dsp_audio_reduce5[256];
  240. static u8 dsp_audio_reduce4[256];
  241. static u8 dsp_audio_reduce3[256];
  242. static u8 dsp_audio_reduce2[256];
  243. static u8 dsp_audio_reduce1[256];
  244. static u8 dsp_audio_increase1[256];
  245. static u8 dsp_audio_increase2[256];
  246. static u8 dsp_audio_increase3[256];
  247. static u8 dsp_audio_increase4[256];
  248. static u8 dsp_audio_increase5[256];
  249. static u8 dsp_audio_increase6[256];
  250. static u8 dsp_audio_increase7[256];
  251. static u8 dsp_audio_increase8[256];
  252. static u8 *dsp_audio_volume_change[16] = {
  253. dsp_audio_reduce8,
  254. dsp_audio_reduce7,
  255. dsp_audio_reduce6,
  256. dsp_audio_reduce5,
  257. dsp_audio_reduce4,
  258. dsp_audio_reduce3,
  259. dsp_audio_reduce2,
  260. dsp_audio_reduce1,
  261. dsp_audio_increase1,
  262. dsp_audio_increase2,
  263. dsp_audio_increase3,
  264. dsp_audio_increase4,
  265. dsp_audio_increase5,
  266. dsp_audio_increase6,
  267. dsp_audio_increase7,
  268. dsp_audio_increase8,
  269. };
  270. void
  271. dsp_audio_generate_volume_changes(void)
  272. {
  273. register s32 sample;
  274. int i;
  275. int num[] = { 110, 125, 150, 175, 200, 300, 400, 500 };
  276. int denum[] = { 100, 100, 100, 100, 100, 100, 100, 100 };
  277. i = 0;
  278. while (i < 256) {
  279. dsp_audio_reduce8[i] = dsp_audio_s16_to_law[
  280. (dsp_audio_law_to_s32[i] * denum[7] / num[7]) & 0xffff];
  281. dsp_audio_reduce7[i] = dsp_audio_s16_to_law[
  282. (dsp_audio_law_to_s32[i] * denum[6] / num[6]) & 0xffff];
  283. dsp_audio_reduce6[i] = dsp_audio_s16_to_law[
  284. (dsp_audio_law_to_s32[i] * denum[5] / num[5]) & 0xffff];
  285. dsp_audio_reduce5[i] = dsp_audio_s16_to_law[
  286. (dsp_audio_law_to_s32[i] * denum[4] / num[4]) & 0xffff];
  287. dsp_audio_reduce4[i] = dsp_audio_s16_to_law[
  288. (dsp_audio_law_to_s32[i] * denum[3] / num[3]) & 0xffff];
  289. dsp_audio_reduce3[i] = dsp_audio_s16_to_law[
  290. (dsp_audio_law_to_s32[i] * denum[2] / num[2]) & 0xffff];
  291. dsp_audio_reduce2[i] = dsp_audio_s16_to_law[
  292. (dsp_audio_law_to_s32[i] * denum[1] / num[1]) & 0xffff];
  293. dsp_audio_reduce1[i] = dsp_audio_s16_to_law[
  294. (dsp_audio_law_to_s32[i] * denum[0] / num[0]) & 0xffff];
  295. sample = dsp_audio_law_to_s32[i] * num[0] / denum[0];
  296. if (sample < -32768)
  297. sample = -32768;
  298. else if (sample > 32767)
  299. sample = 32767;
  300. dsp_audio_increase1[i] = dsp_audio_s16_to_law[sample & 0xffff];
  301. sample = dsp_audio_law_to_s32[i] * num[1] / denum[1];
  302. if (sample < -32768)
  303. sample = -32768;
  304. else if (sample > 32767)
  305. sample = 32767;
  306. dsp_audio_increase2[i] = dsp_audio_s16_to_law[sample & 0xffff];
  307. sample = dsp_audio_law_to_s32[i] * num[2] / denum[2];
  308. if (sample < -32768)
  309. sample = -32768;
  310. else if (sample > 32767)
  311. sample = 32767;
  312. dsp_audio_increase3[i] = dsp_audio_s16_to_law[sample & 0xffff];
  313. sample = dsp_audio_law_to_s32[i] * num[3] / denum[3];
  314. if (sample < -32768)
  315. sample = -32768;
  316. else if (sample > 32767)
  317. sample = 32767;
  318. dsp_audio_increase4[i] = dsp_audio_s16_to_law[sample & 0xffff];
  319. sample = dsp_audio_law_to_s32[i] * num[4] / denum[4];
  320. if (sample < -32768)
  321. sample = -32768;
  322. else if (sample > 32767)
  323. sample = 32767;
  324. dsp_audio_increase5[i] = dsp_audio_s16_to_law[sample & 0xffff];
  325. sample = dsp_audio_law_to_s32[i] * num[5] / denum[5];
  326. if (sample < -32768)
  327. sample = -32768;
  328. else if (sample > 32767)
  329. sample = 32767;
  330. dsp_audio_increase6[i] = dsp_audio_s16_to_law[sample & 0xffff];
  331. sample = dsp_audio_law_to_s32[i] * num[6] / denum[6];
  332. if (sample < -32768)
  333. sample = -32768;
  334. else if (sample > 32767)
  335. sample = 32767;
  336. dsp_audio_increase7[i] = dsp_audio_s16_to_law[sample & 0xffff];
  337. sample = dsp_audio_law_to_s32[i] * num[7] / denum[7];
  338. if (sample < -32768)
  339. sample = -32768;
  340. else if (sample > 32767)
  341. sample = 32767;
  342. dsp_audio_increase8[i] = dsp_audio_s16_to_law[sample & 0xffff];
  343. i++;
  344. }
  345. }
  346. /**************************************
  347. * change the volume of the given skb *
  348. **************************************/
  349. /* this is a helper function for changing volume of skb. the range may be
  350. * -8 to 8, which is a shift to the power of 2. 0 == no volume, 3 == volume*8
  351. */
  352. void
  353. dsp_change_volume(struct sk_buff *skb, int volume)
  354. {
  355. u8 *volume_change;
  356. int i, ii;
  357. u8 *p;
  358. int shift;
  359. if (volume == 0)
  360. return;
  361. /* get correct conversion table */
  362. if (volume < 0) {
  363. shift = volume + 8;
  364. if (shift < 0)
  365. shift = 0;
  366. } else {
  367. shift = volume + 7;
  368. if (shift > 15)
  369. shift = 15;
  370. }
  371. volume_change = dsp_audio_volume_change[shift];
  372. i = 0;
  373. ii = skb->len;
  374. p = skb->data;
  375. /* change volume */
  376. while (i < ii) {
  377. *p = volume_change[*p];
  378. p++;
  379. i++;
  380. }
  381. }