forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1dec' (r5): decoder reads the 8x8-leaf path; RD guard lifted

Browse files at this commit

24df967a9f9cd09b1df1d5f3ac153be9c51dc09aAhmet Kaan GümüşParent: 1d41e74ca24626
decode_leaf8 mirrors write_leaf8 (mi-precise contexts, CFL alphabet,
Luma8/Chroma4 sets); 40x32 and 640x360 byte-exact vs recon, 40x32 agrees
with ffmpeg. leaf8 splits now compete on RD cost everywhere.
crates/ec-av1/src/decode.rsmodified+259 -30
@@ -45,6 +45,7 @@ const SUB: usize = 16;
45 const TX32: usize = 32;45 const TX32: usize = 32;
46 const TX16: usize = 16;46 const TX16: usize = 16;
47 const TX8: usize = 8;47 const TX8: usize = 8;
48+const TX4: usize = 4;
48 const DC_PRED: usize = 0;49 const DC_PRED: usize = 0;
49 /// `V_PRED` (spec 6.10.2): the first directional mode, and so the first that50 /// `V_PRED` (spec 6.10.2): the first directional mode, and so the first that
50 /// carries an angle delta — [`crate::tile`]'s own private copy of the same51 /// carries an angle delta — [`crate::tile`]'s own private copy of the same
@@ -325,6 +326,14 @@ struct Neighbours {
325 left_mode: Vec<usize>,326 left_mode: Vec<usize>,
326 above_side: Vec<usize>,327 above_side: Vec<usize>,
327 left_side: Vec<usize>,328 left_side: Vec<usize>,
329+ /// The same as `above_side`/`left_side`, but kept at the finer mi (4x4)
330+ /// granularity `above`/`left` are, rather than [`SUB`] -- [`crate::tile`]'s
331+ /// `above_side_mi`/`left_side_mi`: two 8x8 leaves of one straddling 16x16
332+ /// block (lane-av1-rect) share a single [`SUB`]-grid cell, so a coarse
333+ /// array cannot tell the second leaf's partition symbol that the first
334+ /// one was coded at 8x8.
335+ above_side_mi: Vec<usize>,
336+ left_side_mi: Vec<usize>,
328 /// Whether the block above/left of this column/row was coded `skip` --337 /// Whether the block above/left of this column/row was coded `skip` --
329 /// an inter frame's own `skip` context (spec `SkipContext`), which the338 /// an inter frame's own `skip` context (spec `SkipContext`), which the
330 /// key-frame writer never tracks (its skip context is always zero); a339 /// key-frame writer never tracks (its skip context is always zero); a
@@ -352,6 +361,8 @@ impl Neighbours {
352 left_mode: vec![DC_PRED; rows],361 left_mode: vec![DC_PRED; rows],
353 above_side: vec![SB; cols],362 above_side: vec![SB; cols],
354 left_side: vec![SB; rows],363 left_side: vec![SB; rows],
364+ above_side_mi: vec![SB; cols * (SUB / MI)],
365+ left_side_mi: vec![SB; rows * (SUB / MI)],
355 above_skip: vec![false; cols],366 above_skip: vec![false; cols],
356 left_skip: vec![false; rows],367 left_skip: vec![false; rows],
357 above_inter: vec![false; cols],368 above_inter: vec![false; cols],
@@ -365,6 +376,7 @@ impl Neighbours {
365 self.left.iter_mut().for_each(|l| *l = Default::default());376 self.left.iter_mut().for_each(|l| *l = Default::default());
366 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);377 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);
367 self.left_side.iter_mut().for_each(|s| *s = SB);378 self.left_side.iter_mut().for_each(|s| *s = SB);
379+ self.left_side_mi.iter_mut().for_each(|s| *s = SB);
368 self.left_skip.iter_mut().for_each(|s| *s = false);380 self.left_skip.iter_mut().for_each(|s| *s = false);
369 self.left_inter.iter_mut().for_each(|i| *i = false);381 self.left_inter.iter_mut().for_each(|i| *i = false);
370 }382 }
@@ -381,15 +393,36 @@ impl Neighbours {
381 }393 }
382 }394 }
383 395
396+ /// The context of a block's partition symbol: delegates to the mi-precise
397+ /// reader (same pattern as `around`/`around_mi`) -- `above_side`/
398+ /// `left_side` are only ever advanced in whole-[`SUB`] steps by
399+ /// [`Self::record`], so a leaf8's `record_mi` (lane-av1-rect) -- which
400+ /// only touches the finer mi arrays -- leaves them stale for the *next*
401+ /// sibling's own partition symbol.
384 fn partition_ctx(&self, at: (usize, usize), side: usize) -> usize {402 fn partition_ctx(&self, at: (usize, usize), side: usize) -> usize {
385 let (r, c) = at;403 let (r, c) = at;
386- 2 * usize::from(self.left_side[r] * 2 <= side) + usize::from(self.above_side[c] * 2 <= side)404+ self.partition_ctx_mi((r * (SUB / MI), c * (SUB / MI)), side)
405+ }
406+
407+ /// [`Self::partition_ctx`] at mi granularity, for an 8x8 leaf of a
408+ /// straddling 16x16 block: reads the finer `above_side_mi`/`left_side_mi`
409+ /// arrays so the second leaf sees the first leaf's own 8x8 side rather
410+ /// than the enclosing 16x16 slot's stale, shared state.
411+ fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize {
412+ 2 * usize::from(self.left_side_mi[mi_r] * 2 <= side)
413+ + usize::from(self.above_side_mi[mi_c] * 2 <= side)
387 }414 }
388 415
389 /// The gathered coded/DC-sign state of the blocks above and to the left416 /// The gathered coded/DC-sign state of the blocks above and to the left
390 /// of a block `side` samples across, per plane.417 /// of a block `side` samples across, per plane.
391 fn around(&self, (r, c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] {418 fn around(&self, (r, c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] {
392- let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI);419+ self.around_mi((r * (SUB / MI), c * (SUB / MI)), side)
420+ }
421+
422+ /// [`Self::around`] taking the block's position directly in 4x4 mode-info
423+ /// units, for the same reason [`Self::record_mi`] does.
424+ fn around_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] {
425+ let side_mi = side / MI;
393 std::array::from_fn(|plane| {426 std::array::from_fn(|plane| {
394 let mut above_coded = false;427 let mut above_coded = false;
395 let mut left_coded = false;428 let mut left_coded = false;
@@ -414,14 +447,27 @@ impl Neighbours {
414 /// not from this block's own side).447 /// not from this block's own side).
415 fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec<i32>; 3]) {448 fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec<i32>; 3]) {
416 let (r, c) = at;449 let (r, c) = at;
417- let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane]));
418 for cell in 0..side / SUB {450 for cell in 0..side / SUB {
419 self.above_mode[c + cell] = mode;451 self.above_mode[c + cell] = mode;
420 self.left_mode[r + cell] = mode;452 self.left_mode[r + cell] = mode;
421 self.above_side[c + cell] = side;453 self.above_side[c + cell] = side;
422 self.left_side[r + cell] = side;454 self.left_side[r + cell] = side;
423 }455 }
424- let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI);456+ self.record_mi((r * (SUB / MI), c * (SUB / MI)), side, grids);
457+ }
458+
459+ /// The coefficient-context half of [`Self::record`], taking the block's
460+ /// position directly in 4x4 mode-info units rather than [`SUB`]-grid
461+ /// `(r, c)`: an 8x8 leaf under a straddling 16x16 (lane-av1-rect) sits at
462+ /// a mi offset [`Self::record`]'s SUB-unit `at` cannot name.
463+ fn record_mi(&mut self, at_mi: (usize, usize), side: usize, grids: &[Vec<i32>; 3]) {
464+ let (mi_r, mi_c) = at_mi;
465+ let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane]));
466+ let side_mi = side / MI;
467+ for cell in 0..side_mi {
468+ self.left_side_mi[mi_r + cell] = side;
469+ self.above_side_mi[mi_c + cell] = side;
470+ }
425 // A chroma 4x4 unit straddling the true luma edge is still whole in471 // A chroma 4x4 unit straddling the true luma edge is still whole in
426 // chroma's own halved grid, so libaom rounds the luma bound up to the472 // chroma's own halved grid, so libaom rounds the luma bound up to the
427 // plane's own 4x4 unit before clamping a subsampled plane473 // plane's own 4x4 unit before clamping a subsampled plane
@@ -734,6 +780,112 @@ fn decode_block(
734 Ok(())780 Ok(())
735 }781 }
736 782
783+/// Decodes one 8x8 leaf of a straddling 16x16 block (lane-av1-rect), mirroring
784+/// [`crate::tile::write_leaf8`]: its intra-mode context comes from the
785+/// *enclosing* 16x16 slot's `above_mode`/`left_mode` (`outer_at`, [`SUB`]-grid),
786+/// overridden on whichever axis `prev_leaf` sits along -- the previous leaf's
787+/// just-decoded mode, not the enclosing slot's stale one, is the true
788+/// neighbour there. `leaf_mi` is this leaf's own position in 4x4 mode-info
789+/// units, which is what its coefficient context (finer than [`SUB`]) is read
790+/// at. Returns the mode this leaf decoded, for the caller's own
791+/// `prev_leaf`/final mode-writeback bookkeeping.
792+#[allow(clippy::too_many_arguments)]
793+fn decode_leaf8(
794+ dec: &mut SymbolDecoder,
795+ cdfs: &mut Cdfs,
796+ neighbours: &mut Neighbours,
797+ outer_at: (usize, usize),
798+ leaf_mi: (usize, usize),
799+ scans: (&[u16], &[u16]),
800+ prev_leaf: Option<((usize, usize), usize)>,
801+ y: &mut PlaneBuf,
802+ u: &mut PlaneBuf,
803+ v: &mut PlaneBuf,
804+ base_q_idx: u8,
805+) -> Result<usize> {
806+ let (r, c) = outer_at;
807+ let mut above_mode = neighbours.above_mode[c];
808+ let mut left_mode = neighbours.left_mode[r];
809+ if let Some(((pr, pc), pmode)) = prev_leaf {
810+ if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 {
811+ above_mode = pmode;
812+ } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 {
813+ left_mode = pmode;
814+ }
815+ }
816+ // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec
817+ // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF, like every other
818+ // `decode_block` caller at 16x16 and up.
819+ let (skip, mode) = read_intra_mode(dec, cdfs, above_mode, left_mode, true)?;
820+ let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
821+ let reach = Reach::of(8, px, py, y.width, y.height);
822+ let (cpx, cpy) = (px / 2, py / 2);
823+ let (luma_grid, u_grid, v_grid);
824+ if skip {
825+ y.reconstruct(px, py, 8, mode, reach, &vec![0i32; 64]);
826+ u.reconstruct(cpx, cpy, 4, DC_PRED, Reach::none(), &vec![0i32; 16]);
827+ v.reconstruct(cpx, cpy, 4, DC_PRED, Reach::none(), &vec![0i32; 16]);
828+ luma_grid = vec![0i32; 64];
829+ u_grid = vec![0i32; 16];
830+ v_grid = vec![0i32; 16];
831+ } else {
832+ let around = neighbours.around_mi(leaf_mi, 8);
833+ luma_grid = read_plane(
834+ dec,
835+ cdfs,
836+ TxbSet::Luma8,
837+ scans.0,
838+ 0,
839+ around[0],
840+ mode,
841+ mode,
842+ reach,
843+ y,
844+ px,
845+ py,
846+ 8,
847+ TX8,
848+ base_q_idx,
849+ )?;
850+ u_grid = read_plane(
851+ dec,
852+ cdfs,
853+ TxbSet::Chroma4,
854+ scans.1,
855+ 1,
856+ around[1],
857+ mode,
858+ DC_PRED,
859+ Reach::none(),
860+ u,
861+ cpx,
862+ cpy,
863+ 4,
864+ TX4,
865+ base_q_idx,
866+ )?;
867+ v_grid = read_plane(
868+ dec,
869+ cdfs,
870+ TxbSet::Chroma4,
871+ scans.1,
872+ 2,
873+ around[2],
874+ mode,
875+ DC_PRED,
876+ Reach::none(),
877+ v,
878+ cpx,
879+ cpy,
880+ 4,
881+ TX4,
882+ base_q_idx,
883+ )?;
884+ }
885+ neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);
886+ Ok(mode)
887+}
888+
737 /// Decodes the payload [`crate::tile::sb_coeff_key_frame_tile`] writes,889 /// Decodes the payload [`crate::tile::sb_coeff_key_frame_tile`] writes,
738 /// returning the picture it reconstructs to.890 /// returning the picture it reconstructs to.
739 ///891 ///
@@ -793,6 +945,7 @@ pub fn decode_key_frame_tile(
793 let scan32 = default_scan(TX32);945 let scan32 = default_scan(TX32);
794 let scan16 = default_scan(TX16);946 let scan16 = default_scan(TX16);
795 let scan8 = default_scan(TX8);947 let scan8 = default_scan(TX8);
948+ let scan4 = default_scan(TX4);
796 949
797 let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx));950 let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx));
798 let mut dec = SymbolDecoder::new(data);951 let mut dec = SymbolDecoder::new(data);
@@ -918,36 +1071,99 @@ pub fn decode_key_frame_tile(
918 has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols),1071 has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols),
919 has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows),1072 has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows),
920 );1073 );
921- if !has_cols16 || !has_rows16 {
922- return Err(unsupported(
923- "a 16x16 block the true frame edge cuts through (round 2)",
924- ));
925- }
926 let at16 = (sr, sc);1074 let at16 = (sr, sc);
927 let ctx16 = neighbours.partition_ctx(at16, SUB);1075 let ctx16 = neighbours.partition_ctx(at16, SUB);
928- let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]);1076+ if has_cols16 && has_rows16 {
929- if part16 != PARTITION_NONE {1077+ let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]);
1078+ if part16 != PARTITION_NONE {
1079+ return Err(unsupported(
1080+ "a partition below 16x16 (this encoder never writes one)",
1081+ ));
1082+ }
1083+ decode_block(
1084+ &mut dec,
1085+ &mut cdfs,
1086+ &mut neighbours,
1087+ at16,
1088+ SUB,
1089+ TxbSet::Luma16,
1090+ TxbSet::Chroma8,
1091+ TX16,
1092+ TX8,
1093+ (&scan16, &scan8),
1094+ true,
1095+ &mut y,
1096+ &mut u,
1097+ &mut v,
1098+ base_q_idx,
1099+ )?;
1100+ continue;
1101+ }
1102+ // The true edge falls inside this 16x16
1103+ // leaf itself (mod-32==8 target sizes,
1104+ // lane-av1-rect): one axis only -- an 8x8
1105+ // leaf never itself straddles, so the
1106+ // block splits cleanly along whichever
1107+ // axis is short.
1108+ if !has_cols16 && !has_rows16 {
930 return Err(unsupported(1109 return Err(unsupported(
931- "a partition below 16x16 (this encoder never writes one)",1110+ "a 16x16 block whose true edge cuts through both \
1111+ axes needs a rectangular transform this decoder \
1112+ does not code yet",
932 ));1113 ));
933 }1114 }
934- decode_block(1115+ if has_cols16 {
935- &mut dec,1116+ dec.symbol_fixed(&gather(
936- &mut cdfs,1117+ &cdfs.partition_w16[ctx16],
937- &mut neighbours,1118+ VERT_ALIKE,
938- at16,1119+ ));
939- SUB,1120+ } else {
940- TxbSet::Luma16,1121+ dec.symbol_fixed(&gather(
941- TxbSet::Chroma8,1122+ &cdfs.partition_w16[ctx16],
942- TX16,1123+ HORZ_ALIKE,
943- TX8,1124+ ));
944- (&scan16, &scan8),1125+ }
945- true,1126+ let (mi_row0, mi_col0) =
946- &mut y,1127+ (sr as u32 * SUB_MI, sc as u32 * SUB_MI);
947- &mut u,1128+ let leaf_positions: Vec<(u32, u32)> = (0..4)
948- &mut v,1129+ .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2))
949- base_q_idx,1130+ .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols)
950- )?;1131+ .collect();
1132+ let mut prev_leaf: Option<((usize, usize), usize)> = None;
1133+ for (mr, mc) in leaf_positions {
1134+ let leaf_mi = (mr as usize, mc as usize);
1135+ let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8);
1136+ let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]);
1137+ if part8 != PARTITION_NONE {
1138+ return Err(unsupported(
1139+ "a partition below 8x8 (this encoder never writes one)",
1140+ ));
1141+ }
1142+ let leaf_mode = decode_leaf8(
1143+ &mut dec,
1144+ &mut cdfs,
1145+ &mut neighbours,
1146+ at16,
1147+ leaf_mi,
1148+ (&scan8, &scan4),
1149+ prev_leaf,
1150+ &mut y,
1151+ &mut u,
1152+ &mut v,
1153+ base_q_idx,
1154+ )?;
1155+ prev_leaf = Some((leaf_mi, leaf_mode));
1156+ }
1157+ // `record()`'s `above_mode`/`left_mode`
1158+ // write is a no-op at an 8x8 leaf's own
1159+ // side, so force the write once the whole
1160+ // 16x16 slot's leaves are done, from the
1161+ // last leaf (mirrors the writer's r15
1162+ // fix).
1163+ if let Some((_, mode)) = prev_leaf {
1164+ neighbours.above_mode[sc] = mode;
1165+ neighbours.left_mode[sr] = mode;
1166+ }
951 }1167 }
952 }1168 }
953 _ => {1169 _ => {
@@ -1810,6 +2026,19 @@ mod tests {
1810 }2026 }
1811 }2027 }
1812 2028
2029+ /// mod-32==8 sizes (lane-av1-rect): the true frame edge falls inside a
2030+ /// 16x16 block itself, forcing the encoder's 8x8-leaf straddle path
2031+ /// ([`crate::tile::write_leaf8`]) regardless of the RD-cost guard, since
2032+ /// no whole 16x16/32x32 legally covers the true edge here -- this decoder
2033+ /// round's leaf8 read path (`decode_leaf8`, gathered `partition_w16` split
2034+ /// bit, `partition_w8` leaves).
2035+ #[test]
2036+ fn leaf8_straddle_sizes_round_trip_bit_exact_against_the_encoder_reconstruction() {
2037+ for (w, h) in [(40usize, 32usize), (640, 360)] {
2038+ round_trips(w, h, &crate::intra::KEY_FRAME_MODES);
2039+ }
2040+ }
2041+
1813 /// A skipped block codes no residual syntax at all (spec 5.11.34):2042 /// A skipped block codes no residual syntax at all (spec 5.11.34):
1814 /// straight prediction on every plane. The real key-frame writer never2043 /// straight prediction on every plane. The real key-frame writer never
1815 /// emits `skip = 1` ([`crate::tile::write_intra_mode`] hardcodes it to2044 /// emits `skip = 1` ([`crate::tile::write_intra_mode`] hardcodes it to
@@ -1998,7 +2227,7 @@ mod tests {
1998 return;2227 return;
1999 }2228 }
2000 use crate::encode::encode_key_frame;2229 use crate::encode::encode_key_frame;
2001- for &(width, height) in &[(64usize, 64usize), (216, 96)] {2230+ for &(width, height) in &[(64usize, 64usize), (216, 96), (40, 32)] {
2002 let picture = round_trip_test_card(width, height);2231 let picture = round_trip_test_card(width, height);
2003 let encoded = encode_key_frame(&picture, 100, 0.5).unwrap();2232 let encoded = encode_key_frame(&picture, 100, 0.5).unwrap();
2004 let (coded_w, coded_h) = ffprobe_size(&encoded.stream);2233 let (coded_w, coded_h) = ffprobe_size(&encoded.stream);
crates/ec-av1/src/encode.rsmodified+8 -10
@@ -1831,16 +1831,14 @@ pub(crate) fn encode_key_frame_inner(
1831 }1831 }
1832 1832
1833 // A split whose subs are all whole 16x16 is a real quality1833 // A split whose subs are all whole 16x16 is a real quality
1834- // alternative to `whole`, exactly as before; a split1834+ // alternative to `whole`, exactly as before. Through r14, a
1835- // carrying an 8x8-leaf sub was, through r14, only ever1835+ // split carrying an 8x8-leaf sub was only ever forced
1836- // forced (`!whole_legal`), never chosen on cost, because the1836+ // (`!whole_legal`), never chosen on cost, because the leaf
1837- // leaf path was not yet proven against a real decoder. r151837+ // path was not yet proven against a real decoder; r15 proved
1838- // proved it against ffmpeg, but crate::decode has not learned1838+ // it against ffmpeg, and lane-av1dec r5 taught crate::decode
1839- // the 8x8-leaf read path yet, so choosing leaf8 on cost at a1839+ // the read path (`decode_leaf8`), so leaf8 can now win on
1840- // size our own decoder round-trips would break decode_stream;1840+ // cost anywhere, same as any other split.
1841- // the guard stays until decode.rs reads leaf8, then lifts.1841+ if !whole_legal || (split_blocks && cost_split < cost_whole) {
1842- let has_leaf8 = split.iter().any(|b| b.eight.is_some());
1843- if !whole_legal || (split_blocks && !has_leaf8 && cost_split < cost_whole) {
1844 modes.extend_from_slice(&split_modes);1842 modes.extend_from_slice(&split_modes);
1845 blocks.push(Quadrant::Split(split));1843 blocks.push(Quadrant::Split(split));
1846 } else {1844 } else {