nx_vp8_kf_gate.nx source
↩ module page · 228 lines · 9525 B
1// nx_vp8_kf_gate.nx -- gate for C3 of the codec-chokepoint program: the lossy
2// VP8 KEYFRAME decoder + its chokepoint wiring. Anchored to an INDEPENDENT
3// ORACLE: libwebp (via PIL, laptop-side) decoded two REAL web-gathered
4// witnesses from knowledge/media/julia_kyoka; their 8x8 Y-tile means and RGB
5// means are baked here. Tolerance is +-2 per tile (loop filter is absent by
6// design in v1); a wrong decode diverges by 30-100 per tile, so the band is
7// tight AND non-vacuous. The bool-decoder KAT values come from an exact
8// independent port of RFC 6386 sec 7.3 -- NOT the (false) raw-bit identity
9// the old header comment claimed. Inherits nx_gate_verdict.
10// license_tier: ORIGINAL expect_exit: 0
11import "nx_syscalls.nx"
12import "nx_gate_verdict.nx"
13import "nx_vp8.nx"
14import "nx_vp8_pred.nx"
15import "nx_vp8_kf.nx"
16import "nx_img_bytes_to_rgb.nx"
17
18func vg_cc(b: *u8, off: i64, a: i64, c: i64, d: i64, e: i64) -> i64 {
19 if ((b[off] as i64)&255) != a { return 0 }
20 if ((b[off+1] as i64)&255) != c { return 0 }
21 if ((b[off+2] as i64)&255) != d { return 0 }
22 if ((b[off+3] as i64)&255) != e { return 0 }
23 return 1
24}
25func vg_u32(b: *u8, off: i64) -> i64 {
26 return ((b[off] as i64)&255) | (((b[off+1] as i64)&255)<<8) | (((b[off+2] as i64)&255)<<16) | (((b[off+3] as i64)&255)<<24)
27}
28
29// RIFF walk to the 'VP8 ' chunk; box[0]=payload off, box[1]=len
30func vg_find(raw: *u8, n: i64, box: *i64) -> i64 {
31 if n < 20 { return 0 }
32 if vg_cc(raw,0,82,73,70,70) == 0 { return 0 }
33 if vg_cc(raw,8,87,69,66,80) == 0 { return 0 }
34 var pos: i64 = 12
35 var go: i64 = 1
36 var ok: i64 = 0
37 while go == 1 {
38 if pos + 8 > n { go = 0 } else {
39 let clen: i64 = vg_u32(raw, pos+4)
40 if vg_cc(raw,pos,86,80,56,32) == 1 {
41 box[0] = pos + 8
42 var pl: i64 = clen
43 if box[0] + pl > n { pl = n - box[0] }
44 box[1] = pl
45 ok = 1
46 go = 0
47 } else {
48 var adv: i64 = clen
49 if (adv & 1) == 1 { adv = adv + 1 }
50 pos = pos + 8 + adv
51 }
52 }
53 }
54 return ok
55}
56
57// 8x8 Y tile means of a decoded frame
58func vg_tiles(py: *u8, ys: i64, w: i64, h: i64, outv: *i64) -> i64 {
59 var ty: i64 = 0
60 while ty < 8 {
61 var tx: i64 = 0
62 while tx < 8 {
63 let x0: i64 = tx*w/8
64 let x1: i64 = (tx+1)*w/8
65 let y0: i64 = ty*h/8
66 let y1: i64 = (ty+1)*h/8
67 var s: i64 = 0
68 var c: i64 = 0
69 var yy: i64 = y0
70 while yy < y1 {
71 var xx: i64 = x0
72 while xx < x1 { s = s + ((py[yy*ys+xx] as i64)&255); c = c + 1; xx = xx + 1 }
73 yy = yy + 1
74 }
75 var m: i64 = 0
76 if c > 0 { m = s / c }
77 outv[ty*8+tx] = m
78 tx = tx + 1
79 }
80 ty = ty + 1
81 }
82 return 0
83}
84
85// decode one witness + compare tiles vs the baked oracle (tol +-2)
86func vg_witness(path: *u8, ew: i64, eh: i64, oracle: *i64) -> i64 {
87 let lb: *i64 = sys_mmap(16) as *i64
88 let raw: *u8 = sys_read_file(path, lb)
89 if raw == (0 as *u8) { return 0 }
90 let box: *i64 = sys_mmap(32) as *i64
91 if vg_find(raw, lb[0], box) == 0 { return 0 }
92 let vb: *i64 = sys_mmap(64) as *i64
93 if nx_vp8_kf_decode(raw + box[0], box[1], vb) == 0 { return 0 }
94 if vb[NX_VP8KF_OUT_W] != ew { return 0 }
95 if vb[NX_VP8KF_OUT_H] != eh { return 0 }
96 let tl: *i64 = sys_mmap(64*8+64) as *i64
97 vg_tiles(vb[NX_VP8KF_OUT_Y] as *u8, vb[NX_VP8KF_OUT_YS], ew, eh, tl)
98 var i: i64 = 0
99 var ok: i64 = 1
100 while i < 64 {
101 var d: i64 = tl[i] - oracle[i]
102 if d < 0 { d = 0 - d }
103 if d > 2 { ok = 0 }
104 i = i + 1
105 }
106 return ok
107}
108
109func vg_fill_ta(t: *i64) -> i64 {
110 t[0]=179; t[1]=196; t[2]=181; t[3]=156; t[4]=74; t[5]=97; t[6]=191; t[7]=148;
111 t[8]=132; t[9]=187; t[10]=160; t[11]=52; t[12]=122; t[13]=143; t[14]=118; t[15]=103;
112 t[16]=137; t[17]=180; t[18]=146; t[19]=52; t[20]=151; t[21]=155; t[22]=88; t[23]=83;
113 t[24]=157; t[25]=149; t[26]=180; t[27]=88; t[28]=107; t[29]=157; t[30]=140; t[31]=85;
114 t[32]=204; t[33]=169; t[34]=184; t[35]=173; t[36]=144; t[37]=178; t[38]=195; t[39]=192;
115 t[40]=196; t[41]=225; t[42]=209; t[43]=161; t[44]=167; t[45]=191; t[46]=193; t[47]=203;
116 t[48]=228; t[49]=223; t[50]=206; t[51]=116; t[52]=156; t[53]=177; t[54]=170; t[55]=199;
117 t[56]=206; t[57]=206; t[58]=188; t[59]=159; t[60]=132; t[61]=138; t[62]=120; t[63]=184;
118 return 0
119}
120
121func vg_fill_tb(t: *i64) -> i64 {
122 t[0]=108; t[1]=101; t[2]=64; t[3]=98; t[4]=137; t[5]=155; t[6]=202; t[7]=201;
123 t[8]=121; t[9]=172; t[10]=161; t[11]=104; t[12]=60; t[13]=129; t[14]=195; t[15]=205;
124 t[16]=187; t[17]=182; t[18]=162; t[19]=81; t[20]=60; t[21]=115; t[22]=169; t[23]=205;
125 t[24]=192; t[25]=196; t[26]=168; t[27]=161; t[28]=154; t[29]=130; t[30]=128; t[31]=168;
126 t[32]=189; t[33]=198; t[34]=202; t[35]=195; t[36]=175; t[37]=132; t[38]=138; t[39]=132;
127 t[40]=194; t[41]=160; t[42]=174; t[43]=191; t[44]=147; t[45]=101; t[46]=129; t[47]=97;
128 t[48]=192; t[49]=181; t[50]=134; t[51]=112; t[52]=114; t[53]=131; t[54]=136; t[55]=160;
129 t[56]=195; t[57]=157; t[58]=139; t[59]=134; t[60]=125; t[61]=131; t[62]=159; t[63]=164;
130 return 0
131}
132
133func main() -> i64 {
134 let ctr: *i64 = gv_ctr()
135 gv_head("nx_vp8_kf_gate -- C3 lossy VP8 keyframe decoder + chokepoint wiring" as *u8)
136
137 // T1 bool-decoder KAT (expected values from an independent RFC 7.3 port)
138 let pb: *u8 = sys_mmap(16)
139 pb[0] = 0xA5 as u8
140 pb[1] = 0x3C as u8
141 pb[2] = 0x0F as u8
142 pb[3] = 0x77 as u8
143 let sbd: *NxVp8Bool = nx_vp8_bool_init(pb, 4, 0)
144 var t1: i64 = 0
145 if nx_vp8_bool_literal(sbd, 16) == 42375 {
146 if nx_vp8_bool_literal(sbd, 16) == 7602 { t1 = 1 }
147 }
148 gv_check("T1 bool-decoder KAT vs independent RFC 7.3 port" as *u8, t1, ctr)
149
150 // T2 floor-shift semantics on negatives
151 var t2: i64 = 0
152 if vp8p_sr16(0 - 65537) == (0 - 2) { if vp8p_sr3(0 - 1) == (0 - 1) { t2 = 1 } }
153 gv_check("T2 floor-shift helpers exact on negatives" as *u8, t2, ctr)
154
155 // T3/T4 real web witnesses vs baked libwebp oracle tile means
156 let ta: *i64 = sys_mmap(64*8+64) as *i64
157 vg_fill_ta(ta)
158 let t3: i64 = vg_witness("knowledge/media/julia_kyoka/766eb1a4e90f464a594e0440d42a0e5179858c1a23a9e5a110ad9d332e7f17f2.webp" as *u8, 400, 500, ta)
159 gv_check("T3 witness A 400x500: 64 tile means within +-2 of libwebp" as *u8, t3, ctr)
160
161 let tb: *i64 = sys_mmap(64*8+64) as *i64
162 vg_fill_tb(tb)
163 let t4: i64 = vg_witness("knowledge/media/julia_kyoka/8aa1fea5fed58ff5f4042af6268843742434f0f668ef68ebf698a7b7d4e20912.webp" as *u8, 147, 200, tb)
164 gv_check("T4 witness B 147x200: 64 tile means within +-2 of libwebp" as *u8, t4, ctr)
165
166 // T5 BITE: corrupted start code refused, intact decodes
167 let lb: *i64 = sys_mmap(16) as *i64
168 let rawa: *u8 = sys_read_file("knowledge/media/julia_kyoka/766eb1a4e90f464a594e0440d42a0e5179858c1a23a9e5a110ad9d332e7f17f2.webp" as *u8, lb)
169 var bite_bad: i64 = 0
170 var bite_good: i64 = 1
171 var t6bad: i64 = 0
172 if rawa != (0 as *u8) {
173 let bx: *i64 = sys_mmap(32) as *i64
174 if vg_find(rawa, lb[0], bx) == 1 {
175 let cp: *u8 = sys_mmap(bx[1] + 64)
176 var i: i64 = 0
177 while i < bx[1] { cp[i] = rawa[bx[0]+i]; i = i + 1 }
178 cp[3] = 0x00 as u8
179 let vb1: *i64 = sys_mmap(64) as *i64
180 if nx_vp8_kf_decode(cp, bx[1], vb1) == 0 { bite_bad = 1 }
181 let vb2: *i64 = sys_mmap(64) as *i64
182 if nx_vp8_kf_decode(rawa + bx[0], bx[1], vb2) == 1 { bite_good = 0 }
183 // T6 truncation refusal (25 bytes cannot carry the partitions)
184 let vb3: *i64 = sys_mmap(64) as *i64
185 if nx_vp8_kf_decode(rawa + bx[0], 25, vb3) == 0 { t6bad = 1 }
186 }
187 }
188 gv_bite("T5 corrupted-start-code refusal" as *u8, bite_bad, bite_good, ctr)
189 gv_bite("T6 truncated-payload refusal" as *u8, t6bad, bite_good, ctr)
190
191 // T7 chokepoint inheritance: the shared decode path returns the image
192 var t7: i64 = 0
193 var t8: i64 = 0
194 if rawa != (0 as *u8) {
195 let wh: *i64 = sys_mmap(32) as *i64
196 wh[0] = 0
197 wh[1] = 0
198 let rgb: *u8 = nx_img_bytes_to_rgb(rawa, lb[0], wh)
199 if rgb != (0 as *u8) { if wh[0] == 400 { if wh[1] == 500 {
200 t7 = 1
201 // T8 RGB means within +-3 of libwebp (182,160,144): proves YUV->RGB
202 var rs: i64 = 0
203 var gs: i64 = 0
204 var bs: i64 = 0
205 var p: i64 = 0
206 while p < 400*500 {
207 rs = rs + ((rgb[p*3] as i64)&255)
208 gs = gs + ((rgb[p*3+1] as i64)&255)
209 bs = bs + ((rgb[p*3+2] as i64)&255)
210 p = p + 1
211 }
212 let n: i64 = 400*500
213 var dr: i64 = rs/n - 182
214 var dg: i64 = gs/n - 160
215 var db: i64 = bs/n - 144
216 if dr < 0 { dr = 0 - dr }
217 if dg < 0 { dg = 0 - dg }
218 if db < 0 { db = 0 - db }
219 if dr <= 3 { if dg <= 3 { if db <= 3 { t8 = 1 } } }
220 } } }
221 }
222 gv_check("T7 chokepoint nx_img_bytes_to_rgb decodes lossy WebP" as *u8, t7, ctr)
223 gv_check("T8 full-path RGB means within +-3 of libwebp" as *u8, t8, ctr)
224
225 let rc: i64 = gv_verdict("VP8-KF-GATE" as *u8, ctr, "C3 lossy VP8 keyframe: RFC-exact, oracle-anchored" as *u8)
226 sys_exit(rc)
227 return rc
228}