code wiki / _hdl_build / nx_vmvres_gate.nx
nx_vmvres_gate.nx source
↩ module page · 82 lines · 3667 B
1// nx_vmvres_gate.nx -- proves MV prediction + efficient residual coding TOGETHER slash the MV bitrate, and round-trips
2// exactly. Same realistic pan field as nx_vmvpred_gate; codes the predicted residuals with mvr_put (1-bit zero-flag),
3// reads them back (exact), and compares the total against absolute varint coding -- the 65% magnitude win now lands in
4// the total because the common 0-residual costs 1 bit not 5.
5import "nx_syscalls.nx"
6import "nx_gate_emit_lib.nx"
7import "nx_vmvpred.nx"
8import "nx_vmvres.nx"
9
10func vbits(v: i64) -> i64 { return 5 + ve_blen(ve_zze(v)) }
11
12func main() -> i64 {
13 g_puts("nx_vmvres gate (MVP + efficient residual coding: total MV bitrate, round-trip)\n" as *u8)
14 var pass: i64 = 0; var total: i64 = 0
15 let BW: i64 = 6; let BH: i64 = 4; let NB: i64 = BW*BH
16
17 let mvx: *i64 = sys_mmap(NB*8) as *i64
18 let mvy: *i64 = sys_mmap(NB*8) as *i64
19 var by: i64 = 0
20 while by < BH { var bx: i64 = 0
21 while bx < BW {
22 let i: i64 = by*BW + bx
23 var dx: i64 = 3; var dy: i64 = 1
24 if bx >= 2 { if bx <= 3 { if by >= 1 { if by <= 2 { dx = 3; dy = 5 } } } }
25 if bx == 5 { if by == 0 { dx = 8; dy = 0 - 2 } }
26 mvx[i] = dx; mvy[i] = dy
27 bx = bx + 1
28 }
29 by = by + 1
30 }
31
32 let L: *i64 = sys_mmap(2*8) as *i64
33 let T: *i64 = sys_mmap(2*8) as *i64
34 let TR: *i64 = sys_mmap(2*8) as *i64
35 let pred: *i64 = sys_mmap(2*8) as *i64
36 let rx: *i64 = sys_mmap(NB*8) as *i64 // residual dx per block
37 let ry: *i64 = sys_mmap(NB*8) as *i64 // residual dy per block
38 let buf: *u8 = sys_mmap(1024) as *u8
39
40 var abs_bits: i64 = 0
41 var bp: i64 = 0
42 by = 0
43 while by < BH { var bx: i64 = 0
44 while bx < BW {
45 let i: i64 = by*BW + bx
46 L[0]=0; L[1]=0; T[0]=0; T[1]=0; TR[0]=0; TR[1]=0
47 if bx > 0 { L[0]=mvx[i-1]; L[1]=mvy[i-1] }
48 if by > 0 { T[0]=mvx[i-BW]; T[1]=mvy[i-BW] }
49 if by > 0 { if bx < BW-1 { TR[0]=mvx[i-BW+1]; TR[1]=mvy[i-BW+1] } }
50 mvp_predict(L, T, TR, pred)
51 rx[i] = mvx[i] - pred[0]
52 ry[i] = mvy[i] - pred[1]
53 abs_bits = abs_bits + vbits(mvx[i]) + vbits(mvy[i]) // baseline: absolute varint
54 bp = mvr_put(buf, bp, rx[i]) // MVP + efficient residual coding
55 bp = mvr_put(buf, bp, ry[i])
56 bx = bx + 1
57 }
58 by = by + 1
59 }
60 let mvp_bits: i64 = bp
61
62 // round-trip: read every component back and check it matches the residual we wrote
63 var rp: i64 = 0
64 var ok: i64 = 1
65 var k: i64 = 0
66 while k < NB {
67 let gx: i64 = mvr_get(buf, rp); rp = rp + mvr_len(buf, rp)
68 let gy: i64 = mvr_get(buf, rp); rp = rp + mvr_len(buf, rp)
69 if gx != rx[k] { ok = 0 }
70 if gy != ry[k] { ok = 0 }
71 k = k + 1
72 }
73
74 g_puts(" [measure] MV bits over " as *u8); g_pn(NB); g_puts(" blocks: absolute-varint=" as *u8); g_pn(abs_bits); g_puts(" MVP+zero-flag=" as *u8); g_pn(mvp_bits); g_puts(" (saving " as *u8); g_pn((abs_bits - mvp_bits) * 100 / abs_bits); g_puts("%)\n" as *u8)
75
76 pass = pass + g_check("MVP+efficient residual round-trips exactly" as *u8, ok); total=total+1
77 pass = pass + g_check("total MV bitrate now cut >= 50% (the magnitude win lands in the total)" as *u8, mvp_bits * 2 <= abs_bits); total=total+1
78
79 g_puts("---- vmvres gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
80 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
81 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
82}