You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
8.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "modules/audio_coding/neteq/histogram.h"
  11. #include <algorithm>
  12. #include <cstdlib>
  13. #include <numeric>
  14. #include "absl/types/optional.h"
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/numerics/safe_conversions.h"
  17. namespace webrtc {
  18. Histogram::Histogram(size_t num_buckets,
  19. int forget_factor,
  20. absl::optional<double> start_forget_weight)
  21. : buckets_(num_buckets, 0),
  22. forget_factor_(0),
  23. base_forget_factor_(forget_factor),
  24. add_count_(0),
  25. start_forget_weight_(start_forget_weight) {
  26. RTC_DCHECK_LT(base_forget_factor_, 1 << 15);
  27. }
  28. Histogram::~Histogram() {}
  29. // Each element in the vector is first multiplied by the forgetting factor
  30. // |forget_factor_|. Then the vector element indicated by |iat_packets| is then
  31. // increased (additive) by 1 - |forget_factor_|. This way, the probability of
  32. // |value| is slightly increased, while the sum of the histogram remains
  33. // constant (=1).
  34. // Due to inaccuracies in the fixed-point arithmetic, the histogram may no
  35. // longer sum up to 1 (in Q30) after the update. To correct this, a correction
  36. // term is added or subtracted from the first element (or elements) of the
  37. // vector.
  38. // The forgetting factor |forget_factor_| is also updated. When the DelayManager
  39. // is reset, the factor is set to 0 to facilitate rapid convergence in the
  40. // beginning. With each update of the histogram, the factor is increased towards
  41. // the steady-state value |base_forget_factor_|.
  42. void Histogram::Add(int value) {
  43. RTC_DCHECK(value >= 0);
  44. RTC_DCHECK(value < static_cast<int>(buckets_.size()));
  45. int vector_sum = 0; // Sum up the vector elements as they are processed.
  46. // Multiply each element in |buckets_| with |forget_factor_|.
  47. for (int& bucket : buckets_) {
  48. bucket = (static_cast<int64_t>(bucket) * forget_factor_) >> 15;
  49. vector_sum += bucket;
  50. }
  51. // Increase the probability for the currently observed inter-arrival time
  52. // by 1 - |forget_factor_|. The factor is in Q15, |buckets_| in Q30.
  53. // Thus, left-shift 15 steps to obtain result in Q30.
  54. buckets_[value] += (32768 - forget_factor_) << 15;
  55. vector_sum += (32768 - forget_factor_) << 15; // Add to vector sum.
  56. // |buckets_| should sum up to 1 (in Q30), but it may not due to
  57. // fixed-point rounding errors.
  58. vector_sum -= 1 << 30; // Should be zero. Compensate if not.
  59. if (vector_sum != 0) {
  60. // Modify a few values early in |buckets_|.
  61. int flip_sign = vector_sum > 0 ? -1 : 1;
  62. for (int& bucket : buckets_) {
  63. // Add/subtract 1/16 of the element, but not more than |vector_sum|.
  64. int correction = flip_sign * std::min(std::abs(vector_sum), bucket >> 4);
  65. bucket += correction;
  66. vector_sum += correction;
  67. if (std::abs(vector_sum) == 0) {
  68. break;
  69. }
  70. }
  71. }
  72. RTC_DCHECK(vector_sum == 0); // Verify that the above is correct.
  73. ++add_count_;
  74. // Update |forget_factor_| (changes only during the first seconds after a
  75. // reset). The factor converges to |base_forget_factor_|.
  76. if (start_forget_weight_) {
  77. if (forget_factor_ != base_forget_factor_) {
  78. int old_forget_factor = forget_factor_;
  79. int forget_factor =
  80. (1 << 15) * (1 - start_forget_weight_.value() / (add_count_ + 1));
  81. forget_factor_ =
  82. std::max(0, std::min(base_forget_factor_, forget_factor));
  83. // The histogram is updated recursively by forgetting the old histogram
  84. // with |forget_factor_| and adding a new sample multiplied by |1 -
  85. // forget_factor_|. We need to make sure that the effective weight on the
  86. // new sample is no smaller than those on the old samples, i.e., to
  87. // satisfy the following DCHECK.
  88. RTC_DCHECK_GE((1 << 15) - forget_factor_,
  89. ((1 << 15) - old_forget_factor) * forget_factor_ >> 15);
  90. }
  91. } else {
  92. forget_factor_ += (base_forget_factor_ - forget_factor_ + 3) >> 2;
  93. }
  94. }
  95. int Histogram::Quantile(int probability) {
  96. // Find the bucket for which the probability of observing an
  97. // inter-arrival time larger than or equal to |index| is larger than or
  98. // equal to |probability|. The sought probability is estimated using
  99. // the histogram as the reverse cumulant PDF, i.e., the sum of elements from
  100. // the end up until |index|. Now, since the sum of all elements is 1
  101. // (in Q30) by definition, and since the solution is often a low value for
  102. // |iat_index|, it is more efficient to start with |sum| = 1 and subtract
  103. // elements from the start of the histogram.
  104. int inverse_probability = (1 << 30) - probability;
  105. size_t index = 0; // Start from the beginning of |buckets_|.
  106. int sum = 1 << 30; // Assign to 1 in Q30.
  107. sum -= buckets_[index];
  108. while ((sum > inverse_probability) && (index < buckets_.size() - 1)) {
  109. // Subtract the probabilities one by one until the sum is no longer greater
  110. // than |inverse_probability|.
  111. ++index;
  112. sum -= buckets_[index];
  113. }
  114. return static_cast<int>(index);
  115. }
  116. // Set the histogram vector to an exponentially decaying distribution
  117. // buckets_[i] = 0.5^(i+1), i = 0, 1, 2, ...
  118. // buckets_ is in Q30.
  119. void Histogram::Reset() {
  120. // Set temp_prob to (slightly more than) 1 in Q14. This ensures that the sum
  121. // of buckets_ is 1.
  122. uint16_t temp_prob = 0x4002; // 16384 + 2 = 100000000000010 binary.
  123. for (int& bucket : buckets_) {
  124. temp_prob >>= 1;
  125. bucket = temp_prob << 16;
  126. }
  127. forget_factor_ = 0; // Adapt the histogram faster for the first few packets.
  128. add_count_ = 0;
  129. }
  130. int Histogram::NumBuckets() const {
  131. return buckets_.size();
  132. }
  133. void Histogram::Scale(int old_bucket_width, int new_bucket_width) {
  134. buckets_ = ScaleBuckets(buckets_, old_bucket_width, new_bucket_width);
  135. }
  136. std::vector<int> Histogram::ScaleBuckets(const std::vector<int>& buckets,
  137. int old_bucket_width,
  138. int new_bucket_width) {
  139. RTC_DCHECK_GT(old_bucket_width, 0);
  140. RTC_DCHECK_GT(new_bucket_width, 0);
  141. RTC_DCHECK_EQ(old_bucket_width % 10, 0);
  142. RTC_DCHECK_EQ(new_bucket_width % 10, 0);
  143. std::vector<int> new_histogram(buckets.size(), 0);
  144. int64_t acc = 0;
  145. int time_counter = 0;
  146. size_t new_histogram_idx = 0;
  147. for (size_t i = 0; i < buckets.size(); i++) {
  148. acc += buckets[i];
  149. time_counter += old_bucket_width;
  150. // The bins should be scaled, to ensure the histogram still sums to one.
  151. const int64_t scaled_acc = acc * new_bucket_width / time_counter;
  152. int64_t actually_used_acc = 0;
  153. while (time_counter >= new_bucket_width) {
  154. const int64_t old_histogram_val = new_histogram[new_histogram_idx];
  155. new_histogram[new_histogram_idx] =
  156. rtc::saturated_cast<int>(old_histogram_val + scaled_acc);
  157. actually_used_acc += new_histogram[new_histogram_idx] - old_histogram_val;
  158. new_histogram_idx =
  159. std::min(new_histogram_idx + 1, new_histogram.size() - 1);
  160. time_counter -= new_bucket_width;
  161. }
  162. // Only subtract the part that was succesfully written to the new histogram.
  163. acc -= actually_used_acc;
  164. }
  165. // If there is anything left in acc (due to rounding errors), add it to the
  166. // last bin. If we cannot add everything to the last bin we need to add as
  167. // much as possible to the bins after the last bin (this is only possible
  168. // when compressing a histogram).
  169. while (acc > 0 && new_histogram_idx < new_histogram.size()) {
  170. const int64_t old_histogram_val = new_histogram[new_histogram_idx];
  171. new_histogram[new_histogram_idx] =
  172. rtc::saturated_cast<int>(old_histogram_val + acc);
  173. acc -= new_histogram[new_histogram_idx] - old_histogram_val;
  174. new_histogram_idx++;
  175. }
  176. RTC_DCHECK_EQ(buckets.size(), new_histogram.size());
  177. if (acc == 0) {
  178. // If acc is non-zero, we were not able to add everything to the new
  179. // histogram, so this check will not hold.
  180. RTC_DCHECK_EQ(accumulate(buckets.begin(), buckets.end(), 0ll),
  181. accumulate(new_histogram.begin(), new_histogram.end(), 0ll));
  182. }
  183. return new_histogram;
  184. }
  185. } // namespace webrtc