blob: 8bc9b0a50fe3ecd043a2dcbb7383f39893abdf4b [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.org5f93d0a2015-01-20 21:36:13 +00009 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000010
kjellandera96e2d72016-02-04 23:52:28 -080011#include "webrtc/media/base/videoadapter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000013#include <algorithm>
kthelgason710c3352016-12-08 02:12:31 -080014#include <cmath>
magjed604abe02016-05-19 06:05:40 -070015#include <cstdlib>
Per766ad3b2016-04-05 15:23:49 +020016#include <limits>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017
kthelgason710c3352016-12-08 02:12:31 -080018#include "webrtc/base/arraysize.h"
magjed709f73c2016-05-13 10:26:00 -070019#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000020#include "webrtc/base/logging.h"
kthelgason710c3352016-12-08 02:12:31 -080021#include "webrtc/base/optional.h"
kjellanderf4752772016-03-02 05:42:30 -080022#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080023#include "webrtc/media/base/videocommon.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
Per766ad3b2016-04-05 15:23:49 +020025namespace {
magjed709f73c2016-05-13 10:26:00 -070026struct Fraction {
27 int numerator;
28 int denominator;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029};
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030
magjed709f73c2016-05-13 10:26:00 -070031// Scale factors optimized for in libYUV that we accept.
32// Must be sorted in decreasing scale factors for FindScaleLargerThan to work.
33const Fraction kScaleFractions[] = {
34 {1, 1},
35 {3, 4},
36 {1, 2},
37 {3, 8},
38 {1, 4},
39 {3, 16},
40};
41
kthelgason710c3352016-12-08 02:12:31 -080042// Round |value_to_round| to a multiple of |multiple|. Prefer rounding upwards,
43// but never more than |max_value|.
44int roundUp(int value_to_round, int multiple, int max_value) {
45 const int rounded_value =
46 (value_to_round + multiple - 1) / multiple * multiple;
47 return rounded_value <= max_value ? rounded_value
48 : (max_value / multiple * multiple);
magjed709f73c2016-05-13 10:26:00 -070049}
50
kthelgason710c3352016-12-08 02:12:31 -080051// Generates a scale factor that makes |input_num_pixels| smaller than
52// |target_num_pixels|. This should only be used after making sure none
53// of the optimized factors are small enough.
magjed709f73c2016-05-13 10:26:00 -070054Fraction FindScaleLessThanOrEqual(int input_num_pixels, int target_num_pixels) {
kthelgason710c3352016-12-08 02:12:31 -080055 // Start searching from the last of the optimal fractions;
56 Fraction best_scale = kScaleFractions[arraysize(kScaleFractions) - 1];
57 const float target_scale =
58 sqrt(target_num_pixels / static_cast<float>(input_num_pixels));
59 do {
60 if (best_scale.numerator % 3 == 0 && best_scale.denominator % 2 == 0) {
61 // Multiply by 2/3
62 best_scale.numerator /= 3;
63 best_scale.denominator /= 2;
64 } else {
65 // Multiply by 3/4
66 best_scale.numerator *= 3;
67 best_scale.denominator *= 4;
68 }
69 } while (best_scale.numerator > (target_scale * best_scale.denominator));
70 return best_scale;
71}
72
73rtc::Optional<Fraction> FindOptimizedScaleLessThanOrEqual(
74 int input_num_pixels,
75 int target_num_pixels) {
Per766ad3b2016-04-05 15:23:49 +020076 float best_distance = std::numeric_limits<float>::max();
kthelgason710c3352016-12-08 02:12:31 -080077 rtc::Optional<Fraction> best_scale;
magjed709f73c2016-05-13 10:26:00 -070078 for (const auto& fraction : kScaleFractions) {
79 const float scale =
80 fraction.numerator / static_cast<float>(fraction.denominator);
81 float test_num_pixels = input_num_pixels * scale * scale;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000082 float diff = target_num_pixels - test_num_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 if (diff < 0) {
Per766ad3b2016-04-05 15:23:49 +020084 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 }
86 if (diff < best_distance) {
87 best_distance = diff;
kthelgason710c3352016-12-08 02:12:31 -080088 best_scale = rtc::Optional<Fraction>(fraction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 if (best_distance == 0) { // Found exact match.
90 break;
91 }
92 }
93 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000094 return best_scale;
95}
96
kthelgason710c3352016-12-08 02:12:31 -080097Fraction FindOptimizedScaleLargerThan(int input_num_pixels,
98 int target_num_pixels,
99 int* resulting_number_of_pixels) {
Per766ad3b2016-04-05 15:23:49 +0200100 float best_distance = std::numeric_limits<float>::max();
magjed709f73c2016-05-13 10:26:00 -0700101 Fraction best_scale = {1, 1}; // Default to unscaled if nothing matches.
102 // Default to input number of pixels.
103 float best_number_of_pixels = input_num_pixels;
104 for (const auto& fraction : kScaleFractions) {
105 const float scale =
106 fraction.numerator / static_cast<float>(fraction.denominator);
107 float test_num_pixels = input_num_pixels * scale * scale;
Per766ad3b2016-04-05 15:23:49 +0200108 float diff = test_num_pixels - target_num_pixels;
109 if (diff <= 0) {
110 break;
111 }
112 if (diff < best_distance) {
113 best_distance = diff;
magjed709f73c2016-05-13 10:26:00 -0700114 best_scale = fraction;
Per766ad3b2016-04-05 15:23:49 +0200115 best_number_of_pixels = test_num_pixels;
116 }
117 }
118
119 *resulting_number_of_pixels = static_cast<int>(best_number_of_pixels + .5f);
120 return best_scale;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000121}
122
kthelgason710c3352016-12-08 02:12:31 -0800123rtc::Optional<Fraction> FindOptimizedScale(int input_num_pixels,
124 int max_pixel_count_step_up,
125 int max_pixel_count) {
magjed709f73c2016-05-13 10:26:00 -0700126 // Try scale just above |max_pixel_count_step_up_|.
127 if (max_pixel_count_step_up > 0) {
128 int resulting_pixel_count;
kthelgason710c3352016-12-08 02:12:31 -0800129 const Fraction scale = FindOptimizedScaleLargerThan(
magjed709f73c2016-05-13 10:26:00 -0700130 input_num_pixels, max_pixel_count_step_up, &resulting_pixel_count);
131 if (resulting_pixel_count <= max_pixel_count)
kthelgason710c3352016-12-08 02:12:31 -0800132 return rtc::Optional<Fraction>(scale);
magjed709f73c2016-05-13 10:26:00 -0700133 }
134 // Return largest scale below |max_pixel_count|.
kthelgason710c3352016-12-08 02:12:31 -0800135 return FindOptimizedScaleLessThanOrEqual(input_num_pixels, max_pixel_count);
magjed709f73c2016-05-13 10:26:00 -0700136}
137
kthelgason710c3352016-12-08 02:12:31 -0800138Fraction FindScale(int input_num_pixels,
139 int max_pixel_count_step_up,
140 int max_pixel_count) {
141 const rtc::Optional<Fraction> optimized_scale = FindOptimizedScale(
142 input_num_pixels, max_pixel_count_step_up, max_pixel_count);
143 if (optimized_scale)
144 return *optimized_scale;
145 return FindScaleLessThanOrEqual(input_num_pixels, max_pixel_count);
146}
Per766ad3b2016-04-05 15:23:49 +0200147} // namespace
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148
Per766ad3b2016-04-05 15:23:49 +0200149namespace cricket {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150
kthelgason710c3352016-12-08 02:12:31 -0800151VideoAdapter::VideoAdapter(int required_resolution_alignment)
magjed709f73c2016-05-13 10:26:00 -0700152 : frames_in_(0),
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000153 frames_out_(0),
154 frames_scaled_(0),
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000155 adaption_changes_(0),
magjed@webrtc.orga73d7462014-11-14 13:25:25 +0000156 previous_width_(0),
157 previous_height_(0),
kthelgason710c3352016-12-08 02:12:31 -0800158 required_resolution_alignment_(required_resolution_alignment),
magjed709f73c2016-05-13 10:26:00 -0700159 resolution_request_max_pixel_count_(std::numeric_limits<int>::max()),
160 resolution_request_max_pixel_count_step_up_(0) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161
kthelgason710c3352016-12-08 02:12:31 -0800162VideoAdapter::VideoAdapter() : VideoAdapter(1) {}
163
Per766ad3b2016-04-05 15:23:49 +0200164VideoAdapter::~VideoAdapter() {}
165
magjed604abe02016-05-19 06:05:40 -0700166bool VideoAdapter::KeepFrame(int64_t in_timestamp_ns) {
Per766ad3b2016-04-05 15:23:49 +0200167 rtc::CritScope cs(&critical_section_);
magjed604abe02016-05-19 06:05:40 -0700168 if (!requested_format_ || requested_format_->interval == 0)
169 return true;
170
171 if (next_frame_timestamp_ns_) {
172 // Time until next frame should be outputted.
173 const int64_t time_until_next_frame_ns =
174 (*next_frame_timestamp_ns_ - in_timestamp_ns);
175
176 // Continue if timestamp is withing expected range.
177 if (std::abs(time_until_next_frame_ns) < 2 * requested_format_->interval) {
178 // Drop if a frame shouldn't be outputted yet.
179 if (time_until_next_frame_ns > 0)
180 return false;
181 // Time to output new frame.
182 *next_frame_timestamp_ns_ += requested_format_->interval;
183 return true;
184 }
185 }
186
187 // First timestamp received or timestamp is way outside expected range, so
188 // reset. Set first timestamp target to just half the interval to prefer
189 // keeping frames in case of jitter.
190 next_frame_timestamp_ns_ =
191 rtc::Optional<int64_t>(in_timestamp_ns + requested_format_->interval / 2);
192 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193}
194
nisse47ac4622016-05-25 08:47:01 -0700195bool VideoAdapter::AdaptFrameResolution(int in_width,
magjed709f73c2016-05-13 10:26:00 -0700196 int in_height,
magjed604abe02016-05-19 06:05:40 -0700197 int64_t in_timestamp_ns,
magjed709f73c2016-05-13 10:26:00 -0700198 int* cropped_width,
199 int* cropped_height,
200 int* out_width,
201 int* out_height) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000202 rtc::CritScope cs(&critical_section_);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000203 ++frames_in_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204
magjed709f73c2016-05-13 10:26:00 -0700205 // The max output pixel count is the minimum of the requests from
206 // OnOutputFormatRequest and OnResolutionRequest.
207 int max_pixel_count = resolution_request_max_pixel_count_;
208 if (requested_format_) {
209 max_pixel_count = std::min(
210 max_pixel_count, requested_format_->width * requested_format_->height);
211 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212
213 // Drop the input frame if necessary.
magjed604abe02016-05-19 06:05:40 -0700214 if (max_pixel_count == 0 || !KeepFrame(in_timestamp_ns)) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000215 // Show VAdapt log every 90 frames dropped. (3 seconds)
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000216 if ((frames_in_ - frames_out_) % 90 == 0) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000217 // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed
218 // in default calls.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000219 LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_
220 << " / out " << frames_out_
221 << " / in " << frames_in_
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000222 << " Changes: " << adaption_changes_
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +0000223 << " Input: " << in_width
224 << "x" << in_height
magjed604abe02016-05-19 06:05:40 -0700225 << " timestamp: " << in_timestamp_ns
magjed709f73c2016-05-13 10:26:00 -0700226 << " Output: i"
227 << (requested_format_ ? requested_format_->interval : 0);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000228 }
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +0000229
magjed709f73c2016-05-13 10:26:00 -0700230 // Drop frame.
nisse47ac4622016-05-25 08:47:01 -0700231 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 }
233
magjed709f73c2016-05-13 10:26:00 -0700234 // Calculate how the input should be cropped.
235 if (!requested_format_ ||
236 requested_format_->width == 0 || requested_format_->height == 0) {
237 *cropped_width = in_width;
238 *cropped_height = in_height;
239 } else {
240 // Adjust |requested_format_| orientation to match input.
241 if ((in_width > in_height) !=
242 (requested_format_->width > requested_format_->height)) {
243 std::swap(requested_format_->width, requested_format_->height);
244 }
245 const float requested_aspect =
246 requested_format_->width /
247 static_cast<float>(requested_format_->height);
248 *cropped_width =
249 std::min(in_width, static_cast<int>(in_height * requested_aspect));
250 *cropped_height =
251 std::min(in_height, static_cast<int>(in_width / requested_aspect));
252 }
magjed709f73c2016-05-13 10:26:00 -0700253 const Fraction scale =
254 FindScale(*cropped_width * *cropped_height,
255 resolution_request_max_pixel_count_step_up_, max_pixel_count);
magjed709f73c2016-05-13 10:26:00 -0700256 // Adjust cropping slightly to get even integer output size and a perfect
kthelgason710c3352016-12-08 02:12:31 -0800257 // scale factor. Make sure the resulting dimensions are aligned correctly
258 // to be nice to hardware encoders.
259 *cropped_width =
260 roundUp(*cropped_width,
261 scale.denominator * required_resolution_alignment_, in_width);
262 *cropped_height =
263 roundUp(*cropped_height,
264 scale.denominator * required_resolution_alignment_, in_height);
magjed709f73c2016-05-13 10:26:00 -0700265 RTC_DCHECK_EQ(0, *cropped_width % scale.denominator);
266 RTC_DCHECK_EQ(0, *cropped_height % scale.denominator);
267
268 // Calculate final output size.
269 *out_width = *cropped_width / scale.denominator * scale.numerator;
270 *out_height = *cropped_height / scale.denominator * scale.numerator;
kthelgason710c3352016-12-08 02:12:31 -0800271 RTC_DCHECK_EQ(0, *out_height % required_resolution_alignment_);
272 RTC_DCHECK_EQ(0, *out_height % required_resolution_alignment_);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000273
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000274 ++frames_out_;
magjed709f73c2016-05-13 10:26:00 -0700275 if (scale.numerator != scale.denominator)
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000276 ++frames_scaled_;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000277
magjed709f73c2016-05-13 10:26:00 -0700278 if (previous_width_ && (previous_width_ != *out_width ||
279 previous_height_ != *out_height)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000280 ++adaption_changes_;
Per766ad3b2016-04-05 15:23:49 +0200281 LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ << " / out "
282 << frames_out_ << " / in " << frames_in_
283 << " Changes: " << adaption_changes_ << " Input: " << in_width
magjed604abe02016-05-19 06:05:40 -0700284 << "x" << in_height
magjed709f73c2016-05-13 10:26:00 -0700285 << " Scale: " << scale.numerator << "/" << scale.denominator
286 << " Output: " << *out_width << "x" << *out_height << " i"
287 << (requested_format_ ? requested_format_->interval : 0);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000288 }
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +0000289
magjed709f73c2016-05-13 10:26:00 -0700290 previous_width_ = *out_width;
291 previous_height_ = *out_height;
nisse47ac4622016-05-25 08:47:01 -0700292
293 return true;
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +0000294}
295
Per766ad3b2016-04-05 15:23:49 +0200296void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) {
297 rtc::CritScope cs(&critical_section_);
magjed709f73c2016-05-13 10:26:00 -0700298 requested_format_ = rtc::Optional<VideoFormat>(format);
magjed604abe02016-05-19 06:05:40 -0700299 next_frame_timestamp_ns_ = rtc::Optional<int64_t>();
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000300}
301
Per766ad3b2016-04-05 15:23:49 +0200302void VideoAdapter::OnResolutionRequest(
perkj2d5f0912016-02-29 00:04:41 -0800303 rtc::Optional<int> max_pixel_count,
304 rtc::Optional<int> max_pixel_count_step_up) {
Per766ad3b2016-04-05 15:23:49 +0200305 rtc::CritScope cs(&critical_section_);
306 resolution_request_max_pixel_count_ =
307 max_pixel_count.value_or(std::numeric_limits<int>::max());
magjed709f73c2016-05-13 10:26:00 -0700308 resolution_request_max_pixel_count_step_up_ =
309 max_pixel_count_step_up.value_or(0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310}
311
312} // namespace cricket