| 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 <cmath> |
| 15 #include <cstdlib> | 15 #include <cstdlib> |
| 16 #include <limits> | 16 #include <limits> |
| 17 | 17 |
| 18 #include "webrtc/base/arraysize.h" | 18 #include "webrtc/base/arraysize.h" |
| 19 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
| 20 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
| 21 #include "webrtc/base/optional.h" | 21 #include "webrtc/base/optional.h" |
| 22 #include "webrtc/media/base/mediaconstants.h" | 22 #include "webrtc/media/base/mediaconstants.h" |
| 23 #include "webrtc/media/base/videocommon.h" | 23 #include "webrtc/media/base/videocommon.h" |
| 24 #include "webrtc/media/base/videosourceinterface.h" |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 struct Fraction { | 27 struct Fraction { |
| 27 int numerator; | 28 uint32_t numerator; |
| 28 int denominator; | 29 uint32_t denominator; |
| 29 | 30 |
| 30 // Determines number of output pixels if both width and height of an input of | 31 // Determines number of output pixels if both width and height of an input of |
| 31 // |input_pixels| pixels is scaled with the fraction numerator / denominator. | 32 // |input_pixels| pixels is scaled with the fraction numerator / denominator. |
| 32 int scale_pixel_count(int input_pixels) { | 33 uint32_t scale_pixel_count(uint32_t input_pixels) { |
| 33 return (numerator * numerator * input_pixels) / (denominator * denominator); | 34 return (numerator * numerator * input_pixels) / (denominator * denominator); |
| 34 } | 35 } |
| 35 }; | 36 }; |
| 36 | 37 |
| 37 // Round |value_to_round| to a multiple of |multiple|. Prefer rounding upwards, | 38 // Round |value_to_round| to a multiple of |multiple|. Prefer rounding upwards, |
| 38 // but never more than |max_value|. | 39 // but never more than |max_value|. |
| 39 int roundUp(int value_to_round, int multiple, int max_value) { | 40 int roundUp(int value_to_round, int multiple, int max_value) { |
| 40 const int rounded_value = | 41 const int rounded_value = |
| 41 (value_to_round + multiple - 1) / multiple * multiple; | 42 (value_to_round + multiple - 1) / multiple * multiple; |
| 42 return rounded_value <= max_value ? rounded_value | 43 return rounded_value <= max_value ? rounded_value |
| 43 : (max_value / multiple * multiple); | 44 : (max_value / multiple * multiple); |
| 44 } | 45 } |
| 45 | 46 |
| 46 // Generates a scale factor that makes |input_pixels| close to |target_pixels|, | 47 // Generates a scale factor that makes |input_pixels| close to |target_pixels|, |
| 47 // but no higher than |max_pixels|. | 48 // but no higher than |max_pixels|. |
| 48 Fraction FindScale(int input_pixels, int target_pixels, int max_pixels) { | 49 Fraction FindScale(uint32_t input_pixels, |
| 50 uint32_t target_pixels, |
| 51 uint32_t max_pixels, |
| 52 uint32_t min_pixels) { |
| 49 // This function only makes sense for a positive target. | 53 // This function only makes sense for a positive target. |
| 50 RTC_DCHECK_GT(target_pixels, 0); | 54 RTC_DCHECK_GT(target_pixels, 0); |
| 51 RTC_DCHECK_GT(max_pixels, 0); | 55 RTC_DCHECK_GT(max_pixels, 0); |
| 52 RTC_DCHECK_GE(max_pixels, target_pixels); | 56 RTC_DCHECK_GE(max_pixels, target_pixels); |
| 53 | 57 |
| 54 // Don't scale up original. | 58 // Don't scale up original. |
| 55 if (target_pixels >= input_pixels) | 59 if (target_pixels >= input_pixels) |
| 56 return Fraction{1, 1}; | 60 return Fraction{1, 1}; |
| 57 | 61 |
| 58 Fraction current_scale = Fraction{1, 1}; | 62 Fraction current_scale = Fraction{1, 1}; |
| 59 Fraction best_scale = Fraction{1, 1}; | 63 Fraction best_scale = Fraction{1, 1}; |
| 60 // The minimum (absolute) difference between the number of output pixels and | 64 // The minimum (absolute) difference between the number of output pixels and |
| 61 // the target pixel count. | 65 // the target pixel count. |
| 62 int min_pixel_diff = std::numeric_limits<int>::max(); | 66 uint32_t min_pixel_diff = std::numeric_limits<uint32_t>::max(); |
| 63 if (input_pixels < max_pixels) { | 67 if (input_pixels < max_pixels) { |
| 64 // Start condition for 1/1 case, if it is less than max. | 68 // Start condition for 1/1 case, if it is less than max. |
| 65 min_pixel_diff = std::abs(input_pixels - target_pixels); | 69 min_pixel_diff = target_pixels > input_pixels |
| 70 ? target_pixels - input_pixels |
| 71 : input_pixels - target_pixels; |
| 66 } | 72 } |
| 67 | 73 |
| 68 // Alternately scale down by 2/3 and 3/4. This results in fractions which are | 74 // Alternately scale down by 2/3 and 3/4. This results in fractions which are |
| 69 // effectively scalable. For instance, starting at 1280x720 will result in | 75 // effectively scalable. For instance, starting at 1280x720 will result in |
| 70 // the series (3/4) => 960x540, (1/2) => 640x360, (3/8) => 480x270, | 76 // the series (3/4) => 960x540, (1/2) => 640x360, (3/8) => 480x270, |
| 71 // (1/4) => 320x180, (3/16) => 240x125, (1/8) => 160x90. | 77 // (1/4) => 320x180, (3/16) => 240x125, (1/8) => 160x90. |
| 72 while (current_scale.scale_pixel_count(input_pixels) > target_pixels) { | 78 while (current_scale.scale_pixel_count(input_pixels) > target_pixels) { |
| 73 if (current_scale.numerator % 3 == 0 && | 79 if (current_scale.numerator % 3 == 0 && |
| 74 current_scale.denominator % 2 == 0) { | 80 current_scale.denominator % 2 == 0) { |
| 75 // Multiply by 2/3. | 81 // Multiply by 2/3. |
| 76 current_scale.numerator /= 3; | 82 current_scale.numerator /= 3; |
| 77 current_scale.denominator /= 2; | 83 current_scale.denominator /= 2; |
| 78 } else { | 84 } else { |
| 79 // Multiply by 3/4. | 85 // Multiply by 3/4. |
| 80 current_scale.numerator *= 3; | 86 current_scale.numerator *= 3; |
| 81 current_scale.denominator *= 4; | 87 current_scale.denominator *= 4; |
| 82 } | 88 } |
| 83 | 89 |
| 84 int output_pixels = current_scale.scale_pixel_count(input_pixels); | 90 uint32_t output_pixels = current_scale.scale_pixel_count(input_pixels); |
| 91 if (output_pixels < min_pixels) { |
| 92 // Don't violate lower bound; |
| 93 break; |
| 94 } |
| 85 if (output_pixels <= max_pixels) { | 95 if (output_pixels <= max_pixels) { |
| 86 int diff = std::abs(target_pixels - output_pixels); | 96 uint32_t diff = target_pixels < output_pixels |
| 97 ? output_pixels - target_pixels |
| 98 : target_pixels - output_pixels; |
| 87 if (diff < min_pixel_diff) { | 99 if (diff < min_pixel_diff) { |
| 88 min_pixel_diff = diff; | 100 min_pixel_diff = diff; |
| 89 best_scale = current_scale; | 101 best_scale = current_scale; |
| 90 } | 102 } |
| 91 } | 103 } |
| 92 } | 104 } |
| 93 | 105 |
| 94 return best_scale; | 106 return best_scale; |
| 95 } | 107 } |
| 96 } // namespace | 108 } // namespace |
| 97 | 109 |
| 98 namespace cricket { | 110 namespace cricket { |
| 99 | 111 |
| 100 VideoAdapter::VideoAdapter(int required_resolution_alignment) | 112 VideoAdapter::VideoAdapter(int required_resolution_alignment) |
| 101 : frames_in_(0), | 113 : frames_in_(0), |
| 102 frames_out_(0), | 114 frames_out_(0), |
| 103 frames_scaled_(0), | 115 frames_scaled_(0), |
| 104 adaption_changes_(0), | 116 adaption_changes_(0), |
| 105 previous_width_(0), | 117 previous_width_(0), |
| 106 previous_height_(0), | 118 previous_height_(0), |
| 107 required_resolution_alignment_(required_resolution_alignment), | 119 required_resolution_alignment_(required_resolution_alignment) {} |
| 108 resolution_request_target_pixel_count_(std::numeric_limits<int>::max()), | |
| 109 resolution_request_max_pixel_count_(std::numeric_limits<int>::max()) {} | |
| 110 | 120 |
| 111 VideoAdapter::VideoAdapter() : VideoAdapter(1) {} | 121 VideoAdapter::VideoAdapter() : VideoAdapter(1) {} |
| 112 | 122 |
| 113 VideoAdapter::~VideoAdapter() {} | 123 VideoAdapter::~VideoAdapter() {} |
| 114 | 124 |
| 115 bool VideoAdapter::KeepFrame(int64_t in_timestamp_ns) { | 125 bool VideoAdapter::KeepFrame(int64_t in_timestamp_ns) { |
| 116 rtc::CritScope cs(&critical_section_); | 126 rtc::CritScope cs(&critical_section_); |
| 117 if (!requested_format_ || requested_format_->interval == 0) | 127 |
| 128 int64_t frame_interval_ns = 0; |
| 129 if (requested_format_ && requested_format_->interval) |
| 130 frame_interval_ns = requested_format_->interval; |
| 131 // TODO(sprang): Take target/min into consideration when implementing smoother |
| 132 // frame dropping. |
| 133 if (requested_framerate_fps_) { |
| 134 frame_interval_ns = std::max<int64_t>( |
| 135 frame_interval_ns, |
| 136 rtc::kNumNanosecsPerSec / requested_framerate_fps_->max); |
| 137 } |
| 138 |
| 139 if (frame_interval_ns <= 0) { |
| 140 // Frame rate throttling not enabled. |
| 118 return true; | 141 return true; |
| 142 } |
| 119 | 143 |
| 120 if (next_frame_timestamp_ns_) { | 144 if (next_frame_timestamp_ns_) { |
| 121 // Time until next frame should be outputted. | 145 // Time until next frame should be outputted. |
| 122 const int64_t time_until_next_frame_ns = | 146 const int64_t time_until_next_frame_ns = |
| 123 (*next_frame_timestamp_ns_ - in_timestamp_ns); | 147 (*next_frame_timestamp_ns_ - in_timestamp_ns); |
| 124 | 148 |
| 125 // Continue if timestamp is withing expected range. | 149 // Continue if timestamp is within expected range. |
| 126 if (std::abs(time_until_next_frame_ns) < 2 * requested_format_->interval) { | 150 if (std::abs(time_until_next_frame_ns) < 2 * frame_interval_ns) { |
| 127 // Drop if a frame shouldn't be outputted yet. | 151 // Drop if a frame shouldn't be outputted yet. |
| 128 if (time_until_next_frame_ns > 0) | 152 if (time_until_next_frame_ns > 0) |
| 129 return false; | 153 return false; |
| 130 // Time to output new frame. | 154 // Time to output new frame. |
| 131 *next_frame_timestamp_ns_ += requested_format_->interval; | 155 *next_frame_timestamp_ns_ += frame_interval_ns; |
| 132 return true; | 156 return true; |
| 133 } | 157 } |
| 134 } | 158 } |
| 135 | 159 |
| 136 // First timestamp received or timestamp is way outside expected range, so | 160 // First timestamp received or timestamp is way outside expected range, so |
| 137 // reset. Set first timestamp target to just half the interval to prefer | 161 // reset. Set first timestamp target to just half the interval to prefer |
| 138 // keeping frames in case of jitter. | 162 // keeping frames in case of jitter. |
| 139 next_frame_timestamp_ns_ = | 163 next_frame_timestamp_ns_ = |
| 140 rtc::Optional<int64_t>(in_timestamp_ns + requested_format_->interval / 2); | 164 rtc::Optional<int64_t>(in_timestamp_ns + frame_interval_ns / 2); |
| 141 return true; | 165 return true; |
| 142 } | 166 } |
| 143 | 167 |
| 144 bool VideoAdapter::AdaptFrameResolution(int in_width, | 168 bool VideoAdapter::AdaptFrameResolution(int in_width, |
| 145 int in_height, | 169 int in_height, |
| 146 int64_t in_timestamp_ns, | 170 int64_t in_timestamp_ns, |
| 147 int* cropped_width, | 171 int* cropped_width, |
| 148 int* cropped_height, | 172 int* cropped_height, |
| 149 int* out_width, | 173 int* out_width, |
| 150 int* out_height) { | 174 int* out_height) { |
| 151 rtc::CritScope cs(&critical_section_); | 175 rtc::CritScope cs(&critical_section_); |
| 152 ++frames_in_; | 176 ++frames_in_; |
| 153 | 177 |
| 154 // The max output pixel count is the minimum of the requests from | 178 // The max output pixel count is the minimum of the requests from |
| 155 // OnOutputFormatRequest and OnResolutionRequest. | 179 // OnOutputFormatRequest and OnResolutionRequest. |
| 156 int max_pixel_count = resolution_request_max_pixel_count_; | 180 |
| 181 rtc::VideoSinkWants::Range requested_range = |
| 182 requested_pixel_count_.value_or(rtc::VideoSinkWants::Range()); |
| 183 RTC_DCHECK_GE(requested_range.target, requested_range.min); |
| 184 RTC_DCHECK_GE(requested_range.max, requested_range.target); |
| 185 uint32_t max_pixel_count = requested_range.max; |
| 157 if (requested_format_) { | 186 if (requested_format_) { |
| 158 max_pixel_count = std::min( | 187 max_pixel_count = std::min( |
| 159 max_pixel_count, requested_format_->width * requested_format_->height); | 188 max_pixel_count, static_cast<uint32_t>(requested_format_->width * |
| 189 requested_format_->height)); |
| 160 } | 190 } |
| 161 int target_pixel_count = | 191 uint32_t target_pixel_count = |
| 162 std::min(resolution_request_target_pixel_count_, max_pixel_count); | 192 std::min(requested_range.target, max_pixel_count); |
| 163 | 193 |
| 164 // Drop the input frame if necessary. | 194 // Drop the input frame if necessary. |
| 165 if (max_pixel_count <= 0 || !KeepFrame(in_timestamp_ns)) { | 195 if (max_pixel_count <= 0 || !KeepFrame(in_timestamp_ns)) { |
| 166 // Show VAdapt log every 90 frames dropped. (3 seconds) | 196 // Show VAdapt log every 90 frames dropped. (3 seconds) |
| 167 if ((frames_in_ - frames_out_) % 90 == 0) { | 197 if ((frames_in_ - frames_out_) % 90 == 0) { |
| 168 // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed | 198 // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed |
| 169 // in default calls. | 199 // in default calls. |
| 170 LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_ | 200 LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_ |
| 171 << " / out " << frames_out_ | 201 << " / out " << frames_out_ |
| 172 << " / in " << frames_in_ | 202 << " / in " << frames_in_ |
| (...skipping 21 matching lines...) Expand all Loading... |
| 194 std::swap(requested_format_->width, requested_format_->height); | 224 std::swap(requested_format_->width, requested_format_->height); |
| 195 } | 225 } |
| 196 const float requested_aspect = | 226 const float requested_aspect = |
| 197 requested_format_->width / | 227 requested_format_->width / |
| 198 static_cast<float>(requested_format_->height); | 228 static_cast<float>(requested_format_->height); |
| 199 *cropped_width = | 229 *cropped_width = |
| 200 std::min(in_width, static_cast<int>(in_height * requested_aspect)); | 230 std::min(in_width, static_cast<int>(in_height * requested_aspect)); |
| 201 *cropped_height = | 231 *cropped_height = |
| 202 std::min(in_height, static_cast<int>(in_width / requested_aspect)); | 232 std::min(in_height, static_cast<int>(in_width / requested_aspect)); |
| 203 } | 233 } |
| 204 const Fraction scale = FindScale((*cropped_width) * (*cropped_height), | 234 const Fraction scale = |
| 205 target_pixel_count, max_pixel_count); | 235 FindScale((*cropped_width) * (*cropped_height), target_pixel_count, |
| 236 max_pixel_count, requested_range.min); |
| 206 // Adjust cropping slightly to get even integer output size and a perfect | 237 // Adjust cropping slightly to get even integer output size and a perfect |
| 207 // scale factor. Make sure the resulting dimensions are aligned correctly | 238 // scale factor. Make sure the resulting dimensions are aligned correctly |
| 208 // to be nice to hardware encoders. | 239 // to be nice to hardware encoders. |
| 209 *cropped_width = | 240 *cropped_width = |
| 210 roundUp(*cropped_width, | 241 roundUp(*cropped_width, |
| 211 scale.denominator * required_resolution_alignment_, in_width); | 242 scale.denominator * required_resolution_alignment_, in_width); |
| 212 *cropped_height = | 243 *cropped_height = |
| 213 roundUp(*cropped_height, | 244 roundUp(*cropped_height, |
| 214 scale.denominator * required_resolution_alignment_, in_height); | 245 scale.denominator * required_resolution_alignment_, in_height); |
| 215 RTC_DCHECK_EQ(0, *cropped_width % scale.denominator); | 246 RTC_DCHECK_EQ(0, *cropped_width % scale.denominator); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 242 | 273 |
| 243 return true; | 274 return true; |
| 244 } | 275 } |
| 245 | 276 |
| 246 void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) { | 277 void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) { |
| 247 rtc::CritScope cs(&critical_section_); | 278 rtc::CritScope cs(&critical_section_); |
| 248 requested_format_ = rtc::Optional<VideoFormat>(format); | 279 requested_format_ = rtc::Optional<VideoFormat>(format); |
| 249 next_frame_timestamp_ns_ = rtc::Optional<int64_t>(); | 280 next_frame_timestamp_ns_ = rtc::Optional<int64_t>(); |
| 250 } | 281 } |
| 251 | 282 |
| 252 void VideoAdapter::OnResolutionRequest( | 283 void VideoAdapter::OnResolutionFramerateRequest( |
| 253 const rtc::Optional<int>& target_pixel_count, | 284 const rtc::Optional<rtc::VideoSinkWants::Range>& pixel_count, |
| 254 const rtc::Optional<int>& max_pixel_count) { | 285 const rtc::Optional<rtc::VideoSinkWants::Range>& framerate_fps) { |
| 255 rtc::CritScope cs(&critical_section_); | 286 rtc::CritScope cs(&critical_section_); |
| 256 resolution_request_max_pixel_count_ = | 287 requested_pixel_count_ = pixel_count; |
| 257 max_pixel_count.value_or(std::numeric_limits<int>::max()); | 288 requested_framerate_fps_ = framerate_fps; |
| 258 resolution_request_target_pixel_count_ = | |
| 259 target_pixel_count.value_or(resolution_request_max_pixel_count_); | |
| 260 } | 289 } |
| 261 | 290 |
| 262 } // namespace cricket | 291 } // namespace cricket |
| OLD | NEW |