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(
16140 // Feature index: `SEG_LVL_ALT_LF_Y_V + dir` for luma, `_U`/`_V` (3/4)16140 // Feature index: `SEG_LVL_ALT_LF_Y_V + dir` for luma, `_U`/`_V` (3/4)
16141 // for chroma.16141 // for chroma.
16142 let feature = if plane_idx == 0 { SEG_LVL_ALT_LF_Y_V + dir } else { plane_idx + 2 };16142 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.
16143 let base = match seg_feature(usize::from(segment_id), feature) {16146 let base = match seg_feature(usize::from(segment_id), feature) {
16144 Some(data) => (base + data).clamp(0, 63),16147 Some(data) => (base + data).clamp(0, 63),
16145 None => base,16148 None => base,
@@ -16173,6 +16176,11 @@ fn edge_params(
16173 chroma: bool,16176 chroma: bool,
16174 x0: usize,16177 x0: usize,
16175 y0: usize,16178 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,
16176 ) -> Option<(u8, i32)> {16184 ) -> Option<(u8, i32)> {
16177 let (mi_r, mi_c) = (plane_to_mi(chroma, y0), plane_to_mi(chroma, x0));16185 let (mi_r, mi_c) = (plane_to_mi(chroma, y0), plane_to_mi(chroma, x0));
16178 let cur_tx = if dir == 0 {16186 let cur_tx = if dir == 0 {
@@ -16220,15 +16228,13 @@ fn edge_params(
16220 {16228 {
16221 return None;16229 return None;
16222 }16230 }
16223- let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf, segment_id_at(mi_r, mi_c));16231+ let (cur_seg, pv_seg) = if seg_lf_active {
16224- let pv_level = lf_level(16232+ (segment_id_at(mi_r, mi_c), segment_id_at(pv_mi_r, pv_mi_c))
16225- lf,16233+ } else {
16226- plane_idx,16234+ (0, 0)
16227- dir,16235+ };
16228- pv_ref,16236+ let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf, cur_seg);
16229- pv_delta_lf,16237+ let pv_level = lf_level(lf, plane_idx, dir, pv_ref, pv_delta_lf, pv_seg);
16230- segment_id_at(pv_mi_r, pv_mi_c),
16231- );
16232 if cur_level == 0 && pv_level == 0 {16238 if cur_level == 0 && pv_level == 0 {
16233 return None;16239 return None;
16234 }16240 }
@@ -16976,13 +16982,14 @@ fn deblock_plane(
16976 // dependence is on ascending `x0` within a row, which stays the outer16982 // dependence is on ascending `x0` within a row, which stays the outer
16977 // loop's order, and rows remain independent (horizontal pass mirrored).16983 // loop's order, and rows remain independent (horizontal pass mirrored).
16978 let rows = plane.data.len() / stride.max(1);16984 let rows = plane.data.len() / stride.max(1);
16985+ let seg_lf_active = SEG.with(|s| s.borrow().enabled);
16979 let mut x0 = 4usize;16986 let mut x0 = 4usize;
16980 while x0 < tw {16987 while x0 < tw {
16981 let mut y0 = 0usize;16988 let mut y0 = 0usize;
16982 while y0 < th {16989 while y0 < th {
16983 if y0 + 16 <= th && x0 >= 8 && x0 + 8 <= stride {16990 if y0 + 16 <= th && x0 >= 8 && x0 + 8 <= stride {
16984 let ps: [Option<(u8, i32)>; 4] =16991 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));
16986 if let Some((len, level)) = ps[0] {16993 if let Some((len, level)) = ps[0] {
16987 if ps.iter().all(|p| *p == ps[0])16994 if ps.iter().all(|p| *p == ps[0])
16988 && filter_edge16(16995 && filter_edge16(
@@ -17015,7 +17022,7 @@ fn deblock_plane(
17015 y0 += 16;17022 y0 += 16;
17016 continue;17023 continue;
17017 }17024 }
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) {
17019 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 {17026 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 {
17020 eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}");17027 eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}");
17021 }17028 }
@@ -17039,7 +17046,7 @@ fn deblock_plane(
17039 while x0 < tw {17046 while x0 < tw {
17040 if x0 + 16 <= tw && y0 >= 8 && y0 + 7 <= rows {17047 if x0 + 16 <= tw && y0 >= 8 && y0 + 7 <= rows {
17041 let ps: [Option<(u8, i32)>; 4] =17048 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));
17043 if let Some((len, level)) = ps[0] {17050 if let Some((len, level)) = ps[0] {
17044 if ps.iter().all(|p| *p == ps[0])17051 if ps.iter().all(|p| *p == ps[0])
17045 && filter_edge16(17052 && filter_edge16(
@@ -17072,7 +17079,7 @@ fn deblock_plane(
17072 x0 += 16;17079 x0 += 16;
17073 continue;17080 continue;
17074 }17081 }
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) {
17076 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE")17083 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE")
17077 && plane_idx == 017084 && plane_idx == 0
17078 && x0 == 4417085 && x0 == 44