ec-vp8: token-decision trace tooling (G n/band/ctx/prob annotations, env-gated) for the M2 cross-MB context hunt; unit suite stays green
Browse files at this commit2e92feca815a23c88e6afd7518519da2c7a95fdeParent: d24d4c41 changed file +78 -44
- crates/ec-vp8/src/tokens.rs +78 -44
crates/ec-vp8/src/tokens.rsmodified+78 -44
| @@ -147,6 +147,7 @@ impl TokenContexts { | ||
| 147 | 147 | /// type; `n0` the first coded position (1 for Y blocks after a Y2). |
| 148 | 148 | /// Returns the number of significant coefficients. Coefficients are |
| 149 | 149 | /// written dequantized into `out` (natural order). |
| 150 | +#[allow(unused_assignments)] // v is always read on every path that matters | |
| 150 | 151 | fn get_coeffs( |
| 151 | 152 | bc: &mut BoolDecoder<'_>, |
| 152 | 153 | probs_t: &[[[u8; 11]; 3]; 8], |
| @@ -156,69 +157,102 @@ fn get_coeffs( | ||
| 156 | 157 | ac_factor: i16, |
| 157 | 158 | out: &mut [i16; 16], |
| 158 | 159 | ) -> usize { |
| 160 | + let trace = std::env::var_os("EC_VP8_TRACE").is_some(); | |
| 161 | + macro_rules! ann { | |
| 162 | + ($n:expr, $band:expr, $ctx:expr, $node:expr) => { | |
| 163 | + if trace { | |
| 164 | + eprintln!("G n={} band={} ctx={} prob={}", $n, $band, $ctx, $node); | |
| 165 | + } | |
| 166 | + }; | |
| 167 | + } | |
| 159 | 168 | let mut n = n0; |
| 160 | - let mut p = &probs_t[BANDS[n0] as usize][ctx]; | |
| 169 | + let mut band = BANDS[n0]; | |
| 170 | + let mut c = ctx; | |
| 171 | + let mut p = &probs_t[band as usize][c]; | |
| 172 | + ann!(n, band, c, 0); | |
| 161 | 173 | if !bc.read_bool(p[0]) { |
| 162 | 174 | return 0; // immediate EOB |
| 163 | 175 | } |
| 164 | 176 | loop { |
| 165 | 177 | n += 1; |
| 166 | 178 | let mut v: i32; |
| 179 | + ann!(n, band, c, 1); | |
| 167 | 180 | if !bc.read_bool(p[1]) { |
| 168 | - // DCT_0: next decision skips the EOB branch (§13.2). | |
| 169 | - p = &probs_t[BANDS[n] as usize][0]; | |
| 181 | + // DCT_0: the next decision skips the EOB branch (§13.2). | |
| 182 | + c = 0; | |
| 183 | + band = BANDS[n]; | |
| 184 | + p = &probs_t[band as usize][0]; | |
| 170 | 185 | } else { |
| 186 | + ann!(n, band, c, 2); | |
| 171 | 187 | if !bc.read_bool(p[2]) { |
| 172 | - p = &probs_t[BANDS[n] as usize][1]; | |
| 188 | + c = 1; | |
| 189 | + band = BANDS[n]; | |
| 190 | + p = &probs_t[band as usize][1]; | |
| 173 | 191 | v = 1; |
| 174 | - } else if !bc.read_bool(p[3]) { | |
| 175 | - if !bc.read_bool(p[4]) { | |
| 176 | - v = 2; | |
| 192 | + } else { | |
| 193 | + ann!(n, band, c, 3); | |
| 194 | + if !bc.read_bool(p[3]) { | |
| 195 | + ann!(n, band, c, 4); | |
| 196 | + if !bc.read_bool(p[4]) { | |
| 197 | + v = 2; | |
| 198 | + } else { | |
| 199 | + ann!(n, band, c, 5); | |
| 200 | + v = 3 + i32::from(bc.read_bool(p[5])); | |
| 201 | + } | |
| 202 | + let _ = v; | |
| 177 | 203 | } else { |
| 178 | - v = 3 + i32::from(bc.read_bool(p[5])); | |
| 204 | + ann!(n, band, c, 6); | |
| 205 | + if !bc.read_bool(p[6]) { | |
| 206 | + ann!(n, band, c, 7); | |
| 207 | + if !bc.read_bool(p[7]) { | |
| 208 | + ann!(n, band, c, 91); | |
| 209 | + v = 5 + i32::from(bc.read_bool(159)); | |
| 210 | + } else { | |
| 211 | + ann!(n, band, c, 161); | |
| 212 | + v = 7 + 2 * i32::from(bc.read_bool(165)); | |
| 213 | + ann!(n, band, c, 162); | |
| 214 | + v += i32::from(bc.read_bool(145)); | |
| 215 | + } | |
| 216 | + } else { | |
| 217 | + ann!(n, band, c, 8); | |
| 218 | + let bit1 = usize::from(bc.read_bool(p[8])); | |
| 219 | + ann!(n, band, c, 9); | |
| 220 | + let bit0 = usize::from(bc.read_bool(p[9 + bit1])); | |
| 221 | + let cat = 2 * bit1 + bit0; | |
| 222 | + v = 0; | |
| 223 | + for (i, &prob) in CAT_EXTRA[cat].iter().enumerate() { | |
| 224 | + if prob == 0 { | |
| 225 | + break; | |
| 226 | + } | |
| 227 | + ann!(n, band, c, 30 + i); | |
| 228 | + v += v + i32::from(bc.read_bool(prob)); | |
| 229 | + } | |
| 230 | + v += 3 + (8 << cat); | |
| 231 | + } | |
| 179 | 232 | } |
| 180 | - } else if !bc.read_bool(p[6]) { | |
| 181 | - if !bc.read_bool(p[7]) { | |
| 182 | - v = 5 + i32::from(bc.read_bool(159)); | |
| 233 | + c = 2; | |
| 234 | + band = BANDS[n]; | |
| 235 | + p = &probs_t[band as usize][2]; | |
| 236 | + | |
| 237 | + // Sign flag, then dequantize (first coded position is DC). | |
| 238 | + ann!(n, band, c, 128); | |
| 239 | + let mag = if bc.read_bool(128) { -v } else { v }; | |
| 240 | + out[ZIGZAG[n - 1]] = if n - 1 == 0 { | |
| 241 | + (mag * i32::from(dc_factor)) as i16 | |
| 183 | 242 | } else { |
| 184 | - v = 7 + 2 * i32::from(bc.read_bool(165)); | |
| 185 | - v += i32::from(bc.read_bool(145)); | |
| 243 | + (mag * i32::from(ac_factor)) as i16 | |
| 244 | + }; | |
| 245 | + if n < 16 { | |
| 246 | + ann!(n, band, c, 0); | |
| 186 | 247 | } |
| 187 | - } else { | |
| 188 | - // Categories 3..6: two tree bits pick the category, then | |
| 189 | - // DCTextra (§13.2). | |
| 190 | - let bit1 = usize::from(bc.read_bool(p[8])); | |
| 191 | - let bit0 = usize::from(bc.read_bool(p[9 + bit1])); | |
| 192 | - let cat = 2 * bit1 + bit0; | |
| 193 | - v = 0; | |
| 194 | - for &prob in CAT_EXTRA[cat] { | |
| 195 | - if prob == 0 { | |
| 196 | - break; | |
| 197 | - } | |
| 198 | - v += v + i32::from(bc.read_bool(prob)); | |
| 248 | + if n == 16 || !bc.read_bool(p[0]) { | |
| 249 | + return n; | |
| 199 | 250 | } |
| 200 | - v += 3 + (8 << cat); | |
| 201 | - } | |
| 202 | - | |
| 203 | - // Sign flag, then dequantize (first coded position is DC). | |
| 204 | - let mag = if bc.read_bool(128) { -v } else { v }; | |
| 205 | - out[ZIGZAG[n - 1]] = if n - 1 == 0 { | |
| 206 | - (mag * i32::from(dc_factor)) as i16 | |
| 207 | - } else { | |
| 208 | - (mag * i32::from(ac_factor)) as i16 | |
| 209 | - }; | |
| 210 | - if n < 16 { | |
| 211 | - // Non-zero coefficients set the magnitude context for | |
| 212 | - // the next coefficient (§13.3). | |
| 213 | - p = &probs_t[BANDS[n] as usize][2]; | |
| 214 | 251 | } |
| 215 | - if n == 16 || !bc.read_bool(p[0]) { | |
| 216 | - return n; | |
| 252 | + if n == 16 { | |
| 253 | + return 16; // malformed without EOB: stop like the reference | |
| 217 | 254 | } |
| 218 | 255 | } |
| 219 | - if n == 16 { | |
| 220 | - return 16; // malformed without EOB: stop like the reference | |
| 221 | - } | |
| 222 | 256 | } |
| 223 | 257 | } |
| 224 | 258 |