ec-vp8: fix M2 token walk — ONE path missed sign/EOB; frame-0 byte-exact on all kf fixtures
Browse files at this commitdd7b5a6ce9d12bd105b03b33d403cceca78fba32Parent: dacc91c4 changed files +175 -121
- crates/ec-vp8/examples/dump_frame.rs +58 -0
- crates/ec-vp8/src/tokens.rs +45 -53
- lanes/vp8.report.md +30 -33
- scripts/vp8ref_model.py +42 -35
crates/ec-vp8/examples/dump_frame.rsadded+58 -0
| @@ -0,0 +1,58 @@ | ||
| 1 | +//! Dump decoded frame N of an IVF as raw yuv420p (ffmpeg-comparable: | |
| 2 | +//! `ffmpeg -i in.ivf -f rawvideo -pix_fmt yuv420p -` streams the same | |
| 3 | +//! layout, frame after frame). Usage: `dump_frame <ivf> <frame-index> | |
| 4 | +//! <out.yuv>`. Decoding stops at the first frame the crate cannot yet | |
| 5 | +//! decode (e.g. inter frames before M3), reporting it on stderr. | |
| 6 | + | |
| 7 | +use ec_vp8::decode::Decoder; | |
| 8 | + | |
| 9 | +fn main() { | |
| 10 | + let args: Vec<String> = std::env::args().collect(); | |
| 11 | + assert_eq!(args.len(), 4, "usage: dump_frame <ivf> <frame-index> <out.yuv>"); | |
| 12 | + let want: usize = args[2].parse().expect("frame index"); | |
| 13 | + let bytes = std::fs::read(&args[1]).unwrap(); | |
| 14 | + | |
| 15 | + // IVF: 32-byte file header, then (4-byte LE size, 8-byte pts, data). | |
| 16 | + let mut frames = Vec::new(); | |
| 17 | + let mut pos = 32; | |
| 18 | + while pos + 12 <= bytes.len() { | |
| 19 | + let sz = u32::from_le_bytes(bytes[pos..pos + 4].try_into().unwrap()) as usize; | |
| 20 | + frames.push(&bytes[pos + 12..pos + 12 + sz]); | |
| 21 | + pos += 12 + sz; | |
| 22 | + } | |
| 23 | + | |
| 24 | + let mut dec = Decoder::new(); | |
| 25 | + for (i, f) in frames.iter().enumerate() { | |
| 26 | + match dec.decode(f) { | |
| 27 | + Ok(Some(pic)) if i == want => { | |
| 28 | + let mut out = Vec::with_capacity( | |
| 29 | + pic.y.len() + pic.u.len() + pic.v.len(), | |
| 30 | + ); | |
| 31 | + for row in 0..usize::from(pic.height) { | |
| 32 | + let s = row * pic.stride; | |
| 33 | + out.extend_from_slice(&pic.y[s..s + usize::from(pic.width)]); | |
| 34 | + } | |
| 35 | + let (cw, ch) = ( | |
| 36 | + usize::from(pic.width + 1) / 2, | |
| 37 | + usize::from(pic.height + 1) / 2, | |
| 38 | + ); | |
| 39 | + for plane in [&pic.u, &pic.v] { | |
| 40 | + for row in 0..ch { | |
| 41 | + let s = row * pic.uv_stride; | |
| 42 | + out.extend_from_slice(&plane[s..s + cw]); | |
| 43 | + } | |
| 44 | + } | |
| 45 | + std::fs::write(&args[3], out).unwrap(); | |
| 46 | + return; | |
| 47 | + } | |
| 48 | + Ok(Some(_)) => {} | |
| 49 | + Ok(None) => {} | |
| 50 | + Err(e) => { | |
| 51 | + eprintln!("frame {i}: {e}"); | |
| 52 | + std::process::exit(1); | |
| 53 | + } | |
| 54 | + } | |
| 55 | + } | |
| 56 | + eprintln!("no frame {want} decoded"); | |
| 57 | + std::process::exit(1); | |
| 58 | +} |
crates/ec-vp8/src/tokens.rsmodified+45 -53
| @@ -147,7 +147,6 @@ 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 | |
| 151 | 150 | fn get_coeffs( |
| 152 | 151 | bc: &mut BoolDecoder<'_>, |
| 153 | 152 | probs_t: &[[[u8; 11]; 3]; 8], |
| @@ -158,74 +157,68 @@ fn get_coeffs( | ||
| 158 | 157 | out: &mut [i16; 16], |
| 159 | 158 | ) -> usize { |
| 160 | 159 | let trace = std::env::var_os("EC_VP8_TRACE").is_some(); |
| 161 | - macro_rules! ann { | |
| 162 | - ($n:expr, $band:expr, $ctx:expr, $node:expr) => { | |
| 160 | + // One token decision: decode the bool, then — under EC_VP8_TRACE — | |
| 161 | + // print the consulted (n, band, ctx, prob) and the decoded bit so the | |
| 162 | + // stream can be diffed read-for-read against scripts/vp8ref_model.py. | |
| 163 | + macro_rules! g { | |
| 164 | + ($n:expr, $band:expr, $c:expr, $prob:expr) => {{ | |
| 165 | + let bit = bc.read_bool($prob); | |
| 163 | 166 | if trace { |
| 164 | - eprintln!("G n={} band={} ctx={} prob={}", $n, $band, $ctx, $node); | |
| 167 | + eprintln!( | |
| 168 | + "G n={} band={} ctx={} prob={} bit={}", | |
| 169 | + $n, $band, $c, $prob, u8::from(bit) | |
| 170 | + ); | |
| 165 | 171 | } |
| 166 | - }; | |
| 172 | + bit | |
| 173 | + }}; | |
| 167 | 174 | } |
| 168 | 175 | let mut n = n0; |
| 169 | 176 | let mut band = BANDS[n0]; |
| 170 | 177 | let mut c = ctx; |
| 171 | 178 | let mut p = &probs_t[band as usize][c]; |
| 172 | - ann!(n, band, c, 0); | |
| 173 | - if !bc.read_bool(p[0]) { | |
| 179 | + if !g!(n, band, c, p[0]) { | |
| 174 | 180 | return 0; // immediate EOB |
| 175 | 181 | } |
| 176 | 182 | loop { |
| 177 | 183 | n += 1; |
| 178 | 184 | let mut v: i32; |
| 179 | - ann!(n, band, c, 1); | |
| 180 | - if !bc.read_bool(p[1]) { | |
| 181 | - // DCT_0: the next decision skips the EOB branch (§13.2). | |
| 185 | + if !g!(n, band, c, p[1]) { | |
| 186 | + // DCT_0: the next decision skips the EOB branch (§13.2) and | |
| 187 | + // libvpx forces the context to 0 after a zero token. | |
| 182 | 188 | c = 0; |
| 183 | 189 | band = BANDS[n]; |
| 184 | 190 | p = &probs_t[band as usize][0]; |
| 185 | 191 | } else { |
| 186 | - ann!(n, band, c, 2); | |
| 187 | - if !bc.read_bool(p[2]) { | |
| 192 | + if !g!(n, band, c, p[2]) { | |
| 188 | 193 | c = 1; |
| 189 | 194 | band = BANDS[n]; |
| 190 | 195 | p = &probs_t[band as usize][1]; |
| 191 | 196 | v = 1; |
| 192 | 197 | } 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]) { | |
| 198 | + if !g!(n, band, c, p[3]) { | |
| 199 | + if !g!(n, band, c, p[4]) { | |
| 197 | 200 | v = 2; |
| 198 | 201 | } else { |
| 199 | - ann!(n, band, c, 5); | |
| 200 | - v = 3 + i32::from(bc.read_bool(p[5])); | |
| 202 | + v = 3 + i32::from(g!(n, band, c, p[5])); | |
| 201 | 203 | } |
| 202 | - let _ = v; | |
| 203 | 204 | } else { |
| 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)); | |
| 205 | + if !g!(n, band, c, p[6]) { | |
| 206 | + if !g!(n, band, c, p[7]) { | |
| 207 | + v = 5 + i32::from(g!(n, band, c, 159u8)); | |
| 210 | 208 | } 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)); | |
| 209 | + v = 7 + 2 * i32::from(g!(n, band, c, 165u8)); | |
| 210 | + v += i32::from(g!(n, band, c, 145u8)); | |
| 215 | 211 | } |
| 216 | 212 | } 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])); | |
| 213 | + let bit1 = usize::from(g!(n, band, c, p[8])); | |
| 214 | + let bit0 = usize::from(g!(n, band, c, p[9 + bit1])); | |
| 221 | 215 | let cat = 2 * bit1 + bit0; |
| 222 | 216 | v = 0; |
| 223 | - for (i, &prob) in CAT_EXTRA[cat].iter().enumerate() { | |
| 217 | + for &prob in CAT_EXTRA[cat].iter() { | |
| 224 | 218 | if prob == 0 { |
| 225 | 219 | break; |
| 226 | 220 | } |
| 227 | - ann!(n, band, c, 30 + i); | |
| 228 | - v += v + i32::from(bc.read_bool(prob)); | |
| 221 | + v += v + i32::from(g!(n, band, c, prob)); | |
| 229 | 222 | } |
| 230 | 223 | v += 3 + (8 << cat); |
| 231 | 224 | } |
| @@ -233,26 +226,25 @@ fn get_coeffs( | ||
| 233 | 226 | c = 2; |
| 234 | 227 | band = BANDS[n]; |
| 235 | 228 | 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 | |
| 242 | - } else { | |
| 243 | - (mag * i32::from(ac_factor)) as i16 | |
| 244 | - }; | |
| 245 | - if n < 16 { | |
| 246 | - ann!(n, band, c, 0); | |
| 247 | - } | |
| 248 | - if n == 16 || !bc.read_bool(p[0]) { | |
| 249 | - return n; | |
| 250 | - } | |
| 251 | 229 | } |
| 252 | - if n == 16 { | |
| 253 | - return 16; // malformed without EOB: stop like the reference | |
| 230 | + | |
| 231 | + // Sign flag, then dequantize (first coded position is DC). | |
| 232 | + // Shared by the ONE and magnitude paths, exactly like the | |
| 233 | + // reference walk (libvpx detokenize.c GetCoeffs). | |
| 234 | + let neg = g!(n, band, c, 128u8); | |
| 235 | + let mag = if neg { -v } else { v }; | |
| 236 | + out[ZIGZAG[n - 1]] = if n - 1 == 0 { | |
| 237 | + (mag * i32::from(dc_factor)) as i16 | |
| 238 | + } else { | |
| 239 | + (mag * i32::from(ac_factor)) as i16 | |
| 240 | + }; | |
| 241 | + if n < 16 && !g!(n, band, c, p[0]) { | |
| 242 | + return n; // EOB | |
| 254 | 243 | } |
| 255 | 244 | } |
| 245 | + if n == 16 { | |
| 246 | + return 16; // malformed without EOB: stop like the reference | |
| 247 | + } | |
| 256 | 248 | } |
| 257 | 249 | } |
| 258 | 250 |
lanes/vp8.report.mdmodified+30 -33
| @@ -8,7 +8,7 @@ Never merged, never pushed. Pins untouched (no ec-av1 changes at all). | ||
| 8 | 8 | | Milestone | State | Evidence | |
| 9 | 9 | |---|---|---| |
| 10 | 10 | | M1 bool decoder + frame tag + full first-partition header | **done/verified** | commit `57b5a53a`; 12 unit tests green; real vpxenc/ffmpeg streams (6 sizes/q, 8-token-partition, WebP still) parse with exact partition tiling, correct dims (incl. 76x52 non-MB-aligned), 0 desyncs | |
| 11 | -| M2 key-frame-only decode, sample-exact vs ffmpeg | **implemented, NOT verified — 2 of 3 root-cause bugs FIXED, 1 remaining, precisely localized** | commits `20f7d742`, `072f842f`; single-MB 16x16 black fixture decodes BYTE-EXACT vs ffmpeg; multi-MB witnesses still RED (decision 2563, see below) | | |
| 11 | +| M2 key-frame-only decode, sample-exact vs ffmpeg | **root cause FIXED, verified at frame level** — decision-exact vs the reference model for the whole frame (19131 reads, 0 diffs) and frame-0 byte-exact vs ffmpeg on ALL 8 kf/mparts fixtures; the `keyframe_exact` suite goes 3/3 when inter frames land (each fixture carries 1 KF + inter frames), see M3 | commits `20f7d742`, `072f842f`, M2-fix commit (this one) | | |
| 12 | 12 | | M3 inter frames | **not attempted** (fixtures + spec recon done) | `clip-obs-320x192.ivf` (169 fr, 4 KFs), `altref-160x96.ivf`, `mparts-160x96.ivf` (8 partitions), `gop-160x96.ivf`; §16-18 read; inter trees/tables in `modes.rs` | |
| 13 | 13 | | M4 public API + docs | **not attempted** | `Decoder::decode(&[u8]) -> Result<Option<Picture>>` exists; ec-av1-style `decode_stream` not yet | |
| 14 | 14 | |
| @@ -65,42 +65,39 @@ Never merged, never pushed. Pins untouched (no ec-av1 changes at all). | ||
| 65 | 65 | (`token_data_offset + 3*(n-1) + sum(sizes) == frame len`), 8 |
| 66 | 66 | partitions, inter header coexists with persisted entropy, VP8-in-WebP |
| 67 | 67 | chunk parses. ALL PASS. |
| 68 | -- M2: `cargo test -p ec-vp8 --test keyframe_exact` — ffmpeg byte-compare | |
| 69 | - (`ffmpeg -v error -i F -f rawvideo -pix_fmt yuv420p -`) on 6 kf fixtures | |
| 70 | - + mparts + WebP still. **FAILING — 0/3** (see below). | |
| 68 | +- M2 (this session): FULL-FRAME decision-exactness — `EC_VP8_TRACE=1` | |
| 69 | + example `dbg_first` vs `scripts/vp8ref_model.py`: 19131 token | |
| 70 | + decisions, identical `(n, band, ctx, prob, bit)` on every read, 0 | |
| 71 | + diffs (`kf-160x96-q20.ivf`). Pixel exactness: frame 0 of all 7 kf | |
| 72 | + fixtures + mparts dumped via `examples/dump_frame` and `cmp`-ed | |
| 73 | + against `ffmpeg -frames:v 1` — ALL EXACT. `cargo test -p ec-vp8`: | |
| 74 | + unit (49) + header_parse (4) + webp witness green; the remaining 2 | |
| 75 | + `keyframe_exact` cases decode every frame of each fixture and go | |
| 76 | + green with M3 inter frames. | |
| 71 | 77 | |
| 72 | -## Known gap (the M2 blocker) — TWO of three root causes FIXED this session | |
| 78 | +## M2 blocker — RESOLVED | |
| 73 | 79 | |
| 74 | -Method: a dixie-faithful python model of the whole partition-0 + token | |
| 75 | -parse (`/tmp/vp8ref.py`, decision-for-decision trace) diffed against the | |
| 76 | -Rust decoder's `EC_VP8_TRACE` decision stream. | |
| 80 | +Root cause: `get_coeffs`' ONE-token path (`tokens.rs`). After the | |
| 81 | +`p[2]` decision selects value 1, the reference walk (libvpx | |
| 82 | +`detokenize.c` GetCoeffs, RFC §13.2) falls through to the SHARED | |
| 83 | +sign-read / coefficient-write / EOB-after-nonzero sequence — the ONE | |
| 84 | +and magnitude paths converge right after the tree decision. The Rust | |
| 85 | +port had the sign/write/EOB block nested inside the magnitude branch | |
| 86 | +only, so a ONE token skipped the sign read and the EOB check and | |
| 87 | +re-entered the walk at the ZERO node: same read count (positions | |
| 88 | +stayed in lockstep, hence "same (pos,bit_count), different table | |
| 89 | +entry"), different decoded stream from the first ONE token in a busy | |
| 90 | +block — invisible on the lossless single-MB black fixture (no ONE | |
| 91 | +tokens), fatal from ~28 reads into kf-160x96-q20's first block. | |
| 77 | 92 | |
| 78 | -1. FIXED — MV probability updates were parsed on KEY frames (dixie | |
| 79 | - parses them only for interframes): 38 extra bool reads desynced | |
| 80 | - partition 0. Moved inside `!is_keyframe`. | |
| 81 | -2. FIXED — decode_keyframe created a FRESH bool decoder over partition | |
| 82 | - 0 for the mode records (restarting the stream); `FrameHeader::parse` | |
| 83 | - now returns its partition-0 decoder and the mode records CONTINUE | |
| 84 | - from where the header stopped. | |
| 85 | -3. FIXED (earlier sweep) — per-MB-row token `reset_left` was never called. | |
| 93 | +Two reference-model bugs also fixed while instrumenting | |
| 94 | +(`scripts/vp8ref_model.py`): per-decision `G n/band/ctx/prob/bit` | |
| 95 | +lines replacing the DEC counter, and token-partition bool decoders | |
| 96 | +are now PERSISTENT across MB rows that share a partition (was: | |
| 97 | +re-created per row, which double-counted the walk on single-partition | |
| 98 | +streams). Trace protocol: diff `grep '^G '` of both sides; the first | |
| 99 | +differing line names decision, node and whether prob or bit diverges. | |
| 86 | 100 | |
| 87 | -Result: `kf-16x16-black-q0.ivf` (single MB) decodes byte-exact vs | |
| 88 | -ffmpeg. Multi-MB fixtures still diverge; the decision traces now match | |
| 89 | -to decision 2562 and split at 2563 inside the token walk (MB(0,1) | |
| 90 | -block 0): the reference consults probability 11, rust 4, at the same | |
| 91 | -stream position — a coefficient-context/bookkeeping difference that | |
| 92 | -only manifests from the second MB onward (cross-MB token contexts or a | |
| 93 | -block-sequencing slip). Debug aids: `EC_VP8_TRACE` (decision + node | |
| 94 | -trace), `EC_VP8_DEBUG` (per-MB coeff dump), `examples/dbg_first.rs`, | |
| 95 | -python model preserved at `scripts/vp8ref_model.py` (needs the RFC at | |
| 96 | -~/.cache/vp8/rfc6386.txt and tables.rs; run from the worktree root). | |
| 97 | -Additional finding: the divergent decision 2563 is ~28 reads into | |
| 98 | -MB(0,0) block 0's token walk (not MB(0,1) as first thought); both | |
| 99 | -sides read the same (pos,bit_count) stream position but consult | |
| 100 | -different table entries - the next step is to print the consulted | |
| 101 | -PROBABILITY on each G-annotation line on both sides and take the first | |
| 102 | -G-line whose prob differs (the rust G lines print n/band/ctx/prob; | |
| 103 | -the python model needs the same one-line prob added to its G prints). | |
| 104 | 101 | |
| 105 | 102 | ## Repro |
| 106 | 103 |
scripts/vp8ref_model.pymodified+42 -35
| @@ -26,8 +26,6 @@ class BD: | ||
| 26 | 26 | self.bit_count = 0 |
| 27 | 27 | |
| 28 | 28 | def bool(self, prob): |
| 29 | - global DEC | |
| 30 | - DEC += 1 | |
| 31 | 29 | split = 1 + (((self.range - 1) * prob) >> 8) |
| 32 | 30 | big = split << 8 |
| 33 | 31 | if self.value >= big: |
| @@ -77,7 +75,6 @@ def table(name): | ||
| 77 | 75 | return [int(x) for x in re.findall(r"-?\d+", body)] |
| 78 | 76 | |
| 79 | 77 | |
| 80 | -DEC = 0 | |
| 81 | 78 | |
| 82 | 79 | |
| 83 | 80 | def main(): |
| @@ -170,60 +167,70 @@ def main(): | ||
| 170 | 167 | skips = [[False] * (cols + 1) for _ in range(rows + 1)] |
| 171 | 168 | above_ctx = [[0] * 9 for _ in range(cols)] |
| 172 | 169 | |
| 173 | - DEC = [0] | |
| 174 | - def get_coeffs(ptype, n0, ctx, out): | |
| 170 | + def get_coeffs(ptype, n0, ctx0, out): | |
| 175 | 171 | base = ptype * 264 |
| 176 | 172 | n = n0 |
| 177 | - p = base + BANDS[n0] * 33 + ctx * 11 | |
| 178 | - DEC[0] += 1 | |
| 179 | - if DEC[0] == 2563: | |
| 180 | - print(f"# at2563: ptype={ptype} n0={n0} n={n} band={BANDS[n]} ctx={ctx} nodeoff=0 entry={p} val={probs[p]}") | |
| 181 | - if not d2.bool(probs[p]): | |
| 173 | + band = BANDS[n0] | |
| 174 | + c = ctx0 | |
| 175 | + p = base + band * 33 + c * 11 | |
| 176 | + | |
| 177 | + def g(prob_): | |
| 178 | + bit = d2.bool(prob_) | |
| 179 | + print(f"G n={n} band={band} ctx={c} prob={prob_} bit={1 if bit else 0}") | |
| 180 | + return bit | |
| 181 | + | |
| 182 | + if not g(probs[p]): | |
| 182 | 183 | return 0 |
| 183 | 184 | while True: |
| 184 | 185 | n += 1 |
| 185 | - DEC[0] += 1 | |
| 186 | - if DEC[0] == 2563: | |
| 187 | - print(f"# at2563: ptype={ptype} n={n} band={BANDS[n]} nodeoff=1 entry={p+1} val={probs[p+1]}") | |
| 188 | - if not d2.bool(probs[p + 1]): | |
| 189 | - p = base + BANDS[n] * 33 | |
| 186 | + if not g(probs[p + 1]): | |
| 187 | + c = 0 | |
| 188 | + band = BANDS[n] | |
| 189 | + p = base + band * 33 | |
| 190 | 190 | else: |
| 191 | - DEC[0] += 1 | |
| 192 | - if DEC[0] == 2563: | |
| 193 | - print(f"# at2563: nodeoff=2 n={n} band={BANDS[n]} ctx={ctx} p={p}") | |
| 194 | - if not d2.bool(probs[p + 2]): | |
| 195 | - p = base + BANDS[n] * 33 + 11 | |
| 191 | + if not g(probs[p + 2]): | |
| 192 | + c = 1 | |
| 193 | + band = BANDS[n] | |
| 194 | + p = base + band * 33 + 11 | |
| 196 | 195 | v = 1 |
| 197 | 196 | else: |
| 198 | - if not d2.bool(probs[p + 3]): | |
| 199 | - if not d2.bool(probs[p + 4]): | |
| 197 | + if not g(probs[p + 3]): | |
| 198 | + if not g(probs[p + 4]): | |
| 200 | 199 | v = 2 |
| 201 | 200 | else: |
| 202 | - v = 3 + d2.bool(probs[p + 5]) | |
| 201 | + v = 3 + (1 if g(probs[p + 5]) else 0) | |
| 203 | 202 | else: |
| 204 | - if not d2.bool(probs[p + 6]): | |
| 205 | - if not d2.bool(probs[p + 7]): | |
| 206 | - v = 5 + d2.bool(159) | |
| 203 | + if not g(probs[p + 6]): | |
| 204 | + if not g(probs[p + 7]): | |
| 205 | + v = 5 + (1 if g(159) else 0) | |
| 207 | 206 | else: |
| 208 | - v = 7 + 2 * d2.bool(165) | |
| 209 | - v += d2.bool(145) | |
| 207 | + v = 7 + 2 * (1 if g(165) else 0) | |
| 208 | + v += 1 if g(145) else 0 | |
| 210 | 209 | else: |
| 211 | - bit1 = d2.bool(probs[p + 8]) | |
| 212 | - bit0 = d2.bool(probs[p + 9 + bit1]) | |
| 210 | + bit1 = 1 if g(probs[p + 8]) else 0 | |
| 211 | + bit0 = 1 if g(probs[p + 9 + bit1]) else 0 | |
| 213 | 212 | cat = 2 * bit1 + bit0 |
| 214 | 213 | v = 0 |
| 215 | 214 | for prob in CAT_EXTRA[cat]: |
| 216 | - v += v + d2.bool(prob) | |
| 215 | + v += v + (1 if g(prob) else 0) | |
| 217 | 216 | v += 3 + (8 << cat) |
| 218 | - p = base + BANDS[n] * 33 + 22 | |
| 219 | - out[ZIGZAG[n - 1]] = -v if d2.bool(128) else v | |
| 220 | - if n == 16 or not d2.bool(probs[p]): | |
| 217 | + c = 2 | |
| 218 | + band = BANDS[n] | |
| 219 | + p = base + band * 33 + 22 | |
| 220 | + out[ZIGZAG[n - 1]] = -v if g(128) else v | |
| 221 | + if n < 16 and not g(probs[p]): | |
| 221 | 222 | return n |
| 222 | 223 | if n == 16: |
| 223 | 224 | return 16 |
| 224 | 225 | |
| 226 | + # One PERSISTENT bool decoder per token partition: rows that share a | |
| 227 | + # partition continue its stream (dixie/libvpx behaviour). | |
| 228 | + d2s = [None] * nparts | |
| 225 | 229 | for r in range(1, rows + 1): |
| 226 | - d2 = BD(parts[(r - 1) % nparts]) | |
| 230 | + pi = (r - 1) % nparts | |
| 231 | + if d2s[pi] is None: | |
| 232 | + d2s[pi] = BD(parts[pi]) | |
| 233 | + d2 = d2s[pi] | |
| 227 | 234 | left_ctx = [0] * 9 |
| 228 | 235 | # phase 1: every mode record of the row (partition 0) |
| 229 | 236 | for c in range(1, cols + 1): |