nx_h264_run_before_gate.nx source
↩ module page · 95 lines · 3842 B
1// nx_h264_run_before_gate.nx -- REFEREE for run_before VLC (rung 4c-iv).
2// (a) ANCHOR bit-exact: zerosLeft=1 run0 -> "1"(0x80,1) run1 -> "0"(0x00,1);
3// zerosLeft=3 run0 -> "11"(0xC0,2).
4// (b) ROUND-TRIP all valid (zerosLeft, run_before) for zerosLeft 1,2,3.
5// Every value PRINTED. stdout + knowledge/status/h264_run_before_gate.log. Exit 0/1.
6// Sovereign: nx_syscalls + nx_h264_bits + nx_h264_bitwriter + nx_h264_run_before.
7// license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_h264_bits.nx"
10import "nx_h264_bitwriter.nx"
11import "nx_h264_run_before.nx"
12
13func gp(logfd: i64, s: *u8) -> i64 {
14 var n: i64 = 0
15 while s[n] != (0 as u8) { n = n + 1 }
16 sys_write(1, s, n)
17 if logfd > 0 { sys_write(logfd, s, n) }
18 return 0
19}
20func gn(logfd: i64, v: i64) -> i64 {
21 let bb: *u8 = sys_mmap(28)
22 var m: i64 = v
23 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m }
24 let t: *u8 = sys_mmap(28)
25 var k: i64 = 0
26 if m == 0 { t[0] = 48 as u8; k = 1 }
27 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
28 var i: i64 = 0
29 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
30 sys_write(1, bb, k)
31 if logfd > 0 { sys_write(logfd, bb, k) }
32 return 0
33}
34func enc1(zl: i64, rb: i64, byteout: *i64, bitsout: *i64) -> i64 {
35 let buf: *u8 = sys_mmap(8)
36 let bw: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter
37 bw_init(bw, buf, 8)
38 nx_run_before_encode(bw, zl, rb)
39 byteout[0] = buf[0] as i64
40 bitsout[0] = bw_total_bits(bw)
41 return 0
42}
43
44func main() -> i64 {
45 let logfd: i64 = sys_openat_append("knowledge/status/h264_run_before_gate.log\x00" as *u8, 0x1a4)
46 gp(logfd, "H264-RUN-BEFORE-GATE (rung 4c-iv, zerosLeft 1..3)\n\x00" as *u8)
47 var ok: i64 = 1
48 let by: *i64 = sys_mmap(8) as *i64
49 let bt: *i64 = sys_mmap(8) as *i64
50
51 enc1(1, 0, by, bt)
52 gp(logfd, " anchor zl1 r0: byte=\x00" as *u8); gn(logfd, by[0]); gp(logfd, " bits=\x00" as *u8); gn(logfd, bt[0]); gp(logfd, " (expect 128,1)\n\x00" as *u8)
53 if by[0] != 128 { ok = 0 }
54 if bt[0] != 1 { ok = 0 }
55 enc1(1, 1, by, bt)
56 gp(logfd, " anchor zl1 r1: byte=\x00" as *u8); gn(logfd, by[0]); gp(logfd, " bits=\x00" as *u8); gn(logfd, bt[0]); gp(logfd, " (expect 0,1)\n\x00" as *u8)
57 if by[0] != 0 { ok = 0 }
58 if bt[0] != 1 { ok = 0 }
59 enc1(3, 0, by, bt)
60 gp(logfd, " anchor zl3 r0: byte=\x00" as *u8); gn(logfd, by[0]); gp(logfd, " bits=\x00" as *u8); gn(logfd, bt[0]); gp(logfd, " (expect 192,2)\n\x00" as *u8)
61 if by[0] != 192 { ok = 0 }
62 if bt[0] != 2 { ok = 0 }
63
64 // round-trip: zerosLeft 1..3, run_before 0..zerosLeft
65 gp(logfd, " round-trip:\n\x00" as *u8)
66 var zl: i64 = 1
67 while zl <= 3 {
68 var rb: i64 = 0
69 while rb <= zl {
70 let buf: *u8 = sys_mmap(8)
71 let bw: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter
72 bw_init(bw, buf, 8)
73 let e: i64 = nx_run_before_encode(bw, zl, rb)
74 let br: *BitReader = sys_mmap(NX_BR_BYTES) as *BitReader
75 br_init(br, buf, 8)
76 let got: i64 = nx_run_before_decode(br, zl)
77 if e != 1 { ok = 0 }
78 if got != rb { ok = 0; gp(logfd, " zl=\x00" as *u8); gn(logfd, zl); gp(logfd, " rb=\x00" as *u8); gn(logfd, rb); gp(logfd, " -> \x00" as *u8); gn(logfd, got); gp(logfd, " MISMATCH\n\x00" as *u8) }
79 rb = rb + 1
80 }
81 zl = zl + 1
82 }
83 gp(logfd, " (all round-tripped)\n\x00" as *u8)
84
85 if ok == 1 {
86 gp(logfd, "H264-RUN-BEFORE-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8)
87 if logfd > 0 { sys_close(logfd) }
88 sys_exit(0)
89 return 0
90 }
91 gp(logfd, "H264-RUN-BEFORE-GATE result=FAIL verdict=RED\n\x00" as *u8)
92 if logfd > 0 { sys_close(logfd) }
93 sys_exit(1)
94 return 1
95}