code wiki / _hdl_build / nx_codec_real_rd.nx

nx_codec_real_rd.nx source

↩ module page · 147 lines · 7877 B

1// nx_codec_real_rd.nx -- HONEST rate-distortion sweep of our SOVEREIGN patent-free intra codec 2// on a REAL frame's luma (ref_frame0.yuv Y plane, 576x1024). Mirrors nx_intra_image (the Phase-1 3// capstone): per 8x8 block [level-shift -> 2D DCT-II (nx_dct8, EXPIRED math) -> scalar quantize Q] 4// -> 16-bit serialize -> ONE amortized rANS (nx_rans, PUBLIC DOMAIN) over the whole image. 5// HONEST total = 512B table + 8B header + rANS payload (table NOT hidden). Sweeps Q to produce an 6// RD curve; prints total bytes + SSE per Q so the harness can compute PSNR and compare vs x264. 7// Entropy round-trip is asserted bit-exact each Q (liar-kill: compression numbers only count if 8// the codec is provably correct -- we own both ends, so lossless entropy is the correctness oracle). 9// Every tool EXPIRED or PUBLIC-DOMAIN; no CABAC; integer-only; nx_cc->nxasm, no gcc. license_tier: ORIGINAL 10import "nx_syscalls.nx" 11import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 12import "nx_dct8.nx" 13import "nx_rans.nx" 14const K_MAGIC_1024: i64 = 1024 15const K_MAGIC_589824: i64 = 589824 16const K_MAGIC_9216: i64 = 9216 17const K_MAGIC_32768: i64 = 32768 18const K_MAGIC_65536: i64 = 65536 19 20func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 21// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 22// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 23// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 24// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 25func g_num(v: i64) -> i64 { nxi_out(v); return 0 } 26func g_w(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 } 27// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 28// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 29// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 30// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 31func g_wn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 } 32func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v } 33func qround(v: i64, q: i64) -> i64 { if v>=0 { return (v + q/2)/q } return 0 - (((0-v) + q/2)/q) } 34 35func main() -> i64 { 36 g_puts("=== OURS: patent-free intra codec RD sweep on REAL frame luma (576x1024) ===\n" as *u8) 37 let W: i64=576; let H: i64=K_MAGIC_1024; let sb: i64=12 38 let npx: i64 = W*H // K_MAGIC_589824 39 40 // read the real frame; first npx bytes = Y plane (4:2:0 luma) 41 let flen: *i64 = sys_mmap(8) as *i64 42 let fb: *u8 = sys_read_file("knowledge/staging/media/ref_frame0.yuv" as *u8, flen) 43 if flen[0] < npx { g_puts("FATAL: frame read short, got=" as *u8); g_num(flen[0]); g_puts("\n" as *u8); sys_exit(2); return 2 } 44 g_puts("-- read " as *u8); g_num(flen[0]); g_puts(" bytes; using first " as *u8); g_num(npx); g_puts(" as luma\n" as *u8) 45 46 let DM: *i64=sys_mmap(64*8) as *i64; nx_dct8_init(DM) 47 let scratch: *i64=sys_mmap(64*8) as *i64 48 let ti: *i64=sys_mmap(8*8) as *i64 49 let to: *i64=sys_mmap(8*8) as *i64 50 let sh: *i64=sys_mmap(64*8) as *i64 51 let co: *i64=sys_mmap(64*8) as *i64 52 let dq: *i64=sys_mmap(64*8) as *i64 53 let rsh: *i64=sys_mmap(64*8) as *i64 54 55 let bw: i64=W/8; let bh: i64=H/8; let nblk: i64=bw*bh // 72*128 = K_MAGIC_9216 56 let ncoef: i64=nblk*64 // K_MAGIC_589824 57 let qall: *i64=sys_mmap(ncoef*8) as *i64 58 let nb: i64=ncoef*2 // 1,179,648 59 let bytes: *u8=sys_mmap(nb+16) 60 let counts: *i64=sys_mmap(257*8) as *i64 61 let fr: *i64=sys_mmap(257*8) as *i64 62 let cu: *i64=sys_mmap(257*8) as *i64 63 let s2s: *i64=sys_mmap((1<<sb)*8) as *i64 64 let cap: i64=nb + (nb/2) + K_MAGIC_1024 65 let enc: *u8=sys_mmap(cap) 66 let bytes2: *u8=sys_mmap(nb+16) 67 let qall2: *i64=sys_mmap(ncoef*8) as *i64 68 let recon: *u8=sys_mmap(npx+16) 69 70 // Q sweep: spans fine->coarse quantization (the RD knob) 71 let qs: *i64=sys_mmap(16*8) as *i64 72 qs[0]=4; qs[1]=8; qs[2]=12; qs[3]=16; qs[4]=24; qs[5]=32; qs[6]=48; qs[7]=64 73 let nq: i64=8 74 75 let lg: i64=sys_openat_append("knowledge/status/codec_real_rd.log" as *u8, 0x1a4) 76 var all_lossless: i64=1 77 var qi: i64=0 78 while qi<nq { 79 let Q: i64=qs[qi] 80 // forward: per 8x8 block DCT + quantize -> qall 81 var byi: i64=0 82 while byi<bh { 83 var bxi: i64=0 84 while bxi<bw { 85 let bidx: i64=byi*bw+bxi 86 var r: i64=0 87 while r<8 { 88 var c: i64=0 89 while c<8 { sh[r*8+c]=(fb[(byi*8+r)*W+(bxi*8+c)] as i64)-128; c=c+1 } 90 r=r+1 91 } 92 nx_dct8_forward_2d(DM, sh, co, scratch, ti, to) 93 var i: i64=0 94 while i<64 { qall[bidx*64+i]=qround(co[i], Q); i=i+1 } 95 bxi=bxi+1 96 } 97 byi=byi+1 98 } 99 // serialize (16-bit LE) + ONE amortized rANS over the whole image 100 var i: i64=0 101 while i<ncoef { let v: i64=qall[i]&0xffff; bytes[2*i]=(v&0xff) as u8; bytes[2*i+1]=((v>>8)&0xff) as u8; i=i+1 } 102 rans_count(bytes, nb, counts) 103 rans_normalize(counts, fr, sb) 104 rans_cum(fr, cu) 105 rans_build_slot2sym(fr, cu, s2s) 106 let enclen: i64=rans_encode(bytes, nb, fr, cu, sb, enc, cap) 107 let total: i64=512+8+enclen 108 // decode -> verify entropy lossless -> deserialize -> reconstruct 109 rans_decode(enc, enclen, nb, fr, cu, sb, s2s, bytes2) 110 var eloss: i64=1 111 i=0; while i<nb { if bytes2[i]!=bytes[i] { eloss=0 } i=i+1 } 112 if eloss==0 { all_lossless=0 } 113 i=0; while i<ncoef { var v: i64=(bytes2[2*i] as i64)|((bytes2[2*i+1] as i64)<<8); if v>=K_MAGIC_32768 { v=v-K_MAGIC_65536 } qall2[i]=v; i=i+1 } 114 byi=0 115 while byi<bh { 116 var bxi: i64=0 117 while bxi<bw { 118 let bidx: i64=byi*bw+bxi 119 var k: i64=0 120 while k<64 { dq[k]=qall2[bidx*64+k]*Q; k=k+1 } 121 nx_dct8_inverse_2d(DM, dq, rsh, scratch, ti, to) 122 var r: i64=0 123 while r<8 { 124 var c: i64=0 125 while c<8 { var p: i64=rsh[r*8+c]+128; if p<0 { p=0 } if p>255 { p=255 } recon[(byi*8+r)*W+(bxi*8+c)]=p as u8; c=c+1 } 126 r=r+1 127 } 128 bxi=bxi+1 129 } 130 byi=byi+1 131 } 132 // SSE + maxErr + meanErr (vs the original luma bytes) 133 var sse: i64=0; var maxe: i64=0; var sume: i64=0 134 i=0; while i<npx { let d: i64=(recon[i] as i64)-(fb[i] as i64); let a: i64=iabs(d); if a>maxe { maxe=a } sume=sume+a; sse=sse+d*d; i=i+1 } 135 let meane: i64=sume/npx 136 let bppx1000: i64=(total*8*1000)/npx 137 138 g_puts("OURS Q=" as *u8); g_num(Q); g_puts(" total=" as *u8); g_num(total); g_puts(" payload=" as *u8); g_num(enclen) 139 g_puts(" sse=" as *u8); g_num(sse); g_puts(" maxErr=" as *u8); g_num(maxe); g_puts(" meanErr=" as *u8); g_num(meane) 140 g_puts(" bppx1000=" as *u8); g_num(bppx1000); g_puts(" eloss=" as *u8); g_num(eloss); g_puts("\n" as *u8) 141 if lg>=0 { g_w(lg,"OURS Q=" as *u8); g_wn(lg,Q); g_w(lg," total=" as *u8); g_wn(lg,total); g_w(lg," sse=" as *u8); g_wn(lg,sse); g_w(lg," maxErr=" as *u8); g_wn(lg,maxe); g_w(lg," meanErr=" as *u8); g_wn(lg,meane); g_w(lg,"\n" as *u8) } 142 qi=qi+1 143 } 144 if lg>=0 { sys_close(lg) } 145 if all_lossless==1 { g_puts("---- entropy lossless ALL Q (codec correct) verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 146 g_puts("---- entropy LOSSY (codec BUG) verdict=RED\n" as *u8); sys_exit(1); return 1 147}