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