diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index e33ccdbc..6389db90 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -4112,15 +4112,32 @@ pub(crate) fn intra_inter_bits_at(stack: &crate::mvstack::MvStack, inter: bool) symbol_bits(&cdf::INTRA_INTER[ctx], usize::from(inter)) } +/// The `comp_mode = 0` symbol tile.rs' `write_comp_mode` codes ahead of a +/// SINGLE-reference block's reference syntax, at `reference_mode_ctx`'s own +/// context -- zero on a frame that carries no `reference_select` bit, where +/// the writer codes no symbol at all. +fn comp_mode_single_bits(stack: &crate::mvstack::MvStack) -> f64 { + if !reference_select() { + return 0.0; + } + symbol_bits( + &cdf::COMP_MODE[crate::mvstack::reference_mode_ctx(stack.above_nbr, stack.left_nbr)], + 0, + ) +} + /// What tile.rs' `write_single_ref` pays for `ref_frame`, symbol for symbol, -/// at the fixed context this RD pricer uses for every neighbour-derived -/// symbol (the writer contexts each symbol off its above/left neighbours' -/// references; the search does not have them). LAST is p1=0,p3=0,p4=0, +/// each at the context that symbol's own `single_ref_p*_ctx` derives from the +/// block's above/left neighbours -- plus the `comp_mode = 0` symbol +/// `write_comp_mode` codes ahead of it on a `reference_select` frame. +/// LAST is p1=0,p3=0,p4=0, /// LAST2 p1=0,p3=0,p4=1, LAST3 p1=0,p3=1,p5=0, GOLDEN p1=0,p3=1,p5=1, /// BWDREF p1=1,p2=0,p6=0, ALTREF2 p1=1,p2=0,p6=1 and ALTREF p1=1,p2=1. /// Before lane-pricer every BACKWARD reference was priced as LAST -- three /// symbols off the wrong CDFs -- which is what made the search believe an -/// ALTREF block cost the same syntax as a LAST one. +/// ALTREF block cost the same syntax as a LAST one. The `comp_mode` term is +/// lane-ctx2's: a compound candidate paid `comp_mode = 1` while a single one +/// paid nothing at all, so compound was overcharged by the whole symbol. pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) -> f64 { use crate::mvstack::{ ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME, @@ -4129,6 +4146,7 @@ pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) -> }; let (a, a1) = stack.above_refs; let (l, l1) = stack.left_refs; + let comp_mode = comp_mode_single_bits(stack); let backward = ref_frame >= BWDREF_FRAME; let p1 = symbol_bits( &cdf::SINGLE_REF[single_ref_p1_ctx(a, a1, l, l1)][0], @@ -4140,7 +4158,8 @@ pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) -> &cdf::SINGLE_REF[single_ref_p2_ctx(a, a1, l, l1)][1], usize::from(is_altref), ); - return p1 + return comp_mode + + p1 + p2 + if is_altref { 0.0 @@ -4152,7 +4171,7 @@ pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) -> }; } let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME; - p1 + symbol_bits( + comp_mode + p1 + symbol_bits( &cdf::SINGLE_REF[single_ref_p3_ctx(a, a1, l, l1)][2], usize::from(far), ) + if far { @@ -12349,11 +12368,16 @@ mod tests { LAST_FRAME, MvStack, single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx, }; - let stack_with = |above_refs, left_refs| MvStack { + let stack_with = |above_refs, left_refs, above_nbr, left_nbr| MvStack { above_refs, left_refs, + above_nbr, + left_nbr, ..bare_stack() }; + let nbr = |ref0, ref1| { + Some(crate::mvstack::NeighbourRef { is_inter: true, ref0, ref1, uni: false }) + }; // (reference, the symbols `write_single_ref` emits as (p index, value)) let cases: [(i8, &[(usize, usize)]); 7] = [ (LAST_FRAME, &[(0, 0), (2, 0), (3, 0)]), @@ -12364,13 +12388,20 @@ mod tests { (ALTREF2_FRAME, &[(0, 1), (1, 0), (5, 1)]), (ALTREF_FRAME, &[(0, 1), (1, 1)]), ]; - // Both neighbours intra, and the mixed LAST-above/ALTREF-left pair - // that puts every p* context on a different row. - for (above_refs, left_refs) in [ - ((None, None), (None, None)), - ((Some(LAST_FRAME), None), (Some(ALTREF_FRAME), Some(LAST_FRAME))), + // Both neighbours intra, then the mixed LAST-above/ALTREF-left pair + // that puts every p* context on a different row -- the second with a + // COMPOUND left neighbour, which is what takes `reference_mode_ctx` + // off row 0. + for (above_refs, left_refs, above_nbr, left_nbr) in [ + ((None, None), (None, None), None, None), + ( + (Some(LAST_FRAME), None), + (Some(ALTREF_FRAME), Some(LAST_FRAME)), + nbr(LAST_FRAME, None), + nbr(ALTREF_FRAME, Some(LAST_FRAME)), + ), ] { - let stack = stack_with(above_refs, left_refs); + let stack = stack_with(above_refs, left_refs, above_nbr, left_nbr); let (a, a1) = above_refs; let (l, l1) = left_refs; let ctx_of = |p: usize| match p { @@ -12382,7 +12413,17 @@ mod tests { _ => single_ref_p6_ctx(a, a1, l, l1), }; for (ref_frame, symbols) in cases { - let want: f64 = symbols.iter().map(|&(p, v)| b(&SR[ctx_of(p)][p], v)).sum(); + // Plus the `comp_mode = 0` symbol the writer codes ahead of + // the reference syntax on a `reference_select` frame. + let mut want: f64 = symbols.iter().map(|&(p, v)| b(&SR[ctx_of(p)][p], v)).sum(); + if super::reference_select() { + want += b( + &crate::cdf::COMP_MODE[crate::mvstack::reference_mode_ctx( + above_nbr, left_nbr, + )], + 0, + ); + } let got = super::single_ref_bits(ref_frame, &stack); assert!( (got - want).abs() < 1e-9, @@ -16075,9 +16116,12 @@ mod tests { // for `skip`/`is_inter` and for the whole compound reference tree, so // every inter block's cost -- and with it the skip and reference // decisions -- moved: 8535 -> 8311 bytes at q=150 and 33221 -> 33087 - // at q=60. + // at q=60. Re-taken on lane-ctx2: a single-reference candidate now + // pays the `comp_mode = 0` symbol the writer codes ahead of it, so + // single and compound are priced against the same syntax -- 8311 -> + // 8325 bytes at q=150 and 33087 -> 33014 at q=60. let pins: [(u8, usize, u64); 2] = - [(150, 8311, 0xfb6d_75a4_5d13_3833), (60, 33087, 0x4d11_a03c_bdc6_e2d5)]; + [(150, 8325, 0xf004_8258_bc60_ff05), (60, 33014, 0x5bc2_5dfd_bd13_18de)]; let coded: Vec<(u8, usize, u64)> = pins .iter() .map(|&(q, _, _)| { diff --git a/lanes/ctx2.report.md b/lanes/ctx2.report.md new file mode 100644 index 00000000..5b5d714e --- /dev/null +++ b/lanes/ctx2.report.md @@ -0,0 +1,109 @@ +# lane-ctx2 — the last two `pricer-context-zero` sites + +Class `pricer-context-zero`: the RD pricer in `crates/ec-av1/src/encode.rs` +priced symbols at CDF row 0 (or with the wrong tree) while the writer in +`crates/ec-av1/src/tile.rs` derives every context from neighbours. lane-pricer +and lane-ctx took single refs, `skip`, `is_inter` and the compound tree. This +lane took the two left: the `comp_mode` term (arm D, SHIPS) and the `tx_size` +depth context (arm B, REFUTED and reverted). + +## arm D — a single-reference candidate pays `comp_mode = 0` + +On a `reference_select` frame `tile::write_comp_mode` codes one `comp_mode` +symbol ahead of EVERY inter block's reference syntax. `compound_ref_bits` +already paid `comp_mode = 1`; `single_ref_bits` paid nothing at all, so the +compound side of every leaf carried the whole symbol as a handicap. +`single_ref_bits` now adds `comp_mode = 0` at the writer's own +`reference_mode_ctx`, and pays zero on a frame that carries no +`reference_select` bit -- exactly where `write_comp_mode` returns early. + +12-frame native gate, BD-rate vs libaom cpu-used 6 / rav1e speed 6: + +| row | control (09590c10) | arm D | +|---|---|---| +| film A | +21.0 / −4.8 | +21.0 / −4.8 | +| film B | +25.0 / −1.8 | **+24.9 / −1.9** | +| screen | +14.5 / −33.2 | +14.5 / −33.2 | +| bars 1080p | −1.8 / −17.5 | −2.0 / −17.6 | +| bars 2160p | +8.8 / −13.3 | +8.7 / −13.4 | + +Ships under the charter's THIRD keep rule (a correctness fix flat within ±0.2 +everywhere): film B improves 0.1 on both columns, both bars rows improve, film +A and screen are unmoved. The long-GOP gate is where it reads as a real win: + +| row | control | arm D | +|---|---|---| +| film A | +25.2 / −7.1 | **+25.2 / −7.2** | +| film B | +87.1 / +7.7 | **+86.7 / +7.3** | + +Control note: the control was NOT re-run at 09590c10 in this worktree (each row +is ~2 min of wall on top of six gate runs). It is corroborated instead -- film A +and screen come back byte-identical to the charter's control numbers on the arm +row, which a stale control could not do. + +## arm B — the `tx_size` depth context, REFUTED + +`tx_depth_bits` priced every block's `tx_depth` symbol at row 0, the row where +both neighbours are NARROWER than this block's transform and splitting reads +cheapest. Built as lane-ctx's recipe asked, for the key-frame path: the +key-frame tile search keeps the same two bands `Neighbours::record_tx` +publishes (each coded block's resolved transform side over its own 4x4 units, +frame-absolute so a superblock row needs no reset, a losing trial overwritten +by the winner's own publish exactly as `above_mode`/`left_mode` are), the +context rule itself moved into `tile::tx_size_ctx_of` so writer and pricer read +ONE function, and `code_square` takes the row its caller derives. + +| row | control (arm D) | arm B | +|---|---|---| +| film A | +21.0 / −4.8 | +21.1 / −4.6 | +| film B | +24.9 / −1.9 | +25.0 / −1.9 | +| screen | +14.5 / −33.2 | +14.5 / −33.2 | +| bars 1080p | −2.0 / −17.6 | −2.0 / −17.6 | +| bars 2160p | +8.7 / −13.4 | +8.7 / −13.4 | + +Every film column moves the wrong way for no win anywhere, so the keep rule is +not met and the commit is reverted (`77347164`). The likely reason it costs: +this is HALF the fix. An INTER frame's intra block takes its row from the +`TXFM_CONTEXT` bands instead (`tx_size_ctx_txfm`, where an inter neighbour +votes its own BLOCK size), which the encoder publishes nowhere, so with arm B +in, key frames and inter frames judge the same depth-versus-split margin +against different rows. What would unblock a second attempt: publish the +`TXFM_CONTEXT` band on the inter path too (the `MiGrid` already carries +`size`/`is_inter` for the inter-neighbour half) and price BOTH paths at once. + +## Unit tests + +* `encode::tests::the_single_ref_pricer_pays_the_writers_own_symbol_sequence` + — extended with the `comp_mode` term and a compound left neighbour (which is + what takes `reference_mode_ctx` off row 0). RED before arm D: + `ref 1 above (None, None) left (None, None): priced 2.0905436498760297 bits, + writer codes 2.5376944668507715`. Green after. +* `encode::tests::the_tx_depth_pricer_uses_the_writers_own_context_row` — arm + B's, green while arm B was in, gone with the revert. Recorded here because it + named the thing that limits the whole arm: libaom's default `TX_SIZE_CAT*` + tables give rows 0 and 1 IDENTICAL probabilities, so the only row that moves + a price at all is row 2 (both neighbours at least as wide). + +## Pins and suite + +* Stream pins re-taken on arm D: `[(150, 8325, 0xf004_8258_bc60_ff05), + (60, 33014, 0x5bc2_5dfd_bd13_18de)]` (was 8311 / 33087), green at the default + preset and at `EC_AV1_SPEED=6`. No other byte pin went red. +* Split suite, release lib, all RC=0: `--skip stream::` 338 passed / 0 failed / + 32 ignored; `stream:: --skip 10bit` 200 / 0 / 15; `10bit` 42 / 0 / 1. + `--ignored + encoder::tests::every_speed_preset_decodes_sample_exact_through_both_decoders` + passed. +* `timeout 900 cargo check --workspace --all-targets -j4`: 0 errors, 0 ec-av1 + warnings (the workspace's 22 are pre-existing, in ec-opus and ec-vorbis). + +## Deferred + +`deferred: the tx_size depth context — the key-frame half is built and +measured (arm B above) and LOSES on its own; what unblocks it is the inter +half, a TXFM_CONTEXT band published on the inter path so both frame types +price the depth symbol at the writer's row at the same time.` + +`deferred: the MV pricer — write_mv has PER-COMPONENT tables where the pricer +has one shared set (the long-standing static-MV approximation); untouched +here, as in lane-ctx.`