pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |
| 12 | #define MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 17 | #include <memory> |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 18 | |
Åsa Persson | 517678c | 2019-05-06 14:17:35 +0200 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
Henrik Boström | 012aa37 | 2020-04-27 17:40:55 +0200 | [diff] [blame] | 20 | #include "api/scoped_refptr.h" |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame^] | 21 | #include "api/sequence_checker.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "api/video_codecs/video_encoder.h" |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 23 | #include "rtc_base/experiments/quality_scaling_experiment.h" |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 24 | #include "rtc_base/numerics/moving_average.h" |
Henrik Boström | 012aa37 | 2020-04-27 17:40:55 +0200 | [diff] [blame] | 25 | #include "rtc_base/ref_count.h" |
| 26 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 20e4c80 | 2020-11-23 11:07:42 +0100 | [diff] [blame] | 27 | #include "rtc_base/system/no_unique_address.h" |
Sebastian Jansson | cda86dd | 2019-03-11 17:26:36 +0100 | [diff] [blame] | 28 | #include "rtc_base/task_queue.h" |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 29 | |
| 30 | namespace webrtc { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 31 | |
Henrik Boström | 012aa37 | 2020-04-27 17:40:55 +0200 | [diff] [blame] | 32 | class QualityScalerQpUsageHandlerCallbackInterface; |
| 33 | class QualityScalerQpUsageHandlerInterface; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 34 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 35 | // QualityScaler runs asynchronously and monitors QP values of encoded frames. |
Henrik Boström | 012aa37 | 2020-04-27 17:40:55 +0200 | [diff] [blame] | 36 | // 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). |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 39 | class QualityScaler { |
| 40 | public: |
Henrik Boström | 012aa37 | 2020-04-27 17:40:55 +0200 | [diff] [blame] | 41 | // Construct a QualityScaler with given |thresholds| and |handler|. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 42 | // This starts the quality scaler periodically checking what the average QP |
| 43 | // has been recently. |
Henrik Boström | 012aa37 | 2020-04-27 17:40:55 +0200 | [diff] [blame] | 44 | QualityScaler(QualityScalerQpUsageHandlerInterface* handler, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 45 | VideoEncoder::QpThresholds thresholds); |
| 46 | virtual ~QualityScaler(); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 47 | // Should be called each time a frame is dropped at encoding. |
| 48 | void ReportDroppedFrameByMediaOpt(); |
| 49 | void ReportDroppedFrameByEncoder(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 50 | // Inform the QualityScaler of the last seen QP. |
Sebastian Jansson | b678940 | 2019-03-01 15:40:49 +0100 | [diff] [blame] | 51 | void ReportQp(int qp, int64_t time_sent_us); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 52 | |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 53 | void SetQpThresholds(VideoEncoder::QpThresholds thresholds); |
Åsa Persson | e644a03 | 2019-11-08 15:56:00 +0100 | [diff] [blame] | 54 | bool QpFastFilterLow() const; |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 55 | |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 56 | // The following members declared protected for testing purposes. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 57 | protected: |
Henrik Boström | 012aa37 | 2020-04-27 17:40:55 +0200 | [diff] [blame] | 58 | QualityScaler(QualityScalerQpUsageHandlerInterface* handler, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 59 | VideoEncoder::QpThresholds thresholds, |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 60 | int64_t sampling_period_ms); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 61 | |
| 62 | private: |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 63 | class QpSmoother; |
Henrik Boström | 012aa37 | 2020-04-27 17:40:55 +0200 | [diff] [blame] | 64 | class CheckQpTask; |
| 65 | class CheckQpTaskHandlerCallback; |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 66 | |
Henrik Boström | 012aa37 | 2020-04-27 17:40:55 +0200 | [diff] [blame] | 67 | 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; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 81 | void ClearSamples(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 82 | |
Henrik Boström | 012aa37 | 2020-04-27 17:40:55 +0200 | [diff] [blame] | 83 | std::unique_ptr<CheckQpTask> pending_qp_task_ RTC_GUARDED_BY(&task_checker_); |
| 84 | QualityScalerQpUsageHandlerInterface* const handler_ |
| 85 | RTC_GUARDED_BY(&task_checker_); |
Mirko Bonadei | 20e4c80 | 2020-11-23 11:07:42 +0100 | [diff] [blame] | 86 | RTC_NO_UNIQUE_ADDRESS SequenceChecker task_checker_; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 87 | |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 88 | VideoEncoder::QpThresholds thresholds_ RTC_GUARDED_BY(&task_checker_); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 89 | const int64_t sampling_period_ms_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 90 | bool fast_rampup_ RTC_GUARDED_BY(&task_checker_); |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 91 | 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 Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 95 | |
| 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 Persson | 517678c | 2019-05-06 14:17:35 +0200 | [diff] [blame] | 101 | |
| 102 | const size_t min_frames_needed_; |
| 103 | const double initial_scale_factor_; |
| 104 | const absl::optional<double> scale_factor_; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 105 | }; |
Henrik Boström | 012aa37 | 2020-04-27 17:40:55 +0200 | [diff] [blame] | 106 | |
| 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. |
| 112 | class QualityScalerQpUsageHandlerInterface { |
| 113 | public: |
| 114 | virtual ~QualityScalerQpUsageHandlerInterface(); |
| 115 | |
Evan Shrubsole | a1c77f6 | 2020-08-10 11:01:06 +0200 | [diff] [blame] | 116 | virtual void OnReportQpUsageHigh() = 0; |
| 117 | virtual void OnReportQpUsageLow() = 0; |
Henrik Boström | 012aa37 | 2020-04-27 17:40:55 +0200 | [diff] [blame] | 118 | }; |
| 119 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 120 | } // namespace webrtc |
| 121 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 122 | #endif // MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |