code wiki / _hdl_build / nx_intra_pred_gate.nx

nx_intra_pred_gate.nx source

↩ module page · 57 lines · 3488 B

1// nx_intra_pred_gate.nx -- MEASURES the directional-intra-prediction lever on REAL content before any core-codec change. 2// On the real 768x768 H.264-decoded luma frame, for every interior 4x4 block, compare the residual SAD of DC-only 3// prediction (what the codec does today) vs the best of {DC, vertical, horizontal, diagonal-down-right}. The reduction 4// is how much smaller the residual that must be entropy-coded becomes -- the first-order size of the intra win. 5// HONEST: predicts from ORIGINAL neighbor samples (an upper bound; a real codec predicts from reconstructed neighbors), 6// so this bounds the available gain rather than the exact post-quant bitrate. Reusable lib nx_intra_pred is the org. 7import "nx_syscalls.nx" 8import "nx_gate_emit_lib.nx" 9import "nx_intra_pred.nx" 10 11func main() -> i64 { 12 g_puts("nx_intra_pred gate (directional intra prediction residual reduction on a REAL frame, MEASURED)\n" as *u8) 13 var pass: i64 = 0; var total: i64 = 0 14 15 let box: *i64 = sys_mmap(16) as *i64 16 let yuv: *u8 = sys_read_file("/mnt/c/Users/elder/nishi-core/nxc2/knowledge/staging/media/ref_frame0.yuv" as *u8, box) 17 if (yuv as i64) == 0 { g_puts(" FAIL cannot read ref_frame0.yuv\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 } 18 let W: i64 = 768; let H: i64 = 768; let N: i64 = W*H 19 pass = pass + g_check("real asset present" as *u8, box[0] >= N); total=total+1 20 if box[0] < N { g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 } 21 let src: *u8 = yuv // real luma plane 22 23 let pred: *u8 = sys_mmap(16) as *u8 24 let mode: *i64 = sys_mmap(8) as *i64 25 let cnt: *i64 = sys_mmap(IP_NMODES*8) as *i64 26 var ci: i64 = 0; while ci < IP_NMODES { cnt[ci]=0; ci=ci+1 } 27 28 var dc_total: i64 = 0; var best_total: i64 = 0; var nblk: i64 = 0 29 var py: i64 = 4 30 while py + 4 <= H { 31 var px: i64 = 4 32 while px + 4 <= W { 33 ip_predict4(src, W, px, py, IP_DC, pred) 34 dc_total = dc_total + ip_sad4(src, W, px, py, pred) 35 best_total = best_total + ip_best4(src, W, px, py, pred, mode) 36 cnt[mode[0]] = cnt[mode[0]] + 1 37 nblk = nblk + 1 38 px = px + 4 39 } 40 py = py + 4 41 } 42 43 let red: i64 = ((dc_total - best_total) * 100) / dc_total 44 g_puts(" [measure] blocks=" as *u8); g_pn(nblk); g_puts(" DC-only residual SAD=" as *u8); g_pn(dc_total) 45 g_puts(" best-of-4 SAD=" as *u8); g_pn(best_total); g_puts(" reduction=" as *u8); g_pn(red); g_puts("%\n" as *u8) 46 g_puts(" [measure] mode picks: DC=" as *u8); g_pn(cnt[IP_DC]); g_puts(" V=" as *u8); g_pn(cnt[IP_V]) 47 g_puts(" H=" as *u8); g_pn(cnt[IP_H]); g_puts(" DDR=" as *u8); g_pn(cnt[IP_DDR]) 48 g_puts(" (HONEST: predicted from original neighbors = upper bound on the gain)\n" as *u8) 49 50 pass = pass + g_check("best-of-4 never worse than DC-only (mode selection is sound)" as *u8, best_total <= dc_total); total=total+1 51 pass = pass + g_check("directional prediction cuts intra residual >= 15% on real content" as *u8, red >= 15); total=total+1 52 pass = pass + g_check("directional modes are actually chosen (V+H+DDR picked somewhere)" as *u8, (cnt[IP_V]+cnt[IP_H]+cnt[IP_DDR]) > 0); total=total+1 53 54 g_puts("---- intra_pred gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 55 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 56 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 57}