nx_h264_block_e2e_gate.nx source
↩ module page · 119 lines · 4840 B
1// nx_h264_block_e2e_gate.nx -- SINGLE 4x4 BLOCK end-to-end micro-decode.
2// A real coefficient block pushed through the ENTIRE sovereign pipeline:
3// scan coeff[0]=5 --CAVLC encode--> bits --CAVLC decode--> scan' (round-trip)
4// --inverse zigzag--> raster --dequant(QP0)--> coeffs --IDCT--> residual
5// --(+ intra DC pred)--> --clip--> reconstructed pixels.
6// Hand-computed expected values (every step):
7// decode scan[0]=5 ; raster[0]=5 ; dequant DC = (5*160+8)>>4 = 50 ;
8// IDCT(DC=50) -> residual 1 everywhere ; intra DC pred(top sum100,left sum260)=45 ;
9// pixel = clip(45+1) = 46 (all 16).
10// Every value PRINTED. stdout + knowledge/status/h264_block_e2e_gate.log. Exit 0/1.
11// Sovereign: nx_syscalls + bits + bitwriter + residual + zigzag + dequant + idct + intra.
12// license_tier: ORIGINAL
13import "nx_syscalls.nx"
14import "nx_h264_bits.nx"
15import "nx_h264_bitwriter.nx"
16import "nx_h264_residual.nx"
17import "nx_h264_zigzag.nx"
18import "nx_h264_dequant.nx"
19import "nx_h264_idct.nx"
20import "nx_h264_intra.nx"
21
22func gp(logfd: i64, s: *u8) -> i64 {
23 var n: i64 = 0
24 while s[n] != (0 as u8) { n = n + 1 }
25 sys_write(1, s, n)
26 if logfd > 0 { sys_write(logfd, s, n) }
27 return 0
28}
29func gn(logfd: i64, v: i64) -> i64 {
30 let bb: *u8 = sys_mmap(28)
31 var m: i64 = v
32 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m }
33 let t: *u8 = sys_mmap(28)
34 var k: i64 = 0
35 if m == 0 { t[0] = 48 as u8; k = 1 }
36 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
37 var i: i64 = 0
38 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
39 sys_write(1, bb, k)
40 if logfd > 0 { sys_write(logfd, bb, k) }
41 return 0
42}
43
44func main() -> i64 {
45 let logfd: i64 = sys_openat_append("knowledge/status/h264_block_e2e_gate.log\x00" as *u8, 0x1a4)
46 gp(logfd, "H264-BLOCK-E2E-GATE (one 4x4 block, bitstream -> pixels)\n\x00" as *u8)
47 var ok: i64 = 1
48
49 // source scan-order block: DC coeff 5
50 let scan: *i64 = sys_mmap(16 * 8) as *i64
51 var z: i64 = 0
52 while z < 16 { scan[z] = 0; z = z + 1 }
53 scan[0] = 5
54
55 // CAVLC encode -> bits -> decode
56 let buf: *u8 = sys_mmap(32)
57 let bw: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter
58 bw_init(bw, buf, 32)
59 nx_h264_residual_encode(bw, scan, 16, 0)
60 let nbits: i64 = bw_total_bits(bw)
61 let br: *BitReader = sys_mmap(NX_BR_BYTES) as *BitReader
62 br_init(br, buf, 32)
63 let dscan: *i64 = sys_mmap(16 * 8) as *i64
64 let tc: i64 = nx_h264_residual_decode(br, 16, 0, dscan)
65 gp(logfd, " CAVLC: bits=\x00" as *u8); gn(logfd, nbits); gp(logfd, " tc=\x00" as *u8); gn(logfd, tc); gp(logfd, " scan[0]=\x00" as *u8); gn(logfd, dscan[0]); gp(logfd, "\n\x00" as *u8)
66 if tc != 1 { ok = 0 }
67 if dscan[0] != 5 { ok = 0 }
68
69 // inverse zigzag -> raster
70 let raster: *i64 = sys_mmap(16 * 8) as *i64
71 nx_h264_inv_zigzag4x4(dscan, raster)
72 if raster[0] != 5 { ok = 0 }
73
74 // dequant (QP 0)
75 let dq: *i64 = sys_mmap(16 * 8) as *i64
76 nx_h264_dequant4x4(raster, 0, dq)
77 let dq_dc: i64 = dq[0]
78 gp(logfd, " dequant DC=\x00" as *u8); gn(logfd, dq_dc); gp(logfd, " (expect 50)\n\x00" as *u8)
79 if dq_dc != 50 { ok = 0 }
80
81 // IDCT (in place) -> residual
82 nx_idct4x4(dq)
83 gp(logfd, " IDCT residual[0]=\x00" as *u8); gn(logfd, dq[0]); gp(logfd, " [15]=\x00" as *u8); gn(logfd, dq[15]); gp(logfd, " (expect 1)\n\x00" as *u8)
84 var res_ok: i64 = 1
85 var i: i64 = 0
86 while i < 16 { if dq[i] != 1 { res_ok = 0 } i = i + 1 }
87 if res_ok != 1 { ok = 0 }
88
89 // intra DC prediction (top sum 100, left sum 260 -> 45)
90 let top: *i64 = sys_mmap(8 * 8) as *i64
91 let left: *i64 = sys_mmap(8 * 8) as *i64
92 top[0]=10; top[1]=20; top[2]=30; top[3]=40
93 left[0]=50; left[1]=60; left[2]=70; left[3]=80
94 let pred: *i64 = sys_mmap(16 * 8) as *i64
95 nx_intra4x4_pred(2, top, left, 1, 1, pred)
96 let predU: *u8 = sys_mmap(16)
97 i = 0
98 while i < 16 { predU[i] = pred[i] as u8; i = i + 1 }
99
100 // reconstruct = clip(pred + residual)
101 let outU: *u8 = sys_mmap(16)
102 nx_idct_add_clip(predU, dq, outU, 16)
103 gp(logfd, " RECON pred=\x00" as *u8); gn(logfd, pred[0]); gp(logfd, " +res=\x00" as *u8); gn(logfd, dq[0]); gp(logfd, " -> pixel=\x00" as *u8); gn(logfd, outU[0] as i64); gp(logfd, " (all16, expect 46)\n\x00" as *u8)
104 var pix_ok: i64 = 1
105 i = 0
106 while i < 16 { if (outU[i] as i64) != 46 { pix_ok = 0 } i = i + 1 }
107 if pix_ok != 1 { ok = 0 }
108
109 if ok == 1 {
110 gp(logfd, "H264-BLOCK-E2E-GATE result=ALL-PASS verdict=GREEN (bitstream->pixels)\n\x00" as *u8)
111 if logfd > 0 { sys_close(logfd) }
112 sys_exit(0)
113 return 0
114 }
115 gp(logfd, "H264-BLOCK-E2E-GATE result=FAIL verdict=RED\n\x00" as *u8)
116 if logfd > 0 { sys_close(logfd) }
117 sys_exit(1)
118 return 1
119}