code wiki / (root) / nx_h264_mb_pred_gate.nx

nx_h264_mb_pred_gate.nx source

↩ module page · 116 lines · 4841 B

1// nx_h264_mb_pred_gate.nx -- REFEREE for the I_NxN prediction layer (rung 4e). 2// (a) CBP BIJECTION (strong self-check): map all 48 codeNums -> cbp; every cbp in 3// 0..47 hit exactly once; unmap(map(c))==c for all. A bad table entry breaks this. 4// (b) CBP anchors: map(0)=47, map(3)=0, map(2)=15. 5// (c) CBP round-trip: encode(cbp)->decode->recover for several cbp. 6// (d) I_NxN pred-mode round-trip: modes[16] (mix of -1 and 0..7) + chroma -> enc -> dec -> recover. 7// Every value PRINTED. stdout + knowledge/status/h264_mb_pred_gate.log. Exit 0/1. 8// Sovereign: nx_syscalls + nx_h264_bits + nx_h264_bitwriter + nx_h264_mb_pred. 9// license_tier: ORIGINAL 10import "nx_syscalls.nx" 11import "nx_h264_bits.nx" 12import "nx_h264_bitwriter.nx" 13import "nx_h264_mb_pred.nx" 14 15func gp(logfd: i64, s: *u8) -> i64 { 16 var n: i64 = 0 17 while s[n] != (0 as u8) { n = n + 1 } 18 sys_write(1, s, n) 19 if logfd > 0 { sys_write(logfd, s, n) } 20 return 0 21} 22func gn(logfd: i64, v: i64) -> i64 { 23 let bb: *u8 = sys_mmap(28) 24 var m: i64 = v 25 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m } 26 let t: *u8 = sys_mmap(28) 27 var k: i64 = 0 28 if m == 0 { t[0] = 48 as u8; k = 1 } 29 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 30 var i: i64 = 0 31 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 32 sys_write(1, bb, k) 33 if logfd > 0 { sys_write(logfd, bb, k) } 34 return 0 35} 36 37func main() -> i64 { 38 let logfd: i64 = sys_openat_append("knowledge/status/h264_mb_pred_gate.log\x00" as *u8, 0x1a4) 39 gp(logfd, "H264-MB-PRED-GATE (rung 4e I_NxN pred + me(v) CBP)\n\x00" as *u8) 40 var ok: i64 = 1 41 42 // (a) bijection 43 let seen: *i64 = sys_mmap(48 * 8) as *i64 44 var i: i64 = 0 45 while i < 48 { seen[i] = 0; i = i + 1 } 46 var bij: i64 = 1 47 i = 0 48 while i < 48 { 49 let c: i64 = nx_h264_cbp_intra_map(i) 50 if c < 0 { bij = 0 } 51 if c > 47 { bij = 0 } 52 if c >= 0 { if c <= 47 { seen[c] = seen[c] + 1 } } 53 if nx_h264_cbp_intra_unmap(c) != i { bij = 0 } 54 i = i + 1 55 } 56 i = 0 57 while i < 48 { if seen[i] != 1 { bij = 0 } i = i + 1 } 58 gp(logfd, " CBP bijection over 48 codeNums: \x00" as *u8); gn(logfd, bij); gp(logfd, " (expect 1)\n\x00" as *u8) 59 if bij != 1 { ok = 0 } 60 61 // (b) anchors 62 let a0: i64 = nx_h264_cbp_intra_map(0) 63 let a3: i64 = nx_h264_cbp_intra_map(3) 64 let a2: i64 = nx_h264_cbp_intra_map(2) 65 gp(logfd, " anchors map(0)=\x00" as *u8); gn(logfd, a0); gp(logfd, " map(3)=\x00" as *u8); gn(logfd, a3); gp(logfd, " map(2)=\x00" as *u8); gn(logfd, a2); gp(logfd, " (expect 47 0 15)\n\x00" as *u8) 66 if a0 != 47 { ok = 0 } 67 if a3 != 0 { ok = 0 } 68 if a2 != 15 { ok = 0 } 69 70 // (c) CBP round-trip 71 let cbps: *i64 = sys_mmap(16 * 8) as *i64 72 cbps[0]=0; cbps[1]=15; cbps[2]=31; cbps[3]=47; cbps[4]=23; cbps[5]=5; cbps[6]=42 73 var ci: i64 = 0 74 while ci < 7 { 75 let buf: *u8 = sys_mmap(8) 76 let bw: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter 77 bw_init(bw, buf, 8) 78 nx_h264_encode_cbp_intra(bw, cbps[ci]) 79 let br: *BitReader = sys_mmap(NX_BR_BYTES) as *BitReader 80 br_init(br, buf, 8) 81 let got: i64 = nx_h264_decode_cbp_intra(br) 82 if got != cbps[ci] { ok = 0; gp(logfd, " cbp rt \x00" as *u8); gn(logfd, cbps[ci]); gp(logfd, "->\x00" as *u8); gn(logfd, got); gp(logfd, " MISMATCH\n\x00" as *u8) } 83 ci = ci + 1 84 } 85 gp(logfd, " CBP round-trip (7 values) done\n\x00" as *u8) 86 87 // (d) I_NxN pred-mode round-trip 88 let modes: *i64 = sys_mmap(20 * 8) as *i64 89 modes[0]=0-1; modes[1]=2; modes[2]=0-1; modes[3]=7; modes[4]=0; modes[5]=0-1; modes[6]=5; modes[7]=3 90 modes[8]=0-1; modes[9]=0-1; modes[10]=1; modes[11]=6; modes[12]=4; modes[13]=0-1; modes[14]=0; modes[15]=2 91 modes[16]=1 // intra_chroma_pred_mode 92 let buf2: *u8 = sys_mmap(16) 93 let bw2: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter 94 bw_init(bw2, buf2, 16) 95 nx_h264_encode_inxn_predmodes(bw2, modes) 96 let br2: *BitReader = sys_mmap(NX_BR_BYTES) as *BitReader 97 br_init(br2, buf2, 16) 98 let dm: *i64 = sys_mmap(20 * 8) as *i64 99 nx_h264_parse_inxn_predmodes(br2, dm) 100 var pm_ok: i64 = 1 101 i = 0 102 while i < 17 { if dm[i] != modes[i] { pm_ok = 0 } i = i + 1 } 103 gp(logfd, " pred-mode round-trip ok=\x00" as *u8); gn(logfd, pm_ok); gp(logfd, " chroma=\x00" as *u8); gn(logfd, dm[16]); gp(logfd, "\n\x00" as *u8) 104 if pm_ok != 1 { ok = 0 } 105 106 if ok == 1 { 107 gp(logfd, "H264-MB-PRED-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8) 108 if logfd > 0 { sys_close(logfd) } 109 sys_exit(0) 110 return 0 111 } 112 gp(logfd, "H264-MB-PRED-GATE result=FAIL verdict=RED\n\x00" as *u8) 113 if logfd > 0 { sys_close(logfd) } 114 sys_exit(1) 115 return 1 116}