18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * SpanDSP - a series of DSP components for telephony
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * echo.c - A line echo canceller.  This code is being developed
68c2ecf20Sopenharmony_ci *          against and partially complies with G168.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Written by Steve Underwood <steveu@coppice.org>
98c2ecf20Sopenharmony_ci *         and David Rowe <david_at_rowetel_dot_com>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Copyright (C) 2001, 2003 Steve Underwood, 2007 David Rowe
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Based on a bit from here, a bit from there, eye of toad, ear of
148c2ecf20Sopenharmony_ci * bat, 15 years of failed attempts by David and a few fried brain
158c2ecf20Sopenharmony_ci * cells.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * All rights reserved.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*! \file */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* Implementation Notes
238c2ecf20Sopenharmony_ci   David Rowe
248c2ecf20Sopenharmony_ci   April 2007
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci   This code started life as Steve's NLMS algorithm with a tap
278c2ecf20Sopenharmony_ci   rotation algorithm to handle divergence during double talk.  I
288c2ecf20Sopenharmony_ci   added a Geigel Double Talk Detector (DTD) [2] and performed some
298c2ecf20Sopenharmony_ci   G168 tests.  However I had trouble meeting the G168 requirements,
308c2ecf20Sopenharmony_ci   especially for double talk - there were always cases where my DTD
318c2ecf20Sopenharmony_ci   failed, for example where near end speech was under the 6dB
328c2ecf20Sopenharmony_ci   threshold required for declaring double talk.
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci   So I tried a two path algorithm [1], which has so far given better
358c2ecf20Sopenharmony_ci   results.  The original tap rotation/Geigel algorithm is available
368c2ecf20Sopenharmony_ci   in SVN http://svn.rowetel.com/software/oslec/tags/before_16bit.
378c2ecf20Sopenharmony_ci   It's probably possible to make it work if some one wants to put some
388c2ecf20Sopenharmony_ci   serious work into it.
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci   At present no special treatment is provided for tones, which
418c2ecf20Sopenharmony_ci   generally cause NLMS algorithms to diverge.  Initial runs of a
428c2ecf20Sopenharmony_ci   subset of the G168 tests for tones (e.g ./echo_test 6) show the
438c2ecf20Sopenharmony_ci   current algorithm is passing OK, which is kind of surprising.  The
448c2ecf20Sopenharmony_ci   full set of tests needs to be performed to confirm this result.
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci   One other interesting change is that I have managed to get the NLMS
478c2ecf20Sopenharmony_ci   code to work with 16 bit coefficients, rather than the original 32
488c2ecf20Sopenharmony_ci   bit coefficents.  This reduces the MIPs and storage required.
498c2ecf20Sopenharmony_ci   I evaulated the 16 bit port using g168_tests.sh and listening tests
508c2ecf20Sopenharmony_ci   on 4 real-world samples.
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci   I also attempted the implementation of a block based NLMS update
538c2ecf20Sopenharmony_ci   [2] but although this passes g168_tests.sh it didn't converge well
548c2ecf20Sopenharmony_ci   on the real-world samples.  I have no idea why, perhaps a scaling
558c2ecf20Sopenharmony_ci   problem.  The block based code is also available in SVN
568c2ecf20Sopenharmony_ci   http://svn.rowetel.com/software/oslec/tags/before_16bit.  If this
578c2ecf20Sopenharmony_ci   code can be debugged, it will lead to further reduction in MIPS, as
588c2ecf20Sopenharmony_ci   the block update code maps nicely onto DSP instruction sets (it's a
598c2ecf20Sopenharmony_ci   dot product) compared to the current sample-by-sample update.
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci   Steve also has some nice notes on echo cancellers in echo.h
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci   References:
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci   [1] Ochiai, Areseki, and Ogihara, "Echo Canceller with Two Echo
668c2ecf20Sopenharmony_ci       Path Models", IEEE Transactions on communications, COM-25,
678c2ecf20Sopenharmony_ci       No. 6, June
688c2ecf20Sopenharmony_ci       1977.
698c2ecf20Sopenharmony_ci       https://www.rowetel.com/images/echo/dual_path_paper.pdf
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci   [2] The classic, very useful paper that tells you how to
728c2ecf20Sopenharmony_ci       actually build a real world echo canceller:
738c2ecf20Sopenharmony_ci	 Messerschmitt, Hedberg, Cole, Haoui, Winship, "Digital Voice
748c2ecf20Sopenharmony_ci	 Echo Canceller with a TMS320020,
758c2ecf20Sopenharmony_ci	 https://www.rowetel.com/images/echo/spra129.pdf
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci   [3] I have written a series of blog posts on this work, here is
788c2ecf20Sopenharmony_ci       Part 1: http://www.rowetel.com/blog/?p=18
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci   [4] The source code http://svn.rowetel.com/software/oslec/
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci   [5] A nice reference on LMS filters:
838c2ecf20Sopenharmony_ci	 https://en.wikipedia.org/wiki/Least_mean_squares_filter
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci   Credits:
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci   Thanks to Steve Underwood, Jean-Marc Valin, and Ramakrishnan
888c2ecf20Sopenharmony_ci   Muthukrishnan for their suggestions and email discussions.  Thanks
898c2ecf20Sopenharmony_ci   also to those people who collected echo samples for me such as
908c2ecf20Sopenharmony_ci   Mark, Pawel, and Pavel.
918c2ecf20Sopenharmony_ci*/
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#include <linux/kernel.h>
948c2ecf20Sopenharmony_ci#include <linux/module.h>
958c2ecf20Sopenharmony_ci#include <linux/slab.h>
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci#include "echo.h"
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define MIN_TX_POWER_FOR_ADAPTION	64
1008c2ecf20Sopenharmony_ci#define MIN_RX_POWER_FOR_ADAPTION	64
1018c2ecf20Sopenharmony_ci#define DTD_HANGOVER			600	/* 600 samples, or 75ms     */
1028c2ecf20Sopenharmony_ci#define DC_LOG2BETA			3	/* log2() of DC filter Beta */
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/* adapting coeffs using the traditional stochastic descent (N)LMS algorithm */
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic inline void lms_adapt_bg(struct oslec_state *ec, int clean, int shift)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	int i;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	int offset1;
1118c2ecf20Sopenharmony_ci	int offset2;
1128c2ecf20Sopenharmony_ci	int factor;
1138c2ecf20Sopenharmony_ci	int exp;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	if (shift > 0)
1168c2ecf20Sopenharmony_ci		factor = clean << shift;
1178c2ecf20Sopenharmony_ci	else
1188c2ecf20Sopenharmony_ci		factor = clean >> -shift;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* Update the FIR taps */
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	offset2 = ec->curr_pos;
1238c2ecf20Sopenharmony_ci	offset1 = ec->taps - offset2;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	for (i = ec->taps - 1; i >= offset1; i--) {
1268c2ecf20Sopenharmony_ci		exp = (ec->fir_state_bg.history[i - offset1] * factor);
1278c2ecf20Sopenharmony_ci		ec->fir_taps16[1][i] += (int16_t) ((exp + (1 << 14)) >> 15);
1288c2ecf20Sopenharmony_ci	}
1298c2ecf20Sopenharmony_ci	for (; i >= 0; i--) {
1308c2ecf20Sopenharmony_ci		exp = (ec->fir_state_bg.history[i + offset2] * factor);
1318c2ecf20Sopenharmony_ci		ec->fir_taps16[1][i] += (int16_t) ((exp + (1 << 14)) >> 15);
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic inline int top_bit(unsigned int bits)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	if (bits == 0)
1388c2ecf20Sopenharmony_ci		return -1;
1398c2ecf20Sopenharmony_ci	else
1408c2ecf20Sopenharmony_ci		return (int)fls((int32_t) bits) - 1;
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistruct oslec_state *oslec_create(int len, int adaption_mode)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	struct oslec_state *ec;
1468c2ecf20Sopenharmony_ci	int i;
1478c2ecf20Sopenharmony_ci	const int16_t *history;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	ec = kzalloc(sizeof(*ec), GFP_KERNEL);
1508c2ecf20Sopenharmony_ci	if (!ec)
1518c2ecf20Sopenharmony_ci		return NULL;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	ec->taps = len;
1548c2ecf20Sopenharmony_ci	ec->log2taps = top_bit(len);
1558c2ecf20Sopenharmony_ci	ec->curr_pos = ec->taps - 1;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	ec->fir_taps16[0] =
1588c2ecf20Sopenharmony_ci	    kcalloc(ec->taps, sizeof(int16_t), GFP_KERNEL);
1598c2ecf20Sopenharmony_ci	if (!ec->fir_taps16[0])
1608c2ecf20Sopenharmony_ci		goto error_oom_0;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	ec->fir_taps16[1] =
1638c2ecf20Sopenharmony_ci	    kcalloc(ec->taps, sizeof(int16_t), GFP_KERNEL);
1648c2ecf20Sopenharmony_ci	if (!ec->fir_taps16[1])
1658c2ecf20Sopenharmony_ci		goto error_oom_1;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	history = fir16_create(&ec->fir_state, ec->fir_taps16[0], ec->taps);
1688c2ecf20Sopenharmony_ci	if (!history)
1698c2ecf20Sopenharmony_ci		goto error_state;
1708c2ecf20Sopenharmony_ci	history = fir16_create(&ec->fir_state_bg, ec->fir_taps16[1], ec->taps);
1718c2ecf20Sopenharmony_ci	if (!history)
1728c2ecf20Sopenharmony_ci		goto error_state_bg;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	for (i = 0; i < 5; i++)
1758c2ecf20Sopenharmony_ci		ec->xvtx[i] = ec->yvtx[i] = ec->xvrx[i] = ec->yvrx[i] = 0;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	ec->cng_level = 1000;
1788c2ecf20Sopenharmony_ci	oslec_adaption_mode(ec, adaption_mode);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	ec->snapshot = kcalloc(ec->taps, sizeof(int16_t), GFP_KERNEL);
1818c2ecf20Sopenharmony_ci	if (!ec->snapshot)
1828c2ecf20Sopenharmony_ci		goto error_snap;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	ec->cond_met = 0;
1858c2ecf20Sopenharmony_ci	ec->pstates = 0;
1868c2ecf20Sopenharmony_ci	ec->ltxacc = ec->lrxacc = ec->lcleanacc = ec->lclean_bgacc = 0;
1878c2ecf20Sopenharmony_ci	ec->ltx = ec->lrx = ec->lclean = ec->lclean_bg = 0;
1888c2ecf20Sopenharmony_ci	ec->tx_1 = ec->tx_2 = ec->rx_1 = ec->rx_2 = 0;
1898c2ecf20Sopenharmony_ci	ec->lbgn = ec->lbgn_acc = 0;
1908c2ecf20Sopenharmony_ci	ec->lbgn_upper = 200;
1918c2ecf20Sopenharmony_ci	ec->lbgn_upper_acc = ec->lbgn_upper << 13;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	return ec;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cierror_snap:
1968c2ecf20Sopenharmony_ci	fir16_free(&ec->fir_state_bg);
1978c2ecf20Sopenharmony_cierror_state_bg:
1988c2ecf20Sopenharmony_ci	fir16_free(&ec->fir_state);
1998c2ecf20Sopenharmony_cierror_state:
2008c2ecf20Sopenharmony_ci	kfree(ec->fir_taps16[1]);
2018c2ecf20Sopenharmony_cierror_oom_1:
2028c2ecf20Sopenharmony_ci	kfree(ec->fir_taps16[0]);
2038c2ecf20Sopenharmony_cierror_oom_0:
2048c2ecf20Sopenharmony_ci	kfree(ec);
2058c2ecf20Sopenharmony_ci	return NULL;
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(oslec_create);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_civoid oslec_free(struct oslec_state *ec)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	int i;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	fir16_free(&ec->fir_state);
2148c2ecf20Sopenharmony_ci	fir16_free(&ec->fir_state_bg);
2158c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++)
2168c2ecf20Sopenharmony_ci		kfree(ec->fir_taps16[i]);
2178c2ecf20Sopenharmony_ci	kfree(ec->snapshot);
2188c2ecf20Sopenharmony_ci	kfree(ec);
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(oslec_free);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_civoid oslec_adaption_mode(struct oslec_state *ec, int adaption_mode)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	ec->adaption_mode = adaption_mode;
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(oslec_adaption_mode);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_civoid oslec_flush(struct oslec_state *ec)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	int i;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	ec->ltxacc = ec->lrxacc = ec->lcleanacc = ec->lclean_bgacc = 0;
2338c2ecf20Sopenharmony_ci	ec->ltx = ec->lrx = ec->lclean = ec->lclean_bg = 0;
2348c2ecf20Sopenharmony_ci	ec->tx_1 = ec->tx_2 = ec->rx_1 = ec->rx_2 = 0;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	ec->lbgn = ec->lbgn_acc = 0;
2378c2ecf20Sopenharmony_ci	ec->lbgn_upper = 200;
2388c2ecf20Sopenharmony_ci	ec->lbgn_upper_acc = ec->lbgn_upper << 13;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	ec->nonupdate_dwell = 0;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	fir16_flush(&ec->fir_state);
2438c2ecf20Sopenharmony_ci	fir16_flush(&ec->fir_state_bg);
2448c2ecf20Sopenharmony_ci	ec->fir_state.curr_pos = ec->taps - 1;
2458c2ecf20Sopenharmony_ci	ec->fir_state_bg.curr_pos = ec->taps - 1;
2468c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++)
2478c2ecf20Sopenharmony_ci		memset(ec->fir_taps16[i], 0, ec->taps * sizeof(int16_t));
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	ec->curr_pos = ec->taps - 1;
2508c2ecf20Sopenharmony_ci	ec->pstates = 0;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(oslec_flush);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_civoid oslec_snapshot(struct oslec_state *ec)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	memcpy(ec->snapshot, ec->fir_taps16[0], ec->taps * sizeof(int16_t));
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(oslec_snapshot);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci/* Dual Path Echo Canceller */
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ciint16_t oslec_update(struct oslec_state *ec, int16_t tx, int16_t rx)
2638c2ecf20Sopenharmony_ci{
2648c2ecf20Sopenharmony_ci	int32_t echo_value;
2658c2ecf20Sopenharmony_ci	int clean_bg;
2668c2ecf20Sopenharmony_ci	int tmp;
2678c2ecf20Sopenharmony_ci	int tmp1;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	/*
2708c2ecf20Sopenharmony_ci	 * Input scaling was found be required to prevent problems when tx
2718c2ecf20Sopenharmony_ci	 * starts clipping.  Another possible way to handle this would be the
2728c2ecf20Sopenharmony_ci	 * filter coefficent scaling.
2738c2ecf20Sopenharmony_ci	 */
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	ec->tx = tx;
2768c2ecf20Sopenharmony_ci	ec->rx = rx;
2778c2ecf20Sopenharmony_ci	tx >>= 1;
2788c2ecf20Sopenharmony_ci	rx >>= 1;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	/*
2818c2ecf20Sopenharmony_ci	 * Filter DC, 3dB point is 160Hz (I think), note 32 bit precision
2828c2ecf20Sopenharmony_ci	 * required otherwise values do not track down to 0. Zero at DC, Pole
2838c2ecf20Sopenharmony_ci	 * at (1-Beta) on real axis.  Some chip sets (like Si labs) don't
2848c2ecf20Sopenharmony_ci	 * need this, but something like a $10 X100P card does.  Any DC really
2858c2ecf20Sopenharmony_ci	 * slows down convergence.
2868c2ecf20Sopenharmony_ci	 *
2878c2ecf20Sopenharmony_ci	 * Note: removes some low frequency from the signal, this reduces the
2888c2ecf20Sopenharmony_ci	 * speech quality when listening to samples through headphones but may
2898c2ecf20Sopenharmony_ci	 * not be obvious through a telephone handset.
2908c2ecf20Sopenharmony_ci	 *
2918c2ecf20Sopenharmony_ci	 * Note that the 3dB frequency in radians is approx Beta, e.g. for Beta
2928c2ecf20Sopenharmony_ci	 * = 2^(-3) = 0.125, 3dB freq is 0.125 rads = 159Hz.
2938c2ecf20Sopenharmony_ci	 */
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (ec->adaption_mode & ECHO_CAN_USE_RX_HPF) {
2968c2ecf20Sopenharmony_ci		tmp = rx << 15;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci		/*
2998c2ecf20Sopenharmony_ci		 * Make sure the gain of the HPF is 1.0. This can still
3008c2ecf20Sopenharmony_ci		 * saturate a little under impulse conditions, and it might
3018c2ecf20Sopenharmony_ci		 * roll to 32768 and need clipping on sustained peak level
3028c2ecf20Sopenharmony_ci		 * signals. However, the scale of such clipping is small, and
3038c2ecf20Sopenharmony_ci		 * the error due to any saturation should not markedly affect
3048c2ecf20Sopenharmony_ci		 * the downstream processing.
3058c2ecf20Sopenharmony_ci		 */
3068c2ecf20Sopenharmony_ci		tmp -= (tmp >> 4);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci		ec->rx_1 += -(ec->rx_1 >> DC_LOG2BETA) + tmp - ec->rx_2;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci		/*
3118c2ecf20Sopenharmony_ci		 * hard limit filter to prevent clipping.  Note that at this
3128c2ecf20Sopenharmony_ci		 * stage rx should be limited to +/- 16383 due to right shift
3138c2ecf20Sopenharmony_ci		 * above
3148c2ecf20Sopenharmony_ci		 */
3158c2ecf20Sopenharmony_ci		tmp1 = ec->rx_1 >> 15;
3168c2ecf20Sopenharmony_ci		if (tmp1 > 16383)
3178c2ecf20Sopenharmony_ci			tmp1 = 16383;
3188c2ecf20Sopenharmony_ci		if (tmp1 < -16383)
3198c2ecf20Sopenharmony_ci			tmp1 = -16383;
3208c2ecf20Sopenharmony_ci		rx = tmp1;
3218c2ecf20Sopenharmony_ci		ec->rx_2 = tmp;
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	/* Block average of power in the filter states.  Used for
3258c2ecf20Sopenharmony_ci	   adaption power calculation. */
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	{
3288c2ecf20Sopenharmony_ci		int new, old;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci		/* efficient "out with the old and in with the new" algorithm so
3318c2ecf20Sopenharmony_ci		   we don't have to recalculate over the whole block of
3328c2ecf20Sopenharmony_ci		   samples. */
3338c2ecf20Sopenharmony_ci		new = (int)tx * (int)tx;
3348c2ecf20Sopenharmony_ci		old = (int)ec->fir_state.history[ec->fir_state.curr_pos] *
3358c2ecf20Sopenharmony_ci		    (int)ec->fir_state.history[ec->fir_state.curr_pos];
3368c2ecf20Sopenharmony_ci		ec->pstates +=
3378c2ecf20Sopenharmony_ci		    ((new - old) + (1 << (ec->log2taps - 1))) >> ec->log2taps;
3388c2ecf20Sopenharmony_ci		if (ec->pstates < 0)
3398c2ecf20Sopenharmony_ci			ec->pstates = 0;
3408c2ecf20Sopenharmony_ci	}
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	/* Calculate short term average levels using simple single pole IIRs */
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	ec->ltxacc += abs(tx) - ec->ltx;
3458c2ecf20Sopenharmony_ci	ec->ltx = (ec->ltxacc + (1 << 4)) >> 5;
3468c2ecf20Sopenharmony_ci	ec->lrxacc += abs(rx) - ec->lrx;
3478c2ecf20Sopenharmony_ci	ec->lrx = (ec->lrxacc + (1 << 4)) >> 5;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	/* Foreground filter */
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	ec->fir_state.coeffs = ec->fir_taps16[0];
3528c2ecf20Sopenharmony_ci	echo_value = fir16(&ec->fir_state, tx);
3538c2ecf20Sopenharmony_ci	ec->clean = rx - echo_value;
3548c2ecf20Sopenharmony_ci	ec->lcleanacc += abs(ec->clean) - ec->lclean;
3558c2ecf20Sopenharmony_ci	ec->lclean = (ec->lcleanacc + (1 << 4)) >> 5;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	/* Background filter */
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	echo_value = fir16(&ec->fir_state_bg, tx);
3608c2ecf20Sopenharmony_ci	clean_bg = rx - echo_value;
3618c2ecf20Sopenharmony_ci	ec->lclean_bgacc += abs(clean_bg) - ec->lclean_bg;
3628c2ecf20Sopenharmony_ci	ec->lclean_bg = (ec->lclean_bgacc + (1 << 4)) >> 5;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	/* Background Filter adaption */
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	/* Almost always adap bg filter, just simple DT and energy
3678c2ecf20Sopenharmony_ci	   detection to minimise adaption in cases of strong double talk.
3688c2ecf20Sopenharmony_ci	   However this is not critical for the dual path algorithm.
3698c2ecf20Sopenharmony_ci	 */
3708c2ecf20Sopenharmony_ci	ec->factor = 0;
3718c2ecf20Sopenharmony_ci	ec->shift = 0;
3728c2ecf20Sopenharmony_ci	if (!ec->nonupdate_dwell) {
3738c2ecf20Sopenharmony_ci		int p, logp, shift;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci		/* Determine:
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci		   f = Beta * clean_bg_rx/P ------ (1)
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci		   where P is the total power in the filter states.
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci		   The Boffins have shown that if we obey (1) we converge
3828c2ecf20Sopenharmony_ci		   quickly and avoid instability.
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci		   The correct factor f must be in Q30, as this is the fixed
3858c2ecf20Sopenharmony_ci		   point format required by the lms_adapt_bg() function,
3868c2ecf20Sopenharmony_ci		   therefore the scaled version of (1) is:
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci		   (2^30) * f  = (2^30) * Beta * clean_bg_rx/P
3898c2ecf20Sopenharmony_ci		   factor      = (2^30) * Beta * clean_bg_rx/P     ----- (2)
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci		   We have chosen Beta = 0.25 by experiment, so:
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci		   factor      = (2^30) * (2^-2) * clean_bg_rx/P
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci		   (30 - 2 - log2(P))
3968c2ecf20Sopenharmony_ci		   factor      = clean_bg_rx 2                     ----- (3)
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci		   To avoid a divide we approximate log2(P) as top_bit(P),
3998c2ecf20Sopenharmony_ci		   which returns the position of the highest non-zero bit in
4008c2ecf20Sopenharmony_ci		   P.  This approximation introduces an error as large as a
4018c2ecf20Sopenharmony_ci		   factor of 2, but the algorithm seems to handle it OK.
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci		   Come to think of it a divide may not be a big deal on a
4048c2ecf20Sopenharmony_ci		   modern DSP, so its probably worth checking out the cycles
4058c2ecf20Sopenharmony_ci		   for a divide versus a top_bit() implementation.
4068c2ecf20Sopenharmony_ci		 */
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci		p = MIN_TX_POWER_FOR_ADAPTION + ec->pstates;
4098c2ecf20Sopenharmony_ci		logp = top_bit(p) + ec->log2taps;
4108c2ecf20Sopenharmony_ci		shift = 30 - 2 - logp;
4118c2ecf20Sopenharmony_ci		ec->shift = shift;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci		lms_adapt_bg(ec, clean_bg, shift);
4148c2ecf20Sopenharmony_ci	}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	/* very simple DTD to make sure we dont try and adapt with strong
4178c2ecf20Sopenharmony_ci	   near end speech */
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	ec->adapt = 0;
4208c2ecf20Sopenharmony_ci	if ((ec->lrx > MIN_RX_POWER_FOR_ADAPTION) && (ec->lrx > ec->ltx))
4218c2ecf20Sopenharmony_ci		ec->nonupdate_dwell = DTD_HANGOVER;
4228c2ecf20Sopenharmony_ci	if (ec->nonupdate_dwell)
4238c2ecf20Sopenharmony_ci		ec->nonupdate_dwell--;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	/* Transfer logic */
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	/* These conditions are from the dual path paper [1], I messed with
4288c2ecf20Sopenharmony_ci	   them a bit to improve performance. */
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	if ((ec->adaption_mode & ECHO_CAN_USE_ADAPTION) &&
4318c2ecf20Sopenharmony_ci	    (ec->nonupdate_dwell == 0) &&
4328c2ecf20Sopenharmony_ci	    /* (ec->Lclean_bg < 0.875*ec->Lclean) */
4338c2ecf20Sopenharmony_ci	    (8 * ec->lclean_bg < 7 * ec->lclean) &&
4348c2ecf20Sopenharmony_ci	    /* (ec->Lclean_bg < 0.125*ec->Ltx) */
4358c2ecf20Sopenharmony_ci	    (8 * ec->lclean_bg < ec->ltx)) {
4368c2ecf20Sopenharmony_ci		if (ec->cond_met == 6) {
4378c2ecf20Sopenharmony_ci			/*
4388c2ecf20Sopenharmony_ci			 * BG filter has had better results for 6 consecutive
4398c2ecf20Sopenharmony_ci			 * samples
4408c2ecf20Sopenharmony_ci			 */
4418c2ecf20Sopenharmony_ci			ec->adapt = 1;
4428c2ecf20Sopenharmony_ci			memcpy(ec->fir_taps16[0], ec->fir_taps16[1],
4438c2ecf20Sopenharmony_ci			       ec->taps * sizeof(int16_t));
4448c2ecf20Sopenharmony_ci		} else
4458c2ecf20Sopenharmony_ci			ec->cond_met++;
4468c2ecf20Sopenharmony_ci	} else
4478c2ecf20Sopenharmony_ci		ec->cond_met = 0;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	/* Non-Linear Processing */
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	ec->clean_nlp = ec->clean;
4528c2ecf20Sopenharmony_ci	if (ec->adaption_mode & ECHO_CAN_USE_NLP) {
4538c2ecf20Sopenharmony_ci		/*
4548c2ecf20Sopenharmony_ci		 * Non-linear processor - a fancy way to say "zap small
4558c2ecf20Sopenharmony_ci		 * signals, to avoid residual echo due to (uLaw/ALaw)
4568c2ecf20Sopenharmony_ci		 * non-linearity in the channel.".
4578c2ecf20Sopenharmony_ci		 */
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci		if ((16 * ec->lclean < ec->ltx)) {
4608c2ecf20Sopenharmony_ci			/*
4618c2ecf20Sopenharmony_ci			 * Our e/c has improved echo by at least 24 dB (each
4628c2ecf20Sopenharmony_ci			 * factor of 2 is 6dB, so 2*2*2*2=16 is the same as
4638c2ecf20Sopenharmony_ci			 * 6+6+6+6=24dB)
4648c2ecf20Sopenharmony_ci			 */
4658c2ecf20Sopenharmony_ci			if (ec->adaption_mode & ECHO_CAN_USE_CNG) {
4668c2ecf20Sopenharmony_ci				ec->cng_level = ec->lbgn;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci				/*
4698c2ecf20Sopenharmony_ci				 * Very elementary comfort noise generation.
4708c2ecf20Sopenharmony_ci				 * Just random numbers rolled off very vaguely
4718c2ecf20Sopenharmony_ci				 * Hoth-like.  DR: This noise doesn't sound
4728c2ecf20Sopenharmony_ci				 * quite right to me - I suspect there are some
4738c2ecf20Sopenharmony_ci				 * overflow issues in the filtering as it's too
4748c2ecf20Sopenharmony_ci				 * "crackly".
4758c2ecf20Sopenharmony_ci				 * TODO: debug this, maybe just play noise at
4768c2ecf20Sopenharmony_ci				 * high level or look at spectrum.
4778c2ecf20Sopenharmony_ci				 */
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci				ec->cng_rndnum =
4808c2ecf20Sopenharmony_ci				    1664525U * ec->cng_rndnum + 1013904223U;
4818c2ecf20Sopenharmony_ci				ec->cng_filter =
4828c2ecf20Sopenharmony_ci				    ((ec->cng_rndnum & 0xFFFF) - 32768 +
4838c2ecf20Sopenharmony_ci				     5 * ec->cng_filter) >> 3;
4848c2ecf20Sopenharmony_ci				ec->clean_nlp =
4858c2ecf20Sopenharmony_ci				    (ec->cng_filter * ec->cng_level * 8) >> 14;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci			} else if (ec->adaption_mode & ECHO_CAN_USE_CLIP) {
4888c2ecf20Sopenharmony_ci				/* This sounds much better than CNG */
4898c2ecf20Sopenharmony_ci				if (ec->clean_nlp > ec->lbgn)
4908c2ecf20Sopenharmony_ci					ec->clean_nlp = ec->lbgn;
4918c2ecf20Sopenharmony_ci				if (ec->clean_nlp < -ec->lbgn)
4928c2ecf20Sopenharmony_ci					ec->clean_nlp = -ec->lbgn;
4938c2ecf20Sopenharmony_ci			} else {
4948c2ecf20Sopenharmony_ci				/*
4958c2ecf20Sopenharmony_ci				 * just mute the residual, doesn't sound very
4968c2ecf20Sopenharmony_ci				 * good, used mainly in G168 tests
4978c2ecf20Sopenharmony_ci				 */
4988c2ecf20Sopenharmony_ci				ec->clean_nlp = 0;
4998c2ecf20Sopenharmony_ci			}
5008c2ecf20Sopenharmony_ci		} else {
5018c2ecf20Sopenharmony_ci			/*
5028c2ecf20Sopenharmony_ci			 * Background noise estimator.  I tried a few
5038c2ecf20Sopenharmony_ci			 * algorithms here without much luck.  This very simple
5048c2ecf20Sopenharmony_ci			 * one seems to work best, we just average the level
5058c2ecf20Sopenharmony_ci			 * using a slow (1 sec time const) filter if the
5068c2ecf20Sopenharmony_ci			 * current level is less than a (experimentally
5078c2ecf20Sopenharmony_ci			 * derived) constant.  This means we dont include high
5088c2ecf20Sopenharmony_ci			 * level signals like near end speech.  When combined
5098c2ecf20Sopenharmony_ci			 * with CNG or especially CLIP seems to work OK.
5108c2ecf20Sopenharmony_ci			 */
5118c2ecf20Sopenharmony_ci			if (ec->lclean < 40) {
5128c2ecf20Sopenharmony_ci				ec->lbgn_acc += abs(ec->clean) - ec->lbgn;
5138c2ecf20Sopenharmony_ci				ec->lbgn = (ec->lbgn_acc + (1 << 11)) >> 12;
5148c2ecf20Sopenharmony_ci			}
5158c2ecf20Sopenharmony_ci		}
5168c2ecf20Sopenharmony_ci	}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	/* Roll around the taps buffer */
5198c2ecf20Sopenharmony_ci	if (ec->curr_pos <= 0)
5208c2ecf20Sopenharmony_ci		ec->curr_pos = ec->taps;
5218c2ecf20Sopenharmony_ci	ec->curr_pos--;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	if (ec->adaption_mode & ECHO_CAN_DISABLE)
5248c2ecf20Sopenharmony_ci		ec->clean_nlp = rx;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	/* Output scaled back up again to match input scaling */
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	return (int16_t) ec->clean_nlp << 1;
5298c2ecf20Sopenharmony_ci}
5308c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(oslec_update);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci/* This function is separated from the echo canceller is it is usually called
5338c2ecf20Sopenharmony_ci   as part of the tx process.  See rx HP (DC blocking) filter above, it's
5348c2ecf20Sopenharmony_ci   the same design.
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci   Some soft phones send speech signals with a lot of low frequency
5378c2ecf20Sopenharmony_ci   energy, e.g. down to 20Hz.  This can make the hybrid non-linear
5388c2ecf20Sopenharmony_ci   which causes the echo canceller to fall over.  This filter can help
5398c2ecf20Sopenharmony_ci   by removing any low frequency before it gets to the tx port of the
5408c2ecf20Sopenharmony_ci   hybrid.
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci   It can also help by removing and DC in the tx signal.  DC is bad
5438c2ecf20Sopenharmony_ci   for LMS algorithms.
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci   This is one of the classic DC removal filters, adjusted to provide
5468c2ecf20Sopenharmony_ci   sufficient bass rolloff to meet the above requirement to protect hybrids
5478c2ecf20Sopenharmony_ci   from things that upset them. The difference between successive samples
5488c2ecf20Sopenharmony_ci   produces a lousy HPF, and then a suitably placed pole flattens things out.
5498c2ecf20Sopenharmony_ci   The final result is a nicely rolled off bass end. The filtering is
5508c2ecf20Sopenharmony_ci   implemented with extended fractional precision, which noise shapes things,
5518c2ecf20Sopenharmony_ci   giving very clean DC removal.
5528c2ecf20Sopenharmony_ci*/
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ciint16_t oslec_hpf_tx(struct oslec_state *ec, int16_t tx)
5558c2ecf20Sopenharmony_ci{
5568c2ecf20Sopenharmony_ci	int tmp;
5578c2ecf20Sopenharmony_ci	int tmp1;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	if (ec->adaption_mode & ECHO_CAN_USE_TX_HPF) {
5608c2ecf20Sopenharmony_ci		tmp = tx << 15;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci		/*
5638c2ecf20Sopenharmony_ci		 * Make sure the gain of the HPF is 1.0. The first can still
5648c2ecf20Sopenharmony_ci		 * saturate a little under impulse conditions, and it might
5658c2ecf20Sopenharmony_ci		 * roll to 32768 and need clipping on sustained peak level
5668c2ecf20Sopenharmony_ci		 * signals. However, the scale of such clipping is small, and
5678c2ecf20Sopenharmony_ci		 * the error due to any saturation should not markedly affect
5688c2ecf20Sopenharmony_ci		 * the downstream processing.
5698c2ecf20Sopenharmony_ci		 */
5708c2ecf20Sopenharmony_ci		tmp -= (tmp >> 4);
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci		ec->tx_1 += -(ec->tx_1 >> DC_LOG2BETA) + tmp - ec->tx_2;
5738c2ecf20Sopenharmony_ci		tmp1 = ec->tx_1 >> 15;
5748c2ecf20Sopenharmony_ci		if (tmp1 > 32767)
5758c2ecf20Sopenharmony_ci			tmp1 = 32767;
5768c2ecf20Sopenharmony_ci		if (tmp1 < -32767)
5778c2ecf20Sopenharmony_ci			tmp1 = -32767;
5788c2ecf20Sopenharmony_ci		tx = tmp1;
5798c2ecf20Sopenharmony_ci		ec->tx_2 = tmp;
5808c2ecf20Sopenharmony_ci	}
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	return tx;
5838c2ecf20Sopenharmony_ci}
5848c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(oslec_hpf_tx);
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
5878c2ecf20Sopenharmony_ciMODULE_AUTHOR("David Rowe");
5888c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Open Source Line Echo Canceller");
5898c2ecf20Sopenharmony_ciMODULE_VERSION("0.3.0");
590