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-14 14:48:10 +02:00
|
|
|
namespace {
|
2016-04-13 02:51:02 -07:00
|
|
|
static const int kMinFps = 5;
|
|
|
|
|
static const int kMeasureSecondsDownscale = 3;
|
|
|
|
|
// Threshold constant used until first downscale (to permit fast rampup).
|
|
|
|
|
static const int kMeasureSecondsFastUpscale = 2;
|
|
|
|
|
static const int kMeasureSecondsUpscale = 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;
|
2016-04-14 14:48:10 +02:00
|
|
|
// Min width/height to downscale to, set to not go below QVGA, but with some
|
|
|
|
|
// margin to permit "almost-QVGA" resolutions, such as QCIF.
|
|
|
|
|
static const int kMinDownscaleDimension = 140;
|
|
|
|
|
} // namespace
|
2014-09-12 11:51:47 +00:00
|
|
|
|
|
|
|
|
QualityScaler::QualityScaler()
|
2016-04-14 14:48:10 +02:00
|
|
|
: low_qp_threshold_(-1), framerate_down_(false) {}
|
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-13 02:51:02 -07:00
|
|
|
int height,
|
|
|
|
|
int fps) {
|
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;
|
2016-04-13 02:51:02 -07:00
|
|
|
// Use a faster window for upscaling initially (but be more graceful later).
|
|
|
|
|
// This enables faster initial rampups without risking strong up-down
|
|
|
|
|
// behavior later.
|
|
|
|
|
measure_seconds_upscale_ = kMeasureSecondsFastUpscale;
|
2016-03-22 21:44:29 +01:00
|
|
|
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);
|
2016-04-13 02:51:02 -07:00
|
|
|
ReportFramerate(fps);
|
2015-07-13 16:26:33 -07:00
|
|
|
target_framerate_ = -1;
|
2015-04-21 15:30:11 -07: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) {
|
2015-07-13 16:26:33 -07:00
|
|
|
framerate_ = framerate;
|
2016-04-13 02:51:02 -07:00
|
|
|
UpdateSampleCounts();
|
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-13 02:51:02 -07:00
|
|
|
average_qp_downscale_.AddSample(qp);
|
|
|
|
|
average_qp_upscale_.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-13 02:51:02 -07:00
|
|
|
assert(num_samples_upscale_ > 0);
|
|
|
|
|
assert(num_samples_downscale_ > 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-13 02:51:02 -07:00
|
|
|
if ((framedrop_percent_.GetAverage(num_samples_downscale_, &avg_drop) &&
|
2015-09-25 17:03:26 +02:00
|
|
|
avg_drop >= kFramedropPercentThreshold) ||
|
2016-04-13 02:51:02 -07:00
|
|
|
(average_qp_downscale_.GetAverage(num_samples_downscale_, &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-13 02:51:02 -07:00
|
|
|
} else if (average_qp_upscale_.GetAverage(num_samples_upscale_, &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_;
|
2016-04-14 14:48:10 +02:00
|
|
|
shift > 0 && (res_.width / 2 >= kMinDownscaleDimension) &&
|
|
|
|
|
(res_.height / 2 >= kMinDownscaleDimension);
|
2016-03-22 21:44:29 +01:00
|
|
|
--shift) {
|
|
|
|
|
res_.width /= 2;
|
|
|
|
|
res_.height /= 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-12 11:51:47 +00:00
|
|
|
void QualityScaler::ClearSamples() {
|
|
|
|
|
framedrop_percent_.Reset();
|
2016-04-13 02:51:02 -07:00
|
|
|
average_qp_downscale_.Reset();
|
|
|
|
|
average_qp_upscale_.Reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QualityScaler::UpdateSampleCounts() {
|
|
|
|
|
num_samples_downscale_ = static_cast<size_t>(
|
|
|
|
|
kMeasureSecondsDownscale * (framerate_ < kMinFps ? kMinFps : framerate_));
|
|
|
|
|
num_samples_upscale_ = static_cast<size_t>(
|
|
|
|
|
measure_seconds_upscale_ * (framerate_ < kMinFps ? kMinFps : framerate_));
|
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;
|
2016-04-13 02:51:02 -07:00
|
|
|
if (!up) {
|
|
|
|
|
// Hit first downscale, start using a slower threshold for going up.
|
|
|
|
|
measure_seconds_upscale_ = kMeasureSecondsUpscale;
|
|
|
|
|
UpdateSampleCounts();
|
|
|
|
|
}
|
2014-09-12 11:51:47 +00:00
|
|
|
ClearSamples();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|