code wiki / _hdl_build / nx_vcodec_dct2_rd_gate.nx

nx_vcodec_dct2_rd_gate.nx source

↩ module page · 127 lines · 6490 B

1// nx_vcodec_dct2_rd_gate.nx -- MEASURED head-to-head: the clean royalty-free integer DCT-II (tf=1) vs the shipped 2// Walsh-Hadamard transform (tf=0), through the SAME real full codec, on the SAME real 768x768 H.264-decoded luma 3// frame, at the SAME QP sweep. Both run via vc_enc_frame_packed_tf / vc_dec_frame_packed_tf (the only difference is 4// the transform). We print both RD curves and the HONEST exceed metric = PSNR gain at MATCHED bitrate (linear 5// interpolation of the DCT-II curve to each WHT operating point -- the BD-PSNR idea, not a cherry-picked QP). 6// Correctness (independent decode bit-exact at every QP, both transforms) is the GREEN bar; the RD gain is MEASURED 7// and printed raw -- never self-scored. [[feedback-no-wave-measured-exceed]] [[feedback-show-raw-output-no-overclaim]] 8import "nx_syscalls.nx" 9import "nx_vcodec.nx" 10import "nx_quality_metric.nx" 11 12func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13func g_pn(v: i64) -> i64 { 14 let b: *u8 = sys_mmap(28); var x: i64 = v 15 if x < 0 { b[0]=45; sys_write(1,b,1); x = 0 - x } 16 if x == 0 { b[0]=48; sys_write(1,b,1); return 0 } 17 var d: i64=0; var y: i64=x 18 while y>0 { d=d+1; y=y/10 } 19 var i: i64=d-1; y=x 20 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 } 21 sys_write(1,b,d); return 0 22} 23func g_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } 24// print a centi-unit value (e.g. centi-dB or ratio*10) as D.DD with sign 25func g_pdb(v: i64) -> i64 { 26 var x: i64 = v 27 if x < 0 { g_puts("-" as *u8); x = 0 - x } 28 g_pn(x/100); g_puts("." as *u8) 29 let f: i64 = x%100; if f < 10 { g_puts("0" as *u8) } g_pn(f) 30 return 0 31} 32func frame_equal(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 } 33 34// encode+decode the real frame at one (qp,tf); store ratio*10 and PSNR(centi-dB) into arrR[idx]/arrP[idx]; print; return bit-exact 35func measure_tf(f0: *u8, W: i64, H: i64, N: i64, qp: i64, tf: i64, eR: *u8, dR: *u8, stream: *u8, blk: *i64, mv: *i64, arrR: *i64, arrP: *i64, idx: i64) -> i64 { 36 let bits: i64 = vc_enc_frame_packed_tf(f0, dR, eR, W, H, qp, 1, 64, stream, blk, mv, tf) 37 let bytes: i64 = (bits + 7) / 8 38 vc_dec_frame_packed_tf(dR, dR, W, H, qp, stream, blk, mv, tf) 39 let exact: i64 = frame_equal(eR, dR, N) 40 let psnr: i64 = qm_psnr_cdb(eR, f0, N) 41 let ratio_x10: i64 = (N * 10) / bytes 42 arrR[idx] = ratio_x10 43 arrP[idx] = psnr 44 g_puts(" QP=" as *u8); g_pn(qp) 45 if tf == 1 { g_puts(" [DCT-II] " as *u8) } else { g_puts(" [WHT] " as *u8) } 46 g_puts("stream=" as *u8); g_pn(bytes); g_puts("B ratio=" as *u8); g_pdb(ratio_x10*10) 47 g_puts("x PSNR=" as *u8); g_pdb(psnr); g_puts("dB bit-exact=" as *u8); g_pn(exact); g_puts("\n" as *u8) 48 return exact 49} 50 51// interpolate the DCT-II PSNR (centi-dB) at a target ratio rt over the (R1,P1) curve (R1 increasing). clamp at ends. 52func interp_psnr(R1: *i64, P1: *i64, n: i64, rt: i64) -> i64 { 53 if rt <= R1[0] { return P1[0] } 54 if rt >= R1[n-1] { return P1[n-1] } 55 var j: i64 = 0 56 while j < n-1 { 57 if rt >= R1[j] { if rt <= R1[j+1] { 58 let dr: i64 = R1[j+1]-R1[j] 59 if dr == 0 { return P1[j] } 60 return P1[j] + ((P1[j+1]-P1[j])*(rt-R1[j]))/dr 61 } } 62 j = j + 1 63 } 64 return P1[n-1] 65} 66 67func main() -> i64 { 68 g_puts("=== DCT-II vs WHT -- MEASURED RD head-to-head (same codec, same real frame, same QP sweep) ===\n" as *u8) 69 let box: *i64 = sys_mmap(16) as *i64 70 let yuv: *u8 = sys_read_file("/mnt/c/Users/elder/nishi-core/nxc2/knowledge/staging/media/ref_frame0.yuv" as *u8, box) 71 if (yuv as i64) == 0 { g_puts(" FAIL cannot read ref_frame0.yuv\nverdict=RED\n" as *u8); sys_exit(1); return 1 } 72 let flen: i64 = box[0] 73 let W: i64 = 768; let H: i64 = 768; let N: i64 = W*H 74 if flen < N { g_puts(" FAIL frame too small\nverdict=RED\n" as *u8); sys_exit(1); return 1 } 75 76 let f0: *u8 = yuv 77 let eR: *u8 = sys_mmap(N) as *u8 78 let dR: *u8 = sys_mmap(N) as *u8 79 let stream: *u8 = sys_mmap(4194304) as *u8 80 let blk: *i64 = sys_mmap(16*8) as *i64 81 let mv: *i64 = sys_mmap(2*8) as *i64 82 let qps: *i64 = sys_mmap(8*8) as *i64 83 qps[0]=12; qps[1]=22; qps[2]=32; qps[3]=40; qps[4]=48 84 let R0: *i64 = sys_mmap(8*8) as *i64 // WHT ratio*10 85 let P0: *i64 = sys_mmap(8*8) as *i64 // WHT psnr cdb 86 let R1: *i64 = sys_mmap(8*8) as *i64 // DCT-II 87 let P1: *i64 = sys_mmap(8*8) as *i64 88 89 var allexact: i64 = 1 90 g_puts("-- tf=0: clean Walsh-Hadamard (shipped, patent-clean) --\n" as *u8) 91 var i: i64 = 0 92 while i < 5 { let e: i64 = measure_tf(f0,W,H,N,qps[i],0,eR,dR,stream,blk,mv,R0,P0,i); if e==0 { allexact=0 } i=i+1 } 93 g_puts("-- tf=1: clean royalty-free integer DCT-II (1974 prior art, non-standard scale) --\n" as *u8) 94 i = 0 95 while i < 5 { let e: i64 = measure_tf(f0,W,H,N,qps[i],1,eR,dR,stream,blk,mv,R1,P1,i); if e==0 { allexact=0 } i=i+1 } 96 97 g_puts("-- HEAD-TO-HEAD: DCT-II PSNR gain at MATCHED bitrate (interpolated to each WHT point) --\n" as *u8) 98 var sumgain: i64 = 0; var cnt: i64 = 0 99 i = 0 100 while i < 5 { 101 let rt: i64 = R0[i] 102 let dpsnr: i64 = interp_psnr(R1, P1, 5, rt) 103 let gain: i64 = dpsnr - P0[i] 104 g_puts(" at ratio " as *u8); g_pdb(rt*10); g_puts("x: WHT " as *u8); g_pdb(P0[i]) 105 g_puts("dB -> DCT-II " as *u8); g_pdb(dpsnr); g_puts("dB gain " as *u8); g_pdb(gain); g_puts("dB\n" as *u8) 106 var inrange: i64 = 1 107 if rt < R1[0] { inrange = 0 } 108 if rt > R1[4] { inrange = 0 } 109 if inrange == 1 { sumgain = sumgain + gain; cnt = cnt + 1 } 110 i = i + 1 111 } 112 g_puts("----\n" as *u8) 113 if cnt > 0 { 114 let avg: i64 = sumgain / cnt 115 g_puts("MEASURED: average DCT-II RD gain at matched bitrate = " as *u8); g_pdb(avg); g_puts(" dB over " as *u8); g_pn(cnt) 116 g_puts(" matched points (positive => DCT-II compacts energy better than WHT on real content)\n" as *u8) 117 } else { 118 g_puts("NOTE: WHT and DCT-II ratio ranges did not overlap -> recalibrate DCT2_QSHIFT for a fair matched-rate compare\n" as *u8) 119 } 120 121 if allexact == 1 { 122 g_puts("verdict=GREEN (both transforms independent-decode bit-exact at every QP; RD gain MEASURED above, shown raw)\n" as *u8) 123 sys_exit(0); return 0 124 } 125 g_puts("verdict=RED (a transform failed independent-decode bit-exactness)\n" as *u8) 126 sys_exit(1); return 1 127}