code wiki / (root) / nx_h264_intra16plane_gate.nx

nx_h264_intra16plane_gate.nx source

↩ module page · 98 lines · 3938 B

1// nx_h264_intra16plane_gate.nx -- validate Intra_16x16 Plane (mode 3). 2// SOVEREIGN ORACLE: if the neighbor samples lie exactly on a plane 3// p(x,y)=base+gx*(x-7)+gy*(y-7), the H.264 plane predictor MUST reproduce p 4// exactly over the whole 16x16 block (the transform's exact-recovery property). 5// We synthesize neighbors from a known plane, predict, and require pred==p for all 256. 6// Tests: 2 gradients + flat + a sensitivity control. Exit 0/1. 7// license_tier: ORIGINAL 8import "nx_syscalls.nx" 9import "nx_h264_intra16.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) 15 if fd > 0 { sys_write(fd, s, n) } 16 return 0 17} 18func gnum(fd: i64, v: i64) -> i64 { 19 let bb: *u8 = sys_mmap(28) 20 var m: i64 = v 21 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if fd > 0 { sys_write(fd, "-\x00" as *u8, 1) } m = 0 - m } 22 let t: *u8 = sys_mmap(28) 23 var k: i64 = 0 24 if m == 0 { t[0] = 48 as u8; k = 1 } 25 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 26 var i: i64 = 0 27 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 28 sys_write(1, bb, k) 29 if fd > 0 { sys_write(fd, bb, k) } 30 return 0 31} 32 33// build neighbors from plane p(x,y)=base+gx*(x-7)+gy*(y-7); run predictor; count exact matches. 34func plane_recover(fd: i64, base: i64, gx: i64, gy: i64) -> i64 { 35 let top: *i64 = sys_mmap(16*8) as *i64 36 let left: *i64 = sys_mmap(16*8) as *i64 37 var i: i64 = 0 38 while i < 16 { 39 top[i] = base + gx * (i - 7) + gy * (0 - 1 - 7) // p[i,-1] 40 left[i] = base + gx * (0 - 1 - 7) + gy * (i - 7) // p[-1,i] 41 i = i + 1 42 } 43 let topleft: i64 = base + gx * (0 - 1 - 7) + gy * (0 - 1 - 7) 44 let pred: *i64 = sys_mmap(256*8) as *i64 45 nx_intra16x16_pred_full(3, top, left, topleft, 1, 1, pred) 46 var exact: i64 = 0 47 var y: i64 = 0 48 while y < 16 { 49 var x: i64 = 0 50 while x < 16 { 51 let want: i64 = base + gx * (x - 7) + gy * (y - 7) 52 if pred[y * 16 + x] == want { exact = exact + 1 } 53 x = x + 1 54 } 55 y = y + 1 56 } 57 gp(fd, " plane base=\x00" as *u8); gnum(fd, base); gp(fd, " gx=\x00" as *u8); gnum(fd, gx) 58 gp(fd, " gy=\x00" as *u8); gnum(fd, gy); gp(fd, " exact=\x00" as *u8); gnum(fd, exact); gp(fd, "/256\n\x00" as *u8) 59 return exact 60} 61 62func main() -> i64 { 63 let fd: i64 = sys_openat_append("knowledge/status/h264_intra16plane_gate.log\x00" as *u8, 0x1a4) 64 gp(fd, "H264-INTRA16-PLANE-GATE\n\x00" as *u8) 65 var ok: i64 = 1 66 if plane_recover(fd, 100, 2, 1) != 256 { ok = 0 } 67 if plane_recover(fd, 128, 0 - 1, 3) != 256 { ok = 0 } 68 if plane_recover(fd, 120, 1, 0 - 2) != 256 { ok = 0 } 69 // flat plane (gx=gy=0) -> all == base 70 if plane_recover(fd, 100, 0, 0) != 256 { ok = 0 } 71 72 // sensitivity control: a corrupted neighbor must change the output (not a constant function) 73 let top: *i64 = sys_mmap(16*8) as *i64 74 let left: *i64 = sys_mmap(16*8) as *i64 75 var i: i64 = 0 76 while i < 16 { top[i] = 100; left[i] = 100; i = i + 1 } 77 let predA: *i64 = sys_mmap(256*8) as *i64 78 nx_intra16x16_pred_full(3, top, left, 100, 1, 1, predA) 79 top[12] = 200 // perturb one top sample 80 let predB: *i64 = sys_mmap(256*8) as *i64 81 nx_intra16x16_pred_full(3, top, left, 100, 1, 1, predB) 82 var changed: i64 = 0 83 i = 0 84 while i < 256 { if predA[i] != predB[i] { changed = 1 } i = i + 1 } 85 gp(fd, " sensitivity(perturb->output changes)=\x00" as *u8); gnum(fd, changed); gp(fd, "\n\x00" as *u8) 86 if changed != 1 { ok = 0 } 87 88 if ok == 1 { 89 gp(fd, "H264-INTRA16-PLANE-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8) 90 if fd > 0 { sys_close(fd) } 91 sys_exit(0) 92 return 0 93 } 94 gp(fd, "H264-INTRA16-PLANE-GATE result=FAIL verdict=RED\n\x00" as *u8) 95 if fd > 0 { sys_close(fd) } 96 sys_exit(1) 97 return 1 98}