palmkvist | 349092b | 2016-12-13 02:45:57 -0800 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "video/quality_threshold.h" |
palmkvist | 349092b | 2016-12-13 02:45:57 -0800 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/checks.h" |
| 14 | #include "rtc_base/logging.h" |
palmkvist | 349092b | 2016-12-13 02:45:57 -0800 | [diff] [blame] | 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), |
palmkvist | a40672a | 2017-01-13 05:58:34 -0800 | [diff] [blame] | 31 | count_high_(0), |
| 32 | num_high_states_(0), |
| 33 | num_certain_states_(0) { |
palmkvist | 349092b | 2016-12-13 02:45:57 -0800 | [diff] [blame] | 34 | RTC_CHECK_GT(fraction, 0.5f); |
| 35 | RTC_CHECK_GT(max_measurements, 1); |
| 36 | RTC_CHECK_LT(low_threshold, high_threshold); |
| 37 | } |
| 38 | |
| 39 | void QualityThreshold::AddMeasurement(int measurement) { |
| 40 | int prev_val = until_full_ > 0 ? 0 : buffer_[next_index_]; |
| 41 | buffer_[next_index_] = measurement; |
| 42 | next_index_ = (next_index_ + 1) % max_measurements_; |
| 43 | |
| 44 | sum_ += measurement - prev_val; |
| 45 | |
| 46 | if (until_full_ == 0) { |
| 47 | if (prev_val <= low_threshold_) { |
| 48 | --count_low_; |
| 49 | } else if (prev_val >= high_threshold_) { |
| 50 | --count_high_; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if (measurement <= low_threshold_) { |
| 55 | ++count_low_; |
| 56 | } else if (measurement >= high_threshold_) { |
| 57 | ++count_high_; |
| 58 | } |
| 59 | |
| 60 | float sufficient_majority = fraction_ * max_measurements_; |
| 61 | if (count_high_ >= sufficient_majority) { |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 62 | is_high_ = true; |
palmkvist | 349092b | 2016-12-13 02:45:57 -0800 | [diff] [blame] | 63 | } else if (count_low_ >= sufficient_majority) { |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 64 | is_high_ = false; |
palmkvist | 349092b | 2016-12-13 02:45:57 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (until_full_ > 0) |
| 68 | --until_full_; |
palmkvist | a40672a | 2017-01-13 05:58:34 -0800 | [diff] [blame] | 69 | |
| 70 | if (is_high_) { |
| 71 | if (*is_high_) |
| 72 | ++num_high_states_; |
| 73 | ++num_certain_states_; |
| 74 | } |
palmkvist | 349092b | 2016-12-13 02:45:57 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | rtc::Optional<bool> QualityThreshold::IsHigh() const { |
| 78 | return is_high_; |
| 79 | } |
| 80 | |
| 81 | rtc::Optional<double> QualityThreshold::CalculateVariance() const { |
| 82 | if (until_full_ > 0) { |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 83 | return rtc::nullopt; |
palmkvist | 349092b | 2016-12-13 02:45:57 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | double variance = 0; |
| 87 | double mean = static_cast<double>(sum_) / max_measurements_; |
| 88 | for (int i = 0; i < max_measurements_; ++i) { |
| 89 | variance += (buffer_[i] - mean) * (buffer_[i] - mean); |
| 90 | } |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 91 | return variance / (max_measurements_ - 1); |
palmkvist | 349092b | 2016-12-13 02:45:57 -0800 | [diff] [blame] | 92 | } |
| 93 | |
palmkvist | a40672a | 2017-01-13 05:58:34 -0800 | [diff] [blame] | 94 | rtc::Optional<double> QualityThreshold::FractionHigh( |
| 95 | int min_required_samples) const { |
| 96 | RTC_DCHECK_GT(min_required_samples, 0); |
| 97 | if (num_certain_states_ < min_required_samples) |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 98 | return rtc::nullopt; |
palmkvist | a40672a | 2017-01-13 05:58:34 -0800 | [diff] [blame] | 99 | |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 100 | return static_cast<double>(num_high_states_) / num_certain_states_; |
palmkvist | a40672a | 2017-01-13 05:58:34 -0800 | [diff] [blame] | 101 | } |
| 102 | |
palmkvist | 349092b | 2016-12-13 02:45:57 -0800 | [diff] [blame] | 103 | } // namespace webrtc |