blob: 5e079c5121b18ae7e1442318d3c20be2b258be27 [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@google.com1ba3dbe2011-10-03 08:18:10 +000014#include "echo_cancellation.h"
15
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
niklase@google.com470e71d2011-07-07 08:21:25 +000023#include "aec_core.h"
bjornv@webrtc.org4b80eb42011-11-29 08:44:01 +000024#include "aec_resampler.h"
andrew@webrtc.orgc8d012f2012-01-13 19:43:09 +000025#include "common_audio/signal_processing/include/signal_processing_library.h"
bjornv@webrtc.org70569082012-04-12 12:13:50 +000026#include "modules/audio_processing/aec/echo_cancellation_internal.h"
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +000027#include "ring_buffer.h"
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000028#include "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
38// Target suppression levels for nlp modes
39// log{0.001, 0.00001, 0.00000001}
40static const float targetSupp[3] = {-6.9f, -11.5f, -18.4f};
41static const float minOverDrive[3] = {1.0f, 2.0f, 5.0f};
42static const int initCheck = 42;
43
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000044#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +000045int webrtc_aec_instance_count = 0;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000046#endif
47
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000048// Estimates delay to set the position of the far-end buffer read pointer
niklase@google.com470e71d2011-07-07 08:21:25 +000049// (controlled by knownDelay)
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000050static int EstBufDelay(aecpc_t *aecInst);
niklase@google.com470e71d2011-07-07 08:21:25 +000051
52WebRtc_Word32 WebRtcAec_Create(void **aecInst)
53{
54 aecpc_t *aecpc;
55 if (aecInst == NULL) {
56 return -1;
57 }
58
59 aecpc = malloc(sizeof(aecpc_t));
60 *aecInst = aecpc;
61 if (aecpc == NULL) {
62 return -1;
63 }
64
65 if (WebRtcAec_CreateAec(&aecpc->aec) == -1) {
66 WebRtcAec_Free(aecpc);
67 aecpc = NULL;
68 return -1;
69 }
70
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000071 if (WebRtcAec_CreateResampler(&aecpc->resampler) == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +000072 WebRtcAec_Free(aecpc);
73 aecpc = NULL;
74 return -1;
75 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000076 // Create far-end pre-buffer. The buffer size has to be large enough for
77 // largest possible drift compensation (kResamplerBufferSize) + "almost" an
78 // FFT buffer (PART_LEN2 - 1).
79 if (WebRtc_CreateBuffer(&aecpc->far_pre_buf,
80 PART_LEN2 + kResamplerBufferSize,
81 sizeof(float)) == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +000082 WebRtcAec_Free(aecpc);
83 aecpc = NULL;
84 return -1;
85 }
86
87 aecpc->initFlag = 0;
88 aecpc->lastError = 0;
89
andrew@webrtc.org8594f762011-11-22 00:51:41 +000090#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000091 if (WebRtc_CreateBuffer(&aecpc->far_pre_buf_s16,
92 PART_LEN2 + kResamplerBufferSize,
93 sizeof(int16_t)) == -1) {
94 WebRtcAec_Free(aecpc);
95 aecpc = NULL;
96 return -1;
97 }
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000098 {
99 char filename[64];
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000100 sprintf(filename, "aec_buf%d.dat", webrtc_aec_instance_count);
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000101 aecpc->bufFile = fopen(filename, "wb");
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000102 sprintf(filename, "aec_skew%d.dat", webrtc_aec_instance_count);
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000103 aecpc->skewFile = fopen(filename, "wb");
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000104 sprintf(filename, "aec_delay%d.dat", webrtc_aec_instance_count);
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000105 aecpc->delayFile = fopen(filename, "wb");
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +0000106 webrtc_aec_instance_count++;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000107 }
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000108#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
110 return 0;
111}
112
113WebRtc_Word32 WebRtcAec_Free(void *aecInst)
114{
115 aecpc_t *aecpc = aecInst;
116
117 if (aecpc == NULL) {
118 return -1;
119 }
120
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000121 WebRtc_FreeBuffer(aecpc->far_pre_buf);
122
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000123#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000124 WebRtc_FreeBuffer(aecpc->far_pre_buf_s16);
niklase@google.com470e71d2011-07-07 08:21:25 +0000125 fclose(aecpc->bufFile);
126 fclose(aecpc->skewFile);
127 fclose(aecpc->delayFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000128#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
130 WebRtcAec_FreeAec(aecpc->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000131 WebRtcAec_FreeResampler(aecpc->resampler);
132 free(aecpc);
133
134 return 0;
135}
136
137WebRtc_Word32 WebRtcAec_Init(void *aecInst, WebRtc_Word32 sampFreq, WebRtc_Word32 scSampFreq)
138{
139 aecpc_t *aecpc = aecInst;
140 AecConfig aecConfig;
141
142 if (aecpc == NULL) {
143 return -1;
144 }
145
146 if (sampFreq != 8000 && sampFreq != 16000 && sampFreq != 32000) {
147 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
148 return -1;
149 }
150 aecpc->sampFreq = sampFreq;
151
152 if (scSampFreq < 1 || scSampFreq > 96000) {
153 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
154 return -1;
155 }
156 aecpc->scSampFreq = scSampFreq;
157
158 // Initialize echo canceller core
159 if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) {
160 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
161 return -1;
162 }
163
niklase@google.com470e71d2011-07-07 08:21:25 +0000164 if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) {
165 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
166 return -1;
167 }
168
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000169 if (WebRtc_InitBuffer(aecpc->far_pre_buf) == -1) {
170 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
171 return -1;
172 }
173 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap.
174
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000175 aecpc->initFlag = initCheck; // indicates that initialization has been done
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
177 if (aecpc->sampFreq == 32000) {
178 aecpc->splitSampFreq = 16000;
179 }
180 else {
181 aecpc->splitSampFreq = sampFreq;
182 }
183
184 aecpc->skewFrCtr = 0;
185 aecpc->activity = 0;
186
niklase@google.com470e71d2011-07-07 08:21:25 +0000187 aecpc->delayCtr = 0;
188
189 aecpc->sum = 0;
190 aecpc->counter = 0;
191 aecpc->checkBuffSize = 1;
192 aecpc->firstVal = 0;
193
194 aecpc->ECstartup = 1;
195 aecpc->bufSizeStart = 0;
196 aecpc->checkBufSizeCtr = 0;
197 aecpc->filtDelay = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000198 aecpc->timeForDelayChange = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000199 aecpc->knownDelay = 0;
200 aecpc->lastDelayDiff = 0;
201
202 aecpc->skew = 0;
203 aecpc->resample = kAecFalse;
204 aecpc->highSkewCtr = 0;
205 aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq;
206
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 // Default settings.
208 aecConfig.nlpMode = kAecNlpModerate;
209 aecConfig.skewMode = kAecFalse;
210 aecConfig.metricsMode = kAecFalse;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000211 aecConfig.delay_logging = kAecFalse;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212
213 if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
214 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
215 return -1;
216 }
217
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000218#ifdef WEBRTC_AEC_DEBUG_DUMP
219 if (WebRtc_InitBuffer(aecpc->far_pre_buf_s16) == -1) {
220 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
221 return -1;
222 }
223 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN); // Start overlap.
224#endif
225
niklase@google.com470e71d2011-07-07 08:21:25 +0000226 return 0;
227}
228
229// only buffer L band for farend
230WebRtc_Word32 WebRtcAec_BufferFarend(void *aecInst, const WebRtc_Word16 *farend,
231 WebRtc_Word16 nrOfSamples)
232{
233 aecpc_t *aecpc = aecInst;
234 WebRtc_Word32 retVal = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000235 int newNrOfSamples = (int) nrOfSamples;
niklase@google.com470e71d2011-07-07 08:21:25 +0000236 short newFarend[MAX_RESAMP_LEN];
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000237 const int16_t* farend_ptr = farend;
238 float tmp_farend[MAX_RESAMP_LEN];
239 const float* farend_float = tmp_farend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000240 float skew;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000241 int i = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000242
243 if (aecpc == NULL) {
244 return -1;
245 }
246
247 if (farend == NULL) {
248 aecpc->lastError = AEC_NULL_POINTER_ERROR;
249 return -1;
250 }
251
252 if (aecpc->initFlag != initCheck) {
253 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
254 return -1;
255 }
256
257 // number of samples == 160 for SWB input
258 if (nrOfSamples != 80 && nrOfSamples != 160) {
259 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
260 return -1;
261 }
262
263 skew = aecpc->skew;
264
niklase@google.com470e71d2011-07-07 08:21:25 +0000265 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
266 // Resample and get a new number of samples
bjornv@webrtc.org281b7982012-05-30 07:41:57 +0000267 WebRtcAec_ResampleLinear(aecpc->resampler, farend, nrOfSamples, skew,
268 newFarend, &newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000269 farend_ptr = (const int16_t*) newFarend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000270 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000271
272 aecpc->aec->system_delay += newNrOfSamples;
273
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);
298 WebRtc_WriteBuffer(aecpc->aec->far_time_buf, &farend_ptr[PART_LEN], 1);
299 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;
398 nBlocks10ms = nFrames / aecpc->aec->mult;
399
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 *
436 aecpc->aec->mult * 8) / (4 * aecpc->counter * PART_LEN),
437 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 *
446 aecpc->aec->mult * 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 int out_elements = 0;
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 int16_t* out_ptr = NULL;
484 int16_t out_tmp[FRAME_LEN];
niklase@google.com470e71d2011-07-07 08:21:25 +0000485
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000486 // Call the AEC.
487 WebRtcAec_ProcessFrame(aecpc->aec,
488 &nearend[FRAME_LEN * i],
489 &nearendH[FRAME_LEN * i],
490 aecpc->knownDelay);
491 // TODO(bjornv): Re-structure such that we don't have to pass
492 // |aecpc->knownDelay| as input. Change name to something like
493 // |system_buffer_diff|.
niklase@google.com470e71d2011-07-07 08:21:25 +0000494
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000495 // Stuff the out buffer if we have less than a frame to output.
496 // This should only happen for the first frame.
497 out_elements = (int) WebRtc_available_read(aecpc->aec->outFrBuf);
498 if (out_elements < FRAME_LEN) {
499 WebRtc_MoveReadPtr(aecpc->aec->outFrBuf,
500 out_elements - FRAME_LEN);
501 if (aecpc->sampFreq == 32000) {
502 WebRtc_MoveReadPtr(aecpc->aec->outFrBufH,
503 out_elements - FRAME_LEN);
504 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000505 }
506
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000507 // Obtain an output frame.
508 WebRtc_ReadBuffer(aecpc->aec->outFrBuf, (void**) &out_ptr,
509 out_tmp, FRAME_LEN);
510 memcpy(&out[FRAME_LEN * i], out_ptr, sizeof(int16_t) * FRAME_LEN);
511 // For H band
512 if (aecpc->sampFreq == 32000) {
513 WebRtc_ReadBuffer(aecpc->aec->outFrBufH, (void**) &out_ptr,
514 out_tmp, FRAME_LEN);
515 memcpy(&outH[FRAME_LEN * i], out_ptr,
516 sizeof(int16_t) * FRAME_LEN);
niklase@google.com470e71d2011-07-07 08:21:25 +0000517 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000518 }
519 }
520
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000521#ifdef WEBRTC_AEC_DEBUG_DUMP
522 {
andrew@webrtc.org7d8c5672012-06-01 02:41:14 +0000523 int16_t far_buf_size_ms = (int16_t)(aecpc->aec->system_delay /
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000524 (sampMsNb * aecpc->aec->mult));
andrew@webrtc.org7d8c5672012-06-01 02:41:14 +0000525 (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile);
526 (void)fwrite(&aecpc->knownDelay, sizeof(aecpc->knownDelay), 1,
527 aecpc->delayFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000528 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000529#endif
530
531 return retVal;
532}
533
534WebRtc_Word32 WebRtcAec_set_config(void *aecInst, AecConfig config)
535{
536 aecpc_t *aecpc = aecInst;
537
538 if (aecpc == NULL) {
539 return -1;
540 }
541
542 if (aecpc->initFlag != initCheck) {
543 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
544 return -1;
545 }
546
547 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
548 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
549 return -1;
550 }
551 aecpc->skewMode = config.skewMode;
552
553 if (config.nlpMode != kAecNlpConservative && config.nlpMode !=
554 kAecNlpModerate && config.nlpMode != kAecNlpAggressive) {
555 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
556 return -1;
557 }
558 aecpc->nlpMode = config.nlpMode;
559 aecpc->aec->targetSupp = targetSupp[aecpc->nlpMode];
560 aecpc->aec->minOverDrive = minOverDrive[aecpc->nlpMode];
561
562 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
563 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
564 return -1;
565 }
566 aecpc->aec->metricsMode = config.metricsMode;
567 if (aecpc->aec->metricsMode == kAecTrue) {
568 WebRtcAec_InitMetrics(aecpc->aec);
569 }
570
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000571 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
572 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
573 return -1;
574 }
575 aecpc->aec->delay_logging_enabled = config.delay_logging;
576 if (aecpc->aec->delay_logging_enabled == kAecTrue) {
577 memset(aecpc->aec->delay_histogram, 0, sizeof(aecpc->aec->delay_histogram));
578 }
579
niklase@google.com470e71d2011-07-07 08:21:25 +0000580 return 0;
581}
582
583WebRtc_Word32 WebRtcAec_get_config(void *aecInst, AecConfig *config)
584{
585 aecpc_t *aecpc = aecInst;
586
587 if (aecpc == NULL) {
588 return -1;
589 }
590
591 if (config == NULL) {
592 aecpc->lastError = AEC_NULL_POINTER_ERROR;
593 return -1;
594 }
595
596 if (aecpc->initFlag != initCheck) {
597 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
598 return -1;
599 }
600
601 config->nlpMode = aecpc->nlpMode;
602 config->skewMode = aecpc->skewMode;
603 config->metricsMode = aecpc->aec->metricsMode;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000604 config->delay_logging = aecpc->aec->delay_logging_enabled;
niklase@google.com470e71d2011-07-07 08:21:25 +0000605
606 return 0;
607}
608
609WebRtc_Word32 WebRtcAec_get_echo_status(void *aecInst, WebRtc_Word16 *status)
610{
611 aecpc_t *aecpc = aecInst;
612
613 if (aecpc == NULL) {
614 return -1;
615 }
616
617 if (status == NULL) {
618 aecpc->lastError = AEC_NULL_POINTER_ERROR;
619 return -1;
620 }
621
622 if (aecpc->initFlag != initCheck) {
623 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
624 return -1;
625 }
626
627 *status = aecpc->aec->echoState;
628
629 return 0;
630}
631
632WebRtc_Word32 WebRtcAec_GetMetrics(void *aecInst, AecMetrics *metrics)
633{
634 const float upweight = 0.7f;
635 float dtmp;
636 short stmp;
637 aecpc_t *aecpc = aecInst;
638
639 if (aecpc == NULL) {
640 return -1;
641 }
642
643 if (metrics == NULL) {
644 aecpc->lastError = AEC_NULL_POINTER_ERROR;
645 return -1;
646 }
647
648 if (aecpc->initFlag != initCheck) {
649 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
650 return -1;
651 }
652
653 // ERL
654 metrics->erl.instant = (short) aecpc->aec->erl.instant;
655
656 if ((aecpc->aec->erl.himean > offsetLevel) && (aecpc->aec->erl.average > offsetLevel)) {
657 // Use a mix between regular average and upper part average
658 dtmp = upweight * aecpc->aec->erl.himean + (1 - upweight) * aecpc->aec->erl.average;
659 metrics->erl.average = (short) dtmp;
660 }
661 else {
662 metrics->erl.average = offsetLevel;
663 }
664
665 metrics->erl.max = (short) aecpc->aec->erl.max;
666
667 if (aecpc->aec->erl.min < (offsetLevel * (-1))) {
668 metrics->erl.min = (short) aecpc->aec->erl.min;
669 }
670 else {
671 metrics->erl.min = offsetLevel;
672 }
673
674 // ERLE
675 metrics->erle.instant = (short) aecpc->aec->erle.instant;
676
677 if ((aecpc->aec->erle.himean > offsetLevel) && (aecpc->aec->erle.average > offsetLevel)) {
678 // Use a mix between regular average and upper part average
679 dtmp = upweight * aecpc->aec->erle.himean + (1 - upweight) * aecpc->aec->erle.average;
680 metrics->erle.average = (short) dtmp;
681 }
682 else {
683 metrics->erle.average = offsetLevel;
684 }
685
686 metrics->erle.max = (short) aecpc->aec->erle.max;
687
688 if (aecpc->aec->erle.min < (offsetLevel * (-1))) {
689 metrics->erle.min = (short) aecpc->aec->erle.min;
690 } else {
691 metrics->erle.min = offsetLevel;
692 }
693
694 // RERL
695 if ((metrics->erl.average > offsetLevel) && (metrics->erle.average > offsetLevel)) {
696 stmp = metrics->erl.average + metrics->erle.average;
697 }
698 else {
699 stmp = offsetLevel;
700 }
701 metrics->rerl.average = stmp;
702
703 // No other statistics needed, but returned for completeness
704 metrics->rerl.instant = stmp;
705 metrics->rerl.max = stmp;
706 metrics->rerl.min = stmp;
707
708 // A_NLP
709 metrics->aNlp.instant = (short) aecpc->aec->aNlp.instant;
710
711 if ((aecpc->aec->aNlp.himean > offsetLevel) && (aecpc->aec->aNlp.average > offsetLevel)) {
712 // Use a mix between regular average and upper part average
713 dtmp = upweight * aecpc->aec->aNlp.himean + (1 - upweight) * aecpc->aec->aNlp.average;
714 metrics->aNlp.average = (short) dtmp;
715 }
716 else {
717 metrics->aNlp.average = offsetLevel;
718 }
719
720 metrics->aNlp.max = (short) aecpc->aec->aNlp.max;
721
722 if (aecpc->aec->aNlp.min < (offsetLevel * (-1))) {
723 metrics->aNlp.min = (short) aecpc->aec->aNlp.min;
724 }
725 else {
726 metrics->aNlp.min = offsetLevel;
727 }
728
729 return 0;
730}
731
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000732int WebRtcAec_GetDelayMetrics(void* handle, int* median, int* std) {
733 aecpc_t* self = handle;
734 int i = 0;
735 int delay_values = 0;
736 int num_delay_values = 0;
737 int my_median = 0;
bjornv@webrtc.org4c636762011-10-19 08:47:40 +0000738 const int kMsPerBlock = (PART_LEN * 1000) / self->splitSampFreq;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000739 float l1_norm = 0;
740
bjornv@webrtc.org61d07452012-05-11 07:51:44 +0000741 if (handle == NULL) {
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000742 return -1;
743 }
744 if (median == NULL) {
745 self->lastError = AEC_NULL_POINTER_ERROR;
746 return -1;
747 }
748 if (std == NULL) {
749 self->lastError = AEC_NULL_POINTER_ERROR;
750 return -1;
751 }
752 if (self->initFlag != initCheck) {
753 self->lastError = AEC_UNINITIALIZED_ERROR;
754 return -1;
755 }
756 if (self->aec->delay_logging_enabled == 0) {
757 // Logging disabled
758 self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR;
759 return -1;
760 }
761
762 // Get number of delay values since last update
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000763 for (i = 0; i < kHistorySizeBlocks; i++) {
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000764 num_delay_values += self->aec->delay_histogram[i];
765 }
766 if (num_delay_values == 0) {
andrew@webrtc.orga919d3a2011-11-27 23:40:58 +0000767 // We have no new delay value data. Even though -1 is a valid estimate, it
768 // will practically never be used since multiples of |kMsPerBlock| will
769 // always be returned.
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000770 *median = -1;
771 *std = -1;
772 return 0;
773 }
774
775 delay_values = num_delay_values >> 1; // Start value for median count down
776 // Get median of delay values since last update
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000777 for (i = 0; i < kHistorySizeBlocks; i++) {
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000778 delay_values -= self->aec->delay_histogram[i];
779 if (delay_values < 0) {
780 my_median = i;
781 break;
782 }
783 }
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000784 // Account for lookahead.
785 *median = (my_median - kLookaheadBlocks) * kMsPerBlock;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000786
787 // Calculate the L1 norm, with median value as central moment
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000788 for (i = 0; i < kHistorySizeBlocks; i++) {
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000789 l1_norm += (float) (fabs(i - my_median) * self->aec->delay_histogram[i]);
790 }
bjornv@webrtc.org4c636762011-10-19 08:47:40 +0000791 *std = (int) (l1_norm / (float) num_delay_values + 0.5f) * kMsPerBlock;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000792
793 // Reset histogram
794 memset(self->aec->delay_histogram, 0, sizeof(self->aec->delay_histogram));
795
796 return 0;
797}
798
niklase@google.com470e71d2011-07-07 08:21:25 +0000799WebRtc_Word32 WebRtcAec_get_error_code(void *aecInst)
800{
801 aecpc_t *aecpc = aecInst;
802
803 if (aecpc == NULL) {
804 return -1;
805 }
806
807 return aecpc->lastError;
808}
809
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000810static int EstBufDelay(aecpc_t* aecpc) {
811 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->aec->mult;
812 int current_delay = nSampSndCard - aecpc->aec->system_delay;
813 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000814
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000815 // Before we proceed with the delay estimate filtering we:
816 // 1) Compensate for the frame that will be read.
817 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000818 // 3) Compensate for non-causality if needed, since the estimated delay can't
819 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000820
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000821 // 1) Compensating for the frame(s) that will be read/processed.
822 current_delay += FRAME_LEN * aecpc->aec->mult;
niklase@google.com470e71d2011-07-07 08:21:25 +0000823
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000824 // 2) Account for resampling frame delay.
825 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
826 current_delay -= kResamplingDelay;
827 }
828
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000829 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000830 if (current_delay < PART_LEN) {
831 current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
832 }
833
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000834 aecpc->filtDelay = WEBRTC_SPL_MAX(0, (short) (0.8 * aecpc->filtDelay +
835 0.2 * current_delay));
836
837 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
838 if (delay_difference > 224) {
839 if (aecpc->lastDelayDiff < 96) {
840 aecpc->timeForDelayChange = 0;
841 } else {
842 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000843 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000844 } else if (delay_difference < 96 && aecpc->knownDelay > 0) {
845 if (aecpc->lastDelayDiff > 224) {
846 aecpc->timeForDelayChange = 0;
847 } else {
848 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000849 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000850 } else {
851 aecpc->timeForDelayChange = 0;
852 }
853 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000854
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000855 if (aecpc->timeForDelayChange > 25) {
856 aecpc->knownDelay = WEBRTC_SPL_MAX((int) aecpc->filtDelay - 160, 0);
857 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000858
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000859 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000860}