code wiki / _hdl_build / nx_vcodec_msu_ours13.nx

nx_vcodec_msu_ours13.nx source

↩ module page · 139 lines · 7670 B

1// nx_vcodec_msu_ours13.nx -- EXACT RD points for OUR codec at the SHIPPED CHAIN (2026-07-27 field 2// config, exactly what the family runs and what nx_vcodec_layout_gate proves): KEY frames ride 3// vv_enc_rc (skip-thresh qp*188), P frames ride vv_enc_rct9 (lean+MVD-median, deadzone qp*94), 4// emode 673 = rc+sig+part+deblock, rctx[9] = the MVD MV row-plane rct9 REQUIRES. Same OURS line 5// format as ours12 (total/key/ptot/sseY/keySSE/npix; PSNR computed exactly downstream -- no float 6// here). The x264 arm runs externally on the SAME y4m; BD-rate joins the two curves. CIF only. 7// license: ORIGINAL 8import "nx_syscalls.nx" 9import "nx_video_codec_wasm.nx" 10// pts emission (2026-07-29): the sovereign BD loop is ours13 -> .pts -> nx_bd_calc; psnr_milli is 11// computed here with the bits-up f64 stack so NO external tool ever touches the lane numbers. 12import "nx_tier.nx" 13import "nx_f64.nx" 14import "nx_f64_div.nx" 15import "nx_f64_cvt.nx" 16import "nx_f64_log2.nx" 17const O13_MAGIC_65025: i64 = 65025 18const O13_MAGIC_4194304: i64 = 4194304 19const O13_MAGIC_8192: i64 = 8192 20const O13_MAGIC_65536: i64 = 65536 21const O13_LOG10_2: i64 = 0x3FD34413509F79FF // log10(2) binary64 22const O13_10K: i64 = 0x40C3880000000000 // 10000.0 (10*log10(x)*1000 = log10(x)*10000) 23const O13_HALF: i64 = 0x3FE0000000000000 24 25const SW: i64 = 352 26const SH: i64 = 288 27const NF: i64 = 48 28const KEYMUL: i64 = 188 // key skip-thresh multiplier (the layout gate's shipped value) 29const PMUL: i64 = 94 // rct9 P deadzone multiplier (the layout gate's shipped value) 30 31func gw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 32func gn(v: i64) -> i64 { 33 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 34 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} 35 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 36func cpb(d: *u8, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 } 37 38func frames(yuv: *u8, hi: i64, total: i64, fp: *i64, fdata: i64) -> i64 { 39 var nf: i64=0; var p: i64=hi 40 while nf < NF { if p>=total { break } 41 var q: i64=p; while q<total { if (yuv[q]&0xff)==10 { break } q=q+1 } q=q+1 42 if q+fdata>total { break } fp[nf]=(yuv as i64)+q; nf=nf+1; p=q+fdata } 43 return nf } 44 45// one rctx re-seed per encode call, mirroring nx_vcodec_layout_gate lg_round exactly (the frame fns 46// mutate est/probs/rctx[5]; a stale seed is the classic cross-frame contamination trap). 47func seed(rctx: *i64, emode: i64, est: *i64, probs: *i64, rcbuf: *u8, t8c: *i64, plane: *i64) -> i64 { 48 rctx[0]=emode; 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 return 0 } 51 52// psnr_milli = round(10000 * log10(255^2*npix/sse)) -- 65025*npix ~ 3.2e11 fits i64 comfortably 53func o13_psnr_milli(sse: i64, npix: i64) -> i64 { 54 let x: i64 = nx_f64_div(nx_i64_to_f64(O13_MAGIC_65025*npix), nx_i64_to_f64(sse)) 55 let v: i64 = nx_f64_mul(nx_f64_mul(nx_f64_log2(x), O13_LOG10_2), O13_10K) 56 return nx_f64_to_i64(nx_f64_add(v, O13_HALF)) } 57func o13_wnum(fd: i64, v: i64) -> i64 { 58 let b: *u8=sys_mmap(28); var m: i64=v 59 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} 60 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(fd,b,k); return 0 } 61func runq(fp: *i64, qp: i64, emode: i64, sz: i64, allkey: i64, ptsfd: i64) -> i64 { 62 let prevE: *u8=sys_mmap(sz+64); let reconE: *u8=sys_mmap(sz+64) 63 let wire: *u8=sys_mmap(O13_MAGIC_4194304); let blk: *i64=sys_mmap(512) as *i64; let mv: *i64=sys_mmap(128) as *i64 64 let est: *i64=sys_mmap(64) as *i64; let probs: *i64=sys_mmap(64*8) as *i64; let rcbuf: *u8=sys_mmap(O13_MAGIC_4194304) 65 let t8c: *i64=sys_mmap(O13_MAGIC_8192) as *i64; let rctx: *i64=sys_mmap(128) as *i64 66 let plane: *i64=sys_mmap(O13_MAGIC_65536) as *i64 67 vc_t8_init(t8c) 68 var z: i64=0; while z<sz { prevE[z]=0 as u8; z=z+1 } 69 var keyB: i64=0; var pB: i64=0; var sseY: i64=0; var keySSE: i64=0; var npix: i64=0 70 var yT: i64=0; var uT: i64=0; var vT: i64=0 71 let NL: i64=SW*SH 72 var f: i64=0 73 while f < NF { 74 let cur: *u8=fp[f] as *u8 75 var key: i64=0; if f==0 { key=1 } if allkey==1 { key=1 } 76 seed(rctx, emode, est, probs, rcbuf, t8c, plane) 77 var nb: i64 = 0 78 if key==1 { nb = vv_enc_rc(cur, prevE, reconE, SW, SH, qp, 1, qp*KEYMUL, wire, O13_MAGIC_4194304, blk, mv, rctx) } 79 else { nb = vv_enc_rct9(cur, prevE, reconE, SW, SH, qp, qp*PMUL, wire, O13_MAGIC_4194304, blk, mv, rctx) } 80 if nb<=0 { gw("enc fail\n" as *u8); return 1 } 81 if key==1 { keyB=keyB+nb } else { pB=pB+nb } 82 // per-plane split from the vv wire header (yb/ub/vb u32s at offsets 0/4/8) 83 yT = yT + vv_rd_u32b(wire, 0) 84 uT = uT + vv_rd_u32b(wire, 4) 85 vT = vT + vv_rd_u32b(wire, 8) 86 var pxi: i64=0; var fsse: i64=0 87 while pxi<NL { let d: i64=(reconE[pxi]&0xff)-(cur[pxi]&0xff); fsse=fsse+d*d; pxi=pxi+1 } 88 sseY=sseY+fsse; if f==0 { keySSE=fsse } 89 npix=npix+NL 90 cpb(prevE, reconE, sz) 91 f=f+1 92 } 93 if allkey==1 { gw("OURS13INTRA q=" as *u8) } else { gw("OURS13 q=" as *u8) } 94 gn(qp); gw(" total=" as *u8); gn(keyB+pB); gw(" key=" as *u8); gn(keyB) 95 gw(" ptot=" as *u8); gn(pB); gw(" sseY=" as *u8); gn(sseY); gw(" keySSE=" as *u8); gn(keySSE) 96 gw(" npix=" as *u8); gn(npix) 97 gw(" ybytes=" as *u8); gn(yT); gw(" ubytes=" as *u8); gn(uT); gw(" vbytes=" as *u8); gn(vT) 98 if yT+uT+vT > 0 { gw(" chroma_permille=" as *u8); gn((uT+vT)*1000/(yT+uT+vT)) } 99 gw("\n" as *u8) 100 // pts row (inter sweeps only): "<kbps_milli> <psnr_milli>" -- kbps_milli = total*8*30/48 = total*5 101 // EXACTLY at 48f@30fps (the sweep's declared clock), so the rate side stays integer-exact. 102 if ptsfd >= 0 { if allkey == 0 { 103 o13_wnum(ptsfd, (keyB+pB)*5); sys_write(ptsfd, " " as *u8, 1) 104 o13_wnum(ptsfd, o13_psnr_milli(sseY, npix)); sys_write(ptsfd, "\n" as *u8, 1) 105 } } 106 return 0 } 107 108func main(argc: i64, argv: *i64) -> i64 { 109 var emode: i64 = 673 110 if argc >= 2 { let a: *u8 = argv[1] as *u8 111 var v: i64 = 0 112 var i: i64 = 0 113 while a[i] != (0 as u8) { v = v * 10 + ((a[i] & 0xff) - 48); i = i + 1 } 114 if v > 0 { emode = v } } 115 var ypath: *u8 = "/tmp/seq.y4m" as *u8 116 if argc >= 3 { ypath = argv[2] as *u8 } 117 var ptsfd: i64 = 0 - 1 118 if argc >= 4 { ptsfd = sys_openat_wr(argv[3] as *u8, 0x1a4) 119 if ptsfd < 0 { gw("cannot open pts out -> RED\n" as *u8); return 1 } } 120 let box: *i64 = sys_mmap(16) as *i64 121 let yuv: *u8 = sys_read_file(ypath, box) 122 if (yuv as i64)==0 { gw("cannot read seq y4m -> RED\n" as *u8); return 1 } 123 let total: i64=box[0] 124 var hi: i64=0; while hi<total { if (yuv[hi]&0xff)==10 { break } hi=hi+1 } hi=hi+1 125 let C2: i64=(SW/2)*(SH/2); let fdata: i64=SW*SH+2*C2; let sz: i64=fdata 126 let fp: *i64=sys_mmap(128*8) as *i64 127 let nf: i64=frames(yuv, hi, total, fp, fdata) 128 if nf < NF { gw("too few frames -> RED\n" as *u8); return 1 } 129 runq(fp, 8, emode, sz, 0, ptsfd) 130 runq(fp, 16, emode, sz, 0, ptsfd) 131 runq(fp, 24, emode, sz, 0, ptsfd) 132 runq(fp, 32, emode, sz, 0, ptsfd) 133 runq(fp, 44, emode, sz, 0, ptsfd) 134 runq(fp, 16, emode, sz, 1, ptsfd) 135 runq(fp, 24, emode, sz, 1, ptsfd) 136 runq(fp, 32, emode, sz, 1, ptsfd) 137 if ptsfd >= 0 { sys_close(ptsfd) } 138 gw("MSU_OURS13: DONE emode=" as *u8); gn(emode); gw("\n" as *u8) 139 return 0 }