Debian Codegraph @

drivers/dahdi/dahdi_echocan_sec.c

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