forge
Explore
Log in

Don't you dare go Hollow

wip(av1): warp candidate snapshot at the agent cap (may not compile)

Browse files at this commit

fbf67c0cffd5987da7fecf523bf5a878c64079e3Ahmet Kaan GümüşParent: cb81370
crates/ec-av1/src/decode.rsmodified+9 -3
@@ -552,7 +552,7 @@ pub(crate) fn warp_plane_suppress_hits() -> usize {
552552 /// block dimensions decide it -- never the square `side`/`chroma_side`, which
553553 /// for a rectangular luma block (8x16, 16x8) or a PARTITION_HORZ/VERT strip is
554554 /// bigger than the real block in one axis.
555-fn warp_plane_allowed(w: usize, h: usize) -> bool {
555+pub(crate) fn warp_plane_allowed(w: usize, h: usize) -> bool {
556556 if w >= 8 && h >= 8 {
557557 return true;
558558 }
@@ -24220,7 +24220,7 @@ fn has_top_right(mi_row: usize, mi_col: usize, bs: usize, fctx: &crate::decode::
2422024220 /// own `bw`/`bh` (fed to [`crate::warp::record_sample`]'s offset formula)
2422124221 /// come from the *neighbour* cell's own coded size, not this block's.
2422224222 #[allow(clippy::too_many_arguments)]
24223-fn find_samples(
24223+pub(crate) fn find_samples(
2422424224 grid: &MiGrid,
2422524225 mi_row: usize,
2422624226 mi_col: usize,
@@ -24378,7 +24378,7 @@ fn find_samples(
2437824378 /// `motion_mode_allowed` needs to pick the 3-symbol `motion_mode_cdf` over
2437924379 /// the 2-symbol `obmc_cdf` (spec 5.11.24).
2438024380 #[allow(clippy::too_many_arguments)]
24381-fn num_proj_ref(
24381+pub(crate) fn num_proj_ref(
2438224382 grid: &MiGrid,
2438324383 mi_row: usize,
2438424384 mi_col: usize,
@@ -32053,6 +32053,12 @@ fn decode_inter_block8(
3205332053 mc::scale_factor(ref_width, frame_width) != mc::REF_NO_SCALE;
3205432054 let warp_eligible =
3205532055 allow_warped_motion && !ref_is_scaled && !force_integer_mv && proj_ok;
32056+ if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") {
32057+ eprintln!(
32058+ "EC_MM8 mi_row={mi_row} mi_col={mi_col} proj={} warp_elig={} awm={}",
32059+ proj_ok as u8, warp_eligible as u8, allow_warped_motion as u8
32060+ );
32061+ }
3205632062 if allow_warped_motion && proj_ok && ref_is_scaled {
3205732063 hit!(SCALED_WARP_SUPPRESSED_HITS);
3205832064 }
crates/ec-av1/src/encode.rsmodified+295 -140
@@ -1028,7 +1028,10 @@ pub(crate) fn key_frame_headers_colour(
10281028 additional_frame_id_length: 0,
10291029 enable_interintra_compound: false,
10301030 enable_masked_compound: false,
1031- enable_warped_motion: false,
1031+ // lane-av1obmc2: the sequence bit `allow_warped_motion` is gated on
1032+ // (spec 5.5.2). Set only under the warp knob, so the DEFAULT sequence
1033+ // header is byte-identical to the streams before this lane.
1034+ enable_warped_motion: crate::envflags::env_flag!("EC_AV1_WARP"),
10321035 enable_dual_filter: false,
10331036 enable_jnt_comp: false,
10341037 enable_ref_frame_mvs: false,
@@ -1166,9 +1169,14 @@ pub fn inter_frame_headers_slots(
11661169 // to the OBMC knob rather than always on: with every block choosing
11671170 // SIMPLE_TRANSLATION the symbol is pure cost (+0.07% / +0.02% on the
11681171 // gate clip), so the DEFAULT path writes no motion_mode syntax at all
1169- // and stays byte-identical to the streams before this lane. A future
1170- // warp knob joins this disjunction.
1171- is_motion_mode_switchable: crate::envflags::env_flag!("EC_AV1_OBMC"),
1172+ // and stays byte-identical to the streams before this lane.
1173+ // lane-av1obmc2: the warp knob joins that disjunction, and turns on
1174+ // `allow_warped_motion` besides -- which is what makes an eligible
1175+ // block read the 3-symbol `motion_mode_cdf` alphabet instead of the
1176+ // 2-symbol `obmc_cdf` one (libaom `motion_mode_allowed`).
1177+ is_motion_mode_switchable: crate::envflags::env_flag!("EC_AV1_OBMC")
1178+ || crate::envflags::env_flag!("EC_AV1_WARP"),
1179+ allow_warped_motion: crate::envflags::env_flag!("EC_AV1_WARP"),
11721180 use_ref_frame_mvs: false,
11731181 // Forces `get_tx_set` (spec 5.11.48) to the two-symbol
11741182 // `TX_SET_INTER_3` for every inter transform below 32x32 -- the only
@@ -2281,11 +2289,113 @@ fn obmc_min_side() -> usize {
22812289 /// this frame's writer really starts from (`tile::obmc_symbol_bits`) -- the
22822290 /// same frame-CDF price the coefficients of a non-screen frame are estimated
22832291 /// with, not the static default table.
2284-fn obmc_bits(write_w: usize, write_h: usize, obmc: bool) -> f64 {
2292+/// The same price for whichever alphabet the writer will really use for this
2293+/// block: `warp_alphabet` is `motion_mode_allowed`'s 3-symbol choice
2294+/// (`allow_warped_motion` and at least one warp sample), otherwise the
2295+/// 2-symbol `obmc` one. Both sides of every motion-mode comparison in the
2296+/// search pay it, so the alphabet cancels out of a comparison and only its
2297+/// per-value spread decides.
2298+fn motion_mode_bits(write_w: usize, write_h: usize, motion: u8, warp_alphabet: bool) -> f64 {
22852299 let Some(row) = crate::decode::motion_mode_cdf_row(write_w, write_h) else {
22862300 return 0.0;
22872301 };
2288- crate::tile::obmc_symbol_bits(row, obmc)
2302+ match warp_alphabet {
2303+ true => crate::tile::motion_mode_symbol_bits(row, motion),
2304+ false => crate::tile::obmc_symbol_bits(row, motion == 1),
2305+ }
2306+}
2307+
2308+/// The WARPED_CAUSAL prediction of one square single-reference block: the
2309+/// decoder's own warp-sample walk (`decode::find_samples` +
2310+/// `warp::select_samples`), least-squares model (`warp::find_projection`) and
2311+/// affine predictor (`warp::warp_affine`), driven off the encoder's own mi
2312+/// grid -- never a second implementation of any of the three. `None` when the
2313+/// block would code no motion_mode symbol at all, when it has no warp sample
2314+/// (the writer then codes the 2-symbol alphabet, which has no WARPED value),
2315+/// or when the projection is not filter-representable (the decoder would then
2316+/// predict translationally, so the mode is pure cost).
2317+#[allow(clippy::too_many_arguments)]
2318+fn warp_prediction(
2319+ grid: &MiGrid,
2320+ (mi_row, mi_col): (usize, usize),
2321+ (mi_rows, mi_cols): (usize, usize),
2322+ (x, y): (usize, usize),
2323+ side: usize,
2324+ mv: (i32, i32),
2325+ ref_frame: i8,
2326+ ref_planes: [(&[u16], usize, usize, usize); 3],
2327+ fctx: &crate::decode::FrameCtx,
2328+) -> Option<[Vec<u8>; 3]> {
2329+ let n4 = side / 4;
2330+ if !crate::decode::has_overlappable_neighbour(grid, mi_row, mi_col, n4, n4, mi_cols, mi_rows) {
2331+ return None;
2332+ }
2333+ let mut samples = crate::decode::find_samples(
2334+ grid, mi_row, mi_col, n4, n4, mi_cols, mi_rows, ref_frame, fctx,
2335+ );
2336+ if samples.is_empty() {
2337+ return None;
2338+ }
2339+ if samples.len() > 1 {
2340+ crate::warp::select_samples(mv, &mut samples, side as i32, side as i32);
2341+ }
2342+ let params = crate::warp::find_projection(
2343+ &samples,
2344+ side as i32,
2345+ side as i32,
2346+ mv.1,
2347+ mv.0,
2348+ mi_row as i32,
2349+ mi_col as i32,
2350+ )?;
2351+ let mut pred = [
2352+ vec![0u16; side * side],
2353+ vec![0u16; side * side / 4],
2354+ vec![0u16; side * side / 4],
2355+ ];
2356+ // The translational prediction first, exactly as the decoder builds it,
2357+ // then the affine one over the planes whose OWN block is at least 8x8
2358+ // (libaom `av1_init_warp_params` bails below that, so an 8x8 luma block's
2359+ // 4x4 chroma stays translational).
2360+ for (i, r) in ref_planes.into_iter().enumerate() {
2361+ let luma_plane = i == 0;
2362+ let (px, py) = if luma_plane { (x, y) } else { (x / 2, y / 2) };
2363+ let s = if luma_plane { side } else { side / 2 };
2364+ mc::predict(
2365+ r.0,
2366+ r.1,
2367+ r.2,
2368+ r.3,
2369+ mv_to_q4(px, mv.1, luma_plane),
2370+ mv_to_q4(py, mv.0, luma_plane),
2371+ s,
2372+ s,
2373+ &mut pred[i],
2374+ fctx,
2375+ );
2376+ if crate::decode::warp_plane_allowed(s, s) {
2377+ let (sub, cside) = (usize::from(!luma_plane) as i32, s as i32);
2378+ crate::warp::warp_affine(
2379+ &params,
2380+ r.0,
2381+ r.2 as i32,
2382+ r.3 as i32,
2383+ r.1 as i32,
2384+ &mut pred[i],
2385+ px as i32,
2386+ py as i32,
2387+ cside,
2388+ cside,
2389+ cside,
2390+ sub,
2391+ sub,
2392+ fctx,
2393+ );
2394+ }
2395+ }
2396+ let [py_buf, pu_buf, pv_buf] = pred;
2397+ let u8s = |b: &[u16]| b.iter().map(|&v| v as u8).collect::<Vec<u8>>();
2398+ Some([u8s(&py_buf), u8s(&pu_buf), u8s(&pv_buf)])
22892399 }
22902400
22912401 pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 {
@@ -3989,15 +4099,14 @@ fn code_square_inter(
39894099 // reconstruction priced here is the one a decoder rebuilds. lane-av1obmc
39904100 // offered this only at 32x32, which is not where libaom cashes its OBMC
39914101 // gain.
3992- let mut obmc_won = false;
4102+ let mut motion_won = 0u8;
39934103 let single = match &best.1 {
39944104 None => Some(nearest_info),
39954105 Some((_, _, _, _, info)) if info.ref1.is_none() => Some(*info),
39964106 Some(_) => None,
39974107 };
39984108 if let Some(info) = single.filter(|i| {
3999- crate::envflags::env_flag!("EC_AV1_OBMC")
4000- && side >= obmc_min_side()
4109+ (crate::envflags::env_flag!("EC_AV1_OBMC") || crate::envflags::env_flag!("EC_AV1_WARP"))
40014110 && i.ref_frame == crate::mvstack::LAST_FRAME
40024111 }) {
40034112 let planes = [
@@ -4005,18 +4114,57 @@ fn code_square_inter(
40054114 (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3),
40064115 (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3),
40074116 ];
4008- if let Some(pred) = obmc_prediction(
4009- grid,
4010- (mi_row, mi_col),
4011- (mi_rows, mi_cols),
4012- (x, y),
4013- side,
4014- info.mv,
4015- refs,
4016- planes,
4017- reference.width,
4018- fctx,
4019- ) {
4117+ // Which alphabet this block's writer will code the symbol against
4118+ // (`tile::write_motion_mode`, libaom `motion_mode_allowed`): the
4119+ // 3-symbol one only under `allow_warped_motion` and at least one warp
4120+ // sample. Mirrored here so the candidates are priced against the
4121+ // table the tile really narrows.
4122+ let warp_alphabet = crate::envflags::env_flag!("EC_AV1_WARP")
4123+ && crate::decode::num_proj_ref(
4124+ grid,
4125+ mi_row,
4126+ mi_col,
4127+ side / 4,
4128+ side / 4,
4129+ mi_cols,
4130+ mi_rows,
4131+ info.ref_frame,
4132+ fctx,
4133+ ) >= 1;
4134+ let mm = |m: u8| motion_mode_bits(side, side, m, warp_alphabet);
4135+ let mut candidates: Vec<(u8, [Vec<u8>; 3])> = Vec::new();
4136+ if crate::envflags::env_flag!("EC_AV1_OBMC") && side >= obmc_min_side() {
4137+ if let Some(pred) = obmc_prediction(
4138+ grid,
4139+ (mi_row, mi_col),
4140+ (mi_rows, mi_cols),
4141+ (x, y),
4142+ side,
4143+ info.mv,
4144+ refs,
4145+ planes,
4146+ reference.width,
4147+ fctx,
4148+ ) {
4149+ candidates.push((1, pred));
4150+ }
4151+ }
4152+ if warp_alphabet {
4153+ if let Some(pred) = warp_prediction(
4154+ grid,
4155+ (mi_row, mi_col),
4156+ (mi_rows, mi_cols),
4157+ (x, y),
4158+ side,
4159+ info.mv,
4160+ info.ref_frame,
4161+ planes,
4162+ fctx,
4163+ ) {
4164+ candidates.push((2, pred));
4165+ }
4166+ }
4167+ for (motion, pred) in candidates {
40204168 let luma_o = luma.code_from_prediction(
40214169 x, y, side, &pred[0], false, search.base_q_idx, search.deadzone, luma_set,
40224170 );
@@ -4037,12 +4185,12 @@ fn code_square_inter(
40374185 + search.lambda
40384186 * (skip_bits(skip_o)
40394187 + single_syntax
4040- + obmc_bits(side, side, true)
4188+ + mm(motion)
40414189 + if skip_o { 0.0 } else { luma_o.bits + u_o.bits + v_o.bits });
4042- // The incumbent pays the SIMPLE_TRANSLATION symbol it was priced
4043- // without, so both sides carry the motion_mode syntax.
4044- if cost_o < best.0 + search.lambda * obmc_bits(side, side, false) {
4045- obmc_won = true;
4190+ // The incumbent pays the motion_mode symbol IT would code, so
4191+ // both sides of the comparison carry that syntax.
4192+ if cost_o < best.0 + search.lambda * mm(motion_won) {
4193+ motion_won = motion;
40464194 best = (cost_o, Some((luma_o, u_o, v_o, skip_o, info)));
40474195 }
40484196 }
@@ -4073,7 +4221,7 @@ fn code_square_inter(
40734221 // An OBMC winner commits the flat trial too: `commit_inter_luma`
40744222 // re-predicts each var-tx unit by plain translation, which is not
40754223 // the prediction this candidate was priced from.
4076- let (levels, tx_depth, dcost) = if info.ref1.is_some() || obmc_won {
4224+ let (levels, tx_depth, dcost) = if info.ref1.is_some() || motion_won != 0 {
40774225 luma.commit(x, y, side, &luma_new);
40784226 (luma_new.levels.clone(), 0, 0.0)
40794227 } else {
@@ -4099,7 +4247,7 @@ fn code_square_inter(
40994247 palette_uv: None,
41004248 tx_depth,
41014249 inter: Some(info),
4102- obmc: obmc_won,
4250+ motion_mode: motion_won,
41034251 },
41044252 cost + dcost,
41054253 );
@@ -4131,7 +4279,7 @@ fn code_square_inter(
41314279 palette: None,
41324280 palette_uv: None,
41334281 tx_depth,
4134- obmc: false,
4282+ motion_mode: 0,
41354283 inter: Some(InterInfo {
41364284 ref1: None,
41374285 mv1: (0, 0),
@@ -6526,117 +6674,117 @@ fn search_inter_block(
65266674 // that is where libaom cashes most of its OBMC gain), and the symbol/
65276675 // residual price here is the static-CDF estimate.
65286676 let mut best = best;
6529- let mut obmc_won = false;
6530- if let Some(info) = best.inter.filter(|i| i.ref1.is_none()) {
6531- if crate::envflags::env_flag!("EC_AV1_OBMC")
6532- && crate::decode::has_overlappable_neighbour(grid, mi_row, mi_col, 8, 8, mi_cols, mi_rows)
6533- {
6534- let mut refs: [Option<&Picture>; 8] = [None; 8];
6535- refs[LAST_FRAME as usize] = Some(reference);
6536- for &(r, p, _) in extra {
6537- refs[r as usize] = Some(p);
6538- }
6539- let refpix = crate::decode::RefPix::ready(refs);
6540- let plan = crate::decode::obmc_plan(
6677+ let mut motion_won = 0u8;
6678+ if let Some(info) = best.inter.filter(|i| {
6679+ i.ref1.is_none()
6680+ && (crate::envflags::env_flag!("EC_AV1_OBMC")
6681+ || crate::envflags::env_flag!("EC_AV1_WARP"))
6682+ }) {
6683+ let mut refs: [Option<&Picture>; 8] = [None; 8];
6684+ refs[LAST_FRAME as usize] = Some(reference);
6685+ for &(r, p, _) in extra {
6686+ refs[r as usize] = Some(p);
6687+ }
6688+ // This block's OWN reference planes: the single-reference winner may
6689+ // be one of the extra references, not LAST.
6690+ let planes: [(&[u16], usize, usize, usize); 3] =
6691+ match extra.iter().find(|(r, _, _)| *r == info.ref_frame) {
6692+ Some(&(_, g, _)) => [
6693+ (g.y.as_slice(), g.width, luma.true_width, luma.true_height),
6694+ (g.u.as_slice(), g.width / 2, chroma[0].true_width, chroma[0].true_height),
6695+ (g.v.as_slice(), g.width / 2, chroma[1].true_width, chroma[1].true_height),
6696+ ],
6697+ None => [
6698+ (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3),
6699+ (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3),
6700+ (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3),
6701+ ],
6702+ };
6703+ // The alphabet `tile::write_motion_mode` will code this block's
6704+ // symbol against, mirrored term for term (libaom
6705+ // `motion_mode_allowed`).
6706+ let warp_alphabet = crate::envflags::env_flag!("EC_AV1_WARP")
6707+ && crate::decode::num_proj_ref(
65416708 grid,
6542- &[],
6543- &[],
65446709 mi_row,
65456710 mi_col,
65466711 8,
65476712 8,
6548- mi_rows,
65496713 mi_cols,
6714+ mi_rows,
6715+ info.ref_frame,
6716+ fctx,
6717+ ) >= 1;
6718+ let mm = |m: u8| motion_mode_bits(BLOCK, BLOCK, m, warp_alphabet);
6719+ let mut candidates: Vec<(u8, [Vec<u8>; 3])> = Vec::new();
6720+ if crate::envflags::env_flag!("EC_AV1_OBMC") {
6721+ if let Some(pred) = obmc_prediction(
6722+ grid,
6723+ (mi_row, mi_col),
6724+ (mi_rows, mi_cols),
6725+ (x, y),
65506726 BLOCK,
6551- BLOCK,
6552- BLOCK,
6553- BLOCK / 2,
6554- x,
6555- y,
6556- x / 2,
6557- y / 2,
6558- &refpix,
6559- Some(mc::InterpFilterKind::Regular),
6727+ info.mv,
6728+ &refs,
6729+ planes,
65606730 reference.width,
6731+ fctx,
6732+ ) {
6733+ candidates.push((1, pred));
6734+ }
6735+ }
6736+ if warp_alphabet {
6737+ if let Some(pred) = warp_prediction(
6738+ grid,
6739+ (mi_row, mi_col),
6740+ (mi_rows, mi_cols),
6741+ (x, y),
6742+ BLOCK,
6743+ info.mv,
6744+ info.ref_frame,
6745+ planes,
6746+ fctx,
6747+ ) {
6748+ candidates.push((2, pred));
6749+ }
6750+ }
6751+ for (motion, pred) in candidates {
6752+ let luma_trial = luma.code_from_prediction(
6753+ x, y, BLOCK, &pred[0], false, search.base_q_idx, search.deadzone,
6754+ TxbSet::Luma32Inter,
65616755 );
6562- if let Ok(plan) = plan {
6563- let (ref_luma, ref_u, ref_v) = match extra
6564- .iter()
6565- .find(|(r, _, _)| *r == info.ref_frame)
6566- {
6567- Some(&(_, g, _)) => (
6568- (&g.y, g.width, luma.true_width, luma.true_height),
6569- (&g.u, g.width / 2, chroma[0].true_width, chroma[0].true_height),
6570- (&g.v, g.width / 2, chroma[1].true_width, chroma[1].true_height),
6571- ),
6572- None => (ref_luma, ref_u, ref_v),
6756+ let u = chroma[0].code_from_prediction(
6757+ x / 2, y / 2, BLOCK / 2, &pred[1], false, search.base_q_idx, search.deadzone,
6758+ chroma_set,
6759+ );
6760+ let v = chroma[1].code_from_prediction(
6761+ x / 2, y / 2, BLOCK / 2, &pred[2], false, search.base_q_idx, search.deadzone,
6762+ chroma_set,
6763+ );
6764+ let skip = luma_trial.levels.iter().all(|&l| l == 0)
6765+ && u.levels.iter().all(|&l| l == 0)
6766+ && v.levels.iter().all(|&l| l == 0);
6767+ let cost = luma_trial.sse
6768+ + u.sse
6769+ + v.sse
6770+ + search.lambda
6771+ * (skip_bits(skip)
6772+ + intra_inter_bits(true)
6773+ + best.mode_bits
6774+ + mm(motion)
6775+ + if skip { 0.0 } else { luma_trial.bits + u.bits + v.bits });
6776+ if cost < best.cost + search.lambda * mm(motion_won) {
6777+ motion_won = motion;
6778+ best = Candidate {
6779+ cost,
6780+ luma: luma_trial,
6781+ u,
6782+ v,
6783+ mode: DC_PRED,
6784+ skip,
6785+ mode_bits: best.mode_bits,
6786+ inter: best.inter,
65736787 };
6574- let mv = info.mv;
6575- let mut pred = [
6576- vec![0u16; BLOCK * BLOCK],
6577- vec![0u16; BLOCK * BLOCK / 4],
6578- vec![0u16; BLOCK * BLOCK / 4],
6579- ];
6580- for (i, (r, luma_plane)) in
6581- [(ref_luma, true), (ref_u, false), (ref_v, false)].into_iter().enumerate()
6582- {
6583- let (px, py) = if luma_plane { (x, y) } else { (x / 2, y / 2) };
6584- let side = if luma_plane { BLOCK } else { BLOCK / 2 };
6585- mc::predict(
6586- r.0,
6587- r.1,
6588- r.2,
6589- r.3,
6590- mv_to_q4(px, mv.1, luma_plane),
6591- mv_to_q4(py, mv.0, luma_plane),
6592- side,
6593- side,
6594- &mut pred[i],
6595- fctx,
6596- );
6597- }
6598- let [mut py_buf, mut pu_buf, mut pv_buf] = pred;
6599- crate::decode::obmc_run(
6600- &plan, &refpix, &mut py_buf, &mut pu_buf, &mut pv_buf, fctx,
6601- );
6602- let u8s = |b: &[u16]| b.iter().map(|&v| v as u8).collect::<Vec<u8>>();
6603- let luma_trial = luma.code_from_prediction(
6604- x, y, BLOCK, &u8s(&py_buf), false, search.base_q_idx, search.deadzone,
6605- TxbSet::Luma32Inter,
6606- );
6607- let u = chroma[0].code_from_prediction(
6608- x / 2, y / 2, BLOCK / 2, &u8s(&pu_buf), false, search.base_q_idx,
6609- search.deadzone, chroma_set,
6610- );
6611- let v = chroma[1].code_from_prediction(
6612- x / 2, y / 2, BLOCK / 2, &u8s(&pv_buf), false, search.base_q_idx,
6613- search.deadzone, chroma_set,
6614- );
6615- let skip = luma_trial.levels.iter().all(|&l| l == 0)
6616- && u.levels.iter().all(|&l| l == 0)
6617- && v.levels.iter().all(|&l| l == 0);
6618- let cost = luma_trial.sse
6619- + u.sse
6620- + v.sse
6621- + search.lambda
6622- * (skip_bits(skip)
6623- + intra_inter_bits(true)
6624- + best.mode_bits
6625- + obmc_bits(BLOCK, BLOCK, true)
6626- + if skip { 0.0 } else { luma_trial.bits + u.bits + v.bits });
6627- if cost < best.cost + search.lambda * obmc_bits(BLOCK, BLOCK, false) {
6628- obmc_won = true;
6629- best = Candidate {
6630- cost,
6631- luma: luma_trial,
6632- u,
6633- v,
6634- mode: DC_PRED,
6635- skip,
6636- mode_bits: best.mode_bits,
6637- inter: best.inter,
6638- };
6639- }
66406788 }
66416789 }
66426790 }
@@ -6661,7 +6809,7 @@ fn search_inter_block(
66616809 uv_mode: DC_PRED,
66626810 skip: best.skip,
66636811 inter: best.inter,
6664- obmc: obmc_won,
6812+ motion_mode: motion_won,
66656813 eight: None,
66666814 dv: None,
66676815 palette: None,
@@ -7411,6 +7559,7 @@ pub(crate) fn encode_inter_frame(
74117559 let start_cdfs = CdfSnapshot(cdfs);
74127560 let reference_select_bit = header.reference_select;
74137561 let switchable_motion_mode = header.is_motion_mode_switchable;
7562+ let warped_motion = header.allow_warped_motion;
74147563 let order_hint_bits = seq.order_hint_bits;
74157564 let lr_horz = crate::restoration::count_units(header.frame_width, 64) as u32;
74167565 let lr_vert = crate::restoration::count_units(header.frame_height, 64) as u32;
@@ -7433,6 +7582,7 @@ pub(crate) fn encode_inter_frame(
74337582 // Thread-locals: every tile job arms its own worker.
74347583 crate::tile::arm_reference_select(reference_select_bit);
74357584 crate::tile::arm_motion_mode(switchable_motion_mode);
7585+ crate::tile::arm_warped_motion(warped_motion);
74367586 crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints);
74377587 crate::tile::arm_screen(screen);
74387588 let mut cdfs = start_cdfs.0.clone();
@@ -8363,7 +8513,7 @@ mod tests {
83638513 mv: (0, 0),
83648514 ref_mv_idx: 0,
83658515 }),
8366- obmc: false,
8516+ motion_mode: 0,
83678517 };
83688518 let skipped_block = BlockCoeffs {
83698519 skip: true,
@@ -8387,6 +8537,7 @@ mod tests {
83878537 // `is_motion_mode_switchable`, so the writer must be armed with it
83888538 // too or the decoder reads a `motion_mode` symbol nobody wrote.
83898539 crate::tile::arm_motion_mode(inter_header.is_motion_mode_switchable);
8540+ crate::tile::arm_warped_motion(inter_header.allow_warped_motion);
83908541 let tile =
83918542 crate::tile::sb_coeff_inter_frame_tile(inter_header.mi_cols, inter_header.mi_rows, 100, &blocks)
83928543 .unwrap();
@@ -11475,10 +11626,12 @@ mod tests {
1147511626 // `gate-blind-to-feature`).
1147611627 let mm = crate::tile::take_motion_mode_hits();
1147711628 eprintln!(
11478- "{name}: motion_mode 32x32 SIMPLE={} OBMC={} | 16x16 SIMPLE={} OBMC={} | \
11479- 8x8 SIMPLE={} OBMC={} ({:.1}% OBMC of {} eligible)",
11480- mm[0], mm[1], mm[2], mm[3], mm[4], mm[5],
11481- 100.0 * (mm[1] + mm[3] + mm[5]) as f64 / mm.iter().sum::<usize>().max(1) as f64,
11629+ "{name}: motion_mode 32x32 SIMPLE={} OBMC={} WARP={} | 16x16 SIMPLE={} OBMC={} \
11630+ WARP={} | 8x8 SIMPLE={} OBMC={} WARP={} ({:.1}% OBMC / {:.1}% WARP of {} \
11631+ eligible)",
11632+ mm[0], mm[1], mm[2], mm[3], mm[4], mm[5], mm[6], mm[7], mm[8],
11633+ 100.0 * (mm[1] + mm[4] + mm[7]) as f64 / mm.iter().sum::<usize>().max(1) as f64,
11634+ 100.0 * (mm[2] + mm[5] + mm[8]) as f64 / mm.iter().sum::<usize>().max(1) as f64,
1148211635 mm.iter().sum::<usize>(),
1148311636 );
1148411637 let (aom, aom_wall) =
@@ -11695,10 +11848,12 @@ mod tests {
1169511848 // say whether the tool fired at all.
1169611849 let mm = crate::tile::take_motion_mode_hits();
1169711850 eprintln!(
11698- "{name}: motion_mode 32x32 SIMPLE={} OBMC={} | 16x16 SIMPLE={} OBMC={} | \
11699- 8x8 SIMPLE={} OBMC={} ({:.1}% OBMC of {} eligible)",
11700- mm[0], mm[1], mm[2], mm[3], mm[4], mm[5],
11701- 100.0 * (mm[1] + mm[3] + mm[5]) as f64 / mm.iter().sum::<usize>().max(1) as f64,
11851+ "{name}: motion_mode 32x32 SIMPLE={} OBMC={} WARP={} | 16x16 SIMPLE={} OBMC={} \
11852+ WARP={} | 8x8 SIMPLE={} OBMC={} WARP={} ({:.1}% OBMC / {:.1}% WARP of {} \
11853+ eligible)",
11854+ mm[0], mm[1], mm[2], mm[3], mm[4], mm[5], mm[6], mm[7], mm[8],
11855+ 100.0 * (mm[1] + mm[4] + mm[7]) as f64 / mm.iter().sum::<usize>().max(1) as f64,
11856+ 100.0 * (mm[2] + mm[5] + mm[8]) as f64 / mm.iter().sum::<usize>().max(1) as f64,
1170211857 mm.iter().sum::<usize>(),
1170311858 );
1170411859 let palette = crate::tile::take_palette_hits();
crates/ec-av1/src/tile.rsmodified+88 -18
@@ -390,6 +390,28 @@ thread_local! {
390390 static MOTION_MODE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
391391 }
392392
393+thread_local! {
394+ /// This frame header's own `allow_warped_motion` bit (spec 5.9.2): with
395+ /// it set, every block libaom's `motion_mode_allowed` accepts AND that
396+ /// has at least one warp sample (`num_proj_ref >= 1`) reads the 3-symbol
397+ /// `motion_mode_cdf` alphabet instead of the 2-symbol `obmc_cdf` one.
398+ /// lane-av1obmc2.
399+ static WARPED_MOTION: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
400+ /// The frame geometry the warp-sample walk needs (`num_proj_ref` ->
401+ /// `has_top_right`), which is only ever this writer's own superblock
402+ /// size. Every stream this crate writes uses a 64 superblock, which is
403+ /// `FrameCtx::new`'s default.
404+ /// corner-cut: ceiling is a 128-superblock WRITER -- it must arm the real
405+ /// frame context here (or take one as a parameter) or the warp-sample
406+ /// walk reads the wrong `has_top_right` geometry.
407+ static WRITER_FCTX: crate::decode::FrameCtx = crate::decode::FrameCtx::new();
408+}
409+
410+/// Arms [`WARPED_MOTION`] for the next tile written on this thread.
411+pub(crate) fn arm_warped_motion(on: bool) {
412+ WARPED_MOTION.with(|c| c.set(on));
413+}
414+
393415 /// Arms the frame's `is_motion_mode_switchable` bit for the next tile written
394416 /// on this thread; cleared by [`CdefIdxGuard`] when that writer returns.
395417 pub(crate) fn arm_motion_mode(on: bool) {
@@ -401,16 +423,27 @@ pub(crate) fn arm_motion_mode(on: bool) {
401423 /// -- the histogram a gate prints so that "OBMC is on" is a measurement, not
402424 /// a claim (class `gate-blind-to-feature`). Index: `0 + 2 * bucket` for
403425 /// SIMPLE, `1 + 2 * bucket` for OBMC, bucket 0/1/2 = 32x32/16x16/8x8.
404-static MOTION_MODE_HITS: [std::sync::atomic::AtomicUsize; 6] =
405- [const { std::sync::atomic::AtomicUsize::new(0) }; 6];
426+static MOTION_MODE_HITS: [std::sync::atomic::AtomicUsize; 9] =
427+ [const { std::sync::atomic::AtomicUsize::new(0) }; 9];
406428
407429 /// Reads [`MOTION_MODE_HITS`] and zeroes it, so a gate can attribute the
408430 /// counts to its own encode.
409431 #[allow(dead_code)] // read only from the `#[cfg(test)]` gates
410-pub(crate) fn take_motion_mode_hits() -> [usize; 6] {
432+pub(crate) fn take_motion_mode_hits() -> [usize; 9] {
411433 std::array::from_fn(|i| MOTION_MODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed))
412434 }
413435
436+/// What the 3-symbol `motion_mode` symbol costs, in bits, against the same
437+/// frame tables [`obmc_symbol_bits`] prices the 2-symbol alphabet off --
438+/// which alphabet a block reads is `motion_mode_allowed`'s own choice
439+/// ([`write_motion_mode`]).
440+pub(crate) fn motion_mode_symbol_bits(row: usize, motion: u8) -> f64 {
441+ PRICING_BASE.with_borrow(|slot| match slot.as_deref() {
442+ Some(s) => crate::encode::symbol_bits(&s.0.motion_mode[row], usize::from(motion)),
443+ None => crate::encode::symbol_bits(&crate::cdf::MOTION_MODE[row], usize::from(motion)),
444+ })
445+}
446+
414447 /// What the `motion_mode`/`obmc` symbol costs, in bits, against the tables
415448 /// this frame's writer really starts from ([`arm_pricing_cdfs`]) -- the same
416449 /// tables the coefficients are priced off since lane-av1price2, rather than
@@ -445,7 +478,12 @@ fn write_motion_mode(
445478 (bw4, bh4): (usize, usize),
446479 (write_w, write_h): (usize, usize),
447480 (mi_cols, mi_rows): (usize, usize),
448- obmc: bool,
481+ // This block's own reference, for the warp-sample walk that picks the
482+ // alphabet (`num_proj_ref`).
483+ ref_frame: i8,
484+ // 0 SIMPLE_TRANSLATION, 1 OBMC_CAUSAL, 2 WARPED_CAUSAL -- what the
485+ // encoder's search committed for this block.
486+ motion: u8,
449487 ) -> Result<()> {
450488 let eligible = MOTION_MODE.with(std::cell::Cell::get)
451489 && write_w.min(write_h) >= 8
@@ -453,9 +491,15 @@ fn write_motion_mode(
453491 grid, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows,
454492 );
455493 if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") {
494+ let proj = WRITER_FCTX.with(|fctx| {
495+ crate::decode::num_proj_ref(
496+ grid, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows, ref_frame, fctx,
497+ )
498+ });
456499 eprintln!(
457- "EC_MMW mi_row={mi_row} mi_col={mi_col} w={write_w} h={write_h} elig={}",
458- eligible as u8
500+ "EC_MMW mi_row={mi_row} mi_col={mi_col} w={write_w} h={write_h} elig={} proj={proj} warp_armed={} motion={motion}",
501+ eligible as u8,
502+ WARPED_MOTION.with(std::cell::Cell::get) as u8,
459503 );
460504 }
461505 if !eligible {
@@ -463,7 +507,7 @@ fn write_motion_mode(
463507 // codes as SIMPLE_TRANSLATION: its reconstruction is not the
464508 // decoder's (class encoder-grid-drift), which is exactly what the
465509 // `EC_COMP_MISMATCH` rung exists to catch.
466- if obmc && crate::envflags::env_flag!("EC_COMP_MISMATCH") {
510+ if motion != 0 && crate::envflags::env_flag!("EC_COMP_MISMATCH") {
467511 eprintln!(
468512 "EC_COMP_MISMATCH motion_mode enc=OBMC wrote=SIMPLE mi=({mi_row},{mi_col}) wh=({write_w}x{write_h})"
469513 );
@@ -476,13 +520,37 @@ fn write_motion_mode(
476520 format!("a {write_w}x{write_h} block has no motion_mode CDF row"),
477521 )
478522 })?;
479- enc.symbol(usize::from(obmc), &mut cdfs.obmc[row]);
523+ // libaom `motion_mode_allowed`: the 3-symbol `motion_mode_cdf` alphabet
524+ // exactly when `allow_warped_motion` is on and the block has at least one
525+ // warp sample. The other two vetoes libaom applies there cannot fire for
526+ // a stream this encoder writes -- it codes no scaled reference and no
527+ // inter frame with `force_integer_mv` -- and the decoder checks all three
528+ // (`decode.rs`'s `warp_eligible`), so the two sides agree term for term.
529+ let warp_alphabet = WARPED_MOTION.with(std::cell::Cell::get)
530+ && WRITER_FCTX.with(|fctx| {
531+ crate::decode::num_proj_ref(
532+ grid, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows, ref_frame, fctx,
533+ )
534+ }) >= 1;
535+ if warp_alphabet {
536+ enc.symbol(usize::from(motion), &mut cdfs.motion_mode[row]);
537+ } else {
538+ // A warp winner whose block the writer resolves to the 2-symbol
539+ // alphabet would reconstruct as SIMPLE at the decoder: the same
540+ // encoder-grid-drift rung the OBMC case above prints.
541+ if motion == 2 && crate::envflags::env_flag!("EC_COMP_MISMATCH") {
542+ eprintln!(
543+ "EC_COMP_MISMATCH motion_mode enc=WARP wrote=SIMPLE mi=({mi_row},{mi_col}) wh=({write_w}x{write_h})"
544+ );
545+ }
546+ enc.symbol(usize::from(motion == 1), &mut cdfs.obmc[row]);
547+ }
480548 let bucket = match write_w.min(write_h) {
481549 32.. => 0,
482550 16 => 1,
483551 _ => 2,
484552 };
485- MOTION_MODE_HITS[usize::from(obmc) + 2 * bucket]
553+ MOTION_MODE_HITS[usize::from(motion.min(2)) + 3 * bucket]
486554 .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
487555 Ok(())
488556 }
@@ -1347,12 +1415,11 @@ pub struct BlockCoeffs {
13471415 /// block. Only [`sb_coeff_inter_frame_tile`] codes `is_inter`; the key
13481416 /// frame writers never read this field.
13491417 pub inter: Option<InterInfo>,
1350- /// Whether this (single-reference inter) block's prediction was built
1351- /// with OBMC -- spec `motion_mode == OBMC_CAUSAL`, written by
1352- /// [`write_motion_mode`]. `false` (the [`Default`]) is
1353- /// `SIMPLE_TRANSLATION`, the only motion mode this writer coded before
1354- /// lane-av1obmc.
1355- pub obmc: bool,
1418+ /// This (single-reference inter) block's spec `motion_mode`, written by
1419+ /// [`write_motion_mode`]: 0 `SIMPLE_TRANSLATION` (the [`Default`], and
1420+ /// the only mode this writer coded before lane-av1obmc), 1 `OBMC_CAUSAL`,
1421+ /// 2 `WARPED_CAUSAL` (lane-av1obmc2).
1422+ pub motion_mode: u8,
13561423 /// The 8x8 leaves a straddling 16x16 block is split into (lane-av1-rect),
13571424 /// each with its own 8x8 luma transform and 4x4 chroma transforms, in
13581425 /// raster order among the leaves that are inside the true frame. `Some`
@@ -5560,7 +5627,8 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
55605627 (8, 8),
55615628 (BLOCK, BLOCK),
55625629 (mi_cols as usize, mi_rows as usize),
5563- block.obmc,
5630+ info.ref_frame,
5631+ block.motion_mode,
55645632 )?;
55655633 mode_for_tx = 0;
55665634 }
@@ -5794,7 +5862,8 @@ fn write_inter_frame_leaf(
57945862 (SUB_MI as usize, SUB_MI as usize),
57955863 (SUB, SUB),
57965864 (mi_cols as usize, mi_rows as usize),
5797- block.obmc,
5865+ info.ref_frame,
5866+ block.motion_mode,
57985867 )?;
57995868 mode_for_tx = 0;
58005869 }
@@ -6034,7 +6103,8 @@ fn write_inter_frame_leaf8(
60346103 (2, 2),
60356104 (8, 8),
60366105 (mi_cols as usize, mi_rows as usize),
6037- block.obmc,
6106+ info.ref_frame,
6107+ block.motion_mode,
60386108 )?;
60396109 mode_for_tx = 0;
60406110 }