blob: fe750f5a68a4cab85b1142b1e835743f48bae8bd [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
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000020// Number of right shifts for scaling is linearly depending on number of bits in
21// the far-end binary spectrum.
22static const int kShiftsAtZero = 13; // Right shifts at zero binary spectrum.
23static const int kShiftsLinearSlope = 3;
24
Yves Gerey665174f2018-06-19 15:03:05 +020025static const int32_t kProbabilityOffset = 1024; // 2 in Q9.
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000026static const int32_t kProbabilityLowerLimit = 8704; // 17 in Q9.
Yves Gerey665174f2018-06-19 15:03:05 +020027static const int32_t kProbabilityMinSpread = 2816; // 5.5 in Q9.
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000028
bjornv@webrtc.org5c645082013-12-16 10:57:53 +000029// Robust validation settings
30static const float kHistogramMax = 3000.f;
31static const float kLastHistogramMax = 250.f;
32static const float kMinHistogramThreshold = 1.5f;
33static const int kMinRequiredHits = 10;
34static const int kMaxHitsWhenPossiblyNonCausal = 10;
35static const int kMaxHitsWhenPossiblyCausal = 1000;
bjornv@webrtc.org5c645082013-12-16 10:57:53 +000036static const float kQ14Scaling = 1.f / (1 << 14); // Scaling by 2^14 to get Q0.
37static const float kFractionSlope = 0.05f;
38static const float kMinFractionWhenPossiblyCausal = 0.5f;
39static const float kMinFractionWhenPossiblyNonCausal = 0.25f;
40
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000041// Counts and returns number of bits of a 32-bit word.
42static int BitCount(uint32_t u32) {
Yves Gerey665174f2018-06-19 15:03:05 +020043 uint32_t tmp =
44 u32 - ((u32 >> 1) & 033333333333) - ((u32 >> 2) & 011111111111);
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000045 tmp = ((tmp + (tmp >> 3)) & 030707070707);
46 tmp = (tmp + (tmp >> 6));
47 tmp = (tmp + (tmp >> 12) + (tmp >> 24)) & 077;
48
Yves Gerey665174f2018-06-19 15:03:05 +020049 return ((int)tmp);
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000050}
bjornv@google.comb47d4b22011-09-15 12:27:36 +000051
bjornv@google.comb47d4b22011-09-15 12:27:36 +000052// Compares the |binary_vector| with all rows of the |binary_matrix| and counts
53// per row the number of times they have the same value.
54//
55// Inputs:
56// - binary_vector : binary "vector" stored in a long
57// - binary_matrix : binary "matrix" stored as a vector of long
58// - matrix_size : size of binary "matrix"
59//
60// Output:
61// - bit_counts : "Vector" stored as a long, containing for each
62// row the number of times the matrix row and the
63// input vector have the same value
64//
65static void BitCountComparison(uint32_t binary_vector,
66 const uint32_t* binary_matrix,
67 int matrix_size,
68 int32_t* bit_counts) {
69 int n = 0;
bjornv@google.comb47d4b22011-09-15 12:27:36 +000070
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +000071 // Compare |binary_vector| with all rows of the |binary_matrix|
bjornv@google.comb47d4b22011-09-15 12:27:36 +000072 for (; n < matrix_size; n++) {
Yves Gerey665174f2018-06-19 15:03:05 +020073 bit_counts[n] = (int32_t)BitCount(binary_vector ^ binary_matrix[n]);
bjornv@google.comb47d4b22011-09-15 12:27:36 +000074 }
75}
76
bjornv@webrtc.org5c645082013-12-16 10:57:53 +000077// Collects necessary statistics for the HistogramBasedValidation(). This
78// function has to be called prior to calling HistogramBasedValidation(). The
79// statistics updated and used by the HistogramBasedValidation() are:
80// 1. the number of |candidate_hits|, which states for how long we have had the
81// same |candidate_delay|
82// 2. the |histogram| of candidate delays over time. This histogram is
83// weighted with respect to a reliability measure and time-varying to cope
84// with possible delay shifts.
85// For further description see commented code.
86//
87// Inputs:
88// - candidate_delay : The delay to validate.
89// - valley_depth_q14 : The cost function has a valley/minimum at the
90// |candidate_delay| location. |valley_depth_q14| is the
91// cost function difference between the minimum and
92// maximum locations. The value is in the Q14 domain.
93// - valley_level_q14 : Is the cost function value at the minimum, in Q14.
94static void UpdateRobustValidationStatistics(BinaryDelayEstimator* self,
95 int candidate_delay,
96 int32_t valley_depth_q14,
97 int32_t valley_level_q14) {
98 const float valley_depth = valley_depth_q14 * kQ14Scaling;
99 float decrease_in_last_set = valley_depth;
Yves Gerey665174f2018-06-19 15:03:05 +0200100 const int max_hits_for_slow_change = (candidate_delay < self->last_delay)
101 ? kMaxHitsWhenPossiblyNonCausal
102 : kMaxHitsWhenPossiblyCausal;
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000103 int i = 0;
104
kwiberg9e2be5f2016-09-14 05:23:22 -0700105 RTC_DCHECK_EQ(self->history_size, self->farend->history_size);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000106 // Reset |candidate_hits| if we have a new candidate.
107 if (candidate_delay != self->last_candidate_delay) {
108 self->candidate_hits = 0;
109 self->last_candidate_delay = candidate_delay;
110 }
111 self->candidate_hits++;
112
113 // The |histogram| is updated differently across the bins.
114 // 1. The |candidate_delay| histogram bin is increased with the
115 // |valley_depth|, which is a simple measure of how reliable the
116 // |candidate_delay| is. The histogram is not increased above
117 // |kHistogramMax|.
118 self->histogram[candidate_delay] += valley_depth;
119 if (self->histogram[candidate_delay] > kHistogramMax) {
120 self->histogram[candidate_delay] = kHistogramMax;
121 }
122 // 2. The histogram bins in the neighborhood of |candidate_delay| are
123 // unaffected. The neighborhood is defined as x + {-2, -1, 0, 1}.
124 // 3. The histogram bins in the neighborhood of |last_delay| are decreased
125 // with |decrease_in_last_set|. This value equals the difference between
126 // the cost function values at the locations |candidate_delay| and
127 // |last_delay| until we reach |max_hits_for_slow_change| consecutive hits
128 // at the |candidate_delay|. If we exceed this amount of hits the
129 // |candidate_delay| is a "potential" candidate and we start decreasing
130 // these histogram bins more rapidly with |valley_depth|.
131 if (self->candidate_hits < max_hits_for_slow_change) {
Yves Gerey665174f2018-06-19 15:03:05 +0200132 decrease_in_last_set =
133 (self->mean_bit_counts[self->compare_delay] - valley_level_q14) *
134 kQ14Scaling;
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000135 }
136 // 4. All other bins are decreased with |valley_depth|.
137 // TODO(bjornv): Investigate how to make this loop more efficient. Split up
138 // the loop? Remove parts that doesn't add too much.
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000139 for (i = 0; i < self->history_size; ++i) {
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000140 int is_in_last_set = (i >= self->last_delay - 2) &&
Yves Gerey665174f2018-06-19 15:03:05 +0200141 (i <= self->last_delay + 1) && (i != candidate_delay);
142 int is_in_candidate_set =
143 (i >= candidate_delay - 2) && (i <= candidate_delay + 1);
144 self->histogram[i] -=
145 decrease_in_last_set * is_in_last_set +
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000146 valley_depth * (!is_in_last_set && !is_in_candidate_set);
147 // 5. No histogram bin can go below 0.
148 if (self->histogram[i] < 0) {
149 self->histogram[i] = 0;
150 }
151 }
152}
153
154// Validates the |candidate_delay|, estimated in WebRtc_ProcessBinarySpectrum(),
155// based on a mix of counting concurring hits with a modified histogram
156// of recent delay estimates. In brief a candidate is valid (returns 1) if it
157// is the most likely according to the histogram. There are a couple of
158// exceptions that are worth mentioning:
159// 1. If the |candidate_delay| < |last_delay| it can be that we are in a
160// non-causal state, breaking a possible echo control algorithm. Hence, we
161// open up for a quicker change by allowing the change even if the
162// |candidate_delay| is not the most likely one according to the histogram.
163// 2. There's a minimum number of hits (kMinRequiredHits) and the histogram
164// value has to reached a minimum (kMinHistogramThreshold) to be valid.
165// 3. The action is also depending on the filter length used for echo control.
166// If the delay difference is larger than what the filter can capture, we
167// also move quicker towards a change.
168// For further description see commented code.
169//
170// Input:
171// - candidate_delay : The delay to validate.
172//
173// Return value:
174// - is_histogram_valid : 1 - The |candidate_delay| is valid.
175// 0 - Otherwise.
176static int HistogramBasedValidation(const BinaryDelayEstimator* self,
177 int candidate_delay) {
178 float fraction = 1.f;
179 float histogram_threshold = self->histogram[self->compare_delay];
180 const int delay_difference = candidate_delay - self->last_delay;
181 int is_histogram_valid = 0;
182
183 // The histogram based validation of |candidate_delay| is done by comparing
184 // the |histogram| at bin |candidate_delay| with a |histogram_threshold|.
185 // This |histogram_threshold| equals a |fraction| of the |histogram| at bin
186 // |last_delay|. The |fraction| is a piecewise linear function of the
187 // |delay_difference| between the |candidate_delay| and the |last_delay|
188 // allowing for a quicker move if
189 // i) a potential echo control filter can not handle these large differences.
190 // ii) keeping |last_delay| instead of updating to |candidate_delay| could
191 // force an echo control into a non-causal state.
192 // We further require the histogram to have reached a minimum value of
193 // |kMinHistogramThreshold|. In addition, we also require the number of
194 // |candidate_hits| to be more than |kMinRequiredHits| to remove spurious
195 // values.
196
197 // Calculate a comparison histogram value (|histogram_threshold|) that is
198 // depending on the distance between the |candidate_delay| and |last_delay|.
199 // TODO(bjornv): How much can we gain by turning the fraction calculation
200 // into tables?
bjornv@webrtc.orgbccd53d2014-01-08 08:18:15 +0000201 if (delay_difference > self->allowed_offset) {
202 fraction = 1.f - kFractionSlope * (delay_difference - self->allowed_offset);
Yves Gerey665174f2018-06-19 15:03:05 +0200203 fraction = (fraction > kMinFractionWhenPossiblyCausal
204 ? fraction
205 : kMinFractionWhenPossiblyCausal);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000206 } else if (delay_difference < 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200207 fraction =
208 kMinFractionWhenPossiblyNonCausal - kFractionSlope * delay_difference;
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000209 fraction = (fraction > 1.f ? 1.f : fraction);
210 }
211 histogram_threshold *= fraction;
Yves Gerey665174f2018-06-19 15:03:05 +0200212 histogram_threshold =
213 (histogram_threshold > kMinHistogramThreshold ? histogram_threshold
214 : kMinHistogramThreshold);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000215
216 is_histogram_valid =
217 (self->histogram[candidate_delay] >= histogram_threshold) &&
218 (self->candidate_hits > kMinRequiredHits);
219
220 return is_histogram_valid;
221}
222
223// Performs a robust validation of the |candidate_delay| estimated in
224// WebRtc_ProcessBinarySpectrum(). The algorithm takes the
225// |is_instantaneous_valid| and the |is_histogram_valid| and combines them
226// into a robust validation. The HistogramBasedValidation() has to be called
227// prior to this call.
228// For further description on how the combination is done, see commented code.
229//
230// Inputs:
231// - candidate_delay : The delay to validate.
232// - is_instantaneous_valid : The instantaneous validation performed in
233// WebRtc_ProcessBinarySpectrum().
234// - is_histogram_valid : The histogram based validation.
235//
236// Return value:
237// - is_robust : 1 - The candidate_delay is valid according to a
238// combination of the two inputs.
239// : 0 - Otherwise.
240static int RobustValidation(const BinaryDelayEstimator* self,
241 int candidate_delay,
242 int is_instantaneous_valid,
243 int is_histogram_valid) {
244 int is_robust = 0;
245
246 // The final robust validation is based on the two algorithms; 1) the
247 // |is_instantaneous_valid| and 2) the histogram based with result stored in
248 // |is_histogram_valid|.
249 // i) Before we actually have a valid estimate (|last_delay| == -2), we say
250 // a candidate is valid if either algorithm states so
251 // (|is_instantaneous_valid| OR |is_histogram_valid|).
Yves Gerey665174f2018-06-19 15:03:05 +0200252 is_robust =
253 (self->last_delay < 0) && (is_instantaneous_valid || is_histogram_valid);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000254 // ii) Otherwise, we need both algorithms to be certain
255 // (|is_instantaneous_valid| AND |is_histogram_valid|)
256 is_robust |= is_instantaneous_valid && is_histogram_valid;
257 // iii) With one exception, i.e., the histogram based algorithm can overrule
258 // the instantaneous one if |is_histogram_valid| = 1 and the histogram
259 // is significantly strong.
260 is_robust |= is_histogram_valid &&
Yves Gerey665174f2018-06-19 15:03:05 +0200261 (self->histogram[candidate_delay] > self->last_delay_histogram);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000262
263 return is_robust;
264}
265
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000266void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self) {
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000267 if (self == NULL) {
268 return;
269 }
270
271 free(self->binary_far_history);
272 self->binary_far_history = NULL;
273
274 free(self->far_bit_counts);
275 self->far_bit_counts = NULL;
276
277 free(self);
278}
279
280BinaryDelayEstimatorFarend* WebRtc_CreateBinaryDelayEstimatorFarend(
281 int history_size) {
bjornv@webrtc.org7ded92b2013-01-30 16:16:59 +0000282 BinaryDelayEstimatorFarend* self = NULL;
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000283
bjornv@webrtc.org7ded92b2013-01-30 16:16:59 +0000284 if (history_size > 1) {
285 // Sanity conditions fulfilled.
peahbdb7af62016-04-12 14:47:40 -0700286 self = static_cast<BinaryDelayEstimatorFarend*>(
287 malloc(sizeof(BinaryDelayEstimatorFarend)));
bjornv@webrtc.org7ded92b2013-01-30 16:16:59 +0000288 }
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000289 if (self == NULL) {
290 return NULL;
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000291 }
292
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000293 self->history_size = 0;
294 self->binary_far_history = NULL;
295 self->far_bit_counts = NULL;
296 if (WebRtc_AllocateFarendBufferMemory(self, history_size) == 0) {
297 WebRtc_FreeBinaryDelayEstimatorFarend(self);
298 self = NULL;
299 }
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000300 return self;
301}
302
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000303int WebRtc_AllocateFarendBufferMemory(BinaryDelayEstimatorFarend* self,
304 int history_size) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700305 RTC_DCHECK(self);
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000306 // (Re-)Allocate memory for history buffers.
peahbdb7af62016-04-12 14:47:40 -0700307 self->binary_far_history = static_cast<uint32_t*>(
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000308 realloc(self->binary_far_history,
peahbdb7af62016-04-12 14:47:40 -0700309 history_size * sizeof(*self->binary_far_history)));
Yves Gerey665174f2018-06-19 15:03:05 +0200310 self->far_bit_counts = static_cast<int*>(realloc(
311 self->far_bit_counts, history_size * sizeof(*self->far_bit_counts)));
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000312 if ((self->binary_far_history == NULL) || (self->far_bit_counts == NULL)) {
313 history_size = 0;
314 }
315 // Fill with zeros if we have expanded the buffers.
316 if (history_size > self->history_size) {
317 int size_diff = history_size - self->history_size;
Yves Gerey665174f2018-06-19 15:03:05 +0200318 memset(&self->binary_far_history[self->history_size], 0,
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000319 sizeof(*self->binary_far_history) * size_diff);
Yves Gerey665174f2018-06-19 15:03:05 +0200320 memset(&self->far_bit_counts[self->history_size], 0,
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000321 sizeof(*self->far_bit_counts) * size_diff);
322 }
323 self->history_size = history_size;
324
325 return self->history_size;
326}
327
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000328void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700329 RTC_DCHECK(self);
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000330 memset(self->binary_far_history, 0, sizeof(uint32_t) * self->history_size);
331 memset(self->far_bit_counts, 0, sizeof(int) * self->history_size);
332}
333
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000334void WebRtc_SoftResetBinaryDelayEstimatorFarend(
Yves Gerey665174f2018-06-19 15:03:05 +0200335 BinaryDelayEstimatorFarend* self,
336 int delay_shift) {
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000337 int abs_shift = abs(delay_shift);
338 int shift_size = 0;
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000339 int dest_index = 0;
340 int src_index = 0;
341 int padding_index = 0;
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000342
kwiberg9e2be5f2016-09-14 05:23:22 -0700343 RTC_DCHECK(self);
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000344 shift_size = self->history_size - abs_shift;
kwiberg9e2be5f2016-09-14 05:23:22 -0700345 RTC_DCHECK_GT(shift_size, 0);
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000346 if (delay_shift == 0) {
347 return;
348 } else if (delay_shift > 0) {
349 dest_index = abs_shift;
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000350 } else if (delay_shift < 0) {
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000351 src_index = abs_shift;
352 padding_index = shift_size;
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000353 }
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000354
355 // Shift and zero pad buffers.
356 memmove(&self->binary_far_history[dest_index],
357 &self->binary_far_history[src_index],
358 sizeof(*self->binary_far_history) * shift_size);
359 memset(&self->binary_far_history[padding_index], 0,
360 sizeof(*self->binary_far_history) * abs_shift);
Yves Gerey665174f2018-06-19 15:03:05 +0200361 memmove(&self->far_bit_counts[dest_index], &self->far_bit_counts[src_index],
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000362 sizeof(*self->far_bit_counts) * shift_size);
363 memset(&self->far_bit_counts[padding_index], 0,
364 sizeof(*self->far_bit_counts) * abs_shift);
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000365}
366
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000367void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend* handle,
368 uint32_t binary_far_spectrum) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700369 RTC_DCHECK(handle);
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000370 // Shift binary spectrum history and insert current |binary_far_spectrum|.
371 memmove(&(handle->binary_far_history[1]), &(handle->binary_far_history[0]),
372 (handle->history_size - 1) * sizeof(uint32_t));
373 handle->binary_far_history[0] = binary_far_spectrum;
374
375 // Shift history of far-end binary spectrum bit counts and insert bit count
376 // of current |binary_far_spectrum|.
377 memmove(&(handle->far_bit_counts[1]), &(handle->far_bit_counts[0]),
378 (handle->history_size - 1) * sizeof(int));
379 handle->far_bit_counts[0] = BitCount(binary_far_spectrum);
380}
381
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000382void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator* self) {
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000383 if (self == NULL) {
bjornv@webrtc.orgbfda85f2012-04-16 07:28:29 +0000384 return;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000385 }
bjornv@webrtc.orgbfda85f2012-04-16 07:28:29 +0000386
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000387 free(self->mean_bit_counts);
388 self->mean_bit_counts = NULL;
bjornv@webrtc.orgbfda85f2012-04-16 07:28:29 +0000389
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000390 free(self->bit_counts);
391 self->bit_counts = NULL;
bjornv@webrtc.orgbfda85f2012-04-16 07:28:29 +0000392
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000393 free(self->binary_near_history);
394 self->binary_near_history = NULL;
bjornv@webrtc.orgbfda85f2012-04-16 07:28:29 +0000395
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000396 free(self->histogram);
397 self->histogram = NULL;
398
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000399 // BinaryDelayEstimator does not have ownership of |farend|, hence we do not
400 // free the memory here. That should be handled separately by the user.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000401 self->farend = NULL;
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000402
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000403 free(self);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000404}
405
bjornv@webrtc.org94c213a2013-01-25 15:53:41 +0000406BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(
Yves Gerey665174f2018-06-19 15:03:05 +0200407 BinaryDelayEstimatorFarend* farend,
408 int max_lookahead) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000409 BinaryDelayEstimator* self = NULL;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000410
bjornv@webrtc.org240eec32014-04-03 08:11:47 +0000411 if ((farend != NULL) && (max_lookahead >= 0)) {
bjornv@webrtc.org2e729762012-04-18 08:30:29 +0000412 // Sanity conditions fulfilled.
peahbdb7af62016-04-12 14:47:40 -0700413 self = static_cast<BinaryDelayEstimator*>(
414 malloc(sizeof(BinaryDelayEstimator)));
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000415 }
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000416 if (self == NULL) {
417 return NULL;
418 }
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000419
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000420 self->farend = farend;
421 self->near_history_size = max_lookahead + 1;
422 self->history_size = 0;
423 self->robust_validation_enabled = 0; // Disabled by default.
424 self->allowed_offset = 0;
bjornv@webrtc.org2e729762012-04-18 08:30:29 +0000425
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000426 self->lookahead = max_lookahead;
bjornv@webrtc.org2e729762012-04-18 08:30:29 +0000427
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000428 // Allocate memory for spectrum and history buffers.
429 self->mean_bit_counts = NULL;
430 self->bit_counts = NULL;
431 self->histogram = NULL;
peahbdb7af62016-04-12 14:47:40 -0700432 self->binary_near_history = static_cast<uint32_t*>(
433 malloc((max_lookahead + 1) * sizeof(*self->binary_near_history)));
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000434 if (self->binary_near_history == NULL ||
435 WebRtc_AllocateHistoryBufferMemory(self, farend->history_size) == 0) {
436 WebRtc_FreeBinaryDelayEstimator(self);
437 self = NULL;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000438 }
439
bjornv@webrtc.org2e729762012-04-18 08:30:29 +0000440 return self;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000441}
442
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000443int WebRtc_AllocateHistoryBufferMemory(BinaryDelayEstimator* self,
444 int history_size) {
445 BinaryDelayEstimatorFarend* far = self->farend;
446 // (Re-)Allocate memory for spectrum and history buffers.
447 if (history_size != far->history_size) {
448 // Only update far-end buffers if we need.
449 history_size = WebRtc_AllocateFarendBufferMemory(far, history_size);
450 }
451 // The extra array element in |mean_bit_counts| and |histogram| is a dummy
452 // element only used while |last_delay| == -2, i.e., before we have a valid
453 // estimate.
peahbdb7af62016-04-12 14:47:40 -0700454 self->mean_bit_counts = static_cast<int32_t*>(
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000455 realloc(self->mean_bit_counts,
peahbdb7af62016-04-12 14:47:40 -0700456 (history_size + 1) * sizeof(*self->mean_bit_counts)));
457 self->bit_counts = static_cast<int32_t*>(
458 realloc(self->bit_counts, history_size * sizeof(*self->bit_counts)));
459 self->histogram = static_cast<float*>(
460 realloc(self->histogram, (history_size + 1) * sizeof(*self->histogram)));
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000461
Yves Gerey665174f2018-06-19 15:03:05 +0200462 if ((self->mean_bit_counts == NULL) || (self->bit_counts == NULL) ||
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000463 (self->histogram == NULL)) {
464 history_size = 0;
465 }
466 // Fill with zeros if we have expanded the buffers.
467 if (history_size > self->history_size) {
468 int size_diff = history_size - self->history_size;
Yves Gerey665174f2018-06-19 15:03:05 +0200469 memset(&self->mean_bit_counts[self->history_size], 0,
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000470 sizeof(*self->mean_bit_counts) * size_diff);
Yves Gerey665174f2018-06-19 15:03:05 +0200471 memset(&self->bit_counts[self->history_size], 0,
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000472 sizeof(*self->bit_counts) * size_diff);
Yves Gerey665174f2018-06-19 15:03:05 +0200473 memset(&self->histogram[self->history_size], 0,
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000474 sizeof(*self->histogram) * size_diff);
475 }
476 self->history_size = history_size;
477
478 return self->history_size;
479}
480
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000481void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator* self) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000482 int i = 0;
kwiberg9e2be5f2016-09-14 05:23:22 -0700483 RTC_DCHECK(self);
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000484
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000485 memset(self->bit_counts, 0, sizeof(int32_t) * self->history_size);
Yves Gerey665174f2018-06-19 15:03:05 +0200486 memset(self->binary_near_history, 0,
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000487 sizeof(uint32_t) * self->near_history_size);
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000488 for (i = 0; i <= self->history_size; ++i) {
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000489 self->mean_bit_counts[i] = (20 << 9); // 20 in Q9.
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000490 self->histogram[i] = 0.f;
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000491 }
Yves Gerey665174f2018-06-19 15:03:05 +0200492 self->minimum_probability = kMaxBitCountsQ9; // 32 in Q9.
493 self->last_delay_probability = (int)kMaxBitCountsQ9; // 32 in Q9.
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000494
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000495 // Default return value if we're unable to estimate. -1 is used for errors.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000496 self->last_delay = -2;
bjornv@webrtc.orgbd41a842013-11-28 14:58:35 +0000497
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000498 self->last_candidate_delay = -2;
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000499 self->compare_delay = self->history_size;
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000500 self->candidate_hits = 0;
501 self->last_delay_histogram = 0.f;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000502}
503
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000504int WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator* self,
505 int delay_shift) {
506 int lookahead = 0;
kwiberg9e2be5f2016-09-14 05:23:22 -0700507 RTC_DCHECK(self);
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000508 lookahead = self->lookahead;
509 self->lookahead -= delay_shift;
510 if (self->lookahead < 0) {
511 self->lookahead = 0;
512 }
513 if (self->lookahead > self->near_history_size - 1) {
514 self->lookahead = self->near_history_size - 1;
515 }
516 return lookahead - self->lookahead;
517}
518
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000519int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* self,
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000520 uint32_t binary_near_spectrum) {
521 int i = 0;
522 int candidate_delay = -1;
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000523 int valid_candidate = 0;
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000524
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000525 int32_t value_best_candidate = kMaxBitCountsQ9;
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000526 int32_t value_worst_candidate = 0;
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000527 int32_t valley_depth = 0;
bjornv@webrtc.orgbb599b72013-01-18 23:16:46 +0000528
kwiberg9e2be5f2016-09-14 05:23:22 -0700529 RTC_DCHECK(self);
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000530 if (self->farend->history_size != self->history_size) {
531 // Non matching history sizes.
532 return -1;
533 }
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000534 if (self->near_history_size > 1) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000535 // If we apply lookahead, shift near-end binary spectrum history. Insert
536 // current |binary_near_spectrum| and pull out the delayed one.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000537 memmove(&(self->binary_near_history[1]), &(self->binary_near_history[0]),
538 (self->near_history_size - 1) * sizeof(uint32_t));
539 self->binary_near_history[0] = binary_near_spectrum;
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000540 binary_near_spectrum = self->binary_near_history[self->lookahead];
andrew@webrtc.org828af1b2011-11-22 22:40:27 +0000541 }
542
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000543 // Compare with delayed spectra and store the |bit_counts| for each delay.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000544 BitCountComparison(binary_near_spectrum, self->farend->binary_far_history,
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000545 self->history_size, self->bit_counts);
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000546
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000547 // Update |mean_bit_counts|, which is the smoothed version of |bit_counts|.
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000548 for (i = 0; i < self->history_size; i++) {
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000549 // |bit_counts| is constrained to [0, 32], meaning we can smooth with a
550 // factor up to 2^26. We use Q9.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000551 int32_t bit_count = (self->bit_counts[i] << 9); // Q9.
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000552
553 // Update |mean_bit_counts| only when far-end signal has something to
554 // contribute. If |far_bit_counts| is zero the far-end signal is weak and
555 // we likely have a poor echo condition, hence don't update.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000556 if (self->farend->far_bit_counts[i] > 0) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000557 // Make number of right shifts piecewise linear w.r.t. |far_bit_counts|.
558 int shifts = kShiftsAtZero;
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000559 shifts -= (kShiftsLinearSlope * self->farend->far_bit_counts[i]) >> 4;
560 WebRtc_MeanEstimatorFix(bit_count, shifts, &(self->mean_bit_counts[i]));
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000561 }
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000562 }
563
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000564 // Find |candidate_delay|, |value_best_candidate| and |value_worst_candidate|
565 // of |mean_bit_counts|.
bjornv@webrtc.org69ef9912014-07-03 14:59:03 +0000566 for (i = 0; i < self->history_size; i++) {
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000567 if (self->mean_bit_counts[i] < value_best_candidate) {
568 value_best_candidate = self->mean_bit_counts[i];
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000569 candidate_delay = i;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000570 }
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000571 if (self->mean_bit_counts[i] > value_worst_candidate) {
572 value_worst_candidate = self->mean_bit_counts[i];
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000573 }
574 }
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000575 valley_depth = value_worst_candidate - value_best_candidate;
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000576
577 // The |value_best_candidate| is a good indicator on the probability of
578 // |candidate_delay| being an accurate delay (a small |value_best_candidate|
579 // means a good binary match). In the following sections we make a decision
580 // whether to update |last_delay| or not.
581 // 1) If the difference bit counts between the best and the worst delay
582 // candidates is too small we consider the situation to be unreliable and
583 // don't update |last_delay|.
584 // 2) If the situation is reliable we update |last_delay| if the value of the
585 // best candidate delay has a value less than
586 // i) an adaptive threshold |minimum_probability|, or
587 // ii) this corresponding value |last_delay_probability|, but updated at
588 // this time instant.
589
590 // Update |minimum_probability|.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000591 if ((self->minimum_probability > kProbabilityLowerLimit) &&
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000592 (valley_depth > kProbabilityMinSpread)) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000593 // The "hard" threshold can't be lower than 17 (in Q9).
594 // The valley in the curve also has to be distinct, i.e., the
595 // difference between |value_worst_candidate| and |value_best_candidate| has
596 // to be large enough.
597 int32_t threshold = value_best_candidate + kProbabilityOffset;
598 if (threshold < kProbabilityLowerLimit) {
599 threshold = kProbabilityLowerLimit;
600 }
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000601 if (self->minimum_probability > threshold) {
602 self->minimum_probability = threshold;
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000603 }
604 }
605 // Update |last_delay_probability|.
606 // We use a Markov type model, i.e., a slowly increasing level over time.
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000607 self->last_delay_probability++;
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000608 // Validate |candidate_delay|. We have a reliable instantaneous delay
609 // estimate if
610 // 1) The valley is distinct enough (|valley_depth| > |kProbabilityOffset|)
611 // and
612 // 2) The depth of the valley is deep enough
613 // (|value_best_candidate| < |minimum_probability|)
614 // and deeper than the best estimate so far
615 // (|value_best_candidate| < |last_delay_probability|)
616 valid_candidate = ((valley_depth > kProbabilityOffset) &&
Yves Gerey665174f2018-06-19 15:03:05 +0200617 ((value_best_candidate < self->minimum_probability) ||
618 (value_best_candidate < self->last_delay_probability)));
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000619
peahb1fc54d2016-05-12 05:08:45 -0700620 // Check for nonstationary farend signal.
621 const bool non_stationary_farend =
622 std::any_of(self->farend->far_bit_counts,
623 self->farend->far_bit_counts + self->history_size,
624 [](int a) { return a > 0; });
625
626 if (non_stationary_farend) {
627 // Only update the validation statistics when the farend is nonstationary
628 // as the underlying estimates are otherwise frozen.
629 UpdateRobustValidationStatistics(self, candidate_delay, valley_depth,
630 value_best_candidate);
631 }
632
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000633 if (self->robust_validation_enabled) {
Bjorn Volcker532531b2015-05-06 11:58:04 +0200634 int is_histogram_valid = HistogramBasedValidation(self, candidate_delay);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000635 valid_candidate = RobustValidation(self, candidate_delay, valid_candidate,
636 is_histogram_valid);
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000637 }
peahb1fc54d2016-05-12 05:08:45 -0700638
639 // Only update the delay estimate when the farend is nonstationary and when
640 // a valid delay candidate is available.
641 if (non_stationary_farend && valid_candidate) {
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000642 if (candidate_delay != self->last_delay) {
bjornv@webrtc.org1e7d6122013-12-16 13:37:28 +0000643 self->last_delay_histogram =
Yves Gerey665174f2018-06-19 15:03:05 +0200644 (self->histogram[candidate_delay] > kLastHistogramMax
645 ? kLastHistogramMax
646 : self->histogram[candidate_delay]);
bjornv@webrtc.org1e7d6122013-12-16 13:37:28 +0000647 // Adjust the histogram if we made a change to |last_delay|, though it was
648 // not the most likely one according to the histogram.
649 if (self->histogram[candidate_delay] <
650 self->histogram[self->compare_delay]) {
651 self->histogram[self->compare_delay] = self->histogram[candidate_delay];
652 }
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000653 }
bjornv@webrtc.orgd1a1c352013-11-28 11:45:05 +0000654 self->last_delay = candidate_delay;
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000655 if (value_best_candidate < self->last_delay_probability) {
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000656 self->last_delay_probability = value_best_candidate;
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000657 }
bjornv@webrtc.org5c645082013-12-16 10:57:53 +0000658 self->compare_delay = self->last_delay;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000659 }
660
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000661 return self->last_delay;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000662}
663
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000664int WebRtc_binary_last_delay(BinaryDelayEstimator* self) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700665 RTC_DCHECK(self);
bjornv@webrtc.org57f3a112013-01-25 22:02:15 +0000666 return self->last_delay;
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000667}
668
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000669float WebRtc_binary_last_delay_quality(BinaryDelayEstimator* self) {
670 float quality = 0;
kwiberg9e2be5f2016-09-14 05:23:22 -0700671 RTC_DCHECK(self);
bjornv@webrtc.orga2d8b752013-01-18 21:54:15 +0000672
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000673 if (self->robust_validation_enabled) {
674 // Simply a linear function of the histogram height at delay estimate.
675 quality = self->histogram[self->compare_delay] / kHistogramMax;
676 } else {
677 // Note that |last_delay_probability| states how deep the minimum of the
678 // cost function is, so it is rather an error probability.
Yves Gerey665174f2018-06-19 15:03:05 +0200679 quality = (float)(kMaxBitCountsQ9 - self->last_delay_probability) /
680 kMaxBitCountsQ9;
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000681 if (quality < 0) {
682 quality = 0;
683 }
bjornv@webrtc.org04ecd492013-03-18 14:15:12 +0000684 }
bjornv@webrtc.org28e83d12014-03-24 15:26:52 +0000685 return quality;
bjornv@webrtc.orga2d8b752013-01-18 21:54:15 +0000686}
687
bjornv@webrtc.org6a9835d2011-11-18 08:30:34 +0000688void WebRtc_MeanEstimatorFix(int32_t new_value,
689 int factor,
690 int32_t* mean_value) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000691 int32_t diff = new_value - *mean_value;
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000692
693 // mean_new = mean_value + ((new_value - mean_value) >> factor);
694 if (diff < 0) {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000695 diff = -((-diff) >> factor);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000696 } else {
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000697 diff = (diff >> factor);
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000698 }
bjornv@webrtc.org70adcd42011-12-29 14:51:21 +0000699 *mean_value += diff;
bjornv@google.comb47d4b22011-09-15 12:27:36 +0000700}