blob: 4f65c2b9830717696e51c430467d4a5b392ed029 [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 */
Henrik Kjellander9b72af92015-11-11 20:16:11 +010014#include "webrtc/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" {
peahfaed4ab2016-04-05 14:57:48 -070021#include "webrtc/common_audio/ring_buffer.h"
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +000022#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
peah8df5d4f2016-02-23 14:34:59 -080023}
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +000024#include "webrtc/modules/audio_processing/aec/aec_core.h"
25#include "webrtc/modules/audio_processing/aec/aec_resampler.h"
peah3f08dc62016-05-05 03:03:55 -070026#include "webrtc/modules/audio_processing/logging/apm_data_dumper.h"
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +000027#include "webrtc/typedefs.h"
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);
108static int ProcessNormal(Aec* self,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000109 const float* const* near,
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);
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000115static void ProcessExtended(Aec* self,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000116 const float* const* near,
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
minyue92594a32015-12-18 15:31:14 -0800299 // TODO(minyue): reduce to |PART_LEN| samples for each buffering, when
300 // WebRtcAec_BufferFarendPartition() is changed to take |PART_LEN| samples.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000301 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
302 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000303 {
andrew@webrtc.orge65d9d92015-01-21 22:05:12 +0000304 float* ptmp = NULL;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000305 float tmp[PART_LEN2];
peah8df5d4f2016-02-23 14:34:59 -0800306 WebRtc_ReadBuffer(aecpc->far_pre_buf,
307 reinterpret_cast<void**>(&ptmp), tmp, PART_LEN2);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000308 WebRtcAec_BufferFarendPartition(aecpc->aec, ptmp);
309 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000310
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000311 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
312 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000313 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000314
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000315 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000316}
317
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000318int32_t WebRtcAec_Process(void* aecInst,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000319 const float* const* nearend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700320 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000321 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700322 size_t nrOfSamples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000323 int16_t msInSndCardBuf,
324 int32_t skew) {
peah8df5d4f2016-02-23 14:34:59 -0800325 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000326 int32_t retVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000327
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000328 if (out == NULL) {
peahc12be392015-11-09 23:53:50 -0800329 return AEC_NULL_POINTER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000330 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000331
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000332 if (aecpc->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800333 return AEC_UNINITIALIZED_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000334 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000335
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000336 // number of samples == 160 for SWB input
337 if (nrOfSamples != 80 && nrOfSamples != 160) {
peahc12be392015-11-09 23:53:50 -0800338 return AEC_BAD_PARAMETER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000339 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000340
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000341 if (msInSndCardBuf < 0) {
342 msInSndCardBuf = 0;
peahc12be392015-11-09 23:53:50 -0800343 retVal = AEC_BAD_PARAMETER_WARNING;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000344 } else if (msInSndCardBuf > kMaxTrustedDelayMs) {
345 // The clamping is now done in ProcessExtended/Normal().
peahc12be392015-11-09 23:53:50 -0800346 retVal = AEC_BAD_PARAMETER_WARNING;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000347 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000348
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000349 // This returns the value of aec->extended_filter_enabled.
Henrik Lundin441f6342015-06-09 16:03:13 +0200350 if (WebRtcAec_extended_filter_enabled(aecpc->aec)) {
peahff63ed22016-01-29 07:46:13 -0800351 ProcessExtended(aecpc, nearend, num_bands, out, nrOfSamples, msInSndCardBuf,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000352 skew);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000353 } else {
peahff63ed22016-01-29 07:46:13 -0800354 retVal = ProcessNormal(aecpc, nearend, num_bands, out, nrOfSamples,
355 msInSndCardBuf, skew);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000356 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000357
peah3f08dc62016-05-05 03:03:55 -0700358 int far_buf_size_samples = WebRtcAec_system_delay(aecpc->aec);
359 aecpc->data_dumper->DumpRaw("aec_system_delay", 1, &far_buf_size_samples);
360 aecpc->data_dumper->DumpRaw("aec_known_delay", 1, &aecpc->knownDelay);
niklase@google.com470e71d2011-07-07 08:21:25 +0000361
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000362 return retVal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000363}
364
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000365int WebRtcAec_set_config(void* handle, AecConfig config) {
peah8df5d4f2016-02-23 14:34:59 -0800366 Aec* self = reinterpret_cast<Aec*>(handle);
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000367 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800368 return AEC_UNINITIALIZED_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000369 }
370
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000371 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
peahc12be392015-11-09 23:53:50 -0800372 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000373 }
374 self->skewMode = config.skewMode;
375
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000376 if (config.nlpMode != kAecNlpConservative &&
377 config.nlpMode != kAecNlpModerate &&
378 config.nlpMode != kAecNlpAggressive) {
peahc12be392015-11-09 23:53:50 -0800379 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000380 }
381
382 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
peahc12be392015-11-09 23:53:50 -0800383 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000384 }
385
386 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
peahc12be392015-11-09 23:53:50 -0800387 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000388 }
389
peahff63ed22016-01-29 07:46:13 -0800390 WebRtcAec_SetConfigCore(self->aec, config.nlpMode, config.metricsMode,
391 config.delay_logging);
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000392 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000393}
394
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000395int WebRtcAec_get_echo_status(void* handle, int* status) {
peah8df5d4f2016-02-23 14:34:59 -0800396 Aec* self = reinterpret_cast<Aec*>(handle);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000397 if (status == NULL) {
peahc12be392015-11-09 23:53:50 -0800398 return AEC_NULL_POINTER_ERROR;
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000399 }
400 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800401 return AEC_UNINITIALIZED_ERROR;
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000402 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000403
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000404 *status = WebRtcAec_echo_state(self->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000406 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000407}
408
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000409int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) {
410 const float kUpWeight = 0.7f;
411 float dtmp;
412 int stmp;
peah8df5d4f2016-02-23 14:34:59 -0800413 Aec* self = reinterpret_cast<Aec*>(handle);
bjornv@webrtc.orgcea70f42013-02-19 21:03:10 +0000414 Stats erl;
415 Stats erle;
416 Stats a_nlp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000417
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000418 if (handle == NULL) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000419 return -1;
420 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000421 if (metrics == NULL) {
peahc12be392015-11-09 23:53:50 -0800422 return AEC_NULL_POINTER_ERROR;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000423 }
424 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800425 return AEC_UNINITIALIZED_ERROR;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000426 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
minyue50453372016-04-07 06:36:43 -0700428 WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp,
429 &metrics->divergent_filter_fraction);
niklase@google.com470e71d2011-07-07 08:21:25 +0000430
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000431 // ERL
peah8df5d4f2016-02-23 14:34:59 -0800432 metrics->erl.instant = static_cast<int>(erl.instant);
niklase@google.com470e71d2011-07-07 08:21:25 +0000433
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000434 if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000435 // Use a mix between regular average and upper part average.
436 dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average;
peah8df5d4f2016-02-23 14:34:59 -0800437 metrics->erl.average = static_cast<int>(dtmp);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000438 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000439 metrics->erl.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000440 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000441
peah8df5d4f2016-02-23 14:34:59 -0800442 metrics->erl.max = static_cast<int>(erl.max);
niklase@google.com470e71d2011-07-07 08:21:25 +0000443
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000444 if (erl.min < (kOffsetLevel * (-1))) {
peah8df5d4f2016-02-23 14:34:59 -0800445 metrics->erl.min = static_cast<int>(erl.min);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000446 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000447 metrics->erl.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000448 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000449
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000450 // ERLE
peah8df5d4f2016-02-23 14:34:59 -0800451 metrics->erle.instant = static_cast<int>(erle.instant);
niklase@google.com470e71d2011-07-07 08:21:25 +0000452
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000453 if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000454 // Use a mix between regular average and upper part average.
455 dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average;
peah8df5d4f2016-02-23 14:34:59 -0800456 metrics->erle.average = static_cast<int>(dtmp);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000457 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000458 metrics->erle.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000459 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000460
peah8df5d4f2016-02-23 14:34:59 -0800461 metrics->erle.max = static_cast<int>(erle.max);
niklase@google.com470e71d2011-07-07 08:21:25 +0000462
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000463 if (erle.min < (kOffsetLevel * (-1))) {
peah8df5d4f2016-02-23 14:34:59 -0800464 metrics->erle.min = static_cast<int>(erle.min);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000465 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000466 metrics->erle.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000467 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000468
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000469 // RERL
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000470 if ((metrics->erl.average > kOffsetLevel) &&
471 (metrics->erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000472 stmp = metrics->erl.average + metrics->erle.average;
473 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000474 stmp = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000475 }
476 metrics->rerl.average = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000477
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000478 // No other statistics needed, but returned for completeness.
479 metrics->rerl.instant = stmp;
480 metrics->rerl.max = stmp;
481 metrics->rerl.min = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000482
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000483 // A_NLP
peah8df5d4f2016-02-23 14:34:59 -0800484 metrics->aNlp.instant = static_cast<int>(a_nlp.instant);
niklase@google.com470e71d2011-07-07 08:21:25 +0000485
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000486 if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000487 // Use a mix between regular average and upper part average.
488 dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average;
peah8df5d4f2016-02-23 14:34:59 -0800489 metrics->aNlp.average = static_cast<int>(dtmp);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000490 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000491 metrics->aNlp.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000492 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000493
peah8df5d4f2016-02-23 14:34:59 -0800494 metrics->aNlp.max = static_cast<int>(a_nlp.max);
niklase@google.com470e71d2011-07-07 08:21:25 +0000495
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000496 if (a_nlp.min < (kOffsetLevel * (-1))) {
peah8df5d4f2016-02-23 14:34:59 -0800497 metrics->aNlp.min = static_cast<int>(a_nlp.min);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000498 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000499 metrics->aNlp.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000500 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000501
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000502 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000503}
504
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000505int WebRtcAec_GetDelayMetrics(void* handle,
506 int* median,
507 int* std,
508 float* fraction_poor_delays) {
peah8df5d4f2016-02-23 14:34:59 -0800509 Aec* self = reinterpret_cast<Aec*>(handle);
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000510 if (median == NULL) {
peahc12be392015-11-09 23:53:50 -0800511 return AEC_NULL_POINTER_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000512 }
513 if (std == NULL) {
peahc12be392015-11-09 23:53:50 -0800514 return AEC_NULL_POINTER_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000515 }
516 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800517 return AEC_UNINITIALIZED_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000518 }
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000519 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std,
peahff63ed22016-01-29 07:46:13 -0800520 fraction_poor_delays) == -1) {
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000521 // Logging disabled.
peahc12be392015-11-09 23:53:50 -0800522 return AEC_UNSUPPORTED_FUNCTION_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000523 }
524
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000525 return 0;
526}
527
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000528AecCore* WebRtcAec_aec_core(void* handle) {
529 if (!handle) {
530 return NULL;
531 }
peah8df5d4f2016-02-23 14:34:59 -0800532 return reinterpret_cast<Aec*>(handle)->aec;
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000533}
534
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000535static int ProcessNormal(Aec* aecpc,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000536 const float* const* nearend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700537 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000538 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700539 size_t nrOfSamples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000540 int16_t msInSndCardBuf,
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000541 int32_t skew) {
542 int retVal = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700543 size_t i;
544 size_t nBlocks10ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000545 // Limit resampling to doubling/halving of signal
546 const float minSkewEst = -0.5f;
547 const float maxSkewEst = 1.0f;
548
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000549 msInSndCardBuf =
550 msInSndCardBuf > kMaxTrustedDelayMs ? kMaxTrustedDelayMs : msInSndCardBuf;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000551 // TODO(andrew): we need to investigate if this +10 is really wanted.
552 msInSndCardBuf += 10;
553 aecpc->msInSndCardBuf = msInSndCardBuf;
554
555 if (aecpc->skewMode == kAecTrue) {
556 if (aecpc->skewFrCtr < 25) {
557 aecpc->skewFrCtr++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000558 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000559 retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
560 if (retVal == -1) {
561 aecpc->skew = 0;
peahc12be392015-11-09 23:53:50 -0800562 retVal = AEC_BAD_PARAMETER_WARNING;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000563 }
564
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000565 aecpc->skew /= aecpc->sampFactor * nrOfSamples;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000566
567 if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
568 aecpc->resample = kAecFalse;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000569 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000570 aecpc->resample = kAecTrue;
571 }
572
573 if (aecpc->skew < minSkewEst) {
574 aecpc->skew = minSkewEst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000575 } else if (aecpc->skew > maxSkewEst) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000576 aecpc->skew = maxSkewEst;
577 }
578
peah3f08dc62016-05-05 03:03:55 -0700579 aecpc->data_dumper->DumpRaw("aec_skew", 1, &aecpc->skew);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000580 }
581 }
582
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000583 nBlocks10ms = nrOfSamples / (FRAME_LEN * aecpc->rate_factor);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000584
585 if (aecpc->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]) {
589 memcpy(out[i], nearend[i], sizeof(nearend[i][0]) * nrOfSamples);
590 }
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.
597 if (aecpc->checkBuffSize) {
598 aecpc->checkBufSizeCtr++;
599 // 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.
604 if (aecpc->counter == 0) {
605 aecpc->firstVal = aecpc->msInSndCardBuf;
606 aecpc->sum = 0;
607 }
608
609 if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000610 WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000611 aecpc->sum += aecpc->msInSndCardBuf;
612 aecpc->counter++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000613 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000614 aecpc->counter = 0;
615 }
616
617 if (aecpc->counter * nBlocks10ms >= 6) {
618 // 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.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000621 aecpc->bufSizeStart =
622 WEBRTC_SPL_MIN((3 * aecpc->sum * aecpc->rate_factor * 8) /
623 (4 * aecpc->counter * PART_LEN),
624 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000625 // Buffer size has now been determined.
626 aecpc->checkBuffSize = 0;
627 }
628
629 if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
630 // For really bad systems, don't disable the echo canceller for
631 // more than 0.5 sec.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000632 aecpc->bufSizeStart = WEBRTC_SPL_MIN(
633 (aecpc->msInSndCardBuf * aecpc->rate_factor * 3) / 40,
634 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000635 aecpc->checkBuffSize = 0;
636 }
637 }
638
639 // If |checkBuffSize| changed in the if-statement above.
640 if (!aecpc->checkBuffSize) {
641 // 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 =
646 WebRtcAec_system_delay(aecpc->aec) / PART_LEN - aecpc->bufSizeStart;
647 if (overhead_elements == 0) {
648 // Enable the AEC
649 aecpc->startup_phase = 0;
650 } else if (overhead_elements > 0) {
651 // TODO(bjornv): Do we need a check on how much we actually
652 // moved the read pointer? It should always be possible to move
653 // the pointer |overhead_elements| since we have only added data
654 // to the buffer and no delay compensation nor AEC processing
655 // has been done.
656 WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements);
657
658 // Enable the AEC
659 aecpc->startup_phase = 0;
660 }
661 }
662 } else {
663 // AEC is enabled.
Bjorn Volcker71012692015-06-18 11:04:56 +0200664 EstBufDelayNormal(aecpc);
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
668 // |aecpc->knownDelay| as input. Change name to something like
669 // |system_buffer_diff|.
peahff63ed22016-01-29 07:46:13 -0800670 WebRtcAec_ProcessFrames(aecpc->aec, nearend, num_bands, nrOfSamples,
671 aecpc->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,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000678 const float* const* near,
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.
710 if (near[i] != out[i]) {
711 memcpy(out[i], near[i], sizeof(near[i][0]) * num_samples);
712 }
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;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000733 WebRtcAec_MoveFarReadPtr(self->aec, overhead_elements);
734 self->startup_phase = 0;
735 }
736
Bjorn Volcker71012692015-06-18 11:04:56 +0200737 EstBufDelayExtended(self);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000738
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000739 {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000740 // |delay_diff_offset| gives us the option to manually rewind the delay on
741 // very low delay platforms which can't be expressed purely through
742 // |reported_delay_ms|.
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000743 const int adjusted_known_delay =
744 WEBRTC_SPL_MAX(0, self->knownDelay + delay_diff_offset);
745
peahff63ed22016-01-29 07:46:13 -0800746 WebRtcAec_ProcessFrames(self->aec, near, num_bands, num_samples,
747 adjusted_known_delay, out);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000748 }
749}
750
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000751static void EstBufDelayNormal(Aec* aecpc) {
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000752 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor;
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000753 int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->aec);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000754 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000755
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000756 // Before we proceed with the delay estimate filtering we:
757 // 1) Compensate for the frame that will be read.
758 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000759 // 3) Compensate for non-causality if needed, since the estimated delay can't
760 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000761
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000762 // 1) Compensating for the frame(s) that will be read/processed.
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000763 current_delay += FRAME_LEN * aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000764
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000765 // 2) Account for resampling frame delay.
766 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
767 current_delay -= kResamplingDelay;
768 }
769
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000770 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000771 if (current_delay < PART_LEN) {
772 current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
773 }
774
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000775 // We use -1 to signal an initialized state in the "extended" implementation;
776 // compensate for that.
777 aecpc->filtDelay = aecpc->filtDelay < 0 ? 0 : aecpc->filtDelay;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000778 aecpc->filtDelay =
peah8df5d4f2016-02-23 14:34:59 -0800779 WEBRTC_SPL_MAX(0, static_cast<int16_t>(0.8 *
780 aecpc->filtDelay +
781 0.2 * current_delay));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000782
783 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
784 if (delay_difference > 224) {
785 if (aecpc->lastDelayDiff < 96) {
786 aecpc->timeForDelayChange = 0;
787 } else {
788 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000789 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000790 } else if (delay_difference < 96 && aecpc->knownDelay > 0) {
791 if (aecpc->lastDelayDiff > 224) {
792 aecpc->timeForDelayChange = 0;
793 } else {
794 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000795 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000796 } else {
797 aecpc->timeForDelayChange = 0;
798 }
799 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000800
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000801 if (aecpc->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000802 aecpc->knownDelay = WEBRTC_SPL_MAX((int)aecpc->filtDelay - 160, 0);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000803 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000804}
niklase@google.com470e71d2011-07-07 08:21:25 +0000805
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000806static void EstBufDelayExtended(Aec* self) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000807 int reported_delay = self->msInSndCardBuf * sampMsNb * self->rate_factor;
808 int current_delay = reported_delay - WebRtcAec_system_delay(self->aec);
809 int delay_difference = 0;
810
811 // Before we proceed with the delay estimate filtering we:
812 // 1) Compensate for the frame that will be read.
813 // 2) Compensate for drift resampling.
814 // 3) Compensate for non-causality if needed, since the estimated delay can't
815 // be negative.
816
817 // 1) Compensating for the frame(s) that will be read/processed.
818 current_delay += FRAME_LEN * self->rate_factor;
819
820 // 2) Account for resampling frame delay.
821 if (self->skewMode == kAecTrue && self->resample == kAecTrue) {
822 current_delay -= kResamplingDelay;
823 }
824
825 // 3) Compensate for non-causality, if needed, by flushing two blocks.
826 if (current_delay < PART_LEN) {
827 current_delay += WebRtcAec_MoveFarReadPtr(self->aec, 2) * PART_LEN;
828 }
829
830 if (self->filtDelay == -1) {
831 self->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay);
832 } else {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000833 self->filtDelay = WEBRTC_SPL_MAX(
peah8df5d4f2016-02-23 14:34:59 -0800834 0, static_cast<int16_t>(0.95 * self->filtDelay + 0.05 * current_delay));
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000835 }
836
837 delay_difference = self->filtDelay - self->knownDelay;
838 if (delay_difference > 384) {
839 if (self->lastDelayDiff < 128) {
840 self->timeForDelayChange = 0;
841 } else {
842 self->timeForDelayChange++;
843 }
844 } else if (delay_difference < 128 && self->knownDelay > 0) {
845 if (self->lastDelayDiff > 384) {
846 self->timeForDelayChange = 0;
847 } else {
848 self->timeForDelayChange++;
849 }
850 } else {
851 self->timeForDelayChange = 0;
852 }
853 self->lastDelayDiff = delay_difference;
854
855 if (self->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000856 self->knownDelay = WEBRTC_SPL_MAX((int)self->filtDelay - 256, 0);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000857 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000858}
peah50e21bd2016-03-05 08:39:21 -0800859} // namespace webrtc