blob: 864db5308e62aecf7b335889dcd9283687366b57 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
bjornv@webrtc.org0c6f9312012-01-30 09:39:08 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11/*
12 * Contains the API functions for the AEC.
13 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/audio_processing/aec/echo_cancellation.h"
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +000015
16#include <math.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000017#include <stdlib.h>
18#include <string.h>
19
peah8df5d4f2016-02-23 14:34:59 -080020extern "C" {
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "common_audio/ring_buffer.h"
22#include "common_audio/signal_processing/include/signal_processing_library.h"
peah8df5d4f2016-02-23 14:34:59 -080023}
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/aec/aec_core.h"
25#include "modules/audio_processing/aec/aec_resampler.h"
26#include "modules/audio_processing/logging/apm_data_dumper.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020027#include "typedefs.h" // NOLINT(build/include)
niklase@google.com470e71d2011-07-07 08:21:25 +000028
peah50e21bd2016-03-05 08:39:21 -080029namespace webrtc {
30
kwiberg83ffe452016-08-29 14:46:07 -070031Aec::Aec() = default;
32Aec::~Aec() = default;
33
andrew@webrtc.org1760a172013-09-25 23:17:38 +000034// Measured delays [ms]
35// Device Chrome GTP
36// MacBook Air 10
37// MacBook Retina 10 100
38// MacPro 30?
39//
40// Win7 Desktop 70 80?
41// Win7 T430s 110
42// Win8 T420s 70
43//
44// Daisy 50
45// Pixel (w/ preproc?) 240
46// Pixel (w/o preproc?) 110 110
47
48// The extended filter mode gives us the flexibility to ignore the system's
49// reported delays. We do this for platforms which we believe provide results
50// which are incompatible with the AEC's expectations. Based on measurements
51// (some provided above) we set a conservative (i.e. lower than measured)
52// fixed delay.
53//
54// WEBRTC_UNTRUSTED_DELAY will only have an impact when |extended_filter_mode|
55// is enabled. See the note along with |DelayCorrection| in
56// echo_cancellation_impl.h for more details on the mode.
57//
58// Justification:
59// Chromium/Mac: Here, the true latency is so low (~10-20 ms), that it plays
60// havoc with the AEC's buffering. To avoid this, we set a fixed delay of 20 ms
61// and then compensate by rewinding by 10 ms (in wideband) through
62// kDelayDiffOffsetSamples. This trick does not seem to work for larger rewind
63// values, but fortunately this is sufficient.
64//
65// Chromium/Linux(ChromeOS): The values we get on this platform don't correspond
66// well to reality. The variance doesn't match the AEC's buffer changes, and the
67// bulk values tend to be too low. However, the range across different hardware
68// appears to be too large to choose a single value.
69//
70// GTP/Linux(ChromeOS): TBD, but for the moment we will trust the values.
71#if defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_MAC)
72#define WEBRTC_UNTRUSTED_DELAY
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +000073#endif
andrew@webrtc.orgacb00502013-10-04 16:59:17 +000074
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +000075#if defined(WEBRTC_UNTRUSTED_DELAY) && defined(WEBRTC_MAC)
andrew@webrtc.orgacb00502013-10-04 16:59:17 +000076static const int kDelayDiffOffsetSamples = -160;
77#else
78// Not enabled for now.
79static const int kDelayDiffOffsetSamples = 0;
80#endif
andrew@webrtc.org1760a172013-09-25 23:17:38 +000081
82#if defined(WEBRTC_MAC)
83static const int kFixedDelayMs = 20;
andrew@webrtc.org1760a172013-09-25 23:17:38 +000084#else
andrew@webrtc.org1760a172013-09-25 23:17:38 +000085static const int kFixedDelayMs = 50;
andrew@webrtc.org1760a172013-09-25 23:17:38 +000086#endif
andrew@webrtc.orgc2e471d2013-10-15 02:11:21 +000087#if !defined(WEBRTC_UNTRUSTED_DELAY)
andrew@webrtc.org1760a172013-09-25 23:17:38 +000088static const int kMinTrustedDelayMs = 20;
andrew@webrtc.orgc2e471d2013-10-15 02:11:21 +000089#endif
andrew@webrtc.org1760a172013-09-25 23:17:38 +000090static const int kMaxTrustedDelayMs = 500;
91
niklase@google.com470e71d2011-07-07 08:21:25 +000092// Maximum length of resampled signal. Must be an integer multiple of frames
93// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
94// The factor of 2 handles wb, and the + 1 is as a safety margin
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000095// TODO(bjornv): Replace with kResamplerBufferSize
niklase@google.com470e71d2011-07-07 08:21:25 +000096#define MAX_RESAMP_LEN (5 * FRAME_LEN)
97
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000098static const int kMaxBufSizeStart = 62; // In partitions
andrew@webrtc.org13b2d462013-10-08 23:41:42 +000099static const int sampMsNb = 8; // samples per ms in nb
niklase@google.com470e71d2011-07-07 08:21:25 +0000100static const int initCheck = 42;
101
peahb46083e2016-05-03 07:01:18 -0700102int Aec::instance_count = 0;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000103
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000104// Estimates delay to set the position of the far-end buffer read pointer
niklase@google.com470e71d2011-07-07 08:21:25 +0000105// (controlled by knownDelay)
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000106static void EstBufDelayNormal(Aec* aecInst);
107static void EstBufDelayExtended(Aec* aecInst);
Alex Loiko890988c2017-08-31 10:25:48 +0200108static int ProcessNormal(Aec* aecInst,
109 const float* const* nearend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700110 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000111 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700112 size_t num_samples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000113 int16_t reported_delay_ms,
114 int32_t skew);
Alex Loiko890988c2017-08-31 10:25:48 +0200115static void ProcessExtended(Aec* aecInst,
116 const float* const* nearend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700117 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000118 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700119 size_t num_samples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000120 int16_t reported_delay_ms,
121 int32_t skew);
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
Bjorn Volcker9345e862015-06-10 21:43:36 +0200123void* WebRtcAec_Create() {
peah3f08dc62016-05-05 03:03:55 -0700124 Aec* aecpc = new Aec();
Bjorn Volcker9345e862015-06-10 21:43:36 +0200125
126 if (!aecpc) {
127 return NULL;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000128 }
peah3f08dc62016-05-05 03:03:55 -0700129 aecpc->data_dumper.reset(new ApmDataDumper(aecpc->instance_count));
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
peahb46083e2016-05-03 07:01:18 -0700131 aecpc->aec = WebRtcAec_CreateAec(aecpc->instance_count);
Bjorn Volcker9345e862015-06-10 21:43:36 +0200132 if (!aecpc->aec) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000133 WebRtcAec_Free(aecpc);
Bjorn Volcker9345e862015-06-10 21:43:36 +0200134 return NULL;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000135 }
Bjorn Volcker9345e862015-06-10 21:43:36 +0200136 aecpc->resampler = WebRtcAec_CreateResampler();
137 if (!aecpc->resampler) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000138 WebRtcAec_Free(aecpc);
Bjorn Volcker9345e862015-06-10 21:43:36 +0200139 return NULL;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000140 }
141 // Create far-end pre-buffer. The buffer size has to be large enough for
142 // largest possible drift compensation (kResamplerBufferSize) + "almost" an
143 // FFT buffer (PART_LEN2 - 1).
144 aecpc->far_pre_buf =
145 WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize, sizeof(float));
146 if (!aecpc->far_pre_buf) {
147 WebRtcAec_Free(aecpc);
Bjorn Volcker9345e862015-06-10 21:43:36 +0200148 return NULL;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000149 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000150
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000151 aecpc->initFlag = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
peahb46083e2016-05-03 07:01:18 -0700153 aecpc->instance_count++;
Bjorn Volcker9345e862015-06-10 21:43:36 +0200154 return aecpc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000155}
156
Bjorn Volckerf6a99e62015-04-10 07:56:57 +0200157void WebRtcAec_Free(void* aecInst) {
peah8df5d4f2016-02-23 14:34:59 -0800158 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000159
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000160 if (aecpc == NULL) {
Bjorn Volckerf6a99e62015-04-10 07:56:57 +0200161 return;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000162 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000163
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000164 WebRtc_FreeBuffer(aecpc->far_pre_buf);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000165
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000166 WebRtcAec_FreeAec(aecpc->aec);
167 WebRtcAec_FreeResampler(aecpc->resampler);
peah3f08dc62016-05-05 03:03:55 -0700168 delete aecpc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000169}
170
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000171int32_t WebRtcAec_Init(void* aecInst, int32_t sampFreq, int32_t scSampFreq) {
peah8df5d4f2016-02-23 14:34:59 -0800172 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
peah3f08dc62016-05-05 03:03:55 -0700173 aecpc->data_dumper->InitiateNewSetOfRecordings();
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000174 AecConfig aecConfig;
niklase@google.com470e71d2011-07-07 08:21:25 +0000175
peahff63ed22016-01-29 07:46:13 -0800176 if (sampFreq != 8000 && sampFreq != 16000 && sampFreq != 32000 &&
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000177 sampFreq != 48000) {
peahc12be392015-11-09 23:53:50 -0800178 return AEC_BAD_PARAMETER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000179 }
180 aecpc->sampFreq = sampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000182 if (scSampFreq < 1 || scSampFreq > 96000) {
peahc12be392015-11-09 23:53:50 -0800183 return AEC_BAD_PARAMETER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000184 }
185 aecpc->scSampFreq = scSampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000186
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000187 // Initialize echo canceller core
188 if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) {
peahc12be392015-11-09 23:53:50 -0800189 return AEC_UNSPECIFIED_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000190 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000191
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000192 if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) {
peahc12be392015-11-09 23:53:50 -0800193 return AEC_UNSPECIFIED_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000194 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
andrew@webrtc.org6b630152015-01-15 00:09:53 +0000196 WebRtc_InitBuffer(aecpc->far_pre_buf);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000197 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap.
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000198
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000199 aecpc->initFlag = initCheck; // indicates that initialization has been done
niklase@google.com470e71d2011-07-07 08:21:25 +0000200
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000201 if (aecpc->sampFreq == 32000 || aecpc->sampFreq == 48000) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000202 aecpc->splitSampFreq = 16000;
203 } else {
204 aecpc->splitSampFreq = sampFreq;
205 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000206
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000207 aecpc->delayCtr = 0;
208 aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq;
209 // Sampling frequency multiplier (SWB is processed as 160 frame size).
210 aecpc->rate_factor = aecpc->splitSampFreq / 8000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000212 aecpc->sum = 0;
213 aecpc->counter = 0;
214 aecpc->checkBuffSize = 1;
215 aecpc->firstVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000216
Bjorn Volcker71012692015-06-18 11:04:56 +0200217 // We skip the startup_phase completely (setting to 0) if DA-AEC is enabled,
218 // but not extended_filter mode.
219 aecpc->startup_phase = WebRtcAec_extended_filter_enabled(aecpc->aec) ||
peahff63ed22016-01-29 07:46:13 -0800220 !WebRtcAec_delay_agnostic_enabled(aecpc->aec);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000221 aecpc->bufSizeStart = 0;
222 aecpc->checkBufSizeCtr = 0;
223 aecpc->msInSndCardBuf = 0;
224 aecpc->filtDelay = -1; // -1 indicates an initialized state.
225 aecpc->timeForDelayChange = 0;
226 aecpc->knownDelay = 0;
227 aecpc->lastDelayDiff = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000228
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000229 aecpc->skewFrCtr = 0;
230 aecpc->resample = kAecFalse;
231 aecpc->highSkewCtr = 0;
232 aecpc->skew = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000233
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000234 aecpc->farend_started = 0;
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000235
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000236 // Default settings.
237 aecConfig.nlpMode = kAecNlpModerate;
238 aecConfig.skewMode = kAecFalse;
239 aecConfig.metricsMode = kAecFalse;
240 aecConfig.delay_logging = kAecFalse;
niklase@google.com470e71d2011-07-07 08:21:25 +0000241
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000242 if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
peahc12be392015-11-09 23:53:50 -0800243 return AEC_UNSPECIFIED_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000244 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000245
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000246 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000247}
248
peahc12be392015-11-09 23:53:50 -0800249// Returns any error that is caused when buffering the
250// far-end signal.
251int32_t WebRtcAec_GetBufferFarendError(void* aecInst,
252 const float* farend,
253 size_t nrOfSamples) {
peah8df5d4f2016-02-23 14:34:59 -0800254 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
peahc12be392015-11-09 23:53:50 -0800255
256 if (!farend)
257 return AEC_NULL_POINTER_ERROR;
258
259 if (aecpc->initFlag != initCheck)
260 return AEC_UNINITIALIZED_ERROR;
261
262 // number of samples == 160 for SWB input
263 if (nrOfSamples != 80 && nrOfSamples != 160)
264 return AEC_BAD_PARAMETER_ERROR;
265
266 return 0;
267}
268
niklase@google.com470e71d2011-07-07 08:21:25 +0000269// only buffer L band for farend
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000270int32_t WebRtcAec_BufferFarend(void* aecInst,
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000271 const float* farend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700272 size_t nrOfSamples) {
peah8df5d4f2016-02-23 14:34:59 -0800273 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700274 size_t newNrOfSamples = nrOfSamples;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000275 float new_farend[MAX_RESAMP_LEN];
276 const float* farend_ptr = farend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000277
peahc12be392015-11-09 23:53:50 -0800278 // Get any error caused by buffering the farend signal.
peahff63ed22016-01-29 07:46:13 -0800279 int32_t error_code =
280 WebRtcAec_GetBufferFarendError(aecInst, farend, nrOfSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +0000281
peahc12be392015-11-09 23:53:50 -0800282 if (error_code != 0)
283 return error_code;
niklase@google.com470e71d2011-07-07 08:21:25 +0000284
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000285 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
286 // Resample and get a new number of samples
peahff63ed22016-01-29 07:46:13 -0800287 WebRtcAec_ResampleLinear(aecpc->resampler, farend, nrOfSamples, aecpc->skew,
288 new_farend, &newNrOfSamples);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000289 farend_ptr = new_farend;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000290 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000291
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000292 aecpc->farend_started = 1;
peah8df5d4f2016-02-23 14:34:59 -0800293 WebRtcAec_SetSystemDelay(aecpc->aec, WebRtcAec_system_delay(aecpc->aec) +
294 static_cast<int>(newNrOfSamples));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000295
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000296 // Write the time-domain data to |far_pre_buf|.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700297 WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_ptr, newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000298
peaha421ddd2016-09-12 11:27:14 -0700299 // TODO(minyue): reduce to |PART_LEN| samples for each buffering.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000300 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
301 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000302 {
andrew@webrtc.orge65d9d92015-01-21 22:05:12 +0000303 float* ptmp = NULL;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000304 float tmp[PART_LEN2];
peah8df5d4f2016-02-23 14:34:59 -0800305 WebRtc_ReadBuffer(aecpc->far_pre_buf,
306 reinterpret_cast<void**>(&ptmp), tmp, PART_LEN2);
peaha421ddd2016-09-12 11:27:14 -0700307 WebRtcAec_BufferFarendBlock(aecpc->aec, &ptmp[PART_LEN]);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000308 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000309
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000310 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
311 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000312 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000313
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000314 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000315}
316
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000317int32_t WebRtcAec_Process(void* aecInst,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000318 const float* const* nearend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700319 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000320 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700321 size_t nrOfSamples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000322 int16_t msInSndCardBuf,
323 int32_t skew) {
peah8df5d4f2016-02-23 14:34:59 -0800324 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000325 int32_t retVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000326
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000327 if (out == NULL) {
peahc12be392015-11-09 23:53:50 -0800328 return AEC_NULL_POINTER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000329 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000330
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000331 if (aecpc->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800332 return AEC_UNINITIALIZED_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000333 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000334
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000335 // number of samples == 160 for SWB input
336 if (nrOfSamples != 80 && nrOfSamples != 160) {
peahc12be392015-11-09 23:53:50 -0800337 return AEC_BAD_PARAMETER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000338 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000339
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000340 if (msInSndCardBuf < 0) {
341 msInSndCardBuf = 0;
peahc12be392015-11-09 23:53:50 -0800342 retVal = AEC_BAD_PARAMETER_WARNING;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000343 } else if (msInSndCardBuf > kMaxTrustedDelayMs) {
344 // The clamping is now done in ProcessExtended/Normal().
peahc12be392015-11-09 23:53:50 -0800345 retVal = AEC_BAD_PARAMETER_WARNING;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000346 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000347
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000348 // This returns the value of aec->extended_filter_enabled.
Henrik Lundin441f6342015-06-09 16:03:13 +0200349 if (WebRtcAec_extended_filter_enabled(aecpc->aec)) {
peahff63ed22016-01-29 07:46:13 -0800350 ProcessExtended(aecpc, nearend, num_bands, out, nrOfSamples, msInSndCardBuf,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000351 skew);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000352 } else {
peahff63ed22016-01-29 07:46:13 -0800353 retVal = ProcessNormal(aecpc, nearend, num_bands, out, nrOfSamples,
354 msInSndCardBuf, skew);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000355 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000356
peah3f08dc62016-05-05 03:03:55 -0700357 int far_buf_size_samples = WebRtcAec_system_delay(aecpc->aec);
358 aecpc->data_dumper->DumpRaw("aec_system_delay", 1, &far_buf_size_samples);
359 aecpc->data_dumper->DumpRaw("aec_known_delay", 1, &aecpc->knownDelay);
niklase@google.com470e71d2011-07-07 08:21:25 +0000360
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000361 return retVal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000362}
363
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000364int WebRtcAec_set_config(void* handle, AecConfig config) {
peah8df5d4f2016-02-23 14:34:59 -0800365 Aec* self = reinterpret_cast<Aec*>(handle);
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000366 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800367 return AEC_UNINITIALIZED_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000368 }
369
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000370 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
peahc12be392015-11-09 23:53:50 -0800371 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000372 }
373 self->skewMode = config.skewMode;
374
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000375 if (config.nlpMode != kAecNlpConservative &&
376 config.nlpMode != kAecNlpModerate &&
377 config.nlpMode != kAecNlpAggressive) {
peahc12be392015-11-09 23:53:50 -0800378 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000379 }
380
381 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
peahc12be392015-11-09 23:53:50 -0800382 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000383 }
384
385 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
peahc12be392015-11-09 23:53:50 -0800386 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000387 }
388
peahff63ed22016-01-29 07:46:13 -0800389 WebRtcAec_SetConfigCore(self->aec, config.nlpMode, config.metricsMode,
390 config.delay_logging);
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000391 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000392}
393
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000394int WebRtcAec_get_echo_status(void* handle, int* status) {
peah8df5d4f2016-02-23 14:34:59 -0800395 Aec* self = reinterpret_cast<Aec*>(handle);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000396 if (status == NULL) {
peahc12be392015-11-09 23:53:50 -0800397 return AEC_NULL_POINTER_ERROR;
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000398 }
399 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800400 return AEC_UNINITIALIZED_ERROR;
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000401 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000402
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000403 *status = WebRtcAec_echo_state(self->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000404
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000405 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000406}
407
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000408int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) {
409 const float kUpWeight = 0.7f;
410 float dtmp;
411 int stmp;
peah8df5d4f2016-02-23 14:34:59 -0800412 Aec* self = reinterpret_cast<Aec*>(handle);
bjornv@webrtc.orgcea70f42013-02-19 21:03:10 +0000413 Stats erl;
414 Stats erle;
415 Stats a_nlp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000416
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000417 if (handle == NULL) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000418 return -1;
419 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000420 if (metrics == NULL) {
peahc12be392015-11-09 23:53:50 -0800421 return AEC_NULL_POINTER_ERROR;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000422 }
423 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800424 return AEC_UNINITIALIZED_ERROR;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000425 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000426
minyue50453372016-04-07 06:36:43 -0700427 WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp,
428 &metrics->divergent_filter_fraction);
niklase@google.com470e71d2011-07-07 08:21:25 +0000429
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000430 // ERL
peah8df5d4f2016-02-23 14:34:59 -0800431 metrics->erl.instant = static_cast<int>(erl.instant);
niklase@google.com470e71d2011-07-07 08:21:25 +0000432
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000433 if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000434 // Use a mix between regular average and upper part average.
435 dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average;
peah8df5d4f2016-02-23 14:34:59 -0800436 metrics->erl.average = static_cast<int>(dtmp);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000437 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000438 metrics->erl.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000439 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000440
peah8df5d4f2016-02-23 14:34:59 -0800441 metrics->erl.max = static_cast<int>(erl.max);
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000443 if (erl.min < (kOffsetLevel * (-1))) {
peah8df5d4f2016-02-23 14:34:59 -0800444 metrics->erl.min = static_cast<int>(erl.min);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000445 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000446 metrics->erl.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000447 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000448
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000449 // ERLE
peah8df5d4f2016-02-23 14:34:59 -0800450 metrics->erle.instant = static_cast<int>(erle.instant);
niklase@google.com470e71d2011-07-07 08:21:25 +0000451
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000452 if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000453 // Use a mix between regular average and upper part average.
454 dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average;
peah8df5d4f2016-02-23 14:34:59 -0800455 metrics->erle.average = static_cast<int>(dtmp);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000456 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000457 metrics->erle.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000458 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000459
peah8df5d4f2016-02-23 14:34:59 -0800460 metrics->erle.max = static_cast<int>(erle.max);
niklase@google.com470e71d2011-07-07 08:21:25 +0000461
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000462 if (erle.min < (kOffsetLevel * (-1))) {
peah8df5d4f2016-02-23 14:34:59 -0800463 metrics->erle.min = static_cast<int>(erle.min);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000464 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000465 metrics->erle.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000466 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000467
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000468 // RERL
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000469 if ((metrics->erl.average > kOffsetLevel) &&
470 (metrics->erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000471 stmp = metrics->erl.average + metrics->erle.average;
472 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000473 stmp = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000474 }
475 metrics->rerl.average = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000476
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000477 // No other statistics needed, but returned for completeness.
478 metrics->rerl.instant = stmp;
479 metrics->rerl.max = stmp;
480 metrics->rerl.min = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000481
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000482 // A_NLP
peah8df5d4f2016-02-23 14:34:59 -0800483 metrics->aNlp.instant = static_cast<int>(a_nlp.instant);
niklase@google.com470e71d2011-07-07 08:21:25 +0000484
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000485 if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000486 // Use a mix between regular average and upper part average.
487 dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average;
peah8df5d4f2016-02-23 14:34:59 -0800488 metrics->aNlp.average = static_cast<int>(dtmp);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000489 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000490 metrics->aNlp.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000491 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
peah8df5d4f2016-02-23 14:34:59 -0800493 metrics->aNlp.max = static_cast<int>(a_nlp.max);
niklase@google.com470e71d2011-07-07 08:21:25 +0000494
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000495 if (a_nlp.min < (kOffsetLevel * (-1))) {
peah8df5d4f2016-02-23 14:34:59 -0800496 metrics->aNlp.min = static_cast<int>(a_nlp.min);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000497 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000498 metrics->aNlp.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000499 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000500
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000501 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000502}
503
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000504int WebRtcAec_GetDelayMetrics(void* handle,
505 int* median,
506 int* std,
507 float* fraction_poor_delays) {
peah8df5d4f2016-02-23 14:34:59 -0800508 Aec* self = reinterpret_cast<Aec*>(handle);
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000509 if (median == NULL) {
peahc12be392015-11-09 23:53:50 -0800510 return AEC_NULL_POINTER_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000511 }
512 if (std == NULL) {
peahc12be392015-11-09 23:53:50 -0800513 return AEC_NULL_POINTER_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000514 }
515 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800516 return AEC_UNINITIALIZED_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000517 }
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000518 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std,
peahff63ed22016-01-29 07:46:13 -0800519 fraction_poor_delays) == -1) {
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000520 // Logging disabled.
peahc12be392015-11-09 23:53:50 -0800521 return AEC_UNSUPPORTED_FUNCTION_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000522 }
523
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000524 return 0;
525}
526
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000527AecCore* WebRtcAec_aec_core(void* handle) {
528 if (!handle) {
529 return NULL;
530 }
peah8df5d4f2016-02-23 14:34:59 -0800531 return reinterpret_cast<Aec*>(handle)->aec;
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000532}
533
Alex Loiko890988c2017-08-31 10:25:48 +0200534static int ProcessNormal(Aec* aecInst,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000535 const float* const* nearend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700536 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000537 float* const* out,
Alex Loiko890988c2017-08-31 10:25:48 +0200538 size_t num_samples,
539 int16_t reported_delay_ms,
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000540 int32_t skew) {
541 int retVal = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700542 size_t i;
543 size_t nBlocks10ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000544 // Limit resampling to doubling/halving of signal
545 const float minSkewEst = -0.5f;
546 const float maxSkewEst = 1.0f;
547
Alex Loiko890988c2017-08-31 10:25:48 +0200548 reported_delay_ms =
549 reported_delay_ms > kMaxTrustedDelayMs ? kMaxTrustedDelayMs :
550 reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000551 // TODO(andrew): we need to investigate if this +10 is really wanted.
Alex Loiko890988c2017-08-31 10:25:48 +0200552 reported_delay_ms += 10;
553 aecInst->msInSndCardBuf = reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000554
Alex Loiko890988c2017-08-31 10:25:48 +0200555 if (aecInst->skewMode == kAecTrue) {
556 if (aecInst->skewFrCtr < 25) {
557 aecInst->skewFrCtr++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000558 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200559 retVal = WebRtcAec_GetSkew(aecInst->resampler, skew, &aecInst->skew);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000560 if (retVal == -1) {
Alex Loiko890988c2017-08-31 10:25:48 +0200561 aecInst->skew = 0;
peahc12be392015-11-09 23:53:50 -0800562 retVal = AEC_BAD_PARAMETER_WARNING;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000563 }
564
Alex Loiko890988c2017-08-31 10:25:48 +0200565 aecInst->skew /= aecInst->sampFactor * num_samples;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000566
Alex Loiko890988c2017-08-31 10:25:48 +0200567 if (aecInst->skew < 1.0e-3 && aecInst->skew > -1.0e-3) {
568 aecInst->resample = kAecFalse;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000569 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200570 aecInst->resample = kAecTrue;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000571 }
572
Alex Loiko890988c2017-08-31 10:25:48 +0200573 if (aecInst->skew < minSkewEst) {
574 aecInst->skew = minSkewEst;
575 } else if (aecInst->skew > maxSkewEst) {
576 aecInst->skew = maxSkewEst;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000577 }
578
Alex Loiko890988c2017-08-31 10:25:48 +0200579 aecInst->data_dumper->DumpRaw("aec_skew", 1, &aecInst->skew);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000580 }
581 }
582
Alex Loiko890988c2017-08-31 10:25:48 +0200583 nBlocks10ms = num_samples / (FRAME_LEN * aecInst->rate_factor);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000584
Alex Loiko890988c2017-08-31 10:25:48 +0200585 if (aecInst->startup_phase) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000586 for (i = 0; i < num_bands; ++i) {
587 // Only needed if they don't already point to the same place.
588 if (nearend[i] != out[i]) {
Alex Loiko890988c2017-08-31 10:25:48 +0200589 memcpy(out[i], nearend[i], sizeof(nearend[i][0]) * num_samples);
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000590 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000591 }
592
593 // The AEC is in the start up mode
594 // AEC is disabled until the system delay is OK
595
596 // Mechanism to ensure that the system delay is reasonably stable.
Alex Loiko890988c2017-08-31 10:25:48 +0200597 if (aecInst->checkBuffSize) {
598 aecInst->checkBufSizeCtr++;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000599 // Before we fill up the far-end buffer we require the system delay
600 // to be stable (+/-8 ms) compared to the first value. This
601 // comparison is made during the following 6 consecutive 10 ms
602 // blocks. If it seems to be stable then we start to fill up the
603 // far-end buffer.
Alex Loiko890988c2017-08-31 10:25:48 +0200604 if (aecInst->counter == 0) {
605 aecInst->firstVal = aecInst->msInSndCardBuf;
606 aecInst->sum = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000607 }
608
Alex Loiko890988c2017-08-31 10:25:48 +0200609 if (abs(aecInst->firstVal - aecInst->msInSndCardBuf) <
610 WEBRTC_SPL_MAX(0.2 * aecInst->msInSndCardBuf, sampMsNb)) {
611 aecInst->sum += aecInst->msInSndCardBuf;
612 aecInst->counter++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000613 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200614 aecInst->counter = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000615 }
616
Alex Loiko890988c2017-08-31 10:25:48 +0200617 if (aecInst->counter * nBlocks10ms >= 6) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000618 // The far-end buffer size is determined in partitions of
619 // PART_LEN samples. Use 75% of the average value of the system
620 // delay as buffer size to start with.
Alex Loiko890988c2017-08-31 10:25:48 +0200621 aecInst->bufSizeStart =
622 WEBRTC_SPL_MIN((3 * aecInst->sum * aecInst->rate_factor * 8) /
623 (4 * aecInst->counter * PART_LEN),
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000624 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000625 // Buffer size has now been determined.
Alex Loiko890988c2017-08-31 10:25:48 +0200626 aecInst->checkBuffSize = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000627 }
628
Alex Loiko890988c2017-08-31 10:25:48 +0200629 if (aecInst->checkBufSizeCtr * nBlocks10ms > 50) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000630 // For really bad systems, don't disable the echo canceller for
631 // more than 0.5 sec.
Alex Loiko890988c2017-08-31 10:25:48 +0200632 aecInst->bufSizeStart = WEBRTC_SPL_MIN(
633 (aecInst->msInSndCardBuf * aecInst->rate_factor * 3) / 40,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000634 kMaxBufSizeStart);
Alex Loiko890988c2017-08-31 10:25:48 +0200635 aecInst->checkBuffSize = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000636 }
637 }
638
639 // If |checkBuffSize| changed in the if-statement above.
Alex Loiko890988c2017-08-31 10:25:48 +0200640 if (!aecInst->checkBuffSize) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000641 // The system delay is now reasonably stable (or has been unstable
642 // for too long). When the far-end buffer is filled with
643 // approximately the same amount of data as reported by the system
644 // we end the startup phase.
645 int overhead_elements =
Alex Loiko890988c2017-08-31 10:25:48 +0200646 WebRtcAec_system_delay(aecInst->aec) / PART_LEN -
647 aecInst->bufSizeStart;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000648 if (overhead_elements == 0) {
649 // Enable the AEC
Alex Loiko890988c2017-08-31 10:25:48 +0200650 aecInst->startup_phase = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000651 } else if (overhead_elements > 0) {
652 // TODO(bjornv): Do we need a check on how much we actually
653 // moved the read pointer? It should always be possible to move
654 // the pointer |overhead_elements| since we have only added data
655 // to the buffer and no delay compensation nor AEC processing
656 // has been done.
Alex Loiko890988c2017-08-31 10:25:48 +0200657 WebRtcAec_AdjustFarendBufferSizeAndSystemDelay(aecInst->aec,
peaha421ddd2016-09-12 11:27:14 -0700658 overhead_elements);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000659
660 // Enable the AEC
Alex Loiko890988c2017-08-31 10:25:48 +0200661 aecInst->startup_phase = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000662 }
663 }
664 } else {
665 // AEC is enabled.
Alex Loiko890988c2017-08-31 10:25:48 +0200666 EstBufDelayNormal(aecInst);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000667
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000668 // Call the AEC.
669 // TODO(bjornv): Re-structure such that we don't have to pass
Alex Loiko890988c2017-08-31 10:25:48 +0200670 // |aecInst->knownDelay| as input. Change name to something like
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000671 // |system_buffer_diff|.
Alex Loiko890988c2017-08-31 10:25:48 +0200672 WebRtcAec_ProcessFrames(aecInst->aec, nearend, num_bands, num_samples,
673 aecInst->knownDelay, out);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000674 }
675
676 return retVal;
677}
678
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000679static void ProcessExtended(Aec* self,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000680 const float* const* near,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700681 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000682 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700683 size_t num_samples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000684 int16_t reported_delay_ms,
685 int32_t skew) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700686 size_t i;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000687 const int delay_diff_offset = kDelayDiffOffsetSamples;
peah906f4032016-09-08 09:49:42 -0700688 RTC_DCHECK(num_samples == 80 || num_samples == 160);
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +0000689#if defined(WEBRTC_UNTRUSTED_DELAY)
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000690 reported_delay_ms = kFixedDelayMs;
691#else
692 // This is the usual mode where we trust the reported system delay values.
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000693 // Due to the longer filter, we no longer add 10 ms to the reported delay
694 // to reduce chance of non-causality. Instead we apply a minimum here to avoid
695 // issues with the read pointer jumping around needlessly.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000696 reported_delay_ms = reported_delay_ms < kMinTrustedDelayMs
697 ? kMinTrustedDelayMs
698 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000699 // If the reported delay appears to be bogus, we attempt to recover by using
700 // the measured fixed delay values. We use >= here because higher layers
701 // may already clamp to this maximum value, and we would otherwise not
702 // detect it here.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000703 reported_delay_ms = reported_delay_ms >= kMaxTrustedDelayMs
704 ? kFixedDelayMs
705 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000706#endif
707 self->msInSndCardBuf = reported_delay_ms;
708
709 if (!self->farend_started) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000710 for (i = 0; i < num_bands; ++i) {
711 // Only needed if they don't already point to the same place.
712 if (near[i] != out[i]) {
713 memcpy(out[i], near[i], sizeof(near[i][0]) * num_samples);
714 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000715 }
716 return;
717 }
718 if (self->startup_phase) {
719 // In the extended mode, there isn't a startup "phase", just a special
720 // action on the first frame. In the trusted delay case, we'll take the
721 // current reported delay, unless it's less then our conservative
722 // measurement.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000723 int startup_size_ms =
724 reported_delay_ms < kFixedDelayMs ? kFixedDelayMs : reported_delay_ms;
Bjorn Volcker71012692015-06-18 11:04:56 +0200725#if defined(WEBRTC_ANDROID)
Bjorn Volcker1ff218f2015-05-06 12:08:38 +0200726 int target_delay = startup_size_ms * self->rate_factor * 8;
Bjorn Volcker71012692015-06-18 11:04:56 +0200727#else
Bjorn Volcker1ff218f2015-05-06 12:08:38 +0200728 // To avoid putting the AEC in a non-causal state we're being slightly
729 // conservative and scale by 2. On Android we use a fixed delay and
730 // therefore there is no need to scale the target_delay.
Bjorn Volcker71012692015-06-18 11:04:56 +0200731 int target_delay = startup_size_ms * self->rate_factor * 8 / 2;
Bjorn Volcker1ff218f2015-05-06 12:08:38 +0200732#endif
733 int overhead_elements =
734 (WebRtcAec_system_delay(self->aec) - target_delay) / PART_LEN;
peaha421ddd2016-09-12 11:27:14 -0700735 WebRtcAec_AdjustFarendBufferSizeAndSystemDelay(self->aec,
736 overhead_elements);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000737 self->startup_phase = 0;
738 }
739
Bjorn Volcker71012692015-06-18 11:04:56 +0200740 EstBufDelayExtended(self);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000741
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000742 {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000743 // |delay_diff_offset| gives us the option to manually rewind the delay on
744 // very low delay platforms which can't be expressed purely through
745 // |reported_delay_ms|.
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000746 const int adjusted_known_delay =
747 WEBRTC_SPL_MAX(0, self->knownDelay + delay_diff_offset);
748
peahff63ed22016-01-29 07:46:13 -0800749 WebRtcAec_ProcessFrames(self->aec, near, num_bands, num_samples,
750 adjusted_known_delay, out);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000751 }
752}
753
Alex Loiko890988c2017-08-31 10:25:48 +0200754static void EstBufDelayNormal(Aec* aecInst) {
755 int nSampSndCard = aecInst->msInSndCardBuf * sampMsNb * aecInst->rate_factor;
756 int current_delay = nSampSndCard - WebRtcAec_system_delay(aecInst->aec);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000757 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000758
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000759 // Before we proceed with the delay estimate filtering we:
760 // 1) Compensate for the frame that will be read.
761 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000762 // 3) Compensate for non-causality if needed, since the estimated delay can't
763 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000764
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000765 // 1) Compensating for the frame(s) that will be read/processed.
Alex Loiko890988c2017-08-31 10:25:48 +0200766 current_delay += FRAME_LEN * aecInst->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000767
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000768 // 2) Account for resampling frame delay.
Alex Loiko890988c2017-08-31 10:25:48 +0200769 if (aecInst->skewMode == kAecTrue && aecInst->resample == kAecTrue) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000770 current_delay -= kResamplingDelay;
771 }
772
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000773 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000774 if (current_delay < PART_LEN) {
peaha421ddd2016-09-12 11:27:14 -0700775 current_delay +=
Alex Loiko890988c2017-08-31 10:25:48 +0200776 WebRtcAec_AdjustFarendBufferSizeAndSystemDelay(aecInst->aec, 1) *
peaha421ddd2016-09-12 11:27:14 -0700777 PART_LEN;
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000778 }
779
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000780 // We use -1 to signal an initialized state in the "extended" implementation;
781 // compensate for that.
Alex Loiko890988c2017-08-31 10:25:48 +0200782 aecInst->filtDelay = aecInst->filtDelay < 0 ? 0 : aecInst->filtDelay;
783 aecInst->filtDelay =
peah8df5d4f2016-02-23 14:34:59 -0800784 WEBRTC_SPL_MAX(0, static_cast<int16_t>(0.8 *
Alex Loiko890988c2017-08-31 10:25:48 +0200785 aecInst->filtDelay +
peah8df5d4f2016-02-23 14:34:59 -0800786 0.2 * current_delay));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000787
Alex Loiko890988c2017-08-31 10:25:48 +0200788 delay_difference = aecInst->filtDelay - aecInst->knownDelay;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000789 if (delay_difference > 224) {
Alex Loiko890988c2017-08-31 10:25:48 +0200790 if (aecInst->lastDelayDiff < 96) {
791 aecInst->timeForDelayChange = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000792 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200793 aecInst->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000794 }
Alex Loiko890988c2017-08-31 10:25:48 +0200795 } else if (delay_difference < 96 && aecInst->knownDelay > 0) {
796 if (aecInst->lastDelayDiff > 224) {
797 aecInst->timeForDelayChange = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000798 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200799 aecInst->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000800 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000801 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200802 aecInst->timeForDelayChange = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000803 }
Alex Loiko890988c2017-08-31 10:25:48 +0200804 aecInst->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000805
Alex Loiko890988c2017-08-31 10:25:48 +0200806 if (aecInst->timeForDelayChange > 25) {
807 aecInst->knownDelay = WEBRTC_SPL_MAX((int)aecInst->filtDelay - 160, 0);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000808 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000809}
niklase@google.com470e71d2011-07-07 08:21:25 +0000810
Alex Loiko890988c2017-08-31 10:25:48 +0200811static void EstBufDelayExtended(Aec* aecInst) {
812 int reported_delay = aecInst->msInSndCardBuf * sampMsNb *
813 aecInst->rate_factor;
814 int current_delay = reported_delay - WebRtcAec_system_delay(aecInst->aec);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000815 int delay_difference = 0;
816
817 // Before we proceed with the delay estimate filtering we:
818 // 1) Compensate for the frame that will be read.
819 // 2) Compensate for drift resampling.
820 // 3) Compensate for non-causality if needed, since the estimated delay can't
821 // be negative.
822
823 // 1) Compensating for the frame(s) that will be read/processed.
Alex Loiko890988c2017-08-31 10:25:48 +0200824 current_delay += FRAME_LEN * aecInst->rate_factor;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000825
826 // 2) Account for resampling frame delay.
Alex Loiko890988c2017-08-31 10:25:48 +0200827 if (aecInst->skewMode == kAecTrue && aecInst->resample == kAecTrue) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000828 current_delay -= kResamplingDelay;
829 }
830
831 // 3) Compensate for non-causality, if needed, by flushing two blocks.
832 if (current_delay < PART_LEN) {
peaha421ddd2016-09-12 11:27:14 -0700833 current_delay +=
Alex Loiko890988c2017-08-31 10:25:48 +0200834 WebRtcAec_AdjustFarendBufferSizeAndSystemDelay(aecInst->aec, 2) *
835 PART_LEN;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000836 }
837
Alex Loiko890988c2017-08-31 10:25:48 +0200838 if (aecInst->filtDelay == -1) {
839 aecInst->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000840 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200841 aecInst->filtDelay = WEBRTC_SPL_MAX(
842 0, static_cast<int16_t>(0.95 * aecInst->filtDelay + 0.05 *
843 current_delay));
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000844 }
845
Alex Loiko890988c2017-08-31 10:25:48 +0200846 delay_difference = aecInst->filtDelay - aecInst->knownDelay;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000847 if (delay_difference > 384) {
Alex Loiko890988c2017-08-31 10:25:48 +0200848 if (aecInst->lastDelayDiff < 128) {
849 aecInst->timeForDelayChange = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000850 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200851 aecInst->timeForDelayChange++;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000852 }
Alex Loiko890988c2017-08-31 10:25:48 +0200853 } else if (delay_difference < 128 && aecInst->knownDelay > 0) {
854 if (aecInst->lastDelayDiff > 384) {
855 aecInst->timeForDelayChange = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000856 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200857 aecInst->timeForDelayChange++;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000858 }
859 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200860 aecInst->timeForDelayChange = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000861 }
Alex Loiko890988c2017-08-31 10:25:48 +0200862 aecInst->lastDelayDiff = delay_difference;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000863
Alex Loiko890988c2017-08-31 10:25:48 +0200864 if (aecInst->timeForDelayChange > 25) {
865 aecInst->knownDelay = WEBRTC_SPL_MAX((int)aecInst->filtDelay - 256, 0);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000866 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000867}
peah50e21bd2016-03-05 08:39:21 -0800868} // namespace webrtc