blob: 795f85ea053901a795778362332f31760932bc6a [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
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
18#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;
kthelgason55a01352017-04-04 02:31:42 -070036static const int 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
kthelgason876222f2016-11-29 01:44:11 -080071
sprangb1ca0732017-02-01 08:38:12 -080072QualityScaler::QualityScaler(AdaptationObserverInterface* observer,
kthelgason876222f2016-11-29 01:44:11 -080073 VideoEncoder::QpThresholds thresholds)
74 : QualityScaler(observer, thresholds, kMeasureMs) {}
75
76// Protected ctor, should not be called directly.
sprangb1ca0732017-02-01 08:38:12 -080077QualityScaler::QualityScaler(AdaptationObserverInterface* observer,
kthelgason876222f2016-11-29 01:44:11 -080078 VideoEncoder::QpThresholds thresholds,
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020079 int64_t sampling_period_ms)
Sebastian Janssonecb68972019-01-18 10:30:54 +010080 : observer_(observer),
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020081 thresholds_(thresholds),
82 sampling_period_ms_(sampling_period_ms),
kthelgason876222f2016-11-29 01:44:11 -080083 fast_rampup_(true),
84 // Arbitrarily choose size based on 30 fps for 5 seconds.
85 average_qp_(5 * 30),
Åsa Perssona945aee2018-04-24 16:53:25 +020086 framedrop_percent_media_opt_(5 * 30),
87 framedrop_percent_all_(5 * 30),
88 experiment_enabled_(QualityScalingExperiment::Enabled()),
89 observed_enough_frames_(false) {
kthelgason876222f2016-11-29 01:44:11 -080090 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +020091 if (experiment_enabled_) {
92 config_ = QualityScalingExperiment::GetConfig();
93 qp_smoother_high_.reset(new QpSmoother(config_.alpha_high));
94 qp_smoother_low_.reset(new QpSmoother(config_.alpha_low));
95 }
kthelgason876222f2016-11-29 01:44:11 -080096 RTC_DCHECK(observer_ != nullptr);
Sebastian Janssonecb68972019-01-18 10:30:54 +010097 check_qp_task_ = RepeatingTaskHandle::DelayedStart(
98 TimeDelta::ms(GetSamplingPeriodMs()), [this]() {
99 CheckQp();
100 return TimeDelta::ms(GetSamplingPeriodMs());
101 });
Mirko Bonadei675513b2017-11-09 11:09:25 +0100102 RTC_LOG(LS_INFO) << "QP thresholds: low: " << thresholds_.low
103 << ", high: " << thresholds_.high;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000104}
105
kthelgason876222f2016-11-29 01:44:11 -0800106QualityScaler::~QualityScaler() {
107 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
Sebastian Janssonecb68972019-01-18 10:30:54 +0100108 check_qp_task_.Stop();
kthelgason876222f2016-11-29 01:44:11 -0800109}
110
111int64_t QualityScaler::GetSamplingPeriodMs() const {
112 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200113 if (fast_rampup_) {
114 return sampling_period_ms_;
115 }
116 if (experiment_enabled_ && !observed_enough_frames_) {
117 // Use half the interval while waiting for enough frames.
118 return sampling_period_ms_ / 2;
119 }
120 return sampling_period_ms_ * kSamplePeriodScaleFactor;
kthelgason876222f2016-11-29 01:44:11 -0800121}
122
Åsa Perssona945aee2018-04-24 16:53:25 +0200123void QualityScaler::ReportDroppedFrameByMediaOpt() {
kthelgason876222f2016-11-29 01:44:11 -0800124 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200125 framedrop_percent_media_opt_.AddSample(100);
126 framedrop_percent_all_.AddSample(100);
127}
128
129void QualityScaler::ReportDroppedFrameByEncoder() {
130 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
131 framedrop_percent_all_.AddSample(100);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000132}
133
Sebastian Janssonb6789402019-03-01 15:40:49 +0100134void QualityScaler::ReportQp(int qp, int64_t time_sent_us) {
kthelgason876222f2016-11-29 01:44:11 -0800135 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200136 framedrop_percent_media_opt_.AddSample(0);
137 framedrop_percent_all_.AddSample(0);
kthelgason194f40a2016-09-14 02:14:58 -0700138 average_qp_.AddSample(qp);
Åsa Perssona945aee2018-04-24 16:53:25 +0200139 if (qp_smoother_high_)
Sebastian Janssonb6789402019-03-01 15:40:49 +0100140 qp_smoother_high_->Add(qp, time_sent_us);
Åsa Perssona945aee2018-04-24 16:53:25 +0200141 if (qp_smoother_low_)
Sebastian Janssonb6789402019-03-01 15:40:49 +0100142 qp_smoother_low_->Add(qp, time_sent_us);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000143}
144
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200145void QualityScaler::CheckQp() {
kthelgason876222f2016-11-29 01:44:11 -0800146 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
jackychen61b4d512015-04-21 15:30:11 -0700147 // Should be set through InitEncode -> Should be set by now.
kthelgason876222f2016-11-29 01:44:11 -0800148 RTC_DCHECK_GE(thresholds_.low, 0);
kthelgason55a01352017-04-04 02:31:42 -0700149
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200150 // If we have not observed at least this many frames we can't make a good
151 // scaling decision.
Åsa Perssona945aee2018-04-24 16:53:25 +0200152 const size_t frames = config_.use_all_drop_reasons
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100153 ? framedrop_percent_all_.Size()
154 : framedrop_percent_media_opt_.Size();
Åsa Perssona945aee2018-04-24 16:53:25 +0200155 if (frames < kMinFramesNeededToScale) {
156 observed_enough_frames_ = false;
kthelgason55a01352017-04-04 02:31:42 -0700157 return;
Åsa Perssona945aee2018-04-24 16:53:25 +0200158 }
159 observed_enough_frames_ = true;
kthelgason55a01352017-04-04 02:31:42 -0700160
kthelgason194f40a2016-09-14 02:14:58 -0700161 // Check if we should scale down due to high frame drop.
Danil Chapovalov0040b662018-06-18 10:48:16 +0200162 const absl::optional<int> drop_rate =
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100163 config_.use_all_drop_reasons
164 ? framedrop_percent_all_.GetAverageRoundedDown()
165 : framedrop_percent_media_opt_.GetAverageRoundedDown();
kthelgason194f40a2016-09-14 02:14:58 -0700166 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) {
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200167 RTC_LOG(LS_INFO) << "Reporting high QP, framedrop percent " << *drop_rate;
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200168 ReportQpHigh();
kthelgason194f40a2016-09-14 02:14:58 -0700169 return;
170 }
171
172 // Check if we should scale up or down based on QP.
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100173 const absl::optional<int> avg_qp_high =
174 qp_smoother_high_ ? qp_smoother_high_->GetAvg()
175 : average_qp_.GetAverageRoundedDown();
Danil Chapovalov0040b662018-06-18 10:48:16 +0200176 const absl::optional<int> avg_qp_low =
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100177 qp_smoother_low_ ? qp_smoother_low_->GetAvg()
178 : average_qp_.GetAverageRoundedDown();
Åsa Perssona945aee2018-04-24 16:53:25 +0200179 if (avg_qp_high && avg_qp_low) {
180 RTC_LOG(LS_INFO) << "Checking average QP " << *avg_qp_high << " ("
181 << *avg_qp_low << ").";
182 if (*avg_qp_high > thresholds_.high) {
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200183 ReportQpHigh();
glaznevd1c44352017-03-23 14:40:08 -0700184 return;
185 }
Åsa Perssona945aee2018-04-24 16:53:25 +0200186 if (*avg_qp_low <= thresholds_.low) {
glaznevd1c44352017-03-23 14:40:08 -0700187 // QP has been low. We want to try a higher resolution.
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200188 ReportQpLow();
glaznevd1c44352017-03-23 14:40:08 -0700189 return;
190 }
kthelgason194f40a2016-09-14 02:14:58 -0700191 }
192}
193
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200194void QualityScaler::ReportQpLow() {
kthelgason876222f2016-11-29 01:44:11 -0800195 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
kthelgason194f40a2016-09-14 02:14:58 -0700196 ClearSamples();
sprangb1ca0732017-02-01 08:38:12 -0800197 observer_->AdaptUp(AdaptationObserverInterface::AdaptReason::kQuality);
kthelgason194f40a2016-09-14 02:14:58 -0700198}
199
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200200void QualityScaler::ReportQpHigh() {
kthelgason876222f2016-11-29 01:44:11 -0800201 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
kthelgason194f40a2016-09-14 02:14:58 -0700202 ClearSamples();
sprangb1ca0732017-02-01 08:38:12 -0800203 observer_->AdaptDown(AdaptationObserverInterface::AdaptReason::kQuality);
kthelgason194f40a2016-09-14 02:14:58 -0700204 // If we've scaled down, wait longer before scaling up again.
205 if (fast_rampup_) {
206 fast_rampup_ = false;
kthelgason194f40a2016-09-14 02:14:58 -0700207 }
jackychen6e2ce6e2015-07-13 16:26:33 -0700208}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000209
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000210void QualityScaler::ClearSamples() {
kthelgason876222f2016-11-29 01:44:11 -0800211 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200212 framedrop_percent_media_opt_.Reset();
213 framedrop_percent_all_.Reset();
kthelgason194f40a2016-09-14 02:14:58 -0700214 average_qp_.Reset();
Åsa Perssona945aee2018-04-24 16:53:25 +0200215 if (qp_smoother_high_)
216 qp_smoother_high_->Reset();
217 if (qp_smoother_low_)
218 qp_smoother_low_->Reset();
pboscbac40d2016-04-13 02:51:02 -0700219}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000220} // namespace webrtc