forge
Explore
Log in

Don't you dare go Hollow

The deblock edge derivation reads segmentation once per plane pass

Browse files at this commit

bf707057b5f8cd009aead885cd32568cbddf48d4Ahmet Kaan GümüşParent: c80d9e9
  • perf
    • the deblock edge derivation reads segmentation once per plane pass
Show full message
perf(av1): the deblock edge derivation reads segmentation once per plane pass

`edge_params` (2.9% self time at 4K, called for every 4x4 edge position) took
four thread-local reads (`SEG_MI_DIMS` and `SEG_IDS`, twice each) plus two
`SEG` borrows per call to look up two segment ids and their `SEG_LVL_ALT_LF_*`
deltas. `segmentation_enabled` is a per-frame flag, so it is read once per
plane pass now: with it clear every segment id is 0 and no LF feature can be
active, so both lookups are skipped and the levels are identical.

Small: seg4k pair deltas 0.20/0.16/0.04 s, medians 35.31 s -> 35.27 s.
Byte-exact on all four inputs.
crates/ec-av1/src/decode.rsmodified+20 -13
@@ -16140,6 +16140,9 @@ fn lf_level(
1614016140 // Feature index: `SEG_LVL_ALT_LF_Y_V + dir` for luma, `_U`/`_V` (3/4)
1614116141 // for chroma.
1614216142 let feature = if plane_idx == 0 { SEG_LVL_ALT_LF_Y_V + dir } else { plane_idx + 2 };
16143+ // lane-perf9: `segment_id` is `0` and this lookup is skipped entirely
16144+ // when the frame codes no segmentation (`seg_lf_active`), which is what
16145+ // spares the deblock loop two thread-local reads per 4x4 edge.
1614316146 let base = match seg_feature(usize::from(segment_id), feature) {
1614416147 Some(data) => (base + data).clamp(0, 63),
1614516148 None => base,
@@ -16173,6 +16176,11 @@ fn edge_params(
1617316176 chroma: bool,
1617416177 x0: usize,
1617516178 y0: usize,
16179+ // lane-perf9: `segmentation_enabled`, read once per plane pass instead of
16180+ // four thread-local reads (`SEG_MI_DIMS`, `SEG_IDS`, twice each) plus two
16181+ // `SEG` borrows per 4x4 edge. With it clear, every segment id is 0 and no
16182+ // `SEG_LVL_ALT_LF_*` feature can be active, so the levels are unchanged.
16183+ seg_lf_active: bool,
1617616184 ) -> Option<(u8, i32)> {
1617716185 let (mi_r, mi_c) = (plane_to_mi(chroma, y0), plane_to_mi(chroma, x0));
1617816186 let cur_tx = if dir == 0 {
@@ -16220,15 +16228,13 @@ fn edge_params(
1622016228 {
1622116229 return None;
1622216230 }
16223- let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf, segment_id_at(mi_r, mi_c));
16224- let pv_level = lf_level(
16225- lf,
16226- plane_idx,
16227- dir,
16228- pv_ref,
16229- pv_delta_lf,
16230- segment_id_at(pv_mi_r, pv_mi_c),
16231- );
16231+ let (cur_seg, pv_seg) = if seg_lf_active {
16232+ (segment_id_at(mi_r, mi_c), segment_id_at(pv_mi_r, pv_mi_c))
16233+ } else {
16234+ (0, 0)
16235+ };
16236+ let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf, cur_seg);
16237+ let pv_level = lf_level(lf, plane_idx, dir, pv_ref, pv_delta_lf, pv_seg);
1623216238 if cur_level == 0 && pv_level == 0 {
1623316239 return None;
1623416240 }
@@ -16976,13 +16982,14 @@ fn deblock_plane(
1697616982 // dependence is on ascending `x0` within a row, which stays the outer
1697716983 // loop's order, and rows remain independent (horizontal pass mirrored).
1697816984 let rows = plane.data.len() / stride.max(1);
16985+ let seg_lf_active = SEG.with(|s| s.borrow().enabled);
1697916986 let mut x0 = 4usize;
1698016987 while x0 < tw {
1698116988 let mut y0 = 0usize;
1698216989 while y0 < th {
1698316990 if y0 + 16 <= th && x0 >= 8 && x0 + 8 <= stride {
1698416991 let ps: [Option<(u8, i32)>; 4] =
16985- std::array::from_fn(|k| edge_params(lf, n, plane_idx, 0, chroma, x0, y0 + 4 * k));
16992+ std::array::from_fn(|k| edge_params(lf, n, plane_idx, 0, chroma, x0, y0 + 4 * k, seg_lf_active));
1698616993 if let Some((len, level)) = ps[0] {
1698716994 if ps.iter().all(|p| *p == ps[0])
1698816995 && filter_edge16(
@@ -17015,7 +17022,7 @@ fn deblock_plane(
1701517022 y0 += 16;
1701617023 continue;
1701717024 }
17018- if let Some((len, level)) = edge_params(lf, n, plane_idx, 0, chroma, x0, y0) {
17025+ if let Some((len, level)) = edge_params(lf, n, plane_idx, 0, chroma, x0, y0, seg_lf_active) {
1701917026 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 {
1702017027 eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}");
1702117028 }
@@ -17039,7 +17046,7 @@ fn deblock_plane(
1703917046 while x0 < tw {
1704017047 if x0 + 16 <= tw && y0 >= 8 && y0 + 7 <= rows {
1704117048 let ps: [Option<(u8, i32)>; 4] =
17042- std::array::from_fn(|k| edge_params(lf, n, plane_idx, 1, chroma, x0 + 4 * k, y0));
17049+ std::array::from_fn(|k| edge_params(lf, n, plane_idx, 1, chroma, x0 + 4 * k, y0, seg_lf_active));
1704317050 if let Some((len, level)) = ps[0] {
1704417051 if ps.iter().all(|p| *p == ps[0])
1704517052 && filter_edge16(
@@ -17072,7 +17079,7 @@ fn deblock_plane(
1707217079 x0 += 16;
1707317080 continue;
1707417081 }
17075- if let Some((len, level)) = edge_params(lf, n, plane_idx, 1, chroma, x0, y0) {
17082+ if let Some((len, level)) = edge_params(lf, n, plane_idx, 1, chroma, x0, y0, seg_lf_active) {
1707617083 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE")
1707717084 && plane_idx == 0
1707817085 && x0 == 44