forge
Explore
Log in

Don't you dare go Hollow

S8: move deblock_picture and boundary-strength helpers into deblock.rs; deblock_mbaff and enc import them there instead of from decoder

Browse files at this commit

87aa56b49dc61503c45ac7b0c030a135a811ab07Ahmet Kaan GümüşParent: d0af163
crates/ec-h264/src/deblock.rsmodified+505 -0
@@ -2,6 +2,8 @@
22 //! macroblock is intra, so boundary strength is 4 on macroblock edges and 3
33 //! inside (8.7.2.1) — no motion-vector or reference comparisons yet.
44
5+use crate::dpb::{FRAME, PicPlane, Picture, SliceParams};
6+use crate::entropy::{FLAG_DECODED, FLAG_INTER, FLAG_PCM, FLAG_TRANS8X8};
57 use crate::sample::Sample;
68 use crate::tables::{ALPHA, BETA, CHROMA_QP, TC0};
79
@@ -356,3 +358,506 @@ mod tests {
356358 assert!(line[3] != 60 || line[4] != 74);
357359 }
358360 }
361+
362+/// Whole-picture deblocking (spec 8.7): macroblocks in raster order, all
363+/// vertical edges then all horizontal edges per macroblock, then chroma.
364+// `e` and `j` are edge and segment positions inside the macroblock, used to
365+// index the plane as well as the strength arrays; iterating the arrays would
366+// hide which geometry each one names.
367+#[allow(clippy::needless_range_loop)]
368+pub(crate) fn deblock_picture<S: Sample>(pic: &mut Picture<S>) {
369+ let mb_w = pic.mb_w;
370+ let bd = pic.bd;
371+ let max = (1i32 << bd) - 1;
372+ let qp_bd = 6 * (bd as i32 - 8);
373+ let cmb_h = pic.cmb_h;
374+ let cat = pic.cat;
375+ let lcomps = if cat == 3 { 3 } else { 1 };
376+ for mb_y in 0..pic.mb_h {
377+ for mb_x in 0..mb_w {
378+ let addr = mb_y * mb_w + mb_x;
379+ if pic.mb_flags[addr] & FLAG_DECODED == 0 {
380+ continue;
381+ }
382+ let sid = pic.mb_slice[addr];
383+ let sp = pic.slices[sid as usize];
384+ if sp.disable_deblock_idc == 1 {
385+ continue;
386+ }
387+ let decoded = |a: usize| pic.mb_flags[a] & FLAG_DECODED != 0;
388+ let cross_ok = |a: usize| sp.disable_deblock_idc != 2 || pic.mb_slice[a] == sid;
389+ let filter_left = mb_x > 0 && decoded(addr - 1) && cross_ok(addr - 1);
390+ let filter_top = mb_y > 0 && decoded(addr - mb_w) && cross_ok(addr - mb_w);
391+ // Deblocking QP: 0 for I_PCM macroblocks (8.7.2).
392+ let qp_of = |a: usize| -> i32 {
393+ if pic.mb_flags[a] & FLAG_PCM != 0 {
394+ 0
395+ } else {
396+ i32::from(pic.mb_qp[a])
397+ }
398+ };
399+ // 8.7: an 8x8-transform macroblock has no transform edge at luma
400+ // 4 or 12, so those internal edges are not filtered. Chroma is
401+ // unaffected for 4:2:0 (its internal edge sits on luma edge 8).
402+ let internal =
403+ |e: usize| e.is_multiple_of(2) || pic.mb_flags[addr] & FLAG_TRANS8X8 == 0;
404+
405+ // Boundary strengths, per 4-sample segment of each edge (8.7.2.1).
406+ //
407+ // Both sides of an internal edge live in this macroblock, so the
408+ // whole 8.7.2.1 ladder is decided once for all 24 of them: an intra
409+ // macroblock gives 3 everywhere, and an inter macroblock with no
410+ // luma coefficients and one motion vector over the whole 16x16
411+ // gives 0 everywhere. Only the two macroblock edges then need the
412+ // per-segment derivation.
413+ let mut bs_v = [[0u8; 4]; 4];
414+ let mut bs_h = [[0u8; 4]; 4];
415+ let cur_intra = pic.mb_flags[addr] & FLAG_INTER == 0;
416+ let internal_uniform = if cur_intra {
417+ Some(3)
418+ } else if pic.mb_cbp[addr] == 0 && uniform_motion(pic, mb_x, mb_y) {
419+ Some(0)
420+ } else {
421+ None
422+ };
423+ for k in 0..4 {
424+ if filter_left {
425+ bs_v[0][k] = boundary_strength(pic, mb_x, mb_y, 0, k, true);
426+ }
427+ if filter_top {
428+ bs_h[0][k] = boundary_strength(pic, mb_x, mb_y, 0, k, false);
429+ }
430+ }
431+ for e in 1..4 {
432+ match internal_uniform {
433+ Some(bs) => {
434+ bs_v[e] = [bs; 4];
435+ bs_h[e] = [bs; 4];
436+ }
437+ None => {
438+ for k in 0..4 {
439+ bs_v[e][k] = boundary_strength(pic, mb_x, mb_y, e, k, true);
440+ bs_h[e][k] = boundary_strength(pic, mb_x, mb_y, e, k, false);
441+ }
442+ }
443+ }
444+ }
445+
446+ // ---- luma (and Cb/Cr, coded like luma, for ChromaArrayType 3) ----
447+ for comp in 0..lcomps {
448+ let pl: &mut PicPlane<S> = match comp {
449+ 0 => &mut pic.y,
450+ 1 => &mut pic.cb,
451+ _ => &mut pic.cr,
452+ };
453+ let stride = pl.stride;
454+ let base = pl.at(mb_x * 16, mb_y * 16);
455+ let qpf = |a: usize| -> i32 {
456+ if comp == 0 {
457+ qp_of(a)
458+ } else {
459+ let s = pic.slices[pic.mb_slice[a] as usize];
460+ let off = if comp == 1 {
461+ s.cb_qp_offset
462+ } else {
463+ s.cr_qp_offset
464+ };
465+ let q = if pic.mb_flags[a] & FLAG_PCM != 0 {
466+ 0
467+ } else {
468+ i32::from(pic.mb_qp[a])
469+ };
470+ chroma_qp(q, off, qp_bd)
471+ }
472+ };
473+ let qp_q = qpf(addr);
474+ // Lossless macroblocks keep their samples (8.7.2.2).
475+ let lossless = |a: usize| {
476+ pic.bypass_enabled
477+ && pic.mb_flags[a] & FLAG_PCM == 0
478+ && i32::from(pic.mb_qp[a]) + qp_bd == 0
479+ };
480+ let (p_ll_v, q_ll) = (filter_left && lossless(addr - 1), lossless(addr));
481+ let snap_v = (p_ll_v || (filter_left && q_ll))
482+ .then(|| snap_edge(&pl.data, base, stride, true));
483+ // Vertical edges (filter across columns), left to right.
484+ for e in 0..4 {
485+ if e == 0 && !filter_left {
486+ continue;
487+ }
488+ if !internal(e) {
489+ continue;
490+ }
491+ let qp_p = if e == 0 { qpf(addr - 1) } else { qp_q };
492+ let qp_avg = (qp_p + qp_q + 1) >> 1;
493+ // Vertical edges stay scalar: a gather/scatter SIMD
494+ // variant measured slower than this loop (strided
495+ // per-lane loads dominate).
496+ for j in 0..4 {
497+ let bs = bs_v[e][j];
498+ if bs == 0 {
499+ continue;
500+ }
501+ let params = edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs, bd);
502+ if params.alpha == 0 || params.beta == 0 {
503+ continue;
504+ }
505+ for k in 0..4 {
506+ let row = j * 4 + k;
507+ filter_luma_line(
508+ &mut pl.data,
509+ max,
510+ base + row * stride + e * 4,
511+ 1,
512+ &params,
513+ );
514+ }
515+ }
516+ }
517+ if let Some(sv) = &snap_v {
518+ restore_edge(&mut pl.data, sv, base, stride, true, p_ll_v, q_ll);
519+ }
520+ let p_ll_h = filter_top && lossless(addr - mb_w);
521+ let snap_h = (p_ll_h || (filter_top && q_ll))
522+ .then(|| snap_edge(&pl.data, base, stride, false));
523+ // Horizontal edges, top to bottom.
524+ for e in 0..4 {
525+ if e == 0 && !filter_top {
526+ continue;
527+ }
528+ if !internal(e) {
529+ continue;
530+ }
531+ let qp_p = if e == 0 { qpf(addr - mb_w) } else { qp_q };
532+ let qp_avg = (qp_p + qp_q + 1) >> 1;
533+ let bs = bs_h[e];
534+ let uniform = bs[0] == bs[1] && bs[1] == bs[2] && bs[2] == bs[3];
535+ if uniform {
536+ if bs[0] == 0 {
537+ continue;
538+ }
539+ let params =
540+ edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs[0], bd);
541+ if params.alpha == 0 || params.beta == 0 {
542+ continue;
543+ }
544+ // Eight rows around the edge are contiguous, so the
545+ // whole 16-sample edge filters in one pass.
546+ S::filter_luma_h_edge16(
547+ &mut pl.data,
548+ max,
549+ base + e * 4 * stride,
550+ stride,
551+ &params,
552+ );
553+ continue;
554+ }
555+ for j in 0..4 {
556+ if bs[j] == 0 {
557+ continue;
558+ }
559+ let params =
560+ edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs[j], bd);
561+ if params.alpha == 0 || params.beta == 0 {
562+ continue;
563+ }
564+ for k in 0..4 {
565+ filter_luma_line(
566+ &mut pl.data,
567+ max,
568+ base + e * 4 * stride + j * 4 + k,
569+ stride,
570+ &params,
571+ );
572+ }
573+ }
574+ }
575+ if let Some(sh) = &snap_h {
576+ restore_edge(&mut pl.data, sh, base, stride, false, p_ll_h, q_ll);
577+ }
578+ }
579+
580+ // ---- chroma (Cb, Cr) ----
581+ for comp in 0..if matches!(cat, 1 | 2) { 2 } else { 0 } {
582+ let (plane, off_sel): (&mut PicPlane<S>, fn(&SliceParams) -> i32) = if comp == 0 {
583+ (&mut pic.cb, |s| s.cb_qp_offset)
584+ } else {
585+ (&mut pic.cr, |s| s.cr_qp_offset)
586+ };
587+ let stride = plane.stride;
588+ let c422 = cat == 2;
589+ let base = plane.origin + (mb_y * cmb_h) * stride + mb_x * 8;
590+ // Chroma QP per side: each macroblock's luma QP mapped with
591+ // the offset of ITS OWN slice's PPS (8.7.2).
592+ let cqp = |a: usize| -> i32 {
593+ let s = pic.slices[pic.mb_slice[a] as usize];
594+ if pic.mb_flags[a] & FLAG_PCM != 0 {
595+ chroma_qp(0, off_sel(&s), qp_bd)
596+ } else {
597+ chroma_qp(i32::from(pic.mb_qp[a]), off_sel(&s), qp_bd)
598+ }
599+ };
600+ let cq_q = cqp(addr);
601+ // A chroma edge inherits the boundary strength of the luma edge
602+ // it sits on: chroma sample k maps to luma sample 2k, so a
603+ // chroma 4-sample run spans two luma 4x4 block rows.
604+ for e in 0..2 {
605+ if e == 0 && !filter_left {
606+ continue;
607+ }
608+ let cq_p = if e == 0 { cqp(addr - 1) } else { cq_q };
609+ let qp_avg = (cq_p + cq_q + 1) >> 1;
610+ for k in 0..cmb_h {
611+ let bs = bs_v[e * 2][k >> if c422 { 2 } else { 1 }];
612+ if bs == 0 {
613+ continue;
614+ }
615+ let params = edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs, bd);
616+ if params.alpha == 0 || params.beta == 0 {
617+ continue;
618+ }
619+ filter_chroma_line(
620+ &mut plane.data,
621+ max,
622+ base + k * stride + e * 4,
623+ 1,
624+ &params,
625+ );
626+ }
627+ }
628+ // 4:2:2 chroma is as tall as luma: its horizontal 4x4 transform
629+ // edges sit at rows 0, 4, 8 and 12, each on the boundary
630+ // strength of the luma edge on the same row.
631+ for e in 0..if c422 { 4 } else { 2 } {
632+ if e == 0 && !filter_top {
633+ continue;
634+ }
635+ let cq_p = if e == 0 { cqp(addr - mb_w) } else { cq_q };
636+ let qp_avg = (cq_p + cq_q + 1) >> 1;
637+ let bs = bs_h[if c422 { e } else { e * 2 }];
638+ if bs[0] == bs[1] && bs[1] == bs[2] && bs[2] == bs[3] {
639+ if bs[0] == 0 {
640+ continue;
641+ }
642+ let params =
643+ edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs[0], bd);
644+ if params.alpha == 0 || params.beta == 0 {
645+ continue;
646+ }
647+ // The four rows around the edge are contiguous, so the
648+ // whole eight-sample edge filters in one pass.
649+ S::filter_chroma_h_edge8(
650+ &mut plane.data,
651+ max,
652+ base + e * 4 * stride,
653+ stride,
654+ &params,
655+ );
656+ continue;
657+ }
658+ for k in 0..8 {
659+ let bs = bs[k / 2];
660+ if bs == 0 {
661+ continue;
662+ }
663+ let params = edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs, bd);
664+ if params.alpha == 0 || params.beta == 0 {
665+ continue;
666+ }
667+ filter_chroma_line(
668+ &mut plane.data,
669+ max,
670+ base + e * 4 * stride + k,
671+ stride,
672+ &params,
673+ );
674+ }
675+ }
676+ }
677+ }
678+ }
679+}
680+
681+/// Copy the 8 samples across a macroblock edge (4 each side) along its 16
682+/// samples, so a lossless side can be put back after the filter ran.
683+fn snap_edge<S: Sample>(data: &[S], base: usize, stride: usize, vertical: bool) -> Vec<S> {
684+ let mut v = Vec::with_capacity(128);
685+ for i in 0..16 {
686+ for d in 0..8 {
687+ v.push(if vertical {
688+ data[base + i * stride + d - 4]
689+ } else {
690+ data[base + d * stride + i - 4 * stride]
691+ });
692+ }
693+ }
694+ v
695+}
696+
697+fn restore_edge<S: Sample>(
698+ data: &mut [S],
699+ snap: &[S],
700+ base: usize,
701+ stride: usize,
702+ vertical: bool,
703+ p: bool,
704+ q: bool,
705+) {
706+ for i in 0..16 {
707+ for d in 0..8 {
708+ if (d < 4 && !p) || (d >= 4 && !q) {
709+ continue;
710+ }
711+ let idx = if vertical {
712+ base + i * stride + d - 4
713+ } else {
714+ base + d * stride + i - 4 * stride
715+ };
716+ data[idx] = snap[i * 8 + d];
717+ }
718+ }
719+}
720+
721+/// True when every 4x4 block of this macroblock predicts from the same
722+/// pictures with the same motion, so no internal edge can reach the motion
723+/// clauses of 8.7.2.1.
724+fn uniform_motion<S: Sample>(pic: &Picture<S>, mb_x: usize, mb_y: usize) -> bool {
725+ let w4 = pic.mb_w * 4;
726+ let first = mb_y * 4 * w4 + mb_x * 4;
727+ let (mv, id) = (pic.mv[first], pic.ref_id[first]);
728+ (0..4).all(|by| {
729+ let row = first + by * w4;
730+ (0..4).all(|bx| pic.mv[row + bx] == mv && pic.ref_id[row + bx] == id)
731+ })
732+}
733+
734+/// What clause 8.7.2.1 needs from the 4x4 block on one side of a luma edge.
735+#[derive(Clone, Copy)]
736+pub(crate) struct BsSide {
737+ /// The containing macroblock is intra: strength 4 on macroblock edges and
738+ /// 3 on internal ones, regardless of motion or levels.
739+ pub(crate) intra: bool,
740+ /// The transform block containing this sample has non-zero levels.
741+ pub(crate) coded: bool,
742+ /// MvLX of the block, `[list][component]`.
743+ pub(crate) mv: [[i16; 2]; 2],
744+ /// RefIdxLX of the block as list identities (`Picture::id`), -1 unused.
745+ pub(crate) refs: [i32; 2],
746+}
747+
748+/// Boundary strength between the 4x4 blocks on the two sides of a luma edge
749+/// (clause 8.7.2.1). `mb_edge` selects the macroblock-edge strengths 4/3.
750+pub(crate) fn bs_between(p: &BsSide, q: &BsSide, mb_edge: bool, field: bool, vertical: bool) -> u8 {
751+ if p.intra || q.intra {
752+ // A horizontal edge of a field picture is never strength 4 (8.7.2.1).
753+ return if mb_edge && (vertical || !field) {
754+ 4
755+ } else {
756+ 3
757+ };
758+ }
759+ if p.coded || q.coded {
760+ return 2;
761+ }
762+ let (pr, qr) = (p.refs, q.refs);
763+ let pn = u8::from(pr[0] >= 0) + u8::from(pr[1] >= 0);
764+ let qn = u8::from(qr[0] >= 0) + u8::from(qr[1] >= 0);
765+ if pn != qn {
766+ return 1;
767+ }
768+ let (pm, qm) = (p.mv, q.mv);
769+ // The vertical limit is 4 quarter frame samples: 2 in field units.
770+ let ylim = if field { 2 } else { 4 };
771+ let differs = |a: [i16; 2], b: [i16; 2]| {
772+ (i32::from(a[0]) - i32::from(b[0])).abs() >= 4
773+ || (i32::from(a[1]) - i32::from(b[1])).abs() >= ylim
774+ };
775+ if pn == 1 {
776+ let pl = usize::from(pr[0] < 0);
777+ let ql = usize::from(qr[0] < 0);
778+ if pr[pl] != qr[ql] {
779+ return 1;
780+ }
781+ return u8::from(differs(pm[pl], qm[ql]));
782+ }
783+ if pn == 0 {
784+ return 0;
785+ }
786+ // Two motion vectors each: compare the two partitions by which pictures
787+ // they reference, not by list position (NOTE 1 of 8.7.2.1).
788+ let same_set = (pr[0] == qr[0] && pr[1] == qr[1]) || (pr[0] == qr[1] && pr[1] == qr[0]);
789+ if !same_set {
790+ return 1;
791+ }
792+ if pr[0] != pr[1] {
793+ return if pr[0] == qr[0] {
794+ u8::from(differs(pm[0], qm[0]) || differs(pm[1], qm[1]))
795+ } else {
796+ u8::from(differs(pm[0], qm[1]) || differs(pm[1], qm[0]))
797+ };
798+ }
799+ // Both lists name the same picture: either pairing agreeing is enough.
800+ let straight = !differs(pm[0], qm[0]) && !differs(pm[1], qm[1]);
801+ let crossed = !differs(pm[0], qm[1]) && !differs(pm[1], qm[0]);
802+ u8::from(!(straight || crossed))
803+}
804+
805+/// One side of a luma edge from picture state: the decoder path, both sides
806+/// final macroblocks. The encoder's inter trial uses this for the
807+/// already-coded neighbour and builds its candidate side itself.
808+pub(crate) fn bs_side<S: Sample>(pic: &Picture<S>, bx: usize, by: usize) -> BsSide {
809+ let w4 = pic.mb_w * 4;
810+ let mb = (by / 4) * pic.mb_w + bx / 4;
811+ BsSide {
812+ intra: pic.mb_flags[mb] & FLAG_INTER == 0,
813+ coded: bs_coded(pic, mb, bx, by),
814+ mv: pic.mv[by * w4 + bx],
815+ refs: pic.ref_id[by * w4 + bx],
816+ }
817+}
818+
819+/// "The transform block containing the sample has non-zero levels": for an
820+/// 8x8-transform macroblock that is the whole 8x8, whose four 4x4 slots
821+/// carry per-slot counts under CAVLC (7.3.5.3.1 splits it into four blocks).
822+fn bs_coded<S: Sample>(pic: &Picture<S>, mb: usize, bx: usize, by: usize) -> bool {
823+ let w4 = pic.mb_w * 4;
824+ if pic.mb_flags[mb] & FLAG_TRANS8X8 == 0 {
825+ return pic.nz_y[by * w4 + bx] != 0;
826+ }
827+ let (ox, oy) = (bx & !1, by & !1);
828+ pic.nz_y[oy * w4 + ox] != 0
829+ || pic.nz_y[oy * w4 + ox + 1] != 0
830+ || pic.nz_y[(oy + 1) * w4 + ox] != 0
831+ || pic.nz_y[(oy + 1) * w4 + ox + 1] != 0
832+}
833+
834+/// Boundary strength of one 4-sample luma edge segment (clause 8.7.2.1).
835+///
836+/// `edge` 0..4 selects the vertical (`vertical`) or horizontal edge inside the
837+/// macroblock, `seg` the 4-sample run along it. Edge 0 is the macroblock edge.
838+fn boundary_strength<S: Sample>(
839+ pic: &Picture<S>,
840+ mb_x: usize,
841+ mb_y: usize,
842+ edge: usize,
843+ seg: usize,
844+ vertical: bool,
845+) -> u8 {
846+ let (qbx, qby) = if vertical {
847+ (mb_x * 4 + edge, mb_y * 4 + seg)
848+ } else {
849+ (mb_x * 4 + seg, mb_y * 4 + edge)
850+ };
851+ let (pbx, pby) = if vertical {
852+ (qbx - 1, qby)
853+ } else {
854+ (qbx, qby - 1)
855+ };
856+ bs_between(
857+ &bs_side(pic, pbx, pby),
858+ &bs_side(pic, qbx, qby),
859+ edge == 0,
860+ pic.structure != FRAME,
861+ vertical,
862+ )
863+}
crates/ec-h264/src/deblock_mbaff.rsmodified+1 -1
@@ -19,7 +19,7 @@
1919 //! progressive filter stays the one the speed budget is spent on.
2020
2121 use crate::deblock::{chroma_qp, edge_params, filter_chroma_line, filter_luma_line};
22-use crate::decoder::{BsSide, bs_between, bs_side};
22+use crate::deblock::{BsSide, bs_between, bs_side};
2323 use crate::dpb::{PicPlane, Picture, SliceParams};
2424 use crate::entropy::{FLAG_DECODED, FLAG_PCM, FLAG_TRANS8X8};
2525 use crate::sample::Sample;
crates/ec-h264/src/decoder.rsmodified+1 -503
@@ -29,7 +29,7 @@ use ec_h264_syntax::{
2929 SliceType, Sps, WeightEntry, unescape_rbsp,
3030 };
3131
32-use crate::deblock::{chroma_qp, edge_params, filter_chroma_line, filter_luma_line};
32+use crate::deblock::{chroma_qp, deblock_picture};
3333 use crate::dpb::{
3434 BLK_DIRECT, BLK_INTRA, BLK_SKIP, BOTTOM, CODED_AFRM, CODED_FLD, Dpb, FRAME, Mark, NO_SLICE,
3535 PicInfo, PicPlane, Picture, RefList, SliceParams, TOP,
@@ -4012,508 +4012,6 @@ pub(crate) fn gather_nbr8_in<S: Sample>(
40124012 }
40134013 }
40144014
4015-/// Whole-picture deblocking (spec 8.7): macroblocks in raster order, all
4016-/// vertical edges then all horizontal edges per macroblock, then chroma.
4017-// `e` and `j` are edge and segment positions inside the macroblock, used to
4018-// index the plane as well as the strength arrays; iterating the arrays would
4019-// hide which geometry each one names.
4020-#[allow(clippy::needless_range_loop)]
4021-pub(crate) fn deblock_picture<S: Sample>(pic: &mut Picture<S>) {
4022- let mb_w = pic.mb_w;
4023- let bd = pic.bd;
4024- let max = (1i32 << bd) - 1;
4025- let qp_bd = 6 * (bd as i32 - 8);
4026- let cmb_h = pic.cmb_h;
4027- let cat = pic.cat;
4028- let lcomps = if cat == 3 { 3 } else { 1 };
4029- for mb_y in 0..pic.mb_h {
4030- for mb_x in 0..mb_w {
4031- let addr = mb_y * mb_w + mb_x;
4032- if pic.mb_flags[addr] & FLAG_DECODED == 0 {
4033- continue;
4034- }
4035- let sid = pic.mb_slice[addr];
4036- let sp = pic.slices[sid as usize];
4037- if sp.disable_deblock_idc == 1 {
4038- continue;
4039- }
4040- let decoded = |a: usize| pic.mb_flags[a] & FLAG_DECODED != 0;
4041- let cross_ok = |a: usize| sp.disable_deblock_idc != 2 || pic.mb_slice[a] == sid;
4042- let filter_left = mb_x > 0 && decoded(addr - 1) && cross_ok(addr - 1);
4043- let filter_top = mb_y > 0 && decoded(addr - mb_w) && cross_ok(addr - mb_w);
4044- // Deblocking QP: 0 for I_PCM macroblocks (8.7.2).
4045- let qp_of = |a: usize| -> i32 {
4046- if pic.mb_flags[a] & FLAG_PCM != 0 {
4047- 0
4048- } else {
4049- i32::from(pic.mb_qp[a])
4050- }
4051- };
4052- // 8.7: an 8x8-transform macroblock has no transform edge at luma
4053- // 4 or 12, so those internal edges are not filtered. Chroma is
4054- // unaffected for 4:2:0 (its internal edge sits on luma edge 8).
4055- let internal =
4056- |e: usize| e.is_multiple_of(2) || pic.mb_flags[addr] & FLAG_TRANS8X8 == 0;
4057-
4058- // Boundary strengths, per 4-sample segment of each edge (8.7.2.1).
4059- //
4060- // Both sides of an internal edge live in this macroblock, so the
4061- // whole 8.7.2.1 ladder is decided once for all 24 of them: an intra
4062- // macroblock gives 3 everywhere, and an inter macroblock with no
4063- // luma coefficients and one motion vector over the whole 16x16
4064- // gives 0 everywhere. Only the two macroblock edges then need the
4065- // per-segment derivation.
4066- let mut bs_v = [[0u8; 4]; 4];
4067- let mut bs_h = [[0u8; 4]; 4];
4068- let cur_intra = pic.mb_flags[addr] & FLAG_INTER == 0;
4069- let internal_uniform = if cur_intra {
4070- Some(3)
4071- } else if pic.mb_cbp[addr] == 0 && uniform_motion(pic, mb_x, mb_y) {
4072- Some(0)
4073- } else {
4074- None
4075- };
4076- for k in 0..4 {
4077- if filter_left {
4078- bs_v[0][k] = boundary_strength(pic, mb_x, mb_y, 0, k, true);
4079- }
4080- if filter_top {
4081- bs_h[0][k] = boundary_strength(pic, mb_x, mb_y, 0, k, false);
4082- }
4083- }
4084- for e in 1..4 {
4085- match internal_uniform {
4086- Some(bs) => {
4087- bs_v[e] = [bs; 4];
4088- bs_h[e] = [bs; 4];
4089- }
4090- None => {
4091- for k in 0..4 {
4092- bs_v[e][k] = boundary_strength(pic, mb_x, mb_y, e, k, true);
4093- bs_h[e][k] = boundary_strength(pic, mb_x, mb_y, e, k, false);
4094- }
4095- }
4096- }
4097- }
4098-
4099- // ---- luma (and Cb/Cr, coded like luma, for ChromaArrayType 3) ----
4100- for comp in 0..lcomps {
4101- let pl: &mut PicPlane<S> = match comp {
4102- 0 => &mut pic.y,
4103- 1 => &mut pic.cb,
4104- _ => &mut pic.cr,
4105- };
4106- let stride = pl.stride;
4107- let base = pl.at(mb_x * 16, mb_y * 16);
4108- let qpf = |a: usize| -> i32 {
4109- if comp == 0 {
4110- qp_of(a)
4111- } else {
4112- let s = pic.slices[pic.mb_slice[a] as usize];
4113- let off = if comp == 1 {
4114- s.cb_qp_offset
4115- } else {
4116- s.cr_qp_offset
4117- };
4118- let q = if pic.mb_flags[a] & FLAG_PCM != 0 {
4119- 0
4120- } else {
4121- i32::from(pic.mb_qp[a])
4122- };
4123- chroma_qp(q, off, qp_bd)
4124- }
4125- };
4126- let qp_q = qpf(addr);
4127- // Lossless macroblocks keep their samples (8.7.2.2).
4128- let lossless = |a: usize| {
4129- pic.bypass_enabled
4130- && pic.mb_flags[a] & FLAG_PCM == 0
4131- && i32::from(pic.mb_qp[a]) + qp_bd == 0
4132- };
4133- let (p_ll_v, q_ll) = (filter_left && lossless(addr - 1), lossless(addr));
4134- let snap_v = (p_ll_v || (filter_left && q_ll))
4135- .then(|| snap_edge(&pl.data, base, stride, true));
4136- // Vertical edges (filter across columns), left to right.
4137- for e in 0..4 {
4138- if e == 0 && !filter_left {
4139- continue;
4140- }
4141- if !internal(e) {
4142- continue;
4143- }
4144- let qp_p = if e == 0 { qpf(addr - 1) } else { qp_q };
4145- let qp_avg = (qp_p + qp_q + 1) >> 1;
4146- // Vertical edges stay scalar: a gather/scatter SIMD
4147- // variant measured slower than this loop (strided
4148- // per-lane loads dominate).
4149- for j in 0..4 {
4150- let bs = bs_v[e][j];
4151- if bs == 0 {
4152- continue;
4153- }
4154- let params = edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs, bd);
4155- if params.alpha == 0 || params.beta == 0 {
4156- continue;
4157- }
4158- for k in 0..4 {
4159- let row = j * 4 + k;
4160- filter_luma_line(
4161- &mut pl.data,
4162- max,
4163- base + row * stride + e * 4,
4164- 1,
4165- &params,
4166- );
4167- }
4168- }
4169- }
4170- if let Some(sv) = &snap_v {
4171- restore_edge(&mut pl.data, sv, base, stride, true, p_ll_v, q_ll);
4172- }
4173- let p_ll_h = filter_top && lossless(addr - mb_w);
4174- let snap_h = (p_ll_h || (filter_top && q_ll))
4175- .then(|| snap_edge(&pl.data, base, stride, false));
4176- // Horizontal edges, top to bottom.
4177- for e in 0..4 {
4178- if e == 0 && !filter_top {
4179- continue;
4180- }
4181- if !internal(e) {
4182- continue;
4183- }
4184- let qp_p = if e == 0 { qpf(addr - mb_w) } else { qp_q };
4185- let qp_avg = (qp_p + qp_q + 1) >> 1;
4186- let bs = bs_h[e];
4187- let uniform = bs[0] == bs[1] && bs[1] == bs[2] && bs[2] == bs[3];
4188- if uniform {
4189- if bs[0] == 0 {
4190- continue;
4191- }
4192- let params =
4193- edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs[0], bd);
4194- if params.alpha == 0 || params.beta == 0 {
4195- continue;
4196- }
4197- // Eight rows around the edge are contiguous, so the
4198- // whole 16-sample edge filters in one pass.
4199- S::filter_luma_h_edge16(
4200- &mut pl.data,
4201- max,
4202- base + e * 4 * stride,
4203- stride,
4204- &params,
4205- );
4206- continue;
4207- }
4208- for j in 0..4 {
4209- if bs[j] == 0 {
4210- continue;
4211- }
4212- let params =
4213- edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs[j], bd);
4214- if params.alpha == 0 || params.beta == 0 {
4215- continue;
4216- }
4217- for k in 0..4 {
4218- filter_luma_line(
4219- &mut pl.data,
4220- max,
4221- base + e * 4 * stride + j * 4 + k,
4222- stride,
4223- &params,
4224- );
4225- }
4226- }
4227- }
4228- if let Some(sh) = &snap_h {
4229- restore_edge(&mut pl.data, sh, base, stride, false, p_ll_h, q_ll);
4230- }
4231- }
4232-
4233- // ---- chroma (Cb, Cr) ----
4234- for comp in 0..if matches!(cat, 1 | 2) { 2 } else { 0 } {
4235- let (plane, off_sel): (&mut PicPlane<S>, fn(&SliceParams) -> i32) = if comp == 0 {
4236- (&mut pic.cb, |s| s.cb_qp_offset)
4237- } else {
4238- (&mut pic.cr, |s| s.cr_qp_offset)
4239- };
4240- let stride = plane.stride;
4241- let c422 = cat == 2;
4242- let base = plane.origin + (mb_y * cmb_h) * stride + mb_x * 8;
4243- // Chroma QP per side: each macroblock's luma QP mapped with
4244- // the offset of ITS OWN slice's PPS (8.7.2).
4245- let cqp = |a: usize| -> i32 {
4246- let s = pic.slices[pic.mb_slice[a] as usize];
4247- if pic.mb_flags[a] & FLAG_PCM != 0 {
4248- chroma_qp(0, off_sel(&s), qp_bd)
4249- } else {
4250- chroma_qp(i32::from(pic.mb_qp[a]), off_sel(&s), qp_bd)
4251- }
4252- };
4253- let cq_q = cqp(addr);
4254- // A chroma edge inherits the boundary strength of the luma edge
4255- // it sits on: chroma sample k maps to luma sample 2k, so a
4256- // chroma 4-sample run spans two luma 4x4 block rows.
4257- for e in 0..2 {
4258- if e == 0 && !filter_left {
4259- continue;
4260- }
4261- let cq_p = if e == 0 { cqp(addr - 1) } else { cq_q };
4262- let qp_avg = (cq_p + cq_q + 1) >> 1;
4263- for k in 0..cmb_h {
4264- let bs = bs_v[e * 2][k >> if c422 { 2 } else { 1 }];
4265- if bs == 0 {
4266- continue;
4267- }
4268- let params = edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs, bd);
4269- if params.alpha == 0 || params.beta == 0 {
4270- continue;
4271- }
4272- filter_chroma_line(
4273- &mut plane.data,
4274- max,
4275- base + k * stride + e * 4,
4276- 1,
4277- &params,
4278- );
4279- }
4280- }
4281- // 4:2:2 chroma is as tall as luma: its horizontal 4x4 transform
4282- // edges sit at rows 0, 4, 8 and 12, each on the boundary
4283- // strength of the luma edge on the same row.
4284- for e in 0..if c422 { 4 } else { 2 } {
4285- if e == 0 && !filter_top {
4286- continue;
4287- }
4288- let cq_p = if e == 0 { cqp(addr - mb_w) } else { cq_q };
4289- let qp_avg = (cq_p + cq_q + 1) >> 1;
4290- let bs = bs_h[if c422 { e } else { e * 2 }];
4291- if bs[0] == bs[1] && bs[1] == bs[2] && bs[2] == bs[3] {
4292- if bs[0] == 0 {
4293- continue;
4294- }
4295- let params =
4296- edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs[0], bd);
4297- if params.alpha == 0 || params.beta == 0 {
4298- continue;
4299- }
4300- // The four rows around the edge are contiguous, so the
4301- // whole eight-sample edge filters in one pass.
4302- S::filter_chroma_h_edge8(
4303- &mut plane.data,
4304- max,
4305- base + e * 4 * stride,
4306- stride,
4307- &params,
4308- );
4309- continue;
4310- }
4311- for k in 0..8 {
4312- let bs = bs[k / 2];
4313- if bs == 0 {
4314- continue;
4315- }
4316- let params = edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs, bd);
4317- if params.alpha == 0 || params.beta == 0 {
4318- continue;
4319- }
4320- filter_chroma_line(
4321- &mut plane.data,
4322- max,
4323- base + e * 4 * stride + k,
4324- stride,
4325- &params,
4326- );
4327- }
4328- }
4329- }
4330- }
4331- }
4332-}
4333-
4334-/// Copy the 8 samples across a macroblock edge (4 each side) along its 16
4335-/// samples, so a lossless side can be put back after the filter ran.
4336-fn snap_edge<S: Sample>(data: &[S], base: usize, stride: usize, vertical: bool) -> Vec<S> {
4337- let mut v = Vec::with_capacity(128);
4338- for i in 0..16 {
4339- for d in 0..8 {
4340- v.push(if vertical {
4341- data[base + i * stride + d - 4]
4342- } else {
4343- data[base + d * stride + i - 4 * stride]
4344- });
4345- }
4346- }
4347- v
4348-}
4349-
4350-fn restore_edge<S: Sample>(
4351- data: &mut [S],
4352- snap: &[S],
4353- base: usize,
4354- stride: usize,
4355- vertical: bool,
4356- p: bool,
4357- q: bool,
4358-) {
4359- for i in 0..16 {
4360- for d in 0..8 {
4361- if (d < 4 && !p) || (d >= 4 && !q) {
4362- continue;
4363- }
4364- let idx = if vertical {
4365- base + i * stride + d - 4
4366- } else {
4367- base + d * stride + i - 4 * stride
4368- };
4369- data[idx] = snap[i * 8 + d];
4370- }
4371- }
4372-}
4373-
4374-/// True when every 4x4 block of this macroblock predicts from the same
4375-/// pictures with the same motion, so no internal edge can reach the motion
4376-/// clauses of 8.7.2.1.
4377-fn uniform_motion<S: Sample>(pic: &Picture<S>, mb_x: usize, mb_y: usize) -> bool {
4378- let w4 = pic.mb_w * 4;
4379- let first = mb_y * 4 * w4 + mb_x * 4;
4380- let (mv, id) = (pic.mv[first], pic.ref_id[first]);
4381- (0..4).all(|by| {
4382- let row = first + by * w4;
4383- (0..4).all(|bx| pic.mv[row + bx] == mv && pic.ref_id[row + bx] == id)
4384- })
4385-}
4386-
4387-/// What clause 8.7.2.1 needs from the 4x4 block on one side of a luma edge.
4388-#[derive(Clone, Copy)]
4389-pub(crate) struct BsSide {
4390- /// The containing macroblock is intra: strength 4 on macroblock edges and
4391- /// 3 on internal ones, regardless of motion or levels.
4392- pub(crate) intra: bool,
4393- /// The transform block containing this sample has non-zero levels.
4394- pub(crate) coded: bool,
4395- /// MvLX of the block, `[list][component]`.
4396- pub(crate) mv: [[i16; 2]; 2],
4397- /// RefIdxLX of the block as list identities (`Picture::id`), -1 unused.
4398- pub(crate) refs: [i32; 2],
4399-}
4400-
4401-/// Boundary strength between the 4x4 blocks on the two sides of a luma edge
4402-/// (clause 8.7.2.1). `mb_edge` selects the macroblock-edge strengths 4/3.
4403-pub(crate) fn bs_between(p: &BsSide, q: &BsSide, mb_edge: bool, field: bool, vertical: bool) -> u8 {
4404- if p.intra || q.intra {
4405- // A horizontal edge of a field picture is never strength 4 (8.7.2.1).
4406- return if mb_edge && (vertical || !field) {
4407- 4
4408- } else {
4409- 3
4410- };
4411- }
4412- if p.coded || q.coded {
4413- return 2;
4414- }
4415- let (pr, qr) = (p.refs, q.refs);
4416- let pn = u8::from(pr[0] >= 0) + u8::from(pr[1] >= 0);
4417- let qn = u8::from(qr[0] >= 0) + u8::from(qr[1] >= 0);
4418- if pn != qn {
4419- return 1;
4420- }
4421- let (pm, qm) = (p.mv, q.mv);
4422- // The vertical limit is 4 quarter frame samples: 2 in field units.
4423- let ylim = if field { 2 } else { 4 };
4424- let differs = |a: [i16; 2], b: [i16; 2]| {
4425- (i32::from(a[0]) - i32::from(b[0])).abs() >= 4
4426- || (i32::from(a[1]) - i32::from(b[1])).abs() >= ylim
4427- };
4428- if pn == 1 {
4429- let pl = usize::from(pr[0] < 0);
4430- let ql = usize::from(qr[0] < 0);
4431- if pr[pl] != qr[ql] {
4432- return 1;
4433- }
4434- return u8::from(differs(pm[pl], qm[ql]));
4435- }
4436- if pn == 0 {
4437- return 0;
4438- }
4439- // Two motion vectors each: compare the two partitions by which pictures
4440- // they reference, not by list position (NOTE 1 of 8.7.2.1).
4441- let same_set = (pr[0] == qr[0] && pr[1] == qr[1]) || (pr[0] == qr[1] && pr[1] == qr[0]);
4442- if !same_set {
4443- return 1;
4444- }
4445- if pr[0] != pr[1] {
4446- return if pr[0] == qr[0] {
4447- u8::from(differs(pm[0], qm[0]) || differs(pm[1], qm[1]))
4448- } else {
4449- u8::from(differs(pm[0], qm[1]) || differs(pm[1], qm[0]))
4450- };
4451- }
4452- // Both lists name the same picture: either pairing agreeing is enough.
4453- let straight = !differs(pm[0], qm[0]) && !differs(pm[1], qm[1]);
4454- let crossed = !differs(pm[0], qm[1]) && !differs(pm[1], qm[0]);
4455- u8::from(!(straight || crossed))
4456-}
4457-
4458-/// One side of a luma edge from picture state: the decoder path, both sides
4459-/// final macroblocks. The encoder's inter trial uses this for the
4460-/// already-coded neighbour and builds its candidate side itself.
4461-pub(crate) fn bs_side<S: Sample>(pic: &Picture<S>, bx: usize, by: usize) -> BsSide {
4462- let w4 = pic.mb_w * 4;
4463- let mb = (by / 4) * pic.mb_w + bx / 4;
4464- BsSide {
4465- intra: pic.mb_flags[mb] & FLAG_INTER == 0,
4466- coded: bs_coded(pic, mb, bx, by),
4467- mv: pic.mv[by * w4 + bx],
4468- refs: pic.ref_id[by * w4 + bx],
4469- }
4470-}
4471-
4472-/// "The transform block containing the sample has non-zero levels": for an
4473-/// 8x8-transform macroblock that is the whole 8x8, whose four 4x4 slots
4474-/// carry per-slot counts under CAVLC (7.3.5.3.1 splits it into four blocks).
4475-fn bs_coded<S: Sample>(pic: &Picture<S>, mb: usize, bx: usize, by: usize) -> bool {
4476- let w4 = pic.mb_w * 4;
4477- if pic.mb_flags[mb] & FLAG_TRANS8X8 == 0 {
4478- return pic.nz_y[by * w4 + bx] != 0;
4479- }
4480- let (ox, oy) = (bx & !1, by & !1);
4481- pic.nz_y[oy * w4 + ox] != 0
4482- || pic.nz_y[oy * w4 + ox + 1] != 0
4483- || pic.nz_y[(oy + 1) * w4 + ox] != 0
4484- || pic.nz_y[(oy + 1) * w4 + ox + 1] != 0
4485-}
4486-
4487-/// Boundary strength of one 4-sample luma edge segment (clause 8.7.2.1).
4488-///
4489-/// `edge` 0..4 selects the vertical (`vertical`) or horizontal edge inside the
4490-/// macroblock, `seg` the 4-sample run along it. Edge 0 is the macroblock edge.
4491-fn boundary_strength<S: Sample>(
4492- pic: &Picture<S>,
4493- mb_x: usize,
4494- mb_y: usize,
4495- edge: usize,
4496- seg: usize,
4497- vertical: bool,
4498-) -> u8 {
4499- let (qbx, qby) = if vertical {
4500- (mb_x * 4 + edge, mb_y * 4 + seg)
4501- } else {
4502- (mb_x * 4 + seg, mb_y * 4 + edge)
4503- };
4504- let (pbx, pby) = if vertical {
4505- (qbx - 1, qby)
4506- } else {
4507- (qbx, qby - 1)
4508- };
4509- bs_between(
4510- &bs_side(pic, pbx, pby),
4511- &bs_side(pic, qbx, qby),
4512- edge == 0,
4513- pic.structure != FRAME,
4514- vertical,
4515- )
4516-}
45174015
45184016 #[cfg(test)]
45194017 mod tests {
crates/ec-h264/src/enc/mod.rsmodified+1 -1
@@ -34,7 +34,7 @@ use ec_core::error::{Error, Result};
3434 use ec_h264_syntax::nal::{NalUnitType, escape_rbsp};
3535 use ec_h264_syntax::{SliceType, Sps};
3636
37-use crate::decoder::deblock_picture;
37+use crate::deblock::deblock_picture;
3838 use crate::dpb::{Picture, SliceParams as DeblockParams};
3939 use crate::entropy::{FLAG_INTER, FLAG_TRANS8X8};
4040 use crate::transform::{LevelScale4x4, LevelScale8x8};