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