Gate the 64x64 residual arm off on screen content
Browse files at this commitaf0283676a56841ffed3e3f8a480db6a166ab371Parent: 236d76c- feat
- gate the 64x64 residual arm off on screen content
- clamp the 64-root early-out
1 changed file +43 -5
- crates/ec-av1/src/encode.rs +43 -5
crates/ec-av1/src/encode.rsmodified+43 -5
| @@ -415,6 +415,16 @@ fn b64_root() -> bool { | ||
| 415 | 415 | /// chroma ones. Off by `EC_AV1_B64RES=0`, which restores the skip-only root |
| 416 | 416 | /// lane-b64 shipped, so the two can be measured against each other on the |
| 417 | 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 | 428 | fn b64_residual() -> bool { |
| 419 | 429 | static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| { |
| 420 | 430 | crate::envflags::var("EC_AV1_B64RES").ok().map(|v| v != "0") |
| @@ -498,10 +508,29 @@ fn split_rd_threshold() -> f64 { | ||
| 498 | 508 | /// Whether `cost` over a `side` x `side` block is cheap enough per pixel that |
| 499 | 509 | /// [`split_rd_threshold`] withholds the split trial. |
| 500 | 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 | 517 | 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 | 534 | /// [`SPLIT_BREAKOUT_COEFFS`], swept by `EC_AV1_SPLIT_BREAKOUT` in a test |
| 506 | 535 | /// build. |
| 507 | 536 | fn split_breakout_coeffs() -> usize { |
| @@ -7269,6 +7298,9 @@ fn search_skip_64( | ||
| 7269 | 7298 | chroma: &mut [Plane; 2], |
| 7270 | 7299 | (x, y): (usize, usize), |
| 7271 | 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 | 7304 | reference: &Picture, |
| 7273 | 7305 | stack: &MvStack, |
| 7274 | 7306 | fctx: &crate::decode::FrameCtx, |
| @@ -7398,7 +7430,7 @@ fn search_skip_64( | ||
| 7398 | 7430 | // two numbers are comparable: `fixed` charged `skip` coded 1, and this |
| 7399 | 7431 | // arm charges 0 instead and adds its three planes' coefficient bits. |
| 7400 | 7432 | let mut chosen = (skip_cost, true, skip_trials); |
| 7401 | - if b64_residual() { | |
| 7433 | + if residual { | |
| 7402 | 7434 | let sets = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32]; |
| 7403 | 7435 | let residual_trials = [0usize, 1, 2].map(|plane| { |
| 7404 | 7436 | let (target, px, py, s) = if plane == 0 { |
| @@ -9130,6 +9162,7 @@ pub(crate) fn encode_inter_frame( | ||
| 9130 | 9162 | &mut chroma, |
| 9131 | 9163 | (x64, y64), |
| 9132 | 9164 | &sb_search, |
| 9165 | + b64_residual() && !screen, | |
| 9133 | 9166 | reference, |
| 9134 | 9167 | &stack, |
| 9135 | 9168 | fctx, |
| @@ -9145,9 +9178,14 @@ pub(crate) fn encode_inter_frame( | ||
| 9145 | 9178 | // pixel is taken outright and its quadrants are never searched -- |
| 9146 | 9179 | // the same `split_rd_breakout` shape the 32x32 level uses one |
| 9147 | 9180 | // 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 | + }); | |
| 9151 | 9189 | for quadrant in 0..if early64 { 0 } else { 4 } { |
| 9152 | 9190 | let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); |
| 9153 | 9191 | // lane-av1tpl2: this 32x32 block's own lambda -- every RD |