Memoise the deblocker's level LUT per plane and pass
Browse files at this commit4dff42ae5214a3b0edc70409da460f29626cbc69Parent: 7fd0e67- perf
- memoise the deblocker's level LUT per plane and pass
1 changed file +49 -11
- crates/ec-av1/src/decode.rs +49 -11
crates/ec-av1/src/decode.rsmodified+49 -11
| @@ -17963,6 +17963,54 @@ fn lf_level( | |||
| 17963 | lvl.clamp(0, 63) | 17963 | lvl.clamp(0, 63) |
| 17964 | } | 17964 | } |
| 17965 | 17965 | ||
| 17966 | +thread_local! { | ||
| 17967 | + /// lane-pipefilt: [`EdgeCtx`]'s level LUT, memoised per thread. The table | ||
| 17968 | + /// is a pure function of the frame's [`LoopFilterParams`], the plane, the | ||
| 17969 | + /// pass and segment 0's own `SEG_LVL_ALT_LF_*` datum (the only thing | ||
| 17970 | + /// [`lf_level`] reads out of the `FrameCtx` at `segment_id = 0`), so one | ||
| 17971 | + /// build per plane and pass serves the whole frame. The pipeline builds | ||
| 17972 | + /// an `EdgeCtx` per superblock-row band rather than per frame, which | ||
| 17973 | + /// turned 20 builds a frame into ~680 -- 960 `lf_level` calls each. | ||
| 17974 | + static LVL_CACHE: std::cell::RefCell< | ||
| 17975 | + Vec<((usize, usize, LoopFilterParams, Option<i32>), [[u8; 15]; 64])>, | ||
| 17976 | + > = const { std::cell::RefCell::new(Vec::new()) }; | ||
| 17977 | +} | ||
| 17978 | + | ||
| 17979 | +/// `lf_level` over the whole `(base level, ref frame)` domain -- see | ||
| 17980 | +/// [`LVL_CACHE`]. `lf_level`'s own first step is | ||
| 17981 | +/// `(base0 + delta_lf).clamp(0, 63)`, so passing `b - base0` pins its base at | ||
| 17982 | +/// `b` and the rest of the function is a pure function of `(b, ref_frame)`. | ||
| 17983 | +fn lvl_table( | ||
| 17984 | + lf: &LoopFilterParams, | ||
| 17985 | + plane_idx: usize, | ||
| 17986 | + dir: usize, | ||
| 17987 | + base0: i32, | ||
| 17988 | + fctx: &crate::decode::FrameCtx, | ||
| 17989 | +) -> [[u8; 15]; 64] { | ||
| 17990 | + let feature = if plane_idx == 0 { SEG_LVL_ALT_LF_Y_V + dir } else { plane_idx + 2 }; | ||
| 17991 | + let key = (plane_idx, dir, *lf, seg_feature(0, feature, fctx)); | ||
| 17992 | + if let Some(t) = LVL_CACHE.with(|c| { | ||
| 17993 | + c.borrow().iter().find(|(k, _)| *k == key).map(|(_, t)| *t) | ||
| 17994 | + }) { | ||
| 17995 | + return t; | ||
| 17996 | + } | ||
| 17997 | + let mut lvl = [[0u8; 15]; 64]; | ||
| 17998 | + for (b, row) in lvl.iter_mut().enumerate() { | ||
| 17999 | + for (r, cell) in row.iter_mut().enumerate() { | ||
| 18000 | + *cell = | ||
| 18001 | + lf_level(lf, plane_idx, dir, (r as i32 - 7) as i8, b as i32 - base0, 0, fctx) as u8; | ||
| 18002 | + } | ||
| 18003 | + } | ||
| 18004 | + LVL_CACHE.with(|c| { | ||
| 18005 | + let mut c = c.borrow_mut(); | ||
| 18006 | + if c.len() >= 8 { | ||
| 18007 | + c.remove(0); | ||
| 18008 | + } | ||
| 18009 | + c.push((key, lvl)); | ||
| 18010 | + }); | ||
| 18011 | + lvl | ||
| 18012 | +} | ||
| 18013 | + | ||
| 17966 | /// Spec 7.14.2's `set_lpf_parameters`, restricted to the uniform-level, | 18014 | /// Spec 7.14.2's `set_lpf_parameters`, restricted to the uniform-level, |
| 17967 | /// no-segmentation, no-delta-LF path and to this decoder's own invariant | 18015 | /// no-segmentation, no-delta-LF path and to this decoder's own invariant |
| 17968 | /// that a coded block's transform is always its own full size -- so a TU | 18016 | /// that a coded block's transform is always its own full size -- so a TU |
| @@ -18015,17 +18063,7 @@ impl<'a> EdgeCtx<'a> { | |||
| 18015 | } else { | 18063 | } else { |
| 18016 | lf.level[3] | 18064 | lf.level[3] |
| 18017 | }); | 18065 | }); |
| 18018 | - // `lf_level`'s own first step is `(base0 + delta_lf).clamp(0, 63)`, | 18066 | + let lvl = lvl_table(lf, plane_idx, dir, base0, fctx); |
| 18019 | - // so passing `b - base0` pins its base at `b` and the rest of the | ||
| 18020 | - // function is a pure function of `(b, ref_frame)`. | ||
| 18021 | - let mut lvl = [[0u8; 15]; 64]; | ||
| 18022 | - for (b, row) in lvl.iter_mut().enumerate() { | ||
| 18023 | - for (r, cell) in row.iter_mut().enumerate() { | ||
| 18024 | - *cell = | ||
| 18025 | - lf_level(lf, plane_idx, dir, (r as i32 - 7) as i8, b as i32 - base0, 0, fctx) | ||
| 18026 | - as u8; | ||
| 18027 | - } | ||
| 18028 | - } | ||
| 18029 | Self { | 18067 | Self { |
| 18030 | lf, | 18068 | lf, |
| 18031 | n, | 18069 | n, |