blob: 54ef76a8eb6e7f6b0bb0550d1851c52569e1ed52 [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
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()),
90 observed_enough_frames_(false) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020091 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +020092 if (experiment_enabled_) {
93 config_ = QualityScalingExperiment::GetConfig();
94 qp_smoother_high_.reset(new QpSmoother(config_.alpha_high));
95 qp_smoother_low_.reset(new QpSmoother(config_.alpha_low));
96 }
kthelgason876222f2016-11-29 01:44:11 -080097 RTC_DCHECK(observer_ != nullptr);
Sebastian Janssonecb68972019-01-18 10:30:54 +010098 check_qp_task_ = RepeatingTaskHandle::DelayedStart(
Sebastian Janssoncda86dd2019-03-11 17:26:36 +010099 task_queue->Get(), TimeDelta::ms(GetSamplingPeriodMs()), [this]() {
Sebastian Janssonecb68972019-01-18 10:30:54 +0100100 CheckQp();
101 return TimeDelta::ms(GetSamplingPeriodMs());
102 });
Mirko Bonadei675513b2017-11-09 11:09:25 +0100103 RTC_LOG(LS_INFO) << "QP thresholds: low: " << thresholds_.low
104 << ", high: " << thresholds_.high;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000105}
106
kthelgason876222f2016-11-29 01:44:11 -0800107QualityScaler::~QualityScaler() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200108 RTC_DCHECK_RUN_ON(&task_checker_);
Sebastian Janssonecb68972019-01-18 10:30:54 +0100109 check_qp_task_.Stop();
kthelgason876222f2016-11-29 01:44:11 -0800110}
111
112int64_t QualityScaler::GetSamplingPeriodMs() const {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200113 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200114 if (fast_rampup_) {
115 return sampling_period_ms_;
116 }
117 if (experiment_enabled_ && !observed_enough_frames_) {
118 // Use half the interval while waiting for enough frames.
119 return sampling_period_ms_ / 2;
120 }
121 return sampling_period_ms_ * kSamplePeriodScaleFactor;
kthelgason876222f2016-11-29 01:44:11 -0800122}
123
Åsa Perssona945aee2018-04-24 16:53:25 +0200124void QualityScaler::ReportDroppedFrameByMediaOpt() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200125 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200126 framedrop_percent_media_opt_.AddSample(100);
127 framedrop_percent_all_.AddSample(100);
128}
129
130void QualityScaler::ReportDroppedFrameByEncoder() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200131 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200132 framedrop_percent_all_.AddSample(100);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000133}
134
Sebastian Janssonb6789402019-03-01 15:40:49 +0100135void QualityScaler::ReportQp(int qp, int64_t time_sent_us) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200136 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200137 framedrop_percent_media_opt_.AddSample(0);
138 framedrop_percent_all_.AddSample(0);
kthelgason194f40a2016-09-14 02:14:58 -0700139 average_qp_.AddSample(qp);
Åsa Perssona945aee2018-04-24 16:53:25 +0200140 if (qp_smoother_high_)
Sebastian Janssonb6789402019-03-01 15:40:49 +0100141 qp_smoother_high_->Add(qp, time_sent_us);
Åsa Perssona945aee2018-04-24 16:53:25 +0200142 if (qp_smoother_low_)
Sebastian Janssonb6789402019-03-01 15:40:49 +0100143 qp_smoother_low_->Add(qp, time_sent_us);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000144}
145
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200146void QualityScaler::CheckQp() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200147 RTC_DCHECK_RUN_ON(&task_checker_);
jackychen61b4d512015-04-21 15:30:11 -0700148 // Should be set through InitEncode -> Should be set by now.
kthelgason876222f2016-11-29 01:44:11 -0800149 RTC_DCHECK_GE(thresholds_.low, 0);
kthelgason55a01352017-04-04 02:31:42 -0700150
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200151 // If we have not observed at least this many frames we can't make a good
152 // scaling decision.
Åsa Perssona945aee2018-04-24 16:53:25 +0200153 const size_t frames = config_.use_all_drop_reasons
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100154 ? framedrop_percent_all_.Size()
155 : framedrop_percent_media_opt_.Size();
Åsa Perssona945aee2018-04-24 16:53:25 +0200156 if (frames < kMinFramesNeededToScale) {
157 observed_enough_frames_ = false;
kthelgason55a01352017-04-04 02:31:42 -0700158 return;
Åsa Perssona945aee2018-04-24 16:53:25 +0200159 }
160 observed_enough_frames_ = true;
kthelgason55a01352017-04-04 02:31:42 -0700161
kthelgason194f40a2016-09-14 02:14:58 -0700162 // Check if we should scale down due to high frame drop.
Danil Chapovalov0040b662018-06-18 10:48:16 +0200163 const absl::optional<int> drop_rate =
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100164 config_.use_all_drop_reasons
165 ? framedrop_percent_all_.GetAverageRoundedDown()
166 : framedrop_percent_media_opt_.GetAverageRoundedDown();
kthelgason194f40a2016-09-14 02:14:58 -0700167 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) {
Åsa Persson0ad2d8a2018-04-19 11:06:11 +0200168 RTC_LOG(LS_INFO) << "Reporting high QP, framedrop percent " << *drop_rate;
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200169 ReportQpHigh();
kthelgason194f40a2016-09-14 02:14:58 -0700170 return;
171 }
172
173 // Check if we should scale up or down based on QP.
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100174 const absl::optional<int> avg_qp_high =
175 qp_smoother_high_ ? qp_smoother_high_->GetAvg()
176 : average_qp_.GetAverageRoundedDown();
Danil Chapovalov0040b662018-06-18 10:48:16 +0200177 const absl::optional<int> avg_qp_low =
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +0100178 qp_smoother_low_ ? qp_smoother_low_->GetAvg()
179 : average_qp_.GetAverageRoundedDown();
Åsa Perssona945aee2018-04-24 16:53:25 +0200180 if (avg_qp_high && avg_qp_low) {
181 RTC_LOG(LS_INFO) << "Checking average QP " << *avg_qp_high << " ("
182 << *avg_qp_low << ").";
183 if (*avg_qp_high > thresholds_.high) {
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200184 ReportQpHigh();
glaznevd1c44352017-03-23 14:40:08 -0700185 return;
186 }
Åsa Perssona945aee2018-04-24 16:53:25 +0200187 if (*avg_qp_low <= thresholds_.low) {
glaznevd1c44352017-03-23 14:40:08 -0700188 // QP has been low. We want to try a higher resolution.
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200189 ReportQpLow();
glaznevd1c44352017-03-23 14:40:08 -0700190 return;
191 }
kthelgason194f40a2016-09-14 02:14:58 -0700192 }
193}
194
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200195void QualityScaler::ReportQpLow() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200196 RTC_DCHECK_RUN_ON(&task_checker_);
kthelgason194f40a2016-09-14 02:14:58 -0700197 ClearSamples();
sprangb1ca0732017-02-01 08:38:12 -0800198 observer_->AdaptUp(AdaptationObserverInterface::AdaptReason::kQuality);
kthelgason194f40a2016-09-14 02:14:58 -0700199}
200
Åsa Persson04d5f1d2018-04-20 15:19:11 +0200201void QualityScaler::ReportQpHigh() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200202 RTC_DCHECK_RUN_ON(&task_checker_);
kthelgason194f40a2016-09-14 02:14:58 -0700203 ClearSamples();
sprangb1ca0732017-02-01 08:38:12 -0800204 observer_->AdaptDown(AdaptationObserverInterface::AdaptReason::kQuality);
kthelgason194f40a2016-09-14 02:14:58 -0700205 // If we've scaled down, wait longer before scaling up again.
206 if (fast_rampup_) {
207 fast_rampup_ = false;
kthelgason194f40a2016-09-14 02:14:58 -0700208 }
jackychen6e2ce6e2015-07-13 16:26:33 -0700209}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000210
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000211void QualityScaler::ClearSamples() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200212 RTC_DCHECK_RUN_ON(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +0200213 framedrop_percent_media_opt_.Reset();
214 framedrop_percent_all_.Reset();
kthelgason194f40a2016-09-14 02:14:58 -0700215 average_qp_.Reset();
Åsa Perssona945aee2018-04-24 16:53:25 +0200216 if (qp_smoother_high_)
217 qp_smoother_high_->Reset();
218 if (qp_smoother_low_)
219 qp_smoother_low_->Reset();
pboscbac40d2016-04-13 02:51:02 -0700220}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000221} // namespace webrtc