code wiki / _hdl_build / nx_vcodec_live_noise_probe.nx
nx_vcodec_live_noise_probe.nx source
↩ module page · 174 lines · 8550 B
1import "nx_gate_base.nx"
2// nx_vcodec_live_noise_probe.nx -- GO/NO-GO for enabling fast RD-skip (emode bit11) in the LIVE call. The 824
3// incident law: clean-content benches CANNOT clear a live emode change -- real webcams dither every pixel every
4// frame, and heat-AQ (bit12) passed every clean bench then collapsed live fps 2.5x on sensor noise. The fast
5// RD-skip trial band [thresh/4, 8*thresh] catches noise-static blocks (zsad ~2-3k) exactly like heat did, so it
6// must prove itself ON NOISE before shipping. This probe: live-like content (textured bg + drifting talking-head
7// face + PER-FRAME temporal sensor noise, the honest vb_* recipe) at live tier 416x320 qp32, through the REAL
8// live path (vv_enc_rct8, 3 planes, recon-chained), emode 673 (ship-830 config) vs 2721 (673|bit11). Reports
9// enc us avg/max, bytes, luma PSNR. GATE: 2721 within 1.3x of 673 enc-time AND avg enc <= 21ms (the 825 ladder's
10// 416x320 tier cost) -> bit11 is live-safe; else ship wide-ME only and keep bit11 VOD. license_tier: ORIGINAL
11import "nx_syscalls.nx"
12import "nx_gate_verdict.nx"
13import "nx_video_codec_wasm.nx"
14import "nx_quality_metric.nx"
15const K_MAGIC_7919: i64 = 7919
16const K_MAGIC_104729: i64 = 104729
17const K_MAGIC_1103515245: i64 = 1103515245
18const K_MAGIC_12345: i64 = 12345
19const K_MAGIC_4194304: i64 = 4194304
20const K_MAGIC_1000000: i64 = 1000000
21const K_MAGIC_4096: i64 = 4096
22const K_MAGIC_2097152: i64 = 2097152
23const K_MAGIC_8192: i64 = 8192
24const K_MAGIC_2721: i64 = 2721
25const K_MAGIC_21000: i64 = 21000
26
27const LW: i64 = 416
28const LH: i64 = 320
29const LQP: i64 = 32
30const LPF: i64 = 30
31
32func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
33" as *u8); return ok }
34func gn(v: i64) -> i64 {
35 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
36 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}
37 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
38func g2(v: i64) -> i64 { gn(v/100); gw("." as *u8); let f: i64=v%100; if f<10 { gw("0" as *u8) } gn(f); return 0 }
39
40func seedctxe(rctx: *i64, emode: i64, est: *i64, probs: *i64, rcbuf: *u8, t8c: *i64) -> i64 {
41 rctx[0]=emode; rctx[1]=est as i64; rctx[2]=probs as i64; rctx[3]=rcbuf as i64; rctx[4]=t8c as i64
42 rctx[5]=0; rctx[6]=0; rctx[7]=0; return 0 }
43
44// live-like LUMA: textured 16px-block background + a 96x96 "face" drifting like a talking head
45func lp_frame(y: *u8, W: i64, H: i64, fi: i64) -> i64 {
46 var yy: i64 = 0
47 while yy < H {
48 var xx: i64 = 0
49 while xx < W {
50 let v: i64 = (((xx/16) * 37 + (yy/16) * 91 + (xx & 3) * 5 + (yy & 3) * 11) & 0x3f) + 96
51 y[yy*W + xx] = v as u8
52 xx = xx + 1
53 }
54 yy = yy + 1
55 }
56 let fx: i64 = 144 + (fi % 8)
57 let fy: i64 = 96 + ((fi / 2) % 6)
58 yy = 0
59 while yy < 96 {
60 var xx: i64 = 0
61 while xx < 96 {
62 let v: i64 = 150 + (((xx + fi * 7) * (yy + 3)) & 0x2f)
63 y[(fy+yy)*W + fx+xx] = v as u8
64 xx = xx + 1
65 }
66 yy = yy + 1
67 }
68 return 0 }
69// TEMPORAL SENSOR NOISE on a whole plane (+-amp, reseeded per frame) -- the 824-killer ingredient
70func lp_noise(p: *u8, n: i64, fi: i64, amp: i64, salt: i64) -> i64 {
71 var s: i64 = 0x5EED + fi * K_MAGIC_7919 + salt * K_MAGIC_104729
72 var i: i64 = 0
73 while i < n {
74 s = (s * K_MAGIC_1103515245 + K_MAGIC_12345) & 0x7fffffff
75 var v: i64 = (p[i] as i64) + ((s >> 9) % (2 * amp + 1)) - amp
76 if v < 0 { v = 0 }
77 if v > 255 { v = 255 }
78 p[i] = v as u8
79 i = i + 1
80 }
81 return 0 }
82
83// one pass at emode: key + LPF P frames, recon-chained, real vv path. out: [0]=avg enc us [1]=max enc us
84// [2]=avg P bytes [3]=avg luma psnr cdb [4]=key bytes
85func pass(emode: i64, bufs: *i64, out: *i64) -> i64 {
86 let N: i64 = LW*LH; let C2: i64 = (LW/2)*(LH/2); let sz: i64 = N + 2*C2
87 let cur: *u8 = bufs[0] as *u8
88 let prevR: *u8 = bufs[1] as *u8
89 let recon: *u8 = bufs[2] as *u8
90 let wire: *u8 = bufs[3] as *u8; let blk: *i64 = bufs[4] as *i64; let mv: *i64 = bufs[5] as *i64
91 let est: *i64 = bufs[6] as *i64; let probs: *i64 = bufs[7] as *i64; let rcbuf: *u8 = bufs[8] as *u8
92 let t8c: *i64 = bufs[9] as *i64; let rctx: *i64 = bufs[10] as *i64
93 var i: i64 = 0
94 while i < sz { prevR[i] = 0 as u8; i = i + 1 }
95 // frame 0: key
96 lp_frame(cur, LW, LH, 0)
97 lp_noise(cur, N, 0, 2, 0)
98 i = 0
99 while i < 2*C2 { cur[N+i] = 128 as u8; i = i + 1 }
100 lp_noise(((cur as i64) + N) as *u8, 2*C2, 0, 1, 1)
101 seedctxe(rctx, emode, est, probs, rcbuf, t8c)
102 let kb: i64 = vv_enc_rct8(cur, prevR, recon, LW, LH, LQP, 1, LQP*188, wire, K_MAGIC_4194304, blk, mv, rctx)
103 if kb <= 0 { return 0-1 }
104 var j: i64 = 0
105 while j < sz { prevR[j] = recon[j]; j = j + 1 }
106 var encSum: i64 = 0; var encMax: i64 = 0; var bSum: i64 = 0; var pSum: i64 = 0
107 var fi: i64 = 1
108 while fi <= LPF {
109 lp_frame(cur, LW, LH, fi)
110 lp_noise(cur, N, fi, 2, 0)
111 i = 0
112 while i < 2*C2 { cur[N+i] = 128 as u8; i = i + 1 }
113 lp_noise(((cur as i64) + N) as *u8, 2*C2, fi, 1, 1)
114 seedctxe(rctx, emode, est, probs, rcbuf, t8c)
115 let t0: i64 = sys_now_us()
116 let nb: i64 = vv_enc_rct8(cur, prevR, recon, LW, LH, LQP, 0, LQP*188, wire, K_MAGIC_4194304, blk, mv, rctx)
117 let t1: i64 = sys_now_us()
118 if nb <= 0 { return 0-1 }
119 encSum = encSum + (t1 - t0)
120 if t1 - t0 > encMax { encMax = t1 - t0 }
121 bSum = bSum + nb
122 pSum = pSum + qm_psnr_cdb(recon, cur, N)
123 j = 0
124 while j < sz { prevR[j] = recon[j]; j = j + 1 }
125 fi = fi + 1
126 }
127 out[0] = encSum / LPF; out[1] = encMax; out[2] = bSum / LPF; out[3] = pSum / LPF; out[4] = kb
128 return 0 }
129
130func report(tag: *u8, o: *i64) -> i64 {
131 gw(" " as *u8); gw(tag)
132 gw(": enc=" as *u8); gn(o[0]); gw("us avg (max " as *u8); gn(o[1]); gw("us, " as *u8)
133 var fps: i64 = 0
134 if o[0] > 0 { fps = K_MAGIC_1000000 / o[0] }
135 gn(fps); gw("fps) P=" as *u8); gn(o[2]); gw("B PSNR=" as *u8); g2(o[3]); gw("dB key=" as *u8); gn(o[4]); gw("B\n" as *u8)
136 return 0 }
137
138func main() -> i64 {
139 gw("=== nx_vcodec_live_noise_probe: bit11 fast-RD-skip on SENSOR NOISE (the 824 law) 416x320 qp32 ===\n" as *u8)
140 let ctr: *i64 = gv_ctr()
141 let N: i64 = LW*LH; let C2: i64 = (LW/2)*(LH/2); let sz: i64 = N + 2*C2
142 let bufs: *i64 = sys_mmap(16*8) as *i64
143 bufs[0]=sys_mmap(sz+64) as i64; bufs[1]=sys_mmap(sz+64) as i64; bufs[2]=sys_mmap(sz+64) as i64
144 bufs[3]=sys_mmap(K_MAGIC_4194304) as i64; bufs[4]=sys_mmap(512) as i64; bufs[5]=sys_mmap(128) as i64
145 bufs[6]=sys_mmap(64) as i64; bufs[7]=sys_mmap(K_MAGIC_4096) as i64; bufs[8]=sys_mmap(K_MAGIC_2097152) as i64
146 bufs[9]=sys_mmap(K_MAGIC_8192) as i64; bufs[10]=sys_mmap(128) as i64
147 vc_t8_init(bufs[9] as *i64)
148 let oA: *i64 = sys_mmap(64) as *i64
149 let oB: *i64 = sys_mmap(64) as *i64
150 if pass(673, bufs, oA) != 0 { gw("pass 673 fail -> RED\n" as *u8); return 1 }
151 if pass(K_MAGIC_2721, bufs, oB) != 0 { gw("pass 2721 fail -> RED\n" as *u8); return 1 }
152 report("emode 673 (ship-830)" as *u8, oA)
153 report("emode 2721 (673|RDsk)" as *u8, oB)
154 let ratio10: i64 = oB[0] * 10 / oA[0]
155 gw(" enc-time ratio = " as *u8); gn(ratio10/10); gw("." as *u8); gn(ratio10%10); gw("x dPSNR = " as *u8)
156 let dq: i64 = oB[3] - oA[3]
157 if dq >= 0 { gw("+" as *u8) } g2(dq)
158 gw("dB dBytes = " as *u8)
159 let db: i64 = (oB[2]-oA[2])*1000/oA[2]
160 if db >= 0 { gw("+" as *u8) } gn(db); gw("permille\n" as *u8)
161 // GATE: within 1.3x of 673 AND under the 825 ladder tier cost (21ms at 416x320)
162 var okr: i64 = 1
163 // cross-multiply, never divide (truncated bar fired this reject one unit early)
164 if oB[0] * 10 > oA[0] * 13 { okr = 0 }
165 gv_check("T1 emode 2721 enc-time within 1.3x of 673 on sensor noise" as *u8, okr, ctr)
166 var okc: i64 = 1
167 if oB[0] > K_MAGIC_21000 { okc = 0 }
168 gv_check("T2 avg enc <= 21ms (the 825 ladder 416x320 tier cost)" as *u8, okc, ctr)
169 let rc: i64 = gv_verdict("VCODEC-LIVE-NOISE-PROBE" as *u8, ctr, "bit11 fast-RD-skip proven on sensor noise" as *u8)
170 if rc == 0 { gw("DECISION: bit11 LIVE-SAFE ON NOISE (within fps budget) -> ship emode 2721\n" as *u8) }
171 if rc != 0 { gw("DECISION: bit11 FAILS the noise-fps gate -> ship wide-ME only, keep bit11 VOD (the 824 law holds)\n" as *u8) }
172 sys_exit(rc)
173 return rc
174}