diff --git a/crates/ec-hw/src/rate.rs b/crates/ec-hw/src/rate.rs index 162fb20e..0aa38599 100644 --- a/crates/ec-hw/src/rate.rs +++ b/crates/ec-hw/src/rate.rs @@ -34,7 +34,9 @@ pub(crate) fn hrd_buffer_ms(config: &EncoderConfig) -> u32 { /// Closed-loop correction of the driver's rate control, which lands off the ask /// on detailed content (testsrc2: +12..19% on mesa 26.2.6, ffmpeg's VA-API /// encoders too): once a second the bitrate sent is rescaled by the measured -/// bias, plus whatever was over- or under-spent so far. +/// bias, plus whatever was over- or under-spent so far. Variable bitrate is only +/// ever steered down: spending less on easy content is what it is for, and +/// steering that credit back up raised its peak past `max_bitrate`. #[derive(Debug, Clone, Copy)] pub(crate) struct RateFeedback { scale: f64, @@ -91,7 +93,10 @@ impl RateFeedback { let asked = self.scale * per_frame * f64::from(self.window_frames); let bias = self.window / asked; let debt = f64::from(self.frames) * per_frame - self.spent; - let scale = ((rate + debt) / rate / bias).clamp(0.5, 1.5); + let scale = match mode { + RateControlMode::VariableBitrate => ((rate + debt.min(0.0)) / rate / bias.max(1.0)).clamp(0.5, 1.0), + _ => ((rate + debt) / rate / bias).clamp(0.5, 1.5), + }; self.window = 0.0; self.window_frames = 0; if (scale / self.scale - 1.0).abs() > 0.02 { diff --git a/crates/ec-hw/src/types.rs b/crates/ec-hw/src/types.rs index 7a6feab1..643e66de 100644 --- a/crates/ec-hw/src/types.rs +++ b/crates/ec-hw/src/types.rs @@ -115,7 +115,10 @@ pub struct EncoderConfig { /// 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. + /// for 20 ms below a GOP of ten pictures and 1000 ms otherwise. Any second + /// may spend up to the peak bitrate plus this buffer; on Vulkan variable + /// bitrate it also caps each picture at `buffer_ms` of the peak. 50 to 100 ms + /// keeps every second near `max_bitrate` for low-latency streaming. pub buffer_ms: Option, } diff --git a/shims/vk-video-ffi/src/av1_encode.rs b/shims/vk-video-ffi/src/av1_encode.rs index 905a04a2..800ace0c 100644 --- a/shims/vk-video-ffi/src/av1_encode.rs +++ b/shims/vk-video-ffi/src/av1_encode.rs @@ -128,16 +128,15 @@ impl RateChain { .frame_rate_numerator(fps.0) .frame_rate_denominator(fps.1) .build(), - layer_av1: match rate.min_qp() { - Some(q) => vk::VideoEncodeAV1RateControlLayerInfoKHR::builder() - .use_min_q_index(true) - .min_q_index(vk::VideoEncodeAV1QIndexKHR { - intra_q_index: q as u32 * 5, - predictive_q_index: q as u32 * 5, - bipredictive_q_index: q as u32 * 5, - }) - .build(), - None => vk::VideoEncodeAV1RateControlLayerInfoKHR::builder().build(), + layer_av1: { + let mut layer = vk::VideoEncodeAV1RateControlLayerInfoKHR::builder(); + if let Some(q) = rate.min_qp() { + layer = layer.use_min_q_index(true).min_q_index(vk::VideoEncodeAV1QIndexKHR { intra_q_index: q as u32 * 5, predictive_q_index: q as u32 * 5, bipredictive_q_index: q as u32 * 5 }); + } + if let Some(m) = rate.max_frame_bytes() { + layer = layer.use_max_frame_size(true).max_frame_size(vk::VideoEncodeAV1FrameSizeKHR { intra_frame_size: m, predictive_frame_size: m, bipredictive_frame_size: m }); + } + layer.build() }, }); c.rc.next = &c.rc_av1 as *const _ as *const c_void; diff --git a/shims/vk-video-ffi/src/h264_encode.rs b/shims/vk-video-ffi/src/h264_encode.rs index 8a3f6769..d428df63 100644 --- a/shims/vk-video-ffi/src/h264_encode.rs +++ b/shims/vk-video-ffi/src/h264_encode.rs @@ -72,6 +72,18 @@ impl RateControl { } } + /// Variable bitrate caps each coded picture at what its HRD buffer holds: `buffer_ms` of the peak bitrate, in bytes. radv's VBR leaves the buffer to the cap; constant bitrate pads up to a cap instead of spending it, so it gets none. + pub(crate) fn max_frame_bytes(self) -> Option { + match self { + RateControl::Vbr { + max_bitrate, + buffer_ms, + .. + } => Some((u64::from(max_bitrate) * u64::from(buffer_ms) / 8000).clamp(1, u64::from(u32::MAX)) as u32), + _ => None, + } + } + pub(crate) fn min_qp(self) -> Option { match self { RateControl::Vbr { min_qp, .. } => min_qp, @@ -220,16 +232,15 @@ impl RateChain { .frame_rate_numerator(fps.0) .frame_rate_denominator(fps.1) .build(), - layer_h264: match rate.min_qp() { - Some(q) => vk::VideoEncodeH264RateControlLayerInfoKHR::builder() - .use_min_qp(true) - .min_qp(vk::VideoEncodeH264QpKHR { - qp_i: q as i32, - qp_p: q as i32, - qp_b: q as i32, - }) - .build(), - None => vk::VideoEncodeH264RateControlLayerInfoKHR::builder().build(), + layer_h264: { + let mut layer = vk::VideoEncodeH264RateControlLayerInfoKHR::builder(); + if let Some(q) = rate.min_qp() { + layer = layer.use_min_qp(true).min_qp(vk::VideoEncodeH264QpKHR { qp_i: q as i32, qp_p: q as i32, qp_b: q as i32 }); + } + if let Some(m) = rate.max_frame_bytes() { + layer = layer.use_max_frame_size(true).max_frame_size(vk::VideoEncodeH264FrameSizeKHR { frame_i_size: m, frame_p_size: m, frame_b_size: m }); + } + layer.build() }, }); c.rc.next = &c.rc_h264 as *const _ as *const c_void; diff --git a/shims/vk-video-ffi/src/h265_encode.rs b/shims/vk-video-ffi/src/h265_encode.rs index 988e8dd8..249d9eda 100644 --- a/shims/vk-video-ffi/src/h265_encode.rs +++ b/shims/vk-video-ffi/src/h265_encode.rs @@ -147,16 +147,15 @@ impl RateChain { .frame_rate_numerator(fps.0) .frame_rate_denominator(fps.1) .build(), - layer_h265: match rate.min_qp() { - Some(q) => vk::VideoEncodeH265RateControlLayerInfoKHR::builder() - .use_min_qp(true) - .min_qp(vk::VideoEncodeH265QpKHR { - qp_i: q as i32, - qp_p: q as i32, - qp_b: q as i32, - }) - .build(), - None => vk::VideoEncodeH265RateControlLayerInfoKHR::builder().build(), + layer_h265: { + let mut layer = vk::VideoEncodeH265RateControlLayerInfoKHR::builder(); + if let Some(q) = rate.min_qp() { + layer = layer.use_min_qp(true).min_qp(vk::VideoEncodeH265QpKHR { qp_i: q as i32, qp_p: q as i32, qp_b: q as i32 }); + } + if let Some(m) = rate.max_frame_bytes() { + layer = layer.use_max_frame_size(true).max_frame_size(vk::VideoEncodeH265FrameSizeKHR { frame_i_size: m, frame_p_size: m, frame_b_size: m }); + } + layer.build() }, }); c.rc.next = &c.rc_h265 as *const _ as *const c_void;