blob: 9248b0d5eb594fe071b0e131f0e6db69ca932e5f [file] [log] [blame]
Erik Språng7ca375c2019-02-06 15:20:171/*
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 Leconte1a8d5292023-10-10 13:21:2916#include "api/field_trials_view.h"
Erik Språngdb65fda2024-07-05 09:41:2917#include "api/units/time_delta.h"
Erik Språng7ca375c2019-02-06 15:20:1718#include "api/video/encoded_image.h"
19#include "api/video/video_bitrate_allocation.h"
20#include "api/video_codecs/video_encoder.h"
Erik Språngdb65fda2024-07-05 09:41:2921#include "system_wrappers/include/clock.h"
Erik Språng7ca375c2019-02-06 15:20:1722#include "video/encoder_overshoot_detector.h"
Erik Språngdb65fda2024-07-05 09:41:2923#include "video/rate_utilization_tracker.h"
Erik Språng7ca375c2019-02-06 15:20:1724
25namespace webrtc {
26
27class EncoderBitrateAdjuster {
28 public:
29 // Size of sliding window used to track overshoot rate.
Erik Språngdb65fda2024-07-05 09:41:2930 static constexpr TimeDelta kWindowSize = TimeDelta::Seconds(3);
Erik Språng7ca375c2019-02-06 15:20:1731 // Minimum number of frames since last layout change required to trust the
32 // overshoot statistics. Otherwise falls back to default utilization.
Henrik Boström3d5c6dd2023-02-22 07:37:4733 // By layout change, we mean any simulcast/spatial/temporal layer being either
34 // enabled or disabled.
Erik Språng7ca375c2019-02-06 15:20:1735 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 Leconte1a8d5292023-10-10 13:21:2941 EncoderBitrateAdjuster(const VideoCodec& codec_settings,
Erik Språngdb65fda2024-07-05 09:41:2942 const FieldTrialsView& field_trials,
43 Clock& clock);
Erik Språng7ca375c2019-02-06 15:20:1744 ~EncoderBitrateAdjuster();
45
46 // Adjusts the given rate allocation to make it paceable within the target
47 // rates.
48 VideoBitrateAllocation AdjustRateAllocation(
Erik Språng3d11e2f2019-04-15 12:48:3049 const VideoEncoder::RateControlParameters& rates);
Erik Språng7ca375c2019-02-06 15:20:1750
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öm3d5c6dd2023-02-22 07:37:4756 // `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ång7ca375c2019-02-06 15:20:1761
62 void Reset();
63
64 private:
Erik Språng3d11e2f2019-04-15 12:48:3065 const bool utilize_bandwidth_headroom_;
Erik Språngdb65fda2024-07-05 09:41:2966 const bool use_newfangled_headroom_adjustment_;
Erik Språng3d11e2f2019-04-15 12:48:3067
68 VideoEncoder::RateControlParameters current_rate_control_parameters_;
Henrik Boström3d5c6dd2023-02-22 07:37:4769 // 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ång7ca375c2019-02-06 15:20:1772 absl::InlinedVector<uint8_t, kMaxTemporalStreams>
73 current_fps_allocation_[kMaxSpatialLayers];
74
Henrik Boström3d5c6dd2023-02-22 07:37:4775 // Frames since layout was changed, mean that any simulcast, spatial or
76 // temporal layer was either disabled or enabled.
Erik Språng7ca375c2019-02-06 15:20:1777 size_t frames_since_layout_change_;
78 std::unique_ptr<EncoderOvershootDetector>
79 overshoot_detectors_[kMaxSpatialLayers][kMaxTemporalStreams];
80
Erik Språngdb65fda2024-07-05 09:41:2981 // Per spatial layer track of average media utilization.
82 std::unique_ptr<RateUtilizationTracker>
83 media_rate_trackers_[kMaxSpatialLayers];
84
Erik Språng7ca375c2019-02-06 15:20:1785 // Minimum bitrates allowed, per spatial layer.
86 uint32_t min_bitrates_bps_[kMaxSpatialLayers];
yingyingmad3416972023-04-20 06:28:0187
88 // Codec type used for encoding.
89 VideoCodecType codec_;
90
91 // Codec mode: { kRealtimeVideo, kScreensharing }.
92 VideoCodecMode codec_mode_;
Erik Språngdb65fda2024-07-05 09:41:2993
94 Clock& clock_;
Erik Språng7ca375c2019-02-06 15:20:1795};
96
97} // namespace webrtc
98
99#endif // VIDEO_ENCODER_BITRATE_ADJUSTER_H_