1 /*
2  *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3  *
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
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef VIDEO_ENCODER_BITRATE_ADJUSTER_H_
12 #define VIDEO_ENCODER_BITRATE_ADJUSTER_H_
13 
14 #include <memory>
15 
16 #include "api/video/encoded_image.h"
17 #include "api/video/video_bitrate_allocation.h"
18 #include "api/video_codecs/video_encoder.h"
19 #include "video/encoder_overshoot_detector.h"
20 
21 namespace webrtc {
22 
23 class EncoderBitrateAdjuster {
24  public:
25   // Size of sliding window used to track overshoot rate.
26   static constexpr int64_t kWindowSizeMs = 3000;
27   // Minimum number of frames since last layout change required to trust the
28   // overshoot statistics. Otherwise falls back to default utilization.
29   // By layout change, we mean any spatial/temporal layer being either enabled
30   // or disabled.
31   static constexpr size_t kMinFramesSinceLayoutChange = 30;
32   // Default utilization, before reliable metrics are available, is set to 20%
33   // overshoot. This is conservative so that badly misbehaving encoders don't
34   // build too much queue at the very start.
35   static constexpr double kDefaultUtilizationFactor = 1.2;
36 
37   explicit EncoderBitrateAdjuster(const VideoCodec& codec_settings);
38   ~EncoderBitrateAdjuster();
39 
40   // Adjusts the given rate allocation to make it paceable within the target
41   // rates.
42   VideoBitrateAllocation AdjustRateAllocation(
43       const VideoEncoder::RateControlParameters& rates);
44 
45   // Updated overuse detectors with data about the encoder, specifically about
46   // the temporal layer frame rate allocation.
47   void OnEncoderInfo(const VideoEncoder::EncoderInfo& encoder_info);
48 
49   // Updates the overuse detectors according to the encoded image size.
50   void OnEncodedFrame(const EncodedImage& encoded_image, int temporal_index);
51 
52   void Reset();
53 
54  private:
55   const bool utilize_bandwidth_headroom_;
56 
57   VideoEncoder::RateControlParameters current_rate_control_parameters_;
58   // FPS allocation of temporal layers, per spatial layer. Represented as a Q8
59   // fraction; 0 = 0%, 255 = 100%. See VideoEncoder::EncoderInfo.fps_allocation.
60   absl::InlinedVector<uint8_t, kMaxTemporalStreams>
61       current_fps_allocation_[kMaxSpatialLayers];
62 
63   // Frames since layout was changed, mean that any spatial or temporal layer
64   // was either disabled or enabled.
65   size_t frames_since_layout_change_;
66   std::unique_ptr<EncoderOvershootDetector>
67       overshoot_detectors_[kMaxSpatialLayers][kMaxTemporalStreams];
68 
69   // Minimum bitrates allowed, per spatial layer.
70   uint32_t min_bitrates_bps_[kMaxSpatialLayers];
71 };
72 
73 }  // namespace webrtc
74 
75 #endif  // VIDEO_ENCODER_BITRATE_ADJUSTER_H_
76