Chromium Code Reviews| 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 int numerator; |
| 28 int denominator; | 29 int 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 int scale_pixel_count(int 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(int input_pixels, |
| 50 int target_pixels, | |
| 51 int max_pixels, | |
| 52 int 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}; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 75 // Multiply by 2/3. | 79 // Multiply by 2/3. |
| 76 current_scale.numerator /= 3; | 80 current_scale.numerator /= 3; |
| 77 current_scale.denominator /= 2; | 81 current_scale.denominator /= 2; |
| 78 } else { | 82 } else { |
| 79 // Multiply by 3/4. | 83 // Multiply by 3/4. |
| 80 current_scale.numerator *= 3; | 84 current_scale.numerator *= 3; |
| 81 current_scale.denominator *= 4; | 85 current_scale.denominator *= 4; |
| 82 } | 86 } |
| 83 | 87 |
| 84 int output_pixels = current_scale.scale_pixel_count(input_pixels); | 88 int output_pixels = current_scale.scale_pixel_count(input_pixels); |
| 89 if (output_pixels < min_pixels) { | |
| 90 // Don't violate lower bound; | |
| 91 break; | |
| 92 } | |
| 85 if (output_pixels <= max_pixels) { | 93 if (output_pixels <= max_pixels) { |
| 86 int diff = std::abs(target_pixels - output_pixels); | 94 int diff = std::abs(target_pixels - output_pixels); |
| 87 if (diff < min_pixel_diff) { | 95 if (diff < min_pixel_diff) { |
| 88 min_pixel_diff = diff; | 96 min_pixel_diff = diff; |
| 89 best_scale = current_scale; | 97 best_scale = current_scale; |
| 90 } | 98 } |
| 91 } | 99 } |
| 92 } | 100 } |
| 93 | 101 |
| 94 return best_scale; | 102 return best_scale; |
| 95 } | 103 } |
| 96 } // namespace | 104 } // namespace |
| 97 | 105 |
| 98 namespace cricket { | 106 namespace cricket { |
| 99 | 107 |
| 100 VideoAdapter::VideoAdapter(int required_resolution_alignment) | 108 VideoAdapter::VideoAdapter(int required_resolution_alignment) |
| 101 : frames_in_(0), | 109 : frames_in_(0), |
| 102 frames_out_(0), | 110 frames_out_(0), |
| 103 frames_scaled_(0), | 111 frames_scaled_(0), |
| 104 adaption_changes_(0), | 112 adaption_changes_(0), |
| 105 previous_width_(0), | 113 previous_width_(0), |
| 106 previous_height_(0), | 114 previous_height_(0), |
| 107 required_resolution_alignment_(required_resolution_alignment), | 115 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 | 116 |
| 111 VideoAdapter::VideoAdapter() : VideoAdapter(1) {} | 117 VideoAdapter::VideoAdapter() : VideoAdapter(1) {} |
| 112 | 118 |
| 113 VideoAdapter::~VideoAdapter() {} | 119 VideoAdapter::~VideoAdapter() {} |
| 114 | 120 |
| 115 bool VideoAdapter::KeepFrame(int64_t in_timestamp_ns) { | 121 bool VideoAdapter::KeepFrame(int64_t in_timestamp_ns) { |
| 116 rtc::CritScope cs(&critical_section_); | 122 rtc::CritScope cs(&critical_section_); |
| 117 if (!requested_format_ || requested_format_->interval == 0) | 123 |
| 124 int64_t frame_interval_ns = 0; | |
| 125 if (requested_format_ && requested_format_->interval) | |
| 126 frame_interval_ns = requested_format_->interval; | |
| 127 // TODO(sprang): Take target/min into consideration when implementing smoother | |
| 128 // frame dropping. | |
| 129 if (requested_sink_wants_.framerate_fps_) { | |
| 130 frame_interval_ns = std::max<int64_t>( | |
| 131 frame_interval_ns, | |
| 132 rtc::kNumNanosecsPerSec / requested_sink_wants_.framerate_fps_->max); | |
| 133 } | |
| 134 | |
| 135 if (frame_interval_ns <= 0) { | |
| 136 // Frame rate throttling not enabled. | |
| 118 return true; | 137 return true; |
| 138 } | |
| 119 | 139 |
| 120 if (next_frame_timestamp_ns_) { | 140 if (next_frame_timestamp_ns_) { |
| 121 // Time until next frame should be outputted. | 141 // Time until next frame should be outputted. |
| 122 const int64_t time_until_next_frame_ns = | 142 const int64_t time_until_next_frame_ns = |
| 123 (*next_frame_timestamp_ns_ - in_timestamp_ns); | 143 (*next_frame_timestamp_ns_ - in_timestamp_ns); |
| 124 | 144 |
| 125 // Continue if timestamp is withing expected range. | 145 // Continue if timestamp is within expected range. |
| 126 if (std::abs(time_until_next_frame_ns) < 2 * requested_format_->interval) { | 146 if (std::abs(time_until_next_frame_ns) < 2 * frame_interval_ns) { |
| 127 // Drop if a frame shouldn't be outputted yet. | 147 // Drop if a frame shouldn't be outputted yet. |
| 128 if (time_until_next_frame_ns > 0) | 148 if (time_until_next_frame_ns > 0) |
| 129 return false; | 149 return false; |
| 130 // Time to output new frame. | 150 // Time to output new frame. |
| 131 *next_frame_timestamp_ns_ += requested_format_->interval; | 151 *next_frame_timestamp_ns_ += frame_interval_ns; |
| 132 return true; | 152 return true; |
| 133 } | 153 } |
| 134 } | 154 } |
| 135 | 155 |
| 136 // First timestamp received or timestamp is way outside expected range, so | 156 // First timestamp received or timestamp is way outside expected range, so |
| 137 // reset. Set first timestamp target to just half the interval to prefer | 157 // reset. Set first timestamp target to just half the interval to prefer |
| 138 // keeping frames in case of jitter. | 158 // keeping frames in case of jitter. |
| 139 next_frame_timestamp_ns_ = | 159 next_frame_timestamp_ns_ = |
| 140 rtc::Optional<int64_t>(in_timestamp_ns + requested_format_->interval / 2); | 160 rtc::Optional<int64_t>(in_timestamp_ns + frame_interval_ns / 2); |
| 141 return true; | 161 return true; |
| 142 } | 162 } |
| 143 | 163 |
| 144 bool VideoAdapter::AdaptFrameResolution(int in_width, | 164 bool VideoAdapter::AdaptFrameResolution(int in_width, |
| 145 int in_height, | 165 int in_height, |
| 146 int64_t in_timestamp_ns, | 166 int64_t in_timestamp_ns, |
| 147 int* cropped_width, | 167 int* cropped_width, |
| 148 int* cropped_height, | 168 int* cropped_height, |
| 149 int* out_width, | 169 int* out_width, |
| 150 int* out_height) { | 170 int* out_height) { |
| 151 rtc::CritScope cs(&critical_section_); | 171 rtc::CritScope cs(&critical_section_); |
| 152 ++frames_in_; | 172 ++frames_in_; |
| 153 | 173 |
| 154 // The max output pixel count is the minimum of the requests from | 174 // The max output pixel count is the minimum of the requests from |
| 155 // OnOutputFormatRequest and OnResolutionRequest. | 175 // OnOutputFormatRequest and OnResolutionRequest. |
| 156 int max_pixel_count = resolution_request_max_pixel_count_; | 176 |
| 177 rtc::VideoSinkWants::ValueRange requested_range = | |
| 178 requested_sink_wants_.pixel_count.value_or( | |
| 179 rtc::VideoSinkWants::ValueRange()); | |
| 180 RTC_DCHECK_GE(requested_range.target, requested_range.min); | |
| 181 RTC_DCHECK_GE(requested_range.max, requested_range.target); | |
| 182 uint32_t max_pixel_count = requested_range.max; | |
| 157 if (requested_format_) { | 183 if (requested_format_) { |
| 158 max_pixel_count = std::min( | 184 max_pixel_count = std::min( |
| 159 max_pixel_count, requested_format_->width * requested_format_->height); | 185 max_pixel_count, static_cast<uint32_t>(requested_format_->width * |
| 186 requested_format_->height)); | |
| 160 } | 187 } |
| 161 int target_pixel_count = | 188 int target_pixel_count = std::min(requested_range.target, max_pixel_count); |
| 162 std::min(resolution_request_target_pixel_count_, max_pixel_count); | |
| 163 | 189 |
| 164 // Drop the input frame if necessary. | 190 // Drop the input frame if necessary. |
| 165 if (max_pixel_count <= 0 || !KeepFrame(in_timestamp_ns)) { | 191 if (max_pixel_count <= 0 || !KeepFrame(in_timestamp_ns)) { |
| 166 // Show VAdapt log every 90 frames dropped. (3 seconds) | 192 // Show VAdapt log every 90 frames dropped. (3 seconds) |
| 167 if ((frames_in_ - frames_out_) % 90 == 0) { | 193 if ((frames_in_ - frames_out_) % 90 == 0) { |
| 168 // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed | 194 // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed |
| 169 // in default calls. | 195 // in default calls. |
| 170 LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_ | 196 LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_ |
| 171 << " / out " << frames_out_ | 197 << " / out " << frames_out_ |
| 172 << " / in " << frames_in_ | 198 << " / in " << frames_in_ |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 194 std::swap(requested_format_->width, requested_format_->height); | 220 std::swap(requested_format_->width, requested_format_->height); |
| 195 } | 221 } |
| 196 const float requested_aspect = | 222 const float requested_aspect = |
| 197 requested_format_->width / | 223 requested_format_->width / |
| 198 static_cast<float>(requested_format_->height); | 224 static_cast<float>(requested_format_->height); |
| 199 *cropped_width = | 225 *cropped_width = |
| 200 std::min(in_width, static_cast<int>(in_height * requested_aspect)); | 226 std::min(in_width, static_cast<int>(in_height * requested_aspect)); |
| 201 *cropped_height = | 227 *cropped_height = |
| 202 std::min(in_height, static_cast<int>(in_width / requested_aspect)); | 228 std::min(in_height, static_cast<int>(in_width / requested_aspect)); |
| 203 } | 229 } |
| 204 const Fraction scale = FindScale((*cropped_width) * (*cropped_height), | 230 const Fraction scale = |
| 205 target_pixel_count, max_pixel_count); | 231 FindScale((*cropped_width) * (*cropped_height), target_pixel_count, |
| 232 max_pixel_count, requested_range.min); | |
| 206 // Adjust cropping slightly to get even integer output size and a perfect | 233 // Adjust cropping slightly to get even integer output size and a perfect |
| 207 // scale factor. Make sure the resulting dimensions are aligned correctly | 234 // scale factor. Make sure the resulting dimensions are aligned correctly |
| 208 // to be nice to hardware encoders. | 235 // to be nice to hardware encoders. |
| 209 *cropped_width = | 236 *cropped_width = |
| 210 roundUp(*cropped_width, | 237 roundUp(*cropped_width, |
| 211 scale.denominator * required_resolution_alignment_, in_width); | 238 scale.denominator * required_resolution_alignment_, in_width); |
| 212 *cropped_height = | 239 *cropped_height = |
| 213 roundUp(*cropped_height, | 240 roundUp(*cropped_height, |
| 214 scale.denominator * required_resolution_alignment_, in_height); | 241 scale.denominator * required_resolution_alignment_, in_height); |
| 215 RTC_DCHECK_EQ(0, *cropped_width % scale.denominator); | 242 RTC_DCHECK_EQ(0, *cropped_width % scale.denominator); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 242 | 269 |
| 243 return true; | 270 return true; |
| 244 } | 271 } |
| 245 | 272 |
| 246 void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) { | 273 void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) { |
| 247 rtc::CritScope cs(&critical_section_); | 274 rtc::CritScope cs(&critical_section_); |
| 248 requested_format_ = rtc::Optional<VideoFormat>(format); | 275 requested_format_ = rtc::Optional<VideoFormat>(format); |
| 249 next_frame_timestamp_ns_ = rtc::Optional<int64_t>(); | 276 next_frame_timestamp_ns_ = rtc::Optional<int64_t>(); |
| 250 } | 277 } |
| 251 | 278 |
| 252 void VideoAdapter::OnResolutionRequest( | 279 void VideoAdapter::OnSinkWantsUpdated(const rtc::VideoSinkWants& sink_wants) { |
|
nisse-webrtc
2017/02/17 08:55:21
I understand this change makes things look a littl
sprang_webrtc
2017/02/20 15:48:35
Fair enough. Just didn't want to pass along 6 opti
| |
| 253 const rtc::Optional<int>& target_pixel_count, | |
| 254 const rtc::Optional<int>& max_pixel_count) { | |
| 255 rtc::CritScope cs(&critical_section_); | 280 rtc::CritScope cs(&critical_section_); |
| 256 resolution_request_max_pixel_count_ = | 281 requested_sink_wants_ = sink_wants; |
| 257 max_pixel_count.value_or(std::numeric_limits<int>::max()); | |
| 258 resolution_request_target_pixel_count_ = | |
| 259 target_pixel_count.value_or(resolution_request_max_pixel_count_); | |
| 260 } | 282 } |
| 261 | 283 |
| 262 } // namespace cricket | 284 } // namespace cricket |
| OLD | NEW |