Chromium Code Reviews| Index: webrtc/media/base/videoadapter.cc |
| diff --git a/webrtc/media/base/videoadapter.cc b/webrtc/media/base/videoadapter.cc |
| index 797a876f2b4049c38049bf1477b5ecb80819e022..28d5988badcdf18b123f20daf4eb2e9e43bd1408 100644 |
| --- a/webrtc/media/base/videoadapter.cc |
| +++ b/webrtc/media/base/videoadapter.cc |
| @@ -30,16 +30,14 @@ const float kScaleFactors[] = { |
| 3.f / 16.f, // 3/16 scale. |
| }; |
| -float FindScaleLessThanOrEqual(int width, |
| - int height, |
| +float FindScaleLessThanOrEqual(int input_num_pixels, |
| int target_num_pixels, |
| int* resulting_number_of_pixels) { |
| float best_distance = std::numeric_limits<float>::max(); |
| float best_scale = 0.0f; // Default to 0 if nothing matches. |
| - float pixels = width * height; |
| float best_number_of_pixels = 0.0f; |
| for (const auto& scale : kScaleFactors) { |
| - float test_num_pixels = pixels * scale * scale; |
| + float test_num_pixels = input_num_pixels * scale * scale; |
| float diff = target_num_pixels - test_num_pixels; |
| if (diff < 0) { |
| continue; |
| @@ -59,16 +57,15 @@ float FindScaleLessThanOrEqual(int width, |
| return best_scale; |
| } |
| -float FindScaleLargerThan(int width, |
| - int height, |
| +float FindScaleLargerThan(int input_num_pixels, |
| int target_num_pixels, |
| int* resulting_number_of_pixels) { |
| float best_distance = std::numeric_limits<float>::max(); |
| float best_scale = 1.f; // Default to unscaled if nothing matches. |
| - float pixels = width * height; |
| - float best_number_of_pixels = pixels; // Default to input number of pixels. |
| + // Default to input number of pixels. |
| + float best_number_of_pixels = input_num_pixels; |
| for (const auto& scale : kScaleFactors) { |
| - float test_num_pixels = pixels * scale * scale; |
| + float test_num_pixels = input_num_pixels * scale * scale; |
| float diff = test_num_pixels - target_num_pixels; |
| if (diff <= 0) { |
| break; |
| @@ -84,21 +81,36 @@ float FindScaleLargerThan(int width, |
| return best_scale; |
| } |
| +float FindScale(int input_num_pixels, |
| + int max_pixel_count_step_up, |
| + int max_pixel_count) { |
| + // Try scale just above |max_pixel_count_step_up_|. |
| + if (max_pixel_count_step_up > 0) { |
| + int resulting_pixel_count; |
| + const float scale = FindScaleLargerThan( |
| + input_num_pixels, max_pixel_count_step_up, &resulting_pixel_count); |
| + if (resulting_pixel_count <= max_pixel_count) |
| + return scale; |
| + } |
| + // Return largest scale below |max_pixel_count|. |
| + return FindScaleLessThanOrEqual(input_num_pixels, max_pixel_count, nullptr); |
| +} |
| + |
| } // namespace |
| namespace cricket { |
| VideoAdapter::VideoAdapter() |
| - : output_num_pixels_(std::numeric_limits<int>::max()), |
| - frames_in_(0), |
| + : frames_in_(0), |
| frames_out_(0), |
| frames_scaled_(0), |
| adaption_changes_(0), |
| previous_width_(0), |
| previous_height_(0), |
| + input_interval_(0), |
| interval_next_frame_(0), |
| - format_request_max_pixel_count_(std::numeric_limits<int>::max()), |
| - resolution_request_max_pixel_count_(std::numeric_limits<int>::max()) {} |
| + resolution_request_max_pixel_count_(std::numeric_limits<int>::max()), |
| + resolution_request_max_pixel_count_step_up_(0) {} |
| VideoAdapter::~VideoAdapter() {} |
| @@ -106,57 +118,39 @@ void VideoAdapter::SetExpectedInputFrameInterval(int64_t interval) { |
| // TODO(perkj): Consider measuring input frame rate instead. |
| // Frame rate typically varies depending on lighting. |
| rtc::CritScope cs(&critical_section_); |
| - input_format_.interval = interval; |
| -} |
| - |
| -void VideoAdapter::SetInputFormat(const VideoFormat& format) { |
| - bool is_resolution_change = (input_format().width != format.width || |
| - input_format().height != format.height); |
| - int64_t old_input_interval = input_format_.interval; |
| - input_format_ = format; |
| - output_format_.interval = |
| - std::max(output_format_.interval, input_format_.interval); |
| - if (old_input_interval != input_format_.interval) { |
| - LOG(LS_INFO) << "VAdapt input interval changed from " |
| - << old_input_interval << " to " << input_format_.interval; |
| - } |
| - if (is_resolution_change) { |
| - // Trigger the adaptation logic again, to potentially reset the adaptation |
| - // state for things like view requests that may not longer be capping |
| - // output (or may now cap output). |
| - Adapt(std::min(format_request_max_pixel_count_, |
| - resolution_request_max_pixel_count_), |
| - 0); |
| - } |
| + input_interval_ = interval; |
| } |
| -const VideoFormat& VideoAdapter::input_format() const { |
| - rtc::CritScope cs(&critical_section_); |
| - return input_format_; |
| -} |
| - |
| -VideoFormat VideoAdapter::AdaptFrameResolution(int in_width, int in_height) { |
| +void VideoAdapter::AdaptFrameResolution(int in_width, |
| + int in_height, |
| + int* cropped_width, |
| + int* cropped_height, |
| + int* out_width, |
| + int* out_height) { |
| rtc::CritScope cs(&critical_section_); |
| ++frames_in_; |
| - SetInputFormat(VideoFormat( |
| - in_width, in_height, input_format_.interval, input_format_.fourcc)); |
| + // The max output pixel count is the minimum of the requests from |
| + // OnOutputFormatRequest and OnResolutionRequest. |
| + int max_pixel_count = resolution_request_max_pixel_count_; |
| + if (requested_format_) { |
| + max_pixel_count = std::min( |
| + max_pixel_count, requested_format_->width * requested_format_->height); |
| + } |
| // Drop the input frame if necessary. |
| bool should_drop = false; |
| - if (!output_num_pixels_) { |
| + if (max_pixel_count == 0) { |
| // Drop all frames as the output format is 0x0. |
| should_drop = true; |
| - } else { |
| + } else if (requested_format_ && requested_format_->interval > 0) { |
| // Drop some frames based on input fps and output fps. |
| // Normally output fps is less than input fps. |
| - interval_next_frame_ += input_format_.interval; |
| - if (output_format_.interval > 0) { |
| - if (interval_next_frame_ >= output_format_.interval) { |
| - interval_next_frame_ %= output_format_.interval; |
| - } else { |
| - should_drop = true; |
| - } |
| + interval_next_frame_ += input_interval_; |
| + if (interval_next_frame_ >= requested_format_->interval) { |
| + interval_next_frame_ %= requested_format_->interval; |
|
nisse-webrtc
2016/05/13 07:58:19
If it is ensured that input_interval < requested_f
magjed_webrtc
2016/05/13 09:12:59
1) I think in the near future, we want to remove t
nisse-webrtc
2016/05/13 11:47:06
I see. Don't bother changing that now, then.
magjed_webrtc
2016/05/13 14:22:28
Done.
|
| + } else { |
| + should_drop = true; |
| } |
| } |
| if (should_drop) { |
| @@ -170,48 +164,71 @@ VideoFormat VideoAdapter::AdaptFrameResolution(int in_width, int in_height) { |
| << " Changes: " << adaption_changes_ |
| << " Input: " << in_width |
| << "x" << in_height |
| - << " i" << input_format_.interval |
| - << " Output: i" << output_format_.interval; |
| + << " i" << input_interval_ |
| + << " Output: i" |
| + << (requested_format_ ? requested_format_->interval : 0); |
| } |
| - return VideoFormat(); // Drop frame. |
| + // Drop frame. |
| + *cropped_width = 0; |
| + *cropped_height = 0; |
| + *out_width = 0; |
| + *out_height = 0; |
| + return; |
| } |
| - const float scale = FindScaleLessThanOrEqual(in_width, in_height, |
| - output_num_pixels_, nullptr); |
| - const int output_width = static_cast<int>(in_width * scale + .5f); |
| - const int output_height = static_cast<int>(in_height * scale + .5f); |
| + // Calculate how the input should be cropped. |
| + if (!requested_format_ || |
| + requested_format_->width == 0 || requested_format_->height == 0) { |
| + *cropped_width = in_width; |
| + *cropped_height = in_height; |
| + } else { |
| + // Adjust |requested_format_| orientation to match input. |
| + if ((in_width > in_height) != |
| + (requested_format_->width > requested_format_->height)) { |
| + std::swap(requested_format_->width, requested_format_->height); |
| + } |
| + const float requested_aspect = |
|
nisse-webrtc
2016/05/13 07:58:19
I liked my integer-only crop logic in
https://co
magjed_webrtc
2016/05/13 09:12:59
Your interger-only crop is clever, but I don't see
nisse-webrtc
2016/05/13 11:47:06
Ok.
|
| + requested_format_->width / |
| + static_cast<float>(requested_format_->height); |
| + *cropped_width = |
| + std::min(in_width, static_cast<int>(in_height * requested_aspect)); |
| + *cropped_height = |
| + std::min(in_height, static_cast<int>(in_width / requested_aspect)); |
| + } |
| + |
| + // Find best scale factor. |
| + const float scale = |
| + FindScale(*cropped_width * *cropped_height, |
| + resolution_request_max_pixel_count_step_up_, max_pixel_count); |
| + |
| + // Calculate final output size. |
| + *out_width = static_cast<int>(*cropped_width * scale + .5f); |
|
nisse-webrtc
2016/05/13 07:58:19
But now I'm getting confused... I thought the requ
magjed_webrtc
2016/05/13 09:12:59
I need to base the scale factor on the number of c
|
| + *out_height = static_cast<int>(*cropped_height * scale + .5f); |
| ++frames_out_; |
| if (scale != 1) |
| ++frames_scaled_; |
| - if (previous_width_ && (previous_width_ != output_width || |
| - previous_height_ != output_height)) { |
| + if (previous_width_ && (previous_width_ != *out_width || |
| + previous_height_ != *out_height)) { |
| ++adaption_changes_; |
| LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ << " / out " |
| << frames_out_ << " / in " << frames_in_ |
| << " Changes: " << adaption_changes_ << " Input: " << in_width |
| - << "x" << in_height << " i" << input_format_.interval |
| - << " Scale: " << scale << " Output: " << output_width << "x" |
| - << output_height << " i" << output_format_.interval; |
| + << "x" << in_height << " i" << input_interval_ |
| + << " Scale: " << scale << " Output: " << *out_width << "x" |
| + << *out_height << " i" |
| + << (requested_format_ ? requested_format_->interval : 0); |
| } |
| - output_format_.width = output_width; |
| - output_format_.height = output_height; |
| - previous_width_ = output_width; |
| - previous_height_ = output_height; |
| - |
| - return output_format_; |
| + previous_width_ = *out_width; |
| + previous_height_ = *out_height; |
| } |
| void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) { |
| rtc::CritScope cs(&critical_section_); |
| - format_request_max_pixel_count_ = format.width * format.height; |
| - output_format_.interval = format.interval; |
| - Adapt(std::min(format_request_max_pixel_count_, |
| - resolution_request_max_pixel_count_), |
| - 0); |
| + requested_format_ = rtc::Optional<VideoFormat>(format); |
| } |
| void VideoAdapter::OnResolutionRequest( |
| @@ -220,44 +237,8 @@ void VideoAdapter::OnResolutionRequest( |
| rtc::CritScope cs(&critical_section_); |
| resolution_request_max_pixel_count_ = |
| max_pixel_count.value_or(std::numeric_limits<int>::max()); |
| - Adapt(std::min(format_request_max_pixel_count_, |
| - resolution_request_max_pixel_count_), |
| - max_pixel_count_step_up.value_or(0)); |
| -} |
| - |
| -bool VideoAdapter::Adapt(int max_num_pixels, int max_pixel_count_step_up) { |
| - float scale_lower = |
| - FindScaleLessThanOrEqual(input_format_.width, input_format_.height, |
| - max_num_pixels, &max_num_pixels); |
| - float scale_upper = |
| - max_pixel_count_step_up > 0 |
| - ? FindScaleLargerThan(input_format_.width, input_format_.height, |
| - max_pixel_count_step_up, |
| - &max_pixel_count_step_up) |
| - : 1.f; |
| - |
| - bool use_max_pixel_count_step_up = |
| - max_pixel_count_step_up > 0 && max_num_pixels > max_pixel_count_step_up; |
| - |
| - int old_num_pixels = output_num_pixels_; |
| - output_num_pixels_ = |
| - use_max_pixel_count_step_up ? max_pixel_count_step_up : max_num_pixels; |
| - // Log the new size. |
| - float scale = use_max_pixel_count_step_up ? scale_upper : scale_lower; |
| - int new_width = static_cast<int>(input_format_.width * scale + .5f); |
| - int new_height = static_cast<int>(input_format_.height * scale + .5f); |
| - |
| - bool changed = output_num_pixels_ != old_num_pixels; |
| - LOG(LS_INFO) << "OnResolutionRequest: " |
| - << " Max pixels: " << max_num_pixels |
| - << " Max pixels step up: " << max_pixel_count_step_up |
| - << " Output Pixels: " << output_num_pixels_ |
| - << " Input: " << input_format_.width << "x" |
| - << input_format_.height << " Scale: " << scale |
| - << " Resolution: " << new_width << "x" << new_height |
| - << " Changed: " << (changed ? "true" : "false"); |
| - |
| - return changed; |
| + resolution_request_max_pixel_count_step_up_ = |
| + max_pixel_count_step_up.value_or(0); |
| } |
| } // namespace cricket |