forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

benchp1.rs139 lines, 5.2 KiBHistoryRawDownload
1//! benchp1: PGS (.sup) decode throughput for lanes/bench.md ("lane benchp1").
2//!
3//! usage:
4//! benchp1 gen <out.sup> <seconds> write a synthetic .sup: every second one display set
5//! shows a 64x32 object, 0.8 s later a clear set follows
6//! (2 display sets per second, 90 kHz pts).
7//! benchp1 decode <file.sup> [reps] decode the whole file `reps` times (default 5) with a
8//! fresh decoder each time; print the best time, MB/s, the
9//! rendered frame count and the stream duration from the pts.
10//!
11//! The generator uses the same segment layout as the crate's tests (`tests/fuzz_regressions.rs`).
12//! ffmpeg has no PGS encoder, so this is the input source for the row.
13use std::hint::black_box;
14use std::time::Instant;
15
16use ec_pgs::{PgsDecoder, SEG_END, SEG_ODS, SEG_PCS, SEG_PDS};
17
18fn segment(pts: u32, kind: u8, body: &[u8]) -> Vec<u8> {
19 let mut out = b"PG".to_vec();
20 out.extend_from_slice(&pts.to_be_bytes());
21 out.extend_from_slice(&pts.to_be_bytes());
22 out.push(kind);
23 out.extend_from_slice(&(body.len() as u16).to_be_bytes());
24 out.extend_from_slice(body);
25 out
26}
27
28/// A 1920x1080 epoch-start composition with `objects` placed objects (0 or 1).
29fn pcs(pts: u32, x: u16, y: u16, objects: u8) -> Vec<u8> {
30 let mut body = vec![0x07, 0x80, 0x04, 0x38, 0x10, 0, 0, 0x80, 0, 0, objects];
31 if objects == 1 {
32 body.extend_from_slice(&[0, 0, 0, 0]);
33 body.extend_from_slice(&x.to_be_bytes());
34 body.extend_from_slice(&y.to_be_bytes());
35 }
36 segment(pts, SEG_PCS, &body)
37}
38
39/// Palette entry 1 opaque white.
40fn pds(pts: u32) -> Vec<u8> {
41 segment(pts, SEG_PDS, &[0, 0, 1, 235, 128, 128, 255])
42}
43
44/// One 64x32 object, RLE: each row is 64 pixels of colour 1 then an end-of-line code.
45fn ods(pts: u32) -> Vec<u8> {
46 let (w, h) = (64u16, 32u16);
47 let mut rle = Vec::new();
48 for _ in 0..h {
49 rle.extend(std::iter::repeat_n(1u8, w as usize));
50 rle.extend_from_slice(&[0, 0]);
51 }
52 let mut body = vec![0, 0, 0, 0xC0];
53 body.extend_from_slice(&((rle.len() + 4) as u32).to_be_bytes()[1..]);
54 body.extend_from_slice(&w.to_be_bytes());
55 body.extend_from_slice(&h.to_be_bytes());
56 body.extend_from_slice(&rle);
57 segment(pts, SEG_ODS, &body)
58}
59
60fn generate(path: &str, seconds: u32) {
61 let mut out = Vec::new();
62 for s in 0..seconds {
63 let t = s * 90_000;
64 let x = ((s * 37) % 1800) as u16;
65 let y = ((s * 19) % 1000) as u16;
66 out.extend(pcs(t, x, y, 1));
67 out.extend(pds(t));
68 out.extend(ods(t));
69 out.extend(segment(t, SEG_END, &[]));
70 let c = t + 72_000;
71 out.extend(pcs(c, 0, 0, 0));
72 out.extend(segment(c, SEG_END, &[]));
73 }
74 std::fs::write(path, &out).expect("write sup");
75 println!("wrote {path} bytes={} display_sets={}", out.len(), 2 * seconds);
76}
77
78/// (segment count, the last segment's pts in seconds), walking the segment headers.
79fn scan(bytes: &[u8]) -> (usize, f64) {
80 let mut pos = 0;
81 let mut last = 0u32;
82 let mut segments = 0;
83 while pos + 13 <= bytes.len() {
84 last = u32::from_be_bytes(bytes[pos + 2..pos + 6].try_into().unwrap());
85 let size = u16::from_be_bytes([bytes[pos + 11], bytes[pos + 12]]) as usize;
86 pos += 13 + size;
87 segments += 1;
88 }
89 (segments, f64::from(last) / 90_000.0)
90}
91
92/// One full decode with a fresh decoder: (rendered frames, push errors). The file is fed one
93/// segment at a time and the finished frames are dropped after each push, as a streaming reader
94/// would. A full 1920x1080 canvas is about 8 MB, so draining per segment keeps the decoder's
95/// output queue at most one frame deep.
96fn decode_once(bytes: &[u8]) -> (usize, usize) {
97 let mut d = PgsDecoder::new();
98 let (mut frames, mut errors) = (0, 0);
99 let mut pos = 0;
100 while pos + 13 <= bytes.len() {
101 let size = u16::from_be_bytes([bytes[pos + 11], bytes[pos + 12]]) as usize;
102 let end = (pos + 13 + size).min(bytes.len());
103 if d.push(black_box(&bytes[pos..end])).is_err() {
104 errors += 1;
105 }
106 pos = end;
107 while let Some(f) = d.take_frame() {
108 frames += 1;
109 black_box(f);
110 }
111 }
112 (frames, errors)
113}
114
115fn decode(path: &str, reps: usize) {
116 let bytes = std::fs::read(path).expect("read sup");
117 let (frames, errors) = decode_once(&bytes);
118 let mut best = f64::MAX;
119 for _ in 0..reps {
120 let t = Instant::now();
121 black_box(decode_once(black_box(&bytes)));
122 best = best.min(t.elapsed().as_secs_f64());
123 }
124 let (segments, dur) = scan(&bytes);
125 println!(
126 "bytes={} segments={segments} frames={frames} push_errors={errors} duration_s={dur:.1} best_decode_s={best:.4} mb_s={:.2} reps={reps}",
127 bytes.len(),
128 bytes.len() as f64 / best / 1e6
129 );
130}
131
132fn main() {
133 let args: Vec<String> = std::env::args().collect();
134 match args.get(1).map(String::as_str) {
135 Some("gen") => generate(&args[2], args[3].parse().expect("seconds")),
136 Some("decode") => decode(&args[2], args.get(3).map_or(5, |s| s.parse().expect("reps"))),
137 _ => panic!("usage: benchp1 gen <out.sup> <seconds> | decode <file.sup> [reps]"),
138 }
139}