⚡ engine-hw on ec_hw::HwDecoder/HwEncoder (Vulkan Video first, VA-API fallback): 4K HEVC Main10 hw export 32.9 -> 153 fps, decode bit-exact; per-picture readback or DMA-BUF export; capabilities from ec_hw
Browse files at this commitff99ad61bd8ccdc89c71277150f10d4ee298234aParent: 527b49c7 changed files +185 -288
- Cargo.lock +36 -1
- crates/engine-hw/Cargo.toml +0 -2
- crates/engine-hw/src/child.rs +1 -1
- crates/engine-hw/src/enc_ec.rs +64 -129
- crates/engine-hw/src/lib.rs +81 -152
- crates/engine/src/export.rs +1 -1
- crates/mcp/src/tools/export.rs +2 -2
Cargo.lockmodified+36 -1
| @@ -1665,11 +1665,14 @@ dependencies = [ | ||
| 1665 | 1665 | name = "ec-hw" |
| 1666 | 1666 | version = "0.1.0" |
| 1667 | 1667 | dependencies = [ |
| 1668 | + "ec-av1", | |
| 1668 | 1669 | "ec-av1-syntax", |
| 1669 | 1670 | "ec-core", |
| 1671 | + "ec-dsp", | |
| 1670 | 1672 | "ec-h264-syntax", |
| 1671 | 1673 | "ec-h265-syntax", |
| 1672 | 1674 | "ec-va", |
| 1675 | + "ec-vk", | |
| 1673 | 1676 | "ec-vp9-syntax", |
| 1674 | 1677 | ] |
| 1675 | 1678 | |
| @@ -1836,6 +1839,13 @@ dependencies = [ | ||
| 1836 | 1839 | "cros-libva", |
| 1837 | 1840 | ] |
| 1838 | 1841 | |
| 1842 | +[[package]] | |
| 1843 | +name = "ec-vk" | |
| 1844 | +version = "0.1.0" | |
| 1845 | +dependencies = [ | |
| 1846 | + "vk-video-ffi", | |
| 1847 | +] | |
| 1848 | + | |
| 1839 | 1849 | [[package]] |
| 1840 | 1850 | name = "ec-vorbis" |
| 1841 | 1851 | version = "0.1.0" |
| @@ -1989,7 +1999,6 @@ version = "0.1.0" | ||
| 1989 | 1999 | dependencies = [ |
| 1990 | 2000 | "bytemuck", |
| 1991 | 2001 | "ec-hw", |
| 1992 | - "ec-va", | |
| 1993 | 2002 | "ec-vp8", |
| 1994 | 2003 | "engine", |
| 1995 | 2004 | ] |
| @@ -6698,6 +6707,13 @@ version = "0.9.5" | ||
| 6698 | 6707 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 6699 | 6708 | checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" |
| 6700 | 6709 | |
| 6710 | +[[package]] | |
| 6711 | +name = "vk-video-ffi" | |
| 6712 | +version = "0.1.0" | |
| 6713 | +dependencies = [ | |
| 6714 | + "vulkanalia", | |
| 6715 | +] | |
| 6716 | + | |
| 6701 | 6717 | [[package]] |
| 6702 | 6718 | name = "vswhom" |
| 6703 | 6719 | version = "0.1.0" |
| @@ -6718,6 +6734,25 @@ dependencies = [ | ||
| 6718 | 6734 | "libc", |
| 6719 | 6735 | ] |
| 6720 | 6736 | |
| 6737 | +[[package]] | |
| 6738 | +name = "vulkanalia" | |
| 6739 | +version = "0.35.0" | |
| 6740 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 6741 | +checksum = "ebd71d6c64383bbdf1c260efa22e81e1a2a7dd832a3831af34495f47d7551d7b" | |
| 6742 | +dependencies = [ | |
| 6743 | + "libloading", | |
| 6744 | + "vulkanalia-sys", | |
| 6745 | +] | |
| 6746 | + | |
| 6747 | +[[package]] | |
| 6748 | +name = "vulkanalia-sys" | |
| 6749 | +version = "0.35.0" | |
| 6750 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 6751 | +checksum = "08d4da5d5e0c7377cea24358fa238b3b22a1913a7890f65dbd8ff9aa371a493a" | |
| 6752 | +dependencies = [ | |
| 6753 | + "bitflags 1.3.2", | |
| 6754 | +] | |
| 6755 | + | |
| 6721 | 6756 | [[package]] |
| 6722 | 6757 | name = "waker-fn" |
| 6723 | 6758 | version = "1.2.0" |
crates/engine-hw/Cargo.tomlmodified+0 -2
| @@ -18,8 +18,6 @@ path = "src/bin/hw-child.rs" | ||
| 18 | 18 | [dependencies] |
| 19 | 19 | bytemuck = "1.25" |
| 20 | 20 | engine = { path = "../engine" } |
| 21 | -# The decode seat: ec-hw drives the VA-API decoders, ec-va the display under it. | |
| 22 | 21 | ec-hw = { path = "../../../edith_codecs/crates/ec-hw" } |
| 23 | -ec-va = { path = "../../../edith_codecs/crates/ec-va" } | |
| 24 | 22 | # The software VP8 seat, this project's own (`ec-vp8`, RFC 6386). |
| 25 | 23 | ec-vp8 = { path = "../../../edith_codecs/crates/ec-vp8" } |
crates/engine-hw/src/child.rsmodified+1 -1
| @@ -491,7 +491,7 @@ enum Seat { | ||
| 491 | 491 | impl Seat { |
| 492 | 492 | fn dma_geometry(&self) -> Option<(u32, u32)> { |
| 493 | 493 | match self { |
| 494 | - Self::Real(encoder) => Some(encoder.coded_size()), | |
| 494 | + Self::Real(encoder) => encoder.coded_size(), | |
| 495 | 495 | // Nothing to import into, so the parent stages pictures instead. |
| 496 | 496 | Self::Fake => None, |
| 497 | 497 | } |
crates/engine-hw/src/enc_ec.rsmodified+64 -129
| @@ -1,21 +1,20 @@ | ||
| 1 | -//! The encode seats, on [`ec_hw::Encoder`]: H.264 (Main profile), HEVC and AV1. | |
| 1 | +//! The encode seats, on [`ec_hw::HwEncoder`]: H.264 (Main profile), HEVC and AV1. | |
| 2 | 2 | //! |
| 3 | -//! A picture reaches the encoder one of two ways. Pixels the caller holds are | |
| 4 | -//! packed I420 and go through [`ec_hw::Encoder::encode`], which uploads them | |
| 5 | -//! into a pooled surface. A picture the decoder left on the GPU arrives as a | |
| 6 | -//! DRM-PRIME descriptor ([`DmaFrame`]); its file descriptors are duplicated into | |
| 7 | -//! an [`ec_va::PrimeSurface`], imported with [`ec_va::import_prime`] | |
| 8 | -//! and read in place by [`ec_hw::Encoder::encode_surface`], so no copy is made. | |
| 3 | +//! Pixels the caller holds are packed I420 and go through [`ec_hw::EncodeInput::I420`] | |
| 4 | +//! (or `I420_16` for 10-bit). A picture the decoder left on the GPU arrives as a | |
| 5 | +//! DRM-PRIME descriptor ([`DmaFrame`]) and goes through [`ec_hw::EncodeInput::DmaBufYuv`], | |
| 6 | +//! which the encoder imports and caches itself. The backend, Vulkan or VA-API, is the | |
| 7 | +//! one [`ec_hw::HwEncoder::open`] picks. | |
| 9 | 8 | |
| 10 | 9 | use std::collections::VecDeque; |
| 11 | -use std::os::fd::{AsFd, OwnedFd}; | |
| 12 | -use std::rc::Rc; | |
| 10 | +use std::os::fd::{AsFd, BorrowedFd}; | |
| 13 | 11 | |
| 14 | -use ec_hw::enc::{EncCodec, EncoderConfig, FrameMetadata, RateControlMode}; | |
| 15 | -use ec_hw::{Encoder, H264Profile, I420, I420_16}; | |
| 16 | -use ec_va::{PrimeLayer, PrimeObject, PrimeSurface}; | |
| 12 | +use ec_hw::{ | |
| 13 | + Backend, DmaBufFormat, DmaBufPlane, DmaBufYuvFrame, EncCodec, EncodeInput, EncoderConfig, | |
| 14 | + FrameMetadata, H264Profile, HwEncoder, I420, I420_16, RateControlMode, | |
| 15 | +}; | |
| 17 | 16 | |
| 18 | -use engine::hw::{DmaFrame, VhDma}; | |
| 17 | +use engine::hw::DmaFrame; | |
| 19 | 18 | use engine::hwproc::SeatRate; |
| 20 | 19 | |
| 21 | 20 | /// `DRM_FORMAT_NV12`, which is also `VA_FOURCC_NV12`: the four characters. |
| @@ -25,30 +24,18 @@ const FOURCC_P010: u32 = u32::from_le_bytes(*b"P010"); | ||
| 25 | 24 | |
| 26 | 25 | /// One hardware encode session on `ec_hw`. |
| 27 | 26 | pub(crate) struct EcEncSession { |
| 28 | - display: Rc<ec_va::Display>, | |
| 29 | - encoder: Encoder, | |
| 27 | + encoder: HwEncoder, | |
| 30 | 28 | /// Display size: what is encoded. |
| 31 | 29 | width: usize, |
| 32 | 30 | height: usize, |
| 33 | - /// Macroblock- or block-aligned size the driver codes at. | |
| 34 | - coded: (u32, u32), | |
| 35 | 31 | timestamp: i64, |
| 36 | 32 | /// Access units the encoder has finished but the caller has not taken yet, |
| 37 | 33 | /// in encode order (which is display order: no B-frames). |
| 38 | 34 | ready: VecDeque<Vec<u8>>, |
| 39 | 35 | /// The access unit currently handed out, kept alive until the next call. |
| 40 | 36 | out: Vec<u8>, |
| 41 | - ten_bit: bool, | |
| 42 | - /// Imported decoder buffers, by the dma-buf inodes and layout they came as: | |
| 43 | - /// the decoder hands the same few surfaces out again and again, and a fresh | |
| 44 | - /// import costs the GPU a new mapping on its first use. | |
| 45 | - imports: VecDeque<(ImportKey, Rc<ec_va::va::Surface<ec_va::surface::PrimeImport>>)>, | |
| 46 | 37 | } |
| 47 | 38 | |
| 48 | -type ImportKey = (Vec<u64>, VhDma); | |
| 49 | - | |
| 50 | -const IMPORTS_MAX: usize = 32; | |
| 51 | - | |
| 52 | 39 | /// The `open` arguments, for any of the three codecs. |
| 53 | 40 | pub(crate) struct EcOpen { |
| 54 | 41 | pub codec: EncCodec, |
| @@ -63,7 +50,7 @@ pub(crate) struct EcOpen { | ||
| 63 | 50 | pub gop: Option<u32>, |
| 64 | 51 | /// Colour description for the VUI as `(primaries, transfer, matrix, full)`. |
| 65 | 52 | pub colour: Option<(u8, u8, u8, bool)>, |
| 66 | - /// HEVC Main10: pictures go through [`EcEncSession::encode16`]. | |
| 53 | + /// HEVC Main10: pictures go through [`EcEncSession::encode16_owned`]. | |
| 67 | 54 | pub ten_bit: bool, |
| 68 | 55 | } |
| 69 | 56 | |
| @@ -122,20 +109,14 @@ impl EcEncSession { | ||
| 122 | 109 | if ten_bit { |
| 123 | 110 | config = config.ten_bit(); |
| 124 | 111 | } |
| 125 | - let display = ec_va::open().ok()?; | |
| 126 | - let encoder = Encoder::new(&display, config).ok()?; | |
| 127 | - let coded = encoder.coded_size(); | |
| 112 | + let encoder = HwEncoder::open(config, None).ok()?; | |
| 128 | 113 | Some(Self { |
| 129 | - display, | |
| 130 | 114 | encoder, |
| 131 | 115 | width: width as usize, |
| 132 | 116 | height: height as usize, |
| 133 | - coded, | |
| 134 | 117 | timestamp: 0, |
| 135 | 118 | ready: VecDeque::new(), |
| 136 | - imports: VecDeque::new(), | |
| 137 | 119 | out: Vec::new(), |
| 138 | - ten_bit, | |
| 139 | 120 | }) |
| 140 | 121 | } |
| 141 | 122 | |
| @@ -163,7 +144,11 @@ impl EcEncSession { | ||
| 163 | 144 | bit_depth: 10, |
| 164 | 145 | }; |
| 165 | 146 | let meta = self.meta(force_keyframe); |
| 166 | - if let Some(coded) = self.encoder.submit_16(&picture, meta).map_err(|e| e.to_string())? { | |
| 147 | + if let Some(coded) = self | |
| 148 | + .encoder | |
| 149 | + .submit(EncodeInput::I420_16(&picture), meta) | |
| 150 | + .map_err(|e| e.to_string())? | |
| 151 | + { | |
| 167 | 152 | self.ready.push_back(coded.data); |
| 168 | 153 | } |
| 169 | 154 | Ok(()) |
| @@ -176,9 +161,13 @@ impl EcEncSession { | ||
| 176 | 161 | Ok(()) |
| 177 | 162 | } |
| 178 | 163 | |
| 179 | - /// The surface size the driver codes at, as the caller must describe it. | |
| 180 | - pub(crate) fn coded_size(&self) -> (u32, u32) { | |
| 181 | - self.coded | |
| 164 | + /// The size a DMA-BUF picture must be described at, or `None` when the | |
| 165 | + /// backend takes no DMA-BUF input (VA-API). | |
| 166 | + pub(crate) fn coded_size(&self) -> Option<(u32, u32)> { | |
| 167 | + match self.encoder.backend() { | |
| 168 | + Backend::Vk => Some((self.width as u32, self.height as u32)), | |
| 169 | + Backend::Va => None, | |
| 170 | + } | |
| 182 | 171 | } |
| 183 | 172 | |
| 184 | 173 | /// Codes one packed I420 picture (`y`, `u`, `v` at the session's size) and |
| @@ -203,7 +192,11 @@ impl EcEncSession { | ||
| 203 | 192 | height: h as u32, |
| 204 | 193 | }; |
| 205 | 194 | let meta = self.meta(force_keyframe); |
| 206 | - if let Some(coded) = self.encoder.submit(&picture, meta).map_err(|e| e.to_string())? { | |
| 195 | + if let Some(coded) = self | |
| 196 | + .encoder | |
| 197 | + .submit(EncodeInput::I420(&picture), meta) | |
| 198 | + .map_err(|e| e.to_string())? | |
| 199 | + { | |
| 207 | 200 | self.ready.push_back(coded.data); |
| 208 | 201 | } |
| 209 | 202 | Ok(()) |
| @@ -211,53 +204,41 @@ impl EcEncSession { | ||
| 211 | 204 | |
| 212 | 205 | /// Codes one picture the decoder left on the GPU, read in place. |
| 213 | 206 | /// |
| 214 | - /// The descriptors stay owned by `frame`: they are duplicated into the | |
| 215 | - /// imported surface, which owns its copies until it is dropped at the end | |
| 216 | - /// of this call. | |
| 207 | + /// The descriptors stay owned by `frame`; the encoder duplicates them on import. | |
| 217 | 208 | pub(crate) fn encode_dma(&mut self, frame: &DmaFrame, force_keyframe: bool) -> Result<(), String> { |
| 218 | - let inodes = frame | |
| 219 | - .fds | |
| 220 | - .iter() | |
| 221 | - .map(|fd| { | |
| 222 | - use std::os::unix::fs::MetadataExt; | |
| 223 | - Some(std::fs::File::from(fd.try_clone().ok()?).metadata().ok()?.ino()) | |
| 224 | - }) | |
| 225 | - .collect::<Option<Vec<u64>>>(); | |
| 226 | - let key = inodes.map(|inodes| (inodes, frame.desc)); | |
| 227 | - let cached = key | |
| 228 | - .as_ref() | |
| 229 | - .and_then(|key| self.imports.iter().find(|(k, _)| k == key)) | |
| 230 | - .map(|(_, surface)| Rc::clone(surface)); | |
| 231 | - let surface = match cached { | |
| 232 | - Some(surface) => surface, | |
| 233 | - None => { | |
| 234 | - let prime = prime_from_vhdma(&frame.desc, &frame.fds)?; | |
| 235 | - let surface = Rc::new(ec_va::import_prime(&self.display, &prime).map_err(|e| e.to_string())?); | |
| 236 | - if let Some(key) = key { | |
| 237 | - self.imports.push_back((key, Rc::clone(&surface))); | |
| 238 | - if self.imports.len() > IMPORTS_MAX { | |
| 239 | - self.imports.pop_front(); | |
| 240 | - } | |
| 241 | - } | |
| 242 | - surface | |
| 243 | - } | |
| 209 | + let format = match (frame.desc.fourcc, frame.fds.len()) { | |
| 210 | + (FOURCC_NV12, 1 | 2) => DmaBufFormat::Nv12, | |
| 211 | + (FOURCC_P010, 1 | 2) => DmaBufFormat::P010, | |
| 212 | + _ => return Err("DMA-BUF is not an NV12 or P010 picture on the GPU".into()), | |
| 213 | + }; | |
| 214 | + let fds: Vec<BorrowedFd> = frame.fds.iter().map(|fd| fd.as_fd()).collect(); | |
| 215 | + let desc = &frame.desc; | |
| 216 | + let chroma_fd = if fds.len() == 2 { 1 } else { 0 }; | |
| 217 | + let input = DmaBufYuvFrame { | |
| 218 | + fds: &fds, | |
| 219 | + planes: [ | |
| 220 | + DmaBufPlane { | |
| 221 | + fd: 0, | |
| 222 | + offset: desc.offset[0], | |
| 223 | + pitch: desc.stride[0], | |
| 224 | + }, | |
| 225 | + DmaBufPlane { | |
| 226 | + fd: chroma_fd, | |
| 227 | + offset: desc.offset[1], | |
| 228 | + pitch: desc.stride[1], | |
| 229 | + }, | |
| 230 | + ], | |
| 231 | + width: self.width as u32, | |
| 232 | + height: self.height as u32, | |
| 233 | + modifier: desc.modifier, | |
| 234 | + format, | |
| 244 | 235 | }; |
| 245 | - let display = (self.width as u32, self.height as u32); | |
| 246 | 236 | let meta = self.meta(force_keyframe); |
| 247 | - if self.ten_bit { | |
| 248 | - if let Some(coded) = self | |
| 249 | - .encoder | |
| 250 | - .submit_prime(surface, meta) | |
| 251 | - .map_err(|e| e.to_string())? | |
| 252 | - { | |
| 253 | - self.ready.push_back(coded.data); | |
| 254 | - } | |
| 255 | - } else { | |
| 256 | - self.flush()?; | |
| 257 | - let coded = self | |
| 258 | - .encoder | |
| 259 | - .encode_surface(&surface, display, meta) | |
| 260 | - .map_err(|e| e.to_string())?; | |
| 237 | + if let Some(coded) = self | |
| 238 | + .encoder | |
| 239 | + .submit(EncodeInput::DmaBufYuv(&input), meta) | |
| 240 | + .map_err(|e| e.to_string())? | |
| 241 | + { | |
| 261 | 242 | self.ready.push_back(coded.data); |
| 262 | 243 | } |
| 263 | 244 | Ok(()) |
| @@ -280,49 +261,3 @@ impl EcEncSession { | ||
| 280 | 261 | Some(&self.out) |
| 281 | 262 | } |
| 282 | 263 | } |
| 283 | - | |
| 284 | -/// Builds the PRIME descriptor for an NV12 or P010 picture the decoder holds. | |
| 285 | -/// | |
| 286 | -/// Luma and chroma are either two objects (two descriptors) or one object | |
| 287 | -/// holding both at their own offsets, as the decoder's descriptor says. | |
| 288 | -fn prime_from_vhdma(desc: &VhDma, fds: &[OwnedFd]) -> Result<PrimeSurface, String> { | |
| 289 | - if fds.is_empty() || fds.len() > 2 || !matches!(desc.fourcc, FOURCC_NV12 | FOURCC_P010) { | |
| 290 | - return Err("DMA-BUF is not an NV12 or P010 picture on the GPU".into()); | |
| 291 | - } | |
| 292 | - let (w, h) = (desc.coded_width, desc.coded_height); | |
| 293 | - let rows = [h, h / 2]; | |
| 294 | - let two_objects = fds.len() == 2; | |
| 295 | - let mut objects = Vec::with_capacity(fds.len()); | |
| 296 | - for fd in fds { | |
| 297 | - objects.push(PrimeObject { | |
| 298 | - fd: fd.as_fd().try_clone_to_owned().map_err(|e| e.to_string())?, | |
| 299 | - size: 0, | |
| 300 | - modifier: desc.modifier, | |
| 301 | - }); | |
| 302 | - } | |
| 303 | - // The size each object must cover, luma rows then chroma rows. | |
| 304 | - for (plane, &plane_rows) in rows.iter().enumerate() { | |
| 305 | - let index = if two_objects { plane } else { 0 }; | |
| 306 | - let end = desc.offset[plane] as u64 + u64::from(desc.stride[plane]) * u64::from(plane_rows); | |
| 307 | - let size = &mut objects[index].size; | |
| 308 | - *size = (*size).max(u32::try_from(end).map_err(|_| "DMA-BUF too large")?); | |
| 309 | - } | |
| 310 | - let layer = PrimeLayer { | |
| 311 | - drm_format: desc.fourcc, | |
| 312 | - planes: vec![ | |
| 313 | - (0, desc.offset[0], desc.stride[0]), | |
| 314 | - ( | |
| 315 | - if two_objects { 1 } else { 0 }, | |
| 316 | - desc.offset[1], | |
| 317 | - desc.stride[1], | |
| 318 | - ), | |
| 319 | - ], | |
| 320 | - }; | |
| 321 | - Ok(PrimeSurface { | |
| 322 | - fourcc: desc.fourcc, | |
| 323 | - width: w, | |
| 324 | - height: h, | |
| 325 | - objects, | |
| 326 | - layers: vec![layer], | |
| 327 | - }) | |
| 328 | -} |
crates/engine-hw/src/lib.rsmodified+81 -152
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | //! Hardware decode and encode, held in a child process. This library is the |
| 2 | 2 | //! whole world of the `hw-child` binary ([`child`]), the one process a video |
| 3 | -//! driver is allowed to kill: VA-API decode (H.264, HEVC, VP9 and AV1), VP8 | |
| 3 | +//! driver is allowed to kill: Vulkan Video or VA-API decode (H.264, HEVC, VP9 and AV1), VP8 | |
| 4 | 4 | //! decode on `ec-vp8` ([`vpx`], software, linked in), and the encode seats |
| 5 | 5 | //! (H.264, HEVC, and AV1 where the GPU has the entrypoint). |
| 6 | 6 | //! |
| @@ -18,11 +18,9 @@ mod vpx; | ||
| 18 | 18 | use std::collections::VecDeque; |
| 19 | 19 | use std::os::fd::OwnedFd; |
| 20 | 20 | use std::path::Path; |
| 21 | -use std::rc::Rc; | |
| 22 | 21 | |
| 23 | -use ec_hw::Frame; | |
| 24 | 22 | use ec_hw::enc::EncCodec as EcCodec; |
| 25 | -use ec_va::caps::Profile; | |
| 23 | +use ec_hw::{CodecProfile, Direction, HwPicture}; | |
| 26 | 24 | use enc_ec::{EcEncSession, EcOpen}; |
| 27 | 25 | use engine::demux::{Codec, Demuxer, VideoMeta}; |
| 28 | 26 | use engine::hw::{CAP_AV1, CAP_H264, CAP_HEVC, CAP_VP8, CAP_VP9, VhCaps, VhDma, VhMeta}; |
| @@ -105,10 +103,7 @@ fn forward_skip(_standing: u32, ahead: u32) -> u32 { | ||
| 105 | 103 | } |
| 106 | 104 | |
| 107 | 105 | struct Session { |
| 108 | - decoder: ec_hw::Decoder, | |
| 109 | - /// The VA-API display the decoder's context and surfaces live on: kept | |
| 110 | - /// alive for as long as the session is. | |
| 111 | - _display: Rc<ec_va::Display>, | |
| 106 | + decoder: ec_hw::HwDecoder, | |
| 112 | 107 | demuxer: Demuxer, |
| 113 | 108 | meta: VhMeta, |
| 114 | 109 | timestamp: i64, |
| @@ -145,20 +140,14 @@ struct Session { | ||
| 145 | 140 | /// holds its surface out of the decoder's pool, so the decoder would write |
| 146 | 141 | /// the next picture into the one an encoder is still reading unless this |
| 147 | 142 | /// keeps the last [`DMA_HOLD`] of them alive. |
| 148 | - held: VecDeque<Frame>, | |
| 149 | -} | |
| 150 | - | |
| 151 | -/// The VA-API display the decoders run on: the first render node that opens. | |
| 152 | -fn open_decode_display() -> Option<Rc<ec_va::Display>> { | |
| 153 | - ec_va::open().ok() | |
| 143 | + held: VecDeque<HwPicture>, | |
| 154 | 144 | } |
| 155 | 145 | |
| 156 | 146 | impl Session { |
| 157 | - /// Assembles the VA-API half around a container that is already read -- | |
| 147 | + /// Assembles the hardware half around a container that is already read -- | |
| 158 | 148 | /// [`OpenSession::open_at`] owns the read and routes VP8 away from here |
| 159 | 149 | /// before any device is opened. |
| 160 | 150 | fn opened(meta: VideoMeta, demuxer: Demuxer) -> Option<Self> { |
| 161 | - let display = open_decode_display()?; | |
| 162 | 151 | let codec = match meta.codec { |
| 163 | 152 | Codec::H264 => ec_hw::Codec::H264, |
| 164 | 153 | Codec::Hevc => ec_hw::Codec::H265, |
| @@ -170,10 +159,9 @@ impl Session { | ||
| 170 | 159 | // Never offered to VA-API: `decode::hw_decodes` refuses it first. |
| 171 | 160 | Codec::Mpeg2 | Codec::ProRes | Codec::Dnxhd | Codec::Mjpeg | Codec::Dv | Codec::Mpeg4 | Codec::MsMpeg4 | Codec::Raw => return None, |
| 172 | 161 | }; |
| 173 | - let decoder = ec_hw::Decoder::new(&display, codec).ok()?; | |
| 162 | + let decoder = ec_hw::HwDecoder::open(codec).ok()?; | |
| 174 | 163 | Some(Self { |
| 175 | 164 | decoder, |
| 176 | - _display: display, | |
| 177 | 165 | demuxer, |
| 178 | 166 | meta: VhMeta { |
| 179 | 167 | width: meta.width, |
| @@ -246,7 +234,7 @@ impl Session { | ||
| 246 | 234 | /// Pumps the decoder until one picture is ready. `Ok(false)` is clean EOF. |
| 247 | 235 | fn pump(&mut self) -> Result<bool, String> { |
| 248 | 236 | loop { |
| 249 | - if let Some(frame) = self.decoder.next_frame() { | |
| 237 | + if let Some(frame) = self.decoder.next_picture().map_err(|e| e.to_string())? { | |
| 250 | 238 | // One picture further along, shown or skipped past alike: |
| 251 | 239 | // this is the only place [`Session::position`] walks. |
| 252 | 240 | self.position += 1; |
| @@ -296,7 +284,7 @@ impl Session { | ||
| 296 | 284 | /// Hands one decoded picture to the caller: as the buffer it was decoded |
| 297 | 285 | /// into where that is what the caller asked for and the encoder on the |
| 298 | 286 | /// other side can read it, and as read-back I420 everywhere else. |
| 299 | - fn deliver(&mut self, frame: Frame) -> Result<(), String> { | |
| 287 | + fn deliver(&mut self, frame: HwPicture) -> Result<(), String> { | |
| 300 | 288 | self.dma = None; |
| 301 | 289 | if self.want.is_some() |
| 302 | 290 | && let Some(dma) = self.export_dma(&frame) |
| @@ -320,68 +308,39 @@ impl Session { | ||
| 320 | 308 | /// objects, the fourcc and both sizes it asked for. `None` for every other |
| 321 | 309 | /// answer, and the caller then gets the pixels it always got rather than a |
| 322 | 310 | /// failure. |
| 323 | - fn export_dma(&self, frame: &Frame) -> Option<(VhDma, Vec<OwnedFd>)> { | |
| 311 | + fn export_dma(&self, frame: &HwPicture) -> Option<(VhDma, Vec<OwnedFd>)> { | |
| 324 | 312 | let want = self.want?; |
| 325 | - let (w, h) = frame.display_size; | |
| 326 | - if (w, h) != (want.width, want.height) { | |
| 313 | + if (frame.width, frame.height) != (want.width, want.height) { | |
| 327 | 314 | return None; |
| 328 | 315 | } |
| 329 | - let prime = frame.export_prime().ok()?; | |
| 330 | - // The driver describes the NV12 buffer as one layer per plane (R8 and | |
| 331 | - // GR88, each one plane into the same object), or as one composed layer | |
| 332 | - // holding both: either is the same two planes, in that order. | |
| 333 | - let planes: Vec<(u32, u32, u32)> = match prime.layers.as_slice() { | |
| 334 | - [layer] => layer.planes.clone(), | |
| 335 | - [luma, chroma] => { | |
| 336 | - let ([y], [uv]) = (luma.planes.as_slice(), chroma.planes.as_slice()) else { | |
| 337 | - return None; | |
| 338 | - }; | |
| 339 | - vec![*y, *uv] | |
| 340 | - } | |
| 341 | - _ => return None, | |
| 342 | - }; | |
| 343 | - let [(obj0, off0, pitch0), (obj1, off1, pitch1)] = planes.as_slice() else { | |
| 344 | - return None; | |
| 345 | - }; | |
| 346 | - let fits = prime.width == want.coded_width && prime.height == want.coded_height; | |
| 347 | - if prime.fourcc != want.fourcc | |
| 348 | - || !fits | |
| 349 | - || *obj0 != 0 | |
| 350 | - || *obj1 > 1 | |
| 351 | - || prime.objects.len() != *obj1 as usize + 1 | |
| 316 | + let pic = frame.export_dmabuf().ok()?; | |
| 317 | + let [plane0, plane1] = pic.planes; | |
| 318 | + if pic.fourcc != want.fourcc | |
| 319 | + || pic.width < want.coded_width | |
| 320 | + || pic.height < want.coded_height | |
| 321 | + || plane0.fd != 0 | |
| 322 | + || plane1.fd > 1 | |
| 323 | + || pic.fds.len() != plane1.fd + 1 | |
| 352 | 324 | { |
| 353 | 325 | return None; |
| 354 | 326 | } |
| 355 | - let planes = [*obj0, *obj1]; | |
| 356 | - let (offset, stride) = ([*off0, *off1], [*pitch0, *pitch1]); | |
| 357 | - // Whatever the driver says the two planes live in, said back to it | |
| 358 | - // unchanged on the import: radeonsi answers with an object per plane, | |
| 359 | - // other drivers with one for both, and neither is ours to reinterpret. | |
| 360 | - let modifier = prime.objects[0].modifier; | |
| 361 | - let mut objects = prime.objects.into_iter(); | |
| 362 | - // Owned by the caller from here: they travel to the encoder with the | |
| 363 | - // descriptor, one per plane object. | |
| 364 | - let mut fds = vec![objects.next()?.fd]; | |
| 365 | - if planes[1] == 1 { | |
| 366 | - fds.push(objects.next()?.fd); | |
| 367 | - } | |
| 368 | 327 | Some(( |
| 369 | 328 | VhDma { |
| 370 | - fourcc: prime.fourcc, | |
| 371 | - modifier, | |
| 372 | - coded_width: prime.width, | |
| 373 | - coded_height: prime.height, | |
| 374 | - width: w, | |
| 375 | - height: h, | |
| 376 | - offset, | |
| 377 | - stride, | |
| 329 | + fourcc: pic.fourcc, | |
| 330 | + modifier: pic.modifier, | |
| 331 | + coded_width: pic.width, | |
| 332 | + coded_height: pic.height, | |
| 333 | + width: frame.width, | |
| 334 | + height: frame.height, | |
| 335 | + offset: [plane0.offset, plane1.offset], | |
| 336 | + stride: [plane0.pitch, plane1.pitch], | |
| 378 | 337 | }, |
| 379 | - fds, | |
| 338 | + pic.fds, | |
| 380 | 339 | )) |
| 381 | 340 | } |
| 382 | 341 | |
| 383 | 342 | /// Copies a decoded picture into `self.out` as packed I420. |
| 384 | - fn emit(&mut self, frame: &Frame) -> Result<(), String> { | |
| 343 | + fn emit(&mut self, frame: &HwPicture) -> Result<(), String> { | |
| 385 | 344 | // The read-back goes through the driver's image copy (`vaGetImage`) |
| 386 | 345 | // rather than a CPU map of the surface: on a discrete GPU that buffer |
| 387 | 346 | // lives in VRAM, and a CPU read of it runs over the PCIe BAR. |
| @@ -709,96 +668,66 @@ fn copy_plane(src: &[u8], src_stride: usize, dst: &mut [u8], w: usize, h: usize) | ||
| 709 | 668 | } |
| 710 | 669 | } |
| 711 | 670 | |
| 712 | -/// One row of [`CAP_TABLE`]: the capability bit, its 8-bit and 10-bit decode profiles, and its encode profile. | |
| 713 | -type CapRow = (u32, &'static [Profile], &'static [Profile], Option<Profile>); | |
| 714 | - | |
| 715 | -/// What each codec needs of the driver before this plugin can claim it: the | |
| 716 | -/// 8-bit decode profiles, the 10-bit ones, and the profile the *encoder* here | |
| 717 | -/// would open -- `None` for the one this plugin has no encoder for at all, so a | |
| 718 | -/// GPU with a VP9 encode entrypoint is never reported as one edith can | |
| 719 | -/// reach. The encode profiles are the very ones the seats in [`enc_ec`] and | |
| 720 | -/// [`EncSession::open_seat`] ask for, which is what keeps this a description of the plugin rather than a | |
| 721 | -/// second opinion about it. | |
| 722 | -const CAP_TABLE: [CapRow; 4] = [ | |
| 723 | - ( | |
| 724 | - CAP_H264, | |
| 725 | - &[ | |
| 726 | - Profile::H264ConstrainedBaseline, | |
| 727 | - Profile::H264Main, | |
| 728 | - Profile::H264High, | |
| 729 | - ], | |
| 730 | - &[], | |
| 731 | - Some(Profile::H264Main), | |
| 732 | - ), | |
| 733 | - ( | |
| 734 | - CAP_HEVC, | |
| 735 | - &[Profile::HEVCMain], | |
| 736 | - &[Profile::HEVCMain10], | |
| 737 | - // 8-bit Main only: the encoder here opens Main whatever the source was, | |
| 738 | - // so a Main 10 entrypoint is a *decode* claim on this line and nothing | |
| 739 | - // more. Main 10 encode would be a second profile here and a 10-bit input | |
| 740 | - // buffer in `EncSession`, neither of which exists yet. | |
| 741 | - Some(Profile::HEVCMain), | |
| 742 | - ), | |
| 743 | - ( | |
| 744 | - CAP_VP9, | |
| 745 | - &[Profile::VP9Profile0], | |
| 746 | - &[Profile::VP9Profile2], | |
| 747 | - None, | |
| 748 | - ), | |
| 749 | - ( | |
| 750 | - CAP_AV1, | |
| 751 | - &[Profile::AV1Profile0], | |
| 752 | - // AV1 carries 8- and 10-bit in the same profile 0, so the profile bit | |
| 753 | - // is the same one and the P010 read-back is what decides. | |
| 754 | - &[Profile::AV1Profile0], | |
| 755 | - Some(Profile::AV1Profile0), | |
| 756 | - ), | |
| 757 | -]; | |
| 758 | - | |
| 759 | -/// This machine's decode and encode seats, asked of the driver rather than | |
| 760 | -/// assumed: which profiles it has, which entrypoints each of those carries, and | |
| 761 | -/// whether a P010 image exists to read a 10-bit picture back through. Only | |
| 762 | -/// codecs this plugin actually implements can light up -- `CAP_TABLE` is that | |
| 763 | -/// half of the intersection. | |
| 671 | +/// This machine's decode and encode seats, as the hardware backends report | |
| 672 | +/// them. `None` when there are none at all, the same "software only" a missing | |
| 673 | +/// plugin means. | |
| 764 | 674 | /// |
| 765 | 675 | /// VP8 is the one bit asked of nobody: it is this plugin's own native seat |
| 766 | -/// (`vpx`, `ec-vp8` linked in), lit unconditionally -- no profile, no | |
| 767 | -/// entrypoint, no display, no library to load. It rides along only when the | |
| 768 | -/// display *is* answerable, because this symbol's shape says a displayless | |
| 769 | -/// machine has nothing to report at all; the decode path itself needs no | |
| 770 | -/// display for it ([`OpenSession::open_at`] routes VP8 before any device | |
| 771 | -/// opens). | |
| 772 | -/// | |
| 773 | -/// `None` when there is no display to ask at all, which is the same "software | |
| 774 | -/// only" a missing plugin means. | |
| 676 | +/// (`vpx`, `ec-vp8` linked in), lit unconditionally. | |
| 775 | 677 | fn query_caps() -> Option<VhCaps> { |
| 776 | - use ec_va::caps::Entrypoint; | |
| 777 | - let display = open_decode_display()?; | |
| 778 | - let report = ec_va::CapReport::probe(&display).ok()?; | |
| 779 | - let p010 = report.image_formats.contains(&VA_FOURCC_P010); | |
| 780 | - let decodes = |list: &[Profile]| list.iter().any(|&p| report.supports(p, Entrypoint::VLD)); | |
| 781 | - let mut caps = VhCaps::default(); | |
| 782 | - for (bit, eight, ten, encode) in CAP_TABLE { | |
| 783 | - if decodes(eight) { | |
| 784 | - caps.decode |= bit; | |
| 678 | + let caps = ec_hw::capabilities(); | |
| 679 | + if caps.is_empty() { | |
| 680 | + return None; | |
| 681 | + } | |
| 682 | + let decode = |profile: CodecProfile| { | |
| 683 | + caps.iter() | |
| 684 | + .any(|c| c.direction == Direction::Decode && c.profile == profile) | |
| 685 | + }; | |
| 686 | + let encode = |profile: CodecProfile| { | |
| 687 | + caps.iter() | |
| 688 | + .any(|c| c.direction == Direction::Encode && c.profile == profile) | |
| 689 | + }; | |
| 690 | + let h264_decode = caps | |
| 691 | + .iter() | |
| 692 | + .any(|c| c.direction == Direction::Decode && c.codec == ec_hw::Codec::H264); | |
| 693 | + let h264_encode = caps | |
| 694 | + .iter() | |
| 695 | + .any(|c| c.direction == Direction::Encode && c.codec == ec_hw::Codec::H264); | |
| 696 | + let table = [ | |
| 697 | + (CAP_H264, h264_decode, false, h264_encode), | |
| 698 | + ( | |
| 699 | + CAP_HEVC, | |
| 700 | + decode(CodecProfile::H265Main), | |
| 701 | + decode(CodecProfile::H265Main10), | |
| 702 | + encode(CodecProfile::H265Main), | |
| 703 | + ), | |
| 704 | + ( | |
| 705 | + CAP_VP9, | |
| 706 | + decode(CodecProfile::Vp9Profile0), | |
| 707 | + decode(CodecProfile::Vp9Profile2), | |
| 708 | + false, | |
| 709 | + ), | |
| 710 | + ( | |
| 711 | + CAP_AV1, | |
| 712 | + decode(CodecProfile::Av1Main), | |
| 713 | + decode(CodecProfile::Av1Main10), | |
| 714 | + encode(CodecProfile::Av1Main), | |
| 715 | + ), | |
| 716 | + ]; | |
| 717 | + let mut out = VhCaps::default(); | |
| 718 | + for (bit, eight, ten, enc) in table { | |
| 719 | + if eight { | |
| 720 | + out.decode |= bit; | |
| 785 | 721 | } |
| 786 | - if p010 && decodes(ten) { | |
| 787 | - caps.decode_10bit |= bit; | |
| 722 | + if ten { | |
| 723 | + out.decode_10bit |= bit; | |
| 788 | 724 | } |
| 789 | - // Both entrypoints, exactly as the encoder's own open takes either. | |
| 790 | - if encode.is_some_and(|p| { | |
| 791 | - report.supports(p, Entrypoint::EncSlice) || report.supports(p, Entrypoint::EncSliceLP) | |
| 792 | - }) { | |
| 793 | - caps.encode |= bit; | |
| 725 | + if enc { | |
| 726 | + out.encode |= bit; | |
| 794 | 727 | } |
| 795 | 728 | } |
| 796 | - // The plugin's own software decoder, on its own account rather than the | |
| 797 | - // driver's -- the plugin's capability, never a GPU feat. Native now, so | |
| 798 | - // the bit has no "when it loads" condition left: it lights on every | |
| 799 | - // machine this plugin runs on. | |
| 800 | - caps.decode |= CAP_VP8; | |
| 801 | - Some(caps) | |
| 729 | + out.decode |= CAP_VP8; | |
| 730 | + Some(out) | |
| 802 | 731 | } |
| 803 | 732 | |
| 804 | 733 | /// How many pictures a group of them holds -- the low-delay predictor's |
crates/engine/src/export.rsmodified+1 -1
| @@ -1411,7 +1411,7 @@ fn hevc_irap(au: &[u8]) -> bool { | ||
| 1411 | 1411 | |
| 1412 | 1412 | /// What a hardware encode is called wherever one is named -- the card's line |
| 1413 | 1413 | /// before the export and the progress line during it. |
| 1414 | -const HW_LABEL: &str = "HW encode (VA-API)"; | |
| 1414 | +const HW_LABEL: &str = "HW encode (GPU)"; | |
| 1415 | 1415 | |
| 1416 | 1416 | /// ...and what a card says instead where [`EncoderSeat::Hardware`] was picked |
| 1417 | 1417 | /// and there is no seat: the export's own refusal in the width of a card row, |
crates/mcp/src/tools/export.rsmodified+2 -2
| @@ -2381,7 +2381,7 @@ mod tests { | ||
| 2381 | 2381 | |
| 2382 | 2382 | #[test] |
| 2383 | 2383 | fn picture_label_names_the_engine_seat_then_the_plan_and_skips_audio_only() { |
| 2384 | - let measured = Some("HW encode (VA-API) · AAC · SW encode (ec-aac)".to_string()); | |
| 2384 | + let measured = Some("HW encode (GPU) · AAC · SW encode (ec-aac)".to_string()); | |
| 2385 | 2385 | assert_eq!(picture_label(measured, "SW encode (rav1e)", Format::Mp4), " · video: H.264 (hardware)"); |
| 2386 | 2386 | assert_eq!(picture_label(None, "SW encode (ec-h264)", Format::Mp4), " · video: H.264 (software)"); |
| 2387 | 2387 | assert_eq!(picture_label(Some("copy · AAC".into()), "SW encode (ec-h264)", Format::Mp4), " · video: H.264 (copy)"); |
| @@ -2404,7 +2404,7 @@ mod tests { | ||
| 2404 | 2404 | assert_eq!(av_words(Format::Wav, "", "PCM · SW (hound)"), "audio: PCM, container: WAV"); |
| 2405 | 2405 | assert_eq!(av_words(Format::Gif, "", ""), "no sound, container: GIF"); |
| 2406 | 2406 | assert_eq!(av_words(Format::PngSequence, "", ""), "no sound, container: PNG folder"); |
| 2407 | - assert_eq!(measured_sound(Some("HW encode (VA-API) · AAC · SW encode (ec-aac)")).as_deref(), Some("AAC")); | |
| 2407 | + assert_eq!(measured_sound(Some("HW encode (GPU) · AAC · SW encode (ec-aac)")).as_deref(), Some("AAC")); | |
| 2408 | 2408 | assert_eq!(measured_sound(Some("SW encode (ec-h264)")), None); |
| 2409 | 2409 | assert_eq!(measured_sound(None), None); |
| 2410 | 2410 | assert_eq!(plain_audio("no sound to write"), "none"); |