diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 0e69ec8d..9886a692 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -7422,7 +7422,7 @@ fn tx_size_context_rect( } /// [`cfl_ac_q3`] for a true `bw`x`bh` rect strip (lane-intradisp r1). -fn cfl_ac_q3_rect(y: &PlaneBuf, px: usize, py: usize, bw: usize, bh: usize) -> Vec { +fn cfl_ac_q3_rect(y: &PlaneBuf<'_>, px: usize, py: usize, bw: usize, bh: usize) -> Vec { let (cw, ch) = (bw / 2, bh / 2); let mut ac = vec![0i32; cw * ch]; let mut sum = 0i32; @@ -7526,9 +7526,9 @@ fn decode_rect_split( tx_w: usize, tx_h: usize, m: &RectStripModes, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, base_q_idx: u8, reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, ) -> Result<()> { @@ -8155,9 +8155,9 @@ fn decode_intra_rect_in_inter( // `(PARTITION_HORZ_4, is_chroma_reference)` when this strip is one of the // four 16x4 / 4x16 strips of a 16x16-level 1:4 partition (lane-intra16x4). strip16: Option<(bool, bool)>, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, base_q_idx: u8, fctx: &crate::decode::FrameCtx, ) -> Result<()> { // `at` is in MI units (the inter path's own unit since lane-inter4); @@ -8708,9 +8708,9 @@ fn decode_block_rect( at: (usize, usize), bw: usize, bh: usize, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, enable_filter_intra: bool, allow_screen_content_tools: bool, base_q_idx: u8, @@ -9164,9 +9164,9 @@ fn decode_leaf_rect( bw: usize, bh: usize, prev_leaf: Option<((usize, usize), usize)>, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, enable_filter_intra: bool, allow_screen_content_tools: bool, base_q_idx: u8, @@ -9504,9 +9504,9 @@ fn decode_block_rect4( bw: usize, bh: usize, prev_strip: Option<((usize, usize), usize)>, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, enable_filter_intra: bool, allow_screen_content_tools: bool, base_q_idx: u8, @@ -9973,9 +9973,9 @@ fn decode_rect4_16( neighbours: &mut Neighbours, at16: (usize, usize), horz: bool, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, enable_filter_intra: bool, allow_screen_content_tools: bool, base_q_idx: u8, @@ -10049,9 +10049,9 @@ fn decode_rect4_16_strip( horz: bool, has_chroma: bool, prev: Option<((usize, usize), usize)>, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, enable_filter_intra: bool, allow_screen_content_tools: bool, base_q_idx: u8, @@ -10551,9 +10551,9 @@ fn decode_block_rect64( at: (usize, usize), bw: usize, bh: usize, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, enable_filter_intra: bool, allow_screen_content_tools: bool, base_q_idx: u8, @@ -11471,7 +11471,7 @@ fn read_cfl_alphas(dec: &mut SymbolDecoder, cdfs: &mut Cdfs) -> (i32, i32) { /// 4:2:0 to Q3 (`(a+b+c+d) << 1`, each 2x2 group), then block-average /// subtracted (`round_offset = num_pel/2`, right-shifted by `log2(num_pel)`) /// to give the AC values [`cfl_scaled`] scales by alpha. -fn cfl_ac_q3(y: &PlaneBuf, px: usize, py: usize, side: usize) -> Vec { +fn cfl_ac_q3(y: &PlaneBuf<'_>, px: usize, py: usize, side: usize) -> Vec { let cside = side / 2; let mut ac = vec![0i32; cside * cside]; let mut sum = 0i32; @@ -11525,7 +11525,7 @@ fn stage_dump_idx() -> usize { PREFILT_PICTURE_IDX.load(std::sync::atomic::Ordering::SeqCst).saturating_sub(1) } -fn dump_stage16(var: &str, y: &PlaneBuf, u: &PlaneBuf, v: &PlaneBuf, fw: usize, fh: usize) { +fn dump_stage16(var: &str, y: &PlaneBuf<'_>, u: &PlaneBuf<'_>, v: &PlaneBuf<'_>, fw: usize, fh: usize) { use std::io::Write; // lane-sbrect10 r2 / lane-cdef r1 (one shared helper): index the dump by // decode-order picture like EC_AV1_PREFILT_DUMP already does -- a fixed @@ -11561,7 +11561,7 @@ fn dump_stage_idx(var: &str) -> usize { i } -fn dump_stage(var: &str, y: &PlaneBuf, u: &PlaneBuf, v: &PlaneBuf) { +fn dump_stage(var: &str, y: &PlaneBuf<'_>, u: &PlaneBuf<'_>, v: &PlaneBuf<'_>) { use std::io::Write; if let Ok(path) = crate::envflags::var(var) && let Ok(mut f) = std::fs::File::create(format!("{path}.f{}", dump_stage_idx(var))) @@ -11574,8 +11574,8 @@ fn dump_stage(var: &str, y: &PlaneBuf, u: &PlaneBuf, v: &PlaneBuf) { } #[derive(Clone)] -struct PlaneBuf { - data: Vec, +struct PlaneBuf<'a> { + data: std::borrow::Cow<'a, [u16]>, width: usize, height: usize, /// The frame's true, decodable extent in this plane's own units — past @@ -11608,9 +11608,9 @@ struct PlaneBuf { /// (lane-av1refs: the same "empty slot" case `GOLDEN_FRAME` already /// refused by name, now generic across `LAST2`/`LAST3`/`GOLDEN`/`BWDREF`/ /// `ALTREF2`/`ALTREF`). -type RefSlots<'a> = [Option<(&'a PlaneBuf, &'a PlaneBuf, &'a PlaneBuf)>; 8]; +type RefSlots<'a> = [Option<(&'a PlaneBuf<'a>, &'a PlaneBuf<'a>, &'a PlaneBuf<'a>)>; 8]; -impl PlaneBuf { +impl PlaneBuf<'_> { /// Sets this plane's own tile pixel origin ([`Self::tile_x0`]/ /// [`Self::tile_y0`]) before that tile's own superblock walk. `x0`/`y0` /// are already in this plane's own units (luma pixels for `y`, chroma @@ -11792,7 +11792,7 @@ impl PlaneBuf { base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max(fctx)); } let sample = (base + residual[idx]).clamp(0, sample_max(fctx)) as u16; - self.data[(y + row) * self.width + x + col] = sample; + self.data.to_mut()[(y + row) * self.width + x + col] = sample; } } } @@ -11899,7 +11899,7 @@ impl PlaneBuf { base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max(fctx)); } let sample = (base + residual[idx]).clamp(0, sample_max(fctx)) as u16; - self.data[(y + row) * self.width + x + col] = sample; + self.data.to_mut()[(y + row) * self.width + x + col] = sample; } } } @@ -11922,7 +11922,7 @@ fn read_plane( predict_mode: usize, angle_delta: i32, reach: Reach, - plane: &mut PlaneBuf, + plane: &mut PlaneBuf<'static>, x: usize, y: usize, side: usize, @@ -12062,9 +12062,9 @@ fn decode_block( chroma_tx: usize, scans: (&[u16], &[u16]), cfl: bool, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, base_q_idx: u8, enable_filter_intra: bool, allow_screen_content_tools: bool, @@ -12807,9 +12807,9 @@ fn decode_block_128rect( at: (usize, usize), bw: usize, bh: usize, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, base_q_idx: u8, enable_filter_intra: bool, allow_screen_content_tools: bool, @@ -13128,9 +13128,9 @@ fn decode_leaf8( leaf_mi: (usize, usize), scans: (&[u16], &[u16]), prev_leaf: Option<((usize, usize), usize)>, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, base_q_idx: u8, enable_filter_intra: bool, allow_screen_content_tools: bool, @@ -13844,9 +13844,9 @@ fn decode_leaf_split4( leaf_mi: (usize, usize), scan4: &[u16], prev_leaf: Option<((usize, usize), usize)>, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, base_q_idx: u8, enable_filter_intra: bool, allow_intrabc: bool, @@ -14064,9 +14064,9 @@ fn decode_leaf_rect8( leaf_mi: (usize, usize), vert: bool, scan4: &[u16], - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, base_q_idx: u8, enable_filter_intra: bool, allow_intrabc: bool, @@ -14574,7 +14574,7 @@ fn cdef_adjust_strength(strength: i32, var: i32) -> i32 { #[allow(unsafe_code)] fn cdef_filter_block( src: &[i32], - dst: &mut PlaneBuf, + dst: &mut PlaneBuf<'static>, ox: usize, oy: usize, bw: usize, @@ -14598,7 +14598,7 @@ fn cdef_filter_block( unsafe { cdef_simd::filter_block_avx2( src, - &mut dst.data, + dst.data.to_mut(), stride, ox, oy, @@ -14619,7 +14619,7 @@ fn cdef_filter_block( } cdef_filter_block_scalar( src, - &mut dst.data, + dst.data.to_mut(), stride, ox, oy, @@ -14949,9 +14949,9 @@ mod cdef_simd { /// Ported from libaom's `av1/common/av1_loopfilter.c` (edge/level decision) /// and `aom_dsp/loopfilter.c` (the `aom_lpf_*` pixel kernels). fn apply_deblock( - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, lf: &LoopFilterParams, n: &Neighbours, frame_width: usize, @@ -16138,7 +16138,7 @@ fn read_inter_plane_rect( plane_idx: usize, around: (bool, bool, i32), tx_mode: usize, - plane: &mut PlaneBuf, + plane: &mut PlaneBuf<'static>, x: usize, y: usize, prediction: &[u16], @@ -16258,7 +16258,7 @@ fn read_inter_luma8( cdfs: &mut Cdfs, neighbours: &mut Neighbours, leaf_mi: (usize, usize), - y: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, px: usize, py: usize, pred_y: &[u16], @@ -17304,7 +17304,7 @@ fn filter_edge16( /// One plane's worth of spec 7.14: every vertical edge across the plane, /// then every horizontal edge (the order the spec and libaom both use). fn deblock_plane( - plane: &mut PlaneBuf, + plane: &mut PlaneBuf<'static>, plane_idx: usize, lf: &LoopFilterParams, n: &Neighbours, @@ -17364,6 +17364,10 @@ fn deblock_plane( // group, whether it goes through `filter_edge16` with three neighbours or // through `filter_edge` alone. Band boundaries are multiples of 16 so no // group ever straddles one. + // lane-perf10 merge: the output plane is owned (perf10 made mutable + // plane params PlaneBuf<'static>); force the Cow to Owned ONCE here so + // the bands below never clone it concurrently. + plane.data.to_mut(); let threads = crate::par::filter_threads(); let sp = crate::par::Shared::new(plane); let vbands: Vec<(usize, usize)> = crate::par::bands(th.div_ceil(16), threads) @@ -17385,7 +17389,7 @@ fn deblock_plane( if let Some((len, level)) = ps[0] { if ps.iter().all(|p| *p == ps[0]) && filter_edge16( - &mut plane.data, + plane.data.to_mut(), y0 * stride + x0, stride, true, @@ -17401,7 +17405,7 @@ fn deblock_plane( for (k, p) in ps.iter().enumerate() { if let Some((len, level)) = *p { filter_edge( - &mut plane.data, + plane.data.to_mut(), (y0 + 4 * k) * stride + x0, stride as isize, 1, @@ -17419,7 +17423,7 @@ fn deblock_plane( eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}"); } filter_edge( - &mut plane.data, + plane.data.to_mut(), y0 * stride + x0, stride as isize, 1, @@ -17453,7 +17457,7 @@ fn deblock_plane( if let Some((len, level)) = ps[0] { if ps.iter().all(|p| *p == ps[0]) && filter_edge16( - &mut plane.data, + plane.data.to_mut(), y0 * stride + x0, stride, false, @@ -17469,7 +17473,7 @@ fn deblock_plane( for (k, p) in ps.iter().enumerate() { if let Some((len, level)) = *p { filter_edge( - &mut plane.data, + plane.data.to_mut(), y0 * stride + x0 + 4 * k, 1, stride as isize, @@ -17493,7 +17497,7 @@ fn deblock_plane( ); } filter_edge( - &mut plane.data, + plane.data.to_mut(), y0 * stride + x0, 1, stride as isize, @@ -17536,8 +17540,8 @@ fn scratch_empty() -> Vec { /// A plane's post-deblock snapshot, its samples taken from the pool (every /// other field is `usize`). -fn plane_snapshot(p: &PlaneBuf) -> PlaneBuf { - PlaneBuf { data: scratch_from(&p.data), ..*p } +fn plane_snapshot(p: &PlaneBuf<'_>) -> PlaneBuf<'static> { + PlaneBuf { data: std::borrow::Cow::Owned(scratch_from(&p.data)), ..*p } } /// Hands a buffer back to the pool; the capacity is what is worth keeping. @@ -17546,9 +17550,9 @@ fn scratch_return(v: Vec) { } fn apply_cdef( - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, cdef: &CdefParams, skip_grid: &Neighbours, // This frame header's own CROPPED luma size, for the straddling-unit @@ -17789,12 +17793,12 @@ fn apply_cdef( /// CDEF-filtered plane, per `restoration.rs`'s own `lr_sample` doc comment. #[allow(clippy::too_many_arguments)] fn apply_loop_restoration( - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, - deblocked_y: &PlaneBuf, - deblocked_u: &PlaneBuf, - deblocked_v: &PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, + deblocked_y: &PlaneBuf<'_>, + deblocked_u: &PlaneBuf<'_>, + deblocked_v: &PlaneBuf<'_>, lr: &LoopRestorationParams, grid: &crate::restoration::RestorationGrid, // lane-golomb r8: this frame header's own (cropped) luma size. Loop @@ -17829,8 +17833,7 @@ fn apply_loop_restoration( 0, &mut out, fctx, ); - std::mem::swap(&mut y.data, &mut out); - scratch_return(out); + scratch_return(std::mem::replace(&mut y.data, std::borrow::Cow::Owned(out)).into_owned()); let mut out = scratch_empty(); crate::restoration::apply_loop_restoration_plane( &u.data, @@ -17845,8 +17848,7 @@ fn apply_loop_restoration( 1, &mut out, fctx, ); - std::mem::swap(&mut u.data, &mut out); - scratch_return(out); + scratch_return(std::mem::replace(&mut u.data, std::borrow::Cow::Owned(out)).into_owned()); let mut out = scratch_empty(); crate::restoration::apply_loop_restoration_plane( &v.data, @@ -17861,8 +17863,7 @@ fn apply_loop_restoration( 2, &mut out, fctx, ); - std::mem::swap(&mut v.data, &mut out); - scratch_return(out); + scratch_return(std::mem::replace(&mut v.data, std::borrow::Cow::Owned(out)).into_owned()); } /// Decodes the payload [`crate::tile::sb_coeff_key_frame_tile`] writes, @@ -18232,7 +18233,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( let (width, height) = (cols32 as usize * BLOCK, rows32 as usize * BLOCK); let mut y = PlaneBuf { - data: vec![0u16; width * height], + data: std::borrow::Cow::Owned(vec![0u16; width * height]), width, height, true_width, @@ -18243,7 +18244,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( tile_y1: height, }; let mut u = PlaneBuf { - data: vec![0u16; width * height / 4], + data: std::borrow::Cow::Owned(vec![0u16; width * height / 4]), width: width / 2, height: height / 2, true_width: true_width / 2, @@ -18254,7 +18255,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( tile_y1: height / 2, }; let mut v = PlaneBuf { - data: vec![0u16; width * height / 4], + data: std::borrow::Cow::Owned(vec![0u16; width * height / 4]), width: width / 2, height: height / 2, true_width: true_width / 2, @@ -20130,7 +20131,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( use std::io::Write; let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { - let narrow = |p: &PlaneBuf| -> Vec { p.data.iter().map(|&s| s as u8).collect() }; + let narrow = |p: &PlaneBuf<'_>| -> Vec { p.data.iter().map(|&s| s as u8).collect() }; let _ = f.write_all(&narrow(&y)); let _ = f.write_all(&narrow(&u)); let _ = f.write_all(&narrow(&v)); @@ -20142,7 +20143,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( use std::io::Write; static IDX2: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); let idx = IDX2.fetch_add(1, std::sync::atomic::Ordering::SeqCst); - let crop_wide = |plane: &PlaneBuf| -> Vec { + let crop_wide = |plane: &PlaneBuf<'_>| -> Vec { let mut out = Vec::with_capacity(plane.true_width * plane.true_height); for row in 0..plane.true_height { out.extend(plane.data[row * plane.width..][..plane.true_width].iter().map(|&s| s as u8)); @@ -20204,10 +20205,10 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( if let Some((upscaled_w, _)) = superres(fctx) { let bd = u32::from(bit_depth(fctx)); let (uw, fw, fh) = (upscaled_w as usize, frame_width as usize, frame_height as usize); - let up = |p: &PlaneBuf, in_w: usize, in_h: usize, out_w: usize| PlaneBuf { - data: crate::superres::upscale_plane_strided( + let up = |p: &PlaneBuf<'_>, in_w: usize, in_h: usize, out_w: usize| PlaneBuf { + data: std::borrow::Cow::Owned(crate::superres::upscale_plane_strided( &p.data, p.width, in_w, in_h, p.true_width, out_w, bd, - ), + )), width: out_w, height: in_h, true_width: out_w, @@ -20234,9 +20235,9 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( ); } if let Some((db_y, db_u, db_v)) = deblocked { - scratch_return(db_y.data); - scratch_return(db_u.data); - scratch_return(db_v.data); + scratch_return(db_y.data.into_owned()); + scratch_return(db_u.data.into_owned()); + scratch_return(db_v.data.into_owned()); } let (fw, fh) = (lr_w, frame_height as usize); @@ -20254,14 +20255,14 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( Picture { width, height, - y: y.data, - u: u.data, - v: v.data, + y: y.data.into_owned(), + u: u.data.into_owned(), + v: v.data.into_owned(), }, result_cdfs, )); } - let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec { + let crop = |plane: &PlaneBuf<'_>, w: usize, h: usize| -> Vec { let mut out = Vec::with_capacity(w * h); for row in 0..h { out.extend(plane.data[row * plane.width..][..w].iter().copied()); @@ -20583,7 +20584,7 @@ fn mv_to_q4(pos: usize, mv_component: i32, luma: bool) -> i32 { (pos as i32) * 16 + mv_component * if luma { 2 } else { 1 } } -impl PlaneBuf { +impl PlaneBuf<'_> { /// Adds `residual` onto an already-computed `prediction` (motion /// compensation's output, rather than [`predict`]'s intra one), writing /// the clamped reconstruction into the plane at `(x, y)` -- the inter @@ -20633,7 +20634,7 @@ impl PlaneBuf { let dst = (y + row) * self.width + x; let pred = &prediction[src..src + w]; let res = &residual[src..src + w]; - let out = &mut self.data[dst..dst + w]; + let out = &mut self.data.to_mut()[dst..dst + w]; for ((o, &p), &r) in out.iter_mut().zip(pred).zip(res) { *o = (i32::from(p) + r).clamp(0, max) as u16; } @@ -20655,7 +20656,7 @@ fn read_inter_plane( plane_idx: usize, around: (bool, bool, i32), tx_mode: usize, - plane: &mut PlaneBuf, + plane: &mut PlaneBuf<'static>, x: usize, y: usize, side: usize, @@ -20794,11 +20795,11 @@ fn read_single_ref( /// the compound path can call it twice (lane-av1comp). fn ref_planes<'a>( ref_frame: i8, - ref_y: &'a PlaneBuf, - ref_u: &'a PlaneBuf, - ref_v: &'a PlaneBuf, + ref_y: &'a PlaneBuf<'a>, + ref_u: &'a PlaneBuf<'a>, + ref_v: &'a PlaneBuf<'a>, other_refs: &'a RefSlots, -) -> Result<(&'a PlaneBuf, &'a PlaneBuf, &'a PlaneBuf)> { +) -> Result<(&'a PlaneBuf<'a>, &'a PlaneBuf<'a>, &'a PlaneBuf<'a>)> { if ref_frame == LAST_FRAME { Ok((ref_y, ref_u, ref_v)) } else { @@ -21236,7 +21237,7 @@ nb_newmv={} cell_size={:?} cell_inter={:?} cell_ref={:?} sb_row_off={} sb_col_of /// under `av1_setup_build_prediction_by_{above,left}_pred`). #[allow(clippy::too_many_arguments)] fn obmc_neighbour_pred( - refplane: &PlaneBuf, + refplane: &PlaneBuf<'_>, x: usize, y: usize, mv: (i32, i32), @@ -21309,7 +21310,7 @@ const II_WEIGHTS_1D: [u8; 128] = [ /// lane-wii r2: `wedge == Some(..)` replaces the mode mask with the wedge /// codebook mask (fixed sign 0; luma stride, 2x2 box-averaged for chroma). fn interintra_blend( - plane: &PlaneBuf, + plane: &PlaneBuf<'_>, x: usize, y: usize, bw: usize, @@ -21512,9 +21513,9 @@ fn obmc_blend( py: usize, cpx: usize, cpy: usize, - ref_y: &PlaneBuf, - ref_u: &PlaneBuf, - ref_v: &PlaneBuf, + ref_y: &PlaneBuf<'_>, + ref_u: &PlaneBuf<'_>, + ref_v: &PlaneBuf<'_>, other_refs: &RefSlots, interp_fixed: Option, // lane-scaledref r1: this frame's own coded luma width -- each OBMC @@ -22086,12 +22087,12 @@ fn decode_inter_block( side: usize, mi_cols: u32, mi_rows: u32, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, - ref_y: &PlaneBuf, - ref_u: &PlaneBuf, - ref_v: &PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, + ref_y: &PlaneBuf<'_>, + ref_u: &PlaneBuf<'_>, + ref_v: &PlaneBuf<'_>, // lane-av1refs: every non-`LAST_FRAME` reference this frame header's own // `ref_frame_idx` names a live DPB slot for, indexed `[ref_frame]` // (`LAST2_FRAME`=2 .. `ALTREF_FRAME`=7; index 0/1 unused, `LAST_FRAME` @@ -25283,7 +25284,7 @@ fn decode_inter_block( ); let cu_scan = default_scan(cu_tx); for plane_idx in 1..3 { - let buf: &mut PlaneBuf = if plane_idx == 1 { &mut *u } else { &mut *v }; + let buf: &mut PlaneBuf<'static> = if plane_idx == 1 { &mut *u } else { &mut *v }; let cu_grid = if skip { buf.reconstruct( cu_x, @@ -25781,12 +25782,12 @@ fn decode_inter_sub8_split4( group_mi: (usize, usize), mi_cols: u32, mi_rows: u32, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, - ref_y: &PlaneBuf, - ref_u: &PlaneBuf, - ref_v: &PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, + ref_y: &PlaneBuf<'_>, + ref_u: &PlaneBuf<'_>, + ref_v: &PlaneBuf<'_>, other_refs: &RefSlots, sign_bias_table: &SignBiasTable, global_motion: &[ec_av1_syntax::WarpParams; 7], @@ -26292,9 +26293,9 @@ fn decode_intra_sub8_leaf( has_chroma: bool, skip: bool, scan4: &[u16], - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, base_q_idx: u8, enable_filter_intra: bool, tx_select: bool, @@ -26738,12 +26739,12 @@ fn decode_inter_sub8_rect2( vert: bool, mi_cols: u32, mi_rows: u32, - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, - ref_y: &PlaneBuf, - ref_u: &PlaneBuf, - ref_v: &PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, + ref_y: &PlaneBuf<'_>, + ref_u: &PlaneBuf<'_>, + ref_v: &PlaneBuf<'_>, other_refs: &RefSlots, sign_bias_table: &SignBiasTable, global_motion: &[ec_av1_syntax::WarpParams; 7], @@ -27345,12 +27346,12 @@ fn decode_inter_block8( mi_rows: u32, outer_at: (usize, usize), leaf_mi: (usize, usize), - y: &mut PlaneBuf, - u: &mut PlaneBuf, - v: &mut PlaneBuf, - ref_y: &PlaneBuf, - ref_u: &PlaneBuf, - ref_v: &PlaneBuf, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, + ref_y: &PlaneBuf<'_>, + ref_u: &PlaneBuf<'_>, + ref_v: &PlaneBuf<'_>, other_refs: &RefSlots, base_q_idx: u8, scan8: &[u16], @@ -29577,7 +29578,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( let (width, height) = (cols as usize * BLOCK, rows as usize * BLOCK); let ref_y = PlaneBuf { - data: reference.y.iter().map(|&s| s as u16).collect(), + data: std::borrow::Cow::Borrowed(&reference.y), width: reference.width, height: reference.height, true_width: reference.width, @@ -29588,7 +29589,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_y1: reference.height, }; let ref_u = PlaneBuf { - data: reference.u.iter().map(|&s| s as u16).collect(), + data: std::borrow::Cow::Borrowed(&reference.u), width: reference.width / 2, height: reference.height / 2, true_width: reference.width / 2, @@ -29599,7 +29600,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_y1: reference.height / 2, }; let ref_v = PlaneBuf { - data: reference.v.iter().map(|&s| s as u16).collect(), + data: std::borrow::Cow::Borrowed(&reference.v), width: reference.width / 2, height: reference.height / 2, true_width: reference.width / 2, @@ -29616,13 +29617,13 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // block it is decoding selects an index that is still `None` here // (spec 7.9 requires every *active* reference be a compatible size; // an inactive one this frame never selects is simply left `None`). - let owned_refs: [Option<(PlaneBuf, PlaneBuf, PlaneBuf)>; 8] = std::array::from_fn(|i| { + let owned_refs: [Option<(PlaneBuf<'_>, PlaneBuf<'_>, PlaneBuf<'_>)>; 8] = std::array::from_fn(|i| { other_refs[i] .filter(|g| g.width == reference.width && g.height == reference.height) .map(|g| { ( PlaneBuf { - data: g.y.iter().map(|&s| s as u16).collect(), + data: std::borrow::Cow::Borrowed(&g.y), width: g.width, height: g.height, true_width: g.width, @@ -29633,7 +29634,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_y1: g.height, }, PlaneBuf { - data: g.u.iter().map(|&s| s as u16).collect(), + data: std::borrow::Cow::Borrowed(&g.u), width: g.width / 2, height: g.height / 2, true_width: g.width / 2, @@ -29644,7 +29645,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_y1: g.height / 2, }, PlaneBuf { - data: g.v.iter().map(|&s| s as u16).collect(), + data: std::borrow::Cow::Borrowed(&g.v), width: g.width / 2, height: g.height / 2, true_width: g.width / 2, @@ -29661,7 +29662,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( std::array::from_fn(|i| owned_refs[i].as_ref().map(|(a, b, c)| (a, b, c))); let mut y = PlaneBuf { - data: vec![0u16; width * height], + data: std::borrow::Cow::Owned(vec![0u16; width * height]), width, height, true_width, @@ -29672,7 +29673,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_y1: height, }; let mut u = PlaneBuf { - data: vec![0u16; width * height / 4], + data: std::borrow::Cow::Owned(vec![0u16; width * height / 4]), width: width / 2, height: height / 2, true_width: true_width / 2, @@ -29683,7 +29684,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_y1: height / 2, }; let mut v = PlaneBuf { - data: vec![0u16; width * height / 4], + data: std::borrow::Cow::Owned(vec![0u16; width * height / 4]), width: width / 2, height: height / 2, true_width: true_width / 2, @@ -32187,7 +32188,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( use std::io::Write; let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { - let narrow = |p: &PlaneBuf| -> Vec { p.data.iter().map(|&s| s as u8).collect() }; + let narrow = |p: &PlaneBuf<'_>| -> Vec { p.data.iter().map(|&s| s as u8).collect() }; let _ = f.write_all(&narrow(&y)); let _ = f.write_all(&narrow(&u)); let _ = f.write_all(&narrow(&v)); @@ -32244,10 +32245,10 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( if let Some((upscaled_w, _)) = superres(fctx) { let bd = u32::from(bit_depth(fctx)); let (uw, fw, fh) = (upscaled_w as usize, frame_width as usize, frame_height as usize); - let up = |p: &PlaneBuf, in_w: usize, in_h: usize, out_w: usize| PlaneBuf { - data: crate::superres::upscale_plane_strided( + let up = |p: &PlaneBuf<'_>, in_w: usize, in_h: usize, out_w: usize| PlaneBuf { + data: std::borrow::Cow::Owned(crate::superres::upscale_plane_strided( &p.data, p.width, in_w, in_h, p.true_width, out_w, bd, - ), + )), width: out_w, height: in_h, true_width: out_w, @@ -32274,9 +32275,9 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( ); } if let Some((db_y, db_u, db_v)) = deblocked { - scratch_return(db_y.data); - scratch_return(db_u.data); - scratch_return(db_v.data); + scratch_return(db_y.data.into_owned()); + scratch_return(db_u.data.into_owned()); + scratch_return(db_v.data.into_owned()); } let motion_field = build_motion_field( @@ -32303,15 +32304,15 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( Picture { width, height, - y: y.data, - u: u.data, - v: v.data, + y: y.data.into_owned(), + u: u.data.into_owned(), + v: v.data.into_owned(), }, result_cdfs, motion_field, )); } - let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec { + let crop = |plane: &PlaneBuf<'_>, w: usize, h: usize| -> Vec { let mut out = Vec::with_capacity(w * h); for row in 0..h { out.extend(plane.data[row * plane.width..][..w].iter().copied()); @@ -32986,7 +32987,7 @@ mod tests { const REFUSAL: &str = "a reference frame selected with no picture at this frame's own ref_frame_idx slot"; let plane = PlaneBuf { - data: vec![0u16; 16], + data: std::borrow::Cow::Owned(vec![0u16; 16]), width: 4, height: 4, true_width: 4, @@ -33708,12 +33709,14 @@ mod tests { let (width, height) = (64usize, 64usize); let ref_pattern: Vec = (0..width * height).map(|i| (i % 251) as u16).collect(); let ref_plane = |scale: usize| PlaneBuf { - data: ref_pattern - .iter() - .step_by(scale.max(1)) - .take((width / scale) * (height / scale)) - .copied() - .collect(), + data: std::borrow::Cow::Owned( + ref_pattern + .iter() + .step_by(scale.max(1)) + .take((width / scale) * (height / scale)) + .copied() + .collect(), + ), width: width / scale, height: height / scale, true_width: width / scale, @@ -33724,7 +33727,7 @@ mod tests { tile_y1: height / scale, }; let ref_y = PlaneBuf { - data: ref_pattern.clone(), + data: std::borrow::Cow::Owned(ref_pattern.clone()), width, height, true_width: width, @@ -33738,7 +33741,7 @@ mod tests { let ref_v = ref_plane(2); let mut y = PlaneBuf { - data: vec![0u16; width * height], + data: std::borrow::Cow::Owned(vec![0u16; width * height]), width, height, true_width: width, @@ -33749,7 +33752,7 @@ mod tests { tile_y1: height, }; let mut u = PlaneBuf { - data: vec![0u16; (width / 2) * (height / 2)], + data: std::borrow::Cow::Owned(vec![0u16; (width / 2) * (height / 2)]), width: width / 2, height: height / 2, true_width: width / 2, @@ -33760,7 +33763,7 @@ mod tests { tile_y1: height / 2, }; let mut v = PlaneBuf { - data: vec![0u16; (width / 2) * (height / 2)], + data: std::borrow::Cow::Owned(vec![0u16; (width / 2) * (height / 2)]), width: width / 2, height: height / 2, true_width: width / 2, diff --git a/crates/ec-av1/src/transform.rs b/crates/ec-av1/src/transform.rs index d78c9d35..ccd853ff 100644 --- a/crates/ec-av1/src/transform.rs +++ b/crates/ec-av1/src/transform.rs @@ -107,7 +107,7 @@ fn clamp_range(x: i32, r: usize) -> i32 { /// `B(a, b, angle, flip, r)` (spec 7.13.2.1): a butterfly rotation, and an /// exchange of the two entries when `flip` is set. -fn butterfly(t: &mut [i32], a: usize, b: usize, angle: i32, flip: bool, _r: usize) { +fn butterfly(t: &mut [i32; 64], a: usize, b: usize, angle: i32, flip: bool, _r: usize) { let (ta, tb) = (i64::from(t[a]), i64::from(t[b])); let (c, s) = (i64::from(cos128(angle)), i64::from(sin128(angle))); let x = ta * c - tb * s; @@ -121,7 +121,7 @@ fn butterfly(t: &mut [i32], a: usize, b: usize, angle: i32, flip: bool, _r: usiz /// `H(a, b, flip, r)` (spec 7.13.2.1): a Hadamard rotation, with the indices /// exchanged when `flip` is set. -fn hadamard(t: &mut [i32], a: usize, b: usize, flip: bool, r: usize) { +fn hadamard(t: &mut [i32; 64], a: usize, b: usize, flip: bool, r: usize) { let (a, b) = if flip { (b, a) } else { (a, b) }; let (x, y) = (t[a], t[b]); t[a] = clamp_range(x.wrapping_add(y), r); @@ -130,7 +130,7 @@ fn hadamard(t: &mut [i32], a: usize, b: usize, flip: bool, r: usize) { /// The inverse DCT array permutation (spec 7.13.2.2): an in-place bit-reversal /// of the first `1 << n` entries. -fn permute(t: &mut [i32], n: u32) { +fn permute(t: &mut [i32; 64], n: u32) { let n0 = 1usize << n; // A stack copy: this ran once per 1D transform and its `Vec` was the // `malloc` at the top of every `inverse_dct` frame in the annotation. @@ -148,6 +148,20 @@ fn permute(t: &mut [i32], n: u32) { /// The stage list is the spec's, in the spec's order; the guards on `n` are /// what make one network serve every transform size from 4 to 64. pub fn inverse_dct(t: &mut [i32], n: u32, r: usize) { + // lane-perf10: the network below indexes a 64-entry scratch with computed + // indices, and on a `&mut [i32]` every one of them carried a bounds check. + // A `&mut [i32; 64]` lets the optimiser discharge them all against the + // constant length; this wrapper keeps the slice-taking signature for + // callers outside the 2D driver. + let m = 1usize << n; + let mut fixed = [0i32; 64]; + fixed[..m].copy_from_slice(&t[..m]); + inverse_dct_fixed(&mut fixed, n, r); + t[..m].copy_from_slice(&fixed[..m]); +} + +/// [`inverse_dct`] on the driver's own 64-entry scratch. +fn inverse_dct_fixed(t: &mut [i32; 64], n: u32, r: usize) { assert!((2..=6).contains(&n), "the inverse DCT is defined for 4..64"); permute(t, n); @@ -827,9 +841,9 @@ fn inverse_identity(t: &mut [i32], side: usize) { /// Dispatches one row or column's 1D inverse transform by [`TxType1d`], /// `log2` the spec's transform-size log (as [`inverse_dct`] takes). -fn inverse_1d(t: &mut [i32], log2: u32, r: usize, kind: TxType1d) { +fn inverse_1d(t: &mut [i32; 64], log2: u32, r: usize, kind: TxType1d) { match kind { - TxType1d::Dct => inverse_dct(t, log2, r), + TxType1d::Dct => inverse_dct_fixed(t, log2, r), TxType1d::Adst => match log2 { 2 => inverse_adst4(t), 3 => inverse_adst8(t, r), @@ -904,6 +918,11 @@ pub fn inverse_transform_2d_typed_wh( } let residual = &mut scratch[..w * h]; let mut t = [0i32; 64]; + // lane-perf10: rows the row pass left non-zero, and whether the first + // of them came out constant along the row -- see the fast path below. + let mut nz_rows = 0usize; + let mut nz_row = 0usize; + let mut const_row = false; for i in 0..h { let dst = &mut residual[i * w..(i + 1) * w]; // Rows at or past 32 carry no coefficients (the spec zeroes them @@ -930,6 +949,26 @@ pub fn inverse_transform_2d_typed_wh( for (d, &v) in dst.iter_mut().zip(t[..w].iter()) { *d = clamp_range(round2(v, shift), col_clamp); } + nz_rows += 1; + nz_row = i; + const_row = nz_rows == 1 && dst.iter().all(|&v| v == dst[0]); + } + + // A block whose row pass left exactly one non-zero row, constant along + // that row, feeds every column of the column pass the same vector -- + // the DC-only case, which dominates inter residuals. `inverse_1d` is a + // pure function of its input, so one column transform broadcast across + // the block is byte-identical to `w` of them, and the per-column gather + // disappears with it. + if nz_rows <= 1 && const_row { + t[..h].fill(0); + t[nz_row] = residual[nz_row * w]; + inverse_1d(&mut t, log2h, col_clamp, col_kind); + for i in 0..h { + let dst_row = if ud_flip { h - 1 - i } else { i }; + out[dst_row * w..dst_row * w + w].fill(round2(t[i], 4)); + } + return; } for j in 0..w {