code wiki / _hdl_build / nx_vcodec_msu_ours12.nx
nx_vcodec_msu_ours12.nx source
↩ module page · 102 lines · 5510 B
1// nx_vcodec_msu_ours12.nx -- Implements a video codec encoder with heat-AQ and skip-threshold adjustments for specific compression efficiency.
2import "nx_gate_base.nx"
3// nx_vcodec_msu_ours12.nx -- EXACT RD points for OUR codec at the P2 HEAT-AQ config (emode 4777 = 681 | 4096 =
4// the shipped vcv-8 RTC stack + bit12 heat-AQ: per-MB skip-thresh /2 once the MB's skip streak >= 4 -- MEASURED
5// -1.72% avg BD-rate vs the 681 baseline by the fidelity-proven mirror probe, nx_vcodec_heataq). This emitter
6// runs the REAL PORTED encoder (rctx 9th slot = heat plane, vv layer clears bit12 for chroma) -- its BD-rate vs
7// h2h_t681 must REPRODUCE the mirror's numbers, or the port is wrong. Same OURS/OURSINTRA line format.
8// license: ORIGINAL
9import "nx_syscalls.nx"
10import "nx_video_codec_wasm.nx"
11const K_MAGIC_4194304: i64 = 4194304
12const K_MAGIC_5120: i64 = 5120
13const K_MAGIC_8192: i64 = 8192
14const K_MAGIC_4777: i64 = 4777
15
16const SW: i64 = 352
17const SH: i64 = 288
18const NF: i64 = 48
19const SKIPMUL: i64 = 30
20
21func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
22" as *u8); return ok }
23func gn(v: i64) -> i64 {
24 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
25 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}
26 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
27func cpb(d: *u8, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 }
28
29func frames(yuv: *u8, hi: i64, total: i64, fp: *i64, fdata: i64) -> i64 {
30 var nf: i64=0; var p: i64=hi
31 while nf < NF { if p>=total { break }
32 var q: i64=p; while q<total { if (yuv[q]&0xff)==10 { break } q=q+1 } q=q+1
33 if q+fdata>total { break } fp[nf]=(yuv as i64)+q; nf=nf+1; p=q+fdata }
34 return nf }
35
36func runpq(fp: *i64, kq: i64, pq: i64, emode: i64, sz: i64, allkey: i64) -> i64 {
37 let prevE: *u8=sys_mmap(sz+64); let reconE: *u8=sys_mmap(sz+64)
38 let wire: *u8=sys_mmap(K_MAGIC_4194304); let blk: *i64=sys_mmap(512) as *i64; let mv: *i64=sys_mmap(128) as *i64
39 let est: *i64=sys_mmap(64) as *i64; let probs: *i64=sys_mmap(32*8) as *i64; let rcbuf: *u8=sys_mmap(K_MAGIC_4194304)
40 let t8c: *i64=sys_mmap(K_MAGIC_5120) as *i64; let rctx: *i64=sys_mmap(128) as *i64
41 let heat: *i64=sys_mmap(K_MAGIC_8192) as *i64 // (352/16)*(288/16)=396 MBs -- zeroed by mmap
42 vc_t8_init(t8c)
43 var z: i64=0; while z<sz { prevE[z]=0 as u8; z=z+1 }
44 rctx[1]=est as i64; rctx[2]=probs as i64; rctx[3]=rcbuf as i64; rctx[4]=t8c as i64; rctx[5]=0; rctx[6]=0; rctx[7]=0
45 rctx[8]=heat as i64 // 9th slot: heat plane (read only under bit12)
46 var keyB: i64=0; var pB: i64=0; var sseY: i64=0; var keySSE: i64=0; var npix: i64=0
47 let NL: i64=SW*SH
48 var f: i64=0
49 while f < NF {
50 let cur: *u8=fp[f] as *u8
51 var key: i64=0; if f==0 { key=1 } if allkey==1 { key=1 }
52 var fq: i64 = kq; if key==0 { fq = pq }
53 rctx[0]=emode
54 let nb: i64 = vv_enc_rct8(cur, prevE, reconE, SW, SH, fq, key, fq*SKIPMUL, wire, K_MAGIC_4194304, blk, mv, rctx)
55 if nb<=0 { gw("enc fail\n" as *u8); return 1 }
56 if f==0 { keyB=nb } else { pB=pB+nb }
57 var pxi: i64=0; var fsse: i64=0
58 while pxi<NL { let d: i64=(reconE[pxi]&0xff)-(cur[pxi]&0xff); fsse=fsse+d*d; pxi=pxi+1 }
59 sseY=sseY+fsse; if f==0 { keySSE=fsse }
60 npix=npix+NL
61 cpb(prevE, reconE, sz)
62 f=f+1
63 }
64 if allkey==1 { gw("OURSINTRA kq=" as *u8) } else { gw("OURS kq=" as *u8) }
65 gn(kq); gw(" pq=" as *u8); gn(pq); gw(" total=" as *u8); gn(keyB+pB); gw(" key=" as *u8); gn(keyB)
66 gw(" ptot=" as *u8); gn(pB); gw(" sseY=" as *u8); gn(sseY); gw(" keySSE=" as *u8); gn(keySSE)
67 gw(" npix=" as *u8); gn(npix); gw("\n" as *u8)
68 return 0 }
69
70func main(argc: i64, argv: *i64) -> i64 {
71 // optional argv[1] = emode (decimal); default 4777 = the shipped 819 RTC config
72 var emode: i64 = K_MAGIC_4777
73 if argc >= 2 { let a: *u8 = argv[1] as *u8
74 var v: i64 = 0
75 var i: i64 = 0
76 while a[i] != (0 as u8) { v = v * 10 + ((a[i] & 0xff) - 48); i = i + 1 }
77 if v > 0 { emode = v } }
78 // argv[2] = y4m path (default /tmp/seq.y4m). Direct paths kill the recurring sibling-`wsl --shutdown`
79 // /tmp-wipe failure class (three casualties on 2026-07-12 alone).
80 var ypath: *u8 = "/tmp/seq.y4m" as *u8
81 if argc >= 3 { ypath = argv[2] as *u8 }
82 let box: *i64 = sys_mmap(16) as *i64
83 let yuv: *u8 = sys_read_file(ypath, box)
84 if (yuv as i64)==0 { gw("cannot read seq y4m -> RED\n" as *u8); return 1 }
85 let total: i64=box[0]
86 var hi: i64=0; while hi<total { if (yuv[hi]&0xff)==10 { break } hi=hi+1 } hi=hi+1
87 let C2: i64=(SW/2)*(SH/2); let fdata: i64=SW*SH+2*C2; let sz: i64=fdata
88 let fp: *i64=sys_mmap(128*8) as *i64
89 let nf: i64=frames(yuv, hi, total, fp, fdata)
90 if nf < NF { gw("too few frames -> RED\n" as *u8); return 1 }
91 runpq(fp, 8, 8, emode, sz, 0)
92 runpq(fp, 16, 16, emode, sz, 0)
93 runpq(fp, 24, 24, emode, sz, 0)
94 runpq(fp, 32, 32, emode, sz, 0)
95 runpq(fp, 44, 44, emode, sz, 0)
96 runpq(fp, 8, 8, emode, sz, 1)
97 runpq(fp, 16, 16, emode, sz, 1)
98 runpq(fp, 24, 24, emode, sz, 1)
99 runpq(fp, 32, 32, emode, sz, 1)
100 runpq(fp, 44, 44, emode, sz, 1)
101 gw("MSU_OURS12: DONE emode=" as *u8); gn(emode); gw("\n" as *u8)
102 return 0 }