OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2010 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/media/base/videoadapter.h" | 11 #include "webrtc/media/base/videoadapter.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <cmath> |
14 #include <cstdlib> | 15 #include <cstdlib> |
15 #include <limits> | 16 #include <limits> |
16 | 17 |
| 18 #include "webrtc/base/arraysize.h" |
17 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
18 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
| 21 #include "webrtc/base/optional.h" |
19 #include "webrtc/media/base/mediaconstants.h" | 22 #include "webrtc/media/base/mediaconstants.h" |
20 #include "webrtc/media/base/videocommon.h" | 23 #include "webrtc/media/base/videocommon.h" |
21 | 24 |
22 namespace { | 25 namespace { |
23 | |
24 struct Fraction { | 26 struct Fraction { |
25 int numerator; | 27 int numerator; |
26 int denominator; | 28 int denominator; |
27 }; | 29 }; |
28 | 30 |
29 // Scale factors optimized for in libYUV that we accept. | 31 // Round |value_to_round| to a multiple of |multiple|. Prefer rounding upwards, |
30 // Must be sorted in decreasing scale factors for FindScaleLargerThan to work. | 32 // but never more than |max_value|. |
31 const Fraction kScaleFractions[] = { | 33 int roundUp(int value_to_round, int multiple, int max_value) { |
32 {1, 1}, | 34 const int rounded_value = |
33 {3, 4}, | 35 (value_to_round + multiple - 1) / multiple * multiple; |
34 {1, 2}, | 36 return rounded_value <= max_value ? rounded_value |
35 {3, 8}, | 37 : (max_value / multiple * multiple); |
36 {1, 4}, | |
37 {3, 16}, | |
38 }; | |
39 | |
40 // Round |valueToRound| to a multiple of |multiple|. Prefer rounding upwards, | |
41 // but never more than |maxValue|. | |
42 int roundUp(int valueToRound, int multiple, int maxValue) { | |
43 const int roundedValue = (valueToRound + multiple - 1) / multiple * multiple; | |
44 return roundedValue <= maxValue ? roundedValue | |
45 : (maxValue / multiple * multiple); | |
46 } | 38 } |
47 | 39 |
48 Fraction FindScaleLessThanOrEqual(int input_num_pixels, int target_num_pixels) { | 40 // Generates a scale factor that makes |input_num_pixels| smaller or |
49 float best_distance = std::numeric_limits<float>::max(); | 41 // larger than |target_num_pixels|, depending on the value of |step_up|. |
50 Fraction best_scale = {0, 1}; // Default to 0 if nothing matches. | 42 Fraction FindScale(int input_num_pixels, int target_num_pixels, bool step_up) { |
51 for (const auto& fraction : kScaleFractions) { | 43 // This function only makes sense for a positive target. |
52 const float scale = | 44 RTC_DCHECK_GT(target_num_pixels, 0); |
53 fraction.numerator / static_cast<float>(fraction.denominator); | 45 Fraction best_scale = Fraction{1, 1}; |
54 float test_num_pixels = input_num_pixels * scale * scale; | 46 Fraction last_scale = Fraction{1, 1}; |
55 float diff = target_num_pixels - test_num_pixels; | 47 const float target_scale = |
56 if (diff < 0) { | 48 sqrt(target_num_pixels / static_cast<float>(input_num_pixels)); |
57 continue; | 49 while (best_scale.numerator > (target_scale * best_scale.denominator)) { |
58 } | 50 last_scale = best_scale; |
59 if (diff < best_distance) { | 51 if (best_scale.numerator % 3 == 0 && best_scale.denominator % 2 == 0) { |
60 best_distance = diff; | 52 // Multiply by 2/3 |
61 best_scale = fraction; | 53 best_scale.numerator /= 3; |
62 if (best_distance == 0) { // Found exact match. | 54 best_scale.denominator /= 2; |
63 break; | 55 } else { |
64 } | 56 // Multiply by 3/4 |
| 57 best_scale.numerator *= 3; |
| 58 best_scale.denominator *= 4; |
65 } | 59 } |
66 } | 60 } |
| 61 if (step_up) |
| 62 return last_scale; |
67 return best_scale; | 63 return best_scale; |
68 } | 64 } |
69 | |
70 Fraction FindScaleLargerThan(int input_num_pixels, | |
71 int target_num_pixels, | |
72 int* resulting_number_of_pixels) { | |
73 float best_distance = std::numeric_limits<float>::max(); | |
74 Fraction best_scale = {1, 1}; // Default to unscaled if nothing matches. | |
75 // Default to input number of pixels. | |
76 float best_number_of_pixels = input_num_pixels; | |
77 for (const auto& fraction : kScaleFractions) { | |
78 const float scale = | |
79 fraction.numerator / static_cast<float>(fraction.denominator); | |
80 float test_num_pixels = input_num_pixels * scale * scale; | |
81 float diff = test_num_pixels - target_num_pixels; | |
82 if (diff <= 0) { | |
83 break; | |
84 } | |
85 if (diff < best_distance) { | |
86 best_distance = diff; | |
87 best_scale = fraction; | |
88 best_number_of_pixels = test_num_pixels; | |
89 } | |
90 } | |
91 | |
92 *resulting_number_of_pixels = static_cast<int>(best_number_of_pixels + .5f); | |
93 return best_scale; | |
94 } | |
95 | |
96 Fraction FindScale(int input_num_pixels, | |
97 int max_pixel_count_step_up, | |
98 int max_pixel_count) { | |
99 // Try scale just above |max_pixel_count_step_up_|. | |
100 if (max_pixel_count_step_up > 0) { | |
101 int resulting_pixel_count; | |
102 const Fraction scale = FindScaleLargerThan( | |
103 input_num_pixels, max_pixel_count_step_up, &resulting_pixel_count); | |
104 if (resulting_pixel_count <= max_pixel_count) | |
105 return scale; | |
106 } | |
107 // Return largest scale below |max_pixel_count|. | |
108 return FindScaleLessThanOrEqual(input_num_pixels, max_pixel_count); | |
109 } | |
110 | |
111 } // namespace | 65 } // namespace |
112 | 66 |
113 namespace cricket { | 67 namespace cricket { |
114 | 68 |
115 VideoAdapter::VideoAdapter() | 69 VideoAdapter::VideoAdapter(int required_resolution_alignment) |
116 : frames_in_(0), | 70 : frames_in_(0), |
117 frames_out_(0), | 71 frames_out_(0), |
118 frames_scaled_(0), | 72 frames_scaled_(0), |
119 adaption_changes_(0), | 73 adaption_changes_(0), |
120 previous_width_(0), | 74 previous_width_(0), |
121 previous_height_(0), | 75 previous_height_(0), |
| 76 required_resolution_alignment_(required_resolution_alignment), |
122 resolution_request_max_pixel_count_(std::numeric_limits<int>::max()), | 77 resolution_request_max_pixel_count_(std::numeric_limits<int>::max()), |
123 resolution_request_max_pixel_count_step_up_(0) {} | 78 step_up_(false) {} |
| 79 |
| 80 VideoAdapter::VideoAdapter() : VideoAdapter(1) {} |
124 | 81 |
125 VideoAdapter::~VideoAdapter() {} | 82 VideoAdapter::~VideoAdapter() {} |
126 | 83 |
127 bool VideoAdapter::KeepFrame(int64_t in_timestamp_ns) { | 84 bool VideoAdapter::KeepFrame(int64_t in_timestamp_ns) { |
128 rtc::CritScope cs(&critical_section_); | 85 rtc::CritScope cs(&critical_section_); |
129 if (!requested_format_ || requested_format_->interval == 0) | 86 if (!requested_format_ || requested_format_->interval == 0) |
130 return true; | 87 return true; |
131 | 88 |
132 if (next_frame_timestamp_ns_) { | 89 if (next_frame_timestamp_ns_) { |
133 // Time until next frame should be outputted. | 90 // Time until next frame should be outputted. |
(...skipping 26 matching lines...) Expand all Loading... |
160 int* cropped_height, | 117 int* cropped_height, |
161 int* out_width, | 118 int* out_width, |
162 int* out_height) { | 119 int* out_height) { |
163 rtc::CritScope cs(&critical_section_); | 120 rtc::CritScope cs(&critical_section_); |
164 ++frames_in_; | 121 ++frames_in_; |
165 | 122 |
166 // The max output pixel count is the minimum of the requests from | 123 // The max output pixel count is the minimum of the requests from |
167 // OnOutputFormatRequest and OnResolutionRequest. | 124 // OnOutputFormatRequest and OnResolutionRequest. |
168 int max_pixel_count = resolution_request_max_pixel_count_; | 125 int max_pixel_count = resolution_request_max_pixel_count_; |
169 if (requested_format_) { | 126 if (requested_format_) { |
| 127 // TODO(kthelgason): remove the - |step_up_| hack when we change how |
| 128 // resolution is requested from VideoSourceProxy. |
| 129 // This is required because we must not scale above the requested |
| 130 // format so we subtract one when scaling up. |
170 max_pixel_count = std::min( | 131 max_pixel_count = std::min( |
171 max_pixel_count, requested_format_->width * requested_format_->height); | 132 max_pixel_count, requested_format_->width * requested_format_->height - |
| 133 static_cast<int>(step_up_)); |
172 } | 134 } |
173 | 135 |
174 // Drop the input frame if necessary. | 136 // Drop the input frame if necessary. |
175 if (max_pixel_count == 0 || !KeepFrame(in_timestamp_ns)) { | 137 if (max_pixel_count <= 0 || !KeepFrame(in_timestamp_ns)) { |
176 // Show VAdapt log every 90 frames dropped. (3 seconds) | 138 // Show VAdapt log every 90 frames dropped. (3 seconds) |
177 if ((frames_in_ - frames_out_) % 90 == 0) { | 139 if ((frames_in_ - frames_out_) % 90 == 0) { |
178 // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed | 140 // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed |
179 // in default calls. | 141 // in default calls. |
180 LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_ | 142 LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_ |
181 << " / out " << frames_out_ | 143 << " / out " << frames_out_ |
182 << " / in " << frames_in_ | 144 << " / in " << frames_in_ |
183 << " Changes: " << adaption_changes_ | 145 << " Changes: " << adaption_changes_ |
184 << " Input: " << in_width | 146 << " Input: " << in_width |
185 << "x" << in_height | 147 << "x" << in_height |
(...skipping 18 matching lines...) Expand all Loading... |
204 std::swap(requested_format_->width, requested_format_->height); | 166 std::swap(requested_format_->width, requested_format_->height); |
205 } | 167 } |
206 const float requested_aspect = | 168 const float requested_aspect = |
207 requested_format_->width / | 169 requested_format_->width / |
208 static_cast<float>(requested_format_->height); | 170 static_cast<float>(requested_format_->height); |
209 *cropped_width = | 171 *cropped_width = |
210 std::min(in_width, static_cast<int>(in_height * requested_aspect)); | 172 std::min(in_width, static_cast<int>(in_height * requested_aspect)); |
211 *cropped_height = | 173 *cropped_height = |
212 std::min(in_height, static_cast<int>(in_width / requested_aspect)); | 174 std::min(in_height, static_cast<int>(in_width / requested_aspect)); |
213 } | 175 } |
214 | |
215 // Find best scale factor. | |
216 const Fraction scale = | 176 const Fraction scale = |
217 FindScale(*cropped_width * *cropped_height, | 177 FindScale(*cropped_width * *cropped_height, max_pixel_count, step_up_); |
218 resolution_request_max_pixel_count_step_up_, max_pixel_count); | |
219 | |
220 // Adjust cropping slightly to get even integer output size and a perfect | 178 // Adjust cropping slightly to get even integer output size and a perfect |
221 // scale factor. | 179 // scale factor. Make sure the resulting dimensions are aligned correctly |
222 *cropped_width = roundUp(*cropped_width, scale.denominator, in_width); | 180 // to be nice to hardware encoders. |
223 *cropped_height = roundUp(*cropped_height, scale.denominator, in_height); | 181 *cropped_width = |
| 182 roundUp(*cropped_width, |
| 183 scale.denominator * required_resolution_alignment_, in_width); |
| 184 *cropped_height = |
| 185 roundUp(*cropped_height, |
| 186 scale.denominator * required_resolution_alignment_, in_height); |
224 RTC_DCHECK_EQ(0, *cropped_width % scale.denominator); | 187 RTC_DCHECK_EQ(0, *cropped_width % scale.denominator); |
225 RTC_DCHECK_EQ(0, *cropped_height % scale.denominator); | 188 RTC_DCHECK_EQ(0, *cropped_height % scale.denominator); |
226 | 189 |
227 // Calculate final output size. | 190 // Calculate final output size. |
228 *out_width = *cropped_width / scale.denominator * scale.numerator; | 191 *out_width = *cropped_width / scale.denominator * scale.numerator; |
229 *out_height = *cropped_height / scale.denominator * scale.numerator; | 192 *out_height = *cropped_height / scale.denominator * scale.numerator; |
| 193 RTC_DCHECK_EQ(0, *out_height % required_resolution_alignment_); |
| 194 RTC_DCHECK_EQ(0, *out_height % required_resolution_alignment_); |
230 | 195 |
231 ++frames_out_; | 196 ++frames_out_; |
232 if (scale.numerator != scale.denominator) | 197 if (scale.numerator != scale.denominator) |
233 ++frames_scaled_; | 198 ++frames_scaled_; |
234 | 199 |
235 if (previous_width_ && (previous_width_ != *out_width || | 200 if (previous_width_ && (previous_width_ != *out_width || |
236 previous_height_ != *out_height)) { | 201 previous_height_ != *out_height)) { |
237 ++adaption_changes_; | 202 ++adaption_changes_; |
238 LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ << " / out " | 203 LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ << " / out " |
239 << frames_out_ << " / in " << frames_in_ | 204 << frames_out_ << " / in " << frames_in_ |
(...skipping 13 matching lines...) Expand all Loading... |
253 void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) { | 218 void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) { |
254 rtc::CritScope cs(&critical_section_); | 219 rtc::CritScope cs(&critical_section_); |
255 requested_format_ = rtc::Optional<VideoFormat>(format); | 220 requested_format_ = rtc::Optional<VideoFormat>(format); |
256 next_frame_timestamp_ns_ = rtc::Optional<int64_t>(); | 221 next_frame_timestamp_ns_ = rtc::Optional<int64_t>(); |
257 } | 222 } |
258 | 223 |
259 void VideoAdapter::OnResolutionRequest( | 224 void VideoAdapter::OnResolutionRequest( |
260 rtc::Optional<int> max_pixel_count, | 225 rtc::Optional<int> max_pixel_count, |
261 rtc::Optional<int> max_pixel_count_step_up) { | 226 rtc::Optional<int> max_pixel_count_step_up) { |
262 rtc::CritScope cs(&critical_section_); | 227 rtc::CritScope cs(&critical_section_); |
263 resolution_request_max_pixel_count_ = | 228 resolution_request_max_pixel_count_ = max_pixel_count.value_or( |
264 max_pixel_count.value_or(std::numeric_limits<int>::max()); | 229 max_pixel_count_step_up.value_or(std::numeric_limits<int>::max())); |
265 resolution_request_max_pixel_count_step_up_ = | 230 step_up_ = static_cast<bool>(max_pixel_count_step_up); |
266 max_pixel_count_step_up.value_or(0); | |
267 } | 231 } |
268 | 232 |
269 } // namespace cricket | 233 } // namespace cricket |
OLD | NEW |