forge
Explore
Log in

Don't you dare go Hollow

Offer the OBMC candidate at the 16x16 and 8x8 inter leaves

Browse files at this commit

74fe5cfd808f09ad5a5e0e0ce57082a694cf4170Ahmet Kaan GümüşParent: c9bed9a
  • feat
    • offer the OBMC candidate at the 16x16 and 8x8 inter leaves
Show full message
feat(av1): offer the OBMC candidate at the 16x16 and 8x8 inter leaves

The motion_mode symbol is now priced off the frame's own starting tables
(tile::obmc_symbol_bits), like the coefficients since lane-av1price2, and
the native BD arm prints the motion_mode histogram the 640x384 gate
already did.
crates/ec-av1/src/encode.rsmodified+254 -10
@@ -2242,14 +2242,33 @@ impl Reach {
2242 }2242 }
2243 2243
2244 /// What one symbol costs against a CDF, in bits.2244 /// What one symbol costs against a CDF, in bits.
2245-/// What the `motion_mode`/`obmc` symbol costs (spec 5.11.24), off the default2245+/// The smallest LEAF an OBMC candidate is offered for, so the census can
2246-/// table rather than the tile's adapted one -- the same static-CDF estimate2246+/// attribute the tool's effect by footprint (`EC_AV1_OBMC_MIN`, default 8 --
2247-/// every other symbol in this search is priced with.2247+/// every leaf; 16 or 32 turn the smaller leaves off). Only read when
2248+/// `EC_AV1_OBMC` is on, and never by the 32x32 whole-block candidate.
2249+///
2250+/// MEASURED at native, `EC_AV1_OBMC=1` throughout (BD vs libaom / vs rav1e,
2251+/// against the +18.0/+0.7, +47.3/+19.2, +59.4/-10.0 of the tool OFF):
2252+/// see `search_inter_block`'s note for the whole table.
2253+fn obmc_min_side() -> usize {
2254+ static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
2255+ *N.get_or_init(|| {
2256+ std::env::var("EC_AV1_OBMC_MIN")
2257+ .ok()
2258+ .and_then(|v| v.parse().ok())
2259+ .unwrap_or(8)
2260+ })
2261+}
2262+
2263+/// What the `motion_mode`/`obmc` symbol costs (spec 5.11.24), off the tables
2264+/// this frame's writer really starts from (`tile::obmc_symbol_bits`) -- the
2265+/// same frame-CDF price the coefficients of a non-screen frame are estimated
2266+/// with, not the static default table.
2248 fn obmc_bits(write_w: usize, write_h: usize, obmc: bool) -> f64 {2267 fn obmc_bits(write_w: usize, write_h: usize, obmc: bool) -> f64 {
2249 let Some(row) = crate::decode::motion_mode_cdf_row(write_w, write_h) else {2268 let Some(row) = crate::decode::motion_mode_cdf_row(write_w, write_h) else {
2250 return 0.0;2269 return 0.0;
2251 };2270 };
2252- symbol_bits(&cdf::OBMC[row], usize::from(obmc))2271+ crate::tile::obmc_symbol_bits(row, obmc)
2253 }2272 }
2254 2273
2255 pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 {2274 pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 {
@@ -3391,6 +3410,86 @@ fn leaf_compound_stacks<'a>(
3391 .collect()3410 .collect()
3392 }3411 }
3393 3412
3413+/// The OBMC prediction of one square single-reference block: this block's own
3414+/// translation prediction (`mc::predict`) with every bordering above/left
3415+/// neighbour blended into the overlap strips by the DECODER's own plan and
3416+/// blend ([`crate::decode::obmc_plan`] / [`crate::decode::obmc_run`]), so what
3417+/// the search prices is what a decoder reconstructs -- never a second
3418+/// implementation of the blend. `None` when the block has no overlappable
3419+/// neighbour at all (the writer would then code no `motion_mode` symbol) or
3420+/// when the plan cannot be built (a neighbour naming a reference this encode
3421+/// does not hold).
3422+#[allow(clippy::too_many_arguments)]
3423+fn obmc_prediction(
3424+ grid: &MiGrid,
3425+ (mi_row, mi_col): (usize, usize),
3426+ (mi_rows, mi_cols): (usize, usize),
3427+ (x, y): (usize, usize),
3428+ side: usize,
3429+ mv: (i32, i32),
3430+ refs: &[Option<&Picture>; 8],
3431+ // This block's OWN reference planes, luma then both chroma, each as
3432+ // (samples, stride, true width, true height).
3433+ ref_planes: [(&[u16], usize, usize, usize); 3],
3434+ frame_width: usize,
3435+ fctx: &crate::decode::FrameCtx,
3436+) -> Option<[Vec<u8>; 3]> {
3437+ let n4 = side / 4;
3438+ if !crate::decode::has_overlappable_neighbour(grid, mi_row, mi_col, n4, n4, mi_cols, mi_rows) {
3439+ return None;
3440+ }
3441+ let refpix = crate::decode::RefPix::ready(*refs);
3442+ let plan = crate::decode::obmc_plan(
3443+ grid,
3444+ &[],
3445+ &[],
3446+ mi_row,
3447+ mi_col,
3448+ n4,
3449+ n4,
3450+ mi_rows,
3451+ mi_cols,
3452+ side,
3453+ side,
3454+ side,
3455+ side / 2,
3456+ x,
3457+ y,
3458+ x / 2,
3459+ y / 2,
3460+ &refpix,
3461+ Some(mc::InterpFilterKind::Regular),
3462+ frame_width,
3463+ )
3464+ .ok()?;
3465+ let mut pred = [
3466+ vec![0u16; side * side],
3467+ vec![0u16; side * side / 4],
3468+ vec![0u16; side * side / 4],
3469+ ];
3470+ for (i, r) in ref_planes.into_iter().enumerate() {
3471+ let luma_plane = i == 0;
3472+ let (px, py) = if luma_plane { (x, y) } else { (x / 2, y / 2) };
3473+ let s = if luma_plane { side } else { side / 2 };
3474+ mc::predict(
3475+ r.0,
3476+ r.1,
3477+ r.2,
3478+ r.3,
3479+ mv_to_q4(px, mv.1, luma_plane),
3480+ mv_to_q4(py, mv.0, luma_plane),
3481+ s,
3482+ s,
3483+ &mut pred[i],
3484+ fctx,
3485+ );
3486+ }
3487+ let [mut py_buf, mut pu_buf, mut pv_buf] = pred;
3488+ crate::decode::obmc_run(&plan, &refpix, &mut py_buf, &mut pu_buf, &mut pv_buf, fctx);
3489+ let u8s = |b: &[u16]| b.iter().map(|&v| v as u8).collect::<Vec<u8>>();
3490+ Some([u8s(&py_buf), u8s(&pu_buf), u8s(&pv_buf)])
3491+}
3492+
3394 fn code_square_inter(3493 fn code_square_inter(
3395 luma: &mut Plane,3494 luma: &mut Plane,
3396 chroma: &mut [Plane; 2],3495 chroma: &mut [Plane; 2],
@@ -3404,6 +3503,17 @@ fn code_square_inter(
3404 // leaf's own `bw4`/`bh4` (empty on a frame that does not code3503 // leaf's own `bw4`/`bh4` (empty on a frame that does not code
3405 // `reference_select`, and at the straddling call sites).3504 // `reference_select`, and at the straddling call sites).
3406 compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)],3505 compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)],
3506+ // lane-av1obmc2: the mi grid this leaf's neighbours were published into
3507+ // (`record_mi`, in the writer's own coding order) and this leaf's place in
3508+ // it -- what the OBMC candidate's eligibility walk and neighbour plan
3509+ // read, the same state the tile writer reads when it codes the leaf's
3510+ // `motion_mode` symbol.
3511+ grid: &MiGrid,
3512+ (mi_row, mi_col): (usize, usize),
3513+ (mi_rows, mi_cols): (usize, usize),
3514+ // Every picture an OBMC neighbour can name: a leaf only searches LAST,
3515+ // but the neighbours it blends may be GOLDEN/ALTREF blocks.
3516+ refs: &[Option<&Picture>; 8],
3407 fctx: &crate::decode::FrameCtx,3517 fctx: &crate::decode::FrameCtx,
3408 ) -> (BlockCoeffs, f64) {3518 ) -> (BlockCoeffs, f64) {
3409 let (intra_block, intra_cost) =3519 let (intra_block, intra_cost) =
@@ -3543,6 +3653,21 @@ fn code_square_inter(
3543 // prices for a whole block -- `motion::search` from `pred_mv`, rounded to3653 // prices for a whole block -- `motion::search` from `pred_mv`, rounded to
3544 // a vector the residual syntax can name -- at this leaf's own side.3654 // a vector the residual syntax can name -- at this leaf's own side.
3545 let mut best: (f64, Option<(Trial, Trial, Trial, bool, InterInfo)>) = (inter_cost, None);3655 let mut best: (f64, Option<(Trial, Trial, Trial, bool, InterInfo)>) = (inter_cost, None);
3656+ // The `NEARESTMV` candidate `inter_cost` prices, named once so the OBMC
3657+ // re-price below can hand it back as the winner's own mode info.
3658+ let nearest_info = InterInfo {
3659+ ref1: None,
3660+ mv1: (0, 0),
3661+ ref_frame: crate::mvstack::LAST_FRAME,
3662+ mode: InterMode::NearestMv,
3663+ mv,
3664+ ref_mv_idx: 0,
3665+ };
3666+ // What the current SINGLE-REFERENCE winner pays in mode/reference/mv
3667+ // syntax, apart from its residual: the OBMC candidate re-prices the same
3668+ // syntax against a different prediction, so both sides of that comparison
3669+ // carry it.
3670+ let mut single_syntax = intra_inter_bits(true) + single_ref_bits + mode_bits_inter;
3546 // This leaf's own `NEWMV` vector, kept whether or not it won the3671 // This leaf's own `NEWMV` vector, kept whether or not it won the
3547 // single-reference comparison: it seeds the compound half-new candidate3672 // single-reference comparison: it seeds the compound half-new candidate
3548 // below, exactly as `search_inter_block` seeds its own from the searches3673 // below, exactly as `search_inter_block` seeds its own from the searches
@@ -3619,6 +3744,11 @@ fn code_square_inter(
3619 luma_new.bits + u_new.bits + v_new.bits3744 luma_new.bits + u_new.bits + v_new.bits
3620 });3745 });
3621 if cost_new < best.0 {3746 if cost_new < best.0 {
3747+ single_syntax = intra_inter_bits(true)
3748+ + single_ref_bits
3749+ + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0)
3750+ + drl_bits
3751+ + mv_bits;
3622 best = (3752 best = (
3623 cost_new,3753 cost_new,
3624 Some((3754 Some((
@@ -3833,6 +3963,74 @@ fn code_square_inter(
3833 }3963 }
3834 }3964 }
3835 3965
3966+ // lane-av1obmc2: the OBMC candidate at a 16x16 / 8x8 LEAF (spec
3967+ // `motion_mode == OBMC_CAUSAL`), offered to this leaf's single-reference
3968+ // winner -- libaom's `motion_mode_allowed` refuses a compound block, and
3969+ // the writer codes no symbol for one. The prediction is the decoder's own
3970+ // plan and blend over the neighbours this leaf's `grid` already carries
3971+ // (published by `record_mi` in the writer's coding order), so the
3972+ // reconstruction priced here is the one a decoder rebuilds. lane-av1obmc
3973+ // offered this only at 32x32, which is not where libaom cashes its OBMC
3974+ // gain.
3975+ let mut obmc_won = false;
3976+ let single = match &best.1 {
3977+ None => Some(nearest_info),
3978+ Some((_, _, _, _, info)) if info.ref1.is_none() => Some(*info),
3979+ Some(_) => None,
3980+ };
3981+ if let Some(info) = single.filter(|i| {
3982+ crate::envflags::env_flag!("EC_AV1_OBMC")
3983+ && side >= obmc_min_side()
3984+ && i.ref_frame == crate::mvstack::LAST_FRAME
3985+ }) {
3986+ let planes = [
3987+ (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3),
3988+ (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3),
3989+ (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3),
3990+ ];
3991+ if let Some(pred) = obmc_prediction(
3992+ grid,
3993+ (mi_row, mi_col),
3994+ (mi_rows, mi_cols),
3995+ (x, y),
3996+ side,
3997+ info.mv,
3998+ refs,
3999+ planes,
4000+ reference.width,
4001+ fctx,
4002+ ) {
4003+ let luma_o = luma.code_from_prediction(
4004+ x, y, side, &pred[0], false, search.base_q_idx, search.deadzone, luma_set,
4005+ );
4006+ let u_o = chroma[0].code_from_prediction(
4007+ x / 2, y / 2, side / 2, &pred[1], false, search.base_q_idx, search.deadzone,
4008+ chroma_set,
4009+ );
4010+ let v_o = chroma[1].code_from_prediction(
4011+ x / 2, y / 2, side / 2, &pred[2], false, search.base_q_idx, search.deadzone,
4012+ chroma_set,
4013+ );
4014+ let skip_o = luma_o.levels.iter().all(|&l| l == 0)
4015+ && u_o.levels.iter().all(|&l| l == 0)
4016+ && v_o.levels.iter().all(|&l| l == 0);
4017+ let cost_o = luma_o.sse
4018+ + u_o.sse
4019+ + v_o.sse
4020+ + search.lambda
4021+ * (skip_bits(skip_o)
4022+ + single_syntax
4023+ + obmc_bits(side, side, true)
4024+ + if skip_o { 0.0 } else { luma_o.bits + u_o.bits + v_o.bits });
4025+ // The incumbent pays the SIMPLE_TRANSLATION symbol it was priced
4026+ // without, so both sides carry the motion_mode syntax.
4027+ if cost_o < best.0 + search.lambda * obmc_bits(side, side, false) {
4028+ obmc_won = true;
4029+ best = (cost_o, Some((luma_o, u_o, v_o, skip_o, info)));
4030+ }
4031+ }
4032+ }
4033+
3836 if let (cost, Some((luma_new, u_new, v_new, skip_new, info))) = best {4034 if let (cost, Some((luma_new, u_new, v_new, skip_new, info))) = best {
3837 if cost < intra_cost {4035 if cost < intra_cost {
3838 // A compound winner commits the flat trial it was priced from:4036 // A compound winner commits the flat trial it was priced from:
@@ -3855,7 +4053,10 @@ fn code_square_inter(
3855 // +1.4 against the +78.8/+95.1/+54.5 and +36.1/+56.4/+1.4 of the4053 // +1.4 against the +78.8/+95.1/+54.5 and +36.1/+56.4/+1.4 of the
3856 // same build without it. The static-CDF estimate was NOT what4054 // same build without it. The static-CDF estimate was NOT what
3857 // held the compound var-tx trial back. The flat trial stays.4055 // held the compound var-tx trial back. The flat trial stays.
3858- let (levels, tx_depth, dcost) = if info.ref1.is_some() {4056+ // An OBMC winner commits the flat trial too: `commit_inter_luma`
4057+ // re-predicts each var-tx unit by plain translation, which is not
4058+ // the prediction this candidate was priced from.
4059+ let (levels, tx_depth, dcost) = if info.ref1.is_some() || obmc_won {
3859 luma.commit(x, y, side, &luma_new);4060 luma.commit(x, y, side, &luma_new);
3860 (luma_new.levels.clone(), 0, 0.0)4061 (luma_new.levels.clone(), 0, 0.0)
3861 } else {4062 } else {
@@ -3881,7 +4082,7 @@ fn code_square_inter(
3881 palette_uv: None,4082 palette_uv: None,
3882 tx_depth,4083 tx_depth,
3883 inter: Some(info),4084 inter: Some(info),
3884- obmc: false,4085+ obmc: obmc_won,
3885 },4086 },
3886 cost + dcost,4087 cost + dcost,
3887 );4088 );
@@ -6721,6 +6922,12 @@ pub(crate) fn encode_inter_frame(
6721 clip_planes_to_tile(&mut luma, &mut chroma, rect);6922 clip_planes_to_tile(&mut luma, &mut chroma, rect);
6722 let mut grid = MiGrid::new(mi_cols, mi_rows);6923 let mut grid = MiGrid::new(mi_cols, mi_rows);
6723 grid.set_sign_bias(sign_bias);6924 grid.set_sign_bias(sign_bias);
6925+ // lane-av1obmc2: every picture an OBMC neighbour of a LEAF can name,
6926+ // built once per tile job (`obmc_prediction`).
6927+ let mut obmc_refs: [Option<&Picture>; 8] = [None; 8];
6928+ obmc_refs[crate::mvstack::LAST_FRAME as usize] = Some(reference);
6929+ obmc_refs[crate::mvstack::GOLDEN_FRAME as usize] = golden;
6930+ obmc_refs[crate::mvstack::ALTREF_FRAME as usize] = altref;
6724 grid.set_tile_bounds(6931 grid.set_tile_bounds(
6725 rect.mi_row0 as usize,6932 rect.mi_row0 as usize,
6726 rect.mi_col0 as usize,6933 rect.mi_col0 as usize,
@@ -6889,7 +7096,13 @@ pub(crate) fn encode_inter_frame(
6889 &search,7096 &search,
6890 &mode_bits_table,7097 &mode_bits_table,
6891 reference,7098 reference,
6892- &stack, &cstacks, fctx,7099+ &stack,
7100+ &cstacks,
7101+ &grid,
7102+ (mi_row, mi_col),
7103+ (mi_rows, mi_cols),
7104+ &obmc_refs,
7105+ fctx,
6893 );7106 );
6894 let leaf_cost = leaf_cost7107 let leaf_cost = leaf_cost
6895 + search.lambda * partition_bits(SUB, false);7108 + search.lambda * partition_bits(SUB, false);
@@ -6950,7 +7163,13 @@ pub(crate) fn encode_inter_frame(
6950 &search,7163 &search,
6951 &mode_bits_table,7164 &mode_bits_table,
6952 reference,7165 reference,
6953- &stack8, &cstacks8, fctx,7166+ &stack8,
7167+ &cstacks8,
7168+ &grid,
7169+ (mr, mc),
7170+ (mi_rows, mi_cols),
7171+ &obmc_refs,
7172+ fctx,
6954 );7173 );
6955 cost8 += cost;7174 cost8 += cost;
6956 record_mi(&mut grid, mr, mc, 2, leaf8.inter);7175 record_mi(&mut grid, mr, mc, 2, leaf8.inter);
@@ -7055,7 +7274,13 @@ pub(crate) fn encode_inter_frame(
7055 &search,7274 &search,
7056 &mode_bits_table,7275 &mode_bits_table,
7057 reference,7276 reference,
7058- &stack, &cstacks, fctx,7277+ &stack,
7278+ &cstacks,
7279+ &grid,
7280+ (mi_row, mi_col),
7281+ (mi_rows, mi_cols),
7282+ &obmc_refs,
7283+ fctx,
7059 );7284 );
7060 // One publication point for every coded leaf7285 // One publication point for every coded leaf
7061 // (`record_mi`): this site used to spell the vote7286 // (`record_mi`): this site used to spell the vote
@@ -7092,7 +7317,13 @@ pub(crate) fn encode_inter_frame(
7092 &search,7317 &search,
7093 &mode_bits_table,7318 &mode_bits_table,
7094 reference,7319 reference,
7095- &stack, &cstacks, fctx,7320+ &stack,
7321+ &cstacks,
7322+ &grid,
7323+ (mi_row, mi_col),
7324+ (mi_rows, mi_cols),
7325+ &obmc_refs,
7326+ fctx,
7096 );7327 );
7097 // Same publication point as the 16x167328 // Same publication point as the 16x16
7098 // straddling leaf above (`record_mi`).7329 // straddling leaf above (`record_mi`).
@@ -11440,6 +11671,19 @@ mod tests {
11440 // the real side, a screen clip none of them.11671 // the real side, a screen clip none of them.
11441 let (price_default, price_real) = crate::tile::take_pricing_hits();11672 let (price_default, price_real) = crate::tile::take_pricing_hits();
11442 eprintln!("{name}: pricer armings default={price_default} real={price_real}");11673 eprintln!("{name}: pricer armings default={price_default} real={price_real}");
11674+ // lane-av1obmc2: the same motion_mode census the 640x384 gate
11675+ // prints (class `gate-blind-to-feature`) -- how many blocks coded
11676+ // the symbol at each footprint and how many took OBMC. Without it
11677+ // the native table, which is the standing keep table, could not
11678+ // say whether the tool fired at all.
11679+ let mm = crate::tile::take_motion_mode_hits();
11680+ eprintln!(
11681+ "{name}: motion_mode 32x32 SIMPLE={} OBMC={} | 16x16 SIMPLE={} OBMC={} | \
11682+ 8x8 SIMPLE={} OBMC={} ({:.1}% OBMC of {} eligible)",
11683+ mm[0], mm[1], mm[2], mm[3], mm[4], mm[5],
11684+ 100.0 * (mm[1] + mm[3] + mm[5]) as f64 / mm.iter().sum::<usize>().max(1) as f64,
11685+ mm.iter().sum::<usize>(),
11686+ );
11443 let palette = crate::tile::take_palette_hits();11687 let palette = crate::tile::take_palette_hits();
11444 let palette_uv = crate::tile::take_palette_uv_hits();11688 let palette_uv = crate::tile::take_palette_uv_hits();
11445 let ibc = crate::tile::take_intrabc_hits();11689 let ibc = crate::tile::take_intrabc_hits();
crates/ec-av1/src/tile.rsmodified+13 -0
@@ -411,6 +411,19 @@ pub(crate) fn take_motion_mode_hits() -> [usize; 6] {
411 std::array::from_fn(|i| MOTION_MODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed))411 std::array::from_fn(|i| MOTION_MODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed))
412 }412 }
413 413
414+/// What the `motion_mode`/`obmc` symbol costs, in bits, against the tables
415+/// this frame's writer really starts from ([`arm_pricing_cdfs`]) -- the same
416+/// tables the coefficients are priced off since lane-av1price2, rather than
417+/// the static defaults the symbol used to be estimated with. Falls back to
418+/// the default table whenever the search is armed with the defaults (a key
419+/// frame, or a screen frame the gate turned off).
420+pub(crate) fn obmc_symbol_bits(row: usize, obmc: bool) -> f64 {
421+ PRICING_BASE.with_borrow(|slot| match slot.as_deref() {
422+ Some(s) => crate::encode::symbol_bits(&s.0.obmc[row], usize::from(obmc)),
423+ None => crate::encode::symbol_bits(&crate::cdf::OBMC[row], usize::from(obmc)),
424+ })
425+}
426+
414 /// `read_motion_mode`'s write side (spec 5.11.24), called on a427 /// `read_motion_mode`'s write side (spec 5.11.24), called on a
415 /// single-reference inter block right after its MV syntax and before the428 /// single-reference inter block right after its MV syntax and before the
416 /// (never switchable here) interpolation filter -- libaom's own sequential429 /// (never switchable here) interpolation filter -- libaom's own sequential