| 1 | |
| 2 | |
| 3 | |
| 4 | use std::path::{Path, PathBuf}; |
| 5 | use std::sync::Arc; |
| 6 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 7 | use std::sync::mpsc::{Receiver, SyncSender, sync_channel}; |
| 8 | use std::thread; |
| 9 | |
| 10 | use rusty_h264::Decoder; |
| 11 | |
| 12 | use crate::convert::i420_to_bgra; |
| 13 | use crate::demux::{Demuxer, VideoMeta}; |
| 14 | use crate::hw::HwSession; |
| 15 | |
| 16 | |
| 17 | pub struct Frame { |
| 18 | |
| 19 | pub index: u32, |
| 20 | pub width: u32, |
| 21 | pub height: u32, |
| 22 | |
| 23 | pub bgra: Vec<u8>, |
| 24 | } |
| 25 | |
| 26 | pub struct DecodeSession; |
| 27 | |
| 28 | impl DecodeSession { |
| 29 | |
| 30 | |
| 31 | |
| 32 | pub fn open(path: impl AsRef<Path>) -> crate::Result<(VideoMeta, Receiver<Frame>)> { |
| 33 | let (meta, rx, _cancel) = Self::open_at(path, 0)?; |
| 34 | Ok((meta, rx)) |
| 35 | } |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | pub fn open_at( |
| 42 | path: impl AsRef<Path>, |
| 43 | start_frame: u32, |
| 44 | ) -> crate::Result<(VideoMeta, Receiver<Frame>, Arc<AtomicBool>)> { |
| 45 | |
| 46 | |
| 47 | Self::open_range(path, start_frame, u32::MAX) |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | pub fn open_range( |
| 56 | path: impl AsRef<Path>, |
| 57 | start_frame: u32, |
| 58 | end_frame: u32, |
| 59 | ) -> crate::Result<(VideoMeta, Receiver<Frame>, Arc<AtomicBool>)> { |
| 60 | let path = path.as_ref().to_path_buf(); |
| 61 | let (meta, demuxer) = Demuxer::open(&path)?; |
| 62 | let end_frame = end_frame.min(meta.frame_count); |
| 63 | |
| 64 | |
| 65 | let (tx, rx) = sync_channel(2); |
| 66 | let cancel = Arc::new(AtomicBool::new(false)); |
| 67 | if end_frame <= start_frame { |
| 68 | |
| 69 | |
| 70 | return Ok((meta, rx, cancel)); |
| 71 | } |
| 72 | let worker_cancel = Arc::clone(&cancel); |
| 73 | thread::Builder::new() |
| 74 | .name("decode".into()) |
| 75 | .spawn(move || { |
| 76 | |
| 77 | |
| 78 | if let Some(hw) = open_hw(&path, start_frame) { |
| 79 | eprintln!("decode backend: hardware (VA-API plugin)"); |
| 80 | if run_hw(hw, &tx, start_frame, end_frame, &worker_cancel) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | eprintln!("hardware decode failed before any frame, falling back to software"); |
| 86 | } |
| 87 | eprintln!("decode backend: software (rusty_h264)"); |
| 88 | run(demuxer, tx, start_frame, end_frame, &worker_cancel) |
| 89 | })?; |
| 90 | Ok((meta, rx, cancel)) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | fn open_hw(path: &PathBuf, start_frame: u32) -> Option<HwSession> { |
| 97 | if std::env::var_os("VE_SW").is_some_and(|v| v == "1") { |
| 98 | return None; |
| 99 | } |
| 100 | HwSession::open_at(path, start_frame) |
| 101 | } |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | fn run_hw( |
| 110 | mut hw: HwSession, |
| 111 | tx: &SyncSender<Frame>, |
| 112 | start_frame: u32, |
| 113 | end_frame: u32, |
| 114 | cancel: &AtomicBool, |
| 115 | ) -> bool { |
| 116 | let mut index = start_frame; |
| 117 | loop { |
| 118 | if cancel.load(Ordering::Relaxed) { |
| 119 | return true; |
| 120 | } |
| 121 | match hw.next_frame() { |
| 122 | Ok(Some((y, u, v, width, height))) => { |
| 123 | let frame = Frame { |
| 124 | index, |
| 125 | width, |
| 126 | height, |
| 127 | bgra: i420_to_bgra(y, u, v, width as usize, height as usize), |
| 128 | }; |
| 129 | index += 1; |
| 130 | if tx.send(frame).is_err() { |
| 131 | return true; |
| 132 | } |
| 133 | if index >= end_frame { |
| 134 | return true; |
| 135 | } |
| 136 | } |
| 137 | Ok(None) => return true, |
| 138 | Err(e) => { |
| 139 | if index == start_frame { |
| 140 | return false; |
| 141 | } |
| 142 | eprintln!("hardware decode error at frame {index}: {e}"); |
| 143 | return true; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | fn run( |
| 150 | mut demuxer: Demuxer, |
| 151 | tx: SyncSender<Frame>, |
| 152 | start_frame: u32, |
| 153 | end_frame: u32, |
| 154 | cancel: &AtomicBool, |
| 155 | ) { |
| 156 | let mut decoder = Decoder::new(); |
| 157 | |
| 158 | |
| 159 | |
| 160 | let sync = demuxer.seek_to_sync_at_or_before(start_frame.saturating_add(1)); |
| 161 | let mut index = sync - 1; |
| 162 | |
| 163 | |
| 164 | |
| 165 | loop { |
| 166 | if cancel.load(Ordering::Relaxed) { |
| 167 | break; |
| 168 | } |
| 169 | let au = match demuxer.next_access_unit() { |
| 170 | Ok(Some(au)) => au, |
| 171 | Ok(None) => break, |
| 172 | Err(e) => { |
| 173 | eprintln!("demux error: {e}"); |
| 174 | break; |
| 175 | } |
| 176 | }; |
| 177 | let yuv = match decoder.decode(&au) { |
| 178 | Ok(Some(yuv)) => yuv, |
| 179 | Ok(None) => continue, |
| 180 | Err(e) => { |
| 181 | eprintln!("decode error at access unit {index}: {e}"); |
| 182 | break; |
| 183 | } |
| 184 | }; |
| 185 | if index < start_frame { |
| 186 | index += 1; |
| 187 | continue; |
| 188 | } |
| 189 | let frame = Frame { |
| 190 | index, |
| 191 | width: yuv.width as u32, |
| 192 | height: yuv.height as u32, |
| 193 | bgra: i420_to_bgra(&yuv.y, &yuv.u, &yuv.v, yuv.width, yuv.height), |
| 194 | }; |
| 195 | index += 1; |
| 196 | if tx.send(frame).is_err() { |
| 197 | break; |
| 198 | } |
| 199 | if index >= end_frame { |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | } |