edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB98eeb6cTahinli3 months ago90 commits
| 1 | //! Hand-written FFI for the subset of libva this family uses. |
| 2 | //! |
| 3 | //! # Why hand-written |
| 4 | //! |
| 5 | //! No `bindgen`: the family rule is no build-time tooling, and a generated |
| 6 | //! 40k-line binding hides exactly the thing that has to be audited — the ABI |
| 7 | //! assumptions. Everything here is transcribed from the system headers and is |
| 8 | //! short enough to read in one sitting. |
| 9 | //! |
| 10 | //! # ABI assumptions (all four are load-bearing) |
| 11 | //! |
| 12 | //! 1. **libva 1.23.0 headers**, `/usr/include/va/{va.h,va_drm.h,va_drmcommon.h}`, |
| 13 | //! target `x86_64-unknown-linux-gnu`, System V C ABI. |
| 14 | //! 2. **C `enum` is `int` (4 bytes)** on this ABI. Every VA enum is transcribed |
| 15 | //! as a plain `i32` type alias plus constants, *never* as a `#[repr(i32)]` |
| 16 | //! Rust enum: the driver writes profile/entrypoint values into buffers we |
| 17 | //! own, and a value we have no variant for would be instant UB on a real |
| 18 | //! Rust enum. Interpretation happens in the safe layer |
| 19 | //! ([`crate::caps::Profile::from_raw`]), which has an `Unknown(i32)` arm. |
| 20 | //! This is the header-drift trap that broke cros-libva at libva >= 1.23. |
| 21 | //! 3. **Struct layouts** below are checked by the `const _: () = assert!(...)` |
| 22 | //! block at the end of this file against numbers printed by |
| 23 | //! `crates/ec-va/abi-probe.c` compiled against the *system* headers. Rerun |
| 24 | //! that probe after a libva upgrade; a mismatch means this file drifted. |
| 25 | //! 4. **`VADisplay` is an opaque pointer**; we never dereference it. |
| 26 | //! |
| 27 | //! Points 1-3 are compile-time. They cannot see a libva that changes layout |
| 28 | //! *after* this crate is built, so [`crate::Display::open`] additionally |
| 29 | //! performs a **runtime** `vaInitialize` version check and refuses anything |
| 30 | //! older than 1.23 with a typed error. |
| 31 | |
| 32 | // Every item keeps its exact C spelling so that this file can be diffed |
| 33 | // against the headers by eye — the whole audit story of a hand-written FFI. |
| 34 | |
| 35 | |
| 36 | |
| 37 | // Fields keep their C names and are documented by the header line cited on |
| 38 | // each item; restating "the fourcc field is the fourcc" 60 times would bury |
| 39 | // the ABI notes that actually matter here. |
| 40 | |
| 41 | |
| 42 | use ; |
| 43 | |
| 44 | // --------------------------------------------------------------------------- |
| 45 | // Scalar types (va.h:260-262, 1580-1647, 2042, 4727) |
| 46 | // --------------------------------------------------------------------------- |
| 47 | |
| 48 | /// Opaque display handle. Never dereferenced by this crate. |
| 49 | pub type VADisplay = *mut c_void; |
| 50 | /// Return status of every VA entry point. `va.h:262`. |
| 51 | pub type VAStatus = i32; |
| 52 | /// `va.h:1580`. |
| 53 | pub type VAGenericID = c_uint; |
| 54 | pub type VAConfigID = VAGenericID; |
| 55 | pub type VAContextID = VAGenericID; |
| 56 | pub type VASurfaceID = VAGenericID; |
| 57 | pub type VABufferID = VAGenericID; |
| 58 | pub type VAImageID = VAGenericID; |
| 59 | |
| 60 | /// C enum `VAProfile` (`va.h:502`), kept as `i32`. See ABI assumption 2. |
| 61 | pub type VAProfile = i32; |
| 62 | /// C enum `VAEntrypoint` (`va.h:552`), kept as `i32`. |
| 63 | pub type VAEntrypoint = i32; |
| 64 | /// C enum `VAConfigAttribType` (`va.h:619`), kept as `i32`. |
| 65 | pub type VAConfigAttribType = i32; |
| 66 | /// C enum `VASurfaceAttribType` (`va.h:1689`), kept as `i32`. |
| 67 | pub type VASurfaceAttribType = i32; |
| 68 | /// C enum `VAGenericValueType` (`va.h:1651`), kept as `i32`. |
| 69 | pub type VAGenericValueType = i32; |
| 70 | /// C enum `VABufferType` (`va.h:2044`), kept as `i32`. |
| 71 | pub type VABufferType = i32; |
| 72 | |
| 73 | pub const VA_INVALID_ID: VAGenericID = 0xffff_ffff; |
| 74 | pub const VA_INVALID_SURFACE: VASurfaceID = VA_INVALID_ID; |
| 75 | |
| 76 | // Status codes actually branched on; the rest are reported via `vaErrorStr`. |
| 77 | // `va.h:264-309`. |
| 78 | pub const VA_STATUS_SUCCESS: VAStatus = 0x0000_0000; |
| 79 | pub const VA_STATUS_ERROR_INVALID_CONTEXT: VAStatus = 0x0000_0005; |
| 80 | pub const VA_STATUS_ERROR_UNSUPPORTED_PROFILE: VAStatus = 0x0000_000c; |
| 81 | pub const VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT: VAStatus = 0x0000_000d; |
| 82 | pub const VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT: VAStatus = 0x0000_000e; |
| 83 | pub const VA_STATUS_ERROR_MAX_NUM_EXCEEDED: VAStatus = 0x0000_000b; |
| 84 | pub const VA_STATUS_ERROR_UNIMPLEMENTED: VAStatus = 0x0000_0014; |
| 85 | pub const VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED: VAStatus = 0x0000_0013; |
| 86 | pub const VA_STATUS_ERROR_OPERATION_FAILED: VAStatus = 0x0000_0001; |
| 87 | pub const VA_STATUS_ERROR_INVALID_PARAMETER: VAStatus = 0x0000_0012; |
| 88 | |
| 89 | // Entrypoints (`va.h:552`). |
| 90 | pub const VAEntrypointVLD: VAEntrypoint = 1; |
| 91 | pub const VAEntrypointEncSlice: VAEntrypoint = 6; |
| 92 | pub const VAEntrypointEncPicture: VAEntrypoint = 7; |
| 93 | pub const VAEntrypointEncSliceLP: VAEntrypoint = 8; |
| 94 | pub const VAEntrypointVideoProc: VAEntrypoint = 10; |
| 95 | |
| 96 | // Config attribute types (`va.h:619`). |
| 97 | pub const VAConfigAttribRTFormat: VAConfigAttribType = 0; |
| 98 | pub const VAConfigAttribRateControl: VAConfigAttribType = 5; |
| 99 | pub const VAConfigAttribDecSliceMode: VAConfigAttribType = 6; |
| 100 | pub const VAConfigAttribEncPackedHeaders: VAConfigAttribType = 10; |
| 101 | |
| 102 | // RT formats (`va.h:1073-1090`). |
| 103 | pub const VA_RT_FORMAT_YUV420: u32 = 0x0000_0001; |
| 104 | pub const VA_RT_FORMAT_YUV422: u32 = 0x0000_0002; |
| 105 | pub const VA_RT_FORMAT_YUV444: u32 = 0x0000_0004; |
| 106 | pub const VA_RT_FORMAT_YUV400: u32 = 0x0000_0010; |
| 107 | pub const VA_RT_FORMAT_YUV420_10: u32 = 0x0000_0100; |
| 108 | pub const VA_RT_FORMAT_YUV422_10: u32 = 0x0000_0200; |
| 109 | pub const VA_RT_FORMAT_YUV444_10: u32 = 0x0000_0400; |
| 110 | pub const VA_RT_FORMAT_YUV420_12: u32 = 0x0000_1000; |
| 111 | pub const VA_RT_FORMAT_RGB32: u32 = 0x0002_0000; |
| 112 | |
| 113 | // Surface attribute types (`va.h:1689`) and flags (`va.h:1681-1684`). |
| 114 | pub const VASurfaceAttribNone: VASurfaceAttribType = 0; |
| 115 | pub const VASurfaceAttribPixelFormat: VASurfaceAttribType = 1; |
| 116 | pub const VASurfaceAttribMinWidth: VASurfaceAttribType = 2; |
| 117 | pub const VASurfaceAttribMaxWidth: VASurfaceAttribType = 3; |
| 118 | pub const VASurfaceAttribMinHeight: VASurfaceAttribType = 4; |
| 119 | pub const VASurfaceAttribMaxHeight: VASurfaceAttribType = 5; |
| 120 | pub const VASurfaceAttribMemoryType: VASurfaceAttribType = 6; |
| 121 | pub const VASurfaceAttribUsageHint: VASurfaceAttribType = 8; |
| 122 | |
| 123 | pub const VA_SURFACE_ATTRIB_GETTABLE: u32 = 0x0000_0001; |
| 124 | pub const VA_SURFACE_ATTRIB_SETTABLE: u32 = 0x0000_0002; |
| 125 | |
| 126 | pub const VA_SURFACE_ATTRIB_USAGE_HINT_GENERIC: u32 = 0x0000_0000; |
| 127 | pub const VA_SURFACE_ATTRIB_USAGE_HINT_DECODER: u32 = 0x0000_0001; |
| 128 | pub const VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER: u32 = 0x0000_0002; |
| 129 | pub const VA_SURFACE_ATTRIB_USAGE_HINT_EXPORT: u32 = 0x0000_0020; |
| 130 | |
| 131 | // Generic value discriminants (`va.h:1651`). |
| 132 | pub const VAGenericValueTypeInteger: VAGenericValueType = 1; |
| 133 | pub const VAGenericValueTypeFloat: VAGenericValueType = 2; |
| 134 | pub const VAGenericValueTypePointer: VAGenericValueType = 3; |
| 135 | pub const VAGenericValueTypeFunc: VAGenericValueType = 4; |
| 136 | |
| 137 | // Buffer types actually used by decode/encode paths (`va.h:2044`). |
| 138 | pub const VAPictureParameterBufferType: VABufferType = 0; |
| 139 | pub const VAIQMatrixBufferType: VABufferType = 1; |
| 140 | pub const VASliceParameterBufferType: VABufferType = 4; |
| 141 | pub const VASliceDataBufferType: VABufferType = 5; |
| 142 | pub const VAImageBufferType: VABufferType = 9; |
| 143 | pub const VAProbabilityBufferType: VABufferType = 13; |
| 144 | pub const VAEncCodedBufferType: VABufferType = 21; |
| 145 | pub const VAEncSequenceParameterBufferType: VABufferType = 22; |
| 146 | pub const VAEncPictureParameterBufferType: VABufferType = 23; |
| 147 | pub const VAEncSliceParameterBufferType: VABufferType = 24; |
| 148 | pub const VAEncPackedHeaderParameterBufferType: VABufferType = 25; |
| 149 | pub const VAEncPackedHeaderDataBufferType: VABufferType = 26; |
| 150 | pub const VAEncMiscParameterBufferType: VABufferType = 27; |
| 151 | |
| 152 | /// `vaCreateContext` flag: sequence contains only progressive frames (`va.h:1919`). |
| 153 | pub const VA_PROGRESSIVE: c_int = 0x1; |
| 154 | |
| 155 | // DRM PRIME memory types and export flags (`va_drmcommon.h:78-94`, `va.h:4130-4150`). |
| 156 | pub const VA_SURFACE_ATTRIB_MEM_TYPE_VA: u32 = 0x0000_0001; |
| 157 | pub const VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM: u32 = 0x1000_0000; |
| 158 | pub const VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME: u32 = 0x2000_0000; |
| 159 | pub const VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2: u32 = 0x4000_0000; |
| 160 | |
| 161 | pub const VA_EXPORT_SURFACE_READ_ONLY: u32 = 0x0001; |
| 162 | pub const VA_EXPORT_SURFACE_WRITE_ONLY: u32 = 0x0002; |
| 163 | pub const VA_EXPORT_SURFACE_READ_WRITE: u32 = 0x0003; |
| 164 | pub const VA_EXPORT_SURFACE_SEPARATE_LAYERS: u32 = 0x0004; |
| 165 | pub const VA_EXPORT_SURFACE_COMPOSED_LAYERS: u32 = 0x0008; |
| 166 | |
| 167 | // FourCCs used by the family (`va.h:4400-4700`). |
| 168 | pub const VA_FOURCC_NV12: u32 = 0x3231_564e; |
| 169 | pub const VA_FOURCC_P010: u32 = 0x3031_3050; |
| 170 | pub const VA_FOURCC_I420: u32 = 0x3032_3449; |
| 171 | pub const VA_FOURCC_YV12: u32 = 0x3231_5659; |
| 172 | |
| 173 | // --------------------------------------------------------------------------- |
| 174 | // Structs |
| 175 | // --------------------------------------------------------------------------- |
| 176 | |
| 177 | /// `VAConfigAttrib`, `va.h:1155`. |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | /// The `value` union of `VAGenericValue`, `va.h:1663-1673`. |
| 186 | /// |
| 187 | /// Only ever read after checking the sibling `type_` discriminant. |
| 188 | |
| 189 | |
| 190 | pub union VAGenericValueUnion |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | /// `VAGenericValue`, `va.h:1661`. |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | /// `VASurfaceAttrib`, `va.h:1741`. |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | /// `VAImageFormat`, `va.h:4712`. `va_reserved` is `VA_PADDING_LOW` = 4 u32s. |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | /// `VAImage`, `va.h:4729`. |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | /// One DRM object of a `VADRMPRIMESurfaceDescriptor`, `va_drmcommon.h:139-147`. |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | /// One layer of a `VADRMPRIMESurfaceDescriptor`, `va_drmcommon.h:151-164`. |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | /// `VADRMPRIMESurfaceDescriptor`, `va_drmcommon.h:130`. Used with |
| 279 | /// [`VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2`]. |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | /// `VARectangle`, `va.h:406`. |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | // --------------------------------------------------------------------------- |
| 312 | // Entry points |
| 313 | // --------------------------------------------------------------------------- |
| 314 | |
| 315 | |
| 316 | unsafe extern "C" |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | unsafe extern "C" |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | // --------------------------------------------------------------------------- |
| 492 | // ABI transcription check |
| 493 | // --------------------------------------------------------------------------- |
| 494 | // |
| 495 | // Reference output of `crates/ec-va/abi-probe.c` compiled against |
| 496 | // /usr/include/va (libva 1.23.0) with gcc on x86_64: |
| 497 | // |
| 498 | // VAConfigAttrib size=8 align=4 type=0 value=4 |
| 499 | // VAGenericValue size=16 align=8 type=0 value=8 |
| 500 | // VASurfaceAttrib size=24 align=8 type=0 flags=4 value=8 |
| 501 | // VAImageFormat size=48 align=4 fourcc=0 .. va_reserved=32 |
| 502 | // VAImage size=120 align=4 image_id=0 format=4 buf=52 |
| 503 | // width=56 height=58 data_size=60 |
| 504 | // num_planes=64 pitches=68 offsets=80 |
| 505 | // num_palette_entries=92 entry_bytes=96 |
| 506 | // component_order=100 va_reserved=104 |
| 507 | // VADRMPRIMESurfaceDescriptor size=312 align=8 fourcc=0 width=4 height=8 |
| 508 | // num_objects=12 objects=16 |
| 509 | // num_layers=80 layers=84 |
| 510 | // object elem size=16, layer elem size=56 |
| 511 | // VARectangle size=8 align=2 |
| 512 | const _: = |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | ; |