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> |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 14 | #include <cstdlib> |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 15 | #include <limits> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 16 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 17 | #include "webrtc/base/checks.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 18 | #include "webrtc/base/logging.h" |
kjellander | f475277 | 2016-03-02 05:42:30 -0800 | [diff] [blame] | 19 | #include "webrtc/media/base/mediaconstants.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 20 | #include "webrtc/media/base/videocommon.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 22 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 23 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 24 | struct Fraction { |
| 25 | int numerator; |
| 26 | int denominator; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 27 | }; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 28 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 29 | // Scale factors optimized for in libYUV that we accept. |
| 30 | // Must be sorted in decreasing scale factors for FindScaleLargerThan to work. |
| 31 | const Fraction kScaleFractions[] = { |
| 32 | {1, 1}, |
| 33 | {3, 4}, |
| 34 | {1, 2}, |
| 35 | {3, 8}, |
| 36 | {1, 4}, |
| 37 | {3, 16}, |
| 38 | }; |
| 39 | |
| 40 | // Round |valueToRound| to a multiple of |multiple|. Prefer rounding upwards, |
| 41 | // but never more than |maxValue|. |
| 42 | int roundUp(int valueToRound, int multiple, int maxValue) { |
| 43 | const int roundedValue = (valueToRound + multiple - 1) / multiple * multiple; |
| 44 | return roundedValue <= maxValue ? roundedValue |
| 45 | : (maxValue / multiple * multiple); |
| 46 | } |
| 47 | |
| 48 | Fraction FindScaleLessThanOrEqual(int input_num_pixels, int target_num_pixels) { |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 49 | float best_distance = std::numeric_limits<float>::max(); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 50 | Fraction best_scale = {0, 1}; // Default to 0 if nothing matches. |
| 51 | for (const auto& fraction : kScaleFractions) { |
| 52 | const float scale = |
| 53 | fraction.numerator / static_cast<float>(fraction.denominator); |
| 54 | float test_num_pixels = input_num_pixels * scale * scale; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 55 | float diff = target_num_pixels - test_num_pixels; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | if (diff < 0) { |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 57 | continue; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | } |
| 59 | if (diff < best_distance) { |
| 60 | best_distance = diff; |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 61 | best_scale = fraction; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | if (best_distance == 0) { // Found exact match. |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 67 | return best_scale; |
| 68 | } |
| 69 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 70 | Fraction FindScaleLargerThan(int input_num_pixels, |
| 71 | int target_num_pixels, |
| 72 | int* resulting_number_of_pixels) { |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 73 | float best_distance = std::numeric_limits<float>::max(); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 74 | Fraction best_scale = {1, 1}; // Default to unscaled if nothing matches. |
| 75 | // Default to input number of pixels. |
| 76 | float best_number_of_pixels = input_num_pixels; |
| 77 | for (const auto& fraction : kScaleFractions) { |
| 78 | const float scale = |
| 79 | fraction.numerator / static_cast<float>(fraction.denominator); |
| 80 | float test_num_pixels = input_num_pixels * scale * scale; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 81 | float diff = test_num_pixels - target_num_pixels; |
| 82 | if (diff <= 0) { |
| 83 | break; |
| 84 | } |
| 85 | if (diff < best_distance) { |
| 86 | best_distance = diff; |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 87 | best_scale = fraction; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 88 | best_number_of_pixels = test_num_pixels; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | *resulting_number_of_pixels = static_cast<int>(best_number_of_pixels + .5f); |
| 93 | return best_scale; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 94 | } |
| 95 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 96 | Fraction FindScale(int input_num_pixels, |
| 97 | int max_pixel_count_step_up, |
| 98 | int max_pixel_count) { |
| 99 | // Try scale just above |max_pixel_count_step_up_|. |
| 100 | if (max_pixel_count_step_up > 0) { |
| 101 | int resulting_pixel_count; |
| 102 | const Fraction scale = FindScaleLargerThan( |
| 103 | input_num_pixels, max_pixel_count_step_up, &resulting_pixel_count); |
| 104 | if (resulting_pixel_count <= max_pixel_count) |
| 105 | return scale; |
| 106 | } |
| 107 | // Return largest scale below |max_pixel_count|. |
| 108 | return FindScaleLessThanOrEqual(input_num_pixels, max_pixel_count); |
| 109 | } |
| 110 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 111 | } // namespace |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 113 | namespace cricket { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | VideoAdapter::VideoAdapter() |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 116 | : frames_in_(0), |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 117 | frames_out_(0), |
| 118 | frames_scaled_(0), |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 119 | adaption_changes_(0), |
magjed@webrtc.org | a73d746 | 2014-11-14 13:25:25 +0000 | [diff] [blame] | 120 | previous_width_(0), |
| 121 | previous_height_(0), |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 122 | resolution_request_max_pixel_count_(std::numeric_limits<int>::max()), |
| 123 | resolution_request_max_pixel_count_step_up_(0) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 124 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 125 | VideoAdapter::~VideoAdapter() {} |
| 126 | |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 127 | bool VideoAdapter::KeepFrame(int64_t in_timestamp_ns) { |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 128 | rtc::CritScope cs(&critical_section_); |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 129 | if (!requested_format_ || requested_format_->interval == 0) |
| 130 | return true; |
| 131 | |
| 132 | if (next_frame_timestamp_ns_) { |
| 133 | // Time until next frame should be outputted. |
| 134 | const int64_t time_until_next_frame_ns = |
| 135 | (*next_frame_timestamp_ns_ - in_timestamp_ns); |
| 136 | |
| 137 | // Continue if timestamp is withing expected range. |
| 138 | if (std::abs(time_until_next_frame_ns) < 2 * requested_format_->interval) { |
| 139 | // Drop if a frame shouldn't be outputted yet. |
| 140 | if (time_until_next_frame_ns > 0) |
| 141 | return false; |
| 142 | // Time to output new frame. |
| 143 | *next_frame_timestamp_ns_ += requested_format_->interval; |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // First timestamp received or timestamp is way outside expected range, so |
| 149 | // reset. Set first timestamp target to just half the interval to prefer |
| 150 | // keeping frames in case of jitter. |
| 151 | next_frame_timestamp_ns_ = |
| 152 | rtc::Optional<int64_t>(in_timestamp_ns + requested_format_->interval / 2); |
| 153 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 154 | } |
| 155 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 156 | void VideoAdapter::AdaptFrameResolution(int in_width, |
| 157 | int in_height, |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 158 | int64_t in_timestamp_ns, |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 159 | int* cropped_width, |
| 160 | int* cropped_height, |
| 161 | int* out_width, |
| 162 | int* out_height) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 163 | rtc::CritScope cs(&critical_section_); |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 164 | ++frames_in_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 165 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 166 | // The max output pixel count is the minimum of the requests from |
| 167 | // OnOutputFormatRequest and OnResolutionRequest. |
| 168 | int max_pixel_count = resolution_request_max_pixel_count_; |
| 169 | if (requested_format_) { |
| 170 | max_pixel_count = std::min( |
| 171 | max_pixel_count, requested_format_->width * requested_format_->height); |
| 172 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 173 | |
| 174 | // Drop the input frame if necessary. |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 175 | if (max_pixel_count == 0 || !KeepFrame(in_timestamp_ns)) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 176 | // Show VAdapt log every 90 frames dropped. (3 seconds) |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 177 | if ((frames_in_ - frames_out_) % 90 == 0) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 178 | // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed |
| 179 | // in default calls. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 180 | LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_ |
| 181 | << " / out " << frames_out_ |
| 182 | << " / in " << frames_in_ |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 183 | << " Changes: " << adaption_changes_ |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 184 | << " Input: " << in_width |
| 185 | << "x" << in_height |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 186 | << " timestamp: " << in_timestamp_ns |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 187 | << " Output: i" |
| 188 | << (requested_format_ ? requested_format_->interval : 0); |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 189 | } |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 190 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 191 | // Drop frame. |
| 192 | *cropped_width = 0; |
| 193 | *cropped_height = 0; |
| 194 | *out_width = 0; |
| 195 | *out_height = 0; |
| 196 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 197 | } |
| 198 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 199 | // Calculate how the input should be cropped. |
| 200 | if (!requested_format_ || |
| 201 | requested_format_->width == 0 || requested_format_->height == 0) { |
| 202 | *cropped_width = in_width; |
| 203 | *cropped_height = in_height; |
| 204 | } else { |
| 205 | // Adjust |requested_format_| orientation to match input. |
| 206 | if ((in_width > in_height) != |
| 207 | (requested_format_->width > requested_format_->height)) { |
| 208 | std::swap(requested_format_->width, requested_format_->height); |
| 209 | } |
| 210 | const float requested_aspect = |
| 211 | requested_format_->width / |
| 212 | static_cast<float>(requested_format_->height); |
| 213 | *cropped_width = |
| 214 | std::min(in_width, static_cast<int>(in_height * requested_aspect)); |
| 215 | *cropped_height = |
| 216 | std::min(in_height, static_cast<int>(in_width / requested_aspect)); |
| 217 | } |
| 218 | |
| 219 | // Find best scale factor. |
| 220 | const Fraction scale = |
| 221 | FindScale(*cropped_width * *cropped_height, |
| 222 | resolution_request_max_pixel_count_step_up_, max_pixel_count); |
| 223 | |
| 224 | // Adjust cropping slightly to get even integer output size and a perfect |
| 225 | // scale factor. |
| 226 | *cropped_width = roundUp(*cropped_width, scale.denominator, in_width); |
| 227 | *cropped_height = roundUp(*cropped_height, scale.denominator, in_height); |
| 228 | RTC_DCHECK_EQ(0, *cropped_width % scale.denominator); |
| 229 | RTC_DCHECK_EQ(0, *cropped_height % scale.denominator); |
| 230 | |
| 231 | // Calculate final output size. |
| 232 | *out_width = *cropped_width / scale.denominator * scale.numerator; |
| 233 | *out_height = *cropped_height / scale.denominator * scale.numerator; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 234 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 235 | ++frames_out_; |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 236 | if (scale.numerator != scale.denominator) |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 237 | ++frames_scaled_; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 238 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 239 | if (previous_width_ && (previous_width_ != *out_width || |
| 240 | previous_height_ != *out_height)) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 241 | ++adaption_changes_; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 242 | LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ << " / out " |
| 243 | << frames_out_ << " / in " << frames_in_ |
| 244 | << " Changes: " << adaption_changes_ << " Input: " << in_width |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 245 | << "x" << in_height |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 246 | << " Scale: " << scale.numerator << "/" << scale.denominator |
| 247 | << " Output: " << *out_width << "x" << *out_height << " i" |
| 248 | << (requested_format_ ? requested_format_->interval : 0); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 249 | } |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 250 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 251 | previous_width_ = *out_width; |
| 252 | previous_height_ = *out_height; |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 255 | void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) { |
| 256 | rtc::CritScope cs(&critical_section_); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 257 | requested_format_ = rtc::Optional<VideoFormat>(format); |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 258 | next_frame_timestamp_ns_ = rtc::Optional<int64_t>(); |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 261 | void VideoAdapter::OnResolutionRequest( |
perkj | 2d5f091 | 2016-02-29 00:04:41 -0800 | [diff] [blame] | 262 | rtc::Optional<int> max_pixel_count, |
| 263 | rtc::Optional<int> max_pixel_count_step_up) { |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 264 | rtc::CritScope cs(&critical_section_); |
| 265 | resolution_request_max_pixel_count_ = |
| 266 | max_pixel_count.value_or(std::numeric_limits<int>::max()); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 267 | resolution_request_max_pixel_count_step_up_ = |
| 268 | max_pixel_count_step_up.value_or(0); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | } // namespace cricket |