diff --git a/crates/ec-h265/src/residual.rs b/crates/ec-h265/src/residual.rs index a3f5a513..1d3fddcb 100644 --- a/crates/ec-h265/src/residual.rs +++ b/crates/ec-h265/src/residual.rs @@ -836,6 +836,7 @@ fn encode_subblock( } else { 15 }; + let sig_base = sig_ctx_base(xs, ys, sub_wide, csbf, log2_size, c_idx, scan_idx); let mut scan_pos = first; while scan_pos >= 0 { let p = scan_pos as usize; @@ -845,7 +846,7 @@ fn encode_subblock( let yc = ys * 4 + yp as usize; let sig = levels[yc * n + xc] != 0; if p > 0 || !infer_dc_sig { - let inc = sig_ctx_inc(xc, yc, xs, ys, sub_wide, csbf, log2_size, c_idx, scan_idx); + let inc = sig_ctx_inc(xc, yc, log2_size, c_idx, sig_base); enc.encode_bin(ctx::SIG_COEFF + inc, u32::from(sig)); } significant[p] = sig; @@ -1095,6 +1096,7 @@ pub fn decode_residual( } else { 15 }; + let sig_base = sig_ctx_base(xs, ys, sub_wide, &csbf, log2_size, c_idx, scan_idx); let mut scan_pos = first; while scan_pos >= 0 { let p = scan_pos as usize; @@ -1103,7 +1105,7 @@ pub fn decode_residual( let xc = xs * 4 + xp as usize; let yc = ys * 4 + yp as usize; let sig = if p > 0 || !infer_dc_sig { - let inc = sig_ctx_inc(xc, yc, xs, ys, sub_wide, &csbf, log2_size, c_idx, scan_idx); + let inc = sig_ctx_inc(xc, yc, log2_size, c_idx, sig_base); dec.decode_bin(ctx::SIG_COEFF + inc) != 0 } else { false // filled in by the post-loop inference below @@ -1202,9 +1204,13 @@ pub fn decode_residual( /// `ctxInc` for `sig_coeff_flag` (9.3.4.2.5). #[allow(clippy::too_many_arguments)] -fn sig_ctx_inc( - xc: usize, - yc: usize, +/// Sub-block-invariant part of [`sig_ctx_inc`]'s context: `prev_csbf` and the +/// additive base both depend only on the sub-block's own coordinates +/// (`xs`/`ys`), never on the position within it, so a caller stepping through +/// up to 15 significant positions in one sub-block computes this once instead +/// of on every position. `None` when `log2_size == 2` (the small-block path +/// doesn't use `prev_csbf`/base at all — [`sig_ctx_inc`] handles it directly). +fn sig_ctx_base( xs: usize, ys: usize, sub_wide: usize, @@ -1212,20 +1218,52 @@ fn sig_ctx_inc( log2_size: u32, c_idx: usize, scan_idx: usize, +) -> Option<(usize, usize)> { + if log2_size == 2 { + return None; + } + let mut prev_csbf = 0usize; + if xs < sub_wide - 1 && csbf[ys * sub_wide + xs + 1] { + prev_csbf += 1; + } + if ys < sub_wide - 1 && csbf[(ys + 1) * sub_wide + xs] { + prev_csbf += 2; + } + let mut base = 0usize; + if c_idx == 0 { + if xs + ys > 0 { + base += 3; + } + if log2_size == 3 { + base += if scan_idx == 0 { 9 } else { 15 }; + } else { + base += 21; + } + } else if log2_size == 3 { + base += 9; + } else { + base += 12; + } + if c_idx != 0 { + base += 27; + } + Some((prev_csbf, base)) +} + +fn sig_ctx_inc( + xc: usize, + yc: usize, + log2_size: u32, + c_idx: usize, + base: Option<(usize, usize)>, ) -> usize { - let mut sig_ctx; + let sig_ctx; if log2_size == 2 { sig_ctx = CTX_IDX_MAP[(yc << 2) + xc]; } else if xc + yc == 0 { sig_ctx = 0; } else { - let mut prev_csbf = 0usize; - if xs < sub_wide - 1 && csbf[ys * sub_wide + xs + 1] { - prev_csbf += 1; - } - if ys < sub_wide - 1 && csbf[(ys + 1) * sub_wide + xs] { - prev_csbf += 2; - } + let (prev_csbf, base) = base.expect("sig_ctx_base returns Some when log2_size != 2"); let (xp, yp) = (xc & 3, yc & 3); sig_ctx = match prev_csbf { 0 => { @@ -1257,20 +1295,7 @@ fn sig_ctx_inc( } _ => 2, }; - if c_idx == 0 { - if xs + ys > 0 { - sig_ctx += 3; - } - if log2_size == 3 { - sig_ctx += if scan_idx == 0 { 9 } else { 15 }; - } else { - sig_ctx += 21; - } - } else if log2_size == 3 { - sig_ctx += 9; - } else { - sig_ctx += 12; - } + return sig_ctx + base; } if c_idx == 0 { sig_ctx } else { 27 + sig_ctx } } diff --git a/crates/ec-h265/src/transform.rs b/crates/ec-h265/src/transform.rs index 97239e51..5502be45 100644 --- a/crates/ec-h265/src/transform.rs +++ b/crates/ec-h265/src/transform.rs @@ -127,6 +127,33 @@ const QUANT_SCALE: [i32; 6] = [26_214, 23_302, 20_560, 18_396, 16_384, 14_564]; const COEFF_MIN: i32 = -32_768; const COEFF_MAX: i32 = 32_767; +/// Dot product of two equal-length slices, SIMD in 8-wide chunks with a +/// scalar tail. +/// +/// Both `forward_1d` and `inverse_1d`'s odd sums are `sum_j m[j] * x[j]` over +/// at most 16 terms; matrix entries are `|m| <= 90` and `x` is a coefficient +/// or residual sample clamped to `COEFF_MIN..=COEFF_MAX` (+/-32768), so each +/// product is bounded by ~3.0e6 and the full sum of 16 by ~4.7e7 — nowhere +/// near overflowing i32, whether accumulated in one lane or spread across +/// eight and reduced at the end. +#[inline] +fn dot(a: &[i32], b: &[i32]) -> i32 { + debug_assert_eq!(a.len(), b.len()); + let mut acc = wide::i32x8::splat(0); + let mut i = 0; + while i + 8 <= a.len() { + let av = wide::i32x8::from(<[i32; 8]>::try_from(&a[i..i + 8]).unwrap()); + let bv = wide::i32x8::from(<[i32; 8]>::try_from(&b[i..i + 8]).unwrap()); + acc += av * bv; + i += 8; + } + let mut total: i32 = acc.to_array().iter().sum(); + for j in i..a.len() { + total += a[j] * b[j]; + } + total +} + /// One-dimensional inverse transform, `y[i] = sum_j M[j][i] * x[j]`. /// /// Split even/odd once per level: the even rows of the size-N matrix *are* the @@ -145,12 +172,19 @@ fn inverse_1d(n: usize, src: &[i32], dst: &mut [i32]) { } let mut even = [0i32; 16]; inverse_1d(half, &even_src[..half], &mut even[..half]); + // `src[2 * j + 1]` doesn't depend on `x`: gather it once instead of once + // per `x` (it was being re-read `half` times over). + let mut odd_src = [0i32; 16]; + for j in 0..half { + odd_src[j] = src[2 * j + 1]; + } for x in 0..half { - let mut odd = 0i32; let matrix_col = &FULL_MATRIX_T[x]; + let mut odd_matrix = [0i32; 16]; for j in 0..half { - odd += matrix_col[(2 * j + 1) * stride] * src[2 * j + 1]; + odd_matrix[j] = matrix_col[(2 * j + 1) * stride]; } + let odd = dot(&odd_matrix[..half], &odd_src[..half]); dst[x] = even[x] + odd; dst[n - 1 - x] = even[x] - odd; } @@ -178,12 +212,8 @@ fn forward_1d(n: usize, src: &[i32], dst: &mut [i32]) { forward_1d(half, &sums[..half], &mut even[..half]); for j in 0..half { dst[2 * j] = even[j]; - let mut odd = 0i32; let matrix_row = &FULL_MATRIX[(2 * j + 1) * stride]; - for (x, &diff) in diffs[..half].iter().enumerate() { - odd += matrix_row[x] * diff; - } - dst[2 * j + 1] = odd; + dst[2 * j + 1] = dot(&matrix_row[..half], &diffs[..half]); } }