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