code wiki / _hdl_build / nx_rangecoder_gain_gate.nx
nx_rangecoder_gain_gate.nx source
↩ module page · 87 lines · 4602 B
1import "nx_gate_base.nx"
2// nx_rangecoder_gain_gate.nx -- MEASURE the range-coder gain vs the current CAVLC (nx_ventropy) on REAL DCT
3// coefficients, and prove the range coder round-trips bit-exact. Gain-first (like the DCT switch): if the range
4// coder is meaningfully smaller AND decodes exact, it earns integration into the live codec (task #31).
5// Method: real frame -> per 4x4 residual block -> vt2_fwd + vt2_quant -> code the SAME (flag,run,level) syntax
6// two ways: (A) ve_encode_at fixed-width bits, (B) rc_block_encode adaptive-context binary range coder.
7// license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_rangecoder.nx"
10import "nx_vtransform_dct2.nx"
11import "nx_gate_verdict.nx"
12
13func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
14" as *u8); return ok }
15func gn(v: i64) -> i64 {
16 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
17 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}
18 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
19
20// rc_block_encode / rc_block_decode now live in nx_rangecoder.nx (shared with the codec integration).
21func main() -> i64 {
22 gw("=== nx_rangecoder_gain_gate: range coder vs CAVLC on REAL DCT coefficients ===\n" as *u8)
23 let W: i64=128; let H: i64=128; let qp: i64=20
24 let img: *u8 = sys_mmap(W*H)
25 var r: i64=0
26 while r < H { var c: i64=0
27 while c < W { var v: i64=(c+r)*2 + ((c*r)%11)*7; if c>W/2 { v=v+40 } if v<0 {v=0} if v>255 {v=255}
28 img[r*W+c]=v as u8; c=c+1 } r=r+1 }
29 let nbx: i64=W/4; let nby: i64=H/4; let nblk: i64=nbx*nby
30 let allc: *i64 = sys_mmap(nblk*16*8) as *i64 // stored coeffs for roundtrip verify
31
32 // ---- transform+quant every block, store coeffs, and VLC-encode into one bitstream ----
33 let vlcbuf: *u8 = sys_mmap(1048576)
34 var vbp: i64=0; var bi: i64=0
35 let blk: *i64 = sys_mmap(16*8) as *i64
36 var by: i64=0
37 while by < nby { var bx: i64=0
38 while bx < nbx {
39 var i: i64=0
40 while i < 16 { let px: i64=(by*4 + i/4)*W + bx*4 + (i%4); blk[i] = (img[px] & 0xff) - 128; i=i+1 }
41 vt2_fwd(blk); vt2_quant(blk, qp)
42 i=0; while i < 16 { allc[bi*16 + i] = blk[i]; i=i+1 }
43 vbp = ve_encode_at(blk, vlcbuf, vbp)
44 bi = bi + 1; bx = bx + 1
45 } by = by + 1
46 }
47 let vlc_bytes: i64 = (vbp + 7) / 8
48
49 // ---- range-encode the SAME blocks (adaptive contexts persist across blocks) ----
50 let probs: *i64 = sys_mmap(16*8) as *i64
51 var pi: i64=0; while pi < 16 { probs[pi]=2048; pi=pi+1 } // start at p=0.5
52 let rcbuf: *u8 = sys_mmap(1048576)
53 let est: *i64 = sys_mmap(8*8) as *i64
54 rc_enc_init(est)
55 bi = 0
56 while bi < nblk { rc_block_encode(((allc as i64) + bi*16*8) as *i64, est, rcbuf, probs); bi = bi + 1 }
57 let rc_bytes: i64 = rc_enc_flush(est, rcbuf)
58
59 // ---- decode the range stream and verify EVERY coeff matches (bit-exact) ----
60 pi=0; while pi < 16 { probs[pi]=2048; pi=pi+1 } // decoder starts identical
61 let dst: *i64 = sys_mmap(8*8) as *i64
62 rc_dec_init(dst, rcbuf)
63 let dblk: *i64 = sys_mmap(16*8) as *i64
64 var bad: i64=0; bi=0
65 while bi < nblk {
66 rc_block_decode(dblk, dst, rcbuf, probs)
67 var i: i64=0; while i < 16 { if dblk[i] != allc[bi*16 + i] { bad=1 } i=i+1 }
68 bi = bi + 1
69 }
70
71 gw(" blocks=" as *u8); gn(nblk); gw(" CAVLC=" as *u8); gn(vlc_bytes); gw("B range=" as *u8); gn(rc_bytes)
72 gw("B ratio=" as *u8); gn(rc_bytes*100/vlc_bytes); gw("% roundtrip=" as *u8)
73 if bad==0 { gw("BIT-EXACT\n" as *u8) } else { gw("MISMATCH\n" as *u8) }
74 let saved: i64 = (vlc_bytes - rc_bytes)*100/vlc_bytes
75 gw("RANGECODER: " as *u8); gn(saved); gw("% smaller than CAVLC" as *u8)
76 if (bad==0) & (rc_bytes < vlc_bytes) { gw(" -> GREEN (real gain + exact) -- earns codec integration\n" as *u8) } else {
77 if bad != 0 { gw(" -> RED (roundtrip mismatch)\n" as *u8) } else { gw(" -> range not smaller here (context model needs work)\n" as *u8) }
78 }
79 let ctr: *i64 = gv_ctr()
80 var t1: i64 = 0
81 if bad == 0 { t1 = 1 }
82 gv_check("T1 roundtrip: every decoded coeff bit-exact" as *u8, t1, ctr)
83 var t2: i64 = 0
84 if rc_bytes < vlc_bytes { t2 = 1 }
85 gv_check("T2 real gain: range stream smaller than CAVLC" as *u8, t2, ctr)
86 return gv_verdict("RANGECODER-GAIN-GATE" as *u8, ctr, "adaptive range coder beats CAVLC bit-exactly on real DCT coefficients" as *u8)
87}