code wiki / (root) / nx_h264_cdc_gate.nx

nx_h264_cdc_gate.nx source

↩ module page · 133 lines · 5037 B

1// nx_h264_cdc_gate.nx -- REFEREE for the chroma-DC coeff_token table (nC=-1). 2// (a) PREFIX-FREE structural validator: no codeword is a prefix of another 3// (catches transcription errors that round-trip can't). 4// (b) COVERAGE: all 14 (TotalCoeff,TrailingOnes) pairs present, distinct. 5// (c) ANCHOR: (0,0)->"01"(0x40,2bit), (1,1)->"1"(0x80,1bit). 6// (d) ROUND-TRIP every entry: encode->decode->recover. 7// Every value PRINTED. stdout + knowledge/status/h264_cdc_gate.log. Exit 0/1. 8// Sovereign: nx_syscalls + nx_h264_bits + nx_h264_bitwriter + nx_h264_coeff_token. 9// license_tier: ORIGINAL 10import "nx_syscalls.nx" 11import "nx_h264_bits.nx" 12import "nx_h264_bitwriter.nx" 13import "nx_h264_coeff_token.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_cdc_gate.log\x00" as *u8, 0x1a4) 39 gp(logfd, "H264-CDC-GATE (chroma DC coeff_token nC=-1)\n\x00" as *u8) 40 var ok: i64 = 1 41 42 let ln: *i64 = sys_mmap(64 * 8) as *i64 43 let cd: *i64 = sys_mmap(64 * 8) as *i64 44 let tc: *i64 = sys_mmap(64 * 8) as *i64 45 let to: *i64 = sys_mmap(64 * 8) as *i64 46 let n: i64 = ct_table_chromadc(ln, cd, tc, to) 47 gp(logfd, " entries=\x00" as *u8); gn(logfd, n); gp(logfd, " (expect 14)\n\x00" as *u8) 48 if n != 14 { ok = 0 } 49 50 // (a) prefix-free: for all i,j (i!=j), code i is not a prefix of code j 51 var pf: i64 = 1 52 var i: i64 = 0 53 while i < n { 54 var j: i64 = 0 55 while j < n { 56 if i != j { 57 if ln[i] <= ln[j] { 58 // top ln[i] bits of cd[j] == cd[i] ? 59 let shift: i64 = ln[j] - ln[i] 60 if (cd[j] >> shift) == cd[i] { pf = 0 } 61 } 62 } 63 j = j + 1 64 } 65 i = i + 1 66 } 67 gp(logfd, " prefix_free=\x00" as *u8); gn(logfd, pf); gp(logfd, " (expect 1)\n\x00" as *u8) 68 if pf != 1 { ok = 0 } 69 70 // (b) coverage: all 14 valid (tc,t1) pairs present 71 var cov: i64 = 1 72 var t: i64 = 0 73 while t <= 4 { 74 var u: i64 = 0 75 while u <= t { 76 if u <= 3 { 77 var found: i64 = 0 78 var z: i64 = 0 79 while z < n { if tc[z] == t { if to[z] == u { found = 1 } } z = z + 1 } 80 if found == 0 { cov = 0 } 81 } 82 u = u + 1 83 } 84 t = t + 1 85 } 86 gp(logfd, " coverage=\x00" as *u8); gn(logfd, cov); gp(logfd, " (expect 1)\n\x00" as *u8) 87 if cov != 1 { ok = 0 } 88 89 // (c) anchors 90 let ba: *u8 = sys_mmap(8) 91 let wa: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter 92 bw_init(wa, ba, 8); nx_coeff_token_encode_cdc(wa, 0, 0) 93 gp(logfd, " anchor (0,0): byte=\x00" as *u8); gn(logfd, ba[0] as i64); gp(logfd, " bits=\x00" as *u8); gn(logfd, bw_total_bits(wa)); gp(logfd, " (expect 64,2)\n\x00" as *u8) 94 if (ba[0] as i64) != 64 { ok = 0 } 95 if bw_total_bits(wa) != 2 { ok = 0 } 96 let bb2: *u8 = sys_mmap(8) 97 let wb: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter 98 bw_init(wb, bb2, 8); nx_coeff_token_encode_cdc(wb, 1, 1) 99 gp(logfd, " anchor (1,1): byte=\x00" as *u8); gn(logfd, bb2[0] as i64); gp(logfd, " bits=\x00" as *u8); gn(logfd, bw_total_bits(wb)); gp(logfd, " (expect 128,1)\n\x00" as *u8) 100 if (bb2[0] as i64) != 128 { ok = 0 } 101 if bw_total_bits(wb) != 1 { ok = 0 } 102 103 // (d) round-trip every entry 104 var rt: i64 = 1 105 i = 0 106 while i < n { 107 let buf: *u8 = sys_mmap(8) 108 let bw: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter 109 bw_init(bw, buf, 8) 110 nx_coeff_token_encode_cdc(bw, tc[i], to[i]) 111 let br: *BitReader = sys_mmap(NX_BR_BYTES) as *BitReader 112 br_init(br, buf, 8) 113 let out: *i64 = sys_mmap(16) as *i64 114 let d: i64 = nx_coeff_token_decode_cdc(br, out) 115 if d != 1 { rt = 0 } 116 if out[0] != tc[i] { rt = 0 } 117 if out[1] != to[i] { rt = 0 } 118 i = i + 1 119 } 120 gp(logfd, " round_trip_all=\x00" as *u8); gn(logfd, rt); gp(logfd, " (expect 1)\n\x00" as *u8) 121 if rt != 1 { ok = 0 } 122 123 if ok == 1 { 124 gp(logfd, "H264-CDC-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8) 125 if logfd > 0 { sys_close(logfd) } 126 sys_exit(0) 127 return 0 128 } 129 gp(logfd, "H264-CDC-GATE result=FAIL verdict=RED\n\x00" as *u8) 130 if logfd > 0 { sys_close(logfd) } 131 sys_exit(1) 132 return 1 133}