forge
Explore
Log in

Don't you dare go Hollow

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 commit

9876fb67e5024ad21452acda9fd1adc97a7066abAhmet Kaan GümüşParent: 511a95b
crates/ec-dsp/src/rgb.rsmodified+22 -3
@@ -125,10 +125,9 @@ pub fn i420_to_packed(
125 let mut sum = [0u32; 3];125 let mut sum = [0u32; 3];
126 for sy in y0..y1 {126 for sy in y0..y1 {
127 for sx in x0..x1 {127 for sx in x0..x1 {
128- let c = (sy / 2) * cw + sx / 2;
129 let yy = i32::from(y[sy * w + sx]) - 16;128 let yy = i32::from(y[sy * w + sx]) - 16;
130- let uu = i32::from(u[c]) - 128;129+ let uu = chroma_at(u, (cw, h / 2), sx, sy) - 128;
131- let vv = i32::from(v[c]) - 128;130+ let vv = chroma_at(v, (cw, h / 2), sx, sy) - 128;
132 let r = ((k.ky * yy + k.rv * vv + 128) >> 8).clamp(0, 255);131 let r = ((k.ky * yy + k.rv * vv + 128) >> 8).clamp(0, 255);
133 let g = ((k.ky * yy + k.gu * uu + k.gv * vv + 128) >> 8).clamp(0, 255);132 let g = ((k.ky * yy + k.gu * uu + k.gv * vv + 128) >> 8).clamp(0, 255);
134 let b = ((k.ky * yy + k.bu * uu + 128) >> 8).clamp(0, 255);133 let b = ((k.ky * yy + k.bu * uu + 128) >> 8).clamp(0, 255);
@@ -147,6 +146,26 @@ pub fn i420_to_packed(
147 out146 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 fn footprint(o: usize, n_in: usize, n_out: usize) -> (usize, usize) {169 fn footprint(o: usize, n_in: usize, n_out: usize) -> (usize, usize) {
151 (o * n_in / n_out, ((o + 1) * n_in).div_ceil(n_out))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 profile: vk_h264_profile(config.h264_profile),931 profile: vk_h264_profile(config.h264_profile),
932 level,932 level,
933 colour,933 colour,
934+ quality: config.quality,
934 },935 },
935 )?),936 )?),
936 EncCodec::H265 => VkEncoder::H265(ec_vk::H265Encoder::new(937 EncCodec::H265 => VkEncoder::H265(ec_vk::H265Encoder::new(
@@ -944,6 +945,7 @@ fn open_vk(config: EncoderConfig) -> Result<VkEncoder> {
944 bit_depth: config.bit_depth,945 bit_depth: config.bit_depth,
945 level,946 level,
946 colour,947 colour,
948+ quality: config.quality,
947 },949 },
948 )?),950 )?),
949 EncCodec::Av1 => VkEncoder::Av1(ec_vk::Av1Encoder::new(951 EncCodec::Av1 => VkEncoder::Av1(ec_vk::Av1Encoder::new(
@@ -957,6 +959,7 @@ fn open_vk(config: EncoderConfig) -> Result<VkEncoder> {
957 bit_depth: config.bit_depth,959 bit_depth: config.bit_depth,
958 level,960 level,
959 colour,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 pub h264_profile: H264Profile,106 pub h264_profile: H264Profile,
107 /// Rate control mode.107 /// Rate control mode.
108 pub rate_control: RateControlMode,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 pub quality: u32,112 pub quality: u32,
111 /// The colour description to write into the VUI, or `None` to leave it113 /// The colour description to write into the VUI, or `None` to leave it
112 /// unsignalled (`video_signal_type_present_flag = 0`) as before.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 bit_depth: 8,39 bit_depth: 8,
40 colour: None,40 colour: None,
41+ quality: 0,
41 };42 };
42 let mut encoder = Av1Encoder::new(&gpu, cfg).expect("create encoder");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 gop_size: 30,30 gop_size: 30,
31 profile: H264Profile::High,31 profile: H264Profile::High,
32 colour: None,32 colour: None,
33+ quality: 0,
33 rate: match std::env::args().nth(2) {34 rate: match std::env::args().nth(2) {
34 Some(b) => RateControl::Cbr {35 Some(b) => RateControl::Cbr {
35 bitrate: b.parse().expect("bitrate"),36 bitrate: b.parse().expect("bitrate"),
crates/ec-vk/examples/h265_encode.rsmodified+1 -0
@@ -37,6 +37,7 @@ fn main() {
37 },37 },
38 bit_depth: 8,38 bit_depth: 8,
39 colour: None,39 colour: None,
40+ quality: 0,
40 };41 };
41 let mut encoder = H265Encoder::new(&gpu, cfg).expect("create encoder");42 let mut encoder = H265Encoder::new(&gpu, cfg).expect("create encoder");
42 let mut out = Vec::new();43 let mut out = Vec::new();
crates/ec-vk/src/av1.rsmodified+3 -0
@@ -26,6 +26,8 @@ pub struct Av1EncoderConfig {
26 pub level: u8,26 pub level: u8,
27 /// Colour config of the sequence header, or none.27 /// Colour config of the sequence header, or none.
28 pub colour: Option<Colour>,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 /// AV1 encoder: order hint, DPB slot and LAST reference bookkeeping over [`Av1EncodeSession`].33 /// AV1 encoder: order hint, DPB slot and LAST reference bookkeeping over [`Av1EncodeSession`].
@@ -59,6 +61,7 @@ impl Av1Encoder {
59 bit_depth: cfg.bit_depth,61 bit_depth: cfg.bit_depth,
60 level: cfg.level,62 level: cfg.level,
61 colour: cfg.colour,63 colour: cfg.colour,
64+ quality: cfg.quality,
62 },65 },
63 )?;66 )?;
64 let sequence_header = session.sequence_header();67 let sequence_header = session.sequence_header();
crates/ec-vk/src/h265.rsmodified+3 -0
@@ -24,6 +24,8 @@ pub struct H265EncoderConfig {
24 pub level: u8,24 pub level: u8,
25 /// Colour description for the VUI, or none.25 /// Colour description for the VUI, or none.
26 pub colour: Option<Colour>,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 /// H.265 encoder: GOP, POC and DPB slot bookkeeping over [`H265EncodeSession`].31 /// H.265 encoder: GOP, POC and DPB slot bookkeeping over [`H265EncodeSession`].
@@ -57,6 +59,7 @@ impl H265Encoder {
57 bit_depth: cfg.bit_depth,59 bit_depth: cfg.bit_depth,
58 level: cfg.level,60 level: cfg.level,
59 colour: cfg.colour,61 colour: cfg.colour,
62+ quality: cfg.quality,
60 },63 },
61 )?;64 )?;
62 let parameter_sets = session.parameter_sets()?;65 let parameter_sets = session.parameter_sets()?;
crates/ec-vk/src/lib.rsmodified+3 -0
@@ -47,6 +47,8 @@ pub struct H264EncoderConfig {
47 pub level: u8,47 pub level: u8,
48 /// Colour description for the VUI, or none.48 /// Colour description for the VUI, or none.
49 pub colour: Option<Colour>,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 /// One coded frame, Annex-B. Keyframes carry SPS and PPS before the slice.54 /// One coded frame, Annex-B. Keyframes carry SPS and PPS before the slice.
@@ -124,6 +126,7 @@ impl H264Encoder {
124 profile: cfg.profile,126 profile: cfg.profile,
125 level: cfg.level,127 level: cfg.level,
126 colour: cfg.colour,128 colour: cfg.colour,
129+ quality: cfg.quality,
127 },130 },
128 )?;131 )?;
129 let parameter_sets = session.parameter_sets()?;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 profile: H264Profile::High,90 profile: H264Profile::High,
91 level: 62,91 level: 62,
92 colour: None,92 colour: None,
93+ quality: 0,
93 };94 };
94 H264EncodeSession::new(gpu, &cfg)95 H264EncodeSession::new(gpu, &cfg)
95 }96 }
@@ -133,6 +134,7 @@ impl Encode for H265EncodeSession {
133 bit_depth: bits,134 bit_depth: bits,
134 level: 186,135 level: 186,
135 colour: None,136 colour: None,
137+ quality: 0,
136 };138 };
137 H265EncodeSession::new(gpu, &cfg)139 H265EncodeSession::new(gpu, &cfg)
138 }140 }
@@ -179,6 +181,7 @@ impl Encode for Av1EncodeSession {
179 bit_depth: bits,181 bit_depth: bits,
180 level: 23,182 level: 23,
181 colour: None,183 colour: None,
184+ quality: 0,
182 };185 };
183 Av1EncodeSession::new(gpu, &cfg)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 return byte_at(e * pc.bytes + pc.bytes - 1u);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 ivec3 rgb_at(uint x, uint y) {54 ivec3 rgb_at(uint x, uint y) {
36 int yy = int(sample_at(y * pc.w + x)) - 16;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);56+ ivec2 uv = chroma_at(x, y);
38- int u = int(sample_at(c)) - 128;57+ int u = uv.x;
39- int v = int(sample_at(c + 1u)) - 128;58+ int v = uv.y;
40 int r = (pc.ky * yy + pc.rv * v + 128) >> 8;59 int r = (pc.ky * yy + pc.rv * v + 128) >> 8;
41 int g = (pc.ky * yy + pc.gu * u + pc.gv * v + 128) >> 8;60 int g = (pc.ky * yy + pc.gu * u + pc.gv * v + 128) >> 8;
42 int b = (pc.ky * yy + pc.bu * u + 128) >> 8;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 use crate::dmabuf::DmaBuf;7 use crate::dmabuf::DmaBuf;
8 use crate::rgb_convert::{Input, check_rgb};8 use crate::rgb_convert::{Input, check_rgb};
9 use crate::session::{9 use crate::session::{
10+ quality_level,
10 DPB_SLOTS, Encoder, VideoSession, component_bit_depth, picture_resource, validate_rate,11 DPB_SLOTS, Encoder, VideoSession, component_bit_depth, picture_resource, validate_rate,
11 video_caps, vk, zeroed,12 video_caps, vk, zeroed,
12 };13 };
@@ -32,6 +33,8 @@ pub struct Av1EncodeConfig {
32 pub level: u8,33 pub level: u8,
33 /// Colour config of the sequence header, or none (unspecified).34 /// Colour config of the sequence header, or none (unspecified).
34 pub colour: Option<Colour>,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 /// One picture to encode.40 /// One picture to encode.
@@ -61,6 +64,7 @@ struct Caps {
61 max_extent: vk::Extent2D,64 max_extent: vk::Extent2D,
62 size_align: u64,65 size_align: u64,
63 rc_modes: vk::VideoEncodeRateControlModeFlagsKHR,66 rc_modes: vk::VideoEncodeRateControlModeFlagsKHR,
67+ quality_levels: u32,
64 single_ref: bool,68 single_ref: bool,
65 sb64: bool,69 sb64: bool,
66 max_level: vk::video::StdVideoAV1Level,70 max_level: vk::video::StdVideoAV1Level,
@@ -87,7 +91,7 @@ impl Profile {
87 .build(),91 .build(),
88 usage: vk::VideoEncodeUsageInfoKHR::builder()92 usage: vk::VideoEncodeUsageInfoKHR::builder()
89 .video_usage_hints(vk::VideoEncodeUsageFlagsKHR::STREAMING)93 .video_usage_hints(vk::VideoEncodeUsageFlagsKHR::STREAMING)
90- .video_content_hints(vk::VideoEncodeContentFlagsKHR::CAMERA)94+ .video_content_hints(vk::VideoEncodeContentFlagsKHR::DESKTOP)
91 .tuning_mode(vk::VideoEncodeTuningModeKHR::LOW_LATENCY)95 .tuning_mode(vk::VideoEncodeTuningModeKHR::LOW_LATENCY)
92 .build(),96 .build(),
93 });97 });
@@ -215,6 +219,7 @@ impl Av1EncodeSession {
215 (cfg.rate, cfg.fps),219 (cfg.rate, cfg.fps),
216 caps.size_align,220 caps.size_align,
217 )?;221 )?;
222+ enc.vs.quality_level = quality_level(cfg.quality, caps.quality_levels);
218 let params = create_parameters(&enc.vs, extent, sb128, cfg, level)?;223 let params = create_parameters(&enc.vs, extent, sb128, cfg, level)?;
219 enc.vs.set_params(params);224 enc.vs.set_params(params);
220 Ok(Av1EncodeSession {225 Ok(Av1EncodeSession {
@@ -374,7 +379,7 @@ impl Av1EncodeSession {
374 quant.base_q_idx = q_index(self.enc.rate) as u8;379 quant.base_q_idx = q_index(self.enc.rate) as u8;
375 let mut loop_filter = zeroed::<vk::video::StdVideoAV1LoopFilter>();380 let mut loop_filter = zeroed::<vk::video::StdVideoAV1LoopFilter>();
376 loop_filter.loop_filter_level = [1; 4];381 loop_filter.loop_filter_level = [1; 4];
377- let cdef = zeroed::<vk::video::StdVideoAV1CDEF>();382+ let cdef = cdef_params();
378 let restoration = zeroed::<vk::video::StdVideoAV1LoopRestoration>();383 let restoration = zeroed::<vk::video::StdVideoAV1LoopRestoration>();
379 let motion = zeroed::<vk::video::StdVideoAV1GlobalMotion>();384 let motion = zeroed::<vk::video::StdVideoAV1GlobalMotion>();
380 std_pic.pTileInfo = &tile;385 std_pic.pTileInfo = &tile;
@@ -536,6 +541,7 @@ fn create_parameters(
536 seq.max_frame_height_minus_1 = (extent.height - 1) as u16;541 seq.max_frame_height_minus_1 = (extent.height - 1) as u16;
537 seq.order_hint_bits_minus_1 = 7;542 seq.order_hint_bits_minus_1 = 7;
538 seq.seq_force_integer_mv = vk::video::STD_VIDEO_AV1_SELECT_INTEGER_MV as u8;543 seq.seq_force_integer_mv = vk::video::STD_VIDEO_AV1_SELECT_INTEGER_MV as u8;
544+ seq.flags.set_enable_cdef(1);
539 seq.pColorConfig = &color;545 seq.pColorConfig = &color;
540 546
541 let mut op = zeroed::<vk::video::StdVideoEncodeAV1OperatingPointInfo>();547 let mut op = zeroed::<vk::video::StdVideoEncodeAV1OperatingPointInfo>();
@@ -547,8 +553,12 @@ fn create_parameters(
547 std_operating_points: &op,553 std_operating_points: &op,
548 ..Default::default()554 ..Default::default()
549 };555 };
556+ let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder()
557+ .quality_level(vs.quality_level)
558+ .build();
550 let info = vk::VideoSessionParametersCreateInfoKHR::builder()559 let info = vk::VideoSessionParametersCreateInfoKHR::builder()
551 .video_session(vs.session)560 .video_session(vs.session)
561+ .push_next(&mut quality)
552 .push_next(&mut av1)562 .push_next(&mut av1)
553 .build();563 .build();
554 unsafe {564 unsafe {
@@ -643,7 +653,7 @@ fn sequence_header_obu(
643 b.put(0, 1);653 b.put(0, 1);
644 b.put(7, 3);654 b.put(7, 3);
645 b.put(0, 1);655 b.put(0, 1);
646- b.put(0, 1);656+ b.put(1, 1);
647 b.put(0, 1);657 b.put(0, 1);
648 b.put((bit_depth == 10) as u32, 1);658 b.put((bit_depth == 10) as u32, 1);
649 b.put(0, 1);659 b.put(0, 1);
@@ -712,6 +722,7 @@ fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Erro
712 max_extent: caps.max_coded_extent,722 max_extent: caps.max_coded_extent,
713 size_align: caps.min_bitstream_buffer_size_alignment,723 size_align: caps.min_bitstream_buffer_size_alignment,
714 rc_modes: encode.rate_control_modes,724 rc_modes: encode.rate_control_modes,
725+ quality_levels: encode.max_quality_levels,
715 single_ref: av1.max_single_reference_count > 0 && av1.single_reference_name_mask & 1 != 0,726 single_ref: av1.max_single_reference_count > 0 && av1.single_reference_name_mask & 1 != 0,
716 sb64: av1.superblock_sizes.contains(727 sb64: av1.superblock_sizes.contains(
717 vk::VideoEncodeAV1SuperblockSizeFlagsKHR::VIDEO_ENCODE_AV1_SUPERBLOCK_SIZE_64,728 vk::VideoEncodeAV1SuperblockSizeFlagsKHR::VIDEO_ENCODE_AV1_SUPERBLOCK_SIZE_64,
@@ -730,3 +741,17 @@ fn av1_level(
730 let level = vk::video::StdVideoAV1Level(i32::from(idx));741 let level = vk::video::StdVideoAV1Level(i32::from(idx));
731 Ok(if level.0 > driver_max.0 { driver_max } else { level })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 use crate::dmabuf::DmaBuf;9 use crate::dmabuf::DmaBuf;
10 use crate::rgb_convert::{Input, check_rgb};10 use crate::rgb_convert::{Input, check_rgb};
11 use crate::session::{11 use crate::session::{
12+ quality_level,
12 DPB_SLOTS, Encoder, VideoSession, align, picture_resource, validate_rate, video_caps, vk,13 DPB_SLOTS, Encoder, VideoSession, align, picture_resource, validate_rate, video_caps, vk,
13 zeroed,14 zeroed,
14 };15 };
@@ -120,6 +121,8 @@ pub struct H264EncodeConfig {
120 pub level: u8,121 pub level: u8,
121 /// Colour description for the VUI, or none.122 /// Colour description for the VUI, or none.
122 pub colour: Option<Colour>,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 /// frame_num wraps at 1 << LOG2_MAX_FRAME_NUM.128 /// frame_num wraps at 1 << LOG2_MAX_FRAME_NUM.
@@ -159,6 +162,7 @@ struct Caps {
159 max_extent: vk::Extent2D,162 max_extent: vk::Extent2D,
160 size_align: u64,163 size_align: u64,
161 rc_modes: vk::VideoEncodeRateControlModeFlagsKHR,164 rc_modes: vk::VideoEncodeRateControlModeFlagsKHR,
165+ quality_levels: u32,
162 max_refs: u32,166 max_refs: u32,
163 max_level: vk::video::StdVideoH264LevelIdc,167 max_level: vk::video::StdVideoH264LevelIdc,
164 }168 }
@@ -188,7 +192,7 @@ impl Profile {
188 .build(),192 .build(),
189 usage: vk::VideoEncodeUsageInfoKHR::builder()193 usage: vk::VideoEncodeUsageInfoKHR::builder()
190 .video_usage_hints(vk::VideoEncodeUsageFlagsKHR::STREAMING)194 .video_usage_hints(vk::VideoEncodeUsageFlagsKHR::STREAMING)
191- .video_content_hints(vk::VideoEncodeContentFlagsKHR::CAMERA)195+ .video_content_hints(vk::VideoEncodeContentFlagsKHR::DESKTOP)
192 .tuning_mode(vk::VideoEncodeTuningModeKHR::LOW_LATENCY)196 .tuning_mode(vk::VideoEncodeTuningModeKHR::LOW_LATENCY)
193 .build(),197 .build(),
194 });198 });
@@ -312,6 +316,7 @@ impl H264EncodeSession {
312 (cfg.rate, cfg.fps),316 (cfg.rate, cfg.fps),
313 caps.size_align,317 caps.size_align,
314 )?;318 )?;
319+ enc.vs.quality_level = quality_level(cfg.quality, caps.quality_levels);
315 let params = create_parameters(&enc.vs, cfg, level)?;320 let params = create_parameters(&enc.vs, cfg, level)?;
316 enc.vs.set_params(params);321 enc.vs.set_params(params);
317 Ok(H264EncodeSession {322 Ok(H264EncodeSession {
@@ -643,8 +648,12 @@ fn create_parameters(
643 .max_std_pps_count(1)648 .max_std_pps_count(1)
644 .parameters_add_info(&add)649 .parameters_add_info(&add)
645 .build();650 .build();
651+ let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder()
652+ .quality_level(vs.quality_level)
653+ .build();
646 let info = vk::VideoSessionParametersCreateInfoKHR::builder()654 let info = vk::VideoSessionParametersCreateInfoKHR::builder()
647 .video_session(vs.session)655 .video_session(vs.session)
656+ .push_next(&mut quality)
648 .push_next(&mut h264)657 .push_next(&mut h264)
649 .build();658 .build();
650 unsafe {659 unsafe {
@@ -681,6 +690,7 @@ fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Erro
681 max_extent: caps.max_coded_extent,690 max_extent: caps.max_coded_extent,
682 size_align: caps.min_bitstream_buffer_size_alignment,691 size_align: caps.min_bitstream_buffer_size_alignment,
683 rc_modes: encode.rate_control_modes,692 rc_modes: encode.rate_control_modes,
693+ quality_levels: encode.max_quality_levels,
684 max_refs: h264.max_p_picture_l0_reference_count,694 max_refs: h264.max_p_picture_l0_reference_count,
685 max_level: h264.max_level_idc,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 use crate::h264_encode::validate;10 use crate::h264_encode::validate;
11 use crate::rgb_convert::{Input, check_rgb};11 use crate::rgb_convert::{Input, check_rgb};
12 use crate::session::{12 use crate::session::{
13+ quality_level,
13 DPB_SLOTS, Encoder, VideoSession, align, component_bit_depth, picture_resource, video_caps, vk,14 DPB_SLOTS, Encoder, VideoSession, align, component_bit_depth, picture_resource, video_caps, vk,
14 zeroed,15 zeroed,
15 };16 };
@@ -32,6 +33,8 @@ pub struct H265EncodeConfig {
32 pub level: u8,33 pub level: u8,
33 /// Colour description for the VUI, or none.34 /// Colour description for the VUI, or none.
34 pub colour: Option<Colour>,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 /// One picture to encode.40 /// One picture to encode.
@@ -63,6 +66,7 @@ struct Caps {
63 max_extent: vk::Extent2D,66 max_extent: vk::Extent2D,
64 size_align: u64,67 size_align: u64,
65 rc_modes: vk::VideoEncodeRateControlModeFlagsKHR,68 rc_modes: vk::VideoEncodeRateControlModeFlagsKHR,
69+ quality_levels: u32,
66 max_refs: u32,70 max_refs: u32,
67 max_level: vk::video::StdVideoH265LevelIdc,71 max_level: vk::video::StdVideoH265LevelIdc,
68 ctb_sizes: vk::VideoEncodeH265CtbSizeFlagsKHR,72 ctb_sizes: vk::VideoEncodeH265CtbSizeFlagsKHR,
@@ -103,7 +107,7 @@ impl Profile {
103 .build(),107 .build(),
104 usage: vk::VideoEncodeUsageInfoKHR::builder()108 usage: vk::VideoEncodeUsageInfoKHR::builder()
105 .video_usage_hints(vk::VideoEncodeUsageFlagsKHR::STREAMING)109 .video_usage_hints(vk::VideoEncodeUsageFlagsKHR::STREAMING)
106- .video_content_hints(vk::VideoEncodeContentFlagsKHR::CAMERA)110+ .video_content_hints(vk::VideoEncodeContentFlagsKHR::DESKTOP)
107 .tuning_mode(vk::VideoEncodeTuningModeKHR::LOW_LATENCY)111 .tuning_mode(vk::VideoEncodeTuningModeKHR::LOW_LATENCY)
108 .build(),112 .build(),
109 });113 });
@@ -235,6 +239,7 @@ impl H265EncodeSession {
235 (cfg.rate, cfg.fps),239 (cfg.rate, cfg.fps),
236 caps.size_align,240 caps.size_align,
237 )?;241 )?;
242+ enc.vs.quality_level = quality_level(cfg.quality, caps.quality_levels);
238 let params = create_parameters(&enc.vs, cfg, &coding)?;243 let params = create_parameters(&enc.vs, cfg, &coding)?;
239 enc.vs.set_params(params);244 enc.vs.set_params(params);
240 Ok(H265EncodeSession { enc })245 Ok(H265EncodeSession { enc })
@@ -594,8 +599,12 @@ fn create_parameters(
594 .max_std_pps_count(1)599 .max_std_pps_count(1)
595 .parameters_add_info(&add)600 .parameters_add_info(&add)
596 .build();601 .build();
602+ let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder()
603+ .quality_level(vs.quality_level)
604+ .build();
597 let info = vk::VideoSessionParametersCreateInfoKHR::builder()605 let info = vk::VideoSessionParametersCreateInfoKHR::builder()
598 .video_session(vs.session)606 .video_session(vs.session)
607+ .push_next(&mut quality)
599 .push_next(&mut h265)608 .push_next(&mut h265)
600 .build();609 .build();
601 unsafe {610 unsafe {
@@ -695,6 +704,7 @@ fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Erro
695 max_extent: caps.max_coded_extent,704 max_extent: caps.max_coded_extent,
696 size_align: caps.min_bitstream_buffer_size_alignment,705 size_align: caps.min_bitstream_buffer_size_alignment,
697 rc_modes: encode.rate_control_modes,706 rc_modes: encode.rate_control_modes,
707+ quality_levels: encode.max_quality_levels,
698 max_refs: h265.max_p_picture_l0_reference_count,708 max_refs: h265.max_p_picture_l0_reference_count,
699 max_level: h265.max_level_idc,709 max_level: h265.max_level_idc,
700 ctb_sizes: h265.ctb_sizes,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 value.div_ceil(to) * to62 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 pub(crate) fn validate_rate(fps: (u32, u32), rate: RateControl) -> Result<(), Error> {76 pub(crate) fn validate_rate(fps: (u32, u32), rate: RateControl) -> Result<(), Error> {
66 if fps.0 == 0 || fps.1 == 0 {77 if fps.0 == 0 || fps.1 == 0 {
67 return Err(Error::Invalid("frame rate".into()));78 return Err(Error::Invalid("frame rate".into()));
@@ -483,6 +494,7 @@ pub(crate) struct VideoSession {
483 pub(crate) format: vk::Format,494 pub(crate) format: vk::Format,
484 pub(crate) session: vk::VideoSessionKHR,495 pub(crate) session: vk::VideoSessionKHR,
485 pub(crate) params: vk::VideoSessionParametersKHR,496 pub(crate) params: vk::VideoSessionParametersKHR,
497+ pub(crate) quality_level: u32,
486 memories: Vec<vk::DeviceMemory>,498 memories: Vec<vk::DeviceMemory>,
487 }499 }
488 500
@@ -507,6 +519,7 @@ impl VideoSession {
507 format,519 format,
508 session: Default::default(),520 session: Default::default(),
509 params: Default::default(),521 params: Default::default(),
522+ quality_level: 0,
510 memories: Vec::new(),523 memories: Vec::new(),
511 };524 };
512 let d = gpu.device();525 let d = gpu.device();
@@ -1343,7 +1356,7 @@ impl Encoder {
1343 let cb = slot.main.cb;1356 let cb = slot.main.cb;
1344 let query = k as u32;1357 let query = k as u32;
1345 let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder()1358 let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder()
1346- .quality_level(0)1359+ .quality_level(self.vs.quality_level)
1347 .build();1360 .build();
1348 let reset = !self.coded;1361 let reset = !self.coded;
1349 let update = reset || self.dirty;1362 let update = reset || self.dirty;