code wiki / _hdl_build / nx_vcodec_speed_bench.nx
nx_vcodec_speed_bench.nx source
↩ module page · 220 lines · 10415 B
1// nx_vcodec_speed_bench.nx -- the GO/NO-GO number for wiring the sovereign video codec into the live
2// room (operator: "way more than 20fps"; the measured edge fan-out ceiling is ~1.1MB/s, so 60fps x 7
3// receivers needs SMALL frames = this codec's P-frames). Encodes a keyframe + 30 P-frames of talking-
4// head-like content at call resolution 320x224, timing ENCODE and DECODE per frame, PLUS RGB-PSNR of the
5// decoded output vs the raw source (speed claims must carry their quality cost -- no false positives).
6// TWO PASSES measure the QP-DERIVED SKIP THRESHOLD trade discovered by the 2026-07-03 stage profile
7// (280/280 blocks failed skip at thresh=64: quantization noise on a STATIC block is ~QP*avg-err*256 SAD,
8// so the tight threshold ran full ME+qpel+DCT on every block every frame):
9// PASS A: sad_thresh=64 (the old always-code baseline)
10// PASS B: sad_thresh=QP*128 (QP-derived: tolerates quant noise, catches real motion) -- encoder-side
11// ONLY (the skip flag already exists in the bitstream; decoders are agnostic).
12// Ledger-reported (return-and-report). expect_exit: 0 = measurement sound. license_tier: ORIGINAL
13import "nx_syscalls.nx"
14import "nx_vcodec_color_codec.nx"
15import "nx_quality_metric.nx"
16import "nx_metric_ledger.nx"
17const VB_MAGIC_7919: i64 = 7919
18const VB_MAGIC_1103515245: i64 = 1103515245
19const VB_MAGIC_12345: i64 = 12345
20const VB_MAGIC_4096: i64 = 4096
21const VB_MAGIC_1000000: i64 = 1000000
22const VB_MAGIC_2500: i64 = 2500
23
24const VB_W: i64 = 320
25const VB_H: i64 = 224
26const VB_QP: i64 = 32
27const VB_PFRAMES: i64 = 30
28
29func vw2(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
30func vn2(v: i64) -> i64 {
31 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
32 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}
33 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
34
35// deterministic textured background (block noise -- honest intra work, P-skippable)
36func vb_background(rgb: *u8, W: i64, H: i64) -> i64 {
37 var y: i64 = 0
38 while y < H {
39 var x: i64 = 0
40 while x < W {
41 let bx: i64 = x / 16
42 let by: i64 = y / 16
43 let v: i64 = ((bx * 37 + by * 91 + (x & 3) * 5 + (y & 3) * 11) & 0x3f) + 96
44 let o: i64 = (y * W + x) * 3
45 rgb[o] = v as u8
46 rgb[o+1] = ((v * 3 / 4) + 20) as u8
47 rgb[o+2] = ((v / 2) + 40) as u8
48 x = x + 1
49 }
50 y = y + 1
51 }
52 return 0
53}
54// TEMPORAL SENSOR NOISE (+-amp per pixel, re-seeded per frame): real webcams dither every frame; a bench
55// with bit-identical statics inflates always-coding (residual re-coding "converges" on a noiseless still,
56// an advantage no camera provides -- it CHASES noise on real content). Measured honesty requires it.
57func vb_noise(rgb: *u8, W: i64, H: i64, fi: i64, amp: i64) -> i64 {
58 var s: i64 = 0x5EED + fi * VB_MAGIC_7919
59 let n3: i64 = W * H * 3
60 var i: i64 = 0
61 while i < n3 {
62 s = (s * VB_MAGIC_1103515245 + VB_MAGIC_12345) & 0x7fffffff
63 var v: i64 = (rgb[i] as i64) + ((s >> 9) % (2 * amp + 1)) - amp
64 if v < 0 { v = 0 }
65 if v > 255 { v = 255 }
66 rgb[i] = v as u8
67 i = i + 1
68 }
69 return 0
70}
71// the "face": a 96x96 region whose position drifts + interior changes each frame (talking-head motion)
72func vb_face(rgb: *u8, W: i64, H: i64, fi: i64) -> i64 {
73 let fx: i64 = 112 + (fi % 8)
74 let fy: i64 = 64 + ((fi / 2) % 6)
75 var y: i64 = 0
76 while y < 96 {
77 var x: i64 = 0
78 while x < 96 {
79 let o: i64 = ((fy + y) * W + fx + x) * 3
80 let v: i64 = 150 + (((x + fi * 7) * (y + 3)) & 0x2f)
81 rgb[o] = (v + 40) as u8
82 rgb[o+1] = v as u8
83 rgb[o+2] = (v * 2 / 3) as u8
84 x = x + 1
85 }
86 y = y + 1
87 }
88 return 0
89}
90
91// one full pass at sad_thresh: keyframe + VB_PFRAMES P-frames through the closed encode->decode loop.
92// res[0]=p_enc_us res[1]=p_dec_us res[2]=p_bytes_avg res[3]=p_bytes_max res[4]=key_bytes
93// res[5]=psnr_cdb_avg (decoded RGB vs raw RGB across P-frames) res[6]=key_enc_us res[7]=key_dec_us
94func bench_pass(sad_thresh: i64, res: *i64) -> i64 {
95 let W: i64 = VB_W
96 let H: i64 = VB_H
97 let N: i64 = W * H
98 let CN: i64 = (W/2) * (H/2)
99 let rgb: *u8 = sys_mmap(N * 3 + 64)
100 let rgbout: *u8 = sys_mmap(N * 3 + 64)
101 let strY: *u8 = sys_mmap(N + VB_MAGIC_4096)
102 let strCb: *u8 = sys_mmap(N + VB_MAGIC_4096)
103 let strCr: *u8 = sys_mmap(N + VB_MAGIC_4096)
104 let bits: *i64 = sys_mmap(64) as *i64
105 let prevY: *u8 = sys_mmap(N + 64)
106 let prevCbs: *u8 = sys_mmap(CN + 64)
107 let prevCrs: *u8 = sys_mmap(CN + 64)
108 let decY: *u8 = sys_mmap(N + 64)
109 let decCbs: *u8 = sys_mmap(CN + 64)
110 let decCrs: *u8 = sys_mmap(CN + 64)
111
112 // keyframe
113 vb_background(rgb, W, H)
114 vb_face(rgb, W, H, 0)
115 vb_noise(rgb, W, H, 0, 2)
116 let tk0: i64 = sys_now_us()
117 let kbits: i64 = vc_color_encode_p(rgb, W, H, VB_QP, 1, sad_thresh, prevY, prevCbs, prevCrs, strY, strCb, strCr, bits)
118 let tk1: i64 = sys_now_us()
119 vc_color_decode_p(W, H, VB_QP, prevY, prevCbs, prevCrs, strY, strCb, strCr, rgbout, decY, decCbs, decCrs)
120 let tk2: i64 = sys_now_us()
121 res[4] = (kbits + 7) / 8
122 res[6] = tk1 - tk0
123 res[7] = tk2 - tk1
124 var ci: i64 = 0
125 while ci < N { prevY[ci] = decY[ci]; ci = ci + 1 }
126 ci = 0
127 while ci < CN { prevCbs[ci] = decCbs[ci]; prevCrs[ci] = decCrs[ci]; ci = ci + 1 }
128
129 // P-frames
130 var enc_us_sum: i64 = 0
131 var dec_us_sum: i64 = 0
132 var pbytes_sum: i64 = 0
133 var pbytes_max: i64 = 0
134 var psnr_sum: i64 = 0
135 var fi: i64 = 1
136 while fi <= VB_PFRAMES {
137 vb_background(rgb, W, H)
138 vb_face(rgb, W, H, fi)
139 vb_noise(rgb, W, H, fi, 2)
140 let t0: i64 = sys_now_us()
141 let pbits: i64 = vc_color_encode_p(rgb, W, H, VB_QP, 0, sad_thresh, prevY, prevCbs, prevCrs, strY, strCb, strCr, bits)
142 let t1: i64 = sys_now_us()
143 vc_color_decode_p(W, H, VB_QP, prevY, prevCbs, prevCrs, strY, strCb, strCr, rgbout, decY, decCbs, decCrs)
144 let t2: i64 = sys_now_us()
145 enc_us_sum = enc_us_sum + (t1 - t0)
146 dec_us_sum = dec_us_sum + (t2 - t1)
147 let pb: i64 = (pbits + 7) / 8
148 pbytes_sum = pbytes_sum + pb
149 if pb > pbytes_max { pbytes_max = pb }
150 psnr_sum = psnr_sum + qm_psnr_cdb(rgbout, rgb, N * 3)
151 ci = 0
152 while ci < N { prevY[ci] = decY[ci]; ci = ci + 1 }
153 ci = 0
154 while ci < CN { prevCbs[ci] = decCbs[ci]; prevCrs[ci] = decCrs[ci]; ci = ci + 1 }
155 fi = fi + 1
156 }
157 res[0] = enc_us_sum / VB_PFRAMES
158 res[1] = dec_us_sum / VB_PFRAMES
159 res[2] = pbytes_sum / VB_PFRAMES
160 res[3] = pbytes_max
161 res[5] = psnr_sum / VB_PFRAMES
162 return 0
163}
164
165func pass_report(tag: *u8, r: *i64) -> i64 {
166 vw2(" " as *u8); vw2(tag)
167 vw2(": P-enc=" as *u8); vn2(r[0]); vw2("us (" as *u8)
168 var fps: i64 = 0
169 if r[0] > 0 { fps = VB_MAGIC_1000000 / r[0] }
170 vn2(fps); vw2("fps) P-dec=" as *u8); vn2(r[1]); vw2("us P-bytes=" as *u8); vn2(r[2]); vw2("B (max " as *u8); vn2(r[3])
171 vw2("B) PSNR=" as *u8); vn2(r[5] / 100); vw2("." as *u8); vn2(r[5] % 100); vw2("dB key=" as *u8); vn2(r[4]); vw2("B\n" as *u8)
172 return 0
173}
174
175func main() -> i64 {
176 vw2("=== nx_vcodec_speed_bench: 320x224 QP=32 -- skip-threshold trade MEASURED (speed AND quality) ===\n" as *u8)
177 let ra: *i64 = sys_mmap(16 * 8) as *i64
178 let r1: *i64 = sys_mmap(16 * 8) as *i64
179 let r2: *i64 = sys_mmap(16 * 8) as *i64
180 let rb: *i64 = sys_mmap(16 * 8) as *i64
181 bench_pass(64, ra) // baseline: always-code (skip never fires on recon refs)
182 bench_pass(VB_QP * 84, r1) // sweep: just above the measured quant-noise floor (~VB_MAGIC_2500)
183 bench_pass(VB_QP * 100, r2) // sweep: middle
184 bench_pass(VB_QP * 128, rb) // sweep: loose (measured 4.6x but -4.1dB = too far)
185 pass_report("A thresh=64 " as *u8, ra)
186 pass_report("S1 thresh=QPx84 " as *u8, r1)
187 pass_report("S2 thresh=QPx100" as *u8, r2)
188 pass_report("S3 thresh=QPx128" as *u8, rb)
189 // SELECT: the tightest swept threshold with dPSNR <= 1.5dB vs always-code (imperceptible-class),
190 // preferring the biggest speed win among those. Data picks, not a magic number.
191 var sel: *i64 = ra
192 var selname: i64 = 0
193 if ra[5] - r1[5] <= 150 { sel = r1; selname = 84 }
194 if ra[5] - r2[5] <= 150 { sel = r2; selname = 100 }
195 var speedx10: i64 = 0
196 if sel[0] > 0 { speedx10 = ra[0] * 10 / sel[0] }
197 let dpsnr: i64 = ra[5] - sel[5]
198 vw2(" SELECTED thresh=QPx" as *u8); vn2(selname); vw2(": " as *u8); vn2(speedx10 / 10); vw2("." as *u8); vn2(speedx10 % 10); vw2("x faster, PSNR cost " as *u8); vn2(dpsnr / 100); vw2("." as *u8); vn2(dpsnr % 100); vw2("dB, bytes " as *u8); vn2(ra[2]); vw2("->" as *u8); vn2(sel[2]); vw2("B\n" as *u8)
199 vw2(" wire: selected x 60fps x 7rx = " as *u8); vn2(sel[2] * 60 * 7 / 1000); vw2("KB/s vs ~1100KB/s measured ceiling\n" as *u8)
200
201 vw2(" -- return-and-report --\n" as *u8)
202 ml_report("vcodec_p_enc_us_320x224" as *u8, ra[0], 0 - 1, "us" as *u8, "nx_vcodec_speed_bench" as *u8)
203 ml_report("vcodec_p_dec_us_320x224" as *u8, ra[1], 0 - 1, "us" as *u8, "nx_vcodec_speed_bench" as *u8)
204 ml_report("vcodec_p_bytes_320x224_qp32" as *u8, ra[2], 0 - 1, "B" as *u8, "nx_vcodec_speed_bench" as *u8)
205 ml_report("vcodec_key_bytes_320x224_qp32" as *u8, ra[4], 0 - 1, "B" as *u8, "nx_vcodec_speed_bench" as *u8)
206 ml_report("vcodec_p_enc_us_qpskip" as *u8, sel[0], 0 - 1, "us" as *u8, "nx_vcodec_speed_bench" as *u8)
207 ml_report("vcodec_p_psnr_cdb_qpskip" as *u8, sel[5], 1, "cdB" as *u8, "nx_vcodec_speed_bench" as *u8)
208 ml_report("vcodec_p_bytes_qpskip" as *u8, sel[2], 0 - 1, "B" as *u8, "nx_vcodec_speed_bench" as *u8)
209
210 // soundness: real streams; P < key; the SELECTED threshold's PSNR cost imperceptible-class (<=1.5dB)
211 var ok: i64 = 1
212 if ra[4] <= 0 { ok = 0 }
213 if ra[2] <= 0 { ok = 0 }
214 if sel[2] <= 0 { ok = 0 }
215 if ra[2] >= ra[4] { ok = 0 }
216 if dpsnr > 150 { ok = 0 }
217 if ok == 1 { vw2("VCODEC-SPEED verdict=SOUND\n" as *u8); return 0 }
218 vw2("VCODEC-SPEED verdict=UNSOUND (empty streams, P>=key, or skip cost >3dB)\n" as *u8)
219 return 1
220}