blob: 0ddd20b9b367a82edf08a2b121526061202faf14 [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ång7ca375c2019-02-06 15:20:1717#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
22namespace webrtc {
23
24class 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öm3d5c6dd2023-02-22 07:37:4730 // By layout change, we mean any simulcast/spatial/temporal layer being either
31 // enabled or disabled.
Erik Språng7ca375c2019-02-06 15:20:1732 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 Leconte1a8d5292023-10-10 13:21:2938 EncoderBitrateAdjuster(const VideoCodec& codec_settings,
39 const FieldTrialsView& field_trials);
Erik Språng7ca375c2019-02-06 15:20:1740 ~EncoderBitrateAdjuster();
41
42 // Adjusts the given rate allocation to make it paceable within the target
43 // rates.
44 VideoBitrateAllocation AdjustRateAllocation(
Erik Språng3d11e2f2019-04-15 12:48:3045 const VideoEncoder::RateControlParameters& rates);
Erik Språng7ca375c2019-02-06 15:20:1746
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öm3d5c6dd2023-02-22 07:37:4752 // `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ång7ca375c2019-02-06 15:20:1757
58 void Reset();
59
60 private:
Erik Språng3d11e2f2019-04-15 12:48:3061 const bool utilize_bandwidth_headroom_;
62
63 VideoEncoder::RateControlParameters current_rate_control_parameters_;
Henrik Boström3d5c6dd2023-02-22 07:37:4764 // 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ång7ca375c2019-02-06 15:20:1767 absl::InlinedVector<uint8_t, kMaxTemporalStreams>
68 current_fps_allocation_[kMaxSpatialLayers];
69
Henrik Boström3d5c6dd2023-02-22 07:37:4770 // Frames since layout was changed, mean that any simulcast, spatial or
71 // temporal layer was either disabled or enabled.
Erik Språng7ca375c2019-02-06 15:20:1772 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];
yingyingmad3416972023-04-20 06:28:0178
Dan Tan6f348432023-09-14 09:59:0079 // Size in pixels of each spatial layer.
80 uint32_t frame_size_pixels_[kMaxSpatialLayers];
81
yingyingmad3416972023-04-20 06:28:0182 // Codec type used for encoding.
83 VideoCodecType codec_;
84
85 // Codec mode: { kRealtimeVideo, kScreensharing }.
86 VideoCodecMode codec_mode_;
Erik Språng7ca375c2019-02-06 15:20:1787};
88
89} // namespace webrtc
90
91#endif // VIDEO_ENCODER_BITRATE_ADJUSTER_H_