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.
|
|
|
|
|
*/
|
2016-06-13 13:06:01 +02:00
|
|
|
|
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
|
|
|
|
2016-09-14 02:14:58 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
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
|
|
|
// Threshold constant used until first downscale (to permit fast rampup).
|
|
|
|
|
static const int kMeasureSecondsFastUpscale = 2;
|
|
|
|
|
static const int kMeasureSecondsUpscale = 5;
|
2016-04-18 12:58:02 +02:00
|
|
|
static const int kMeasureSecondsDownscale = 5;
|
2014-09-12 11:51:47 +00:00
|
|
|
static const int kFramedropPercentThreshold = 60;
|
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;
|
2016-04-18 22:45:55 +02:00
|
|
|
// Initial resolutions corresponding to a bitrate. Aa bit above their actual
|
|
|
|
|
// values to permit near-VGA and near-QVGA resolutions to use the same
|
|
|
|
|
// mechanism.
|
|
|
|
|
static const int kVgaBitrateThresholdKbps = 500;
|
|
|
|
|
static const int kVgaNumPixels = 700 * 500; // 640x480
|
|
|
|
|
static const int kQvgaBitrateThresholdKbps = 250;
|
|
|
|
|
static const int kQvgaNumPixels = 400 * 300; // 320x240
|
2016-04-14 14:48:10 +02:00
|
|
|
} // namespace
|
2014-09-12 11:51:47 +00:00
|
|
|
|
2016-05-13 11:05:35 -07:00
|
|
|
// QP thresholds are chosen to be high enough to be hit in practice when quality
|
|
|
|
|
// is good, but also low enough to not cause a flip-flop behavior (e.g. going up
|
|
|
|
|
// in resolution shouldn't give so bad quality that we should go back down).
|
|
|
|
|
|
|
|
|
|
const int QualityScaler::kLowVp8QpThreshold = 29;
|
|
|
|
|
const int QualityScaler::kBadVp8QpThreshold = 95;
|
|
|
|
|
|
2016-08-15 16:37:56 -07:00
|
|
|
#if defined(WEBRTC_IOS)
|
2016-08-08 15:27:36 -07:00
|
|
|
const int QualityScaler::kLowH264QpThreshold = 32;
|
|
|
|
|
const int QualityScaler::kBadH264QpThreshold = 42;
|
2016-08-15 16:37:56 -07:00
|
|
|
#else
|
2016-08-24 12:09:16 -07:00
|
|
|
const int QualityScaler::kLowH264QpThreshold = 24;
|
|
|
|
|
const int QualityScaler::kBadH264QpThreshold = 37;
|
2016-08-15 16:37:56 -07:00
|
|
|
#endif
|
2016-05-13 11:05:35 -07:00
|
|
|
|
2016-09-14 02:14:58 -07:00
|
|
|
// Default values. Should immediately get set to something more sensible.
|
|
|
|
|
QualityScaler::QualityScaler()
|
|
|
|
|
: average_qp_(kMeasureSecondsUpscale * 30),
|
|
|
|
|
framedrop_percent_(kMeasureSecondsUpscale * 30),
|
|
|
|
|
low_qp_threshold_(-1) {}
|
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
|
|
|
int initial_bitrate_kbps,
|
|
|
|
|
int width,
|
2016-04-13 02:51:02 -07:00
|
|
|
int height,
|
|
|
|
|
int fps) {
|
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;
|
2016-03-22 21:44:29 +01:00
|
|
|
downscale_shift_ = 0;
|
2016-09-14 02:14:58 -07:00
|
|
|
|
|
|
|
|
fast_rampup_ = true;
|
|
|
|
|
|
|
|
|
|
ClearSamples();
|
|
|
|
|
ReportFramerate(fps);
|
|
|
|
|
|
2016-03-22 21:44:29 +01:00
|
|
|
const int init_width = width;
|
|
|
|
|
const int init_height = height;
|
2016-04-18 22:45:55 +02:00
|
|
|
if (initial_bitrate_kbps > 0) {
|
|
|
|
|
int init_num_pixels = width * height;
|
|
|
|
|
if (initial_bitrate_kbps < kVgaBitrateThresholdKbps)
|
|
|
|
|
init_num_pixels = kVgaNumPixels;
|
|
|
|
|
if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps)
|
|
|
|
|
init_num_pixels = kQvgaNumPixels;
|
|
|
|
|
while (width * height > init_num_pixels) {
|
2016-02-19 15:24:06 -08:00
|
|
|
++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-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) {
|
2016-09-14 02:14:58 -07:00
|
|
|
// Use a faster window for upscaling initially.
|
|
|
|
|
// This enables faster initial rampups without risking strong up-down
|
|
|
|
|
// behavior later.
|
|
|
|
|
num_samples_upscale_ = framerate * (fast_rampup_ ? kMeasureSecondsFastUpscale
|
|
|
|
|
: kMeasureSecondsUpscale);
|
|
|
|
|
num_samples_downscale_ = framerate * kMeasureSecondsDownscale;
|
|
|
|
|
|
|
|
|
|
average_qp_ =
|
|
|
|
|
MovingAverage(std::max(num_samples_upscale_, num_samples_downscale_));
|
|
|
|
|
framedrop_percent_ =
|
|
|
|
|
MovingAverage(std::max(num_samples_upscale_, num_samples_downscale_));
|
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-09-14 02:14:58 -07:00
|
|
|
average_qp_.AddSample(qp);
|
2014-09-12 11:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QualityScaler::ReportDroppedFrame() {
|
|
|
|
|
framedrop_percent_.AddSample(100);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-13 13:06:01 +02:00
|
|
|
void QualityScaler::OnEncodeFrame(int width, int height) {
|
2015-04-21 15:30:11 -07:00
|
|
|
// Should be set through InitEncode -> Should be set by now.
|
2016-06-13 13:06:01 +02:00
|
|
|
RTC_DCHECK_GE(low_qp_threshold_, 0);
|
2016-09-14 02:14:58 -07:00
|
|
|
if (target_res_.width != width || target_res_.height != height) {
|
|
|
|
|
UpdateTargetResolution(width, height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if we should scale down due to high frame drop.
|
|
|
|
|
const auto drop_rate = framedrop_percent_.GetAverage(num_samples_downscale_);
|
|
|
|
|
if (drop_rate && *drop_rate >= kFramedropPercentThreshold) {
|
|
|
|
|
ScaleDown();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if we should scale up or down based on QP.
|
|
|
|
|
const auto avg_qp_down = average_qp_.GetAverage(num_samples_downscale_);
|
|
|
|
|
if (avg_qp_down && *avg_qp_down > high_qp_threshold_) {
|
|
|
|
|
ScaleDown();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const auto avg_qp_up = average_qp_.GetAverage(num_samples_upscale_);
|
|
|
|
|
if (avg_qp_up && *avg_qp_up <= low_qp_threshold_) {
|
|
|
|
|
// QP has been low. We want to try a higher resolution.
|
|
|
|
|
ScaleUp();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QualityScaler::ScaleUp() {
|
|
|
|
|
downscale_shift_ = std::max(0, downscale_shift_ - 1);
|
|
|
|
|
ClearSamples();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QualityScaler::ScaleDown() {
|
|
|
|
|
downscale_shift_ = std::min(maximum_shift_, downscale_shift_ + 1);
|
|
|
|
|
ClearSamples();
|
|
|
|
|
// If we've scaled down, wait longer before scaling up again.
|
|
|
|
|
if (fast_rampup_) {
|
|
|
|
|
fast_rampup_ = false;
|
|
|
|
|
num_samples_upscale_ = (num_samples_upscale_ / kMeasureSecondsFastUpscale) *
|
|
|
|
|
kMeasureSecondsUpscale;
|
2015-04-21 15:30:11 -07:00
|
|
|
}
|
2015-07-13 16:26:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
|
2016-09-14 02:14:58 -07:00
|
|
|
const int frame_width = target_res_.width >> downscale_shift_;
|
|
|
|
|
const int frame_height = target_res_.height >> downscale_shift_;
|
|
|
|
|
return Resolution{frame_width, frame_height};
|
2015-07-13 16:26:33 -07:00
|
|
|
}
|
2014-09-12 11:51:47 +00:00
|
|
|
|
2016-06-13 13:06:01 +02:00
|
|
|
rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer(
|
|
|
|
|
const rtc::scoped_refptr<VideoFrameBuffer>& frame) {
|
2015-07-13 16:26:33 -07:00
|
|
|
Resolution res = GetScaledResolution();
|
2016-09-14 02:14:58 -07:00
|
|
|
const int src_width = frame->width();
|
|
|
|
|
const int src_height = frame->height();
|
2014-09-12 11:51:47 +00:00
|
|
|
|
2016-06-13 13:06:01 +02:00
|
|
|
if (res.width == src_width && res.height == src_height)
|
2014-09-12 11:51:47 +00:00
|
|
|
return frame;
|
2016-06-13 13:06:01 +02:00
|
|
|
rtc::scoped_refptr<I420Buffer> scaled_buffer =
|
|
|
|
|
pool_.CreateBuffer(res.width, res.height);
|
|
|
|
|
|
|
|
|
|
scaled_buffer->ScaleFrom(frame);
|
2014-09-12 11:51:47 +00:00
|
|
|
|
2016-06-13 13:06:01 +02:00
|
|
|
return scaled_buffer;
|
2014-09-12 11:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-14 02:14:58 -07:00
|
|
|
void QualityScaler::UpdateTargetResolution(int width, int height) {
|
|
|
|
|
if (width < kMinDownscaleDimension || height < kMinDownscaleDimension) {
|
|
|
|
|
maximum_shift_ = 0;
|
|
|
|
|
} else {
|
|
|
|
|
maximum_shift_ = static_cast<int>(
|
|
|
|
|
std::log2(std::min(width, height) / kMinDownscaleDimension));
|
2016-03-22 21:44:29 +01:00
|
|
|
}
|
2016-09-14 02:14:58 -07:00
|
|
|
target_res_ = Resolution{width, height};
|
2016-03-22 21:44:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-09-12 11:51:47 +00:00
|
|
|
void QualityScaler::ClearSamples() {
|
|
|
|
|
framedrop_percent_.Reset();
|
2016-09-14 02:14:58 -07:00
|
|
|
average_qp_.Reset();
|
2016-04-13 02:51:02 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-12 11:51:47 +00:00
|
|
|
|
|
|
|
|
} // namespace webrtc
|