forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-vp8-swap' (36bd0e7): the VP8 decode seat is the native ec-vp8 -- libvpx's dlopen leaves the plugin (no symbols, no strings, no DT_NEEDED); per-frame hashes identical to the libvpx seat on every witness (720p 60/60, the 2560x1321 odd-height clip 270/270, two-pass altref 90/90 shown + 6 hidden with identical seek landings); CAP_VP8 unconditionally lit, still never a GPU seat; un-parked after lane-vp8-perf put the big-obs class at ~64 fps seat-equivalent (1.4x the line)

Browse files at this commit

6421f59a035a803b159766cf98db165bba12b6baAhmet Kaan GümüşParent: 431144436bd0e7
Cargo.lockmodified+8 -1
@@ -1649,6 +1649,13 @@ dependencies = [
16491649 "wide",
16501650 ]
16511651
1652+[[package]]
1653+name = "ec-vp8"
1654+version = "0.1.0"
1655+dependencies = [
1656+ "ec-core",
1657+]
1658+
16521659 [[package]]
16531660 name = "edith"
16541661 version = "0.1.0"
@@ -1739,9 +1746,9 @@ name = "engine-hw"
17391746 version = "0.1.0"
17401747 dependencies = [
17411748 "cros-codecs",
1749+ "ec-vp8",
17421750 "engine",
17431751 "gbm",
1744- "libloading",
17451752 ]
17461753
17471754 [[package]]
crates/engine-hw/Cargo.tomlmodified+4 -4
@@ -14,7 +14,7 @@ engine = { path = "../engine" }
1414 cros-codecs = { version = "0.0.6", features = ["vaapi"] }
1515 # Same version cros-codecs already pulls in; we allocate output frames ourselves.
1616 gbm = { version = "0.15", default-features = false, features = ["drm-support"] }
17-# Same version `engine` pins for its own dlopens (`hw.rs`, `ao.rs`); this is
18-# the plugin's half of the same philosophy, one level down: `libvpx` for the
19-# software VP8 seat (`src/vpx.rs`), resolved at runtime like libva itself.
20-libloading = "0.8"
17+# The software VP8 seat, this project's own (`ec-vp8`, RFC 6386): linked in
18+# as ordinary Rust, the one codec with no runtime dlopen behind it -- the
19+# `libloading` the old libvpx FFI needed is gone with the FFI.
20+ec-vp8 = { path = "../../../edith_codecs/crates/ec-vp8" }
crates/engine-hw/src/lib.rsmodified+69 -102
@@ -1,10 +1,11 @@
11 //! VA-API decode (H.264, HEVC, VP9 and AV1) -- and VP8, which decodes on
2-//! `libvpx`, dlopen'd by this plugin in turn ([`vpx`], software, no VA-API
3-//! anything) -- and encode (H.264 and HEVC, and AV1 where the GPU has an
4-//! entrypoint for it), shipped as a
2+//! `ec-vp8` ([`vpx`], this project's own, software, no VA-API anything),
3+//! linked into the plugin itself -- and encode (H.264 and HEVC, and AV1
4+//! where the GPU has an entrypoint for it), shipped as a
55 //! `dlopen`-able plugin so the main binary never gets a DT_NEEDED on
6-//! libva/gbm/drm -- nor on libvpx, the same rule one level down. Every entry
7-//! point is
6+//! libva/gbm/drm. VP8's seat is the one codec with no runtime dlopen
7+//! behind it anywhere: the old libvpx FFI is gone with the library it
8+//! loaded. Every entry point is
89 //! `extern "C"`, catches unwinds and reports failure as a null pointer or a
910 //! negative code: the caller's contract is "any failure means use the software
1011 //! codec".
@@ -71,10 +72,7 @@ use cros_codecs::{Fourcc, FrameLayout, PlaneLayout, Resolution};
7172 use gbm::{BufferObjectFlags, Format as GbmFormat};
7273
7374 use engine::demux::{Codec, Demuxer, VideoMeta};
74-use engine::hw::{
75- CAP_AV1, CAP_H264, CAP_HEVC, CAP_VP8, CAP_VP9, VhCaps, VhDma, VhFrame, VhMeta,
76-};
77-use vpx::VpxCtx;
75+use engine::hw::{CAP_AV1, CAP_H264, CAP_HEVC, CAP_VP8, CAP_VP9, VhCaps, VhDma, VhFrame, VhMeta};
7876
7977 type PooledFrame = PooledVideoFrame<GenericDmaVideoFrame>;
8078 type Dec<C> = StatelessDecoder<C, VaapiBackend<PooledFrame>>;
@@ -872,19 +870,19 @@ impl OpenSession {
872870 }
873871 }
874872
875-/// One libvpx decode session: the VP8 half of the plugin, the whole of the
873+/// One VP8 decode session: the VP8 half of the plugin, the whole of the
876874 /// same job [`Session`] does for the VA-API codecs. No display, no GBM, no
877-/// surface pool -- libvpx hands the planes back in memory, and the only
875+/// surface pool -- `ec-vp8` hands the planes back in memory, and the only
878876 /// shapes worth carrying over from the other half are the ones the caller
879877 /// sees: [`VhMeta`], one picture per `pump`, the same [`VhFrame`] layout, the
880878 /// same skip/position arithmetic a seek is.
881879 ///
882-/// VP8 reorders nothing: every access unit fed is one picture out, in feed
883-/// order, which is why `position` advances exactly as the VA-API half's does
884-/// without any of its drain machinery.
880+/// VP8 reorders nothing: every access unit fed is *at most* one picture out,
881+/// in feed order (a hidden altref frame produces none), which is why
882+/// `position` advances exactly as the VA-API half's does without any of its
883+/// drain machinery.
885884 struct VpxSession {
886- vpx: &'static vpx::Vpx,
887- ctx: VpxCtx,
885+ dec: vpx::Decoder,
888886 demuxer: Demuxer,
889887 meta: VhMeta,
890888 flushed: bool,
@@ -905,10 +903,11 @@ struct VpxSession {
905903
906904 impl VpxSession {
907905 fn opened(meta: VideoMeta, demuxer: Demuxer) -> Option<Self> {
908- let vpx = vpx::vpx()?;
909- let mut session = Self {
910- vpx,
911- ctx: VpxCtx::default(),
906+ // The decoder cannot refuse to exist any more: it is linked in. The
907+ // `Option` is the shape [`OpenSession::open_at`] routes on, shared
908+ // with [`Session::opened`], whose failures are real.
909+ Some(Self {
910+ dec: vpx::Decoder::new(),
912911 demuxer,
913912 meta: VhMeta {
914913 width: meta.width,
@@ -922,50 +921,30 @@ impl VpxSession {
922921 out: Vec::new(),
923922 luma: 0,
924923 chroma: 0,
925- };
926- if let Err(e) = session.init() {
927- eprintln!("engine_hw: {e}");
928- return None;
929- }
930- Some(session)
931- }
932-
933- /// (Re-)initialises the decoder. libvpx has no mid-stream reset, so this
934- /// *is* the flush: a seek pays destroy + init, and what makes that safe
935- /// is the demuxer's sync-sample seek, which restarts the feed where the
936- /// reference chain restarts -- the same discipline [`Session`]'s VA-API
937- /// flush leans on.
938- fn init(&mut self) -> Result<(), String> {
939- self.vpx.dec_init(&mut self.ctx)
924+ })
940925 }
941926
942927 /// Feeds one access unit. VP8 emits at most one picture per unit, so the
943928 /// pull belongs to the `pump` loop, not here.
944929 fn decode(&mut self, au: &[u8]) -> Result<(), String> {
945- if au.is_empty() {
946- return Ok(());
947- }
948- self.vpx.decode(&mut self.ctx, au)
930+ self.dec.decode(au)
949931 }
950932
951933 fn pump(&mut self) -> Result<bool, String> {
952934 loop {
953935 // Whatever the last fed unit produced is the picture this call
954- // hands back. The iterator starts fresh per feed because VP8
955- // queues nothing: one unit in, at most one picture out.
956- let mut iter: *const c_void = std::ptr::null();
957- while let Some(img) = self.vpx.get_frame(&mut self.ctx, &mut iter) {
936+ // hands back. The slot is taken, not peeked -- the next fed unit
937+ // overwrites it, because VP8 queues nothing: one unit in, at
938+ // most one picture out.
939+ if let Some(pic) = self.dec.get_frame() {
958940 // One picture further along, shown or skipped past alike --
959- // the same walk [`Session::pump`] makes. The fields the copy
960- // needs are taken by value, which ends the context's borrow
961- // before `emit` may take the session again.
962- let (planes, stride, (w, h)) = (img.planes, img.stride, img.display());
941+ // the same walk [`Session::pump`] makes.
963942 self.position += 1;
964943 if self.skip > 0 {
965944 self.skip -= 1;
966945 continue;
967946 }
968- self.emit(planes, stride, w, h);
947+ self.emit(&pic);
969948 return Ok(true);
970949 }
971950 if self.flushed {
@@ -974,11 +953,12 @@ impl VpxSession {
974953 match self.demuxer.next_access_unit() {
975954 Ok(Some(au)) => self.decode(&au)?,
976955 Ok(None) => {
977- // End of the track: the empty `vpx_codec_decode` that
978- // releases anything held back is this decoder's flush,
979- // and it costs no iteration of its own -- the pull at the
980- // top of the loop drains it.
981- self.vpx.flush(&mut self.ctx)?;
956+ // End of the track. VP8 holds nothing back -- the flush
957+ // is the formality [`vpx::Decoder::flush`] documents --
958+ // and it costs no iteration of its own: the pull at the
959+ // top of the loop finds the slot empty and ends the
960+ // stream.
961+ self.dec.flush()?;
982962 self.flushed = true;
983963 }
984964 Err(e) => return Err(e.to_string()),
@@ -990,27 +970,24 @@ impl VpxSession {
990970 /// for a decoder that hands the planes back in memory rather than behind
991971 /// a `vaGetImage`. VP8 output is always 8-bit 4:2:0, so there is no
992972 /// 10-bit branch to mirror.
993- fn emit(&mut self, planes: [*mut u8; 4], stride: [std::ffi::c_int; 4], w: u32, h: u32) {
994- let (w, h) = (w as usize, h as usize);
973+ fn emit(&mut self, pic: &vpx::Picture) {
974+ let (w, h) = (usize::from(pic.width), usize::from(pic.height));
995975 let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));
996976 if self.luma != w * h || self.chroma != cw * ch {
997977 self.luma = w * h;
998978 self.chroma = cw * ch;
999979 self.out = vec![0u8; self.luma + 2 * self.chroma];
1000980 }
1001- self.meta.width = w as u32;
1002- self.meta.height = h as u32;
981+ self.meta.width = u32::from(pic.width);
982+ self.meta.height = u32::from(pic.height);
1003983 let (dst_y, rest) = self.out.split_at_mut(self.luma);
1004984 let (dst_u, dst_v) = rest.split_at_mut(self.chroma);
1005- // SAFETY: libvpx guarantees the planes stride-cover the *display*
1006- // size (4:2:0, so the chroma planes cover cw x ch) for the life of
1007- // the image, which ends at this session's next `get_frame` -- after
1008- // the copies below.
1009- unsafe {
1010- copy_plane(planes[0], stride[0] as usize, dst_y, w, h);
1011- copy_plane(planes[1], stride[1] as usize, dst_u, cw, ch);
1012- copy_plane(planes[2], stride[2] as usize, dst_v, cw, ch);
1013- }
985+ // The planes stride-cover the visible frame by the `Picture`
986+ // contract (4:2:0, so the chroma planes cover cw x ch), and they are
987+ // owned by this session -- no lifetime to out-think, no unsafe.
988+ copy_plane(&pic.y, pic.stride, dst_y, w, h);
989+ copy_plane(&pic.u, pic.uv_stride, dst_u, cw, ch);
990+ copy_plane(&pic.v, pic.uv_stride, dst_v, cw, ch);
1014991 }
1015992
1016993 fn fill(&self, out: &mut VhFrame) {
@@ -1031,9 +1008,9 @@ impl VpxSession {
10311008
10321009 /// Repositions the session, exactly [`Session::seek_to`]: the same
10331010 /// forward walk within [`FORWARD_SECS`], the same demuxer seek to a sync
1034- /// sample otherwise -- and for the decoder, destroy + re-init where the
1035- /// VA-API half pays a flush, because VP8 is stateful to its last inter
1036- /// frame and libvpx offers no softer reset.
1011+ /// sample otherwise -- and for the decoder, a fresh [`vpx::Decoder`]
1012+ /// where the VA-API half pays a flush, because VP8 is stateful to its
1013+ /// last inter frame.
10371014 fn seek_to(&mut self, target_sample: u32) -> Result<(), String> {
10381015 let target_frame = target_sample.saturating_sub(1);
10391016 if let Reposition::Forward(ahead) = reposition(
@@ -1045,11 +1022,11 @@ impl VpxSession {
10451022 self.skip = forward_skip(self.skip, ahead);
10461023 return Ok(());
10471024 }
1048- // Dropping every reference the decoder holds. The destroy's answer is
1049- // not checked against `Ok` because the init below re-zeroes the
1050- // context either way; a destroy that failed has nothing to leak.
1051- self.vpx.destroy(&mut self.ctx);
1052- self.init()?;
1025+ // Dropping every reference the decoder holds: the whole of what the
1026+ // FFI seat paid as destroy + re-init is now the assignment of an
1027+ // owned value. What makes it safe is unchanged -- the demuxer's
1028+ // sync-sample seek restarts the feed where the reference chain does.
1029+ self.dec = vpx::Decoder::new();
10531030 self.flushed = false;
10541031 let first = self.demuxer.seek_to_sync_at_or_before(target_frame);
10551032 self.skip = (i64::from(target_frame) - first).max(0) as u32;
@@ -1058,20 +1035,11 @@ impl VpxSession {
10581035 }
10591036 }
10601037
1061-impl Drop for VpxSession {
1062- fn drop(&mut self) {
1063- self.vpx.destroy(&mut self.ctx);
1064- }
1065-}
1066-
10671038 /// One strided plane down to tightly packed rows: the whole of the read-back
10681039 /// a VP8 picture costs, where the VA-API half pays `vaGetImage` for the same.
1069-///
1070-/// # Safety
1071-/// `src` must stride-cover `w` x `h`, and `dst` must be `w` x `h`.
1072-unsafe fn copy_plane(src: *const u8, stride: usize, dst: &mut [u8], w: usize, h: usize) {
1040+fn copy_plane(src: &[u8], src_stride: usize, dst: &mut [u8], w: usize, h: usize) {
10731041 for row in 0..h {
1074- let s = unsafe { std::slice::from_raw_parts(src.add(row * stride), w) };
1042+ let s = &src[row * src_stride..row * src_stride + w];
10751043 dst[row * w..(row + 1) * w].copy_from_slice(s);
10761044 }
10771045 }
@@ -1079,8 +1047,8 @@ unsafe fn copy_plane(src: *const u8, stride: usize, dst: &mut [u8], w: usize, h:
10791047 /// Opens `path` for hardware decode, positioned so the first
10801048 /// [`vh_next_frame`] returns sample `target_sample` (1-based; 0 and 1 both mean
10811049 /// the start of the stream). Returns null on any failure at all: no libva
1082-/// runtime, no render node, no libvpx for a VP8 file, unsupported profile,
1083-/// unreadable file.
1050+/// runtime, no render node, unsupported profile, unreadable file, a VP8
1051+/// stream the decoder rejects.
10841052 ///
10851053 /// # Safety
10861054 /// `path` must be a valid NUL-terminated C string.
@@ -1215,7 +1183,7 @@ pub unsafe extern "C" fn vh_next_frame_dma(
12151183 let session = unsafe { &mut *(session as *mut OpenSession) };
12161184 // SAFETY: as above.
12171185 unsafe { *dma = VhDma::default() };
1218- // A libvpx picture has no GPU buffer to hand over: this session reads
1186+ // A VP8 picture has no GPU buffer to hand over: this session reads
12191187 // back, exactly as `vh_next_frame` does, and the caller's `dma` keeps
12201188 // the fd-below-zero default that says so.
12211189 match session {
@@ -1333,12 +1301,13 @@ const CAP_TABLE: [(u32, &[VAProfile::Type], &[VAProfile::Type], Option<VAProfile
13331301 /// codecs this plugin actually implements can light up -- `CAP_TABLE` is that
13341302 /// half of the intersection.
13351303 ///
1336-/// VP8 is the one bit asked of nobody: it is this plugin's libvpx seat, lit
1337-/// when the library loads and nothing else -- no profile, no entrypoint, no
1338-/// display. It rides along only when the display *is* answerable, because
1339-/// this symbol's shape says a displayless machine has nothing to report at
1340-/// all; the decode path itself needs no display for it
1341-/// ([`OpenSession::open_at`] routes VP8 before any device opens).
1304+/// VP8 is the one bit asked of nobody: it is this plugin's own native seat
1305+/// (`vpx`, `ec-vp8` linked in), lit unconditionally -- no profile, no
1306+/// entrypoint, no display, no library to load. It rides along only when the
1307+/// display *is* answerable, because this symbol's shape says a displayless
1308+/// machine has nothing to report at all; the decode path itself needs no
1309+/// display for it ([`OpenSession::open_at`] routes VP8 before any device
1310+/// opens).
13421311 ///
13431312 /// `None` when there is no display to ask at all, which is the same "software
13441313 /// only" a missing plugin means.
@@ -1354,10 +1323,8 @@ fn query_caps() -> Option<VhCaps> {
13541323 .query_config_entrypoints(profile)
13551324 .is_ok_and(|e| e.contains(&entrypoint))
13561325 };
1357- let decodes = |list: &[VAProfile::Type]| {
1358- list.iter()
1359- .any(|&p| has(p, VAEntrypoint::VAEntrypointVLD))
1360- };
1326+ let decodes =
1327+ |list: &[VAProfile::Type]| list.iter().any(|&p| has(p, VAEntrypoint::VAEntrypointVLD));
13611328 let mut caps = VhCaps::default();
13621329 for (bit, eight, ten, encode) in CAP_TABLE {
13631330 if decodes(eight) {
@@ -1374,10 +1341,10 @@ fn query_caps() -> Option<VhCaps> {
13741341 }
13751342 }
13761343 // The plugin's own software decoder, on its own account rather than the
1377- // driver's -- the plugin's capability, never a GPU feat.
1378- if vpx::vpx().is_some() {
1379- caps.decode |= CAP_VP8;
1380- }
1344+ // driver's -- the plugin's capability, never a GPU feat. Native now, so
1345+ // the bit has no "when it loads" condition left: it lights on every
1346+ // machine this plugin runs on.
1347+ caps.decode |= CAP_VP8;
13811348 Some(caps)
13821349 }
13831350
crates/engine-hw/src/vpx.rsmodified+63 -258
@@ -1,273 +1,78 @@
1-//! `libvpx`, dlopen'd: the software VP8 decoder, and the reason a VP8 file
2-//! decodes on a machine whose GPU has no VP8 profile -- or whose GPU is
3-//! absent altogether.
1+//! The software VP8 decoder: `ec-vp8`, this project's own implementation of
2+//! RFC 6386 -- the reason a VP8 file decodes on a machine whose GPU has no
3+//! VP8 profile, or whose GPU is absent altogether.
44 //!
5-//! The plugin's own libva linkage, one level down: this plugin keeps no
6-//! DT_NEEDED on `libvpx` any more than the main binary does. The symbols are
7-//! resolved lazily, once, and a machine without the library is a machine
8-//! without the codec -- answered as `None` at [`vpx`] before any file is
9-//! opened, which is what [`crate::query_caps`] reports as the absence of
10-//! `CAP_VP8`.
5+//! The seat was `libvpx`, resolved at runtime behind a hand-written FFI; the
6+//! native crate replaces it outright, and with it the whole dlopen layer
7+//! goes: the plugin links the decoder as ordinary Rust, so there is no
8+//! library to look for, no ABI to pin, and no machine where the codec is
9+//! absent from a build that carries this plugin. What survives from the FFI
10+//! days is the seat's shape -- the five verbs the session above lives on:
1111 //!
12-//! The extern declarations are hand-written against `vpx_decoder.h`,
13-//! `vpx_codec.h` and `vpx_image.h` as this soname has carried them since
14-//! libvpx 1.14: `VPX_DECODER_ABI_VERSION` 12. A libvpx from another ABI
15-//! answers `vpx_codec_dec_init_ver` with `VPX_CODEC_ABI_MISMATCH`, which
16-//! surfaces as a refused session -- loudly, in the eprintln, never as a
17-//! wrong picture. The struct layouts below are the ABI, not an assumption:
18-//! `vpx_codec_ctx_t`'s fields have not moved since the library's first
19-//! release, and `vpx_image_t`'s prefix is what every decoder in the field
20-//! reads.
21-
22-use std::ffi::{CStr, c_char, c_int, c_long, c_uint, c_void};
23-use std::sync::LazyLock;
24-
25-use libloading::Library;
26-
27-/// `VPX_DECODER_ABI_VERSION`: `3 + VPX_CODEC_ABI_VERSION`, and
28-/// `VPX_CODEC_ABI_VERSION = 4 + VPX_IMAGE_ABI_VERSION` with
29-/// `VPX_IMAGE_ABI_VERSION = 5` -- the three headers this module mirrors.
30-const VPX_DECODER_ABI_VERSION: c_int = 12;
31-
32-/// `VPX_CODEC_OK`, the one return code that is not a refusal.
33-const VPX_CODEC_OK: c_int = 0;
34-
35-/// `VPX_DL_REALTIME`, the decode `deadline` argument -- which `vpx_decoder.h`
36-/// states this API ignores; passed because the type demands a value, and the
37-/// constant because it is the documented one.
38-const VPX_DL_REALTIME: c_long = 1;
39-
40-/// The decoder context, `vpx_codec_ctx_t` field for field. Nothing here is
41-/// read: the struct exists so the context is laid out exactly as libvpx
42-/// lays it out, and so the `&mut` handoffs to the C side are the whole of
43-/// the story.
44-#[repr(C)]
45-pub struct VpxCtx {
46- name: *const c_char,
47- iface: *const c_void,
48- err: c_int,
49- err_detail: *const c_char,
50- init_flags: c_uint,
51- config: *const c_void,
52- priv_: *mut c_void,
53-}
54-
55-impl Default for VpxCtx {
56- fn default() -> Self {
57- // SAFETY: all-pointer/zero initialisation is what libvpx's own init
58- // performs on the context before it fills it; `vpx_codec_dec_init_ver`
59- // re-zeroes it anyway.
60- unsafe { std::mem::zeroed() }
61- }
12+//! - `init` -- a fresh [`Decoder`], infallible now that there is no ABI to
13+//! negotiate;
14+//! - `decode` -- one access unit in, the picture (if any) held for the next
15+//! `get_frame`;
16+//! - `get_frame` -- the picture the last `decode` produced, taken once;
17+//! - `flush` -- a formality: VP8 reorders nothing and holds nothing back, so
18+//! end of stream has nothing to release (libvpx wanted the empty
19+//! `vpx_codec_decode`; the native decoder has no equivalent need, and the
20+//! verb stays because the session's pump is written around it);
21+//! - `destroy` -- now simply the drop of an owned value, which is why a
22+//! seek is `self.decoder = Decoder::new()` where the FFI seat paid a
23+//! destroy + re-init round trip.
24+
25+use ec_vp8::decode;
26+
27+/// The VP8 decoder: [`decode::Decoder`] plus the one-picture slot between
28+/// `decode` and `get_frame`.
29+pub(super) struct Decoder {
30+ inner: decode::Decoder,
31+ pending: Option<Picture>,
6232 }
6333
64-/// `vpx_codec_dec_cfg_t`: thread count and a best-known size the caller may
65-/// leave at zero.
66-#[repr(C)]
67-struct VpxDecCfg {
68- threads: c_uint,
69- w: c_uint,
70- h: c_uint,
71-}
72-
73-/// `vpx_image_t` complete: the planes of one decoded picture and the strides
74-/// they sit at. The decoder owns the bytes -- they stay valid only until the
75-/// next `get_frame` or `decode` on the same context, which is exactly the
76-/// [`crate::VhFrame`] contract the caller already lives with.
77-#[repr(C)]
78-pub struct VpxImage {
79- fmt: c_int,
80- cs: c_int,
81- range: c_int,
82- w: c_uint,
83- h: c_uint,
84- bit_depth: c_uint,
85- d_w: c_uint,
86- d_h: c_uint,
87- r_w: c_uint,
88- r_h: c_uint,
89- x_chroma_shift: c_uint,
90- y_chroma_shift: c_uint,
91- pub planes: [*mut u8; 4],
92- pub stride: [c_int; 4],
93- bps: c_int,
94- user_priv: *mut c_void,
95- img_data: *mut u8,
96- img_data_owner: c_int,
97- self_allocd: c_int,
98- fb_priv: *mut c_void,
99-}
100-
101-impl VpxImage {
102- /// Displayed dimensions: what [`crate::VhFrame`] describes, not the
103- /// macroblock-padded storage the planes actually cover.
104- pub fn display(&self) -> (u32, u32) {
105- (self.d_w, self.d_h)
106- }
107-}
108-
109-type Dx = unsafe extern "C" fn() -> *const c_void;
110-type DecInitVer =
111- unsafe extern "C" fn(*mut VpxCtx, *const c_void, *const VpxDecCfg, c_uint, c_int) -> c_int;
112-type Decode = unsafe extern "C" fn(*mut VpxCtx, *const u8, c_uint, *mut c_void, c_long) -> c_int;
113-type GetFrame = unsafe extern "C" fn(*mut VpxCtx, *mut *const c_void) -> *mut VpxImage;
114-type Destroy = unsafe extern "C" fn(*mut VpxCtx) -> c_int;
115-type ErrorFn = unsafe extern "C" fn(*const VpxCtx) -> *const c_char;
116-
117-/// The resolved symbols, held with the library that carries them -- exactly
118-/// the shape `engine::hw`'s `Plugin` has on the other side of the ABI.
119-pub struct Vpx {
120- vp8_dx: Dx,
121- dec_init_ver: DecInitVer,
122- decode: Decode,
123- get_frame: GetFrame,
124- destroy: Destroy,
125- error: ErrorFn,
126- // Never dropped (the struct lives in a static), so the pointers above
127- // stay valid for the life of the process.
128- _lib: Library,
129-}
130-
131-/// The library, loaded once per process. `None` -- no `libvpx.so` on this
132-/// machine, or one missing a symbol this module names -- is the one answer,
133-/// asked before any file is opened.
134-static VPX: LazyLock<Option<Vpx>> = LazyLock::new(load);
135-
136-/// The loaded library, or `None` on a machine without one this module can
137-/// use. Asked before any file is opened.
138-pub fn vpx() -> Option<&'static Vpx> {
139- VPX.as_ref()
140-}
141-
142-fn load() -> Option<Vpx> {
143- // The versioned soname first -- the name every distribution actually
144- // ships -- then the unversioned development name for the odd link that
145- // has it.
146- for name in ["libvpx.so.9", "libvpx.so"] {
147- let lib = match unsafe { Library::new(name) } {
148- Ok(lib) => lib,
149- Err(_) => continue,
150- };
151- // SAFETY: the symbols are type-checked against the declarations above;
152- // a missing one unloads the library with the dropped closure and the
153- // next candidate gets its turn.
154- let vpx = unsafe {
155- (|| {
156- Some(Vpx {
157- vp8_dx: *lib.get(b"vpx_codec_vp8_dx\0").ok()?,
158- dec_init_ver: *lib.get(b"vpx_codec_dec_init_ver\0").ok()?,
159- decode: *lib.get(b"vpx_codec_decode\0").ok()?,
160- get_frame: *lib.get(b"vpx_codec_get_frame\0").ok()?,
161- destroy: *lib.get(b"vpx_codec_destroy\0").ok()?,
162- error: *lib.get(b"vpx_codec_error\0").ok()?,
163- _lib: lib,
164- })
165- })()
166- };
167- if vpx.is_some() {
168- return vpx;
34+/// One decoded picture: the visible frame's planes, 8-bit 4:2:0, strided
35+/// (`stride` may exceed `width`) and owned by whoever holds it -- what
36+/// [`crate::VhFrame`] hands on is the packed copy the session makes.
37+pub(super) use decode::Picture;
38+
39+impl Decoder {
40+ /// `init`: a decoder holding no references yet; the first key frame
41+ /// sets the size.
42+ pub(super) fn new() -> Self {
43+ Self {
44+ inner: decode::Decoder::new(),
45+ pending: None,
16946 }
17047 }
171- None
172-}
17348
174-impl Vpx {
175- /// `vpx_codec_error`'s line about the context's last answer: what an init
176- /// or decode refusal says instead of a bare `vpx_codec_err_t` number.
177- fn last_error(&self, ctx: &VpxCtx) -> String {
178- // SAFETY: `ctx` is a live libvpx context; the string it names is
179- // static and read before anything else touches the context.
180- let msg = unsafe { (self.error)(ctx) };
181- if msg.is_null() {
182- return "no description".to_string();
49+ /// `decode`: one access unit in -- one complete VP8 frame payload,
50+ /// exactly what a container demuxer hands over for `V_VP8`/`vp08`. A
51+ /// hidden frame (an alternate reference, `show_frame == 0`) updates the
52+ /// reference buffers and produces no picture: the same nothing libvpx
53+ /// emitted for one, so the seat's picture accounting does not move.
54+ pub(super) fn decode(&mut self, au: &[u8]) -> Result<(), String> {
55+ if au.is_empty() {
56+ return Ok(());
18357 }
184- // SAFETY: non-null per the check above, NUL-terminated per libvpx.
185- unsafe { CStr::from_ptr(msg) }.to_string_lossy().into_owned()
186- }
187-
188- /// The VP8 decoder interface, `vpx_codec_vp8_dx`: the argument every
189- /// `dec_init_ver` on this context takes.
190- fn iface(&self) -> *const c_void {
191- // SAFETY: a pure lookup in libvpx's own tables.
192- unsafe { (self.vp8_dx)() }
193- }
194-
195- /// `vpx_codec_dec_init_ver` with the ABI version this module was written
196- /// against -- the check a mismatched libvpx fails rather than a call it
197- /// misreads.
198- pub(super) fn dec_init(&self, ctx: &mut VpxCtx) -> Result<(), String> {
199- let cfg = VpxDecCfg {
200- threads: threads(),
201- w: 0,
202- h: 0,
203- };
204- // SAFETY: the context is ours and live for the session; `cfg` is read
205- // only during the call; the version pins the ABI.
206- let rc = unsafe { (self.dec_init_ver)(ctx, self.iface(), &cfg, 0, VPX_DECODER_ABI_VERSION) };
207- match rc {
208- VPX_CODEC_OK => Ok(()),
209- _ => Err(format!("libvpx init failed: {}", self.last_error(ctx))),
58+ match self.inner.decode(au) {
59+ Ok(picture) => self.pending = picture,
60+ Err(e) => return Err(format!("vp8 decode failed: {e}")),
21061 }
62+ Ok(())
21163 }
21264
213- /// `vpx_codec_decode`: one access unit in. `VPX_DL_REALTIME` is the
214- /// documented-but-ignored deadline.
215- pub(super) fn decode(&self, ctx: &mut VpxCtx, au: &[u8]) -> Result<(), String> {
216- // SAFETY: `au` outlives the call; the context is live and used from
217- // the one thread the session lives on.
218- let rc = unsafe {
219- (self.decode)(
220- ctx,
221- au.as_ptr(),
222- au.len() as c_uint,
223- std::ptr::null_mut(),
224- VPX_DL_REALTIME,
225- )
226- };
227- match rc {
228- VPX_CODEC_OK => Ok(()),
229- _ => Err(format!("libvpx decode failed: {}", self.last_error(ctx))),
230- }
231- }
232-
233- /// `vpx_codec_decode` with a null, empty frame: the end-of-stream signal
234- /// that releases whatever the decoder held back.
235- pub(super) fn flush(&self, ctx: &mut VpxCtx) -> Result<(), String> {
236- // SAFETY: as `decode`, with the nulls libvpx reads as "no more data".
237- let rc = unsafe {
238- (self.decode)(ctx, std::ptr::null(), 0, std::ptr::null_mut(), VPX_DL_REALTIME)
239- };
240- match rc {
241- VPX_CODEC_OK => Ok(()),
242- _ => Err(format!("libvpx flush failed: {}", self.last_error(ctx))),
243- }
65+ /// `get_frame`: the picture the last `decode` produced, if that frame
66+ /// was shown. Taken, not peeked -- the slot is empty again afterwards,
67+ /// which is what lets the pump loop feed the next unit.
68+ pub(super) fn get_frame(&mut self) -> Option<Picture> {
69+ self.pending.take()
24470 }
24571
246- /// `vpx_codec_get_frame`: the next picture of the current iteration, or
247- /// `None` when the iteration is done. The bytes belong to the decoder
248- /// until the next `decode`/`flush`.
249- pub(super) fn get_frame<'a>(
250- &self,
251- ctx: &'a mut VpxCtx,
252- iter: &mut *const c_void,
253- ) -> Option<&'a VpxImage> {
254- // SAFETY: the context is live; the returned pointer is libvpx's own
255- // image, borrowed for exactly as long as the lifetime above says.
256- unsafe { (self.get_frame)(ctx, iter).as_ref() }
72+ /// `flush`: end of the track. VP8 queues nothing -- one unit in, at
73+ /// most one picture out, feed order -- so there is nothing held back to
74+ /// drain; the answer is always ok, immediately.
75+ pub(super) fn flush(&mut self) -> Result<(), String> {
76+ Ok(())
25777 }
258-
259- /// `vpx_codec_destroy`: the context's half of `VpxSession`'s `Drop`.
260- pub(super) fn destroy(&self, ctx: &mut VpxCtx) {
261- // SAFETY: called once per live context; libvpx frees what the context
262- // holds, and a context whose init never completed reads as an
263- // `INVALID_PARAM` refusal rather than a crash.
264- unsafe { (self.destroy)(ctx) };
265- }
266-}
267-
268-/// VP8's row-based multithreaded decode stops paying past a handful of
269-/// threads -- the row sync costs what the parallelism buys -- so this is not
270-/// the machine's whole core count.
271-pub(super) fn threads() -> c_uint {
272- std::thread::available_parallelism().map_or(1, |n| n.get().min(4) as c_uint)
27378 }
crates/engine/src/caps.rsmodified+10 -9
@@ -12,12 +12,13 @@
1212 //! driver all read the same way -- "software only" -- because that is what they
1313 //! mean for the file a user is about to write.
1414 //!
15-//! VP8 is deliberately on neither line: the plugin decodes it on libvpx,
16-//! dlopen'd at runtime, and reports `CAP_VP8` for it -- the plugin's seat,
17-//! never the GPU's. [`HW_CODECS`] does not carry the bit, because this file's
18-//! one job is not presenting a seat as a GPU feat that no GPU has.
15+//! VP8 is deliberately on neither line: the plugin decodes it on its own
16+//! native `ec-vp8`, always present, and reports `CAP_VP8` for it -- the
17+//! plugin's seat, never the GPU's. [`HW_CODECS`] does not carry the bit,
18+//! because this file's one job is not presenting a seat as a GPU feat that
19+//! no GPU has.
1920
20-use crate::hw::{VhCaps, CAP_AV1, CAP_H264, CAP_HEVC, CAP_VP9};
21+use crate::hw::{CAP_AV1, CAP_H264, CAP_HEVC, CAP_VP9, VhCaps};
2122
2223 /// The codecs a hardware line can name, in the order it lists them.
2324 const HW_CODECS: [(u32, &str); 4] = [
@@ -136,10 +137,10 @@ mod tests {
136137 assert_eq!(hw_line(Some(h264_only)), "H.264 dec");
137138 }
138139
139- /// The plugin's VP8 bit is its own libvpx software decoder. However lit
140- /// the mask is, the hardware line never reads it as a GPU seat -- and a
141- /// mask holding nothing *but* it still reads as the nothing a driver
142- /// without seats would answer.
140+ /// The plugin's VP8 bit is its own native decoder (`ec-vp8`), lit
141+ /// unconditionally. However lit the mask is, the hardware line never
142+ /// reads it as a GPU seat -- and a mask holding nothing *but* it still
143+ /// reads as the nothing a driver without seats would answer.
143144 #[test]
144145 fn the_plugin_s_software_vp8_never_reads_as_a_gpu_seat() {
145146 let vpx_only = VhCaps {
crates/engine/src/decode.rsmodified+2 -2
@@ -1016,8 +1016,8 @@ fn run_span(
10161016 }
10171017 // ...except where there is nothing to fall back to. Feeding HEVC, VP9 or
10181018 // VP8 bytes to either software seat would be garbage, not a fallback --
1019- // VP8's software decoder is libvpx, inside the plugin. H.264 and AV1 each
1020- // have one of this project's own: `ec-h264`, `ec-av1`.
1019+ // VP8's decoder is `ec-vp8`, inside the plugin. H.264 and AV1 each
1020+ // have one of this project's own here: `ec-h264`, `ec-av1`.
10211021 match opened.meta.codec {
10221022 Codec::H264 => {
10231023 eprintln!("decode backend: software (ec-h264)");
crates/engine/src/demux.rsmodified+6 -6
@@ -110,8 +110,8 @@ pub fn cancelled_read_error() -> crate::Error {
110110 CANCELLED.into()
111111 }
112112
113-/// What the video track is coded with. Not a decoder choice by itself: only
114-/// H.264 has a software decoder in this binary, and VP8's (libvpx) lives
113+/// What the video track is coded with. Not a decoder choice by itself: H.264
114+/// and AV1 have software decoders in this binary, and VP8's (`ec-vp8`) lives
115115 /// inside the plugin -- which is what [`Codec::needs_plugin`] says for the
116116 /// rest, per codec, in the words that stay true.
117117 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -140,9 +140,9 @@ impl Codec {
140140 /// own (`ec-h264`, `ec-av1`) and are never refused for missing hardware on
141141 /// the *playback* path; HEVC and VP9 have no software decoder at all, so
142142 /// the refusal names the plugin as the only way. VP8's software decoder
143- /// *is* libvpx inside the plugin, so "there is no software decoder" would
144- /// be a lie the day the plugin grew that arm -- the refusal keeps naming
145- /// the plugin, because that is still the thing to make present. AV1's arm
143+ /// *is* this project's own `ec-vp8`, inside the plugin, so "there is no
144+ /// software decoder" would be a lie -- the refusal keeps naming the
145+ /// plugin, because that is still the thing to make present. AV1's arm
146146 /// is the one refusal left that can reach it: the software *export*
147147 /// decoder is H.264-only (`export::SwDecoder`), so an AV1 source on a
148148 /// machine with no plugin exports through nothing. Shared so playback and
@@ -150,7 +150,7 @@ impl Codec {
150150 pub fn needs_plugin(self) -> String {
151151 match self {
152152 Self::Vp8 => {
153- "VP8 needs the VA-API plugin (libengine_hw.so) — its decoder is libvpx, inside the plugin"
153+ "VP8 needs the VA-API plugin (libengine_hw.so) — its decoder is ec-vp8, inside the plugin"
154154 .to_string()
155155 }
156156 Self::Av1 => {
crates/engine/src/hw.rsmodified+6 -5
@@ -178,10 +178,11 @@ pub const CAP_HEVC: u32 = 1 << 1;
178178 pub const CAP_VP9: u32 = 1 << 2;
179179 pub const CAP_AV1: u32 = 1 << 3;
180180
181-/// VP8 decodes on libvpx inside the plugin -- software, dlopen'd at runtime,
182-/// no GPU and no VA-API profile anywhere in it -- so this bit is the
183-/// *plugin's* seat, never the driver's. It lights up [`VhCaps::decode`] when
184-/// the library is present; the hardware line in [`crate::caps`] deliberately
181+/// VP8 decodes on `ec-vp8` inside the plugin -- software, linked into the
182+/// plugin itself, no GPU and no VA-API profile anywhere in it -- so this bit
183+/// is the *plugin's* seat, never the driver's, and it lights up
184+/// [`VhCaps::decode`] unconditionally: there is no library whose presence the
185+/// bit depends on any more. The hardware line in [`crate::caps`] deliberately
185186 /// keeps it off, being a line about what the GPU takes.
186187 pub const CAP_VP8: u32 = 1 << 4;
187188
@@ -263,7 +264,7 @@ fn load() -> Option<Plugin> {
263264 /// "software only" for all of them, which is what they mean.
264265 ///
265266 /// VP8 sits outside that reading on a driverless machine: its decoder is
266-/// libvpx inside the plugin, so the plugin may open a VP8 file where this
267+/// `ec-vp8` inside the plugin, so the plugin may open a VP8 file where this
267268 /// table answers `None`. Nothing here promises otherwise -- the decode path
268269 /// asks the plugin, not this table -- and the one display-dependent half the
269270 /// front-end draws from this stays exactly as truthful as it was.
crates/engine/tests/vp8.rsmodified+9 -9
@@ -1,7 +1,7 @@
11 //! VP8 import: the container half, and the decode half -- software, on the
2-//! libvpx the plugin dlopens (`engine-hw`'s `vpx.rs`), which is why these
3-//! twins need a built `libengine_hw.so` and a `libvpx.so` but **no VA-API
4-//! VP8 profile**: no such profile exists on this GPU, and none is asked for.
2+//! plugin's own native decoder (`ec-vp8`, `engine-hw`'s `vpx.rs`), which is
3+//! why these twins need a built `libengine_hw.so` but **no VA-API VP8
4+//! profile**: no such profile exists on this GPU, and none is asked for.
55 //!
66 //! ```text
77 //! cargo build -p engine -p engine-hw --release
@@ -55,9 +55,9 @@ fn the_demuxer_reports_a_vp8_track() {
5555 /// There is no software VP8 decoder *in this binary*, so the software path
5656 /// must refuse by name rather than feed VP8 bytes to `ec-h264` -- and it
5757 /// must refuse where a caller can still show it, i.e. out of `open`, not from
58-/// inside the worker. The decoder the refusal names is the plugin's libvpx
59-/// arm, which is why the sentence names the plugin and not a nonexistent
60-/// absence.
58+/// inside the worker. The decoder the refusal names is the plugin's own
59+/// (`ec-vp8`), which is why the sentence names the plugin and not a
60+/// nonexistent absence.
6161 #[test]
6262 fn the_software_path_refuses_vp8_by_name() {
6363 // SAFETY: the suite is documented to run with --test-threads=1.
@@ -73,10 +73,10 @@ fn the_software_path_refuses_vp8_by_name() {
7373 }
7474
7575 /// The end-to-end user path: opening the file yields pictures, all of them,
76-/// through the plugin's libvpx arm. The `.webm` is the file this decodes,
76+/// through the plugin's VP8 arm. The `.webm` is the file this decodes,
7777 /// the container half being the Matroska demuxer's `V_VP8` row.
7878 #[test]
79-#[ignore = "needs a built libengine_hw.so and libvpx.so.9 -- no VA-API profile involved"]
79+#[ignore = "needs a built libengine_hw.so -- no VA-API profile involved"]
8080 fn the_plugin_decodes_every_vp8_frame() {
8181 let start = Instant::now();
8282 let (meta, frames) = DecodeSession::open(asset("test_vp8.webm")).expect("open test_vp8.webm");
@@ -117,7 +117,7 @@ fn the_plugin_decodes_every_vp8_frame() {
117117 /// Export re-encodes to H.264 whatever the source was coded with: the packet
118118 /// copy path is audio-only, so no VP8 byte can reach an `avc1` track.
119119 #[test]
120-#[ignore = "needs a built libengine_hw.so and libvpx.so.9 -- no VA-API profile involved"]
120+#[ignore = "needs a built libengine_hw.so -- no VA-API profile involved"]
121121 fn a_vp8_source_exports_as_h264() {
122122 let session = PlaybackSession::open(asset("test_vp8.webm")).expect("open test_vp8.webm");
123123 let meta = *session.meta();