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 <memory> | |
15 #include <utility> | |
16 | |
14 #include "webrtc/common_types.h" | 17 #include "webrtc/common_types.h" |
18 #include "webrtc/video_frame.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 // Called to signal that we can handle larger frames. | |
29 virtual void ScaleUp() = 0; | |
30 // Called to signal that encoder to scale down. | |
31 virtual void ScaleDown() = 0; | |
32 | |
33 protected: | |
34 virtual ~ScalingObserverInterface() {} | |
35 }; | |
36 | |
37 // QualityScaler runs asynchronously and monitors QP values of encoded frames. | |
38 // It holds a reference to a ScalingObserverInterface implementation to signal | |
39 // an intent to scale up or down. | |
19 class QualityScaler { | 40 class QualityScaler { |
20 public: | 41 public: |
21 struct Resolution { | 42 typedef std::pair<int, int> QPThresholds; |
22 int width; | 43 struct Settings { |
23 int height; | 44 Settings(bool on, int low, int high) : enabled(on) { |
45 thresholds = std::unique_ptr<QPThresholds>(new QPThresholds(low, high)); | |
46 } | |
47 explicit Settings(bool on) : enabled(on) { | |
48 thresholds = std::unique_ptr<QPThresholds>(); | |
magjed_webrtc
2016/10/19 13:00:12
Remove this line, it's unnecessary.
kthelgason
2016/10/19 13:07:27
Acknowledged.
| |
49 } | |
50 bool enabled; | |
magjed_webrtc
2016/10/19 13:00:12
Make this const.
kthelgason
2016/10/19 13:07:27
Acknowledged.
| |
51 std::unique_ptr<QPThresholds> thresholds; | |
magjed_webrtc
2016/10/19 13:00:12
Use rtc::Optional instead to avoid an extra heap a
magjed_webrtc
2016/10/19 13:00:12
Make this const.
kthelgason
2016/10/19 13:07:27
Good point, thanks.
| |
24 }; | 52 }; |
25 | 53 |
26 QualityScaler(); | 54 QualityScaler(); |
27 void Init(VideoCodecType codec_type, | 55 virtual ~QualityScaler(); |
28 int initial_bitrate_kbps, | 56 // Init starts the quality scaler periodically checking what the average QP |
29 int width, | 57 // has been recently. |
30 int height, | 58 // arguments: |
31 int fps); | 59 // observer_: an implementation of webrtc::ScalingObserverInterface. |
32 void Init(int low_qp_threshold, | 60 // codec_type: either H264 or VP8, used to determine default thresholds. |
33 int high_qp_threshold, | 61 void Init(ScalingObserverInterface* obeserver_, VideoCodecType codec_type); |
34 int initial_bitrate_kbps, | 62 // If specific thresholds are desired these can be supplied instead of |
35 int width, | 63 // codec_type_. |
36 int height, | 64 void Init(ScalingObserverInterface* obeserver_, |
37 int fps); | 65 int low_qp_threshold, |
38 void ReportFramerate(int framerate); | 66 int high_qp_threshold); |
67 // Stop must be called to stop the periodic task before QualityScaler is | |
68 // destroyed. | |
69 void Stop(); | |
70 // Should be called each time the encoder drops a frame | |
71 void ReportDroppedFrame(); | |
72 // Inform the QualityScaler of the last seen QP. | |
39 void ReportQP(int qp); | 73 void ReportQP(int qp); |
40 void ReportDroppedFrame(); | 74 |
41 void OnEncodeFrame(int width, int height); | 75 // This method declared virtual to help with testing. |
42 Resolution GetScaledResolution() const; | 76 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 | 77 |
47 private: | 78 private: |
79 class CheckQPTask; | |
80 void CheckQP(); | |
48 void ClearSamples(); | 81 void ClearSamples(); |
49 void ScaleUp(); | 82 void ReportQPLow(); |
50 void ScaleDown(); | 83 void ReportQPHigh(); |
51 void UpdateTargetResolution(int width, int height); | |
52 | 84 |
53 I420BufferPool pool_; | 85 ScalingObserverInterface* observer_ GUARDED_BY(&task_checker_); |
86 CheckQPTask* check_qp_task_ GUARDED_BY(&task_checker_); | |
87 rtc::SequencedTaskChecker task_checker_; | |
54 | 88 |
55 size_t num_samples_downscale_; | 89 bool fast_rampup_ GUARDED_BY(&task_checker_); |
56 size_t num_samples_upscale_; | 90 int64_t measure_interval_ GUARDED_BY(&task_checker_); |
57 bool fast_rampup_; | 91 MovingAverage average_qp_ GUARDED_BY(&task_checker_); |
58 MovingAverage average_qp_; | 92 MovingAverage framedrop_percent_ GUARDED_BY(&task_checker_); |
59 MovingAverage framedrop_percent_; | |
60 | 93 |
61 int low_qp_threshold_; | 94 int low_qp_threshold_ GUARDED_BY(&task_checker_); |
62 int high_qp_threshold_; | 95 int high_qp_threshold_ GUARDED_BY(&task_checker_); |
63 Resolution target_res_; | |
64 | |
65 int downscale_shift_; | |
66 int maximum_shift_; | |
67 }; | 96 }; |
68 | 97 |
69 } // namespace webrtc | 98 } // namespace webrtc |
70 | 99 |
71 #endif // WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ | 100 #endif // WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |
OLD | NEW |