forge
Explore
Log in

Don't you dare go Hollow

A native-resolution screen BD arm, crop not downscale

Browse files at this commit

cd2c1838ada9ece2f2b772e0079f33098f5dc62cAhmet Kaan GümüşParent: cbbd6e2
  • test
    • a native-resolution screen BD arm, crop not downscale
Show full message
test(av1): a native-resolution screen BD arm, crop not downscale

The default gate downscales the OBS capture to 640x384, which destroys the
exact 16x16 repeats intrabc and part of the palette exist for. bd_rate_screen_native
crops a superblock-aligned 1920x1024 window out of the middle at native size
and runs the same 12-frame four-quantizer ladder against libaom cpu-used 6 and
rav1e speed 6, with the same three-way sample-exact assertion.
crates/ec-av1/src/encode.rsmodified+183 -1
@@ -9698,11 +9698,26 @@ mod tests {
96989698 width: usize,
96999699 height: usize,
97009700 frames: usize,
9701+ ) -> Vec<Picture> {
9702+ clip_frames_vf(clip, skip, &format!("scale={width}:{height}"), width, height, frames)
9703+ }
9704+
9705+ /// [`clip_frames`] with the filter chain spelled out, so a caller can ask
9706+ /// for a native-resolution CROP instead of a downscale (the downscale is
9707+ /// what destroys a screen capture's exact repeats -- class
9708+ /// `gate-recipe-confound`). `width`/`height` must be what `vf` produces.
9709+ fn clip_frames_vf(
9710+ clip: &str,
9711+ skip: &str,
9712+ vf: &str,
9713+ width: usize,
9714+ height: usize,
9715+ frames: usize,
97019716 ) -> Vec<Picture> {
97029717 let out = Command::new("ffmpeg")
97039718 .args(["-v", "error", "-ss", skip, "-i", clip])
97049719 .args(["-frames:v", &frames.to_string()])
9705- .args(["-vf", &format!("scale={width}:{height}")])
9720+ .args(["-vf", vf])
97069721 .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"])
97079722 .output()
97089723 .expect("ffmpeg failed to run");
@@ -10694,4 +10709,171 @@ mod tests {
1069410709 );
1069510710 }
1069610711 }
10712+ /// The video stream's coded size, from ffprobe.
10713+ fn probe_dims(clip: &str) -> Option<(usize, usize)> {
10714+ let out = Command::new("ffprobe")
10715+ .args(["-v", "error", "-select_streams", "v:0"])
10716+ .args(["-show_entries", "stream=width,height", "-of", "csv=p=0"])
10717+ .arg(clip)
10718+ .output()
10719+ .ok()?;
10720+ let text = String::from_utf8_lossy(&out.stdout);
10721+ let mut f = text.trim().split(',');
10722+ Some((f.next()?.trim().parse().ok()?, f.next()?.trim().parse().ok()?))
10723+ }
10724+
10725+ /// The NATIVE-resolution screen arm of the BD gate. The default gate
10726+ /// downscales the OBS capture to 640x384, which destroys exactly the
10727+ /// pixel-identical 16x16 repeats intrabc and (part of) the palette exist
10728+ /// for (class `gate-recipe-confound`), so every screen-tool decision taken
10729+ /// on that recipe is taken on content the tool cannot serve. This arm
10730+ /// crops -- never scales -- a superblock-aligned window out of the middle
10731+ /// of the capture at its own resolution (at most 1920x1024, so the wall
10732+ /// stays in the minutes) and runs the same 12-frame, four-quantizer
10733+ /// ladder against libaom `cpu-used 6` and rav1e `speed=6`, one tile, one
10734+ /// thread, with the same three-way (encoder / ffmpeg / our decoder)
10735+ /// sample-exactness assertion `our_ladder` makes.
10736+ ///
10737+ /// It is its own `--ignored` test so the default gate's wall does not
10738+ /// grow. Every variant is an environment knob, and each of those is a
10739+ /// `OnceLock` read once per process, so a variant is a separate run:
10740+ ///
10741+ /// cargo test -p ec-av1 --release --lib -- --ignored \
10742+ /// bd_rate_screen_native --nocapture # baseline
10743+ /// EC_AV1_INTRABC=1 ... same command # intra block copy
10744+ /// EC_AV1_PAL_MAXCOLORS=256 ... same command # palette bound
10745+ /// EC_AV1_TILES=1:1 EC_AV1_TILE_THREADS=4 ... # 2x2 tiles
10746+ /// EC_AV1_NATIVE_FILM=1 ... same command # + a film crop row
10747+ #[test]
10748+ #[ignore = "the native-resolution screen BD arm: minutes per row, needs ffmpeg"]
10749+ fn bd_rate_screen_native() {
10750+ if !have_ffmpeg() {
10751+ eprintln!("SKIP bd_rate_screen_native: no ffmpeg");
10752+ return;
10753+ }
10754+ let fixtures = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures");
10755+ let mut clips: Vec<(String, String)> = Vec::new();
10756+ match std::fs::read_to_string(fixtures.join("real-library-manifest.tsv")) {
10757+ Ok(manifest) => match manifest
10758+ .lines()
10759+ .skip(1)
10760+ .filter_map(|l| l.split('\t').next())
10761+ .find(|p| p.contains("/OBS/") && p.ends_with(".mkv") && std::path::Path::new(p).exists())
10762+ {
10763+ Some(p) => clips.push((
10764+ format!("screen capture ({})", p.rsplit('/').next().unwrap_or(p)),
10765+ p.to_string(),
10766+ )),
10767+ None => eprintln!("SKIP screen capture: no OBS recording in the manifest exists"),
10768+ },
10769+ Err(e) => eprintln!("SKIP bd_rate_screen_native: no real-library manifest ({e})"),
10770+ }
10771+ // The film crop is an extra row, not a default-moving measurement.
10772+ if std::env::var("EC_AV1_NATIVE_FILM").ok().as_deref() == Some("1") {
10773+ let path = fixtures.join("video").join("h264-1080p-23.976-8bit.mp4");
10774+ if path.exists() {
10775+ clips.push((
10776+ "film 1080p (native crop)".to_string(),
10777+ path.to_str().unwrap().to_string(),
10778+ ));
10779+ }
10780+ }
10781+ if clips.is_empty() {
10782+ eprintln!("SKIP bd_rate_screen_native: no clip");
10783+ return;
10784+ }
10785+
10786+ let frames = 12usize;
10787+ let aom_points: Vec<Vec<String>> = [5, 20, 35, 45]
10788+ .iter()
10789+ .map(|q| {
10790+ ["-cpu-used", "6", "-b:v", "0", "-crf", &q.to_string()]
10791+ .map(String::from)
10792+ .to_vec()
10793+ })
10794+ .collect();
10795+ let rav1e_points: Vec<Vec<String>> = [50, 100, 150, 200]
10796+ .iter()
10797+ .map(|q| {
10798+ vec![
10799+ "-rav1e-params".to_string(),
10800+ format!("speed=6:quantizer={q}:tile_cols=1:tile_rows=1:threads=1"),
10801+ ]
10802+ })
10803+ .collect();
10804+ println!(
10805+ "\nnative crop, {frames} frames, gop={frames}; tiles {:?}, tile threads {}, \
10806+ intrabc {}, palette maxcolors {}",
10807+ armed_tiles(),
10808+ crate::par::tile_threads(),
10809+ intrabc_enabled(),
10810+ palette_max_colors(),
10811+ );
10812+ println!("| clip | size | ours PSNR/bytes per point | BD-rate vs libaom | BD-rate vs rav1e | wall ours:libaom:rav1e |");
10813+ println!("|---|---|---|---|---|---|");
10814+ for (name, path) in &clips {
10815+ let fctx = &crate::decode::FrameCtx::new();
10816+ let Some((nw, nh)) = probe_dims(path) else {
10817+ eprintln!("SKIP {name}: ffprobe gave no size");
10818+ continue;
10819+ };
10820+ // A whole number of 128-wide superblocks, at most 1920x1024, out
10821+ // of the middle of the frame; the offsets stay even for 4:2:0.
10822+ let cw = nw.min(1920) / 128 * 128;
10823+ let ch = nh.min(1024) / 128 * 128;
10824+ assert!(cw >= 128 && ch >= 128, "{name}: {nw}x{nh} is smaller than a superblock");
10825+ let (x, y) = ((nw - cw) / 2 & !1, (nh - ch) / 2 & !1);
10826+ let source = clip_frames_vf(
10827+ path,
10828+ "0",
10829+ &format!("crop={cw}:{ch}:{x}:{y}"),
10830+ cw,
10831+ ch,
10832+ frames,
10833+ );
10834+ SCREEN_FRAMES.iter().for_each(|c| {
10835+ c.store(0, std::sync::atomic::Ordering::Relaxed);
10836+ });
10837+ let _ = crate::tile::take_palette_hits();
10838+ let _ = crate::tile::take_palette_uv_hits();
10839+ let _ = crate::tile::take_intrabc_hits();
10840+ let _ = take_intrabc_search();
10841+ let (ours, ours_wall) = our_ladder(name, &source, cw, ch, fctx);
10842+ // gate-blind-to-feature: what the screen tools actually did at
10843+ // native resolution, over this clip's four encodes.
10844+ let screen_on = SCREEN_FRAMES[1].load(std::sync::atomic::Ordering::Relaxed);
10845+ let screen_off = SCREEN_FRAMES[0].load(std::sync::atomic::Ordering::Relaxed);
10846+ let palette = crate::tile::take_palette_hits();
10847+ let palette_uv = crate::tile::take_palette_uv_hits();
10848+ let ibc = crate::tile::take_intrabc_hits();
10849+ let search = take_intrabc_search();
10850+ eprintln!(
10851+ "{name}: screen frames on={screen_on} off={screen_off} ({:.1}% on); \
10852+ palette blocks luma {} chroma {}; intrabc blocks {} (searched {} / valid {} / \
10853+ won {})",
10854+ 100.0 * screen_on as f64 / (screen_on + screen_off).max(1) as f64,
10855+ palette[0],
10856+ palette_uv[0],
10857+ ibc[0],
10858+ search[0],
10859+ search[1],
10860+ search[2],
10861+ );
10862+ let (aom, aom_wall) = external_ladder(&source, cw, ch, "libaom-av1", &aom_points);
10863+ let (rav1e, rav1e_wall) = external_ladder(&source, cw, ch, "librav1e", &rav1e_points);
10864+ assert_monotone(&format!("{name}: ours"), &ours);
10865+ assert_monotone(&format!("{name}: libaom"), &aom);
10866+ assert_monotone(&format!("{name}: rav1e"), &rav1e);
10867+ let points = ours
10868+ .iter()
10869+ .map(|(p, b)| format!("{p:.2} dB/{:.0} B", 10f64.powf(*b)))
10870+ .collect::<Vec<_>>()
10871+ .join(", ");
10872+ println!(
10873+ "| {name} | {cw}x{ch} | {points} | {:+.1}% | {:+.1}% | {ours_wall:.1}s:{aom_wall:.1}s:{rav1e_wall:.1}s |",
10874+ bd_rate(&aom, &ours) * 100.0,
10875+ bd_rate(&rav1e, &ours) * 100.0,
10876+ );
10877+ }
10878+ }
1069710879 }