An inter LEAF searches its second reference, so NEAREST_NEW and NEW_NEW reach the leaves
Browse files at this commite83e29a304dec735edd5d3dfe2cdb1fb60d54ee3Parent: bbbf1cd- feat
- an inter LEAF searches its second reference, so NEAREST_NEW and NEW_NEW reach the leaves
1 changed file +119 -11
- crates/ec-av1/src/encode.rs +119 -11
crates/ec-av1/src/encode.rsmodified+119 -11
| @@ -190,6 +190,42 @@ fn extra_new_skip_margin() -> f64 { | ||
| 190 | 190 | } |
| 191 | 191 | } |
| 192 | 192 | |
| 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 | + | |
| 193 | 229 | /// Whether a 16x16 or 8x8 inter LEAF is offered the compound candidates the |
| 194 | 230 | /// whole 32x32 block is (lane-av1comp3). The writer already codes a compound |
| 195 | 231 | /// block at any leaf size (`tile::write_inter_frame_leaf`/`..._leaf8` both |
| @@ -2976,8 +3012,11 @@ fn code_square_inter( | ||
| 2976 | 3012 | // below, exactly as `search_inter_block` seeds its own from the searches |
| 2977 | 3013 | // it already ran. |
| 2978 | 3014 | 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; | |
| 2980 | 3018 | let source_block = luma.source_block(x, y, side); |
| 3019 | + if leaf_new_mv() { | |
| 2981 | 3020 | let (seeds, seed_n) = mv_seeds(stack); |
| 2982 | 3021 | let found = motion::search( |
| 2983 | 3022 | ref_luma.0, |
| @@ -2995,6 +3034,7 @@ fn code_square_inter( | ||
| 2995 | 3034 | 1, |
| 2996 | 3035 | ); |
| 2997 | 3036 | let new_mv = round_to_valid_mv(found.mv, stack.pred_mv); |
| 3037 | + searched_new_cost = Some(found.cost); | |
| 2998 | 3038 | if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) { |
| 2999 | 3039 | searched_new = Some(new_mv); |
| 3000 | 3040 | // Same reuse as `search_inter_block`: a NEWMV equal to the |
| @@ -3065,13 +3105,12 @@ fn code_square_inter( | ||
| 3065 | 3105 | } |
| 3066 | 3106 | |
| 3067 | 3107 | // 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. | |
| 3075 | 3114 | for (ref1, g, cstack) in compound.iter().filter(|_| leaf_compound()) { |
| 3076 | 3115 | let (ref1, g) = (*ref1, *g); |
| 3077 | 3116 | let uni = |
| @@ -3118,13 +3157,48 @@ fn code_square_inter( | ||
| 3118 | 3157 | }, |
| 3119 | 3158 | ), |
| 3120 | 3159 | ]; |
| 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`. | |
| 3124 | 3165 | let cbase = cstack |
| 3125 | 3166 | .entries |
| 3126 | 3167 | .first() |
| 3127 | 3168 | .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 | + } | |
| 3128 | 3202 | if let Some(new_mv) = searched_new { |
| 3129 | 3203 | if let Some(b0) = mv_residual_bits(new_mv, cbase.0) { |
| 3130 | 3204 | ccands.push(( |
| @@ -3141,6 +3215,40 @@ fn code_square_inter( | ||
| 3141 | 3215 | )); |
| 3142 | 3216 | } |
| 3143 | 3217 | } |
| 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 | + } | |
| 3144 | 3252 | ccands.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1))); |
| 3145 | 3253 | ccands.dedup_by_key(|c| c.0); |
| 3146 | 3254 | let g_luma = (g.y.as_slice(), g.width, luma.true_width, luma.true_height); |