code wiki / _hdl_build / nx_vcodec_pt8_gate.nx

nx_vcodec_pt8_gate.nx source

↩ module page · 92 lines · 5612 B

1import "nx_gate_base.nx" 2// nx_vcodec_pt8_gate.nx -- 803: prove P-frame 8x8 RD-auto (decoder-transparent debt-eat) on /tmp/seq.y4m. 3// Runs the FULL encode->decode roundtrip with SEPARATE enc/dec recon chains (the real conformance test: if the 4// decoder desyncs on any P-frame t8==1 MB, the two chains diverge and memcmp trips). Reports, per sequence: 5// emode 41 (bit2 OFF = baseline 4x4-only P) vs emode 45 (bit2 ON = P-frame RD 4x4-vs-8x8) -- P-total bytes + 6// BIT-EXACT verdict for BOTH. A byte drop at 45 with GREEN bit-exact = a real, shippable, every-room win. 7// license: ORIGINAL 8import "nx_syscalls.nx" 9import "nx_video_codec_wasm.nx" 10 11const SW: i64 = 352 12const SH: i64 = 288 13const NF: i64 = 48 14 15func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 16" as *u8); return ok } 17func gn(v: i64) -> i64 { 18 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 19 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} 20 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 21func cpb(d: *u8, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 } 22func 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 } 23 24func frames(yuv: *u8, hi: i64, total: i64, fp: *i64, fdata: i64) -> i64 { 25 var nf: i64=0; var p: i64=hi 26 while nf < NF { if p>=total { break } 27 var q: i64=p; while q<total { if (yuv[q]&0xff)==10 { break } q=q+1 } q=q+1 28 if q+fdata>total { break } fp[nf]=(yuv as i64)+q; nf=nf+1; p=q+fdata } 29 return nf } 30 31// returns P-total bytes; sets ok[0]=first mismatch frame (-1 = bit-exact GREEN) 32func roundtrip(fp: *i64, qp: i64, emode: i64, sz: i64, ok: *i64) -> i64 { 33 let prevE: *u8=sys_mmap(sz+64); let reconE: *u8=sys_mmap(sz+64) 34 let prevD: *u8=sys_mmap(sz+64); let reconD: *u8=sys_mmap(sz+64) 35 let wire: *u8=sys_mmap(4194304); let blk: *i64=sys_mmap(512) as *i64; let mv: *i64=sys_mmap(128) as *i64 36 // separate enc/dec state (est/probs/rcbuf/t8c) -- they must not share scratch 37 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 38 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 39 let rctxE: *i64=sys_mmap(64) as *i64; let rctxD: *i64=sys_mmap(64) as *i64 40 vc_t8_init(t8cE); vc_t8_init(t8cD) 41 var z: i64=0; while z<sz { prevE[z]=0 as u8; prevD[z]=0 as u8; z=z+1 } 42 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 43 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 44 ok[0]=0-1 45 var pB: i64=0 46 var f: i64=0 47 while f < NF { 48 let cur: *u8=fp[f] as *u8 49 var key: i64=0; if f==0 { key=1 } 50 rctxE[0]=emode 51 let nb: i64 = vv_enc_rct8(cur, prevE, reconE, SW, SH, qp, key, qp*188, wire, 4194304, blk, mv, rctxE) 52 if nb<=0 { gw(" enc FAIL frame " as *u8); gn(f); gw("\n" as *u8); ok[0]=f; return pB } 53 rctxD[0]=emode 54 let dr: i64 = vv_dec_rct8(prevD, reconD, SW, SH, qp, wire, nb, blk, mv, rctxD) 55 if dr<0 { gw(" dec FAIL frame " as *u8); gn(f); gw("\n" as *u8); ok[0]=f; return pB } 56 let mm: i64 = eqb(reconE, reconD, sz) 57 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) } } 58 // accumulate luma SSE (recon vs original) for PSNR -- confirms the RD pick doesn't trade quality for bytes 59 var pxi: i64=0; let NL: i64=SW*SH 60 while pxi<NL { let d: i64=(reconE[pxi]&0xff)-(cur[pxi]&0xff); ok[1]=ok[1]+d*d; pxi=pxi+1 } 61 if key==1 { } else { pB=pB+nb } 62 cpb(prevE, reconE, sz); cpb(prevD, reconD, sz) 63 f=f+1 64 } 65 return pB } 66 67func onerun(fp: *i64, sz: i64, emode: i64, label: *u8) -> i64 { 68 let ok: *i64=sys_mmap(16) as *i64 69 let pB: i64=roundtrip(fp, 8, emode, sz, ok) 70 let NL: i64=NF*SW*SH; let mmse: i64=ok[1]*1000/NL 71 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) 72 if ok[0] < 0 { gw("GREEN" as *u8) } else { gw("RED(frame " as *u8); gn(ok[0]); gw(")" as *u8) } 73 gw("\n" as *u8) 74 return pB } 75 76func main() -> i64 { 77 gw("=== nx_vcodec_pt8_gate: P-frame 8x8 RD-auto roundtrip on /tmp/seq.y4m (qp=8) ===\n" as *u8) 78 let box: *i64 = sys_mmap(16) as *i64 79 let yuv: *u8 = sys_read_file("/tmp/seq.y4m" as *u8, box) 80 if (yuv as i64)==0 { gw("cannot read /tmp/seq.y4m -> RED\n" as *u8); return 1 } 81 let total: i64=box[0] 82 var hi: i64=0; while hi<total { if (yuv[hi]&0xff)==10 { break } hi=hi+1 } hi=hi+1 83 let C2: i64=(SW/2)*(SH/2); let fdata: i64=SW*SH+2*C2; let sz: i64=fdata 84 let fp: *i64=sys_mmap(128*8) as *i64 85 let nf: i64=frames(yuv, hi, total, fp, fdata) 86 if nf < NF { gw("too few frames -> RED\n" as *u8); return 1 } 87 let base: i64=onerun(fp, sz, 41, "baseline (4x4-only P)" as *u8) 88 let feat: i64=onerun(fp, sz, 45, "P-t8 RD-auto (803) " as *u8) 89 if base>0 { gw(" --> P-frame delta = " as *u8); gn((base-feat)*1000/base); gw(" per-mille (" as *u8) 90 if feat<base { gw("SMALLER, feature helps" as *u8) } else { gw("no gain" as *u8) } gw(")\n" as *u8) } 91 gw("PT8: DONE\n" as *u8) 92 return 0 }