isdn_audio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /* $Id: isdn_audio.c,v 1.1.2.2 2004/01/12 22:37:18 keil Exp $
  2. *
  3. * Linux ISDN subsystem, audio conversion and compression (linklevel).
  4. *
  5. * Copyright 1994-1999 by Fritz Elfert (fritz@isdn4linux.de)
  6. * DTMF code (c) 1996 by Christian Mock (cm@kukuruz.ping.at)
  7. * Silence detection (c) 1998 by Armin Schindler (mac@gismo.telekom.de)
  8. *
  9. * This software may be used and distributed according to the terms
  10. * of the GNU General Public License, incorporated herein by reference.
  11. *
  12. */
  13. #include <linux/isdn.h>
  14. #include <linux/slab.h>
  15. #include "isdn_audio.h"
  16. #include "isdn_common.h"
  17. char *isdn_audio_revision = "$Revision: 1.1.2.2 $";
  18. /*
  19. * Misc. lookup-tables.
  20. */
  21. /* ulaw -> signed 16-bit */
  22. static short isdn_audio_ulaw_to_s16[] =
  23. {
  24. 0x8284, 0x8684, 0x8a84, 0x8e84, 0x9284, 0x9684, 0x9a84, 0x9e84,
  25. 0xa284, 0xa684, 0xaa84, 0xae84, 0xb284, 0xb684, 0xba84, 0xbe84,
  26. 0xc184, 0xc384, 0xc584, 0xc784, 0xc984, 0xcb84, 0xcd84, 0xcf84,
  27. 0xd184, 0xd384, 0xd584, 0xd784, 0xd984, 0xdb84, 0xdd84, 0xdf84,
  28. 0xe104, 0xe204, 0xe304, 0xe404, 0xe504, 0xe604, 0xe704, 0xe804,
  29. 0xe904, 0xea04, 0xeb04, 0xec04, 0xed04, 0xee04, 0xef04, 0xf004,
  30. 0xf0c4, 0xf144, 0xf1c4, 0xf244, 0xf2c4, 0xf344, 0xf3c4, 0xf444,
  31. 0xf4c4, 0xf544, 0xf5c4, 0xf644, 0xf6c4, 0xf744, 0xf7c4, 0xf844,
  32. 0xf8a4, 0xf8e4, 0xf924, 0xf964, 0xf9a4, 0xf9e4, 0xfa24, 0xfa64,
  33. 0xfaa4, 0xfae4, 0xfb24, 0xfb64, 0xfba4, 0xfbe4, 0xfc24, 0xfc64,
  34. 0xfc94, 0xfcb4, 0xfcd4, 0xfcf4, 0xfd14, 0xfd34, 0xfd54, 0xfd74,
  35. 0xfd94, 0xfdb4, 0xfdd4, 0xfdf4, 0xfe14, 0xfe34, 0xfe54, 0xfe74,
  36. 0xfe8c, 0xfe9c, 0xfeac, 0xfebc, 0xfecc, 0xfedc, 0xfeec, 0xfefc,
  37. 0xff0c, 0xff1c, 0xff2c, 0xff3c, 0xff4c, 0xff5c, 0xff6c, 0xff7c,
  38. 0xff88, 0xff90, 0xff98, 0xffa0, 0xffa8, 0xffb0, 0xffb8, 0xffc0,
  39. 0xffc8, 0xffd0, 0xffd8, 0xffe0, 0xffe8, 0xfff0, 0xfff8, 0x0000,
  40. 0x7d7c, 0x797c, 0x757c, 0x717c, 0x6d7c, 0x697c, 0x657c, 0x617c,
  41. 0x5d7c, 0x597c, 0x557c, 0x517c, 0x4d7c, 0x497c, 0x457c, 0x417c,
  42. 0x3e7c, 0x3c7c, 0x3a7c, 0x387c, 0x367c, 0x347c, 0x327c, 0x307c,
  43. 0x2e7c, 0x2c7c, 0x2a7c, 0x287c, 0x267c, 0x247c, 0x227c, 0x207c,
  44. 0x1efc, 0x1dfc, 0x1cfc, 0x1bfc, 0x1afc, 0x19fc, 0x18fc, 0x17fc,
  45. 0x16fc, 0x15fc, 0x14fc, 0x13fc, 0x12fc, 0x11fc, 0x10fc, 0x0ffc,
  46. 0x0f3c, 0x0ebc, 0x0e3c, 0x0dbc, 0x0d3c, 0x0cbc, 0x0c3c, 0x0bbc,
  47. 0x0b3c, 0x0abc, 0x0a3c, 0x09bc, 0x093c, 0x08bc, 0x083c, 0x07bc,
  48. 0x075c, 0x071c, 0x06dc, 0x069c, 0x065c, 0x061c, 0x05dc, 0x059c,
  49. 0x055c, 0x051c, 0x04dc, 0x049c, 0x045c, 0x041c, 0x03dc, 0x039c,
  50. 0x036c, 0x034c, 0x032c, 0x030c, 0x02ec, 0x02cc, 0x02ac, 0x028c,
  51. 0x026c, 0x024c, 0x022c, 0x020c, 0x01ec, 0x01cc, 0x01ac, 0x018c,
  52. 0x0174, 0x0164, 0x0154, 0x0144, 0x0134, 0x0124, 0x0114, 0x0104,
  53. 0x00f4, 0x00e4, 0x00d4, 0x00c4, 0x00b4, 0x00a4, 0x0094, 0x0084,
  54. 0x0078, 0x0070, 0x0068, 0x0060, 0x0058, 0x0050, 0x0048, 0x0040,
  55. 0x0038, 0x0030, 0x0028, 0x0020, 0x0018, 0x0010, 0x0008, 0x0000
  56. };
  57. /* alaw -> signed 16-bit */
  58. static short isdn_audio_alaw_to_s16[] =
  59. {
  60. 0x13fc, 0xec04, 0x0144, 0xfebc, 0x517c, 0xae84, 0x051c, 0xfae4,
  61. 0x0a3c, 0xf5c4, 0x0048, 0xffb8, 0x287c, 0xd784, 0x028c, 0xfd74,
  62. 0x1bfc, 0xe404, 0x01cc, 0xfe34, 0x717c, 0x8e84, 0x071c, 0xf8e4,
  63. 0x0e3c, 0xf1c4, 0x00c4, 0xff3c, 0x387c, 0xc784, 0x039c, 0xfc64,
  64. 0x0ffc, 0xf004, 0x0104, 0xfefc, 0x417c, 0xbe84, 0x041c, 0xfbe4,
  65. 0x083c, 0xf7c4, 0x0008, 0xfff8, 0x207c, 0xdf84, 0x020c, 0xfdf4,
  66. 0x17fc, 0xe804, 0x018c, 0xfe74, 0x617c, 0x9e84, 0x061c, 0xf9e4,
  67. 0x0c3c, 0xf3c4, 0x0084, 0xff7c, 0x307c, 0xcf84, 0x030c, 0xfcf4,
  68. 0x15fc, 0xea04, 0x0164, 0xfe9c, 0x597c, 0xa684, 0x059c, 0xfa64,
  69. 0x0b3c, 0xf4c4, 0x0068, 0xff98, 0x2c7c, 0xd384, 0x02cc, 0xfd34,
  70. 0x1dfc, 0xe204, 0x01ec, 0xfe14, 0x797c, 0x8684, 0x07bc, 0xf844,
  71. 0x0f3c, 0xf0c4, 0x00e4, 0xff1c, 0x3c7c, 0xc384, 0x03dc, 0xfc24,
  72. 0x11fc, 0xee04, 0x0124, 0xfedc, 0x497c, 0xb684, 0x049c, 0xfb64,
  73. 0x093c, 0xf6c4, 0x0028, 0xffd8, 0x247c, 0xdb84, 0x024c, 0xfdb4,
  74. 0x19fc, 0xe604, 0x01ac, 0xfe54, 0x697c, 0x9684, 0x069c, 0xf964,
  75. 0x0d3c, 0xf2c4, 0x00a4, 0xff5c, 0x347c, 0xcb84, 0x034c, 0xfcb4,
  76. 0x12fc, 0xed04, 0x0134, 0xfecc, 0x4d7c, 0xb284, 0x04dc, 0xfb24,
  77. 0x09bc, 0xf644, 0x0038, 0xffc8, 0x267c, 0xd984, 0x026c, 0xfd94,
  78. 0x1afc, 0xe504, 0x01ac, 0xfe54, 0x6d7c, 0x9284, 0x06dc, 0xf924,
  79. 0x0dbc, 0xf244, 0x00b4, 0xff4c, 0x367c, 0xc984, 0x036c, 0xfc94,
  80. 0x0f3c, 0xf0c4, 0x00f4, 0xff0c, 0x3e7c, 0xc184, 0x03dc, 0xfc24,
  81. 0x07bc, 0xf844, 0x0008, 0xfff8, 0x1efc, 0xe104, 0x01ec, 0xfe14,
  82. 0x16fc, 0xe904, 0x0174, 0xfe8c, 0x5d7c, 0xa284, 0x05dc, 0xfa24,
  83. 0x0bbc, 0xf444, 0x0078, 0xff88, 0x2e7c, 0xd184, 0x02ec, 0xfd14,
  84. 0x14fc, 0xeb04, 0x0154, 0xfeac, 0x557c, 0xaa84, 0x055c, 0xfaa4,
  85. 0x0abc, 0xf544, 0x0058, 0xffa8, 0x2a7c, 0xd584, 0x02ac, 0xfd54,
  86. 0x1cfc, 0xe304, 0x01cc, 0xfe34, 0x757c, 0x8a84, 0x075c, 0xf8a4,
  87. 0x0ebc, 0xf144, 0x00d4, 0xff2c, 0x3a7c, 0xc584, 0x039c, 0xfc64,
  88. 0x10fc, 0xef04, 0x0114, 0xfeec, 0x457c, 0xba84, 0x045c, 0xfba4,
  89. 0x08bc, 0xf744, 0x0018, 0xffe8, 0x227c, 0xdd84, 0x022c, 0xfdd4,
  90. 0x18fc, 0xe704, 0x018c, 0xfe74, 0x657c, 0x9a84, 0x065c, 0xf9a4,
  91. 0x0cbc, 0xf344, 0x0094, 0xff6c, 0x327c, 0xcd84, 0x032c, 0xfcd4
  92. };
  93. /* alaw -> ulaw */
  94. static char isdn_audio_alaw_to_ulaw[] =
  95. {
  96. 0xab, 0x2b, 0xe3, 0x63, 0x8b, 0x0b, 0xc9, 0x49,
  97. 0xba, 0x3a, 0xf6, 0x76, 0x9b, 0x1b, 0xd7, 0x57,
  98. 0xa3, 0x23, 0xdd, 0x5d, 0x83, 0x03, 0xc1, 0x41,
  99. 0xb2, 0x32, 0xeb, 0x6b, 0x93, 0x13, 0xcf, 0x4f,
  100. 0xaf, 0x2f, 0xe7, 0x67, 0x8f, 0x0f, 0xcd, 0x4d,
  101. 0xbe, 0x3e, 0xfe, 0x7e, 0x9f, 0x1f, 0xdb, 0x5b,
  102. 0xa7, 0x27, 0xdf, 0x5f, 0x87, 0x07, 0xc5, 0x45,
  103. 0xb6, 0x36, 0xef, 0x6f, 0x97, 0x17, 0xd3, 0x53,
  104. 0xa9, 0x29, 0xe1, 0x61, 0x89, 0x09, 0xc7, 0x47,
  105. 0xb8, 0x38, 0xf2, 0x72, 0x99, 0x19, 0xd5, 0x55,
  106. 0xa1, 0x21, 0xdc, 0x5c, 0x81, 0x01, 0xbf, 0x3f,
  107. 0xb0, 0x30, 0xe9, 0x69, 0x91, 0x11, 0xce, 0x4e,
  108. 0xad, 0x2d, 0xe5, 0x65, 0x8d, 0x0d, 0xcb, 0x4b,
  109. 0xbc, 0x3c, 0xfa, 0x7a, 0x9d, 0x1d, 0xd9, 0x59,
  110. 0xa5, 0x25, 0xde, 0x5e, 0x85, 0x05, 0xc3, 0x43,
  111. 0xb4, 0x34, 0xed, 0x6d, 0x95, 0x15, 0xd1, 0x51,
  112. 0xac, 0x2c, 0xe4, 0x64, 0x8c, 0x0c, 0xca, 0x4a,
  113. 0xbb, 0x3b, 0xf8, 0x78, 0x9c, 0x1c, 0xd8, 0x58,
  114. 0xa4, 0x24, 0xde, 0x5e, 0x84, 0x04, 0xc2, 0x42,
  115. 0xb3, 0x33, 0xec, 0x6c, 0x94, 0x14, 0xd0, 0x50,
  116. 0xb0, 0x30, 0xe8, 0x68, 0x90, 0x10, 0xce, 0x4e,
  117. 0xbf, 0x3f, 0xfe, 0x7e, 0xa0, 0x20, 0xdc, 0x5c,
  118. 0xa8, 0x28, 0xe0, 0x60, 0x88, 0x08, 0xc6, 0x46,
  119. 0xb7, 0x37, 0xf0, 0x70, 0x98, 0x18, 0xd4, 0x54,
  120. 0xaa, 0x2a, 0xe2, 0x62, 0x8a, 0x0a, 0xc8, 0x48,
  121. 0xb9, 0x39, 0xf4, 0x74, 0x9a, 0x1a, 0xd6, 0x56,
  122. 0xa2, 0x22, 0xdd, 0x5d, 0x82, 0x02, 0xc0, 0x40,
  123. 0xb1, 0x31, 0xea, 0x6a, 0x92, 0x12, 0xcf, 0x4f,
  124. 0xae, 0x2e, 0xe6, 0x66, 0x8e, 0x0e, 0xcc, 0x4c,
  125. 0xbd, 0x3d, 0xfc, 0x7c, 0x9e, 0x1e, 0xda, 0x5a,
  126. 0xa6, 0x26, 0xdf, 0x5f, 0x86, 0x06, 0xc4, 0x44,
  127. 0xb5, 0x35, 0xee, 0x6e, 0x96, 0x16, 0xd2, 0x52
  128. };
  129. /* ulaw -> alaw */
  130. static char isdn_audio_ulaw_to_alaw[] =
  131. {
  132. 0xab, 0x55, 0xd5, 0x15, 0x95, 0x75, 0xf5, 0x35,
  133. 0xb5, 0x45, 0xc5, 0x05, 0x85, 0x65, 0xe5, 0x25,
  134. 0xa5, 0x5d, 0xdd, 0x1d, 0x9d, 0x7d, 0xfd, 0x3d,
  135. 0xbd, 0x4d, 0xcd, 0x0d, 0x8d, 0x6d, 0xed, 0x2d,
  136. 0xad, 0x51, 0xd1, 0x11, 0x91, 0x71, 0xf1, 0x31,
  137. 0xb1, 0x41, 0xc1, 0x01, 0x81, 0x61, 0xe1, 0x21,
  138. 0x59, 0xd9, 0x19, 0x99, 0x79, 0xf9, 0x39, 0xb9,
  139. 0x49, 0xc9, 0x09, 0x89, 0x69, 0xe9, 0x29, 0xa9,
  140. 0xd7, 0x17, 0x97, 0x77, 0xf7, 0x37, 0xb7, 0x47,
  141. 0xc7, 0x07, 0x87, 0x67, 0xe7, 0x27, 0xa7, 0xdf,
  142. 0x9f, 0x7f, 0xff, 0x3f, 0xbf, 0x4f, 0xcf, 0x0f,
  143. 0x8f, 0x6f, 0xef, 0x2f, 0x53, 0x13, 0x73, 0x33,
  144. 0xb3, 0x43, 0xc3, 0x03, 0x83, 0x63, 0xe3, 0x23,
  145. 0xa3, 0x5b, 0xdb, 0x1b, 0x9b, 0x7b, 0xfb, 0x3b,
  146. 0xbb, 0xbb, 0x4b, 0x4b, 0xcb, 0xcb, 0x0b, 0x0b,
  147. 0x8b, 0x8b, 0x6b, 0x6b, 0xeb, 0xeb, 0x2b, 0x2b,
  148. 0xab, 0x54, 0xd4, 0x14, 0x94, 0x74, 0xf4, 0x34,
  149. 0xb4, 0x44, 0xc4, 0x04, 0x84, 0x64, 0xe4, 0x24,
  150. 0xa4, 0x5c, 0xdc, 0x1c, 0x9c, 0x7c, 0xfc, 0x3c,
  151. 0xbc, 0x4c, 0xcc, 0x0c, 0x8c, 0x6c, 0xec, 0x2c,
  152. 0xac, 0x50, 0xd0, 0x10, 0x90, 0x70, 0xf0, 0x30,
  153. 0xb0, 0x40, 0xc0, 0x00, 0x80, 0x60, 0xe0, 0x20,
  154. 0x58, 0xd8, 0x18, 0x98, 0x78, 0xf8, 0x38, 0xb8,
  155. 0x48, 0xc8, 0x08, 0x88, 0x68, 0xe8, 0x28, 0xa8,
  156. 0xd6, 0x16, 0x96, 0x76, 0xf6, 0x36, 0xb6, 0x46,
  157. 0xc6, 0x06, 0x86, 0x66, 0xe6, 0x26, 0xa6, 0xde,
  158. 0x9e, 0x7e, 0xfe, 0x3e, 0xbe, 0x4e, 0xce, 0x0e,
  159. 0x8e, 0x6e, 0xee, 0x2e, 0x52, 0x12, 0x72, 0x32,
  160. 0xb2, 0x42, 0xc2, 0x02, 0x82, 0x62, 0xe2, 0x22,
  161. 0xa2, 0x5a, 0xda, 0x1a, 0x9a, 0x7a, 0xfa, 0x3a,
  162. 0xba, 0xba, 0x4a, 0x4a, 0xca, 0xca, 0x0a, 0x0a,
  163. 0x8a, 0x8a, 0x6a, 0x6a, 0xea, 0xea, 0x2a, 0x2a
  164. };
  165. #define NCOEFF 8 /* number of frequencies to be analyzed */
  166. #define DTMF_TRESH 4000 /* above this is dtmf */
  167. #define SILENCE_TRESH 200 /* below this is silence */
  168. #define AMP_BITS 9 /* bits per sample, reduced to avoid overflow */
  169. #define LOGRP 0
  170. #define HIGRP 1
  171. /* For DTMF recognition:
  172. * 2 * cos(2 * PI * k / N) precalculated for all k
  173. */
  174. static int cos2pik[NCOEFF] =
  175. {
  176. 55813, 53604, 51193, 48591, 38114, 33057, 25889, 18332
  177. };
  178. static char dtmf_matrix[4][4] =
  179. {
  180. {'1', '2', '3', 'A'},
  181. {'4', '5', '6', 'B'},
  182. {'7', '8', '9', 'C'},
  183. {'*', '0', '#', 'D'}
  184. };
  185. static inline void
  186. isdn_audio_tlookup(const u_char *table, u_char *buff, unsigned long n)
  187. {
  188. #ifdef __i386__
  189. unsigned long d0, d1, d2, d3;
  190. __asm__ __volatile__(
  191. "cld\n"
  192. "1:\tlodsb\n\t"
  193. "xlatb\n\t"
  194. "stosb\n\t"
  195. "loop 1b\n\t"
  196. : "=&b"(d0), "=&c"(d1), "=&D"(d2), "=&S"(d3)
  197. : "0"((long) table), "1"(n), "2"((long) buff), "3"((long) buff)
  198. : "memory", "ax");
  199. #else
  200. while (n--)
  201. *buff = table[*(unsigned char *)buff], buff++;
  202. #endif
  203. }
  204. void
  205. isdn_audio_ulaw2alaw(unsigned char *buff, unsigned long len)
  206. {
  207. isdn_audio_tlookup(isdn_audio_ulaw_to_alaw, buff, len);
  208. }
  209. void
  210. isdn_audio_alaw2ulaw(unsigned char *buff, unsigned long len)
  211. {
  212. isdn_audio_tlookup(isdn_audio_alaw_to_ulaw, buff, len);
  213. }
  214. /*
  215. * linear <-> adpcm conversion stuff
  216. * Most parts from the mgetty-package.
  217. * (C) by Gert Doering and Klaus Weidner
  218. * Used by permission of Gert Doering
  219. */
  220. #define ZEROTRAP /* turn on the trap as per the MIL-STD */
  221. #undef ZEROTRAP
  222. #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
  223. #define CLIP 32635
  224. static unsigned char
  225. isdn_audio_linear2ulaw(int sample)
  226. {
  227. static int exp_lut[256] =
  228. {
  229. 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  230. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  231. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  232. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  233. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  234. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  235. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  236. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  237. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  238. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  239. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  240. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  241. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  242. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  243. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  244. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
  245. };
  246. int sign,
  247. exponent,
  248. mantissa;
  249. unsigned char ulawbyte;
  250. /* Get the sample into sign-magnitude. */
  251. sign = (sample >> 8) & 0x80; /* set aside the sign */
  252. if (sign != 0)
  253. sample = -sample; /* get magnitude */
  254. if (sample > CLIP)
  255. sample = CLIP; /* clip the magnitude */
  256. /* Convert from 16 bit linear to ulaw. */
  257. sample = sample + BIAS;
  258. exponent = exp_lut[(sample >> 7) & 0xFF];
  259. mantissa = (sample >> (exponent + 3)) & 0x0F;
  260. ulawbyte = ~(sign | (exponent << 4) | mantissa);
  261. #ifdef ZEROTRAP
  262. /* optional CCITT trap */
  263. if (ulawbyte == 0)
  264. ulawbyte = 0x02;
  265. #endif
  266. return (ulawbyte);
  267. }
  268. static int Mx[3][8] =
  269. {
  270. {0x3800, 0x5600, 0, 0, 0, 0, 0, 0},
  271. {0x399a, 0x3a9f, 0x4d14, 0x6607, 0, 0, 0, 0},
  272. {0x3556, 0x3556, 0x399A, 0x3A9F, 0x4200, 0x4D14, 0x6607, 0x6607},
  273. };
  274. static int bitmask[9] =
  275. {
  276. 0, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff
  277. };
  278. static int
  279. isdn_audio_get_bits(adpcm_state *s, unsigned char **in, int *len)
  280. {
  281. while (s->nleft < s->nbits) {
  282. int d = *((*in)++);
  283. (*len)--;
  284. s->word = (s->word << 8) | d;
  285. s->nleft += 8;
  286. }
  287. s->nleft -= s->nbits;
  288. return (s->word >> s->nleft) & bitmask[s->nbits];
  289. }
  290. static void
  291. isdn_audio_put_bits(int data, int nbits, adpcm_state *s,
  292. unsigned char **out, int *len)
  293. {
  294. s->word = (s->word << nbits) | (data & bitmask[nbits]);
  295. s->nleft += nbits;
  296. while (s->nleft >= 8) {
  297. int d = (s->word >> (s->nleft - 8));
  298. *(out[0]++) = d & 255;
  299. (*len)++;
  300. s->nleft -= 8;
  301. }
  302. }
  303. adpcm_state *
  304. isdn_audio_adpcm_init(adpcm_state *s, int nbits)
  305. {
  306. if (!s)
  307. s = kmalloc(sizeof(adpcm_state), GFP_ATOMIC);
  308. if (s) {
  309. s->a = 0;
  310. s->d = 5;
  311. s->word = 0;
  312. s->nleft = 0;
  313. s->nbits = nbits;
  314. }
  315. return s;
  316. }
  317. dtmf_state *
  318. isdn_audio_dtmf_init(dtmf_state *s)
  319. {
  320. if (!s)
  321. s = kmalloc(sizeof(dtmf_state), GFP_ATOMIC);
  322. if (s) {
  323. s->idx = 0;
  324. s->last = ' ';
  325. }
  326. return s;
  327. }
  328. /*
  329. * Decompression of adpcm data to a/u-law
  330. *
  331. */
  332. int
  333. isdn_audio_adpcm2xlaw(adpcm_state *s, int fmt, unsigned char *in,
  334. unsigned char *out, int len)
  335. {
  336. int a = s->a;
  337. int d = s->d;
  338. int nbits = s->nbits;
  339. int olen = 0;
  340. while (len) {
  341. int e = isdn_audio_get_bits(s, &in, &len);
  342. int sign;
  343. if (nbits == 4 && e == 0)
  344. d = 4;
  345. sign = (e >> (nbits - 1)) ? -1 : 1;
  346. e &= bitmask[nbits - 1];
  347. a += sign * ((e << 1) + 1) * d >> 1;
  348. if (d & 1)
  349. a++;
  350. if (fmt)
  351. *out++ = isdn_audio_ulaw_to_alaw[
  352. isdn_audio_linear2ulaw(a << 2)];
  353. else
  354. *out++ = isdn_audio_linear2ulaw(a << 2);
  355. olen++;
  356. d = (d * Mx[nbits - 2][e] + 0x2000) >> 14;
  357. if (d < 5)
  358. d = 5;
  359. }
  360. s->a = a;
  361. s->d = d;
  362. return olen;
  363. }
  364. int
  365. isdn_audio_xlaw2adpcm(adpcm_state *s, int fmt, unsigned char *in,
  366. unsigned char *out, int len)
  367. {
  368. int a = s->a;
  369. int d = s->d;
  370. int nbits = s->nbits;
  371. int olen = 0;
  372. while (len--) {
  373. int e = 0,
  374. nmax = 1 << (nbits - 1);
  375. int sign,
  376. delta;
  377. if (fmt)
  378. delta = (isdn_audio_alaw_to_s16[*in++] >> 2) - a;
  379. else
  380. delta = (isdn_audio_ulaw_to_s16[*in++] >> 2) - a;
  381. if (delta < 0) {
  382. e = nmax;
  383. delta = -delta;
  384. }
  385. while (--nmax && delta > d) {
  386. delta -= d;
  387. e++;
  388. }
  389. if (nbits == 4 && ((e & 0x0f) == 0))
  390. e = 8;
  391. isdn_audio_put_bits(e, nbits, s, &out, &olen);
  392. sign = (e >> (nbits - 1)) ? -1 : 1;
  393. e &= bitmask[nbits - 1];
  394. a += sign * ((e << 1) + 1) * d >> 1;
  395. if (d & 1)
  396. a++;
  397. d = (d * Mx[nbits - 2][e] + 0x2000) >> 14;
  398. if (d < 5)
  399. d = 5;
  400. }
  401. s->a = a;
  402. s->d = d;
  403. return olen;
  404. }
  405. /*
  406. * Goertzel algorithm.
  407. * See http://ptolemy.eecs.berkeley.edu/papers/96/dtmf_ict/
  408. * for more info.
  409. * Result is stored into an sk_buff and queued up for later
  410. * evaluation.
  411. */
  412. static void
  413. isdn_audio_goertzel(int *sample, modem_info *info)
  414. {
  415. int sk,
  416. sk1,
  417. sk2;
  418. int k,
  419. n;
  420. struct sk_buff *skb;
  421. int *result;
  422. skb = dev_alloc_skb(sizeof(int) * NCOEFF);
  423. if (!skb) {
  424. printk(KERN_WARNING
  425. "isdn_audio: Could not alloc DTMF result for ttyI%d\n",
  426. info->line);
  427. return;
  428. }
  429. result = skb_put(skb, sizeof(int) * NCOEFF);
  430. for (k = 0; k < NCOEFF; k++) {
  431. sk = sk1 = sk2 = 0;
  432. for (n = 0; n < DTMF_NPOINTS; n++) {
  433. sk = sample[n] + ((cos2pik[k] * sk1) >> 15) - sk2;
  434. sk2 = sk1;
  435. sk1 = sk;
  436. }
  437. /* Avoid overflows */
  438. sk >>= 1;
  439. sk2 >>= 1;
  440. /* compute |X(k)|**2 */
  441. /* report overflows. This should not happen. */
  442. /* Comment this out if desired */
  443. if (sk < -32768 || sk > 32767)
  444. printk(KERN_DEBUG
  445. "isdn_audio: dtmf goertzel overflow, sk=%d\n", sk);
  446. if (sk2 < -32768 || sk2 > 32767)
  447. printk(KERN_DEBUG
  448. "isdn_audio: dtmf goertzel overflow, sk2=%d\n", sk2);
  449. result[k] =
  450. ((sk * sk) >> AMP_BITS) -
  451. ((((cos2pik[k] * sk) >> 15) * sk2) >> AMP_BITS) +
  452. ((sk2 * sk2) >> AMP_BITS);
  453. }
  454. skb_queue_tail(&info->dtmf_queue, skb);
  455. isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
  456. }
  457. void
  458. isdn_audio_eval_dtmf(modem_info *info)
  459. {
  460. struct sk_buff *skb;
  461. int *result;
  462. dtmf_state *s;
  463. int silence;
  464. int i;
  465. int di;
  466. int ch;
  467. int grp[2];
  468. char what;
  469. char *p;
  470. int thresh;
  471. while ((skb = skb_dequeue(&info->dtmf_queue))) {
  472. result = (int *) skb->data;
  473. s = info->dtmf_state;
  474. grp[LOGRP] = grp[HIGRP] = -1;
  475. silence = 0;
  476. thresh = 0;
  477. for (i = 0; i < NCOEFF; i++) {
  478. if (result[i] > DTMF_TRESH) {
  479. if (result[i] > thresh)
  480. thresh = result[i];
  481. }
  482. else if (result[i] < SILENCE_TRESH)
  483. silence++;
  484. }
  485. if (silence == NCOEFF)
  486. what = ' ';
  487. else {
  488. if (thresh > 0) {
  489. thresh = thresh >> 4; /* touchtones must match within 12 dB */
  490. for (i = 0; i < NCOEFF; i++) {
  491. if (result[i] < thresh)
  492. continue; /* ignore */
  493. /* good level found. This is allowed only one time per group */
  494. if (i < NCOEFF / 2) {
  495. /* lowgroup*/
  496. if (grp[LOGRP] >= 0) {
  497. // Bad. Another tone found. */
  498. grp[LOGRP] = -1;
  499. break;
  500. }
  501. else
  502. grp[LOGRP] = i;
  503. }
  504. else { /* higroup */
  505. if (grp[HIGRP] >= 0) { // Bad. Another tone found. */
  506. grp[HIGRP] = -1;
  507. break;
  508. }
  509. else
  510. grp[HIGRP] = i - NCOEFF/2;
  511. }
  512. }
  513. if ((grp[LOGRP] >= 0) && (grp[HIGRP] >= 0)) {
  514. what = dtmf_matrix[grp[LOGRP]][grp[HIGRP]];
  515. if (s->last != ' ' && s->last != '.')
  516. s->last = what; /* min. 1 non-DTMF between DTMF */
  517. } else
  518. what = '.';
  519. }
  520. else
  521. what = '.';
  522. }
  523. if ((what != s->last) && (what != ' ') && (what != '.')) {
  524. printk(KERN_DEBUG "dtmf: tt='%c'\n", what);
  525. p = skb->data;
  526. *p++ = 0x10;
  527. *p = what;
  528. skb_trim(skb, 2);
  529. ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
  530. ISDN_AUDIO_SKB_LOCK(skb) = 0;
  531. di = info->isdn_driver;
  532. ch = info->isdn_channel;
  533. __skb_queue_tail(&dev->drv[di]->rpqueue[ch], skb);
  534. dev->drv[di]->rcvcount[ch] += 2;
  535. /* Schedule dequeuing */
  536. if ((dev->modempoll) && (info->rcvsched))
  537. isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
  538. wake_up_interruptible(&dev->drv[di]->rcv_waitq[ch]);
  539. } else
  540. kfree_skb(skb);
  541. s->last = what;
  542. }
  543. }
  544. /*
  545. * Decode DTMF tones, queue result in separate sk_buf for
  546. * later examination.
  547. * Parameters:
  548. * s = pointer to state-struct.
  549. * buf = input audio data
  550. * len = size of audio data.
  551. * fmt = audio data format (0 = ulaw, 1 = alaw)
  552. */
  553. void
  554. isdn_audio_calc_dtmf(modem_info *info, unsigned char *buf, int len, int fmt)
  555. {
  556. dtmf_state *s = info->dtmf_state;
  557. int i;
  558. int c;
  559. while (len) {
  560. c = DTMF_NPOINTS - s->idx;
  561. if (c > len)
  562. c = len;
  563. if (c <= 0)
  564. break;
  565. for (i = 0; i < c; i++) {
  566. if (fmt)
  567. s->buf[s->idx++] =
  568. isdn_audio_alaw_to_s16[*buf++] >> (15 - AMP_BITS);
  569. else
  570. s->buf[s->idx++] =
  571. isdn_audio_ulaw_to_s16[*buf++] >> (15 - AMP_BITS);
  572. }
  573. if (s->idx == DTMF_NPOINTS) {
  574. isdn_audio_goertzel(s->buf, info);
  575. s->idx = 0;
  576. }
  577. len -= c;
  578. }
  579. }
  580. silence_state *
  581. isdn_audio_silence_init(silence_state *s)
  582. {
  583. if (!s)
  584. s = kmalloc(sizeof(silence_state), GFP_ATOMIC);
  585. if (s) {
  586. s->idx = 0;
  587. s->state = 0;
  588. }
  589. return s;
  590. }
  591. void
  592. isdn_audio_calc_silence(modem_info *info, unsigned char *buf, int len, int fmt)
  593. {
  594. silence_state *s = info->silence_state;
  595. int i;
  596. signed char c;
  597. if (!info->emu.vpar[1]) return;
  598. for (i = 0; i < len; i++) {
  599. if (fmt)
  600. c = isdn_audio_alaw_to_ulaw[*buf++];
  601. else
  602. c = *buf++;
  603. if (c > 0) c -= 128;
  604. c = abs(c);
  605. if (c > (info->emu.vpar[1] * 4)) {
  606. s->idx = 0;
  607. s->state = 1;
  608. } else {
  609. if (s->idx < 210000) s->idx++;
  610. }
  611. }
  612. }
  613. void
  614. isdn_audio_put_dle_code(modem_info *info, u_char code)
  615. {
  616. struct sk_buff *skb;
  617. int di;
  618. int ch;
  619. char *p;
  620. skb = dev_alloc_skb(2);
  621. if (!skb) {
  622. printk(KERN_WARNING
  623. "isdn_audio: Could not alloc skb for ttyI%d\n",
  624. info->line);
  625. return;
  626. }
  627. p = skb_put(skb, 2);
  628. p[0] = 0x10;
  629. p[1] = code;
  630. ISDN_AUDIO_SKB_DLECOUNT(skb) = 0;
  631. ISDN_AUDIO_SKB_LOCK(skb) = 0;
  632. di = info->isdn_driver;
  633. ch = info->isdn_channel;
  634. __skb_queue_tail(&dev->drv[di]->rpqueue[ch], skb);
  635. dev->drv[di]->rcvcount[ch] += 2;
  636. /* Schedule dequeuing */
  637. if ((dev->modempoll) && (info->rcvsched))
  638. isdn_timer_ctrl(ISDN_TIMER_MODEMREAD, 1);
  639. wake_up_interruptible(&dev->drv[di]->rcv_waitq[ch]);
  640. }
  641. void
  642. isdn_audio_eval_silence(modem_info *info)
  643. {
  644. silence_state *s = info->silence_state;
  645. char what;
  646. what = ' ';
  647. if (s->idx > (info->emu.vpar[2] * 800)) {
  648. s->idx = 0;
  649. if (!s->state) { /* silence from beginning of rec */
  650. what = 's';
  651. } else {
  652. what = 'q';
  653. }
  654. }
  655. if ((what == 's') || (what == 'q')) {
  656. printk(KERN_DEBUG "ttyI%d: %s\n", info->line,
  657. (what == 's') ? "silence" : "quiet");
  658. isdn_audio_put_dle_code(info, what);
  659. }
  660. }