blob: 57208cd907af4108eef9e2b3769c171362b8eccc [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
andrew@webrtc.orgacb00502013-10-04 16:59:17 +000069
70#if defined(WEBRTC_MAC)
71static const int kDelayDiffOffsetSamples = -160;
72#else
73// Not enabled for now.
74static const int kDelayDiffOffsetSamples = 0;
75#endif
andrew@webrtc.org1760a172013-09-25 23:17:38 +000076#endif
77
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
83static const int kMinTrustedDelayMs = 20;
84static const int kMaxTrustedDelayMs = 500;
85
niklase@google.com470e71d2011-07-07 08:21:25 +000086// Maximum length of resampled signal. Must be an integer multiple of frames
87// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
88// The factor of 2 handles wb, and the + 1 is as a safety margin
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000089// TODO(bjornv): Replace with kResamplerBufferSize
niklase@google.com470e71d2011-07-07 08:21:25 +000090#define MAX_RESAMP_LEN (5 * FRAME_LEN)
91
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000092static const int kMaxBufSizeStart = 62; // In partitions
niklase@google.com470e71d2011-07-07 08:21:25 +000093static const int sampMsNb = 8; // samples per ms in nb
niklase@google.com470e71d2011-07-07 08:21:25 +000094static const int initCheck = 42;
95
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000096#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +000097int webrtc_aec_instance_count = 0;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000098#endif
99
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000100// Estimates delay to set the position of the far-end buffer read pointer
niklase@google.com470e71d2011-07-07 08:21:25 +0000101// (controlled by knownDelay)
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000102static void EstBufDelayNormal(aecpc_t *aecInst);
103static void EstBufDelayExtended(aecpc_t *aecInst);
104static int ProcessNormal(aecpc_t* self, const int16_t* near,
105 const int16_t* near_high, int16_t* out, int16_t* out_high,
106 int16_t num_samples, int16_t reported_delay_ms, int32_t skew);
107static void ProcessExtended(aecpc_t* self, const int16_t* near,
108 const int16_t* near_high, int16_t* out, int16_t* out_high,
109 int16_t num_samples, int16_t reported_delay_ms, int32_t skew);
niklase@google.com470e71d2011-07-07 08:21:25 +0000110
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000111int32_t WebRtcAec_Create(void **aecInst)
niklase@google.com470e71d2011-07-07 08:21:25 +0000112{
113 aecpc_t *aecpc;
114 if (aecInst == NULL) {
115 return -1;
116 }
117
118 aecpc = malloc(sizeof(aecpc_t));
119 *aecInst = aecpc;
120 if (aecpc == NULL) {
121 return -1;
122 }
123
124 if (WebRtcAec_CreateAec(&aecpc->aec) == -1) {
125 WebRtcAec_Free(aecpc);
126 aecpc = NULL;
127 return -1;
128 }
129
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000130 if (WebRtcAec_CreateResampler(&aecpc->resampler) == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000131 WebRtcAec_Free(aecpc);
132 aecpc = NULL;
133 return -1;
134 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000135 // Create far-end pre-buffer. The buffer size has to be large enough for
136 // largest possible drift compensation (kResamplerBufferSize) + "almost" an
137 // FFT buffer (PART_LEN2 - 1).
andrew@webrtc.org91f32552013-02-27 00:35:06 +0000138 aecpc->far_pre_buf = WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize,
139 sizeof(float));
140 if (!aecpc->far_pre_buf) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000141 WebRtcAec_Free(aecpc);
142 aecpc = NULL;
143 return -1;
144 }
145
146 aecpc->initFlag = 0;
147 aecpc->lastError = 0;
148
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000149#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org91f32552013-02-27 00:35:06 +0000150 aecpc->far_pre_buf_s16 = WebRtc_CreateBuffer(
andrew@webrtc.org52b57cc2013-03-07 00:45:50 +0000151 PART_LEN2 + kResamplerBufferSize, sizeof(int16_t));
andrew@webrtc.org91f32552013-02-27 00:35:06 +0000152 if (!aecpc->far_pre_buf_s16) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000153 WebRtcAec_Free(aecpc);
154 aecpc = NULL;
155 return -1;
156 }
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000157 {
158 char filename[64];
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000159 sprintf(filename, "aec_buf%d.dat", webrtc_aec_instance_count);
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000160 aecpc->bufFile = fopen(filename, "wb");
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000161 sprintf(filename, "aec_skew%d.dat", webrtc_aec_instance_count);
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000162 aecpc->skewFile = fopen(filename, "wb");
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000163 sprintf(filename, "aec_delay%d.dat", webrtc_aec_instance_count);
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000164 aecpc->delayFile = fopen(filename, "wb");
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000165 webrtc_aec_instance_count++;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000166 }
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000167#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000168
169 return 0;
170}
171
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000172int32_t WebRtcAec_Free(void *aecInst)
niklase@google.com470e71d2011-07-07 08:21:25 +0000173{
174 aecpc_t *aecpc = aecInst;
175
176 if (aecpc == NULL) {
177 return -1;
178 }
179
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000180 WebRtc_FreeBuffer(aecpc->far_pre_buf);
181
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000182#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000183 WebRtc_FreeBuffer(aecpc->far_pre_buf_s16);
niklase@google.com470e71d2011-07-07 08:21:25 +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
189 WebRtcAec_FreeAec(aecpc->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000190 WebRtcAec_FreeResampler(aecpc->resampler);
191 free(aecpc);
192
193 return 0;
194}
195
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000196int32_t WebRtcAec_Init(void *aecInst, int32_t sampFreq, int32_t scSampFreq)
niklase@google.com470e71d2011-07-07 08:21:25 +0000197{
198 aecpc_t *aecpc = aecInst;
199 AecConfig aecConfig;
200
niklase@google.com470e71d2011-07-07 08:21:25 +0000201 if (sampFreq != 8000 && sampFreq != 16000 && sampFreq != 32000) {
202 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
203 return -1;
204 }
205 aecpc->sampFreq = sampFreq;
206
207 if (scSampFreq < 1 || scSampFreq > 96000) {
208 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
209 return -1;
210 }
211 aecpc->scSampFreq = scSampFreq;
212
213 // Initialize echo canceller core
214 if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) {
215 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
216 return -1;
217 }
218
niklase@google.com470e71d2011-07-07 08:21:25 +0000219 if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) {
220 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
221 return -1;
222 }
223
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000224 if (WebRtc_InitBuffer(aecpc->far_pre_buf) == -1) {
225 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
226 return -1;
227 }
228 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap.
229
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000230 aecpc->initFlag = initCheck; // indicates that initialization has been done
niklase@google.com470e71d2011-07-07 08:21:25 +0000231
232 if (aecpc->sampFreq == 32000) {
233 aecpc->splitSampFreq = 16000;
234 }
235 else {
236 aecpc->splitSampFreq = sampFreq;
237 }
238
niklase@google.com470e71d2011-07-07 08:21:25 +0000239 aecpc->delayCtr = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000240 aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq;
241 // Sampling frequency multiplier (SWB is processed as 160 frame size).
242 aecpc->rate_factor = aecpc->splitSampFreq / 8000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000243
244 aecpc->sum = 0;
245 aecpc->counter = 0;
246 aecpc->checkBuffSize = 1;
247 aecpc->firstVal = 0;
248
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000249 aecpc->startup_phase = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000250 aecpc->bufSizeStart = 0;
251 aecpc->checkBufSizeCtr = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000252 aecpc->msInSndCardBuf = 0;
253 aecpc->filtDelay = -1; // -1 indicates an initialized state.
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000254 aecpc->timeForDelayChange = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000255 aecpc->knownDelay = 0;
256 aecpc->lastDelayDiff = 0;
257
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000258 aecpc->skewFrCtr = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000259 aecpc->resample = kAecFalse;
260 aecpc->highSkewCtr = 0;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000261 aecpc->skew = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000262
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000263 aecpc->farend_started = 0;
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000264
niklase@google.com470e71d2011-07-07 08:21:25 +0000265 // Default settings.
266 aecConfig.nlpMode = kAecNlpModerate;
267 aecConfig.skewMode = kAecFalse;
268 aecConfig.metricsMode = kAecFalse;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000269 aecConfig.delay_logging = kAecFalse;
niklase@google.com470e71d2011-07-07 08:21:25 +0000270
271 if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
272 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
273 return -1;
274 }
275
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000276#ifdef WEBRTC_AEC_DEBUG_DUMP
277 if (WebRtc_InitBuffer(aecpc->far_pre_buf_s16) == -1) {
278 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
279 return -1;
280 }
281 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN); // Start overlap.
282#endif
283
niklase@google.com470e71d2011-07-07 08:21:25 +0000284 return 0;
285}
286
287// only buffer L band for farend
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000288int32_t WebRtcAec_BufferFarend(void *aecInst, const int16_t *farend,
289 int16_t nrOfSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +0000290{
291 aecpc_t *aecpc = aecInst;
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000292 int32_t retVal = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000293 int newNrOfSamples = (int) nrOfSamples;
niklase@google.com470e71d2011-07-07 08:21:25 +0000294 short newFarend[MAX_RESAMP_LEN];
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000295 const int16_t* farend_ptr = farend;
296 float tmp_farend[MAX_RESAMP_LEN];
297 const float* farend_float = tmp_farend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000298 float skew;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000299 int i = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
niklase@google.com470e71d2011-07-07 08:21:25 +0000301 if (farend == NULL) {
302 aecpc->lastError = AEC_NULL_POINTER_ERROR;
303 return -1;
304 }
305
306 if (aecpc->initFlag != initCheck) {
307 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
308 return -1;
309 }
310
311 // number of samples == 160 for SWB input
312 if (nrOfSamples != 80 && nrOfSamples != 160) {
313 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
314 return -1;
315 }
316
317 skew = aecpc->skew;
318
niklase@google.com470e71d2011-07-07 08:21:25 +0000319 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
320 // Resample and get a new number of samples
bjornv@webrtc.org281b7982012-05-30 07:41:57 +0000321 WebRtcAec_ResampleLinear(aecpc->resampler, farend, nrOfSamples, skew,
322 newFarend, &newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000323 farend_ptr = (const int16_t*) newFarend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000324 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000325
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000326 aecpc->farend_started = 1;
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000327 WebRtcAec_SetSystemDelay(aecpc->aec, WebRtcAec_system_delay(aecpc->aec) +
328 newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000329
330#ifdef WEBRTC_AEC_DEBUG_DUMP
331 WebRtc_WriteBuffer(aecpc->far_pre_buf_s16, farend_ptr,
332 (size_t) newNrOfSamples);
333#endif
334 // Cast to float and write the time-domain data to |far_pre_buf|.
335 for (i = 0; i < newNrOfSamples; i++) {
336 tmp_farend[i] = (float) farend_ptr[i];
337 }
338 WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_float,
339 (size_t) newNrOfSamples);
340
341 // Transform to frequency domain if we have enough data.
342 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
343 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
344 WebRtc_ReadBuffer(aecpc->far_pre_buf, (void**) &farend_float, tmp_farend,
345 PART_LEN2);
346
347 WebRtcAec_BufferFarendPartition(aecpc->aec, farend_float);
348
349 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
350 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
351#ifdef WEBRTC_AEC_DEBUG_DUMP
352 WebRtc_ReadBuffer(aecpc->far_pre_buf_s16, (void**) &farend_ptr, newFarend,
353 PART_LEN2);
bjornv@webrtc.org0a480cb2013-02-19 21:41:27 +0000354 WebRtc_WriteBuffer(WebRtcAec_far_time_buf(aecpc->aec),
355 &farend_ptr[PART_LEN], 1);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000356 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN);
357#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000358 }
359
360 return retVal;
361}
362
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000363int32_t WebRtcAec_Process(void *aecInst, const int16_t *nearend,
364 const int16_t *nearendH, int16_t *out, int16_t *outH,
365 int16_t nrOfSamples, int16_t msInSndCardBuf,
366 int32_t skew)
niklase@google.com470e71d2011-07-07 08:21:25 +0000367{
368 aecpc_t *aecpc = aecInst;
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000369 int32_t retVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000370 if (nearend == NULL) {
371 aecpc->lastError = AEC_NULL_POINTER_ERROR;
372 return -1;
373 }
374
375 if (out == NULL) {
376 aecpc->lastError = AEC_NULL_POINTER_ERROR;
377 return -1;
378 }
379
380 if (aecpc->initFlag != initCheck) {
381 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
382 return -1;
383 }
384
385 // number of samples == 160 for SWB input
386 if (nrOfSamples != 80 && nrOfSamples != 160) {
387 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
388 return -1;
389 }
390
391 // Check for valid pointers based on sampling rate
392 if (aecpc->sampFreq == 32000 && nearendH == NULL) {
393 aecpc->lastError = AEC_NULL_POINTER_ERROR;
394 return -1;
395 }
396
397 if (msInSndCardBuf < 0) {
398 msInSndCardBuf = 0;
399 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
400 retVal = -1;
401 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000402 else if (msInSndCardBuf > kMaxTrustedDelayMs) {
403 // The clamping is now done in ProcessExtended/Normal().
niklase@google.com470e71d2011-07-07 08:21:25 +0000404 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
405 retVal = -1;
406 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000407
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000408 // This returns the value of aec->extended_filter_enabled.
409 if (WebRtcAec_delay_correction_enabled(aecpc->aec)) {
410 ProcessExtended(aecpc, nearend, nearendH, out, outH, nrOfSamples,
411 msInSndCardBuf, skew);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000412 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000413 if (ProcessNormal(aecpc, nearend, nearendH, out, outH, nrOfSamples,
414 msInSndCardBuf, skew) != 0) {
415 retVal = -1;
416 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000417 }
418
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000419#ifdef WEBRTC_AEC_DEBUG_DUMP
420 {
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000421 int16_t far_buf_size_ms = (int16_t)(WebRtcAec_system_delay(aecpc->aec) /
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000422 (sampMsNb * aecpc->rate_factor));
andrew@webrtc.org7d8c5672012-06-01 02:41:14 +0000423 (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile);
424 (void)fwrite(&aecpc->knownDelay, sizeof(aecpc->knownDelay), 1,
425 aecpc->delayFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000426 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000427#endif
428
429 return retVal;
430}
431
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000432int WebRtcAec_set_config(void* handle, AecConfig config) {
433 aecpc_t* self = (aecpc_t*)handle;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000434 if (self->initFlag != initCheck) {
435 self->lastError = AEC_UNINITIALIZED_ERROR;
436 return -1;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000437 }
438
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000439 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
440 self->lastError = AEC_BAD_PARAMETER_ERROR;
441 return -1;
442 }
443 self->skewMode = config.skewMode;
444
445 if (config.nlpMode != kAecNlpConservative && config.nlpMode != kAecNlpModerate
446 && config.nlpMode != kAecNlpAggressive) {
447 self->lastError = AEC_BAD_PARAMETER_ERROR;
448 return -1;
449 }
450
451 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
452 self->lastError = AEC_BAD_PARAMETER_ERROR;
453 return -1;
454 }
455
456 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
457 self->lastError = AEC_BAD_PARAMETER_ERROR;
458 return -1;
459 }
460
461 WebRtcAec_SetConfigCore(self->aec, config.nlpMode, config.metricsMode,
462 config.delay_logging);
463 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000464}
465
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000466int WebRtcAec_get_echo_status(void* handle, int* status) {
467 aecpc_t* self = (aecpc_t*)handle;
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000468 if (status == NULL ) {
469 self->lastError = AEC_NULL_POINTER_ERROR;
470 return -1;
471 }
472 if (self->initFlag != initCheck) {
473 self->lastError = AEC_UNINITIALIZED_ERROR;
474 return -1;
475 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000476
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000477 *status = WebRtcAec_echo_state(self->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000478
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000479 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000480}
481
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000482int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) {
483 const float kUpWeight = 0.7f;
484 float dtmp;
485 int stmp;
486 aecpc_t* self = (aecpc_t*)handle;
bjornv@webrtc.orgcea70f42013-02-19 21:03:10 +0000487 Stats erl;
488 Stats erle;
489 Stats a_nlp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000490
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000491 if (handle == NULL ) {
492 return -1;
493 }
494 if (metrics == NULL ) {
495 self->lastError = AEC_NULL_POINTER_ERROR;
496 return -1;
497 }
498 if (self->initFlag != initCheck) {
499 self->lastError = AEC_UNINITIALIZED_ERROR;
500 return -1;
501 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000502
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000503 WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000504
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000505 // ERL
506 metrics->erl.instant = (int) erl.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000507
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000508 if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000509 // Use a mix between regular average and upper part average.
510 dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average;
511 metrics->erl.average = (int) dtmp;
512 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000513 metrics->erl.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000514 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000515
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000516 metrics->erl.max = (int) erl.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000517
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000518 if (erl.min < (kOffsetLevel * (-1))) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000519 metrics->erl.min = (int) erl.min;
520 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000521 metrics->erl.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000522 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000523
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000524 // ERLE
525 metrics->erle.instant = (int) erle.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000526
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000527 if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000528 // Use a mix between regular average and upper part average.
529 dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average;
530 metrics->erle.average = (int) dtmp;
531 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000532 metrics->erle.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000533 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000534
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000535 metrics->erle.max = (int) erle.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000536
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000537 if (erle.min < (kOffsetLevel * (-1))) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000538 metrics->erle.min = (int) erle.min;
539 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000540 metrics->erle.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000541 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000542
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000543 // RERL
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000544 if ((metrics->erl.average > kOffsetLevel)
545 && (metrics->erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000546 stmp = metrics->erl.average + metrics->erle.average;
547 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000548 stmp = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000549 }
550 metrics->rerl.average = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000551
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000552 // No other statistics needed, but returned for completeness.
553 metrics->rerl.instant = stmp;
554 metrics->rerl.max = stmp;
555 metrics->rerl.min = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000556
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000557 // A_NLP
558 metrics->aNlp.instant = (int) a_nlp.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000559
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000560 if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000561 // Use a mix between regular average and upper part average.
562 dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average;
563 metrics->aNlp.average = (int) dtmp;
564 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000565 metrics->aNlp.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000566 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000567
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000568 metrics->aNlp.max = (int) a_nlp.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000569
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000570 if (a_nlp.min < (kOffsetLevel * (-1))) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000571 metrics->aNlp.min = (int) a_nlp.min;
572 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000573 metrics->aNlp.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000574 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000575
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000576 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000577}
578
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000579int WebRtcAec_GetDelayMetrics(void* handle, int* median, int* std) {
580 aecpc_t* self = handle;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000581 if (median == NULL) {
582 self->lastError = AEC_NULL_POINTER_ERROR;
583 return -1;
584 }
585 if (std == NULL) {
586 self->lastError = AEC_NULL_POINTER_ERROR;
587 return -1;
588 }
589 if (self->initFlag != initCheck) {
590 self->lastError = AEC_UNINITIALIZED_ERROR;
591 return -1;
592 }
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000593 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std) == -1) {
594 // Logging disabled.
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000595 self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR;
596 return -1;
597 }
598
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000599 return 0;
600}
601
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000602int32_t WebRtcAec_get_error_code(void *aecInst)
niklase@google.com470e71d2011-07-07 08:21:25 +0000603{
604 aecpc_t *aecpc = aecInst;
niklase@google.com470e71d2011-07-07 08:21:25 +0000605 return aecpc->lastError;
606}
607
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000608AecCore* WebRtcAec_aec_core(void* handle) {
609 if (!handle) {
610 return NULL;
611 }
612 return ((aecpc_t*) handle)->aec;
613}
614
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000615static int ProcessNormal(aecpc_t *aecpc, const int16_t *nearend,
616 const int16_t *nearendH, int16_t *out, int16_t *outH,
617 int16_t nrOfSamples, int16_t msInSndCardBuf,
618 int32_t skew) {
619 int retVal = 0;
620 short i;
621 short nBlocks10ms;
622 short nFrames;
623 // Limit resampling to doubling/halving of signal
624 const float minSkewEst = -0.5f;
625 const float maxSkewEst = 1.0f;
626
627 msInSndCardBuf = msInSndCardBuf > kMaxTrustedDelayMs ?
628 kMaxTrustedDelayMs : msInSndCardBuf;
629 // TODO(andrew): we need to investigate if this +10 is really wanted.
630 msInSndCardBuf += 10;
631 aecpc->msInSndCardBuf = msInSndCardBuf;
632
633 if (aecpc->skewMode == kAecTrue) {
634 if (aecpc->skewFrCtr < 25) {
635 aecpc->skewFrCtr++;
636 }
637 else {
638 retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
639 if (retVal == -1) {
640 aecpc->skew = 0;
641 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
642 }
643
644 aecpc->skew /= aecpc->sampFactor*nrOfSamples;
645
646 if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
647 aecpc->resample = kAecFalse;
648 }
649 else {
650 aecpc->resample = kAecTrue;
651 }
652
653 if (aecpc->skew < minSkewEst) {
654 aecpc->skew = minSkewEst;
655 }
656 else if (aecpc->skew > maxSkewEst) {
657 aecpc->skew = maxSkewEst;
658 }
659
660#ifdef WEBRTC_AEC_DEBUG_DUMP
661 (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile);
662#endif
663 }
664 }
665
666 nFrames = nrOfSamples / FRAME_LEN;
667 nBlocks10ms = nFrames / aecpc->rate_factor;
668
669 if (aecpc->startup_phase) {
670 // Only needed if they don't already point to the same place.
671 if (nearend != out) {
672 memcpy(out, nearend, sizeof(short) * nrOfSamples);
673 }
674 if (nearendH != outH) {
675 memcpy(outH, nearendH, sizeof(short) * nrOfSamples);
676 }
677
678 // The AEC is in the start up mode
679 // AEC is disabled until the system delay is OK
680
681 // Mechanism to ensure that the system delay is reasonably stable.
682 if (aecpc->checkBuffSize) {
683 aecpc->checkBufSizeCtr++;
684 // Before we fill up the far-end buffer we require the system delay
685 // to be stable (+/-8 ms) compared to the first value. This
686 // comparison is made during the following 6 consecutive 10 ms
687 // blocks. If it seems to be stable then we start to fill up the
688 // far-end buffer.
689 if (aecpc->counter == 0) {
690 aecpc->firstVal = aecpc->msInSndCardBuf;
691 aecpc->sum = 0;
692 }
693
694 if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
695 WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
696 aecpc->sum += aecpc->msInSndCardBuf;
697 aecpc->counter++;
698 }
699 else {
700 aecpc->counter = 0;
701 }
702
703 if (aecpc->counter * nBlocks10ms >= 6) {
704 // The far-end buffer size is determined in partitions of
705 // PART_LEN samples. Use 75% of the average value of the system
706 // delay as buffer size to start with.
707 aecpc->bufSizeStart = WEBRTC_SPL_MIN((3 * aecpc->sum *
708 aecpc->rate_factor * 8) / (4 * aecpc->counter * PART_LEN),
709 kMaxBufSizeStart);
710 // Buffer size has now been determined.
711 aecpc->checkBuffSize = 0;
712 }
713
714 if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
715 // For really bad systems, don't disable the echo canceller for
716 // more than 0.5 sec.
717 aecpc->bufSizeStart = WEBRTC_SPL_MIN((aecpc->msInSndCardBuf *
718 aecpc->rate_factor * 3) / 40, kMaxBufSizeStart);
719 aecpc->checkBuffSize = 0;
720 }
721 }
722
723 // If |checkBuffSize| changed in the if-statement above.
724 if (!aecpc->checkBuffSize) {
725 // The system delay is now reasonably stable (or has been unstable
726 // for too long). When the far-end buffer is filled with
727 // approximately the same amount of data as reported by the system
728 // we end the startup phase.
729 int overhead_elements =
730 WebRtcAec_system_delay(aecpc->aec) / PART_LEN - aecpc->bufSizeStart;
731 if (overhead_elements == 0) {
732 // Enable the AEC
733 aecpc->startup_phase = 0;
734 } else if (overhead_elements > 0) {
735 // TODO(bjornv): Do we need a check on how much we actually
736 // moved the read pointer? It should always be possible to move
737 // the pointer |overhead_elements| since we have only added data
738 // to the buffer and no delay compensation nor AEC processing
739 // has been done.
740 WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements);
741
742 // Enable the AEC
743 aecpc->startup_phase = 0;
744 }
745 }
746 } else {
747 // AEC is enabled.
748 EstBufDelayNormal(aecpc);
749
750 // Note that 1 frame is supported for NB and 2 frames for WB.
751 for (i = 0; i < nFrames; i++) {
752 // Call the AEC.
753 WebRtcAec_ProcessFrame(aecpc->aec,
754 &nearend[FRAME_LEN * i],
755 &nearendH[FRAME_LEN * i],
756 aecpc->knownDelay,
757 &out[FRAME_LEN * i],
758 &outH[FRAME_LEN * i]);
759 // TODO(bjornv): Re-structure such that we don't have to pass
760 // |aecpc->knownDelay| as input. Change name to something like
761 // |system_buffer_diff|.
762 }
763 }
764
765 return retVal;
766}
767
768static void ProcessExtended(aecpc_t* self, const int16_t* near,
769 const int16_t* near_high, int16_t* out, int16_t* out_high,
770 int16_t num_samples, int16_t reported_delay_ms, int32_t skew) {
771 int i;
772 const int num_frames = num_samples / FRAME_LEN;
773#if defined(WEBRTC_UNTRUSTED_DELAY)
774 const int delay_diff_offset = kDelayDiffOffsetSamples;
775 reported_delay_ms = kFixedDelayMs;
776#else
777 // This is the usual mode where we trust the reported system delay values.
778 const int delay_diff_offset = 0;
779 // Due to the longer filter, we no longer add 10 ms to the reported delay
780 // to reduce chance of non-causality. Instead we apply a minimum here to avoid
781 // issues with the read pointer jumping around needlessly.
782 reported_delay_ms = reported_delay_ms < kMinTrustedDelayMs ?
783 kMinTrustedDelayMs : reported_delay_ms;
784 // If the reported delay appears to be bogus, we attempt to recover by using
785 // the measured fixed delay values. We use >= here because higher layers
786 // may already clamp to this maximum value, and we would otherwise not
787 // detect it here.
788 reported_delay_ms = reported_delay_ms >= kMaxTrustedDelayMs ?
789 kFixedDelayMs : reported_delay_ms;
790#endif
791 self->msInSndCardBuf = reported_delay_ms;
792
793 if (!self->farend_started) {
794 // Only needed if they don't already point to the same place.
795 if (near != out) {
796 memcpy(out, near, sizeof(short) * num_samples);
797 }
798 if (near_high != out_high) {
799 memcpy(out_high, near_high, sizeof(short) * num_samples);
800 }
801 return;
802 }
803 if (self->startup_phase) {
804 // In the extended mode, there isn't a startup "phase", just a special
805 // action on the first frame. In the trusted delay case, we'll take the
806 // current reported delay, unless it's less then our conservative
807 // measurement.
808 int startup_size_ms = reported_delay_ms < kFixedDelayMs ?
809 kFixedDelayMs : reported_delay_ms;
810 int overhead_elements = (WebRtcAec_system_delay(self->aec) -
811 startup_size_ms / 2 * self->rate_factor * 8) / PART_LEN;
812 WebRtcAec_MoveFarReadPtr(self->aec, overhead_elements);
813 self->startup_phase = 0;
814 }
815
816 EstBufDelayExtended(self);
817
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000818 {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000819 // |delay_diff_offset| gives us the option to manually rewind the delay on
820 // very low delay platforms which can't be expressed purely through
821 // |reported_delay_ms|.
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000822 const int adjusted_known_delay =
823 WEBRTC_SPL_MAX(0, self->knownDelay + delay_diff_offset);
824
825 for (i = 0; i < num_frames; ++i) {
826 WebRtcAec_ProcessFrame(self->aec, &near[FRAME_LEN * i],
827 &near_high[FRAME_LEN * i], adjusted_known_delay,
828 &out[FRAME_LEN * i], &out_high[FRAME_LEN * i]);
829 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000830 }
831}
832
833static void EstBufDelayNormal(aecpc_t* aecpc) {
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000834 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor;
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000835 int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->aec);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000836 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000837
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000838 // Before we proceed with the delay estimate filtering we:
839 // 1) Compensate for the frame that will be read.
840 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000841 // 3) Compensate for non-causality if needed, since the estimated delay can't
842 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000843
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000844 // 1) Compensating for the frame(s) that will be read/processed.
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000845 current_delay += FRAME_LEN * aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000846
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000847 // 2) Account for resampling frame delay.
848 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
849 current_delay -= kResamplingDelay;
850 }
851
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000852 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000853 if (current_delay < PART_LEN) {
854 current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
855 }
856
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000857 // We use -1 to signal an initialized state in the "extended" implementation;
858 // compensate for that.
859 aecpc->filtDelay = aecpc->filtDelay < 0 ? 0 : aecpc->filtDelay;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000860 aecpc->filtDelay = WEBRTC_SPL_MAX(0, (short) (0.8 * aecpc->filtDelay +
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000861 0.2 * current_delay));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000862
863 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
864 if (delay_difference > 224) {
865 if (aecpc->lastDelayDiff < 96) {
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 if (delay_difference < 96 && aecpc->knownDelay > 0) {
871 if (aecpc->lastDelayDiff > 224) {
872 aecpc->timeForDelayChange = 0;
873 } else {
874 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000875 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000876 } else {
877 aecpc->timeForDelayChange = 0;
878 }
879 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000880
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000881 if (aecpc->timeForDelayChange > 25) {
882 aecpc->knownDelay = WEBRTC_SPL_MAX((int) aecpc->filtDelay - 160, 0);
883 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000884}
niklase@google.com470e71d2011-07-07 08:21:25 +0000885
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000886static void EstBufDelayExtended(aecpc_t* self) {
887 int reported_delay = self->msInSndCardBuf * sampMsNb * self->rate_factor;
888 int current_delay = reported_delay - WebRtcAec_system_delay(self->aec);
889 int delay_difference = 0;
890
891 // Before we proceed with the delay estimate filtering we:
892 // 1) Compensate for the frame that will be read.
893 // 2) Compensate for drift resampling.
894 // 3) Compensate for non-causality if needed, since the estimated delay can't
895 // be negative.
896
897 // 1) Compensating for the frame(s) that will be read/processed.
898 current_delay += FRAME_LEN * self->rate_factor;
899
900 // 2) Account for resampling frame delay.
901 if (self->skewMode == kAecTrue && self->resample == kAecTrue) {
902 current_delay -= kResamplingDelay;
903 }
904
905 // 3) Compensate for non-causality, if needed, by flushing two blocks.
906 if (current_delay < PART_LEN) {
907 current_delay += WebRtcAec_MoveFarReadPtr(self->aec, 2) * PART_LEN;
908 }
909
910 if (self->filtDelay == -1) {
911 self->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay);
912 } else {
913 self->filtDelay = WEBRTC_SPL_MAX(0, (short) (0.95 * self->filtDelay +
914 0.05 * current_delay));
915 }
916
917 delay_difference = self->filtDelay - self->knownDelay;
918 if (delay_difference > 384) {
919 if (self->lastDelayDiff < 128) {
920 self->timeForDelayChange = 0;
921 } else {
922 self->timeForDelayChange++;
923 }
924 } else if (delay_difference < 128 && self->knownDelay > 0) {
925 if (self->lastDelayDiff > 384) {
926 self->timeForDelayChange = 0;
927 } else {
928 self->timeForDelayChange++;
929 }
930 } else {
931 self->timeForDelayChange = 0;
932 }
933 self->lastDelayDiff = delay_difference;
934
935 if (self->timeForDelayChange > 25) {
936 self->knownDelay = WEBRTC_SPL_MAX((int) self->filtDelay - 256, 0);
937 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000938}