code wiki / _hdl_build / nx_rdcurve_gate.nx

nx_rdcurve_gate.nx source

↩ module page · 65 lines · 3927 B

1// nx_rdcurve_gate.nx -- the runnable RATE-DISTORTION harness: intra-code a frame through the sovereign codec 2// (transform + deadzone-quant + entropy) at several QP levels and MEASURE (bytes, PSNR, SSIM) at each -> our RD 3// curve. This is the artifact competitor reconstructions overlay onto for a no-wave exceed/parity verdict. 4// Composes nx_vtransform + nx_ventropy + nx_quality_metric. Native, fast. 5import "nx_syscalls.nx" 6import "nx_gate_emit_lib.nx" 7import "nx_vtransform.nx" 8import "nx_ventropy.nx" 9import "nx_quality_metric.nx" 10 11func rd_block(orig: *u8, recon: *u8, W: i64, bx: i64, by: i64, qp: i64, ebuf: *u8, blk: *i64) -> i64 { 12 let cx: i64 = bx*4; let cy: i64 = by*4 13 var yy: i64 = 0 14 while yy < 4 { var xx: i64 = 0; while xx < 4 { blk[yy*4+xx] = (orig[(cy+yy)*W+(cx+xx)] as i64) - 128; xx=xx+1 } yy=yy+1 } 15 vt_fwd(blk); vt_quant(blk, qp) 16 let bits: i64 = ve_encode(blk, ebuf) 17 vt_dequant(blk, qp); vt_inv(blk) 18 yy = 0 19 while yy < 4 { var xx: i64 = 0; while xx < 4 { var v: i64 = 128 + blk[yy*4+xx]; if v<0 {v=0} if v>255 {v=255} recon[(cy+yy)*W+(cx+xx)] = v as u8; xx=xx+1 } yy=yy+1 } 20 return bits 21} 22func rd_frame(orig: *u8, recon: *u8, W: i64, H: i64, qp: i64, ebuf: *u8, blk: *i64) -> i64 { 23 var bits: i64 = 0 24 var by: i64 = 0 25 while by < H/4 { var bx: i64 = 0; while bx < W/4 { bits = bits + rd_block(orig, recon, W, bx, by, qp, ebuf, blk); bx=bx+1 } by=by+1 } 26 return bits 27} 28 29func main() -> i64 { 30 g_puts("nx_rdcurve harness (our codec's rate-distortion curve, MEASURED)\n" as *u8) 31 var pass: i64 = 0; var total: i64 = 0 32 let W: i64 = 32; let H: i64 = 32; let n: i64 = W*H 33 let orig: *u8 = sys_mmap(n); let recon: *u8 = sys_mmap(n) 34 let ebuf: *u8 = sys_mmap(256); let blk: *i64 = sys_mmap(16*8) as *i64 35 // realistic smooth frame: gradient + a smooth block (low spatial frequency) 36 var y: i64 = 0 37 while y < H { var x: i64 = 0; while x < W { var v: i64 = 70 + (x+y); if x>=8 { if x<24 { if y>=8 { if y<24 { v = 150 + (x/4) } } } } if v>255 {v=255} orig[y*W+x] = v as u8; x=x+1 } y=y+1 } 38 39 // sweep QP: low (fine) -> high (coarse). measure bytes + PSNR + SSIM at each. 40 g_puts(" QP bytes PSNR(cdB) SSIM(x1000)\n" as *u8) 41 let qps: *i64 = sys_mmap(4*8) as *i64; qps[0]=2; qps[1]=8; qps[2]=32 42 let by_arr: *i64 = sys_mmap(4*8) as *i64 43 let ps_arr: *i64 = sys_mmap(4*8) as *i64 44 var i: i64 = 0 45 while i < 3 { 46 let bits: i64 = rd_frame(orig, recon, W, H, qps[i], ebuf, blk) 47 let bytes: i64 = (bits+7)/8 48 let psnr: i64 = qm_psnr_cdb(orig, recon, n) 49 let ssim: i64 = qm_ssim_milli(orig, recon, W, H) 50 by_arr[i] = bytes; ps_arr[i] = psnr 51 g_puts(" " as *u8); g_pn(qps[i]); g_puts(" " as *u8); g_pn(bytes); g_puts(" " as *u8); g_pn(psnr); g_puts(" " as *u8); g_pn(ssim); g_puts("\n" as *u8) 52 i = i + 1 53 } 54 55 // the RD tradeoff must be monotone: coarser QP -> fewer bytes AND lower quality 56 pass = pass + g_check("rate drops as QP rises (fewer bytes at coarser quant)" as *u8, (by_arr[0] > by_arr[1]) & (by_arr[1] > by_arr[2])); total=total+1 57 // PSNR is non-increasing as QP rises (coarser quant never IMPROVES quality) and genuinely degrades at the 58 // coarsest QP. (Low QP plateaus at lossless on smooth content -> capped PSNR; that's correct, not a failure.) 59 pass = pass + g_check("distortion non-increasing + degrades at coarse QP (RD tradeoff)" as *u8, (ps_arr[0] >= ps_arr[1]) & (ps_arr[1] > ps_arr[2]) & (ps_arr[0] > ps_arr[2])); total=total+1 60 pass = pass + g_check("RD curve well-formed (3 measured operating points)" as *u8, (by_arr[0]>0) & (ps_arr[2]>0)); total=total+1 61 62 g_puts("---- rdcurve harness: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 63 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 64 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 65}