blob: acb0e2c1a19888d758cd2d46893fca9e2b93a477 [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>
magjed604abe02016-05-19 06:05:40 -070014#include <cstdlib>
Per766ad3b2016-04-05 15:23:49 +020015#include <limits>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
magjed709f73c2016-05-13 10:26:00 -070017#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000018#include "webrtc/base/logging.h"
kjellanderf4752772016-03-02 05:42:30 -080019#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080020#include "webrtc/media/base/videocommon.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
Per766ad3b2016-04-05 15:23:49 +020022namespace {
kthelgason7722a4c2016-12-08 02:18:25 -080023
magjed709f73c2016-05-13 10:26:00 -070024struct Fraction {
25 int numerator;
26 int denominator;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027};
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
magjed709f73c2016-05-13 10:26:00 -070029// Scale factors optimized for in libYUV that we accept.
30// Must be sorted in decreasing scale factors for FindScaleLargerThan to work.
31const Fraction kScaleFractions[] = {
32 {1, 1},
33 {3, 4},
34 {1, 2},
35 {3, 8},
36 {1, 4},
37 {3, 16},
38};
39
kthelgason7722a4c2016-12-08 02:18:25 -080040// Round |valueToRound| to a multiple of |multiple|. Prefer rounding upwards,
41// but never more than |maxValue|.
42int 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);
magjed709f73c2016-05-13 10:26:00 -070046}
47
48Fraction FindScaleLessThanOrEqual(int input_num_pixels, int target_num_pixels) {
Per766ad3b2016-04-05 15:23:49 +020049 float best_distance = std::numeric_limits<float>::max();
kthelgason7722a4c2016-12-08 02:18:25 -080050 Fraction best_scale = {0, 1}; // Default to 0 if nothing matches.
magjed709f73c2016-05-13 10:26:00 -070051 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.orgcadf9042013-08-30 21:24:16 +000055 float diff = target_num_pixels - test_num_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 if (diff < 0) {
Per766ad3b2016-04-05 15:23:49 +020057 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 }
59 if (diff < best_distance) {
60 best_distance = diff;
kthelgason7722a4c2016-12-08 02:18:25 -080061 best_scale = fraction;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 if (best_distance == 0) { // Found exact match.
63 break;
64 }
65 }
66 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000067 return best_scale;
68}
69
kthelgason7722a4c2016-12-08 02:18:25 -080070Fraction FindScaleLargerThan(int input_num_pixels,
71 int target_num_pixels,
72 int* resulting_number_of_pixels) {
Per766ad3b2016-04-05 15:23:49 +020073 float best_distance = std::numeric_limits<float>::max();
magjed709f73c2016-05-13 10:26:00 -070074 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;
Per766ad3b2016-04-05 15:23:49 +020081 float diff = test_num_pixels - target_num_pixels;
82 if (diff <= 0) {
83 break;
84 }
85 if (diff < best_distance) {
86 best_distance = diff;
magjed709f73c2016-05-13 10:26:00 -070087 best_scale = fraction;
Per766ad3b2016-04-05 15:23:49 +020088 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.orgcadf9042013-08-30 21:24:16 +000094}
95
kthelgason710c3352016-12-08 02:12:31 -080096Fraction FindScale(int input_num_pixels,
97 int max_pixel_count_step_up,
98 int max_pixel_count) {
kthelgason7722a4c2016-12-08 02:18:25 -080099 // 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|.
kthelgason710c3352016-12-08 02:12:31 -0800108 return FindScaleLessThanOrEqual(input_num_pixels, max_pixel_count);
109}
kthelgason7722a4c2016-12-08 02:18:25 -0800110
Per766ad3b2016-04-05 15:23:49 +0200111} // namespace
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
Per766ad3b2016-04-05 15:23:49 +0200113namespace cricket {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114
kthelgason7722a4c2016-12-08 02:18:25 -0800115VideoAdapter::VideoAdapter()
magjed709f73c2016-05-13 10:26:00 -0700116 : frames_in_(0),
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000117 frames_out_(0),
118 frames_scaled_(0),
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000119 adaption_changes_(0),
magjed@webrtc.orga73d7462014-11-14 13:25:25 +0000120 previous_width_(0),
121 previous_height_(0),
magjed709f73c2016-05-13 10:26:00 -0700122 resolution_request_max_pixel_count_(std::numeric_limits<int>::max()),
123 resolution_request_max_pixel_count_step_up_(0) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124
Per766ad3b2016-04-05 15:23:49 +0200125VideoAdapter::~VideoAdapter() {}
126
magjed604abe02016-05-19 06:05:40 -0700127bool VideoAdapter::KeepFrame(int64_t in_timestamp_ns) {
Per766ad3b2016-04-05 15:23:49 +0200128 rtc::CritScope cs(&critical_section_);
magjed604abe02016-05-19 06:05:40 -0700129 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.org28e20752013-07-10 00:45:36 +0000154}
155
nisse47ac4622016-05-25 08:47:01 -0700156bool VideoAdapter::AdaptFrameResolution(int in_width,
magjed709f73c2016-05-13 10:26:00 -0700157 int in_height,
magjed604abe02016-05-19 06:05:40 -0700158 int64_t in_timestamp_ns,
magjed709f73c2016-05-13 10:26:00 -0700159 int* cropped_width,
160 int* cropped_height,
161 int* out_width,
162 int* out_height) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000163 rtc::CritScope cs(&critical_section_);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000164 ++frames_in_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165
magjed709f73c2016-05-13 10:26:00 -0700166 // 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.org28e20752013-07-10 00:45:36 +0000173
174 // Drop the input frame if necessary.
magjed604abe02016-05-19 06:05:40 -0700175 if (max_pixel_count == 0 || !KeepFrame(in_timestamp_ns)) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000176 // Show VAdapt log every 90 frames dropped. (3 seconds)
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000177 if ((frames_in_ - frames_out_) % 90 == 0) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000178 // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed
179 // in default calls.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000180 LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_
181 << " / out " << frames_out_
182 << " / in " << frames_in_
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000183 << " Changes: " << adaption_changes_
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +0000184 << " Input: " << in_width
185 << "x" << in_height
magjed604abe02016-05-19 06:05:40 -0700186 << " timestamp: " << in_timestamp_ns
magjed709f73c2016-05-13 10:26:00 -0700187 << " Output: i"
188 << (requested_format_ ? requested_format_->interval : 0);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000189 }
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +0000190
magjed709f73c2016-05-13 10:26:00 -0700191 // Drop frame.
nisse47ac4622016-05-25 08:47:01 -0700192 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 }
194
magjed709f73c2016-05-13 10:26:00 -0700195 // Calculate how the input should be cropped.
196 if (!requested_format_ ||
197 requested_format_->width == 0 || requested_format_->height == 0) {
198 *cropped_width = in_width;
199 *cropped_height = in_height;
200 } else {
201 // Adjust |requested_format_| orientation to match input.
202 if ((in_width > in_height) !=
203 (requested_format_->width > requested_format_->height)) {
204 std::swap(requested_format_->width, requested_format_->height);
205 }
206 const float requested_aspect =
207 requested_format_->width /
208 static_cast<float>(requested_format_->height);
209 *cropped_width =
210 std::min(in_width, static_cast<int>(in_height * requested_aspect));
211 *cropped_height =
212 std::min(in_height, static_cast<int>(in_width / requested_aspect));
213 }
kthelgason7722a4c2016-12-08 02:18:25 -0800214
215 // Find best scale factor.
magjed709f73c2016-05-13 10:26:00 -0700216 const Fraction scale =
217 FindScale(*cropped_width * *cropped_height,
218 resolution_request_max_pixel_count_step_up_, max_pixel_count);
kthelgason7722a4c2016-12-08 02:18:25 -0800219
magjed709f73c2016-05-13 10:26:00 -0700220 // Adjust cropping slightly to get even integer output size and a perfect
kthelgason7722a4c2016-12-08 02:18:25 -0800221 // scale factor.
222 *cropped_width = roundUp(*cropped_width, scale.denominator, in_width);
223 *cropped_height = roundUp(*cropped_height, scale.denominator, in_height);
magjed709f73c2016-05-13 10:26:00 -0700224 RTC_DCHECK_EQ(0, *cropped_width % scale.denominator);
225 RTC_DCHECK_EQ(0, *cropped_height % scale.denominator);
226
227 // Calculate final output size.
228 *out_width = *cropped_width / scale.denominator * scale.numerator;
229 *out_height = *cropped_height / scale.denominator * scale.numerator;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000230
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000231 ++frames_out_;
magjed709f73c2016-05-13 10:26:00 -0700232 if (scale.numerator != scale.denominator)
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000233 ++frames_scaled_;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000234
magjed709f73c2016-05-13 10:26:00 -0700235 if (previous_width_ && (previous_width_ != *out_width ||
236 previous_height_ != *out_height)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000237 ++adaption_changes_;
Per766ad3b2016-04-05 15:23:49 +0200238 LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ << " / out "
239 << frames_out_ << " / in " << frames_in_
240 << " Changes: " << adaption_changes_ << " Input: " << in_width
magjed604abe02016-05-19 06:05:40 -0700241 << "x" << in_height
magjed709f73c2016-05-13 10:26:00 -0700242 << " Scale: " << scale.numerator << "/" << scale.denominator
243 << " Output: " << *out_width << "x" << *out_height << " i"
244 << (requested_format_ ? requested_format_->interval : 0);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000245 }
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +0000246
magjed709f73c2016-05-13 10:26:00 -0700247 previous_width_ = *out_width;
248 previous_height_ = *out_height;
nisse47ac4622016-05-25 08:47:01 -0700249
250 return true;
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +0000251}
252
Per766ad3b2016-04-05 15:23:49 +0200253void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) {
254 rtc::CritScope cs(&critical_section_);
magjed709f73c2016-05-13 10:26:00 -0700255 requested_format_ = rtc::Optional<VideoFormat>(format);
magjed604abe02016-05-19 06:05:40 -0700256 next_frame_timestamp_ns_ = rtc::Optional<int64_t>();
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000257}
258
Per766ad3b2016-04-05 15:23:49 +0200259void VideoAdapter::OnResolutionRequest(
perkj2d5f0912016-02-29 00:04:41 -0800260 rtc::Optional<int> max_pixel_count,
261 rtc::Optional<int> max_pixel_count_step_up) {
Per766ad3b2016-04-05 15:23:49 +0200262 rtc::CritScope cs(&critical_section_);
263 resolution_request_max_pixel_count_ =
264 max_pixel_count.value_or(std::numeric_limits<int>::max());
magjed709f73c2016-05-13 10:26:00 -0700265 resolution_request_max_pixel_count_step_up_ =
266 max_pixel_count_step_up.value_or(0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267}
268
269} // namespace cricket