Merge branch 'lane-filt1' (616629c3): in-frame filter bands on scoped worker threads for deblock, CDEF, loop restoration and film grain (EC_AV1_FILTER_THREADS, default 1); byte-exact at 1 and 4 filter threads on four inputs; 4K 34.4 s to 28.4 s at four filter threads
Browse files at this commit7 changed files +858 -531
- crates/ec-av1/fixtures/grain_cdef_lr_128x128.obu +0 -0
- crates/ec-av1/src/decode.rs +291 -217
- crates/ec-av1/src/film_grain.rs +318 -286
- crates/ec-av1/src/lib.rs +1 -0
- crates/ec-av1/src/par.rs +135 -0
- crates/ec-av1/src/restoration.rs +44 -28
- crates/ec-av1/src/stream.rs +69 -0
crates/ec-av1/fixtures/grain_cdef_lr_128x128.obuadded+0 -0
Binary file changed
crates/ec-av1/src/decode.rsmodified+291 -217
| @@ -151,6 +151,27 @@ pub(crate) trait WithRef { | |||
| 151 | } | 151 | } |
| 152 | impl<T: ?Sized> WithRef for T {} | 152 | impl<T: ?Sized> WithRef for T {} |
| 153 | 153 | ||
| 154 | +/// A [`FrameCtx`] copy for one of [`crate::par`]'s filter worker threads. | ||
| 155 | +/// | ||
| 156 | +/// `FrameCtx` is `!Sync` on purpose (a decode's state belongs to one thread), | ||
| 157 | +/// and the post-decode filter chain reads exactly these fields: the bit depth | ||
| 158 | +/// (every kernel's clamp and threshold shift), the segmentation table | ||
| 159 | +/// (`segment_id_at`/`lf_level` in the deblocker), the frame's superres state | ||
| 160 | +/// and the film-grain bit depth. Everything else keeps [`FrameCtx::new`]'s | ||
| 161 | +/// default -- a field that turns out to be read after all shows up as a hash | ||
| 162 | +/// mismatch in | ||
| 163 | +/// `a_real_stream_filters_identically_with_one_and_four_filter_threads`. | ||
| 164 | +pub(crate) fn filter_ctx_copy(fctx: &FrameCtx) -> FrameCtx { | ||
| 165 | + let out = FrameCtx::new(); | ||
| 166 | + out.bit_depth.set(fctx.bit_depth.get()); | ||
| 167 | + out.grain_bit_depth.set(fctx.grain_bit_depth.get()); | ||
| 168 | + out.superres.set(fctx.superres.get()); | ||
| 169 | + out.seg_mi_dims.set(fctx.seg_mi_dims.get()); | ||
| 170 | + out.seg.replace(fctx.seg.borrow().clone()); | ||
| 171 | + out.seg_ids.replace(fctx.seg_ids.borrow().clone()); | ||
| 172 | + out | ||
| 173 | +} | ||
| 174 | + | ||
| 154 | use crate::cdf; | 175 | use crate::cdf; |
| 155 | use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables}; | 176 | use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables}; |
| 156 | use crate::encode::{Picture, Reach}; | 177 | use crate::encode::{Picture, Reach}; |
| @@ -17333,126 +17354,158 @@ fn deblock_plane( | |||
| 17333 | // loop's order, and rows remain independent (horizontal pass mirrored). | 17354 | // loop's order, and rows remain independent (horizontal pass mirrored). |
| 17334 | let rows = plane.data.len() / stride.max(1); | 17355 | let rows = plane.data.len() / stride.max(1); |
| 17335 | let seg_lf_active = fctx.seg.with(|s| s.borrow().enabled); | 17356 | let seg_lf_active = fctx.seg.with(|s| s.borrow().enabled); |
| 17336 | - let mut x0 = 4usize; | 17357 | + // lane-filt1: both passes are split across `EC_AV1_FILTER_THREADS` |
| 17337 | - while x0 < tw { | 17358 | + // bands along the axis whose writes are disjoint. A vertical edge's |
| 17338 | - let mut y0 = 0usize; | 17359 | + // 4-sample groups write columns `x0-7 ..= x0+5` of their OWN 4 rows, so |
| 17339 | - while y0 < th { | 17360 | + // row bands never touch each other; a horizontal edge's groups write rows |
| 17340 | - if y0 + 16 <= th && x0 >= 8 && x0 + 8 <= stride { | 17361 | + // `y0-7 ..= y0+6` of their own 4 columns, so column bands never do. Both |
| 17341 | - let ps: [Option<(u8, i32)>; 4] = | 17362 | + // read the mode-info grids immutably. The grouping a band starts with is |
| 17342 | - std::array::from_fn(|k| edge_params(lf, n, plane_idx, 0, chroma, x0, y0 + 4 * k, seg_lf_active, fctx)); | 17363 | + // free: every 4-row (4-column) group's output is a pure function of that |
| 17343 | - if let Some((len, level)) = ps[0] { | 17364 | + // group, whether it goes through `filter_edge16` with three neighbours or |
| 17344 | - if ps.iter().all(|p| *p == ps[0]) | 17365 | + // through `filter_edge` alone. Band boundaries are multiples of 16 so no |
| 17345 | - && filter_edge16( | 17366 | + // group ever straddles one. |
| 17346 | - &mut plane.data, | 17367 | + let threads = crate::par::filter_threads(); |
| 17347 | - y0 * stride + x0, | 17368 | + let sp = crate::par::Shared::new(plane); |
| 17348 | - stride, | 17369 | + let vbands: Vec<(usize, usize)> = crate::par::bands(th.div_ceil(16), threads) |
| 17349 | - true, | 17370 | + .into_iter() |
| 17350 | - len, | 17371 | + .map(|(a, b)| (a * 16, (b * 16).min(th))) |
| 17351 | - level, | 17372 | + .collect(); |
| 17352 | - lf.sharpness, fctx, | 17373 | + crate::par::run_bands(&vbands, fctx, |y_lo, y_hi, fctx| { |
| 17353 | - ) | 17374 | + // SAFETY: this band writes only rows `y_lo..y_hi` of the plane, which |
| 17354 | - { | 17375 | + // no other band touches (see `Shared`'s own corner-cut note). |
| 17355 | - y0 += 16; | 17376 | + #[allow(unsafe_code)] |
| 17356 | - continue; | 17377 | + let plane = unsafe { sp.get() }; |
| 17378 | + let mut x0 = 4usize; | ||
| 17379 | + while x0 < tw { | ||
| 17380 | + let mut y0 = y_lo; | ||
| 17381 | + while y0 < y_hi { | ||
| 17382 | + if y0 + 16 <= y_hi && x0 >= 8 && x0 + 8 <= stride { | ||
| 17383 | + let ps: [Option<(u8, i32)>; 4] = | ||
| 17384 | + std::array::from_fn(|k| edge_params(lf, n, plane_idx, 0, chroma, x0, y0 + 4 * k, seg_lf_active, fctx)); | ||
| 17385 | + if let Some((len, level)) = ps[0] { | ||
| 17386 | + if ps.iter().all(|p| *p == ps[0]) | ||
| 17387 | + && filter_edge16( | ||
| 17388 | + &mut plane.data, | ||
| 17389 | + y0 * stride + x0, | ||
| 17390 | + stride, | ||
| 17391 | + true, | ||
| 17392 | + len, | ||
| 17393 | + level, | ||
| 17394 | + lf.sharpness, fctx, | ||
| 17395 | + ) | ||
| 17396 | + { | ||
| 17397 | + y0 += 16; | ||
| 17398 | + continue; | ||
| 17399 | + } | ||
| 17357 | } | 17400 | } |
| 17358 | - } | 17401 | + for (k, p) in ps.iter().enumerate() { |
| 17359 | - for (k, p) in ps.iter().enumerate() { | 17402 | + if let Some((len, level)) = *p { |
| 17360 | - if let Some((len, level)) = *p { | 17403 | + filter_edge( |
| 17361 | - filter_edge( | 17404 | + &mut plane.data, |
| 17362 | - &mut plane.data, | 17405 | + (y0 + 4 * k) * stride + x0, |
| 17363 | - (y0 + 4 * k) * stride + x0, | 17406 | + stride as isize, |
| 17364 | - stride as isize, | 17407 | + 1, |
| 17365 | - 1, | 17408 | + len, |
| 17366 | - len, | 17409 | + level, |
| 17367 | - level, | 17410 | + lf.sharpness, fctx, |
| 17368 | - lf.sharpness, fctx, | 17411 | + ); |
| 17369 | - ); | 17412 | + } |
| 17370 | } | 17413 | } |
| 17414 | + y0 += 16; | ||
| 17415 | + continue; | ||
| 17371 | } | 17416 | } |
| 17372 | - y0 += 16; | 17417 | + if let Some((len, level)) = edge_params(lf, n, plane_idx, 0, chroma, x0, y0, seg_lf_active, fctx) { |
| 17373 | - continue; | 17418 | + if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 { |
| 17374 | - } | 17419 | + eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}"); |
| 17375 | - if let Some((len, level)) = edge_params(lf, n, plane_idx, 0, chroma, x0, y0, seg_lf_active, fctx) { | 17420 | + } |
| 17376 | - if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 { | 17421 | + filter_edge( |
| 17377 | - eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}"); | 17422 | + &mut plane.data, |
| 17423 | + y0 * stride + x0, | ||
| 17424 | + stride as isize, | ||
| 17425 | + 1, | ||
| 17426 | + len, | ||
| 17427 | + level, | ||
| 17428 | + lf.sharpness, fctx, | ||
| 17429 | + ); | ||
| 17378 | } | 17430 | } |
| 17379 | - filter_edge( | 17431 | + y0 += 4; |
| 17380 | - &mut plane.data, | ||
| 17381 | - y0 * stride + x0, | ||
| 17382 | - stride as isize, | ||
| 17383 | - 1, | ||
| 17384 | - len, | ||
| 17385 | - level, | ||
| 17386 | - lf.sharpness, fctx, | ||
| 17387 | - ); | ||
| 17388 | } | 17432 | } |
| 17389 | - y0 += 4; | 17433 | + x0 += 4; |
| 17390 | } | 17434 | } |
| 17391 | - x0 += 4; | 17435 | + }); |
| 17392 | - } | 17436 | + // The horizontal pass runs only after the WHOLE vertical pass of this |
| 17393 | - let mut y0 = 4usize; | 17437 | + // plane (spec 7.14 order), which `run_bands` guarantees by joining. |
| 17394 | - while y0 < th { | 17438 | + let hbands: Vec<(usize, usize)> = crate::par::bands(tw.div_ceil(16), threads) |
| 17395 | - let mut x0 = 0usize; | 17439 | + .into_iter() |
| 17396 | - while x0 < tw { | 17440 | + .map(|(a, b)| (a * 16, (b * 16).min(tw))) |
| 17397 | - if x0 + 16 <= tw && y0 >= 8 && y0 + 7 <= rows { | 17441 | + .collect(); |
| 17398 | - let ps: [Option<(u8, i32)>; 4] = | 17442 | + crate::par::run_bands(&hbands, fctx, |x_lo, x_hi, fctx| { |
| 17399 | - std::array::from_fn(|k| edge_params(lf, n, plane_idx, 1, chroma, x0 + 4 * k, y0, seg_lf_active, fctx)); | 17443 | + // SAFETY: this band writes only columns `x_lo..x_hi`. |
| 17400 | - if let Some((len, level)) = ps[0] { | 17444 | + #[allow(unsafe_code)] |
| 17401 | - if ps.iter().all(|p| *p == ps[0]) | 17445 | + let plane = unsafe { sp.get() }; |
| 17402 | - && filter_edge16( | 17446 | + let mut y0 = 4usize; |
| 17403 | - &mut plane.data, | 17447 | + while y0 < th { |
| 17404 | - y0 * stride + x0, | 17448 | + let mut x0 = x_lo; |
| 17405 | - stride, | 17449 | + while x0 < x_hi { |
| 17406 | - false, | 17450 | + if x0 + 16 <= x_hi && y0 >= 8 && y0 + 7 <= rows { |
| 17407 | - len, | 17451 | + let ps: [Option<(u8, i32)>; 4] = |
| 17408 | - level, | 17452 | + std::array::from_fn(|k| edge_params(lf, n, plane_idx, 1, chroma, x0 + 4 * k, y0, seg_lf_active, fctx)); |
| 17409 | - lf.sharpness, fctx, | 17453 | + if let Some((len, level)) = ps[0] { |
| 17410 | - ) | 17454 | + if ps.iter().all(|p| *p == ps[0]) |
| 17411 | - { | 17455 | + && filter_edge16( |
| 17412 | - x0 += 16; | 17456 | + &mut plane.data, |
| 17413 | - continue; | 17457 | + y0 * stride + x0, |
| 17458 | + stride, | ||
| 17459 | + false, | ||
| 17460 | + len, | ||
| 17461 | + level, | ||
| 17462 | + lf.sharpness, fctx, | ||
| 17463 | + ) | ||
| 17464 | + { | ||
| 17465 | + x0 += 16; | ||
| 17466 | + continue; | ||
| 17467 | + } | ||
| 17414 | } | 17468 | } |
| 17469 | + for (k, p) in ps.iter().enumerate() { | ||
| 17470 | + if let Some((len, level)) = *p { | ||
| 17471 | + filter_edge( | ||
| 17472 | + &mut plane.data, | ||
| 17473 | + y0 * stride + x0 + 4 * k, | ||
| 17474 | + 1, | ||
| 17475 | + stride as isize, | ||
| 17476 | + len, | ||
| 17477 | + level, | ||
| 17478 | + lf.sharpness, fctx, | ||
| 17479 | + ); | ||
| 17480 | + } | ||
| 17481 | + } | ||
| 17482 | + x0 += 16; | ||
| 17483 | + continue; | ||
| 17415 | } | 17484 | } |
| 17416 | - for (k, p) in ps.iter().enumerate() { | 17485 | + if let Some((len, level)) = edge_params(lf, n, plane_idx, 1, chroma, x0, y0, seg_lf_active, fctx) { |
| 17417 | - if let Some((len, level)) = *p { | 17486 | + if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE") |
| 17418 | - filter_edge( | 17487 | + && plane_idx == 0 |
| 17419 | - &mut plane.data, | 17488 | + && x0 == 44 |
| 17420 | - y0 * stride + x0 + 4 * k, | 17489 | + { |
| 17421 | - 1, | 17490 | + eprintln!( |
| 17422 | - stride as isize, | 17491 | + "HEDGE x0={x0} y0={y0} len={len} level={level} sharpness={}", |
| 17423 | - len, | 17492 | + lf.sharpness |
| 17424 | - level, | ||
| 17425 | - lf.sharpness, fctx, | ||
| 17426 | ); | 17493 | ); |
| 17427 | } | 17494 | } |
| 17428 | - } | 17495 | + filter_edge( |
| 17429 | - x0 += 16; | 17496 | + &mut plane.data, |
| 17430 | - continue; | 17497 | + y0 * stride + x0, |
| 17431 | - } | 17498 | + 1, |
| 17432 | - if let Some((len, level)) = edge_params(lf, n, plane_idx, 1, chroma, x0, y0, seg_lf_active, fctx) { | 17499 | + stride as isize, |
| 17433 | - if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE") | 17500 | + len, |
| 17434 | - && plane_idx == 0 | 17501 | + level, |
| 17435 | - && x0 == 44 | 17502 | + lf.sharpness, fctx, |
| 17436 | - { | ||
| 17437 | - eprintln!( | ||
| 17438 | - "HEDGE x0={x0} y0={y0} len={len} level={level} sharpness={}", | ||
| 17439 | - lf.sharpness | ||
| 17440 | ); | 17503 | ); |
| 17441 | } | 17504 | } |
| 17442 | - filter_edge( | 17505 | + x0 += 4; |
| 17443 | - &mut plane.data, | ||
| 17444 | - y0 * stride + x0, | ||
| 17445 | - 1, | ||
| 17446 | - stride as isize, | ||
| 17447 | - len, | ||
| 17448 | - level, | ||
| 17449 | - lf.sharpness, fctx, | ||
| 17450 | - ); | ||
| 17451 | } | 17506 | } |
| 17452 | - x0 += 4; | 17507 | + y0 += 4; |
| 17453 | - } | 17508 | + } }); |
| 17454 | - y0 += 4; | ||
| 17455 | - } | ||
| 17456 | } | 17509 | } |
| 17457 | 17510 | ||
| 17458 | thread_local! { | 17511 | thread_local! { |
| @@ -17583,124 +17636,145 @@ fn apply_cdef( | |||
| 17583 | let coeff_shift = i32::from(bit_depth(fctx)) - 8; | 17636 | let coeff_shift = i32::from(bit_depth(fctx)) - 8; |
| 17584 | let damping = i32::from(cdef.damping) + coeff_shift; | 17637 | let damping = i32::from(cdef.damping) + coeff_shift; |
| 17585 | let damping_uv = (damping - 1).max(0); | 17638 | let damping_uv = (damping - 1).max(0); |
| 17586 | - // One scratch window per plane kind, reused by every unit (8x8 luma and | 17639 | + // lane-filt1: every CDEF unit reads ONLY the pre-CDEF snapshots |
| 17587 | - // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring). | 17640 | + // (`src_y`/`src_u`/`src_v`, immutable here) and writes its own 8x8 luma / |
| 17588 | - let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)]; | 17641 | + // 4x4 chroma square, so units are independent. Bands are mi ROW bands (2 |
| 17589 | - let mut win_uv = [0i32; (4 + 2 * CDEF_BORDER) * (4 + 2 * CDEF_BORDER)]; | 17642 | + // mi = 8 luma rows, 4 chroma rows), which makes each band's writes a |
| 17590 | - let mut mi_r = 0usize; | 17643 | + // disjoint row range of all three planes. |
| 17591 | - while mi_r < skip_grid.mi_rows { | 17644 | + let (sy, su, sv) = ( |
| 17592 | - let mut mi_c = 0usize; | 17645 | + crate::par::Shared::new(y), |
| 17593 | - while mi_c < skip_grid.mi_cols { | 17646 | + crate::par::Shared::new(u), |
| 17594 | - if skip_grid.is_skip_txfm(mi_r, mi_c) { | 17647 | + crate::par::Shared::new(v), |
| 17595 | - CDEF_SKIPPED_UNITS.with(|c| c.set(c.get() + 1)); | 17648 | + ); |
| 17596 | - } | 17649 | + let mi_rows = skip_grid.mi_rows; |
| 17597 | - if !skip_grid.is_skip_txfm(mi_r, mi_c) { | 17650 | + let cbands: Vec<(usize, usize)> = crate::par::bands(mi_rows.div_ceil(2), crate::par::filter_threads()) |
| 17598 | - let sidx = strength_idx(mi_r, mi_c); | 17651 | + .into_iter() |
| 17599 | - let (ox, oy) = (mi_c * 4, mi_r * 4); | 17652 | + .map(|(a, b)| (a * 2, (b * 2).min(mi_rows))) |
| 17600 | - // Nothing to do for this unit at all: the direction search is | 17653 | + .collect(); |
| 17601 | - // only ever consumed through a strength, so a unit whose four | 17654 | + crate::par::run_bands(&cbands, fctx, |r_lo, r_hi, fctx| { |
| 17602 | - // strengths are zero never needs its 8x8 window gathered. | 17655 | + // SAFETY: this band writes only the plane rows its own mi rows cover, |
| 17603 | - if cdef.y_pri_strength[sidx] == 0 | 17656 | + // disjoint from every other band's (see `Shared`). |
| 17604 | - && cdef.y_sec_strength[sidx] == 0 | 17657 | + #[allow(unsafe_code)] |
| 17605 | - && cdef.uv_pri_strength[sidx] == 0 | 17658 | + let (y, u, v) = unsafe { (sy.get(), su.get(), sv.get()) }; |
| 17606 | - && cdef.uv_sec_strength[sidx] == 0 | 17659 | + // One scratch window per plane kind, reused by every unit (8x8 luma and |
| 17607 | - { | 17660 | + // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring). |
| 17608 | - mi_c += 2; | 17661 | + let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)]; |
| 17609 | - continue; | 17662 | + let mut win_uv = [0i32; (4 + 2 * CDEF_BORDER) * (4 + 2 * CDEF_BORDER)]; |
| 17610 | - } | 17663 | + let mut mi_r = r_lo; |
| 17611 | - let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height); | 17664 | + while mi_r < r_hi { |
| 17612 | - cdef_gather(&src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y); | 17665 | + let mut mi_c = 0usize; |
| 17613 | - let sample_y = |r: i32, c: i32| -> i32 { | 17666 | + while mi_c < skip_grid.mi_cols { |
| 17614 | - win_y[(r + CDEF_BORDER as i32) as usize * (8 + 2 * CDEF_BORDER) | 17667 | + if skip_grid.is_skip_txfm(mi_r, mi_c) { |
| 17615 | - + (c + CDEF_BORDER as i32) as usize] | 17668 | + CDEF_SKIPPED_UNITS.with(|c| c.set(c.get() + 1)); |
| 17616 | - }; | ||
| 17617 | - // lane-cdef r1: the direction search reads the SAME padded | ||
| 17618 | - // block the taps do -- libaom runs `cdef_find_dir` on the | ||
| 17619 | - // prepared filter block (`cdef_block.c:296-320` over | ||
| 17620 | - // `cdef_prepare_fb`'s buffer, `cdef.c:249-256`), whose columns | ||
| 17621 | - // and rows past the mi-rounded frame extent are CDEF_VERY_LARGE. | ||
| 17622 | - // Clamping to the last real sample instead made every straddling | ||
| 17623 | - // edge 8x8 (odd `mi_cols`/`mi_rows`) pick a different var, hence | ||
| 17624 | - // a different `adjust_strength`, hence a +-1 band at the crop edge. | ||
| 17625 | - let (dir, var) = cdef_find_dir(coeff_shift, |r, c| sample_y(r as i32, c as i32)); | ||
| 17626 | - | ||
| 17627 | - // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0` | ||
| 17628 | - // -- each plane zeroes the shared direction when *its own* | ||
| 17629 | - // frame-level primary strength is 0, independent of the | ||
| 17630 | - // per-block adjusted `t` and of the other plane's strength. | ||
| 17631 | - let y_pri_strength = i32::from(cdef.y_pri_strength[sidx]) << coeff_shift; | ||
| 17632 | - let y_sec_strength = i32::from(cdef.y_sec_strength[sidx]) << coeff_shift; | ||
| 17633 | - let y_dir = if y_pri_strength != 0 { dir } else { 0 }; | ||
| 17634 | - let t = cdef_adjust_strength(y_pri_strength, var); | ||
| 17635 | - // lane-cdef r1 rung: one line per filtered 8x8 luma CDEF unit. | ||
| 17636 | - if crate::envflags::env_flag!("EC_AV1_CDEF_DBG") { | ||
| 17637 | - eprintln!( | ||
| 17638 | - "EC_CDEF mi_r={mi_r} mi_c={mi_c} x={ox} y={oy} sidx={sidx} dir={dir} var={var} t={t} pri={y_pri_strength} sec={y_sec_strength} damp={damping}" | ||
| 17639 | - ); | ||
| 17640 | - } | ||
| 17641 | - let enable_primary = t != 0; | ||
| 17642 | - let enable_secondary = y_sec_strength != 0; | ||
| 17643 | - if (ox + 8 > frame_w || oy + 8 > frame_h) | ||
| 17644 | - && (enable_primary | ||
| 17645 | - || enable_secondary | ||
| 17646 | - || cdef.uv_pri_strength[sidx] != 0 | ||
| 17647 | - || cdef.uv_sec_strength[sidx] != 0) | ||
| 17648 | - { | ||
| 17649 | - CDEF_STRADDLE_UNITS.with(|c| c.set(c.get() + 1)); | ||
| 17650 | - } | ||
| 17651 | - if enable_primary || enable_secondary { | ||
| 17652 | - cdef_filter_block( | ||
| 17653 | - &win_y, | ||
| 17654 | - y, | ||
| 17655 | - ox, | ||
| 17656 | - oy, | ||
| 17657 | - 8, | ||
| 17658 | - 8, | ||
| 17659 | - t, | ||
| 17660 | - y_sec_strength, | ||
| 17661 | - y_dir, | ||
| 17662 | - damping, | ||
| 17663 | - damping, | ||
| 17664 | - enable_primary, | ||
| 17665 | - enable_secondary, | ||
| 17666 | - coeff_shift, fctx, | ||
| 17667 | - ); | ||
| 17668 | } | 17669 | } |
| 17669 | - | 17670 | + if !skip_grid.is_skip_txfm(mi_r, mi_c) { |
| 17670 | - let t_uv = i32::from(cdef.uv_pri_strength[sidx]) << coeff_shift; | 17671 | + let sidx = strength_idx(mi_r, mi_c); |
| 17671 | - let uv_sec_strength = i32::from(cdef.uv_sec_strength[sidx]) << coeff_shift; | 17672 | + let (ox, oy) = (mi_c * 4, mi_r * 4); |
| 17672 | - let uv_dir = if t_uv != 0 { dir } else { 0 }; | 17673 | + // Nothing to do for this unit at all: the direction search is |
| 17673 | - let enable_primary_uv = t_uv != 0; | 17674 | + // only ever consumed through a strength, so a unit whose four |
| 17674 | - let enable_secondary_uv = uv_sec_strength != 0; | 17675 | + // strengths are zero never needs its 8x8 window gathered. |
| 17675 | - if enable_primary_uv || enable_secondary_uv { | 17676 | + if cdef.y_pri_strength[sidx] == 0 |
| 17676 | - let (cox, coy) = (ox / 2, oy / 2); | 17677 | + && cdef.y_sec_strength[sidx] == 0 |
| 17677 | - for (plane, src_p) in [(&mut *u, &src_u), (&mut *v, &src_v)] { | 17678 | + && cdef.uv_pri_strength[sidx] == 0 |
| 17678 | - let (tw, th) = (plane.true_width, plane.true_height); | 17679 | + && cdef.uv_sec_strength[sidx] == 0 |
| 17679 | - let stride = plane.width; | 17680 | + { |
| 17680 | - cdef_gather(src_p, stride, tw, th, cox, coy, 4, 4, &mut win_uv); | 17681 | + mi_c += 2; |
| 17682 | + continue; | ||
| 17683 | + } | ||
| 17684 | + let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height); | ||
| 17685 | + cdef_gather(&src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y); | ||
| 17686 | + let sample_y = |r: i32, c: i32| -> i32 { | ||
| 17687 | + win_y[(r + CDEF_BORDER as i32) as usize * (8 + 2 * CDEF_BORDER) | ||
| 17688 | + + (c + CDEF_BORDER as i32) as usize] | ||
| 17689 | + }; | ||
| 17690 | + // lane-cdef r1: the direction search reads the SAME padded | ||
| 17691 | + // block the taps do -- libaom runs `cdef_find_dir` on the | ||
| 17692 | + // prepared filter block (`cdef_block.c:296-320` over | ||
| 17693 | + // `cdef_prepare_fb`'s buffer, `cdef.c:249-256`), whose columns | ||
| 17694 | + // and rows past the mi-rounded frame extent are CDEF_VERY_LARGE. | ||
| 17695 | + // Clamping to the last real sample instead made every straddling | ||
| 17696 | + // edge 8x8 (odd `mi_cols`/`mi_rows`) pick a different var, hence | ||
| 17697 | + // a different `adjust_strength`, hence a +-1 band at the crop edge. | ||
| 17698 | + let (dir, var) = cdef_find_dir(coeff_shift, |r, c| sample_y(r as i32, c as i32)); | ||
| 17699 | + | ||
| 17700 | + // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0` | ||
| 17701 | + // -- each plane zeroes the shared direction when *its own* | ||
| 17702 | + // frame-level primary strength is 0, independent of the | ||
| 17703 | + // per-block adjusted `t` and of the other plane's strength. | ||
| 17704 | + let y_pri_strength = i32::from(cdef.y_pri_strength[sidx]) << coeff_shift; | ||
| 17705 | + let y_sec_strength = i32::from(cdef.y_sec_strength[sidx]) << coeff_shift; | ||
| 17706 | + let y_dir = if y_pri_strength != 0 { dir } else { 0 }; | ||
| 17707 | + let t = cdef_adjust_strength(y_pri_strength, var); | ||
| 17708 | + // lane-cdef r1 rung: one line per filtered 8x8 luma CDEF unit. | ||
| 17709 | + if crate::envflags::env_flag!("EC_AV1_CDEF_DBG") { | ||
| 17710 | + eprintln!( | ||
| 17711 | + "EC_CDEF mi_r={mi_r} mi_c={mi_c} x={ox} y={oy} sidx={sidx} dir={dir} var={var} t={t} pri={y_pri_strength} sec={y_sec_strength} damp={damping}" | ||
| 17712 | + ); | ||
| 17713 | + } | ||
| 17714 | + let enable_primary = t != 0; | ||
| 17715 | + let enable_secondary = y_sec_strength != 0; | ||
| 17716 | + if (ox + 8 > frame_w || oy + 8 > frame_h) | ||
| 17717 | + && (enable_primary | ||
| 17718 | + || enable_secondary | ||
| 17719 | + || cdef.uv_pri_strength[sidx] != 0 | ||
| 17720 | + || cdef.uv_sec_strength[sidx] != 0) | ||
| 17721 | + { | ||
| 17722 | + CDEF_STRADDLE_UNITS.with(|c| c.set(c.get() + 1)); | ||
| 17723 | + } | ||
| 17724 | + if enable_primary || enable_secondary { | ||
| 17681 | cdef_filter_block( | 17725 | cdef_filter_block( |
| 17682 | - &win_uv, | 17726 | + &win_y, |
| 17683 | - plane, | 17727 | + y, |
| 17684 | - cox, | 17728 | + ox, |
| 17685 | - coy, | 17729 | + oy, |
| 17686 | - 4, | 17730 | + 8, |
| 17687 | - 4, | 17731 | + 8, |
| 17688 | - t_uv, | 17732 | + t, |
| 17689 | - uv_sec_strength, | 17733 | + y_sec_strength, |
| 17690 | - uv_dir, | 17734 | + y_dir, |
| 17691 | - damping_uv, | 17735 | + damping, |
| 17692 | - damping_uv, | 17736 | + damping, |
| 17693 | - enable_primary_uv, | 17737 | + enable_primary, |
| 17694 | - enable_secondary_uv, | 17738 | + enable_secondary, |
| 17695 | coeff_shift, fctx, | 17739 | coeff_shift, fctx, |
| 17696 | ); | 17740 | ); |
| 17697 | } | 17741 | } |
| 17742 | + | ||
| 17743 | + let t_uv = i32::from(cdef.uv_pri_strength[sidx]) << coeff_shift; | ||
| 17744 | + let uv_sec_strength = i32::from(cdef.uv_sec_strength[sidx]) << coeff_shift; | ||
| 17745 | + let uv_dir = if t_uv != 0 { dir } else { 0 }; | ||
| 17746 | + let enable_primary_uv = t_uv != 0; | ||
| 17747 | + let enable_secondary_uv = uv_sec_strength != 0; | ||
| 17748 | + if enable_primary_uv || enable_secondary_uv { | ||
| 17749 | + let (cox, coy) = (ox / 2, oy / 2); | ||
| 17750 | + for (plane, src_p) in [(&mut *u, &src_u), (&mut *v, &src_v)] { | ||
| 17751 | + let (tw, th) = (plane.true_width, plane.true_height); | ||
| 17752 | + let stride = plane.width; | ||
| 17753 | + cdef_gather(src_p, stride, tw, th, cox, coy, 4, 4, &mut win_uv); | ||
| 17754 | + cdef_filter_block( | ||
| 17755 | + &win_uv, | ||
| 17756 | + plane, | ||
| 17757 | + cox, | ||
| 17758 | + coy, | ||
| 17759 | + 4, | ||
| 17760 | + 4, | ||
| 17761 | + t_uv, | ||
| 17762 | + uv_sec_strength, | ||
| 17763 | + uv_dir, | ||
| 17764 | + damping_uv, | ||
| 17765 | + damping_uv, | ||
| 17766 | + enable_primary_uv, | ||
| 17767 | + enable_secondary_uv, | ||
| 17768 | + coeff_shift, fctx, | ||
| 17769 | + ); | ||
| 17770 | + } | ||
| 17771 | + } | ||
| 17698 | } | 17772 | } |
| 17773 | + mi_c += 2; | ||
| 17699 | } | 17774 | } |
| 17700 | - mi_c += 2; | ||
| 17701 | - } | ||
| 17702 | mi_r += 2; | 17775 | mi_r += 2; |
| 17703 | - } | 17776 | + } |
| 17777 | + }); | ||
| 17704 | scratch_return(src_y); | 17778 | scratch_return(src_y); |
| 17705 | scratch_return(src_u); | 17779 | scratch_return(src_u); |
| 17706 | scratch_return(src_v); | 17780 | scratch_return(src_v); |
crates/ec-av1/src/film_grain.rsmodified+318 -286
| @@ -880,342 +880,374 @@ pub(crate) fn apply_grain( | |||
| 880 | 880 | ||
| 881 | let overlap = fg.overlap_flag; | 881 | let overlap = fg.overlap_flag; |
| 882 | 882 | ||
| 883 | - let mut y_line_buf = vec![0i32; (y_stride * 2).max(1) as usize]; | 883 | + // lane-filt1: 32-pixel block ROWS are the parallel axis. A row's noise |
| 884 | - let mut cb_line_buf = vec![0i32; c_stride.max(1) as usize]; | 884 | + // offsets come from `Rng::line(y * 2, grain_seed)` -- a function of the |
| 885 | - let mut cr_line_buf = vec![0i32; c_stride.max(1) as usize]; | 885 | + // row and the frame seed alone -- and a row writes only luma rows |
| 886 | - let mut y_col_buf = vec![0i32; ((LUMA_SUBBLOCK + 2) * 2) as usize]; | 886 | + // `y*2 .. y*2+31` (chroma `y .. y+15`), so rows are write-disjoint. The |
| 887 | - let mut cb_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; | 887 | + // one carry ACROSS rows is `*_line_buf` (the row above's bottom lines, |
| 888 | - let mut cr_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; | 888 | + // blended into this row's top lines when `overlap_flag`), and its final |
| 889 | + // content is written by that row's own blocks out of the grain templates | ||
| 890 | + // and `*_col_buf` -- never out of the row above it. So a band replays | ||
| 891 | + // exactly ONE preceding row with its pixel writes suppressed (`prelude`) | ||
| 892 | + // to rebuild the line buffers, then codes its own rows exactly as the | ||
| 893 | + // single-threaded walk does. | ||
| 894 | + let row_step = LUMA_SUBBLOCK >> 1; | ||
| 895 | + let nrows = ((height / 2).max(0) as usize).div_ceil(row_step as usize); | ||
| 896 | + let so = crate::par::Shared::new(&mut out); | ||
| 897 | + let gbands = crate::par::bands(nrows, crate::par::filter_threads()); | ||
| 898 | + crate::par::run_bands(&gbands, fctx, |b_lo, b_hi, fctx| { | ||
| 899 | + // SAFETY: this band writes only the picture rows its own block rows | ||
| 900 | + // cover, disjoint from every other band's. | ||
| 901 | + #[allow(unsafe_code)] | ||
| 902 | + let out = unsafe { so.get() }; | ||
| 903 | + let y_lo = (b_lo * row_step as usize) as i32; | ||
| 904 | + let y_hi = ((b_hi * row_step as usize) as i32).min(height / 2); | ||
| 905 | + let y_first = if b_lo > 0 { y_lo - row_step } else { y_lo }; | ||
| 906 | + let mut y_line_buf = vec![0i32; (y_stride * 2).max(1) as usize]; | ||
| 907 | + let mut cb_line_buf = vec![0i32; c_stride.max(1) as usize]; | ||
| 908 | + let mut cr_line_buf = vec![0i32; c_stride.max(1) as usize]; | ||
| 909 | + let mut y_col_buf = vec![0i32; ((LUMA_SUBBLOCK + 2) * 2) as usize]; | ||
| 910 | + let mut cb_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; | ||
| 911 | + let mut cr_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; | ||
| 889 | 912 | ||
| 890 | - let mut y = 0; | 913 | + let mut y = y_first; |
| 891 | - while y < height / 2 { | 914 | + while y < y_hi { |
| 892 | - let mut row_rng = Rng::line(y * 2, fg.grain_seed); | 915 | + let prelude = y < y_lo; |
| 893 | - let mut x = 0; | 916 | + let mut row_rng = Rng::line(y * 2, fg.grain_seed); |
| 894 | - while x < width / 2 { | 917 | + let mut x = 0; |
| 895 | - let raw = row_rng.get(8); | 918 | + while x < width / 2 { |
| 896 | - let offset_x = (raw >> 4) & 15; | 919 | + let raw = row_rng.get(8); |
| 897 | - let offset_y = raw & 15; | 920 | + let offset_x = (raw >> 4) & 15; |
| 921 | + let offset_y = raw & 15; | ||
| 898 | 922 | ||
| 899 | - let luma_off_y = LEFT_PAD + 2 * AR_PADDING + (offset_y << 1); | 923 | + let luma_off_y = LEFT_PAD + 2 * AR_PADDING + (offset_y << 1); |
| 900 | - let luma_off_x = TOP_PAD + 2 * AR_PADDING + (offset_x << 1); | 924 | + let luma_off_x = TOP_PAD + 2 * AR_PADDING + (offset_x << 1); |
| 901 | - let chroma_off_y = TOP_PAD + AR_PADDING + offset_y; | 925 | + let chroma_off_y = TOP_PAD + AR_PADDING + offset_y; |
| 902 | - let chroma_off_x = LEFT_PAD + AR_PADDING + offset_x; | 926 | + let chroma_off_x = LEFT_PAD + AR_PADDING + offset_x; |
| 903 | 927 | ||
| 904 | - if overlap && x != 0 { | 928 | + if overlap && x != 0 { |
| 905 | - let h_count = (LUMA_SUBBLOCK + 2).min(height - (y << 1)); | 929 | + let h_count = (LUMA_SUBBLOCK + 2).min(height - (y << 1)); |
| 906 | - ver_overlap_inplace( | 930 | + ver_overlap_inplace( |
| 907 | - &mut y_col_buf, | 931 | + &mut y_col_buf, |
| 908 | - 2, | 932 | + 2, |
| 909 | - 0, | 933 | + 0, |
| 910 | - 0, | 934 | + 0, |
| 911 | - &luma_template, | 935 | + &luma_template, |
| 912 | - LUMA_BLOCK_W, | 936 | + LUMA_BLOCK_W, |
| 913 | - luma_off_y, | 937 | + luma_off_y, |
| 914 | - luma_off_x, | 938 | + luma_off_x, |
| 915 | - 2, | 939 | + 2, |
| 916 | - h_count, fctx, | 940 | + h_count, fctx, |
| 917 | - ); | 941 | + ); |
| 918 | - let hc_count = (CHROMA_SUBBLOCK + 1).min((height - (y << 1)) >> 1); | 942 | + let hc_count = (CHROMA_SUBBLOCK + 1).min((height - (y << 1)) >> 1); |
| 919 | - ver_overlap_inplace( | 943 | + ver_overlap_inplace( |
| 920 | - &mut cb_col_buf, | 944 | + &mut cb_col_buf, |
| 921 | - 1, | 945 | + 1, |
| 922 | - 0, | 946 | + 0, |
| 923 | - 0, | 947 | + 0, |
| 924 | - &cb_template, | 948 | + &cb_template, |
| 925 | - CHROMA_BLOCK_W, | 949 | + CHROMA_BLOCK_W, |
| 926 | - chroma_off_y, | 950 | + chroma_off_y, |
| 927 | - chroma_off_x, | 951 | + chroma_off_x, |
| 928 | - 1, | 952 | + 1, |
| 929 | - hc_count, fctx, | 953 | + hc_count, fctx, |
| 930 | - ); | 954 | + ); |
| 931 | - ver_overlap_inplace( | 955 | + ver_overlap_inplace( |
| 932 | - &mut cr_col_buf, | 956 | + &mut cr_col_buf, |
| 933 | - 1, | 957 | + 1, |
| 934 | - 0, | 958 | + 0, |
| 935 | - 0, | 959 | + 0, |
| 936 | - &cr_template, | 960 | + &cr_template, |
| 937 | - CHROMA_BLOCK_W, | 961 | + CHROMA_BLOCK_W, |
| 938 | - chroma_off_y, | 962 | + chroma_off_y, |
| 939 | - chroma_off_x, | 963 | + chroma_off_x, |
| 940 | - 1, | 964 | + 1, |
| 941 | - hc_count, fctx, | 965 | + hc_count, fctx, |
| 942 | - ); | 966 | + ); |
| 943 | 967 | ||
| 944 | - let i = if y != 0 { 1 } else { 0 }; | 968 | + let i = if y != 0 { 1 } else { 0 }; |
| 945 | - add_noise_to_block( | 969 | + if !prelude { |
| 946 | - fg, | 970 | + add_noise_to_block( |
| 947 | - mc_identity, | 971 | + fg, |
| 948 | - &scaling_y, | 972 | + mc_identity, |
| 949 | - &scaling_cb, | 973 | + &scaling_y, |
| 950 | - &scaling_cr, | 974 | + &scaling_cb, |
| 951 | - &mut out, | 975 | + &scaling_cr, |
| 952 | - (y + i) << 1, | 976 | + &mut *out, |
| 953 | - x << 1, | 977 | + (y + i) << 1, |
| 954 | - &y_col_buf, | 978 | + x << 1, |
| 955 | - 2, | 979 | + &y_col_buf, |
| 956 | - i * 2, | 980 | + 2, |
| 957 | - 0, | 981 | + i * 2, |
| 958 | - &cb_col_buf, | 982 | + 0, |
| 959 | - &cr_col_buf, | 983 | + &cb_col_buf, |
| 960 | - 1, | 984 | + &cr_col_buf, |
| 961 | - i, | 985 | + 1, |
| 962 | - 0, | 986 | + i, |
| 963 | - (LUMA_SUBBLOCK >> 1).min(height / 2 - y) - i, | 987 | + 0, |
| 964 | - 1, fctx, | 988 | + (LUMA_SUBBLOCK >> 1).min(height / 2 - y) - i, |
| 965 | - ); | 989 | + 1, fctx, |
| 966 | - } | 990 | + ); |
| 991 | + } | ||
| 992 | + } | ||
| 967 | 993 | ||
| 968 | - if overlap && y != 0 { | 994 | + if overlap && y != 0 { |
| 969 | - if x != 0 { | 995 | + if x != 0 { |
| 996 | + hor_overlap_inplace( | ||
| 997 | + &mut y_line_buf, | ||
| 998 | + y_stride, | ||
| 999 | + 0, | ||
| 1000 | + x << 1, | ||
| 1001 | + &y_col_buf, | ||
| 1002 | + 2, | ||
| 1003 | + 0, | ||
| 1004 | + 0, | ||
| 1005 | + 2, | ||
| 1006 | + 2, fctx, | ||
| 1007 | + ); | ||
| 1008 | + hor_overlap_inplace( | ||
| 1009 | + &mut cb_line_buf, | ||
| 1010 | + c_stride, | ||
| 1011 | + 0, | ||
| 1012 | + x, | ||
| 1013 | + &cb_col_buf, | ||
| 1014 | + 1, | ||
| 1015 | + 0, | ||
| 1016 | + 0, | ||
| 1017 | + 1, | ||
| 1018 | + 1, fctx, | ||
| 1019 | + ); | ||
| 1020 | + hor_overlap_inplace( | ||
| 1021 | + &mut cr_line_buf, | ||
| 1022 | + c_stride, | ||
| 1023 | + 0, | ||
| 1024 | + x, | ||
| 1025 | + &cr_col_buf, | ||
| 1026 | + 1, | ||
| 1027 | + 0, | ||
| 1028 | + 0, | ||
| 1029 | + 1, | ||
| 1030 | + 1, fctx, | ||
| 1031 | + ); | ||
| 1032 | + } | ||
| 1033 | + let lcol0 = if x != 0 { (x + 1) << 1 } else { 0 }; | ||
| 1034 | + let ccol0 = if x != 0 { x + 1 } else { 0 }; | ||
| 1035 | + let lw = (LUMA_SUBBLOCK - (if x != 0 { 2 } else { 0 })).min(width - lcol0); | ||
| 970 | hor_overlap_inplace( | 1036 | hor_overlap_inplace( |
| 971 | &mut y_line_buf, | 1037 | &mut y_line_buf, |
| 972 | y_stride, | 1038 | y_stride, |
| 973 | 0, | 1039 | 0, |
| 974 | - x << 1, | 1040 | + lcol0, |
| 975 | - &y_col_buf, | 1041 | + &luma_template, |
| 976 | - 2, | 1042 | + LUMA_BLOCK_W, |
| 977 | - 0, | 1043 | + luma_off_y, |
| 978 | - 0, | 1044 | + luma_off_x + if x != 0 { 2 } else { 0 }, |
| 979 | - 2, | 1045 | + lw, |
| 980 | 2, fctx, | 1046 | 2, fctx, |
| 981 | ); | 1047 | ); |
| 1048 | + let cw = (CHROMA_SUBBLOCK - (if x != 0 { 1 } else { 0 })).min((width - lcol0) >> 1); | ||
| 982 | hor_overlap_inplace( | 1049 | hor_overlap_inplace( |
| 983 | &mut cb_line_buf, | 1050 | &mut cb_line_buf, |
| 984 | c_stride, | 1051 | c_stride, |
| 985 | 0, | 1052 | 0, |
| 986 | - x, | 1053 | + ccol0, |
| 987 | - &cb_col_buf, | 1054 | + &cb_template, |
| 988 | - 1, | 1055 | + CHROMA_BLOCK_W, |
| 989 | - 0, | 1056 | + chroma_off_y, |
| 990 | - 0, | 1057 | + chroma_off_x + if x != 0 { 1 } else { 0 }, |
| 991 | - 1, | 1058 | + cw, |
| 992 | 1, fctx, | 1059 | 1, fctx, |
| 993 | ); | 1060 | ); |
| 994 | hor_overlap_inplace( | 1061 | hor_overlap_inplace( |
| 995 | &mut cr_line_buf, | 1062 | &mut cr_line_buf, |
| 996 | c_stride, | 1063 | c_stride, |
| 997 | 0, | 1064 | 0, |
| 998 | - x, | 1065 | + ccol0, |
| 999 | - &cr_col_buf, | 1066 | + &cr_template, |
| 1000 | - 1, | 1067 | + CHROMA_BLOCK_W, |
| 1001 | - 0, | 1068 | + chroma_off_y, |
| 1002 | - 0, | 1069 | + chroma_off_x + if x != 0 { 1 } else { 0 }, |
| 1003 | - 1, | 1070 | + cw, |
| 1004 | 1, fctx, | 1071 | 1, fctx, |
| 1005 | ); | 1072 | ); |
| 1006 | - } | ||
| 1007 | - let lcol0 = if x != 0 { (x + 1) << 1 } else { 0 }; | ||
| 1008 | - let ccol0 = if x != 0 { x + 1 } else { 0 }; | ||
| 1009 | - let lw = (LUMA_SUBBLOCK - (if x != 0 { 2 } else { 0 })).min(width - lcol0); | ||
| 1010 | - hor_overlap_inplace( | ||
| 1011 | - &mut y_line_buf, | ||
| 1012 | - y_stride, | ||
| 1013 | - 0, | ||
| 1014 | - lcol0, | ||
| 1015 | - &luma_template, | ||
| 1016 | - LUMA_BLOCK_W, | ||
| 1017 | - luma_off_y, | ||
| 1018 | - luma_off_x + if x != 0 { 2 } else { 0 }, | ||
| 1019 | - lw, | ||
| 1020 | - 2, fctx, | ||
| 1021 | - ); | ||
| 1022 | - let cw = (CHROMA_SUBBLOCK - (if x != 0 { 1 } else { 0 })).min((width - lcol0) >> 1); | ||
| 1023 | - hor_overlap_inplace( | ||
| 1024 | - &mut cb_line_buf, | ||
| 1025 | - c_stride, | ||
| 1026 | - 0, | ||
| 1027 | - ccol0, | ||
| 1028 | - &cb_template, | ||
| 1029 | - CHROMA_BLOCK_W, | ||
| 1030 | - chroma_off_y, | ||
| 1031 | - chroma_off_x + if x != 0 { 1 } else { 0 }, | ||
| 1032 | - cw, | ||
| 1033 | - 1, fctx, | ||
| 1034 | - ); | ||
| 1035 | - hor_overlap_inplace( | ||
| 1036 | - &mut cr_line_buf, | ||
| 1037 | - c_stride, | ||
| 1038 | - 0, | ||
| 1039 | - ccol0, | ||
| 1040 | - &cr_template, | ||
| 1041 | - CHROMA_BLOCK_W, | ||
| 1042 | - chroma_off_y, | ||
| 1043 | - chroma_off_x + if x != 0 { 1 } else { 0 }, | ||
| 1044 | - cw, | ||
| 1045 | - 1, fctx, | ||
| 1046 | - ); | ||
| 1047 | 1073 | ||
| 1048 | - add_noise_to_block( | 1074 | + if !prelude { |
| 1049 | - fg, | 1075 | + add_noise_to_block( |
| 1050 | - mc_identity, | 1076 | + fg, |
| 1051 | - &scaling_y, | 1077 | + mc_identity, |
| 1052 | - &scaling_cb, | 1078 | + &scaling_y, |
| 1053 | - &scaling_cr, | 1079 | + &scaling_cb, |
| 1054 | - &mut out, | 1080 | + &scaling_cr, |
| 1055 | - y << 1, | 1081 | + &mut *out, |
| 1056 | - x << 1, | 1082 | + y << 1, |
| 1057 | - &y_line_buf, | 1083 | + x << 1, |
| 1058 | - y_stride, | 1084 | + &y_line_buf, |
| 1059 | - 0, | 1085 | + y_stride, |
| 1060 | - x << 1, | 1086 | + 0, |
| 1061 | - &cb_line_buf, | 1087 | + x << 1, |
| 1062 | - &cr_line_buf, | 1088 | + &cb_line_buf, |
| 1063 | - c_stride, | 1089 | + &cr_line_buf, |
| 1064 | - 0, | 1090 | + c_stride, |
| 1065 | - x, | 1091 | + 0, |
| 1066 | - 1, | 1092 | + x, |
| 1067 | - (LUMA_SUBBLOCK >> 1).min(width / 2 - x), fctx, | 1093 | + 1, |
| 1068 | - ); | 1094 | + (LUMA_SUBBLOCK >> 1).min(width / 2 - x), fctx, |
| 1069 | - } | 1095 | + ); |
| 1096 | + } | ||
| 1097 | + } | ||
| 1070 | 1098 | ||
| 1071 | - let i = if overlap && y != 0 { 1 } else { 0 }; | 1099 | + let i = if overlap && y != 0 { 1 } else { 0 }; |
| 1072 | - let j = if overlap && x != 0 { 1 } else { 0 }; | 1100 | + let j = if overlap && x != 0 { 1 } else { 0 }; |
| 1073 | - add_noise_to_block( | 1101 | + if !prelude { |
| 1074 | - fg, | 1102 | + add_noise_to_block( |
| 1075 | - mc_identity, | 1103 | + fg, |
| 1076 | - &scaling_y, | 1104 | + mc_identity, |
| 1077 | - &scaling_cb, | 1105 | + &scaling_y, |
| 1078 | - &scaling_cr, | 1106 | + &scaling_cb, |
| 1079 | - &mut out, | 1107 | + &scaling_cr, |
| 1080 | - (y + i) << 1, | 1108 | + &mut *out, |
| 1081 | - (x + j) << 1, | 1109 | + (y + i) << 1, |
| 1082 | - &luma_template, | 1110 | + (x + j) << 1, |
| 1083 | - LUMA_BLOCK_W, | 1111 | + &luma_template, |
| 1084 | - luma_off_y + (i << 1), | 1112 | + LUMA_BLOCK_W, |
| 1085 | - luma_off_x + (j << 1), | 1113 | + luma_off_y + (i << 1), |
| 1086 | - &cb_template, | 1114 | + luma_off_x + (j << 1), |
| 1087 | - &cr_template, | 1115 | + &cb_template, |
| 1088 | - CHROMA_BLOCK_W, | 1116 | + &cr_template, |
| 1089 | - chroma_off_y + i, | 1117 | + CHROMA_BLOCK_W, |
| 1090 | - chroma_off_x + j, | 1118 | + chroma_off_y + i, |
| 1091 | - (LUMA_SUBBLOCK >> 1).min(height / 2 - y) - i, | 1119 | + chroma_off_x + j, |
| 1092 | - (LUMA_SUBBLOCK >> 1).min(width / 2 - x) - j, fctx, | 1120 | + (LUMA_SUBBLOCK >> 1).min(height / 2 - y) - i, |
| 1093 | - ); | 1121 | + (LUMA_SUBBLOCK >> 1).min(width / 2 - x) - j, fctx, |
| 1122 | + ); | ||
| 1123 | + } | ||
| 1094 | 1124 | ||
| 1095 | - if overlap { | 1125 | + if overlap { |
| 1096 | - if x != 0 { | 1126 | + if x != 0 { |
| 1127 | + copy_area( | ||
| 1128 | + &mut y_line_buf, | ||
| 1129 | + y_stride, | ||
| 1130 | + 0, | ||
| 1131 | + x << 1, | ||
| 1132 | + &y_col_buf, | ||
| 1133 | + 2, | ||
| 1134 | + LUMA_SUBBLOCK, | ||
| 1135 | + 0, | ||
| 1136 | + 2, | ||
| 1137 | + 2, fctx, | ||
| 1138 | + ); | ||
| 1139 | + copy_area( | ||
| 1140 | + &mut cb_line_buf, | ||
| 1141 | + c_stride, | ||
| 1142 | + 0, | ||
| 1143 | + x, | ||
| 1144 | + &cb_col_buf, | ||
| 1145 | + 1, | ||
| 1146 | + CHROMA_SUBBLOCK, | ||
| 1147 | + 0, | ||
| 1148 | + 1, | ||
| 1149 | + 1, fctx, | ||
| 1150 | + ); | ||
| 1151 | + copy_area( | ||
| 1152 | + &mut cr_line_buf, | ||
| 1153 | + c_stride, | ||
| 1154 | + 0, | ||
| 1155 | + x, | ||
| 1156 | + &cr_col_buf, | ||
| 1157 | + 1, | ||
| 1158 | + CHROMA_SUBBLOCK, | ||
| 1159 | + 0, | ||
| 1160 | + 1, | ||
| 1161 | + 1, fctx, | ||
| 1162 | + ); | ||
| 1163 | + } | ||
| 1164 | + let lcol0 = if x != 0 { (x + 1) << 1 } else { 0 }; | ||
| 1165 | + let ccol0 = if x != 0 { x + 1 } else { 0 }; | ||
| 1166 | + let lcut = if x != 0 { 2 } else { 0 }; | ||
| 1167 | + let ccut = if x != 0 { 1 } else { 0 }; | ||
| 1097 | copy_area( | 1168 | copy_area( |
| 1098 | &mut y_line_buf, | 1169 | &mut y_line_buf, |
| 1099 | y_stride, | 1170 | y_stride, |
| 1100 | 0, | 1171 | 0, |
| 1101 | - x << 1, | 1172 | + lcol0, |
| 1102 | - &y_col_buf, | 1173 | + &luma_template, |
| 1103 | - 2, | 1174 | + LUMA_BLOCK_W, |
| 1104 | - LUMA_SUBBLOCK, | 1175 | + luma_off_y + LUMA_SUBBLOCK, |
| 1105 | - 0, | 1176 | + luma_off_x + lcut, |
| 1106 | - 2, | 1177 | + LUMA_SUBBLOCK.min(width - (x << 1)) - lcut, |
| 1107 | 2, fctx, | 1178 | 2, fctx, |
| 1108 | ); | 1179 | ); |
| 1109 | copy_area( | 1180 | copy_area( |
| 1110 | &mut cb_line_buf, | 1181 | &mut cb_line_buf, |
| 1111 | c_stride, | 1182 | c_stride, |
| 1112 | 0, | 1183 | 0, |
| 1113 | - x, | 1184 | + ccol0, |
| 1114 | - &cb_col_buf, | 1185 | + &cb_template, |
| 1115 | - 1, | 1186 | + CHROMA_BLOCK_W, |
| 1116 | - CHROMA_SUBBLOCK, | 1187 | + chroma_off_y + CHROMA_SUBBLOCK, |
| 1117 | - 0, | 1188 | + chroma_off_x + ccut, |
| 1118 | - 1, | 1189 | + CHROMA_SUBBLOCK.min((width - (x << 1)) >> 1) - ccut, |
| 1119 | 1, fctx, | 1190 | 1, fctx, |
| 1120 | ); | 1191 | ); |
| 1121 | copy_area( | 1192 | copy_area( |
| 1122 | &mut cr_line_buf, | 1193 | &mut cr_line_buf, |
| 1123 | c_stride, | 1194 | c_stride, |
| 1124 | 0, | 1195 | 0, |
| 1125 | - x, | 1196 | + ccol0, |
| 1126 | - &cr_col_buf, | 1197 | + &cr_template, |
| 1198 | + CHROMA_BLOCK_W, | ||
| 1199 | + chroma_off_y + CHROMA_SUBBLOCK, | ||
| 1200 | + chroma_off_x + ccut, | ||
| 1201 | + CHROMA_SUBBLOCK.min((width - (x << 1)) >> 1) - ccut, | ||
| 1202 | + 1, fctx, | ||
| 1203 | + ); | ||
| 1204 | + | ||
| 1205 | + let h_count = (LUMA_SUBBLOCK + 2).min(height - (y << 1)); | ||
| 1206 | + copy_area( | ||
| 1207 | + &mut y_col_buf, | ||
| 1208 | + 2, | ||
| 1209 | + 0, | ||
| 1210 | + 0, | ||
| 1211 | + &luma_template, | ||
| 1212 | + LUMA_BLOCK_W, | ||
| 1213 | + luma_off_y, | ||
| 1214 | + luma_off_x + LUMA_SUBBLOCK, | ||
| 1215 | + 2, | ||
| 1216 | + h_count, fctx, | ||
| 1217 | + ); | ||
| 1218 | + let hc_count = (CHROMA_SUBBLOCK + 1).min((height - (y << 1)) >> 1); | ||
| 1219 | + copy_area( | ||
| 1220 | + &mut cb_col_buf, | ||
| 1127 | 1, | 1221 | 1, |
| 1128 | - CHROMA_SUBBLOCK, | ||
| 1129 | 0, | 1222 | 0, |
| 1223 | + 0, | ||
| 1224 | + &cb_template, | ||
| 1225 | + CHROMA_BLOCK_W, | ||
| 1226 | + chroma_off_y, | ||
| 1227 | + chroma_off_x + CHROMA_SUBBLOCK, | ||
| 1130 | 1, | 1228 | 1, |
| 1131 | - 1, fctx, | 1229 | + hc_count, fctx, |
| 1230 | + ); | ||
| 1231 | + copy_area( | ||
| 1232 | + &mut cr_col_buf, | ||
| 1233 | + 1, | ||
| 1234 | + 0, | ||
| 1235 | + 0, | ||
| 1236 | + &cr_template, | ||
| 1237 | + CHROMA_BLOCK_W, | ||
| 1238 | + chroma_off_y, | ||
| 1239 | + chroma_off_x + CHROMA_SUBBLOCK, | ||
| 1240 | + 1, | ||
| 1241 | + hc_count, fctx, | ||
| 1132 | ); | 1242 | ); |
| 1133 | } | 1243 | } |
| 1134 | - let lcol0 = if x != 0 { (x + 1) << 1 } else { 0 }; | ||
| 1135 | - let ccol0 = if x != 0 { x + 1 } else { 0 }; | ||
| 1136 | - let lcut = if x != 0 { 2 } else { 0 }; | ||
| 1137 | - let ccut = if x != 0 { 1 } else { 0 }; | ||
| 1138 | - copy_area( | ||
| 1139 | - &mut y_line_buf, | ||
| 1140 | - y_stride, | ||
| 1141 | - 0, | ||
| 1142 | - lcol0, | ||
| 1143 | - &luma_template, | ||
| 1144 | - LUMA_BLOCK_W, | ||
| 1145 | - luma_off_y + LUMA_SUBBLOCK, | ||
| 1146 | - luma_off_x + lcut, | ||
| 1147 | - LUMA_SUBBLOCK.min(width - (x << 1)) - lcut, | ||
| 1148 | - 2, fctx, | ||
| 1149 | - ); | ||
| 1150 | - copy_area( | ||
| 1151 | - &mut cb_line_buf, | ||
| 1152 | - c_stride, | ||
| 1153 | - 0, | ||
| 1154 | - ccol0, | ||
| 1155 | - &cb_template, | ||
| 1156 | - CHROMA_BLOCK_W, | ||
| 1157 | - chroma_off_y + CHROMA_SUBBLOCK, | ||
| 1158 | - chroma_off_x + ccut, | ||
| 1159 | - CHROMA_SUBBLOCK.min((width - (x << 1)) >> 1) - ccut, | ||
| 1160 | - 1, fctx, | ||
| 1161 | - ); | ||
| 1162 | - copy_area( | ||
| 1163 | - &mut cr_line_buf, | ||
| 1164 | - c_stride, | ||
| 1165 | - 0, | ||
| 1166 | - ccol0, | ||
| 1167 | - &cr_template, | ||
| 1168 | - CHROMA_BLOCK_W, | ||
| 1169 | - chroma_off_y + CHROMA_SUBBLOCK, | ||
| 1170 | - chroma_off_x + ccut, | ||
| 1171 | - CHROMA_SUBBLOCK.min((width - (x << 1)) >> 1) - ccut, | ||
| 1172 | - 1, fctx, | ||
| 1173 | - ); | ||
| 1174 | 1244 | ||
| 1175 | - let h_count = (LUMA_SUBBLOCK + 2).min(height - (y << 1)); | 1245 | + x += LUMA_SUBBLOCK >> 1; |
| 1176 | - copy_area( | ||
| 1177 | - &mut y_col_buf, | ||
| 1178 | - 2, | ||
| 1179 | - 0, | ||
| 1180 | - 0, | ||
| 1181 | - &luma_template, | ||
| 1182 | - LUMA_BLOCK_W, | ||
| 1183 | - luma_off_y, | ||
| 1184 | - luma_off_x + LUMA_SUBBLOCK, | ||
| 1185 | - 2, | ||
| 1186 | - h_count, fctx, | ||
| 1187 | - ); | ||
| 1188 | - let hc_count = (CHROMA_SUBBLOCK + 1).min((height - (y << 1)) >> 1); | ||
| 1189 | - copy_area( | ||
| 1190 | - &mut cb_col_buf, | ||
| 1191 | - 1, | ||
| 1192 | - 0, | ||
| 1193 | - 0, | ||
| 1194 | - &cb_template, | ||
| 1195 | - CHROMA_BLOCK_W, | ||
| 1196 | - chroma_off_y, | ||
| 1197 | - chroma_off_x + CHROMA_SUBBLOCK, | ||
| 1198 | - 1, | ||
| 1199 | - hc_count, fctx, | ||
| 1200 | - ); | ||
| 1201 | - copy_area( | ||
| 1202 | - &mut cr_col_buf, | ||
| 1203 | - 1, | ||
| 1204 | - 0, | ||
| 1205 | - 0, | ||
| 1206 | - &cr_template, | ||
| 1207 | - CHROMA_BLOCK_W, | ||
| 1208 | - chroma_off_y, | ||
| 1209 | - chroma_off_x + CHROMA_SUBBLOCK, | ||
| 1210 | - 1, | ||
| 1211 | - hc_count, fctx, | ||
| 1212 | - ); | ||
| 1213 | } | 1246 | } |
| 1214 | - | 1247 | + y += row_step; |
| 1215 | - x += LUMA_SUBBLOCK >> 1; | ||
| 1216 | } | 1248 | } |
| 1217 | - y += LUMA_SUBBLOCK >> 1; | 1249 | + }); |
| 1218 | - } | 1250 | + drop(so); |
| 1219 | 1251 | ||
| 1220 | out | 1252 | out |
| 1221 | } | 1253 | } |
crates/ec-av1/src/lib.rsmodified+1 -0
| @@ -41,6 +41,7 @@ mod motion_field; | |||
| 41 | pub mod msac; | 41 | pub mod msac; |
| 42 | pub mod mvstack; | 42 | pub mod mvstack; |
| 43 | pub mod obu; | 43 | pub mod obu; |
| 44 | +mod par; | ||
| 44 | pub mod quant; | 45 | pub mod quant; |
| 45 | mod refusal_inventory; | 46 | mod refusal_inventory; |
| 46 | mod restoration; | 47 | mod restoration; |
crates/ec-av1/src/par.rsadded+135 -0
| @@ -0,0 +1,135 @@ | |||
| 1 | +//! lane-filt1: in-frame parallelism for the post-decode filter chain | ||
| 2 | +//! (deblock, CDEF, loop restoration, film grain). | ||
| 3 | +//! | ||
| 4 | +//! Every stage here is split along an axis whose per-band writes are | ||
| 5 | +//! *disjoint* and whose reads come either from an immutable snapshot or from | ||
| 6 | +//! the band's own pixels, so N bands reproduce the single-threaded output | ||
| 7 | +//! byte for byte -- gated by | ||
| 8 | +//! `a_real_stream_filters_identically_with_one_and_four_filter_threads`. | ||
| 9 | +//! | ||
| 10 | +//! Threading is opt-in: [`filter_threads`] is 1 unless `EC_AV1_FILTER_THREADS` | ||
| 11 | +//! says otherwise, and at 1 band nothing is spawned at all (the work runs on | ||
| 12 | +//! the calling thread, so every `thread_local!` gate counter in `decode.rs` | ||
| 13 | +//! still sees it). Above 1, a worker's counter increments are lost -- which is | ||
| 14 | +//! why the default stays 1 and every existing gate is unaffected. The knob | ||
| 15 | +//! composes multiplicatively with `EC_AV1_THREADS` (frame workers), so the | ||
| 16 | +//! product of the two is what must stay inside the core count. | ||
| 17 | +//! | ||
| 18 | +//! Measured on a 12-core box, `seg4k` (272 shown frames, interleaved medians | ||
| 19 | +//! of 3, other lanes decoding alongside): 34.41 s at 1 filter thread, 30.04 s | ||
| 20 | +//! at 2, 28.41 s at 4, 28.36 s at 6, 27.70 s at 8 -- i.e. the filter chain is | ||
| 21 | +//! ~19% of frame time and 4 threads take most of what is there. Recommended: | ||
| 22 | +//! `EC_AV1_FILTER_THREADS=4` with frame threading off, and the product with | ||
| 23 | +//! `EC_AV1_THREADS` kept at or below the core count once it lands (e.g. 2 | ||
| 24 | +//! frame workers x 4 filter threads on 12 cores). | ||
| 25 | +#![allow(unsafe_code)] | ||
| 26 | + | ||
| 27 | +/// `EC_AV1_FILTER_THREADS`, the number of worker threads each in-frame filter | ||
| 28 | +/// stage splits itself across (default 1 = single-threaded, the shipped | ||
| 29 | +/// behaviour). Read once per process. | ||
| 30 | +/// 0 = "not read yet"; the env read happens once and the value is a plain | ||
| 31 | +/// atomic afterwards, so a test can flip it ([`set_filter_threads`]) without | ||
| 32 | +/// touching the process environment. | ||
| 33 | +static FILTER_THREADS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 34 | + | ||
| 35 | +pub(crate) fn filter_threads() -> usize { | ||
| 36 | + match FILTER_THREADS.load(std::sync::atomic::Ordering::Relaxed) { | ||
| 37 | + 0 => { | ||
| 38 | + let n = crate::envflags::var("EC_AV1_FILTER_THREADS") | ||
| 39 | + .ok() | ||
| 40 | + .and_then(|v| v.trim().parse::<usize>().ok()) | ||
| 41 | + .unwrap_or(1) | ||
| 42 | + .clamp(1, 64); | ||
| 43 | + FILTER_THREADS.store(n, std::sync::atomic::Ordering::Relaxed); | ||
| 44 | + n | ||
| 45 | + } | ||
| 46 | + n => n, | ||
| 47 | + } | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +/// Test-only override of [`filter_threads`]. Process-global, so a gate using | ||
| 51 | +/// it must hold `stream.rs`'s gate-counter lock (which also keeps the | ||
| 52 | +/// counter-delta gates from decoding while the workers are on). | ||
| 53 | +#[cfg(test)] | ||
| 54 | +pub(crate) fn set_filter_threads(n: usize) { | ||
| 55 | + FILTER_THREADS.store(n.clamp(1, 64), std::sync::atomic::Ordering::Relaxed); | ||
| 56 | +} | ||
| 57 | + | ||
| 58 | +/// Splits `units` work units into at most `threads` contiguous, non-empty | ||
| 59 | +/// bands. The caller picks the unit so that a band boundary is always a legal | ||
| 60 | +/// cut of its stage (16-pixel rows for the deblocker, 2 mi for CDEF, one | ||
| 61 | +/// restoration-unit row for LR, one 32-pixel block row for film grain). | ||
| 62 | +pub(crate) fn bands(units: usize, threads: usize) -> Vec<(usize, usize)> { | ||
| 63 | + if units == 0 { | ||
| 64 | + return Vec::new(); | ||
| 65 | + } | ||
| 66 | + let t = threads.clamp(1, units); | ||
| 67 | + let per = units.div_ceil(t); | ||
| 68 | + (0..t) | ||
| 69 | + .map(|i| (i * per, ((i + 1) * per).min(units))) | ||
| 70 | + .filter(|(a, b)| a < b) | ||
| 71 | + .collect() | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +/// A `&mut T` handed to several scoped workers that write provably disjoint | ||
| 75 | +/// parts of it. | ||
| 76 | +/// | ||
| 77 | +/// corner-cut: the disjointness is proved by the caller's band arithmetic and | ||
| 78 | +/// checked end-to-end by the 1-vs-4-thread byte-exact gate, not by the borrow | ||
| 79 | +/// checker -- so Miri/stacked-borrows would flag the overlapping `&mut`s. The | ||
| 80 | +/// upgrade path is per-stage `chunks_mut`/`split_at_mut` plumbing (an origin | ||
| 81 | +/// offset through `filter_edge`, `cdef_filter_block`, | ||
| 82 | +/// `filter_restoration_unit` and `add_noise_to_block`), which is a far larger | ||
| 83 | +/// diff for the same output. | ||
| 84 | +pub(crate) struct Shared<'a, T: ?Sized>(*mut T, std::marker::PhantomData<&'a mut T>); | ||
| 85 | + | ||
| 86 | +// SAFETY: `Shared` only ever hands out the pointer it was built from; sending | ||
| 87 | +// it to a scoped thread is sending the `&mut T` the constructor consumed, and | ||
| 88 | +// the bands each worker touches are disjoint. | ||
| 89 | +unsafe impl<T: ?Sized + Send> Send for Shared<'_, T> {} | ||
| 90 | +unsafe impl<T: ?Sized + Send> Sync for Shared<'_, T> {} | ||
| 91 | + | ||
| 92 | +impl<T: ?Sized> Clone for Shared<'_, T> { | ||
| 93 | + fn clone(&self) -> Self { | ||
| 94 | + *self | ||
| 95 | + } | ||
| 96 | +} | ||
| 97 | +impl<T: ?Sized> Copy for Shared<'_, T> {} | ||
| 98 | + | ||
| 99 | +impl<'a, T: ?Sized> Shared<'a, T> { | ||
| 100 | + pub(crate) fn new(v: &'a mut T) -> Self { | ||
| 101 | + Self(v, std::marker::PhantomData) | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + /// # Safety | ||
| 105 | + /// The caller must touch only its own band, disjoint from every other | ||
| 106 | + /// worker's, for as long as the returned reference lives. | ||
| 107 | + pub(crate) unsafe fn get(&self) -> &mut T { | ||
| 108 | + unsafe { &mut *self.0 } | ||
| 109 | + } | ||
| 110 | +} | ||
| 111 | + | ||
| 112 | +/// Runs `f(band_start, band_end, ctx)` once per band: band 0 on this thread | ||
| 113 | +/// with the caller's own [`crate::decode::FrameCtx`], the rest on scoped | ||
| 114 | +/// worker threads, each with its own filter-scope copy of it (`FrameCtx` is | ||
| 115 | +/// `!Sync` by design). Nothing outlives the call. | ||
| 116 | +#[inline] | ||
| 117 | +pub(crate) fn run_bands<F>(bands: &[(usize, usize)], fctx: &crate::decode::FrameCtx, f: F) | ||
| 118 | +where | ||
| 119 | + F: Fn(usize, usize, &crate::decode::FrameCtx) + Sync, | ||
| 120 | +{ | ||
| 121 | + match bands { | ||
| 122 | + [] => {} | ||
| 123 | + [(a, b)] => f(*a, *b, fctx), | ||
| 124 | + [(a0, b0), rest @ ..] => { | ||
| 125 | + std::thread::scope(|s| { | ||
| 126 | + for &(a, b) in rest { | ||
| 127 | + let ctx = crate::decode::filter_ctx_copy(fctx); | ||
| 128 | + let f = &f; | ||
| 129 | + s.spawn(move || f(a, b, &ctx)); | ||
| 130 | + } | ||
| 131 | + f(*a0, *b0, fctx); | ||
| 132 | + }); | ||
| 133 | + } | ||
| 134 | + } | ||
| 135 | +} | ||
crates/ec-av1/src/restoration.rsmodified+44 -28
| @@ -1081,8 +1081,13 @@ pub(crate) fn apply_loop_restoration_plane( | |||
| 1081 | } | 1081 | } |
| 1082 | let voffset = 8u32 >> ss_y; | 1082 | let voffset = 8u32 >> ss_y; |
| 1083 | let ext_size = (unit_size * 3) / 2; | 1083 | let ext_size = (unit_size * 3) / 2; |
| 1084 | + // lane-filt1: the RU row walk is data-dependent (a short remainder is | ||
| 1085 | + // swallowed by the previous unit row), so resolve it into a row list | ||
| 1086 | + // first. Each row writes exactly `v_start..v_end` of `out` and the next | ||
| 1087 | + // row's `v_start` is this one's `v_end`, so row bands are disjoint; the | ||
| 1088 | + // reads are all from `cdef`/`deblocked`, immutable snapshots. | ||
| 1089 | + let mut rows: Vec<(u32, u32)> = Vec::new(); | ||
| 1084 | let mut y0 = 0u32; | 1090 | let mut y0 = 0u32; |
| 1085 | - let mut rrow = 0usize; | ||
| 1086 | while y0 < plane_h as u32 { | 1091 | while y0 < plane_h as u32 { |
| 1087 | let remaining_h = plane_h as u32 - y0; | 1092 | let remaining_h = plane_h as u32 - y0; |
| 1088 | let uh = if remaining_h < ext_size { remaining_h } else { unit_size }; | 1093 | let uh = if remaining_h < ext_size { remaining_h } else { unit_size }; |
| @@ -1091,35 +1096,46 @@ pub(crate) fn apply_loop_restoration_plane( | |||
| 1091 | if v_end < plane_h as u32 { | 1096 | if v_end < plane_h as u32 { |
| 1092 | v_end -= voffset; | 1097 | v_end -= voffset; |
| 1093 | } | 1098 | } |
| 1094 | - | 1099 | + rows.push((v_start, v_end)); |
| 1095 | - let mut x0 = 0u32; | ||
| 1096 | - let mut rcol = 0usize; | ||
| 1097 | - while x0 < plane_w as u32 { | ||
| 1098 | - let remaining_w = plane_w as u32 - x0; | ||
| 1099 | - let uw = if remaining_w < ext_size { remaining_w } else { unit_size }; | ||
| 1100 | - let filter = grid.get(plane, rrow, rcol); | ||
| 1101 | - if filter != UnitFilter::None { | ||
| 1102 | - filter_restoration_unit( | ||
| 1103 | - out, | ||
| 1104 | - cdef, | ||
| 1105 | - deblocked, | ||
| 1106 | - stride, | ||
| 1107 | - plane_w, | ||
| 1108 | - plane_h, | ||
| 1109 | - x0 as usize, | ||
| 1110 | - (x0 + uw) as usize, | ||
| 1111 | - v_start as usize, | ||
| 1112 | - v_end as usize, | ||
| 1113 | - ss_y, | ||
| 1114 | - filter, fctx, | ||
| 1115 | - ); | ||
| 1116 | - } | ||
| 1117 | - x0 += uw; | ||
| 1118 | - rcol += 1; | ||
| 1119 | - } | ||
| 1120 | y0 += uh; | 1100 | y0 += uh; |
| 1121 | - rrow += 1; | ||
| 1122 | } | 1101 | } |
| 1102 | + let rrow_count = rows.len(); | ||
| 1103 | + let so = crate::par::Shared::new(out); | ||
| 1104 | + let rbands = crate::par::bands(rrow_count, crate::par::filter_threads()); | ||
| 1105 | + crate::par::run_bands(&rbands, fctx, |r_lo, r_hi, fctx| { | ||
| 1106 | + // SAFETY: this band writes only the `out` rows its own RU rows cover. | ||
| 1107 | + #[allow(unsafe_code)] | ||
| 1108 | + let out = unsafe { so.get() }; | ||
| 1109 | + for rrow in r_lo..r_hi { | ||
| 1110 | + let (v_start, v_end) = rows[rrow]; | ||
| 1111 | + let mut x0 = 0u32; | ||
| 1112 | + let mut rcol = 0usize; | ||
| 1113 | + while x0 < plane_w as u32 { | ||
| 1114 | + let remaining_w = plane_w as u32 - x0; | ||
| 1115 | + let uw = if remaining_w < ext_size { remaining_w } else { unit_size }; | ||
| 1116 | + let filter = grid.get(plane, rrow, rcol); | ||
| 1117 | + if filter != UnitFilter::None { | ||
| 1118 | + filter_restoration_unit( | ||
| 1119 | + out, | ||
| 1120 | + cdef, | ||
| 1121 | + deblocked, | ||
| 1122 | + stride, | ||
| 1123 | + plane_w, | ||
| 1124 | + plane_h, | ||
| 1125 | + x0 as usize, | ||
| 1126 | + (x0 + uw) as usize, | ||
| 1127 | + v_start as usize, | ||
| 1128 | + v_end as usize, | ||
| 1129 | + ss_y, | ||
| 1130 | + filter, fctx, | ||
| 1131 | + ); | ||
| 1132 | + } | ||
| 1133 | + x0 += uw; | ||
| 1134 | + rcol += 1; | ||
| 1135 | + } | ||
| 1136 | + } | ||
| 1137 | + }); | ||
| 1138 | + let rrow = rrow_count; | ||
| 1123 | debug_assert_eq!(rrow, grid.vert_units[plane], "RU row walk must match RestorationGrid::new's count_units"); | 1139 | debug_assert_eq!(rrow, grid.vert_units[plane], "RU row walk must match RestorationGrid::new's count_units"); |
| 1124 | } | 1140 | } |
| 1125 | 1141 | ||
crates/ec-av1/src/stream.rsmodified+69 -0
| @@ -6091,6 +6091,75 @@ mod tests { | |||
| 6091 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg"); | 6091 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg"); |
| 6092 | } | 6092 | } |
| 6093 | 6093 | ||
| 6094 | + /// lane-filt1: `EC_AV1_FILTER_THREADS` splits every in-frame filter stage | ||
| 6095 | + /// -- deblock (row bands for the vertical-edge pass, column bands for the | ||
| 6096 | + /// horizontal one), CDEF (mi row bands), loop restoration (RU row bands) | ||
| 6097 | + /// and film grain (32-pixel block rows) -- across scoped worker threads. | ||
| 6098 | + /// A split is only legal if it is BYTE-EXACT, so this gate decodes two | ||
| 6099 | + /// real aomenc streams at 1 and at 4 filter threads and compares every | ||
| 6100 | + /// output frame's three planes. | ||
| 6101 | + /// | ||
| 6102 | + /// Between them the fixtures fire all four stages, and the firing | ||
| 6103 | + /// counters are asserted rather than assumed (class `gate-blind-to-feature`): | ||
| 6104 | + /// `superres_alltools_sb128_320x180.obu` is the 10-bit CDEF + loop | ||
| 6105 | + /// restoration stream, `grain_cdef_lr_128x128.obu` a 128x128 | ||
| 6106 | + /// `--denoise-noise-level=25` stream whose frames carry film grain | ||
| 6107 | + /// parameters (small enough that 4 bands is one block row each, which is | ||
| 6108 | + /// what exercises the grain band's one-row `prelude` replay). | ||
| 6109 | + #[test] | ||
| 6110 | + fn a_real_stream_filters_identically_with_one_and_four_filter_threads() { | ||
| 6111 | + const NAME: &str = "a_real_stream_filters_identically_with_one_and_four_filter_threads"; | ||
| 6112 | + let _gate_lock = lock_gate_counters(); | ||
| 6113 | + for (fixture, wants_lr, wants_grain) in [ | ||
| 6114 | + ("superres_alltools_sb128_320x180.obu", true, false), | ||
| 6115 | + ("grain_cdef_lr_128x128.obu", false, true), | ||
| 6116 | + ] { | ||
| 6117 | + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) | ||
| 6118 | + .join("fixtures") | ||
| 6119 | + .join(fixture); | ||
| 6120 | + let stream = std::fs::read(&path) | ||
| 6121 | + .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display())); | ||
| 6122 | + let before = ( | ||
| 6123 | + crate::decode::deblock_hits(), | ||
| 6124 | + crate::restoration::wiener_hits() + crate::restoration::sgrproj_hits(), | ||
| 6125 | + crate::film_grain::grain_hits(), | ||
| 6126 | + ); | ||
| 6127 | + crate::par::set_filter_threads(1); | ||
| 6128 | + let one = match decode_stream(&stream) { | ||
| 6129 | + Ok(frames) => frames, | ||
| 6130 | + Err(e) => panic!("{NAME}: {fixture} refused at 1 filter thread: {e}"), | ||
| 6131 | + }; | ||
| 6132 | + let after = ( | ||
| 6133 | + crate::decode::deblock_hits(), | ||
| 6134 | + crate::restoration::wiener_hits() + crate::restoration::sgrproj_hits(), | ||
| 6135 | + crate::film_grain::grain_hits(), | ||
| 6136 | + ); | ||
| 6137 | + crate::par::set_filter_threads(4); | ||
| 6138 | + let four = match decode_stream(&stream) { | ||
| 6139 | + Ok(frames) => frames, | ||
| 6140 | + Err(e) => { | ||
| 6141 | + crate::par::set_filter_threads(1); | ||
| 6142 | + panic!("{NAME}: {fixture} refused at 4 filter threads: {e}") | ||
| 6143 | + } | ||
| 6144 | + }; | ||
| 6145 | + crate::par::set_filter_threads(1); | ||
| 6146 | + assert!(after.0 > before.0, "{NAME}: {fixture} deblocked no edge at all"); | ||
| 6147 | + if wants_lr { | ||
| 6148 | + assert!(after.1 > before.1, "{NAME}: {fixture} ran no restoration unit"); | ||
| 6149 | + } | ||
| 6150 | + if wants_grain { | ||
| 6151 | + assert!(after.2 > before.2, "{NAME}: {fixture} applied no film grain"); | ||
| 6152 | + } | ||
| 6153 | + assert_eq!(one.len(), four.len(), "{NAME}: {fixture} frame count"); | ||
| 6154 | + assert!(!one.is_empty(), "{NAME}: {fixture} decoded nothing"); | ||
| 6155 | + for (i, (a, b)) in one.iter().zip(four.iter()).enumerate() { | ||
| 6156 | + assert_eq!(a.y, b.y, "{NAME}: {fixture} frame {i} luma differs at 4 filter threads"); | ||
| 6157 | + assert_eq!(a.u, b.u, "{NAME}: {fixture} frame {i} U differs at 4 filter threads"); | ||
| 6158 | + assert_eq!(a.v, b.v, "{NAME}: {fixture} frame {i} V differs at 4 filter threads"); | ||
| 6159 | + } | ||
| 6160 | + } | ||
| 6161 | + } | ||
| 6162 | + | ||
| 6094 | /// lane-t900 r30b: `fixtures/superres_alltools_sb128_320x180.obu` is 10 | 6163 | /// lane-t900 r30b: `fixtures/superres_alltools_sb128_320x180.obu` is 10 |
| 6095 | /// frames of `--cpu-used=4 --sb-size=128 --superres-mode=1 | 6164 | /// frames of `--cpu-used=4 --sb-size=128 --superres-mode=1 |
| 6096 | /// --superres-denominator=12 --cq-level=32` with every tool aomenc turns | 6165 | /// --superres-denominator=12 --cq-level=32` with every tool aomenc turns |