Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 11 #include "webrtc/video/quality_threshold.h" | |
| 12 | |
| 13 #include "webrtc/base/checks.h" | |
| 14 #include "webrtc/base/logging.h" | |
| 15 | |
| 16 namespace webrtc { | |
| 17 | |
| 18 QualityThreshold::QualityThreshold(int low_threshold, | |
| 19 int high_threshold, | |
| 20 float fraction, | |
| 21 int max_measurements) | |
| 22 : buffer_(new int[max_measurements]), | |
| 23 max_measurements_(max_measurements), | |
| 24 fraction_(fraction), | |
| 25 low_threshold_(low_threshold), | |
| 26 high_threshold_(high_threshold), | |
| 27 until_full_(max_measurements), | |
| 28 next_index_(0), | |
| 29 sum_(0), | |
| 30 count_low_(0), | |
| 31 count_high_(0) { | |
| 32 RTC_CHECK_GE(fraction, 0.5f); | |
| 33 RTC_CHECK_LT(low_threshold, high_threshold); | |
| 34 } | |
| 35 | |
| 36 void QualityThreshold::AddMeasurement(int measurement) { | |
| 37 int prev_val = until_full_ > 0 ? 0 : buffer_[next_index_]; | |
| 38 buffer_[next_index_] = measurement; | |
| 39 next_index_ = (next_index_ + 1) % max_measurements_; | |
| 40 | |
| 41 sum_ += -prev_val + measurement; | |
|
stefan-webrtc
2016/11/28 15:55:24
sum_ += measurement - prev_val;
| |
| 42 | |
| 43 if (until_full_ == 0) { | |
| 44 if (prev_val <= low_threshold_) { | |
| 45 count_low_--; | |
|
stefan-webrtc
2016/11/28 15:55:24
--count_low_ here and on all other places.
| |
| 46 } else if (prev_val >= high_threshold_) { | |
| 47 count_high_--; | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 if (measurement <= low_threshold_) { | |
| 52 count_low_++; | |
| 53 } else if (measurement >= high_threshold_) { | |
| 54 count_high_++; | |
| 55 } | |
| 56 | |
| 57 float sufficient_majority = fraction_ * max_measurements_; | |
| 58 if (count_high_ >= sufficient_majority) { | |
| 59 is_high_ = rtc::Optional<bool>(true); | |
| 60 } else if (count_low_ >= sufficient_majority) { | |
| 61 is_high_ = rtc::Optional<bool>(false); | |
| 62 } | |
| 63 | |
| 64 if (until_full_ > 0) | |
| 65 until_full_--; | |
| 66 } | |
| 67 | |
| 68 rtc::Optional<bool> QualityThreshold::IsHigh() const { | |
| 69 return is_high_; | |
| 70 } | |
| 71 | |
| 72 rtc::Optional<double> QualityThreshold::CalculateVariance() const { | |
| 73 if (until_full_ > 0) { | |
| 74 return {}; | |
|
stefan-webrtc
2016/11/28 15:55:24
Never seen this as a short for rtc::Optional<doubl
| |
| 75 } | |
| 76 | |
| 77 double variance = 0; | |
| 78 double mean = static_cast<double>(sum_) / max_measurements_; | |
| 79 for (int i = 0; i < max_measurements_; ++i) { | |
| 80 variance += (buffer_[i] - mean) * (buffer_[i] - mean); | |
| 81 } | |
| 82 return rtc::Optional<double>(variance / (max_measurements_ - 1)); | |
| 83 } | |
| 84 | |
| 85 } // namespace webrtc | |
| OLD | NEW |