M13.4: hevcenc deblocking filter (in-loop, bit-exact with ffmpeg), scratch buffers, POC-wrap / high-QP tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Brandon Thomas
2026-10-03 05:15:09 -04:00
co-authored by Claude Opus 5.5
parent 4130172bbe
commit c9d5e7335e
215 changed files with 841 additions and 37 deletions
+149
View File
@@ -0,0 +1,149 @@
//! Deblocking filter (8.7.2) over a whole reconstructed picture, mirrored from the filmcraft-hevc
//! decoder: all vertical edges on the 8x8 grid first, then all horizontal edges. One slice, one QP,
//! uni-directional prediction from a single reference picture, offsets 0.
use crate::frame::{Blk, E_EDGE_H, E_EDGE_V, F_CBF, F_INTRA};
use crate::tables::{BETA_TABLE, TC_TABLE, qpc_420};
#[inline]
fn tc_beta(q: i32, bs: i32, bd: u32) -> (i32, i32) {
let qb = q.clamp(0, 51);
let qt = (q + 2 * (bs - 1)).clamp(0, 53);
((TC_TABLE[qt as usize] as i32) << (bd - 8), (BETA_TABLE[qb as usize] as i32) << (bd - 8))
}
/// Boundary strength (8.7.2.4) between 4x4 blocks p and q on a transform / prediction edge.
fn boundary_strength(p: &Blk, q: &Blk) -> i32 {
if (p.flags | q.flags) & F_INTRA != 0 {
return 2;
}
if (p.flags | q.flags) & F_CBF != 0 {
return 1;
}
let far = (p.mv[0] as i32 - q.mv[0] as i32).abs() >= 4 || (p.mv[1] as i32 - q.mv[1] as i32).abs() >= 4;
far as i32
}
/// Deblock the planes in place. `qp` is the slice QpY (without QpBdOffset).
pub fn deblock(planes: &mut [Vec<u16>; 3], blk: &[Blk], w: usize, h: usize, qp: i32, bd: u32) {
let (w4, h4) = (w / 4, h / 4);
let cw = w / 2;
// chroma tc (bS 2 only; QpC from the averaged luma QP, no offsets)
let qt = (qpc_420(qp) + 2).clamp(0, 53);
let tc_c = (TC_TABLE[qt as usize] as i32) << (bd - 8);
// vertical edges
for by in 0..h4 {
for bx in (2..w4).step_by(2) {
let qi = by * w4 + bx;
if blk[qi].flags & E_EDGE_V == 0 {
continue;
}
let bs = boundary_strength(&blk[qi - 1], &blk[qi]);
if bs == 0 {
continue;
}
let (x, y) = (bx * 4, by * 4);
let (tc, beta) = tc_beta(qp, bs, bd);
filter_luma(&mut planes[0], y * w + x, 1, w, tc, beta, bd);
// chroma: 8x8 chroma grid (16 luma samples), one 4-sample chroma segment per 8 luma rows
if bs == 2 && bx % 4 == 0 && by % 2 == 0 {
for c in 1..3 {
filter_chroma(&mut planes[c], (y / 2) * cw + x / 2, 1, cw, tc_c, bd);
}
}
}
}
// horizontal edges
for by in (2..h4).step_by(2) {
for bx in 0..w4 {
let qi = by * w4 + bx;
if blk[qi].flags & E_EDGE_H == 0 {
continue;
}
let bs = boundary_strength(&blk[qi - w4], &blk[qi]);
if bs == 0 {
continue;
}
let (x, y) = (bx * 4, by * 4);
let (tc, beta) = tc_beta(qp, bs, bd);
filter_luma(&mut planes[0], y * w + x, w, 1, tc, beta, bd);
if bs == 2 && by % 4 == 0 && bx % 2 == 0 {
for c in 1..3 {
filter_chroma(&mut planes[c], (y / 2) * cw + x / 2, cw, 1, tc_c, bd);
}
}
}
}
}
/// Filter one 4-sample luma edge segment. `o` = offset of q0 of the first line, `step` = distance
/// between samples across the edge, `along` = distance between lines.
#[allow(clippy::too_many_arguments)]
fn filter_luma(s: &mut [u16], o: usize, step: usize, along: usize, tc: i32, beta: i32, bd: u32) {
let px = |s: &[u16], k: usize, i: usize| s[o + k * along - (i + 1) * step] as i32;
let qx = |s: &[u16], k: usize, i: usize| s[o + k * along + i * step] as i32;
let dp0 = (px(s, 0, 2) - 2 * px(s, 0, 1) + px(s, 0, 0)).abs();
let dp3 = (px(s, 3, 2) - 2 * px(s, 3, 1) + px(s, 3, 0)).abs();
let dq0 = (qx(s, 0, 2) - 2 * qx(s, 0, 1) + qx(s, 0, 0)).abs();
let dq3 = (qx(s, 3, 2) - 2 * qx(s, 3, 1) + qx(s, 3, 0)).abs();
let (dpq0, dpq3) = (dp0 + dq0, dp3 + dq3);
let (dp, dq) = (dp0 + dp3, dq0 + dq3);
let d = dpq0 + dpq3;
if d >= beta {
return;
}
let dsam = |s: &[u16], k: usize, dpq: i32| -> bool {
dpq < (beta >> 2)
&& (px(s, k, 3) - px(s, k, 0)).abs() + (qx(s, k, 0) - qx(s, k, 3)).abs() < (beta >> 3)
&& (px(s, k, 0) - qx(s, k, 0)).abs() < ((5 * tc + 1) >> 1)
};
let strong = dsam(s, 0, 2 * dpq0) && dsam(s, 3, 2 * dpq3);
let dep = dp < ((beta + (beta >> 1)) >> 3);
let deq = dq < ((beta + (beta >> 1)) >> 3);
let max = (1i32 << bd) - 1;
for k in 0..4 {
let base = o + k * along;
let p = |i: usize| s[base - (i + 1) * step] as i32;
let q = |i: usize| s[base + i * step] as i32;
let (p0, p1, p2, p3) = (p(0), p(1), p(2), p(3));
let (q0, q1, q2, q3) = (q(0), q(1), q(2), q(3));
if strong {
let t2 = 2 * tc;
s[base - step] = ((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3).clamp(p0 - t2, p0 + t2) as u16;
s[base - 2 * step] = ((p2 + p1 + p0 + q0 + 2) >> 2).clamp(p1 - t2, p1 + t2) as u16;
s[base - 3 * step] = ((2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) >> 3).clamp(p2 - t2, p2 + t2) as u16;
s[base] = ((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3).clamp(q0 - t2, q0 + t2) as u16;
s[base + step] = ((p0 + q0 + q1 + q2 + 2) >> 2).clamp(q1 - t2, q1 + t2) as u16;
s[base + 2 * step] = ((p0 + q0 + q1 + 3 * q2 + 2 * q3 + 4) >> 3).clamp(q2 - t2, q2 + t2) as u16;
} else {
let mut delta = (9 * (q0 - p0) - 3 * (q1 - p1) + 8) >> 4;
if delta.abs() >= tc * 10 {
continue;
}
delta = delta.clamp(-tc, tc);
s[base - step] = (p0 + delta).clamp(0, max) as u16;
s[base] = (q0 - delta).clamp(0, max) as u16;
if dep {
let dp = ((((p2 + p0 + 1) >> 1) - p1 + delta) >> 1).clamp(-(tc >> 1), tc >> 1);
s[base - 2 * step] = (p1 + dp).clamp(0, max) as u16;
}
if deq {
let dq = ((((q2 + q0 + 1) >> 1) - q1 - delta) >> 1).clamp(-(tc >> 1), tc >> 1);
s[base + step] = (q1 + dq).clamp(0, max) as u16;
}
}
}
}
/// Filter one 4-sample chroma edge segment.
fn filter_chroma(s: &mut [u16], o: usize, step: usize, along: usize, tc: i32, bd: u32) {
let max = (1i32 << bd) - 1;
for k in 0..4 {
let base = o + k * along;
let (p0, p1) = (s[base - step] as i32, s[base - 2 * step] as i32);
let (q0, q1) = (s[base] as i32, s[base + step] as i32);
let delta = ((((q0 - p0) << 2) + p1 - q1 + 4) >> 3).clamp(-tc, tc);
s[base - step] = (p0 + delta).clamp(0, max) as u16;
s[base] = (q0 - delta).clamp(0, max) as u16;
}
}
+47 -10
View File
@@ -12,6 +12,11 @@ use crate::transform;
pub const F_INTRA: u8 = 1;
pub const F_SKIP: u8 = 2;
/// The luma transform block covering this 4x4 block has non-zero coefficients.
pub const F_CBF: u8 = 4;
/// The left / top edge of this 4x4 block is a transform (and prediction) block edge.
pub const E_EDGE_V: u8 = 8;
pub const E_EDGE_H: u8 = 16;
/// Per 4x4 luma block state of the current picture.
#[derive(Clone, Copy, Default)]
@@ -79,6 +84,7 @@ pub struct FrameCoder<'a> {
hctb: usize,
pub kind: SliceKind,
/// QP' (with QpBdOffset) for luma and chroma.
slice_qp: i32,
qp_y: i32,
qp_c: i32,
lambda: f64,
@@ -91,6 +97,7 @@ pub struct FrameCoder<'a> {
tbs: Vec<Tb>,
coefs: Vec<i32>,
pred: Vec<i16>,
scratch: [Vec<i32>; 2],
}
#[inline]
@@ -134,6 +141,7 @@ impl<'a> FrameCoder<'a> {
wctb: w.div_ceil(ctb),
hctb: h.div_ceil(ctb),
kind,
slice_qp,
qp_y: slice_qp + off,
qp_c,
lambda,
@@ -146,6 +154,7 @@ impl<'a> FrameCoder<'a> {
tbs: Vec::with_capacity(16),
coefs: Vec::with_capacity(64 * 64 * 2),
pred: vec![0; 64 * 64],
scratch: [vec![0; 32 * 32], vec![0; 32 * 32]],
}
}
@@ -182,6 +191,28 @@ impl<'a> FrameCoder<'a> {
}
}
/// Mark the CU's left / top edges and, for CUs larger than the maximum transform size, the
/// inner transform edges (deblocking).
fn mark_edges(&mut self, x: usize, y: usize, log2: u32) {
let n = 1usize << log2;
let t = 1usize << log2.min(LOG2_MAX_TB);
for ty in (y..y + n).step_by(t) {
for tx in (x..x + n).step_by(t) {
for by in ty / 4..(ty + t) / 4 {
self.blk[by * self.w4 + tx / 4].flags |= E_EDGE_V;
}
for b in &mut self.blk[(ty / 4) * self.w4 + tx / 4..(ty / 4) * self.w4 + (tx + t) / 4] {
b.flags |= E_EDGE_H;
}
}
}
}
/// In-loop deblocking of the finished picture.
pub fn deblock(&mut self) {
crate::deblock::deblock(&mut self.rec, &self.blk, self.w, self.h, self.slice_qp, self.bd);
}
fn snap(&self, x: usize, y: usize, n: usize) -> Snap {
let mut planes: [Vec<u16>; 3] = Default::default();
for (c, p) in planes.iter_mut().enumerate() {
@@ -387,6 +418,7 @@ impl<'a> FrameCoder<'a> {
b.flags = F_INTRA;
b.ipm = modes[0];
});
self.mark_edges(x, y, log2);
if nxn {
let h = n / 2;
for k in 1..4 {
@@ -430,6 +462,7 @@ impl<'a> FrameCoder<'a> {
b.ipm = 1;
b.mv = mv;
});
self.mark_edges(x, y, log2);
self.predict_inter(x, y, n, mv);
let mut eff = mode;
let mut root = false;
@@ -439,7 +472,7 @@ impl<'a> FrameCoder<'a> {
if !root && let CuMode::Merge { idx } = mode {
// rqt_root_cbf would be inferred: code a skipped CU instead
eff = CuMode::Skip { idx };
self.fill(x, y, n, |b| b.flags = F_SKIP);
self.fill(x, y, n, |b| b.flags |= F_SKIP);
}
}
match eff {
@@ -515,7 +548,8 @@ impl<'a> FrameCoder<'a> {
let bd = self.bd;
predict_from_refs(&refs, c, m, n, bd, &mut self.rec[c][y * stride + x..], stride);
}
let mut r = [0i32; 32 * 32];
let mut scr = std::mem::take(&mut self.scratch);
let [r, coef] = &mut scr;
for yy in 0..n {
let o = (y + yy) * stride + x;
for xx in 0..n {
@@ -524,15 +558,16 @@ impl<'a> FrameCoder<'a> {
}
let intra = intra_mode.is_some();
let dst = intra && c == 0 && n == 4;
let mut coef = [0i32; 32 * 32];
transform::forward(&r[..n * n], &mut coef[..n * n], n, dst, self.bd);
let qp = if c == 0 { self.qp_y } else { self.qp_c };
let mut lv = [0i32; 32 * 32];
let nz = transform::quantize(&coef[..n * n], &mut lv[..n * n], n, qp, intra, self.bd) > 0;
let off = self.coefs.len();
self.coefs.resize(off + n * n, 0);
let lv = &mut self.coefs[off..off + n * n];
let nz = transform::quantize(&coef[..n * n], lv, n, qp, intra, self.bd) > 0;
if nz {
let mut d = [0i32; 32 * 32];
transform::dequantize(&lv[..n * n], &mut d[..n * n], n, qp, self.bd);
transform::inverse(&mut d[..n * n], n, dst, self.bd);
let d = &mut r[..n * n];
transform::dequantize(lv, d, n, qp, self.bd);
transform::inverse(d, n, dst, self.bd);
let max = (1i32 << self.bd) - 1;
for yy in 0..n {
let o = (y + yy) * stride + x;
@@ -542,12 +577,14 @@ impl<'a> FrameCoder<'a> {
}
}
}
self.scratch = scr;
if nz && c == 0 {
self.fill(x, y, n, |b| b.flags |= F_CBF);
}
let scan = match intra_mode {
Some(m) if log2 == 2 || (log2 == 3 && c == 0) => scan_idx_for_mode(m),
_ => 0,
};
let off = self.coefs.len();
self.coefs.extend_from_slice(&lv[..n * n]);
self.tbs.push(Tb { c: c as u8, lx: lpos.0 as u32, ly: lpos.1 as u32, log2, nz, off, scan });
}
+22 -23
View File
@@ -3,9 +3,6 @@
use crate::tables::{CHROMA_FILTER, LUMA_FILTER};
/// Max prediction block size + filter margin.
const WIN: usize = 64 + 7;
/// One plane of a reference picture.
#[derive(Clone, Copy)]
pub struct PlaneRef<'a> {
@@ -44,22 +41,23 @@ pub fn mc_luma(f: PlaneRef, bd: u32, x: i32, y: i32, mv: [i16; 2], w: usize, h:
let yi = y + (mv[1] as i32 >> 2);
let shift1 = bd.min(12) - 8;
let shift3 = 14 - bd;
let mut win = [0i16; WIN * WIN];
let ws = w + 7;
let mut win = vec![0i16; ws * (h + 7)];
if fx == 0 && fy == 0 {
f.window(xi, yi, w, h, &mut win, WIN);
f.window(xi, yi, w, h, &mut win, ws);
for r in 0..h {
for c in 0..w {
out[r * w + c] = win[r * WIN + c] << shift3;
out[r * w + c] = win[r * ws + c] << shift3;
}
}
return;
}
f.window(xi - 3, yi - 3, w + 7, h + 7, &mut win, WIN);
f.window(xi - 3, yi - 3, w + 7, h + 7, &mut win, ws);
let hf = &LUMA_FILTER[fx];
let vf = &LUMA_FILTER[fy];
if fy == 0 {
for r in 0..h {
let row = &win[(r + 3) * WIN..];
let row = &win[(r + 3) * ws..];
for c in 0..w {
let s: i32 = (0..8).map(|k| hf[k] as i32 * row[c + k] as i32).sum();
out[r * w + c] = (s >> shift1) as i16;
@@ -68,22 +66,22 @@ pub fn mc_luma(f: PlaneRef, bd: u32, x: i32, y: i32, mv: [i16; 2], w: usize, h:
} else if fx == 0 {
for r in 0..h {
for c in 0..w {
let s: i32 = (0..8).map(|k| vf[k] as i32 * win[(r + k) * WIN + c + 3] as i32).sum();
let s: i32 = (0..8).map(|k| vf[k] as i32 * win[(r + k) * ws + c + 3] as i32).sum();
out[r * w + c] = (s >> shift1) as i16;
}
}
} else {
let mut tmp = [0i16; WIN * WIN];
let mut tmp = vec![0i16; ws * (h + 7)];
for r in 0..h + 7 {
let row = &win[r * WIN..];
let row = &win[r * ws..];
for c in 0..w {
let s: i32 = (0..8).map(|k| hf[k] as i32 * row[c + k] as i32).sum();
tmp[r * WIN + c] = (s >> shift1) as i16;
tmp[r * ws + c] = (s >> shift1) as i16;
}
}
for r in 0..h {
for c in 0..w {
let s: i32 = (0..8).map(|k| vf[k] as i32 * tmp[(r + k) * WIN + c] as i32).sum();
let s: i32 = (0..8).map(|k| vf[k] as i32 * tmp[(r + k) * ws + c] as i32).sum();
out[r * w + c] = (s >> 6) as i16;
}
}
@@ -97,22 +95,23 @@ pub fn mc_chroma(f: PlaneRef, bd: u32, x: i32, y: i32, mv: [i16; 2], w: usize, h
let yi = y + (mv[1] as i32 >> 3);
let shift1 = bd.min(12) - 8;
let shift3 = 14 - bd;
let mut win = [0i16; WIN * WIN];
let ws = w + 7;
let mut win = vec![0i16; ws * (h + 7)];
if fx == 0 && fy == 0 {
f.window(xi, yi, w, h, &mut win, WIN);
f.window(xi, yi, w, h, &mut win, ws);
for r in 0..h {
for c in 0..w {
out[r * w + c] = win[r * WIN + c] << shift3;
out[r * w + c] = win[r * ws + c] << shift3;
}
}
return;
}
f.window(xi - 1, yi - 1, w + 3, h + 3, &mut win, WIN);
f.window(xi - 1, yi - 1, w + 3, h + 3, &mut win, ws);
let hf = &CHROMA_FILTER[fx];
let vf = &CHROMA_FILTER[fy];
if fy == 0 {
for r in 0..h {
let row = &win[(r + 1) * WIN..];
let row = &win[(r + 1) * ws..];
for c in 0..w {
let s: i32 = (0..4).map(|k| hf[k] as i32 * row[c + k] as i32).sum();
out[r * w + c] = (s >> shift1) as i16;
@@ -121,22 +120,22 @@ pub fn mc_chroma(f: PlaneRef, bd: u32, x: i32, y: i32, mv: [i16; 2], w: usize, h
} else if fx == 0 {
for r in 0..h {
for c in 0..w {
let s: i32 = (0..4).map(|k| vf[k] as i32 * win[(r + k) * WIN + c + 1] as i32).sum();
let s: i32 = (0..4).map(|k| vf[k] as i32 * win[(r + k) * ws + c + 1] as i32).sum();
out[r * w + c] = (s >> shift1) as i16;
}
}
} else {
let mut tmp = [0i16; WIN * WIN];
let mut tmp = vec![0i16; ws * (h + 7)];
for r in 0..h + 3 {
let row = &win[r * WIN..];
let row = &win[r * ws..];
for c in 0..w {
let s: i32 = (0..4).map(|k| hf[k] as i32 * row[c + k] as i32).sum();
tmp[r * WIN + c] = (s >> shift1) as i16;
tmp[r * ws + c] = (s >> shift1) as i16;
}
}
for r in 0..h {
for c in 0..w {
let s: i32 = (0..4).map(|k| vf[k] as i32 * tmp[(r + k) * WIN + c] as i32).sum();
let s: i32 = (0..4).map(|k| vf[k] as i32 * tmp[(r + k) * ws + c] as i32).sum();
out[r * w + c] = (s >> 6) as i16;
}
}
+3 -1
View File
@@ -3,7 +3,7 @@
//! Main and Main 10 profiles, 4:2:0, one slice per picture: IDR pictures and single-reference P
//! pictures (no reordering), 64x64 CTUs with rate-distortion coding-unit decisions (8x8 .. 64x64),
//! intra (35 modes, NxN at 8x8), merge / skip and AMVP inter prediction with quarter-sample motion,
//! DCT / DST transforms and CABAC. In-loop filters (deblocking, SAO) are disabled.
//! DCT / DST transforms and CABAC. The deblocking filter is on; SAO is not used.
//!
//! ```
//! use effectcraft_hevcenc::{Encoder, EncoderConfig, Frame};
@@ -19,6 +19,7 @@
mod bitstream;
mod cabac;
mod deblock;
mod frame;
mod inter;
mod intra;
@@ -333,6 +334,7 @@ impl Encoder {
let ctx = cabac::init_contexts(qp, if idr { 0 } else { 1 });
let mut enc = cabac::CabacEncoder::new(bw, ctx);
fc.encode(&mut enc);
fc.deblock();
let rbsp = enc.finish().into_bytes();
let nal = nal_unit(if idr { NAL_IDR_N_LP } else { NAL_TRAIL_R }, &rbsp);
let mut data = Vec::with_capacity(nal.len() + 4);
+3 -1
View File
@@ -208,7 +208,9 @@ pub fn pps() -> Vec<u8> {
w.put(0, 1); // pps_loop_filter_across_slices_enabled_flag
w.put(1, 1); // deblocking_filter_control_present_flag
w.put(0, 1); // deblocking_filter_override_enabled_flag
w.put(1, 1); // pps_deblocking_filter_disabled_flag
w.put(0, 1); // pps_deblocking_filter_disabled_flag
w.se(0); // pps_beta_offset_div2
w.se(0); // pps_tc_offset_div2
w.put(0, 1); // pps_scaling_list_data_present_flag
w.put(0, 1); // lists_modification_present_flag
w.ue(0); // log2_parallel_merge_level_minus2
+4
View File
@@ -230,3 +230,7 @@ pub static TRANS_MATRIX: [[i8; 32]; 32] = [
[9, -25, 43, -57, 70, -80, 87, -90, 90, -87, 80, -70, 57, -43, 25, -9, -9, 25, -43, 57, -70, 80, -87, 90, -90, 87, -80, 70, -57, 43, -25, 9],
[4, -13, 22, -31, 38, -46, 54, -61, 67, -73, 78, -82, 85, -88, 90, -90, 90, -90, 88, -85, 82, -78, 73, -67, 61, -54, 46, -38, 31, -22, 13, -4],
];
/// Table 8-12: beta' for Q = 0..51.
pub static BETA_TABLE: [u8; 52] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64];
/// Table 8-12: tC' for Q = 0..53.
pub static TC_TABLE: [u8; 54] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 9, 10, 11, 13, 14, 16, 18, 20, 22, 24];
+2 -2
View File
@@ -95,7 +95,7 @@ pub fn forward(r: &[i32], c: &mut [i32], n: usize, dst: bool, bit_depth: u32) {
let shift1 = log2 + bit_depth - 9;
let shift2 = log2 + 6;
let (r1, r2) = (1i32 << (shift1 - 1), 1i32 << (shift2 - 1));
let mut tmp = [0i32; 32 * 32];
let mut tmp = vec![0i32; n * n];
let mut out = [0i32; 32];
// rows (horizontal frequencies)
for y in 0..n {
@@ -127,7 +127,7 @@ pub fn forward(r: &[i32], c: &mut [i32], n: usize, dst: bool, bit_depth: u32) {
/// Inverse 2-D transform (8.6.4.2) of scaled coefficients `d` in place, producing residuals.
pub fn inverse(d: &mut [i32], n: usize, dst: bool, bit_depth: u32) {
let bd_shift = 20 - bit_depth;
let mut tmp = [0i32; 32 * 32];
let mut tmp = vec![0i32; n * n];
let mut col = [0i32; 32];
let mut out = [0i32; 32];
// max non-zero row, to skip empty work