diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index fc53c539..70602dd7 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -66,6 +66,21 @@ pub(crate) fn filter_intra_hits() -> usize { FILTER_INTRA_HITS.with(|c| c.get()) } +/// How many key-frame luma blocks resolved `smooth_neighbor` (spec +/// `get_intra_edge_filter_type`) to `true` -- lane-chroma r2's own before/after +/// counter, proving a stream actually put a directional block next to a +/// smooth-predicted neighbour rather than every block reading the (previously +/// hardcoded `false`) edge-filter strength bucket unchanged. +thread_local! { + static SMOOTH_LUMA_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`SMOOTH_LUMA_HITS`]. +pub(crate) fn smooth_luma_hits() -> usize { + SMOOTH_LUMA_HITS.with(|c| c.get()) +} + /// How many 4-pixel deblocking edge groups [`edge_params`] has actually /// selected for filtering, across every call in the process -- the same /// before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream @@ -950,6 +965,13 @@ fn default_intra_tx_type(mode: u8) -> TxType { } } +/// `is_smooth` (libaom `reconintra.c`): true for `SMOOTH_PRED..=SMOOTH_H_PRED` +/// (9..=11) — deliberately excludes `PAETH_PRED` (12), which the spec's own +/// `get_intra_edge_filter_type`/libaom `get_filt_type` never treat as smooth. +fn is_smooth_mode(mode: usize) -> bool { + (crate::intra::SMOOTH_PRED as usize..=crate::intra::SMOOTH_H_PRED as usize).contains(&mode) +} + fn read_coeffs( dec: &mut SymbolDecoder, coding: &mut TxbTables, @@ -2090,6 +2112,13 @@ fn decode_block_rect( neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), allow_screen_content_tools, )?; + // 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(neighbours.above_mode[c]) || is_smooth_mode(neighbours.left_mode[r]); + if smooth_neighbor { + SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); + } if filter_intra.is_some() { // `predict_filter_intra` (spec 7.11.2.3) is square-only in this // decoder (`assert_eq!(dst.len(), side*side)`) -- a real symbol was @@ -2155,6 +2184,7 @@ fn decode_block_rect( &vec![0i32; bw * bh], None, filter_intra, + smooth_neighbor, ); let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); u.reconstruct_rect( @@ -2168,6 +2198,7 @@ fn decode_block_rect( &vec![0i32; chroma_w * chroma_h], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, + false, ); v.reconstruct_rect( cpx, @@ -2180,6 +2211,7 @@ fn decode_block_rect( &vec![0i32; chroma_w * chroma_h], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, + false, ); neighbours.record_rect( at, @@ -2273,6 +2305,7 @@ fn decode_block_rect( &luma_residual, None, filter_intra, + smooth_neighbor, ); let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); let u_default_tx = default_intra_tx_type(uv_predict_mode as u8); @@ -2311,6 +2344,7 @@ fn decode_block_rect( &u_residual, alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, + false, ); let v_default_tx = default_intra_tx_type(uv_predict_mode as u8); let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); @@ -2348,6 +2382,7 @@ fn decode_block_rect( &v_residual, alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, + false, ); neighbours.record_rect(at, bw, bh, mode, &[luma_levels, u_levels, v_levels]); RECT_COEFF_HITS.with(|c| c.set(c.get() + 1)); @@ -2713,6 +2748,7 @@ impl PlaneBuf { residual: &[i32], cfl: Option<(i32, &[i32])>, filter_intra: Option, + smooth_neighbor: bool, ) { let (above, left, corner) = self.edges_rect(x, y, bw, bh, reach); let mut prediction = vec![0u8; bw * bh]; @@ -2736,7 +2772,7 @@ impl PlaneBuf { bw, bh, enable_edge_filter, - false, + smooth_neighbor, &mut prediction, ); } @@ -2770,6 +2806,15 @@ impl PlaneBuf { residual: &[i32], cfl: Option<(i32, &[i32])>, filter_intra: Option, + // `smooth_neighbor` (spec `get_intra_edge_filter_type` / + // libaom `get_filt_type`): true when the block's above OR left + // neighbour's mode is `SMOOTH_PRED..=SMOOTH_H_PRED` (never `PAETH`). + // Callers pass their own plane's neighbour-mode check + // ([`is_smooth_mode`]) — chroma's is still hardcoded `false` by its + // callers until a smooth/paeth `uv_mode` can reach this call at all + // (still refused in `read_intra_mode`/`read_intra_mode_rect`), which + // keeps `false` exact for chroma without this fn needing to know why. + smooth_neighbor: bool, ) { let (above, left, corner) = self.edges(x, y, side, reach); let mut prediction = vec![0u8; side * side]; @@ -2789,13 +2834,7 @@ impl PlaneBuf { // `read_plane`/`decode_block`/`decode_leaf8` call would touch two // dozen sites for a value that never changes mid-decode, so it // rides a thread-local instead (this decoder is single-threaded - // per stream). `smooth_neighbor` (spec `get_intra_edge_filter_type`) - // is left `false`: exact for chroma here (a smooth `uv_mode` is - // refused before decode reaches this call) and a corner-cut for - // luma -- ceiling is one wrong filter-strength bucket on a block - // whose real neighbour predicted smooth; upgrade path is - // threading `Neighbours::above_mode`/`left_mode`'s - // `SMOOTH_PRED..=SMOOTH_H_PRED` membership down instead of `false`. + // per stream). let enable_edge_filter = ENABLE_EDGE_FILTER.with(std::cell::Cell::get); predict( mode as u8, @@ -2806,7 +2845,7 @@ impl PlaneBuf { side, side, enable_edge_filter, - false, + smooth_neighbor, &mut prediction, ); } @@ -2856,6 +2895,8 @@ fn read_plane( // chroma plane), where `txb_skip_ctx` is 0 or the usual OR-of-neighbours // formula below. luma_skip_ctx: Option, + // See [`PlaneBuf::reconstruct`]'s own doc: caller's neighbour-smooth check. + smooth_neighbor: bool, ) -> Result> { let skip_ctx = if plane_idx == 0 { luma_skip_ctx.unwrap_or(0) @@ -2921,6 +2962,7 @@ fn read_plane( &residual, cfl, filter_intra, + smooth_neighbor, ); Ok(if tx_side == side { residual_grid_placeholder(&levels, side) @@ -2990,6 +3032,13 @@ fn decode_block( } else { uv_mode }; + // 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(neighbours.above_mode[c]) || is_smooth_mode(neighbours.left_mode[r]); + if smooth_neighbor { + SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); + } if std::env::var_os("EC_AV1_TRACE").is_some() { eprintln!( "TRACE block px={px} py={py} side={side} mode={mode} uv_mode={uv_mode} \ @@ -3031,6 +3080,7 @@ fn decode_block( &vec![0i32; side * side], None, filter_intra, + smooth_neighbor, ); let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); u.reconstruct( @@ -3043,6 +3093,7 @@ fn decode_block( &vec![0i32; chroma_side * chroma_side], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, + false, ); v.reconstruct( cpx, @@ -3054,6 +3105,7 @@ fn decode_block( &vec![0i32; chroma_side * chroma_side], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, + false, ); let luma_grid = vec![0i32; side * side]; let u_grid = vec![0i32; chroma_side * chroma_side]; @@ -3084,6 +3136,7 @@ fn decode_block( None, filter_intra, None, + smooth_neighbor, )?; let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); let u_grid = read_plane( @@ -3106,6 +3159,7 @@ fn decode_block( alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, None, + false, )?; if std::env::var_os("EC_AV1_TRACE").is_some() { eprintln!( @@ -3133,6 +3187,7 @@ fn decode_block( alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, None, + false, )?; neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]); } else { @@ -3178,6 +3233,7 @@ fn decode_block( None, filter_intra, Some(tu_skip_ctx), + smooth_neighbor, )?; if std::env::var_os("EC_AV1_TRACE").is_some() { eprintln!( @@ -3212,6 +3268,7 @@ fn decode_block( alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, None, + false, )?; let v_grid = read_plane( dec, @@ -3233,6 +3290,7 @@ fn decode_block( alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, None, + false, )?; neighbours.record_split_luma(at, side, mode, [&u_grid, &v_grid]); } @@ -3284,6 +3342,12 @@ fn decode_leaf8( left_mode = pmode; } } + // 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); + if smooth_neighbor { + SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); + } // 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. @@ -3330,6 +3394,7 @@ fn decode_leaf8( &vec![0i32; 64], None, filter_intra, + smooth_neighbor, ); let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); u.reconstruct( @@ -3342,6 +3407,7 @@ fn decode_leaf8( &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, + false, ); v.reconstruct( cpx, @@ -3353,6 +3419,7 @@ fn decode_leaf8( &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, + false, ); luma_grid = vec![0i32; 64]; u_grid = vec![0i32; 16]; @@ -3383,6 +3450,7 @@ fn decode_leaf8( None, filter_intra, None, + smooth_neighbor, )?; let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); u_grid = read_plane( @@ -3405,6 +3473,7 @@ fn decode_leaf8( alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, None, + false, )?; v_grid = read_plane( dec, @@ -3426,6 +3495,7 @@ fn decode_leaf8( alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, None, + false, )?; } else { // `tx_depth` resolved this leaf's luma to `TX4`: a 2x2 raster-order @@ -3466,6 +3536,7 @@ fn decode_leaf8( None, filter_intra, Some(tu_skip_ctx), + smooth_neighbor, )?; if std::env::var_os("EC_AV1_TRACE").is_some() { eprintln!( @@ -3500,6 +3571,7 @@ fn decode_leaf8( alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, None, + false, )?; v_grid = read_plane( dec, @@ -3521,6 +3593,7 @@ fn decode_leaf8( alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, None, + false, )?; // `record_split_luma` expects a `record`-style `(r, c)` position in // `SUB`-grid units and rescales it by `SUB / MI`; `leaf_mi` is @@ -7843,6 +7916,7 @@ fn decode_inter_block( &vec![0i32; side * side], None, None, + false, ); let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); u.reconstruct( @@ -7855,6 +7929,7 @@ fn decode_inter_block( &vec![0i32; chroma_side * chroma_side], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, + false, ); v.reconstruct( cpx, @@ -7866,6 +7941,7 @@ fn decode_inter_block( &vec![0i32; chroma_side * chroma_side], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, + false, ); luma_grid = vec![0i32; side * side]; u_grid = vec![0i32; chroma_side * chroma_side]; @@ -7892,6 +7968,7 @@ fn decode_inter_block( None, None, None, + false, )?; let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); u_grid = read_plane( @@ -7914,6 +7991,7 @@ fn decode_inter_block( alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, None, + false, )?; v_grid = read_plane( dec, @@ -7935,6 +8013,7 @@ fn decode_inter_block( alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, None, + false, )?; } } @@ -8916,6 +8995,7 @@ fn decode_inter_block8( &vec![0i32; SIDE * SIDE], None, None, + false, ); u.reconstruct( cpx, @@ -8927,6 +9007,7 @@ fn decode_inter_block8( &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], None, None, + false, ); v.reconstruct( cpx, @@ -8938,6 +9019,7 @@ fn decode_inter_block8( &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], None, None, + false, ); luma_grid = vec![0i32; SIDE * SIDE]; u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; @@ -8964,6 +9046,7 @@ fn decode_inter_block8( None, None, None, + false, )?; u_grid = read_plane( dec, @@ -8985,6 +9068,7 @@ fn decode_inter_block8( None, None, None, + false, )?; v_grid = read_plane( dec, @@ -9006,6 +9090,7 @@ fn decode_inter_block8( None, None, None, + false, )?; } }