echo_cancellation.c revision 9b72af94cd61782ada88f777b07854daf9657bb2
1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
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 */
14#include "webrtc/modules/audio_processing/aec/echo_cancellation.h"
15
16#include <math.h>
17#ifdef WEBRTC_AEC_DEBUG_DUMP
18#include <stdio.h>
19#endif
20#include <stdlib.h>
21#include <string.h>
22
23#include "webrtc/common_audio/ring_buffer.h"
24#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
25#include "webrtc/modules/audio_processing/aec/aec_core.h"
26#include "webrtc/modules/audio_processing/aec/aec_resampler.h"
27#include "webrtc/modules/audio_processing/aec/echo_cancellation_internal.h"
28#include "webrtc/typedefs.h"
29
30// 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
69#endif
70
71#if defined(WEBRTC_UNTRUSTED_DELAY) && defined(WEBRTC_MAC)
72static const int kDelayDiffOffsetSamples = -160;
73#else
74// Not enabled for now.
75static const int kDelayDiffOffsetSamples = 0;
76#endif
77
78#if defined(WEBRTC_MAC)
79static const int kFixedDelayMs = 20;
80#else
81static const int kFixedDelayMs = 50;
82#endif
83#if !defined(WEBRTC_UNTRUSTED_DELAY)
84static const int kMinTrustedDelayMs = 20;
85#endif
86static const int kMaxTrustedDelayMs = 500;
87
88// 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
91// TODO(bjornv): Replace with kResamplerBufferSize
92#define MAX_RESAMP_LEN (5 * FRAME_LEN)
93
94static const int kMaxBufSizeStart = 62;  // In partitions
95static const int sampMsNb = 8;           // samples per ms in nb
96static const int initCheck = 42;
97
98#ifdef WEBRTC_AEC_DEBUG_DUMP
99int webrtc_aec_instance_count = 0;
100#endif
101
102// Estimates delay to set the position of the far-end buffer read pointer
103// (controlled by knownDelay)
104static void EstBufDelayNormal(Aec* aecInst);
105static void EstBufDelayExtended(Aec* aecInst);
106static int ProcessNormal(Aec* self,
107                         const float* const* near,
108                         size_t num_bands,
109                         float* const* out,
110                         size_t num_samples,
111                         int16_t reported_delay_ms,
112                         int32_t skew);
113static void ProcessExtended(Aec* self,
114                            const float* const* near,
115                            size_t num_bands,
116                            float* const* out,
117                            size_t num_samples,
118                            int16_t reported_delay_ms,
119                            int32_t skew);
120
121void* WebRtcAec_Create() {
122  Aec* aecpc = malloc(sizeof(Aec));
123
124  if (!aecpc) {
125    return NULL;
126  }
127
128  aecpc->aec = WebRtcAec_CreateAec();
129  if (!aecpc->aec) {
130    WebRtcAec_Free(aecpc);
131    return NULL;
132  }
133  aecpc->resampler = WebRtcAec_CreateResampler();
134  if (!aecpc->resampler) {
135    WebRtcAec_Free(aecpc);
136    return NULL;
137  }
138  // Create far-end pre-buffer. The buffer size has to be large enough for
139  // largest possible drift compensation (kResamplerBufferSize) + "almost" an
140  // FFT buffer (PART_LEN2 - 1).
141  aecpc->far_pre_buf =
142      WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize, sizeof(float));
143  if (!aecpc->far_pre_buf) {
144    WebRtcAec_Free(aecpc);
145    return NULL;
146  }
147
148  aecpc->initFlag = 0;
149
150#ifdef WEBRTC_AEC_DEBUG_DUMP
151  {
152    char filename[64];
153    sprintf(filename, "aec_buf%d.dat", webrtc_aec_instance_count);
154    aecpc->bufFile = fopen(filename, "wb");
155    sprintf(filename, "aec_skew%d.dat", webrtc_aec_instance_count);
156    aecpc->skewFile = fopen(filename, "wb");
157    sprintf(filename, "aec_delay%d.dat", webrtc_aec_instance_count);
158    aecpc->delayFile = fopen(filename, "wb");
159    webrtc_aec_instance_count++;
160  }
161#endif
162
163  return aecpc;
164}
165
166void WebRtcAec_Free(void* aecInst) {
167  Aec* aecpc = aecInst;
168
169  if (aecpc == NULL) {
170    return;
171  }
172
173  WebRtc_FreeBuffer(aecpc->far_pre_buf);
174
175#ifdef WEBRTC_AEC_DEBUG_DUMP
176  fclose(aecpc->bufFile);
177  fclose(aecpc->skewFile);
178  fclose(aecpc->delayFile);
179#endif
180
181  WebRtcAec_FreeAec(aecpc->aec);
182  WebRtcAec_FreeResampler(aecpc->resampler);
183  free(aecpc);
184}
185
186int32_t WebRtcAec_Init(void* aecInst, int32_t sampFreq, int32_t scSampFreq) {
187  Aec* aecpc = aecInst;
188  AecConfig aecConfig;
189
190  if (sampFreq != 8000 &&
191      sampFreq != 16000 &&
192      sampFreq != 32000 &&
193      sampFreq != 48000) {
194    return AEC_BAD_PARAMETER_ERROR;
195  }
196  aecpc->sampFreq = sampFreq;
197
198  if (scSampFreq < 1 || scSampFreq > 96000) {
199    return AEC_BAD_PARAMETER_ERROR;
200  }
201  aecpc->scSampFreq = scSampFreq;
202
203  // Initialize echo canceller core
204  if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) {
205    return AEC_UNSPECIFIED_ERROR;
206  }
207
208  if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) {
209    return AEC_UNSPECIFIED_ERROR;
210  }
211
212  WebRtc_InitBuffer(aecpc->far_pre_buf);
213  WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);  // Start overlap.
214
215  aecpc->initFlag = initCheck;  // indicates that initialization has been done
216
217  if (aecpc->sampFreq == 32000 || aecpc->sampFreq == 48000) {
218    aecpc->splitSampFreq = 16000;
219  } else {
220    aecpc->splitSampFreq = sampFreq;
221  }
222
223  aecpc->delayCtr = 0;
224  aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq;
225  // Sampling frequency multiplier (SWB is processed as 160 frame size).
226  aecpc->rate_factor = aecpc->splitSampFreq / 8000;
227
228  aecpc->sum = 0;
229  aecpc->counter = 0;
230  aecpc->checkBuffSize = 1;
231  aecpc->firstVal = 0;
232
233  // We skip the startup_phase completely (setting to 0) if DA-AEC is enabled,
234  // but not extended_filter mode.
235  aecpc->startup_phase = WebRtcAec_extended_filter_enabled(aecpc->aec) ||
236      !WebRtcAec_delay_agnostic_enabled(aecpc->aec);
237  aecpc->bufSizeStart = 0;
238  aecpc->checkBufSizeCtr = 0;
239  aecpc->msInSndCardBuf = 0;
240  aecpc->filtDelay = -1;  // -1 indicates an initialized state.
241  aecpc->timeForDelayChange = 0;
242  aecpc->knownDelay = 0;
243  aecpc->lastDelayDiff = 0;
244
245  aecpc->skewFrCtr = 0;
246  aecpc->resample = kAecFalse;
247  aecpc->highSkewCtr = 0;
248  aecpc->skew = 0;
249
250  aecpc->farend_started = 0;
251
252  // Default settings.
253  aecConfig.nlpMode = kAecNlpModerate;
254  aecConfig.skewMode = kAecFalse;
255  aecConfig.metricsMode = kAecFalse;
256  aecConfig.delay_logging = kAecFalse;
257
258  if (WebRtcAec_set_config(aecpc, aecConfig) == -1) {
259    return AEC_UNSPECIFIED_ERROR;
260  }
261
262  return 0;
263}
264
265// Returns any error that is caused when buffering the
266// far-end signal.
267int32_t WebRtcAec_GetBufferFarendError(void* aecInst,
268                                       const float* farend,
269                                       size_t nrOfSamples) {
270  Aec* aecpc = aecInst;
271
272  if (!farend)
273    return AEC_NULL_POINTER_ERROR;
274
275  if (aecpc->initFlag != initCheck)
276    return AEC_UNINITIALIZED_ERROR;
277
278  // number of samples == 160 for SWB input
279  if (nrOfSamples != 80 && nrOfSamples != 160)
280    return AEC_BAD_PARAMETER_ERROR;
281
282  return 0;
283}
284
285// only buffer L band for farend
286int32_t WebRtcAec_BufferFarend(void* aecInst,
287                               const float* farend,
288                               size_t nrOfSamples) {
289  Aec* aecpc = aecInst;
290  size_t newNrOfSamples = nrOfSamples;
291  float new_farend[MAX_RESAMP_LEN];
292  const float* farend_ptr = farend;
293
294  // Get any error caused by buffering the farend signal.
295  int32_t error_code = WebRtcAec_GetBufferFarendError(aecInst, farend,
296                                                      nrOfSamples);
297
298  if (error_code != 0)
299    return error_code;
300
301
302  if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
303    // Resample and get a new number of samples
304    WebRtcAec_ResampleLinear(aecpc->resampler,
305                             farend,
306                             nrOfSamples,
307                             aecpc->skew,
308                             new_farend,
309                             &newNrOfSamples);
310    farend_ptr = new_farend;
311  }
312
313  aecpc->farend_started = 1;
314  WebRtcAec_SetSystemDelay(
315      aecpc->aec, WebRtcAec_system_delay(aecpc->aec) + (int)newNrOfSamples);
316
317  // Write the time-domain data to |far_pre_buf|.
318  WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_ptr, newNrOfSamples);
319
320  // Transform to frequency domain if we have enough data.
321  while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) {
322    // We have enough data to pass to the FFT, hence read PART_LEN2 samples.
323    {
324      float* ptmp = NULL;
325      float tmp[PART_LEN2];
326      WebRtc_ReadBuffer(aecpc->far_pre_buf, (void**)&ptmp, tmp, PART_LEN2);
327      WebRtcAec_BufferFarendPartition(aecpc->aec, ptmp);
328#ifdef WEBRTC_AEC_DEBUG_DUMP
329      WebRtc_WriteBuffer(
330          WebRtcAec_far_time_buf(aecpc->aec), &ptmp[PART_LEN], 1);
331#endif
332    }
333
334    // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing.
335    WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN);
336  }
337
338  return 0;
339}
340
341int32_t WebRtcAec_Process(void* aecInst,
342                          const float* const* nearend,
343                          size_t num_bands,
344                          float* const* out,
345                          size_t nrOfSamples,
346                          int16_t msInSndCardBuf,
347                          int32_t skew) {
348  Aec* aecpc = aecInst;
349  int32_t retVal = 0;
350
351  if (out == NULL) {
352    return AEC_NULL_POINTER_ERROR;
353  }
354
355  if (aecpc->initFlag != initCheck) {
356    return AEC_UNINITIALIZED_ERROR;
357  }
358
359  // number of samples == 160 for SWB input
360  if (nrOfSamples != 80 && nrOfSamples != 160) {
361    return AEC_BAD_PARAMETER_ERROR;
362  }
363
364  if (msInSndCardBuf < 0) {
365    msInSndCardBuf = 0;
366    retVal = AEC_BAD_PARAMETER_WARNING;
367  } else if (msInSndCardBuf > kMaxTrustedDelayMs) {
368    // The clamping is now done in ProcessExtended/Normal().
369    retVal = AEC_BAD_PARAMETER_WARNING;
370  }
371
372  // This returns the value of aec->extended_filter_enabled.
373  if (WebRtcAec_extended_filter_enabled(aecpc->aec)) {
374    ProcessExtended(aecpc,
375                    nearend,
376                    num_bands,
377                    out,
378                    nrOfSamples,
379                    msInSndCardBuf,
380                    skew);
381  } else {
382    retVal = ProcessNormal(aecpc,
383                           nearend,
384                           num_bands,
385                           out,
386                           nrOfSamples,
387                           msInSndCardBuf,
388                           skew);
389  }
390
391#ifdef WEBRTC_AEC_DEBUG_DUMP
392  {
393    int16_t far_buf_size_ms = (int16_t)(WebRtcAec_system_delay(aecpc->aec) /
394                                        (sampMsNb * aecpc->rate_factor));
395    (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile);
396    (void)fwrite(
397        &aecpc->knownDelay, sizeof(aecpc->knownDelay), 1, aecpc->delayFile);
398  }
399#endif
400
401  return retVal;
402}
403
404int WebRtcAec_set_config(void* handle, AecConfig config) {
405  Aec* self = (Aec*)handle;
406  if (self->initFlag != initCheck) {
407    return AEC_UNINITIALIZED_ERROR;
408  }
409
410  if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) {
411    return AEC_BAD_PARAMETER_ERROR;
412  }
413  self->skewMode = config.skewMode;
414
415  if (config.nlpMode != kAecNlpConservative &&
416      config.nlpMode != kAecNlpModerate &&
417      config.nlpMode != kAecNlpAggressive) {
418    return AEC_BAD_PARAMETER_ERROR;
419  }
420
421  if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) {
422    return AEC_BAD_PARAMETER_ERROR;
423  }
424
425  if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) {
426    return AEC_BAD_PARAMETER_ERROR;
427  }
428
429  WebRtcAec_SetConfigCore(
430      self->aec, config.nlpMode, config.metricsMode, config.delay_logging);
431  return 0;
432}
433
434int WebRtcAec_get_echo_status(void* handle, int* status) {
435  Aec* self = (Aec*)handle;
436  if (status == NULL) {
437    return AEC_NULL_POINTER_ERROR;
438  }
439  if (self->initFlag != initCheck) {
440    return AEC_UNINITIALIZED_ERROR;
441  }
442
443  *status = WebRtcAec_echo_state(self->aec);
444
445  return 0;
446}
447
448int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) {
449  const float kUpWeight = 0.7f;
450  float dtmp;
451  int stmp;
452  Aec* self = (Aec*)handle;
453  Stats erl;
454  Stats erle;
455  Stats a_nlp;
456
457  if (handle == NULL) {
458    return -1;
459  }
460  if (metrics == NULL) {
461    return AEC_NULL_POINTER_ERROR;
462  }
463  if (self->initFlag != initCheck) {
464    return AEC_UNINITIALIZED_ERROR;
465  }
466
467  WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp);
468
469  // ERL
470  metrics->erl.instant = (int)erl.instant;
471
472  if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) {
473    // Use a mix between regular average and upper part average.
474    dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average;
475    metrics->erl.average = (int)dtmp;
476  } else {
477    metrics->erl.average = kOffsetLevel;
478  }
479
480  metrics->erl.max = (int)erl.max;
481
482  if (erl.min < (kOffsetLevel * (-1))) {
483    metrics->erl.min = (int)erl.min;
484  } else {
485    metrics->erl.min = kOffsetLevel;
486  }
487
488  // ERLE
489  metrics->erle.instant = (int)erle.instant;
490
491  if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) {
492    // Use a mix between regular average and upper part average.
493    dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average;
494    metrics->erle.average = (int)dtmp;
495  } else {
496    metrics->erle.average = kOffsetLevel;
497  }
498
499  metrics->erle.max = (int)erle.max;
500
501  if (erle.min < (kOffsetLevel * (-1))) {
502    metrics->erle.min = (int)erle.min;
503  } else {
504    metrics->erle.min = kOffsetLevel;
505  }
506
507  // RERL
508  if ((metrics->erl.average > kOffsetLevel) &&
509      (metrics->erle.average > kOffsetLevel)) {
510    stmp = metrics->erl.average + metrics->erle.average;
511  } else {
512    stmp = kOffsetLevel;
513  }
514  metrics->rerl.average = stmp;
515
516  // No other statistics needed, but returned for completeness.
517  metrics->rerl.instant = stmp;
518  metrics->rerl.max = stmp;
519  metrics->rerl.min = stmp;
520
521  // A_NLP
522  metrics->aNlp.instant = (int)a_nlp.instant;
523
524  if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) {
525    // Use a mix between regular average and upper part average.
526    dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average;
527    metrics->aNlp.average = (int)dtmp;
528  } else {
529    metrics->aNlp.average = kOffsetLevel;
530  }
531
532  metrics->aNlp.max = (int)a_nlp.max;
533
534  if (a_nlp.min < (kOffsetLevel * (-1))) {
535    metrics->aNlp.min = (int)a_nlp.min;
536  } else {
537    metrics->aNlp.min = kOffsetLevel;
538  }
539
540  return 0;
541}
542
543int WebRtcAec_GetDelayMetrics(void* handle,
544                              int* median,
545                              int* std,
546                              float* fraction_poor_delays) {
547  Aec* self = handle;
548  if (median == NULL) {
549    return AEC_NULL_POINTER_ERROR;
550  }
551  if (std == NULL) {
552    return AEC_NULL_POINTER_ERROR;
553  }
554  if (self->initFlag != initCheck) {
555    return AEC_UNINITIALIZED_ERROR;
556  }
557  if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std,
558                                    fraction_poor_delays) ==
559      -1) {
560    // Logging disabled.
561    return AEC_UNSUPPORTED_FUNCTION_ERROR;
562  }
563
564  return 0;
565}
566
567
568AecCore* WebRtcAec_aec_core(void* handle) {
569  if (!handle) {
570    return NULL;
571  }
572  return ((Aec*)handle)->aec;
573}
574
575static int ProcessNormal(Aec* aecpc,
576                         const float* const* nearend,
577                         size_t num_bands,
578                         float* const* out,
579                         size_t nrOfSamples,
580                         int16_t msInSndCardBuf,
581                         int32_t skew) {
582  int retVal = 0;
583  size_t i;
584  size_t nBlocks10ms;
585  // Limit resampling to doubling/halving of signal
586  const float minSkewEst = -0.5f;
587  const float maxSkewEst = 1.0f;
588
589  msInSndCardBuf =
590      msInSndCardBuf > kMaxTrustedDelayMs ? kMaxTrustedDelayMs : msInSndCardBuf;
591  // TODO(andrew): we need to investigate if this +10 is really wanted.
592  msInSndCardBuf += 10;
593  aecpc->msInSndCardBuf = msInSndCardBuf;
594
595  if (aecpc->skewMode == kAecTrue) {
596    if (aecpc->skewFrCtr < 25) {
597      aecpc->skewFrCtr++;
598    } else {
599      retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew);
600      if (retVal == -1) {
601        aecpc->skew = 0;
602        retVal = AEC_BAD_PARAMETER_WARNING;
603      }
604
605      aecpc->skew /= aecpc->sampFactor * nrOfSamples;
606
607      if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) {
608        aecpc->resample = kAecFalse;
609      } else {
610        aecpc->resample = kAecTrue;
611      }
612
613      if (aecpc->skew < minSkewEst) {
614        aecpc->skew = minSkewEst;
615      } else if (aecpc->skew > maxSkewEst) {
616        aecpc->skew = maxSkewEst;
617      }
618
619#ifdef WEBRTC_AEC_DEBUG_DUMP
620      (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile);
621#endif
622    }
623  }
624
625  nBlocks10ms = nrOfSamples / (FRAME_LEN * aecpc->rate_factor);
626
627  if (aecpc->startup_phase) {
628    for (i = 0; i < num_bands; ++i) {
629      // Only needed if they don't already point to the same place.
630      if (nearend[i] != out[i]) {
631        memcpy(out[i], nearend[i], sizeof(nearend[i][0]) * nrOfSamples);
632      }
633    }
634
635    // The AEC is in the start up mode
636    // AEC is disabled until the system delay is OK
637
638    // Mechanism to ensure that the system delay is reasonably stable.
639    if (aecpc->checkBuffSize) {
640      aecpc->checkBufSizeCtr++;
641      // Before we fill up the far-end buffer we require the system delay
642      // to be stable (+/-8 ms) compared to the first value. This
643      // comparison is made during the following 6 consecutive 10 ms
644      // blocks. If it seems to be stable then we start to fill up the
645      // far-end buffer.
646      if (aecpc->counter == 0) {
647        aecpc->firstVal = aecpc->msInSndCardBuf;
648        aecpc->sum = 0;
649      }
650
651      if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) <
652          WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) {
653        aecpc->sum += aecpc->msInSndCardBuf;
654        aecpc->counter++;
655      } else {
656        aecpc->counter = 0;
657      }
658
659      if (aecpc->counter * nBlocks10ms >= 6) {
660        // The far-end buffer size is determined in partitions of
661        // PART_LEN samples. Use 75% of the average value of the system
662        // delay as buffer size to start with.
663        aecpc->bufSizeStart =
664            WEBRTC_SPL_MIN((3 * aecpc->sum * aecpc->rate_factor * 8) /
665                               (4 * aecpc->counter * PART_LEN),
666                           kMaxBufSizeStart);
667        // Buffer size has now been determined.
668        aecpc->checkBuffSize = 0;
669      }
670
671      if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) {
672        // For really bad systems, don't disable the echo canceller for
673        // more than 0.5 sec.
674        aecpc->bufSizeStart = WEBRTC_SPL_MIN(
675            (aecpc->msInSndCardBuf * aecpc->rate_factor * 3) / 40,
676            kMaxBufSizeStart);
677        aecpc->checkBuffSize = 0;
678      }
679    }
680
681    // If |checkBuffSize| changed in the if-statement above.
682    if (!aecpc->checkBuffSize) {
683      // The system delay is now reasonably stable (or has been unstable
684      // for too long). When the far-end buffer is filled with
685      // approximately the same amount of data as reported by the system
686      // we end the startup phase.
687      int overhead_elements =
688          WebRtcAec_system_delay(aecpc->aec) / PART_LEN - aecpc->bufSizeStart;
689      if (overhead_elements == 0) {
690        // Enable the AEC
691        aecpc->startup_phase = 0;
692      } else if (overhead_elements > 0) {
693        // TODO(bjornv): Do we need a check on how much we actually
694        // moved the read pointer? It should always be possible to move
695        // the pointer |overhead_elements| since we have only added data
696        // to the buffer and no delay compensation nor AEC processing
697        // has been done.
698        WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements);
699
700        // Enable the AEC
701        aecpc->startup_phase = 0;
702      }
703    }
704  } else {
705    // AEC is enabled.
706    EstBufDelayNormal(aecpc);
707
708    // Call the AEC.
709    // TODO(bjornv): Re-structure such that we don't have to pass
710    // |aecpc->knownDelay| as input. Change name to something like
711    // |system_buffer_diff|.
712    WebRtcAec_ProcessFrames(aecpc->aec,
713                            nearend,
714                            num_bands,
715                            nrOfSamples,
716                            aecpc->knownDelay,
717                            out);
718  }
719
720  return retVal;
721}
722
723static void ProcessExtended(Aec* self,
724                            const float* const* near,
725                            size_t num_bands,
726                            float* const* out,
727                            size_t num_samples,
728                            int16_t reported_delay_ms,
729                            int32_t skew) {
730  size_t i;
731  const int delay_diff_offset = kDelayDiffOffsetSamples;
732#if defined(WEBRTC_UNTRUSTED_DELAY)
733  reported_delay_ms = kFixedDelayMs;
734#else
735  // This is the usual mode where we trust the reported system delay values.
736  // Due to the longer filter, we no longer add 10 ms to the reported delay
737  // to reduce chance of non-causality. Instead we apply a minimum here to avoid
738  // issues with the read pointer jumping around needlessly.
739  reported_delay_ms = reported_delay_ms < kMinTrustedDelayMs
740                          ? kMinTrustedDelayMs
741                          : reported_delay_ms;
742  // If the reported delay appears to be bogus, we attempt to recover by using
743  // the measured fixed delay values. We use >= here because higher layers
744  // may already clamp to this maximum value, and we would otherwise not
745  // detect it here.
746  reported_delay_ms = reported_delay_ms >= kMaxTrustedDelayMs
747                          ? kFixedDelayMs
748                          : reported_delay_ms;
749#endif
750  self->msInSndCardBuf = reported_delay_ms;
751
752  if (!self->farend_started) {
753    for (i = 0; i < num_bands; ++i) {
754      // Only needed if they don't already point to the same place.
755      if (near[i] != out[i]) {
756        memcpy(out[i], near[i], sizeof(near[i][0]) * num_samples);
757      }
758    }
759    return;
760  }
761  if (self->startup_phase) {
762    // In the extended mode, there isn't a startup "phase", just a special
763    // action on the first frame. In the trusted delay case, we'll take the
764    // current reported delay, unless it's less then our conservative
765    // measurement.
766    int startup_size_ms =
767        reported_delay_ms < kFixedDelayMs ? kFixedDelayMs : reported_delay_ms;
768#if defined(WEBRTC_ANDROID)
769    int target_delay = startup_size_ms * self->rate_factor * 8;
770#else
771    // To avoid putting the AEC in a non-causal state we're being slightly
772    // conservative and scale by 2. On Android we use a fixed delay and
773    // therefore there is no need to scale the target_delay.
774    int target_delay = startup_size_ms * self->rate_factor * 8 / 2;
775#endif
776    int overhead_elements =
777        (WebRtcAec_system_delay(self->aec) - target_delay) / PART_LEN;
778    WebRtcAec_MoveFarReadPtr(self->aec, overhead_elements);
779    self->startup_phase = 0;
780  }
781
782  EstBufDelayExtended(self);
783
784  {
785    // |delay_diff_offset| gives us the option to manually rewind the delay on
786    // very low delay platforms which can't be expressed purely through
787    // |reported_delay_ms|.
788    const int adjusted_known_delay =
789        WEBRTC_SPL_MAX(0, self->knownDelay + delay_diff_offset);
790
791    WebRtcAec_ProcessFrames(self->aec,
792                            near,
793                            num_bands,
794                            num_samples,
795                            adjusted_known_delay,
796                            out);
797  }
798}
799
800static void EstBufDelayNormal(Aec* aecpc) {
801  int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor;
802  int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->aec);
803  int delay_difference = 0;
804
805  // Before we proceed with the delay estimate filtering we:
806  // 1) Compensate for the frame that will be read.
807  // 2) Compensate for drift resampling.
808  // 3) Compensate for non-causality if needed, since the estimated delay can't
809  //    be negative.
810
811  // 1) Compensating for the frame(s) that will be read/processed.
812  current_delay += FRAME_LEN * aecpc->rate_factor;
813
814  // 2) Account for resampling frame delay.
815  if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) {
816    current_delay -= kResamplingDelay;
817  }
818
819  // 3) Compensate for non-causality, if needed, by flushing one block.
820  if (current_delay < PART_LEN) {
821    current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN;
822  }
823
824  // We use -1 to signal an initialized state in the "extended" implementation;
825  // compensate for that.
826  aecpc->filtDelay = aecpc->filtDelay < 0 ? 0 : aecpc->filtDelay;
827  aecpc->filtDelay =
828      WEBRTC_SPL_MAX(0, (short)(0.8 * aecpc->filtDelay + 0.2 * current_delay));
829
830  delay_difference = aecpc->filtDelay - aecpc->knownDelay;
831  if (delay_difference > 224) {
832    if (aecpc->lastDelayDiff < 96) {
833      aecpc->timeForDelayChange = 0;
834    } else {
835      aecpc->timeForDelayChange++;
836    }
837  } else if (delay_difference < 96 && aecpc->knownDelay > 0) {
838    if (aecpc->lastDelayDiff > 224) {
839      aecpc->timeForDelayChange = 0;
840    } else {
841      aecpc->timeForDelayChange++;
842    }
843  } else {
844    aecpc->timeForDelayChange = 0;
845  }
846  aecpc->lastDelayDiff = delay_difference;
847
848  if (aecpc->timeForDelayChange > 25) {
849    aecpc->knownDelay = WEBRTC_SPL_MAX((int)aecpc->filtDelay - 160, 0);
850  }
851}
852
853static void EstBufDelayExtended(Aec* self) {
854  int reported_delay = self->msInSndCardBuf * sampMsNb * self->rate_factor;
855  int current_delay = reported_delay - WebRtcAec_system_delay(self->aec);
856  int delay_difference = 0;
857
858  // Before we proceed with the delay estimate filtering we:
859  // 1) Compensate for the frame that will be read.
860  // 2) Compensate for drift resampling.
861  // 3) Compensate for non-causality if needed, since the estimated delay can't
862  //    be negative.
863
864  // 1) Compensating for the frame(s) that will be read/processed.
865  current_delay += FRAME_LEN * self->rate_factor;
866
867  // 2) Account for resampling frame delay.
868  if (self->skewMode == kAecTrue && self->resample == kAecTrue) {
869    current_delay -= kResamplingDelay;
870  }
871
872  // 3) Compensate for non-causality, if needed, by flushing two blocks.
873  if (current_delay < PART_LEN) {
874    current_delay += WebRtcAec_MoveFarReadPtr(self->aec, 2) * PART_LEN;
875  }
876
877  if (self->filtDelay == -1) {
878    self->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay);
879  } else {
880    self->filtDelay = WEBRTC_SPL_MAX(
881        0, (short)(0.95 * self->filtDelay + 0.05 * current_delay));
882  }
883
884  delay_difference = self->filtDelay - self->knownDelay;
885  if (delay_difference > 384) {
886    if (self->lastDelayDiff < 128) {
887      self->timeForDelayChange = 0;
888    } else {
889      self->timeForDelayChange++;
890    }
891  } else if (delay_difference < 128 && self->knownDelay > 0) {
892    if (self->lastDelayDiff > 384) {
893      self->timeForDelayChange = 0;
894    } else {
895      self->timeForDelayChange++;
896    }
897  } else {
898    self->timeForDelayChange = 0;
899  }
900  self->lastDelayDiff = delay_difference;
901
902  if (self->timeForDelayChange > 25) {
903    self->knownDelay = WEBRTC_SPL_MAX((int)self->filtDelay - 256, 0);
904  }
905}
906