dahdi_echocan_sec2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * echo.c - An echo cancellor, suitable for electrical and acoustic
  5. * cancellation. This code does not currently comply with
  6. * any relevant standards (e.g. G.164/5/7/8). One day....
  7. *
  8. * Written by Steve Underwood <steveu@coppice.org>
  9. *
  10. * Copyright (C) 2001 Steve Underwood
  11. *
  12. * Based on a bit from here, a bit from there, eye of toad,
  13. * ear of bat, etc - plus, of course, my own 2 cents.
  14. *
  15. * All rights reserved.
  16. *
  17. */
  18. /*
  19. * See http://www.asterisk.org for more information about
  20. * the Asterisk project. Please do not directly contact
  21. * any of the maintainers of this project for assistance;
  22. * the project provides a web site, mailing lists and IRC
  23. * channels for your use.
  24. *
  25. * This program is free software, distributed under the terms of
  26. * the GNU General Public License Version 2 as published by the
  27. * Free Software Foundation. See the LICENSE file included with
  28. * this program for more details.
  29. */
  30. /* TODO:
  31. Finish the echo suppressor option, however nasty suppression may be
  32. Add an option to reintroduce side tone at -24dB under appropriate conditions.
  33. Improve double talk detector (iterative!)
  34. */
  35. #include <linux/kernel.h>
  36. #include <linux/slab.h>
  37. #include <linux/errno.h>
  38. #include <linux/module.h>
  39. #include <linux/init.h>
  40. #include <linux/ctype.h>
  41. #include <linux/moduleparam.h>
  42. #include <dahdi/kernel.h>
  43. static int debug;
  44. #include "fir.h"
  45. #ifndef NULL
  46. #define NULL 0
  47. #endif
  48. #ifndef FALSE
  49. #define FALSE 0
  50. #endif
  51. #ifndef TRUE
  52. #define TRUE (!FALSE)
  53. #endif
  54. #define NONUPDATE_DWELL_TIME 600 /* 600 samples, or 75ms */
  55. /*
  56. * According to Jim...
  57. */
  58. #define MIN_TX_POWER_FOR_ADAPTION 512
  59. #define MIN_RX_POWER_FOR_ADAPTION 64
  60. /*
  61. * According to Steve...
  62. */
  63. /* #define MIN_TX_POWER_FOR_ADAPTION 4096
  64. #define MIN_RX_POWER_FOR_ADAPTION 64 */
  65. static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp,
  66. struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec);
  67. static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec);
  68. static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, u32 size);
  69. static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val);
  70. static const char *name = "SEC2";
  71. static const char *ec_name(const struct dahdi_chan *chan) { return name; }
  72. static const struct dahdi_echocan_factory my_factory = {
  73. .get_name = ec_name,
  74. .owner = THIS_MODULE,
  75. .echocan_create = echo_can_create,
  76. };
  77. static const struct dahdi_echocan_ops my_ops = {
  78. .echocan_free = echo_can_free,
  79. .echocan_process = echo_can_process,
  80. .echocan_traintap = echo_can_traintap,
  81. };
  82. struct ec_pvt {
  83. struct dahdi_echocan_state dahdi;
  84. int tx_power;
  85. int rx_power;
  86. int clean_rx_power;
  87. int rx_power_threshold;
  88. int nonupdate_dwell;
  89. fir16_state_t fir_state;
  90. int16_t *fir_taps16; /* 16-bit version of FIR taps */
  91. int32_t *fir_taps32; /* 32-bit version of FIR taps */
  92. int curr_pos;
  93. int taps;
  94. int tap_mask;
  95. int use_nlp;
  96. int use_suppressor;
  97. int32_t supp_test1;
  98. int32_t supp_test2;
  99. int32_t supp1;
  100. int32_t supp2;
  101. int32_t latest_correction; /* Indication of the magnitude of the latest
  102. adaption, or a code to indicate why adaption
  103. was skipped, for test purposes */
  104. };
  105. #define dahdi_to_pvt(a) container_of(a, struct ec_pvt, dahdi)
  106. static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp,
  107. struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec)
  108. {
  109. struct ec_pvt *pvt;
  110. size_t size;
  111. if (ecp->param_count > 0) {
  112. printk(KERN_WARNING "SEC2 does not support parameters; failing request\n");
  113. return -EINVAL;
  114. }
  115. size = sizeof(*pvt) + ecp->tap_length * sizeof(int32_t) + ecp->tap_length * 3 * sizeof(int16_t);
  116. pvt = kzalloc(size, GFP_KERNEL);
  117. if (!pvt)
  118. return -ENOMEM;
  119. pvt->dahdi.ops = &my_ops;
  120. if (ecp->param_count > 0) {
  121. printk(KERN_WARNING "SEC-2 echo canceler does not support parameters; failing request\n");
  122. return -EINVAL;
  123. }
  124. pvt->taps = ecp->tap_length;
  125. pvt->curr_pos = ecp->tap_length - 1;
  126. pvt->tap_mask = ecp->tap_length - 1;
  127. pvt->fir_taps32 = (int32_t *) (pvt + sizeof(*pvt));
  128. pvt->fir_taps16 = (int16_t *) (pvt + sizeof(*pvt) + ecp->tap_length * sizeof(int32_t));
  129. /* Create FIR filter */
  130. fir16_create(&pvt->fir_state, pvt->fir_taps16, pvt->taps);
  131. pvt->rx_power_threshold = 10000000;
  132. pvt->use_suppressor = FALSE;
  133. /* Non-linear processor - a fancy way to say "zap small signals, to avoid
  134. accumulating noise". */
  135. pvt->use_nlp = FALSE;
  136. *ec = &pvt->dahdi;
  137. return 0;
  138. }
  139. static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec)
  140. {
  141. struct ec_pvt *pvt = dahdi_to_pvt(ec);
  142. fir16_free(&pvt->fir_state);
  143. kfree(pvt);
  144. }
  145. static inline int16_t sample_update(struct ec_pvt *pvt, int16_t tx, int16_t rx)
  146. {
  147. int offset1;
  148. int offset2;
  149. int32_t echo_value;
  150. int clean_rx;
  151. int nsuppr;
  152. int i;
  153. int correction;
  154. /* Evaluate the echo - i.e. apply the FIR filter */
  155. /* Assume the gain of the FIR does not exceed unity. Exceeding unity
  156. would seem like a rather poor thing for an echo cancellor to do :)
  157. This means we can compute the result with a total disregard for
  158. overflows. 16bits x 16bits -> 31bits, so no overflow can occur in
  159. any multiply. While accumulating we may overflow and underflow the
  160. 32 bit scale often. However, if the gain does not exceed unity,
  161. everything should work itself out, and the final result will be
  162. OK, without any saturation logic. */
  163. /* Overflow is very much possible here, and we do nothing about it because
  164. of the compute costs */
  165. /* 16 bit coeffs for the LMS give lousy results (maths good, actual sound
  166. bad!), but 32 bit coeffs require some shifting. On balance 32 bit seems
  167. best */
  168. echo_value = fir16 (&pvt->fir_state, tx);
  169. /* And the answer is..... */
  170. clean_rx = rx - echo_value;
  171. /* That was the easy part. Now we need to adapt! */
  172. if (pvt->nonupdate_dwell > 0)
  173. pvt->nonupdate_dwell--;
  174. /* If there is very little being transmitted, any attempt to train is
  175. futile. We would either be training on the far end's noise or signal,
  176. the channel's own noise, or our noise. Either way, this is hardly good
  177. training, so don't do it (avoid trouble). */
  178. /* If the received power is very low, either we are sending very little or
  179. we are already well adapted. There is little point in trying to improve
  180. the adaption under these circumstanceson, so don't do it (reduce the
  181. compute load). */
  182. if (pvt->tx_power > MIN_TX_POWER_FOR_ADAPTION && pvt->rx_power > MIN_RX_POWER_FOR_ADAPTION) {
  183. /* This is a really crude piece of decision logic, but it does OK
  184. for now. */
  185. if (pvt->tx_power > 2*pvt->rx_power) {
  186. /* There is no far-end speech detected */
  187. if (pvt->nonupdate_dwell == 0) {
  188. /* ... and we are not in the dwell time from previous speech. */
  189. /* nsuppr = saturate((clean_rx << 16)/pvt->tx_power); */
  190. nsuppr = clean_rx >> 3;
  191. /* Update the FIR taps */
  192. offset2 = pvt->curr_pos + 1;
  193. offset1 = pvt->taps - offset2;
  194. pvt->latest_correction = 0;
  195. for (i = pvt->taps - 1; i >= offset1; i--) {
  196. correction = pvt->fir_state.history[i - offset1]*nsuppr;
  197. /* Leak to avoid false training on signals with multiple
  198. strong correlations. */
  199. pvt->fir_taps32[i] -= (pvt->fir_taps32[i] >> 12);
  200. pvt->fir_taps32[i] += correction;
  201. pvt->fir_state.coeffs[i] = pvt->fir_taps32[i] >> 15;
  202. pvt->latest_correction += abs(correction);
  203. }
  204. for ( ; i >= 0; i--) {
  205. correction = pvt->fir_state.history[i + offset2]*nsuppr;
  206. /* Leak to avoid false training on signals with multiple
  207. strong correlations. */
  208. pvt->fir_taps32[i] -= (pvt->fir_taps32[i] >> 12);
  209. pvt->fir_taps32[i] += correction;
  210. pvt->fir_state.coeffs[i] = pvt->fir_taps32[i] >> 15;
  211. pvt->latest_correction += abs(correction);
  212. }
  213. } else {
  214. pvt->latest_correction = -1;
  215. }
  216. } else {
  217. pvt->nonupdate_dwell = NONUPDATE_DWELL_TIME;
  218. pvt->latest_correction = -2;
  219. }
  220. } else {
  221. pvt->nonupdate_dwell = 0;
  222. pvt->latest_correction = -3;
  223. }
  224. /* Calculate short term power levels using very simple single pole IIRs */
  225. /* TODO: Is the nasty modulus approach the fastest, or would a real
  226. tx*tx power calculation actually be faster? */
  227. pvt->tx_power += ((abs(tx) - pvt->tx_power) >> 5);
  228. pvt->rx_power += ((abs(rx) - pvt->rx_power) >> 5);
  229. pvt->clean_rx_power += ((abs(clean_rx) - pvt->clean_rx_power) >> 5);
  230. #if defined(XYZZY)
  231. if (pvt->use_suppressor) {
  232. pvt->supp_test1 += (pvt->fir_state.history[pvt->curr_pos] - pvt->fir_state.history[(pvt->curr_pos - 7) & pvt->tap_mask]);
  233. pvt->supp_test2 += (pvt->fir_state.history[(pvt->curr_pos - 24) & pvt->tap_mask] - pvt->fir_state.history[(pvt->curr_pos - 31) & pvt->tap_mask]);
  234. if (pvt->supp_test1 > 42 && pvt->supp_test2 > 42)
  235. supp_change = 25;
  236. else
  237. supp_change = 50;
  238. supp = supp_change + k1*pvt->supp1 + k2*pvt->supp2;
  239. pvt->supp2 = pvt->supp1;
  240. pvt->supp1 = supp;
  241. clean_rx *= (1 - supp);
  242. }
  243. #endif
  244. if (pvt->use_nlp && pvt->rx_power < 32)
  245. clean_rx = 0;
  246. /* Roll around the rolling buffer */
  247. if (pvt->curr_pos <= 0)
  248. pvt->curr_pos = pvt->taps;
  249. pvt->curr_pos--;
  250. return clean_rx;
  251. }
  252. static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, u32 size)
  253. {
  254. struct ec_pvt *pvt = dahdi_to_pvt(ec);
  255. u32 x;
  256. short result;
  257. for (x = 0; x < size; x++) {
  258. result = sample_update(pvt, *iref, *isig);
  259. *isig++ = result;
  260. ++iref;
  261. }
  262. }
  263. static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val)
  264. {
  265. struct ec_pvt *pvt = dahdi_to_pvt(ec);
  266. /* Reset hang counter to avoid adjustments after
  267. initial forced training */
  268. pvt->nonupdate_dwell = pvt->taps << 1;
  269. if (pos >= pvt->taps)
  270. return 1;
  271. pvt->fir_taps32[pos] = val << 17;
  272. pvt->fir_taps16[pos] = val << 1;
  273. if (++pos >= pvt->taps)
  274. return 1;
  275. else
  276. return 0;
  277. }
  278. static int __init mod_init(void)
  279. {
  280. if (dahdi_register_echocan_factory(&my_factory)) {
  281. module_printk(KERN_ERR, "could not register with DAHDI core\n");
  282. return -EPERM;
  283. }
  284. module_printk(KERN_NOTICE, "Registered echo canceler '%s'\n",
  285. my_factory.get_name(NULL));
  286. return 0;
  287. }
  288. static void __exit mod_exit(void)
  289. {
  290. dahdi_unregister_echocan_factory(&my_factory);
  291. }
  292. module_param(debug, int, S_IRUGO | S_IWUSR);
  293. MODULE_DESCRIPTION("DAHDI 'SEC2' Echo Canceler");
  294. MODULE_AUTHOR("Steve Underwood <steveu@coppice.org>");
  295. MODULE_LICENSE("GPL");
  296. module_init(mod_init);
  297. module_exit(mod_exit);