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 <algorithm> |
| 14 #include <cmath> |
| 15 |
13 namespace webrtc { | 16 namespace webrtc { |
14 | 17 |
15 namespace { | 18 namespace { |
16 static const int kMinFps = 5; | |
17 // Threshold constant used until first downscale (to permit fast rampup). | 19 // Threshold constant used until first downscale (to permit fast rampup). |
18 static const int kMeasureSecondsFastUpscale = 2; | 20 static const int kMeasureSecondsFastUpscale = 2; |
19 static const int kMeasureSecondsUpscale = 5; | 21 static const int kMeasureSecondsUpscale = 5; |
20 static const int kMeasureSecondsDownscale = 5; | 22 static const int kMeasureSecondsDownscale = 5; |
21 static const int kFramedropPercentThreshold = 60; | 23 static const int kFramedropPercentThreshold = 60; |
22 // Min width/height to downscale to, set to not go below QVGA, but with some | 24 // Min width/height to downscale to, set to not go below QVGA, but with some |
23 // margin to permit "almost-QVGA" resolutions, such as QCIF. | 25 // margin to permit "almost-QVGA" resolutions, such as QCIF. |
24 static const int kMinDownscaleDimension = 140; | 26 static const int kMinDownscaleDimension = 140; |
25 // Initial resolutions corresponding to a bitrate. Aa bit above their actual | 27 // Initial resolutions corresponding to a bitrate. Aa bit above their actual |
26 // values to permit near-VGA and near-QVGA resolutions to use the same | 28 // values to permit near-VGA and near-QVGA resolutions to use the same |
(...skipping 12 matching lines...) Expand all Loading... |
39 const int QualityScaler::kBadVp8QpThreshold = 95; | 41 const int QualityScaler::kBadVp8QpThreshold = 95; |
40 | 42 |
41 #if defined(WEBRTC_IOS) | 43 #if defined(WEBRTC_IOS) |
42 const int QualityScaler::kLowH264QpThreshold = 32; | 44 const int QualityScaler::kLowH264QpThreshold = 32; |
43 const int QualityScaler::kBadH264QpThreshold = 42; | 45 const int QualityScaler::kBadH264QpThreshold = 42; |
44 #else | 46 #else |
45 const int QualityScaler::kLowH264QpThreshold = 24; | 47 const int QualityScaler::kLowH264QpThreshold = 24; |
46 const int QualityScaler::kBadH264QpThreshold = 37; | 48 const int QualityScaler::kBadH264QpThreshold = 37; |
47 #endif | 49 #endif |
48 | 50 |
49 QualityScaler::QualityScaler() : low_qp_threshold_(-1) {} | 51 // Default values. Should immediately get set to something more sensible. |
| 52 QualityScaler::QualityScaler() |
| 53 : average_qp_(kMeasureSecondsUpscale * 30), |
| 54 framedrop_percent_(kMeasureSecondsUpscale * 30), |
| 55 low_qp_threshold_(-1) {} |
50 | 56 |
51 void QualityScaler::Init(int low_qp_threshold, | 57 void QualityScaler::Init(int low_qp_threshold, |
52 int high_qp_threshold, | 58 int high_qp_threshold, |
53 int initial_bitrate_kbps, | 59 int initial_bitrate_kbps, |
54 int width, | 60 int width, |
55 int height, | 61 int height, |
56 int fps) { | 62 int fps) { |
57 ClearSamples(); | |
58 low_qp_threshold_ = low_qp_threshold; | 63 low_qp_threshold_ = low_qp_threshold; |
59 high_qp_threshold_ = high_qp_threshold; | 64 high_qp_threshold_ = high_qp_threshold; |
60 downscale_shift_ = 0; | 65 downscale_shift_ = 0; |
61 // Use a faster window for upscaling initially (but be more graceful later). | 66 |
62 // This enables faster initial rampups without risking strong up-down | 67 fast_rampup_ = true; |
63 // behavior later. | 68 |
64 measure_seconds_upscale_ = kMeasureSecondsFastUpscale; | 69 ClearSamples(); |
| 70 ReportFramerate(fps); |
| 71 |
65 const int init_width = width; | 72 const int init_width = width; |
66 const int init_height = height; | 73 const int init_height = height; |
67 if (initial_bitrate_kbps > 0) { | 74 if (initial_bitrate_kbps > 0) { |
68 int init_num_pixels = width * height; | 75 int init_num_pixels = width * height; |
69 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps) | 76 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps) |
70 init_num_pixels = kVgaNumPixels; | 77 init_num_pixels = kVgaNumPixels; |
71 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps) | 78 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps) |
72 init_num_pixels = kQvgaNumPixels; | 79 init_num_pixels = kQvgaNumPixels; |
73 while (width * height > init_num_pixels) { | 80 while (width * height > init_num_pixels) { |
74 ++downscale_shift_; | 81 ++downscale_shift_; |
75 width /= 2; | 82 width /= 2; |
76 height /= 2; | 83 height /= 2; |
77 } | 84 } |
78 } | 85 } |
79 | |
80 // Zero out width/height so they can be checked against inside | |
81 // UpdateTargetResolution. | |
82 res_.width = res_.height = 0; | |
83 UpdateTargetResolution(init_width, init_height); | 86 UpdateTargetResolution(init_width, init_height); |
84 ReportFramerate(fps); | 87 ReportFramerate(fps); |
85 } | 88 } |
86 | 89 |
87 // Report framerate(fps) to estimate # of samples. | 90 // Report framerate(fps) to estimate # of samples. |
88 void QualityScaler::ReportFramerate(int framerate) { | 91 void QualityScaler::ReportFramerate(int framerate) { |
89 framerate_ = framerate; | 92 // Use a faster window for upscaling initially. |
90 UpdateSampleCounts(); | 93 // This enables faster initial rampups without risking strong up-down |
| 94 // behavior later. |
| 95 num_samples_upscale_ = framerate * (fast_rampup_ ? kMeasureSecondsFastUpscale |
| 96 : kMeasureSecondsUpscale); |
| 97 num_samples_downscale_ = framerate * kMeasureSecondsDownscale; |
| 98 |
| 99 average_qp_ = |
| 100 MovingAverage(std::max(num_samples_upscale_, num_samples_downscale_)); |
| 101 framedrop_percent_ = |
| 102 MovingAverage(std::max(num_samples_upscale_, num_samples_downscale_)); |
91 } | 103 } |
92 | 104 |
93 void QualityScaler::ReportQP(int qp) { | 105 void QualityScaler::ReportQP(int qp) { |
94 framedrop_percent_.AddSample(0); | 106 framedrop_percent_.AddSample(0); |
95 average_qp_downscale_.AddSample(qp); | 107 average_qp_.AddSample(qp); |
96 average_qp_upscale_.AddSample(qp); | |
97 } | 108 } |
98 | 109 |
99 void QualityScaler::ReportDroppedFrame() { | 110 void QualityScaler::ReportDroppedFrame() { |
100 framedrop_percent_.AddSample(100); | 111 framedrop_percent_.AddSample(100); |
101 } | 112 } |
102 | 113 |
103 void QualityScaler::OnEncodeFrame(int width, int height) { | 114 void QualityScaler::OnEncodeFrame(int width, int height) { |
104 // Should be set through InitEncode -> Should be set by now. | 115 // Should be set through InitEncode -> Should be set by now. |
105 RTC_DCHECK_GE(low_qp_threshold_, 0); | 116 RTC_DCHECK_GE(low_qp_threshold_, 0); |
106 RTC_DCHECK_GT(num_samples_upscale_, 0u); | 117 if (target_res_.width != width || target_res_.height != height) { |
107 RTC_DCHECK_GT(num_samples_downscale_, 0u); | 118 UpdateTargetResolution(width, height); |
| 119 } |
108 | 120 |
109 // Update scale factor. | 121 // Check if we should scale down due to high frame drop. |
110 int avg_drop = 0; | 122 const auto drop_rate = framedrop_percent_.GetAverage(num_samples_downscale_); |
111 int avg_qp = 0; | 123 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) { |
| 124 ScaleDown(); |
| 125 return; |
| 126 } |
112 | 127 |
113 if ((framedrop_percent_.GetAverage(num_samples_downscale_, &avg_drop) && | 128 // Check if we should scale up or down based on QP. |
114 avg_drop >= kFramedropPercentThreshold) || | 129 const auto avg_qp_down = average_qp_.GetAverage(num_samples_downscale_); |
115 (average_qp_downscale_.GetAverage(num_samples_downscale_, &avg_qp) && | 130 if (avg_qp_down && *avg_qp_down > high_qp_threshold_) { |
116 avg_qp > high_qp_threshold_)) { | 131 ScaleDown(); |
117 AdjustScale(false); | 132 return; |
118 } else if (average_qp_upscale_.GetAverage(num_samples_upscale_, &avg_qp) && | |
119 avg_qp <= low_qp_threshold_) { | |
120 AdjustScale(true); | |
121 } | 133 } |
122 UpdateTargetResolution(width, height); | 134 const auto avg_qp_up = average_qp_.GetAverage(num_samples_upscale_); |
| 135 if (avg_qp_up && *avg_qp_up <= low_qp_threshold_) { |
| 136 // QP has been low. We want to try a higher resolution. |
| 137 ScaleUp(); |
| 138 return; |
| 139 } |
| 140 } |
| 141 |
| 142 void QualityScaler::ScaleUp() { |
| 143 downscale_shift_ = std::max(0, downscale_shift_ - 1); |
| 144 ClearSamples(); |
| 145 } |
| 146 |
| 147 void QualityScaler::ScaleDown() { |
| 148 downscale_shift_ = std::min(maximum_shift_, downscale_shift_ + 1); |
| 149 ClearSamples(); |
| 150 // If we've scaled down, wait longer before scaling up again. |
| 151 if (fast_rampup_) { |
| 152 fast_rampup_ = false; |
| 153 num_samples_upscale_ = (num_samples_upscale_ / kMeasureSecondsFastUpscale) * |
| 154 kMeasureSecondsUpscale; |
| 155 } |
123 } | 156 } |
124 | 157 |
125 QualityScaler::Resolution QualityScaler::GetScaledResolution() const { | 158 QualityScaler::Resolution QualityScaler::GetScaledResolution() const { |
126 return res_; | 159 const int frame_width = target_res_.width >> downscale_shift_; |
| 160 const int frame_height = target_res_.height >> downscale_shift_; |
| 161 return Resolution{frame_width, frame_height}; |
127 } | 162 } |
128 | 163 |
129 rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer( | 164 rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer( |
130 const rtc::scoped_refptr<VideoFrameBuffer>& frame) { | 165 const rtc::scoped_refptr<VideoFrameBuffer>& frame) { |
131 Resolution res = GetScaledResolution(); | 166 Resolution res = GetScaledResolution(); |
132 int src_width = frame->width(); | 167 const int src_width = frame->width(); |
133 int src_height = frame->height(); | 168 const int src_height = frame->height(); |
134 | 169 |
135 if (res.width == src_width && res.height == src_height) | 170 if (res.width == src_width && res.height == src_height) |
136 return frame; | 171 return frame; |
137 rtc::scoped_refptr<I420Buffer> scaled_buffer = | 172 rtc::scoped_refptr<I420Buffer> scaled_buffer = |
138 pool_.CreateBuffer(res.width, res.height); | 173 pool_.CreateBuffer(res.width, res.height); |
139 | 174 |
140 scaled_buffer->ScaleFrom(frame); | 175 scaled_buffer->ScaleFrom(frame); |
141 | 176 |
142 return scaled_buffer; | 177 return scaled_buffer; |
143 } | 178 } |
144 | 179 |
145 void QualityScaler::UpdateTargetResolution(int frame_width, int frame_height) { | 180 void QualityScaler::UpdateTargetResolution(int width, int height) { |
146 RTC_DCHECK_GE(downscale_shift_, 0); | 181 if (width < kMinDownscaleDimension || height < kMinDownscaleDimension) { |
147 int shifts_performed = 0; | 182 maximum_shift_ = 0; |
148 for (int shift = downscale_shift_; | 183 } else { |
149 shift > 0 && (frame_width / 2 >= kMinDownscaleDimension) && | 184 maximum_shift_ = static_cast<int>( |
150 (frame_height / 2 >= kMinDownscaleDimension); | 185 std::log2(std::min(width, height) / kMinDownscaleDimension)); |
151 --shift, ++shifts_performed) { | |
152 frame_width /= 2; | |
153 frame_height /= 2; | |
154 } | 186 } |
155 // Clamp to number of shifts actually performed to not be stuck trying to | 187 target_res_ = Resolution{width, height}; |
156 // scale way beyond QVGA. | |
157 downscale_shift_ = shifts_performed; | |
158 if (res_.width == frame_width && res_.height == frame_height) { | |
159 // No reset done/needed, using same resolution. | |
160 return; | |
161 } | |
162 res_.width = frame_width; | |
163 res_.height = frame_height; | |
164 ClearSamples(); | |
165 } | 188 } |
166 | 189 |
167 void QualityScaler::ClearSamples() { | 190 void QualityScaler::ClearSamples() { |
168 framedrop_percent_.Reset(); | 191 framedrop_percent_.Reset(); |
169 average_qp_downscale_.Reset(); | 192 average_qp_.Reset(); |
170 average_qp_upscale_.Reset(); | |
171 } | 193 } |
172 | 194 |
173 void QualityScaler::UpdateSampleCounts() { | |
174 num_samples_downscale_ = static_cast<size_t>( | |
175 kMeasureSecondsDownscale * (framerate_ < kMinFps ? kMinFps : framerate_)); | |
176 num_samples_upscale_ = static_cast<size_t>( | |
177 measure_seconds_upscale_ * (framerate_ < kMinFps ? kMinFps : framerate_)); | |
178 } | |
179 | |
180 void QualityScaler::AdjustScale(bool up) { | |
181 downscale_shift_ += up ? -1 : 1; | |
182 if (downscale_shift_ < 0) | |
183 downscale_shift_ = 0; | |
184 if (!up) { | |
185 // First downscale hit, start using a slower threshold for going up. | |
186 measure_seconds_upscale_ = kMeasureSecondsUpscale; | |
187 UpdateSampleCounts(); | |
188 } | |
189 } | |
190 | 195 |
191 } // namespace webrtc | 196 } // namespace webrtc |
OLD | NEW |