blob: dc5c47979c7aea7ad7a727ed491939861bd97647 [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
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
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000207 // Sampling frequency multiplier (SWB is processed as 160 frame size).
208 aecpc->rate_factor = aecpc->splitSampFreq / 8000;
209
niklase@google.com470e71d2011-07-07 08:21:25 +0000210 // Default settings.
211 aecConfig.nlpMode = kAecNlpModerate;
212 aecConfig.skewMode = kAecFalse;
213 aecConfig.metricsMode = kAecFalse;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000214 aecConfig.delay_logging = kAecFalse;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215
216 if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
217 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
218 return -1;
219 }
220
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000221#ifdef WEBRTC_AEC_DEBUG_DUMP
222 if (WebRtc_InitBuffer(aecpc->far_pre_buf_s16) == -1) {
223 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
224 return -1;
225 }
226 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN); // Start overlap.
227#endif
228
niklase@google.com470e71d2011-07-07 08:21:25 +0000229 return 0;
230}
231
232// only buffer L band for farend
233WebRtc_Word32 WebRtcAec_BufferFarend(void *aecInst, const WebRtc_Word16 *farend,
234 WebRtc_Word16 nrOfSamples)
235{
236 aecpc_t *aecpc = aecInst;
237 WebRtc_Word32 retVal = 0;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000238 int newNrOfSamples = (int) nrOfSamples;
niklase@google.com470e71d2011-07-07 08:21:25 +0000239 short newFarend[MAX_RESAMP_LEN];
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000240 const int16_t* farend_ptr = farend;
241 float tmp_farend[MAX_RESAMP_LEN];
242 const float* farend_float = tmp_farend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000243 float skew;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000244 int i = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245
246 if (aecpc == NULL) {
247 return -1;
248 }
249
250 if (farend == NULL) {
251 aecpc->lastError = AEC_NULL_POINTER_ERROR;
252 return -1;
253 }
254
255 if (aecpc->initFlag != initCheck) {
256 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
257 return -1;
258 }
259
260 // number of samples == 160 for SWB input
261 if (nrOfSamples != 80 && nrOfSamples != 160) {
262 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
263 return -1;
264 }
265
266 skew = aecpc->skew;
267
niklase@google.com470e71d2011-07-07 08:21:25 +0000268 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
269 // Resample and get a new number of samples
bjornv@webrtc.org281b7982012-05-30 07:41:57 +0000270 WebRtcAec_ResampleLinear(aecpc->resampler, farend, nrOfSamples, skew,
271 newFarend, &newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000272 farend_ptr = (const int16_t*) newFarend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000273 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000274
275 aecpc->aec->system_delay += newNrOfSamples;
276
277#ifdef WEBRTC_AEC_DEBUG_DUMP
278 WebRtc_WriteBuffer(aecpc->far_pre_buf_s16, farend_ptr,
279 (size_t) newNrOfSamples);
280#endif
281 // Cast to float and write the time-domain data to |far_pre_buf|.
282 for (i = 0; i < newNrOfSamples; i++) {
283 tmp_farend[i] = (float) farend_ptr[i];
284 }
285 WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_float,
286 (size_t) newNrOfSamples);
287
288 // Transform to frequency domain if we have enough data.
289 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
290 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
291 WebRtc_ReadBuffer(aecpc->far_pre_buf, (void**) &farend_float, tmp_farend,
292 PART_LEN2);
293
294 WebRtcAec_BufferFarendPartition(aecpc->aec, farend_float);
295
296 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
297 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
298#ifdef WEBRTC_AEC_DEBUG_DUMP
299 WebRtc_ReadBuffer(aecpc->far_pre_buf_s16, (void**) &farend_ptr, newFarend,
300 PART_LEN2);
301 WebRtc_WriteBuffer(aecpc->aec->far_time_buf, &farend_ptr[PART_LEN], 1);
302 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN);
303#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000304 }
305
306 return retVal;
307}
308
309WebRtc_Word32 WebRtcAec_Process(void *aecInst, const WebRtc_Word16 *nearend,
310 const WebRtc_Word16 *nearendH, WebRtc_Word16 *out, WebRtc_Word16 *outH,
311 WebRtc_Word16 nrOfSamples, WebRtc_Word16 msInSndCardBuf, WebRtc_Word32 skew)
312{
313 aecpc_t *aecpc = aecInst;
314 WebRtc_Word32 retVal = 0;
315 short i;
niklase@google.com470e71d2011-07-07 08:21:25 +0000316 short nBlocks10ms;
317 short nFrames;
niklase@google.com470e71d2011-07-07 08:21:25 +0000318 // Limit resampling to doubling/halving of signal
319 const float minSkewEst = -0.5f;
320 const float maxSkewEst = 1.0f;
321
322 if (aecpc == NULL) {
323 return -1;
324 }
325
326 if (nearend == NULL) {
327 aecpc->lastError = AEC_NULL_POINTER_ERROR;
328 return -1;
329 }
330
331 if (out == NULL) {
332 aecpc->lastError = AEC_NULL_POINTER_ERROR;
333 return -1;
334 }
335
336 if (aecpc->initFlag != initCheck) {
337 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
338 return -1;
339 }
340
341 // number of samples == 160 for SWB input
342 if (nrOfSamples != 80 && nrOfSamples != 160) {
343 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
344 return -1;
345 }
346
347 // Check for valid pointers based on sampling rate
348 if (aecpc->sampFreq == 32000 && nearendH == NULL) {
349 aecpc->lastError = AEC_NULL_POINTER_ERROR;
350 return -1;
351 }
352
353 if (msInSndCardBuf < 0) {
354 msInSndCardBuf = 0;
355 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
356 retVal = -1;
357 }
358 else if (msInSndCardBuf > 500) {
359 msInSndCardBuf = 500;
360 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
361 retVal = -1;
362 }
andrew@webrtc.orga919d3a2011-11-27 23:40:58 +0000363 // TODO(andrew): we need to investigate if this +10 is really wanted.
niklase@google.com470e71d2011-07-07 08:21:25 +0000364 msInSndCardBuf += 10;
365 aecpc->msInSndCardBuf = msInSndCardBuf;
366
367 if (aecpc->skewMode == kAecTrue) {
368 if (aecpc->skewFrCtr < 25) {
369 aecpc->skewFrCtr++;
370 }
371 else {
372 retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
373 if (retVal == -1) {
374 aecpc->skew = 0;
375 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
376 }
377
378 aecpc->skew /= aecpc->sampFactor*nrOfSamples;
379
380 if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
381 aecpc->resample = kAecFalse;
382 }
383 else {
384 aecpc->resample = kAecTrue;
385 }
386
387 if (aecpc->skew < minSkewEst) {
388 aecpc->skew = minSkewEst;
389 }
390 else if (aecpc->skew > maxSkewEst) {
391 aecpc->skew = maxSkewEst;
392 }
393
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000394#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org7d8c5672012-06-01 02:41:14 +0000395 (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile);
niklase@google.com470e71d2011-07-07 08:21:25 +0000396#endif
397 }
398 }
399
400 nFrames = nrOfSamples / FRAME_LEN;
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000401 nBlocks10ms = nFrames / aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000402
403 if (aecpc->ECstartup) {
andrew@webrtc.org64235092011-08-19 21:22:08 +0000404 if (nearend != out) {
405 // Only needed if they don't already point to the same place.
406 memcpy(out, nearend, sizeof(short) * nrOfSamples);
407 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000408
409 // The AEC is in the start up mode
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000410 // AEC is disabled until the system delay is OK
niklase@google.com470e71d2011-07-07 08:21:25 +0000411
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000412 // Mechanism to ensure that the system delay is reasonably stable.
niklase@google.com470e71d2011-07-07 08:21:25 +0000413 if (aecpc->checkBuffSize) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000414 aecpc->checkBufSizeCtr++;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000415 // Before we fill up the far-end buffer we require the system delay
416 // to be stable (+/-8 ms) compared to the first value. This
417 // comparison is made during the following 6 consecutive 10 ms
418 // blocks. If it seems to be stable then we start to fill up the
419 // far-end buffer.
niklase@google.com470e71d2011-07-07 08:21:25 +0000420 if (aecpc->counter == 0) {
421 aecpc->firstVal = aecpc->msInSndCardBuf;
422 aecpc->sum = 0;
423 }
424
425 if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
426 WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
427 aecpc->sum += aecpc->msInSndCardBuf;
428 aecpc->counter++;
429 }
430 else {
431 aecpc->counter = 0;
432 }
433
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000434 if (aecpc->counter * nBlocks10ms >= 6) {
435 // The far-end buffer size is determined in partitions of
436 // PART_LEN samples. Use 75% of the average value of the system
437 // delay as buffer size to start with.
438 aecpc->bufSizeStart = WEBRTC_SPL_MIN((3 * aecpc->sum *
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000439 aecpc->rate_factor * 8) / (4 * aecpc->counter * PART_LEN),
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000440 kMaxBufSizeStart);
441 // Buffer size has now been determined.
niklase@google.com470e71d2011-07-07 08:21:25 +0000442 aecpc->checkBuffSize = 0;
443 }
444
445 if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000446 // For really bad systems, don't disable the echo canceller for
447 // more than 0.5 sec.
448 aecpc->bufSizeStart = WEBRTC_SPL_MIN((aecpc->msInSndCardBuf *
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000449 aecpc->rate_factor * 3) / 40, kMaxBufSizeStart);
niklase@google.com470e71d2011-07-07 08:21:25 +0000450 aecpc->checkBuffSize = 0;
451 }
452 }
453
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000454 // If |checkBuffSize| changed in the if-statement above.
niklase@google.com470e71d2011-07-07 08:21:25 +0000455 if (!aecpc->checkBuffSize) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000456 // The system delay is now reasonably stable (or has been unstable
457 // for too long). When the far-end buffer is filled with
458 // approximately the same amount of data as reported by the system
459 // we end the startup phase.
460 int overhead_elements = aecpc->aec->system_delay / PART_LEN -
461 aecpc->bufSizeStart;
462 if (overhead_elements == 0) {
463 // Enable the AEC
464 aecpc->ECstartup = 0;
465 } else if (overhead_elements > 0) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000466 // TODO(bjornv): Do we need a check on how much we actually
467 // moved the read pointer? It should always be possible to move
468 // the pointer |overhead_elements| since we have only added data
469 // to the buffer and no delay compensation nor AEC processing
470 // has been done.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000471 WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements);
niklase@google.com470e71d2011-07-07 08:21:25 +0000472
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000473 // Enable the AEC
niklase@google.com470e71d2011-07-07 08:21:25 +0000474 aecpc->ECstartup = 0;
475 }
476 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000477 } else {
478 // AEC is enabled.
niklase@google.com470e71d2011-07-07 08:21:25 +0000479
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000480 int out_elements = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000481
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000482 EstBufDelay(aecpc);
483
484 // Note that 1 frame is supported for NB and 2 frames for WB.
niklase@google.com470e71d2011-07-07 08:21:25 +0000485 for (i = 0; i < nFrames; i++) {
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000486 int16_t* out_ptr = NULL;
487 int16_t out_tmp[FRAME_LEN];
niklase@google.com470e71d2011-07-07 08:21:25 +0000488
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000489 // Call the AEC.
490 WebRtcAec_ProcessFrame(aecpc->aec,
491 &nearend[FRAME_LEN * i],
492 &nearendH[FRAME_LEN * i],
493 aecpc->knownDelay);
494 // TODO(bjornv): Re-structure such that we don't have to pass
495 // |aecpc->knownDelay| as input. Change name to something like
496 // |system_buffer_diff|.
niklase@google.com470e71d2011-07-07 08:21:25 +0000497
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000498 // Stuff the out buffer if we have less than a frame to output.
499 // This should only happen for the first frame.
500 out_elements = (int) WebRtc_available_read(aecpc->aec->outFrBuf);
501 if (out_elements < FRAME_LEN) {
502 WebRtc_MoveReadPtr(aecpc->aec->outFrBuf,
503 out_elements - FRAME_LEN);
504 if (aecpc->sampFreq == 32000) {
505 WebRtc_MoveReadPtr(aecpc->aec->outFrBufH,
506 out_elements - FRAME_LEN);
507 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000508 }
509
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000510 // Obtain an output frame.
511 WebRtc_ReadBuffer(aecpc->aec->outFrBuf, (void**) &out_ptr,
512 out_tmp, FRAME_LEN);
513 memcpy(&out[FRAME_LEN * i], out_ptr, sizeof(int16_t) * FRAME_LEN);
514 // For H band
515 if (aecpc->sampFreq == 32000) {
516 WebRtc_ReadBuffer(aecpc->aec->outFrBufH, (void**) &out_ptr,
517 out_tmp, FRAME_LEN);
518 memcpy(&outH[FRAME_LEN * i], out_ptr,
519 sizeof(int16_t) * FRAME_LEN);
niklase@google.com470e71d2011-07-07 08:21:25 +0000520 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000521 }
522 }
523
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000524#ifdef WEBRTC_AEC_DEBUG_DUMP
525 {
andrew@webrtc.org7d8c5672012-06-01 02:41:14 +0000526 int16_t far_buf_size_ms = (int16_t)(aecpc->aec->system_delay /
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000527 (sampMsNb * aecpc->rate_factor));
andrew@webrtc.org7d8c5672012-06-01 02:41:14 +0000528 (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile);
529 (void)fwrite(&aecpc->knownDelay, sizeof(aecpc->knownDelay), 1,
530 aecpc->delayFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000531 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000532#endif
533
534 return retVal;
535}
536
537WebRtc_Word32 WebRtcAec_set_config(void *aecInst, AecConfig config)
538{
539 aecpc_t *aecpc = aecInst;
540
541 if (aecpc == NULL) {
542 return -1;
543 }
544
545 if (aecpc->initFlag != initCheck) {
546 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
547 return -1;
548 }
549
550 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
551 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
552 return -1;
553 }
554 aecpc->skewMode = config.skewMode;
555
556 if (config.nlpMode != kAecNlpConservative && config.nlpMode !=
557 kAecNlpModerate && config.nlpMode != kAecNlpAggressive) {
558 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
559 return -1;
560 }
561 aecpc->nlpMode = config.nlpMode;
562 aecpc->aec->targetSupp = targetSupp[aecpc->nlpMode];
563 aecpc->aec->minOverDrive = minOverDrive[aecpc->nlpMode];
564
565 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
566 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
567 return -1;
568 }
569 aecpc->aec->metricsMode = config.metricsMode;
570 if (aecpc->aec->metricsMode == kAecTrue) {
571 WebRtcAec_InitMetrics(aecpc->aec);
572 }
573
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000574 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
575 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
576 return -1;
577 }
578 aecpc->aec->delay_logging_enabled = config.delay_logging;
579 if (aecpc->aec->delay_logging_enabled == kAecTrue) {
580 memset(aecpc->aec->delay_histogram, 0, sizeof(aecpc->aec->delay_histogram));
581 }
582
niklase@google.com470e71d2011-07-07 08:21:25 +0000583 return 0;
584}
585
586WebRtc_Word32 WebRtcAec_get_config(void *aecInst, AecConfig *config)
587{
588 aecpc_t *aecpc = aecInst;
589
590 if (aecpc == NULL) {
591 return -1;
592 }
593
594 if (config == NULL) {
595 aecpc->lastError = AEC_NULL_POINTER_ERROR;
596 return -1;
597 }
598
599 if (aecpc->initFlag != initCheck) {
600 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
601 return -1;
602 }
603
604 config->nlpMode = aecpc->nlpMode;
605 config->skewMode = aecpc->skewMode;
606 config->metricsMode = aecpc->aec->metricsMode;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000607 config->delay_logging = aecpc->aec->delay_logging_enabled;
niklase@google.com470e71d2011-07-07 08:21:25 +0000608
609 return 0;
610}
611
612WebRtc_Word32 WebRtcAec_get_echo_status(void *aecInst, WebRtc_Word16 *status)
613{
614 aecpc_t *aecpc = aecInst;
615
616 if (aecpc == NULL) {
617 return -1;
618 }
619
620 if (status == NULL) {
621 aecpc->lastError = AEC_NULL_POINTER_ERROR;
622 return -1;
623 }
624
625 if (aecpc->initFlag != initCheck) {
626 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
627 return -1;
628 }
629
630 *status = aecpc->aec->echoState;
631
632 return 0;
633}
634
635WebRtc_Word32 WebRtcAec_GetMetrics(void *aecInst, AecMetrics *metrics)
636{
637 const float upweight = 0.7f;
638 float dtmp;
639 short stmp;
640 aecpc_t *aecpc = aecInst;
641
642 if (aecpc == NULL) {
643 return -1;
644 }
645
646 if (metrics == NULL) {
647 aecpc->lastError = AEC_NULL_POINTER_ERROR;
648 return -1;
649 }
650
651 if (aecpc->initFlag != initCheck) {
652 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
653 return -1;
654 }
655
656 // ERL
657 metrics->erl.instant = (short) aecpc->aec->erl.instant;
658
659 if ((aecpc->aec->erl.himean > offsetLevel) && (aecpc->aec->erl.average > offsetLevel)) {
660 // Use a mix between regular average and upper part average
661 dtmp = upweight * aecpc->aec->erl.himean + (1 - upweight) * aecpc->aec->erl.average;
662 metrics->erl.average = (short) dtmp;
663 }
664 else {
665 metrics->erl.average = offsetLevel;
666 }
667
668 metrics->erl.max = (short) aecpc->aec->erl.max;
669
670 if (aecpc->aec->erl.min < (offsetLevel * (-1))) {
671 metrics->erl.min = (short) aecpc->aec->erl.min;
672 }
673 else {
674 metrics->erl.min = offsetLevel;
675 }
676
677 // ERLE
678 metrics->erle.instant = (short) aecpc->aec->erle.instant;
679
680 if ((aecpc->aec->erle.himean > offsetLevel) && (aecpc->aec->erle.average > offsetLevel)) {
681 // Use a mix between regular average and upper part average
682 dtmp = upweight * aecpc->aec->erle.himean + (1 - upweight) * aecpc->aec->erle.average;
683 metrics->erle.average = (short) dtmp;
684 }
685 else {
686 metrics->erle.average = offsetLevel;
687 }
688
689 metrics->erle.max = (short) aecpc->aec->erle.max;
690
691 if (aecpc->aec->erle.min < (offsetLevel * (-1))) {
692 metrics->erle.min = (short) aecpc->aec->erle.min;
693 } else {
694 metrics->erle.min = offsetLevel;
695 }
696
697 // RERL
698 if ((metrics->erl.average > offsetLevel) && (metrics->erle.average > offsetLevel)) {
699 stmp = metrics->erl.average + metrics->erle.average;
700 }
701 else {
702 stmp = offsetLevel;
703 }
704 metrics->rerl.average = stmp;
705
706 // No other statistics needed, but returned for completeness
707 metrics->rerl.instant = stmp;
708 metrics->rerl.max = stmp;
709 metrics->rerl.min = stmp;
710
711 // A_NLP
712 metrics->aNlp.instant = (short) aecpc->aec->aNlp.instant;
713
714 if ((aecpc->aec->aNlp.himean > offsetLevel) && (aecpc->aec->aNlp.average > offsetLevel)) {
715 // Use a mix between regular average and upper part average
716 dtmp = upweight * aecpc->aec->aNlp.himean + (1 - upweight) * aecpc->aec->aNlp.average;
717 metrics->aNlp.average = (short) dtmp;
718 }
719 else {
720 metrics->aNlp.average = offsetLevel;
721 }
722
723 metrics->aNlp.max = (short) aecpc->aec->aNlp.max;
724
725 if (aecpc->aec->aNlp.min < (offsetLevel * (-1))) {
726 metrics->aNlp.min = (short) aecpc->aec->aNlp.min;
727 }
728 else {
729 metrics->aNlp.min = offsetLevel;
730 }
731
732 return 0;
733}
734
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000735int WebRtcAec_GetDelayMetrics(void* handle, int* median, int* std) {
736 aecpc_t* self = handle;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000737
bjornv@webrtc.org61d07452012-05-11 07:51:44 +0000738 if (handle == NULL) {
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000739 return -1;
740 }
741 if (median == NULL) {
742 self->lastError = AEC_NULL_POINTER_ERROR;
743 return -1;
744 }
745 if (std == NULL) {
746 self->lastError = AEC_NULL_POINTER_ERROR;
747 return -1;
748 }
749 if (self->initFlag != initCheck) {
750 self->lastError = AEC_UNINITIALIZED_ERROR;
751 return -1;
752 }
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000753 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std) == -1) {
754 // Logging disabled.
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000755 self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR;
756 return -1;
757 }
758
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000759 return 0;
760}
761
niklase@google.com470e71d2011-07-07 08:21:25 +0000762WebRtc_Word32 WebRtcAec_get_error_code(void *aecInst)
763{
764 aecpc_t *aecpc = aecInst;
765
766 if (aecpc == NULL) {
767 return -1;
768 }
769
770 return aecpc->lastError;
771}
772
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000773static int EstBufDelay(aecpc_t* aecpc) {
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000774 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor;
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000775 int current_delay = nSampSndCard - aecpc->aec->system_delay;
776 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000777
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000778 // Before we proceed with the delay estimate filtering we:
779 // 1) Compensate for the frame that will be read.
780 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000781 // 3) Compensate for non-causality if needed, since the estimated delay can't
782 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000783
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000784 // 1) Compensating for the frame(s) that will be read/processed.
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000785 current_delay += FRAME_LEN * aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000786
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000787 // 2) Account for resampling frame delay.
788 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
789 current_delay -= kResamplingDelay;
790 }
791
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000792 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000793 if (current_delay < PART_LEN) {
794 current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
795 }
796
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000797 aecpc->filtDelay = WEBRTC_SPL_MAX(0, (short) (0.8 * aecpc->filtDelay +
798 0.2 * current_delay));
799
800 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
801 if (delay_difference > 224) {
802 if (aecpc->lastDelayDiff < 96) {
803 aecpc->timeForDelayChange = 0;
804 } else {
805 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000806 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000807 } else if (delay_difference < 96 && aecpc->knownDelay > 0) {
808 if (aecpc->lastDelayDiff > 224) {
809 aecpc->timeForDelayChange = 0;
810 } else {
811 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000812 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000813 } else {
814 aecpc->timeForDelayChange = 0;
815 }
816 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000817
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000818 if (aecpc->timeForDelayChange > 25) {
819 aecpc->knownDelay = WEBRTC_SPL_MAX((int) aecpc->filtDelay - 160, 0);
820 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000821
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000822 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000823}