From 13af755223bd52528449add43ea14b08d114d31b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 19 Sep 2026 20:39:32 +0300 Subject: [PATCH] av1: WIP monochrome plane model (key-frame entropy syncs; luma not yet byte-exact) --- crates/ec-av1/examples/decode_probe.rs | 13 ++- crates/ec-av1/src/decode.rs | 146 ++++++++++++++++++++++--- crates/ec-av1/src/stream.rs | 8 ++ lanes/av1mono.report.md | 104 ++++++++++++++++++ 4 files changed, 249 insertions(+), 22 deletions(-) create mode 100644 lanes/av1mono.report.md diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index 5f16d35e..771ade05 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -266,9 +266,10 @@ fn main() { // triage needs. if let Some(seq) = parser.sequence_header() { println!( - "SEQ: use_128x128_superblock={} bit_depth={} max_frame={}x{}", + "SEQ: use_128x128_superblock={} bit_depth={} mono_chrome={} max_frame={}x{}", seq.use_128x128_superblock, seq.color_config.bit_depth, + seq.color_config.mono_chrome, seq.max_frame_width, seq.max_frame_height ); @@ -310,6 +311,9 @@ fn main() { let mut shown = 0usize; let mut dims = (0usize, 0usize); let mut buf: Vec = Vec::new(); + // lane-av1mono: a monochrome stream is luma only, so the raw dump is a + // single plane -- exactly what `ffmpeg -pix_fmt gray -f rawvideo` writes. + let mono = parser.sequence_header().is_some_and(|s| s.color_config.mono_chrome); let result = ec_av1::stream::decode_stream_with(&data, |f, _idx, is_shown| { if !is_shown { return Ok(()); @@ -318,8 +322,9 @@ fn main() { if shown == 1 { dims = (f.width, f.height); } + let planes: &[&Vec] = if mono { &[&f.y] } else { &[&f.y, &f.u, &f.v] }; if let Some(w) = f16.as_mut() { - let n = 2 * (f.y.len() + f.u.len() + f.v.len()); + let n = 2 * planes.iter().map(|p| p.len()).sum::(); if null16 { // Timing runs point EC_PROBE_OUT16 at /dev/null; the u16->LE // conversion was ~2% of the main thread for bytes nobody reads. @@ -328,7 +333,7 @@ fn main() { buf.clear(); buf.resize(n, 0); let mut off = 0; - for p in [&f.y, &f.u, &f.v] { + for p in planes { let end = off + 2 * p.len(); for (d, &s) in buf[off..end].chunks_exact_mut(2).zip(p.iter()) { d.copy_from_slice(&s.to_le_bytes()); @@ -341,7 +346,7 @@ fn main() { } if let Some(w) = f8.as_mut() { buf.clear(); - for p in [&f.y, &f.u, &f.v] { + for p in planes { buf.extend(p.iter().map(|&s| s as u8)); } w.write_all(&buf).expect("writing raw planes"); diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 8ba409de..d7f6068f 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -86,6 +86,16 @@ pub(crate) struct FrameCtx { pub(crate) lossless_flag: std::cell::Cell, pub(crate) cdef_bits: std::cell::Cell, pub(crate) bit_depth: std::cell::Cell, + /// lane-av1mono: the sequence header's `color_config.mono_chrome` (spec + /// 5.5.2), i.e. `NumPlanes == 1`. libaom threads this as + /// `av1_num_planes(cm)` and every chroma read site is guarded by + /// `!cm->seq_params->monochrome && xd->is_chroma_ref` (`decodemv.c:933`, + /// `:1208`) or bounded by `plane < num_planes`; a monochrome frame codes + /// NO `uv_mode`/`cfl`/`angle_delta_uv`/`palette_uv` symbol and NO chroma + /// coefficients on any block. Set once per frame from the sequence header + /// (like [`FrameCtx::bit_depth`]), so a mono frame decoded on a thread + /// that last saw a 4:2:0 one never inherits the wrong plane count. + pub(crate) mono_chrome: std::cell::Cell, pub(crate) superres: std::cell::Cell<(u32, u32)>, pub(crate) cdef_transmitted: std::cell::Cell, pub(crate) cdef_sb_cols: std::cell::Cell, @@ -175,6 +185,7 @@ impl FrameCtx { lossless_flag: std::cell::Cell::new(false), cdef_bits: std::cell::Cell::new(0), bit_depth: std::cell::Cell::new(8), + mono_chrome: std::cell::Cell::new(false), superres: std::cell::Cell::new((0, 8)), cdef_transmitted: std::cell::Cell::new(false), cdef_sb_cols: std::cell::Cell::new(0), @@ -269,6 +280,7 @@ impl WithRef for T {} pub(crate) fn filter_ctx_copy(fctx: &FrameCtx) -> FrameCtx { let out = FrameCtx::new(); out.bit_depth.set(fctx.bit_depth.get()); + out.mono_chrome.set(fctx.mono_chrome.get()); out.interp_filter.set(fctx.interp_filter.get()); out.grain_bit_depth.set(fctx.grain_bit_depth.get()); out.superres.set(fctx.superres.get()); @@ -566,6 +578,19 @@ pub(crate) fn set_bit_depth(bit_depth: u8, fctx: &crate::decode::FrameCtx) { fctx.bit_depth.with(|c| c.set(bit_depth)); } +/// This stream's `mono_chrome` (see [`FrameCtx::mono_chrome`]): a +/// monochrome frame has `NumPlanes == 1`, so no block codes any chroma +/// symbol or chroma coefficient and the output picture is luma only. +pub(crate) fn mono(fctx: &crate::decode::FrameCtx) -> bool { + fctx.mono_chrome.with(std::cell::Cell::get) +} + +/// Sets [`mono`] for the current thread, called once per frame from +/// [`crate::stream`]'s decode loop like [`set_bit_depth`]. +pub(crate) fn set_mono(mono_chrome: bool, fctx: &crate::decode::FrameCtx) { + fctx.mono_chrome.with(|c| c.set(mono_chrome)); +} + /// This frame's superres state: `(upscaled_width, superres_denom)`, or /// `(0, 8)` when the frame is not scaled. Spec 7.16's upscale runs /// between CDEF and loop restoration, so LOOP RESTORATION -- unit grid, @@ -9047,6 +9072,12 @@ fn read_intra_mode_rect( Option, Option, )> { + // lane-av1mono: libaom guards every chroma symbol read with + // `!cm->seq_params->monochrome && xd->is_chroma_ref` (`decodemv.c:933`), + // so a monochrome block codes no `uv_mode`/`cfl`/`angle_delta_uv`/ + // `palette_uv` even on a chroma-reference strip. Fold the plane count + // into the same `has_chroma` this reader already gates on. + let has_chroma = has_chroma && !mono(fctx); let ec_istep = crate::envflags::env_flag!("EC_TRACE_MODE_STEP"); if ec_istep { // Mirrors the oracle's own `EC_IMODE mi_row=.. mi_col=.. rng=..` line @@ -9805,7 +9836,14 @@ fn decode_rect_split( let mut ll_chroma_units: Vec<((usize, usize), usize, Grid)> = Vec::new(); let ac = m.alpha.map(|_| cfl_src_rect(px, py, bw, bh)); let reach = block_reach; - let (u_grid, v_grid) = if m.skip { + let (u_grid, v_grid) = if mono(fctx) { + // lane-av1mono: a monochrome frame codes no chroma coefficients on + // any block (`plane < av1_num_planes == 1`), so the whole chroma + // section -- the skipped-strip pushes, the lossless raster and the + // un-split `read_coeffs_rect` below -- is bypassed. + let zero = Grid::Zero(chroma_w * chroma_h); + (zero.clone(), zero) + } else if m.skip { let zero = Grid::Zero(chroma_w * chroma_h); if let Some((ub, _)) = &m.palette_uv { set_palette_pred(ub.clone(), fctx); @@ -10456,8 +10494,14 @@ fn decode_intra_rect_in_inter( // `is_cfl_allowed` (spec 5.11.5) caps CFL at 32x32, so the // superblock-level strip reads the no-CFL alphabet -- the same split // [`read_intra_mode_rect`] makes for [`decode_block_rect64`]. - let cfl_allowed = cfl_allowed_px(bw, bh, fctx); - let uv_mode = if cfl_allowed { + // + // lane-av1mono: a monochrome strip codes no chroma mode/CFL symbol + // (libaom `decodemv.c:933`). + let has_chroma = !mono(fctx); + let cfl_allowed = has_chroma && cfl_allowed_px(bw, bh, fctx); + let uv_mode = if !has_chroma { + DC_PRED + } else if cfl_allowed { dec.symbol(&mut cdfs.uv_mode_cfl[mode]) } else { dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) @@ -10518,8 +10562,11 @@ fn decode_intra_rect_in_inter( } // A 2:1 strip of at least 8x8 is always a chroma reference, so // `read_palette_mode_info`'s `xd->is_chroma_ref` guard holds here. - let use_palette_uv = - uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[usize::from(use_palette_y)]) != 0; + // lane-av1mono: libaom's actual guard is `num_planes > 1 && + // uv_mode == UV_DC_PRED && xd->is_chroma_ref` (decodemv.c:602). + let use_palette_uv = has_chroma + && uv_mode == DC_PRED + && dec.symbol(&mut cdfs.palette_uv_mode[usize::from(use_palette_y)]) != 0; if crate::envflags::env_flag!("EC_PALSYN") { eprintln!("EC_PALSYN rect mi={mi_r},{mi_c} bw={bw} bh={bh} mode={mode} uv={uv_mode} ctx={palette_ctx} y={} uv={} rng={}", use_palette_y as i32, use_palette_uv as i32, dec.debug_state().0); } @@ -13270,11 +13317,15 @@ fn read_intra_mode( 0 }; istep!("angle_y", angle_delta_y); - // lane-sb128c r3 (class sweep): `is_cfl_allowed` on the luma bsize, decided - // here so every `decode_block` caller (64x64 and 128x128 roots included) - // picks the right alphabet -- see [`read_intra_mode_rect`]'s twin. - let cfl = cfl && cfl_allowed_px(side, side, fctx); - let uv_mode = if cfl { + // lane-av1mono: a monochrome block codes no chroma mode/CFL/palette + // symbol at all (libaom `decodemv.c:933`'s `!monochrome && is_chroma_ref` + // guard). Fold `NumPlanes == 1` into the same `has_chroma` this reader + // gates its chroma symbols on. + let has_chroma = !mono(fctx); + let cfl = cfl && has_chroma && cfl_allowed_px(side, side, fctx); + let uv_mode = if !has_chroma { + DC_PRED + } else if cfl { dec.symbol(&mut cdfs.uv_mode_cfl[mode]) } else { dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) @@ -13375,8 +13426,16 @@ fn read_intra_mode( // scope always reconstructs chroma alongside luma (`decode_block` // always calls `u.reconstruct`/`v.reconstruct`), so `is_chroma_ref` // is always true here and does not need threading in. + // + // lane-av1mono: libaom's actual guard is `num_planes > 1 && + // uv_mode == UV_DC_PRED && xd->is_chroma_ref` (decodemv.c:602), so a + // monochrome frame reads NO `palette_uv_mode`/`palette_uv_size` + // symbol even at this chroma-owning square block. let palette_uv_mode_ctx = usize::from(use_palette_y); - if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 { + if has_chroma + && uv_mode == DC_PRED + && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 + { let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]); let (u_colors, v_colors) = read_palette_colors_uv(dec, n, palette_uv_cache, fctx); palette_uv_pending = Some((n, u_colors, v_colors)); @@ -14038,6 +14097,14 @@ fn read_plane( // See [`PlaneBuf::reconstruct`]'s own doc: caller's neighbour-smooth check. smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, ) -> Result { + // lane-av1mono: a monochrome frame has `NumPlanes == 1`, so libaom's + // coefficient loop (`for (plane = 0; plane < av1_num_planes(cm); ++plane)`) + // never reaches a chroma plane -- no u/v coefficient symbol is coded and + // no chroma write is recorded. Return the all-zero grid the caller's + // neighbour bookkeeping reads, without consuming a bit. + if plane_idx > 0 && mono(fctx) { + return Ok(Grid::Zero(side * side)); + } let skip_ctx = if plane_idx == 0 { luma_skip_ctx.unwrap_or(0) } else { @@ -15916,6 +15983,9 @@ fn read_intra_mode_sub8( seg_w_mi: usize, seg_h_mi: usize, fctx: &crate::decode::FrameCtx, ) -> Result<(bool, usize, i32, Option<(usize, i32, Option<(i32, i32)>)>, Option)> { + // lane-av1mono: a monochrome leaf codes no chroma mode (libaom + // `decodemv.c:933`), so fold `NumPlanes == 1` into `has_chroma`. + let has_chroma = has_chroma && !mono(fctx); let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); let ec_istep = crate::envflags::env_flag!("EC_TRACE_MODE_STEP"); if ec_istep { @@ -16131,6 +16201,11 @@ fn decode_leaf_split4( last_uv = chroma; } } + // lane-av1mono: as [`decode_leaf_rect8`] -- a monochrome frame has no + // chroma to reconstruct and no uv_mode was read. + let (u_grid, v_grid): (Grid, Grid) = if mono(fctx) { + (Grid::Zero(16), Grid::Zero(16)) + } else { let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI); let (cpx, cpy) = (gpx / 2, gpy / 2); let group_reach = Reach::of(8, gpx, gpy, y.width, y.height, fctx); @@ -16172,6 +16247,8 @@ fn decode_leaf_split4( )?; (ug, vg) }; + (u_grid, v_grid) + }; // Chroma neighbour bookkeeping for the whole 8x8 group -- mirrors // [`decode_leaf8`]'s own tx4-chroma section (`record_split_luma`'s // rescaling is wrong for an already-mi-unit position, so this inlines @@ -16518,6 +16595,12 @@ fn decode_leaf_rect8( last_uv = chroma; } } + // lane-av1mono: a monochrome frame codes no chroma at all -- no + // `uv_mode` was read ([`read_intra_mode_sub8`] folds the plane count in), + // so there is no chroma to reconstruct and no chroma coefficient to read. + let (u_grid, v_grid): (Grid, Grid) = if mono(fctx) { + (Grid::Zero(16), Grid::Zero(16)) + } else { let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI); let (cpx, cpy) = (gpx / 2, gpy / 2); let group_reach = Reach::of(8, gpx, gpy, y.width, y.height, fctx); @@ -16559,6 +16642,8 @@ fn decode_leaf_rect8( )?; (ug, vg) }; + (u_grid, v_grid) + }; // `update_partition_context` at the 8x8 level writes // `partition_context_lookup[subsize]` over the whole 8x8 span: BLOCK_4X8 // is `{31, 30}` (bit0 set on the above half only), BLOCK_8X4 `{30, 31}` @@ -18498,6 +18583,11 @@ fn read_inter_plane_rect( inherited_luma_tx_type: Option, luma_skip_ctx: Option, fctx: &crate::decode::FrameCtx, ) -> Result<(Grid, TxType)> { + // lane-av1mono: as [`read_plane`] -- a monochrome frame codes no chroma + // coefficients (`plane < av1_num_planes == 1`). + if plane_idx > 0 && mono(fctx) { + return Ok((Grid::Zero(w * h), TxType::DctDct)); + } let skip_ctx = if plane_idx == 0 { // `get_txb_ctx_general`: 0 only when this unit IS the whole plane // block; a var-tx leaf passes its own neighbour-table context. @@ -24737,6 +24827,11 @@ fn read_inter_plane( // lone-transform-unit 0. luma_skip_ctx: Option, fctx: &crate::decode::FrameCtx, ) -> Result<(Grid, TxType)> { + // lane-av1mono: as [`read_plane`] -- a monochrome frame codes no chroma + // coefficients (`plane < av1_num_planes == 1`). + if plane_idx > 0 && mono(fctx) { + return Ok((Grid::Zero(side * side), TxType::DctDct)); + } let skip_ctx = if plane_idx == 0 { luma_skip_ctx.unwrap_or(0) } else { @@ -29747,11 +29842,16 @@ fn decode_inter_block( // the 14-symbol CFL one gave the same DC_PRED VALUE but narrowed the // arithmetic coder by the wrong interval, desyncing the tile from this // block on. - let cfl_allowed = cfl_allowed_px(write_w, write_h, fctx); + // lane-av1mono: a monochrome block codes no chroma mode/CFL symbol + // (libaom `decodemv.c:933`). + let has_chroma = !mono(fctx); + let cfl_allowed = has_chroma && cfl_allowed_px(write_w, write_h, fctx); if !cfl_allowed { hit!(NOCFL_UV_MODE_HITS); } - let uv_mode = if cfl_allowed { + let uv_mode = if !has_chroma { + DC_PRED + } else if cfl_allowed { dec.symbol(&mut cdfs.uv_mode_cfl[mode]) } else { dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) @@ -29822,8 +29922,9 @@ fn decode_inter_block( if crate::envflags::env_flag!("EC_PALSYN") { eprintln!("EC_PALSYN_PRE sq mi={rmi},{cmi} bw={write_w} bh={write_h} mode={mode} uv={uv_mode} ctx={palette_ctx} y={} rng={}", palette_y_pending.map_or(0, |(n, _)| n), dec.debug_state().0); } - let use_palette_uv = - uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[usize::from(use_palette_y)]) != 0; + let use_palette_uv = has_chroma + && uv_mode == DC_PRED + && dec.symbol(&mut cdfs.palette_uv_mode[usize::from(use_palette_y)]) != 0; if crate::envflags::env_flag!("EC_PALSYN") { eprintln!("EC_PALSYN sq mi={rmi},{cmi} bw={write_w} bh={write_h} mode={mode} uv={uv_mode} ctx={palette_ctx} y={} uv={} rng={}", use_palette_y as i32, use_palette_uv as i32, dec.debug_state().0); } @@ -31177,6 +31278,9 @@ fn decode_intra_sub8_leaf( tx_select: bool, reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, ) -> Result { + // lane-av1mono: a monochrome leaf codes no chroma mode (libaom + // `decodemv.c:933`). + let has_chroma = has_chroma && !mono(fctx); let vert = bw < bh; let (w_mi, h_mi) = (bw / MI, bh / MI); let (gr, gc) = group_mi; @@ -33871,7 +33975,12 @@ fn decode_inter_block8( // row (`get_uv_mode`, identity over `V_PRED..=D67_PRED`). Every value // but `DC_PRED` was refused here, which is what blocked the low-cq // CDEF arms and the 10-bit gates. - let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]); + let has_chroma = !mono(fctx); + let uv_mode = if !has_chroma { + DC_PRED + } else { + dec.symbol(&mut cdfs.uv_mode_cfl[mode]) + }; if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { eprintln!( "EC_ISTEP8 mi_row={} mi_col={} name=modes y={mode} uv={uv_mode} fi_enabled={} rng={}", @@ -33885,7 +33994,7 @@ fn decode_inter_block8( hit!(SMOOTH_UV_HITS); hit!(INTRA_IN_INTER8_UV_SMOOTH_HITS); } - let alpha = if uv_mode == UV_CFL_PRED { + let alpha = if has_chroma && uv_mode == UV_CFL_PRED { hit!(INTRA_IN_INTER8_UV_CFL_HITS); Some(read_cfl_alphas(dec, cdfs)) } else { @@ -33942,7 +34051,8 @@ fn decode_inter_block8( } // lane-uv8 r1: `read_palette_mode_info` reads the UV symbol only // when `uv_mode == UV_DC_PRED` (decodemv.c). - let use_palette_uv = uv_mode == DC_PRED + let use_palette_uv = has_chroma + && uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[usize::from(use_palette_y)]) != 0; if crate::envflags::env_flag!("EC_PALSYN") { eprintln!( diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index da227e8a..9f224ba3 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -1243,6 +1243,9 @@ pub(crate) struct SeqFlags { pub(crate) enable_interintra_compound: bool, pub(crate) use_sb128: bool, pub(crate) bit_depth: u8, + /// lane-av1mono: `color_config.mono_chrome` (spec 5.5.2): + /// `NumPlanes == 1`, no chroma is coded or reconstructed. + pub(crate) mono_chrome: bool, pub(crate) order_hint_bits: u32, pub(crate) mc_identity: bool, } @@ -1259,6 +1262,7 @@ impl SeqFlags { enable_interintra_compound: seq.is_some_and(|s| s.enable_interintra_compound), use_sb128: seq.is_some_and(|s| s.use_128x128_superblock), bit_depth: seq.map_or(8, |s| s.color_config.bit_depth), + mono_chrome: seq.is_some_and(|s| s.color_config.mono_chrome), order_hint_bits: seq.map_or(0, |s| s.order_hint_bits), mc_identity: seq.is_some_and(|s| s.color_config.matrix_coefficients == 0), } @@ -1633,6 +1637,10 @@ fn decode_frame( // `a_lossless_libaom_inter_frame_decodes_sample_exact`. crate::decode::set_lossless(coded_lossless, fctx); crate::decode::set_bit_depth(bit_depth, fctx); + // lane-av1mono: the sequence header's `mono_chrome`, set per frame like + // the bit depth so a mono frame decoded on a thread that last saw a + // 4:2:0 one never inherits the wrong plane count. + crate::decode::set_mono(seq.mono_chrome, fctx); // spec 7.16: this frame's superres state, for the filter chain -- // `decode.rs` upscales between CDEF and loop restoration and sizes // the LR unit grid in upscaled coordinates. Cleared (0, 8) on an diff --git a/lanes/av1mono.report.md b/lanes/av1mono.report.md new file mode 100644 index 00000000..e8331799 --- /dev/null +++ b/lanes/av1mono.report.md @@ -0,0 +1,104 @@ +# lane-av1-mono (WIP) — monochrome AV1 key-frame decompiles; luma not yet byte-exact + +Base: `main` **f0727a87** (2026-09-19). Worktree `edith_codecs-av1mono`, branch +`lane-av1-mono`, `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-av1mono` (private). +No push. Status: **INCOMPLETE** — entropy now syncs through a real monochrome +key frame (the pilot's first-wall refusal is gone), but the reconstructed luma is +NOT yet byte-exact against ffmpeg; a small block region diverges. + +## TL;DR + +The pilot (`lanes/av1decode.report.md`, branch `lane-av1-decode`) root-caused the +first monochrome stop to libaom `decodemv.c:933`'s +`!monochrome && is_chroma_ref` guard on the chroma mode symbols. This lane ports +the monochrome plane model into ec-av1: + +- new `FrameCtx::mono_chrome` (from the sequence header's `color_config.mono_chrome`), +- every chroma **symbol** read gated (uv_mode/cfl/angle_uv/palette_uv), +- every chroma **coefficient** read gated (`read_plane`/`read_inter_plane`/ + `read_inter_plane_rect`/`decode_rect_split`'s un-split `read_coeffs_rect`), +- the probe writes a single luma plane for a mono sequence. + +**Result: the gray/key-frame refusal `a Golomb tail longer than this decoder +reads` is GONE — `decode_probe` now returns `OK: 60 frames decoded` for both the +pilot's gray recipe and `fixtures/bitstreams/av1-monochrome.ivf`.** The 4:2:0 +twin still decodes 60/60. + +**Remaining defect:** the decoded luma is NOT byte-exact. First divergence on the +pilot's gray stream is frame 0, row 0, column 37 (off-by-one), and a small +~6×2-pixel block region at row 1, cols 36-42 decodes wrong (probe 20/19 vs ref +76). Because entropy stays in sync (all 60 frames decode, no refusal) this is a +**luma reconstruction/prediction** defect, not an entropy desync — most likely a +block whose intra prediction reads a neighbour state that the mono path no longer +updates, or a residual/palette interaction on a chroma-adjacent block. This needs +one more round. + +## Changes (uncommitted→committed on `lane-av1-mono`) + +`crates/ec-av1/src/decode.rs` +- `FrameCtx::mono_chrome: Cell` + `mono(fctx)` / `set_mono(...)` helpers; + copied in `filter_ctx_copy`. +- `read_intra_mode_rect`: `has_chroma &= !mono` (gates uv_mode/cfl/angle_uv/palette_uv). +- `read_intra_mode` (square), `decode_intra_rect_in_inter`, `decode_inter_block` + intra-in-inter, `decode_inter_block8` intra-in-inter: explicit mono branch on + the chroma mode read + palette_uv gate. +- `read_intra_mode_sub8`, `decode_intra_sub8_leaf`: `has_chroma &= !mono`. +- `read_plane`, `read_inter_plane`, `read_inter_plane_rect`: early-return + `Grid::Zero` for `plane_idx > 0 && mono` (no coefficient symbol consumed). +- `decode_rect_split`: whole chroma section bypassed for mono (its un-split path + read chroma through `read_coeffs_rect` directly, NOT through `read_plane`, so + the reader gate did not cover it — this was a real desync source). +- `decode_leaf_rect8` / `decode_leaf_split4`: chroma reconstruction tail + bypassed for mono (the `last_uv.expect("…has_chroma…")` panics otherwise). + +`crates/ec-av1/src/stream.rs` +- `SeqFlags::mono_chrome` from `sequence_header().color_config.mono_chrome`; + `set_mono(...)` called per frame in `decode_frame` (like `set_bit_depth`). + +`crates/ec-av1/examples/decode_probe.rs` +- prints `mono_chrome` in the `SEQ:` line; writes only the luma plane when the + sequence is monochrome (so the raw dump is what `ffmpeg -pix_fmt gray -f + rawvideo` writes). + +## Repro + +``` +# generate (pilot recipe) +ffmpeg -v error -f lavfi -i "testsrc2=size=320x240:rate=30:duration=2" \ + -pix_fmt gray -c:v libaom-av1 -cpu-used 8 -b:v 300k -f obu -y /tmp/gray.obu +ffmpeg -v error -i fixtures/bitstreams/av1-monochrome.ivf -c:v copy -f obu -y /tmp/mono.obu + +P=$HOME/.cache/cargo-target-av1mono/release/examples/decode_probe +EC_PROBE_OUT16=$HOME/m.raw16 $P /tmp/gray.obu # OK: 60 frames (was REFUSED) +ffmpeg -v error -i /tmp/gray.obu -pix_fmt gray -f rawvideo $HOME/ref.gray +python3 -c "d=open('$HOME/m.raw16','rb').read();print(bytes(d[0::2])==open('$HOME/ref.gray','rb').read())" +# -> False today; first differing byte at index 37. +``` + +## Verified + +- `decode_probe` gray recipe: `OK: 60 frames decoded, 320x240` (was REFUSED). +- `decode_probe` `av1-monochrome.ivf` (remuxed to OBU): `OK: 60 frames` (was REFUSED). +- 4:2:0 twin of the same recipe: `OK: 60 frames` (unchanged). +- Byte-exactness vs ffmpeg gray: **FAILS** (see above). + +## NOT done (blocking acceptance) + +- Byte-exact luma on the three required witnesses (gray recipe, av1-monochrome.ivf, + a 60+ frame inter gray encode). The key-frame luma mismatch must be found first. +- Inter-frame mono streams not yet exercised past the key frame (an earlier run + panicked in `mc.rs:1141` "a reference plane has samples" when the mono Picture + carried empty u/v; that was fixed by keeping the chroma scratch planes in the + Picture and making the probe — not the Picture — luma-only. Inter mono still + needs a byte-exactness gate.) +- The pilot's gate `a_real_libaom_monochrome_key_frame_is_refused_by_name` + (branch `lane-av1-decode`, not on main) must be INVERTED to a pixel-exact + witness once the luma defect is fixed. +- `cargo test -p ec-av1 --lib` suite + `cargo check -p ec-av1 --all-targets` + 0-warning gate not run this session. +- Refusal inventory count not updated. +- Film-window probes not re-run on this build. + +## Deferred / not chased + +- The other 34 refusals (pilot terrain map Wave 2/3) — untouched, per charter.