blob: e1f506f10b0eec15e66930959d40df9a1d6c94f4 [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 {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023
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
40// 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);
46}
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();
magjed709f73c2016-05-13 10:26:00 -070050 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.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;
magjed709f73c2016-05-13 10:26:00 -070061 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
magjed709f73c2016-05-13 10:26:00 -070070Fraction 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
magjed709f73c2016-05-13 10:26:00 -070096Fraction 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
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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115VideoAdapter::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
magjed709f73c2016-05-13 10:26:00 -0700156void VideoAdapter::AdaptFrameResolution(int in_width,
157 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.
192 *cropped_width = 0;
193 *cropped_height = 0;
194 *out_width = 0;
195 *out_height = 0;
196 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 }
198
magjed709f73c2016-05-13 10:26:00 -0700199 // 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.orgcadf9042013-08-30 21:24:16 +0000234
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000235 ++frames_out_;
magjed709f73c2016-05-13 10:26:00 -0700236 if (scale.numerator != scale.denominator)
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000237 ++frames_scaled_;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000238
magjed709f73c2016-05-13 10:26:00 -0700239 if (previous_width_ && (previous_width_ != *out_width ||
240 previous_height_ != *out_height)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000241 ++adaption_changes_;
Per766ad3b2016-04-05 15:23:49 +0200242 LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ << " / out "
243 << frames_out_ << " / in " << frames_in_
244 << " Changes: " << adaption_changes_ << " Input: " << in_width
magjed604abe02016-05-19 06:05:40 -0700245 << "x" << in_height
magjed709f73c2016-05-13 10:26:00 -0700246 << " Scale: " << scale.numerator << "/" << scale.denominator
247 << " Output: " << *out_width << "x" << *out_height << " i"
248 << (requested_format_ ? requested_format_->interval : 0);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000249 }
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +0000250
magjed709f73c2016-05-13 10:26:00 -0700251 previous_width_ = *out_width;
252 previous_height_ = *out_height;
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +0000253}
254
Per766ad3b2016-04-05 15:23:49 +0200255void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) {
256 rtc::CritScope cs(&critical_section_);
magjed709f73c2016-05-13 10:26:00 -0700257 requested_format_ = rtc::Optional<VideoFormat>(format);
magjed604abe02016-05-19 06:05:40 -0700258 next_frame_timestamp_ns_ = rtc::Optional<int64_t>();
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000259}
260
Per766ad3b2016-04-05 15:23:49 +0200261void VideoAdapter::OnResolutionRequest(
perkj2d5f0912016-02-29 00:04:41 -0800262 rtc::Optional<int> max_pixel_count,
263 rtc::Optional<int> max_pixel_count_step_up) {
Per766ad3b2016-04-05 15:23:49 +0200264 rtc::CritScope cs(&critical_section_);
265 resolution_request_max_pixel_count_ =
266 max_pixel_count.value_or(std::numeric_limits<int>::max());
magjed709f73c2016-05-13 10:26:00 -0700267 resolution_request_max_pixel_count_step_up_ =
268 max_pixel_count_step_up.value_or(0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269}
270
271} // namespace cricket