code wiki / _hdl_build / nx_vcodec_gop_vm_gate.nx

nx_vcodec_gop_vm_gate.nx source

↩ module page · 126 lines · 6333 B

1// nx_vcodec_gop_vm_gate.nx -- SOVEREIGN VM verification of a full GOP (group-of-pictures): the 2// shipped nx_vcodec.wasm coding a REAL multi-frame video sequence (1 keyframe + 7 inter frames 3// with a moving object), prev-recon chained between frames exactly as the browser does. The 4// existing inter gate proves keyframe + ONE P-frame; this proves the chain stays byte-exact over 5// a WHOLE sequence -- i.e. NO temporal drift accumulates, and the per-op-alloc leak fix holds over 6// the long op-sequence a real video drives. Proven via the ecosystem's OWN wasm VM vs the native 7// codec authority, every frame: 8// - every frame's recon VM == native (byte-exact -- drift-free over the GOP) <- the new coverage 9// - every frame's bitcount VM == native (compiler faithful on the cost path) 10// - the GOP costs FAR less than coding every frame as a keyframe (temporal compression real) 11// - NEG-CONTROL: the byte-exact comparator detects a real difference (moved object) -> has teeth 12// No deploy, no live-client touch -- pure headless de-risk. expect_exit: 0 license_tier: ORIGINAL 13import "nx_syscalls.nx" 14import "nx_gate_emit_lib.nx" 15import "nx_wasm_vm.nx" 16import "nx_vcodec.nx" 17 18const VCODEC_WASM: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/web_assets/_video_build/nx_vcodec.wasm" 19const O_CUR: i64 = 0 20const O_PREV: i64 = 16384 21const O_DEC: i64 = 49152 22 23func gen_frame(cur: *u8, W: i64, H: i64, ox: i64, oy: i64) -> i64 { 24 let ox2: i64 = ox + 4; let oy2: i64 = oy + 4 25 var y: i64 = 0 26 while y < H { 27 var x: i64 = 0 28 while x < W { 29 var v: i64 = 40 + ((x*11 + y*7) % 120) 30 if x >= ox { if x < ox2 { if y >= oy { if y < oy2 { v = 210 } } } } 31 cur[y*W+x] = v as u8 32 x = x + 1 33 } 34 y = y + 1 35 } 36 return 0 37} 38 39func main() -> i64 { 40 g_puts("nx_vcodec GOP wasm-VM gate (1 keyframe + 7 inter, moving object, prev-recon chained -- drift-free?)\n" as *u8) 41 var pass: i64 = 0; var total: i64 = 0 42 43 let box: *i64 = sys_mmap(16) as *i64 44 let wasm: *u8 = sys_read_file(VCODEC_WASM, box) 45 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 } 46 let mod: *WasmMod = wm_new(wasm, box[0]) 47 if wm_parse(mod) != 0 { g_puts(" FAIL parse\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 } 48 mod.mem = sys_mmap(mod.mem_bytes) as *u8 49 50 let W: i64 = 16; let H: i64 = 16; let N: i64 = W*H; let QP: i64 = 20; let SAD: i64 = 64 51 let NF: i64 = 8 52 let cur: *u8 = sys_mmap(N) as *u8 53 let prev: *u8 = sys_mmap(N) as *u8 54 let eR: *u8 = sys_mmap(N) as *u8 55 let dR: *u8 = sys_mmap(N) as *u8 56 let dR0: *u8 = sys_mmap(N) as *u8 57 let stream: *u8 = sys_mmap(8192) as *u8 58 let blk: *i64 = sys_mmap(16*8) as *i64 59 let mv: *i64 = sys_mmap(2*8) as *i64 60 61 var tot_mm: i64 = 0 62 var bits_all_ok: i64 = 1 63 var key_bits: i64 = 0 64 var inter_sum: i64 = 0 65 var gop_total: i64 = 0 66 67 var f: i64 = 0 68 while f < NF { 69 var kf: i64 = 0 70 if f == 0 { kf = 1 } 71 let ox: i64 = 2 + f 72 gen_frame(cur, W, H, ox, 6) 73 74 // ---- VM path: load cur, encode, decode (independent of native) ---- 75 var i: i64 = 0 76 while i < N { mod.mem[O_CUR + i] = cur[i]; i = i + 1 } 77 let vbits: i64 = wm_run(mod, "vc_wasm_encode" as *u8, W, H, QP, kf, SAD, 5) 78 wm_run(mod, "vc_wasm_decode" as *u8, W, H, QP, 0, 0, 3) 79 80 // ---- native authority ---- 81 let nbits: i64 = vc_enc_frame_packed(cur, prev, eR, W, H, QP, kf, SAD, stream, blk, mv) 82 vc_dec_frame_packed(prev, dR, W, H, QP, stream, blk, mv) 83 84 // ---- VM recon == native recon, byte-exact ---- 85 var mm: i64 = 0 86 i = 0 87 while i < N { if (mod.mem[O_DEC + i] as i64) != (dR[i] as i64) { mm = mm + 1 } i = i + 1 } 88 tot_mm = tot_mm + mm 89 if vbits != nbits { bits_all_ok = 0 } 90 91 g_puts(" frame " as *u8); g_pn(f) 92 if kf == 1 { g_puts(" KEY " as *u8) } else { g_puts(" INT " as *u8) } 93 g_puts("bits VM=" as *u8); g_pn(vbits); g_puts(" native=" as *u8); g_pn(nbits); g_puts(" mismatch-px=" as *u8); g_pn(mm); g_puts("\n" as *u8) 94 95 gop_total = gop_total + vbits 96 if kf == 1 { key_bits = vbits } else { inter_sum = inter_sum + vbits } 97 98 // save frame-0 recon for the negative control 99 if f == 0 { i = 0; while i < N { dR0[i] = dR[i]; i = i + 1 } } 100 101 // ---- chain: this frame's recon becomes next frame's PREV (VM and native both) ---- 102 i = 0 103 while i < N { mod.mem[O_PREV + i] = mod.mem[O_DEC + i]; prev[i] = dR[i]; i = i + 1 } 104 f = f + 1 105 } 106 107 pass = pass + g_check("every frame recon VM==native byte-exact over the whole GOP (no temporal drift)" as *u8, tot_mm == 0); total = total + 1 108 pass = pass + g_check("every frame bitcount VM==native (compiler faithful across the sequence)" as *u8, bits_all_ok); total = total + 1 109 110 // temporal compression: the 8-frame GOP must cost FAR less than 8 keyframes would 111 var comp_ok: i64 = 0 112 if key_bits >= 1 { if gop_total * 2 < key_bits * NF { comp_ok = 1 } } 113 g_puts(" GOP total bits=" as *u8); g_pn(gop_total); g_puts(" vs all-keyframe ~=" as *u8); g_pn(key_bits * NF); g_puts(" (key=" as *u8); g_pn(key_bits); g_puts(" inter-sum=" as *u8); g_pn(inter_sum); g_puts(")\n" as *u8) 114 pass = pass + g_check("GOP costs < half of all-intra (temporal compression real over the sequence)" as *u8, comp_ok); total = total + 1 115 116 // NEG-CONTROL: the object moved, so frame-0 recon must DIFFER from the last frame's recon. 117 // If the comparator reported 0 here it would be trivially-true (no teeth). 118 var diff: i64 = 0 119 var j: i64 = 0 120 while j < N { if (dR0[j] as i64) != (dR[j] as i64) { diff = diff + 1 } j = j + 1 } 121 pass = pass + g_check("neg-control: byte-exact comparator DETECTS the moved object (frame0 != frameN)" as *u8, diff > 0); total = total + 1 122 123 g_puts("---- vcodec GOP wasm-VM gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 124 if pass == total { g_puts("verdict=GREEN -- a full GOP is wasm-faithful + drift-free + temporally compressed (real video, VM-proven)\n" as *u8); sys_exit(0); return 0 } 125 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 126}