code wiki / _hdl_build / nx_video_codec_wasm_gate.nx

nx_video_codec_wasm_gate.nx source

↩ module page · 140 lines · 7243 B

1// nx_video_codec_wasm_gate.nx -- gate the YUV420 wire wrapper (nx_video_codec_wasm) BEFORE it ships (task 2// #27): color round-trip sane, encode->decode CLOSED LOOP bit-exact (decoder recon == encoder recon, the 3// law the realseq gate proved for luma, now for all 3 planes through the packed wire stream), P-frame 4// smaller than key on real motion, and the wire boundary REFUSES truncated/hostile frames (rule 12). 5// Frames carry +/-2 noise (the synthetic-bench trap fix: bit-identical statics flatter the codec). 6// 0=GREEN / 1=RED. license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_video_codec_wasm.nx" 9 10func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func g_n(v: i64) -> i64 { 12 var m: i64=v; if m<0{g_w("-\x00" as *u8);m=0-m} 13 let t: *u8=sys_mmap(24); var k: i64=0; if m==0{t[0]=48 as u8;k=1} else {while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}} 14 let o: *u8=sys_mmap(24); var j: i64=0; while j<k{o[j]=t[k-1-j];j=j+1} sys_write(1,o,k); return 0 } 15func chk(cond: i64, name: *u8, pp: *i64, tp: *i64) -> i64 { 16 tp[0]=tp[0]+1; g_w(name) 17 if cond==1 { pp[0]=pp[0]+1; g_w(" ok\n\x00" as *u8) } else { g_w(" FAIL\n\x00" as *u8) } 18 return 0 } 19 20const GW: i64 = 320 21const GH: i64 = 240 22 23// deterministic pseudo-noise (no Math.random: reproducible gate) 24func g_noise(seed: i64) -> i64 { return ((seed * 1103515245 + 12345) >> 16) & 3 } 25 26// paint frame f: horizontal gradient + a 48x48 bright square at (sx, 60) + +/-2 noise keyed by (f,x,y) 27func g_paint_rgba(rgba: *u8, sx: i64, f: i64) -> i64 { 28 var y: i64 = 0 29 while y < GH { 30 var x: i64 = 0 31 while x < GW { 32 let o: i64 = (y * GW + x) * 4 33 var r: i64 = (x * 255) / GW 34 var g: i64 = (y * 255) / GH 35 var b: i64 = 96 36 if x >= sx { if x < sx + 48 { if y >= 60 { if y < 108 { r = 230; g = 220; b = 40 } } } } 37 let nz: i64 = g_noise(f * 131071 + y * GW + x) - 1 38 rgba[o] = vv_clamp255(r + nz) as u8 39 rgba[o+1] = vv_clamp255(g + nz) as u8 40 rgba[o+2] = vv_clamp255(b + nz) as u8 41 rgba[o+3] = 255 as u8 42 x = x + 1 43 } 44 y = y + 1 45 } 46 return 0 47} 48 49func g_bytes_eq(a: *u8, b: *u8, n: i64) -> i64 { 50 var i: i64 = 0 51 while i < n { if a[i] != b[i] { return 0 } i = i + 1 } 52 return 1 53} 54// PSNR(dB, integer floor) of the Y plane vs reference 55func g_psnr_y(a: *u8, b: *u8, n: i64) -> i64 { 56 var se: i64 = 0 57 var i: i64 = 0 58 while i < n { let d: i64 = (a[i] & 0xff) - (b[i] & 0xff); se = se + d * d; i = i + 1 } 59 if se == 0 { return 99 } 60 let mse_x100: i64 = (se * 100) / n 61 if mse_x100 == 0 { return 99 } 62 // psnr = 10*log10(255^2/mse); integer approx via table thresholds 63 let ratio: i64 = (65025 * 100) / mse_x100 64 var db: i64 = 0 65 var r: i64 = ratio 66 while r >= 10 { db = db + 10; r = r / 10 } // log10 integer decades 67 // refine one decade: r in [1,10) 68 if r >= 8 { db = db + 9 } else { if r >= 6 { db = db + 8 } else { if r >= 5 { db = db + 7 } else { if r >= 4 { db = db + 6 } else { if r >= 3 { db = db + 5 } else { if r >= 2 { db = db + 3 } else { db = db + 0 } } } } } } 69 return db 70} 71 72func main() -> i64 { 73 g_w("=== nx_video_codec_wasm_gate: YUV420 wire wrapper -- closed-loop, P<key, boundary-refusal ===\n\x00" as *u8) 74 let pp: *i64 = sys_mmap(8) as *i64; pp[0]=0 75 let tp: *i64 = sys_mmap(8) as *i64; tp[0]=0 76 let N: i64 = GW * GH 77 let YUVB: i64 = N + N / 2 78 let rgba1: *u8 = sys_mmap(N * 4) 79 let rgba2: *u8 = sys_mmap(N * 4) 80 let rgbaBack: *u8 = sys_mmap(N * 4) 81 let yuv1: *u8 = sys_mmap(YUVB) 82 let yuv2: *u8 = sys_mmap(YUVB) 83 let prevE: *u8 = sys_mmap(YUVB) // encoder chain 84 let reconE: *u8 = sys_mmap(YUVB) 85 let prevD: *u8 = sys_mmap(YUVB) // decoder chain 86 let reconD: *u8 = sys_mmap(YUVB) 87 let stream: *u8 = sys_mmap(262144) 88 let blk: *i64 = sys_mmap(256) as *i64 89 let mv: *i64 = sys_mmap(64) as *i64 90 91 // T1: color round-trip on frame1 (gradient+square): Y-plane PSNR of rgba->yuv->rgba >= 30dB 92 g_paint_rgba(rgba1, 100, 1) 93 vv_rgba_to_yuv420(rgba1, GW, GH, yuv1) 94 vv_yuv420_to_rgba(yuv1, GW, GH, rgbaBack) 95 let yA: *u8 = sys_mmap(N) 96 let yB: *u8 = sys_mmap(N) 97 var i: i64 = 0 98 while i < N { yA[i] = ((77*(rgba1[i*4]&0xff) + 150*(rgba1[i*4+1]&0xff) + 29*(rgba1[i*4+2]&0xff)) >> 8) as u8 99 yB[i] = ((77*(rgbaBack[i*4]&0xff) + 150*(rgbaBack[i*4+1]&0xff) + 29*(rgbaBack[i*4+2]&0xff)) >> 8) as u8 100 i = i + 1 } 101 let cdb: i64 = g_psnr_y(yA, yB, N) 102 g_w(" color round-trip Y-PSNR ~\x00" as *u8); g_n(cdb); g_w("dB\n\x00" as *u8) 103 chk((cdb >= 30) as i64, "T1 rgba->yuv420->rgba >= 30dB\x00" as *u8, pp, tp) 104 105 // T2: KEYFRAME closed loop: enc then dec; decoder recon must equal encoder recon BYTE-IDENTICAL 106 let kb: i64 = vv_enc(yuv1, prevE, reconE, GW, GH, 32, 1, 6016, stream, 262144, blk, mv) 107 g_w(" key bytes=\x00" as *u8); g_n(kb); g_w("\n\x00" as *u8) 108 chk((kb > 12) as i64, "T2a key encodes (>12B)\x00" as *u8, pp, tp) 109 let dr: i64 = vv_dec(prevD, reconD, GW, GH, 32, stream, kb, blk, mv) 110 chk((dr == 0) as i64, "T2b key decodes rc=0\x00" as *u8, pp, tp) 111 chk(g_bytes_eq(reconD, reconE, YUVB), "T2c CLOSED LOOP: decoder recon == encoder recon (all 3 planes, bit-exact)\x00" as *u8, pp, tp) 112 let kdb: i64 = g_psnr_y(reconD, yuv1, N) 113 g_w(" key Y-PSNR ~\x00" as *u8); g_n(kdb); g_w("dB\n\x00" as *u8) 114 chk((kdb >= 28) as i64, "T2d key quality >= 28dB at qp32\x00" as *u8, pp, tp) 115 116 // T3: P-FRAME on real motion (square moves 12px) + noise: closed loop again + P < key 117 g_paint_rgba(rgba2, 112, 2) 118 vv_rgba_to_yuv420(rgba2, GW, GH, yuv2) 119 var c: i64 = 0 120 while c < YUVB { prevE[c] = reconE[c]; prevD[c] = reconD[c]; c = c + 1 } 121 let pb: i64 = vv_enc(yuv2, prevE, reconE, GW, GH, 32, 0, 6016, stream, 262144, blk, mv) 122 g_w(" P bytes=\x00" as *u8); g_n(pb); g_w("\n\x00" as *u8) 123 chk((pb > 12) as i64, "T3a P encodes\x00" as *u8, pp, tp) 124 let dr2: i64 = vv_dec(prevD, reconD, GW, GH, 32, stream, pb, blk, mv) 125 chk((dr2 == 0) as i64, "T3b P decodes rc=0\x00" as *u8, pp, tp) 126 chk(g_bytes_eq(reconD, reconE, YUVB), "T3c P closed loop bit-exact\x00" as *u8, pp, tp) 127 chk((pb * 2 < kb) as i64, "T3d P < key/2 (motion comp earns its keep)\x00" as *u8, pp, tp) 128 129 // T4 NEG: truncated wire frame REFUSED (no read past the buffer) 130 chk((vv_dec(prevD, reconD, GW, GH, 32, stream, pb / 2, blk, mv) == 0 - 1) as i64, "T4 NEG truncated frame -> -1\x00" as *u8, pp, tp) 131 // T5 NEG: hostile length header (yb lies huge) REFUSED 132 let evil: *u8 = sys_mmap(64) 133 vv_wr_u32(evil, 0, 1000000); vv_wr_u32(evil, 4, 0); vv_wr_u32(evil, 8, 0) 134 chk((vv_dec(prevD, reconD, GW, GH, 32, evil, 64, blk, mv) == 0 - 1) as i64, "T5 NEG hostile length header -> -1\x00" as *u8, pp, tp) 135 136 g_w(" SCORE: \x00" as *u8); g_n(pp[0]); g_w("/\x00" as *u8); g_n(tp[0]); g_w("\n\x00" as *u8) 137 if pp[0] == tp[0] { g_w("VIDEO-CODEC-WASM-GATE verdict=GREEN -- YUV420 wire wrapper: closed-loop bit-exact, P<key on motion, boundary-hardened\n\x00" as *u8); return 0 } 138 g_w("VIDEO-CODEC-WASM-GATE verdict=RED\n\x00" as *u8) 139 return 1 140}