| 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 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | pub(crate) struct Worker { |
| 32 | cancel: Arc<AtomicBool>, |
| 33 | |
| 34 | |
| 35 | handle: Option<thread::JoinHandle<()>>, |
| 36 | } |
| 37 | |
| 38 | impl Worker { |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | pub(crate) fn cancel(&self) { |
| 44 | self.cancel.store(true, Ordering::Release); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | pub(crate) fn is_finished(&self) -> bool { |
| 51 | self.handle |
| 52 | .as_ref() |
| 53 | .is_none_or(thread::JoinHandle::is_finished) |
| 54 | } |
| 55 | |
| 56 | |
| 57 | |
| 58 | fn detach(mut self) -> Arc<AtomicBool> { |
| 59 | self.handle = None; |
| 60 | Arc::clone(&self.cancel) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | impl Drop for Worker { |
| 65 | fn drop(&mut self) { |
| 66 | if let Some(handle) = self.handle.take() { |
| 67 | self.cancel(); |
| 68 | let _ = handle.join(); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub struct DecodeSession; |
| 74 | |
| 75 | impl DecodeSession { |
| 76 | |
| 77 | |
| 78 | |
| 79 | pub fn open(path: impl AsRef<Path>) -> crate::Result<(VideoMeta, Receiver<Frame>)> { |
| 80 | let (meta, rx, _cancel) = Self::open_at(path, 0)?; |
| 81 | Ok((meta, rx)) |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | pub fn open_at( |
| 89 | path: impl AsRef<Path>, |
| 90 | start_frame: u32, |
| 91 | ) -> crate::Result<(VideoMeta, Receiver<Frame>, Arc<AtomicBool>)> { |
| 92 | |
| 93 | |
| 94 | Self::open_range(path, start_frame, u32::MAX) |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | pub fn open_range( |
| 103 | path: impl AsRef<Path>, |
| 104 | start_frame: u32, |
| 105 | end_frame: u32, |
| 106 | ) -> crate::Result<(VideoMeta, Receiver<Frame>, Arc<AtomicBool>)> { |
| 107 | let (meta, rx, worker) = Self::open_worker(path, start_frame, end_frame)?; |
| 108 | Ok((meta, rx, worker.detach())) |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | pub(crate) fn open_black(width: u32, height: u32, len: u32) -> (Receiver<Frame>, Worker) { |
| 117 | let (tx, rx) = sync_channel(2); |
| 118 | let cancel = Arc::new(AtomicBool::new(false)); |
| 119 | if len == 0 { |
| 120 | return ( |
| 121 | rx, |
| 122 | Worker { |
| 123 | cancel, |
| 124 | handle: None, |
| 125 | }, |
| 126 | ); |
| 127 | } |
| 128 | let worker_cancel = Arc::clone(&cancel); |
| 129 | |
| 130 | |
| 131 | let black = vec![[0u8, 0, 0, 255]; (width as usize) * (height as usize)].concat(); |
| 132 | let handle = thread::Builder::new() |
| 133 | .name("black".into()) |
| 134 | .spawn(move || { |
| 135 | for index in 0..len { |
| 136 | if worker_cancel.load(Ordering::Relaxed) { |
| 137 | return; |
| 138 | } |
| 139 | let frame = Frame { |
| 140 | index, |
| 141 | width, |
| 142 | height, |
| 143 | bgra: black.clone(), |
| 144 | }; |
| 145 | if tx.send(frame).is_err() { |
| 146 | return; |
| 147 | } |
| 148 | } |
| 149 | }) |
| 150 | .ok(); |
| 151 | (rx, Worker { cancel, handle }) |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | pub(crate) fn open_worker( |
| 158 | path: impl AsRef<Path>, |
| 159 | start_frame: u32, |
| 160 | end_frame: u32, |
| 161 | ) -> crate::Result<(VideoMeta, Receiver<Frame>, Worker)> { |
| 162 | let path = path.as_ref().to_path_buf(); |
| 163 | let (meta, demuxer) = Demuxer::open(&path)?; |
| 164 | let end_frame = end_frame.min(meta.frame_count); |
| 165 | |
| 166 | |
| 167 | let (tx, rx) = sync_channel(2); |
| 168 | let cancel = Arc::new(AtomicBool::new(false)); |
| 169 | if end_frame <= start_frame { |
| 170 | |
| 171 | |
| 172 | return Ok(( |
| 173 | meta, |
| 174 | rx, |
| 175 | Worker { |
| 176 | cancel, |
| 177 | handle: None, |
| 178 | }, |
| 179 | )); |
| 180 | } |
| 181 | let worker_cancel = Arc::clone(&cancel); |
| 182 | let handle = thread::Builder::new() |
| 183 | .name("decode".into()) |
| 184 | .spawn(move || { |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | if worker_cancel.load(Ordering::Relaxed) { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | if let Some(hw) = open_hw(&path, start_frame) { |
| 195 | |
| 196 | |
| 197 | if worker_cancel.load(Ordering::Relaxed) { |
| 198 | return; |
| 199 | } |
| 200 | eprintln!("decode backend: hardware (VA-API plugin)"); |
| 201 | if run_hw(hw, &tx, start_frame, end_frame, &worker_cancel) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | eprintln!("hardware decode failed before any frame, falling back to software"); |
| 207 | } |
| 208 | eprintln!("decode backend: software (rusty_h264)"); |
| 209 | run(demuxer, tx, start_frame, end_frame, &worker_cancel) |
| 210 | })?; |
| 211 | Ok(( |
| 212 | meta, |
| 213 | rx, |
| 214 | Worker { |
| 215 | cancel, |
| 216 | handle: Some(handle), |
| 217 | }, |
| 218 | )) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | |
| 224 | fn open_hw(path: &PathBuf, start_frame: u32) -> Option<HwSession> { |
| 225 | if std::env::var_os("VE_SW").is_some_and(|v| v == "1") { |
| 226 | return None; |
| 227 | } |
| 228 | HwSession::open_at(path, start_frame) |
| 229 | } |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | fn run_hw( |
| 238 | mut hw: HwSession, |
| 239 | tx: &SyncSender<Frame>, |
| 240 | start_frame: u32, |
| 241 | end_frame: u32, |
| 242 | cancel: &AtomicBool, |
| 243 | ) -> bool { |
| 244 | let mut index = start_frame; |
| 245 | loop { |
| 246 | if cancel.load(Ordering::Relaxed) { |
| 247 | return true; |
| 248 | } |
| 249 | match hw.next_frame() { |
| 250 | Ok(Some((y, u, v, width, height))) => { |
| 251 | let frame = Frame { |
| 252 | index, |
| 253 | width, |
| 254 | height, |
| 255 | bgra: i420_to_bgra(y, u, v, width as usize, height as usize), |
| 256 | }; |
| 257 | index += 1; |
| 258 | if tx.send(frame).is_err() { |
| 259 | return true; |
| 260 | } |
| 261 | if index >= end_frame { |
| 262 | return true; |
| 263 | } |
| 264 | } |
| 265 | Ok(None) => return true, |
| 266 | Err(e) => { |
| 267 | if index == start_frame { |
| 268 | return false; |
| 269 | } |
| 270 | eprintln!("hardware decode error at frame {index}: {e}"); |
| 271 | return true; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | fn run( |
| 278 | mut demuxer: Demuxer, |
| 279 | tx: SyncSender<Frame>, |
| 280 | start_frame: u32, |
| 281 | end_frame: u32, |
| 282 | cancel: &AtomicBool, |
| 283 | ) { |
| 284 | let mut decoder = Decoder::new(); |
| 285 | |
| 286 | |
| 287 | |
| 288 | let sync = demuxer.seek_to_sync_at_or_before(start_frame.saturating_add(1)); |
| 289 | let mut index = sync - 1; |
| 290 | |
| 291 | |
| 292 | |
| 293 | loop { |
| 294 | if cancel.load(Ordering::Relaxed) { |
| 295 | break; |
| 296 | } |
| 297 | let au = match demuxer.next_access_unit() { |
| 298 | Ok(Some(au)) => au, |
| 299 | Ok(None) => break, |
| 300 | Err(e) => { |
| 301 | eprintln!("demux error: {e}"); |
| 302 | break; |
| 303 | } |
| 304 | }; |
| 305 | let yuv = match decoder.decode(&au) { |
| 306 | Ok(Some(yuv)) => yuv, |
| 307 | Ok(None) => continue, |
| 308 | Err(e) => { |
| 309 | eprintln!("decode error at access unit {index}: {e}"); |
| 310 | break; |
| 311 | } |
| 312 | }; |
| 313 | if index < start_frame { |
| 314 | index += 1; |
| 315 | continue; |
| 316 | } |
| 317 | let frame = Frame { |
| 318 | index, |
| 319 | width: yuv.width as u32, |
| 320 | height: yuv.height as u32, |
| 321 | bgra: i420_to_bgra(&yuv.y, &yuv.u, &yuv.v, yuv.width, yuv.height), |
| 322 | }; |
| 323 | index += 1; |
| 324 | if tx.send(frame).is_err() { |
| 325 | break; |
| 326 | } |
| 327 | if index >= end_frame { |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | } |