Unscaled MC does one contiguous horizontal pass, no per-call alloc
Browse files at this commit62f159c29e56dd4fa52f93dc6db95a1975c6d7bfParent: 1a54674- perf
- unscaled MC does one contiguous horizontal pass, no per-call alloc
1 changed file +129 -25
- crates/ec-av1/src/mc.rs +129 -25
crates/ec-av1/src/mc.rsmodified+129 -25
| @@ -243,6 +243,111 @@ fn sample( | ||
| 243 | 243 | i32::from(reference[cy * stride + cx]) |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | +/// lane-perf2: one row of the separable filter's horizontal pass, dav1d | |
| 247 | +/// style. When the row's whole 8-tap footprint (`x0-3 .. x0+block_w+4`) lies | |
| 248 | +/// inside the reference's true extent -- the overwhelmingly common case -- | |
| 249 | +/// the row is one contiguous slice and the taps are a fixed `[i32; 8]`, so | |
| 250 | +/// the per-sample `Clip3` and the per-tap table load both leave the inner | |
| 251 | +/// loop and it auto-vectorises. The edge case keeps the exact spec form | |
| 252 | +/// (`sample`'s clamp per tap); both arms compute the same sums. | |
| 253 | +#[inline] | |
| 254 | +fn hpass_row(reference: &[u16], row_base: usize, true_width: usize, x0: i32, taps: &[i32; 8], out: &mut [i32]) { | |
| 255 | + let block_w = out.len(); | |
| 256 | + let start = x0 - 3; | |
| 257 | + if start >= 0 && start as usize + block_w + 7 <= true_width { | |
| 258 | + let s = row_base + start as usize; | |
| 259 | + if let Some(src) = reference.get(s..s + block_w + 7) { | |
| 260 | + for (o, w) in out.iter_mut().zip(src.windows(8)) { | |
| 261 | + let mut sum = 0i32; | |
| 262 | + for t in 0..8 { | |
| 263 | + sum += taps[t] * i32::from(w[t]); | |
| 264 | + } | |
| 265 | + *o = round2(sum, INTER_ROUND_0); | |
| 266 | + } | |
| 267 | + return; | |
| 268 | + } | |
| 269 | + } | |
| 270 | + for (c, o) in out.iter_mut().enumerate() { | |
| 271 | + let mut sum = 0i32; | |
| 272 | + for (t, &tap) in taps.iter().enumerate() { | |
| 273 | + let x = (x0 + c as i32 + t as i32 - 3).clamp(0, true_width as i32 - 1) as usize; | |
| 274 | + sum += tap * i32::from(reference[row_base + x]); | |
| 275 | + } | |
| 276 | + *o = round2(sum, INTER_ROUND_0); | |
| 277 | + } | |
| 278 | +} | |
| 279 | + | |
| 280 | +/// lane-perf2: the unscaled horizontal pass, `block_h + 7` rows of | |
| 281 | +/// [`hpass_row`] with the vertical clamp hoisted out of the row. | |
| 282 | +#[allow(clippy::too_many_arguments)] | |
| 283 | +fn horizontal_pass_unscaled( | |
| 284 | + reference: &[u16], | |
| 285 | + stride: usize, | |
| 286 | + true_width: usize, | |
| 287 | + true_height: usize, | |
| 288 | + x0: i32, | |
| 289 | + y0: i32, | |
| 290 | + taps: &[i32; 8], | |
| 291 | + block_w: usize, | |
| 292 | + rows: usize, | |
| 293 | + intermediate: &mut [i32], | |
| 294 | +) { | |
| 295 | + for r in 0..rows { | |
| 296 | + let y = (y0 - 3 + r as i32).clamp(0, true_height as i32 - 1) as usize; | |
| 297 | + hpass_row( | |
| 298 | + reference, | |
| 299 | + y * stride, | |
| 300 | + true_width, | |
| 301 | + x0, | |
| 302 | + taps, | |
| 303 | + &mut intermediate[r * block_w..(r + 1) * block_w], | |
| 304 | + ); | |
| 305 | + } | |
| 306 | +} | |
| 307 | + | |
| 308 | +/// lane-perf2: one output row of the vertical pass, accumulated tap-major so | |
| 309 | +/// the inner loop walks two contiguous `i32` slices (vectorisable) instead of | |
| 310 | +/// striding the intermediate by `block_w` per tap. A zero tap is skipped -- | |
| 311 | +/// adding `0 * x` is exactly nothing, and the 4-tap kernels zero four of the | |
| 312 | +/// eight slots. `acc` is left holding the unrounded sums. | |
| 313 | +#[inline] | |
| 314 | +fn vpass_row(intermediate: &[i32], block_w: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) { | |
| 315 | + acc.fill(0); | |
| 316 | + for (t, &tap) in taps.iter().enumerate() { | |
| 317 | + if tap == 0 { | |
| 318 | + continue; | |
| 319 | + } | |
| 320 | + let base = (row + t) * block_w; | |
| 321 | + for (a, &s) in acc.iter_mut().zip(&intermediate[base..base + block_w]) { | |
| 322 | + *a += tap * s; | |
| 323 | + } | |
| 324 | + } | |
| 325 | +} | |
| 326 | + | |
| 327 | +thread_local! { | |
| 328 | + /// lane-perf2: the separable filter's intermediate buffer, reused across | |
| 329 | + /// calls -- a 4K inter frame runs `predict*` hundreds of thousands of | |
| 330 | + /// times and each call used to `vec![0i32; (block_h+7) * block_w]`. The | |
| 331 | + /// horizontal pass writes every element it later reads, so no clearing is | |
| 332 | + /// needed; the tail `block_w` elements are the vertical pass's row | |
| 333 | + /// accumulator. | |
| 334 | + static MC_SCRATCH: std::cell::RefCell<Vec<i32>> = const { std::cell::RefCell::new(Vec::new()) }; | |
| 335 | +} | |
| 336 | + | |
| 337 | +/// Runs `f` on a scratch slice of `rows * block_w` intermediate elements plus | |
| 338 | +/// a `block_w` accumulator (see [`MC_SCRATCH`]). | |
| 339 | +fn with_scratch<R>(rows: usize, block_w: usize, f: impl FnOnce(&mut [i32], &mut [i32]) -> R) -> R { | |
| 340 | + MC_SCRATCH.with(|s| { | |
| 341 | + let mut buf = s.borrow_mut(); | |
| 342 | + let need = rows * block_w + block_w; | |
| 343 | + if buf.len() < need { | |
| 344 | + buf.resize(need, 0); | |
| 345 | + } | |
| 346 | + let (inter, acc) = buf[..need].split_at_mut(rows * block_w); | |
| 347 | + f(inter, acc) | |
| 348 | + }) | |
| 349 | +} | |
| 350 | + | |
| 246 | 351 | /// Predicts one `block_w * block_h` block into `dst`, row-major, from |
| 247 | 352 | /// `reference` (row-major, stride `stride`, true content |
| 248 | 353 | /// `true_width` x `true_height` -- a reference frame's decoded picture |
| @@ -395,11 +500,19 @@ pub fn predict_with_filters_kern( | ||
| 395 | 500 | // it whole-pel candidates from stage 1). |
| 396 | 501 | if xfrac == 0 && yfrac == 0 { |
| 397 | 502 | for row in 0..block_h { |
| 398 | - let y = y0 + row as i32; | |
| 399 | - for col in 0..block_w { | |
| 400 | - let x = x0 + col as i32; | |
| 401 | - dst[row * block_w + col] = | |
| 402 | - sample(reference, stride, true_width, true_height, x, y) as u16; | |
| 503 | + let y = (y0 + row as i32).clamp(0, true_height as i32 - 1) as usize; | |
| 504 | + let row_base = y * stride; | |
| 505 | + let out = &mut dst[row * block_w..(row + 1) * block_w]; | |
| 506 | + // lane-perf2: an interior row is one contiguous copy. | |
| 507 | + if x0 >= 0 && x0 as usize + block_w <= true_width { | |
| 508 | + if let Some(src) = reference.get(row_base + x0 as usize..row_base + x0 as usize + block_w) { | |
| 509 | + out.copy_from_slice(src); | |
| 510 | + continue; | |
| 511 | + } | |
| 512 | + } | |
| 513 | + for (col, o) in out.iter_mut().enumerate() { | |
| 514 | + let x = (x0 + col as i32).clamp(0, true_width as i32 - 1) as usize; | |
| 515 | + *o = reference[row_base + x]; | |
| 403 | 516 | } |
| 404 | 517 | } |
| 405 | 518 | #[cfg(test)] |
| @@ -424,28 +537,19 @@ pub fn predict_with_filters_kern( | ||
| 424 | 537 | // The vertical pass reads 3 rows above and 4 below the block, so the |
| 425 | 538 | // horizontal pass must produce that many extra intermediate rows. |
| 426 | 539 | let rows = block_h + 7; |
| 427 | - let mut intermediate = vec![0i32; rows * block_w]; | |
| 428 | - for r in 0..rows { | |
| 429 | - let y = y0 - 3 + r as i32; | |
| 430 | - for c in 0..block_w { | |
| 431 | - let mut sum = 0; | |
| 432 | - for (t, &tap) in h_filter.iter().enumerate() { | |
| 433 | - let x = x0 + c as i32 + t as i32 - 3; | |
| 434 | - sum += tap * sample(reference, stride, true_width, true_height, x, y); | |
| 435 | - } | |
| 436 | - intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0); | |
| 437 | - } | |
| 438 | - } | |
| 439 | - | |
| 440 | - for row in 0..block_h { | |
| 441 | - for col in 0..block_w { | |
| 442 | - let mut sum = 0; | |
| 443 | - for (t, &tap) in v_filter.iter().enumerate() { | |
| 444 | - sum += tap * intermediate[(row + t) * block_w + col]; | |
| 540 | + let max = crate::decode::sample_max(); | |
| 541 | + with_scratch(rows, block_w, |intermediate, acc| { | |
| 542 | + horizontal_pass_unscaled( | |
| 543 | + reference, stride, true_width, true_height, x0, y0, h_filter, block_w, rows, | |
| 544 | + intermediate, | |
| 545 | + ); | |
| 546 | + for row in 0..block_h { | |
| 547 | + vpass_row(intermediate, block_w, row, v_filter, acc); | |
| 548 | + for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) { | |
| 549 | + *d = round2(a, INTER_ROUND_1).clamp(0, max) as u16; | |
| 445 | 550 | } |
| 446 | - dst[row * block_w + col] = round2(sum, INTER_ROUND_1).clamp(0, crate::decode::sample_max()) as u16; | |
| 447 | 551 | } |
| 448 | - } | |
| 552 | + }); | |
| 449 | 553 | |
| 450 | 554 | #[cfg(test)] |
| 451 | 555 | crate::encode::stage_add(1, stage_t.elapsed()); |