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(); |
sakal
2016/09/07 09:47:21
This is not necessary since ReportFramerate will c
| |
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_) | |
96 num_samples_upscale_ = framerate * kMeasureSecondsFastUpscale; | |
magjed_webrtc
2016/09/07 11:55:16
nit: I would like to write this as a single assign
| |
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) { |
108 frames_seen_++; | |
94 framedrop_percent_.AddSample(0); | 109 framedrop_percent_.AddSample(0); |
95 average_qp_downscale_.AddSample(qp); | 110 average_qp_.AddSample(qp); |
96 average_qp_upscale_.AddSample(qp); | |
97 } | 111 } |
98 | 112 |
99 void QualityScaler::ReportDroppedFrame() { | 113 void QualityScaler::ReportDroppedFrame() { |
114 frames_seen_++; | |
100 framedrop_percent_.AddSample(100); | 115 framedrop_percent_.AddSample(100); |
101 } | 116 } |
102 | 117 |
103 void QualityScaler::OnEncodeFrame(int width, int height) { | 118 void QualityScaler::OnEncodeFrame(int width, int height) { |
104 // Should be set through InitEncode -> Should be set by now. | 119 // Should be set through InitEncode -> Should be set by now. |
105 RTC_DCHECK_GE(low_qp_threshold_, 0); | 120 RTC_DCHECK_GE(low_qp_threshold_, 0); |
106 RTC_DCHECK_GT(num_samples_upscale_, 0u); | 121 if (target_res_.width != width || target_res_.height != height) { |
107 RTC_DCHECK_GT(num_samples_downscale_, 0u); | 122 UpdateTargetResolution(width, height); |
123 } | |
108 | 124 |
109 // Update scale factor. | 125 // Check if we should scale down due to high frame drop. |
110 int avg_drop = 0; | 126 if (frames_seen_ >= num_samples_downscale_) { |
magjed_webrtc
2016/09/07 11:55:16
Do we really need this check now that we have rtc:
kthelgason
2016/09/07 12:31:46
Yeah, you're right that it's not necessary. I thou
| |
111 int avg_qp = 0; | 127 const auto drop_rate = |
128 framedrop_percent_.GetAverage(num_samples_downscale_); | |
129 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) { | |
130 ScaleDown(); | |
131 return; | |
132 } | |
112 | 133 |
113 if ((framedrop_percent_.GetAverage(num_samples_downscale_, &avg_drop) && | 134 // Check if we should scale up or down based on QP. |
sakal
2016/09/07 09:47:21
We never scale up here.
kthelgason
2016/09/07 12:31:46
The comment applies to the following two branches,
| |
114 avg_drop >= kFramedropPercentThreshold) || | 135 const auto avg_qp_down = average_qp_.GetAverage(num_samples_downscale_); |
115 (average_qp_downscale_.GetAverage(num_samples_downscale_, &avg_qp) && | 136 if (avg_qp_down && *avg_qp_down > high_qp_threshold_) { |
116 avg_qp > high_qp_threshold_)) { | 137 ScaleDown(); |
117 AdjustScale(false); | 138 return; |
118 } else if (average_qp_upscale_.GetAverage(num_samples_upscale_, &avg_qp) && | 139 } |
119 avg_qp <= low_qp_threshold_) { | |
120 AdjustScale(true); | |
121 } | 140 } |
122 UpdateTargetResolution(width, height); | 141 if (frames_seen_ >= num_samples_upscale_) { |
magjed_webrtc
2016/09/07 11:55:16
ditto: the if-statement seems redundant.
| |
142 const auto avg_qp_up = average_qp_.GetAverage(num_samples_upscale_); | |
143 if (avg_qp_up && *avg_qp_up <= low_qp_threshold_) { | |
144 // QP has been low. We want to try a higher resolution. | |
145 ScaleUp(); | |
146 return; | |
147 } | |
148 } | |
149 } | |
150 | |
151 void QualityScaler::ScaleUp() { | |
152 downscale_shift_ = std::max(0, downscale_shift_ - 1); | |
153 ClearSamples(); | |
154 } | |
155 | |
156 void QualityScaler::ScaleDown() { | |
157 downscale_shift_ = std::min(maximum_shift_, downscale_shift_ + 1); | |
158 ClearSamples(); | |
159 // If we've scaled down, wait longer before scaling up again. | |
160 if (fast_rampup_) { | |
161 fast_rampup_ = false; | |
162 num_samples_upscale_ = (num_samples_upscale_ / kMeasureSecondsFastUpscale) * | |
163 kMeasureSecondsUpscale; | |
164 } | |
123 } | 165 } |
124 | 166 |
125 QualityScaler::Resolution QualityScaler::GetScaledResolution() const { | 167 QualityScaler::Resolution QualityScaler::GetScaledResolution() const { |
126 return res_; | 168 const int frame_width = target_res_.width >> downscale_shift_; |
169 const int frame_height = target_res_.height >> downscale_shift_; | |
170 return Resolution{frame_width, frame_height}; | |
127 } | 171 } |
128 | 172 |
129 rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer( | 173 rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer( |
130 const rtc::scoped_refptr<VideoFrameBuffer>& frame) { | 174 const rtc::scoped_refptr<VideoFrameBuffer>& frame) { |
131 Resolution res = GetScaledResolution(); | 175 Resolution res = GetScaledResolution(); |
132 int src_width = frame->width(); | 176 const int src_width = frame->width(); |
133 int src_height = frame->height(); | 177 const int src_height = frame->height(); |
134 | 178 |
135 if (res.width == src_width && res.height == src_height) | 179 if (res.width == src_width && res.height == src_height) |
136 return frame; | 180 return frame; |
137 rtc::scoped_refptr<I420Buffer> scaled_buffer = | 181 rtc::scoped_refptr<I420Buffer> scaled_buffer = |
138 pool_.CreateBuffer(res.width, res.height); | 182 pool_.CreateBuffer(res.width, res.height); |
139 | 183 |
140 scaled_buffer->ScaleFrom(frame); | 184 scaled_buffer->ScaleFrom(frame); |
141 | 185 |
142 return scaled_buffer; | 186 return scaled_buffer; |
143 } | 187 } |
144 | 188 |
145 void QualityScaler::UpdateTargetResolution(int frame_width, int frame_height) { | 189 void QualityScaler::UpdateTargetResolution(int width, int height) { |
146 RTC_DCHECK_GE(downscale_shift_, 0); | 190 if (width < kMinDownscaleDimension || height < kMinDownscaleDimension) { |
147 int shifts_performed = 0; | 191 maximum_shift_ = 0; |
148 for (int shift = downscale_shift_; | 192 } else { |
149 shift > 0 && (frame_width / 2 >= kMinDownscaleDimension) && | 193 maximum_shift_ = static_cast<int>( |
150 (frame_height / 2 >= kMinDownscaleDimension); | 194 std::log2(std::min(width, height) / kMinDownscaleDimension)); |
151 --shift, ++shifts_performed) { | |
152 frame_width /= 2; | |
153 frame_height /= 2; | |
154 } | 195 } |
155 // Clamp to number of shifts actually performed to not be stuck trying to | 196 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 } | 197 } |
166 | 198 |
167 void QualityScaler::ClearSamples() { | 199 void QualityScaler::ClearSamples() { |
168 framedrop_percent_.Reset(); | 200 framedrop_percent_.Reset(); |
169 average_qp_downscale_.Reset(); | 201 average_qp_.Reset(); |
170 average_qp_upscale_.Reset(); | 202 frames_seen_ = 0; |
171 } | 203 } |
172 | 204 |
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 | 205 |
191 } // namespace webrtc | 206 } // namespace webrtc |
OLD | NEW |