diff --git a/crates/ec-h264/src/deblock.rs b/crates/ec-h264/src/deblock.rs index 85a9067d..283ecc05 100644 --- a/crates/ec-h264/src/deblock.rs +++ b/crates/ec-h264/src/deblock.rs @@ -2,6 +2,8 @@ //! 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}; @@ -356,3 +358,506 @@ mod tests { 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, + ) +} diff --git a/crates/ec-h264/src/deblock_mbaff.rs b/crates/ec-h264/src/deblock_mbaff.rs index b820780b..fef065c3 100644 --- a/crates/ec-h264/src/deblock_mbaff.rs +++ b/crates/ec-h264/src/deblock_mbaff.rs @@ -19,7 +19,7 @@ //! progressive filter stays the one the speed budget is spent on. use crate::deblock::{chroma_qp, edge_params, filter_chroma_line, filter_luma_line}; -use crate::decoder::{BsSide, bs_between, bs_side}; +use crate::deblock::{BsSide, bs_between, bs_side}; use crate::dpb::{PicPlane, Picture, SliceParams}; use crate::entropy::{FLAG_DECODED, FLAG_PCM, FLAG_TRANS8X8}; use crate::sample::Sample; diff --git a/crates/ec-h264/src/decoder.rs b/crates/ec-h264/src/decoder.rs index fb191b01..fd4b0220 100644 --- a/crates/ec-h264/src/decoder.rs +++ b/crates/ec-h264/src/decoder.rs @@ -29,7 +29,7 @@ use ec_h264_syntax::{ SliceType, Sps, WeightEntry, unescape_rbsp, }; -use crate::deblock::{chroma_qp, edge_params, filter_chroma_line, filter_luma_line}; +use crate::deblock::{chroma_qp, deblock_picture}; use crate::dpb::{ BLK_DIRECT, BLK_INTRA, BLK_SKIP, BOTTOM, CODED_AFRM, CODED_FLD, Dpb, FRAME, Mark, NO_SLICE, PicInfo, PicPlane, Picture, RefList, SliceParams, TOP, @@ -4012,508 +4012,6 @@ pub(crate) fn gather_nbr8_in( } } -/// 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, - ) -} #[cfg(test)] mod tests { diff --git a/crates/ec-h264/src/enc/mod.rs b/crates/ec-h264/src/enc/mod.rs index 4c9e7a83..58c95232 100644 --- a/crates/ec-h264/src/enc/mod.rs +++ b/crates/ec-h264/src/enc/mod.rs @@ -34,7 +34,7 @@ use ec_core::error::{Error, Result}; use ec_h264_syntax::nal::{NalUnitType, escape_rbsp}; use ec_h264_syntax::{SliceType, Sps}; -use crate::decoder::deblock_picture; +use crate::deblock::deblock_picture; use crate::dpb::{Picture, SliceParams as DeblockParams}; use crate::entropy::{FLAG_INTER, FLAG_TRANS8X8}; use crate::transform::{LevelScale4x4, LevelScale8x8};