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 {
415415 /// chroma ones. Off by `EC_AV1_B64RES=0`, which restores the skip-only root
416416 /// lane-b64 shipped, so the two can be measured against each other on the
417417 /// 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.
418428 fn b64_residual() -> bool {
419429 static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| {
420430 crate::envflags::var("EC_AV1_B64RES").ok().map(|v| v != "0")
@@ -498,10 +508,29 @@ fn split_rd_threshold() -> f64 {
498508 /// Whether `cost` over a `side` x `side` block is cheap enough per pixel that
499509 /// [`split_rd_threshold`] withholds the split trial.
500510 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 {
502517 t > 0.0 && cost < t * lambda * (side * side) as f64
503518 }
504519
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+
505534 /// [`SPLIT_BREAKOUT_COEFFS`], swept by `EC_AV1_SPLIT_BREAKOUT` in a test
506535 /// build.
507536 fn split_breakout_coeffs() -> usize {
@@ -7269,6 +7298,9 @@ fn search_skip_64(
72697298 chroma: &mut [Plane; 2],
72707299 (x, y): (usize, usize),
72717300 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,
72727304 reference: &Picture,
72737305 stack: &MvStack,
72747306 fctx: &crate::decode::FrameCtx,
@@ -7398,7 +7430,7 @@ fn search_skip_64(
73987430 // two numbers are comparable: `fixed` charged `skip` coded 1, and this
73997431 // arm charges 0 instead and adds its three planes' coefficient bits.
74007432 let mut chosen = (skip_cost, true, skip_trials);
7401- if b64_residual() {
7433+ if residual {
74027434 let sets = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32];
74037435 let residual_trials = [0usize, 1, 2].map(|plane| {
74047436 let (target, px, py, s) = if plane == 0 {
@@ -9130,6 +9162,7 @@ pub(crate) fn encode_inter_frame(
91309162 &mut chroma,
91319163 (x64, y64),
91329164 &sb_search,
9165+ b64_residual() && !screen,
91339166 reference,
91349167 &stack,
91359168 fctx,
@@ -9145,9 +9178,14 @@ pub(crate) fn encode_inter_frame(
91459178 // pixel is taken outright and its quadrants are never searched --
91469179 // the same `split_rd_breakout` shape the 32x32 level uses one
91479180 // size up, and where this lever BUYS wall instead of spending it.
9148- let early64 = sb64
9149- .as_ref()
9150- .is_some_and(|(c, _, _)| split_rd_breakout(*c, SUPERBLOCK, sb_search.lambda));
9181+ let early64 = sb64.as_ref().is_some_and(|(c, _, _)| {
9182+ split_rd_breakout_at(
9183+ b64_breakout_threshold(),
9184+ *c,
9185+ SUPERBLOCK,
9186+ sb_search.lambda,
9187+ )
9188+ });
91519189 for quadrant in 0..if early64 { 0 } else { 4 } {
91529190 let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2);
91539191 // lane-av1tpl2: this 32x32 block's own lambda -- every RD