Tune screen detection to the measured colour-count split, gate prints
Browse files at this commit55cb55e92458d07607fe828b0471a486493b86c4Parent: f2eac79- feat
- tune screen detection to the measured colour-count split, gate prints
1 changed file +150 -4
- crates/ec-av1/src/encode.rs +150 -4
crates/ec-av1/src/encode.rsmodified+150 -4
| @@ -735,19 +735,25 @@ fn seq_screen() -> bool { | |||
| 735 | 735 | ||
| 736 | /// How many distinct colours a 16x16 luma block may hold and still count as | 736 | /// How many distinct colours a 16x16 luma block may hold and still count as |
| 737 | /// screen content ([`screen_content`]), `EC_AV1_SCREEN_COLORS`. libaom's own | 737 | /// screen content ([`screen_content`]), `EC_AV1_SCREEN_COLORS`. libaom's own |
| 738 | -/// `av1_set_screen_content_options` bound is 4. | 738 | +/// `av1_set_screen_content_options` bound is 4, which separates NOTHING on a |
| 739 | +/// real (lossily coded, so noisy) desktop capture: `probe_screen_detect` | ||
| 740 | +/// measures the share of 16x16 blocks under each bound on the gate's own | ||
| 741 | +/// three clips, and only at 32 do the populations part -- film 20.3..21.9%, | ||
| 742 | +/// his OBS capture 51.1..51.5%. | ||
| 739 | fn screen_colors() -> usize { | 743 | fn screen_colors() -> usize { |
| 740 | static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new(); | 744 | static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new(); |
| 741 | *N.get_or_init(|| { | 745 | *N.get_or_init(|| { |
| 742 | std::env::var("EC_AV1_SCREEN_COLORS") | 746 | std::env::var("EC_AV1_SCREEN_COLORS") |
| 743 | .ok() | 747 | .ok() |
| 744 | .and_then(|v| v.parse().ok()) | 748 | .and_then(|v| v.parse().ok()) |
| 745 | - .unwrap_or(4) | 749 | + .unwrap_or(32) |
| 746 | }) | 750 | }) |
| 747 | } | 751 | } |
| 748 | 752 | ||
| 749 | /// The reciprocal of the frame area those blocks must cover, in tenths -- | 753 | /// The reciprocal of the frame area those blocks must cover, in tenths -- |
| 750 | -/// libaom's `counts * blk_h * blk_w * 10 > width * height` is 10 (one tenth). | 754 | +/// libaom's own `counts * blk_h * blk_w * 10 > width * height` is 10 (one |
| 755 | +/// tenth). 3 (a third) is the midpoint of the two populations | ||
| 756 | +/// `probe_screen_detect` measured at the colour bound above. | ||
| 751 | /// `EC_AV1_SCREEN_PCT`. | 757 | /// `EC_AV1_SCREEN_PCT`. |
| 752 | fn screen_pct() -> usize { | 758 | fn screen_pct() -> usize { |
| 753 | static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new(); | 759 | static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new(); |
| @@ -755,7 +761,7 @@ fn screen_pct() -> usize { | |||
| 755 | std::env::var("EC_AV1_SCREEN_PCT") | 761 | std::env::var("EC_AV1_SCREEN_PCT") |
| 756 | .ok() | 762 | .ok() |
| 757 | .and_then(|v| v.parse().ok()) | 763 | .and_then(|v| v.parse().ok()) |
| 758 | - .unwrap_or(10) | 764 | + .unwrap_or(3) |
| 759 | }) | 765 | }) |
| 760 | } | 766 | } |
| 761 | 767 | ||
| @@ -8373,6 +8379,146 @@ mod tests { | |||
| 8373 | } | 8379 | } |
| 8374 | } | 8380 | } |
| 8375 | 8381 | ||
| 8382 | + /// The missing instrument: what this encoder costs against libaom and | ||
| 8383 | + /// rav1e at matched fidelity, and how long it takes to get there. | ||
| 8384 | + /// | ||
| 8385 | + /// Recipe, identical on all three sides: 12 consecutive frames of each | ||
| 8386 | + /// clip scaled to 640x384 (a whole number of superblocks), one key frame | ||
| 8387 | + /// then 11 inter frames (`-g 12`, matching our own sequence shape), one | ||
| 8388 | + /// tile, `-threads 1`, no film grain synthesis (neither reference enables | ||
| 8389 | + /// it by default), 4:2:0 8-bit in and out. Reference points: | ||
| 8390 | + /// libaom-av1: `-cpu-used 6 -b:v 0 -crf {5,20,35,45}` | ||
| 8391 | + /// librav1e: `-rav1e-params speed=6:quantizer={50,100,150,200}: | ||
| 8392 | + /// tile_cols=1:tile_rows=1:threads=1` | ||
| 8393 | + /// ours: `base_q_idx {60,90,120,150}`. Every PSNR is all-plane, on | ||
| 8394 | + /// ffmpeg-decoded frames of each encoder's own stream. | ||
| 8395 | + /// | ||
| 8396 | + /// Measured 2026-09-06 after the in-loop filters (lane-av1filt: per-frame | ||
| 8397 | + /// deblocking levels and CDEF strengths, both chosen by re-decoding the | ||
| 8398 | + /// coded tile -- `crate::filter_search`), 12 frames, this box (wall = the | ||
| 8399 | + /// four encodes of that ladder together): | ||
| 8400 | + /// | ||
| 8401 | + /// | clip | ours (q 150/120/90/60) | BD-rate vs libaom | vs rav1e | wall ours:aom:rav1e | | ||
| 8402 | + /// |---|---|---|---|---| | ||
| 8403 | + /// | 1080p fixture | 42.11 dB/17774 B .. 50.99 dB/76838 B | +143.6% | +94.0% | 9.2s:1.1s:2.6s | | ||
| 8404 | + /// | 2160p fixture | 42.84 dB/12480 B .. 51.43 dB/46557 B | +179.5% | +128.8% | 8.6s:1.0s:2.2s | | ||
| 8405 | + /// | screen capture | 39.76 dB/9536 B .. 48.87 dB/29189 B | +88.0% | +21.5% | 7.2s:1.0s:2.1s | | ||
| 8406 | + /// | ||
| 8407 | + /// Re-measured 2026-09-06 after lane-av1fwd, which changed only how long | ||
| 8408 | + /// the encoder takes -- every stream it writes is byte-identical, so the | ||
| 8409 | + /// BD columns are this box's own re-run of the same recipe rather than a | ||
| 8410 | + /// quality move (+141.3/+174.2/+88.9 vs libaom, +91.9/+124.5/+21.6 vs | ||
| 8411 | + /// rav1e). What moved is the wall of the four encodes: | ||
| 8412 | + /// | ||
| 8413 | + /// | clip | wall ours before | after | instructions:u before -> after | | ||
| 8414 | + /// |---|---|---|---| | ||
| 8415 | + /// | 1080p fixture | 9.2s | 7.2s | 191.5G -> 139.8G (-27.0%) | | ||
| 8416 | + /// | 2160p fixture | 8.6s | 6.9s | 183.5G -> 136.4G (-25.6%) | | ||
| 8417 | + /// | screen capture | 7.2s | 6.7s | 157.2G -> 122.5G (-22.1%) | | ||
| 8418 | + /// | ||
| 8419 | + /// Three exact steps, none of which touches a coded bit: the coefficient | ||
| 8420 | + /// rate pricer stopped rebuilding every CDF table per candidate | ||
| 8421 | + /// (`tile::coeff_bits`), the filter search stopped re-decoding its own | ||
| 8422 | + /// tile once per candidate (`decode::FilterReplay`), and the forward | ||
| 8423 | + /// transform stopped transforming the all-zero residual that 70% of its | ||
| 8424 | + /// calls carry. | ||
| 8425 | + /// | ||
| 8426 | + /// The three steps of that lane, same recipe (vs libaom, then vs rav1e): | ||
| 8427 | + /// | ||
| 8428 | + /// | step | 1080p | 2160p | screen | | ||
| 8429 | + /// |---|---|---|---| | ||
| 8430 | + /// | no in-loop filter | +234.0 / +171.9 | +260.3 / +203.2 | +318.5 / +155.3 | | ||
| 8431 | + /// | + deblocking | +198.2 / +143.1 | +218.1 / +162.4 | +136.9 / +52.7 | | ||
| 8432 | + /// | + CDEF | +143.6 / +94.0 | +179.5 / +128.8 | +88.0 / +21.5 | | ||
| 8433 | + /// | ||
| 8434 | + /// Loop restoration is NOT in yet: it is the one filter whose parameters | ||
| 8435 | + /// are per restoration UNIT inside the tile payload, so it needs both | ||
| 8436 | + /// tile writers to code `lr` syntax and an encoder-visible post-CDEF | ||
| 8437 | + /// buffer to solve against -- neither of which this frame-header-level | ||
| 8438 | + /// search reaches. | ||
| 8439 | + /// | ||
| 8440 | + /// The baseline it replaced, before an inter frame's blocks chose their | ||
| 8441 | + /// own partition, was +501.4/+504.4/+440.9 against libaom and | ||
| 8442 | + /// +364.4/+401.0/+206.0 against rav1e at 5.1/4.6/5.9 s. | ||
| 8443 | + /// | ||
| 8444 | + /// So: roughly 2.5-3x libaom's rate and 1.5-2x rav1e's at matched | ||
| 8445 | + /// fidelity, while spending far more time than either (the partition | ||
| 8446 | + /// search's own cost, uncut: see the two intra-pruning variants measured | ||
| 8447 | + /// and rejected in the lane report). No | ||
| 8448 | + /// threshold is asserted yet -- this run sets the baseline; the gate | ||
| 8449 | + /// asserts only four monotone points per ladder and that ffmpeg decodes | ||
| 8450 | + /// our stream sample-exact against our reconstruction. | ||
| 8451 | + /// | ||
| 8452 | + /// Run: | ||
| 8453 | + /// cargo test -p ec-av1 --release --lib -- --ignored \ | ||
| 8454 | + /// bd_rate_vs_libaom_and_rav1e --nocapture | ||
| 8455 | + #[test] | ||
| 8456 | + #[ignore = "the encoder BD-rate baseline: minutes per clip, needs ffmpeg"] | ||
| 8457 | + /// The sweep behind [`screen_content`]'s two constants | ||
| 8458 | + /// (`unswept-decision-constants`): the share of 16x16 luma blocks with at | ||
| 8459 | + /// most N distinct colours, per gate clip, at the size the gate codes. | ||
| 8460 | + /// `cargo test -p ec-av1 --release --lib -- --ignored probe_screen_detect --nocapture` | ||
| 8461 | + #[test] | ||
| 8462 | + #[ignore = "reads the real library"] | ||
| 8463 | + fn probe_screen_detect() { | ||
| 8464 | + let fixtures = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures"); | ||
| 8465 | + let mut clips: Vec<(String, std::path::PathBuf)> = Vec::new(); | ||
| 8466 | + for name in ["h264-1080p-23.976-8bit.mp4", "h264-2160p-23.976-8bit.mp4"] { | ||
| 8467 | + let path = fixtures.join("video").join(name); | ||
| 8468 | + if path.exists() { | ||
| 8469 | + clips.push((name.to_string(), path)); | ||
| 8470 | + } | ||
| 8471 | + } | ||
| 8472 | + if let Ok(manifest) = std::fs::read_to_string(fixtures.join("real-library-manifest.tsv")) | ||
| 8473 | + && let Some(p) = manifest | ||
| 8474 | + .lines() | ||
| 8475 | + .skip(1) | ||
| 8476 | + .filter_map(|l| l.split('\t').next()) | ||
| 8477 | + .find(|p| p.contains("/OBS/") && p.ends_with(".mkv") && std::path::Path::new(p).exists()) | ||
| 8478 | + { | ||
| 8479 | + clips.push(("screen".to_string(), std::path::PathBuf::from(p))); | ||
| 8480 | + } | ||
| 8481 | + let (width, height) = (640usize, 384usize); | ||
| 8482 | + for (name, path) in &clips { | ||
| 8483 | + let frames = clip_frames(path.to_str().unwrap(), "0", width, height, 4); | ||
| 8484 | + for (f, picture) in frames.iter().enumerate() { | ||
| 8485 | + let y: Vec<u8> = picture.y.iter().map(|&v| v as u8).collect(); | ||
| 8486 | + let mut hist = [0usize; 257]; | ||
| 8487 | + let mut blocks = 0usize; | ||
| 8488 | + for by in (0..height).step_by(16) { | ||
| 8489 | + for bx in (0..width).step_by(16) { | ||
| 8490 | + let mut seen = [false; 256]; | ||
| 8491 | + let mut colors = 0usize; | ||
| 8492 | + for row in 0..16 { | ||
| 8493 | + for col in 0..16 { | ||
| 8494 | + let v = usize::from(y[(by + row) * width + bx + col]); | ||
| 8495 | + if !seen[v] { | ||
| 8496 | + seen[v] = true; | ||
| 8497 | + colors += 1; | ||
| 8498 | + } | ||
| 8499 | + } | ||
| 8500 | + } | ||
| 8501 | + hist[colors] += 1; | ||
| 8502 | + blocks += 1; | ||
| 8503 | + } | ||
| 8504 | + } | ||
| 8505 | + let share = |limit: usize| { | ||
| 8506 | + 100.0 * hist[2..=limit.min(256)].iter().sum::<usize>() as f64 | ||
| 8507 | + / blocks.max(1) as f64 | ||
| 8508 | + }; | ||
| 8509 | + eprintln!( | ||
| 8510 | + "{name} frame {f}: <=4 {:.1}% <=8 {:.1}% <=16 {:.1}% <=32 {:.1}% <=64 {:.1}% <=128 {:.1}%", | ||
| 8511 | + share(4), | ||
| 8512 | + share(8), | ||
| 8513 | + share(16), | ||
| 8514 | + share(32), | ||
| 8515 | + share(64), | ||
| 8516 | + share(128), | ||
| 8517 | + ); | ||
| 8518 | + } | ||
| 8519 | + } | ||
| 8520 | + } | ||
| 8521 | + | ||
| 8376 | /// The missing instrument: what this encoder costs against libaom and | 8522 | /// The missing instrument: what this encoder costs against libaom and |
| 8377 | /// rav1e at matched fidelity, and how long it takes to get there. | 8523 | /// rav1e at matched fidelity, and how long it takes to get there. |
| 8378 | /// | 8524 | /// |