| /* | |
| * Copyright (c) 2026 The WebRTC project authors. All Rights Reserved. | |
| * | |
| * Use of this source code is governed by a BSD-style license | |
| * that can be found in the LICENSE file in the root of the source | |
| * tree. An additional intellectual property rights grant can be found | |
| * in the file PATENTS. All contributing project authors may | |
| * be found in the AUTHORS file in the root of the source tree. | |
| */ | |
| #ifndef API_VIDEO_CODECS_BUFFER_LEVEL_RATE_CONTROLLER_CONFIG_H_ | |
| #define API_VIDEO_CODECS_BUFFER_LEVEL_RATE_CONTROLLER_CONFIG_H_ | |
| #include <optional> | |
| #include "api/units/time_delta.h" | |
| namespace webrtc { | |
| // NOTE: This interface is for prototyping purposes only and may change without | |
| // prior notice. | |
| // Configuration and optional behaviors of `BufferLevelRateController`, a | |
| // leaky-bucket one-pass real-time CBR rate controller. | |
| struct BufferLevelRateControllerConfig { | |
| // Rate control contract, see | |
| // `VideoEncoderFactoryInterface::StaticEncoderSettings::Cbr`. | |
| TimeDelta max_buffer_size = TimeDelta::Seconds(1); | |
| TimeDelta target_buffer_size = TimeDelta::Millis(600); | |
| double max_intra_bitrate_factor = 3.0; | |
| // Allowed QP range [1, 255]. | |
| int min_qp = 1; | |
| int max_qp = 255; | |
| // Below this percentage of the target buffer level every other frame is | |
| // dropped, below an empty buffer every frame. Zero disables pre-encode | |
| // dropping. | |
| int drop_frames_water_mark_pct = 0; | |
| // Maximum duration of consecutive pre-encode frame drops. `TimeDelta::Zero()` | |
| // or `TimeDelta::PlusInfinity()` disables the cap. | |
| TimeDelta max_consecutive_drop = TimeDelta::Millis(250); | |
| // Advise to drop a frame that is far larger than its budget and leaves the | |
| // buffer low, then code the next frame at the max QP. | |
| bool postencode_drop = false; | |
| // Advise to re-encode such frames instead, at a QP derived from the size | |
| // of the rejected encode. Takes precedence over `postencode_drop`. | |
| bool reencode_overshoot = false; | |
| // Code frames flagged as scene changes (when `frame_buffer` is supplied to | |
| // `PlanFrame`) at a high QP (the max QP for screen content). | |
| bool scene_change_high_qp = false; | |
| // Reset the buffer level to the target level after a post-encode drop or a | |
| // scene change frame. | |
| bool reset_buffer_on_overshoot = true; | |
| // Target of a re-encode of a delta frame, as a multiple of the average frame | |
| // budget, if the buffer has room for it. Zero keeps the normal target, which | |
| // tends to land the retry at the max QP. | |
| double reencode_target_factor = 0.0; | |
| // After an accepted re-encode, start the following frames from its QP, like | |
| // after a scene change, rather than from the average QP before it. | |
| bool seed_after_reencode = false; | |
| // For a second after a scene change, re-encode or post-encode drop, let the | |
| // QP come down twice as fast per frame. | |
| bool fast_recovery = false; | |
| // Code screen content scene changes halfway from the planned QP to the max | |
| // QP, rather than at the max QP. Meant to be combined with | |
| // `reencode_overshoot` to catch slides that are too big. | |
| bool soft_screen_scene_change = false; | |
| // Once screen content has been static for a few frames since a scene change | |
| // and the buffer is at least half full, let the QP drop by up to 60 per | |
| // frame, and bias it down after an undershoot. | |
| bool faster_convergence_static = false; | |
| // After a keyframe, weigh in the keyframe QP itself rather than only its | |
| // slowly moving average, as done for layered streams. Without it, the frames | |
| // after a keyframe start near the worst quality and ramp down slowly. | |
| bool keyframe_recovery = false; | |
| // Lower the QP of base layer delta frames by this many qindex steps while | |
| // the buffer is stable (layered streams use 4 for two layers and 10 for more; | |
| // single-layer streams use 0 by default). | |
| int base_layer_qp_push = 0; | |
| // After a post-encode drop of a base layer frame, advise to drop the upper | |
| // temporal layer frames until a base layer frame is coded again. They still | |
| // reference the base layer frame before the dropped one, so each of them | |
| // would code the whole change again. | |
| bool drop_upper_layers_after_base_drop = false; | |
| // Caps the level of each layer buffer at the optimal level plus this | |
| // percentage of the room up to the maximum. The send queue can't bank | |
| // undershoot, so credit above the optimal level that is spent later shows | |
| // up as queueing delay. Unset caps at the maximum buffer size. | |
| std::optional<int> max_buffer_credit_pct; | |
| // Source SAD (per 64x64 block) up to which a frame still counts as static | |
| // for `faster_convergence_static`, e.g. while typing. 0 means only frames | |
| // without any change. | |
| double max_static_source_sad_per_64x64 = 0.0; | |
| // Recommended settings from the rate control benchmark: re-encodes instead of | |
| // drops, high-QP scene changes, faster recovery, and no buffer credit. | |
| // Expects `frame_buffer` in `PlanFrame` and an encoder that can discard | |
| // rejected attempts. | |
| static constexpr BufferLevelRateControllerConfig Recommended() { | |
| return {.reencode_overshoot = true, | |
| .scene_change_high_qp = true, | |
| .reencode_target_factor = 4.0, | |
| .seed_after_reencode = true, | |
| .fast_recovery = true, | |
| .soft_screen_scene_change = true, | |
| .faster_convergence_static = true, | |
| .keyframe_recovery = true, | |
| .max_buffer_credit_pct = 0}; | |
| } | |
| }; | |
| } // namespace webrtc | |
| #endif // API_VIDEO_CODECS_BUFFER_LEVEL_RATE_CONTROLLER_CONFIG_H_ |