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