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