Merge branch 'lane-tx64' (b771ffc0): the 64x64 inter root codes a real residual; the root's early-out clamped so it ships at every preset
Browse files at this commit4 changed files +343 -60
- crates/ec-av1/src/encode.rs +187 -21
- crates/ec-av1/src/encoder.rs +69 -0
- crates/ec-av1/src/speed.rs +19 -21
- crates/ec-av1/src/tile.rs +68 -18
crates/ec-av1/src/encode.rsmodified+187 -21
| @@ -386,10 +386,16 @@ fn split_inter_blocks() -> bool { | |||
| 386 | /// 32x32 or smaller, which put our mode+mv+partition+tx_size spend at 162,669 | 386 | /// 32x32 or smaller, which put our mode+mv+partition+tx_size spend at 162,669 |
| 387 | /// bits against rav1e's 61,988, about 46% of the film B gap. | 387 | /// bits against rav1e's 61,988, about 46% of the film B gap. |
| 388 | /// | 388 | /// |
| 389 | -/// SKIP-only: a 64x64 residual needs a forward TX_64X64 the encoder does not | 389 | +/// lane-tx64: no longer skip-only. The root also prices a REAL residual -- |
| 390 | -/// have (`transform.rs` tops out at 32x32), and the writer refuses any other | 390 | +/// one TX_64X64 luma transform (`transform.rs`'s forward network has always |
| 391 | -/// shape (`crate::tile::Quadrant::Whole64`). Switched off by `EC_AV1_B64=0` | 391 | +/// reached 64 points; what was missing was the block coder and the writer |
| 392 | -/// in any build. | 392 | +/// around it) and one TX_32X32 per chroma plane -- see [`b64_residual`], |
| 393 | +/// which carries that arm's own gate table. Still refused at the 64 root: | ||
| 394 | +/// compound references, a depth-1 4x32x32 var-tx tree, intra, and a | ||
| 395 | +/// superblock the true frame edge cuts through. | ||
| 396 | +/// | ||
| 397 | +/// Switched off by `EC_AV1_B64=0` in any build; `EC_AV1_B64RES=0` keeps the | ||
| 398 | +/// root and restores the skip-only shape lane-b64 shipped. | ||
| 393 | pub(crate) const B64_ROOT: bool = true; | 399 | pub(crate) const B64_ROOT: bool = true; |
| 394 | 400 | ||
| 395 | /// How many superblocks took the 64x64 root since the last | 401 | /// How many superblocks took the 64x64 root since the last |
| @@ -410,6 +416,57 @@ fn b64_root() -> bool { | |||
| 410 | ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::B64_ROOT)) | 416 | ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::B64_ROOT)) |
| 411 | } | 417 | } |
| 412 | 418 | ||
| 419 | +/// Whether the 64x64 root also prices a NON-skip candidate (lane-tx64): the | ||
| 420 | +/// winner's prediction through one TX_64X64 luma transform and two TX_32X32 | ||
| 421 | +/// chroma ones. Off by `EC_AV1_B64RES=0`, which restores the skip-only root | ||
| 422 | +/// lane-b64 shipped, so the two can be measured against each other on the | ||
| 423 | +/// same build. | ||
| 424 | +/// | ||
| 425 | +/// MEASURED on the native gate (two arms of one build, baseline reproduced to | ||
| 426 | +/// the tenth): both film rows improve on both columns -- film A | ||
| 427 | +/// +43.0/+15.0 -> +42.9/+14.8, film B +64.7/+34.0 -> +63.3/+31.5 -- and | ||
| 428 | +/// SCREEN CAPTURE loses +33.4/-23.8 -> +34.3/-23.4, outside the keep rule's | ||
| 429 | +/// +-0.3 screen clause. So the caller gates this off on a screen frame | ||
| 430 | +/// (`allow_screen_content_tools`), the same content gate the coding pyramid | ||
| 431 | +/// takes: a desktop capture's 64x64 superblocks are flat runs whose skip arm | ||
| 432 | +/// already codes them for nothing, and a residual there only spends bits the | ||
| 433 | +/// intra/palette tools would have spent better. | ||
| 434 | +/// | ||
| 435 | +/// CENSUS on film B (12 frames, q120, `EC_AV1_BITCENSUS=1` + | ||
| 436 | +/// `examples/syntax_census.rs`, both streams read by OUR decoder), off vs on: | ||
| 437 | +/// | ||
| 438 | +/// | | skip-only | + residual | | ||
| 439 | +/// |---|---|---| | ||
| 440 | +/// | 64x64 share, all frames | 36.4% | 38.3% | | ||
| 441 | +/// | 64x64 share, leaf frames | 53.3% | 56.0% | | ||
| 442 | +/// | skip area, all frames | 80.4% | 78.8% | | ||
| 443 | +/// | skip area, leaf frames | 90.2% | 88.5% | | ||
| 444 | +/// | stream bytes | 34,366 | 34,295 | | ||
| 445 | +/// | encoder wall | 13.4s | 9.6s | | ||
| 446 | +/// | ||
| 447 | +/// `base_luma_64` enters the leaf frames' top tables at 5.7%, i.e. the | ||
| 448 | +/// TX_64X64 coefficients are really being coded. The root wins MORE often | ||
| 449 | +/// with a residual to offer (rav1e speed 6's own 64x64 share on this clip is | ||
| 450 | +/// 45.2%), which is also why the arm is 28% FASTER despite costing three more | ||
| 451 | +/// transforms per superblock: a root that wins is four quadrant searches that | ||
| 452 | +/// never run. | ||
| 453 | +fn b64_residual() -> bool { | ||
| 454 | + static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| { | ||
| 455 | + crate::envflags::var("EC_AV1_B64RES").ok().map(|v| v != "0") | ||
| 456 | + }); | ||
| 457 | + ENV.unwrap_or(true) && b64_root() | ||
| 458 | +} | ||
| 459 | + | ||
| 460 | +/// How many 64x64 roots came out cheaper with a residual than skipped, since | ||
| 461 | +/// the last [`take_b64_residual_hits`] -- so a gate reports the fire rate | ||
| 462 | +/// rather than assuming one (class `gate-blind-to-feature`). | ||
| 463 | +static B64_RESIDUAL_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 464 | + | ||
| 465 | +/// The non-skip 64x64-root count since the last call, and zero it. | ||
| 466 | +pub fn take_b64_residual_hits() -> usize { | ||
| 467 | + B64_RESIDUAL_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) | ||
| 468 | +} | ||
| 469 | + | ||
| 413 | /// Whether the encoder searches loop restoration at all (spec 7.17): one | 470 | /// Whether the encoder searches loop restoration at all (spec 7.17): one |
| 414 | /// Wiener filter per 64x64 luma restoration unit, picked from | 471 | /// Wiener filter per 64x64 luma restoration unit, picked from |
| 415 | /// [`crate::filter_search::WIENER_CANDIDATES`]. Swept by `EC_AV1_LR` in a | 472 | /// [`crate::filter_search::WIENER_CANDIDATES`]. Swept by `EC_AV1_LR` in a |
| @@ -476,10 +533,29 @@ fn split_rd_threshold() -> f64 { | |||
| 476 | /// Whether `cost` over a `side` x `side` block is cheap enough per pixel that | 533 | /// Whether `cost` over a `side` x `side` block is cheap enough per pixel that |
| 477 | /// [`split_rd_threshold`] withholds the split trial. | 534 | /// [`split_rd_threshold`] withholds the split trial. |
| 478 | fn split_rd_breakout(cost: f64, side: usize, lambda: f64) -> bool { | 535 | fn split_rd_breakout(cost: f64, side: usize, lambda: f64) -> bool { |
| 479 | - let t = split_rd_threshold(); | 536 | + split_rd_breakout_at(split_rd_threshold(), cost, side, lambda) |
| 537 | +} | ||
| 538 | + | ||
| 539 | +/// [`split_rd_breakout`] at an explicit threshold -- what the 64x64 root's own | ||
| 540 | +/// early-out reads. | ||
| 541 | +fn split_rd_breakout_at(t: f64, cost: f64, side: usize, lambda: f64) -> bool { | ||
| 480 | t > 0.0 && cost < t * lambda * (side * side) as f64 | 542 | t > 0.0 && cost < t * lambda * (side * side) as f64 |
| 481 | } | 543 | } |
| 482 | 544 | ||
| 545 | +/// The threshold the 64x64 root's early-out uses: [`split_rd_threshold`] | ||
| 546 | +/// CLAMPED at the shipped [`SPLIT_RD_THRESHOLD`] (lane-tx64). | ||
| 547 | +/// | ||
| 548 | +/// The 32x32 level is allowed to loosen its breakout with the speed preset | ||
| 549 | +/// (`crate::speed::SPLIT_RD` steps to 0.5 at preset 6), but lane-b64 measured | ||
| 550 | +/// what that does one size up: at 0.5 a whole superblock is taken at 64x64 | ||
| 551 | +/// without its four quadrants ever being searched, and film B goes | ||
| 552 | +/// +91.5/+53.4 -> +114.6/+71.5. A 64x64 block is four times the area, so the | ||
| 553 | +/// same per-pixel threshold withholds four times as much search -- the | ||
| 554 | +/// preset's step is priced for the 32 level and does not carry up here. | ||
| 555 | +fn b64_breakout_threshold() -> f64 { | ||
| 556 | + split_rd_threshold().min(SPLIT_RD_THRESHOLD) | ||
| 557 | +} | ||
| 558 | + | ||
| 483 | /// [`SPLIT_BREAKOUT_COEFFS`], swept by `EC_AV1_SPLIT_BREAKOUT` in a test | 559 | /// [`SPLIT_BREAKOUT_COEFFS`], swept by `EC_AV1_SPLIT_BREAKOUT` in a test |
| 484 | /// build. | 560 | /// build. |
| 485 | fn split_breakout_coeffs() -> usize { | 561 | fn split_breakout_coeffs() -> usize { |
| @@ -1829,8 +1905,20 @@ impl Plane<'_> { | |||
| 1829 | bits: 0.0, | 1905 | bits: 0.0, |
| 1830 | }; | 1906 | }; |
| 1831 | } | 1907 | } |
| 1832 | - let mut residual = [0i32; BLOCK * BLOCK]; | 1908 | + // A 64x64 root's residual is four times the scratch every other |
| 1833 | - let residual = &mut residual[..side * side]; | 1909 | + // block coder here sizes for [`BLOCK`]; only that one case pays for |
| 1910 | + // a heap buffer, so the 4x4..32x32 path keeps its stack array. | ||
| 1911 | + let mut small = [0i32; BLOCK * BLOCK]; | ||
| 1912 | + let mut big = if side > BLOCK { | ||
| 1913 | + vec![0i32; side * side] | ||
| 1914 | + } else { | ||
| 1915 | + Vec::new() | ||
| 1916 | + }; | ||
| 1917 | + let residual: &mut [i32] = if side > BLOCK { | ||
| 1918 | + &mut big | ||
| 1919 | + } else { | ||
| 1920 | + &mut small[..side * side] | ||
| 1921 | + }; | ||
| 1834 | for row in 0..side { | 1922 | for row in 0..side { |
| 1835 | for col in 0..side { | 1923 | for col in 0..side { |
| 1836 | residual[row * side + col] = | 1924 | residual[row * side + col] = |
| @@ -1864,6 +1952,16 @@ impl Plane<'_> { | |||
| 1864 | .collect() | 1952 | .collect() |
| 1865 | }; | 1953 | }; |
| 1866 | let sse = self.block_sse(x, y, side, &reconstruction); | 1954 | let sse = self.block_sse(x, y, side, &reconstruction); |
| 1955 | + // A 64-point transform codes only its top-left 32x32 corner (spec | ||
| 1956 | + // 5.11.39's zero-out; `TxbSet::Luma64`'s tables are 32-sided and the | ||
| 1957 | + // key frame writer takes a 32x32 level grid for one). The inverse | ||
| 1958 | + // above reads the whole 64x64 array, but the price below and the | ||
| 1959 | + // levels the writer takes are that corner. | ||
| 1960 | + let levels = if side > BLOCK { | ||
| 1961 | + coded_corner(&levels, side, BLOCK) | ||
| 1962 | + } else { | ||
| 1963 | + levels | ||
| 1964 | + }; | ||
| 1867 | #[cfg(test)] | 1965 | #[cfg(test)] |
| 1868 | let t = stage_start(); | 1966 | let t = stage_start(); |
| 1869 | let bits = if cfg!(test) && std::env::var_os("EC_AV1_ESTIMATE").is_some() { | 1967 | let bits = if cfg!(test) && std::env::var_os("EC_AV1_ESTIMATE").is_some() { |
| @@ -5657,6 +5755,17 @@ struct Trial { | |||
| 5657 | bits: f64, | 5755 | bits: f64, |
| 5658 | } | 5756 | } |
| 5659 | 5757 | ||
| 5758 | +/// The top-left `to` x `to` corner of a `from`-sided level grid -- the part | ||
| 5759 | +/// of a 64-point transform that is actually coded (everything outside it is | ||
| 5760 | +/// zeroed by [`crate::transform::quantize`] already, so this only reshapes). | ||
| 5761 | +fn coded_corner(levels: &[i32], from: usize, to: usize) -> Vec<i32> { | ||
| 5762 | + let mut out = vec![0i32; to * to]; | ||
| 5763 | + for row in 0..to { | ||
| 5764 | + out[row * to..][..to].copy_from_slice(&levels[row * from..][..to]); | ||
| 5765 | + } | ||
| 5766 | + out | ||
| 5767 | +} | ||
| 5768 | + | ||
| 5660 | /// The non-zero levels of a block, as the tile writer takes them. | 5769 | /// The non-zero levels of a block, as the tile writer takes them. |
| 5661 | fn coeffs(levels: &[i32], side: usize) -> Vec<Coeff> { | 5770 | fn coeffs(levels: &[i32], side: usize) -> Vec<Coeff> { |
| 5662 | levels | 5771 | levels |
| @@ -7214,6 +7323,9 @@ fn search_skip_64( | |||
| 7214 | chroma: &mut [Plane; 2], | 7323 | chroma: &mut [Plane; 2], |
| 7215 | (x, y): (usize, usize), | 7324 | (x, y): (usize, usize), |
| 7216 | search: &Search, | 7325 | search: &Search, |
| 7326 | + // Whether the non-skip arm is priced at all ([`b64_residual`], and off on | ||
| 7327 | + // screen content -- see that function's doc). | ||
| 7328 | + residual: bool, | ||
| 7217 | reference: &Picture, | 7329 | reference: &Picture, |
| 7218 | stack: &MvStack, | 7330 | stack: &MvStack, |
| 7219 | fctx: &crate::decode::FrameCtx, | 7331 | fctx: &crate::decode::FrameCtx, |
| @@ -7282,7 +7394,7 @@ fn search_skip_64( | |||
| 7282 | } | 7394 | } |
| 7283 | } | 7395 | } |
| 7284 | 7396 | ||
| 7285 | - let mut best: Option<(f64, InterInfo, [Trial; 3])> = None; | 7397 | + let mut best: Option<(f64, f64, InterInfo, [Trial; 3])> = None; |
| 7286 | for (bits, info) in cands { | 7398 | for (bits, info) in cands { |
| 7287 | let trials = [ | 7399 | let trials = [ |
| 7288 | (0usize, x, y, side, true), | 7400 | (0usize, x, y, side, true), |
| @@ -7326,18 +7438,66 @@ fn search_skip_64( | |||
| 7326 | + trials[2].sse | 7438 | + trials[2].sse |
| 7327 | + search.lambda * (fixed + bits); | 7439 | + search.lambda * (fixed + bits); |
| 7328 | if best.as_ref().is_none_or(|b| cost < b.0) { | 7440 | if best.as_ref().is_none_or(|b| cost < b.0) { |
| 7329 | - best = Some((cost, info, trials)); | 7441 | + best = Some((cost, bits, info, trials)); |
| 7442 | + } | ||
| 7443 | + } | ||
| 7444 | + let (skip_cost, bits, info, skip_trials) = best?; | ||
| 7445 | + | ||
| 7446 | + // The winner's prediction coded with a REAL residual (lane-tx64): one | ||
| 7447 | + // TX_64X64 luma transform -- `TxbSet::Luma64`, whose coded quarter is the | ||
| 7448 | + // 32x32 corner `code_from_prediction` hands back -- and one TX_32X32 per | ||
| 7449 | + // chroma plane, since a 64x64 root's chroma block IS 32x32. Only the mv | ||
| 7450 | + // the skip arm already chose is re-coded: the prediction with the lowest | ||
| 7451 | + // SSE is also the cheapest residual to code, and a second 64x64 motion | ||
| 7452 | + // search per superblock is the wall this root was taken to SAVE. | ||
| 7453 | + // | ||
| 7454 | + // Priced against the skip arm through the same `symbol_bits` path, so the | ||
| 7455 | + // two numbers are comparable: `fixed` charged `skip` coded 1, and this | ||
| 7456 | + // arm charges 0 instead and adds its three planes' coefficient bits. | ||
| 7457 | + let mut chosen = (skip_cost, true, skip_trials); | ||
| 7458 | + if residual { | ||
| 7459 | + let sets = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32]; | ||
| 7460 | + let residual_trials = [0usize, 1, 2].map(|plane| { | ||
| 7461 | + let (target, px, py, s) = if plane == 0 { | ||
| 7462 | + (&*luma, x, y, side) | ||
| 7463 | + } else { | ||
| 7464 | + (&chroma[plane - 1], x / 2, y / 2, side / 2) | ||
| 7465 | + }; | ||
| 7466 | + target.code_from_prediction( | ||
| 7467 | + px, | ||
| 7468 | + py, | ||
| 7469 | + s, | ||
| 7470 | + &chosen.2[plane].reconstruction, | ||
| 7471 | + false, | ||
| 7472 | + search.base_q_idx, | ||
| 7473 | + search.deadzone, | ||
| 7474 | + sets[plane], | ||
| 7475 | + ) | ||
| 7476 | + }); | ||
| 7477 | + let unskip = symbol_bits(&cdf::SKIP[0], 0) - symbol_bits(&cdf::SKIP[0], 1); | ||
| 7478 | + let coeff_bits: f64 = residual_trials.iter().map(|t| t.bits).sum(); | ||
| 7479 | + let cost = residual_trials.iter().map(|t| t.sse).sum::<f64>() | ||
| 7480 | + + search.lambda * (fixed + bits + unskip + coeff_bits); | ||
| 7481 | + if cost < skip_cost { | ||
| 7482 | + B64_RESIDUAL_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 7483 | + chosen = (cost, false, residual_trials); | ||
| 7330 | } | 7484 | } |
| 7331 | } | 7485 | } |
| 7332 | - let (cost, info, trials) = best?; | 7486 | + |
| 7487 | + let (cost, skip, trials) = chosen; | ||
| 7333 | luma.commit(x, y, side, &trials[0]); | 7488 | luma.commit(x, y, side, &trials[0]); |
| 7334 | chroma[0].commit(x / 2, y / 2, side / 2, &trials[1]); | 7489 | chroma[0].commit(x / 2, y / 2, side / 2, &trials[1]); |
| 7335 | chroma[1].commit(x / 2, y / 2, side / 2, &trials[2]); | 7490 | chroma[1].commit(x / 2, y / 2, side / 2, &trials[2]); |
| 7336 | Some(( | 7491 | Some(( |
| 7337 | cost, | 7492 | cost, |
| 7338 | BlockCoeffs { | 7493 | BlockCoeffs { |
| 7339 | - skip: true, | 7494 | + skip, |
| 7340 | inter: Some(info), | 7495 | inter: Some(info), |
| 7496 | + // Both grids are 32-sided: the luma one is the 64-point | ||
| 7497 | + // transform's coded corner, the chroma ones are whole TX_32X32. | ||
| 7498 | + luma: if skip { Vec::new() } else { coeffs(&trials[0].levels, BLOCK) }, | ||
| 7499 | + u: if skip { Vec::new() } else { coeffs(&trials[1].levels, side / 2) }, | ||
| 7500 | + v: if skip { Vec::new() } else { coeffs(&trials[2].levels, side / 2) }, | ||
| 7341 | ..BlockCoeffs::default() | 7501 | ..BlockCoeffs::default() |
| 7342 | }, | 7502 | }, |
| 7343 | )) | 7503 | )) |
| @@ -9027,6 +9187,7 @@ pub(crate) fn encode_inter_frame( | |||
| 9027 | &mut chroma, | 9187 | &mut chroma, |
| 9028 | (x64, y64), | 9188 | (x64, y64), |
| 9029 | &sb_search, | 9189 | &sb_search, |
| 9190 | + b64_residual() && !screen, | ||
| 9030 | reference, | 9191 | reference, |
| 9031 | &stack, | 9192 | &stack, |
| 9032 | fctx, | 9193 | fctx, |
| @@ -9042,9 +9203,14 @@ pub(crate) fn encode_inter_frame( | |||
| 9042 | // pixel is taken outright and its quadrants are never searched -- | 9203 | // pixel is taken outright and its quadrants are never searched -- |
| 9043 | // the same `split_rd_breakout` shape the 32x32 level uses one | 9204 | // the same `split_rd_breakout` shape the 32x32 level uses one |
| 9044 | // size up, and where this lever BUYS wall instead of spending it. | 9205 | // size up, and where this lever BUYS wall instead of spending it. |
| 9045 | - let early64 = sb64 | 9206 | + let early64 = sb64.as_ref().is_some_and(|(c, _, _)| { |
| 9046 | - .as_ref() | 9207 | + split_rd_breakout_at( |
| 9047 | - .is_some_and(|(c, _, _)| split_rd_breakout(*c, SUPERBLOCK, sb_search.lambda)); | 9208 | + b64_breakout_threshold(), |
| 9209 | + *c, | ||
| 9210 | + SUPERBLOCK, | ||
| 9211 | + sb_search.lambda, | ||
| 9212 | + ) | ||
| 9213 | + }); | ||
| 9048 | for quadrant in 0..if early64 { 0 } else { 4 } { | 9214 | for quadrant in 0..if early64 { 0 } else { 4 } { |
| 9049 | let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); | 9215 | let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); |
| 9050 | // lane-av1tpl2: this 32x32 block's own lambda -- every RD | 9216 | // lane-av1tpl2: this 32x32 block's own lambda -- every RD |
| @@ -13348,14 +13514,14 @@ mod tests { | |||
| 13348 | // the blocks that take one code a filter-intra mode, a different | 13514 | // the blocks that take one code a filter-intra mode, a different |
| 13349 | // prediction and different coefficients -- 7321 -> 7373 at q=150 and | 13515 | // prediction and different coefficients -- 7321 -> 7373 at q=150 and |
| 13350 | // 27285 -> 27074 at q=60. `EC_AV1_FILTER_INTRA=0` restores these. | 13516 | // 27285 -> 27074 at q=60. `EC_AV1_FILTER_INTRA=0` restores these. |
| 13351 | - // Re-taken on lane-b64 (64x64 skip root, `EC_AV1_B64=0` restores | 13517 | + // Re-taken on lane-b64 (64x64 skip root), at the lane-pyr4 merge |
| 13352 | - // 8076 / 27585 under the 16:-24:16 pyramid) and again at the lane-pyr4 | 13518 | + // (default pyramid `8:-32:16`, one truncated group of 8 with its |
| 13353 | - // merge: the default pyramid is `8:-32:16` now, the long-GOP sweep's | 13519 | + // hidden ALTREF at `q-32`), and at the lane-tx64 merge: the 64x64 |
| 13354 | - // shape (`lanes/pyr4.sweep.txt`), so these four pictures code as one | 13520 | + // root can code a REAL residual now (one TX_64X64 luma transform and |
| 13355 | - // truncated group of 8 with its hidden ALTREF at `q-32`. | 13521 | + // two TX_32X32 chroma ones). `EC_AV1_B64RES=0` restores 8446 / 28548; |
| 13356 | - // `EC_AV1_PYRAMID=16:-24:16` restores 7291 / 27466. | 13522 | + // `EC_AV1_B64=0` gives the pre-root bytes under this pyramid. |
| 13357 | let pins: [(u8, usize, u64); 2] = | 13523 | let pins: [(u8, usize, u64); 2] = |
| 13358 | - [(150, 8446, 0xaf24_d4bc_c9a3_9819), (60, 28548, 0x1459_61a1_f2b6_0f05)]; | 13524 | + [(150, 8590, 0x407d_4c27_d24f_d830), (60, 28535, 0xf033_c999_93a3_5b27)]; |
| 13359 | for (q, bytes, hash) in pins { | 13525 | for (q, bytes, hash) in pins { |
| 13360 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); | 13526 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); |
| 13361 | assert_eq!( | 13527 | assert_eq!( |
crates/ec-av1/src/encoder.rsmodified+69 -0
| @@ -2413,6 +2413,75 @@ mod tests { | |||
| 2413 | } | 2413 | } |
| 2414 | } | 2414 | } |
| 2415 | 2415 | ||
| 2416 | + /// The same clip with a DC step between frames (lane-tx64): the 64x64 | ||
| 2417 | + /// root still predicts the whole superblock from the last frame, but the | ||
| 2418 | + /// prediction is uniformly off, so the root's NON-skip arm -- one | ||
| 2419 | + /// TX_64X64 luma transform and two TX_32X32 chroma ones -- prices below | ||
| 2420 | + /// its skip arm and a 64x64 residual is really written. The counter is | ||
| 2421 | + /// the gate (class `gate-blind-to-feature`; the static clip above cannot | ||
| 2422 | + /// reach this arm at all because its prediction is exact), and both | ||
| 2423 | + /// decoders have to reconstruct those coefficients sample-exact -- a | ||
| 2424 | + /// writer that names the wrong coefficient set or the wrong `txfm_split` | ||
| 2425 | + /// desyncs the whole tile here. | ||
| 2426 | + #[test] | ||
| 2427 | + fn a_dc_stepped_clip_codes_a_64x64_residual_and_decodes_sample_exact() { | ||
| 2428 | + let _gate_lock = crate::stream::tests::lock_gate_counters(); | ||
| 2429 | + let (width, height) = (256usize, 128usize); | ||
| 2430 | + let still = test_card(width, height, 0); | ||
| 2431 | + let sources: Vec<Picture> = (0..3) | ||
| 2432 | + .map(|t| { | ||
| 2433 | + let mut p = still.clone(); | ||
| 2434 | + let step = (t * 6) as u16; | ||
| 2435 | + for v in &mut p.y { | ||
| 2436 | + *v = (*v + step).min(255); | ||
| 2437 | + } | ||
| 2438 | + p | ||
| 2439 | + }) | ||
| 2440 | + .collect(); | ||
| 2441 | + let config = EncoderConfig { | ||
| 2442 | + width, | ||
| 2443 | + height, | ||
| 2444 | + base_q_idx: 120, | ||
| 2445 | + gop: 8, | ||
| 2446 | + colour: Colour::Bt709Limited, | ||
| 2447 | + tile_cols_log2: 0, | ||
| 2448 | + tile_rows_log2: 0, | ||
| 2449 | + }; | ||
| 2450 | + let mut enc = Av1Encoder::new(config).unwrap(); | ||
| 2451 | + let mut stream = Vec::new(); | ||
| 2452 | + let _ = crate::encode::take_b64_residual_hits(); | ||
| 2453 | + for packet in encode_all(&mut enc, &sources) { | ||
| 2454 | + stream.extend_from_slice(&packet.data); | ||
| 2455 | + } | ||
| 2456 | + let residuals = crate::encode::take_b64_residual_hits(); | ||
| 2457 | + assert!( | ||
| 2458 | + residuals > 0, | ||
| 2459 | + "a DC-stepped clip coded no 64x64 root with a residual at all" | ||
| 2460 | + ); | ||
| 2461 | + eprintln!("64x64 roots with a residual: {residuals} of 16 inter superblocks"); | ||
| 2462 | + | ||
| 2463 | + let ours = crate::stream::decode_stream(&stream).expect("our decoder"); | ||
| 2464 | + assert_eq!(ours.len(), sources.len(), "our decoder's frames"); | ||
| 2465 | + if !have_ffmpeg() { | ||
| 2466 | + eprintln!("SKIP the ffmpeg half: no ffmpeg"); | ||
| 2467 | + return; | ||
| 2468 | + } | ||
| 2469 | + let theirs = ffmpeg_decode_luma(&stream, width, height); | ||
| 2470 | + assert_eq!(theirs.len(), sources.len(), "ffmpeg's frames"); | ||
| 2471 | + for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() { | ||
| 2472 | + let got: Vec<u8> = a.y.iter().map(|&v| v as u8).collect(); | ||
| 2473 | + if let Some(at) = got.iter().zip(b).position(|(x, y)| x != y) { | ||
| 2474 | + panic!( | ||
| 2475 | + "frame {i}: luma differs first at ({}, {}): ours {} vs ffmpeg {}", | ||
| 2476 | + at % width, | ||
| 2477 | + at / width, | ||
| 2478 | + got[at], | ||
| 2479 | + b[at], | ||
| 2480 | + ); | ||
| 2481 | + } | ||
| 2482 | + } | ||
| 2483 | + } | ||
| 2484 | + | ||
| 2416 | #[test] | 2485 | #[test] |
| 2417 | fn every_tile_layout_decodes_sample_exact_through_both_decoders() { | 2486 | fn every_tile_layout_decodes_sample_exact_through_both_decoders() { |
| 2418 | let _knobs = crate::speed::knob_read(); | 2487 | let _knobs = crate::speed::knob_read(); |
crates/ec-av1/src/speed.rsmodified+19 -21
| @@ -149,27 +149,25 @@ pub(crate) fn at<T: Copy>(table: &[T; 11]) -> T { | |||
| 149 | /// film B +70.5/+35.2 -> +64.7/+34.0, screen +48.2/-16.4 -> +33.4/-23.8) at | 149 | /// film B +70.5/+35.2 -> +64.7/+34.0, screen +48.2/-16.4 -> +33.4/-23.8) at |
| 150 | /// -4% to -20% wall on four of the five rows. | 150 | /// -4% to -20% wall on four of the five rows. |
| 151 | /// | 151 | /// |
| 152 | -/// MEASURED OFF from preset 4 up (lane-b64, native gate at `EC_AV1_SPEED=6`, | 152 | +/// lane-b64 MEASURED it off from preset 4 up: at `EC_AV1_SPEED=6` ON cost |
| 153 | -/// two arms side by side): ON costs film A +55.1/+24.8 -> +64.5/+37.5 and | 153 | +/// film A +55.1/+24.8 -> +64.5/+37.5 and film B +91.5/+53.4 -> +114.6/+71.5. |
| 154 | -/// film B +91.5/+53.4 -> +114.6/+71.5 for -16%/-36% wall. The cause is the | 154 | +/// The cause was named there and FIXED in lane-tx64: the trial's early-out |
| 155 | -/// trial's own early-out, which rides [`SPLIT_RD`]: at 0.5 (preset 6) a | 155 | +/// rode [`SPLIT_RD`], so at 0.5 (preset 6) a superblock was taken whole with |
| 156 | -/// superblock is taken whole without its quadrants ever being searched, and | 156 | +/// its quadrants never searched. `encode::b64_breakout_threshold` clamps that |
| 157 | -/// film B's q150 point loses 1.2 dB. Presets 1..=3 keep it because they keep | 157 | +/// early-out at the shipped 0.125 whatever the preset, and the same gate at |
| 158 | -/// `SPLIT_RD` at 0.125, i.e. the early-out they get is the measured one; | 158 | +/// `EC_AV1_SPEED=6` now reads, every row better on both columns: |
| 159 | -/// 4 and up switch it off with `SPLIT_RD`'s first step. | 159 | +/// |
| 160 | -pub(crate) const B64_ROOT: [bool; 11] = [ | 160 | +/// | row | B64 off | B64 on, clamped | |
| 161 | - crate::encode::B64_ROOT, | 161 | +/// |---|---|---| |
| 162 | - true, | 162 | +/// | bars 1080p | +88.9 / +54.8 | +79.5 / +47.2 | |
| 163 | - true, | 163 | +/// | bars 2160p | +91.6 / +49.8 | +72.6 / +35.1 | |
| 164 | - true, | 164 | +/// | film A | +55.1 / +24.8 | +51.0 / +21.7 | |
| 165 | - false, | 165 | +/// | film B | +91.5 / +53.4 | +77.9 / +44.3 | |
| 166 | - false, | 166 | +/// | screen | +57.7 / -11.7 | +40.3 / -20.0 | |
| 167 | - false, | 167 | +/// |
| 168 | - false, | 168 | +/// at 28.1s vs 30.8s (film A), 31.3s vs 28.0s (film B), 14.3s vs 15.2s |
| 169 | - false, | 169 | +/// (screen). So it is on at every preset. |
| 170 | - false, | 170 | +pub(crate) const B64_ROOT: [bool; 11] = [crate::encode::B64_ROOT; 11]; |
| 171 | - false, | ||
| 172 | -]; | ||
| 173 | 171 | ||
| 174 | /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split | 172 | /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split |
| 175 | /// trial is withheld. The single biggest wall lever in the tile search. | 173 | /// trial is withheld. The single biggest wall lever in the tile search. |
crates/ec-av1/src/tile.rsmodified+68 -18
| @@ -1770,9 +1770,10 @@ pub enum Quadrant { | |||
| 1770 | /// and the other three carry [`Covered`](Quadrant::Covered), so the | 1770 | /// and the other three carry [`Covered`](Quadrant::Covered), so the |
| 1771 | /// per-32x32-entry shape the inter writer takes is unchanged. | 1771 | /// per-32x32-entry shape the inter writer takes is unchanged. |
| 1772 | /// | 1772 | /// |
| 1773 | - /// The block is coded SKIP and single-reference: a 64x64 residual needs | 1773 | + /// The block is single-reference; the writer refuses a compound one. It |
| 1774 | - /// the forward TX_64X64 the encoder does not have (`transform.rs` tops | 1774 | + /// may be skipped or carry a real residual -- one TX_64X64 luma transform |
| 1775 | - /// out at 32x32), and the writer refuses anything else. | 1775 | + /// whose coded quarter is a 32x32 level grid, and one TX_32X32 per chroma |
| 1776 | + /// plane (lane-tx64, `crate::encode::b64_residual`). | ||
| 1776 | Whole64(BlockCoeffs), | 1777 | Whole64(BlockCoeffs), |
| 1777 | /// A quadrant a [`Whole64`](Quadrant::Whole64) at its superblock's | 1778 | /// A quadrant a [`Whole64`](Quadrant::Whole64) at its superblock's |
| 1778 | /// top-left already coded; it carries no block of its own. | 1779 | /// top-left already coded; it carries no block of its own. |
| @@ -4365,9 +4366,24 @@ pub(crate) fn predicted_coeff_bits(blocks: &[Quadrant], base_q_idx: u8) -> f64 { | |||
| 4365 | let side = match q { | 4366 | let side = match q { |
| 4366 | Quadrant::Whole(_) => 32, | 4367 | Quadrant::Whole(_) => 32, |
| 4367 | Quadrant::Split(_) => 16, | 4368 | Quadrant::Split(_) => 16, |
| 4368 | - // A 64x64 root is skip-only, so it carries no coefficient | 4369 | + // lane-tx64: a 64x64 root that codes a residual pays for one |
| 4369 | - // bits to predict at all (`Quadrant::Whole64`). | 4370 | + // TX_64X64 luma transform (only the top-left 32x32 carries |
| 4370 | - Quadrant::Whole64(_) | Quadrant::Covered => return 0.0, | 4371 | + // coefficients) and one TX_32X32 per chroma plane -- the same |
| 4372 | + // three prices `predicted_coeff_bits_sb` takes for a key | ||
| 4373 | + // frame's whole superblock, and the same sets the writer's | ||
| 4374 | + // `Whole64` arm codes. `block_bits`'s table has no 64 row, so | ||
| 4375 | + // it is spelled out here. | ||
| 4376 | + Quadrant::Whole64(block) => { | ||
| 4377 | + return match block.skip { | ||
| 4378 | + true => 0.0, | ||
| 4379 | + false => { | ||
| 4380 | + coeff_bits(&dense(&block.luma, 32), TxbSet::Luma64, q_ctx, 0, 0) | ||
| 4381 | + + coeff_bits(&dense(&block.u, 32), TxbSet::Chroma32, q_ctx, 0, 0) | ||
| 4382 | + + coeff_bits(&dense(&block.v, 32), TxbSet::Chroma32, q_ctx, 0, 0) | ||
| 4383 | + } | ||
| 4384 | + }; | ||
| 4385 | + } | ||
| 4386 | + Quadrant::Covered => return 0.0, | ||
| 4371 | }; | 4387 | }; |
| 4372 | q.blocks().iter().map(|b| block_bits(b, side, q_ctx)).sum::<f64>() | 4388 | q.blocks().iter().map(|b| block_bits(b, side, q_ctx)).sum::<f64>() |
| 4373 | }) | 4389 | }) |
| @@ -5558,6 +5574,12 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 5558 | // derived from luma's, not coded, so it never differs between the two. | 5574 | // derived from luma's, not coded, so it never differs between the two. |
| 5559 | let intra_planes = [TxbSet::Luma32, TxbSet::Chroma16, TxbSet::Chroma16]; | 5575 | let intra_planes = [TxbSet::Luma32, TxbSet::Chroma16, TxbSet::Chroma16]; |
| 5560 | let inter_planes = [TxbSet::Luma32Inter, TxbSet::Chroma16, TxbSet::Chroma16]; | 5576 | let inter_planes = [TxbSet::Luma32Inter, TxbSet::Chroma16, TxbSet::Chroma16]; |
| 5577 | + // A 64x64 root's planes (lane-tx64): TX_64X64 luma -- DCT-only at this | ||
| 5578 | + // size for an inter block too (spec `av1_get_ext_tx_set_type` returns | ||
| 5579 | + // `TX_SET_DCTONLY` for `tx_size_sqr_up == TX_64X64`), so `Luma64` carries | ||
| 5580 | + // no `tx_type` symbol and there is no `Luma64Inter` to write -- and one | ||
| 5581 | + // TX_32X32 per chroma plane. | ||
| 5582 | + let sb64_planes = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32]; | ||
| 5561 | let scan32 = default_scan(TX32); | 5583 | let scan32 = default_scan(TX32); |
| 5562 | let scan16 = default_scan(TX16); | 5584 | let scan16 = default_scan(TX16); |
| 5563 | let scan8 = default_scan(TX8); | 5585 | let scan8 = default_scan(TX8); |
| @@ -5593,8 +5615,9 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 5593 | ); | 5615 | ); |
| 5594 | // lane-b64: a superblock the search left WHOLE — one 64x64 block | 5616 | // lane-b64: a superblock the search left WHOLE — one 64x64 block |
| 5595 | // at `PARTITION_NONE`, carried by its top-left quadrant's entry | 5617 | // at `PARTITION_NONE`, carried by its top-left quadrant's entry |
| 5596 | - // ([`Quadrant::Whole64`]). Coded skip and single-reference: no | 5618 | + // ([`Quadrant::Whole64`]). Single-reference; skipped, or with a |
| 5597 | - // residual, so no TX_64X64 coefficients and no `cdef_idx`. | 5619 | + // real residual through `TxbSet::Luma64` + two `Chroma32` |
| 5620 | + // (lane-tx64). | ||
| 5598 | let sb64 = ((sb_r * 2) < rows && (sb_c * 2) < cols) | 5621 | let sb64 = ((sb_r * 2) < rows && (sb_c * 2) < cols) |
| 5599 | .then(|| &blocks[((sb_r * 2) * cols + sb_c * 2) as usize]) | 5622 | .then(|| &blocks[((sb_r * 2) * cols + sb_c * 2) as usize]) |
| 5600 | .and_then(|q| match q { | 5623 | .and_then(|q| match q { |
| @@ -5611,11 +5634,10 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 5611 | let info = block.inter.ok_or_else(|| { | 5634 | let info = block.inter.ok_or_else(|| { |
| 5612 | Error::unsupported("AV1 tile", "a 64x64 root block is coded inter") | 5635 | Error::unsupported("AV1 tile", "a 64x64 root block is coded inter") |
| 5613 | })?; | 5636 | })?; |
| 5614 | - if !block.skip || info.ref1.is_some() { | 5637 | + if info.ref1.is_some() { |
| 5615 | return Err(Error::unsupported( | 5638 | return Err(Error::unsupported( |
| 5616 | "AV1 tile", | 5639 | "AV1 tile", |
| 5617 | - "a 64x64 root block is coded skip and single-reference \ | 5640 | + "a 64x64 root block is coded single-reference", |
| 5618 | - (a 64x64 residual needs a forward TX_64X64)", | ||
| 5619 | )); | 5641 | )); |
| 5620 | } | 5642 | } |
| 5621 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w64[sb_ctx]); | 5643 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w64[sb_ctx]); |
| @@ -5624,8 +5646,8 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 5624 | let has_left = neighbours.has_left(mi_c); | 5646 | let has_left = neighbours.has_left(mi_c); |
| 5625 | let skip_ctx = usize::from(neighbours.above_skip[mi_c]) | 5647 | let skip_ctx = usize::from(neighbours.above_skip[mi_c]) |
| 5626 | + usize::from(neighbours.left_skip[mi_r]); | 5648 | + usize::from(neighbours.left_skip[mi_r]); |
| 5627 | - enc.symbol(1, &mut cdfs.skip[skip_ctx]); | 5649 | + enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); |
| 5628 | - write_cdef_idx(&mut enc, (mi_r, mi_c), true); | 5650 | + write_cdef_idx(&mut enc, (mi_r, mi_c), block.skip); |
| 5629 | let ii_ctx = intra_inter_ctx( | 5651 | let ii_ctx = intra_inter_ctx( |
| 5630 | has_above, | 5652 | has_above, |
| 5631 | has_left, | 5653 | has_left, |
| @@ -5694,8 +5716,11 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 5694 | )?; | 5716 | )?; |
| 5695 | if tx_select { | 5717 | if tx_select { |
| 5696 | // A skipped inter block writes no `txfm_split` at all -- | 5718 | // A skipped inter block writes no `txfm_split` at all -- |
| 5697 | - // this only publishes the max transform size the reader | 5719 | + // that call only publishes the max transform size the |
| 5698 | - // infers for it (`write_tx_syntax_inter`). | 5720 | + // reader infers for it; an unskipped one writes the |
| 5721 | + // `txfm_split` flag, 0 here because a 64x64 root's luma | ||
| 5722 | + // is one whole TX_64X64 (depth 1 is lane-tx64's deferred | ||
| 5723 | + // var-tx arm). | ||
| 5699 | write_tx_syntax_inter( | 5724 | write_tx_syntax_inter( |
| 5700 | &mut enc, | 5725 | &mut enc, |
| 5701 | &mut cdfs, | 5726 | &mut cdfs, |
| @@ -5703,12 +5728,37 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 5703 | (mi_r, mi_c), | 5728 | (mi_r, mi_c), |
| 5704 | SB, | 5729 | SB, |
| 5705 | true, | 5730 | true, |
| 5706 | - true, | 5731 | + block.skip, |
| 5707 | 0, | 5732 | 0, |
| 5708 | ); | 5733 | ); |
| 5709 | } | 5734 | } |
| 5710 | - neighbours.record(sb_at, SB, 0, &zero_grids); | 5735 | + if block.skip { |
| 5711 | - neighbours.record_inter(sb_at, SB, true, true, block_ref(block)); | 5736 | + neighbours.record(sb_at, SB, 0, &zero_grids); |
| 5737 | + } else { | ||
| 5738 | + // lane-tx64: the 64x64 root's real residual. Luma is one | ||
| 5739 | + // TX_64X64 whose coded quarter is a 32x32 level grid | ||
| 5740 | + // (`TxbSet::Luma64`, `TX32`'s own scan -- the same pair | ||
| 5741 | + // `sb_coeff_key_frame_tile`'s `Superblock::Whole` writes), | ||
| 5742 | + // chroma one whole TX_32X32 per plane, and `split` is | ||
| 5743 | + // false because the luma transform covers the block. | ||
| 5744 | + let grids = [ | ||
| 5745 | + level_grid(&block.luma, TX32)?, | ||
| 5746 | + level_grid(&block.u, TX32)?, | ||
| 5747 | + level_grid(&block.v, TX32)?, | ||
| 5748 | + ]; | ||
| 5749 | + write_block_planes( | ||
| 5750 | + &mut enc, | ||
| 5751 | + &mut cdfs, | ||
| 5752 | + &sb64_planes, | ||
| 5753 | + &grids, | ||
| 5754 | + &[&scan32, &scan32, &scan32], | ||
| 5755 | + &neighbours.around(sb_at, SB), | ||
| 5756 | + 0, | ||
| 5757 | + false, | ||
| 5758 | + ); | ||
| 5759 | + neighbours.record_planes(sb_at, SB, 0, &grids, true); | ||
| 5760 | + } | ||
| 5761 | + neighbours.record_inter(sb_at, SB, block.skip, true, block_ref(block)); | ||
| 5712 | continue; | 5762 | continue; |
| 5713 | } | 5763 | } |
| 5714 | match (has_cols, has_rows) { | 5764 | match (has_cols, has_rows) { |