code wiki / _hdl_build / nx_vcodec_pskip_probe.nx
nx_vcodec_pskip_probe.nx source
↩ module page · 94 lines · 4834 B
1// nx_vcodec_pskip_probe.nx -- SIZE THE P-SKIP GAP (seq1105 rung-1 diagnostic). x264 skips 41.9% of
2// foreman P-MBs; its P-SKIP is a MOTION-COMPENSATED copy at the predicted MV with zero residual.
3// OUR skip is ZERO-MV-only (dosk iff vm_sad_zero <= sad_thresh) -- on panning content it may never
4// fire. This probe replays the SHIPPED P chain (rct9, emode 673) on a y4m and, per P frame, counts
5// against the SAME pre-encode reference the encoder saw:
6// ourskip = MBs with zero-MV SAD <= qp*94 (what the shipped gate can skip today)
7// mcskip = MBs with best-searched SAD <= qp*94 (what an MC-skip/merge mode could skip)
8// The mcskip-ourskip delta is the addressable population of rung-1. license: ORIGINAL
9import "nx_syscalls.nx"
10import "nx_video_codec_wasm.nx"
11
12const SW: i64 = 352
13const SH: i64 = 288
14const NF: i64 = 48
15const KEYMUL: i64 = 188
16const PMUL: i64 = 94
17
18func gw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
19func gn(v: i64) -> i64 {
20 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
21 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}
22 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
23func cpb(d: *u8, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 }
24
25func frames(yuv: *u8, hi: i64, total: i64, fp: *i64, fdata: i64) -> i64 {
26 var nf: i64=0; var p: i64=hi
27 while nf < NF { if p>=total { break }
28 var q: i64=p; while q<total { if (yuv[q]&0xff)==10 { break } q=q+1 } q=q+1
29 if q+fdata>total { break } fp[nf]=(yuv as i64)+q; nf=nf+1; p=q+fdata }
30 return nf }
31
32func runq(fp: *i64, qp: i64, sz: i64) -> i64 {
33 let prevE: *u8=sys_mmap(sz+64); let reconE: *u8=sys_mmap(sz+64); let ref0: *u8=sys_mmap(sz+64)
34 let wire: *u8=sys_mmap(4194304); let blk: *i64=sys_mmap(512) as *i64; let mv: *i64=sys_mmap(128) as *i64
35 let est: *i64=sys_mmap(64) as *i64; let probs: *i64=sys_mmap(64*8) as *i64; let rcbuf: *u8=sys_mmap(4194304)
36 let t8c: *i64=sys_mmap(8192) as *i64; let rctx: *i64=sys_mmap(128) as *i64
37 let plane: *i64=sys_mmap(65536) as *i64
38 let smv: *i64=sys_mmap(32) as *i64
39 vc_t8_init(t8c)
40 var z: i64=0; while z<sz { prevE[z]=0 as u8; z=z+1 }
41 let BW: i64=SW/16; let BH: i64=SH/16
42 let thr: i64 = qp*PMUL
43 var mbs: i64=0; var ours: i64=0; var mcs: i64=0
44 var zsum: i64=0; var bsum: i64=0
45 var f: i64=0
46 while f < NF {
47 let cur: *u8=fp[f] as *u8
48 rctx[0]=673; rctx[1]=est as i64; rctx[2]=probs as i64; rctx[3]=rcbuf as i64; rctx[4]=t8c as i64
49 rctx[5]=0; rctx[6]=0; rctx[7]=0; rctx[8]=0; rctx[9]=plane as i64
50 if f==0 {
51 vv_enc_rc(cur, prevE, reconE, SW, SH, qp, 1, qp*KEYMUL, wire, 4194304, blk, mv, rctx)
52 } else {
53 cpb(ref0, prevE, sz) // the exact reference the encoder is about to use
54 vv_enc_rct9(cur, prevE, reconE, SW, SH, qp, thr, wire, 4194304, blk, mv, rctx)
55 var by: i64=0
56 while by < BH { var bx: i64=0
57 while bx < BW {
58 let zs: i64 = vm_sad_zero(cur, ref0, SW, bx, by, 16)
59 let bs: i64 = vm_search_q(cur, ref0, SW, SH, bx, by, 16, 16, smv, qp, vmscr3)
60 mbs=mbs+1; zsum=zsum+zs; bsum=bsum+bs
61 if zs <= thr { ours=ours+1 }
62 if bs <= thr { mcs=mcs+1 }
63 bx=bx+1 } by=by+1 }
64 }
65 cpb(prevE, reconE, sz)
66 f=f+1
67 }
68 gw("PSKIP q=" as *u8); gn(qp); gw(" thr=" as *u8); gn(thr)
69 gw(" mbs=" as *u8); gn(mbs)
70 gw(" ourskip_permille=" as *u8); gn(ours*1000/mbs)
71 gw(" mcskip_permille=" as *u8); gn(mcs*1000/mbs)
72 gw(" mean_zsad=" as *u8); gn(zsum/mbs)
73 gw(" mean_bestsad=" as *u8); gn(bsum/mbs)
74 gw("\n" as *u8)
75 return 0 }
76
77func main(argc: i64, argv: *i64) -> i64 {
78 let vmscr3: *i64 = sys_mmap(32) as *i64 // vm_search scratch (2026-07-29: hoisted ONE alloc; the per-call sys_mmap inside vm_search was the wasm 0-stub + native map-leak class)
79 var ypath: *u8 = "/tmp/seq.y4m" as *u8
80 if argc >= 2 { ypath = argv[1] as *u8 }
81 let box: *i64 = sys_mmap(16) as *i64
82 let yuv: *u8 = sys_read_file(ypath, box)
83 if (yuv as i64)==0 { gw("cannot read y4m -> RED\n" as *u8); return 1 }
84 let total: i64=box[0]
85 var hi: i64=0; while hi<total { if (yuv[hi]&0xff)==10 { break } hi=hi+1 } hi=hi+1
86 let C2: i64=(SW/2)*(SH/2); let fdata: i64=SW*SH+2*C2
87 let fp: *i64=sys_mmap(128*8) as *i64
88 let nf: i64=frames(yuv, hi, total, fp, fdata)
89 if nf < NF { gw("too few frames -> RED\n" as *u8); return 1 }
90 runq(fp, 16, fdata)
91 runq(fp, 24, fdata)
92 runq(fp, 32, fdata)
93 gw("PSKIP-PROBE: DONE\n" as *u8)
94 return 0 }