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 | |
kjellander@webrtc.org | b7ce964 | 2015-11-18 23:04:10 +0100 | [diff] [blame] | 11 | #include "webrtc/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 | |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 18 | #include "webrtc/base/checks.h" |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 19 | #include "webrtc/base/logging.h" |
| 20 | #include "webrtc/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 { |
pbos | cbac40d | 2016-04-13 02:51:02 -0700 | [diff] [blame] | 31 | // Threshold constant used until first downscale (to permit fast rampup). |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 32 | static const int kMeasureMs = 2000; |
| 33 | static const float kSamplePeriodScaleFactor = 2.5; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 34 | static const int kFramedropPercentThreshold = 60; |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 35 | // QP scaling threshold defaults: |
| 36 | static const int kLowH264QpThreshold = 24; |
| 37 | static const int kHighH264QpThreshold = 37; |
| 38 | // QP is obtained from VP8-bitstream for HW, so the QP corresponds to the |
| 39 | // bitstream range of [0, 127] and not the user-level range of [0,63]. |
| 40 | static const int kLowVp8QpThreshold = 29; |
| 41 | static const int kHighVp8QpThreshold = 95; |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame^] | 42 | static const int kMinFramesNeededToScale = 2 * 30; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 43 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 44 | static VideoEncoder::QpThresholds CodecTypeToDefaultThresholds( |
| 45 | VideoCodecType codec_type) { |
| 46 | int low = -1; |
| 47 | int high = -1; |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 48 | switch (codec_type) { |
| 49 | case kVideoCodecH264: |
| 50 | low = kLowH264QpThreshold; |
| 51 | high = kHighH264QpThreshold; |
| 52 | break; |
| 53 | case kVideoCodecVP8: |
| 54 | low = kLowVp8QpThreshold; |
| 55 | high = kHighVp8QpThreshold; |
| 56 | break; |
| 57 | default: |
| 58 | RTC_NOTREACHED() << "Invalid codec type for QualityScaler."; |
| 59 | } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 60 | return VideoEncoder::QpThresholds(low, high); |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 61 | } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 62 | } // namespace |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 63 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 64 | class QualityScaler::CheckQPTask : public rtc::QueuedTask { |
| 65 | public: |
| 66 | explicit CheckQPTask(QualityScaler* scaler) : scaler_(scaler) { |
| 67 | LOG(LS_INFO) << "Created CheckQPTask. Scheduling on queue..."; |
| 68 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 69 | std::unique_ptr<rtc::QueuedTask>(this), scaler_->GetSamplingPeriodMs()); |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 70 | } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 71 | void Stop() { |
| 72 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 73 | LOG(LS_INFO) << "Stopping QP Check task."; |
| 74 | stop_ = true; |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | bool Run() override { |
| 79 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 80 | if (stop_) |
| 81 | return true; // TaskQueue will free this task. |
| 82 | scaler_->CheckQP(); |
| 83 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 84 | std::unique_ptr<rtc::QueuedTask>(this), scaler_->GetSamplingPeriodMs()); |
| 85 | return false; // Retain the task in order to reuse it. |
| 86 | } |
| 87 | |
| 88 | QualityScaler* const scaler_; |
| 89 | bool stop_ = false; |
| 90 | rtc::SequencedTaskChecker task_checker_; |
| 91 | }; |
| 92 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 93 | QualityScaler::QualityScaler(AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 94 | VideoCodecType codec_type) |
| 95 | : QualityScaler(observer, CodecTypeToDefaultThresholds(codec_type)) {} |
| 96 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 97 | QualityScaler::QualityScaler(AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 98 | VideoEncoder::QpThresholds thresholds) |
| 99 | : QualityScaler(observer, thresholds, kMeasureMs) {} |
| 100 | |
| 101 | // Protected ctor, should not be called directly. |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 102 | QualityScaler::QualityScaler(AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 103 | VideoEncoder::QpThresholds thresholds, |
| 104 | int64_t sampling_period) |
| 105 | : check_qp_task_(nullptr), |
| 106 | observer_(observer), |
| 107 | sampling_period_ms_(sampling_period), |
| 108 | fast_rampup_(true), |
| 109 | // Arbitrarily choose size based on 30 fps for 5 seconds. |
| 110 | average_qp_(5 * 30), |
| 111 | framedrop_percent_(5 * 30), |
| 112 | thresholds_(thresholds) { |
| 113 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 114 | RTC_DCHECK(observer_ != nullptr); |
| 115 | check_qp_task_ = new CheckQPTask(this); |
glaznev | d1c4435 | 2017-03-23 14:40:08 -0700 | [diff] [blame] | 116 | LOG(LS_INFO) << "QP thresholds: low: " << thresholds_.low |
| 117 | << ", high: " << thresholds_.high; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 118 | } |
| 119 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 120 | QualityScaler::~QualityScaler() { |
| 121 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 122 | check_qp_task_->Stop(); |
| 123 | } |
| 124 | |
| 125 | int64_t QualityScaler::GetSamplingPeriodMs() const { |
| 126 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 127 | return fast_rampup_ ? sampling_period_ms_ |
| 128 | : (sampling_period_ms_ * kSamplePeriodScaleFactor); |
| 129 | } |
| 130 | |
| 131 | void QualityScaler::ReportDroppedFrame() { |
| 132 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 133 | framedrop_percent_.AddSample(100); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 134 | } |
| 135 | |
jackychen | 98d8cf5 | 2015-05-21 11:12:02 -0700 | [diff] [blame] | 136 | void QualityScaler::ReportQP(int qp) { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 137 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 138 | framedrop_percent_.AddSample(0); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 139 | average_qp_.AddSample(qp); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 140 | } |
| 141 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 142 | void QualityScaler::CheckQP() { |
| 143 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 144 | // Should be set through InitEncode -> Should be set by now. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 145 | RTC_DCHECK_GE(thresholds_.low, 0); |
kthelgason | 55a0135 | 2017-04-04 02:31:42 -0700 | [diff] [blame^] | 146 | |
| 147 | // If we have not observed at least this many frames we can't |
| 148 | // make a good scaling decision. |
| 149 | if (framedrop_percent_.size() < kMinFramesNeededToScale) |
| 150 | return; |
| 151 | |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 152 | // Check if we should scale down due to high frame drop. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 153 | const rtc::Optional<int> drop_rate = framedrop_percent_.GetAverage(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 154 | if (drop_rate && *drop_rate >= kFramedropPercentThreshold) { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 155 | ReportQPHigh(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | |
| 159 | // Check if we should scale up or down based on QP. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 160 | const rtc::Optional<int> avg_qp = average_qp_.GetAverage(); |
glaznev | d1c4435 | 2017-03-23 14:40:08 -0700 | [diff] [blame] | 161 | if (avg_qp) { |
| 162 | LOG(LS_INFO) << "Checking average QP " << *avg_qp; |
| 163 | if (*avg_qp > thresholds_.high) { |
| 164 | ReportQPHigh(); |
| 165 | return; |
| 166 | } |
| 167 | if (*avg_qp <= thresholds_.low) { |
| 168 | // QP has been low. We want to try a higher resolution. |
| 169 | ReportQPLow(); |
| 170 | return; |
| 171 | } |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 175 | void QualityScaler::ReportQPLow() { |
| 176 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 177 | ClearSamples(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 178 | observer_->AdaptUp(AdaptationObserverInterface::AdaptReason::kQuality); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 179 | } |
| 180 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 181 | void QualityScaler::ReportQPHigh() { |
| 182 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 183 | ClearSamples(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 184 | observer_->AdaptDown(AdaptationObserverInterface::AdaptReason::kQuality); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 185 | // If we've scaled down, wait longer before scaling up again. |
| 186 | if (fast_rampup_) { |
| 187 | fast_rampup_ = false; |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 188 | } |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 189 | } |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 190 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 191 | void QualityScaler::ClearSamples() { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 192 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 193 | framedrop_percent_.Reset(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 194 | average_qp_.Reset(); |
pbos | cbac40d | 2016-04-13 02:51:02 -0700 | [diff] [blame] | 195 | } |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 196 | } // namespace webrtc |