code wiki / (root) / nx_h264_cavlc_level_gate.nx

nx_h264_cavlc_level_gate.nx source

↩ module page · 97 lines · 4225 B

1// nx_h264_cavlc_level_gate.nx -- REFEREE for CAVLC level decode (rung 4c-i). 2// levelCode->level map (spec): 0->+1 1->-1 2->+2 3->-2 4->+3. 3// read_level bit patterns (hand-traced): 4// sl=0 "1" -> prefix0,code0 -> +1 5// sl=0 "01" -> prefix1,code1 -> -1 6// sl=1 "10" -> prefix0,suffix0,code0 -> +1 7// sl=1 "11" -> prefix0,suffix1,code1 -> -1 8// sl=1 "010" -> prefix1,suffix0,code2 -> +2 9// suffixLength update: (0,1)->1 ; (1,4)->2 ; (0,5)->2. 10// Every value PRINTED. stdout + knowledge/status/h264_cavlc_level_gate.log. Exit 0/1. 11// Sovereign: nx_syscalls + nx_h264_bits + nx_h264_cavlc_level. license_tier: ORIGINAL 12import "nx_syscalls.nx" 13import "nx_h264_bits.nx" 14import "nx_h264_cavlc_level.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// build a BitReader over a single byte whose top bits are the pattern 38func mkbr(byte0: i64) -> *BitReader { 39 let buf: *u8 = sys_mmap(4) 40 buf[0] = byte0 as u8 41 let br: *BitReader = sys_mmap(NX_BR_BYTES) as *BitReader 42 br_init(br, buf, 4) 43 return br 44} 45 46func main() -> i64 { 47 let logfd: i64 = sys_openat_append("knowledge/status/h264_cavlc_level_gate.log\x00" as *u8, 0x1a4) 48 gp(logfd, "H264-CAVLC-LEVEL-GATE (rung 4c-i)\n\x00" as *u8) 49 50 // levelCode -> level mapping 51 let m0: i64 = nx_cavlc_levelcode_to_level(0) 52 let m1: i64 = nx_cavlc_levelcode_to_level(1) 53 let m2: i64 = nx_cavlc_levelcode_to_level(2) 54 let m3: i64 = nx_cavlc_levelcode_to_level(3) 55 let m4: i64 = nx_cavlc_levelcode_to_level(4) 56 gp(logfd, " levelcode map: \x00" as *u8); gn(logfd, m0); gp(logfd, " \x00" as *u8); gn(logfd, m1); gp(logfd, " \x00" as *u8); gn(logfd, m2); gp(logfd, " \x00" as *u8); gn(logfd, m3); gp(logfd, " \x00" as *u8); gn(logfd, m4); gp(logfd, " (expect 1 -1 2 -2 3)\n\x00" as *u8) 57 58 // read_level: pattern bytes (top bits = the code, rest pad 0) 59 let l1: i64 = nx_cavlc_read_level(mkbr(0x80), 0) // "1......" sl0 -> +1 60 let l2: i64 = nx_cavlc_read_level(mkbr(0x40), 0) // "01....." sl0 -> -1 61 let l3: i64 = nx_cavlc_read_level(mkbr(0x80), 1) // "10....." sl1 -> +1 62 let l4: i64 = nx_cavlc_read_level(mkbr(0xC0), 1) // "11....." sl1 -> -1 63 let l5: i64 = nx_cavlc_read_level(mkbr(0x40), 1) // "010...." sl1 -> +2 64 gp(logfd, " read_level: \x00" as *u8); gn(logfd, l1); gp(logfd, " \x00" as *u8); gn(logfd, l2); gp(logfd, " \x00" as *u8); gn(logfd, l3); gp(logfd, " \x00" as *u8); gn(logfd, l4); gp(logfd, " \x00" as *u8); gn(logfd, l5); gp(logfd, " (expect 1 -1 1 -1 2)\n\x00" as *u8) 65 66 // suffixLength update 67 let u1: i64 = nx_cavlc_update_suffix(0, 1) 68 let u2: i64 = nx_cavlc_update_suffix(1, 4) 69 let u3: i64 = nx_cavlc_update_suffix(0, 5) 70 gp(logfd, " suffix update: \x00" as *u8); gn(logfd, u1); gp(logfd, " \x00" as *u8); gn(logfd, u2); gp(logfd, " \x00" as *u8); gn(logfd, u3); gp(logfd, " (expect 1 2 2)\n\x00" as *u8) 71 72 var ok: i64 = 1 73 if m0 != 1 { ok = 0 } 74 if m1 != 0 - 1 { ok = 0 } 75 if m2 != 2 { ok = 0 } 76 if m3 != 0 - 2 { ok = 0 } 77 if m4 != 3 { ok = 0 } 78 if l1 != 1 { ok = 0 } 79 if l2 != 0 - 1 { ok = 0 } 80 if l3 != 1 { ok = 0 } 81 if l4 != 0 - 1 { ok = 0 } 82 if l5 != 2 { ok = 0 } 83 if u1 != 1 { ok = 0 } 84 if u2 != 2 { ok = 0 } 85 if u3 != 2 { ok = 0 } 86 87 if ok == 1 { 88 gp(logfd, "H264-CAVLC-LEVEL-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8) 89 if logfd > 0 { sys_close(logfd) } 90 sys_exit(0) 91 return 0 92 } 93 gp(logfd, "H264-CAVLC-LEVEL-GATE result=FAIL verdict=RED\n\x00" as *u8) 94 if logfd > 0 { sys_close(logfd) } 95 sys_exit(1) 96 return 1 97}