bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | bfda85f | 2012-04-16 07:28:29 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/utility/delay_estimator.h" |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 12 | |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
peah | b1fc54d | 2016-05-12 05:08:45 -0700 | [diff] [blame] | 16 | #include <algorithm> |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 19 | |
Per Åhgren | e7175c9 | 2020-03-20 16:43:34 +0100 | [diff] [blame] | 20 | namespace webrtc { |
| 21 | |
| 22 | namespace { |
| 23 | |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 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 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 29 | static const int32_t kProbabilityOffset = 1024; // 2 in Q9. |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 30 | static const int32_t kProbabilityLowerLimit = 8704; // 17 in Q9. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 31 | static const int32_t kProbabilityMinSpread = 2816; // 5.5 in Q9. |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 32 | |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 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; |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 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 | |
Per Åhgren | e7175c9 | 2020-03-20 16:43:34 +0100 | [diff] [blame] | 45 | } // namespace |
| 46 | |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 47 | // Counts and returns number of bits of a 32-bit word. |
| 48 | static int BitCount(uint32_t u32) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 49 | uint32_t tmp = |
| 50 | u32 - ((u32 >> 1) & 033333333333) - ((u32 >> 2) & 011111111111); |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 51 | tmp = ((tmp + (tmp >> 3)) & 030707070707); |
| 52 | tmp = (tmp + (tmp >> 6)); |
| 53 | tmp = (tmp + (tmp >> 12) + (tmp >> 24)) & 077; |
| 54 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 55 | return ((int)tmp); |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 56 | } |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 57 | |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 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; |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 76 | |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 77 | // Compare |binary_vector| with all rows of the |binary_matrix| |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 78 | for (; n < matrix_size; n++) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 79 | bit_counts[n] = (int32_t)BitCount(binary_vector ^ binary_matrix[n]); |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 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; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 106 | const int max_hits_for_slow_change = (candidate_delay < self->last_delay) |
| 107 | ? kMaxHitsWhenPossiblyNonCausal |
| 108 | : kMaxHitsWhenPossiblyCausal; |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 109 | int i = 0; |
| 110 | |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 111 | RTC_DCHECK_EQ(self->history_size, self->farend->history_size); |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 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) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 138 | decrease_in_last_set = |
| 139 | (self->mean_bit_counts[self->compare_delay] - valley_level_q14) * |
| 140 | kQ14Scaling; |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 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. |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 145 | for (i = 0; i < self->history_size; ++i) { |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 146 | int is_in_last_set = (i >= self->last_delay - 2) && |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 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 + |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 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? |
bjornv@webrtc.org | bccd53d | 2014-01-08 08:18:15 +0000 | [diff] [blame] | 207 | if (delay_difference > self->allowed_offset) { |
| 208 | fraction = 1.f - kFractionSlope * (delay_difference - self->allowed_offset); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 209 | fraction = (fraction > kMinFractionWhenPossiblyCausal |
| 210 | ? fraction |
| 211 | : kMinFractionWhenPossiblyCausal); |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 212 | } else if (delay_difference < 0) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 213 | fraction = |
| 214 | kMinFractionWhenPossiblyNonCausal - kFractionSlope * delay_difference; |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 215 | fraction = (fraction > 1.f ? 1.f : fraction); |
| 216 | } |
| 217 | histogram_threshold *= fraction; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 218 | histogram_threshold = |
| 219 | (histogram_threshold > kMinHistogramThreshold ? histogram_threshold |
| 220 | : kMinHistogramThreshold); |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 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|). |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 258 | is_robust = |
| 259 | (self->last_delay < 0) && (is_instantaneous_valid || is_histogram_valid); |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 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 && |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 267 | (self->histogram[candidate_delay] > self->last_delay_histogram); |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 268 | |
| 269 | return is_robust; |
| 270 | } |
| 271 | |
bjornv@webrtc.org | 94c213a | 2013-01-25 15:53:41 +0000 | [diff] [blame] | 272 | void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self) { |
bjornv@webrtc.org | 94c213a | 2013-01-25 15:53:41 +0000 | [diff] [blame] | 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 | |
| 286 | BinaryDelayEstimatorFarend* WebRtc_CreateBinaryDelayEstimatorFarend( |
| 287 | int history_size) { |
bjornv@webrtc.org | 7ded92b | 2013-01-30 16:16:59 +0000 | [diff] [blame] | 288 | BinaryDelayEstimatorFarend* self = NULL; |
bjornv@webrtc.org | 94c213a | 2013-01-25 15:53:41 +0000 | [diff] [blame] | 289 | |
bjornv@webrtc.org | 7ded92b | 2013-01-30 16:16:59 +0000 | [diff] [blame] | 290 | if (history_size > 1) { |
| 291 | // Sanity conditions fulfilled. |
peah | bdb7af6 | 2016-04-12 14:47:40 -0700 | [diff] [blame] | 292 | self = static_cast<BinaryDelayEstimatorFarend*>( |
| 293 | malloc(sizeof(BinaryDelayEstimatorFarend))); |
bjornv@webrtc.org | 7ded92b | 2013-01-30 16:16:59 +0000 | [diff] [blame] | 294 | } |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 295 | if (self == NULL) { |
| 296 | return NULL; |
bjornv@webrtc.org | 94c213a | 2013-01-25 15:53:41 +0000 | [diff] [blame] | 297 | } |
| 298 | |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 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 | } |
bjornv@webrtc.org | 94c213a | 2013-01-25 15:53:41 +0000 | [diff] [blame] | 306 | return self; |
| 307 | } |
| 308 | |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 309 | int WebRtc_AllocateFarendBufferMemory(BinaryDelayEstimatorFarend* self, |
| 310 | int history_size) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 311 | RTC_DCHECK(self); |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 312 | // (Re-)Allocate memory for history buffers. |
peah | bdb7af6 | 2016-04-12 14:47:40 -0700 | [diff] [blame] | 313 | self->binary_far_history = static_cast<uint32_t*>( |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 314 | realloc(self->binary_far_history, |
peah | bdb7af6 | 2016-04-12 14:47:40 -0700 | [diff] [blame] | 315 | history_size * sizeof(*self->binary_far_history))); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 316 | self->far_bit_counts = static_cast<int*>(realloc( |
| 317 | self->far_bit_counts, history_size * sizeof(*self->far_bit_counts))); |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 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; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 324 | memset(&self->binary_far_history[self->history_size], 0, |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 325 | sizeof(*self->binary_far_history) * size_diff); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 326 | memset(&self->far_bit_counts[self->history_size], 0, |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 327 | sizeof(*self->far_bit_counts) * size_diff); |
| 328 | } |
| 329 | self->history_size = history_size; |
| 330 | |
| 331 | return self->history_size; |
| 332 | } |
| 333 | |
bjornv@webrtc.org | 94c213a | 2013-01-25 15:53:41 +0000 | [diff] [blame] | 334 | void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 335 | RTC_DCHECK(self); |
bjornv@webrtc.org | 94c213a | 2013-01-25 15:53:41 +0000 | [diff] [blame] | 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 | |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 340 | void WebRtc_SoftResetBinaryDelayEstimatorFarend( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 341 | BinaryDelayEstimatorFarend* self, |
| 342 | int delay_shift) { |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 343 | int abs_shift = abs(delay_shift); |
| 344 | int shift_size = 0; |
bjornv@webrtc.org | 240eec3 | 2014-04-03 08:11:47 +0000 | [diff] [blame] | 345 | int dest_index = 0; |
| 346 | int src_index = 0; |
| 347 | int padding_index = 0; |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 348 | |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 349 | RTC_DCHECK(self); |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 350 | shift_size = self->history_size - abs_shift; |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 351 | RTC_DCHECK_GT(shift_size, 0); |
bjornv@webrtc.org | 240eec3 | 2014-04-03 08:11:47 +0000 | [diff] [blame] | 352 | if (delay_shift == 0) { |
| 353 | return; |
| 354 | } else if (delay_shift > 0) { |
| 355 | dest_index = abs_shift; |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 356 | } else if (delay_shift < 0) { |
bjornv@webrtc.org | 240eec3 | 2014-04-03 08:11:47 +0000 | [diff] [blame] | 357 | src_index = abs_shift; |
| 358 | padding_index = shift_size; |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 359 | } |
bjornv@webrtc.org | 240eec3 | 2014-04-03 08:11:47 +0000 | [diff] [blame] | 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); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 367 | memmove(&self->far_bit_counts[dest_index], &self->far_bit_counts[src_index], |
bjornv@webrtc.org | 240eec3 | 2014-04-03 08:11:47 +0000 | [diff] [blame] | 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); |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 371 | } |
| 372 | |
bjornv@webrtc.org | 94c213a | 2013-01-25 15:53:41 +0000 | [diff] [blame] | 373 | void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend* handle, |
| 374 | uint32_t binary_far_spectrum) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 375 | RTC_DCHECK(handle); |
bjornv@webrtc.org | 94c213a | 2013-01-25 15:53:41 +0000 | [diff] [blame] | 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 | |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 388 | void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator* self) { |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 389 | if (self == NULL) { |
bjornv@webrtc.org | bfda85f | 2012-04-16 07:28:29 +0000 | [diff] [blame] | 390 | return; |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 391 | } |
bjornv@webrtc.org | bfda85f | 2012-04-16 07:28:29 +0000 | [diff] [blame] | 392 | |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 393 | free(self->mean_bit_counts); |
| 394 | self->mean_bit_counts = NULL; |
bjornv@webrtc.org | bfda85f | 2012-04-16 07:28:29 +0000 | [diff] [blame] | 395 | |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 396 | free(self->bit_counts); |
| 397 | self->bit_counts = NULL; |
bjornv@webrtc.org | bfda85f | 2012-04-16 07:28:29 +0000 | [diff] [blame] | 398 | |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 399 | free(self->binary_near_history); |
| 400 | self->binary_near_history = NULL; |
bjornv@webrtc.org | bfda85f | 2012-04-16 07:28:29 +0000 | [diff] [blame] | 401 | |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 402 | free(self->histogram); |
| 403 | self->histogram = NULL; |
| 404 | |
bjornv@webrtc.org | 94c213a | 2013-01-25 15:53:41 +0000 | [diff] [blame] | 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. |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 407 | self->farend = NULL; |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 408 | |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 409 | free(self); |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 410 | } |
| 411 | |
bjornv@webrtc.org | 94c213a | 2013-01-25 15:53:41 +0000 | [diff] [blame] | 412 | BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 413 | BinaryDelayEstimatorFarend* farend, |
| 414 | int max_lookahead) { |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 415 | BinaryDelayEstimator* self = NULL; |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 416 | |
bjornv@webrtc.org | 240eec3 | 2014-04-03 08:11:47 +0000 | [diff] [blame] | 417 | if ((farend != NULL) && (max_lookahead >= 0)) { |
bjornv@webrtc.org | 2e72976 | 2012-04-18 08:30:29 +0000 | [diff] [blame] | 418 | // Sanity conditions fulfilled. |
peah | bdb7af6 | 2016-04-12 14:47:40 -0700 | [diff] [blame] | 419 | self = static_cast<BinaryDelayEstimator*>( |
| 420 | malloc(sizeof(BinaryDelayEstimator))); |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 421 | } |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 422 | if (self == NULL) { |
| 423 | return NULL; |
| 424 | } |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 425 | |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 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; |
bjornv@webrtc.org | 2e72976 | 2012-04-18 08:30:29 +0000 | [diff] [blame] | 431 | |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 432 | self->lookahead = max_lookahead; |
bjornv@webrtc.org | 2e72976 | 2012-04-18 08:30:29 +0000 | [diff] [blame] | 433 | |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 434 | // Allocate memory for spectrum and history buffers. |
| 435 | self->mean_bit_counts = NULL; |
| 436 | self->bit_counts = NULL; |
| 437 | self->histogram = NULL; |
peah | bdb7af6 | 2016-04-12 14:47:40 -0700 | [diff] [blame] | 438 | self->binary_near_history = static_cast<uint32_t*>( |
| 439 | malloc((max_lookahead + 1) * sizeof(*self->binary_near_history))); |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 440 | if (self->binary_near_history == NULL || |
| 441 | WebRtc_AllocateHistoryBufferMemory(self, farend->history_size) == 0) { |
| 442 | WebRtc_FreeBinaryDelayEstimator(self); |
| 443 | self = NULL; |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 444 | } |
| 445 | |
bjornv@webrtc.org | 2e72976 | 2012-04-18 08:30:29 +0000 | [diff] [blame] | 446 | return self; |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 447 | } |
| 448 | |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 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. |
peah | bdb7af6 | 2016-04-12 14:47:40 -0700 | [diff] [blame] | 460 | self->mean_bit_counts = static_cast<int32_t*>( |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 461 | realloc(self->mean_bit_counts, |
peah | bdb7af6 | 2016-04-12 14:47:40 -0700 | [diff] [blame] | 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))); |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 467 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 468 | if ((self->mean_bit_counts == NULL) || (self->bit_counts == NULL) || |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 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; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 475 | memset(&self->mean_bit_counts[self->history_size], 0, |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 476 | sizeof(*self->mean_bit_counts) * size_diff); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 477 | memset(&self->bit_counts[self->history_size], 0, |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 478 | sizeof(*self->bit_counts) * size_diff); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 479 | memset(&self->histogram[self->history_size], 0, |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 480 | sizeof(*self->histogram) * size_diff); |
| 481 | } |
| 482 | self->history_size = history_size; |
| 483 | |
| 484 | return self->history_size; |
| 485 | } |
| 486 | |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 487 | void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator* self) { |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 488 | int i = 0; |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 489 | RTC_DCHECK(self); |
andrew@webrtc.org | 828af1b | 2011-11-22 22:40:27 +0000 | [diff] [blame] | 490 | |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 491 | memset(self->bit_counts, 0, sizeof(int32_t) * self->history_size); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 492 | memset(self->binary_near_history, 0, |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 493 | sizeof(uint32_t) * self->near_history_size); |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 494 | for (i = 0; i <= self->history_size; ++i) { |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 495 | self->mean_bit_counts[i] = (20 << 9); // 20 in Q9. |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 496 | self->histogram[i] = 0.f; |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 497 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 498 | self->minimum_probability = kMaxBitCountsQ9; // 32 in Q9. |
| 499 | self->last_delay_probability = (int)kMaxBitCountsQ9; // 32 in Q9. |
andrew@webrtc.org | 828af1b | 2011-11-22 22:40:27 +0000 | [diff] [blame] | 500 | |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 501 | // Default return value if we're unable to estimate. -1 is used for errors. |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 502 | self->last_delay = -2; |
bjornv@webrtc.org | bd41a84 | 2013-11-28 14:58:35 +0000 | [diff] [blame] | 503 | |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 504 | self->last_candidate_delay = -2; |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 505 | self->compare_delay = self->history_size; |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 506 | self->candidate_hits = 0; |
| 507 | self->last_delay_histogram = 0.f; |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 508 | } |
| 509 | |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 510 | int WebRtc_SoftResetBinaryDelayEstimator(BinaryDelayEstimator* self, |
| 511 | int delay_shift) { |
| 512 | int lookahead = 0; |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 513 | RTC_DCHECK(self); |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 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 | |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 525 | int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* self, |
bjornv@webrtc.org | bb599b7 | 2013-01-18 23:16:46 +0000 | [diff] [blame] | 526 | uint32_t binary_near_spectrum) { |
| 527 | int i = 0; |
| 528 | int candidate_delay = -1; |
bjornv@webrtc.org | d1a1c35 | 2013-11-28 11:45:05 +0000 | [diff] [blame] | 529 | int valid_candidate = 0; |
bjornv@webrtc.org | bb599b7 | 2013-01-18 23:16:46 +0000 | [diff] [blame] | 530 | |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 531 | int32_t value_best_candidate = kMaxBitCountsQ9; |
bjornv@webrtc.org | bb599b7 | 2013-01-18 23:16:46 +0000 | [diff] [blame] | 532 | int32_t value_worst_candidate = 0; |
bjornv@webrtc.org | d1a1c35 | 2013-11-28 11:45:05 +0000 | [diff] [blame] | 533 | int32_t valley_depth = 0; |
bjornv@webrtc.org | bb599b7 | 2013-01-18 23:16:46 +0000 | [diff] [blame] | 534 | |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 535 | RTC_DCHECK(self); |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 536 | if (self->farend->history_size != self->history_size) { |
| 537 | // Non matching history sizes. |
| 538 | return -1; |
| 539 | } |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 540 | if (self->near_history_size > 1) { |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 541 | // If we apply lookahead, shift near-end binary spectrum history. Insert |
| 542 | // current |binary_near_spectrum| and pull out the delayed one. |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 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; |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 546 | binary_near_spectrum = self->binary_near_history[self->lookahead]; |
andrew@webrtc.org | 828af1b | 2011-11-22 22:40:27 +0000 | [diff] [blame] | 547 | } |
| 548 | |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 549 | // Compare with delayed spectra and store the |bit_counts| for each delay. |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 550 | BitCountComparison(binary_near_spectrum, self->farend->binary_far_history, |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 551 | self->history_size, self->bit_counts); |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 552 | |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 553 | // Update |mean_bit_counts|, which is the smoothed version of |bit_counts|. |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 554 | for (i = 0; i < self->history_size; i++) { |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 555 | // |bit_counts| is constrained to [0, 32], meaning we can smooth with a |
| 556 | // factor up to 2^26. We use Q9. |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 557 | int32_t bit_count = (self->bit_counts[i] << 9); // Q9. |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 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. |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 562 | if (self->farend->far_bit_counts[i] > 0) { |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 563 | // Make number of right shifts piecewise linear w.r.t. |far_bit_counts|. |
| 564 | int shifts = kShiftsAtZero; |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 565 | shifts -= (kShiftsLinearSlope * self->farend->far_bit_counts[i]) >> 4; |
| 566 | WebRtc_MeanEstimatorFix(bit_count, shifts, &(self->mean_bit_counts[i])); |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 567 | } |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 568 | } |
| 569 | |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 570 | // Find |candidate_delay|, |value_best_candidate| and |value_worst_candidate| |
| 571 | // of |mean_bit_counts|. |
bjornv@webrtc.org | 69ef991 | 2014-07-03 14:59:03 +0000 | [diff] [blame] | 572 | for (i = 0; i < self->history_size; i++) { |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 573 | if (self->mean_bit_counts[i] < value_best_candidate) { |
| 574 | value_best_candidate = self->mean_bit_counts[i]; |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 575 | candidate_delay = i; |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 576 | } |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 577 | if (self->mean_bit_counts[i] > value_worst_candidate) { |
| 578 | value_worst_candidate = self->mean_bit_counts[i]; |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
bjornv@webrtc.org | d1a1c35 | 2013-11-28 11:45:05 +0000 | [diff] [blame] | 581 | valley_depth = value_worst_candidate - value_best_candidate; |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 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|. |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 597 | if ((self->minimum_probability > kProbabilityLowerLimit) && |
bjornv@webrtc.org | d1a1c35 | 2013-11-28 11:45:05 +0000 | [diff] [blame] | 598 | (valley_depth > kProbabilityMinSpread)) { |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 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 | } |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 607 | if (self->minimum_probability > threshold) { |
| 608 | self->minimum_probability = threshold; |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | // Update |last_delay_probability|. |
| 612 | // We use a Markov type model, i.e., a slowly increasing level over time. |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 613 | self->last_delay_probability++; |
bjornv@webrtc.org | d1a1c35 | 2013-11-28 11:45:05 +0000 | [diff] [blame] | 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) && |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 623 | ((value_best_candidate < self->minimum_probability) || |
| 624 | (value_best_candidate < self->last_delay_probability))); |
bjornv@webrtc.org | d1a1c35 | 2013-11-28 11:45:05 +0000 | [diff] [blame] | 625 | |
peah | b1fc54d | 2016-05-12 05:08:45 -0700 | [diff] [blame] | 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 | |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 639 | if (self->robust_validation_enabled) { |
Bjorn Volcker | 532531b | 2015-05-06 11:58:04 +0200 | [diff] [blame] | 640 | int is_histogram_valid = HistogramBasedValidation(self, candidate_delay); |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 641 | valid_candidate = RobustValidation(self, candidate_delay, valid_candidate, |
| 642 | is_histogram_valid); |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 643 | } |
peah | b1fc54d | 2016-05-12 05:08:45 -0700 | [diff] [blame] | 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) { |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 648 | if (candidate_delay != self->last_delay) { |
bjornv@webrtc.org | 1e7d612 | 2013-12-16 13:37:28 +0000 | [diff] [blame] | 649 | self->last_delay_histogram = |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 650 | (self->histogram[candidate_delay] > kLastHistogramMax |
| 651 | ? kLastHistogramMax |
| 652 | : self->histogram[candidate_delay]); |
bjornv@webrtc.org | 1e7d612 | 2013-12-16 13:37:28 +0000 | [diff] [blame] | 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 | } |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 659 | } |
bjornv@webrtc.org | d1a1c35 | 2013-11-28 11:45:05 +0000 | [diff] [blame] | 660 | self->last_delay = candidate_delay; |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 661 | if (value_best_candidate < self->last_delay_probability) { |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 662 | self->last_delay_probability = value_best_candidate; |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 663 | } |
bjornv@webrtc.org | 5c64508 | 2013-12-16 10:57:53 +0000 | [diff] [blame] | 664 | self->compare_delay = self->last_delay; |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 665 | } |
| 666 | |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 667 | return self->last_delay; |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 668 | } |
| 669 | |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 670 | int WebRtc_binary_last_delay(BinaryDelayEstimator* self) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 671 | RTC_DCHECK(self); |
bjornv@webrtc.org | 57f3a11 | 2013-01-25 22:02:15 +0000 | [diff] [blame] | 672 | return self->last_delay; |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 673 | } |
| 674 | |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 675 | float WebRtc_binary_last_delay_quality(BinaryDelayEstimator* self) { |
| 676 | float quality = 0; |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 677 | RTC_DCHECK(self); |
bjornv@webrtc.org | a2d8b75 | 2013-01-18 21:54:15 +0000 | [diff] [blame] | 678 | |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 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. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 685 | quality = (float)(kMaxBitCountsQ9 - self->last_delay_probability) / |
| 686 | kMaxBitCountsQ9; |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 687 | if (quality < 0) { |
| 688 | quality = 0; |
| 689 | } |
bjornv@webrtc.org | 04ecd49 | 2013-03-18 14:15:12 +0000 | [diff] [blame] | 690 | } |
bjornv@webrtc.org | 28e83d1 | 2014-03-24 15:26:52 +0000 | [diff] [blame] | 691 | return quality; |
bjornv@webrtc.org | a2d8b75 | 2013-01-18 21:54:15 +0000 | [diff] [blame] | 692 | } |
| 693 | |
bjornv@webrtc.org | 6a9835d | 2011-11-18 08:30:34 +0000 | [diff] [blame] | 694 | void WebRtc_MeanEstimatorFix(int32_t new_value, |
| 695 | int factor, |
| 696 | int32_t* mean_value) { |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 697 | int32_t diff = new_value - *mean_value; |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 698 | |
| 699 | // mean_new = mean_value + ((new_value - mean_value) >> factor); |
| 700 | if (diff < 0) { |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 701 | diff = -((-diff) >> factor); |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 702 | } else { |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 703 | diff = (diff >> factor); |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 704 | } |
bjornv@webrtc.org | 70adcd4 | 2011-12-29 14:51:21 +0000 | [diff] [blame] | 705 | *mean_value += diff; |
bjornv@google.com | b47d4b2 | 2011-09-15 12:27:36 +0000 | [diff] [blame] | 706 | } |
Per Åhgren | e7175c9 | 2020-03-20 16:43:34 +0100 | [diff] [blame] | 707 | |
| 708 | } // namespace webrtc |