nx_h264_chroma_dc_gate.nx source
↩ module page · 90 lines · 3704 B
1// nx_h264_chroma_dc_gate.nx -- validate chroma DC scaling (spec 8.5.11.2).
2// Oracles (hand-computed from the spec, independent of the impl loop):
3// c=[1,0,0,0] qP=0 -> f=[1,1,1,1], LS=160, sh=0 -> dcC=asr(160,5)=5 each
4// c=[1,0,0,0] qP=6 -> sh=1 -> dcC=asr(320,5)=10 each
5// c=[0,0,0,1] qP=0 -> f=[1,-1,-1,1] -> dcC=[5,-5,-5,5]
6// linearity: scale(2c)==2*scale(c)
7// Exit 0/1. license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_h264_chroma.nx"
10
11func gp(fd: i64, s: *u8) -> i64 {
12 var n: i64 = 0
13 while s[n] != (0 as u8) { n = n + 1 }
14 sys_write(1, s, n); if fd > 0 { sys_write(fd, s, n) }
15 return 0
16}
17func gnum(fd: i64, v: i64) -> i64 {
18 let bb: *u8 = sys_mmap(28); var m: i64 = v
19 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if fd > 0 { sys_write(fd, "-\x00" as *u8, 1) } m = 0 - m }
20 let t: *u8 = sys_mmap(28); var k: i64 = 0
21 if m == 0 { t[0] = 48 as u8; k = 1 }
22 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
23 var i: i64 = 0
24 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
25 sys_write(1, bb, k); if fd > 0 { sys_write(fd, bb, k) }
26 return 0
27}
28
29func setc(c: *i64, a: i64, b: i64, d: i64, e: i64) -> i64 { c[0]=a; c[1]=b; c[2]=d; c[3]=e; return 0 }
30
31func main() -> i64 {
32 let fd: i64 = sys_openat_append("knowledge/status/h264_chroma_dc_gate.log\x00" as *u8, 0x1a4)
33 gp(fd, "H264-CHROMA-DC-GATE (scaling 8.5.11.2)\n\x00" as *u8)
34 var ok: i64 = 1
35 let c: *i64 = sys_mmap(4*8) as *i64
36 let out: *i64 = sys_mmap(4*8) as *i64
37
38 // c=[1,0,0,0] qP=0 -> all 5
39 setc(c, 1, 0, 0, 0)
40 nx_h264_chroma_dc_scale(c, 0, out)
41 var t1: i64 = 1
42 var i: i64 = 0
43 while i < 4 { if out[i] != 5 { t1 = 0 } i = i + 1 }
44 gp(fd, " c=[1,0,0,0] qP=0 -> [\x00" as *u8); gnum(fd, out[0]); gp(fd, " \x00" as *u8); gnum(fd, out[1]); gp(fd, " \x00" as *u8); gnum(fd, out[2]); gp(fd, " \x00" as *u8); gnum(fd, out[3]); gp(fd, "] expect all 5 ok=\x00" as *u8); gnum(fd, t1); gp(fd, "\n\x00" as *u8)
45 if t1 != 1 { ok = 0 }
46
47 // c=[1,0,0,0] qP=6 -> all 10
48 setc(c, 1, 0, 0, 0)
49 nx_h264_chroma_dc_scale(c, 6, out)
50 var t2: i64 = 1
51 i = 0
52 while i < 4 { if out[i] != 10 { t2 = 0 } i = i + 1 }
53 gp(fd, " c=[1,0,0,0] qP=6 -> all 10 ok=\x00" as *u8); gnum(fd, t2); gp(fd, "\n\x00" as *u8)
54 if t2 != 1 { ok = 0 }
55
56 // c=[0,0,0,1] qP=0 -> [5,-5,-5,5]
57 setc(c, 0, 0, 0, 1)
58 nx_h264_chroma_dc_scale(c, 0, out)
59 var t3: i64 = 1
60 if out[0] != 5 { t3 = 0 }
61 if out[1] != 0 - 5 { t3 = 0 }
62 if out[2] != 0 - 5 { t3 = 0 }
63 if out[3] != 5 { t3 = 0 }
64 gp(fd, " c=[0,0,0,1] qP=0 -> [\x00" as *u8); gnum(fd, out[0]); gp(fd, " \x00" as *u8); gnum(fd, out[1]); gp(fd, " \x00" as *u8); gnum(fd, out[2]); gp(fd, " \x00" as *u8); gnum(fd, out[3]); gp(fd, "] expect [5 -5 -5 5] ok=\x00" as *u8); gnum(fd, t3); gp(fd, "\n\x00" as *u8)
65 if t3 != 1 { ok = 0 }
66
67 // linearity: scale(2c) == 2*scale(c) for c=[3,1,-2,4] qP=18
68 let outA: *i64 = sys_mmap(4*8) as *i64
69 let outB: *i64 = sys_mmap(4*8) as *i64
70 setc(c, 3, 1, 0 - 2, 4)
71 nx_h264_chroma_dc_scale(c, 18, outA)
72 setc(c, 6, 2, 0 - 4, 8)
73 nx_h264_chroma_dc_scale(c, 18, outB)
74 var lin: i64 = 1
75 i = 0
76 while i < 4 { if outB[i] != 2 * outA[i] { lin = 0 } i = i + 1 }
77 gp(fd, " linearity scale(2c)==2scale(c) ok=\x00" as *u8); gnum(fd, lin); gp(fd, "\n\x00" as *u8)
78 if lin != 1 { ok = 0 }
79
80 if ok == 1 {
81 gp(fd, "H264-CHROMA-DC-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8)
82 if fd > 0 { sys_close(fd) }
83 sys_exit(0)
84 return 0
85 }
86 gp(fd, "H264-CHROMA-DC-GATE result=FAIL verdict=RED\n\x00" as *u8)
87 if fd > 0 { sys_close(fd) }
88 sys_exit(1)
89 return 1
90}