code wiki / _hdl_build / nx_vcodec_inter_vm_gate.nx
nx_vcodec_inter_vm_gate.nx source
↩ module page · 94 lines · 5690 B
1// nx_vcodec_inter_vm_gate.nx -- SOVEREIGN VM verification of the INTER-FRAME (P-frame)
2// path of the shipped nx_vcodec.wasm. The existing nx_wasm_vm_exec_gate proves the
3// KEYFRAME path byte-exact; this proves the TEMPORAL-COMPRESSION path (keyframe=0,
4// coded against O_PREV) -- the actual 9->30fps lever the H9 live-wiring depends on.
5//
6// Flow (mirrors the browser's real inter loop): encode frame A as a KEYFRAME, decode it,
7// copy the decoded recon -> O_PREV (what the browser does between frames), then encode a
8// near-static frame B as an INTER frame (keyframe=0) against that PREV, decode it. Prove,
9// via the ecosystem's OWN wasm VM vs the native codec authority:
10// - keyframe recon VM == native (byte-exact)
11// - inter-frame recon VM == native (byte-exact) <- the new coverage
12// - inter frame costs FAR fewer bits than the keyframe (temporal compression is real)
13// No deploy, no live-client touch -- pure headless de-risk. expect_exit: 0 license_tier: ORIGINAL
14import "nx_syscalls.nx"
15import "nx_gate_emit_lib.nx"
16import "nx_wasm_vm.nx"
17import "nx_vcodec.nx"
18
19const VCODEC_WASM: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/web_assets/_video_build/nx_vcodec.wasm"
20const O_CUR: i64 = 0
21const O_PREV: i64 = 16384
22const O_DEC: i64 = 49152
23
24func main() -> i64 {
25 g_puts("nx_vcodec INTER-frame wasm-VM gate (P-frame path VM==native, temporal compression)\n" as *u8)
26 var pass: i64 = 0; var total: i64 = 0
27
28 let box: *i64 = sys_mmap(16) as *i64
29 let wasm: *u8 = sys_read_file(VCODEC_WASM, box)
30 if (wasm as i64) == 0 { g_puts(" FAIL cannot read nx_vcodec.wasm\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 }
31 let mod: *WasmMod = wm_new(wasm, box[0])
32 if wm_parse(mod) != 0 { g_puts(" FAIL parse\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 }
33 mod.mem = sys_mmap(mod.mem_bytes) as *u8
34
35 let W: i64 = 16; let H: i64 = 16; let N: i64 = W*H; let QP: i64 = 20; let SAD: i64 = 64
36 let curA: *u8 = sys_mmap(N) as *u8
37 let curB: *u8 = sys_mmap(N) as *u8
38 let prev: *u8 = sys_mmap(N) as *u8
39 let eR: *u8 = sys_mmap(N) as *u8
40 let dRA: *u8 = sys_mmap(N) as *u8
41 let dRB: *u8 = sys_mmap(N) as *u8
42 let stream: *u8 = sys_mmap(8192) as *u8
43 let blk: *i64 = sys_mmap(16*8) as *i64
44 let mv: *i64 = sys_mmap(2*8) as *i64
45
46 // frame A = a textured scene
47 var y: i64 = 0
48 while y < H { var x: i64 = 0
49 while x < W { var v: i64 = 40 + ((x*11 + y*7) % 120); if y>=4 { if y<12 { if x>=4 { if x<12 { v = 210 } } } } curA[y*W+x] = v as u8; x = x + 1 } y = y + 1 }
50 // frame B = A with ONE 4x4 block changed (near-static -> most blocks SKIP -> tiny inter cost)
51 var i: i64 = 0
52 while i < N { curB[i] = curA[i]; i = i + 1 }
53 y = 0
54 while y < 4 { var x: i64 = 0; while x < 4 { curB[y*W+x] = (60 + x*5) as u8; x = x + 1 } y = y + 1 }
55
56 // ---- KEYFRAME (frame A): VM vs native ----
57 i = 0; while i < N { mod.mem[O_CUR + i] = curA[i]; i = i + 1 }
58 let vbitsA: i64 = wm_run(mod, "vc_wasm_encode" as *u8, W, H, QP, 1, SAD, 5)
59 wm_run(mod, "vc_wasm_decode" as *u8, W, H, QP, 0, 0, 3)
60 let nbitsA: i64 = vc_enc_frame_packed(curA, prev, eR, W, H, QP, 1, SAD, stream, blk, mv)
61 vc_dec_frame_packed(prev, dRA, W, H, QP, stream, blk, mv)
62 var mmA: i64 = 0
63 i = 0; while i < N { if (mod.mem[O_DEC + i] as i64) != (dRA[i] as i64) { mmA = mmA + 1 } i = i + 1 }
64 g_puts(" keyframe bits VM=" as *u8); g_pn(vbitsA); g_puts(" native=" as *u8); g_pn(nbitsA); g_puts(" recon-mismatch-px=" as *u8); g_pn(mmA); g_puts("\n" as *u8)
65 pass = pass + g_check("keyframe recon VM==native byte-exact" as *u8, mmA == 0); total = total + 1
66
67 // ---- copy decoded recon -> PREV (what the browser does between frames) ----
68 i = 0; while i < N { mod.mem[O_PREV + i] = mod.mem[O_DEC + i]; prev[i] = dRA[i]; i = i + 1 }
69
70 // ---- INTER frame (frame B, keyframe=0, coded against PREV): VM vs native ----
71 i = 0; while i < N { mod.mem[O_CUR + i] = curB[i]; i = i + 1 }
72 let vbitsB: i64 = wm_run(mod, "vc_wasm_encode" as *u8, W, H, QP, 0, SAD, 5)
73 wm_run(mod, "vc_wasm_decode" as *u8, W, H, QP, 0, 0, 3)
74 let nbitsB: i64 = vc_enc_frame_packed(curB, prev, eR, W, H, QP, 0, SAD, stream, blk, mv)
75 vc_dec_frame_packed(prev, dRB, W, H, QP, stream, blk, mv)
76 var mmB: i64 = 0
77 i = 0; while i < N { if (mod.mem[O_DEC + i] as i64) != (dRB[i] as i64) { mmB = mmB + 1 } i = i + 1 }
78 g_puts(" inter bits VM=" as *u8); g_pn(vbitsB); g_puts(" native=" as *u8); g_pn(nbitsB); g_puts(" recon-mismatch-px=" as *u8); g_pn(mmB); g_puts("\n" as *u8)
79 pass = pass + g_check("INTER-frame recon VM==native byte-exact (P-frame path is wasm-faithful)" as *u8, mmB == 0); total = total + 1
80
81 // ---- temporal compression: the near-static inter frame must cost FAR fewer bits ----
82 var comp_ok: i64 = 0
83 if vbitsA >= 1 { if vbitsB * 2 < vbitsA { comp_ok = 1 } }
84 pass = pass + g_check("inter frame costs < half the keyframe bits (temporal compression real)" as *u8, comp_ok); total = total + 1
85
86 // ---- VM and native agree on the bitcount too (compiler faithful on the cost path) ----
87 var bits_ok: i64 = 0
88 if vbitsA == nbitsA { if vbitsB == nbitsB { bits_ok = 1 } }
89 pass = pass + g_check("VM bitcounts == native (keyframe AND inter)" as *u8, bits_ok); total = total + 1
90
91 g_puts("---- vcodec inter wasm-VM gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
92 if pass == total { g_puts("verdict=GREEN -- the INTER-frame wasm codec is faithful + compresses (H9 codec half is wasm-ready)\n" as *u8); sys_exit(0); return 0 }
93 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
94}