blob: 4a517dcfdf78caac42e73656076fa34c18fb81fa [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>
Kári Tristan Helgason5a20ed32016-09-15 10:56:19 +020014
Yves Gerey3e707812018-11-28 16:47:49 +010015#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
17#include "rtc_base/logging.h"
Åsa Perssona945aee2018-04-24 16:53:25 +020018#include "rtc_base/numerics/exp_filter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/task_queue.h"
Åsa Perssona945aee2018-04-24 16:53:25 +020020#include "rtc_base/timeutils.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)
43 : alpha_(alpha), last_sample_ms_(rtc::TimeMillis()), smoother_(alpha) {}
44
Danil Chapovalov0040b662018-06-18 10:48:16 +020045 absl::optional<int> GetAvg() const {
Åsa Perssona945aee2018-04-24 16:53:25 +020046 float value = smoother_.filtered();
47 if (value == rtc::ExpFilter::kValueUndefined) {
Danil Chapovalov0040b662018-06-18 10:48:16 +020048 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020049 }
50 return static_cast<int>(value);
51 }
52
53 void Add(float sample) {
54 int64_t now_ms = rtc::TimeMillis();
55 smoother_.Apply(static_cast<float>(now_ms - last_sample_ms_), sample);
56 last_sample_ms_ = now_ms;
57 }
58
59 void Reset() { smoother_.Reset(alpha_); }
60
61 private:
62 const float alpha_;
63 int64_t last_sample_ms_;
64 rtc::ExpFilter smoother_;
65};
66
Åsa Persson04d5f1d2018-04-20 15:19:11 +020067class QualityScaler::CheckQpTask : public rtc::QueuedTask {
kthelgason876222f2016-11-29 01:44:11 -080068 public:
Åsa Persson04d5f1d2018-04-20 15:19:11 +020069 explicit CheckQpTask(QualityScaler* scaler) : scaler_(scaler) {
70 RTC_LOG(LS_INFO) << "Created CheckQpTask. Scheduling on queue...";
kthelgason876222f2016-11-29 01:44:11 -080071 rtc::TaskQueue::Current()->PostDelayedTask(
72 std::unique_ptr<rtc::QueuedTask>(this), scaler_->GetSamplingPeriodMs());
Alex Glazneva9d08922016-02-19 15:24:06 -080073 }
kthelgason876222f2016-11-29 01:44:11 -080074 void Stop() {
75 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
Mirko Bonadei675513b2017-11-09 11:09:25 +010076 RTC_LOG(LS_INFO) << "Stopping QP Check task.";
kthelgason876222f2016-11-29 01:44:11 -080077 stop_ = true;
78 }
79
80 private:
81 bool Run() override {
82 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
83 if (stop_)
84 return true; // TaskQueue will free this task.
Åsa Persson04d5f1d2018-04-20 15:19:11 +020085 scaler_->CheckQp();
kthelgason876222f2016-11-29 01:44:11 -080086 rtc::TaskQueue::Current()->PostDelayedTask(
87 std::unique_ptr<rtc::QueuedTask>(this), scaler_->GetSamplingPeriodMs());
88 return false; // Retain the task in order to reuse it.
89 }
90
91 QualityScaler* const scaler_;
92 bool stop_ = false;
93 rtc::SequencedTaskChecker task_checker_;
94};
95
sprangb1ca0732017-02-01 08:38:12 -080096QualityScaler::QualityScaler(AdaptationObserverInterface* observer,
kthelgason876222f2016-11-29 01:44:11 -080097 VideoEncoder::QpThresholds thresholds)
98 : QualityScaler(observer, thresholds, kMeasureMs) {}
99
100// Protected ctor, should not be called directly.
sprangb1ca0732017-02-01 08:38:12 -0800101QualityScaler::QualityScaler(AdaptationObserverInterface* observer,
kthelgason876222f2016-11-29 01:44:11 -0800102 VideoEncoder::QpThresholds thresholds,
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200103 int64_t sampling_period_ms)
kthelgason876222f2016-11-29 01:44:11 -0800104 : check_qp_task_(nullptr),
105 observer_(observer),
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200106 thresholds_(thresholds),
107 sampling_period_ms_(sampling_period_ms),
kthelgason876222f2016-11-29 01:44:11 -0800108 fast_rampup_(true),
109 // Arbitrarily choose size based on 30 fps for 5 seconds.
110 average_qp_(5 * 30),
Åsa Perssona945aee2018-04-24 16:53:25 +0200111 framedrop_percent_media_opt_(5 * 30),
112 framedrop_percent_all_(5 * 30),
113 experiment_enabled_(QualityScalingExperiment::Enabled()),
114 observed_enough_frames_(false) {
kthelgason876222f2016-11-29 01:44:11 -0800115 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200116 if (experiment_enabled_) {
117 config_ = QualityScalingExperiment::GetConfig();
118 qp_smoother_high_.reset(new QpSmoother(config_.alpha_high));
119 qp_smoother_low_.reset(new QpSmoother(config_.alpha_low));
120 }
kthelgason876222f2016-11-29 01:44:11 -0800121 RTC_DCHECK(observer_ != nullptr);
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200122 check_qp_task_ = new CheckQpTask(this);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100123 RTC_LOG(LS_INFO) << "QP thresholds: low: " << thresholds_.low
124 << ", high: " << thresholds_.high;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000125}
126
kthelgason876222f2016-11-29 01:44:11 -0800127QualityScaler::~QualityScaler() {
128 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
129 check_qp_task_->Stop();
130}
131
132int64_t QualityScaler::GetSamplingPeriodMs() const {
133 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200134 if (fast_rampup_) {
135 return sampling_period_ms_;
136 }
137 if (experiment_enabled_ && !observed_enough_frames_) {
138 // Use half the interval while waiting for enough frames.
139 return sampling_period_ms_ / 2;
140 }
141 return sampling_period_ms_ * kSamplePeriodScaleFactor;
kthelgason876222f2016-11-29 01:44:11 -0800142}
143
Åsa Perssona945aee2018-04-24 16:53:25 +0200144void QualityScaler::ReportDroppedFrameByMediaOpt() {
kthelgason876222f2016-11-29 01:44:11 -0800145 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200146 framedrop_percent_media_opt_.AddSample(100);
147 framedrop_percent_all_.AddSample(100);
148}
149
150void QualityScaler::ReportDroppedFrameByEncoder() {
151 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
152 framedrop_percent_all_.AddSample(100);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000153}
154
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200155void QualityScaler::ReportQp(int qp) {
kthelgason876222f2016-11-29 01:44:11 -0800156 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200157 framedrop_percent_media_opt_.AddSample(0);
158 framedrop_percent_all_.AddSample(0);
kthelgason194f40a2016-09-14 02:14:58 -0700159 average_qp_.AddSample(qp);
Åsa Perssona945aee2018-04-24 16:53:25 +0200160 if (qp_smoother_high_)
161 qp_smoother_high_->Add(qp);
162 if (qp_smoother_low_)
163 qp_smoother_low_->Add(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000164}
165
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200166void QualityScaler::CheckQp() {
kthelgason876222f2016-11-29 01:44:11 -0800167 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
jackychen61b4d512015-04-21 15:30:11 -0700168 // Should be set through InitEncode -> Should be set by now.
kthelgason876222f2016-11-29 01:44:11 -0800169 RTC_DCHECK_GE(thresholds_.low, 0);
kthelgason55a01352017-04-04 02:31:42 -0700170
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200171 // If we have not observed at least this many frames we can't make a good
172 // scaling decision.
Åsa Perssona945aee2018-04-24 16:53:25 +0200173 const size_t frames = config_.use_all_drop_reasons
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100174 ? framedrop_percent_all_.Size()
175 : framedrop_percent_media_opt_.Size();
Åsa Perssona945aee2018-04-24 16:53:25 +0200176 if (frames < kMinFramesNeededToScale) {
177 observed_enough_frames_ = false;
kthelgason55a01352017-04-04 02:31:42 -0700178 return;
Åsa Perssona945aee2018-04-24 16:53:25 +0200179 }
180 observed_enough_frames_ = true;
kthelgason55a01352017-04-04 02:31:42 -0700181
kthelgason194f40a2016-09-14 02:14:58 -0700182 // Check if we should scale down due to high frame drop.
Danil Chapovalov0040b662018-06-18 10:48:16 +0200183 const absl::optional<int> drop_rate =
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100184 config_.use_all_drop_reasons
185 ? framedrop_percent_all_.GetAverageRoundedDown()
186 : framedrop_percent_media_opt_.GetAverageRoundedDown();
kthelgason194f40a2016-09-14 02:14:58 -0700187 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) {
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200188 RTC_LOG(LS_INFO) << "Reporting high QP, framedrop percent " << *drop_rate;
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200189 ReportQpHigh();
kthelgason194f40a2016-09-14 02:14:58 -0700190 return;
191 }
192
193 // Check if we should scale up or down based on QP.
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100194 const absl::optional<int> avg_qp_high =
195 qp_smoother_high_ ? qp_smoother_high_->GetAvg()
196 : average_qp_.GetAverageRoundedDown();
Danil Chapovalov0040b662018-06-18 10:48:16 +0200197 const absl::optional<int> avg_qp_low =
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100198 qp_smoother_low_ ? qp_smoother_low_->GetAvg()
199 : average_qp_.GetAverageRoundedDown();
Åsa Perssona945aee2018-04-24 16:53:25 +0200200 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 Persson04d5f1d2018-04-20 15:19:11 +0200204 ReportQpHigh();
glaznevd1c44352017-03-23 14:40:08 -0700205 return;
206 }
Åsa Perssona945aee2018-04-24 16:53:25 +0200207 if (*avg_qp_low <= thresholds_.low) {
glaznevd1c44352017-03-23 14:40:08 -0700208 // QP has been low. We want to try a higher resolution.
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200209 ReportQpLow();
glaznevd1c44352017-03-23 14:40:08 -0700210 return;
211 }
kthelgason194f40a2016-09-14 02:14:58 -0700212 }
213}
214
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200215void QualityScaler::ReportQpLow() {
kthelgason876222f2016-11-29 01:44:11 -0800216 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
kthelgason194f40a2016-09-14 02:14:58 -0700217 ClearSamples();
sprangb1ca0732017-02-01 08:38:12 -0800218 observer_->AdaptUp(AdaptationObserverInterface::AdaptReason::kQuality);
kthelgason194f40a2016-09-14 02:14:58 -0700219}
220
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200221void QualityScaler::ReportQpHigh() {
kthelgason876222f2016-11-29 01:44:11 -0800222 RTC_DCHECK_CALLED_SEQUENTIALLY(&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 }
jackychen6e2ce6e2015-07-13 16:26:33 -0700229}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000230
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000231void QualityScaler::ClearSamples() {
kthelgason876222f2016-11-29 01:44:11 -0800232 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200233 framedrop_percent_media_opt_.Reset();
234 framedrop_percent_all_.Reset();
kthelgason194f40a2016-09-14 02:14:58 -0700235 average_qp_.Reset();
Åsa Perssona945aee2018-04-24 16:53:25 +0200236 if (qp_smoother_high_)
237 qp_smoother_high_->Reset();
238 if (qp_smoother_low_)
239 qp_smoother_low_->Reset();
pboscbac40d2016-04-13 02:51:02 -0700240}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000241} // namespace webrtc