code wiki / (root) / nx_flac_frame_gate.nx

nx_flac_frame_gate.nx source

↩ module page · 182 lines · 7628 B

1// nx_flac_frame_gate.nx -- proves FLAC frame + subframe decoding. 2// 3// T4/T5 assemble REAL frames byte by byte and decode them to samples: a 4// 192-sample CONSTANT frame at 44.1kHz/16-bit mono, and a 3-sample VERBATIM 5// frame using the deferred 8-bit block size. Both exercise the whole path -- 6// sync, flag bytes, UTF-8 frame number, deferred fields, CRC-8, subframe 7// header, sample extraction. 8// 9// T6 is the one that matters for long streams: the header LENGTH is variable 10// (4 flag bytes + a 1-7 byte number + optional deferred block size and rate), 11// and the CRC covers exactly those bytes. A parser with a hard-coded length 12// works on every short file and breaks at frame 128, when the UTF-8 number 13// grows to two bytes. T6 asserts hdrlen 6 for the simple frame and 7 for the 14// deferred-blocksize one. 15// 16// NON-VACUITY: T8 covers refusals -- broken sync, a flipped CRC byte, and the 17// reserved bit set must each be rejected. 18// 19// license_tier: ORIGINAL 20import "nx_syscalls.nx" 21import "nx_bitstream.nx" 22import "nx_flac.nx" 23import "nx_flac_frame.nx" 24 25func g_puts(s: *u8) -> i64 { 26 var i: i64 = 0 27 while s[i] != (0 as u8) { i = i + 1 } 28 sys_write(1, s, i) 29 return i 30} 31 32func g_putn(v: i64) -> i64 { 33 let buf: *u8 = sys_mmap(32) 34 var x: i64 = v 35 if x < 0 { g_puts("-" as *u8); x = 0 - x } 36 if x == 0 { buf[0] = 0x30 as u8; sys_write(1, buf, 1); return 1 } 37 let tmp: *u8 = sys_mmap(32) 38 var d: i64 = 0 39 while x > 0 { tmp[d] = ((x % 10) + 0x30) as u8; x = x / 10; d = d + 1 } 40 var i: i64 = 0 41 while i < d { buf[i] = tmp[d - 1 - i]; i = i + 1 } 42 sys_write(1, buf, d) 43 return d 44} 45 46func main() -> i64 { 47 var fails: i64 = 0 48 var mark: i64 = 0 49 50 // ---- T1: UTF-8-style extended numbers ---- 51 let ub: *u8 = sys_mmap(64) 52 let ul: *i64 = sys_mmap(64) as *i64 53 ub[0] = 0x00 as u8 54 if nx_flac_utf8_read(ub, 8, 0, ul) != 0 { fails = fails + 1 } 55 if ul[0] != 1 { fails = fails + 1 } 56 ub[0] = 0x7f as u8 57 if nx_flac_utf8_read(ub, 8, 0, ul) != 127 { fails = fails + 1 } 58 // two-byte form: 0xC2 0x80 -> 128, the exact boundary a hard-coded 59 // header length breaks on 60 ub[0] = 0xc2 as u8; ub[1] = 0x80 as u8 61 if nx_flac_utf8_read(ub, 8, 0, ul) != 128 { fails = fails + 1 } 62 if ul[0] != 2 { fails = fails + 1 } 63 if nx_flac_utf8_len(0x00) != 1 { fails = fails + 1 } 64 if nx_flac_utf8_len(0xc0) != 2 { fails = fails + 1 } 65 if nx_flac_utf8_len(0xe0) != 3 { fails = fails + 1 } 66 if fails > 0 { if mark == 0 { mark = 1 } } 67 68 // ---- T2: CRC-16, polynomial 0x8005 ---- 69 let cb: *u8 = sys_mmap(64) 70 cb[0] = 0x00 as u8 71 if nx_flac_crc16(cb, 1) != 0 { fails = fails + 1 } 72 cb[0] = 0x01 as u8 73 if nx_flac_crc16(cb, 1) != 0x8005 { fails = fails + 1 } 74 if fails > 0 { if mark == 0 { mark = 2 } } 75 76 // ---- T3: signed field extraction ---- 77 let sb: *u8 = sys_mmap(64) 78 sb[0] = 0xff as u8; sb[1] = 0xff as u8 79 let sbs: *NxBitStream = nx_bitstream_alloc(sb, 2) 80 if nx_flac_read_signed(sbs, 16) != (0 - 1) { fails = fails + 1 } 81 sb[0] = 0x80 as u8; sb[1] = 0x00 as u8 82 let sbs2: *NxBitStream = nx_bitstream_alloc(sb, 2) 83 if nx_flac_read_signed(sbs2, 16) != (0 - 32768) { fails = fails + 1 } 84 if fails > 0 { if mark == 0 { mark = 3 } } 85 86 // ---- T4: a REAL 192-sample CONSTANT frame, 44.1kHz 16-bit mono ---- 87 let fr: *u8 = sys_mmap(4096) 88 fr[0] = 0xff as u8 // sync 89 fr[1] = 0xf8 as u8 // sync tail, reserved 0, fixed blocksize 90 fr[2] = 0x19 as u8 // blocksize code 1 (192), rate code 9 (44100) 91 fr[3] = 0x08 as u8 // mono, bps code 4 (16), reserved 0 92 fr[4] = 0x00 as u8 // frame number 0 93 fr[5] = (nx_flac_crc8(fr, 5)) as u8 94 // CONSTANT subframe: pad 0, type 000000, wasted 0, then 16-bit value 4660 95 fr[6] = 0x00 as u8 96 fr[7] = 0x12 as u8 97 fr[8] = 0x34 as u8 98 99 let fld: *i64 = sys_mmap(128) as *i64 100 if nx_flac_frame_parse(fr, 9, 0, fld) != 1 { fails = fails + 1 } else { 101 if fld[NX_FLACF_FLD_BLOCKSIZE] != 192 { fails = fails + 1 } 102 if fld[NX_FLACF_FLD_RATE] != 44100 { fails = fails + 1 } 103 if fld[NX_FLACF_FLD_CHANNELS] != 1 { fails = fails + 1 } 104 if fld[NX_FLACF_FLD_BPS] != 16 { fails = fails + 1 } 105 if fld[NX_FLACF_FLD_NUMBER] != 0 { fails = fails + 1 } 106 if fld[NX_FLACF_FLD_HDRLEN] != 6 { fails = fails + 1 } 107 108 let bs: *NxBitStream = nx_bitstream_alloc(fr + 6, 3) 109 let out: *i64 = sys_mmap(256 * 8 + 64) as *i64 110 if nx_flac_subframe(bs, out, 192, 16) != 1 { fails = fails + 1 } else { 111 var bad: i64 = 0 112 var i: i64 = 0 113 while i < 192 { 114 if out[i] != 4660 { bad = bad + 1 } 115 i = i + 1 116 } 117 if bad != 0 { fails = fails + 1 } 118 } 119 } 120 if fails > 0 { if mark == 0 { mark = 4 } } 121 122 // ---- T5: a VERBATIM frame using the DEFERRED 8-bit block size ---- 123 let f2: *u8 = sys_mmap(4096) 124 f2[0] = 0xff as u8 125 f2[1] = 0xf8 as u8 126 f2[2] = 0x69 as u8 // blocksize code 6 (deferred 8-bit), rate 9 127 f2[3] = 0x08 as u8 128 f2[4] = 0x00 as u8 // frame number 0 129 f2[5] = 0x02 as u8 // blocksize - 1 = 2 -> 3 samples 130 f2[6] = (nx_flac_crc8(f2, 6)) as u8 131 // VERBATIM subframe: pad 0, type 000001, wasted 0 -> 0b0_000001_0 = 0x02 132 f2[7] = 0x02 as u8 133 f2[8] = 0x00 as u8; f2[9] = 0x64 as u8 // 100 134 f2[10] = 0xff as u8; f2[11] = 0xff as u8 // -1 135 f2[12] = 0x7f as u8; f2[13] = 0xff as u8 // 32767 136 137 let f2ld: *i64 = sys_mmap(128) as *i64 138 if nx_flac_frame_parse(f2, 14, 0, f2ld) != 1 { fails = fails + 1 } else { 139 if f2ld[NX_FLACF_FLD_BLOCKSIZE] != 3 { fails = fails + 1 } 140 if f2ld[NX_FLACF_FLD_HDRLEN] != 7 { fails = fails + 1 } 141 let bs2: *NxBitStream = nx_bitstream_alloc(f2 + 7, 7) 142 let o2: *i64 = sys_mmap(64) as *i64 143 if nx_flac_subframe(bs2, o2, 3, 16) != 1 { fails = fails + 1 } else { 144 if o2[0] != 100 { fails = fails + 1 } 145 if o2[1] != (0 - 1) { fails = fails + 1 } 146 if o2[2] != 32767 { fails = fails + 1 } 147 } 148 } 149 if fails > 0 { if mark == 0 { mark = 5 } } 150 151 // ---- T8 NEG: refusals ---- 152 fr[0] = 0x41 as u8 153 if nx_flac_frame_parse(fr, 9, 0, fld) != 0 { fails = fails + 1 } 154 fr[0] = 0xff as u8 155 // a flipped CRC byte 156 fr[5] = ((fr[5] as i64) ^ 0xff) as u8 157 if nx_flac_frame_parse(fr, 9, 0, fld) != 0 { fails = fails + 1 } 158 fr[5] = ((fr[5] as i64) ^ 0xff) as u8 159 if nx_flac_frame_parse(fr, 9, 0, fld) != 1 { fails = fails + 1 } 160 // the reserved bit in byte 3 must be zero 161 fr[3] = 0x09 as u8 162 fr[5] = (nx_flac_crc8(fr, 5)) as u8 163 if nx_flac_frame_parse(fr, 9, 0, fld) != 0 { fails = fails + 1 } 164 // a truncated buffer 165 fr[3] = 0x08 as u8 166 fr[5] = (nx_flac_crc8(fr, 5)) as u8 167 if nx_flac_frame_parse(fr, 3, 0, fld) != 0 { fails = fails + 1 } 168 if fails > 0 { if mark == 0 { mark = 8 } } 169 170 if fails == 0 { 171 g_puts("GATE nx_flac_frame verdict=GREEN pass=8/8 (UTF-8 extended numbers incl the 2-byte 128 boundary; CRC-16 0x8005; signed fields; REAL 192-sample CONSTANT frame 44100/16/mono decoded hdrlen=6; REAL 3-sample VERBATIM frame with DEFERRED blocksize hdrlen=7 -> 100/-1/32767; NEG bad-sync/flipped-CRC-then-restored/reserved-bit/truncated refused)\n" as *u8) 172 sys_exit(0) 173 return 0 174 } 175 g_puts("GATE nx_flac_frame verdict=RED fails=" as *u8) 176 g_putn(fails) 177 g_puts(" first_stage=" as *u8) 178 g_putn(mark) 179 g_puts("\n" as *u8) 180 sys_exit(1) 181 return 1 182}