code wiki / _hdl_build / nx_vcodec_bandwire_gate.nx
nx_vcodec_bandwire_gate.nx source
↩ module page · 84 lines · 5327 B
1import "nx_gate_base.nx"
2// nx_vcodec_bandwire_gate.nx -- SOVEREIGN verification of the tile-parallel BANDED WIRE FORMAT + full-prev
3// orchestration (the logic the client's Web Workers wrap). Nishi all the way -- native nx, NO Node. Encodes
4// a frame as K bands (full-prev: shared prev, each vv_enc_band writes its rows into a shared recon), packs
5// the client's banded wire [0x80|K][qp][w][h][(by0,by1,len)xK][streams], PARSES it back, decodes each band
6// (vv_dec_band into a fresh recon, same prev), and asserts the decoded recon == the encoded recon BIT-EXACT.
7// This is the SSOT for the wire format the browser client must mirror. license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_video_codec_wasm.nx"
10
11func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
12" as *u8); return ok }
13func gn(v: i64) -> i64 {
14 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
15 let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}
16 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
17func ck(name: *u8, ok: i64, p: *i64, t: *i64) -> i64 {
18 t[0]=t[0]+1; gw(" " as *u8); gw(name); if ok==1 { gw(" PASS\n" as *u8); p[0]=p[0]+1 } else { gw(" FAIL\n" as *u8) } return 0 }
19func eqb(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if (a[i]&0xff)!=(b[i]&0xff) { return 0 } i=i+1 } return 1 }
20func fillf(yuv: *u8, W: i64, H: i64, sh: i64, seedprev: i64) -> i64 {
21 let N: i64=W*H; let C: i64=(W/2)*(H/2)
22 var r: i64=0
23 while r<H { var c: i64=0
24 while c<W { let sc: i64=c+sh; var v: i64=(sc*2)+30+(seedprev*9); if sc>W/2 { v=v+45 }
25 if v<0 {v=0} if v>255 {v=255}; yuv[r*W+c]=v as u8; c=c+1 } r=r+1 }
26 var i: i64=N; while i<N+2*C { yuv[i]=120 as u8; i=i+1 } return 0 }
27
28// run one banded roundtrip through the CLIENT wire format; return 1 if the decoded recon is bit-exact.
29func bandwire(W: i64, H: i64, qp: i64, K: i64, key: i64, seedprev: i64) -> i64 {
30 let N: i64=W*H; let C: i64=(W/2)*(H/2); let sz: i64=N+2*C
31 let cur: *u8=sys_mmap(sz); let prev: *u8=sys_mmap(sz)
32 let recon: *u8=sys_mmap(sz); let recon2: *u8=sys_mmap(sz)
33 let blk: *i64=sys_mmap(512) as *i64; let mv: *i64=sys_mmap(128) as *i64
34 let str: *u8=sys_mmap(262144) // per-band streams scratch
35 let wire: *u8=sys_mmap(262144) // the assembled banded wire
36 var z: i64=0; while z<sz { if seedprev==1 { prev[z]=((z*3+7)&255) as u8 } else { prev[z]=0 as u8 } z=z+1 }
37 fillf(cur, W, H, 0, seedprev)
38 let BH: i64=H/16; let tr: i64=BH/K
39 // ---- ENCODE: K bands, shared prev + shared recon (full-prev) ----
40 let lens: *i64=sys_mmap(8*32) as *i64
41 var k: i64=0
42 while k < K {
43 let by0: i64=k*tr; let by1: i64=(k+1)*tr; let so: i64=(str as i64)+k*0x8000
44 let n: i64=vv_enc_band(cur, prev, recon, W, H, by0, by1, qp, key, 6016, so as *u8, 0x8000, blk, mv)
45 if n <= 0 { return 0 }
46 lens[k]=n
47 k=k+1
48 }
49 // ---- ASSEMBLE the client wire: [0x80|K][qp][wlo][whi][hlo][hhi][(by0,by1,lenlo,lenhi)xK][streams] ----
50 var wp: i64=0
51 wire[wp]=(0x80 | (key << 6) | K) as u8; wp=wp+1; wire[wp]=qp as u8; wp=wp+1 // b7=banded b6=key b0-3=K
52 wire[wp]=(W&255) as u8; wp=wp+1; wire[wp]=(W>>8) as u8; wp=wp+1
53 wire[wp]=(H&255) as u8; wp=wp+1; wire[wp]=(H>>8) as u8; wp=wp+1
54 k=0
55 while k < K { let by0: i64=k*tr; let by1: i64=(k+1)*tr; let n: i64=lens[k]
56 wire[wp]=by0 as u8; wp=wp+1; wire[wp]=by1 as u8; wp=wp+1
57 wire[wp]=(n&255) as u8; wp=wp+1; wire[wp]=(n>>8) as u8; wp=wp+1; k=k+1 }
58 k=0
59 while k < K { let n: i64=lens[k]; let src: *u8=((str as i64)+k*0x8000) as *u8
60 var j: i64=0; while j<n { wire[wp+j]=src[j]; j=j+1 } wp=wp+n; k=k+1 }
61 // ---- PARSE the wire + DECODE each band into recon2 (fresh), same prev ----
62 let dK: i64=wire[0]&0x0f; let dqp: i64=wire[1]&0xff
63 let dw: i64=(wire[2]&0xff)|((wire[3]&0xff)<<8); let dh: i64=(wire[4]&0xff)|((wire[5]&0xff)<<8)
64 var tp: i64=6; var sp: i64=6+dK*4
65 var d: i64=0
66 while d < dK {
67 let by0: i64=wire[tp]&0xff; let by1: i64=wire[tp+1]&0xff
68 let len: i64=(wire[tp+2]&0xff)|((wire[tp+3]&0xff)<<8); tp=tp+4
69 vv_dec_band(prev, recon2, dw, dh, by0, by1, dqp, ((wire as i64)+sp) as *u8, len, blk, mv)
70 sp=sp+len; d=d+1
71 }
72 if dK != K { return 0 }
73 return eqb(recon, recon2, sz) }
74
75func main() -> i64 {
76 gw("=== nx_vcodec_bandwire_gate: SOVEREIGN banded wire + full-prev orchestration (nishi, no Node) ===\n" as *u8)
77 let p: *i64=sys_mmap(16) as *i64; let t: *i64=sys_mmap(16) as *i64
78 ck("320x256 KEY 4-band -> wire -> decode == encode recon\x00" as *u8, bandwire(320, 256, 18, 4, 1, 0), p, t)
79 ck("320x256 P 4-band (full-prev motion) bit-exact\x00" as *u8, bandwire(320, 256, 18, 4, 0, 1), p, t)
80 ck("256x192 KEY 3-band bit-exact\x00" as *u8, bandwire(256, 192, 18, 3, 1, 0), p, t)
81 ck("320x256 KEY 2-band bit-exact\x00" as *u8, bandwire(320, 256, 18, 2, 1, 0), p, t)
82 gw("BANDWIRE: " as *u8); gn(p[0]); gw("/" as *u8); gn(t[0])
83 if p[0]==t[0] { gw(" GREEN -- the wire format is the SSOT; the browser Workers just fan the band encodes over it\n" as *u8); return 0 }
84 gw(" RED\n" as *u8); return 1 }