2015-01-20 21:36:13 +00:00
|
|
|
/*
|
2016-02-07 20:46:45 -08:00
|
|
|
* Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
|
2015-01-20 21:36:13 +00:00
|
|
|
*
|
2016-02-07 20:46:45 -08:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2015-01-20 21:36:13 +00:00
|
|
|
*/
|
2013-07-10 00:45:36 +00:00
|
|
|
|
Move talk/media to webrtc/media
I removed the 'libjingle' target in talk/libjingle.gyp and replaced
all users of it with base/base.gyp:rtc_base. It seems the jsoncpp
and expat dependencies were not used by it's previous references.
The files in talk/media/testdata were uploaded to Google Storage and
added .sha1 files in resources/media instead of simply moving them.
The previously disabled warnings that were inherited from
talk/build/common.gypi are now replaced by target-specific disabling
of only the failing warnings. Additional disabling was needed since the stricter
compilation warnings that applies to code in webrtc/.
License headers will be updated in a follow-up CL in order to not
break Git history.
Other modifications:
* Updated the header guards.
* Sorted the includes using chromium/src/tools/sort-headers.py
except for these files:
talk/app/webrtc/peerconnectionendtoend_unittest.cc
talk/app/webrtc/java/jni/androidmediadecoder_jni.cc
talk/app/webrtc/java/jni/androidmediaencoder_jni.cc
webrtc/media/devices/win32devicemanager.cc.
* Unused GYP reference to libjingle_tests_additional_deps was removed.
* Removed duplicated GYP entries of
webrtc/base/testutils.cc
webrtc/base/testutils.h
The HAVE_WEBRTC_VIDEO and HAVE_WEBRTC_VOICE defines were used by only talk/media,
so they were moved to the media.gyp.
I also checked that none of
EXPAT_RELATIVE_PATH,
FEATURE_ENABLE_VOICEMAIL,
GTEST_RELATIVE_PATH,
JSONCPP_RELATIVE_PATH,
LOGGING=1,
SRTP_RELATIVE_PATH,
FEATURE_ENABLE_SSL,
FEATURE_ENABLE_VOICEMAIL,
FEATURE_ENABLE_PSTN,
HAVE_SCTP,
HAVE_SRTP,
are used by the talk/media code.
For Chromium, the following changes will need to be applied to the roll CL that updates the
DEPS for WebRTC and libjingle: https://codereview.chromium.org/1604303002/
BUG=webrtc:5420
NOPRESUBMIT=True
TBR=tommi@webrtc.org
Review URL: https://codereview.webrtc.org/1587193006
Cr-Commit-Position: refs/heads/master@{#11495}
2016-02-04 23:52:28 -08:00
|
|
|
#include "webrtc/media/base/videoadapter.h"
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2015-02-12 11:54:26 +00:00
|
|
|
#include <algorithm>
|
2016-12-08 08:04:51 -08:00
|
|
|
#include <cmath>
|
2016-05-19 06:05:40 -07:00
|
|
|
#include <cstdlib>
|
2016-04-05 15:23:49 +02:00
|
|
|
#include <limits>
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2016-12-08 08:04:51 -08:00
|
|
|
#include "webrtc/base/arraysize.h"
|
2016-05-13 10:26:00 -07:00
|
|
|
#include "webrtc/base/checks.h"
|
2014-08-13 17:26:08 +00:00
|
|
|
#include "webrtc/base/logging.h"
|
2016-12-08 08:04:51 -08:00
|
|
|
#include "webrtc/base/optional.h"
|
2016-03-02 05:42:30 -08:00
|
|
|
#include "webrtc/media/base/mediaconstants.h"
|
Move talk/media to webrtc/media
I removed the 'libjingle' target in talk/libjingle.gyp and replaced
all users of it with base/base.gyp:rtc_base. It seems the jsoncpp
and expat dependencies were not used by it's previous references.
The files in talk/media/testdata were uploaded to Google Storage and
added .sha1 files in resources/media instead of simply moving them.
The previously disabled warnings that were inherited from
talk/build/common.gypi are now replaced by target-specific disabling
of only the failing warnings. Additional disabling was needed since the stricter
compilation warnings that applies to code in webrtc/.
License headers will be updated in a follow-up CL in order to not
break Git history.
Other modifications:
* Updated the header guards.
* Sorted the includes using chromium/src/tools/sort-headers.py
except for these files:
talk/app/webrtc/peerconnectionendtoend_unittest.cc
talk/app/webrtc/java/jni/androidmediadecoder_jni.cc
talk/app/webrtc/java/jni/androidmediaencoder_jni.cc
webrtc/media/devices/win32devicemanager.cc.
* Unused GYP reference to libjingle_tests_additional_deps was removed.
* Removed duplicated GYP entries of
webrtc/base/testutils.cc
webrtc/base/testutils.h
The HAVE_WEBRTC_VIDEO and HAVE_WEBRTC_VOICE defines were used by only talk/media,
so they were moved to the media.gyp.
I also checked that none of
EXPAT_RELATIVE_PATH,
FEATURE_ENABLE_VOICEMAIL,
GTEST_RELATIVE_PATH,
JSONCPP_RELATIVE_PATH,
LOGGING=1,
SRTP_RELATIVE_PATH,
FEATURE_ENABLE_SSL,
FEATURE_ENABLE_VOICEMAIL,
FEATURE_ENABLE_PSTN,
HAVE_SCTP,
HAVE_SRTP,
are used by the talk/media code.
For Chromium, the following changes will need to be applied to the roll CL that updates the
DEPS for WebRTC and libjingle: https://codereview.chromium.org/1604303002/
BUG=webrtc:5420
NOPRESUBMIT=True
TBR=tommi@webrtc.org
Review URL: https://codereview.webrtc.org/1587193006
Cr-Commit-Position: refs/heads/master@{#11495}
2016-02-04 23:52:28 -08:00
|
|
|
#include "webrtc/media/base/videocommon.h"
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2016-04-05 15:23:49 +02:00
|
|
|
namespace {
|
2016-05-13 10:26:00 -07:00
|
|
|
struct Fraction {
|
|
|
|
|
int numerator;
|
|
|
|
|
int denominator;
|
2017-02-10 07:04:27 -08:00
|
|
|
|
|
|
|
|
// Determines number of output pixels if both width and height of an input of
|
|
|
|
|
// |input_pixels| pixels is scaled with the fraction numerator / denominator.
|
|
|
|
|
int scale_pixel_count(int input_pixels) {
|
|
|
|
|
return (numerator * numerator * input_pixels) / (denominator * denominator);
|
|
|
|
|
}
|
2016-05-13 10:26:00 -07:00
|
|
|
};
|
|
|
|
|
|
2016-12-08 08:04:51 -08:00
|
|
|
// Round |value_to_round| to a multiple of |multiple|. Prefer rounding upwards,
|
|
|
|
|
// but never more than |max_value|.
|
|
|
|
|
int roundUp(int value_to_round, int multiple, int max_value) {
|
|
|
|
|
const int rounded_value =
|
|
|
|
|
(value_to_round + multiple - 1) / multiple * multiple;
|
|
|
|
|
return rounded_value <= max_value ? rounded_value
|
|
|
|
|
: (max_value / multiple * multiple);
|
2013-08-30 21:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
2017-02-10 07:04:27 -08:00
|
|
|
// Generates a scale factor that makes |input_pixels| close to |target_pixels|,
|
|
|
|
|
// but no higher than |max_pixels|.
|
|
|
|
|
Fraction FindScale(int input_pixels, int target_pixels, int max_pixels) {
|
2016-12-08 08:04:51 -08:00
|
|
|
// This function only makes sense for a positive target.
|
2017-02-10 07:04:27 -08:00
|
|
|
RTC_DCHECK_GT(target_pixels, 0);
|
|
|
|
|
RTC_DCHECK_GT(max_pixels, 0);
|
|
|
|
|
RTC_DCHECK_GE(max_pixels, target_pixels);
|
|
|
|
|
|
|
|
|
|
// Don't scale up original.
|
|
|
|
|
if (target_pixels >= input_pixels)
|
|
|
|
|
return Fraction{1, 1};
|
|
|
|
|
|
|
|
|
|
Fraction current_scale = Fraction{1, 1};
|
2016-12-08 08:04:51 -08:00
|
|
|
Fraction best_scale = Fraction{1, 1};
|
2017-02-10 07:04:27 -08:00
|
|
|
// The minimum (absolute) difference between the number of output pixels and
|
|
|
|
|
// the target pixel count.
|
|
|
|
|
int min_pixel_diff = std::numeric_limits<int>::max();
|
|
|
|
|
if (input_pixels < max_pixels) {
|
|
|
|
|
// Start condition for 1/1 case, if it is less than max.
|
|
|
|
|
min_pixel_diff = std::abs(input_pixels - target_pixels);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Alternately scale down by 2/3 and 3/4. This results in fractions which are
|
|
|
|
|
// effectively scalable. For instance, starting at 1280x720 will result in
|
|
|
|
|
// the series (3/4) => 960x540, (1/2) => 640x360, (3/8) => 480x270,
|
|
|
|
|
// (1/4) => 320x180, (3/16) => 240x125, (1/8) => 160x90.
|
|
|
|
|
while (current_scale.scale_pixel_count(input_pixels) > target_pixels) {
|
|
|
|
|
if (current_scale.numerator % 3 == 0 &&
|
|
|
|
|
current_scale.denominator % 2 == 0) {
|
|
|
|
|
// Multiply by 2/3.
|
|
|
|
|
current_scale.numerator /= 3;
|
|
|
|
|
current_scale.denominator /= 2;
|
2016-12-08 08:04:51 -08:00
|
|
|
} else {
|
2017-02-10 07:04:27 -08:00
|
|
|
// Multiply by 3/4.
|
|
|
|
|
current_scale.numerator *= 3;
|
|
|
|
|
current_scale.denominator *= 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int output_pixels = current_scale.scale_pixel_count(input_pixels);
|
|
|
|
|
if (output_pixels <= max_pixels) {
|
|
|
|
|
int diff = std::abs(target_pixels - output_pixels);
|
|
|
|
|
if (diff < min_pixel_diff) {
|
|
|
|
|
min_pixel_diff = diff;
|
|
|
|
|
best_scale = current_scale;
|
|
|
|
|
}
|
2016-04-05 15:23:49 +02:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-10 07:04:27 -08:00
|
|
|
|
2016-04-05 15:23:49 +02:00
|
|
|
return best_scale;
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
2016-04-05 15:23:49 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
namespace cricket {
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2016-12-08 08:04:51 -08:00
|
|
|
VideoAdapter::VideoAdapter(int required_resolution_alignment)
|
2016-05-13 10:26:00 -07:00
|
|
|
: frames_in_(0),
|
2014-02-07 19:03:26 +00:00
|
|
|
frames_out_(0),
|
|
|
|
|
frames_scaled_(0),
|
2013-08-30 21:24:16 +00:00
|
|
|
adaption_changes_(0),
|
2014-11-14 13:25:25 +00:00
|
|
|
previous_width_(0),
|
|
|
|
|
previous_height_(0),
|
2016-12-08 08:04:51 -08:00
|
|
|
required_resolution_alignment_(required_resolution_alignment),
|
2017-02-10 07:04:27 -08:00
|
|
|
resolution_request_target_pixel_count_(std::numeric_limits<int>::max()),
|
|
|
|
|
resolution_request_max_pixel_count_(std::numeric_limits<int>::max()) {}
|
2016-12-08 08:04:51 -08:00
|
|
|
|
|
|
|
|
VideoAdapter::VideoAdapter() : VideoAdapter(1) {}
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2016-04-05 15:23:49 +02:00
|
|
|
VideoAdapter::~VideoAdapter() {}
|
|
|
|
|
|
2016-05-19 06:05:40 -07:00
|
|
|
bool VideoAdapter::KeepFrame(int64_t in_timestamp_ns) {
|
2016-04-05 15:23:49 +02:00
|
|
|
rtc::CritScope cs(&critical_section_);
|
2016-05-19 06:05:40 -07:00
|
|
|
if (!requested_format_ || requested_format_->interval == 0)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (next_frame_timestamp_ns_) {
|
|
|
|
|
// Time until next frame should be outputted.
|
|
|
|
|
const int64_t time_until_next_frame_ns =
|
|
|
|
|
(*next_frame_timestamp_ns_ - in_timestamp_ns);
|
|
|
|
|
|
|
|
|
|
// Continue if timestamp is withing expected range.
|
|
|
|
|
if (std::abs(time_until_next_frame_ns) < 2 * requested_format_->interval) {
|
|
|
|
|
// Drop if a frame shouldn't be outputted yet.
|
|
|
|
|
if (time_until_next_frame_ns > 0)
|
|
|
|
|
return false;
|
|
|
|
|
// Time to output new frame.
|
|
|
|
|
*next_frame_timestamp_ns_ += requested_format_->interval;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// First timestamp received or timestamp is way outside expected range, so
|
|
|
|
|
// reset. Set first timestamp target to just half the interval to prefer
|
|
|
|
|
// keeping frames in case of jitter.
|
|
|
|
|
next_frame_timestamp_ns_ =
|
|
|
|
|
rtc::Optional<int64_t>(in_timestamp_ns + requested_format_->interval / 2);
|
|
|
|
|
return true;
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-25 08:47:01 -07:00
|
|
|
bool VideoAdapter::AdaptFrameResolution(int in_width,
|
2016-05-13 10:26:00 -07:00
|
|
|
int in_height,
|
2016-05-19 06:05:40 -07:00
|
|
|
int64_t in_timestamp_ns,
|
2016-05-13 10:26:00 -07:00
|
|
|
int* cropped_width,
|
|
|
|
|
int* cropped_height,
|
|
|
|
|
int* out_width,
|
|
|
|
|
int* out_height) {
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::CritScope cs(&critical_section_);
|
2014-02-07 19:03:26 +00:00
|
|
|
++frames_in_;
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2016-05-13 10:26:00 -07:00
|
|
|
// The max output pixel count is the minimum of the requests from
|
|
|
|
|
// OnOutputFormatRequest and OnResolutionRequest.
|
|
|
|
|
int max_pixel_count = resolution_request_max_pixel_count_;
|
|
|
|
|
if (requested_format_) {
|
|
|
|
|
max_pixel_count = std::min(
|
2017-02-10 07:04:27 -08:00
|
|
|
max_pixel_count, requested_format_->width * requested_format_->height);
|
2016-05-13 10:26:00 -07:00
|
|
|
}
|
2017-02-10 07:04:27 -08:00
|
|
|
int target_pixel_count =
|
|
|
|
|
std::min(resolution_request_target_pixel_count_, max_pixel_count);
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
// Drop the input frame if necessary.
|
2016-12-08 08:04:51 -08:00
|
|
|
if (max_pixel_count <= 0 || !KeepFrame(in_timestamp_ns)) {
|
2014-02-07 19:03:26 +00:00
|
|
|
// Show VAdapt log every 90 frames dropped. (3 seconds)
|
2014-02-13 23:18:49 +00:00
|
|
|
if ((frames_in_ - frames_out_) % 90 == 0) {
|
2014-02-07 19:03:26 +00:00
|
|
|
// TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed
|
|
|
|
|
// in default calls.
|
2014-02-13 23:18:49 +00:00
|
|
|
LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_
|
|
|
|
|
<< " / out " << frames_out_
|
|
|
|
|
<< " / in " << frames_in_
|
2014-02-07 19:03:26 +00:00
|
|
|
<< " Changes: " << adaption_changes_
|
2014-11-19 18:09:14 +00:00
|
|
|
<< " Input: " << in_width
|
|
|
|
|
<< "x" << in_height
|
2016-05-19 06:05:40 -07:00
|
|
|
<< " timestamp: " << in_timestamp_ns
|
2016-05-13 10:26:00 -07:00
|
|
|
<< " Output: i"
|
|
|
|
|
<< (requested_format_ ? requested_format_->interval : 0);
|
2014-02-07 19:03:26 +00:00
|
|
|
}
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2016-05-13 10:26:00 -07:00
|
|
|
// Drop frame.
|
2016-05-25 08:47:01 -07:00
|
|
|
return false;
|
2016-05-13 10:26:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate how the input should be cropped.
|
|
|
|
|
if (!requested_format_ ||
|
|
|
|
|
requested_format_->width == 0 || requested_format_->height == 0) {
|
|
|
|
|
*cropped_width = in_width;
|
|
|
|
|
*cropped_height = in_height;
|
|
|
|
|
} else {
|
|
|
|
|
// Adjust |requested_format_| orientation to match input.
|
|
|
|
|
if ((in_width > in_height) !=
|
|
|
|
|
(requested_format_->width > requested_format_->height)) {
|
|
|
|
|
std::swap(requested_format_->width, requested_format_->height);
|
|
|
|
|
}
|
|
|
|
|
const float requested_aspect =
|
|
|
|
|
requested_format_->width /
|
|
|
|
|
static_cast<float>(requested_format_->height);
|
|
|
|
|
*cropped_width =
|
|
|
|
|
std::min(in_width, static_cast<int>(in_height * requested_aspect));
|
|
|
|
|
*cropped_height =
|
|
|
|
|
std::min(in_height, static_cast<int>(in_width / requested_aspect));
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
2017-02-10 07:04:27 -08:00
|
|
|
const Fraction scale = FindScale((*cropped_width) * (*cropped_height),
|
|
|
|
|
target_pixel_count, max_pixel_count);
|
2016-05-13 10:26:00 -07:00
|
|
|
// Adjust cropping slightly to get even integer output size and a perfect
|
2016-12-08 08:04:51 -08:00
|
|
|
// scale factor. Make sure the resulting dimensions are aligned correctly
|
|
|
|
|
// to be nice to hardware encoders.
|
|
|
|
|
*cropped_width =
|
|
|
|
|
roundUp(*cropped_width,
|
|
|
|
|
scale.denominator * required_resolution_alignment_, in_width);
|
|
|
|
|
*cropped_height =
|
|
|
|
|
roundUp(*cropped_height,
|
|
|
|
|
scale.denominator * required_resolution_alignment_, in_height);
|
2016-05-13 10:26:00 -07:00
|
|
|
RTC_DCHECK_EQ(0, *cropped_width % scale.denominator);
|
|
|
|
|
RTC_DCHECK_EQ(0, *cropped_height % scale.denominator);
|
|
|
|
|
|
|
|
|
|
// Calculate final output size.
|
|
|
|
|
*out_width = *cropped_width / scale.denominator * scale.numerator;
|
|
|
|
|
*out_height = *cropped_height / scale.denominator * scale.numerator;
|
2016-12-08 08:04:51 -08:00
|
|
|
RTC_DCHECK_EQ(0, *out_height % required_resolution_alignment_);
|
|
|
|
|
RTC_DCHECK_EQ(0, *out_height % required_resolution_alignment_);
|
2013-08-30 21:24:16 +00:00
|
|
|
|
2014-02-07 19:03:26 +00:00
|
|
|
++frames_out_;
|
2016-05-13 10:26:00 -07:00
|
|
|
if (scale.numerator != scale.denominator)
|
2014-02-07 19:03:26 +00:00
|
|
|
++frames_scaled_;
|
2016-04-05 15:23:49 +02:00
|
|
|
|
2016-05-13 10:26:00 -07:00
|
|
|
if (previous_width_ && (previous_width_ != *out_width ||
|
|
|
|
|
previous_height_ != *out_height)) {
|
2013-08-30 21:24:16 +00:00
|
|
|
++adaption_changes_;
|
2016-04-05 15:23:49 +02:00
|
|
|
LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ << " / out "
|
|
|
|
|
<< frames_out_ << " / in " << frames_in_
|
|
|
|
|
<< " Changes: " << adaption_changes_ << " Input: " << in_width
|
2016-05-19 06:05:40 -07:00
|
|
|
<< "x" << in_height
|
2016-05-13 10:26:00 -07:00
|
|
|
<< " Scale: " << scale.numerator << "/" << scale.denominator
|
|
|
|
|
<< " Output: " << *out_width << "x" << *out_height << " i"
|
|
|
|
|
<< (requested_format_ ? requested_format_->interval : 0);
|
2013-08-30 21:24:16 +00:00
|
|
|
}
|
2014-11-19 18:09:14 +00:00
|
|
|
|
2016-05-13 10:26:00 -07:00
|
|
|
previous_width_ = *out_width;
|
|
|
|
|
previous_height_ = *out_height;
|
2016-05-25 08:47:01 -07:00
|
|
|
|
|
|
|
|
return true;
|
2014-11-19 18:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
2016-04-05 15:23:49 +02:00
|
|
|
void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) {
|
|
|
|
|
rtc::CritScope cs(&critical_section_);
|
2016-05-13 10:26:00 -07:00
|
|
|
requested_format_ = rtc::Optional<VideoFormat>(format);
|
2016-05-19 06:05:40 -07:00
|
|
|
next_frame_timestamp_ns_ = rtc::Optional<int64_t>();
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
2016-04-05 15:23:49 +02:00
|
|
|
void VideoAdapter::OnResolutionRequest(
|
2017-02-10 07:04:27 -08:00
|
|
|
const rtc::Optional<int>& target_pixel_count,
|
|
|
|
|
const rtc::Optional<int>& max_pixel_count) {
|
2016-04-05 15:23:49 +02:00
|
|
|
rtc::CritScope cs(&critical_section_);
|
2017-02-10 07:04:27 -08:00
|
|
|
resolution_request_max_pixel_count_ =
|
|
|
|
|
max_pixel_count.value_or(std::numeric_limits<int>::max());
|
|
|
|
|
resolution_request_target_pixel_count_ =
|
|
|
|
|
target_pixel_count.value_or(resolution_request_max_pixel_count_);
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace cricket
|