| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #include "media/base/video_adapter.h" |
| 12 |
| 13 | #include <algorithm> |
| 14 | #include <cmath> |
| 15 | #include <cstdint> |
| 16 | #include <cstdlib> |
| 17 | #include <limits> |
| 18 | #include <utility> |
| 19 |
| 20 | #include "absl/types/optional.h" |
| 21 | #include "media/base/video_common.h" |
| 22 | #include "rtc_base/checks.h" |
| 23 | #include "rtc_base/logging.h" |
| 24 | #include "rtc_base/strings/string_builder.h" |
| 25 | #include "rtc_base/time_utils.h" |
| 26 | #include "system_wrappers/include/field_trial.h" |
| 27 |
| 28 | namespace { |
| 29 |
| 30 | struct Fraction { |
| 31 | int numerator; |
| 32 | int denominator; |
| 33 |
| 34 | void DivideByGcd() { |
| 35 | int g = cricket::GreatestCommonDivisor(numerator, denominator); |
| 36 | numerator /= g; |
| 37 | denominator /= g; |
| 38 | } |
| 39 |
| 40 | // Determines number of output pixels if both width and height of an input of |
| 41 | // `input_pixels` pixels is scaled with the fraction numerator / denominator. |
| 42 | int scale_pixel_count(int input_pixels) { |
| 43 | return (numerator * numerator * static_cast<int64_t>(input_pixels)) / |
| 44 | (denominator * denominator); |
| 45 | } |
| 46 | }; |
| 47 |
| 48 | // Round `value_to_round` to a multiple of `multiple`. Prefer rounding upwards, |
| 49 | // but never more than `max_value`. |
| 50 | int roundUp(int value_to_round, int multiple, int max_value) { |
| 51 | const int rounded_value = |
| 52 | (value_to_round + multiple - 1) / multiple * multiple; |
| 53 | return rounded_value <= max_value ? rounded_value |
| 54 | : (max_value / multiple * multiple); |
| 55 | } |
| 56 |
| 57 | // Generates a scale factor that makes `input_pixels` close to `target_pixels`, |
| 58 | // but no higher than `max_pixels`. |
| 59 | Fraction FindScale(int input_width, |
| 60 | int input_height, |
| 61 | int target_pixels, |
| 62 | int max_pixels, |
| 63 | bool variable_start_scale_factor) { |
| 64 | // This function only makes sense for a positive target. |
| 65 | RTC_DCHECK_GT(target_pixels, 0); |
| 66 | RTC_DCHECK_GT(max_pixels, 0); |
| 67 | RTC_DCHECK_GE(max_pixels, target_pixels); |
| 68 |
| 69 | const int input_pixels = input_width * input_height; |
| 70 |
| 71 | // Don't scale up original. |
| 72 | if (target_pixels >= input_pixels) |
| 73 | return Fraction{1, 1}; |
| 74 |
| 75 | Fraction current_scale = Fraction{1, 1}; |
| 76 | Fraction best_scale = Fraction{1, 1}; |
| 77 |
| 78 | if (variable_start_scale_factor) { |
| 79 | // Start scaling down by 2/3 depending on `input_width` and `input_height`. |
| 80 | if (input_width % 3 == 0 && input_height % 3 == 0) { |
| 81 | // 2/3 (then alternates 3/4, 2/3, 3/4,...). |
| 82 | current_scale = Fraction{6, 6}; |
| 83 | } |
| 84 | if (input_width % 9 == 0 && input_height % 9 == 0) { |
| 85 | // 2/3, 2/3 (then alternates 3/4, 2/3, 3/4,...). |
| 86 | current_scale = Fraction{36, 36}; |
| 87 | } |
| 88 | } |
| 89 |
| 90 | // The minimum (absolute) difference between the number of output pixels and |
| 91 | // the target pixel count. |
| 92 | int min_pixel_diff = std::numeric_limits<int>::max(); |
| 93 | if (input_pixels <= max_pixels) { |
| 94 | // Start condition for 1/1 case, if it is less than max. |
| 95 | min_pixel_diff = std::abs(input_pixels - target_pixels); |
| 96 | } |
| 97 |
| 98 | // Alternately scale down by 3/4 and 2/3. This results in fractions which are |
| 99 | // effectively scalable. For instance, starting at 1280x720 will result in |
| 100 | // the series (3/4) => 960x540, (1/2) => 640x360, (3/8) => 480x270, |
| 101 | // (1/4) => 320x180, (3/16) => 240x125, (1/8) => 160x90. |
| 102 | while (current_scale.scale_pixel_count(input_pixels) > target_pixels) { |
| 103 | if (current_scale.numerator % 3 == 0 && |
| 104 | current_scale.denominator % 2 == 0) { |
| 105 | // Multiply by 2/3. |
| 106 | current_scale.numerator /= 3; |
| 107 | current_scale.denominator /= 2; |
| 108 | } else { |
| 109 | // Multiply by 3/4. |
| 110 | current_scale.numerator *= 3; |
| 111 | current_scale.denominator *= 4; |
| 112 | } |
| 113 |
| 114 | int output_pixels = current_scale.scale_pixel_count(input_pixels); |
| 115 | if (output_pixels <= max_pixels) { |
| 116 | int diff = std::abs(target_pixels - output_pixels); |
| 117 | if (diff < min_pixel_diff) { |
| 118 | min_pixel_diff = diff; |
| 119 | best_scale = current_scale; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | best_scale.DivideByGcd(); |
| 124 |
| 125 | return best_scale; |
| 126 | } |
| 127 |
| 128 | absl::optional<std::pair<int, int>> Swap( |
| 129 | const absl::optional<std::pair<int, int>>& in) { |
| 130 | if (!in) { |
| 131 | return absl::nullopt; |
| 132 | } |
| 133 | return std::make_pair(in->second, in->first); |
| 134 | } |
| 135 |
| 136 | } // namespace |
| 137 |
| 138 | namespace cricket { |
| 139 |
| 140 | VideoAdapter::VideoAdapter(int source_resolution_alignment) |
| 141 | : frames_in_(0), |
| 142 | frames_out_(0), |
| 143 | frames_scaled_(0), |
| 144 | adaption_changes_(0), |
| 145 | previous_width_(0), |
| 146 | previous_height_(0), |
| 147 | variable_start_scale_factor_(!webrtc::field_trial::IsDisabled( |
| 148 | "WebRTC-Video-VariableStartScaleFactor")), |
| 149 | source_resolution_alignment_(source_resolution_alignment), |
| 150 | resolution_alignment_(source_resolution_alignment), |
| 151 | resolution_request_target_pixel_count_(std::numeric_limits<int>::max()), |
| 152 | resolution_request_max_pixel_count_(std::numeric_limits<int>::max()), |
| 153 | max_framerate_request_(std::numeric_limits<int>::max()) {} |
| 154 |
| 155 | VideoAdapter::VideoAdapter() : VideoAdapter(1) {} |
| 156 |
| 157 | VideoAdapter::~VideoAdapter() {} |
| 158 |
| 159 | bool VideoAdapter::DropFrame(int64_t in_timestamp_ns) { |
| 160 | int max_fps = max_framerate_request_; |
| 161 | if (output_format_request_.max_fps) |
| 162 | max_fps = std::min(max_fps, *output_format_request_.max_fps); |
| 163 |
| 164 | framerate_controller_.SetMaxFramerate(max_fps); |
| 165 | return framerate_controller_.ShouldDropFrame(in_timestamp_ns); |
| 166 | } |
| 167 |
| 168 | bool VideoAdapter::AdaptFrameResolution(int in_width, |
| 169 | int in_height, |
| 170 | int64_t in_timestamp_ns, |
| 171 | int* cropped_width, |
| 172 | int* cropped_height, |
| 173 | int* out_width, |
| 174 | int* out_height) { |
| 175 | webrtc::MutexLock lock(&mutex_); |
| 176 | ++frames_in_; |
| 177 |
| 178 | // The max output pixel count is the minimum of the requests from |
| 179 | // OnOutputFormatRequest and OnResolutionFramerateRequest. |
| 180 | int max_pixel_count = resolution_request_max_pixel_count_; |
| 181 |
| 182 | // Select target aspect ratio and max pixel count depending on input frame |
| 183 | // orientation. |
| 184 | absl::optional<std::pair<int, int>> target_aspect_ratio; |
| 185 | if (in_width > in_height) { |
| 186 | target_aspect_ratio = output_format_request_.target_landscape_aspect_ratio; |
| 187 | if (output_format_request_.max_landscape_pixel_count) |
| 188 | max_pixel_count = std::min( |
| 189 | max_pixel_count, *output_format_request_.max_landscape_pixel_count); |
| 190 | } else { |
| 191 | target_aspect_ratio = output_format_request_.target_portrait_aspect_ratio; |
| 192 | if (output_format_request_.max_portrait_pixel_count) |
| 193 | max_pixel_count = std::min( |
| 194 | max_pixel_count, *output_format_request_.max_portrait_pixel_count); |
| 195 | } |
| 196 |
| 197 | int target_pixel_count = |
| 198 | std::min(resolution_request_target_pixel_count_, max_pixel_count); |
| 199 |
| 200 | // Drop the input frame if necessary. |
| 201 | if (max_pixel_count <= 0 || DropFrame(in_timestamp_ns)) { |
| 202 | // Show VAdapt log every 90 frames dropped. (3 seconds) |
| 203 | if ((frames_in_ - frames_out_) % 90 == 0) { |
| 204 | // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed |
| 205 | // in default calls. |
| 206 | RTC_LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_ |
| 207 | << " / out " << frames_out_ << " / in " << frames_in_ |
| 208 | << " Changes: " << adaption_changes_ |
| 209 | << " Input: " << in_width << "x" << in_height |
| 210 | << " timestamp: " << in_timestamp_ns |
| 211 | << " Output fps: " << max_framerate_request_ << "/" |
| 212 | << output_format_request_.max_fps.value_or(-1) |
| 213 | << " alignment: " << resolution_alignment_; |
| 214 | } |
| 215 |
| 216 | // Drop frame. |
| 217 | return false; |
| 218 | } |
| 219 |
| 220 | // Calculate how the input should be cropped. |
| 221 | if (!target_aspect_ratio || target_aspect_ratio->first <= 0 || |
| 222 | target_aspect_ratio->second <= 0) { |
| 223 | *cropped_width = in_width; |
| 224 | *cropped_height = in_height; |
| 225 | } else { |
| 226 | const float requested_aspect = |
| 227 | target_aspect_ratio->first / |
| 228 | static_cast<float>(target_aspect_ratio->second); |
| 229 | *cropped_width = |
| 230 | std::min(in_width, static_cast<int>(in_height * requested_aspect)); |
| 231 | *cropped_height = |
| 232 | std::min(in_height, static_cast<int>(in_width / requested_aspect)); |
| 233 | } |
| 234 | const Fraction scale = |
| 235 | FindScale(*cropped_width, *cropped_height, target_pixel_count, |
| 236 | max_pixel_count, variable_start_scale_factor_); |
| 237 | // Adjust cropping slightly to get correctly aligned output size and a perfect |
| 238 | // scale factor. |
| 239 | *cropped_width = roundUp(*cropped_width, |
| 240 | scale.denominator * resolution_alignment_, in_width); |
| 241 | *cropped_height = roundUp( |
| 242 | *cropped_height, scale.denominator * resolution_alignment_, in_height); |
| 243 | RTC_DCHECK_EQ(0, *cropped_width % scale.denominator); |
| 244 | RTC_DCHECK_EQ(0, *cropped_height % scale.denominator); |
| 245 |
| 246 | // Calculate final output size. |
| 247 | *out_width = *cropped_width / scale.denominator * scale.numerator; |
| 248 | *out_height = *cropped_height / scale.denominator * scale.numerator; |
| 249 | RTC_DCHECK_EQ(0, *out_width % resolution_alignment_); |
| 250 | RTC_DCHECK_EQ(0, *out_height % resolution_alignment_); |
| 251 |
| 252 | ++frames_out_; |
| 253 | if (scale.numerator != scale.denominator) |
| 254 | ++frames_scaled_; |
| 255 |
| 256 | if (previous_width_ && |
| 257 | (previous_width_ != *out_width || previous_height_ != *out_height)) { |
| 258 | ++adaption_changes_; |
| 259 | RTC_LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ |
| 260 | << " / out " << frames_out_ << " / in " << frames_in_ |
| 261 | << " Changes: " << adaption_changes_ |
| 262 | << " Input: " << in_width << "x" << in_height |
| 263 | << " Scale: " << scale.numerator << "/" |
| 264 | << scale.denominator << " Output: " << *out_width << "x" |
| 265 | << *out_height << " fps: " << max_framerate_request_ << "/" |
| 266 | << output_format_request_.max_fps.value_or(-1) |
| 267 | << " alignment: " << resolution_alignment_; |
| 268 | } |
| 269 |
| 270 | previous_width_ = *out_width; |
| 271 | previous_height_ = *out_height; |
| 272 |
| 273 | return true; |
| 274 | } |
| 275 |
| 276 | void VideoAdapter::OnOutputFormatRequest( |
| 277 | const absl::optional<VideoFormat>& format) { |
| 278 | absl::optional<std::pair<int, int>> target_aspect_ratio; |
| 279 | absl::optional<int> max_pixel_count; |
| 280 | absl::optional<int> max_fps; |
| 281 | if (format) { |
| 282 | target_aspect_ratio = std::make_pair(format->width, format->height); |
| 283 | max_pixel_count = format->width * format->height; |
| 284 | if (format->interval > 0) |
| 285 | max_fps = rtc::kNumNanosecsPerSec / format->interval; |
| 286 | } |
| 287 | OnOutputFormatRequest(target_aspect_ratio, max_pixel_count, max_fps); |
| 288 | } |
| 289 |
| 290 | void VideoAdapter::OnOutputFormatRequest( |
| 291 | const absl::optional<std::pair<int, int>>& target_aspect_ratio, |
| 292 | const absl::optional<int>& max_pixel_count, |
| 293 | const absl::optional<int>& max_fps) { |
| 294 | absl::optional<std::pair<int, int>> target_landscape_aspect_ratio; |
| 295 | absl::optional<std::pair<int, int>> target_portrait_aspect_ratio; |
| 296 | if (target_aspect_ratio && target_aspect_ratio->first > 0 && |
| 297 | target_aspect_ratio->second > 0) { |
| 298 | // Maintain input orientation. |
| 299 | const int max_side = |
| 300 | std::max(target_aspect_ratio->first, target_aspect_ratio->second); |
| 301 | const int min_side = |
| 302 | std::min(target_aspect_ratio->first, target_aspect_ratio->second); |
| 303 | target_landscape_aspect_ratio = std::make_pair(max_side, min_side); |
| 304 | target_portrait_aspect_ratio = std::make_pair(min_side, max_side); |
| 305 | } |
| 306 | OnOutputFormatRequest(target_landscape_aspect_ratio, max_pixel_count, |
| 307 | target_portrait_aspect_ratio, max_pixel_count, max_fps); |
| 308 | } |
| 309 |
| 310 | void VideoAdapter::OnOutputFormatRequest( |
| 311 | const absl::optional<std::pair<int, int>>& target_landscape_aspect_ratio, |
| 312 | const absl::optional<int>& max_landscape_pixel_count, |
| 313 | const absl::optional<std::pair<int, int>>& target_portrait_aspect_ratio, |
| 314 | const absl::optional<int>& max_portrait_pixel_count, |
| 315 | const absl::optional<int>& max_fps) { |
| 316 | webrtc::MutexLock lock(&mutex_); |
| 317 |
| 318 | OutputFormatRequest request = { |
| 319 | .target_landscape_aspect_ratio = target_landscape_aspect_ratio, |
| 320 | .max_landscape_pixel_count = max_landscape_pixel_count, |
| 321 | .target_portrait_aspect_ratio = target_portrait_aspect_ratio, |
| 322 | .max_portrait_pixel_count = max_portrait_pixel_count, |
| 323 | .max_fps = max_fps}; |
| 324 |
| 325 | if (stashed_output_format_request_) { |
| 326 | // Save the output format request for later use in case the encoder making |
| 327 | // this call would become active, because currently all active encoders use |
| 328 | // requested_resolution instead. |
| 329 | stashed_output_format_request_ = request; |
| 330 | RTC_LOG(LS_INFO) << "Stashing OnOutputFormatRequest: " |
| 331 | << stashed_output_format_request_->ToString(); |
| 332 | } else { |
| 333 | output_format_request_ = request; |
| 334 | RTC_LOG(LS_INFO) << "Setting output_format_request_: " |
| 335 | << output_format_request_.ToString(); |
| 336 | } |
| 337 |
| 338 | framerate_controller_.Reset(); |
| 339 | } |
| 340 |
| 341 | void VideoAdapter::OnSinkWants(const rtc::VideoSinkWants& sink_wants) { |
| 342 | webrtc::MutexLock lock(&mutex_); |
| 343 | resolution_request_max_pixel_count_ = sink_wants.max_pixel_count; |
| 344 | resolution_request_target_pixel_count_ = |
| 345 | sink_wants.target_pixel_count.value_or( |
| 346 | resolution_request_max_pixel_count_); |
| 347 | max_framerate_request_ = sink_wants.max_framerate_fps; |
| 348 | resolution_alignment_ = cricket::LeastCommonMultiple( |
| 349 | source_resolution_alignment_, sink_wants.resolution_alignment); |
| 350 |
| 351 | if (!sink_wants.aggregates) { |
| 352 | RTC_LOG(LS_WARNING) |
| 353 | << "These should always be created by VideoBroadcaster!"; |
| 354 | return; |
| 355 | } |
| 356 |
| 357 | // If requested_resolution is used, and there are no active encoders |
| 358 | // that are NOT using requested_resolution (aka newapi), then override |
| 359 | // calls to OnOutputFormatRequest and use values from requested_resolution |
| 360 | // instead (combined with qualityscaling based on pixel counts above). |
| 361 | if (webrtc::field_trial::IsDisabled( |
| 362 | "WebRTC-Video-RequestedResolutionOverrideOutputFormatRequest")) { |
| 363 | // kill-switch... |
| 364 | return; |
| 365 | } |
| 366 |
| 367 | if (!sink_wants.requested_resolution) { |
| 368 | if (stashed_output_format_request_) { |
| 369 | // because current active_output_format_request is based on |
| 370 | // requested_resolution logic, while current encoder(s) doesn't want that, |
| 371 | // we have to restore the stashed request. |
| 372 | RTC_LOG(LS_INFO) << "Unstashing OnOutputFormatRequest: " |
| 373 | << stashed_output_format_request_->ToString(); |
| 374 | output_format_request_ = *stashed_output_format_request_; |
| 375 | stashed_output_format_request_.reset(); |
| 376 | } |
| 377 | return; |
| 378 | } |
| 379 |
| 380 | if (sink_wants.aggregates->any_active_without_requested_resolution) { |
| 381 | return; |
| 382 | } |
| 383 |
| 384 | if (!stashed_output_format_request_) { |
| 385 | // The active output format request is about to be rewritten by |
| 386 | // request_resolution. We need to save it for later use in case the encoder |
| 387 | // which doesn't use request_resolution logic become active in the future. |
| 388 | stashed_output_format_request_ = output_format_request_; |
| 389 | RTC_LOG(LS_INFO) << "Stashing OnOutputFormatRequest: " |
| 390 | << stashed_output_format_request_->ToString(); |
| 391 | } |
| 392 |
| 393 | auto res = *sink_wants.requested_resolution; |
| 394 | auto pixel_count = res.width * res.height; |
| 395 | output_format_request_.target_landscape_aspect_ratio = |
| 396 | std::make_pair(res.width, res.height); |
| 397 | output_format_request_.max_landscape_pixel_count = pixel_count; |
| 398 | output_format_request_.target_portrait_aspect_ratio = |
| 399 | std::make_pair(res.height, res.width); |
| 400 | output_format_request_.max_portrait_pixel_count = pixel_count; |
| 401 | output_format_request_.max_fps = max_framerate_request_; |
| 402 | RTC_LOG(LS_INFO) << "Setting output_format_request_ based on sink_wants: " |
| 403 | << output_format_request_.ToString(); |
| 404 | } |
| 405 |
| 406 | int VideoAdapter::GetTargetPixels() const { |
| 407 | webrtc::MutexLock lock(&mutex_); |
| 408 | return resolution_request_target_pixel_count_; |
| 409 | } |
| 410 |
| 411 | float VideoAdapter::GetMaxFramerate() const { |
| 412 | webrtc::MutexLock lock(&mutex_); |
| 413 | // Minimum of `output_format_request_.max_fps` and `max_framerate_request_` is |
| 414 | // used to throttle frame-rate. |
| 415 | int framerate = |
| 416 | std::min(max_framerate_request_, |
| 417 | output_format_request_.max_fps.value_or(max_framerate_request_)); |
| 418 | if (framerate == std::numeric_limits<int>::max()) { |
| 419 | return std::numeric_limits<float>::infinity(); |
| 420 | } else { |
| 421 | return max_framerate_request_; |
| 422 | } |
| 423 | } |
| 424 |
| 425 | std::string VideoAdapter::OutputFormatRequest::ToString() const { |
| 426 | rtc::StringBuilder oss; |
| 427 | oss << "[ "; |
| 428 | if (target_landscape_aspect_ratio == Swap(target_portrait_aspect_ratio) && |
| 429 | max_landscape_pixel_count == max_portrait_pixel_count) { |
| 430 | if (target_landscape_aspect_ratio) { |
| 431 | oss << target_landscape_aspect_ratio->first << "x" |
| 432 | << target_landscape_aspect_ratio->second; |
| 433 | } else { |
| 434 | oss << "unset-resolution"; |
| 435 | } |
| 436 | if (max_landscape_pixel_count) { |
| 437 | oss << " max_pixel_count: " << *max_landscape_pixel_count; |
| 438 | } |
| 439 | } else { |
| 440 | oss << "[ landscape: "; |
| 441 | if (target_landscape_aspect_ratio) { |
| 442 | oss << target_landscape_aspect_ratio->first << "x" |
| 443 | << target_landscape_aspect_ratio->second; |
| 444 | } else { |
| 445 | oss << "unset"; |
| 446 | } |
| 447 | if (max_landscape_pixel_count) { |
| 448 | oss << " max_pixel_count: " << *max_landscape_pixel_count; |
| 449 | } |
| 450 | oss << " ] [ portrait: "; |
| 451 | if (target_portrait_aspect_ratio) { |
| 452 | oss << target_portrait_aspect_ratio->first << "x" |
| 453 | << target_portrait_aspect_ratio->second; |
| 454 | } |
| 455 | if (max_portrait_pixel_count) { |
| 456 | oss << " max_pixel_count: " << *max_portrait_pixel_count; |
| 457 | } |
| 458 | oss << " ]"; |
| 459 | } |
| 460 | oss << " max_fps: "; |
| 461 | if (max_fps) { |
| 462 | oss << *max_fps; |
| 463 | } else { |
| 464 | oss << "unset"; |
| 465 | } |
| 466 | oss << " ]"; |
| 467 | return oss.Release(); |
| 468 | } |
| 469 |
| 470 | } // namespace cricket |
| 471 |