forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1inter8': inter frames at 8x8-leaf straddle sizes

Browse files at this commit

b3bcc7e0d675e6632cfd2af6372b42776edec9f9Ahmet Kaan GümüşParent: 24e74ceaad8b59
Extends the mod-32==8 straddle path to inter frames end-to-end
(encoder, tile writer, decoder), and fixes the subpel filter selection
mc.rs shared with every consumer: blocks 4 wide or tall take the 4-tap
table. The 640x360 inter sequence round-trips through ffmpeg
pixel-exact.
crates/ec-av1/src/cdf.rsmodified+9 -0
@@ -1043,6 +1043,15 @@ pub const INTER_TX_TYPE_SET3_32: [u16; 3] = [748, 32768, 0];
10431043 /// DCT_DCT, index one of `Tx_Type_Inter_Inv_Set3`, same as the 32x32 table.
10441044 pub const INTER_TX_TYPE_SET3_16: [u16; 3] = [1998, 32768, 0];
10451045
1046+/// `Default_Inter_Ext_Tx_Cdf`'s `TX_SET_INTER_3` row for `TX_8X8` (spec 9.4):
1047+/// the transform type of an `is_inter` 8x8 luma transform under
1048+/// `reduced_tx_set` -- an 8x8 leaf under a straddling 16x16 block
1049+/// (lane-av1inter8), the same two-symbol set [`INTER_TX_TYPE_SET3_16`] and
1050+/// [`INTER_TX_TYPE_SET3_32`] read at their own sizes, just its own default
1051+/// probability and adaptation state (libaom `entropymode.c`'s
1052+/// `default_inter_ext_tx_cdf[EXT_TX_SET_DCT_IDTX][TX_8X8]`).
1053+pub const INTER_TX_TYPE_SET3_8: [u16; 3] = [4167, 32768, 0];
1054+
10461055 // The inter-frame tables below (spec 9.4) support the syntax an inter block
10471056 // needs: whether it is coded as intra at all, which reference frame and
10481057 // motion vector it takes when it is not. Compound reference and compound
crates/ec-av1/src/cdf_state.rsmodified+29 -2
@@ -37,9 +37,20 @@ pub(crate) enum TxbSet {
3737 /// reads, but its own two-symbol `tx_type` table rather than `Luma16`'s
3838 /// five-symbol, mode-indexed one.
3939 Luma16Inter,
40- /// The 8x8 luma transform of an 8x8 leaf under a straddling 16x16 block
41- /// (lane-av1-rect): intra-only, so no `Luma8Inter` counterpart exists yet.
40+ /// The 8x8 luma transform of an intra 8x8 leaf under a straddling 16x16
41+ /// block (lane-av1-rect). An `is_inter` leaf reads
42+ /// [`Luma8Inter`](TxbSet::Luma8Inter) instead.
4243 Luma8,
44+ /// The 8x8 luma transform of an `is_inter` 8x8 leaf under a straddling
45+ /// 16x16 block (lane-av1inter8): the same coefficient tables
46+ /// [`Luma8`](TxbSet::Luma8) reads, but `get_tx_set` (spec 5.11.48)
47+ /// returns `TX_SET_INTER_3` at this size too under this encoder's
48+ /// always-on `reduced_tx_set` -- the same two-symbol alphabet
49+ /// [`Luma16Inter`](TxbSet::Luma16Inter) carries, just its own CDF (libaom
50+ /// `default_inter_ext_tx_cdf[EXT_TX_SET_DCT_IDTX][TX_8X8]`, a distinct
51+ /// adapted table from the 16x16 and 32x32 sizes' own entries in the same
52+ /// set, per `entropymode.c`).
53+ Luma8Inter,
4354 /// The 8x8 transform of a chroma plane of a 16x16 block.
4455 Chroma8,
4556 /// The 4x4 transform of a chroma plane of an 8x8 leaf (lane-av1-rect).
@@ -217,6 +228,10 @@ pub(crate) struct Cdfs {
217228 /// `reduced_tx_set` (spec `TX_SET_INTER_3`, `TX_16X16` row) -- not
218229 /// indexed by mode, same as [`Self::inter_tx_type_32`].
219230 pub inter_tx_type_16: [u16; 3],
231+ /// The transform type of an `is_inter` 8x8 luma transform under
232+ /// `reduced_tx_set` (spec `TX_SET_INTER_3`, `TX_8X8` row) -- not indexed
233+ /// by mode, same as [`Self::inter_tx_type_32`] (lane-av1inter8).
234+ pub inter_tx_type_8: [u16; 3],
220235 /// The DC sign of a chroma coefficient, shared by both chroma sets.
221236 pub dc_sign_chroma: [[u16; 3]; 3],
222237 /// Whether an inter frame's block is coded as intra.
@@ -638,6 +653,7 @@ impl Cdfs {
638653 intra_tx_type_8: cdf::INTRA_TX_TYPE_SET2_8,
639654 inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32,
640655 inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16,
656+ inter_tx_type_8: cdf::INTER_TX_TYPE_SET3_8,
641657 dc_sign_chroma: cdf::DC_SIGN_CHROMA,
642658 intra_inter: cdf::INTRA_INTER,
643659 single_ref: cdf::SINGLE_REF,
@@ -722,6 +738,17 @@ impl Cdfs {
722738 dc_sign: &mut self.dc_sign_luma,
723739 tx_type: Some(self.intra_tx_type_8[mode].as_mut_slice()),
724740 },
741+ TxbSet::Luma8Inter => TxbTables {
742+ side: 8,
743+ txb_skip: &mut self.txb_skip_luma_8,
744+ eob_pt: &mut self.eob_pt_64_luma,
745+ eob_extra: &mut self.eob_extra_luma_8,
746+ base: &mut self.base_luma_8,
747+ base_eob: &mut self.base_eob_luma_8,
748+ br: &mut self.br_luma_8,
749+ dc_sign: &mut self.dc_sign_luma,
750+ tx_type: Some(self.inter_tx_type_8.as_mut_slice()),
751+ },
725752 TxbSet::Chroma8 => TxbTables {
726753 side: 8,
727754 txb_skip: &mut self.txb_skip_chroma_8,
crates/ec-av1/src/decode.rsmodified+456 -33
@@ -2353,6 +2353,371 @@ fn decode_inter_block(
23532353 Ok(())
23542354 }
23552355
2356+/// Decodes one 8x8 leaf of a straddling 16x16 inter-frame block
2357+/// (lane-av1inter8), mirroring [`crate::tile::write_inter_frame_leaf8`]'s
2358+/// exact symbol order: its own skip flag, intra/inter choice and (when
2359+/// inter) `NEARESTMV`/`NEWMV` chain against this leaf's own 2x2-mi mv-stack
2360+/// window, or (when intra) `Y_MODE`, then its own luma and two chroma
2361+/// transform blocks through [`TxbSet::Luma8Inter`]/[`TxbSet::Chroma4`],
2362+/// same as [`decode_leaf8`]'s coefficient layer but with
2363+/// motion-compensated prediction instead of intra prediction when inter.
2364+/// `outer_at` (in [`SUB`]-grid units) is the enclosing 16x16 slot's
2365+/// skip/intra-inter context, overridden by `prev_leaf` exactly as
2366+/// [`decode_leaf8`] overrides mode context: `Neighbours`' above/left arrays
2367+/// only resolve to [`SUB`] granularity. Hands back this leaf's own skip flag
2368+/// and intra/inter choice, for the next leaf or the caller's final
2369+/// write-back.
2370+#[allow(clippy::too_many_arguments)]
2371+fn decode_inter_block8(
2372+ dec: &mut SymbolDecoder,
2373+ cdfs: &mut Cdfs,
2374+ neighbours: &mut Neighbours,
2375+ grid: &mut MiGrid,
2376+ mi_cols: u32,
2377+ mi_rows: u32,
2378+ outer_at: (usize, usize),
2379+ leaf_mi: (usize, usize),
2380+ y: &mut PlaneBuf,
2381+ u: &mut PlaneBuf,
2382+ v: &mut PlaneBuf,
2383+ ref_y: &PlaneBuf,
2384+ ref_u: &PlaneBuf,
2385+ ref_v: &PlaneBuf,
2386+ base_q_idx: u8,
2387+ scan8: &[u16],
2388+ scan4: &[u16],
2389+ prev_leaf: Option<((usize, usize), bool, bool)>,
2390+) -> Result<(bool, bool)> {
2391+ const LAST_FRAME: i8 = 1;
2392+ /// `Y_MODE`'s size group (`common_data.h`'s `size_group_lookup[BLOCK_8X8]`).
2393+ const SIZE_GROUP_8: usize = 1;
2394+
2395+ let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
2396+ let (cpx, cpy) = (px / 2, py / 2);
2397+ const SIDE: usize = 8;
2398+ const CHROMA_SIDE: usize = 4;
2399+
2400+ let (r, c) = outer_at;
2401+ let mut above_skip = neighbours.above_skip[c];
2402+ let mut left_skip = neighbours.left_skip[r];
2403+ let mut above_inter = neighbours.above_inter[c];
2404+ let mut left_inter = neighbours.left_inter[r];
2405+ if let Some(((pr, pc), pskip, pinter)) = prev_leaf {
2406+ if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 {
2407+ above_skip = pskip;
2408+ above_inter = pinter;
2409+ } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 {
2410+ left_skip = pskip;
2411+ left_inter = pinter;
2412+ }
2413+ }
2414+ let skip_ctx = usize::from(above_skip) + usize::from(left_skip);
2415+ let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) == 1;
2416+
2417+ let (has_above, has_left) = (leaf_mi.0 > 0, leaf_mi.1 > 0);
2418+ let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);
2419+ let is_inter = dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1;
2420+
2421+ let mode_for_tx;
2422+ let (luma_grid, u_grid, v_grid);
2423+ if is_inter {
2424+ let sr_ctx = single_ref_ctx(above_inter || left_inter);
2425+ let p1 = dec.symbol(&mut cdfs.single_ref[sr_ctx][0]);
2426+ let p3 = dec.symbol(&mut cdfs.single_ref[sr_ctx][2]);
2427+ let p4 = dec.symbol(&mut cdfs.single_ref[sr_ctx][3]);
2428+ if (p1, p3, p4) != (0, 0, 0) {
2429+ return Err(unsupported(
2430+ "a reference frame other than LAST_FRAME (round 2)",
2431+ ));
2432+ }
2433+
2434+ let (mi_row, mi_col) = leaf_mi;
2435+ let stack = find_mv_stack(
2436+ grid,
2437+ mi_row,
2438+ mi_col,
2439+ 2,
2440+ 2,
2441+ LAST_FRAME,
2442+ mi_cols as usize,
2443+ mi_rows as usize,
2444+ );
2445+
2446+ let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1;
2447+ let (mv, is_new_mv) = if !not_new {
2448+ if stack.entries.len() > 1 {
2449+ dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[0]]);
2450+ }
2451+ (
2452+ read_mv(dec, &mut cdfs.mv_comp, &mut cdfs.mv_joint, stack.pred_mv),
2453+ true,
2454+ )
2455+ } else {
2456+ let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1;
2457+ if !not_zero {
2458+ return Err(unsupported("GLOBALMV (round 3)"));
2459+ }
2460+ let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0;
2461+ let mv = if nearest {
2462+ stack.nearest_mv
2463+ } else {
2464+ let mut idx = 1usize;
2465+ while idx < 3 && stack.entries.len() > idx + 1 {
2466+ if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 {
2467+ break;
2468+ }
2469+ idx += 1;
2470+ }
2471+ stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv)
2472+ };
2473+ (mv, false)
2474+ };
2475+ for dr in 0..2 {
2476+ for dc in 0..2 {
2477+ grid.set(
2478+ mi_row + dr,
2479+ mi_col + dc,
2480+ MiInfo {
2481+ is_inter: true,
2482+ ref_frame: LAST_FRAME,
2483+ mv,
2484+ is_new_mv,
2485+ size: 2,
2486+ },
2487+ );
2488+ }
2489+ }
2490+ mode_for_tx = 0;
2491+
2492+ let mut pred_y = vec![0u8; SIDE * SIDE];
2493+ mc::predict(
2494+ &ref_y.data,
2495+ ref_y.width,
2496+ ref_y.true_width,
2497+ ref_y.true_height,
2498+ mv_to_q4(px, mv.1, true),
2499+ mv_to_q4(py, mv.0, true),
2500+ SIDE,
2501+ SIDE,
2502+ &mut pred_y,
2503+ );
2504+ let mut pred_u = vec![0u8; CHROMA_SIDE * CHROMA_SIDE];
2505+ mc::predict(
2506+ &ref_u.data,
2507+ ref_u.width,
2508+ ref_u.true_width,
2509+ ref_u.true_height,
2510+ mv_to_q4(cpx, mv.1, false),
2511+ mv_to_q4(cpy, mv.0, false),
2512+ CHROMA_SIDE,
2513+ CHROMA_SIDE,
2514+ &mut pred_u,
2515+ );
2516+ let mut pred_v = vec![0u8; CHROMA_SIDE * CHROMA_SIDE];
2517+ mc::predict(
2518+ &ref_v.data,
2519+ ref_v.width,
2520+ ref_v.true_width,
2521+ ref_v.true_height,
2522+ mv_to_q4(cpx, mv.1, false),
2523+ mv_to_q4(cpy, mv.0, false),
2524+ CHROMA_SIDE,
2525+ CHROMA_SIDE,
2526+ &mut pred_v,
2527+ );
2528+
2529+ if skip {
2530+ y.reconstruct_mc(px, py, SIDE, &pred_y, &vec![0i32; SIDE * SIDE]);
2531+ u.reconstruct_mc(
2532+ cpx,
2533+ cpy,
2534+ CHROMA_SIDE,
2535+ &pred_u,
2536+ &vec![0i32; CHROMA_SIDE * CHROMA_SIDE],
2537+ );
2538+ v.reconstruct_mc(
2539+ cpx,
2540+ cpy,
2541+ CHROMA_SIDE,
2542+ &pred_v,
2543+ &vec![0i32; CHROMA_SIDE * CHROMA_SIDE],
2544+ );
2545+ luma_grid = vec![0i32; SIDE * SIDE];
2546+ u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
2547+ v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
2548+ } else {
2549+ let around = neighbours.around_mi(leaf_mi, 8);
2550+ luma_grid = read_inter_plane(
2551+ dec,
2552+ cdfs,
2553+ TxbSet::Luma8Inter,
2554+ scan8,
2555+ 0,
2556+ around[0],
2557+ mode_for_tx,
2558+ y,
2559+ px,
2560+ py,
2561+ SIDE,
2562+ base_q_idx,
2563+ &pred_y,
2564+ )?;
2565+ u_grid = read_inter_plane(
2566+ dec,
2567+ cdfs,
2568+ TxbSet::Chroma4,
2569+ scan4,
2570+ 1,
2571+ around[1],
2572+ mode_for_tx,
2573+ u,
2574+ cpx,
2575+ cpy,
2576+ CHROMA_SIDE,
2577+ base_q_idx,
2578+ &pred_u,
2579+ )?;
2580+ v_grid = read_inter_plane(
2581+ dec,
2582+ cdfs,
2583+ TxbSet::Chroma4,
2584+ scan4,
2585+ 2,
2586+ around[2],
2587+ mode_for_tx,
2588+ v,
2589+ cpx,
2590+ cpy,
2591+ CHROMA_SIDE,
2592+ base_q_idx,
2593+ &pred_v,
2594+ )?;
2595+ }
2596+ } else {
2597+ let mode = dec.symbol(&mut cdfs.y_mode[SIZE_GROUP_8]);
2598+ if mode >= 13 {
2599+ return Err(unsupported(
2600+ "an intra mode this decoder does not code (round 2)",
2601+ ));
2602+ }
2603+ if (V_PRED..=D67_PRED).contains(&mode) {
2604+ let angle = dec.symbol(&mut cdfs.angle_delta[mode - V_PRED]);
2605+ if angle != ANGLE_DELTA_ZERO {
2606+ return Err(unsupported(
2607+ "a nonzero angle delta (this encoder never writes one)",
2608+ ));
2609+ }
2610+ }
2611+ let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]);
2612+ if uv_mode != DC_PRED {
2613+ return Err(unsupported(
2614+ "a non-DC chroma mode on an 8x8 inter-frame leaf (this encoder never writes one)",
2615+ ));
2616+ }
2617+ mode_for_tx = mode;
2618+ let (mi_row, mi_col) = leaf_mi;
2619+ for dr in 0..2 {
2620+ for dc in 0..2 {
2621+ grid.set(
2622+ mi_row + dr,
2623+ mi_col + dc,
2624+ MiInfo {
2625+ is_inter: false,
2626+ ref_frame: -1,
2627+ mv: (0, 0),
2628+ is_new_mv: false,
2629+ size: 2,
2630+ },
2631+ );
2632+ }
2633+ }
2634+
2635+ let reach = Reach::of(SIDE, px, py, y.width, y.height);
2636+ if skip {
2637+ y.reconstruct(px, py, SIDE, mode, reach, &vec![0i32; SIDE * SIDE], None);
2638+ u.reconstruct(
2639+ cpx,
2640+ cpy,
2641+ CHROMA_SIDE,
2642+ DC_PRED,
2643+ Reach::none(),
2644+ &vec![0i32; CHROMA_SIDE * CHROMA_SIDE],
2645+ None,
2646+ );
2647+ v.reconstruct(
2648+ cpx,
2649+ cpy,
2650+ CHROMA_SIDE,
2651+ DC_PRED,
2652+ Reach::none(),
2653+ &vec![0i32; CHROMA_SIDE * CHROMA_SIDE],
2654+ None,
2655+ );
2656+ luma_grid = vec![0i32; SIDE * SIDE];
2657+ u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
2658+ v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
2659+ } else {
2660+ let around = neighbours.around_mi(leaf_mi, 8);
2661+ luma_grid = read_plane(
2662+ dec,
2663+ cdfs,
2664+ TxbSet::Luma8,
2665+ scan8,
2666+ 0,
2667+ around[0],
2668+ mode,
2669+ mode,
2670+ reach,
2671+ y,
2672+ px,
2673+ py,
2674+ SIDE,
2675+ TX8,
2676+ base_q_idx,
2677+ None,
2678+ )?;
2679+ u_grid = read_plane(
2680+ dec,
2681+ cdfs,
2682+ TxbSet::Chroma4,
2683+ scan4,
2684+ 1,
2685+ around[1],
2686+ mode,
2687+ DC_PRED,
2688+ Reach::none(),
2689+ u,
2690+ cpx,
2691+ cpy,
2692+ CHROMA_SIDE,
2693+ TX4,
2694+ base_q_idx,
2695+ None,
2696+ )?;
2697+ v_grid = read_plane(
2698+ dec,
2699+ cdfs,
2700+ TxbSet::Chroma4,
2701+ scan4,
2702+ 2,
2703+ around[2],
2704+ mode,
2705+ DC_PRED,
2706+ Reach::none(),
2707+ v,
2708+ cpx,
2709+ cpy,
2710+ CHROMA_SIDE,
2711+ TX4,
2712+ base_q_idx,
2713+ None,
2714+ )?;
2715+ }
2716+ }
2717+ neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);
2718+ Ok((skip, is_inter))
2719+}
2720+
23562721 /// Decodes the payload [`crate::tile::sb_coeff_inter_frame_tile`] writes,
23572722 /// against `reference` (the previous frame's own decoded picture, at this
23582723 /// frame's true size -- a decoder's single-slot DPB, matching what this
@@ -2436,6 +2801,7 @@ pub fn decode_inter_frame_tile(
24362801 let scan32 = default_scan(TX32);
24372802 let scan16 = default_scan(TX16);
24382803 let scan8 = default_scan(TX8);
2804+ let scan4 = default_scan(TX4);
24392805
24402806 let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx));
24412807 let mut dec = SymbolDecoder::new(data);
@@ -2532,44 +2898,101 @@ pub fn decode_inter_frame_tile(
25322898 has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols),
25332899 has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows),
25342900 );
2535- if !has_cols16 || !has_rows16 {
2901+ if !has_cols16 && !has_rows16 {
25362902 return Err(unsupported(
2537- "a 16x16 inter block the true frame edge cuts through (round 2)",
2903+ "a 16x16 inter block whose true edge cuts through both \
2904+ axes needs a rectangular transform this decoder does \
2905+ not code yet",
25382906 ));
25392907 }
25402908 let at16 = (sr, sc);
2541- let ctx16 = neighbours.partition_ctx(at16, SUB);
2542- let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]);
2543- if part16 != PARTITION_NONE {
2544- return Err(unsupported(
2545- "a partition below 16x16 (this encoder never writes one)",
2546- ));
2909+ if has_cols16 && has_rows16 {
2910+ let ctx16 = neighbours.partition_ctx(at16, SUB);
2911+ let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]);
2912+ if part16 != PARTITION_NONE {
2913+ return Err(unsupported(
2914+ "a partition below 16x16 (this encoder never writes one)",
2915+ ));
2916+ }
2917+ decode_inter_block(
2918+ &mut dec,
2919+ &mut cdfs,
2920+ &mut neighbours,
2921+ &mut grid,
2922+ at16,
2923+ SUB,
2924+ mi_cols,
2925+ mi_rows,
2926+ &mut y,
2927+ &mut u,
2928+ &mut v,
2929+ &ref_y,
2930+ &ref_u,
2931+ &ref_v,
2932+ base_q_idx,
2933+ TxbSet::Luma16,
2934+ TxbSet::Luma16Inter,
2935+ TxbSet::Chroma8,
2936+ TX16,
2937+ TX8,
2938+ &scan16,
2939+ &scan8,
2940+ 2,
2941+ )?;
2942+ } else {
2943+ let ctx16 = neighbours.partition_ctx(at16, SUB);
2944+ if has_cols16 {
2945+ dec.symbol_fixed(&gather(
2946+ &cdfs.partition_w16[ctx16],
2947+ VERT_ALIKE,
2948+ ));
2949+ } else {
2950+ dec.symbol_fixed(&gather(
2951+ &cdfs.partition_w16[ctx16],
2952+ HORZ_ALIKE,
2953+ ));
2954+ }
2955+ let (mi_row0, mi_col0) = (sr as u32 * SUB_MI, sc as u32 * SUB_MI);
2956+ let leaf_positions: Vec<(u32, u32)> = (0..4)
2957+ .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2))
2958+ .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols)
2959+ .collect();
2960+ let mut prev_leaf: Option<((usize, usize), bool, bool)> = None;
2961+ for (mr, mc) in leaf_positions {
2962+ let leaf_mi = (mr as usize, mc as usize);
2963+ let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8);
2964+ let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]);
2965+ if part8 != PARTITION_NONE {
2966+ return Err(unsupported(
2967+ "a partition below 8x8 (this encoder never writes one)",
2968+ ));
2969+ }
2970+ let (skip, is_inter) = decode_inter_block8(
2971+ &mut dec,
2972+ &mut cdfs,
2973+ &mut neighbours,
2974+ &mut grid,
2975+ mi_cols,
2976+ mi_rows,
2977+ at16,
2978+ leaf_mi,
2979+ &mut y,
2980+ &mut u,
2981+ &mut v,
2982+ &ref_y,
2983+ &ref_u,
2984+ &ref_v,
2985+ base_q_idx,
2986+ &scan8,
2987+ &scan4,
2988+ prev_leaf,
2989+ )?;
2990+ prev_leaf = Some((leaf_mi, skip, is_inter));
2991+ }
2992+ if let Some((_, skip, is_inter)) = prev_leaf {
2993+ neighbours.record_inter(at16, SUB, skip, is_inter);
2994+ }
25472995 }
2548- decode_inter_block(
2549- &mut dec,
2550- &mut cdfs,
2551- &mut neighbours,
2552- &mut grid,
2553- at16,
2554- SUB,
2555- mi_cols,
2556- mi_rows,
2557- &mut y,
2558- &mut u,
2559- &mut v,
2560- &ref_y,
2561- &ref_u,
2562- &ref_v,
2563- base_q_idx,
2564- TxbSet::Luma16,
2565- TxbSet::Luma16Inter,
2566- TxbSet::Chroma8,
2567- TX16,
2568- TX8,
2569- &scan16,
2570- &scan8,
2571- 2,
2572- )?;
25732996 }
25742997 }
25752998 _ => {
crates/ec-av1/src/encode.rsmodified+201 -97
@@ -1162,26 +1162,39 @@ fn code_square(
11621162 )
11631163 }
11641164
1165-/// Codes one 16x16 leaf of a straddling 32x32 quadrant as whichever costs
1166-/// least of intra ([`code_square`], unchanged) or `NEARESTMV` against
1167-/// `reference` at `stack.nearest_mv` -- the same NEARESTMV candidate
1168-/// [`search_inter_block`] prices for a whole 32x32 block, just at this leaf's
1169-/// own 16x16 side and through [`TxbSet::Luma16Inter`]. `NEWMV` (a motion
1170-/// search of its own) is not attempted here: these leaves only ever fill the
1171-/// sliver of real content a true edge lands in the middle of a 32x32 block,
1172-/// not a leaf worth a full search, so `NEARESTMV`-or-intra is this function's
1173-/// deliberate first cut (see `write_inter_frame_leaf`'s doc comment).
1165+/// Codes one leaf of a straddling quadrant as whichever costs least of intra
1166+/// ([`code_square`], unchanged) or `NEARESTMV` against `reference` at
1167+/// `stack.nearest_mv` -- the same NEARESTMV candidate [`search_inter_block`]
1168+/// prices for a whole 32x32 block, just at this leaf's own `side` (16, for a
1169+/// 16x16 leaf under a straddling 32x32 quadrant, or 8, for an 8x8 leaf under
1170+/// a straddling 16x16 -- lane-av1inter8) and through [`TxbSet::Luma16Inter`]
1171+/// or [`TxbSet::Luma8Inter`]. `NEWMV` (a motion search of its own) is not
1172+/// attempted here: these leaves only ever fill the sliver of real content a
1173+/// true edge lands in the middle of a larger block, not a leaf worth a full
1174+/// search, so `NEARESTMV`-or-intra is this function's deliberate first cut
1175+/// (see `write_inter_frame_leaf`'s doc comment).
11741176 #[allow(clippy::too_many_arguments)]
11751177 fn code_square_inter(
11761178 luma: &mut Plane,
11771179 chroma: &mut [Plane; 2],
11781180 (x, y): (usize, usize),
1181+ side: usize,
11791182 search: &Search,
11801183 mode_bits: &[f64; 13],
11811184 reference: &Picture,
11821185 stack: &MvStack,
11831186 ) -> BlockCoeffs {
1184- let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), SUB, search, mode_bits);
1187+ let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits);
1188+ let luma_set = if side == 8 {
1189+ TxbSet::Luma8Inter
1190+ } else {
1191+ TxbSet::Luma16Inter
1192+ };
1193+ let chroma_set = if side == 8 {
1194+ TxbSet::Chroma4
1195+ } else {
1196+ TxbSet::Chroma8
1197+ };
11851198
11861199 let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip));
11871200 let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter));
@@ -1215,7 +1228,7 @@ fn code_square_inter(
12151228 luma,
12161229 x,
12171230 y,
1218- SUB,
1231+ side,
12191232 mv,
12201233 true,
12211234 ref_luma.0,
@@ -1225,13 +1238,13 @@ fn code_square_inter(
12251238 false,
12261239 search.base_q_idx,
12271240 search.deadzone,
1228- TxbSet::Luma16Inter,
1241+ luma_set,
12291242 );
12301243 let u = mc_trial(
12311244 &chroma[0],
12321245 x / 2,
12331246 y / 2,
1234- SUB / 2,
1247+ side / 2,
12351248 mv,
12361249 false,
12371250 ref_u.0,
@@ -1241,13 +1254,13 @@ fn code_square_inter(
12411254 false,
12421255 search.base_q_idx,
12431256 search.deadzone,
1244- TxbSet::Chroma8,
1257+ chroma_set,
12451258 );
12461259 let v = mc_trial(
12471260 &chroma[1],
12481261 x / 2,
12491262 y / 2,
1250- SUB / 2,
1263+ side / 2,
12511264 mv,
12521265 false,
12531266 ref_v.0,
@@ -1257,7 +1270,7 @@ fn code_square_inter(
12571270 false,
12581271 search.base_q_idx,
12591272 search.deadzone,
1260- TxbSet::Chroma8,
1273+ chroma_set,
12611274 );
12621275 let skip = luma_trial.levels.iter().all(|&l| l == 0)
12631276 && u.levels.iter().all(|&l| l == 0)
@@ -1291,13 +1304,13 @@ fn code_square_inter(
12911304 // branches), which is what made 8-mi geometry a true no-op instead of
12921305 // one that only held when every neighbour happened to be inter.
12931306 if inter_cost < intra_cost {
1294- luma.commit(x, y, SUB, &luma_trial);
1295- chroma[0].commit(x / 2, y / 2, SUB / 2, &u);
1296- chroma[1].commit(x / 2, y / 2, SUB / 2, &v);
1307+ luma.commit(x, y, side, &luma_trial);
1308+ chroma[0].commit(x / 2, y / 2, side / 2, &u);
1309+ chroma[1].commit(x / 2, y / 2, side / 2, &v);
12971310 BlockCoeffs {
1298- luma: coeffs(&luma_trial.levels, SUB),
1299- u: coeffs(&u.levels, SUB / 2),
1300- v: coeffs(&v.levels, SUB / 2),
1311+ luma: coeffs(&luma_trial.levels, side),
1312+ u: coeffs(&u.levels, side / 2),
1313+ v: coeffs(&v.levels, side / 2),
13011314 mode: DC_PRED as u8,
13021315 skip,
13031316 eight: None,
@@ -2606,75 +2619,139 @@ pub(crate) fn encode_inter_frame(
26062619 && (sc as u32) * crate::tile::SUB_MI < header.mi_cols
26072620 })
26082621 .collect();
2609- // A 16x16 leaf the true edge itself cuts through needs a
2610- // rectangular (HORZ/VERT) transform this writer does not
2611- // code, same refusal as the key frame search's
2612- // `split_legal` check.
2613- if let Some(&(sr, sc)) = sub_positions.iter().find(|&&(sr, sc)| {
2614- !crate::tile::has_half(
2615- sc as u32 * crate::tile::SUB_MI,
2616- crate::tile::SUB_MI,
2617- header.mi_cols,
2618- ) || !crate::tile::has_half(
2619- sr as u32 * crate::tile::SUB_MI,
2620- crate::tile::SUB_MI,
2621- header.mi_rows,
2622- )
2623- }) {
2624- return Err(Error::unsupported(
2625- "AV1 encode",
2626- format!(
2627- "the 16x16 block at ({sc},{sr}) straddles the true frame \
2628- edge in a way that needs a rectangular (HORZ/VERT) \
2629- transform this encoder does not code yet"
2630- ),
2631- ));
2632- }
26332622 // Each leaf searches intra against its own `NEARESTMV`
26342623 // candidate (`code_square_inter`), same real-inter choice
26352624 // `search_inter_block` makes for a whole 32x32 block, at
2636- // this leaf's own 4x4-mi mv-stack window.
2625+ // this leaf's own 4x4-mi mv-stack window -- unless the
2626+ // leaf's own half itself straddles the true edge, which
2627+ // it codes as two (or one, at a true corner) 8x8 leaves
2628+ // (`crate::tile::write_leaf8`'s inter counterpart),
2629+ // mirroring the key frame search's straddling-16x16
2630+ // handling above (lane-av1inter8).
26372631 let mut leaves = Vec::with_capacity(sub_positions.len());
26382632 for (sr, sc) in sub_positions {
2639- let (x, y) = (sc * SUB, sr * SUB);
2640- let (mi_row, mi_col) = (sr * 4, sc * 4);
2641- let stack = find_mv_stack(
2642- &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows,
2643- );
2644- let block = code_square_inter(
2645- &mut luma,
2646- &mut chroma,
2647- (x, y),
2648- &search,
2649- &mode_bits_table,
2650- reference,
2651- &stack,
2633+ let (has_cols16, has_rows16) = (
2634+ crate::tile::has_half(
2635+ sc as u32 * crate::tile::SUB_MI,
2636+ crate::tile::SUB_MI,
2637+ header.mi_cols,
2638+ ),
2639+ crate::tile::has_half(
2640+ sr as u32 * crate::tile::SUB_MI,
2641+ crate::tile::SUB_MI,
2642+ header.mi_rows,
2643+ ),
26522644 );
2653- for dr in 0..4 {
2654- for dc in 0..4 {
2655- grid.set(
2656- mi_row + dr,
2657- mi_col + dc,
2658- match block.inter {
2659- Some(info) => MiInfo {
2660- is_inter: true,
2661- ref_frame: LAST_FRAME,
2662- mv: info.mv,
2663- is_new_mv: matches!(info.mode, InterMode::NewMv),
2664- size: 4,
2665- },
2666- None => MiInfo {
2667- is_inter: false,
2668- ref_frame: -1,
2669- mv: (0, 0),
2670- is_new_mv: false,
2671- size: 4,
2645+ if !has_cols16 && !has_rows16 {
2646+ return Err(Error::unsupported(
2647+ "AV1 encode",
2648+ format!(
2649+ "the 16x16 block at ({sc},{sr}) straddles the true \
2650+ frame edge in a way that needs a rectangular \
2651+ (HORZ/VERT) transform this encoder does not code yet"
2652+ ),
2653+ ));
2654+ }
2655+ if has_cols16 && has_rows16 {
2656+ let (x, y) = (sc * SUB, sr * SUB);
2657+ let (mi_row, mi_col) = (sr * 4, sc * 4);
2658+ let stack = find_mv_stack(
2659+ &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows,
2660+ );
2661+ let block = code_square_inter(
2662+ &mut luma,
2663+ &mut chroma,
2664+ (x, y),
2665+ SUB,
2666+ &search,
2667+ &mode_bits_table,
2668+ reference,
2669+ &stack,
2670+ );
2671+ for dr in 0..4 {
2672+ for dc in 0..4 {
2673+ grid.set(
2674+ mi_row + dr,
2675+ mi_col + dc,
2676+ match block.inter {
2677+ Some(info) => MiInfo {
2678+ is_inter: true,
2679+ ref_frame: LAST_FRAME,
2680+ mv: info.mv,
2681+ is_new_mv: matches!(info.mode, InterMode::NewMv),
2682+ size: 4,
2683+ },
2684+ None => MiInfo {
2685+ is_inter: false,
2686+ ref_frame: -1,
2687+ mv: (0, 0),
2688+ is_new_mv: false,
2689+ size: 4,
2690+ },
26722691 },
2673- },
2692+ );
2693+ }
2694+ }
2695+ leaves.push(block);
2696+ } else {
2697+ let (x_sub, y_sub) = (sc * SUB, sr * SUB);
2698+ let (mi_row0, mi_col0) = (sr * 4, sc * 4);
2699+ let mut eight = Vec::with_capacity(2);
2700+ for i in 0..4 {
2701+ let leaf_x = x_sub + (i % 2) * 8;
2702+ let leaf_y = y_sub + (i / 2) * 8;
2703+ if leaf_x >= luma.true_width || leaf_y >= luma.true_height {
2704+ continue;
2705+ }
2706+ let (mi_row, mi_col) =
2707+ (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2);
2708+ let stack = find_mv_stack(
2709+ &grid, mi_row, mi_col, 2, 2, LAST_FRAME, mi_cols, mi_rows,
2710+ );
2711+ let leaf = code_square_inter(
2712+ &mut luma,
2713+ &mut chroma,
2714+ (leaf_x, leaf_y),
2715+ 8,
2716+ &search,
2717+ &mode_bits_table,
2718+ reference,
2719+ &stack,
26742720 );
2721+ for dr in 0..2 {
2722+ for dc in 0..2 {
2723+ grid.set(
2724+ mi_row + dr,
2725+ mi_col + dc,
2726+ match leaf.inter {
2727+ Some(info) => MiInfo {
2728+ is_inter: true,
2729+ ref_frame: LAST_FRAME,
2730+ mv: info.mv,
2731+ is_new_mv: matches!(
2732+ info.mode,
2733+ InterMode::NewMv
2734+ ),
2735+ size: 2,
2736+ },
2737+ None => MiInfo {
2738+ is_inter: false,
2739+ ref_frame: -1,
2740+ mv: (0, 0),
2741+ is_new_mv: false,
2742+ size: 2,
2743+ },
2744+ },
2745+ );
2746+ }
2747+ }
2748+ eight.push(leaf);
26752749 }
2750+ leaves.push(BlockCoeffs {
2751+ eight: Some(eight),
2752+ ..BlockCoeffs::default()
2753+ });
26762754 }
2677- leaves.push(block);
26782755 }
26792756 blocks[r32 * cols + c32] = Quadrant::Split(leaves);
26802757 }
@@ -3317,29 +3394,26 @@ mod tests {
33173394
33183395 /// 640x360 is the class the charter names: 360 mod 32 == 8, so a 16x16
33193396 /// leaf's own half (`has_half` at the SUB level) itself straddles the
3320- /// true edge on one axis only. lane-av1-rect r7 wires the key frame
3397+ /// true edge on one axis only. lane-av1-rect r7 wired the key frame
33213398 /// writer's 8x8-leaf path (`crate::tile::write_leaf8`) live for exactly
3322- /// this case, so the key frame path no longer *refuses* it; the inter
3323- /// frame path (`sb_coeff_inter_frame_tile`) was not part of this round's
3324- /// wiring and still has no leaf-8 generation, so it must still refuse.
3325- /// This only proves the encoder does not error out -- the leaf-8 stream
3326- /// it produces is NOT yet proven decoder-clean (see
3327- /// `a_single_axis_straddle_round_trips_through_ffmpeg`, currently
3328- /// `#[ignore]`d on a genuine dav1d desync at the smaller 40x32 case).
3399+ /// this case; lane-av1inter8 extends the same leaf-8 path to inter
3400+ /// frames (`code_square_inter` at `side == 8`, `sb_coeff_inter_frame_tile`'s
3401+ /// straddling-16x16 branch), so an inter frame no longer refuses this
3402+ /// size either. This only proves the encoder does not error out -- see
3403+ /// `a_640x360_half_straddle_sequence_round_trips_through_ffmpeg` for the
3404+ /// decoder-clean proof (a 3-frame sequence, so the inter path is
3405+ /// actually exercised).
33293406 #[test]
3330- fn key_frame_encodes_the_inter_frame_still_refuses_at_half_straddle_size() {
3407+ fn key_frame_and_inter_frame_both_encode_at_half_straddle_size() {
33313408 let (width, height) = (640usize, 360usize);
33323409 let picture = Picture::grey(width, height);
33333410 encode_key_frame(&picture, 100, 0.5).expect("key frame now codes the mod-32==8 straddle");
33343411
3335- // `encode_sequence`'s second frame is the inter path: same size,
3336- // still refused -- that machinery was not wired this round.
3412+ // `encode_sequence`'s second frame is the inter path: same size, now
3413+ // wired the same way.
33373414 let pictures = vec![picture.clone(), picture];
3338- let inter_err = encode_sequence(&pictures, 100, 0.5).unwrap_err();
3339- assert!(
3340- inter_err.to_string().contains("rectangular"),
3341- "inter frame refusal: {inter_err}"
3342- );
3415+ encode_sequence(&pictures, 100, 0.5)
3416+ .expect("inter frame now codes the mod-32==8 straddle too");
33433417 }
33443418
33453419 /// The r15 fix at production size: three 640x360 key frames (inter is
@@ -3367,6 +3441,36 @@ mod tests {
33673441 }
33683442 }
33693443
3444+ /// The 640x360 straddle class, but through the inter path an actual
3445+ /// 3-frame sequence exercises (lane-av1inter8's own ladder rung (b)):
3446+ /// proves the leaf-8 inter stream this round wires up is not just
3447+ /// encoder-clean (the test above) but decoder-clean against ffmpeg too.
3448+ #[test]
3449+ fn a_640x360_half_straddle_sequence_round_trips_through_ffmpeg() {
3450+ if !have_ffmpeg() {
3451+ eprintln!(
3452+ "SKIP a_640x360_half_straddle_sequence_round_trips_through_ffmpeg: no ffmpeg"
3453+ );
3454+ return;
3455+ }
3456+ let (width, height) = (640usize, 360usize);
3457+ let pictures: Vec<Picture> = (0..4)
3458+ .map(|i| panned_test_card(width, height, i * 3))
3459+ .collect();
3460+ let encoded = encode_sequence(&pictures, 100, 0.5).unwrap();
3461+ assert_eq!(encoded.frames.len(), 4);
3462+ if let Ok(path) = std::env::var("EC_AV1_DUMP") {
3463+ std::fs::write(&path, &encoded.stream).expect("dump the raw stream");
3464+ }
3465+ let decoded = ffmpeg_decode_sequence(&encoded.stream, width, height, 4);
3466+ assert_eq!(decoded.len(), 4, "decoded frame count");
3467+ for (i, (frame, decoded)) in encoded.frames.iter().zip(&decoded).enumerate() {
3468+ assert_eq!(decoded.y, frame.reconstruction.y, "frame {i}: luma");
3469+ assert_eq!(decoded.u, frame.reconstruction.u, "frame {i}: U");
3470+ assert_eq!(decoded.v, frame.reconstruction.v, "frame {i}: V");
3471+ }
3472+ }
3473+
33703474 /// One frame of a real clip, scaled to a whole number of 32x32 blocks.
33713475 fn clip_frame(clip: &str, skip: &str, width: usize, height: usize) -> Picture {
33723476 let out = Command::new("ffmpeg")
crates/ec-av1/src/mc.rsmodified+29 -2
@@ -52,6 +52,33 @@ const SUBPEL_FILTERS: [[i32; 8]; 16] = [
5252 [0, 0, -2, 8, 126, -6, 2, 0],
5353 ];
5454
55+/// `Subpel_Filters[EIGHTTAP]`'s narrow-block counterpart (spec 7.11.3.4's
56+/// filter selection is per-axis: a `predict` axis whose output block
57+/// dimension is 4 or less reads this table instead of [`SUBPEL_FILTERS`]),
58+/// mirroring `av1_sub_pel_filters_4` (`filter.h`): the same 8-slot shape but
59+/// zeroed at taps 0/1/6/7, so a 4-wide (or 4-tall) block's filter never
60+/// reaches a full 3 samples past its own edge. Every 4:2:0 chroma block
61+/// under an 8x8 (or smaller) luma leaf hits this on at least one axis --
62+/// this is not an edge case, it is the common chroma shape.
63+const SUBPEL_FILTERS_4: [[i32; 8]; 16] = [
64+ [0, 0, 0, 128, 0, 0, 0, 0],
65+ [0, 0, -4, 126, 8, -2, 0, 0],
66+ [0, 0, -8, 122, 18, -4, 0, 0],
67+ [0, 0, -10, 116, 28, -6, 0, 0],
68+ [0, 0, -12, 110, 38, -8, 0, 0],
69+ [0, 0, -12, 102, 48, -10, 0, 0],
70+ [0, 0, -14, 94, 58, -10, 0, 0],
71+ [0, 0, -12, 84, 66, -10, 0, 0],
72+ [0, 0, -12, 76, 76, -12, 0, 0],
73+ [0, 0, -10, 66, 84, -12, 0, 0],
74+ [0, 0, -10, 58, 94, -14, 0, 0],
75+ [0, 0, -10, 48, 102, -12, 0, 0],
76+ [0, 0, -8, 38, 110, -12, 0, 0],
77+ [0, 0, -6, 28, 116, -10, 0, 0],
78+ [0, 0, -4, 18, 122, -8, 0, 0],
79+ [0, 0, -2, 8, 126, -4, 0, 0],
80+];
81+
5582 /// A reference sample at `(x, y)`, clamped to the frame's true (unpadded)
5683 /// extent -- `true_width`/`true_height`, which can be narrower than
5784 /// `stride` -- rather than read out of range (spec 7.11.3.4's `Clip3`
@@ -137,8 +164,8 @@ pub fn predict(
137164 return;
138165 }
139166
140- let h_filter = &SUBPEL_FILTERS[xfrac];
141- let v_filter = &SUBPEL_FILTERS[yfrac];
167+ let h_filter = if block_w <= 4 { &SUBPEL_FILTERS_4[xfrac] } else { &SUBPEL_FILTERS[xfrac] };
168+ let v_filter = if block_h <= 4 { &SUBPEL_FILTERS_4[yfrac] } else { &SUBPEL_FILTERS[yfrac] };
142169
143170 // The vertical pass reads 3 rows above and 4 below the block, so the
144171 // horizontal pass must produce that many extra intermediate rows.
crates/ec-av1/src/tile.rsmodified+275 -22
@@ -2165,6 +2165,7 @@ pub fn sb_coeff_inter_frame_tile(
21652165 let scan32 = default_scan(TX32);
21662166 let scan16 = default_scan(TX16);
21672167 let scan8 = default_scan(TX8);
2168+ let scan4 = default_scan(TX4);
21682169 let zero_grids = [
21692170 vec![0i32; TX32 * TX32],
21702171 vec![0i32; TX16 * TX16],
@@ -2268,38 +2269,102 @@ pub fn sb_coeff_inter_frame_tile(
22682269 (false, false) => {}
22692270 }
22702271 for (leaf, (sr, sc)) in sub_blocks.iter().zip(sub_positions) {
2271- // A 16x16 leaf the true frame edge itself cuts
2272- // through needs a rectangular transform this
2273- // writer does not code yet -- same refusal as
2274- // `sb_coeff_key_frame_tile`'s SUB level, just
2275- // named for an inter-frame leaf.
2272+ // A 16x16 leaf whose own half straddles the true
2273+ // frame edge on both axes needs a rectangular
2274+ // transform this writer does not code yet; one
2275+ // axis only is two (or, at a true corner, one)
2276+ // 8x8 leaves (lane-av1inter8), same split the key
2277+ // frame search takes at this geometry.
22762278 let (has_cols16, has_rows16) = (
22772279 has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols),
22782280 has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows),
22792281 );
2280- if !has_cols16 || !has_rows16 {
2282+ if !has_cols16 && !has_rows16 {
22812283 return Err(Error::unsupported(
22822284 "AV1 tile",
2283- "a 16x16 inter-frame block the true frame edge cuts \
2284- through needs a rectangular transform this writer \
2285+ "a 16x16 inter-frame block whose true edge cuts through \
2286+ both axes needs a rectangular transform this writer \
22852287 does not code yet",
22862288 ));
22872289 }
22882290 let at16 = (sr, sc);
2289- let ctx16 = neighbours.partition_ctx(at16, SUB);
2290- enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx16]);
2291- write_inter_frame_leaf(
2292- &mut enc,
2293- &mut cdfs,
2294- &mut neighbours,
2295- &mut grid,
2296- mi_cols,
2297- mi_rows,
2298- leaf,
2299- at16,
2300- &scan16,
2301- &scan8,
2302- )?;
2291+ if has_cols16 && has_rows16 {
2292+ let ctx16 = neighbours.partition_ctx(at16, SUB);
2293+ enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx16]);
2294+ write_inter_frame_leaf(
2295+ &mut enc,
2296+ &mut cdfs,
2297+ &mut neighbours,
2298+ &mut grid,
2299+ mi_cols,
2300+ mi_rows,
2301+ leaf,
2302+ at16,
2303+ &scan16,
2304+ &scan8,
2305+ )?;
2306+ } else {
2307+ let ctx16 = neighbours.partition_ctx(at16, SUB);
2308+ if has_cols16 {
2309+ enc.symbol_fixed(
2310+ 1,
2311+ &gather(&cdfs.partition_w16[ctx16], VERT_ALIKE),
2312+ );
2313+ } else {
2314+ enc.symbol_fixed(
2315+ 1,
2316+ &gather(&cdfs.partition_w16[ctx16], HORZ_ALIKE),
2317+ );
2318+ }
2319+ let leaves = leaf.eight.as_ref().ok_or_else(|| {
2320+ Error::unsupported(
2321+ "AV1 tile",
2322+ "a 16x16 inter-frame block the true frame edge cuts \
2323+ through needs its `eight` leaves populated",
2324+ )
2325+ })?;
2326+ let (mi_row0, mi_col0) = (sr as u32 * SUB_MI, sc as u32 * SUB_MI);
2327+ let leaf_positions: Vec<(u32, u32)> = (0..4)
2328+ .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2))
2329+ .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols)
2330+ .collect();
2331+ if leaves.len() != leaf_positions.len() {
2332+ return Err(Error::unsupported(
2333+ "AV1 tile",
2334+ "a straddling 16x16 inter-frame block needs one \
2335+ `eight` entry per 8x8 leaf inside the true frame",
2336+ ));
2337+ }
2338+ let mut prev_leaf: Option<((usize, usize), bool, bool)> = None;
2339+ for (leaf8, (mr, mc)) in leaves.iter().zip(leaf_positions) {
2340+ let leaf_mi = (mr as usize, mc as usize);
2341+ let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8);
2342+ enc.symbol(PARTITION_NONE, &mut cdfs.partition_w8[leaf_ctx]);
2343+ let (skip, is_inter) = write_inter_frame_leaf8(
2344+ &mut enc,
2345+ &mut cdfs,
2346+ &mut neighbours,
2347+ &mut grid,
2348+ mi_cols,
2349+ mi_rows,
2350+ leaf8,
2351+ at16,
2352+ leaf_mi,
2353+ &scan8,
2354+ &scan4,
2355+ prev_leaf,
2356+ )?;
2357+ prev_leaf = Some((leaf_mi, skip, is_inter));
2358+ }
2359+ // Same write-back-once-from-the-last-leaf rule
2360+ // as the key frame search's `write_leaf8`
2361+ // caller (r15): the SUB-grid skip/inter arrays
2362+ // are otherwise left stale for the next
2363+ // 16x16 slot.
2364+ if let Some((_, skip, is_inter)) = prev_leaf {
2365+ neighbours.record_inter(at16, SUB, skip, is_inter);
2366+ }
2367+ }
23032368 }
23042369 continue;
23052370 }
@@ -2618,6 +2683,194 @@ fn write_inter_frame_leaf(
26182683 Ok(())
26192684 }
26202685
2686+/// Writes one 8x8 leaf of a straddling 16x16 inter-frame block
2687+/// (lane-av1inter8): its own skip flag, intra/inter choice and (when inter)
2688+/// `NEARESTMV`/`NEWMV` chain, or (when intra) `Y_MODE`, then its own luma and
2689+/// two chroma transform blocks -- coded exactly like
2690+/// [`write_inter_frame_leaf`] but through [`TxbSet::Luma8Inter`]/
2691+/// [`TxbSet::Chroma4`] and at this leaf's own 2x2-mi mv-stack window, reading
2692+/// its skip/intra-inter context from the *enclosing* 16x16 slot's
2693+/// [`Neighbours`] arrays (`outer_at`, in [`SUB`]-grid units) unless
2694+/// `prev_leaf` names this straddling block's own first leaf as the true
2695+/// mi-adjacent neighbour -- the same override [`write_leaf8`] applies to its
2696+/// mode context, needed for the same reason: `Neighbours`' above/left arrays
2697+/// only resolve to [`SUB`] granularity, too coarse for the second leaf of a
2698+/// straddling 16x16 to see the first. `Y_MODE`'s own context (`Size_Group`)
2699+/// is not neighbour-dependent, unlike the key frame path's `kf_y_mode`, so no
2700+/// override is needed there. Hands back this leaf's own skip flag and
2701+/// intra/inter choice, which is what the next leaf (or the caller's final
2702+/// write-back, mirroring `write_leaf8`'s caller) reads.
2703+#[allow(clippy::too_many_arguments)]
2704+fn write_inter_frame_leaf8(
2705+ enc: &mut SymbolEncoder,
2706+ cdfs: &mut Cdfs,
2707+ neighbours: &mut Neighbours,
2708+ grid: &mut MiGrid,
2709+ mi_cols: u32,
2710+ mi_rows: u32,
2711+ block: &BlockCoeffs,
2712+ outer_at: (usize, usize),
2713+ leaf_mi: (usize, usize),
2714+ scan8: &Vec<u16>,
2715+ scan4: &Vec<u16>,
2716+ prev_leaf: Option<((usize, usize), bool, bool)>,
2717+) -> Result<(bool, bool)> {
2718+ if block.inter.is_none() && usize::from(block.mode) >= INTRA_MODES {
2719+ return Err(Error::unsupported(
2720+ "AV1 tile",
2721+ format!(
2722+ "intra mode {} is not one of the thirteen this writer codes",
2723+ block.mode
2724+ ),
2725+ ));
2726+ }
2727+ /// `Y_MODE`'s size group (spec `Size_Group`, `common_data.h`'s
2728+ /// `size_group_lookup[BLOCK_8X8]`) for this leaf's own size.
2729+ const SIZE_GROUP_8: usize = 1;
2730+ /// `Ref_Frame_List`'s `LAST_FRAME` (spec 3), same as the 16x16/32x32
2731+ /// branches.
2732+ const LAST_FRAME: i8 = 1;
2733+
2734+ let (r, c) = outer_at;
2735+ let mut above_skip = neighbours.above_skip[c];
2736+ let mut left_skip = neighbours.left_skip[r];
2737+ let mut above_inter = neighbours.above_inter[c];
2738+ let mut left_inter = neighbours.left_inter[r];
2739+ if let Some(((pr, pc), pskip, pinter)) = prev_leaf {
2740+ if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 {
2741+ above_skip = pskip;
2742+ above_inter = pinter;
2743+ } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 {
2744+ left_skip = pskip;
2745+ left_inter = pinter;
2746+ }
2747+ }
2748+ let skip_ctx = usize::from(above_skip) + usize::from(left_skip);
2749+ enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);
2750+
2751+ let (has_above, has_left) = (leaf_mi.0 > 0, leaf_mi.1 > 0);
2752+ let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);
2753+ let is_inter = block.inter.is_some();
2754+ enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]);
2755+
2756+ let mode_for_tx;
2757+ if let Some(info) = block.inter {
2758+ let sr_ctx = single_ref_ctx(above_inter || left_inter);
2759+ enc.symbol(0, &mut cdfs.single_ref[sr_ctx][0]); // p1: forward
2760+ enc.symbol(0, &mut cdfs.single_ref[sr_ctx][2]); // p3: LAST/LAST2
2761+ enc.symbol(0, &mut cdfs.single_ref[sr_ctx][3]); // p4: LAST
2762+
2763+ let (mi_row, mi_col) = leaf_mi;
2764+ let stack = find_mv_stack(
2765+ grid,
2766+ mi_row,
2767+ mi_col,
2768+ 2,
2769+ 2,
2770+ LAST_FRAME,
2771+ mi_cols as usize,
2772+ mi_rows as usize,
2773+ );
2774+
2775+ let is_new_mv = matches!(info.mode, InterMode::NewMv);
2776+ if is_new_mv {
2777+ enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); // NEWMV
2778+ } else {
2779+ enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not new
2780+ enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not zero
2781+ enc.symbol(0, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // NEARESTMV
2782+ }
2783+ if is_new_mv && stack.entries.len() > 1 {
2784+ enc.symbol(0, &mut cdfs.drl_mode[stack.drl_ctx[0]]);
2785+ }
2786+ let mv = if is_new_mv {
2787+ write_mv(
2788+ enc,
2789+ &mut cdfs.mv_comp,
2790+ &mut cdfs.mv_joint,
2791+ info.mv,
2792+ stack.pred_mv,
2793+ )?;
2794+ info.mv
2795+ } else {
2796+ stack.nearest_mv
2797+ };
2798+ for dr in 0..2 {
2799+ for dc in 0..2 {
2800+ grid.set(
2801+ mi_row + dr,
2802+ mi_col + dc,
2803+ MiInfo {
2804+ is_inter: true,
2805+ ref_frame: LAST_FRAME,
2806+ mv,
2807+ is_new_mv,
2808+ size: 2,
2809+ },
2810+ );
2811+ }
2812+ }
2813+ mode_for_tx = 0;
2814+ } else {
2815+ let mode = usize::from(block.mode);
2816+ enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_8]);
2817+ if (V_PRED..=D67_PRED).contains(&mode) {
2818+ enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]);
2819+ }
2820+ enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]);
2821+ mode_for_tx = mode;
2822+ // Intra: no vote, but still a coded cell -- mvstack's extended-scan
2823+ // coverage must see it (module doc).
2824+ let (mi_row, mi_col) = leaf_mi;
2825+ for dr in 0..2 {
2826+ for dc in 0..2 {
2827+ grid.set(
2828+ mi_row + dr,
2829+ mi_col + dc,
2830+ MiInfo {
2831+ is_inter: false,
2832+ ref_frame: -1,
2833+ mv: (0, 0),
2834+ is_new_mv: false,
2835+ size: 2,
2836+ },
2837+ );
2838+ }
2839+ }
2840+ }
2841+
2842+ let planes = if is_inter {
2843+ [TxbSet::Luma8Inter, TxbSet::Chroma4, TxbSet::Chroma4]
2844+ } else {
2845+ [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4]
2846+ };
2847+ if block.skip {
2848+ let zero_grids = [
2849+ vec![0i32; TX8 * TX8],
2850+ vec![0i32; TX4 * TX4],
2851+ vec![0i32; TX4 * TX4],
2852+ ];
2853+ neighbours.record_mi(leaf_mi, 8, &zero_grids);
2854+ } else {
2855+ let grids = [
2856+ level_grid(&block.luma, TX8)?,
2857+ level_grid(&block.u, TX4)?,
2858+ level_grid(&block.v, TX4)?,
2859+ ];
2860+ write_block_planes(
2861+ enc,
2862+ cdfs,
2863+ &planes,
2864+ &grids,
2865+ &[scan8, scan4, scan4],
2866+ &neighbours.around_mi(leaf_mi, 8),
2867+ mode_for_tx,
2868+ );
2869+ neighbours.record_mi(leaf_mi, 8, &grids);
2870+ }
2871+ Ok((block.skip, is_inter))
2872+}
2873+
26212874 #[cfg(test)]
26222875 mod tests {
26232876 use super::*;