An inter frame's 32x32 blocks pick their own partition
Browse files at this commite2e76c0565e3b7b104cf55a21305a886df00510bParent: 45dba91- feat
- an inter frame's 32x32 blocks pick their own partition
1 changed file +172 -54
- crates/ec-av1/src/encode.rs +172 -54
crates/ec-av1/src/encode.rsmodified+172 -54
| @@ -110,6 +110,41 @@ pub(crate) fn take_predicted_bits() -> Vec<f64> { | |||
| 110 | PREDICTED_BITS.with(|p| std::mem::take(&mut *p.borrow_mut())) | 110 | PREDICTED_BITS.with(|p| std::mem::take(&mut *p.borrow_mut())) |
| 111 | } | 111 | } |
| 112 | 112 | ||
| 113 | +/// Whether an INTER frame's 32x32 block may be split into four 16x16 ones | ||
| 114 | +/// when the trial says four cost less. Before lane-av1rd2 only a quadrant | ||
| 115 | +/// the true frame edge cut through was ever split there; inside the frame | ||
| 116 | +/// every block was coded whole, which left the partition decision the key | ||
| 117 | +/// frame already makes unmade for eleven frames out of twelve. | ||
| 118 | +const SPLIT_INTER_BLOCKS: bool = true; | ||
| 119 | + | ||
| 120 | +/// [`SPLIT_INTER_BLOCKS`], or what `EC_AV1_SPLIT_INTER` names when the | ||
| 121 | +/// measurement that keeps it is running (test builds only, like | ||
| 122 | +/// [`lambda_scale`]). | ||
| 123 | +fn split_inter_blocks() -> bool { | ||
| 124 | + match std::env::var("EC_AV1_SPLIT_INTER").ok() { | ||
| 125 | + Some(v) if cfg!(test) => v != "0", | ||
| 126 | + _ => SPLIT_INTER_BLOCKS, | ||
| 127 | + } | ||
| 128 | +} | ||
| 129 | + | ||
| 130 | +/// How often each partition decision was taken since the last | ||
| 131 | +/// [`take_partition_hits`], so a gate can report how often a new partition | ||
| 132 | +/// fires rather than assume it does (gate-blind-to-feature). Index 0 is an | ||
| 133 | +/// inter 32x32 block left whole, 1 one split into four 16x16 leaves. | ||
| 134 | +static PARTITION_HITS: [std::sync::atomic::AtomicUsize; 2] = | ||
| 135 | + [std::sync::atomic::AtomicUsize::new(0), std::sync::atomic::AtomicUsize::new(0)]; | ||
| 136 | + | ||
| 137 | +/// Records one such decision. | ||
| 138 | +fn partition_hit(which: usize) { | ||
| 139 | + PARTITION_HITS[which].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 140 | +} | ||
| 141 | + | ||
| 142 | +/// Takes (and clears) the counts. | ||
| 143 | +#[cfg(test)] | ||
| 144 | +pub(crate) fn take_partition_hits() -> [usize; 2] { | ||
| 145 | + [0, 1].map(|i| PARTITION_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | ||
| 146 | +} | ||
| 147 | + | ||
| 113 | /// Whether a 32x32 block may be split into four 16x16 ones when the trial says | 148 | /// Whether a 32x32 block may be split into four 16x16 ones when the trial says |
| 114 | /// four cost less. Set from the measurement in the lane report. | 149 | /// four cost less. Set from the measurement in the lane report. |
| 115 | const SPLIT_BLOCKS: bool = true; | 150 | const SPLIT_BLOCKS: bool = true; |
| @@ -1649,7 +1684,7 @@ fn code_square_inter( | |||
| 1649 | mode_bits: &[f64; 13], | 1684 | mode_bits: &[f64; 13], |
| 1650 | reference: &Picture, | 1685 | reference: &Picture, |
| 1651 | stack: &MvStack, fctx: &crate::decode::FrameCtx, | 1686 | stack: &MvStack, fctx: &crate::decode::FrameCtx, |
| 1652 | -) -> BlockCoeffs { | 1687 | +) -> (BlockCoeffs, f64) { |
| 1653 | let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits, fctx); | 1688 | let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits, fctx); |
| 1654 | let luma_set = if side == 8 { | 1689 | let luma_set = if side == 8 { |
| 1655 | TxbSet::Luma8Inter | 1690 | TxbSet::Luma8Inter |
| @@ -1773,7 +1808,7 @@ fn code_square_inter( | |||
| 1773 | luma.commit(x, y, side, &luma_trial); | 1808 | luma.commit(x, y, side, &luma_trial); |
| 1774 | chroma[0].commit(x / 2, y / 2, side / 2, &u); | 1809 | chroma[0].commit(x / 2, y / 2, side / 2, &u); |
| 1775 | chroma[1].commit(x / 2, y / 2, side / 2, &v); | 1810 | chroma[1].commit(x / 2, y / 2, side / 2, &v); |
| 1776 | - BlockCoeffs { | 1811 | + (BlockCoeffs { |
| 1777 | luma: coeffs(&luma_trial.levels, side), | 1812 | luma: coeffs(&luma_trial.levels, side), |
| 1778 | u: coeffs(&u.levels, side / 2), | 1813 | u: coeffs(&u.levels, side / 2), |
| 1779 | v: coeffs(&v.levels, side / 2), | 1814 | v: coeffs(&v.levels, side / 2), |
| @@ -1784,9 +1819,47 @@ fn code_square_inter( | |||
| 1784 | mode: InterMode::NearestMv, | 1819 | mode: InterMode::NearestMv, |
| 1785 | mv, | 1820 | mv, |
| 1786 | }), | 1821 | }), |
| 1787 | - } | 1822 | + }, inter_cost) |
| 1788 | } else { | 1823 | } else { |
| 1789 | - intra_block | 1824 | + (intra_block, intra_cost) |
| 1825 | + } | ||
| 1826 | +} | ||
| 1827 | + | ||
| 1828 | +/// Publishes one coded inter-frame block into the `mi` grid the next block's | ||
| 1829 | +/// MV stack reads, over the `size` x `size` 4x4 units it covers. An intra | ||
| 1830 | +/// block casts no vote but is still a coded cell (see `code_square_inter`'s | ||
| 1831 | +/// note on `processed_rows`/`processed_cols`). | ||
| 1832 | +fn record_mi(grid: &mut MiGrid, mi_row: usize, mi_col: usize, size: u8, inter: Option<InterInfo>) { | ||
| 1833 | + let info = match inter { | ||
| 1834 | + Some(info) => MiInfo { | ||
| 1835 | + is_inter: true, | ||
| 1836 | + ref_frame: LAST_FRAME, | ||
| 1837 | + ref_frame1: NO_REF1, | ||
| 1838 | + mv1: (0, 0), | ||
| 1839 | + mv: mv16(info.mv), | ||
| 1840 | + is_new_mv: matches!(info.mode, InterMode::NewMv), | ||
| 1841 | + size, | ||
| 1842 | + size_h: size, | ||
| 1843 | + is_global_mv0: false, | ||
| 1844 | + is_global_mv1: false, | ||
| 1845 | + }, | ||
| 1846 | + None => MiInfo { | ||
| 1847 | + is_inter: false, | ||
| 1848 | + ref_frame: -1, | ||
| 1849 | + ref_frame1: NO_REF1, | ||
| 1850 | + mv1: (0, 0), | ||
| 1851 | + mv: (0, 0), | ||
| 1852 | + is_new_mv: false, | ||
| 1853 | + size, | ||
| 1854 | + size_h: size, | ||
| 1855 | + is_global_mv0: false, | ||
| 1856 | + is_global_mv1: false, | ||
| 1857 | + }, | ||
| 1858 | + }; | ||
| 1859 | + for dr in 0..usize::from(size) { | ||
| 1860 | + for dc in 0..usize::from(size) { | ||
| 1861 | + grid.set(mi_row + dr, mi_col + dc, info); | ||
| 1862 | + } | ||
| 1790 | } | 1863 | } |
| 1791 | } | 1864 | } |
| 1792 | 1865 | ||
| @@ -2615,7 +2688,7 @@ fn search_inter_block( | |||
| 2615 | mode_bits: &[f64; 13], | 2688 | mode_bits: &[f64; 13], |
| 2616 | reference: &Picture, | 2689 | reference: &Picture, |
| 2617 | stack: &MvStack, fctx: &crate::decode::FrameCtx, | 2690 | stack: &MvStack, fctx: &crate::decode::FrameCtx, |
| 2618 | -) -> BlockCoeffs { | 2691 | +) -> (BlockCoeffs, f64) { |
| 2619 | // `luma_set` is the INTRA candidates' table; the two inter candidates | 2692 | // `luma_set` is the INTRA candidates' table; the two inter candidates |
| 2620 | // below price their coefficients through `TxbSet::Luma32Inter`, which is | 2693 | // below price their coefficients through `TxbSet::Luma32Inter`, which is |
| 2621 | // what `sb_coeff_inter_frame_tile` codes an inter block with (it carries | 2694 | // what `sb_coeff_inter_frame_tile` codes an inter block with (it carries |
| @@ -2961,15 +3034,18 @@ fn search_inter_block( | |||
| 2961 | luma.commit(x, y, BLOCK, &best.luma); | 3034 | luma.commit(x, y, BLOCK, &best.luma); |
| 2962 | chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u); | 3035 | chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u); |
| 2963 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); | 3036 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); |
| 2964 | - BlockCoeffs { | 3037 | + ( |
| 2965 | - luma: coeffs(&best.luma.levels, BLOCK), | 3038 | + BlockCoeffs { |
| 2966 | - u: coeffs(&best.u.levels, BLOCK / 2), | 3039 | + luma: coeffs(&best.luma.levels, BLOCK), |
| 2967 | - v: coeffs(&best.v.levels, BLOCK / 2), | 3040 | + u: coeffs(&best.u.levels, BLOCK / 2), |
| 2968 | - mode: best.mode, | 3041 | + v: coeffs(&best.v.levels, BLOCK / 2), |
| 2969 | - skip: best.skip, | 3042 | + mode: best.mode, |
| 2970 | - inter: best.inter, | 3043 | + skip: best.skip, |
| 2971 | - eight: None, | 3044 | + inter: best.inter, |
| 2972 | - } | 3045 | + eight: None, |
| 3046 | + }, | ||
| 3047 | + best.cost, | ||
| 3048 | + ) | ||
| 2973 | } | 3049 | } |
| 2974 | 3050 | ||
| 2975 | /// Encodes one picture as an inter frame predicting from `reference`'s | 3051 | /// Encodes one picture as an inter frame predicting from `reference`'s |
| @@ -3105,7 +3181,8 @@ pub(crate) fn encode_inter_frame( | |||
| 3105 | let stack = | 3181 | let stack = |
| 3106 | find_mv_stack(&grid, mi_row, mi_col, 8, 8, LAST_FRAME, mi_cols, mi_rows); | 3182 | find_mv_stack(&grid, mi_row, mi_col, 8, 8, LAST_FRAME, mi_cols, mi_rows); |
| 3107 | 3183 | ||
| 3108 | - let block = search_inter_block( | 3184 | + let base = snapshot(&luma, &chroma, (x, y)); |
| 3185 | + let (block, mut cost_whole) = search_inter_block( | ||
| 3109 | &mut luma, | 3186 | &mut luma, |
| 3110 | &mut chroma, | 3187 | &mut chroma, |
| 3111 | (x, y), | 3188 | (x, y), |
| @@ -3114,45 +3191,76 @@ pub(crate) fn encode_inter_frame( | |||
| 3114 | reference, | 3191 | reference, |
| 3115 | &stack, fctx, | 3192 | &stack, fctx, |
| 3116 | ); | 3193 | ); |
| 3117 | - | 3194 | + cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 3118 | - for dr in 0..8 { | 3195 | + let after_whole = snapshot(&luma, &chroma, (x, y)); |
| 3119 | - for dc in 0..8 { | 3196 | + |
| 3120 | - grid.set( | 3197 | + // What four 16x16 leaves cost instead (spec |
| 3121 | - mi_row + dr, | 3198 | + // PARTITION_SPLIT at BLOCK_32X32, which |
| 3122 | - mi_col + dc, | 3199 | + // `sb_coeff_inter_frame_tile`'s `Quadrant::Split` arm |
| 3123 | - match block.inter { | 3200 | + // already writes for a straddling quadrant). Each leaf is |
| 3124 | - Some(info) => MiInfo { | 3201 | + // searched against the reconstruction -- and the `mi` |
| 3125 | - is_inter: true, | 3202 | + // grid -- the ones before it left, exactly as the writer |
| 3126 | - ref_frame: LAST_FRAME, | 3203 | + // and the decoder will read them. |
| 3127 | - ref_frame1: NO_REF1, | 3204 | + // |
| 3128 | - mv1: (0, 0), | 3205 | + // A block the whole-32 search left skipped (no residual |
| 3129 | - mv: mv16(info.mv), | 3206 | + // at all) is not offered the split: libaom prunes the |
| 3130 | - is_new_mv: matches!(info.mode, InterMode::NewMv), | 3207 | + // same way at cpu-used 6, and it is where the wall would |
| 3131 | - size: 8, | 3208 | + // otherwise go on static content. |
| 3132 | - size_h: 8, | 3209 | + let leaf_positions: Vec<(usize, usize)> = (0..4) |
| 3133 | - is_global_mv0: false, | 3210 | + .map(|i| (r32 * 2 + i / 2, c32 * 2 + i % 2)) |
| 3134 | - is_global_mv1: false, | 3211 | + .collect(); |
| 3135 | - }, | 3212 | + let leaves_legal = leaf_positions.iter().all(|&(sr, sc)| { |
| 3136 | - // Intra: no vote, but still a coded cell | 3213 | + crate::tile::has_half( |
| 3137 | - // -- mvstack's extended-scan coverage | 3214 | + sc as u32 * crate::tile::SUB_MI, |
| 3138 | - // (`processed_rows`/`processed_cols`) | 3215 | + crate::tile::SUB_MI, |
| 3139 | - // must see it (module doc). | 3216 | + header.mi_cols, |
| 3140 | - None => MiInfo { | 3217 | + ) && crate::tile::has_half( |
| 3141 | - is_inter: false, | 3218 | + sr as u32 * crate::tile::SUB_MI, |
| 3142 | - ref_frame: -1, | 3219 | + crate::tile::SUB_MI, |
| 3143 | - ref_frame1: NO_REF1, | 3220 | + header.mi_rows, |
| 3144 | - mv1: (0, 0), | 3221 | + ) |
| 3145 | - mv: (0, 0), | 3222 | + }); |
| 3146 | - is_new_mv: false, | 3223 | + let split = (!block.skip && leaves_legal && split_inter_blocks()) |
| 3147 | - size: 8, | 3224 | + .then(|| { |
| 3148 | - size_h: 8, | 3225 | + restore(&mut luma, &mut chroma, (x, y), &base); |
| 3149 | - is_global_mv0: false, | 3226 | + let mut cost = search.lambda |
| 3150 | - is_global_mv1: false, | 3227 | + * (partition_bits(BLOCK, true) |
| 3151 | - }, | 3228 | + + 4.0 * partition_bits(SUB, false)); |
| 3152 | - }, | 3229 | + let mut leaves = Vec::with_capacity(4); |
| 3153 | - ); | 3230 | + for &(sr, sc) in &leaf_positions { |
| 3231 | + let (mi_row, mi_col) = (sr * 4, sc * 4); | ||
| 3232 | + let stack = find_mv_stack( | ||
| 3233 | + &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows, | ||
| 3234 | + ); | ||
| 3235 | + let (leaf, leaf_cost) = code_square_inter( | ||
| 3236 | + &mut luma, | ||
| 3237 | + &mut chroma, | ||
| 3238 | + (sc * SUB, sr * SUB), | ||
| 3239 | + SUB, | ||
| 3240 | + &search, | ||
| 3241 | + &mode_bits_table, | ||
| 3242 | + reference, | ||
| 3243 | + &stack, fctx, | ||
| 3244 | + ); | ||
| 3245 | + cost += leaf_cost; | ||
| 3246 | + record_mi(&mut grid, mi_row, mi_col, 4, leaf.inter); | ||
| 3247 | + leaves.push(leaf); | ||
| 3248 | + } | ||
| 3249 | + (leaves, cost) | ||
| 3250 | + }); | ||
| 3251 | + if let Some((leaves, cost)) = split { | ||
| 3252 | + if cost < cost_whole { | ||
| 3253 | + // The leaves' own `mi` cells are already in the | ||
| 3254 | + // grid, and their reconstruction is what the | ||
| 3255 | + // planes hold. | ||
| 3256 | + partition_hit(1); | ||
| 3257 | + blocks[r32 * cols + c32] = Quadrant::Split(leaves); | ||
| 3258 | + continue; | ||
| 3154 | } | 3259 | } |
| 3260 | + restore(&mut luma, &mut chroma, (x, y), &after_whole); | ||
| 3155 | } | 3261 | } |
| 3262 | + partition_hit(0); | ||
| 3263 | + record_mi(&mut grid, mi_row, mi_col, 8, block.inter); | ||
| 3156 | blocks[r32 * cols + c32] = Quadrant::Whole(block); | 3264 | blocks[r32 * cols + c32] = Quadrant::Whole(block); |
| 3157 | } else { | 3265 | } else { |
| 3158 | // The sub-positions actually inside the true frame, same | 3266 | // The sub-positions actually inside the true frame, same |
| @@ -3203,7 +3311,7 @@ pub(crate) fn encode_inter_frame( | |||
| 3203 | let stack = find_mv_stack( | 3311 | let stack = find_mv_stack( |
| 3204 | &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows, | 3312 | &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows, |
| 3205 | ); | 3313 | ); |
| 3206 | - let block = code_square_inter( | 3314 | + let (block, _) = code_square_inter( |
| 3207 | &mut luma, | 3315 | &mut luma, |
| 3208 | &mut chroma, | 3316 | &mut chroma, |
| 3209 | (x, y), | 3317 | (x, y), |
| @@ -3263,7 +3371,7 @@ pub(crate) fn encode_inter_frame( | |||
| 3263 | let stack = find_mv_stack( | 3371 | let stack = find_mv_stack( |
| 3264 | &grid, mi_row, mi_col, 2, 2, LAST_FRAME, mi_cols, mi_rows, | 3372 | &grid, mi_row, mi_col, 2, 2, LAST_FRAME, mi_cols, mi_rows, |
| 3265 | ); | 3373 | ); |
| 3266 | - let leaf = code_square_inter( | 3374 | + let (leaf, _) = code_square_inter( |
| 3267 | &mut luma, | 3375 | &mut luma, |
| 3268 | &mut chroma, | 3376 | &mut chroma, |
| 3269 | (leaf_x, leaf_y), | 3377 | (leaf_x, leaf_y), |
| @@ -6104,7 +6212,17 @@ mod tests { | |||
| 6104 | for (name, path) in &clips { | 6212 | for (name, path) in &clips { |
| 6105 | let fctx = &crate::decode::FrameCtx::new(); | 6213 | let fctx = &crate::decode::FrameCtx::new(); |
| 6106 | let source = clip_frames(path.to_str().unwrap(), "0", width, height, frames); | 6214 | let source = clip_frames(path.to_str().unwrap(), "0", width, height, frames); |
| 6215 | + let _ = take_partition_hits(); | ||
| 6107 | let (ours, ours_wall) = our_ladder(&source, width, height, fctx); | 6216 | let (ours, ours_wall) = our_ladder(&source, width, height, fctx); |
| 6217 | + // How often the inter split actually fired over this clip's four | ||
| 6218 | + // encodes, against the whole 32x32 that was the only outcome | ||
| 6219 | + // before (gate-blind-to-feature). | ||
| 6220 | + let [whole32, split32] = take_partition_hits(); | ||
| 6221 | + eprintln!( | ||
| 6222 | + "{name}: inter 32x32 split into 16s {split32} of {} blocks ({:.1}%)", | ||
| 6223 | + whole32 + split32, | ||
| 6224 | + 100.0 * split32 as f64 / (whole32 + split32).max(1) as f64 | ||
| 6225 | + ); | ||
| 6108 | let (aom, aom_wall) = | 6226 | let (aom, aom_wall) = |
| 6109 | external_ladder(&source, width, height, "libaom-av1", &aom_points); | 6227 | external_ladder(&source, width, height, "libaom-av1", &aom_points); |
| 6110 | let (rav1e, rav1e_wall) = | 6228 | let (rav1e, rav1e_wall) = |