blob: fd1aec4058eafeba7ad8c6139efc083e936d61da [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"
niklase@google.com470e71d2011-07-07 08:21:25 +000027
peah50e21bd2016-03-05 08:39:21 -080028namespace webrtc {
29
kwiberg83ffe452016-08-29 14:46:07 -070030Aec::Aec() = default;
31Aec::~Aec() = default;
32
andrew@webrtc.org1760a172013-09-25 23:17:38 +000033// Measured delays [ms]
34// Device Chrome GTP
35// MacBook Air 10
36// MacBook Retina 10 100
37// MacPro 30?
38//
39// Win7 Desktop 70 80?
40// Win7 T430s 110
41// Win8 T420s 70
42//
43// Daisy 50
44// Pixel (w/ preproc?) 240
45// Pixel (w/o preproc?) 110 110
46
47// The extended filter mode gives us the flexibility to ignore the system's
48// reported delays. We do this for platforms which we believe provide results
49// which are incompatible with the AEC's expectations. Based on measurements
50// (some provided above) we set a conservative (i.e. lower than measured)
51// fixed delay.
52//
53// WEBRTC_UNTRUSTED_DELAY will only have an impact when |extended_filter_mode|
54// is enabled. See the note along with |DelayCorrection| in
55// echo_cancellation_impl.h for more details on the mode.
56//
57// Justification:
58// Chromium/Mac: Here, the true latency is so low (~10-20 ms), that it plays
59// havoc with the AEC's buffering. To avoid this, we set a fixed delay of 20 ms
60// and then compensate by rewinding by 10 ms (in wideband) through
61// kDelayDiffOffsetSamples. This trick does not seem to work for larger rewind
62// values, but fortunately this is sufficient.
63//
64// Chromium/Linux(ChromeOS): The values we get on this platform don't correspond
65// well to reality. The variance doesn't match the AEC's buffer changes, and the
66// bulk values tend to be too low. However, the range across different hardware
67// appears to be too large to choose a single value.
68//
69// GTP/Linux(ChromeOS): TBD, but for the moment we will trust the values.
70#if defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_MAC)
71#define WEBRTC_UNTRUSTED_DELAY
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +000072#endif
andrew@webrtc.orgacb00502013-10-04 16:59:17 +000073
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +000074#if defined(WEBRTC_UNTRUSTED_DELAY) && defined(WEBRTC_MAC)
andrew@webrtc.orgacb00502013-10-04 16:59:17 +000075static const int kDelayDiffOffsetSamples = -160;
76#else
77// Not enabled for now.
78static const int kDelayDiffOffsetSamples = 0;
79#endif
andrew@webrtc.org1760a172013-09-25 23:17:38 +000080
81#if defined(WEBRTC_MAC)
82static const int kFixedDelayMs = 20;
andrew@webrtc.org1760a172013-09-25 23:17:38 +000083#else
andrew@webrtc.org1760a172013-09-25 23:17:38 +000084static const int kFixedDelayMs = 50;
andrew@webrtc.org1760a172013-09-25 23:17:38 +000085#endif
andrew@webrtc.orgc2e471d2013-10-15 02:11:21 +000086#if !defined(WEBRTC_UNTRUSTED_DELAY)
andrew@webrtc.org1760a172013-09-25 23:17:38 +000087static const int kMinTrustedDelayMs = 20;
andrew@webrtc.orgc2e471d2013-10-15 02:11:21 +000088#endif
andrew@webrtc.org1760a172013-09-25 23:17:38 +000089static const int kMaxTrustedDelayMs = 500;
90
niklase@google.com470e71d2011-07-07 08:21:25 +000091// Maximum length of resampled signal. Must be an integer multiple of frames
92// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
93// The factor of 2 handles wb, and the + 1 is as a safety margin
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000094// TODO(bjornv): Replace with kResamplerBufferSize
niklase@google.com470e71d2011-07-07 08:21:25 +000095#define MAX_RESAMP_LEN (5 * FRAME_LEN)
96
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000097static const int kMaxBufSizeStart = 62; // In partitions
andrew@webrtc.org13b2d462013-10-08 23:41:42 +000098static const int sampMsNb = 8; // samples per ms in nb
niklase@google.com470e71d2011-07-07 08:21:25 +000099static const int initCheck = 42;
100
peahb46083e2016-05-03 07:01:18 -0700101int Aec::instance_count = 0;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000102
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000103// Estimates delay to set the position of the far-end buffer read pointer
niklase@google.com470e71d2011-07-07 08:21:25 +0000104// (controlled by knownDelay)
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000105static void EstBufDelayNormal(Aec* aecInst);
106static void EstBufDelayExtended(Aec* aecInst);
Alex Loiko890988c2017-08-31 10:25:48 +0200107static int ProcessNormal(Aec* aecInst,
108 const float* const* nearend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700109 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000110 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700111 size_t num_samples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000112 int16_t reported_delay_ms,
113 int32_t skew);
Alex Loiko890988c2017-08-31 10:25:48 +0200114static void ProcessExtended(Aec* aecInst,
115 const float* const* nearend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700116 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000117 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700118 size_t num_samples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000119 int16_t reported_delay_ms,
120 int32_t skew);
niklase@google.com470e71d2011-07-07 08:21:25 +0000121
Bjorn Volcker9345e862015-06-10 21:43:36 +0200122void* WebRtcAec_Create() {
peah3f08dc62016-05-05 03:03:55 -0700123 Aec* aecpc = new Aec();
Bjorn Volcker9345e862015-06-10 21:43:36 +0200124
125 if (!aecpc) {
126 return NULL;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000127 }
peah3f08dc62016-05-05 03:03:55 -0700128 aecpc->data_dumper.reset(new ApmDataDumper(aecpc->instance_count));
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
peahb46083e2016-05-03 07:01:18 -0700130 aecpc->aec = WebRtcAec_CreateAec(aecpc->instance_count);
Bjorn Volcker9345e862015-06-10 21:43:36 +0200131 if (!aecpc->aec) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000132 WebRtcAec_Free(aecpc);
Bjorn Volcker9345e862015-06-10 21:43:36 +0200133 return NULL;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000134 }
Bjorn Volcker9345e862015-06-10 21:43:36 +0200135 aecpc->resampler = WebRtcAec_CreateResampler();
136 if (!aecpc->resampler) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000137 WebRtcAec_Free(aecpc);
Bjorn Volcker9345e862015-06-10 21:43:36 +0200138 return NULL;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000139 }
140 // Create far-end pre-buffer. The buffer size has to be large enough for
141 // largest possible drift compensation (kResamplerBufferSize) + "almost" an
142 // FFT buffer (PART_LEN2 - 1).
143 aecpc->far_pre_buf =
144 WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize, sizeof(float));
145 if (!aecpc->far_pre_buf) {
146 WebRtcAec_Free(aecpc);
Bjorn Volcker9345e862015-06-10 21:43:36 +0200147 return NULL;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000148 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000149
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000150 aecpc->initFlag = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000151
peahb46083e2016-05-03 07:01:18 -0700152 aecpc->instance_count++;
Bjorn Volcker9345e862015-06-10 21:43:36 +0200153 return aecpc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000154}
155
Bjorn Volckerf6a99e62015-04-10 07:56:57 +0200156void WebRtcAec_Free(void* aecInst) {
peah8df5d4f2016-02-23 14:34:59 -0800157 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000158
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000159 if (aecpc == NULL) {
Bjorn Volckerf6a99e62015-04-10 07:56:57 +0200160 return;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000161 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000162
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000163 WebRtc_FreeBuffer(aecpc->far_pre_buf);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000164
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000165 WebRtcAec_FreeAec(aecpc->aec);
166 WebRtcAec_FreeResampler(aecpc->resampler);
peah3f08dc62016-05-05 03:03:55 -0700167 delete aecpc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000168}
169
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000170int32_t WebRtcAec_Init(void* aecInst, int32_t sampFreq, int32_t scSampFreq) {
peah8df5d4f2016-02-23 14:34:59 -0800171 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
peah3f08dc62016-05-05 03:03:55 -0700172 aecpc->data_dumper->InitiateNewSetOfRecordings();
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000173 AecConfig aecConfig;
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
peahff63ed22016-01-29 07:46:13 -0800175 if (sampFreq != 8000 && sampFreq != 16000 && sampFreq != 32000 &&
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000176 sampFreq != 48000) {
peahc12be392015-11-09 23:53:50 -0800177 return AEC_BAD_PARAMETER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000178 }
179 aecpc->sampFreq = sampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000181 if (scSampFreq < 1 || scSampFreq > 96000) {
peahc12be392015-11-09 23:53:50 -0800182 return AEC_BAD_PARAMETER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000183 }
184 aecpc->scSampFreq = scSampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000185
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000186 // Initialize echo canceller core
187 if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) {
peahc12be392015-11-09 23:53:50 -0800188 return AEC_UNSPECIFIED_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000189 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000190
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000191 if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) {
peahc12be392015-11-09 23:53:50 -0800192 return AEC_UNSPECIFIED_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000193 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000194
andrew@webrtc.org6b630152015-01-15 00:09:53 +0000195 WebRtc_InitBuffer(aecpc->far_pre_buf);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000196 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap.
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000197
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000198 aecpc->initFlag = initCheck; // indicates that initialization has been done
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000200 if (aecpc->sampFreq == 32000 || aecpc->sampFreq == 48000) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000201 aecpc->splitSampFreq = 16000;
202 } else {
203 aecpc->splitSampFreq = sampFreq;
204 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000206 aecpc->delayCtr = 0;
207 aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq;
208 // Sampling frequency multiplier (SWB is processed as 160 frame size).
209 aecpc->rate_factor = aecpc->splitSampFreq / 8000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000211 aecpc->sum = 0;
212 aecpc->counter = 0;
213 aecpc->checkBuffSize = 1;
214 aecpc->firstVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215
Bjorn Volcker71012692015-06-18 11:04:56 +0200216 // We skip the startup_phase completely (setting to 0) if DA-AEC is enabled,
217 // but not extended_filter mode.
218 aecpc->startup_phase = WebRtcAec_extended_filter_enabled(aecpc->aec) ||
peahff63ed22016-01-29 07:46:13 -0800219 !WebRtcAec_delay_agnostic_enabled(aecpc->aec);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000220 aecpc->bufSizeStart = 0;
221 aecpc->checkBufSizeCtr = 0;
222 aecpc->msInSndCardBuf = 0;
223 aecpc->filtDelay = -1; // -1 indicates an initialized state.
224 aecpc->timeForDelayChange = 0;
225 aecpc->knownDelay = 0;
226 aecpc->lastDelayDiff = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000228 aecpc->skewFrCtr = 0;
229 aecpc->resample = kAecFalse;
230 aecpc->highSkewCtr = 0;
231 aecpc->skew = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000233 aecpc->farend_started = 0;
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000234
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000235 // Default settings.
236 aecConfig.nlpMode = kAecNlpModerate;
237 aecConfig.skewMode = kAecFalse;
238 aecConfig.metricsMode = kAecFalse;
239 aecConfig.delay_logging = kAecFalse;
niklase@google.com470e71d2011-07-07 08:21:25 +0000240
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000241 if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
peahc12be392015-11-09 23:53:50 -0800242 return AEC_UNSPECIFIED_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000243 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000244
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000245 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000246}
247
peahc12be392015-11-09 23:53:50 -0800248// Returns any error that is caused when buffering the
249// far-end signal.
250int32_t WebRtcAec_GetBufferFarendError(void* aecInst,
251 const float* farend,
252 size_t nrOfSamples) {
peah8df5d4f2016-02-23 14:34:59 -0800253 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
peahc12be392015-11-09 23:53:50 -0800254
255 if (!farend)
256 return AEC_NULL_POINTER_ERROR;
257
258 if (aecpc->initFlag != initCheck)
259 return AEC_UNINITIALIZED_ERROR;
260
261 // number of samples == 160 for SWB input
262 if (nrOfSamples != 80 && nrOfSamples != 160)
263 return AEC_BAD_PARAMETER_ERROR;
264
265 return 0;
266}
267
niklase@google.com470e71d2011-07-07 08:21:25 +0000268// only buffer L band for farend
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000269int32_t WebRtcAec_BufferFarend(void* aecInst,
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000270 const float* farend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700271 size_t nrOfSamples) {
peah8df5d4f2016-02-23 14:34:59 -0800272 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700273 size_t newNrOfSamples = nrOfSamples;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000274 float new_farend[MAX_RESAMP_LEN];
275 const float* farend_ptr = farend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000276
peahc12be392015-11-09 23:53:50 -0800277 // Get any error caused by buffering the farend signal.
peahff63ed22016-01-29 07:46:13 -0800278 int32_t error_code =
279 WebRtcAec_GetBufferFarendError(aecInst, farend, nrOfSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +0000280
peahc12be392015-11-09 23:53:50 -0800281 if (error_code != 0)
282 return error_code;
niklase@google.com470e71d2011-07-07 08:21:25 +0000283
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000284 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
285 // Resample and get a new number of samples
peahff63ed22016-01-29 07:46:13 -0800286 WebRtcAec_ResampleLinear(aecpc->resampler, farend, nrOfSamples, aecpc->skew,
287 new_farend, &newNrOfSamples);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000288 farend_ptr = new_farend;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000289 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000290
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000291 aecpc->farend_started = 1;
peah8df5d4f2016-02-23 14:34:59 -0800292 WebRtcAec_SetSystemDelay(aecpc->aec, WebRtcAec_system_delay(aecpc->aec) +
Yves Gerey665174f2018-06-19 15:03:05 +0200293 static_cast<int>(newNrOfSamples));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000294
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000295 // Write the time-domain data to |far_pre_buf|.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700296 WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_ptr, newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000297
peaha421ddd2016-09-12 11:27:14 -0700298 // TODO(minyue): reduce to |PART_LEN| samples for each buffering.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000299 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
300 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000301 {
andrew@webrtc.orge65d9d92015-01-21 22:05:12 +0000302 float* ptmp = NULL;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000303 float tmp[PART_LEN2];
Yves Gerey665174f2018-06-19 15:03:05 +0200304 WebRtc_ReadBuffer(aecpc->far_pre_buf, reinterpret_cast<void**>(&ptmp),
305 tmp, PART_LEN2);
peaha421ddd2016-09-12 11:27:14 -0700306 WebRtcAec_BufferFarendBlock(aecpc->aec, &ptmp[PART_LEN]);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000307 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000308
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000309 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
310 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000311 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000312
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000313 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000314}
315
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000316int32_t WebRtcAec_Process(void* aecInst,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000317 const float* const* nearend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700318 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000319 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700320 size_t nrOfSamples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000321 int16_t msInSndCardBuf,
322 int32_t skew) {
peah8df5d4f2016-02-23 14:34:59 -0800323 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000324 int32_t retVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000325
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000326 if (out == NULL) {
peahc12be392015-11-09 23:53:50 -0800327 return AEC_NULL_POINTER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000328 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000329
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000330 if (aecpc->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800331 return AEC_UNINITIALIZED_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000332 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000333
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000334 // number of samples == 160 for SWB input
335 if (nrOfSamples != 80 && nrOfSamples != 160) {
peahc12be392015-11-09 23:53:50 -0800336 return AEC_BAD_PARAMETER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000337 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000338
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000339 if (msInSndCardBuf < 0) {
340 msInSndCardBuf = 0;
peahc12be392015-11-09 23:53:50 -0800341 retVal = AEC_BAD_PARAMETER_WARNING;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000342 } else if (msInSndCardBuf > kMaxTrustedDelayMs) {
343 // The clamping is now done in ProcessExtended/Normal().
peahc12be392015-11-09 23:53:50 -0800344 retVal = AEC_BAD_PARAMETER_WARNING;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000345 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000346
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000347 // This returns the value of aec->extended_filter_enabled.
Henrik Lundin441f6342015-06-09 16:03:13 +0200348 if (WebRtcAec_extended_filter_enabled(aecpc->aec)) {
peahff63ed22016-01-29 07:46:13 -0800349 ProcessExtended(aecpc, nearend, num_bands, out, nrOfSamples, msInSndCardBuf,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000350 skew);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000351 } else {
peahff63ed22016-01-29 07:46:13 -0800352 retVal = ProcessNormal(aecpc, nearend, num_bands, out, nrOfSamples,
353 msInSndCardBuf, skew);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000354 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000355
peah3f08dc62016-05-05 03:03:55 -0700356 int far_buf_size_samples = WebRtcAec_system_delay(aecpc->aec);
357 aecpc->data_dumper->DumpRaw("aec_system_delay", 1, &far_buf_size_samples);
358 aecpc->data_dumper->DumpRaw("aec_known_delay", 1, &aecpc->knownDelay);
niklase@google.com470e71d2011-07-07 08:21:25 +0000359
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000360 return retVal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000361}
362
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000363int WebRtcAec_set_config(void* handle, AecConfig config) {
peah8df5d4f2016-02-23 14:34:59 -0800364 Aec* self = reinterpret_cast<Aec*>(handle);
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000365 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800366 return AEC_UNINITIALIZED_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000367 }
368
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000369 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
peahc12be392015-11-09 23:53:50 -0800370 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000371 }
372 self->skewMode = config.skewMode;
373
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000374 if (config.nlpMode != kAecNlpConservative &&
375 config.nlpMode != kAecNlpModerate &&
376 config.nlpMode != kAecNlpAggressive) {
peahc12be392015-11-09 23:53:50 -0800377 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000378 }
379
380 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
peahc12be392015-11-09 23:53:50 -0800381 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000382 }
383
384 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
peahc12be392015-11-09 23:53:50 -0800385 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000386 }
387
peahff63ed22016-01-29 07:46:13 -0800388 WebRtcAec_SetConfigCore(self->aec, config.nlpMode, config.metricsMode,
389 config.delay_logging);
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000390 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000391}
392
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000393int WebRtcAec_get_echo_status(void* handle, int* status) {
peah8df5d4f2016-02-23 14:34:59 -0800394 Aec* self = reinterpret_cast<Aec*>(handle);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000395 if (status == NULL) {
peahc12be392015-11-09 23:53:50 -0800396 return AEC_NULL_POINTER_ERROR;
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000397 }
398 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800399 return AEC_UNINITIALIZED_ERROR;
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000400 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000401
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000402 *status = WebRtcAec_echo_state(self->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000403
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000404 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000405}
406
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000407int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) {
408 const float kUpWeight = 0.7f;
409 float dtmp;
410 int stmp;
peah8df5d4f2016-02-23 14:34:59 -0800411 Aec* self = reinterpret_cast<Aec*>(handle);
bjornv@webrtc.orgcea70f42013-02-19 21:03:10 +0000412 Stats erl;
413 Stats erle;
414 Stats a_nlp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000415
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000416 if (handle == NULL) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000417 return -1;
418 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000419 if (metrics == NULL) {
peahc12be392015-11-09 23:53:50 -0800420 return AEC_NULL_POINTER_ERROR;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000421 }
422 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800423 return AEC_UNINITIALIZED_ERROR;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000424 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000425
minyue50453372016-04-07 06:36:43 -0700426 WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp,
427 &metrics->divergent_filter_fraction);
niklase@google.com470e71d2011-07-07 08:21:25 +0000428
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000429 // ERL
peah8df5d4f2016-02-23 14:34:59 -0800430 metrics->erl.instant = static_cast<int>(erl.instant);
niklase@google.com470e71d2011-07-07 08:21:25 +0000431
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000432 if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000433 // Use a mix between regular average and upper part average.
434 dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average;
peah8df5d4f2016-02-23 14:34:59 -0800435 metrics->erl.average = static_cast<int>(dtmp);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000436 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000437 metrics->erl.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000438 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
peah8df5d4f2016-02-23 14:34:59 -0800440 metrics->erl.max = static_cast<int>(erl.max);
niklase@google.com470e71d2011-07-07 08:21:25 +0000441
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000442 if (erl.min < (kOffsetLevel * (-1))) {
peah8df5d4f2016-02-23 14:34:59 -0800443 metrics->erl.min = static_cast<int>(erl.min);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000444 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000445 metrics->erl.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000446 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000447
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000448 // ERLE
peah8df5d4f2016-02-23 14:34:59 -0800449 metrics->erle.instant = static_cast<int>(erle.instant);
niklase@google.com470e71d2011-07-07 08:21:25 +0000450
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000451 if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000452 // Use a mix between regular average and upper part average.
453 dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average;
peah8df5d4f2016-02-23 14:34:59 -0800454 metrics->erle.average = static_cast<int>(dtmp);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000455 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000456 metrics->erle.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000457 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000458
peah8df5d4f2016-02-23 14:34:59 -0800459 metrics->erle.max = static_cast<int>(erle.max);
niklase@google.com470e71d2011-07-07 08:21:25 +0000460
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000461 if (erle.min < (kOffsetLevel * (-1))) {
peah8df5d4f2016-02-23 14:34:59 -0800462 metrics->erle.min = static_cast<int>(erle.min);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000463 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000464 metrics->erle.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000465 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000466
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000467 // RERL
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000468 if ((metrics->erl.average > kOffsetLevel) &&
469 (metrics->erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000470 stmp = metrics->erl.average + metrics->erle.average;
471 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000472 stmp = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000473 }
474 metrics->rerl.average = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000475
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000476 // No other statistics needed, but returned for completeness.
477 metrics->rerl.instant = stmp;
478 metrics->rerl.max = stmp;
479 metrics->rerl.min = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000480
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000481 // A_NLP
peah8df5d4f2016-02-23 14:34:59 -0800482 metrics->aNlp.instant = static_cast<int>(a_nlp.instant);
niklase@google.com470e71d2011-07-07 08:21:25 +0000483
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000484 if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000485 // Use a mix between regular average and upper part average.
486 dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average;
peah8df5d4f2016-02-23 14:34:59 -0800487 metrics->aNlp.average = static_cast<int>(dtmp);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000488 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000489 metrics->aNlp.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000490 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000491
peah8df5d4f2016-02-23 14:34:59 -0800492 metrics->aNlp.max = static_cast<int>(a_nlp.max);
niklase@google.com470e71d2011-07-07 08:21:25 +0000493
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000494 if (a_nlp.min < (kOffsetLevel * (-1))) {
peah8df5d4f2016-02-23 14:34:59 -0800495 metrics->aNlp.min = static_cast<int>(a_nlp.min);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000496 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000497 metrics->aNlp.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000498 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000499
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000500 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000501}
502
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000503int WebRtcAec_GetDelayMetrics(void* handle,
504 int* median,
505 int* std,
506 float* fraction_poor_delays) {
peah8df5d4f2016-02-23 14:34:59 -0800507 Aec* self = reinterpret_cast<Aec*>(handle);
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000508 if (median == NULL) {
peahc12be392015-11-09 23:53:50 -0800509 return AEC_NULL_POINTER_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000510 }
511 if (std == NULL) {
peahc12be392015-11-09 23:53:50 -0800512 return AEC_NULL_POINTER_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000513 }
514 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800515 return AEC_UNINITIALIZED_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000516 }
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000517 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std,
peahff63ed22016-01-29 07:46:13 -0800518 fraction_poor_delays) == -1) {
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000519 // Logging disabled.
peahc12be392015-11-09 23:53:50 -0800520 return AEC_UNSUPPORTED_FUNCTION_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000521 }
522
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000523 return 0;
524}
525
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000526AecCore* WebRtcAec_aec_core(void* handle) {
527 if (!handle) {
528 return NULL;
529 }
peah8df5d4f2016-02-23 14:34:59 -0800530 return reinterpret_cast<Aec*>(handle)->aec;
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000531}
532
Alex Loiko890988c2017-08-31 10:25:48 +0200533static int ProcessNormal(Aec* aecInst,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000534 const float* const* nearend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700535 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000536 float* const* out,
Alex Loiko890988c2017-08-31 10:25:48 +0200537 size_t num_samples,
538 int16_t reported_delay_ms,
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000539 int32_t skew) {
540 int retVal = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700541 size_t i;
542 size_t nBlocks10ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000543 // Limit resampling to doubling/halving of signal
544 const float minSkewEst = -0.5f;
545 const float maxSkewEst = 1.0f;
546
Yves Gerey665174f2018-06-19 15:03:05 +0200547 reported_delay_ms = reported_delay_ms > kMaxTrustedDelayMs
548 ? kMaxTrustedDelayMs
549 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000550 // TODO(andrew): we need to investigate if this +10 is really wanted.
Alex Loiko890988c2017-08-31 10:25:48 +0200551 reported_delay_ms += 10;
552 aecInst->msInSndCardBuf = reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000553
Alex Loiko890988c2017-08-31 10:25:48 +0200554 if (aecInst->skewMode == kAecTrue) {
555 if (aecInst->skewFrCtr < 25) {
556 aecInst->skewFrCtr++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000557 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200558 retVal = WebRtcAec_GetSkew(aecInst->resampler, skew, &aecInst->skew);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000559 if (retVal == -1) {
Alex Loiko890988c2017-08-31 10:25:48 +0200560 aecInst->skew = 0;
peahc12be392015-11-09 23:53:50 -0800561 retVal = AEC_BAD_PARAMETER_WARNING;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000562 }
563
Alex Loiko890988c2017-08-31 10:25:48 +0200564 aecInst->skew /= aecInst->sampFactor * num_samples;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000565
Alex Loiko890988c2017-08-31 10:25:48 +0200566 if (aecInst->skew < 1.0e-3 && aecInst->skew > -1.0e-3) {
567 aecInst->resample = kAecFalse;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000568 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200569 aecInst->resample = kAecTrue;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000570 }
571
Alex Loiko890988c2017-08-31 10:25:48 +0200572 if (aecInst->skew < minSkewEst) {
573 aecInst->skew = minSkewEst;
574 } else if (aecInst->skew > maxSkewEst) {
575 aecInst->skew = maxSkewEst;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000576 }
577
Alex Loiko890988c2017-08-31 10:25:48 +0200578 aecInst->data_dumper->DumpRaw("aec_skew", 1, &aecInst->skew);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000579 }
580 }
581
Alex Loiko890988c2017-08-31 10:25:48 +0200582 nBlocks10ms = num_samples / (FRAME_LEN * aecInst->rate_factor);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000583
Alex Loiko890988c2017-08-31 10:25:48 +0200584 if (aecInst->startup_phase) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000585 for (i = 0; i < num_bands; ++i) {
586 // Only needed if they don't already point to the same place.
587 if (nearend[i] != out[i]) {
Alex Loiko890988c2017-08-31 10:25:48 +0200588 memcpy(out[i], nearend[i], sizeof(nearend[i][0]) * num_samples);
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000589 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000590 }
591
592 // The AEC is in the start up mode
593 // AEC is disabled until the system delay is OK
594
595 // Mechanism to ensure that the system delay is reasonably stable.
Alex Loiko890988c2017-08-31 10:25:48 +0200596 if (aecInst->checkBuffSize) {
597 aecInst->checkBufSizeCtr++;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000598 // Before we fill up the far-end buffer we require the system delay
599 // to be stable (+/-8 ms) compared to the first value. This
600 // comparison is made during the following 6 consecutive 10 ms
601 // blocks. If it seems to be stable then we start to fill up the
602 // far-end buffer.
Alex Loiko890988c2017-08-31 10:25:48 +0200603 if (aecInst->counter == 0) {
604 aecInst->firstVal = aecInst->msInSndCardBuf;
605 aecInst->sum = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000606 }
607
Alex Loiko890988c2017-08-31 10:25:48 +0200608 if (abs(aecInst->firstVal - aecInst->msInSndCardBuf) <
609 WEBRTC_SPL_MAX(0.2 * aecInst->msInSndCardBuf, sampMsNb)) {
610 aecInst->sum += aecInst->msInSndCardBuf;
611 aecInst->counter++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000612 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200613 aecInst->counter = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000614 }
615
Alex Loiko890988c2017-08-31 10:25:48 +0200616 if (aecInst->counter * nBlocks10ms >= 6) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000617 // The far-end buffer size is determined in partitions of
618 // PART_LEN samples. Use 75% of the average value of the system
619 // delay as buffer size to start with.
Alex Loiko890988c2017-08-31 10:25:48 +0200620 aecInst->bufSizeStart =
621 WEBRTC_SPL_MIN((3 * aecInst->sum * aecInst->rate_factor * 8) /
622 (4 * aecInst->counter * PART_LEN),
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000623 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000624 // Buffer size has now been determined.
Alex Loiko890988c2017-08-31 10:25:48 +0200625 aecInst->checkBuffSize = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000626 }
627
Alex Loiko890988c2017-08-31 10:25:48 +0200628 if (aecInst->checkBufSizeCtr * nBlocks10ms > 50) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000629 // For really bad systems, don't disable the echo canceller for
630 // more than 0.5 sec.
Alex Loiko890988c2017-08-31 10:25:48 +0200631 aecInst->bufSizeStart = WEBRTC_SPL_MIN(
632 (aecInst->msInSndCardBuf * aecInst->rate_factor * 3) / 40,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000633 kMaxBufSizeStart);
Alex Loiko890988c2017-08-31 10:25:48 +0200634 aecInst->checkBuffSize = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000635 }
636 }
637
638 // If |checkBuffSize| changed in the if-statement above.
Alex Loiko890988c2017-08-31 10:25:48 +0200639 if (!aecInst->checkBuffSize) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000640 // The system delay is now reasonably stable (or has been unstable
641 // for too long). When the far-end buffer is filled with
642 // approximately the same amount of data as reported by the system
643 // we end the startup phase.
Yves Gerey665174f2018-06-19 15:03:05 +0200644 int overhead_elements = WebRtcAec_system_delay(aecInst->aec) / PART_LEN -
645 aecInst->bufSizeStart;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000646 if (overhead_elements == 0) {
647 // Enable the AEC
Alex Loiko890988c2017-08-31 10:25:48 +0200648 aecInst->startup_phase = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000649 } else if (overhead_elements > 0) {
650 // TODO(bjornv): Do we need a check on how much we actually
651 // moved the read pointer? It should always be possible to move
652 // the pointer |overhead_elements| since we have only added data
653 // to the buffer and no delay compensation nor AEC processing
654 // has been done.
Alex Loiko890988c2017-08-31 10:25:48 +0200655 WebRtcAec_AdjustFarendBufferSizeAndSystemDelay(aecInst->aec,
peaha421ddd2016-09-12 11:27:14 -0700656 overhead_elements);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000657
658 // Enable the AEC
Alex Loiko890988c2017-08-31 10:25:48 +0200659 aecInst->startup_phase = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000660 }
661 }
662 } else {
663 // AEC is enabled.
Alex Loiko890988c2017-08-31 10:25:48 +0200664 EstBufDelayNormal(aecInst);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000665
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000666 // Call the AEC.
667 // TODO(bjornv): Re-structure such that we don't have to pass
Alex Loiko890988c2017-08-31 10:25:48 +0200668 // |aecInst->knownDelay| as input. Change name to something like
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000669 // |system_buffer_diff|.
Alex Loiko890988c2017-08-31 10:25:48 +0200670 WebRtcAec_ProcessFrames(aecInst->aec, nearend, num_bands, num_samples,
671 aecInst->knownDelay, out);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000672 }
673
674 return retVal;
675}
676
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000677static void ProcessExtended(Aec* self,
Artem Titove62f6002018-03-19 15:40:00 +0100678 const float* const* nearend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700679 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000680 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700681 size_t num_samples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000682 int16_t reported_delay_ms,
683 int32_t skew) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700684 size_t i;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000685 const int delay_diff_offset = kDelayDiffOffsetSamples;
peah906f4032016-09-08 09:49:42 -0700686 RTC_DCHECK(num_samples == 80 || num_samples == 160);
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +0000687#if defined(WEBRTC_UNTRUSTED_DELAY)
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000688 reported_delay_ms = kFixedDelayMs;
689#else
690 // This is the usual mode where we trust the reported system delay values.
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000691 // Due to the longer filter, we no longer add 10 ms to the reported delay
692 // to reduce chance of non-causality. Instead we apply a minimum here to avoid
693 // issues with the read pointer jumping around needlessly.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000694 reported_delay_ms = reported_delay_ms < kMinTrustedDelayMs
695 ? kMinTrustedDelayMs
696 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000697 // If the reported delay appears to be bogus, we attempt to recover by using
698 // the measured fixed delay values. We use >= here because higher layers
699 // may already clamp to this maximum value, and we would otherwise not
700 // detect it here.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000701 reported_delay_ms = reported_delay_ms >= kMaxTrustedDelayMs
702 ? kFixedDelayMs
703 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000704#endif
705 self->msInSndCardBuf = reported_delay_ms;
706
707 if (!self->farend_started) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000708 for (i = 0; i < num_bands; ++i) {
709 // Only needed if they don't already point to the same place.
Artem Titove62f6002018-03-19 15:40:00 +0100710 if (nearend[i] != out[i]) {
711 memcpy(out[i], nearend[i], sizeof(nearend[i][0]) * num_samples);
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000712 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000713 }
714 return;
715 }
716 if (self->startup_phase) {
717 // In the extended mode, there isn't a startup "phase", just a special
718 // action on the first frame. In the trusted delay case, we'll take the
719 // current reported delay, unless it's less then our conservative
720 // measurement.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000721 int startup_size_ms =
722 reported_delay_ms < kFixedDelayMs ? kFixedDelayMs : reported_delay_ms;
Bjorn Volcker71012692015-06-18 11:04:56 +0200723#if defined(WEBRTC_ANDROID)
Bjorn Volcker1ff218f2015-05-06 12:08:38 +0200724 int target_delay = startup_size_ms * self->rate_factor * 8;
Bjorn Volcker71012692015-06-18 11:04:56 +0200725#else
Bjorn Volcker1ff218f2015-05-06 12:08:38 +0200726 // To avoid putting the AEC in a non-causal state we're being slightly
727 // conservative and scale by 2. On Android we use a fixed delay and
728 // therefore there is no need to scale the target_delay.
Bjorn Volcker71012692015-06-18 11:04:56 +0200729 int target_delay = startup_size_ms * self->rate_factor * 8 / 2;
Bjorn Volcker1ff218f2015-05-06 12:08:38 +0200730#endif
731 int overhead_elements =
732 (WebRtcAec_system_delay(self->aec) - target_delay) / PART_LEN;
peaha421ddd2016-09-12 11:27:14 -0700733 WebRtcAec_AdjustFarendBufferSizeAndSystemDelay(self->aec,
734 overhead_elements);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000735 self->startup_phase = 0;
736 }
737
Bjorn Volcker71012692015-06-18 11:04:56 +0200738 EstBufDelayExtended(self);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000739
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000740 {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000741 // |delay_diff_offset| gives us the option to manually rewind the delay on
742 // very low delay platforms which can't be expressed purely through
743 // |reported_delay_ms|.
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000744 const int adjusted_known_delay =
745 WEBRTC_SPL_MAX(0, self->knownDelay + delay_diff_offset);
746
Artem Titove62f6002018-03-19 15:40:00 +0100747 WebRtcAec_ProcessFrames(self->aec, nearend, num_bands, num_samples,
peahff63ed22016-01-29 07:46:13 -0800748 adjusted_known_delay, out);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000749 }
750}
751
Alex Loiko890988c2017-08-31 10:25:48 +0200752static void EstBufDelayNormal(Aec* aecInst) {
753 int nSampSndCard = aecInst->msInSndCardBuf * sampMsNb * aecInst->rate_factor;
754 int current_delay = nSampSndCard - WebRtcAec_system_delay(aecInst->aec);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000755 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000756
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000757 // Before we proceed with the delay estimate filtering we:
758 // 1) Compensate for the frame that will be read.
759 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000760 // 3) Compensate for non-causality if needed, since the estimated delay can't
761 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000762
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000763 // 1) Compensating for the frame(s) that will be read/processed.
Alex Loiko890988c2017-08-31 10:25:48 +0200764 current_delay += FRAME_LEN * aecInst->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000765
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000766 // 2) Account for resampling frame delay.
Alex Loiko890988c2017-08-31 10:25:48 +0200767 if (aecInst->skewMode == kAecTrue && aecInst->resample == kAecTrue) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000768 current_delay -= kResamplingDelay;
769 }
770
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000771 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000772 if (current_delay < PART_LEN) {
peaha421ddd2016-09-12 11:27:14 -0700773 current_delay +=
Alex Loiko890988c2017-08-31 10:25:48 +0200774 WebRtcAec_AdjustFarendBufferSizeAndSystemDelay(aecInst->aec, 1) *
peaha421ddd2016-09-12 11:27:14 -0700775 PART_LEN;
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000776 }
777
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000778 // We use -1 to signal an initialized state in the "extended" implementation;
779 // compensate for that.
Alex Loiko890988c2017-08-31 10:25:48 +0200780 aecInst->filtDelay = aecInst->filtDelay < 0 ? 0 : aecInst->filtDelay;
Yves Gerey665174f2018-06-19 15:03:05 +0200781 aecInst->filtDelay = WEBRTC_SPL_MAX(
782 0, static_cast<int16_t>(0.8 * aecInst->filtDelay + 0.2 * current_delay));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000783
Alex Loiko890988c2017-08-31 10:25:48 +0200784 delay_difference = aecInst->filtDelay - aecInst->knownDelay;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000785 if (delay_difference > 224) {
Alex Loiko890988c2017-08-31 10:25:48 +0200786 if (aecInst->lastDelayDiff < 96) {
787 aecInst->timeForDelayChange = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000788 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200789 aecInst->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000790 }
Alex Loiko890988c2017-08-31 10:25:48 +0200791 } else if (delay_difference < 96 && aecInst->knownDelay > 0) {
792 if (aecInst->lastDelayDiff > 224) {
793 aecInst->timeForDelayChange = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000794 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200795 aecInst->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000796 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000797 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200798 aecInst->timeForDelayChange = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000799 }
Alex Loiko890988c2017-08-31 10:25:48 +0200800 aecInst->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000801
Alex Loiko890988c2017-08-31 10:25:48 +0200802 if (aecInst->timeForDelayChange > 25) {
803 aecInst->knownDelay = WEBRTC_SPL_MAX((int)aecInst->filtDelay - 160, 0);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000804 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000805}
niklase@google.com470e71d2011-07-07 08:21:25 +0000806
Alex Loiko890988c2017-08-31 10:25:48 +0200807static void EstBufDelayExtended(Aec* aecInst) {
Yves Gerey665174f2018-06-19 15:03:05 +0200808 int reported_delay =
809 aecInst->msInSndCardBuf * sampMsNb * aecInst->rate_factor;
Alex Loiko890988c2017-08-31 10:25:48 +0200810 int current_delay = reported_delay - WebRtcAec_system_delay(aecInst->aec);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000811 int delay_difference = 0;
812
813 // Before we proceed with the delay estimate filtering we:
814 // 1) Compensate for the frame that will be read.
815 // 2) Compensate for drift resampling.
816 // 3) Compensate for non-causality if needed, since the estimated delay can't
817 // be negative.
818
819 // 1) Compensating for the frame(s) that will be read/processed.
Alex Loiko890988c2017-08-31 10:25:48 +0200820 current_delay += FRAME_LEN * aecInst->rate_factor;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000821
822 // 2) Account for resampling frame delay.
Alex Loiko890988c2017-08-31 10:25:48 +0200823 if (aecInst->skewMode == kAecTrue && aecInst->resample == kAecTrue) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000824 current_delay -= kResamplingDelay;
825 }
826
827 // 3) Compensate for non-causality, if needed, by flushing two blocks.
828 if (current_delay < PART_LEN) {
peaha421ddd2016-09-12 11:27:14 -0700829 current_delay +=
Alex Loiko890988c2017-08-31 10:25:48 +0200830 WebRtcAec_AdjustFarendBufferSizeAndSystemDelay(aecInst->aec, 2) *
831 PART_LEN;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000832 }
833
Alex Loiko890988c2017-08-31 10:25:48 +0200834 if (aecInst->filtDelay == -1) {
835 aecInst->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000836 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200837 aecInst->filtDelay = WEBRTC_SPL_MAX(
Yves Gerey665174f2018-06-19 15:03:05 +0200838 0,
839 static_cast<int16_t>(0.95 * aecInst->filtDelay + 0.05 * current_delay));
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000840 }
841
Alex Loiko890988c2017-08-31 10:25:48 +0200842 delay_difference = aecInst->filtDelay - aecInst->knownDelay;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000843 if (delay_difference > 384) {
Alex Loiko890988c2017-08-31 10:25:48 +0200844 if (aecInst->lastDelayDiff < 128) {
845 aecInst->timeForDelayChange = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000846 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200847 aecInst->timeForDelayChange++;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000848 }
Alex Loiko890988c2017-08-31 10:25:48 +0200849 } else if (delay_difference < 128 && aecInst->knownDelay > 0) {
850 if (aecInst->lastDelayDiff > 384) {
851 aecInst->timeForDelayChange = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000852 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200853 aecInst->timeForDelayChange++;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000854 }
855 } else {
Alex Loiko890988c2017-08-31 10:25:48 +0200856 aecInst->timeForDelayChange = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000857 }
Alex Loiko890988c2017-08-31 10:25:48 +0200858 aecInst->lastDelayDiff = delay_difference;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000859
Alex Loiko890988c2017-08-31 10:25:48 +0200860 if (aecInst->timeForDelayChange > 25) {
861 aecInst->knownDelay = WEBRTC_SPL_MAX((int)aecInst->filtDelay - 256, 0);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000862 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000863}
peah50e21bd2016-03-05 08:39:21 -0800864} // namespace webrtc