pure sdk for main
This commit is contained in:
@@ -0,0 +1,723 @@
|
||||
/*
|
||||
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "delay_estimator.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#include "signal_processing_library.h"
|
||||
|
||||
|
||||
// Number of right shifts for scaling is linearly depending on number of bits in
|
||||
// the far-end binary spectrum.
|
||||
static const int kShiftsAtZero = 13; // Right shifts at zero binary spectrum.
|
||||
static const int kShiftsLinearSlope = 3;
|
||||
|
||||
static const int32_t kProbabilityOffset = 1024; // 2 in Q9.
|
||||
static const int32_t kProbabilityLowerLimit = 8704; // 17 in Q9.
|
||||
static const int32_t kProbabilityMinSpread = 2816; // 5.5 in Q9.
|
||||
|
||||
// Robust validation settings
|
||||
static const float kHistogramMax = 3000.f;
|
||||
static const float kLastHistogramMax = 250.f;
|
||||
static const float kMinHistogramThreshold = 1.5f;
|
||||
static const int kMinRequiredHits = 10;
|
||||
static const int kMaxHitsWhenPossiblyNonCausal = 10;
|
||||
static const int kMaxHitsWhenPossiblyCausal = 1000;
|
||||
static const float kQ14Scaling = 1.f / (1 << 14); // Scaling by 2^14 to get Q0.
|
||||
static const float kFractionSlope = 0.05f;
|
||||
static const float kMinFractionWhenPossiblyCausal = 0.5f;
|
||||
static const float kMinFractionWhenPossiblyNonCausal = 0.25f;
|
||||
|
||||
|
||||
// Counts and returns number of bits of a 32-bit word.
|
||||
static int BitCount(uint32_t u32) {
|
||||
uint32_t tmp =
|
||||
u32 - ((u32 >> 1) & 033333333333) - ((u32 >> 2) & 011111111111);
|
||||
tmp = ((tmp + (tmp >> 3)) & 030707070707);
|
||||
tmp = (tmp + (tmp >> 6));
|
||||
tmp = (tmp + (tmp >> 12) + (tmp >> 24)) & 077;
|
||||
|
||||
return ((int) tmp);
|
||||
}
|
||||
|
||||
// Compares the |binary_vector| with all rows of the |binary_matrix| and counts
|
||||
// per row the number of times they have the same value.
|
||||
//
|
||||
// Inputs:
|
||||
// - binary_vector : binary "vector" stored in a long
|
||||
// - binary_matrix : binary "matrix" stored as a vector of long
|
||||
// - matrix_size : size of binary "matrix"
|
||||
//
|
||||
// Output:
|
||||
// - bit_counts : "Vector" stored as a long, containing for each
|
||||
// row the number of times the matrix row and the
|
||||
// input vector have the same value
|
||||
//
|
||||
static void BitCountComparison(uint32_t binary_vector,
|
||||
const uint32_t *binary_matrix,
|
||||
int matrix_size,
|
||||
int32_t *bit_counts) {
|
||||
int n = 0;
|
||||
|
||||
// Compare |binary_vector| with all rows of the |binary_matrix|
|
||||
for (; n < matrix_size; n++) {
|
||||
bit_counts[n] = (int32_t) BitCount(binary_vector ^ binary_matrix[n]);
|
||||
}
|
||||
}
|
||||
|
||||
// Collects necessary statistics for the HistogramBasedValidation(). This
|
||||
// function has to be called prior to calling HistogramBasedValidation(). The
|
||||
// statistics updated and used by the HistogramBasedValidation() are:
|
||||
// 1. the number of |candidate_hits|, which states for how long we have had the
|
||||
// same |candidate_delay|
|
||||
// 2. the |histogram| of candidate delays over time. This histogram is
|
||||
// weighted with respect to a reliability measure and time-varying to cope
|
||||
// with possible delay shifts.
|
||||
// For further description see commented code.
|
||||
//
|
||||
// Inputs:
|
||||
// - candidate_delay : The delay to validate.
|
||||
// - valley_depth_q14 : The cost function has a valley/minimum at the
|
||||
// |candidate_delay| location. |valley_depth_q14| is the
|
||||
// cost function difference between the minimum and
|
||||
// maximum locations. The value is in the Q14 domain.
|
||||
// - valley_level_q14 : Is the cost function value at the minimum, in Q14.
|
||||
static void UpdateRobustValidationStatistics(BinaryDelayEstimator *self,
|
||||
int candidate_delay,
|
||||
int32_t valley_depth_q14,
|
||||
int32_t valley_level_q14) {
|
||||
const float valley_depth = valley_depth_q14 * kQ14Scaling;
|
||||
float decrease_in_last_set = valley_depth;
|
||||
const int max_hits_for_slow_change = (candidate_delay < self->last_delay)
|
||||
? kMaxHitsWhenPossiblyNonCausal
|
||||
: kMaxHitsWhenPossiblyCausal;
|
||||
int i = 0;
|
||||
|
||||
RTC_DCHECK_EQ(self->history_size, self->farend->history_size);
|
||||
// Reset |candidate_hits| if we have a new candidate.
|
||||
if (candidate_delay != self->last_candidate_delay) {
|
||||
self->candidate_hits = 0;
|
||||
self->last_candidate_delay = candidate_delay;
|
||||
}
|
||||
self->candidate_hits++;
|
||||
|
||||
// The |histogram| is updated differently across the bins.
|
||||
// 1. The |candidate_delay| histogram bin is increased with the
|
||||
// |valley_depth|, which is a simple measure of how reliable the
|
||||
// |candidate_delay| is. The histogram is not increased above
|
||||
// |kHistogramMax|.
|
||||
self->histogram[candidate_delay] += valley_depth;
|
||||
if (self->histogram[candidate_delay] > kHistogramMax) {
|
||||
self->histogram[candidate_delay] = kHistogramMax;
|
||||
}
|
||||
// 2. The histogram bins in the neighborhood of |candidate_delay| are
|
||||
// unaffected. The neighborhood is defined as x + {-2, -1, 0, 1}.
|
||||
// 3. The histogram bins in the neighborhood of |last_delay| are decreased
|
||||
// with |decrease_in_last_set|. This value equals the difference between
|
||||
// the cost function values at the locations |candidate_delay| and
|
||||
// |last_delay| until we reach |max_hits_for_slow_change| consecutive hits
|
||||
// at the |candidate_delay|. If we exceed this amount of hits the
|
||||
// |candidate_delay| is a "potential" candidate and we start decreasing
|
||||
// these histogram bins more rapidly with |valley_depth|.
|
||||
if (self->candidate_hits < max_hits_for_slow_change) {
|
||||
decrease_in_last_set =
|
||||
(self->mean_bit_counts[self->compare_delay] - valley_level_q14) *
|
||||
kQ14Scaling;
|
||||
}
|
||||
// 4. All other bins are decreased with |valley_depth|.
|
||||
// TODO(bjornv): Investigate how to make this loop more efficient. Split up
|
||||
// the loop? Remove parts that doesn't add too much.
|
||||
for (i = 0; i < self->history_size; ++i) {
|
||||
int is_in_last_set = (i >= self->last_delay - 2) &&
|
||||
(i <= self->last_delay + 1) && (i != candidate_delay);
|
||||
int is_in_candidate_set =
|
||||
(i >= candidate_delay - 2) && (i <= candidate_delay + 1);
|
||||
self->histogram[i] -=
|
||||
decrease_in_last_set * is_in_last_set +
|
||||
valley_depth * (!is_in_last_set && !is_in_candidate_set);
|
||||
// 5. No histogram bin can go below 0.
|
||||
if (self->histogram[i] < 0) {
|
||||
self->histogram[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validates the |candidate_delay|, estimated in WebRtc_ProcessBinarySpectrum(),
|
||||
// based on a mix of counting concurring hits with a modified histogram
|
||||
// of recent delay estimates. In brief a candidate is valid (returns 1) if it
|
||||
// is the most likely according to the histogram. There are a couple of
|
||||
// exceptions that are worth mentioning:
|
||||
// 1. If the |candidate_delay| < |last_delay| it can be that we are in a
|
||||
// non-causal state, breaking a possible echo control algorithm. Hence, we
|
||||
// open up for a quicker change by allowing the change even if the
|
||||
// |candidate_delay| is not the most likely one according to the histogram.
|
||||
// 2. There's a minimum number of hits (kMinRequiredHits) and the histogram
|
||||
// value has to reached a minimum (kMinHistogramThreshold) to be valid.
|
||||
// 3. The action is also depending on the filter length used for echo control.
|
||||
// If the delay difference is larger than what the filter can capture, we
|
||||
// also move quicker towards a change.
|
||||
// For further description see commented code.
|
||||
//
|
||||
// Input:
|
||||
// - candidate_delay : The delay to validate.
|
||||
//
|
||||
// Return value:
|
||||
// - is_histogram_valid : 1 - The |candidate_delay| is valid.
|
||||
// 0 - Otherwise.
|
||||
static int HistogramBasedValidation(const BinaryDelayEstimator *self,
|
||||
int candidate_delay) {
|
||||
float fraction = 1.f;
|
||||
float histogram_threshold = self->histogram[self->compare_delay];
|
||||
const int delay_difference = candidate_delay - self->last_delay;
|
||||
int is_histogram_valid = 0;
|
||||
|
||||
// The histogram based validation of |candidate_delay| is done by comparing
|
||||
// the |histogram| at bin |candidate_delay| with a |histogram_threshold|.
|
||||
// This |histogram_threshold| equals a |fraction| of the |histogram| at bin
|
||||
// |last_delay|. The |fraction| is a piecewise linear function of the
|
||||
// |delay_difference| between the |candidate_delay| and the |last_delay|
|
||||
// allowing for a quicker move if
|
||||
// i) a potential echo control filter can not handle these large differences.
|
||||
// ii) keeping |last_delay| instead of updating to |candidate_delay| could
|
||||
// force an echo control into a non-causal state.
|
||||
// We further require the histogram to have reached a minimum value of
|
||||
// |kMinHistogramThreshold|. In addition, we also require the number of
|
||||
// |candidate_hits| to be more than |kMinRequiredHits| to remove spurious
|
||||
// values.
|
||||
|
||||
// Calculate a comparison histogram value (|histogram_threshold|) that is
|
||||
// depending on the distance between the |candidate_delay| and |last_delay|.
|
||||
// TODO(bjornv): How much can we gain by turning the fraction calculation
|
||||
// into tables?
|
||||
if (delay_difference > self->allowed_offset) {
|
||||
fraction = 1.f - kFractionSlope * (delay_difference - self->allowed_offset);
|
||||
fraction = (fraction > kMinFractionWhenPossiblyCausal
|
||||
? fraction
|
||||
: kMinFractionWhenPossiblyCausal);
|
||||
} else if (delay_difference < 0) {
|
||||
fraction =
|
||||
kMinFractionWhenPossiblyNonCausal - kFractionSlope * delay_difference;
|
||||
fraction = (fraction > 1.f ? 1.f : fraction);
|
||||
}
|
||||
histogram_threshold *= fraction;
|
||||
histogram_threshold =
|
||||
(histogram_threshold > kMinHistogramThreshold ? histogram_threshold
|
||||
: kMinHistogramThreshold);
|
||||
|
||||
is_histogram_valid =
|
||||
(self->histogram[candidate_delay] >= histogram_threshold) &&
|
||||
(self->candidate_hits > kMinRequiredHits);
|
||||
|
||||
return is_histogram_valid;
|
||||
}
|
||||
|
||||
// Performs a robust validation of the |candidate_delay| estimated in
|
||||
// WebRtc_ProcessBinarySpectrum(). The algorithm takes the
|
||||
// |is_instantaneous_valid| and the |is_histogram_valid| and combines them
|
||||
// into a robust validation. The HistogramBasedValidation() has to be called
|
||||
// prior to this call.
|
||||
// For further description on how the combination is done, see commented code.
|
||||
//
|
||||
// Inputs:
|
||||
// - candidate_delay : The delay to validate.
|
||||
// - is_instantaneous_valid : The instantaneous validation performed in
|
||||
// WebRtc_ProcessBinarySpectrum().
|
||||
// - is_histogram_valid : The histogram based validation.
|
||||
//
|
||||
// Return value:
|
||||
// - is_robust : 1 - The candidate_delay is valid according to a
|
||||
// combination of the two inputs.
|
||||
// : 0 - Otherwise.
|
||||
static int RobustValidation(const BinaryDelayEstimator *self,
|
||||
int candidate_delay,
|
||||
int is_instantaneous_valid,
|
||||
int is_histogram_valid) {
|
||||
int is_robust = 0;
|
||||
|
||||
// The final robust validation is based on the two algorithms; 1) the
|
||||
// |is_instantaneous_valid| and 2) the histogram based with result stored in
|
||||
// |is_histogram_valid|.
|
||||
// i) Before we actually have a valid estimate (|last_delay| == -2), we say
|
||||
// a candidate is valid if either algorithm states so
|
||||
// (|is_instantaneous_valid| OR |is_histogram_valid|).
|
||||
is_robust =
|
||||
(self->last_delay < 0) && (is_instantaneous_valid || is_histogram_valid);
|
||||
// ii) Otherwise, we need both algorithms to be certain
|
||||
// (|is_instantaneous_valid| AND |is_histogram_valid|)
|
||||
is_robust |= is_instantaneous_valid && is_histogram_valid;
|
||||
// iii) With one exception, i.e., the histogram based algorithm can overrule
|
||||
// the instantaneous one if |is_histogram_valid| = 1 and the histogram
|
||||
// is significantly strong.
|
||||
is_robust |= is_histogram_valid &&
|
||||
(self->histogram[candidate_delay] > self->last_delay_histogram);
|
||||
|
||||
return is_robust;
|
||||
}
|
||||
|
||||
void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend *self) {
|
||||
if (self == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
webrtc_free(self->binary_far_history);
|
||||
self->binary_far_history = NULL;
|
||||
|
||||
webrtc_free(self->far_bit_counts);
|
||||
self->far_bit_counts = NULL;
|
||||
|
||||
webrtc_free(self);
|
||||
}
|
||||
|
||||
BinaryDelayEstimatorFarend *WebRtc_CreateBinaryDelayEstimatorFarend(
|
||||
int history_size) {
|
||||
BinaryDelayEstimatorFarend *self = NULL;
|
||||
|
||||
if (history_size > 1) {
|
||||
// Sanity conditions fulfilled.
|
||||
self = (BinaryDelayEstimatorFarend *)(
|
||||
webrtc_malloc(sizeof(BinaryDelayEstimatorFarend)));
|
||||
}
|
||||
if (self == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
self->history_size = 0;
|
||||
self->binary_far_history = NULL;
|
||||
self->far_bit_counts = NULL;
|
||||
if (WebRtc_AllocateFarendBufferMemory(self, history_size) == 0) {
|
||||
WebRtc_FreeBinaryDelayEstimatorFarend(self);
|
||||
self = NULL;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
int WebRtc_AllocateFarendBufferMemory(BinaryDelayEstimatorFarend *self,
|
||||
int history_size) {
|
||||
RTC_DCHECK(self);
|
||||
// (Re-)Allocate memory for history buffers.
|
||||
self->binary_far_history = (uint32_t *)(
|
||||
realloc(self->binary_far_history,
|
||||
history_size * sizeof(*self->binary_far_history)));
|
||||
self->far_bit_counts = (int *)(realloc(
|
||||
self->far_bit_counts, history_size * sizeof(*self->far_bit_counts)));
|
||||
if ((self->binary_far_history == NULL) || (self->far_bit_counts == NULL)) {
|
||||
history_size = 0;
|
||||
}
|
||||
// Fill with zeros if we have expanded the buffers.
|
||||
if (history_size > self->history_size) {
|
||||
int size_diff = history_size - self->history_size;
|
||||
memset(&self->binary_far_history[self->history_size], 0,
|
||||
sizeof(*self->binary_far_history) * size_diff);
|
||||
memset(&self->far_bit_counts[self->history_size], 0,
|
||||
sizeof(*self->far_bit_counts) * size_diff);
|
||||
}
|
||||
self->history_size = history_size;
|
||||
|
||||
return self->history_size;
|
||||
}
|
||||
|
||||
void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend *self) {
|
||||
RTC_DCHECK(self);
|
||||
memset(self->binary_far_history, 0, sizeof(uint32_t) * self->history_size);
|
||||
memset(self->far_bit_counts, 0, sizeof(int) * self->history_size);
|
||||
}
|
||||
|
||||
void WebRtc_SoftResetBinaryDelayEstimatorFarend(
|
||||
BinaryDelayEstimatorFarend *self,
|
||||
int delay_shift) {
|
||||
int abs_shift = abs(delay_shift);
|
||||
int shift_size = 0;
|
||||
int dest_index = 0;
|
||||
int src_index = 0;
|
||||
int padding_index = 0;
|
||||
|
||||
RTC_DCHECK(self);
|
||||
shift_size = self->history_size - abs_shift;
|
||||
RTC_DCHECK_GT(shift_size, 0);
|
||||
if (delay_shift == 0) {
|
||||
return;
|
||||
} else if (delay_shift > 0) {
|
||||
dest_index = abs_shift;
|
||||
} else if (delay_shift < 0) {
|
||||
src_index = abs_shift;
|
||||
padding_index = shift_size;
|
||||
}
|
||||
|
||||
// Shift and zero pad buffers.
|
||||
memmove(&self->binary_far_history[dest_index],
|
||||
&self->binary_far_history[src_index],
|
||||
sizeof(*self->binary_far_history) * shift_size);
|
||||
memset(&self->binary_far_history[padding_index], 0,
|
||||
sizeof(*self->binary_far_history) * abs_shift);
|
||||
memmove(&self->far_bit_counts[dest_index], &self->far_bit_counts[src_index],
|
||||
sizeof(*self->far_bit_counts) * shift_size);
|
||||
memset(&self->far_bit_counts[padding_index], 0,
|
||||
sizeof(*self->far_bit_counts) * abs_shift);
|
||||
}
|
||||
|
||||
void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend *handle,
|
||||
uint32_t binary_far_spectrum) {
|
||||
RTC_DCHECK(handle);
|
||||
// Shift binary spectrum history and insert current |binary_far_spectrum|.
|
||||
memmove(&(handle->binary_far_history[1]), &(handle->binary_far_history[0]),
|
||||
(handle->history_size - 1) * sizeof(uint32_t));
|
||||
handle->binary_far_history[0] = binary_far_spectrum;
|
||||
|
||||
// Shift history of far-end binary spectrum bit counts and insert bit count
|
||||
// of current |binary_far_spectrum|.
|
||||
memmove(&(handle->far_bit_counts[1]), &(handle->far_bit_counts[0]),
|
||||
(handle->history_size - 1) * sizeof(int));
|
||||
handle->far_bit_counts[0] = BitCount(binary_far_spectrum);
|
||||
}
|
||||
|
||||
void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator *self) {
|
||||
if (self == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
webrtc_free(self->mean_bit_counts);
|
||||
self->mean_bit_counts = NULL;
|
||||
|
||||
webrtc_free(self->bit_counts);
|
||||
self->bit_counts = NULL;
|
||||
|
||||
webrtc_free(self->binary_near_history);
|
||||
self->binary_near_history = NULL;
|
||||
|
||||
webrtc_free(self->histogram);
|
||||
self->histogram = NULL;
|
||||
|
||||
// BinaryDelayEstimator does not have ownership of |farend|, hence we do not
|
||||
// free the memory here. That should be handled separately by the user.
|
||||
self->farend = NULL;
|
||||
|
||||
webrtc_free(self);
|
||||
}
|
||||
|
||||
BinaryDelayEstimator *WebRtc_CreateBinaryDelayEstimator(
|
||||
BinaryDelayEstimatorFarend *farend,
|
||||
int max_lookahead) {
|
||||
BinaryDelayEstimator *self = NULL;
|
||||
|
||||
if ((farend != NULL) && (max_lookahead >= 0)) {
|
||||
// Sanity conditions fulfilled.
|
||||
self = (BinaryDelayEstimator *)(
|
||||
webrtc_malloc(sizeof(BinaryDelayEstimator)));
|
||||
}
|
||||
if (self == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
self->farend = farend;
|
||||
self->near_history_size = max_lookahead + 1;
|
||||
self->history_size = 0;
|
||||
self->robust_validation_enabled = 0; // Disabled by default.
|
||||
self->allowed_offset = 0;
|
||||
|
||||
self->lookahead = max_lookahead;
|
||||
|
||||
// Allocate memory for spectrum and history buffers.
|
||||
self->mean_bit_counts = NULL;
|
||||
self->bit_counts = NULL;
|
||||
self->histogram = NULL;
|
||||
self->binary_near_history = (uint32_t *)(
|
||||
webrtc_malloc((max_lookahead + 1) * sizeof(*self->binary_near_history)));
|
||||
if (self->binary_near_history == NULL ||
|
||||
WebRtc_AllocateHistoryBufferMemory(self, farend->history_size) == 0) {
|
||||
WebRtc_FreeBinaryDelayEstimator(self);
|
||||
self = NULL;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
int WebRtc_AllocateHistoryBufferMemory(BinaryDelayEstimator *self,
|
||||
int history_size) {
|
||||
BinaryDelayEstimatorFarend *far = self->farend;
|
||||
// (Re-)Allocate memory for spectrum and history buffers.
|
||||
if (history_size != far->history_size) {
|
||||
// Only update far-end buffers if we need.
|
||||
history_size = WebRtc_AllocateFarendBufferMemory(far, history_size);
|
||||
}
|
||||
// The extra array element in |mean_bit_counts| and |histogram| is a dummy
|
||||
// element only used while |last_delay| == -2, i.e., before we have a valid
|
||||
// estimate.
|
||||
self->mean_bit_counts = (int32_t *)(
|
||||
realloc(self->mean_bit_counts,
|
||||
(history_size + 1) * sizeof(*self->mean_bit_counts)));
|
||||
self->bit_counts = (int32_t *)(
|
||||
realloc(self->bit_counts, history_size * sizeof(*self->bit_counts)));
|
||||
self->histogram = (float *)(
|
||||
realloc(self->histogram, (history_size + 1) * sizeof(*self->histogram)));
|
||||
|
||||
if ((self->mean_bit_counts == NULL) || (self->bit_counts == NULL) ||
|
||||
(self->histogram == NULL)) {
|
||||
history_size = 0;
|
||||
}
|
||||
// Fill with zeros if we have expanded the buffers.
|
||||
if (history_size > self->history_size) {
|
||||
int size_diff = history_size - self->history_size;
|
||||
memset(&self->mean_bit_counts[self->history_size], 0,
|
||||
sizeof(*self->mean_bit_counts) * size_diff);
|
||||
memset(&self->bit_counts[self->history_size], 0,
|
||||
sizeof(*self->bit_counts) * size_diff);
|
||||
memset(&self->histogram[self->history_size], 0,
|
||||
sizeof(*self->histogram) * size_diff);
|
||||
}
|
||||
self->history_size = history_size;
|
||||
|
||||
return self->history_size;
|
||||
}
|
||||
|
||||
void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator *self) {
|
||||
int i = 0;
|
||||
RTC_DCHECK(self);
|
||||
|
||||
memset(self->bit_counts, 0, sizeof(int32_t) * self->history_size);
|
||||
memset(self->binary_near_history, 0,
|
||||
sizeof(uint32_t) * self->near_history_size);
|
||||
for (i = 0; i <= self->history_size; ++i) {
|
||||
self->mean_bit_counts[i] = (20 << 9); // 20 in Q9.
|
||||
self->histogram[i] = 0.f;
|
||||
}
|
||||
self->minimum_probability = kMaxBitCountsQ9; // 32 in Q9.
|
||||
self->last_delay_probability = (int) kMaxBitCountsQ9; // 32 in Q9.
|
||||
|
||||
// Default return value if we're unable to estimate. -1 is used for errors.
|
||||
self->last_delay = -2;
|
||||
|
||||
self->last_candidate_delay = -2;
|
||||
self->compare_delay = self->history_size;
|
||||
self->candidate_hits = 0;
|
||||
self->last_delay_histogram = 0.f;
|
||||
}
|
||||
|
||||
int WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator *self,
|
||||
int delay_shift) {
|
||||
int lookahead = 0;
|
||||
RTC_DCHECK(self);
|
||||
lookahead = self->lookahead;
|
||||
self->lookahead -= delay_shift;
|
||||
if (self->lookahead < 0) {
|
||||
self->lookahead = 0;
|
||||
}
|
||||
if (self->lookahead > self->near_history_size - 1) {
|
||||
self->lookahead = self->near_history_size - 1;
|
||||
}
|
||||
return lookahead - self->lookahead;
|
||||
}
|
||||
|
||||
//模拟c++的std::any_of
|
||||
#if 1
|
||||
int any_of(int arr[], int size, int (*predicate)(int)) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (predicate(arr[i])) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int ret_predicate(int a)
|
||||
{
|
||||
return a > 0;
|
||||
}
|
||||
|
||||
int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator *self,
|
||||
uint32_t binary_near_spectrum) {
|
||||
int i = 0;
|
||||
int candidate_delay = -1;
|
||||
int valid_candidate = 0;
|
||||
|
||||
int32_t value_best_candidate = kMaxBitCountsQ9;
|
||||
int32_t value_worst_candidate = 0;
|
||||
int32_t valley_depth = 0;
|
||||
|
||||
RTC_DCHECK(self);
|
||||
if (self->farend->history_size != self->history_size) {
|
||||
// Non matching history sizes.
|
||||
return -1;
|
||||
}
|
||||
if (self->near_history_size > 1) {
|
||||
// If we apply lookahead, shift near-end binary spectrum history. Insert
|
||||
// current |binary_near_spectrum| and pull out the delayed one.
|
||||
memmove(&(self->binary_near_history[1]), &(self->binary_near_history[0]),
|
||||
(self->near_history_size - 1) * sizeof(uint32_t));
|
||||
self->binary_near_history[0] = binary_near_spectrum;
|
||||
binary_near_spectrum = self->binary_near_history[self->lookahead];
|
||||
}
|
||||
|
||||
// Compare with delayed spectra and store the |bit_counts| for each delay.
|
||||
BitCountComparison(binary_near_spectrum, self->farend->binary_far_history,
|
||||
self->history_size, self->bit_counts);
|
||||
|
||||
// Update |mean_bit_counts|, which is the smoothed version of |bit_counts|.
|
||||
for (i = 0; i < self->history_size; i++) {
|
||||
// |bit_counts| is constrained to [0, 32], meaning we can smooth with a
|
||||
// factor up to 2^26. We use Q9.
|
||||
int32_t bit_count = (self->bit_counts[i] << 9); // Q9.
|
||||
|
||||
// Update |mean_bit_counts| only when far-end signal has something to
|
||||
// contribute. If |far_bit_counts| is zero the far-end signal is weak and
|
||||
// we likely have a poor echo condition, hence don't update.
|
||||
if (self->farend->far_bit_counts[i] > 0) {
|
||||
// Make number of right shifts piecewise linear w.r.t. |far_bit_counts|.
|
||||
int shifts = kShiftsAtZero;
|
||||
shifts -= (kShiftsLinearSlope * self->farend->far_bit_counts[i]) >> 4;
|
||||
WebRtc_MeanEstimatorFix(bit_count, shifts, &(self->mean_bit_counts[i]));
|
||||
}
|
||||
}
|
||||
|
||||
// Find |candidate_delay|, |value_best_candidate| and |value_worst_candidate|
|
||||
// of |mean_bit_counts|.
|
||||
for (i = 0; i < self->history_size; i++) {
|
||||
if (self->mean_bit_counts[i] < value_best_candidate) {
|
||||
value_best_candidate = self->mean_bit_counts[i];
|
||||
candidate_delay = i;
|
||||
}
|
||||
if (self->mean_bit_counts[i] > value_worst_candidate) {
|
||||
value_worst_candidate = self->mean_bit_counts[i];
|
||||
}
|
||||
}
|
||||
valley_depth = value_worst_candidate - value_best_candidate;
|
||||
|
||||
// The |value_best_candidate| is a good indicator on the probability of
|
||||
// |candidate_delay| being an accurate delay (a small |value_best_candidate|
|
||||
// means a good binary match). In the following sections we make a decision
|
||||
// whether to update |last_delay| or not.
|
||||
// 1) If the difference bit counts between the best and the worst delay
|
||||
// candidates is too small we consider the situation to be unreliable and
|
||||
// don't update |last_delay|.
|
||||
// 2) If the situation is reliable we update |last_delay| if the value of the
|
||||
// best candidate delay has a value less than
|
||||
// i) an adaptive threshold |minimum_probability|, or
|
||||
// ii) this corresponding value |last_delay_probability|, but updated at
|
||||
// this time instant.
|
||||
|
||||
// Update |minimum_probability|.
|
||||
if ((self->minimum_probability > kProbabilityLowerLimit) &&
|
||||
(valley_depth > kProbabilityMinSpread)) {
|
||||
// The "hard" threshold can't be lower than 17 (in Q9).
|
||||
// The valley in the curve also has to be distinct, i.e., the
|
||||
// difference between |value_worst_candidate| and |value_best_candidate| has
|
||||
// to be large enough.
|
||||
int32_t threshold = value_best_candidate + kProbabilityOffset;
|
||||
if (threshold < kProbabilityLowerLimit) {
|
||||
threshold = kProbabilityLowerLimit;
|
||||
}
|
||||
if (self->minimum_probability > threshold) {
|
||||
self->minimum_probability = threshold;
|
||||
}
|
||||
}
|
||||
// Update |last_delay_probability|.
|
||||
// We use a Markov type model, i.e., a slowly increasing level over time.
|
||||
self->last_delay_probability++;
|
||||
// Validate |candidate_delay|. We have a reliable instantaneous delay
|
||||
// estimate if
|
||||
// 1) The valley is distinct enough (|valley_depth| > |kProbabilityOffset|)
|
||||
// and
|
||||
// 2) The depth of the valley is deep enough
|
||||
// (|value_best_candidate| < |minimum_probability|)
|
||||
// and deeper than the best estimate so far
|
||||
// (|value_best_candidate| < |last_delay_probability|)
|
||||
valid_candidate = ((valley_depth > kProbabilityOffset) &&
|
||||
((value_best_candidate < self->minimum_probability) ||
|
||||
(value_best_candidate < self->last_delay_probability)));
|
||||
|
||||
// Check for nonstationary farend signal.
|
||||
#if 0
|
||||
const bool non_stationary_farend =
|
||||
std::any_of(self->farend->far_bit_counts,
|
||||
self->farend->far_bit_counts + self->history_size,
|
||||
[](int a) { return a > 0; });
|
||||
#else
|
||||
const int non_stationary_farend = any_of(self->farend->far_bit_counts,self->history_size,ret_predicate);
|
||||
#endif
|
||||
|
||||
|
||||
if (non_stationary_farend) {
|
||||
// Only update the validation statistics when the farend is nonstationary
|
||||
// as the underlying estimates are otherwise frozen.
|
||||
UpdateRobustValidationStatistics(self, candidate_delay, valley_depth,
|
||||
value_best_candidate);
|
||||
}
|
||||
|
||||
if (self->robust_validation_enabled) {
|
||||
int is_histogram_valid = HistogramBasedValidation(self, candidate_delay);
|
||||
valid_candidate = RobustValidation(self, candidate_delay, valid_candidate,
|
||||
is_histogram_valid);
|
||||
}
|
||||
|
||||
// Only update the delay estimate when the farend is nonstationary and when
|
||||
// a valid delay candidate is available.
|
||||
if (non_stationary_farend && valid_candidate) {
|
||||
if (candidate_delay != self->last_delay) {
|
||||
self->last_delay_histogram =
|
||||
(self->histogram[candidate_delay] > kLastHistogramMax
|
||||
? kLastHistogramMax
|
||||
: self->histogram[candidate_delay]);
|
||||
// Adjust the histogram if we made a change to |last_delay|, though it was
|
||||
// not the most likely one according to the histogram.
|
||||
if (self->histogram[candidate_delay] <
|
||||
self->histogram[self->compare_delay]) {
|
||||
self->histogram[self->compare_delay] = self->histogram[candidate_delay];
|
||||
}
|
||||
}
|
||||
self->last_delay = candidate_delay;
|
||||
if (value_best_candidate < self->last_delay_probability) {
|
||||
self->last_delay_probability = value_best_candidate;
|
||||
}
|
||||
self->compare_delay = self->last_delay;
|
||||
}
|
||||
|
||||
return self->last_delay;
|
||||
}
|
||||
|
||||
int WebRtc_binary_last_delay(BinaryDelayEstimator *self) {
|
||||
RTC_DCHECK(self);
|
||||
return self->last_delay;
|
||||
}
|
||||
|
||||
float WebRtc_binary_last_delay_quality(BinaryDelayEstimator *self) {
|
||||
float quality = 0;
|
||||
RTC_DCHECK(self);
|
||||
|
||||
if (self->robust_validation_enabled) {
|
||||
// Simply a linear function of the histogram height at delay estimate.
|
||||
quality = self->histogram[self->compare_delay] / kHistogramMax;
|
||||
} else {
|
||||
// Note that |last_delay_probability| states how deep the minimum of the
|
||||
// cost function is, so it is rather an error probability.
|
||||
quality = (float) (kMaxBitCountsQ9 - self->last_delay_probability) /
|
||||
kMaxBitCountsQ9;
|
||||
if (quality < 0) {
|
||||
quality = 0;
|
||||
}
|
||||
}
|
||||
return quality;
|
||||
}
|
||||
|
||||
void WebRtc_MeanEstimatorFix(int32_t new_value,
|
||||
int factor,
|
||||
int32_t *mean_value) {
|
||||
int32_t diff = new_value - *mean_value;
|
||||
|
||||
// mean_new = mean_value + ((new_value - mean_value) >> factor);
|
||||
if (diff < 0) {
|
||||
diff = -((-diff) >> factor);
|
||||
} else {
|
||||
diff = (diff >> factor);
|
||||
}
|
||||
*mean_value += diff;
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
/*
|
||||
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
// Performs delay estimation on binary converted spectra.
|
||||
// The return value is 0 - OK and -1 - Error, unless otherwise stated.
|
||||
|
||||
#ifndef MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_
|
||||
#define MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "rtc_base/checks.h"
|
||||
|
||||
static const int32_t kMaxBitCountsQ9 = (32 << 9); // 32 matching bits in Q9.
|
||||
|
||||
typedef struct {
|
||||
// Pointer to bit counts.
|
||||
int *far_bit_counts;
|
||||
// Binary history variables.
|
||||
uint32_t *binary_far_history;
|
||||
int history_size;
|
||||
} BinaryDelayEstimatorFarend;
|
||||
|
||||
typedef struct {
|
||||
// Pointer to bit counts.
|
||||
int32_t *mean_bit_counts;
|
||||
// Array only used locally in ProcessBinarySpectrum() but whose size is
|
||||
// determined at run-time.
|
||||
int32_t *bit_counts;
|
||||
|
||||
// Binary history variables.
|
||||
uint32_t *binary_near_history;
|
||||
int near_history_size;
|
||||
int history_size;
|
||||
|
||||
// Delay estimation variables.
|
||||
int32_t minimum_probability;
|
||||
int last_delay_probability;
|
||||
|
||||
// Delay memory.
|
||||
int last_delay;
|
||||
|
||||
// Robust validation
|
||||
int robust_validation_enabled;
|
||||
int allowed_offset;
|
||||
int last_candidate_delay;
|
||||
int compare_delay;
|
||||
int candidate_hits;
|
||||
float *histogram;
|
||||
float last_delay_histogram;
|
||||
|
||||
// For dynamically changing the lookahead when using SoftReset...().
|
||||
int lookahead;
|
||||
|
||||
// Far-end binary spectrum history buffer etc.
|
||||
BinaryDelayEstimatorFarend *farend;
|
||||
} BinaryDelayEstimator;
|
||||
|
||||
// Releases the memory allocated by
|
||||
// WebRtc_CreateBinaryDelayEstimatorFarend(...).
|
||||
// Input:
|
||||
// - self : Pointer to the binary delay estimation far-end
|
||||
// instance which is the return value of
|
||||
// WebRtc_CreateBinaryDelayEstimatorFarend().
|
||||
//
|
||||
void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend *self);
|
||||
|
||||
// Allocates the memory needed by the far-end part of the binary delay
|
||||
// estimation. The memory needs to be initialized separately through
|
||||
// WebRtc_InitBinaryDelayEstimatorFarend(...).
|
||||
//
|
||||
// Inputs:
|
||||
// - history_size : Size of the far-end binary spectrum history.
|
||||
//
|
||||
// Return value:
|
||||
// - BinaryDelayEstimatorFarend*
|
||||
// : Created |handle|. If the memory can't be allocated
|
||||
// or if any of the input parameters are invalid NULL
|
||||
// is returned.
|
||||
//
|
||||
BinaryDelayEstimatorFarend *WebRtc_CreateBinaryDelayEstimatorFarend(
|
||||
int history_size);
|
||||
|
||||
// Re-allocates the buffers.
|
||||
//
|
||||
// Inputs:
|
||||
// - self : Pointer to the binary estimation far-end instance
|
||||
// which is the return value of
|
||||
// WebRtc_CreateBinaryDelayEstimatorFarend().
|
||||
// - history_size : Size of the far-end binary spectrum history.
|
||||
//
|
||||
// Return value:
|
||||
// - history_size : The history size allocated.
|
||||
int WebRtc_AllocateFarendBufferMemory(BinaryDelayEstimatorFarend *self,
|
||||
int history_size);
|
||||
|
||||
// Initializes the delay estimation far-end instance created with
|
||||
// WebRtc_CreateBinaryDelayEstimatorFarend(...).
|
||||
//
|
||||
// Input:
|
||||
// - self : Pointer to the delay estimation far-end instance.
|
||||
//
|
||||
// Output:
|
||||
// - self : Initialized far-end instance.
|
||||
//
|
||||
void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend *self);
|
||||
|
||||
// Soft resets the delay estimation far-end instance created with
|
||||
// WebRtc_CreateBinaryDelayEstimatorFarend(...).
|
||||
//
|
||||
// Input:
|
||||
// - delay_shift : The amount of blocks to shift history buffers.
|
||||
//
|
||||
void WebRtc_SoftResetBinaryDelayEstimatorFarend(
|
||||
BinaryDelayEstimatorFarend *self,
|
||||
int delay_shift);
|
||||
|
||||
// Adds the binary far-end spectrum to the internal far-end history buffer. This
|
||||
// spectrum is used as reference when calculating the delay using
|
||||
// WebRtc_ProcessBinarySpectrum().
|
||||
//
|
||||
// Inputs:
|
||||
// - self : Pointer to the delay estimation far-end
|
||||
// instance.
|
||||
// - binary_far_spectrum : Far-end binary spectrum.
|
||||
//
|
||||
// Output:
|
||||
// - self : Updated far-end instance.
|
||||
//
|
||||
void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend *self,
|
||||
uint32_t binary_far_spectrum);
|
||||
|
||||
// Releases the memory allocated by WebRtc_CreateBinaryDelayEstimator(...).
|
||||
//
|
||||
// Note that BinaryDelayEstimator utilizes BinaryDelayEstimatorFarend, but does
|
||||
// not take ownership of it, hence the BinaryDelayEstimator has to be torn down
|
||||
// before the far-end.
|
||||
//
|
||||
// Input:
|
||||
// - self : Pointer to the binary delay estimation instance
|
||||
// which is the return value of
|
||||
// WebRtc_CreateBinaryDelayEstimator().
|
||||
//
|
||||
void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator *self);
|
||||
|
||||
// Allocates the memory needed by the binary delay estimation. The memory needs
|
||||
// to be initialized separately through WebRtc_InitBinaryDelayEstimator(...).
|
||||
//
|
||||
// See WebRtc_CreateDelayEstimator(..) in delay_estimator_wrapper.c for detailed
|
||||
// description.
|
||||
BinaryDelayEstimator *WebRtc_CreateBinaryDelayEstimator(
|
||||
BinaryDelayEstimatorFarend *farend,
|
||||
int max_lookahead);
|
||||
|
||||
// Re-allocates |history_size| dependent buffers. The far-end buffers will be
|
||||
// updated at the same time if needed.
|
||||
//
|
||||
// Input:
|
||||
// - self : Pointer to the binary estimation instance which is
|
||||
// the return value of
|
||||
// WebRtc_CreateBinaryDelayEstimator().
|
||||
// - history_size : Size of the history buffers.
|
||||
//
|
||||
// Return value:
|
||||
// - history_size : The history size allocated.
|
||||
int WebRtc_AllocateHistoryBufferMemory(BinaryDelayEstimator *self,
|
||||
int history_size);
|
||||
|
||||
// Initializes the delay estimation instance created with
|
||||
// WebRtc_CreateBinaryDelayEstimator(...).
|
||||
//
|
||||
// Input:
|
||||
// - self : Pointer to the delay estimation instance.
|
||||
//
|
||||
// Output:
|
||||
// - self : Initialized instance.
|
||||
//
|
||||
void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator *self);
|
||||
|
||||
// Soft resets the delay estimation instance created with
|
||||
// WebRtc_CreateBinaryDelayEstimator(...).
|
||||
//
|
||||
// Input:
|
||||
// - delay_shift : The amount of blocks to shift history buffers.
|
||||
//
|
||||
// Return value:
|
||||
// - actual_shifts : The actual number of shifts performed.
|
||||
//
|
||||
int WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator *self,
|
||||
int delay_shift);
|
||||
|
||||
// Estimates and returns the delay between the binary far-end and binary near-
|
||||
// end spectra. It is assumed the binary far-end spectrum has been added using
|
||||
// WebRtc_AddBinaryFarSpectrum() prior to this call. The value will be offset by
|
||||
// the lookahead (i.e. the lookahead should be subtracted from the returned
|
||||
// value).
|
||||
//
|
||||
// Inputs:
|
||||
// - self : Pointer to the delay estimation instance.
|
||||
// - binary_near_spectrum : Near-end binary spectrum of the current block.
|
||||
//
|
||||
// Output:
|
||||
// - self : Updated instance.
|
||||
//
|
||||
// Return value:
|
||||
// - delay : >= 0 - Calculated delay value.
|
||||
// -2 - Insufficient data for estimation.
|
||||
//
|
||||
int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator *self,
|
||||
uint32_t binary_near_spectrum);
|
||||
|
||||
// Returns the last calculated delay updated by the function
|
||||
// WebRtc_ProcessBinarySpectrum(...).
|
||||
//
|
||||
// Input:
|
||||
// - self : Pointer to the delay estimation instance.
|
||||
//
|
||||
// Return value:
|
||||
// - delay : >= 0 - Last calculated delay value
|
||||
// -2 - Insufficient data for estimation.
|
||||
//
|
||||
int WebRtc_binary_last_delay(BinaryDelayEstimator *self);
|
||||
|
||||
// Returns the estimation quality of the last calculated delay updated by the
|
||||
// function WebRtc_ProcessBinarySpectrum(...). The estimation quality is a value
|
||||
// in the interval [0, 1]. The higher the value, the better the quality.
|
||||
//
|
||||
// Return value:
|
||||
// - delay_quality : >= 0 - Estimation quality of last calculated
|
||||
// delay value.
|
||||
float WebRtc_binary_last_delay_quality(BinaryDelayEstimator *self);
|
||||
|
||||
// Updates the |mean_value| recursively with a step size of 2^-|factor|. This
|
||||
// function is used internally in the Binary Delay Estimator as well as the
|
||||
// Fixed point wrapper.
|
||||
//
|
||||
// Inputs:
|
||||
// - new_value : The new value the mean should be updated with.
|
||||
// - factor : The step size, in number of right shifts.
|
||||
//
|
||||
// Input/Output:
|
||||
// - mean_value : Pointer to the mean value.
|
||||
//
|
||||
void WebRtc_MeanEstimatorFix(int32_t new_value,
|
||||
int factor,
|
||||
int32_t *mean_value);
|
||||
|
||||
|
||||
#endif // MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_H_
|
||||
@@ -0,0 +1,517 @@
|
||||
/*
|
||||
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "delay_estimator_wrapper.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "delay_estimator.h"
|
||||
|
||||
#include "signal_processing_library.h"
|
||||
|
||||
typedef union {
|
||||
float float_;
|
||||
int32_t int32_;
|
||||
} SpectrumType;
|
||||
|
||||
typedef struct {
|
||||
// Pointers to mean values of spectrum.
|
||||
SpectrumType *mean_far_spectrum;
|
||||
// |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;
|
||||
} DelayEstimator;
|
||||
// Only bit |kBandFirst| through bit |kBandLast| are processed and
|
||||
// |kBandFirst| - |kBandLast| must be < 32.
|
||||
enum {
|
||||
kBandFirst = 12
|
||||
};
|
||||
enum {
|
||||
kBandLast = 43
|
||||
};
|
||||
|
||||
static __inline uint32_t SetBit(uint32_t in, int pos) {
|
||||
uint32_t mask = (1 << pos);
|
||||
uint32_t out = (in | mask);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
// Calculates the mean recursively. Same version as WebRtc_MeanEstimatorFix(),
|
||||
// but for float.
|
||||
//
|
||||
// Inputs:
|
||||
// - new_value : New additional value.
|
||||
// - scale : Scale for smoothing (should be less than 1.0).
|
||||
//
|
||||
// Input/Output:
|
||||
// - mean_value : Pointer to the mean value for updating.
|
||||
//
|
||||
static void MeanEstimatorFloat(float new_value,
|
||||
float scale,
|
||||
float *mean_value) {
|
||||
RTC_DCHECK_LT(scale, 1.0f);
|
||||
*mean_value += (new_value - *mean_value) * scale;
|
||||
}
|
||||
|
||||
// Computes the binary spectrum by comparing the input |spectrum| with a
|
||||
// |threshold_spectrum|. Float and fixed point versions.
|
||||
//
|
||||
// Inputs:
|
||||
// - spectrum : Spectrum of which the binary spectrum should be
|
||||
// calculated.
|
||||
// - threshold_spectrum : Threshold spectrum with which the input
|
||||
// spectrum is compared.
|
||||
// Return:
|
||||
// - out : Binary spectrum.
|
||||
//
|
||||
static uint32_t BinarySpectrumFix(const uint16_t *spectrum,
|
||||
SpectrumType *threshold_spectrum,
|
||||
int q_domain,
|
||||
int *threshold_initialized) {
|
||||
int i = kBandFirst;
|
||||
uint32_t out = 0;
|
||||
|
||||
RTC_DCHECK_LT(q_domain, 16);
|
||||
|
||||
if (!(*threshold_initialized)) {
|
||||
// Set the |threshold_spectrum| to half the input |spectrum| as starting
|
||||
// value. This speeds up the convergence.
|
||||
for (i = kBandFirst; i <= kBandLast; i++) {
|
||||
if (spectrum[i] > 0) {
|
||||
// Convert input spectrum from Q(|q_domain|) to Q15.
|
||||
int32_t spectrum_q15 = ((int32_t) spectrum[i]) << (15 - q_domain);
|
||||
threshold_spectrum[i].int32_ = (spectrum_q15 >> 1);
|
||||
*threshold_initialized = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i = kBandFirst; i <= kBandLast; i++) {
|
||||
// Convert input spectrum from Q(|q_domain|) to Q15.
|
||||
int32_t spectrum_q15 = ((int32_t) spectrum[i]) << (15 - q_domain);
|
||||
// Update the |threshold_spectrum|.
|
||||
WebRtc_MeanEstimatorFix(spectrum_q15, 6, &(threshold_spectrum[i].int32_));
|
||||
// Convert |spectrum| at current frequency bin to a binary value.
|
||||
if (spectrum_q15 > threshold_spectrum[i].int32_) {
|
||||
out = SetBit(out, i - kBandFirst);
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
static uint32_t BinarySpectrumFloat(const float *spectrum,
|
||||
SpectrumType *threshold_spectrum,
|
||||
int *threshold_initialized) {
|
||||
int i = kBandFirst;
|
||||
uint32_t out = 0;
|
||||
const float kScale = 1 / 64.0;
|
||||
|
||||
if (!(*threshold_initialized)) {
|
||||
// Set the |threshold_spectrum| to half the input |spectrum| as starting
|
||||
// value. This speeds up the convergence.
|
||||
for (i = kBandFirst; i <= kBandLast; i++) {
|
||||
if (spectrum[i] > 0.0f) {
|
||||
threshold_spectrum[i].float_ = (spectrum[i] / 2);
|
||||
*threshold_initialized = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i = kBandFirst; i <= kBandLast; i++) {
|
||||
// Update the |threshold_spectrum|.
|
||||
MeanEstimatorFloat(spectrum[i], kScale, &(threshold_spectrum[i].float_));
|
||||
// Convert |spectrum| at current frequency bin to a binary value.
|
||||
if (spectrum[i] > threshold_spectrum[i].float_) {
|
||||
out = SetBit(out, i - kBandFirst);
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void WebRtc_FreeDelayEstimatorFarend(void *handle) {
|
||||
DelayEstimatorFarend *self = (DelayEstimatorFarend *) handle;
|
||||
|
||||
if (handle == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
webrtc_free(self->mean_far_spectrum);
|
||||
self->mean_far_spectrum = NULL;
|
||||
|
||||
WebRtc_FreeBinaryDelayEstimatorFarend(self->binary_farend);
|
||||
self->binary_farend = NULL;
|
||||
|
||||
webrtc_free(self);
|
||||
}
|
||||
|
||||
void *WebRtc_CreateDelayEstimatorFarend(int spectrum_size, int history_size) {
|
||||
DelayEstimatorFarend *self = NULL;
|
||||
|
||||
// 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, "");
|
||||
|
||||
if (spectrum_size >= kBandLast) {
|
||||
self = (DelayEstimatorFarend *)(
|
||||
webrtc_malloc(sizeof(DelayEstimatorFarend)));
|
||||
}
|
||||
|
||||
if (self != NULL) {
|
||||
int memory_fail = 0;
|
||||
|
||||
// Allocate memory for the binary far-end spectrum handling.
|
||||
self->binary_farend = WebRtc_CreateBinaryDelayEstimatorFarend(history_size);
|
||||
memory_fail |= (self->binary_farend == NULL);
|
||||
|
||||
// Allocate memory for spectrum buffers.
|
||||
self->mean_far_spectrum = (SpectrumType *)(
|
||||
webrtc_malloc(spectrum_size * sizeof(SpectrumType)));
|
||||
memory_fail |= (self->mean_far_spectrum == NULL);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void WebRtc_SoftResetDelayEstimatorFarend(void *handle, int delay_shift) {
|
||||
DelayEstimatorFarend *self = (DelayEstimatorFarend *) handle;
|
||||
RTC_DCHECK(self);
|
||||
WebRtc_SoftResetBinaryDelayEstimatorFarend(self->binary_farend, delay_shift);
|
||||
}
|
||||
|
||||
int WebRtc_AddFarSpectrumFix(void *handle,
|
||||
const 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,
|
||||
const 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;
|
||||
}
|
||||
|
||||
webrtc_free(self->mean_near_spectrum);
|
||||
self->mean_near_spectrum = NULL;
|
||||
|
||||
WebRtc_FreeBinaryDelayEstimator(self->binary_handle);
|
||||
self->binary_handle = NULL;
|
||||
|
||||
webrtc_free(self);
|
||||
}
|
||||
|
||||
void *WebRtc_CreateDelayEstimator(void *farend_handle, int max_lookahead) {
|
||||
DelayEstimator *self = NULL;
|
||||
DelayEstimatorFarend *farend = (DelayEstimatorFarend *) farend_handle;
|
||||
|
||||
if (farend_handle != NULL) {
|
||||
self = (DelayEstimator *)(webrtc_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, max_lookahead);
|
||||
memory_fail |= (self->binary_handle == NULL);
|
||||
|
||||
// Allocate memory for spectrum buffers.
|
||||
self->mean_near_spectrum = (SpectrumType *)(
|
||||
webrtc_malloc(farend->spectrum_size * sizeof(SpectrumType)));
|
||||
memory_fail |= (self->mean_near_spectrum == NULL);
|
||||
|
||||
self->spectrum_size = farend->spectrum_size;
|
||||
|
||||
if (memory_fail) {
|
||||
WebRtc_FreeDelayEstimator(self);
|
||||
self = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
int WebRtc_InitDelayEstimator(void *handle) {
|
||||
DelayEstimator *self = (DelayEstimator *) handle;
|
||||
|
||||
if (self == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Initialize binary delay estimator.
|
||||
WebRtc_InitBinaryDelayEstimator(self->binary_handle);
|
||||
|
||||
// Set averaged far and near end spectra to zero.
|
||||
memset(self->mean_near_spectrum, 0,
|
||||
sizeof(SpectrumType) * self->spectrum_size);
|
||||
// Reset initialization indicators.
|
||||
self->near_spectrum_initialized = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WebRtc_SoftResetDelayEstimator(void *handle, int delay_shift) {
|
||||
DelayEstimator *self = (DelayEstimator *) handle;
|
||||
RTC_DCHECK(self);
|
||||
return WebRtc_SoftResetBinaryDelayEstimator(self->binary_handle, delay_shift);
|
||||
}
|
||||
|
||||
int WebRtc_set_history_size(void *handle, int history_size) {
|
||||
DelayEstimator *self = (DelayEstimator *)(handle);
|
||||
|
||||
if ((self == NULL) || (history_size <= 1)) {
|
||||
return -1;
|
||||
}
|
||||
return WebRtc_AllocateHistoryBufferMemory(self->binary_handle, history_size);
|
||||
}
|
||||
|
||||
int WebRtc_history_size(const void *handle) {
|
||||
const DelayEstimator *self = (const DelayEstimator *)(handle);
|
||||
|
||||
if (self == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (self->binary_handle->farend->history_size !=
|
||||
self->binary_handle->history_size) {
|
||||
// Non matching history sizes.
|
||||
return -1;
|
||||
}
|
||||
return self->binary_handle->history_size;
|
||||
}
|
||||
|
||||
int WebRtc_set_lookahead(void *handle, int lookahead) {
|
||||
DelayEstimator *self = (DelayEstimator *) handle;
|
||||
RTC_DCHECK(self);
|
||||
RTC_DCHECK(self->binary_handle);
|
||||
if ((lookahead > self->binary_handle->near_history_size - 1) ||
|
||||
(lookahead < 0)) {
|
||||
return -1;
|
||||
}
|
||||
self->binary_handle->lookahead = lookahead;
|
||||
return self->binary_handle->lookahead;
|
||||
}
|
||||
|
||||
int WebRtc_lookahead(void *handle) {
|
||||
DelayEstimator *self = (DelayEstimator *) handle;
|
||||
RTC_DCHECK(self);
|
||||
RTC_DCHECK(self->binary_handle);
|
||||
return self->binary_handle->lookahead;
|
||||
}
|
||||
|
||||
int WebRtc_set_allowed_offset(void *handle, int allowed_offset) {
|
||||
DelayEstimator *self = (DelayEstimator *) handle;
|
||||
|
||||
if ((self == NULL) || (allowed_offset < 0)) {
|
||||
return -1;
|
||||
}
|
||||
self->binary_handle->allowed_offset = allowed_offset;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WebRtc_get_allowed_offset(const void *handle) {
|
||||
const DelayEstimator *self = (const DelayEstimator *) handle;
|
||||
|
||||
if (self == NULL) {
|
||||
return -1;
|
||||
}
|
||||
return self->binary_handle->allowed_offset;
|
||||
}
|
||||
|
||||
int WebRtc_enable_robust_validation(void *handle, int enable) {
|
||||
DelayEstimator *self = (DelayEstimator *) handle;
|
||||
|
||||
if (self == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if ((enable < 0) || (enable > 1)) {
|
||||
return -1;
|
||||
}
|
||||
RTC_DCHECK(self->binary_handle);
|
||||
self->binary_handle->robust_validation_enabled = enable;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WebRtc_is_robust_validation_enabled(const void *handle) {
|
||||
const DelayEstimator *self = (const DelayEstimator *) handle;
|
||||
|
||||
if (self == NULL) {
|
||||
return -1;
|
||||
}
|
||||
return self->binary_handle->robust_validation_enabled;
|
||||
}
|
||||
|
||||
int WebRtc_DelayEstimatorProcessFix(void *handle,
|
||||
const uint16_t *near_spectrum,
|
||||
int spectrum_size,
|
||||
int near_q) {
|
||||
DelayEstimator *self = (DelayEstimator *) handle;
|
||||
uint32_t binary_spectrum = 0;
|
||||
|
||||
if (self == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (near_spectrum == NULL) {
|
||||
// Empty near end spectrum.
|
||||
return -1;
|
||||
}
|
||||
if (spectrum_size != self->spectrum_size) {
|
||||
// Data sizes don't match.
|
||||
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_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,
|
||||
const float *near_spectrum,
|
||||
int spectrum_size) {
|
||||
DelayEstimator *self = (DelayEstimator *) handle;
|
||||
uint32_t binary_spectrum = 0;
|
||||
|
||||
if (self == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (near_spectrum == NULL) {
|
||||
// Empty near end spectrum.
|
||||
return -1;
|
||||
}
|
||||
if (spectrum_size != self->spectrum_size) {
|
||||
// Data sizes don't match.
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get binary spectrum.
|
||||
binary_spectrum = BinarySpectrumFloat(near_spectrum, self->mean_near_spectrum,
|
||||
&(self->near_spectrum_initialized));
|
||||
|
||||
return WebRtc_ProcessBinarySpectrum(self->binary_handle, binary_spectrum);
|
||||
}
|
||||
|
||||
int WebRtc_last_delay(void *handle) {
|
||||
DelayEstimator *self = (DelayEstimator *) handle;
|
||||
|
||||
if (self == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return WebRtc_binary_last_delay(self->binary_handle);
|
||||
}
|
||||
|
||||
float WebRtc_last_delay_quality(void *handle) {
|
||||
DelayEstimator *self = (DelayEstimator *) handle;
|
||||
RTC_DCHECK(self);
|
||||
return WebRtc_binary_last_delay_quality(self->binary_handle);
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
// Performs delay estimation on block by block basis.
|
||||
// The return value is 0 - OK and -1 - Error, unless otherwise stated.
|
||||
|
||||
#ifndef MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_WRAPPER_H_
|
||||
#define MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_WRAPPER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
// Releases the memory allocated by WebRtc_CreateDelayEstimatorFarend(...)
|
||||
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. A change in buffer
|
||||
// size can be forced with WebRtc_set_history_size().
|
||||
// Note that the maximum delay which can be estimated is
|
||||
// determined together with WebRtc_set_lookahead().
|
||||
//
|
||||
// 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(...)
|
||||
int WebRtc_InitDelayEstimatorFarend(void *handle);
|
||||
|
||||
// Soft resets the far-end part of the delay estimation instance returned by
|
||||
// WebRtc_CreateDelayEstimatorFarend(...).
|
||||
// Input:
|
||||
// - delay_shift : The amount of blocks to shift history buffers.
|
||||
void WebRtc_SoftResetDelayEstimatorFarend(void *handle, int delay_shift);
|
||||
|
||||
// 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:
|
||||
// - 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,
|
||||
const uint16_t *far_spectrum,
|
||||
int spectrum_size,
|
||||
int far_q);
|
||||
|
||||
// See WebRtc_AddFarSpectrumFix() for description.
|
||||
int WebRtc_AddFarSpectrumFloat(void *handle,
|
||||
const float *far_spectrum,
|
||||
int spectrum_size);
|
||||
|
||||
// Releases the memory allocated by WebRtc_CreateDelayEstimator(...)
|
||||
void WebRtc_FreeDelayEstimator(void *handle);
|
||||
|
||||
// Allocates the memory needed by the delay estimation. The memory needs to be
|
||||
// initialized separately through WebRtc_InitDelayEstimator(...).
|
||||
//
|
||||
// Inputs:
|
||||
// - 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.
|
||||
//
|
||||
// - max_lookahead : Maximum amount of non-causal lookahead allowed. The
|
||||
// actual amount of lookahead used can be controlled by
|
||||
// WebRtc_set_lookahead(...). The default |lookahead| is
|
||||
// set to |max_lookahead| at create time. Use
|
||||
// WebRtc_set_lookahead(...) before start if a different
|
||||
// value is desired.
|
||||
//
|
||||
// Using lookahead can detect cases in which a near-end
|
||||
// signal occurs before the corresponding far-end signal.
|
||||
// It will delay the estimate for the current block by an
|
||||
// equal amount, and the returned values will be offset
|
||||
// by it.
|
||||
//
|
||||
// A value of zero is the typical no-lookahead case.
|
||||
// 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| is set through
|
||||
// WebRtc_set_history_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(void *farend_handle, int max_lookahead);
|
||||
|
||||
// Initializes the delay estimation instance returned by
|
||||
// WebRtc_CreateDelayEstimator(...)
|
||||
int WebRtc_InitDelayEstimator(void *handle);
|
||||
|
||||
// Soft resets the delay estimation instance returned by
|
||||
// WebRtc_CreateDelayEstimator(...)
|
||||
// Input:
|
||||
// - delay_shift : The amount of blocks to shift history buffers.
|
||||
//
|
||||
// Return value:
|
||||
// - actual_shifts : The actual number of shifts performed.
|
||||
int WebRtc_SoftResetDelayEstimator(void *handle, int delay_shift);
|
||||
|
||||
// Sets the effective |history_size| used. Valid values from 2. We simply need
|
||||
// at least two delays to compare to perform an estimate. If |history_size| is
|
||||
// changed, buffers are reallocated filling in with zeros if necessary.
|
||||
// Note that changing the |history_size| affects both buffers in far-end and
|
||||
// near-end. Hence it is important to change all DelayEstimators that use the
|
||||
// same reference far-end, to the same |history_size| value.
|
||||
// Inputs:
|
||||
// - handle : Pointer to the delay estimation instance.
|
||||
// - history_size : Effective history size to be used.
|
||||
// Return value:
|
||||
// - new_history_size : The new history size used. If the memory was not able
|
||||
// to be allocated 0 is returned.
|
||||
int WebRtc_set_history_size(void *handle, int history_size);
|
||||
|
||||
// Returns the history_size currently used.
|
||||
// Input:
|
||||
// - handle : Pointer to the delay estimation instance.
|
||||
int WebRtc_history_size(const void *handle);
|
||||
|
||||
// Sets the amount of |lookahead| to use. Valid values are [0, max_lookahead]
|
||||
// where |max_lookahead| was set at create time through
|
||||
// WebRtc_CreateDelayEstimator(...).
|
||||
//
|
||||
// Input:
|
||||
// - handle : Pointer to the delay estimation instance.
|
||||
// - lookahead : The amount of lookahead to be used.
|
||||
//
|
||||
// Return value:
|
||||
// - new_lookahead : The actual amount of lookahead set, unless |handle| is
|
||||
// a NULL pointer or |lookahead| is invalid, for which an
|
||||
// error is returned.
|
||||
int WebRtc_set_lookahead(void *handle, int lookahead);
|
||||
|
||||
// Returns the amount of lookahead we currently use.
|
||||
// Input:
|
||||
// - handle : Pointer to the delay estimation instance.
|
||||
int WebRtc_lookahead(void *handle);
|
||||
|
||||
// Sets the |allowed_offset| used in the robust validation scheme. If the
|
||||
// delay estimator is used in an echo control component, this parameter is
|
||||
// related to the filter length. In principle |allowed_offset| should be set to
|
||||
// the echo control filter length minus the expected echo duration, i.e., the
|
||||
// delay offset the echo control can handle without quality regression. The
|
||||
// default value, used if not set manually, is zero. Note that |allowed_offset|
|
||||
// has to be non-negative.
|
||||
// Inputs:
|
||||
// - handle : Pointer to the delay estimation instance.
|
||||
// - allowed_offset : The amount of delay offset, measured in partitions,
|
||||
// the echo control filter can handle.
|
||||
int WebRtc_set_allowed_offset(void *handle, int allowed_offset);
|
||||
|
||||
// Returns the |allowed_offset| in number of partitions.
|
||||
int WebRtc_get_allowed_offset(const void *handle);
|
||||
|
||||
// Enables/Disables a robust validation functionality in the delay estimation.
|
||||
// This is by default set to disabled at create time. The state is preserved
|
||||
// over a reset.
|
||||
// Inputs:
|
||||
// - handle : Pointer to the delay estimation instance.
|
||||
// - enable : Enable (1) or disable (0) this feature.
|
||||
int WebRtc_enable_robust_validation(void *handle, int enable);
|
||||
|
||||
// Returns 1 if robust validation is enabled and 0 if disabled.
|
||||
int WebRtc_is_robust_validation_enabled(const void *handle);
|
||||
|
||||
// Estimates and returns the delay between the far-end and near-end blocks. The
|
||||
// value will be offset by the lookahead (i.e. the lookahead should be
|
||||
// subtracted from the returned value).
|
||||
// Inputs:
|
||||
// - handle : Pointer to the delay estimation instance.
|
||||
// - 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).
|
||||
// - near_q : The Q-domain of the near-end data.
|
||||
//
|
||||
// Output:
|
||||
// - handle : Updated instance.
|
||||
//
|
||||
// Return value:
|
||||
// - delay : >= 0 - Calculated delay value.
|
||||
// -1 - Error.
|
||||
// -2 - Insufficient data for estimation.
|
||||
int WebRtc_DelayEstimatorProcessFix(void *handle,
|
||||
const uint16_t *near_spectrum,
|
||||
int spectrum_size,
|
||||
int near_q);
|
||||
|
||||
// See WebRtc_DelayEstimatorProcessFix() for description.
|
||||
int WebRtc_DelayEstimatorProcessFloat(void *handle,
|
||||
const float *near_spectrum,
|
||||
int spectrum_size);
|
||||
|
||||
// Returns the last calculated delay updated by the function
|
||||
// WebRtc_DelayEstimatorProcess(...).
|
||||
//
|
||||
// Input:
|
||||
// - handle : Pointer to the delay estimation instance.
|
||||
//
|
||||
// Return value:
|
||||
// - delay : >= 0 - Last calculated delay value.
|
||||
// -1 - Error.
|
||||
// -2 - Insufficient data for estimation.
|
||||
int WebRtc_last_delay(void *handle);
|
||||
|
||||
// Returns the estimation quality/probability of the last calculated delay
|
||||
// updated by the function WebRtc_DelayEstimatorProcess(...). The estimation
|
||||
// quality is a value in the interval [0, 1]. The higher the value, the better
|
||||
// the quality.
|
||||
//
|
||||
// Return value:
|
||||
// - delay_quality : >= 0 - Estimation quality of last calculated delay.
|
||||
float WebRtc_last_delay_quality(void *handle);
|
||||
|
||||
|
||||
#endif // MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_WRAPPER_H_
|
||||
Reference in New Issue
Block a user