dsp_audio.c 11 KB

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