forge
Explore
Log in

Don't you dare go Hollow

Gate the 64x64 residual arm off on screen content

Browse files at this commit

af0283676a56841ffed3e3f8a480db6a166ab371Ahmet Kaan GümüşParent: 236d76c
  • feat
    • gate the 64x64 residual arm off on screen content
    • clamp the 64-root early-out
Show full message
feat(av1): gate the 64x64 residual arm off on screen content; clamp the 64-root early-out

Native gate, two arms of one build, baseline reproduced to the tenth
(film A +43.0/+15.0, film B +64.7/+34.0, screen +33.4/-23.8):

| row | skip-only root | + residual arm |
|---|---|---|
| film A | +43.0 / +15.0 | +42.9 / +14.8 |
| film B | +64.7 / +34.0 | +63.3 / +31.5 |
| screen | +33.4 / -23.8 | +34.3 / -23.4 |

Both film rows improve on both columns; screen loses 0.9 vs libaom,
outside the keep rule's screen clause -- so the arm is gated off on a
screen frame, the same content gate the coding pyramid takes. Re-run with
the gate: film rows unchanged, screen row byte-identical to the skip-only
arm (40155/51325/63998/80203 B), keep rule passes.

Also: the 64-root early-out no longer follows crate::speed::SPLIT_RD up
the preset ladder (b64_breakout_threshold clamps it at the shipped 0.125).
lane-b64 measured what 0.5 does one size up: a whole superblock taken at
64x64 with its four quadrants never searched, film B +91.5/+53.4 ->
+114.6/+71.5. Inert at presets 0..3, which already run 0.125.
crates/ec-av1/src/encode.rsmodified+43 -5
@@ -415,6 +415,16 @@ fn b64_root() -> bool {
415 /// chroma ones. Off by `EC_AV1_B64RES=0`, which restores the skip-only root415 /// chroma ones. Off by `EC_AV1_B64RES=0`, which restores the skip-only root
416 /// lane-b64 shipped, so the two can be measured against each other on the416 /// lane-b64 shipped, so the two can be measured against each other on the
417 /// same build.417 /// same build.
418+///
419+/// MEASURED on the native gate (two arms of one build, baseline reproduced to
420+/// the tenth): both film rows improve on both columns -- film A
421+/// +43.0/+15.0 -> +42.9/+14.8, film B +64.7/+34.0 -> +63.3/+31.5 -- and
422+/// SCREEN CAPTURE loses +33.4/-23.8 -> +34.3/-23.4, outside the keep rule's
423+/// +-0.3 screen clause. So the caller gates this off on a screen frame
424+/// (`allow_screen_content_tools`), the same content gate the coding pyramid
425+/// takes: a desktop capture's 64x64 superblocks are flat runs whose skip arm
426+/// already codes them for nothing, and a residual there only spends bits the
427+/// intra/palette tools would have spent better.
418 fn b64_residual() -> bool {428 fn b64_residual() -> bool {
419 static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| {429 static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| {
420 crate::envflags::var("EC_AV1_B64RES").ok().map(|v| v != "0")430 crate::envflags::var("EC_AV1_B64RES").ok().map(|v| v != "0")
@@ -498,10 +508,29 @@ fn split_rd_threshold() -> f64 {
498 /// Whether `cost` over a `side` x `side` block is cheap enough per pixel that508 /// Whether `cost` over a `side` x `side` block is cheap enough per pixel that
499 /// [`split_rd_threshold`] withholds the split trial.509 /// [`split_rd_threshold`] withholds the split trial.
500 fn split_rd_breakout(cost: f64, side: usize, lambda: f64) -> bool {510 fn split_rd_breakout(cost: f64, side: usize, lambda: f64) -> bool {
501- let t = split_rd_threshold();511+ split_rd_breakout_at(split_rd_threshold(), cost, side, lambda)
512+}
513+
514+/// [`split_rd_breakout`] at an explicit threshold -- what the 64x64 root's own
515+/// early-out reads.
516+fn split_rd_breakout_at(t: f64, cost: f64, side: usize, lambda: f64) -> bool {
502 t > 0.0 && cost < t * lambda * (side * side) as f64517 t > 0.0 && cost < t * lambda * (side * side) as f64
503 }518 }
504 519
520+/// The threshold the 64x64 root's early-out uses: [`split_rd_threshold`]
521+/// CLAMPED at the shipped [`SPLIT_RD_THRESHOLD`] (lane-tx64).
522+///
523+/// The 32x32 level is allowed to loosen its breakout with the speed preset
524+/// (`crate::speed::SPLIT_RD` steps to 0.5 at preset 6), but lane-b64 measured
525+/// what that does one size up: at 0.5 a whole superblock is taken at 64x64
526+/// without its four quadrants ever being searched, and film B goes
527+/// +91.5/+53.4 -> +114.6/+71.5. A 64x64 block is four times the area, so the
528+/// same per-pixel threshold withholds four times as much search -- the
529+/// preset's step is priced for the 32 level and does not carry up here.
530+fn b64_breakout_threshold() -> f64 {
531+ split_rd_threshold().min(SPLIT_RD_THRESHOLD)
532+}
533+
505 /// [`SPLIT_BREAKOUT_COEFFS`], swept by `EC_AV1_SPLIT_BREAKOUT` in a test534 /// [`SPLIT_BREAKOUT_COEFFS`], swept by `EC_AV1_SPLIT_BREAKOUT` in a test
506 /// build.535 /// build.
507 fn split_breakout_coeffs() -> usize {536 fn split_breakout_coeffs() -> usize {
@@ -7269,6 +7298,9 @@ fn search_skip_64(
7269 chroma: &mut [Plane; 2],7298 chroma: &mut [Plane; 2],
7270 (x, y): (usize, usize),7299 (x, y): (usize, usize),
7271 search: &Search,7300 search: &Search,
7301+ // Whether the non-skip arm is priced at all ([`b64_residual`], and off on
7302+ // screen content -- see that function's doc).
7303+ residual: bool,
7272 reference: &Picture,7304 reference: &Picture,
7273 stack: &MvStack,7305 stack: &MvStack,
7274 fctx: &crate::decode::FrameCtx,7306 fctx: &crate::decode::FrameCtx,
@@ -7398,7 +7430,7 @@ fn search_skip_64(
7398 // two numbers are comparable: `fixed` charged `skip` coded 1, and this7430 // two numbers are comparable: `fixed` charged `skip` coded 1, and this
7399 // arm charges 0 instead and adds its three planes' coefficient bits.7431 // arm charges 0 instead and adds its three planes' coefficient bits.
7400 let mut chosen = (skip_cost, true, skip_trials);7432 let mut chosen = (skip_cost, true, skip_trials);
7401- if b64_residual() {7433+ if residual {
7402 let sets = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32];7434 let sets = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32];
7403 let residual_trials = [0usize, 1, 2].map(|plane| {7435 let residual_trials = [0usize, 1, 2].map(|plane| {
7404 let (target, px, py, s) = if plane == 0 {7436 let (target, px, py, s) = if plane == 0 {
@@ -9130,6 +9162,7 @@ pub(crate) fn encode_inter_frame(
9130 &mut chroma,9162 &mut chroma,
9131 (x64, y64),9163 (x64, y64),
9132 &sb_search,9164 &sb_search,
9165+ b64_residual() && !screen,
9133 reference,9166 reference,
9134 &stack,9167 &stack,
9135 fctx,9168 fctx,
@@ -9145,9 +9178,14 @@ pub(crate) fn encode_inter_frame(
9145 // pixel is taken outright and its quadrants are never searched --9178 // pixel is taken outright and its quadrants are never searched --
9146 // the same `split_rd_breakout` shape the 32x32 level uses one9179 // the same `split_rd_breakout` shape the 32x32 level uses one
9147 // size up, and where this lever BUYS wall instead of spending it.9180 // size up, and where this lever BUYS wall instead of spending it.
9148- let early64 = sb649181+ let early64 = sb64.as_ref().is_some_and(|(c, _, _)| {
9149- .as_ref()9182+ split_rd_breakout_at(
9150- .is_some_and(|(c, _, _)| split_rd_breakout(*c, SUPERBLOCK, sb_search.lambda));9183+ b64_breakout_threshold(),
9184+ *c,
9185+ SUPERBLOCK,
9186+ sb_search.lambda,
9187+ )
9188+ });
9151 for quadrant in 0..if early64 { 0 } else { 4 } {9189 for quadrant in 0..if early64 { 0 } else { 4 } {
9152 let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2);9190 let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2);
9153 // lane-av1tpl2: this 32x32 block's own lambda -- every RD9191 // lane-av1tpl2: this 32x32 block's own lambda -- every RD