pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 1 | /* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
Ilya Nikolaevskiy | bf35298 | 2017-10-02 10:08:25 +0200 | [diff] [blame] | 2 | * |
| 3 | * Use of this source code is governed by a BSD-style license |
| 4 | * that can be found in the LICENSE file in the root of the source |
| 5 | * tree. An additional intellectual property rights grant can be found |
| 6 | * in the file PATENTS. All contributing project authors may |
| 7 | * be found in the AUTHORS file in the root of the source tree. |
| 8 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 9 | #ifndef MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_ |
| 10 | #define MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_ |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 11 | |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 12 | #include <vector> |
pbos@webrtc.org | e728ee0 | 2014-12-17 13:43:55 +0000 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/video_coding/codecs/vp8/temporal_layers.h" |
| 15 | #include "modules/video_coding/utility/frame_dropper.h" |
| 16 | #include "rtc_base/rate_statistics.h" |
| 17 | #include "rtc_base/timeutils.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 18 | #include "typedefs.h" // NOLINT(build/include) |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 19 | |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 20 | namespace webrtc { |
| 21 | |
| 22 | struct CodecSpecificInfoVP8; |
sprang | b0fdfea | 2016-03-01 05:51:16 -0800 | [diff] [blame] | 23 | class Clock; |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 24 | |
| 25 | class ScreenshareLayers : public TemporalLayers { |
| 26 | public: |
sprang@webrtc.org | 70f74f3 | 2014-12-17 10:57:10 +0000 | [diff] [blame] | 27 | static const double kMaxTL0FpsReduction; |
| 28 | static const double kAcceptableTargetOvershoot; |
sprang | afe1f74 | 2016-04-12 02:45:13 -0700 | [diff] [blame] | 29 | static const int kMaxFrameIntervalMs; |
sprang@webrtc.org | 70f74f3 | 2014-12-17 10:57:10 +0000 | [diff] [blame] | 30 | |
sprang | b0fdfea | 2016-03-01 05:51:16 -0800 | [diff] [blame] | 31 | ScreenshareLayers(int num_temporal_layers, |
| 32 | uint8_t initial_tl0_pic_idx, |
| 33 | Clock* clock); |
| 34 | virtual ~ScreenshareLayers(); |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 35 | |
| 36 | // Returns the recommended VP8 encode flags needed. May refresh the decoder |
| 37 | // and/or update the reference buffers. |
pbos | 18ad1d4 | 2017-05-04 05:04:46 -0700 | [diff] [blame] | 38 | TemporalLayers::FrameConfig UpdateLayerConfig(uint32_t timestamp) override; |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 39 | |
Erik Språng | bb60a3a | 2018-03-19 18:25:10 +0100 | [diff] [blame^] | 40 | // New target bitrate, per temporal layer. |
| 41 | void OnRatesUpdated(const std::vector<uint32_t>& bitrates_bps, |
| 42 | int framerate_fps) override; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 43 | |
| 44 | // Update the encoder configuration with target bitrates or other parameters. |
| 45 | // Returns true iff the configuration was actually modified. |
Anders Carlsson | beabdcb | 2018-01-24 10:25:15 +0100 | [diff] [blame] | 46 | bool UpdateConfiguration(Vp8EncoderConfig* cfg) override; |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 47 | |
Erik Språng | 2c4c914 | 2015-06-24 11:24:44 +0200 | [diff] [blame] | 48 | void PopulateCodecSpecific(bool base_layer_sync, |
pbos | 18ad1d4 | 2017-05-04 05:04:46 -0700 | [diff] [blame] | 49 | const TemporalLayers::FrameConfig& tl_config, |
Erik Språng | 2c4c914 | 2015-06-24 11:24:44 +0200 | [diff] [blame] | 50 | CodecSpecificInfoVP8* vp8_info, |
| 51 | uint32_t timestamp) override; |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 52 | |
Peter Boström | 1436c83 | 2017-03-27 15:01:49 -0400 | [diff] [blame] | 53 | void FrameEncoded(unsigned int size, int qp) override; |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 54 | |
brandtr | 080830c | 2017-05-03 03:25:53 -0700 | [diff] [blame] | 55 | uint8_t Tl0PicIdx() const override; |
| 56 | |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 57 | private: |
sprang | 916170a | 2017-05-23 07:47:55 -0700 | [diff] [blame] | 58 | enum class TemporalLayerState : int { kDrop, kTl0, kTl1, kTl1Sync }; |
pbos | 51f083c | 2017-05-04 06:39:04 -0700 | [diff] [blame] | 59 | |
Erik Språng | 2c4c914 | 2015-06-24 11:24:44 +0200 | [diff] [blame] | 60 | bool TimeToSync(int64_t timestamp) const; |
sprang | c7805db | 2016-11-25 08:09:43 -0800 | [diff] [blame] | 61 | uint32_t GetCodecTargetBitrateKbps() const; |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 62 | |
sprang | b0fdfea | 2016-03-01 05:51:16 -0800 | [diff] [blame] | 63 | Clock* const clock_; |
| 64 | |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 65 | int number_of_temporal_layers_; |
| 66 | bool last_base_layer_sync_; |
| 67 | uint8_t tl0_pic_idx_; |
| 68 | int active_layer_; |
Erik Språng | 2c4c914 | 2015-06-24 11:24:44 +0200 | [diff] [blame] | 69 | int64_t last_timestamp_; |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 70 | int64_t last_sync_timestamp_; |
sprang | afe1f74 | 2016-04-12 02:45:13 -0700 | [diff] [blame] | 71 | int64_t last_emitted_tl0_timestamp_; |
Erik Språng | 27a457d | 2018-01-12 11:00:21 +0100 | [diff] [blame] | 72 | int64_t last_frame_time_ms_; |
Erik Språng | 2c4c914 | 2015-06-24 11:24:44 +0200 | [diff] [blame] | 73 | rtc::TimestampWrapAroundHandler time_wrap_handler_; |
| 74 | int min_qp_; |
| 75 | int max_qp_; |
| 76 | uint32_t max_debt_bytes_; |
sprang | ac4a90d | 2016-12-28 05:58:07 -0800 | [diff] [blame] | 77 | |
| 78 | // Configured max framerate. |
| 79 | rtc::Optional<uint32_t> target_framerate_; |
| 80 | // Incoming framerate from capturer. |
sprang | 0ad0de6 | 2017-01-11 05:01:32 -0800 | [diff] [blame] | 81 | rtc::Optional<uint32_t> capture_framerate_; |
sprang | ac4a90d | 2016-12-28 05:58:07 -0800 | [diff] [blame] | 82 | // Tracks what framerate we actually encode, and drops frames on overshoot. |
| 83 | RateStatistics encode_framerate_; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 84 | bool bitrate_updated_; |
Erik Språng | 2c4c914 | 2015-06-24 11:24:44 +0200 | [diff] [blame] | 85 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 86 | static constexpr int kMaxNumTemporalLayers = 2; |
Erik Språng | 2c4c914 | 2015-06-24 11:24:44 +0200 | [diff] [blame] | 87 | struct TemporalLayer { |
| 88 | TemporalLayer() |
| 89 | : state(State::kNormal), |
| 90 | enhanced_max_qp(-1), |
| 91 | last_qp(-1), |
| 92 | debt_bytes_(0), |
| 93 | target_rate_kbps_(0) {} |
| 94 | |
| 95 | enum class State { |
| 96 | kNormal, |
| 97 | kDropped, |
| 98 | kReencoded, |
| 99 | kQualityBoost, |
| 100 | } state; |
| 101 | |
| 102 | int enhanced_max_qp; |
| 103 | int last_qp; |
| 104 | uint32_t debt_bytes_; |
| 105 | uint32_t target_rate_kbps_; |
| 106 | |
| 107 | void UpdateDebt(int64_t delta_ms); |
| 108 | } layers_[kMaxNumTemporalLayers]; |
sprang | b0fdfea | 2016-03-01 05:51:16 -0800 | [diff] [blame] | 109 | |
| 110 | void UpdateHistograms(); |
| 111 | // Data for histogram statistics. |
| 112 | struct Stats { |
| 113 | int64_t first_frame_time_ms_ = -1; |
| 114 | int64_t num_tl0_frames_ = 0; |
| 115 | int64_t num_tl1_frames_ = 0; |
| 116 | int64_t num_dropped_frames_ = 0; |
| 117 | int64_t num_overshoots_ = 0; |
| 118 | int64_t tl0_qp_sum_ = 0; |
| 119 | int64_t tl1_qp_sum_ = 0; |
| 120 | int64_t tl0_target_bitrate_sum_ = 0; |
| 121 | int64_t tl1_target_bitrate_sum_ = 0; |
| 122 | } stats_; |
pbos@webrtc.org | 9115cde | 2014-12-09 10:36:40 +0000 | [diff] [blame] | 123 | }; |
| 124 | } // namespace webrtc |
| 125 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 126 | #endif // MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_ |