blob: 1f1f1929647f440f415822007467d858215207b0 [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 Perssona945aee2018-04-24 16:53:25 +0200137void QualityScaler::ReportDroppedFrameByMediaOpt() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200138 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200139 framedrop_percent_media_opt_.AddSample(100);
140 framedrop_percent_all_.AddSample(100);
141}
142
143void QualityScaler::ReportDroppedFrameByEncoder() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200144 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200145 framedrop_percent_all_.AddSample(100);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000146}
147
Sebastian Janssonb6789402019-03-01 15:40:49 +0100148void QualityScaler::ReportQp(int qp, int64_t time_sent_us) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200149 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200150 framedrop_percent_media_opt_.AddSample(0);
151 framedrop_percent_all_.AddSample(0);
kthelgason194f40a2016-09-14 02:14:58 -0700152 average_qp_.AddSample(qp);
Åsa Perssona945aee2018-04-24 16:53:25 +0200153 if (qp_smoother_high_)
Sebastian Janssonb6789402019-03-01 15:40:49 +0100154 qp_smoother_high_->Add(qp, time_sent_us);
Åsa Perssona945aee2018-04-24 16:53:25 +0200155 if (qp_smoother_low_)
Sebastian Janssonb6789402019-03-01 15:40:49 +0100156 qp_smoother_low_->Add(qp, time_sent_us);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000157}
158
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200159void QualityScaler::CheckQp() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200160 RTC_DCHECK_RUN_ON(&task_checker_);
jackychen61b4d512015-04-21 15:30:11 -0700161 // Should be set through InitEncode -> Should be set by now.
kthelgason876222f2016-11-29 01:44:11 -0800162 RTC_DCHECK_GE(thresholds_.low, 0);
Åsa Persson517678c2019-05-06 14:17:35 +0200163 last_adapted_ = false;
kthelgason55a01352017-04-04 02:31:42 -0700164
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200165 // If we have not observed at least this many frames we can't make a good
166 // scaling decision.
Åsa Perssona945aee2018-04-24 16:53:25 +0200167 const size_t frames = config_.use_all_drop_reasons
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100168 ? framedrop_percent_all_.Size()
169 : framedrop_percent_media_opt_.Size();
Åsa Persson517678c2019-05-06 14:17:35 +0200170 if (frames < min_frames_needed_) {
Åsa Perssona945aee2018-04-24 16:53:25 +0200171 observed_enough_frames_ = false;
kthelgason55a01352017-04-04 02:31:42 -0700172 return;
Åsa Perssona945aee2018-04-24 16:53:25 +0200173 }
174 observed_enough_frames_ = true;
kthelgason55a01352017-04-04 02:31:42 -0700175
kthelgason194f40a2016-09-14 02:14:58 -0700176 // Check if we should scale down due to high frame drop.
Danil Chapovalov0040b662018-06-18 10:48:16 +0200177 const absl::optional<int> drop_rate =
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100178 config_.use_all_drop_reasons
179 ? framedrop_percent_all_.GetAverageRoundedDown()
180 : framedrop_percent_media_opt_.GetAverageRoundedDown();
kthelgason194f40a2016-09-14 02:14:58 -0700181 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) {
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200182 RTC_LOG(LS_INFO) << "Reporting high QP, framedrop percent " << *drop_rate;
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200183 ReportQpHigh();
kthelgason194f40a2016-09-14 02:14:58 -0700184 return;
185 }
186
187 // Check if we should scale up or down based on QP.
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100188 const absl::optional<int> avg_qp_high =
189 qp_smoother_high_ ? qp_smoother_high_->GetAvg()
190 : average_qp_.GetAverageRoundedDown();
Danil Chapovalov0040b662018-06-18 10:48:16 +0200191 const absl::optional<int> avg_qp_low =
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100192 qp_smoother_low_ ? qp_smoother_low_->GetAvg()
193 : average_qp_.GetAverageRoundedDown();
Åsa Perssona945aee2018-04-24 16:53:25 +0200194 if (avg_qp_high && avg_qp_low) {
195 RTC_LOG(LS_INFO) << "Checking average QP " << *avg_qp_high << " ("
196 << *avg_qp_low << ").";
197 if (*avg_qp_high > thresholds_.high) {
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200198 ReportQpHigh();
glaznevd1c44352017-03-23 14:40:08 -0700199 return;
200 }
Åsa Perssona945aee2018-04-24 16:53:25 +0200201 if (*avg_qp_low <= thresholds_.low) {
glaznevd1c44352017-03-23 14:40:08 -0700202 // QP has been low. We want to try a higher resolution.
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200203 ReportQpLow();
glaznevd1c44352017-03-23 14:40:08 -0700204 return;
205 }
kthelgason194f40a2016-09-14 02:14:58 -0700206 }
207}
208
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200209void QualityScaler::ReportQpLow() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200210 RTC_DCHECK_RUN_ON(&task_checker_);
kthelgason194f40a2016-09-14 02:14:58 -0700211 ClearSamples();
sprangb1ca0732017-02-01 08:38:12 -0800212 observer_->AdaptUp(AdaptationObserverInterface::AdaptReason::kQuality);
Åsa Persson517678c2019-05-06 14:17:35 +0200213 last_adapted_ = true;
kthelgason194f40a2016-09-14 02:14:58 -0700214}
215
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200216void QualityScaler::ReportQpHigh() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200217 RTC_DCHECK_RUN_ON(&task_checker_);
kthelgason194f40a2016-09-14 02:14:58 -0700218 ClearSamples();
sprangb1ca0732017-02-01 08:38:12 -0800219 observer_->AdaptDown(AdaptationObserverInterface::AdaptReason::kQuality);
kthelgason194f40a2016-09-14 02:14:58 -0700220 // If we've scaled down, wait longer before scaling up again.
221 if (fast_rampup_) {
222 fast_rampup_ = false;
kthelgason194f40a2016-09-14 02:14:58 -0700223 }
Åsa Persson517678c2019-05-06 14:17:35 +0200224 last_adapted_ = true;
jackychen6e2ce6e2015-07-13 16:26:33 -0700225}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000226
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000227void QualityScaler::ClearSamples() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200228 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200229 framedrop_percent_media_opt_.Reset();
230 framedrop_percent_all_.Reset();
kthelgason194f40a2016-09-14 02:14:58 -0700231 average_qp_.Reset();
Åsa Perssona945aee2018-04-24 16:53:25 +0200232 if (qp_smoother_high_)
233 qp_smoother_high_->Reset();
234 if (qp_smoother_low_)
235 qp_smoother_low_->Reset();
pboscbac40d2016-04-13 02:51:02 -0700236}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000237} // namespace webrtc