diff --git a/crates/ec-hw/src/enc/mod.rs b/crates/ec-hw/src/enc/mod.rs index 9622c15e..95223044 100644 --- a/crates/ec-hw/src/enc/mod.rs +++ b/crates/ec-hw/src/enc/mod.rs @@ -723,7 +723,7 @@ impl Encoder { // quarter of a window of budget: radeonsi then spent 12.5% under the // ask on a 2 s clip, and the gap is exactly that quarter over the // clip length (measured, 2026-10-16). Its size: see `hrd_buffer_ms`. - let buffer_ms = u64::from(hrd_buffer_ms(self.config.gop_size)); + let buffer_ms = u64::from(hrd_buffer_ms(&self.config)); let hrd_bits = (u64::from(bits) * buffer_ms / 1000) as u32; out.push(BufferType::EncMiscParameter(EncMiscParameter::HRD( EncMiscParameterHRD::new(hrd_bits, hrd_bits), diff --git a/crates/ec-hw/src/hw.rs b/crates/ec-hw/src/hw.rs index 1191cbef..0a242468 100644 --- a/crates/ec-hw/src/hw.rs +++ b/crates/ec-hw/src/hw.rs @@ -933,7 +933,7 @@ fn vk_rate(config: &EncoderConfig, bitrate: u32) -> ec_vk::RateControl { }, _ => ec_vk::RateControl::Cbr { bitrate, - buffer_ms: hrd_buffer_ms(config.gop_size), + buffer_ms: hrd_buffer_ms(config), }, } } diff --git a/crates/ec-hw/src/rate.rs b/crates/ec-hw/src/rate.rs index 35e24982..162fb20e 100644 --- a/crates/ec-hw/src/rate.rs +++ b/crates/ec-hw/src/rate.rs @@ -1,4 +1,4 @@ -use crate::types::{RateControlMode, Tunings}; +use crate::types::{EncoderConfig, RateControlMode, Tunings}; pub(crate) const RATE_WINDOW_MS: u32 = 1000; @@ -23,8 +23,12 @@ pub(crate) const RATE_WINDOW_MS: u32 = 1000; /// So a GOP shorter than ten pictures gets a 20 ms buffer. A GOP of thirty keeps /// the full rate window: the short buffer lands on the request there too, but /// costs 3.4 dB at the same rate. -pub(crate) fn hrd_buffer_ms(gop_size: u32) -> u32 { - if gop_size < 10 { 20 } else { RATE_WINDOW_MS } +pub(crate) fn hrd_buffer_ms(config: &EncoderConfig) -> u32 { + match config.buffer_ms { + Some(ms) => ms.max(1), + None if config.gop_size < 10 => 20, + None => RATE_WINDOW_MS, + } } /// Closed-loop correction of the driver's rate control, which lands off the ask diff --git a/crates/ec-hw/src/types.rs b/crates/ec-hw/src/types.rs index e417c1b9..e6843d85 100644 --- a/crates/ec-hw/src/types.rs +++ b/crates/ec-hw/src/types.rs @@ -114,6 +114,9 @@ pub struct EncoderConfig { /// Luma bit depth: 8, or 10 for H.265 and AV1. A 10-bit encoder takes its /// pixels through [`Encoder::encode_16`] or [`HwEncoder::encode_16`]. pub bit_depth: u8, + /// Rate-control (HRD/VBV) buffer in milliseconds of the bitrate, or `None` + /// for 20 ms below a GOP of ten pictures and 1000 ms otherwise. + pub buffer_ms: Option, } /// H.265 E.2.1 / H.264 E.1.1 colour description code points for the VUI. @@ -149,6 +152,7 @@ impl EncoderConfig { quality: 0, colour: None, bit_depth: 8, + buffer_ms: None, } }