blob: 2cff8a210ae76debb8b883f93359b41f38678c61 [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 {
324 float* ptmp;
325 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@google.com1ba3dbe2011-10-03 08:18:10 +0000559int WebRtcAec_GetDelayMetrics(void* handle, int* median, int* std) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000560 Aec* self = handle;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000561 if (median == NULL) {
562 self->lastError = AEC_NULL_POINTER_ERROR;
563 return -1;
564 }
565 if (std == NULL) {
566 self->lastError = AEC_NULL_POINTER_ERROR;
567 return -1;
568 }
569 if (self->initFlag != initCheck) {
570 self->lastError = AEC_UNINITIALIZED_ERROR;
571 return -1;
572 }
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000573 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std) == -1) {
574 // Logging disabled.
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000575 self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR;
576 return -1;
577 }
578
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000579 return 0;
580}
581
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000582int32_t WebRtcAec_get_error_code(void* aecInst) {
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000583 Aec* aecpc = aecInst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000584 return aecpc->lastError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000585}
586
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000587AecCore* WebRtcAec_aec_core(void* handle) {
588 if (!handle) {
589 return NULL;
590 }
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000591 return ((Aec*)handle)->aec;
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000592}
593
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000594static int ProcessNormal(Aec* aecpc,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000595 const float* const* nearend,
596 int num_bands,
597 float* const* out,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000598 int16_t nrOfSamples,
599 int16_t msInSndCardBuf,
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000600 int32_t skew) {
601 int retVal = 0;
602 short i;
603 short nBlocks10ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000604 // Limit resampling to doubling/halving of signal
605 const float minSkewEst = -0.5f;
606 const float maxSkewEst = 1.0f;
607
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000608 msInSndCardBuf =
609 msInSndCardBuf > kMaxTrustedDelayMs ? kMaxTrustedDelayMs : msInSndCardBuf;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000610 // TODO(andrew): we need to investigate if this +10 is really wanted.
611 msInSndCardBuf += 10;
612 aecpc->msInSndCardBuf = msInSndCardBuf;
613
614 if (aecpc->skewMode == kAecTrue) {
615 if (aecpc->skewFrCtr < 25) {
616 aecpc->skewFrCtr++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000617 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000618 retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
619 if (retVal == -1) {
620 aecpc->skew = 0;
621 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
622 }
623
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000624 aecpc->skew /= aecpc->sampFactor * nrOfSamples;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000625
626 if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
627 aecpc->resample = kAecFalse;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000628 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000629 aecpc->resample = kAecTrue;
630 }
631
632 if (aecpc->skew < minSkewEst) {
633 aecpc->skew = minSkewEst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000634 } else if (aecpc->skew > maxSkewEst) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000635 aecpc->skew = maxSkewEst;
636 }
637
638#ifdef WEBRTC_AEC_DEBUG_DUMP
639 (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile);
640#endif
641 }
642 }
643
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000644 nBlocks10ms = nrOfSamples / (FRAME_LEN * aecpc->rate_factor);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000645
646 if (aecpc->startup_phase) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000647 for (i = 0; i < num_bands; ++i) {
648 // Only needed if they don't already point to the same place.
649 if (nearend[i] != out[i]) {
650 memcpy(out[i], nearend[i], sizeof(nearend[i][0]) * nrOfSamples);
651 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000652 }
653
654 // The AEC is in the start up mode
655 // AEC is disabled until the system delay is OK
656
657 // Mechanism to ensure that the system delay is reasonably stable.
658 if (aecpc->checkBuffSize) {
659 aecpc->checkBufSizeCtr++;
660 // Before we fill up the far-end buffer we require the system delay
661 // to be stable (+/-8 ms) compared to the first value. This
662 // comparison is made during the following 6 consecutive 10 ms
663 // blocks. If it seems to be stable then we start to fill up the
664 // far-end buffer.
665 if (aecpc->counter == 0) {
666 aecpc->firstVal = aecpc->msInSndCardBuf;
667 aecpc->sum = 0;
668 }
669
670 if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000671 WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000672 aecpc->sum += aecpc->msInSndCardBuf;
673 aecpc->counter++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000674 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000675 aecpc->counter = 0;
676 }
677
678 if (aecpc->counter * nBlocks10ms >= 6) {
679 // The far-end buffer size is determined in partitions of
680 // PART_LEN samples. Use 75% of the average value of the system
681 // delay as buffer size to start with.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000682 aecpc->bufSizeStart =
683 WEBRTC_SPL_MIN((3 * aecpc->sum * aecpc->rate_factor * 8) /
684 (4 * aecpc->counter * PART_LEN),
685 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000686 // Buffer size has now been determined.
687 aecpc->checkBuffSize = 0;
688 }
689
690 if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
691 // For really bad systems, don't disable the echo canceller for
692 // more than 0.5 sec.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000693 aecpc->bufSizeStart = WEBRTC_SPL_MIN(
694 (aecpc->msInSndCardBuf * aecpc->rate_factor * 3) / 40,
695 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000696 aecpc->checkBuffSize = 0;
697 }
698 }
699
700 // If |checkBuffSize| changed in the if-statement above.
701 if (!aecpc->checkBuffSize) {
702 // The system delay is now reasonably stable (or has been unstable
703 // for too long). When the far-end buffer is filled with
704 // approximately the same amount of data as reported by the system
705 // we end the startup phase.
706 int overhead_elements =
707 WebRtcAec_system_delay(aecpc->aec) / PART_LEN - aecpc->bufSizeStart;
708 if (overhead_elements == 0) {
709 // Enable the AEC
710 aecpc->startup_phase = 0;
711 } else if (overhead_elements > 0) {
712 // TODO(bjornv): Do we need a check on how much we actually
713 // moved the read pointer? It should always be possible to move
714 // the pointer |overhead_elements| since we have only added data
715 // to the buffer and no delay compensation nor AEC processing
716 // has been done.
717 WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements);
718
719 // Enable the AEC
720 aecpc->startup_phase = 0;
721 }
722 }
723 } else {
724 // AEC is enabled.
bjornv@webrtc.orge9d37602014-04-23 13:20:07 +0000725 if (WebRtcAec_reported_delay_enabled(aecpc->aec)) {
726 EstBufDelayNormal(aecpc);
727 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000728
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000729 // Call the AEC.
730 // TODO(bjornv): Re-structure such that we don't have to pass
731 // |aecpc->knownDelay| as input. Change name to something like
732 // |system_buffer_diff|.
733 WebRtcAec_ProcessFrames(aecpc->aec,
734 nearend,
735 num_bands,
736 nrOfSamples,
737 aecpc->knownDelay,
738 out);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000739 }
740
741 return retVal;
742}
743
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000744static void ProcessExtended(Aec* self,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000745 const float* const* near,
746 int num_bands,
747 float* const* out,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000748 int16_t num_samples,
749 int16_t reported_delay_ms,
750 int32_t skew) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000751 int i;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000752 const int delay_diff_offset = kDelayDiffOffsetSamples;
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +0000753#if defined(WEBRTC_UNTRUSTED_DELAY)
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000754 reported_delay_ms = kFixedDelayMs;
755#else
756 // This is the usual mode where we trust the reported system delay values.
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000757 // Due to the longer filter, we no longer add 10 ms to the reported delay
758 // to reduce chance of non-causality. Instead we apply a minimum here to avoid
759 // issues with the read pointer jumping around needlessly.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000760 reported_delay_ms = reported_delay_ms < kMinTrustedDelayMs
761 ? kMinTrustedDelayMs
762 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000763 // If the reported delay appears to be bogus, we attempt to recover by using
764 // the measured fixed delay values. We use >= here because higher layers
765 // may already clamp to this maximum value, and we would otherwise not
766 // detect it here.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000767 reported_delay_ms = reported_delay_ms >= kMaxTrustedDelayMs
768 ? kFixedDelayMs
769 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000770#endif
771 self->msInSndCardBuf = reported_delay_ms;
772
773 if (!self->farend_started) {
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000774 for (i = 0; i < num_bands; ++i) {
775 // Only needed if they don't already point to the same place.
776 if (near[i] != out[i]) {
777 memcpy(out[i], near[i], sizeof(near[i][0]) * num_samples);
778 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000779 }
780 return;
781 }
782 if (self->startup_phase) {
783 // In the extended mode, there isn't a startup "phase", just a special
784 // action on the first frame. In the trusted delay case, we'll take the
785 // current reported delay, unless it's less then our conservative
786 // measurement.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000787 int startup_size_ms =
788 reported_delay_ms < kFixedDelayMs ? kFixedDelayMs : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000789 int overhead_elements = (WebRtcAec_system_delay(self->aec) -
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000790 startup_size_ms / 2 * self->rate_factor * 8) /
791 PART_LEN;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000792 WebRtcAec_MoveFarReadPtr(self->aec, overhead_elements);
793 self->startup_phase = 0;
794 }
795
bjornv@webrtc.orge9d37602014-04-23 13:20:07 +0000796 if (WebRtcAec_reported_delay_enabled(self->aec)) {
797 EstBufDelayExtended(self);
798 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000799
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000800 {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000801 // |delay_diff_offset| gives us the option to manually rewind the delay on
802 // very low delay platforms which can't be expressed purely through
803 // |reported_delay_ms|.
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000804 const int adjusted_known_delay =
805 WEBRTC_SPL_MAX(0, self->knownDelay + delay_diff_offset);
806
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000807 WebRtcAec_ProcessFrames(self->aec,
808 near,
809 num_bands,
810 num_samples,
811 adjusted_known_delay,
812 out);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000813 }
814}
815
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000816static void EstBufDelayNormal(Aec* aecpc) {
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000817 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor;
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000818 int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->aec);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000819 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000820
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000821 // Before we proceed with the delay estimate filtering we:
822 // 1) Compensate for the frame that will be read.
823 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000824 // 3) Compensate for non-causality if needed, since the estimated delay can't
825 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000826
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000827 // 1) Compensating for the frame(s) that will be read/processed.
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000828 current_delay += FRAME_LEN * aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000829
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000830 // 2) Account for resampling frame delay.
831 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
832 current_delay -= kResamplingDelay;
833 }
834
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000835 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000836 if (current_delay < PART_LEN) {
837 current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
838 }
839
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000840 // We use -1 to signal an initialized state in the "extended" implementation;
841 // compensate for that.
842 aecpc->filtDelay = aecpc->filtDelay < 0 ? 0 : aecpc->filtDelay;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000843 aecpc->filtDelay =
844 WEBRTC_SPL_MAX(0, (short)(0.8 * aecpc->filtDelay + 0.2 * current_delay));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000845
846 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
847 if (delay_difference > 224) {
848 if (aecpc->lastDelayDiff < 96) {
849 aecpc->timeForDelayChange = 0;
850 } else {
851 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000852 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000853 } else if (delay_difference < 96 && aecpc->knownDelay > 0) {
854 if (aecpc->lastDelayDiff > 224) {
855 aecpc->timeForDelayChange = 0;
856 } else {
857 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000858 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000859 } else {
860 aecpc->timeForDelayChange = 0;
861 }
862 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000863
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000864 if (aecpc->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000865 aecpc->knownDelay = WEBRTC_SPL_MAX((int)aecpc->filtDelay - 160, 0);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000866 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000867}
niklase@google.com470e71d2011-07-07 08:21:25 +0000868
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000869static void EstBufDelayExtended(Aec* self) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000870 int reported_delay = self->msInSndCardBuf * sampMsNb * self->rate_factor;
871 int current_delay = reported_delay - WebRtcAec_system_delay(self->aec);
872 int delay_difference = 0;
873
874 // Before we proceed with the delay estimate filtering we:
875 // 1) Compensate for the frame that will be read.
876 // 2) Compensate for drift resampling.
877 // 3) Compensate for non-causality if needed, since the estimated delay can't
878 // be negative.
879
880 // 1) Compensating for the frame(s) that will be read/processed.
881 current_delay += FRAME_LEN * self->rate_factor;
882
883 // 2) Account for resampling frame delay.
884 if (self->skewMode == kAecTrue && self->resample == kAecTrue) {
885 current_delay -= kResamplingDelay;
886 }
887
888 // 3) Compensate for non-causality, if needed, by flushing two blocks.
889 if (current_delay < PART_LEN) {
890 current_delay += WebRtcAec_MoveFarReadPtr(self->aec, 2) * PART_LEN;
891 }
892
893 if (self->filtDelay == -1) {
894 self->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay);
895 } else {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000896 self->filtDelay = WEBRTC_SPL_MAX(
897 0, (short)(0.95 * self->filtDelay + 0.05 * current_delay));
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000898 }
899
900 delay_difference = self->filtDelay - self->knownDelay;
901 if (delay_difference > 384) {
902 if (self->lastDelayDiff < 128) {
903 self->timeForDelayChange = 0;
904 } else {
905 self->timeForDelayChange++;
906 }
907 } else if (delay_difference < 128 && self->knownDelay > 0) {
908 if (self->lastDelayDiff > 384) {
909 self->timeForDelayChange = 0;
910 } else {
911 self->timeForDelayChange++;
912 }
913 } else {
914 self->timeForDelayChange = 0;
915 }
916 self->lastDelayDiff = delay_difference;
917
918 if (self->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000919 self->knownDelay = WEBRTC_SPL_MAX((int)self->filtDelay - 256, 0);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000920 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000921}