code wiki / _hdl_build / nx_vcodec_rdskip_gate.nx

nx_vcodec_rdskip_gate.nx source

↩ module page · 216 lines · 11559 B

1import "nx_gate_base.nx" 2// nx_vcodec_rdskip_gate.nx -- GEN-2 RUNG-0 P0c: RD-BASED SKIP ceiling (and a candidate GEN-1 SHIP: the skip bit 3// is on the wire, so a smarter encoder-side skip decision is DECODER-TRANSPARENT -- no vcv bump). Today skip is 4// a threshold (zsad < qp*30, the audit's known-compromise constant: optimum ~30 akiyo vs ~20 foreman). The RIGHT 5// rule is RD: skip iff J_skip = 32*D_skip <= J_code = 32*D_code + qp^2*R_code with REAL coded bits. This gate 6// implements it by TRIAL CODING each P-MB both ways through the live coder: 7// - snapshot the range-coder state (est 8xi64 + probs 32xi64 + rctx[5..7]) and the CAVLC bit position, 8// - arm CODE = vc_enc_block_packed_e with sad_thresh=0 (forces doskip=0) -> real bits (flush-delta) + D, 9// - arm SKIP = restore, call with sad_thresh=HUGE (forces doskip=1) -> ~1 bit + D_freeze, 10// - commit the J-winner (restore + re-run winner arm). 11// R_code counted as CAVLC bp-delta + range-bytes delta via rc_enc_flush on a COPY of est (exact if-stream-ended- 12// now length, state untouched). Fidelity: B0 = mirror with the SHIPPED threshold rule must equal the real 13// encoder BIT-FOR-BIT (else the probe is invalid). Reports P-bytes + MSE + flip counts at qp 8/20/32. license: ORIGINAL 14import "nx_syscalls.nx" 15import "nx_video_codec_wasm.nx" 16 17const SW: i64 = 352 18const SH: i64 = 288 19const NF: i64 = 48 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 } 28func cpi(d: *i64, s: *i64, 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 37// exact stream-length-so-far in BITS if the range stream ended now: flush a COPY of est into scratch. 38func rc_len_now(est: *i64, estC: *i64, scratch: *u8) -> i64 { 39 cpi(estC, est, 8) 40 let rb: i64 = rc_enc_flush(estC, scratch) 41 return rb * 8 42} 43// SSE of one 16x16 luma MB recon vs cur 44func mb_sse(cur: *u8, recon: *u8, W: i64, cx: i64, cy: i64) -> i64 { 45 var s: i64 = 0 46 var y: i64 = 0 47 while y < 16 { 48 var x: i64 = 0 49 while x < 16 { let d: i64 = (recon[(cy+y)*W + cx+x] as i64) - (cur[(cy+y)*W + cx+x] as i64); s = s + d*d; x = x + 1 } 50 y = y + 1 } 51 return s 52} 53 54// MIRROR of the rct8 frame fn (both frame types), rdmode: 0 = shipped threshold rule (fidelity arm), 55// 1 = RD-SKIP trial coding on P MBs. flips[0]+= threshold-said-skip but RD codes; flips[1]+= threshold-said-code 56// but RD skips. 57func enc_mirror(cur: *u8, prev: *u8, recon: *u8, W: i64, H: i64, qp: i64, keyframe: i64, buf: *u8, blk: *i64, mv: *i64, rctx: *i64, rdmode: i64, flips: *i64) -> i64 { 58 rctx[0] = rctx[0] | 8 59 if (rctx[0] & 16) != 0 { rctx[0] = (rctx[0] | 4) - 4 } 60 let est: *i64 = rctx[1] as *i64 61 let probs: *i64 = rctx[2] as *i64 62 rc_enc_init(est) 63 var ci: i64 = 0 64 while ci < RC_NCTX8 { probs[ci] = 2048; ci = ci + 1 } 65 var bp: i64 = nx_bw_put(buf, 16, keyframe, 1) 66 bp = nx_bw_put(buf, bp, 0, 1) 67 let BW: i64 = W / 16 68 let BH: i64 = H / 16 69 let estS: *i64 = sys_mmap(64) as *i64 70 let probsS: *i64 = sys_mmap(32*8) as *i64 71 let estC: *i64 = sys_mmap(64) as *i64 72 let fl: *u8 = sys_mmap(65536) 73 let r5: *i64 = sys_mmap(24) as *i64 74 var by: i64 = 0 75 while by < BH { 76 var bx: i64 = 0 77 while bx < BW { 78 let cx: i64 = bx*16 79 let cy: i64 = by*16 80 let lv: i64 = vc_aq_level(vc_mb_mad(cur, W, cx, cy)) 81 bp = nx_bw_put(buf, bp, lv, 2) 82 var t8: i64 = 0 83 if (rctx[0] & 16) == 0 { if keyframe == 1 { if lv <= 1 { t8 = vc_mb_t8_ok(cur, W, cx, cy) } } } 84 let aq: i64 = vc_aq_qp(qp, lv) 85 var dotrial: i64 = 0 86 if rdmode == 1 { if keyframe == 0 { dotrial = 1 } } 87 if dotrial == 0 { 88 rctx[5] = bp 89 bp = nx_bw_put(buf, bp, t8, 1) 90 bp = vc_enc_block_packed_e(cur, prev, recon, W, H, bx, by, aq, keyframe, qp*30, buf, bp, blk, mv, 1 + (t8 << 1), rctx) 91 } else { 92 // ---- RD-SKIP trial ---- 93 let zsad: i64 = vm_sad_zero(cur, prev, W, bx, by, 16) 94 let thsays: i64 = vs_skip(zsad, 0, 0, qp*30) 95 cpi(estS, est, 8) 96 cpi(probsS, probs, 32) 97 r5[0] = rctx[5]; r5[1] = rctx[6]; r5[2] = rctx[7] 98 let bp0: i64 = bp 99 let rc0: i64 = rc_len_now(est, estC, fl) 100 // arm CODE (sad_thresh=0 -> never skip) 101 rctx[5] = bp0 102 var bpc: i64 = nx_bw_put(buf, bp0, t8, 1) 103 bpc = vc_enc_block_packed_e(cur, prev, recon, W, H, bx, by, aq, 0, 0, buf, bpc, blk, mv, 1, rctx) 104 let bits_code: i64 = (bpc - bp0) + (rc_len_now(est, estC, fl) - rc0) 105 let d_code: i64 = mb_sse(cur, recon, W, cx, cy) 106 // restore -> arm SKIP (sad_thresh=HUGE -> always skip; writes 1 skip bit + freezes recon) 107 cpi(est, estS, 8) 108 cpi(probs, probsS, 32) 109 rctx[5] = r5[0]; rctx[6] = r5[1]; rctx[7] = r5[2] 110 rctx[5] = bp0 111 var bps: i64 = nx_bw_put(buf, bp0, t8, 1) 112 bps = vc_enc_block_packed_e(cur, prev, recon, W, H, bx, by, aq, 0, 1152921504606846976, buf, bps, blk, mv, 1, rctx) 113 let bits_skip: i64 = bps - bp0 114 let d_skip: i64 = mb_sse(cur, recon, W, cx, cy) 115 // J = 32*D + qp^2 * bits (the codec's own transform-select Lagrangian) 116 let jc: i64 = 32*d_code + aq*aq*bits_code 117 let js: i64 = 32*d_skip + aq*aq*bits_skip 118 var choose_skip: i64 = 0 119 if js <= jc { choose_skip = 1 } 120 if choose_skip != thsays { if choose_skip == 0 { flips[0] = flips[0] + 1 } else { flips[1] = flips[1] + 1 } } 121 if choose_skip == 1 { 122 bp = bps 123 } else { 124 // recon currently holds the SKIP arm -> redo the CODE arm as the commit 125 cpi(est, estS, 8) 126 cpi(probs, probsS, 32) 127 rctx[5] = bp0 128 var bpf: i64 = nx_bw_put(buf, bp0, t8, 1) 129 bpf = vc_enc_block_packed_e(cur, prev, recon, W, H, bx, by, aq, 0, 0, buf, bpf, blk, mv, 1, rctx) 130 bp = bpf 131 } 132 } 133 bx = bx + 1 } 134 by = by + 1 } 135 let rbytes: i64 = rc_enc_flush(est, rctx[3] as *u8) 136 let cavlc_end: i64 = (bp + 7) / 8 137 buf[0] = (cavlc_end & 0xff) as u8 138 buf[1] = ((cavlc_end >> 8) & 0xff) as u8 139 let rcbuf: *u8 = rctx[3] as *u8 140 var i: i64 = 0 141 while i < rbytes { buf[cavlc_end + i] = rcbuf[i]; i = i + 1 } 142 return (cavlc_end + rbytes) * 8 143} 144 145// mode 0 = real frame fn (A) · 1 = mirror threshold rule (B0 fidelity) · 2 = mirror RD-skip (C). 146// out: [0]=P bits total, [1]=luma SSE, [2]=flips code<-skip, [3]=flips skip<-code 147func runv(fp: *i64, qp: i64, sz: i64, mode: i64, out: *i64) -> i64 { 148 let prevE: *u8=sys_mmap(sz+64); let reconE: *u8=sys_mmap(sz+64) 149 let buf: *u8=sys_mmap(4194304); let blk: *i64=sys_mmap(512) as *i64; let mv: *i64=sys_mmap(128) as *i64 150 let est: *i64=sys_mmap(64) as *i64; let probs: *i64=sys_mmap(32*8) as *i64; let rcb: *u8=sys_mmap(4194304) 151 let t8c: *i64=sys_mmap(5120) as *i64; let rctx: *i64=sys_mmap(64) as *i64 152 let flips: *i64=sys_mmap(16) as *i64 153 vc_t8_init(t8c) 154 var z: i64=0; while z<sz { prevE[z]=0 as u8; z=z+1 } 155 rctx[1]=est as i64; rctx[2]=probs as i64; rctx[3]=rcb as i64; rctx[4]=t8c as i64; rctx[5]=0; rctx[6]=0; rctx[7]=0 156 flips[0]=0; flips[1]=0 157 out[0]=0; out[1]=0; out[2]=0; out[3]=0 158 let NL: i64 = SW*SH 159 var f: i64=0 160 while f < NF { 161 let cur: *u8=fp[f] as *u8 162 var key: i64=0; if f==0 { key=1 } 163 rctx[0]=681 164 var bits: i64 = 0 165 if mode == 0 { bits = vc_enc_frame_packed_rct8(cur, prevE, reconE, SW, SH, qp, key, qp*30, buf, blk, mv, 1, rctx) } 166 if mode == 1 { bits = enc_mirror(cur, prevE, reconE, SW, SH, qp, key, buf, blk, mv, rctx, 0, flips) } 167 if mode == 2 { bits = enc_mirror(cur, prevE, reconE, SW, SH, qp, key, buf, blk, mv, rctx, 1, flips) } 168 if mode == 3 { rctx[0]=681+2048; bits = vc_enc_frame_packed_rct8(cur, prevE, reconE, SW, SH, qp, key, qp*30, buf, blk, mv, 1, rctx) } 169 if key == 0 { out[0] = out[0] + bits } 170 var pxi: i64=0 171 while pxi<NL { let d: i64=(reconE[pxi]&0xff)-(cur[pxi]&0xff); out[1]=out[1]+d*d; pxi=pxi+1 } 172 cpb(prevE, reconE, sz) 173 f=f+1 174 } 175 out[2]=flips[0]; out[3]=flips[1] 176 return 0 } 177 178func sweepqp(fp: *i64, sz: i64, qp: i64) -> i64 { 179 gw(" --- qp=" as *u8); gn(qp); gw(" (LUMA I+P, base 681) ---\n" as *u8) 180 let oA: *i64=sys_mmap(40) as *i64 181 let oB0: *i64=sys_mmap(40) as *i64 182 let oC: *i64=sys_mmap(40) as *i64 183 runv(fp, qp, sz, 0, oA) 184 runv(fp, qp, sz, 1, oB0) 185 runv(fp, qp, sz, 2, oC) 186 let NL: i64 = NF*SW*SH 187 gw(" A real : Pbits=" as *u8); gn(oA[0]); gw(" MSEx1000=" as *u8); gn(oA[1]*1000/NL); gw("\n" as *u8) 188 gw(" B0 mirror : Pbits=" as *u8); gn(oB0[0]); gw(" fidelity=" as *u8) 189 if oB0[0] == oA[0] { gw("GREEN (bit-identical)" as *u8) } else { gw("RED -- PROBE INVALID" as *u8) } 190 gw("\n" as *u8) 191 gw(" C RD-skip : Pbits=" as *u8); gn(oC[0]); gw(" MSEx1000=" as *u8); gn(oC[1]*1000/NL) 192 gw(" flips code<-skip=" as *u8); gn(oC[2]); gw(" skip<-code=" as *u8); gn(oC[3]); gw("\n" as *u8) 193 let oD: *i64=sys_mmap(40) as *i64 194 runv(fp, qp, sz, 3, oD) 195 gw(" D PORT 2729: Pbits=" as *u8); gn(oD[0]); gw(" MSEx1000=" as *u8); gn(oD[1]*1000/NL); gw(" port-fidelity=" as *u8) 196 if oD[0] == oC[0] { gw("GREEN (port == mirror bit-identical)" as *u8) } else { gw("RED -- PORT DIVERGES FROM REFERENCE" as *u8) } 197 gw("\n" as *u8) 198 if oA[0] > 0 { gw(" --> RD-skip P-bit delta = " as *u8); gn((oA[0]-oC[0])*1000/oA[0]); gw(" permille (positive = RD-skip SMALLER; compare MSE too)\n" as *u8) } 199 return 0 } 200 201func main() -> i64 { 202 gw("=== nx_vcodec_rdskip_gate: RD-based skip (real trial bits) vs qp*30 threshold on /tmp/seq.y4m ===\n" as *u8) 203 let box: *i64 = sys_mmap(16) as *i64 204 let yuv: *u8 = sys_read_file("/tmp/seq.y4m" as *u8, box) 205 if (yuv as i64)==0 { gw("cannot read /tmp/seq.y4m -> RED\n" as *u8); return 1 } 206 let total: i64=box[0] 207 var hi: i64=0; while hi<total { if (yuv[hi]&0xff)==10 { break } hi=hi+1 } hi=hi+1 208 let C2: i64=(SW/2)*(SH/2); let fdata: i64=SW*SH+2*C2; let sz: i64=fdata 209 let fp: *i64=sys_mmap(128*8) as *i64 210 let nf: i64=frames(yuv, hi, total, fp, fdata) 211 if nf < NF { gw("too few frames -> RED\n" as *u8); return 1 } 212 sweepqp(fp, sz, 8) 213 sweepqp(fp, sz, 20) 214 sweepqp(fp, sz, 32) 215 gw("RDSKIP: DONE\n" as *u8) 216 return 0 }