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" |
Evan Shrubsole | ce0a11d | 2020-04-16 11:36:55 +0200 | [diff] [blame] | 20 | #include "api/video/video_adaptation_reason.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "api/video_codecs/video_encoder.h" |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 22 | #include "rtc_base/experiments/quality_scaling_experiment.h" |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 23 | #include "rtc_base/numerics/moving_average.h" |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 24 | #include "rtc_base/synchronization/sequence_checker.h" |
Sebastian Jansson | cda86dd | 2019-03-11 17:26:36 +0100 | [diff] [blame] | 25 | #include "rtc_base/task_queue.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 26 | #include "rtc_base/task_utils/repeating_task.h" |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 29 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 30 | // An interface for signaling requests to limit or increase the resolution or |
| 31 | // framerate of the captured video stream. |
Henrik Boström | 48258ac | 2020-02-06 12:49:57 +0100 | [diff] [blame] | 32 | // TODO(hbos): Can we remove AdaptationObserverInterface in favor of |
| 33 | // ResourceUsageListener? If we need to adapt that is because of resource usage. |
| 34 | // A multi-stream and multi-resource aware solution needs to sparate the notion |
| 35 | // of being resource constrained from the decision to downgrade a specific |
| 36 | // stream. |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 37 | class AdaptationObserverInterface { |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 38 | public: |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 39 | // Called to signal that we can handle larger or more frequent frames. |
Evan Shrubsole | ce0a11d | 2020-04-16 11:36:55 +0200 | [diff] [blame] | 40 | virtual void AdaptUp(VideoAdaptationReason reason) = 0; |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 41 | // Called to signal that the source should reduce the resolution or framerate. |
Åsa Persson | f5e5d25 | 2019-08-16 17:24:59 +0200 | [diff] [blame] | 42 | // Returns false if a downgrade was requested but the request did not result |
| 43 | // in a new limiting resolution or fps. |
Evan Shrubsole | ce0a11d | 2020-04-16 11:36:55 +0200 | [diff] [blame] | 44 | virtual bool AdaptDown(VideoAdaptationReason reason) = 0; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 45 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 46 | protected: |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 47 | virtual ~AdaptationObserverInterface() {} |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 50 | // QualityScaler runs asynchronously and monitors QP values of encoded frames. |
Niels Möller | d692ef9 | 2017-10-04 15:28:55 +0200 | [diff] [blame] | 51 | // It holds a reference to an AdaptationObserverInterface implementation to |
| 52 | // signal an intent to scale up or down. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 53 | class QualityScaler { |
| 54 | public: |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 55 | // Construct a QualityScaler with given |thresholds| and |observer|. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 56 | // This starts the quality scaler periodically checking what the average QP |
| 57 | // has been recently. |
Evan Shrubsole | 73a5e91 | 2020-01-28 15:10:32 +0100 | [diff] [blame] | 58 | QualityScaler(AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 59 | VideoEncoder::QpThresholds thresholds); |
| 60 | virtual ~QualityScaler(); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 61 | // Should be called each time a frame is dropped at encoding. |
| 62 | void ReportDroppedFrameByMediaOpt(); |
| 63 | void ReportDroppedFrameByEncoder(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 64 | // Inform the QualityScaler of the last seen QP. |
Sebastian Jansson | b678940 | 2019-03-01 15:40:49 +0100 | [diff] [blame] | 65 | void ReportQp(int qp, int64_t time_sent_us); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 66 | |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 67 | void SetQpThresholds(VideoEncoder::QpThresholds thresholds); |
Åsa Persson | e644a03 | 2019-11-08 15:56:00 +0100 | [diff] [blame] | 68 | bool QpFastFilterLow() const; |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 69 | |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 70 | // The following members declared protected for testing purposes. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 71 | protected: |
Evan Shrubsole | 73a5e91 | 2020-01-28 15:10:32 +0100 | [diff] [blame] | 72 | QualityScaler(AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 73 | VideoEncoder::QpThresholds thresholds, |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 74 | int64_t sampling_period_ms); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 75 | |
| 76 | private: |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 77 | class QpSmoother; |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 78 | |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 79 | void CheckQp(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 80 | void ClearSamples(); |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 81 | void ReportQpLow(); |
| 82 | void ReportQpHigh(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 83 | int64_t GetSamplingPeriodMs() const; |
| 84 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 85 | RepeatingTaskHandle check_qp_task_ RTC_GUARDED_BY(&task_checker_); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 86 | AdaptationObserverInterface* const observer_ RTC_GUARDED_BY(&task_checker_); |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 87 | SequenceChecker task_checker_; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 88 | |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 89 | VideoEncoder::QpThresholds thresholds_ RTC_GUARDED_BY(&task_checker_); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 90 | const int64_t sampling_period_ms_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 91 | bool fast_rampup_ RTC_GUARDED_BY(&task_checker_); |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 92 | rtc::MovingAverage average_qp_ RTC_GUARDED_BY(&task_checker_); |
| 93 | rtc::MovingAverage framedrop_percent_media_opt_ |
| 94 | RTC_GUARDED_BY(&task_checker_); |
| 95 | rtc::MovingAverage framedrop_percent_all_ RTC_GUARDED_BY(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 96 | |
| 97 | // Used by QualityScalingExperiment. |
| 98 | const bool experiment_enabled_; |
| 99 | QualityScalingExperiment::Config config_ RTC_GUARDED_BY(&task_checker_); |
| 100 | std::unique_ptr<QpSmoother> qp_smoother_high_ RTC_GUARDED_BY(&task_checker_); |
| 101 | std::unique_ptr<QpSmoother> qp_smoother_low_ RTC_GUARDED_BY(&task_checker_); |
| 102 | bool observed_enough_frames_ RTC_GUARDED_BY(&task_checker_); |
Åsa Persson | 517678c | 2019-05-06 14:17:35 +0200 | [diff] [blame] | 103 | |
| 104 | const size_t min_frames_needed_; |
| 105 | const double initial_scale_factor_; |
| 106 | const absl::optional<double> scale_factor_; |
Åsa Persson | f5e5d25 | 2019-08-16 17:24:59 +0200 | [diff] [blame] | 107 | bool adapt_called_ RTC_GUARDED_BY(&task_checker_); |
| 108 | bool adapt_failed_ RTC_GUARDED_BY(&task_checker_); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 109 | }; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 110 | } // namespace webrtc |
| 111 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 112 | #endif // MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |