Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/neteq/histogram.h" |
| 12 | |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 13 | #include <algorithm> |
Piasy | cc35032 | 2019-03-05 23:00:36 +0800 | [diff] [blame] | 14 | #include <cstdlib> |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 15 | #include <numeric> |
| 16 | |
Jakob Ivarsson | e9a2ee2 | 2019-05-22 16:54:09 +0200 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/numerics/safe_conversions.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
Jakob Ivarsson | e9a2ee2 | 2019-05-22 16:54:09 +0200 | [diff] [blame] | 23 | Histogram::Histogram(size_t num_buckets, |
| 24 | int forget_factor, |
| 25 | absl::optional<double> start_forget_weight) |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 26 | : buckets_(num_buckets, 0), |
| 27 | forget_factor_(0), |
Jakob Ivarsson | e9a2ee2 | 2019-05-22 16:54:09 +0200 | [diff] [blame] | 28 | base_forget_factor_(forget_factor), |
| 29 | add_count_(0), |
| 30 | start_forget_weight_(start_forget_weight) { |
| 31 | RTC_DCHECK_LT(base_forget_factor_, 1 << 15); |
| 32 | } |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 33 | |
| 34 | Histogram::~Histogram() {} |
| 35 | |
| 36 | // Each element in the vector is first multiplied by the forgetting factor |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 37 | // `forget_factor_`. Then the vector element indicated by `iat_packets` is then |
| 38 | // increased (additive) by 1 - `forget_factor_`. This way, the probability of |
| 39 | // `value` is slightly increased, while the sum of the histogram remains |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 40 | // constant (=1). |
| 41 | // Due to inaccuracies in the fixed-point arithmetic, the histogram may no |
| 42 | // longer sum up to 1 (in Q30) after the update. To correct this, a correction |
| 43 | // term is added or subtracted from the first element (or elements) of the |
| 44 | // vector. |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 45 | // The forgetting factor `forget_factor_` is also updated. When the DelayManager |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 46 | // is reset, the factor is set to 0 to facilitate rapid convergence in the |
| 47 | // beginning. With each update of the histogram, the factor is increased towards |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 48 | // the steady-state value `base_forget_factor_`. |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 49 | void Histogram::Add(int value) { |
| 50 | RTC_DCHECK(value >= 0); |
| 51 | RTC_DCHECK(value < static_cast<int>(buckets_.size())); |
| 52 | int vector_sum = 0; // Sum up the vector elements as they are processed. |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 53 | // Multiply each element in `buckets_` with `forget_factor_`. |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 54 | for (int& bucket : buckets_) { |
| 55 | bucket = (static_cast<int64_t>(bucket) * forget_factor_) >> 15; |
| 56 | vector_sum += bucket; |
| 57 | } |
| 58 | |
| 59 | // Increase the probability for the currently observed inter-arrival time |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 60 | // by 1 - `forget_factor_`. The factor is in Q15, `buckets_` in Q30. |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 61 | // Thus, left-shift 15 steps to obtain result in Q30. |
| 62 | buckets_[value] += (32768 - forget_factor_) << 15; |
| 63 | vector_sum += (32768 - forget_factor_) << 15; // Add to vector sum. |
| 64 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 65 | // `buckets_` should sum up to 1 (in Q30), but it may not due to |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 66 | // fixed-point rounding errors. |
| 67 | vector_sum -= 1 << 30; // Should be zero. Compensate if not. |
| 68 | if (vector_sum != 0) { |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 69 | // Modify a few values early in `buckets_`. |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 70 | int flip_sign = vector_sum > 0 ? -1 : 1; |
| 71 | for (int& bucket : buckets_) { |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 72 | // Add/subtract 1/16 of the element, but not more than `vector_sum`. |
Piasy | cc35032 | 2019-03-05 23:00:36 +0800 | [diff] [blame] | 73 | int correction = flip_sign * std::min(std::abs(vector_sum), bucket >> 4); |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 74 | bucket += correction; |
| 75 | vector_sum += correction; |
Piasy | cc35032 | 2019-03-05 23:00:36 +0800 | [diff] [blame] | 76 | if (std::abs(vector_sum) == 0) { |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 77 | break; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | RTC_DCHECK(vector_sum == 0); // Verify that the above is correct. |
| 82 | |
Jakob Ivarsson | e9a2ee2 | 2019-05-22 16:54:09 +0200 | [diff] [blame] | 83 | ++add_count_; |
| 84 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 85 | // Update `forget_factor_` (changes only during the first seconds after a |
| 86 | // reset). The factor converges to `base_forget_factor_`. |
Jakob Ivarsson | e9a2ee2 | 2019-05-22 16:54:09 +0200 | [diff] [blame] | 87 | if (start_forget_weight_) { |
| 88 | if (forget_factor_ != base_forget_factor_) { |
| 89 | int old_forget_factor = forget_factor_; |
| 90 | int forget_factor = |
| 91 | (1 << 15) * (1 - start_forget_weight_.value() / (add_count_ + 1)); |
| 92 | forget_factor_ = |
| 93 | std::max(0, std::min(base_forget_factor_, forget_factor)); |
| 94 | // The histogram is updated recursively by forgetting the old histogram |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 95 | // with `forget_factor_` and adding a new sample multiplied by |1 - |
Jakob Ivarsson | e9a2ee2 | 2019-05-22 16:54:09 +0200 | [diff] [blame] | 96 | // forget_factor_|. We need to make sure that the effective weight on the |
| 97 | // new sample is no smaller than those on the old samples, i.e., to |
| 98 | // satisfy the following DCHECK. |
| 99 | RTC_DCHECK_GE((1 << 15) - forget_factor_, |
| 100 | ((1 << 15) - old_forget_factor) * forget_factor_ >> 15); |
| 101 | } |
| 102 | } else { |
| 103 | forget_factor_ += (base_forget_factor_ - forget_factor_ + 3) >> 2; |
| 104 | } |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | int Histogram::Quantile(int probability) { |
| 108 | // Find the bucket for which the probability of observing an |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 109 | // inter-arrival time larger than or equal to `index` is larger than or |
| 110 | // equal to `probability`. The sought probability is estimated using |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 111 | // the histogram as the reverse cumulant PDF, i.e., the sum of elements from |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 112 | // the end up until `index`. Now, since the sum of all elements is 1 |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 113 | // (in Q30) by definition, and since the solution is often a low value for |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 114 | // `iat_index`, it is more efficient to start with `sum` = 1 and subtract |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 115 | // elements from the start of the histogram. |
| 116 | int inverse_probability = (1 << 30) - probability; |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 117 | size_t index = 0; // Start from the beginning of `buckets_`. |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 118 | int sum = 1 << 30; // Assign to 1 in Q30. |
Jakob Ivarsson | a2479f7 | 2019-08-29 10:33:06 +0200 | [diff] [blame] | 119 | sum -= buckets_[index]; |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 120 | |
Jakob Ivarsson | a2479f7 | 2019-08-29 10:33:06 +0200 | [diff] [blame] | 121 | while ((sum > inverse_probability) && (index < buckets_.size() - 1)) { |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 122 | // Subtract the probabilities one by one until the sum is no longer greater |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 123 | // than `inverse_probability`. |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 124 | ++index; |
| 125 | sum -= buckets_[index]; |
Jakob Ivarsson | a2479f7 | 2019-08-29 10:33:06 +0200 | [diff] [blame] | 126 | } |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 127 | return static_cast<int>(index); |
| 128 | } |
| 129 | |
| 130 | // Set the histogram vector to an exponentially decaying distribution |
| 131 | // buckets_[i] = 0.5^(i+1), i = 0, 1, 2, ... |
| 132 | // buckets_ is in Q30. |
| 133 | void Histogram::Reset() { |
| 134 | // Set temp_prob to (slightly more than) 1 in Q14. This ensures that the sum |
| 135 | // of buckets_ is 1. |
| 136 | uint16_t temp_prob = 0x4002; // 16384 + 2 = 100000000000010 binary. |
| 137 | for (int& bucket : buckets_) { |
| 138 | temp_prob >>= 1; |
| 139 | bucket = temp_prob << 16; |
| 140 | } |
| 141 | forget_factor_ = 0; // Adapt the histogram faster for the first few packets. |
Jakob Ivarsson | e9a2ee2 | 2019-05-22 16:54:09 +0200 | [diff] [blame] | 142 | add_count_ = 0; |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int Histogram::NumBuckets() const { |
| 146 | return buckets_.size(); |
| 147 | } |
| 148 | |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 149 | } // namespace webrtc |