Merge branch 'lane-ctx2' (2efeb8f6): single-reference candidates pay comp_mode=0 at the writer's context; the key-only tx_size context arm measured worse and is reverted; pins re-taken 8325 / 33014
Browse files at this commit2 changed files +169 -16
- crates/ec-av1/src/encode.rs +60 -16
- lanes/ctx2.report.md +109 -0
crates/ec-av1/src/encode.rsmodified+60 -16
| @@ -4112,15 +4112,32 @@ pub(crate) fn intra_inter_bits_at(stack: &crate::mvstack::MvStack, inter: bool) | ||
| 4112 | 4112 | symbol_bits(&cdf::INTRA_INTER[ctx], usize::from(inter)) |
| 4113 | 4113 | } |
| 4114 | 4114 | |
| 4115 | +/// The `comp_mode = 0` symbol tile.rs' `write_comp_mode` codes ahead of a | |
| 4116 | +/// SINGLE-reference block's reference syntax, at `reference_mode_ctx`'s own | |
| 4117 | +/// context -- zero on a frame that carries no `reference_select` bit, where | |
| 4118 | +/// the writer codes no symbol at all. | |
| 4119 | +fn comp_mode_single_bits(stack: &crate::mvstack::MvStack) -> f64 { | |
| 4120 | + if !reference_select() { | |
| 4121 | + return 0.0; | |
| 4122 | + } | |
| 4123 | + symbol_bits( | |
| 4124 | + &cdf::COMP_MODE[crate::mvstack::reference_mode_ctx(stack.above_nbr, stack.left_nbr)], | |
| 4125 | + 0, | |
| 4126 | + ) | |
| 4127 | +} | |
| 4128 | + | |
| 4115 | 4129 | /// What tile.rs' `write_single_ref` pays for `ref_frame`, symbol for symbol, |
| 4116 | -/// at the fixed context this RD pricer uses for every neighbour-derived | |
| 4117 | -/// symbol (the writer contexts each symbol off its above/left neighbours' | |
| 4118 | -/// references; the search does not have them). LAST is p1=0,p3=0,p4=0, | |
| 4130 | +/// each at the context that symbol's own `single_ref_p*_ctx` derives from the | |
| 4131 | +/// block's above/left neighbours -- plus the `comp_mode = 0` symbol | |
| 4132 | +/// `write_comp_mode` codes ahead of it on a `reference_select` frame. | |
| 4133 | +/// LAST is p1=0,p3=0,p4=0, | |
| 4119 | 4134 | /// LAST2 p1=0,p3=0,p4=1, LAST3 p1=0,p3=1,p5=0, GOLDEN p1=0,p3=1,p5=1, |
| 4120 | 4135 | /// BWDREF p1=1,p2=0,p6=0, ALTREF2 p1=1,p2=0,p6=1 and ALTREF p1=1,p2=1. |
| 4121 | 4136 | /// Before lane-pricer every BACKWARD reference was priced as LAST -- three |
| 4122 | 4137 | /// symbols off the wrong CDFs -- which is what made the search believe an |
| 4123 | -/// ALTREF block cost the same syntax as a LAST one. | |
| 4138 | +/// ALTREF block cost the same syntax as a LAST one. The `comp_mode` term is | |
| 4139 | +/// lane-ctx2's: a compound candidate paid `comp_mode = 1` while a single one | |
| 4140 | +/// paid nothing at all, so compound was overcharged by the whole symbol. | |
| 4124 | 4141 | pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) -> f64 { |
| 4125 | 4142 | use crate::mvstack::{ |
| 4126 | 4143 | 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) -> | ||
| 4129 | 4146 | }; |
| 4130 | 4147 | let (a, a1) = stack.above_refs; |
| 4131 | 4148 | let (l, l1) = stack.left_refs; |
| 4149 | + let comp_mode = comp_mode_single_bits(stack); | |
| 4132 | 4150 | let backward = ref_frame >= BWDREF_FRAME; |
| 4133 | 4151 | let p1 = symbol_bits( |
| 4134 | 4152 | &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) -> | ||
| 4140 | 4158 | &cdf::SINGLE_REF[single_ref_p2_ctx(a, a1, l, l1)][1], |
| 4141 | 4159 | usize::from(is_altref), |
| 4142 | 4160 | ); |
| 4143 | - return p1 | |
| 4161 | + return comp_mode | |
| 4162 | + + p1 | |
| 4144 | 4163 | + p2 |
| 4145 | 4164 | + if is_altref { |
| 4146 | 4165 | 0.0 |
| @@ -4152,7 +4171,7 @@ pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) -> | ||
| 4152 | 4171 | }; |
| 4153 | 4172 | } |
| 4154 | 4173 | let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME; |
| 4155 | - p1 + symbol_bits( | |
| 4174 | + comp_mode + p1 + symbol_bits( | |
| 4156 | 4175 | &cdf::SINGLE_REF[single_ref_p3_ctx(a, a1, l, l1)][2], |
| 4157 | 4176 | usize::from(far), |
| 4158 | 4177 | ) + if far { |
| @@ -12349,11 +12368,16 @@ mod tests { | ||
| 12349 | 12368 | LAST_FRAME, MvStack, single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, |
| 12350 | 12369 | single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx, |
| 12351 | 12370 | }; |
| 12352 | - let stack_with = |above_refs, left_refs| MvStack { | |
| 12371 | + let stack_with = |above_refs, left_refs, above_nbr, left_nbr| MvStack { | |
| 12353 | 12372 | above_refs, |
| 12354 | 12373 | left_refs, |
| 12374 | + above_nbr, | |
| 12375 | + left_nbr, | |
| 12355 | 12376 | ..bare_stack() |
| 12356 | 12377 | }; |
| 12378 | + let nbr = |ref0, ref1| { | |
| 12379 | + Some(crate::mvstack::NeighbourRef { is_inter: true, ref0, ref1, uni: false }) | |
| 12380 | + }; | |
| 12357 | 12381 | // (reference, the symbols `write_single_ref` emits as (p index, value)) |
| 12358 | 12382 | let cases: [(i8, &[(usize, usize)]); 7] = [ |
| 12359 | 12383 | (LAST_FRAME, &[(0, 0), (2, 0), (3, 0)]), |
| @@ -12364,13 +12388,20 @@ mod tests { | ||
| 12364 | 12388 | (ALTREF2_FRAME, &[(0, 1), (1, 0), (5, 1)]), |
| 12365 | 12389 | (ALTREF_FRAME, &[(0, 1), (1, 1)]), |
| 12366 | 12390 | ]; |
| 12367 | - // Both neighbours intra, and the mixed LAST-above/ALTREF-left pair | |
| 12368 | - // that puts every p* context on a different row. | |
| 12369 | - for (above_refs, left_refs) in [ | |
| 12370 | - ((None, None), (None, None)), | |
| 12371 | - ((Some(LAST_FRAME), None), (Some(ALTREF_FRAME), Some(LAST_FRAME))), | |
| 12391 | + // Both neighbours intra, then the mixed LAST-above/ALTREF-left pair | |
| 12392 | + // that puts every p* context on a different row -- the second with a | |
| 12393 | + // COMPOUND left neighbour, which is what takes `reference_mode_ctx` | |
| 12394 | + // off row 0. | |
| 12395 | + for (above_refs, left_refs, above_nbr, left_nbr) in [ | |
| 12396 | + ((None, None), (None, None), None, None), | |
| 12397 | + ( | |
| 12398 | + (Some(LAST_FRAME), None), | |
| 12399 | + (Some(ALTREF_FRAME), Some(LAST_FRAME)), | |
| 12400 | + nbr(LAST_FRAME, None), | |
| 12401 | + nbr(ALTREF_FRAME, Some(LAST_FRAME)), | |
| 12402 | + ), | |
| 12372 | 12403 | ] { |
| 12373 | - let stack = stack_with(above_refs, left_refs); | |
| 12404 | + let stack = stack_with(above_refs, left_refs, above_nbr, left_nbr); | |
| 12374 | 12405 | let (a, a1) = above_refs; |
| 12375 | 12406 | let (l, l1) = left_refs; |
| 12376 | 12407 | let ctx_of = |p: usize| match p { |
| @@ -12382,7 +12413,17 @@ mod tests { | ||
| 12382 | 12413 | _ => single_ref_p6_ctx(a, a1, l, l1), |
| 12383 | 12414 | }; |
| 12384 | 12415 | for (ref_frame, symbols) in cases { |
| 12385 | - let want: f64 = symbols.iter().map(|&(p, v)| b(&SR[ctx_of(p)][p], v)).sum(); | |
| 12416 | + // Plus the `comp_mode = 0` symbol the writer codes ahead of | |
| 12417 | + // the reference syntax on a `reference_select` frame. | |
| 12418 | + let mut want: f64 = symbols.iter().map(|&(p, v)| b(&SR[ctx_of(p)][p], v)).sum(); | |
| 12419 | + if super::reference_select() { | |
| 12420 | + want += b( | |
| 12421 | + &crate::cdf::COMP_MODE[crate::mvstack::reference_mode_ctx( | |
| 12422 | + above_nbr, left_nbr, | |
| 12423 | + )], | |
| 12424 | + 0, | |
| 12425 | + ); | |
| 12426 | + } | |
| 12386 | 12427 | let got = super::single_ref_bits(ref_frame, &stack); |
| 12387 | 12428 | assert!( |
| 12388 | 12429 | (got - want).abs() < 1e-9, |
| @@ -16075,9 +16116,12 @@ mod tests { | ||
| 16075 | 16116 | // for `skip`/`is_inter` and for the whole compound reference tree, so |
| 16076 | 16117 | // every inter block's cost -- and with it the skip and reference |
| 16077 | 16118 | // decisions -- moved: 8535 -> 8311 bytes at q=150 and 33221 -> 33087 |
| 16078 | - // at q=60. | |
| 16119 | + // at q=60. Re-taken on lane-ctx2: a single-reference candidate now | |
| 16120 | + // pays the `comp_mode = 0` symbol the writer codes ahead of it, so | |
| 16121 | + // single and compound are priced against the same syntax -- 8311 -> | |
| 16122 | + // 8325 bytes at q=150 and 33087 -> 33014 at q=60. | |
| 16079 | 16123 | let pins: [(u8, usize, u64); 2] = |
| 16080 | - [(150, 8311, 0xfb6d_75a4_5d13_3833), (60, 33087, 0x4d11_a03c_bdc6_e2d5)]; | |
| 16124 | + [(150, 8325, 0xf004_8258_bc60_ff05), (60, 33014, 0x5bc2_5dfd_bd13_18de)]; | |
| 16081 | 16125 | let coded: Vec<(u8, usize, u64)> = pins |
| 16082 | 16126 | .iter() |
| 16083 | 16127 | .map(|&(q, _, _)| { |
lanes/ctx2.report.mdadded+109 -0
| @@ -0,0 +1,109 @@ | ||
| 1 | +# lane-ctx2 — the last two `pricer-context-zero` sites | |
| 2 | + | |
| 3 | +Class `pricer-context-zero`: the RD pricer in `crates/ec-av1/src/encode.rs` | |
| 4 | +priced symbols at CDF row 0 (or with the wrong tree) while the writer in | |
| 5 | +`crates/ec-av1/src/tile.rs` derives every context from neighbours. lane-pricer | |
| 6 | +and lane-ctx took single refs, `skip`, `is_inter` and the compound tree. This | |
| 7 | +lane took the two left: the `comp_mode` term (arm D, SHIPS) and the `tx_size` | |
| 8 | +depth context (arm B, REFUTED and reverted). | |
| 9 | + | |
| 10 | +## arm D — a single-reference candidate pays `comp_mode = 0` | |
| 11 | + | |
| 12 | +On a `reference_select` frame `tile::write_comp_mode` codes one `comp_mode` | |
| 13 | +symbol ahead of EVERY inter block's reference syntax. `compound_ref_bits` | |
| 14 | +already paid `comp_mode = 1`; `single_ref_bits` paid nothing at all, so the | |
| 15 | +compound side of every leaf carried the whole symbol as a handicap. | |
| 16 | +`single_ref_bits` now adds `comp_mode = 0` at the writer's own | |
| 17 | +`reference_mode_ctx`, and pays zero on a frame that carries no | |
| 18 | +`reference_select` bit -- exactly where `write_comp_mode` returns early. | |
| 19 | + | |
| 20 | +12-frame native gate, BD-rate vs libaom cpu-used 6 / rav1e speed 6: | |
| 21 | + | |
| 22 | +| row | control (09590c10) | arm D | | |
| 23 | +|---|---|---| | |
| 24 | +| film A | +21.0 / −4.8 | +21.0 / −4.8 | | |
| 25 | +| film B | +25.0 / −1.8 | **+24.9 / −1.9** | | |
| 26 | +| screen | +14.5 / −33.2 | +14.5 / −33.2 | | |
| 27 | +| bars 1080p | −1.8 / −17.5 | −2.0 / −17.6 | | |
| 28 | +| bars 2160p | +8.8 / −13.3 | +8.7 / −13.4 | | |
| 29 | + | |
| 30 | +Ships under the charter's THIRD keep rule (a correctness fix flat within ±0.2 | |
| 31 | +everywhere): film B improves 0.1 on both columns, both bars rows improve, film | |
| 32 | +A and screen are unmoved. The long-GOP gate is where it reads as a real win: | |
| 33 | + | |
| 34 | +| row | control | arm D | | |
| 35 | +|---|---|---| | |
| 36 | +| film A | +25.2 / −7.1 | **+25.2 / −7.2** | | |
| 37 | +| film B | +87.1 / +7.7 | **+86.7 / +7.3** | | |
| 38 | + | |
| 39 | +Control note: the control was NOT re-run at 09590c10 in this worktree (each row | |
| 40 | +is ~2 min of wall on top of six gate runs). It is corroborated instead -- film A | |
| 41 | +and screen come back byte-identical to the charter's control numbers on the arm | |
| 42 | +row, which a stale control could not do. | |
| 43 | + | |
| 44 | +## arm B — the `tx_size` depth context, REFUTED | |
| 45 | + | |
| 46 | +`tx_depth_bits` priced every block's `tx_depth` symbol at row 0, the row where | |
| 47 | +both neighbours are NARROWER than this block's transform and splitting reads | |
| 48 | +cheapest. Built as lane-ctx's recipe asked, for the key-frame path: the | |
| 49 | +key-frame tile search keeps the same two bands `Neighbours::record_tx` | |
| 50 | +publishes (each coded block's resolved transform side over its own 4x4 units, | |
| 51 | +frame-absolute so a superblock row needs no reset, a losing trial overwritten | |
| 52 | +by the winner's own publish exactly as `above_mode`/`left_mode` are), the | |
| 53 | +context rule itself moved into `tile::tx_size_ctx_of` so writer and pricer read | |
| 54 | +ONE function, and `code_square` takes the row its caller derives. | |
| 55 | + | |
| 56 | +| row | control (arm D) | arm B | | |
| 57 | +|---|---|---| | |
| 58 | +| film A | +21.0 / −4.8 | +21.1 / −4.6 | | |
| 59 | +| film B | +24.9 / −1.9 | +25.0 / −1.9 | | |
| 60 | +| screen | +14.5 / −33.2 | +14.5 / −33.2 | | |
| 61 | +| bars 1080p | −2.0 / −17.6 | −2.0 / −17.6 | | |
| 62 | +| bars 2160p | +8.7 / −13.4 | +8.7 / −13.4 | | |
| 63 | + | |
| 64 | +Every film column moves the wrong way for no win anywhere, so the keep rule is | |
| 65 | +not met and the commit is reverted (`77347164`). The likely reason it costs: | |
| 66 | +this is HALF the fix. An INTER frame's intra block takes its row from the | |
| 67 | +`TXFM_CONTEXT` bands instead (`tx_size_ctx_txfm`, where an inter neighbour | |
| 68 | +votes its own BLOCK size), which the encoder publishes nowhere, so with arm B | |
| 69 | +in, key frames and inter frames judge the same depth-versus-split margin | |
| 70 | +against different rows. What would unblock a second attempt: publish the | |
| 71 | +`TXFM_CONTEXT` band on the inter path too (the `MiGrid` already carries | |
| 72 | +`size`/`is_inter` for the inter-neighbour half) and price BOTH paths at once. | |
| 73 | + | |
| 74 | +## Unit tests | |
| 75 | + | |
| 76 | +* `encode::tests::the_single_ref_pricer_pays_the_writers_own_symbol_sequence` | |
| 77 | + — extended with the `comp_mode` term and a compound left neighbour (which is | |
| 78 | + what takes `reference_mode_ctx` off row 0). RED before arm D: | |
| 79 | + `ref 1 above (None, None) left (None, None): priced 2.0905436498760297 bits, | |
| 80 | + writer codes 2.5376944668507715`. Green after. | |
| 81 | +* `encode::tests::the_tx_depth_pricer_uses_the_writers_own_context_row` — arm | |
| 82 | + B's, green while arm B was in, gone with the revert. Recorded here because it | |
| 83 | + named the thing that limits the whole arm: libaom's default `TX_SIZE_CAT*` | |
| 84 | + tables give rows 0 and 1 IDENTICAL probabilities, so the only row that moves | |
| 85 | + a price at all is row 2 (both neighbours at least as wide). | |
| 86 | + | |
| 87 | +## Pins and suite | |
| 88 | + | |
| 89 | +* Stream pins re-taken on arm D: `[(150, 8325, 0xf004_8258_bc60_ff05), | |
| 90 | + (60, 33014, 0x5bc2_5dfd_bd13_18de)]` (was 8311 / 33087), green at the default | |
| 91 | + preset and at `EC_AV1_SPEED=6`. No other byte pin went red. | |
| 92 | +* Split suite, release lib, all RC=0: `--skip stream::` 338 passed / 0 failed / | |
| 93 | + 32 ignored; `stream:: --skip 10bit` 200 / 0 / 15; `10bit` 42 / 0 / 1. | |
| 94 | + `--ignored | |
| 95 | + encoder::tests::every_speed_preset_decodes_sample_exact_through_both_decoders` | |
| 96 | + passed. | |
| 97 | +* `timeout 900 cargo check --workspace --all-targets -j4`: 0 errors, 0 ec-av1 | |
| 98 | + warnings (the workspace's 22 are pre-existing, in ec-opus and ec-vorbis). | |
| 99 | + | |
| 100 | +## Deferred | |
| 101 | + | |
| 102 | +`deferred: the tx_size depth context — the key-frame half is built and | |
| 103 | +measured (arm B above) and LOSES on its own; what unblocks it is the inter | |
| 104 | +half, a TXFM_CONTEXT band published on the inter path so both frame types | |
| 105 | +price the depth symbol at the writer's row at the same time.` | |
| 106 | + | |
| 107 | +`deferred: the MV pricer — write_mv has PER-COMPONENT tables where the pricer | |
| 108 | +has one shared set (the long-standing static-MV approximation); untouched | |
| 109 | +here, as in lane-ctx.` |