//! In-loop deblocking filter (spec 8.7), intra-picture scope: every //! macroblock is intra, so boundary strength is 4 on macroblock edges and 3 //! inside (8.7.2.1) — no motion-vector or reference comparisons yet. use crate::dpb::{FRAME, PicPlane, Picture, SliceParams}; use crate::entropy::{FLAG_DECODED, FLAG_INTER, FLAG_PCM, FLAG_TRANS8X8}; use crate::sample::Sample; use crate::tables::{ALPHA, BETA, CHROMA_QP, TC0}; /// Filter parameters of one edge segment. #[derive(Debug, Clone, Copy)] pub struct EdgeParams { /// indexA-resolved alpha threshold. pub alpha: i32, /// indexB-resolved beta threshold. pub beta: i32, /// tC0 for bS < 4 (unused for bS 4). pub tc0: i32, /// Boundary strength (3 or 4 here). pub bs: u8, } /// Resolve thresholds (spec 8.7.2.2) for a luma or chroma edge. /// `qp_avg` = (qPp + qPq + 1) >> 1, offsets are the slice's FilterOffsetA/B. #[inline] pub fn edge_params(qp_avg: i32, offset_a: i32, offset_b: i32, bs: u8, bd: u32) -> EdgeParams { let scale = bd - 8; let index_a = (qp_avg + offset_a).clamp(0, 51) as usize; let index_b = (qp_avg + offset_b).clamp(0, 51) as usize; EdgeParams { alpha: i32::from(ALPHA[index_a]) << scale, beta: i32::from(BETA[index_b]) << scale, tc0: i32::from(TC0[(bs - 1).min(2) as usize][index_a]) << scale, bs, } } /// Map a luma QP to the chroma QP for deblocking (spec 8.7.2 via 8.5.8). #[inline] pub fn chroma_qp(qp_luma: i32, offset: i32, qp_bd_offset: i32) -> i32 { let qpi = (qp_luma + offset).clamp(-qp_bd_offset, 51); if qpi < 0 { qpi } else { i32::from(CHROMA_QP[qpi as usize]) } } /// Filter one line of samples across a luma edge (spec 8.7.2.3 / 8.7.2.4). /// /// `p` indexes the plane; `s` is the distance between consecutive samples /// across the edge (1 for vertical edges, stride for horizontal), and `edge` /// is the index of q0. Samples p3..q3 must be in bounds. #[inline] pub fn filter_luma_line( data: &mut [S], max: i32, edge: usize, s: usize, e: &EdgeParams, ) { let q0 = data[edge].to_i32(); let p0 = data[edge - s].to_i32(); if (p0 - q0).abs() >= e.alpha { return; } let p1 = data[edge - 2 * s].to_i32(); let q1 = data[edge + s].to_i32(); if (p1 - p0).abs() >= e.beta || (q1 - q0).abs() >= e.beta { return; } let p2 = data[edge - 3 * s].to_i32(); let q2 = data[edge + 2 * s].to_i32(); let ap = (p2 - p0).abs(); let aq = (q2 - q0).abs(); if e.bs < 4 { let tc = e.tc0 + i32::from(ap < e.beta) + i32::from(aq < e.beta); let delta = (((q0 - p0) * 4 + (p1 - q1) + 4) >> 3).clamp(-tc, tc); data[edge - s] = S::clip(p0 + delta, max); data[edge] = S::clip(q0 - delta, max); if ap < e.beta { let d = ((p2 + ((p0 + q0 + 1) >> 1) - (p1 << 1)) >> 1).clamp(-e.tc0, e.tc0); data[edge - 2 * s] = S::from_i32(p1 + d); } if aq < e.beta { let d = ((q2 + ((p0 + q0 + 1) >> 1) - (q1 << 1)) >> 1).clamp(-e.tc0, e.tc0); data[edge + s] = S::from_i32(q1 + d); } } else { // bS == 4 (8.7.2.4). let strong = (p0 - q0).abs() < (e.alpha >> 2) + 2; if strong && ap < e.beta { let p3 = data[edge - 4 * s].to_i32(); data[edge - s] = S::from_i32((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3); data[edge - 2 * s] = S::from_i32((p2 + p1 + p0 + q0 + 2) >> 2); data[edge - 3 * s] = S::from_i32((2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) >> 3); } else { data[edge - s] = S::from_i32((2 * p1 + p0 + q1 + 2) >> 2); } if strong && aq < e.beta { let q3 = data[edge + 3 * s].to_i32(); data[edge] = S::from_i32((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3); data[edge + s] = S::from_i32((p0 + q0 + q1 + q2 + 2) >> 2); data[edge + 2 * s] = S::from_i32((2 * q3 + 3 * q2 + q1 + q0 + p0 + 4) >> 3); } else { data[edge] = S::from_i32((2 * q1 + q0 + p1 + 2) >> 2); } } } /// Filter one line across a chroma edge (chromaStyleFilteringFlag = 1). #[inline] pub fn filter_chroma_line( data: &mut [S], max: i32, edge: usize, s: usize, e: &EdgeParams, ) { let q0 = data[edge].to_i32(); let p0 = data[edge - s].to_i32(); if (p0 - q0).abs() >= e.alpha { return; } let p1 = data[edge - 2 * s].to_i32(); let q1 = data[edge + s].to_i32(); if (p1 - p0).abs() >= e.beta || (q1 - q0).abs() >= e.beta { return; } if e.bs < 4 { let tc = e.tc0 + 1; let delta = (((q0 - p0) * 4 + (p1 - q1) + 4) >> 3).clamp(-tc, tc); data[edge - s] = S::clip(p0 + delta, max); data[edge] = S::clip(q0 - delta, max); } else { data[edge - s] = S::from_i32((2 * p1 + p0 + q1 + 2) >> 2); data[edge] = S::from_i32((2 * q1 + q0 + p1 + 2) >> 2); } } /// Vectorised luma edge filter core (spec 8.7.2.3 / 8.7.2.4), 16 lanes. /// /// Takes the eight sample vectors across the edge, returns the six filtered /// vectors `[p2', p1', p0', q0', q1', q2']`, or `None` when no lane passes /// the filterSamplesFlag gate. Bit-identical to [`filter_luma_line`] per /// lane. #[allow(clippy::too_many_arguments)] fn luma_edge_core16( p3: wide::i16x16, p2: wide::i16x16, p1: wide::i16x16, p0: wide::i16x16, q0: wide::i16x16, q1: wide::i16x16, q2: wide::i16x16, q3: wide::i16x16, e: &EdgeParams, ) -> Option<[wide::i16x16; 6]> { use wide::i16x16; let alpha = i16x16::splat(e.alpha as i16); let beta = i16x16::splat(e.beta as i16); let filter = (p0 - q0).abs().simd_lt(alpha) & (p1 - p0).abs().simd_lt(beta) & (q1 - q0).abs().simd_lt(beta); if !filter.any() { return None; } let ap = (p2 - p0).abs().simd_lt(beta); let aq = (q2 - q0).abs().simd_lt(beta); let one = i16x16::ONE; Some(if e.bs < 4 { let tc0 = i16x16::splat(e.tc0 as i16); let tc = tc0 + ap.select(one, i16x16::ZERO) + aq.select(one, i16x16::ZERO); let four = i16x16::splat(4); let delta = ((((q0 - p0) << 2u32) + (p1 - q1) + four) >> 3u32) .max(-tc) .min(tc); let avg = (p0 + q0 + one) >> 1u32; let dp1 = ((p2 + avg - (p1 << 1u32)) >> 1u32).max(-tc0).min(tc0); let dq1 = ((q2 + avg - (q1 << 1u32)) >> 1u32).max(-tc0).min(tc0); [ p2, (filter & ap).select(p1 + dp1, p1), filter.select(p0 + delta, p0), filter.select(q0 - delta, q0), (filter & aq).select(q1 + dq1, q1), q2, ] } else { let two = i16x16::splat(2); let four = i16x16::splat(4); let strong = (p0 - q0) .abs() .simd_lt(i16x16::splat(((e.alpha >> 2) + 2) as i16)); let sp = filter & strong & ap; let sq = filter & strong & aq; let p0s = (p2 + (p1 << 1u32) + (p0 << 1u32) + (q0 << 1u32) + q1 + four) >> 3u32; let p1s = (p2 + p1 + p0 + q0 + two) >> 2u32; let p2s = ((p3 << 1u32) + p2 + (p2 << 1u32) + p1 + p0 + q0 + four) >> 3u32; let p0w = ((p1 << 1u32) + p0 + q1 + two) >> 2u32; let q0s = (p1 + (p0 << 1u32) + (q0 << 1u32) + (q1 << 1u32) + q2 + four) >> 3u32; let q1s = (p0 + q0 + q1 + q2 + two) >> 2u32; let q2s = ((q3 << 1u32) + q2 + (q2 << 1u32) + q1 + q0 + p0 + four) >> 3u32; let q0w = ((q1 << 1u32) + q0 + p1 + two) >> 2u32; [ sp.select(p2s, p2), sp.select(p1s, p1), sp.select(p0s, filter.select(p0w, p0)), sq.select(q0s, filter.select(q0w, q0)), sq.select(q1s, q1), sq.select(q2s, q2), ] }) } #[inline] fn load_row16(data: &[u8], at: usize) -> wide::i16x16 { let mut a = [0i16; 16]; for (o, &b) in a.iter_mut().zip(&data[at..at + 16]) { *o = i16::from(b); } wide::i16x16::from(a) } #[inline] fn store_row16(data: &mut [u8], at: usize, v: wide::i16x16) { let clipped = v.max(wide::i16x16::ZERO).min(wide::i16x16::splat(255)); for (o, &s) in data[at..at + 16].iter_mut().zip(clipped.as_array().iter()) { *o = s as u8; } } /// Filter a whole horizontal luma edge (16 samples wide): the eight rows /// around the edge are contiguous, so the filter runs 16 lanes at once with /// masked selects instead of per-pixel branches. `q0_row` indexes the first /// sample of the q0 row. pub fn filter_luma_h_edge16(data: &mut [u8], q0_row: usize, stride: usize, e: &EdgeParams) { let p3 = load_row16(data, q0_row - 4 * stride); let p2 = load_row16(data, q0_row - 3 * stride); let p1 = load_row16(data, q0_row - 2 * stride); let p0 = load_row16(data, q0_row - stride); let q0 = load_row16(data, q0_row); let q1 = load_row16(data, q0_row + stride); let q2 = load_row16(data, q0_row + 2 * stride); let q3 = load_row16(data, q0_row + 3 * stride); let Some(out) = luma_edge_core16(p3, p2, p1, p0, q0, q1, q2, q3, e) else { return; }; store_row16(data, q0_row - 3 * stride, out[0]); store_row16(data, q0_row - 2 * stride, out[1]); store_row16(data, q0_row - stride, out[2]); store_row16(data, q0_row, out[3]); store_row16(data, q0_row + stride, out[4]); store_row16(data, q0_row + 2 * stride, out[5]); } /// Filter a whole horizontal chroma edge (eight samples wide) in one pass. /// /// The four rows around a horizontal edge are contiguous, so the eight lines of /// the edge are eight lanes of one vector instead of eight scalar calls. Bit- /// identical to [`filter_chroma_line`] per lane. pub fn filter_chroma_h_edge8(data: &mut [u8], q0_row: usize, stride: usize, e: &EdgeParams) { use wide::i16x8; let load = |at: usize| -> i16x8 { let mut a = [0i16; 8]; for (o, &b) in a.iter_mut().zip(&data[at..at + 8]) { *o = i16::from(b); } i16x8::from(a) }; let p1 = load(q0_row - 2 * stride); let p0 = load(q0_row - stride); let q0 = load(q0_row); let q1 = load(q0_row + stride); let alpha = i16x8::splat(e.alpha as i16); let beta = i16x8::splat(e.beta as i16); let filter = (p0 - q0).abs().simd_lt(alpha) & (p1 - p0).abs().simd_lt(beta) & (q1 - q0).abs().simd_lt(beta); if !filter.any() { return; } let two = i16x8::splat(2); let (p0n, q0n) = if e.bs < 4 { let tc = i16x8::splat((e.tc0 + 1) as i16); let delta = ((((q0 - p0) << 2u32) + (p1 - q1) + i16x8::splat(4)) >> 3u32) .max(-tc) .min(tc); (p0 + delta, q0 - delta) } else { ( ((p1 << 1u32) + p0 + q1 + two) >> 2u32, ((q1 << 1u32) + q0 + p1 + two) >> 2u32, ) }; let clip = |v: i16x8| v.max(i16x8::ZERO).min(i16x8::splat(255)); let store = |data: &mut [u8], at: usize, v: i16x8| { for (o, &s) in data[at..at + 8].iter_mut().zip(clip(v).as_array()) { *o = s as u8; } }; store(data, q0_row - stride, filter.select(p0n, p0)); store(data, q0_row, filter.select(q0n, q0)); } #[cfg(test)] mod tests { use super::*; #[test] fn thresholds_clamp_and_lookup() { let e = edge_params(26, 0, 0, 3, 8); assert_eq!(e.alpha, i32::from(ALPHA[26])); assert_eq!(e.beta, i32::from(BETA[26])); assert_eq!(e.tc0, i32::from(TC0[2][26])); // Offsets push the index outside [0, 51] and clamp. let e2 = edge_params(51, 12, 12, 4, 8); assert_eq!(e2.alpha, 255); let e3 = edge_params(0, -12, -12, 3, 8); assert_eq!(e3.alpha, 0); assert_eq!(chroma_qp(51, 0, 0), 39); assert_eq!(chroma_qp(20, 2, 0), 22); } /// A hard step across a bS=4 edge at moderate QP gets smoothed; a flat /// area is untouched. #[test] fn strong_filter_smooths_step() { // Line: p3 p2 p1 p0 | q0 q1 q2 q3. let mut line = [60u8, 60, 60, 60, 80, 80, 80, 80]; let e = edge_params(32, 0, 0, 4, 8); filter_luma_line(&mut line, 255, 4, 1, &e); // (p0, q0) move toward each other. assert!(line[3] > 60 && line[4] < 80, "{line:?}"); let mut flat = [90u8; 8]; filter_luma_line(&mut flat, 255, 4, 1, &e); assert_eq!(flat, [90; 8]); } /// Above alpha the edge is treated as a real feature and left alone. #[test] fn large_step_is_preserved() { let mut line = [10u8, 10, 10, 10, 200, 200, 200, 200]; let e = edge_params(26, 0, 0, 4, 8); filter_luma_line(&mut line, 255, 4, 1, &e); assert_eq!(line, [10, 10, 10, 10, 200, 200, 200, 200]); } #[test] fn chroma_filter_touches_only_p0_q0() { let mut line = [60u8, 60, 60, 60, 74, 74, 74, 74]; let e = edge_params(32, 0, 0, 3, 8); filter_chroma_line(&mut line, 255, 4, 1, &e); assert_eq!(line[2], 60); assert_eq!(line[5], 74); assert!(line[3] >= 60 && line[4] <= 74); assert!(line[3] != 60 || line[4] != 74); } } /// Whole-picture deblocking (spec 8.7): macroblocks in raster order, all /// vertical edges then all horizontal edges per macroblock, then chroma. // `e` and `j` are edge and segment positions inside the macroblock, used to // index the plane as well as the strength arrays; iterating the arrays would // hide which geometry each one names. #[allow(clippy::needless_range_loop)] pub(crate) fn deblock_picture(pic: &mut Picture) { let mb_w = pic.mb_w; let bd = pic.bd; let max = (1i32 << bd) - 1; let qp_bd = 6 * (bd as i32 - 8); let cmb_h = pic.cmb_h; let cat = pic.cat; let lcomps = if cat == 3 { 3 } else { 1 }; for mb_y in 0..pic.mb_h { for mb_x in 0..mb_w { let addr = mb_y * mb_w + mb_x; if pic.mb_flags[addr] & FLAG_DECODED == 0 { continue; } let sid = pic.mb_slice[addr]; let sp = pic.slices[sid as usize]; if sp.disable_deblock_idc == 1 { continue; } let decoded = |a: usize| pic.mb_flags[a] & FLAG_DECODED != 0; let cross_ok = |a: usize| sp.disable_deblock_idc != 2 || pic.mb_slice[a] == sid; let filter_left = mb_x > 0 && decoded(addr - 1) && cross_ok(addr - 1); let filter_top = mb_y > 0 && decoded(addr - mb_w) && cross_ok(addr - mb_w); // Deblocking QP: 0 for I_PCM macroblocks (8.7.2). let qp_of = |a: usize| -> i32 { if pic.mb_flags[a] & FLAG_PCM != 0 { 0 } else { i32::from(pic.mb_qp[a]) } }; // 8.7: an 8x8-transform macroblock has no transform edge at luma // 4 or 12, so those internal edges are not filtered. Chroma is // unaffected for 4:2:0 (its internal edge sits on luma edge 8). let internal = |e: usize| e.is_multiple_of(2) || pic.mb_flags[addr] & FLAG_TRANS8X8 == 0; // Boundary strengths, per 4-sample segment of each edge (8.7.2.1). // // Both sides of an internal edge live in this macroblock, so the // whole 8.7.2.1 ladder is decided once for all 24 of them: an intra // macroblock gives 3 everywhere, and an inter macroblock with no // luma coefficients and one motion vector over the whole 16x16 // gives 0 everywhere. Only the two macroblock edges then need the // per-segment derivation. let mut bs_v = [[0u8; 4]; 4]; let mut bs_h = [[0u8; 4]; 4]; let cur_intra = pic.mb_flags[addr] & FLAG_INTER == 0; let internal_uniform = if cur_intra { Some(3) } else if pic.mb_cbp[addr] == 0 && uniform_motion(pic, mb_x, mb_y) { Some(0) } else { None }; for k in 0..4 { if filter_left { bs_v[0][k] = boundary_strength(pic, mb_x, mb_y, 0, k, true); } if filter_top { bs_h[0][k] = boundary_strength(pic, mb_x, mb_y, 0, k, false); } } for e in 1..4 { match internal_uniform { Some(bs) => { bs_v[e] = [bs; 4]; bs_h[e] = [bs; 4]; } None => { for k in 0..4 { bs_v[e][k] = boundary_strength(pic, mb_x, mb_y, e, k, true); bs_h[e][k] = boundary_strength(pic, mb_x, mb_y, e, k, false); } } } } // ---- luma (and Cb/Cr, coded like luma, for ChromaArrayType 3) ---- for comp in 0..lcomps { let pl: &mut PicPlane = match comp { 0 => &mut pic.y, 1 => &mut pic.cb, _ => &mut pic.cr, }; let stride = pl.stride; let base = pl.at(mb_x * 16, mb_y * 16); let qpf = |a: usize| -> i32 { if comp == 0 { qp_of(a) } else { let s = pic.slices[pic.mb_slice[a] as usize]; let off = if comp == 1 { s.cb_qp_offset } else { s.cr_qp_offset }; let q = if pic.mb_flags[a] & FLAG_PCM != 0 { 0 } else { i32::from(pic.mb_qp[a]) }; chroma_qp(q, off, qp_bd) } }; let qp_q = qpf(addr); // Lossless macroblocks keep their samples (8.7.2.2). let lossless = |a: usize| { pic.bypass_enabled && pic.mb_flags[a] & FLAG_PCM == 0 && i32::from(pic.mb_qp[a]) + qp_bd == 0 }; let (p_ll_v, q_ll) = (filter_left && lossless(addr - 1), lossless(addr)); let snap_v = (p_ll_v || (filter_left && q_ll)) .then(|| snap_edge(&pl.data, base, stride, true)); // Vertical edges (filter across columns), left to right. for e in 0..4 { if e == 0 && !filter_left { continue; } if !internal(e) { continue; } let qp_p = if e == 0 { qpf(addr - 1) } else { qp_q }; let qp_avg = (qp_p + qp_q + 1) >> 1; // Vertical edges stay scalar: a gather/scatter SIMD // variant measured slower than this loop (strided // per-lane loads dominate). for j in 0..4 { let bs = bs_v[e][j]; if bs == 0 { continue; } let params = edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs, bd); if params.alpha == 0 || params.beta == 0 { continue; } for k in 0..4 { let row = j * 4 + k; filter_luma_line( &mut pl.data, max, base + row * stride + e * 4, 1, ¶ms, ); } } } if let Some(sv) = &snap_v { restore_edge(&mut pl.data, sv, base, stride, true, p_ll_v, q_ll); } let p_ll_h = filter_top && lossless(addr - mb_w); let snap_h = (p_ll_h || (filter_top && q_ll)) .then(|| snap_edge(&pl.data, base, stride, false)); // Horizontal edges, top to bottom. for e in 0..4 { if e == 0 && !filter_top { continue; } if !internal(e) { continue; } let qp_p = if e == 0 { qpf(addr - mb_w) } else { qp_q }; let qp_avg = (qp_p + qp_q + 1) >> 1; let bs = bs_h[e]; let uniform = bs[0] == bs[1] && bs[1] == bs[2] && bs[2] == bs[3]; if uniform { if bs[0] == 0 { continue; } let params = edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs[0], bd); if params.alpha == 0 || params.beta == 0 { continue; } // Eight rows around the edge are contiguous, so the // whole 16-sample edge filters in one pass. S::filter_luma_h_edge16( &mut pl.data, max, base + e * 4 * stride, stride, ¶ms, ); continue; } for j in 0..4 { if bs[j] == 0 { continue; } let params = edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs[j], bd); if params.alpha == 0 || params.beta == 0 { continue; } for k in 0..4 { filter_luma_line( &mut pl.data, max, base + e * 4 * stride + j * 4 + k, stride, ¶ms, ); } } } if let Some(sh) = &snap_h { restore_edge(&mut pl.data, sh, base, stride, false, p_ll_h, q_ll); } } // ---- chroma (Cb, Cr) ---- for comp in 0..if matches!(cat, 1 | 2) { 2 } else { 0 } { let (plane, off_sel): (&mut PicPlane, fn(&SliceParams) -> i32) = if comp == 0 { (&mut pic.cb, |s| s.cb_qp_offset) } else { (&mut pic.cr, |s| s.cr_qp_offset) }; let stride = plane.stride; let c422 = cat == 2; let base = plane.origin + (mb_y * cmb_h) * stride + mb_x * 8; // Chroma QP per side: each macroblock's luma QP mapped with // the offset of ITS OWN slice's PPS (8.7.2). let cqp = |a: usize| -> i32 { let s = pic.slices[pic.mb_slice[a] as usize]; if pic.mb_flags[a] & FLAG_PCM != 0 { chroma_qp(0, off_sel(&s), qp_bd) } else { chroma_qp(i32::from(pic.mb_qp[a]), off_sel(&s), qp_bd) } }; let cq_q = cqp(addr); // A chroma edge inherits the boundary strength of the luma edge // it sits on: chroma sample k maps to luma sample 2k, so a // chroma 4-sample run spans two luma 4x4 block rows. for e in 0..2 { if e == 0 && !filter_left { continue; } let cq_p = if e == 0 { cqp(addr - 1) } else { cq_q }; let qp_avg = (cq_p + cq_q + 1) >> 1; for k in 0..cmb_h { let bs = bs_v[e * 2][k >> if c422 { 2 } else { 1 }]; if bs == 0 { continue; } let params = edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs, bd); if params.alpha == 0 || params.beta == 0 { continue; } filter_chroma_line( &mut plane.data, max, base + k * stride + e * 4, 1, ¶ms, ); } } // 4:2:2 chroma is as tall as luma: its horizontal 4x4 transform // edges sit at rows 0, 4, 8 and 12, each on the boundary // strength of the luma edge on the same row. for e in 0..if c422 { 4 } else { 2 } { if e == 0 && !filter_top { continue; } let cq_p = if e == 0 { cqp(addr - mb_w) } else { cq_q }; let qp_avg = (cq_p + cq_q + 1) >> 1; let bs = bs_h[if c422 { e } else { e * 2 }]; if bs[0] == bs[1] && bs[1] == bs[2] && bs[2] == bs[3] { if bs[0] == 0 { continue; } let params = edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs[0], bd); if params.alpha == 0 || params.beta == 0 { continue; } // The four rows around the edge are contiguous, so the // whole eight-sample edge filters in one pass. S::filter_chroma_h_edge8( &mut plane.data, max, base + e * 4 * stride, stride, ¶ms, ); continue; } for k in 0..8 { let bs = bs[k / 2]; if bs == 0 { continue; } let params = edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs, bd); if params.alpha == 0 || params.beta == 0 { continue; } filter_chroma_line( &mut plane.data, max, base + e * 4 * stride + k, stride, ¶ms, ); } } } } } } /// Copy the 8 samples across a macroblock edge (4 each side) along its 16 /// samples, so a lossless side can be put back after the filter ran. fn snap_edge(data: &[S], base: usize, stride: usize, vertical: bool) -> Vec { let mut v = Vec::with_capacity(128); for i in 0..16 { for d in 0..8 { v.push(if vertical { data[base + i * stride + d - 4] } else { data[base + d * stride + i - 4 * stride] }); } } v } fn restore_edge( data: &mut [S], snap: &[S], base: usize, stride: usize, vertical: bool, p: bool, q: bool, ) { for i in 0..16 { for d in 0..8 { if (d < 4 && !p) || (d >= 4 && !q) { continue; } let idx = if vertical { base + i * stride + d - 4 } else { base + d * stride + i - 4 * stride }; data[idx] = snap[i * 8 + d]; } } } /// True when every 4x4 block of this macroblock predicts from the same /// pictures with the same motion, so no internal edge can reach the motion /// clauses of 8.7.2.1. fn uniform_motion(pic: &Picture, mb_x: usize, mb_y: usize) -> bool { let w4 = pic.mb_w * 4; let first = mb_y * 4 * w4 + mb_x * 4; let (mv, id) = (pic.mv[first], pic.ref_id[first]); (0..4).all(|by| { let row = first + by * w4; (0..4).all(|bx| pic.mv[row + bx] == mv && pic.ref_id[row + bx] == id) }) } /// What clause 8.7.2.1 needs from the 4x4 block on one side of a luma edge. #[derive(Clone, Copy)] pub(crate) struct BsSide { /// The containing macroblock is intra: strength 4 on macroblock edges and /// 3 on internal ones, regardless of motion or levels. pub(crate) intra: bool, /// The transform block containing this sample has non-zero levels. pub(crate) coded: bool, /// MvLX of the block, `[list][component]`. pub(crate) mv: [[i16; 2]; 2], /// RefIdxLX of the block as list identities (`Picture::id`), -1 unused. pub(crate) refs: [i32; 2], } /// Boundary strength between the 4x4 blocks on the two sides of a luma edge /// (clause 8.7.2.1). `mb_edge` selects the macroblock-edge strengths 4/3. pub(crate) fn bs_between(p: &BsSide, q: &BsSide, mb_edge: bool, field: bool, vertical: bool) -> u8 { if p.intra || q.intra { // A horizontal edge of a field picture is never strength 4 (8.7.2.1). return if mb_edge && (vertical || !field) { 4 } else { 3 }; } if p.coded || q.coded { return 2; } let (pr, qr) = (p.refs, q.refs); let pn = u8::from(pr[0] >= 0) + u8::from(pr[1] >= 0); let qn = u8::from(qr[0] >= 0) + u8::from(qr[1] >= 0); if pn != qn { return 1; } let (pm, qm) = (p.mv, q.mv); // The vertical limit is 4 quarter frame samples: 2 in field units. let ylim = if field { 2 } else { 4 }; let differs = |a: [i16; 2], b: [i16; 2]| { (i32::from(a[0]) - i32::from(b[0])).abs() >= 4 || (i32::from(a[1]) - i32::from(b[1])).abs() >= ylim }; if pn == 1 { let pl = usize::from(pr[0] < 0); let ql = usize::from(qr[0] < 0); if pr[pl] != qr[ql] { return 1; } return u8::from(differs(pm[pl], qm[ql])); } if pn == 0 { return 0; } // Two motion vectors each: compare the two partitions by which pictures // they reference, not by list position (NOTE 1 of 8.7.2.1). let same_set = (pr[0] == qr[0] && pr[1] == qr[1]) || (pr[0] == qr[1] && pr[1] == qr[0]); if !same_set { return 1; } if pr[0] != pr[1] { return if pr[0] == qr[0] { u8::from(differs(pm[0], qm[0]) || differs(pm[1], qm[1])) } else { u8::from(differs(pm[0], qm[1]) || differs(pm[1], qm[0])) }; } // Both lists name the same picture: either pairing agreeing is enough. let straight = !differs(pm[0], qm[0]) && !differs(pm[1], qm[1]); let crossed = !differs(pm[0], qm[1]) && !differs(pm[1], qm[0]); u8::from(!(straight || crossed)) } /// One side of a luma edge from picture state: the decoder path, both sides /// final macroblocks. The encoder's inter trial uses this for the /// already-coded neighbour and builds its candidate side itself. pub(crate) fn bs_side(pic: &Picture, bx: usize, by: usize) -> BsSide { let w4 = pic.mb_w * 4; let mb = (by / 4) * pic.mb_w + bx / 4; BsSide { intra: pic.mb_flags[mb] & FLAG_INTER == 0, coded: bs_coded(pic, mb, bx, by), mv: pic.mv[by * w4 + bx], refs: pic.ref_id[by * w4 + bx], } } /// "The transform block containing the sample has non-zero levels": for an /// 8x8-transform macroblock that is the whole 8x8, whose four 4x4 slots /// carry per-slot counts under CAVLC (7.3.5.3.1 splits it into four blocks). fn bs_coded(pic: &Picture, mb: usize, bx: usize, by: usize) -> bool { let w4 = pic.mb_w * 4; if pic.mb_flags[mb] & FLAG_TRANS8X8 == 0 { return pic.nz_y[by * w4 + bx] != 0; } let (ox, oy) = (bx & !1, by & !1); pic.nz_y[oy * w4 + ox] != 0 || pic.nz_y[oy * w4 + ox + 1] != 0 || pic.nz_y[(oy + 1) * w4 + ox] != 0 || pic.nz_y[(oy + 1) * w4 + ox + 1] != 0 } /// Boundary strength of one 4-sample luma edge segment (clause 8.7.2.1). /// /// `edge` 0..4 selects the vertical (`vertical`) or horizontal edge inside the /// macroblock, `seg` the 4-sample run along it. Edge 0 is the macroblock edge. fn boundary_strength( pic: &Picture, mb_x: usize, mb_y: usize, edge: usize, seg: usize, vertical: bool, ) -> u8 { let (qbx, qby) = if vertical { (mb_x * 4 + edge, mb_y * 4 + seg) } else { (mb_x * 4 + seg, mb_y * 4 + edge) }; let (pbx, pby) = if vertical { (qbx - 1, qby) } else { (qbx, qby - 1) }; bs_between( &bs_side(pic, pbx, pby), &bs_side(pic, qbx, qby), edge == 0, pic.structure != FRAME, vertical, ) }