blob: de26c67cd076394083dcafc405b0797c638f4d9a [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).
75 if (WebRtc_CreateBuffer(&aecpc->far_pre_buf,
76 PART_LEN2 + kResamplerBufferSize,
77 sizeof(float)) == -1) {
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
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000087 if (WebRtc_CreateBuffer(&aecpc->far_pre_buf_s16,
88 PART_LEN2 + kResamplerBufferSize,
89 sizeof(int16_t)) == -1) {
90 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
271 aecpc->aec->system_delay += newNrOfSamples;
272
273#ifdef WEBRTC_AEC_DEBUG_DUMP
274 WebRtc_WriteBuffer(aecpc->far_pre_buf_s16, farend_ptr,
275 (size_t) newNrOfSamples);
276#endif
277 // Cast to float and write the time-domain data to |far_pre_buf|.
278 for (i = 0; i < newNrOfSamples; i++) {
279 tmp_farend[i] = (float) farend_ptr[i];
280 }
281 WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_float,
282 (size_t) newNrOfSamples);
283
284 // Transform to frequency domain if we have enough data.
285 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
286 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
287 WebRtc_ReadBuffer(aecpc->far_pre_buf, (void**) &farend_float, tmp_farend,
288 PART_LEN2);
289
290 WebRtcAec_BufferFarendPartition(aecpc->aec, farend_float);
291
292 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
293 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
294#ifdef WEBRTC_AEC_DEBUG_DUMP
295 WebRtc_ReadBuffer(aecpc->far_pre_buf_s16, (void**) &farend_ptr, newFarend,
296 PART_LEN2);
bjornv@webrtc.org0a480cb2013-02-19 21:41:27 +0000297 WebRtc_WriteBuffer(WebRtcAec_far_time_buf(aecpc->aec),
298 &farend_ptr[PART_LEN], 1);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000299 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN);
300#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000301 }
302
303 return retVal;
304}
305
306WebRtc_Word32 WebRtcAec_Process(void *aecInst, const WebRtc_Word16 *nearend,
307 const WebRtc_Word16 *nearendH, WebRtc_Word16 *out, WebRtc_Word16 *outH,
308 WebRtc_Word16 nrOfSamples, WebRtc_Word16 msInSndCardBuf, WebRtc_Word32 skew)
309{
310 aecpc_t *aecpc = aecInst;
311 WebRtc_Word32 retVal = 0;
312 short i;
niklase@google.com470e71d2011-07-07 08:21:25 +0000313 short nBlocks10ms;
314 short nFrames;
niklase@google.com470e71d2011-07-07 08:21:25 +0000315 // Limit resampling to doubling/halving of signal
316 const float minSkewEst = -0.5f;
317 const float maxSkewEst = 1.0f;
318
319 if (aecpc == NULL) {
320 return -1;
321 }
322
323 if (nearend == NULL) {
324 aecpc->lastError = AEC_NULL_POINTER_ERROR;
325 return -1;
326 }
327
328 if (out == NULL) {
329 aecpc->lastError = AEC_NULL_POINTER_ERROR;
330 return -1;
331 }
332
333 if (aecpc->initFlag != initCheck) {
334 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
335 return -1;
336 }
337
338 // number of samples == 160 for SWB input
339 if (nrOfSamples != 80 && nrOfSamples != 160) {
340 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
341 return -1;
342 }
343
344 // Check for valid pointers based on sampling rate
345 if (aecpc->sampFreq == 32000 && nearendH == NULL) {
346 aecpc->lastError = AEC_NULL_POINTER_ERROR;
347 return -1;
348 }
349
350 if (msInSndCardBuf < 0) {
351 msInSndCardBuf = 0;
352 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
353 retVal = -1;
354 }
355 else if (msInSndCardBuf > 500) {
356 msInSndCardBuf = 500;
357 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
358 retVal = -1;
359 }
andrew@webrtc.orga919d3a2011-11-27 23:40:58 +0000360 // TODO(andrew): we need to investigate if this +10 is really wanted.
niklase@google.com470e71d2011-07-07 08:21:25 +0000361 msInSndCardBuf += 10;
362 aecpc->msInSndCardBuf = msInSndCardBuf;
363
364 if (aecpc->skewMode == kAecTrue) {
365 if (aecpc->skewFrCtr < 25) {
366 aecpc->skewFrCtr++;
367 }
368 else {
369 retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
370 if (retVal == -1) {
371 aecpc->skew = 0;
372 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
373 }
374
375 aecpc->skew /= aecpc->sampFactor*nrOfSamples;
376
377 if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
378 aecpc->resample = kAecFalse;
379 }
380 else {
381 aecpc->resample = kAecTrue;
382 }
383
384 if (aecpc->skew < minSkewEst) {
385 aecpc->skew = minSkewEst;
386 }
387 else if (aecpc->skew > maxSkewEst) {
388 aecpc->skew = maxSkewEst;
389 }
390
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000391#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org7d8c5672012-06-01 02:41:14 +0000392 (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile);
niklase@google.com470e71d2011-07-07 08:21:25 +0000393#endif
394 }
395 }
396
397 nFrames = nrOfSamples / FRAME_LEN;
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000398 nBlocks10ms = nFrames / aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000399
400 if (aecpc->ECstartup) {
andrew@webrtc.org64235092011-08-19 21:22:08 +0000401 if (nearend != out) {
402 // Only needed if they don't already point to the same place.
403 memcpy(out, nearend, sizeof(short) * nrOfSamples);
404 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
406 // The AEC is in the start up mode
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000407 // AEC is disabled until the system delay is OK
niklase@google.com470e71d2011-07-07 08:21:25 +0000408
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000409 // Mechanism to ensure that the system delay is reasonably stable.
niklase@google.com470e71d2011-07-07 08:21:25 +0000410 if (aecpc->checkBuffSize) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000411 aecpc->checkBufSizeCtr++;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000412 // Before we fill up the far-end buffer we require the system delay
413 // to be stable (+/-8 ms) compared to the first value. This
414 // comparison is made during the following 6 consecutive 10 ms
415 // blocks. If it seems to be stable then we start to fill up the
416 // far-end buffer.
niklase@google.com470e71d2011-07-07 08:21:25 +0000417 if (aecpc->counter == 0) {
418 aecpc->firstVal = aecpc->msInSndCardBuf;
419 aecpc->sum = 0;
420 }
421
422 if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
423 WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
424 aecpc->sum += aecpc->msInSndCardBuf;
425 aecpc->counter++;
426 }
427 else {
428 aecpc->counter = 0;
429 }
430
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000431 if (aecpc->counter * nBlocks10ms >= 6) {
432 // The far-end buffer size is determined in partitions of
433 // PART_LEN samples. Use 75% of the average value of the system
434 // delay as buffer size to start with.
435 aecpc->bufSizeStart = WEBRTC_SPL_MIN((3 * aecpc->sum *
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000436 aecpc->rate_factor * 8) / (4 * aecpc->counter * PART_LEN),
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000437 kMaxBufSizeStart);
438 // Buffer size has now been determined.
niklase@google.com470e71d2011-07-07 08:21:25 +0000439 aecpc->checkBuffSize = 0;
440 }
441
442 if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000443 // For really bad systems, don't disable the echo canceller for
444 // more than 0.5 sec.
445 aecpc->bufSizeStart = WEBRTC_SPL_MIN((aecpc->msInSndCardBuf *
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000446 aecpc->rate_factor * 3) / 40, kMaxBufSizeStart);
niklase@google.com470e71d2011-07-07 08:21:25 +0000447 aecpc->checkBuffSize = 0;
448 }
449 }
450
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000451 // If |checkBuffSize| changed in the if-statement above.
niklase@google.com470e71d2011-07-07 08:21:25 +0000452 if (!aecpc->checkBuffSize) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000453 // The system delay is now reasonably stable (or has been unstable
454 // for too long). When the far-end buffer is filled with
455 // approximately the same amount of data as reported by the system
456 // we end the startup phase.
457 int overhead_elements = aecpc->aec->system_delay / PART_LEN -
458 aecpc->bufSizeStart;
459 if (overhead_elements == 0) {
460 // Enable the AEC
461 aecpc->ECstartup = 0;
462 } else if (overhead_elements > 0) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000463 // TODO(bjornv): Do we need a check on how much we actually
464 // moved the read pointer? It should always be possible to move
465 // the pointer |overhead_elements| since we have only added data
466 // to the buffer and no delay compensation nor AEC processing
467 // has been done.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000468 WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements);
niklase@google.com470e71d2011-07-07 08:21:25 +0000469
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000470 // Enable the AEC
niklase@google.com470e71d2011-07-07 08:21:25 +0000471 aecpc->ECstartup = 0;
472 }
473 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000474 } else {
475 // AEC is enabled.
niklase@google.com470e71d2011-07-07 08:21:25 +0000476
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000477 EstBufDelay(aecpc);
478
479 // Note that 1 frame is supported for NB and 2 frames for WB.
niklase@google.com470e71d2011-07-07 08:21:25 +0000480 for (i = 0; i < nFrames; i++) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000481 // Call the AEC.
482 WebRtcAec_ProcessFrame(aecpc->aec,
483 &nearend[FRAME_LEN * i],
484 &nearendH[FRAME_LEN * i],
bjornv@webrtc.org716fd902013-02-20 16:59:41 +0000485 aecpc->knownDelay,
486 &out[FRAME_LEN * i],
487 &outH[FRAME_LEN * i]);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000488 // TODO(bjornv): Re-structure such that we don't have to pass
489 // |aecpc->knownDelay| as input. Change name to something like
490 // |system_buffer_diff|.
niklase@google.com470e71d2011-07-07 08:21:25 +0000491 }
492 }
493
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000494#ifdef WEBRTC_AEC_DEBUG_DUMP
495 {
andrew@webrtc.org7d8c5672012-06-01 02:41:14 +0000496 int16_t far_buf_size_ms = (int16_t)(aecpc->aec->system_delay /
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000497 (sampMsNb * aecpc->rate_factor));
andrew@webrtc.org7d8c5672012-06-01 02:41:14 +0000498 (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile);
499 (void)fwrite(&aecpc->knownDelay, sizeof(aecpc->knownDelay), 1,
500 aecpc->delayFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000501 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000502#endif
503
504 return retVal;
505}
506
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000507int WebRtcAec_set_config(void* handle, AecConfig config) {
508 aecpc_t* self = (aecpc_t*)handle;
niklase@google.com470e71d2011-07-07 08:21:25 +0000509
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000510 if (handle == NULL ) {
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000511 return -1;
512 }
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000513
514 if (self->initFlag != initCheck) {
515 self->lastError = AEC_UNINITIALIZED_ERROR;
516 return -1;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000517 }
518
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000519 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
520 self->lastError = AEC_BAD_PARAMETER_ERROR;
521 return -1;
522 }
523 self->skewMode = config.skewMode;
524
525 if (config.nlpMode != kAecNlpConservative && config.nlpMode != kAecNlpModerate
526 && config.nlpMode != kAecNlpAggressive) {
527 self->lastError = AEC_BAD_PARAMETER_ERROR;
528 return -1;
529 }
530
531 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
532 self->lastError = AEC_BAD_PARAMETER_ERROR;
533 return -1;
534 }
535
536 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
537 self->lastError = AEC_BAD_PARAMETER_ERROR;
538 return -1;
539 }
540
541 WebRtcAec_SetConfigCore(self->aec, config.nlpMode, config.metricsMode,
542 config.delay_logging);
543 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000544}
545
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000546int WebRtcAec_get_echo_status(void* handle, int* status) {
547 aecpc_t* self = (aecpc_t*)handle;
niklase@google.com470e71d2011-07-07 08:21:25 +0000548
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000549 if (handle == NULL ) {
550 return -1;
551 }
552 if (status == NULL ) {
553 self->lastError = AEC_NULL_POINTER_ERROR;
554 return -1;
555 }
556 if (self->initFlag != initCheck) {
557 self->lastError = AEC_UNINITIALIZED_ERROR;
558 return -1;
559 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000560
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000561 *status = WebRtcAec_echo_state(self->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000562
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000563 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000564}
565
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000566int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) {
567 const float kUpWeight = 0.7f;
568 float dtmp;
569 int stmp;
570 aecpc_t* self = (aecpc_t*)handle;
bjornv@webrtc.orgcea70f42013-02-19 21:03:10 +0000571 Stats erl;
572 Stats erle;
573 Stats a_nlp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000574
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000575 if (handle == NULL ) {
576 return -1;
577 }
578 if (metrics == NULL ) {
579 self->lastError = AEC_NULL_POINTER_ERROR;
580 return -1;
581 }
582 if (self->initFlag != initCheck) {
583 self->lastError = AEC_UNINITIALIZED_ERROR;
584 return -1;
585 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000586
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000587 WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000588
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000589 // ERL
590 metrics->erl.instant = (int) erl.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000591
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000592 if ((erl.himean > offsetLevel) && (erl.average > offsetLevel)) {
593 // Use a mix between regular average and upper part average.
594 dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average;
595 metrics->erl.average = (int) dtmp;
596 } else {
597 metrics->erl.average = offsetLevel;
598 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000599
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000600 metrics->erl.max = (int) erl.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000601
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000602 if (erl.min < (offsetLevel * (-1))) {
603 metrics->erl.min = (int) erl.min;
604 } else {
605 metrics->erl.min = offsetLevel;
606 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000607
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000608 // ERLE
609 metrics->erle.instant = (int) erle.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000610
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000611 if ((erle.himean > offsetLevel) && (erle.average > offsetLevel)) {
612 // Use a mix between regular average and upper part average.
613 dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average;
614 metrics->erle.average = (int) dtmp;
615 } else {
616 metrics->erle.average = offsetLevel;
617 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000618
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000619 metrics->erle.max = (int) erle.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000620
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000621 if (erle.min < (offsetLevel * (-1))) {
622 metrics->erle.min = (int) erle.min;
623 } else {
624 metrics->erle.min = offsetLevel;
625 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000626
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000627 // RERL
628 if ((metrics->erl.average > offsetLevel)
629 && (metrics->erle.average > offsetLevel)) {
630 stmp = metrics->erl.average + metrics->erle.average;
631 } else {
632 stmp = offsetLevel;
633 }
634 metrics->rerl.average = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000635
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000636 // No other statistics needed, but returned for completeness.
637 metrics->rerl.instant = stmp;
638 metrics->rerl.max = stmp;
639 metrics->rerl.min = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000640
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000641 // A_NLP
642 metrics->aNlp.instant = (int) a_nlp.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000643
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000644 if ((a_nlp.himean > offsetLevel) && (a_nlp.average > offsetLevel)) {
645 // Use a mix between regular average and upper part average.
646 dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average;
647 metrics->aNlp.average = (int) dtmp;
648 } else {
649 metrics->aNlp.average = offsetLevel;
650 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000651
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000652 metrics->aNlp.max = (int) a_nlp.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000653
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000654 if (a_nlp.min < (offsetLevel * (-1))) {
655 metrics->aNlp.min = (int) a_nlp.min;
656 } else {
657 metrics->aNlp.min = offsetLevel;
658 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000659
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000660 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000661}
662
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000663int WebRtcAec_GetDelayMetrics(void* handle, int* median, int* std) {
664 aecpc_t* self = handle;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000665
bjornv@webrtc.org61d07452012-05-11 07:51:44 +0000666 if (handle == NULL) {
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000667 return -1;
668 }
669 if (median == NULL) {
670 self->lastError = AEC_NULL_POINTER_ERROR;
671 return -1;
672 }
673 if (std == NULL) {
674 self->lastError = AEC_NULL_POINTER_ERROR;
675 return -1;
676 }
677 if (self->initFlag != initCheck) {
678 self->lastError = AEC_UNINITIALIZED_ERROR;
679 return -1;
680 }
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000681 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std) == -1) {
682 // Logging disabled.
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000683 self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR;
684 return -1;
685 }
686
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000687 return 0;
688}
689
niklase@google.com470e71d2011-07-07 08:21:25 +0000690WebRtc_Word32 WebRtcAec_get_error_code(void *aecInst)
691{
692 aecpc_t *aecpc = aecInst;
693
694 if (aecpc == NULL) {
695 return -1;
696 }
697
698 return aecpc->lastError;
699}
700
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000701static int EstBufDelay(aecpc_t* aecpc) {
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000702 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000703 int current_delay = nSampSndCard - aecpc->aec->system_delay;
704 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000705
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000706 // Before we proceed with the delay estimate filtering we:
707 // 1) Compensate for the frame that will be read.
708 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000709 // 3) Compensate for non-causality if needed, since the estimated delay can't
710 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000711
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000712 // 1) Compensating for the frame(s) that will be read/processed.
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000713 current_delay += FRAME_LEN * aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000714
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000715 // 2) Account for resampling frame delay.
716 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
717 current_delay -= kResamplingDelay;
718 }
719
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000720 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000721 if (current_delay < PART_LEN) {
722 current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
723 }
724
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000725 aecpc->filtDelay = WEBRTC_SPL_MAX(0, (short) (0.8 * aecpc->filtDelay +
726 0.2 * current_delay));
727
728 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
729 if (delay_difference > 224) {
730 if (aecpc->lastDelayDiff < 96) {
731 aecpc->timeForDelayChange = 0;
732 } else {
733 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000734 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000735 } else if (delay_difference < 96 && aecpc->knownDelay > 0) {
736 if (aecpc->lastDelayDiff > 224) {
737 aecpc->timeForDelayChange = 0;
738 } else {
739 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000740 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000741 } else {
742 aecpc->timeForDelayChange = 0;
743 }
744 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000745
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000746 if (aecpc->timeForDelayChange > 25) {
747 aecpc->knownDelay = WEBRTC_SPL_MAX((int) aecpc->filtDelay - 160, 0);
748 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000749
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000750 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000751}