code wiki / _hdl_build / nx_room_interframe.nx

nx_room_interframe.nx source

↩ module page · 105 lines · 7025 B

1// nx_room_interframe.nx -- the FIX for the measured 9fps (nx_room_bw_measure: full-frame JPEG saturates 2// the uplink; inter-frame coding -> 31fps). Sovereign inter-frame (P-frame) codec CORE, ready to wire 3// onto the live media plane: encode(prev,cur) -> per-block {SKIP if unchanged | quantized WHT residual}, 4// decode(prev,stream) -> cur'. Skip blocks copy prev EXACTLY (zero bytes); changed blocks carry only the 5// residual. 4x4 Walsh-Hadamard (self-inverse up to /16) + rounding quant. Proves the mechanism is real + 6// byte-budget; the client capture-diff + the daemon relay of residual packets + deploy = the wire-in. 7// GREEN = reconstruct within quant error, skip-blocks bit-exact, bandwidth strictly smaller. 8// Evidence -> knowledge/status/room_interframe.log. license_tier: ORIGINAL 9import "nx_syscalls.nx" 10 11const IF_LOG: *u8 = "knowledge/status/room_interframe.log" 12 13func iw(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 14func in2(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(fd,"-" as *u8,1)} 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} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(fd,bb,k); return 0 } 15 16// 4x4 WHT (symmetric H, self-inverse up to scale): out = H*X*H 17func wht(X: *i64, tmp: *i64, out: *i64) -> i64 { 18 var c: i64=0 19 while c<4 { let a0: i64=X[c]; let a1: i64=X[4+c]; let a2: i64=X[8+c]; let a3: i64=X[12+c]; tmp[c]=a0+a1+a2+a3; tmp[4+c]=a0+a1-a2-a3; tmp[8+c]=a0-a1-a2+a3; tmp[12+c]=a0-a1+a2-a3; c=c+1 } 20 var r: i64=0 21 while r<4 { let b0: i64=tmp[r*4]; let b1: i64=tmp[r*4+1]; let b2: i64=tmp[r*4+2]; let b3: i64=tmp[r*4+3]; out[r*4]=b0+b1+b2+b3; out[r*4+1]=b0+b1-b2-b3; out[r*4+2]=b0-b1-b2+b3; out[r*4+3]=b0-b1+b2-b3; r=r+1 } 22 return 0 23} 24// encode one residual block -> quantized coeffs q[16]; returns nonzero count (0 => caller marks SKIP) 25func enc_block(res: *i64, tmp: *i64, T: *i64, q: *i64, QSTEP: i64) -> i64 { 26 wht(res, tmp, T) 27 var nz: i64=0; var i: i64=0 28 while i<16 { var v: i64=T[i]; var qq: i64=0; if v>=0 { qq=(v+QSTEP/2)/QSTEP } else { qq=0-((0-v+QSTEP/2)/QSTEP) } q[i]=qq; if qq!=0 {nz=nz+1} i=i+1 } 29 return nz 30} 31// decode quantized coeffs -> residual block out[16] (dequant -> inverse WHT /16) 32func dec_block(q: *i64, tmp: *i64, T: *i64, out: *i64, QSTEP: i64) -> i64 { 33 var i: i64=0; while i<16 { T[i]=q[i]*QSTEP; i=i+1 } 34 wht(T, tmp, out) 35 i=0; while i<16 { out[i]=out[i]/16; i=i+1 } 36 return 0 37} 38 39func main() -> i64 { 40 let W: i64=64; let H: i64=64; let QSTEP: i64=12 41 let BX: i64=W/4; let BY: i64=H/4; let NB: i64=BX*BY 42 let prev:*i64 = sys_mmap(8*W*H) as *i64 43 let cur: *i64 = sys_mmap(8*W*H) as *i64 44 let rec: *i64 = sys_mmap(8*W*H) as *i64 // decoder reconstruction 45 let res: *i64 = sys_mmap(8*16) as *i64 46 let q: *i64 = sys_mmap(8*16) as *i64 47 let tmp: *i64 = sys_mmap(8*16) as *i64 48 let T: *i64 = sys_mmap(8*16) as *i64 49 let rb: *i64 = sys_mmap(8*16) as *i64 50 let curblk: *i64 = sys_mmap(8*16) as *i64 51 52 // prev = static textured background; cur = prev + a small moving change (top-left quadrant) 53 var y: i64=0 54 while y<H { var x: i64=0; while x<W { let bg: i64 = 60 + (((x>>3)+(y>>3)) & 7)*7; prev[y*W+x]=bg; var cc: i64=bg; if x<24 { if y<24 { cc = bg + 30 + ((x+y) & 15) } } cur[y*W+x]=cc; x=x+1 } y=y+1 } 55 56 var bytes_full: i64=0; var bytes_inter: i64=0; var changed: i64=0 57 var max_err: i64=0; var skip_exact: i64=1 58 var by: i64=0 59 while by < BY { 60 var bx: i64=0 61 while bx < BX { 62 // build residual + detect change 63 var i: i64=0; var any: i64=0 64 while i<4 { var j: i64=0; while j<4 { let p: i64=(by*4+i)*W+(bx*4+j); curblk[i*4+j]=cur[p]; let d: i64=cur[p]-prev[p]; res[i*4+j]=d; if d!=0 {any=1} j=j+1 } i=i+1 } 65 // FULL-frame cost = encode the cur block itself (intra) 66 bytes_full = bytes_full + 1 + enc_block(curblk, tmp, T, q, QSTEP) * 2 67 if any==0 { 68 // SKIP: decoder copies prev -> must be exact (unchanged block) 69 i=0; while i<4 { var j: i64=0; while j<4 { let p: i64=(by*4+i)*W+(bx*4+j); rec[p]=prev[p]; if rec[p]!=cur[p] { skip_exact=0 } j=j+1 } i=i+1 } 70 } else { 71 changed = changed + 1 72 let nz: i64 = enc_block(res, tmp, T, q, QSTEP) 73 bytes_inter = bytes_inter + 1 + nz * 2 74 dec_block(q, tmp, T, rb, QSTEP) 75 i=0; while i<4 { var j: i64=0; while j<4 { let p: i64=(by*4+i)*W+(bx*4+j); let v: i64=prev[p]+rb[i*4+j]; rec[p]=v; var e: i64=v-cur[p]; if e<0 {e=0-e} if e>max_err {max_err=e} j=j+1 } i=i+1 } 76 } 77 bx=bx+1 78 } 79 by=by+1 80 } 81 bytes_inter = bytes_inter + NB/8 // 1 skip bit per block 82 83 let ratio_x10: i64 = (bytes_full*10)/bytes_inter 84 let changed_pct: i64 = (changed*100)/NB 85 86 var ok: i64=1 87 if skip_exact != 1 { ok=0 } // unchanged blocks MUST reconstruct exactly 88 if max_err > QSTEP { ok=0 } // changed blocks within quant error 89 if bytes_inter >= bytes_full { ok=0 } // inter MUST be smaller 90 if changed == 0 { ok=0 } // there IS a change (non-vacuous) 91 92 iw(1, "=== nx_room_interframe -- sovereign inter-frame (P-frame) codec: the 9fps FIX ===\n") 93 iw(1, " "); in2(1, W); iw(1, "x"); in2(1, H); iw(1, ", "); in2(1, NB); iw(1, " blocks, changed="); in2(1, changed_pct); iw(1, "%, QSTEP="); in2(1, QSTEP); iw(1, "\n") 94 iw(1, " full-frame bytes="); in2(1, bytes_full); iw(1, " inter-frame bytes="); in2(1, bytes_inter); iw(1, " -> "); in2(1, ratio_x10); iw(1, "/10x smaller\n") 95 iw(1, " T1 skip-blocks reconstruct EXACT: "); if skip_exact==1 { iw(1,"PASS") } else { iw(1,"FAIL") } 96 iw(1, "\n T2 changed-blocks within quant error (max_err="); in2(1, max_err); iw(1, " <= QSTEP="); in2(1, QSTEP); iw(1, "): "); if max_err<=QSTEP { iw(1,"PASS") } else { iw(1,"FAIL") } 97 iw(1, "\n T3 inter < full (bandwidth win): "); if bytes_inter<bytes_full { iw(1,"PASS") } else { iw(1,"FAIL") } 98 iw(1, "\n WIRE-IN: client keeps prev canvas -> diff -> send changed blocks; daemon relays residual packets; client applies residual to prev. + adaptive bitrate + datagram transport.\n") 99 if ok==1 { iw(1, "VERDICT: GREEN (inter-frame codec reconstructs + saves bandwidth -- the measured 9->31fps fix, implementable)\n") } else { iw(1, "VERDICT: RED\n") } 100 101 let lfd: i64 = sys_openat_append(IF_LOG, 420) 102 if lfd>=0 { iw(lfd, "ROOMIF full="); in2(lfd, bytes_full); iw(lfd, " inter="); in2(lfd, bytes_inter); iw(lfd, " changed_pct="); in2(lfd, changed_pct); iw(lfd, " max_err="); in2(lfd, max_err); iw(lfd, " skip_exact="); in2(lfd, skip_exact); if ok==1 { iw(lfd, " verdict=GREEN\n") } else { iw(lfd, " verdict=RED\n") } sys_close(lfd) } 103 if ok==1 { sys_exit(0) } else { sys_exit(1) } 104 return 0 105}