diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index a7edca41..8b0452a7 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -32,7 +32,22 @@ fn main() { Ok(frames) if frames.is_empty() => { println!("OK but EMPTY: no frames -- is {path} an IVF rather than a raw OBU stream?"); } - Ok(frames) => println!("OK: {} frames decoded", frames.len()), + Ok(frames) => { + println!("OK: {} frames decoded", frames.len()); + // Optional second arg: dump the decoded planes as raw yuv420p so a + // pixel diff against `ffmpeg -i s.obu -f rawvideo` needs no test harness. + if let Some(out) = std::env::args().nth(2) { + // 8-bit only: planes are u16, take the low byte. + let mut buf: Vec = Vec::new(); + for f in &frames { + for p in [&f.y, &f.u, &f.v] { + buf.extend(p.iter().map(|&s| s as u8)); + } + } + std::fs::write(&out, &buf).expect("writing raw planes"); + println!("wrote {} bytes of yuv420p to {out}", buf.len()); + } + } Err(e) => println!("REFUSED: {e}"), } } diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index cc4fa41e..73ab1148 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1826,6 +1826,19 @@ struct Neighbours { /// one was coded at 8x8. above_side_mi: Vec, left_side_mi: Vec, + /// The luma `mode` of the last `BLOCK_4X4` leaf decoded at each mi column + /// / row, with the mi position it was written at + /// (`(usize::MAX, _)` = never written). `above_mode`/`left_mode` are on + /// the coarse 16x16 grid, which cannot distinguish the two 4x4 leaves an + /// 8x8 `PARTITION_SPLIT` puts in one mi column; libaom's intra-mode + /// context reads `mi(row, col-1)`/`mi(row-1, col)` exactly, so a sub-8x8 + /// leaf whose neighbour is another split group needs that group's leaf 3 + /// (bottom-right), not the leaf the coarse slot happens to hold. Written + /// and read only by [`decode_leaf_split4`]; the position guard makes it + /// fall back to the coarse slot whenever the real neighbour was a block + /// of 8x8 or larger (whose one mode is correct for every row it spans). + sub8_mode_col: Vec<(usize, usize)>, + sub8_mode_row: Vec<(usize, usize)>, /// Whether the block above/left of this column/row was coded `skip` -- /// an inter frame's own `skip` context (spec `SkipContext`), which the /// key-frame writer never tracks (its skip context is always zero); a @@ -1953,6 +1966,8 @@ impl Neighbours { left_side: vec![SB; rows], above_side_mi: vec![SB; cols * (SUB / MI)], left_side_mi: vec![SB; rows * (SUB / MI)], + sub8_mode_col: vec![(usize::MAX, 0); cols * (SUB / MI)], + sub8_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)], above_skip: vec![false; cols], left_skip: vec![false; rows], above_skip_mode: vec![false; cols], @@ -2514,6 +2529,46 @@ impl Neighbours { /// [`Self::partition_ctx`]) are the neighbour's own width/height per the /// spec's per-edge partition-context rule, so a true rectangular block /// naturally wants two different values here rather than one. + /// Writes this block's luma `mode` into the mi-granular + /// [`Self::sub8_mode_col`]/[`Self::sub8_mode_row`] maps, keyed by the mi + /// position a *later* neighbour would read it from: the block's LAST mi + /// row for every column it spans (what the block below reads via + /// `mi(row-1, col)`) and its LAST mi column for every row (what the + /// block to the right reads via `mi(row, col-1)`). Only sub-8x8 leaves + /// read these back, and only when the recorded position is exactly their + /// own neighbour -- see [`Self::sub8_mode_col`]'s doc. + fn record_mode_mi(&mut self, mi_r: usize, mi_c: usize, mi_w: usize, mi_h: usize, mode: usize) { + let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1); + for cell in 0..mi_w { + if let Some(slot) = self.sub8_mode_col.get_mut(mi_c + cell) { + *slot = (last_r, mode); + } + } + for cell in 0..mi_h { + if let Some(slot) = self.sub8_mode_row.get_mut(mi_r + cell) { + *slot = (last_c, mode); + } + } + } + + /// The mi-exact above/left neighbour luma mode for a block whose top-left + /// mi is `(mi_r, mi_c)`, or `None` when no block has recorded that exact + /// mi position (then the coarse [`SUB`]-grid slot is right by + /// construction). See [`Self::record_mode_mi`]. + fn mode_above_mi(&self, mi_r: usize, mi_c: usize) -> Option { + match self.sub8_mode_col.get(mi_c) { + Some(&(row, m)) if mi_r > 0 && row == mi_r - 1 => Some(m), + _ => None, + } + } + + fn mode_left_mi(&self, mi_r: usize, mi_c: usize) -> Option { + match self.sub8_mode_row.get(mi_r) { + Some(&(col, m)) if mi_c > 0 && col == mi_c - 1 => Some(m), + _ => None, + } + } + fn record_rect( &mut self, at: (usize, usize), @@ -2534,6 +2589,7 @@ impl Neighbours { self.left_uv_mode[r + cell] = uv_mode; self.left_side[r + cell] = h; } + self.record_mode_mi(r * (SUB / MI), c * (SUB / MI), w / MI, h / MI, mode); self.record_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h, grids); } @@ -2644,6 +2700,7 @@ impl Neighbours { } let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); let side_mi = side / MI; + self.record_mode_mi(mi_r, mi_c, side_mi, side_mi, mode); for cell in 0..side_mi { self.left_side_mi[mi_r + cell] = side; self.above_side_mi[mi_c + cell] = side; @@ -3590,6 +3647,16 @@ fn decode_leaf_rect( left_mode = pmode; } } + // Same mi-exact override [`decode_leaf8`] takes: one coarse 16x16 slot + // cannot hold the modes a split (or a pair of strips) leaves behind, so + // when the map holds the block at exactly this strip's above/left mi it + // is the neighbour libaom's `above_mi`/`left_mi` reads. + if let Some(m) = neighbours.mode_above_mi(leaf_mi.0, leaf_mi.1) { + above_mode = m; + } + if let Some(m) = neighbours.mode_left_mi(leaf_mi.0, leaf_mi.1) { + left_mode = m; + } let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); if smooth_neighbor { @@ -3726,6 +3793,9 @@ fn decode_leaf_rect( neighbours.record_mi_rect(leaf_mi, bw, bh, &[luma_levels, u_levels, v_levels]); neighbours.fill_skip_grid_rect(leaf_mi, mi_w, mi_h, skip); neighbours.fill_lf_grid_rect(leaf_mi, mi_w, mi_h, bw as u8, bh as u8, 0); + // A rect leaf spans mi_w x mi_h, so its mode reaches several columns and + // rows -- record the whole span, not one cell. + neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, mi_w, mi_h, mode); Ok(mode) } @@ -5309,6 +5379,16 @@ fn decode_leaf8( left_mode = pmode; } } + // The mi-exact neighbour wins over both the coarse 16x16 slot and + // `prev_leaf`: one 16x16 slot cannot hold the two different modes its + // two 8x8 (or four 4x4) columns leave behind, so a leaf whose above/left + // neighbour sits in a split cell read the wrong `kf_y_mode` row. + if let Some(m) = neighbours.mode_above_mi(leaf_mi.0, leaf_mi.1) { + above_mode = m; + } + if let Some(m) = neighbours.mode_left_mi(leaf_mi.0, leaf_mi.1) { + left_mode = m; + } // See [`PlaneBuf::reconstruct`]'s doc: chroma stays `false` (exact) until // a smooth/paeth `uv_mode` can reach decode at all. let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); @@ -5596,11 +5676,13 @@ fn decode_leaf8( } neighbours.fill_skip_grid(leaf_mi, 2, skip); neighbours.fill_lf_grid(leaf_mi, 2, 4, 0); + neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); return Ok(mode); } neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); neighbours.fill_skip_grid(leaf_mi, 2, skip); neighbours.fill_lf_grid(leaf_mi, 2, 8, 0); + neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); Ok(mode) } diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index b84fdf92..857eeff6 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1020,7 +1020,7 @@ pub(crate) struct Reach { /// it. Indexed by the block's position in the superblock, in blocks of its own /// size, as `row * (128 / side) + col` (`Reach::table_stride`) -- libaom's /// 128, not this crate's 64 superblock -- bit by bit from the low end. -const HAS_TOP_RIGHT: [&[u8]; 3] = [ +const HAS_TOP_RIGHT: [&[u8]; 4] = [ &[255, 85, 119, 85, 127, 85, 119, 85], &[95, 87], // has_tr_8x8 (libaom av1/common/reconintra.c) -- table_stride(8) == 16 @@ -1029,11 +1029,28 @@ const HAS_TOP_RIGHT: [&[u8]; 3] = [ 255, 255, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85, 255, 127, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85, ], + + // has_tr_4x4 (libaom av1/common/reconintra.c, transcribed verbatim) -- + // table_stride(4) == 32 (128 / 4), so 32 bits per row, 32 rows, 128 + // bytes. Sub-8x8 leaves (lane-sub8) are the only blocks that reach here + // with side 4 outside a TX4 split; without their own row they clamped + // into the 8x8 table and read above-right samples the decoder has not + // written yet (all-zero pixels in the bottom-right triangle of a + // directional 4x4 leaf). + &[ + 255, 255, 255, 255, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 127, 127, 127, 127, + 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 255, 127, 255, 127, 85, 85, 85, 85, + 119, 119, 119, 119, 85, 85, 85, 85, 127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, + 85, 85, 85, 85, 255, 255, 255, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, + 127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 255, 127, 255, 127, + 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 127, 127, 127, 127, 85, 85, 85, 85, + 119, 119, 119, 119, 85, 85, 85, 85, + ], ]; /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the /// left. -const HAS_BOTTOM_LEFT: [&[u8]; 3] = [ +const HAS_BOTTOM_LEFT: [&[u8]; 4] = [ &[84, 16, 84, 0, 84, 16, 84, 0], &[4, 4], // has_bl_8x8 (libaom av1/common/reconintra.c) @@ -1041,6 +1058,16 @@ const HAS_BOTTOM_LEFT: [&[u8]; 3] = [ 84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0, 84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0, ], + + // has_bl_4x4 (same source, same order). + &[ + 84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 1, 1, 1, 84, 85, 85, 85, 16, 17, 17, 17, + 84, 85, 85, 85, 0, 0, 1, 0, 84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 1, 1, 1, 84, + 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 0, 0, 0, 84, 85, 85, 85, 16, 17, 17, 17, 84, + 85, 85, 85, 0, 1, 1, 1, 84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 0, 1, 0, 84, 85, + 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 1, 1, 1, 84, 85, 85, 85, 16, 17, 17, 17, 84, 85, + 85, 85, 0, 0, 0, 0, + ], ]; /// `has_tr_32x16`/`has_tr_16x32` (libaom `reconintra.c`) -- lane-intradisp @@ -1051,6 +1078,34 @@ const HAS_TOP_RIGHT_RECT: [&[u8]; 2] = [&[15, 5, 7, 5], &[255, 119, 127, 119]]; /// `has_bl_32x16`/`has_bl_16x32`, same source, same order. const HAS_BOTTOM_LEFT_RECT: [&[u8]; 2] = [&[78, 14, 78, 14], &[16, 0, 16, 0]]; +/// libaom `reconintra.c`'s `has_tr_*`/`has_bl_*` for the four rectangular +/// block sizes this decoder codes, transcribed verbatim (lane-rectx r4). +/// The 16x8/8x16 rows are what the 32x16/16x32 pair above cannot stand in +/// for: they are 16 bytes, not 4, and their bit pattern is a different +/// coding order entirely -- reading the 16x32 table for a 16x8 block gave +/// the wrong bottom-left availability and a directional (D203) strip whose +/// prediction diverged exactly where it starts reading below its own height. +/// `has_tr_vert_tables`/`has_bl_vert_tables` hold the SAME pointers as these +/// for every WH (no VERT_A/B ever produces one), +/// so the partition type does not select a different table at these sizes. +fn has_tr_rect_table(bw: usize, bh: usize) -> &'static [u8] { + match (bw, bh) { + (16, 8) => &[255, 0, 85, 0, 119, 0, 85, 0, 127, 0, 85, 0, 119, 0, 85, 0], + (8, 16) => &[255, 255, 119, 119, 127, 127, 119, 119, 255, 127, 119, 119, 127, 127, 119, 119], + (32, 16) => &[15, 5, 7, 5], + _ => &[255, 119, 127, 119], + } +} + +fn has_bl_rect_table(bw: usize, bh: usize) -> &'static [u8] { + match (bw, bh) { + (16, 8) => &[254, 84, 254, 16, 254, 84, 254, 0, 254, 84, 254, 16, 254, 84, 254, 0], + (8, 16) => &[16, 17, 0, 1, 16, 17, 0, 0, 16, 17, 0, 1, 16, 17, 0, 0], + (32, 16) => &[78, 14, 78, 14], + _ => &[16, 0, 16, 0], + } +} + impl Reach { /// What a block of `side` samples at `(x, y)` may read past its own edges, /// in a frame of `width` by `height`. @@ -1080,37 +1135,49 @@ impl Reach { } } - fn position_rect(bw: usize, bh: usize, x: usize, y: usize) -> (usize, usize, usize, usize) { - ( - (y % SUPERBLOCK) / bh, - (x % SUPERBLOCK) / bw, - SUPERBLOCK / bh, - SUPERBLOCK / bw, - ) - } - + /// libaom `has_top_right` at block granularity (`row_off`/`col_off` 0, + /// the transform covering the whole block, luma): everything before the + /// table lookup is that function's own early-exit ladder. fn top_right_rect(bw: usize, bh: usize, x: usize, y: usize) -> bool { - let (row, col, _per_row, per_col) = Self::position_rect(bw, bh, x, y); - if row == 0 { + const SB_MI: usize = SUPERBLOCK / 4; + let (mi_row, mi_col) = (y / 4, x / 4); + let bw_log2 = (bw / 4).trailing_zeros() as usize; + let bh_log2 = (bh / 4).trailing_zeros() as usize; + let blk_row = (mi_row & (SB_MI - 1)) >> bh_log2; + let blk_col = (mi_col & (SB_MI - 1)) >> bw_log2; + // Top row of the superblock: the top-right pixels are in the (already + // decoded) superblock above. + if blk_row == 0 { return true; } - if col + 1 == per_col { + // Rightmost column: they fall in the superblock to the right. + if ((blk_col + 1) << bw_log2) >= SB_MI { return false; } - let table = HAS_TOP_RIGHT_RECT[usize::from(bw == 16)]; - Self::bit(table, (row * Self::table_stride(bw) + col) % (table.len() * 8)) + let table = has_tr_rect_table(bw, bh); + Self::bit(table, (blk_row << (4 - bw_log2)) + blk_col) } + /// libaom `has_bottom_left`, same granularity as [`Self::top_right_rect`]. fn bottom_left_rect(bw: usize, bh: usize, x: usize, y: usize) -> bool { - let (row, col, per_row, _per_col) = Self::position_rect(bw, bh, x, y); - if col == 0 { - return row * bh + bh < SUPERBLOCK; + const SB_MI: usize = SUPERBLOCK / 4; + let (mi_row, mi_col) = (y / 4, x / 4); + let bw_log2 = (bw / 4).trailing_zeros() as usize; + let bh_log2 = (bh / 4).trailing_zeros() as usize; + let blk_row = (mi_row & (SB_MI - 1)) >> bh_log2; + let blk_col = (mi_col & (SB_MI - 1)) >> bw_log2; + // Leftmost column of the superblock: the bottom-left pixels are in + // the left superblock iff they stay inside its height. + if blk_col == 0 { + return (blk_row << bh_log2) + (bh / 4) < SB_MI; } - if row + 1 == per_row { + // Bottom row (and not the leftmost column): they fall in the + // superblock below, which is not decoded yet. + if ((blk_row + 1) << bh_log2) >= SB_MI { return false; } - let table = HAS_BOTTOM_LEFT_RECT[usize::from(bw == 16)]; - Self::bit(table, (row * Self::table_stride(bw) + col) % (table.len() * 8)) + let table = has_bl_rect_table(bw, bh); + Self::bit(table, (blk_row << (4 - bw_log2)) + blk_col) } /// Neither, which is all a mode that reads no further than its own edges @@ -1134,17 +1201,9 @@ impl Reach { if col + 1 == per_side { return false; } - // corner-cut: `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` only carry dedicated - // rows for side 8/16/32/64 (libaom's own `has_tr_8x8`/`has_tr_16x16` - // families); a `TxMode::Select` TX4 transform unit reaches here with - // side 4, whose real table (`has_tr_4x4`, 128 bytes) was never - // transcribed. Clamping into the 16/32 table's own bit range keeps - // this in-bounds and correct at every position that table already - // covers periodically -- wrong only at the small remainder past its - // length, a reach over-/under-estimate (extra or missing above-right - // reference pixels), never a stream desync (Reach never gates a - // symbol read). Ceiling: transcribe `has_tr_4x4`/`has_bl_4x4` - // verbatim and give TX4 its own `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` row. + // lane-sub8 r4: side 4 now has its own `has_tr_4x4`/`has_bl_4x4` + // row (the corner-cut this comment used to describe); sides 16/32/64 + // share row 0 exactly as libaom's tables repeat. let table = HAS_TOP_RIGHT[Self::table(side)]; Self::bit( table, @@ -1164,7 +1223,7 @@ impl Reach { if row + 1 == per_side { return false; } - // corner-cut: see `top_right`'s note -- same clamp, same ceiling. + // See `top_right`'s note. let table = HAS_BOTTOM_LEFT[Self::table(side)]; Self::bit( table, @@ -1200,6 +1259,8 @@ impl Reach { 1 } else if side == 8 { 2 + } else if side == 4 { + 3 } else { 0 } diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 96b71d59..8e43d92a 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -5370,7 +5370,7 @@ mod tests { "-f", "lavfi", "-i", - &format!("mandelbrot=size={width}x{height}:start_x=0.4"), + &format!("mandelbrot=size={width}x{height}:start_x=-0.6"), "-pix_fmt", "yuv420p", "-t", @@ -5393,7 +5393,7 @@ mod tests { "--codec=av1", "--passes=1", "--end-usage=q", - "--cq-level=24", + "--cq-level=16", "--cpu-used=4", "--threads=1", "--row-mt=0", @@ -5403,6 +5403,11 @@ mod tests { "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-tx-size-search=0", + // Filter intra on a strip is a separate, still-refused + // predictor (`filter intra on a HORZ/VERT strip`); this + // gate is about the rect RESIDUAL path, so it is off here + // and covered by the square filter-intra gate instead. + "--enable-filter-intra=0", "--reduced-tx-type-set=1", "--min-partition-size=8", "--max-partition-size=32",