pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 1 | /* |
| 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öller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/utility/quality_scaler.h" |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 12 | |
Kári Tristan Helgason | 5a20ed3 | 2016-09-15 10:56:19 +0200 | [diff] [blame] | 13 | #include <math.h> |
| 14 | |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 15 | #include <algorithm> |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 16 | #include <memory> |
Kári Tristan Helgason | 5a20ed3 | 2016-09-15 10:56:19 +0200 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/logging.h" |
| 20 | #include "rtc_base/task_queue.h" |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 21 | |
Kári Tristan Helgason | 5a20ed3 | 2016-09-15 10:56:19 +0200 | [diff] [blame] | 22 | // 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 |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 27 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 28 | namespace webrtc { |
| 29 | |
Peter Boström | 926dfcd | 2016-04-14 14:48:10 +0200 | [diff] [blame] | 30 | namespace { |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 31 | // TODO(nisse): Delete, delegate to encoders. |
pbos | cbac40d | 2016-04-13 02:51:02 -0700 | [diff] [blame] | 32 | // Threshold constant used until first downscale (to permit fast rampup). |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 33 | static const int kMeasureMs = 2000; |
| 34 | static const float kSamplePeriodScaleFactor = 2.5; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 35 | static const int kFramedropPercentThreshold = 60; |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 36 | static const int kMinFramesNeededToScale = 2 * 30; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 37 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 38 | } // namespace |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 39 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 40 | class QualityScaler::CheckQPTask : public rtc::QueuedTask { |
| 41 | public: |
| 42 | explicit CheckQPTask(QualityScaler* scaler) : scaler_(scaler) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 43 | RTC_LOG(LS_INFO) << "Created CheckQPTask. Scheduling on queue..."; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 44 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 45 | std::unique_ptr<rtc::QueuedTask>(this), scaler_->GetSamplingPeriodMs()); |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 46 | } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 47 | void Stop() { |
| 48 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 49 | RTC_LOG(LS_INFO) << "Stopping QP Check task."; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 50 | stop_ = true; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | bool Run() override { |
| 55 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 56 | if (stop_) |
| 57 | return true; // TaskQueue will free this task. |
| 58 | scaler_->CheckQP(); |
| 59 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 60 | std::unique_ptr<rtc::QueuedTask>(this), scaler_->GetSamplingPeriodMs()); |
| 61 | return false; // Retain the task in order to reuse it. |
| 62 | } |
| 63 | |
| 64 | QualityScaler* const scaler_; |
| 65 | bool stop_ = false; |
| 66 | rtc::SequencedTaskChecker task_checker_; |
| 67 | }; |
| 68 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 69 | QualityScaler::QualityScaler(AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 70 | VideoEncoder::QpThresholds thresholds) |
| 71 | : QualityScaler(observer, thresholds, kMeasureMs) {} |
| 72 | |
| 73 | // Protected ctor, should not be called directly. |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 74 | QualityScaler::QualityScaler(AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 75 | VideoEncoder::QpThresholds thresholds, |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 76 | int64_t sampling_period_ms) |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 77 | : check_qp_task_(nullptr), |
| 78 | observer_(observer), |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 79 | thresholds_(thresholds), |
| 80 | sampling_period_ms_(sampling_period_ms), |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 81 | fast_rampup_(true), |
| 82 | // Arbitrarily choose size based on 30 fps for 5 seconds. |
| 83 | average_qp_(5 * 30), |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 84 | framedrop_percent_(5 * 30) { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 85 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 86 | RTC_DCHECK(observer_ != nullptr); |
| 87 | check_qp_task_ = new CheckQPTask(this); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 88 | RTC_LOG(LS_INFO) << "QP thresholds: low: " << thresholds_.low |
| 89 | << ", high: " << thresholds_.high; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 90 | } |
| 91 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 92 | QualityScaler::~QualityScaler() { |
| 93 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 94 | check_qp_task_->Stop(); |
| 95 | } |
| 96 | |
| 97 | int64_t QualityScaler::GetSamplingPeriodMs() const { |
| 98 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 99 | return fast_rampup_ ? sampling_period_ms_ |
| 100 | : (sampling_period_ms_ * kSamplePeriodScaleFactor); |
| 101 | } |
| 102 | |
| 103 | void QualityScaler::ReportDroppedFrame() { |
| 104 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 105 | framedrop_percent_.AddSample(100); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 106 | } |
| 107 | |
jackychen | 98d8cf5 | 2015-05-21 11:12:02 -0700 | [diff] [blame] | 108 | void QualityScaler::ReportQP(int qp) { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 109 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 110 | framedrop_percent_.AddSample(0); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 111 | average_qp_.AddSample(qp); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 112 | } |
| 113 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 114 | void QualityScaler::CheckQP() { |
| 115 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 116 | // Should be set through InitEncode -> Should be set by now. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 117 | RTC_DCHECK_GE(thresholds_.low, 0); |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 118 | |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 119 | // If we have not observed at least this many frames we can't make a good |
| 120 | // scaling decision. |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame] | 121 | if (framedrop_percent_.size() < kMinFramesNeededToScale) |
| 122 | return; |
| 123 | |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 124 | // Check if we should scale down due to high frame drop. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 125 | const rtc::Optional<int> drop_rate = framedrop_percent_.GetAverage(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 126 | if (drop_rate && *drop_rate >= kFramedropPercentThreshold) { |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 127 | RTC_LOG(LS_INFO) << "Reporting high QP, framedrop percent " << *drop_rate; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 128 | ReportQPHigh(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
| 132 | // Check if we should scale up or down based on QP. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 133 | const rtc::Optional<int> avg_qp = average_qp_.GetAverage(); |
glaznev | d1c4435 | 2017-03-23 14:40:08 -0700 | [diff] [blame] | 134 | if (avg_qp) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 135 | RTC_LOG(LS_INFO) << "Checking average QP " << *avg_qp; |
glaznev | d1c4435 | 2017-03-23 14:40:08 -0700 | [diff] [blame] | 136 | if (*avg_qp > thresholds_.high) { |
| 137 | ReportQPHigh(); |
| 138 | return; |
| 139 | } |
| 140 | if (*avg_qp <= thresholds_.low) { |
| 141 | // QP has been low. We want to try a higher resolution. |
| 142 | ReportQPLow(); |
| 143 | return; |
| 144 | } |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 148 | void QualityScaler::ReportQPLow() { |
| 149 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 150 | ClearSamples(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 151 | observer_->AdaptUp(AdaptationObserverInterface::AdaptReason::kQuality); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 152 | } |
| 153 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 154 | void QualityScaler::ReportQPHigh() { |
| 155 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 156 | ClearSamples(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 157 | observer_->AdaptDown(AdaptationObserverInterface::AdaptReason::kQuality); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 158 | // If we've scaled down, wait longer before scaling up again. |
| 159 | if (fast_rampup_) { |
| 160 | fast_rampup_ = false; |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 161 | } |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 162 | } |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 163 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 164 | void QualityScaler::ClearSamples() { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 165 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 166 | framedrop_percent_.Reset(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 167 | average_qp_.Reset(); |
pbos | cbac40d | 2016-04-13 02:51:02 -0700 | [diff] [blame] | 168 | } |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 169 | } // namespace webrtc |