OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/video_coding/utility/quality_scaler.h" | 11 #include "webrtc/modules/video_coding/utility/quality_scaler.h" |
12 | 12 |
13 #include <math.h> | 13 #include <math.h> |
14 | 14 |
15 #include <algorithm> | 15 #include <algorithm> |
| 16 #include <memory> |
16 | 17 |
17 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/base/logging.h" |
| 20 #include "webrtc/base/task_queue.h" |
18 | 21 |
19 // TODO(kthelgason): Some versions of Android have issues with log2. | 22 // TODO(kthelgason): Some versions of Android have issues with log2. |
20 // See https://code.google.com/p/android/issues/detail?id=212634 for details | 23 // See https://code.google.com/p/android/issues/detail?id=212634 for details |
21 #if defined(WEBRTC_ANDROID) | 24 #if defined(WEBRTC_ANDROID) |
22 #define log2(x) (log(x) / log(2)) | 25 #define log2(x) (log(x) / log(2)) |
23 #endif | 26 #endif |
24 | 27 |
25 namespace webrtc { | 28 namespace webrtc { |
26 | 29 |
27 namespace { | 30 namespace { |
28 // Threshold constant used until first downscale (to permit fast rampup). | 31 // Threshold constant used until first downscale (to permit fast rampup). |
29 static const int kMeasureSecondsFastUpscale = 2; | 32 static const int kMeasureMs = 2000; |
30 static const int kMeasureSecondsUpscale = 5; | 33 static const float kSamplePeriodScaleFactor = 2.5; |
31 static const int kMeasureSecondsDownscale = 5; | |
32 static const int kFramedropPercentThreshold = 60; | 34 static const int kFramedropPercentThreshold = 60; |
33 // Min width/height to downscale to, set to not go below QVGA, but with some | |
34 // margin to permit "almost-QVGA" resolutions, such as QCIF. | |
35 static const int kMinDownscaleDimension = 140; | |
36 // Initial resolutions corresponding to a bitrate. Aa bit above their actual | |
37 // values to permit near-VGA and near-QVGA resolutions to use the same | |
38 // mechanism. | |
39 static const int kVgaBitrateThresholdKbps = 500; | |
40 static const int kVgaNumPixels = 700 * 500; // 640x480 | |
41 static const int kQvgaBitrateThresholdKbps = 250; | |
42 static const int kQvgaNumPixels = 400 * 300; // 320x240 | |
43 | |
44 // QP scaling threshold defaults: | 35 // QP scaling threshold defaults: |
45 static const int kLowH264QpThreshold = 24; | 36 static const int kLowH264QpThreshold = 24; |
46 static const int kHighH264QpThreshold = 37; | 37 static const int kHighH264QpThreshold = 37; |
47 // QP is obtained from VP8-bitstream for HW, so the QP corresponds to the | 38 // QP is obtained from VP8-bitstream for HW, so the QP corresponds to the |
48 // bitstream range of [0, 127] and not the user-level range of [0,63]. | 39 // bitstream range of [0, 127] and not the user-level range of [0,63]. |
49 static const int kLowVp8QpThreshold = 29; | 40 static const int kLowVp8QpThreshold = 29; |
50 static const int kHighVp8QpThreshold = 95; | 41 static const int kHighVp8QpThreshold = 95; |
51 } // namespace | 42 const ScalingObserverInterface::ScaleReason scale_reason_ = |
| 43 ScalingObserverInterface::ScaleReason::kQuality; |
52 | 44 |
53 // Default values. Should immediately get set to something more sensible. | 45 static VideoEncoder::QpThresholds CodecTypeToDefaultThresholds( |
54 QualityScaler::QualityScaler() | 46 VideoCodecType codec_type) { |
55 : average_qp_(kMeasureSecondsUpscale * 30), | 47 int low = -1; |
56 framedrop_percent_(kMeasureSecondsUpscale * 30), | 48 int high = -1; |
57 low_qp_threshold_(-1) {} | |
58 | |
59 void QualityScaler::Init(VideoCodecType codec_type, | |
60 int initial_bitrate_kbps, | |
61 int width, | |
62 int height, | |
63 int fps) { | |
64 int low = -1, high = -1; | |
65 switch (codec_type) { | 49 switch (codec_type) { |
66 case kVideoCodecH264: | 50 case kVideoCodecH264: |
67 low = kLowH264QpThreshold; | 51 low = kLowH264QpThreshold; |
68 high = kHighH264QpThreshold; | 52 high = kHighH264QpThreshold; |
69 break; | 53 break; |
70 case kVideoCodecVP8: | 54 case kVideoCodecVP8: |
71 low = kLowVp8QpThreshold; | 55 low = kLowVp8QpThreshold; |
72 high = kHighVp8QpThreshold; | 56 high = kHighVp8QpThreshold; |
73 break; | 57 break; |
74 default: | 58 default: |
75 RTC_NOTREACHED() << "Invalid codec type for QualityScaler."; | 59 RTC_NOTREACHED() << "Invalid codec type for QualityScaler."; |
76 } | 60 } |
77 Init(low, high, initial_bitrate_kbps, width, height, fps); | 61 return VideoEncoder::QpThresholds(low, high); |
| 62 } |
| 63 } // namespace |
| 64 |
| 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()); |
| 71 } |
| 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); |
78 } | 117 } |
79 | 118 |
80 void QualityScaler::Init(int low_qp_threshold, | 119 QualityScaler::~QualityScaler() { |
81 int high_qp_threshold, | 120 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
82 int initial_bitrate_kbps, | 121 check_qp_task_->Stop(); |
83 int width, | |
84 int height, | |
85 int fps) { | |
86 ClearSamples(); | |
87 low_qp_threshold_ = low_qp_threshold; | |
88 high_qp_threshold_ = high_qp_threshold; | |
89 downscale_shift_ = 0; | |
90 fast_rampup_ = true; | |
91 | |
92 const int init_width = width; | |
93 const int init_height = height; | |
94 if (initial_bitrate_kbps > 0) { | |
95 int init_num_pixels = width * height; | |
96 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps) | |
97 init_num_pixels = kVgaNumPixels; | |
98 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps) | |
99 init_num_pixels = kQvgaNumPixels; | |
100 while (width * height > init_num_pixels) { | |
101 ++downscale_shift_; | |
102 width /= 2; | |
103 height /= 2; | |
104 } | |
105 } | |
106 UpdateTargetResolution(init_width, init_height); | |
107 ReportFramerate(fps); | |
108 } | 122 } |
109 | 123 |
110 // Report framerate(fps) to estimate # of samples. | 124 int64_t QualityScaler::GetSamplingPeriodMs() const { |
111 void QualityScaler::ReportFramerate(int framerate) { | 125 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
112 // Use a faster window for upscaling initially. | 126 return fast_rampup_ ? sampling_period_ms_ |
113 // This enables faster initial rampups without risking strong up-down | 127 : (sampling_period_ms_ * kSamplePeriodScaleFactor); |
114 // behavior later. | 128 } |
115 num_samples_upscale_ = framerate * (fast_rampup_ ? kMeasureSecondsFastUpscale | 129 |
116 : kMeasureSecondsUpscale); | 130 void QualityScaler::ReportDroppedFrame() { |
117 num_samples_downscale_ = framerate * kMeasureSecondsDownscale; | 131 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 132 framedrop_percent_.AddSample(100); |
118 } | 133 } |
119 | 134 |
120 void QualityScaler::ReportQP(int qp) { | 135 void QualityScaler::ReportQP(int qp) { |
| 136 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
121 framedrop_percent_.AddSample(0); | 137 framedrop_percent_.AddSample(0); |
122 average_qp_.AddSample(qp); | 138 average_qp_.AddSample(qp); |
123 } | 139 } |
124 | 140 |
125 void QualityScaler::ReportDroppedFrame() { | 141 void QualityScaler::CheckQP() { |
126 framedrop_percent_.AddSample(100); | 142 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
127 } | |
128 | |
129 void QualityScaler::OnEncodeFrame(int width, int height) { | |
130 // Should be set through InitEncode -> Should be set by now. | 143 // Should be set through InitEncode -> Should be set by now. |
131 RTC_DCHECK_GE(low_qp_threshold_, 0); | 144 RTC_DCHECK_GE(thresholds_.low, 0); |
132 if (target_res_.width != width || target_res_.height != height) { | 145 LOG(LS_INFO) << "Checking if average QP exceeds threshold"; |
133 UpdateTargetResolution(width, height); | |
134 } | |
135 | |
136 // Check if we should scale down due to high frame drop. | 146 // Check if we should scale down due to high frame drop. |
137 const auto drop_rate = framedrop_percent_.GetAverage(num_samples_downscale_); | 147 const rtc::Optional<int> drop_rate = framedrop_percent_.GetAverage(); |
138 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) { | 148 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) { |
139 ScaleDown(); | 149 ReportQPHigh(); |
140 return; | 150 return; |
141 } | 151 } |
142 | 152 |
143 // Check if we should scale up or down based on QP. | 153 // Check if we should scale up or down based on QP. |
144 const auto avg_qp_down = average_qp_.GetAverage(num_samples_downscale_); | 154 const rtc::Optional<int> avg_qp = average_qp_.GetAverage(); |
145 if (avg_qp_down && *avg_qp_down > high_qp_threshold_) { | 155 if (avg_qp && *avg_qp > thresholds_.high) { |
146 ScaleDown(); | 156 ReportQPHigh(); |
147 return; | 157 return; |
148 } | 158 } |
149 const auto avg_qp_up = average_qp_.GetAverage(num_samples_upscale_); | 159 if (avg_qp && *avg_qp <= thresholds_.low) { |
150 if (avg_qp_up && *avg_qp_up <= low_qp_threshold_) { | |
151 // QP has been low. We want to try a higher resolution. | 160 // QP has been low. We want to try a higher resolution. |
152 ScaleUp(); | 161 ReportQPLow(); |
153 return; | 162 return; |
154 } | 163 } |
155 } | 164 } |
156 | 165 |
157 void QualityScaler::ScaleUp() { | 166 void QualityScaler::ReportQPLow() { |
158 downscale_shift_ = std::max(0, downscale_shift_ - 1); | 167 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 168 LOG(LS_INFO) << "QP has been low, asking for higher resolution."; |
159 ClearSamples(); | 169 ClearSamples(); |
| 170 observer_->ScaleUp(scale_reason_); |
160 } | 171 } |
161 | 172 |
162 void QualityScaler::ScaleDown() { | 173 void QualityScaler::ReportQPHigh() { |
163 downscale_shift_ = std::min(maximum_shift_, downscale_shift_ + 1); | 174 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 175 LOG(LS_INFO) << "QP has been high , asking for lower resolution."; |
164 ClearSamples(); | 176 ClearSamples(); |
| 177 observer_->ScaleDown(scale_reason_); |
165 // If we've scaled down, wait longer before scaling up again. | 178 // If we've scaled down, wait longer before scaling up again. |
166 if (fast_rampup_) { | 179 if (fast_rampup_) { |
167 fast_rampup_ = false; | 180 fast_rampup_ = false; |
168 num_samples_upscale_ = (num_samples_upscale_ / kMeasureSecondsFastUpscale) * | |
169 kMeasureSecondsUpscale; | |
170 } | 181 } |
171 } | 182 } |
172 | 183 |
173 QualityScaler::Resolution QualityScaler::GetScaledResolution() const { | |
174 const int frame_width = target_res_.width >> downscale_shift_; | |
175 const int frame_height = target_res_.height >> downscale_shift_; | |
176 return Resolution{frame_width, frame_height}; | |
177 } | |
178 | |
179 rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer( | |
180 const rtc::scoped_refptr<VideoFrameBuffer>& frame) { | |
181 Resolution res = GetScaledResolution(); | |
182 const int src_width = frame->width(); | |
183 const int src_height = frame->height(); | |
184 | |
185 if (res.width == src_width && res.height == src_height) | |
186 return frame; | |
187 rtc::scoped_refptr<I420Buffer> scaled_buffer = | |
188 pool_.CreateBuffer(res.width, res.height); | |
189 | |
190 scaled_buffer->ScaleFrom(*frame); | |
191 | |
192 return scaled_buffer; | |
193 } | |
194 | |
195 void QualityScaler::UpdateTargetResolution(int width, int height) { | |
196 if (width < kMinDownscaleDimension || height < kMinDownscaleDimension) { | |
197 maximum_shift_ = 0; | |
198 } else { | |
199 maximum_shift_ = static_cast<int>( | |
200 log2(std::min(width, height) / kMinDownscaleDimension)); | |
201 } | |
202 target_res_ = Resolution{width, height}; | |
203 } | |
204 | |
205 void QualityScaler::ClearSamples() { | 184 void QualityScaler::ClearSamples() { |
| 185 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
206 framedrop_percent_.Reset(); | 186 framedrop_percent_.Reset(); |
207 average_qp_.Reset(); | 187 average_qp_.Reset(); |
208 } | 188 } |
209 | |
210 | |
211 } // namespace webrtc | 189 } // namespace webrtc |
OLD | NEW |