Debian Codegraph @

drivers/dahdi/dahdi_echocan_sec2.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 *
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 /*
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
32 /* TODO:
33 Finish the echo suppressor option, however nasty suppression may be
34 Add an option to reintroduce side tone at -24dB under appropriate conditions.
35 Improve double talk detector (iterative!)
36 */
37
38 #include <linux/kernel.h>
39 #include <linux/slab.h>
40 #include <linux/errno.h>
41 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/ctype.h>
44 #include <linux/moduleparam.h>
45
46 #include <dahdi/kernel.h>
47
48 static int debug;
49
50 #include "fir.h"
51
52 #ifndef NULL
53 #define NULL 0
54 #endif
55 #ifndef FALSE
56 #define FALSE 0
57 #endif
58 #ifndef TRUE
59 #define TRUE (!FALSE)
60 #endif
61
62 #define NONUPDATE_DWELL_TIME 600 /* 600 samples, or 75ms */
63
64 /*
65 * According to Jim...
66 */
67 #define MIN_TX_POWER_FOR_ADAPTION 512
68 #define MIN_RX_POWER_FOR_ADAPTION 64
69
70 /*
71 * According to Steve...
72 */
73 /* #define MIN_TX_POWER_FOR_ADAPTION 4096
74 #define MIN_RX_POWER_FOR_ADAPTION 64 */
75
76 static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp,
77 struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec);
78 static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec);
79 static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, u32 size);
80 static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val);
81 static const char *name = "SEC2";
82 static const char *ec_name(const struct dahdi_chan *chan) { return name; }
83
84 static const struct dahdi_echocan_factory my_factory = {
85 .get_name = ec_name,
86 .owner = THIS_MODULE,
87 .echocan_create = echo_can_create,
88 };
89
90 static const struct dahdi_echocan_ops my_ops = {
91 .echocan_free = echo_can_free,
92 .echocan_process = echo_can_process,
93 .echocan_traintap = echo_can_traintap,
94 };
95
96 struct ec_pvt {
97 struct dahdi_echocan_state dahdi;
98 int tx_power;
99 int rx_power;
100 int clean_rx_power;
101
102 int rx_power_threshold;
103 int nonupdate_dwell;
104
105 fir16_state_t fir_state;
106 int16_t *fir_taps16; /* 16-bit version of FIR taps */
107 int32_t *fir_taps32; /* 32-bit version of FIR taps */
108
109 int curr_pos;
110
111 int taps;
112 int tap_mask;
113 int use_nlp;
114 int use_suppressor;
115
116 int32_t supp_test1;
117 int32_t supp_test2;
118 int32_t supp1;
119 int32_t supp2;
120
121 int32_t latest_correction; /* Indication of the magnitude of the latest
122 adaption, or a code to indicate why adaption
123 was skipped, for test purposes */
124 };
125
126 #define dahdi_to_pvt(a) container_of(a, struct ec_pvt, dahdi)
127
128 static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp,
129 struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec)
130 {
131 struct ec_pvt *pvt;
132 size_t size;
133
134 if (ecp->param_count > 0) {
135 printk(KERN_WARNING "SEC2 does not support parameters; failing request\n");
136 return -EINVAL;
137 }
138
139 size = sizeof(*pvt) + ecp->tap_length * sizeof(int32_t) + ecp->tap_length * 3 * sizeof(int16_t);
140
141 pvt = kzalloc(size, GFP_KERNEL);
142 if (!pvt)
143 return -ENOMEM;
144
145 pvt->dahdi.ops = &my_ops;
146
147 if (ecp->param_count > 0) {
148 printk(KERN_WARNING "SEC-2 echo canceler does not support parameters; failing request\n");
149 return -EINVAL;
150 }
151
152 pvt->taps = ecp->tap_length;
153 pvt->curr_pos = ecp->tap_length - 1;
154 pvt->tap_mask = ecp->tap_length - 1;
155 pvt->fir_taps32 = (int32_t *) (pvt + sizeof(*pvt));
156 pvt->fir_taps16 = (int16_t *) (pvt + sizeof(*pvt) + ecp->tap_length * sizeof(int32_t));
157 /* Create FIR filter */
158 fir16_create(&pvt->fir_state, pvt->fir_taps16, pvt->taps);
159 pvt->rx_power_threshold = 10000000;
160 pvt->use_suppressor = FALSE;
161 /* Non-linear processor - a fancy way to say "zap small signals, to avoid
162 accumulating noise". */
163 pvt->use_nlp = FALSE;
164
165 *ec = &pvt->dahdi;
166 return 0;
167 }
168
169 static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec)
170 {
171 struct ec_pvt *pvt = dahdi_to_pvt(ec);
172
173 fir16_free(&pvt->fir_state);
174 kfree(pvt);
175 }
176
177 static inline int16_t sample_update(struct ec_pvt *pvt, int16_t tx, int16_t rx)
178 {
179 int offset1;
180 int offset2;
181 int32_t echo_value;
182 int clean_rx;
183 int nsuppr;
184 int i;
185 int correction;
186
187 /* Evaluate the echo - i.e. apply the FIR filter */
188 /* Assume the gain of the FIR does not exceed unity. Exceeding unity
189 would seem like a rather poor thing for an echo cancellor to do :)
190 This means we can compute the result with a total disregard for
191 overflows. 16bits x 16bits -> 31bits, so no overflow can occur in
192 any multiply. While accumulating we may overflow and underflow the
193 32 bit scale often. However, if the gain does not exceed unity,
194 everything should work itself out, and the final result will be
195 OK, without any saturation logic. */
196 /* Overflow is very much possible here, and we do nothing about it because
197 of the compute costs */
198 /* 16 bit coeffs for the LMS give lousy results (maths good, actual sound
199 bad!), but 32 bit coeffs require some shifting. On balance 32 bit seems
200 best */
201 echo_value = fir16 (&pvt->fir_state, tx);
202
203 /* And the answer is..... */
204 clean_rx = rx - echo_value;
205
206 /* That was the easy part. Now we need to adapt! */
207 if (pvt->nonupdate_dwell > 0)
208 pvt->nonupdate_dwell--;
209
210 /* If there is very little being transmitted, any attempt to train is
211 futile. We would either be training on the far end's noise or signal,
212 the channel's own noise, or our noise. Either way, this is hardly good
213 training, so don't do it (avoid trouble). */
214 /* If the received power is very low, either we are sending very little or
215 we are already well adapted. There is little point in trying to improve
216 the adaption under these circumstanceson, so don't do it (reduce the
217 compute load). */
218 if (pvt->tx_power > MIN_TX_POWER_FOR_ADAPTION && pvt->rx_power > MIN_RX_POWER_FOR_ADAPTION) {
219 /* This is a really crude piece of decision logic, but it does OK
220 for now. */
221 if (pvt->tx_power > 2*pvt->rx_power) {
222 /* There is no far-end speech detected */
223 if (pvt->nonupdate_dwell == 0) {
224 /* ... and we are not in the dwell time from previous speech. */
225 /* nsuppr = saturate((clean_rx << 16)/pvt->tx_power); */
226 nsuppr = clean_rx >> 3;
227
228 /* Update the FIR taps */
229 offset2 = pvt->curr_pos + 1;
230 offset1 = pvt->taps - offset2;
231 pvt->latest_correction = 0;
232 for (i = pvt->taps - 1; i >= offset1; i--) {
233 correction = pvt->fir_state.history[i - offset1]*nsuppr;
234 /* Leak to avoid false training on signals with multiple
235 strong correlations. */
236 pvt->fir_taps32[i] -= (pvt->fir_taps32[i] >> 12);
237 pvt->fir_taps32[i] += correction;
238 pvt->fir_state.coeffs[i] = pvt->fir_taps32[i] >> 15;
239 pvt->latest_correction += abs(correction);
240 }
241 for ( ; i >= 0; i--) {
242 correction = pvt->fir_state.history[i + offset2]*nsuppr;
243 /* Leak to avoid false training on signals with multiple
244 strong correlations. */
245 pvt->fir_taps32[i] -= (pvt->fir_taps32[i] >> 12);
246 pvt->fir_taps32[i] += correction;
247 pvt->fir_state.coeffs[i] = pvt->fir_taps32[i] >> 15;
248 pvt->latest_correction += abs(correction);
249 }
250 } else {
251 pvt->latest_correction = -1;
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 = -3;
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->fir_state.history[pvt->curr_pos] - pvt->fir_state.history[(pvt->curr_pos - 7) & pvt->tap_mask]);
271 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]);
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 if (pvt->curr_pos <= 0)
288 pvt->curr_pos = pvt->taps;
289 pvt->curr_pos--;
290
291 return clean_rx;
292 }
293
294 static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, u32 size)
295 {
296 struct ec_pvt *pvt = dahdi_to_pvt(ec);
297 u32 x;
298 short result;
299
300 for (x = 0; x < size; x++) {
301 result = sample_update(pvt, *iref, *isig);
302 *isig++ = result;
303 ++iref;
304 }
305 }
306
307 static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val)
308 {
309 struct ec_pvt *pvt = dahdi_to_pvt(ec);
310
311 /* Reset hang counter to avoid adjustments after
312 initial forced training */
313 pvt->nonupdate_dwell = pvt->taps << 1;
314 if (pos >= pvt->taps)
315 return 1;
316 pvt->fir_taps32[pos] = val << 17;
317 pvt->fir_taps16[pos] = val << 1;
318 if (++pos >= pvt->taps)
319 return 1;
320 else
321 return 0;
322 }
323
324 static int __init mod_init(void)
325 {
326 if (dahdi_register_echocan_factory(&my_factory)) {
327 module_printk(KERN_ERR, "could not register with DAHDI core\n");
328
329 return -EPERM;
330 }
331
332 module_printk(KERN_NOTICE, "Registered echo canceler '%s'\n",
333 my_factory.get_name(NULL));
334
335 return 0;
336 }
337
338 static void __exit mod_exit(void)
339 {
340 dahdi_unregister_echocan_factory(&my_factory);
341 }
342
343 module_param(debug, int, S_IRUGO | S_IWUSR);
344
345 MODULE_DESCRIPTION("DAHDI 'SEC2' Echo Canceler");
346 MODULE_AUTHOR("Steve Underwood <[email protected]>");
347 MODULE_LICENSE("GPL");
348
349 module_init(mod_init);
350 module_exit(mod_exit);