blob: cf95e43b2f39ddc129f60afb97af49adcff45e46 [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
andrew@webrtc.orgacb00502013-10-04 16:59:17 +000069
70#if defined(WEBRTC_MAC)
71static const int kDelayDiffOffsetSamples = -160;
72#else
73// Not enabled for now.
74static const int kDelayDiffOffsetSamples = 0;
75#endif
andrew@webrtc.org1760a172013-09-25 23:17:38 +000076#endif
77
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
83static const int kMinTrustedDelayMs = 20;
84static const int kMaxTrustedDelayMs = 500;
85
niklase@google.com470e71d2011-07-07 08:21:25 +000086// Maximum length of resampled signal. Must be an integer multiple of frames
87// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
88// The factor of 2 handles wb, and the + 1 is as a safety margin
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000089// TODO(bjornv): Replace with kResamplerBufferSize
niklase@google.com470e71d2011-07-07 08:21:25 +000090#define MAX_RESAMP_LEN (5 * FRAME_LEN)
91
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +000092static const int kMaxBufSizeStart = 62; // In partitions
andrew@webrtc.org13b2d462013-10-08 23:41:42 +000093static const int sampMsNb = 8; // samples per ms in nb
niklase@google.com470e71d2011-07-07 08:21:25 +000094static const int initCheck = 42;
95
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000096#ifdef WEBRTC_AEC_DEBUG_DUMP
bjornv@webrtc.org7267ffd2013-02-14 17:56:23 +000097int webrtc_aec_instance_count = 0;
andrew@webrtc.org1e39bc82011-11-27 23:46:23 +000098#endif
99
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000100// Estimates delay to set the position of the far-end buffer read pointer
niklase@google.com470e71d2011-07-07 08:21:25 +0000101// (controlled by knownDelay)
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000102static void EstBufDelayNormal(aecpc_t* aecInst);
103static void EstBufDelayExtended(aecpc_t* aecInst);
104static int ProcessNormal(aecpc_t* self,
105 const int16_t* near,
106 const int16_t* near_high,
107 int16_t* out,
108 int16_t* out_high,
109 int16_t num_samples,
110 int16_t reported_delay_ms,
111 int32_t skew);
112static void ProcessExtended(aecpc_t* self,
113 const int16_t* near,
114 const int16_t* near_high,
115 int16_t* out,
116 int16_t* out_high,
117 int16_t num_samples,
118 int16_t reported_delay_ms,
119 int32_t skew);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000121int32_t WebRtcAec_Create(void** aecInst) {
122 aecpc_t* aecpc;
123 if (aecInst == NULL) {
124 return -1;
125 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000127 aecpc = malloc(sizeof(aecpc_t));
128 *aecInst = aecpc;
129 if (aecpc == NULL) {
130 return -1;
131 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000133 if (WebRtcAec_CreateAec(&aecpc->aec) == -1) {
134 WebRtcAec_Free(aecpc);
135 aecpc = NULL;
136 return -1;
137 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000139 if (WebRtcAec_CreateResampler(&aecpc->resampler) == -1) {
140 WebRtcAec_Free(aecpc);
141 aecpc = NULL;
142 return -1;
143 }
144 // Create far-end pre-buffer. The buffer size has to be large enough for
145 // largest possible drift compensation (kResamplerBufferSize) + "almost" an
146 // FFT buffer (PART_LEN2 - 1).
147 aecpc->far_pre_buf =
148 WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize, sizeof(float));
149 if (!aecpc->far_pre_buf) {
150 WebRtcAec_Free(aecpc);
151 aecpc = NULL;
152 return -1;
153 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000155 aecpc->initFlag = 0;
156 aecpc->lastError = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000158#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000159 aecpc->far_pre_buf_s16 =
160 WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize, sizeof(int16_t));
161 if (!aecpc->far_pre_buf_s16) {
162 WebRtcAec_Free(aecpc);
163 aecpc = NULL;
164 return -1;
165 }
166 {
167 char filename[64];
168 sprintf(filename, "aec_buf%d.dat", webrtc_aec_instance_count);
169 aecpc->bufFile = fopen(filename, "wb");
170 sprintf(filename, "aec_skew%d.dat", webrtc_aec_instance_count);
171 aecpc->skewFile = fopen(filename, "wb");
172 sprintf(filename, "aec_delay%d.dat", webrtc_aec_instance_count);
173 aecpc->delayFile = fopen(filename, "wb");
174 webrtc_aec_instance_count++;
175 }
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000176#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000178 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000179}
180
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000181int32_t WebRtcAec_Free(void* aecInst) {
182 aecpc_t* aecpc = aecInst;
niklase@google.com470e71d2011-07-07 08:21:25 +0000183
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000184 if (aecpc == NULL) {
185 return -1;
186 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000187
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000188 WebRtc_FreeBuffer(aecpc->far_pre_buf);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000189
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000190#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000191 WebRtc_FreeBuffer(aecpc->far_pre_buf_s16);
192 fclose(aecpc->bufFile);
193 fclose(aecpc->skewFile);
194 fclose(aecpc->delayFile);
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000195#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000196
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000197 WebRtcAec_FreeAec(aecpc->aec);
198 WebRtcAec_FreeResampler(aecpc->resampler);
199 free(aecpc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000200
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000201 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000202}
203
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000204int32_t WebRtcAec_Init(void* aecInst, int32_t sampFreq, int32_t scSampFreq) {
205 aecpc_t* aecpc = aecInst;
206 AecConfig aecConfig;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000208 if (sampFreq != 8000 && sampFreq != 16000 && sampFreq != 32000) {
209 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
210 return -1;
211 }
212 aecpc->sampFreq = sampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000214 if (scSampFreq < 1 || scSampFreq > 96000) {
215 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
216 return -1;
217 }
218 aecpc->scSampFreq = scSampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000220 // Initialize echo canceller core
221 if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) {
222 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
223 return -1;
224 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000225
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000226 if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) {
227 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
228 return -1;
229 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000230
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000231 if (WebRtc_InitBuffer(aecpc->far_pre_buf) == -1) {
232 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
233 return -1;
234 }
235 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap.
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000236
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000237 aecpc->initFlag = initCheck; // indicates that initialization has been done
niklase@google.com470e71d2011-07-07 08:21:25 +0000238
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000239 if (aecpc->sampFreq == 32000) {
240 aecpc->splitSampFreq = 16000;
241 } else {
242 aecpc->splitSampFreq = sampFreq;
243 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000244
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000245 aecpc->delayCtr = 0;
246 aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq;
247 // Sampling frequency multiplier (SWB is processed as 160 frame size).
248 aecpc->rate_factor = aecpc->splitSampFreq / 8000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000249
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000250 aecpc->sum = 0;
251 aecpc->counter = 0;
252 aecpc->checkBuffSize = 1;
253 aecpc->firstVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000254
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000255 aecpc->startup_phase = 1;
256 aecpc->bufSizeStart = 0;
257 aecpc->checkBufSizeCtr = 0;
258 aecpc->msInSndCardBuf = 0;
259 aecpc->filtDelay = -1; // -1 indicates an initialized state.
260 aecpc->timeForDelayChange = 0;
261 aecpc->knownDelay = 0;
262 aecpc->lastDelayDiff = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000263
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000264 aecpc->skewFrCtr = 0;
265 aecpc->resample = kAecFalse;
266 aecpc->highSkewCtr = 0;
267 aecpc->skew = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000268
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000269 aecpc->farend_started = 0;
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000270
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000271 // Default settings.
272 aecConfig.nlpMode = kAecNlpModerate;
273 aecConfig.skewMode = kAecFalse;
274 aecConfig.metricsMode = kAecFalse;
275 aecConfig.delay_logging = kAecFalse;
niklase@google.com470e71d2011-07-07 08:21:25 +0000276
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000277 if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
278 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
279 return -1;
280 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000281
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000282#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000283 if (WebRtc_InitBuffer(aecpc->far_pre_buf_s16) == -1) {
284 aecpc->lastError = AEC_UNSPECIFIED_ERROR;
285 return -1;
286 }
287 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN); // Start overlap.
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000288#endif
289
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000290 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000291}
292
293// only buffer L band for farend
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000294int32_t WebRtcAec_BufferFarend(void* aecInst,
295 const int16_t* farend,
296 int16_t nrOfSamples) {
297 aecpc_t* aecpc = aecInst;
298 int32_t retVal = 0;
299 int newNrOfSamples = (int)nrOfSamples;
300 short newFarend[MAX_RESAMP_LEN];
301 const int16_t* farend_ptr = farend;
302 float tmp_farend[MAX_RESAMP_LEN];
303 const float* farend_float = tmp_farend;
304 float skew;
305 int i = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000306
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000307 if (farend == NULL) {
308 aecpc->lastError = AEC_NULL_POINTER_ERROR;
309 return -1;
310 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000311
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000312 if (aecpc->initFlag != initCheck) {
313 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
314 return -1;
315 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000316
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000317 // number of samples == 160 for SWB input
318 if (nrOfSamples != 80 && nrOfSamples != 160) {
319 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
320 return -1;
321 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000322
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000323 skew = aecpc->skew;
niklase@google.com470e71d2011-07-07 08:21:25 +0000324
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000325 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
326 // Resample and get a new number of samples
327 WebRtcAec_ResampleLinear(aecpc->resampler,
328 farend,
329 nrOfSamples,
330 skew,
331 newFarend,
332 &newNrOfSamples);
333 farend_ptr = (const int16_t*)newFarend;
334 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000335
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000336 aecpc->farend_started = 1;
337 WebRtcAec_SetSystemDelay(aecpc->aec,
338 WebRtcAec_system_delay(aecpc->aec) + newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000339
340#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000341 WebRtc_WriteBuffer(
342 aecpc->far_pre_buf_s16, farend_ptr, (size_t)newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000343#endif
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000344 // 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, (size_t)newNrOfSamples);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000349
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000350 // Transform to frequency domain if we have enough data.
351 while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
352 // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
353 WebRtc_ReadBuffer(
354 aecpc->far_pre_buf, (void**)&farend_float, tmp_farend, PART_LEN2);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000355
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000356 WebRtcAec_BufferFarendPartition(aecpc->aec, farend_float);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000357
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000358 // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
359 WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000360#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000361 WebRtc_ReadBuffer(
362 aecpc->far_pre_buf_s16, (void**)&farend_ptr, newFarend, PART_LEN2);
363 WebRtc_WriteBuffer(
364 WebRtcAec_far_time_buf(aecpc->aec), &farend_ptr[PART_LEN], 1);
365 WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000366#endif
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000367 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000368
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000369 return retVal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000370}
371
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000372int32_t WebRtcAec_Process(void* aecInst,
373 const int16_t* nearend,
374 const int16_t* nearendH,
375 int16_t* out,
376 int16_t* outH,
377 int16_t nrOfSamples,
378 int16_t msInSndCardBuf,
379 int32_t skew) {
380 aecpc_t* aecpc = aecInst;
381 int32_t retVal = 0;
382 if (nearend == NULL) {
383 aecpc->lastError = AEC_NULL_POINTER_ERROR;
384 return -1;
385 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000386
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000387 if (out == NULL) {
388 aecpc->lastError = AEC_NULL_POINTER_ERROR;
389 return -1;
390 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000391
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000392 if (aecpc->initFlag != initCheck) {
393 aecpc->lastError = AEC_UNINITIALIZED_ERROR;
394 return -1;
395 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000396
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000397 // number of samples == 160 for SWB input
398 if (nrOfSamples != 80 && nrOfSamples != 160) {
399 aecpc->lastError = AEC_BAD_PARAMETER_ERROR;
400 return -1;
401 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000402
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000403 // Check for valid pointers based on sampling rate
404 if (aecpc->sampFreq == 32000 && nearendH == NULL) {
405 aecpc->lastError = AEC_NULL_POINTER_ERROR;
406 return -1;
407 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000408
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000409 if (msInSndCardBuf < 0) {
410 msInSndCardBuf = 0;
411 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
412 retVal = -1;
413 } else if (msInSndCardBuf > kMaxTrustedDelayMs) {
414 // The clamping is now done in ProcessExtended/Normal().
415 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
416 retVal = -1;
417 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000418
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000419 // This returns the value of aec->extended_filter_enabled.
420 if (WebRtcAec_delay_correction_enabled(aecpc->aec)) {
421 ProcessExtended(
422 aecpc, nearend, nearendH, out, outH, nrOfSamples, msInSndCardBuf, skew);
423 } else {
424 if (ProcessNormal(aecpc,
425 nearend,
426 nearendH,
427 out,
428 outH,
429 nrOfSamples,
430 msInSndCardBuf,
431 skew) != 0) {
432 retVal = -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000433 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000434 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000435
andrew@webrtc.org8594f762011-11-22 00:51:41 +0000436#ifdef WEBRTC_AEC_DEBUG_DUMP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000437 {
438 int16_t far_buf_size_ms = (int16_t)(WebRtcAec_system_delay(aecpc->aec) /
439 (sampMsNb * aecpc->rate_factor));
440 (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile);
441 (void)fwrite(
442 &aecpc->knownDelay, sizeof(aecpc->knownDelay), 1, aecpc->delayFile);
443 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000444#endif
445
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000446 return retVal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000449int WebRtcAec_set_config(void* handle, AecConfig config) {
450 aecpc_t* self = (aecpc_t*)handle;
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000451 if (self->initFlag != initCheck) {
452 self->lastError = AEC_UNINITIALIZED_ERROR;
453 return -1;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000454 }
455
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000456 if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
457 self->lastError = AEC_BAD_PARAMETER_ERROR;
458 return -1;
459 }
460 self->skewMode = config.skewMode;
461
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000462 if (config.nlpMode != kAecNlpConservative &&
463 config.nlpMode != kAecNlpModerate &&
464 config.nlpMode != kAecNlpAggressive) {
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000465 self->lastError = AEC_BAD_PARAMETER_ERROR;
466 return -1;
467 }
468
469 if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
470 self->lastError = AEC_BAD_PARAMETER_ERROR;
471 return -1;
472 }
473
474 if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
475 self->lastError = AEC_BAD_PARAMETER_ERROR;
476 return -1;
477 }
478
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000479 WebRtcAec_SetConfigCore(
480 self->aec, config.nlpMode, config.metricsMode, config.delay_logging);
bjornv@webrtc.org47b274d2013-02-20 17:09:47 +0000481 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000482}
483
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000484int WebRtcAec_get_echo_status(void* handle, int* status) {
485 aecpc_t* self = (aecpc_t*)handle;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000486 if (status == NULL) {
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000487 self->lastError = AEC_NULL_POINTER_ERROR;
488 return -1;
489 }
490 if (self->initFlag != initCheck) {
491 self->lastError = AEC_UNINITIALIZED_ERROR;
492 return -1;
493 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000494
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000495 *status = WebRtcAec_echo_state(self->aec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000496
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000497 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000498}
499
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000500int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) {
501 const float kUpWeight = 0.7f;
502 float dtmp;
503 int stmp;
504 aecpc_t* self = (aecpc_t*)handle;
bjornv@webrtc.orgcea70f42013-02-19 21:03:10 +0000505 Stats erl;
506 Stats erle;
507 Stats a_nlp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000508
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000509 if (handle == NULL) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000510 return -1;
511 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000512 if (metrics == NULL) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000513 self->lastError = AEC_NULL_POINTER_ERROR;
514 return -1;
515 }
516 if (self->initFlag != initCheck) {
517 self->lastError = AEC_UNINITIALIZED_ERROR;
518 return -1;
519 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000520
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000521 WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000522
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000523 // ERL
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000524 metrics->erl.instant = (int)erl.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000525
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000526 if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000527 // Use a mix between regular average and upper part average.
528 dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000529 metrics->erl.average = (int)dtmp;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000530 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000531 metrics->erl.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000532 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000533
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000534 metrics->erl.max = (int)erl.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000535
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000536 if (erl.min < (kOffsetLevel * (-1))) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000537 metrics->erl.min = (int)erl.min;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000538 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000539 metrics->erl.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000540 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000541
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000542 // ERLE
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000543 metrics->erle.instant = (int)erle.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000544
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000545 if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000546 // Use a mix between regular average and upper part average.
547 dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000548 metrics->erle.average = (int)dtmp;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000549 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000550 metrics->erle.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000551 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000552
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000553 metrics->erle.max = (int)erle.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000554
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000555 if (erle.min < (kOffsetLevel * (-1))) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000556 metrics->erle.min = (int)erle.min;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000557 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000558 metrics->erle.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000559 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000560
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000561 // RERL
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000562 if ((metrics->erl.average > kOffsetLevel) &&
563 (metrics->erle.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000564 stmp = metrics->erl.average + metrics->erle.average;
565 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000566 stmp = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000567 }
568 metrics->rerl.average = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000569
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000570 // No other statistics needed, but returned for completeness.
571 metrics->rerl.instant = stmp;
572 metrics->rerl.max = stmp;
573 metrics->rerl.min = stmp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000574
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000575 // A_NLP
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000576 metrics->aNlp.instant = (int)a_nlp.instant;
niklase@google.com470e71d2011-07-07 08:21:25 +0000577
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000578 if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) {
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000579 // Use a mix between regular average and upper part average.
580 dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000581 metrics->aNlp.average = (int)dtmp;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000582 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000583 metrics->aNlp.average = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000584 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000585
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000586 metrics->aNlp.max = (int)a_nlp.max;
niklase@google.com470e71d2011-07-07 08:21:25 +0000587
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000588 if (a_nlp.min < (kOffsetLevel * (-1))) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000589 metrics->aNlp.min = (int)a_nlp.min;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000590 } else {
bjornv@webrtc.org71e91f32013-02-20 19:24:50 +0000591 metrics->aNlp.min = kOffsetLevel;
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000592 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000593
bjornv@webrtc.orgb4cd3422013-02-15 18:40:34 +0000594 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000595}
596
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000597int WebRtcAec_GetDelayMetrics(void* handle, int* median, int* std) {
598 aecpc_t* self = handle;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000599 if (median == NULL) {
600 self->lastError = AEC_NULL_POINTER_ERROR;
601 return -1;
602 }
603 if (std == NULL) {
604 self->lastError = AEC_NULL_POINTER_ERROR;
605 return -1;
606 }
607 if (self->initFlag != initCheck) {
608 self->lastError = AEC_UNINITIALIZED_ERROR;
609 return -1;
610 }
bjornv@webrtc.org325f6252013-02-15 15:21:02 +0000611 if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std) == -1) {
612 // Logging disabled.
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000613 self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR;
614 return -1;
615 }
616
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000617 return 0;
618}
619
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000620int32_t WebRtcAec_get_error_code(void* aecInst) {
621 aecpc_t* aecpc = aecInst;
622 return aecpc->lastError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000623}
624
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000625AecCore* WebRtcAec_aec_core(void* handle) {
626 if (!handle) {
627 return NULL;
628 }
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000629 return ((aecpc_t*)handle)->aec;
bjornv@webrtc.org132c15d2013-02-27 21:03:41 +0000630}
631
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000632static int ProcessNormal(aecpc_t* aecpc,
633 const int16_t* nearend,
634 const int16_t* nearendH,
635 int16_t* out,
636 int16_t* outH,
637 int16_t nrOfSamples,
638 int16_t msInSndCardBuf,
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000639 int32_t skew) {
640 int retVal = 0;
641 short i;
642 short nBlocks10ms;
643 short nFrames;
644 // Limit resampling to doubling/halving of signal
645 const float minSkewEst = -0.5f;
646 const float maxSkewEst = 1.0f;
647
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000648 msInSndCardBuf =
649 msInSndCardBuf > kMaxTrustedDelayMs ? kMaxTrustedDelayMs : msInSndCardBuf;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000650 // TODO(andrew): we need to investigate if this +10 is really wanted.
651 msInSndCardBuf += 10;
652 aecpc->msInSndCardBuf = msInSndCardBuf;
653
654 if (aecpc->skewMode == kAecTrue) {
655 if (aecpc->skewFrCtr < 25) {
656 aecpc->skewFrCtr++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000657 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000658 retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
659 if (retVal == -1) {
660 aecpc->skew = 0;
661 aecpc->lastError = AEC_BAD_PARAMETER_WARNING;
662 }
663
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000664 aecpc->skew /= aecpc->sampFactor * nrOfSamples;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000665
666 if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
667 aecpc->resample = kAecFalse;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000668 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000669 aecpc->resample = kAecTrue;
670 }
671
672 if (aecpc->skew < minSkewEst) {
673 aecpc->skew = minSkewEst;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000674 } else if (aecpc->skew > maxSkewEst) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000675 aecpc->skew = maxSkewEst;
676 }
677
678#ifdef WEBRTC_AEC_DEBUG_DUMP
679 (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile);
680#endif
681 }
682 }
683
684 nFrames = nrOfSamples / FRAME_LEN;
685 nBlocks10ms = nFrames / aecpc->rate_factor;
686
687 if (aecpc->startup_phase) {
688 // Only needed if they don't already point to the same place.
689 if (nearend != out) {
690 memcpy(out, nearend, sizeof(short) * nrOfSamples);
691 }
692 if (nearendH != outH) {
693 memcpy(outH, nearendH, sizeof(short) * nrOfSamples);
694 }
695
696 // The AEC is in the start up mode
697 // AEC is disabled until the system delay is OK
698
699 // Mechanism to ensure that the system delay is reasonably stable.
700 if (aecpc->checkBuffSize) {
701 aecpc->checkBufSizeCtr++;
702 // Before we fill up the far-end buffer we require the system delay
703 // to be stable (+/-8 ms) compared to the first value. This
704 // comparison is made during the following 6 consecutive 10 ms
705 // blocks. If it seems to be stable then we start to fill up the
706 // far-end buffer.
707 if (aecpc->counter == 0) {
708 aecpc->firstVal = aecpc->msInSndCardBuf;
709 aecpc->sum = 0;
710 }
711
712 if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000713 WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000714 aecpc->sum += aecpc->msInSndCardBuf;
715 aecpc->counter++;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000716 } else {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000717 aecpc->counter = 0;
718 }
719
720 if (aecpc->counter * nBlocks10ms >= 6) {
721 // The far-end buffer size is determined in partitions of
722 // PART_LEN samples. Use 75% of the average value of the system
723 // delay as buffer size to start with.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000724 aecpc->bufSizeStart =
725 WEBRTC_SPL_MIN((3 * aecpc->sum * aecpc->rate_factor * 8) /
726 (4 * aecpc->counter * PART_LEN),
727 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000728 // Buffer size has now been determined.
729 aecpc->checkBuffSize = 0;
730 }
731
732 if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
733 // For really bad systems, don't disable the echo canceller for
734 // more than 0.5 sec.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000735 aecpc->bufSizeStart = WEBRTC_SPL_MIN(
736 (aecpc->msInSndCardBuf * aecpc->rate_factor * 3) / 40,
737 kMaxBufSizeStart);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000738 aecpc->checkBuffSize = 0;
739 }
740 }
741
742 // If |checkBuffSize| changed in the if-statement above.
743 if (!aecpc->checkBuffSize) {
744 // The system delay is now reasonably stable (or has been unstable
745 // for too long). When the far-end buffer is filled with
746 // approximately the same amount of data as reported by the system
747 // we end the startup phase.
748 int overhead_elements =
749 WebRtcAec_system_delay(aecpc->aec) / PART_LEN - aecpc->bufSizeStart;
750 if (overhead_elements == 0) {
751 // Enable the AEC
752 aecpc->startup_phase = 0;
753 } else if (overhead_elements > 0) {
754 // TODO(bjornv): Do we need a check on how much we actually
755 // moved the read pointer? It should always be possible to move
756 // the pointer |overhead_elements| since we have only added data
757 // to the buffer and no delay compensation nor AEC processing
758 // has been done.
759 WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements);
760
761 // Enable the AEC
762 aecpc->startup_phase = 0;
763 }
764 }
765 } else {
766 // AEC is enabled.
767 EstBufDelayNormal(aecpc);
768
769 // Note that 1 frame is supported for NB and 2 frames for WB.
770 for (i = 0; i < nFrames; i++) {
771 // Call the AEC.
772 WebRtcAec_ProcessFrame(aecpc->aec,
773 &nearend[FRAME_LEN * i],
774 &nearendH[FRAME_LEN * i],
775 aecpc->knownDelay,
776 &out[FRAME_LEN * i],
777 &outH[FRAME_LEN * i]);
778 // TODO(bjornv): Re-structure such that we don't have to pass
779 // |aecpc->knownDelay| as input. Change name to something like
780 // |system_buffer_diff|.
781 }
782 }
783
784 return retVal;
785}
786
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000787static void ProcessExtended(aecpc_t* self,
788 const int16_t* near,
789 const int16_t* near_high,
790 int16_t* out,
791 int16_t* out_high,
792 int16_t num_samples,
793 int16_t reported_delay_ms,
794 int32_t skew) {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000795 int i;
796 const int num_frames = num_samples / FRAME_LEN;
797#if defined(WEBRTC_UNTRUSTED_DELAY)
798 const int delay_diff_offset = kDelayDiffOffsetSamples;
799 reported_delay_ms = kFixedDelayMs;
800#else
801 // This is the usual mode where we trust the reported system delay values.
802 const int delay_diff_offset = 0;
803 // Due to the longer filter, we no longer add 10 ms to the reported delay
804 // to reduce chance of non-causality. Instead we apply a minimum here to avoid
805 // issues with the read pointer jumping around needlessly.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000806 reported_delay_ms = reported_delay_ms < kMinTrustedDelayMs
807 ? kMinTrustedDelayMs
808 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000809 // If the reported delay appears to be bogus, we attempt to recover by using
810 // the measured fixed delay values. We use >= here because higher layers
811 // may already clamp to this maximum value, and we would otherwise not
812 // detect it here.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000813 reported_delay_ms = reported_delay_ms >= kMaxTrustedDelayMs
814 ? kFixedDelayMs
815 : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000816#endif
817 self->msInSndCardBuf = reported_delay_ms;
818
819 if (!self->farend_started) {
820 // Only needed if they don't already point to the same place.
821 if (near != out) {
822 memcpy(out, near, sizeof(short) * num_samples);
823 }
824 if (near_high != out_high) {
825 memcpy(out_high, near_high, sizeof(short) * num_samples);
826 }
827 return;
828 }
829 if (self->startup_phase) {
830 // In the extended mode, there isn't a startup "phase", just a special
831 // action on the first frame. In the trusted delay case, we'll take the
832 // current reported delay, unless it's less then our conservative
833 // measurement.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000834 int startup_size_ms =
835 reported_delay_ms < kFixedDelayMs ? kFixedDelayMs : reported_delay_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000836 int overhead_elements = (WebRtcAec_system_delay(self->aec) -
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000837 startup_size_ms / 2 * self->rate_factor * 8) /
838 PART_LEN;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000839 WebRtcAec_MoveFarReadPtr(self->aec, overhead_elements);
840 self->startup_phase = 0;
841 }
842
843 EstBufDelayExtended(self);
844
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000845 {
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000846 // |delay_diff_offset| gives us the option to manually rewind the delay on
847 // very low delay platforms which can't be expressed purely through
848 // |reported_delay_ms|.
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000849 const int adjusted_known_delay =
850 WEBRTC_SPL_MAX(0, self->knownDelay + delay_diff_offset);
851
852 for (i = 0; i < num_frames; ++i) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000853 WebRtcAec_ProcessFrame(self->aec,
854 &near[FRAME_LEN * i],
855 &near_high[FRAME_LEN * i],
856 adjusted_known_delay,
857 &out[FRAME_LEN * i],
858 &out_high[FRAME_LEN * i]);
andrew@webrtc.org8e2f9bc2013-10-01 01:12:25 +0000859 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000860 }
861}
862
863static void EstBufDelayNormal(aecpc_t* aecpc) {
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000864 int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor;
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000865 int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->aec);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000866 int delay_difference = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000867
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000868 // Before we proceed with the delay estimate filtering we:
869 // 1) Compensate for the frame that will be read.
870 // 2) Compensate for drift resampling.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000871 // 3) Compensate for non-causality if needed, since the estimated delay can't
872 // be negative.
niklase@google.com470e71d2011-07-07 08:21:25 +0000873
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000874 // 1) Compensating for the frame(s) that will be read/processed.
bjornv@webrtc.org6f6acd92013-02-14 21:17:12 +0000875 current_delay += FRAME_LEN * aecpc->rate_factor;
niklase@google.com470e71d2011-07-07 08:21:25 +0000876
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000877 // 2) Account for resampling frame delay.
878 if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
879 current_delay -= kResamplingDelay;
880 }
881
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000882 // 3) Compensate for non-causality, if needed, by flushing one block.
andrew@webrtc.org61bf8e32012-03-15 19:04:55 +0000883 if (current_delay < PART_LEN) {
884 current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
885 }
886
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000887 // We use -1 to signal an initialized state in the "extended" implementation;
888 // compensate for that.
889 aecpc->filtDelay = aecpc->filtDelay < 0 ? 0 : aecpc->filtDelay;
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000890 aecpc->filtDelay =
891 WEBRTC_SPL_MAX(0, (short)(0.8 * aecpc->filtDelay + 0.2 * current_delay));
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000892
893 delay_difference = aecpc->filtDelay - aecpc->knownDelay;
894 if (delay_difference > 224) {
895 if (aecpc->lastDelayDiff < 96) {
896 aecpc->timeForDelayChange = 0;
897 } else {
898 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000899 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000900 } else if (delay_difference < 96 && aecpc->knownDelay > 0) {
901 if (aecpc->lastDelayDiff > 224) {
902 aecpc->timeForDelayChange = 0;
903 } else {
904 aecpc->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000905 }
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000906 } else {
907 aecpc->timeForDelayChange = 0;
908 }
909 aecpc->lastDelayDiff = delay_difference;
niklase@google.com470e71d2011-07-07 08:21:25 +0000910
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000911 if (aecpc->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000912 aecpc->knownDelay = WEBRTC_SPL_MAX((int)aecpc->filtDelay - 160, 0);
bjornv@webrtc.org7270a6b2011-12-28 08:44:17 +0000913 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000914}
niklase@google.com470e71d2011-07-07 08:21:25 +0000915
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000916static void EstBufDelayExtended(aecpc_t* self) {
917 int reported_delay = self->msInSndCardBuf * sampMsNb * self->rate_factor;
918 int current_delay = reported_delay - WebRtcAec_system_delay(self->aec);
919 int delay_difference = 0;
920
921 // Before we proceed with the delay estimate filtering we:
922 // 1) Compensate for the frame that will be read.
923 // 2) Compensate for drift resampling.
924 // 3) Compensate for non-causality if needed, since the estimated delay can't
925 // be negative.
926
927 // 1) Compensating for the frame(s) that will be read/processed.
928 current_delay += FRAME_LEN * self->rate_factor;
929
930 // 2) Account for resampling frame delay.
931 if (self->skewMode == kAecTrue && self->resample == kAecTrue) {
932 current_delay -= kResamplingDelay;
933 }
934
935 // 3) Compensate for non-causality, if needed, by flushing two blocks.
936 if (current_delay < PART_LEN) {
937 current_delay += WebRtcAec_MoveFarReadPtr(self->aec, 2) * PART_LEN;
938 }
939
940 if (self->filtDelay == -1) {
941 self->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay);
942 } else {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000943 self->filtDelay = WEBRTC_SPL_MAX(
944 0, (short)(0.95 * self->filtDelay + 0.05 * current_delay));
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000945 }
946
947 delay_difference = self->filtDelay - self->knownDelay;
948 if (delay_difference > 384) {
949 if (self->lastDelayDiff < 128) {
950 self->timeForDelayChange = 0;
951 } else {
952 self->timeForDelayChange++;
953 }
954 } else if (delay_difference < 128 && self->knownDelay > 0) {
955 if (self->lastDelayDiff > 384) {
956 self->timeForDelayChange = 0;
957 } else {
958 self->timeForDelayChange++;
959 }
960 } else {
961 self->timeForDelayChange = 0;
962 }
963 self->lastDelayDiff = delay_difference;
964
965 if (self->timeForDelayChange > 25) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000966 self->knownDelay = WEBRTC_SPL_MAX((int)self->filtDelay - 256, 0);
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000967 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000968}