code wiki / _hdl_build / nx_vcodec_bgop_bench.nx
nx_vcodec_bgop_bench.nx source
↩ module page · 466 lines · 28063 B
1import "nx_gate_base.nx"
2// nx_vcodec_bgop_bench.nx -- the B-FRAME (vcv-9 VOD) measurement + decode-parity gate. The bframe ceiling probe
3// measured -43% residual for per-MB direction selection (past vs future ref); this bench measures what that is
4// WORTH end-to-end in real bits: ARM-P = today's chain (key + 11 P, anchors at distance 1) vs ARM-B = key +
5// P-anchors at distance 2 + disposable B between (I B P B P ... P), both at emode 4005 (VOD-best), same content
6// (bframe_test 576x1024), same qp ladder. LIAR-KILLER: EVERY frame of BOTH arms is DECODED through a decode-side
7// reference chain (never fed encoder state) and must be BYTE-IDENTICAL to the encoder recon -- an encoder that
8// "reconstructs" from data not actually in the stream cannot pass. Verdict material: per-qp bytes/PSNR per arm ->
9// paste the B-arm curve into extjudge_verdict.js vs the x264-with-B oracle. license_tier: ORIGINAL
10import "nx_syscalls.nx"
11import "nx_estate_path.nx" // ep_anchor: the CWD must not decide this organ's verdict
12import "nx_video_codec_wasm.nx"
13import "nx_quality_metric.nx"
14import "nx_gate_verdict.nx"
15const K_MAGIC_4096: i64 = 4096
16const K_MAGIC_2048: i64 = 2048
17const K_MAGIC_4194304: i64 = 4194304
18const K_MAGIC_2097152: i64 = 2097152
19const K_MAGIC_8192: i64 = 8192
20
21const NW: i64 = 576
22const NH: i64 = 1024
23const EMODE: i64 = 4005
24
25func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
26" as *u8); return ok }
27func gn(v: i64) -> i64 {
28 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
29 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}
30 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
31func 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 }
32
33func seedctxe(rctx: *i64, emode: i64, est: *i64, probs: *i64, rcbuf: *u8, t8c: *i64) -> i64 {
34 rctx[0]=emode; rctx[1]=est as i64; rctx[2]=probs as i64; rctx[3]=rcbuf as i64; rctx[4]=t8c as i64
35 rctx[5]=0; rctx[6]=0; rctx[7]=0; rctx[8]=0; rctx[9]=sys_mmap(K_MAGIC_4096) as i64; return 0 } // rctx[9] = vcv-10 MV row-plane ([6]/[7]=nf, [8]=heat are TAKEN); 4KB covers W<=K_MAGIC_2048
36
37// first mismatching byte index between a and b over n, or -1 if identical
38func bcmp(a: *u8, b: *u8, n: i64) -> i64 {
39 var i: i64 = 0
40 while i < n { if (a[i] & 0xff) != (b[i] & 0xff) { return i } i = i + 1 }
41 return 0 - 1 }
42
43func zero(p: *u8, n: i64) -> i64 { var i: i64=0; while i<n { p[i]=0 as u8; i=i+1 } return 0 }
44
45// ---- ARM P: key + 11 sequential P (today's chain). out[0]=bytes out[1]=mean psnr cdb out[2]=parity fails
46func arm_p(yuv: *u8, qp: i64, bufs: *i64, out: *i64) -> i64 {
47 let N: i64=NW*NH; let C2: i64=(NW/2)*(NH/2); let sz: i64=N+2*C2
48 let FB: i64 = N + N/2
49 let e0: *u8 = bufs[0] as *u8; let e1: *u8 = bufs[1] as *u8
50 let d0: *u8 = bufs[3] as *u8; let d1: *u8 = bufs[4] as *u8
51 let wire: *u8 = bufs[6] as *u8; let blk: *i64 = bufs[7] as *i64; let mv: *i64 = bufs[8] as *i64
52 let estE: *i64 = bufs[9] as *i64; let probsE: *i64 = bufs[10] as *i64; let rcbufE: *u8 = bufs[11] as *u8
53 let t8cE: *i64 = bufs[12] as *i64; let rctxE: *i64 = bufs[13] as *i64
54 let estD: *i64 = bufs[14] as *i64; let probsD: *i64 = bufs[15] as *i64; let rcbufD: *u8 = bufs[16] as *u8
55 let t8cD: *i64 = bufs[17] as *i64; let rctxD: *i64 = bufs[18] as *i64
56 zero(e0, sz); zero(d0, sz)
57 var ePrev: *u8 = e0; var eCur: *u8 = e1
58 var dPrev: *u8 = d0; var dCur: *u8 = d1
59 var bytes: i64=0; var cdb: i64=0; var pfail: i64=0
60 var f: i64=0
61 while f<12 {
62 let cur: *u8 = ((yuv as i64) + f*FB) as *u8
63 var key: i64=0; if f==0 { key=1 }
64 seedctxe(rctxE, EMODE, estE, probsE, rcbufE, t8cE)
65 let nb: i64 = vv_enc_rct8(cur, ePrev, eCur, NW, NH, qp, key, qp*188, wire, K_MAGIC_4194304, blk, mv, rctxE)
66 if nb <= 0 { return 0-1 }
67 seedctxe(rctxD, EMODE, estD, probsD, rcbufD, t8cD)
68 if vv_dec_rct8(dPrev, dCur, NW, NH, qp, wire, nb, blk, mv, rctxD) != 0 { pfail = pfail + 1000 }
69 if bcmp(eCur, dCur, sz) >= 0 { pfail = pfail + 1 }
70 bytes = bytes + nb
71 cdb = cdb + qm_psnr_cdb(eCur, cur, N)
72 let tE: *u8 = ePrev; ePrev = eCur; eCur = tE
73 let tD: *u8 = dPrev; dPrev = dCur; dCur = tD
74 f=f+1
75 }
76 out[0]=bytes; out[1]=cdb/12; out[2]=pfail
77 return 0 }
78
79// ---- ARM B: I(0), then {P(t) from t-2, B(t-1) from {t-2,t}} for t=2,4,6,8,10, then P(11) from 10.
80// out[0]=bytes out[1]=mean psnr out[2]=parity fails out[3]=I bytes out[4]=P bytes out[5]=B bytes
81func arm_b(yuv: *u8, qp: i64, bufs: *i64, out: *i64) -> i64 {
82 let N: i64=NW*NH; let C2: i64=(NW/2)*(NH/2); let sz: i64=N+2*C2
83 let FB: i64 = N + N/2
84 let e0: *u8 = bufs[0] as *u8; let e1: *u8 = bufs[1] as *u8; let eb: *u8 = bufs[2] as *u8
85 let d0: *u8 = bufs[3] as *u8; let d1: *u8 = bufs[4] as *u8; let db: *u8 = bufs[5] as *u8
86 let wire: *u8 = bufs[6] as *u8; let blk: *i64 = bufs[7] as *i64; let mv: *i64 = bufs[8] as *i64
87 let estE: *i64 = bufs[9] as *i64; let probsE: *i64 = bufs[10] as *i64; let rcbufE: *u8 = bufs[11] as *u8
88 let t8cE: *i64 = bufs[12] as *i64; let rctxE: *i64 = bufs[13] as *i64
89 let estD: *i64 = bufs[14] as *i64; let probsD: *i64 = bufs[15] as *i64; let rcbufD: *u8 = bufs[16] as *u8
90 let t8cD: *i64 = bufs[17] as *i64; let rctxD: *i64 = bufs[18] as *i64
91 zero(e0, sz); zero(d0, sz)
92 var eA0: *u8 = e0; var eA1: *u8 = e1 // encoder anchor rotation (prev, cur)
93 var dA0: *u8 = d0; var dA1: *u8 = d1 // decoder anchor rotation
94 var bytes: i64=0; var cdb: i64=0; var pfail: i64=0
95 var bI: i64=0; var bP: i64=0; var bB: i64=0
96 // I frame (display 0) -> lands in eA1/dA1, then rotate so it becomes the prev anchor
97 let cur0: *u8 = yuv
98 seedctxe(rctxE, EMODE, estE, probsE, rcbufE, t8cE)
99 let nb0: i64 = vv_enc_rct8(cur0, eA0, eA1, NW, NH, qp, 1, qp*188, wire, K_MAGIC_4194304, blk, mv, rctxE)
100 if nb0 <= 0 { return 0-1 }
101 seedctxe(rctxD, EMODE, estD, probsD, rcbufD, t8cD)
102 if vv_dec_rct8(dA0, dA1, NW, NH, qp, wire, nb0, blk, mv, rctxD) != 0 { pfail = pfail + 1000 }
103 if bcmp(eA1, dA1, sz) >= 0 { pfail = pfail + 1 }
104 bytes = bytes + nb0; bI = nb0
105 cdb = cdb + qm_psnr_cdb(eA1, cur0, N)
106 var tE: *u8 = eA0; eA0 = eA1; eA1 = tE
107 var tD: *u8 = dA0; dA0 = dA1; dA1 = tD
108 // anchor pairs
109 var t: i64 = 2
110 while t <= 10 {
111 // P anchor at display t, ref = prev anchor (distance 2)
112 let curP: *u8 = ((yuv as i64) + t*FB) as *u8
113 seedctxe(rctxE, EMODE, estE, probsE, rcbufE, t8cE)
114 let nbp: i64 = vv_enc_rct8(curP, eA0, eA1, NW, NH, qp, 0, qp*188, wire, K_MAGIC_4194304, blk, mv, rctxE)
115 if nbp <= 0 { return 0-1 }
116 seedctxe(rctxD, EMODE, estD, probsD, rcbufD, t8cD)
117 if vv_dec_rct8(dA0, dA1, NW, NH, qp, wire, nbp, blk, mv, rctxD) != 0 { pfail = pfail + 1000 }
118 if bcmp(eA1, dA1, sz) >= 0 { pfail = pfail + 1 }
119 bytes = bytes + nbp; bP = bP + nbp
120 cdb = cdb + qm_psnr_cdb(eA1, curP, N)
121 // B at display t-1, refs = {prev anchor (past), this anchor (future)}; recon DISPOSABLE
122 let curB: *u8 = ((yuv as i64) + (t-1)*FB) as *u8
123 seedctxe(rctxE, EMODE, estE, probsE, rcbufE, t8cE)
124 let nbb: i64 = vv_enc_b(curB, eA0, eA1, eb, NW, NH, qp, qp*188, wire, K_MAGIC_4194304, blk, mv, rctxE)
125 if nbb <= 0 { return 0-1 }
126 seedctxe(rctxD, EMODE, estD, probsD, rcbufD, t8cD)
127 if vv_dec_b(dA0, dA1, db, NW, NH, qp, wire, nbb, blk, mv, rctxD) != 0 { pfail = pfail + 1000 }
128 if bcmp(eb, db, sz) >= 0 { pfail = pfail + 1 }
129 bytes = bytes + nbb; bB = bB + nbb
130 cdb = cdb + qm_psnr_cdb(eb, curB, N)
131 // rotate anchors
132 tE = eA0; eA0 = eA1; eA1 = tE
133 tD = dA0; dA0 = dA1; dA1 = tD
134 t = t + 2
135 }
136 // tail P at display 11 from anchor 10
137 let cur11: *u8 = ((yuv as i64) + 11*FB) as *u8
138 seedctxe(rctxE, EMODE, estE, probsE, rcbufE, t8cE)
139 let nbt: i64 = vv_enc_rct8(cur11, eA0, eA1, NW, NH, qp, 0, qp*188, wire, K_MAGIC_4194304, blk, mv, rctxE)
140 if nbt <= 0 { return 0-1 }
141 seedctxe(rctxD, EMODE, estD, probsD, rcbufD, t8cD)
142 if vv_dec_rct8(dA0, dA1, NW, NH, qp, wire, nbt, blk, mv, rctxD) != 0 { pfail = pfail + 1000 }
143 if bcmp(eA1, dA1, sz) >= 0 { pfail = pfail + 1 }
144 bytes = bytes + nbt; bP = bP + nbt
145 cdb = cdb + qm_psnr_cdb(eA1, cur11, N)
146 out[0]=bytes; out[1]=cdb/12; out[2]=pfail; out[3]=bI; out[4]=bP; out[5]=bB
147 return 0 }
148
149// ---- ARM B3: x264-class structure -- I(0), anchors P(4),P(8),P(11), B's between (8 B's = 67%), refs = the
150// two bracketing anchors, per-B direction bits. bqoff = B-frame qp offset (disposable frames tolerate coarser
151// quant; x264 pbratio-style). out fields as arm_b.
152func arm_b3(yuv: *u8, qp: i64, bqoff: i64, bufs: *i64, out: *i64) -> i64 {
153 let N: i64=NW*NH; let C2: i64=(NW/2)*(NH/2); let sz: i64=N+2*C2
154 let FB: i64 = N + N/2
155 let e0: *u8 = bufs[0] as *u8; let e1: *u8 = bufs[1] as *u8; let eb: *u8 = bufs[2] as *u8
156 let d0: *u8 = bufs[3] as *u8; let d1: *u8 = bufs[4] as *u8; let db: *u8 = bufs[5] as *u8
157 let wire: *u8 = bufs[6] as *u8; let blk: *i64 = bufs[7] as *i64; let mv: *i64 = bufs[8] as *i64
158 let estE: *i64 = bufs[9] as *i64; let probsE: *i64 = bufs[10] as *i64; let rcbufE: *u8 = bufs[11] as *u8
159 let t8cE: *i64 = bufs[12] as *i64; let rctxE: *i64 = bufs[13] as *i64
160 let estD: *i64 = bufs[14] as *i64; let probsD: *i64 = bufs[15] as *i64; let rcbufD: *u8 = bufs[16] as *u8
161 let t8cD: *i64 = bufs[17] as *i64; let rctxD: *i64 = bufs[18] as *i64
162 zero(e0, sz); zero(d0, sz)
163 var eA0: *u8 = e0; var eA1: *u8 = e1
164 var dA0: *u8 = d0; var dA1: *u8 = d1
165 var bytes: i64=0; var cdb: i64=0; var pfail: i64=0
166 var bI: i64=0; var bP: i64=0; var bB: i64=0
167 let qb: i64 = qp + bqoff
168 // I at display 0
169 seedctxe(rctxE, EMODE, estE, probsE, rcbufE, t8cE)
170 let nb0: i64 = vv_enc_rct8(yuv, eA0, eA1, NW, NH, qp, 1, qp*188, wire, K_MAGIC_4194304, blk, mv, rctxE)
171 if nb0 <= 0 { return 0-1 }
172 seedctxe(rctxD, EMODE, estD, probsD, rcbufD, t8cD)
173 if vv_dec_rct8(dA0, dA1, NW, NH, qp, wire, nb0, blk, mv, rctxD) != 0 { pfail = pfail + 1000 }
174 if bcmp(eA1, dA1, sz) >= 0 { pfail = pfail + 1 }
175 bytes = bytes + nb0; bI = nb0
176 cdb = cdb + qm_psnr_cdb(eA1, yuv, N)
177 var tE: *u8 = eA0; eA0 = eA1; eA1 = tE
178 var tD: *u8 = dA0; dA0 = dA1; dA1 = tD
179 // groups: {anchor t, B's [bs..t-1]} = {4: 1,2,3}, {8: 5,6,7}, {11: 9,10}
180 var gi: i64 = 0
181 while gi < 3 {
182 var t: i64 = 4; var bs: i64 = 1
183 if gi == 1 { t = 8; bs = 5 }
184 if gi == 2 { t = 11; bs = 9 }
185 let curP: *u8 = ((yuv as i64) + t*FB) as *u8
186 seedctxe(rctxE, EMODE, estE, probsE, rcbufE, t8cE)
187 let nbp: i64 = vv_enc_rct8(curP, eA0, eA1, NW, NH, qp, 0, qp*188, wire, K_MAGIC_4194304, blk, mv, rctxE)
188 if nbp <= 0 { return 0-1 }
189 seedctxe(rctxD, EMODE, estD, probsD, rcbufD, t8cD)
190 if vv_dec_rct8(dA0, dA1, NW, NH, qp, wire, nbp, blk, mv, rctxD) != 0 { pfail = pfail + 1000 }
191 if bcmp(eA1, dA1, sz) >= 0 { pfail = pfail + 1 }
192 bytes = bytes + nbp; bP = bP + nbp
193 cdb = cdb + qm_psnr_cdb(eA1, curP, N)
194 var bt: i64 = bs
195 while bt < t {
196 let curB: *u8 = ((yuv as i64) + bt*FB) as *u8
197 seedctxe(rctxE, EMODE, estE, probsE, rcbufE, t8cE)
198 let nbb: i64 = vv_enc_b(curB, eA0, eA1, eb, NW, NH, qb, qb*188, wire, K_MAGIC_4194304, blk, mv, rctxE)
199 if nbb <= 0 { return 0-1 }
200 seedctxe(rctxD, EMODE, estD, probsD, rcbufD, t8cD)
201 if vv_dec_b(dA0, dA1, db, NW, NH, qb, wire, nbb, blk, mv, rctxD) != 0 { pfail = pfail + 1000 }
202 if bcmp(eb, db, sz) >= 0 { pfail = pfail + 1 }
203 bytes = bytes + nbb; bB = bB + nbb
204 cdb = cdb + qm_psnr_cdb(eb, curB, N)
205 bt = bt + 1
206 }
207 tE = eA0; eA0 = eA1; eA1 = tE
208 tD = dA0; dA0 = dA1; dA1 = tD
209 gi = gi + 1
210 }
211 out[0]=bytes; out[1]=cdb/12; out[2]=pfail; out[3]=bI; out[4]=bP; out[5]=bB
212 return 0 }
213
214// ---- ARM LTR: key + 11 CHAINED dual-ref frames -- each P uses the _b syntax with past=prev recon,
215// fut=THE KEYFRAME recon (a long-term reference). No reorder, no latency: BOTH refs are past => RTC-SAFE.
216// The clean-ref diagnostic proved the ~5dB gradual drift is 100% reference quant-error; static-but-drifting
217// MBs re-anchor on the pristine keyframe via the per-MB direction bit. recon chains as the next ref (deblocked
218// in-loop by vv_enc_b, decoder mirrors) -- unlike the disposable B, these frames ARE references.
219// out[0]=bytes out[1]=mean psnr out[2]=parity fails out[3]=I bytes out[4]=chained bytes out[5]=0
220func arm_ltr(yuv: *u8, qp: i64, bufs: *i64, out: *i64) -> i64 {
221 let N: i64=NW*NH; let C2: i64=(NW/2)*(NH/2); let sz: i64=N+2*C2
222 let FB: i64 = N + N/2
223 let e0: *u8 = bufs[0] as *u8; let e1: *u8 = bufs[1] as *u8; let ek: *u8 = bufs[2] as *u8
224 let d0: *u8 = bufs[3] as *u8; let d1: *u8 = bufs[4] as *u8; let dk: *u8 = bufs[5] as *u8
225 let wire: *u8 = bufs[6] as *u8; let blk: *i64 = bufs[7] as *i64; let mv: *i64 = bufs[8] as *i64
226 let estE: *i64 = bufs[9] as *i64; let probsE: *i64 = bufs[10] as *i64; let rcbufE: *u8 = bufs[11] as *u8
227 let t8cE: *i64 = bufs[12] as *i64; let rctxE: *i64 = bufs[13] as *i64
228 let estD: *i64 = bufs[14] as *i64; let probsD: *i64 = bufs[15] as *i64; let rcbufD: *u8 = bufs[16] as *u8
229 let t8cD: *i64 = bufs[17] as *i64; let rctxD: *i64 = bufs[18] as *i64
230 zero(e0, sz); zero(d0, sz)
231 var ePrev: *u8 = e0; var eCur: *u8 = e1
232 var dPrev: *u8 = d0; var dCur: *u8 = d1
233 var bytes: i64=0; var cdb: i64=0; var pfail: i64=0
234 // keyframe -> its recon is ALSO copied to ek/dk as the standing long-term ref
235 seedctxe(rctxE, EMODE, estE, probsE, rcbufE, t8cE)
236 let nb0: i64 = vv_enc_rct8(yuv, ePrev, eCur, NW, NH, qp, 1, qp*188, wire, K_MAGIC_4194304, blk, mv, rctxE)
237 if nb0 <= 0 { return 0-1 }
238 seedctxe(rctxD, EMODE, estD, probsD, rcbufD, t8cD)
239 if vv_dec_rct8(dPrev, dCur, NW, NH, qp, wire, nb0, blk, mv, rctxD) != 0 { pfail = pfail + 1000 }
240 if bcmp(eCur, dCur, sz) >= 0 { pfail = pfail + 1 }
241 bytes = bytes + nb0
242 cdb = cdb + qm_psnr_cdb(eCur, yuv, N)
243 var i: i64=0; while i<sz { ek[i]=eCur[i]; i=i+1 }
244 i=0; while i<sz { dk[i]=dCur[i]; i=i+1 }
245 var tE: *u8 = ePrev; ePrev = eCur; eCur = tE
246 var tD: *u8 = dPrev; dPrev = dCur; dCur = tD
247 var bC: i64=0
248 var f: i64=1
249 while f<12 {
250 let cur: *u8 = ((yuv as i64) + f*FB) as *u8
251 seedctxe(rctxE, EMODE, estE, probsE, rcbufE, t8cE)
252 let nb: i64 = vv_enc_b(cur, ePrev, ek, eCur, NW, NH, qp, qp*188, wire, K_MAGIC_4194304, blk, mv, rctxE)
253 if nb <= 0 { return 0-1 }
254 seedctxe(rctxD, EMODE, estD, probsD, rcbufD, t8cD)
255 if vv_dec_b(dPrev, dk, dCur, NW, NH, qp, wire, nb, blk, mv, rctxD) != 0 { pfail = pfail + 1000 }
256 if bcmp(eCur, dCur, sz) >= 0 { pfail = pfail + 1 }
257 bytes = bytes + nb; bC = bC + nb
258 cdb = cdb + qm_psnr_cdb(eCur, cur, N)
259 tE = ePrev; ePrev = eCur; eCur = tE
260 tD = dPrev; dPrev = dCur; dCur = tD
261 f=f+1
262 }
263 out[0]=bytes; out[1]=cdb/12; out[2]=pfail; out[3]=nb0; out[4]=bC; out[5]=0
264 return 0 }
265
266// ---- ARM LEAN (rct9): key(rct8) + 11 P via the LEAN-SKIP syntax -- rc-context skip flag, lv/t8 only on
267// coded MBs. The measured ~1.1KB/frame header floor is the prize. Chain + decode-parity like arm_p.
268func arm_lean(yuv: *u8, qp: i64, bufs: *i64, out: *i64) -> i64 {
269 return arm_lean_t(yuv, qp, 188, bufs, out) }
270// thm = skip-threshold multiplier (thresh = qp*thm): the qp*188 skip economics were tuned when a skip cost
271// 4 header bits; under lean-skip (~0.2 bits) coding MORE cheap-residual MBs may win RD (less drift).
272func arm_lean_t(yuv: *u8, qp: i64, thm: i64, bufs: *i64, out: *i64) -> i64 {
273 let N: i64=NW*NH; let C2: i64=(NW/2)*(NH/2); let sz: i64=N+2*C2
274 let FB: i64 = N + N/2
275 let e0: *u8 = bufs[0] as *u8; let e1: *u8 = bufs[1] as *u8
276 let d0: *u8 = bufs[3] as *u8; let d1: *u8 = bufs[4] as *u8
277 let wire: *u8 = bufs[6] as *u8; let blk: *i64 = bufs[7] as *i64; let mv: *i64 = bufs[8] as *i64
278 let estE: *i64 = bufs[9] as *i64; let probsE: *i64 = bufs[10] as *i64; let rcbufE: *u8 = bufs[11] as *u8
279 let t8cE: *i64 = bufs[12] as *i64; let rctxE: *i64 = bufs[13] as *i64
280 let estD: *i64 = bufs[14] as *i64; let probsD: *i64 = bufs[15] as *i64; let rcbufD: *u8 = bufs[16] as *u8
281 let t8cD: *i64 = bufs[17] as *i64; let rctxD: *i64 = bufs[18] as *i64
282 zero(e0, sz); zero(d0, sz)
283 var ePrev: *u8 = e0; var eCur: *u8 = e1
284 var dPrev: *u8 = d0; var dCur: *u8 = d1
285 var bytes: i64=0; var cdb: i64=0; var pfail: i64=0
286 var f: i64=0
287 while f<12 {
288 let cur: *u8 = ((yuv as i64) + f*FB) as *u8
289 var nb: i64 = 0
290 if f == 0 {
291 seedctxe(rctxE, EMODE, estE, probsE, rcbufE, t8cE)
292 nb = vv_enc_rct8(cur, ePrev, eCur, NW, NH, qp, 1, qp*188, wire, K_MAGIC_4194304, blk, mv, rctxE)
293 if nb <= 0 { return 0-1 }
294 seedctxe(rctxD, EMODE, estD, probsD, rcbufD, t8cD)
295 if vv_dec_rct8(dPrev, dCur, NW, NH, qp, wire, nb, blk, mv, rctxD) != 0 { pfail = pfail + 1000 }
296 } else {
297 seedctxe(rctxE, EMODE, estE, probsE, rcbufE, t8cE)
298 nb = vv_enc_rct9(cur, ePrev, eCur, NW, NH, qp, qp*thm, wire, K_MAGIC_4194304, blk, mv, rctxE)
299 if nb <= 0 { return 0-1 }
300 seedctxe(rctxD, EMODE, estD, probsD, rcbufD, t8cD)
301 if vv_dec_rct9(dPrev, dCur, NW, NH, qp, wire, nb, blk, mv, rctxD) != 0 { pfail = pfail + 1000 }
302 }
303 if bcmp(eCur, dCur, sz) >= 0 { pfail = pfail + 1 }
304 bytes = bytes + nb
305 cdb = cdb + qm_psnr_cdb(eCur, cur, N)
306 let tE: *u8 = ePrev; ePrev = eCur; eCur = tE
307 let tD: *u8 = dPrev; dPrev = dCur; dCur = tD
308 f=f+1
309 }
310 out[0]=bytes; out[1]=cdb/12; out[2]=pfail
311 return 0 }
312
313// bytes the P-arm needs at PSNR `pt` (cdb), interpolated on its measured curve (arrays high->low psnr); -1 if out of range
314func p_bytes_at(pP: *i64, bPp: *i64, n: i64, pt: i64) -> i64 {
315 var j: i64 = 0
316 while j < n-1 {
317 if pt <= pP[j] { if pt >= pP[j+1] {
318 return bPp[j+1] + (pt - pP[j+1]) * (bPp[j] - bPp[j+1]) / (pP[j] - pP[j+1]) } }
319 j = j + 1
320 }
321 return 0-1 }
322
323func main() -> i64 {
324 // ANCHOR FIRST (2026-08-04, nx_cwdguard finding): this organ reads a RELATIVE
325 // knowledge/ path, so its answer depended on where it was launched. No-op when
326 // already at the estate root, so the cron/MCP context is unchanged.
327 ep_anchor()
328 gw("=== nx_vcodec_bgop_bench: B-frames (vcv-9 VOD) vs P-chain, emode 4005 + FULL decode-parity ===\n" as *u8)
329 let box: *i64 = sys_mmap(16) as *i64
330 // ★PATH MADE RELATIVE 2026-08-01 (debt 1785560248 + 1785526434). This baked an ABSOLUTE LAPTOP path,
331 // /mnt/c/Users/elder/nishi-core/nxc2/... , which does not exist on the NAS where organs now run. All 17
332 // videocodec gates shared it, so all 17 returned 'cannot read yuv -> RED' with pass=0/0 -- ZERO teeth
333 // run -- and videocodec is the domain BLOCKING EVERY DEPLOY through nx_deploy_ready evidence-honesty.
334 // A relative path resolves on BOTH hosts (organs run with cwd=nishihost on the NAS, nxc2 on the laptop),
335 // so this is the portable fix rather than swapping one machine's absolute path for another's.
336 // The asset itself was absent from the LAPTOP too, so the clip is now SYNTHESISED by nx_yuvgen
337 // (576x1024x16, controlled diagonal motion + moving block) -- hermetic, exact ground truth, no 14MB
338 // transfer, and it cannot silently change underneath the bench.
339 let yuv: *u8 = sys_read_file("knowledge/staging/media/bframe_test_decoded.yuv" as *u8, box)
340 if (yuv as i64) == 0 { gw("cannot read yuv -> RED\n" as *u8); return 1 }
341 let N: i64=NW*NH; let C2: i64=(NW/2)*(NH/2); let sz: i64=N+2*C2
342 if box[0] < 12 * (N + N/2) { gw("file too small -> RED\n" as *u8); return 1 }
343 let bufs: *i64 = sys_mmap(24*8) as *i64
344 bufs[0]=sys_mmap(sz+64) as i64; bufs[1]=sys_mmap(sz+64) as i64; bufs[2]=sys_mmap(sz+64) as i64
345 bufs[3]=sys_mmap(sz+64) as i64; bufs[4]=sys_mmap(sz+64) as i64; bufs[5]=sys_mmap(sz+64) as i64
346 bufs[6]=sys_mmap(K_MAGIC_4194304) as i64; bufs[7]=sys_mmap(512) as i64; bufs[8]=sys_mmap(128) as i64
347 bufs[9]=sys_mmap(64) as i64; bufs[10]=sys_mmap(K_MAGIC_4096) as i64; bufs[11]=sys_mmap(K_MAGIC_2097152) as i64
348 bufs[12]=sys_mmap(K_MAGIC_8192) as i64; bufs[13]=sys_mmap(128) as i64
349 bufs[14]=sys_mmap(64) as i64; bufs[15]=sys_mmap(K_MAGIC_4096) as i64; bufs[16]=sys_mmap(K_MAGIC_2097152) as i64
350 bufs[17]=sys_mmap(K_MAGIC_8192) as i64; bufs[18]=sys_mmap(128) as i64
351 vc_t8_init(bufs[12] as *i64)
352 vc_t8_init(bufs[17] as *i64)
353 let oP: *i64=sys_mmap(64) as *i64; let oB: *i64=sys_mmap(64) as *i64
354 let oC: *i64=sys_mmap(64) as *i64; let oD: *i64=sys_mmap(64) as *i64
355 let qs: *i64=sys_mmap(64) as *i64
356 qs[0]=10; qs[1]=16; qs[2]=22; qs[3]=28; qs[4]=34; qs[5]=40
357 // ---- GOLDEN OUTPUT DIGESTS (2026-08-03, debt 1785773858) -------------------------------------------
358 // WHY: T1 (decode parity) compares ENCODER recon against DECODER recon -- both produced by the SAME
359 // binary -- so any mutation that shifts encoder and decoder SYMMETRICALLY leaves them agreeing and the
360 // tooth cannot fail. MEASURED: nx_gate_bite killed 0 of 7 valid mutants, INCLUDING one landed inside
361 // vv_dec_rct8, i.e. live decode code. A ROUND-TRIP EQUIVALENCE GATE CANNOT KILL A SYMMETRIC MUTANT --
362 // self-consistency is not correctness. The sibling nx_vcodec_band_gate had the identical shape (0/15)
363 // and reached killed=1 by exactly this remedy, so this is an adopted recipe, not a new invention.
364 // PROVENANCE -- these were CAPTURED, NEVER CHOSEN: they are the byte counts printed by the 2026-08-03
365 // post-fix sweep, which reported par=0 AND was byte-identical to the independently-built pre-fix GREEN
366 // artifact across all 6 qp. A GOLDEN THAT WAS INVENTED IS A FABRICATION; two independent binaries
367 // agreeing on every figure is what makes these safe to pin.
368 // ⚠RE-PINNING IS DELIBERATE: a real codec improvement SHOULD turn this RED. Re-capture from a fresh
369 // par=0 run and update these lines in the same commit as the improvement -- never relax the tooth.
370 let gP: *i64=sys_mmap(64) as *i64
371 gP[0]=263220; gP[1]=213376; gP[2]=161376; gP[3]=149834; gP[4]=139145; gP[5]=126722
372 let gS: *i64=sys_mmap(64) as *i64
373 gS[0]=227307; gS[1]=184439; gS[2]=132292; gS[3]=121376; gS[4]=113394; gS[5]=101419
374 var gmisP: i64=0
375 var gmisS: i64=0
376 // per-arm curves for the matched-PSNR verdict: [psnr cdb, bytes] x 6
377 let pP: *i64=sys_mmap(64) as *i64; let bPp: *i64=sys_mmap(64) as *i64
378 let pB1: *i64=sys_mmap(64) as *i64; let bB1: *i64=sys_mmap(64) as *i64
379 let pB3: *i64=sys_mmap(64) as *i64; let bB3: *i64=sys_mmap(64) as *i64
380 let pB3q: *i64=sys_mmap(64) as *i64; let bB3q: *i64=sys_mmap(64) as *i64
381 let pL: *i64=sys_mmap(64) as *i64; let bL: *i64=sys_mmap(64) as *i64
382 let pS: *i64=sys_mmap(64) as *i64; let bS: *i64=sys_mmap(64) as *i64
383 let pS2: *i64=sys_mmap(64) as *i64; let bS2: *i64=sys_mmap(64) as *i64
384 let pS3: *i64=sys_mmap(64) as *i64; let bS3: *i64=sys_mmap(64) as *i64
385 let oL: *i64=sys_mmap(64) as *i64; let oS: *i64=sys_mmap(64) as *i64
386 let oS2: *i64=sys_mmap(64) as *i64; let oS3: *i64=sys_mmap(64) as *i64
387 var pfailT: i64=0
388 var qi: i64=0
389 while qi<6 {
390 let qp: i64 = qs[qi]
391 if arm_p(yuv, qp, bufs, oP) != 0 { gw("ARM-P enc fail -> RED\n" as *u8); return 1 }
392 if arm_b(yuv, qp, bufs, oB) != 0 { gw("ARM-B enc fail -> RED\n" as *u8); return 1 }
393 if arm_b3(yuv, qp, 0, bufs, oC) != 0 { gw("ARM-B3 enc fail -> RED\n" as *u8); return 1 }
394 if arm_b3(yuv, qp, 3, bufs, oD) != 0 { gw("ARM-B3q enc fail -> RED\n" as *u8); return 1 }
395 if arm_ltr(yuv, qp, bufs, oL) != 0 { gw("ARM-LTR enc fail -> RED\n" as *u8); return 1 }
396 if arm_lean_t(yuv, qp, 188, bufs, oS) != 0 { gw("ARM-LEAN enc fail -> RED\n" as *u8); return 1 }
397 if arm_lean_t(yuv, qp, 94, bufs, oS2) != 0 { gw("ARM-LEAN94 enc fail -> RED\n" as *u8); return 1 }
398 if arm_lean_t(yuv, qp, 47, bufs, oS3) != 0 { gw("ARM-LEAN47 enc fail -> RED\n" as *u8); return 1 }
399 pfailT = pfailT + oP[2] + oB[2] + oC[2] + oD[2] + oL[2] + oS[2] + oS2[2] + oS3[2]
400 // OUTPUT-BINDING: the emitted byte count is a function of the WHOLE encode path, so a symmetric
401 // wire-layer mutation that T1 cannot see still moves this number and dies here.
402 if oP[0] != gP[qi] { gmisP = gmisP + 1 }
403 if oS[0] != gS[qi] { gmisS = gmisS + 1 }
404 pP[qi]=oP[1]; bPp[qi]=oP[0]
405 pB1[qi]=oB[1]; bB1[qi]=oB[0]
406 pB3[qi]=oC[1]; bB3[qi]=oC[0]
407 pB3q[qi]=oD[1]; bB3q[qi]=oD[0]
408 pL[qi]=oL[1]; bL[qi]=oL[0]
409 pS[qi]=oS[1]; bS[qi]=oS[0]
410 pS2[qi]=oS2[1]; bS2[qi]=oS2[0]
411 pS3[qi]=oS3[1]; bS3[qi]=oS3[0]
412 gw("qp=" as *u8); gn(qp)
413 gw(" P:" as *u8); gn(oP[0]); gw("B@" as *u8); g2(oP[1])
414 gw(" LEAN188:" as *u8); gn(oS[0]); gw("B@" as *u8); g2(oS[1])
415 gw(" LEAN94:" as *u8); gn(oS2[0]); gw("B@" as *u8); g2(oS2[1])
416 gw(" LEAN47:" as *u8); gn(oS3[0]); gw("B@" as *u8); g2(oS3[1])
417 gw(" par=" as *u8)
418 gn(oP[2]+oB[2]+oC[2]+oD[2]+oL[2]+oS[2]+oS2[2]+oS3[2]); gw("\n" as *u8)
419 qi=qi+1
420 }
421 // ---- MATCHED-PSNR VERDICT: bytes of each arm vs P-arm bytes interpolated at the SAME PSNR ----
422 gw("-- matched-PSNR bytes vs P-chain (permille; <1000 = arm wins) --\n" as *u8)
423 var ai: i64 = 0
424 while ai < 7 {
425 var pX: *i64 = pB1; var bX: *i64 = bB1
426 if ai == 1 { pX = pB3; bX = bB3 }
427 if ai == 2 { pX = pB3q; bX = bB3q }
428 if ai == 3 { pX = pL; bX = bL }
429 if ai == 4 { pX = pS; bX = bS }
430 if ai == 5 { pX = pS2; bX = bS2 }
431 if ai == 6 { pX = pS3; bX = bS3 }
432 if ai == 0 { gw(" bf1 : " as *u8) }
433 if ai == 1 { gw(" bf3 : " as *u8) }
434 if ai == 2 { gw(" bf3+q3 : " as *u8) }
435 if ai == 3 { gw(" LTR : " as *u8) }
436 if ai == 4 { gw(" LEAN : " as *u8) }
437 if ai == 5 { gw(" LEAN94 : " as *u8) }
438 if ai == 6 { gw(" LEAN47 : " as *u8) }
439 var sum: i64 = 0; var cnt: i64 = 0
440 var j: i64 = 0
441 while j < 6 {
442 let pb: i64 = p_bytes_at(pP, bPp, 6, pX[j])
443 if pb > 0 {
444 let r: i64 = bX[j]*1000/pb
445 gn(r); gw(" " as *u8)
446 sum = sum + r; cnt = cnt + 1
447 } else { gw("--- " as *u8) }
448 j = j + 1
449 }
450 if cnt > 0 { gw(" mean=" as *u8); gn(sum/cnt) }
451 gw("\n" as *u8)
452 ai = ai + 1
453 }
454 gw("DECODE-PARITY (liar-killer): total fails=" as *u8); gn(pfailT)
455 if pfailT == 0 { gw(" PASS -- every frame of ALL arms decodes byte-identical off the wire\n" as *u8) }
456 else { gw(" FAIL -- bitstream lies, fix before believing ANY number above\n" as *u8) }
457 if pfailT == 0 { gw("BGOP-BENCH GREEN (numbers honest to the wire; paste winning curve into extjudge vs x264 -bf)\n" as *u8) }
458 let ctr: *i64 = gv_ctr()
459 var t1: i64 = 0
460 if pfailT == 0 { t1 = 1 }
461 gv_check("T1 decode-parity liar-killer: zero fails across all arms x all qp" as *u8, t1, ctr)
462 gw("GOLDEN bytes: P mismatches=" as *u8); gn(gmisP); gw(" LEAN188 mismatches=" as *u8); gn(gmisS); gw("\n" as *u8)
463 var t2: i64 = 0
464 if gmisP == 0 { if gmisS == 0 { t2 = 1 } }
465 gv_check("T2 golden output digests: P + LEAN188 emitted bytes match the pinned 6-qp curve (kills the SYMMETRIC mutant T1 is blind to)" as *u8, t2, ctr)
466 return gv_verdict("BGOP-BENCH" as *u8, ctr, "matched-PSNR sweep, wire parity, AND output bytes bound to captured goldens" as *u8) }