forge
Explore
Log in

Don't you dare go Hollow

An inter LEAF searches its second reference, so NEAREST_NEW and NEW_NEW reach the leaves

Browse files at this commit

e83e29a304dec735edd5d3dfe2cdb1fb60d54ee3Ahmet Kaan GümüşParent: bbbf1cd
  • feat
    • an inter LEAF searches its second reference, so NEAREST_NEW and NEW_NEW reach the leaves
Show full message
feat(av1): an inter LEAF searches its second reference, so NEAREST_NEW and NEW_NEW reach the leaves

A 16x16/8x8 leaf only ever ran one motion search (against LAST), so its
compound candidates could name a stack vector for the second half and nothing
else. It now runs a search against the second reference too, seeded from and
rounded to compound stack entry 0 (what assign_compound_mv predicts from at
ref_mv_idx 0), behind the same early-out shape the whole-block extra-reference
NEWMV uses, swept to 0.8.

BD vs libaom +83.0/+99.9/+54.5 -> +79.9/+94.6/+54.5, vs rav1e
+39.7/+60.0/+1.1 -> +37.3/+55.7/+1.1; extra motion searches +23% at 2160p.
crates/ec-av1/src/encode.rsmodified+119 -11
@@ -190,6 +190,42 @@ fn extra_new_skip_margin() -> f64 {
190190 }
191191 }
192192
193+/// The margin on a LEAF's SECOND-reference motion search, the same early-out
194+/// shape [`EXTRA_NEW_SKIP_MARGIN`] gives a whole block's extra-reference
195+/// `NEWMV`: the search is skipped when that reference's own
196+/// `NEAREST_NEARESTMV` vector already prices this many times better than the
197+/// leaf's `LAST` search found. `0.0` never skips.
198+///
199+/// Swept on the BD gate over 0.15/0.35/0.6/0.8/1.2 and 0.0, reading BD vs
200+/// libaom (1080p/2160p/screen) against the extra `motion::search` calls at
201+/// 2160p (38195 with no second-reference search at all):
202+/// 0.15 +81.7/+97.4/+54.5 at 41286, 0.35 +81.4/+96.7 at 43093,
203+/// 0.6 +80.6/+95.5 at 44843, 0.8 +79.9/+94.6 at 47008, 1.2 +80.2/+94.2 at
204+/// 77882, never-skip +79.7/+94.1 at 81801. 0.8 takes the whole gain the
205+/// unskipped search buys (-3.1/-5.3 off the +83.0/+99.9 base) for a quarter
206+/// of its extra searches; past it the calls double for another -0.3/-0.5.
207+/// Screen capture is BYTE-IDENTICAL at every margin -- its leaf compound is
208+/// all `NEAREST_NEARESTMV` and no searched second vector ever wins there --
209+/// so the margin is pure wall on that content (+10% searches at 0.8).
210+const LEAF_SECOND_NEW_MARGIN: f64 = 0.8;
211+
212+/// [`LEAF_SECOND_NEW_MARGIN`], swept by `EC_AV1_LEAF_SECOND_MARGIN`; the
213+/// search itself is switched off by `EC_AV1_LEAF_SECOND_NEWMV=0`.
214+fn leaf_second_new_margin() -> f64 {
215+ match std::env::var("EC_AV1_LEAF_SECOND_MARGIN").ok() {
216+ Some(v) => v.parse().unwrap_or(LEAF_SECOND_NEW_MARGIN),
217+ None => LEAF_SECOND_NEW_MARGIN,
218+ }
219+}
220+
221+/// Whether a leaf searches its SECOND reference at all (lane-av1comp4).
222+fn leaf_second_new_mv() -> bool {
223+ match std::env::var("EC_AV1_LEAF_SECOND_NEWMV").ok() {
224+ Some(v) => v != "0",
225+ None => true,
226+ }
227+}
228+
193229 /// Whether a 16x16 or 8x8 inter LEAF is offered the compound candidates the
194230 /// whole 32x32 block is (lane-av1comp3). The writer already codes a compound
195231 /// block at any leaf size (`tile::write_inter_frame_leaf`/`..._leaf8` both
@@ -2976,8 +3012,11 @@ fn code_square_inter(
29763012 // below, exactly as `search_inter_block` seeds its own from the searches
29773013 // it already ran.
29783014 let mut searched_new: Option<(i32, i32)> = None;
2979- if leaf_new_mv() {
3015+ // What that search cost, kept for the second reference's own early-out
3016+ // below (`None` when this leaf ran no search at all).
3017+ let mut searched_new_cost: Option<f64> = None;
29803018 let source_block = luma.source_block(x, y, side);
3019+ if leaf_new_mv() {
29813020 let (seeds, seed_n) = mv_seeds(stack);
29823021 let found = motion::search(
29833022 ref_luma.0,
@@ -2995,6 +3034,7 @@ fn code_square_inter(
29953034 1,
29963035 );
29973036 let new_mv = round_to_valid_mv(found.mv, stack.pred_mv);
3037+ searched_new_cost = Some(found.cost);
29983038 if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) {
29993039 searched_new = Some(new_mv);
30003040 // Same reuse as `search_inter_block`: a NEWMV equal to the
@@ -3065,13 +3105,12 @@ fn code_square_inter(
30653105 }
30663106
30673107 // The COMPOUND candidates at this leaf (lane-av1comp3), the same set
3068- // `search_inter_block` prices for a whole 32x32 block minus the two that
3069- // need a motion search against the SECOND reference: a leaf never runs
3070- // one (`NEAREST_NEWMV`/`NEW_NEWMV` would each cost another
3071- // `motion::search` per extra reference per leaf), so the set here is
3072- // `NEAREST_NEARESTMV`, `GLOBAL_GLOBALMV` and `NEW_NEARESTMV` off the
3073- // vector this leaf already searched. Priced with the same static-CDF
3074- // approximation, plus the compound-only syntax the writer emits.
3108+ // `search_inter_block` prices for a whole 32x32 block:
3109+ // `NEAREST_NEARESTMV`, `GLOBAL_GLOBALMV`, `NEW_NEARESTMV` off the vector
3110+ // this leaf already searched, and -- since lane-av1comp4 gave the leaf a
3111+ // SECOND-reference search of its own -- `NEAREST_NEWMV` and `NEW_NEWMV`
3112+ // too. Priced with the same static-CDF approximation, plus the
3113+ // compound-only syntax the writer emits.
30753114 for (ref1, g, cstack) in compound.iter().filter(|_| leaf_compound()) {
30763115 let (ref1, g) = (*ref1, *g);
30773116 let uni =
@@ -3118,13 +3157,48 @@ fn code_square_inter(
31183157 },
31193158 ),
31203159 ];
3121- // `NEW_NEARESTMV`: the NEW half is this leaf's own searched vector,
3122- // its residual priced against compound stack entry 0 (what
3123- // `assign_compound_mv` predicts from at `ref_mv_idx = 0`).
3160+ // Every compound MV residual here is priced against compound stack
3161+ // entry 0, which is what `assign_compound_mv` predicts from at
3162+ // `ref_mv_idx = 0` -- so it is also what the second-reference search
3163+ // below seeds from and rounds to, and no candidate it finds can then
3164+ // fail `mv_residual_bits`.
31243165 let cbase = cstack
31253166 .entries
31263167 .first()
31273168 .map_or(cstack.nearest_mv, |e| (e.mv0, e.mv1));
3169+ // This leaf's SECOND-reference motion search (lane-av1comp4). Without
3170+ // it a leaf could only name a stack vector for the second half, and
3171+ // screen capture showed the consequence: every leaf compound block
3172+ // there was `NEAREST_NEARESTMV`. Skipped, like `search_inter_block`'s
3173+ // extra-reference `NEWMV`, when this reference's own
3174+ // `NEAREST_NEARESTMV` vector already prices `margin` times better
3175+ // than the `LAST` search of this same leaf did -- one candidate
3176+ // evaluation standing in for a whole search.
3177+ let margin = leaf_second_new_margin();
3178+ let mut second_new: Option<(i32, i32)> = None;
3179+ if leaf_second_new_mv() {
3180+ let skip = margin > 0.0
3181+ && searched_new_cost.is_some_and(|last| {
3182+ motion::cost_at(
3183+ &g.y, g.width, luma.true_width, luma.true_height, &source_block, x, y,
3184+ side, side, cstack.nearest_mv.1, cbase.1, search.lambda, fctx,
3185+ ) * margin
3186+ <= last
3187+ });
3188+ if !skip {
3189+ let mut seeds = [(0, 0); 4];
3190+ let mut seed_n = 1;
3191+ for e in cstack.entries.iter().take(3) {
3192+ seeds[seed_n] = e.mv1;
3193+ seed_n += 1;
3194+ }
3195+ let found = motion::search(
3196+ &g.y, g.width, luma.true_width, luma.true_height, &source_block, x, y, side,
3197+ side, cbase.1, &seeds[..seed_n], search.lambda, fctx, ref_distance(ref1),
3198+ );
3199+ second_new = Some(round_to_valid_mv(found.mv, cbase.1));
3200+ }
3201+ }
31283202 if let Some(new_mv) = searched_new {
31293203 if let Some(b0) = mv_residual_bits(new_mv, cbase.0) {
31303204 ccands.push((
@@ -3141,6 +3215,40 @@ fn code_square_inter(
31413215 ));
31423216 }
31433217 }
3218+ // `NEAREST_NEWMV` and `NEW_NEWMV`: the NEW halves are the two
3219+ // searches this leaf ran, each residual priced against `cbase`.
3220+ if let Some(mv1) = second_new {
3221+ if let Some(b1) = mv_residual_bits(mv1, cbase.1) {
3222+ ccands.push((
3223+ (cstack.nearest_mv.0, mv1),
3224+ mode_bits_of(2) + b1,
3225+ InterInfo {
3226+ ref_frame: crate::mvstack::LAST_FRAME,
3227+ mode: InterMode::NearestNewMv,
3228+ mv: cstack.nearest_mv.0,
3229+ mv1,
3230+ ref1: Some(ref1),
3231+ ref_mv_idx: 0,
3232+ },
3233+ ));
3234+ if let Some((new_mv, b0)) =
3235+ searched_new.and_then(|m| mv_residual_bits(m, cbase.0).map(|b| (m, b)))
3236+ {
3237+ ccands.push((
3238+ (new_mv, mv1),
3239+ mode_bits_of(7) + b0 + b1,
3240+ InterInfo {
3241+ ref_frame: crate::mvstack::LAST_FRAME,
3242+ mode: InterMode::NewNewMv,
3243+ mv: new_mv,
3244+ mv1,
3245+ ref1: Some(ref1),
3246+ ref_mv_idx: 0,
3247+ },
3248+ ));
3249+ }
3250+ }
3251+ }
31443252 ccands.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1)));
31453253 ccands.dedup_by_key(|c| c.0);
31463254 let g_luma = (g.y.as_slice(), g.width, luma.true_width, luma.true_height);