 Chromium Code Reviews
 Chromium Code Reviews Issue 2555483005:
  Add ability to scale to arbitrary factors  (Closed)
    
  
    Issue 2555483005:
  Add ability to scale to arbitrary factors  (Closed) 
  | 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 { | 
| 26 const int kResolutionRequiredDivisor = 4; | |
| 23 | 27 | 
| 24 struct Fraction { | 28 struct Fraction { | 
| 25 int numerator; | 29 int numerator; | 
| 26 int denominator; | 30 int denominator; | 
| 27 }; | 31 }; | 
| 28 | 32 | 
| 29 // Scale factors optimized for in libYUV that we accept. | 33 // Scale factors optimized for in libYUV that we accept. | 
| 30 // Must be sorted in decreasing scale factors for FindScaleLargerThan to work. | 34 // Must be sorted in decreasing scale factors for FindScaleLargerThan to work. | 
| 31 const Fraction kScaleFractions[] = { | 35 const Fraction kScaleFractions[] = { | 
| 32 {1, 1}, | 36 {1, 1}, | 
| 33 {3, 4}, | 37 {3, 4}, | 
| 34 {1, 2}, | 38 {1, 2}, | 
| 35 {3, 8}, | 39 {3, 8}, | 
| 36 {1, 4}, | 40 {1, 4}, | 
| 37 {3, 16}, | 41 {3, 16}, | 
| 38 }; | 42 }; | 
| 39 | 43 | 
| 40 // Round |valueToRound| to a multiple of |multiple|. Prefer rounding upwards, | 44 // Round |valueToRound| to a multiple of |multiple|. Prefer rounding upwards, | 
| 41 // but never more than |maxValue|. | 45 // but never more than |maxValue|. | 
| 42 int roundUp(int valueToRound, int multiple, int maxValue) { | 46 int roundUp(int valueToRound, int multiple, int maxValue) { | 
| 43 const int roundedValue = (valueToRound + multiple - 1) / multiple * multiple; | 47 const int roundedValue = (valueToRound + multiple - 1) / multiple * multiple; | 
| 44 return roundedValue <= maxValue ? roundedValue | 48 return roundedValue <= maxValue ? roundedValue | 
| 45 : (maxValue / multiple * multiple); | 49 : (maxValue / multiple * multiple); | 
| 46 } | 50 } | 
| 47 | 51 | 
| 48 Fraction FindScaleLessThanOrEqual(int input_num_pixels, int target_num_pixels) { | 52 Fraction FindScaleLessThanOrEqual(int input_num_pixels, int target_num_pixels) { | 
| 53 // Start searching from the last of the optimal fractions; | |
| 54 Fraction best_scale = kScaleFractions[arraysize(kScaleFractions) - 1]; | |
| 55 while (true) { | |
| 56 const float scale = | |
| 57 best_scale.numerator / static_cast<float>(best_scale.denominator); | |
| 58 float test_num_pixels = input_num_pixels * scale * scale; | |
| 59 float diff = target_num_pixels - test_num_pixels; | |
| 60 if (diff < 0) { | |
| 
magjed_webrtc
2016/12/06 16:35:08
nit: I think this logic is cleaner:
if (test_num_p
 
kthelgason
2016/12/07 12:35:25
Acknowledged.
 | |
| 61 if (best_scale.numerator == 1) { | |
| 62 best_scale.numerator = 3; | |
| 63 best_scale.denominator *= 4; | |
| 64 } else { | |
| 65 best_scale.numerator = 1; | |
| 66 best_scale.denominator /= 2; | |
| 67 } | |
| 68 } else { | |
| 69 return best_scale; | |
| 70 } | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 rtc::Optional<Fraction> FindOptimizedScaleLessThanOrEqual( | |
| 75 int input_num_pixels, | |
| 76 int target_num_pixels) { | |
| 49 float best_distance = std::numeric_limits<float>::max(); | 77 float best_distance = std::numeric_limits<float>::max(); | 
| 50 Fraction best_scale = {0, 1}; // Default to 0 if nothing matches. | 78 rtc::Optional<Fraction> best_scale; | 
| 51 for (const auto& fraction : kScaleFractions) { | 79 for (const auto& fraction : kScaleFractions) { | 
| 52 const float scale = | 80 const float scale = | 
| 53 fraction.numerator / static_cast<float>(fraction.denominator); | 81 fraction.numerator / static_cast<float>(fraction.denominator); | 
| 54 float test_num_pixels = input_num_pixels * scale * scale; | 82 float test_num_pixels = input_num_pixels * scale * scale; | 
| 55 float diff = target_num_pixels - test_num_pixels; | 83 float diff = target_num_pixels - test_num_pixels; | 
| 56 if (diff < 0) { | 84 if (diff < 0) { | 
| 57 continue; | 85 continue; | 
| 58 } | 86 } | 
| 59 if (diff < best_distance) { | 87 if (diff < best_distance) { | 
| 60 best_distance = diff; | 88 best_distance = diff; | 
| 61 best_scale = fraction; | 89 best_scale = rtc::Optional<Fraction>(fraction); | 
| 62 if (best_distance == 0) { // Found exact match. | 90 if (best_distance == 0) { // Found exact match. | 
| 63 break; | 91 break; | 
| 64 } | 92 } | 
| 65 } | 93 } | 
| 66 } | 94 } | 
| 67 return best_scale; | 95 return best_scale; | 
| 68 } | 96 } | 
| 69 | 97 | 
| 70 Fraction FindScaleLargerThan(int input_num_pixels, | 98 Fraction FindOptimizedScaleLargerThan(int input_num_pixels, | 
| 71 int target_num_pixels, | 99 int target_num_pixels, | 
| 72 int* resulting_number_of_pixels) { | 100 int* resulting_number_of_pixels) { | 
| 73 float best_distance = std::numeric_limits<float>::max(); | 101 float best_distance = std::numeric_limits<float>::max(); | 
| 74 Fraction best_scale = {1, 1}; // Default to unscaled if nothing matches. | 102 Fraction best_scale = {1, 1}; // Default to unscaled if nothing matches. | 
| 75 // Default to input number of pixels. | 103 // Default to input number of pixels. | 
| 76 float best_number_of_pixels = input_num_pixels; | 104 float best_number_of_pixels = input_num_pixels; | 
| 77 for (const auto& fraction : kScaleFractions) { | 105 for (const auto& fraction : kScaleFractions) { | 
| 78 const float scale = | 106 const float scale = | 
| 79 fraction.numerator / static_cast<float>(fraction.denominator); | 107 fraction.numerator / static_cast<float>(fraction.denominator); | 
| 80 float test_num_pixels = input_num_pixels * scale * scale; | 108 float test_num_pixels = input_num_pixels * scale * scale; | 
| 81 float diff = test_num_pixels - target_num_pixels; | 109 float diff = test_num_pixels - target_num_pixels; | 
| 82 if (diff <= 0) { | 110 if (diff <= 0) { | 
| 83 break; | 111 break; | 
| 84 } | 112 } | 
| 85 if (diff < best_distance) { | 113 if (diff < best_distance) { | 
| 86 best_distance = diff; | 114 best_distance = diff; | 
| 87 best_scale = fraction; | 115 best_scale = fraction; | 
| 88 best_number_of_pixels = test_num_pixels; | 116 best_number_of_pixels = test_num_pixels; | 
| 89 } | 117 } | 
| 90 } | 118 } | 
| 91 | 119 | 
| 92 *resulting_number_of_pixels = static_cast<int>(best_number_of_pixels + .5f); | 120 *resulting_number_of_pixels = static_cast<int>(best_number_of_pixels + .5f); | 
| 93 return best_scale; | 121 return best_scale; | 
| 94 } | 122 } | 
| 95 | 123 | 
| 124 rtc::Optional<Fraction> FindOptimizedScale(int input_num_pixels, | |
| 125 int max_pixel_count_step_up, | |
| 126 int max_pixel_count) { | |
| 127 // Try scale just above |max_pixel_count_step_up_|. | |
| 128 if (max_pixel_count_step_up > 0) { | |
| 129 int resulting_pixel_count; | |
| 130 const Fraction scale = FindOptimizedScaleLargerThan( | |
| 131 input_num_pixels, max_pixel_count_step_up, &resulting_pixel_count); | |
| 132 if (resulting_pixel_count <= max_pixel_count) | |
| 133 return rtc::Optional<Fraction>(scale); | |
| 134 } | |
| 135 // Return largest scale below |max_pixel_count|. | |
| 136 return FindOptimizedScaleLessThanOrEqual(input_num_pixels, max_pixel_count); | |
| 137 } | |
| 138 | |
| 96 Fraction FindScale(int input_num_pixels, | 139 Fraction FindScale(int input_num_pixels, | 
| 97 int max_pixel_count_step_up, | 140 int max_pixel_count_step_up, | 
| 98 int max_pixel_count) { | 141 int max_pixel_count) { | 
| 99 // Try scale just above |max_pixel_count_step_up_|. | 142 const rtc::Optional<Fraction> optimized_scale = FindOptimizedScale( | 
| 100 if (max_pixel_count_step_up > 0) { | 143 input_num_pixels, max_pixel_count_step_up, max_pixel_count); | 
| 101 int resulting_pixel_count; | 144 if (optimized_scale) | 
| 102 const Fraction scale = FindScaleLargerThan( | 145 return *optimized_scale; | 
| 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); | 146 return FindScaleLessThanOrEqual(input_num_pixels, max_pixel_count); | 
| 109 } | 147 } | 
| 110 | |
| 111 } // namespace | 148 } // namespace | 
| 112 | 149 | 
| 113 namespace cricket { | 150 namespace cricket { | 
| 114 | 151 | 
| 115 VideoAdapter::VideoAdapter() | 152 VideoAdapter::VideoAdapter() | 
| 116 : frames_in_(0), | 153 : frames_in_(0), | 
| 117 frames_out_(0), | 154 frames_out_(0), | 
| 118 frames_scaled_(0), | 155 frames_scaled_(0), | 
| 119 adaption_changes_(0), | 156 adaption_changes_(0), | 
| 120 previous_width_(0), | 157 previous_width_(0), | 
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 std::swap(requested_format_->width, requested_format_->height); | 241 std::swap(requested_format_->width, requested_format_->height); | 
| 205 } | 242 } | 
| 206 const float requested_aspect = | 243 const float requested_aspect = | 
| 207 requested_format_->width / | 244 requested_format_->width / | 
| 208 static_cast<float>(requested_format_->height); | 245 static_cast<float>(requested_format_->height); | 
| 209 *cropped_width = | 246 *cropped_width = | 
| 210 std::min(in_width, static_cast<int>(in_height * requested_aspect)); | 247 std::min(in_width, static_cast<int>(in_height * requested_aspect)); | 
| 211 *cropped_height = | 248 *cropped_height = | 
| 212 std::min(in_height, static_cast<int>(in_width / requested_aspect)); | 249 std::min(in_height, static_cast<int>(in_width / requested_aspect)); | 
| 213 } | 250 } | 
| 214 | |
| 215 // Find best scale factor. | |
| 216 const Fraction scale = | 251 const Fraction scale = | 
| 217 FindScale(*cropped_width * *cropped_height, | 252 FindScale(*cropped_width * *cropped_height, | 
| 218 resolution_request_max_pixel_count_step_up_, max_pixel_count); | 253 resolution_request_max_pixel_count_step_up_, max_pixel_count); | 
| 219 | |
| 220 // Adjust cropping slightly to get even integer output size and a perfect | 254 // Adjust cropping slightly to get even integer output size and a perfect | 
| 221 // scale factor. | 255 // scale factor. Make sure the resulting dimensions are a multiple of 4 | 
| 222 *cropped_width = roundUp(*cropped_width, scale.denominator, in_width); | 256 // to be nice to hardware encoders. | 
| 223 *cropped_height = roundUp(*cropped_height, scale.denominator, in_height); | 257 *cropped_width = roundUp( | 
| 224 RTC_DCHECK_EQ(0, *cropped_width % scale.denominator); | 258 *cropped_width, scale.denominator * kResolutionRequiredDivisor, in_width); | 
| 225 RTC_DCHECK_EQ(0, *cropped_height % scale.denominator); | 259 *cropped_height = | 
| 260 roundUp(*cropped_height, scale.denominator * kResolutionRequiredDivisor, | |
| 261 in_height); | |
| 262 RTC_DCHECK_EQ( | |
| 263 0, *cropped_width % scale.denominator * kResolutionRequiredDivisor); | |
| 264 RTC_DCHECK_EQ( | |
| 265 0, *cropped_height % scale.denominator * kResolutionRequiredDivisor); | |
| 226 | 266 | 
| 227 // Calculate final output size. | 267 // Calculate final output size. | 
| 228 *out_width = *cropped_width / scale.denominator * scale.numerator; | 268 *out_width = *cropped_width / scale.denominator * scale.numerator; | 
| 229 *out_height = *cropped_height / scale.denominator * scale.numerator; | 269 *out_height = *cropped_height / scale.denominator * scale.numerator; | 
| 230 | |
| 231 ++frames_out_; | 270 ++frames_out_; | 
| 232 if (scale.numerator != scale.denominator) | 271 if (scale.numerator != scale.denominator) | 
| 233 ++frames_scaled_; | 272 ++frames_scaled_; | 
| 234 | 273 | 
| 235 if (previous_width_ && (previous_width_ != *out_width || | 274 if (previous_width_ && (previous_width_ != *out_width || | 
| 236 previous_height_ != *out_height)) { | 275 previous_height_ != *out_height)) { | 
| 237 ++adaption_changes_; | 276 ++adaption_changes_; | 
| 238 LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ << " / out " | 277 LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ << " / out " | 
| 239 << frames_out_ << " / in " << frames_in_ | 278 << frames_out_ << " / in " << frames_in_ | 
| 240 << " Changes: " << adaption_changes_ << " Input: " << in_width | 279 << " Changes: " << adaption_changes_ << " Input: " << in_width | 
| (...skipping 19 matching lines...) Expand all Loading... | |
| 260 rtc::Optional<int> max_pixel_count, | 299 rtc::Optional<int> max_pixel_count, | 
| 261 rtc::Optional<int> max_pixel_count_step_up) { | 300 rtc::Optional<int> max_pixel_count_step_up) { | 
| 262 rtc::CritScope cs(&critical_section_); | 301 rtc::CritScope cs(&critical_section_); | 
| 263 resolution_request_max_pixel_count_ = | 302 resolution_request_max_pixel_count_ = | 
| 264 max_pixel_count.value_or(std::numeric_limits<int>::max()); | 303 max_pixel_count.value_or(std::numeric_limits<int>::max()); | 
| 265 resolution_request_max_pixel_count_step_up_ = | 304 resolution_request_max_pixel_count_step_up_ = | 
| 266 max_pixel_count_step_up.value_or(0); | 305 max_pixel_count_step_up.value_or(0); | 
| 267 } | 306 } | 
| 268 | 307 | 
| 269 } // namespace cricket | 308 } // namespace cricket | 
| OLD | NEW |