nx_h264_deblock_gate.nx source
↩ module page · 90 lines · 3798 B
1// nx_h264_deblock_gate.nx -- REFEREE for the normal luma deblocking filter.
2// Hand-derived from the REAL MB edge (139|142, bS=3, QP=31):
3// alpha[31]=28, beta[31]=8, tc0[3][31]=3; ap=aq=0<beta -> tc=5;
4// delta=Clip3(-5,5,13>>3=1)=1 -> p0=140,q0=141; p1=140,q1=141.
5// line [139,139,139,139,142,142,142,142] -> [139,139,140,140,141,141,142,142]
6// Plus: a flat edge (all 142) -> unchanged (|p0-q0|=0, but filter runs; delta=0 -> no change).
7// And table anchors: alpha[31]=28, beta[31]=8, tc0(3,31)=3, alpha[16]=4.
8// NOTE 2026-06-17: beta table was CORRECTED (memorized values were wrong from qp>=22;
9// beta(31) is 8 not 9, ffmpeg h264_loopfilter.c verbatim). This corrected the full-frame
10// deblock to 100% bit-exact. Memorized deblock tables = unreliable, same lesson as VLC tables.
11// Every value PRINTED. stdout + knowledge/status/h264_deblock_gate.log. Exit 0/1.
12// Sovereign: nx_syscalls + nx_h264_deblock. license_tier: ORIGINAL
13import "nx_syscalls.nx"
14import "nx_h264_deblock.nx"
15
16func gp(logfd: i64, s: *u8) -> i64 {
17 var n: i64 = 0
18 while s[n] != (0 as u8) { n = n + 1 }
19 sys_write(1, s, n)
20 if logfd > 0 { sys_write(logfd, s, n) }
21 return 0
22}
23func gn(logfd: i64, v: i64) -> i64 {
24 let bb: *u8 = sys_mmap(28)
25 var m: i64 = v
26 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m }
27 let t: *u8 = sys_mmap(28)
28 var k: i64 = 0
29 if m == 0 { t[0] = 48 as u8; k = 1 }
30 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
31 var i: i64 = 0
32 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
33 sys_write(1, bb, k)
34 if logfd > 0 { sys_write(logfd, bb, k) }
35 return 0
36}
37
38func main() -> i64 {
39 let logfd: i64 = sys_openat_append("knowledge/status/h264_deblock_gate.log\x00" as *u8, 0x1a4)
40 gp(logfd, "H264-DEBLOCK-GATE (normal luma edge)\n\x00" as *u8)
41 var ok: i64 = 1
42
43 // table anchors
44 let a31: i64 = nx_db_alpha(31)
45 let b31: i64 = nx_db_beta(31)
46 let t31: i64 = nx_db_tc0(3, 31)
47 let a16: i64 = nx_db_alpha(16)
48 gp(logfd, " tables: alpha[31]=\x00" as *u8); gn(logfd, a31); gp(logfd, " beta[31]=\x00" as *u8); gn(logfd, b31); gp(logfd, " tc0(3,31)=\x00" as *u8); gn(logfd, t31); gp(logfd, " alpha[16]=\x00" as *u8); gn(logfd, a16); gp(logfd, " (expect 28 8 3 4 -- beta(31)=8 ffmpeg-verified, was wrongly 9)\n\x00" as *u8)
49 if a31 != 28 { ok = 0 }
50 if b31 != 8 { ok = 0 }
51 if t31 != 3 { ok = 0 }
52 if a16 != 4 { ok = 0 }
53
54 // the real-MB edge KAT
55 let s: *i64 = sys_mmap(8 * 8) as *i64
56 s[0]=139; s[1]=139; s[2]=139; s[3]=139; s[4]=142; s[5]=142; s[6]=142; s[7]=142
57 nx_deblock_luma_edge(s, 3, 31)
58 gp(logfd, " edge 139|142 ->\x00" as *u8)
59 var i: i64 = 0
60 while i < 8 { gp(logfd, " \x00" as *u8); gn(logfd, s[i]); i = i + 1 }
61 gp(logfd, " (expect 139 139 140 140 141 141 142 142)\n\x00" as *u8)
62 if s[2] != 140 { ok = 0 }
63 if s[3] != 140 { ok = 0 }
64 if s[4] != 141 { ok = 0 }
65 if s[5] != 141 { ok = 0 }
66 if s[1] != 139 { ok = 0 }
67 if s[6] != 142 { ok = 0 }
68
69 // flat edge -> unchanged
70 let f: *i64 = sys_mmap(8 * 8) as *i64
71 var j: i64 = 0
72 while j < 8 { f[j] = 142; j = j + 1 }
73 nx_deblock_luma_edge(f, 3, 31)
74 var flat_ok: i64 = 1
75 j = 0
76 while j < 8 { if f[j] != 142 { flat_ok = 0 } j = j + 1 }
77 gp(logfd, " flat edge unchanged=\x00" as *u8); gn(logfd, flat_ok); gp(logfd, "\n\x00" as *u8)
78 if flat_ok != 1 { ok = 0 }
79
80 if ok == 1 {
81 gp(logfd, "H264-DEBLOCK-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8)
82 if logfd > 0 { sys_close(logfd) }
83 sys_exit(0)
84 return 0
85 }
86 gp(logfd, "H264-DEBLOCK-GATE result=FAIL verdict=RED\n\x00" as *u8)
87 if logfd > 0 { sys_close(logfd) }
88 sys_exit(1)
89 return 1
90}