code wiki / _hdl_build / nx_vcodec_bframe_ceiling.nx

nx_vcodec_bframe_ceiling.nx source

↩ module page · 99 lines · 5780 B

1// nx_vcodec_bframe_ceiling.nx -- measure the B-FRAME ceiling, decoder-transparent. B-frames (bidirectional prediction) 2// are x264's single biggest lever and the residual decomposition points here: our P-frames are ~5-9x behind x264 while 3// intra is only 2.6x, and sub-pel (6-tap re-audit) is dead -> the gap is STRUCTURAL prediction. A B-frame predicts each 4// block as the AVERAGE of a best-match in a PAST ref and a best-match in a FUTURE ref: averaging cancels noise, and (the 5// big win) reveal/occlusion regions that one direction cannot predict are covered by the other. This probe, on the aptly 6// named bframe_test, integer-searches each interior 16x16 block against both the previous AND next CLEAN frame, and 7// compares the residual of uni-directional (past-only, what our codec does today) vs bi-directional (past+future avg). 8// A large bi<uni cut = B-frames are THE lever and the (pervasive, VOD-only) enc+dec+reorder build is justified; a small 9// cut = look elsewhere (multi-ref, partitions). Pure measurement on clean originals => no bitstream, no parity risk. license_tier: ORIGINAL 10import "nx_syscalls.nx" 11import "nx_vmotion.nx" 12const K_MAGIC_2026: i64 = 2026 13 14const NW: i64 = 576 15const NH: i64 = 1024 16 17func gw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 18func gn(v: i64) -> i64 { 19 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 20 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} 21 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 22 23// SAD of cur's TxT block at (cx,cy) vs the BI-DIRECTIONAL average of past@(ppx,ppy) and future@(fpx,fpy) 24func bi_sad(cur: *u8, past: *u8, fut: *u8, W: i64, cx: i64, cy: i64, 25 ppx: i64, ppy: i64, fpx: i64, fpy: i64, T: i64) -> i64 { 26 var s: i64 = 0 27 var ry: i64 = 0 28 while ry < T { 29 var rx: i64 = 0 30 while rx < T { 31 let p: i64 = past[(ppy+ry)*W + (ppx+rx)] as i64 32 let f: i64 = fut[(fpy+ry)*W + (fpx+rx)] as i64 33 let pred: i64 = (p + f + 1) / 2 34 let d: i64 = (cur[(cy+ry)*W + (cx+rx)] as i64) - pred 35 if d < 0 { s = s - d } else { s = s + d } 36 rx = rx + 1 37 } 38 ry = ry + 1 39 } 40 return s 41} 42 43func main() -> i64 { 44 let vmscr3: *i64 = sys_mmap(32) as *i64 // vm_search scratch (K_MAGIC_2026-07-29: hoisted ONE alloc; the per-call sys_mmap inside vm_search was the wasm 0-stub + native map-leak class) 45 gw("=== nx_vcodec_bframe_ceiling: bidirectional vs unidirectional residual (is B-frames the P-gap lever?) ===\n" as *u8) 46 let box: *i64 = sys_mmap(16) as *i64 47 let yuv: *u8 = sys_read_file("/mnt/c/Users/elder/nishi-core/nxc2/knowledge/staging/media/bframe_test_decoded.yuv" as *u8, box) 48 if (yuv as i64) == 0 { gw("cannot read yuv -> RED\n" as *u8); return 1 } 49 let N: i64 = NW*NH 50 let FB: i64 = N + N/2 51 if box[0] < 12 * FB { gw("file too small -> RED\n" as *u8); return 1 } 52 let mvp: *i64 = sys_mmap(64) as *i64 53 let mvf: *i64 = sys_mmap(64) as *i64 54 let T: i64 = 16 55 56 var totPast: i64 = 0; var totFut: i64 = 0; var totUni: i64 = 0; var totBi: i64 = 0; var nblk: i64 = 0 57 // B-candidate frames 7,8,9, each predicted from its previous AND next CLEAN neighbour 58 var fc: i64 = 7 59 while fc <= 9 { 60 let cur: *u8 = ((yuv as i64) + fc*FB) as *u8 61 let past: *u8 = ((yuv as i64) + (fc-1)*FB) as *u8 62 let fut: *u8 = ((yuv as i64) + (fc-2)*FB) as *u8 // RTC-SAFE: 2nd PAST ref (N-2), no future/reorder/latency 63 var cy: i64 = 32 64 while cy <= NH-48 { 65 var cx: i64 = 32 66 while cx <= NW-48 { 67 let sadP: i64 = vm_search(cur, past, NW, NH, cx/16, cy/16, T, 16, mvp, vmscr3) 68 let sadF: i64 = vm_search(cur, fut, NW, NH, cx/16, cy/16, T, 16, mvf, vmscr3) 69 let ppx: i64 = cx + mvp[0]; let ppy: i64 = cy + mvp[1] 70 let fpx: i64 = cx + mvf[0]; let fpy: i64 = cy + mvf[1] 71 let sadBi: i64 = bi_sad(cur, past, fut, NW, cx, cy, ppx, ppy, fpx, fpy, T) 72 var uni: i64 = sadP 73 if sadF < uni { uni = sadF } 74 totPast = totPast + sadP 75 totFut = totFut + sadF 76 totUni = totUni + uni 77 totBi = totBi + sadBi 78 nblk = nblk + 1 79 cx = cx + 16 80 } 81 cy = cy + 16 82 } 83 fc = fc + 1 84 } 85 gw("blocks measured=" as *u8); gn(nblk); gw(" (interior 16x16, 3 B-candidate frames)\n" as *u8) 86 gw(" uni PAST-only total SAD = " as *u8); gn(totPast); gw(" <- what our codec does today\n" as *u8) 87 gw(" uni FUTURE-only total SAD = " as *u8); gn(totFut); gw("\n" as *u8) 88 gw(" uni BEST-of-2 total SAD = " as *u8); gn(totUni); gw(" <- multi-ref (pick better direction) ceiling\n" as *u8) 89 gw(" BIDIRECTIONAL total SAD = " as *u8); gn(totBi); gw(" <- B-frame ceiling\n" as *u8) 90 let biVsPast: i64 = (totPast-totBi)*1000/totPast 91 let biVsUni: i64 = (totUni-totBi)*1000/totUni 92 let uniVsPast: i64 = (totPast-totUni)*1000/totPast 93 gw(" === B-frame gain vs our past-only = -" as *u8); gn(biVsPast); gw("permille residual ===\n" as *u8) 94 gw(" === multi-ref (best-of-2) gain vs past-only = -" as *u8); gn(uniVsPast); gw("permille ===\n" as *u8) 95 gw(" === B-frame gain OVER multi-ref = -" as *u8); gn(biVsUni); gw("permille (the pure bidirectional prize) ===\n" as *u8) 96 if totBi < totPast { gw("LIAR-KILLER PASS: bidirectional beats past-only (B-prediction does real work)\n" as *u8) } 97 else { gw("LIAR-KILLER WARN: bi did NOT beat past-only -- unexpected on bframe content\n" as *u8) } 98 return 0 99}