blob: 54ac24dcf43e3e23e1a5b3ba9bc52559593e0920 [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 */
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +000014#include "webrtc/modules/audio_processing/aec/include/echo_cancellation.h"
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +000015
16#include <math.h>
andrew@webrtc.org8594f762011-11-22 00:51:41 +000017#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +000018#include <stdio.h>
19#endif
niklase@google.com470e71d2011-07-07 08:21:25 +000020#include <stdlib.h>
21#include <string.h>
22
andrew@webrtc.org6b630152015-01-15 00:09:53 +000023#include "webrtc/common_audio/ring_buffer.h"
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +000024#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
25#include "webrtc/modules/audio_processing/aec/aec_core.h"
26#include "webrtc/modules/audio_processing/aec/aec_resampler.h"
27#include "webrtc/modules/audio_processing/aec/echo_cancellation_internal.h"
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +000028#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000029
andrew@webrtc.org1760a172013-09-25 23:17:38 +000030// Measured delays [ms]
31// Device Chrome GTP
32// MacBook Air 10
33// MacBook Retina 10 100
34// MacPro 30?
35//
36// Win7 Desktop 70 80?
37// Win7 T430s 110
38// Win8 T420s 70
39//
40// Daisy 50
41// Pixel (w/ preproc?) 240
42// Pixel (w/o preproc?) 110 110
43
44// The extended filter mode gives us the flexibility to ignore the system's
45// reported delays. We do this for platforms which we believe provide results
46// which are incompatible with the AEC's expectations. Based on measurements
47// (some provided above) we set a conservative (i.e. lower than measured)
48// fixed delay.
49//
50// WEBRTC_UNTRUSTED_DELAY will only have an impact when |extended_filter_mode|
51// is enabled. See the note along with |DelayCorrection| in
52// echo_cancellation_impl.h for more details on the mode.
53//
54// Justification:
55// Chromium/Mac: Here, the true latency is so low (~10-20 ms), that it plays
56// havoc with the AEC's buffering. To avoid this, we set a fixed delay of 20 ms
57// and then compensate by rewinding by 10 ms (in wideband) through
58// kDelayDiffOffsetSamples. This trick does not seem to work for larger rewind
59// values, but fortunately this is sufficient.
60//
61// Chromium/Linux(ChromeOS): The values we get on this platform don't correspond
62// well to reality. The variance doesn't match the AEC's buffer changes, and the
63// bulk values tend to be too low. However, the range across different hardware
64// appears to be too large to choose a single value.
65//
66// GTP/Linux(ChromeOS): TBD, but for the moment we will trust the values.
67#if defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_MAC)
68#define WEBRTC_UNTRUSTED_DELAY
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +000069#endif
andrew@webrtc.orgacb00502013-10-04 16:59:17 +000070
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +000071#if defined(WEBRTC_UNTRUSTED_DELAY) && defined(WEBRTC_MAC)
andrew@webrtc.orgacb00502013-10-04 16:59:17 +000072static const int kDelayDiffOffsetSamples = -160;
73#else
74// Not enabled for now.
75static const int kDelayDiffOffsetSamples = 0;
76#endif
andrew@webrtc.org1760a172013-09-25 23:17:38 +000077
78#if defined(WEBRTC_MAC)
79static const int kFixedDelayMs = 20;
andrew@webrtc.org1760a172013-09-25 23:17:38 +000080#else
andrew@webrtc.org1760a172013-09-25 23:17:38 +000081static const int kFixedDelayMs = 50;
andrew@webrtc.org1760a172013-09-25 23:17:38 +000082#endif
andrew@webrtc.orgc2e471d2013-10-15 02:11:21 +000083#if !defined(WEBRTC_UNTRUSTED_DELAY)
andrew@webrtc.org1760a172013-09-25 23:17:38 +000084static const int kMinTrustedDelayMs = 20;
andrew@webrtc.orgc2e471d2013-10-15 02:11:21 +000085#endif
andrew@webrtc.org1760a172013-09-25 23:17:38 +000086static const int kMaxTrustedDelayMs = 500;
87
niklase@google.com470e71d2011-07-07 08:21:25 +000088// Maximum length of resampled signal. Must be an integer multiple of frames
89// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
90// The factor of 2 handles wb, and the + 1 is as a safety margin
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000091// TODO(bjornv): Replace with kResamplerBufferSize
niklase@google.com470e71d2011-07-07 08:21:25 +000092#define MAX_RESAMP_LEN (5 * FRAME_LEN)
93
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000094static const int kMaxBufSizeStart = 62; // In partitions
andrew@webrtc.org13b2d462013-10-08 23:41:42 +000095static const int sampMsNb = 8; // samples per ms in nb
niklase@google.com470e71d2011-07-07 08:21:25 +000096static const int initCheck = 42;
97
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000098#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +000099int webrtc_aec_instance_count = 0;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000100#endif
101
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000102// Estimates delay to set the position of the far-end buffer read pointer
niklase@google.com470e71d2011-07-07 08:21:25 +0000103// (controlled by knownDelay)
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000104static void EstBufDelayNormal(Aec* aecInst);
105static void EstBufDelayExtended(Aec* aecInst);
106static int ProcessNormal(Aec* self,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000107 const float* const* near,
108 int num_bands,
109 float* const* out,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000110 int16_t num_samples,
111 int16_t reported_delay_ms,
112 int32_t skew);
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000113static void ProcessExtended(Aec* self,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000114 const float* const* near,
115 int num_bands,
116 float* const* out,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000117 int16_t num_samples,
118 int16_t reported_delay_ms,
119 int32_t skew);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000121int32_t WebRtcAec_Create(void** aecInst) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000122 Aec* aecpc;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000123 if (aecInst == NULL) {
124 return -1;
125 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000127 aecpc = malloc(sizeof(Aec));
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000128 *aecInst = aecpc;
129 if (aecpc == NULL) {
130 return -1;
131 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000133 if (WebRtcAec_CreateAec(&aecpc->aec) == -1) {
134 WebRtcAec_Free(aecpc);
135 aecpc = NULL;
136 return -1;
137 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000139 if (WebRtcAec_CreateResampler(&aecpc->resampler) == -1) {
140 WebRtcAec_Free(aecpc);
141 aecpc = NULL;
142 return -1;
143 }
144 // Create far-end pre-buffer. The buffer size has to be large enough for
145 // largest possible drift compensation (kResamplerBufferSize) + "almost" an
146 // FFT buffer (PART_LEN2 - 1).
147 aecpc->far_pre_buf =
148 WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize, sizeof(float));
149 if (!aecpc->far_pre_buf) {
150 WebRtcAec_Free(aecpc);
151 aecpc = NULL;
152 return -1;
153 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000155 aecpc->initFlag = 0;
156 aecpc->lastError = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000158#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000159 {
160 char filename[64];
161 sprintf(filename, "aec_buf%d.dat", webrtc_aec_instance_count);
162 aecpc->bufFile = fopen(filename, "wb");
163 sprintf(filename, "aec_skew%d.dat", webrtc_aec_instance_count);
164 aecpc->skewFile = fopen(filename, "wb");
165 sprintf(filename, "aec_delay%d.dat", webrtc_aec_instance_count);
166 aecpc->delayFile = fopen(filename, "wb");
167 webrtc_aec_instance_count++;
168 }
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000169#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000170
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000171 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000172}
173
Bjorn Volckerf6a99e62015-04-10 07:56:57 +0200174void WebRtcAec_Free(void* aecInst) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000175 Aec* aecpc = aecInst;
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000177 if (aecpc == NULL) {
Bjorn Volckerf6a99e62015-04-10 07:56:57 +0200178 return;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000179 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000180
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000181 WebRtc_FreeBuffer(aecpc->far_pre_buf);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000182
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000183#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000184 fclose(aecpc->bufFile);
185 fclose(aecpc->skewFile);
186 fclose(aecpc->delayFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000187#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000189 WebRtcAec_FreeAec(aecpc->aec);
190 WebRtcAec_FreeResampler(aecpc->resampler);
191 free(aecpc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000192}
193
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000194int32_t WebRtcAec_Init(void* aecInst, int32_t sampFreq, int32_t scSampFreq) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000195 Aec* aecpc = aecInst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000196 AecConfig aecConfig;
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000198 if (sampFreq != 8000 &&
199 sampFreq != 16000 &&
200 sampFreq != 32000 &&
201 sampFreq != 48000) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000202 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
203 return -1;
204 }
205 aecpc->sampFreq = sampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000206
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000207 if (scSampFreq < 1 || scSampFreq > 96000) {
208 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
209 return -1;
210 }
211 aecpc->scSampFreq = scSampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000213 // Initialize echo canceller core
214 if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) {
215 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
216 return -1;
217 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000218
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000219 if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) {
220 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
221 return -1;
222 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000223
andrew@webrtc.org6b630152015-01-15 00:09:53 +0000224 WebRtc_InitBuffer(aecpc->far_pre_buf);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000225 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap.
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000226
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000227 aecpc->initFlag = initCheck; // indicates that initialization has been done
niklase@google.com470e71d2011-07-07 08:21:25 +0000228
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000229 if (aecpc->sampFreq == 32000 || aecpc->sampFreq == 48000) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000230 aecpc->splitSampFreq = 16000;
231 } else {
232 aecpc->splitSampFreq = sampFreq;
233 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000234
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000235 aecpc->delayCtr = 0;
236 aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq;
237 // Sampling frequency multiplier (SWB is processed as 160 frame size).
238 aecpc->rate_factor = aecpc->splitSampFreq / 8000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000239
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000240 aecpc->sum = 0;
241 aecpc->counter = 0;
242 aecpc->checkBuffSize = 1;
243 aecpc->firstVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000244
bjornv@webrtc.org059488f2014-04-29 08:09:26 +0000245 aecpc->startup_phase = WebRtcAec_reported_delay_enabled(aecpc->aec);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000246 aecpc->bufSizeStart = 0;
247 aecpc->checkBufSizeCtr = 0;
248 aecpc->msInSndCardBuf = 0;
249 aecpc->filtDelay = -1; // -1 indicates an initialized state.
250 aecpc->timeForDelayChange = 0;
251 aecpc->knownDelay = 0;
252 aecpc->lastDelayDiff = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000253
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000254 aecpc->skewFrCtr = 0;
255 aecpc->resample = kAecFalse;
256 aecpc->highSkewCtr = 0;
257 aecpc->skew = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000258
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000259 aecpc->farend_started = 0;
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000260
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000261 // Default settings.
262 aecConfig.nlpMode = kAecNlpModerate;
263 aecConfig.skewMode = kAecFalse;
264 aecConfig.metricsMode = kAecFalse;
265 aecConfig.delay_logging = kAecFalse;
niklase@google.com470e71d2011-07-07 08:21:25 +0000266
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000267 if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
268 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
269 return -1;
270 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000271
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000272 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000273}
274
275// only buffer L band for farend
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000276int32_t WebRtcAec_BufferFarend(void* aecInst,
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000277 const float* farend,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000278 int16_t nrOfSamples) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000279 Aec* aecpc = aecInst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000280 int newNrOfSamples = (int)nrOfSamples;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000281 float new_farend[MAX_RESAMP_LEN];
282 const float* farend_ptr = farend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000283
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000284 if (farend == NULL) {
285 aecpc->lastError = AEC_NULL_POINTER_ERROR;
286 return -1;
287 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000288
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000289 if (aecpc->initFlag != initCheck) {
290 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
291 return -1;
292 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000293
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000294 // number of samples == 160 for SWB input
295 if (nrOfSamples != 80 && nrOfSamples != 160) {
296 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
297 return -1;
298 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000299
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000300 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
301 // Resample and get a new number of samples
302 WebRtcAec_ResampleLinear(aecpc->resampler,
303 farend,
304 nrOfSamples,
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000305 aecpc->skew,
306 new_farend,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000307 &newNrOfSamples);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000308 farend_ptr = new_farend;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000309 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000310
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000311 aecpc->farend_started = 1;
312 WebRtcAec_SetSystemDelay(aecpc->aec,
313 WebRtcAec_system_delay(aecpc->aec) + newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000314
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000315 // Write the time-domain data to |far_pre_buf|.
316 WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_ptr, (size_t)newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000317
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000318 // Transform to frequency domain if we have enough data.
319 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
320 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000321 {
andrew@webrtc.orge65d9d92015-01-21 22:05:12 +0000322 float* ptmp = NULL;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000323 float tmp[PART_LEN2];
324 WebRtc_ReadBuffer(aecpc->far_pre_buf, (void**)&ptmp, tmp, PART_LEN2);
325 WebRtcAec_BufferFarendPartition(aecpc->aec, ptmp);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000326#ifdef WEBRTC_AEC_DEBUG_DUMP
327 WebRtc_WriteBuffer(
328 WebRtcAec_far_time_buf(aecpc->aec), &ptmp[PART_LEN], 1);
329#endif
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000330 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000331
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000332 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
333 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000334 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000335
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000336 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000337}
338
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000339int32_t WebRtcAec_Process(void* aecInst,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000340 const float* const* nearend,
341 int num_bands,
342 float* const* out,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000343 int16_t nrOfSamples,
344 int16_t msInSndCardBuf,
345 int32_t skew) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000346 Aec* aecpc = aecInst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000347 int32_t retVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000348
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000349 if (out == NULL) {
350 aecpc->lastError = AEC_NULL_POINTER_ERROR;
351 return -1;
352 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000353
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000354 if (aecpc->initFlag != initCheck) {
355 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
356 return -1;
357 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000358
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000359 // number of samples == 160 for SWB input
360 if (nrOfSamples != 80 && nrOfSamples != 160) {
361 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
362 return -1;
363 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000364
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000365 if (msInSndCardBuf < 0) {
366 msInSndCardBuf = 0;
367 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
368 retVal = -1;
369 } else if (msInSndCardBuf > kMaxTrustedDelayMs) {
370 // The clamping is now done in ProcessExtended/Normal().
371 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
372 retVal = -1;
373 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000374
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000375 // This returns the value of aec->extended_filter_enabled.
376 if (WebRtcAec_delay_correction_enabled(aecpc->aec)) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000377 ProcessExtended(aecpc,
378 nearend,
379 num_bands,
380 out,
381 nrOfSamples,
382 msInSndCardBuf,
383 skew);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000384 } else {
385 if (ProcessNormal(aecpc,
386 nearend,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000387 num_bands,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000388 out,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000389 nrOfSamples,
390 msInSndCardBuf,
391 skew) != 0) {
392 retVal = -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000393 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000394 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000395
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000396#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000397 {
398 int16_t far_buf_size_ms = (int16_t)(WebRtcAec_system_delay(aecpc->aec) /
399 (sampMsNb * aecpc->rate_factor));
400 (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile);
401 (void)fwrite(
402 &aecpc->knownDelay, sizeof(aecpc->knownDelay), 1, aecpc->delayFile);
403 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000404#endif
405
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000406 return retVal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000407}
408
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000409int WebRtcAec_set_config(void* handle, AecConfig config) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000410 Aec* self = (Aec*)handle;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000411 if (self->initFlag != initCheck) {
412 self->lastError = AEC_UNINITIALIZED_ERROR;
413 return -1;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000414 }
415
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000416 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
417 self->lastError = AEC_BAD_PARAMETER_ERROR;
418 return -1;
419 }
420 self->skewMode = config.skewMode;
421
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000422 if (config.nlpMode != kAecNlpConservative &&
423 config.nlpMode != kAecNlpModerate &&
424 config.nlpMode != kAecNlpAggressive) {
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000425 self->lastError = AEC_BAD_PARAMETER_ERROR;
426 return -1;
427 }
428
429 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
430 self->lastError = AEC_BAD_PARAMETER_ERROR;
431 return -1;
432 }
433
434 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
435 self->lastError = AEC_BAD_PARAMETER_ERROR;
436 return -1;
437 }
438
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000439 WebRtcAec_SetConfigCore(
440 self->aec, config.nlpMode, config.metricsMode, config.delay_logging);
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000441 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000442}
443
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000444int WebRtcAec_get_echo_status(void* handle, int* status) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000445 Aec* self = (Aec*)handle;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000446 if (status == NULL) {
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000447 self->lastError = AEC_NULL_POINTER_ERROR;
448 return -1;
449 }
450 if (self->initFlag != initCheck) {
451 self->lastError = AEC_UNINITIALIZED_ERROR;
452 return -1;
453 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000454
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000455 *status = WebRtcAec_echo_state(self->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000456
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000457 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000458}
459
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000460int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) {
461 const float kUpWeight = 0.7f;
462 float dtmp;
463 int stmp;
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000464 Aec* self = (Aec*)handle;
bjornv@webrtc.orgcea70f42013-02-19 21:03:10 +0000465 Stats erl;
466 Stats erle;
467 Stats a_nlp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000468
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000469 if (handle == NULL) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000470 return -1;
471 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000472 if (metrics == NULL) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000473 self->lastError = AEC_NULL_POINTER_ERROR;
474 return -1;
475 }
476 if (self->initFlag != initCheck) {
477 self->lastError = AEC_UNINITIALIZED_ERROR;
478 return -1;
479 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000480
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000481 WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000482
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000483 // ERL
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000484 metrics->erl.instant = (int)erl.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000485
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000486 if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000487 // Use a mix between regular average and upper part average.
488 dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000489 metrics->erl.average = (int)dtmp;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000490 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000491 metrics->erl.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000492 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000493
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000494 metrics->erl.max = (int)erl.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000495
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000496 if (erl.min < (kOffsetLevel * (-1))) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000497 metrics->erl.min = (int)erl.min;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000498 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000499 metrics->erl.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 // ERLE
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000503 metrics->erle.instant = (int)erle.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000504
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000505 if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000506 // Use a mix between regular average and upper part average.
507 dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000508 metrics->erle.average = (int)dtmp;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000509 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000510 metrics->erle.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000511 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000512
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000513 metrics->erle.max = (int)erle.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000514
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000515 if (erle.min < (kOffsetLevel * (-1))) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000516 metrics->erle.min = (int)erle.min;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000517 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000518 metrics->erle.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000519 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000520
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000521 // RERL
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000522 if ((metrics->erl.average > kOffsetLevel) &&
523 (metrics->erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000524 stmp = metrics->erl.average + metrics->erle.average;
525 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000526 stmp = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000527 }
528 metrics->rerl.average = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000529
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000530 // No other statistics needed, but returned for completeness.
531 metrics->rerl.instant = stmp;
532 metrics->rerl.max = stmp;
533 metrics->rerl.min = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000534
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000535 // A_NLP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000536 metrics->aNlp.instant = (int)a_nlp.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000537
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000538 if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000539 // Use a mix between regular average and upper part average.
540 dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000541 metrics->aNlp.average = (int)dtmp;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000542 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000543 metrics->aNlp.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000544 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000545
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000546 metrics->aNlp.max = (int)a_nlp.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000547
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000548 if (a_nlp.min < (kOffsetLevel * (-1))) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000549 metrics->aNlp.min = (int)a_nlp.min;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000550 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000551 metrics->aNlp.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000552 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000553
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000554 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000555}
556
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000557int WebRtcAec_GetDelayMetrics(void* handle,
558 int* median,
559 int* std,
560 float* fraction_poor_delays) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000561 Aec* self = handle;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000562 if (median == NULL) {
563 self->lastError = AEC_NULL_POINTER_ERROR;
564 return -1;
565 }
566 if (std == NULL) {
567 self->lastError = AEC_NULL_POINTER_ERROR;
568 return -1;
569 }
570 if (self->initFlag != initCheck) {
571 self->lastError = AEC_UNINITIALIZED_ERROR;
572 return -1;
573 }
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000574 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std,
575 fraction_poor_delays) ==
576 -1) {
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000577 // Logging disabled.
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000578 self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR;
579 return -1;
580 }
581
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000582 return 0;
583}
584
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000585int32_t WebRtcAec_get_error_code(void* aecInst) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000586 Aec* aecpc = aecInst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000587 return aecpc->lastError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000588}
589
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000590AecCore* WebRtcAec_aec_core(void* handle) {
591 if (!handle) {
592 return NULL;
593 }
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000594 return ((Aec*)handle)->aec;
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000595}
596
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000597static int ProcessNormal(Aec* aecpc,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000598 const float* const* nearend,
599 int num_bands,
600 float* const* out,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000601 int16_t nrOfSamples,
602 int16_t msInSndCardBuf,
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000603 int32_t skew) {
604 int retVal = 0;
605 short i;
606 short nBlocks10ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000607 // Limit resampling to doubling/halving of signal
608 const float minSkewEst = -0.5f;
609 const float maxSkewEst = 1.0f;
610
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000611 msInSndCardBuf =
612 msInSndCardBuf > kMaxTrustedDelayMs ? kMaxTrustedDelayMs : msInSndCardBuf;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000613 // TODO(andrew): we need to investigate if this +10 is really wanted.
614 msInSndCardBuf += 10;
615 aecpc->msInSndCardBuf = msInSndCardBuf;
616
617 if (aecpc->skewMode == kAecTrue) {
618 if (aecpc->skewFrCtr < 25) {
619 aecpc->skewFrCtr++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000620 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000621 retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
622 if (retVal == -1) {
623 aecpc->skew = 0;
624 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
625 }
626
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000627 aecpc->skew /= aecpc->sampFactor * nrOfSamples;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000628
629 if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
630 aecpc->resample = kAecFalse;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000631 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000632 aecpc->resample = kAecTrue;
633 }
634
635 if (aecpc->skew < minSkewEst) {
636 aecpc->skew = minSkewEst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000637 } else if (aecpc->skew > maxSkewEst) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000638 aecpc->skew = maxSkewEst;
639 }
640
641#ifdef WEBRTC_AEC_DEBUG_DUMP
642 (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile);
643#endif
644 }
645 }
646
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000647 nBlocks10ms = nrOfSamples / (FRAME_LEN * aecpc->rate_factor);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000648
649 if (aecpc->startup_phase) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000650 for (i = 0; i < num_bands; ++i) {
651 // Only needed if they don't already point to the same place.
652 if (nearend[i] != out[i]) {
653 memcpy(out[i], nearend[i], sizeof(nearend[i][0]) * nrOfSamples);
654 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000655 }
656
657 // The AEC is in the start up mode
658 // AEC is disabled until the system delay is OK
659
660 // Mechanism to ensure that the system delay is reasonably stable.
661 if (aecpc->checkBuffSize) {
662 aecpc->checkBufSizeCtr++;
663 // Before we fill up the far-end buffer we require the system delay
664 // to be stable (+/-8 ms) compared to the first value. This
665 // comparison is made during the following 6 consecutive 10 ms
666 // blocks. If it seems to be stable then we start to fill up the
667 // far-end buffer.
668 if (aecpc->counter == 0) {
669 aecpc->firstVal = aecpc->msInSndCardBuf;
670 aecpc->sum = 0;
671 }
672
673 if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000674 WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000675 aecpc->sum += aecpc->msInSndCardBuf;
676 aecpc->counter++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000677 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000678 aecpc->counter = 0;
679 }
680
681 if (aecpc->counter * nBlocks10ms >= 6) {
682 // The far-end buffer size is determined in partitions of
683 // PART_LEN samples. Use 75% of the average value of the system
684 // delay as buffer size to start with.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000685 aecpc->bufSizeStart =
686 WEBRTC_SPL_MIN((3 * aecpc->sum * aecpc->rate_factor * 8) /
687 (4 * aecpc->counter * PART_LEN),
688 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000689 // Buffer size has now been determined.
690 aecpc->checkBuffSize = 0;
691 }
692
693 if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
694 // For really bad systems, don't disable the echo canceller for
695 // more than 0.5 sec.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000696 aecpc->bufSizeStart = WEBRTC_SPL_MIN(
697 (aecpc->msInSndCardBuf * aecpc->rate_factor * 3) / 40,
698 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000699 aecpc->checkBuffSize = 0;
700 }
701 }
702
703 // If |checkBuffSize| changed in the if-statement above.
704 if (!aecpc->checkBuffSize) {
705 // The system delay is now reasonably stable (or has been unstable
706 // for too long). When the far-end buffer is filled with
707 // approximately the same amount of data as reported by the system
708 // we end the startup phase.
709 int overhead_elements =
710 WebRtcAec_system_delay(aecpc->aec) / PART_LEN - aecpc->bufSizeStart;
711 if (overhead_elements == 0) {
712 // Enable the AEC
713 aecpc->startup_phase = 0;
714 } else if (overhead_elements > 0) {
715 // TODO(bjornv): Do we need a check on how much we actually
716 // moved the read pointer? It should always be possible to move
717 // the pointer |overhead_elements| since we have only added data
718 // to the buffer and no delay compensation nor AEC processing
719 // has been done.
720 WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements);
721
722 // Enable the AEC
723 aecpc->startup_phase = 0;
724 }
725 }
726 } else {
727 // AEC is enabled.
bjornv@webrtc.orge9d37602014-04-23 13:20:07 +0000728 if (WebRtcAec_reported_delay_enabled(aecpc->aec)) {
729 EstBufDelayNormal(aecpc);
730 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000731
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000732 // Call the AEC.
733 // TODO(bjornv): Re-structure such that we don't have to pass
734 // |aecpc->knownDelay| as input. Change name to something like
735 // |system_buffer_diff|.
736 WebRtcAec_ProcessFrames(aecpc->aec,
737 nearend,
738 num_bands,
739 nrOfSamples,
740 aecpc->knownDelay,
741 out);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000742 }
743
744 return retVal;
745}
746
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000747static void ProcessExtended(Aec* self,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000748 const float* const* near,
749 int num_bands,
750 float* const* out,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000751 int16_t num_samples,
752 int16_t reported_delay_ms,
753 int32_t skew) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000754 int i;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000755 const int delay_diff_offset = kDelayDiffOffsetSamples;
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +0000756#if defined(WEBRTC_UNTRUSTED_DELAY)
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000757 reported_delay_ms = kFixedDelayMs;
758#else
759 // This is the usual mode where we trust the reported system delay values.
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000760 // Due to the longer filter, we no longer add 10 ms to the reported delay
761 // to reduce chance of non-causality. Instead we apply a minimum here to avoid
762 // issues with the read pointer jumping around needlessly.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000763 reported_delay_ms = reported_delay_ms < kMinTrustedDelayMs
764 ? kMinTrustedDelayMs
765 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000766 // If the reported delay appears to be bogus, we attempt to recover by using
767 // the measured fixed delay values. We use >= here because higher layers
768 // may already clamp to this maximum value, and we would otherwise not
769 // detect it here.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000770 reported_delay_ms = reported_delay_ms >= kMaxTrustedDelayMs
771 ? kFixedDelayMs
772 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000773#endif
774 self->msInSndCardBuf = reported_delay_ms;
775
776 if (!self->farend_started) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000777 for (i = 0; i < num_bands; ++i) {
778 // Only needed if they don't already point to the same place.
779 if (near[i] != out[i]) {
780 memcpy(out[i], near[i], sizeof(near[i][0]) * num_samples);
781 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000782 }
783 return;
784 }
785 if (self->startup_phase) {
786 // In the extended mode, there isn't a startup "phase", just a special
787 // action on the first frame. In the trusted delay case, we'll take the
788 // current reported delay, unless it's less then our conservative
789 // measurement.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000790 int startup_size_ms =
791 reported_delay_ms < kFixedDelayMs ? kFixedDelayMs : reported_delay_ms;
Bjorn Volcker1ff218f2015-05-06 12:08:38 +0200792 int target_delay = startup_size_ms * self->rate_factor * 8;
793#if !defined(WEBRTC_ANDROID)
794 // To avoid putting the AEC in a non-causal state we're being slightly
795 // conservative and scale by 2. On Android we use a fixed delay and
796 // therefore there is no need to scale the target_delay.
797 target_delay /= 2;
798#endif
799 int overhead_elements =
800 (WebRtcAec_system_delay(self->aec) - target_delay) / PART_LEN;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000801 WebRtcAec_MoveFarReadPtr(self->aec, overhead_elements);
802 self->startup_phase = 0;
803 }
804
bjornv@webrtc.orge9d37602014-04-23 13:20:07 +0000805 if (WebRtcAec_reported_delay_enabled(self->aec)) {
806 EstBufDelayExtended(self);
807 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000808
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000809 {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000810 // |delay_diff_offset| gives us the option to manually rewind the delay on
811 // very low delay platforms which can't be expressed purely through
812 // |reported_delay_ms|.
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000813 const int adjusted_known_delay =
814 WEBRTC_SPL_MAX(0, self->knownDelay + delay_diff_offset);
815
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000816 WebRtcAec_ProcessFrames(self->aec,
817 near,
818 num_bands,
819 num_samples,
820 adjusted_known_delay,
821 out);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000822 }
823}
824
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000825static void EstBufDelayNormal(Aec* aecpc) {
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000826 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor;
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000827 int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->aec);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000828 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000829
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000830 // Before we proceed with the delay estimate filtering we:
831 // 1) Compensate for the frame that will be read.
832 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000833 // 3) Compensate for non-causality if needed, since the estimated delay can't
834 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000835
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000836 // 1) Compensating for the frame(s) that will be read/processed.
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000837 current_delay += FRAME_LEN * aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000838
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000839 // 2) Account for resampling frame delay.
840 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
841 current_delay -= kResamplingDelay;
842 }
843
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000844 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000845 if (current_delay < PART_LEN) {
846 current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
847 }
848
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000849 // We use -1 to signal an initialized state in the "extended" implementation;
850 // compensate for that.
851 aecpc->filtDelay = aecpc->filtDelay < 0 ? 0 : aecpc->filtDelay;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000852 aecpc->filtDelay =
853 WEBRTC_SPL_MAX(0, (short)(0.8 * aecpc->filtDelay + 0.2 * current_delay));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000854
855 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
856 if (delay_difference > 224) {
857 if (aecpc->lastDelayDiff < 96) {
858 aecpc->timeForDelayChange = 0;
859 } else {
860 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000861 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000862 } else if (delay_difference < 96 && aecpc->knownDelay > 0) {
863 if (aecpc->lastDelayDiff > 224) {
864 aecpc->timeForDelayChange = 0;
865 } else {
866 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000867 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000868 } else {
869 aecpc->timeForDelayChange = 0;
870 }
871 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000872
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000873 if (aecpc->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000874 aecpc->knownDelay = WEBRTC_SPL_MAX((int)aecpc->filtDelay - 160, 0);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000875 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000876}
niklase@google.com470e71d2011-07-07 08:21:25 +0000877
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000878static void EstBufDelayExtended(Aec* self) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000879 int reported_delay = self->msInSndCardBuf * sampMsNb * self->rate_factor;
880 int current_delay = reported_delay - WebRtcAec_system_delay(self->aec);
881 int delay_difference = 0;
882
883 // Before we proceed with the delay estimate filtering we:
884 // 1) Compensate for the frame that will be read.
885 // 2) Compensate for drift resampling.
886 // 3) Compensate for non-causality if needed, since the estimated delay can't
887 // be negative.
888
889 // 1) Compensating for the frame(s) that will be read/processed.
890 current_delay += FRAME_LEN * self->rate_factor;
891
892 // 2) Account for resampling frame delay.
893 if (self->skewMode == kAecTrue && self->resample == kAecTrue) {
894 current_delay -= kResamplingDelay;
895 }
896
897 // 3) Compensate for non-causality, if needed, by flushing two blocks.
898 if (current_delay < PART_LEN) {
899 current_delay += WebRtcAec_MoveFarReadPtr(self->aec, 2) * PART_LEN;
900 }
901
902 if (self->filtDelay == -1) {
903 self->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay);
904 } else {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000905 self->filtDelay = WEBRTC_SPL_MAX(
906 0, (short)(0.95 * self->filtDelay + 0.05 * current_delay));
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000907 }
908
909 delay_difference = self->filtDelay - self->knownDelay;
910 if (delay_difference > 384) {
911 if (self->lastDelayDiff < 128) {
912 self->timeForDelayChange = 0;
913 } else {
914 self->timeForDelayChange++;
915 }
916 } else if (delay_difference < 128 && self->knownDelay > 0) {
917 if (self->lastDelayDiff > 384) {
918 self->timeForDelayChange = 0;
919 } else {
920 self->timeForDelayChange++;
921 }
922 } else {
923 self->timeForDelayChange = 0;
924 }
925 self->lastDelayDiff = delay_difference;
926
927 if (self->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000928 self->knownDelay = WEBRTC_SPL_MAX((int)self->filtDelay - 256, 0);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000929 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000930}