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