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 #include "webrtc/modules/video_coding/utility/quality_scaler.h" | 10 #include "webrtc/modules/video_coding/utility/quality_scaler.h" |
11 | 11 |
12 namespace webrtc { | 12 namespace webrtc { |
13 | 13 |
14 static const int kMinFps = 5; | 14 static const int kMinFps = 10; |
15 static const int kMeasureSecondsDownscale = 3; | 15 static const int kMeasureSeconds = 5; |
16 // Threshold constant used until first downscale (to permit fast rampup). | |
17 static const int kMeasureSecondsFastUpscale = 2; | |
18 static const int kMeasureSecondsUpscale = 5; | |
19 static const int kFramedropPercentThreshold = 60; | 16 static const int kFramedropPercentThreshold = 60; |
20 static const int kHdResolutionThreshold = 700 * 500; | 17 static const int kHdResolutionThreshold = 700 * 500; |
21 static const int kHdBitrateThresholdKbps = 500; | 18 static const int kHdBitrateThresholdKbps = 500; |
22 | 19 |
23 const int QualityScaler::kDefaultLowQpDenominator = 3; | 20 const int QualityScaler::kDefaultLowQpDenominator = 3; |
24 // Note that this is the same for width and height to permit 120x90 in both | 21 // Note that this is the same for width and height to permit 120x90 in both |
25 // portrait and landscape mode. | 22 // portrait and landscape mode. |
26 const int QualityScaler::kDefaultMinDownscaleDimension = 90; | 23 const int QualityScaler::kDefaultMinDownscaleDimension = 90; |
27 | 24 |
28 QualityScaler::QualityScaler() | 25 QualityScaler::QualityScaler() |
29 : low_qp_threshold_(-1), | 26 : num_samples_(0), |
| 27 low_qp_threshold_(-1), |
| 28 downscale_shift_(0), |
30 framerate_down_(false), | 29 framerate_down_(false), |
31 min_width_(kDefaultMinDownscaleDimension), | 30 min_width_(kDefaultMinDownscaleDimension), |
32 min_height_(kDefaultMinDownscaleDimension) {} | 31 min_height_(kDefaultMinDownscaleDimension) {} |
33 | 32 |
34 void QualityScaler::Init(int low_qp_threshold, | 33 void QualityScaler::Init(int low_qp_threshold, |
35 int high_qp_threshold, | 34 int high_qp_threshold, |
36 bool use_framerate_reduction, | 35 bool use_framerate_reduction, |
37 int initial_bitrate_kbps, | 36 int initial_bitrate_kbps, |
38 int width, | 37 int width, |
39 int height, | 38 int height) { |
40 int fps) { | |
41 ClearSamples(); | 39 ClearSamples(); |
42 low_qp_threshold_ = low_qp_threshold; | 40 low_qp_threshold_ = low_qp_threshold; |
43 high_qp_threshold_ = high_qp_threshold; | 41 high_qp_threshold_ = high_qp_threshold; |
44 use_framerate_reduction_ = use_framerate_reduction; | 42 use_framerate_reduction_ = use_framerate_reduction; |
45 downscale_shift_ = 0; | 43 downscale_shift_ = 0; |
46 // Use a faster window for upscaling initially (but be more graceful later). | |
47 // This enables faster initial rampups without risking strong up-down | |
48 // behavior later. | |
49 measure_seconds_upscale_ = kMeasureSecondsFastUpscale; | |
50 const int init_width = width; | 44 const int init_width = width; |
51 const int init_height = height; | 45 const int init_height = height; |
52 // TODO(glaznev): Investigate using thresholds for other resolutions | 46 // TODO(glaznev): Investigate using thresholds for other resolutions |
53 // or threshold tables. | 47 // or threshold tables. |
54 if (initial_bitrate_kbps > 0 && | 48 if (initial_bitrate_kbps > 0 && |
55 initial_bitrate_kbps < kHdBitrateThresholdKbps) { | 49 initial_bitrate_kbps < kHdBitrateThresholdKbps) { |
56 // Start scaling to roughly VGA. | 50 // Start scaling to roughly VGA. |
57 while (width * height > kHdResolutionThreshold) { | 51 while (width * height > kHdResolutionThreshold) { |
58 ++downscale_shift_; | 52 ++downscale_shift_; |
59 width /= 2; | 53 width /= 2; |
60 height /= 2; | 54 height /= 2; |
61 } | 55 } |
62 } | 56 } |
63 UpdateTargetResolution(init_width, init_height); | 57 UpdateTargetResolution(init_width, init_height); |
64 ReportFramerate(fps); | |
65 target_framerate_ = -1; | 58 target_framerate_ = -1; |
66 } | 59 } |
67 | 60 |
68 void QualityScaler::SetMinResolution(int min_width, int min_height) { | 61 void QualityScaler::SetMinResolution(int min_width, int min_height) { |
69 min_width_ = min_width; | 62 min_width_ = min_width; |
70 min_height_ = min_height; | 63 min_height_ = min_height; |
71 } | 64 } |
72 | 65 |
73 // Report framerate(fps) to estimate # of samples. | 66 // Report framerate(fps) to estimate # of samples. |
74 void QualityScaler::ReportFramerate(int framerate) { | 67 void QualityScaler::ReportFramerate(int framerate) { |
| 68 num_samples_ = static_cast<size_t>( |
| 69 kMeasureSeconds * (framerate < kMinFps ? kMinFps : framerate)); |
75 framerate_ = framerate; | 70 framerate_ = framerate; |
76 UpdateSampleCounts(); | |
77 } | 71 } |
78 | 72 |
79 void QualityScaler::ReportQP(int qp) { | 73 void QualityScaler::ReportQP(int qp) { |
80 framedrop_percent_.AddSample(0); | 74 framedrop_percent_.AddSample(0); |
81 average_qp_downscale_.AddSample(qp); | 75 average_qp_.AddSample(qp); |
82 average_qp_upscale_.AddSample(qp); | |
83 } | 76 } |
84 | 77 |
85 void QualityScaler::ReportDroppedFrame() { | 78 void QualityScaler::ReportDroppedFrame() { |
86 framedrop_percent_.AddSample(100); | 79 framedrop_percent_.AddSample(100); |
87 } | 80 } |
88 | 81 |
89 void QualityScaler::OnEncodeFrame(const VideoFrame& frame) { | 82 void QualityScaler::OnEncodeFrame(const VideoFrame& frame) { |
90 // Should be set through InitEncode -> Should be set by now. | 83 // Should be set through InitEncode -> Should be set by now. |
91 assert(low_qp_threshold_ >= 0); | 84 assert(low_qp_threshold_ >= 0); |
92 assert(num_samples_upscale_ > 0); | 85 assert(num_samples_ > 0); |
93 assert(num_samples_downscale_ > 0); | |
94 | 86 |
95 // Update scale factor. | 87 // Update scale factor. |
96 int avg_drop = 0; | 88 int avg_drop = 0; |
97 int avg_qp = 0; | 89 int avg_qp = 0; |
98 | 90 |
99 // When encoder consistently overshoots, framerate reduction and spatial | 91 // When encoder consistently overshoots, framerate reduction and spatial |
100 // resizing will be triggered to get a smoother video. | 92 // resizing will be triggered to get a smoother video. |
101 if ((framedrop_percent_.GetAverage(num_samples_downscale_, &avg_drop) && | 93 if ((framedrop_percent_.GetAverage(num_samples_, &avg_drop) && |
102 avg_drop >= kFramedropPercentThreshold) || | 94 avg_drop >= kFramedropPercentThreshold) || |
103 (average_qp_downscale_.GetAverage(num_samples_downscale_, &avg_qp) && | 95 (average_qp_.GetAverage(num_samples_, &avg_qp) && |
104 avg_qp > high_qp_threshold_)) { | 96 avg_qp > high_qp_threshold_)) { |
105 // Reducing frame rate before spatial resolution change. | 97 // Reducing frame rate before spatial resolution change. |
106 // Reduce frame rate only when it is above a certain number. | 98 // Reduce frame rate only when it is above a certain number. |
107 // Only one reduction is allowed for now. | 99 // Only one reduction is allowed for now. |
108 // TODO(jackychen): Allow more than one framerate reduction. | 100 // TODO(jackychen): Allow more than one framerate reduction. |
109 if (use_framerate_reduction_ && !framerate_down_ && framerate_ >= 20) { | 101 if (use_framerate_reduction_ && !framerate_down_ && framerate_ >= 20) { |
110 target_framerate_ = framerate_ / 2; | 102 target_framerate_ = framerate_ / 2; |
111 framerate_down_ = true; | 103 framerate_down_ = true; |
112 // If frame rate has been updated, clear the buffer. We don't want | 104 // If frame rate has been updated, clear the buffer. We don't want |
113 // spatial resolution to change right after frame rate change. | 105 // spatial resolution to change right after frame rate change. |
114 ClearSamples(); | 106 ClearSamples(); |
115 } else { | 107 } else { |
116 AdjustScale(false); | 108 AdjustScale(false); |
117 } | 109 } |
118 } else if (average_qp_upscale_.GetAverage(num_samples_upscale_, &avg_qp) && | 110 } else if (average_qp_.GetAverage(num_samples_, &avg_qp) && |
119 avg_qp <= low_qp_threshold_) { | 111 avg_qp <= low_qp_threshold_) { |
120 if (use_framerate_reduction_ && framerate_down_) { | 112 if (use_framerate_reduction_ && framerate_down_) { |
121 target_framerate_ = -1; | 113 target_framerate_ = -1; |
122 framerate_down_ = false; | 114 framerate_down_ = false; |
123 ClearSamples(); | 115 ClearSamples(); |
124 } else { | 116 } else { |
125 AdjustScale(true); | 117 AdjustScale(true); |
126 } | 118 } |
127 } | 119 } |
128 UpdateTargetResolution(frame.width(), frame.height()); | 120 UpdateTargetResolution(frame.width(), frame.height()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 shift > 0 && (res_.width / 2 >= min_width_) && | 153 shift > 0 && (res_.width / 2 >= min_width_) && |
162 (res_.height / 2 >= min_height_); | 154 (res_.height / 2 >= min_height_); |
163 --shift) { | 155 --shift) { |
164 res_.width /= 2; | 156 res_.width /= 2; |
165 res_.height /= 2; | 157 res_.height /= 2; |
166 } | 158 } |
167 } | 159 } |
168 | 160 |
169 void QualityScaler::ClearSamples() { | 161 void QualityScaler::ClearSamples() { |
170 framedrop_percent_.Reset(); | 162 framedrop_percent_.Reset(); |
171 average_qp_downscale_.Reset(); | 163 average_qp_.Reset(); |
172 average_qp_upscale_.Reset(); | |
173 } | |
174 | |
175 void QualityScaler::UpdateSampleCounts() { | |
176 num_samples_downscale_ = static_cast<size_t>( | |
177 kMeasureSecondsDownscale * (framerate_ < kMinFps ? kMinFps : framerate_)); | |
178 num_samples_upscale_ = static_cast<size_t>( | |
179 measure_seconds_upscale_ * (framerate_ < kMinFps ? kMinFps : framerate_)); | |
180 } | 164 } |
181 | 165 |
182 void QualityScaler::AdjustScale(bool up) { | 166 void QualityScaler::AdjustScale(bool up) { |
183 downscale_shift_ += up ? -1 : 1; | 167 downscale_shift_ += up ? -1 : 1; |
184 if (downscale_shift_ < 0) | 168 if (downscale_shift_ < 0) |
185 downscale_shift_ = 0; | 169 downscale_shift_ = 0; |
186 if (!up) { | |
187 // Hit first downscale, start using a slower threshold for going up. | |
188 measure_seconds_upscale_ = kMeasureSecondsUpscale; | |
189 UpdateSampleCounts(); | |
190 } | |
191 ClearSamples(); | 170 ClearSamples(); |
192 } | 171 } |
193 | 172 |
194 } // namespace webrtc | 173 } // namespace webrtc |
OLD | NEW |