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 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | // Maximum length of resampled signal. Must be an integer multiple of frames |
| 31 | // (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN |
| 32 | // 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] | 33 | // TODO(bjornv): Replace with kResamplerBufferSize |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | #define MAX_RESAMP_LEN (5 * FRAME_LEN) |
| 35 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 36 | static const int kMaxBufSizeStart = 62; // In partitions |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | static const int sampMsNb = 8; // samples per ms in nb |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | static const int initCheck = 42; |
| 39 | |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 40 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
bjornv@webrtc.org | 7267ffd | 2013-02-14 17:56:23 +0000 | [diff] [blame] | 41 | int webrtc_aec_instance_count = 0; |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 44 | // 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] | 45 | // (controlled by knownDelay) |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 46 | static int EstBufDelay(aecpc_t *aecInst); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | |
| 48 | WebRtc_Word32 WebRtcAec_Create(void **aecInst) |
| 49 | { |
| 50 | aecpc_t *aecpc; |
| 51 | if (aecInst == NULL) { |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | aecpc = malloc(sizeof(aecpc_t)); |
| 56 | *aecInst = aecpc; |
| 57 | if (aecpc == NULL) { |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | if (WebRtcAec_CreateAec(&aecpc->aec) == -1) { |
| 62 | WebRtcAec_Free(aecpc); |
| 63 | aecpc = NULL; |
| 64 | return -1; |
| 65 | } |
| 66 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 67 | if (WebRtcAec_CreateResampler(&aecpc->resampler) == -1) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | WebRtcAec_Free(aecpc); |
| 69 | aecpc = NULL; |
| 70 | return -1; |
| 71 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 72 | // Create far-end pre-buffer. The buffer size has to be large enough for |
| 73 | // largest possible drift compensation (kResamplerBufferSize) + "almost" an |
| 74 | // FFT buffer (PART_LEN2 - 1). |
andrew@webrtc.org | 91f3255 | 2013-02-27 00:35:06 +0000 | [diff] [blame] | 75 | aecpc->far_pre_buf = WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize, |
| 76 | sizeof(float)); |
| 77 | if (!aecpc->far_pre_buf) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | WebRtcAec_Free(aecpc); |
| 79 | aecpc = NULL; |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | aecpc->initFlag = 0; |
| 84 | aecpc->lastError = 0; |
| 85 | |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 86 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
andrew@webrtc.org | 91f3255 | 2013-02-27 00:35:06 +0000 | [diff] [blame] | 87 | aecpc->far_pre_buf_s16 = WebRtc_CreateBuffer( |
andrew@webrtc.org | 52b57cc | 2013-03-07 00:45:50 +0000 | [diff] [blame^] | 88 | PART_LEN2 + kResamplerBufferSize, sizeof(int16_t)); |
andrew@webrtc.org | 91f3255 | 2013-02-27 00:35:06 +0000 | [diff] [blame] | 89 | if (!aecpc->far_pre_buf_s16) { |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 90 | WebRtcAec_Free(aecpc); |
| 91 | aecpc = NULL; |
| 92 | return -1; |
| 93 | } |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 94 | { |
| 95 | char filename[64]; |
bjornv@webrtc.org | 7267ffd | 2013-02-14 17:56:23 +0000 | [diff] [blame] | 96 | sprintf(filename, "aec_buf%d.dat", webrtc_aec_instance_count); |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 97 | aecpc->bufFile = fopen(filename, "wb"); |
bjornv@webrtc.org | 7267ffd | 2013-02-14 17:56:23 +0000 | [diff] [blame] | 98 | sprintf(filename, "aec_skew%d.dat", webrtc_aec_instance_count); |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 99 | aecpc->skewFile = fopen(filename, "wb"); |
bjornv@webrtc.org | 7267ffd | 2013-02-14 17:56:23 +0000 | [diff] [blame] | 100 | sprintf(filename, "aec_delay%d.dat", webrtc_aec_instance_count); |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 101 | aecpc->delayFile = fopen(filename, "wb"); |
bjornv@webrtc.org | 7267ffd | 2013-02-14 17:56:23 +0000 | [diff] [blame] | 102 | webrtc_aec_instance_count++; |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 103 | } |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 104 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | WebRtc_Word32 WebRtcAec_Free(void *aecInst) |
| 110 | { |
| 111 | aecpc_t *aecpc = aecInst; |
| 112 | |
| 113 | if (aecpc == NULL) { |
| 114 | return -1; |
| 115 | } |
| 116 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 117 | WebRtc_FreeBuffer(aecpc->far_pre_buf); |
| 118 | |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 119 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 120 | WebRtc_FreeBuffer(aecpc->far_pre_buf_s16); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | fclose(aecpc->bufFile); |
| 122 | fclose(aecpc->skewFile); |
| 123 | fclose(aecpc->delayFile); |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 124 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | |
| 126 | WebRtcAec_FreeAec(aecpc->aec); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 127 | WebRtcAec_FreeResampler(aecpc->resampler); |
| 128 | free(aecpc); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | WebRtc_Word32 WebRtcAec_Init(void *aecInst, WebRtc_Word32 sampFreq, WebRtc_Word32 scSampFreq) |
| 134 | { |
| 135 | aecpc_t *aecpc = aecInst; |
| 136 | AecConfig aecConfig; |
| 137 | |
| 138 | if (aecpc == NULL) { |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | if (sampFreq != 8000 && sampFreq != 16000 && sampFreq != 32000) { |
| 143 | aecpc->lastError = AEC_BAD_PARAMETER_ERROR; |
| 144 | return -1; |
| 145 | } |
| 146 | aecpc->sampFreq = sampFreq; |
| 147 | |
| 148 | if (scSampFreq < 1 || scSampFreq > 96000) { |
| 149 | aecpc->lastError = AEC_BAD_PARAMETER_ERROR; |
| 150 | return -1; |
| 151 | } |
| 152 | aecpc->scSampFreq = scSampFreq; |
| 153 | |
| 154 | // Initialize echo canceller core |
| 155 | if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) { |
| 156 | aecpc->lastError = AEC_UNSPECIFIED_ERROR; |
| 157 | return -1; |
| 158 | } |
| 159 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) { |
| 161 | aecpc->lastError = AEC_UNSPECIFIED_ERROR; |
| 162 | return -1; |
| 163 | } |
| 164 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 165 | if (WebRtc_InitBuffer(aecpc->far_pre_buf) == -1) { |
| 166 | aecpc->lastError = AEC_UNSPECIFIED_ERROR; |
| 167 | return -1; |
| 168 | } |
| 169 | WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap. |
| 170 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 171 | aecpc->initFlag = initCheck; // indicates that initialization has been done |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | |
| 173 | if (aecpc->sampFreq == 32000) { |
| 174 | aecpc->splitSampFreq = 16000; |
| 175 | } |
| 176 | else { |
| 177 | aecpc->splitSampFreq = sampFreq; |
| 178 | } |
| 179 | |
| 180 | aecpc->skewFrCtr = 0; |
| 181 | aecpc->activity = 0; |
| 182 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 183 | aecpc->delayCtr = 0; |
| 184 | |
| 185 | aecpc->sum = 0; |
| 186 | aecpc->counter = 0; |
| 187 | aecpc->checkBuffSize = 1; |
| 188 | aecpc->firstVal = 0; |
| 189 | |
| 190 | aecpc->ECstartup = 1; |
| 191 | aecpc->bufSizeStart = 0; |
| 192 | aecpc->checkBufSizeCtr = 0; |
| 193 | aecpc->filtDelay = 0; |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 194 | aecpc->timeForDelayChange = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 195 | aecpc->knownDelay = 0; |
| 196 | aecpc->lastDelayDiff = 0; |
| 197 | |
| 198 | aecpc->skew = 0; |
| 199 | aecpc->resample = kAecFalse; |
| 200 | aecpc->highSkewCtr = 0; |
| 201 | aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq; |
| 202 | |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 203 | // Sampling frequency multiplier (SWB is processed as 160 frame size). |
| 204 | aecpc->rate_factor = aecpc->splitSampFreq / 8000; |
| 205 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 206 | // Default settings. |
| 207 | aecConfig.nlpMode = kAecNlpModerate; |
| 208 | aecConfig.skewMode = kAecFalse; |
| 209 | aecConfig.metricsMode = kAecFalse; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 210 | aecConfig.delay_logging = kAecFalse; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 211 | |
| 212 | if (WebRtcAec_set_config(aecpc, aecConfig) == -1) { |
| 213 | aecpc->lastError = AEC_UNSPECIFIED_ERROR; |
| 214 | return -1; |
| 215 | } |
| 216 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 217 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
| 218 | if (WebRtc_InitBuffer(aecpc->far_pre_buf_s16) == -1) { |
| 219 | aecpc->lastError = AEC_UNSPECIFIED_ERROR; |
| 220 | return -1; |
| 221 | } |
| 222 | WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN); // Start overlap. |
| 223 | #endif |
| 224 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | // only buffer L band for farend |
| 229 | WebRtc_Word32 WebRtcAec_BufferFarend(void *aecInst, const WebRtc_Word16 *farend, |
| 230 | WebRtc_Word16 nrOfSamples) |
| 231 | { |
| 232 | aecpc_t *aecpc = aecInst; |
| 233 | WebRtc_Word32 retVal = 0; |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 234 | int newNrOfSamples = (int) nrOfSamples; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 235 | short newFarend[MAX_RESAMP_LEN]; |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 236 | const int16_t* farend_ptr = farend; |
| 237 | float tmp_farend[MAX_RESAMP_LEN]; |
| 238 | const float* farend_float = tmp_farend; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 239 | float skew; |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 240 | int i = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | |
| 242 | if (aecpc == NULL) { |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | if (farend == NULL) { |
| 247 | aecpc->lastError = AEC_NULL_POINTER_ERROR; |
| 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | if (aecpc->initFlag != initCheck) { |
| 252 | aecpc->lastError = AEC_UNINITIALIZED_ERROR; |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | // number of samples == 160 for SWB input |
| 257 | if (nrOfSamples != 80 && nrOfSamples != 160) { |
| 258 | aecpc->lastError = AEC_BAD_PARAMETER_ERROR; |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | skew = aecpc->skew; |
| 263 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 264 | if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) { |
| 265 | // Resample and get a new number of samples |
bjornv@webrtc.org | 281b798 | 2012-05-30 07:41:57 +0000 | [diff] [blame] | 266 | WebRtcAec_ResampleLinear(aecpc->resampler, farend, nrOfSamples, skew, |
| 267 | newFarend, &newNrOfSamples); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 268 | farend_ptr = (const int16_t*) newFarend; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 269 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 270 | |
bjornv@webrtc.org | 4d1cfae | 2013-02-20 17:31:38 +0000 | [diff] [blame] | 271 | WebRtcAec_SetSystemDelay(aecpc->aec, WebRtcAec_system_delay(aecpc->aec) + |
| 272 | newNrOfSamples); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 273 | |
| 274 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
| 275 | WebRtc_WriteBuffer(aecpc->far_pre_buf_s16, farend_ptr, |
| 276 | (size_t) newNrOfSamples); |
| 277 | #endif |
| 278 | // Cast to float and write the time-domain data to |far_pre_buf|. |
| 279 | for (i = 0; i < newNrOfSamples; i++) { |
| 280 | tmp_farend[i] = (float) farend_ptr[i]; |
| 281 | } |
| 282 | WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_float, |
| 283 | (size_t) newNrOfSamples); |
| 284 | |
| 285 | // Transform to frequency domain if we have enough data. |
| 286 | while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) { |
| 287 | // We have enough data to pass to the FFT, hence read PART_LEN2 samples. |
| 288 | WebRtc_ReadBuffer(aecpc->far_pre_buf, (void**) &farend_float, tmp_farend, |
| 289 | PART_LEN2); |
| 290 | |
| 291 | WebRtcAec_BufferFarendPartition(aecpc->aec, farend_float); |
| 292 | |
| 293 | // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing. |
| 294 | WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); |
| 295 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
| 296 | WebRtc_ReadBuffer(aecpc->far_pre_buf_s16, (void**) &farend_ptr, newFarend, |
| 297 | PART_LEN2); |
bjornv@webrtc.org | 0a480cb | 2013-02-19 21:41:27 +0000 | [diff] [blame] | 298 | WebRtc_WriteBuffer(WebRtcAec_far_time_buf(aecpc->aec), |
| 299 | &farend_ptr[PART_LEN], 1); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 300 | WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN); |
| 301 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | return retVal; |
| 305 | } |
| 306 | |
| 307 | WebRtc_Word32 WebRtcAec_Process(void *aecInst, const WebRtc_Word16 *nearend, |
| 308 | const WebRtc_Word16 *nearendH, WebRtc_Word16 *out, WebRtc_Word16 *outH, |
| 309 | WebRtc_Word16 nrOfSamples, WebRtc_Word16 msInSndCardBuf, WebRtc_Word32 skew) |
| 310 | { |
| 311 | aecpc_t *aecpc = aecInst; |
| 312 | WebRtc_Word32 retVal = 0; |
| 313 | short i; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 314 | short nBlocks10ms; |
| 315 | short nFrames; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 316 | // Limit resampling to doubling/halving of signal |
| 317 | const float minSkewEst = -0.5f; |
| 318 | const float maxSkewEst = 1.0f; |
| 319 | |
| 320 | if (aecpc == NULL) { |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | if (nearend == NULL) { |
| 325 | aecpc->lastError = AEC_NULL_POINTER_ERROR; |
| 326 | return -1; |
| 327 | } |
| 328 | |
| 329 | if (out == NULL) { |
| 330 | aecpc->lastError = AEC_NULL_POINTER_ERROR; |
| 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | if (aecpc->initFlag != initCheck) { |
| 335 | aecpc->lastError = AEC_UNINITIALIZED_ERROR; |
| 336 | return -1; |
| 337 | } |
| 338 | |
| 339 | // number of samples == 160 for SWB input |
| 340 | if (nrOfSamples != 80 && nrOfSamples != 160) { |
| 341 | aecpc->lastError = AEC_BAD_PARAMETER_ERROR; |
| 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | // Check for valid pointers based on sampling rate |
| 346 | if (aecpc->sampFreq == 32000 && nearendH == NULL) { |
| 347 | aecpc->lastError = AEC_NULL_POINTER_ERROR; |
| 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | if (msInSndCardBuf < 0) { |
| 352 | msInSndCardBuf = 0; |
| 353 | aecpc->lastError = AEC_BAD_PARAMETER_WARNING; |
| 354 | retVal = -1; |
| 355 | } |
| 356 | else if (msInSndCardBuf > 500) { |
| 357 | msInSndCardBuf = 500; |
| 358 | aecpc->lastError = AEC_BAD_PARAMETER_WARNING; |
| 359 | retVal = -1; |
| 360 | } |
andrew@webrtc.org | a919d3a | 2011-11-27 23:40:58 +0000 | [diff] [blame] | 361 | // TODO(andrew): we need to investigate if this +10 is really wanted. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 362 | msInSndCardBuf += 10; |
| 363 | aecpc->msInSndCardBuf = msInSndCardBuf; |
| 364 | |
| 365 | if (aecpc->skewMode == kAecTrue) { |
| 366 | if (aecpc->skewFrCtr < 25) { |
| 367 | aecpc->skewFrCtr++; |
| 368 | } |
| 369 | else { |
| 370 | retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew); |
| 371 | if (retVal == -1) { |
| 372 | aecpc->skew = 0; |
| 373 | aecpc->lastError = AEC_BAD_PARAMETER_WARNING; |
| 374 | } |
| 375 | |
| 376 | aecpc->skew /= aecpc->sampFactor*nrOfSamples; |
| 377 | |
| 378 | if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) { |
| 379 | aecpc->resample = kAecFalse; |
| 380 | } |
| 381 | else { |
| 382 | aecpc->resample = kAecTrue; |
| 383 | } |
| 384 | |
| 385 | if (aecpc->skew < minSkewEst) { |
| 386 | aecpc->skew = minSkewEst; |
| 387 | } |
| 388 | else if (aecpc->skew > maxSkewEst) { |
| 389 | aecpc->skew = maxSkewEst; |
| 390 | } |
| 391 | |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 392 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
andrew@webrtc.org | 7d8c567 | 2012-06-01 02:41:14 +0000 | [diff] [blame] | 393 | (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 394 | #endif |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | nFrames = nrOfSamples / FRAME_LEN; |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 399 | nBlocks10ms = nFrames / aecpc->rate_factor; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 400 | |
| 401 | if (aecpc->ECstartup) { |
andrew@webrtc.org | 6423509 | 2011-08-19 21:22:08 +0000 | [diff] [blame] | 402 | if (nearend != out) { |
| 403 | // Only needed if they don't already point to the same place. |
| 404 | memcpy(out, nearend, sizeof(short) * nrOfSamples); |
| 405 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 406 | |
| 407 | // The AEC is in the start up mode |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 408 | // AEC is disabled until the system delay is OK |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 409 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 410 | // Mechanism to ensure that the system delay is reasonably stable. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 411 | if (aecpc->checkBuffSize) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 412 | aecpc->checkBufSizeCtr++; |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 413 | // Before we fill up the far-end buffer we require the system delay |
| 414 | // to be stable (+/-8 ms) compared to the first value. This |
| 415 | // comparison is made during the following 6 consecutive 10 ms |
| 416 | // blocks. If it seems to be stable then we start to fill up the |
| 417 | // far-end buffer. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 418 | if (aecpc->counter == 0) { |
| 419 | aecpc->firstVal = aecpc->msInSndCardBuf; |
| 420 | aecpc->sum = 0; |
| 421 | } |
| 422 | |
| 423 | if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) < |
| 424 | WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) { |
| 425 | aecpc->sum += aecpc->msInSndCardBuf; |
| 426 | aecpc->counter++; |
| 427 | } |
| 428 | else { |
| 429 | aecpc->counter = 0; |
| 430 | } |
| 431 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 432 | if (aecpc->counter * nBlocks10ms >= 6) { |
| 433 | // The far-end buffer size is determined in partitions of |
| 434 | // PART_LEN samples. Use 75% of the average value of the system |
| 435 | // delay as buffer size to start with. |
| 436 | aecpc->bufSizeStart = WEBRTC_SPL_MIN((3 * aecpc->sum * |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 437 | aecpc->rate_factor * 8) / (4 * aecpc->counter * PART_LEN), |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 438 | kMaxBufSizeStart); |
| 439 | // Buffer size has now been determined. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 440 | aecpc->checkBuffSize = 0; |
| 441 | } |
| 442 | |
| 443 | if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) { |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 444 | // For really bad systems, don't disable the echo canceller for |
| 445 | // more than 0.5 sec. |
| 446 | aecpc->bufSizeStart = WEBRTC_SPL_MIN((aecpc->msInSndCardBuf * |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 447 | aecpc->rate_factor * 3) / 40, kMaxBufSizeStart); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 448 | aecpc->checkBuffSize = 0; |
| 449 | } |
| 450 | } |
| 451 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 452 | // If |checkBuffSize| changed in the if-statement above. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 453 | if (!aecpc->checkBuffSize) { |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 454 | // The system delay is now reasonably stable (or has been unstable |
| 455 | // for too long). When the far-end buffer is filled with |
| 456 | // approximately the same amount of data as reported by the system |
| 457 | // we end the startup phase. |
bjornv@webrtc.org | 4d1cfae | 2013-02-20 17:31:38 +0000 | [diff] [blame] | 458 | int overhead_elements = |
| 459 | WebRtcAec_system_delay(aecpc->aec) / PART_LEN - |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 460 | aecpc->bufSizeStart; |
| 461 | if (overhead_elements == 0) { |
| 462 | // Enable the AEC |
| 463 | aecpc->ECstartup = 0; |
| 464 | } else if (overhead_elements > 0) { |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 465 | // TODO(bjornv): Do we need a check on how much we actually |
| 466 | // moved the read pointer? It should always be possible to move |
| 467 | // the pointer |overhead_elements| since we have only added data |
| 468 | // to the buffer and no delay compensation nor AEC processing |
| 469 | // has been done. |
andrew@webrtc.org | 61bf8e3 | 2012-03-15 19:04:55 +0000 | [diff] [blame] | 470 | WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 471 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 472 | // Enable the AEC |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 473 | aecpc->ECstartup = 0; |
| 474 | } |
| 475 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 476 | } else { |
| 477 | // AEC is enabled. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 478 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 479 | EstBufDelay(aecpc); |
| 480 | |
| 481 | // Note that 1 frame is supported for NB and 2 frames for WB. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 482 | for (i = 0; i < nFrames; i++) { |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 483 | // Call the AEC. |
| 484 | WebRtcAec_ProcessFrame(aecpc->aec, |
| 485 | &nearend[FRAME_LEN * i], |
| 486 | &nearendH[FRAME_LEN * i], |
bjornv@webrtc.org | 716fd90 | 2013-02-20 16:59:41 +0000 | [diff] [blame] | 487 | aecpc->knownDelay, |
| 488 | &out[FRAME_LEN * i], |
| 489 | &outH[FRAME_LEN * i]); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 490 | // TODO(bjornv): Re-structure such that we don't have to pass |
| 491 | // |aecpc->knownDelay| as input. Change name to something like |
| 492 | // |system_buffer_diff|. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 496 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
| 497 | { |
bjornv@webrtc.org | 4d1cfae | 2013-02-20 17:31:38 +0000 | [diff] [blame] | 498 | 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] | 499 | (sampMsNb * aecpc->rate_factor)); |
andrew@webrtc.org | 7d8c567 | 2012-06-01 02:41:14 +0000 | [diff] [blame] | 500 | (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile); |
| 501 | (void)fwrite(&aecpc->knownDelay, sizeof(aecpc->knownDelay), 1, |
| 502 | aecpc->delayFile); |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 503 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 504 | #endif |
| 505 | |
| 506 | return retVal; |
| 507 | } |
| 508 | |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame] | 509 | int WebRtcAec_set_config(void* handle, AecConfig config) { |
| 510 | aecpc_t* self = (aecpc_t*)handle; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 511 | |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame] | 512 | if (handle == NULL ) { |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 513 | return -1; |
| 514 | } |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame] | 515 | |
| 516 | if (self->initFlag != initCheck) { |
| 517 | self->lastError = AEC_UNINITIALIZED_ERROR; |
| 518 | return -1; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 519 | } |
| 520 | |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame] | 521 | if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) { |
| 522 | self->lastError = AEC_BAD_PARAMETER_ERROR; |
| 523 | return -1; |
| 524 | } |
| 525 | self->skewMode = config.skewMode; |
| 526 | |
| 527 | if (config.nlpMode != kAecNlpConservative && config.nlpMode != kAecNlpModerate |
| 528 | && config.nlpMode != kAecNlpAggressive) { |
| 529 | self->lastError = AEC_BAD_PARAMETER_ERROR; |
| 530 | return -1; |
| 531 | } |
| 532 | |
| 533 | if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) { |
| 534 | self->lastError = AEC_BAD_PARAMETER_ERROR; |
| 535 | return -1; |
| 536 | } |
| 537 | |
| 538 | if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) { |
| 539 | self->lastError = AEC_BAD_PARAMETER_ERROR; |
| 540 | return -1; |
| 541 | } |
| 542 | |
| 543 | WebRtcAec_SetConfigCore(self->aec, config.nlpMode, config.metricsMode, |
| 544 | config.delay_logging); |
| 545 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 546 | } |
| 547 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 548 | int WebRtcAec_get_echo_status(void* handle, int* status) { |
| 549 | aecpc_t* self = (aecpc_t*)handle; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 550 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 551 | if (handle == NULL ) { |
| 552 | return -1; |
| 553 | } |
| 554 | if (status == NULL ) { |
| 555 | self->lastError = AEC_NULL_POINTER_ERROR; |
| 556 | return -1; |
| 557 | } |
| 558 | if (self->initFlag != initCheck) { |
| 559 | self->lastError = AEC_UNINITIALIZED_ERROR; |
| 560 | return -1; |
| 561 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 562 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 563 | *status = WebRtcAec_echo_state(self->aec); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 564 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 565 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 566 | } |
| 567 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 568 | int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) { |
| 569 | const float kUpWeight = 0.7f; |
| 570 | float dtmp; |
| 571 | int stmp; |
| 572 | aecpc_t* self = (aecpc_t*)handle; |
bjornv@webrtc.org | cea70f4 | 2013-02-19 21:03:10 +0000 | [diff] [blame] | 573 | Stats erl; |
| 574 | Stats erle; |
| 575 | Stats a_nlp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 576 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 577 | if (handle == NULL ) { |
| 578 | return -1; |
| 579 | } |
| 580 | if (metrics == NULL ) { |
| 581 | self->lastError = AEC_NULL_POINTER_ERROR; |
| 582 | return -1; |
| 583 | } |
| 584 | if (self->initFlag != initCheck) { |
| 585 | self->lastError = AEC_UNINITIALIZED_ERROR; |
| 586 | return -1; |
| 587 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 588 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 589 | WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 590 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 591 | // ERL |
| 592 | metrics->erl.instant = (int) erl.instant; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 593 | |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 594 | if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) { |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 595 | // Use a mix between regular average and upper part average. |
| 596 | dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average; |
| 597 | metrics->erl.average = (int) dtmp; |
| 598 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 599 | metrics->erl.average = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 600 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 601 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 602 | metrics->erl.max = (int) erl.max; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 603 | |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 604 | if (erl.min < (kOffsetLevel * (-1))) { |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 605 | metrics->erl.min = (int) erl.min; |
| 606 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 607 | metrics->erl.min = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 608 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 609 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 610 | // ERLE |
| 611 | metrics->erle.instant = (int) erle.instant; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 612 | |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 613 | if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) { |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 614 | // Use a mix between regular average and upper part average. |
| 615 | dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average; |
| 616 | metrics->erle.average = (int) dtmp; |
| 617 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 618 | metrics->erle.average = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 619 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 620 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 621 | metrics->erle.max = (int) erle.max; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 622 | |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 623 | if (erle.min < (kOffsetLevel * (-1))) { |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 624 | metrics->erle.min = (int) erle.min; |
| 625 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 626 | metrics->erle.min = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 627 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 628 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 629 | // RERL |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 630 | if ((metrics->erl.average > kOffsetLevel) |
| 631 | && (metrics->erle.average > kOffsetLevel)) { |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 632 | stmp = metrics->erl.average + metrics->erle.average; |
| 633 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 634 | stmp = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 635 | } |
| 636 | metrics->rerl.average = stmp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 637 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 638 | // No other statistics needed, but returned for completeness. |
| 639 | metrics->rerl.instant = stmp; |
| 640 | metrics->rerl.max = stmp; |
| 641 | metrics->rerl.min = stmp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 642 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 643 | // A_NLP |
| 644 | metrics->aNlp.instant = (int) a_nlp.instant; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 645 | |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 646 | if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) { |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 647 | // Use a mix between regular average and upper part average. |
| 648 | dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average; |
| 649 | metrics->aNlp.average = (int) dtmp; |
| 650 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 651 | metrics->aNlp.average = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 652 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 653 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 654 | metrics->aNlp.max = (int) a_nlp.max; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 655 | |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 656 | if (a_nlp.min < (kOffsetLevel * (-1))) { |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 657 | metrics->aNlp.min = (int) a_nlp.min; |
| 658 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 659 | metrics->aNlp.min = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 660 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 661 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 662 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 663 | } |
| 664 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 665 | int WebRtcAec_GetDelayMetrics(void* handle, int* median, int* std) { |
| 666 | aecpc_t* self = handle; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 667 | |
bjornv@webrtc.org | 61d0745 | 2012-05-11 07:51:44 +0000 | [diff] [blame] | 668 | if (handle == NULL) { |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 669 | return -1; |
| 670 | } |
| 671 | if (median == NULL) { |
| 672 | self->lastError = AEC_NULL_POINTER_ERROR; |
| 673 | return -1; |
| 674 | } |
| 675 | if (std == NULL) { |
| 676 | self->lastError = AEC_NULL_POINTER_ERROR; |
| 677 | return -1; |
| 678 | } |
| 679 | if (self->initFlag != initCheck) { |
| 680 | self->lastError = AEC_UNINITIALIZED_ERROR; |
| 681 | return -1; |
| 682 | } |
bjornv@webrtc.org | 325f625 | 2013-02-15 15:21:02 +0000 | [diff] [blame] | 683 | if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std) == -1) { |
| 684 | // Logging disabled. |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 685 | self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR; |
| 686 | return -1; |
| 687 | } |
| 688 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 692 | WebRtc_Word32 WebRtcAec_get_error_code(void *aecInst) |
| 693 | { |
| 694 | aecpc_t *aecpc = aecInst; |
| 695 | |
| 696 | if (aecpc == NULL) { |
| 697 | return -1; |
| 698 | } |
| 699 | |
| 700 | return aecpc->lastError; |
| 701 | } |
| 702 | |
bjornv@webrtc.org | 132c15d | 2013-02-27 21:03:41 +0000 | [diff] [blame] | 703 | AecCore* WebRtcAec_aec_core(void* handle) { |
| 704 | if (!handle) { |
| 705 | return NULL; |
| 706 | } |
| 707 | return ((aecpc_t*) handle)->aec; |
| 708 | } |
| 709 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 710 | static int EstBufDelay(aecpc_t* aecpc) { |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 711 | int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor; |
bjornv@webrtc.org | 4d1cfae | 2013-02-20 17:31:38 +0000 | [diff] [blame] | 712 | int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->aec); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 713 | int delay_difference = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 714 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 715 | // Before we proceed with the delay estimate filtering we: |
| 716 | // 1) Compensate for the frame that will be read. |
| 717 | // 2) Compensate for drift resampling. |
bjornv@webrtc.org | 7056908 | 2012-04-12 12:13:50 +0000 | [diff] [blame] | 718 | // 3) Compensate for non-causality if needed, since the estimated delay can't |
| 719 | // be negative. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 720 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 721 | // 1) Compensating for the frame(s) that will be read/processed. |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 722 | current_delay += FRAME_LEN * aecpc->rate_factor; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 723 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 724 | // 2) Account for resampling frame delay. |
| 725 | if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) { |
| 726 | current_delay -= kResamplingDelay; |
| 727 | } |
| 728 | |
bjornv@webrtc.org | 7056908 | 2012-04-12 12:13:50 +0000 | [diff] [blame] | 729 | // 3) Compensate for non-causality, if needed, by flushing one block. |
andrew@webrtc.org | 61bf8e3 | 2012-03-15 19:04:55 +0000 | [diff] [blame] | 730 | if (current_delay < PART_LEN) { |
| 731 | current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN; |
| 732 | } |
| 733 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 734 | aecpc->filtDelay = WEBRTC_SPL_MAX(0, (short) (0.8 * aecpc->filtDelay + |
| 735 | 0.2 * current_delay)); |
| 736 | |
| 737 | delay_difference = aecpc->filtDelay - aecpc->knownDelay; |
| 738 | if (delay_difference > 224) { |
| 739 | if (aecpc->lastDelayDiff < 96) { |
| 740 | aecpc->timeForDelayChange = 0; |
| 741 | } else { |
| 742 | aecpc->timeForDelayChange++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 743 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 744 | } else if (delay_difference < 96 && aecpc->knownDelay > 0) { |
| 745 | if (aecpc->lastDelayDiff > 224) { |
| 746 | aecpc->timeForDelayChange = 0; |
| 747 | } else { |
| 748 | aecpc->timeForDelayChange++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 749 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 750 | } else { |
| 751 | aecpc->timeForDelayChange = 0; |
| 752 | } |
| 753 | aecpc->lastDelayDiff = delay_difference; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 754 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 755 | if (aecpc->timeForDelayChange > 25) { |
| 756 | aecpc->knownDelay = WEBRTC_SPL_MAX((int) aecpc->filtDelay - 160, 0); |
| 757 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 758 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 759 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 760 | } |