ec-hw Vk: VariableBitrate and Qvbr map to VBR (peak 1.5x, or peak = bitrate with a QP floor) instead of silently CBR; VA sends EncoderConfig::quality as the quality level
Browse files at this commit299aff6793c2ca5b1b0cd9f192393245e4ee0af6Parent: 600391d6 changed files +132 -64
- crates/ec-hw/src/enc/mod.rs +8 -2
- crates/ec-hw/src/hw.rs +13 -1
- shims/vk-video-ffi/src/av1_encode.rs +18 -19
- shims/vk-video-ffi/src/h264_encode.rs +66 -21
- shims/vk-video-ffi/src/h265_encode.rs +17 -21
- shims/vk-video-ffi/src/session.rs +10 -0
crates/ec-hw/src/enc/mod.rsmodified+8 -2
| @@ -42,8 +42,9 @@ use std::rc::Rc; | ||
| 42 | 42 | use ec_va::caps::{Entrypoint, Profile}; |
| 43 | 43 | use ec_va::va::SurfaceMemoryDescriptor; |
| 44 | 44 | use ec_va::va::{ |
| 45 | - BufferType, EncMiscParameter, EncMiscParameterFrameRate, EncMiscParameterHRD, | |
| 46 | - EncMiscParameterRateControl, EncPackedHeaderParameter, EncPackedHeaderType, RcFlags, | |
| 45 | + BufferType, EncMiscParameter, EncMiscParameterBufferQualityLevel, EncMiscParameterFrameRate, | |
| 46 | + EncMiscParameterHRD, EncMiscParameterRateControl, EncPackedHeaderParameter, EncPackedHeaderType, | |
| 47 | + RcFlags, | |
| 47 | 48 | VA_ENC_PACKED_HEADER_PICTURE, VA_ENC_PACKED_HEADER_SEQUENCE, VA_ENC_PACKED_HEADER_SLICE, |
| 48 | 49 | VA_FOURCC_NV12, VA_FOURCC_P010, VA_RT_FORMAT_YUV420, VA_RT_FORMAT_YUV420_10, |
| 49 | 50 | VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER, VAConfigAttribType, VAImage, |
| @@ -718,6 +719,11 @@ impl Encoder { | ||
| 718 | 719 | out.push(BufferType::EncMiscParameter(EncMiscParameter::FrameRate( |
| 719 | 720 | EncMiscParameterFrameRate::new(packed, 0), |
| 720 | 721 | ))); |
| 722 | + if self.config.quality > 0 { | |
| 723 | + out.push(BufferType::EncMiscParameter(EncMiscParameter::QualityLevel( | |
| 724 | + EncMiscParameterBufferQualityLevel::new(self.config.quality), | |
| 725 | + ))); | |
| 726 | + } | |
| 721 | 727 | if bits > 0 && !self.rc.take_steered() { |
| 722 | 728 | // The buffer starts full. Starting at three quarters full cost a |
| 723 | 729 | // quarter of a window of budget: radeonsi then spent 12.5% under the |
crates/ec-hw/src/hw.rsmodified+13 -1
| @@ -931,10 +931,22 @@ fn vk_rate(config: &EncoderConfig, bitrate: u32) -> ec_vk::RateControl { | ||
| 931 | 931 | (RateControlMode::ConstantQp { qp }, _) => ec_vk::RateControl::ConstantQp { |
| 932 | 932 | qp: qp.min(51) as u8, |
| 933 | 933 | }, |
| 934 | - _ => ec_vk::RateControl::Cbr { | |
| 934 | + (RateControlMode::ConstantBitrate, _) => ec_vk::RateControl::Cbr { | |
| 935 | 935 | bitrate, |
| 936 | 936 | buffer_ms: hrd_buffer_ms(config), |
| 937 | 937 | }, |
| 938 | + (RateControlMode::VariableBitrate, _) => ec_vk::RateControl::Vbr { | |
| 939 | + bitrate, | |
| 940 | + max_bitrate: bitrate.saturating_mul(3) / 2, | |
| 941 | + buffer_ms: hrd_buffer_ms(config), | |
| 942 | + min_qp: None, | |
| 943 | + }, | |
| 944 | + (RateControlMode::Qvbr { quality }, _) => ec_vk::RateControl::Vbr { | |
| 945 | + bitrate, | |
| 946 | + max_bitrate: bitrate, | |
| 947 | + buffer_ms: hrd_buffer_ms(config), | |
| 948 | + min_qp: Some(quality.min(51) as u8), | |
| 949 | + }, | |
| 938 | 950 | } |
| 939 | 951 | } |
| 940 | 952 |
shims/vk-video-ffi/src/av1_encode.rsmodified+18 -19
| @@ -106,17 +106,9 @@ struct RateChain { | ||
| 106 | 106 | |
| 107 | 107 | impl RateChain { |
| 108 | 108 | fn new(rate: RateControl, fps: (u32, u32)) -> Box<Self> { |
| 109 | - let (mode, layers, bitrate, buffer_ms) = match rate { | |
| 110 | - RateControl::ConstantQp { .. } => { | |
| 111 | - (vk::VideoEncodeRateControlModeFlagsKHR::DISABLED, 0, 0, 0) | |
| 112 | - } | |
| 113 | - RateControl::Cbr { bitrate, buffer_ms } => ( | |
| 114 | - vk::VideoEncodeRateControlModeFlagsKHR::CBR, | |
| 115 | - 1, | |
| 116 | - bitrate, | |
| 117 | - buffer_ms, | |
| 118 | - ), | |
| 119 | - }; | |
| 109 | + let mode = rate.mode(); | |
| 110 | + let layers = u32::from(rate.layer().is_some()); | |
| 111 | + let (bitrate, max_bitrate, buffer_ms) = rate.layer().unwrap_or_default(); | |
| 120 | 112 | let mut c = Box::new(RateChain { |
| 121 | 113 | rc: vk::VideoEncodeRateControlInfoKHR::builder() |
| 122 | 114 | .rate_control_mode(mode) |
| @@ -132,11 +124,21 @@ impl RateChain { | ||
| 132 | 124 | .build(), |
| 133 | 125 | layer: vk::VideoEncodeRateControlLayerInfoKHR::builder() |
| 134 | 126 | .average_bitrate(bitrate as u64) |
| 135 | - .max_bitrate(bitrate as u64) | |
| 127 | + .max_bitrate(max_bitrate as u64) | |
| 136 | 128 | .frame_rate_numerator(fps.0) |
| 137 | 129 | .frame_rate_denominator(fps.1) |
| 138 | 130 | .build(), |
| 139 | - layer_av1: vk::VideoEncodeAV1RateControlLayerInfoKHR::builder().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(), | |
| 141 | + }, | |
| 140 | 142 | }); |
| 141 | 143 | c.rc.next = &c.rc_av1 as *const _ as *const c_void; |
| 142 | 144 | c.rc.layer_count = layers; |
| @@ -183,10 +185,7 @@ impl Av1EncodeSession { | ||
| 183 | 185 | return Err(Error::Unsupported("AV1 single LAST reference".into())); |
| 184 | 186 | } |
| 185 | 187 | let level = av1_level(cfg.level, caps.max_level)?; |
| 186 | - let mode = match cfg.rate { | |
| 187 | - RateControl::ConstantQp { .. } => vk::VideoEncodeRateControlModeFlagsKHR::DISABLED, | |
| 188 | - RateControl::Cbr { .. } => vk::VideoEncodeRateControlModeFlagsKHR::CBR, | |
| 189 | - }; | |
| 188 | + let mode = cfg.rate.mode(); | |
| 190 | 189 | if !caps.rc_modes.contains(mode) { |
| 191 | 190 | return Err(Error::Unsupported(format!("rate control {mode:?}"))); |
| 192 | 191 | } |
| @@ -686,14 +685,14 @@ fn validate(cfg: &Av1EncodeConfig) -> Result<(), Error> { | ||
| 686 | 685 | fn q_index(rate: RateControl) -> u32 { |
| 687 | 686 | match rate { |
| 688 | 687 | RateControl::ConstantQp { qp } => qp as u32 * 5, |
| 689 | - RateControl::Cbr { .. } => CBR_Q_INDEX, | |
| 688 | + RateControl::Cbr { .. } | RateControl::Vbr { .. } => CBR_Q_INDEX, | |
| 690 | 689 | } |
| 691 | 690 | } |
| 692 | 691 | |
| 693 | 692 | fn constant_q_index(rate: RateControl) -> u32 { |
| 694 | 693 | match rate { |
| 695 | 694 | RateControl::ConstantQp { qp } => qp as u32 * 5, |
| 696 | - RateControl::Cbr { .. } => 0, | |
| 695 | + RateControl::Cbr { .. } | RateControl::Vbr { .. } => 0, | |
| 697 | 696 | } |
| 698 | 697 | } |
| 699 | 698 |
shims/vk-video-ffi/src/h264_encode.rsmodified+66 -21
| @@ -29,6 +29,55 @@ pub enum RateControl { | ||
| 29 | 29 | /// HRD virtual buffer in milliseconds of `bitrate`, also the initial fullness. |
| 30 | 30 | buffer_ms: u32, |
| 31 | 31 | }, |
| 32 | + /// Variable bitrate: `bitrate` on average, `max_bitrate` at peak, and no | |
| 33 | + /// quantizer below `min_qp` when set, so easy content spends less. | |
| 34 | + Vbr { | |
| 35 | + /// Average bitrate in bit/s. | |
| 36 | + bitrate: u32, | |
| 37 | + /// Peak bitrate in bit/s, at least `bitrate`. | |
| 38 | + max_bitrate: u32, | |
| 39 | + /// HRD virtual buffer in milliseconds, also the initial fullness. | |
| 40 | + buffer_ms: u32, | |
| 41 | + /// Quantizer floor, 0 to 51. | |
| 42 | + min_qp: Option<u8>, | |
| 43 | + }, | |
| 44 | +} | |
| 45 | + | |
| 46 | +impl RateControl { | |
| 47 | + pub(crate) fn mode(self) -> vk::VideoEncodeRateControlModeFlagsKHR { | |
| 48 | + match self { | |
| 49 | + RateControl::ConstantQp { .. } => vk::VideoEncodeRateControlModeFlagsKHR::DISABLED, | |
| 50 | + RateControl::Cbr { .. } => vk::VideoEncodeRateControlModeFlagsKHR::CBR, | |
| 51 | + RateControl::Vbr { .. } => vk::VideoEncodeRateControlModeFlagsKHR::VBR, | |
| 52 | + } | |
| 53 | + } | |
| 54 | + | |
| 55 | + pub(crate) fn layer(self) -> Option<(u32, u32, u32)> { | |
| 56 | + match self { | |
| 57 | + RateControl::ConstantQp { .. } => None, | |
| 58 | + RateControl::Cbr { bitrate, buffer_ms } => Some((bitrate, bitrate, buffer_ms)), | |
| 59 | + RateControl::Vbr { | |
| 60 | + bitrate, | |
| 61 | + max_bitrate, | |
| 62 | + buffer_ms, | |
| 63 | + .. | |
| 64 | + } => Some((bitrate, max_bitrate, buffer_ms)), | |
| 65 | + } | |
| 66 | + } | |
| 67 | + | |
| 68 | + pub(crate) fn constant_qp(self) -> Option<u8> { | |
| 69 | + match self { | |
| 70 | + RateControl::ConstantQp { qp } => Some(qp), | |
| 71 | + _ => None, | |
| 72 | + } | |
| 73 | + } | |
| 74 | + | |
| 75 | + pub(crate) fn min_qp(self) -> Option<u8> { | |
| 76 | + match self { | |
| 77 | + RateControl::Vbr { min_qp, .. } => min_qp, | |
| 78 | + _ => None, | |
| 79 | + } | |
| 80 | + } | |
| 32 | 81 | } |
| 33 | 82 | |
| 34 | 83 | /// H.264 profile written into the SPS and PPS. |
| @@ -142,17 +191,9 @@ struct RateChain { | ||
| 142 | 191 | |
| 143 | 192 | impl RateChain { |
| 144 | 193 | fn new(rate: RateControl, fps: (u32, u32)) -> Box<Self> { |
| 145 | - let (mode, layers, bitrate, buffer_ms) = match rate { | |
| 146 | - RateControl::ConstantQp { .. } => { | |
| 147 | - (vk::VideoEncodeRateControlModeFlagsKHR::DISABLED, 0, 0, 0) | |
| 148 | - } | |
| 149 | - RateControl::Cbr { bitrate, buffer_ms } => ( | |
| 150 | - vk::VideoEncodeRateControlModeFlagsKHR::CBR, | |
| 151 | - 1, | |
| 152 | - bitrate, | |
| 153 | - buffer_ms, | |
| 154 | - ), | |
| 155 | - }; | |
| 194 | + let mode = rate.mode(); | |
| 195 | + let layers = u32::from(rate.layer().is_some()); | |
| 196 | + let (bitrate, max_bitrate, buffer_ms) = rate.layer().unwrap_or_default(); | |
| 156 | 197 | let mut c = Box::new(RateChain { |
| 157 | 198 | rc: vk::VideoEncodeRateControlInfoKHR::builder() |
| 158 | 199 | .rate_control_mode(mode) |
| @@ -171,11 +212,21 @@ impl RateChain { | ||
| 171 | 212 | .build(), |
| 172 | 213 | layer: vk::VideoEncodeRateControlLayerInfoKHR::builder() |
| 173 | 214 | .average_bitrate(bitrate as u64) |
| 174 | - .max_bitrate(bitrate as u64) | |
| 215 | + .max_bitrate(max_bitrate as u64) | |
| 175 | 216 | .frame_rate_numerator(fps.0) |
| 176 | 217 | .frame_rate_denominator(fps.1) |
| 177 | 218 | .build(), |
| 178 | - layer_h264: vk::VideoEncodeH264RateControlLayerInfoKHR::builder().build(), | |
| 219 | + layer_h264: match rate.min_qp() { | |
| 220 | + Some(q) => vk::VideoEncodeH264RateControlLayerInfoKHR::builder() | |
| 221 | + .use_min_qp(true) | |
| 222 | + .min_qp(vk::VideoEncodeH264QpKHR { | |
| 223 | + qp_i: q as i32, | |
| 224 | + qp_p: q as i32, | |
| 225 | + qp_b: q as i32, | |
| 226 | + }) | |
| 227 | + .build(), | |
| 228 | + None => vk::VideoEncodeH264RateControlLayerInfoKHR::builder().build(), | |
| 229 | + }, | |
| 179 | 230 | }); |
| 180 | 231 | c.rc.next = &c.rc_h264 as *const _ as *const c_void; |
| 181 | 232 | c.rc.layer_count = layers; |
| @@ -218,10 +269,7 @@ impl H264EncodeSession { | ||
| 218 | 269 | if caps.max_refs < 1 { |
| 219 | 270 | return Err(Error::Unsupported("H.264 encode reference".into())); |
| 220 | 271 | } |
| 221 | - let mode = match cfg.rate { | |
| 222 | - RateControl::ConstantQp { .. } => vk::VideoEncodeRateControlModeFlagsKHR::DISABLED, | |
| 223 | - RateControl::Cbr { .. } => vk::VideoEncodeRateControlModeFlagsKHR::CBR, | |
| 224 | - }; | |
| 272 | + let mode = cfg.rate.mode(); | |
| 225 | 273 | if !caps.rc_modes.contains(mode) { |
| 226 | 274 | return Err(Error::Unsupported(format!("rate control {mode:?}"))); |
| 227 | 275 | } |
| @@ -409,10 +457,7 @@ impl H264EncodeSession { | ||
| 409 | 457 | } else { |
| 410 | 458 | vk::video::STD_VIDEO_H264_SLICE_TYPE_P |
| 411 | 459 | }; |
| 412 | - let constant_qp = match self.enc.rate { | |
| 413 | - RateControl::ConstantQp { qp } => qp as i32, | |
| 414 | - RateControl::Cbr { .. } => 0, | |
| 415 | - }; | |
| 460 | + let constant_qp = self.enc.rate.constant_qp().map_or(0, i32::from); | |
| 416 | 461 | let slices = [vk::VideoEncodeH264NaluSliceInfoKHR::builder() |
| 417 | 462 | .constant_qp(constant_qp) |
| 418 | 463 | .std_slice_header(&slice_std) |
shims/vk-video-ffi/src/h265_encode.rsmodified+17 -21
| @@ -120,17 +120,9 @@ struct RateChain { | ||
| 120 | 120 | |
| 121 | 121 | impl RateChain { |
| 122 | 122 | fn new(rate: RateControl, fps: (u32, u32)) -> Box<Self> { |
| 123 | - let (mode, layers, bitrate, buffer_ms) = match rate { | |
| 124 | - RateControl::ConstantQp { .. } => { | |
| 125 | - (vk::VideoEncodeRateControlModeFlagsKHR::DISABLED, 0, 0, 0) | |
| 126 | - } | |
| 127 | - RateControl::Cbr { bitrate, buffer_ms } => ( | |
| 128 | - vk::VideoEncodeRateControlModeFlagsKHR::CBR, | |
| 129 | - 1, | |
| 130 | - bitrate, | |
| 131 | - buffer_ms, | |
| 132 | - ), | |
| 133 | - }; | |
| 123 | + let mode = rate.mode(); | |
| 124 | + let layers = u32::from(rate.layer().is_some()); | |
| 125 | + let (bitrate, max_bitrate, buffer_ms) = rate.layer().unwrap_or_default(); | |
| 134 | 126 | let mut c = Box::new(RateChain { |
| 135 | 127 | rc: vk::VideoEncodeRateControlInfoKHR::builder() |
| 136 | 128 | .rate_control_mode(mode) |
| @@ -149,11 +141,21 @@ impl RateChain { | ||
| 149 | 141 | .build(), |
| 150 | 142 | layer: vk::VideoEncodeRateControlLayerInfoKHR::builder() |
| 151 | 143 | .average_bitrate(bitrate as u64) |
| 152 | - .max_bitrate(bitrate as u64) | |
| 144 | + .max_bitrate(max_bitrate as u64) | |
| 153 | 145 | .frame_rate_numerator(fps.0) |
| 154 | 146 | .frame_rate_denominator(fps.1) |
| 155 | 147 | .build(), |
| 156 | - layer_h265: vk::VideoEncodeH265RateControlLayerInfoKHR::builder().build(), | |
| 148 | + layer_h265: match rate.min_qp() { | |
| 149 | + Some(q) => vk::VideoEncodeH265RateControlLayerInfoKHR::builder() | |
| 150 | + .use_min_qp(true) | |
| 151 | + .min_qp(vk::VideoEncodeH265QpKHR { | |
| 152 | + qp_i: q as i32, | |
| 153 | + qp_p: q as i32, | |
| 154 | + qp_b: q as i32, | |
| 155 | + }) | |
| 156 | + .build(), | |
| 157 | + None => vk::VideoEncodeH265RateControlLayerInfoKHR::builder().build(), | |
| 158 | + }, | |
| 157 | 159 | }); |
| 158 | 160 | c.rc.next = &c.rc_h265 as *const _ as *const c_void; |
| 159 | 161 | c.rc.layer_count = layers; |
| @@ -202,10 +204,7 @@ impl H265EncodeSession { | ||
| 202 | 204 | if caps.max_refs < 1 { |
| 203 | 205 | return Err(Error::Unsupported("H.265 encode reference".into())); |
| 204 | 206 | } |
| 205 | - let mode = match cfg.rate { | |
| 206 | - RateControl::ConstantQp { .. } => vk::VideoEncodeRateControlModeFlagsKHR::DISABLED, | |
| 207 | - RateControl::Cbr { .. } => vk::VideoEncodeRateControlModeFlagsKHR::CBR, | |
| 208 | - }; | |
| 207 | + let mode = cfg.rate.mode(); | |
| 209 | 208 | if !caps.rc_modes.contains(mode) { |
| 210 | 209 | return Err(Error::Unsupported(format!("rate control {mode:?}"))); |
| 211 | 210 | } |
| @@ -425,10 +424,7 @@ impl H265EncodeSession { | ||
| 425 | 424 | vk::video::STD_VIDEO_H265_SLICE_TYPE_P |
| 426 | 425 | }; |
| 427 | 426 | slice_std.MaxNumMergeCand = 5; |
| 428 | - let constant_qp = match self.enc.rate { | |
| 429 | - RateControl::ConstantQp { qp } => qp as i32, | |
| 430 | - RateControl::Cbr { .. } => 0, | |
| 431 | - }; | |
| 427 | + let constant_qp = self.enc.rate.constant_qp().map_or(0, i32::from); | |
| 432 | 428 | let slices = [vk::VideoEncodeH265NaluSliceSegmentInfoKHR::builder() |
| 433 | 429 | .constant_qp(constant_qp) |
| 434 | 430 | .std_slice_segment_header(&slice_std) |
shims/vk-video-ffi/src/session.rsmodified+10 -0
| @@ -69,6 +69,16 @@ pub(crate) fn validate_rate(fps: (u32, u32), rate: RateControl) -> Result<(), Er | ||
| 69 | 69 | RateControl::ConstantQp { qp } if qp > 51 => Err(Error::Invalid("qp above 51".into())), |
| 70 | 70 | RateControl::Cbr { bitrate: 0, .. } => Err(Error::Invalid("bitrate is zero".into())), |
| 71 | 71 | RateControl::Cbr { buffer_ms: 0, .. } => Err(Error::Invalid("buffer is zero".into())), |
| 72 | + RateControl::Vbr { bitrate: 0, .. } => Err(Error::Invalid("bitrate is zero".into())), | |
| 73 | + RateControl::Vbr { buffer_ms: 0, .. } => Err(Error::Invalid("buffer is zero".into())), | |
| 74 | + RateControl::Vbr { | |
| 75 | + bitrate, | |
| 76 | + max_bitrate, | |
| 77 | + .. | |
| 78 | + } if max_bitrate < bitrate => Err(Error::Invalid("peak bitrate below average".into())), | |
| 79 | + RateControl::Vbr { | |
| 80 | + min_qp: Some(q), .. | |
| 81 | + } if q > 51 => Err(Error::Invalid("qp above 51".into())), | |
| 72 | 82 | _ => Ok(()), |
| 73 | 83 | } |
| 74 | 84 | } |