diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 5833cdc0..e8f03726 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -624,7 +624,7 @@ fn read_intra_mode( cfl: bool, side: usize, enable_filter_intra: bool, -) -> Result<(bool, usize, Option<(i32, i32)>)> { +) -> Result<(bool, usize, Option<(i32, i32)>, Option)> { let trace = std::env::var_os("EC_AV1_TRACE").is_some(); let skip = dec.symbol(&mut cdfs.skip[0]) != 0; if trace { @@ -669,6 +669,7 @@ fn read_intra_mode( // 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). + let mut filter_intra = None; if mode == DC_PRED && enable_filter_intra && let Some(class) = filter_intra_size_class(side) @@ -681,18 +682,14 @@ fn read_intra_mode( ); } 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)", - )); + let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); + if trace { + eprintln!("TRACE filter_intra_mode value={fi_mode}"); + } + filter_intra = Some(fi_mode); } } - Ok((skip, mode, alpha)) + Ok((skip, mode, alpha, filter_intra)) } /// `UV_CFL_PRED` (spec 6.10.19's chroma intra mode enum): the fourteenth @@ -830,17 +827,29 @@ impl PlaneBuf { reach: Reach, residual: &[i32], cfl: Option<(i32, &[i32])>, + filter_intra: Option, ) { let (above, left, corner) = self.edges(x, y, side, reach); let mut prediction = vec![0u8; side * side]; - predict( - mode as u8, - above.as_deref(), - left.as_deref(), - corner, - side, - &mut prediction, - ); + if let Some(fi_mode) = filter_intra { + crate::intra::predict_filter_intra( + fi_mode, + above.as_deref(), + left.as_deref(), + corner, + side, + &mut prediction, + ); + } else { + predict( + mode as u8, + above.as_deref(), + left.as_deref(), + corner, + side, + &mut prediction, + ); + } for row in 0..side { for col in 0..side { let idx = row * side + col; @@ -878,6 +887,7 @@ fn read_plane( tx_side: usize, base_q_idx: u8, cfl: Option<(i32, &[i32])>, + filter_intra: Option, ) -> Result> { let skip_ctx = if plane_idx == 0 { 0 @@ -911,7 +921,7 @@ fn read_plane( &residual[..4.min(residual.len())] ); } - plane.reconstruct(x, y, side, predict_mode, reach, &residual, cfl); + plane.reconstruct(x, y, side, predict_mode, reach, &residual, cfl, filter_intra); Ok(if tx_side == side { residual_grid_placeholder(&levels, side) } else { @@ -951,7 +961,7 @@ fn decode_block( ) -> Result<()> { let (r, c) = at; let (px, py) = (c * SUB, r * SUB); - let (skip, mode, alpha) = read_intra_mode( + let (skip, mode, alpha, filter_intra) = read_intra_mode( dec, cdfs, neighbours.above_mode[c], @@ -967,7 +977,16 @@ 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], None); + y.reconstruct( + px, + py, + side, + mode, + reach, + &vec![0i32; side * side], + None, + filter_intra, + ); let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); u.reconstruct( cpx, @@ -977,6 +996,7 @@ fn decode_block( Reach::none(), &vec![0i32; chroma_side * chroma_side], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + None, ); v.reconstruct( cpx, @@ -986,6 +1006,7 @@ fn decode_block( Reach::none(), &vec![0i32; chroma_side * chroma_side], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + None, ); luma_grid = vec![0i32; side * side]; u_grid = vec![0i32; chroma_side * chroma_side]; @@ -994,7 +1015,7 @@ 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, None, + luma_tx, base_q_idx, None, filter_intra, )?; let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); u_grid = read_plane( @@ -1014,6 +1035,7 @@ fn decode_block( chroma_tx, base_q_idx, alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + None, )?; v_grid = read_plane( dec, @@ -1032,6 +1054,7 @@ fn decode_block( chroma_tx, base_q_idx, alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + None, )?; } neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]); @@ -1076,7 +1099,7 @@ 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, alpha) = read_intra_mode( + let (skip, mode, alpha, filter_intra) = read_intra_mode( dec, cdfs, above_mode, @@ -1090,7 +1113,7 @@ fn decode_leaf8( 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], None); + y.reconstruct(px, py, 8, mode, reach, &vec![0i32; 64], None, filter_intra); let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); u.reconstruct( cpx, @@ -1100,6 +1123,7 @@ fn decode_leaf8( Reach::none(), &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + None, ); v.reconstruct( cpx, @@ -1109,6 +1133,7 @@ fn decode_leaf8( Reach::none(), &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + None, ); luma_grid = vec![0i32; 64]; u_grid = vec![0i32; 16]; @@ -1132,6 +1157,7 @@ fn decode_leaf8( TX8, base_q_idx, None, + filter_intra, )?; let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); u_grid = read_plane( @@ -1151,6 +1177,7 @@ fn decode_leaf8( TX4, base_q_idx, alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + None, )?; v_grid = read_plane( dec, @@ -1169,6 +1196,7 @@ fn decode_leaf8( TX4, base_q_idx, alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + None, )?; } neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); @@ -2265,7 +2293,16 @@ 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], None); + y.reconstruct( + px, + py, + side, + mode, + reach, + &vec![0i32; side * side], + None, + None, + ); let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); u.reconstruct( cpx, @@ -2275,6 +2312,7 @@ fn decode_inter_block( Reach::none(), &vec![0i32; chroma_side * chroma_side], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + None, ); v.reconstruct( cpx, @@ -2284,6 +2322,7 @@ fn decode_inter_block( Reach::none(), &vec![0i32; chroma_side * chroma_side], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + None, ); luma_grid = vec![0i32; side * side]; u_grid = vec![0i32; chroma_side * chroma_side]; @@ -2307,6 +2346,7 @@ fn decode_inter_block( luma_tx, base_q_idx, None, + None, )?; let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); u_grid = read_plane( @@ -2326,6 +2366,7 @@ fn decode_inter_block( chroma_tx, base_q_idx, alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + None, )?; v_grid = read_plane( dec, @@ -2344,6 +2385,7 @@ fn decode_inter_block( chroma_tx, base_q_idx, alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + None, )?; } } @@ -2634,7 +2676,16 @@ fn decode_inter_block8( let reach = Reach::of(SIDE, px, py, y.width, y.height); if skip { - y.reconstruct(px, py, SIDE, mode, reach, &vec![0i32; SIDE * SIDE], None); + y.reconstruct( + px, + py, + SIDE, + mode, + reach, + &vec![0i32; SIDE * SIDE], + None, + None, + ); u.reconstruct( cpx, cpy, @@ -2643,6 +2694,7 @@ fn decode_inter_block8( Reach::none(), &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], None, + None, ); v.reconstruct( cpx, @@ -2652,6 +2704,7 @@ fn decode_inter_block8( Reach::none(), &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], None, + None, ); luma_grid = vec![0i32; SIDE * SIDE]; u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; @@ -2675,6 +2728,7 @@ fn decode_inter_block8( TX8, base_q_idx, None, + None, )?; u_grid = read_plane( dec, @@ -2693,6 +2747,7 @@ fn decode_inter_block8( TX4, base_q_idx, None, + None, )?; v_grid = read_plane( dec, @@ -2711,6 +2766,7 @@ fn decode_inter_block8( TX4, base_q_idx, None, + None, )?; } } diff --git a/crates/ec-av1/src/intra.rs b/crates/ec-av1/src/intra.rs index 73978ea3..1ba1367f 100644 --- a/crates/ec-av1/src/intra.rs +++ b/crates/ec-av1/src/intra.rs @@ -341,6 +341,139 @@ fn dc(above: Option<&[u8]>, left: Option<&[u8]>, side: usize) -> i32 { } } +/// `Intra_Filter_Taps` (spec 7.11.2.3 / libaom `av1_filter_intra_taps`, +/// `av1/common/reconintra.c`): five recursive-filter modes (DC/V/H/D157/Paeth +/// variants, in that order), each predicting an 8-tap block of eight output +/// samples from seven already-known neighbours. +const FILTER_INTRA_TAPS: [[[i32; 7]; 8]; 5] = [ + [ + [-6, 10, 0, 0, 0, 12, 0], + [-5, 2, 10, 0, 0, 9, 0], + [-3, 1, 1, 10, 0, 7, 0], + [-3, 1, 1, 2, 10, 5, 0], + [-4, 6, 0, 0, 0, 2, 12], + [-3, 2, 6, 0, 0, 2, 9], + [-3, 2, 2, 6, 0, 2, 7], + [-3, 1, 2, 2, 6, 3, 5], + ], + [ + [-10, 16, 0, 0, 0, 10, 0], + [-6, 0, 16, 0, 0, 6, 0], + [-4, 0, 0, 16, 0, 4, 0], + [-2, 0, 0, 0, 16, 2, 0], + [-10, 16, 0, 0, 0, 0, 10], + [-6, 0, 16, 0, 0, 0, 6], + [-4, 0, 0, 16, 0, 0, 4], + [-2, 0, 0, 0, 16, 0, 2], + ], + [ + [-8, 8, 0, 0, 0, 16, 0], + [-8, 0, 8, 0, 0, 16, 0], + [-8, 0, 0, 8, 0, 16, 0], + [-8, 0, 0, 0, 8, 16, 0], + [-4, 4, 0, 0, 0, 0, 16], + [-4, 0, 4, 0, 0, 0, 16], + [-4, 0, 0, 4, 0, 0, 16], + [-4, 0, 0, 0, 4, 0, 16], + ], + [ + [-2, 8, 0, 0, 0, 10, 0], + [-1, 3, 8, 0, 0, 6, 0], + [-1, 2, 3, 8, 0, 4, 0], + [0, 1, 2, 3, 8, 2, 0], + [-1, 4, 0, 0, 0, 3, 10], + [-1, 3, 4, 0, 0, 4, 6], + [-1, 2, 3, 4, 0, 4, 4], + [-1, 2, 2, 3, 4, 3, 3], + ], + [ + [-12, 14, 0, 0, 0, 14, 0], + [-10, 0, 14, 0, 0, 12, 0], + [-9, 0, 0, 14, 0, 11, 0], + [-8, 0, 0, 0, 14, 10, 0], + [-10, 12, 0, 0, 0, 0, 14], + [-9, 1, 12, 0, 0, 0, 12], + [-8, 0, 0, 12, 0, 1, 11], + [-7, 0, 0, 1, 12, 1, 9], + ], +]; + +/// `FILTER_INTRA_SCALE_BITS` (libaom `reconintra.h`): the taps' fixed-point +/// shift. +const FILTER_INTRA_SCALE_BITS: u32 = 4; + +/// Recursive filter-intra prediction (spec 7.11.2.3, libaom +/// `av1_filter_intra_predictor_c`): walks 4x2 sub-blocks left-to-right, +/// top-to-bottom, each of the eight output samples a linear combination of +/// seven already-decoded or already-predicted neighbours (never past the +/// block's own top row / left column -- unlike the directional modes, this +/// one never reaches beyond `side` samples of either edge). +/// +/// `mode` is `0..=4`, indexing [`FILTER_INTRA_TAPS`]/`FILTER_DC_PRED` +/// through `FILTER_PAETH_PRED`. `above`/`left`/`corner` are exactly +/// [`predict`]'s edge arguments -- a side that does not exist is filled from +/// the side that does and a side that runs out is extended by repeating its +/// last sample, the same [`Edges::build`] rule every other mode uses, even +/// though this mode itself never reaches past its own `side` samples of +/// either edge. +/// +/// # Panics +/// Panics when `dst` is not `side * side` long, or `side` is not a multiple +/// of 4 (spec `av1_filter_intra_allowed_bsize` never offers this mode past +/// 32x32, and only on square blocks: 4, 8, 16 or 32). +pub fn predict_filter_intra( + mode: usize, + above: Option<&[u8]>, + left: Option<&[u8]>, + corner: Option, + side: usize, + dst: &mut [u8], +) { + assert_eq!(dst.len(), side * side, "the destination is the block"); + assert_eq!(side % 4, 0, "filter intra only offers 4/8/16/32"); + let edges = Edges::build(above, left, corner, side); + let taps = &FILTER_INTRA_TAPS[mode]; + // A (side+1)-square buffer: row 0 / column 0 hold the corner and the + // above/left edges, `buffer[r+1][c+1]` the block's own sample at (r, c). + let mut buffer = vec![0i32; (side + 1) * (side + 1)]; + let at = |buffer: &[i32], r: usize, c: usize| buffer[r * (side + 1) + c]; + buffer[0] = edges.above(-1); + for c in 0..side { + buffer[c + 1] = edges.above(c as i32); + } + for r in 0..side { + buffer[(r + 1) * (side + 1)] = edges.left(r as i32); + } + let mut r = 1; + while r < side + 1 { + let mut c = 1; + while c < side + 1 { + let p0 = at(&buffer, r - 1, c - 1); + let p1 = at(&buffer, r - 1, c); + let p2 = at(&buffer, r - 1, c + 1); + let p3 = at(&buffer, r - 1, c + 2); + let p4 = at(&buffer, r - 1, c + 3); + let p5 = at(&buffer, r, c - 1); + let p6 = at(&buffer, r + 1, c - 1); + let p = [p0, p1, p2, p3, p4, p5, p6]; + for k in 0..8 { + let (r_off, c_off) = (k >> 2, k & 3); + let pr: i32 = taps[k].iter().zip(&p).map(|(t, s)| t * s).sum(); + let value = round2(pr, FILTER_INTRA_SCALE_BITS).clamp(0, 255); + let idx = (r + r_off) * (side + 1) + c + c_off; + buffer[idx] = value; + } + c += 4; + } + r += 2; + } + for row in 0..side { + for col in 0..side { + dst[row * side + col] = at(&buffer, row + 1, col + 1) as u8; + } + } +} + /// `paeth_predict` (spec 7.11.2.2): of the three neighbours, the one nearest /// the gradient they describe. fn paeth(above: i32, left: i32, corner: i32) -> i32 {