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> |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 16 | #include <memory> |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/video_codecs/video_encoder.h" |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 19 | #include "rtc_base/experiments/quality_scaling_experiment.h" |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 20 | #include "rtc_base/numerics/moving_average.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/sequenced_task_checker.h" |
Sebastian Jansson | cda86dd | 2019-03-11 17:26:36 +0100 | [diff] [blame] | 22 | #include "rtc_base/task_queue.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 23 | #include "rtc_base/task_utils/repeating_task.h" |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 26 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 27 | // An interface for signaling requests to limit or increase the resolution or |
| 28 | // framerate of the captured video stream. |
| 29 | class AdaptationObserverInterface { |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 30 | public: |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 31 | // Indicates if the adaptation is due to overuse of the CPU resources, or if |
| 32 | // the quality of the encoded frames have dropped too low. |
| 33 | enum AdaptReason : size_t { kQuality = 0, kCpu = 1 }; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 34 | static const size_t kScaleReasonSize = 2; |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 35 | // Called to signal that we can handle larger or more frequent frames. |
| 36 | virtual void AdaptUp(AdaptReason reason) = 0; |
| 37 | // Called to signal that the source should reduce the resolution or framerate. |
| 38 | virtual void AdaptDown(AdaptReason reason) = 0; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 39 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 40 | protected: |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 41 | virtual ~AdaptationObserverInterface() {} |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 44 | // QualityScaler runs asynchronously and monitors QP values of encoded frames. |
Niels Möller | d692ef9 | 2017-10-04 15:28:55 +0200 | [diff] [blame] | 45 | // It holds a reference to an AdaptationObserverInterface implementation to |
| 46 | // signal an intent to scale up or down. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 47 | class QualityScaler { |
| 48 | public: |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 49 | // Construct a QualityScaler with given |thresholds| and |observer|. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 50 | // This starts the quality scaler periodically checking what the average QP |
| 51 | // has been recently. |
Sebastian Jansson | cda86dd | 2019-03-11 17:26:36 +0100 | [diff] [blame] | 52 | QualityScaler(rtc::TaskQueue* task_queue, |
| 53 | AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 54 | VideoEncoder::QpThresholds thresholds); |
| 55 | virtual ~QualityScaler(); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 56 | // Should be called each time a frame is dropped at encoding. |
| 57 | void ReportDroppedFrameByMediaOpt(); |
| 58 | void ReportDroppedFrameByEncoder(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 59 | // Inform the QualityScaler of the last seen QP. |
Sebastian Jansson | b678940 | 2019-03-01 15:40:49 +0100 | [diff] [blame] | 60 | void ReportQp(int qp, int64_t time_sent_us); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 61 | |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 62 | // The following members declared protected for testing purposes. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 63 | protected: |
Sebastian Jansson | cda86dd | 2019-03-11 17:26:36 +0100 | [diff] [blame] | 64 | QualityScaler(rtc::TaskQueue* task_queue, |
| 65 | AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 66 | VideoEncoder::QpThresholds thresholds, |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 67 | int64_t sampling_period_ms); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 68 | |
| 69 | private: |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 70 | class QpSmoother; |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 71 | |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 72 | void CheckQp(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 73 | void ClearSamples(); |
Åsa Persson | 04d5f1d | 2018-04-20 15:19:11 +0200 | [diff] [blame] | 74 | void ReportQpLow(); |
| 75 | void ReportQpHigh(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 76 | int64_t GetSamplingPeriodMs() const; |
| 77 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 78 | RepeatingTaskHandle check_qp_task_ RTC_GUARDED_BY(&task_checker_); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 79 | AdaptationObserverInterface* const observer_ RTC_GUARDED_BY(&task_checker_); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 80 | rtc::SequencedTaskChecker task_checker_; |
| 81 | |
Åsa Persson | 0ad2d8a | 2018-04-19 11:06:11 +0200 | [diff] [blame] | 82 | const VideoEncoder::QpThresholds thresholds_; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 83 | const int64_t sampling_period_ms_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 84 | bool fast_rampup_ RTC_GUARDED_BY(&task_checker_); |
Ilya Nikolaevskiy | 2634199 | 2018-11-05 12:55:18 +0100 | [diff] [blame] | 85 | rtc::MovingAverage average_qp_ RTC_GUARDED_BY(&task_checker_); |
| 86 | rtc::MovingAverage framedrop_percent_media_opt_ |
| 87 | RTC_GUARDED_BY(&task_checker_); |
| 88 | rtc::MovingAverage framedrop_percent_all_ RTC_GUARDED_BY(&task_checker_); |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 89 | |
| 90 | // Used by QualityScalingExperiment. |
| 91 | const bool experiment_enabled_; |
| 92 | QualityScalingExperiment::Config config_ RTC_GUARDED_BY(&task_checker_); |
| 93 | std::unique_ptr<QpSmoother> qp_smoother_high_ RTC_GUARDED_BY(&task_checker_); |
| 94 | std::unique_ptr<QpSmoother> qp_smoother_low_ RTC_GUARDED_BY(&task_checker_); |
| 95 | bool observed_enough_frames_ RTC_GUARDED_BY(&task_checker_); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 96 | }; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 97 | } // namespace webrtc |
| 98 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 99 | #endif // MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |