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> |
Kári Tristan Helgason | 5a20ed3 | 2016-09-15 10:56:19 +0200 | [diff] [blame] | 14 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/logging.h" |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 18 | #include "rtc_base/numerics/exp_filter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/task_queue.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 20 | #include "rtc_base/time_utils.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) |
| 43 | : alpha_(alpha), last_sample_ms_(rtc::TimeMillis()), smoother_(alpha) {} |
| 44 | |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 45 | absl::optional<int> GetAvg() const { |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 46 | float value = smoother_.filtered(); |
| 47 | if (value == rtc::ExpFilter::kValueUndefined) { |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 48 | return absl::nullopt; |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 49 | } |
| 50 | return static_cast<int>(value); |
| 51 | } |
| 52 | |
| 53 | void Add(float sample) { |
| 54 | int64_t now_ms = rtc::TimeMillis(); |
| 55 | smoother_.Apply(static_cast<float>(now_ms - last_sample_ms_), sample); |
| 56 | last_sample_ms_ = now_ms; |
| 57 | } |
| 58 | |
| 59 | void Reset() { smoother_.Reset(alpha_); } |
| 60 | |
| 61 | private: |
| 62 | const float alpha_; |
| 63 | int64_t last_sample_ms_; |
| 64 | rtc::ExpFilter smoother_; |
| 65 | }; |
| 66 | |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 67 | class QualityScaler::CheckQpTask : public rtc::QueuedTask { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 68 | public: |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 69 | explicit CheckQpTask(QualityScaler* scaler) : scaler_(scaler) { |
| 70 | RTC_LOG(LS_INFO) << "Created CheckQpTask. Scheduling on queue..."; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 71 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 72 | std::unique_ptr<rtc::QueuedTask>(this), scaler_->GetSamplingPeriodMs()); |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 73 | } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 74 | void Stop() { |
| 75 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 76 | RTC_LOG(LS_INFO) << "Stopping QP Check task."; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 77 | stop_ = true; |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | bool Run() override { |
| 82 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 83 | if (stop_) |
| 84 | return true; // TaskQueue will free this task. |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 85 | scaler_->CheckQp(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 86 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 87 | std::unique_ptr<rtc::QueuedTask>(this), scaler_->GetSamplingPeriodMs()); |
| 88 | return false; // Retain the task in order to reuse it. |
| 89 | } |
| 90 | |
| 91 | QualityScaler* const scaler_; |
| 92 | bool stop_ = false; |
| 93 | rtc::SequencedTaskChecker task_checker_; |
| 94 | }; |
| 95 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 96 | QualityScaler::QualityScaler(AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 97 | VideoEncoder::QpThresholds thresholds) |
| 98 | : QualityScaler(observer, thresholds, kMeasureMs) {} |
| 99 | |
| 100 | // Protected ctor, should not be called directly. |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 101 | QualityScaler::QualityScaler(AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 102 | VideoEncoder::QpThresholds thresholds, |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 103 | int64_t sampling_period_ms) |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 104 | : check_qp_task_(nullptr), |
| 105 | observer_(observer), |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 106 | thresholds_(thresholds), |
| 107 | sampling_period_ms_(sampling_period_ms), |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 108 | fast_rampup_(true), |
| 109 | // Arbitrarily choose size based on 30 fps for 5 seconds. |
| 110 | average_qp_(5 * 30), |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 111 | framedrop_percent_media_opt_(5 * 30), |
| 112 | framedrop_percent_all_(5 * 30), |
| 113 | experiment_enabled_(QualityScalingExperiment::Enabled()), |
| 114 | observed_enough_frames_(false) { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 115 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 116 | if (experiment_enabled_) { |
| 117 | config_ = QualityScalingExperiment::GetConfig(); |
| 118 | qp_smoother_high_.reset(new QpSmoother(config_.alpha_high)); |
| 119 | qp_smoother_low_.reset(new QpSmoother(config_.alpha_low)); |
| 120 | } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 121 | RTC_DCHECK(observer_ != nullptr); |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 122 | check_qp_task_ = new CheckQpTask(this); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 123 | RTC_LOG(LS_INFO) << "QP thresholds: low: " << thresholds_.low |
| 124 | << ", high: " << thresholds_.high; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 125 | } |
| 126 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 127 | QualityScaler::~QualityScaler() { |
| 128 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 129 | check_qp_task_->Stop(); |
| 130 | } |
| 131 | |
| 132 | int64_t QualityScaler::GetSamplingPeriodMs() const { |
| 133 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 134 | if (fast_rampup_) { |
| 135 | return sampling_period_ms_; |
| 136 | } |
| 137 | if (experiment_enabled_ && !observed_enough_frames_) { |
| 138 | // Use half the interval while waiting for enough frames. |
| 139 | return sampling_period_ms_ / 2; |
| 140 | } |
| 141 | return sampling_period_ms_ * kSamplePeriodScaleFactor; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 144 | void QualityScaler::ReportDroppedFrameByMediaOpt() { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 145 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 146 | framedrop_percent_media_opt_.AddSample(100); |
| 147 | framedrop_percent_all_.AddSample(100); |
| 148 | } |
| 149 | |
| 150 | void QualityScaler::ReportDroppedFrameByEncoder() { |
| 151 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 152 | framedrop_percent_all_.AddSample(100); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 155 | void QualityScaler::ReportQp(int qp) { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 156 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 157 | framedrop_percent_media_opt_.AddSample(0); |
| 158 | framedrop_percent_all_.AddSample(0); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 159 | average_qp_.AddSample(qp); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 160 | if (qp_smoother_high_) |
| 161 | qp_smoother_high_->Add(qp); |
| 162 | if (qp_smoother_low_) |
| 163 | qp_smoother_low_->Add(qp); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 166 | void QualityScaler::CheckQp() { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 167 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 168 | // Should be set through InitEncode -> Should be set by now. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 169 | RTC_DCHECK_GE(thresholds_.low, 0); |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 170 | |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 171 | // If we have not observed at least this many frames we can't make a good |
| 172 | // scaling decision. |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 173 | const size_t frames = config_.use_all_drop_reasons |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 174 | ? framedrop_percent_all_.Size() |
| 175 | : framedrop_percent_media_opt_.Size(); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 176 | if (frames < kMinFramesNeededToScale) { |
| 177 | observed_enough_frames_ = false; |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 178 | return; |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 179 | } |
| 180 | observed_enough_frames_ = true; |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 181 | |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 182 | // Check if we should scale down due to high frame drop. |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 183 | const absl::optional<int> drop_rate = |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 184 | config_.use_all_drop_reasons |
| 185 | ? framedrop_percent_all_.GetAverageRoundedDown() |
| 186 | : framedrop_percent_media_opt_.GetAverageRoundedDown(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 187 | if (drop_rate && *drop_rate >= kFramedropPercentThreshold) { |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 188 | RTC_LOG(LS_INFO) << "Reporting high QP, framedrop percent " << *drop_rate; |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 189 | ReportQpHigh(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 190 | return; |
| 191 | } |
| 192 | |
| 193 | // Check if we should scale up or down based on QP. |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 194 | const absl::optional<int> avg_qp_high = |
| 195 | qp_smoother_high_ ? qp_smoother_high_->GetAvg() |
| 196 | : average_qp_.GetAverageRoundedDown(); |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 197 | const absl::optional<int> avg_qp_low = |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 198 | qp_smoother_low_ ? qp_smoother_low_->GetAvg() |
| 199 | : average_qp_.GetAverageRoundedDown(); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 200 | if (avg_qp_high && avg_qp_low) { |
| 201 | RTC_LOG(LS_INFO) << "Checking average QP " << *avg_qp_high << " (" |
| 202 | << *avg_qp_low << ")."; |
| 203 | if (*avg_qp_high > thresholds_.high) { |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 204 | ReportQpHigh(); |
glaznev | d1c4435 | 2017-03-23 14:40:08 -0700 | [diff] [blame] | 205 | return; |
| 206 | } |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 207 | if (*avg_qp_low <= thresholds_.low) { |
glaznev | d1c4435 | 2017-03-23 14:40:08 -0700 | [diff] [blame] | 208 | // QP has been low. We want to try a higher resolution. |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 209 | ReportQpLow(); |
glaznev | d1c4435 | 2017-03-23 14:40:08 -0700 | [diff] [blame] | 210 | return; |
| 211 | } |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 215 | void QualityScaler::ReportQpLow() { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 216 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 217 | ClearSamples(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 218 | observer_->AdaptUp(AdaptationObserverInterface::AdaptReason::kQuality); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 221 | void QualityScaler::ReportQpHigh() { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 222 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 223 | ClearSamples(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 224 | observer_->AdaptDown(AdaptationObserverInterface::AdaptReason::kQuality); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 225 | // If we've scaled down, wait longer before scaling up again. |
| 226 | if (fast_rampup_) { |
| 227 | fast_rampup_ = false; |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 228 | } |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 229 | } |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 230 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 231 | void QualityScaler::ClearSamples() { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 232 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 233 | framedrop_percent_media_opt_.Reset(); |
| 234 | framedrop_percent_all_.Reset(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 235 | average_qp_.Reset(); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 236 | if (qp_smoother_high_) |
| 237 | qp_smoother_high_->Reset(); |
| 238 | if (qp_smoother_low_) |
| 239 | qp_smoother_low_->Reset(); |
pbos | cbac40d | 2016-04-13 02:51:02 -0700 | [diff] [blame] | 240 | } |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 241 | } // namespace webrtc |