code wiki / _hdl_build / nx_vcodec_intrap_gate.nx

nx_vcodec_intrap_gate.nx source

↩ module page · 93 lines · 5353 B

1import "nx_gate_base.nx" 2// nx_vcodec_intrap_gate.nx -- vcv-8 INTRA-IN-P measurement on /tmp/seq.y4m. Full encode->decode roundtrip with 3// SEPARATE recon chains (desync => memcmp trips). Compares emode 41 (baseline), 297 (41|bit8 = intra-in-P), and 4// 425 (41|bit7|bit8 = intra-in-P + partition together), each at qp 8/20/32: P-total bytes + luma MSE + BIT-EXACT. 5// A non-skip P-MB may be coded intra (9-mode, no MV) when even flat-DC intra beats the best inter. license: ORIGINAL 6import "nx_syscalls.nx" 7import "nx_video_codec_wasm.nx" 8 9const SW: i64 = 352 10const SH: i64 = 288 11const NF: i64 = 48 12 13func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 14" as *u8); return ok } 15func gn(v: i64) -> i64 { 16 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 17 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} 18 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 19func cpb(d: *u8, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 } 20func 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 } 21 22func frames(yuv: *u8, hi: i64, total: i64, fp: *i64, fdata: i64) -> i64 { 23 var nf: i64=0; var p: i64=hi 24 while nf < NF { if p>=total { break } 25 var q: i64=p; while q<total { if (yuv[q]&0xff)==10 { break } q=q+1 } q=q+1 26 if q+fdata>total { break } fp[nf]=(yuv as i64)+q; nf=nf+1; p=q+fdata } 27 return nf } 28 29func roundtrip(fp: *i64, qp: i64, emode: i64, sz: i64, ok: *i64) -> i64 { 30 let prevE: *u8=sys_mmap(sz+64); let reconE: *u8=sys_mmap(sz+64) 31 let prevD: *u8=sys_mmap(sz+64); let reconD: *u8=sys_mmap(sz+64) 32 let wire: *u8=sys_mmap(4194304); let blk: *i64=sys_mmap(512) as *i64; let mv: *i64=sys_mmap(128) as *i64 33 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 34 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 35 let rctxE: *i64=sys_mmap(64) as *i64; let rctxD: *i64=sys_mmap(64) as *i64 36 vc_t8_init(t8cE); vc_t8_init(t8cD) 37 var z: i64=0; while z<sz { prevE[z]=0 as u8; prevD[z]=0 as u8; z=z+1 } 38 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 39 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 40 ok[0]=0-1; ok[1]=0 41 var pB: i64=0 42 var f: i64=0 43 while f < NF { 44 let cur: *u8=fp[f] as *u8 45 var key: i64=0; if f==0 { key=1 } 46 rctxE[0]=emode 47 let nb: i64 = vv_enc_rct8(cur, prevE, reconE, SW, SH, qp, key, qp*188, wire, 4194304, blk, mv, rctxE) 48 if nb<=0 { gw(" enc FAIL frame " as *u8); gn(f); gw("\n" as *u8); ok[0]=f; return pB } 49 rctxD[0]=emode 50 let dr: i64 = vv_dec_rct8(prevD, reconD, SW, SH, qp, wire, nb, blk, mv, rctxD) 51 if dr<0 { gw(" dec FAIL frame " as *u8); gn(f); gw("\n" as *u8); ok[0]=f; return pB } 52 let mm: i64 = eqb(reconE, reconD, sz) 53 if mm >= 0 { if ok[0] < 0 { ok[0]=f } } 54 var pxi: i64=0; let NLp: i64=SW*SH 55 while pxi<NLp { let d: i64=(reconE[pxi]&0xff)-(cur[pxi]&0xff); ok[1]=ok[1]+d*d; pxi=pxi+1 } 56 if key==1 { } else { pB=pB+nb } 57 cpb(prevE, reconE, sz); cpb(prevD, reconD, sz) 58 f=f+1 59 } 60 return pB } 61 62func onerun(fp: *i64, sz: i64, qp: i64, emode: i64, label: *u8) -> i64 { 63 let ok: *i64=sys_mmap(16) as *i64 64 let pB: i64=roundtrip(fp, qp, emode, sz, ok) 65 let NL: i64=NF*SW*SH; let mmse: i64=ok[1]*1000/NL 66 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) 67 if ok[0] < 0 { gw("GREEN" as *u8) } else { gw("RED(f" as *u8); gn(ok[0]); gw(")" as *u8) } 68 gw("\n" as *u8) 69 return pB } 70 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 " as *u8) 74 let intr: i64=onerun(fp, sz, qp, 41+256, "intra-in-P (vcv-8) " as *u8) 75 let both: i64=onerun(fp, sz, qp, 41+128+256, "intra-in-P+partition" as *u8) 76 if base>0 { gw(" --> intra-in-P delta=" as *u8); gn((base-intr)*1000/base); gw("permil both delta=" as *u8); gn((base-both)*1000/base); gw("permil\n" as *u8) } 77 return 0 } 78 79func main() -> i64 { 80 gw("=== nx_vcodec_intrap_gate: vcv-8 INTRA-IN-P roundtrip on /tmp/seq.y4m ===\n" as *u8) 81 let box: *i64 = sys_mmap(16) as *i64 82 let yuv: *u8 = sys_read_file("/tmp/seq.y4m" as *u8, box) 83 if (yuv as i64)==0 { gw("cannot read /tmp/seq.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; let sz: i64=fdata 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 sweepqp(fp, sz, 8) 91 sweepqp(fp, sz, 32) 92 gw("INTRAP: DONE\n" as *u8) 93 return 0 }