dahdi_echocan_sec.c 10 KB

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