ec-vp8 M3 debugging: inter census localization — model extended to inter frames, divergence pinned to MB(2,7) census count
Browse files at this commit1c98b098660995973942bb10281a4fa7b4774e61Parent: 10ef9be3 changed files +573 -189
- crates/ec-vp8/src/decode.rs +22 -0
- crates/ec-vp8/src/header.rs +6 -0
- scripts/vp8ref_model.py +545 -189
crates/ec-vp8/src/decode.rsmodified+22 -0
| @@ -558,6 +558,7 @@ impl Decoder { | |||
| 558 | ]; | 558 | ]; |
| 559 | 559 | ||
| 560 | // Phase 1: every mode record of the frame, row-major (§16). | 560 | // Phase 1: every mode record of the frame, row-major (§16). |
| 561 | + eprintln!("STAGE modes"); | ||
| 561 | for row in 1..=self.mb_rows { | 562 | for row in 1..=self.mb_rows { |
| 562 | for col in 1..=self.mb_cols { | 563 | for col in 1..=self.mb_cols { |
| 563 | let mut mb = MbInfo::default(); | 564 | let mut mb = MbInfo::default(); |
| @@ -708,6 +709,9 @@ impl Decoder { | |||
| 708 | let left = self.mv_neighbor(row, col - 1, sign_bias); | 709 | let left = self.mv_neighbor(row, col - 1, sign_bias); |
| 709 | let aboveleft = self.mv_neighbor(row - 1, col - 1, sign_bias); | 710 | let aboveleft = self.mv_neighbor(row - 1, col - 1, sign_bias); |
| 710 | let (mut near_mvs, cnt) = modes::find_near_mvs(above, left, aboveleft, to_bias); | 711 | let (mut near_mvs, cnt) = modes::find_near_mvs(above, left, aboveleft, to_bias); |
| 712 | + if std::env::var_os("EC_VP8_TRACE").is_some() { | ||
| 713 | + eprintln!("CNT r={} c={} cnt={:?} mvs={:?}", row - 1, col - 1, cnt, near_mvs); | ||
| 714 | + } | ||
| 711 | 715 | ||
| 712 | if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_INTRA] as usize][0]) { | 716 | if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_INTRA] as usize][0]) { |
| 713 | mb.mv_ref = 0; // ZEROMV | 717 | mb.mv_ref = 0; // ZEROMV |
| @@ -815,6 +819,24 @@ impl Decoder { | |||
| 815 | mv | 819 | mv |
| 816 | } | 820 | } |
| 817 | }; | 821 | }; |
| 822 | + if std::env::var_os("EC_VP8_TRACE").is_some() { | ||
| 823 | + eprintln!( | ||
| 824 | + "SP r={} c={} j={} k={} l=({},{}) a=({},{}) ctx={} mv=({},{}) best=({},{})", | ||
| 825 | + row - 1, | ||
| 826 | + col - 1, | ||
| 827 | + j, | ||
| 828 | + k, | ||
| 829 | + leftmv.0, | ||
| 830 | + leftmv.1, | ||
| 831 | + abovemv.0, | ||
| 832 | + abovemv.1, | ||
| 833 | + ctx, | ||
| 834 | + blockmv.0, | ||
| 835 | + blockmv.1, | ||
| 836 | + best.0, | ||
| 837 | + best.1 | ||
| 838 | + ); | ||
| 839 | + } | ||
| 818 | mb.mv_clamp |= modes::mv_out_of_bounds(blockmv, edges); | 840 | mb.mv_clamp |= modes::mv_out_of_bounds(blockmv, edges); |
| 819 | let count = modes::MBSPLIT_FILL_COUNT[s]; | 841 | let count = modes::MBSPLIT_FILL_COUNT[s]; |
| 820 | for &off in &modes::MBSPLIT_FILL_OFFSET[s][j * count..(j + 1) * count] { | 842 | for &off in &modes::MBSPLIT_FILL_OFFSET[s][j * count..(j + 1) * count] { |
crates/ec-vp8/src/header.rsmodified+6 -0
| @@ -392,6 +392,7 @@ impl FrameHeader { | |||
| 392 | 128 | 392 | 128 |
| 393 | }; | 393 | }; |
| 394 | // Interframe-only probability updates. | 394 | // Interframe-only probability updates. |
| 395 | + eprintln!("STAGE interprobs"); | ||
| 395 | let mut ymode_probs_updated = false; | 396 | let mut ymode_probs_updated = false; |
| 396 | let mut uv_mode_probs_updated = false; | 397 | let mut uv_mode_probs_updated = false; |
| 397 | let (prob_intra, prob_last, prob_gf) = if !key { | 398 | let (prob_intra, prob_last, prob_gf) = if !key { |
| @@ -420,6 +421,11 @@ impl FrameHeader { | |||
| 420 | } | 421 | } |
| 421 | } | 422 | } |
| 422 | } | 423 | } |
| 424 | + if std::env::var_os("EC_VP8_TRACE").is_some() { | ||
| 425 | + eprintln!("YUV ymode = {:?} uv = {:?}", state.ymode_probs, state.uv_mode_probs); | ||
| 426 | + eprintln!("MVT c0 = {:?}", state.mv_probs[0]); | ||
| 427 | + eprintln!("MVT c1 = {:?}", state.mv_probs[1]); | ||
| 428 | + } | ||
| 423 | probs | 429 | probs |
| 424 | } else { | 430 | } else { |
| 425 | (112, 86, 140) // placeholders; only read on inter frames | 431 | (112, 86, 140) // placeholders; only read on inter frames |
scripts/vp8ref_model.pymodified+545 -189
| @@ -1,10 +1,9 @@ | |||
| 1 | #!/usr/bin/env python3 | 1 | #!/usr/bin/env python3 |
| 2 | -"""Dixie-faithful full key-frame VP8 decode model (modes + tokens, no | 2 | +"""Dixie-faithful multi-frame VP8 decode model (header + mode records + |
| 3 | -pixels) with per-decision tracing, interleaved per MB row exactly like | 3 | +tokens, no pixels) with per-read `B` and per-token-decision `G` tracing |
| 4 | -the reference decoder. Diff its `B` lines against the Rust decoder's | 4 | +that mirror the Rust decoder's EC_VP8_TRACE output byte-for-byte. |
| 5 | -EC_VP8_TRACE output. | ||
| 6 | 5 | ||
| 7 | -Usage: python3 vp8ref.py frame.ivf | 6 | +Usage: python3 vp8ref_model.py frame.ivf [max-frames] |
| 8 | """ | 7 | """ |
| 9 | import re | 8 | import re |
| 10 | import sys | 9 | import sys |
| @@ -75,11 +74,46 @@ def table(name): | |||
| 75 | return [int(x) for x in re.findall(r"-?\d+", body)] | 74 | return [int(x) for x in re.findall(r"-?\d+", body)] |
| 76 | 75 | ||
| 77 | 76 | ||
| 77 | +# --------------------------------------------------------------------------- | ||
| 78 | +# Inter-parse constants (RFC 6386; cross-checked against libvpx modecont.c / | ||
| 79 | +# findnearmv.h / blockd.h — see managed skill ec-vp8-lane-recipe). | ||
| 80 | +MODE_CONTEXTS = [ | ||
| 81 | + [7, 1, 1, 143], | ||
| 82 | + [14, 18, 14, 107], | ||
| 83 | + [135, 64, 57, 68], | ||
| 84 | + [60, 56, 128, 65], | ||
| 85 | + [159, 134, 128, 34], | ||
| 86 | + [234, 188, 128, 28], | ||
| 87 | +] | ||
| 88 | +SUB_MV_REF_PROBS = [ | ||
| 89 | + [147, 136, 18], | ||
| 90 | + [106, 145, 1], | ||
| 91 | + [179, 121, 1], | ||
| 92 | + [223, 1, 34], | ||
| 93 | + [208, 1, 1], | ||
| 94 | +] | ||
| 95 | +MV_PARTITION_TREE = [-3, 2, -2, 4, 0, -1] | ||
| 96 | +MV_PARTITION_PROBS = [110, 111, 150] | ||
| 97 | +SUB_MV_REF_TREE = [0, 2, -1, 4, -2, -3] | ||
| 98 | +SMALL_MV_TREE = [2, 8, 4, 6, 0, -1, -2, -3, 10, 12, -4, -5, -6, -7] | ||
| 99 | +MBSPLIT_OFFSET = [ | ||
| 100 | + [0, 8], [0, 2], [0, 2, 8, 10], list(range(16)), | ||
| 101 | +] | ||
| 102 | +MBSPLIT_FILL_COUNT = [8, 8, 4, 1] | ||
| 103 | +MBSPLIT_FILL_OFFSET = [ | ||
| 104 | + list(range(16)), | ||
| 105 | + [0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15], | ||
| 106 | + [0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15], | ||
| 107 | + list(range(16)), | ||
| 108 | +] | ||
| 78 | 109 | ||
| 79 | 110 | ||
| 80 | def main(): | 111 | def main(): |
| 81 | CUP = table("COEFF_UPDATE_PROBS") | 112 | CUP = table("COEFF_UPDATE_PROBS") |
| 82 | KF_BMODE = table("KF_BMODE_PROB") | 113 | KF_BMODE = table("KF_BMODE_PROB") |
| 114 | + DEF_COEF = table("DEFAULT_COEFF_PROBS") | ||
| 115 | + DEF_MV = table("DEFAULT_MV_PROBS") | ||
| 116 | + MV_UP = table("MV_UPDATE_PROBS") | ||
| 83 | BANDS = [0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 0] | 117 | BANDS = [0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 0] |
| 84 | ZIGZAG = [0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15] | 118 | ZIGZAG = [0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15] |
| 85 | CAT_EXTRA = [ | 119 | CAT_EXTRA = [ |
| @@ -91,201 +125,523 @@ def main(): | |||
| 91 | KF_YMODE_TREE = [-4, 2, 4, 6, 0, -1, -2, -3] | 125 | KF_YMODE_TREE = [-4, 2, 4, 6, 0, -1, -2, -3] |
| 92 | BMODE_TREE = [0, 2, -1, 4, -2, 6, 8, 12, -3, 10, -5, -6, -4, 14, -7, 16, -8, -9] | 126 | BMODE_TREE = [0, 2, -1, 4, -2, 6, 8, 12, -3, 10, -5, -6, -4, 14, -7, 16, -8, -9] |
| 93 | UV_MODE_TREE = [0, 2, -1, 4, -2, -3] | 127 | UV_MODE_TREE = [0, 2, -1, 4, -2, -3] |
| 94 | - YMODE_MAP = {0: 0, 1: 2, 2: 3, 3: 1} # 16x16 mode -> bmode context | 128 | + YMODE_TREE = [0, 2, 4, 6, -1, -2, -3, -4] |
| 129 | + YMODE_MAP = {0: 0, 1: 2, 2: 3, 3: 1} | ||
| 95 | LEFT = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8] | 130 | LEFT = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8] |
| 96 | ABOVE = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8] | 131 | ABOVE = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8] |
| 97 | 132 | ||
| 98 | raw = open(sys.argv[1], "rb").read() | 133 | raw = open(sys.argv[1], "rb").read() |
| 99 | - sz = int.from_bytes(raw[32:36], "little") | 134 | + max_frames = int(sys.argv[2]) if len(sys.argv) > 2 else 2 |
| 100 | - frame = raw[44 : 44 + sz] | 135 | + frames = [] |
| 101 | - tag = int.from_bytes(frame[0:3], "little") | 136 | + pos = 32 |
| 102 | - part0_sz = tag >> 5 | 137 | + while pos + 12 <= len(raw): |
| 103 | - w = int.from_bytes(frame[6:8], "little") & 0x3FFF | 138 | + sz = int.from_bytes(raw[pos : pos + 4], "little") |
| 104 | - h = int.from_bytes(frame[8:10], "little") & 0x3FFF | 139 | + frames.append(raw[pos + 12 : pos + 12 + sz]) |
| 105 | - cols, rows = (w + 15) // 16, (h + 15) // 16 | 140 | + pos += 12 + sz |
| 106 | - | 141 | + |
| 107 | - d = BD(frame[10 : 10 + part0_sz]) | 142 | + w = h = 0 |
| 108 | - d.lit(2) # colour space + clamping | 143 | + cols = rows = 0 |
| 109 | - # segmentation | 144 | + ctx = { |
| 110 | - if d.bool(128): | 145 | + "coef": DEF_COEF[:], |
| 111 | - d.bool(128) | 146 | + "mv": DEF_MV[:], |
| 112 | - if d.bool(128): | 147 | + "ymode": [145, 156, 163, 128], |
| 113 | - d.bool(128) | 148 | + "uv": [142, 114, 183], |
| 114 | - for _ in range(4): | 149 | + } |
| 115 | - d.maybe_int(7) | 150 | + probs = None |
| 116 | - for _ in range(4): | 151 | + seg_map = [0] * 65536 |
| 117 | - d.maybe_int(6) | 152 | + |
| 118 | - # loop filter header | 153 | + for fi, frame in enumerate(frames[:max_frames]): |
| 119 | - d.lit(1) | 154 | + tag = int.from_bytes(frame[0:3], "little") |
| 120 | - d.lit(6) | 155 | + kf = (frame[0] & 1) == 0 |
| 121 | - d.lit(3) | 156 | + part0_sz = tag >> 5 |
| 122 | - if d.bool(128): | 157 | + # Uncompressed data chunk: key frames carry start code + dims |
| 158 | + # (partition 0 at byte 10); inter frames start partition 0 at | ||
| 159 | + # byte 3 (RFC 6386 §9.1). | ||
| 160 | + hdr0 = 10 if kf else 3 | ||
| 161 | + d = BD(frame[hdr0 : hdr0 + part0_sz]) | ||
| 162 | + | ||
| 163 | + if kf: | ||
| 164 | + w = int.from_bytes(frame[6:8], "little") & 0x3FFF | ||
| 165 | + h = int.from_bytes(frame[8:10], "little") & 0x3FFF | ||
| 166 | + cols, rows = (w + 15) // 16, (h + 15) // 16 | ||
| 167 | + d.lit(2) # colour space + clamping | ||
| 168 | + ctx["coef"] = DEF_COEF[:] | ||
| 169 | + | ||
| 170 | + # segmentation | ||
| 171 | + seg_enabled = d.bool(128) | ||
| 172 | + seg_update_map = False | ||
| 173 | + seg_tree_probs = [255, 255, 255] | ||
| 174 | + if seg_enabled: | ||
| 175 | + seg_update_map = d.bool(128) | ||
| 176 | + if d.bool(128): | ||
| 177 | + d.bool(128) | ||
| 178 | + for _ in range(4): | ||
| 179 | + d.maybe_int(7) | ||
| 180 | + for _ in range(4): | ||
| 181 | + d.maybe_int(6) | ||
| 182 | + if seg_update_map: | ||
| 183 | + seg_tree_probs = [d.lit(8) for _ in range(3)] | ||
| 184 | + # loop filter header | ||
| 185 | + d.lit(1) | ||
| 186 | + d.lit(6) | ||
| 187 | + d.lit(3) | ||
| 123 | if d.bool(128): | 188 | if d.bool(128): |
| 124 | - for _ in range(4): | 189 | + if d.bool(128): |
| 125 | - d.maybe_int(6) | 190 | + for _ in range(4): |
| 126 | - for _ in range(4): | 191 | + d.maybe_int(6) |
| 127 | - d.maybe_int(6) | 192 | + for _ in range(4): |
| 128 | - # token partitions | 193 | + d.maybe_int(6) |
| 129 | - log2 = d.lit(2) | 194 | + # token partitions |
| 130 | - nparts = 1 << log2 | 195 | + log2 = d.lit(2) |
| 131 | - off = 10 + part0_sz | 196 | + nparts = 1 << log2 |
| 132 | - sizes = [] | 197 | + off = hdr0 + part0_sz |
| 133 | - for i in range(nparts): | 198 | + sizes = [] |
| 134 | - if i < nparts - 1: | 199 | + for i in range(nparts): |
| 135 | - sizes.append(int.from_bytes(frame[off : off + 3], "little")) | 200 | + if i < nparts - 1: |
| 136 | - off += 3 | 201 | + sizes.append(int.from_bytes(frame[off : off + 3], "little")) |
| 202 | + off += 3 | ||
| 203 | + else: | ||
| 204 | + sizes.append(len(frame) - off) | ||
| 205 | + parts = [] | ||
| 206 | + po = off | ||
| 207 | + for i in range(nparts): | ||
| 208 | + parts.append(frame[po : po + sizes[i]]) | ||
| 209 | + po += sizes[i] | ||
| 210 | + # quant | ||
| 211 | + d.lit(7) | ||
| 212 | + for _ in range(5): | ||
| 213 | + d.maybe_int(4) | ||
| 214 | + | ||
| 215 | + # refresh header | ||
| 216 | + if kf: | ||
| 217 | + refresh_entropy = d.bool(128) | ||
| 218 | + sb = [False, False] | ||
| 137 | else: | 219 | else: |
| 138 | - sizes.append(len(frame) - off) | 220 | + refresh_gf = d.bool(128) |
| 139 | - parts = [] | 221 | + if not refresh_gf: |
| 140 | - po = off | 222 | + d.lit(2) |
| 141 | - for i in range(nparts): | 223 | + refresh_arf = d.bool(128) |
| 142 | - parts.append(frame[po : po + sizes[i]]) | 224 | + if not refresh_arf: |
| 143 | - po += sizes[i] | 225 | + d.lit(2) |
| 144 | - # quant | 226 | + sb = [False, d.bool(128), d.bool(128)] |
| 145 | - d.lit(7) | 227 | + refresh_entropy = d.bool(128) |
| 146 | - for _ in range(5): | 228 | + d.bool(128) # refresh_last |
| 147 | - d.maybe_int(4) | 229 | + snapshot = (ctx["coef"][:], ctx["mv"][:], ctx["ymode"][:], ctx["uv"][:]) |
| 148 | - # reference header: refresh_entropy is read on key frames too | 230 | + |
| 149 | - d.bool(128) | 231 | + # coefficient probability updates |
| 150 | - # coefficient probability updates | 232 | + idx = 0 |
| 151 | - probs = table("DEFAULT_COEFF_PROBS") | 233 | + for _i in range(4): |
| 152 | - idx = 0 | 234 | + for _j in range(8): |
| 153 | - for _i in range(4): | 235 | + for _k in range(3): |
| 154 | - for _j in range(8): | 236 | + for _l in range(11): |
| 155 | - for _k in range(3): | 237 | + if d.bool(CUP[idx]): |
| 156 | - for _l in range(11): | 238 | + ctx["coef"][idx] = d.lit(8) |
| 157 | - if d.bool(CUP[idx]): | 239 | + idx += 1 |
| 158 | - probs[idx] = d.lit(8) | 240 | + # mb_no_skip_coeff |
| 159 | - idx += 1 | 241 | + skip_enabled = d.bool(128) |
| 160 | - # mb_no_skip_coeff | 242 | + prob_skip = d.lit(8) if skip_enabled else 128 |
| 161 | - skip_enabled = d.bool(128) | 243 | + |
| 162 | - prob_skip = d.lit(8) if skip_enabled else 128 | 244 | + probs = ctx["coef"] |
| 163 | - | 245 | + prob_intra = prob_last = prob_gf = 128 |
| 164 | - # per-MB state; row 0 / col W are the DC_PRED border | 246 | + if not kf: |
| 165 | - mbmode = [[0] * (cols + 1) for _ in range(rows + 1)] | 247 | + prob_intra = d.lit(8) |
| 166 | - bmode = [[[0] * 16 for _ in range(cols + 1)] for _ in range(rows + 1)] | 248 | + prob_last = d.lit(8) |
| 167 | - skips = [[False] * (cols + 1) for _ in range(rows + 1)] | 249 | + prob_gf = d.lit(8) |
| 168 | - above_ctx = [[0] * 9 for _ in range(cols)] | 250 | + yflag = d.bool(128) |
| 169 | - | 251 | + print(f"HDR pi={prob_intra} pl={prob_last} pg={prob_gf} yflag={1 if yflag else 0}") |
| 170 | - def get_coeffs(ptype, n0, ctx0, out): | 252 | + if yflag: |
| 171 | - base = ptype * 264 | 253 | + ctx["ymode"] = [d.lit(8) for _ in range(4)] |
| 172 | - n = n0 | 254 | + uflag = d.bool(128) |
| 173 | - band = BANDS[n0] | 255 | + print(f"HDR uflag={1 if uflag else 0}") |
| 174 | - c = ctx0 | 256 | + if uflag: |
| 175 | - p = base + band * 33 + c * 11 | 257 | + ctx["uv"] = [d.lit(8) for _ in range(3)] |
| 176 | - | 258 | + for c in range(2): |
| 177 | - def g(prob_): | 259 | + for j in range(19): |
| 178 | - bit = d2.bool(prob_) | 260 | + if d.bool(MV_UP[c * 19 + j]): |
| 179 | - print(f"G n={n} band={band} ctx={c} prob={prob_} bit={1 if bit else 0}") | 261 | + x = d.lit(7) |
| 180 | - return bit | 262 | + ctx["mv"][c * 19 + j] = (x << 1) if x else 1 |
| 181 | - | 263 | + |
| 182 | - if not g(probs[p]): | 264 | + if not kf: |
| 183 | - return 0 | 265 | + print("YUV ymode =", ctx["ymode"], "uv =", ctx["uv"]) |
| 184 | - while True: | 266 | + print("MVT c0 =", ctx["mv"][0:19]) |
| 185 | - n += 1 | 267 | + print("MVT c1 =", ctx["mv"][19:38]) |
| 186 | - if not g(probs[p + 1]): | 268 | + |
| 187 | - c = 0 | 269 | + # per-MB state (row/col 0 = border) |
| 188 | - band = BANDS[n] | 270 | + mbmode = [[0] * (cols + 1) for _ in range(rows + 1)] |
| 189 | - p = base + band * 33 | 271 | + bmode = [[[0] * 16 for _ in range(cols + 1)] for _ in range(rows + 1)] |
| 272 | + skips = [[False] * (cols + 1) for _ in range(rows + 1)] | ||
| 273 | + hasy2 = [[True] * (cols + 1) for _ in range(rows + 1)] | ||
| 274 | + mbref = [[0] * (cols + 1) for _ in range(rows + 1)] | ||
| 275 | + mbmv = [[(0, 0)] * (cols + 1) for _ in range(rows + 1)] | ||
| 276 | + mbsplit = [[False] * (cols + 1) for _ in range(rows + 1)] | ||
| 277 | + mbbmi = [[[(0, 0)] * 16 for _ in range(cols + 1)] for _ in range(rows + 1)] | ||
| 278 | + mbmvclamp = [[False] * (cols + 1) for _ in range(rows + 1)] | ||
| 279 | + above_ctx = [[0] * 9 for _ in range(cols)] | ||
| 280 | + | ||
| 281 | + def clamp_mv2(mv, edges): | ||
| 282 | + l, r, t, b = edges | ||
| 283 | + if mv[1] < l - 128: | ||
| 284 | + mv = (l - 128, mv[1]) | ||
| 285 | + elif mv[1] > r + 128: | ||
| 286 | + mv = (r + 128, mv[1]) | ||
| 287 | + if mv[0] < t - 128: | ||
| 288 | + mv = (t - 128, mv[1]) | ||
| 289 | + elif mv[0] > b + 128: | ||
| 290 | + mv = (b + 128, mv[1]) | ||
| 291 | + return mv | ||
| 292 | + | ||
| 293 | + def oob(mv, edges): | ||
| 294 | + l, r, t, b = edges | ||
| 295 | + return mv[1] < l or mv[1] > r or mv[0] < t or mv[0] > b | ||
| 296 | + | ||
| 297 | + def read_mv_component(p): | ||
| 298 | + if d.bool(p[0]): | ||
| 299 | + x = 0 | ||
| 300 | + for i in range(3): | ||
| 301 | + x += d.bool(p[9 + i]) << i | ||
| 302 | + for i in (8, 7, 6, 5, 4): | ||
| 303 | + x += d.bool(p[9 + i]) << i | ||
| 304 | + if (x & 0xFFF0) == 0 or d.bool(p[9 + 3]): | ||
| 305 | + x += 8 | ||
| 190 | else: | 306 | else: |
| 191 | - if not g(probs[p + 2]): | 307 | + x = d.tree(SMALL_MV_TREE, p[2:9]) |
| 192 | - c = 1 | 308 | + if x and d.bool(p[1]): |
| 193 | - band = BANDS[n] | 309 | + x = -x |
| 194 | - p = base + band * 33 + 11 | 310 | + return x |
| 195 | - v = 1 | 311 | + |
| 196 | - else: | 312 | + def read_mv(): |
| 197 | - if not g(probs[p + 3]): | 313 | + r = read_mv_component(ctx["mv"][0:19]) * 2 |
| 198 | - if not g(probs[p + 4]): | 314 | + c = read_mv_component(ctx["mv"][19:38]) * 2 |
| 199 | - v = 2 | 315 | + return (r, c) |
| 200 | - else: | 316 | + |
| 201 | - v = 3 + (1 if g(probs[p + 5]) else 0) | 317 | + def find_near(above, left, aboveleft, to_bias): |
| 318 | + mvs = [(0, 0), (0, 0), (0, 0), (0, 0)] | ||
| 319 | + cnt = [0, 0, 0, 0] | ||
| 320 | + ix = 0 | ||
| 321 | + cx = 0 | ||
| 322 | + | ||
| 323 | + def bias(mv, from_b): | ||
| 324 | + if from_b != to_bias: | ||
| 325 | + return (-mv[0], -mv[1]) | ||
| 326 | + return mv | ||
| 327 | + | ||
| 328 | + if not above["intra"]: | ||
| 329 | + if above["mv"] != (0, 0): | ||
| 330 | + ix += 1 | ||
| 331 | + mvs[ix] = bias(above["mv"], above["sb"]) | ||
| 332 | + cx += 1 | ||
| 333 | + cnt[cx] += 2 | ||
| 334 | + for mb, wgt in ((left, 2), (aboveleft, 1)): | ||
| 335 | + if not mb["intra"]: | ||
| 336 | + if mb["mv"] != (0, 0): | ||
| 337 | + this = bias(mb["mv"], mb["sb"]) | ||
| 338 | + if this != mvs[ix]: | ||
| 339 | + ix += 1 | ||
| 340 | + mvs[ix] = this | ||
| 341 | + cx += 1 | ||
| 342 | + cnt[cx] += wgt | ||
| 202 | else: | 343 | else: |
| 203 | - if not g(probs[p + 6]): | 344 | + cnt[0] += wgt |
| 204 | - if not g(probs[p + 7]): | 345 | + cnt[3] = (above["split"] * 2) + (left["split"] * 2) + aboveleft["split"] |
| 205 | - v = 5 + (1 if g(159) else 0) | 346 | + if cnt[2] > cnt[1]: |
| 347 | + cnt[1], cnt[2] = cnt[2], cnt[1] | ||
| 348 | + mvs[1], mvs[2] = mvs[2], mvs[1] | ||
| 349 | + return mvs, cnt | ||
| 350 | + | ||
| 351 | + def parse_inter_modes(r, c): | ||
| 352 | + mb_edge = ( | ||
| 353 | + -(((c - 1) * 16) << 3), | ||
| 354 | + (((cols - 1 - (c - 1)) * 16) << 3), | ||
| 355 | + -(((r - 1) * 16) << 3), | ||
| 356 | + (((rows - 1 - (r - 1)) * 16) << 3), | ||
| 357 | + ) | ||
| 358 | + to_bias = sb[mbref[r][c] - 1] | ||
| 359 | + | ||
| 360 | + def neigh(rr, cc): | ||
| 361 | + if rr == 0 or cc == 0 or mbref[rr][cc] == 0: | ||
| 362 | + return {"intra": True, "zero": False, "mv": (0, 0), "split": False, "sb": False} | ||
| 363 | + return { | ||
| 364 | + "intra": False, | ||
| 365 | + "zero": mbmv[rr][cc] == (0, 0), | ||
| 366 | + "mv": mbmv[rr][cc], | ||
| 367 | + "split": mbsplit[rr][cc], | ||
| 368 | + "sb": sb[mbref[rr][cc] - 1], | ||
| 369 | + } | ||
| 370 | + | ||
| 371 | + near_mvs, cnt = find_near( | ||
| 372 | + neigh(r - 1, c), neigh(r, c - 1), neigh(r - 1, c - 1), to_bias | ||
| 373 | + ) | ||
| 374 | + print(f"CNT r={r-1} c={c-1} cnt={cnt} mvs={near_mvs}") | ||
| 375 | + MC = MODE_CONTEXTS | ||
| 376 | + if not d.bool(MC[cnt[0]][0]): | ||
| 377 | + mbmode[r][c] = -1 # ZEROMV marker | ||
| 378 | + mbmv[r][c] = (0, 0) | ||
| 379 | + mbref_marker = 1 | ||
| 380 | + return | ||
| 381 | + if not d.bool(MC[cnt[1]][1]): | ||
| 382 | + mbmode[r][c] = -2 # NEAREST | ||
| 383 | + mv = clamp_mv2(near_mvs[1], mb_edge) | ||
| 384 | + mbmv[r][c] = mv | ||
| 385 | + mbref_marker = 1 | ||
| 386 | + return | ||
| 387 | + if not d.bool(MC[cnt[2]][2]): | ||
| 388 | + mbmode[r][c] = -3 # NEAR | ||
| 389 | + mv = clamp_mv2(near_mvs[2], mb_edge) | ||
| 390 | + mbmv[r][c] = mv | ||
| 391 | + mbref_marker = 1 | ||
| 392 | + return | ||
| 393 | + ni = 1 if cnt[1] >= cnt[0] else 0 | ||
| 394 | + near_mvs[ni] = clamp_mv2(near_mvs[ni], mb_edge) | ||
| 395 | + best = near_mvs[ni] | ||
| 396 | + if not d.bool(MC[cnt[3]][3]): | ||
| 397 | + mbmode[r][c] = -4 # NEW | ||
| 398 | + mv = read_mv() | ||
| 399 | + mv = (mv[0] + best[0], mv[1] + best[1]) | ||
| 400 | + mbmvclamp[r][c] = oob(mv, mb_edge) | ||
| 401 | + mbmv[r][c] = mv | ||
| 402 | + mbref_marker = 1 | ||
| 403 | + return | ||
| 404 | + # SPLITMV | ||
| 405 | + s = d.tree(MV_PARTITION_TREE, MV_PARTITION_PROBS) | ||
| 406 | + num_p = [2, 2, 4, 16][s] | ||
| 407 | + bmi = [(0, 0)] * 16 | ||
| 408 | + mbbmi[r][c] = bmi | ||
| 409 | + for j in range(num_p): | ||
| 410 | + k = MBSPLIT_OFFSET[s][j] | ||
| 411 | + if k % 4 == 0: | ||
| 412 | + lm = r, c - 1 | ||
| 413 | + leftmv = ( | ||
| 414 | + mbbmi[lm[0]][lm[1]][k + 3] | ||
| 415 | + if mbsplit[lm[0]][lm[1]] | ||
| 416 | + else mbmv[lm[0]][lm[1]] | ||
| 417 | + ) | ||
| 418 | + else: | ||
| 419 | + leftmv = bmi[k - 1] | ||
| 420 | + if k < 4: | ||
| 421 | + am = r - 1, c | ||
| 422 | + abovemv = ( | ||
| 423 | + mbbmi[am[0]][am[1]][k + 12] | ||
| 424 | + if mbsplit[am[0]][am[1]] | ||
| 425 | + else mbmv[am[0]][am[1]] | ||
| 426 | + ) | ||
| 427 | + else: | ||
| 428 | + abovemv = bmi[k - 4] | ||
| 429 | + lez = leftmv == (0, 0) | ||
| 430 | + aez = abovemv == (0, 0) | ||
| 431 | + lea = leftmv == abovemv | ||
| 432 | + ctxi = 4 if (lez and aez) else (3 if lea else (1 if lez else (2 if aez else 0))) | ||
| 433 | + pr = d.tree(SUB_MV_REF_TREE, SUB_MV_REF_PROBS[ctxi]) | ||
| 434 | + if pr == 0: | ||
| 435 | + blockmv = leftmv | ||
| 436 | + elif pr == 1: | ||
| 437 | + blockmv = abovemv | ||
| 438 | + elif pr == 2: | ||
| 439 | + blockmv = (0, 0) | ||
| 440 | + else: | ||
| 441 | + mv = read_mv() | ||
| 442 | + blockmv = (mv[0] + best[0], mv[1] + best[1]) | ||
| 443 | + print( | ||
| 444 | + f"SP r={r-1} c={c-1} j={j} k={k} l=({leftmv[0]},{leftmv[1]}) a=({abovemv[0]},{abovemv[1]}) ctx={ctxi} mv=({blockmv[0]},{blockmv[1]}) best=({best[0]},{best[1]})" | ||
| 445 | + ) | ||
| 446 | + mbmvclamp[r][c] = mbmvclamp[r][c] or oob(blockmv, mb_edge) | ||
| 447 | + cntf = MBSPLIT_FILL_COUNT[s] | ||
| 448 | + for o in MBSPLIT_FILL_OFFSET[s][j * cntf : (j + 1) * cntf]: | ||
| 449 | + bmi[o] = blockmv | ||
| 450 | + mbbmi[r][c] = bmi | ||
| 451 | + mbsplit[r][c] = True | ||
| 452 | + mbmode[r][c] = 4 | ||
| 453 | + hasy2[r][c] = False | ||
| 454 | + mbmv[r][c] = bmi[15] | ||
| 455 | + return | ||
| 456 | + | ||
| 457 | + # ---- mode records + tokens ---------------------------------------- | ||
| 458 | + # Key frames interleave per row (like the Rust decode_keyframe); | ||
| 459 | + # inter frames read ALL mode records first, then token rows | ||
| 460 | + # (like decode_interframe). B-line order must match Rust exactly. | ||
| 461 | + d2s = [None] * nparts | ||
| 462 | + | ||
| 463 | + def row_modes(r): | ||
| 464 | + for c in range(1, cols + 1): | ||
| 465 | + if seg_enabled and seg_update_map: | ||
| 466 | + seg_map[(r - 1) * cols + (c - 1)] = d.tree( | ||
| 467 | + [2, 4, 0, -1, -2, -3], seg_tree_probs | ||
| 468 | + ) | ||
| 469 | + if skip_enabled: | ||
| 470 | + skips[r][c] = bool(d.bool(prob_skip)) | ||
| 471 | + if kf: | ||
| 472 | + ym = d.tree(KF_YMODE_TREE, [145, 156, 163, 128]) | ||
| 473 | + mbmode[r][c] = ym | ||
| 474 | + hasy2[r][c] = ym != 4 | ||
| 475 | + if ym == 4: | ||
| 476 | + for j in range(16): | ||
| 477 | + if j < 4: | ||
| 478 | + am = mbmode[r - 1][c] | ||
| 479 | + a = bmode[r - 1][c][j + 12] if am == 4 else YMODE_MAP[am] | ||
| 206 | else: | 480 | else: |
| 207 | - v = 7 + 2 * (1 if g(165) else 0) | 481 | + a = bmode[r][c][j - 4] |
| 208 | - v += 1 if g(145) else 0 | 482 | + if j % 4 == 0: |
| 209 | - else: | 483 | + lm = mbmode[r][c - 1] |
| 210 | - bit1 = 1 if g(probs[p + 8]) else 0 | 484 | + l = bmode[r][c - 1][j + 3] if lm == 4 else YMODE_MAP[lm] |
| 211 | - bit0 = 1 if g(probs[p + 9 + bit1]) else 0 | 485 | + else: |
| 212 | - cat = 2 * bit1 + bit0 | 486 | + l = bmode[r][c][j - 1] |
| 213 | - v = 0 | 487 | + bmode[r][c][j] = d.tree( |
| 214 | - for prob in CAT_EXTRA[cat]: | 488 | + BMODE_TREE, KF_BMODE[(a * 10 + l) * 9 : (a * 10 + l) * 9 + 9] |
| 215 | - v += v + (1 if g(prob) else 0) | 489 | + ) |
| 216 | - v += 3 + (8 << cat) | 490 | + d.tree(UV_MODE_TREE, [142, 114, 183]) |
| 217 | - c = 2 | 491 | + else: |
| 218 | - band = BANDS[n] | 492 | + # ---- inter mode record (§16) ---- |
| 219 | - p = base + band * 33 + 22 | 493 | + if not d.bool(prob_intra): |
| 220 | - out[ZIGZAG[n - 1]] = -v if g(128) else v | 494 | + mbref[r][c] = 0 |
| 221 | - if n < 16 and not g(probs[p]): | 495 | + ym = d.tree(YMODE_TREE, ctx["ymode"]) |
| 222 | - return n | 496 | + mbmode[r][c] = ym |
| 223 | - if n == 16: | 497 | + hasy2[r][c] = ym != 4 |
| 224 | - return 16 | 498 | + if ym == 4: |
| 225 | - | 499 | + for j in range(16): |
| 226 | - # One PERSISTENT bool decoder per token partition: rows that share a | 500 | + if j < 4: |
| 227 | - # partition continue its stream (dixie/libvpx behaviour). | 501 | + am = mbmode[r - 1][c] |
| 228 | - d2s = [None] * nparts | 502 | + a = bmode[r - 1][c][j + 12] if am == 4 else YMODE_MAP[am] |
| 229 | - for r in range(1, rows + 1): | 503 | + else: |
| 230 | - pi = (r - 1) % nparts | 504 | + a = bmode[r][c][j - 4] |
| 231 | - if d2s[pi] is None: | 505 | + if j % 4 == 0: |
| 232 | - d2s[pi] = BD(parts[pi]) | 506 | + lm = mbmode[r][c - 1] |
| 233 | - d2 = d2s[pi] | 507 | + l = bmode[r][c - 1][j + 3] if lm == 4 else YMODE_MAP[lm] |
| 234 | - left_ctx = [0] * 9 | 508 | + else: |
| 235 | - # phase 1: every mode record of the row (partition 0) | 509 | + l = bmode[r][c][j - 1] |
| 236 | - for c in range(1, cols + 1): | 510 | + bmode[r][c][j] = d.tree( |
| 237 | - print(f"M {r-1} {c-1}") | 511 | + BMODE_TREE, |
| 238 | - if skip_enabled: | 512 | + KF_BMODE[(a * 10 + l) * 9 : (a * 10 + l) * 9 + 9], |
| 239 | - skips[r][c] = bool(d.bool(prob_skip)) | 513 | + ) |
| 240 | - ym = d.tree(KF_YMODE_TREE, [145, 156, 163, 128]) | 514 | + d.tree(UV_MODE_TREE, ctx["uv"]) |
| 241 | - mbmode[r][c] = ym | ||
| 242 | - if ym == 4: | ||
| 243 | - for j in range(16): | ||
| 244 | - if j < 4: | ||
| 245 | - am = mbmode[r - 1][c] | ||
| 246 | - a = bmode[r - 1][c][j + 12] if am == 4 else YMODE_MAP[am] | ||
| 247 | else: | 515 | else: |
| 248 | - a = bmode[r][c][j - 4] | 516 | + if not d.bool(prob_last): |
| 249 | - if j % 4 == 0: | 517 | + mbref[r][c] = 1 |
| 250 | - lm = mbmode[r][c - 1] | 518 | + elif not d.bool(prob_gf): |
| 251 | - l = bmode[r][c - 1][j + 3] if lm == 4 else YMODE_MAP[lm] | 519 | + mbref[r][c] = 2 |
| 520 | + else: | ||
| 521 | + mbref[r][c] = 3 | ||
| 522 | + try: | ||
| 523 | + parse_inter_modes(r, c) | ||
| 524 | + except Exception as e: | ||
| 525 | + import traceback; traceback.print_exc() | ||
| 526 | + print(f"EXC at r={r} c={c}: {e}") | ||
| 527 | + raise SystemExit(1) | ||
| 528 | + mvref_print = 4 if mbsplit[r][c] else {-1: 3, -2: 1, -3: 2, -4: 3}.get( | ||
| 529 | + mbmode[r][c], 0 | ||
| 530 | + ) | ||
| 531 | + print( | ||
| 532 | + f"IMB r={r-1} c={c-1} ref={mbref[r][c]} mvref={mvref_print} " | ||
| 533 | + f"mv=({mbmv[r][c][0]},{mbmv[r][c][1]}) clamp={mbmvclamp[r][c]}" | ||
| 534 | + ) | ||
| 535 | + if 0 < mbref[r][c] and mbmode[r][c] >= 0: | ||
| 536 | + hasy2[r][c] = True | ||
| 537 | + | ||
| 538 | + def row_tokens(r): | ||
| 539 | + pi = (r - 1) % nparts | ||
| 540 | + if d2s[pi] is None: | ||
| 541 | + d2s[pi] = BD(parts[pi]) | ||
| 542 | + d2 = d2s[pi] | ||
| 543 | + left_ctx = [0] * 9 | ||
| 544 | + | ||
| 545 | + def get_coeffs(ptype, n0, ctx0, out): | ||
| 546 | + base = ptype * 264 | ||
| 547 | + n = n0 | ||
| 548 | + band = BANDS[n0] | ||
| 549 | + c = ctx0 | ||
| 550 | + p = base + band * 33 + c * 11 | ||
| 551 | + | ||
| 552 | + def g(prob_): | ||
| 553 | + bit = d2.bool(prob_) | ||
| 554 | + print(f"G n={n} band={band} ctx={c} prob={prob_} bit={1 if bit else 0}") | ||
| 555 | + return bit | ||
| 556 | + | ||
| 557 | + if not g(probs[p]): | ||
| 558 | + return 0 | ||
| 559 | + while True: | ||
| 560 | + n += 1 | ||
| 561 | + if not g(probs[p + 1]): | ||
| 562 | + c = 0 | ||
| 563 | + band = BANDS[n] | ||
| 564 | + p = base + band * 33 | ||
| 252 | else: | 565 | else: |
| 253 | - l = bmode[r][c][j - 1] | 566 | + if not g(probs[p + 2]): |
| 254 | - bmode[r][c][j] = d.tree(BMODE_TREE, KF_BMODE[(a * 10 + l) * 9 : (a * 10 + l) * 9 + 9]) | 567 | + c = 1 |
| 255 | - d.tree(UV_MODE_TREE, [142, 114, 183]) | 568 | + band = BANDS[n] |
| 256 | - # phase 2: tokens of the row (partition r-1 % n) | 569 | + p = base + band * 33 + 11 |
| 257 | - for c in range(1, cols + 1): | 570 | + v = 1 |
| 258 | - ac = above_ctx[c - 1] | 571 | + else: |
| 259 | - hy2 = mbmode[r][c] != 4 | 572 | + if not g(probs[p + 3]): |
| 260 | - if skips[r][c]: | 573 | + if not g(probs[p + 4]): |
| 261 | - for i in range(8): | 574 | + v = 2 |
| 262 | - ac[i] = 0 | 575 | + else: |
| 263 | - left_ctx[i] = 0 | 576 | + v = 3 + (1 if g(probs[p + 5]) else 0) |
| 264 | - if hy2: | 577 | + else: |
| 265 | - ac[8] = 0 | 578 | + if not g(probs[p + 6]): |
| 266 | - left_ctx[8] = 0 | 579 | + if not g(probs[p + 7]): |
| 267 | - continue | 580 | + v = 5 + (1 if g(159) else 0) |
| 268 | - | 581 | + else: |
| 269 | - def one(blkidx, ptype, n0): | 582 | + v = 7 + 2 * (1 if g(165) else 0) |
| 270 | - o = [0] * 16 | 583 | + v += 1 if g(145) else 0 |
| 271 | - ctxv = min(2, left_ctx[LEFT[blkidx]] + ac[ABOVE[blkidx]]) | 584 | + else: |
| 272 | - print(f"T {r-1} {c-1} {blkidx} {ptype} {ctxv}") | 585 | + bit1 = 1 if g(probs[p + 8]) else 0 |
| 273 | - eob = get_coeffs(ptype, n0, ctxv, o) | 586 | + bit0 = 1 if g(probs[p + 9 + bit1]) else 0 |
| 274 | - t = 1 if eob > 0 else 0 | 587 | + cat = 2 * bit1 + bit0 |
| 275 | - left_ctx[LEFT[blkidx]] = t | 588 | + v = 0 |
| 276 | - ac[ABOVE[blkidx]] = t | 589 | + for prob in CAT_EXTRA[cat]: |
| 277 | - | 590 | + v += v + (1 if g(prob) else 0) |
| 278 | - if hy2: | 591 | + v += 3 + (8 << cat) |
| 279 | - one(24, 1, 0) | 592 | + c = 2 |
| 280 | - for b in range(16): | 593 | + band = BANDS[n] |
| 281 | - one(b, 0, 1) | 594 | + p = base + band * 33 + 22 |
| 282 | - else: | 595 | + out[ZIGZAG[n - 1]] = -v if g(128) else v |
| 283 | - for b in range(16): | 596 | + if n < 16 and not g(probs[p]): |
| 284 | - one(b, 3, 0) | 597 | + return n |
| 285 | - for b in range(4): | 598 | + if n == 16: |
| 286 | - one(16 + b, 2, 0) | 599 | + return 16 |
| 287 | - for b in range(4): | 600 | + |
| 288 | - one(20 + b, 2, 0) | 601 | + for c in range(1, cols + 1): |
| 602 | + left_ctx = [0] * 9 if c == 1 else left_ctx | ||
| 603 | + | ||
| 604 | + def one(blkidx, ptype, n0): | ||
| 605 | + o = [0] * 16 | ||
| 606 | + ctxv = min(2, left_ctx[LEFT[blkidx]] + above_ctx[c - 1][ABOVE[blkidx]]) | ||
| 607 | + print(f"T {r-1} {c-1} {blkidx} {ptype} {ctxv}") | ||
| 608 | + eob = get_coeffs(ptype, n0, ctxv, o) | ||
| 609 | + t = 1 if eob > 0 else 0 | ||
| 610 | + left_ctx[LEFT[blkidx]] = t | ||
| 611 | + above_ctx[c - 1][ABOVE[blkidx]] = t | ||
| 612 | + | ||
| 613 | + if skips[r][c]: | ||
| 614 | + for i in range(8): | ||
| 615 | + above_ctx[c - 1][i] = 0 | ||
| 616 | + left_ctx[i] = 0 | ||
| 617 | + if hasy2[r][c]: | ||
| 618 | + above_ctx[c - 1][8] = 0 | ||
| 619 | + left_ctx[8] = 0 | ||
| 620 | + continue | ||
| 621 | + if hasy2[r][c]: | ||
| 622 | + one(24, 1, 0) | ||
| 623 | + for b in range(16): | ||
| 624 | + one(b, 0, 1) | ||
| 625 | + else: | ||
| 626 | + for b in range(16): | ||
| 627 | + one(b, 3, 0) | ||
| 628 | + for b in range(4): | ||
| 629 | + one(16 + b, 2, 0) | ||
| 630 | + for b in range(4): | ||
| 631 | + one(20 + b, 2, 0) | ||
| 632 | + | ||
| 633 | + if kf: | ||
| 634 | + for r in range(1, rows + 1): | ||
| 635 | + row_modes(r) | ||
| 636 | + row_tokens(r) | ||
| 637 | + else: | ||
| 638 | + for r in range(1, rows + 1): | ||
| 639 | + row_modes(r) | ||
| 640 | + for r in range(1, rows + 1): | ||
| 641 | + row_tokens(r) | ||
| 642 | + | ||
| 643 | + if not refresh_entropy: | ||
| 644 | + ctx["coef"], ctx["mv"], ctx["ymode"], ctx["uv"] = snapshot | ||
| 289 | 645 | ||
| 290 | 646 | ||
| 291 | main() | 647 | main() |