Parallelise the in-frame filter chain (deblock, CDEF, LR, film grain)
Browse files at this commit63e598c51ac03a12d5fda9d37695f1b0d2b2ac13Parent: 16ae97f- perf
- parallelise the in-frame filter chain (deblock, CDEF, LR, film grain)
6 changed files +858 -531
- 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/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}; |
| @@ -17175,126 +17196,158 @@ fn deblock_plane( | |||
| 17175 | // loop's order, and rows remain independent (horizontal pass mirrored). | 17196 | // loop's order, and rows remain independent (horizontal pass mirrored). |
| 17176 | let rows = plane.data.len() / stride.max(1); | 17197 | let rows = plane.data.len() / stride.max(1); |
| 17177 | let seg_lf_active = fctx.seg.with(|s| s.borrow().enabled); | 17198 | let seg_lf_active = fctx.seg.with(|s| s.borrow().enabled); |
| 17178 | - let mut x0 = 4usize; | 17199 | + // lane-filt1: both passes are split across `EC_AV1_FILTER_THREADS` |
| 17179 | - while x0 < tw { | 17200 | + // bands along the axis whose writes are disjoint. A vertical edge's |
| 17180 | - let mut y0 = 0usize; | 17201 | + // 4-sample groups write columns `x0-7 ..= x0+5` of their OWN 4 rows, so |
| 17181 | - while y0 < th { | 17202 | + // row bands never touch each other; a horizontal edge's groups write rows |
| 17182 | - if y0 + 16 <= th && x0 >= 8 && x0 + 8 <= stride { | 17203 | + // `y0-7 ..= y0+6` of their own 4 columns, so column bands never do. Both |
| 17183 | - let ps: [Option<(u8, i32)>; 4] = | 17204 | + // read the mode-info grids immutably. The grouping a band starts with is |
| 17184 | - std::array::from_fn(|k| edge_params(lf, n, plane_idx, 0, chroma, x0, y0 + 4 * k, seg_lf_active, fctx)); | 17205 | + // free: every 4-row (4-column) group's output is a pure function of that |
| 17185 | - if let Some((len, level)) = ps[0] { | 17206 | + // group, whether it goes through `filter_edge16` with three neighbours or |
| 17186 | - if ps.iter().all(|p| *p == ps[0]) | 17207 | + // through `filter_edge` alone. Band boundaries are multiples of 16 so no |
| 17187 | - && filter_edge16( | 17208 | + // group ever straddles one. |
| 17188 | - &mut plane.data, | 17209 | + let threads = crate::par::filter_threads(); |
| 17189 | - y0 * stride + x0, | 17210 | + let sp = crate::par::Shared::new(plane); |
| 17190 | - stride, | 17211 | + let vbands: Vec<(usize, usize)> = crate::par::bands(th.div_ceil(16), threads) |
| 17191 | - true, | 17212 | + .into_iter() |
| 17192 | - len, | 17213 | + .map(|(a, b)| (a * 16, (b * 16).min(th))) |
| 17193 | - level, | 17214 | + .collect(); |
| 17194 | - lf.sharpness, fctx, | 17215 | + crate::par::run_bands(&vbands, fctx, |y_lo, y_hi, fctx| { |
| 17195 | - ) | 17216 | + // SAFETY: this band writes only rows `y_lo..y_hi` of the plane, which |
| 17196 | - { | 17217 | + // no other band touches (see `Shared`'s own corner-cut note). |
| 17197 | - y0 += 16; | 17218 | + #[allow(unsafe_code)] |
| 17198 | - continue; | 17219 | + let plane = unsafe { sp.get() }; |
| 17220 | + let mut x0 = 4usize; | ||
| 17221 | + while x0 < tw { | ||
| 17222 | + let mut y0 = y_lo; | ||
| 17223 | + while y0 < y_hi { | ||
| 17224 | + if y0 + 16 <= y_hi && x0 >= 8 && x0 + 8 <= stride { | ||
| 17225 | + let ps: [Option<(u8, i32)>; 4] = | ||
| 17226 | + std::array::from_fn(|k| edge_params(lf, n, plane_idx, 0, chroma, x0, y0 + 4 * k, seg_lf_active, fctx)); | ||
| 17227 | + if let Some((len, level)) = ps[0] { | ||
| 17228 | + if ps.iter().all(|p| *p == ps[0]) | ||
| 17229 | + && filter_edge16( | ||
| 17230 | + &mut plane.data, | ||
| 17231 | + y0 * stride + x0, | ||
| 17232 | + stride, | ||
| 17233 | + true, | ||
| 17234 | + len, | ||
| 17235 | + level, | ||
| 17236 | + lf.sharpness, fctx, | ||
| 17237 | + ) | ||
| 17238 | + { | ||
| 17239 | + y0 += 16; | ||
| 17240 | + continue; | ||
| 17241 | + } | ||
| 17199 | } | 17242 | } |
| 17200 | - } | 17243 | + for (k, p) in ps.iter().enumerate() { |
| 17201 | - for (k, p) in ps.iter().enumerate() { | 17244 | + if let Some((len, level)) = *p { |
| 17202 | - if let Some((len, level)) = *p { | 17245 | + filter_edge( |
| 17203 | - filter_edge( | 17246 | + &mut plane.data, |
| 17204 | - &mut plane.data, | 17247 | + (y0 + 4 * k) * stride + x0, |
| 17205 | - (y0 + 4 * k) * stride + x0, | 17248 | + stride as isize, |
| 17206 | - stride as isize, | 17249 | + 1, |
| 17207 | - 1, | 17250 | + len, |
| 17208 | - len, | 17251 | + level, |
| 17209 | - level, | 17252 | + lf.sharpness, fctx, |
| 17210 | - lf.sharpness, fctx, | 17253 | + ); |
| 17211 | - ); | 17254 | + } |
| 17212 | } | 17255 | } |
| 17256 | + y0 += 16; | ||
| 17257 | + continue; | ||
| 17213 | } | 17258 | } |
| 17214 | - y0 += 16; | 17259 | + if let Some((len, level)) = edge_params(lf, n, plane_idx, 0, chroma, x0, y0, seg_lf_active, fctx) { |
| 17215 | - continue; | 17260 | + if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 { |
| 17216 | - } | 17261 | + eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}"); |
| 17217 | - if let Some((len, level)) = edge_params(lf, n, plane_idx, 0, chroma, x0, y0, seg_lf_active, fctx) { | 17262 | + } |
| 17218 | - if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 { | 17263 | + filter_edge( |
| 17219 | - eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}"); | 17264 | + &mut plane.data, |
| 17265 | + y0 * stride + x0, | ||
| 17266 | + stride as isize, | ||
| 17267 | + 1, | ||
| 17268 | + len, | ||
| 17269 | + level, | ||
| 17270 | + lf.sharpness, fctx, | ||
| 17271 | + ); | ||
| 17220 | } | 17272 | } |
| 17221 | - filter_edge( | 17273 | + y0 += 4; |
| 17222 | - &mut plane.data, | ||
| 17223 | - y0 * stride + x0, | ||
| 17224 | - stride as isize, | ||
| 17225 | - 1, | ||
| 17226 | - len, | ||
| 17227 | - level, | ||
| 17228 | - lf.sharpness, fctx, | ||
| 17229 | - ); | ||
| 17230 | } | 17274 | } |
| 17231 | - y0 += 4; | 17275 | + x0 += 4; |
| 17232 | } | 17276 | } |
| 17233 | - x0 += 4; | 17277 | + }); |
| 17234 | - } | 17278 | + // The horizontal pass runs only after the WHOLE vertical pass of this |
| 17235 | - let mut y0 = 4usize; | 17279 | + // plane (spec 7.14 order), which `run_bands` guarantees by joining. |
| 17236 | - while y0 < th { | 17280 | + let hbands: Vec<(usize, usize)> = crate::par::bands(tw.div_ceil(16), threads) |
| 17237 | - let mut x0 = 0usize; | 17281 | + .into_iter() |
| 17238 | - while x0 < tw { | 17282 | + .map(|(a, b)| (a * 16, (b * 16).min(tw))) |
| 17239 | - if x0 + 16 <= tw && y0 >= 8 && y0 + 7 <= rows { | 17283 | + .collect(); |
| 17240 | - let ps: [Option<(u8, i32)>; 4] = | 17284 | + crate::par::run_bands(&hbands, fctx, |x_lo, x_hi, fctx| { |
| 17241 | - std::array::from_fn(|k| edge_params(lf, n, plane_idx, 1, chroma, x0 + 4 * k, y0, seg_lf_active, fctx)); | 17285 | + // SAFETY: this band writes only columns `x_lo..x_hi`. |
| 17242 | - if let Some((len, level)) = ps[0] { | 17286 | + #[allow(unsafe_code)] |
| 17243 | - if ps.iter().all(|p| *p == ps[0]) | 17287 | + let plane = unsafe { sp.get() }; |
| 17244 | - && filter_edge16( | 17288 | + let mut y0 = 4usize; |
| 17245 | - &mut plane.data, | 17289 | + while y0 < th { |
| 17246 | - y0 * stride + x0, | 17290 | + let mut x0 = x_lo; |
| 17247 | - stride, | 17291 | + while x0 < x_hi { |
| 17248 | - false, | 17292 | + if x0 + 16 <= x_hi && y0 >= 8 && y0 + 7 <= rows { |
| 17249 | - len, | 17293 | + let ps: [Option<(u8, i32)>; 4] = |
| 17250 | - level, | 17294 | + std::array::from_fn(|k| edge_params(lf, n, plane_idx, 1, chroma, x0 + 4 * k, y0, seg_lf_active, fctx)); |
| 17251 | - lf.sharpness, fctx, | 17295 | + if let Some((len, level)) = ps[0] { |
| 17252 | - ) | 17296 | + if ps.iter().all(|p| *p == ps[0]) |
| 17253 | - { | 17297 | + && filter_edge16( |
| 17254 | - x0 += 16; | 17298 | + &mut plane.data, |
| 17255 | - continue; | 17299 | + y0 * stride + x0, |
| 17300 | + stride, | ||
| 17301 | + false, | ||
| 17302 | + len, | ||
| 17303 | + level, | ||
| 17304 | + lf.sharpness, fctx, | ||
| 17305 | + ) | ||
| 17306 | + { | ||
| 17307 | + x0 += 16; | ||
| 17308 | + continue; | ||
| 17309 | + } | ||
| 17256 | } | 17310 | } |
| 17311 | + for (k, p) in ps.iter().enumerate() { | ||
| 17312 | + if let Some((len, level)) = *p { | ||
| 17313 | + filter_edge( | ||
| 17314 | + &mut plane.data, | ||
| 17315 | + y0 * stride + x0 + 4 * k, | ||
| 17316 | + 1, | ||
| 17317 | + stride as isize, | ||
| 17318 | + len, | ||
| 17319 | + level, | ||
| 17320 | + lf.sharpness, fctx, | ||
| 17321 | + ); | ||
| 17322 | + } | ||
| 17323 | + } | ||
| 17324 | + x0 += 16; | ||
| 17325 | + continue; | ||
| 17257 | } | 17326 | } |
| 17258 | - for (k, p) in ps.iter().enumerate() { | 17327 | + if let Some((len, level)) = edge_params(lf, n, plane_idx, 1, chroma, x0, y0, seg_lf_active, fctx) { |
| 17259 | - if let Some((len, level)) = *p { | 17328 | + if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE") |
| 17260 | - filter_edge( | 17329 | + && plane_idx == 0 |
| 17261 | - &mut plane.data, | 17330 | + && x0 == 44 |
| 17262 | - y0 * stride + x0 + 4 * k, | 17331 | + { |
| 17263 | - 1, | 17332 | + eprintln!( |
| 17264 | - stride as isize, | 17333 | + "HEDGE x0={x0} y0={y0} len={len} level={level} sharpness={}", |
| 17265 | - len, | 17334 | + lf.sharpness |
| 17266 | - level, | ||
| 17267 | - lf.sharpness, fctx, | ||
| 17268 | ); | 17335 | ); |
| 17269 | } | 17336 | } |
| 17270 | - } | 17337 | + filter_edge( |
| 17271 | - x0 += 16; | 17338 | + &mut plane.data, |
| 17272 | - continue; | 17339 | + y0 * stride + x0, |
| 17273 | - } | 17340 | + 1, |
| 17274 | - if let Some((len, level)) = edge_params(lf, n, plane_idx, 1, chroma, x0, y0, seg_lf_active, fctx) { | 17341 | + stride as isize, |
| 17275 | - if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE") | 17342 | + len, |
| 17276 | - && plane_idx == 0 | 17343 | + level, |
| 17277 | - && x0 == 44 | 17344 | + lf.sharpness, fctx, |
| 17278 | - { | ||
| 17279 | - eprintln!( | ||
| 17280 | - "HEDGE x0={x0} y0={y0} len={len} level={level} sharpness={}", | ||
| 17281 | - lf.sharpness | ||
| 17282 | ); | 17345 | ); |
| 17283 | } | 17346 | } |
| 17284 | - filter_edge( | 17347 | + x0 += 4; |
| 17285 | - &mut plane.data, | ||
| 17286 | - y0 * stride + x0, | ||
| 17287 | - 1, | ||
| 17288 | - stride as isize, | ||
| 17289 | - len, | ||
| 17290 | - level, | ||
| 17291 | - lf.sharpness, fctx, | ||
| 17292 | - ); | ||
| 17293 | } | 17348 | } |
| 17294 | - x0 += 4; | 17349 | + y0 += 4; |
| 17295 | - } | 17350 | + } }); |
| 17296 | - y0 += 4; | ||
| 17297 | - } | ||
| 17298 | } | 17351 | } |
| 17299 | 17352 | ||
| 17300 | thread_local! { | 17353 | thread_local! { |
| @@ -17425,124 +17478,145 @@ fn apply_cdef( | |||
| 17425 | let coeff_shift = i32::from(bit_depth(fctx)) - 8; | 17478 | let coeff_shift = i32::from(bit_depth(fctx)) - 8; |
| 17426 | let damping = i32::from(cdef.damping) + coeff_shift; | 17479 | let damping = i32::from(cdef.damping) + coeff_shift; |
| 17427 | let damping_uv = (damping - 1).max(0); | 17480 | let damping_uv = (damping - 1).max(0); |
| 17428 | - // One scratch window per plane kind, reused by every unit (8x8 luma and | 17481 | + // lane-filt1: every CDEF unit reads ONLY the pre-CDEF snapshots |
| 17429 | - // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring). | 17482 | + // (`src_y`/`src_u`/`src_v`, immutable here) and writes its own 8x8 luma / |
| 17430 | - let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)]; | 17483 | + // 4x4 chroma square, so units are independent. Bands are mi ROW bands (2 |
| 17431 | - let mut win_uv = [0i32; (4 + 2 * CDEF_BORDER) * (4 + 2 * CDEF_BORDER)]; | 17484 | + // mi = 8 luma rows, 4 chroma rows), which makes each band's writes a |
| 17432 | - let mut mi_r = 0usize; | 17485 | + // disjoint row range of all three planes. |
| 17433 | - while mi_r < skip_grid.mi_rows { | 17486 | + let (sy, su, sv) = ( |
| 17434 | - let mut mi_c = 0usize; | 17487 | + crate::par::Shared::new(y), |
| 17435 | - while mi_c < skip_grid.mi_cols { | 17488 | + crate::par::Shared::new(u), |
| 17436 | - if skip_grid.is_skip_txfm(mi_r, mi_c) { | 17489 | + crate::par::Shared::new(v), |
| 17437 | - CDEF_SKIPPED_UNITS.with(|c| c.set(c.get() + 1)); | 17490 | + ); |
| 17438 | - } | 17491 | + let mi_rows = skip_grid.mi_rows; |
| 17439 | - if !skip_grid.is_skip_txfm(mi_r, mi_c) { | 17492 | + let cbands: Vec<(usize, usize)> = crate::par::bands(mi_rows.div_ceil(2), crate::par::filter_threads()) |
| 17440 | - let sidx = strength_idx(mi_r, mi_c); | 17493 | + .into_iter() |
| 17441 | - let (ox, oy) = (mi_c * 4, mi_r * 4); | 17494 | + .map(|(a, b)| (a * 2, (b * 2).min(mi_rows))) |
| 17442 | - // Nothing to do for this unit at all: the direction search is | 17495 | + .collect(); |
| 17443 | - // only ever consumed through a strength, so a unit whose four | 17496 | + crate::par::run_bands(&cbands, fctx, |r_lo, r_hi, fctx| { |
| 17444 | - // strengths are zero never needs its 8x8 window gathered. | 17497 | + // SAFETY: this band writes only the plane rows its own mi rows cover, |
| 17445 | - if cdef.y_pri_strength[sidx] == 0 | 17498 | + // disjoint from every other band's (see `Shared`). |
| 17446 | - && cdef.y_sec_strength[sidx] == 0 | 17499 | + #[allow(unsafe_code)] |
| 17447 | - && cdef.uv_pri_strength[sidx] == 0 | 17500 | + let (y, u, v) = unsafe { (sy.get(), su.get(), sv.get()) }; |
| 17448 | - && cdef.uv_sec_strength[sidx] == 0 | 17501 | + // One scratch window per plane kind, reused by every unit (8x8 luma and |
| 17449 | - { | 17502 | + // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring). |
| 17450 | - mi_c += 2; | 17503 | + let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)]; |
| 17451 | - continue; | 17504 | + let mut win_uv = [0i32; (4 + 2 * CDEF_BORDER) * (4 + 2 * CDEF_BORDER)]; |
| 17452 | - } | 17505 | + let mut mi_r = r_lo; |
| 17453 | - let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height); | 17506 | + while mi_r < r_hi { |
| 17454 | - cdef_gather(&src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y); | 17507 | + let mut mi_c = 0usize; |
| 17455 | - let sample_y = |r: i32, c: i32| -> i32 { | 17508 | + while mi_c < skip_grid.mi_cols { |
| 17456 | - win_y[(r + CDEF_BORDER as i32) as usize * (8 + 2 * CDEF_BORDER) | 17509 | + if skip_grid.is_skip_txfm(mi_r, mi_c) { |
| 17457 | - + (c + CDEF_BORDER as i32) as usize] | 17510 | + CDEF_SKIPPED_UNITS.with(|c| c.set(c.get() + 1)); |
| 17458 | - }; | ||
| 17459 | - // lane-cdef r1: the direction search reads the SAME padded | ||
| 17460 | - // block the taps do -- libaom runs `cdef_find_dir` on the | ||
| 17461 | - // prepared filter block (`cdef_block.c:296-320` over | ||
| 17462 | - // `cdef_prepare_fb`'s buffer, `cdef.c:249-256`), whose columns | ||
| 17463 | - // and rows past the mi-rounded frame extent are CDEF_VERY_LARGE. | ||
| 17464 | - // Clamping to the last real sample instead made every straddling | ||
| 17465 | - // edge 8x8 (odd `mi_cols`/`mi_rows`) pick a different var, hence | ||
| 17466 | - // a different `adjust_strength`, hence a +-1 band at the crop edge. | ||
| 17467 | - let (dir, var) = cdef_find_dir(coeff_shift, |r, c| sample_y(r as i32, c as i32)); | ||
| 17468 | - | ||
| 17469 | - // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0` | ||
| 17470 | - // -- each plane zeroes the shared direction when *its own* | ||
| 17471 | - // frame-level primary strength is 0, independent of the | ||
| 17472 | - // per-block adjusted `t` and of the other plane's strength. | ||
| 17473 | - let y_pri_strength = i32::from(cdef.y_pri_strength[sidx]) << coeff_shift; | ||
| 17474 | - let y_sec_strength = i32::from(cdef.y_sec_strength[sidx]) << coeff_shift; | ||
| 17475 | - let y_dir = if y_pri_strength != 0 { dir } else { 0 }; | ||
| 17476 | - let t = cdef_adjust_strength(y_pri_strength, var); | ||
| 17477 | - // lane-cdef r1 rung: one line per filtered 8x8 luma CDEF unit. | ||
| 17478 | - if crate::envflags::env_flag!("EC_AV1_CDEF_DBG") { | ||
| 17479 | - eprintln!( | ||
| 17480 | - "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}" | ||
| 17481 | - ); | ||
| 17482 | - } | ||
| 17483 | - let enable_primary = t != 0; | ||
| 17484 | - let enable_secondary = y_sec_strength != 0; | ||
| 17485 | - if (ox + 8 > frame_w || oy + 8 > frame_h) | ||
| 17486 | - && (enable_primary | ||
| 17487 | - || enable_secondary | ||
| 17488 | - || cdef.uv_pri_strength[sidx] != 0 | ||
| 17489 | - || cdef.uv_sec_strength[sidx] != 0) | ||
| 17490 | - { | ||
| 17491 | - CDEF_STRADDLE_UNITS.with(|c| c.set(c.get() + 1)); | ||
| 17492 | - } | ||
| 17493 | - if enable_primary || enable_secondary { | ||
| 17494 | - cdef_filter_block( | ||
| 17495 | - &win_y, | ||
| 17496 | - y, | ||
| 17497 | - ox, | ||
| 17498 | - oy, | ||
| 17499 | - 8, | ||
| 17500 | - 8, | ||
| 17501 | - t, | ||
| 17502 | - y_sec_strength, | ||
| 17503 | - y_dir, | ||
| 17504 | - damping, | ||
| 17505 | - damping, | ||
| 17506 | - enable_primary, | ||
| 17507 | - enable_secondary, | ||
| 17508 | - coeff_shift, fctx, | ||
| 17509 | - ); | ||
| 17510 | } | 17511 | } |
| 17511 | - | 17512 | + if !skip_grid.is_skip_txfm(mi_r, mi_c) { |
| 17512 | - let t_uv = i32::from(cdef.uv_pri_strength[sidx]) << coeff_shift; | 17513 | + let sidx = strength_idx(mi_r, mi_c); |
| 17513 | - let uv_sec_strength = i32::from(cdef.uv_sec_strength[sidx]) << coeff_shift; | 17514 | + let (ox, oy) = (mi_c * 4, mi_r * 4); |
| 17514 | - let uv_dir = if t_uv != 0 { dir } else { 0 }; | 17515 | + // Nothing to do for this unit at all: the direction search is |
| 17515 | - let enable_primary_uv = t_uv != 0; | 17516 | + // only ever consumed through a strength, so a unit whose four |
| 17516 | - let enable_secondary_uv = uv_sec_strength != 0; | 17517 | + // strengths are zero never needs its 8x8 window gathered. |
| 17517 | - if enable_primary_uv || enable_secondary_uv { | 17518 | + if cdef.y_pri_strength[sidx] == 0 |
| 17518 | - let (cox, coy) = (ox / 2, oy / 2); | 17519 | + && cdef.y_sec_strength[sidx] == 0 |
| 17519 | - for (plane, src_p) in [(&mut *u, &src_u), (&mut *v, &src_v)] { | 17520 | + && cdef.uv_pri_strength[sidx] == 0 |
| 17520 | - let (tw, th) = (plane.true_width, plane.true_height); | 17521 | + && cdef.uv_sec_strength[sidx] == 0 |
| 17521 | - let stride = plane.width; | 17522 | + { |
| 17522 | - cdef_gather(src_p, stride, tw, th, cox, coy, 4, 4, &mut win_uv); | 17523 | + mi_c += 2; |
| 17524 | + continue; | ||
| 17525 | + } | ||
| 17526 | + let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height); | ||
| 17527 | + cdef_gather(&src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y); | ||
| 17528 | + let sample_y = |r: i32, c: i32| -> i32 { | ||
| 17529 | + win_y[(r + CDEF_BORDER as i32) as usize * (8 + 2 * CDEF_BORDER) | ||
| 17530 | + + (c + CDEF_BORDER as i32) as usize] | ||
| 17531 | + }; | ||
| 17532 | + // lane-cdef r1: the direction search reads the SAME padded | ||
| 17533 | + // block the taps do -- libaom runs `cdef_find_dir` on the | ||
| 17534 | + // prepared filter block (`cdef_block.c:296-320` over | ||
| 17535 | + // `cdef_prepare_fb`'s buffer, `cdef.c:249-256`), whose columns | ||
| 17536 | + // and rows past the mi-rounded frame extent are CDEF_VERY_LARGE. | ||
| 17537 | + // Clamping to the last real sample instead made every straddling | ||
| 17538 | + // edge 8x8 (odd `mi_cols`/`mi_rows`) pick a different var, hence | ||
| 17539 | + // a different `adjust_strength`, hence a +-1 band at the crop edge. | ||
| 17540 | + let (dir, var) = cdef_find_dir(coeff_shift, |r, c| sample_y(r as i32, c as i32)); | ||
| 17541 | + | ||
| 17542 | + // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0` | ||
| 17543 | + // -- each plane zeroes the shared direction when *its own* | ||
| 17544 | + // frame-level primary strength is 0, independent of the | ||
| 17545 | + // per-block adjusted `t` and of the other plane's strength. | ||
| 17546 | + let y_pri_strength = i32::from(cdef.y_pri_strength[sidx]) << coeff_shift; | ||
| 17547 | + let y_sec_strength = i32::from(cdef.y_sec_strength[sidx]) << coeff_shift; | ||
| 17548 | + let y_dir = if y_pri_strength != 0 { dir } else { 0 }; | ||
| 17549 | + let t = cdef_adjust_strength(y_pri_strength, var); | ||
| 17550 | + // lane-cdef r1 rung: one line per filtered 8x8 luma CDEF unit. | ||
| 17551 | + if crate::envflags::env_flag!("EC_AV1_CDEF_DBG") { | ||
| 17552 | + eprintln!( | ||
| 17553 | + "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}" | ||
| 17554 | + ); | ||
| 17555 | + } | ||
| 17556 | + let enable_primary = t != 0; | ||
| 17557 | + let enable_secondary = y_sec_strength != 0; | ||
| 17558 | + if (ox + 8 > frame_w || oy + 8 > frame_h) | ||
| 17559 | + && (enable_primary | ||
| 17560 | + || enable_secondary | ||
| 17561 | + || cdef.uv_pri_strength[sidx] != 0 | ||
| 17562 | + || cdef.uv_sec_strength[sidx] != 0) | ||
| 17563 | + { | ||
| 17564 | + CDEF_STRADDLE_UNITS.with(|c| c.set(c.get() + 1)); | ||
| 17565 | + } | ||
| 17566 | + if enable_primary || enable_secondary { | ||
| 17523 | cdef_filter_block( | 17567 | cdef_filter_block( |
| 17524 | - &win_uv, | 17568 | + &win_y, |
| 17525 | - plane, | 17569 | + y, |
| 17526 | - cox, | 17570 | + ox, |
| 17527 | - coy, | 17571 | + oy, |
| 17528 | - 4, | 17572 | + 8, |
| 17529 | - 4, | 17573 | + 8, |
| 17530 | - t_uv, | 17574 | + t, |
| 17531 | - uv_sec_strength, | 17575 | + y_sec_strength, |
| 17532 | - uv_dir, | 17576 | + y_dir, |
| 17533 | - damping_uv, | 17577 | + damping, |
| 17534 | - damping_uv, | 17578 | + damping, |
| 17535 | - enable_primary_uv, | 17579 | + enable_primary, |
| 17536 | - enable_secondary_uv, | 17580 | + enable_secondary, |
| 17537 | coeff_shift, fctx, | 17581 | coeff_shift, fctx, |
| 17538 | ); | 17582 | ); |
| 17539 | } | 17583 | } |
| 17584 | + | ||
| 17585 | + let t_uv = i32::from(cdef.uv_pri_strength[sidx]) << coeff_shift; | ||
| 17586 | + let uv_sec_strength = i32::from(cdef.uv_sec_strength[sidx]) << coeff_shift; | ||
| 17587 | + let uv_dir = if t_uv != 0 { dir } else { 0 }; | ||
| 17588 | + let enable_primary_uv = t_uv != 0; | ||
| 17589 | + let enable_secondary_uv = uv_sec_strength != 0; | ||
| 17590 | + if enable_primary_uv || enable_secondary_uv { | ||
| 17591 | + let (cox, coy) = (ox / 2, oy / 2); | ||
| 17592 | + for (plane, src_p) in [(&mut *u, &src_u), (&mut *v, &src_v)] { | ||
| 17593 | + let (tw, th) = (plane.true_width, plane.true_height); | ||
| 17594 | + let stride = plane.width; | ||
| 17595 | + cdef_gather(src_p, stride, tw, th, cox, coy, 4, 4, &mut win_uv); | ||
| 17596 | + cdef_filter_block( | ||
| 17597 | + &win_uv, | ||
| 17598 | + plane, | ||
| 17599 | + cox, | ||
| 17600 | + coy, | ||
| 17601 | + 4, | ||
| 17602 | + 4, | ||
| 17603 | + t_uv, | ||
| 17604 | + uv_sec_strength, | ||
| 17605 | + uv_dir, | ||
| 17606 | + damping_uv, | ||
| 17607 | + damping_uv, | ||
| 17608 | + enable_primary_uv, | ||
| 17609 | + enable_secondary_uv, | ||
| 17610 | + coeff_shift, fctx, | ||
| 17611 | + ); | ||
| 17612 | + } | ||
| 17613 | + } | ||
| 17540 | } | 17614 | } |
| 17615 | + mi_c += 2; | ||
| 17541 | } | 17616 | } |
| 17542 | - mi_c += 2; | ||
| 17543 | - } | ||
| 17544 | mi_r += 2; | 17617 | mi_r += 2; |
| 17545 | - } | 17618 | + } |
| 17619 | + }); | ||
| 17546 | scratch_return(src_y); | 17620 | scratch_return(src_y); |
| 17547 | scratch_return(src_u); | 17621 | scratch_return(src_u); |
| 17548 | scratch_return(src_v); | 17622 | scratch_return(src_v); |
crates/ec-av1/src/film_grain.rsmodified+318 -286
| @@ -879,342 +879,374 @@ pub(crate) fn apply_grain( | |||
| 879 | 879 | ||
| 880 | let overlap = fg.overlap_flag; | 880 | let overlap = fg.overlap_flag; |
| 881 | 881 | ||
| 882 | - let mut y_line_buf = vec![0i32; (y_stride * 2).max(1) as usize]; | 882 | + // lane-filt1: 32-pixel block ROWS are the parallel axis. A row's noise |
| 883 | - let mut cb_line_buf = vec![0i32; c_stride.max(1) as usize]; | 883 | + // offsets come from `Rng::line(y * 2, grain_seed)` -- a function of the |
| 884 | - let mut cr_line_buf = vec![0i32; c_stride.max(1) as usize]; | 884 | + // row and the frame seed alone -- and a row writes only luma rows |
| 885 | - let mut y_col_buf = vec![0i32; ((LUMA_SUBBLOCK + 2) * 2) as usize]; | 885 | + // `y*2 .. y*2+31` (chroma `y .. y+15`), so rows are write-disjoint. The |
| 886 | - let mut cb_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; | 886 | + // one carry ACROSS rows is `*_line_buf` (the row above's bottom lines, |
| 887 | - let mut cr_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; | 887 | + // blended into this row's top lines when `overlap_flag`), and its final |
| 888 | + // content is written by that row's own blocks out of the grain templates | ||
| 889 | + // and `*_col_buf` -- never out of the row above it. So a band replays | ||
| 890 | + // exactly ONE preceding row with its pixel writes suppressed (`prelude`) | ||
| 891 | + // to rebuild the line buffers, then codes its own rows exactly as the | ||
| 892 | + // single-threaded walk does. | ||
| 893 | + let row_step = LUMA_SUBBLOCK >> 1; | ||
| 894 | + let nrows = ((height / 2).max(0) as usize).div_ceil(row_step as usize); | ||
| 895 | + let so = crate::par::Shared::new(&mut out); | ||
| 896 | + let gbands = crate::par::bands(nrows, crate::par::filter_threads()); | ||
| 897 | + crate::par::run_bands(&gbands, fctx, |b_lo, b_hi, fctx| { | ||
| 898 | + // SAFETY: this band writes only the picture rows its own block rows | ||
| 899 | + // cover, disjoint from every other band's. | ||
| 900 | + #[allow(unsafe_code)] | ||
| 901 | + let out = unsafe { so.get() }; | ||
| 902 | + let y_lo = (b_lo * row_step as usize) as i32; | ||
| 903 | + let y_hi = ((b_hi * row_step as usize) as i32).min(height / 2); | ||
| 904 | + let y_first = if b_lo > 0 { y_lo - row_step } else { y_lo }; | ||
| 905 | + let mut y_line_buf = vec![0i32; (y_stride * 2).max(1) as usize]; | ||
| 906 | + let mut cb_line_buf = vec![0i32; c_stride.max(1) as usize]; | ||
| 907 | + let mut cr_line_buf = vec![0i32; c_stride.max(1) as usize]; | ||
| 908 | + let mut y_col_buf = vec![0i32; ((LUMA_SUBBLOCK + 2) * 2) as usize]; | ||
| 909 | + let mut cb_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; | ||
| 910 | + let mut cr_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; | ||
| 888 | 911 | ||
| 889 | - let mut y = 0; | 912 | + let mut y = y_first; |
| 890 | - while y < height / 2 { | 913 | + while y < y_hi { |
| 891 | - let mut row_rng = Rng::line(y * 2, fg.grain_seed); | 914 | + let prelude = y < y_lo; |
| 892 | - let mut x = 0; | 915 | + let mut row_rng = Rng::line(y * 2, fg.grain_seed); |
| 893 | - while x < width / 2 { | 916 | + let mut x = 0; |
| 894 | - let raw = row_rng.get(8); | 917 | + while x < width / 2 { |
| 895 | - let offset_x = (raw >> 4) & 15; | 918 | + let raw = row_rng.get(8); |
| 896 | - let offset_y = raw & 15; | 919 | + let offset_x = (raw >> 4) & 15; |
| 920 | + let offset_y = raw & 15; | ||
| 897 | 921 | ||
| 898 | - let luma_off_y = LEFT_PAD + 2 * AR_PADDING + (offset_y << 1); | 922 | + let luma_off_y = LEFT_PAD + 2 * AR_PADDING + (offset_y << 1); |
| 899 | - let luma_off_x = TOP_PAD + 2 * AR_PADDING + (offset_x << 1); | 923 | + let luma_off_x = TOP_PAD + 2 * AR_PADDING + (offset_x << 1); |
| 900 | - let chroma_off_y = TOP_PAD + AR_PADDING + offset_y; | 924 | + let chroma_off_y = TOP_PAD + AR_PADDING + offset_y; |
| 901 | - let chroma_off_x = LEFT_PAD + AR_PADDING + offset_x; | 925 | + let chroma_off_x = LEFT_PAD + AR_PADDING + offset_x; |
| 902 | 926 | ||
| 903 | - if overlap && x != 0 { | 927 | + if overlap && x != 0 { |
| 904 | - let h_count = (LUMA_SUBBLOCK + 2).min(height - (y << 1)); | 928 | + let h_count = (LUMA_SUBBLOCK + 2).min(height - (y << 1)); |
| 905 | - ver_overlap_inplace( | 929 | + ver_overlap_inplace( |
| 906 | - &mut y_col_buf, | 930 | + &mut y_col_buf, |
| 907 | - 2, | 931 | + 2, |
| 908 | - 0, | 932 | + 0, |
| 909 | - 0, | 933 | + 0, |
| 910 | - &luma_template, | 934 | + &luma_template, |
| 911 | - LUMA_BLOCK_W, | 935 | + LUMA_BLOCK_W, |
| 912 | - luma_off_y, | 936 | + luma_off_y, |
| 913 | - luma_off_x, | 937 | + luma_off_x, |
| 914 | - 2, | 938 | + 2, |
| 915 | - h_count, fctx, | 939 | + h_count, fctx, |
| 916 | - ); | 940 | + ); |
| 917 | - let hc_count = (CHROMA_SUBBLOCK + 1).min((height - (y << 1)) >> 1); | 941 | + let hc_count = (CHROMA_SUBBLOCK + 1).min((height - (y << 1)) >> 1); |
| 918 | - ver_overlap_inplace( | 942 | + ver_overlap_inplace( |
| 919 | - &mut cb_col_buf, | 943 | + &mut cb_col_buf, |
| 920 | - 1, | 944 | + 1, |
| 921 | - 0, | 945 | + 0, |
| 922 | - 0, | 946 | + 0, |
| 923 | - &cb_template, | 947 | + &cb_template, |
| 924 | - CHROMA_BLOCK_W, | 948 | + CHROMA_BLOCK_W, |
| 925 | - chroma_off_y, | 949 | + chroma_off_y, |
| 926 | - chroma_off_x, | 950 | + chroma_off_x, |
| 927 | - 1, | 951 | + 1, |
| 928 | - hc_count, fctx, | 952 | + hc_count, fctx, |
| 929 | - ); | 953 | + ); |
| 930 | - ver_overlap_inplace( | 954 | + ver_overlap_inplace( |
| 931 | - &mut cr_col_buf, | 955 | + &mut cr_col_buf, |
| 932 | - 1, | 956 | + 1, |
| 933 | - 0, | 957 | + 0, |
| 934 | - 0, | 958 | + 0, |
| 935 | - &cr_template, | 959 | + &cr_template, |
| 936 | - CHROMA_BLOCK_W, | 960 | + CHROMA_BLOCK_W, |
| 937 | - chroma_off_y, | 961 | + chroma_off_y, |
| 938 | - chroma_off_x, | 962 | + chroma_off_x, |
| 939 | - 1, | 963 | + 1, |
| 940 | - hc_count, fctx, | 964 | + hc_count, fctx, |
| 941 | - ); | 965 | + ); |
| 942 | 966 | ||
| 943 | - let i = if y != 0 { 1 } else { 0 }; | 967 | + let i = if y != 0 { 1 } else { 0 }; |
| 944 | - add_noise_to_block( | 968 | + if !prelude { |
| 945 | - fg, | 969 | + add_noise_to_block( |
| 946 | - mc_identity, | 970 | + fg, |
| 947 | - &scaling_y, | 971 | + mc_identity, |
| 948 | - &scaling_cb, | 972 | + &scaling_y, |
| 949 | - &scaling_cr, | 973 | + &scaling_cb, |
| 950 | - &mut out, | 974 | + &scaling_cr, |
| 951 | - (y + i) << 1, | 975 | + &mut *out, |
| 952 | - x << 1, | 976 | + (y + i) << 1, |
| 953 | - &y_col_buf, | 977 | + x << 1, |
| 954 | - 2, | 978 | + &y_col_buf, |
| 955 | - i * 2, | 979 | + 2, |
| 956 | - 0, | 980 | + i * 2, |
| 957 | - &cb_col_buf, | 981 | + 0, |
| 958 | - &cr_col_buf, | 982 | + &cb_col_buf, |
| 959 | - 1, | 983 | + &cr_col_buf, |
| 960 | - i, | 984 | + 1, |
| 961 | - 0, | 985 | + i, |
| 962 | - (LUMA_SUBBLOCK >> 1).min(height / 2 - y) - i, | 986 | + 0, |
| 963 | - 1, fctx, | 987 | + (LUMA_SUBBLOCK >> 1).min(height / 2 - y) - i, |
| 964 | - ); | 988 | + 1, fctx, |
| 965 | - } | 989 | + ); |
| 990 | + } | ||
| 991 | + } | ||
| 966 | 992 | ||
| 967 | - if overlap && y != 0 { | 993 | + if overlap && y != 0 { |
| 968 | - if x != 0 { | 994 | + if x != 0 { |
| 995 | + hor_overlap_inplace( | ||
| 996 | + &mut y_line_buf, | ||
| 997 | + y_stride, | ||
| 998 | + 0, | ||
| 999 | + x << 1, | ||
| 1000 | + &y_col_buf, | ||
| 1001 | + 2, | ||
| 1002 | + 0, | ||
| 1003 | + 0, | ||
| 1004 | + 2, | ||
| 1005 | + 2, fctx, | ||
| 1006 | + ); | ||
| 1007 | + hor_overlap_inplace( | ||
| 1008 | + &mut cb_line_buf, | ||
| 1009 | + c_stride, | ||
| 1010 | + 0, | ||
| 1011 | + x, | ||
| 1012 | + &cb_col_buf, | ||
| 1013 | + 1, | ||
| 1014 | + 0, | ||
| 1015 | + 0, | ||
| 1016 | + 1, | ||
| 1017 | + 1, fctx, | ||
| 1018 | + ); | ||
| 1019 | + hor_overlap_inplace( | ||
| 1020 | + &mut cr_line_buf, | ||
| 1021 | + c_stride, | ||
| 1022 | + 0, | ||
| 1023 | + x, | ||
| 1024 | + &cr_col_buf, | ||
| 1025 | + 1, | ||
| 1026 | + 0, | ||
| 1027 | + 0, | ||
| 1028 | + 1, | ||
| 1029 | + 1, fctx, | ||
| 1030 | + ); | ||
| 1031 | + } | ||
| 1032 | + let lcol0 = if x != 0 { (x + 1) << 1 } else { 0 }; | ||
| 1033 | + let ccol0 = if x != 0 { x + 1 } else { 0 }; | ||
| 1034 | + let lw = (LUMA_SUBBLOCK - (if x != 0 { 2 } else { 0 })).min(width - lcol0); | ||
| 969 | hor_overlap_inplace( | 1035 | hor_overlap_inplace( |
| 970 | &mut y_line_buf, | 1036 | &mut y_line_buf, |
| 971 | y_stride, | 1037 | y_stride, |
| 972 | 0, | 1038 | 0, |
| 973 | - x << 1, | 1039 | + lcol0, |
| 974 | - &y_col_buf, | 1040 | + &luma_template, |
| 975 | - 2, | 1041 | + LUMA_BLOCK_W, |
| 976 | - 0, | 1042 | + luma_off_y, |
| 977 | - 0, | 1043 | + luma_off_x + if x != 0 { 2 } else { 0 }, |
| 978 | - 2, | 1044 | + lw, |
| 979 | 2, fctx, | 1045 | 2, fctx, |
| 980 | ); | 1046 | ); |
| 1047 | + let cw = (CHROMA_SUBBLOCK - (if x != 0 { 1 } else { 0 })).min((width - lcol0) >> 1); | ||
| 981 | hor_overlap_inplace( | 1048 | hor_overlap_inplace( |
| 982 | &mut cb_line_buf, | 1049 | &mut cb_line_buf, |
| 983 | c_stride, | 1050 | c_stride, |
| 984 | 0, | 1051 | 0, |
| 985 | - x, | 1052 | + ccol0, |
| 986 | - &cb_col_buf, | 1053 | + &cb_template, |
| 987 | - 1, | 1054 | + CHROMA_BLOCK_W, |
| 988 | - 0, | 1055 | + chroma_off_y, |
| 989 | - 0, | 1056 | + chroma_off_x + if x != 0 { 1 } else { 0 }, |
| 990 | - 1, | 1057 | + cw, |
| 991 | 1, fctx, | 1058 | 1, fctx, |
| 992 | ); | 1059 | ); |
| 993 | hor_overlap_inplace( | 1060 | hor_overlap_inplace( |
| 994 | &mut cr_line_buf, | 1061 | &mut cr_line_buf, |
| 995 | c_stride, | 1062 | c_stride, |
| 996 | 0, | 1063 | 0, |
| 997 | - x, | 1064 | + ccol0, |
| 998 | - &cr_col_buf, | 1065 | + &cr_template, |
| 999 | - 1, | 1066 | + CHROMA_BLOCK_W, |
| 1000 | - 0, | 1067 | + chroma_off_y, |
| 1001 | - 0, | 1068 | + chroma_off_x + if x != 0 { 1 } else { 0 }, |
| 1002 | - 1, | 1069 | + cw, |
| 1003 | 1, fctx, | 1070 | 1, fctx, |
| 1004 | ); | 1071 | ); |
| 1005 | - } | ||
| 1006 | - let lcol0 = if x != 0 { (x + 1) << 1 } else { 0 }; | ||
| 1007 | - let ccol0 = if x != 0 { x + 1 } else { 0 }; | ||
| 1008 | - let lw = (LUMA_SUBBLOCK - (if x != 0 { 2 } else { 0 })).min(width - lcol0); | ||
| 1009 | - hor_overlap_inplace( | ||
| 1010 | - &mut y_line_buf, | ||
| 1011 | - y_stride, | ||
| 1012 | - 0, | ||
| 1013 | - lcol0, | ||
| 1014 | - &luma_template, | ||
| 1015 | - LUMA_BLOCK_W, | ||
| 1016 | - luma_off_y, | ||
| 1017 | - luma_off_x + if x != 0 { 2 } else { 0 }, | ||
| 1018 | - lw, | ||
| 1019 | - 2, fctx, | ||
| 1020 | - ); | ||
| 1021 | - let cw = (CHROMA_SUBBLOCK - (if x != 0 { 1 } else { 0 })).min((width - lcol0) >> 1); | ||
| 1022 | - hor_overlap_inplace( | ||
| 1023 | - &mut cb_line_buf, | ||
| 1024 | - c_stride, | ||
| 1025 | - 0, | ||
| 1026 | - ccol0, | ||
| 1027 | - &cb_template, | ||
| 1028 | - CHROMA_BLOCK_W, | ||
| 1029 | - chroma_off_y, | ||
| 1030 | - chroma_off_x + if x != 0 { 1 } else { 0 }, | ||
| 1031 | - cw, | ||
| 1032 | - 1, fctx, | ||
| 1033 | - ); | ||
| 1034 | - hor_overlap_inplace( | ||
| 1035 | - &mut cr_line_buf, | ||
| 1036 | - c_stride, | ||
| 1037 | - 0, | ||
| 1038 | - ccol0, | ||
| 1039 | - &cr_template, | ||
| 1040 | - CHROMA_BLOCK_W, | ||
| 1041 | - chroma_off_y, | ||
| 1042 | - chroma_off_x + if x != 0 { 1 } else { 0 }, | ||
| 1043 | - cw, | ||
| 1044 | - 1, fctx, | ||
| 1045 | - ); | ||
| 1046 | 1072 | ||
| 1047 | - add_noise_to_block( | 1073 | + if !prelude { |
| 1048 | - fg, | 1074 | + add_noise_to_block( |
| 1049 | - mc_identity, | 1075 | + fg, |
| 1050 | - &scaling_y, | 1076 | + mc_identity, |
| 1051 | - &scaling_cb, | 1077 | + &scaling_y, |
| 1052 | - &scaling_cr, | 1078 | + &scaling_cb, |
| 1053 | - &mut out, | 1079 | + &scaling_cr, |
| 1054 | - y << 1, | 1080 | + &mut *out, |
| 1055 | - x << 1, | 1081 | + y << 1, |
| 1056 | - &y_line_buf, | 1082 | + x << 1, |
| 1057 | - y_stride, | 1083 | + &y_line_buf, |
| 1058 | - 0, | 1084 | + y_stride, |
| 1059 | - x << 1, | 1085 | + 0, |
| 1060 | - &cb_line_buf, | 1086 | + x << 1, |
| 1061 | - &cr_line_buf, | 1087 | + &cb_line_buf, |
| 1062 | - c_stride, | 1088 | + &cr_line_buf, |
| 1063 | - 0, | 1089 | + c_stride, |
| 1064 | - x, | 1090 | + 0, |
| 1065 | - 1, | 1091 | + x, |
| 1066 | - (LUMA_SUBBLOCK >> 1).min(width / 2 - x), fctx, | 1092 | + 1, |
| 1067 | - ); | 1093 | + (LUMA_SUBBLOCK >> 1).min(width / 2 - x), fctx, |
| 1068 | - } | 1094 | + ); |
| 1095 | + } | ||
| 1096 | + } | ||
| 1069 | 1097 | ||
| 1070 | - let i = if overlap && y != 0 { 1 } else { 0 }; | 1098 | + let i = if overlap && y != 0 { 1 } else { 0 }; |
| 1071 | - let j = if overlap && x != 0 { 1 } else { 0 }; | 1099 | + let j = if overlap && x != 0 { 1 } else { 0 }; |
| 1072 | - add_noise_to_block( | 1100 | + if !prelude { |
| 1073 | - fg, | 1101 | + add_noise_to_block( |
| 1074 | - mc_identity, | 1102 | + fg, |
| 1075 | - &scaling_y, | 1103 | + mc_identity, |
| 1076 | - &scaling_cb, | 1104 | + &scaling_y, |
| 1077 | - &scaling_cr, | 1105 | + &scaling_cb, |
| 1078 | - &mut out, | 1106 | + &scaling_cr, |
| 1079 | - (y + i) << 1, | 1107 | + &mut *out, |
| 1080 | - (x + j) << 1, | 1108 | + (y + i) << 1, |
| 1081 | - &luma_template, | 1109 | + (x + j) << 1, |
| 1082 | - LUMA_BLOCK_W, | 1110 | + &luma_template, |
| 1083 | - luma_off_y + (i << 1), | 1111 | + LUMA_BLOCK_W, |
| 1084 | - luma_off_x + (j << 1), | 1112 | + luma_off_y + (i << 1), |
| 1085 | - &cb_template, | 1113 | + luma_off_x + (j << 1), |
| 1086 | - &cr_template, | 1114 | + &cb_template, |
| 1087 | - CHROMA_BLOCK_W, | 1115 | + &cr_template, |
| 1088 | - chroma_off_y + i, | 1116 | + CHROMA_BLOCK_W, |
| 1089 | - chroma_off_x + j, | 1117 | + chroma_off_y + i, |
| 1090 | - (LUMA_SUBBLOCK >> 1).min(height / 2 - y) - i, | 1118 | + chroma_off_x + j, |
| 1091 | - (LUMA_SUBBLOCK >> 1).min(width / 2 - x) - j, fctx, | 1119 | + (LUMA_SUBBLOCK >> 1).min(height / 2 - y) - i, |
| 1092 | - ); | 1120 | + (LUMA_SUBBLOCK >> 1).min(width / 2 - x) - j, fctx, |
| 1121 | + ); | ||
| 1122 | + } | ||
| 1093 | 1123 | ||
| 1094 | - if overlap { | 1124 | + if overlap { |
| 1095 | - if x != 0 { | 1125 | + if x != 0 { |
| 1126 | + copy_area( | ||
| 1127 | + &mut y_line_buf, | ||
| 1128 | + y_stride, | ||
| 1129 | + 0, | ||
| 1130 | + x << 1, | ||
| 1131 | + &y_col_buf, | ||
| 1132 | + 2, | ||
| 1133 | + LUMA_SUBBLOCK, | ||
| 1134 | + 0, | ||
| 1135 | + 2, | ||
| 1136 | + 2, fctx, | ||
| 1137 | + ); | ||
| 1138 | + copy_area( | ||
| 1139 | + &mut cb_line_buf, | ||
| 1140 | + c_stride, | ||
| 1141 | + 0, | ||
| 1142 | + x, | ||
| 1143 | + &cb_col_buf, | ||
| 1144 | + 1, | ||
| 1145 | + CHROMA_SUBBLOCK, | ||
| 1146 | + 0, | ||
| 1147 | + 1, | ||
| 1148 | + 1, fctx, | ||
| 1149 | + ); | ||
| 1150 | + copy_area( | ||
| 1151 | + &mut cr_line_buf, | ||
| 1152 | + c_stride, | ||
| 1153 | + 0, | ||
| 1154 | + x, | ||
| 1155 | + &cr_col_buf, | ||
| 1156 | + 1, | ||
| 1157 | + CHROMA_SUBBLOCK, | ||
| 1158 | + 0, | ||
| 1159 | + 1, | ||
| 1160 | + 1, fctx, | ||
| 1161 | + ); | ||
| 1162 | + } | ||
| 1163 | + let lcol0 = if x != 0 { (x + 1) << 1 } else { 0 }; | ||
| 1164 | + let ccol0 = if x != 0 { x + 1 } else { 0 }; | ||
| 1165 | + let lcut = if x != 0 { 2 } else { 0 }; | ||
| 1166 | + let ccut = if x != 0 { 1 } else { 0 }; | ||
| 1096 | copy_area( | 1167 | copy_area( |
| 1097 | &mut y_line_buf, | 1168 | &mut y_line_buf, |
| 1098 | y_stride, | 1169 | y_stride, |
| 1099 | 0, | 1170 | 0, |
| 1100 | - x << 1, | 1171 | + lcol0, |
| 1101 | - &y_col_buf, | 1172 | + &luma_template, |
| 1102 | - 2, | 1173 | + LUMA_BLOCK_W, |
| 1103 | - LUMA_SUBBLOCK, | 1174 | + luma_off_y + LUMA_SUBBLOCK, |
| 1104 | - 0, | 1175 | + luma_off_x + lcut, |
| 1105 | - 2, | 1176 | + LUMA_SUBBLOCK.min(width - (x << 1)) - lcut, |
| 1106 | 2, fctx, | 1177 | 2, fctx, |
| 1107 | ); | 1178 | ); |
| 1108 | copy_area( | 1179 | copy_area( |
| 1109 | &mut cb_line_buf, | 1180 | &mut cb_line_buf, |
| 1110 | c_stride, | 1181 | c_stride, |
| 1111 | 0, | 1182 | 0, |
| 1112 | - x, | 1183 | + ccol0, |
| 1113 | - &cb_col_buf, | 1184 | + &cb_template, |
| 1114 | - 1, | 1185 | + CHROMA_BLOCK_W, |
| 1115 | - CHROMA_SUBBLOCK, | 1186 | + chroma_off_y + CHROMA_SUBBLOCK, |
| 1116 | - 0, | 1187 | + chroma_off_x + ccut, |
| 1117 | - 1, | 1188 | + CHROMA_SUBBLOCK.min((width - (x << 1)) >> 1) - ccut, |
| 1118 | 1, fctx, | 1189 | 1, fctx, |
| 1119 | ); | 1190 | ); |
| 1120 | copy_area( | 1191 | copy_area( |
| 1121 | &mut cr_line_buf, | 1192 | &mut cr_line_buf, |
| 1122 | c_stride, | 1193 | c_stride, |
| 1123 | 0, | 1194 | 0, |
| 1124 | - x, | 1195 | + ccol0, |
| 1125 | - &cr_col_buf, | 1196 | + &cr_template, |
| 1197 | + CHROMA_BLOCK_W, | ||
| 1198 | + chroma_off_y + CHROMA_SUBBLOCK, | ||
| 1199 | + chroma_off_x + ccut, | ||
| 1200 | + CHROMA_SUBBLOCK.min((width - (x << 1)) >> 1) - ccut, | ||
| 1201 | + 1, fctx, | ||
| 1202 | + ); | ||
| 1203 | + | ||
| 1204 | + let h_count = (LUMA_SUBBLOCK + 2).min(height - (y << 1)); | ||
| 1205 | + copy_area( | ||
| 1206 | + &mut y_col_buf, | ||
| 1207 | + 2, | ||
| 1208 | + 0, | ||
| 1209 | + 0, | ||
| 1210 | + &luma_template, | ||
| 1211 | + LUMA_BLOCK_W, | ||
| 1212 | + luma_off_y, | ||
| 1213 | + luma_off_x + LUMA_SUBBLOCK, | ||
| 1214 | + 2, | ||
| 1215 | + h_count, fctx, | ||
| 1216 | + ); | ||
| 1217 | + let hc_count = (CHROMA_SUBBLOCK + 1).min((height - (y << 1)) >> 1); | ||
| 1218 | + copy_area( | ||
| 1219 | + &mut cb_col_buf, | ||
| 1126 | 1, | 1220 | 1, |
| 1127 | - CHROMA_SUBBLOCK, | ||
| 1128 | 0, | 1221 | 0, |
| 1222 | + 0, | ||
| 1223 | + &cb_template, | ||
| 1224 | + CHROMA_BLOCK_W, | ||
| 1225 | + chroma_off_y, | ||
| 1226 | + chroma_off_x + CHROMA_SUBBLOCK, | ||
| 1129 | 1, | 1227 | 1, |
| 1130 | - 1, fctx, | 1228 | + hc_count, fctx, |
| 1229 | + ); | ||
| 1230 | + copy_area( | ||
| 1231 | + &mut cr_col_buf, | ||
| 1232 | + 1, | ||
| 1233 | + 0, | ||
| 1234 | + 0, | ||
| 1235 | + &cr_template, | ||
| 1236 | + CHROMA_BLOCK_W, | ||
| 1237 | + chroma_off_y, | ||
| 1238 | + chroma_off_x + CHROMA_SUBBLOCK, | ||
| 1239 | + 1, | ||
| 1240 | + hc_count, fctx, | ||
| 1131 | ); | 1241 | ); |
| 1132 | } | 1242 | } |
| 1133 | - let lcol0 = if x != 0 { (x + 1) << 1 } else { 0 }; | ||
| 1134 | - let ccol0 = if x != 0 { x + 1 } else { 0 }; | ||
| 1135 | - let lcut = if x != 0 { 2 } else { 0 }; | ||
| 1136 | - let ccut = if x != 0 { 1 } else { 0 }; | ||
| 1137 | - copy_area( | ||
| 1138 | - &mut y_line_buf, | ||
| 1139 | - y_stride, | ||
| 1140 | - 0, | ||
| 1141 | - lcol0, | ||
| 1142 | - &luma_template, | ||
| 1143 | - LUMA_BLOCK_W, | ||
| 1144 | - luma_off_y + LUMA_SUBBLOCK, | ||
| 1145 | - luma_off_x + lcut, | ||
| 1146 | - LUMA_SUBBLOCK.min(width - (x << 1)) - lcut, | ||
| 1147 | - 2, fctx, | ||
| 1148 | - ); | ||
| 1149 | - copy_area( | ||
| 1150 | - &mut cb_line_buf, | ||
| 1151 | - c_stride, | ||
| 1152 | - 0, | ||
| 1153 | - ccol0, | ||
| 1154 | - &cb_template, | ||
| 1155 | - CHROMA_BLOCK_W, | ||
| 1156 | - chroma_off_y + CHROMA_SUBBLOCK, | ||
| 1157 | - chroma_off_x + ccut, | ||
| 1158 | - CHROMA_SUBBLOCK.min((width - (x << 1)) >> 1) - ccut, | ||
| 1159 | - 1, fctx, | ||
| 1160 | - ); | ||
| 1161 | - copy_area( | ||
| 1162 | - &mut cr_line_buf, | ||
| 1163 | - c_stride, | ||
| 1164 | - 0, | ||
| 1165 | - ccol0, | ||
| 1166 | - &cr_template, | ||
| 1167 | - CHROMA_BLOCK_W, | ||
| 1168 | - chroma_off_y + CHROMA_SUBBLOCK, | ||
| 1169 | - chroma_off_x + ccut, | ||
| 1170 | - CHROMA_SUBBLOCK.min((width - (x << 1)) >> 1) - ccut, | ||
| 1171 | - 1, fctx, | ||
| 1172 | - ); | ||
| 1173 | 1243 | ||
| 1174 | - let h_count = (LUMA_SUBBLOCK + 2).min(height - (y << 1)); | 1244 | + x += LUMA_SUBBLOCK >> 1; |
| 1175 | - copy_area( | ||
| 1176 | - &mut y_col_buf, | ||
| 1177 | - 2, | ||
| 1178 | - 0, | ||
| 1179 | - 0, | ||
| 1180 | - &luma_template, | ||
| 1181 | - LUMA_BLOCK_W, | ||
| 1182 | - luma_off_y, | ||
| 1183 | - luma_off_x + LUMA_SUBBLOCK, | ||
| 1184 | - 2, | ||
| 1185 | - h_count, fctx, | ||
| 1186 | - ); | ||
| 1187 | - let hc_count = (CHROMA_SUBBLOCK + 1).min((height - (y << 1)) >> 1); | ||
| 1188 | - copy_area( | ||
| 1189 | - &mut cb_col_buf, | ||
| 1190 | - 1, | ||
| 1191 | - 0, | ||
| 1192 | - 0, | ||
| 1193 | - &cb_template, | ||
| 1194 | - CHROMA_BLOCK_W, | ||
| 1195 | - chroma_off_y, | ||
| 1196 | - chroma_off_x + CHROMA_SUBBLOCK, | ||
| 1197 | - 1, | ||
| 1198 | - hc_count, fctx, | ||
| 1199 | - ); | ||
| 1200 | - copy_area( | ||
| 1201 | - &mut cr_col_buf, | ||
| 1202 | - 1, | ||
| 1203 | - 0, | ||
| 1204 | - 0, | ||
| 1205 | - &cr_template, | ||
| 1206 | - CHROMA_BLOCK_W, | ||
| 1207 | - chroma_off_y, | ||
| 1208 | - chroma_off_x + CHROMA_SUBBLOCK, | ||
| 1209 | - 1, | ||
| 1210 | - hc_count, fctx, | ||
| 1211 | - ); | ||
| 1212 | } | 1245 | } |
| 1213 | - | 1246 | + y += row_step; |
| 1214 | - x += LUMA_SUBBLOCK >> 1; | ||
| 1215 | } | 1247 | } |
| 1216 | - y += LUMA_SUBBLOCK >> 1; | 1248 | + }); |
| 1217 | - } | 1249 | + drop(so); |
| 1218 | 1250 | ||
| 1219 | out | 1251 | out |
| 1220 | } | 1252 | } |
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
| @@ -1074,8 +1074,13 @@ pub(crate) fn apply_loop_restoration_plane( | |||
| 1074 | } | 1074 | } |
| 1075 | let voffset = 8u32 >> ss_y; | 1075 | let voffset = 8u32 >> ss_y; |
| 1076 | let ext_size = (unit_size * 3) / 2; | 1076 | let ext_size = (unit_size * 3) / 2; |
| 1077 | + // lane-filt1: the RU row walk is data-dependent (a short remainder is | ||
| 1078 | + // swallowed by the previous unit row), so resolve it into a row list | ||
| 1079 | + // first. Each row writes exactly `v_start..v_end` of `out` and the next | ||
| 1080 | + // row's `v_start` is this one's `v_end`, so row bands are disjoint; the | ||
| 1081 | + // reads are all from `cdef`/`deblocked`, immutable snapshots. | ||
| 1082 | + let mut rows: Vec<(u32, u32)> = Vec::new(); | ||
| 1077 | let mut y0 = 0u32; | 1083 | let mut y0 = 0u32; |
| 1078 | - let mut rrow = 0usize; | ||
| 1079 | while y0 < plane_h as u32 { | 1084 | while y0 < plane_h as u32 { |
| 1080 | let remaining_h = plane_h as u32 - y0; | 1085 | let remaining_h = plane_h as u32 - y0; |
| 1081 | let uh = if remaining_h < ext_size { remaining_h } else { unit_size }; | 1086 | let uh = if remaining_h < ext_size { remaining_h } else { unit_size }; |
| @@ -1084,35 +1089,46 @@ pub(crate) fn apply_loop_restoration_plane( | |||
| 1084 | if v_end < plane_h as u32 { | 1089 | if v_end < plane_h as u32 { |
| 1085 | v_end -= voffset; | 1090 | v_end -= voffset; |
| 1086 | } | 1091 | } |
| 1087 | - | 1092 | + rows.push((v_start, v_end)); |
| 1088 | - let mut x0 = 0u32; | ||
| 1089 | - let mut rcol = 0usize; | ||
| 1090 | - while x0 < plane_w as u32 { | ||
| 1091 | - let remaining_w = plane_w as u32 - x0; | ||
| 1092 | - let uw = if remaining_w < ext_size { remaining_w } else { unit_size }; | ||
| 1093 | - let filter = grid.get(plane, rrow, rcol); | ||
| 1094 | - if filter != UnitFilter::None { | ||
| 1095 | - filter_restoration_unit( | ||
| 1096 | - out, | ||
| 1097 | - cdef, | ||
| 1098 | - deblocked, | ||
| 1099 | - stride, | ||
| 1100 | - plane_w, | ||
| 1101 | - plane_h, | ||
| 1102 | - x0 as usize, | ||
| 1103 | - (x0 + uw) as usize, | ||
| 1104 | - v_start as usize, | ||
| 1105 | - v_end as usize, | ||
| 1106 | - ss_y, | ||
| 1107 | - filter, fctx, | ||
| 1108 | - ); | ||
| 1109 | - } | ||
| 1110 | - x0 += uw; | ||
| 1111 | - rcol += 1; | ||
| 1112 | - } | ||
| 1113 | y0 += uh; | 1093 | y0 += uh; |
| 1114 | - rrow += 1; | ||
| 1115 | } | 1094 | } |
| 1095 | + let rrow_count = rows.len(); | ||
| 1096 | + let so = crate::par::Shared::new(out); | ||
| 1097 | + let rbands = crate::par::bands(rrow_count, crate::par::filter_threads()); | ||
| 1098 | + crate::par::run_bands(&rbands, fctx, |r_lo, r_hi, fctx| { | ||
| 1099 | + // SAFETY: this band writes only the `out` rows its own RU rows cover. | ||
| 1100 | + #[allow(unsafe_code)] | ||
| 1101 | + let out = unsafe { so.get() }; | ||
| 1102 | + for rrow in r_lo..r_hi { | ||
| 1103 | + let (v_start, v_end) = rows[rrow]; | ||
| 1104 | + let mut x0 = 0u32; | ||
| 1105 | + let mut rcol = 0usize; | ||
| 1106 | + while x0 < plane_w as u32 { | ||
| 1107 | + let remaining_w = plane_w as u32 - x0; | ||
| 1108 | + let uw = if remaining_w < ext_size { remaining_w } else { unit_size }; | ||
| 1109 | + let filter = grid.get(plane, rrow, rcol); | ||
| 1110 | + if filter != UnitFilter::None { | ||
| 1111 | + filter_restoration_unit( | ||
| 1112 | + out, | ||
| 1113 | + cdef, | ||
| 1114 | + deblocked, | ||
| 1115 | + stride, | ||
| 1116 | + plane_w, | ||
| 1117 | + plane_h, | ||
| 1118 | + x0 as usize, | ||
| 1119 | + (x0 + uw) as usize, | ||
| 1120 | + v_start as usize, | ||
| 1121 | + v_end as usize, | ||
| 1122 | + ss_y, | ||
| 1123 | + filter, fctx, | ||
| 1124 | + ); | ||
| 1125 | + } | ||
| 1126 | + x0 += uw; | ||
| 1127 | + rcol += 1; | ||
| 1128 | + } | ||
| 1129 | + } | ||
| 1130 | + }); | ||
| 1131 | + let rrow = rrow_count; | ||
| 1116 | debug_assert_eq!(rrow, grid.vert_units[plane], "RU row walk must match RestorationGrid::new's count_units"); | 1132 | debug_assert_eq!(rrow, grid.vert_units[plane], "RU row walk must match RestorationGrid::new's count_units"); |
| 1117 | } | 1133 | } |
| 1118 | 1134 | ||
crates/ec-av1/src/stream.rsmodified+69 -0
| @@ -5816,6 +5816,75 @@ mod tests { | |||
| 5816 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg"); | 5816 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg"); |
| 5817 | } | 5817 | } |
| 5818 | 5818 | ||
| 5819 | + /// lane-filt1: `EC_AV1_FILTER_THREADS` splits every in-frame filter stage | ||
| 5820 | + /// -- deblock (row bands for the vertical-edge pass, column bands for the | ||
| 5821 | + /// horizontal one), CDEF (mi row bands), loop restoration (RU row bands) | ||
| 5822 | + /// and film grain (32-pixel block rows) -- across scoped worker threads. | ||
| 5823 | + /// A split is only legal if it is BYTE-EXACT, so this gate decodes two | ||
| 5824 | + /// real aomenc streams at 1 and at 4 filter threads and compares every | ||
| 5825 | + /// output frame's three planes. | ||
| 5826 | + /// | ||
| 5827 | + /// Between them the fixtures fire all four stages, and the firing | ||
| 5828 | + /// counters are asserted rather than assumed (class `gate-blind-to-feature`): | ||
| 5829 | + /// `superres_alltools_sb128_320x180.obu` is the 10-bit CDEF + loop | ||
| 5830 | + /// restoration stream, `grain_cdef_lr_128x128.obu` a 128x128 | ||
| 5831 | + /// `--denoise-noise-level=25` stream whose frames carry film grain | ||
| 5832 | + /// parameters (small enough that 4 bands is one block row each, which is | ||
| 5833 | + /// what exercises the grain band's one-row `prelude` replay). | ||
| 5834 | + #[test] | ||
| 5835 | + fn a_real_stream_filters_identically_with_one_and_four_filter_threads() { | ||
| 5836 | + const NAME: &str = "a_real_stream_filters_identically_with_one_and_four_filter_threads"; | ||
| 5837 | + let _gate_lock = lock_gate_counters(); | ||
| 5838 | + for (fixture, wants_lr, wants_grain) in [ | ||
| 5839 | + ("superres_alltools_sb128_320x180.obu", true, false), | ||
| 5840 | + ("grain_cdef_lr_128x128.obu", false, true), | ||
| 5841 | + ] { | ||
| 5842 | + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) | ||
| 5843 | + .join("fixtures") | ||
| 5844 | + .join(fixture); | ||
| 5845 | + let stream = std::fs::read(&path) | ||
| 5846 | + .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display())); | ||
| 5847 | + let before = ( | ||
| 5848 | + crate::decode::deblock_hits(), | ||
| 5849 | + crate::restoration::wiener_hits() + crate::restoration::sgrproj_hits(), | ||
| 5850 | + crate::film_grain::grain_hits(), | ||
| 5851 | + ); | ||
| 5852 | + crate::par::set_filter_threads(1); | ||
| 5853 | + let one = match decode_stream(&stream) { | ||
| 5854 | + Ok(frames) => frames, | ||
| 5855 | + Err(e) => panic!("{NAME}: {fixture} refused at 1 filter thread: {e}"), | ||
| 5856 | + }; | ||
| 5857 | + let after = ( | ||
| 5858 | + crate::decode::deblock_hits(), | ||
| 5859 | + crate::restoration::wiener_hits() + crate::restoration::sgrproj_hits(), | ||
| 5860 | + crate::film_grain::grain_hits(), | ||
| 5861 | + ); | ||
| 5862 | + crate::par::set_filter_threads(4); | ||
| 5863 | + let four = match decode_stream(&stream) { | ||
| 5864 | + Ok(frames) => frames, | ||
| 5865 | + Err(e) => { | ||
| 5866 | + crate::par::set_filter_threads(1); | ||
| 5867 | + panic!("{NAME}: {fixture} refused at 4 filter threads: {e}") | ||
| 5868 | + } | ||
| 5869 | + }; | ||
| 5870 | + crate::par::set_filter_threads(1); | ||
| 5871 | + assert!(after.0 > before.0, "{NAME}: {fixture} deblocked no edge at all"); | ||
| 5872 | + if wants_lr { | ||
| 5873 | + assert!(after.1 > before.1, "{NAME}: {fixture} ran no restoration unit"); | ||
| 5874 | + } | ||
| 5875 | + if wants_grain { | ||
| 5876 | + assert!(after.2 > before.2, "{NAME}: {fixture} applied no film grain"); | ||
| 5877 | + } | ||
| 5878 | + assert_eq!(one.len(), four.len(), "{NAME}: {fixture} frame count"); | ||
| 5879 | + assert!(!one.is_empty(), "{NAME}: {fixture} decoded nothing"); | ||
| 5880 | + for (i, (a, b)) in one.iter().zip(four.iter()).enumerate() { | ||
| 5881 | + assert_eq!(a.y, b.y, "{NAME}: {fixture} frame {i} luma differs at 4 filter threads"); | ||
| 5882 | + assert_eq!(a.u, b.u, "{NAME}: {fixture} frame {i} U differs at 4 filter threads"); | ||
| 5883 | + assert_eq!(a.v, b.v, "{NAME}: {fixture} frame {i} V differs at 4 filter threads"); | ||
| 5884 | + } | ||
| 5885 | + } | ||
| 5886 | + } | ||
| 5887 | + | ||
| 5819 | /// lane-t900 r30b: `fixtures/superres_alltools_sb128_320x180.obu` is 10 | 5888 | /// lane-t900 r30b: `fixtures/superres_alltools_sb128_320x180.obu` is 10 |
| 5820 | /// frames of `--cpu-used=4 --sb-size=128 --superres-mode=1 | 5889 | /// frames of `--cpu-used=4 --sb-size=128 --superres-mode=1 |
| 5821 | /// --superres-denominator=12 --cq-level=32` with every tool aomenc turns | 5890 | /// --superres-denominator=12 --cq-level=32` with every tool aomenc turns |