Merge branch 'lane-bench': standing codec performance harness
Browse files at this commit3 changed files +603 -0
- Cargo.lock +14 -0
- tools/ec-bench/Cargo.toml +24 -0
- tools/ec-bench/src/main.rs +565 -0
Cargo.lockmodified+14 -0
| @@ -272,6 +272,20 @@ dependencies = [ | |||
| 272 | "ec-core", | 272 | "ec-core", |
| 273 | ] | 273 | ] |
| 274 | 274 | ||
| 275 | +[[package]] | ||
| 276 | +name = "ec-bench" | ||
| 277 | +version = "0.1.0" | ||
| 278 | +dependencies = [ | ||
| 279 | + "ec-av1", | ||
| 280 | + "ec-core", | ||
| 281 | + "ec-h264", | ||
| 282 | + "ec-h264-syntax", | ||
| 283 | + "ec-h265", | ||
| 284 | + "ec-image", | ||
| 285 | + "ec-inflate", | ||
| 286 | + "ec-probe", | ||
| 287 | +] | ||
| 288 | + | ||
| 275 | [[package]] | 289 | [[package]] |
| 276 | name = "ec-core" | 290 | name = "ec-core" |
| 277 | version = "0.1.0" | 291 | version = "0.1.0" |
tools/ec-bench/Cargo.tomladded+24 -0
| @@ -0,0 +1,24 @@ | |||
| 1 | +[package] | ||
| 2 | +name = "ec-bench" | ||
| 3 | +version.workspace = true | ||
| 4 | +edition.workspace = true | ||
| 5 | +license.workspace = true | ||
| 6 | +authors.workspace = true | ||
| 7 | +publish = false | ||
| 8 | + | ||
| 9 | +[[bin]] | ||
| 10 | +name = "ec-bench" | ||
| 11 | +path = "src/main.rs" | ||
| 12 | + | ||
| 13 | +# Standing performance harness: real media -> each codec's public one-shot | ||
| 14 | +# API -> a ranked realtime-factor table. Drives ffmpeg/ffprobe as a child | ||
| 15 | +# process for extraction and duration only, same as the crates' own tests. | ||
| 16 | +[dependencies] | ||
| 17 | +ec-core = { workspace = true } | ||
| 18 | +ec-h264 = { workspace = true } | ||
| 19 | +ec-h265 = { workspace = true } | ||
| 20 | +ec-av1 = { workspace = true } | ||
| 21 | +ec-probe = { workspace = true } | ||
| 22 | +ec-h264-syntax = { workspace = true } | ||
| 23 | +ec-image = { workspace = true } | ||
| 24 | +ec-inflate = { workspace = true } | ||
tools/ec-bench/src/main.rsadded+565 -0
| @@ -0,0 +1,565 @@ | |||
| 1 | +//! Standing performance harness: real media in, each codec's own public | ||
| 2 | +//! one-shot API exercised, a ranked realtime-factor table out. | ||
| 3 | +//! | ||
| 4 | +//! Realtime factor = media-seconds decoded (or encoded) per wall-second; | ||
| 5 | +//! bigger is faster. ffmpeg/ffprobe are used only to extract elementary | ||
| 6 | +//! streams and raw planes and to read duration, exactly as the crates' own | ||
| 7 | +//! tests already do -- this binary never decodes anything itself. | ||
| 8 | +//! | ||
| 9 | +//! Run: `cargo run -p ec-bench --release` | ||
| 10 | + | ||
| 11 | +use std::path::{Path, PathBuf}; | ||
| 12 | +use std::process::Command; | ||
| 13 | +use std::time::Instant; | ||
| 14 | + | ||
| 15 | +use ec_av1::encode::Picture as Av1Picture; | ||
| 16 | +use ec_av1::encoder::{Av1Encoder, Colour, EncoderConfig as Av1Config}; | ||
| 17 | +use ec_core::frame::{PixelFormat, Plane, VideoFrame}; | ||
| 18 | +use ec_core::registry::MediaType; | ||
| 19 | +use ec_h264::{ | ||
| 20 | + Decoder as H264Decoder, Encoder as H264Encoder, EncoderConfig as H264Config, NalOutcome, | ||
| 21 | + PictureView, | ||
| 22 | +}; | ||
| 23 | +use ec_h264_syntax::AnnexBIter; | ||
| 24 | +use ec_h265::encoder::{Encoder as H265Encoder, EncoderConfig as H265Config}; | ||
| 25 | +use ec_probe::Reader; | ||
| 26 | + | ||
| 27 | +struct Row { | ||
| 28 | + component: &'static str, | ||
| 29 | + direction: &'static str, | ||
| 30 | + content: String, | ||
| 31 | + media: String, | ||
| 32 | + wall_ms: f64, | ||
| 33 | + rtf: Option<f64>, | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +fn main() { | ||
| 37 | + let mut rows = Vec::new(); | ||
| 38 | + | ||
| 39 | + bench_h264(&mut rows); | ||
| 40 | + bench_h265_encode(&mut rows); | ||
| 41 | + bench_av1_encode(&mut rows); | ||
| 42 | + bench_audio_decode(&mut rows); | ||
| 43 | + bench_image_decode(&mut rows); | ||
| 44 | + bench_inflate(&mut rows); | ||
| 45 | + | ||
| 46 | + rows.sort_by(|a, b| { | ||
| 47 | + a.rtf | ||
| 48 | + .unwrap_or(f64::INFINITY) | ||
| 49 | + .partial_cmp(&b.rtf.unwrap_or(f64::INFINITY)) | ||
| 50 | + .unwrap() | ||
| 51 | + }); | ||
| 52 | + | ||
| 53 | + println!("| component | direction | content | media | wall ms | realtime factor |"); | ||
| 54 | + println!("|---|---|---|---|---|---|"); | ||
| 55 | + for r in &rows { | ||
| 56 | + let rtf = r | ||
| 57 | + .rtf | ||
| 58 | + .map_or_else(|| "-".to_string(), |v| format!("{v:.2}x")); | ||
| 59 | + println!( | ||
| 60 | + "| {} | {} | {} | {} | {:.1} | {} |", | ||
| 61 | + r.component, r.direction, r.content, r.media, r.wall_ms, rtf | ||
| 62 | + ); | ||
| 63 | + } | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +// --------------------------------------------------------------------------- | ||
| 67 | +// media discovery | ||
| 68 | +// --------------------------------------------------------------------------- | ||
| 69 | + | ||
| 70 | +fn fixtures() -> PathBuf { | ||
| 71 | + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures") | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +fn home() -> PathBuf { | ||
| 75 | + PathBuf::from(std::env::var("HOME").expect("HOME")) | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | +/// A handful of real-library candidates, largest-effort first, that ffprobe | ||
| 79 | +/// confirms carry the wanted video codec. Read-only: never touches the file | ||
| 80 | +/// beyond `ffprobe`/`ffmpeg -i ... -o <scratch>`. | ||
| 81 | +fn real_video(codec: &str) -> Option<PathBuf> { | ||
| 82 | + let roots = [home().join("Videos"), home().join("Downloads")]; | ||
| 83 | + let mut found = Vec::new(); | ||
| 84 | + for root in roots { | ||
| 85 | + let Ok(walker) = std::fs::read_dir(&root) else { | ||
| 86 | + continue; | ||
| 87 | + }; | ||
| 88 | + for entry in walker.flatten() { | ||
| 89 | + let p = entry.path(); | ||
| 90 | + let Some(ext) = p.extension().and_then(|e| e.to_str()) else { | ||
| 91 | + continue; | ||
| 92 | + }; | ||
| 93 | + if !matches!( | ||
| 94 | + ext.to_ascii_lowercase().as_str(), | ||
| 95 | + "mp4" | "mkv" | "mov" | "avi" | ||
| 96 | + ) { | ||
| 97 | + continue; | ||
| 98 | + } | ||
| 99 | + found.push(p); | ||
| 100 | + } | ||
| 101 | + } | ||
| 102 | + found.sort(); | ||
| 103 | + for p in found { | ||
| 104 | + if ffprobe_field(&p, "v:0", "codec_name").as_deref() == Some(codec) { | ||
| 105 | + return Some(p); | ||
| 106 | + } | ||
| 107 | + } | ||
| 108 | + None | ||
| 109 | +} | ||
| 110 | + | ||
| 111 | +fn ffprobe_field(path: &Path, stream: &str, key: &str) -> Option<String> { | ||
| 112 | + let out = Command::new("ffprobe") | ||
| 113 | + .args(["-v", "error", "-select_streams", stream, "-show_entries"]) | ||
| 114 | + .arg(format!("stream={key}")) | ||
| 115 | + .args(["-of", "default=nw=1:nk=1"]) | ||
| 116 | + .arg(path) | ||
| 117 | + .output() | ||
| 118 | + .ok()?; | ||
| 119 | + if !out.status.success() { | ||
| 120 | + return None; | ||
| 121 | + } | ||
| 122 | + let s = String::from_utf8_lossy(&out.stdout).trim().to_string(); | ||
| 123 | + (!s.is_empty()).then_some(s) | ||
| 124 | +} | ||
| 125 | + | ||
| 126 | +// --------------------------------------------------------------------------- | ||
| 127 | +// h264: decode (real film, 10s) + encode (raw planes from the same clip) | ||
| 128 | +// --------------------------------------------------------------------------- | ||
| 129 | + | ||
| 130 | +fn bench_h264(rows: &mut Vec<Row>) { | ||
| 131 | + let Some(src) = real_video("h264") else { | ||
| 132 | + rows.push(missing("ec-h264", "decode")); | ||
| 133 | + rows.push(missing("ec-h264", "encode")); | ||
| 134 | + return; | ||
| 135 | + }; | ||
| 136 | + let w: u32 = ffprobe_field(&src, "v:0", "width") | ||
| 137 | + .and_then(|s| s.parse().ok()) | ||
| 138 | + .unwrap_or(0); | ||
| 139 | + let h: u32 = ffprobe_field(&src, "v:0", "height") | ||
| 140 | + .and_then(|s| s.parse().ok()) | ||
| 141 | + .unwrap_or(0); | ||
| 142 | + | ||
| 143 | + // decode: 10s Annex-B extract, trimmed inside ffmpeg (never decode-then-trim). | ||
| 144 | + let out = Command::new("ffmpeg") | ||
| 145 | + .args(["-v", "error", "-t", "10", "-i"]) | ||
| 146 | + .arg(&src) | ||
| 147 | + .args([ | ||
| 148 | + "-an", | ||
| 149 | + "-c:v", | ||
| 150 | + "copy", | ||
| 151 | + "-bsf:v", | ||
| 152 | + "h264_mp4toannexb", | ||
| 153 | + "-f", | ||
| 154 | + "h264", | ||
| 155 | + "-", | ||
| 156 | + ]) | ||
| 157 | + .output() | ||
| 158 | + .expect("ffmpeg extract h264"); | ||
| 159 | + if out.status.success() && !out.stdout.is_empty() { | ||
| 160 | + let bytes = out.stdout; | ||
| 161 | + let start = Instant::now(); | ||
| 162 | + let mut dec = H264Decoder::new(); | ||
| 163 | + let mut frames = 0u64; | ||
| 164 | + for nal in AnnexBIter::new(&bytes) { | ||
| 165 | + if let Ok(NalOutcome::PictureBoundary) = dec.push_nal(nal) { | ||
| 166 | + let _ = dec.end_picture(); | ||
| 167 | + let _ = dec.push_nal(nal); | ||
| 168 | + } | ||
| 169 | + while dec.next_frame().is_some() { | ||
| 170 | + frames += 1; | ||
| 171 | + } | ||
| 172 | + } | ||
| 173 | + let _ = dec.flush(); | ||
| 174 | + while dec.next_frame().is_some() { | ||
| 175 | + frames += 1; | ||
| 176 | + } | ||
| 177 | + let wall = start.elapsed().as_secs_f64(); | ||
| 178 | + let fps = ffprobe_field(&src, "v:0", "r_frame_rate") | ||
| 179 | + .and_then(|s| parse_ratio(&s)) | ||
| 180 | + .unwrap_or(25.0); | ||
| 181 | + let media_s = frames as f64 / fps; | ||
| 182 | + rows.push(Row { | ||
| 183 | + component: "ec-h264", | ||
| 184 | + direction: "decode", | ||
| 185 | + content: format!("{w}x{h} film, {frames} frames"), | ||
| 186 | + media: format!("{media_s:.1}s"), | ||
| 187 | + wall_ms: wall * 1000.0, | ||
| 188 | + rtf: (wall > 0.0).then_some(media_s / wall), | ||
| 189 | + }); | ||
| 190 | + } else { | ||
| 191 | + rows.push(missing("ec-h264", "decode")); | ||
| 192 | + } | ||
| 193 | + | ||
| 194 | + // encode: 30 raw yuv420p frames scaled to 640x360 from the same clip. | ||
| 195 | + let (planes, ew, eh, n) = extract_yuv420p(&src, 640, 360, 30); | ||
| 196 | + if n > 0 { | ||
| 197 | + let mut cfg = H264Config::new(ew, eh); | ||
| 198 | + cfg.threads = 1; // one worker: single-core cost, not a parallelism claim | ||
| 199 | + let mut enc = H264Encoder::new(cfg).expect("h264 encoder"); | ||
| 200 | + let frame_len = (ew * eh + 2 * (ew.div_ceil(2) * eh.div_ceil(2))) as usize; | ||
| 201 | + let start = Instant::now(); | ||
| 202 | + for frame in planes.chunks_exact(frame_len) { | ||
| 203 | + let (y, rest) = frame.split_at((ew * eh) as usize); | ||
| 204 | + let (u, v) = rest.split_at(rest.len() / 2); | ||
| 205 | + let view = PictureView::i420(ew, eh, y, u, v); | ||
| 206 | + enc.encode(&view).expect("h264 encode"); | ||
| 207 | + } | ||
| 208 | + let wall = start.elapsed().as_secs_f64(); | ||
| 209 | + let media_s = n as f64 / 30.0; | ||
| 210 | + rows.push(Row { | ||
| 211 | + component: "ec-h264", | ||
| 212 | + direction: "encode", | ||
| 213 | + content: format!("{ew}x{eh}, {n} frames, 1 thread"), | ||
| 214 | + media: format!("{media_s:.1}s"), | ||
| 215 | + wall_ms: wall * 1000.0, | ||
| 216 | + rtf: (wall > 0.0).then_some(media_s / wall), | ||
| 217 | + }); | ||
| 218 | + } else { | ||
| 219 | + rows.push(missing("ec-h264", "encode")); | ||
| 220 | + } | ||
| 221 | +} | ||
| 222 | + | ||
| 223 | +/// `n` progressive frames of raw I420 at `w x h`, scaled and trimmed inside | ||
| 224 | +/// ffmpeg. | ||
| 225 | +fn extract_yuv420p(src: &Path, w: u32, h: u32, n: u32) -> (Vec<u8>, u32, u32, u32) { | ||
| 226 | + let out = Command::new("ffmpeg") | ||
| 227 | + .args(["-v", "error", "-i"]) | ||
| 228 | + .arg(src) | ||
| 229 | + .args([ | ||
| 230 | + "-vf", | ||
| 231 | + &format!("scale={w}:{h}"), | ||
| 232 | + "-frames:v", | ||
| 233 | + &n.to_string(), | ||
| 234 | + "-pix_fmt", | ||
| 235 | + "yuv420p", | ||
| 236 | + "-f", | ||
| 237 | + "rawvideo", | ||
| 238 | + "-", | ||
| 239 | + ]) | ||
| 240 | + .output() | ||
| 241 | + .expect("ffmpeg extract yuv420p"); | ||
| 242 | + if !out.status.success() || out.stdout.is_empty() { | ||
| 243 | + return (Vec::new(), w, h, 0); | ||
| 244 | + } | ||
| 245 | + let frame_len = (w * h + 2 * (w.div_ceil(2) * h.div_ceil(2))) as usize; | ||
| 246 | + let frames = (out.stdout.len() / frame_len) as u32; | ||
| 247 | + (out.stdout, w, h, frames) | ||
| 248 | +} | ||
| 249 | + | ||
| 250 | +fn parse_ratio(s: &str) -> Option<f64> { | ||
| 251 | + let (n, d) = s.split_once('/')?; | ||
| 252 | + let (n, d) = (n.parse::<f64>().ok()?, d.parse::<f64>().ok()?); | ||
| 253 | + (d != 0.0).then_some(n / d) | ||
| 254 | +} | ||
| 255 | + | ||
| 256 | +// --------------------------------------------------------------------------- | ||
| 257 | +// h265: encode only -- this crate has no decoder (intra-only HEVC encoder). | ||
| 258 | +// --------------------------------------------------------------------------- | ||
| 259 | + | ||
| 260 | +fn bench_h265_encode(rows: &mut Vec<Row>) { | ||
| 261 | + rows.push(missing_direction( | ||
| 262 | + "ec-h265", | ||
| 263 | + "decode", | ||
| 264 | + "encode-only crate, no decoder present", | ||
| 265 | + )); | ||
| 266 | + let Some(src) = real_video("h264").or_else(|| real_video("hevc")) else { | ||
| 267 | + rows.push(missing("ec-h265", "encode")); | ||
| 268 | + return; | ||
| 269 | + }; | ||
| 270 | + let (planes, w, h, n) = extract_yuv420p(&src, 640, 360, 10); | ||
| 271 | + if n == 0 { | ||
| 272 | + rows.push(missing("ec-h265", "encode")); | ||
| 273 | + return; | ||
| 274 | + } | ||
| 275 | + let cfg = H265Config::new(w, h); | ||
| 276 | + let enc = H265Encoder::new(cfg).expect("h265 encoder"); | ||
| 277 | + let frame_len = (w * h + 2 * (w.div_ceil(2) * h.div_ceil(2))) as usize; | ||
| 278 | + let cw = w.div_ceil(2); | ||
| 279 | + let ch = h.div_ceil(2); | ||
| 280 | + let start = Instant::now(); | ||
| 281 | + for frame in planes.chunks_exact(frame_len) { | ||
| 282 | + let (y, rest) = frame.split_at((w * h) as usize); | ||
| 283 | + let (u, v) = rest.split_at(rest.len() / 2); | ||
| 284 | + let vf = VideoFrame::try_new( | ||
| 285 | + PixelFormat::I420, | ||
| 286 | + w, | ||
| 287 | + h, | ||
| 288 | + vec![ | ||
| 289 | + Plane::new(y.to_vec(), w as usize), | ||
| 290 | + Plane::new(u.to_vec(), cw as usize), | ||
| 291 | + Plane::new(v.to_vec(), cw as usize), | ||
| 292 | + ], | ||
| 293 | + ) | ||
| 294 | + .expect("i420 frame"); | ||
| 295 | + let _ = ch; // silence unused if plane_geometry ignores it | ||
| 296 | + enc.encode_idr(&vf).expect("h265 encode"); | ||
| 297 | + } | ||
| 298 | + let wall = start.elapsed().as_secs_f64(); | ||
| 299 | + let media_s = n as f64 / 30.0; | ||
| 300 | + rows.push(Row { | ||
| 301 | + component: "ec-h265", | ||
| 302 | + direction: "encode", | ||
| 303 | + content: format!("{w}x{h}, {n} frames, IDR-only"), | ||
| 304 | + media: format!("{media_s:.1}s"), | ||
| 305 | + wall_ms: wall * 1000.0, | ||
| 306 | + rtf: (wall > 0.0).then_some(media_s / wall), | ||
| 307 | + }); | ||
| 308 | +} | ||
| 309 | + | ||
| 310 | +// --------------------------------------------------------------------------- | ||
| 311 | +// av1: encode only -- same shape as h265. | ||
| 312 | +// --------------------------------------------------------------------------- | ||
| 313 | + | ||
| 314 | +fn bench_av1_encode(rows: &mut Vec<Row>) { | ||
| 315 | + rows.push(missing_direction( | ||
| 316 | + "ec-av1", | ||
| 317 | + "decode", | ||
| 318 | + "encode-only crate, no decoder present", | ||
| 319 | + )); | ||
| 320 | + let Some(src) = real_video("h264") else { | ||
| 321 | + rows.push(missing("ec-av1", "encode")); | ||
| 322 | + return; | ||
| 323 | + }; | ||
| 324 | + // AV1's block grid is 32x32 superblocks: pick a size already a multiple of it. | ||
| 325 | + let (planes, w, h, n) = extract_yuv420p(&src, 640, 384, 10); | ||
| 326 | + if n == 0 { | ||
| 327 | + rows.push(missing("ec-av1", "encode")); | ||
| 328 | + return; | ||
| 329 | + } | ||
| 330 | + let cfg = Av1Config { | ||
| 331 | + width: w as usize, | ||
| 332 | + height: h as usize, | ||
| 333 | + base_q_idx: 100, | ||
| 334 | + gop: 10, | ||
| 335 | + colour: Colour::default(), | ||
| 336 | + }; | ||
| 337 | + let mut enc = Av1Encoder::new(cfg).expect("av1 encoder"); | ||
| 338 | + let frame_len = (w * h + 2 * (w.div_ceil(2) * h.div_ceil(2))) as usize; | ||
| 339 | + let start = Instant::now(); | ||
| 340 | + for frame in planes.chunks_exact(frame_len) { | ||
| 341 | + let (y, rest) = frame.split_at((w * h) as usize); | ||
| 342 | + let (u, v) = rest.split_at(rest.len() / 2); | ||
| 343 | + let pic = Av1Picture { | ||
| 344 | + width: w as usize, | ||
| 345 | + height: h as usize, | ||
| 346 | + y: y.to_vec(), | ||
| 347 | + u: u.to_vec(), | ||
| 348 | + v: v.to_vec(), | ||
| 349 | + }; | ||
| 350 | + enc.encode(&pic).expect("av1 encode"); | ||
| 351 | + } | ||
| 352 | + let wall = start.elapsed().as_secs_f64(); | ||
| 353 | + let media_s = n as f64 / 30.0; | ||
| 354 | + rows.push(Row { | ||
| 355 | + component: "ec-av1", | ||
| 356 | + direction: "encode", | ||
| 357 | + content: format!("{w}x{h}, {n} frames, gop=10"), | ||
| 358 | + media: format!("{media_s:.1}s"), | ||
| 359 | + wall_ms: wall * 1000.0, | ||
| 360 | + rtf: (wall > 0.0).then_some(media_s / wall), | ||
| 361 | + }); | ||
| 362 | +} | ||
| 363 | + | ||
| 364 | +// --------------------------------------------------------------------------- | ||
| 365 | +// audio decode: one loop over ec-probe's unified reader covers every codec | ||
| 366 | +// it wraps (mp3/flac/aac/alac/ac3/truehd/opus/vorbis via ogg/mp4/matroska). | ||
| 367 | +// --------------------------------------------------------------------------- | ||
| 368 | + | ||
| 369 | +fn bench_audio_decode(rows: &mut Vec<Row>) { | ||
| 370 | + let mut candidates: Vec<PathBuf> = Vec::new(); | ||
| 371 | + if let Ok(entries) = std::fs::read_dir(fixtures().join("audio")) { | ||
| 372 | + candidates.extend(entries.flatten().map(|e| e.path())); | ||
| 373 | + } | ||
| 374 | + // Real-library samples: mp3 and wav are on disk; ac3 lives inside the | ||
| 375 | + // ReelT mux (5.1 AC-3), extracted as its own elementary file. | ||
| 376 | + for f in ["8a3b6d1d19.mp3", "test.wav"] { | ||
| 377 | + let p = home().join("Downloads").join(f); | ||
| 378 | + if p.exists() { | ||
| 379 | + candidates.push(p); | ||
| 380 | + } | ||
| 381 | + } | ||
| 382 | + if let Some(src) = real_video("h264") { | ||
| 383 | + if ffprobe_field(&src, "a:0", "codec_name").as_deref() == Some("ac3") { | ||
| 384 | + let out = Command::new("ffmpeg") | ||
| 385 | + .args(["-v", "error", "-t", "10", "-i"]) | ||
| 386 | + .arg(&src) | ||
| 387 | + .args(["-vn", "-c:a", "copy", "-f", "ac3"]) | ||
| 388 | + .arg( | ||
| 389 | + Path::new(&std::env::var("HOME").unwrap()) | ||
| 390 | + .join(".cache/bench-media/sample.ac3"), | ||
| 391 | + ) | ||
| 392 | + .output(); | ||
| 393 | + if let Ok(o) = out { | ||
| 394 | + if o.status.success() { | ||
| 395 | + candidates.push(home().join(".cache/bench-media/sample.ac3")); | ||
| 396 | + } | ||
| 397 | + } | ||
| 398 | + } | ||
| 399 | + } | ||
| 400 | + candidates.sort(); | ||
| 401 | + candidates.dedup(); | ||
| 402 | + | ||
| 403 | + for path in candidates { | ||
| 404 | + let Ok(mut reader) = Reader::open(&path) else { | ||
| 405 | + continue; | ||
| 406 | + }; | ||
| 407 | + let Some(stream) = reader.default_stream(MediaType::Audio).map(|s| s.index) else { | ||
| 408 | + continue; | ||
| 409 | + }; | ||
| 410 | + let Ok(mut dec) = reader.make_decoder(stream) else { | ||
| 411 | + continue; | ||
| 412 | + }; | ||
| 413 | + let codec = format!("{:?}", dec.codec()); | ||
| 414 | + let mut samples = 0u64; | ||
| 415 | + let start = Instant::now(); | ||
| 416 | + let mut out = Vec::new(); | ||
| 417 | + loop { | ||
| 418 | + match reader.next_packet() { | ||
| 419 | + Ok(pkt) if pkt.stream == stream => { | ||
| 420 | + out.clear(); | ||
| 421 | + if dec.decode(&pkt, &mut out).is_ok() { | ||
| 422 | + samples += (out.len() / dec.channels().max(1)) as u64; | ||
| 423 | + } | ||
| 424 | + } | ||
| 425 | + Ok(_) => {} | ||
| 426 | + Err(_) => break, | ||
| 427 | + } | ||
| 428 | + } | ||
| 429 | + let _ = dec.flush(&mut out); | ||
| 430 | + let wall = start.elapsed().as_secs_f64(); | ||
| 431 | + let media_s = samples as f64 / dec.sample_rate().max(1) as f64; | ||
| 432 | + let name = path.file_name().unwrap().to_string_lossy().into_owned(); | ||
| 433 | + rows.push(Row { | ||
| 434 | + component: Box::leak(format!("ec-probe/{codec}").into_boxed_str()), | ||
| 435 | + direction: "decode", | ||
| 436 | + content: name, | ||
| 437 | + media: format!("{media_s:.1}s"), | ||
| 438 | + wall_ms: wall * 1000.0, | ||
| 439 | + rtf: (wall > 0.0 && media_s > 0.0).then_some(media_s / wall), | ||
| 440 | + }); | ||
| 441 | + } | ||
| 442 | +} | ||
| 443 | + | ||
| 444 | +// --------------------------------------------------------------------------- | ||
| 445 | +// images: one decode entry point (`ec_image::decode`) covers every format | ||
| 446 | +// the crate sniffs, so every real-library still is a candidate. | ||
| 447 | +// --------------------------------------------------------------------------- | ||
| 448 | + | ||
| 449 | +fn bench_image_decode(rows: &mut Vec<Row>) { | ||
| 450 | + let mut candidates: Vec<PathBuf> = Vec::new(); | ||
| 451 | + if let Ok(entries) = std::fs::read_dir(fixtures().join("stills")) { | ||
| 452 | + candidates.extend( | ||
| 453 | + entries | ||
| 454 | + .flatten() | ||
| 455 | + .map(|e| e.path()) | ||
| 456 | + .filter(|p| p.extension().is_some()), | ||
| 457 | + ); | ||
| 458 | + } | ||
| 459 | + for f in [ | ||
| 460 | + "IMG_20260804_170612.jpg", | ||
| 461 | + "drawing-1.png", | ||
| 462 | + "20260722-233942.png", | ||
| 463 | + "giphy.gif", | ||
| 464 | + ] { | ||
| 465 | + let p = home().join("Downloads").join(f); | ||
| 466 | + if p.exists() { | ||
| 467 | + candidates.push(p); | ||
| 468 | + } | ||
| 469 | + } | ||
| 470 | + candidates.sort(); | ||
| 471 | + | ||
| 472 | + for path in candidates { | ||
| 473 | + let Ok(bytes) = std::fs::read(&path) else { | ||
| 474 | + continue; | ||
| 475 | + }; | ||
| 476 | + let mp = bytes.len() as f64 / 1_000_000.0; | ||
| 477 | + let start = Instant::now(); | ||
| 478 | + let Ok(img) = ec_image::decode(&bytes) else { | ||
| 479 | + continue; | ||
| 480 | + }; | ||
| 481 | + let wall_ms = start.elapsed().as_secs_f64() * 1000.0; | ||
| 482 | + let (w, h) = img.dimensions(); | ||
| 483 | + let megapixels = (w as f64 * h as f64) / 1_000_000.0; | ||
| 484 | + let name = path.file_name().unwrap().to_string_lossy().into_owned(); | ||
| 485 | + rows.push(Row { | ||
| 486 | + component: "ec-image", | ||
| 487 | + direction: "decode", | ||
| 488 | + content: format!("{name} ({w}x{h}, {mp:.2} MB coded)"), | ||
| 489 | + media: format!("{megapixels:.2} MP"), | ||
| 490 | + wall_ms, | ||
| 491 | + rtf: (wall_ms > 0.0).then_some(megapixels / (wall_ms / 1000.0)), | ||
| 492 | + }); | ||
| 493 | + } | ||
| 494 | +} | ||
| 495 | + | ||
| 496 | +// --------------------------------------------------------------------------- | ||
| 497 | +// ec-inflate: the zlib stream a real PNG's IDAT chain already carries. | ||
| 498 | +// --------------------------------------------------------------------------- | ||
| 499 | + | ||
| 500 | +fn bench_inflate(rows: &mut Vec<Row>) { | ||
| 501 | + let Some(png) = [ | ||
| 502 | + fixtures().join("stills/gray16.png"), | ||
| 503 | + home().join("Downloads/drawing-1.png"), | ||
| 504 | + ] | ||
| 505 | + .into_iter() | ||
| 506 | + .find(|p| p.exists()) else { | ||
| 507 | + rows.push(missing("ec-inflate", "inflate")); | ||
| 508 | + return; | ||
| 509 | + }; | ||
| 510 | + let Ok(bytes) = std::fs::read(&png) else { | ||
| 511 | + rows.push(missing("ec-inflate", "inflate")); | ||
| 512 | + return; | ||
| 513 | + }; | ||
| 514 | + // Concatenate every IDAT chunk's payload: that byte string is exactly the | ||
| 515 | + // zlib stream `ec_image`'s own PNG path hands to `ec_inflate`. | ||
| 516 | + let mut idat = Vec::new(); | ||
| 517 | + let mut i = 8usize; // past the PNG signature | ||
| 518 | + while i + 8 <= bytes.len() { | ||
| 519 | + let len = u32::from_be_bytes(bytes[i..i + 4].try_into().unwrap()) as usize; | ||
| 520 | + let kind = &bytes[i + 4..i + 8]; | ||
| 521 | + let start = i + 8; | ||
| 522 | + if kind == b"IDAT" && start + len <= bytes.len() { | ||
| 523 | + idat.extend_from_slice(&bytes[start..start + len]); | ||
| 524 | + } | ||
| 525 | + i = start + len + 4; // skip CRC | ||
| 526 | + } | ||
| 527 | + if idat.is_empty() { | ||
| 528 | + rows.push(missing("ec-inflate", "inflate")); | ||
| 529 | + return; | ||
| 530 | + } | ||
| 531 | + let compressed_mb = idat.len() as f64 / 1_000_000.0; | ||
| 532 | + let start = Instant::now(); | ||
| 533 | + let Ok(out) = ec_inflate::inflate_zlib(&idat, 1 << 30) else { | ||
| 534 | + rows.push(missing("ec-inflate", "inflate")); | ||
| 535 | + return; | ||
| 536 | + }; | ||
| 537 | + let wall_ms = start.elapsed().as_secs_f64() * 1000.0; | ||
| 538 | + let out_mb = out.len() as f64 / 1_000_000.0; | ||
| 539 | + rows.push(Row { | ||
| 540 | + component: "ec-inflate", | ||
| 541 | + direction: "inflate", | ||
| 542 | + content: format!( | ||
| 543 | + "{} IDAT stream, {compressed_mb:.2} MB in", | ||
| 544 | + png.file_name().unwrap().to_string_lossy() | ||
| 545 | + ), | ||
| 546 | + media: format!("{out_mb:.2} MB out"), | ||
| 547 | + wall_ms, | ||
| 548 | + rtf: (wall_ms > 0.0).then_some(out_mb / (wall_ms / 1000.0)), | ||
| 549 | + }); | ||
| 550 | +} | ||
| 551 | + | ||
| 552 | +fn missing(component: &'static str, direction: &'static str) -> Row { | ||
| 553 | + missing_direction(component, direction, "no real-media candidate found") | ||
| 554 | +} | ||
| 555 | + | ||
| 556 | +fn missing_direction(component: &'static str, direction: &'static str, why: &str) -> Row { | ||
| 557 | + Row { | ||
| 558 | + component, | ||
| 559 | + direction, | ||
| 560 | + content: why.to_string(), | ||
| 561 | + media: "-".to_string(), | ||
| 562 | + wall_ms: 0.0, | ||
| 563 | + rtf: None, | ||
| 564 | + } | ||
| 565 | +} | ||