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