forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB84fa44cTahinli2 months ago3331 commits

bench_decode.rs158 lines, 5.4 KiBHistoryRawDownload
1//! Throwaway decode-throughput probe for lane-h264perf: raw demux+decode
2//! Mpx/s, no render. One untimed warm pass, then best-of-N timed passes over a
3//! decoder built fresh per pass. `--hash` FNV-1a-hashes every released frame
4//! (display order, visible rows only) so a byte-exact A/B is a file diff.
5//!
6//! `.mp4` drives the registry surface (ec-mp4 demux, avcC extradata,
7//! send_packet/receive_frame); `.264` drives the NAL surface (push_nal +
8//! end_picture + flush), both the way the crate's own conformance suite does.
9//!
10//! usage: bench_decode <file.mp4|file.264> [reps] [--hash]
11use std::time::Instant;
12
13use ec_core::frame::{Frame, Plane};
14use ec_core::packet::Packet;
15use ec_core::registry::{CodecId, Decoder as _, Demuxer as _};
16use ec_h264::{Decoder as H264NalDecoder, H264Decoder, NalOutcome};
17
18/// FNV-1a 64 over the visible bytes of every plane (I420 geometry — the same
19/// bytes conformance.rs's frame_bytes packs), plus the geometry.
20fn hash_frame(h: &mut u64, w: u32, height: u32, planes: &[Plane]) {
21 let mut fold = |bytes: &[u8]| {
22 for &b in bytes {
23 *h ^= u64::from(b);
24 *h = h.wrapping_mul(0x100000001b3);
25 }
26 };
27 fold(&w.to_le_bytes());
28 fold(&height.to_le_bytes());
29 let (w2, h2) = (w.div_ceil(2) as usize, height.div_ceil(2) as usize);
30 let dims = [(w as usize, height as usize), (w2, h2), (w2, h2)];
31 for (plane, (pw, ph)) in planes.iter().zip(dims) {
32 for y in 0..ph {
33 let row = plane.row(y, pw).expect("plane row");
34 fold(row);
35 }
36 }
37}
38
39fn decode_mp4(
40 packets: &[Packet],
41 params: &ec_core::registry::CodecParameters,
42 hash: bool,
43) -> (usize, u64, u32, u32) {
44 let mut dec = H264Decoder::new(params.clone()).expect("h264 decoder");
45 let mut frames = 0usize;
46 let mut h: u64 = 0xcbf29ce484222325;
47 let mut dims = (0u32, 0u32);
48 for p in packets {
49 dec.send_packet(p).expect("send_packet");
50 while let Ok(Frame::Video(f)) = dec.receive_frame() {
51 if hash {
52 hash_frame(&mut h, f.width, f.height, &f.planes);
53 }
54 dims = (f.width, f.height);
55 frames += 1;
56 }
57 }
58 dec.flush().expect("flush");
59 while let Ok(Frame::Video(f)) = dec.receive_frame() {
60 if hash {
61 hash_frame(&mut h, f.width, f.height, &f.planes);
62 }
63 dims = (f.width, f.height);
64 frames += 1;
65 }
66 (frames, h, dims.0, dims.1)
67}
68
69fn decode_annexb(bytes: &[u8], hash: bool) -> (usize, u64, u32, u32) {
70 let mut dec = H264NalDecoder::new();
71 let mut frames = 0usize;
72 let mut h: u64 = 0xcbf29ce484222325;
73 let mut dims = (0u32, 0u32);
74 for nal in ec_h264_syntax::AnnexBIter::new(bytes) {
75 if dec.push_nal(nal).expect("push_nal") == NalOutcome::PictureBoundary {
76 dec.end_picture().expect("end_picture");
77 dec.push_nal(nal).expect("re-push");
78 }
79 while let Some(f) = dec.next_frame() {
80 if hash {
81 hash_frame(&mut h, f.width, f.height, &f.planes);
82 }
83 dims = (f.width, f.height);
84 frames += 1;
85 }
86 }
87 dec.flush().expect("flush");
88 while let Some(f) = dec.next_frame() {
89 if hash {
90 hash_frame(&mut h, f.width, f.height, &f.planes);
91 }
92 dims = (f.width, f.height);
93 frames += 1;
94 }
95 (frames, h, dims.0, dims.1)
96}
97
98enum Source {
99 Mp4(Vec<Packet>, ec_core::registry::CodecParameters),
100 AnnexB(Vec<u8>),
101}
102
103fn decode(src: &Source, hash: bool) -> (usize, u64, u32, u32) {
104 match src {
105 Source::Mp4(p, par) => decode_mp4(p, par, hash),
106 Source::AnnexB(b) => decode_annexb(b, hash),
107 }
108}
109
110fn main() {
111 let args: Vec<String> = std::env::args().skip(1).collect();
112 let path = args.first().expect("usage: bench_decode <file.mp4|.264> [reps] [--hash]");
113 let reps: u32 = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(3);
114 let hash = args.iter().any(|a| a == "--hash");
115
116 let src = if path.ends_with(".mp4") {
117 let file = std::fs::File::open(path).expect("open mp4");
118 let mut demux =
119 ec_mp4::Mp4Demuxer::new(std::io::BufReader::new(file)).expect("ec-mp4 open");
120 let (idx, params) = demux
121 .streams()
122 .iter()
123 .find(|s| s.params.codec == CodecId::H264)
124 .map(|s| (s.index, s.params.clone()))
125 .expect("no H.264 track");
126 let mut packets = Vec::new();
127 while let Ok(p) = demux.next_packet() {
128 if p.stream == idx {
129 packets.push(p);
130 }
131 }
132 Source::Mp4(packets, params)
133 } else {
134 Source::AnnexB(std::fs::read(path).expect("read annexb"))
135 };
136
137 // Warm pass (untimed): page in, branch predictors, allocator pools.
138 let (frames, h, w, height) = decode(&src, hash);
139 assert!(frames > 0, "no frames decoded");
140 if hash {
141 println!("hash={h:016x} frames={frames} {w}x{height}");
142 }
143
144 let px = u64::from(w) * u64::from(height) * frames as u64;
145 let mut best = f64::MAX;
146 for _ in 0..reps {
147 let t = Instant::now();
148 let r = decode(&src, false);
149 let e = t.elapsed().as_secs_f64();
150 assert_eq!(r.0, frames, "frame count diverged between passes");
151 best = best.min(e);
152 }
153 println!(
154 "{path}: frames={frames} px={px} best={:.1}ms over {reps} -> {:.1} Mpx/s",
155 best * 1e3,
156 px as f64 / best / 1e6
157 );
158}