blob: bde9c87c5b9d423fa5a042499f6bbeeb4408f755 [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@google.com1ba3dbe2011-10-03 08:18:10 +000026#include "ring_buffer.h"
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000027#include "typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000028
niklase@google.com470e71d2011-07-07 08:21:25 +000029// Maximum length of resampled signal. Must be an integer multiple of frames
30// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
31// The factor of 2 handles wb, and the + 1 is as a safety margin
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000032// TODO(bjornv): Replace with kResamplerBufferSize
niklase@google.com470e71d2011-07-07 08:21:25 +000033#define MAX_RESAMP_LEN (5 * FRAME_LEN)
34
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000035static const int kMaxBufSizeStart = 62; // In partitions
niklase@google.com470e71d2011-07-07 08:21:25 +000036static const int sampMsNb = 8; // samples per ms in nb
37// Target suppression levels for nlp modes
38// log{0.001, 0.00001, 0.00000001}
39static const float targetSupp[3] = {-6.9f, -11.5f, -18.4f};
40static const float minOverDrive[3] = {1.0f, 2.0f, 5.0f};
41static const int initCheck = 42;
42
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000043#ifdef WEBRTC_AEC_DEBUG_DUMP
44static int instance_count = 0;
45#endif
46
niklase@google.com470e71d2011-07-07 08:21:25 +000047typedef struct {
48 int delayCtr;
49 int sampFreq;
50 int splitSampFreq;
51 int scSampFreq;
52 float sampFactor; // scSampRate / sampFreq
53 short nlpMode;
54 short autoOnOff;
55 short activity;
56 short skewMode;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000057 int bufSizeStart;
niklase@google.com470e71d2011-07-07 08:21:25 +000058 //short bufResetCtr; // counts number of noncausal frames
59 int knownDelay;
60
niklase@google.com470e71d2011-07-07 08:21:25 +000061 short initFlag; // indicates if AEC has been initialized
62
63 // Variables used for averaging far end buffer size
64 short counter;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000065 int sum;
niklase@google.com470e71d2011-07-07 08:21:25 +000066 short firstVal;
67 short checkBufSizeCtr;
68
69 // Variables used for delay shifts
70 short msInSndCardBuf;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000071 short filtDelay; // Filtered delay estimate.
niklase@google.com470e71d2011-07-07 08:21:25 +000072 int timeForDelayChange;
73 int ECstartup;
74 int checkBuffSize;
niklase@google.com470e71d2011-07-07 08:21:25 +000075 short lastDelayDiff;
76
andrew@webrtc.org8594f762011-11-22 00:51:41 +000077#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000078 void* far_pre_buf_s16; // Time domain far-end pre-buffer in int16_t.
niklase@google.com470e71d2011-07-07 08:21:25 +000079 FILE *bufFile;
80 FILE *delayFile;
81 FILE *skewFile;
andrew@webrtc.org8594f762011-11-22 00:51:41 +000082#endif
niklase@google.com470e71d2011-07-07 08:21:25 +000083
84 // Structures
niklase@google.com470e71d2011-07-07 08:21:25 +000085 void *resampler;
86
87 int skewFrCtr;
88 int resample; // if the skew is small enough we don't resample
89 int highSkewCtr;
90 float skew;
91
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000092 void* far_pre_buf; // Time domain far-end pre-buffer.
93
niklase@google.com470e71d2011-07-07 08:21:25 +000094 int lastError;
95
96 aec_t *aec;
97} aecpc_t;
98
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000099// Estimates delay to set the position of the far-end buffer read pointer
niklase@google.com470e71d2011-07-07 08:21:25 +0000100// (controlled by knownDelay)
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000101static int EstBufDelay(aecpc_t *aecInst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
103WebRtc_Word32 WebRtcAec_Create(void **aecInst)
104{
105 aecpc_t *aecpc;
106 if (aecInst == NULL) {
107 return -1;
108 }
109
110 aecpc = malloc(sizeof(aecpc_t));
111 *aecInst = aecpc;
112 if (aecpc == NULL) {
113 return -1;
114 }
115
116 if (WebRtcAec_CreateAec(&aecpc->aec) == -1) {
117 WebRtcAec_Free(aecpc);
118 aecpc = NULL;
119 return -1;
120 }
121
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000122 if (WebRtcAec_CreateResampler(&aecpc->resampler) == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000123 WebRtcAec_Free(aecpc);
124 aecpc = NULL;
125 return -1;
126 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000127 // Create far-end pre-buffer. The buffer size has to be large enough for
128 // largest possible drift compensation (kResamplerBufferSize) + "almost" an
129 // FFT buffer (PART_LEN2 - 1).
130 if (WebRtc_CreateBuffer(&aecpc->far_pre_buf,
131 PART_LEN2 + kResamplerBufferSize,
132 sizeof(float)) == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000133 WebRtcAec_Free(aecpc);
134 aecpc = NULL;
135 return -1;
136 }
137
138 aecpc->initFlag = 0;
139 aecpc->lastError = 0;
140
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000141#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000142 if (WebRtc_CreateBuffer(&aecpc->far_pre_buf_s16,
143 PART_LEN2 + kResamplerBufferSize,
144 sizeof(int16_t)) == -1) {
145 WebRtcAec_Free(aecpc);
146 aecpc = NULL;
147 return -1;
148 }
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000149 {
150 char filename[64];
151 sprintf(filename, "aec_far%d.pcm", instance_count);
152 aecpc->aec->farFile = fopen(filename, "wb");
153 sprintf(filename, "aec_near%d.pcm", instance_count);
154 aecpc->aec->nearFile = fopen(filename, "wb");
155 sprintf(filename, "aec_out%d.pcm", instance_count);
156 aecpc->aec->outFile = fopen(filename, "wb");
157 sprintf(filename, "aec_out_linear%d.pcm", instance_count);
158 aecpc->aec->outLinearFile = fopen(filename, "wb");
159 sprintf(filename, "aec_buf%d.dat", instance_count);
160 aecpc->bufFile = fopen(filename, "wb");
161 sprintf(filename, "aec_skew%d.dat", instance_count);
162 aecpc->skewFile = fopen(filename, "wb");
163 sprintf(filename, "aec_delay%d.dat", instance_count);
164 aecpc->delayFile = fopen(filename, "wb");
165 instance_count++;
166 }
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000167#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000168
169 return 0;
170}
171
172WebRtc_Word32 WebRtcAec_Free(void *aecInst)
173{
174 aecpc_t *aecpc = aecInst;
175
176 if (aecpc == NULL) {
177 return -1;
178 }
179
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000180 WebRtc_FreeBuffer(aecpc->far_pre_buf);
181
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000182#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000183 WebRtc_FreeBuffer(aecpc->far_pre_buf_s16);
niklase@google.com470e71d2011-07-07 08:21:25 +0000184 fclose(aecpc->aec->farFile);
185 fclose(aecpc->aec->nearFile);
186 fclose(aecpc->aec->outFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000187 fclose(aecpc->aec->outLinearFile);
niklase@google.com470e71d2011-07-07 08:21:25 +0000188 fclose(aecpc->bufFile);
189 fclose(aecpc->skewFile);
190 fclose(aecpc->delayFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000191#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000192
193 WebRtcAec_FreeAec(aecpc->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000194 WebRtcAec_FreeResampler(aecpc->resampler);
195 free(aecpc);
196
197 return 0;
198}
199
200WebRtc_Word32 WebRtcAec_Init(void *aecInst, WebRtc_Word32 sampFreq, WebRtc_Word32 scSampFreq)
201{
202 aecpc_t *aecpc = aecInst;
203 AecConfig aecConfig;
204
205 if (aecpc == NULL) {
206 return -1;
207 }
208
209 if (sampFreq != 8000 && sampFreq != 16000 && sampFreq != 32000) {
210 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
211 return -1;
212 }
213 aecpc->sampFreq = sampFreq;
214
215 if (scSampFreq < 1 || scSampFreq > 96000) {
216 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
217 return -1;
218 }
219 aecpc->scSampFreq = scSampFreq;
220
221 // Initialize echo canceller core
222 if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) {
223 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
224 return -1;
225 }
226
niklase@google.com470e71d2011-07-07 08:21:25 +0000227 if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) {
228 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
229 return -1;
230 }
231
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000232 if (WebRtc_InitBuffer(aecpc->far_pre_buf) == -1) {
233 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
234 return -1;
235 }
236 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap.
237
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000238 aecpc->initFlag = initCheck; // indicates that initialization has been done
niklase@google.com470e71d2011-07-07 08:21:25 +0000239
240 if (aecpc->sampFreq == 32000) {
241 aecpc->splitSampFreq = 16000;
242 }
243 else {
244 aecpc->splitSampFreq = sampFreq;
245 }
246
247 aecpc->skewFrCtr = 0;
248 aecpc->activity = 0;
249
niklase@google.com470e71d2011-07-07 08:21:25 +0000250 aecpc->delayCtr = 0;
251
252 aecpc->sum = 0;
253 aecpc->counter = 0;
254 aecpc->checkBuffSize = 1;
255 aecpc->firstVal = 0;
256
257 aecpc->ECstartup = 1;
258 aecpc->bufSizeStart = 0;
259 aecpc->checkBufSizeCtr = 0;
260 aecpc->filtDelay = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000261 aecpc->timeForDelayChange = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000262 aecpc->knownDelay = 0;
263 aecpc->lastDelayDiff = 0;
264
265 aecpc->skew = 0;
266 aecpc->resample = kAecFalse;
267 aecpc->highSkewCtr = 0;
268 aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq;
269
niklase@google.com470e71d2011-07-07 08:21:25 +0000270 // Default settings.
271 aecConfig.nlpMode = kAecNlpModerate;
272 aecConfig.skewMode = kAecFalse;
273 aecConfig.metricsMode = kAecFalse;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000274 aecConfig.delay_logging = kAecFalse;
niklase@google.com470e71d2011-07-07 08:21:25 +0000275
276 if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
277 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
278 return -1;
279 }
280
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000281#ifdef WEBRTC_AEC_DEBUG_DUMP
282 if (WebRtc_InitBuffer(aecpc->far_pre_buf_s16) == -1) {
283 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
284 return -1;
285 }
286 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN); // Start overlap.
287#endif
288
niklase@google.com470e71d2011-07-07 08:21:25 +0000289 return 0;
290}
291
292// only buffer L band for farend
293WebRtc_Word32 WebRtcAec_BufferFarend(void *aecInst, const WebRtc_Word16 *farend,
294 WebRtc_Word16 nrOfSamples)
295{
296 aecpc_t *aecpc = aecInst;
297 WebRtc_Word32 retVal = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000298 int newNrOfSamples = (int) nrOfSamples;
niklase@google.com470e71d2011-07-07 08:21:25 +0000299 short newFarend[MAX_RESAMP_LEN];
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000300 const int16_t* farend_ptr = farend;
301 float tmp_farend[MAX_RESAMP_LEN];
302 const float* farend_float = tmp_farend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000303 float skew;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000304 int i = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000305
306 if (aecpc == NULL) {
307 return -1;
308 }
309
310 if (farend == NULL) {
311 aecpc->lastError = AEC_NULL_POINTER_ERROR;
312 return -1;
313 }
314
315 if (aecpc->initFlag != initCheck) {
316 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
317 return -1;
318 }
319
320 // number of samples == 160 for SWB input
321 if (nrOfSamples != 80 && nrOfSamples != 160) {
322 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
323 return -1;
324 }
325
326 skew = aecpc->skew;
327
niklase@google.com470e71d2011-07-07 08:21:25 +0000328 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
329 // Resample and get a new number of samples
330 newNrOfSamples = WebRtcAec_ResampleLinear(aecpc->resampler,
331 farend,
332 nrOfSamples,
333 skew,
334 newFarend);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000335 farend_ptr = (const int16_t*) newFarend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000336 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000337
338 aecpc->aec->system_delay += newNrOfSamples;
339
340#ifdef WEBRTC_AEC_DEBUG_DUMP
341 WebRtc_WriteBuffer(aecpc->far_pre_buf_s16, farend_ptr,
342 (size_t) newNrOfSamples);
343#endif
344 // Cast to float and write the time-domain data to |far_pre_buf|.
345 for (i = 0; i < newNrOfSamples; i++) {
346 tmp_farend[i] = (float) farend_ptr[i];
347 }
348 WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_float,
349 (size_t) newNrOfSamples);
350
351 // Transform to frequency domain if we have enough data.
352 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
353 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
354 WebRtc_ReadBuffer(aecpc->far_pre_buf, (void**) &farend_float, tmp_farend,
355 PART_LEN2);
356
357 WebRtcAec_BufferFarendPartition(aecpc->aec, farend_float);
358
359 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
360 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
361#ifdef WEBRTC_AEC_DEBUG_DUMP
362 WebRtc_ReadBuffer(aecpc->far_pre_buf_s16, (void**) &farend_ptr, newFarend,
363 PART_LEN2);
364 WebRtc_WriteBuffer(aecpc->aec->far_time_buf, &farend_ptr[PART_LEN], 1);
365 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN);
366#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000367 }
368
369 return retVal;
370}
371
372WebRtc_Word32 WebRtcAec_Process(void *aecInst, const WebRtc_Word16 *nearend,
373 const WebRtc_Word16 *nearendH, WebRtc_Word16 *out, WebRtc_Word16 *outH,
374 WebRtc_Word16 nrOfSamples, WebRtc_Word16 msInSndCardBuf, WebRtc_Word32 skew)
375{
376 aecpc_t *aecpc = aecInst;
377 WebRtc_Word32 retVal = 0;
378 short i;
niklase@google.com470e71d2011-07-07 08:21:25 +0000379 short nBlocks10ms;
380 short nFrames;
niklase@google.com470e71d2011-07-07 08:21:25 +0000381 // Limit resampling to doubling/halving of signal
382 const float minSkewEst = -0.5f;
383 const float maxSkewEst = 1.0f;
384
385 if (aecpc == NULL) {
386 return -1;
387 }
388
389 if (nearend == NULL) {
390 aecpc->lastError = AEC_NULL_POINTER_ERROR;
391 return -1;
392 }
393
394 if (out == NULL) {
395 aecpc->lastError = AEC_NULL_POINTER_ERROR;
396 return -1;
397 }
398
399 if (aecpc->initFlag != initCheck) {
400 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
401 return -1;
402 }
403
404 // number of samples == 160 for SWB input
405 if (nrOfSamples != 80 && nrOfSamples != 160) {
406 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
407 return -1;
408 }
409
410 // Check for valid pointers based on sampling rate
411 if (aecpc->sampFreq == 32000 && nearendH == NULL) {
412 aecpc->lastError = AEC_NULL_POINTER_ERROR;
413 return -1;
414 }
415
416 if (msInSndCardBuf < 0) {
417 msInSndCardBuf = 0;
418 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
419 retVal = -1;
420 }
421 else if (msInSndCardBuf > 500) {
422 msInSndCardBuf = 500;
423 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
424 retVal = -1;
425 }
andrew@webrtc.orga919d3a2011-11-27 23:40:58 +0000426 // TODO(andrew): we need to investigate if this +10 is really wanted.
niklase@google.com470e71d2011-07-07 08:21:25 +0000427 msInSndCardBuf += 10;
428 aecpc->msInSndCardBuf = msInSndCardBuf;
429
430 if (aecpc->skewMode == kAecTrue) {
431 if (aecpc->skewFrCtr < 25) {
432 aecpc->skewFrCtr++;
433 }
434 else {
435 retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
436 if (retVal == -1) {
437 aecpc->skew = 0;
438 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
439 }
440
441 aecpc->skew /= aecpc->sampFactor*nrOfSamples;
442
443 if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
444 aecpc->resample = kAecFalse;
445 }
446 else {
447 aecpc->resample = kAecTrue;
448 }
449
450 if (aecpc->skew < minSkewEst) {
451 aecpc->skew = minSkewEst;
452 }
453 else if (aecpc->skew > maxSkewEst) {
454 aecpc->skew = maxSkewEst;
455 }
456
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000457#ifdef WEBRTC_AEC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000458 fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile);
459#endif
460 }
461 }
462
463 nFrames = nrOfSamples / FRAME_LEN;
464 nBlocks10ms = nFrames / aecpc->aec->mult;
465
466 if (aecpc->ECstartup) {
andrew@webrtc.org64235092011-08-19 21:22:08 +0000467 if (nearend != out) {
468 // Only needed if they don't already point to the same place.
469 memcpy(out, nearend, sizeof(short) * nrOfSamples);
470 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000471
472 // The AEC is in the start up mode
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000473 // AEC is disabled until the system delay is OK
niklase@google.com470e71d2011-07-07 08:21:25 +0000474
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000475 // Mechanism to ensure that the system delay is reasonably stable.
niklase@google.com470e71d2011-07-07 08:21:25 +0000476 if (aecpc->checkBuffSize) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000477 aecpc->checkBufSizeCtr++;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000478 // Before we fill up the far-end buffer we require the system delay
479 // to be stable (+/-8 ms) compared to the first value. This
480 // comparison is made during the following 6 consecutive 10 ms
481 // blocks. If it seems to be stable then we start to fill up the
482 // far-end buffer.
niklase@google.com470e71d2011-07-07 08:21:25 +0000483 if (aecpc->counter == 0) {
484 aecpc->firstVal = aecpc->msInSndCardBuf;
485 aecpc->sum = 0;
486 }
487
488 if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
489 WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
490 aecpc->sum += aecpc->msInSndCardBuf;
491 aecpc->counter++;
492 }
493 else {
494 aecpc->counter = 0;
495 }
496
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000497 if (aecpc->counter * nBlocks10ms >= 6) {
498 // The far-end buffer size is determined in partitions of
499 // PART_LEN samples. Use 75% of the average value of the system
500 // delay as buffer size to start with.
501 aecpc->bufSizeStart = WEBRTC_SPL_MIN((3 * aecpc->sum *
502 aecpc->aec->mult * 8) / (4 * aecpc->counter * PART_LEN),
503 kMaxBufSizeStart);
504 // Buffer size has now been determined.
niklase@google.com470e71d2011-07-07 08:21:25 +0000505 aecpc->checkBuffSize = 0;
506 }
507
508 if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000509 // For really bad systems, don't disable the echo canceller for
510 // more than 0.5 sec.
511 aecpc->bufSizeStart = WEBRTC_SPL_MIN((aecpc->msInSndCardBuf *
512 aecpc->aec->mult * 3) / 40, kMaxBufSizeStart);
niklase@google.com470e71d2011-07-07 08:21:25 +0000513 aecpc->checkBuffSize = 0;
514 }
515 }
516
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000517 // If |checkBuffSize| changed in the if-statement above.
niklase@google.com470e71d2011-07-07 08:21:25 +0000518 if (!aecpc->checkBuffSize) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000519 // The system delay is now reasonably stable (or has been unstable
520 // for too long). When the far-end buffer is filled with
521 // approximately the same amount of data as reported by the system
522 // we end the startup phase.
523 int overhead_elements = aecpc->aec->system_delay / PART_LEN -
524 aecpc->bufSizeStart;
525 if (overhead_elements == 0) {
526 // Enable the AEC
527 aecpc->ECstartup = 0;
528 } else if (overhead_elements > 0) {
529 WebRtc_MoveReadPtr(aecpc->aec->far_buf_windowed,
530 overhead_elements);
531 WebRtc_MoveReadPtr(aecpc->aec->far_buf, overhead_elements);
532#ifdef WEBRTC_AEC_DEBUG_DUMP
533 WebRtc_MoveReadPtr(aecpc->aec->far_time_buf, overhead_elements);
534#endif
535 // TODO(bjornv): Do we need a check on how much we actually
536 // moved the read pointer? It should always be possible to move
537 // the pointer |overhead_elements| since we have only added data
538 // to the buffer and no delay compensation nor AEC processing
539 // has been done.
540 aecpc->aec->system_delay -= overhead_elements * PART_LEN;
niklase@google.com470e71d2011-07-07 08:21:25 +0000541
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000542 // Enable the AEC
niklase@google.com470e71d2011-07-07 08:21:25 +0000543 aecpc->ECstartup = 0;
544 }
545 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000546 } else {
547 // AEC is enabled.
niklase@google.com470e71d2011-07-07 08:21:25 +0000548
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000549 int out_elements = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000550
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000551 EstBufDelay(aecpc);
552
553 // Note that 1 frame is supported for NB and 2 frames for WB.
niklase@google.com470e71d2011-07-07 08:21:25 +0000554 for (i = 0; i < nFrames; i++) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000555 int16_t* out_ptr = NULL;
556 int16_t out_tmp[FRAME_LEN];
niklase@google.com470e71d2011-07-07 08:21:25 +0000557
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000558 // Call the AEC.
559 WebRtcAec_ProcessFrame(aecpc->aec,
560 &nearend[FRAME_LEN * i],
561 &nearendH[FRAME_LEN * i],
562 aecpc->knownDelay);
563 // TODO(bjornv): Re-structure such that we don't have to pass
564 // |aecpc->knownDelay| as input. Change name to something like
565 // |system_buffer_diff|.
niklase@google.com470e71d2011-07-07 08:21:25 +0000566
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000567 // Stuff the out buffer if we have less than a frame to output.
568 // This should only happen for the first frame.
569 out_elements = (int) WebRtc_available_read(aecpc->aec->outFrBuf);
570 if (out_elements < FRAME_LEN) {
571 WebRtc_MoveReadPtr(aecpc->aec->outFrBuf,
572 out_elements - FRAME_LEN);
573 if (aecpc->sampFreq == 32000) {
574 WebRtc_MoveReadPtr(aecpc->aec->outFrBufH,
575 out_elements - FRAME_LEN);
576 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000577 }
578
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000579 // Obtain an output frame.
580 WebRtc_ReadBuffer(aecpc->aec->outFrBuf, (void**) &out_ptr,
581 out_tmp, FRAME_LEN);
582 memcpy(&out[FRAME_LEN * i], out_ptr, sizeof(int16_t) * FRAME_LEN);
583 // For H band
584 if (aecpc->sampFreq == 32000) {
585 WebRtc_ReadBuffer(aecpc->aec->outFrBufH, (void**) &out_ptr,
586 out_tmp, FRAME_LEN);
587 memcpy(&outH[FRAME_LEN * i], out_ptr,
588 sizeof(int16_t) * FRAME_LEN);
niklase@google.com470e71d2011-07-07 08:21:25 +0000589 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000590 }
591 }
592
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000593#ifdef WEBRTC_AEC_DEBUG_DUMP
594 {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000595 int16_t far_buf_size_ms = (int16_t) (aecpc->aec->system_delay /
596 (sampMsNb * aecpc->aec->mult));
597 fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000598 fwrite(&(aecpc->knownDelay), sizeof(aecpc->knownDelay), 1, aecpc->delayFile);
599 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000600#endif
601
602 return retVal;
603}
604
605WebRtc_Word32 WebRtcAec_set_config(void *aecInst, AecConfig config)
606{
607 aecpc_t *aecpc = aecInst;
608
609 if (aecpc == NULL) {
610 return -1;
611 }
612
613 if (aecpc->initFlag != initCheck) {
614 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
615 return -1;
616 }
617
618 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
619 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
620 return -1;
621 }
622 aecpc->skewMode = config.skewMode;
623
624 if (config.nlpMode != kAecNlpConservative && config.nlpMode !=
625 kAecNlpModerate && config.nlpMode != kAecNlpAggressive) {
626 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
627 return -1;
628 }
629 aecpc->nlpMode = config.nlpMode;
630 aecpc->aec->targetSupp = targetSupp[aecpc->nlpMode];
631 aecpc->aec->minOverDrive = minOverDrive[aecpc->nlpMode];
632
633 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
634 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
635 return -1;
636 }
637 aecpc->aec->metricsMode = config.metricsMode;
638 if (aecpc->aec->metricsMode == kAecTrue) {
639 WebRtcAec_InitMetrics(aecpc->aec);
640 }
641
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000642 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
643 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
644 return -1;
645 }
646 aecpc->aec->delay_logging_enabled = config.delay_logging;
647 if (aecpc->aec->delay_logging_enabled == kAecTrue) {
648 memset(aecpc->aec->delay_histogram, 0, sizeof(aecpc->aec->delay_histogram));
649 }
650
niklase@google.com470e71d2011-07-07 08:21:25 +0000651 return 0;
652}
653
654WebRtc_Word32 WebRtcAec_get_config(void *aecInst, AecConfig *config)
655{
656 aecpc_t *aecpc = aecInst;
657
658 if (aecpc == NULL) {
659 return -1;
660 }
661
662 if (config == NULL) {
663 aecpc->lastError = AEC_NULL_POINTER_ERROR;
664 return -1;
665 }
666
667 if (aecpc->initFlag != initCheck) {
668 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
669 return -1;
670 }
671
672 config->nlpMode = aecpc->nlpMode;
673 config->skewMode = aecpc->skewMode;
674 config->metricsMode = aecpc->aec->metricsMode;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000675 config->delay_logging = aecpc->aec->delay_logging_enabled;
niklase@google.com470e71d2011-07-07 08:21:25 +0000676
677 return 0;
678}
679
680WebRtc_Word32 WebRtcAec_get_echo_status(void *aecInst, WebRtc_Word16 *status)
681{
682 aecpc_t *aecpc = aecInst;
683
684 if (aecpc == NULL) {
685 return -1;
686 }
687
688 if (status == NULL) {
689 aecpc->lastError = AEC_NULL_POINTER_ERROR;
690 return -1;
691 }
692
693 if (aecpc->initFlag != initCheck) {
694 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
695 return -1;
696 }
697
698 *status = aecpc->aec->echoState;
699
700 return 0;
701}
702
703WebRtc_Word32 WebRtcAec_GetMetrics(void *aecInst, AecMetrics *metrics)
704{
705 const float upweight = 0.7f;
706 float dtmp;
707 short stmp;
708 aecpc_t *aecpc = aecInst;
709
710 if (aecpc == NULL) {
711 return -1;
712 }
713
714 if (metrics == NULL) {
715 aecpc->lastError = AEC_NULL_POINTER_ERROR;
716 return -1;
717 }
718
719 if (aecpc->initFlag != initCheck) {
720 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
721 return -1;
722 }
723
724 // ERL
725 metrics->erl.instant = (short) aecpc->aec->erl.instant;
726
727 if ((aecpc->aec->erl.himean > offsetLevel) && (aecpc->aec->erl.average > offsetLevel)) {
728 // Use a mix between regular average and upper part average
729 dtmp = upweight * aecpc->aec->erl.himean + (1 - upweight) * aecpc->aec->erl.average;
730 metrics->erl.average = (short) dtmp;
731 }
732 else {
733 metrics->erl.average = offsetLevel;
734 }
735
736 metrics->erl.max = (short) aecpc->aec->erl.max;
737
738 if (aecpc->aec->erl.min < (offsetLevel * (-1))) {
739 metrics->erl.min = (short) aecpc->aec->erl.min;
740 }
741 else {
742 metrics->erl.min = offsetLevel;
743 }
744
745 // ERLE
746 metrics->erle.instant = (short) aecpc->aec->erle.instant;
747
748 if ((aecpc->aec->erle.himean > offsetLevel) && (aecpc->aec->erle.average > offsetLevel)) {
749 // Use a mix between regular average and upper part average
750 dtmp = upweight * aecpc->aec->erle.himean + (1 - upweight) * aecpc->aec->erle.average;
751 metrics->erle.average = (short) dtmp;
752 }
753 else {
754 metrics->erle.average = offsetLevel;
755 }
756
757 metrics->erle.max = (short) aecpc->aec->erle.max;
758
759 if (aecpc->aec->erle.min < (offsetLevel * (-1))) {
760 metrics->erle.min = (short) aecpc->aec->erle.min;
761 } else {
762 metrics->erle.min = offsetLevel;
763 }
764
765 // RERL
766 if ((metrics->erl.average > offsetLevel) && (metrics->erle.average > offsetLevel)) {
767 stmp = metrics->erl.average + metrics->erle.average;
768 }
769 else {
770 stmp = offsetLevel;
771 }
772 metrics->rerl.average = stmp;
773
774 // No other statistics needed, but returned for completeness
775 metrics->rerl.instant = stmp;
776 metrics->rerl.max = stmp;
777 metrics->rerl.min = stmp;
778
779 // A_NLP
780 metrics->aNlp.instant = (short) aecpc->aec->aNlp.instant;
781
782 if ((aecpc->aec->aNlp.himean > offsetLevel) && (aecpc->aec->aNlp.average > offsetLevel)) {
783 // Use a mix between regular average and upper part average
784 dtmp = upweight * aecpc->aec->aNlp.himean + (1 - upweight) * aecpc->aec->aNlp.average;
785 metrics->aNlp.average = (short) dtmp;
786 }
787 else {
788 metrics->aNlp.average = offsetLevel;
789 }
790
791 metrics->aNlp.max = (short) aecpc->aec->aNlp.max;
792
793 if (aecpc->aec->aNlp.min < (offsetLevel * (-1))) {
794 metrics->aNlp.min = (short) aecpc->aec->aNlp.min;
795 }
796 else {
797 metrics->aNlp.min = offsetLevel;
798 }
799
800 return 0;
801}
802
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000803int WebRtcAec_GetDelayMetrics(void* handle, int* median, int* std) {
804 aecpc_t* self = handle;
805 int i = 0;
806 int delay_values = 0;
807 int num_delay_values = 0;
808 int my_median = 0;
bjornv@webrtc.org4c636762011-10-19 08:47:40 +0000809 const int kMsPerBlock = (PART_LEN * 1000) / self->splitSampFreq;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000810 float l1_norm = 0;
811
812 if (self == NULL) {
813 return -1;
814 }
815 if (median == NULL) {
816 self->lastError = AEC_NULL_POINTER_ERROR;
817 return -1;
818 }
819 if (std == NULL) {
820 self->lastError = AEC_NULL_POINTER_ERROR;
821 return -1;
822 }
823 if (self->initFlag != initCheck) {
824 self->lastError = AEC_UNINITIALIZED_ERROR;
825 return -1;
826 }
827 if (self->aec->delay_logging_enabled == 0) {
828 // Logging disabled
829 self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR;
830 return -1;
831 }
832
833 // Get number of delay values since last update
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000834 for (i = 0; i < kHistorySizeBlocks; i++) {
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000835 num_delay_values += self->aec->delay_histogram[i];
836 }
837 if (num_delay_values == 0) {
andrew@webrtc.orga919d3a2011-11-27 23:40:58 +0000838 // We have no new delay value data. Even though -1 is a valid estimate, it
839 // will practically never be used since multiples of |kMsPerBlock| will
840 // always be returned.
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000841 *median = -1;
842 *std = -1;
843 return 0;
844 }
845
846 delay_values = num_delay_values >> 1; // Start value for median count down
847 // Get median of delay values since last update
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000848 for (i = 0; i < kHistorySizeBlocks; i++) {
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000849 delay_values -= self->aec->delay_histogram[i];
850 if (delay_values < 0) {
851 my_median = i;
852 break;
853 }
854 }
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000855 // Account for lookahead.
856 *median = (my_median - kLookaheadBlocks) * kMsPerBlock;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000857
858 // Calculate the L1 norm, with median value as central moment
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000859 for (i = 0; i < kHistorySizeBlocks; i++) {
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000860 l1_norm += (float) (fabs(i - my_median) * self->aec->delay_histogram[i]);
861 }
bjornv@webrtc.org4c636762011-10-19 08:47:40 +0000862 *std = (int) (l1_norm / (float) num_delay_values + 0.5f) * kMsPerBlock;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000863
864 // Reset histogram
865 memset(self->aec->delay_histogram, 0, sizeof(self->aec->delay_histogram));
866
867 return 0;
868}
869
niklase@google.com470e71d2011-07-07 08:21:25 +0000870WebRtc_Word32 WebRtcAec_get_error_code(void *aecInst)
871{
872 aecpc_t *aecpc = aecInst;
873
874 if (aecpc == NULL) {
875 return -1;
876 }
877
878 return aecpc->lastError;
879}
880
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000881static int EstBufDelay(aecpc_t* aecpc) {
882 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->aec->mult;
883 int current_delay = nSampSndCard - aecpc->aec->system_delay;
884 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000885
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000886 // Before we proceed with the delay estimate filtering we:
887 // 1) Compensate for the frame that will be read.
888 // 2) Compensate for drift resampling.
niklase@google.com470e71d2011-07-07 08:21:25 +0000889
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000890 // 1) Compensating for the frame(s) that will be read/processed.
891 current_delay += FRAME_LEN * aecpc->aec->mult;
niklase@google.com470e71d2011-07-07 08:21:25 +0000892
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000893 // 2) Account for resampling frame delay.
894 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
895 current_delay -= kResamplingDelay;
896 }
897
898 aecpc->filtDelay = WEBRTC_SPL_MAX(0, (short) (0.8 * aecpc->filtDelay +
899 0.2 * current_delay));
900
901 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
902 if (delay_difference > 224) {
903 if (aecpc->lastDelayDiff < 96) {
904 aecpc->timeForDelayChange = 0;
905 } else {
906 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000907 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000908 } else if (delay_difference < 96 && aecpc->knownDelay > 0) {
909 if (aecpc->lastDelayDiff > 224) {
910 aecpc->timeForDelayChange = 0;
911 } else {
912 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000913 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000914 } else {
915 aecpc->timeForDelayChange = 0;
916 }
917 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000918
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000919 if (aecpc->timeForDelayChange > 25) {
920 aecpc->knownDelay = WEBRTC_SPL_MAX((int) aecpc->filtDelay - 160, 0);
921 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000922
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000923 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000924}