blob: 987d49f1a83d7b76ba860c3f68f05a4404fe15f8 [file] [log] [blame]
pbos@webrtc.orga0d78272014-09-12 11:51:47 +00001/*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_
12#define MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Åsa Perssona945aee2018-04-24 16:53:25 +020017#include <memory>
kthelgason876222f2016-11-29 01:44:11 -080018
Åsa Persson517678c2019-05-06 14:17:35 +020019#include "absl/types/optional.h"
Henrik Boström012aa372020-04-27 17:40:55 +020020#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/video_codecs/video_encoder.h"
Åsa Perssona945aee2018-04-24 16:53:25 +020022#include "rtc_base/experiments/quality_scaling_experiment.h"
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +010023#include "rtc_base/numerics/moving_average.h"
Henrik Boström012aa372020-04-27 17:40:55 +020024#include "rtc_base/ref_count.h"
25#include "rtc_base/ref_counted_object.h"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020026#include "rtc_base/synchronization/sequence_checker.h"
Mirko Bonadei20e4c802020-11-23 11:07:42 +010027#include "rtc_base/system/no_unique_address.h"
Sebastian Janssoncda86dd2019-03-11 17:26:36 +010028#include "rtc_base/task_queue.h"
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000029
30namespace webrtc {
kthelgason876222f2016-11-29 01:44:11 -080031
Henrik Boström012aa372020-04-27 17:40:55 +020032class QualityScalerQpUsageHandlerCallbackInterface;
33class QualityScalerQpUsageHandlerInterface;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000034
kthelgason876222f2016-11-29 01:44:11 -080035// QualityScaler runs asynchronously and monitors QP values of encoded frames.
Henrik Boström012aa372020-04-27 17:40:55 +020036// It holds a reference to a QualityScalerQpUsageHandlerInterface implementation
37// to signal an overuse or underuse of QP (which indicate a desire to scale the
38// video stream down or up).
kthelgason876222f2016-11-29 01:44:11 -080039class QualityScaler {
40 public:
Henrik Boström012aa372020-04-27 17:40:55 +020041 // Construct a QualityScaler with given |thresholds| and |handler|.
kthelgason876222f2016-11-29 01:44:11 -080042 // This starts the quality scaler periodically checking what the average QP
43 // has been recently.
Henrik Boström012aa372020-04-27 17:40:55 +020044 QualityScaler(QualityScalerQpUsageHandlerInterface* handler,
kthelgason876222f2016-11-29 01:44:11 -080045 VideoEncoder::QpThresholds thresholds);
46 virtual ~QualityScaler();
Åsa Perssona945aee2018-04-24 16:53:25 +020047 // Should be called each time a frame is dropped at encoding.
48 void ReportDroppedFrameByMediaOpt();
49 void ReportDroppedFrameByEncoder();
kthelgason876222f2016-11-29 01:44:11 -080050 // Inform the QualityScaler of the last seen QP.
Sebastian Janssonb6789402019-03-01 15:40:49 +010051 void ReportQp(int qp, int64_t time_sent_us);
kthelgason876222f2016-11-29 01:44:11 -080052
Åsa Persson12314192019-06-20 15:45:07 +020053 void SetQpThresholds(VideoEncoder::QpThresholds thresholds);
Åsa Perssone644a032019-11-08 15:56:00 +010054 bool QpFastFilterLow() const;
Åsa Persson12314192019-06-20 15:45:07 +020055
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020056 // The following members declared protected for testing purposes.
kthelgason876222f2016-11-29 01:44:11 -080057 protected:
Henrik Boström012aa372020-04-27 17:40:55 +020058 QualityScaler(QualityScalerQpUsageHandlerInterface* handler,
kthelgason876222f2016-11-29 01:44:11 -080059 VideoEncoder::QpThresholds thresholds,
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020060 int64_t sampling_period_ms);
kthelgason876222f2016-11-29 01:44:11 -080061
62 private:
Åsa Perssona945aee2018-04-24 16:53:25 +020063 class QpSmoother;
Henrik Boström012aa372020-04-27 17:40:55 +020064 class CheckQpTask;
65 class CheckQpTaskHandlerCallback;
Yves Gerey3e707812018-11-28 16:47:49 +010066
Henrik Boström012aa372020-04-27 17:40:55 +020067 enum class CheckQpResult {
68 kInsufficientSamples,
69 kNormalQp,
70 kHighQp,
71 kLowQp,
72 };
73
74 // Starts checking for QP in a delayed task. When the resulting CheckQpTask
75 // completes, it will invoke this method again, ensuring that we always
76 // periodically check for QP. See CheckQpTask for more details. We never run
77 // more than one CheckQpTask at a time.
78 void StartNextCheckQpTask();
79
80 CheckQpResult CheckQp() const;
kthelgason876222f2016-11-29 01:44:11 -080081 void ClearSamples();
kthelgason876222f2016-11-29 01:44:11 -080082
Henrik Boström012aa372020-04-27 17:40:55 +020083 std::unique_ptr<CheckQpTask> pending_qp_task_ RTC_GUARDED_BY(&task_checker_);
84 QualityScalerQpUsageHandlerInterface* const handler_
85 RTC_GUARDED_BY(&task_checker_);
Mirko Bonadei20e4c802020-11-23 11:07:42 +010086 RTC_NO_UNIQUE_ADDRESS SequenceChecker task_checker_;
kthelgason876222f2016-11-29 01:44:11 -080087
Åsa Persson12314192019-06-20 15:45:07 +020088 VideoEncoder::QpThresholds thresholds_ RTC_GUARDED_BY(&task_checker_);
kthelgason876222f2016-11-29 01:44:11 -080089 const int64_t sampling_period_ms_;
danilchap56359be2017-09-07 07:53:45 -070090 bool fast_rampup_ RTC_GUARDED_BY(&task_checker_);
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +010091 rtc::MovingAverage average_qp_ RTC_GUARDED_BY(&task_checker_);
92 rtc::MovingAverage framedrop_percent_media_opt_
93 RTC_GUARDED_BY(&task_checker_);
94 rtc::MovingAverage framedrop_percent_all_ RTC_GUARDED_BY(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +020095
96 // Used by QualityScalingExperiment.
97 const bool experiment_enabled_;
98 QualityScalingExperiment::Config config_ RTC_GUARDED_BY(&task_checker_);
99 std::unique_ptr<QpSmoother> qp_smoother_high_ RTC_GUARDED_BY(&task_checker_);
100 std::unique_ptr<QpSmoother> qp_smoother_low_ RTC_GUARDED_BY(&task_checker_);
Åsa Persson517678c2019-05-06 14:17:35 +0200101
102 const size_t min_frames_needed_;
103 const double initial_scale_factor_;
104 const absl::optional<double> scale_factor_;
kthelgason876222f2016-11-29 01:44:11 -0800105};
Henrik Boström012aa372020-04-27 17:40:55 +0200106
107// Reacts to QP being too high or too low. For best quality, when QP is high it
108// is desired to decrease the resolution or frame rate of the stream and when QP
109// is low it is desired to increase the resolution or frame rate of the stream.
110// Whether to reconfigure the stream is ultimately up to the handler, which is
111// able to respond asynchronously.
112class QualityScalerQpUsageHandlerInterface {
113 public:
114 virtual ~QualityScalerQpUsageHandlerInterface();
115
Evan Shrubsolea1c77f62020-08-10 11:01:06 +0200116 virtual void OnReportQpUsageHigh() = 0;
117 virtual void OnReportQpUsageLow() = 0;
Henrik Boström012aa372020-04-27 17:40:55 +0200118};
119
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000120} // namespace webrtc
121
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200122#endif // MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_