blob: 716da38ab8815efebbbb495b372f65ed3eb47464 [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
andrew@webrtc.org1760a172013-09-25 23:17:38 +000031// Measured delays [ms]
32// Device Chrome GTP
33// MacBook Air 10
34// MacBook Retina 10 100
35// MacPro 30?
36//
37// Win7 Desktop 70 80?
38// Win7 T430s 110
39// Win8 T420s 70
40//
41// Daisy 50
42// Pixel (w/ preproc?) 240
43// Pixel (w/o preproc?) 110 110
44
45// The extended filter mode gives us the flexibility to ignore the system's
46// reported delays. We do this for platforms which we believe provide results
47// which are incompatible with the AEC's expectations. Based on measurements
48// (some provided above) we set a conservative (i.e. lower than measured)
49// fixed delay.
50//
51// WEBRTC_UNTRUSTED_DELAY will only have an impact when |extended_filter_mode|
52// is enabled. See the note along with |DelayCorrection| in
53// echo_cancellation_impl.h for more details on the mode.
54//
55// Justification:
56// Chromium/Mac: Here, the true latency is so low (~10-20 ms), that it plays
57// havoc with the AEC's buffering. To avoid this, we set a fixed delay of 20 ms
58// and then compensate by rewinding by 10 ms (in wideband) through
59// kDelayDiffOffsetSamples. This trick does not seem to work for larger rewind
60// values, but fortunately this is sufficient.
61//
62// Chromium/Linux(ChromeOS): The values we get on this platform don't correspond
63// well to reality. The variance doesn't match the AEC's buffer changes, and the
64// bulk values tend to be too low. However, the range across different hardware
65// appears to be too large to choose a single value.
66//
67// GTP/Linux(ChromeOS): TBD, but for the moment we will trust the values.
68#if defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_MAC)
69#define WEBRTC_UNTRUSTED_DELAY
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +000070#endif
andrew@webrtc.orgacb00502013-10-04 16:59:17 +000071
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +000072#if defined(WEBRTC_UNTRUSTED_DELAY) && defined(WEBRTC_MAC)
andrew@webrtc.orgacb00502013-10-04 16:59:17 +000073static const int kDelayDiffOffsetSamples = -160;
74#else
75// Not enabled for now.
76static const int kDelayDiffOffsetSamples = 0;
77#endif
andrew@webrtc.org1760a172013-09-25 23:17:38 +000078
79#if defined(WEBRTC_MAC)
80static const int kFixedDelayMs = 20;
andrew@webrtc.org1760a172013-09-25 23:17:38 +000081#else
andrew@webrtc.org1760a172013-09-25 23:17:38 +000082static const int kFixedDelayMs = 50;
andrew@webrtc.org1760a172013-09-25 23:17:38 +000083#endif
andrew@webrtc.orgc2e471d2013-10-15 02:11:21 +000084#if !defined(WEBRTC_UNTRUSTED_DELAY)
andrew@webrtc.org1760a172013-09-25 23:17:38 +000085static const int kMinTrustedDelayMs = 20;
andrew@webrtc.orgc2e471d2013-10-15 02:11:21 +000086#endif
andrew@webrtc.org1760a172013-09-25 23:17:38 +000087static const int kMaxTrustedDelayMs = 500;
88
niklase@google.com470e71d2011-07-07 08:21:25 +000089// Maximum length of resampled signal. Must be an integer multiple of frames
90// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
91// The factor of 2 handles wb, and the + 1 is as a safety margin
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000092// TODO(bjornv): Replace with kResamplerBufferSize
niklase@google.com470e71d2011-07-07 08:21:25 +000093#define MAX_RESAMP_LEN (5 * FRAME_LEN)
94
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000095static const int kMaxBufSizeStart = 62; // In partitions
andrew@webrtc.org13b2d462013-10-08 23:41:42 +000096static const int sampMsNb = 8; // samples per ms in nb
niklase@google.com470e71d2011-07-07 08:21:25 +000097static const int initCheck = 42;
98
peahb46083e2016-05-03 07:01:18 -070099int Aec::instance_count = 0;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000100
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000101// Estimates delay to set the position of the far-end buffer read pointer
niklase@google.com470e71d2011-07-07 08:21:25 +0000102// (controlled by knownDelay)
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000103static void EstBufDelayNormal(Aec* aecInst);
104static void EstBufDelayExtended(Aec* aecInst);
105static int ProcessNormal(Aec* self,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000106 const float* const* near,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700107 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000108 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700109 size_t num_samples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000110 int16_t reported_delay_ms,
111 int32_t skew);
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000112static void ProcessExtended(Aec* self,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000113 const float* const* near,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700114 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000115 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700116 size_t num_samples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000117 int16_t reported_delay_ms,
118 int32_t skew);
niklase@google.com470e71d2011-07-07 08:21:25 +0000119
Bjorn Volcker9345e862015-06-10 21:43:36 +0200120void* WebRtcAec_Create() {
peah3f08dc62016-05-05 03:03:55 -0700121 Aec* aecpc = new Aec();
Bjorn Volcker9345e862015-06-10 21:43:36 +0200122
123 if (!aecpc) {
124 return NULL;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000125 }
peah3f08dc62016-05-05 03:03:55 -0700126 aecpc->data_dumper.reset(new ApmDataDumper(aecpc->instance_count));
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
peahb46083e2016-05-03 07:01:18 -0700128 aecpc->aec = WebRtcAec_CreateAec(aecpc->instance_count);
Bjorn Volcker9345e862015-06-10 21:43:36 +0200129 if (!aecpc->aec) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000130 WebRtcAec_Free(aecpc);
Bjorn Volcker9345e862015-06-10 21:43:36 +0200131 return NULL;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000132 }
Bjorn Volcker9345e862015-06-10 21:43:36 +0200133 aecpc->resampler = WebRtcAec_CreateResampler();
134 if (!aecpc->resampler) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000135 WebRtcAec_Free(aecpc);
Bjorn Volcker9345e862015-06-10 21:43:36 +0200136 return NULL;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000137 }
138 // Create far-end pre-buffer. The buffer size has to be large enough for
139 // largest possible drift compensation (kResamplerBufferSize) + "almost" an
140 // FFT buffer (PART_LEN2 - 1).
141 aecpc->far_pre_buf =
142 WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize, sizeof(float));
143 if (!aecpc->far_pre_buf) {
144 WebRtcAec_Free(aecpc);
Bjorn Volcker9345e862015-06-10 21:43:36 +0200145 return NULL;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000146 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000148 aecpc->initFlag = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000149
peahb46083e2016-05-03 07:01:18 -0700150 aecpc->instance_count++;
Bjorn Volcker9345e862015-06-10 21:43:36 +0200151 return aecpc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000152}
153
Bjorn Volckerf6a99e62015-04-10 07:56:57 +0200154void WebRtcAec_Free(void* aecInst) {
peah8df5d4f2016-02-23 14:34:59 -0800155 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000156
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000157 if (aecpc == NULL) {
Bjorn Volckerf6a99e62015-04-10 07:56:57 +0200158 return;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000159 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000160
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000161 WebRtc_FreeBuffer(aecpc->far_pre_buf);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000162
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000163 WebRtcAec_FreeAec(aecpc->aec);
164 WebRtcAec_FreeResampler(aecpc->resampler);
peah3f08dc62016-05-05 03:03:55 -0700165 delete aecpc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000166}
167
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000168int32_t WebRtcAec_Init(void* aecInst, int32_t sampFreq, int32_t scSampFreq) {
peah8df5d4f2016-02-23 14:34:59 -0800169 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
peah3f08dc62016-05-05 03:03:55 -0700170 aecpc->data_dumper->InitiateNewSetOfRecordings();
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000171 AecConfig aecConfig;
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
peahff63ed22016-01-29 07:46:13 -0800173 if (sampFreq != 8000 && sampFreq != 16000 && sampFreq != 32000 &&
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000174 sampFreq != 48000) {
peahc12be392015-11-09 23:53:50 -0800175 return AEC_BAD_PARAMETER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000176 }
177 aecpc->sampFreq = sampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000178
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000179 if (scSampFreq < 1 || scSampFreq > 96000) {
peahc12be392015-11-09 23:53:50 -0800180 return AEC_BAD_PARAMETER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000181 }
182 aecpc->scSampFreq = scSampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000183
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000184 // Initialize echo canceller core
185 if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) {
peahc12be392015-11-09 23:53:50 -0800186 return AEC_UNSPECIFIED_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000187 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000189 if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) {
peahc12be392015-11-09 23:53:50 -0800190 return AEC_UNSPECIFIED_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000191 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000192
andrew@webrtc.org6b630152015-01-15 00:09:53 +0000193 WebRtc_InitBuffer(aecpc->far_pre_buf);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000194 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap.
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000195
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000196 aecpc->initFlag = initCheck; // indicates that initialization has been done
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000198 if (aecpc->sampFreq == 32000 || aecpc->sampFreq == 48000) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000199 aecpc->splitSampFreq = 16000;
200 } else {
201 aecpc->splitSampFreq = sampFreq;
202 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000203
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000204 aecpc->delayCtr = 0;
205 aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq;
206 // Sampling frequency multiplier (SWB is processed as 160 frame size).
207 aecpc->rate_factor = aecpc->splitSampFreq / 8000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000209 aecpc->sum = 0;
210 aecpc->counter = 0;
211 aecpc->checkBuffSize = 1;
212 aecpc->firstVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213
Bjorn Volcker71012692015-06-18 11:04:56 +0200214 // We skip the startup_phase completely (setting to 0) if DA-AEC is enabled,
215 // but not extended_filter mode.
216 aecpc->startup_phase = WebRtcAec_extended_filter_enabled(aecpc->aec) ||
peahff63ed22016-01-29 07:46:13 -0800217 !WebRtcAec_delay_agnostic_enabled(aecpc->aec);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000218 aecpc->bufSizeStart = 0;
219 aecpc->checkBufSizeCtr = 0;
220 aecpc->msInSndCardBuf = 0;
221 aecpc->filtDelay = -1; // -1 indicates an initialized state.
222 aecpc->timeForDelayChange = 0;
223 aecpc->knownDelay = 0;
224 aecpc->lastDelayDiff = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000225
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000226 aecpc->skewFrCtr = 0;
227 aecpc->resample = kAecFalse;
228 aecpc->highSkewCtr = 0;
229 aecpc->skew = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000230
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000231 aecpc->farend_started = 0;
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000232
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000233 // Default settings.
234 aecConfig.nlpMode = kAecNlpModerate;
235 aecConfig.skewMode = kAecFalse;
236 aecConfig.metricsMode = kAecFalse;
237 aecConfig.delay_logging = kAecFalse;
niklase@google.com470e71d2011-07-07 08:21:25 +0000238
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000239 if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
peahc12be392015-11-09 23:53:50 -0800240 return AEC_UNSPECIFIED_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000241 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000242
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000243 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000244}
245
peahc12be392015-11-09 23:53:50 -0800246// Returns any error that is caused when buffering the
247// far-end signal.
248int32_t WebRtcAec_GetBufferFarendError(void* aecInst,
249 const float* farend,
250 size_t nrOfSamples) {
peah8df5d4f2016-02-23 14:34:59 -0800251 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
peahc12be392015-11-09 23:53:50 -0800252
253 if (!farend)
254 return AEC_NULL_POINTER_ERROR;
255
256 if (aecpc->initFlag != initCheck)
257 return AEC_UNINITIALIZED_ERROR;
258
259 // number of samples == 160 for SWB input
260 if (nrOfSamples != 80 && nrOfSamples != 160)
261 return AEC_BAD_PARAMETER_ERROR;
262
263 return 0;
264}
265
niklase@google.com470e71d2011-07-07 08:21:25 +0000266// only buffer L band for farend
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000267int32_t WebRtcAec_BufferFarend(void* aecInst,
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000268 const float* farend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700269 size_t nrOfSamples) {
peah8df5d4f2016-02-23 14:34:59 -0800270 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700271 size_t newNrOfSamples = nrOfSamples;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000272 float new_farend[MAX_RESAMP_LEN];
273 const float* farend_ptr = farend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000274
peahc12be392015-11-09 23:53:50 -0800275 // Get any error caused by buffering the farend signal.
peahff63ed22016-01-29 07:46:13 -0800276 int32_t error_code =
277 WebRtcAec_GetBufferFarendError(aecInst, farend, nrOfSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +0000278
peahc12be392015-11-09 23:53:50 -0800279 if (error_code != 0)
280 return error_code;
niklase@google.com470e71d2011-07-07 08:21:25 +0000281
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000282 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
283 // Resample and get a new number of samples
peahff63ed22016-01-29 07:46:13 -0800284 WebRtcAec_ResampleLinear(aecpc->resampler, farend, nrOfSamples, aecpc->skew,
285 new_farend, &newNrOfSamples);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000286 farend_ptr = new_farend;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000287 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000288
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000289 aecpc->farend_started = 1;
peah8df5d4f2016-02-23 14:34:59 -0800290 WebRtcAec_SetSystemDelay(aecpc->aec, WebRtcAec_system_delay(aecpc->aec) +
291 static_cast<int>(newNrOfSamples));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000292
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000293 // Write the time-domain data to |far_pre_buf|.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700294 WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_ptr, newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000295
minyue92594a32015-12-18 15:31:14 -0800296 // TODO(minyue): reduce to |PART_LEN| samples for each buffering, when
297 // WebRtcAec_BufferFarendPartition() is changed to take |PART_LEN| samples.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000298 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
299 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000300 {
andrew@webrtc.orge65d9d92015-01-21 22:05:12 +0000301 float* ptmp = NULL;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000302 float tmp[PART_LEN2];
peah8df5d4f2016-02-23 14:34:59 -0800303 WebRtc_ReadBuffer(aecpc->far_pre_buf,
304 reinterpret_cast<void**>(&ptmp), tmp, PART_LEN2);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000305 WebRtcAec_BufferFarendPartition(aecpc->aec, ptmp);
306 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000307
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000308 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
309 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000310 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000311
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000312 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000313}
314
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000315int32_t WebRtcAec_Process(void* aecInst,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000316 const float* const* nearend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700317 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000318 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700319 size_t nrOfSamples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000320 int16_t msInSndCardBuf,
321 int32_t skew) {
peah8df5d4f2016-02-23 14:34:59 -0800322 Aec* aecpc = reinterpret_cast<Aec*>(aecInst);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000323 int32_t retVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000324
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000325 if (out == NULL) {
peahc12be392015-11-09 23:53:50 -0800326 return AEC_NULL_POINTER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000327 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000328
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000329 if (aecpc->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800330 return AEC_UNINITIALIZED_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000331 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000332
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000333 // number of samples == 160 for SWB input
334 if (nrOfSamples != 80 && nrOfSamples != 160) {
peahc12be392015-11-09 23:53:50 -0800335 return AEC_BAD_PARAMETER_ERROR;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000336 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000337
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000338 if (msInSndCardBuf < 0) {
339 msInSndCardBuf = 0;
peahc12be392015-11-09 23:53:50 -0800340 retVal = AEC_BAD_PARAMETER_WARNING;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000341 } else if (msInSndCardBuf > kMaxTrustedDelayMs) {
342 // The clamping is now done in ProcessExtended/Normal().
peahc12be392015-11-09 23:53:50 -0800343 retVal = AEC_BAD_PARAMETER_WARNING;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000344 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000345
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000346 // This returns the value of aec->extended_filter_enabled.
Henrik Lundin441f6342015-06-09 16:03:13 +0200347 if (WebRtcAec_extended_filter_enabled(aecpc->aec)) {
peahff63ed22016-01-29 07:46:13 -0800348 ProcessExtended(aecpc, nearend, num_bands, out, nrOfSamples, msInSndCardBuf,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000349 skew);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000350 } else {
peahff63ed22016-01-29 07:46:13 -0800351 retVal = ProcessNormal(aecpc, nearend, num_bands, out, nrOfSamples,
352 msInSndCardBuf, skew);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000353 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000354
peah3f08dc62016-05-05 03:03:55 -0700355 int far_buf_size_samples = WebRtcAec_system_delay(aecpc->aec);
356 aecpc->data_dumper->DumpRaw("aec_system_delay", 1, &far_buf_size_samples);
357 aecpc->data_dumper->DumpRaw("aec_known_delay", 1, &aecpc->knownDelay);
niklase@google.com470e71d2011-07-07 08:21:25 +0000358
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000359 return retVal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000360}
361
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000362int WebRtcAec_set_config(void* handle, AecConfig config) {
peah8df5d4f2016-02-23 14:34:59 -0800363 Aec* self = reinterpret_cast<Aec*>(handle);
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000364 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800365 return AEC_UNINITIALIZED_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000366 }
367
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000368 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
peahc12be392015-11-09 23:53:50 -0800369 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000370 }
371 self->skewMode = config.skewMode;
372
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000373 if (config.nlpMode != kAecNlpConservative &&
374 config.nlpMode != kAecNlpModerate &&
375 config.nlpMode != kAecNlpAggressive) {
peahc12be392015-11-09 23:53:50 -0800376 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000377 }
378
379 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
peahc12be392015-11-09 23:53:50 -0800380 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000381 }
382
383 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
peahc12be392015-11-09 23:53:50 -0800384 return AEC_BAD_PARAMETER_ERROR;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000385 }
386
peahff63ed22016-01-29 07:46:13 -0800387 WebRtcAec_SetConfigCore(self->aec, config.nlpMode, config.metricsMode,
388 config.delay_logging);
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000389 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000390}
391
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000392int WebRtcAec_get_echo_status(void* handle, int* status) {
peah8df5d4f2016-02-23 14:34:59 -0800393 Aec* self = reinterpret_cast<Aec*>(handle);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000394 if (status == NULL) {
peahc12be392015-11-09 23:53:50 -0800395 return AEC_NULL_POINTER_ERROR;
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000396 }
397 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800398 return AEC_UNINITIALIZED_ERROR;
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000399 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000400
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000401 *status = WebRtcAec_echo_state(self->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000402
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000403 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000404}
405
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000406int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) {
407 const float kUpWeight = 0.7f;
408 float dtmp;
409 int stmp;
peah8df5d4f2016-02-23 14:34:59 -0800410 Aec* self = reinterpret_cast<Aec*>(handle);
bjornv@webrtc.orgcea70f42013-02-19 21:03:10 +0000411 Stats erl;
412 Stats erle;
413 Stats a_nlp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000415 if (handle == NULL) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000416 return -1;
417 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000418 if (metrics == NULL) {
peahc12be392015-11-09 23:53:50 -0800419 return AEC_NULL_POINTER_ERROR;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000420 }
421 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800422 return AEC_UNINITIALIZED_ERROR;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000423 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000424
minyue50453372016-04-07 06:36:43 -0700425 WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp,
426 &metrics->divergent_filter_fraction);
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000428 // ERL
peah8df5d4f2016-02-23 14:34:59 -0800429 metrics->erl.instant = static_cast<int>(erl.instant);
niklase@google.com470e71d2011-07-07 08:21:25 +0000430
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000431 if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000432 // Use a mix between regular average and upper part average.
433 dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average;
peah8df5d4f2016-02-23 14:34:59 -0800434 metrics->erl.average = static_cast<int>(dtmp);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000435 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000436 metrics->erl.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000437 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000438
peah8df5d4f2016-02-23 14:34:59 -0800439 metrics->erl.max = static_cast<int>(erl.max);
niklase@google.com470e71d2011-07-07 08:21:25 +0000440
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000441 if (erl.min < (kOffsetLevel * (-1))) {
peah8df5d4f2016-02-23 14:34:59 -0800442 metrics->erl.min = static_cast<int>(erl.min);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000443 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000444 metrics->erl.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000445 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000446
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000447 // ERLE
peah8df5d4f2016-02-23 14:34:59 -0800448 metrics->erle.instant = static_cast<int>(erle.instant);
niklase@google.com470e71d2011-07-07 08:21:25 +0000449
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000450 if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000451 // Use a mix between regular average and upper part average.
452 dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average;
peah8df5d4f2016-02-23 14:34:59 -0800453 metrics->erle.average = static_cast<int>(dtmp);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000454 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000455 metrics->erle.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000456 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000457
peah8df5d4f2016-02-23 14:34:59 -0800458 metrics->erle.max = static_cast<int>(erle.max);
niklase@google.com470e71d2011-07-07 08:21:25 +0000459
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000460 if (erle.min < (kOffsetLevel * (-1))) {
peah8df5d4f2016-02-23 14:34:59 -0800461 metrics->erle.min = static_cast<int>(erle.min);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000462 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000463 metrics->erle.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000464 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000465
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000466 // RERL
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000467 if ((metrics->erl.average > kOffsetLevel) &&
468 (metrics->erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000469 stmp = metrics->erl.average + metrics->erle.average;
470 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000471 stmp = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000472 }
473 metrics->rerl.average = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000474
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000475 // No other statistics needed, but returned for completeness.
476 metrics->rerl.instant = stmp;
477 metrics->rerl.max = stmp;
478 metrics->rerl.min = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000479
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000480 // A_NLP
peah8df5d4f2016-02-23 14:34:59 -0800481 metrics->aNlp.instant = static_cast<int>(a_nlp.instant);
niklase@google.com470e71d2011-07-07 08:21:25 +0000482
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000483 if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000484 // Use a mix between regular average and upper part average.
485 dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average;
peah8df5d4f2016-02-23 14:34:59 -0800486 metrics->aNlp.average = static_cast<int>(dtmp);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000487 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000488 metrics->aNlp.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000489 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000490
peah8df5d4f2016-02-23 14:34:59 -0800491 metrics->aNlp.max = static_cast<int>(a_nlp.max);
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000493 if (a_nlp.min < (kOffsetLevel * (-1))) {
peah8df5d4f2016-02-23 14:34:59 -0800494 metrics->aNlp.min = static_cast<int>(a_nlp.min);
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000495 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000496 metrics->aNlp.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000497 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000498
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000499 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000500}
501
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000502int WebRtcAec_GetDelayMetrics(void* handle,
503 int* median,
504 int* std,
505 float* fraction_poor_delays) {
peah8df5d4f2016-02-23 14:34:59 -0800506 Aec* self = reinterpret_cast<Aec*>(handle);
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000507 if (median == NULL) {
peahc12be392015-11-09 23:53:50 -0800508 return AEC_NULL_POINTER_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000509 }
510 if (std == NULL) {
peahc12be392015-11-09 23:53:50 -0800511 return AEC_NULL_POINTER_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000512 }
513 if (self->initFlag != initCheck) {
peahc12be392015-11-09 23:53:50 -0800514 return AEC_UNINITIALIZED_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000515 }
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000516 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std,
peahff63ed22016-01-29 07:46:13 -0800517 fraction_poor_delays) == -1) {
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000518 // Logging disabled.
peahc12be392015-11-09 23:53:50 -0800519 return AEC_UNSUPPORTED_FUNCTION_ERROR;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000520 }
521
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000522 return 0;
523}
524
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000525AecCore* WebRtcAec_aec_core(void* handle) {
526 if (!handle) {
527 return NULL;
528 }
peah8df5d4f2016-02-23 14:34:59 -0800529 return reinterpret_cast<Aec*>(handle)->aec;
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000530}
531
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000532static int ProcessNormal(Aec* aecpc,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000533 const float* const* nearend,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700534 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000535 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700536 size_t nrOfSamples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000537 int16_t msInSndCardBuf,
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000538 int32_t skew) {
539 int retVal = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700540 size_t i;
541 size_t nBlocks10ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000542 // Limit resampling to doubling/halving of signal
543 const float minSkewEst = -0.5f;
544 const float maxSkewEst = 1.0f;
545
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000546 msInSndCardBuf =
547 msInSndCardBuf > kMaxTrustedDelayMs ? kMaxTrustedDelayMs : msInSndCardBuf;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000548 // TODO(andrew): we need to investigate if this +10 is really wanted.
549 msInSndCardBuf += 10;
550 aecpc->msInSndCardBuf = msInSndCardBuf;
551
552 if (aecpc->skewMode == kAecTrue) {
553 if (aecpc->skewFrCtr < 25) {
554 aecpc->skewFrCtr++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000555 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000556 retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
557 if (retVal == -1) {
558 aecpc->skew = 0;
peahc12be392015-11-09 23:53:50 -0800559 retVal = AEC_BAD_PARAMETER_WARNING;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000560 }
561
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000562 aecpc->skew /= aecpc->sampFactor * nrOfSamples;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000563
564 if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
565 aecpc->resample = kAecFalse;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000566 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000567 aecpc->resample = kAecTrue;
568 }
569
570 if (aecpc->skew < minSkewEst) {
571 aecpc->skew = minSkewEst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000572 } else if (aecpc->skew > maxSkewEst) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000573 aecpc->skew = maxSkewEst;
574 }
575
peah3f08dc62016-05-05 03:03:55 -0700576 aecpc->data_dumper->DumpRaw("aec_skew", 1, &aecpc->skew);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000577 }
578 }
579
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000580 nBlocks10ms = nrOfSamples / (FRAME_LEN * aecpc->rate_factor);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000581
582 if (aecpc->startup_phase) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000583 for (i = 0; i < num_bands; ++i) {
584 // Only needed if they don't already point to the same place.
585 if (nearend[i] != out[i]) {
586 memcpy(out[i], nearend[i], sizeof(nearend[i][0]) * nrOfSamples);
587 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000588 }
589
590 // The AEC is in the start up mode
591 // AEC is disabled until the system delay is OK
592
593 // Mechanism to ensure that the system delay is reasonably stable.
594 if (aecpc->checkBuffSize) {
595 aecpc->checkBufSizeCtr++;
596 // Before we fill up the far-end buffer we require the system delay
597 // to be stable (+/-8 ms) compared to the first value. This
598 // comparison is made during the following 6 consecutive 10 ms
599 // blocks. If it seems to be stable then we start to fill up the
600 // far-end buffer.
601 if (aecpc->counter == 0) {
602 aecpc->firstVal = aecpc->msInSndCardBuf;
603 aecpc->sum = 0;
604 }
605
606 if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000607 WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000608 aecpc->sum += aecpc->msInSndCardBuf;
609 aecpc->counter++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000610 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000611 aecpc->counter = 0;
612 }
613
614 if (aecpc->counter * nBlocks10ms >= 6) {
615 // The far-end buffer size is determined in partitions of
616 // PART_LEN samples. Use 75% of the average value of the system
617 // delay as buffer size to start with.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000618 aecpc->bufSizeStart =
619 WEBRTC_SPL_MIN((3 * aecpc->sum * aecpc->rate_factor * 8) /
620 (4 * aecpc->counter * PART_LEN),
621 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000622 // Buffer size has now been determined.
623 aecpc->checkBuffSize = 0;
624 }
625
626 if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
627 // For really bad systems, don't disable the echo canceller for
628 // more than 0.5 sec.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000629 aecpc->bufSizeStart = WEBRTC_SPL_MIN(
630 (aecpc->msInSndCardBuf * aecpc->rate_factor * 3) / 40,
631 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000632 aecpc->checkBuffSize = 0;
633 }
634 }
635
636 // If |checkBuffSize| changed in the if-statement above.
637 if (!aecpc->checkBuffSize) {
638 // The system delay is now reasonably stable (or has been unstable
639 // for too long). When the far-end buffer is filled with
640 // approximately the same amount of data as reported by the system
641 // we end the startup phase.
642 int overhead_elements =
643 WebRtcAec_system_delay(aecpc->aec) / PART_LEN - aecpc->bufSizeStart;
644 if (overhead_elements == 0) {
645 // Enable the AEC
646 aecpc->startup_phase = 0;
647 } else if (overhead_elements > 0) {
648 // TODO(bjornv): Do we need a check on how much we actually
649 // moved the read pointer? It should always be possible to move
650 // the pointer |overhead_elements| since we have only added data
651 // to the buffer and no delay compensation nor AEC processing
652 // has been done.
653 WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements);
654
655 // Enable the AEC
656 aecpc->startup_phase = 0;
657 }
658 }
659 } else {
660 // AEC is enabled.
Bjorn Volcker71012692015-06-18 11:04:56 +0200661 EstBufDelayNormal(aecpc);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000662
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000663 // Call the AEC.
664 // TODO(bjornv): Re-structure such that we don't have to pass
665 // |aecpc->knownDelay| as input. Change name to something like
666 // |system_buffer_diff|.
peahff63ed22016-01-29 07:46:13 -0800667 WebRtcAec_ProcessFrames(aecpc->aec, nearend, num_bands, nrOfSamples,
668 aecpc->knownDelay, out);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000669 }
670
671 return retVal;
672}
673
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000674static void ProcessExtended(Aec* self,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000675 const float* const* near,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700676 size_t num_bands,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000677 float* const* out,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700678 size_t num_samples,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000679 int16_t reported_delay_ms,
680 int32_t skew) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700681 size_t i;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000682 const int delay_diff_offset = kDelayDiffOffsetSamples;
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +0000683#if defined(WEBRTC_UNTRUSTED_DELAY)
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000684 reported_delay_ms = kFixedDelayMs;
685#else
686 // This is the usual mode where we trust the reported system delay values.
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000687 // Due to the longer filter, we no longer add 10 ms to the reported delay
688 // to reduce chance of non-causality. Instead we apply a minimum here to avoid
689 // issues with the read pointer jumping around needlessly.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000690 reported_delay_ms = reported_delay_ms < kMinTrustedDelayMs
691 ? kMinTrustedDelayMs
692 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000693 // If the reported delay appears to be bogus, we attempt to recover by using
694 // the measured fixed delay values. We use >= here because higher layers
695 // may already clamp to this maximum value, and we would otherwise not
696 // detect it here.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000697 reported_delay_ms = reported_delay_ms >= kMaxTrustedDelayMs
698 ? kFixedDelayMs
699 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000700#endif
701 self->msInSndCardBuf = reported_delay_ms;
702
703 if (!self->farend_started) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000704 for (i = 0; i < num_bands; ++i) {
705 // Only needed if they don't already point to the same place.
706 if (near[i] != out[i]) {
707 memcpy(out[i], near[i], sizeof(near[i][0]) * num_samples);
708 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000709 }
710 return;
711 }
712 if (self->startup_phase) {
713 // In the extended mode, there isn't a startup "phase", just a special
714 // action on the first frame. In the trusted delay case, we'll take the
715 // current reported delay, unless it's less then our conservative
716 // measurement.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000717 int startup_size_ms =
718 reported_delay_ms < kFixedDelayMs ? kFixedDelayMs : reported_delay_ms;
Bjorn Volcker71012692015-06-18 11:04:56 +0200719#if defined(WEBRTC_ANDROID)
Bjorn Volcker1ff218f2015-05-06 12:08:38 +0200720 int target_delay = startup_size_ms * self->rate_factor * 8;
Bjorn Volcker71012692015-06-18 11:04:56 +0200721#else
Bjorn Volcker1ff218f2015-05-06 12:08:38 +0200722 // To avoid putting the AEC in a non-causal state we're being slightly
723 // conservative and scale by 2. On Android we use a fixed delay and
724 // therefore there is no need to scale the target_delay.
Bjorn Volcker71012692015-06-18 11:04:56 +0200725 int target_delay = startup_size_ms * self->rate_factor * 8 / 2;
Bjorn Volcker1ff218f2015-05-06 12:08:38 +0200726#endif
727 int overhead_elements =
728 (WebRtcAec_system_delay(self->aec) - target_delay) / PART_LEN;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000729 WebRtcAec_MoveFarReadPtr(self->aec, overhead_elements);
730 self->startup_phase = 0;
731 }
732
Bjorn Volcker71012692015-06-18 11:04:56 +0200733 EstBufDelayExtended(self);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000734
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000735 {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000736 // |delay_diff_offset| gives us the option to manually rewind the delay on
737 // very low delay platforms which can't be expressed purely through
738 // |reported_delay_ms|.
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000739 const int adjusted_known_delay =
740 WEBRTC_SPL_MAX(0, self->knownDelay + delay_diff_offset);
741
peahff63ed22016-01-29 07:46:13 -0800742 WebRtcAec_ProcessFrames(self->aec, near, num_bands, num_samples,
743 adjusted_known_delay, out);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000744 }
745}
746
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000747static void EstBufDelayNormal(Aec* aecpc) {
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000748 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor;
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000749 int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->aec);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000750 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000751
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000752 // Before we proceed with the delay estimate filtering we:
753 // 1) Compensate for the frame that will be read.
754 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000755 // 3) Compensate for non-causality if needed, since the estimated delay can't
756 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000757
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000758 // 1) Compensating for the frame(s) that will be read/processed.
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000759 current_delay += FRAME_LEN * aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000760
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000761 // 2) Account for resampling frame delay.
762 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
763 current_delay -= kResamplingDelay;
764 }
765
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000766 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000767 if (current_delay < PART_LEN) {
768 current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
769 }
770
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000771 // We use -1 to signal an initialized state in the "extended" implementation;
772 // compensate for that.
773 aecpc->filtDelay = aecpc->filtDelay < 0 ? 0 : aecpc->filtDelay;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000774 aecpc->filtDelay =
peah8df5d4f2016-02-23 14:34:59 -0800775 WEBRTC_SPL_MAX(0, static_cast<int16_t>(0.8 *
776 aecpc->filtDelay +
777 0.2 * current_delay));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000778
779 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
780 if (delay_difference > 224) {
781 if (aecpc->lastDelayDiff < 96) {
782 aecpc->timeForDelayChange = 0;
783 } else {
784 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000785 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000786 } else if (delay_difference < 96 && aecpc->knownDelay > 0) {
787 if (aecpc->lastDelayDiff > 224) {
788 aecpc->timeForDelayChange = 0;
789 } else {
790 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000791 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000792 } else {
793 aecpc->timeForDelayChange = 0;
794 }
795 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000796
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000797 if (aecpc->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000798 aecpc->knownDelay = WEBRTC_SPL_MAX((int)aecpc->filtDelay - 160, 0);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000799 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000800}
niklase@google.com470e71d2011-07-07 08:21:25 +0000801
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000802static void EstBufDelayExtended(Aec* self) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000803 int reported_delay = self->msInSndCardBuf * sampMsNb * self->rate_factor;
804 int current_delay = reported_delay - WebRtcAec_system_delay(self->aec);
805 int delay_difference = 0;
806
807 // Before we proceed with the delay estimate filtering we:
808 // 1) Compensate for the frame that will be read.
809 // 2) Compensate for drift resampling.
810 // 3) Compensate for non-causality if needed, since the estimated delay can't
811 // be negative.
812
813 // 1) Compensating for the frame(s) that will be read/processed.
814 current_delay += FRAME_LEN * self->rate_factor;
815
816 // 2) Account for resampling frame delay.
817 if (self->skewMode == kAecTrue && self->resample == kAecTrue) {
818 current_delay -= kResamplingDelay;
819 }
820
821 // 3) Compensate for non-causality, if needed, by flushing two blocks.
822 if (current_delay < PART_LEN) {
823 current_delay += WebRtcAec_MoveFarReadPtr(self->aec, 2) * PART_LEN;
824 }
825
826 if (self->filtDelay == -1) {
827 self->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay);
828 } else {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000829 self->filtDelay = WEBRTC_SPL_MAX(
peah8df5d4f2016-02-23 14:34:59 -0800830 0, static_cast<int16_t>(0.95 * self->filtDelay + 0.05 * current_delay));
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000831 }
832
833 delay_difference = self->filtDelay - self->knownDelay;
834 if (delay_difference > 384) {
835 if (self->lastDelayDiff < 128) {
836 self->timeForDelayChange = 0;
837 } else {
838 self->timeForDelayChange++;
839 }
840 } else if (delay_difference < 128 && self->knownDelay > 0) {
841 if (self->lastDelayDiff > 384) {
842 self->timeForDelayChange = 0;
843 } else {
844 self->timeForDelayChange++;
845 }
846 } else {
847 self->timeForDelayChange = 0;
848 }
849 self->lastDelayDiff = delay_difference;
850
851 if (self->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000852 self->knownDelay = WEBRTC_SPL_MAX((int)self->filtDelay - 256, 0);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000853 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000854}
peah50e21bd2016-03-05 08:39:21 -0800855} // namespace webrtc