code wiki / _hdl_build / nx_vcodec_color_gop_vm_gate.nx

nx_vcodec_color_gop_vm_gate.nx source

↩ module page · 129 lines · 7018 B

1// nx_vcodec_color_gop_vm_gate.nx -- SOVEREIGN VM proof of a full COLOR GOP: the shipped 2// nx_vcodec_color.wasm coding a REAL multi-frame COLOR video (1 keyframe + 5 P-frames, a moving 3// colored object), prev-recon chained across all 3 planes (Y full-res + Cb/Cr 4:2:0) exactly as 4// the browser would. The color VM gate proves ONE keyframe; this proves the chain stays byte-exact 5// over a whole COLOR sequence (no temporal drift) via the ecosystem's OWN wasm VM vs the new native 6// color-inter authority (vc_color_encode_p / vc_color_decode_p): 7// - every frame's decoded RGB VM == native byte-exact (drift-free over the GOP) <- new coverage 8// - every frame's bitcount VM == native (wat->wasm lowering faithful across the sequence) 9// - the COLOR GOP costs far less than coding every frame as a keyframe (temporal compression real) 10// - NEG-CONTROL: the byte-exact comparator detects the moved object (frame0 RGB != frameN RGB) 11// No deploy, no browser, no JS -- pure headless de-risk in our VM. expect_exit: 0 license_tier: ORIGINAL 12import "nx_syscalls.nx" 13import "nx_gate_emit_lib.nx" 14import "nx_wasm_vm.nx" 15import "nx_vcodec_color_codec.nx" 16 17const COLOR_WASM: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/web_assets/_video_build/nx_vcodec_color.wasm" 18// wrapper linear-memory offsets (mirror nx_vcodec_color_wasm.nx; the VM==native check self-validates 19// them -- a wrong offset breaks the prev-recon chain and the byte-exact compare goes RED). 20const O_RGB: i64 = 0 21const O_PREVY: i64 = 106496 22const O_PREVCB: i64 = 122880 23const O_PREVCR: i64 = 126976 24const O_DECY: i64 = 188416 25const O_DECCB: i64 = 204800 26const O_DECCR: i64 = 208896 27const O_RGBOUT: i64 = 245760 28 29func gen_color_frame(rgb: *u8, W: i64, H: i64, ox: i64, oy: i64) -> i64 { 30 let ox2: i64 = ox + 6; let oy2: i64 = oy + 6 31 var y: i64 = 0 32 while y < H { 33 var x: i64 = 0 34 while x < W { 35 let p: i64 = (y*W+x)*3 36 var r: i64 = 40 + x*5; var g: i64 = 50 + y*5; var b: i64 = 128 37 if x >= ox { if x < ox2 { if y >= oy { if y < oy2 { r = 220; g = 40; b = 60 } } } } 38 rgb[p] = r as u8; rgb[p+1] = g as u8; rgb[p+2] = b as u8 39 x = x + 1 40 } 41 y = y + 1 42 } 43 return 0 44} 45 46func main() -> i64 { 47 g_puts("nx_vcodec COLOR GOP wasm-VM gate (1 keyframe + 5 inter, moving colored object, 3-plane chained)\n" as *u8) 48 var pass: i64 = 0; var total: i64 = 0 49 50 let box: *i64 = sys_mmap(16) as *i64 51 let wasm: *u8 = sys_read_file(COLOR_WASM, box) 52 if (wasm as i64) == 0 { g_puts(" FAIL cannot read nx_vcodec_color.wasm\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 } 53 let mod: *WasmMod = wm_new(wasm, box[0]) 54 if wm_parse(mod) != 0 { g_puts(" FAIL parse\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 } 55 mod.mem = sys_mmap(mod.mem_bytes) as *u8 56 57 let W: i64 = 32; let H: i64 = 32; let N: i64 = W*H; let CN: i64 = (W/2)*(H/2); let QP: i64 = 20; let SAD: i64 = 64 58 let NF: i64 = 6 59 let rgb: *u8 = sys_mmap(N*3) 60 let natRGB: *u8 = sys_mmap(N*3) 61 let rgb0: *u8 = sys_mmap(N*3) 62 // native prev recon planes (Y full-res, Cb/Cr subsampled), zeroed = ignored on the keyframe 63 let prevY: *u8 = sys_mmap(N); let prevCbs: *u8 = sys_mmap(CN); let prevCrs: *u8 = sys_mmap(CN) 64 let decY: *u8 = sys_mmap(N); let decCbs: *u8 = sys_mmap(CN); let decCrs: *u8 = sys_mmap(CN) 65 let strY: *u8 = sys_mmap(8192); let strCb: *u8 = sys_mmap(8192); let strCr: *u8 = sys_mmap(8192) 66 let bits: *i64 = sys_mmap(3*8) as *i64 67 68 var tot_mm: i64 = 0 69 var bits_all_ok: i64 = 1 70 var key_bits: i64 = 0 71 var gop_total: i64 = 0 72 73 var f: i64 = 0 74 while f < NF { 75 var kf: i64 = 0 76 if f == 0 { kf = 1 } 77 let ox: i64 = 4 + f*3 78 gen_color_frame(rgb, W, H, ox, 12) 79 80 // ---- VM path: write RGB, encode, decode ---- 81 var i: i64 = 0 82 while i < N*3 { mod.mem[O_RGB + i] = rgb[i]; i = i + 1 } 83 let vbits: i64 = wm_run(mod, "vc_color_wasm_encode" as *u8, W, H, QP, kf, SAD, 5) 84 wm_run(mod, "vc_color_wasm_decode" as *u8, W, H, QP, 0, 0, 3) 85 86 // ---- native authority (the new color-inter rung) ---- 87 let nbits: i64 = vc_color_encode_p(rgb, W, H, QP, kf, SAD, prevY, prevCbs, prevCrs, strY, strCb, strCr, bits) 88 vc_color_decode_p(W, H, QP, prevY, prevCbs, prevCrs, strY, strCb, strCr, natRGB, decY, decCbs, decCrs) 89 90 // ---- decoded RGB: VM == native, byte-exact ---- 91 var mm: i64 = 0 92 i = 0 93 while i < N*3 { if (mod.mem[O_RGBOUT + i] as i64) != (natRGB[i] as i64) { mm = mm + 1 } i = i + 1 } 94 tot_mm = tot_mm + mm 95 if vbits != nbits { bits_all_ok = 0 } 96 97 g_puts(" frame " as *u8); g_pn(f) 98 if kf == 1 { g_puts(" KEY " as *u8) } else { g_puts(" INT " as *u8) } 99 g_puts("bits VM=" as *u8); g_pn(vbits); g_puts(" native=" as *u8); g_pn(nbits); g_puts(" rgb-mismatch=" as *u8); g_pn(mm); g_puts(" / " as *u8); g_pn(N*3); g_puts("\n" as *u8) 100 101 gop_total = gop_total + vbits 102 if kf == 1 { key_bits = vbits } 103 104 // frame-0 RGB saved for the negative control 105 if f == 0 { i = 0; while i < N*3 { rgb0[i] = natRGB[i]; i = i + 1 } } 106 107 // ---- chain recon -> prev (VM: 3 planes O_DEC*->O_PREV*; native: dec*->prev*) ---- 108 i = 0; while i < N { mod.mem[O_PREVY + i] = mod.mem[O_DECY + i]; prevY[i] = decY[i]; i = i + 1 } 109 i = 0; while i < CN { mod.mem[O_PREVCB + i] = mod.mem[O_DECCB + i]; mod.mem[O_PREVCR + i] = mod.mem[O_DECCR + i]; prevCbs[i] = decCbs[i]; prevCrs[i] = decCrs[i]; i = i + 1 } 110 f = f + 1 111 } 112 113 pass = pass + g_check("every COLOR frame decoded-RGB VM==native byte-exact over the whole GOP (no drift)" as *u8, tot_mm == 0); total = total + 1 114 pass = pass + g_check("every COLOR frame bitcount VM==native (wat->wasm lowering faithful across the sequence)" as *u8, bits_all_ok); total = total + 1 115 116 var comp_ok: i64 = 0 117 if key_bits >= 1 { if gop_total * 2 < key_bits * NF { comp_ok = 1 } } 118 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(")\n" as *u8) 119 pass = pass + g_check("COLOR GOP costs < half of all-intra (temporal compression real over the sequence)" as *u8, comp_ok); total = total + 1 120 121 var diff: i64 = 0 122 var j: i64 = 0 123 while j < N*3 { if (rgb0[j] as i64) != (natRGB[j] as i64) { diff = diff + 1 } j = j + 1 } 124 pass = pass + g_check("neg-control: byte-exact comparator DETECTS the moved object (frame0 != frameN)" as *u8, diff > 0); total = total + 1 125 126 g_puts("---- vcodec COLOR GOP wasm-VM gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 127 if pass == total { g_puts("verdict=GREEN -- a full COLOR GOP is wasm-faithful + drift-free + temporally compressed (real color video, VM-proven)\n" as *u8); sys_exit(0); return 0 } 128 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 129}