blob: 07bca559afb4cbdc42721d93d9a1d8306b86fed8 [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
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +000023#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
24#include "webrtc/modules/audio_processing/aec/aec_core.h"
25#include "webrtc/modules/audio_processing/aec/aec_resampler.h"
26#include "webrtc/modules/audio_processing/aec/echo_cancellation_internal.h"
27#include "webrtc/modules/audio_processing/utility/ring_buffer.h"
28#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
69#endif
70
71#if defined(WEBRTC_MAC)
72static const int kFixedDelayMs = 20;
73static const int kDelayDiffOffsetSamples = -160;
74#elif defined(WEBRTC_WIN)
75static const int kFixedDelayMs = 50;
76static const int kDelayDiffOffsetSamples = 0;
77#else
78// Essentially ChromeOS.
79static const int kFixedDelayMs = 50;
80static const int kDelayDiffOffsetSamples = 0;
81#endif
82static const int kMinTrustedDelayMs = 20;
83static const int kMaxTrustedDelayMs = 500;
84
niklase@google.com470e71d2011-07-07 08:21:25 +000085// Maximum length of resampled signal. Must be an integer multiple of frames
86// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
87// The factor of 2 handles wb, and the + 1 is as a safety margin
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000088// TODO(bjornv): Replace with kResamplerBufferSize
niklase@google.com470e71d2011-07-07 08:21:25 +000089#define MAX_RESAMP_LEN (5 * FRAME_LEN)
90
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000091static const int kMaxBufSizeStart = 62; // In partitions
niklase@google.com470e71d2011-07-07 08:21:25 +000092static const int sampMsNb = 8; // samples per ms in nb
niklase@google.com470e71d2011-07-07 08:21:25 +000093static const int initCheck = 42;
94
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000095#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +000096int webrtc_aec_instance_count = 0;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000097#endif
98
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000099// Estimates delay to set the position of the far-end buffer read pointer
niklase@google.com470e71d2011-07-07 08:21:25 +0000100// (controlled by knownDelay)
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000101static void EstBufDelayNormal(aecpc_t *aecInst);
102static void EstBufDelayExtended(aecpc_t *aecInst);
103static int ProcessNormal(aecpc_t* self, const int16_t* near,
104 const int16_t* near_high, int16_t* out, int16_t* out_high,
105 int16_t num_samples, int16_t reported_delay_ms, int32_t skew);
106static void ProcessExtended(aecpc_t* self, const int16_t* near,
107 const int16_t* near_high, int16_t* out, int16_t* out_high,
108 int16_t num_samples, int16_t reported_delay_ms, int32_t skew);
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000110int32_t WebRtcAec_Create(void **aecInst)
niklase@google.com470e71d2011-07-07 08:21:25 +0000111{
112 aecpc_t *aecpc;
113 if (aecInst == NULL) {
114 return -1;
115 }
116
117 aecpc = malloc(sizeof(aecpc_t));
118 *aecInst = aecpc;
119 if (aecpc == NULL) {
120 return -1;
121 }
122
123 if (WebRtcAec_CreateAec(&aecpc->aec) == -1) {
124 WebRtcAec_Free(aecpc);
125 aecpc = NULL;
126 return -1;
127 }
128
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000129 if (WebRtcAec_CreateResampler(&aecpc->resampler) == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000130 WebRtcAec_Free(aecpc);
131 aecpc = NULL;
132 return -1;
133 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000134 // Create far-end pre-buffer. The buffer size has to be large enough for
135 // largest possible drift compensation (kResamplerBufferSize) + "almost" an
136 // FFT buffer (PART_LEN2 - 1).
andrew@webrtc.org91f32552013-02-27 00:35:06 +0000137 aecpc->far_pre_buf = WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize,
138 sizeof(float));
139 if (!aecpc->far_pre_buf) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000140 WebRtcAec_Free(aecpc);
141 aecpc = NULL;
142 return -1;
143 }
144
145 aecpc->initFlag = 0;
146 aecpc->lastError = 0;
147
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000148#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org91f32552013-02-27 00:35:06 +0000149 aecpc->far_pre_buf_s16 = WebRtc_CreateBuffer(
andrew@webrtc.org52b57cc2013-03-07 00:45:50 +0000150 PART_LEN2 + kResamplerBufferSize, sizeof(int16_t));
andrew@webrtc.org91f32552013-02-27 00:35:06 +0000151 if (!aecpc->far_pre_buf_s16) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000152 WebRtcAec_Free(aecpc);
153 aecpc = NULL;
154 return -1;
155 }
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000156 {
157 char filename[64];
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000158 sprintf(filename, "aec_buf%d.dat", webrtc_aec_instance_count);
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000159 aecpc->bufFile = fopen(filename, "wb");
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000160 sprintf(filename, "aec_skew%d.dat", webrtc_aec_instance_count);
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000161 aecpc->skewFile = fopen(filename, "wb");
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000162 sprintf(filename, "aec_delay%d.dat", webrtc_aec_instance_count);
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000163 aecpc->delayFile = fopen(filename, "wb");
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000164 webrtc_aec_instance_count++;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000165 }
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000166#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000167
168 return 0;
169}
170
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000171int32_t WebRtcAec_Free(void *aecInst)
niklase@google.com470e71d2011-07-07 08:21:25 +0000172{
173 aecpc_t *aecpc = aecInst;
174
175 if (aecpc == NULL) {
176 return -1;
177 }
178
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000179 WebRtc_FreeBuffer(aecpc->far_pre_buf);
180
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000181#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000182 WebRtc_FreeBuffer(aecpc->far_pre_buf_s16);
niklase@google.com470e71d2011-07-07 08:21:25 +0000183 fclose(aecpc->bufFile);
184 fclose(aecpc->skewFile);
185 fclose(aecpc->delayFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000186#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000187
188 WebRtcAec_FreeAec(aecpc->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000189 WebRtcAec_FreeResampler(aecpc->resampler);
190 free(aecpc);
191
192 return 0;
193}
194
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000195int32_t WebRtcAec_Init(void *aecInst, int32_t sampFreq, int32_t scSampFreq)
niklase@google.com470e71d2011-07-07 08:21:25 +0000196{
197 aecpc_t *aecpc = aecInst;
198 AecConfig aecConfig;
199
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 if (sampFreq != 8000 && sampFreq != 16000 && sampFreq != 32000) {
201 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
202 return -1;
203 }
204 aecpc->sampFreq = sampFreq;
205
206 if (scSampFreq < 1 || scSampFreq > 96000) {
207 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
208 return -1;
209 }
210 aecpc->scSampFreq = scSampFreq;
211
212 // Initialize echo canceller core
213 if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) {
214 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
215 return -1;
216 }
217
niklase@google.com470e71d2011-07-07 08:21:25 +0000218 if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) {
219 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
220 return -1;
221 }
222
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000223 if (WebRtc_InitBuffer(aecpc->far_pre_buf) == -1) {
224 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
225 return -1;
226 }
227 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap.
228
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000229 aecpc->initFlag = initCheck; // indicates that initialization has been done
niklase@google.com470e71d2011-07-07 08:21:25 +0000230
231 if (aecpc->sampFreq == 32000) {
232 aecpc->splitSampFreq = 16000;
233 }
234 else {
235 aecpc->splitSampFreq = sampFreq;
236 }
237
niklase@google.com470e71d2011-07-07 08:21:25 +0000238 aecpc->delayCtr = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000239 aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq;
240 // Sampling frequency multiplier (SWB is processed as 160 frame size).
241 aecpc->rate_factor = aecpc->splitSampFreq / 8000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000242
243 aecpc->sum = 0;
244 aecpc->counter = 0;
245 aecpc->checkBuffSize = 1;
246 aecpc->firstVal = 0;
247
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000248 aecpc->startup_phase = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000249 aecpc->bufSizeStart = 0;
250 aecpc->checkBufSizeCtr = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000251 aecpc->msInSndCardBuf = 0;
252 aecpc->filtDelay = -1; // -1 indicates an initialized state.
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000253 aecpc->timeForDelayChange = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000254 aecpc->knownDelay = 0;
255 aecpc->lastDelayDiff = 0;
256
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000257 aecpc->skewFrCtr = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000258 aecpc->resample = kAecFalse;
259 aecpc->highSkewCtr = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000260 aecpc->skew = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000261
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000262 aecpc->farend_started = 0;
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000263
niklase@google.com470e71d2011-07-07 08:21:25 +0000264 // Default settings.
265 aecConfig.nlpMode = kAecNlpModerate;
266 aecConfig.skewMode = kAecFalse;
267 aecConfig.metricsMode = kAecFalse;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000268 aecConfig.delay_logging = kAecFalse;
niklase@google.com470e71d2011-07-07 08:21:25 +0000269
270 if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
271 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
272 return -1;
273 }
274
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000275#ifdef WEBRTC_AEC_DEBUG_DUMP
276 if (WebRtc_InitBuffer(aecpc->far_pre_buf_s16) == -1) {
277 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
278 return -1;
279 }
280 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN); // Start overlap.
281#endif
282
niklase@google.com470e71d2011-07-07 08:21:25 +0000283 return 0;
284}
285
286// only buffer L band for farend
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000287int32_t WebRtcAec_BufferFarend(void *aecInst, const int16_t *farend,
288 int16_t nrOfSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +0000289{
290 aecpc_t *aecpc = aecInst;
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000291 int32_t retVal = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000292 int newNrOfSamples = (int) nrOfSamples;
niklase@google.com470e71d2011-07-07 08:21:25 +0000293 short newFarend[MAX_RESAMP_LEN];
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000294 const int16_t* farend_ptr = farend;
295 float tmp_farend[MAX_RESAMP_LEN];
296 const float* farend_float = tmp_farend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000297 float skew;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000298 int i = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000299
niklase@google.com470e71d2011-07-07 08:21:25 +0000300 if (farend == NULL) {
301 aecpc->lastError = AEC_NULL_POINTER_ERROR;
302 return -1;
303 }
304
305 if (aecpc->initFlag != initCheck) {
306 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
307 return -1;
308 }
309
310 // number of samples == 160 for SWB input
311 if (nrOfSamples != 80 && nrOfSamples != 160) {
312 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
313 return -1;
314 }
315
316 skew = aecpc->skew;
317
niklase@google.com470e71d2011-07-07 08:21:25 +0000318 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
319 // Resample and get a new number of samples
bjornv@webrtc.org281b7982012-05-30 07:41:57 +0000320 WebRtcAec_ResampleLinear(aecpc->resampler, farend, nrOfSamples, skew,
321 newFarend, &newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000322 farend_ptr = (const int16_t*) newFarend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000323 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000324
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000325 aecpc->farend_started = 1;
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000326 WebRtcAec_SetSystemDelay(aecpc->aec, WebRtcAec_system_delay(aecpc->aec) +
327 newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000328
329#ifdef WEBRTC_AEC_DEBUG_DUMP
330 WebRtc_WriteBuffer(aecpc->far_pre_buf_s16, farend_ptr,
331 (size_t) newNrOfSamples);
332#endif
333 // Cast to float and write the time-domain data to |far_pre_buf|.
334 for (i = 0; i < newNrOfSamples; i++) {
335 tmp_farend[i] = (float) farend_ptr[i];
336 }
337 WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_float,
338 (size_t) newNrOfSamples);
339
340 // Transform to frequency domain if we have enough data.
341 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
342 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
343 WebRtc_ReadBuffer(aecpc->far_pre_buf, (void**) &farend_float, tmp_farend,
344 PART_LEN2);
345
346 WebRtcAec_BufferFarendPartition(aecpc->aec, farend_float);
347
348 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
349 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
350#ifdef WEBRTC_AEC_DEBUG_DUMP
351 WebRtc_ReadBuffer(aecpc->far_pre_buf_s16, (void**) &farend_ptr, newFarend,
352 PART_LEN2);
bjornv@webrtc.org0a480cb2013-02-19 21:41:27 +0000353 WebRtc_WriteBuffer(WebRtcAec_far_time_buf(aecpc->aec),
354 &farend_ptr[PART_LEN], 1);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000355 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN);
356#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000357 }
358
359 return retVal;
360}
361
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000362int32_t WebRtcAec_Process(void *aecInst, const int16_t *nearend,
363 const int16_t *nearendH, int16_t *out, int16_t *outH,
364 int16_t nrOfSamples, int16_t msInSndCardBuf,
365 int32_t skew)
niklase@google.com470e71d2011-07-07 08:21:25 +0000366{
367 aecpc_t *aecpc = aecInst;
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000368 int32_t retVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000369 if (nearend == NULL) {
370 aecpc->lastError = AEC_NULL_POINTER_ERROR;
371 return -1;
372 }
373
374 if (out == NULL) {
375 aecpc->lastError = AEC_NULL_POINTER_ERROR;
376 return -1;
377 }
378
379 if (aecpc->initFlag != initCheck) {
380 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
381 return -1;
382 }
383
384 // number of samples == 160 for SWB input
385 if (nrOfSamples != 80 && nrOfSamples != 160) {
386 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
387 return -1;
388 }
389
390 // Check for valid pointers based on sampling rate
391 if (aecpc->sampFreq == 32000 && nearendH == NULL) {
392 aecpc->lastError = AEC_NULL_POINTER_ERROR;
393 return -1;
394 }
395
396 if (msInSndCardBuf < 0) {
397 msInSndCardBuf = 0;
398 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
399 retVal = -1;
400 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000401 else if (msInSndCardBuf > kMaxTrustedDelayMs) {
402 // The clamping is now done in ProcessExtended/Normal().
niklase@google.com470e71d2011-07-07 08:21:25 +0000403 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
404 retVal = -1;
405 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000406
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000407 // This returns the value of aec->extended_filter_enabled.
408 if (WebRtcAec_delay_correction_enabled(aecpc->aec)) {
409 ProcessExtended(aecpc, nearend, nearendH, out, outH, nrOfSamples,
410 msInSndCardBuf, skew);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000411 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000412 if (ProcessNormal(aecpc, nearend, nearendH, out, outH, nrOfSamples,
413 msInSndCardBuf, skew) != 0) {
414 retVal = -1;
415 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000416 }
417
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000418#ifdef WEBRTC_AEC_DEBUG_DUMP
419 {
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000420 int16_t far_buf_size_ms = (int16_t)(WebRtcAec_system_delay(aecpc->aec) /
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000421 (sampMsNb * aecpc->rate_factor));
andrew@webrtc.org7d8c5672012-06-01 02:41:14 +0000422 (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile);
423 (void)fwrite(&aecpc->knownDelay, sizeof(aecpc->knownDelay), 1,
424 aecpc->delayFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000425 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000426#endif
427
428 return retVal;
429}
430
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000431int WebRtcAec_set_config(void* handle, AecConfig config) {
432 aecpc_t* self = (aecpc_t*)handle;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000433 if (self->initFlag != initCheck) {
434 self->lastError = AEC_UNINITIALIZED_ERROR;
435 return -1;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000436 }
437
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000438 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
439 self->lastError = AEC_BAD_PARAMETER_ERROR;
440 return -1;
441 }
442 self->skewMode = config.skewMode;
443
444 if (config.nlpMode != kAecNlpConservative && config.nlpMode != kAecNlpModerate
445 && config.nlpMode != kAecNlpAggressive) {
446 self->lastError = AEC_BAD_PARAMETER_ERROR;
447 return -1;
448 }
449
450 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
451 self->lastError = AEC_BAD_PARAMETER_ERROR;
452 return -1;
453 }
454
455 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
456 self->lastError = AEC_BAD_PARAMETER_ERROR;
457 return -1;
458 }
459
460 WebRtcAec_SetConfigCore(self->aec, config.nlpMode, config.metricsMode,
461 config.delay_logging);
462 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000463}
464
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000465int WebRtcAec_get_echo_status(void* handle, int* status) {
466 aecpc_t* self = (aecpc_t*)handle;
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000467 if (status == NULL ) {
468 self->lastError = AEC_NULL_POINTER_ERROR;
469 return -1;
470 }
471 if (self->initFlag != initCheck) {
472 self->lastError = AEC_UNINITIALIZED_ERROR;
473 return -1;
474 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000475
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000476 *status = WebRtcAec_echo_state(self->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000477
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000478 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000479}
480
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000481int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) {
482 const float kUpWeight = 0.7f;
483 float dtmp;
484 int stmp;
485 aecpc_t* self = (aecpc_t*)handle;
bjornv@webrtc.orgcea70f42013-02-19 21:03:10 +0000486 Stats erl;
487 Stats erle;
488 Stats a_nlp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000489
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000490 if (handle == NULL ) {
491 return -1;
492 }
493 if (metrics == NULL ) {
494 self->lastError = AEC_NULL_POINTER_ERROR;
495 return -1;
496 }
497 if (self->initFlag != initCheck) {
498 self->lastError = AEC_UNINITIALIZED_ERROR;
499 return -1;
500 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000501
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000502 WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000503
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000504 // ERL
505 metrics->erl.instant = (int) erl.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000506
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000507 if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000508 // Use a mix between regular average and upper part average.
509 dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average;
510 metrics->erl.average = (int) dtmp;
511 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000512 metrics->erl.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000513 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000514
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000515 metrics->erl.max = (int) erl.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000516
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000517 if (erl.min < (kOffsetLevel * (-1))) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000518 metrics->erl.min = (int) erl.min;
519 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000520 metrics->erl.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 // ERLE
524 metrics->erle.instant = (int) erle.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000525
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000526 if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000527 // Use a mix between regular average and upper part average.
528 dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average;
529 metrics->erle.average = (int) dtmp;
530 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000531 metrics->erle.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000532 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000533
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000534 metrics->erle.max = (int) erle.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000535
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000536 if (erle.min < (kOffsetLevel * (-1))) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000537 metrics->erle.min = (int) erle.min;
538 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000539 metrics->erle.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000540 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000541
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000542 // RERL
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000543 if ((metrics->erl.average > kOffsetLevel)
544 && (metrics->erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000545 stmp = metrics->erl.average + metrics->erle.average;
546 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000547 stmp = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000548 }
549 metrics->rerl.average = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000550
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000551 // No other statistics needed, but returned for completeness.
552 metrics->rerl.instant = stmp;
553 metrics->rerl.max = stmp;
554 metrics->rerl.min = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000555
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000556 // A_NLP
557 metrics->aNlp.instant = (int) a_nlp.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000558
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000559 if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000560 // Use a mix between regular average and upper part average.
561 dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average;
562 metrics->aNlp.average = (int) dtmp;
563 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000564 metrics->aNlp.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000565 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000566
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000567 metrics->aNlp.max = (int) a_nlp.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000568
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000569 if (a_nlp.min < (kOffsetLevel * (-1))) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000570 metrics->aNlp.min = (int) a_nlp.min;
571 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000572 metrics->aNlp.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000573 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000574
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000575 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000576}
577
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000578int WebRtcAec_GetDelayMetrics(void* handle, int* median, int* std) {
579 aecpc_t* self = handle;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000580 if (median == NULL) {
581 self->lastError = AEC_NULL_POINTER_ERROR;
582 return -1;
583 }
584 if (std == NULL) {
585 self->lastError = AEC_NULL_POINTER_ERROR;
586 return -1;
587 }
588 if (self->initFlag != initCheck) {
589 self->lastError = AEC_UNINITIALIZED_ERROR;
590 return -1;
591 }
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000592 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std) == -1) {
593 // Logging disabled.
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000594 self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR;
595 return -1;
596 }
597
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000598 return 0;
599}
600
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000601int32_t WebRtcAec_get_error_code(void *aecInst)
niklase@google.com470e71d2011-07-07 08:21:25 +0000602{
603 aecpc_t *aecpc = aecInst;
niklase@google.com470e71d2011-07-07 08:21:25 +0000604 return aecpc->lastError;
605}
606
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000607AecCore* WebRtcAec_aec_core(void* handle) {
608 if (!handle) {
609 return NULL;
610 }
611 return ((aecpc_t*) handle)->aec;
612}
613
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000614static int ProcessNormal(aecpc_t *aecpc, const int16_t *nearend,
615 const int16_t *nearendH, int16_t *out, int16_t *outH,
616 int16_t nrOfSamples, int16_t msInSndCardBuf,
617 int32_t skew) {
618 int retVal = 0;
619 short i;
620 short nBlocks10ms;
621 short nFrames;
622 // Limit resampling to doubling/halving of signal
623 const float minSkewEst = -0.5f;
624 const float maxSkewEst = 1.0f;
625
626 msInSndCardBuf = msInSndCardBuf > kMaxTrustedDelayMs ?
627 kMaxTrustedDelayMs : msInSndCardBuf;
628 // TODO(andrew): we need to investigate if this +10 is really wanted.
629 msInSndCardBuf += 10;
630 aecpc->msInSndCardBuf = msInSndCardBuf;
631
632 if (aecpc->skewMode == kAecTrue) {
633 if (aecpc->skewFrCtr < 25) {
634 aecpc->skewFrCtr++;
635 }
636 else {
637 retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
638 if (retVal == -1) {
639 aecpc->skew = 0;
640 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
641 }
642
643 aecpc->skew /= aecpc->sampFactor*nrOfSamples;
644
645 if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
646 aecpc->resample = kAecFalse;
647 }
648 else {
649 aecpc->resample = kAecTrue;
650 }
651
652 if (aecpc->skew < minSkewEst) {
653 aecpc->skew = minSkewEst;
654 }
655 else if (aecpc->skew > maxSkewEst) {
656 aecpc->skew = maxSkewEst;
657 }
658
659#ifdef WEBRTC_AEC_DEBUG_DUMP
660 (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile);
661#endif
662 }
663 }
664
665 nFrames = nrOfSamples / FRAME_LEN;
666 nBlocks10ms = nFrames / aecpc->rate_factor;
667
668 if (aecpc->startup_phase) {
669 // Only needed if they don't already point to the same place.
670 if (nearend != out) {
671 memcpy(out, nearend, sizeof(short) * nrOfSamples);
672 }
673 if (nearendH != outH) {
674 memcpy(outH, nearendH, sizeof(short) * nrOfSamples);
675 }
676
677 // The AEC is in the start up mode
678 // AEC is disabled until the system delay is OK
679
680 // Mechanism to ensure that the system delay is reasonably stable.
681 if (aecpc->checkBuffSize) {
682 aecpc->checkBufSizeCtr++;
683 // Before we fill up the far-end buffer we require the system delay
684 // to be stable (+/-8 ms) compared to the first value. This
685 // comparison is made during the following 6 consecutive 10 ms
686 // blocks. If it seems to be stable then we start to fill up the
687 // far-end buffer.
688 if (aecpc->counter == 0) {
689 aecpc->firstVal = aecpc->msInSndCardBuf;
690 aecpc->sum = 0;
691 }
692
693 if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
694 WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
695 aecpc->sum += aecpc->msInSndCardBuf;
696 aecpc->counter++;
697 }
698 else {
699 aecpc->counter = 0;
700 }
701
702 if (aecpc->counter * nBlocks10ms >= 6) {
703 // The far-end buffer size is determined in partitions of
704 // PART_LEN samples. Use 75% of the average value of the system
705 // delay as buffer size to start with.
706 aecpc->bufSizeStart = WEBRTC_SPL_MIN((3 * aecpc->sum *
707 aecpc->rate_factor * 8) / (4 * aecpc->counter * PART_LEN),
708 kMaxBufSizeStart);
709 // Buffer size has now been determined.
710 aecpc->checkBuffSize = 0;
711 }
712
713 if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
714 // For really bad systems, don't disable the echo canceller for
715 // more than 0.5 sec.
716 aecpc->bufSizeStart = WEBRTC_SPL_MIN((aecpc->msInSndCardBuf *
717 aecpc->rate_factor * 3) / 40, kMaxBufSizeStart);
718 aecpc->checkBuffSize = 0;
719 }
720 }
721
722 // If |checkBuffSize| changed in the if-statement above.
723 if (!aecpc->checkBuffSize) {
724 // The system delay is now reasonably stable (or has been unstable
725 // for too long). When the far-end buffer is filled with
726 // approximately the same amount of data as reported by the system
727 // we end the startup phase.
728 int overhead_elements =
729 WebRtcAec_system_delay(aecpc->aec) / PART_LEN - aecpc->bufSizeStart;
730 if (overhead_elements == 0) {
731 // Enable the AEC
732 aecpc->startup_phase = 0;
733 } else if (overhead_elements > 0) {
734 // TODO(bjornv): Do we need a check on how much we actually
735 // moved the read pointer? It should always be possible to move
736 // the pointer |overhead_elements| since we have only added data
737 // to the buffer and no delay compensation nor AEC processing
738 // has been done.
739 WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements);
740
741 // Enable the AEC
742 aecpc->startup_phase = 0;
743 }
744 }
745 } else {
746 // AEC is enabled.
747 EstBufDelayNormal(aecpc);
748
749 // Note that 1 frame is supported for NB and 2 frames for WB.
750 for (i = 0; i < nFrames; i++) {
751 // Call the AEC.
752 WebRtcAec_ProcessFrame(aecpc->aec,
753 &nearend[FRAME_LEN * i],
754 &nearendH[FRAME_LEN * i],
755 aecpc->knownDelay,
756 &out[FRAME_LEN * i],
757 &outH[FRAME_LEN * i]);
758 // TODO(bjornv): Re-structure such that we don't have to pass
759 // |aecpc->knownDelay| as input. Change name to something like
760 // |system_buffer_diff|.
761 }
762 }
763
764 return retVal;
765}
766
767static void ProcessExtended(aecpc_t* self, const int16_t* near,
768 const int16_t* near_high, int16_t* out, int16_t* out_high,
769 int16_t num_samples, int16_t reported_delay_ms, int32_t skew) {
770 int i;
771 const int num_frames = num_samples / FRAME_LEN;
772#if defined(WEBRTC_UNTRUSTED_DELAY)
773 const int delay_diff_offset = kDelayDiffOffsetSamples;
774 reported_delay_ms = kFixedDelayMs;
775#else
776 // This is the usual mode where we trust the reported system delay values.
777 const int delay_diff_offset = 0;
778 // Due to the longer filter, we no longer add 10 ms to the reported delay
779 // to reduce chance of non-causality. Instead we apply a minimum here to avoid
780 // issues with the read pointer jumping around needlessly.
781 reported_delay_ms = reported_delay_ms < kMinTrustedDelayMs ?
782 kMinTrustedDelayMs : reported_delay_ms;
783 // If the reported delay appears to be bogus, we attempt to recover by using
784 // the measured fixed delay values. We use >= here because higher layers
785 // may already clamp to this maximum value, and we would otherwise not
786 // detect it here.
787 reported_delay_ms = reported_delay_ms >= kMaxTrustedDelayMs ?
788 kFixedDelayMs : reported_delay_ms;
789#endif
790 self->msInSndCardBuf = reported_delay_ms;
791
792 if (!self->farend_started) {
793 // Only needed if they don't already point to the same place.
794 if (near != out) {
795 memcpy(out, near, sizeof(short) * num_samples);
796 }
797 if (near_high != out_high) {
798 memcpy(out_high, near_high, sizeof(short) * num_samples);
799 }
800 return;
801 }
802 if (self->startup_phase) {
803 // In the extended mode, there isn't a startup "phase", just a special
804 // action on the first frame. In the trusted delay case, we'll take the
805 // current reported delay, unless it's less then our conservative
806 // measurement.
807 int startup_size_ms = reported_delay_ms < kFixedDelayMs ?
808 kFixedDelayMs : reported_delay_ms;
809 int overhead_elements = (WebRtcAec_system_delay(self->aec) -
810 startup_size_ms / 2 * self->rate_factor * 8) / PART_LEN;
811 WebRtcAec_MoveFarReadPtr(self->aec, overhead_elements);
812 self->startup_phase = 0;
813 }
814
815 EstBufDelayExtended(self);
816
817 for (i = 0; i < num_frames; ++i) {
818 // |delay_diff_offset| gives us the option to manually rewind the delay on
819 // very low delay platforms which can't be expressed purely through
820 // |reported_delay_ms|.
821 WebRtcAec_ProcessFrame(self->aec, &near[FRAME_LEN * i],
822 &near_high[FRAME_LEN * i], self->knownDelay + delay_diff_offset,
823 &out[FRAME_LEN * i], &out_high[FRAME_LEN * i]);
824 }
825}
826
827static void EstBufDelayNormal(aecpc_t* aecpc) {
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000828 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor;
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000829 int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->aec);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000830 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000831
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000832 // Before we proceed with the delay estimate filtering we:
833 // 1) Compensate for the frame that will be read.
834 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000835 // 3) Compensate for non-causality if needed, since the estimated delay can't
836 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000837
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000838 // 1) Compensating for the frame(s) that will be read/processed.
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000839 current_delay += FRAME_LEN * aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000840
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000841 // 2) Account for resampling frame delay.
842 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
843 current_delay -= kResamplingDelay;
844 }
845
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000846 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000847 if (current_delay < PART_LEN) {
848 current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
849 }
850
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000851 // We use -1 to signal an initialized state in the "extended" implementation;
852 // compensate for that.
853 aecpc->filtDelay = aecpc->filtDelay < 0 ? 0 : aecpc->filtDelay;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000854 aecpc->filtDelay = WEBRTC_SPL_MAX(0, (short) (0.8 * aecpc->filtDelay +
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000855 0.2 * current_delay));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000856
857 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
858 if (delay_difference > 224) {
859 if (aecpc->lastDelayDiff < 96) {
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 if (delay_difference < 96 && aecpc->knownDelay > 0) {
865 if (aecpc->lastDelayDiff > 224) {
866 aecpc->timeForDelayChange = 0;
867 } else {
868 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000869 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000870 } else {
871 aecpc->timeForDelayChange = 0;
872 }
873 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000874
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000875 if (aecpc->timeForDelayChange > 25) {
876 aecpc->knownDelay = WEBRTC_SPL_MAX((int) aecpc->filtDelay - 160, 0);
877 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000878}
niklase@google.com470e71d2011-07-07 08:21:25 +0000879
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000880static void EstBufDelayExtended(aecpc_t* self) {
881 int reported_delay = self->msInSndCardBuf * sampMsNb * self->rate_factor;
882 int current_delay = reported_delay - WebRtcAec_system_delay(self->aec);
883 int delay_difference = 0;
884
885 // Before we proceed with the delay estimate filtering we:
886 // 1) Compensate for the frame that will be read.
887 // 2) Compensate for drift resampling.
888 // 3) Compensate for non-causality if needed, since the estimated delay can't
889 // be negative.
890
891 // 1) Compensating for the frame(s) that will be read/processed.
892 current_delay += FRAME_LEN * self->rate_factor;
893
894 // 2) Account for resampling frame delay.
895 if (self->skewMode == kAecTrue && self->resample == kAecTrue) {
896 current_delay -= kResamplingDelay;
897 }
898
899 // 3) Compensate for non-causality, if needed, by flushing two blocks.
900 if (current_delay < PART_LEN) {
901 current_delay += WebRtcAec_MoveFarReadPtr(self->aec, 2) * PART_LEN;
902 }
903
904 if (self->filtDelay == -1) {
905 self->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay);
906 } else {
907 self->filtDelay = WEBRTC_SPL_MAX(0, (short) (0.95 * self->filtDelay +
908 0.05 * current_delay));
909 }
910
911 delay_difference = self->filtDelay - self->knownDelay;
912 if (delay_difference > 384) {
913 if (self->lastDelayDiff < 128) {
914 self->timeForDelayChange = 0;
915 } else {
916 self->timeForDelayChange++;
917 }
918 } else if (delay_difference < 128 && self->knownDelay > 0) {
919 if (self->lastDelayDiff > 384) {
920 self->timeForDelayChange = 0;
921 } else {
922 self->timeForDelayChange++;
923 }
924 } else {
925 self->timeForDelayChange = 0;
926 }
927 self->lastDelayDiff = delay_difference;
928
929 if (self->timeForDelayChange > 25) {
930 self->knownDelay = WEBRTC_SPL_MAX((int) self->filtDelay - 256, 0);
931 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000932}