blob: 42c40c5c581f0f3f3a947e4d4a331b3c2b138e4a [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()),
99 last_adapted_(false) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200100 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200101 if (experiment_enabled_) {
102 config_ = QualityScalingExperiment::GetConfig();
103 qp_smoother_high_.reset(new QpSmoother(config_.alpha_high));
104 qp_smoother_low_.reset(new QpSmoother(config_.alpha_low));
105 }
kthelgason876222f2016-11-29 01:44:11 -0800106 RTC_DCHECK(observer_ != nullptr);
Sebastian Janssonecb68972019-01-18 10:30:54 +0100107 check_qp_task_ = RepeatingTaskHandle::DelayedStart(
Sebastian Janssoncda86dd2019-03-11 17:26:36 +0100108 task_queue->Get(), TimeDelta::ms(GetSamplingPeriodMs()), [this]() {
Sebastian Janssonecb68972019-01-18 10:30:54 +0100109 CheckQp();
110 return TimeDelta::ms(GetSamplingPeriodMs());
111 });
Mirko Bonadei675513b2017-11-09 11:09:25 +0100112 RTC_LOG(LS_INFO) << "QP thresholds: low: " << thresholds_.low
113 << ", high: " << thresholds_.high;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000114}
115
kthelgason876222f2016-11-29 01:44:11 -0800116QualityScaler::~QualityScaler() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200117 RTC_DCHECK_RUN_ON(&task_checker_);
Sebastian Janssonecb68972019-01-18 10:30:54 +0100118 check_qp_task_.Stop();
kthelgason876222f2016-11-29 01:44:11 -0800119}
120
121int64_t QualityScaler::GetSamplingPeriodMs() const {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200122 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200123 if (fast_rampup_) {
124 return sampling_period_ms_;
125 }
126 if (experiment_enabled_ && !observed_enough_frames_) {
127 // Use half the interval while waiting for enough frames.
128 return sampling_period_ms_ / 2;
129 }
Åsa Persson517678c2019-05-06 14:17:35 +0200130 if (scale_factor_ && !last_adapted_) {
131 // Last check did not result in a AdaptDown/Up, possibly reduce interval.
132 return sampling_period_ms_ * scale_factor_.value();
133 }
134 return sampling_period_ms_ * initial_scale_factor_;
kthelgason876222f2016-11-29 01:44:11 -0800135}
136
Åsa Persson12314192019-06-20 15:45:07 +0200137void QualityScaler::SetQpThresholds(VideoEncoder::QpThresholds thresholds) {
138 RTC_DCHECK_RUN_ON(&task_checker_);
139 thresholds_ = thresholds;
140}
141
Åsa Perssona945aee2018-04-24 16:53:25 +0200142void QualityScaler::ReportDroppedFrameByMediaOpt() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200143 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200144 framedrop_percent_media_opt_.AddSample(100);
145 framedrop_percent_all_.AddSample(100);
146}
147
148void QualityScaler::ReportDroppedFrameByEncoder() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200149 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200150 framedrop_percent_all_.AddSample(100);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000151}
152
Sebastian Janssonb6789402019-03-01 15:40:49 +0100153void QualityScaler::ReportQp(int qp, int64_t time_sent_us) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200154 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200155 framedrop_percent_media_opt_.AddSample(0);
156 framedrop_percent_all_.AddSample(0);
kthelgason194f40a2016-09-14 02:14:58 -0700157 average_qp_.AddSample(qp);
Åsa Perssona945aee2018-04-24 16:53:25 +0200158 if (qp_smoother_high_)
Sebastian Janssonb6789402019-03-01 15:40:49 +0100159 qp_smoother_high_->Add(qp, time_sent_us);
Åsa Perssona945aee2018-04-24 16:53:25 +0200160 if (qp_smoother_low_)
Sebastian Janssonb6789402019-03-01 15:40:49 +0100161 qp_smoother_low_->Add(qp, time_sent_us);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000162}
163
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200164void QualityScaler::CheckQp() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200165 RTC_DCHECK_RUN_ON(&task_checker_);
jackychen61b4d512015-04-21 15:30:11 -0700166 // Should be set through InitEncode -> Should be set by now.
kthelgason876222f2016-11-29 01:44:11 -0800167 RTC_DCHECK_GE(thresholds_.low, 0);
Åsa Persson517678c2019-05-06 14:17:35 +0200168 last_adapted_ = false;
kthelgason55a01352017-04-04 02:31:42 -0700169
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200170 // If we have not observed at least this many frames we can't make a good
171 // scaling decision.
Åsa Perssona945aee2018-04-24 16:53:25 +0200172 const size_t frames = config_.use_all_drop_reasons
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100173 ? framedrop_percent_all_.Size()
174 : framedrop_percent_media_opt_.Size();
Åsa Persson517678c2019-05-06 14:17:35 +0200175 if (frames < min_frames_needed_) {
Åsa Perssona945aee2018-04-24 16:53:25 +0200176 observed_enough_frames_ = false;
kthelgason55a01352017-04-04 02:31:42 -0700177 return;
Åsa Perssona945aee2018-04-24 16:53:25 +0200178 }
179 observed_enough_frames_ = true;
kthelgason55a01352017-04-04 02:31:42 -0700180
kthelgason194f40a2016-09-14 02:14:58 -0700181 // Check if we should scale down due to high frame drop.
Danil Chapovalov0040b662018-06-18 10:48:16 +0200182 const absl::optional<int> drop_rate =
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100183 config_.use_all_drop_reasons
184 ? framedrop_percent_all_.GetAverageRoundedDown()
185 : framedrop_percent_media_opt_.GetAverageRoundedDown();
kthelgason194f40a2016-09-14 02:14:58 -0700186 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) {
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200187 RTC_LOG(LS_INFO) << "Reporting high QP, framedrop percent " << *drop_rate;
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200188 ReportQpHigh();
kthelgason194f40a2016-09-14 02:14:58 -0700189 return;
190 }
191
192 // Check if we should scale up or down based on QP.
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100193 const absl::optional<int> avg_qp_high =
194 qp_smoother_high_ ? qp_smoother_high_->GetAvg()
195 : average_qp_.GetAverageRoundedDown();
Danil Chapovalov0040b662018-06-18 10:48:16 +0200196 const absl::optional<int> avg_qp_low =
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100197 qp_smoother_low_ ? qp_smoother_low_->GetAvg()
198 : average_qp_.GetAverageRoundedDown();
Åsa Perssona945aee2018-04-24 16:53:25 +0200199 if (avg_qp_high && avg_qp_low) {
200 RTC_LOG(LS_INFO) << "Checking average QP " << *avg_qp_high << " ("
201 << *avg_qp_low << ").";
202 if (*avg_qp_high > thresholds_.high) {
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200203 ReportQpHigh();
glaznevd1c44352017-03-23 14:40:08 -0700204 return;
205 }
Åsa Perssona945aee2018-04-24 16:53:25 +0200206 if (*avg_qp_low <= thresholds_.low) {
glaznevd1c44352017-03-23 14:40:08 -0700207 // QP has been low. We want to try a higher resolution.
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200208 ReportQpLow();
glaznevd1c44352017-03-23 14:40:08 -0700209 return;
210 }
kthelgason194f40a2016-09-14 02:14:58 -0700211 }
212}
213
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200214void QualityScaler::ReportQpLow() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200215 RTC_DCHECK_RUN_ON(&task_checker_);
kthelgason194f40a2016-09-14 02:14:58 -0700216 ClearSamples();
sprangb1ca0732017-02-01 08:38:12 -0800217 observer_->AdaptUp(AdaptationObserverInterface::AdaptReason::kQuality);
Åsa Persson517678c2019-05-06 14:17:35 +0200218 last_adapted_ = true;
kthelgason194f40a2016-09-14 02:14:58 -0700219}
220
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200221void QualityScaler::ReportQpHigh() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200222 RTC_DCHECK_RUN_ON(&task_checker_);
kthelgason194f40a2016-09-14 02:14:58 -0700223 ClearSamples();
sprangb1ca0732017-02-01 08:38:12 -0800224 observer_->AdaptDown(AdaptationObserverInterface::AdaptReason::kQuality);
kthelgason194f40a2016-09-14 02:14:58 -0700225 // If we've scaled down, wait longer before scaling up again.
226 if (fast_rampup_) {
227 fast_rampup_ = false;
kthelgason194f40a2016-09-14 02:14:58 -0700228 }
Åsa Persson517678c2019-05-06 14:17:35 +0200229 last_adapted_ = true;
jackychen6e2ce6e2015-07-13 16:26:33 -0700230}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000231
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000232void QualityScaler::ClearSamples() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200233 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200234 framedrop_percent_media_opt_.Reset();
235 framedrop_percent_all_.Reset();
kthelgason194f40a2016-09-14 02:14:58 -0700236 average_qp_.Reset();
Åsa Perssona945aee2018-04-24 16:53:25 +0200237 if (qp_smoother_high_)
238 qp_smoother_high_->Reset();
239 if (qp_smoother_low_)
240 qp_smoother_low_->Reset();
pboscbac40d2016-04-13 02:51:02 -0700241}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000242} // namespace webrtc