2014-09-12 11:51:47 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* 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-11-18 23:04:10 +01:00
|
|
|
#include "webrtc/modules/video_coding/utility/quality_scaler.h"
|
2014-09-12 11:51:47 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2016-04-12 09:05:58 -07:00
|
|
|
static const int kMinFps = 10;
|
|
|
|
|
static const int kMeasureSeconds = 5;
|
2014-09-12 11:51:47 +00:00
|
|
|
static const int kFramedropPercentThreshold = 60;
|
2016-02-19 15:24:06 -08:00
|
|
|
static const int kHdResolutionThreshold = 700 * 500;
|
|
|
|
|
static const int kHdBitrateThresholdKbps = 500;
|
2014-09-12 11:51:47 +00:00
|
|
|
|
2015-06-22 08:02:58 +02:00
|
|
|
const int QualityScaler::kDefaultLowQpDenominator = 3;
|
|
|
|
|
// Note that this is the same for width and height to permit 120x90 in both
|
|
|
|
|
// portrait and landscape mode.
|
|
|
|
|
const int QualityScaler::kDefaultMinDownscaleDimension = 90;
|
|
|
|
|
|
2014-09-12 11:51:47 +00:00
|
|
|
QualityScaler::QualityScaler()
|
2016-04-12 09:05:58 -07:00
|
|
|
: num_samples_(0),
|
|
|
|
|
low_qp_threshold_(-1),
|
|
|
|
|
downscale_shift_(0),
|
2015-07-13 16:26:33 -07:00
|
|
|
framerate_down_(false),
|
2015-06-22 08:02:58 +02:00
|
|
|
min_width_(kDefaultMinDownscaleDimension),
|
2015-12-21 08:23:20 -08:00
|
|
|
min_height_(kDefaultMinDownscaleDimension) {}
|
2014-09-12 11:51:47 +00:00
|
|
|
|
2015-09-25 17:03:26 +02:00
|
|
|
void QualityScaler::Init(int low_qp_threshold,
|
|
|
|
|
int high_qp_threshold,
|
2016-02-19 15:24:06 -08:00
|
|
|
bool use_framerate_reduction,
|
|
|
|
|
int initial_bitrate_kbps,
|
|
|
|
|
int width,
|
2016-04-12 09:05:58 -07:00
|
|
|
int height) {
|
2014-09-12 11:51:47 +00:00
|
|
|
ClearSamples();
|
2015-05-21 11:12:02 -07:00
|
|
|
low_qp_threshold_ = low_qp_threshold;
|
2015-09-25 17:03:26 +02:00
|
|
|
high_qp_threshold_ = high_qp_threshold;
|
2015-07-13 16:26:33 -07:00
|
|
|
use_framerate_reduction_ = use_framerate_reduction;
|
2016-03-22 21:44:29 +01:00
|
|
|
downscale_shift_ = 0;
|
|
|
|
|
const int init_width = width;
|
|
|
|
|
const int init_height = height;
|
2016-02-19 15:24:06 -08:00
|
|
|
// TODO(glaznev): Investigate using thresholds for other resolutions
|
|
|
|
|
// or threshold tables.
|
|
|
|
|
if (initial_bitrate_kbps > 0 &&
|
|
|
|
|
initial_bitrate_kbps < kHdBitrateThresholdKbps) {
|
|
|
|
|
// Start scaling to roughly VGA.
|
|
|
|
|
while (width * height > kHdResolutionThreshold) {
|
|
|
|
|
++downscale_shift_;
|
|
|
|
|
width /= 2;
|
|
|
|
|
height /= 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-22 21:44:29 +01:00
|
|
|
UpdateTargetResolution(init_width, init_height);
|
2015-07-13 16:26:33 -07:00
|
|
|
target_framerate_ = -1;
|
2015-04-21 15:30:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QualityScaler::SetMinResolution(int min_width, int min_height) {
|
|
|
|
|
min_width_ = min_width;
|
|
|
|
|
min_height_ = min_height;
|
2014-09-12 11:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-21 11:12:02 -07:00
|
|
|
// Report framerate(fps) to estimate # of samples.
|
2014-09-12 11:51:47 +00:00
|
|
|
void QualityScaler::ReportFramerate(int framerate) {
|
2016-04-12 09:05:58 -07:00
|
|
|
num_samples_ = static_cast<size_t>(
|
|
|
|
|
kMeasureSeconds * (framerate < kMinFps ? kMinFps : framerate));
|
2015-07-13 16:26:33 -07:00
|
|
|
framerate_ = framerate;
|
2014-09-12 11:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-21 11:12:02 -07:00
|
|
|
void QualityScaler::ReportQP(int qp) {
|
2014-09-12 11:51:47 +00:00
|
|
|
framedrop_percent_.AddSample(0);
|
2016-04-12 09:05:58 -07:00
|
|
|
average_qp_.AddSample(qp);
|
2014-09-12 11:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QualityScaler::ReportDroppedFrame() {
|
|
|
|
|
framedrop_percent_.AddSample(100);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-13 16:26:33 -07:00
|
|
|
void QualityScaler::OnEncodeFrame(const VideoFrame& frame) {
|
2015-04-21 15:30:11 -07:00
|
|
|
// Should be set through InitEncode -> Should be set by now.
|
2014-09-12 11:51:47 +00:00
|
|
|
assert(low_qp_threshold_ >= 0);
|
2016-04-12 09:05:58 -07:00
|
|
|
assert(num_samples_ > 0);
|
2014-09-12 11:51:47 +00:00
|
|
|
|
2015-04-21 15:30:11 -07:00
|
|
|
// Update scale factor.
|
2015-05-21 14:11:36 -07:00
|
|
|
int avg_drop = 0;
|
|
|
|
|
int avg_qp = 0;
|
2015-07-13 16:26:33 -07:00
|
|
|
|
|
|
|
|
// When encoder consistently overshoots, framerate reduction and spatial
|
|
|
|
|
// resizing will be triggered to get a smoother video.
|
2016-04-12 09:05:58 -07:00
|
|
|
if ((framedrop_percent_.GetAverage(num_samples_, &avg_drop) &&
|
2015-09-25 17:03:26 +02:00
|
|
|
avg_drop >= kFramedropPercentThreshold) ||
|
2016-04-12 09:05:58 -07:00
|
|
|
(average_qp_.GetAverage(num_samples_, &avg_qp) &&
|
2015-09-25 17:03:26 +02:00
|
|
|
avg_qp > high_qp_threshold_)) {
|
2015-07-13 16:26:33 -07:00
|
|
|
// Reducing frame rate before spatial resolution change.
|
|
|
|
|
// Reduce frame rate only when it is above a certain number.
|
|
|
|
|
// Only one reduction is allowed for now.
|
|
|
|
|
// TODO(jackychen): Allow more than one framerate reduction.
|
|
|
|
|
if (use_framerate_reduction_ && !framerate_down_ && framerate_ >= 20) {
|
|
|
|
|
target_framerate_ = framerate_ / 2;
|
|
|
|
|
framerate_down_ = true;
|
|
|
|
|
// If frame rate has been updated, clear the buffer. We don't want
|
|
|
|
|
// spatial resolution to change right after frame rate change.
|
|
|
|
|
ClearSamples();
|
|
|
|
|
} else {
|
|
|
|
|
AdjustScale(false);
|
|
|
|
|
}
|
2016-04-12 09:05:58 -07:00
|
|
|
} else if (average_qp_.GetAverage(num_samples_, &avg_qp) &&
|
2015-12-21 08:23:20 -08:00
|
|
|
avg_qp <= low_qp_threshold_) {
|
2015-07-13 16:26:33 -07:00
|
|
|
if (use_framerate_reduction_ && framerate_down_) {
|
|
|
|
|
target_framerate_ = -1;
|
|
|
|
|
framerate_down_ = false;
|
|
|
|
|
ClearSamples();
|
|
|
|
|
} else {
|
|
|
|
|
AdjustScale(true);
|
|
|
|
|
}
|
2015-04-21 15:30:11 -07:00
|
|
|
}
|
2016-03-22 21:44:29 +01:00
|
|
|
UpdateTargetResolution(frame.width(), frame.height());
|
2015-07-13 16:26:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
|
|
|
|
|
return res_;
|
|
|
|
|
}
|
2014-09-12 11:51:47 +00:00
|
|
|
|
2015-07-13 16:26:33 -07:00
|
|
|
int QualityScaler::GetTargetFramerate() const {
|
|
|
|
|
return target_framerate_;
|
2014-09-12 11:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-29 17:21:40 -07:00
|
|
|
const VideoFrame& QualityScaler::GetScaledFrame(const VideoFrame& frame) {
|
2015-07-13 16:26:33 -07:00
|
|
|
Resolution res = GetScaledResolution();
|
2014-09-12 11:51:47 +00:00
|
|
|
if (res.width == frame.width())
|
|
|
|
|
return frame;
|
|
|
|
|
|
2015-12-21 08:23:20 -08:00
|
|
|
scaler_.Set(frame.width(), frame.height(), res.width, res.height, kI420,
|
|
|
|
|
kI420, kScaleBox);
|
2014-09-12 11:51:47 +00:00
|
|
|
if (scaler_.Scale(frame, &scaled_frame_) != 0)
|
|
|
|
|
return frame;
|
|
|
|
|
|
|
|
|
|
scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms());
|
|
|
|
|
scaled_frame_.set_timestamp(frame.timestamp());
|
|
|
|
|
scaled_frame_.set_render_time_ms(frame.render_time_ms());
|
|
|
|
|
|
|
|
|
|
return scaled_frame_;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 21:44:29 +01:00
|
|
|
void QualityScaler::UpdateTargetResolution(int frame_width, int frame_height) {
|
|
|
|
|
assert(downscale_shift_ >= 0);
|
|
|
|
|
res_.width = frame_width;
|
|
|
|
|
res_.height = frame_height;
|
|
|
|
|
for (int shift = downscale_shift_;
|
|
|
|
|
shift > 0 && (res_.width / 2 >= min_width_) &&
|
|
|
|
|
(res_.height / 2 >= min_height_);
|
|
|
|
|
--shift) {
|
|
|
|
|
res_.width /= 2;
|
|
|
|
|
res_.height /= 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-12 11:51:47 +00:00
|
|
|
void QualityScaler::ClearSamples() {
|
|
|
|
|
framedrop_percent_.Reset();
|
2016-04-12 09:05:58 -07:00
|
|
|
average_qp_.Reset();
|
2014-09-12 11:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QualityScaler::AdjustScale(bool up) {
|
|
|
|
|
downscale_shift_ += up ? -1 : 1;
|
|
|
|
|
if (downscale_shift_ < 0)
|
|
|
|
|
downscale_shift_ = 0;
|
|
|
|
|
ClearSamples();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|