code wiki / (root) / nx_h264_ctctx_gate.nx

nx_h264_ctctx_gate.nx source

↩ module page · 168 lines · 5896 B

1// nx_h264_ctctx_gate.nx -- validate ALL coeff_token nC contexts via the dispatcher. 2// For nC in {0,2,4}: prefix-free + coverage + round-trip (encode_ctx<->decode_ctx). 3// For nC=8: FLC round-trip of all 62 valid (tc,t1) + distinct-code (bijection). 4// For nC=-1: chroma DC round-trip (14). 5// Exit 0 GREEN / 1 RED. Log knowledge/status/h264_ctctx_gate.log. 6// license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_h264_bits.nx" 9import "nx_h264_bitwriter.nx" 10import "nx_h264_coeff_token.nx" 11 12func gp(fd: i64, s: *u8) -> i64 { 13 var n: i64 = 0 14 while s[n] != (0 as u8) { n = n + 1 } 15 sys_write(1, s, n) 16 if fd > 0 { sys_write(fd, s, n) } 17 return 0 18} 19func gnum(fd: i64, v: i64) -> i64 { 20 let bb: *u8 = sys_mmap(28) 21 var m: i64 = v 22 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if fd > 0 { sys_write(fd, "-\x00" as *u8, 1) } m = 0 - m } 23 let t: *u8 = sys_mmap(28) 24 var k: i64 = 0 25 if m == 0 { t[0] = 48 as u8; k = 1 } 26 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 27 var i: i64 = 0 28 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 29 sys_write(1, bb, k) 30 if fd > 0 { sys_write(fd, bb, k) } 31 return 0 32} 33 34// validate one VLC context nC (must be 0,2,4). returns 1 ok / 0 fail. 35func validate_vlc_ctx(fd: i64, nC: i64) -> i64 { 36 let ln: *i64 = sys_mmap(80*8) as *i64 37 let cd: *i64 = sys_mmap(80*8) as *i64 38 let tc: *i64 = sys_mmap(80*8) as *i64 39 let to: *i64 = sys_mmap(80*8) as *i64 40 let n: i64 = ct_table_for(nC, ln, cd, tc, to) 41 var ok: i64 = 1 42 gp(fd, " nC=\x00" as *u8); gnum(fd, nC); gp(fd, " count=\x00" as *u8); gnum(fd, n) 43 if n != 62 { ok = 0 } 44 // prefix-free 45 var pf: i64 = 1 46 var i: i64 = 0 47 while i < n { 48 var j: i64 = 0 49 while j < n { 50 if i != j { if ln[i] <= ln[j] { if (cd[j] >> (ln[j] - ln[i])) == cd[i] { pf = 0 } } } 51 j = j + 1 52 } 53 i = i + 1 54 } 55 gp(fd, " prefixfree=\x00" as *u8); gnum(fd, pf) 56 if pf != 1 { ok = 0 } 57 // coverage + round-trip 58 var rt: i64 = 1 59 var t: i64 = 0 60 while t <= 16 { 61 var umax: i64 = t 62 if umax > 3 { umax = 3 } 63 if t == 0 { umax = 0 } 64 var u: i64 = 0 65 while u <= umax { 66 // present? 67 var found: i64 = 0 68 var z: i64 = 0 69 while z < n { if tc[z] == t { if to[z] == u { found = 1 } } z = z + 1 } 70 if found == 0 { rt = 0 } 71 // round-trip 72 let buf: *u8 = sys_mmap(8) 73 let bw: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter 74 bw_init(bw, buf, 8) 75 nx_coeff_token_encode_ctx(bw, nC, t, u) 76 let br: *BitReader = sys_mmap(NX_BR_BYTES) as *BitReader 77 br_init(br, buf, 8) 78 let out: *i64 = sys_mmap(16) as *i64 79 nx_coeff_token_decode_ctx(br, nC, out) 80 if out[0] != t { rt = 0 } 81 if out[1] != u { rt = 0 } 82 u = u + 1 83 } 84 t = t + 1 85 } 86 gp(fd, " cov+roundtrip=\x00" as *u8); gnum(fd, rt); gp(fd, "\n\x00" as *u8) 87 if rt != 1 { ok = 0 } 88 return ok 89} 90 91func main() -> i64 { 92 let fd: i64 = sys_openat_append("knowledge/status/h264_ctctx_gate.log\x00" as *u8, 0x1a4) 93 gp(fd, "H264-CTCTX-GATE (all coeff_token nC contexts)\n\x00" as *u8) 94 var ok: i64 = 1 95 if validate_vlc_ctx(fd, 0) == 0 { ok = 0 } 96 if validate_vlc_ctx(fd, 2) == 0 { ok = 0 } 97 if validate_vlc_ctx(fd, 4) == 0 { ok = 0 } 98 99 // FLC nC>=8: round-trip all valid (tc,t1) + distinct-code bijection 100 var flc: i64 = 1 101 let seen: *i64 = sys_mmap(64*8) as *i64 102 var s: i64 = 0 103 while s < 64 { seen[s] = 0; s = s + 1 } 104 var t: i64 = 0 105 while t <= 16 { 106 var umax: i64 = t 107 if umax > 3 { umax = 3 } 108 if t == 0 { umax = 0 } 109 var u: i64 = 0 110 while u <= umax { 111 let buf: *u8 = sys_mmap(8) 112 let bw: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter 113 bw_init(bw, buf, 8) 114 nx_coeff_token_encode_ctx(bw, 8, t, u) 115 // capture the 6-bit code that was written 116 let code: i64 = (buf[0] as i64) >> 2 // top 6 bits of byte 0 117 if seen[code] != 0 { flc = 0 } // collision -> not a bijection 118 seen[code] = 1 119 let br: *BitReader = sys_mmap(NX_BR_BYTES) as *BitReader 120 br_init(br, buf, 8) 121 let out: *i64 = sys_mmap(16) as *i64 122 nx_coeff_token_decode_ctx(br, 8, out) 123 if out[0] != t { flc = 0 } 124 if out[1] != u { flc = 0 } 125 u = u + 1 126 } 127 t = t + 1 128 } 129 gp(fd, " nC>=8 FLC roundtrip+bijection=\x00" as *u8); gnum(fd, flc); gp(fd, "\n\x00" as *u8) 130 if flc != 1 { ok = 0 } 131 132 // chroma DC nC=-1 133 var cdc: i64 = 1 134 t = 0 135 while t <= 4 { 136 var umax: i64 = t 137 if umax > 3 { umax = 3 } 138 if t == 0 { umax = 0 } 139 var u: i64 = 0 140 while u <= umax { 141 let buf: *u8 = sys_mmap(8) 142 let bw: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter 143 bw_init(bw, buf, 8) 144 nx_coeff_token_encode_ctx(bw, 0 - 1, t, u) 145 let br: *BitReader = sys_mmap(NX_BR_BYTES) as *BitReader 146 br_init(br, buf, 8) 147 let out: *i64 = sys_mmap(16) as *i64 148 nx_coeff_token_decode_ctx(br, 0 - 1, out) 149 if out[0] != t { cdc = 0 } 150 if out[1] != u { cdc = 0 } 151 u = u + 1 152 } 153 t = t + 1 154 } 155 gp(fd, " nC=-1 chromaDC roundtrip=\x00" as *u8); gnum(fd, cdc); gp(fd, "\n\x00" as *u8) 156 if cdc != 1 { ok = 0 } 157 158 if ok == 1 { 159 gp(fd, "H264-CTCTX-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8) 160 if fd > 0 { sys_close(fd) } 161 sys_exit(0) 162 return 0 163 } 164 gp(fd, "H264-CTCTX-GATE result=FAIL verdict=RED\n\x00" as *u8) 165 if fd > 0 { sys_close(fd) } 166 sys_exit(1) 167 return 1 168}