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