code wiki / _hdl_build / nx_vcodec_part8_gate.nx
nx_vcodec_part8_gate.nx source
↩ module page · 93 lines · 5439 B
1import "nx_gate_base.nx"
2// nx_vcodec_part8_gate.nx -- vcv-7 rung 1: prove P_8x8 MOTION PARTITION on /tmp/seq.y4m. Full encode->decode
3// roundtrip with SEPARATE enc/dec recon chains (if the decoder mis-parses a partitioned MB the chains diverge
4// and memcmp trips). Reports emode 41 (baseline = shipped, no partition) vs 169 (41|bit7 = partition-enabled):
5// P-total bytes + luma MSE + BIT-EXACT verdict. Bytes DOWN at 169 with GREEN bit-exact + flat MSE = the measured
6// residual-cut ceiling (10-19%) realized net of MV cost = a real vcv-7 win. license: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_video_codec_wasm.nx"
9
10const SW: i64 = 352
11const SH: i64 = 288
12const NF: i64 = 48
13
14func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
15" as *u8); return ok }
16func gn(v: i64) -> i64 {
17 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
18 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}
19 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
20func cpb(d: *u8, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 }
21func eqb(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return i } i=i+1 } return 0-1 }
22
23func frames(yuv: *u8, hi: i64, total: i64, fp: *i64, fdata: i64) -> i64 {
24 var nf: i64=0; var p: i64=hi
25 while nf < NF { if p>=total { break }
26 var q: i64=p; while q<total { if (yuv[q]&0xff)==10 { break } q=q+1 } q=q+1
27 if q+fdata>total { break } fp[nf]=(yuv as i64)+q; nf=nf+1; p=q+fdata }
28 return nf }
29
30func roundtrip(fp: *i64, qp: i64, emode: i64, sz: i64, ok: *i64) -> i64 {
31 let prevE: *u8=sys_mmap(sz+64); let reconE: *u8=sys_mmap(sz+64)
32 let prevD: *u8=sys_mmap(sz+64); let reconD: *u8=sys_mmap(sz+64)
33 let wire: *u8=sys_mmap(4194304); let blk: *i64=sys_mmap(512) as *i64; let mv: *i64=sys_mmap(128) as *i64
34 let estE: *i64=sys_mmap(64) as *i64; let probsE: *i64=sys_mmap(32*8) as *i64; let rcE: *u8=sys_mmap(4194304); let t8cE: *i64=sys_mmap(5120) as *i64
35 let estD: *i64=sys_mmap(64) as *i64; let probsD: *i64=sys_mmap(32*8) as *i64; let t8cD: *i64=sys_mmap(5120) as *i64
36 let rctxE: *i64=sys_mmap(64) as *i64; let rctxD: *i64=sys_mmap(64) as *i64
37 vc_t8_init(t8cE); vc_t8_init(t8cD)
38 var z: i64=0; while z<sz { prevE[z]=0 as u8; prevD[z]=0 as u8; z=z+1 }
39 rctxE[1]=estE as i64; rctxE[2]=probsE as i64; rctxE[3]=rcE as i64; rctxE[4]=t8cE as i64; rctxE[5]=0; rctxE[6]=0; rctxE[7]=0
40 rctxD[1]=estD as i64; rctxD[2]=probsD as i64; rctxD[3]=0; rctxD[4]=t8cD as i64; rctxD[5]=0; rctxD[6]=0; rctxD[7]=0
41 ok[0]=0-1; ok[1]=0
42 var pB: i64=0
43 var f: i64=0
44 while f < NF {
45 let cur: *u8=fp[f] as *u8
46 var key: i64=0; if f==0 { key=1 }
47 rctxE[0]=emode
48 let nb: i64 = vv_enc_rct8(cur, prevE, reconE, SW, SH, qp, key, qp*30, wire, 4194304, blk, mv, rctxE)
49 if nb<=0 { gw(" enc FAIL frame " as *u8); gn(f); gw("\n" as *u8); ok[0]=f; return pB }
50 rctxD[0]=emode
51 let dr: i64 = vv_dec_rct8(prevD, reconD, SW, SH, qp, wire, nb, blk, mv, rctxD)
52 if dr<0 { gw(" dec FAIL frame " as *u8); gn(f); gw("\n" as *u8); ok[0]=f; return pB }
53 let mm: i64 = eqb(reconE, reconD, sz)
54 if mm >= 0 { if ok[0] < 0 { ok[0]=f; gw(" DESYNC frame " as *u8); gn(f); gw(" at byte " as *u8); gn(mm); gw("\n" as *u8) } }
55 var pxi: i64=0; let NLp: i64=SW*SH
56 while pxi<NLp { let d: i64=(reconE[pxi]&0xff)-(cur[pxi]&0xff); ok[1]=ok[1]+d*d; pxi=pxi+1 }
57 if key==1 { } else { pB=pB+nb }
58 cpb(prevE, reconE, sz); cpb(prevD, reconD, sz)
59 f=f+1
60 }
61 return pB }
62
63func onerun(fp: *i64, sz: i64, qp: i64, emode: i64, label: *u8) -> i64 {
64 let ok: *i64=sys_mmap(16) as *i64
65 let pB: i64=roundtrip(fp, qp, emode, sz, ok)
66 let NL: i64=NF*SW*SH; let mmse: i64=ok[1]*1000/NL
67 gw(" " as *u8); gw(label); gw(" emode=" as *u8); gn(emode); gw(" P-total=" as *u8); gn(pB); gw("B lumaMSEx1000=" as *u8); gn(mmse); gw(" bitexact=" as *u8)
68 if ok[0] < 0 { gw("GREEN" as *u8) } else { gw("RED(frame " as *u8); gn(ok[0]); gw(")" as *u8) }
69 gw("\n" as *u8)
70 return pB }
71func sweepqp(fp: *i64, sz: i64, qp: i64) -> i64 {
72 gw(" --- qp=" as *u8); gn(qp); gw(" ---\n" as *u8)
73 let base: i64=onerun(fp, sz, qp, 41, "baseline (no partition)" as *u8)
74 let feat: i64=onerun(fp, sz, qp, 41+128, "P_8x8 partition (vcv-7)" as *u8)
75 if base>0 { gw(" --> P-frame delta = " as *u8); gn((base-feat)*1000/base); gw(" per-mille\n" as *u8) }
76 return 0 }
77
78func main() -> i64 {
79 gw("=== nx_vcodec_part8_gate: vcv-7 P_8x8 motion-partition roundtrip on /tmp/seq.y4m (qp=8) ===\n" as *u8)
80 let box: *i64 = sys_mmap(16) as *i64
81 let yuv: *u8 = sys_read_file("/tmp/seq.y4m" as *u8, box)
82 if (yuv as i64)==0 { gw("cannot read /tmp/seq.y4m -> RED\n" as *u8); return 1 }
83 let total: i64=box[0]
84 var hi: i64=0; while hi<total { if (yuv[hi]&0xff)==10 { break } hi=hi+1 } hi=hi+1
85 let C2: i64=(SW/2)*(SH/2); let fdata: i64=SW*SH+2*C2; let sz: i64=fdata
86 let fp: *i64=sys_mmap(128*8) as *i64
87 let nf: i64=frames(yuv, hi, total, fp, fdata)
88 if nf < NF { gw("too few frames -> RED\n" as *u8); return 1 }
89 sweepqp(fp, sz, 8)
90 sweepqp(fp, sz, 20)
91 sweepqp(fp, sz, 32)
92 gw("PART8: DONE\n" as *u8)
93 return 0 }