Add prune_k quality sweep + 24-frame 720p sequence sanity probes
Browse files at this commit6c0d62184bfd3a02b1f7fc55248a26f0b4950c5eParent: 7f9b971- test
- add prune_k quality sweep + 24-frame 720p sequence sanity probes
1 changed file +115 -0
- crates/ec-av1/src/encode.rs +115 -0
crates/ec-av1/src/encode.rsmodified+115 -0
| @@ -1090,6 +1090,12 @@ struct Search<'a> { | |||
| 1090 | /// (the default, until a lever's quality gate justifies pruning by default); | 1090 | /// (the default, until a lever's quality gate justifies pruning by default); |
| 1091 | /// `EC_AV1_PRUNE_K` sweeps it in a test build without touching callers. | 1091 | /// `EC_AV1_PRUNE_K` sweeps it in a test build without touching callers. |
| 1092 | fn prune_top_k() -> Option<usize> { | 1092 | fn prune_top_k() -> Option<usize> { |
| 1093 | + #[cfg(test)] | ||
| 1094 | + { | ||
| 1095 | + if let Some(k) = TEST_TOP_K_OVERRIDE.with(|cell| cell.get()) { | ||
| 1096 | + return k; | ||
| 1097 | + } | ||
| 1098 | + } | ||
| 1093 | let swept = std::env::var("EC_AV1_PRUNE_K") | 1099 | let swept = std::env::var("EC_AV1_PRUNE_K") |
| 1094 | .ok() | 1100 | .ok() |
| 1095 | .and_then(|v| v.parse::<usize>().ok()); | 1101 | .and_then(|v| v.parse::<usize>().ok()); |
| @@ -1099,6 +1105,20 @@ fn prune_top_k() -> Option<usize> { | |||
| 1099 | } | 1105 | } |
| 1100 | } | 1106 | } |
| 1101 | 1107 | ||
| 1108 | +/// A per-thread override [`prune_top_k`] checks first, so one test can sweep | ||
| 1109 | +/// `top_k` in-process without mutating the environment (forbidden here -- | ||
| 1110 | +/// `#![forbid(unsafe_code)]` -- and racy across parallel tests besides). | ||
| 1111 | +#[cfg(test)] | ||
| 1112 | +thread_local! { | ||
| 1113 | + static TEST_TOP_K_OVERRIDE: std::cell::Cell<Option<Option<usize>>> = | ||
| 1114 | + const { std::cell::Cell::new(None) }; | ||
| 1115 | +} | ||
| 1116 | + | ||
| 1117 | +#[cfg(test)] | ||
| 1118 | +fn set_test_top_k_override(value: Option<usize>) { | ||
| 1119 | + TEST_TOP_K_OVERRIDE.with(|cell| cell.set(Some(value))); | ||
| 1120 | +} | ||
| 1121 | + | ||
| 1102 | /// The default: unpruned, thirteen full trials per block, same as before this | 1122 | /// The default: unpruned, thirteen full trials per block, same as before this |
| 1103 | /// lever. Set from the sweep in the lane report if the quality gate clears it. | 1123 | /// lever. Set from the sweep in the lane report if the quality gate clears it. |
| 1104 | const PRUNE_TOP_K: Option<usize> = None; | 1124 | const PRUNE_TOP_K: Option<usize> = None; |
| @@ -3663,4 +3683,99 @@ mod tests { | |||
| 3663 | ); | 3683 | ); |
| 3664 | } | 3684 | } |
| 3665 | } | 3685 | } |
| 3686 | + | ||
| 3687 | + /// The quality gate [`Search::top_k`]'s lever lives or dies by: three real | ||
| 3688 | + /// frames (a dark/flat one, a detailed one, a normal film one) at two | ||
| 3689 | + /// `base_q_idx` values, encoded with the mode search unpruned (`top_k = | ||
| 3690 | + /// None`, the baseline this same build produces without | ||
| 3691 | + /// `EC_AV1_PRUNE_K` set) and with it pruned to each of a few `K`, bytes | ||
| 3692 | + /// and luma PSNR compared side by side. Not a gate itself -- the numbers | ||
| 3693 | + /// go in the lane report, which is where "ship as default" is decided -- | ||
| 3694 | + /// so it stays `#[ignore]`, run by hand with real clips on this machine. | ||
| 3695 | + /// `cargo test -p ec-av1 --release prune_k_quality_sweep -- --ignored --nocapture`. | ||
| 3696 | + #[test] | ||
| 3697 | + #[ignore = "needs real clips and ffmpeg; prints numbers for the lane report to judge"] | ||
| 3698 | + fn prune_k_quality_sweep() { | ||
| 3699 | + if !have_ffmpeg() { | ||
| 3700 | + eprintln!("SKIP prune_k_quality_sweep: no ffmpeg"); | ||
| 3701 | + return; | ||
| 3702 | + } | ||
| 3703 | + let (width, height) = (640usize, 352usize); | ||
| 3704 | + let frames: &[(&str, &str, &str)] = &[ | ||
| 3705 | + // (label, clip, seek) -- a near-black scene, a busy one, and an | ||
| 3706 | + // ordinary film frame, each from a different real file. | ||
| 3707 | + ( | ||
| 3708 | + "dark/flat", | ||
| 3709 | + "/home/user/Videos/reela.mp4", | ||
| 3710 | + "3", | ||
| 3711 | + ), | ||
| 3712 | + ( | ||
| 3713 | + "detailed", | ||
| 3714 | + "/home/user/Downloads/reelb.mkv", | ||
| 3715 | + "1200", | ||
| 3716 | + ), | ||
| 3717 | + ( | ||
| 3718 | + "film", | ||
| 3719 | + "/home/user/Videos/Films/reelc.mkv", | ||
| 3720 | + "600", | ||
| 3721 | + ), | ||
| 3722 | + ]; | ||
| 3723 | + for &(label, clip, skip) in frames { | ||
| 3724 | + if !std::path::Path::new(clip).exists() { | ||
| 3725 | + eprintln!("SKIP {label}: {clip} not present on this machine"); | ||
| 3726 | + continue; | ||
| 3727 | + } | ||
| 3728 | + let picture = clip_frame(clip, skip, width, height); | ||
| 3729 | + for &q in &[60u8, 150] { | ||
| 3730 | + // SAFETY (not literally unsafe, just process-global): this is | ||
| 3731 | + // a single-threaded #[ignore] probe, run by hand, never | ||
| 3732 | + // alongside other tests that read Search::top_k. | ||
| 3733 | + set_test_top_k_override(None); | ||
| 3734 | + let baseline = encode_key_frame(&picture, q, 0.5).unwrap(); | ||
| 3735 | + let baseline_psnr = psnr(&baseline.reconstruction.y, &picture.y); | ||
| 3736 | + eprint!( | ||
| 3737 | + "{label:9} q={q:3} baseline {:6} bytes {:6.2} dB", | ||
| 3738 | + baseline.stream.len(), | ||
| 3739 | + baseline_psnr | ||
| 3740 | + ); | ||
| 3741 | + for &k in &[3usize, 4, 6] { | ||
| 3742 | + set_test_top_k_override(Some(k)); | ||
| 3743 | + let pruned = encode_key_frame(&picture, q, 0.5).unwrap(); | ||
| 3744 | + let pruned_psnr = psnr(&pruned.reconstruction.y, &picture.y); | ||
| 3745 | + eprint!( | ||
| 3746 | + " K={k} {:6} bytes {:6.2} dB ({:+.3} dB)", | ||
| 3747 | + pruned.stream.len(), | ||
| 3748 | + pruned_psnr, | ||
| 3749 | + pruned_psnr - baseline_psnr | ||
| 3750 | + ); | ||
| 3751 | + } | ||
| 3752 | + eprintln!(); | ||
| 3753 | + } | ||
| 3754 | + } | ||
| 3755 | + set_test_top_k_override(None); | ||
| 3756 | + } | ||
| 3757 | + | ||
| 3758 | + /// The replica-shaped path: 24 frames, 1280x720, one key frame followed | ||
| 3759 | + /// by inter frames -- the same GOP shape [`encode_sequence`] gives the | ||
| 3760 | + /// facade -- timed end to end, so the edith export-time projection is a | ||
| 3761 | + /// real number rather than a guess from the isolated key/inter numbers | ||
| 3762 | + /// above. Run with and without `EC_AV1_PRUNE_K` set to compare. | ||
| 3763 | + /// `cargo test -p ec-av1 --release sequence_bench_sanity -- --ignored --nocapture`. | ||
| 3764 | + #[test] | ||
| 3765 | + #[ignore = "a perf probe, not a gate"] | ||
| 3766 | + fn sequence_bench_sanity() { | ||
| 3767 | + use std::time::Instant; | ||
| 3768 | + let (width, height) = (1280usize, 720usize); | ||
| 3769 | + let pictures: Vec<Picture> = (0..24) | ||
| 3770 | + .map(|i| panned_test_card(width, height, i * 3)) | ||
| 3771 | + .collect(); | ||
| 3772 | + let t = Instant::now(); | ||
| 3773 | + let encoded = encode_sequence(&pictures, 100, 0.5).unwrap(); | ||
| 3774 | + let elapsed = t.elapsed(); | ||
| 3775 | + let total_bytes: usize = encoded.frames.iter().map(|f| f.stream.len()).sum(); | ||
| 3776 | + eprintln!( | ||
| 3777 | + "24 frames @ 1280x720: {elapsed:>9.2?} total, {:>9.2?}/frame, {total_bytes} bytes", | ||
| 3778 | + elapsed / 24 | ||
| 3779 | + ); | ||
| 3780 | + } | ||
| 3666 | } | 3781 | } |