forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

decode_bench.rs100 lines, 3.3 KiBHistoryRawDownload
1//! Decode benchmark for ec-h265: one raw Annex-B H.265 file in, ms/frame out.
2//!
3//! Run (release, from the repo root; fixtures from `scripts/gen-hevcperf-fixtures.sh`):
4//!
5//! ```text
6//! cargo run -p ec-h265 --release --example decode_bench -- \
7//! ~/.cache/hevcperf-fixtures/hevc1080_moving.hevc
8//! ```
9//!
10//! Decodes the whole file `EC_BENCH_RUNS` times (default 3) and prints the best run
11//! as ms/frame and the realtime factor against the stream's frame rate (30 fps unless
12//! `EC_BENCH_FPS` says otherwise). The FNV-1a checksum over every output plane is
13//! printed too, so a speed change that alters any sample shows up here before the
14//! conformance suite runs.
15
16use std::time::Instant;
17
18use ec_h265::Decoder;
19
20fn fnv1a(hash: &mut u64, samples: &[u16]) {
21 for &s in samples {
22 for b in s.to_le_bytes() {
23 *hash ^= u64::from(b);
24 *hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
25 }
26 }
27}
28
29/// Decode the whole stream once. Returns the frame count, and the output checksum when
30/// `want_hash` is set. The checksum is kept out of the timed runs (it costs more than some
31/// stages), so the timed runs only time the decoder.
32fn decode_once(data: &[u8], want_hash: bool) -> Result<(usize, u64), String> {
33 let mut dec = Decoder::new();
34 dec.decode_annex_b(data)
35 .map_err(|e| format!("decode: {e}"))?;
36 dec.flush().map_err(|e| format!("flush: {e}"))?;
37 let mut frames = 0usize;
38 let mut hash = 0xcbf2_9ce4_8422_2325u64;
39 while let Some(frame) = dec.next_frame() {
40 if want_hash {
41 let p = &frame.picture;
42 fnv1a(&mut hash, &p.y);
43 fnv1a(&mut hash, &p.cb);
44 fnv1a(&mut hash, &p.cr);
45 }
46 frames += 1;
47 }
48 Ok((frames, hash))
49}
50
51fn main() {
52 let Some(path) = std::env::args().nth(1) else {
53 eprintln!("usage: decode_bench <file.hevc>");
54 std::process::exit(2);
55 };
56 let runs: usize = std::env::var("EC_BENCH_RUNS")
57 .ok()
58 .and_then(|v| v.parse().ok())
59 .unwrap_or(3)
60 .max(1);
61 let fps: f64 = std::env::var("EC_BENCH_FPS")
62 .ok()
63 .and_then(|v| v.parse().ok())
64 .unwrap_or(30.0);
65 let data = match std::fs::read(&path) {
66 Ok(d) => d,
67 Err(e) => {
68 eprintln!("decode_bench: cannot read {path}: {e}");
69 std::process::exit(2);
70 }
71 };
72
73 let mut best = f64::INFINITY;
74 let mut frames = 0usize;
75 for _ in 0..runs {
76 let t = Instant::now();
77 match decode_once(&data, false) {
78 Ok((n, _)) => frames = n,
79 Err(e) => {
80 eprintln!("decode_bench: {e}");
81 std::process::exit(1);
82 }
83 }
84 best = best.min(t.elapsed().as_secs_f64() * 1e3);
85 }
86 let checksum = match decode_once(&data, true) {
87 Ok((_, h)) => h,
88 Err(e) => {
89 eprintln!("decode_bench: {e}");
90 std::process::exit(1);
91 }
92 };
93 let ms_per_frame = best / frames.max(1) as f64;
94 let threads = std::env::var("EC_H265_THREADS").unwrap_or_else(|_| "default".into());
95 println!(
96 "{path}: frames={frames} threads={threads} best_of={runs} ms_total={best:.1} \
97 ms_per_frame={ms_per_frame:.2} realtime_x={:.2} checksum={checksum:016x}",
98 (1000.0 / fps) / ms_per_frame
99 );
100}