forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB8dbd683Tahinli3 months ago856 commits

mod.rs134 lines, 5.2 KiBHistoryRawDownload
1//! `#[repr(C)]` transcriptions of the libva codec parameter structs.
2//!
3//! `ec-va` deliberately stops at the generic VA surface (display, config,
4//! context, buffer, picture); the per-codec buffers live here because they are
5//! what a *decoder* is, field for field. Every struct is transcribed by hand
6//! from the system headers, in header order, and every one of them is checked
7//! against `crates/ec-hw/abi-probe.c` output by the `const` assertion block at
8//! the end of its module. A transcription slip is therefore a compile error,
9//! not a driver reading garbage.
10//!
11//! Reference output of the probe (libva 1.23.0, x86_64 System V) is pasted
12//! above each assertion block. Rerun it after a libva upgrade:
13//!
14//! ```text
15//! cc -o /tmp/abi-probe crates/ec-hw/abi-probe.c $(pkg-config --cflags libva)
16//! ```
17//!
18//! # Bit fields
19//!
20//! The C headers pack flags into `union { struct { uint32_t a : 2; ... } bits;
21//! uint32_t value; }`. Rust has no bit fields, so each union becomes a
22//! `#[repr(transparent)]` newtype over its storage integer with one chained
23//! setter per flag, generated by [`va_bits`]. The System V ABI allocates
24//! bit fields LSB first within the storage unit, which is what the generated
25//! shifts assume — the one ABI assumption in this module that the probe cannot
26//! see, and the reason every generated setter is exercised by a unit test.
27
28use std::sync::Arc;
29
30use ec_va::sys::{VABufferType, VASurfaceID};
31use ec_va::{Buffer, Context};
32
33use crate::error::Result;
34
35pub mod av1;
36pub mod enc;
37pub mod h264;
38pub mod hevc;
39pub mod vp9;
40
41/// A plain-old-data parameter struct libva reads verbatim.
42///
43/// # Safety
44///
45/// The implementing type must be a `#[repr(C)]` transcription of exactly the
46/// libva struct that belongs to [`VaParam::TYPE`], with the same size and field
47/// offsets (which is what the `const` assertions in this module check), and
48/// must be valid for any bit pattern it is constructed with — the driver reads
49/// `size_of::<Self>()` bytes of it, padding included.
50pub unsafe trait VaParam: Copy {
51 /// The `VABufferType` this struct is submitted as.
52 const TYPE: VABufferType;
53}
54
55/// Create a VA buffer holding one parameter struct.
56///
57/// This is the *only* place `ec-hw` hands a Rust value to the driver as bytes;
58/// the [`VaParam`] bound is what makes it sound, and every implementation of
59/// that trait sits next to the transcription it vouches for.
60pub fn param_buffer<T: VaParam>(context: &Arc<Context>, param: &T) -> Result<Buffer> {
61 // SAFETY: `T: VaParam` guarantees `T` is the `#[repr(C)]` transcription of
62 // the libva parameter struct for `T::TYPE`, and the layout assertions in
63 // this module check that guarantee at compile time against the system
64 // headers. libva copies the bytes during the call.
65 Ok(unsafe { Buffer::from_param(context, T::TYPE, param) }?)
66}
67
68/// `VA_INVALID_SURFACE`, spelled out where a parameter struct means "no picture".
69pub const INVALID_SURFACE: VASurfaceID = ec_va::sys::VA_INVALID_SURFACE;
70
71/// Generate a `#[repr(transparent)]` newtype standing in for a C bit field union.
72///
73/// Fields are listed in header order with their widths; the generated setters
74/// place them LSB first, which is how the System V ABI allocates `uint32_t`
75/// bit fields. Each setter masks its value, so an out-of-range argument cannot
76/// corrupt a neighbouring field.
77#[macro_export]
78macro_rules! va_bits {
79 (
80 $(#[$meta:meta])*
81 $name:ident : $storage:ty { $( $field:ident : $width:expr ),* $(,)? }
82 ) => {
83 $(#[$meta])*
84 #[repr(transparent)]
85 #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
86 pub struct $name(pub $storage);
87
88 impl $name {
89 $crate::va_bits!(@fields $name, $storage, 0, $( $field : $width, )*);
90
91 /// The packed value, as the C union's `value` member.
92 pub fn value(self) -> $storage {
93 self.0
94 }
95 }
96 };
97 (@fields $name:ident, $storage:ty, $offset:expr,) => {};
98 (@fields $name:ident, $storage:ty, $offset:expr, $field:ident : $width:expr, $($rest:tt)*) => {
99 #[doc = concat!("Set `", stringify!($field), "` (", stringify!($width), " bit(s)).")]
100 #[must_use]
101 pub fn $field(mut self, value: $storage) -> Self {
102 let mask: $storage = ((1 as $storage) << $width) - 1;
103 self.0 &= !(mask << $offset);
104 self.0 |= (value & mask) << $offset;
105 self
106 }
107 $crate::va_bits!(@fields $name, $storage, $offset + $width, $($rest)*);
108 };
109}
110
111#[cfg(test)]
112mod tests {
113 va_bits! {
114 /// Three fields spanning a byte boundary, to pin the LSB-first order.
115 Probe: u32 { a: 2, b: 1, c: 4, d: 8 }
116 }
117
118 #[test]
119 fn bit_fields_pack_least_significant_first() {
120 let v = Probe::default().a(3).b(1).c(0xf).d(0xff);
121 assert_eq!(v.value(), 0x7fff);
122 // Each setter is independent and masks its argument.
123 let v = Probe::default().c(0xff);
124 assert_eq!(v.value(), 0b111_1000);
125 let v = Probe::default().d(1).c(0).a(1);
126 assert_eq!(v.value(), (1 << 7) | 1);
127 }
128
129 #[test]
130 fn setters_overwrite_rather_than_or() {
131 let v = Probe::default().a(3).a(1);
132 assert_eq!(v.value(), 1);
133 }
134}