blob: d31b2cd9c7f043443da7685536140c22e0789927 [file] [log] [blame]
pbos@webrtc.orga0d78272014-09-12 11:51:47 +00001/*
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öller718a7632016-06-13 13:06:01 +020010
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/utility/quality_scaler.h"
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000012
kthelgason876222f2016-11-29 01:44:11 -080013#include <memory>
Sebastian Janssonecb68972019-01-18 10:30:54 +010014#include <utility>
Kári Tristan Helgason5a20ed32016-09-15 10:56:19 +020015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
Åsa Persson517678c2019-05-06 14:17:35 +020017#include "rtc_base/experiments/quality_scaler_settings.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/logging.h"
Åsa Perssona945aee2018-04-24 16:53:25 +020019#include "rtc_base/numerics/exp_filter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/task_queue.h"
kthelgason478681e2016-09-28 08:17:43 -070021
Kári Tristan Helgason5a20ed32016-09-15 10:56:19 +020022// 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
kthelgason194f40a2016-09-14 02:14:58 -070027
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000028namespace webrtc {
29
Peter Boström926dfcd2016-04-14 14:48:10 +020030namespace {
Niels Möller225c7872018-02-22 15:03:53 +010031// TODO(nisse): Delete, delegate to encoders.
pboscbac40d2016-04-13 02:51:02 -070032// Threshold constant used until first downscale (to permit fast rampup).
kthelgason876222f2016-11-29 01:44:11 -080033static const int kMeasureMs = 2000;
34static const float kSamplePeriodScaleFactor = 2.5;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000035static const int kFramedropPercentThreshold = 60;
Åsa Persson517678c2019-05-06 14:17:35 +020036static const size_t kMinFramesNeededToScale = 2 * 30;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000037
kthelgason876222f2016-11-29 01:44:11 -080038} // namespace
kthelgason478681e2016-09-28 08:17:43 -070039
Åsa Perssona945aee2018-04-24 16:53:25 +020040class QualityScaler::QpSmoother {
41 public:
42 explicit QpSmoother(float alpha)
Sebastian Janssonb6789402019-03-01 15:40:49 +010043 : 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 Perssona945aee2018-04-24 16:53:25 +020048
Danil Chapovalov0040b662018-06-18 10:48:16 +020049 absl::optional<int> GetAvg() const {
Åsa Perssona945aee2018-04-24 16:53:25 +020050 float value = smoother_.filtered();
51 if (value == rtc::ExpFilter::kValueUndefined) {
Danil Chapovalov0040b662018-06-18 10:48:16 +020052 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020053 }
54 return static_cast<int>(value);
55 }
56
Sebastian Janssonb6789402019-03-01 15:40:49 +010057 void Add(float sample, int64_t time_sent_us) {
58 int64_t now_ms = time_sent_us / 1000;
Åsa Perssona945aee2018-04-24 16:53:25 +020059 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 Janssoncda86dd2019-03-11 17:26:36 +010071QualityScaler::QualityScaler(rtc::TaskQueue* task_queue,
72 AdaptationObserverInterface* observer,
kthelgason876222f2016-11-29 01:44:11 -080073 VideoEncoder::QpThresholds thresholds)
Sebastian Janssoncda86dd2019-03-11 17:26:36 +010074 : QualityScaler(task_queue, observer, thresholds, kMeasureMs) {}
kthelgason876222f2016-11-29 01:44:11 -080075
76// Protected ctor, should not be called directly.
Sebastian Janssoncda86dd2019-03-11 17:26:36 +010077QualityScaler::QualityScaler(rtc::TaskQueue* task_queue,
78 AdaptationObserverInterface* observer,
kthelgason876222f2016-11-29 01:44:11 -080079 VideoEncoder::QpThresholds thresholds,
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020080 int64_t sampling_period_ms)
Sebastian Janssonecb68972019-01-18 10:30:54 +010081 : observer_(observer),
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020082 thresholds_(thresholds),
83 sampling_period_ms_(sampling_period_ms),
kthelgason876222f2016-11-29 01:44:11 -080084 fast_rampup_(true),
85 // Arbitrarily choose size based on 30 fps for 5 seconds.
86 average_qp_(5 * 30),
Åsa Perssona945aee2018-04-24 16:53:25 +020087 framedrop_percent_media_opt_(5 * 30),
88 framedrop_percent_all_(5 * 30),
89 experiment_enabled_(QualityScalingExperiment::Enabled()),
Åsa Persson517678c2019-05-06 14:17:35 +020090 observed_enough_frames_(false),
91 min_frames_needed_(
92 QualityScalerSettings::ParseFromFieldTrials().MinFrames().value_or(
93 kMinFramesNeededToScale)),
94 initial_scale_factor_(QualityScalerSettings::ParseFromFieldTrials()
95 .InitialScaleFactor()
96 .value_or(kSamplePeriodScaleFactor)),
97 scale_factor_(
98 QualityScalerSettings::ParseFromFieldTrials().ScaleFactor()),
Åsa Perssonf5e5d252019-08-16 17:24:59 +020099 adapt_called_(false),
100 adapt_failed_(false) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200101 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200102 if (experiment_enabled_) {
103 config_ = QualityScalingExperiment::GetConfig();
104 qp_smoother_high_.reset(new QpSmoother(config_.alpha_high));
105 qp_smoother_low_.reset(new QpSmoother(config_.alpha_low));
106 }
kthelgason876222f2016-11-29 01:44:11 -0800107 RTC_DCHECK(observer_ != nullptr);
Sebastian Janssonecb68972019-01-18 10:30:54 +0100108 check_qp_task_ = RepeatingTaskHandle::DelayedStart(
Sebastian Janssoncda86dd2019-03-11 17:26:36 +0100109 task_queue->Get(), TimeDelta::ms(GetSamplingPeriodMs()), [this]() {
Sebastian Janssonecb68972019-01-18 10:30:54 +0100110 CheckQp();
111 return TimeDelta::ms(GetSamplingPeriodMs());
112 });
Mirko Bonadei675513b2017-11-09 11:09:25 +0100113 RTC_LOG(LS_INFO) << "QP thresholds: low: " << thresholds_.low
114 << ", high: " << thresholds_.high;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000115}
116
kthelgason876222f2016-11-29 01:44:11 -0800117QualityScaler::~QualityScaler() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200118 RTC_DCHECK_RUN_ON(&task_checker_);
Sebastian Janssonecb68972019-01-18 10:30:54 +0100119 check_qp_task_.Stop();
kthelgason876222f2016-11-29 01:44:11 -0800120}
121
122int64_t QualityScaler::GetSamplingPeriodMs() const {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200123 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200124 if (fast_rampup_) {
125 return sampling_period_ms_;
126 }
127 if (experiment_enabled_ && !observed_enough_frames_) {
128 // Use half the interval while waiting for enough frames.
129 return sampling_period_ms_ / 2;
130 }
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200131 if (adapt_failed_) {
132 // Check shortly again.
133 return sampling_period_ms_ / 8;
134 }
135 if (scale_factor_ && !adapt_called_) {
136 // Last CheckQp did not call AdaptDown/Up, possibly reduce interval.
Åsa Persson517678c2019-05-06 14:17:35 +0200137 return sampling_period_ms_ * scale_factor_.value();
138 }
139 return sampling_period_ms_ * initial_scale_factor_;
kthelgason876222f2016-11-29 01:44:11 -0800140}
141
Åsa Persson12314192019-06-20 15:45:07 +0200142void QualityScaler::SetQpThresholds(VideoEncoder::QpThresholds thresholds) {
143 RTC_DCHECK_RUN_ON(&task_checker_);
144 thresholds_ = thresholds;
145}
146
Åsa Perssona945aee2018-04-24 16:53:25 +0200147void QualityScaler::ReportDroppedFrameByMediaOpt() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200148 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200149 framedrop_percent_media_opt_.AddSample(100);
150 framedrop_percent_all_.AddSample(100);
151}
152
153void QualityScaler::ReportDroppedFrameByEncoder() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200154 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200155 framedrop_percent_all_.AddSample(100);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000156}
157
Sebastian Janssonb6789402019-03-01 15:40:49 +0100158void QualityScaler::ReportQp(int qp, int64_t time_sent_us) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200159 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200160 framedrop_percent_media_opt_.AddSample(0);
161 framedrop_percent_all_.AddSample(0);
kthelgason194f40a2016-09-14 02:14:58 -0700162 average_qp_.AddSample(qp);
Åsa Perssona945aee2018-04-24 16:53:25 +0200163 if (qp_smoother_high_)
Sebastian Janssonb6789402019-03-01 15:40:49 +0100164 qp_smoother_high_->Add(qp, time_sent_us);
Åsa Perssona945aee2018-04-24 16:53:25 +0200165 if (qp_smoother_low_)
Sebastian Janssonb6789402019-03-01 15:40:49 +0100166 qp_smoother_low_->Add(qp, time_sent_us);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000167}
168
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200169void QualityScaler::CheckQp() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200170 RTC_DCHECK_RUN_ON(&task_checker_);
jackychen61b4d512015-04-21 15:30:11 -0700171 // Should be set through InitEncode -> Should be set by now.
kthelgason876222f2016-11-29 01:44:11 -0800172 RTC_DCHECK_GE(thresholds_.low, 0);
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200173 adapt_failed_ = false;
174 adapt_called_ = false;
kthelgason55a01352017-04-04 02:31:42 -0700175
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200176 // If we have not observed at least this many frames we can't make a good
177 // scaling decision.
Åsa Perssona945aee2018-04-24 16:53:25 +0200178 const size_t frames = config_.use_all_drop_reasons
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100179 ? framedrop_percent_all_.Size()
180 : framedrop_percent_media_opt_.Size();
Åsa Persson517678c2019-05-06 14:17:35 +0200181 if (frames < min_frames_needed_) {
Åsa Perssona945aee2018-04-24 16:53:25 +0200182 observed_enough_frames_ = false;
kthelgason55a01352017-04-04 02:31:42 -0700183 return;
Åsa Perssona945aee2018-04-24 16:53:25 +0200184 }
185 observed_enough_frames_ = true;
kthelgason55a01352017-04-04 02:31:42 -0700186
kthelgason194f40a2016-09-14 02:14:58 -0700187 // Check if we should scale down due to high frame drop.
Danil Chapovalov0040b662018-06-18 10:48:16 +0200188 const absl::optional<int> drop_rate =
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100189 config_.use_all_drop_reasons
190 ? framedrop_percent_all_.GetAverageRoundedDown()
191 : framedrop_percent_media_opt_.GetAverageRoundedDown();
kthelgason194f40a2016-09-14 02:14:58 -0700192 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) {
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200193 RTC_LOG(LS_INFO) << "Reporting high QP, framedrop percent " << *drop_rate;
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200194 ReportQpHigh();
kthelgason194f40a2016-09-14 02:14:58 -0700195 return;
196 }
197
198 // Check if we should scale up or down based on QP.
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100199 const absl::optional<int> avg_qp_high =
200 qp_smoother_high_ ? qp_smoother_high_->GetAvg()
201 : average_qp_.GetAverageRoundedDown();
Danil Chapovalov0040b662018-06-18 10:48:16 +0200202 const absl::optional<int> avg_qp_low =
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100203 qp_smoother_low_ ? qp_smoother_low_->GetAvg()
204 : average_qp_.GetAverageRoundedDown();
Åsa Perssona945aee2018-04-24 16:53:25 +0200205 if (avg_qp_high && avg_qp_low) {
206 RTC_LOG(LS_INFO) << "Checking average QP " << *avg_qp_high << " ("
207 << *avg_qp_low << ").";
208 if (*avg_qp_high > thresholds_.high) {
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200209 ReportQpHigh();
glaznevd1c44352017-03-23 14:40:08 -0700210 return;
211 }
Åsa Perssona945aee2018-04-24 16:53:25 +0200212 if (*avg_qp_low <= thresholds_.low) {
glaznevd1c44352017-03-23 14:40:08 -0700213 // QP has been low. We want to try a higher resolution.
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200214 ReportQpLow();
glaznevd1c44352017-03-23 14:40:08 -0700215 return;
216 }
kthelgason194f40a2016-09-14 02:14:58 -0700217 }
218}
219
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200220void QualityScaler::ReportQpLow() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200221 RTC_DCHECK_RUN_ON(&task_checker_);
kthelgason194f40a2016-09-14 02:14:58 -0700222 ClearSamples();
sprangb1ca0732017-02-01 08:38:12 -0800223 observer_->AdaptUp(AdaptationObserverInterface::AdaptReason::kQuality);
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200224 adapt_called_ = true;
kthelgason194f40a2016-09-14 02:14:58 -0700225}
226
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200227void QualityScaler::ReportQpHigh() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200228 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200229
230 if (observer_->AdaptDown(
231 AdaptationObserverInterface::AdaptReason::kQuality)) {
232 ClearSamples();
233 } else {
234 adapt_failed_ = true;
235 }
236
kthelgason194f40a2016-09-14 02:14:58 -0700237 // If we've scaled down, wait longer before scaling up again.
238 if (fast_rampup_) {
239 fast_rampup_ = false;
kthelgason194f40a2016-09-14 02:14:58 -0700240 }
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200241 adapt_called_ = true;
jackychen6e2ce6e2015-07-13 16:26:33 -0700242}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000243
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000244void QualityScaler::ClearSamples() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200245 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200246 framedrop_percent_media_opt_.Reset();
247 framedrop_percent_all_.Reset();
kthelgason194f40a2016-09-14 02:14:58 -0700248 average_qp_.Reset();
Åsa Perssona945aee2018-04-24 16:53:25 +0200249 if (qp_smoother_high_)
250 qp_smoother_high_->Reset();
251 if (qp_smoother_low_)
252 qp_smoother_low_->Reset();
pboscbac40d2016-04-13 02:51:02 -0700253}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000254} // namespace webrtc