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 = 10; |
15 static const int kMeasureSeconds = 5; | 15 static const int kMeasureSeconds = 5; |
16 static const int kFramedropPercentThreshold = 60; | 16 static const int kFramedropPercentThreshold = 60; |
17 | 17 |
18 const int QualityScaler::kDefaultLowQpDenominator = 3; | 18 const int QualityScaler::kDefaultLowQpDenominator = 3; |
19 // Note that this is the same for width and height to permit 120x90 in both | 19 // Note that this is the same for width and height to permit 120x90 in both |
20 // portrait and landscape mode. | 20 // portrait and landscape mode. |
21 const int QualityScaler::kDefaultMinDownscaleDimension = 90; | 21 const int QualityScaler::kDefaultMinDownscaleDimension = 90; |
22 | 22 |
23 QualityScaler::QualityScaler() | 23 QualityScaler::QualityScaler() |
24 : num_samples_(0), | 24 : num_samples_(0), |
25 low_qp_threshold_(-1), | 25 low_qp_threshold_(-1), |
26 downscale_shift_(0), | 26 downscale_shift_(0), |
27 framerate_down_(false), | 27 framerate_down_(false), |
28 min_width_(kDefaultMinDownscaleDimension), | 28 min_width_(kDefaultMinDownscaleDimension), |
29 min_height_(kDefaultMinDownscaleDimension) { | 29 min_height_(kDefaultMinDownscaleDimension) {} |
30 } | |
31 | 30 |
32 void QualityScaler::Init(int low_qp_threshold, | 31 void QualityScaler::Init(int low_qp_threshold, |
33 int high_qp_threshold, | 32 int high_qp_threshold, |
34 bool use_framerate_reduction) { | 33 bool use_framerate_reduction) { |
35 ClearSamples(); | 34 ClearSamples(); |
36 low_qp_threshold_ = low_qp_threshold; | 35 low_qp_threshold_ = low_qp_threshold; |
37 high_qp_threshold_ = high_qp_threshold; | 36 high_qp_threshold_ = high_qp_threshold; |
38 use_framerate_reduction_ = use_framerate_reduction; | 37 use_framerate_reduction_ = use_framerate_reduction; |
39 target_framerate_ = -1; | 38 target_framerate_ = -1; |
40 } | 39 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 if (use_framerate_reduction_ && !framerate_down_ && framerate_ >= 20) { | 83 if (use_framerate_reduction_ && !framerate_down_ && framerate_ >= 20) { |
85 target_framerate_ = framerate_ / 2; | 84 target_framerate_ = framerate_ / 2; |
86 framerate_down_ = true; | 85 framerate_down_ = true; |
87 // If frame rate has been updated, clear the buffer. We don't want | 86 // If frame rate has been updated, clear the buffer. We don't want |
88 // spatial resolution to change right after frame rate change. | 87 // spatial resolution to change right after frame rate change. |
89 ClearSamples(); | 88 ClearSamples(); |
90 } else { | 89 } else { |
91 AdjustScale(false); | 90 AdjustScale(false); |
92 } | 91 } |
93 } else if (average_qp_.GetAverage(num_samples_, &avg_qp) && | 92 } else if (average_qp_.GetAverage(num_samples_, &avg_qp) && |
94 avg_qp <= low_qp_threshold_) { | 93 avg_qp <= low_qp_threshold_) { |
95 if (use_framerate_reduction_ && framerate_down_) { | 94 if (use_framerate_reduction_ && framerate_down_) { |
96 target_framerate_ = -1; | 95 target_framerate_ = -1; |
97 framerate_down_ = false; | 96 framerate_down_ = false; |
98 ClearSamples(); | 97 ClearSamples(); |
99 } else { | 98 } else { |
100 AdjustScale(true); | 99 AdjustScale(true); |
101 } | 100 } |
102 } | 101 } |
103 | 102 |
104 assert(downscale_shift_ >= 0); | 103 assert(downscale_shift_ >= 0); |
105 for (int shift = downscale_shift_; | 104 for (int shift = downscale_shift_; |
106 shift > 0 && (res_.width / 2 >= min_width_) && | 105 shift > 0 && (res_.width / 2 >= min_width_) && |
107 (res_.height / 2 >= min_height_); | 106 (res_.height / 2 >= min_height_); |
108 --shift) { | 107 --shift) { |
109 res_.width /= 2; | 108 res_.width /= 2; |
110 res_.height /= 2; | 109 res_.height /= 2; |
111 } | 110 } |
112 } | 111 } |
113 | 112 |
114 QualityScaler::Resolution QualityScaler::GetScaledResolution() const { | 113 QualityScaler::Resolution QualityScaler::GetScaledResolution() const { |
115 return res_; | 114 return res_; |
116 } | 115 } |
117 | 116 |
118 int QualityScaler::GetTargetFramerate() const { | 117 int QualityScaler::GetTargetFramerate() const { |
119 return target_framerate_; | 118 return target_framerate_; |
120 } | 119 } |
121 | 120 |
122 const VideoFrame& QualityScaler::GetScaledFrame(const VideoFrame& frame) { | 121 const VideoFrame& QualityScaler::GetScaledFrame(const VideoFrame& frame) { |
123 Resolution res = GetScaledResolution(); | 122 Resolution res = GetScaledResolution(); |
124 if (res.width == frame.width()) | 123 if (res.width == frame.width()) |
125 return frame; | 124 return frame; |
126 | 125 |
127 scaler_.Set(frame.width(), | 126 scaler_.Set(frame.width(), frame.height(), res.width, res.height, kI420, |
128 frame.height(), | 127 kI420, kScaleBox); |
129 res.width, | |
130 res.height, | |
131 kI420, | |
132 kI420, | |
133 kScaleBox); | |
134 if (scaler_.Scale(frame, &scaled_frame_) != 0) | 128 if (scaler_.Scale(frame, &scaled_frame_) != 0) |
135 return frame; | 129 return frame; |
136 | 130 |
137 scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms()); | 131 scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms()); |
138 scaled_frame_.set_timestamp(frame.timestamp()); | 132 scaled_frame_.set_timestamp(frame.timestamp()); |
139 scaled_frame_.set_render_time_ms(frame.render_time_ms()); | 133 scaled_frame_.set_render_time_ms(frame.render_time_ms()); |
140 | 134 |
141 return scaled_frame_; | 135 return scaled_frame_; |
142 } | 136 } |
143 | 137 |
144 void QualityScaler::ClearSamples() { | 138 void QualityScaler::ClearSamples() { |
145 framedrop_percent_.Reset(); | 139 framedrop_percent_.Reset(); |
146 average_qp_.Reset(); | 140 average_qp_.Reset(); |
147 } | 141 } |
148 | 142 |
149 void QualityScaler::AdjustScale(bool up) { | 143 void QualityScaler::AdjustScale(bool up) { |
150 downscale_shift_ += up ? -1 : 1; | 144 downscale_shift_ += up ? -1 : 1; |
151 if (downscale_shift_ < 0) | 145 if (downscale_shift_ < 0) |
152 downscale_shift_ = 0; | 146 downscale_shift_ = 0; |
153 ClearSamples(); | 147 ClearSamples(); |
154 } | 148 } |
155 | 149 |
156 } // namespace webrtc | 150 } // namespace webrtc |
OLD | NEW |