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 commit511a95b523ae4d94051ec8bd1e6043753d5d82f2Parent: d51b2ae5 changed files +50 -33
- crates/ec-hw/src/rate.rs +7 -2
- crates/ec-hw/src/types.rs +4 -1
- shims/vk-video-ffi/src/av1_encode.rs +9 -10
- shims/vk-video-ffi/src/h264_encode.rs +21 -10
- shims/vk-video-ffi/src/h265_encode.rs +9 -10
crates/ec-hw/src/rate.rsmodified+7 -2
| @@ -34,7 +34,9 @@ pub(crate) fn hrd_buffer_ms(config: &EncoderConfig) -> u32 { | ||
| 34 | 34 | /// Closed-loop correction of the driver's rate control, which lands off the ask |
| 35 | 35 | /// on detailed content (testsrc2: +12..19% on mesa 26.2.6, ffmpeg's VA-API |
| 36 | 36 | /// 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`. | |
| 38 | 40 | #[derive(Debug, Clone, Copy)] |
| 39 | 41 | pub(crate) struct RateFeedback { |
| 40 | 42 | scale: f64, |
| @@ -91,7 +93,10 @@ impl RateFeedback { | ||
| 91 | 93 | let asked = self.scale * per_frame * f64::from(self.window_frames); |
| 92 | 94 | let bias = self.window / asked; |
| 93 | 95 | 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 | + }; | |
| 95 | 100 | self.window = 0.0; |
| 96 | 101 | self.window_frames = 0; |
| 97 | 102 | if (scale / self.scale - 1.0).abs() > 0.02 { |
crates/ec-hw/src/types.rsmodified+4 -1
| @@ -115,7 +115,10 @@ pub struct EncoderConfig { | ||
| 115 | 115 | /// pixels through [`Encoder::encode_16`] or [`HwEncoder::encode_16`]. |
| 116 | 116 | pub bit_depth: u8, |
| 117 | 117 | /// 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. | |
| 119 | 122 | pub buffer_ms: Option<u32>, |
| 120 | 123 | } |
| 121 | 124 |
shims/vk-video-ffi/src/av1_encode.rsmodified+9 -10
| @@ -128,16 +128,15 @@ impl RateChain { | ||
| 128 | 128 | .frame_rate_numerator(fps.0) |
| 129 | 129 | .frame_rate_denominator(fps.1) |
| 130 | 130 | .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() | |
| 141 | 140 | }, |
| 142 | 141 | }); |
| 143 | 142 | 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 { | ||
| 72 | 72 | } |
| 73 | 73 | } |
| 74 | 74 | |
| 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 | + | |
| 75 | 87 | pub(crate) fn min_qp(self) -> Option<u8> { |
| 76 | 88 | match self { |
| 77 | 89 | RateControl::Vbr { min_qp, .. } => min_qp, |
| @@ -220,16 +232,15 @@ impl RateChain { | ||
| 220 | 232 | .frame_rate_numerator(fps.0) |
| 221 | 233 | .frame_rate_denominator(fps.1) |
| 222 | 234 | .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() | |
| 233 | 244 | }, |
| 234 | 245 | }); |
| 235 | 246 | 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 { | ||
| 147 | 147 | .frame_rate_numerator(fps.0) |
| 148 | 148 | .frame_rate_denominator(fps.1) |
| 149 | 149 | .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() | |
| 160 | 159 | }, |
| 161 | 160 | }); |
| 162 | 161 | c.rc.next = &c.rc_h265 as *const _ as *const c_void; |