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 | */ |
Henrik Kjellander | 9b72af9 | 2015-11-11 20:16:11 +0100 | [diff] [blame] | 14 | #include "webrtc/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" { |
peah | faed4ab | 2016-04-05 14:57:48 -0700 | [diff] [blame] | 21 | #include "webrtc/common_audio/ring_buffer.h" |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 22 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
peah | 8df5d4f | 2016-02-23 14:34:59 -0800 | [diff] [blame] | 23 | } |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 24 | #include "webrtc/modules/audio_processing/aec/aec_core.h" |
| 25 | #include "webrtc/modules/audio_processing/aec/aec_resampler.h" |
peah | 3f08dc6 | 2016-05-05 03:03:55 -0700 | [diff] [blame] | 26 | #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 27 | #include "webrtc/typedefs.h" |
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); |
| 108 | static int ProcessNormal(Aec* self, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 109 | const float* const* near, |
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); |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 115 | static void ProcessExtended(Aec* self, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 116 | const float* const* near, |
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 | |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 534 | static int ProcessNormal(Aec* aecpc, |
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, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 538 | size_t nrOfSamples, |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 539 | int16_t msInSndCardBuf, |
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 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 548 | msInSndCardBuf = |
| 549 | msInSndCardBuf > kMaxTrustedDelayMs ? kMaxTrustedDelayMs : msInSndCardBuf; |
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. |
| 551 | msInSndCardBuf += 10; |
| 552 | aecpc->msInSndCardBuf = msInSndCardBuf; |
| 553 | |
| 554 | if (aecpc->skewMode == kAecTrue) { |
| 555 | if (aecpc->skewFrCtr < 25) { |
| 556 | aecpc->skewFrCtr++; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 557 | } else { |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 558 | retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew); |
| 559 | if (retVal == -1) { |
| 560 | aecpc->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 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 564 | aecpc->skew /= aecpc->sampFactor * nrOfSamples; |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 565 | |
| 566 | if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) { |
| 567 | aecpc->resample = kAecFalse; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 568 | } else { |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 569 | aecpc->resample = kAecTrue; |
| 570 | } |
| 571 | |
| 572 | if (aecpc->skew < minSkewEst) { |
| 573 | aecpc->skew = minSkewEst; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 574 | } else if (aecpc->skew > maxSkewEst) { |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 575 | aecpc->skew = maxSkewEst; |
| 576 | } |
| 577 | |
peah | 3f08dc6 | 2016-05-05 03:03:55 -0700 | [diff] [blame] | 578 | aecpc->data_dumper->DumpRaw("aec_skew", 1, &aecpc->skew); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 582 | nBlocks10ms = nrOfSamples / (FRAME_LEN * aecpc->rate_factor); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 583 | |
| 584 | if (aecpc->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]) { |
| 588 | memcpy(out[i], nearend[i], sizeof(nearend[i][0]) * nrOfSamples); |
| 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. |
| 596 | if (aecpc->checkBuffSize) { |
| 597 | aecpc->checkBufSizeCtr++; |
| 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. |
| 603 | if (aecpc->counter == 0) { |
| 604 | aecpc->firstVal = aecpc->msInSndCardBuf; |
| 605 | aecpc->sum = 0; |
| 606 | } |
| 607 | |
| 608 | if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) < |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 609 | WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) { |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 610 | aecpc->sum += aecpc->msInSndCardBuf; |
| 611 | aecpc->counter++; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 612 | } else { |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 613 | aecpc->counter = 0; |
| 614 | } |
| 615 | |
| 616 | if (aecpc->counter * nBlocks10ms >= 6) { |
| 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. |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 620 | aecpc->bufSizeStart = |
| 621 | WEBRTC_SPL_MIN((3 * aecpc->sum * aecpc->rate_factor * 8) / |
| 622 | (4 * aecpc->counter * PART_LEN), |
| 623 | kMaxBufSizeStart); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 624 | // Buffer size has now been determined. |
| 625 | aecpc->checkBuffSize = 0; |
| 626 | } |
| 627 | |
| 628 | if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) { |
| 629 | // For really bad systems, don't disable the echo canceller for |
| 630 | // more than 0.5 sec. |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 631 | aecpc->bufSizeStart = WEBRTC_SPL_MIN( |
| 632 | (aecpc->msInSndCardBuf * aecpc->rate_factor * 3) / 40, |
| 633 | kMaxBufSizeStart); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 634 | aecpc->checkBuffSize = 0; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | // If |checkBuffSize| changed in the if-statement above. |
| 639 | if (!aecpc->checkBuffSize) { |
| 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. |
| 644 | int overhead_elements = |
| 645 | WebRtcAec_system_delay(aecpc->aec) / PART_LEN - aecpc->bufSizeStart; |
| 646 | if (overhead_elements == 0) { |
| 647 | // Enable the AEC |
| 648 | aecpc->startup_phase = 0; |
| 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. |
peah | a421ddd | 2016-09-12 11:27:14 -0700 | [diff] [blame] | 655 | WebRtcAec_AdjustFarendBufferSizeAndSystemDelay(aecpc->aec, |
| 656 | overhead_elements); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 657 | |
| 658 | // Enable the AEC |
| 659 | aecpc->startup_phase = 0; |
| 660 | } |
| 661 | } |
| 662 | } else { |
| 663 | // AEC is enabled. |
Bjorn Volcker | 7101269 | 2015-06-18 11:04:56 +0200 | [diff] [blame] | 664 | EstBufDelayNormal(aecpc); |
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 |
| 668 | // |aecpc->knownDelay| as input. Change name to something like |
| 669 | // |system_buffer_diff|. |
peah | ff63ed2 | 2016-01-29 07:46:13 -0800 | [diff] [blame] | 670 | WebRtcAec_ProcessFrames(aecpc->aec, nearend, num_bands, nrOfSamples, |
| 671 | aecpc->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, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 678 | const float* const* near, |
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. |
| 710 | if (near[i] != out[i]) { |
| 711 | memcpy(out[i], near[i], sizeof(near[i][0]) * num_samples); |
| 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 | |
peah | ff63ed2 | 2016-01-29 07:46:13 -0800 | [diff] [blame] | 747 | WebRtcAec_ProcessFrames(self->aec, near, num_bands, num_samples, |
| 748 | adjusted_known_delay, out); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 752 | static void EstBufDelayNormal(Aec* aecpc) { |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 753 | int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor; |
bjornv@webrtc.org | 4d1cfae | 2013-02-20 17:31:38 +0000 | [diff] [blame] | 754 | int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->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. |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 764 | current_delay += FRAME_LEN * aecpc->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. |
| 767 | if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) { |
| 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 += |
| 774 | WebRtcAec_AdjustFarendBufferSizeAndSystemDelay(aecpc->aec, 1) * |
| 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. |
| 780 | aecpc->filtDelay = aecpc->filtDelay < 0 ? 0 : aecpc->filtDelay; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 781 | aecpc->filtDelay = |
peah | 8df5d4f | 2016-02-23 14:34:59 -0800 | [diff] [blame] | 782 | WEBRTC_SPL_MAX(0, static_cast<int16_t>(0.8 * |
| 783 | aecpc->filtDelay + |
| 784 | 0.2 * current_delay)); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 785 | |
| 786 | delay_difference = aecpc->filtDelay - aecpc->knownDelay; |
| 787 | if (delay_difference > 224) { |
| 788 | if (aecpc->lastDelayDiff < 96) { |
| 789 | aecpc->timeForDelayChange = 0; |
| 790 | } else { |
| 791 | aecpc->timeForDelayChange++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 792 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 793 | } else if (delay_difference < 96 && aecpc->knownDelay > 0) { |
| 794 | if (aecpc->lastDelayDiff > 224) { |
| 795 | aecpc->timeForDelayChange = 0; |
| 796 | } else { |
| 797 | aecpc->timeForDelayChange++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 798 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 799 | } else { |
| 800 | aecpc->timeForDelayChange = 0; |
| 801 | } |
| 802 | aecpc->lastDelayDiff = delay_difference; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 803 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 804 | if (aecpc->timeForDelayChange > 25) { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 805 | aecpc->knownDelay = WEBRTC_SPL_MAX((int)aecpc->filtDelay - 160, 0); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 806 | } |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 807 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 808 | |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 809 | static void EstBufDelayExtended(Aec* self) { |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 810 | int reported_delay = self->msInSndCardBuf * sampMsNb * self->rate_factor; |
| 811 | int current_delay = reported_delay - WebRtcAec_system_delay(self->aec); |
| 812 | int delay_difference = 0; |
| 813 | |
| 814 | // Before we proceed with the delay estimate filtering we: |
| 815 | // 1) Compensate for the frame that will be read. |
| 816 | // 2) Compensate for drift resampling. |
| 817 | // 3) Compensate for non-causality if needed, since the estimated delay can't |
| 818 | // be negative. |
| 819 | |
| 820 | // 1) Compensating for the frame(s) that will be read/processed. |
| 821 | current_delay += FRAME_LEN * self->rate_factor; |
| 822 | |
| 823 | // 2) Account for resampling frame delay. |
| 824 | if (self->skewMode == kAecTrue && self->resample == kAecTrue) { |
| 825 | current_delay -= kResamplingDelay; |
| 826 | } |
| 827 | |
| 828 | // 3) Compensate for non-causality, if needed, by flushing two blocks. |
| 829 | if (current_delay < PART_LEN) { |
peah | a421ddd | 2016-09-12 11:27:14 -0700 | [diff] [blame] | 830 | current_delay += |
| 831 | WebRtcAec_AdjustFarendBufferSizeAndSystemDelay(self->aec, 2) * PART_LEN; |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | if (self->filtDelay == -1) { |
| 835 | self->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay); |
| 836 | } else { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 837 | self->filtDelay = WEBRTC_SPL_MAX( |
peah | 8df5d4f | 2016-02-23 14:34:59 -0800 | [diff] [blame] | 838 | 0, static_cast<int16_t>(0.95 * self->filtDelay + 0.05 * current_delay)); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | delay_difference = self->filtDelay - self->knownDelay; |
| 842 | if (delay_difference > 384) { |
| 843 | if (self->lastDelayDiff < 128) { |
| 844 | self->timeForDelayChange = 0; |
| 845 | } else { |
| 846 | self->timeForDelayChange++; |
| 847 | } |
| 848 | } else if (delay_difference < 128 && self->knownDelay > 0) { |
| 849 | if (self->lastDelayDiff > 384) { |
| 850 | self->timeForDelayChange = 0; |
| 851 | } else { |
| 852 | self->timeForDelayChange++; |
| 853 | } |
| 854 | } else { |
| 855 | self->timeForDelayChange = 0; |
| 856 | } |
| 857 | self->lastDelayDiff = delay_difference; |
| 858 | |
| 859 | if (self->timeForDelayChange > 25) { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 860 | self->knownDelay = WEBRTC_SPL_MAX((int)self->filtDelay - 256, 0); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 861 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 862 | } |
peah | 50e21bd | 2016-03-05 08:39:21 -0800 | [diff] [blame] | 863 | } // namespace webrtc |