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 | |
Kári Tristan Helgason | 5a20ed3 | 2016-09-15 10:56:19 +0200 | [diff] [blame] | 13 | #include <math.h> |
| 14 | |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 15 | #include <algorithm> |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 16 | #include <memory> |
Kári Tristan Helgason | 5a20ed3 | 2016-09-15 10:56:19 +0200 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/logging.h" |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 20 | #include "rtc_base/numerics/exp_filter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/task_queue.h" |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 22 | #include "rtc_base/timeutils.h" |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 23 | |
Kári Tristan Helgason | 5a20ed3 | 2016-09-15 10:56:19 +0200 | [diff] [blame] | 24 | // TODO(kthelgason): Some versions of Android have issues with log2. |
| 25 | // See https://code.google.com/p/android/issues/detail?id=212634 for details |
| 26 | #if defined(WEBRTC_ANDROID) |
| 27 | #define log2(x) (log(x) / log(2)) |
| 28 | #endif |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 29 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 30 | namespace webrtc { |
| 31 | |
Peter Boström | 926dfcd | 2016-04-14 14:48:10 +0200 | [diff] [blame] | 32 | namespace { |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 33 | // TODO(nisse): Delete, delegate to encoders. |
pbos | cbac40d | 2016-04-13 02:51:02 -0700 | [diff] [blame] | 34 | // Threshold constant used until first downscale (to permit fast rampup). |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 35 | static const int kMeasureMs = 2000; |
| 36 | static const float kSamplePeriodScaleFactor = 2.5; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 37 | static const int kFramedropPercentThreshold = 60; |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 38 | static const int kMinFramesNeededToScale = 2 * 30; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 39 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 40 | } // namespace |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 41 | |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 42 | class QualityScaler::QpSmoother { |
| 43 | public: |
| 44 | explicit QpSmoother(float alpha) |
| 45 | : alpha_(alpha), last_sample_ms_(rtc::TimeMillis()), smoother_(alpha) {} |
| 46 | |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 47 | absl::optional<int> GetAvg() const { |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 48 | float value = smoother_.filtered(); |
| 49 | if (value == rtc::ExpFilter::kValueUndefined) { |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 50 | return absl::nullopt; |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 51 | } |
| 52 | return static_cast<int>(value); |
| 53 | } |
| 54 | |
| 55 | void Add(float sample) { |
| 56 | int64_t now_ms = rtc::TimeMillis(); |
| 57 | smoother_.Apply(static_cast<float>(now_ms - last_sample_ms_), sample); |
| 58 | last_sample_ms_ = now_ms; |
| 59 | } |
| 60 | |
| 61 | void Reset() { smoother_.Reset(alpha_); } |
| 62 | |
| 63 | private: |
| 64 | const float alpha_; |
| 65 | int64_t last_sample_ms_; |
| 66 | rtc::ExpFilter smoother_; |
| 67 | }; |
| 68 | |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 69 | class QualityScaler::CheckQpTask : public rtc::QueuedTask { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 70 | public: |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 71 | explicit CheckQpTask(QualityScaler* scaler) : scaler_(scaler) { |
| 72 | RTC_LOG(LS_INFO) << "Created CheckQpTask. Scheduling on queue..."; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 73 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 74 | std::unique_ptr<rtc::QueuedTask>(this), scaler_->GetSamplingPeriodMs()); |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 75 | } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 76 | void Stop() { |
| 77 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 78 | RTC_LOG(LS_INFO) << "Stopping QP Check task."; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 79 | stop_ = true; |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | bool Run() override { |
| 84 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 85 | if (stop_) |
| 86 | return true; // TaskQueue will free this task. |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 87 | scaler_->CheckQp(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 88 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 89 | std::unique_ptr<rtc::QueuedTask>(this), scaler_->GetSamplingPeriodMs()); |
| 90 | return false; // Retain the task in order to reuse it. |
| 91 | } |
| 92 | |
| 93 | QualityScaler* const scaler_; |
| 94 | bool stop_ = false; |
| 95 | rtc::SequencedTaskChecker task_checker_; |
| 96 | }; |
| 97 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 98 | QualityScaler::QualityScaler(AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 99 | VideoEncoder::QpThresholds thresholds) |
| 100 | : QualityScaler(observer, thresholds, kMeasureMs) {} |
| 101 | |
| 102 | // Protected ctor, should not be called directly. |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 103 | QualityScaler::QualityScaler(AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 104 | VideoEncoder::QpThresholds thresholds, |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 105 | int64_t sampling_period_ms) |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 106 | : check_qp_task_(nullptr), |
| 107 | observer_(observer), |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 108 | thresholds_(thresholds), |
| 109 | sampling_period_ms_(sampling_period_ms), |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 110 | fast_rampup_(true), |
| 111 | // Arbitrarily choose size based on 30 fps for 5 seconds. |
| 112 | average_qp_(5 * 30), |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 113 | framedrop_percent_media_opt_(5 * 30), |
| 114 | framedrop_percent_all_(5 * 30), |
| 115 | experiment_enabled_(QualityScalingExperiment::Enabled()), |
| 116 | observed_enough_frames_(false) { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 117 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 118 | if (experiment_enabled_) { |
| 119 | config_ = QualityScalingExperiment::GetConfig(); |
| 120 | qp_smoother_high_.reset(new QpSmoother(config_.alpha_high)); |
| 121 | qp_smoother_low_.reset(new QpSmoother(config_.alpha_low)); |
| 122 | } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 123 | RTC_DCHECK(observer_ != nullptr); |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 124 | check_qp_task_ = new CheckQpTask(this); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 125 | RTC_LOG(LS_INFO) << "QP thresholds: low: " << thresholds_.low |
| 126 | << ", high: " << thresholds_.high; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 127 | } |
| 128 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 129 | QualityScaler::~QualityScaler() { |
| 130 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 131 | check_qp_task_->Stop(); |
| 132 | } |
| 133 | |
| 134 | int64_t QualityScaler::GetSamplingPeriodMs() const { |
| 135 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 136 | if (fast_rampup_) { |
| 137 | return sampling_period_ms_; |
| 138 | } |
| 139 | if (experiment_enabled_ && !observed_enough_frames_) { |
| 140 | // Use half the interval while waiting for enough frames. |
| 141 | return sampling_period_ms_ / 2; |
| 142 | } |
| 143 | return sampling_period_ms_ * kSamplePeriodScaleFactor; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 146 | void QualityScaler::ReportDroppedFrameByMediaOpt() { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 147 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 148 | framedrop_percent_media_opt_.AddSample(100); |
| 149 | framedrop_percent_all_.AddSample(100); |
| 150 | } |
| 151 | |
| 152 | void QualityScaler::ReportDroppedFrameByEncoder() { |
| 153 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 154 | framedrop_percent_all_.AddSample(100); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 157 | void QualityScaler::ReportQp(int qp) { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 158 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 159 | framedrop_percent_media_opt_.AddSample(0); |
| 160 | framedrop_percent_all_.AddSample(0); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 161 | average_qp_.AddSample(qp); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 162 | if (qp_smoother_high_) |
| 163 | qp_smoother_high_->Add(qp); |
| 164 | if (qp_smoother_low_) |
| 165 | qp_smoother_low_->Add(qp); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 168 | void QualityScaler::CheckQp() { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 169 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 170 | // Should be set through InitEncode -> Should be set by now. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 171 | RTC_DCHECK_GE(thresholds_.low, 0); |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 172 | |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 173 | // If we have not observed at least this many frames we can't make a good |
| 174 | // scaling decision. |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 175 | const size_t frames = config_.use_all_drop_reasons |
| 176 | ? framedrop_percent_all_.size() |
| 177 | : framedrop_percent_media_opt_.size(); |
| 178 | if (frames < kMinFramesNeededToScale) { |
| 179 | observed_enough_frames_ = false; |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 180 | return; |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 181 | } |
| 182 | observed_enough_frames_ = true; |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 183 | |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 184 | // Check if we should scale down due to high frame drop. |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 185 | const absl::optional<int> drop_rate = |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 186 | config_.use_all_drop_reasons ? framedrop_percent_all_.GetAverage() |
| 187 | : framedrop_percent_media_opt_.GetAverage(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 188 | if (drop_rate && *drop_rate >= kFramedropPercentThreshold) { |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 189 | RTC_LOG(LS_INFO) << "Reporting high QP, framedrop percent " << *drop_rate; |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 190 | ReportQpHigh(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | |
| 194 | // Check if we should scale up or down based on QP. |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 195 | const absl::optional<int> avg_qp_high = qp_smoother_high_ |
| 196 | ? qp_smoother_high_->GetAvg() |
| 197 | : average_qp_.GetAverage(); |
| 198 | const absl::optional<int> avg_qp_low = |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 199 | qp_smoother_low_ ? qp_smoother_low_->GetAvg() : average_qp_.GetAverage(); |
| 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 |