From a1a1150ebccc052d3bf976fc505be069d3b1ffea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 14 Aug 2026 18:45:57 +0300 Subject: [PATCH] feat(va): encode capabilities and the image upload path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `CapEntry` now reports `VAConfigAttribEncPackedHeaders` and `VAConfigAttribRateControl` alongside the RT formats, in the same round trip. The packed-header mask is not decoration: a config created without asking for those headers gets a driver that ignores the ones the application writes, which on radeonsi means a stream with no parameter sets in front of it. `Surface::write_from` wraps `vaPutImage`, the encoder's upload path — fill a standalone image with source pixels and let the driver detile and convert on the way in. `VA_ATTRIB_NOT_SUPPORTED` is reported as zero rather than as every bit set: a decode entrypoint answers that for the encode-only attributes, and "all capabilities" is not what it means. --- crates/ec-va/src/caps.rs | 40 +++++++++++++++++++++++++++++-------- crates/ec-va/src/surface.rs | 34 +++++++++++++++++++++++++++++++ crates/ec-va/src/sys.rs | 29 +++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 8 deletions(-) diff --git a/crates/ec-va/src/caps.rs b/crates/ec-va/src/caps.rs index 9a7136db..222939b1 100644 --- a/crates/ec-va/src/caps.rs +++ b/crates/ec-va/src/caps.rs @@ -157,6 +157,12 @@ pub struct CapEntry { pub entrypoint: Entrypoint, /// `VAConfigAttribRTFormat` bitmask (`VA_RT_FORMAT_*`). pub rt_formats: u32, + /// `VAConfigAttribEncPackedHeaders` bitmask (`VA_ENC_PACKED_HEADER_*`); + /// zero for a decode entrypoint. An encoder has to ask for these in its + /// config before the driver will use the headers it writes. + pub packed_headers: u32, + /// `VAConfigAttribRateControl` bitmask (`VA_RC_*`); zero for decode. + pub rate_control: u32, /// Surface geometry and pixel formats this config accepts, when the driver /// reports them. `None` if `vaQuerySurfaceAttributes` refused the config. pub surfaces: Option, @@ -224,7 +230,8 @@ fn probe_entries(display: &Arc) -> Result> { let mut entries = Vec::new(); for profile in query_profiles(display)? { for entrypoint in query_entrypoints(display, profile)? { - let rt_formats = query_rt_formats(display, profile, entrypoint)?; + let [rt_formats, packed_headers, rate_control] = + query_attribs(display, profile, entrypoint)?; // Creating the config is also how the legal surface sizes are // discovered: `vaQuerySurfaceAttributes` needs a config id. A // driver may refuse a pair it just advertised (radeonsi does this @@ -238,6 +245,8 @@ fn probe_entries(display: &Arc) -> Result> { profile, entrypoint, rt_formats, + packed_headers, + rate_control, surfaces, }); } @@ -282,15 +291,27 @@ fn query_entrypoints(display: &Arc, profile: Profile) -> Result, profile: Profile, entrypoint: Entrypoint, -) -> Result { - let mut attrs = [sys::VAConfigAttrib { - type_: sys::VAConfigAttribRTFormat, - value: 0, - }]; +) -> Result<[u32; 3]> { + let mut attrs = [ + sys::VAConfigAttrib { + type_: sys::VAConfigAttribRTFormat, + value: 0, + }, + sys::VAConfigAttrib { + type_: sys::VAConfigAttribEncPackedHeaders, + value: 0, + }, + sys::VAConfigAttrib { + type_: sys::VAConfigAttribRateControl, + value: 0, + }, + ]; // SAFETY: `attrs` is a valid, initialized array of 1 VAConfigAttrib and the // length passed matches. vaGetConfigAttributes writes only `value`. let status = unsafe { @@ -303,7 +324,10 @@ fn query_rt_formats( ) }; check("vaGetConfigAttributes", status)?; - Ok(attrs[0].value) + // VA_ATTRIB_NOT_SUPPORTED is 0xffffffff; a decode entrypoint answers that + // for the encode-only attributes, and "all bits set" is not a capability. + let value = |a: &sys::VAConfigAttrib| if a.value == u32::MAX { 0 } else { a.value }; + Ok([value(&attrs[0]), value(&attrs[1]), value(&attrs[2])]) } fn query_image_formats(display: &Arc) -> Result> { diff --git a/crates/ec-va/src/surface.rs b/crates/ec-va/src/surface.rs index 8fc2f04f..04542e73 100644 --- a/crates/ec-va/src/surface.rs +++ b/crates/ec-va/src/surface.rs @@ -232,6 +232,40 @@ impl Surface { check("vaGetImage", status) } + /// Copy an [`Image`] into the surface (`vaPutImage`). + /// + /// The upload path an encoder needs: fill a standalone image with source + /// pixels, then hand it to the driver, which detiles and converts on the + /// way in. The region is `(0, 0, width, height)` on both sides — scaling + /// during upload is a video-processing job, not an encoder's. + pub fn write_from(&self, image: &Image, width: u32, height: u32) -> Result<()> { + let (img_w, img_h) = image.size(); + if width > img_w || height > img_h { + return Err(Error::Protocol(format!( + "vaPutImage region {width}x{height} exceeds image {img_w}x{img_h}" + ))); + } + // SAFETY: valid display and surface id; `image.raw.image_id` is a live + // image on the same display, and the region is bounded above by both + // the image and the surface size (va.h:4860). + let status = unsafe { + sys::vaPutImage( + self.display.handle(), + self.id, + image.raw.image_id, + 0, + 0, + width, + height, + 0, + 0, + width, + height, + ) + }; + check("vaPutImage", status) + } + /// The surface id, for FFI-level callers (`ec-hw` picture parameters). pub fn id(&self) -> sys::VASurfaceID { self.id diff --git a/crates/ec-va/src/sys.rs b/crates/ec-va/src/sys.rs index ea7acd38..16c1511a 100644 --- a/crates/ec-va/src/sys.rs +++ b/crates/ec-va/src/sys.rs @@ -98,6 +98,7 @@ pub const VAConfigAttribRTFormat: VAConfigAttribType = 0; pub const VAConfigAttribRateControl: VAConfigAttribType = 5; pub const VAConfigAttribDecSliceMode: VAConfigAttribType = 6; pub const VAConfigAttribEncPackedHeaders: VAConfigAttribType = 10; +pub const VAConfigAttribEncMaxRefFrames: VAConfigAttribType = 13; // RT formats (`va.h:1073-1090`). pub const VA_RT_FORMAT_YUV420: u32 = 0x0000_0001; @@ -149,6 +150,20 @@ pub const VAEncPackedHeaderParameterBufferType: VABufferType = 25; pub const VAEncPackedHeaderDataBufferType: VABufferType = 26; pub const VAEncMiscParameterBufferType: VABufferType = 27; +// Packed header bits an encoder may take over (`va.h:1199-1231`). +pub const VA_ENC_PACKED_HEADER_NONE: u32 = 0x0000_0000; +pub const VA_ENC_PACKED_HEADER_SEQUENCE: u32 = 0x0000_0001; +pub const VA_ENC_PACKED_HEADER_PICTURE: u32 = 0x0000_0002; +pub const VA_ENC_PACKED_HEADER_SLICE: u32 = 0x0000_0004; +pub const VA_ENC_PACKED_HEADER_MISC: u32 = 0x0000_0008; +pub const VA_ENC_PACKED_HEADER_RAW_DATA: u32 = 0x0000_0010; + +// Rate control modes (`va.h:1108-1164`). +pub const VA_RC_NONE: u32 = 0x0000_0001; +pub const VA_RC_CBR: u32 = 0x0000_0002; +pub const VA_RC_VBR: u32 = 0x0000_0004; +pub const VA_RC_CQP: u32 = 0x0000_0010; + /// `vaCreateContext` flag: sequence contains only progressive frames (`va.h:1919`). pub const VA_PROGRESSIVE: c_int = 0x1; @@ -480,6 +495,20 @@ unsafe extern "C" { ) -> VAStatus; /// `va.h:4888`. Zero-copy view of a surface, when the driver allows it. pub fn vaDeriveImage(dpy: VADisplay, surface: VASurfaceID, image: *mut VAImage) -> VAStatus; + /// `va.h:4860`. Copies an image into a surface — the encoder upload path. + pub fn vaPutImage( + dpy: VADisplay, + surface: VASurfaceID, + image: VAImageID, + src_x: c_int, + src_y: c_int, + src_width: c_uint, + src_height: c_uint, + dest_x: c_int, + dest_y: c_int, + dest_width: c_uint, + dest_height: c_uint, + ) -> VAStatus; } #[link(name = "va-drm")]