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 if (fast_rampup_) | |
sakal
2016/09/08 08:21:41
nit: num_samples_upscale_ = framerate * (fast_ramp
| |
96 num_samples_upscale_ = framerate * kMeasureSecondsFastUpscale; | |
97 else | |
98 num_samples_upscale_ = framerate * kMeasureSecondsUpscale; | |
99 num_samples_downscale_ = framerate * kMeasureSecondsDownscale; | |
100 | |
101 average_qp_ = | |
102 MovingAverage(std::max(num_samples_upscale_, num_samples_downscale_)); | |
103 framedrop_percent_ = | |
104 MovingAverage(std::max(num_samples_upscale_, num_samples_downscale_)); | |
91 } | 105 } |
92 | 106 |
93 void QualityScaler::ReportQP(int qp) { | 107 void QualityScaler::ReportQP(int qp) { |
94 framedrop_percent_.AddSample(0); | 108 framedrop_percent_.AddSample(0); |
95 average_qp_downscale_.AddSample(qp); | 109 average_qp_.AddSample(qp); |
96 average_qp_upscale_.AddSample(qp); | |
97 } | 110 } |
98 | 111 |
99 void QualityScaler::ReportDroppedFrame() { | 112 void QualityScaler::ReportDroppedFrame() { |
100 framedrop_percent_.AddSample(100); | 113 framedrop_percent_.AddSample(100); |
101 } | 114 } |
102 | 115 |
103 void QualityScaler::OnEncodeFrame(int width, int height) { | 116 void QualityScaler::OnEncodeFrame(int width, int height) { |
104 // Should be set through InitEncode -> Should be set by now. | 117 // Should be set through InitEncode -> Should be set by now. |
105 RTC_DCHECK_GE(low_qp_threshold_, 0); | 118 RTC_DCHECK_GE(low_qp_threshold_, 0); |
106 RTC_DCHECK_GT(num_samples_upscale_, 0u); | 119 if (target_res_.width != width || target_res_.height != height) { |
107 RTC_DCHECK_GT(num_samples_downscale_, 0u); | 120 UpdateTargetResolution(width, height); |
121 } | |
108 | 122 |
109 // Update scale factor. | 123 // Check if we should scale down due to high frame drop. |
110 int avg_drop = 0; | 124 const auto drop_rate = |
111 int avg_qp = 0; | 125 framedrop_percent_.GetAverage(num_samples_downscale_); |
126 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) { | |
127 ScaleDown(); | |
128 return; | |
129 } | |
112 | 130 |
113 if ((framedrop_percent_.GetAverage(num_samples_downscale_, &avg_drop) && | 131 // Check if we should scale up or down based on QP. |
114 avg_drop >= kFramedropPercentThreshold) || | 132 const auto avg_qp_down = average_qp_.GetAverage(num_samples_downscale_); |
115 (average_qp_downscale_.GetAverage(num_samples_downscale_, &avg_qp) && | 133 if (avg_qp_down && *avg_qp_down > high_qp_threshold_) { |
116 avg_qp > high_qp_threshold_)) { | 134 ScaleDown(); |
117 AdjustScale(false); | 135 return; |
118 } else if (average_qp_upscale_.GetAverage(num_samples_upscale_, &avg_qp) && | |
119 avg_qp <= low_qp_threshold_) { | |
120 AdjustScale(true); | |
121 } | 136 } |
122 UpdateTargetResolution(width, height); | 137 const auto avg_qp_up = average_qp_.GetAverage(num_samples_upscale_); |
138 if (avg_qp_up && *avg_qp_up <= low_qp_threshold_) { | |
139 // QP has been low. We want to try a higher resolution. | |
140 ScaleUp(); | |
141 return; | |
142 } | |
143 } | |
144 | |
145 void QualityScaler::ScaleUp() { | |
146 downscale_shift_ = std::max(0, downscale_shift_ - 1); | |
147 ClearSamples(); | |
148 } | |
149 | |
150 void QualityScaler::ScaleDown() { | |
151 downscale_shift_ = std::min(maximum_shift_, downscale_shift_ + 1); | |
152 ClearSamples(); | |
153 // If we've scaled down, wait longer before scaling up again. | |
154 if (fast_rampup_) { | |
155 fast_rampup_ = false; | |
156 num_samples_upscale_ = (num_samples_upscale_ / kMeasureSecondsFastUpscale) * | |
157 kMeasureSecondsUpscale; | |
158 } | |
123 } | 159 } |
124 | 160 |
125 QualityScaler::Resolution QualityScaler::GetScaledResolution() const { | 161 QualityScaler::Resolution QualityScaler::GetScaledResolution() const { |
126 return res_; | 162 const int frame_width = target_res_.width >> downscale_shift_; |
163 const int frame_height = target_res_.height >> downscale_shift_; | |
164 return Resolution{frame_width, frame_height}; | |
127 } | 165 } |
128 | 166 |
129 rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer( | 167 rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer( |
130 const rtc::scoped_refptr<VideoFrameBuffer>& frame) { | 168 const rtc::scoped_refptr<VideoFrameBuffer>& frame) { |
131 Resolution res = GetScaledResolution(); | 169 Resolution res = GetScaledResolution(); |
132 int src_width = frame->width(); | 170 const int src_width = frame->width(); |
133 int src_height = frame->height(); | 171 const int src_height = frame->height(); |
134 | 172 |
135 if (res.width == src_width && res.height == src_height) | 173 if (res.width == src_width && res.height == src_height) |
136 return frame; | 174 return frame; |
137 rtc::scoped_refptr<I420Buffer> scaled_buffer = | 175 rtc::scoped_refptr<I420Buffer> scaled_buffer = |
138 pool_.CreateBuffer(res.width, res.height); | 176 pool_.CreateBuffer(res.width, res.height); |
139 | 177 |
140 scaled_buffer->ScaleFrom(frame); | 178 scaled_buffer->ScaleFrom(frame); |
141 | 179 |
142 return scaled_buffer; | 180 return scaled_buffer; |
143 } | 181 } |
144 | 182 |
145 void QualityScaler::UpdateTargetResolution(int frame_width, int frame_height) { | 183 void QualityScaler::UpdateTargetResolution(int width, int height) { |
146 RTC_DCHECK_GE(downscale_shift_, 0); | 184 if (width < kMinDownscaleDimension || height < kMinDownscaleDimension) { |
147 int shifts_performed = 0; | 185 maximum_shift_ = 0; |
148 for (int shift = downscale_shift_; | 186 } else { |
149 shift > 0 && (frame_width / 2 >= kMinDownscaleDimension) && | 187 maximum_shift_ = static_cast<int>( |
150 (frame_height / 2 >= kMinDownscaleDimension); | 188 std::log2(std::min(width, height) / kMinDownscaleDimension)); |
151 --shift, ++shifts_performed) { | |
152 frame_width /= 2; | |
153 frame_height /= 2; | |
154 } | 189 } |
155 // Clamp to number of shifts actually performed to not be stuck trying to | 190 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 } | 191 } |
166 | 192 |
167 void QualityScaler::ClearSamples() { | 193 void QualityScaler::ClearSamples() { |
168 framedrop_percent_.Reset(); | 194 framedrop_percent_.Reset(); |
169 average_qp_downscale_.Reset(); | 195 average_qp_.Reset(); |
170 average_qp_upscale_.Reset(); | |
171 } | 196 } |
172 | 197 |
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 | 198 |
191 } // namespace webrtc | 199 } // namespace webrtc |
OLD | NEW |