forge
Explore
Log in

Don't you dare go Hollow

wip(hbd): scale CDEF strength/damping/direction-search by bit-depth coeff_shift

Browse files at this commit

50d59cc49dd769c882fcb68142e24aa6965150c1Ahmet Kaan GümüşParent: 9c66378
libaom cdef_block.c: coeff_shift = bit_depth - 8 scales pri/sec strength
(<< coeff_shift) and damping (luma + coeff_shift, chroma one less), and
cdef_find_dir normalises each sample (>> coeff_shift) before the -128
centring (cdef_block.c:74).

Coded by lane-hbd r6, `cargo check -p ec-av1 --lib` clean, committed by
the orchestrator at the agent's turn cap. NOT yet run against the gate.
crates/ec-av1/src/decode.rsmodified+19 -11
@@ -5106,12 +5106,14 @@ fn cdef_constrain(diff: i32, threshold: i32, damping: i32) -> i32 {
51065106 /// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant
51075107 /// direction of an 8x8 luma window and the variance gap between it and its
51085108 /// orthogonal, `sample(row, col)` reading that window's own pixels.
5109-fn cdef_find_dir(sample: impl Fn(usize, usize) -> i32) -> (usize, i32) {
5109+fn cdef_find_dir(coeff_shift: i32, sample: impl Fn(usize, usize) -> i32) -> (usize, i32) {
51105110 const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105];
51115111 let mut partial = [[0i64; 15]; 8];
51125112 for i in 0..8i64 {
51135113 for j in 0..8i64 {
5114- let x = i64::from(sample(i as usize, j as usize)) - 128;
5114+ // libaom `cdef_find_dir_c`: normalise to an 8-bit sample before
5115+ // accumulating, same as `constrain`'s threshold shift below.
5116+ let x = i64::from(sample(i as usize, j as usize) >> coeff_shift) - 128;
51155117 partial[0][(i + j) as usize] += x;
51165118 partial[1][(i + j / 2) as usize] += x;
51175119 partial[2][i as usize] += x;
@@ -5907,7 +5909,10 @@ fn apply_cdef(
59075909 let src_y = y.data.clone();
59085910 let src_u = u.data.clone();
59095911 let src_v = v.data.clone();
5910- let damping = i32::from(cdef.damping);
5912+ // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`,
5913+ // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`.
5914+ let coeff_shift = i32::from(bit_depth()) - 8;
5915+ let damping = i32::from(cdef.damping) + coeff_shift;
59115916 let damping_uv = (damping - 1).max(0);
59125917 let mut mi_r = 0usize;
59135918 while mi_r < skip_grid.mi_rows {
@@ -5925,7 +5930,7 @@ fn apply_cdef(
59255930 i32::from(src_y[ny as usize * y_stride + nx as usize])
59265931 }
59275932 };
5928- let (dir, var) = cdef_find_dir(|r, c| {
5933+ let (dir, var) = cdef_find_dir(coeff_shift, |r, c| {
59295934 let (ny, nx) = ((oy + r).min(y_true_h - 1), (ox + c).min(y_true_w - 1));
59305935 i32::from(src_y[ny * y_stride + nx])
59315936 });
@@ -5934,10 +5939,12 @@ fn apply_cdef(
59345939 // -- each plane zeroes the shared direction when *its own*
59355940 // frame-level primary strength is 0, independent of the
59365941 // per-block adjusted `t` and of the other plane's strength.
5937- let y_dir = if cdef.y_pri_strength[sidx] != 0 { dir } else { 0 };
5938- let t = cdef_adjust_strength(i32::from(cdef.y_pri_strength[sidx]), var);
5942+ let y_pri_strength = i32::from(cdef.y_pri_strength[sidx]) << coeff_shift;
5943+ let y_sec_strength = i32::from(cdef.y_sec_strength[sidx]) << coeff_shift;
5944+ let y_dir = if y_pri_strength != 0 { dir } else { 0 };
5945+ let t = cdef_adjust_strength(y_pri_strength, var);
59395946 let enable_primary = t != 0;
5940- let enable_secondary = cdef.y_sec_strength[sidx] != 0;
5947+ let enable_secondary = y_sec_strength != 0;
59415948 if enable_primary || enable_secondary {
59425949 cdef_filter_block(
59435950 sample_y,
@@ -5947,7 +5954,7 @@ fn apply_cdef(
59475954 8,
59485955 8,
59495956 t,
5950- i32::from(cdef.y_sec_strength[sidx]),
5957+ y_sec_strength,
59515958 y_dir,
59525959 damping,
59535960 damping,
@@ -5956,10 +5963,11 @@ fn apply_cdef(
59565963 );
59575964 }
59585965
5959- let t_uv = i32::from(cdef.uv_pri_strength[sidx]);
5966+ let t_uv = i32::from(cdef.uv_pri_strength[sidx]) << coeff_shift;
5967+ let uv_sec_strength = i32::from(cdef.uv_sec_strength[sidx]) << coeff_shift;
59605968 let uv_dir = if t_uv != 0 { dir } else { 0 };
59615969 let enable_primary_uv = t_uv != 0;
5962- let enable_secondary_uv = cdef.uv_sec_strength[sidx] != 0;
5970+ let enable_secondary_uv = uv_sec_strength != 0;
59635971 if enable_primary_uv || enable_secondary_uv {
59645972 let (cox, coy) = (ox / 2, oy / 2);
59655973 for (plane, src_p) in [(&mut *u, &src_u), (&mut *v, &src_v)] {
@@ -5981,7 +5989,7 @@ fn apply_cdef(
59815989 4,
59825990 4,
59835991 t_uv,
5984- i32::from(cdef.uv_sec_strength[sidx]),
5992+ uv_sec_strength,
59855993 uv_dir,
59865994 damping_uv,
59875995 damping_uv,