PageRenderTime 27ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/isdn/mISDN/dsp_audio.c

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