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" |
15 #include "webrtc/common_video/include/i420_buffer_pool.h" | 17 #include "webrtc/video_encoder.h" |
| 18 #include "webrtc/base/optional.h" |
| 19 #include "webrtc/base/sequenced_task_checker.h" |
16 #include "webrtc/modules/video_coding/utility/moving_average.h" | 20 #include "webrtc/modules/video_coding/utility/moving_average.h" |
17 | 21 |
18 namespace webrtc { | 22 namespace webrtc { |
| 23 |
| 24 // An interface for a class that receives scale up/down requests. |
| 25 class ScalingObserverInterface { |
| 26 public: |
| 27 enum ScaleReason : size_t { kQuality = 0, kCpu = 1 }; |
| 28 static const size_t kScaleReasonSize = 2; |
| 29 // Called to signal that we can handle larger frames. |
| 30 virtual void ScaleUp(ScaleReason reason) = 0; |
| 31 // Called to signal that encoder to scale down. |
| 32 virtual void ScaleDown(ScaleReason reason) = 0; |
| 33 |
| 34 protected: |
| 35 virtual ~ScalingObserverInterface() {} |
| 36 }; |
| 37 |
| 38 // QualityScaler runs asynchronously and monitors QP values of encoded frames. |
| 39 // It holds a reference to a ScalingObserverInterface implementation to signal |
| 40 // an intent to scale up or down. |
19 class QualityScaler { | 41 class QualityScaler { |
20 public: | 42 public: |
21 struct Resolution { | 43 // Construct a QualityScaler with a given |observer|. |
22 int width; | 44 // This starts the quality scaler periodically checking what the average QP |
23 int height; | 45 // has been recently. |
24 }; | 46 QualityScaler(ScalingObserverInterface* observer, VideoCodecType codec_type); |
| 47 // If specific thresholds are desired these can be supplied as |thresholds|. |
| 48 QualityScaler(ScalingObserverInterface* observer, |
| 49 VideoEncoder::QpThresholds thresholds); |
| 50 virtual ~QualityScaler(); |
| 51 // Should be called each time the encoder drops a frame |
| 52 void ReportDroppedFrame(); |
| 53 // Inform the QualityScaler of the last seen QP. |
| 54 void ReportQP(int qp); |
25 | 55 |
26 QualityScaler(); | 56 // The following members declared protected for testing purposes |
27 void Init(VideoCodecType codec_type, | 57 protected: |
28 int initial_bitrate_kbps, | 58 QualityScaler(ScalingObserverInterface* observer, |
29 int width, | 59 VideoEncoder::QpThresholds thresholds, |
30 int height, | 60 int64_t sampling_period); |
31 int fps); | |
32 void Init(int low_qp_threshold, | |
33 int high_qp_threshold, | |
34 int initial_bitrate_kbps, | |
35 int width, | |
36 int height, | |
37 int fps); | |
38 void ReportFramerate(int framerate); | |
39 void ReportQP(int qp); | |
40 void ReportDroppedFrame(); | |
41 void OnEncodeFrame(int width, int height); | |
42 Resolution GetScaledResolution() const; | |
43 rtc::scoped_refptr<VideoFrameBuffer> GetScaledBuffer( | |
44 const rtc::scoped_refptr<VideoFrameBuffer>& frame); | |
45 int downscale_shift() const { return downscale_shift_; } | |
46 | 61 |
47 private: | 62 private: |
| 63 class CheckQPTask; |
| 64 void CheckQP(); |
48 void ClearSamples(); | 65 void ClearSamples(); |
49 void ScaleUp(); | 66 void ReportQPLow(); |
50 void ScaleDown(); | 67 void ReportQPHigh(); |
51 void UpdateTargetResolution(int width, int height); | 68 int64_t GetSamplingPeriodMs() const; |
52 | 69 |
53 I420BufferPool pool_; | 70 CheckQPTask* check_qp_task_ GUARDED_BY(&task_checker_); |
| 71 ScalingObserverInterface* const observer_ GUARDED_BY(&task_checker_); |
| 72 rtc::SequencedTaskChecker task_checker_; |
54 | 73 |
55 size_t num_samples_downscale_; | 74 const int64_t sampling_period_ms_; |
56 size_t num_samples_upscale_; | 75 bool fast_rampup_ GUARDED_BY(&task_checker_); |
57 bool fast_rampup_; | 76 MovingAverage average_qp_ GUARDED_BY(&task_checker_); |
58 MovingAverage average_qp_; | 77 MovingAverage framedrop_percent_ GUARDED_BY(&task_checker_); |
59 MovingAverage framedrop_percent_; | |
60 | 78 |
61 int low_qp_threshold_; | 79 VideoEncoder::QpThresholds thresholds_ GUARDED_BY(&task_checker_); |
62 int high_qp_threshold_; | |
63 Resolution target_res_; | |
64 | |
65 int downscale_shift_; | |
66 int maximum_shift_; | |
67 }; | 80 }; |
68 | |
69 } // namespace webrtc | 81 } // namespace webrtc |
70 | 82 |
71 #endif // WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ | 83 #endif // WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |
OLD | NEW |