code wiki / _hdl_build / nx_vcodec_entropy_gap.nx
nx_vcodec_entropy_gap.nx source
↩ module page · 135 lines · 7436 B
1// nx_vcodec_entropy_gap.nx -- Measures the neural-entropy prize using real coefficient data to quantify gains from neighbor-context significance coding.
2import "nx_gate_base.nx"
3// nx_vcodec_entropy_gap.nx -- MEASURES the neural-entropy prize (R2a) with real coefficient data, so the
4// flagship investment is justified by a number, not a hope. The biggest classical->CABAC/learned entropy
5// win is NEIGHBOR-CONTEXT significance coding: the probability that a coefficient is nonzero depends heavily
6// on whether its already-coded neighbors were nonzero -- a signal the current run-length range coder throws
7// away. On REAL residuals (frame diff, 8x8 DCT + quant at a field qp) this computes the significance-map
8// entropy under three context models: order-0 (no context) -> scan-position -> position + neighbor-count.
9// The drop from position to position+neighbor = the measured prize a learned entropy model would capture.
10// GREEN = the ladder is monotone (richer context = fewer bits) and the neighbor prize is a real, reported
11// percentage. This bounds R2a: if the prize is large, the neural jump is worth it; if small, it isn't.
12// license_tier: ORIGINAL
13import "nx_syscalls.nx"
14import "nx_dct8.nx"
15import "nx_vcodec.nx"
16import "nx_quality_metric.nx"
17const K_MAGIC_1000000: i64 = 1000000
18
19const NW: i64 = 576
20const NH: i64 = 1024
21
22func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
23" as *u8); return ok }
24func gn(v: i64) -> i64 {
25 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
26 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}
27 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
28
29// conditional entropy in BITS (integer) of a binary event over C contexts. h[c*2+s] = count of symbol s in ctx c.
30// H = sum_c sum_s n * log2(total_c / n) = sum_c sum_s n*(log2(total_c) - log2(n)). qm_log2_q16 = Q16 log2.
31func cond_bits(h: *i64, C: i64) -> i64 {
32 var bitsq: i64 = 0 // accumulate in Q16
33 var c: i64 = 0
34 while c < C {
35 let n0: i64 = h[c*2]; let n1: i64 = h[c*2+1]; let tot: i64 = n0 + n1
36 if tot > 0 {
37 let lt: i64 = qm_log2_q16(tot)
38 if n0 > 0 { bitsq = bitsq + n0 * (lt - qm_log2_q16(n0)) }
39 if n1 > 0 { bitsq = bitsq + n1 * (lt - qm_log2_q16(n1)) }
40 }
41 c = c + 1
42 }
43 return bitsq >> 16
44}
45
46func main() -> i64 {
47 gw("=== nx_vcodec_entropy_gap: the R2a neural-entropy prize, MEASURED on real coefficients ===\n" as *u8)
48 let box: *i64 = sys_mmap(16) as *i64
49 let yuv: *u8 = sys_read_file("/mnt/c/Users/elder/nishi-core/nxc2/knowledge/staging/media/bframe_test_decoded.yuv" as *u8, box)
50 if (yuv as i64) == 0 { gw("cannot read yuv -> RED\n" as *u8); return 1 }
51 let N: i64=NW*NH; let FB: i64 = N + N/2
52 if box[0] < 12*FB { gw("file too small -> RED\n" as *u8); return 1 }
53
54 let M: *i64 = sys_mmap(64*8) as *i64
55 nx_dct8_init(M)
56 let block: *i64 = sys_mmap(64*8) as *i64
57 let dout: *i64 = sys_mmap(64*8) as *i64
58 let scr: *i64 = sys_mmap(64*8) as *i64
59 let ti: *i64 = sys_mmap(8*8) as *i64
60 let to: *i64 = sys_mmap(8*8) as *i64
61 let sig: *i64 = sys_mmap(64*8) as *i64 // per-position significance in the current 8x8 block
62
63 // histograms: A = order-0 (1 ctx), B = scan-position bucket (8), C = position(8) x neighborcount(4) = 32
64 let hA: *i64 = sys_mmap(2*8) as *i64
65 let hB: *i64 = sys_mmap(8*2*8) as *i64
66 let hC: *i64 = sys_mmap(32*2*8) as *i64
67 var z: i64=0; while z<2 { hA[z]=0; z=z+1 }
68 z=0; while z<16 { hB[z]=0; z=z+1 }
69 z=0; while z<64 { hC[z]=0; z=z+1 }
70
71 let qp: i64 = 28 // a field operating point
72 var coeffs_total: i64 = 0
73 // frames 7..11: residual = cur - prev (zero-MV P-residual proxy -> representative coefficient statistics)
74 var f: i64 = 7
75 while f < 12 {
76 let cur: *u8 = ((yuv as i64) + f*FB) as *u8
77 let prv: *u8 = ((yuv as i64) + (f-1)*FB) as *u8
78 var by: i64 = 0
79 while by < NH/8 {
80 var bx: i64 = 0
81 while bx < NW/8 {
82 var yy: i64=0
83 while yy < 8 { var xx: i64=0
84 while xx < 8 {
85 let p: i64 = (by*8+yy)*NW + (bx*8+xx)
86 block[yy*8+xx] = (cur[p] & 0xff) - (prv[p] & 0xff)
87 xx=xx+1 } yy=yy+1 }
88 nx_dct8_forward_2d(M, block, dout, scr, ti, to)
89 var qi: i64=0; while qi<64 { dout[qi] = dout[qi]; qi=qi+1 } // dout holds DCT coeffs
90 vc_quant8(dout, qp) // quantize in place -> levels
91 var i: i64=0; while i<64 { if dout[i] != 0 { sig[i]=1 } else { sig[i]=0 } i=i+1 }
92 // tally significance under the 3 context models (raster scan; causal neighbors L, U, UL)
93 var yb: i64=0
94 while yb < 8 { var xb: i64=0
95 while xb < 8 {
96 let pos: i64 = yb*8 + xb
97 let s: i64 = sig[pos]
98 hA[s] = hA[s] + 1 // order-0
99 var pb: i64 = pos >> 3; if pb > 7 { pb = 7 } // position bucket (row index 0..7)
100 hB[pb*2 + s] = hB[pb*2 + s] + 1 // position context
101 var nb: i64 = 0 // causal neighbor significance count
102 if xb > 0 { nb = nb + sig[pos-1] }
103 if yb > 0 { nb = nb + sig[pos-8] }
104 if xb > 0 { if yb > 0 { nb = nb + sig[pos-9] } }
105 if nb > 3 { nb = 3 }
106 hC[(pb*4 + nb)*2 + s] = hC[(pb*4 + nb)*2 + s] + 1 // position + neighbor context
107 coeffs_total = coeffs_total + 1
108 xb=xb+1 } yb=yb+1 }
109 bx=bx+1 }
110 by=by+1 }
111 f=f+1
112 }
113
114 let bA: i64 = cond_bits(hA, 1)
115 let bB: i64 = cond_bits(hB, 8)
116 let bC: i64 = cond_bits(hC, 32)
117 gw(" coefficients measured: " as *u8); gn(coeffs_total); gw("\n" as *u8)
118 gw(" significance-map bits -- order0=" as *u8); gn(bA)
119 gw(" position=" as *u8); gn(bB)
120 gw(" position+neighbor=" as *u8); gn(bC); gw("\n" as *u8)
121 let posgain: i64 = (bA - bB)*1000 / bA
122 let nbgain: i64 = (bB - bC)*1000 / bB
123 let bitspc_A: i64 = bA*1000/coeffs_total
124 let bitspc_C: i64 = bC*1000/coeffs_total
125 gw(" position saves " as *u8); gn(posgain); gw("permille vs order0; NEIGHBOR-CONTEXT (the R2a prize) saves " as *u8)
126 gn(nbgain); gw("permille MORE\n" as *u8)
127 gw(" significance cost: order0=" as *u8); gn(bitspc_A); gw("milli-bits/coeff -> pos+neighbor=" as *u8); gn(bitspc_C); gw("milli-bits/coeff\n" as *u8)
128
129 var pass: i64=0; var tot: i64=0
130 tot=tot+1; if bB < bA { pass=pass+1 } // position context helps
131 tot=tot+1; if bC < bB { pass=pass+1 } // neighbor context helps MORE (the prize is real)
132 tot=tot+1; if coeffs_total > K_MAGIC_1000000 { pass=pass+1 } // measured on real, substantial data
133 gw("ENTROPY-GAP: " as *u8); gn(pass); gw("/" as *u8); gn(tot)
134 if pass==tot { gw(" GREEN -- neighbor-context significance is a MEASURED entropy prize (R2a justified)\n" as *u8); return 0 }
135 gw(" RED\n" as *u8); return 1 }