code wiki / _hdl_build / nx_vcodec_handoff_bench.nx

nx_vcodec_handoff_bench.nx source

↩ module page · 126 lines · 9006 B

1import "nx_gate_base.nx" 2// nx_vcodec_handoff_bench.nx -- HANDOFF-CHAIN TELEMETRY (the racing crew's per-corner sensors). For every frame 3// it decomposes the delivered quality into the pipeline HANDOFFS so you can see WHERE quality is lost, not just 4// the final number. Two chains run in lockstep on the same content: 5// REAL chain: predict from the RECON reference (what a decoder gets) -> psnrR 6// CLEAN chain: predict from the ORIGINAL previous frame (a drift-free reference) -> psnrC 7// Then per frame: 8// PRED+CODE handoff (the intra-frame floor) = psnrC -- prediction + transform + quant + entropy, no drift 9// REFERENCE handoff (drift injected) = psnrC - psnrR -- quality lost purely to reference quant-error 10// the I->P1 gap (keyframe - psnrC[1]) = the CODING CLIFF (fundamental P-frame RD at this qp) 11// This is the FOUNDATION the racing organs drive (researcher = find SOTA per-handoff techniques; census = grade 12// chain coverage; critic/adversary = stress each handoff; the crew tunes toward the perfect RD line). license_tier: ORIGINAL 13import "nx_syscalls.nx" 14import "nx_video_codec_wasm.nx" 15import "nx_quality_metric.nx" 16const K_MAGIC_1000000: i64 = 1000000 17const K_MAGIC_4194304: i64 = 4194304 18const K_MAGIC_2097152: i64 = 2097152 19const K_MAGIC_5120: i64 = 5120 20const K_MAGIC_2049: i64 = 2049 21const K_MAGIC_1600: i64 = 1600 22const K_MAGIC_2100: i64 = 2100 23 24const NW: i64 = 576 25const NH: i64 = 1024 26const GOP: i64 = 12 27 28func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 29" as *u8); return ok } 30func gn(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 } 34func g2(v: i64) -> i64 { var x: i64=v; if x<0 { gw("-" as *u8); x=0-x } gn(x/100); gw("." as *u8); let f: i64=x%100; if f<10 { gw("0" as *u8) } gn(f); return 0 } 35func cpb(d: *u8, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 } 36func seedctxe(rctx: *i64, emode: i64, est: *i64, probs: *i64, rcbuf: *u8, t8c: *i64) -> i64 { 37 rctx[0]=emode; rctx[1]=est as i64; rctx[2]=probs as i64; rctx[3]=rcbuf as i64; rctx[4]=t8c as i64 38 rctx[5]=0; rctx[6]=0; rctx[7]=0; return 0 } 39// TIME sensor: monotonic microseconds (CLOCK_MONOTONIC=1, syscall 113 -> timespec [sec,nsec]). The "where are 40// we slow" axis of the handoff telemetry, alongside the "where do we lose quality" axis. 41func mono_us(ts: *i64) -> i64 { __syscall(113, 1, ts as i64, 0, 0, 0, 0); return ts[0]*K_MAGIC_1000000 + ts[1]/1000 } 42 43func main() -> i64 { 44 gw("=== nx_vcodec_handoff_bench: PER-FRAME HANDOFF TELEMETRY (predict+code floor vs reference-drift) ===\n" as *u8) 45 let box: *i64 = sys_mmap(16) as *i64 46 let yuv: *u8 = sys_read_file("/mnt/c/Users/elder/nishi-core/nxc2/knowledge/staging/media/bframe_test_decoded.yuv" as *u8, box) 47 if (yuv as i64) == 0 { gw("cannot read yuv -> RED\n" as *u8); return 1 } 48 let N: i64=NW*NH; let C2: i64=(NW/2)*(NH/2); let sz: i64=N+2*C2; let FB: i64 = N + N/2 49 if box[0] < 12 * FB { gw("file too small -> RED\n" as *u8); return 1 } 50 let bufs: *i64 = sys_mmap(16*8) as *i64 51 bufs[0]=sys_mmap(sz+64) as i64; bufs[1]=sys_mmap(sz+64) as i64; bufs[2]=sys_mmap(sz+64) as i64 52 bufs[4]=sys_mmap(K_MAGIC_4194304) as i64; bufs[5]=sys_mmap(512) as i64; bufs[6]=sys_mmap(128) as i64 53 bufs[7]=sys_mmap(64) as i64; bufs[8]=sys_mmap(32*8) as i64; bufs[9]=sys_mmap(K_MAGIC_2097152) as i64 54 bufs[10]=sys_mmap(K_MAGIC_5120) as i64; bufs[11]=sys_mmap(64) as i64 55 vc_t8_init(bufs[10] as *i64) 56 let prevR: *u8 = bufs[0] as *u8; let reconR: *u8 = bufs[1] as *u8; let reconC: *u8 = bufs[2] as *u8 57 let wire: *u8 = bufs[4] as *u8; let blk: *i64 = bufs[5] as *i64; let mv: *i64 = bufs[6] as *i64 58 let est: *i64 = bufs[7] as *i64; let probs: *i64 = bufs[8] as *i64; let rcbuf: *u8 = bufs[9] as *u8 59 let t8c: *i64 = bufs[10] as *i64; let rctx: *i64 = bufs[11] as *i64 60 let qp: i64 = 22 61 let tsb: *i64 = sys_mmap(16) as *i64 62 var sumUs: i64 = 0 63 64 var z: i64=0; while z<sz { prevR[z]=0 as u8; z=z+1 } 65 gw("-- rct8 qp=22, key+11P. cols: frame | coded(real) | floor(clean-ref) | REFdrift | bytes --\n" as *u8) 66 var keyp: i64 = 0; var sumRef: i64 = 0; var lastR: i64 = 0; var floor1: i64 = 0; var liar: i64 = 0; var monobad: i64 = 0; var prevref: i64 = 0 67 var f: i64=0 68 while f<GOP { 69 let cur: *u8 = ((yuv as i64) + f*FB) as *u8 70 var key: i64=0; if f==0 { key=1 } 71 // REAL chain: predict from recon reference 72 seedctxe(rctx, 1, est, probs, rcbuf, t8c) // RTC default (RD-skip OFF). K_MAGIC_2049 = fast RD-skip on (+7dB, byte-identical to full-RD, ~2.5x faster). 73 let te0: i64 = mono_us(tsb) 74 let nb: i64 = vv_enc_rct8(cur, prevR, reconR, NW, NH, qp, key, qp*188, wire, K_MAGIC_4194304, blk, mv, rctx) 75 let eus: i64 = mono_us(tsb) - te0 76 sumUs = sumUs + eus 77 if nb <= 0 { gw("enc FAIL -> RED\n" as *u8); return 1 } 78 let psR: i64 = qm_psnr_cdb(reconR, cur, N) 79 var psC: i64 = psR 80 if key == 0 { 81 // CLEAN chain: SAME frame, predict from the ORIGINAL previous frame (drift-free reference) 82 let cleanref: *u8 = ((yuv as i64) + (f-1)*FB) as *u8 83 seedctxe(rctx, 1, est, probs, rcbuf, t8c) // RTC default (RD-skip OFF). K_MAGIC_2049 = fast RD-skip on (+7dB, byte-identical to full-RD, ~2.5x faster). 84 let nc: i64 = vv_enc_rct8(cur, cleanref, reconC, NW, NH, qp, key, qp*188, wire, K_MAGIC_4194304, blk, mv, rctx) 85 if nc > 0 { psC = qm_psnr_cdb(reconC, cur, N) } 86 } 87 let refdrift: i64 = psC - psR 88 // LIAR-KILLER 1 (physics): a DRIFTED reference can NEVER give higher quality than the CLEAN-ref floor. 89 // If coded(real) beats floor(clean) by > 0.2dB, the measurement is inconsistent (a gamed/buggy sensor). 90 if key==0 { if psR > psC + 20 { liar = 1 } } 91 // LIAR-KILLER 2 (monotonic drift): reference-handoff loss must not DECREASE frame-over-frame on a static 92 // single-GOP chain (drift only accumulates). A non-monotone dip = a sensor that flatters itself. 93 if key==0 { if refdrift + 40 < prevref { monobad = monobad + 1 } prevref = refdrift } 94 gw(" f" as *u8); gn(f) 95 if key==1 { gw(" I coded=" as *u8) } else { gw(" P coded=" as *u8) } 96 g2(psR); gw(" floor=" as *u8); g2(psC); gw(" REFdrift=" as *u8); g2(refdrift) 97 gw(" bytes=" as *u8); gn(nb); gw(" enc=" as *u8); gn(eus); gw("us\n" as *u8) 98 if f==0 { keyp = psR } 99 if f==1 { floor1 = psC } 100 if key==0 { sumRef = sumRef + refdrift } 101 lastR = psR 102 cpb(prevR, reconR, sz) 103 f=f+1 104 } 105 gw("=== HANDOFF DECOMPOSITION (where the quality goes) ===\n" as *u8) 106 gw(" CODING-CLIFF handoff (keyframe - P1 clean-floor) = " as *u8); g2(keyp - floor1) 107 gw("dB <- fundamental P-frame prediction+quant at qp (the BIG lever: sub-pel/multi-ref/B)\n" as *u8) 108 gw(" REFERENCE handoff (mean drift/P-frame, x11 cumulative) = mean " as *u8); g2(sumRef/11) 109 gw("dB/frame <- reference quant-error (the LTR lever)\n" as *u8) 110 gw(" TOTAL delivered drop (keyframe - last coded) = " as *u8); g2(keyp - lastR); gw("dB\n" as *u8) 111 gw(" TIME handoff: total encode = " as *u8); gn(sumUs); gw("us / " as *u8); gn(GOP); gw(" frames (per-frame enc us above -> spot the SLOW frames)\n" as *u8) 112 // ---- LIAR KILLERS + REGRESSION GATE (the template the nishi teams universalize to every capability) ---- 113 gw("=== GATE ===\n" as *u8) 114 var red: i64 = 0 115 if liar == 1 { gw(" LIAR-KILLER-1 FAIL: a drifted-ref frame beat its clean-ref floor -> sensor lying -> RED\n" as *u8); red = 1 } 116 else { gw(" LIAR-KILLER-1 PASS: clean-ref floor >= drifted coded on every P (physically consistent)\n" as *u8) } 117 if monobad > 0 { gw(" LIAR-KILLER-2 WARN: reference-handoff drift non-monotone on " as *u8); gn(monobad); gw(" frame(s) (content-motion, not a lie -- watch)\n" as *u8) } 118 else { gw(" LIAR-KILLER-2 PASS: reference-handoff drift monotone-accumulating\n" as *u8) } 119 // REGRESSION GATE: the coding-cliff + total-drop are the SOTA-track numbers; a change that WORSENS them regresses. 120 // Baseline (2026-07-14, this content/qp): cliff 13.43dB, total drop 18.70dB, encode 574ms. Tighten as the crew climbs. 121 let cliff: i64 = keyp - floor1; let drop: i64 = keyp - lastR 122 if cliff > K_MAGIC_1600 { gw(" REGRESSION: coding-cliff worse than 16.0dB baseline-guard -> RED\n" as *u8); red = 1 } 123 if drop > K_MAGIC_2100 { gw(" REGRESSION: total drop worse than 21.0dB baseline-guard -> RED\n" as *u8); red = 1 } 124 if red == 0 { gw(" verdict=GREEN (handoff chain within SOTA-track guards; liar-killers passed)\n" as *u8) } 125 else { gw(" verdict=RED\n" as *u8) } 126 return 0 }