jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 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 |
| 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. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 9 | */ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 10 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 11 | #include "webrtc/media/base/videoadapter.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 13 | #include <algorithm> |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 14 | #include <cmath> |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 15 | #include <cstdlib> |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 16 | #include <limits> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 17 | |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 18 | #include "webrtc/base/arraysize.h" |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 19 | #include "webrtc/base/checks.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 20 | #include "webrtc/base/logging.h" |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 21 | #include "webrtc/base/optional.h" |
kjellander | f475277 | 2016-03-02 05:42:30 -0800 | [diff] [blame] | 22 | #include "webrtc/media/base/mediaconstants.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 23 | #include "webrtc/media/base/videocommon.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 24 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 25 | namespace { |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 26 | struct Fraction { |
| 27 | int numerator; |
| 28 | int denominator; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | }; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 30 | |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 31 | // Round |value_to_round| to a multiple of |multiple|. Prefer rounding upwards, |
| 32 | // but never more than |max_value|. |
| 33 | int roundUp(int value_to_round, int multiple, int max_value) { |
| 34 | const int rounded_value = |
| 35 | (value_to_round + multiple - 1) / multiple * multiple; |
| 36 | return rounded_value <= max_value ? rounded_value |
| 37 | : (max_value / multiple * multiple); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 38 | } |
| 39 | |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 40 | // Generates a scale factor that makes |input_num_pixels| smaller or |
| 41 | // larger than |target_num_pixels|, depending on the value of |step_up|. |
| 42 | Fraction FindScale(int input_num_pixels, int target_num_pixels, bool step_up) { |
| 43 | // This function only makes sense for a positive target. |
| 44 | RTC_DCHECK_GT(target_num_pixels, 0); |
| 45 | Fraction best_scale = Fraction{1, 1}; |
| 46 | Fraction last_scale = Fraction{1, 1}; |
| 47 | const float target_scale = |
| 48 | sqrt(target_num_pixels / static_cast<float>(input_num_pixels)); |
| 49 | while (best_scale.numerator > (target_scale * best_scale.denominator)) { |
| 50 | last_scale = best_scale; |
| 51 | if (best_scale.numerator % 3 == 0 && best_scale.denominator % 2 == 0) { |
| 52 | // Multiply by 2/3 |
| 53 | best_scale.numerator /= 3; |
| 54 | best_scale.denominator /= 2; |
| 55 | } else { |
| 56 | // Multiply by 3/4 |
| 57 | best_scale.numerator *= 3; |
| 58 | best_scale.denominator *= 4; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 61 | if (step_up) |
| 62 | return last_scale; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 63 | return best_scale; |
| 64 | } |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 65 | } // namespace |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 67 | namespace cricket { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 69 | VideoAdapter::VideoAdapter(int required_resolution_alignment) |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 70 | : frames_in_(0), |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 71 | frames_out_(0), |
| 72 | frames_scaled_(0), |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 73 | adaption_changes_(0), |
magjed@webrtc.org | a73d746 | 2014-11-14 13:25:25 +0000 | [diff] [blame] | 74 | previous_width_(0), |
| 75 | previous_height_(0), |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 76 | required_resolution_alignment_(required_resolution_alignment), |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 77 | resolution_request_max_pixel_count_(std::numeric_limits<int>::max()), |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 78 | step_up_(false) {} |
| 79 | |
| 80 | VideoAdapter::VideoAdapter() : VideoAdapter(1) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 81 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 82 | VideoAdapter::~VideoAdapter() {} |
| 83 | |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 84 | bool VideoAdapter::KeepFrame(int64_t in_timestamp_ns) { |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 85 | rtc::CritScope cs(&critical_section_); |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 86 | if (!requested_format_ || requested_format_->interval == 0) |
| 87 | return true; |
| 88 | |
| 89 | if (next_frame_timestamp_ns_) { |
| 90 | // Time until next frame should be outputted. |
| 91 | const int64_t time_until_next_frame_ns = |
| 92 | (*next_frame_timestamp_ns_ - in_timestamp_ns); |
| 93 | |
| 94 | // Continue if timestamp is withing expected range. |
| 95 | if (std::abs(time_until_next_frame_ns) < 2 * requested_format_->interval) { |
| 96 | // Drop if a frame shouldn't be outputted yet. |
| 97 | if (time_until_next_frame_ns > 0) |
| 98 | return false; |
| 99 | // Time to output new frame. |
| 100 | *next_frame_timestamp_ns_ += requested_format_->interval; |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // First timestamp received or timestamp is way outside expected range, so |
| 106 | // reset. Set first timestamp target to just half the interval to prefer |
| 107 | // keeping frames in case of jitter. |
| 108 | next_frame_timestamp_ns_ = |
| 109 | rtc::Optional<int64_t>(in_timestamp_ns + requested_format_->interval / 2); |
| 110 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | } |
| 112 | |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 113 | bool VideoAdapter::AdaptFrameResolution(int in_width, |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 114 | int in_height, |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 115 | int64_t in_timestamp_ns, |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 116 | int* cropped_width, |
| 117 | int* cropped_height, |
| 118 | int* out_width, |
| 119 | int* out_height) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 120 | rtc::CritScope cs(&critical_section_); |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 121 | ++frames_in_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 123 | // The max output pixel count is the minimum of the requests from |
| 124 | // OnOutputFormatRequest and OnResolutionRequest. |
| 125 | int max_pixel_count = resolution_request_max_pixel_count_; |
| 126 | if (requested_format_) { |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 127 | // TODO(kthelgason): remove the - |step_up_| hack when we change how |
| 128 | // resolution is requested from VideoSourceProxy. |
| 129 | // This is required because we must not scale above the requested |
| 130 | // format so we subtract one when scaling up. |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 131 | max_pixel_count = std::min( |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 132 | max_pixel_count, requested_format_->width * requested_format_->height - |
| 133 | static_cast<int>(step_up_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 134 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 135 | |
| 136 | // Drop the input frame if necessary. |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 137 | if (max_pixel_count <= 0 || !KeepFrame(in_timestamp_ns)) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 138 | // Show VAdapt log every 90 frames dropped. (3 seconds) |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 139 | if ((frames_in_ - frames_out_) % 90 == 0) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 140 | // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed |
| 141 | // in default calls. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 142 | LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_ |
| 143 | << " / out " << frames_out_ |
| 144 | << " / in " << frames_in_ |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 145 | << " Changes: " << adaption_changes_ |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 146 | << " Input: " << in_width |
| 147 | << "x" << in_height |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 148 | << " timestamp: " << in_timestamp_ns |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 149 | << " Output: i" |
| 150 | << (requested_format_ ? requested_format_->interval : 0); |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 151 | } |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 152 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 153 | // Drop frame. |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 154 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | } |
| 156 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 157 | // Calculate how the input should be cropped. |
| 158 | if (!requested_format_ || |
| 159 | requested_format_->width == 0 || requested_format_->height == 0) { |
| 160 | *cropped_width = in_width; |
| 161 | *cropped_height = in_height; |
| 162 | } else { |
| 163 | // Adjust |requested_format_| orientation to match input. |
| 164 | if ((in_width > in_height) != |
| 165 | (requested_format_->width > requested_format_->height)) { |
| 166 | std::swap(requested_format_->width, requested_format_->height); |
| 167 | } |
| 168 | const float requested_aspect = |
| 169 | requested_format_->width / |
| 170 | static_cast<float>(requested_format_->height); |
| 171 | *cropped_width = |
| 172 | std::min(in_width, static_cast<int>(in_height * requested_aspect)); |
| 173 | *cropped_height = |
| 174 | std::min(in_height, static_cast<int>(in_width / requested_aspect)); |
| 175 | } |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 176 | const Fraction scale = |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 177 | FindScale(*cropped_width * *cropped_height, max_pixel_count, step_up_); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 178 | // Adjust cropping slightly to get even integer output size and a perfect |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 179 | // scale factor. Make sure the resulting dimensions are aligned correctly |
| 180 | // to be nice to hardware encoders. |
| 181 | *cropped_width = |
| 182 | roundUp(*cropped_width, |
| 183 | scale.denominator * required_resolution_alignment_, in_width); |
| 184 | *cropped_height = |
| 185 | roundUp(*cropped_height, |
| 186 | scale.denominator * required_resolution_alignment_, in_height); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 187 | RTC_DCHECK_EQ(0, *cropped_width % scale.denominator); |
| 188 | RTC_DCHECK_EQ(0, *cropped_height % scale.denominator); |
| 189 | |
| 190 | // Calculate final output size. |
| 191 | *out_width = *cropped_width / scale.denominator * scale.numerator; |
| 192 | *out_height = *cropped_height / scale.denominator * scale.numerator; |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 193 | RTC_DCHECK_EQ(0, *out_height % required_resolution_alignment_); |
| 194 | RTC_DCHECK_EQ(0, *out_height % required_resolution_alignment_); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 195 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 196 | ++frames_out_; |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 197 | if (scale.numerator != scale.denominator) |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 198 | ++frames_scaled_; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 199 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 200 | if (previous_width_ && (previous_width_ != *out_width || |
| 201 | previous_height_ != *out_height)) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 202 | ++adaption_changes_; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 203 | LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ << " / out " |
| 204 | << frames_out_ << " / in " << frames_in_ |
| 205 | << " Changes: " << adaption_changes_ << " Input: " << in_width |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 206 | << "x" << in_height |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 207 | << " Scale: " << scale.numerator << "/" << scale.denominator |
| 208 | << " Output: " << *out_width << "x" << *out_height << " i" |
| 209 | << (requested_format_ ? requested_format_->interval : 0); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 210 | } |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 211 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 212 | previous_width_ = *out_width; |
| 213 | previous_height_ = *out_height; |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 214 | |
| 215 | return true; |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 218 | void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) { |
| 219 | rtc::CritScope cs(&critical_section_); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 220 | requested_format_ = rtc::Optional<VideoFormat>(format); |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 221 | next_frame_timestamp_ns_ = rtc::Optional<int64_t>(); |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 224 | void VideoAdapter::OnResolutionRequest( |
perkj | 2d5f091 | 2016-02-29 00:04:41 -0800 | [diff] [blame] | 225 | rtc::Optional<int> max_pixel_count, |
| 226 | rtc::Optional<int> max_pixel_count_step_up) { |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 227 | rtc::CritScope cs(&critical_section_); |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame^] | 228 | resolution_request_max_pixel_count_ = max_pixel_count.value_or( |
| 229 | max_pixel_count_step_up.value_or(std::numeric_limits<int>::max())); |
| 230 | step_up_ = static_cast<bool>(max_pixel_count_step_up); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | } // namespace cricket |