Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 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 | |
Jeremy Leconte | 1a8d529 | 2023-10-10 13:21:29 | [diff] [blame] | 16 | #include "api/field_trials_view.h" |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 17 | #include "api/video/encoded_image.h" |
| 18 | #include "api/video/video_bitrate_allocation.h" |
| 19 | #include "api/video_codecs/video_encoder.h" |
| 20 | #include "video/encoder_overshoot_detector.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | class EncoderBitrateAdjuster { |
| 25 | public: |
| 26 | // Size of sliding window used to track overshoot rate. |
| 27 | static constexpr int64_t kWindowSizeMs = 3000; |
| 28 | // Minimum number of frames since last layout change required to trust the |
| 29 | // overshoot statistics. Otherwise falls back to default utilization. |
Henrik Boström | 3d5c6dd | 2023-02-22 07:37:47 | [diff] [blame] | 30 | // By layout change, we mean any simulcast/spatial/temporal layer being either |
| 31 | // enabled or disabled. |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 32 | static constexpr size_t kMinFramesSinceLayoutChange = 30; |
| 33 | // Default utilization, before reliable metrics are available, is set to 20% |
| 34 | // overshoot. This is conservative so that badly misbehaving encoders don't |
| 35 | // build too much queue at the very start. |
| 36 | static constexpr double kDefaultUtilizationFactor = 1.2; |
| 37 | |
Jeremy Leconte | 1a8d529 | 2023-10-10 13:21:29 | [diff] [blame] | 38 | EncoderBitrateAdjuster(const VideoCodec& codec_settings, |
| 39 | const FieldTrialsView& field_trials); |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 40 | ~EncoderBitrateAdjuster(); |
| 41 | |
| 42 | // Adjusts the given rate allocation to make it paceable within the target |
| 43 | // rates. |
| 44 | VideoBitrateAllocation AdjustRateAllocation( |
Erik Språng | 3d11e2f | 2019-04-15 12:48:30 | [diff] [blame] | 45 | const VideoEncoder::RateControlParameters& rates); |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 46 | |
| 47 | // Updated overuse detectors with data about the encoder, specifically about |
| 48 | // the temporal layer frame rate allocation. |
| 49 | void OnEncoderInfo(const VideoEncoder::EncoderInfo& encoder_info); |
| 50 | |
| 51 | // Updates the overuse detectors according to the encoded image size. |
Henrik Boström | 3d5c6dd | 2023-02-22 07:37:47 | [diff] [blame] | 52 | // `stream_index` is the spatial or simulcast index. |
| 53 | // TODO(https://crbug.com/webrtc/14891): If we want to support a mix of |
| 54 | // simulcast and SVC we'll also need to consider the case where we have both |
| 55 | // simulcast and spatial indices. |
| 56 | void OnEncodedFrame(DataSize size, int stream_index, int temporal_index); |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 57 | |
| 58 | void Reset(); |
| 59 | |
| 60 | private: |
Erik Språng | 3d11e2f | 2019-04-15 12:48:30 | [diff] [blame] | 61 | const bool utilize_bandwidth_headroom_; |
| 62 | |
| 63 | VideoEncoder::RateControlParameters current_rate_control_parameters_; |
Henrik Boström | 3d5c6dd | 2023-02-22 07:37:47 | [diff] [blame] | 64 | // FPS allocation of temporal layers, per simulcast/spatial layer. Represented |
| 65 | // as a Q8 fraction; 0 = 0%, 255 = 100%. See |
| 66 | // VideoEncoder::EncoderInfo.fps_allocation. |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 67 | absl::InlinedVector<uint8_t, kMaxTemporalStreams> |
| 68 | current_fps_allocation_[kMaxSpatialLayers]; |
| 69 | |
Henrik Boström | 3d5c6dd | 2023-02-22 07:37:47 | [diff] [blame] | 70 | // Frames since layout was changed, mean that any simulcast, spatial or |
| 71 | // temporal layer was either disabled or enabled. |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 72 | size_t frames_since_layout_change_; |
| 73 | std::unique_ptr<EncoderOvershootDetector> |
| 74 | overshoot_detectors_[kMaxSpatialLayers][kMaxTemporalStreams]; |
| 75 | |
| 76 | // Minimum bitrates allowed, per spatial layer. |
| 77 | uint32_t min_bitrates_bps_[kMaxSpatialLayers]; |
yingyingma | d341697 | 2023-04-20 06:28:01 | [diff] [blame] | 78 | |
Dan Tan | 6f34843 | 2023-09-14 09:59:00 | [diff] [blame] | 79 | // Size in pixels of each spatial layer. |
| 80 | uint32_t frame_size_pixels_[kMaxSpatialLayers]; |
| 81 | |
yingyingma | d341697 | 2023-04-20 06:28:01 | [diff] [blame] | 82 | // Codec type used for encoding. |
| 83 | VideoCodecType codec_; |
| 84 | |
| 85 | // Codec mode: { kRealtimeVideo, kScreensharing }. |
| 86 | VideoCodecMode codec_mode_; |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace webrtc |
| 90 | |
| 91 | #endif // VIDEO_ENCODER_BITRATE_ADJUSTER_H_ |