//! `#[repr(C)]` transcriptions of the libva codec parameter structs. //! //! `ec-va` deliberately stops at the generic VA surface (display, config, //! context, buffer, picture); the per-codec buffers live here because they are //! what a *decoder* is, field for field. Every struct is transcribed by hand //! from the system headers, in header order, and every one of them is checked //! against `crates/ec-hw/abi-probe.c` output by the `const` assertion block at //! the end of its module. A transcription slip is therefore a compile error, //! not a driver reading garbage. //! //! Reference output of the probe (libva 1.23.0, x86_64 System V) is pasted //! above each assertion block. Rerun it after a libva upgrade: //! //! ```text //! cc -o /tmp/abi-probe crates/ec-hw/abi-probe.c $(pkg-config --cflags libva) //! ``` //! //! # Bit fields //! //! The C headers pack flags into `union { struct { uint32_t a : 2; ... } bits; //! uint32_t value; }`. Rust has no bit fields, so each union becomes a //! `#[repr(transparent)]` newtype over its storage integer with one chained //! setter per flag, generated by [`va_bits`]. The System V ABI allocates //! bit fields LSB first within the storage unit, which is what the generated //! shifts assume — the one ABI assumption in this module that the probe cannot //! see, and the reason every generated setter is exercised by a unit test. use std::sync::Arc; use ec_va::sys::{VABufferType, VASurfaceID}; use ec_va::{Buffer, Context}; use crate::error::Result; pub mod av1; pub mod enc; pub mod h264; pub mod hevc; pub mod vp9; /// A plain-old-data parameter struct libva reads verbatim. /// /// # Safety /// /// The implementing type must be a `#[repr(C)]` transcription of exactly the /// libva struct that belongs to [`VaParam::TYPE`], with the same size and field /// offsets (which is what the `const` assertions in this module check), and /// must be valid for any bit pattern it is constructed with — the driver reads /// `size_of::()` bytes of it, padding included. pub unsafe trait VaParam: Copy { /// The `VABufferType` this struct is submitted as. const TYPE: VABufferType; } /// Create a VA buffer holding one parameter struct. /// /// This is the *only* place `ec-hw` hands a Rust value to the driver as bytes; /// the [`VaParam`] bound is what makes it sound, and every implementation of /// that trait sits next to the transcription it vouches for. pub fn param_buffer(context: &Arc, param: &T) -> Result { // SAFETY: `T: VaParam` guarantees `T` is the `#[repr(C)]` transcription of // the libva parameter struct for `T::TYPE`, and the layout assertions in // this module check that guarantee at compile time against the system // headers. libva copies the bytes during the call. Ok(unsafe { Buffer::from_param(context, T::TYPE, param) }?) } /// `VA_INVALID_SURFACE`, spelled out where a parameter struct means "no picture". pub const INVALID_SURFACE: VASurfaceID = ec_va::sys::VA_INVALID_SURFACE; /// Generate a `#[repr(transparent)]` newtype standing in for a C bit field union. /// /// Fields are listed in header order with their widths; the generated setters /// place them LSB first, which is how the System V ABI allocates `uint32_t` /// bit fields. Each setter masks its value, so an out-of-range argument cannot /// corrupt a neighbouring field. #[macro_export] macro_rules! va_bits { ( $(#[$meta:meta])* $name:ident : $storage:ty { $( $field:ident : $width:expr ),* $(,)? } ) => { $(#[$meta])* #[repr(transparent)] #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] pub struct $name(pub $storage); impl $name { $crate::va_bits!(@fields $name, $storage, 0, $( $field : $width, )*); /// The packed value, as the C union's `value` member. pub fn value(self) -> $storage { self.0 } } }; (@fields $name:ident, $storage:ty, $offset:expr,) => {}; (@fields $name:ident, $storage:ty, $offset:expr, $field:ident : $width:expr, $($rest:tt)*) => { #[doc = concat!("Set `", stringify!($field), "` (", stringify!($width), " bit(s)).")] #[must_use] pub fn $field(mut self, value: $storage) -> Self { let mask: $storage = ((1 as $storage) << $width) - 1; self.0 &= !(mask << $offset); self.0 |= (value & mask) << $offset; self } $crate::va_bits!(@fields $name, $storage, $offset + $width, $($rest)*); }; } #[cfg(test)] mod tests { va_bits! { /// Three fields spanning a byte boundary, to pin the LSB-first order. Probe: u32 { a: 2, b: 1, c: 4, d: 8 } } #[test] fn bit_fields_pack_least_significant_first() { let v = Probe::default().a(3).b(1).c(0xf).d(0xff); assert_eq!(v.value(), 0x7fff); // Each setter is independent and masks its argument. let v = Probe::default().c(0xff); assert_eq!(v.value(), 0b111_1000); let v = Probe::default().d(1).c(0).a(1); assert_eq!(v.value(), (1 << 7) | 1); } #[test] fn setters_overwrite_rather_than_or() { let v = Probe::default().a(3).a(1); assert_eq!(v.value(), 1); } }