forge
Explore
Log in

Don't you dare go Hollow

The filter stage's capture decode replaced by one more filter replay

Browse files at this commit

2d19659e9b034b3f171e94ee787fc9f619ab58b4Ahmet Kaan GümüşParent: 1cc5362
  • perf
    • the filter stage's capture decode replaced by one more filter replay
Show full message
perf(av1): the filter stage's capture decode replaced by one more filter replay

The stage decoded the coded tile a SECOND time after the search -- 10% of a
4K frame's encode -- only to reach the post-CDEF plane the loop restoration
search reads and the picture the stage splices back. Both come out of one
more replay of the winning deblock/CDEF parameters on the pre-filter planes
the search already captured: the same two kernels, on the same input, in the
same order. The re-coded tile still carries the cdef_idx literals, which the
replay is handed directly.

Also gates the per-trial stage timers on par::stage_times: two Instant::now()
ran per transform trial and per mc::predict call in every cfg(test) build
whether or not anyone read the breakdown.
crates/ec-av1/src/decode.rsmodified+85 -8
@@ -19766,6 +19766,27 @@ pub(crate) fn set_filter_replay_disabled(off: bool) {
1976619766 REPLAY_DISABLED.store(off, std::sync::atomic::Ordering::Relaxed);
1976719767 }
1976819768
19769+/// Test-only: run the capture decode [`replay_final`] replaced as well, and
19770+/// hold its picture and its [`FilterStages`] against the replay's
19771+/// (`encoder::tests::filter_replay_final_matches_the_capture_decode`).
19772+/// Thread-local, not a global: the filter stage runs on the frame thread the
19773+/// test itself drives, and a process-wide flag would put every test encoding
19774+/// in parallel with this one through the extra decode too.
19775+#[cfg(test)]
19776+thread_local! {
19777+ static VERIFY_FINAL: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
19778+}
19779+
19780+#[cfg(test)]
19781+pub(crate) fn set_verify_final_replay(on: bool) {
19782+ VERIFY_FINAL.set(on);
19783+}
19784+
19785+#[cfg(test)]
19786+pub(crate) fn verify_final_replay() -> bool {
19787+ VERIFY_FINAL.get()
19788+}
19789+
1976919790 /// Drop the armed flag and anything captured: the tile it belongs to is done.
1977019791 pub(crate) fn clear_filter_replay() {
1977119792 REPLAY_ARMED.set(false);
@@ -19845,6 +19866,45 @@ pub(crate) fn replay_filters(
1984519866 if REPLAY_DISABLED.load(std::sync::atomic::Ordering::Relaxed) {
1984619867 return None;
1984719868 }
19869+ replay_inner(loop_filter, cdef, None, fctx).map(|(picture, _)| picture)
19870+}
19871+
19872+/// lane-av1cap: the filter stage's LAST replay -- the WINNING parameters,
19873+/// under the per-64x64 `cdef_idx` grid the re-coded tile carries. The stage
19874+/// used to reach this picture by decoding that re-coded tile a second time
19875+/// (10% of a 4K frame's encode), only because the loop-restoration search
19876+/// needs the decoder's own post-CDEF plane
19877+/// ([`FrameCtx::capture_stages`]). It is the same two kernel calls
19878+/// ([`apply_deblock`], [`apply_cdef`]) on the same captured pre-filter
19879+/// planes in the same order, so it is the same bytes -- pinned by
19880+/// `encoder::tests::filter_replay_final_matches_the_capture_decode`, which
19881+/// runs the decode as well and holds picture and stages against it.
19882+///
19883+/// `want_stages` mirrors `capture_stages`' own `deblocked = None` arm: that
19884+/// decode's header says `RESTORE_NONE`, so the decoder took no pre-CDEF
19885+/// snapshot and handed the post-CDEF plane over for both stages.
19886+pub(crate) fn replay_final(
19887+ loop_filter: &LoopFilterParams,
19888+ cdef: &CdefParams,
19889+ idx_grid: &[u8],
19890+ want_stages: bool,
19891+ fctx: &FrameCtx,
19892+) -> Option<(Picture, Option<FilterStages>)> {
19893+ #[cfg(test)]
19894+ if REPLAY_DISABLED.load(std::sync::atomic::Ordering::Relaxed) {
19895+ return None;
19896+ }
19897+ replay_inner(loop_filter, cdef, Some((idx_grid, want_stages)), fctx)
19898+}
19899+
19900+fn replay_inner(
19901+ loop_filter: &LoopFilterParams,
19902+ cdef: &CdefParams,
19903+ // `Some((cdef_idx grid, capture the filter stages))` for the final
19904+ // replay; `None` for a scoring candidate, which always codes `bits == 0`.
19905+ final_pass: Option<(&[u8], bool)>,
19906+ fctx: &FrameCtx,
19907+) -> Option<(Picture, Option<FilterStages>)> {
1984819908 REPLAY.with_borrow_mut(|slot| {
1984919909 let r = slot.as_mut()?;
1985019910 let hit = matches!(&r.deblocked, Some((lf, ..)) if lf == loop_filter);
@@ -19881,9 +19941,23 @@ pub(crate) fn replay_filters(
1988119941 (y, u, v)
1988219942 };
1988319943 drop(cpt);
19944+ // The winner's `cdef_idx` literals live in the re-coded tile, which
19945+ // nothing parses on this path -- hand the grid the encoder already
19946+ // chose straight to the filter (`bits == 0` leaves the grid the last
19947+ // parse sized, which is what every scoring candidate reads).
19948+ if let Some((grid, _)) = final_pass.filter(|(g, _)| !g.is_empty()) {
19949+ fctx.cdef_idx_grid.with(|g| *g.borrow_mut() = grid.to_vec());
19950+ }
1988419951 let ct = crate::par::timer(crate::par::S_CDEF);
1988519952 apply_cdef(&mut y, &mut u, &mut v, cdef, &r.neighbours, (r.frame_width, r.frame_height), fctx);
1988619953 drop(ct);
19954+ let stages = final_pass.and_then(|(_, want)| {
19955+ want.then(|| FilterStages {
19956+ deblocked: [y.data.to_vec(), u.data.to_vec(), v.data.to_vec()],
19957+ cdefed: [y.data.to_vec(), u.data.to_vec(), v.data.to_vec()],
19958+ stride: [y.width, u.width, v.width],
19959+ })
19960+ });
1988719961 // The capture only happens on the shape whose tail is the two
1988819962 // filter calls and the crop (no superres, no loop restoration), so
1988919963 // what is left is that crop -- byte for byte the cropped return path
@@ -19897,13 +19971,16 @@ pub(crate) fn replay_filters(
1989719971 let (fw, fh) = (r.frame_width, r.frame_height);
1989819972 if fw == r.width && fh == r.height {
1989919973 fctx.last_frame_wide_margin.with(|m| *m.borrow_mut() = None);
19900- return Some(Picture {
19901- width: r.width,
19902- height: r.height,
19903- y: y.data.into_owned(),
19904- u: u.data.into_owned(),
19905- v: v.data.into_owned(),
19906- });
19974+ return Some((
19975+ Picture {
19976+ width: r.width,
19977+ height: r.height,
19978+ y: y.data.into_owned(),
19979+ u: u.data.into_owned(),
19980+ v: v.data.into_owned(),
19981+ },
19982+ stages,
19983+ ));
1990719984 }
1990819985 let crop = |plane: &PlaneBuf<'_>, w: usize, h: usize| -> Vec<u16> {
1990919986 let mut out = Vec::with_capacity(w * h);
@@ -19935,7 +20012,7 @@ pub(crate) fn replay_filters(
1993520012 r.scratch.push(plane.data.into_owned());
1993620013 }
1993720014 }
19938- Some(out)
20015+ Some((out, stages))
1993920016 })
1994020017 }
1994120018
crates/ec-av1/src/encode.rsmodified+131 -28
@@ -1566,7 +1566,7 @@ impl Plane<'_> {
15661566 }
15671567 }
15681568 #[cfg(test)]
1569- let t = std::time::Instant::now();
1569+ let t = stage_start();
15701570 let levels =
15711571 forward_and_quantize_typed(residual, side, 8, i32::from(base_q_idx), deadzone, tx_type);
15721572 // lane-av1speed2: 60% of the transform units of an inter stream carry
@@ -1580,7 +1580,7 @@ impl Plane<'_> {
15801580 &levels, side, side, 8, i32::from(base_q_idx), 0, 0, tx_type,
15811581 );
15821582 #[cfg(test)]
1583- stage_add(2, t.elapsed());
1583+ stage_since(2, t);
15841584
15851585 let reconstruction: Vec<u8> = if coded.is_empty() {
15861586 prediction.to_vec()
@@ -1596,7 +1596,7 @@ impl Plane<'_> {
15961596 // will code them with, so the search ranks modes -- and the partition
15971597 // trial ranks trees -- by the bits they actually spend.
15981598 #[cfg(test)]
1599- let t = std::time::Instant::now();
1599+ let t = stage_start();
16001600 let bits = if cfg!(test) && std::env::var_os("EC_AV1_ESTIMATE").is_some() {
16011601 // What the search used before it could price a block exactly: a
16021602 // level costs its magnitude's width plus a sign and a run. Kept
@@ -1620,7 +1620,7 @@ impl Plane<'_> {
16201620 }
16211621 };
16221622 #[cfg(test)]
1623- stage_add(3, t.elapsed());
1623+ stage_since(3, t);
16241624 Trial {
16251625 levels,
16261626 reconstruction,
@@ -1666,7 +1666,7 @@ impl Plane<'_> {
16661666 }
16671667 }
16681668 #[cfg(test)]
1669- let t = std::time::Instant::now();
1669+ let t = stage_start();
16701670 let levels = forward_and_quantize(residual, side, 8, i32::from(base_q_idx), deadzone);
16711671 // The same all-zero shortcut [`Self::trial_typed`] takes.
16721672 let coded = dequant_and_inverse_typed_wh(
@@ -1680,7 +1680,7 @@ impl Plane<'_> {
16801680 TxType::DctDct,
16811681 );
16821682 #[cfg(test)]
1683- stage_add(2, t.elapsed());
1683+ stage_since(2, t);
16841684 let reconstruction: Vec<u8> = if coded.is_empty() {
16851685 prediction.to_vec()
16861686 } else {
@@ -1692,7 +1692,7 @@ impl Plane<'_> {
16921692 };
16931693 let sse = self.block_sse(x, y, side, &reconstruction);
16941694 #[cfg(test)]
1695- let t = std::time::Instant::now();
1695+ let t = stage_start();
16961696 let bits = if cfg!(test) && std::env::var_os("EC_AV1_ESTIMATE").is_some() {
16971697 levels
16981698 .iter()
@@ -1712,7 +1712,7 @@ impl Plane<'_> {
17121712 }
17131713 };
17141714 #[cfg(test)]
1715- stage_add(3, t.elapsed());
1715+ stage_since(3, t);
17161716 Trial {
17171717 levels,
17181718 reconstruction,
@@ -5035,6 +5035,24 @@ thread_local! {
50355035 ];
50365036 }
50375037
5038+/// lane-av1cap: the trial timers are `#[cfg(test)]`, and the BD gate and
5039+/// every encoder test are `cfg(test)` builds -- so two `Instant::now()` ran
5040+/// per transform trial and per `mc::predict` call whether or not anyone was
5041+/// reading the breakdown (the vdso clock was 1.7% of the encoder's profile).
5042+/// The clock is now read only when [`crate::par::stage_times`] is on, which
5043+/// is what prints the breakdown in the first place.
5044+#[cfg(test)]
5045+pub(crate) fn stage_start() -> Option<std::time::Instant> {
5046+ crate::par::stage_times().then(std::time::Instant::now)
5047+}
5048+
5049+#[cfg(test)]
5050+pub(crate) fn stage_since(bucket: usize, t: Option<std::time::Instant>) {
5051+ if let Some(t) = t {
5052+ stage_add(bucket, t.elapsed());
5053+ }
5054+}
5055+
50385056 #[cfg(test)]
50395057 pub(crate) fn stage_add(bucket: usize, dur: std::time::Duration) {
50405058 STAGE_NS.with(|c| c[bucket].set(c[bucket].get() + dur.as_nanos() as u64));
@@ -5042,11 +5060,13 @@ pub(crate) fn stage_add(bucket: usize, dur: std::time::Duration) {
50425060
50435061 #[cfg(test)]
50445062 fn stage_reset() {
5063+ crate::par::set_stage_times(true);
50455064 STAGE_NS.with(|c| c.iter().for_each(|cell| cell.set(0)));
50465065 }
50475066
50485067 #[cfg(test)]
50495068 fn stage_read() -> [std::time::Duration; 4] {
5069+ crate::par::set_stage_times(false);
50505070 STAGE_NS.with(|c| {
50515071 c.each_ref()
50525072 .map(|cell| std::time::Duration::from_nanos(cell.get()))
@@ -6018,7 +6038,6 @@ fn pick_and_apply_filters(
60186038 lambda,
60196039 );
60206040 drop(ft);
6021- crate::decode::clear_filter_replay();
60226041 let (lf, cdef, idx_grid, picture) = search_result?;
60236042 header.loop_filter = lf;
60246043 header.cdef = cdef;
@@ -6037,22 +6056,86 @@ fn pick_and_apply_filters(
60376056 // an approximation inside the SEARCH only; the reconstruction this
60386057 // function ends up splicing is always a real decode of the real tile.
60396058 let lr_on = restoration_enabled();
6040- fctx.capture_stages.set(lr_on);
6041- let mut filtered = if idx_grid.is_empty() {
6042- if lr_on {
6043- let _t = crate::par::timer(crate::par::S_CAPTURE);
6044- decode(&lf, &cdef, &hdr, tiles, &none_lr)?
6045- } else {
6046- picture
6059+ // The `cdef_idx` literals live in the tile payload, so a frame that
6060+ // chose a per-64x64 preset list codes its tiles a second time.
6061+ if !idx_grid.is_empty() {
6062+ *tiles = recode(cdef.bits, sb_cols, &idx_grid, &[])?;
6063+ }
6064+ // lane-av1cap: that re-coded tile used to be DECODED here as well --
6065+ // 10% of a 4K frame -- purely to reach the post-CDEF plane the loop
6066+ // restoration search reads (`crate::decode::FrameCtx::capture_stages`)
6067+ // and the picture this function splices. Both come out of ONE more
6068+ // replay of the winning parameters on the same captured pre-filter
6069+ // planes every candidate was scored on: the same two filter kernels, on
6070+ // the same input, in the same order, so the same bytes. Re-coding the
6071+ // tile does not move the reconstruction the replay holds -- `cdef_idx`
6072+ // is an equiprobable literal, so it adapts no CDF and changes no
6073+ // residual.
6074+ let replayed = crate::decode::replay_final(&lf, &cdef, &idx_grid, lr_on, fctx);
6075+ #[cfg(test)]
6076+ let replay_was_used = replayed.is_some();
6077+ let (mut filtered, mut stages) = match replayed {
6078+ Some((picture, stages)) => (picture, stages),
6079+ // Nothing captured (the shapes [`crate::decode::FilterReplay`]
6080+ // refuses): every candidate was a full decode, so the winner's own
6081+ // picture stands, and the stages still need a capturing decode.
6082+ None => {
6083+ fctx.capture_stages.set(lr_on);
6084+ let picture = if idx_grid.is_empty() && !lr_on {
6085+ picture
6086+ } else {
6087+ let _t = crate::par::timer(crate::par::S_CAPTURE);
6088+ decode(&lf, &cdef, &hdr, tiles, &none_lr)?
6089+ };
6090+ fctx.capture_stages.set(false);
6091+ (picture, crate::decode::take_filter_stages(fctx))
60476092 }
6048- } else {
6049- let coded = recode(cdef.bits, sb_cols, &idx_grid, &[])?;
6050- let _t = crate::par::timer(crate::par::S_CAPTURE);
6051- let picture = decode(&lf, &cdef, &hdr, &coded, &none_lr)?;
6052- *tiles = coded;
6053- picture
60546093 };
6055- fctx.capture_stages.set(false);
6094+ #[cfg(test)]
6095+ if crate::decode::verify_final_replay() && replay_was_used {
6096+ fctx.capture_stages.set(lr_on);
6097+ let want = decode(&lf, &cdef, &hdr, tiles, &none_lr)?;
6098+ fctx.capture_stages.set(false);
6099+ let want_stages = crate::decode::take_filter_stages(fctx);
6100+ assert!(
6101+ (want.width, want.height) == (filtered.width, filtered.height)
6102+ && want.y == filtered.y
6103+ && want.u == filtered.u
6104+ && want.v == filtered.v,
6105+ "the final filter replay is not the capture decode it stands in for",
6106+ );
6107+ // Only the frame's own cropped region: the plane is padded to the
6108+ // superblock grid and the columns past the mi extent are written by
6109+ // nothing (the decoder's plane pool leaves whatever the last frame
6110+ // put there), while the restoration search reads strictly inside
6111+ // `fw x fh` -- `restoration::lr_sample` clamps its column to
6112+ // `plane_w - 1` and its row to the frame's, and
6113+ // `filter_search::pick_restoration`'s own SSE walk never leaves the
6114+ // crop either.
6115+ let region = |a: &[u16], b: &[u16], stride: usize, w: usize, h: usize, what: &str| {
6116+ for row in 0..h {
6117+ let (x, y) = (&a[row * stride..][..w], &b[row * stride..][..w]);
6118+ assert!(
6119+ x == y,
6120+ "{what}: the replayed filter stages are not the ones the capture \
6121+ decode returns (row {row} of {h}, width {w}, stride {stride})",
6122+ );
6123+ }
6124+ };
6125+ match (&want_stages, &stages) {
6126+ (Some(a), Some(b)) => {
6127+ assert_eq!(a.stride, b.stride, "the captured plane strides moved");
6128+ let (cw, ch) = (fw.div_ceil(2), fh.div_ceil(2));
6129+ for (i, (w, h)) in [(fw, fh), (cw, ch), (cw, ch)].into_iter().enumerate() {
6130+ region(&a.deblocked[i], &b.deblocked[i], a.stride[i], w, h, "deblocked");
6131+ region(&a.cdefed[i], &b.cdefed[i], a.stride[i], w, h, "cdefed");
6132+ }
6133+ }
6134+ (None, None) => {}
6135+ _ => panic!("one path captured filter stages and the other did not"),
6136+ }
6137+ }
6138+ crate::decode::clear_filter_replay();
60566139 let mut lr = LoopRestorationParams::default();
60576140 if lr_on {
60586141 let mut want = LoopRestorationParams {
@@ -6062,7 +6145,7 @@ fn pick_and_apply_filters(
60626145 };
60636146 want.frame_restoration_type[0] = ec_av1_syntax::RestorationType::Wiener;
60646147 let lrt = crate::par::timer(crate::par::S_LR);
6065- let units = match crate::decode::take_filter_stages(fctx) {
6148+ let units = match stages.take() {
60666149 Some(stages) => crate::filter_search::pick_restoration(
60676150 &stages,
60686151 source[0],
@@ -6714,7 +6797,7 @@ fn search_inter_block(
67146797 let source_block = luma.source_block(x, y, BLOCK);
67156798 let (seeds, seed_n) = mv_seeds(stack);
67166799 #[cfg(test)]
6717- let t = std::time::Instant::now();
6800+ let t = stage_start();
67186801 let found = motion::search(
67196802 ref_luma.0,
67206803 ref_luma.1,
@@ -6731,7 +6814,7 @@ fn search_inter_block(
67316814 1,
67326815 );
67336816 #[cfg(test)]
6734- stage_add(0, t.elapsed());
6817+ stage_since(0, t);
67356818 let mv = round_to_valid_mv(found.mv, stack.pred_mv);
67366819 // Each DRL entry is a different `PredMv` (spec 7.10.2.10 `assign_mv`), so
67376820 // the same vector costs a different residual from each: take the index
@@ -6814,7 +6897,7 @@ fn search_inter_block(
68146897 if extra_ref_new_mv() && !skip_search {
68156898 let (gseeds, gn) = mv_seeds(gstack);
68166899 #[cfg(test)]
6817- let t = std::time::Instant::now();
6900+ let t = stage_start();
68186901 let gfound = motion::search(
68196902 &g.y,
68206903 g.width,
@@ -6831,7 +6914,7 @@ fn search_inter_block(
68316914 ref_distance(ref_frame),
68326915 );
68336916 #[cfg(test)]
6834- stage_add(0, t.elapsed());
6917+ stage_since(0, t);
68356918 let gmv = round_to_valid_mv(gfound.mv, gstack.pred_mv);
68366919 extra_new_mvs.push((ref_frame, gmv));
68376920 if let Some((bits, ref_mv_idx)) = best_new_mv_syntax(gstack, gmv) {
@@ -9349,6 +9432,26 @@ mod tests {
93499432 (width, height)
93509433 }
93519434
9435+ /// lane-av1cap: the final filter replay against the capture decode it
9436+ /// replaced, at the block-padded and straddling sizes the whole-frame
9437+ /// tests cover -- `set_verify_final_replay` makes
9438+ /// [`pick_and_apply_filters`] run both on every frame and assert, per
9439+ /// frame, that the spliced picture and the two filter stages the
9440+ /// restoration search reads are bit-identical over the frame's own crop.
9441+ #[test]
9442+ fn the_final_filter_replay_matches_the_capture_decode_at_odd_sizes() {
9443+ let fctx = &crate::decode::FrameCtx::new();
9444+ crate::decode::set_verify_final_replay(true);
9445+ for &(width, height) in
9446+ &[(64usize, 56usize), (216, 96), (192, 120), (640, 352), (1280, 720)]
9447+ {
9448+ eprintln!("--- {width}x{height}");
9449+ let picture = test_card(width, height);
9450+ let _ = encode_key_frame_with_ctx(&picture, 100, 0.5, fctx).unwrap();
9451+ }
9452+ crate::decode::set_verify_final_replay(false);
9453+ }
9454+
93529455 /// A key frame at an arbitrary even size round-trips through ffmpeg over
93539456 /// its own render rectangle: [`Encoded::reconstruction`] is exactly the
93549457 /// picture's own size, and what ffmpeg decodes over that same top-left
crates/ec-av1/src/encoder.rsmodified+42 -0
@@ -2092,6 +2092,48 @@ mod tests {
20922092 }
20932093 }
20942094
2095+ /// lane-av1cap: the filter stage's FINAL replay -- the one that replaced
2096+ /// the capture decode -- against that decode. `set_verify_final_replay`
2097+ /// makes `encode::pick_and_apply_filters` run both on every frame and
2098+ /// assert, per frame, that the spliced picture and the two filter stages
2099+ /// the loop-restoration search reads are bit-identical.
2100+ #[test]
2101+ fn filter_replay_final_matches_the_capture_decode() {
2102+ let _gate_lock = crate::stream::tests::lock_gate_counters();
2103+ let run = |sources: &[Picture], width: usize, height: usize, tiles: (u32, u32)| {
2104+ let config = EncoderConfig {
2105+ width,
2106+ height,
2107+ base_q_idx: 120,
2108+ gop: 2,
2109+ colour: Colour::Bt709Limited,
2110+ tile_cols_log2: tiles.0,
2111+ tile_rows_log2: tiles.1,
2112+ };
2113+ let mut enc = Av1Encoder::new(config).unwrap();
2114+ for packet in encode_all(&mut enc, sources) {
2115+ assert!(!packet.data.is_empty());
2116+ }
2117+ };
2118+ crate::decode::set_verify_final_replay(true);
2119+ // 320x160 crops (its coding surface is one superblock row taller);
2120+ // 320x192 is a whole number of superblocks.
2121+ for (width, height) in [(320usize, 160usize), (320, 192)] {
2122+ let sources: Vec<Picture> =
2123+ (0..4).map(|t| test_card(width, height, t * 3)).collect();
2124+ run(&sources, width, height, (0, 0));
2125+ }
2126+ // Real gate content, where the CDEF preset search actually chooses
2127+ // `bits > 0` and the tile is re-coded, one tile and four.
2128+ if let Some(sources) = h264_clip_frames(640, 384, 3) {
2129+ run(&sources, 640, 384, (0, 0));
2130+ run(&sources, 640, 384, (1, 1));
2131+ } else {
2132+ eprintln!("SKIP the gate-clip arm: no fixture or no ffmpeg");
2133+ }
2134+ crate::decode::set_verify_final_replay(false);
2135+ }
2136+
20952137 /// lane-av1fpar: where a frame's wall actually goes, stage by stage --
20962138 /// the per-tile search, the tile write, the frame-level filter search
20972139 /// (with its deblock/CDEF replay and per-64 SSE inside it), the capture
crates/ec-av1/src/mc.rsmodified+4 -4
@@ -1137,7 +1137,7 @@ pub(crate) fn predict_with_filters_kern(
11371137 hit!(INTER_PRED_HITS);
11381138
11391139 #[cfg(test)]
1140- let stage_t = std::time::Instant::now();
1140+ let stage_t = crate::encode::stage_start();
11411141
11421142 let x0 = x_q4.div_euclid(16);
11431143 let xfrac = x_q4.rem_euclid(16) as usize;
@@ -1188,7 +1188,7 @@ pub(crate) fn predict_with_filters_kern(
11881188 }
11891189 }
11901190 #[cfg(test)]
1191- crate::encode::stage_add(1, stage_t.elapsed());
1191+ crate::encode::stage_since(1, stage_t);
11921192 return;
11931193 }
11941194
@@ -1244,7 +1244,7 @@ pub(crate) fn predict_with_filters_kern(
12441244 unsafe { std::slice::from_raw_parts(reference.as_ptr().cast::<i16>(), reference.len()) };
12451245 vpass_row_u16(&src[base..], block_w, stride, block_h, &v16, max, dst);
12461246 #[cfg(test)]
1247- crate::encode::stage_add(1, stage_t.elapsed());
1247+ crate::encode::stage_since(1, stage_t);
12481248 return;
12491249 }
12501250 }
@@ -1265,7 +1265,7 @@ pub(crate) fn predict_with_filters_kern(
12651265 });
12661266
12671267 #[cfg(test)]
1268- crate::encode::stage_add(1, stage_t.elapsed());
1268+ crate::encode::stage_since(1, stage_t);
12691269 }
12701270
12711271 /// `REF_SCALE_SHIFT`'s "no scaling" value (spec 7.11.3.3, libaom `scale.h`
crates/ec-av1/src/par.rsmodified+1 -1
@@ -531,7 +531,7 @@ static STAGE_NS: [std::sync::atomic::AtomicU64; STAGES.len()] =
531531 /// [`set_stage_times`] from a gate (the env snapshot is process-constant).
532532 static STAGE_ON: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
533533
534-fn stage_times() -> bool {
534+pub(crate) fn stage_times() -> bool {
535535 match STAGE_ON.load(std::sync::atomic::Ordering::Relaxed) {
536536 0 => {
537537 let on = 1 + usize::from(crate::envflags::is_set("EC_AV1_STAGE_TIMES"));