blob: 73c70b0c346b9067b0ca47978ce608927ed59602 [file] [log] [blame]
bjornv@google.comb47d4b22011-09-15 12:27:36 +00001/*
bjornv@webrtc.orgbfda85f2012-04-16 07:28:29 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
bjornv@google.comb47d4b22011-09-15 12:27:36 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/utility/delay_estimator.h"
bjornv@google.comb47d4b22011-09-15 12:27:36 +000012
bjornv@google.comb47d4b22011-09-15 12:27:36 +000013#include <stdlib.h>
14#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
peahb1fc54d2016-05-12 05:08:45 -070016#include <algorithm>
bjornv@google.comb47d4b22011-09-15 12:27:36 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
kwiberg9e2be5f2016-09-14 05:23:22 -070019
Per Åhgrene7175c92020-03-20 16:43:34 +010020namespace webrtc {
21
22namespace {
23
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000024// Number of right shifts for scaling is linearly depending on number of bits in
25// the far-end binary spectrum.
26static const int kShiftsAtZero = 13; // Right shifts at zero binary spectrum.
27static const int kShiftsLinearSlope = 3;
28
Yves Gerey665174f2018-06-19 15:03:05 +020029static const int32_t kProbabilityOffset = 1024; // 2 in Q9.
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000030static const int32_t kProbabilityLowerLimit = 8704; // 17 in Q9.
Yves Gerey665174f2018-06-19 15:03:05 +020031static const int32_t kProbabilityMinSpread = 2816; // 5.5 in Q9.
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000032
bjornv@webrtc.org5c645082013-12-16 10:57:53 +000033// Robust validation settings
34static const float kHistogramMax = 3000.f;
35static const float kLastHistogramMax = 250.f;
36static const float kMinHistogramThreshold = 1.5f;
37static const int kMinRequiredHits = 10;
38static const int kMaxHitsWhenPossiblyNonCausal = 10;
39static const int kMaxHitsWhenPossiblyCausal = 1000;
bjornv@webrtc.org5c645082013-12-16 10:57:53 +000040static const float kQ14Scaling = 1.f / (1 << 14); // Scaling by 2^14 to get Q0.
41static const float kFractionSlope = 0.05f;
42static const float kMinFractionWhenPossiblyCausal = 0.5f;
43static const float kMinFractionWhenPossiblyNonCausal = 0.25f;
44
Per Åhgrene7175c92020-03-20 16:43:34 +010045} // namespace
46
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000047// Counts and returns number of bits of a 32-bit word.
48static int BitCount(uint32_t u32) {
Yves Gerey665174f2018-06-19 15:03:05 +020049 uint32_t tmp =
50 u32 - ((u32 >> 1) & 033333333333) - ((u32 >> 2) & 011111111111);
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000051 tmp = ((tmp + (tmp >> 3)) & 030707070707);
52 tmp = (tmp + (tmp >> 6));
53 tmp = (tmp + (tmp >> 12) + (tmp >> 24)) & 077;
54
Yves Gerey665174f2018-06-19 15:03:05 +020055 return ((int)tmp);
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000056}
bjornv@google.comb47d4b22011-09-15 12:27:36 +000057
bjornv@google.comb47d4b22011-09-15 12:27:36 +000058// Compares the |binary_vector| with all rows of the |binary_matrix| and counts
59// per row the number of times they have the same value.
60//
61// Inputs:
62// - binary_vector : binary "vector" stored in a long
63// - binary_matrix : binary "matrix" stored as a vector of long
64// - matrix_size : size of binary "matrix"
65//
66// Output:
67// - bit_counts : "Vector" stored as a long, containing for each
68// row the number of times the matrix row and the
69// input vector have the same value
70//
71static void BitCountComparison(uint32_t binary_vector,
72 const uint32_t* binary_matrix,
73 int matrix_size,
74 int32_t* bit_counts) {
75 int n = 0;
bjornv@google.comb47d4b22011-09-15 12:27:36 +000076
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000077 // Compare |binary_vector| with all rows of the |binary_matrix|
bjornv@google.comb47d4b22011-09-15 12:27:36 +000078 for (; n < matrix_size; n++) {
Yves Gerey665174f2018-06-19 15:03:05 +020079 bit_counts[n] = (int32_t)BitCount(binary_vector ^ binary_matrix[n]);
bjornv@google.comb47d4b22011-09-15 12:27:36 +000080 }
81}
82
bjornv@webrtc.org5c645082013-12-16 10:57:53 +000083// Collects necessary statistics for the HistogramBasedValidation(). This
84// function has to be called prior to calling HistogramBasedValidation(). The
85// statistics updated and used by the HistogramBasedValidation() are:
86// 1. the number of |candidate_hits|, which states for how long we have had the
87// same |candidate_delay|
88// 2. the |histogram| of candidate delays over time. This histogram is
89// weighted with respect to a reliability measure and time-varying to cope
90// with possible delay shifts.
91// For further description see commented code.
92//
93// Inputs:
94// - candidate_delay : The delay to validate.
95// - valley_depth_q14 : The cost function has a valley/minimum at the
96// |candidate_delay| location. |valley_depth_q14| is the
97// cost function difference between the minimum and
98// maximum locations. The value is in the Q14 domain.
99// - valley_level_q14 : Is the cost function value at the minimum, in Q14.
100static void UpdateRobustValidationStatistics(BinaryDelayEstimator* self,
101 int candidate_delay,
102 int32_t valley_depth_q14,
103 int32_t valley_level_q14) {
104 const float valley_depth = valley_depth_q14 * kQ14Scaling;
105 float decrease_in_last_set = valley_depth;
Yves Gerey665174f2018-06-19 15:03:05 +0200106 const int max_hits_for_slow_change = (candidate_delay < self->last_delay)
107 ? kMaxHitsWhenPossiblyNonCausal
108 : kMaxHitsWhenPossiblyCausal;
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000109 int i = 0;
110
kwiberg9e2be5f2016-09-14 05:23:22 -0700111 RTC_DCHECK_EQ(self->history_size, self->farend->history_size);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000112 // Reset |candidate_hits| if we have a new candidate.
113 if (candidate_delay != self->last_candidate_delay) {
114 self->candidate_hits = 0;
115 self->last_candidate_delay = candidate_delay;
116 }
117 self->candidate_hits++;
118
119 // The |histogram| is updated differently across the bins.
120 // 1. The |candidate_delay| histogram bin is increased with the
121 // |valley_depth|, which is a simple measure of how reliable the
122 // |candidate_delay| is. The histogram is not increased above
123 // |kHistogramMax|.
124 self->histogram[candidate_delay] += valley_depth;
125 if (self->histogram[candidate_delay] > kHistogramMax) {
126 self->histogram[candidate_delay] = kHistogramMax;
127 }
128 // 2. The histogram bins in the neighborhood of |candidate_delay| are
129 // unaffected. The neighborhood is defined as x + {-2, -1, 0, 1}.
130 // 3. The histogram bins in the neighborhood of |last_delay| are decreased
131 // with |decrease_in_last_set|. This value equals the difference between
132 // the cost function values at the locations |candidate_delay| and
133 // |last_delay| until we reach |max_hits_for_slow_change| consecutive hits
134 // at the |candidate_delay|. If we exceed this amount of hits the
135 // |candidate_delay| is a "potential" candidate and we start decreasing
136 // these histogram bins more rapidly with |valley_depth|.
137 if (self->candidate_hits < max_hits_for_slow_change) {
Yves Gerey665174f2018-06-19 15:03:05 +0200138 decrease_in_last_set =
139 (self->mean_bit_counts[self->compare_delay] - valley_level_q14) *
140 kQ14Scaling;
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000141 }
142 // 4. All other bins are decreased with |valley_depth|.
143 // TODO(bjornv): Investigate how to make this loop more efficient. Split up
144 // the loop? Remove parts that doesn't add too much.
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000145 for (i = 0; i < self->history_size; ++i) {
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000146 int is_in_last_set = (i >= self->last_delay - 2) &&
Yves Gerey665174f2018-06-19 15:03:05 +0200147 (i <= self->last_delay + 1) && (i != candidate_delay);
148 int is_in_candidate_set =
149 (i >= candidate_delay - 2) && (i <= candidate_delay + 1);
150 self->histogram[i] -=
151 decrease_in_last_set * is_in_last_set +
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000152 valley_depth * (!is_in_last_set && !is_in_candidate_set);
153 // 5. No histogram bin can go below 0.
154 if (self->histogram[i] < 0) {
155 self->histogram[i] = 0;
156 }
157 }
158}
159
160// Validates the |candidate_delay|, estimated in WebRtc_ProcessBinarySpectrum(),
161// based on a mix of counting concurring hits with a modified histogram
162// of recent delay estimates. In brief a candidate is valid (returns 1) if it
163// is the most likely according to the histogram. There are a couple of
164// exceptions that are worth mentioning:
165// 1. If the |candidate_delay| < |last_delay| it can be that we are in a
166// non-causal state, breaking a possible echo control algorithm. Hence, we
167// open up for a quicker change by allowing the change even if the
168// |candidate_delay| is not the most likely one according to the histogram.
169// 2. There's a minimum number of hits (kMinRequiredHits) and the histogram
170// value has to reached a minimum (kMinHistogramThreshold) to be valid.
171// 3. The action is also depending on the filter length used for echo control.
172// If the delay difference is larger than what the filter can capture, we
173// also move quicker towards a change.
174// For further description see commented code.
175//
176// Input:
177// - candidate_delay : The delay to validate.
178//
179// Return value:
180// - is_histogram_valid : 1 - The |candidate_delay| is valid.
181// 0 - Otherwise.
182static int HistogramBasedValidation(const BinaryDelayEstimator* self,
183 int candidate_delay) {
184 float fraction = 1.f;
185 float histogram_threshold = self->histogram[self->compare_delay];
186 const int delay_difference = candidate_delay - self->last_delay;
187 int is_histogram_valid = 0;
188
189 // The histogram based validation of |candidate_delay| is done by comparing
190 // the |histogram| at bin |candidate_delay| with a |histogram_threshold|.
191 // This |histogram_threshold| equals a |fraction| of the |histogram| at bin
192 // |last_delay|. The |fraction| is a piecewise linear function of the
193 // |delay_difference| between the |candidate_delay| and the |last_delay|
194 // allowing for a quicker move if
195 // i) a potential echo control filter can not handle these large differences.
196 // ii) keeping |last_delay| instead of updating to |candidate_delay| could
197 // force an echo control into a non-causal state.
198 // We further require the histogram to have reached a minimum value of
199 // |kMinHistogramThreshold|. In addition, we also require the number of
200 // |candidate_hits| to be more than |kMinRequiredHits| to remove spurious
201 // values.
202
203 // Calculate a comparison histogram value (|histogram_threshold|) that is
204 // depending on the distance between the |candidate_delay| and |last_delay|.
205 // TODO(bjornv): How much can we gain by turning the fraction calculation
206 // into tables?
bjornv@webrtc.orgbccd53d2014-01-08 08:18:15 +0000207 if (delay_difference > self->allowed_offset) {
208 fraction = 1.f - kFractionSlope * (delay_difference - self->allowed_offset);
Yves Gerey665174f2018-06-19 15:03:05 +0200209 fraction = (fraction > kMinFractionWhenPossiblyCausal
210 ? fraction
211 : kMinFractionWhenPossiblyCausal);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000212 } else if (delay_difference < 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200213 fraction =
214 kMinFractionWhenPossiblyNonCausal - kFractionSlope * delay_difference;
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000215 fraction = (fraction > 1.f ? 1.f : fraction);
216 }
217 histogram_threshold *= fraction;
Yves Gerey665174f2018-06-19 15:03:05 +0200218 histogram_threshold =
219 (histogram_threshold > kMinHistogramThreshold ? histogram_threshold
220 : kMinHistogramThreshold);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000221
222 is_histogram_valid =
223 (self->histogram[candidate_delay] >= histogram_threshold) &&
224 (self->candidate_hits > kMinRequiredHits);
225
226 return is_histogram_valid;
227}
228
229// Performs a robust validation of the |candidate_delay| estimated in
230// WebRtc_ProcessBinarySpectrum(). The algorithm takes the
231// |is_instantaneous_valid| and the |is_histogram_valid| and combines them
232// into a robust validation. The HistogramBasedValidation() has to be called
233// prior to this call.
234// For further description on how the combination is done, see commented code.
235//
236// Inputs:
237// - candidate_delay : The delay to validate.
238// - is_instantaneous_valid : The instantaneous validation performed in
239// WebRtc_ProcessBinarySpectrum().
240// - is_histogram_valid : The histogram based validation.
241//
242// Return value:
243// - is_robust : 1 - The candidate_delay is valid according to a
244// combination of the two inputs.
245// : 0 - Otherwise.
246static int RobustValidation(const BinaryDelayEstimator* self,
247 int candidate_delay,
248 int is_instantaneous_valid,
249 int is_histogram_valid) {
250 int is_robust = 0;
251
252 // The final robust validation is based on the two algorithms; 1) the
253 // |is_instantaneous_valid| and 2) the histogram based with result stored in
254 // |is_histogram_valid|.
255 // i) Before we actually have a valid estimate (|last_delay| == -2), we say
256 // a candidate is valid if either algorithm states so
257 // (|is_instantaneous_valid| OR |is_histogram_valid|).
Yves Gerey665174f2018-06-19 15:03:05 +0200258 is_robust =
259 (self->last_delay < 0) && (is_instantaneous_valid || is_histogram_valid);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000260 // ii) Otherwise, we need both algorithms to be certain
261 // (|is_instantaneous_valid| AND |is_histogram_valid|)
262 is_robust |= is_instantaneous_valid && is_histogram_valid;
263 // iii) With one exception, i.e., the histogram based algorithm can overrule
264 // the instantaneous one if |is_histogram_valid| = 1 and the histogram
265 // is significantly strong.
266 is_robust |= is_histogram_valid &&
Yves Gerey665174f2018-06-19 15:03:05 +0200267 (self->histogram[candidate_delay] > self->last_delay_histogram);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000268
269 return is_robust;
270}
271
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000272void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self) {
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000273 if (self == NULL) {
274 return;
275 }
276
277 free(self->binary_far_history);
278 self->binary_far_history = NULL;
279
280 free(self->far_bit_counts);
281 self->far_bit_counts = NULL;
282
283 free(self);
284}
285
286BinaryDelayEstimatorFarend* WebRtc_CreateBinaryDelayEstimatorFarend(
287 int history_size) {
bjornv@webrtc.org7ded92b2013-01-30 16:16:59 +0000288 BinaryDelayEstimatorFarend* self = NULL;
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000289
bjornv@webrtc.org7ded92b2013-01-30 16:16:59 +0000290 if (history_size > 1) {
291 // Sanity conditions fulfilled.
peahbdb7af62016-04-12 14:47:40 -0700292 self = static_cast<BinaryDelayEstimatorFarend*>(
293 malloc(sizeof(BinaryDelayEstimatorFarend)));
bjornv@webrtc.org7ded92b2013-01-30 16:16:59 +0000294 }
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000295 if (self == NULL) {
296 return NULL;
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000297 }
298
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000299 self->history_size = 0;
300 self->binary_far_history = NULL;
301 self->far_bit_counts = NULL;
302 if (WebRtc_AllocateFarendBufferMemory(self, history_size) == 0) {
303 WebRtc_FreeBinaryDelayEstimatorFarend(self);
304 self = NULL;
305 }
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000306 return self;
307}
308
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000309int WebRtc_AllocateFarendBufferMemory(BinaryDelayEstimatorFarend* self,
310 int history_size) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700311 RTC_DCHECK(self);
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000312 // (Re-)Allocate memory for history buffers.
peahbdb7af62016-04-12 14:47:40 -0700313 self->binary_far_history = static_cast<uint32_t*>(
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000314 realloc(self->binary_far_history,
peahbdb7af62016-04-12 14:47:40 -0700315 history_size * sizeof(*self->binary_far_history)));
Yves Gerey665174f2018-06-19 15:03:05 +0200316 self->far_bit_counts = static_cast<int*>(realloc(
317 self->far_bit_counts, history_size * sizeof(*self->far_bit_counts)));
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000318 if ((self->binary_far_history == NULL) || (self->far_bit_counts == NULL)) {
319 history_size = 0;
320 }
321 // Fill with zeros if we have expanded the buffers.
322 if (history_size > self->history_size) {
323 int size_diff = history_size - self->history_size;
Yves Gerey665174f2018-06-19 15:03:05 +0200324 memset(&self->binary_far_history[self->history_size], 0,
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000325 sizeof(*self->binary_far_history) * size_diff);
Yves Gerey665174f2018-06-19 15:03:05 +0200326 memset(&self->far_bit_counts[self->history_size], 0,
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000327 sizeof(*self->far_bit_counts) * size_diff);
328 }
329 self->history_size = history_size;
330
331 return self->history_size;
332}
333
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000334void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700335 RTC_DCHECK(self);
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000336 memset(self->binary_far_history, 0, sizeof(uint32_t) * self->history_size);
337 memset(self->far_bit_counts, 0, sizeof(int) * self->history_size);
338}
339
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000340void WebRtc_SoftResetBinaryDelayEstimatorFarend(
Yves Gerey665174f2018-06-19 15:03:05 +0200341 BinaryDelayEstimatorFarend* self,
342 int delay_shift) {
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000343 int abs_shift = abs(delay_shift);
344 int shift_size = 0;
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000345 int dest_index = 0;
346 int src_index = 0;
347 int padding_index = 0;
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000348
kwiberg9e2be5f2016-09-14 05:23:22 -0700349 RTC_DCHECK(self);
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000350 shift_size = self->history_size - abs_shift;
kwiberg9e2be5f2016-09-14 05:23:22 -0700351 RTC_DCHECK_GT(shift_size, 0);
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000352 if (delay_shift == 0) {
353 return;
354 } else if (delay_shift > 0) {
355 dest_index = abs_shift;
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000356 } else if (delay_shift < 0) {
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000357 src_index = abs_shift;
358 padding_index = shift_size;
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000359 }
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000360
361 // Shift and zero pad buffers.
362 memmove(&self->binary_far_history[dest_index],
363 &self->binary_far_history[src_index],
364 sizeof(*self->binary_far_history) * shift_size);
365 memset(&self->binary_far_history[padding_index], 0,
366 sizeof(*self->binary_far_history) * abs_shift);
Yves Gerey665174f2018-06-19 15:03:05 +0200367 memmove(&self->far_bit_counts[dest_index], &self->far_bit_counts[src_index],
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000368 sizeof(*self->far_bit_counts) * shift_size);
369 memset(&self->far_bit_counts[padding_index], 0,
370 sizeof(*self->far_bit_counts) * abs_shift);
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000371}
372
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000373void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend* handle,
374 uint32_t binary_far_spectrum) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700375 RTC_DCHECK(handle);
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000376 // Shift binary spectrum history and insert current |binary_far_spectrum|.
377 memmove(&(handle->binary_far_history[1]), &(handle->binary_far_history[0]),
378 (handle->history_size - 1) * sizeof(uint32_t));
379 handle->binary_far_history[0] = binary_far_spectrum;
380
381 // Shift history of far-end binary spectrum bit counts and insert bit count
382 // of current |binary_far_spectrum|.
383 memmove(&(handle->far_bit_counts[1]), &(handle->far_bit_counts[0]),
384 (handle->history_size - 1) * sizeof(int));
385 handle->far_bit_counts[0] = BitCount(binary_far_spectrum);
386}
387
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000388void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator* self) {
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000389 if (self == NULL) {
bjornv@webrtc.orgbfda85f2012-04-16 07:28:29 +0000390 return;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000391 }
bjornv@webrtc.orgbfda85f2012-04-16 07:28:29 +0000392
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000393 free(self->mean_bit_counts);
394 self->mean_bit_counts = NULL;
bjornv@webrtc.orgbfda85f2012-04-16 07:28:29 +0000395
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000396 free(self->bit_counts);
397 self->bit_counts = NULL;
bjornv@webrtc.orgbfda85f2012-04-16 07:28:29 +0000398
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000399 free(self->binary_near_history);
400 self->binary_near_history = NULL;
bjornv@webrtc.orgbfda85f2012-04-16 07:28:29 +0000401
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000402 free(self->histogram);
403 self->histogram = NULL;
404
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000405 // BinaryDelayEstimator does not have ownership of |farend|, hence we do not
406 // free the memory here. That should be handled separately by the user.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000407 self->farend = NULL;
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000408
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000409 free(self);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000410}
411
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000412BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(
Yves Gerey665174f2018-06-19 15:03:05 +0200413 BinaryDelayEstimatorFarend* farend,
414 int max_lookahead) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000415 BinaryDelayEstimator* self = NULL;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000416
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000417 if ((farend != NULL) && (max_lookahead >= 0)) {
bjornv@webrtc.org2e729762012-04-18 08:30:29 +0000418 // Sanity conditions fulfilled.
peahbdb7af62016-04-12 14:47:40 -0700419 self = static_cast<BinaryDelayEstimator*>(
420 malloc(sizeof(BinaryDelayEstimator)));
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000421 }
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000422 if (self == NULL) {
423 return NULL;
424 }
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000425
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000426 self->farend = farend;
427 self->near_history_size = max_lookahead + 1;
428 self->history_size = 0;
429 self->robust_validation_enabled = 0; // Disabled by default.
430 self->allowed_offset = 0;
bjornv@webrtc.org2e729762012-04-18 08:30:29 +0000431
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000432 self->lookahead = max_lookahead;
bjornv@webrtc.org2e729762012-04-18 08:30:29 +0000433
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000434 // Allocate memory for spectrum and history buffers.
435 self->mean_bit_counts = NULL;
436 self->bit_counts = NULL;
437 self->histogram = NULL;
peahbdb7af62016-04-12 14:47:40 -0700438 self->binary_near_history = static_cast<uint32_t*>(
439 malloc((max_lookahead + 1) * sizeof(*self->binary_near_history)));
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000440 if (self->binary_near_history == NULL ||
441 WebRtc_AllocateHistoryBufferMemory(self, farend->history_size) == 0) {
442 WebRtc_FreeBinaryDelayEstimator(self);
443 self = NULL;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000444 }
445
bjornv@webrtc.org2e729762012-04-18 08:30:29 +0000446 return self;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000447}
448
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000449int WebRtc_AllocateHistoryBufferMemory(BinaryDelayEstimator* self,
450 int history_size) {
451 BinaryDelayEstimatorFarend* far = self->farend;
452 // (Re-)Allocate memory for spectrum and history buffers.
453 if (history_size != far->history_size) {
454 // Only update far-end buffers if we need.
455 history_size = WebRtc_AllocateFarendBufferMemory(far, history_size);
456 }
457 // The extra array element in |mean_bit_counts| and |histogram| is a dummy
458 // element only used while |last_delay| == -2, i.e., before we have a valid
459 // estimate.
peahbdb7af62016-04-12 14:47:40 -0700460 self->mean_bit_counts = static_cast<int32_t*>(
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000461 realloc(self->mean_bit_counts,
peahbdb7af62016-04-12 14:47:40 -0700462 (history_size + 1) * sizeof(*self->mean_bit_counts)));
463 self->bit_counts = static_cast<int32_t*>(
464 realloc(self->bit_counts, history_size * sizeof(*self->bit_counts)));
465 self->histogram = static_cast<float*>(
466 realloc(self->histogram, (history_size + 1) * sizeof(*self->histogram)));
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000467
Yves Gerey665174f2018-06-19 15:03:05 +0200468 if ((self->mean_bit_counts == NULL) || (self->bit_counts == NULL) ||
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000469 (self->histogram == NULL)) {
470 history_size = 0;
471 }
472 // Fill with zeros if we have expanded the buffers.
473 if (history_size > self->history_size) {
474 int size_diff = history_size - self->history_size;
Yves Gerey665174f2018-06-19 15:03:05 +0200475 memset(&self->mean_bit_counts[self->history_size], 0,
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000476 sizeof(*self->mean_bit_counts) * size_diff);
Yves Gerey665174f2018-06-19 15:03:05 +0200477 memset(&self->bit_counts[self->history_size], 0,
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000478 sizeof(*self->bit_counts) * size_diff);
Yves Gerey665174f2018-06-19 15:03:05 +0200479 memset(&self->histogram[self->history_size], 0,
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000480 sizeof(*self->histogram) * size_diff);
481 }
482 self->history_size = history_size;
483
484 return self->history_size;
485}
486
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000487void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator* self) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000488 int i = 0;
kwiberg9e2be5f2016-09-14 05:23:22 -0700489 RTC_DCHECK(self);
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000490
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000491 memset(self->bit_counts, 0, sizeof(int32_t) * self->history_size);
Yves Gerey665174f2018-06-19 15:03:05 +0200492 memset(self->binary_near_history, 0,
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000493 sizeof(uint32_t) * self->near_history_size);
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000494 for (i = 0; i <= self->history_size; ++i) {
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000495 self->mean_bit_counts[i] = (20 << 9); // 20 in Q9.
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000496 self->histogram[i] = 0.f;
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000497 }
Yves Gerey665174f2018-06-19 15:03:05 +0200498 self->minimum_probability = kMaxBitCountsQ9; // 32 in Q9.
499 self->last_delay_probability = (int)kMaxBitCountsQ9; // 32 in Q9.
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000500
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000501 // Default return value if we're unable to estimate. -1 is used for errors.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000502 self->last_delay = -2;
bjornv@webrtc.orgbd41a842013-11-28 14:58:35 +0000503
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000504 self->last_candidate_delay = -2;
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000505 self->compare_delay = self->history_size;
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000506 self->candidate_hits = 0;
507 self->last_delay_histogram = 0.f;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000508}
509
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000510int WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator* self,
511 int delay_shift) {
512 int lookahead = 0;
kwiberg9e2be5f2016-09-14 05:23:22 -0700513 RTC_DCHECK(self);
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000514 lookahead = self->lookahead;
515 self->lookahead -= delay_shift;
516 if (self->lookahead < 0) {
517 self->lookahead = 0;
518 }
519 if (self->lookahead > self->near_history_size - 1) {
520 self->lookahead = self->near_history_size - 1;
521 }
522 return lookahead - self->lookahead;
523}
524
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000525int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* self,
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000526 uint32_t binary_near_spectrum) {
527 int i = 0;
528 int candidate_delay = -1;
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000529 int valid_candidate = 0;
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000530
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000531 int32_t value_best_candidate = kMaxBitCountsQ9;
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000532 int32_t value_worst_candidate = 0;
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000533 int32_t valley_depth = 0;
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000534
kwiberg9e2be5f2016-09-14 05:23:22 -0700535 RTC_DCHECK(self);
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000536 if (self->farend->history_size != self->history_size) {
537 // Non matching history sizes.
538 return -1;
539 }
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000540 if (self->near_history_size > 1) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000541 // If we apply lookahead, shift near-end binary spectrum history. Insert
542 // current |binary_near_spectrum| and pull out the delayed one.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000543 memmove(&(self->binary_near_history[1]), &(self->binary_near_history[0]),
544 (self->near_history_size - 1) * sizeof(uint32_t));
545 self->binary_near_history[0] = binary_near_spectrum;
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000546 binary_near_spectrum = self->binary_near_history[self->lookahead];
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000547 }
548
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000549 // Compare with delayed spectra and store the |bit_counts| for each delay.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000550 BitCountComparison(binary_near_spectrum, self->farend->binary_far_history,
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000551 self->history_size, self->bit_counts);
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000552
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000553 // Update |mean_bit_counts|, which is the smoothed version of |bit_counts|.
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000554 for (i = 0; i < self->history_size; i++) {
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000555 // |bit_counts| is constrained to [0, 32], meaning we can smooth with a
556 // factor up to 2^26. We use Q9.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000557 int32_t bit_count = (self->bit_counts[i] << 9); // Q9.
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000558
559 // Update |mean_bit_counts| only when far-end signal has something to
560 // contribute. If |far_bit_counts| is zero the far-end signal is weak and
561 // we likely have a poor echo condition, hence don't update.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000562 if (self->farend->far_bit_counts[i] > 0) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000563 // Make number of right shifts piecewise linear w.r.t. |far_bit_counts|.
564 int shifts = kShiftsAtZero;
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000565 shifts -= (kShiftsLinearSlope * self->farend->far_bit_counts[i]) >> 4;
566 WebRtc_MeanEstimatorFix(bit_count, shifts, &(self->mean_bit_counts[i]));
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000567 }
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000568 }
569
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000570 // Find |candidate_delay|, |value_best_candidate| and |value_worst_candidate|
571 // of |mean_bit_counts|.
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000572 for (i = 0; i < self->history_size; i++) {
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000573 if (self->mean_bit_counts[i] < value_best_candidate) {
574 value_best_candidate = self->mean_bit_counts[i];
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000575 candidate_delay = i;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000576 }
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000577 if (self->mean_bit_counts[i] > value_worst_candidate) {
578 value_worst_candidate = self->mean_bit_counts[i];
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000579 }
580 }
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000581 valley_depth = value_worst_candidate - value_best_candidate;
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000582
583 // The |value_best_candidate| is a good indicator on the probability of
584 // |candidate_delay| being an accurate delay (a small |value_best_candidate|
585 // means a good binary match). In the following sections we make a decision
586 // whether to update |last_delay| or not.
587 // 1) If the difference bit counts between the best and the worst delay
588 // candidates is too small we consider the situation to be unreliable and
589 // don't update |last_delay|.
590 // 2) If the situation is reliable we update |last_delay| if the value of the
591 // best candidate delay has a value less than
592 // i) an adaptive threshold |minimum_probability|, or
593 // ii) this corresponding value |last_delay_probability|, but updated at
594 // this time instant.
595
596 // Update |minimum_probability|.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000597 if ((self->minimum_probability > kProbabilityLowerLimit) &&
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000598 (valley_depth > kProbabilityMinSpread)) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000599 // The "hard" threshold can't be lower than 17 (in Q9).
600 // The valley in the curve also has to be distinct, i.e., the
601 // difference between |value_worst_candidate| and |value_best_candidate| has
602 // to be large enough.
603 int32_t threshold = value_best_candidate + kProbabilityOffset;
604 if (threshold < kProbabilityLowerLimit) {
605 threshold = kProbabilityLowerLimit;
606 }
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000607 if (self->minimum_probability > threshold) {
608 self->minimum_probability = threshold;
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000609 }
610 }
611 // Update |last_delay_probability|.
612 // We use a Markov type model, i.e., a slowly increasing level over time.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000613 self->last_delay_probability++;
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000614 // Validate |candidate_delay|. We have a reliable instantaneous delay
615 // estimate if
616 // 1) The valley is distinct enough (|valley_depth| > |kProbabilityOffset|)
617 // and
618 // 2) The depth of the valley is deep enough
619 // (|value_best_candidate| < |minimum_probability|)
620 // and deeper than the best estimate so far
621 // (|value_best_candidate| < |last_delay_probability|)
622 valid_candidate = ((valley_depth > kProbabilityOffset) &&
Yves Gerey665174f2018-06-19 15:03:05 +0200623 ((value_best_candidate < self->minimum_probability) ||
624 (value_best_candidate < self->last_delay_probability)));
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000625
peahb1fc54d2016-05-12 05:08:45 -0700626 // Check for nonstationary farend signal.
627 const bool non_stationary_farend =
628 std::any_of(self->farend->far_bit_counts,
629 self->farend->far_bit_counts + self->history_size,
630 [](int a) { return a > 0; });
631
632 if (non_stationary_farend) {
633 // Only update the validation statistics when the farend is nonstationary
634 // as the underlying estimates are otherwise frozen.
635 UpdateRobustValidationStatistics(self, candidate_delay, valley_depth,
636 value_best_candidate);
637 }
638
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000639 if (self->robust_validation_enabled) {
Bjorn Volcker532531b2015-05-06 11:58:04 +0200640 int is_histogram_valid = HistogramBasedValidation(self, candidate_delay);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000641 valid_candidate = RobustValidation(self, candidate_delay, valid_candidate,
642 is_histogram_valid);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000643 }
peahb1fc54d2016-05-12 05:08:45 -0700644
645 // Only update the delay estimate when the farend is nonstationary and when
646 // a valid delay candidate is available.
647 if (non_stationary_farend && valid_candidate) {
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000648 if (candidate_delay != self->last_delay) {
bjornv@webrtc.org1e7d6122013-12-16 13:37:28 +0000649 self->last_delay_histogram =
Yves Gerey665174f2018-06-19 15:03:05 +0200650 (self->histogram[candidate_delay] > kLastHistogramMax
651 ? kLastHistogramMax
652 : self->histogram[candidate_delay]);
bjornv@webrtc.org1e7d6122013-12-16 13:37:28 +0000653 // Adjust the histogram if we made a change to |last_delay|, though it was
654 // not the most likely one according to the histogram.
655 if (self->histogram[candidate_delay] <
656 self->histogram[self->compare_delay]) {
657 self->histogram[self->compare_delay] = self->histogram[candidate_delay];
658 }
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000659 }
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000660 self->last_delay = candidate_delay;
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000661 if (value_best_candidate < self->last_delay_probability) {
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000662 self->last_delay_probability = value_best_candidate;
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000663 }
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000664 self->compare_delay = self->last_delay;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000665 }
666
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000667 return self->last_delay;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000668}
669
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000670int WebRtc_binary_last_delay(BinaryDelayEstimator* self) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700671 RTC_DCHECK(self);
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000672 return self->last_delay;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000673}
674
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000675float WebRtc_binary_last_delay_quality(BinaryDelayEstimator* self) {
676 float quality = 0;
kwiberg9e2be5f2016-09-14 05:23:22 -0700677 RTC_DCHECK(self);
bjornv@webrtc.orga2d8b752013-01-18 21:54:15 +0000678
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000679 if (self->robust_validation_enabled) {
680 // Simply a linear function of the histogram height at delay estimate.
681 quality = self->histogram[self->compare_delay] / kHistogramMax;
682 } else {
683 // Note that |last_delay_probability| states how deep the minimum of the
684 // cost function is, so it is rather an error probability.
Yves Gerey665174f2018-06-19 15:03:05 +0200685 quality = (float)(kMaxBitCountsQ9 - self->last_delay_probability) /
686 kMaxBitCountsQ9;
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000687 if (quality < 0) {
688 quality = 0;
689 }
bjornv@webrtc.org04ecd492013-03-18 14:15:12 +0000690 }
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000691 return quality;
bjornv@webrtc.orga2d8b752013-01-18 21:54:15 +0000692}
693
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000694void WebRtc_MeanEstimatorFix(int32_t new_value,
695 int factor,
696 int32_t* mean_value) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000697 int32_t diff = new_value - *mean_value;
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000698
699 // mean_new = mean_value + ((new_value - mean_value) >> factor);
700 if (diff < 0) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000701 diff = -((-diff) >> factor);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000702 } else {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000703 diff = (diff >> factor);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000704 }
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000705 *mean_value += diff;
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000706}
Per Åhgrene7175c92020-03-20 16:43:34 +0100707
708} // namespace webrtc