code wiki / (root) / nx_h264_chroma_pred_gate.nx

nx_h264_chroma_pred_gate.nx source

↩ module page · 121 lines · 4892 B

1// nx_h264_chroma_pred_gate.nx -- validate intra chroma 8x8 prediction (4 modes). 2// plane(3): neighbours on plane p=base+gx*(x-3)+gy*(y-3) -> pred==p exactly (64/64) 3// horiz(1)/vert(2): exact copy 4// DC(0): per-quadrant with hand-computed expected (both-avail + top-only fallback) 5// Exit 0/1. license_tier: ORIGINAL 6import "nx_syscalls.nx" 7import "nx_h264_chroma_pred.nx" 8 9func gp(fd: i64, s: *u8) -> i64 { 10 var n: i64 = 0 11 while s[n] != (0 as u8) { n = n + 1 } 12 sys_write(1, s, n); if fd > 0 { sys_write(fd, s, n) } 13 return 0 14} 15func gnum(fd: i64, v: i64) -> i64 { 16 let bb: *u8 = sys_mmap(28); var m: i64 = v 17 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if fd > 0 { sys_write(fd, "-\x00" as *u8, 1) } m = 0 - m } 18 let t: *u8 = sys_mmap(28); var k: i64 = 0 19 if m == 0 { t[0] = 48 as u8; k = 1 } 20 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 21 var i: i64 = 0 22 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 23 sys_write(1, bb, k); if fd > 0 { sys_write(fd, bb, k) } 24 return 0 25} 26 27func plane_recover(fd: i64, base: i64, gx: i64, gy: i64) -> i64 { 28 let top: *i64 = sys_mmap(8*8) as *i64 29 let left: *i64 = sys_mmap(8*8) as *i64 30 var i: i64 = 0 31 while i < 8 { 32 top[i] = base + gx * (i - 3) + gy * (0 - 1 - 3) 33 left[i] = base + gx * (0 - 1 - 3) + gy * (i - 3) 34 i = i + 1 35 } 36 let tl: i64 = base + gx * (0 - 1 - 3) + gy * (0 - 1 - 3) 37 let pred: *i64 = sys_mmap(64*8) as *i64 38 nx_intra_chroma_pred(3, top, left, tl, 1, 1, pred) 39 var exact: i64 = 0 40 var y: i64 = 0 41 while y < 8 { 42 var x: i64 = 0 43 while x < 8 { if pred[y*8+x] == base + gx*(x-3) + gy*(y-3) { exact = exact + 1 } x = x + 1 } 44 y = y + 1 45 } 46 gp(fd, " plane base=\x00" as *u8); gnum(fd, base); gp(fd, " gx=\x00" as *u8); gnum(fd, gx); gp(fd, " gy=\x00" as *u8); gnum(fd, gy); gp(fd, " exact=\x00" as *u8); gnum(fd, exact); gp(fd, "/64\n\x00" as *u8) 47 return exact 48} 49 50// check a quadrant (qx,qy in {0,4}) is uniformly == want 51func quad_is(pred: *i64, qx: i64, qy: i64, want: i64) -> i64 { 52 var y: i64 = 0 53 while y < 4 { 54 var x: i64 = 0 55 while x < 4 { if pred[(qy+y)*8 + (qx+x)] != want { return 0 } x = x + 1 } 56 y = y + 1 57 } 58 return 1 59} 60 61func main() -> i64 { 62 let fd: i64 = sys_openat_append("knowledge/status/h264_chroma_pred_gate.log\x00" as *u8, 0x1a4) 63 gp(fd, "H264-CHROMA-PRED-GATE\n\x00" as *u8) 64 var ok: i64 = 1 65 66 if plane_recover(fd, 128, 1, 1) != 64 { ok = 0 } 67 if plane_recover(fd, 110, 2, 0 - 1) != 64 { ok = 0 } 68 69 let top: *i64 = sys_mmap(8*8) as *i64 70 let left: *i64 = sys_mmap(8*8) as *i64 71 let pred: *i64 = sys_mmap(64*8) as *i64 72 var i: i64 = 0 73 while i < 8 { top[i] = 30 + i*5; left[i] = 200 - i*4; i = i + 1 } 74 // Horizontal 75 nx_intra_chroma_pred(1, top, left, 50, 1, 1, pred) 76 var hok: i64 = 1 77 var y: i64 = 0 78 while y < 8 { var x: i64 = 0; while x < 8 { if pred[y*8+x] != left[y] { hok = 0 } x = x + 1 } y = y + 1 } 79 // Vertical 80 nx_intra_chroma_pred(2, top, left, 50, 1, 1, pred) 81 var vok: i64 = 1 82 y = 0 83 while y < 8 { var x: i64 = 0; while x < 8 { if pred[y*8+x] != top[x] { vok = 0 } x = x + 1 } y = y + 1 } 84 gp(fd, " horiz exact=\x00" as *u8); gnum(fd, hok); gp(fd, " vert exact=\x00" as *u8); gnum(fd, vok); gp(fd, "\n\x00" as *u8) 85 if hok != 1 { ok = 0 } 86 if vok != 1 { ok = 0 } 87 88 // DC both-avail: top=[100x4,200x4], left=[50x4,150x4] 89 i = 0 90 while i < 4 { top[i] = 100; top[4+i] = 200; left[i] = 50; left[4+i] = 150; i = i + 1 } 91 nx_intra_chroma_pred(0, top, left, 0, 1, 1, pred) 92 // TL=(400+200+4)>>3=75 TR=(800+2)>>2=200 BL=(600+2)>>2=150 BR=(800+600+4)>>3=175 93 var dcok: i64 = 1 94 if quad_is(pred, 0, 0, 75) != 1 { dcok = 0 } 95 if quad_is(pred, 4, 0, 200) != 1 { dcok = 0 } 96 if quad_is(pred, 0, 4, 150) != 1 { dcok = 0 } 97 if quad_is(pred, 4, 4, 175) != 1 { dcok = 0 } 98 gp(fd, " DC both-avail quadrants ok=\x00" as *u8); gnum(fd, dcok); gp(fd, " (TL75 TR200 BL150 BR175)\n\x00" as *u8) 99 if dcok != 1 { ok = 0 } 100 101 // DC top-only fallback: TL=(400+2)>>2=100 TR=(800+2)>>2=200 BL=(400+2)>>2=100 BR=(800+2)>>2=200 102 nx_intra_chroma_pred(0, top, left, 0, 1, 0, pred) 103 var dc2: i64 = 1 104 if quad_is(pred, 0, 0, 100) != 1 { dc2 = 0 } 105 if quad_is(pred, 4, 0, 200) != 1 { dc2 = 0 } 106 if quad_is(pred, 0, 4, 100) != 1 { dc2 = 0 } 107 if quad_is(pred, 4, 4, 200) != 1 { dc2 = 0 } 108 gp(fd, " DC top-only fallback ok=\x00" as *u8); gnum(fd, dc2); gp(fd, "\n\x00" as *u8) 109 if dc2 != 1 { ok = 0 } 110 111 if ok == 1 { 112 gp(fd, "H264-CHROMA-PRED-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8) 113 if fd > 0 { sys_close(fd) } 114 sys_exit(0) 115 return 0 116 } 117 gp(fd, "H264-CHROMA-PRED-GATE result=FAIL verdict=RED\n\x00" as *u8) 118 if fd > 0 { sys_close(fd) } 119 sys_exit(1) 120 return 1 121}