| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h" | 11 #include "webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h" |
| 12 | 12 |
| 13 #include <assert.h> | |
| 14 #include <stdlib.h> | 13 #include <stdlib.h> |
| 15 #include <string.h> | 14 #include <string.h> |
| 16 | 15 |
| 16 #include "webrtc/base/checks.h" |
| 17 #include "webrtc/modules/audio_processing/utility/delay_estimator.h" | 17 #include "webrtc/modules/audio_processing/utility/delay_estimator.h" |
| 18 #include "webrtc/modules/audio_processing/utility/delay_estimator_internal.h" | 18 #include "webrtc/modules/audio_processing/utility/delay_estimator_internal.h" |
| 19 | 19 |
| 20 // Only bit |kBandFirst| through bit |kBandLast| are processed and | 20 // Only bit |kBandFirst| through bit |kBandLast| are processed and |
| 21 // |kBandFirst| - |kBandLast| must be < 32. | 21 // |kBandFirst| - |kBandLast| must be < 32. |
| 22 enum { kBandFirst = 12 }; | 22 enum { kBandFirst = 12 }; |
| 23 enum { kBandLast = 43 }; | 23 enum { kBandLast = 43 }; |
| 24 | 24 |
| 25 static __inline uint32_t SetBit(uint32_t in, int pos) { | 25 static __inline uint32_t SetBit(uint32_t in, int pos) { |
| 26 uint32_t mask = (1 << pos); | 26 uint32_t mask = (1 << pos); |
| 27 uint32_t out = (in | mask); | 27 uint32_t out = (in | mask); |
| 28 | 28 |
| 29 return out; | 29 return out; |
| 30 } | 30 } |
| 31 | 31 |
| 32 // Calculates the mean recursively. Same version as WebRtc_MeanEstimatorFix(), | 32 // Calculates the mean recursively. Same version as WebRtc_MeanEstimatorFix(), |
| 33 // but for float. | 33 // but for float. |
| 34 // | 34 // |
| 35 // Inputs: | 35 // Inputs: |
| 36 // - new_value : New additional value. | 36 // - new_value : New additional value. |
| 37 // - scale : Scale for smoothing (should be less than 1.0). | 37 // - scale : Scale for smoothing (should be less than 1.0). |
| 38 // | 38 // |
| 39 // Input/Output: | 39 // Input/Output: |
| 40 // - mean_value : Pointer to the mean value for updating. | 40 // - mean_value : Pointer to the mean value for updating. |
| 41 // | 41 // |
| 42 static void MeanEstimatorFloat(float new_value, | 42 static void MeanEstimatorFloat(float new_value, |
| 43 float scale, | 43 float scale, |
| 44 float* mean_value) { | 44 float* mean_value) { |
| 45 assert(scale < 1.0f); | 45 RTC_DCHECK_LT(scale, 1.0f); |
| 46 *mean_value += (new_value - *mean_value) * scale; | 46 *mean_value += (new_value - *mean_value) * scale; |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Computes the binary spectrum by comparing the input |spectrum| with a | 49 // Computes the binary spectrum by comparing the input |spectrum| with a |
| 50 // |threshold_spectrum|. Float and fixed point versions. | 50 // |threshold_spectrum|. Float and fixed point versions. |
| 51 // | 51 // |
| 52 // Inputs: | 52 // Inputs: |
| 53 // - spectrum : Spectrum of which the binary spectrum should be | 53 // - spectrum : Spectrum of which the binary spectrum should be |
| 54 // calculated. | 54 // calculated. |
| 55 // - threshold_spectrum : Threshold spectrum with which the input | 55 // - threshold_spectrum : Threshold spectrum with which the input |
| 56 // spectrum is compared. | 56 // spectrum is compared. |
| 57 // Return: | 57 // Return: |
| 58 // - out : Binary spectrum. | 58 // - out : Binary spectrum. |
| 59 // | 59 // |
| 60 static uint32_t BinarySpectrumFix(const uint16_t* spectrum, | 60 static uint32_t BinarySpectrumFix(const uint16_t* spectrum, |
| 61 SpectrumType* threshold_spectrum, | 61 SpectrumType* threshold_spectrum, |
| 62 int q_domain, | 62 int q_domain, |
| 63 int* threshold_initialized) { | 63 int* threshold_initialized) { |
| 64 int i = kBandFirst; | 64 int i = kBandFirst; |
| 65 uint32_t out = 0; | 65 uint32_t out = 0; |
| 66 | 66 |
| 67 assert(q_domain < 16); | 67 RTC_DCHECK_LT(q_domain, 16); |
| 68 | 68 |
| 69 if (!(*threshold_initialized)) { | 69 if (!(*threshold_initialized)) { |
| 70 // Set the |threshold_spectrum| to half the input |spectrum| as starting | 70 // Set the |threshold_spectrum| to half the input |spectrum| as starting |
| 71 // value. This speeds up the convergence. | 71 // value. This speeds up the convergence. |
| 72 for (i = kBandFirst; i <= kBandLast; i++) { | 72 for (i = kBandFirst; i <= kBandLast; i++) { |
| 73 if (spectrum[i] > 0) { | 73 if (spectrum[i] > 0) { |
| 74 // Convert input spectrum from Q(|q_domain|) to Q15. | 74 // Convert input spectrum from Q(|q_domain|) to Q15. |
| 75 int32_t spectrum_q15 = ((int32_t) spectrum[i]) << (15 - q_domain); | 75 int32_t spectrum_q15 = ((int32_t) spectrum[i]) << (15 - q_domain); |
| 76 threshold_spectrum[i].int32_ = (spectrum_q15 >> 1); | 76 threshold_spectrum[i].int32_ = (spectrum_q15 >> 1); |
| 77 *threshold_initialized = 1; | 77 *threshold_initialized = 1; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 memset(self->mean_far_spectrum, 0, | 187 memset(self->mean_far_spectrum, 0, |
| 188 sizeof(SpectrumType) * self->spectrum_size); | 188 sizeof(SpectrumType) * self->spectrum_size); |
| 189 // Reset initialization indicators. | 189 // Reset initialization indicators. |
| 190 self->far_spectrum_initialized = 0; | 190 self->far_spectrum_initialized = 0; |
| 191 | 191 |
| 192 return 0; | 192 return 0; |
| 193 } | 193 } |
| 194 | 194 |
| 195 void WebRtc_SoftResetDelayEstimatorFarend(void* handle, int delay_shift) { | 195 void WebRtc_SoftResetDelayEstimatorFarend(void* handle, int delay_shift) { |
| 196 DelayEstimatorFarend* self = (DelayEstimatorFarend*) handle; | 196 DelayEstimatorFarend* self = (DelayEstimatorFarend*) handle; |
| 197 assert(self != NULL); | 197 RTC_DCHECK(self); |
| 198 WebRtc_SoftResetBinaryDelayEstimatorFarend(self->binary_farend, delay_shift); | 198 WebRtc_SoftResetBinaryDelayEstimatorFarend(self->binary_farend, delay_shift); |
| 199 } | 199 } |
| 200 | 200 |
| 201 int WebRtc_AddFarSpectrumFix(void* handle, | 201 int WebRtc_AddFarSpectrumFix(void* handle, |
| 202 const uint16_t* far_spectrum, | 202 const uint16_t* far_spectrum, |
| 203 int spectrum_size, | 203 int spectrum_size, |
| 204 int far_q) { | 204 int far_q) { |
| 205 DelayEstimatorFarend* self = (DelayEstimatorFarend*) handle; | 205 DelayEstimatorFarend* self = (DelayEstimatorFarend*) handle; |
| 206 uint32_t binary_spectrum = 0; | 206 uint32_t binary_spectrum = 0; |
| 207 | 207 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 memset(self->mean_near_spectrum, 0, | 317 memset(self->mean_near_spectrum, 0, |
| 318 sizeof(SpectrumType) * self->spectrum_size); | 318 sizeof(SpectrumType) * self->spectrum_size); |
| 319 // Reset initialization indicators. | 319 // Reset initialization indicators. |
| 320 self->near_spectrum_initialized = 0; | 320 self->near_spectrum_initialized = 0; |
| 321 | 321 |
| 322 return 0; | 322 return 0; |
| 323 } | 323 } |
| 324 | 324 |
| 325 int WebRtc_SoftResetDelayEstimator(void* handle, int delay_shift) { | 325 int WebRtc_SoftResetDelayEstimator(void* handle, int delay_shift) { |
| 326 DelayEstimator* self = (DelayEstimator*) handle; | 326 DelayEstimator* self = (DelayEstimator*) handle; |
| 327 assert(self != NULL); | 327 RTC_DCHECK(self); |
| 328 return WebRtc_SoftResetBinaryDelayEstimator(self->binary_handle, delay_shift); | 328 return WebRtc_SoftResetBinaryDelayEstimator(self->binary_handle, delay_shift); |
| 329 } | 329 } |
| 330 | 330 |
| 331 int WebRtc_set_history_size(void* handle, int history_size) { | 331 int WebRtc_set_history_size(void* handle, int history_size) { |
| 332 DelayEstimator* self = static_cast<DelayEstimator*>(handle); | 332 DelayEstimator* self = static_cast<DelayEstimator*>(handle); |
| 333 | 333 |
| 334 if ((self == NULL) || (history_size <= 1)) { | 334 if ((self == NULL) || (history_size <= 1)) { |
| 335 return -1; | 335 return -1; |
| 336 } | 336 } |
| 337 return WebRtc_AllocateHistoryBufferMemory(self->binary_handle, history_size); | 337 return WebRtc_AllocateHistoryBufferMemory(self->binary_handle, history_size); |
| 338 } | 338 } |
| 339 | 339 |
| 340 int WebRtc_history_size(const void* handle) { | 340 int WebRtc_history_size(const void* handle) { |
| 341 const DelayEstimator* self = static_cast<const DelayEstimator*>(handle); | 341 const DelayEstimator* self = static_cast<const DelayEstimator*>(handle); |
| 342 | 342 |
| 343 if (self == NULL) { | 343 if (self == NULL) { |
| 344 return -1; | 344 return -1; |
| 345 } | 345 } |
| 346 if (self->binary_handle->farend->history_size != | 346 if (self->binary_handle->farend->history_size != |
| 347 self->binary_handle->history_size) { | 347 self->binary_handle->history_size) { |
| 348 // Non matching history sizes. | 348 // Non matching history sizes. |
| 349 return -1; | 349 return -1; |
| 350 } | 350 } |
| 351 return self->binary_handle->history_size; | 351 return self->binary_handle->history_size; |
| 352 } | 352 } |
| 353 | 353 |
| 354 int WebRtc_set_lookahead(void* handle, int lookahead) { | 354 int WebRtc_set_lookahead(void* handle, int lookahead) { |
| 355 DelayEstimator* self = (DelayEstimator*) handle; | 355 DelayEstimator* self = (DelayEstimator*) handle; |
| 356 assert(self != NULL); | 356 RTC_DCHECK(self); |
| 357 assert(self->binary_handle != NULL); | 357 RTC_DCHECK(self->binary_handle); |
| 358 if ((lookahead > self->binary_handle->near_history_size - 1) || | 358 if ((lookahead > self->binary_handle->near_history_size - 1) || |
| 359 (lookahead < 0)) { | 359 (lookahead < 0)) { |
| 360 return -1; | 360 return -1; |
| 361 } | 361 } |
| 362 self->binary_handle->lookahead = lookahead; | 362 self->binary_handle->lookahead = lookahead; |
| 363 return self->binary_handle->lookahead; | 363 return self->binary_handle->lookahead; |
| 364 } | 364 } |
| 365 | 365 |
| 366 int WebRtc_lookahead(void* handle) { | 366 int WebRtc_lookahead(void* handle) { |
| 367 DelayEstimator* self = (DelayEstimator*) handle; | 367 DelayEstimator* self = (DelayEstimator*) handle; |
| 368 assert(self != NULL); | 368 RTC_DCHECK(self); |
| 369 assert(self->binary_handle != NULL); | 369 RTC_DCHECK(self->binary_handle); |
| 370 return self->binary_handle->lookahead; | 370 return self->binary_handle->lookahead; |
| 371 } | 371 } |
| 372 | 372 |
| 373 int WebRtc_set_allowed_offset(void* handle, int allowed_offset) { | 373 int WebRtc_set_allowed_offset(void* handle, int allowed_offset) { |
| 374 DelayEstimator* self = (DelayEstimator*) handle; | 374 DelayEstimator* self = (DelayEstimator*) handle; |
| 375 | 375 |
| 376 if ((self == NULL) || (allowed_offset < 0)) { | 376 if ((self == NULL) || (allowed_offset < 0)) { |
| 377 return -1; | 377 return -1; |
| 378 } | 378 } |
| 379 self->binary_handle->allowed_offset = allowed_offset; | 379 self->binary_handle->allowed_offset = allowed_offset; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 391 | 391 |
| 392 int WebRtc_enable_robust_validation(void* handle, int enable) { | 392 int WebRtc_enable_robust_validation(void* handle, int enable) { |
| 393 DelayEstimator* self = (DelayEstimator*) handle; | 393 DelayEstimator* self = (DelayEstimator*) handle; |
| 394 | 394 |
| 395 if (self == NULL) { | 395 if (self == NULL) { |
| 396 return -1; | 396 return -1; |
| 397 } | 397 } |
| 398 if ((enable < 0) || (enable > 1)) { | 398 if ((enable < 0) || (enable > 1)) { |
| 399 return -1; | 399 return -1; |
| 400 } | 400 } |
| 401 assert(self->binary_handle != NULL); | 401 RTC_DCHECK(self->binary_handle); |
| 402 self->binary_handle->robust_validation_enabled = enable; | 402 self->binary_handle->robust_validation_enabled = enable; |
| 403 return 0; | 403 return 0; |
| 404 } | 404 } |
| 405 | 405 |
| 406 int WebRtc_is_robust_validation_enabled(const void* handle) { | 406 int WebRtc_is_robust_validation_enabled(const void* handle) { |
| 407 const DelayEstimator* self = (const DelayEstimator*) handle; | 407 const DelayEstimator* self = (const DelayEstimator*) handle; |
| 408 | 408 |
| 409 if (self == NULL) { | 409 if (self == NULL) { |
| 410 return -1; | 410 return -1; |
| 411 } | 411 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 | 474 |
| 475 if (self == NULL) { | 475 if (self == NULL) { |
| 476 return -1; | 476 return -1; |
| 477 } | 477 } |
| 478 | 478 |
| 479 return WebRtc_binary_last_delay(self->binary_handle); | 479 return WebRtc_binary_last_delay(self->binary_handle); |
| 480 } | 480 } |
| 481 | 481 |
| 482 float WebRtc_last_delay_quality(void* handle) { | 482 float WebRtc_last_delay_quality(void* handle) { |
| 483 DelayEstimator* self = (DelayEstimator*) handle; | 483 DelayEstimator* self = (DelayEstimator*) handle; |
| 484 assert(self != NULL); | 484 RTC_DCHECK(self); |
| 485 return WebRtc_binary_last_delay_quality(self->binary_handle); | 485 return WebRtc_binary_last_delay_quality(self->binary_handle); |
| 486 } | 486 } |
| OLD | NEW |