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