blob: 794b02b8522fe7fa3bc1754b2be105f2e24318cb [file] [log] [blame]
pbos@webrtc.org9115cde2014-12-09 10:36:40 +00001/* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
Ilya Nikolaevskiybf352982017-10-02 10:08:25 +02002 *
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 Bonadei92ea95e2017-09-15 06:47:31 +02009#ifndef MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_
10#define MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000011
Erik Språng08127a92016-11-16 16:41:30 +010012#include <vector>
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#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 Bonadei71207422017-09-15 13:58:09 +020018#include "typedefs.h" // NOLINT(build/include)
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000019
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000020namespace webrtc {
21
22struct CodecSpecificInfoVP8;
sprangb0fdfea2016-03-01 05:51:16 -080023class Clock;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000024
25class ScreenshareLayers : public TemporalLayers {
26 public:
sprang@webrtc.org70f74f32014-12-17 10:57:10 +000027 static const double kMaxTL0FpsReduction;
28 static const double kAcceptableTargetOvershoot;
sprangafe1f742016-04-12 02:45:13 -070029 static const int kMaxFrameIntervalMs;
sprang@webrtc.org70f74f32014-12-17 10:57:10 +000030
sprangb0fdfea2016-03-01 05:51:16 -080031 ScreenshareLayers(int num_temporal_layers,
32 uint8_t initial_tl0_pic_idx,
33 Clock* clock);
34 virtual ~ScreenshareLayers();
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000035
36 // Returns the recommended VP8 encode flags needed. May refresh the decoder
37 // and/or update the reference buffers.
pbos18ad1d42017-05-04 05:04:46 -070038 TemporalLayers::FrameConfig UpdateLayerConfig(uint32_t timestamp) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000039
Erik Språng08127a92016-11-16 16:41:30 +010040 // Update state based on new bitrate target and incoming framerate.
41 // Returns the bitrate allocation for the active temporal layers.
42 std::vector<uint32_t> OnRatesUpdated(int bitrate_kbps,
43 int max_bitrate_kbps,
44 int framerate) override;
45
46 // Update the encoder configuration with target bitrates or other parameters.
47 // Returns true iff the configuration was actually modified.
Anders Carlssonbeabdcb2018-01-24 10:25:15 +010048 bool UpdateConfiguration(Vp8EncoderConfig* cfg) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000049
Erik Språng2c4c9142015-06-24 11:24:44 +020050 void PopulateCodecSpecific(bool base_layer_sync,
pbos18ad1d42017-05-04 05:04:46 -070051 const TemporalLayers::FrameConfig& tl_config,
Erik Språng2c4c9142015-06-24 11:24:44 +020052 CodecSpecificInfoVP8* vp8_info,
53 uint32_t timestamp) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000054
Peter Boström1436c832017-03-27 15:01:49 -040055 void FrameEncoded(unsigned int size, int qp) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000056
brandtr080830c2017-05-03 03:25:53 -070057 uint8_t Tl0PicIdx() const override;
58
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000059 private:
sprang916170a2017-05-23 07:47:55 -070060 enum class TemporalLayerState : int { kDrop, kTl0, kTl1, kTl1Sync };
pbos51f083c2017-05-04 06:39:04 -070061
Erik Språng2c4c9142015-06-24 11:24:44 +020062 bool TimeToSync(int64_t timestamp) const;
sprangc7805db2016-11-25 08:09:43 -080063 uint32_t GetCodecTargetBitrateKbps() const;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000064
sprangb0fdfea2016-03-01 05:51:16 -080065 Clock* const clock_;
66
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000067 int number_of_temporal_layers_;
68 bool last_base_layer_sync_;
69 uint8_t tl0_pic_idx_;
70 int active_layer_;
Erik Språng2c4c9142015-06-24 11:24:44 +020071 int64_t last_timestamp_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000072 int64_t last_sync_timestamp_;
sprangafe1f742016-04-12 02:45:13 -070073 int64_t last_emitted_tl0_timestamp_;
Erik Språng27a457d2018-01-12 11:00:21 +010074 int64_t last_frame_time_ms_;
Erik Språng2c4c9142015-06-24 11:24:44 +020075 rtc::TimestampWrapAroundHandler time_wrap_handler_;
76 int min_qp_;
77 int max_qp_;
78 uint32_t max_debt_bytes_;
sprangac4a90d2016-12-28 05:58:07 -080079
80 // Configured max framerate.
81 rtc::Optional<uint32_t> target_framerate_;
82 // Incoming framerate from capturer.
sprang0ad0de62017-01-11 05:01:32 -080083 rtc::Optional<uint32_t> capture_framerate_;
sprangac4a90d2016-12-28 05:58:07 -080084 // Tracks what framerate we actually encode, and drops frames on overshoot.
85 RateStatistics encode_framerate_;
Erik Språng08127a92016-11-16 16:41:30 +010086 bool bitrate_updated_;
Erik Språng2c4c9142015-06-24 11:24:44 +020087
sprang429600d2017-01-26 06:12:26 -080088 static constexpr int kMaxNumTemporalLayers = 2;
Erik Språng2c4c9142015-06-24 11:24:44 +020089 struct TemporalLayer {
90 TemporalLayer()
91 : state(State::kNormal),
92 enhanced_max_qp(-1),
93 last_qp(-1),
94 debt_bytes_(0),
95 target_rate_kbps_(0) {}
96
97 enum class State {
98 kNormal,
99 kDropped,
100 kReencoded,
101 kQualityBoost,
102 } state;
103
104 int enhanced_max_qp;
105 int last_qp;
106 uint32_t debt_bytes_;
107 uint32_t target_rate_kbps_;
108
109 void UpdateDebt(int64_t delta_ms);
110 } layers_[kMaxNumTemporalLayers];
sprangb0fdfea2016-03-01 05:51:16 -0800111
112 void UpdateHistograms();
113 // Data for histogram statistics.
114 struct Stats {
115 int64_t first_frame_time_ms_ = -1;
116 int64_t num_tl0_frames_ = 0;
117 int64_t num_tl1_frames_ = 0;
118 int64_t num_dropped_frames_ = 0;
119 int64_t num_overshoots_ = 0;
120 int64_t tl0_qp_sum_ = 0;
121 int64_t tl1_qp_sum_ = 0;
122 int64_t tl0_target_bitrate_sum_ = 0;
123 int64_t tl1_target_bitrate_sum_ = 0;
124 } stats_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000125};
126} // namespace webrtc
127
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200128#endif // MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_