blob: 9b711be2250a4ac862dd4355bdcff7896309a6cd [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
niklase@google.com470e71d2011-07-07 08:21:25 +000030// Maximum length of resampled signal. Must be an integer multiple of frames
31// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
32// The factor of 2 handles wb, and the + 1 is as a safety margin
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000033// TODO(bjornv): Replace with kResamplerBufferSize
niklase@google.com470e71d2011-07-07 08:21:25 +000034#define MAX_RESAMP_LEN (5 * FRAME_LEN)
35
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000036static const int kMaxBufSizeStart = 62; // In partitions
niklase@google.com470e71d2011-07-07 08:21:25 +000037static const int sampMsNb = 8; // samples per ms in nb
niklase@google.com470e71d2011-07-07 08:21:25 +000038static const int initCheck = 42;
39
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000040#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +000041int webrtc_aec_instance_count = 0;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000042#endif
43
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000044// Estimates delay to set the position of the far-end buffer read pointer
niklase@google.com470e71d2011-07-07 08:21:25 +000045// (controlled by knownDelay)
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000046static int EstBufDelay(aecpc_t *aecInst);
niklase@google.com470e71d2011-07-07 08:21:25 +000047
48WebRtc_Word32 WebRtcAec_Create(void **aecInst)
49{
50 aecpc_t *aecpc;
51 if (aecInst == NULL) {
52 return -1;
53 }
54
55 aecpc = malloc(sizeof(aecpc_t));
56 *aecInst = aecpc;
57 if (aecpc == NULL) {
58 return -1;
59 }
60
61 if (WebRtcAec_CreateAec(&aecpc->aec) == -1) {
62 WebRtcAec_Free(aecpc);
63 aecpc = NULL;
64 return -1;
65 }
66
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000067 if (WebRtcAec_CreateResampler(&aecpc->resampler) == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +000068 WebRtcAec_Free(aecpc);
69 aecpc = NULL;
70 return -1;
71 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000072 // Create far-end pre-buffer. The buffer size has to be large enough for
73 // largest possible drift compensation (kResamplerBufferSize) + "almost" an
74 // FFT buffer (PART_LEN2 - 1).
andrew@webrtc.org91f32552013-02-27 00:35:06 +000075 aecpc->far_pre_buf = WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize,
76 sizeof(float));
77 if (!aecpc->far_pre_buf) {
niklase@google.com470e71d2011-07-07 08:21:25 +000078 WebRtcAec_Free(aecpc);
79 aecpc = NULL;
80 return -1;
81 }
82
83 aecpc->initFlag = 0;
84 aecpc->lastError = 0;
85
andrew@webrtc.org8594f762011-11-22 00:51:41 +000086#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org91f32552013-02-27 00:35:06 +000087 aecpc->far_pre_buf_s16 = WebRtc_CreateBuffer(
andrew@webrtc.org52b57cc2013-03-07 00:45:50 +000088 PART_LEN2 + kResamplerBufferSize, sizeof(int16_t));
andrew@webrtc.org91f32552013-02-27 00:35:06 +000089 if (!aecpc->far_pre_buf_s16) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000090 WebRtcAec_Free(aecpc);
91 aecpc = NULL;
92 return -1;
93 }
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000094 {
95 char filename[64];
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +000096 sprintf(filename, "aec_buf%d.dat", webrtc_aec_instance_count);
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000097 aecpc->bufFile = fopen(filename, "wb");
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +000098 sprintf(filename, "aec_skew%d.dat", webrtc_aec_instance_count);
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000099 aecpc->skewFile = fopen(filename, "wb");
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000100 sprintf(filename, "aec_delay%d.dat", webrtc_aec_instance_count);
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000101 aecpc->delayFile = fopen(filename, "wb");
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000102 webrtc_aec_instance_count++;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000103 }
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000104#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
106 return 0;
107}
108
109WebRtc_Word32 WebRtcAec_Free(void *aecInst)
110{
111 aecpc_t *aecpc = aecInst;
112
113 if (aecpc == NULL) {
114 return -1;
115 }
116
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000117 WebRtc_FreeBuffer(aecpc->far_pre_buf);
118
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000119#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000120 WebRtc_FreeBuffer(aecpc->far_pre_buf_s16);
niklase@google.com470e71d2011-07-07 08:21:25 +0000121 fclose(aecpc->bufFile);
122 fclose(aecpc->skewFile);
123 fclose(aecpc->delayFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000124#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
126 WebRtcAec_FreeAec(aecpc->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000127 WebRtcAec_FreeResampler(aecpc->resampler);
128 free(aecpc);
129
130 return 0;
131}
132
133WebRtc_Word32 WebRtcAec_Init(void *aecInst, WebRtc_Word32 sampFreq, WebRtc_Word32 scSampFreq)
134{
135 aecpc_t *aecpc = aecInst;
136 AecConfig aecConfig;
137
138 if (aecpc == NULL) {
139 return -1;
140 }
141
142 if (sampFreq != 8000 && sampFreq != 16000 && sampFreq != 32000) {
143 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
144 return -1;
145 }
146 aecpc->sampFreq = sampFreq;
147
148 if (scSampFreq < 1 || scSampFreq > 96000) {
149 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
150 return -1;
151 }
152 aecpc->scSampFreq = scSampFreq;
153
154 // Initialize echo canceller core
155 if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) {
156 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
157 return -1;
158 }
159
niklase@google.com470e71d2011-07-07 08:21:25 +0000160 if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) {
161 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
162 return -1;
163 }
164
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000165 if (WebRtc_InitBuffer(aecpc->far_pre_buf) == -1) {
166 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
167 return -1;
168 }
169 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap.
170
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000171 aecpc->initFlag = initCheck; // indicates that initialization has been done
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
173 if (aecpc->sampFreq == 32000) {
174 aecpc->splitSampFreq = 16000;
175 }
176 else {
177 aecpc->splitSampFreq = sampFreq;
178 }
179
180 aecpc->skewFrCtr = 0;
181 aecpc->activity = 0;
182
niklase@google.com470e71d2011-07-07 08:21:25 +0000183 aecpc->delayCtr = 0;
184
185 aecpc->sum = 0;
186 aecpc->counter = 0;
187 aecpc->checkBuffSize = 1;
188 aecpc->firstVal = 0;
189
190 aecpc->ECstartup = 1;
191 aecpc->bufSizeStart = 0;
192 aecpc->checkBufSizeCtr = 0;
193 aecpc->filtDelay = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000194 aecpc->timeForDelayChange = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000195 aecpc->knownDelay = 0;
196 aecpc->lastDelayDiff = 0;
197
198 aecpc->skew = 0;
199 aecpc->resample = kAecFalse;
200 aecpc->highSkewCtr = 0;
201 aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq;
202
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000203 // Sampling frequency multiplier (SWB is processed as 160 frame size).
204 aecpc->rate_factor = aecpc->splitSampFreq / 8000;
205
niklase@google.com470e71d2011-07-07 08:21:25 +0000206 // Default settings.
207 aecConfig.nlpMode = kAecNlpModerate;
208 aecConfig.skewMode = kAecFalse;
209 aecConfig.metricsMode = kAecFalse;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000210 aecConfig.delay_logging = kAecFalse;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211
212 if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
213 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
214 return -1;
215 }
216
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000217#ifdef WEBRTC_AEC_DEBUG_DUMP
218 if (WebRtc_InitBuffer(aecpc->far_pre_buf_s16) == -1) {
219 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
220 return -1;
221 }
222 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN); // Start overlap.
223#endif
224
niklase@google.com470e71d2011-07-07 08:21:25 +0000225 return 0;
226}
227
228// only buffer L band for farend
229WebRtc_Word32 WebRtcAec_BufferFarend(void *aecInst, const WebRtc_Word16 *farend,
230 WebRtc_Word16 nrOfSamples)
231{
232 aecpc_t *aecpc = aecInst;
233 WebRtc_Word32 retVal = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000234 int newNrOfSamples = (int) nrOfSamples;
niklase@google.com470e71d2011-07-07 08:21:25 +0000235 short newFarend[MAX_RESAMP_LEN];
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000236 const int16_t* farend_ptr = farend;
237 float tmp_farend[MAX_RESAMP_LEN];
238 const float* farend_float = tmp_farend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000239 float skew;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000240 int i = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000241
242 if (aecpc == NULL) {
243 return -1;
244 }
245
246 if (farend == NULL) {
247 aecpc->lastError = AEC_NULL_POINTER_ERROR;
248 return -1;
249 }
250
251 if (aecpc->initFlag != initCheck) {
252 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
253 return -1;
254 }
255
256 // number of samples == 160 for SWB input
257 if (nrOfSamples != 80 && nrOfSamples != 160) {
258 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
259 return -1;
260 }
261
262 skew = aecpc->skew;
263
niklase@google.com470e71d2011-07-07 08:21:25 +0000264 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
265 // Resample and get a new number of samples
bjornv@webrtc.org281b7982012-05-30 07:41:57 +0000266 WebRtcAec_ResampleLinear(aecpc->resampler, farend, nrOfSamples, skew,
267 newFarend, &newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000268 farend_ptr = (const int16_t*) newFarend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000269 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000270
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000271 WebRtcAec_SetSystemDelay(aecpc->aec, WebRtcAec_system_delay(aecpc->aec) +
272 newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000273
274#ifdef WEBRTC_AEC_DEBUG_DUMP
275 WebRtc_WriteBuffer(aecpc->far_pre_buf_s16, farend_ptr,
276 (size_t) newNrOfSamples);
277#endif
278 // Cast to float and write the time-domain data to |far_pre_buf|.
279 for (i = 0; i < newNrOfSamples; i++) {
280 tmp_farend[i] = (float) farend_ptr[i];
281 }
282 WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_float,
283 (size_t) newNrOfSamples);
284
285 // Transform to frequency domain if we have enough data.
286 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
287 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
288 WebRtc_ReadBuffer(aecpc->far_pre_buf, (void**) &farend_float, tmp_farend,
289 PART_LEN2);
290
291 WebRtcAec_BufferFarendPartition(aecpc->aec, farend_float);
292
293 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
294 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
295#ifdef WEBRTC_AEC_DEBUG_DUMP
296 WebRtc_ReadBuffer(aecpc->far_pre_buf_s16, (void**) &farend_ptr, newFarend,
297 PART_LEN2);
bjornv@webrtc.org0a480cb2013-02-19 21:41:27 +0000298 WebRtc_WriteBuffer(WebRtcAec_far_time_buf(aecpc->aec),
299 &farend_ptr[PART_LEN], 1);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000300 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN);
301#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000302 }
303
304 return retVal;
305}
306
307WebRtc_Word32 WebRtcAec_Process(void *aecInst, const WebRtc_Word16 *nearend,
308 const WebRtc_Word16 *nearendH, WebRtc_Word16 *out, WebRtc_Word16 *outH,
309 WebRtc_Word16 nrOfSamples, WebRtc_Word16 msInSndCardBuf, WebRtc_Word32 skew)
310{
311 aecpc_t *aecpc = aecInst;
312 WebRtc_Word32 retVal = 0;
313 short i;
niklase@google.com470e71d2011-07-07 08:21:25 +0000314 short nBlocks10ms;
315 short nFrames;
niklase@google.com470e71d2011-07-07 08:21:25 +0000316 // Limit resampling to doubling/halving of signal
317 const float minSkewEst = -0.5f;
318 const float maxSkewEst = 1.0f;
319
320 if (aecpc == NULL) {
321 return -1;
322 }
323
324 if (nearend == NULL) {
325 aecpc->lastError = AEC_NULL_POINTER_ERROR;
326 return -1;
327 }
328
329 if (out == NULL) {
330 aecpc->lastError = AEC_NULL_POINTER_ERROR;
331 return -1;
332 }
333
334 if (aecpc->initFlag != initCheck) {
335 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
336 return -1;
337 }
338
339 // number of samples == 160 for SWB input
340 if (nrOfSamples != 80 && nrOfSamples != 160) {
341 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
342 return -1;
343 }
344
345 // Check for valid pointers based on sampling rate
346 if (aecpc->sampFreq == 32000 && nearendH == NULL) {
347 aecpc->lastError = AEC_NULL_POINTER_ERROR;
348 return -1;
349 }
350
351 if (msInSndCardBuf < 0) {
352 msInSndCardBuf = 0;
353 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
354 retVal = -1;
355 }
356 else if (msInSndCardBuf > 500) {
357 msInSndCardBuf = 500;
358 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
359 retVal = -1;
360 }
andrew@webrtc.orga919d3a2011-11-27 23:40:58 +0000361 // TODO(andrew): we need to investigate if this +10 is really wanted.
niklase@google.com470e71d2011-07-07 08:21:25 +0000362 msInSndCardBuf += 10;
363 aecpc->msInSndCardBuf = msInSndCardBuf;
364
365 if (aecpc->skewMode == kAecTrue) {
366 if (aecpc->skewFrCtr < 25) {
367 aecpc->skewFrCtr++;
368 }
369 else {
370 retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
371 if (retVal == -1) {
372 aecpc->skew = 0;
373 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
374 }
375
376 aecpc->skew /= aecpc->sampFactor*nrOfSamples;
377
378 if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
379 aecpc->resample = kAecFalse;
380 }
381 else {
382 aecpc->resample = kAecTrue;
383 }
384
385 if (aecpc->skew < minSkewEst) {
386 aecpc->skew = minSkewEst;
387 }
388 else if (aecpc->skew > maxSkewEst) {
389 aecpc->skew = maxSkewEst;
390 }
391
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000392#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org7d8c5672012-06-01 02:41:14 +0000393 (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile);
niklase@google.com470e71d2011-07-07 08:21:25 +0000394#endif
395 }
396 }
397
398 nFrames = nrOfSamples / FRAME_LEN;
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000399 nBlocks10ms = nFrames / aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000400
401 if (aecpc->ECstartup) {
andrew@webrtc.org64235092011-08-19 21:22:08 +0000402 if (nearend != out) {
403 // Only needed if they don't already point to the same place.
404 memcpy(out, nearend, sizeof(short) * nrOfSamples);
405 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000406
407 // The AEC is in the start up mode
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000408 // AEC is disabled until the system delay is OK
niklase@google.com470e71d2011-07-07 08:21:25 +0000409
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000410 // Mechanism to ensure that the system delay is reasonably stable.
niklase@google.com470e71d2011-07-07 08:21:25 +0000411 if (aecpc->checkBuffSize) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000412 aecpc->checkBufSizeCtr++;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000413 // Before we fill up the far-end buffer we require the system delay
414 // to be stable (+/-8 ms) compared to the first value. This
415 // comparison is made during the following 6 consecutive 10 ms
416 // blocks. If it seems to be stable then we start to fill up the
417 // far-end buffer.
niklase@google.com470e71d2011-07-07 08:21:25 +0000418 if (aecpc->counter == 0) {
419 aecpc->firstVal = aecpc->msInSndCardBuf;
420 aecpc->sum = 0;
421 }
422
423 if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
424 WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
425 aecpc->sum += aecpc->msInSndCardBuf;
426 aecpc->counter++;
427 }
428 else {
429 aecpc->counter = 0;
430 }
431
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000432 if (aecpc->counter * nBlocks10ms >= 6) {
433 // The far-end buffer size is determined in partitions of
434 // PART_LEN samples. Use 75% of the average value of the system
435 // delay as buffer size to start with.
436 aecpc->bufSizeStart = WEBRTC_SPL_MIN((3 * aecpc->sum *
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000437 aecpc->rate_factor * 8) / (4 * aecpc->counter * PART_LEN),
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000438 kMaxBufSizeStart);
439 // Buffer size has now been determined.
niklase@google.com470e71d2011-07-07 08:21:25 +0000440 aecpc->checkBuffSize = 0;
441 }
442
443 if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000444 // For really bad systems, don't disable the echo canceller for
445 // more than 0.5 sec.
446 aecpc->bufSizeStart = WEBRTC_SPL_MIN((aecpc->msInSndCardBuf *
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000447 aecpc->rate_factor * 3) / 40, kMaxBufSizeStart);
niklase@google.com470e71d2011-07-07 08:21:25 +0000448 aecpc->checkBuffSize = 0;
449 }
450 }
451
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000452 // If |checkBuffSize| changed in the if-statement above.
niklase@google.com470e71d2011-07-07 08:21:25 +0000453 if (!aecpc->checkBuffSize) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000454 // The system delay is now reasonably stable (or has been unstable
455 // for too long). When the far-end buffer is filled with
456 // approximately the same amount of data as reported by the system
457 // we end the startup phase.
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000458 int overhead_elements =
459 WebRtcAec_system_delay(aecpc->aec) / PART_LEN -
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000460 aecpc->bufSizeStart;
461 if (overhead_elements == 0) {
462 // Enable the AEC
463 aecpc->ECstartup = 0;
464 } else if (overhead_elements > 0) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000465 // TODO(bjornv): Do we need a check on how much we actually
466 // moved the read pointer? It should always be possible to move
467 // the pointer |overhead_elements| since we have only added data
468 // to the buffer and no delay compensation nor AEC processing
469 // has been done.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000470 WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements);
niklase@google.com470e71d2011-07-07 08:21:25 +0000471
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000472 // Enable the AEC
niklase@google.com470e71d2011-07-07 08:21:25 +0000473 aecpc->ECstartup = 0;
474 }
475 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000476 } else {
477 // AEC is enabled.
niklase@google.com470e71d2011-07-07 08:21:25 +0000478
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000479 EstBufDelay(aecpc);
480
481 // Note that 1 frame is supported for NB and 2 frames for WB.
niklase@google.com470e71d2011-07-07 08:21:25 +0000482 for (i = 0; i < nFrames; i++) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000483 // Call the AEC.
484 WebRtcAec_ProcessFrame(aecpc->aec,
485 &nearend[FRAME_LEN * i],
486 &nearendH[FRAME_LEN * i],
bjornv@webrtc.org716fd902013-02-20 16:59:41 +0000487 aecpc->knownDelay,
488 &out[FRAME_LEN * i],
489 &outH[FRAME_LEN * i]);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000490 // TODO(bjornv): Re-structure such that we don't have to pass
491 // |aecpc->knownDelay| as input. Change name to something like
492 // |system_buffer_diff|.
niklase@google.com470e71d2011-07-07 08:21:25 +0000493 }
494 }
495
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000496#ifdef WEBRTC_AEC_DEBUG_DUMP
497 {
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000498 int16_t far_buf_size_ms = (int16_t)(WebRtcAec_system_delay(aecpc->aec) /
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000499 (sampMsNb * aecpc->rate_factor));
andrew@webrtc.org7d8c5672012-06-01 02:41:14 +0000500 (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile);
501 (void)fwrite(&aecpc->knownDelay, sizeof(aecpc->knownDelay), 1,
502 aecpc->delayFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000503 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000504#endif
505
506 return retVal;
507}
508
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000509int WebRtcAec_set_config(void* handle, AecConfig config) {
510 aecpc_t* self = (aecpc_t*)handle;
niklase@google.com470e71d2011-07-07 08:21:25 +0000511
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000512 if (handle == NULL ) {
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000513 return -1;
514 }
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000515
516 if (self->initFlag != initCheck) {
517 self->lastError = AEC_UNINITIALIZED_ERROR;
518 return -1;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000519 }
520
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000521 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
522 self->lastError = AEC_BAD_PARAMETER_ERROR;
523 return -1;
524 }
525 self->skewMode = config.skewMode;
526
527 if (config.nlpMode != kAecNlpConservative && config.nlpMode != kAecNlpModerate
528 && config.nlpMode != kAecNlpAggressive) {
529 self->lastError = AEC_BAD_PARAMETER_ERROR;
530 return -1;
531 }
532
533 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
534 self->lastError = AEC_BAD_PARAMETER_ERROR;
535 return -1;
536 }
537
538 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
539 self->lastError = AEC_BAD_PARAMETER_ERROR;
540 return -1;
541 }
542
543 WebRtcAec_SetConfigCore(self->aec, config.nlpMode, config.metricsMode,
544 config.delay_logging);
545 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000546}
547
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000548int WebRtcAec_get_echo_status(void* handle, int* status) {
549 aecpc_t* self = (aecpc_t*)handle;
niklase@google.com470e71d2011-07-07 08:21:25 +0000550
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000551 if (handle == NULL ) {
552 return -1;
553 }
554 if (status == NULL ) {
555 self->lastError = AEC_NULL_POINTER_ERROR;
556 return -1;
557 }
558 if (self->initFlag != initCheck) {
559 self->lastError = AEC_UNINITIALIZED_ERROR;
560 return -1;
561 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000562
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000563 *status = WebRtcAec_echo_state(self->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000564
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000565 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000566}
567
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000568int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) {
569 const float kUpWeight = 0.7f;
570 float dtmp;
571 int stmp;
572 aecpc_t* self = (aecpc_t*)handle;
bjornv@webrtc.orgcea70f42013-02-19 21:03:10 +0000573 Stats erl;
574 Stats erle;
575 Stats a_nlp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000576
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000577 if (handle == NULL ) {
578 return -1;
579 }
580 if (metrics == NULL ) {
581 self->lastError = AEC_NULL_POINTER_ERROR;
582 return -1;
583 }
584 if (self->initFlag != initCheck) {
585 self->lastError = AEC_UNINITIALIZED_ERROR;
586 return -1;
587 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000588
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000589 WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000590
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000591 // ERL
592 metrics->erl.instant = (int) erl.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000593
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000594 if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000595 // Use a mix between regular average and upper part average.
596 dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average;
597 metrics->erl.average = (int) dtmp;
598 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000599 metrics->erl.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000600 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000601
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000602 metrics->erl.max = (int) erl.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000603
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000604 if (erl.min < (kOffsetLevel * (-1))) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000605 metrics->erl.min = (int) erl.min;
606 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000607 metrics->erl.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000608 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000609
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000610 // ERLE
611 metrics->erle.instant = (int) erle.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000612
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000613 if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000614 // Use a mix between regular average and upper part average.
615 dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average;
616 metrics->erle.average = (int) dtmp;
617 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000618 metrics->erle.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000619 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000620
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000621 metrics->erle.max = (int) erle.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000622
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000623 if (erle.min < (kOffsetLevel * (-1))) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000624 metrics->erle.min = (int) erle.min;
625 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000626 metrics->erle.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000627 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000628
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000629 // RERL
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000630 if ((metrics->erl.average > kOffsetLevel)
631 && (metrics->erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000632 stmp = metrics->erl.average + metrics->erle.average;
633 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000634 stmp = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000635 }
636 metrics->rerl.average = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000637
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000638 // No other statistics needed, but returned for completeness.
639 metrics->rerl.instant = stmp;
640 metrics->rerl.max = stmp;
641 metrics->rerl.min = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000642
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000643 // A_NLP
644 metrics->aNlp.instant = (int) a_nlp.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000645
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000646 if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000647 // Use a mix between regular average and upper part average.
648 dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average;
649 metrics->aNlp.average = (int) dtmp;
650 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000651 metrics->aNlp.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000652 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000653
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000654 metrics->aNlp.max = (int) a_nlp.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000655
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000656 if (a_nlp.min < (kOffsetLevel * (-1))) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000657 metrics->aNlp.min = (int) a_nlp.min;
658 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000659 metrics->aNlp.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000660 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000661
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000662 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000663}
664
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000665int WebRtcAec_GetDelayMetrics(void* handle, int* median, int* std) {
666 aecpc_t* self = handle;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000667
bjornv@webrtc.org61d07452012-05-11 07:51:44 +0000668 if (handle == NULL) {
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000669 return -1;
670 }
671 if (median == NULL) {
672 self->lastError = AEC_NULL_POINTER_ERROR;
673 return -1;
674 }
675 if (std == NULL) {
676 self->lastError = AEC_NULL_POINTER_ERROR;
677 return -1;
678 }
679 if (self->initFlag != initCheck) {
680 self->lastError = AEC_UNINITIALIZED_ERROR;
681 return -1;
682 }
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000683 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std) == -1) {
684 // Logging disabled.
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000685 self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR;
686 return -1;
687 }
688
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000689 return 0;
690}
691
niklase@google.com470e71d2011-07-07 08:21:25 +0000692WebRtc_Word32 WebRtcAec_get_error_code(void *aecInst)
693{
694 aecpc_t *aecpc = aecInst;
695
696 if (aecpc == NULL) {
697 return -1;
698 }
699
700 return aecpc->lastError;
701}
702
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000703AecCore* WebRtcAec_aec_core(void* handle) {
704 if (!handle) {
705 return NULL;
706 }
707 return ((aecpc_t*) handle)->aec;
708}
709
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000710static int EstBufDelay(aecpc_t* aecpc) {
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000711 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor;
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000712 int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->aec);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000713 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000714
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000715 // Before we proceed with the delay estimate filtering we:
716 // 1) Compensate for the frame that will be read.
717 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000718 // 3) Compensate for non-causality if needed, since the estimated delay can't
719 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000720
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000721 // 1) Compensating for the frame(s) that will be read/processed.
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000722 current_delay += FRAME_LEN * aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000723
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000724 // 2) Account for resampling frame delay.
725 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
726 current_delay -= kResamplingDelay;
727 }
728
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000729 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000730 if (current_delay < PART_LEN) {
731 current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
732 }
733
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000734 aecpc->filtDelay = WEBRTC_SPL_MAX(0, (short) (0.8 * aecpc->filtDelay +
735 0.2 * current_delay));
736
737 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
738 if (delay_difference > 224) {
739 if (aecpc->lastDelayDiff < 96) {
740 aecpc->timeForDelayChange = 0;
741 } else {
742 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000743 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000744 } else if (delay_difference < 96 && aecpc->knownDelay > 0) {
745 if (aecpc->lastDelayDiff > 224) {
746 aecpc->timeForDelayChange = 0;
747 } else {
748 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000749 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000750 } else {
751 aecpc->timeForDelayChange = 0;
752 }
753 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000754
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000755 if (aecpc->timeForDelayChange > 25) {
756 aecpc->knownDelay = WEBRTC_SPL_MAX((int) aecpc->filtDelay - 160, 0);
757 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000758
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000759 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000760}