diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index 6fa8d74e..8c382ab5 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -305,6 +305,58 @@ pub const UV_MODE_CFL: [[u16; 15]; 13] = [ ], ]; +/// `Default_Cfl_Sign_Cdf` (spec 9.4): the joint sign of a `UV_CFL_PRED` +/// block's U/V alpha (spec 5.11.45's `cfl_alpha_signs`) — libaom's +/// `default_cfl_sign_cdf` (entropymode.c), whose `AOM_CDF8(a0..a6)` arguments +/// are this convention's cumulative points verbatim (only the *storage*, not +/// the spec's own table, is `AOM_ICDF`-inverted). +pub const CFL_SIGN: [u16; 9] = [1418, 2123, 13340, 18405, 26972, 28343, 32294, 32768, 0]; + +/// `default_filter_intra_cdfs` (entropymode.c), the `use_filter_intra` flag's +/// prior, indexed by block-size class: `[0]`=4x4, `[1]`=8x8, `[2]`=16x16, +/// `[3]`=32x32 -- the only square sizes `av1_filter_intra_allowed_bsize` +/// (width and height both <=32) ever offers this decoder's square blocks. +pub const FILTER_INTRA: [[u16; 3]; 4] = [ + [4621, 32768, 0], + [7866, 32768, 0], + [12408, 32768, 0], + [22343, 32768, 0], +]; +/// `default_filter_intra_mode_cdf` (entropymode.c): which of the five +/// `FILTER_INTRA_MODES` a `use_filter_intra` block picks. +pub const FILTER_INTRA_MODE: [u16; 6] = [8949, 12776, 17211, 29558, 32768, 0]; + +/// `Default_Cfl_Alpha_Cdf` (spec 9.4): a `UV_CFL_PRED` block's per-plane +/// alpha magnitude (spec 5.11.45's `cfl_alpha_u`/`cfl_alpha_v`), indexed by +/// the joint sign's context (`CFL_CONTEXT_U`/`CFL_CONTEXT_V`, +/// libaom's cfl.h) — libaom's `default_cfl_alpha_cdf`. +pub const CFL_ALPHA: [[u16; 17]; 6] = [ + [ + 7637, 20719, 31401, 32481, 32657, 32688, 32692, 32696, 32700, 32704, 32708, 32712, 32716, + 32720, 32724, 32768, 0, + ], + [ + 14365, 23603, 28135, 31168, 32167, 32395, 32487, 32573, 32620, 32647, 32668, 32672, 32676, + 32680, 32684, 32768, 0, + ], + [ + 11532, 22380, 28445, 31360, 32349, 32523, 32584, 32649, 32673, 32677, 32681, 32685, 32689, + 32693, 32697, 32768, 0, + ], + [ + 26990, 31402, 32282, 32571, 32692, 32696, 32700, 32704, 32708, 32712, 32716, 32720, 32724, + 32728, 32732, 32768, 0, + ], + [ + 17248, 26058, 28904, 30608, 31305, 31877, 32126, 32321, 32394, 32464, 32516, 32560, 32576, + 32593, 32622, 32768, 0, + ], + [ + 14738, 21678, 25779, 27901, 29024, 30302, 30980, 31843, 32144, 32413, 32520, 32594, 32622, + 32656, 32660, 32768, 0, + ], +]; + /// `Default_Txb_Skip_Cdf[2][3][0]` (spec 9.4): the all-zero flag of a 32x32 /// luma transform block that covers its whole plane block, which is context 0. pub const TXB_SKIP_LUMA_32: [u16; 3] = [30669, 32768, 0]; diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index feaaa976..6c831036 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -98,6 +98,10 @@ pub(crate) struct Cdfs { pub uv_mode_no_cfl: [[u16; 14]; 13], /// The chroma mode of a block that is offered it. pub uv_mode_cfl: [[u16; 15]; 13], + /// The joint sign of a `UV_CFL_PRED` block's U/V alpha. + pub cfl_sign: [u16; 9], + /// A `UV_CFL_PRED` block's per-plane alpha magnitude, by joint-sign context. + pub cfl_alpha: [[u16; 17]; 6], /// The angle a directional mode is nudged by. pub angle_delta: [[u16; 8]; 8], /// The one-context all-zero flag of a 16x16 luma transform. @@ -235,6 +239,11 @@ pub(crate) struct Cdfs { /// The two motion vector components' own tables: separate owned state, /// since spec 8.3.2 adapts one component without touching the other. pub mv_comp: [MvComponentCdfs; 2], + /// The `use_filter_intra` flag, indexed by block-size class (`[0]`=4x4, + /// `[1]`=8x8, `[2]`=16x16, `[3]`=32x32) -- see [`cdf::FILTER_INTRA`]. + pub filter_intra: [[u16; 3]; 4], + /// Which `FILTER_INTRA_MODES` entry a `use_filter_intra` block picks. + pub filter_intra_mode: [u16; 6], } /// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`, @@ -297,6 +306,8 @@ impl Cdfs { kf_y_mode: cdf::KF_Y_MODE, uv_mode_no_cfl: cdf::UV_MODE_NO_CFL, uv_mode_cfl: cdf::UV_MODE_CFL, + cfl_sign: cdf::CFL_SIGN, + cfl_alpha: cdf::CFL_ALPHA, angle_delta: cdf::ANGLE_DELTA, txb_skip_luma_16: [pick( q_ctx, @@ -637,6 +648,8 @@ impl Cdfs { drl_mode: cdf::DRL_MODE, mv_joint: cdf::MV_JOINT, mv_comp: [MvComponentCdfs::new(), MvComponentCdfs::new()], + filter_intra: cdf::FILTER_INTRA, + filter_intra_mode: cdf::FILTER_INTRA_MODE, } } diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 0020f3f6..3b72f7d2 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -201,11 +201,18 @@ fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables) -> usize { const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513]; const OFFSET_BITS: [u32; 12] = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + let trace = std::env::var_os("EC_AV1_TRACE").is_some(); let group = dec.symbol(coding.eob_pt) + 1; + if trace { + eprintln!("TRACE eob_pt value={group}"); + } let bits = OFFSET_BITS[group]; let mut offset = 0u32; if bits > 0 { let top = dec.symbol(&mut coding.eob_extra[group - 3]) as u32; + if trace { + eprintln!("TRACE eob_extra ctx={} value={top}", group - 3); + } offset = top << (bits - 1); if bits > 1 { offset |= dec.literal(bits - 1); @@ -232,31 +239,53 @@ fn read_coeffs( skip_ctx: usize, sign_ctx: usize, ) -> Result<(Vec, TxType)> { + let trace = std::env::var_os("EC_AV1_TRACE").is_some(); let side = coding.side; let mut grid = vec![0i32; side * side]; let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; + if trace { + eprintln!("TRACE all_zero ctx={skip_ctx} value={}", all_zero as i32); + } if all_zero { return Ok((grid, TxType::DctDct)); } let mut tx_type = TxType::DctDct; if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { let t = dec.symbol(tx_type_cdf); + if trace { + eprintln!("TRACE tx_type value={t}"); + } tx_type = TxType::from_symbol(t).ok_or_else(|| { unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")) })?; } let eob = read_eob(dec, coding); + if trace { + eprintln!("TRACE eob value={eob}"); + } let mut levels = vec![0i32; side * side]; for scan_idx in (0..eob).rev() { let pos = scan[scan_idx] as usize; let (row, col) = (pos / side, pos % side); let level = if scan_idx == eob - 1 { let ctx = eob_coeff_ctx(scan_idx, side * side); - dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1 + let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1; + if trace { + eprintln!( + "TRACE base_eob scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}" + ); + } + v } else { let ctx = base_ctx(&levels, side, row, col); - dec.symbol(&mut coding.base[ctx]) as i32 + let v = dec.symbol(&mut coding.base[ctx]) as i32; + if trace { + eprintln!( + "TRACE base scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}" + ); + } + v }; let level = if level > NUM_BASE_LEVELS { let ctx = br_ctx(&levels, side, row, col); @@ -264,6 +293,11 @@ fn read_coeffs( let mut sent = 0; loop { let k = dec.symbol(&mut coding.br[ctx]) as i32; + if trace { + eprintln!( + "TRACE br scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={k}" + ); + } level += k; sent += BR_STEP; if k < BR_STEP || sent >= COEFF_BASE_RANGE { @@ -283,12 +317,20 @@ fn read_coeffs( continue; } let negative = if pos == 0 { - dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1 + let v = dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1; + if trace { + eprintln!("TRACE dc_sign ctx={sign_ctx} value={}", v as i32); + } + v } else { dec.literal(1) == 1 }; let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { - level + read_golomb(dec)? as i32 + let g = read_golomb(dec)?; + if trace { + eprintln!("TRACE golomb pos={pos} value={g}"); + } + level + g as i32 } else { level }; @@ -510,20 +552,49 @@ impl Neighbours { /// exactly — including its skip context, which the real key-frame writer /// always writes at a fixed context of zero (it never tracks a neighbour-based /// one for intra blocks; that is the inter-frame writer's rule instead). -/// Refuses a nonzero angle delta or a chroma-from-luma mode: this decoder -/// reconstructs the thirteen luma modes at their base angle, DC-only chroma. +/// Refuses a nonzero angle delta: this decoder reconstructs the thirteen +/// luma modes at their base angle only. Chroma is DC-only or, where CFL is +/// offered (`cfl`, `is_cfl_allowed`, spec 5.11.5), chroma-from-luma -- the +/// third element carries that mode's `(alpha_q3_u, alpha_q3_v)`, `None` for +/// plain DC chroma. +/// A block-size class index into [`cdf::FILTER_INTRA`]/`Cdfs::filter_intra`, +/// or `None` when `side` is past `av1_filter_intra_allowed_bsize`'s <=32 +/// bound (spec `filter_intra_mode_info` never reads a symbol there). +fn filter_intra_size_class(side: usize) -> Option { + match side { + 4 => Some(0), + 8 => Some(1), + 16 => Some(2), + 32 => Some(3), + _ => None, + } +} + fn read_intra_mode( dec: &mut SymbolDecoder, cdfs: &mut Cdfs, above_mode: usize, left_mode: usize, cfl: bool, -) -> Result<(bool, usize)> { + side: usize, + enable_filter_intra: bool, +) -> Result<(bool, usize, Option<(i32, i32)>)> { + let trace = std::env::var_os("EC_AV1_TRACE").is_some(); let skip = dec.symbol(&mut cdfs.skip[0]) != 0; - let mode = - dec.symbol(&mut cdfs.kf_y_mode[INTRA_MODE_CTX[above_mode]][INTRA_MODE_CTX[left_mode]]); + if trace { + eprintln!("TRACE skip value={}", skip as i32); + } + let above_ctx = INTRA_MODE_CTX[above_mode]; + let left_ctx = INTRA_MODE_CTX[left_mode]; + let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); + if trace { + eprintln!("TRACE y_mode ctx=({above_ctx},{left_ctx}) value={mode}"); + } if (V_PRED..=D67_PRED).contains(&mode) { let angle = dec.symbol(&mut cdfs.angle_delta[mode - V_PRED]); + if trace { + eprintln!("TRACE angle_delta value={angle}"); + } if angle != ANGLE_DELTA_ZERO { return Err(unsupported( "a nonzero angle delta (this encoder never writes one)", @@ -535,10 +606,118 @@ fn read_intra_mode( } else { dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) }; - if uv_mode != DC_PRED { - return Err(unsupported("a chroma-from-luma mode (round 2)")); + if trace { + eprintln!("TRACE uv_mode cfl={cfl} y_mode={mode} value={uv_mode}"); + } + let alpha = if uv_mode == DC_PRED { + None + } else if cfl && uv_mode == UV_CFL_PRED { + Some(read_cfl_alphas(dec, cdfs)) + } else { + return Err(unsupported("a directional chroma mode (round 2)")); + }; + // `read_filter_intra_mode_info` (spec 5.11.14, libaom decodemv.c): a + // `use_filter_intra` symbol this decoder never read at all until + // lane-av1real r10 -- silently skipping it left every bit past a + // DC_PRED block at <=32x32 read one symbol short of libaom for the rest + // of the tile (the pinned CfL stream's actual bug: partition/skip/mode + // matched bit-exact, then `eob_pt` read 5 instead of 6, because this + // symbol's bits were never consumed). + if mode == DC_PRED + && enable_filter_intra + && let Some(class) = filter_intra_size_class(side) + { + let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; + if trace { + eprintln!( + "TRACE use_filter_intra side={side} value={}", + use_filter_intra as i32 + ); + } + if use_filter_intra { + // libaom still reads `filter_intra_mode` to stay in sync, but + // this decoder does not implement filter-intra prediction + // (spec 7.11.2.3's 5-tap recursive filter) -- refuse rather + // than silently miscode the reconstruction, the same pattern + // as this crate's other `Error::unsupported` refusals. + let _ = dec.symbol(&mut cdfs.filter_intra_mode); + return Err(unsupported( + "a filter-intra block (this decoder does not implement the 5.11.14 recursive predictor)", + )); + } + } + Ok((skip, mode, alpha)) +} + +/// `UV_CFL_PRED` (spec 6.10.19's chroma intra mode enum): the fourteenth +/// entry of [`crate::cdf::UV_MODE_CFL`], one past the thirteen ordinary +/// intra modes. +const UV_CFL_PRED: usize = 13; + +/// `read_cfl_alphas` (libaom decodemv.c): the joint sign symbol (8-ary, spec +/// 5.11.45's `cfl_alpha_signs`), then each plane's alpha magnitude symbol +/// (16-ary, `cfl_alpha_u`/`cfl_alpha_v`) where its sign is nonzero -- context +/// indices `CFL_CONTEXT_U`/`CFL_CONTEXT_V` (libaom cfl.h) derived from the +/// joint sign the same way. Returns each plane's final signed `alpha_q3` +/// (`cfl_idx_to_alpha`, cfl.c): magnitude `idx + 1`, sign-zero collapsing to 0. +fn read_cfl_alphas(dec: &mut SymbolDecoder, cdfs: &mut Cdfs) -> (i32, i32) { + let joint_sign = dec.symbol(&mut cdfs.cfl_sign) as i32; + let sign_u = ((joint_sign + 1) * 11) >> 5; + let sign_v = (joint_sign + 1) - 3 * sign_u; + let alpha_u = if sign_u == 0 { + 0 + } else { + let ctx_u = (joint_sign + 1 - 3) as usize; + let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_u]) as i32 + 1; + if sign_u == 2 { mag } else { -mag } + }; + let alpha_v = if sign_v == 0 { + 0 + } else { + let ctx_v = (sign_v * 3 + sign_u - 3) as usize; + let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_v]) as i32 + 1; + if sign_v == 2 { mag } else { -mag } + }; + (alpha_u, alpha_v) +} + +/// `cfl_luma_subsampling_420_lbd_c` + `subtract_average_c` (libaom cfl.c): +/// the reconstructed luma samples under a `side`-square block, subsampled +/// 4:2:0 to Q3 (`(a+b+c+d) << 1`, each 2x2 group), then block-average +/// subtracted (`round_offset = num_pel/2`, right-shifted by `log2(num_pel)`) +/// to give the AC values [`cfl_scaled`] scales by alpha. +fn cfl_ac_q3(y: &PlaneBuf, px: usize, py: usize, side: usize) -> Vec { + let cside = side / 2; + let mut ac = vec![0i32; cside * cside]; + let mut sum = 0i32; + for cy in 0..cside { + for cx in 0..cside { + let (lx, ly) = (px + cx * 2, py + cy * 2); + let q3 = (i32::from(y.data[ly * y.width + lx]) + + i32::from(y.data[ly * y.width + lx + 1]) + + i32::from(y.data[(ly + 1) * y.width + lx]) + + i32::from(y.data[(ly + 1) * y.width + lx + 1])) + << 1; + ac[cy * cside + cx] = q3; + sum += q3; + } + } + let num_pel = (cside * cside) as i32; + let avg = (sum + num_pel / 2) >> num_pel.trailing_zeros(); + ac.iter_mut().for_each(|v| *v -= avg); + ac +} + +/// `get_scaled_luma_q0` (libaom cfl.h): `alpha_q3 * ac_q3`, rounded from Q6 +/// back to whole samples with `ROUND_POWER_OF_TWO_SIGNED` (round-to-nearest, +/// ties away from zero on the shifted-out sign). +fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 { + let v = alpha_q3 * ac_q3; + if v >= 0 { + (v + 32) >> 6 + } else { + -((-v + 32) >> 6) } - Ok((skip, mode)) } /// One plane's reconstruction buffer, `width * height` samples, and the @@ -592,6 +771,10 @@ impl PlaneBuf { /// Predicts under `mode` then adds `residual` (side*side, raster), /// writing the clamped reconstruction back into the plane at `(x, y)`. + /// `cfl`, when `Some((alpha_q3, ac_q3))` (a `UV_CFL_PRED` chroma block), + /// nudges the prediction by [`cfl_scaled`]'s per-sample amount before the + /// residual is added — `av1_cfl_predict_block`'s own clip-then-add-residual + /// order (cfl.c). fn reconstruct( &mut self, x: usize, @@ -600,6 +783,7 @@ impl PlaneBuf { mode: usize, reach: Reach, residual: &[i32], + cfl: Option<(i32, &[i32])>, ) { let (above, left, corner) = self.edges(x, y, side, reach); let mut prediction = vec![0u8; side * side]; @@ -613,8 +797,12 @@ impl PlaneBuf { ); for row in 0..side { for col in 0..side { - let sample = (i32::from(prediction[row * side + col]) + residual[row * side + col]) - .clamp(0, 255) as u8; + let idx = row * side + col; + let mut base = i32::from(prediction[idx]); + if let Some((alpha_q3, ac_q3)) = cfl { + base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, 255); + } + let sample = (base + residual[idx]).clamp(0, 255) as u8; self.data[(y + row) * self.width + x + col] = sample; } } @@ -643,6 +831,7 @@ fn read_plane( side: usize, tx_side: usize, base_q_idx: u8, + cfl: Option<(i32, &[i32])>, ) -> Result> { let skip_ctx = if plane_idx == 0 { 0 @@ -669,7 +858,14 @@ fn read_plane( full }; let residual = dequant_and_inverse_typed(&levels, side, 8, i32::from(base_q_idx), tx_type); - plane.reconstruct(x, y, side, predict_mode, reach, &residual); + if std::env::var_os("EC_AV1_TRACE").is_some() { + eprintln!( + "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} levels[0..4]={:?} residual[0..4]={:?}", + &levels[..4.min(levels.len())], + &residual[..4.min(residual.len())] + ); + } + plane.reconstruct(x, y, side, predict_mode, reach, &residual, cfl); Ok(if tx_side == side { residual_grid_placeholder(&levels, side) } else { @@ -705,15 +901,18 @@ fn decode_block( u: &mut PlaneBuf, v: &mut PlaneBuf, base_q_idx: u8, + enable_filter_intra: bool, ) -> Result<()> { let (r, c) = at; let (px, py) = (c * SUB, r * SUB); - let (skip, mode) = read_intra_mode( + let (skip, mode, alpha) = read_intra_mode( dec, cdfs, neighbours.above_mode[c], neighbours.left_mode[r], cfl, + side, + enable_filter_intra, )?; let reach = Reach::of(side, px, py, y.width, y.height); let (luma_grid, u_grid, v_grid); @@ -722,7 +921,8 @@ fn decode_block( if skip { // A skipped block codes no residual syntax at all (spec 5.11.34): // straight prediction, on every plane. - y.reconstruct(px, py, side, mode, reach, &vec![0i32; side * side]); + y.reconstruct(px, py, side, mode, reach, &vec![0i32; side * side], None); + let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); u.reconstruct( cpx, cpy, @@ -730,6 +930,7 @@ fn decode_block( DC_PRED, Reach::none(), &vec![0i32; chroma_side * chroma_side], + alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), ); v.reconstruct( cpx, @@ -738,6 +939,7 @@ fn decode_block( DC_PRED, Reach::none(), &vec![0i32; chroma_side * chroma_side], + alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), ); luma_grid = vec![0i32; side * side]; u_grid = vec![0i32; chroma_side * chroma_side]; @@ -746,8 +948,9 @@ fn decode_block( let around = neighbours.around(at, side); luma_grid = read_plane( dec, cdfs, luma_set, scans.0, 0, around[0], mode, mode, reach, y, px, py, side, - luma_tx, base_q_idx, + luma_tx, base_q_idx, None, )?; + let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); u_grid = read_plane( dec, cdfs, @@ -764,6 +967,7 @@ fn decode_block( chroma_side, chroma_tx, base_q_idx, + alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), )?; v_grid = read_plane( dec, @@ -781,6 +985,7 @@ fn decode_block( chroma_side, chroma_tx, base_q_idx, + alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), )?; } neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]); @@ -809,6 +1014,7 @@ fn decode_leaf8( u: &mut PlaneBuf, v: &mut PlaneBuf, base_q_idx: u8, + enable_filter_intra: bool, ) -> Result { let (r, c) = outer_at; let mut above_mode = neighbours.above_mode[c]; @@ -823,15 +1029,40 @@ fn decode_leaf8( // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF, like every other // `decode_block` caller at 16x16 and up. - let (skip, mode) = read_intra_mode(dec, cdfs, above_mode, left_mode, true)?; + let (skip, mode, alpha) = read_intra_mode( + dec, + cdfs, + above_mode, + left_mode, + true, + 8, + enable_filter_intra, + )?; let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); let reach = Reach::of(8, px, py, y.width, y.height); let (cpx, cpy) = (px / 2, py / 2); let (luma_grid, u_grid, v_grid); if skip { - y.reconstruct(px, py, 8, mode, reach, &vec![0i32; 64]); - u.reconstruct(cpx, cpy, 4, DC_PRED, Reach::none(), &vec![0i32; 16]); - v.reconstruct(cpx, cpy, 4, DC_PRED, Reach::none(), &vec![0i32; 16]); + y.reconstruct(px, py, 8, mode, reach, &vec![0i32; 64], None); + let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); + u.reconstruct( + cpx, + cpy, + 4, + DC_PRED, + Reach::none(), + &vec![0i32; 16], + alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + ); + v.reconstruct( + cpx, + cpy, + 4, + DC_PRED, + Reach::none(), + &vec![0i32; 16], + alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + ); luma_grid = vec![0i32; 64]; u_grid = vec![0i32; 16]; v_grid = vec![0i32; 16]; @@ -853,7 +1084,9 @@ fn decode_leaf8( 8, TX8, base_q_idx, + None, )?; + let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); u_grid = read_plane( dec, cdfs, @@ -870,6 +1103,7 @@ fn decode_leaf8( 4, TX4, base_q_idx, + alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), )?; v_grid = read_plane( dec, @@ -887,6 +1121,7 @@ fn decode_leaf8( 4, TX4, base_q_idx, + alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), )?; } neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); @@ -918,6 +1153,7 @@ pub fn decode_key_frame_tile( base_q_idx: u8, frame_width: u32, frame_height: u32, + enable_filter_intra: bool, ) -> Result { if mi_cols == 0 || mi_rows == 0 { return Err(unsupported("a frame with no mode-info grid")); @@ -955,6 +1191,13 @@ pub fn decode_key_frame_tile( let scan4 = default_scan(TX4); let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); + if std::env::var_os("EC_AV1_TRACE").is_some() { + eprintln!( + "TRACE key_tile_bytes len={} first8={:02x?} base_q_idx={base_q_idx} mi_cols={mi_cols} mi_rows={mi_rows}", + data.len(), + &data[..data.len().min(8)] + ); + } let mut dec = SymbolDecoder::new(data); let mut neighbours = Neighbours::new( cols32 as usize * 2, @@ -980,7 +1223,11 @@ pub fn decode_key_frame_tile( // mirroring [`crate::tile::sb_coeff_key_frame_tile`]'s own // three-way write exactly. let part = if has_cols && has_rows { - dec.symbol(&mut cdfs.partition_w64[ctx]) + let p = dec.symbol(&mut cdfs.partition_w64[ctx]); + if std::env::var_os("EC_AV1_TRACE").is_some() { + eprintln!("TRACE partition_w64 ctx={ctx} value={p}"); + } + p } else { match (has_cols, has_rows) { (true, false) => { @@ -1011,6 +1258,7 @@ pub fn decode_key_frame_tile( &mut u, &mut v, base_q_idx, + enable_filter_intra, )?; } PARTITION_SPLIT => { @@ -1026,7 +1274,11 @@ pub fn decode_key_frame_tile( has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows), ); let part32 = if has_cols32 && has_rows32 { - dec.symbol(&mut cdfs.partition_w32[ctx32]) + let p = dec.symbol(&mut cdfs.partition_w32[ctx32]); + if std::env::var_os("EC_AV1_TRACE").is_some() { + eprintln!("TRACE partition_w32 ctx={ctx32} value={p}"); + } + p } else { match (has_cols32, has_rows32) { (true, false) => { @@ -1063,6 +1315,7 @@ pub fn decode_key_frame_tile( &mut u, &mut v, base_q_idx, + enable_filter_intra, )?; } PARTITION_SPLIT => { @@ -1099,6 +1352,7 @@ pub fn decode_key_frame_tile( &mut u, &mut v, base_q_idx, + enable_filter_intra, )?; continue; } @@ -1141,6 +1395,7 @@ pub fn decode_key_frame_tile( &mut u, &mut v, base_q_idx, + enable_filter_intra, )?; prev_leaf = Some((leaf_mi, leaf_mode)); } @@ -1202,6 +1457,7 @@ pub fn decode_key_frame_tile( &mut u, &mut v, base_q_idx, + enable_filter_intra, )?; prev_leaf = Some((leaf_mi, leaf_mode)); } @@ -1641,9 +1897,13 @@ fn decode_inter_block( } } let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]); - if uv_mode != DC_PRED { - return Err(unsupported("a chroma-from-luma mode (round 2)")); - } + let alpha = if uv_mode == DC_PRED { + None + } else if uv_mode == UV_CFL_PRED { + Some(read_cfl_alphas(dec, cdfs)) + } else { + return Err(unsupported("a directional chroma mode (round 2)")); + }; mode_for_tx = mode; let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); for dr in 0..side / 4 { @@ -1664,7 +1924,8 @@ fn decode_inter_block( let reach = Reach::of(side, px, py, y.width, y.height); if skip { - y.reconstruct(px, py, side, mode, reach, &vec![0i32; side * side]); + y.reconstruct(px, py, side, mode, reach, &vec![0i32; side * side], None); + let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); u.reconstruct( cpx, cpy, @@ -1672,6 +1933,7 @@ fn decode_inter_block( DC_PRED, Reach::none(), &vec![0i32; chroma_side * chroma_side], + alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), ); v.reconstruct( cpx, @@ -1680,6 +1942,7 @@ fn decode_inter_block( DC_PRED, Reach::none(), &vec![0i32; chroma_side * chroma_side], + alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), ); luma_grid = vec![0i32; side * side]; u_grid = vec![0i32; chroma_side * chroma_side]; @@ -1702,7 +1965,9 @@ fn decode_inter_block( side, luma_tx, base_q_idx, + None, )?; + let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); u_grid = read_plane( dec, cdfs, @@ -1719,6 +1984,7 @@ fn decode_inter_block( chroma_side, chroma_tx, base_q_idx, + alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), )?; v_grid = read_plane( dec, @@ -1736,6 +2002,7 @@ fn decode_inter_block( chroma_side, chroma_tx, base_q_idx, + alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), )?; } } @@ -2012,7 +2279,7 @@ mod tests { #[test] fn a_frame_with_no_mode_info_grid_is_refused() { - assert!(decode_key_frame_tile(&[0u8; 4], 0, 32, 32, 0, 128).is_err()); + assert!(decode_key_frame_tile(&[0u8; 4], 0, 32, 32, 0, 128, false).is_err()); } /// Encodes and decodes one picture with `modes`, asserting the decoder's @@ -2040,8 +2307,10 @@ mod tests { base_q_idx, .. }: Encoded = crate::encode::encode_key_frame_with_modes(&picture, 40, 0.0, modes).unwrap(); - let decoded = - decode_key_frame_tile(&tile, mi_cols, mi_rows, base_q_idx, w as u32, h as u32).unwrap(); + let decoded = decode_key_frame_tile( + &tile, mi_cols, mi_rows, base_q_idx, w as u32, h as u32, false, + ) + .unwrap(); assert_eq!(decoded.width, w, "{w}x{h}: decoded width"); assert_eq!(decoded.height, h, "{w}x{h}: decoded height"); assert_eq!(decoded.y, reconstruction.y, "{w}x{h} luma mismatch"); @@ -2111,7 +2380,7 @@ mod tests { ); enc.symbol(DC_PRED, &mut cdfs.uv_mode_no_cfl[DC_PRED]); let data = enc.finish(); - let decoded = decode_key_frame_tile(&data, 16, 16, 40, 64, 64).unwrap(); + let decoded = decode_key_frame_tile(&data, 16, 16, 40, 64, 64, false).unwrap(); assert!( decoded.y.iter().all(|&s| s == 128), "a skipped DC_PRED block with no neighbours predicts flat mid-grey" @@ -2290,6 +2559,7 @@ mod tests { encoded.base_q_idx, coded_w, coded_h, + false, ) .unwrap(); assert_eq!(ours.y, ffmpeg_decoded.y, "{width}x{height}: luma vs ffmpeg"); @@ -2357,6 +2627,7 @@ mod tests { key.base_q_idx, width as u32, height as u32, + false, ) .unwrap(); assert_eq!( @@ -2623,6 +2894,7 @@ mod tests { key.base_q_idx, coded_w, coded_h, + false, ) .unwrap(); assert_eq!(reference.y, ffmpeg_frames[0].y, "frame 0 luma vs ffmpeg"); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 74370c6c..55ee950c 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -10,7 +10,7 @@ //! are `pub(crate)`, and rightly so: the wire is `stream`, everything else is //! an implementation detail of how this crate happens to build it). -use ec_av1_syntax::{Av1Parser, CdefParams, FrameType, ObuKind}; +use ec_av1_syntax::{Av1Parser, CdefParams, FrameType, ObuKind, TxMode}; use ec_core::{Error, Result}; use crate::decode::{decode_inter_frame_tile, decode_key_frame_tile}; @@ -75,11 +75,60 @@ pub fn decode_stream(data: &[u8]) -> Result> { "a frame with an active CDEF filter (not yet applied by this decoder)", )); } + // Every `decode_block` call below assumes the block's transform is + // its own full size (`TxMode::Largest`, which is all this crate's own + // encoder ever writes, spec `read_tx_size` inferring the max depth + // with no bits) -- it never reads a `tx_depth` symbol at all. A real + // encoder's `TxMode::Select` (spec 5.9.2's `tx_mode` bit) codes one + // per intra block, and skipping that read does not error -- the + // decoder just goes on consuming the next bits as if they were the + // uv_mode/coefficient symbols they are not, so the block "decodes" + // syntactically clean into low-energy garbage: lane-av1real r3's + // luma-near-flat-vs-ffmpeg-gradient bug. Refuse before that, the same + // way as the CDEF case above, rather than silently miscode every + // `TxMode::Select` stream. + if header.tx_mode == TxMode::Select { + return Err(Error::unsupported( + "AV1 decode_stream", + "a frame using TxMode::Select (this decoder never reads a tx_depth symbol, so it desyncs after the first intra block's mode)", + )); + } + // lane-av1real r11's blindness-audit sweep (`read_intra_frame_mode_info`, + // libaom decodemv.c): three more per-block/per-SB symbols this crate + // never reads at all, each gated by a header field this decoder + // otherwise accepts uncomplaining -- the same silent-desync shape + // `use_filter_intra` was. `allow_screen_content_tools` gates both + // `read_intrabc_info` and `read_palette_mode_info`; `delta.q_present`/ + // `delta.lf_present` gate `read_delta_qindex`/`read_delta_lflevel` + // (spec 5.11.10/5.11.11, one symbol group per superblock). Refuse + // before desyncing rather than silently miscode, the same pattern as + // the CDEF/TxMode::Select refusals above. + if header.allow_screen_content_tools { + return Err(Error::unsupported( + "AV1 decode_stream", + "a frame with allow_screen_content_tools set (this decoder never reads intrabc/palette_mode_info)", + )); + } + if header.delta.q_present || header.delta.lf_present { + return Err(Error::unsupported( + "AV1 decode_stream", + "a frame with delta_q_present or delta_lf_present set (this decoder never reads the per-superblock delta symbols)", + )); + } + if header.segmentation.enabled { + return Err(Error::unsupported( + "AV1 decode_stream", + "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", + )); + } // `Tile::offset` is relative to the buffer `parse_obu` was handed // (`&data[pos..]` at the time this OBU was parsed), so it is relative // to `obu_offset`, not to `data` as a whole. let start = obu_offset + tile.offset; let tile_bytes = data.get(start..start + tile.size).ok_or(Error::NeedMore)?; + let enable_filter_intra = parser + .sequence_header() + .is_some_and(|seq| seq.enable_filter_intra); let picture = if header.frame_type == FrameType::Key { decode_key_frame_tile( @@ -89,6 +138,7 @@ pub fn decode_stream(data: &[u8]) -> Result> { header.quantization.base_q_idx, header.frame_width, header.frame_height, + enable_filter_intra, )? } else { let reference = reference.as_ref().ok_or_else(|| { @@ -170,6 +220,7 @@ mod tests { encoded.base_q_idx, width as u32, height as u32, + false, ) .unwrap(); let via_stream = decode_stream(&encoded.stream).unwrap(); @@ -481,6 +532,52 @@ mod tests { assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg"); } + /// As [`a_real_libaom_gradients_stream_decodes_pixel_exact`], but at + /// 32x32 (below `is_cfl_allowed`'s 32x32 luma bound as a *single* + /// undivided block, spec 5.11.5) rather than 64x64 -- a whole 64x64 SB + /// is never CFL-eligible itself, only its <=32x32 partitions, and + /// `testsrc2`/`gradients` at 64x64 with rect/ab/1to4/angle-delta search + /// disabled reliably pick a whole-SB `PARTITION_NONE` (probed + /// lane-av1real r2), so no fixture at that size exercises CfL at all. + /// A 32x32 frame is exactly the whole-block, CFL-allowed case, and + /// lane-av1real r2 found `gradients` there reliably selects + /// `UV_CFL_PRED` (`uv_mode=13`) across a crf sweep -- `testsrc2` there + /// stays DC. Proves the CfL port this round added, not just DC-chroma + /// decode: pixel-exact against ffmpeg's own decode, not just "decoded". + /// + #[test] + fn a_real_libaom_cfl_stream_decodes_pixel_exact() { + if !have_ffmpeg() { + eprintln!("SKIP a_real_libaom_cfl_stream_decodes_pixel_exact: no ffmpeg"); + return; + } + let (width, height) = (32, 32); + let stream = libaom_encode_with( + "gradients=size=32x32:c0=red:c1=blue:c2=green:rate=1:seed=42", + width, + height, + 15, + &[ + "-threads", + "1", + "-row-mt", + "0", + "-tile-columns", + "0", + "-tile-rows", + "0", + "-enable-cdef", + "0", + ], + ) + .expect("ffmpeg encode"); + let frames = decode_stream(&stream).expect("decode_stream on a real libaom stream"); + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); + assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg"); + assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg"); + assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg"); + } + /// scratch: isolate a pinned mismatching stream's first divergent pixel. #[test] #[ignore] @@ -495,11 +592,18 @@ mod tests { pos += obu.total_size; if let ObuKind::Frame(header, _) = obu.kind { eprintln!("cdef: {:?}", header.cdef); + eprintln!("quantization: {:?}", header.quantization); + eprintln!( + "coded_lossless: {:?} tx_mode: {:?}", + header.coded_lossless, header.tx_mode + ); + eprintln!("delta: {:?}", header.delta); + eprintln!("segmentation: {:?}", header.segmentation); } } } - let width = 64; - let height = 64; + let width = 32; + let height = 32; let frames = decode_stream(&stream).expect("our decode"); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); let ours = &frames[0]; @@ -531,5 +635,15 @@ mod tests { eprintln!("plane {plane_name}: MATCH"); } } + for row in 0..8 { + eprintln!( + "ours row{row}: {:?}", + &ours.y[row * width..row * width + 8] + ); + eprintln!( + "theirs row{row}: {:?}", + &theirs.y[row * width..row * width + 8] + ); + } } } diff --git a/crates/ec-av1/src/transform.rs b/crates/ec-av1/src/transform.rs index 41a3fbce..ae89866c 100644 --- a/crates/ec-av1/src/transform.rs +++ b/crates/ec-av1/src/transform.rs @@ -1050,6 +1050,41 @@ mod tests { .collect() } + #[test] + #[ignore] + fn scratch_probe_32x32_dequant() { + let mut levels = vec![0i32; 32 * 32]; + levels[0] = -2; + levels[1] = -2; + levels[2] = -2; + let dq = crate::quant::dequant(&levels, 32, 8, 60); + eprintln!("dq[0..4]={:?}", &dq[0..4]); + let residual = dequant_and_inverse(&levels, 32, 8, 60); + eprintln!("row0: {:?}", &residual[0..32]); + eprintln!("row1: {:?}", &residual[32..64]); + + // superposition probe: does inverse_transform_2d at side=32 sum + // three simultaneous low-frequency coefficients the same as it + // reconstructs each alone? + let side = 32usize; + let mut single_sum = vec![0i32; side * side]; + for &(pos, val) in &[(0usize, -57i32), (1, -67), (2, -67)] { + let mut c = vec![0i32; side * side]; + c[pos] = val; + let got = inverse_transform_2d(&c, side, 8); + for i in 0..side * side { + single_sum[i] += got[i]; + } + } + let mut combined = vec![0i32; side * side]; + combined[0] = -57; + combined[1] = -67; + combined[2] = -67; + let got_combined = inverse_transform_2d(&combined, side, 8); + eprintln!("single_sum row0: {:?}", &single_sum[0..8]); + eprintln!("combined row0: {:?}", &got_combined[0..8]); + } + fn rmse(a: &[i32], b: &[i32]) -> f64 { let sum: f64 = a .iter()