forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

decode_bench.rs87 lines, 3.1 KiBHistoryRawDownload
1//! Decode one AV1 OBU stream (low-overhead, `ffmpeg -f obu`) and report the
2//! decode rate, for the 1080p30 realtime criterion in DONE.md.
3//!
4//! ```text
5//! cargo run --release -p ec-av1 --example decode_bench -- s.obu [--threads N]
6//! cargo run --release -p ec-av1 --example decode_bench -- s.obu --threads N --raw | md5sum
7//! ```
8//!
9//! Without `--threads` the decoder's own default is used (`EC_AV1_THREADS`, or
10//! the core-count default). `--raw` writes every shown frame as 8-bit planar
11//! YUV to stdout instead of timing only, so its md5 can be compared with
12//! `ffmpeg -i s.obu -f rawvideo -pix_fmt yuv420p - | md5sum`. Timing goes to
13//! stderr in both modes.
14use std::io::Write;
15use std::time::Instant;
16
17fn main() {
18 let mut args = std::env::args().skip(1);
19 let Some(path) = args.next() else {
20 eprintln!("usage: decode_bench <stream.obu> [--threads N] [--raw]");
21 std::process::exit(2);
22 };
23 let mut threads: Option<usize> = None;
24 let mut raw = false;
25 while let Some(arg) = args.next() {
26 match arg.as_str() {
27 "--threads" => {
28 threads = args.next().and_then(|v| v.parse().ok());
29 if threads.is_none() {
30 eprintln!("--threads needs a number");
31 std::process::exit(2);
32 }
33 }
34 "--raw" => raw = true,
35 other => {
36 eprintln!("unknown argument: {other}");
37 std::process::exit(2);
38 }
39 }
40 }
41 let data = match std::fs::read(&path) {
42 Ok(data) => data,
43 Err(e) => {
44 eprintln!("{path}: {e}");
45 std::process::exit(2);
46 }
47 };
48 let mut out = std::io::BufWriter::new(std::io::stdout().lock());
49 let mut frames = 0usize;
50 let mut pixels = 0usize;
51 let start = Instant::now();
52 let mut sink = |picture: &ec_av1::encode::Picture, _decode_idx: usize, shown: bool| {
53 if !shown {
54 return Ok(());
55 }
56 frames += 1;
57 pixels += picture.width * picture.height;
58 if raw {
59 for plane in [&picture.y, &picture.u, &picture.v] {
60 let bytes: Vec<u8> = plane.iter().map(|&s| s as u8).collect();
61 out.write_all(&bytes).map_err(|e| {
62 ec_core::Error::unsupported("decode_bench stdout", e.to_string())
63 })?;
64 }
65 }
66 Ok(())
67 };
68 let result = match threads {
69 Some(n) => ec_av1::stream::decode_stream_with_threads(&data, n, &mut sink),
70 None => ec_av1::stream::decode_stream_with(&data, &mut sink),
71 };
72 if let Err(e) = result {
73 eprintln!("decode failed: {e}");
74 std::process::exit(1);
75 }
76 let _ = out.flush();
77 let secs = start.elapsed().as_secs_f64();
78 eprintln!(
79 "threads={} frames={frames} secs={secs:.3} fps={:.2} mp={:.2}",
80 threads.map_or_else(
81 || std::env::var("EC_AV1_THREADS").unwrap_or_else(|_| "default".to_string()),
82 |n| n.to_string(),
83 ),
84 frames as f64 / secs,
85 pixels as f64 / 1e6,
86 );
87}