diff --git a/Cargo.lock b/Cargo.lock index 65792d79..ed306929 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -329,6 +329,7 @@ version = "0.1.0" dependencies = [ "ec-core", "ec-h265-syntax", + "wide", ] [[package]] diff --git a/crates/ec-h265/Cargo.toml b/crates/ec-h265/Cargo.toml index ce5e1f69..3a11220d 100644 --- a/crates/ec-h265/Cargo.toml +++ b/crates/ec-h265/Cargo.toml @@ -9,6 +9,7 @@ authors.workspace = true [dependencies] ec-core.workspace = true ec-h265-syntax.workspace = true +wide.workspace = true [dev-dependencies] ec-h265-syntax.workspace = true diff --git a/crates/ec-h265/src/intra.rs b/crates/ec-h265/src/intra.rs index 2107a666..5710854f 100644 --- a/crates/ec-h265/src/intra.rs +++ b/crates/ec-h265/src/intra.rs @@ -282,21 +282,47 @@ fn predict_angular(refs: &Refs, mode: u8, n: usize, is_luma: bool, out: &mut [u8 reference[BIAS + x] = i32::from(main[x - 1]); } } - for y in 0..n { - // The "row" index along the prediction direction: y for the vertical - // modes, x for the horizontal ones, which is the whole difference - // between the two halves of 8.4.4.2.6. + // `idx`/`fact` depend only on the "row" index along the prediction + // direction (y for the vertical modes, x for the horizontal ones — the + // whole difference between the two halves of 8.4.4.2.6), not on the + // position across it. The mode>=18 branch and the idx/fact division used + // to be recomputed at every one of the n*n pixels; hoisting them to one + // per row/column turns the inner loop into a contiguous, near-linear scan + // over `reference`, which is both fewer instructions and autovectorizes. + if mode >= 18 { + for y in 0..n { + let idx = ((y as i32 + 1) * angle) >> 5; + let fact = ((y as i32 + 1) * angle) & 31; + let base = (BIAS as i32 + idx + 1) as usize; + let row = &mut out[y * n..y * n + n]; + if fact != 0 { + let (w0, w1) = (32 - fact, fact); + for x in 0..n { + row[x] = + ((w0 * reference[base + x] + w1 * reference[base + x + 1] + 16) >> 5) as u8; + } + } else { + for x in 0..n { + row[x] = reference[base + x] as u8; + } + } + } + } else { for x in 0..n { - let (along, across) = if mode >= 18 { (y, x) } else { (x, y) }; - let idx = ((along as i32 + 1) * angle) >> 5; - let fact = ((along as i32 + 1) * angle) & 31; - let base = (BIAS as i32 + across as i32 + idx + 1) as usize; - let value = if fact != 0 { - ((32 - fact) * reference[base] + fact * reference[base + 1] + 16) >> 5 + let idx = ((x as i32 + 1) * angle) >> 5; + let fact = ((x as i32 + 1) * angle) & 31; + let base = (BIAS as i32 + idx + 1) as usize; + if fact != 0 { + let (w0, w1) = (32 - fact, fact); + for y in 0..n { + out[y * n + x] = + ((w0 * reference[base + y] + w1 * reference[base + y + 1] + 16) >> 5) as u8; + } } else { - reference[base] - }; - out[y * n + x] = value as u8; + for y in 0..n { + out[y * n + x] = reference[base + y] as u8; + } + } } } // The vertical and horizontal modes carry a boundary filter on luma diff --git a/crates/ec-h265/src/transform.rs b/crates/ec-h265/src/transform.rs index 2038c86e..97239e51 100644 --- a/crates/ec-h265/src/transform.rs +++ b/crates/ec-h265/src/transform.rs @@ -365,11 +365,35 @@ pub fn quantize_offset( let qbits = 21 + qp / 6 - log2n; let scale = QUANT_SCALE[(qp % 6) as usize] as i64; let offset = (1i64 << qbits) / dead_zone; + // `scale` <= 26_214 and `|coeffs[i]|` <= 32_768 (COEFF_MIN/MAX), and qp is + // clamped to 0..=51 (crates/ec-h265/src/encoder.rs), so + // `abs(c) * scale + offset` never exceeds ~1e9: it fits in i32 without + // overflow, which is what lets this run in 32-bit SIMD lanes and still be + // the exact same integer as the scalar i64 computation below. + debug_assert!(scale <= i64::from(i32::MAX) && offset <= i64::from(i32::MAX)); + let scale32 = scale as i32; + let offset32 = offset as i32; let mut nonzero = 0; - for (i, &c) in coeffs[..n * n].iter().enumerate() { + let coeffs = &coeffs[..n * n]; + let mut i = 0; + while i + 8 <= coeffs.len() { + let c = wide::i32x8::from(<[i32; 8]>::try_from(&coeffs[i..i + 8]).unwrap()); + let mag = + ((c.abs() * scale32 + offset32) >> (qbits as u32)).min(wide::i32x8::splat(COEFF_MAX)); + let level = c.simd_lt(wide::i32x8::splat(0)).select(-mag, mag); + for (j, &lv) in level.to_array().iter().enumerate() { + levels[i + j] = lv; + if lv != 0 { + nonzero += 1; + } + } + i += 8; + } + for j in i..coeffs.len() { + let c = coeffs[j]; let magnitude = ((c.unsigned_abs() as i64 * scale + offset) >> qbits) as i32; let level = magnitude.min(COEFF_MAX); - levels[i] = if c < 0 { -level } else { level }; + levels[j] = if c < 0 { -level } else { level }; if level != 0 { nonzero += 1; }