blob: 016c45538fbf45d1d1645aa5f6fce0b3f9b04131 [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
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000174int32_t 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) {
178 return -1;
179 }
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
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000193 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000194}
195
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000196int32_t WebRtcAec_Init(void* aecInst, int32_t sampFreq, int32_t scSampFreq) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000197 Aec* aecpc = aecInst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000198 AecConfig aecConfig;
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000200 if (sampFreq != 8000 &&
201 sampFreq != 16000 &&
202 sampFreq != 32000 &&
203 sampFreq != 48000) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000204 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
205 return -1;
206 }
207 aecpc->sampFreq = sampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000209 if (scSampFreq < 1 || scSampFreq > 96000) {
210 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
211 return -1;
212 }
213 aecpc->scSampFreq = scSampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000214
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000215 // Initialize echo canceller core
216 if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) {
217 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
218 return -1;
219 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000220
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000221 if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) {
222 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
223 return -1;
224 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000225
andrew@webrtc.org6b630152015-01-15 00:09:53 +0000226 WebRtc_InitBuffer(aecpc->far_pre_buf);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000227 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap.
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000228
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000229 aecpc->initFlag = initCheck; // indicates that initialization has been done
niklase@google.com470e71d2011-07-07 08:21:25 +0000230
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000231 if (aecpc->sampFreq == 32000 || aecpc->sampFreq == 48000) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000232 aecpc->splitSampFreq = 16000;
233 } else {
234 aecpc->splitSampFreq = sampFreq;
235 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000237 aecpc->delayCtr = 0;
238 aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq;
239 // Sampling frequency multiplier (SWB is processed as 160 frame size).
240 aecpc->rate_factor = aecpc->splitSampFreq / 8000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000241
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000242 aecpc->sum = 0;
243 aecpc->counter = 0;
244 aecpc->checkBuffSize = 1;
245 aecpc->firstVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000246
bjornv@webrtc.org059488f2014-04-29 08:09:26 +0000247 aecpc->startup_phase = WebRtcAec_reported_delay_enabled(aecpc->aec);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000248 aecpc->bufSizeStart = 0;
249 aecpc->checkBufSizeCtr = 0;
250 aecpc->msInSndCardBuf = 0;
251 aecpc->filtDelay = -1; // -1 indicates an initialized state.
252 aecpc->timeForDelayChange = 0;
253 aecpc->knownDelay = 0;
254 aecpc->lastDelayDiff = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000255
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000256 aecpc->skewFrCtr = 0;
257 aecpc->resample = kAecFalse;
258 aecpc->highSkewCtr = 0;
259 aecpc->skew = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000260
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000261 aecpc->farend_started = 0;
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000262
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000263 // Default settings.
264 aecConfig.nlpMode = kAecNlpModerate;
265 aecConfig.skewMode = kAecFalse;
266 aecConfig.metricsMode = kAecFalse;
267 aecConfig.delay_logging = kAecFalse;
niklase@google.com470e71d2011-07-07 08:21:25 +0000268
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000269 if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
270 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
271 return -1;
272 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000273
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000274 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000275}
276
277// only buffer L band for farend
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000278int32_t WebRtcAec_BufferFarend(void* aecInst,
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000279 const float* farend,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000280 int16_t nrOfSamples) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000281 Aec* aecpc = aecInst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000282 int newNrOfSamples = (int)nrOfSamples;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000283 float new_farend[MAX_RESAMP_LEN];
284 const float* farend_ptr = farend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000285
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000286 if (farend == NULL) {
287 aecpc->lastError = AEC_NULL_POINTER_ERROR;
288 return -1;
289 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000290
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000291 if (aecpc->initFlag != initCheck) {
292 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
293 return -1;
294 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000295
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000296 // number of samples == 160 for SWB input
297 if (nrOfSamples != 80 && nrOfSamples != 160) {
298 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
299 return -1;
300 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000301
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000302 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
303 // Resample and get a new number of samples
304 WebRtcAec_ResampleLinear(aecpc->resampler,
305 farend,
306 nrOfSamples,
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000307 aecpc->skew,
308 new_farend,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000309 &newNrOfSamples);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000310 farend_ptr = new_farend;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000311 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000312
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000313 aecpc->farend_started = 1;
314 WebRtcAec_SetSystemDelay(aecpc->aec,
315 WebRtcAec_system_delay(aecpc->aec) + newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000316
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000317 // Write the time-domain data to |far_pre_buf|.
318 WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_ptr, (size_t)newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000319
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000320 // Transform to frequency domain if we have enough data.
321 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
322 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000323 {
andrew@webrtc.orge65d9d92015-01-21 22:05:12 +0000324 float* ptmp = NULL;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000325 float tmp[PART_LEN2];
326 WebRtc_ReadBuffer(aecpc->far_pre_buf, (void**)&ptmp, tmp, PART_LEN2);
327 WebRtcAec_BufferFarendPartition(aecpc->aec, ptmp);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000328#ifdef WEBRTC_AEC_DEBUG_DUMP
329 WebRtc_WriteBuffer(
330 WebRtcAec_far_time_buf(aecpc->aec), &ptmp[PART_LEN], 1);
331#endif
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000332 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000333
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000334 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
335 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000336 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000337
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000338 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000339}
340
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000341int32_t WebRtcAec_Process(void* aecInst,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000342 const float* const* nearend,
343 int num_bands,
344 float* const* out,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000345 int16_t nrOfSamples,
346 int16_t msInSndCardBuf,
347 int32_t skew) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000348 Aec* aecpc = aecInst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000349 int32_t retVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000350
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000351 if (out == NULL) {
352 aecpc->lastError = AEC_NULL_POINTER_ERROR;
353 return -1;
354 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000355
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000356 if (aecpc->initFlag != initCheck) {
357 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
358 return -1;
359 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000360
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000361 // number of samples == 160 for SWB input
362 if (nrOfSamples != 80 && nrOfSamples != 160) {
363 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
364 return -1;
365 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000366
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000367 if (msInSndCardBuf < 0) {
368 msInSndCardBuf = 0;
369 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
370 retVal = -1;
371 } else if (msInSndCardBuf > kMaxTrustedDelayMs) {
372 // The clamping is now done in ProcessExtended/Normal().
373 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
374 retVal = -1;
375 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000376
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000377 // This returns the value of aec->extended_filter_enabled.
378 if (WebRtcAec_delay_correction_enabled(aecpc->aec)) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000379 ProcessExtended(aecpc,
380 nearend,
381 num_bands,
382 out,
383 nrOfSamples,
384 msInSndCardBuf,
385 skew);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000386 } else {
387 if (ProcessNormal(aecpc,
388 nearend,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000389 num_bands,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000390 out,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000391 nrOfSamples,
392 msInSndCardBuf,
393 skew) != 0) {
394 retVal = -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000395 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000396 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000397
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000398#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000399 {
400 int16_t far_buf_size_ms = (int16_t)(WebRtcAec_system_delay(aecpc->aec) /
401 (sampMsNb * aecpc->rate_factor));
402 (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile);
403 (void)fwrite(
404 &aecpc->knownDelay, sizeof(aecpc->knownDelay), 1, aecpc->delayFile);
405 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000406#endif
407
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000408 return retVal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000409}
410
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000411int WebRtcAec_set_config(void* handle, AecConfig config) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000412 Aec* self = (Aec*)handle;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000413 if (self->initFlag != initCheck) {
414 self->lastError = AEC_UNINITIALIZED_ERROR;
415 return -1;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000416 }
417
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000418 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
419 self->lastError = AEC_BAD_PARAMETER_ERROR;
420 return -1;
421 }
422 self->skewMode = config.skewMode;
423
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000424 if (config.nlpMode != kAecNlpConservative &&
425 config.nlpMode != kAecNlpModerate &&
426 config.nlpMode != kAecNlpAggressive) {
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000427 self->lastError = AEC_BAD_PARAMETER_ERROR;
428 return -1;
429 }
430
431 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
432 self->lastError = AEC_BAD_PARAMETER_ERROR;
433 return -1;
434 }
435
436 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
437 self->lastError = AEC_BAD_PARAMETER_ERROR;
438 return -1;
439 }
440
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000441 WebRtcAec_SetConfigCore(
442 self->aec, config.nlpMode, config.metricsMode, config.delay_logging);
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000443 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000444}
445
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000446int WebRtcAec_get_echo_status(void* handle, int* status) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000447 Aec* self = (Aec*)handle;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000448 if (status == NULL) {
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000449 self->lastError = AEC_NULL_POINTER_ERROR;
450 return -1;
451 }
452 if (self->initFlag != initCheck) {
453 self->lastError = AEC_UNINITIALIZED_ERROR;
454 return -1;
455 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000456
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000457 *status = WebRtcAec_echo_state(self->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000458
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000459 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000460}
461
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000462int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) {
463 const float kUpWeight = 0.7f;
464 float dtmp;
465 int stmp;
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000466 Aec* self = (Aec*)handle;
bjornv@webrtc.orgcea70f42013-02-19 21:03:10 +0000467 Stats erl;
468 Stats erle;
469 Stats a_nlp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000470
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000471 if (handle == NULL) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000472 return -1;
473 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000474 if (metrics == NULL) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000475 self->lastError = AEC_NULL_POINTER_ERROR;
476 return -1;
477 }
478 if (self->initFlag != initCheck) {
479 self->lastError = AEC_UNINITIALIZED_ERROR;
480 return -1;
481 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000482
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000483 WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000484
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000485 // ERL
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000486 metrics->erl.instant = (int)erl.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000487
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000488 if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000489 // Use a mix between regular average and upper part average.
490 dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000491 metrics->erl.average = (int)dtmp;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000492 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000493 metrics->erl.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000494 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000495
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000496 metrics->erl.max = (int)erl.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000497
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000498 if (erl.min < (kOffsetLevel * (-1))) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000499 metrics->erl.min = (int)erl.min;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000500 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000501 metrics->erl.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000502 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000503
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000504 // ERLE
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000505 metrics->erle.instant = (int)erle.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000506
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000507 if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000508 // Use a mix between regular average and upper part average.
509 dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000510 metrics->erle.average = (int)dtmp;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000511 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000512 metrics->erle.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000513 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000514
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000515 metrics->erle.max = (int)erle.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000516
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000517 if (erle.min < (kOffsetLevel * (-1))) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000518 metrics->erle.min = (int)erle.min;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000519 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000520 metrics->erle.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000521 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000522
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000523 // RERL
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000524 if ((metrics->erl.average > kOffsetLevel) &&
525 (metrics->erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000526 stmp = metrics->erl.average + metrics->erle.average;
527 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000528 stmp = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000529 }
530 metrics->rerl.average = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000531
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000532 // No other statistics needed, but returned for completeness.
533 metrics->rerl.instant = stmp;
534 metrics->rerl.max = stmp;
535 metrics->rerl.min = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000536
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000537 // A_NLP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000538 metrics->aNlp.instant = (int)a_nlp.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000539
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000540 if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000541 // Use a mix between regular average and upper part average.
542 dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000543 metrics->aNlp.average = (int)dtmp;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000544 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000545 metrics->aNlp.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000546 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000547
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000548 metrics->aNlp.max = (int)a_nlp.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000549
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000550 if (a_nlp.min < (kOffsetLevel * (-1))) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000551 metrics->aNlp.min = (int)a_nlp.min;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000552 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000553 metrics->aNlp.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000554 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000555
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000556 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000557}
558
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000559int WebRtcAec_GetDelayMetrics(void* handle,
560 int* median,
561 int* std,
562 float* fraction_poor_delays) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000563 Aec* self = handle;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000564 if (median == NULL) {
565 self->lastError = AEC_NULL_POINTER_ERROR;
566 return -1;
567 }
568 if (std == NULL) {
569 self->lastError = AEC_NULL_POINTER_ERROR;
570 return -1;
571 }
572 if (self->initFlag != initCheck) {
573 self->lastError = AEC_UNINITIALIZED_ERROR;
574 return -1;
575 }
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000576 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std,
577 fraction_poor_delays) ==
578 -1) {
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000579 // Logging disabled.
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000580 self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR;
581 return -1;
582 }
583
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000584 return 0;
585}
586
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000587int32_t WebRtcAec_get_error_code(void* aecInst) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000588 Aec* aecpc = aecInst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000589 return aecpc->lastError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000590}
591
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000592AecCore* WebRtcAec_aec_core(void* handle) {
593 if (!handle) {
594 return NULL;
595 }
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000596 return ((Aec*)handle)->aec;
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000597}
598
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000599static int ProcessNormal(Aec* aecpc,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000600 const float* const* nearend,
601 int num_bands,
602 float* const* out,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000603 int16_t nrOfSamples,
604 int16_t msInSndCardBuf,
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000605 int32_t skew) {
606 int retVal = 0;
607 short i;
608 short nBlocks10ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000609 // Limit resampling to doubling/halving of signal
610 const float minSkewEst = -0.5f;
611 const float maxSkewEst = 1.0f;
612
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000613 msInSndCardBuf =
614 msInSndCardBuf > kMaxTrustedDelayMs ? kMaxTrustedDelayMs : msInSndCardBuf;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000615 // TODO(andrew): we need to investigate if this +10 is really wanted.
616 msInSndCardBuf += 10;
617 aecpc->msInSndCardBuf = msInSndCardBuf;
618
619 if (aecpc->skewMode == kAecTrue) {
620 if (aecpc->skewFrCtr < 25) {
621 aecpc->skewFrCtr++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000622 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000623 retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
624 if (retVal == -1) {
625 aecpc->skew = 0;
626 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
627 }
628
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000629 aecpc->skew /= aecpc->sampFactor * nrOfSamples;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000630
631 if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
632 aecpc->resample = kAecFalse;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000633 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000634 aecpc->resample = kAecTrue;
635 }
636
637 if (aecpc->skew < minSkewEst) {
638 aecpc->skew = minSkewEst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000639 } else if (aecpc->skew > maxSkewEst) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000640 aecpc->skew = maxSkewEst;
641 }
642
643#ifdef WEBRTC_AEC_DEBUG_DUMP
644 (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile);
645#endif
646 }
647 }
648
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000649 nBlocks10ms = nrOfSamples / (FRAME_LEN * aecpc->rate_factor);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000650
651 if (aecpc->startup_phase) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000652 for (i = 0; i < num_bands; ++i) {
653 // Only needed if they don't already point to the same place.
654 if (nearend[i] != out[i]) {
655 memcpy(out[i], nearend[i], sizeof(nearend[i][0]) * nrOfSamples);
656 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000657 }
658
659 // The AEC is in the start up mode
660 // AEC is disabled until the system delay is OK
661
662 // Mechanism to ensure that the system delay is reasonably stable.
663 if (aecpc->checkBuffSize) {
664 aecpc->checkBufSizeCtr++;
665 // Before we fill up the far-end buffer we require the system delay
666 // to be stable (+/-8 ms) compared to the first value. This
667 // comparison is made during the following 6 consecutive 10 ms
668 // blocks. If it seems to be stable then we start to fill up the
669 // far-end buffer.
670 if (aecpc->counter == 0) {
671 aecpc->firstVal = aecpc->msInSndCardBuf;
672 aecpc->sum = 0;
673 }
674
675 if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000676 WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000677 aecpc->sum += aecpc->msInSndCardBuf;
678 aecpc->counter++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000679 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000680 aecpc->counter = 0;
681 }
682
683 if (aecpc->counter * nBlocks10ms >= 6) {
684 // The far-end buffer size is determined in partitions of
685 // PART_LEN samples. Use 75% of the average value of the system
686 // delay as buffer size to start with.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000687 aecpc->bufSizeStart =
688 WEBRTC_SPL_MIN((3 * aecpc->sum * aecpc->rate_factor * 8) /
689 (4 * aecpc->counter * PART_LEN),
690 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000691 // Buffer size has now been determined.
692 aecpc->checkBuffSize = 0;
693 }
694
695 if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
696 // For really bad systems, don't disable the echo canceller for
697 // more than 0.5 sec.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000698 aecpc->bufSizeStart = WEBRTC_SPL_MIN(
699 (aecpc->msInSndCardBuf * aecpc->rate_factor * 3) / 40,
700 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000701 aecpc->checkBuffSize = 0;
702 }
703 }
704
705 // If |checkBuffSize| changed in the if-statement above.
706 if (!aecpc->checkBuffSize) {
707 // The system delay is now reasonably stable (or has been unstable
708 // for too long). When the far-end buffer is filled with
709 // approximately the same amount of data as reported by the system
710 // we end the startup phase.
711 int overhead_elements =
712 WebRtcAec_system_delay(aecpc->aec) / PART_LEN - aecpc->bufSizeStart;
713 if (overhead_elements == 0) {
714 // Enable the AEC
715 aecpc->startup_phase = 0;
716 } else if (overhead_elements > 0) {
717 // TODO(bjornv): Do we need a check on how much we actually
718 // moved the read pointer? It should always be possible to move
719 // the pointer |overhead_elements| since we have only added data
720 // to the buffer and no delay compensation nor AEC processing
721 // has been done.
722 WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements);
723
724 // Enable the AEC
725 aecpc->startup_phase = 0;
726 }
727 }
728 } else {
729 // AEC is enabled.
bjornv@webrtc.orge9d37602014-04-23 13:20:07 +0000730 if (WebRtcAec_reported_delay_enabled(aecpc->aec)) {
731 EstBufDelayNormal(aecpc);
732 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000733
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000734 // Call the AEC.
735 // TODO(bjornv): Re-structure such that we don't have to pass
736 // |aecpc->knownDelay| as input. Change name to something like
737 // |system_buffer_diff|.
738 WebRtcAec_ProcessFrames(aecpc->aec,
739 nearend,
740 num_bands,
741 nrOfSamples,
742 aecpc->knownDelay,
743 out);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000744 }
745
746 return retVal;
747}
748
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000749static void ProcessExtended(Aec* self,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000750 const float* const* near,
751 int num_bands,
752 float* const* out,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000753 int16_t num_samples,
754 int16_t reported_delay_ms,
755 int32_t skew) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000756 int i;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000757 const int delay_diff_offset = kDelayDiffOffsetSamples;
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +0000758#if defined(WEBRTC_UNTRUSTED_DELAY)
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000759 reported_delay_ms = kFixedDelayMs;
760#else
761 // This is the usual mode where we trust the reported system delay values.
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000762 // Due to the longer filter, we no longer add 10 ms to the reported delay
763 // to reduce chance of non-causality. Instead we apply a minimum here to avoid
764 // issues with the read pointer jumping around needlessly.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000765 reported_delay_ms = reported_delay_ms < kMinTrustedDelayMs
766 ? kMinTrustedDelayMs
767 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000768 // If the reported delay appears to be bogus, we attempt to recover by using
769 // the measured fixed delay values. We use >= here because higher layers
770 // may already clamp to this maximum value, and we would otherwise not
771 // detect it here.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000772 reported_delay_ms = reported_delay_ms >= kMaxTrustedDelayMs
773 ? kFixedDelayMs
774 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000775#endif
776 self->msInSndCardBuf = reported_delay_ms;
777
778 if (!self->farend_started) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000779 for (i = 0; i < num_bands; ++i) {
780 // Only needed if they don't already point to the same place.
781 if (near[i] != out[i]) {
782 memcpy(out[i], near[i], sizeof(near[i][0]) * num_samples);
783 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000784 }
785 return;
786 }
787 if (self->startup_phase) {
788 // In the extended mode, there isn't a startup "phase", just a special
789 // action on the first frame. In the trusted delay case, we'll take the
790 // current reported delay, unless it's less then our conservative
791 // measurement.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000792 int startup_size_ms =
793 reported_delay_ms < kFixedDelayMs ? kFixedDelayMs : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000794 int overhead_elements = (WebRtcAec_system_delay(self->aec) -
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000795 startup_size_ms / 2 * self->rate_factor * 8) /
796 PART_LEN;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000797 WebRtcAec_MoveFarReadPtr(self->aec, overhead_elements);
798 self->startup_phase = 0;
799 }
800
bjornv@webrtc.orge9d37602014-04-23 13:20:07 +0000801 if (WebRtcAec_reported_delay_enabled(self->aec)) {
802 EstBufDelayExtended(self);
803 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000804
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000805 {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000806 // |delay_diff_offset| gives us the option to manually rewind the delay on
807 // very low delay platforms which can't be expressed purely through
808 // |reported_delay_ms|.
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000809 const int adjusted_known_delay =
810 WEBRTC_SPL_MAX(0, self->knownDelay + delay_diff_offset);
811
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000812 WebRtcAec_ProcessFrames(self->aec,
813 near,
814 num_bands,
815 num_samples,
816 adjusted_known_delay,
817 out);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000818 }
819}
820
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000821static void EstBufDelayNormal(Aec* aecpc) {
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000822 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor;
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000823 int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->aec);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000824 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000825
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000826 // Before we proceed with the delay estimate filtering we:
827 // 1) Compensate for the frame that will be read.
828 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000829 // 3) Compensate for non-causality if needed, since the estimated delay can't
830 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000831
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000832 // 1) Compensating for the frame(s) that will be read/processed.
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000833 current_delay += FRAME_LEN * aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000834
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000835 // 2) Account for resampling frame delay.
836 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
837 current_delay -= kResamplingDelay;
838 }
839
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000840 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000841 if (current_delay < PART_LEN) {
842 current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
843 }
844
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000845 // We use -1 to signal an initialized state in the "extended" implementation;
846 // compensate for that.
847 aecpc->filtDelay = aecpc->filtDelay < 0 ? 0 : aecpc->filtDelay;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000848 aecpc->filtDelay =
849 WEBRTC_SPL_MAX(0, (short)(0.8 * aecpc->filtDelay + 0.2 * current_delay));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000850
851 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
852 if (delay_difference > 224) {
853 if (aecpc->lastDelayDiff < 96) {
854 aecpc->timeForDelayChange = 0;
855 } else {
856 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000857 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000858 } else if (delay_difference < 96 && aecpc->knownDelay > 0) {
859 if (aecpc->lastDelayDiff > 224) {
860 aecpc->timeForDelayChange = 0;
861 } else {
862 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000863 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000864 } else {
865 aecpc->timeForDelayChange = 0;
866 }
867 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000868
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000869 if (aecpc->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000870 aecpc->knownDelay = WEBRTC_SPL_MAX((int)aecpc->filtDelay - 160, 0);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000871 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000872}
niklase@google.com470e71d2011-07-07 08:21:25 +0000873
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000874static void EstBufDelayExtended(Aec* self) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000875 int reported_delay = self->msInSndCardBuf * sampMsNb * self->rate_factor;
876 int current_delay = reported_delay - WebRtcAec_system_delay(self->aec);
877 int delay_difference = 0;
878
879 // Before we proceed with the delay estimate filtering we:
880 // 1) Compensate for the frame that will be read.
881 // 2) Compensate for drift resampling.
882 // 3) Compensate for non-causality if needed, since the estimated delay can't
883 // be negative.
884
885 // 1) Compensating for the frame(s) that will be read/processed.
886 current_delay += FRAME_LEN * self->rate_factor;
887
888 // 2) Account for resampling frame delay.
889 if (self->skewMode == kAecTrue && self->resample == kAecTrue) {
890 current_delay -= kResamplingDelay;
891 }
892
893 // 3) Compensate for non-causality, if needed, by flushing two blocks.
894 if (current_delay < PART_LEN) {
895 current_delay += WebRtcAec_MoveFarReadPtr(self->aec, 2) * PART_LEN;
896 }
897
898 if (self->filtDelay == -1) {
899 self->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay);
900 } else {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000901 self->filtDelay = WEBRTC_SPL_MAX(
902 0, (short)(0.95 * self->filtDelay + 0.05 * current_delay));
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000903 }
904
905 delay_difference = self->filtDelay - self->knownDelay;
906 if (delay_difference > 384) {
907 if (self->lastDelayDiff < 128) {
908 self->timeForDelayChange = 0;
909 } else {
910 self->timeForDelayChange++;
911 }
912 } else if (delay_difference < 128 && self->knownDelay > 0) {
913 if (self->lastDelayDiff > 384) {
914 self->timeForDelayChange = 0;
915 } else {
916 self->timeForDelayChange++;
917 }
918 } else {
919 self->timeForDelayChange = 0;
920 }
921 self->lastDelayDiff = delay_difference;
922
923 if (self->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000924 self->knownDelay = WEBRTC_SPL_MAX((int)self->filtDelay - 256, 0);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000925 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000926}