M13.4: hevcenc sign data hiding, noise / QP 0 tests, README
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
c9d5e7335e
commit
9e4942592c
@@ -563,7 +563,14 @@ impl<'a> FrameCoder<'a> {
|
||||
let off = self.coefs.len();
|
||||
self.coefs.resize(off + n * n, 0);
|
||||
let lv = &mut self.coefs[off..off + n * n];
|
||||
let scan = match intra_mode {
|
||||
Some(m) if log2 == 2 || (log2 == 3 && c == 0) => scan_idx_for_mode(m),
|
||||
_ => 0,
|
||||
};
|
||||
let nz = transform::quantize(&coef[..n * n], lv, n, qp, intra, self.bd) > 0;
|
||||
if nz {
|
||||
transform::sign_hiding(&coef[..n * n], lv, log2, scan as usize, qp, self.bd);
|
||||
}
|
||||
if nz {
|
||||
let d = &mut r[..n * n];
|
||||
transform::dequantize(lv, d, n, qp, self.bd);
|
||||
@@ -581,10 +588,6 @@ impl<'a> FrameCoder<'a> {
|
||||
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,
|
||||
};
|
||||
self.tbs.push(Tb { c: c as u8, lx: lpos.0 as u32, ly: lpos.1 as u32, log2, nz, off, scan });
|
||||
}
|
||||
|
||||
@@ -1313,9 +1316,17 @@ fn residual_coding<S: Sink>(s: &mut S, lv: &[i32], log2: u32, c: usize, scan_idx
|
||||
gt2 = (abs_at(k) > 2) as u32;
|
||||
s.decision(GT2 + ctx_set + if c > 0 { 4 } else { 0 }, gt2);
|
||||
}
|
||||
for k in 0..nsig {
|
||||
// sign data hiding: the sign of the first coefficient in scan order (sig[nsig - 1]) is
|
||||
// inferred from the parity of the sum of absolute levels
|
||||
let hidden = sig[0] as i32 - sig[nsig - 1] as i32 > 3;
|
||||
let nsigns = if hidden { nsig - 1 } else { nsig };
|
||||
for k in 0..nsigns {
|
||||
s.bypass((at(xs, ys, sig[k] as usize) < 0) as u32);
|
||||
}
|
||||
if hidden {
|
||||
let sum: u32 = (0..nsig).map(abs_at).sum();
|
||||
debug_assert_eq!(at(xs, ys, sig[nsig - 1] as usize) < 0, sum & 1 == 1, "sign hiding parity");
|
||||
}
|
||||
let mut rice = 0u32;
|
||||
for k in 0..nsig {
|
||||
let mut base = 1 + gt1[k] as u32;
|
||||
|
||||
@@ -189,7 +189,7 @@ pub fn pps() -> Vec<u8> {
|
||||
w.put(0, 1); // dependent_slice_segments_enabled_flag
|
||||
w.put(0, 1); // output_flag_present_flag
|
||||
w.put(0, 3); // num_extra_slice_header_bits
|
||||
w.put(0, 1); // sign_data_hiding_enabled_flag
|
||||
w.put(1, 1); // sign_data_hiding_enabled_flag
|
||||
w.put(0, 1); // cabac_init_present_flag
|
||||
w.ue(0); // num_ref_idx_l0_default_active_minus1
|
||||
w.ue(0); // num_ref_idx_l1_default_active_minus1
|
||||
|
||||
@@ -193,6 +193,56 @@ pub fn quantize(c: &[i32], l: &mut [i32], n: usize, qp: i32, intra: bool, bit_de
|
||||
nz
|
||||
}
|
||||
|
||||
/// Sign data hiding (7.3.8.11 signHidden): in every 4x4 sub-block whose first and last non-zero
|
||||
/// coefficients (in scan order) are more than 3 positions apart, the sign of the first one is not
|
||||
/// coded but inferred from the parity of the sum of absolute levels. Fix the parity where needed by
|
||||
/// the +-1 level change that adds the least quantisation error, without moving the first / last
|
||||
/// non-zero positions.
|
||||
pub fn sign_hiding(c: &[i32], l: &mut [i32], log2: u32, scan_idx: usize, qp: i32, bit_depth: u32) {
|
||||
use crate::tables::{SCAN_4, scan_order};
|
||||
let n = 1usize << log2;
|
||||
let tshift = 15 - bit_depth as i32 - log2 as i32;
|
||||
let qbits = 14 + qp / 6 + tshift;
|
||||
let scale = QUANT_SCALE[(qp % 6) as usize];
|
||||
let sb_scan = scan_order(log2 - 2, scan_idx);
|
||||
let pos = &SCAN_4[scan_idx];
|
||||
for &(xs, ys) in sb_scan {
|
||||
let idx = |p: usize| (ys as usize * 4 + pos[p].1 as usize) * n + xs as usize * 4 + pos[p].0 as usize;
|
||||
let (Some(first), Some(last)) = ((0..16).find(|&p| l[idx(p)] != 0), (0..16).rev().find(|&p| l[idx(p)] != 0)) else { continue };
|
||||
if last - first <= 3 {
|
||||
continue;
|
||||
}
|
||||
let sum: i32 = (0..16).map(|p| l[idx(p)].abs()).sum();
|
||||
let negative = l[idx(first)] < 0;
|
||||
if negative == (sum & 1 == 1) {
|
||||
continue;
|
||||
}
|
||||
// cost of +-1 changes in 1/256 units of quantisation error
|
||||
let mut best: Option<(i64, usize, i32)> = None;
|
||||
for p in first..=last {
|
||||
let i = idx(p);
|
||||
let q = (c[i] as i64).abs() * scale >> (qbits - 8);
|
||||
let a = l[i].abs() as i64;
|
||||
let err = |lv: i64| (q - (lv << 8)).abs();
|
||||
let mut consider = |cost: i64, d: i32| {
|
||||
if best.is_none_or(|b| cost < b.0) {
|
||||
best = Some((cost, i, d));
|
||||
}
|
||||
};
|
||||
if a < 32767 && (a > 0 || (p > first && p < last)) {
|
||||
consider(err(a + 1) - err(a), 1);
|
||||
}
|
||||
if a > 1 || (a == 1 && p > first && p < last) {
|
||||
consider(err(a - 1) - err(a), -1);
|
||||
}
|
||||
}
|
||||
if let Some((_, i, d)) = best {
|
||||
let a = l[i].abs() + d;
|
||||
l[i] = if c[i] < 0 { -a } else { a };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Scaling process (8.6.2 / 8.6.3, flat scaling matrix): levels to scaled coefficients.
|
||||
pub fn dequantize(l: &[i32], d: &mut [i32], n: usize, qp: i32, bit_depth: u32) {
|
||||
let log2 = n.trailing_zeros();
|
||||
|
||||
Reference in New Issue
Block a user