diff --git a/tools/ec-bench/Cargo.toml b/tools/ec-bench/Cargo.toml index af4f2082..7c81ed4c 100644 --- a/tools/ec-bench/Cargo.toml +++ b/tools/ec-bench/Cargo.toml @@ -19,7 +19,6 @@ ec-h264 = { workspace = true } ec-h265 = { workspace = true } ec-av1 = { workspace = true } ec-hw = { workspace = true } -ec-va = { workspace = true } ec-probe = { workspace = true } ec-h264-syntax = { workspace = true } ec-image = { workspace = true } @@ -33,3 +32,6 @@ ec-alac = { workspace = true } ec-ac3 = { workspace = true } ec-truehd = { workspace = true } ec-testkit.workspace = true + +[target.'cfg(target_os = "linux")'.dependencies] +ec-va = { workspace = true } diff --git a/tools/ec-bench/src/main.rs b/tools/ec-bench/src/main.rs index 64f6643a..7f2dd128 100644 --- a/tools/ec-bench/src/main.rs +++ b/tools/ec-bench/src/main.rs @@ -37,14 +37,35 @@ struct Row { rtf: Option, } +impl Row { + fn timed( + component: &'static str, + direction: &'static str, + content: String, + media_s: f64, + wall: f64, + ) -> Row { + Row { + component, + direction, + content, + media: format!("{media_s:.1}s"), + wall_ms: wall * 1000.0, + rtf: (wall > 0.0).then_some(media_s / wall), + } + } +} + fn main() -> ec_core::Result<()> { let mut rows = Vec::new(); bench_h264(&mut rows)?; bench_h265_encode(&mut rows); bench_av1_encode(&mut rows); - bench_hw_av1_encode(&mut rows); - bench_hw_av1_encode_10bit(&mut rows); + #[cfg(target_os = "linux")] + bench_hw_av1_encode(&mut rows, false); + #[cfg(target_os = "linux")] + bench_hw_av1_encode(&mut rows, true); bench_audio_decode(&mut rows)?; bench_audio_encode(&mut rows); bench_truehd(&mut rows); @@ -166,25 +187,20 @@ fn bench_h264(rows: &mut Vec) -> ec_core::Result<()> { .and_then(|s| parse_ratio(&s)) .unwrap_or(25.0); let media_s = frames as f64 / fps; - rows.push(Row { - component: "ec-h264", - direction: "decode", - content: format!("{w}x{h} film, {frames} frames"), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-h264", "decode", format!("{w}x{h} film, {frames} frames"), media_s, wall)); } else { rows.push(missing("ec-h264", "decode")); } // encode: 30 raw yuv420p frames scaled to 640x360 from the same clip. - let (planes, ew, eh, n) = extract_yuv420p(&src, 640, 360, 30); + let (ew, eh) = (640, 360); + let planes = extract_raw(&src, ew, eh, 30, "yuv420p"); + let frame_len = yuv420_frame_len(ew, eh, 1); + let n = (planes.len() / frame_len) as u32; if n > 0 { let mut cfg = H264Config::new(ew, eh); cfg.threads = 1; // one worker: single-core cost, not a parallelism claim let mut enc = H264Encoder::new(cfg).expect("h264 encoder"); - let frame_len = (ew * eh + 2 * (ew.div_ceil(2) * eh.div_ceil(2))) as usize; let start = Instant::now(); for frame in planes.chunks_exact(frame_len) { let (y, rest) = frame.split_at((ew * eh) as usize); @@ -194,23 +210,20 @@ fn bench_h264(rows: &mut Vec) -> ec_core::Result<()> { } let wall = start.elapsed().as_secs_f64(); let media_s = n as f64 / 30.0; - rows.push(Row { - component: "ec-h264", - direction: "encode", - content: format!("{ew}x{eh}, {n} frames, 1 thread"), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-h264", "encode", format!("{ew}x{eh}, {n} frames, 1 thread"), media_s, wall)); } else { rows.push(missing("ec-h264", "encode")); } Ok(()) } -/// `n` progressive frames of raw I420 at `w x h`, scaled and trimmed inside -/// ffmpeg. -fn extract_yuv420p(src: &Path, w: u32, h: u32, n: u32) -> (Vec, u32, u32, u32) { +fn yuv420_frame_len(w: u32, h: u32, bytes_per_sample: usize) -> usize { + (w * h + 2 * (w.div_ceil(2) * h.div_ceil(2))) as usize * bytes_per_sample +} + +/// Up to `n` progressive frames at `w x h` in `pix_fmt`, scaled and trimmed +/// inside ffmpeg from `SEEK_SECONDS` on. Empty when ffmpeg fails. +fn extract_raw(src: &Path, w: u32, h: u32, n: u32, pix_fmt: &str) -> Vec { let out = ec_testkit::ffmpeg() .args(["-v", "error", "-ss", SEEK_SECONDS, "-i"]) .arg(src) @@ -220,19 +233,14 @@ fn extract_yuv420p(src: &Path, w: u32, h: u32, n: u32) -> (Vec, u32, u32, u3 "-frames:v", &n.to_string(), "-pix_fmt", - "yuv420p", + pix_fmt, "-f", "rawvideo", "-", ]) .output() - .expect("ffmpeg extract yuv420p"); - if !out.status.success() || out.stdout.is_empty() { - return (Vec::new(), w, h, 0); - } - let frame_len = (w * h + 2 * (w.div_ceil(2) * h.div_ceil(2))) as usize; - let frames = (out.stdout.len() / frame_len) as u32; - (out.stdout, w, h, frames) + .expect("ffmpeg extract raw"); + if out.status.success() { out.stdout } else { Vec::new() } } fn parse_ratio(s: &str) -> Option { @@ -251,16 +259,17 @@ fn bench_h265_encode(rows: &mut Vec) { rows.push(missing("ec-h265", "encode")); return; }; - let (planes, w, h, n) = extract_yuv420p(&src, 640, 360, 10); + let (w, h) = (640, 360); + let planes = extract_raw(&src, w, h, 10, "yuv420p"); + let frame_len = yuv420_frame_len(w, h, 1); + let n = (planes.len() / frame_len) as u32; if n == 0 { rows.push(missing("ec-h265", "encode")); rows.push(missing("ec-h265", "decode")); return; } let enc = H265Encoder::new(H265Config::new(w, h)).expect("h265 encoder"); - let frame_len = (w * h + 2 * (w.div_ceil(2) * h.div_ceil(2))) as usize; let cw = w.div_ceil(2); - let ch = h.div_ceil(2); let mut aus = Vec::new(); let start = Instant::now(); for frame in planes.chunks_exact(frame_len) { @@ -277,196 +286,115 @@ fn bench_h265_encode(rows: &mut Vec) { ], ) .expect("i420 frame"); - let _ = ch; // silence unused if plane_geometry ignores it aus.push(enc.encode_idr(&vf).expect("h265 encode").au); } let wall = start.elapsed().as_secs_f64(); let media_s = n as f64 / 30.0; - rows.push(Row { - component: "ec-h265", - direction: "encode", - content: format!("{w}x{h}, {n} frames, IDR-only"), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-h265", "encode", format!("{w}x{h}, {n} frames, IDR-only"), media_s, wall)); let start = Instant::now(); for au in &aus { ec_h265::decode::decode_idr_au(au).expect("h265 decode"); } let wall = start.elapsed().as_secs_f64(); - rows.push(Row { - component: "ec-h265", - direction: "decode", - content: format!("{w}x{h}, {n} frames, own IDR stream"), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-h265", "decode", format!("{w}x{h}, {n} frames, own IDR stream"), media_s, wall)); } // --------------------------------------------------------------------------- -// hardware av1: the GPU encoder, at the size his library is in. +// hardware av1: the GPU encoder (`ec-hw`, VA-API), at 8 or 10 bits. // --------------------------------------------------------------------------- -/// AV1 encode on the GPU (`ec-hw`, VA-API). Skips as a row rather than a -/// failure when there is no VA device or the driver has no AV1 encode -/// entrypoint -- a machine without one is not a slow machine. -fn bench_hw_av1_encode(rows: &mut Vec) { - let Some(src) = real_video("h264").or_else(|| real_video("hevc")) else { - rows.push(missing("ec-hw av1", "encode")); - return; +/// AV1 encode on the GPU. Skips as a row rather than a failure when there is +/// no VA device or the driver has no AV1 encode entrypoint -- a machine +/// without one is not a slow machine. +#[cfg(target_os = "linux")] +fn bench_hw_av1_encode(rows: &mut Vec, ten_bit: bool) { + let (component, pix_fmt, bytes) = if ten_bit { + ("ec-hw av1 10-bit", "yuv420p10le", 2) + } else { + ("ec-hw av1", "yuv420p", 1) }; - let display = match ec_va::open() { - Ok(display) => display, - Err(e) => { - rows.push(missing_direction("ec-hw av1", "encode", &format!("no VA display ({e})"))); - return; - } + let src = if ten_bit { + real_video("hevc").or_else(|| real_video("h264")) + } else { + real_video("h264").or_else(|| real_video("hevc")) }; - let (planes, w, h, n) = extract_yuv420p(&src, 1920, 1080, 30); - if n == 0 { - rows.push(missing("ec-hw av1", "encode")); - return; - } - let mut cfg = ec_hw::EncoderConfig::new(ec_hw::EncCodec::Av1, w, h); - cfg.rate_control = ec_hw::RateControlMode::ConstantQp { qp: 120 }; - let mut enc = match ec_hw::Encoder::new(&display, cfg) { - Ok(enc) => enc, - Err(e) => { - rows.push(missing_direction("ec-hw av1", "encode", &format!("driver refused ({e})"))); - return; - } - }; - let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); - let frame_len = (w * h + 2 * cw * ch) as usize; - let start = Instant::now(); - let mut coded = 0usize; - for (i, frame) in planes.chunks_exact(frame_len).enumerate() { - let (y, rest) = frame.split_at((w * h) as usize); - let (u, v) = rest.split_at(rest.len() / 2); - let picture = ec_hw::I420 { - y: y.to_vec(), - u: u.to_vec(), - v: v.to_vec(), - width: w, - height: h, - }; - let meta = ec_hw::FrameMetadata { - timestamp: i as i64, - force_keyframe: false, - }; - match enc.encode(&picture, meta) { - Ok(out) => coded += out.data.len(), - Err(e) => { - rows.push(missing_direction("ec-hw av1", "encode", &format!("encode failed ({e})"))); - return; - } - } - } - let wall = start.elapsed().as_secs_f64(); - let media_s = n as f64 / 30.0; - rows.push(Row { - component: "ec-hw av1", - direction: "encode", - content: format!("{w}x{h}, {n} frames, CQP 120, {} kB", coded / 1000), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); -} - -/// The same row at 10 bits, which is the depth his own AV1 library is stored -/// at (`yuv420p10le`, both films). Skips as a row when there is no VA device, -/// no AV1 encode entrypoint, or no source to read. -fn bench_hw_av1_encode_10bit(rows: &mut Vec) { - let Some(src) = real_video("av1").or_else(|| real_video("hevc")).or_else(|| real_video("h264")) - else { - rows.push(missing("ec-hw av1 10-bit", "encode")); + let Some(src) = src else { + rows.push(missing(component, "encode")); return; }; let display = match ec_va::open() { Ok(display) => display, Err(e) => { - rows.push(missing_direction("ec-hw av1 10-bit", "encode", &format!("no VA display ({e})"))); + rows.push(missing_direction(component, "encode", &format!("no VA display ({e})"))); return; } }; - let (w, h) = (1920u32, 1080u32); - let raw = extract_yuv420p10(&src, w, h, 30); - let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); - let frame_len = ((w * h + 2 * cw * ch) * 2) as usize; + let (w, h) = (1920, 1080); + let raw = extract_raw(&src, w, h, 30, pix_fmt); + let frame_len = yuv420_frame_len(w, h, bytes); let n = (raw.len() / frame_len) as u32; if n == 0 { - rows.push(missing("ec-hw av1 10-bit", "encode")); + rows.push(missing(component, "encode")); return; } - let mut cfg = ec_hw::EncoderConfig::new(ec_hw::EncCodec::Av1, w, h).ten_bit(); + let mut cfg = ec_hw::EncoderConfig::new(ec_hw::EncCodec::Av1, w, h); + if ten_bit { + cfg = cfg.ten_bit(); + } cfg.rate_control = ec_hw::RateControlMode::ConstantQp { qp: 120 }; let mut enc = match ec_hw::Encoder::new(&display, cfg) { Ok(enc) => enc, Err(e) => { - rows.push(missing_direction("ec-hw av1 10-bit", "encode", &format!("driver refused ({e})"))); + rows.push(missing_direction(component, "encode", &format!("driver refused ({e})"))); return; } }; - let read = |s: &[u8]| -> Vec { + let read16 = |s: &[u8]| -> Vec { s.as_chunks::<2>().0.iter().map(|p| u16::from_le_bytes([p[0], p[1]])).collect() }; let start = Instant::now(); let mut coded = 0usize; for (i, frame) in raw.chunks_exact(frame_len).enumerate() { - let (y, rest) = frame.split_at((w * h * 2) as usize); + let (y, rest) = frame.split_at((w * h) as usize * bytes); let (u, v) = rest.split_at(rest.len() / 2); - let picture = ec_hw::I420_16 { - y: read(y), - u: read(u), - v: read(v), - width: w, - height: h, - bit_depth: 10, - }; let meta = ec_hw::FrameMetadata { timestamp: i as i64, force_keyframe: false }; - match enc.encode_16(&picture, meta) { + let result = if ten_bit { + enc.encode_16( + &ec_hw::I420_16 { + y: read16(y), + u: read16(u), + v: read16(v), + width: w, + height: h, + bit_depth: 10, + }, + meta, + ) + } else { + enc.encode( + &ec_hw::I420 { + y: y.to_vec(), + u: u.to_vec(), + v: v.to_vec(), + width: w, + height: h, + }, + meta, + ) + }; + match result { Ok(out) => coded += out.data.len(), Err(e) => { - rows.push(missing_direction("ec-hw av1 10-bit", "encode", &format!("encode failed ({e})"))); + rows.push(missing_direction(component, "encode", &format!("encode failed ({e})"))); return; } } } let wall = start.elapsed().as_secs_f64(); let media_s = f64::from(n) / 30.0; - rows.push(Row { - component: "ec-hw av1 10-bit", - direction: "encode", - content: format!("{w}x{h} yuv420p10le, {n} frames from 00:00:30, CQP 120, {} kB", coded / 1000), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); -} - -/// `extract_yuv420p` at ten bits, planar 16-bit little endian, seeked -/// into the clip: frame 0 of a film is its black leader, and a black leader codes -/// to nothing and benchmarks nothing. -fn extract_yuv420p10(src: &Path, w: u32, h: u32, n: u32) -> Vec { - let out = ec_testkit::ffmpeg() - .args(["-v", "error", "-ss", SEEK_SECONDS, "-i"]) - .arg(src) - .args([ - "-vf", &format!("scale={w}:{h}"), - "-frames:v", &n.to_string(), - "-pix_fmt", "yuv420p10le", - "-f", "rawvideo", "-", - ]) - .output() - .expect("ffmpeg extract yuv420p10le"); - if !out.status.success() { - return Vec::new(); - } - out.stdout + let content = format!("{w}x{h} {pix_fmt}, {n} frames, CQP 120, {} kB", coded / 1000); + rows.push(Row::timed(component, "encode", content, media_s, wall)); } // --------------------------------------------------------------------------- @@ -480,7 +408,10 @@ fn bench_av1_encode(rows: &mut Vec) { return; }; // AV1's block grid is 32x32 superblocks: pick a size already a multiple of it. - let (planes, w, h, n) = extract_yuv420p(&src, 640, 384, 10); + let (w, h) = (640, 384); + let planes = extract_raw(&src, w, h, 10, "yuv420p"); + let frame_len = yuv420_frame_len(w, h, 1); + let n = (planes.len() / frame_len) as u32; if n == 0 { rows.push(missing("ec-av1", "encode")); rows.push(missing("ec-av1", "decode")); @@ -530,7 +461,6 @@ fn bench_av1_encode(rows: &mut Vec) { None => Av1Encoder::with_speed(cfg, speed).expect("av1 encoder"), Some(p) => Av1Encoder::with_pyramid(cfg, p).expect("av1 encoder"), }; - let frame_len = (w * h + 2 * (w.div_ceil(2) * h.div_ceil(2))) as usize; let mut stream = Vec::new(); let mut hidden = 0usize; let start = Instant::now(); @@ -571,17 +501,11 @@ fn bench_av1_encode(rows: &mut Vec) { }; let fps = if wall > 0.0 { f64::from(n) / wall } else { 0.0 }; let bytes_per_frame = stream.len() as f64 / f64::from(n); - rows.push(Row { - component: "ec-av1", - direction: "encode", - content: format!( - "speed {speed} [{levers}], {w}x{h}, {n} frames, gop=10, {coded_under}, \ - {hidden} hidden, {fps:.1} fps, {bytes_per_frame:.0} B/frame" - ), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + let content = format!( + "speed {speed} [{levers}], {w}x{h}, {n} frames, gop=10, {coded_under}, \ + {hidden} hidden, {fps:.1} fps, {bytes_per_frame:.0} B/frame" + ); + rows.push(Row::timed("ec-av1", "encode", content, media_s, wall)); if first_stream.is_empty() { first_stream = stream; } @@ -595,14 +519,7 @@ fn bench_av1_encode(rows: &mut Vec) { let pictures = ec_av1::stream::decode_stream(&stream).expect("av1 decode_stream"); let wall = start.elapsed().as_secs_f64(); assert_eq!(pictures.len(), n as usize, "decode_stream frame count"); - rows.push(Row { - component: "ec-av1", - direction: "decode", - content: format!("{w}x{h}, {n} frames, gop=10"), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-av1", "decode", format!("{w}x{h}, {n} frames, gop=10"), media_s, wall)); } // --------------------------------------------------------------------------- @@ -719,14 +636,7 @@ fn bench_audio_encode(rows: &mut Vec) { .expect("opus encode"); } let wall = start.elapsed().as_secs_f64(); - rows.push(Row { - component: "ec-opus", - direction: "encode", - content: content.clone(), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-opus", "encode", content.clone(), media_s, wall)); } // vorbis: quality 0.6, the level the oracle tests already encode at. @@ -751,14 +661,7 @@ fn bench_audio_encode(rows: &mut Vec) { } let wall = start.elapsed().as_secs_f64(); let _ = n; - rows.push(Row { - component: "ec-vorbis", - direction: "encode", - content: content.clone(), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-vorbis", "encode", content.clone(), media_s, wall)); } // aac: 128 kbit/s, raw (no ADTS wrap) -- the default config. @@ -769,14 +672,7 @@ fn bench_audio_encode(rows: &mut Vec) { enc.finish(); while enc.next_packet().is_ok() {} let wall = start.elapsed().as_secs_f64(); - rows.push(Row { - component: "ec-aac", - direction: "encode", - content: content.clone(), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-aac", "encode", content.clone(), media_s, wall)); } // mp3: CBR 192 kbit/s, the incumbent-matching default the tests gate on. @@ -791,14 +687,7 @@ fn bench_audio_encode(rows: &mut Vec) { enc.finish(); while enc.next_packet().is_ok() {} let wall = start.elapsed().as_secs_f64(); - rows.push(Row { - component: "ec-mp3", - direction: "encode", - content: content.clone(), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-mp3", "encode", content.clone(), media_s, wall)); } // flac: single-shot, default config (4096-sample blocks, LPC-12), 16-bit. @@ -811,14 +700,7 @@ fn bench_audio_encode(rows: &mut Vec) { let start = Instant::now(); let _ = ec_flac::encode(&cfg, &samples, channels, 16, rate).expect("flac encode"); let wall = start.elapsed().as_secs_f64(); - rows.push(Row { - component: "ec-flac", - direction: "encode", - content: content.clone(), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-flac", "encode", content.clone(), media_s, wall)); } // alac: 4096-sample frames, 16-bit -- same shape the round-trip tests use. @@ -834,14 +716,7 @@ fn bench_audio_encode(rows: &mut Vec) { enc.encode_frame(chunk).expect("alac frame"); } let wall = start.elapsed().as_secs_f64(); - rows.push(Row { - component: "ec-alac", - direction: "encode", - content: content.clone(), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-alac", "encode", content.clone(), media_s, wall)); } // ac3: 192 kbit/s -- only if the source's channel count is one A/52 codes. @@ -863,14 +738,7 @@ fn bench_audio_encode(rows: &mut Vec) { } } let wall = start.elapsed().as_secs_f64(); - rows.push(Row { - component: "ec-ac3", - direction: "encode", - content, - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-ac3", "encode", content, media_s, wall)); } else { rows.push(missing_direction( "ec-ac3", @@ -930,14 +798,7 @@ fn bench_truehd(rows: &mut Vec) { thd.extend_from_slice(&pkt.data); } let wall = start.elapsed().as_secs_f64(); - rows.push(Row { - component: "ec-truehd", - direction: "encode", - content: format!("{media_s:.1}s wav16 stereo @ {rate}Hz"), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-truehd", "encode", format!("{media_s:.1}s wav16 stereo @ {rate}Hz"), media_s, wall)); let mut dec = ec_truehd::TrueHdDecoder::new(); let start = Instant::now(); @@ -957,14 +818,7 @@ fn bench_truehd(rows: &mut Vec) { } let wall = start.elapsed().as_secs_f64(); let media_s = samples as f64 / f64::from(rate); - rows.push(Row { - component: "ec-truehd", - direction: "decode", - content: "own encoded stream, stereo".to_string(), - media: format!("{media_s:.1}s"), - wall_ms: wall * 1000.0, - rtf: (wall > 0.0).then_some(media_s / wall), - }); + rows.push(Row::timed("ec-truehd", "decode", "own encoded stream, stereo".to_string(), media_s, wall)); } // ---------------------------------------------------------------------------