OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ | 11 #ifndef WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |
12 #define WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ | 12 #define WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |
13 | 13 |
| 14 #include <utility> |
| 15 |
14 #include "webrtc/common_types.h" | 16 #include "webrtc/common_types.h" |
| 17 #include "webrtc/video_frame.h" |
| 18 #include "webrtc/base/optional.h" |
| 19 #include "webrtc/base/sequenced_task_checker.h" |
15 #include "webrtc/common_video/include/i420_buffer_pool.h" | 20 #include "webrtc/common_video/include/i420_buffer_pool.h" |
16 #include "webrtc/modules/video_coding/utility/moving_average.h" | 21 #include "webrtc/modules/video_coding/utility/moving_average.h" |
17 | 22 |
18 namespace webrtc { | 23 namespace webrtc { |
| 24 |
| 25 // An interface for a class that receives scale up/down requests. |
| 26 class ScalingObserverInterface { |
| 27 public: |
| 28 enum ScaleReason { kQuality, kCpu }; |
| 29 static const size_t ScaleReasonSize = 2; |
| 30 // Called to signal that we can handle larger frames. |
| 31 virtual void ScaleUp(ScaleReason reason) = 0; |
| 32 // Called to signal that encoder to scale down. |
| 33 virtual void ScaleDown(ScaleReason reason) = 0; |
| 34 |
| 35 protected: |
| 36 virtual ~ScalingObserverInterface() {} |
| 37 }; |
| 38 |
| 39 // QualityScaler runs asynchronously and monitors QP values of encoded frames. |
| 40 // It holds a reference to a ScalingObserverInterface implementation to signal |
| 41 // an intent to scale up or down. |
19 class QualityScaler { | 42 class QualityScaler { |
20 public: | 43 public: |
21 struct Resolution { | 44 typedef std::pair<int, int> QPThresholds; |
22 int width; | 45 struct Settings { |
23 int height; | 46 Settings(bool on, int low, int high) |
| 47 : enabled(on), |
| 48 thresholds(rtc::Optional<QPThresholds>(QPThresholds(low, high))) {} |
| 49 explicit Settings(bool on) : enabled(on) {} |
| 50 const bool enabled; |
| 51 const rtc::Optional<QPThresholds> thresholds; |
24 }; | 52 }; |
25 | 53 |
26 QualityScaler(); | 54 // Construct a QualityScaler with a given |observer| |
27 void Init(VideoCodecType codec_type, | 55 explicit QualityScaler(ScalingObserverInterface* observer); |
28 int initial_bitrate_kbps, | 56 virtual ~QualityScaler(); |
29 int width, | 57 // Init starts the quality scaler periodically checking what the average QP |
30 int height, | 58 // has been recently. |
31 int fps); | 59 void Init(VideoCodecType codec_type); |
32 void Init(int low_qp_threshold, | 60 // If specific thresholds are desired these can be supplied as |thresholds|. |
33 int high_qp_threshold, | 61 void Init(QPThresholds thresholds); |
34 int initial_bitrate_kbps, | 62 // Stop must be called to stop the periodic task before QualityScaler is |
35 int width, | 63 // destroyed. |
36 int height, | 64 void Stop(); |
37 int fps); | 65 // Should be called each time the encoder drops a frame |
38 void ReportFramerate(int framerate); | 66 void ReportDroppedFrame(); |
| 67 // Inform the QualityScaler of the last seen QP. |
39 void ReportQP(int qp); | 68 void ReportQP(int qp); |
40 void ReportDroppedFrame(); | 69 |
41 void OnEncodeFrame(int width, int height); | 70 // This method declared virtual to help with testing. |
42 Resolution GetScaledResolution() const; | 71 virtual int64_t GetTimeoutMs(); |
43 rtc::scoped_refptr<VideoFrameBuffer> GetScaledBuffer( | |
44 const rtc::scoped_refptr<VideoFrameBuffer>& frame); | |
45 int downscale_shift() const { return downscale_shift_; } | |
46 | 72 |
47 private: | 73 private: |
| 74 class CheckQPTask; |
| 75 void CheckQP(); |
48 void ClearSamples(); | 76 void ClearSamples(); |
49 void ScaleUp(); | 77 void ReportQPLow(); |
50 void ScaleDown(); | 78 void ReportQPHigh(); |
51 void UpdateTargetResolution(int width, int height); | |
52 | 79 |
53 I420BufferPool pool_; | 80 ScalingObserverInterface* observer_ GUARDED_BY(&task_checker_); |
| 81 CheckQPTask* check_qp_task_ GUARDED_BY(&task_checker_); |
| 82 rtc::SequencedTaskChecker task_checker_; |
54 | 83 |
55 size_t num_samples_downscale_; | 84 bool fast_rampup_ GUARDED_BY(&task_checker_); |
56 size_t num_samples_upscale_; | 85 int64_t measure_interval_ GUARDED_BY(&task_checker_); |
57 bool fast_rampup_; | 86 MovingAverage average_qp_ GUARDED_BY(&task_checker_); |
58 MovingAverage average_qp_; | 87 MovingAverage framedrop_percent_ GUARDED_BY(&task_checker_); |
59 MovingAverage framedrop_percent_; | |
60 | 88 |
61 int low_qp_threshold_; | 89 int low_qp_threshold_ GUARDED_BY(&task_checker_); |
62 int high_qp_threshold_; | 90 int high_qp_threshold_ GUARDED_BY(&task_checker_); |
63 Resolution target_res_; | |
64 | 91 |
65 int downscale_shift_; | 92 static const auto scale_reason_ = ScalingObserverInterface::kQuality; |
66 int maximum_shift_; | |
67 }; | 93 }; |
68 | 94 |
69 } // namespace webrtc | 95 } // namespace webrtc |
70 | 96 |
71 #endif // WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ | 97 #endif // WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |
OLD | NEW |