pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | */ |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/utility/quality_scaler.h" |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 12 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 13 | #include <memory> |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 14 | #include <utility> |
Kári Tristan Helgason | 5a20ed3 | 2016-09-15 10:56:19 +0200 | [diff] [blame] | 15 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/logging.h" |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 19 | #include "rtc_base/numerics/exp_filter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/task_queue.h" |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 21 | |
Kári Tristan Helgason | 5a20ed3 | 2016-09-15 10:56:19 +0200 | [diff] [blame] | 22 | // TODO(kthelgason): Some versions of Android have issues with log2. |
| 23 | // See https://code.google.com/p/android/issues/detail?id=212634 for details |
| 24 | #if defined(WEBRTC_ANDROID) |
| 25 | #define log2(x) (log(x) / log(2)) |
| 26 | #endif |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 27 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 28 | namespace webrtc { |
| 29 | |
Peter Boström | 926dfcd | 2016-04-14 14:48:10 +0200 | [diff] [blame] | 30 | namespace { |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 31 | // TODO(nisse): Delete, delegate to encoders. |
pbos | cbac40d | 2016-04-13 02:51:02 -0700 | [diff] [blame] | 32 | // Threshold constant used until first downscale (to permit fast rampup). |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 33 | static const int kMeasureMs = 2000; |
| 34 | static const float kSamplePeriodScaleFactor = 2.5; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 35 | static const int kFramedropPercentThreshold = 60; |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 36 | static const int kMinFramesNeededToScale = 2 * 30; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 37 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 38 | } // namespace |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 39 | |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 40 | class QualityScaler::QpSmoother { |
| 41 | public: |
| 42 | explicit QpSmoother(float alpha) |
Sebastian Jansson | b678940 | 2019-03-01 15:40:49 +0100 | [diff] [blame] | 43 | : alpha_(alpha), |
| 44 | // The initial value of last_sample_ms doesn't matter since the smoother |
| 45 | // will ignore the time delta for the first update. |
| 46 | last_sample_ms_(0), |
| 47 | smoother_(alpha) {} |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 48 | |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 49 | absl::optional<int> GetAvg() const { |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 50 | float value = smoother_.filtered(); |
| 51 | if (value == rtc::ExpFilter::kValueUndefined) { |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 52 | return absl::nullopt; |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 53 | } |
| 54 | return static_cast<int>(value); |
| 55 | } |
| 56 | |
Sebastian Jansson | b678940 | 2019-03-01 15:40:49 +0100 | [diff] [blame] | 57 | void Add(float sample, int64_t time_sent_us) { |
| 58 | int64_t now_ms = time_sent_us / 1000; |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 59 | smoother_.Apply(static_cast<float>(now_ms - last_sample_ms_), sample); |
| 60 | last_sample_ms_ = now_ms; |
| 61 | } |
| 62 | |
| 63 | void Reset() { smoother_.Reset(alpha_); } |
| 64 | |
| 65 | private: |
| 66 | const float alpha_; |
| 67 | int64_t last_sample_ms_; |
| 68 | rtc::ExpFilter smoother_; |
| 69 | }; |
| 70 | |
Sebastian Jansson | cda86dd | 2019-03-11 17:26:36 +0100 | [diff] [blame] | 71 | QualityScaler::QualityScaler(rtc::TaskQueue* task_queue, |
| 72 | AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 73 | VideoEncoder::QpThresholds thresholds) |
Sebastian Jansson | cda86dd | 2019-03-11 17:26:36 +0100 | [diff] [blame] | 74 | : QualityScaler(task_queue, observer, thresholds, kMeasureMs) {} |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 75 | |
| 76 | // Protected ctor, should not be called directly. |
Sebastian Jansson | cda86dd | 2019-03-11 17:26:36 +0100 | [diff] [blame] | 77 | QualityScaler::QualityScaler(rtc::TaskQueue* task_queue, |
| 78 | AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 79 | VideoEncoder::QpThresholds thresholds, |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 80 | int64_t sampling_period_ms) |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 81 | : observer_(observer), |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 82 | thresholds_(thresholds), |
| 83 | sampling_period_ms_(sampling_period_ms), |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 84 | fast_rampup_(true), |
| 85 | // Arbitrarily choose size based on 30 fps for 5 seconds. |
| 86 | average_qp_(5 * 30), |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 87 | framedrop_percent_media_opt_(5 * 30), |
| 88 | framedrop_percent_all_(5 * 30), |
| 89 | experiment_enabled_(QualityScalingExperiment::Enabled()), |
| 90 | observed_enough_frames_(false) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 91 | RTC_DCHECK_RUN_ON(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 92 | if (experiment_enabled_) { |
| 93 | config_ = QualityScalingExperiment::GetConfig(); |
| 94 | qp_smoother_high_.reset(new QpSmoother(config_.alpha_high)); |
| 95 | qp_smoother_low_.reset(new QpSmoother(config_.alpha_low)); |
| 96 | } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 97 | RTC_DCHECK(observer_ != nullptr); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 98 | check_qp_task_ = RepeatingTaskHandle::DelayedStart( |
Sebastian Jansson | cda86dd | 2019-03-11 17:26:36 +0100 | [diff] [blame] | 99 | task_queue->Get(), TimeDelta::ms(GetSamplingPeriodMs()), [this]() { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 100 | CheckQp(); |
| 101 | return TimeDelta::ms(GetSamplingPeriodMs()); |
| 102 | }); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 103 | RTC_LOG(LS_INFO) << "QP thresholds: low: " << thresholds_.low |
| 104 | << ", high: " << thresholds_.high; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 105 | } |
| 106 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 107 | QualityScaler::~QualityScaler() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 108 | RTC_DCHECK_RUN_ON(&task_checker_); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 109 | check_qp_task_.Stop(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int64_t QualityScaler::GetSamplingPeriodMs() const { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 113 | RTC_DCHECK_RUN_ON(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 114 | if (fast_rampup_) { |
| 115 | return sampling_period_ms_; |
| 116 | } |
| 117 | if (experiment_enabled_ && !observed_enough_frames_) { |
| 118 | // Use half the interval while waiting for enough frames. |
| 119 | return sampling_period_ms_ / 2; |
| 120 | } |
| 121 | return sampling_period_ms_ * kSamplePeriodScaleFactor; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 124 | void QualityScaler::ReportDroppedFrameByMediaOpt() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 125 | RTC_DCHECK_RUN_ON(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 126 | framedrop_percent_media_opt_.AddSample(100); |
| 127 | framedrop_percent_all_.AddSample(100); |
| 128 | } |
| 129 | |
| 130 | void QualityScaler::ReportDroppedFrameByEncoder() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 131 | RTC_DCHECK_RUN_ON(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 132 | framedrop_percent_all_.AddSample(100); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Sebastian Jansson | b678940 | 2019-03-01 15:40:49 +0100 | [diff] [blame] | 135 | void QualityScaler::ReportQp(int qp, int64_t time_sent_us) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 136 | RTC_DCHECK_RUN_ON(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 137 | framedrop_percent_media_opt_.AddSample(0); |
| 138 | framedrop_percent_all_.AddSample(0); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 139 | average_qp_.AddSample(qp); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 140 | if (qp_smoother_high_) |
Sebastian Jansson | b678940 | 2019-03-01 15:40:49 +0100 | [diff] [blame] | 141 | qp_smoother_high_->Add(qp, time_sent_us); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 142 | if (qp_smoother_low_) |
Sebastian Jansson | b678940 | 2019-03-01 15:40:49 +0100 | [diff] [blame] | 143 | qp_smoother_low_->Add(qp, time_sent_us); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 146 | void QualityScaler::CheckQp() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 147 | RTC_DCHECK_RUN_ON(&task_checker_); |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 148 | // Should be set through InitEncode -> Should be set by now. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 149 | RTC_DCHECK_GE(thresholds_.low, 0); |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 150 | |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 151 | // If we have not observed at least this many frames we can't make a good |
| 152 | // scaling decision. |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 153 | const size_t frames = config_.use_all_drop_reasons |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 154 | ? framedrop_percent_all_.Size() |
| 155 | : framedrop_percent_media_opt_.Size(); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 156 | if (frames < kMinFramesNeededToScale) { |
| 157 | observed_enough_frames_ = false; |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 158 | return; |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 159 | } |
| 160 | observed_enough_frames_ = true; |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 161 | |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 162 | // Check if we should scale down due to high frame drop. |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 163 | const absl::optional<int> drop_rate = |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 164 | config_.use_all_drop_reasons |
| 165 | ? framedrop_percent_all_.GetAverageRoundedDown() |
| 166 | : framedrop_percent_media_opt_.GetAverageRoundedDown(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 167 | if (drop_rate && *drop_rate >= kFramedropPercentThreshold) { |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 168 | RTC_LOG(LS_INFO) << "Reporting high QP, framedrop percent " << *drop_rate; |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 169 | ReportQpHigh(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
| 173 | // Check if we should scale up or down based on QP. |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 174 | const absl::optional<int> avg_qp_high = |
| 175 | qp_smoother_high_ ? qp_smoother_high_->GetAvg() |
| 176 | : average_qp_.GetAverageRoundedDown(); |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 177 | const absl::optional<int> avg_qp_low = |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 178 | qp_smoother_low_ ? qp_smoother_low_->GetAvg() |
| 179 | : average_qp_.GetAverageRoundedDown(); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 180 | if (avg_qp_high && avg_qp_low) { |
| 181 | RTC_LOG(LS_INFO) << "Checking average QP " << *avg_qp_high << " (" |
| 182 | << *avg_qp_low << ")."; |
| 183 | if (*avg_qp_high > thresholds_.high) { |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 184 | ReportQpHigh(); |
glaznev | d1c4435 | 2017-03-23 14:40:08 -0700 | [diff] [blame] | 185 | return; |
| 186 | } |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 187 | if (*avg_qp_low <= thresholds_.low) { |
glaznev | d1c4435 | 2017-03-23 14:40:08 -0700 | [diff] [blame] | 188 | // QP has been low. We want to try a higher resolution. |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 189 | ReportQpLow(); |
glaznev | d1c4435 | 2017-03-23 14:40:08 -0700 | [diff] [blame] | 190 | return; |
| 191 | } |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 195 | void QualityScaler::ReportQpLow() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 196 | RTC_DCHECK_RUN_ON(&task_checker_); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 197 | ClearSamples(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 198 | observer_->AdaptUp(AdaptationObserverInterface::AdaptReason::kQuality); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 201 | void QualityScaler::ReportQpHigh() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 202 | RTC_DCHECK_RUN_ON(&task_checker_); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 203 | ClearSamples(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 204 | observer_->AdaptDown(AdaptationObserverInterface::AdaptReason::kQuality); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 205 | // If we've scaled down, wait longer before scaling up again. |
| 206 | if (fast_rampup_) { |
| 207 | fast_rampup_ = false; |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 208 | } |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 209 | } |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 210 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 211 | void QualityScaler::ClearSamples() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 212 | RTC_DCHECK_RUN_ON(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 213 | framedrop_percent_media_opt_.Reset(); |
| 214 | framedrop_percent_all_.Reset(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 215 | average_qp_.Reset(); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 216 | if (qp_smoother_high_) |
| 217 | qp_smoother_high_->Reset(); |
| 218 | if (qp_smoother_low_) |
| 219 | qp_smoother_low_->Reset(); |
pbos | cbac40d | 2016-04-13 02:51:02 -0700 | [diff] [blame] | 220 | } |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 221 | } // namespace webrtc |