diff options
author | bjornv@webrtc.org <bjornv@webrtc.org@4adac7df-926f-26a2-2b94-8c16560cd09d> | 2013-01-30 16:16:59 +0000 |
---|---|---|
committer | bjornv@webrtc.org <bjornv@webrtc.org@4adac7df-926f-26a2-2b94-8c16560cd09d> | 2013-01-30 16:16:59 +0000 |
commit | 7ded92b71e439f22f8f0b948452e7c19822163c7 (patch) | |
tree | fb49e1567f32094ddd761579c2e740fbff676fd3 /webrtc/modules/audio_processing/utility | |
parent | 51f11eb5aea3f67ae4dbaa011e320effa6ea4233 (diff) | |
download | webrtc-7ded92b71e439f22f8f0b948452e7c19822163c7.tar.gz |
Re-committing r3428
TBR=ajm
BUG=None
Review URL: https://webrtc-codereview.appspot.com/1066008
git-svn-id: http://webrtc.googlecode.com/svn/trunk@3436 4adac7df-926f-26a2-2b94-8c16560cd09d
Diffstat (limited to 'webrtc/modules/audio_processing/utility')
6 files changed, 342 insertions, 155 deletions
diff --git a/webrtc/modules/audio_processing/utility/delay_estimator.c b/webrtc/modules/audio_processing/utility/delay_estimator.c index 2ef5c5f35b..bcbb00aa8d 100644 --- a/webrtc/modules/audio_processing/utility/delay_estimator.c +++ b/webrtc/modules/audio_processing/utility/delay_estimator.c @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include "delay_estimator.h" +#include "webrtc/modules/audio_processing/utility/delay_estimator.h" #include <assert.h> #include <stdlib.h> @@ -76,9 +76,12 @@ void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self) { BinaryDelayEstimatorFarend* WebRtc_CreateBinaryDelayEstimatorFarend( int history_size) { - BinaryDelayEstimatorFarend* self = malloc(sizeof(BinaryDelayEstimatorFarend)); + BinaryDelayEstimatorFarend* self = NULL; - assert(history_size > 1); + if (history_size > 1) { + // Sanity conditions fulfilled. + self = malloc(sizeof(BinaryDelayEstimatorFarend)); + } if (self != NULL) { int malloc_fail = 0; diff --git a/webrtc/modules/audio_processing/utility/delay_estimator.h b/webrtc/modules/audio_processing/utility/delay_estimator.h index 11468f2899..bf2b08a217 100644 --- a/webrtc/modules/audio_processing/utility/delay_estimator.h +++ b/webrtc/modules/audio_processing/utility/delay_estimator.h @@ -14,14 +14,13 @@ #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_ #define WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_ -#include "typedefs.h" +#include "webrtc/typedefs.h" typedef struct { // Pointer to bit counts. int* far_bit_counts; // Binary history variables. uint32_t* binary_far_history; - // Buffer size. int history_size; } BinaryDelayEstimatorFarend; @@ -34,6 +33,7 @@ typedef struct { // Binary history variables. uint32_t* binary_near_history; + int near_history_size; // Delay estimation variables. int32_t minimum_probability; @@ -42,8 +42,6 @@ typedef struct { // Delay memory. int last_delay; - // Near-end buffer size. - int near_history_size; // Far-end binary spectrum history buffer etc. BinaryDelayEstimatorFarend* farend; } BinaryDelayEstimator; @@ -94,7 +92,7 @@ void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self); // - binary_far_spectrum : Far-end binary spectrum. // // Output: -// - handle : Updated far-end instance. +// - self : Updated far-end instance. // void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend* self, uint32_t binary_far_spectrum); diff --git a/webrtc/modules/audio_processing/utility/delay_estimator_internal.h b/webrtc/modules/audio_processing/utility/delay_estimator_internal.h index 43bbc4d012..fd11028fd6 100644 --- a/webrtc/modules/audio_processing/utility/delay_estimator_internal.h +++ b/webrtc/modules/audio_processing/utility/delay_estimator_internal.h @@ -13,8 +13,8 @@ #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_INTERNAL_H_ #define WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_INTERNAL_H_ -#include "modules/audio_processing/utility/delay_estimator.h" -#include "typedefs.h" +#include "webrtc/modules/audio_processing/utility/delay_estimator.h" +#include "webrtc/typedefs.h" typedef union { float float_; @@ -24,18 +24,25 @@ typedef union { typedef struct { // Pointers to mean values of spectrum. SpectrumType* mean_far_spectrum; - SpectrumType* mean_near_spectrum; - // |mean_*_spectrum| initialization indicator. + // |mean_far_spectrum| initialization indicator. int far_spectrum_initialized; + + int spectrum_size; + + // Far-end part of binary spectrum based delay estimation. + BinaryDelayEstimatorFarend* binary_farend; +} DelayEstimatorFarend; + +typedef struct { + // Pointers to mean values of spectrum. + SpectrumType* mean_near_spectrum; + // |mean_near_spectrum| initialization indicator. int near_spectrum_initialized; int spectrum_size; // Binary spectrum based delay estimator BinaryDelayEstimator* binary_handle; - // TODO(bjornv): This is an intermediate member variable. To be removed when - // we complete full support. - BinaryDelayEstimatorFarend* binary_farend; } DelayEstimator; #endif // WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_INTERNAL_H_ diff --git a/webrtc/modules/audio_processing/utility/delay_estimator_unittest.cc b/webrtc/modules/audio_processing/utility/delay_estimator_unittest.cc index 27f883272e..ac3bc16463 100644 --- a/webrtc/modules/audio_processing/utility/delay_estimator_unittest.cc +++ b/webrtc/modules/audio_processing/utility/delay_estimator_unittest.cc @@ -11,11 +11,11 @@ #include "gtest/gtest.h" extern "C" { -#include "modules/audio_processing/utility/delay_estimator.h" -#include "modules/audio_processing/utility/delay_estimator_internal.h" -#include "modules/audio_processing/utility/delay_estimator_wrapper.h" +#include "webrtc/modules/audio_processing/utility/delay_estimator.h" +#include "webrtc/modules/audio_processing/utility/delay_estimator_internal.h" +#include "webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h" } -#include "typedefs.h" +#include "webrtc/typedefs.h" namespace { @@ -42,6 +42,8 @@ class DelayEstimatorTest : public ::testing::Test { void* handle_; DelayEstimator* self_; + void* farend_handle_; + DelayEstimatorFarend* farend_self_; BinaryDelayEstimator* binary_; BinaryDelayEstimatorFarend* binary_farend_; int spectrum_size_; @@ -56,6 +58,8 @@ class DelayEstimatorTest : public ::testing::Test { DelayEstimatorTest::DelayEstimatorTest() : handle_(NULL), self_(NULL), + farend_handle_(NULL), + farend_self_(NULL), binary_(NULL), binary_farend_(NULL), spectrum_size_(kSpectrumSize) { @@ -74,7 +78,11 @@ DelayEstimatorTest::DelayEstimatorTest() } void DelayEstimatorTest::SetUp() { - handle_ = WebRtc_CreateDelayEstimator(kSpectrumSize, kMaxDelay, kLookahead); + farend_handle_ = WebRtc_CreateDelayEstimatorFarend(kSpectrumSize, + kMaxDelay + kLookahead); + ASSERT_TRUE(farend_handle_ != NULL); + farend_self_ = reinterpret_cast<DelayEstimatorFarend*>(farend_handle_); + handle_ = WebRtc_CreateDelayEstimator(farend_handle_, kLookahead); ASSERT_TRUE(handle_ != NULL); self_ = reinterpret_cast<DelayEstimator*>(handle_); binary_farend_ = WebRtc_CreateBinaryDelayEstimatorFarend(kMaxDelay + @@ -88,6 +96,9 @@ void DelayEstimatorTest::TearDown() { WebRtc_FreeDelayEstimator(handle_); handle_ = NULL; self_ = NULL; + WebRtc_FreeDelayEstimatorFarend(farend_handle_); + farend_handle_ = NULL; + farend_self_ = NULL; WebRtc_FreeBinaryDelayEstimator(binary_); binary_ = NULL; WebRtc_FreeBinaryDelayEstimatorFarend(binary_farend_); @@ -96,9 +107,10 @@ void DelayEstimatorTest::TearDown() { void DelayEstimatorTest::Init() { // Initialize Delay Estimator + EXPECT_EQ(0, WebRtc_InitDelayEstimatorFarend(farend_handle_)); EXPECT_EQ(0, WebRtc_InitDelayEstimator(handle_)); // Verify initialization. - EXPECT_EQ(0, self_->far_spectrum_initialized); + EXPECT_EQ(0, farend_self_->far_spectrum_initialized); EXPECT_EQ(0, self_->near_spectrum_initialized); EXPECT_EQ(-2, WebRtc_last_delay(handle_)); // Delay in initial state. EXPECT_EQ(0, WebRtc_last_delay_quality(handle_)); // Zero quality. @@ -187,59 +199,78 @@ void DelayEstimatorTest::RunBinarySpectraTest(int near_offset, TEST_F(DelayEstimatorTest, CorrectErrorReturnsOfWrapper) { // In this test we verify correct error returns on invalid API calls. - // WebRtc_CreateDelayEstimator() should return a NULL pointer on invalid input - // values. + // WebRtc_CreateDelayEstimatorFarend() and WebRtc_CreateDelayEstimator() + // should return a NULL pointer on invalid input values. // Make sure we have a non-NULL value at start, so we can detect NULL after // create failure. - void* handle = handle_; - handle = WebRtc_CreateDelayEstimator(33, kMaxDelay, kLookahead); + void* handle = farend_handle_; + handle = WebRtc_CreateDelayEstimatorFarend(33, kMaxDelay + kLookahead); EXPECT_TRUE(handle == NULL); + handle = farend_handle_; + handle = WebRtc_CreateDelayEstimatorFarend(kSpectrumSize, 1); + EXPECT_TRUE(handle == NULL); + handle = handle_; - handle = WebRtc_CreateDelayEstimator(kSpectrumSize, kMaxDelay, -1); + handle = WebRtc_CreateDelayEstimator(NULL, kLookahead); EXPECT_TRUE(handle == NULL); handle = handle_; - handle = WebRtc_CreateDelayEstimator(kSpectrumSize, 0, 0); + handle = WebRtc_CreateDelayEstimator(farend_handle_, -1); EXPECT_TRUE(handle == NULL); - // WebRtc_InitDelayEstimator() should return -1 if we have a NULL pointer as - // |handle|. + // WebRtc_InitDelayEstimatorFarend() and WebRtc_InitDelayEstimator() should + // return -1 if we have a NULL pointer as |handle|. + EXPECT_EQ(-1, WebRtc_InitDelayEstimatorFarend(NULL)); EXPECT_EQ(-1, WebRtc_InitDelayEstimator(NULL)); - // WebRtc_DelayEstimatorProcessFloat() should return -1 if we have: + // WebRtc_AddFarSpectrumFloat() should return -1 if we have: // 1) NULL pointer as |handle|. // 2) NULL pointer as far-end spectrum. - // 3) NULL pointer as near-end spectrum. - // 4) Incorrect spectrum size. - EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFloat(NULL, far_f_, near_f_, + // 3) Incorrect spectrum size. + EXPECT_EQ(-1, WebRtc_AddFarSpectrumFloat(NULL, far_f_, spectrum_size_)); + // Use |farend_handle_| which is properly created at SetUp(). + EXPECT_EQ(-1, WebRtc_AddFarSpectrumFloat(farend_handle_, NULL, + spectrum_size_)); + EXPECT_EQ(-1, WebRtc_AddFarSpectrumFloat(farend_handle_, far_f_, + spectrum_size_ + 1)); + + // WebRtc_AddFarSpectrumFix() should return -1 if we have: + // 1) NULL pointer as |handle|. + // 2) NULL pointer as far-end spectrum. + // 3) Incorrect spectrum size. + // 4) Too high precision in far-end spectrum (Q-domain > 15). + EXPECT_EQ(-1, WebRtc_AddFarSpectrumFix(NULL, far_u16_, spectrum_size_, 0)); + EXPECT_EQ(-1, WebRtc_AddFarSpectrumFix(farend_handle_, NULL, spectrum_size_, + 0)); + EXPECT_EQ(-1, WebRtc_AddFarSpectrumFix(farend_handle_, far_u16_, + spectrum_size_ + 1, 0)); + EXPECT_EQ(-1, WebRtc_AddFarSpectrumFix(farend_handle_, far_u16_, + spectrum_size_, 16)); + + // WebRtc_DelayEstimatorProcessFloat() should return -1 if we have: + // 1) NULL pointer as |handle|. + // 2) NULL pointer as near-end spectrum. + // 3) Incorrect spectrum size. + EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFloat(NULL, near_f_, spectrum_size_)); // Use |handle_| which is properly created at SetUp(). - EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFloat(handle_, NULL, near_f_, - spectrum_size_)); - EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFloat(handle_, far_f_, NULL, + EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFloat(handle_, NULL, spectrum_size_)); - EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFloat(handle_, far_f_, near_f_, + EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFloat(handle_, near_f_, spectrum_size_ + 1)); // WebRtc_DelayEstimatorProcessFix() should return -1 if we have: // 1) NULL pointer as |handle|. - // 2) NULL pointer as far-end spectrum. // 3) NULL pointer as near-end spectrum. // 4) Incorrect spectrum size. - // 5) Too high precision in far-end spectrum (Q-domain > 15). // 6) Too high precision in near-end spectrum (Q-domain > 15). - EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(NULL, far_u16_, near_u16_, - spectrum_size_, 0, 0)); - // Use |handle_| which is properly created at SetUp(). - EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(handle_, NULL, near_u16_, - spectrum_size_, 0, 0)); - EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(handle_, far_u16_, NULL, - spectrum_size_, 0, 0)); - EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(handle_, far_u16_, near_u16_, - spectrum_size_ + 1, 0, 0)); - EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(handle_, far_u16_, near_u16_, - spectrum_size_, 16, 0)); - EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(handle_, far_u16_, near_u16_, - spectrum_size_, 0, 16)); + EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(NULL, near_u16_, spectrum_size_, + 0)); + EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(handle_, NULL, spectrum_size_, + 0)); + EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(handle_, near_u16_, + spectrum_size_ + 1, 0)); + EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(handle_, near_u16_, + spectrum_size_, 16)); // WebRtc_last_delay() should return -1 if we have a NULL pointer as |handle|. EXPECT_EQ(-1, WebRtc_last_delay(NULL)); @@ -254,22 +285,26 @@ TEST_F(DelayEstimatorTest, CorrectErrorReturnsOfWrapper) { TEST_F(DelayEstimatorTest, InitializedSpectrumAfterProcess) { // In this test we verify that the mean spectra are initialized after first - // time we call Process(). + // time we call WebRtc_AddFarSpectrum() and Process() respectively. // For floating point operations, process one frame and verify initialization // flag. Init(); - EXPECT_EQ(-2, WebRtc_DelayEstimatorProcessFloat(handle_, far_f_, near_f_, + EXPECT_EQ(0, WebRtc_AddFarSpectrumFloat(farend_handle_, far_f_, + spectrum_size_)); + EXPECT_EQ(1, farend_self_->far_spectrum_initialized); + EXPECT_EQ(-2, WebRtc_DelayEstimatorProcessFloat(handle_, near_f_, spectrum_size_)); - EXPECT_EQ(1, self_->far_spectrum_initialized); EXPECT_EQ(1, self_->near_spectrum_initialized); // For fixed point operations, process one frame and verify initialization // flag. Init(); - EXPECT_EQ(-2, WebRtc_DelayEstimatorProcessFix(handle_, far_u16_, near_u16_, - spectrum_size_, 0, 0)); - EXPECT_EQ(1, self_->far_spectrum_initialized); + EXPECT_EQ(0, WebRtc_AddFarSpectrumFix(farend_handle_, far_u16_, + spectrum_size_, 0)); + EXPECT_EQ(1, farend_self_->far_spectrum_initialized); + EXPECT_EQ(-2, WebRtc_DelayEstimatorProcessFix(handle_, near_u16_, + spectrum_size_, 0)); EXPECT_EQ(1, self_->near_spectrum_initialized); } @@ -283,7 +318,9 @@ TEST_F(DelayEstimatorTest, CorrectLastDelay) { // Floating point operations. Init(); for (int i = 0; i < 200; i++) { - last_delay = WebRtc_DelayEstimatorProcessFloat(handle_, far_f_, near_f_, + EXPECT_EQ(0, WebRtc_AddFarSpectrumFloat(farend_handle_, far_f_, + spectrum_size_)); + last_delay = WebRtc_DelayEstimatorProcessFloat(handle_, near_f_, spectrum_size_); if (last_delay != -2) { EXPECT_EQ(last_delay, WebRtc_last_delay(handle_)); @@ -298,8 +335,10 @@ TEST_F(DelayEstimatorTest, CorrectLastDelay) { // Fixed point operations. Init(); for (int i = 0; i < 200; i++) { - last_delay = WebRtc_DelayEstimatorProcessFix(handle_, far_u16_, near_u16_, - spectrum_size_, 0, 0); + EXPECT_EQ(0, WebRtc_AddFarSpectrumFix(farend_handle_, far_u16_, + spectrum_size_, 0)); + last_delay = WebRtc_DelayEstimatorProcessFix(handle_, near_u16_, + spectrum_size_, 0); if (last_delay != -2) { EXPECT_EQ(last_delay, WebRtc_last_delay(handle_)); EXPECT_EQ(7203, WebRtc_last_delay_quality(handle_)); @@ -311,6 +350,20 @@ TEST_F(DelayEstimatorTest, CorrectLastDelay) { EXPECT_NE(0, WebRtc_last_delay_quality(handle_)); } +TEST_F(DelayEstimatorTest, CorrectErrorReturnsOfBinaryEstimatorFarend) { + // In this test we verify correct output on invalid API calls to the Binary + // Delay Estimator (far-end part). + + BinaryDelayEstimatorFarend* binary = binary_farend_; + // WebRtc_CreateBinaryDelayEstimatorFarend() should return -1 if the input + // history size is less than 2. This is to make sure the buffer shifting + // applies properly. + // Make sure we have a non-NULL value at start, so we can detect NULL after + // create failure. + binary = WebRtc_CreateBinaryDelayEstimatorFarend(1); + EXPECT_TRUE(binary == NULL); +} + TEST_F(DelayEstimatorTest, CorrectErrorReturnsOfBinaryEstimator) { // In this test we verify correct output on invalid API calls to the Binary // Delay Estimator. diff --git a/webrtc/modules/audio_processing/utility/delay_estimator_wrapper.c b/webrtc/modules/audio_processing/utility/delay_estimator_wrapper.c index a00923f667..c358f13836 100644 --- a/webrtc/modules/audio_processing/utility/delay_estimator_wrapper.c +++ b/webrtc/modules/audio_processing/utility/delay_estimator_wrapper.c @@ -8,19 +8,20 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include "delay_estimator_wrapper.h" +#include "webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h" #include <assert.h> #include <stdlib.h> #include <string.h> -#include "delay_estimator.h" -#include "modules/audio_processing/utility/delay_estimator_internal.h" +#include "webrtc/modules/audio_processing/utility/delay_estimator.h" +#include "webrtc/modules/audio_processing/utility/delay_estimator_internal.h" +#include "webrtc/system_wrappers/interface/compile_assert.h" // Only bit |kBandFirst| through bit |kBandLast| are processed and // |kBandFirst| - |kBandLast| must be < 32. -static const int kBandFirst = 12; -static const int kBandLast = 43; +enum { kBandFirst = 12 }; +enum { kBandLast = 43 }; static __inline uint32_t SetBit(uint32_t in, int pos) { uint32_t mask = (1 << pos); @@ -122,8 +123,8 @@ static uint32_t BinarySpectrumFloat(float* spectrum, return out; } -void WebRtc_FreeDelayEstimator(void* handle) { - DelayEstimator* self = (DelayEstimator*) handle; +void WebRtc_FreeDelayEstimatorFarend(void* handle) { + DelayEstimatorFarend* self = (DelayEstimatorFarend*) handle; if (handle == NULL) { return; @@ -132,54 +133,156 @@ void WebRtc_FreeDelayEstimator(void* handle) { free(self->mean_far_spectrum); self->mean_far_spectrum = NULL; - free(self->mean_near_spectrum); - self->mean_near_spectrum = NULL; - - WebRtc_FreeBinaryDelayEstimator(self->binary_handle); - self->binary_handle = NULL; - WebRtc_FreeBinaryDelayEstimatorFarend(self->binary_farend); self->binary_farend = NULL; free(self); } -void* WebRtc_CreateDelayEstimator(int spectrum_size, int max_delay, - int lookahead) { - DelayEstimator* self = NULL; - const int history_size = max_delay + lookahead; // For buffer shifting: > 1 +void* WebRtc_CreateDelayEstimatorFarend(int spectrum_size, int history_size) { + DelayEstimatorFarend* self = NULL; - // TODO(bjornv): Make this a static assert. // Check if the sub band used in the delay estimation is small enough to fit // the binary spectra in a uint32_t. - assert(kBandLast - kBandFirst < 32); + COMPILE_ASSERT(kBandLast - kBandFirst < 32); - if ((spectrum_size >= kBandLast) && (history_size > 1)) { + if (spectrum_size >= kBandLast) { self = malloc(sizeof(DelayEstimator)); } if (self != NULL) { int memory_fail = 0; - self->mean_far_spectrum = NULL; - self->mean_near_spectrum = NULL; - self->binary_farend = NULL; - - // Allocate memory for the farend spectrum handling. + // Allocate memory for the binary far-end spectrum handling. self->binary_farend = WebRtc_CreateBinaryDelayEstimatorFarend(history_size); - - self->binary_handle = WebRtc_CreateBinaryDelayEstimator(self->binary_farend, - lookahead); - memory_fail |= (self->binary_handle == NULL); + memory_fail |= (self->binary_farend == NULL); // Allocate memory for spectrum buffers. self->mean_far_spectrum = malloc(spectrum_size * sizeof(SpectrumType)); memory_fail |= (self->mean_far_spectrum == NULL); - self->mean_near_spectrum = malloc(spectrum_size * sizeof(SpectrumType)); + self->spectrum_size = spectrum_size; + + if (memory_fail) { + WebRtc_FreeDelayEstimatorFarend(self); + self = NULL; + } + } + + return self; +} + +int WebRtc_InitDelayEstimatorFarend(void* handle) { + DelayEstimatorFarend* self = (DelayEstimatorFarend*) handle; + + if (self == NULL) { + return -1; + } + + // Initialize far-end part of binary delay estimator. + WebRtc_InitBinaryDelayEstimatorFarend(self->binary_farend); + + // Set averaged far and near end spectra to zero. + memset(self->mean_far_spectrum, 0, + sizeof(SpectrumType) * self->spectrum_size); + // Reset initialization indicators. + self->far_spectrum_initialized = 0; + + return 0; +} + +int WebRtc_AddFarSpectrumFix(void* handle, uint16_t* far_spectrum, + int spectrum_size, int far_q) { + DelayEstimatorFarend* self = (DelayEstimatorFarend*) handle; + uint32_t binary_spectrum = 0; + + if (self == NULL) { + return -1; + } + if (far_spectrum == NULL) { + // Empty far end spectrum. + return -1; + } + if (spectrum_size != self->spectrum_size) { + // Data sizes don't match. + return -1; + } + if (far_q > 15) { + // If |far_q| is larger than 15 we cannot guarantee no wrap around. + return -1; + } + + // Get binary spectrum. + binary_spectrum = BinarySpectrumFix(far_spectrum, self->mean_far_spectrum, + far_q, &(self->far_spectrum_initialized)); + WebRtc_AddBinaryFarSpectrum(self->binary_farend, binary_spectrum); + + return 0; +} + +int WebRtc_AddFarSpectrumFloat(void* handle, float* far_spectrum, + int spectrum_size) { + DelayEstimatorFarend* self = (DelayEstimatorFarend*) handle; + uint32_t binary_spectrum = 0; + + if (self == NULL) { + return -1; + } + if (far_spectrum == NULL) { + // Empty far end spectrum. + return -1; + } + if (spectrum_size != self->spectrum_size) { + // Data sizes don't match. + return -1; + } + + // Get binary spectrum. + binary_spectrum = BinarySpectrumFloat(far_spectrum, self->mean_far_spectrum, + &(self->far_spectrum_initialized)); + WebRtc_AddBinaryFarSpectrum(self->binary_farend, binary_spectrum); + + return 0; +} + +void WebRtc_FreeDelayEstimator(void* handle) { + DelayEstimator* self = (DelayEstimator*) handle; + + if (handle == NULL) { + return; + } + + free(self->mean_near_spectrum); + self->mean_near_spectrum = NULL; + + WebRtc_FreeBinaryDelayEstimator(self->binary_handle); + self->binary_handle = NULL; + + free(self); +} + +void* WebRtc_CreateDelayEstimator(void* farend_handle, int lookahead) { + DelayEstimator* self = NULL; + DelayEstimatorFarend* farend = (DelayEstimatorFarend*) farend_handle; + + if (farend_handle != NULL) { + self = malloc(sizeof(DelayEstimator)); + } + + if (self != NULL) { + int memory_fail = 0; + + // Allocate memory for the farend spectrum handling. + self->binary_handle = + WebRtc_CreateBinaryDelayEstimator(farend->binary_farend, lookahead); + memory_fail |= (self->binary_handle == NULL); + + // Allocate memory for spectrum buffers. + self->mean_near_spectrum = malloc(farend->spectrum_size * + sizeof(SpectrumType)); memory_fail |= (self->mean_near_spectrum == NULL); - self->spectrum_size = spectrum_size; + self->spectrum_size = farend->spectrum_size; if (memory_fail) { WebRtc_FreeDelayEstimator(self); @@ -197,40 +300,28 @@ int WebRtc_InitDelayEstimator(void* handle) { return -1; } - // Initialize far-end part of binary delay estimator. - WebRtc_InitBinaryDelayEstimatorFarend(self->binary_farend); // Initialize binary delay estimator. WebRtc_InitBinaryDelayEstimator(self->binary_handle); // Set averaged far and near end spectra to zero. - memset(self->mean_far_spectrum, 0, - sizeof(SpectrumType) * self->spectrum_size); memset(self->mean_near_spectrum, 0, sizeof(SpectrumType) * self->spectrum_size); // Reset initialization indicators. - self->far_spectrum_initialized = 0; self->near_spectrum_initialized = 0; return 0; } int WebRtc_DelayEstimatorProcessFix(void* handle, - uint16_t* far_spectrum, uint16_t* near_spectrum, int spectrum_size, - int far_q, int near_q) { DelayEstimator* self = (DelayEstimator*) handle; - uint32_t binary_far_spectrum = 0; - uint32_t binary_near_spectrum = 0; + uint32_t binary_spectrum = 0; if (self == NULL) { return -1; } - if (far_spectrum == NULL) { - // Empty far end spectrum. - return -1; - } if (near_spectrum == NULL) { // Empty near end spectrum. return -1; @@ -239,46 +330,29 @@ int WebRtc_DelayEstimatorProcessFix(void* handle, // Data sizes don't match. return -1; } - if (far_q > 15) { - // If |far_q| is larger than 15 we cannot guarantee no wrap around. - return -1; - } if (near_q > 15) { // If |near_q| is larger than 15 we cannot guarantee no wrap around. return -1; } // Get binary spectra. - binary_far_spectrum = BinarySpectrumFix(far_spectrum, - self->mean_far_spectrum, - far_q, - &(self->far_spectrum_initialized)); - binary_near_spectrum = BinarySpectrumFix(near_spectrum, - self->mean_near_spectrum, - near_q, - &(self->near_spectrum_initialized)); - - WebRtc_AddBinaryFarSpectrum(self->binary_handle->farend, binary_far_spectrum); - - return WebRtc_ProcessBinarySpectrum(self->binary_handle, - binary_near_spectrum); + binary_spectrum = BinarySpectrumFix(near_spectrum, + self->mean_near_spectrum, + near_q, + &(self->near_spectrum_initialized)); + + return WebRtc_ProcessBinarySpectrum(self->binary_handle, binary_spectrum); } int WebRtc_DelayEstimatorProcessFloat(void* handle, - float* far_spectrum, float* near_spectrum, int spectrum_size) { DelayEstimator* self = (DelayEstimator*) handle; - uint32_t binary_far_spectrum = 0; - uint32_t binary_near_spectrum = 0; + uint32_t binary_spectrum = 0; if (self == NULL) { return -1; } - if (far_spectrum == NULL) { - // Empty far end spectrum. - return -1; - } if (near_spectrum == NULL) { // Empty near end spectrum. return -1; @@ -288,18 +362,11 @@ int WebRtc_DelayEstimatorProcessFloat(void* handle, return -1; } - // Get binary spectra. - binary_far_spectrum = BinarySpectrumFloat(far_spectrum, - self->mean_far_spectrum, - &(self->far_spectrum_initialized)); - binary_near_spectrum = - BinarySpectrumFloat(near_spectrum, self->mean_near_spectrum, - &(self->near_spectrum_initialized)); - - WebRtc_AddBinaryFarSpectrum(self->binary_handle->farend, binary_far_spectrum); + // Get binary spectrum. + binary_spectrum = BinarySpectrumFloat(near_spectrum, self->mean_near_spectrum, + &(self->near_spectrum_initialized)); - return WebRtc_ProcessBinarySpectrum(self->binary_handle, - binary_near_spectrum); + return WebRtc_ProcessBinarySpectrum(self->binary_handle, binary_spectrum); } int WebRtc_last_delay(void* handle) { diff --git a/webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h b/webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h index 3d243db062..51b9a0a1d5 100644 --- a/webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h +++ b/webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h @@ -14,7 +14,64 @@ #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_WRAPPER_H_ #define WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_WRAPPER_H_ -#include "typedefs.h" +#include "webrtc/typedefs.h" + +// Releases the memory allocated by WebRtc_CreateDelayEstimatorFarend(...) +// Input: +// - handle : Pointer to the delay estimation far-end instance. +// +void WebRtc_FreeDelayEstimatorFarend(void* handle); + +// Allocates the memory needed by the far-end part of the delay estimation. The +// memory needs to be initialized separately through +// WebRtc_InitDelayEstimatorFarend(...). +// +// Inputs: +// - spectrum_size : Size of the spectrum used both in far-end and +// near-end. Used to allocate memory for spectrum +// specific buffers. +// - history_size : The far-end history buffer size. Note that the maximum +// delay which can be estimated is controlled together +// with |lookahead| through +// WebRtc_CreateDelayEstimator(). +// +// Return value: +// - void* : Created |handle|. If the memory can't be allocated or +// if any of the input parameters are invalid NULL is +// returned. +// +void* WebRtc_CreateDelayEstimatorFarend(int spectrum_size, int history_size); + +// Initializes the far-end part of the delay estimation instance returned by +// WebRtc_CreateDelayEstimatorFarend(...) +// Input: +// - handle : Pointer to the delay estimation far-end instance. +// +// Output: +// - handle : Initialized instance. +// +int WebRtc_InitDelayEstimatorFarend(void* handle); + +// Adds the far-end spectrum to the far-end history buffer. This spectrum is +// used as reference when calculating the delay using +// WebRtc_ProcessSpectrum(). +// +// Inputs: +// - handle : Pointer to the delay estimation far-end instance. +// - far_spectrum : Far-end spectrum. +// - spectrum_size : The size of the data arrays (same for both far- and +// near-end). +// - far_q : The Q-domain of the far-end data. +// +// Output: +// - handle : Updated far-end instance. +// +int WebRtc_AddFarSpectrumFix(void* handle, uint16_t* far_spectrum, + int spectrum_size, int far_q); + +// See WebRtc_AddFarSpectrumFix() for description. +int WebRtc_AddFarSpectrumFloat(void* handle, float* far_spectrum, + int spectrum_size); // Releases the memory allocated by WebRtc_CreateDelayEstimator(...) // Input: @@ -26,11 +83,14 @@ void WebRtc_FreeDelayEstimator(void* handle); // initialized separately through WebRtc_InitDelayEstimator(...). // // Inputs: -// - spectrum_size : Size of the spectrum used both in far-end and -// near-end. Used to allocate memory for spectrum -// specific buffers. -// - max_delay : The maximum delay which can be estimated. Needed to -// allocate memory for history buffers. +// - farend_handle : Pointer to the far-end part of the delay estimation +// instance created prior to this call using +// WebRtc_CreateDelayEstimatorFarend(). +// +// Note that WebRtc_CreateDelayEstimator does not take +// ownership of |farend_handle|, which has to be torn +// down properly after this instance. +// // - lookahead : Amount of non-causal lookahead to use. This can // detect cases in which a near-end signal occurs before // the corresponding far-end signal. It will delay the @@ -41,13 +101,17 @@ void WebRtc_FreeDelayEstimator(void* handle); // This also represents the minimum delay which can be // estimated. // +// Note that the effective range of delay estimates is +// [-|lookahead|,... ,|history_size|-|lookahead|) +// where |history_size| was set upon creating the far-end +// history buffer size. +// // Return value: // - void* : Created |handle|. If the memory can't be allocated or // if any of the input parameters are invalid NULL is // returned. // -void* WebRtc_CreateDelayEstimator(int spectrum_size, int max_delay, - int lookahead); +void* WebRtc_CreateDelayEstimator(void* farend_handle, int lookahead); // Initializes the delay estimation instance returned by // WebRtc_CreateDelayEstimator(...) @@ -64,12 +128,10 @@ int WebRtc_InitDelayEstimator(void* handle); // subtracted from the returned value). // Inputs: // - handle : Pointer to the delay estimation instance. -// - far_spectrum : Pointer to the far-end spectrum data. // - near_spectrum : Pointer to the near-end spectrum data of the current // block. // - spectrum_size : The size of the data arrays (same for both far- and // near-end). -// - far_q : The Q-domain of the far-end data. // - near_q : The Q-domain of the near-end data. // // Output: @@ -81,15 +143,12 @@ int WebRtc_InitDelayEstimator(void* handle); // -2 - Insufficient data for estimation. // int WebRtc_DelayEstimatorProcessFix(void* handle, - uint16_t* far_spectrum, uint16_t* near_spectrum, int spectrum_size, - int far_q, int near_q); // See WebRtc_DelayEstimatorProcessFix() for description. int WebRtc_DelayEstimatorProcessFloat(void* handle, - float* far_spectrum, float* near_spectrum, int spectrum_size); @@ -100,7 +159,7 @@ int WebRtc_DelayEstimatorProcessFloat(void* handle, // - handle : Pointer to the delay estimation instance. // // Return value: -// - delay : >= 0 - Last calculated delay value. +// - delay : >= 0 - Last calculated delay value. // -1 - Error. // -2 - Insufficient data for estimation. // @@ -115,7 +174,7 @@ int WebRtc_last_delay(void* handle); // - handle : Pointer to the delay estimation instance. // // Return value: -// - delay_quality : >= 0 - Estimation quality (in Q9) of last calculated +// - delay_quality : >= 0 - Estimation quality (in Q9) of last calculated // delay value. // -1 - Error. // -2 - Insufficient data for estimation. |