forge
Explore
Log in

Don't you dare go Hollow

Extend the 8x8-leaf straddle path to inter frames

Browse files at this commit

cc290f509f1fafe63b6ec4bc9cf3d94562b9754bAhmet Kaan GümüşParent: 588c3e4
  • feat
    • extend the 8x8-leaf straddle path to inter frames
Show full message
feat(av1): extend the 8x8-leaf straddle path to inter frames

Mirrors lane-av1-rect's key-frame 16x16-straddles-one-axis -> up-to-two
8x8-leaf path (code_square side==8, tile.rs write_leaf8) on the inter
side: code_square_inter takes a `side` param (8 or 16) and picks
Luma8Inter/Chroma4 or Luma16Inter/Chroma8 accordingly; the encoder's
inter loop builds `eight` leaves per straddling 16x16 the same way the
key-frame search does, each leaf's own NEARESTMV mv-stack window at
2x2-mi; the tile writer (write_inter_frame_leaf8) and decoder
(decode_inter_block8) mirror that leaf-by-leaf, with the same
prev_leaf skip/intra-inter context override write_leaf8 uses for mode
context (Neighbours' above/left arrays only resolve to SUB=16
granularity). New TxbSet::Luma8Inter/INTER_TX_TYPE_SET3_8 CDF
(libaom default_inter_ext_tx_cdf[EXT_TX_SET_DCT_IDTX][TX_8X8] = 4167,
distinct default+adaptation state from the 16x16/32x32 rows already
carried).

