forge
Explore
Log in

Don't you dare go Hollow

ec-hw Vk VBR: closed-loop steering only pulls the bitrate down (steering under-spend back up pushed the peak to 2.25x); each picture is capped at buffer_ms of max_bitrate (maxFrameSize), so a 50-100 ms buffer keeps every 1 s window near max_bitrate (Sintel 1440p60 8.9 Mb: worst second 36.9 -> 24.0 Mb at the default buffer, 22.1 -> 12.8 Mb at 66 ms). CBR gets no cap: radv pads to it

Browse files at this commit

511a95b523ae4d94051ec8bd1e6043753d5d82f2Ahmet Kaan GümüşParent: d51b2ae
crates/ec-hw/src/rate.rsmodified+7 -2
@@ -34,7 +34,9 @@ pub(crate) fn hrd_buffer_ms(config: &EncoderConfig) -> u32 {
3434 /// Closed-loop correction of the driver's rate control, which lands off the ask
3535 /// on detailed content (testsrc2: +12..19% on mesa 26.2.6, ffmpeg's VA-API
3636 /// encoders too): once a second the bitrate sent is rescaled by the measured
37-/// bias, plus whatever was over- or under-spent so far.
37+/// bias, plus whatever was over- or under-spent so far. Variable bitrate is only
38+/// ever steered down: spending less on easy content is what it is for, and
39+/// steering that credit back up raised its peak past `max_bitrate`.
3840 #[derive(Debug, Clone, Copy)]
3941 pub(crate) struct RateFeedback {
4042 scale: f64,
@@ -91,7 +93,10 @@ impl RateFeedback {
9193 let asked = self.scale * per_frame * f64::from(self.window_frames);
9294 let bias = self.window / asked;
9395 let debt = f64::from(self.frames) * per_frame - self.spent;
94- let scale = ((rate + debt) / rate / bias).clamp(0.5, 1.5);
96+ let scale = match mode {
97+ RateControlMode::VariableBitrate => ((rate + debt.min(0.0)) / rate / bias.max(1.0)).clamp(0.5, 1.0),
98+ _ => ((rate + debt) / rate / bias).clamp(0.5, 1.5),
99+ };
95100 self.window = 0.0;
96101 self.window_frames = 0;
97102 if (scale / self.scale - 1.0).abs() > 0.02 {
crates/ec-hw/src/types.rsmodified+4 -1
@@ -115,7 +115,10 @@ pub struct EncoderConfig {
115115 /// pixels through [`Encoder::encode_16`] or [`HwEncoder::encode_16`].
116116 pub bit_depth: u8,
117117 /// Rate-control (HRD/VBV) buffer in milliseconds of the bitrate, or `None`
118- /// for 20 ms below a GOP of ten pictures and 1000 ms otherwise.
118+ /// for 20 ms below a GOP of ten pictures and 1000 ms otherwise. Any second
119+ /// may spend up to the peak bitrate plus this buffer; on Vulkan variable
120+ /// bitrate it also caps each picture at `buffer_ms` of the peak. 50 to 100 ms
121+ /// keeps every second near `max_bitrate` for low-latency streaming.
119122 pub buffer_ms: Option<u32>,
120123 }
121124
shims/vk-video-ffi/src/av1_encode.rsmodified+9 -10
@@ -128,16 +128,15 @@ impl RateChain {
128128 .frame_rate_numerator(fps.0)
129129 .frame_rate_denominator(fps.1)
130130 .build(),
131- layer_av1: match rate.min_qp() {
132- Some(q) => vk::VideoEncodeAV1RateControlLayerInfoKHR::builder()
133- .use_min_q_index(true)
134- .min_q_index(vk::VideoEncodeAV1QIndexKHR {
135- intra_q_index: q as u32 * 5,
136- predictive_q_index: q as u32 * 5,
137- bipredictive_q_index: q as u32 * 5,
138- })
139- .build(),
140- None => vk::VideoEncodeAV1RateControlLayerInfoKHR::builder().build(),
131+ layer_av1: {
132+ let mut layer = vk::VideoEncodeAV1RateControlLayerInfoKHR::builder();
133+ if let Some(q) = rate.min_qp() {
134+ 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 });
135+ }
136+ if let Some(m) = rate.max_frame_bytes() {
137+ layer = layer.use_max_frame_size(true).max_frame_size(vk::VideoEncodeAV1FrameSizeKHR { intra_frame_size: m, predictive_frame_size: m, bipredictive_frame_size: m });
138+ }
139+ layer.build()
141140 },
142141 });
143142 c.rc.next = &c.rc_av1 as *const _ as *const c_void;
shims/vk-video-ffi/src/h264_encode.rsmodified+21 -10
@@ -72,6 +72,18 @@ impl RateControl {
7272 }
7373 }
7474
75+ /// 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.
76+ pub(crate) fn max_frame_bytes(self) -> Option<u32> {
77+ match self {
78+ RateControl::Vbr {
79+ max_bitrate,
80+ buffer_ms,
81+ ..
82+ } => Some((u64::from(max_bitrate) * u64::from(buffer_ms) / 8000).clamp(1, u64::from(u32::MAX)) as u32),
83+ _ => None,
84+ }
85+ }
86+
7587 pub(crate) fn min_qp(self) -> Option<u8> {
7688 match self {
7789 RateControl::Vbr { min_qp, .. } => min_qp,
@@ -220,16 +232,15 @@ impl RateChain {
220232 .frame_rate_numerator(fps.0)
221233 .frame_rate_denominator(fps.1)
222234 .build(),
223- layer_h264: match rate.min_qp() {
224- Some(q) => vk::VideoEncodeH264RateControlLayerInfoKHR::builder()
225- .use_min_qp(true)
226- .min_qp(vk::VideoEncodeH264QpKHR {
227- qp_i: q as i32,
228- qp_p: q as i32,
229- qp_b: q as i32,
230- })
231- .build(),
232- None => vk::VideoEncodeH264RateControlLayerInfoKHR::builder().build(),
235+ layer_h264: {
236+ let mut layer = vk::VideoEncodeH264RateControlLayerInfoKHR::builder();
237+ if let Some(q) = rate.min_qp() {
238+ 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 });
239+ }
240+ if let Some(m) = rate.max_frame_bytes() {
241+ layer = layer.use_max_frame_size(true).max_frame_size(vk::VideoEncodeH264FrameSizeKHR { frame_i_size: m, frame_p_size: m, frame_b_size: m });
242+ }
243+ layer.build()
233244 },
234245 });
235246 c.rc.next = &c.rc_h264 as *const _ as *const c_void;
shims/vk-video-ffi/src/h265_encode.rsmodified+9 -10
@@ -147,16 +147,15 @@ impl RateChain {
147147 .frame_rate_numerator(fps.0)
148148 .frame_rate_denominator(fps.1)
149149 .build(),
150- layer_h265: match rate.min_qp() {
151- Some(q) => vk::VideoEncodeH265RateControlLayerInfoKHR::builder()
152- .use_min_qp(true)
153- .min_qp(vk::VideoEncodeH265QpKHR {
154- qp_i: q as i32,
155- qp_p: q as i32,
156- qp_b: q as i32,
157- })
158- .build(),
159- None => vk::VideoEncodeH265RateControlLayerInfoKHR::builder().build(),
150+ layer_h265: {
151+ let mut layer = vk::VideoEncodeH265RateControlLayerInfoKHR::builder();
152+ if let Some(q) = rate.min_qp() {
153+ 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 });
154+ }
155+ if let Some(m) = rate.max_frame_bytes() {
156+ layer = layer.use_max_frame_size(true).max_frame_size(vk::VideoEncodeH265FrameSizeKHR { frame_i_size: m, frame_p_size: m, frame_b_size: m });
157+ }
158+ layer.build()
160159 },
161160 });
162161 c.rc.next = &c.rc_h265 as *const _ as *const c_void;