A 16x16 inter leaf splits into four 8x8 ones on cost
Browse files at this commit17448fad957f0c4c72caaa4a05bb6345b9959c96Parent: 62967a8- feat
- a 16x16 inter leaf splits into four 8x8 ones on cost
2 changed files +193 -64
- crates/ec-av1/src/encode.rs +109 -27
- crates/ec-av1/src/tile.rs +84 -37
crates/ec-av1/src/encode.rsmodified+109 -27
| @@ -110,6 +110,17 @@ pub(crate) fn take_predicted_bits() -> Vec<f64> { | ||
| 110 | 110 | PREDICTED_BITS.with(|p| std::mem::take(&mut *p.borrow_mut())) |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | +/// Whether a 16x16 inter leaf may split into four 8x8 ones of its own. | |
| 114 | +const SPLIT_INTER_8: bool = true; | |
| 115 | + | |
| 116 | +/// [`SPLIT_INTER_8`], or what `EC_AV1_SPLIT_INTER8` names in a test build. | |
| 117 | +fn split_inter_8() -> bool { | |
| 118 | + match std::env::var("EC_AV1_SPLIT_INTER8").ok() { | |
| 119 | + Some(v) if cfg!(test) => v != "0", | |
| 120 | + _ => SPLIT_INTER_8, | |
| 121 | + } | |
| 122 | +} | |
| 123 | + | |
| 113 | 124 | /// Whether a 16x16 (or 8x8) leaf runs a motion search of its own beside the |
| 114 | 125 | /// NEARESTMV candidate it always had. |
| 115 | 126 | const LEAF_NEW_MV: bool = true; |
| @@ -142,9 +153,14 @@ fn split_inter_blocks() -> bool { | ||
| 142 | 153 | /// How often each partition decision was taken since the last |
| 143 | 154 | /// [`take_partition_hits`], so a gate can report how often a new partition |
| 144 | 155 | /// fires rather than assume it does (gate-blind-to-feature). Index 0 is an |
| 145 | -/// inter 32x32 block left whole, 1 one split into four 16x16 leaves. | |
| 146 | -static PARTITION_HITS: [std::sync::atomic::AtomicUsize; 2] = | |
| 147 | - [std::sync::atomic::AtomicUsize::new(0), std::sync::atomic::AtomicUsize::new(0)]; | |
| 156 | +/// inter 32x32 block left whole, 1 one split into four 16x16 leaves; 2 is | |
| 157 | +/// such a leaf left whole, 3 one split into four 8x8 leaves of its own. | |
| 158 | +static PARTITION_HITS: [std::sync::atomic::AtomicUsize; 4] = [ | |
| 159 | + std::sync::atomic::AtomicUsize::new(0), | |
| 160 | + std::sync::atomic::AtomicUsize::new(0), | |
| 161 | + std::sync::atomic::AtomicUsize::new(0), | |
| 162 | + std::sync::atomic::AtomicUsize::new(0), | |
| 163 | +]; | |
| 148 | 164 | |
| 149 | 165 | /// Records one such decision. |
| 150 | 166 | fn partition_hit(which: usize) { |
| @@ -153,8 +169,8 @@ fn partition_hit(which: usize) { | ||
| 153 | 169 | |
| 154 | 170 | /// Takes (and clears) the counts. |
| 155 | 171 | #[cfg(test)] |
| 156 | -pub(crate) fn take_partition_hits() -> [usize; 2] { | |
| 157 | - [0, 1].map(|i| PARTITION_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | |
| 172 | +pub(crate) fn take_partition_hits() -> [usize; 4] { | |
| 173 | + [0, 1, 2, 3].map(|i| PARTITION_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | |
| 158 | 174 | } |
| 159 | 175 | |
| 160 | 176 | /// Whether a 32x32 block may be split into four 16x16 ones when the trial says |
| @@ -1965,11 +1981,16 @@ fn record_mi(grid: &mut MiGrid, mi_row: usize, mi_col: usize, size: u8, inter: O | ||
| 1965 | 1981 | |
| 1966 | 1982 | /// The reconstructed samples one 32x32 square covers in all three planes, so |
| 1967 | 1983 | /// that a partition trial can be undone. |
| 1968 | -fn snapshot(luma: &Plane, chroma: &[Plane; 2], (x, y): (usize, usize)) -> [Vec<u8>; 3] { | |
| 1984 | +fn snapshot( | |
| 1985 | + luma: &Plane, | |
| 1986 | + chroma: &[Plane; 2], | |
| 1987 | + (x, y): (usize, usize), | |
| 1988 | + side: usize, | |
| 1989 | +) -> [Vec<u8>; 3] { | |
| 1969 | 1990 | [ |
| 1970 | - luma.snapshot(x, y, BLOCK), | |
| 1971 | - chroma[0].snapshot(x / 2, y / 2, BLOCK / 2), | |
| 1972 | - chroma[1].snapshot(x / 2, y / 2, BLOCK / 2), | |
| 1991 | + luma.snapshot(x, y, side), | |
| 1992 | + chroma[0].snapshot(x / 2, y / 2, side / 2), | |
| 1993 | + chroma[1].snapshot(x / 2, y / 2, side / 2), | |
| 1973 | 1994 | ] |
| 1974 | 1995 | } |
| 1975 | 1996 | |
| @@ -1978,11 +1999,12 @@ fn restore( | ||
| 1978 | 1999 | luma: &mut Plane, |
| 1979 | 2000 | chroma: &mut [Plane; 2], |
| 1980 | 2001 | (x, y): (usize, usize), |
| 2002 | + side: usize, | |
| 1981 | 2003 | saved: &[Vec<u8>; 3], |
| 1982 | 2004 | ) { |
| 1983 | - luma.restore(x, y, BLOCK, &saved[0]); | |
| 1984 | - chroma[0].restore(x / 2, y / 2, BLOCK / 2, &saved[1]); | |
| 1985 | - chroma[1].restore(x / 2, y / 2, BLOCK / 2, &saved[2]); | |
| 2005 | + luma.restore(x, y, side, &saved[0]); | |
| 2006 | + chroma[0].restore(x / 2, y / 2, side / 2, &saved[1]); | |
| 2007 | + chroma[1].restore(x / 2, y / 2, side / 2, &saved[2]); | |
| 1986 | 2008 | } |
| 1987 | 2009 | |
| 1988 | 2010 | /// Keeps the DC candidate plus the cheapest `top_k` (or fewer, once DC is set |
| @@ -2385,7 +2407,7 @@ pub(crate) fn encode_key_frame_inner( | ||
| 2385 | 2407 | } |
| 2386 | 2408 | let (x, y) = (col * BLOCK, row * BLOCK); |
| 2387 | 2409 | let (c0, r0) = (col * 2, row * 2); |
| 2388 | - let base = snapshot(&luma, &chroma, (x, y)); | |
| 2410 | + let base = snapshot(&luma, &chroma, (x, y), BLOCK); | |
| 2389 | 2411 | |
| 2390 | 2412 | // spec `decode_partition`'s hasRows/hasCols recomputed at this |
| 2391 | 2413 | // 32x32 block's own half (see `crate::tile::has_half`): the |
| @@ -2459,11 +2481,11 @@ pub(crate) fn encode_key_frame_inner( | ||
| 2459 | 2481 | &mode_bits(above_mode[c0], left_mode[r0]), fctx, |
| 2460 | 2482 | ); |
| 2461 | 2483 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 2462 | - let after_whole = snapshot(&luma, &chroma, (x, y)); | |
| 2484 | + let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); | |
| 2463 | 2485 | |
| 2464 | 2486 | // What four 16x16 blocks cost instead, each searched against |
| 2465 | 2487 | // the reconstruction the ones before it left. |
| 2466 | - restore(&mut luma, &mut chroma, (x, y), &base); | |
| 2488 | + restore(&mut luma, &mut chroma, (x, y), BLOCK, &base); | |
| 2467 | 2489 | let mut split = Vec::with_capacity(4); |
| 2468 | 2490 | let mut cost_split = search.lambda |
| 2469 | 2491 | * (partition_bits(BLOCK, true) + 4.0 * partition_bits(SUB, false)); |
| @@ -2545,7 +2567,7 @@ pub(crate) fn encode_key_frame_inner( | ||
| 2545 | 2567 | modes.extend_from_slice(&split_modes); |
| 2546 | 2568 | blocks.push(Quadrant::Split(split)); |
| 2547 | 2569 | } else { |
| 2548 | - restore(&mut luma, &mut chroma, (x, y), &after_whole); | |
| 2570 | + restore(&mut luma, &mut chroma, (x, y), BLOCK, &after_whole); | |
| 2549 | 2571 | for cell in 0..2 { |
| 2550 | 2572 | above_mode[c0 + cell] = whole.mode; |
| 2551 | 2573 | left_mode[r0 + cell] = whole.mode; |
| @@ -3284,7 +3306,7 @@ pub(crate) fn encode_inter_frame( | ||
| 3284 | 3306 | let stack = |
| 3285 | 3307 | find_mv_stack(&grid, mi_row, mi_col, 8, 8, LAST_FRAME, mi_cols, mi_rows); |
| 3286 | 3308 | |
| 3287 | - let base = snapshot(&luma, &chroma, (x, y)); | |
| 3309 | + let base = snapshot(&luma, &chroma, (x, y), BLOCK); | |
| 3288 | 3310 | let (block, mut cost_whole) = search_inter_block( |
| 3289 | 3311 | &mut luma, |
| 3290 | 3312 | &mut chroma, |
| @@ -3296,7 +3318,7 @@ pub(crate) fn encode_inter_frame( | ||
| 3296 | 3318 | &stack, fctx, |
| 3297 | 3319 | ); |
| 3298 | 3320 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 3299 | - let after_whole = snapshot(&luma, &chroma, (x, y)); | |
| 3321 | + let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); | |
| 3300 | 3322 | |
| 3301 | 3323 | // What four 16x16 leaves cost instead (spec |
| 3302 | 3324 | // PARTITION_SPLIT at BLOCK_32X32, which |
| @@ -3326,20 +3348,20 @@ pub(crate) fn encode_inter_frame( | ||
| 3326 | 3348 | }); |
| 3327 | 3349 | let split = (!block.skip && leaves_legal && split_inter_blocks()) |
| 3328 | 3350 | .then(|| { |
| 3329 | - restore(&mut luma, &mut chroma, (x, y), &base); | |
| 3330 | - let mut cost = search.lambda | |
| 3331 | - * (partition_bits(BLOCK, true) | |
| 3332 | - + 4.0 * partition_bits(SUB, false)); | |
| 3351 | + restore(&mut luma, &mut chroma, (x, y), BLOCK, &base); | |
| 3352 | + let mut cost = search.lambda * partition_bits(BLOCK, true); | |
| 3333 | 3353 | let mut leaves = Vec::with_capacity(4); |
| 3334 | 3354 | for &(sr, sc) in &leaf_positions { |
| 3335 | 3355 | let (mi_row, mi_col) = (sr * 4, sc * 4); |
| 3356 | + let (lx, ly) = (sc * SUB, sr * SUB); | |
| 3357 | + let leaf_base = snapshot(&luma, &chroma, (lx, ly), SUB); | |
| 3336 | 3358 | let stack = find_mv_stack( |
| 3337 | 3359 | &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows, |
| 3338 | 3360 | ); |
| 3339 | 3361 | let (leaf, leaf_cost) = code_square_inter( |
| 3340 | 3362 | &mut luma, |
| 3341 | 3363 | &mut chroma, |
| 3342 | - (sc * SUB, sr * SUB), | |
| 3364 | + (lx, ly), | |
| 3343 | 3365 | SUB, |
| 3344 | 3366 | &search, |
| 3345 | 3367 | &mode_bits_table, |
| @@ -3347,6 +3369,63 @@ pub(crate) fn encode_inter_frame( | ||
| 3347 | 3369 | &ref_y8, |
| 3348 | 3370 | &stack, fctx, |
| 3349 | 3371 | ); |
| 3372 | + let leaf_cost = leaf_cost | |
| 3373 | + + search.lambda * partition_bits(SUB, false); | |
| 3374 | + let after_leaf = snapshot(&luma, &chroma, (lx, ly), SUB); | |
| 3375 | + | |
| 3376 | + // And what four 8x8 leaves cost instead -- | |
| 3377 | + // the same tree one step further down, which | |
| 3378 | + // the writer codes as a real PARTITION_SPLIT | |
| 3379 | + // at BLOCK_16X16 (lane-av1rd2). Same | |
| 3380 | + // skipped-block prune as the level above. | |
| 3381 | + let eight = (!leaf.skip && split_inter_8()) | |
| 3382 | + .then(|| { | |
| 3383 | + restore( | |
| 3384 | + &mut luma, &mut chroma, (lx, ly), SUB, &leaf_base, | |
| 3385 | + ); | |
| 3386 | + let mut cost8 = search.lambda | |
| 3387 | + * (partition_bits(SUB, true) | |
| 3388 | + + 4.0 * partition_bits(8, false)); | |
| 3389 | + let mut eight = Vec::with_capacity(4); | |
| 3390 | + for i in 0..4 { | |
| 3391 | + let (mr, mc) = | |
| 3392 | + (mi_row + (i / 2) * 2, mi_col + (i % 2) * 2); | |
| 3393 | + let stack8 = find_mv_stack( | |
| 3394 | + &grid, mr, mc, 2, 2, LAST_FRAME, mi_cols, mi_rows, | |
| 3395 | + ); | |
| 3396 | + let (leaf8, cost) = code_square_inter( | |
| 3397 | + &mut luma, | |
| 3398 | + &mut chroma, | |
| 3399 | + (lx + (i % 2) * 8, ly + (i / 2) * 8), | |
| 3400 | + 8, | |
| 3401 | + &search, | |
| 3402 | + &mode_bits_table, | |
| 3403 | + reference, | |
| 3404 | + &ref_y8, | |
| 3405 | + &stack8, fctx, | |
| 3406 | + ); | |
| 3407 | + cost8 += cost; | |
| 3408 | + record_mi(&mut grid, mr, mc, 2, leaf8.inter); | |
| 3409 | + eight.push(leaf8); | |
| 3410 | + } | |
| 3411 | + (eight, cost8) | |
| 3412 | + }); | |
| 3413 | + match eight { | |
| 3414 | + Some((eight, cost8)) if cost8 < leaf_cost => { | |
| 3415 | + partition_hit(3); | |
| 3416 | + cost += cost8; | |
| 3417 | + leaves.push(BlockCoeffs { | |
| 3418 | + eight: Some(eight), | |
| 3419 | + ..BlockCoeffs::default() | |
| 3420 | + }); | |
| 3421 | + continue; | |
| 3422 | + } | |
| 3423 | + Some(_) => restore( | |
| 3424 | + &mut luma, &mut chroma, (lx, ly), SUB, &after_leaf, | |
| 3425 | + ), | |
| 3426 | + None => {} | |
| 3427 | + } | |
| 3428 | + partition_hit(2); | |
| 3350 | 3429 | cost += leaf_cost; |
| 3351 | 3430 | record_mi(&mut grid, mi_row, mi_col, 4, leaf.inter); |
| 3352 | 3431 | leaves.push(leaf); |
| @@ -3362,7 +3441,7 @@ pub(crate) fn encode_inter_frame( | ||
| 3362 | 3441 | blocks[r32 * cols + c32] = Quadrant::Split(leaves); |
| 3363 | 3442 | continue; |
| 3364 | 3443 | } |
| 3365 | - restore(&mut luma, &mut chroma, (x, y), &after_whole); | |
| 3444 | + restore(&mut luma, &mut chroma, (x, y), BLOCK, &after_whole); | |
| 3366 | 3445 | } |
| 3367 | 3446 | partition_hit(0); |
| 3368 | 3447 | record_mi(&mut grid, mi_row, mi_col, 8, block.inter); |
| @@ -6324,11 +6403,14 @@ mod tests { | ||
| 6324 | 6403 | // How often the inter split actually fired over this clip's four |
| 6325 | 6404 | // encodes, against the whole 32x32 that was the only outcome |
| 6326 | 6405 | // before (gate-blind-to-feature). |
| 6327 | - let [whole32, split32] = take_partition_hits(); | |
| 6406 | + let [whole32, split32, whole16, split16] = take_partition_hits(); | |
| 6328 | 6407 | eprintln!( |
| 6329 | - "{name}: inter 32x32 split into 16s {split32} of {} blocks ({:.1}%)", | |
| 6408 | + "{name}: inter 32x32 split into 16s {split32} of {} blocks ({:.1}%); \ | |
| 6409 | + 16x16 leaves split into 8s {split16} of {} ({:.1}%)", | |
| 6330 | 6410 | whole32 + split32, |
| 6331 | - 100.0 * split32 as f64 / (whole32 + split32).max(1) as f64 | |
| 6411 | + 100.0 * split32 as f64 / (whole32 + split32).max(1) as f64, | |
| 6412 | + whole16 + split16, | |
| 6413 | + 100.0 * split16 as f64 / (whole16 + split16).max(1) as f64 | |
| 6332 | 6414 | ); |
| 6333 | 6415 | let (aom, aom_wall) = |
| 6334 | 6416 | external_ladder(&source, width, height, "libaom-av1", &aom_points); |
crates/ec-av1/src/tile.rsmodified+84 -37
| @@ -629,6 +629,9 @@ struct Neighbours { | ||
| 629 | 629 | left_side_mi: Vec<usize>, |
| 630 | 630 | /// Whether the neighbour carried no residual, for [`sb_coeff_inter_frame_tile`]'s |
| 631 | 631 | /// skip context. Unused (and left at its default) by the key frame writers. |
| 632 | + /// Kept per 4x4 mode-info unit, like [`Self::above_side_mi`] and for the | |
| 633 | + /// same reason: under a real PARTITION_SPLIT to 8x8 the four leaves of a | |
| 634 | + /// 16x16 read (and leave) different contexts inside one [`SUB`] slot. | |
| 632 | 635 | above_skip: Vec<bool>, |
| 633 | 636 | /// The same, down the left edge. |
| 634 | 637 | left_skip: Vec<bool>, |
| @@ -676,10 +679,10 @@ impl Neighbours { | ||
| 676 | 679 | left_side: vec![SB; rows], |
| 677 | 680 | above_side_mi: vec![SB; cols * (SUB / MI)], |
| 678 | 681 | left_side_mi: vec![SB; rows * (SUB / MI)], |
| 679 | - above_skip: vec![false; cols], | |
| 680 | - left_skip: vec![false; rows], | |
| 681 | - above_inter: vec![false; cols], | |
| 682 | - left_inter: vec![false; rows], | |
| 682 | + above_skip: vec![false; cols * (SUB / MI)], | |
| 683 | + left_skip: vec![false; rows * (SUB / MI)], | |
| 684 | + above_inter: vec![false; cols * (SUB / MI)], | |
| 685 | + left_inter: vec![false; rows * (SUB / MI)], | |
| 683 | 686 | mi_cols, |
| 684 | 687 | mi_rows, |
| 685 | 688 | } |
| @@ -768,11 +771,17 @@ impl Neighbours { | ||
| 768 | 771 | /// fills for the coefficient and mode state. |
| 769 | 772 | fn record_inter(&mut self, at: (usize, usize), side: usize, skip: bool, is_inter: bool) { |
| 770 | 773 | let (r, c) = at; |
| 771 | - for cell in 0..side / SUB { | |
| 772 | - self.above_skip[c + cell] = skip; | |
| 773 | - self.left_skip[r + cell] = skip; | |
| 774 | - self.above_inter[c + cell] = is_inter; | |
| 775 | - self.left_inter[r + cell] = is_inter; | |
| 774 | + self.record_inter_mi((r * (SUB / MI), c * (SUB / MI)), side, skip, is_inter); | |
| 775 | + } | |
| 776 | + | |
| 777 | + /// [`Self::record_inter`] taking the block's position directly in 4x4 | |
| 778 | + /// mode-info units, for a block finer than one [`SUB`] slot. | |
| 779 | + fn record_inter_mi(&mut self, (mi_r, mi_c): (usize, usize), side: usize, skip: bool, is_inter: bool) { | |
| 780 | + for cell in 0..side / MI { | |
| 781 | + self.above_skip[mi_c + cell] = skip; | |
| 782 | + self.left_skip[mi_r + cell] = skip; | |
| 783 | + self.above_inter[mi_c + cell] = is_inter; | |
| 784 | + self.left_inter[mi_r + cell] = is_inter; | |
| 776 | 785 | } |
| 777 | 786 | } |
| 778 | 787 | |
| @@ -1682,6 +1691,7 @@ pub(crate) fn partition_bits(side: usize, split: bool) -> f64 { | ||
| 1682 | 1691 | let cdf: &[u16] = match side { |
| 1683 | 1692 | SB => &cdfs.partition_w64[0], |
| 1684 | 1693 | BLOCK => &cdfs.partition_w32[0], |
| 1694 | + TX8 => &cdfs.partition_w8[0], | |
| 1685 | 1695 | _ => &cdfs.partition_w16[0], |
| 1686 | 1696 | }; |
| 1687 | 1697 | let symbol = if split { |
| @@ -2368,6 +2378,50 @@ pub fn sb_coeff_inter_frame_tile( | ||
| 2368 | 2378 | let at16 = (sr, sc); |
| 2369 | 2379 | if has_cols16 && has_rows16 { |
| 2370 | 2380 | let ctx16 = neighbours.partition_ctx(at16, SUB); |
| 2381 | + // A 16x16 leaf wholly inside the frame that | |
| 2382 | + // carries `eight` split itself further, on | |
| 2383 | + // cost rather than on geometry (lane-av1rd2): | |
| 2384 | + // spec PARTITION_SPLIT at BLOCK_16X16, then | |
| 2385 | + // the same four 8x8 leaves the straddling | |
| 2386 | + // branch below writes -- all four inside, so | |
| 2387 | + // the partition symbol is a real one rather | |
| 2388 | + // than the edge's gathered flag. | |
| 2389 | + if let Some(leaves) = &leaf.eight { | |
| 2390 | + if leaves.len() != 4 { | |
| 2391 | + return Err(Error::unsupported( | |
| 2392 | + "AV1 tile", | |
| 2393 | + "a 16x16 inter-frame block inside the frame that splits needs all four 8x8 leaves", | |
| 2394 | + )); | |
| 2395 | + } | |
| 2396 | + enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w16[ctx16]); | |
| 2397 | + let (mi_row0, mi_col0) = | |
| 2398 | + (sr as u32 * SUB_MI, sc as u32 * SUB_MI); | |
| 2399 | + for (i, leaf8) in leaves.iter().enumerate() { | |
| 2400 | + let leaf_mi = ( | |
| 2401 | + (mi_row0 + (i as u32 / 2) * 2) as usize, | |
| 2402 | + (mi_col0 + (i as u32 % 2) * 2) as usize, | |
| 2403 | + ); | |
| 2404 | + let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); | |
| 2405 | + enc.symbol( | |
| 2406 | + PARTITION_NONE, | |
| 2407 | + &mut cdfs.partition_w8[leaf_ctx], | |
| 2408 | + ); | |
| 2409 | + let (skip, is_inter) = write_inter_frame_leaf8( | |
| 2410 | + &mut enc, | |
| 2411 | + &mut cdfs, | |
| 2412 | + &mut neighbours, | |
| 2413 | + &mut grid, | |
| 2414 | + mi_cols, | |
| 2415 | + mi_rows, | |
| 2416 | + leaf8, | |
| 2417 | + leaf_mi, | |
| 2418 | + &scan8, | |
| 2419 | + &scan4, | |
| 2420 | + )?; | |
| 2421 | + let _ = (skip, is_inter); | |
| 2422 | + } | |
| 2423 | + continue; | |
| 2424 | + } | |
| 2371 | 2425 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx16]); |
| 2372 | 2426 | write_inter_frame_leaf( |
| 2373 | 2427 | &mut enc, |
| @@ -2413,7 +2467,6 @@ pub fn sb_coeff_inter_frame_tile( | ||
| 2413 | 2467 | `eight` entry per 8x8 leaf inside the true frame", |
| 2414 | 2468 | )); |
| 2415 | 2469 | } |
| 2416 | - let mut prev_leaf: Option<((usize, usize), bool, bool)> = None; | |
| 2417 | 2470 | for (leaf8, (mr, mc)) in leaves.iter().zip(leaf_positions) { |
| 2418 | 2471 | let leaf_mi = (mr as usize, mc as usize); |
| 2419 | 2472 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); |
| @@ -2426,22 +2479,18 @@ pub fn sb_coeff_inter_frame_tile( | ||
| 2426 | 2479 | mi_cols, |
| 2427 | 2480 | mi_rows, |
| 2428 | 2481 | leaf8, |
| 2429 | - at16, | |
| 2430 | 2482 | leaf_mi, |
| 2431 | 2483 | &scan8, |
| 2432 | 2484 | &scan4, |
| 2433 | - prev_leaf, | |
| 2434 | 2485 | )?; |
| 2435 | - prev_leaf = Some((leaf_mi, skip, is_inter)); | |
| 2486 | + let _ = (skip, is_inter); | |
| 2436 | 2487 | } |
| 2437 | 2488 | // Same write-back-once-from-the-last-leaf rule |
| 2438 | 2489 | // as the key frame search's `write_leaf8` |
| 2439 | 2490 | // caller (r15): the SUB-grid skip/inter arrays |
| 2440 | 2491 | // are otherwise left stale for the next |
| 2441 | 2492 | // 16x16 slot. |
| 2442 | - if let Some((_, skip, is_inter)) = prev_leaf { | |
| 2443 | - neighbours.record_inter(at16, SUB, skip, is_inter); | |
| 2444 | - } | |
| 2493 | + | |
| 2445 | 2494 | } |
| 2446 | 2495 | } |
| 2447 | 2496 | continue; |
| @@ -2451,13 +2500,14 @@ pub fn sb_coeff_inter_frame_tile( | ||
| 2451 | 2500 | let (r, c) = at; |
| 2452 | 2501 | let has_above = r32 > 0; |
| 2453 | 2502 | let has_left = c32 > 0; |
| 2454 | - let skip_ctx = | |
| 2455 | - usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]); | |
| 2503 | + let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); | |
| 2504 | + let skip_ctx = usize::from(neighbours.above_skip[mi_c]) | |
| 2505 | + + usize::from(neighbours.left_skip[mi_r]); | |
| 2456 | 2506 | enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); |
| 2457 | 2507 | |
| 2458 | 2508 | let is_inter = block.inter.is_some(); |
| 2459 | 2509 | let (above_inter, left_inter) = |
| 2460 | - (neighbours.above_inter[c], neighbours.left_inter[r]); | |
| 2510 | + (neighbours.above_inter[mi_c], neighbours.left_inter[mi_r]); | |
| 2461 | 2511 | let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); |
| 2462 | 2512 | enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]); |
| 2463 | 2513 | |
| @@ -2649,11 +2699,12 @@ fn write_inter_frame_leaf( | ||
| 2649 | 2699 | const LAST_FRAME: i8 = 1; |
| 2650 | 2700 | |
| 2651 | 2701 | let (r, c) = at; |
| 2652 | - let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]); | |
| 2702 | + let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); | |
| 2703 | + let skip_ctx = usize::from(neighbours.above_skip[mi_c]) + usize::from(neighbours.left_skip[mi_r]); | |
| 2653 | 2704 | enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); |
| 2654 | 2705 | |
| 2655 | 2706 | let (has_above, has_left) = (r > 0, c > 0); |
| 2656 | - let (above_inter, left_inter) = (neighbours.above_inter[c], neighbours.left_inter[r]); | |
| 2707 | + let (above_inter, left_inter) = (neighbours.above_inter[mi_c], neighbours.left_inter[mi_r]); | |
| 2657 | 2708 | let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); |
| 2658 | 2709 | let is_inter = block.inter.is_some(); |
| 2659 | 2710 | enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]); |
| @@ -2812,11 +2863,9 @@ fn write_inter_frame_leaf8( | ||
| 2812 | 2863 | mi_cols: u32, |
| 2813 | 2864 | mi_rows: u32, |
| 2814 | 2865 | block: &BlockCoeffs, |
| 2815 | - outer_at: (usize, usize), | |
| 2816 | 2866 | leaf_mi: (usize, usize), |
| 2817 | 2867 | scan8: &Vec<u16>, |
| 2818 | 2868 | scan4: &Vec<u16>, |
| 2819 | - prev_leaf: Option<((usize, usize), bool, bool)>, | |
| 2820 | 2869 | ) -> Result<(bool, bool)> { |
| 2821 | 2870 | if block.inter.is_none() && usize::from(block.mode) >= INTRA_MODES { |
| 2822 | 2871 | return Err(Error::unsupported( |
| @@ -2834,20 +2883,16 @@ fn write_inter_frame_leaf8( | ||
| 2834 | 2883 | /// branches. |
| 2835 | 2884 | const LAST_FRAME: i8 = 1; |
| 2836 | 2885 | |
| 2837 | - let (r, c) = outer_at; | |
| 2838 | - let mut above_skip = neighbours.above_skip[c]; | |
| 2839 | - let mut left_skip = neighbours.left_skip[r]; | |
| 2840 | - let mut above_inter = neighbours.above_inter[c]; | |
| 2841 | - let mut left_inter = neighbours.left_inter[r]; | |
| 2842 | - if let Some(((pr, pc), pskip, pinter)) = prev_leaf { | |
| 2843 | - if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { | |
| 2844 | - above_skip = pskip; | |
| 2845 | - above_inter = pinter; | |
| 2846 | - } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 { | |
| 2847 | - left_skip = pskip; | |
| 2848 | - left_inter = pinter; | |
| 2849 | - } | |
| 2850 | - } | |
| 2886 | + // This leaf's own 4x4 mode-info cells, not the enclosing 16x16 slot's: | |
| 2887 | + // under a real PARTITION_SPLIT all four leaves are coded, so the | |
| 2888 | + // bottom-left leaf's true above neighbour is the FIRST leaf and the | |
| 2889 | + // bottom-right's above and left are two DIFFERENT ones (lane-av1rd2 -- | |
| 2890 | + // reading the coarse slot desynced the stream, and so did an override | |
| 2891 | + // that remembered only the previous leaf). | |
| 2892 | + let above_skip = neighbours.above_skip[leaf_mi.1]; | |
| 2893 | + let left_skip = neighbours.left_skip[leaf_mi.0]; | |
| 2894 | + let above_inter = neighbours.above_inter[leaf_mi.1]; | |
| 2895 | + let left_inter = neighbours.left_inter[leaf_mi.0]; | |
| 2851 | 2896 | let skip_ctx = usize::from(above_skip) + usize::from(left_skip); |
| 2852 | 2897 | enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); |
| 2853 | 2898 | |
| @@ -2964,6 +3009,7 @@ fn write_inter_frame_leaf8( | ||
| 2964 | 3009 | vec![0i32; TX4 * TX4], |
| 2965 | 3010 | ]; |
| 2966 | 3011 | neighbours.record_mi(leaf_mi, 8, &zero_grids); |
| 3012 | + neighbours.record_inter_mi(leaf_mi, 8, block.skip, block.inter.is_some()); | |
| 2967 | 3013 | } else { |
| 2968 | 3014 | let grids = [ |
| 2969 | 3015 | level_grid(&block.luma, TX8)?, |
| @@ -2980,6 +3026,7 @@ fn write_inter_frame_leaf8( | ||
| 2980 | 3026 | mode_for_tx, |
| 2981 | 3027 | ); |
| 2982 | 3028 | neighbours.record_mi(leaf_mi, 8, &grids); |
| 3029 | + neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter); | |
| 2983 | 3030 | } |
| 2984 | 3031 | Ok((block.skip, is_inter)) |
| 2985 | 3032 | } |