Vk encode: EncoderConfig::quality picks the Vulkan quality level (1 = driver best, each step faster; default level 1, RADV balance), session parameters created at that level; content hint DESKTOP; AV1 enables CDEF with four strength candidates per 64x64 block. Decode RGB upsamples chroma bilinearly, left-sited and row-centred as the RGB->NV12 shader writes it (GPU shader and ec-dsp i420_to_packed stay bit-identical)
Browse files at this commit9876fb67e5024ad21452acda9fd1adc97a7066abParent: 511a95b16 changed files +129 -13
- crates/ec-dsp/src/rgb.rs +22 -3
- crates/ec-hw/src/hw.rs +3 -0
- crates/ec-hw/src/types.rs +3 -1
- crates/ec-vk/examples/av1_encode.rs +1 -0
- crates/ec-vk/examples/h264_encode.rs +1 -0
- crates/ec-vk/examples/h265_encode.rs +1 -0
- crates/ec-vk/src/av1.rs +3 -0
- crates/ec-vk/src/h265.rs +3 -0
- crates/ec-vk/src/lib.rs +3 -0
- shims/vk-video-ffi/examples/dmabuf_smoke.rs +3 -0
- shims/vk-video-ffi/shaders/nv12_to_rgb.comp +22 -3
- shims/vk-video-ffi/shaders/nv12_to_rgb.spv +0 -0
- shims/vk-video-ffi/src/av1_encode.rs +28 -3
- shims/vk-video-ffi/src/h264_encode.rs +11 -1
- shims/vk-video-ffi/src/h265_encode.rs +11 -1
- shims/vk-video-ffi/src/session.rs +14 -1
crates/ec-dsp/src/rgb.rsmodified+22 -3
| @@ -125,10 +125,9 @@ pub fn i420_to_packed( | ||
| 125 | 125 | let mut sum = [0u32; 3]; |
| 126 | 126 | for sy in y0..y1 { |
| 127 | 127 | for sx in x0..x1 { |
| 128 | - let c = (sy / 2) * cw + sx / 2; | |
| 129 | 128 | let yy = i32::from(y[sy * w + sx]) - 16; |
| 130 | - let uu = i32::from(u[c]) - 128; | |
| 131 | - let vv = i32::from(v[c]) - 128; | |
| 129 | + let uu = chroma_at(u, (cw, h / 2), sx, sy) - 128; | |
| 130 | + let vv = chroma_at(v, (cw, h / 2), sx, sy) - 128; | |
| 132 | 131 | let r = ((k.ky * yy + k.rv * vv + 128) >> 8).clamp(0, 255); |
| 133 | 132 | let g = ((k.ky * yy + k.gu * uu + k.gv * vv + 128) >> 8).clamp(0, 255); |
| 134 | 133 | let b = ((k.ky * yy + k.bu * uu + 128) >> 8).clamp(0, 255); |
| @@ -147,6 +146,26 @@ pub fn i420_to_packed( | ||
| 147 | 146 | out |
| 148 | 147 | } |
| 149 | 148 | |
| 149 | +/// Bilinear 4:2:0 chroma at luma `(x, y)`: samples left-sited on even columns and centred between row pairs, as the RGB to I420 conversions write them. | |
| 150 | +fn chroma_at(plane: &[u8], (cw, ch): (usize, usize), x: usize, y: usize) -> i32 { | |
| 151 | + let j = x / 2; | |
| 152 | + let row = |r: usize| { | |
| 153 | + let a = i32::from(plane[r * cw + j]); | |
| 154 | + if x % 2 == 0 { | |
| 155 | + 2 * a | |
| 156 | + } else { | |
| 157 | + a + i32::from(plane[r * cw + (j + 1).min(cw - 1)]) | |
| 158 | + } | |
| 159 | + }; | |
| 160 | + let k = y / 2; | |
| 161 | + let s = if y % 2 == 0 { | |
| 162 | + row(k.saturating_sub(1)) + 3 * row(k) | |
| 163 | + } else { | |
| 164 | + 3 * row(k) + row((k + 1).min(ch - 1)) | |
| 165 | + }; | |
| 166 | + (s + 4) >> 3 | |
| 167 | +} | |
| 168 | + | |
| 150 | 169 | fn footprint(o: usize, n_in: usize, n_out: usize) -> (usize, usize) { |
| 151 | 170 | (o * n_in / n_out, ((o + 1) * n_in).div_ceil(n_out)) |
| 152 | 171 | } |
crates/ec-hw/src/hw.rsmodified+3 -0
| @@ -931,6 +931,7 @@ fn open_vk(config: EncoderConfig) -> Result<VkEncoder> { | ||
| 931 | 931 | profile: vk_h264_profile(config.h264_profile), |
| 932 | 932 | level, |
| 933 | 933 | colour, |
| 934 | + quality: config.quality, | |
| 934 | 935 | }, |
| 935 | 936 | )?), |
| 936 | 937 | EncCodec::H265 => VkEncoder::H265(ec_vk::H265Encoder::new( |
| @@ -944,6 +945,7 @@ fn open_vk(config: EncoderConfig) -> Result<VkEncoder> { | ||
| 944 | 945 | bit_depth: config.bit_depth, |
| 945 | 946 | level, |
| 946 | 947 | colour, |
| 948 | + quality: config.quality, | |
| 947 | 949 | }, |
| 948 | 950 | )?), |
| 949 | 951 | EncCodec::Av1 => VkEncoder::Av1(ec_vk::Av1Encoder::new( |
| @@ -957,6 +959,7 @@ fn open_vk(config: EncoderConfig) -> Result<VkEncoder> { | ||
| 957 | 959 | bit_depth: config.bit_depth, |
| 958 | 960 | level, |
| 959 | 961 | colour, |
| 962 | + quality: config.quality, | |
| 960 | 963 | }, |
| 961 | 964 | )?), |
| 962 | 965 | }) |
crates/ec-hw/src/types.rsmodified+3 -1
| @@ -106,7 +106,9 @@ pub struct EncoderConfig { | ||
| 106 | 106 | pub h264_profile: H264Profile, |
| 107 | 107 | /// Rate control mode. |
| 108 | 108 | pub rate_control: RateControlMode, |
| 109 | - /// Driver quality/speed level, 1 (best quality) upwards; 0 = driver default. | |
| 109 | + /// Driver quality/speed level, 1 (best quality) upwards, each step one level | |
| 110 | + /// faster; 0 = default. On Vulkan the default is the second-fastest level, | |
| 111 | + /// which costs RADV about 0.3 ms over the fastest. | |
| 110 | 112 | pub quality: u32, |
| 111 | 113 | /// The colour description to write into the VUI, or `None` to leave it |
| 112 | 114 | /// unsignalled (`video_signal_type_present_flag = 0`) as before. |
crates/ec-vk/examples/av1_encode.rsmodified+1 -0
| @@ -38,6 +38,7 @@ fn main() { | ||
| 38 | 38 | }, |
| 39 | 39 | bit_depth: 8, |
| 40 | 40 | colour: None, |
| 41 | + quality: 0, | |
| 41 | 42 | }; |
| 42 | 43 | let mut encoder = Av1Encoder::new(&gpu, cfg).expect("create encoder"); |
| 43 | 44 |
crates/ec-vk/examples/h264_encode.rsmodified+1 -0
| @@ -30,6 +30,7 @@ fn main() { | ||
| 30 | 30 | gop_size: 30, |
| 31 | 31 | profile: H264Profile::High, |
| 32 | 32 | colour: None, |
| 33 | + quality: 0, | |
| 33 | 34 | rate: match std::env::args().nth(2) { |
| 34 | 35 | Some(b) => RateControl::Cbr { |
| 35 | 36 | bitrate: b.parse().expect("bitrate"), |
crates/ec-vk/examples/h265_encode.rsmodified+1 -0
| @@ -37,6 +37,7 @@ fn main() { | ||
| 37 | 37 | }, |
| 38 | 38 | bit_depth: 8, |
| 39 | 39 | colour: None, |
| 40 | + quality: 0, | |
| 40 | 41 | }; |
| 41 | 42 | let mut encoder = H265Encoder::new(&gpu, cfg).expect("create encoder"); |
| 42 | 43 | let mut out = Vec::new(); |
crates/ec-vk/src/av1.rsmodified+3 -0
| @@ -26,6 +26,8 @@ pub struct Av1EncoderConfig { | ||
| 26 | 26 | pub level: u8, |
| 27 | 27 | /// Colour config of the sequence header, or none. |
| 28 | 28 | pub colour: Option<Colour>, |
| 29 | + /// Speed/quality trade-off as in ec-hw's `EncoderConfig::quality`: 0 for the default level, 1 for the driver's best, each step up one level faster. | |
| 30 | + pub quality: u32, | |
| 29 | 31 | } |
| 30 | 32 | |
| 31 | 33 | /// AV1 encoder: order hint, DPB slot and LAST reference bookkeeping over [`Av1EncodeSession`]. |
| @@ -59,6 +61,7 @@ impl Av1Encoder { | ||
| 59 | 61 | bit_depth: cfg.bit_depth, |
| 60 | 62 | level: cfg.level, |
| 61 | 63 | colour: cfg.colour, |
| 64 | + quality: cfg.quality, | |
| 62 | 65 | }, |
| 63 | 66 | )?; |
| 64 | 67 | let sequence_header = session.sequence_header(); |
crates/ec-vk/src/h265.rsmodified+3 -0
| @@ -24,6 +24,8 @@ pub struct H265EncoderConfig { | ||
| 24 | 24 | pub level: u8, |
| 25 | 25 | /// Colour description for the VUI, or none. |
| 26 | 26 | pub colour: Option<Colour>, |
| 27 | + /// Speed/quality trade-off as in ec-hw's `EncoderConfig::quality`: 0 for the default level, 1 for the driver's best, each step up one level faster. | |
| 28 | + pub quality: u32, | |
| 27 | 29 | } |
| 28 | 30 | |
| 29 | 31 | /// H.265 encoder: GOP, POC and DPB slot bookkeeping over [`H265EncodeSession`]. |
| @@ -57,6 +59,7 @@ impl H265Encoder { | ||
| 57 | 59 | bit_depth: cfg.bit_depth, |
| 58 | 60 | level: cfg.level, |
| 59 | 61 | colour: cfg.colour, |
| 62 | + quality: cfg.quality, | |
| 60 | 63 | }, |
| 61 | 64 | )?; |
| 62 | 65 | let parameter_sets = session.parameter_sets()?; |
crates/ec-vk/src/lib.rsmodified+3 -0
| @@ -47,6 +47,8 @@ pub struct H264EncoderConfig { | ||
| 47 | 47 | pub level: u8, |
| 48 | 48 | /// Colour description for the VUI, or none. |
| 49 | 49 | pub colour: Option<Colour>, |
| 50 | + /// Speed/quality trade-off as in ec-hw's `EncoderConfig::quality`: 0 for the default level, 1 for the driver's best, each step up one level faster. | |
| 51 | + pub quality: u32, | |
| 50 | 52 | } |
| 51 | 53 | |
| 52 | 54 | /// One coded frame, Annex-B. Keyframes carry SPS and PPS before the slice. |
| @@ -124,6 +126,7 @@ impl H264Encoder { | ||
| 124 | 126 | profile: cfg.profile, |
| 125 | 127 | level: cfg.level, |
| 126 | 128 | colour: cfg.colour, |
| 129 | + quality: cfg.quality, | |
| 127 | 130 | }, |
| 128 | 131 | )?; |
| 129 | 132 | let parameter_sets = session.parameter_sets()?; |
shims/vk-video-ffi/examples/dmabuf_smoke.rsmodified+3 -0
| @@ -90,6 +90,7 @@ impl Encode for H264EncodeSession { | ||
| 90 | 90 | profile: H264Profile::High, |
| 91 | 91 | level: 62, |
| 92 | 92 | colour: None, |
| 93 | + quality: 0, | |
| 93 | 94 | }; |
| 94 | 95 | H264EncodeSession::new(gpu, &cfg) |
| 95 | 96 | } |
| @@ -133,6 +134,7 @@ impl Encode for H265EncodeSession { | ||
| 133 | 134 | bit_depth: bits, |
| 134 | 135 | level: 186, |
| 135 | 136 | colour: None, |
| 137 | + quality: 0, | |
| 136 | 138 | }; |
| 137 | 139 | H265EncodeSession::new(gpu, &cfg) |
| 138 | 140 | } |
| @@ -179,6 +181,7 @@ impl Encode for Av1EncodeSession { | ||
| 179 | 181 | bit_depth: bits, |
| 180 | 182 | level: 23, |
| 181 | 183 | colour: None, |
| 184 | + quality: 0, | |
| 182 | 185 | }; |
| 183 | 186 | Av1EncodeSession::new(gpu, &cfg) |
| 184 | 187 | } |
shims/vk-video-ffi/shaders/nv12_to_rgb.compmodified+22 -3
| @@ -32,11 +32,30 @@ uint sample_at(uint e) { | ||
| 32 | 32 | return byte_at(e * pc.bytes + pc.bytes - 1u); |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | +ivec2 chroma_row(uint r, uint x) { | |
| 36 | + uint j = x / 2u; | |
| 37 | + uint c = pc.w * pc.h + r * pc.w + 2u * j; | |
| 38 | + ivec2 a = ivec2(int(sample_at(c)), int(sample_at(c + 1u))); | |
| 39 | + if ((x & 1u) == 0u) { | |
| 40 | + return 2 * a; | |
| 41 | + } | |
| 42 | + uint n = pc.w * pc.h + r * pc.w + 2u * min(j + 1u, pc.w / 2u - 1u); | |
| 43 | + return a + ivec2(int(sample_at(n)), int(sample_at(n + 1u))); | |
| 44 | +} | |
| 45 | + | |
| 46 | +ivec2 chroma_at(uint x, uint y) { | |
| 47 | + uint k = y / 2u; | |
| 48 | + ivec2 s = (y & 1u) == 0u | |
| 49 | + ? chroma_row(k == 0u ? 0u : k - 1u, x) + 3 * chroma_row(k, x) | |
| 50 | + : 3 * chroma_row(k, x) + chroma_row(min(k + 1u, pc.h / 2u - 1u), x); | |
| 51 | + return ((s + 4) >> 3) - 128; | |
| 52 | +} | |
| 53 | + | |
| 35 | 54 | ivec3 rgb_at(uint x, uint y) { |
| 36 | 55 | int yy = int(sample_at(y * pc.w + x)) - 16; |
| 37 | - uint c = pc.w * pc.h + (y / 2u) * pc.w + 2u * (x / 2u); | |
| 38 | - int u = int(sample_at(c)) - 128; | |
| 39 | - int v = int(sample_at(c + 1u)) - 128; | |
| 56 | + ivec2 uv = chroma_at(x, y); | |
| 57 | + int u = uv.x; | |
| 58 | + int v = uv.y; | |
| 40 | 59 | int r = (pc.ky * yy + pc.rv * v + 128) >> 8; |
| 41 | 60 | int g = (pc.ky * yy + pc.gu * u + pc.gv * v + 128) >> 8; |
| 42 | 61 | int b = (pc.ky * yy + pc.bu * u + 128) >> 8; |
shims/vk-video-ffi/shaders/nv12_to_rgb.spvmodified+0 -0
Binary file changed
shims/vk-video-ffi/src/av1_encode.rsmodified+28 -3
| @@ -7,6 +7,7 @@ use vulkanalia::vk::KhrVideoQueueExtensionDeviceCommands; | ||
| 7 | 7 | use crate::dmabuf::DmaBuf; |
| 8 | 8 | use crate::rgb_convert::{Input, check_rgb}; |
| 9 | 9 | use crate::session::{ |
| 10 | + quality_level, | |
| 10 | 11 | DPB_SLOTS, Encoder, VideoSession, component_bit_depth, picture_resource, validate_rate, |
| 11 | 12 | video_caps, vk, zeroed, |
| 12 | 13 | }; |
| @@ -32,6 +33,8 @@ pub struct Av1EncodeConfig { | ||
| 32 | 33 | pub level: u8, |
| 33 | 34 | /// Colour config of the sequence header, or none (unspecified). |
| 34 | 35 | pub colour: Option<Colour>, |
| 36 | + /// Speed/quality trade-off as in ec-hw's `EncoderConfig::quality`: 0 for the default level, 1 for the driver's best, each step up one level faster. | |
| 37 | + pub quality: u32, | |
| 35 | 38 | } |
| 36 | 39 | |
| 37 | 40 | /// One picture to encode. |
| @@ -61,6 +64,7 @@ struct Caps { | ||
| 61 | 64 | max_extent: vk::Extent2D, |
| 62 | 65 | size_align: u64, |
| 63 | 66 | rc_modes: vk::VideoEncodeRateControlModeFlagsKHR, |
| 67 | + quality_levels: u32, | |
| 64 | 68 | single_ref: bool, |
| 65 | 69 | sb64: bool, |
| 66 | 70 | max_level: vk::video::StdVideoAV1Level, |
| @@ -87,7 +91,7 @@ impl Profile { | ||
| 87 | 91 | .build(), |
| 88 | 92 | usage: vk::VideoEncodeUsageInfoKHR::builder() |
| 89 | 93 | .video_usage_hints(vk::VideoEncodeUsageFlagsKHR::STREAMING) |
| 90 | - .video_content_hints(vk::VideoEncodeContentFlagsKHR::CAMERA) | |
| 94 | + .video_content_hints(vk::VideoEncodeContentFlagsKHR::DESKTOP) | |
| 91 | 95 | .tuning_mode(vk::VideoEncodeTuningModeKHR::LOW_LATENCY) |
| 92 | 96 | .build(), |
| 93 | 97 | }); |
| @@ -215,6 +219,7 @@ impl Av1EncodeSession { | ||
| 215 | 219 | (cfg.rate, cfg.fps), |
| 216 | 220 | caps.size_align, |
| 217 | 221 | )?; |
| 222 | + enc.vs.quality_level = quality_level(cfg.quality, caps.quality_levels); | |
| 218 | 223 | let params = create_parameters(&enc.vs, extent, sb128, cfg, level)?; |
| 219 | 224 | enc.vs.set_params(params); |
| 220 | 225 | Ok(Av1EncodeSession { |
| @@ -374,7 +379,7 @@ impl Av1EncodeSession { | ||
| 374 | 379 | quant.base_q_idx = q_index(self.enc.rate) as u8; |
| 375 | 380 | let mut loop_filter = zeroed::<vk::video::StdVideoAV1LoopFilter>(); |
| 376 | 381 | loop_filter.loop_filter_level = [1; 4]; |
| 377 | - let cdef = zeroed::<vk::video::StdVideoAV1CDEF>(); | |
| 382 | + let cdef = cdef_params(); | |
| 378 | 383 | let restoration = zeroed::<vk::video::StdVideoAV1LoopRestoration>(); |
| 379 | 384 | let motion = zeroed::<vk::video::StdVideoAV1GlobalMotion>(); |
| 380 | 385 | std_pic.pTileInfo = &tile; |
| @@ -536,6 +541,7 @@ fn create_parameters( | ||
| 536 | 541 | seq.max_frame_height_minus_1 = (extent.height - 1) as u16; |
| 537 | 542 | seq.order_hint_bits_minus_1 = 7; |
| 538 | 543 | seq.seq_force_integer_mv = vk::video::STD_VIDEO_AV1_SELECT_INTEGER_MV as u8; |
| 544 | + seq.flags.set_enable_cdef(1); | |
| 539 | 545 | seq.pColorConfig = &color; |
| 540 | 546 | |
| 541 | 547 | let mut op = zeroed::<vk::video::StdVideoEncodeAV1OperatingPointInfo>(); |
| @@ -547,8 +553,12 @@ fn create_parameters( | ||
| 547 | 553 | std_operating_points: &op, |
| 548 | 554 | ..Default::default() |
| 549 | 555 | }; |
| 556 | + let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder() | |
| 557 | + .quality_level(vs.quality_level) | |
| 558 | + .build(); | |
| 550 | 559 | let info = vk::VideoSessionParametersCreateInfoKHR::builder() |
| 551 | 560 | .video_session(vs.session) |
| 561 | + .push_next(&mut quality) | |
| 552 | 562 | .push_next(&mut av1) |
| 553 | 563 | .build(); |
| 554 | 564 | unsafe { |
| @@ -643,7 +653,7 @@ fn sequence_header_obu( | ||
| 643 | 653 | b.put(0, 1); |
| 644 | 654 | b.put(7, 3); |
| 645 | 655 | b.put(0, 1); |
| 646 | - b.put(0, 1); | |
| 656 | + b.put(1, 1); | |
| 647 | 657 | b.put(0, 1); |
| 648 | 658 | b.put((bit_depth == 10) as u32, 1); |
| 649 | 659 | b.put(0, 1); |
| @@ -712,6 +722,7 @@ fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Erro | ||
| 712 | 722 | max_extent: caps.max_coded_extent, |
| 713 | 723 | size_align: caps.min_bitstream_buffer_size_alignment, |
| 714 | 724 | rc_modes: encode.rate_control_modes, |
| 725 | + quality_levels: encode.max_quality_levels, | |
| 715 | 726 | single_ref: av1.max_single_reference_count > 0 && av1.single_reference_name_mask & 1 != 0, |
| 716 | 727 | sb64: av1.superblock_sizes.contains( |
| 717 | 728 | vk::VideoEncodeAV1SuperblockSizeFlagsKHR::VIDEO_ENCODE_AV1_SUPERBLOCK_SIZE_64, |
| @@ -730,3 +741,17 @@ fn av1_level( | ||
| 730 | 741 | let level = vk::video::StdVideoAV1Level(i32::from(idx)); |
| 731 | 742 | Ok(if level.0 > driver_max.0 { driver_max } else { level }) |
| 732 | 743 | } |
| 744 | + | |
| 745 | +/// CDEF candidates the encoder picks from per 64x64 block: a main strength, off for fine texture, half and double. | |
| 746 | +fn cdef_params() -> vk::video::StdVideoAV1CDEF { | |
| 747 | + let mut cdef = zeroed::<vk::video::StdVideoAV1CDEF>(); | |
| 748 | + cdef.cdef_damping_minus_3 = 2; | |
| 749 | + cdef.cdef_bits = 2; | |
| 750 | + for (i, (pri, sec)) in [(4, 1), (0, 0), (2, 1), (8, 2)].into_iter().enumerate() { | |
| 751 | + cdef.cdef_y_pri_strength[i] = pri; | |
| 752 | + cdef.cdef_uv_pri_strength[i] = pri; | |
| 753 | + cdef.cdef_y_sec_strength[i] = sec; | |
| 754 | + cdef.cdef_uv_sec_strength[i] = sec; | |
| 755 | + } | |
| 756 | + cdef | |
| 757 | +} |
shims/vk-video-ffi/src/h264_encode.rsmodified+11 -1
| @@ -9,6 +9,7 @@ use vulkanalia::vk::{ | ||
| 9 | 9 | use crate::dmabuf::DmaBuf; |
| 10 | 10 | use crate::rgb_convert::{Input, check_rgb}; |
| 11 | 11 | use crate::session::{ |
| 12 | + quality_level, | |
| 12 | 13 | DPB_SLOTS, Encoder, VideoSession, align, picture_resource, validate_rate, video_caps, vk, |
| 13 | 14 | zeroed, |
| 14 | 15 | }; |
| @@ -120,6 +121,8 @@ pub struct H264EncodeConfig { | ||
| 120 | 121 | pub level: u8, |
| 121 | 122 | /// Colour description for the VUI, or none. |
| 122 | 123 | pub colour: Option<Colour>, |
| 124 | + /// Speed/quality trade-off as in ec-hw's `EncoderConfig::quality`: 0 for the default level, 1 for the driver's best, each step up one level faster. | |
| 125 | + pub quality: u32, | |
| 123 | 126 | } |
| 124 | 127 | |
| 125 | 128 | /// frame_num wraps at 1 << LOG2_MAX_FRAME_NUM. |
| @@ -159,6 +162,7 @@ struct Caps { | ||
| 159 | 162 | max_extent: vk::Extent2D, |
| 160 | 163 | size_align: u64, |
| 161 | 164 | rc_modes: vk::VideoEncodeRateControlModeFlagsKHR, |
| 165 | + quality_levels: u32, | |
| 162 | 166 | max_refs: u32, |
| 163 | 167 | max_level: vk::video::StdVideoH264LevelIdc, |
| 164 | 168 | } |
| @@ -188,7 +192,7 @@ impl Profile { | ||
| 188 | 192 | .build(), |
| 189 | 193 | usage: vk::VideoEncodeUsageInfoKHR::builder() |
| 190 | 194 | .video_usage_hints(vk::VideoEncodeUsageFlagsKHR::STREAMING) |
| 191 | - .video_content_hints(vk::VideoEncodeContentFlagsKHR::CAMERA) | |
| 195 | + .video_content_hints(vk::VideoEncodeContentFlagsKHR::DESKTOP) | |
| 192 | 196 | .tuning_mode(vk::VideoEncodeTuningModeKHR::LOW_LATENCY) |
| 193 | 197 | .build(), |
| 194 | 198 | }); |
| @@ -312,6 +316,7 @@ impl H264EncodeSession { | ||
| 312 | 316 | (cfg.rate, cfg.fps), |
| 313 | 317 | caps.size_align, |
| 314 | 318 | )?; |
| 319 | + enc.vs.quality_level = quality_level(cfg.quality, caps.quality_levels); | |
| 315 | 320 | let params = create_parameters(&enc.vs, cfg, level)?; |
| 316 | 321 | enc.vs.set_params(params); |
| 317 | 322 | Ok(H264EncodeSession { |
| @@ -643,8 +648,12 @@ fn create_parameters( | ||
| 643 | 648 | .max_std_pps_count(1) |
| 644 | 649 | .parameters_add_info(&add) |
| 645 | 650 | .build(); |
| 651 | + let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder() | |
| 652 | + .quality_level(vs.quality_level) | |
| 653 | + .build(); | |
| 646 | 654 | let info = vk::VideoSessionParametersCreateInfoKHR::builder() |
| 647 | 655 | .video_session(vs.session) |
| 656 | + .push_next(&mut quality) | |
| 648 | 657 | .push_next(&mut h264) |
| 649 | 658 | .build(); |
| 650 | 659 | unsafe { |
| @@ -681,6 +690,7 @@ fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Erro | ||
| 681 | 690 | max_extent: caps.max_coded_extent, |
| 682 | 691 | size_align: caps.min_bitstream_buffer_size_alignment, |
| 683 | 692 | rc_modes: encode.rate_control_modes, |
| 693 | + quality_levels: encode.max_quality_levels, | |
| 684 | 694 | max_refs: h264.max_p_picture_l0_reference_count, |
| 685 | 695 | max_level: h264.max_level_idc, |
| 686 | 696 | }) |
shims/vk-video-ffi/src/h265_encode.rsmodified+11 -1
| @@ -10,6 +10,7 @@ use crate::dmabuf::DmaBuf; | ||
| 10 | 10 | use crate::h264_encode::validate; |
| 11 | 11 | use crate::rgb_convert::{Input, check_rgb}; |
| 12 | 12 | use crate::session::{ |
| 13 | + quality_level, | |
| 13 | 14 | DPB_SLOTS, Encoder, VideoSession, align, component_bit_depth, picture_resource, video_caps, vk, |
| 14 | 15 | zeroed, |
| 15 | 16 | }; |
| @@ -32,6 +33,8 @@ pub struct H265EncodeConfig { | ||
| 32 | 33 | pub level: u8, |
| 33 | 34 | /// Colour description for the VUI, or none. |
| 34 | 35 | pub colour: Option<Colour>, |
| 36 | + /// Speed/quality trade-off as in ec-hw's `EncoderConfig::quality`: 0 for the default level, 1 for the driver's best, each step up one level faster. | |
| 37 | + pub quality: u32, | |
| 35 | 38 | } |
| 36 | 39 | |
| 37 | 40 | /// One picture to encode. |
| @@ -63,6 +66,7 @@ struct Caps { | ||
| 63 | 66 | max_extent: vk::Extent2D, |
| 64 | 67 | size_align: u64, |
| 65 | 68 | rc_modes: vk::VideoEncodeRateControlModeFlagsKHR, |
| 69 | + quality_levels: u32, | |
| 66 | 70 | max_refs: u32, |
| 67 | 71 | max_level: vk::video::StdVideoH265LevelIdc, |
| 68 | 72 | ctb_sizes: vk::VideoEncodeH265CtbSizeFlagsKHR, |
| @@ -103,7 +107,7 @@ impl Profile { | ||
| 103 | 107 | .build(), |
| 104 | 108 | usage: vk::VideoEncodeUsageInfoKHR::builder() |
| 105 | 109 | .video_usage_hints(vk::VideoEncodeUsageFlagsKHR::STREAMING) |
| 106 | - .video_content_hints(vk::VideoEncodeContentFlagsKHR::CAMERA) | |
| 110 | + .video_content_hints(vk::VideoEncodeContentFlagsKHR::DESKTOP) | |
| 107 | 111 | .tuning_mode(vk::VideoEncodeTuningModeKHR::LOW_LATENCY) |
| 108 | 112 | .build(), |
| 109 | 113 | }); |
| @@ -235,6 +239,7 @@ impl H265EncodeSession { | ||
| 235 | 239 | (cfg.rate, cfg.fps), |
| 236 | 240 | caps.size_align, |
| 237 | 241 | )?; |
| 242 | + enc.vs.quality_level = quality_level(cfg.quality, caps.quality_levels); | |
| 238 | 243 | let params = create_parameters(&enc.vs, cfg, &coding)?; |
| 239 | 244 | enc.vs.set_params(params); |
| 240 | 245 | Ok(H265EncodeSession { enc }) |
| @@ -594,8 +599,12 @@ fn create_parameters( | ||
| 594 | 599 | .max_std_pps_count(1) |
| 595 | 600 | .parameters_add_info(&add) |
| 596 | 601 | .build(); |
| 602 | + let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder() | |
| 603 | + .quality_level(vs.quality_level) | |
| 604 | + .build(); | |
| 597 | 605 | let info = vk::VideoSessionParametersCreateInfoKHR::builder() |
| 598 | 606 | .video_session(vs.session) |
| 607 | + .push_next(&mut quality) | |
| 599 | 608 | .push_next(&mut h265) |
| 600 | 609 | .build(); |
| 601 | 610 | unsafe { |
| @@ -695,6 +704,7 @@ fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Erro | ||
| 695 | 704 | max_extent: caps.max_coded_extent, |
| 696 | 705 | size_align: caps.min_bitstream_buffer_size_alignment, |
| 697 | 706 | rc_modes: encode.rate_control_modes, |
| 707 | + quality_levels: encode.max_quality_levels, | |
| 698 | 708 | max_refs: h265.max_p_picture_l0_reference_count, |
| 699 | 709 | max_level: h265.max_level_idc, |
| 700 | 710 | ctb_sizes: h265.ctb_sizes, |
shims/vk-video-ffi/src/session.rsmodified+14 -1
| @@ -62,6 +62,17 @@ pub(crate) fn align_u64(value: u64, to: u64) -> u64 { | ||
| 62 | 62 | value.div_ceil(to) * to |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | +const DEFAULT_QUALITY_LEVEL: u32 = 1; | |
| 66 | + | |
| 67 | +/// The encode quality level for `quality` (0 default, 1 best, each step up one level faster) on a driver with `levels` levels, best last. | |
| 68 | +pub(crate) fn quality_level(quality: u32, levels: u32) -> u32 { | |
| 69 | + let top = levels.saturating_sub(1); | |
| 70 | + match quality { | |
| 71 | + 0 => DEFAULT_QUALITY_LEVEL.min(top), | |
| 72 | + q => top.saturating_sub(q - 1), | |
| 73 | + } | |
| 74 | +} | |
| 75 | + | |
| 65 | 76 | pub(crate) fn validate_rate(fps: (u32, u32), rate: RateControl) -> Result<(), Error> { |
| 66 | 77 | if fps.0 == 0 || fps.1 == 0 { |
| 67 | 78 | return Err(Error::Invalid("frame rate".into())); |
| @@ -483,6 +494,7 @@ pub(crate) struct VideoSession { | ||
| 483 | 494 | pub(crate) format: vk::Format, |
| 484 | 495 | pub(crate) session: vk::VideoSessionKHR, |
| 485 | 496 | pub(crate) params: vk::VideoSessionParametersKHR, |
| 497 | + pub(crate) quality_level: u32, | |
| 486 | 498 | memories: Vec<vk::DeviceMemory>, |
| 487 | 499 | } |
| 488 | 500 | |
| @@ -507,6 +519,7 @@ impl VideoSession { | ||
| 507 | 519 | format, |
| 508 | 520 | session: Default::default(), |
| 509 | 521 | params: Default::default(), |
| 522 | + quality_level: 0, | |
| 510 | 523 | memories: Vec::new(), |
| 511 | 524 | }; |
| 512 | 525 | let d = gpu.device(); |
| @@ -1343,7 +1356,7 @@ impl Encoder { | ||
| 1343 | 1356 | let cb = slot.main.cb; |
| 1344 | 1357 | let query = k as u32; |
| 1345 | 1358 | let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder() |
| 1346 | - .quality_level(0) | |
| 1359 | + .quality_level(self.vs.quality_level) | |
| 1347 | 1360 | .build(); |
| 1348 | 1361 | let reset = !self.coded; |
| 1349 | 1362 | let update = reset || self.dirty; |