nx_vcodec_stage_bench_recovery_20260912.nx source
↩ module page · 384 lines · 24580 B
1// nx_vcodec_stage_bench.nx -- WHERE DOES THE ENCODE TIME GO. Stage attribution of the LIVE rct8 RTC encoder, per
2// macroblock, on the REAL head-to-head test card (the akiyo index-strip card nx_video_h2h judged 2026-09-02).
3// WHY IT EXISTS: the same-instrument head-to-head measured ours at 9.9-18.2 of 30 fps against Jitsi's 29, and the
4// probes' own arming dump showed the cause is encode COST (93 ms single-thread, 48 ms banded at 640x480), not the
5// ladder (30/45/60 rungs exist) and not wasm (native 74 us per MB == browser 77 us per MB). Nothing in the estate
6// attributed that cost to a stage (nx_capsearch 2026-09-02: no encoder stage profiler), so every fix would have been
7// a guess. This organ MEASURES: (A) the whole production frame encode, (B) the integer motion search kernel called
8// once per MB exactly as the block coder calls it, (C) the quarter-pel refine kernel on the CODED MBs (and on all
9// MBs, the ceiling), and derives REST = A - B - C (transform, quant, RDOQ, entropy, recon, deblock). Kernel costs
10// are the cost of ONE call per MB, which is an UPPER BOUND on their share if the encoder early-outs before them.
11// Historical baseline: emode 2593 = range coder + sig-map + gentle deblock + RD-skip.
12// Build 865 additionally enables quiet-quadrant skip (emode 18977); both arms remain in this benchmark.
13// The live skip threshold is qp*30; qp*188 is the separate RD-benchmark control. Two qp points bracket
14// the ladder. Prints one line per stage per qp; last line is the verdict for gv_last_line readers.
15// Card: 352x288, first 120 frames of the h2h card (banked in _ops/video_h2h/testcard120.y4m). license_tier: ORIGINAL
16import "nx_syscalls.nx"
17import "nx_video_codec_wasm.nx"
18import "nx_quality_metric.nx"
19
20const SB_W: i64 = 352
21const SB_H: i64 = 288
22const SB_NF: i64 = 120
23const SB_T: i64 = 16
24const SB_QP_A: i64 = 24
25const SB_QP_B: i64 = 32
26const SB_THRESH_LIVE: i64 = 30 // the LIVE worker's skip threshold: app.v2.js passes B(m.qp * 30) to vv_enc_rct8
27const SB_THRESH_RDBENCH: i64 = 188 // the rd bench's value (qp*188 = the measured real-translation band) -- the OTHER convention
28const SB_NOISE_MAX: i64 = 2 // sensor / colour-conversion noise emulation: +-0, +-1, +-2 LSB per luma pixel
29const SB_LCG_A: i64 = 1103515245
30const SB_LCG_C: i64 = 12345
31const SB_LCG_MASK: i64 = 2147483647
32const SB_EMODE_RTC: i64 = 2593 // 1 | 32 | 512 | 2048 -- rc + sig + deblock + rdskip (vv_seed_rctx, RTC room)
33const SB_WIRE_CAP: i64 = 4194304
34const SB_RCBUF: i64 = 2097152
35const SB_T8C_BYTES: i64 = 5120
36const SB_MAXFRAMES: i64 = 1024
37const SB_NS_PER_US: i64 = 1000
38
39func sw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
40func sn(v: i64) -> i64 {
41 let b: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
42 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 }
43 var i: i64 = 0; while i < k { b[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, b, k); return 0 }
44const SB_EMODE_NORDSKIP: i64 = 545 // 2593 - 2048: the RD-skip band trial OFF (rc + sig + deblock)
45const SB_EMODE_NOSIG: i64 = 513 // 545 - 32: sig-map coefficient coder OFF (rc + deblock)
46const SB_EMODE_RCONLY: i64 = 1 // range coder only: no sig-map, no deblock, no RD-skip
47const SB_EMODE_QQ: i64 = 18977 // 2593 | 16384: the live RTC mode plus the quiet-quadrant early skip (VC_EMODE_QQSKIP)
48const SB_EMODE_QQ_HEX: i64 = 51745 // 18977 | 32768: plus the hexagon motion search (VC_EMODE_HEXME)
49func seedctx(rctx: *i64, est: *i64, probs: *i64, rcbuf: *u8, t8c: *i64, emode: i64) -> i64 {
50 rctx[0] = emode; rctx[1] = est as i64; rctx[2] = probs as i64; rctx[3] = rcbuf as i64; rctx[4] = t8c as i64
51 rctx[5] = 0; rctx[6] = 0; rctx[7] = 0; rctx[8] = 0; rctx[9] = 0; return 0 }
52func cpb(d: *u8, s: *u8, n: i64) -> i64 { var i: i64 = 0; while i < n { d[i] = s[i]; i = i + 1 } return 0 }
53const SB_RC_P_HALF: i64 = 2048
54// zero-motion residual of one 4x4 at (sx,sy): blk[j*4+i] = cur - prev, exactly what the code arm codes at MV (0,0)
55func sb_fill_res(blk: *i64, cur: *u8, prev: *u8, W: i64, sx: i64, sy: i64) -> i64 {
56 var j: i64 = 0
57 while j < 4 { var i: i64 = 0
58 while i < 4 { blk[j * 4 + i] = (cur[(sy + j) * W + sx + i] as i64) - (prev[(sy + j) * W + sx + i] as i64); i = i + 1 } j = j + 1 }
59 return 0 }
60
61// copy a frame into scratch and add +-amp LSB deterministic noise to the LUMA plane (what a camera pipeline and a
62// YUV->RGB->YUV round trip do to "static" pixels). amp=0 = the pristine card. Returns the advanced PRNG state.
63func sb_noisy(dst: *u8, src: *u8, sz: i64, n_luma: i64, amp: i64, seed: i64) -> i64 {
64 cpb(dst, src, sz)
65 if amp <= 0 { return seed }
66 var s: i64 = seed
67 let span: i64 = 2 * amp + 1
68 var i: i64 = 0
69 while i < n_luma {
70 s = (s * SB_LCG_A + SB_LCG_C) & SB_LCG_MASK
71 var v: i64 = (dst[i] as i64) + ((s >> 16) % span) - amp
72 if v < 0 { v = 0 }
73 if v > 255 { v = 255 }
74 dst[i] = v as u8
75 i = i + 1
76 }
77 return s
78}
79
80// write a decimal to an fd (pts rows), then a separator char
81func sb_wnfd(fd: i64, v: i64, sep: i64) -> i64 {
82 let b: *u8 = sys_mmap(32); var m: i64 = v; var k: i64 = 0
83 let t: *u8 = sys_mmap(32)
84 if m < 0 { m = 0 - m }
85 if m == 0 { t[0] = 48 as u8; k = 1 }
86 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
87 var i: i64 = 0; while i < k { b[i] = t[k - 1 - i]; i = i + 1 }
88 b[k] = sep as u8
89 sys_write(fd, b, k + 1)
90 return 0 }
91// PSNR in centi-dB from a TOTAL sse over n pixels (the sweep convention: one PSNR per curve point over all frames),
92// the same Q16 log2 path as qm_psnr_cdb_exact so the two agree to the log2 approximation.
93func sb_psnr_cdb_sse(s: i64, n: i64) -> i64 {
94 if n <= 0 { return 0 }
95 if s <= 0 { return QM_MAGIC_9900 }
96 let diff: i64 = qm_log2_q16(QM_MAGIC_65025 * n) - qm_log2_q16(s)
97 if diff <= 0 { return 0 }
98 return diff * 1000 / QM_MAGIC_217707 }
99// luma SSE between two planes
100func sb_sse(a: *u8, b: *u8, n: i64) -> i64 {
101 var s: i64 = 0; var i: i64 = 0
102 while i < n { let d: i64 = (a[i] as i64) - (b[i] as i64); s = s + d * d; i = i + 1 }
103 return s }
104// parse a 352x288 4:2:0 y4m into frame pointers (up to maxf); returns the count, 0 on failure
105func sb_parse_y4m(raw: *u8, total: i64, fp: *i64, maxf: i64) -> i64 {
106 var hi: i64 = 0
107 while hi < total { if (raw[hi] & 0xff) == 10 { break } hi = hi + 1 }
108 if hi >= total { return 0 }
109 hi = hi + 1
110 let N: i64 = SB_W * SB_H; let C2: i64 = (SB_W / 2) * (SB_H / 2); let fdata: i64 = N + 2 * C2
111 var nf: i64 = 0; var p: i64 = hi
112 while nf < maxf {
113 if p >= total { break }
114 var q: i64 = p
115 while q < total { if (raw[q] & 0xff) == 10 { break } q = q + 1 }
116 q = q + 1
117 if q + fdata > total { break }
118 fp[nf] = (raw as i64) + q; nf = nf + 1; p = q + fdata
119 }
120 return nf }
121const SB_BD_NF: i64 = 48 // the sweep clock: 48 frames at 30 fps -> kbps_milli = total_bytes*5 (nx_bd_calc contract)
122const SB_BD_KBPS_MUL: i64 = 5
123const SB_BD_NQ: i64 = 5
124// BD SWEEP VERB: `bd <seq.y4m> <emode> <out.pts>` -- the LIVE chain (vv_enc_rct8 keys and P, skip threshold qp*30, the
125// given emode) over 48 frames at q in {8,16,24,32,44}; prints one OURS line per q and appends "<kbps_milli> <psnr_milli>"
126// to the pts file for nx_bd_calc. PSNR is over the TOTAL luma SSE of all 48 recon frames (the ours13 convention).
127func sb_bd(fp: *i64, nf: i64, emode: i64, bufs: *i64, ptsfd: i64) -> i64 {
128 let W: i64 = SB_W; let H: i64 = SB_H
129 let N: i64 = W * H; let C: i64 = (W / 2) * (H / 2); let sz: i64 = N + 2 * C
130 let prevE: *u8 = bufs[0] as *u8; let reconE: *u8 = bufs[1] as *u8
131 let wire: *u8 = bufs[2] as *u8; let blk: *i64 = bufs[3] as *i64; let mv: *i64 = bufs[4] as *i64
132 let est: *i64 = bufs[5] as *i64; let probs: *i64 = bufs[6] as *i64; let rcbuf: *u8 = bufs[7] as *u8
133 let t8c: *i64 = bufs[8] as *i64; let rctx: *i64 = bufs[9] as *i64
134 let qs: *i64 = sys_mmap(64) as *i64
135 qs[0] = 8; qs[1] = 16; qs[2] = 24; qs[3] = 32; qs[4] = 44
136 var qi: i64 = 0
137 while qi < SB_BD_NQ {
138 let qp: i64 = qs[qi]
139 let thresh: i64 = qp * SB_THRESH_LIVE
140 var z: i64 = 0; while z < sz { prevE[z] = 0 as u8; z = z + 1 }
141 var total: i64 = 0; var sse: i64 = 0; var keyb: i64 = 0
142 let t0: i64 = sys_now_us()
143 var f: i64 = 0
144 while f < nf {
145 let cur: *u8 = fp[f] as *u8
146 var key: i64 = 0; if f == 0 { key = 1 }
147 seedctx(rctx, est, probs, rcbuf, t8c, emode)
148 let nb: i64 = vv_enc_rct8(cur, prevE, reconE, W, H, qp, key, thresh, wire, SB_WIRE_CAP, blk, mv, rctx)
149 if nb > 0 { total = total + nb }
150 if key == 1 { keyb = nb }
151 sse = sse + sb_sse(reconE, cur, N)
152 cpb(prevE, reconE, sz)
153 f = f + 1
154 }
155 let t1: i64 = sys_now_us()
156 let cdb: i64 = sb_psnr_cdb_sse(sse, N * nf)
157 sw("OURS-LIVE q=" as *u8); sn(qp); sw(" emode=" as *u8); sn(emode); sw(" frames=" as *u8); sn(nf)
158 sw(" total=" as *u8); sn(total); sw(" key=" as *u8); sn(keyb); sw(" sseY=" as *u8); sn(sse); sw(" npix=" as *u8); sn(N * nf)
159 sw(" psnr_cdb=" as *u8); sn(cdb); sw(" enc_us_per_frame=" as *u8); sn((t1 - t0) / nf); sw("\n" as *u8)
160 if ptsfd >= 0 { sb_wnfd(ptsfd, total * SB_BD_KBPS_MUL, 32); sb_wnfd(ptsfd, cdb * 10, 10) }
161 qi = qi + 1
162 }
163 return 0 }
164
165// one (qp, threshold multiplier, noise amplitude) point over the parsed frames: prints the stage lines, returns 0
166func sb_run(fp: *i64, nf: i64, qp: i64, tmult: i64, amp: i64, emode: i64, bufs: *i64) -> i64 {
167 let W: i64 = SB_W; let H: i64 = SB_H
168 let N: i64 = W * H; let C: i64 = (W / 2) * (H / 2); let sz: i64 = N + 2 * C
169 let BW: i64 = W / SB_T; let BH: i64 = H / SB_T; let nmb: i64 = BW * BH
170 let prevE: *u8 = bufs[0] as *u8; let reconE: *u8 = bufs[1] as *u8
171 let wire: *u8 = bufs[2] as *u8; let blk: *i64 = bufs[3] as *i64; let mv: *i64 = bufs[4] as *i64
172 let est: *i64 = bufs[5] as *i64; let probs: *i64 = bufs[6] as *i64; let rcbuf: *u8 = bufs[7] as *u8
173 let t8c: *i64 = bufs[8] as *i64; let rctx: *i64 = bufs[9] as *i64
174 let scr3: *i64 = bufs[10] as *i64; let mvs: *i64 = bufs[11] as *i64; let mvq: *i64 = bufs[12] as *i64
175 let curS: *u8 = bufs[13] as *u8
176 let est2: *i64 = bufs[14] as *i64; let probs2: *i64 = bufs[15] as *i64; let rcbuf2: *u8 = bufs[16] as *u8
177 let sigmap2: *i64 = bufs[17] as *i64; let rblk: *i64 = bufs[18] as *i64
178 let thresh: i64 = qp * tmult
179 var seed: i64 = 7919
180 var z: i64 = 0; while z < sz { prevE[z] = 0 as u8; z = z + 1 }
181 // keyframe
182 seed = sb_noisy(curS, fp[0] as *u8, sz, N, amp, seed)
183 seedctx(rctx, est, probs, rcbuf, t8c, emode)
184 let k0: i64 = sys_now_us()
185 let kb: i64 = vv_enc_rct8(curS, prevE, reconE, W, H, qp, 1, thresh, wire, SB_WIRE_CAP, blk, mv, rctx)
186 let k1: i64 = sys_now_us()
187 cpb(prevE, reconE, sz)
188 var tot_us: i64 = 0; var me_us: i64 = 0; var qc_us: i64 = 0; var qa_us: i64 = 0
189 var bytes: i64 = 0; var coded: i64 = 0; var pf: i64 = 0
190 var dig: i64 = 5381 // djb2 over every P-frame's wire bytes: two encoder builds that print the same digest emitted the same stream
191 var pe1: i64 = 0; var pe13: i64 = 0; var pe2: i64 = 0; var pemb: i64 = 0
192 var psnr_sum: i64 = 0
193 var band_lo: i64 = 0; var band_hi: i64 = 0 // RD-skip trial band [thr/4, thr) and [thr, 8*thr] populations (summed over P-frames)
194 var f: i64 = 1
195 while f < nf {
196 seed = sb_noisy(curS, fp[f] as *u8, sz, N, amp, seed)
197 let cur: *u8 = curS
198 // (A) the production frame encode
199 seedctx(rctx, est, probs, rcbuf, t8c, emode)
200 let t0: i64 = sys_now_us()
201 let nb: i64 = vv_enc_rct8(cur, prevE, reconE, W, H, qp, 0, thresh, wire, SB_WIRE_CAP, blk, mv, rctx)
202 let t1: i64 = sys_now_us()
203 psnr_sum = psnr_sum + qm_psnr_cdb_exact(reconE, cur, N) // recon vs the (noisy) input the encoder saw, luma
204 if nb > 0 { bytes = bytes + nb; var di: i64 = 0; while di < nb { dig = (dig * 33 + (wire[di] as i64 & 0xff)) & SB_LCG_MASK; di = di + 1 } }
205 // (B) integer search kernel, once per MB, as the block coder calls it (T=16, R=VC_ME_R, qp-scaled floor)
206 let t2: i64 = sys_now_us()
207 var by: i64 = 0
208 while by < BH { var bx: i64 = 0
209 while bx < BW {
210 let sad: i64 = vm_search_q(cur, prevE, W, H, bx, by, SB_T, VC_ME_R, mv, qp, scr3)
211 let mi: i64 = by * BW + bx
212 mvs[mi * 3] = mv[0]; mvs[mi * 3 + 1] = mv[1]; mvs[mi * 3 + 2] = sad
213 if sad >= thresh { coded = coded + 1 }
214 let zs0: i64 = vm_sad_zero(cur, prevE, W, bx, by, SB_T)
215 if zs0 >= thresh / 4 { if zs0 < thresh { band_lo = band_lo + 1 } }
216 if zs0 >= thresh { if zs0 <= thresh * 8 { band_hi = band_hi + 1 } }
217 bx = bx + 1 } by = by + 1 }
218 let t3: i64 = sys_now_us()
219 // (C) quarter-pel refine on the CODED MBs (what the block coder pays) ...
220 var by2: i64 = 0
221 while by2 < BH { var bx2: i64 = 0
222 while bx2 < BW {
223 let mi2: i64 = by2 * BW + bx2
224 if mvs[mi2 * 3 + 2] >= thresh {
225 qp_refine(cur, prevE, W, H, bx2 * SB_T, by2 * SB_T, mvs[mi2 * 3], mvs[mi2 * 3 + 1], SB_T, mvq)
226 }
227 bx2 = bx2 + 1 } by2 = by2 + 1 }
228 let t4: i64 = sys_now_us()
229 // ... and on ALL MBs (the ceiling if skip did not spare any)
230 var by3: i64 = 0
231 while by3 < BH { var bx3: i64 = 0
232 while bx3 < BW {
233 let mi3: i64 = by3 * BW + bx3
234 qp_refine(cur, prevE, W, H, bx3 * SB_T, by3 * SB_T, mvs[mi3 * 3], mvs[mi3 * 3 + 1], SB_T, mvq)
235 bx3 = bx3 + 1 } by3 = by3 + 1 }
236 let t5: i64 = sys_now_us()
237 // (E) the 4x4 residual pipeline the code arm runs 16x per coded MB, timed per MB (one clock pair per region per MB)
238 // on the CODED MBs' zero-motion residual: E1 = 16x(fill + vt2_fwd + vt2_quant_rdoq); E13 = E1 + 16x(vt2_dequant +
239 // vt2_inv); E2 = 16x(fill + vc_enc_sub_sig) = the whole sub-block coder incl. the range-coded sig-map entropy,
240 // with a fresh estimator per frame. Same functions, same qp, same tf=1 as the RTC path. entropy ~= E2 - E13.
241 rc_enc_init(est2)
242 var ci2: i64 = 0; while ci2 < RC_NCTX8 { probs2[ci2] = SB_RC_P_HALF; ci2 = ci2 + 1 }
243 var e1: i64 = 0; var e13: i64 = 0; var e2: i64 = 0; var emb: i64 = 0
244 var by4: i64 = 0
245 while by4 < BH { var bx4: i64 = 0
246 while bx4 < BW {
247 let mi4: i64 = by4 * BW + bx4
248 if mvs[mi4 * 3 + 2] >= thresh {
249 emb = emb + 1
250 let cx4: i64 = bx4 * SB_T; let cy4: i64 = by4 * SB_T
251 let u0: i64 = sys_now_us()
252 var k1: i64 = 0
253 while k1 < 16 { sb_fill_res(rblk, cur, prevE, W, cx4 + (k1 % 4) * 4, cy4 + (k1 / 4) * 4); vt2_fwd(rblk); vt2_quant_rdoq(rblk, qp); k1 = k1 + 1 }
254 let u1: i64 = sys_now_us()
255 var k2: i64 = 0
256 while k2 < 16 { sb_fill_res(rblk, cur, prevE, W, cx4 + (k2 % 4) * 4, cy4 + (k2 / 4) * 4); vt2_fwd(rblk); vt2_quant_rdoq(rblk, qp); vt2_dequant(rblk, qp); vt2_inv(rblk); k2 = k2 + 1 }
257 let u2: i64 = sys_now_us()
258 var k3: i64 = 0
259 while k3 < 16 { sb_fill_res(rblk, cur, prevE, W, cx4 + (k3 % 4) * 4, cy4 + (k3 / 4) * 4); vc_enc_sub_sig(rblk, qp, est2, rcbuf2, probs2, 1, 16, 4, sigmap2); k3 = k3 + 1 }
260 let u3: i64 = sys_now_us()
261 e1 = e1 + (u1 - u0); e13 = e13 + (u2 - u1); e2 = e2 + (u3 - u2)
262 }
263 bx4 = bx4 + 1 } by4 = by4 + 1 }
264 pe1 = pe1 + e1; pe13 = pe13 + e13; pe2 = pe2 + e2; pemb = pemb + emb
265 tot_us = tot_us + (t1 - t0); me_us = me_us + (t3 - t2); qc_us = qc_us + (t4 - t3); qa_us = qa_us + (t5 - t4)
266 pf = pf + 1
267 cpb(prevE, reconE, sz)
268 f = f + 1
269 }
270 if pf <= 0 { sw(" no P-frames measured\n" as *u8); return 1 }
271 let a_us: i64 = tot_us / pf; let b_us: i64 = me_us / pf; let c_us: i64 = qc_us / pf; let ca_us: i64 = qa_us / pf
272 var rest_us: i64 = a_us - b_us - c_us; if rest_us < 0 { rest_us = 0 }
273 sw("STAGE qp=" as *u8); sn(qp); sw(" thresh_mult=" as *u8); sn(tmult); sw(" noise_lsb=" as *u8); sn(amp); sw(" emode=" as *u8); sn(emode)
274 sw(" frames=" as *u8); sn(pf); sw(" mbs_per_frame=" as *u8); sn(nmb)
275 sw(" coded_mbs_avg=" as *u8); sn(coded / pf); sw(" rdskip_band_lo_avg=" as *u8); sn(band_lo / pf); sw(" rdskip_band_hi_avg=" as *u8); sn(band_hi / pf); sw(" key_us=" as *u8); sn(k1 - k0); sw(" key_bytes=" as *u8); sn(kb)
276 sw(" p_bytes_avg=" as *u8); sn(bytes / pf); sw(" psnr_cdb_avg=" as *u8); sn(psnr_sum / pf); sw(" wire_digest=" as *u8); sn(dig); sw("\n" as *u8)
277 sw(" A total_encode_us=" as *u8); sn(a_us); sw(" ns_per_mb=" as *u8); sn(a_us * SB_NS_PER_US / nmb); sw("\n" as *u8)
278 sw(" B int_search_us=" as *u8); sn(b_us); sw(" ns_per_mb=" as *u8); sn(b_us * SB_NS_PER_US / nmb)
279 sw(" share_permil=" as *u8); if a_us > 0 { sn(b_us * 1000 / a_us) } else { sn(0) } sw("\n" as *u8)
280 sw(" C qpel_refine_coded_us=" as *u8); sn(c_us); sw(" share_permil=" as *u8); if a_us > 0 { sn(c_us * 1000 / a_us) } else { sn(0) }
281 sw(" qpel_refine_all_us=" as *u8); sn(ca_us); sw(" ns_per_mb_all=" as *u8); sn(ca_us * SB_NS_PER_US / nmb); sw("\n" as *u8)
282 if pemb > 0 {
283 sw(" E per_coded_mb_ns: fwd_rdoq=" as *u8); sn(pe1 * SB_NS_PER_US / pemb); sw(" fwd_rdoq_deq_inv=" as *u8); sn(pe13 * SB_NS_PER_US / pemb)
284 sw(" whole_sub_sig=" as *u8); sn(pe2 * SB_NS_PER_US / pemb); sw(" entropy_derived=" as *u8); sn((pe2 - pe13) * SB_NS_PER_US / pemb)
285 sw(" coded_mbs_timed=" as *u8); sn(pemb); sw(" (16 sub-blocks each; one clock pair per region per MB)
286" as *u8)
287 }
288 sw(" D rest_us_derived=" as *u8); sn(rest_us); sw(" share_permil=" as *u8); if a_us > 0 { sn(rest_us * 1000 / a_us) } else { sn(0) }
289 sw(" (transform+quant+rdoq+entropy+recon+deblock = A - B - C, derived not timed)\n" as *u8)
290 return 0
291}
292
293func sb_atoi(a: *u8) -> i64 { var v: i64 = 0; var i: i64 = 0; while a[i] != (0 as u8) { v = v * 10 + ((a[i] as i64 & 0xff) - 48); i = i + 1 } return v }
294func sb_is_bd(a: *u8) -> i64 { if a[0] != (98 as u8) { return 0 } if a[1] != (100 as u8) { return 0 } if a[2] != (0 as u8) { return 0 } return 1 }
295func main(argc: i64, argv: *i64) -> i64 {
296 if argc >= 5 { if sb_is_bd(argv[1] as *u8) == 1 {
297 let boxb: *i64 = sys_mmap(16) as *i64
298 let rawb: *u8 = sys_read_file(argv[2] as *u8, boxb)
299 if (rawb as i64) == 0 { sw("bd: cannot read sequence -> UNMEASURED\n" as *u8); return 3 }
300 let fpb: *i64 = sys_mmap(SB_MAXFRAMES * 8) as *i64
301 let nfb: i64 = sb_parse_y4m(rawb, boxb[0], fpb, SB_BD_NF)
302 if nfb < SB_BD_NF { sw("bd: fewer than 48 frames -> UNMEASURED\n" as *u8); return 3 }
303 let emodeb: i64 = sb_atoi(argv[3] as *u8)
304 let ptsfd: i64 = sys_openat_wr(argv[4] as *u8, 420)
305 if ptsfd < 0 { sw("bd: cannot open pts -> UNMEASURED\n" as *u8); return 3 }
306 let Nb: i64 = SB_W * SB_H; let szb: i64 = Nb + 2 * ((SB_W / 2) * (SB_H / 2))
307 let bufsb: *i64 = sys_mmap(24 * 8) as *i64
308 bufsb[0] = sys_mmap(szb + 64) as i64; bufsb[1] = sys_mmap(szb + 64) as i64
309 bufsb[2] = sys_mmap(SB_WIRE_CAP) as i64; bufsb[3] = sys_mmap(512) as i64; bufsb[4] = sys_mmap(128) as i64
310 bufsb[5] = sys_mmap(64) as i64; bufsb[6] = sys_mmap(32 * 8) as i64; bufsb[7] = sys_mmap(SB_RCBUF) as i64
311 bufsb[8] = sys_mmap(SB_T8C_BYTES) as i64; bufsb[9] = sys_mmap(128) as i64
312 vc_t8_init(bufsb[8] as *i64)
313 sb_bd(fpb, nfb, emodeb, bufsb, ptsfd)
314 sys_close(ptsfd)
315 sw("STAGE-BENCH bd verdict=MEASURED\n" as *u8)
316 return 0
317 } }
318 sw("=== nx_vcodec_stage_bench: rct8 encoder stage attribution (baseline 2593, build-865 18977, experimental 51745) ===\n" as *u8)
319 let box: *i64 = sys_mmap(16) as *i64
320 // Explicit card paths remain supported. The bootstrap is relative to the management organ runner's NAS root.
321 // Input provenance: buildroot/knowledge/fixtures/video_h2h/testcard120.provenance.json.
322 var cardp: *u8 = "buildroot/knowledge/fixtures/video_h2h/testcard120.y4m" as *u8
323 if argc >= 2 { cardp = argv[1] as *u8 } else {
324 // Qualification runs from buildroot; the management organ runner starts at its parent.
325 let cardfd: i64 = sys_openat_rd(cardp)
326 if cardfd < 0 { cardp = "knowledge/fixtures/video_h2h/testcard120.y4m" as *u8 } else { sys_close(cardfd) }
327 }
328 sw("card=" as *u8); sw(cardp); sw("\n" as *u8)
329 let raw: *u8 = sys_read_file(cardp, box)
330 if (raw as i64) == 0 { sw("cannot read the card -> UNMEASURED\nSTAGE-BENCH verdict=UNMEASURED\n" as *u8); return 3 }
331 let total: i64 = box[0]
332 var hi: i64 = 0
333 while hi < total { if (raw[hi] & 0xff) == 10 { break } hi = hi + 1 }
334 if hi >= total { sw("no y4m header -> UNMEASURED\nSTAGE-BENCH verdict=UNMEASURED\n" as *u8); return 3 }
335 hi = hi + 1
336 let N: i64 = SB_W * SB_H; let C2: i64 = (SB_W / 2) * (SB_H / 2); let fdata: i64 = N + 2 * C2
337 let fp: *i64 = sys_mmap(SB_MAXFRAMES * 8) as *i64
338 var nf: i64 = 0; var p: i64 = hi
339 while nf < SB_NF {
340 if p >= total { break }
341 var q: i64 = p
342 while q < total { if (raw[q] & 0xff) == 10 { break } q = q + 1 }
343 q = q + 1
344 if q + fdata > total { break }
345 fp[nf] = (raw as i64) + q; nf = nf + 1; p = q + fdata
346 }
347 sw("card_bytes=" as *u8); sn(total); sw(" frames_parsed=" as *u8); sn(nf); sw("\n" as *u8)
348 if nf < 2 { sw("fewer than 2 frames -> UNMEASURED\nSTAGE-BENCH verdict=UNMEASURED\n" as *u8); return 3 }
349 let sz: i64 = fdata
350 let bufs: *i64 = sys_mmap(24 * 8) as *i64
351 bufs[0] = sys_mmap(sz + 64) as i64; bufs[1] = sys_mmap(sz + 64) as i64
352 bufs[2] = sys_mmap(SB_WIRE_CAP) as i64; bufs[3] = sys_mmap(512) as i64; bufs[4] = sys_mmap(128) as i64
353 bufs[5] = sys_mmap(64) as i64; bufs[6] = sys_mmap(32 * 8) as i64; bufs[7] = sys_mmap(SB_RCBUF) as i64
354 bufs[8] = sys_mmap(SB_T8C_BYTES) as i64; bufs[9] = sys_mmap(128) as i64
355 bufs[10] = sys_mmap(64) as i64; bufs[11] = sys_mmap((SB_W / SB_T) * (SB_H / SB_T) * 3 * 8) as i64; bufs[12] = sys_mmap(32) as i64
356 bufs[13] = sys_mmap(sz + 64) as i64
357 bufs[14] = sys_mmap(64) as i64; bufs[15] = sys_mmap(32 * 8) as i64; bufs[16] = sys_mmap(SB_RCBUF) as i64
358 bufs[17] = sys_mmap(16 * 8) as i64; bufs[18] = sys_mmap(16 * 8) as i64
359 vc_t8_init(bufs[8] as *i64)
360 // the matrix: LIVE threshold (qp*30) and the rd bench's (qp*188) x pristine / +-1 / +-2 LSB luma noise, two qp points
361 var amp: i64 = 0
362 while amp <= SB_NOISE_MAX {
363 sb_run(fp, nf, SB_QP_A, SB_THRESH_LIVE, amp, SB_EMODE_RTC, bufs)
364 sb_run(fp, nf, SB_QP_A, SB_THRESH_RDBENCH, amp, SB_EMODE_RTC, bufs)
365 amp = amp + 1
366 }
367 sb_run(fp, nf, SB_QP_B, SB_THRESH_LIVE, 0, SB_EMODE_RTC, bufs)
368 sb_run(fp, nf, SB_QP_B, SB_THRESH_LIVE, SB_NOISE_MAX, SB_EMODE_RTC, bufs)
369 // FEATURE SPLIT at the live point (qp 24, thresh qp*30, +-2 LSB): the same frames with encoder-only bits removed one
370 // at a time -- RD-skip trial, then the sig-map coder, then deblock -- so the derived REST stage is attributed by
371 // feature at zero source change (emode is a runtime word in rctx[0]).
372 sb_run(fp, nf, SB_QP_A, SB_THRESH_LIVE, SB_NOISE_MAX, SB_EMODE_NORDSKIP, bufs)
373 sb_run(fp, nf, SB_QP_A, SB_THRESH_LIVE, 0, SB_EMODE_NORDSKIP, bufs)
374 sb_run(fp, nf, SB_QP_A, SB_THRESH_LIVE, SB_NOISE_MAX, SB_EMODE_NOSIG, bufs)
375 sb_run(fp, nf, SB_QP_A, SB_THRESH_LIVE, SB_NOISE_MAX, SB_EMODE_RCONLY, bufs)
376 // SHIPPED BUILD-865 ARM: quiet-quadrant early skip, at the live point (pristine and noisy) and at qp 32
377 sb_run(fp, nf, SB_QP_A, SB_THRESH_LIVE, 0, SB_EMODE_QQ, bufs)
378 sb_run(fp, nf, SB_QP_A, SB_THRESH_LIVE, SB_NOISE_MAX, SB_EMODE_QQ, bufs)
379 sb_run(fp, nf, SB_QP_B, SB_THRESH_LIVE, 0, SB_EMODE_QQ, bufs)
380 // EXPERIMENTAL, NOT SHIPPED: hexagon search; retained for comparison after failing the four-sequence BD-rate bar.
381 sb_run(fp, nf, SB_QP_A, SB_THRESH_LIVE, 0, SB_EMODE_QQ_HEX, bufs)
382 sw("STAGE-BENCH verdict=MEASURED\n" as *u8)
383 return 0
384}