forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

dec.rs54 lines, 2.6 KiBHistoryRawDownload
1//! Decode a ProRes `.mov`/`.mp4`: print the facts, optionally write raw planar frames
2//! (video planes, then 16-bit alpha when present) and measure throughput.
3//! `dec in.mov [out.raw] ` with env `THREADS`, `REPS`, `QUIET`.
4use std::io::{BufReader, Write};
5use std::time::Instant;
6
7use ec_core::registry::{CodecId, CodecParameters, Demuxer};
8use ec_mp4::Mp4Demuxer;
9use ec_prores::ProResDecoder;
10
11fn main() {
12 let args: Vec<String> = std::env::args().collect();
13 let reps: usize = std::env::var("REPS").ok().and_then(|v| v.parse().ok()).unwrap_or(1);
14 let threads: usize = std::env::var("THREADS").ok().and_then(|v| v.parse().ok()).unwrap_or(1);
15 let quiet = std::env::var_os("QUIET").is_some();
16 let mut out = args.get(2).map(|p| std::fs::File::create(p).expect("create"));
17 let mut frames = 0usize;
18 let t0 = Instant::now();
19 for _ in 0..reps {
20 let mut dm = Mp4Demuxer::new(BufReader::new(std::fs::File::open(&args[1]).unwrap())).unwrap();
21 let vs = dm.streams().iter().position(|s| s.params.codec == CodecId::ProRes).expect("no ProRes stream") as u32;
22 let mut dec = ProResDecoder::new(CodecParameters::new(CodecId::ProRes)).unwrap();
23 dec.set_threads(threads);
24 while let Ok(p) = dm.next_packet() {
25 if p.stream != vs {
26 continue;
27 }
28 let f = dec.decode_full(&p.data).unwrap_or_else(|e| panic!("frame {frames}: {e}"));
29 frames += 1;
30 if !quiet {
31 println!("{:?} {}x{} {:?} alpha {:?} {:?}", f.video.format, f.video.width, f.video.height, f.video.interlace, f.alpha.as_ref().map(|_| f.info.alpha_bits), f.info);
32 }
33 if let Some(o) = out.as_mut() {
34 let (w, h) = (f.video.width as usize, f.video.height as usize);
35 let (sx, sy) = f.video.format.chroma_shift();
36 for (i, pl) in f.video.planes.iter().enumerate() {
37 let (pw, ph) = if i == 0 { (w, h) } else { (w.div_ceil(1 << sx), h.div_ceil(1 << sy)) };
38 for y in 0..ph {
39 o.write_all(&pl.data[y * pl.stride..y * pl.stride + pw * 2]).unwrap();
40 }
41 }
42 if let Some(a) = &f.alpha {
43 for y in 0..h {
44 for x in 0..w {
45 o.write_all(&a.data[y * a.stride + x].to_le_bytes()).unwrap();
46 }
47 }
48 }
49 }
50 }
51 }
52 let dt = t0.elapsed().as_secs_f64();
53 eprintln!("{frames} frames in {dt:.3}s = {:.1} fps", frames as f64 / dt);
54}