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
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/video_coding/utility/quality_scaler.h"
|
2014-09-12 11:51:47 +00:00
|
|
|
|
2016-11-29 01:44:11 -08:00
|
|
|
#include <memory>
|
2019-01-18 10:30:54 +01:00
|
|
|
#include <utility>
|
2016-09-15 10:56:19 +02:00
|
|
|
|
2018-11-28 16:47:49 +01:00
|
|
|
#include "absl/types/optional.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
#include "rtc_base/logging.h"
|
2018-04-24 16:53:25 +02:00
|
|
|
#include "rtc_base/numerics/exp_filter.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/task_queue.h"
|
2016-09-28 08:17:43 -07:00
|
|
|
|
2016-09-15 10:56:19 +02:00
|
|
|
// TODO(kthelgason): Some versions of Android have issues with log2.
|
|
|
|
|
// See https://code.google.com/p/android/issues/detail?id=212634 for details
|
|
|
|
|
#if defined(WEBRTC_ANDROID)
|
|
|
|
|
#define log2(x) (log(x) / log(2))
|
|
|
|
|
#endif
|
2016-09-14 02:14:58 -07:00
|
|
|
|
2014-09-12 11:51:47 +00:00
|
|
|
namespace webrtc {
|
|
|
|
|
|
2016-04-14 14:48:10 +02:00
|
|
|
namespace {
|
2018-02-22 15:03:53 +01:00
|
|
|
// TODO(nisse): Delete, delegate to encoders.
|
2016-04-13 02:51:02 -07:00
|
|
|
// Threshold constant used until first downscale (to permit fast rampup).
|
2016-11-29 01:44:11 -08:00
|
|
|
static const int kMeasureMs = 2000;
|
|
|
|
|
static const float kSamplePeriodScaleFactor = 2.5;
|
2014-09-12 11:51:47 +00:00
|
|
|
static const int kFramedropPercentThreshold = 60;
|
2017-04-04 02:31:42 -07:00
|
|
|
static const int kMinFramesNeededToScale = 2 * 30;
|
2016-05-13 11:05:35 -07:00
|
|
|
|
2016-11-29 01:44:11 -08:00
|
|
|
} // namespace
|
2016-09-28 08:17:43 -07:00
|
|
|
|
2018-04-24 16:53:25 +02:00
|
|
|
class QualityScaler::QpSmoother {
|
|
|
|
|
public:
|
|
|
|
|
explicit QpSmoother(float alpha)
|
2019-03-01 15:40:49 +01:00
|
|
|
: alpha_(alpha),
|
|
|
|
|
// The initial value of last_sample_ms doesn't matter since the smoother
|
|
|
|
|
// will ignore the time delta for the first update.
|
|
|
|
|
last_sample_ms_(0),
|
|
|
|
|
smoother_(alpha) {}
|
2018-04-24 16:53:25 +02:00
|
|
|
|
2018-06-18 10:48:16 +02:00
|
|
|
absl::optional<int> GetAvg() const {
|
2018-04-24 16:53:25 +02:00
|
|
|
float value = smoother_.filtered();
|
|
|
|
|
if (value == rtc::ExpFilter::kValueUndefined) {
|
2018-06-18 10:48:16 +02:00
|
|
|
return absl::nullopt;
|
2018-04-24 16:53:25 +02:00
|
|
|
}
|
|
|
|
|
return static_cast<int>(value);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 15:40:49 +01:00
|
|
|
void Add(float sample, int64_t time_sent_us) {
|
|
|
|
|
int64_t now_ms = time_sent_us / 1000;
|
2018-04-24 16:53:25 +02:00
|
|
|
smoother_.Apply(static_cast<float>(now_ms - last_sample_ms_), sample);
|
|
|
|
|
last_sample_ms_ = now_ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Reset() { smoother_.Reset(alpha_); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const float alpha_;
|
|
|
|
|
int64_t last_sample_ms_;
|
|
|
|
|
rtc::ExpFilter smoother_;
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-29 01:44:11 -08:00
|
|
|
|
2017-02-01 08:38:12 -08:00
|
|
|
QualityScaler::QualityScaler(AdaptationObserverInterface* observer,
|
2016-11-29 01:44:11 -08:00
|
|
|
VideoEncoder::QpThresholds thresholds)
|
|
|
|
|
: QualityScaler(observer, thresholds, kMeasureMs) {}
|
|
|
|
|
|
|
|
|
|
// Protected ctor, should not be called directly.
|
2017-02-01 08:38:12 -08:00
|
|
|
QualityScaler::QualityScaler(AdaptationObserverInterface* observer,
|
2016-11-29 01:44:11 -08:00
|
|
|
VideoEncoder::QpThresholds thresholds,
|
2018-04-19 11:06:11 +02:00
|
|
|
int64_t sampling_period_ms)
|
2019-01-18 10:30:54 +01:00
|
|
|
: observer_(observer),
|
2018-04-19 11:06:11 +02:00
|
|
|
thresholds_(thresholds),
|
|
|
|
|
sampling_period_ms_(sampling_period_ms),
|
2016-11-29 01:44:11 -08:00
|
|
|
fast_rampup_(true),
|
|
|
|
|
// Arbitrarily choose size based on 30 fps for 5 seconds.
|
|
|
|
|
average_qp_(5 * 30),
|
2018-04-24 16:53:25 +02:00
|
|
|
framedrop_percent_media_opt_(5 * 30),
|
|
|
|
|
framedrop_percent_all_(5 * 30),
|
|
|
|
|
experiment_enabled_(QualityScalingExperiment::Enabled()),
|
|
|
|
|
observed_enough_frames_(false) {
|
2016-11-29 01:44:11 -08:00
|
|
|
RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
|
2018-04-24 16:53:25 +02:00
|
|
|
if (experiment_enabled_) {
|
|
|
|
|
config_ = QualityScalingExperiment::GetConfig();
|
|
|
|
|
qp_smoother_high_.reset(new QpSmoother(config_.alpha_high));
|
|
|
|
|
qp_smoother_low_.reset(new QpSmoother(config_.alpha_low));
|
|
|
|
|
}
|
2016-11-29 01:44:11 -08:00
|
|
|
RTC_DCHECK(observer_ != nullptr);
|
2019-01-18 10:30:54 +01:00
|
|
|
check_qp_task_ = RepeatingTaskHandle::DelayedStart(
|
|
|
|
|
TimeDelta::ms(GetSamplingPeriodMs()), [this]() {
|
|
|
|
|
CheckQp();
|
|
|
|
|
return TimeDelta::ms(GetSamplingPeriodMs());
|
|
|
|
|
});
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_INFO) << "QP thresholds: low: " << thresholds_.low
|
|
|
|
|
<< ", high: " << thresholds_.high;
|
2015-04-21 15:30:11 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-29 01:44:11 -08:00
|
|
|
QualityScaler::~QualityScaler() {
|
|
|
|
|
RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
|
2019-01-18 10:30:54 +01:00
|
|
|
check_qp_task_.Stop();
|
2014-09-12 11:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-29 01:44:11 -08:00
|
|
|
int64_t QualityScaler::GetSamplingPeriodMs() const {
|
|
|
|
|
RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
|
2018-04-24 16:53:25 +02:00
|
|
|
if (fast_rampup_) {
|
|
|
|
|
return sampling_period_ms_;
|
|
|
|
|
}
|
|
|
|
|
if (experiment_enabled_ && !observed_enough_frames_) {
|
|
|
|
|
// Use half the interval while waiting for enough frames.
|
|
|
|
|
return sampling_period_ms_ / 2;
|
|
|
|
|
}
|
|
|
|
|
return sampling_period_ms_ * kSamplePeriodScaleFactor;
|
2014-09-12 11:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
2018-04-24 16:53:25 +02:00
|
|
|
void QualityScaler::ReportDroppedFrameByMediaOpt() {
|
2016-11-29 01:44:11 -08:00
|
|
|
RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
|
2018-04-24 16:53:25 +02:00
|
|
|
framedrop_percent_media_opt_.AddSample(100);
|
|
|
|
|
framedrop_percent_all_.AddSample(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QualityScaler::ReportDroppedFrameByEncoder() {
|
|
|
|
|
RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
|
|
|
|
|
framedrop_percent_all_.AddSample(100);
|
2014-09-12 11:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-01 15:40:49 +01:00
|
|
|
void QualityScaler::ReportQp(int qp, int64_t time_sent_us) {
|
2016-11-29 01:44:11 -08:00
|
|
|
RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
|
2018-04-24 16:53:25 +02:00
|
|
|
framedrop_percent_media_opt_.AddSample(0);
|
|
|
|
|
framedrop_percent_all_.AddSample(0);
|
2016-11-29 01:44:11 -08:00
|
|
|
average_qp_.AddSample(qp);
|
2018-04-24 16:53:25 +02:00
|
|
|
if (qp_smoother_high_)
|
2019-03-01 15:40:49 +01:00
|
|
|
qp_smoother_high_->Add(qp, time_sent_us);
|
2018-04-24 16:53:25 +02:00
|
|
|
if (qp_smoother_low_)
|
2019-03-01 15:40:49 +01:00
|
|
|
qp_smoother_low_->Add(qp, time_sent_us);
|
2016-11-29 01:44:11 -08:00
|
|
|
}
|
2016-09-14 02:14:58 -07:00
|
|
|
|
2018-04-20 15:19:11 +02:00
|
|
|
void QualityScaler::CheckQp() {
|
2016-11-29 01:44:11 -08:00
|
|
|
RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
|
|
|
|
|
// Should be set through InitEncode -> Should be set by now.
|
|
|
|
|
RTC_DCHECK_GE(thresholds_.low, 0);
|
2017-04-04 02:31:42 -07:00
|
|
|
|
2018-04-19 11:06:11 +02:00
|
|
|
// If we have not observed at least this many frames we can't make a good
|
|
|
|
|
// scaling decision.
|
2018-04-24 16:53:25 +02:00
|
|
|
const size_t frames = config_.use_all_drop_reasons
|
2018-11-05 12:55:18 +01:00
|
|
|
? framedrop_percent_all_.Size()
|
|
|
|
|
: framedrop_percent_media_opt_.Size();
|
2018-04-24 16:53:25 +02:00
|
|
|
if (frames < kMinFramesNeededToScale) {
|
|
|
|
|
observed_enough_frames_ = false;
|
2017-04-04 02:31:42 -07:00
|
|
|
return;
|
2018-04-24 16:53:25 +02:00
|
|
|
}
|
|
|
|
|
observed_enough_frames_ = true;
|
2017-04-04 02:31:42 -07:00
|
|
|
|
2016-09-14 02:14:58 -07:00
|
|
|
// Check if we should scale down due to high frame drop.
|
2018-06-18 10:48:16 +02:00
|
|
|
const absl::optional<int> drop_rate =
|
2018-11-05 12:55:18 +01:00
|
|
|
config_.use_all_drop_reasons
|
|
|
|
|
? framedrop_percent_all_.GetAverageRoundedDown()
|
|
|
|
|
: framedrop_percent_media_opt_.GetAverageRoundedDown();
|
2016-09-14 02:14:58 -07:00
|
|
|
if (drop_rate && *drop_rate >= kFramedropPercentThreshold) {
|
2018-04-19 11:06:11 +02:00
|
|
|
RTC_LOG(LS_INFO) << "Reporting high QP, framedrop percent " << *drop_rate;
|
2018-04-20 15:19:11 +02:00
|
|
|
ReportQpHigh();
|
2016-09-14 02:14:58 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if we should scale up or down based on QP.
|
2018-11-05 12:55:18 +01:00
|
|
|
const absl::optional<int> avg_qp_high =
|
|
|
|
|
qp_smoother_high_ ? qp_smoother_high_->GetAvg()
|
|
|
|
|
: average_qp_.GetAverageRoundedDown();
|
2018-06-18 10:48:16 +02:00
|
|
|
const absl::optional<int> avg_qp_low =
|
2018-11-05 12:55:18 +01:00
|
|
|
qp_smoother_low_ ? qp_smoother_low_->GetAvg()
|
|
|
|
|
: average_qp_.GetAverageRoundedDown();
|
2018-04-24 16:53:25 +02:00
|
|
|
if (avg_qp_high && avg_qp_low) {
|
|
|
|
|
RTC_LOG(LS_INFO) << "Checking average QP " << *avg_qp_high << " ("
|
|
|
|
|
<< *avg_qp_low << ").";
|
|
|
|
|
if (*avg_qp_high > thresholds_.high) {
|
2018-04-20 15:19:11 +02:00
|
|
|
ReportQpHigh();
|
2017-03-23 14:40:08 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2018-04-24 16:53:25 +02:00
|
|
|
if (*avg_qp_low <= thresholds_.low) {
|
2017-03-23 14:40:08 -07:00
|
|
|
// QP has been low. We want to try a higher resolution.
|
2018-04-20 15:19:11 +02:00
|
|
|
ReportQpLow();
|
2017-03-23 14:40:08 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2016-09-14 02:14:58 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 15:19:11 +02:00
|
|
|
void QualityScaler::ReportQpLow() {
|
2016-11-29 01:44:11 -08:00
|
|
|
RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
|
2016-09-14 02:14:58 -07:00
|
|
|
ClearSamples();
|
2017-02-01 08:38:12 -08:00
|
|
|
observer_->AdaptUp(AdaptationObserverInterface::AdaptReason::kQuality);
|
2016-09-14 02:14:58 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-20 15:19:11 +02:00
|
|
|
void QualityScaler::ReportQpHigh() {
|
2016-11-29 01:44:11 -08:00
|
|
|
RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
|
2016-09-14 02:14:58 -07:00
|
|
|
ClearSamples();
|
2017-02-01 08:38:12 -08:00
|
|
|
observer_->AdaptDown(AdaptationObserverInterface::AdaptReason::kQuality);
|
2016-09-14 02:14:58 -07:00
|
|
|
// If we've scaled down, wait longer before scaling up again.
|
|
|
|
|
if (fast_rampup_) {
|
|
|
|
|
fast_rampup_ = false;
|
2015-04-21 15:30:11 -07:00
|
|
|
}
|
2015-07-13 16:26:33 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-12 11:51:47 +00:00
|
|
|
void QualityScaler::ClearSamples() {
|
2016-11-29 01:44:11 -08:00
|
|
|
RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
|
2018-04-24 16:53:25 +02:00
|
|
|
framedrop_percent_media_opt_.Reset();
|
|
|
|
|
framedrop_percent_all_.Reset();
|
2016-09-14 02:14:58 -07:00
|
|
|
average_qp_.Reset();
|
2018-04-24 16:53:25 +02:00
|
|
|
if (qp_smoother_high_)
|
|
|
|
|
qp_smoother_high_->Reset();
|
|
|
|
|
if (qp_smoother_low_)
|
|
|
|
|
qp_smoother_low_->Reset();
|
2016-04-13 02:51:02 -07:00
|
|
|
}
|
2014-09-12 11:51:47 +00:00
|
|
|
} // namespace webrtc
|