1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
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 
11 #include "modules/audio_processing/utility/delay_estimator.h"
12 
13 #include <stdlib.h>
14 #include <string.h>
15 
16 #include <algorithm>
17 
18 #include "rtc_base/checks.h"
19 
20 namespace webrtc {
21 
22 namespace {
23 
24 // Number of right shifts for scaling is linearly depending on number of bits in
25 // the far-end binary spectrum.
26 static const int kShiftsAtZero = 13;  // Right shifts at zero binary spectrum.
27 static const int kShiftsLinearSlope = 3;
28 
29 static const int32_t kProbabilityOffset = 1024;      // 2 in Q9.
30 static const int32_t kProbabilityLowerLimit = 8704;  // 17 in Q9.
31 static const int32_t kProbabilityMinSpread = 2816;   // 5.5 in Q9.
32 
33 // Robust validation settings
34 static const float kHistogramMax = 3000.f;
35 static const float kLastHistogramMax = 250.f;
36 static const float kMinHistogramThreshold = 1.5f;
37 static const int kMinRequiredHits = 10;
38 static const int kMaxHitsWhenPossiblyNonCausal = 10;
39 static const int kMaxHitsWhenPossiblyCausal = 1000;
40 static const float kQ14Scaling = 1.f / (1 << 14);  // Scaling by 2^14 to get Q0.
41 static const float kFractionSlope = 0.05f;
42 static const float kMinFractionWhenPossiblyCausal = 0.5f;
43 static const float kMinFractionWhenPossiblyNonCausal = 0.25f;
44 
45 }  // namespace
46 
47 // Counts and returns number of bits of a 32-bit word.
BitCount(uint32_t u32)48 static int BitCount(uint32_t u32) {
49   uint32_t tmp =
50       u32 - ((u32 >> 1) & 033333333333) - ((u32 >> 2) & 011111111111);
51   tmp = ((tmp + (tmp >> 3)) & 030707070707);
52   tmp = (tmp + (tmp >> 6));
53   tmp = (tmp + (tmp >> 12) + (tmp >> 24)) & 077;
54 
55   return ((int)tmp);
56 }
57 
58 // 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 //
BitCountComparison(uint32_t binary_vector,const uint32_t * binary_matrix,int matrix_size,int32_t * bit_counts)71 static 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;
76 
77   // Compare |binary_vector| with all rows of the |binary_matrix|
78   for (; n < matrix_size; n++) {
79     bit_counts[n] = (int32_t)BitCount(binary_vector ^ binary_matrix[n]);
80   }
81 }
82 
83 // 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.
UpdateRobustValidationStatistics(BinaryDelayEstimator * self,int candidate_delay,int32_t valley_depth_q14,int32_t valley_level_q14)100 static 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;
106   const int max_hits_for_slow_change = (candidate_delay < self->last_delay)
107                                            ? kMaxHitsWhenPossiblyNonCausal
108                                            : kMaxHitsWhenPossiblyCausal;
109   int i = 0;
110 
111   RTC_DCHECK_EQ(self->history_size, self->farend->history_size);
112   // 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) {
138     decrease_in_last_set =
139         (self->mean_bit_counts[self->compare_delay] - valley_level_q14) *
140         kQ14Scaling;
141   }
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.
145   for (i = 0; i < self->history_size; ++i) {
146     int is_in_last_set = (i >= self->last_delay - 2) &&
147                          (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 +
152         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.
HistogramBasedValidation(const BinaryDelayEstimator * self,int candidate_delay)182 static 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?
207   if (delay_difference > self->allowed_offset) {
208     fraction = 1.f - kFractionSlope * (delay_difference - self->allowed_offset);
209     fraction = (fraction > kMinFractionWhenPossiblyCausal
210                     ? fraction
211                     : kMinFractionWhenPossiblyCausal);
212   } else if (delay_difference < 0) {
213     fraction =
214         kMinFractionWhenPossiblyNonCausal - kFractionSlope * delay_difference;
215     fraction = (fraction > 1.f ? 1.f : fraction);
216   }
217   histogram_threshold *= fraction;
218   histogram_threshold =
219       (histogram_threshold > kMinHistogramThreshold ? histogram_threshold
220                                                     : kMinHistogramThreshold);
221 
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.
RobustValidation(const BinaryDelayEstimator * self,int candidate_delay,int is_instantaneous_valid,int is_histogram_valid)246 static 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|).
258   is_robust =
259       (self->last_delay < 0) && (is_instantaneous_valid || is_histogram_valid);
260   //  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 &&
267                (self->histogram[candidate_delay] > self->last_delay_histogram);
268 
269   return is_robust;
270 }
271 
WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend * self)272 void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self) {
273   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 
WebRtc_CreateBinaryDelayEstimatorFarend(int history_size)286 BinaryDelayEstimatorFarend* WebRtc_CreateBinaryDelayEstimatorFarend(
287     int history_size) {
288   BinaryDelayEstimatorFarend* self = NULL;
289 
290   if (history_size > 1) {
291     // Sanity conditions fulfilled.
292     self = static_cast<BinaryDelayEstimatorFarend*>(
293         malloc(sizeof(BinaryDelayEstimatorFarend)));
294   }
295   if (self == NULL) {
296     return NULL;
297   }
298 
299   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   }
306   return self;
307 }
308 
WebRtc_AllocateFarendBufferMemory(BinaryDelayEstimatorFarend * self,int history_size)309 int WebRtc_AllocateFarendBufferMemory(BinaryDelayEstimatorFarend* self,
310                                       int history_size) {
311   RTC_DCHECK(self);
312   // (Re-)Allocate memory for history buffers.
313   self->binary_far_history = static_cast<uint32_t*>(
314       realloc(self->binary_far_history,
315               history_size * sizeof(*self->binary_far_history)));
316   self->far_bit_counts = static_cast<int*>(realloc(
317       self->far_bit_counts, history_size * sizeof(*self->far_bit_counts)));
318   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;
324     memset(&self->binary_far_history[self->history_size], 0,
325            sizeof(*self->binary_far_history) * size_diff);
326     memset(&self->far_bit_counts[self->history_size], 0,
327            sizeof(*self->far_bit_counts) * size_diff);
328   }
329   self->history_size = history_size;
330 
331   return self->history_size;
332 }
333 
WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend * self)334 void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self) {
335   RTC_DCHECK(self);
336   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 
WebRtc_SoftResetBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend * self,int delay_shift)340 void WebRtc_SoftResetBinaryDelayEstimatorFarend(
341     BinaryDelayEstimatorFarend* self,
342     int delay_shift) {
343   int abs_shift = abs(delay_shift);
344   int shift_size = 0;
345   int dest_index = 0;
346   int src_index = 0;
347   int padding_index = 0;
348 
349   RTC_DCHECK(self);
350   shift_size = self->history_size - abs_shift;
351   RTC_DCHECK_GT(shift_size, 0);
352   if (delay_shift == 0) {
353     return;
354   } else if (delay_shift > 0) {
355     dest_index = abs_shift;
356   } else if (delay_shift < 0) {
357     src_index = abs_shift;
358     padding_index = shift_size;
359   }
360 
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);
367   memmove(&self->far_bit_counts[dest_index], &self->far_bit_counts[src_index],
368           sizeof(*self->far_bit_counts) * shift_size);
369   memset(&self->far_bit_counts[padding_index], 0,
370          sizeof(*self->far_bit_counts) * abs_shift);
371 }
372 
WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend * handle,uint32_t binary_far_spectrum)373 void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend* handle,
374                                  uint32_t binary_far_spectrum) {
375   RTC_DCHECK(handle);
376   // 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 
WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator * self)388 void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator* self) {
389   if (self == NULL) {
390     return;
391   }
392 
393   free(self->mean_bit_counts);
394   self->mean_bit_counts = NULL;
395 
396   free(self->bit_counts);
397   self->bit_counts = NULL;
398 
399   free(self->binary_near_history);
400   self->binary_near_history = NULL;
401 
402   free(self->histogram);
403   self->histogram = NULL;
404 
405   // BinaryDelayEstimator does not have ownership of |farend|, hence we do not
406   // free the memory here. That should be handled separately by the user.
407   self->farend = NULL;
408 
409   free(self);
410 }
411 
WebRtc_CreateBinaryDelayEstimator(BinaryDelayEstimatorFarend * farend,int max_lookahead)412 BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(
413     BinaryDelayEstimatorFarend* farend,
414     int max_lookahead) {
415   BinaryDelayEstimator* self = NULL;
416 
417   if ((farend != NULL) && (max_lookahead >= 0)) {
418     // Sanity conditions fulfilled.
419     self = static_cast<BinaryDelayEstimator*>(
420         malloc(sizeof(BinaryDelayEstimator)));
421   }
422   if (self == NULL) {
423     return NULL;
424   }
425 
426   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;
431 
432   self->lookahead = max_lookahead;
433 
434   // Allocate memory for spectrum and history buffers.
435   self->mean_bit_counts = NULL;
436   self->bit_counts = NULL;
437   self->histogram = NULL;
438   self->binary_near_history = static_cast<uint32_t*>(
439       malloc((max_lookahead + 1) * sizeof(*self->binary_near_history)));
440   if (self->binary_near_history == NULL ||
441       WebRtc_AllocateHistoryBufferMemory(self, farend->history_size) == 0) {
442     WebRtc_FreeBinaryDelayEstimator(self);
443     self = NULL;
444   }
445 
446   return self;
447 }
448 
WebRtc_AllocateHistoryBufferMemory(BinaryDelayEstimator * self,int history_size)449 int 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.
460   self->mean_bit_counts = static_cast<int32_t*>(
461       realloc(self->mean_bit_counts,
462               (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)));
467 
468   if ((self->mean_bit_counts == NULL) || (self->bit_counts == NULL) ||
469       (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;
475     memset(&self->mean_bit_counts[self->history_size], 0,
476            sizeof(*self->mean_bit_counts) * size_diff);
477     memset(&self->bit_counts[self->history_size], 0,
478            sizeof(*self->bit_counts) * size_diff);
479     memset(&self->histogram[self->history_size], 0,
480            sizeof(*self->histogram) * size_diff);
481   }
482   self->history_size = history_size;
483 
484   return self->history_size;
485 }
486 
WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator * self)487 void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator* self) {
488   int i = 0;
489   RTC_DCHECK(self);
490 
491   memset(self->bit_counts, 0, sizeof(int32_t) * self->history_size);
492   memset(self->binary_near_history, 0,
493          sizeof(uint32_t) * self->near_history_size);
494   for (i = 0; i <= self->history_size; ++i) {
495     self->mean_bit_counts[i] = (20 << 9);  // 20 in Q9.
496     self->histogram[i] = 0.f;
497   }
498   self->minimum_probability = kMaxBitCountsQ9;          // 32 in Q9.
499   self->last_delay_probability = (int)kMaxBitCountsQ9;  // 32 in Q9.
500 
501   // Default return value if we're unable to estimate. -1 is used for errors.
502   self->last_delay = -2;
503 
504   self->last_candidate_delay = -2;
505   self->compare_delay = self->history_size;
506   self->candidate_hits = 0;
507   self->last_delay_histogram = 0.f;
508 }
509 
WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator * self,int delay_shift)510 int WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator* self,
511                                          int delay_shift) {
512   int lookahead = 0;
513   RTC_DCHECK(self);
514   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 
WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator * self,uint32_t binary_near_spectrum)525 int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* self,
526                                  uint32_t binary_near_spectrum) {
527   int i = 0;
528   int candidate_delay = -1;
529   int valid_candidate = 0;
530 
531   int32_t value_best_candidate = kMaxBitCountsQ9;
532   int32_t value_worst_candidate = 0;
533   int32_t valley_depth = 0;
534 
535   RTC_DCHECK(self);
536   if (self->farend->history_size != self->history_size) {
537     // Non matching history sizes.
538     return -1;
539   }
540   if (self->near_history_size > 1) {
541     // If we apply lookahead, shift near-end binary spectrum history. Insert
542     // current |binary_near_spectrum| and pull out the delayed one.
543     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;
546     binary_near_spectrum = self->binary_near_history[self->lookahead];
547   }
548 
549   // Compare with delayed spectra and store the |bit_counts| for each delay.
550   BitCountComparison(binary_near_spectrum, self->farend->binary_far_history,
551                      self->history_size, self->bit_counts);
552 
553   // Update |mean_bit_counts|, which is the smoothed version of |bit_counts|.
554   for (i = 0; i < self->history_size; i++) {
555     // |bit_counts| is constrained to [0, 32], meaning we can smooth with a
556     // factor up to 2^26. We use Q9.
557     int32_t bit_count = (self->bit_counts[i] << 9);  // Q9.
558 
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.
562     if (self->farend->far_bit_counts[i] > 0) {
563       // Make number of right shifts piecewise linear w.r.t. |far_bit_counts|.
564       int shifts = kShiftsAtZero;
565       shifts -= (kShiftsLinearSlope * self->farend->far_bit_counts[i]) >> 4;
566       WebRtc_MeanEstimatorFix(bit_count, shifts, &(self->mean_bit_counts[i]));
567     }
568   }
569 
570   // Find |candidate_delay|, |value_best_candidate| and |value_worst_candidate|
571   // of |mean_bit_counts|.
572   for (i = 0; i < self->history_size; i++) {
573     if (self->mean_bit_counts[i] < value_best_candidate) {
574       value_best_candidate = self->mean_bit_counts[i];
575       candidate_delay = i;
576     }
577     if (self->mean_bit_counts[i] > value_worst_candidate) {
578       value_worst_candidate = self->mean_bit_counts[i];
579     }
580   }
581   valley_depth = value_worst_candidate - value_best_candidate;
582 
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|.
597   if ((self->minimum_probability > kProbabilityLowerLimit) &&
598       (valley_depth > kProbabilityMinSpread)) {
599     // 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     }
607     if (self->minimum_probability > threshold) {
608       self->minimum_probability = threshold;
609     }
610   }
611   // Update |last_delay_probability|.
612   // We use a Markov type model, i.e., a slowly increasing level over time.
613   self->last_delay_probability++;
614   // 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) &&
623                      ((value_best_candidate < self->minimum_probability) ||
624                       (value_best_candidate < self->last_delay_probability)));
625 
626   // 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 
639   if (self->robust_validation_enabled) {
640     int is_histogram_valid = HistogramBasedValidation(self, candidate_delay);
641     valid_candidate = RobustValidation(self, candidate_delay, valid_candidate,
642                                        is_histogram_valid);
643   }
644 
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) {
648     if (candidate_delay != self->last_delay) {
649       self->last_delay_histogram =
650           (self->histogram[candidate_delay] > kLastHistogramMax
651                ? kLastHistogramMax
652                : self->histogram[candidate_delay]);
653       // 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       }
659     }
660     self->last_delay = candidate_delay;
661     if (value_best_candidate < self->last_delay_probability) {
662       self->last_delay_probability = value_best_candidate;
663     }
664     self->compare_delay = self->last_delay;
665   }
666 
667   return self->last_delay;
668 }
669 
WebRtc_binary_last_delay(BinaryDelayEstimator * self)670 int WebRtc_binary_last_delay(BinaryDelayEstimator* self) {
671   RTC_DCHECK(self);
672   return self->last_delay;
673 }
674 
WebRtc_binary_last_delay_quality(BinaryDelayEstimator * self)675 float WebRtc_binary_last_delay_quality(BinaryDelayEstimator* self) {
676   float quality = 0;
677   RTC_DCHECK(self);
678 
679   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.
685     quality = (float)(kMaxBitCountsQ9 - self->last_delay_probability) /
686               kMaxBitCountsQ9;
687     if (quality < 0) {
688       quality = 0;
689     }
690   }
691   return quality;
692 }
693 
WebRtc_MeanEstimatorFix(int32_t new_value,int factor,int32_t * mean_value)694 void WebRtc_MeanEstimatorFix(int32_t new_value,
695                              int factor,
696                              int32_t* mean_value) {
697   int32_t diff = new_value - *mean_value;
698 
699   // mean_new = mean_value + ((new_value - mean_value) >> factor);
700   if (diff < 0) {
701     diff = -((-diff) >> factor);
702   } else {
703     diff = (diff >> factor);
704   }
705   *mean_value += diff;
706 }
707 
708 }  // namespace webrtc
709