code wiki / _hdl_build / nx_vcodec_drift_bench.nx

nx_vcodec_drift_bench.nx source

↩ module page · 73 lines · 4483 B

1import "nx_gate_base.nx" 2// nx_vcodec_drift_bench.nx -- exposes the P-frame DRIFT the short-GOP RD bench (key+5P) HID. Encodes a LONG 3// GOP (key+11P) on the same real content and prints the PER-FRAME PSNR curve. If PSNR decays frame-over-frame 4// while the keyframe is high, that is drift: deadzone/RDOQ zero small residuals -> recon diverges from source -> 5// compounds over the GOP. This is the measurement FOUNDATION for the codec-parity program (the gen-2 arc 6// optimized on the short GOP and never saw drift, the dominant real-video gap vs x264). license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_video_codec_wasm.nx" 9import "nx_quality_metric.nx" 10const K_MAGIC_4194304: i64 = 4194304 11const K_MAGIC_2097152: i64 = 2097152 12const K_MAGIC_5120: i64 = 5120 13 14const NW: i64 = 576 15const NH: i64 = 1024 16const GOP: i64 = 12 // key + 11P -- the content file holds 12 frames 17 18func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 19" as *u8); return ok } 20func gn(v: i64) -> i64 { 21 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 22 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} 23 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 24func 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 } 25func cpb(d: *u8, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 } 26func seedctxe(rctx: *i64, emode: i64, est: *i64, probs: *i64, rcbuf: *u8, t8c: *i64) -> i64 { 27 rctx[0]=emode; rctx[1]=est as i64; rctx[2]=probs as i64; rctx[3]=rcbuf as i64; rctx[4]=t8c as i64 28 rctx[5]=0; rctx[6]=0; rctx[7]=0; return 0 } 29 30func main() -> i64 { 31 gw("=== nx_vcodec_drift_bench: PER-FRAME PSNR over key+11P (long GOP) -- exposes the drift the 5P bench HID ===\n" as *u8) 32 let box: *i64 = sys_mmap(16) as *i64 33 let yuv: *u8 = sys_read_file("/mnt/c/Users/elder/nishi-core/nxc2/knowledge/staging/media/bframe_test_decoded.yuv" as *u8, box) 34 if (yuv as i64) == 0 { gw("cannot read yuv -> RED\n" as *u8); return 1 } 35 let N: i64=NW*NH; let C2: i64=(NW/2)*(NH/2); let sz: i64=N+2*C2; let FB: i64 = N + N/2 36 if box[0] < 12 * FB { gw("file too small -> RED\n" as *u8); return 1 } 37 let bufs: *i64 = sys_mmap(16*8) as *i64 38 bufs[0]=sys_mmap(sz+64) as i64; bufs[1]=sys_mmap(sz+64) as i64 39 bufs[4]=sys_mmap(K_MAGIC_4194304) as i64; bufs[5]=sys_mmap(512) as i64; bufs[6]=sys_mmap(128) as i64 40 bufs[7]=sys_mmap(64) as i64; bufs[8]=sys_mmap(32*8) as i64; bufs[9]=sys_mmap(K_MAGIC_2097152) as i64 41 bufs[10]=sys_mmap(K_MAGIC_5120) as i64; bufs[11]=sys_mmap(64) as i64 42 vc_t8_init(bufs[10] as *i64) 43 let prevE: *u8 = bufs[0] as *u8; let reconE: *u8 = bufs[1] as *u8 44 let wire: *u8 = bufs[4] as *u8; let blk: *i64 = bufs[5] as *i64; let mv: *i64 = bufs[6] as *i64 45 let est: *i64 = bufs[7] as *i64; let probs: *i64 = bufs[8] as *i64; let rcbuf: *u8 = bufs[9] as *u8 46 let t8c: *i64 = bufs[10] as *i64; let rctx: *i64 = bufs[11] as *i64 47 let qp: i64 = 22 48 49 var z: i64=0; while z<sz { prevE[z]=0 as u8; z=z+1 } 50 gw("-- rct8 qp=22, key+11P --\n" as *u8) 51 var keyp: i64 = 0; var lastp: i64 = 0; var totb: i64 = 0 52 var f: i64=0 53 while f<GOP { 54 let cur: *u8 = ((yuv as i64) + f*FB) as *u8 55 var key: i64=0; if f==0 { key=1 } 56 seedctxe(rctx, 1, est, probs, rcbuf, t8c) 57 let nb: i64 = vv_enc_rct8(cur, prevE, reconE, NW, NH, qp, key, qp*188, wire, K_MAGIC_4194304, blk, mv, rctx) 58 if nb <= 0 { gw("enc FAIL -> RED\n" as *u8); return 1 } 59 let ps: i64 = qm_psnr_cdb(reconE, cur, N) 60 totb = totb + nb 61 gw(" frame " as *u8); gn(f) 62 if key==1 { gw(" I psnr=" as *u8) } else { gw(" P psnr=" as *u8) } 63 g2(ps); gw("dB bytes=" as *u8); gn(nb); gw("\n" as *u8) 64 if f==0 { keyp = ps } 65 lastp = ps 66 cpb(prevE, reconE, sz) // real behavior: next prev = the RECON (decoder-reproducible). Flip to `cur` to isolate reference drift. 67 f=f+1 68 } 69 gw("=== DRIFT = keyPSNR - lastP-PSNR (bigger = worse P-frame degradation) = " as *u8) 70 g2(keyp - lastp); gw("dB total bytes=" as *u8); gn(totb); gw("\n" as *u8) 71 if keyp - lastp >= 300 { gw("VERDICT: DRIFT CONFIRMED (>=3dB P degradation over 11 P-frames)\n" as *u8) } 72 else { gw("VERDICT: low drift (<3dB)\n" as *u8) } 73 return 0 }