tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

delay_estimator.cc (28526B)


      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 <algorithm>
     14 #include <cstdint>
     15 #include <cstdlib>
     16 #include <cstring>
     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 const int kShiftsAtZero = 13;  // Right shifts at zero binary spectrum.
     27 const int kShiftsLinearSlope = 3;
     28 
     29 const int32_t kProbabilityOffset = 1024;      // 2 in Q9.
     30 const int32_t kProbabilityLowerLimit = 8704;  // 17 in Q9.
     31 const int32_t kProbabilityMinSpread = 2816;   // 5.5 in Q9.
     32 
     33 // Robust validation settings
     34 const float kHistogramMax = 3000.f;
     35 const float kLastHistogramMax = 250.f;
     36 const float kMinHistogramThreshold = 1.5f;
     37 const int kMinRequiredHits = 10;
     38 const int kMaxHitsWhenPossiblyNonCausal = 10;
     39 const int kMaxHitsWhenPossiblyCausal = 1000;
     40 const float kQ14Scaling = 1.f / (1 << 14);  // Scaling by 2^14 to get Q0.
     41 const float kFractionSlope = 0.05f;
     42 const float kMinFractionWhenPossiblyCausal = 0.5f;
     43 const float kMinFractionWhenPossiblyNonCausal = 0.25f;
     44 
     45 }  // namespace
     46 
     47 // Counts and returns number of bits of a 32-bit word.
     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 //
     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.
    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.
    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.
    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 
    272 void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self) {
    273  if (self == nullptr) {
    274    return;
    275  }
    276 
    277  free(self->binary_far_history);
    278  self->binary_far_history = nullptr;
    279 
    280  free(self->far_bit_counts);
    281  self->far_bit_counts = nullptr;
    282 
    283  free(self);
    284 }
    285 
    286 BinaryDelayEstimatorFarend* WebRtc_CreateBinaryDelayEstimatorFarend(
    287    int history_size) {
    288  BinaryDelayEstimatorFarend* self = nullptr;
    289 
    290  if (history_size > 1) {
    291    // Sanity conditions fulfilled.
    292    self = static_cast<BinaryDelayEstimatorFarend*>(
    293        malloc(sizeof(BinaryDelayEstimatorFarend)));
    294  }
    295  if (self == nullptr) {
    296    return nullptr;
    297  }
    298 
    299  self->history_size = 0;
    300  self->binary_far_history = nullptr;
    301  self->far_bit_counts = nullptr;
    302  if (WebRtc_AllocateFarendBufferMemory(self, history_size) == 0) {
    303    WebRtc_FreeBinaryDelayEstimatorFarend(self);
    304    self = nullptr;
    305  }
    306  return self;
    307 }
    308 
    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 == nullptr) ||
    319      (self->far_bit_counts == nullptr)) {
    320    history_size = 0;
    321  }
    322  // Fill with zeros if we have expanded the buffers.
    323  if (history_size > self->history_size) {
    324    int size_diff = history_size - self->history_size;
    325    memset(&self->binary_far_history[self->history_size], 0,
    326           sizeof(*self->binary_far_history) * size_diff);
    327    memset(&self->far_bit_counts[self->history_size], 0,
    328           sizeof(*self->far_bit_counts) * size_diff);
    329  }
    330  self->history_size = history_size;
    331 
    332  return self->history_size;
    333 }
    334 
    335 void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self) {
    336  RTC_DCHECK(self);
    337  memset(self->binary_far_history, 0, sizeof(uint32_t) * self->history_size);
    338  memset(self->far_bit_counts, 0, sizeof(int) * self->history_size);
    339 }
    340 
    341 void WebRtc_SoftResetBinaryDelayEstimatorFarend(
    342    BinaryDelayEstimatorFarend* self,
    343    int delay_shift) {
    344  int abs_shift = abs(delay_shift);
    345  int shift_size = 0;
    346  int dest_index = 0;
    347  int src_index = 0;
    348  int padding_index = 0;
    349 
    350  RTC_DCHECK(self);
    351  shift_size = self->history_size - abs_shift;
    352  RTC_DCHECK_GT(shift_size, 0);
    353  if (delay_shift == 0) {
    354    return;
    355  } else if (delay_shift > 0) {
    356    dest_index = abs_shift;
    357  } else if (delay_shift < 0) {
    358    src_index = abs_shift;
    359    padding_index = shift_size;
    360  }
    361 
    362  // Shift and zero pad buffers.
    363  memmove(&self->binary_far_history[dest_index],
    364          &self->binary_far_history[src_index],
    365          sizeof(*self->binary_far_history) * shift_size);
    366  memset(&self->binary_far_history[padding_index], 0,
    367         sizeof(*self->binary_far_history) * abs_shift);
    368  memmove(&self->far_bit_counts[dest_index], &self->far_bit_counts[src_index],
    369          sizeof(*self->far_bit_counts) * shift_size);
    370  memset(&self->far_bit_counts[padding_index], 0,
    371         sizeof(*self->far_bit_counts) * abs_shift);
    372 }
    373 
    374 void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend* handle,
    375                                 uint32_t binary_far_spectrum) {
    376  RTC_DCHECK(handle);
    377  // Shift binary spectrum history and insert current `binary_far_spectrum`.
    378  memmove(&(handle->binary_far_history[1]), &(handle->binary_far_history[0]),
    379          (handle->history_size - 1) * sizeof(uint32_t));
    380  handle->binary_far_history[0] = binary_far_spectrum;
    381 
    382  // Shift history of far-end binary spectrum bit counts and insert bit count
    383  // of current `binary_far_spectrum`.
    384  memmove(&(handle->far_bit_counts[1]), &(handle->far_bit_counts[0]),
    385          (handle->history_size - 1) * sizeof(int));
    386  handle->far_bit_counts[0] = BitCount(binary_far_spectrum);
    387 }
    388 
    389 void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator* self) {
    390  if (self == nullptr) {
    391    return;
    392  }
    393 
    394  free(self->mean_bit_counts);
    395  self->mean_bit_counts = nullptr;
    396 
    397  free(self->bit_counts);
    398  self->bit_counts = nullptr;
    399 
    400  free(self->binary_near_history);
    401  self->binary_near_history = nullptr;
    402 
    403  free(self->histogram);
    404  self->histogram = nullptr;
    405 
    406  // BinaryDelayEstimator does not have ownership of `farend`, hence we do not
    407  // free the memory here. That should be handled separately by the user.
    408  self->farend = nullptr;
    409 
    410  free(self);
    411 }
    412 
    413 BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(
    414    BinaryDelayEstimatorFarend* farend,
    415    int max_lookahead) {
    416  BinaryDelayEstimator* self = nullptr;
    417 
    418  if ((farend != nullptr) && (max_lookahead >= 0)) {
    419    // Sanity conditions fulfilled.
    420    self = static_cast<BinaryDelayEstimator*>(
    421        malloc(sizeof(BinaryDelayEstimator)));
    422  }
    423  if (self == nullptr) {
    424    return nullptr;
    425  }
    426 
    427  self->farend = farend;
    428  self->near_history_size = max_lookahead + 1;
    429  self->history_size = 0;
    430  self->robust_validation_enabled = 0;  // Disabled by default.
    431  self->allowed_offset = 0;
    432 
    433  self->lookahead = max_lookahead;
    434 
    435  // Allocate memory for spectrum and history buffers.
    436  self->mean_bit_counts = nullptr;
    437  self->bit_counts = nullptr;
    438  self->histogram = nullptr;
    439  self->binary_near_history = static_cast<uint32_t*>(
    440      malloc((max_lookahead + 1) * sizeof(*self->binary_near_history)));
    441  if (self->binary_near_history == nullptr ||
    442      WebRtc_AllocateHistoryBufferMemory(self, farend->history_size) == 0) {
    443    WebRtc_FreeBinaryDelayEstimator(self);
    444    self = nullptr;
    445  }
    446 
    447  return self;
    448 }
    449 
    450 int WebRtc_AllocateHistoryBufferMemory(BinaryDelayEstimator* self,
    451                                       int history_size) {
    452  BinaryDelayEstimatorFarend* far = self->farend;
    453  // (Re-)Allocate memory for spectrum and history buffers.
    454  if (history_size != far->history_size) {
    455    // Only update far-end buffers if we need.
    456    history_size = WebRtc_AllocateFarendBufferMemory(far, history_size);
    457  }
    458  // The extra array element in `mean_bit_counts` and `histogram` is a dummy
    459  // element only used while `last_delay` == -2, i.e., before we have a valid
    460  // estimate.
    461  self->mean_bit_counts = static_cast<int32_t*>(
    462      realloc(self->mean_bit_counts,
    463              (history_size + 1) * sizeof(*self->mean_bit_counts)));
    464  self->bit_counts = static_cast<int32_t*>(
    465      realloc(self->bit_counts, history_size * sizeof(*self->bit_counts)));
    466  self->histogram = static_cast<float*>(
    467      realloc(self->histogram, (history_size + 1) * sizeof(*self->histogram)));
    468 
    469  if ((self->mean_bit_counts == nullptr) || (self->bit_counts == nullptr) ||
    470      (self->histogram == nullptr)) {
    471    history_size = 0;
    472  }
    473  // Fill with zeros if we have expanded the buffers.
    474  if (history_size > self->history_size) {
    475    int size_diff = history_size - self->history_size;
    476    memset(&self->mean_bit_counts[self->history_size], 0,
    477           sizeof(*self->mean_bit_counts) * size_diff);
    478    memset(&self->bit_counts[self->history_size], 0,
    479           sizeof(*self->bit_counts) * size_diff);
    480    memset(&self->histogram[self->history_size], 0,
    481           sizeof(*self->histogram) * size_diff);
    482  }
    483  self->history_size = history_size;
    484 
    485  return self->history_size;
    486 }
    487 
    488 void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator* self) {
    489  int i = 0;
    490  RTC_DCHECK(self);
    491 
    492  memset(self->bit_counts, 0, sizeof(int32_t) * self->history_size);
    493  memset(self->binary_near_history, 0,
    494         sizeof(uint32_t) * self->near_history_size);
    495  for (i = 0; i <= self->history_size; ++i) {
    496    self->mean_bit_counts[i] = (20 << 9);  // 20 in Q9.
    497    self->histogram[i] = 0.f;
    498  }
    499  self->minimum_probability = kMaxBitCountsQ9;          // 32 in Q9.
    500  self->last_delay_probability = (int)kMaxBitCountsQ9;  // 32 in Q9.
    501 
    502  // Default return value if we're unable to estimate. -1 is used for errors.
    503  self->last_delay = -2;
    504 
    505  self->last_candidate_delay = -2;
    506  self->compare_delay = self->history_size;
    507  self->candidate_hits = 0;
    508  self->last_delay_histogram = 0.f;
    509 }
    510 
    511 int WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator* self,
    512                                         int delay_shift) {
    513  int lookahead = 0;
    514  RTC_DCHECK(self);
    515  lookahead = self->lookahead;
    516  self->lookahead -= delay_shift;
    517  if (self->lookahead < 0) {
    518    self->lookahead = 0;
    519  }
    520  if (self->lookahead > self->near_history_size - 1) {
    521    self->lookahead = self->near_history_size - 1;
    522  }
    523  return lookahead - self->lookahead;
    524 }
    525 
    526 int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* self,
    527                                 uint32_t binary_near_spectrum) {
    528  int i = 0;
    529  int candidate_delay = -1;
    530  int valid_candidate = 0;
    531 
    532  int32_t value_best_candidate = kMaxBitCountsQ9;
    533  int32_t value_worst_candidate = 0;
    534  int32_t valley_depth = 0;
    535 
    536  RTC_DCHECK(self);
    537  if (self->farend->history_size != self->history_size) {
    538    // Non matching history sizes.
    539    return -1;
    540  }
    541  if (self->near_history_size > 1) {
    542    // If we apply lookahead, shift near-end binary spectrum history. Insert
    543    // current `binary_near_spectrum` and pull out the delayed one.
    544    memmove(&(self->binary_near_history[1]), &(self->binary_near_history[0]),
    545            (self->near_history_size - 1) * sizeof(uint32_t));
    546    self->binary_near_history[0] = binary_near_spectrum;
    547    binary_near_spectrum = self->binary_near_history[self->lookahead];
    548  }
    549 
    550  // Compare with delayed spectra and store the `bit_counts` for each delay.
    551  BitCountComparison(binary_near_spectrum, self->farend->binary_far_history,
    552                     self->history_size, self->bit_counts);
    553 
    554  // Update `mean_bit_counts`, which is the smoothed version of `bit_counts`.
    555  for (i = 0; i < self->history_size; i++) {
    556    // `bit_counts` is constrained to [0, 32], meaning we can smooth with a
    557    // factor up to 2^26. We use Q9.
    558    int32_t bit_count = (self->bit_counts[i] << 9);  // Q9.
    559 
    560    // Update `mean_bit_counts` only when far-end signal has something to
    561    // contribute. If `far_bit_counts` is zero the far-end signal is weak and
    562    // we likely have a poor echo condition, hence don't update.
    563    if (self->farend->far_bit_counts[i] > 0) {
    564      // Make number of right shifts piecewise linear w.r.t. `far_bit_counts`.
    565      int shifts = kShiftsAtZero;
    566      shifts -= (kShiftsLinearSlope * self->farend->far_bit_counts[i]) >> 4;
    567      WebRtc_MeanEstimatorFix(bit_count, shifts, &(self->mean_bit_counts[i]));
    568    }
    569  }
    570 
    571  // Find `candidate_delay`, `value_best_candidate` and `value_worst_candidate`
    572  // of `mean_bit_counts`.
    573  for (i = 0; i < self->history_size; i++) {
    574    if (self->mean_bit_counts[i] < value_best_candidate) {
    575      value_best_candidate = self->mean_bit_counts[i];
    576      candidate_delay = i;
    577    }
    578    if (self->mean_bit_counts[i] > value_worst_candidate) {
    579      value_worst_candidate = self->mean_bit_counts[i];
    580    }
    581  }
    582  valley_depth = value_worst_candidate - value_best_candidate;
    583 
    584  // The `value_best_candidate` is a good indicator on the probability of
    585  // `candidate_delay` being an accurate delay (a small `value_best_candidate`
    586  // means a good binary match). In the following sections we make a decision
    587  // whether to update `last_delay` or not.
    588  // 1) If the difference bit counts between the best and the worst delay
    589  //    candidates is too small we consider the situation to be unreliable and
    590  //    don't update `last_delay`.
    591  // 2) If the situation is reliable we update `last_delay` if the value of the
    592  //    best candidate delay has a value less than
    593  //     i) an adaptive threshold `minimum_probability`, or
    594  //    ii) this corresponding value `last_delay_probability`, but updated at
    595  //        this time instant.
    596 
    597  // Update `minimum_probability`.
    598  if ((self->minimum_probability > kProbabilityLowerLimit) &&
    599      (valley_depth > kProbabilityMinSpread)) {
    600    // The "hard" threshold can't be lower than 17 (in Q9).
    601    // The valley in the curve also has to be distinct, i.e., the
    602    // difference between `value_worst_candidate` and `value_best_candidate` has
    603    // to be large enough.
    604    int32_t threshold = value_best_candidate + kProbabilityOffset;
    605    if (threshold < kProbabilityLowerLimit) {
    606      threshold = kProbabilityLowerLimit;
    607    }
    608    if (self->minimum_probability > threshold) {
    609      self->minimum_probability = threshold;
    610    }
    611  }
    612  // Update `last_delay_probability`.
    613  // We use a Markov type model, i.e., a slowly increasing level over time.
    614  self->last_delay_probability++;
    615  // Validate `candidate_delay`.  We have a reliable instantaneous delay
    616  // estimate if
    617  //  1) The valley is distinct enough (`valley_depth` > `kProbabilityOffset`)
    618  // and
    619  //  2) The depth of the valley is deep enough
    620  //      (`value_best_candidate` < `minimum_probability`)
    621  //     and deeper than the best estimate so far
    622  //      (`value_best_candidate` < `last_delay_probability`)
    623  valid_candidate = ((valley_depth > kProbabilityOffset) &&
    624                     ((value_best_candidate < self->minimum_probability) ||
    625                      (value_best_candidate < self->last_delay_probability)));
    626 
    627  // Check for nonstationary farend signal.
    628  const bool non_stationary_farend =
    629      std::any_of(self->farend->far_bit_counts,
    630                  self->farend->far_bit_counts + self->history_size,
    631                  [](int a) { return a > 0; });
    632 
    633  if (non_stationary_farend) {
    634    // Only update the validation statistics when the farend is nonstationary
    635    // as the underlying estimates are otherwise frozen.
    636    UpdateRobustValidationStatistics(self, candidate_delay, valley_depth,
    637                                     value_best_candidate);
    638  }
    639 
    640  if (self->robust_validation_enabled) {
    641    int is_histogram_valid = HistogramBasedValidation(self, candidate_delay);
    642    valid_candidate = RobustValidation(self, candidate_delay, valid_candidate,
    643                                       is_histogram_valid);
    644  }
    645 
    646  // Only update the delay estimate when the farend is nonstationary and when
    647  // a valid delay candidate is available.
    648  if (non_stationary_farend && valid_candidate) {
    649    if (candidate_delay != self->last_delay) {
    650      self->last_delay_histogram =
    651          (self->histogram[candidate_delay] > kLastHistogramMax
    652               ? kLastHistogramMax
    653               : self->histogram[candidate_delay]);
    654      // Adjust the histogram if we made a change to `last_delay`, though it was
    655      // not the most likely one according to the histogram.
    656      if (self->histogram[candidate_delay] <
    657          self->histogram[self->compare_delay]) {
    658        self->histogram[self->compare_delay] = self->histogram[candidate_delay];
    659      }
    660    }
    661    self->last_delay = candidate_delay;
    662    if (value_best_candidate < self->last_delay_probability) {
    663      self->last_delay_probability = value_best_candidate;
    664    }
    665    self->compare_delay = self->last_delay;
    666  }
    667 
    668  return self->last_delay;
    669 }
    670 
    671 int WebRtc_binary_last_delay(BinaryDelayEstimator* self) {
    672  RTC_DCHECK(self);
    673  return self->last_delay;
    674 }
    675 
    676 float WebRtc_binary_last_delay_quality(BinaryDelayEstimator* self) {
    677  float quality = 0;
    678  RTC_DCHECK(self);
    679 
    680  if (self->robust_validation_enabled) {
    681    // Simply a linear function of the histogram height at delay estimate.
    682    quality = self->histogram[self->compare_delay] / kHistogramMax;
    683  } else {
    684    // Note that `last_delay_probability` states how deep the minimum of the
    685    // cost function is, so it is rather an error probability.
    686    quality = (float)(kMaxBitCountsQ9 - self->last_delay_probability) /
    687              kMaxBitCountsQ9;
    688    if (quality < 0) {
    689      quality = 0;
    690    }
    691  }
    692  return quality;
    693 }
    694 
    695 void WebRtc_MeanEstimatorFix(int32_t new_value,
    696                             int factor,
    697                             int32_t* mean_value) {
    698  int32_t diff = new_value - *mean_value;
    699 
    700  // mean_new = mean_value + ((new_value - mean_value) >> factor);
    701  if (diff < 0) {
    702    diff = -((-diff) >> factor);
    703  } else {
    704    diff = (diff >> factor);
    705  }
    706  *mean_value += diff;
    707 }
    708 
    709 }  // namespace webrtc