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.

104 lines
2.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2016 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 "video/quality_threshold.h"
  11. #include "rtc_base/checks.h"
  12. namespace webrtc {
  13. QualityThreshold::QualityThreshold(int low_threshold,
  14. int high_threshold,
  15. float fraction,
  16. int max_measurements)
  17. : buffer_(new int[max_measurements]),
  18. max_measurements_(max_measurements),
  19. fraction_(fraction),
  20. low_threshold_(low_threshold),
  21. high_threshold_(high_threshold),
  22. until_full_(max_measurements),
  23. next_index_(0),
  24. sum_(0),
  25. count_low_(0),
  26. count_high_(0),
  27. num_high_states_(0),
  28. num_certain_states_(0) {
  29. RTC_CHECK_GT(fraction, 0.5f);
  30. RTC_CHECK_GT(max_measurements, 1);
  31. RTC_CHECK_LT(low_threshold, high_threshold);
  32. }
  33. QualityThreshold::~QualityThreshold() = default;
  34. void QualityThreshold::AddMeasurement(int measurement) {
  35. int prev_val = until_full_ > 0 ? 0 : buffer_[next_index_];
  36. buffer_[next_index_] = measurement;
  37. next_index_ = (next_index_ + 1) % max_measurements_;
  38. sum_ += measurement - prev_val;
  39. if (until_full_ == 0) {
  40. if (prev_val <= low_threshold_) {
  41. --count_low_;
  42. } else if (prev_val >= high_threshold_) {
  43. --count_high_;
  44. }
  45. }
  46. if (measurement <= low_threshold_) {
  47. ++count_low_;
  48. } else if (measurement >= high_threshold_) {
  49. ++count_high_;
  50. }
  51. float sufficient_majority = fraction_ * max_measurements_;
  52. if (count_high_ >= sufficient_majority) {
  53. is_high_ = true;
  54. } else if (count_low_ >= sufficient_majority) {
  55. is_high_ = false;
  56. }
  57. if (until_full_ > 0)
  58. --until_full_;
  59. if (is_high_) {
  60. if (*is_high_)
  61. ++num_high_states_;
  62. ++num_certain_states_;
  63. }
  64. }
  65. absl::optional<bool> QualityThreshold::IsHigh() const {
  66. return is_high_;
  67. }
  68. absl::optional<double> QualityThreshold::CalculateVariance() const {
  69. if (until_full_ > 0) {
  70. return absl::nullopt;
  71. }
  72. double variance = 0;
  73. double mean = static_cast<double>(sum_) / max_measurements_;
  74. for (int i = 0; i < max_measurements_; ++i) {
  75. variance += (buffer_[i] - mean) * (buffer_[i] - mean);
  76. }
  77. return variance / (max_measurements_ - 1);
  78. }
  79. absl::optional<double> QualityThreshold::FractionHigh(
  80. int min_required_samples) const {
  81. RTC_DCHECK_GT(min_required_samples, 0);
  82. if (num_certain_states_ < min_required_samples)
  83. return absl::nullopt;
  84. return static_cast<double>(num_high_states_) / num_certain_states_;
  85. }
  86. } // namespace webrtc