//! Background decode worker. Frames leave over a bounded channel so the caller's //! thread (the UI thread) never blocks on decode and memory stays capped. use std::path::{Path, PathBuf}; use std::sync::mpsc::{Receiver, SyncSender, sync_channel}; use std::thread; use rusty_h264::Decoder; use crate::convert::i420_to_bgra; use crate::demux::{Demuxer, VideoMeta}; use crate::hw::HwSession; /// One decoded picture, ready to hand to a renderer. pub struct Frame { /// Position in decode order, starting at 0. pub index: u32, pub width: u32, pub height: u32, /// BGRA8, straight alpha, tightly packed. pub bgra: Vec, } pub struct DecodeSession; impl DecodeSession { /// Opens `path`, returning stream metadata immediately and a receiver that /// yields frames as the worker decodes them. The worker stops when the /// receiver is dropped, at end of stream, or on the first decode error. pub fn open(path: impl AsRef) -> crate::Result<(VideoMeta, Receiver)> { let path = path.as_ref().to_path_buf(); let (meta, demuxer) = Demuxer::open(&path)?; // Small bound: a 720p BGRA frame is ~3.5 MB, so we must not let the // decoder run ahead of the display without limit. let (tx, rx) = sync_channel(2); thread::Builder::new() .name("decode".into()) .spawn(move || { // The plugin has to be opened on the thread that uses it: its // VA-API state is not `Send`-safe across a later hand-off. if let Some(hw) = open_hw(&path) { eprintln!("decode backend: hardware (VA-API plugin)"); if run_hw(hw, &tx) { return; } // A driver that opens but cannot decode a single frame is // still a fallback case, not a dead session. eprintln!("hardware decode failed before any frame, falling back to software"); } eprintln!("decode backend: software (rusty_h264)"); run(demuxer, tx) })?; Ok((meta, rx)) } } /// `None` when the software path must be used: forced by `VE_SW=1`, or the /// plugin is absent/broken/unable to open this particular file. fn open_hw(path: &PathBuf) -> Option { if std::env::var_os("VE_SW").is_some_and(|v| v == "1") { return None; } HwSession::open(path) } /// Returns whether the session was handled; `false` only when hardware decode /// failed without emitting a single frame, so software can still take over. fn run_hw(mut hw: HwSession, tx: &SyncSender) -> bool { let mut index = 0u32; loop { match hw.next_frame() { Ok(Some((y, u, v, width, height))) => { let frame = Frame { index, width, height, bgra: i420_to_bgra(y, u, v, width as usize, height as usize), }; index += 1; if tx.send(frame).is_err() { return true; // consumer went away } } Ok(None) => return true, Err(e) => { if index == 0 { return false; } eprintln!("hardware decode error at frame {index}: {e}"); return true; } } } } fn run(mut demuxer: Demuxer, tx: SyncSender) { let mut decoder = Decoder::new(); let mut index = 0u32; // ponytail: emits pictures in decode order. Fine for Baseline (no B-frames); // reordering streams need POC-sorted output before display. loop { let au = match demuxer.next_access_unit() { Ok(Some(au)) => au, Ok(None) => break, Err(e) => { eprintln!("demux error: {e}"); break; } }; let yuv = match decoder.decode(&au) { Ok(Some(yuv)) => yuv, Ok(None) => continue, Err(e) => { eprintln!("decode error at access unit {index}: {e}"); break; } }; let frame = Frame { index, width: yuv.width as u32, height: yuv.height as u32, bgra: i420_to_bgra(&yuv.y, &yuv.u, &yuv.v, yuv.width, yuv.height), }; index += 1; if tx.send(frame).is_err() { break; // consumer went away } } }