Encoder-side round-trips clean (both-axis-cut still refuses correctly);
a real multi-frame decode round-trip through ffmpeg desyncs starting
at the third coded frame (the second inter frame, U plane) -- logged
as an open dead-end for the next round, not chased further this round.
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
@@ -2011,6 +2011,371 @@ fn decode_inter_block(
20112011 Ok(())
20122012 }
20132013
2014+/// Decodes one 8x8 leaf of a straddling 16x16 inter-frame block
2015+/// (lane-av1inter8), mirroring [`crate::tile::write_inter_frame_leaf8`]'s
2016+/// exact symbol order: its own skip flag, intra/inter choice and (when
2017+/// inter) `NEARESTMV`/`NEWMV` chain against this leaf's own 2x2-mi mv-stack
2018+/// window, or (when intra) `Y_MODE`, then its own luma and two chroma
2019+/// transform blocks through [`TxbSet::Luma8Inter`]/[`TxbSet::Chroma4`],
2020+/// same as [`decode_leaf8`]'s coefficient layer but with
2021+/// motion-compensated prediction instead of intra prediction when inter.
2022+/// `outer_at` (in [`SUB`]-grid units) is the enclosing 16x16 slot's
2023+/// skip/intra-inter context, overridden by `prev_leaf` exactly as
2024+/// [`decode_leaf8`] overrides mode context: `Neighbours`' above/left arrays
2025+/// only resolve to [`SUB`] granularity. Hands back this leaf's own skip flag
2026+/// and intra/inter choice, for the next leaf or the caller's final
2027+/// write-back.
2028+#[allow(clippy::too_many_arguments)]
2029+fn decode_inter_block8(
2030+ dec: &mut SymbolDecoder,
2031+ cdfs: &mut Cdfs,
2032+ neighbours: &mut Neighbours,
2033+ grid: &mut MiGrid,
2034+ mi_cols: u32,
2035+ mi_rows: u32,
2036+ outer_at: (usize, usize),
2037+ leaf_mi: (usize, usize),
2038+ y: &mut PlaneBuf,
2039+ u: &mut PlaneBuf,
2040+ v: &mut PlaneBuf,
2041+ ref_y: &PlaneBuf,
2042+ ref_u: &PlaneBuf,
2043+ ref_v: &PlaneBuf,
2044+ base_q_idx: u8,
2045+ scan8: &[u16],
2046+ scan4: &[u16],
2047+ prev_leaf: Option<((usize, usize), bool, bool)>,
2048+) -> Result<(bool, bool)> {
2049+ const LAST_FRAME: i8 = 1;
2050+ /// `Y_MODE`'s size group (`common_data.h`'s `size_group_lookup[BLOCK_8X8]`).
2051+ const SIZE_GROUP_8: usize = 1;
2052+
2053+ let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
2054+ let (cpx, cpy) = (px / 2, py / 2);
2055+ const SIDE: usize = 8;
2056+ const CHROMA_SIDE: usize = 4;
2057+
2058+ let (r, c) = outer_at;
2059+ let mut above_skip = neighbours.above_skip[c];
2060+ let mut left_skip = neighbours.left_skip[r];
2061+ let mut above_inter = neighbours.above_inter[c];
2062+ let mut left_inter = neighbours.left_inter[r];
2063+ if let Some(((pr, pc), pskip, pinter)) = prev_leaf {
2064+ if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 {
2065+ above_skip = pskip;
2066+ above_inter = pinter;
2067+ } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 {
2068+ left_skip = pskip;
2069+ left_inter = pinter;
2070+ }
2071+ }
2072+ let skip_ctx = usize::from(above_skip) + usize::from(left_skip);
2073+ let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) == 1;
2074+
2075+ let (has_above, has_left) = (leaf_mi.0 > 0, leaf_mi.1 > 0);
2076+ let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);
2077+ let is_inter = dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1;
2078+
2079+ let mode_for_tx;
2080+ let (luma_grid, u_grid, v_grid);
2081+ if is_inter {
2082+ let sr_ctx = single_ref_ctx(above_inter || left_inter);
2083+ let p1 = dec.symbol(&mut cdfs.single_ref[sr_ctx][0]);
2084+ let p3 = dec.symbol(&mut cdfs.single_ref[sr_ctx][2]);
2085+ let p4 = dec.symbol(&mut cdfs.single_ref[sr_ctx][3]);
2086+ if (p1, p3, p4) != (0, 0, 0) {
2087+ return Err(unsupported(
2088+ "a reference frame other than LAST_FRAME (round 2)",
2089+ ));
2090+ }
2091+
2092+ let (mi_row, mi_col) = leaf_mi;
2093+ let stack = find_mv_stack(
2094+ grid,
2095+ mi_row,
2096+ mi_col,
2097+ 2,
2098+ 2,
2099+ LAST_FRAME,
2100+ mi_cols as usize,
2101+ mi_rows as usize,
2102+ );
2103+
2104+ let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1;
2105+ let (mv, is_new_mv) = if !not_new {
2106+ if stack.entries.len() > 1 {
2107+ dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[0]]);
2108+ }
2109+ (
2110+ read_mv(dec, &mut cdfs.mv_comp, &mut cdfs.mv_joint, stack.pred_mv),
2111+ true,
2112+ )
2113+ } else {
2114+ let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1;
2115+ if !not_zero {
2116+ return Err(unsupported("GLOBALMV (round 3)"));
2117+ }
2118+ let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0;
2119+ let mv = if nearest {
2120+ stack.nearest_mv
2121+ } else {
2122+ let mut idx = 1usize;
2123+ while idx < 3 && stack.entries.len() > idx + 1 {
2124+ if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 {
2125+ break;
2126+ }
2127+ idx += 1;
2128+ }
2129+ stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv)
2130+ };
2131+ (mv, false)
2132+ };
2133+ for dr in 0..2 {
2134+ for dc in 0..2 {
2135+ grid.set(
2136+ mi_row + dr,
2137+ mi_col + dc,
2138+ MiInfo {
2139+ is_inter: true,
2140+ ref_frame: LAST_FRAME,
2141+ mv,
2142+ is_new_mv,
2143+ size: 2,
2144+ },
2145+ );
2146+ }
2147+ }
2148+ mode_for_tx = 0;
2149+
2150+ let mut pred_y = vec![0u8; SIDE * SIDE];
2151+ mc::predict(
2152+ &ref_y.data,
2153+ ref_y.width,
2154+ ref_y.true_width,
2155+ ref_y.true_height,
2156+ mv_to_q4(px, mv.1, true),
2157+ mv_to_q4(py, mv.0, true),
2158+ SIDE,
2159+ SIDE,
2160+ &mut pred_y,
2161+ );
2162+ let mut pred_u = vec![0u8; CHROMA_SIDE * CHROMA_SIDE];
2163+ mc::predict(
2164+ &ref_u.data,
2165+ ref_u.width,
2166+ ref_u.true_width,
2167+ ref_u.true_height,
2168+ mv_to_q4(cpx, mv.1, false),
2169+ mv_to_q4(cpy, mv.0, false),
2170+ CHROMA_SIDE,
2171+ CHROMA_SIDE,
2172+ &mut pred_u,
2173+ );
2174+ let mut pred_v = vec![0u8; CHROMA_SIDE * CHROMA_SIDE];
2175+ mc::predict(
2176+ &ref_v.data,
2177+ ref_v.width,
2178+ ref_v.true_width,
2179+ ref_v.true_height,
2180+ mv_to_q4(cpx, mv.1, false),
2181+ mv_to_q4(cpy, mv.0, false),
2182+ CHROMA_SIDE,
2183+ CHROMA_SIDE,
2184+ &mut pred_v,
2185+ );
2186+
2187+ if skip {
2188+ y.reconstruct_mc(px, py, SIDE, &pred_y, &vec![0i32; SIDE * SIDE]);
2189+ u.reconstruct_mc(
2190+ cpx,
2191+ cpy,
2192+ CHROMA_SIDE,
2193+ &pred_u,
2194+ &vec![0i32; CHROMA_SIDE * CHROMA_SIDE],
2195+ );
2196+ v.reconstruct_mc(
2197+ cpx,
2198+ cpy,
2199+ CHROMA_SIDE,
2200+ &pred_v,
2201+ &vec![0i32; CHROMA_SIDE * CHROMA_SIDE],
2202+ );
2203+ luma_grid = vec![0i32; SIDE * SIDE];
2204+ u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
2205+ v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
2206+ } else {
2207+ let around = neighbours.around_mi(leaf_mi, 8);
2208+ luma_grid = read_inter_plane(
2209+ dec,
2210+ cdfs,
2211+ TxbSet::Luma8Inter,
2212+ scan8,
2213+ 0,
2214+ around[0],
2215+ mode_for_tx,
2216+ y,
2217+ px,
2218+ py,
2219+ SIDE,
2220+ base_q_idx,
2221+ &pred_y,
2222+ )?;
2223+ u_grid = read_inter_plane(
2224+ dec,
2225+ cdfs,
2226+ TxbSet::Chroma4,
2227+ scan4,
2228+ 1,
2229+ around[1],
2230+ mode_for_tx,
2231+ u,
2232+ cpx,
2233+ cpy,
2234+ CHROMA_SIDE,
2235+ base_q_idx,
2236+ &pred_u,
2237+ )?;
2238+ v_grid = read_inter_plane(
2239+ dec,
2240+ cdfs,
2241+ TxbSet::Chroma4,
2242+ scan4,
2243+ 2,
2244+ around[2],
2245+ mode_for_tx,
2246+ v,
2247+ cpx,
2248+ cpy,
2249+ CHROMA_SIDE,
2250+ base_q_idx,
2251+ &pred_v,
2252+ )?;
2253+ }
2254+ } else {
2255+ let mode = dec.symbol(&mut cdfs.y_mode[SIZE_GROUP_8]);
2256+ if mode >= 13 {
2257+ return Err(unsupported(
2258+ "an intra mode this decoder does not code (round 2)",
2259+ ));
2260+ }
2261+ if (V_PRED..=D67_PRED).contains(&mode) {
2262+ let angle = dec.symbol(&mut cdfs.angle_delta[mode - V_PRED]);
2263+ if angle != ANGLE_DELTA_ZERO {
2264+ return Err(unsupported(
2265+ "a nonzero angle delta (this encoder never writes one)",
2266+ ));
2267+ }
2268+ }
2269+ let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]);
2270+ if uv_mode != DC_PRED {
2271+ return Err(unsupported(
2272+ "a non-DC chroma mode on an 8x8 inter-frame leaf (this encoder never writes one)",
2273+ ));
2274+ }
2275+ mode_for_tx = mode;
2276+ let (mi_row, mi_col) = leaf_mi;
2277+ for dr in 0..2 {
2278+ for dc in 0..2 {
2279+ grid.set(
2280+ mi_row + dr,
2281+ mi_col + dc,
2282+ MiInfo {
2283+ is_inter: false,
2284+ ref_frame: -1,
2285+ mv: (0, 0),
2286+ is_new_mv: false,
2287+ size: 2,
2288+ },
2289+ );
2290+ }
2291+ }
2292+
2293+ let reach = Reach::of(SIDE, px, py, y.width, y.height);
2294+ if skip {
2295+ y.reconstruct(px, py, SIDE, mode, reach, &vec![0i32; SIDE * SIDE], None);
2296+ u.reconstruct(
2297+ cpx,
2298+ cpy,
2299+ CHROMA_SIDE,
2300+ DC_PRED,
2301+ Reach::none(),
2302+ &vec![0i32; CHROMA_SIDE * CHROMA_SIDE],
2303+ None,
2304+ );
2305+ v.reconstruct(
2306+ cpx,
2307+ cpy,
2308+ CHROMA_SIDE,
2309+ DC_PRED,
2310+ Reach::none(),
2311+ &vec![0i32; CHROMA_SIDE * CHROMA_SIDE],
2312+ None,
2313+ );
2314+ luma_grid = vec![0i32; SIDE * SIDE];
2315+ u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
2316+ v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
2317+ } else {
2318+ let around = neighbours.around_mi(leaf_mi, 8);
2319+ luma_grid = read_plane(
2320+ dec,
2321+ cdfs,
2322+ TxbSet::Luma8,
2323+ scan8,
2324+ 0,
2325+ around[0],
2326+ mode,
2327+ mode,
2328+ reach,
2329+ y,
2330+ px,
2331+ py,
2332+ SIDE,
2333+ TX8,
2334+ base_q_idx,
2335+ None,
2336+ )?;
2337+ u_grid = read_plane(
2338+ dec,
2339+ cdfs,
2340+ TxbSet::Chroma4,
2341+ scan4,
2342+ 1,
2343+ around[1],
2344+ mode,
2345+ DC_PRED,
2346+ Reach::none(),
2347+ u,
2348+ cpx,
2349+ cpy,
2350+ CHROMA_SIDE,
2351+ TX4,
2352+ base_q_idx,
2353+ None,
2354+ )?;
2355+ v_grid = read_plane(
2356+ dec,
2357+ cdfs,
2358+ TxbSet::Chroma4,
2359+ scan4,
2360+ 2,
2361+ around[2],
2362+ mode,
2363+ DC_PRED,
2364+ Reach::none(),
2365+ v,
2366+ cpx,
2367+ cpy,
2368+ CHROMA_SIDE,
2369+ TX4,
2370+ base_q_idx,
2371+ None,
2372+ )?;
2373+ }
2374+ }
2375+ neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);
2376+ Ok((skip, is_inter))
2377+}
2378+
20142379 /// Decodes the payload [`crate::tile::sb_coeff_inter_frame_tile`] writes,
20152380 /// against `reference` (the previous frame's own decoded picture, at this
20162381 /// frame's true size -- a decoder's single-slot DPB, matching what this
@@ -2093,6 +2458,7 @@ pub fn decode_inter_frame_tile(
20932458 let scan32 = default_scan(TX32);
20942459 let scan16 = default_scan(TX16);
20952460 let scan8 = default_scan(TX8);
2461+ let scan4 = default_scan(TX4);
20962462
20972463 let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx));
20982464 let mut dec = SymbolDecoder::new(data);
@@ -2189,44 +2555,101 @@ pub fn decode_inter_frame_tile(
21892555 has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols),
21902556 has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows),
21912557 );
2192- if !has_cols16 || !has_rows16 {
2558+ if !has_cols16 && !has_rows16 {
21932559 return Err(unsupported(
2194- "a 16x16 inter block the true frame edge cuts through (round 2)",
2560+ "a 16x16 inter block whose true edge cuts through both \
2561+ axes needs a rectangular transform this decoder does \
2562+ not code yet",
21952563 ));
21962564 }
21972565 let at16 = (sr, sc);
2198- let ctx16 = neighbours.partition_ctx(at16, SUB);
2199- let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]);
2200- if part16 != PARTITION_NONE {
2201- return Err(unsupported(
2202- "a partition below 16x16 (this encoder never writes one)",
2203- ));
2566+ if has_cols16 && has_rows16 {
2567+ let ctx16 = neighbours.partition_ctx(at16, SUB);
2568+ let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]);
2569+ if part16 != PARTITION_NONE {
2570+ return Err(unsupported(
2571+ "a partition below 16x16 (this encoder never writes one)",
2572+ ));
2573+ }
2574+ decode_inter_block(
2575+ &mut dec,
2576+ &mut cdfs,
2577+ &mut neighbours,
2578+ &mut grid,
2579+ at16,
2580+ SUB,
2581+ mi_cols,
2582+ mi_rows,
2583+ &mut y,
2584+ &mut u,
2585+ &mut v,
2586+ &ref_y,
2587+ &ref_u,
2588+ &ref_v,
2589+ base_q_idx,
2590+ TxbSet::Luma16,
2591+ TxbSet::Luma16Inter,
2592+ TxbSet::Chroma8,
2593+ TX16,
2594+ TX8,
2595+ &scan16,
2596+ &scan8,
2597+ 2,
2598+ )?;
2599+ } else {
2600+ let ctx16 = neighbours.partition_ctx(at16, SUB);
2601+ if has_cols16 {
2602+ dec.symbol_fixed(&gather(
2603+ &cdfs.partition_w16[ctx16],
2604+ VERT_ALIKE,
2605+ ));
2606+ } else {
2607+ dec.symbol_fixed(&gather(
2608+ &cdfs.partition_w16[ctx16],
2609+ HORZ_ALIKE,
2610+ ));
2611+ }
2612+ let (mi_row0, mi_col0) = (sr as u32 * SUB_MI, sc as u32 * SUB_MI);
2613+ let leaf_positions: Vec<(u32, u32)> = (0..4)
2614+ .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2))
2615+ .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols)
2616+ .collect();
2617+ let mut prev_leaf: Option<((usize, usize), bool, bool)> = None;
2618+ for (mr, mc) in leaf_positions {
2619+ let leaf_mi = (mr as usize, mc as usize);
2620+ let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8);
2621+ let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]);
2622+ if part8 != PARTITION_NONE {
2623+ return Err(unsupported(
2624+ "a partition below 8x8 (this encoder never writes one)",
2625+ ));
2626+ }
2627+ let (skip, is_inter) = decode_inter_block8(
2628+ &mut dec,
2629+ &mut cdfs,
2630+ &mut neighbours,
2631+ &mut grid,
2632+ mi_cols,
2633+ mi_rows,
2634+ at16,
2635+ leaf_mi,
2636+ &mut y,
2637+ &mut u,
2638+ &mut v,
2639+ &ref_y,
2640+ &ref_u,
2641+ &ref_v,
2642+ base_q_idx,
2643+ &scan8,
2644+ &scan4,
2645+ prev_leaf,
2646+ )?;
2647+ prev_leaf = Some((leaf_mi, skip, is_inter));
2648+ }
2649+ if let Some((_, skip, is_inter)) = prev_leaf {
2650+ neighbours.record_inter(at16, SUB, skip, is_inter);
2651+ }
22042652 }
2205- decode_inter_block(
2206- &mut dec,
2207- &mut cdfs,
2208- &mut neighbours,
2209- &mut grid,
2210- at16,
2211- SUB,
2212- mi_cols,
2213- mi_rows,
2214- &mut y,
2215- &mut u,
2216- &mut v,
2217- &ref_y,
2218- &ref_u,
2219- &ref_v,
2220- base_q_idx,
2221- TxbSet::Luma16,
2222- TxbSet::Luma16Inter,
2223- TxbSet::Chroma8,
2224- TX16,
2225- TX8,
2226- &scan16,
2227- &scan8,
2228- 2,
2229- )?;
22302653 }
22312654 }
22322655 _ => {
crates/ec-av1/src/encode.rsmodified+198 -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,33 @@ 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+ let decoded = ffmpeg_decode_sequence(&encoded.stream, width, height, 4);
3463+ assert_eq!(decoded.len(), 4, "decoded frame count");
3464+ for (i, (frame, decoded)) in encoded.frames.iter().zip(&decoded).enumerate() {
3465+ assert_eq!(decoded.y, frame.reconstruction.y, "frame {i}: luma");
3466+ assert_eq!(decoded.u, frame.reconstruction.u, "frame {i}: U");
3467+ assert_eq!(decoded.v, frame.reconstruction.v, "frame {i}: V");
3468+ }
3469+ }
3470+
33703471 /// One frame of a real clip, scaled to a whole number of 32x32 blocks.
33713472 fn clip_frame(clip: &str, skip: &str, width: usize, height: usize) -> Picture {
33723473 let out = Command::new("ffmpeg")
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::*;