Merge main into lane-av1simd
Browse files at this commit2 changed files +336 -106
- crates/ec-av1/src/encode.rs +321 -106
- crates/ec-av1/src/tile.rs +15 -0
crates/ec-av1/src/encode.rsmodified+321 -106
| @@ -190,6 +190,21 @@ fn extra_new_skip_margin() -> f64 { | |||
| 190 | } | 190 | } |
| 191 | } | 191 | } |
| 192 | 192 | ||
| 193 | +/// Whether a 16x16 or 8x8 inter LEAF is offered the compound candidates the | ||
| 194 | +/// whole 32x32 block is (lane-av1comp3). The writer already codes a compound | ||
| 195 | +/// block at any leaf size (`tile::write_inter_frame_leaf`/`..._leaf8` both | ||
| 196 | +/// route through `write_compound_block`, which is sized by `bw4`/`bh4`); what | ||
| 197 | +/// was missing was the encoder ever forming one below 32x32. | ||
| 198 | +const LEAF_COMPOUND: bool = true; | ||
| 199 | + | ||
| 200 | +/// [`LEAF_COMPOUND`], or what `EC_AV1_LEAF_COMPOUND` names in any build. | ||
| 201 | +fn leaf_compound() -> bool { | ||
| 202 | + match std::env::var("EC_AV1_LEAF_COMPOUND").ok() { | ||
| 203 | + Some(v) => v != "0", | ||
| 204 | + None => LEAF_COMPOUND, | ||
| 205 | + } | ||
| 206 | +} | ||
| 207 | + | ||
| 193 | /// [`LEAF_NEW_MV`], or what `EC_AV1_LEAF_NEWMV` names in a test build. | 208 | /// [`LEAF_NEW_MV`], or what `EC_AV1_LEAF_NEWMV` names in a test build. |
| 194 | fn leaf_new_mv() -> bool { | 209 | fn leaf_new_mv() -> bool { |
| 195 | match std::env::var("EC_AV1_LEAF_NEWMV").ok() { | 210 | match std::env::var("EC_AV1_LEAF_NEWMV").ok() { |
| @@ -2819,7 +2834,12 @@ fn code_square_inter( | |||
| 2819 | search: &Search, | 2834 | search: &Search, |
| 2820 | mode_bits: &[f64; 13], | 2835 | mode_bits: &[f64; 13], |
| 2821 | reference: &Picture, | 2836 | reference: &Picture, |
| 2822 | - stack: &MvStack, fctx: &crate::decode::FrameCtx, | 2837 | + stack: &MvStack, |
| 2838 | + // The COMPOUND stack of each `LAST` + extra-reference pair, built at THIS | ||
| 2839 | + // leaf's own `bw4`/`bh4` (empty on a frame that does not code | ||
| 2840 | + // `reference_select`, and at the straddling call sites). | ||
| 2841 | + compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], | ||
| 2842 | + fctx: &crate::decode::FrameCtx, | ||
| 2823 | ) -> (BlockCoeffs, f64) { | 2843 | ) -> (BlockCoeffs, f64) { |
| 2824 | let (intra_block, intra_cost) = | 2844 | let (intra_block, intra_cost) = |
| 2825 | code_square( | 2845 | code_square( |
| @@ -2956,7 +2976,12 @@ fn code_square_inter( | |||
| 2956 | // not code the movement it split for. Same candidate `search_inter_block` | 2976 | // not code the movement it split for. Same candidate `search_inter_block` |
| 2957 | // prices for a whole block -- `motion::search` from `pred_mv`, rounded to | 2977 | // prices for a whole block -- `motion::search` from `pred_mv`, rounded to |
| 2958 | // a vector the residual syntax can name -- at this leaf's own side. | 2978 | // a vector the residual syntax can name -- at this leaf's own side. |
| 2959 | - let mut best = (inter_cost, None); | 2979 | + let mut best: (f64, Option<(Trial, Trial, Trial, bool, InterInfo)>) = (inter_cost, None); |
| 2980 | + // This leaf's own `NEWMV` vector, kept whether or not it won the | ||
| 2981 | + // single-reference comparison: it seeds the compound half-new candidate | ||
| 2982 | + // below, exactly as `search_inter_block` seeds its own from the searches | ||
| 2983 | + // it already ran. | ||
| 2984 | + let mut searched_new: Option<(i32, i32)> = None; | ||
| 2960 | if leaf_new_mv() { | 2985 | if leaf_new_mv() { |
| 2961 | let source_block = luma.source_block(x, y, side); | 2986 | let source_block = luma.source_block(x, y, side); |
| 2962 | let (seeds, seed_n) = mv_seeds(stack); | 2987 | let (seeds, seed_n) = mv_seeds(stack); |
| @@ -2977,6 +3002,7 @@ fn code_square_inter( | |||
| 2977 | ); | 3002 | ); |
| 2978 | let new_mv = round_to_valid_mv(found.mv, stack.pred_mv); | 3003 | let new_mv = round_to_valid_mv(found.mv, stack.pred_mv); |
| 2979 | if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) { | 3004 | if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) { |
| 3005 | + searched_new = Some(new_mv); | ||
| 2980 | // Same reuse as `search_inter_block`: a NEWMV equal to the | 3006 | // Same reuse as `search_inter_block`: a NEWMV equal to the |
| 2981 | // stack's NEARESTMV is the same three trials, already run above. | 3007 | // stack's NEARESTMV is the same three trials, already run above. |
| 2982 | let (luma_new, u_new, v_new) = if new_mv == mv { | 3008 | let (luma_new, u_new, v_new) = if new_mv == mv { |
| @@ -3023,15 +3049,170 @@ fn code_square_inter( | |||
| 3023 | luma_new.bits + u_new.bits + v_new.bits | 3049 | luma_new.bits + u_new.bits + v_new.bits |
| 3024 | }); | 3050 | }); |
| 3025 | if cost_new < best.0 { | 3051 | if cost_new < best.0 { |
| 3026 | - best = (cost_new, Some((luma_new, u_new, v_new, skip_new, new_mv))); | 3052 | + best = ( |
| 3053 | + cost_new, | ||
| 3054 | + Some(( | ||
| 3055 | + luma_new, | ||
| 3056 | + u_new, | ||
| 3057 | + v_new, | ||
| 3058 | + skip_new, | ||
| 3059 | + InterInfo { | ||
| 3060 | + ref1: None, | ||
| 3061 | + mv1: (0, 0), | ||
| 3062 | + ref_frame: crate::mvstack::LAST_FRAME, | ||
| 3063 | + mode: InterMode::NewMv, | ||
| 3064 | + mv: new_mv, | ||
| 3065 | + ref_mv_idx: 0, | ||
| 3066 | + }, | ||
| 3067 | + )), | ||
| 3068 | + ); | ||
| 3027 | } | 3069 | } |
| 3028 | } | 3070 | } |
| 3029 | } | 3071 | } |
| 3030 | - if let (cost, Some((luma_new, u_new, v_new, skip_new, new_mv))) = best { | 3072 | + |
| 3031 | - if cost < intra_cost { | 3073 | + // The COMPOUND candidates at this leaf (lane-av1comp3), the same set |
| 3032 | - let (levels, tx_depth, dcost) = commit_inter_luma( | 3074 | + // `search_inter_block` prices for a whole 32x32 block minus the two that |
| 3033 | - luma, (x, y), side, new_mv, (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), search, &luma_new, skip_new, fctx, | 3075 | + // need a motion search against the SECOND reference: a leaf never runs |
| 3076 | + // one (`NEAREST_NEWMV`/`NEW_NEWMV` would each cost another | ||
| 3077 | + // `motion::search` per extra reference per leaf), so the set here is | ||
| 3078 | + // `NEAREST_NEARESTMV`, `GLOBAL_GLOBALMV` and `NEW_NEARESTMV` off the | ||
| 3079 | + // vector this leaf already searched. Priced with the same static-CDF | ||
| 3080 | + // approximation, plus the compound-only syntax the writer emits. | ||
| 3081 | + for (ref1, g, cstack) in compound.iter().filter(|_| leaf_compound()) { | ||
| 3082 | + let (ref1, g) = (*ref1, *g); | ||
| 3083 | + let uni = | ||
| 3084 | + (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME).contains(&ref1); | ||
| 3085 | + let pair_bits = symbol_bits(&cdf::COMP_MODE[0], 1) | ||
| 3086 | + + if uni { | ||
| 3087 | + symbol_bits(&cdf::COMP_REF_TYPE[0], 1) | ||
| 3088 | + + symbol_bits(&cdf::COMP_REF[0][0], 0) | ||
| 3089 | + + symbol_bits(&cdf::COMP_REF[0][1], 0) | ||
| 3090 | + + symbol_bits(&cdf::COMP_BWDREF[0][0], 1) | ||
| 3091 | + } else { | ||
| 3092 | + symbol_bits(&cdf::COMP_REF_TYPE[0], 0) | ||
| 3093 | + + symbol_bits(&cdf::UNI_COMP_REF[0][0], 0) | ||
| 3094 | + + symbol_bits(&cdf::UNI_COMP_REF[0][1], 1) | ||
| 3095 | + + symbol_bits(&cdf::UNI_COMP_REF[0][2], 1) | ||
| 3096 | + }; | ||
| 3097 | + let mode_ctx = | ||
| 3098 | + cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)]; | ||
| 3099 | + let mode_bits_of = | ||
| 3100 | + |mode: usize| pair_bits + symbol_bits(&cdf::INTER_COMPOUND_MODE[mode_ctx], mode); | ||
| 3101 | + let mut ccands: Vec<(((i32, i32), (i32, i32)), f64, InterInfo)> = vec![ | ||
| 3102 | + ( | ||
| 3103 | + cstack.nearest_mv, | ||
| 3104 | + mode_bits_of(0), | ||
| 3105 | + InterInfo { | ||
| 3106 | + ref_frame: crate::mvstack::LAST_FRAME, | ||
| 3107 | + mode: InterMode::NearestNearestMv, | ||
| 3108 | + mv: cstack.nearest_mv.0, | ||
| 3109 | + mv1: cstack.nearest_mv.1, | ||
| 3110 | + ref1: Some(ref1), | ||
| 3111 | + ref_mv_idx: 0, | ||
| 3112 | + }, | ||
| 3113 | + ), | ||
| 3114 | + ( | ||
| 3115 | + ((0, 0), (0, 0)), | ||
| 3116 | + mode_bits_of(6), | ||
| 3117 | + InterInfo { | ||
| 3118 | + ref_frame: crate::mvstack::LAST_FRAME, | ||
| 3119 | + mode: InterMode::GlobalGlobalMv, | ||
| 3120 | + mv: (0, 0), | ||
| 3121 | + mv1: (0, 0), | ||
| 3122 | + ref1: Some(ref1), | ||
| 3123 | + ref_mv_idx: 0, | ||
| 3124 | + }, | ||
| 3125 | + ), | ||
| 3126 | + ]; | ||
| 3127 | + // `NEW_NEARESTMV`: the NEW half is this leaf's own searched vector, | ||
| 3128 | + // its residual priced against compound stack entry 0 (what | ||
| 3129 | + // `assign_compound_mv` predicts from at `ref_mv_idx = 0`). | ||
| 3130 | + let cbase = cstack | ||
| 3131 | + .entries | ||
| 3132 | + .first() | ||
| 3133 | + .map_or(cstack.nearest_mv, |e| (e.mv0, e.mv1)); | ||
| 3134 | + if let Some(new_mv) = searched_new { | ||
| 3135 | + if let Some(b0) = mv_residual_bits(new_mv, cbase.0) { | ||
| 3136 | + ccands.push(( | ||
| 3137 | + (new_mv, cstack.nearest_mv.1), | ||
| 3138 | + mode_bits_of(3) + b0, | ||
| 3139 | + InterInfo { | ||
| 3140 | + ref_frame: crate::mvstack::LAST_FRAME, | ||
| 3141 | + mode: InterMode::NewNearestMv, | ||
| 3142 | + mv: new_mv, | ||
| 3143 | + mv1: cstack.nearest_mv.1, | ||
| 3144 | + ref1: Some(ref1), | ||
| 3145 | + ref_mv_idx: 0, | ||
| 3146 | + }, | ||
| 3147 | + )); | ||
| 3148 | + } | ||
| 3149 | + } | ||
| 3150 | + ccands.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1))); | ||
| 3151 | + ccands.dedup_by_key(|c| c.0); | ||
| 3152 | + let g_luma = (g.y.as_slice(), g.width, luma.true_width, luma.true_height); | ||
| 3153 | + let g_u = ( | ||
| 3154 | + g.u.as_slice(), | ||
| 3155 | + g.width / 2, | ||
| 3156 | + chroma[0].true_width, | ||
| 3157 | + chroma[0].true_height, | ||
| 3158 | + ); | ||
| 3159 | + let g_v = ( | ||
| 3160 | + g.v.as_slice(), | ||
| 3161 | + g.width / 2, | ||
| 3162 | + chroma[1].true_width, | ||
| 3163 | + chroma[1].true_height, | ||
| 3164 | + ); | ||
| 3165 | + for (mvs, bits, info) in ccands { | ||
| 3166 | + let luma_c = mc_trial_compound( | ||
| 3167 | + luma, x, y, side, mvs, true, | ||
| 3168 | + (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), g_luma, | ||
| 3169 | + search.base_q_idx, search.deadzone, luma_set, fctx, | ||
| 3170 | + ); | ||
| 3171 | + let u_c = mc_trial_compound( | ||
| 3172 | + &chroma[0], x / 2, y / 2, side / 2, mvs, false, | ||
| 3173 | + (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3), g_u, | ||
| 3174 | + search.base_q_idx, search.deadzone, chroma_set, fctx, | ||
| 3034 | ); | 3175 | ); |
| 3176 | + let v_c = mc_trial_compound( | ||
| 3177 | + &chroma[1], x / 2, y / 2, side / 2, mvs, false, | ||
| 3178 | + (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3), g_v, | ||
| 3179 | + search.base_q_idx, search.deadzone, chroma_set, fctx, | ||
| 3180 | + ); | ||
| 3181 | + let skip_c = luma_c.levels.iter().all(|&l| l == 0) | ||
| 3182 | + && u_c.levels.iter().all(|&l| l == 0) | ||
| 3183 | + && v_c.levels.iter().all(|&l| l == 0); | ||
| 3184 | + let cost_c = luma_c.sse | ||
| 3185 | + + u_c.sse | ||
| 3186 | + + v_c.sse | ||
| 3187 | + + search.lambda | ||
| 3188 | + * (skip_bits(skip_c) | ||
| 3189 | + + intra_inter_bits(true) | ||
| 3190 | + + bits | ||
| 3191 | + + if skip_c { 0.0 } else { luma_c.bits + u_c.bits + v_c.bits }); | ||
| 3192 | + if cost_c < best.0 { | ||
| 3193 | + best = (cost_c, Some((luma_c, u_c, v_c, skip_c, info))); | ||
| 3194 | + } | ||
| 3195 | + } | ||
| 3196 | + } | ||
| 3197 | + | ||
| 3198 | + if let (cost, Some((luma_new, u_new, v_new, skip_new, info))) = best { | ||
| 3199 | + if cost < intra_cost { | ||
| 3200 | + // A compound winner commits the flat trial it was priced from: | ||
| 3201 | + // `commit_inter_luma`'s var-tx trial set is single-reference | ||
| 3202 | + // (`mc_trial`), so a compound leaf codes `tx_depth = 0`. | ||
| 3203 | + // corner-cut: no var-tx split for a compound leaf -- ceiling is | ||
| 3204 | + // the split gain on 16x16 compound blocks; upgrade path is a | ||
| 3205 | + // compound `commit_inter_luma` taking both references. | ||
| 3206 | + let (levels, tx_depth, dcost) = if info.ref1.is_some() { | ||
| 3207 | + luma.commit(x, y, side, &luma_new); | ||
| 3208 | + (luma_new.levels.clone(), 0, 0.0) | ||
| 3209 | + } else { | ||
| 3210 | + commit_inter_luma( | ||
| 3211 | + luma, (x, y), side, info.mv, | ||
| 3212 | + (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), | ||
| 3213 | + search, &luma_new, skip_new, fctx, | ||
| 3214 | + ) | ||
| 3215 | + }; | ||
| 3035 | chroma[0].commit(x / 2, y / 2, side / 2, &u_new); | 3216 | chroma[0].commit(x / 2, y / 2, side / 2, &u_new); |
| 3036 | chroma[1].commit(x / 2, y / 2, side / 2, &v_new); | 3217 | chroma[1].commit(x / 2, y / 2, side / 2, &v_new); |
| 3037 | return ( | 3218 | return ( |
| @@ -3046,14 +3227,7 @@ fn code_square_inter( | |||
| 3046 | palette: None, | 3227 | palette: None, |
| 3047 | palette_uv: None, | 3228 | palette_uv: None, |
| 3048 | tx_depth, | 3229 | tx_depth, |
| 3049 | - inter: Some(InterInfo { | 3230 | + inter: Some(info), |
| 3050 | - ref1: None, | ||
| 3051 | - mv1: (0, 0), | ||
| 3052 | - ref_frame: crate::mvstack::LAST_FRAME, | ||
| 3053 | - mode: InterMode::NewMv, | ||
| 3054 | - mv: new_mv, | ||
| 3055 | - ref_mv_idx: 0, | ||
| 3056 | - }), | ||
| 3057 | }, | 3231 | }, |
| 3058 | cost + dcost, | 3232 | cost + dcost, |
| 3059 | ); | 3233 | ); |
| @@ -5823,6 +5997,30 @@ pub(crate) fn encode_inter_frame( | |||
| 5823 | let stack = find_mv_stack( | 5997 | let stack = find_mv_stack( |
| 5824 | &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows, | 5998 | &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows, |
| 5825 | ); | 5999 | ); |
| 6000 | + let cstacks: Vec<(i8, &Picture, crate::mvstack::CompoundMvStack)> = | ||
| 6001 | + if header.reference_select && leaf_compound() { | ||
| 6002 | + [ | ||
| 6003 | + (crate::mvstack::GOLDEN_FRAME, golden), | ||
| 6004 | + (crate::mvstack::ALTREF_FRAME, altref), | ||
| 6005 | + ] | ||
| 6006 | + .into_iter() | ||
| 6007 | + .filter_map(|(r, pic)| { | ||
| 6008 | + pic.map(|p| { | ||
| 6009 | + ( | ||
| 6010 | + r, | ||
| 6011 | + p, | ||
| 6012 | + crate::mvstack::find_mv_stack_compound( | ||
| 6013 | + &grid, mi_row, mi_col, 4, 4, | ||
| 6014 | + (crate::mvstack::LAST_FRAME, r), mi_cols, mi_rows, | ||
| 6015 | + grid.sign_bias_table(), &[(0, 0); 7], None, | ||
| 6016 | + ), | ||
| 6017 | + ) | ||
| 6018 | + }) | ||
| 6019 | + }) | ||
| 6020 | + .collect() | ||
| 6021 | + } else { | ||
| 6022 | + Vec::new() | ||
| 6023 | + }; | ||
| 5826 | let (leaf, leaf_cost) = code_square_inter( | 6024 | let (leaf, leaf_cost) = code_square_inter( |
| 5827 | &mut luma, | 6025 | &mut luma, |
| 5828 | &mut chroma, | 6026 | &mut chroma, |
| @@ -5831,7 +6029,7 @@ pub(crate) fn encode_inter_frame( | |||
| 5831 | &search, | 6029 | &search, |
| 5832 | &mode_bits_table, | 6030 | &mode_bits_table, |
| 5833 | reference, | 6031 | reference, |
| 5834 | - &stack, fctx, | 6032 | + &stack, &cstacks, fctx, |
| 5835 | ); | 6033 | ); |
| 5836 | let leaf_cost = leaf_cost | 6034 | let leaf_cost = leaf_cost |
| 5837 | + search.lambda * partition_bits(SUB, false); | 6035 | + search.lambda * partition_bits(SUB, false); |
| @@ -5860,6 +6058,30 @@ pub(crate) fn encode_inter_frame( | |||
| 5860 | let stack8 = find_mv_stack( | 6058 | let stack8 = find_mv_stack( |
| 5861 | &grid, mr, mc, 2, 2, LAST_FRAME, mi_cols, mi_rows, | 6059 | &grid, mr, mc, 2, 2, LAST_FRAME, mi_cols, mi_rows, |
| 5862 | ); | 6060 | ); |
| 6061 | + let cstacks8: Vec<(i8, &Picture, crate::mvstack::CompoundMvStack)> = | ||
| 6062 | + if header.reference_select && leaf_compound() { | ||
| 6063 | + [ | ||
| 6064 | + (crate::mvstack::GOLDEN_FRAME, golden), | ||
| 6065 | + (crate::mvstack::ALTREF_FRAME, altref), | ||
| 6066 | + ] | ||
| 6067 | + .into_iter() | ||
| 6068 | + .filter_map(|(r, pic)| { | ||
| 6069 | + pic.map(|p| { | ||
| 6070 | + ( | ||
| 6071 | + r, | ||
| 6072 | + p, | ||
| 6073 | + crate::mvstack::find_mv_stack_compound( | ||
| 6074 | + &grid, mr, mc, 2, 2, | ||
| 6075 | + (crate::mvstack::LAST_FRAME, r), mi_cols, mi_rows, | ||
| 6076 | + grid.sign_bias_table(), &[(0, 0); 7], None, | ||
| 6077 | + ), | ||
| 6078 | + ) | ||
| 6079 | + }) | ||
| 6080 | + }) | ||
| 6081 | + .collect() | ||
| 6082 | + } else { | ||
| 6083 | + Vec::new() | ||
| 6084 | + }; | ||
| 5863 | let (leaf8, cost) = code_square_inter( | 6085 | let (leaf8, cost) = code_square_inter( |
| 5864 | &mut luma, | 6086 | &mut luma, |
| 5865 | &mut chroma, | 6087 | &mut chroma, |
| @@ -5868,7 +6090,7 @@ pub(crate) fn encode_inter_frame( | |||
| 5868 | &search, | 6090 | &search, |
| 5869 | &mode_bits_table, | 6091 | &mode_bits_table, |
| 5870 | reference, | 6092 | reference, |
| 5871 | - &stack8, fctx, | 6093 | + &stack8, &cstacks8, fctx, |
| 5872 | ); | 6094 | ); |
| 5873 | cost8 += cost; | 6095 | cost8 += cost; |
| 5874 | record_mi(&mut grid, mr, mc, 2, leaf8.inter); | 6096 | record_mi(&mut grid, mr, mc, 2, leaf8.inter); |
| @@ -5969,42 +6191,15 @@ pub(crate) fn encode_inter_frame( | |||
| 5969 | &search, | 6191 | &search, |
| 5970 | &mode_bits_table, | 6192 | &mode_bits_table, |
| 5971 | reference, | 6193 | reference, |
| 5972 | - &stack, fctx, | 6194 | + &stack, &[], fctx, |
| 5973 | ); | 6195 | ); |
| 5974 | - for dr in 0..4 { | 6196 | + // One publication point for every coded leaf |
| 5975 | - for dc in 0..4 { | 6197 | + // (`record_mi`): this site used to spell the vote |
| 5976 | - grid.set( | 6198 | + // out with `ref_frame` pinned to LAST and `mv1` |
| 5977 | - mi_row + dr, | 6199 | + // to (0,0), which is the encoder-grid-drift class |
| 5978 | - mi_col + dc, | 6200 | + // -- correct only for as long as a leaf can never |
| 5979 | - match block.inter { | 6201 | + // be compound. |
| 5980 | - Some(info) => MiInfo { | 6202 | + record_mi(&mut grid, mi_row, mi_col, 4, block.inter); |
| 5981 | - is_inter: true, | ||
| 5982 | - ref_frame: LAST_FRAME, | ||
| 5983 | - ref_frame1: NO_REF1, | ||
| 5984 | - mv1: (0, 0), | ||
| 5985 | - mv: mv16(info.mv), | ||
| 5986 | - is_new_mv: matches!(info.mode, InterMode::NewMv), | ||
| 5987 | - size: 4, | ||
| 5988 | - size_h: 4, | ||
| 5989 | - is_global_mv0: false, | ||
| 5990 | - is_global_mv1: false, | ||
| 5991 | - }, | ||
| 5992 | - None => MiInfo { | ||
| 5993 | - is_inter: false, | ||
| 5994 | - ref_frame: -1, | ||
| 5995 | - ref_frame1: NO_REF1, | ||
| 5996 | - mv1: (0, 0), | ||
| 5997 | - mv: (0, 0), | ||
| 5998 | - is_new_mv: false, | ||
| 5999 | - size: 4, | ||
| 6000 | - size_h: 4, | ||
| 6001 | - is_global_mv0: false, | ||
| 6002 | - is_global_mv1: false, | ||
| 6003 | - }, | ||
| 6004 | - }, | ||
| 6005 | - ); | ||
| 6006 | - } | ||
| 6007 | - } | ||
| 6008 | leaves.push(block); | 6203 | leaves.push(block); |
| 6009 | } else { | 6204 | } else { |
| 6010 | let (x_sub, y_sub) = (sc * SUB, sr * SUB); | 6205 | let (x_sub, y_sub) = (sc * SUB, sr * SUB); |
| @@ -6029,45 +6224,11 @@ pub(crate) fn encode_inter_frame( | |||
| 6029 | &search, | 6224 | &search, |
| 6030 | &mode_bits_table, | 6225 | &mode_bits_table, |
| 6031 | reference, | 6226 | reference, |
| 6032 | - &stack, fctx, | 6227 | + &stack, &[], fctx, |
| 6033 | ); | 6228 | ); |
| 6034 | - for dr in 0..2 { | 6229 | + // Same publication point as the 16x16 |
| 6035 | - for dc in 0..2 { | 6230 | + // straddling leaf above (`record_mi`). |
| 6036 | - grid.set( | 6231 | + record_mi(&mut grid, mi_row, mi_col, 2, leaf.inter); |
| 6037 | - mi_row + dr, | ||
| 6038 | - mi_col + dc, | ||
| 6039 | - match leaf.inter { | ||
| 6040 | - Some(info) => MiInfo { | ||
| 6041 | - is_inter: true, | ||
| 6042 | - ref_frame: LAST_FRAME, | ||
| 6043 | - ref_frame1: NO_REF1, | ||
| 6044 | - mv1: (0, 0), | ||
| 6045 | - mv: mv16(info.mv), | ||
| 6046 | - is_new_mv: matches!( | ||
| 6047 | - info.mode, | ||
| 6048 | - InterMode::NewMv | ||
| 6049 | - ), | ||
| 6050 | - size: 2, | ||
| 6051 | - size_h: 2, | ||
| 6052 | - is_global_mv0: false, | ||
| 6053 | - is_global_mv1: false, | ||
| 6054 | - }, | ||
| 6055 | - None => MiInfo { | ||
| 6056 | - is_inter: false, | ||
| 6057 | - ref_frame: -1, | ||
| 6058 | - ref_frame1: NO_REF1, | ||
| 6059 | - mv1: (0, 0), | ||
| 6060 | - mv: (0, 0), | ||
| 6061 | - is_new_mv: false, | ||
| 6062 | - size: 2, | ||
| 6063 | - size_h: 2, | ||
| 6064 | - is_global_mv0: false, | ||
| 6065 | - is_global_mv1: false, | ||
| 6066 | - }, | ||
| 6067 | - }, | ||
| 6068 | - ); | ||
| 6069 | - } | ||
| 6070 | - } | ||
| 6071 | eight.push(leaf); | 6232 | eight.push(leaf); |
| 6072 | } | 6233 | } |
| 6073 | leaves.push(BlockCoeffs { | 6234 | leaves.push(BlockCoeffs { |
| @@ -9257,11 +9418,32 @@ mod tests { | |||
| 9257 | (ladder, wall) | 9418 | (ladder, wall) |
| 9258 | } | 9419 | } |
| 9259 | 9420 | ||
| 9421 | + /// First differing sample between two pictures, as | ||
| 9422 | + /// `(plane, index, left, right)` -- so a gate failure names the plane and | ||
| 9423 | + /// the sample instead of just "differs". A length difference counts as a | ||
| 9424 | + /// mismatch of that plane at the first index past the shorter one. | ||
| 9425 | + fn first_plane_mismatch(a: &Picture, b: &Picture) -> Option<(&'static str, usize, i64, i64)> { | ||
| 9426 | + for (plane, l, r) in [("Y", &a.y, &b.y), ("U", &a.u, &b.u), ("V", &a.v, &b.v)] { | ||
| 9427 | + if let Some(i) = l.iter().zip(r.iter()).position(|(x, y)| x != y) { | ||
| 9428 | + return Some((plane, i, i64::from(l[i]), i64::from(r[i]))); | ||
| 9429 | + } | ||
| 9430 | + if l.len() != r.len() { | ||
| 9431 | + return Some((plane, l.len().min(r.len()), l.len() as i64, r.len() as i64)); | ||
| 9432 | + } | ||
| 9433 | + } | ||
| 9434 | + None | ||
| 9435 | + } | ||
| 9436 | + | ||
| 9260 | /// The same ladder for this encoder, at four `base_q_idx` points. The PSNR | 9437 | /// The same ladder for this encoder, at four `base_q_idx` points. The PSNR |
| 9261 | - /// comes from ffmpeg's decode of our own stream, and every frame of that | 9438 | + /// comes from ffmpeg's decode of our own stream, and at EVERY point the |
| 9262 | - /// decode is asserted sample-exact against our reconstruction -- which is | 9439 | + /// stream is decoded twice -- by ffmpeg and by our own |
| 9263 | - /// itself a gate: it proves the bitstream says what the encoder thinks. | 9440 | + /// [`crate::stream::decode_stream`] -- and all three pictures (encoder |
| 9441 | + /// reconstruction, ffmpeg's decode, our decode) are asserted sample-exact | ||
| 9442 | + /// in Y, U and V over every shown frame in display order. That is itself | ||
| 9443 | + /// a gate: it proves the bitstream says what the encoder thinks, in the | ||
| 9444 | + /// only two decoders that read it. | ||
| 9264 | fn our_ladder( | 9445 | fn our_ladder( |
| 9446 | + name: &str, | ||
| 9265 | source: &[Picture], | 9447 | source: &[Picture], |
| 9266 | width: usize, | 9448 | width: usize, |
| 9267 | height: usize, | 9449 | height: usize, |
| @@ -9274,13 +9456,27 @@ mod tests { | |||
| 9274 | let encoded = encode_sequence_with_ctx(source, q, 0.5, fctx).unwrap(); | 9456 | let encoded = encode_sequence_with_ctx(source, q, 0.5, fctx).unwrap(); |
| 9275 | wall += start.elapsed().as_secs_f64(); | 9457 | wall += start.elapsed().as_secs_f64(); |
| 9276 | let decoded = ffmpeg_decode_sequence(&encoded.stream, width, height, source.len()); | 9458 | let decoded = ffmpeg_decode_sequence(&encoded.stream, width, height, source.len()); |
| 9459 | + let ours = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); | ||
| 9460 | + assert_eq!( | ||
| 9461 | + ours.len(), | ||
| 9462 | + source.len(), | ||
| 9463 | + "{name} q={q}: our decoder's display-order frame count" | ||
| 9464 | + ); | ||
| 9277 | for (i, (d, e)) in decoded.iter().zip(&encoded.frames).enumerate() { | 9465 | for (i, (d, e)) in decoded.iter().zip(&encoded.frames).enumerate() { |
| 9278 | - assert!( | 9466 | + if let Some((plane, s, got, want)) = first_plane_mismatch(d, &e.reconstruction) { |
| 9279 | - d.y == e.reconstruction.y | 9467 | + panic!( |
| 9280 | - && d.u == e.reconstruction.u | 9468 | + "{name} q={q} frame {i} plane {plane} sample {s}: ffmpeg decoded \ |
| 9281 | - && d.v == e.reconstruction.v, | 9469 | + {got}, the encoder reconstructed {want}" |
| 9282 | - "q={q} frame {i}: ffmpeg's decode differs from our reconstruction" | 9470 | + ); |
| 9283 | - ); | 9471 | + } |
| 9472 | + } | ||
| 9473 | + for (i, (o, e)) in ours.iter().zip(&encoded.frames).enumerate() { | ||
| 9474 | + if let Some((plane, s, got, want)) = first_plane_mismatch(o, &e.reconstruction) { | ||
| 9475 | + panic!( | ||
| 9476 | + "{name} q={q} frame {i} plane {plane} sample {s}: our decoder decoded \ | ||
| 9477 | + {got}, the encoder reconstructed {want}" | ||
| 9478 | + ); | ||
| 9479 | + } | ||
| 9284 | } | 9480 | } |
| 9285 | let mean: f64 = decoded | 9481 | let mean: f64 = decoded |
| 9286 | .iter() | 9482 | .iter() |
| @@ -9317,6 +9513,7 @@ mod tests { | |||
| 9317 | /// the ladder, the wall, and — per pyramid level over the whole ladder — | 9513 | /// the ladder, the wall, and — per pyramid level over the whole ladder — |
| 9318 | /// how many frames and how many bytes it spent. | 9514 | /// how many frames and how many bytes it spent. |
| 9319 | fn our_ladder_pyramid( | 9515 | fn our_ladder_pyramid( |
| 9516 | + name: &str, | ||
| 9320 | source: &[Picture], | 9517 | source: &[Picture], |
| 9321 | width: usize, | 9518 | width: usize, |
| 9322 | height: usize, | 9519 | height: usize, |
| @@ -9361,10 +9558,15 @@ mod tests { | |||
| 9361 | // source picture, in the right order -- checked against our own | 9558 | // source picture, in the right order -- checked against our own |
| 9362 | // decoder as well as ffmpeg's, both below. | 9559 | // decoder as well as ffmpeg's, both below. |
| 9363 | let ours = crate::stream::decode_stream(&stream).expect("our decoder"); | 9560 | let ours = crate::stream::decode_stream(&stream).expect("our decoder"); |
| 9364 | - assert_eq!(ours.len(), source.len(), "q={q}: our display-order count"); | 9561 | + assert_eq!(ours.len(), source.len(), "{name} q={q}: our display-order count"); |
| 9365 | let decoded = ffmpeg_decode_sequence(&stream, width, height, source.len()); | 9562 | let decoded = ffmpeg_decode_sequence(&stream, width, height, source.len()); |
| 9366 | for (i, (d, o)) in decoded.iter().zip(&ours).enumerate() { | 9563 | for (i, (d, o)) in decoded.iter().zip(&ours).enumerate() { |
| 9367 | - assert!(d.y == o.y, "q={q} display frame {i}: ffmpeg differs from our decoder"); | 9564 | + if let Some((plane, s, got, want)) = first_plane_mismatch(d, o) { |
| 9565 | + panic!( | ||
| 9566 | + "{name} q={q} display frame {i} plane {plane} sample {s}: ffmpeg \ | ||
| 9567 | + decoded {got}, our decoder decoded {want}" | ||
| 9568 | + ); | ||
| 9569 | + } | ||
| 9368 | } | 9570 | } |
| 9369 | let mean: f64 = decoded | 9571 | let mean: f64 = decoded |
| 9370 | .iter() | 9572 | .iter() |
| @@ -9409,7 +9611,7 @@ mod tests { | |||
| 9409 | }) | 9611 | }) |
| 9410 | }; | 9612 | }; |
| 9411 | let pins: [(u8, usize, u64); 2] = | 9613 | let pins: [(u8, usize, u64); 2] = |
| 9412 | - [(150, 7103, 0xb9b7_763b_6f18_a887), (60, 26778, 0x4071_f16e_1e5d_87c3)]; | 9614 | + [(150, 7084, 0xe6a9_2f62_3855_0eee), (60, 26116, 0xa831_9866_b799_80fc)]; |
| 9413 | for (q, bytes, hash) in pins { | 9615 | for (q, bytes, hash) in pins { |
| 9414 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); | 9616 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); |
| 9415 | assert_eq!( | 9617 | assert_eq!( |
| @@ -9563,8 +9765,15 @@ mod tests { | |||
| 9563 | /// search's own cost, uncut: see the two intra-pruning variants measured | 9765 | /// search's own cost, uncut: see the two intra-pruning variants measured |
| 9564 | /// and rejected in the lane report). No | 9766 | /// and rejected in the lane report). No |
| 9565 | /// threshold is asserted yet -- this run sets the baseline; the gate | 9767 | /// threshold is asserted yet -- this run sets the baseline; the gate |
| 9566 | - /// asserts only four monotone points per ladder and that ffmpeg decodes | 9768 | + /// asserts four monotone points per ladder, and at EVERY ladder point of |
| 9567 | - /// our stream sample-exact against our reconstruction. | 9769 | + /// EVERY clip it decodes our stream twice -- with ffmpeg and with our own |
| 9770 | + /// `crate::stream::decode_stream` -- and asserts the encoder's | ||
| 9771 | + /// reconstruction, ffmpeg's decode and our decode sample-exact in Y, U | ||
| 9772 | + /// and V over every shown frame in display order. A failure names the | ||
| 9773 | + /// clip, the q, the frame, the plane and the first differing sample. | ||
| 9774 | + /// (Under `EC_AV1_PYRAMID` the frames come out of the streaming facade, | ||
| 9775 | + /// which exposes no per-packet reconstruction, so that path asserts the | ||
| 9776 | + /// two decoders against each other on all three planes.) | ||
| 9568 | /// | 9777 | /// |
| 9569 | /// Run: | 9778 | /// Run: |
| 9570 | /// cargo test -p ec-av1 --release --lib -- --ignored \ | 9779 | /// cargo test -p ec-av1 --release --lib -- --ignored \ |
| @@ -9644,12 +9853,13 @@ mod tests { | |||
| 9644 | let _ = crate::tile::take_ref_hits(); | 9853 | let _ = crate::tile::take_ref_hits(); |
| 9645 | let _ = crate::tile::take_compound_mode_hits(); | 9854 | let _ = crate::tile::take_compound_mode_hits(); |
| 9646 | let _ = crate::tile::take_compound_pair_hits(); | 9855 | let _ = crate::tile::take_compound_pair_hits(); |
| 9856 | + let _ = crate::tile::take_compound_size_hits(); | ||
| 9647 | let _ = crate::motion::take_census(); | 9857 | let _ = crate::motion::take_census(); |
| 9648 | let (ours, ours_wall) = match pyramid_from_env() { | 9858 | let (ours, ours_wall) = match pyramid_from_env() { |
| 9649 | - None => our_ladder(&source, width, height, fctx), | 9859 | + None => our_ladder(name, &source, width, height, fctx), |
| 9650 | Some(pyramid) => { | 9860 | Some(pyramid) => { |
| 9651 | let (ladder, wall, counts, bytes) = | 9861 | let (ladder, wall, counts, bytes) = |
| 9652 | - our_ladder_pyramid(&source, width, height, pyramid); | 9862 | + our_ladder_pyramid(name, &source, width, height, pyramid); |
| 9653 | eprintln!( | 9863 | eprintln!( |
| 9654 | "{name}: pyramid {pyramid:?} -- frames key {} arf {} leaf {} \ | 9864 | "{name}: pyramid {pyramid:?} -- frames key {} arf {} leaf {} \ |
| 9655 | show_existing {}; bytes key {} arf {} leaf {} show_existing {}", | 9865 | show_existing {}; bytes key {} arf {} leaf {} show_existing {}", |
| @@ -9685,6 +9895,7 @@ mod tests { | |||
| 9685 | // picks cannot be what a BD number credits). | 9895 | // picks cannot be what a BD number credits). |
| 9686 | let comp_modes = crate::tile::take_compound_mode_hits(); | 9896 | let comp_modes = crate::tile::take_compound_mode_hits(); |
| 9687 | let comp_pairs = crate::tile::take_compound_pair_hits(); | 9897 | let comp_pairs = crate::tile::take_compound_pair_hits(); |
| 9898 | + let comp_sizes = crate::tile::take_compound_size_hits(); | ||
| 9688 | let comp_total: usize = comp_modes.iter().sum(); | 9899 | let comp_total: usize = comp_modes.iter().sum(); |
| 9689 | eprintln!( | 9900 | eprintln!( |
| 9690 | "{name}: compound {comp_total} of {} inter blocks ({:.1}%); modes \ | 9901 | "{name}: compound {comp_total} of {} inter blocks ({:.1}%); modes \ |
| @@ -9696,6 +9907,10 @@ mod tests { | |||
| 9696 | comp_modes[6], comp_modes[7], | 9907 | comp_modes[6], comp_modes[7], |
| 9697 | comp_pairs[3], comp_pairs[6], | 9908 | comp_pairs[3], comp_pairs[6], |
| 9698 | ); | 9909 | ); |
| 9910 | + eprintln!( | ||
| 9911 | + "{name}: compound by size 8x8 {} 16x16 {} 32x32 {}", | ||
| 9912 | + comp_sizes[1], comp_sizes[2], comp_sizes[3], | ||
| 9913 | + ); | ||
| 9699 | // Where the wall-owning stage actually goes: how many candidate | 9914 | // Where the wall-owning stage actually goes: how many candidate |
| 9700 | // evaluations (one `mc::predict` + SAD each) a search spends, how | 9915 | // evaluations (one `mc::predict` + SAD each) a search spends, how |
| 9701 | // many of them are subpel, and how often the winner is the seed | 9916 | // many of them are subpel, and how often the winner is the seed |
crates/ec-av1/src/tile.rsmodified+15 -0
| @@ -478,6 +478,19 @@ pub(crate) fn take_compound_pair_hits() -> [usize; 7] { | |||
| 478 | std::array::from_fn(|i| COMPOUND_PAIR_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | 478 | std::array::from_fn(|i| COMPOUND_PAIR_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) |
| 479 | } | 479 | } |
| 480 | 480 | ||
| 481 | +/// Per-SIZE fire counts of [`write_compound_block`], indexed by | ||
| 482 | +/// `log2(bw4)` (1 = 8x8, 2 = 16x16, 3 = 32x32 and up): compound started as a | ||
| 483 | +/// 32x32-only tool, so the share by size is what says whether the leaf | ||
| 484 | +/// candidates fire at all (gate-blind-to-feature). | ||
| 485 | +static COMPOUND_SIZE_HITS: [std::sync::atomic::AtomicUsize; 4] = | ||
| 486 | + [const { std::sync::atomic::AtomicUsize::new(0) }; 4]; | ||
| 487 | + | ||
| 488 | +/// Takes and clears the compound size histogram. | ||
| 489 | +#[cfg(test)] | ||
| 490 | +pub(crate) fn take_compound_size_hits() -> [usize; 4] { | ||
| 491 | + std::array::from_fn(|i| COMPOUND_SIZE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | ||
| 492 | +} | ||
| 493 | + | ||
| 481 | /// Writes one COMPOUND_REFERENCE block's whole mode chain -- the writer side | 494 | /// Writes one COMPOUND_REFERENCE block's whole mode chain -- the writer side |
| 482 | /// of decode.rs' `read_compound_ref_frames` / `read_inter_compound_mode` / | 495 | /// of decode.rs' `read_compound_ref_frames` / `read_inter_compound_mode` / |
| 483 | /// `assign_compound_mv` / `comp_group_idx` / `compound_idx` sequence, in that | 496 | /// `assign_compound_mv` / `comp_group_idx` / `compound_idx` sequence, in that |
| @@ -615,6 +628,8 @@ fn write_compound_block( | |||
| 615 | COMPOUND_MODE_HITS[mode].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 628 | COMPOUND_MODE_HITS[mode].fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 616 | COMPOUND_PAIR_HITS[(ref1.max(1) - 1) as usize % 7] | 629 | COMPOUND_PAIR_HITS[(ref1.max(1) - 1) as usize % 7] |
| 617 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 630 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 631 | + COMPOUND_SIZE_HITS[(bw4.max(1).trailing_zeros() as usize).min(3)] | ||
| 632 | + .fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 618 | if crate::envflags::env_flag!("EC_COMP_MISMATCH") && (mvs.0 != info.mv || mvs.1 != info.mv1) { | 633 | if crate::envflags::env_flag!("EC_COMP_MISMATCH") && (mvs.0 != info.mv || mvs.1 != info.mv1) { |
| 619 | eprintln!( | 634 | eprintln!( |
| 620 | "EC_COMP_MISMATCH mode={mode} enc=({:?},{:?}) wrote=({:?},{:?})", | 635 | "EC_COMP_MISMATCH mode={mode} enc=({:?},{:?}) wrote=({:?},{:?})", |