From 1e371cb774f2df9c364a9eab14dd091292d0e241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 13:33:31 +0300 Subject: [PATCH] U5: remove EC_VP8_TRACE and EC_VP8_DEBUG gates from ec-vp8 Per-read and per-MB eprintln diagnostics and their cached env lookups. tokens::get_coeffs loses the trace macro; the now-unused context cursor goes with it. Output bits unchanged. --- crates/ec-vp8/src/bool.rs | 3 -- crates/ec-vp8/src/decode.rs | 56 ---------------------------------- crates/ec-vp8/src/header.rs | 8 ----- crates/ec-vp8/src/lib.rs | 34 --------------------- crates/ec-vp8/src/tokens.rs | 60 +++++++++++-------------------------- 5 files changed, 17 insertions(+), 144 deletions(-) diff --git a/crates/ec-vp8/src/bool.rs b/crates/ec-vp8/src/bool.rs index b117efe3..58293009 100644 --- a/crates/ec-vp8/src/bool.rs +++ b/crates/ec-vp8/src/bool.rs @@ -111,9 +111,6 @@ impl<'a> BoolDecoder<'a> { self.value |= self.next_byte(); } } - if crate::trace_enabled() { - eprintln!("B {} {} {} {}", self.pos, self.bit_count, prob, u8::from(bit)); - } bit } diff --git a/crates/ec-vp8/src/decode.rs b/crates/ec-vp8/src/decode.rs index f8d30760..792f2001 100644 --- a/crates/ec-vp8/src/decode.rs +++ b/crates/ec-vp8/src/decode.rs @@ -396,9 +396,6 @@ impl Decoder { // `r` (0-based) reads partition `r % n` like the reference. let part = (row - 1) % partitions.len(); for col in 1..=self.mb_cols { - if crate::trace_enabled() { - eprintln!("M {} {}", row - 1, col - 1); - } let mb = self.mbi(row, col).clone(); let has_y2 = mb.has_y2(); let seg_i = usize::from(header.segmentation.enabled) * usize::from(mb.segment_id); @@ -411,9 +408,6 @@ impl Decoder { y2_dc: dq.y2_dc, y2_ac: dq.y2_ac, }; - if crate::trace_enabled() { - eprintln!("T {} {} hy2={}", row - 1, col - 1, has_y2); - } let probs = self.state.coeff_probs; let coeffs = if mb.skip { tokens::skip_mb_tokens(&mut self.token_ctxs, col - 1, has_y2) @@ -606,9 +600,6 @@ impl Decoder { self.token_ctxs.reset_above_row(); // Phase 1: every mode record of the frame, row-major (§16). - if crate::trace_enabled() { - eprintln!("STAGE modes"); - } for row in 1..=self.mb_rows { for col in 1..=self.mb_cols { let mut mb = MbInfo::default(); @@ -758,9 +749,6 @@ impl Decoder { let left = self.mv_neighbor(row, col - 1, sign_bias); let aboveleft = self.mv_neighbor(row - 1, col - 1, sign_bias); let (mut near_mvs, cnt) = modes::find_near_mvs(above, left, aboveleft, to_bias); - if crate::trace_enabled() { - eprintln!("CNT r={} c={} cnt={:?} mvs={:?}", row - 1, col - 1, cnt, near_mvs); - } if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_INTRA] as usize][0]) { mb.mv_ref = 0; // ZEROMV @@ -868,24 +856,6 @@ impl Decoder { mv } }; - if crate::trace_enabled() { - eprintln!( - "SP r={} c={} j={} k={} l=({},{}) a=({},{}) ctx={} mv=({},{}) best=({},{})", - row - 1, - col - 1, - j, - k, - leftmv.0, - leftmv.1, - abovemv.0, - abovemv.1, - ctx, - blockmv.0, - blockmv.1, - best.0, - best.1 - ); - } mb.mv_clamp |= modes::mv_out_of_bounds(blockmv, edges); let count = modes::MBSPLIT_FILL_COUNT[s]; for &off in &modes::MBSPLIT_FILL_OFFSET[s][j * count..(j + 1) * count] { @@ -911,12 +881,6 @@ impl Decoder { if mb.ref_frame == 0 { return self.reconstruct_intra_mb(row, col, mb, coeffs); } - if crate::trace_enabled() { - eprintln!( - "IMB r={} c={} ref={} mvref={} mv=({},{}) clamp={} skip={}", - row, col, mb.ref_frame, mb.mv_ref, mb.mv.0, mb.mv.1, mb.mv_clamp, mb.skip - ); - } let reff = self.refs[usize::from(mb.ref_frame)] .as_ref() .ok_or_else(|| Error::corrupt("VP8 inter MB refers to an empty reference slot"))?; @@ -1005,14 +969,6 @@ impl Decoder { &mut pv, 16, 8, 8, self.mc_bilinear, ); } - if crate::trace_enabled() && row == 0 && col == 0 { - eprintln!( - "CHROMA pred_u[0..8]={:?} refu_vis[0..8]={:?} coeffs.u0dcsum={}", - &pu[0..8], - &reff.u[MC_BORDER * reff.uv_stride + MC_BORDER..MC_BORDER * reff.uv_stride + MC_BORDER + 8], - coeffs.u[0].iter().sum::() - ); - } // Residual: every block adds onto its MC prediction. let y_dcs: [i16; 16] = if coeffs.has_y2 { @@ -1126,18 +1082,6 @@ impl Decoder { } else { [0; 16] }; - if crate::debug_enabled() { - eprintln!( - "MB({row},{col}) ymode={} uv={} y2={:?} y2has={} y0[:4]={:?} eobs24={} dcs={:?}", - y_mode, - uv_mode, - coeffs.y2, - coeffs.has_y2, - &coeffs.y[0][..4], - coeffs.eobs[24], - &y_dcs[..4] - ); - } if y_mode == modes::YMode::BPred.as_u8() { for b in 0..16usize { diff --git a/crates/ec-vp8/src/header.rs b/crates/ec-vp8/src/header.rs index cf34ca69..c681bdcf 100644 --- a/crates/ec-vp8/src/header.rs +++ b/crates/ec-vp8/src/header.rs @@ -396,9 +396,6 @@ impl FrameHeader { 128 }; // Interframe-only probability updates. - if crate::trace_enabled() { - eprintln!("STAGE interprobs"); - } let mut ymode_probs_updated = false; let mut uv_mode_probs_updated = false; let (prob_intra, prob_last, prob_gf) = if !key { @@ -431,11 +428,6 @@ impl FrameHeader { } } } - if crate::trace_enabled() { - eprintln!("YUV ymode = {:?} uv = {:?}", state.ymode_probs, state.uv_mode_probs); - eprintln!("MVT c0 = {:?}", state.mv_probs[0]); - eprintln!("MVT c1 = {:?}", state.mv_probs[1]); - } probs } else { (112, 86, 140) // placeholders; only read on inter frames diff --git a/crates/ec-vp8/src/lib.rs b/crates/ec-vp8/src/lib.rs index f3cbf2a7..8256d463 100644 --- a/crates/ec-vp8/src/lib.rs +++ b/crates/ec-vp8/src/lib.rs @@ -30,37 +30,3 @@ pub mod transform; pub use ec_core::{Error, Result}; pub use header::PersistedState; - -/// Whether `EC_VP8_TRACE` decision tracing is enabled, resolved once per -/// process. A `var_os` per bool read was 46% of the decode profile (each -/// call locks std's ENV lock and allocates); `OnceLock::get_or_init` was -/// no better, so the hot path is a single relaxed atomic load. -pub(crate) fn trace_enabled() -> bool { - use std::sync::atomic::{AtomicU8, Ordering}; - static STATE: AtomicU8 = AtomicU8::new(0); - match STATE.load(Ordering::Relaxed) { - 0 => { - let on = std::env::var_os("EC_VP8_TRACE").is_some(); - STATE.store(if on { 2 } else { 1 }, Ordering::Relaxed); - on - } - 1 => false, - _ => true, - } -} - -/// Whether `EC_VP8_DEBUG` per-macroblock dumps are enabled (same shape as -/// [`trace_enabled`]; the check sits in the per-MB reconstruct path). -pub(crate) fn debug_enabled() -> bool { - use std::sync::atomic::{AtomicU8, Ordering}; - static STATE: AtomicU8 = AtomicU8::new(0); - match STATE.load(Ordering::Relaxed) { - 0 => { - let on = std::env::var_os("EC_VP8_DEBUG").is_some(); - STATE.store(if on { 2 } else { 1 }, Ordering::Relaxed); - on - } - 1 => false, - _ => true, - } -} diff --git a/crates/ec-vp8/src/tokens.rs b/crates/ec-vp8/src/tokens.rs index fa3cd270..d59cd228 100644 --- a/crates/ec-vp8/src/tokens.rs +++ b/crates/ec-vp8/src/tokens.rs @@ -156,75 +156,55 @@ fn get_coeffs( ac_factor: i16, out: &mut [i16; 16], ) -> usize { - let trace = crate::trace_enabled(); - // One token decision: decode the bool, then — under EC_VP8_TRACE — - // print the consulted (n, band, ctx, prob) and the decoded bit so the - // stream can be diffed read-for-read against scripts/vp8ref_model.py. - macro_rules! g { - ($n:expr, $band:expr, $c:expr, $prob:expr) => {{ - let bit = bc.read_bool($prob); - if trace { - eprintln!( - "G n={} band={} ctx={} prob={} bit={}", - $n, $band, $c, $prob, u8::from(bit) - ); - } - bit - }}; - } let mut n = n0; let mut band = BANDS[n0]; - let mut c = ctx; - let mut p = &probs_t[band as usize][c]; - if !g!(n, band, c, p[0]) { + let mut p = &probs_t[band as usize][ctx]; + if !bc.read_bool(p[0]) { return 0; // immediate EOB } loop { n += 1; let mut v: i32; - if !g!(n, band, c, p[1]) { + if !bc.read_bool(p[1]) { // DCT_0: the next decision skips the EOB branch (§13.2) and // libvpx forces the context to 0 after a zero token. - c = 0; band = BANDS[n]; p = &probs_t[band as usize][0]; } else { - if !g!(n, band, c, p[2]) { - c = 1; + if !bc.read_bool(p[2]) { band = BANDS[n]; p = &probs_t[band as usize][1]; v = 1; } else { - if !g!(n, band, c, p[3]) { - if !g!(n, band, c, p[4]) { + if !bc.read_bool(p[3]) { + if !bc.read_bool(p[4]) { v = 2; } else { - v = 3 + i32::from(g!(n, band, c, p[5])); + v = 3 + i32::from(bc.read_bool(p[5])); } } else { - if !g!(n, band, c, p[6]) { - if !g!(n, band, c, p[7]) { - v = 5 + i32::from(g!(n, band, c, 159u8)); + if !bc.read_bool(p[6]) { + if !bc.read_bool(p[7]) { + v = 5 + i32::from(bc.read_bool(159u8)); } else { - v = 7 + 2 * i32::from(g!(n, band, c, 165u8)); - v += i32::from(g!(n, band, c, 145u8)); + v = 7 + 2 * i32::from(bc.read_bool(165u8)); + v += i32::from(bc.read_bool(145u8)); } } else { - let bit1 = usize::from(g!(n, band, c, p[8])); - let bit0 = usize::from(g!(n, band, c, p[9 + bit1])); + let bit1 = usize::from(bc.read_bool(p[8])); + let bit0 = usize::from(bc.read_bool(p[9 + bit1])); let cat = 2 * bit1 + bit0; v = 0; for &prob in CAT_EXTRA[cat].iter() { if prob == 0 { break; } - let g = i32::from(g!(n, band, c, prob)); + let g = i32::from(bc.read_bool(prob)); v = v + v + g; } v += 3 + (8 << cat); } } - c = 2; band = BANDS[n]; p = &probs_t[band as usize][2]; } @@ -232,14 +212,14 @@ fn get_coeffs( // Sign flag, then dequantize (first coded position is DC). // Shared by the ONE and magnitude paths, exactly like the // reference walk (libvpx detokenize.c GetCoeffs). - let neg = g!(n, band, c, 128u8); + let neg = bc.read_bool(128u8); let mag = if neg { -v } else { v }; out[ZIGZAG[n - 1]] = if n - 1 == 0 { (mag * i32::from(dc_factor)) as i16 } else { (mag * i32::from(ac_factor)) as i16 }; - if n < 16 && !g!(n, band, c, p[0]) { + if n < 16 && !bc.read_bool(p[0]) { return n; // EOB } } @@ -269,13 +249,7 @@ pub(crate) fn decode_mb_tokens( let n0 = usize::from($ptype == 0); let (l, a) = ctxs.ctx_pair(mb_col, $idx); let ctx = (usize::from(l) + usize::from(a)).min(2); - if crate::trace_enabled() { - eprintln!("TB {} ctx={} l={} a={}", $idx, ctx, l, a); - } let eob = get_coeffs(bc, &probs[$ptype], ctx, n0, $dc, $ac, $slot); - if crate::trace_enabled() { - eprintln!("TBE {} eob={}", $idx, eob); - } out.eobs[$idx] = eob as u8; let t = u8::from(eob > 0); ctxs.set_ctx(mb_col, $idx, t);