blob: 7221966567a73c1b127093bef0d68ad092dd53a7 [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
andrew@webrtc.org1760a172013-09-25 23:17:38 +000030// Measured delays [ms]
31// Device Chrome GTP
32// MacBook Air 10
33// MacBook Retina 10 100
34// MacPro 30?
35//
36// Win7 Desktop 70 80?
37// Win7 T430s 110
38// Win8 T420s 70
39//
40// Daisy 50
41// Pixel (w/ preproc?) 240
42// Pixel (w/o preproc?) 110 110
43
44// The extended filter mode gives us the flexibility to ignore the system's
45// reported delays. We do this for platforms which we believe provide results
46// which are incompatible with the AEC's expectations. Based on measurements
47// (some provided above) we set a conservative (i.e. lower than measured)
48// fixed delay.
49//
50// WEBRTC_UNTRUSTED_DELAY will only have an impact when |extended_filter_mode|
51// is enabled. See the note along with |DelayCorrection| in
52// echo_cancellation_impl.h for more details on the mode.
53//
54// Justification:
55// Chromium/Mac: Here, the true latency is so low (~10-20 ms), that it plays
56// havoc with the AEC's buffering. To avoid this, we set a fixed delay of 20 ms
57// and then compensate by rewinding by 10 ms (in wideband) through
58// kDelayDiffOffsetSamples. This trick does not seem to work for larger rewind
59// values, but fortunately this is sufficient.
60//
61// Chromium/Linux(ChromeOS): The values we get on this platform don't correspond
62// well to reality. The variance doesn't match the AEC's buffer changes, and the
63// bulk values tend to be too low. However, the range across different hardware
64// appears to be too large to choose a single value.
65//
66// GTP/Linux(ChromeOS): TBD, but for the moment we will trust the values.
67#if defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_MAC)
68#define WEBRTC_UNTRUSTED_DELAY
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +000069#endif
andrew@webrtc.orgacb00502013-10-04 16:59:17 +000070
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +000071#if defined(WEBRTC_UNTRUSTED_DELAY) && defined(WEBRTC_MAC)
andrew@webrtc.orgacb00502013-10-04 16:59:17 +000072static const int kDelayDiffOffsetSamples = -160;
73#else
74// Not enabled for now.
75static const int kDelayDiffOffsetSamples = 0;
76#endif
andrew@webrtc.org1760a172013-09-25 23:17:38 +000077
78#if defined(WEBRTC_MAC)
79static const int kFixedDelayMs = 20;
andrew@webrtc.org1760a172013-09-25 23:17:38 +000080#else
andrew@webrtc.org1760a172013-09-25 23:17:38 +000081static const int kFixedDelayMs = 50;
andrew@webrtc.org1760a172013-09-25 23:17:38 +000082#endif
andrew@webrtc.orgc2e471d2013-10-15 02:11:21 +000083#if !defined(WEBRTC_UNTRUSTED_DELAY)
andrew@webrtc.org1760a172013-09-25 23:17:38 +000084static const int kMinTrustedDelayMs = 20;
andrew@webrtc.orgc2e471d2013-10-15 02:11:21 +000085#endif
andrew@webrtc.org1760a172013-09-25 23:17:38 +000086static const int kMaxTrustedDelayMs = 500;
87
niklase@google.com470e71d2011-07-07 08:21:25 +000088// Maximum length of resampled signal. Must be an integer multiple of frames
89// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
90// The factor of 2 handles wb, and the + 1 is as a safety margin
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000091// TODO(bjornv): Replace with kResamplerBufferSize
niklase@google.com470e71d2011-07-07 08:21:25 +000092#define MAX_RESAMP_LEN (5 * FRAME_LEN)
93
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000094static const int kMaxBufSizeStart = 62; // In partitions
andrew@webrtc.org13b2d462013-10-08 23:41:42 +000095static const int sampMsNb = 8; // samples per ms in nb
niklase@google.com470e71d2011-07-07 08:21:25 +000096static const int initCheck = 42;
97
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000098#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +000099int webrtc_aec_instance_count = 0;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +0000100#endif
101
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000102// Estimates delay to set the position of the far-end buffer read pointer
niklase@google.com470e71d2011-07-07 08:21:25 +0000103// (controlled by knownDelay)
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000104static void EstBufDelayNormal(aecpc_t* aecInst);
105static void EstBufDelayExtended(aecpc_t* aecInst);
106static int ProcessNormal(aecpc_t* self,
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000107 const float* near,
108 const float* near_high,
109 float* out,
110 float* out_high,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000111 int16_t num_samples,
112 int16_t reported_delay_ms,
113 int32_t skew);
114static void ProcessExtended(aecpc_t* self,
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000115 const float* near,
116 const float* near_high,
117 float* out,
118 float* out_high,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000119 int16_t num_samples,
120 int16_t reported_delay_ms,
121 int32_t skew);
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000123int32_t WebRtcAec_Create(void** aecInst) {
124 aecpc_t* aecpc;
125 if (aecInst == NULL) {
126 return -1;
127 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000128
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000129 aecpc = malloc(sizeof(aecpc_t));
130 *aecInst = aecpc;
131 if (aecpc == NULL) {
132 return -1;
133 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000134
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000135 if (WebRtcAec_CreateAec(&aecpc->aec) == -1) {
136 WebRtcAec_Free(aecpc);
137 aecpc = NULL;
138 return -1;
139 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000140
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000141 if (WebRtcAec_CreateResampler(&aecpc->resampler) == -1) {
142 WebRtcAec_Free(aecpc);
143 aecpc = NULL;
144 return -1;
145 }
146 // Create far-end pre-buffer. The buffer size has to be large enough for
147 // largest possible drift compensation (kResamplerBufferSize) + "almost" an
148 // FFT buffer (PART_LEN2 - 1).
149 aecpc->far_pre_buf =
150 WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize, sizeof(float));
151 if (!aecpc->far_pre_buf) {
152 WebRtcAec_Free(aecpc);
153 aecpc = NULL;
154 return -1;
155 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000156
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000157 aecpc->initFlag = 0;
158 aecpc->lastError = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000159
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000160#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000161 aecpc->far_pre_buf_s16 =
162 WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize, sizeof(int16_t));
163 if (!aecpc->far_pre_buf_s16) {
164 WebRtcAec_Free(aecpc);
165 aecpc = NULL;
166 return -1;
167 }
168 {
169 char filename[64];
170 sprintf(filename, "aec_buf%d.dat", webrtc_aec_instance_count);
171 aecpc->bufFile = fopen(filename, "wb");
172 sprintf(filename, "aec_skew%d.dat", webrtc_aec_instance_count);
173 aecpc->skewFile = fopen(filename, "wb");
174 sprintf(filename, "aec_delay%d.dat", webrtc_aec_instance_count);
175 aecpc->delayFile = fopen(filename, "wb");
176 webrtc_aec_instance_count++;
177 }
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000178#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000180 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181}
182
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000183int32_t WebRtcAec_Free(void* aecInst) {
184 aecpc_t* aecpc = aecInst;
niklase@google.com470e71d2011-07-07 08:21:25 +0000185
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000186 if (aecpc == NULL) {
187 return -1;
188 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000189
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000190 WebRtc_FreeBuffer(aecpc->far_pre_buf);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000191
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000192#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000193 WebRtc_FreeBuffer(aecpc->far_pre_buf_s16);
194 fclose(aecpc->bufFile);
195 fclose(aecpc->skewFile);
196 fclose(aecpc->delayFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000197#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000198
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000199 WebRtcAec_FreeAec(aecpc->aec);
200 WebRtcAec_FreeResampler(aecpc->resampler);
201 free(aecpc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000203 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000204}
205
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000206int32_t WebRtcAec_Init(void* aecInst, int32_t sampFreq, int32_t scSampFreq) {
207 aecpc_t* aecpc = aecInst;
208 AecConfig aecConfig;
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000210 if (sampFreq != 8000 && sampFreq != 16000 && sampFreq != 32000) {
211 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
212 return -1;
213 }
214 aecpc->sampFreq = sampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000216 if (scSampFreq < 1 || scSampFreq > 96000) {
217 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
218 return -1;
219 }
220 aecpc->scSampFreq = scSampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000221
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000222 // Initialize echo canceller core
223 if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) {
224 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
225 return -1;
226 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000228 if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) {
229 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
230 return -1;
231 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000233 if (WebRtc_InitBuffer(aecpc->far_pre_buf) == -1) {
234 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
235 return -1;
236 }
237 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap.
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000238
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000239 aecpc->initFlag = initCheck; // indicates that initialization has been done
niklase@google.com470e71d2011-07-07 08:21:25 +0000240
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000241 if (aecpc->sampFreq == 32000) {
242 aecpc->splitSampFreq = 16000;
243 } else {
244 aecpc->splitSampFreq = sampFreq;
245 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000246
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000247 aecpc->delayCtr = 0;
248 aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq;
249 // Sampling frequency multiplier (SWB is processed as 160 frame size).
250 aecpc->rate_factor = aecpc->splitSampFreq / 8000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000251
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000252 aecpc->sum = 0;
253 aecpc->counter = 0;
254 aecpc->checkBuffSize = 1;
255 aecpc->firstVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000256
bjornv@webrtc.org059488f2014-04-29 08:09:26 +0000257 aecpc->startup_phase = WebRtcAec_reported_delay_enabled(aecpc->aec);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000258 aecpc->bufSizeStart = 0;
259 aecpc->checkBufSizeCtr = 0;
260 aecpc->msInSndCardBuf = 0;
261 aecpc->filtDelay = -1; // -1 indicates an initialized state.
262 aecpc->timeForDelayChange = 0;
263 aecpc->knownDelay = 0;
264 aecpc->lastDelayDiff = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000265
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000266 aecpc->skewFrCtr = 0;
267 aecpc->resample = kAecFalse;
268 aecpc->highSkewCtr = 0;
269 aecpc->skew = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000270
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000271 aecpc->farend_started = 0;
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000272
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000273 // Default settings.
274 aecConfig.nlpMode = kAecNlpModerate;
275 aecConfig.skewMode = kAecFalse;
276 aecConfig.metricsMode = kAecFalse;
277 aecConfig.delay_logging = kAecFalse;
niklase@google.com470e71d2011-07-07 08:21:25 +0000278
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000279 if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
280 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
281 return -1;
282 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000283
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000284#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000285 if (WebRtc_InitBuffer(aecpc->far_pre_buf_s16) == -1) {
286 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
287 return -1;
288 }
289 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN); // Start overlap.
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000290#endif
291
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000292 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000293}
294
295// only buffer L band for farend
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000296int32_t WebRtcAec_BufferFarend(void* aecInst,
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000297 const float* farend,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000298 int16_t nrOfSamples) {
299 aecpc_t* aecpc = aecInst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000300 int newNrOfSamples = (int)nrOfSamples;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000301 float new_farend[MAX_RESAMP_LEN];
302 const float* farend_ptr = farend;
niklase@google.com470e71d2011-07-07 08:21:25 +0000303
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000304 if (farend == NULL) {
305 aecpc->lastError = AEC_NULL_POINTER_ERROR;
306 return -1;
307 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000308
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000309 if (aecpc->initFlag != initCheck) {
310 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
311 return -1;
312 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000313
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000314 // number of samples == 160 for SWB input
315 if (nrOfSamples != 80 && nrOfSamples != 160) {
316 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
317 return -1;
318 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000319
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000320 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
321 // Resample and get a new number of samples
322 WebRtcAec_ResampleLinear(aecpc->resampler,
323 farend,
324 nrOfSamples,
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000325 aecpc->skew,
326 new_farend,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000327 &newNrOfSamples);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000328 farend_ptr = new_farend;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000329 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000330
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000331 aecpc->farend_started = 1;
332 WebRtcAec_SetSystemDelay(aecpc->aec,
333 WebRtcAec_system_delay(aecpc->aec) + newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000334
335#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000336 WebRtc_WriteBuffer(
337 aecpc->far_pre_buf_s16, farend_ptr, (size_t)newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000338#endif
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000339 // Write the time-domain data to |far_pre_buf|.
340 WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_ptr, (size_t)newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000341
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000342 // Transform to frequency domain if we have enough data.
343 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
344 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000345 {
346 float* ptmp;
347 float tmp[PART_LEN2];
348 WebRtc_ReadBuffer(aecpc->far_pre_buf, (void**)&ptmp, tmp, PART_LEN2);
349 WebRtcAec_BufferFarendPartition(aecpc->aec, ptmp);
350 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000351
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000352 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
353 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000354#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000355 WebRtc_ReadBuffer(
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000356 aecpc->far_pre_buf_s16, (void**)&farend_ptr, new_farend, PART_LEN2);
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000357 WebRtc_WriteBuffer(
358 WebRtcAec_far_time_buf(aecpc->aec), &farend_ptr[PART_LEN], 1);
359 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000360#endif
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000361 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000362
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000363 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000364}
365
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000366int32_t WebRtcAec_Process(void* aecInst,
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000367 const float* nearend,
368 const float* nearendH,
369 float* out,
370 float* outH,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000371 int16_t nrOfSamples,
372 int16_t msInSndCardBuf,
373 int32_t skew) {
374 aecpc_t* aecpc = aecInst;
375 int32_t retVal = 0;
376 if (nearend == NULL) {
377 aecpc->lastError = AEC_NULL_POINTER_ERROR;
378 return -1;
379 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000380
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000381 if (out == NULL) {
382 aecpc->lastError = AEC_NULL_POINTER_ERROR;
383 return -1;
384 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000385
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000386 if (aecpc->initFlag != initCheck) {
387 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
388 return -1;
389 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000390
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000391 // number of samples == 160 for SWB input
392 if (nrOfSamples != 80 && nrOfSamples != 160) {
393 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
394 return -1;
395 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000396
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000397 // Check for valid pointers based on sampling rate
398 if (aecpc->sampFreq == 32000 && nearendH == NULL) {
399 aecpc->lastError = AEC_NULL_POINTER_ERROR;
400 return -1;
401 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000402
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000403 if (msInSndCardBuf < 0) {
404 msInSndCardBuf = 0;
405 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
406 retVal = -1;
407 } else if (msInSndCardBuf > kMaxTrustedDelayMs) {
408 // The clamping is now done in ProcessExtended/Normal().
409 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
410 retVal = -1;
411 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000412
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000413 // This returns the value of aec->extended_filter_enabled.
414 if (WebRtcAec_delay_correction_enabled(aecpc->aec)) {
415 ProcessExtended(
416 aecpc, nearend, nearendH, out, outH, nrOfSamples, msInSndCardBuf, skew);
417 } else {
418 if (ProcessNormal(aecpc,
419 nearend,
420 nearendH,
421 out,
422 outH,
423 nrOfSamples,
424 msInSndCardBuf,
425 skew) != 0) {
426 retVal = -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000427 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000428 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000429
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000430#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000431 {
432 int16_t far_buf_size_ms = (int16_t)(WebRtcAec_system_delay(aecpc->aec) /
433 (sampMsNb * aecpc->rate_factor));
434 (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile);
435 (void)fwrite(
436 &aecpc->knownDelay, sizeof(aecpc->knownDelay), 1, aecpc->delayFile);
437 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000438#endif
439
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000440 return retVal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000441}
442
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000443int WebRtcAec_set_config(void* handle, AecConfig config) {
444 aecpc_t* self = (aecpc_t*)handle;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000445 if (self->initFlag != initCheck) {
446 self->lastError = AEC_UNINITIALIZED_ERROR;
447 return -1;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000448 }
449
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000450 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
451 self->lastError = AEC_BAD_PARAMETER_ERROR;
452 return -1;
453 }
454 self->skewMode = config.skewMode;
455
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000456 if (config.nlpMode != kAecNlpConservative &&
457 config.nlpMode != kAecNlpModerate &&
458 config.nlpMode != kAecNlpAggressive) {
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000459 self->lastError = AEC_BAD_PARAMETER_ERROR;
460 return -1;
461 }
462
463 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
464 self->lastError = AEC_BAD_PARAMETER_ERROR;
465 return -1;
466 }
467
468 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
469 self->lastError = AEC_BAD_PARAMETER_ERROR;
470 return -1;
471 }
472
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000473 WebRtcAec_SetConfigCore(
474 self->aec, config.nlpMode, config.metricsMode, config.delay_logging);
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000475 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000476}
477
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000478int WebRtcAec_get_echo_status(void* handle, int* status) {
479 aecpc_t* self = (aecpc_t*)handle;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000480 if (status == NULL) {
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000481 self->lastError = AEC_NULL_POINTER_ERROR;
482 return -1;
483 }
484 if (self->initFlag != initCheck) {
485 self->lastError = AEC_UNINITIALIZED_ERROR;
486 return -1;
487 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000488
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000489 *status = WebRtcAec_echo_state(self->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000490
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000491 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000492}
493
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000494int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) {
495 const float kUpWeight = 0.7f;
496 float dtmp;
497 int stmp;
498 aecpc_t* self = (aecpc_t*)handle;
bjornv@webrtc.orgcea70f42013-02-19 21:03:10 +0000499 Stats erl;
500 Stats erle;
501 Stats a_nlp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000502
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000503 if (handle == NULL) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000504 return -1;
505 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000506 if (metrics == NULL) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000507 self->lastError = AEC_NULL_POINTER_ERROR;
508 return -1;
509 }
510 if (self->initFlag != initCheck) {
511 self->lastError = AEC_UNINITIALIZED_ERROR;
512 return -1;
513 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000514
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000515 WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000516
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000517 // ERL
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000518 metrics->erl.instant = (int)erl.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000519
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000520 if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000521 // Use a mix between regular average and upper part average.
522 dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000523 metrics->erl.average = (int)dtmp;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000524 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000525 metrics->erl.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000526 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000527
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000528 metrics->erl.max = (int)erl.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000529
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000530 if (erl.min < (kOffsetLevel * (-1))) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000531 metrics->erl.min = (int)erl.min;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000532 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000533 metrics->erl.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000534 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000535
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000536 // ERLE
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000537 metrics->erle.instant = (int)erle.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000538
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000539 if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000540 // Use a mix between regular average and upper part average.
541 dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000542 metrics->erle.average = (int)dtmp;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000543 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000544 metrics->erle.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000545 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000546
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000547 metrics->erle.max = (int)erle.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000548
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000549 if (erle.min < (kOffsetLevel * (-1))) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000550 metrics->erle.min = (int)erle.min;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000551 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000552 metrics->erle.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000553 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000554
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000555 // RERL
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000556 if ((metrics->erl.average > kOffsetLevel) &&
557 (metrics->erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000558 stmp = metrics->erl.average + metrics->erle.average;
559 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000560 stmp = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000561 }
562 metrics->rerl.average = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000563
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000564 // No other statistics needed, but returned for completeness.
565 metrics->rerl.instant = stmp;
566 metrics->rerl.max = stmp;
567 metrics->rerl.min = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000568
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000569 // A_NLP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000570 metrics->aNlp.instant = (int)a_nlp.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000571
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000572 if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000573 // Use a mix between regular average and upper part average.
574 dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000575 metrics->aNlp.average = (int)dtmp;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000576 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000577 metrics->aNlp.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000578 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000579
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000580 metrics->aNlp.max = (int)a_nlp.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000581
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000582 if (a_nlp.min < (kOffsetLevel * (-1))) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000583 metrics->aNlp.min = (int)a_nlp.min;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000584 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000585 metrics->aNlp.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000586 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000587
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000588 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000589}
590
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000591int WebRtcAec_GetDelayMetrics(void* handle, int* median, int* std) {
592 aecpc_t* self = handle;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000593 if (median == NULL) {
594 self->lastError = AEC_NULL_POINTER_ERROR;
595 return -1;
596 }
597 if (std == NULL) {
598 self->lastError = AEC_NULL_POINTER_ERROR;
599 return -1;
600 }
601 if (self->initFlag != initCheck) {
602 self->lastError = AEC_UNINITIALIZED_ERROR;
603 return -1;
604 }
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000605 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std) == -1) {
606 // Logging disabled.
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000607 self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR;
608 return -1;
609 }
610
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000611 return 0;
612}
613
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000614int32_t WebRtcAec_get_error_code(void* aecInst) {
615 aecpc_t* aecpc = aecInst;
616 return aecpc->lastError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000617}
618
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000619AecCore* WebRtcAec_aec_core(void* handle) {
620 if (!handle) {
621 return NULL;
622 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000623 return ((aecpc_t*)handle)->aec;
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000624}
625
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000626static int ProcessNormal(aecpc_t* aecpc,
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000627 const float* nearend,
628 const float* nearendH,
629 float* out,
630 float* outH,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000631 int16_t nrOfSamples,
632 int16_t msInSndCardBuf,
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000633 int32_t skew) {
634 int retVal = 0;
635 short i;
636 short nBlocks10ms;
637 short nFrames;
638 // Limit resampling to doubling/halving of signal
639 const float minSkewEst = -0.5f;
640 const float maxSkewEst = 1.0f;
641
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000642 msInSndCardBuf =
643 msInSndCardBuf > kMaxTrustedDelayMs ? kMaxTrustedDelayMs : msInSndCardBuf;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000644 // TODO(andrew): we need to investigate if this +10 is really wanted.
645 msInSndCardBuf += 10;
646 aecpc->msInSndCardBuf = msInSndCardBuf;
647
648 if (aecpc->skewMode == kAecTrue) {
649 if (aecpc->skewFrCtr < 25) {
650 aecpc->skewFrCtr++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000651 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000652 retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
653 if (retVal == -1) {
654 aecpc->skew = 0;
655 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
656 }
657
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000658 aecpc->skew /= aecpc->sampFactor * nrOfSamples;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000659
660 if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
661 aecpc->resample = kAecFalse;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000662 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000663 aecpc->resample = kAecTrue;
664 }
665
666 if (aecpc->skew < minSkewEst) {
667 aecpc->skew = minSkewEst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000668 } else if (aecpc->skew > maxSkewEst) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000669 aecpc->skew = maxSkewEst;
670 }
671
672#ifdef WEBRTC_AEC_DEBUG_DUMP
673 (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile);
674#endif
675 }
676 }
677
678 nFrames = nrOfSamples / FRAME_LEN;
679 nBlocks10ms = nFrames / aecpc->rate_factor;
680
681 if (aecpc->startup_phase) {
682 // Only needed if they don't already point to the same place.
683 if (nearend != out) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000684 memcpy(out, nearend, sizeof(*out) * nrOfSamples);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000685 }
686 if (nearendH != outH) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000687 memcpy(outH, nearendH, sizeof(*outH) * nrOfSamples);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000688 }
689
690 // The AEC is in the start up mode
691 // AEC is disabled until the system delay is OK
692
693 // Mechanism to ensure that the system delay is reasonably stable.
694 if (aecpc->checkBuffSize) {
695 aecpc->checkBufSizeCtr++;
696 // Before we fill up the far-end buffer we require the system delay
697 // to be stable (+/-8 ms) compared to the first value. This
698 // comparison is made during the following 6 consecutive 10 ms
699 // blocks. If it seems to be stable then we start to fill up the
700 // far-end buffer.
701 if (aecpc->counter == 0) {
702 aecpc->firstVal = aecpc->msInSndCardBuf;
703 aecpc->sum = 0;
704 }
705
706 if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000707 WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000708 aecpc->sum += aecpc->msInSndCardBuf;
709 aecpc->counter++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000710 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000711 aecpc->counter = 0;
712 }
713
714 if (aecpc->counter * nBlocks10ms >= 6) {
715 // The far-end buffer size is determined in partitions of
716 // PART_LEN samples. Use 75% of the average value of the system
717 // delay as buffer size to start with.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000718 aecpc->bufSizeStart =
719 WEBRTC_SPL_MIN((3 * aecpc->sum * aecpc->rate_factor * 8) /
720 (4 * aecpc->counter * PART_LEN),
721 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000722 // Buffer size has now been determined.
723 aecpc->checkBuffSize = 0;
724 }
725
726 if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
727 // For really bad systems, don't disable the echo canceller for
728 // more than 0.5 sec.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000729 aecpc->bufSizeStart = WEBRTC_SPL_MIN(
730 (aecpc->msInSndCardBuf * aecpc->rate_factor * 3) / 40,
731 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000732 aecpc->checkBuffSize = 0;
733 }
734 }
735
736 // If |checkBuffSize| changed in the if-statement above.
737 if (!aecpc->checkBuffSize) {
738 // The system delay is now reasonably stable (or has been unstable
739 // for too long). When the far-end buffer is filled with
740 // approximately the same amount of data as reported by the system
741 // we end the startup phase.
742 int overhead_elements =
743 WebRtcAec_system_delay(aecpc->aec) / PART_LEN - aecpc->bufSizeStart;
744 if (overhead_elements == 0) {
745 // Enable the AEC
746 aecpc->startup_phase = 0;
747 } else if (overhead_elements > 0) {
748 // TODO(bjornv): Do we need a check on how much we actually
749 // moved the read pointer? It should always be possible to move
750 // the pointer |overhead_elements| since we have only added data
751 // to the buffer and no delay compensation nor AEC processing
752 // has been done.
753 WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements);
754
755 // Enable the AEC
756 aecpc->startup_phase = 0;
757 }
758 }
759 } else {
760 // AEC is enabled.
bjornv@webrtc.orge9d37602014-04-23 13:20:07 +0000761 if (WebRtcAec_reported_delay_enabled(aecpc->aec)) {
762 EstBufDelayNormal(aecpc);
763 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000764
765 // Note that 1 frame is supported for NB and 2 frames for WB.
766 for (i = 0; i < nFrames; i++) {
767 // Call the AEC.
768 WebRtcAec_ProcessFrame(aecpc->aec,
769 &nearend[FRAME_LEN * i],
770 &nearendH[FRAME_LEN * i],
771 aecpc->knownDelay,
772 &out[FRAME_LEN * i],
773 &outH[FRAME_LEN * i]);
774 // TODO(bjornv): Re-structure such that we don't have to pass
775 // |aecpc->knownDelay| as input. Change name to something like
776 // |system_buffer_diff|.
777 }
778 }
779
780 return retVal;
781}
782
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000783static void ProcessExtended(aecpc_t* self,
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000784 const float* near,
785 const float* near_high,
786 float* out,
787 float* out_high,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000788 int16_t num_samples,
789 int16_t reported_delay_ms,
790 int32_t skew) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000791 int i;
792 const int num_frames = num_samples / FRAME_LEN;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000793 const int delay_diff_offset = kDelayDiffOffsetSamples;
bjornv@webrtc.org820f8e92014-08-11 15:39:00 +0000794#if defined(WEBRTC_UNTRUSTED_DELAY)
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000795 reported_delay_ms = kFixedDelayMs;
796#else
797 // This is the usual mode where we trust the reported system delay values.
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000798 // Due to the longer filter, we no longer add 10 ms to the reported delay
799 // to reduce chance of non-causality. Instead we apply a minimum here to avoid
800 // issues with the read pointer jumping around needlessly.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000801 reported_delay_ms = reported_delay_ms < kMinTrustedDelayMs
802 ? kMinTrustedDelayMs
803 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000804 // If the reported delay appears to be bogus, we attempt to recover by using
805 // the measured fixed delay values. We use >= here because higher layers
806 // may already clamp to this maximum value, and we would otherwise not
807 // detect it here.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000808 reported_delay_ms = reported_delay_ms >= kMaxTrustedDelayMs
809 ? kFixedDelayMs
810 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000811#endif
812 self->msInSndCardBuf = reported_delay_ms;
813
814 if (!self->farend_started) {
815 // Only needed if they don't already point to the same place.
816 if (near != out) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000817 memcpy(out, near, sizeof(*out) * num_samples);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000818 }
819 if (near_high != out_high) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000820 memcpy(out_high, near_high, sizeof(*out_high) * num_samples);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000821 }
822 return;
823 }
824 if (self->startup_phase) {
825 // In the extended mode, there isn't a startup "phase", just a special
826 // action on the first frame. In the trusted delay case, we'll take the
827 // current reported delay, unless it's less then our conservative
828 // measurement.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000829 int startup_size_ms =
830 reported_delay_ms < kFixedDelayMs ? kFixedDelayMs : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000831 int overhead_elements = (WebRtcAec_system_delay(self->aec) -
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000832 startup_size_ms / 2 * self->rate_factor * 8) /
833 PART_LEN;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000834 WebRtcAec_MoveFarReadPtr(self->aec, overhead_elements);
835 self->startup_phase = 0;
836 }
837
bjornv@webrtc.orge9d37602014-04-23 13:20:07 +0000838 if (WebRtcAec_reported_delay_enabled(self->aec)) {
839 EstBufDelayExtended(self);
840 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000841
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000842 {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000843 // |delay_diff_offset| gives us the option to manually rewind the delay on
844 // very low delay platforms which can't be expressed purely through
845 // |reported_delay_ms|.
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000846 const int adjusted_known_delay =
847 WEBRTC_SPL_MAX(0, self->knownDelay + delay_diff_offset);
848
849 for (i = 0; i < num_frames; ++i) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000850 WebRtcAec_ProcessFrame(self->aec,
851 &near[FRAME_LEN * i],
852 &near_high[FRAME_LEN * i],
853 adjusted_known_delay,
854 &out[FRAME_LEN * i],
855 &out_high[FRAME_LEN * i]);
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000856 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000857 }
858}
859
860static void EstBufDelayNormal(aecpc_t* aecpc) {
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000861 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor;
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000862 int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->aec);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000863 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000864
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000865 // Before we proceed with the delay estimate filtering we:
866 // 1) Compensate for the frame that will be read.
867 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000868 // 3) Compensate for non-causality if needed, since the estimated delay can't
869 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000870
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000871 // 1) Compensating for the frame(s) that will be read/processed.
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000872 current_delay += FRAME_LEN * aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000873
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000874 // 2) Account for resampling frame delay.
875 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
876 current_delay -= kResamplingDelay;
877 }
878
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000879 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000880 if (current_delay < PART_LEN) {
881 current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
882 }
883
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000884 // We use -1 to signal an initialized state in the "extended" implementation;
885 // compensate for that.
886 aecpc->filtDelay = aecpc->filtDelay < 0 ? 0 : aecpc->filtDelay;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000887 aecpc->filtDelay =
888 WEBRTC_SPL_MAX(0, (short)(0.8 * aecpc->filtDelay + 0.2 * current_delay));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000889
890 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
891 if (delay_difference > 224) {
892 if (aecpc->lastDelayDiff < 96) {
893 aecpc->timeForDelayChange = 0;
894 } else {
895 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000896 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000897 } else if (delay_difference < 96 && aecpc->knownDelay > 0) {
898 if (aecpc->lastDelayDiff > 224) {
899 aecpc->timeForDelayChange = 0;
900 } else {
901 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000902 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000903 } else {
904 aecpc->timeForDelayChange = 0;
905 }
906 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000907
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000908 if (aecpc->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000909 aecpc->knownDelay = WEBRTC_SPL_MAX((int)aecpc->filtDelay - 160, 0);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000910 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000911}
niklase@google.com470e71d2011-07-07 08:21:25 +0000912
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000913static void EstBufDelayExtended(aecpc_t* self) {
914 int reported_delay = self->msInSndCardBuf * sampMsNb * self->rate_factor;
915 int current_delay = reported_delay - WebRtcAec_system_delay(self->aec);
916 int delay_difference = 0;
917
918 // Before we proceed with the delay estimate filtering we:
919 // 1) Compensate for the frame that will be read.
920 // 2) Compensate for drift resampling.
921 // 3) Compensate for non-causality if needed, since the estimated delay can't
922 // be negative.
923
924 // 1) Compensating for the frame(s) that will be read/processed.
925 current_delay += FRAME_LEN * self->rate_factor;
926
927 // 2) Account for resampling frame delay.
928 if (self->skewMode == kAecTrue && self->resample == kAecTrue) {
929 current_delay -= kResamplingDelay;
930 }
931
932 // 3) Compensate for non-causality, if needed, by flushing two blocks.
933 if (current_delay < PART_LEN) {
934 current_delay += WebRtcAec_MoveFarReadPtr(self->aec, 2) * PART_LEN;
935 }
936
937 if (self->filtDelay == -1) {
938 self->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay);
939 } else {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000940 self->filtDelay = WEBRTC_SPL_MAX(
941 0, (short)(0.95 * self->filtDelay + 0.05 * current_delay));
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000942 }
943
944 delay_difference = self->filtDelay - self->knownDelay;
945 if (delay_difference > 384) {
946 if (self->lastDelayDiff < 128) {
947 self->timeForDelayChange = 0;
948 } else {
949 self->timeForDelayChange++;
950 }
951 } else if (delay_difference < 128 && self->knownDelay > 0) {
952 if (self->lastDelayDiff > 384) {
953 self->timeForDelayChange = 0;
954 } else {
955 self->timeForDelayChange++;
956 }
957 } else {
958 self->timeForDelayChange = 0;
959 }
960 self->lastDelayDiff = delay_difference;
961
962 if (self->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000963 self->knownDelay = WEBRTC_SPL_MAX((int)self->filtDelay - 256, 0);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000964 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000965}