code wiki / _hdl_build / nx_vcodec_msu_hp.nx

nx_vcodec_msu_hp.nx source

↩ module page · 95 lines · 5102 B

1// nx_vcodec_msu_hp.nx -- Implements hierarchical P-frame quality pumping with alternating fine and coarse quantization for rate-distortion optimization. 2import "nx_gate_base.nx" 3// nx_vcodec_msu_hp.nx -- GEN-LADDER P1a: QP-CASCADE (hierarchical-P quality pumping, LINEAR-chain half) RD 4// points at the SHIPPED config (emode 2729 = 816 RD-skip). P-frames alternate qp-DLT / qp+DLT (same average): 5// even P-frames get FINER quant (better references for the next frame), odd get coarser (cheaper; their extra 6// error is partially re-anchored by the next fine frame). DECODER-TRANSPARENT: qp rides the per-frame vv header, 7// any decoder reads it. The Fraunhofer PCS-2010 hier-P claim (-7.9%) includes reference restructuring (vcv-9); 8// this probes the QP half alone. HP_DLT=1 here; the _hp2 clone probes DLT=2 (⚠visual pumping risk at 2+ -- BD 9// won't see flicker; eyes required before any ship). Emits OURS/OURSINTRA lines for msu_bdrate.sh. license: ORIGINAL 10import "nx_syscalls.nx" 11import "nx_video_codec_wasm.nx" 12const HP_MAGIC_4194304: i64 = 4194304 13const HP_MAGIC_5120: i64 = 5120 14const HP_MAGIC_2729: i64 = 2729 15 16const SW: i64 = 352 17const SH: i64 = 288 18const NF: i64 = 48 19const SKIPMUL: i64 = 30 20const HP_DLT: i64 = 1 21 22func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 23" as *u8); return ok } 24func gn(v: i64) -> i64 { 25 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 26 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} 27 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 28func cpb(d: *u8, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 } 29 30func frames(yuv: *u8, hi: i64, total: i64, fp: *i64, fdata: i64) -> i64 { 31 var nf: i64=0; var p: i64=hi 32 while nf < NF { if p>=total { break } 33 var q: i64=p; while q<total { if (yuv[q]&0xff)==10 { break } q=q+1 } q=q+1 34 if q+fdata>total { break } fp[nf]=(yuv as i64)+q; nf=nf+1; p=q+fdata } 35 return nf } 36 37func run(fp: *i64, qp: i64, emode: i64, sz: i64, allkey: i64) -> i64 { 38 let prevE: *u8=sys_mmap(sz+64); let reconE: *u8=sys_mmap(sz+64) 39 let wire: *u8=sys_mmap(HP_MAGIC_4194304); let blk: *i64=sys_mmap(512) as *i64; let mv: *i64=sys_mmap(128) as *i64 40 let est: *i64=sys_mmap(64) as *i64; let probs: *i64=sys_mmap(32*8) as *i64; let rcbuf: *u8=sys_mmap(HP_MAGIC_4194304) 41 let t8c: *i64=sys_mmap(HP_MAGIC_5120) as *i64; let rctx: *i64=sys_mmap(64) as *i64 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 var keyB: i64=0; var pB: i64=0; var sseY: i64=0; var keySSE: i64=0; var npix: i64=0 46 let NL: i64=SW*SH 47 var f: i64=0 48 while f < NF { 49 let cur: *u8=fp[f] as *u8 50 var key: i64=0; if f==0 { key=1 } if allkey==1 { key=1 } 51 var fq: i64 = qp 52 if key == 0 { 53 // QP CASCADE: even P-frames fine (qp-DLT), odd coarse (qp+DLT); same average as flat qp 54 if (f & 1) == 0 { fq = qp - HP_DLT } else { fq = qp + HP_DLT } 55 if fq < 1 { fq = 1 } 56 } 57 rctx[0]=emode 58 let nb: i64 = vv_enc_rct8(cur, prevE, reconE, SW, SH, fq, key, fq*SKIPMUL, wire, HP_MAGIC_4194304, blk, mv, rctx) 59 if nb<=0 { gw("enc fail\n" as *u8); return 1 } 60 if f==0 { keyB=nb } else { pB=pB+nb } 61 var pxi: i64=0; var fsse: i64=0 62 while pxi<NL { let d: i64=(reconE[pxi]&0xff)-(cur[pxi]&0xff); fsse=fsse+d*d; pxi=pxi+1 } 63 sseY=sseY+fsse; if f==0 { keySSE=fsse } 64 npix=npix+NL 65 cpb(prevE, reconE, sz) 66 f=f+1 67 } 68 if allkey==1 { gw("OURSINTRA kq=" as *u8) } else { gw("OURS kq=" as *u8) } 69 gn(qp); gw(" pq=" as *u8); gn(qp); gw(" total=" as *u8); gn(keyB+pB); gw(" key=" as *u8); gn(keyB) 70 gw(" ptot=" as *u8); gn(pB); gw(" sseY=" as *u8); gn(sseY); gw(" keySSE=" as *u8); gn(keySSE) 71 gw(" npix=" as *u8); gn(npix); gw("\n" as *u8) 72 return 0 } 73 74func main() -> i64 { 75 let box: *i64 = sys_mmap(16) as *i64 76 let yuv: *u8 = sys_read_file("/tmp/seq.y4m" as *u8, box) 77 if (yuv as i64)==0 { gw("cannot read /tmp/seq.y4m -> RED\n" as *u8); return 1 } 78 let total: i64=box[0] 79 var hi: i64=0; while hi<total { if (yuv[hi]&0xff)==10 { break } hi=hi+1 } hi=hi+1 80 let C2: i64=(SW/2)*(SH/2); let fdata: i64=SW*SH+2*C2; let sz: i64=fdata 81 let fp: *i64=sys_mmap(128*8) as *i64 82 let nf: i64=frames(yuv, hi, total, fp, fdata) 83 if nf < NF { gw("too few frames -> RED\n" as *u8); return 1 } 84 run(fp, 8, HP_MAGIC_2729, sz, 0) 85 run(fp, 16, HP_MAGIC_2729, sz, 0) 86 run(fp, 24, HP_MAGIC_2729, sz, 0) 87 run(fp, 32, HP_MAGIC_2729, sz, 0) 88 run(fp, 44, HP_MAGIC_2729, sz, 0) 89 run(fp, 8, HP_MAGIC_2729, sz, 1) 90 run(fp, 16, HP_MAGIC_2729, sz, 1) 91 run(fp, 24, HP_MAGIC_2729, sz, 1) 92 run(fp, 32, HP_MAGIC_2729, sz, 1) 93 run(fp, 44, HP_MAGIC_2729, sz, 1) 94 gw("MSU_HP: DONE\n" as *u8) 95 return 0 }