Merge branch 'lane-h265simd': quantize SIMD + angular-intra hoist
Browse files at this commit4 changed files +67 -15
- Cargo.lock +1 -0
- crates/ec-h265/Cargo.toml +1 -0
- crates/ec-h265/src/intra.rs +39 -13
- crates/ec-h265/src/transform.rs +26 -2
Cargo.lockmodified+1 -0
| @@ -329,6 +329,7 @@ version = "0.1.0" | |||
| 329 | dependencies = [ | 329 | dependencies = [ |
| 330 | "ec-core", | 330 | "ec-core", |
| 331 | "ec-h265-syntax", | 331 | "ec-h265-syntax", |
| 332 | + "wide", | ||
| 332 | ] | 333 | ] |
| 333 | 334 | ||
| 334 | [[package]] | 335 | [[package]] |
crates/ec-h265/Cargo.tomlmodified+1 -0
| @@ -9,6 +9,7 @@ authors.workspace = true | |||
| 9 | [dependencies] | 9 | [dependencies] |
| 10 | ec-core.workspace = true | 10 | ec-core.workspace = true |
| 11 | ec-h265-syntax.workspace = true | 11 | ec-h265-syntax.workspace = true |
| 12 | +wide.workspace = true | ||
| 12 | 13 | ||
| 13 | [dev-dependencies] | 14 | [dev-dependencies] |
| 14 | ec-h265-syntax.workspace = true | 15 | ec-h265-syntax.workspace = true |
crates/ec-h265/src/intra.rsmodified+39 -13
| @@ -282,21 +282,47 @@ fn predict_angular(refs: &Refs, mode: u8, n: usize, is_luma: bool, out: &mut [u8 | |||
| 282 | reference[BIAS + x] = i32::from(main[x - 1]); | 282 | reference[BIAS + x] = i32::from(main[x - 1]); |
| 283 | } | 283 | } |
| 284 | } | 284 | } |
| 285 | - for y in 0..n { | 285 | + // `idx`/`fact` depend only on the "row" index along the prediction |
| 286 | - // The "row" index along the prediction direction: y for the vertical | 286 | + // direction (y for the vertical modes, x for the horizontal ones — the |
| 287 | - // modes, x for the horizontal ones, which is the whole difference | 287 | + // whole difference between the two halves of 8.4.4.2.6), not on the |
| 288 | - // between the two halves of 8.4.4.2.6. | 288 | + // position across it. The mode>=18 branch and the idx/fact division used |
| 289 | + // to be recomputed at every one of the n*n pixels; hoisting them to one | ||
| 290 | + // per row/column turns the inner loop into a contiguous, near-linear scan | ||
| 291 | + // over `reference`, which is both fewer instructions and autovectorizes. | ||
| 292 | + if mode >= 18 { | ||
| 293 | + for y in 0..n { | ||
| 294 | + let idx = ((y as i32 + 1) * angle) >> 5; | ||
| 295 | + let fact = ((y as i32 + 1) * angle) & 31; | ||
| 296 | + let base = (BIAS as i32 + idx + 1) as usize; | ||
| 297 | + let row = &mut out[y * n..y * n + n]; | ||
| 298 | + if fact != 0 { | ||
| 299 | + let (w0, w1) = (32 - fact, fact); | ||
| 300 | + for x in 0..n { | ||
| 301 | + row[x] = | ||
| 302 | + ((w0 * reference[base + x] + w1 * reference[base + x + 1] + 16) >> 5) as u8; | ||
| 303 | + } | ||
| 304 | + } else { | ||
| 305 | + for x in 0..n { | ||
| 306 | + row[x] = reference[base + x] as u8; | ||
| 307 | + } | ||
| 308 | + } | ||
| 309 | + } | ||
| 310 | + } else { | ||
| 289 | for x in 0..n { | 311 | for x in 0..n { |
| 290 | - let (along, across) = if mode >= 18 { (y, x) } else { (x, y) }; | 312 | + let idx = ((x as i32 + 1) * angle) >> 5; |
| 291 | - let idx = ((along as i32 + 1) * angle) >> 5; | 313 | + let fact = ((x as i32 + 1) * angle) & 31; |
| 292 | - let fact = ((along as i32 + 1) * angle) & 31; | 314 | + let base = (BIAS as i32 + idx + 1) as usize; |
| 293 | - let base = (BIAS as i32 + across as i32 + idx + 1) as usize; | 315 | + if fact != 0 { |
| 294 | - let value = if fact != 0 { | 316 | + let (w0, w1) = (32 - fact, fact); |
| 295 | - ((32 - fact) * reference[base] + fact * reference[base + 1] + 16) >> 5 | 317 | + for y in 0..n { |
| 318 | + out[y * n + x] = | ||
| 319 | + ((w0 * reference[base + y] + w1 * reference[base + y + 1] + 16) >> 5) as u8; | ||
| 320 | + } | ||
| 296 | } else { | 321 | } else { |
| 297 | - reference[base] | 322 | + for y in 0..n { |
| 298 | - }; | 323 | + out[y * n + x] = reference[base + y] as u8; |
| 299 | - out[y * n + x] = value as u8; | 324 | + } |
| 325 | + } | ||
| 300 | } | 326 | } |
| 301 | } | 327 | } |
| 302 | // The vertical and horizontal modes carry a boundary filter on luma | 328 | // The vertical and horizontal modes carry a boundary filter on luma |
crates/ec-h265/src/transform.rsmodified+26 -2
| @@ -365,11 +365,35 @@ pub fn quantize_offset( | |||
| 365 | let qbits = 21 + qp / 6 - log2n; | 365 | let qbits = 21 + qp / 6 - log2n; |
| 366 | let scale = QUANT_SCALE[(qp % 6) as usize] as i64; | 366 | let scale = QUANT_SCALE[(qp % 6) as usize] as i64; |
| 367 | let offset = (1i64 << qbits) / dead_zone; | 367 | let offset = (1i64 << qbits) / dead_zone; |
| 368 | + // `scale` <= 26_214 and `|coeffs[i]|` <= 32_768 (COEFF_MIN/MAX), and qp is | ||
| 369 | + // clamped to 0..=51 (crates/ec-h265/src/encoder.rs), so | ||
| 370 | + // `abs(c) * scale + offset` never exceeds ~1e9: it fits in i32 without | ||
| 371 | + // overflow, which is what lets this run in 32-bit SIMD lanes and still be | ||
| 372 | + // the exact same integer as the scalar i64 computation below. | ||
| 373 | + debug_assert!(scale <= i64::from(i32::MAX) && offset <= i64::from(i32::MAX)); | ||
| 374 | + let scale32 = scale as i32; | ||
| 375 | + let offset32 = offset as i32; | ||
| 368 | let mut nonzero = 0; | 376 | let mut nonzero = 0; |
| 369 | - for (i, &c) in coeffs[..n * n].iter().enumerate() { | 377 | + let coeffs = &coeffs[..n * n]; |
| 378 | + let mut i = 0; | ||
| 379 | + while i + 8 <= coeffs.len() { | ||
| 380 | + let c = wide::i32x8::from(<[i32; 8]>::try_from(&coeffs[i..i + 8]).unwrap()); | ||
| 381 | + let mag = | ||
| 382 | + ((c.abs() * scale32 + offset32) >> (qbits as u32)).min(wide::i32x8::splat(COEFF_MAX)); | ||
| 383 | + let level = c.simd_lt(wide::i32x8::splat(0)).select(-mag, mag); | ||
| 384 | + for (j, &lv) in level.to_array().iter().enumerate() { | ||
| 385 | + levels[i + j] = lv; | ||
| 386 | + if lv != 0 { | ||
| 387 | + nonzero += 1; | ||
| 388 | + } | ||
| 389 | + } | ||
| 390 | + i += 8; | ||
| 391 | + } | ||
| 392 | + for j in i..coeffs.len() { | ||
| 393 | + let c = coeffs[j]; | ||
| 370 | let magnitude = ((c.unsigned_abs() as i64 * scale + offset) >> qbits) as i32; | 394 | let magnitude = ((c.unsigned_abs() as i64 * scale + offset) >> qbits) as i32; |
| 371 | let level = magnitude.min(COEFF_MAX); | 395 | let level = magnitude.min(COEFF_MAX); |
| 372 | - levels[i] = if c < 0 { -level } else { level }; | 396 | + levels[j] = if c < 0 { -level } else { level }; |
| 373 | if level != 0 { | 397 | if level != 0 { |
| 374 | nonzero += 1; | 398 | nonzero += 1; |
| 375 | } | 399 | } |