code wiki / (root) / nx_flac_gate.nx

nx_flac_gate.nx source

↩ module page · 166 lines · 7008 B

1// nx_flac_gate.nx -- proves the FLAC decoder core. 2// 3// Two of these tests guard defects that produce NO error and NO crash: 4// T1 zigzag unfolding -- invert it and every residual has the right 5// magnitude and the wrong sign. Output is noise, nothing reports it. 6// T8 mid/side low-bit -- the encoder drops the low bit of mid and hides it 7// in the low bit of side. Forget to re-insert it and every odd-summed 8// sample pair is off by one: inaudible, invisible on a spectrum, and 9// fatal to the word 'lossless'. T8 uses an ODD sum specifically. 10// 11// NON-VACUITY: T14/T15 are negative controls -- a non-FLAC magic, an 12// over-order fixed predictor and a zero-order LPC must each be REFUSED. 13// 14// license_tier: ORIGINAL 15import "nx_syscalls.nx" 16import "nx_bitstream.nx" 17import "nx_flac.nx" 18 19func g_puts(s: *u8) -> i64 { 20 var i: i64 = 0 21 while s[i] != (0 as u8) { i = i + 1 } 22 sys_write(1, s, i) 23 return i 24} 25 26func main() -> i64 { 27 var fails: i64 = 0 28 29 // ---- T1: zigzag unfold -- even is positive, odd is negative ---- 30 if nx_flac_unfold(0) != 0 { fails = fails + 1 } 31 if nx_flac_unfold(1) != (0 - 1) { fails = fails + 1 } 32 if nx_flac_unfold(2) != 1 { fails = fails + 1 } 33 if nx_flac_unfold(3) != (0 - 2) { fails = fails + 1 } 34 if nx_flac_unfold(4) != 2 { fails = fails + 1 } 35 36 // ---- T2: fold is its exact inverse ---- 37 var v: i64 = 0 - 5 38 var rt: i64 = 0 39 while v <= 5 { 40 if nx_flac_unfold(nx_flac_fold(v)) != v { rt = rt + 1 } 41 v = v + 1 42 } 43 if rt != 0 { fails = fails + 1 } 44 45 // ---- T3: the fixed predictor coefficient patterns ---- 46 if nx_flac_fixed_predict(0, 9, 9, 9, 9) != 0 { fails = fails + 1 } 47 if nx_flac_fixed_predict(1, 7, 0, 0, 0) != 7 { fails = fails + 1 } 48 if nx_flac_fixed_predict(2, 3, 1, 0, 0) != 5 { fails = fails + 1 } 49 if nx_flac_fixed_predict(3, 3, 2, 1, 0) != 4 { fails = fails + 1 } 50 if nx_flac_fixed_predict(4, 4, 3, 2, 1) != 5 { fails = fails + 1 } 51 52 // ---- T4: order-1 restore turns constant residuals into a ramp ---- 53 let b1: *i64 = sys_mmap(64) as *i64 54 b1[0] = 10; b1[1] = 5; b1[2] = 5; b1[3] = 5 55 if nx_flac_restore_fixed(b1, 4, 1) != 1 { fails = fails + 1 } else { 56 if b1[1] != 15 { fails = fails + 1 } 57 if b1[2] != 20 { fails = fails + 1 } 58 if b1[3] != 25 { fails = fails + 1 } 59 } 60 61 // ---- T5: order-2 restore reconstructs a ramp from ZERO residuals ---- 62 let b2: *i64 = sys_mmap(64) as *i64 63 b2[0] = 0; b2[1] = 1; b2[2] = 0; b2[3] = 0; b2[4] = 0 64 if nx_flac_restore_fixed(b2, 5, 2) != 1 { fails = fails + 1 } else { 65 if b2[2] != 2 { fails = fails + 1 } 66 if b2[3] != 3 { fails = fails + 1 } 67 if b2[4] != 4 { fails = fails + 1 } 68 } 69 70 // ---- T6: LPC with coef 2 and shift 1 is an identity predictor ---- 71 let b3: *i64 = sys_mmap(64) as *i64 72 b3[0] = 10; b3[1] = 0; b3[2] = 0 73 let co: *i64 = sys_mmap(64) as *i64 74 co[0] = 2 75 if nx_flac_restore_lpc(b3, 3, 1, co, 1) != 1 { fails = fails + 1 } else { 76 if b3[1] != 10 { fails = fails + 1 } 77 if b3[2] != 10 { fails = fails + 1 } 78 } 79 80 // ---- T7: left/side and right/side decorrelation ---- 81 let la: *i64 = sys_mmap(64) as *i64 82 let lb: *i64 = sys_mmap(64) as *i64 83 la[0] = 100; lb[0] = 30 84 nx_flac_undo_stereo(la, lb, 1, NX_FLAC_CH_LEFT_SIDE) 85 if la[0] != 100 { fails = fails + 1 } 86 if lb[0] != 70 { fails = fails + 1 } 87 88 let ra: *i64 = sys_mmap(64) as *i64 89 let rb: *i64 = sys_mmap(64) as *i64 90 ra[0] = 30; rb[0] = 70 91 nx_flac_undo_stereo(ra, rb, 1, NX_FLAC_CH_RIGHT_SIDE) 92 if ra[0] != 100 { fails = fails + 1 } 93 if rb[0] != 70 { fails = fails + 1 } 94 95 // ---- T8: mid/side with an ODD sum -- the dropped-low-bit trap ---- 96 // left=101 right=70 -> side=31, stored mid=(101+70)>>1=85 (low bit lost) 97 let ma: *i64 = sys_mmap(64) as *i64 98 let mb: *i64 = sys_mmap(64) as *i64 99 ma[0] = 85; mb[0] = 31 100 nx_flac_undo_stereo(ma, mb, 1, NX_FLAC_CH_MID_SIDE) 101 if ma[0] != 101 { fails = fails + 1 } 102 if mb[0] != 70 { fails = fails + 1 } 103 104 // ---- T9: block-size table, incl. the two deferred codes ---- 105 if nx_flac_block_size(1) != 192 { fails = fails + 1 } 106 if nx_flac_block_size(2) != 576 { fails = fails + 1 } 107 if nx_flac_block_size(5) != 4608 { fails = fails + 1 } 108 if nx_flac_block_size(8) != 256 { fails = fails + 1 } 109 if nx_flac_block_size(15) != 32768 { fails = fails + 1 } 110 if nx_flac_block_size(6) != 0 { fails = fails + 1 } 111 if nx_flac_block_size(0) != (0 - 1) { fails = fails + 1 } 112 113 // ---- T10: sample-rate table ---- 114 if nx_flac_sample_rate(9) != 44100 { fails = fails + 1 } 115 if nx_flac_sample_rate(10) != 48000 { fails = fails + 1 } 116 if nx_flac_sample_rate(4) != 8000 { fails = fails + 1 } 117 if nx_flac_sample_rate(15) != (0 - 1) { fails = fails + 1 } 118 if nx_flac_sample_rate(0) != 0 { fails = fails + 1 } 119 120 // ---- T11: bit-depth table ---- 121 if nx_flac_bit_depth(4) != 16 { fails = fails + 1 } 122 if nx_flac_bit_depth(6) != 24 { fails = fails + 1 } 123 if nx_flac_bit_depth(3) != (0 - 1) { fails = fails + 1 } 124 125 // ---- T12: CRC-8, polynomial 0x07 ---- 126 let cb: *u8 = sys_mmap(64) 127 cb[0] = 0x00 as u8 128 if nx_flac_crc8(cb, 1) != 0 { fails = fails + 1 } 129 cb[0] = 0x01 as u8 130 if nx_flac_crc8(cb, 1) != 0x07 { fails = fails + 1 } 131 132 // ---- T13: Rice decode off a real MSB-first bitstream ---- 133 // 0x10 = 0,0,0,1,... -> q=3, param=0 -> folded 3 -> signed -2 134 let r1b: *u8 = sys_mmap(64) 135 r1b[0] = 0x10 as u8 136 let bs1: *NxBitStream = nx_bitstream_alloc(r1b, 1) 137 if nx_flac_read_rice(bs1, 0) != (0 - 2) { fails = fails + 1 } 138 // 0x60 = 0,1 then '10' -> q=1, param=2, r=2 -> folded 6 -> signed 3 139 let r2b: *u8 = sys_mmap(64) 140 r2b[0] = 0x60 as u8 141 let bs2: *NxBitStream = nx_bitstream_alloc(r2b, 1) 142 if nx_flac_read_rice(bs2, 2) != 3 { fails = fails + 1 } 143 144 // ---- T14 NEG: stream magic ---- 145 let mg: *u8 = sys_mmap(64) 146 mg[0] = 0x66 as u8; mg[1] = 0x4c as u8; mg[2] = 0x61 as u8; mg[3] = 0x43 as u8 147 if nx_flac_is_stream(mg, 4) != 1 { fails = fails + 1 } 148 mg[1] = 0x4d as u8 149 if nx_flac_is_stream(mg, 4) != 0 { fails = fails + 1 } 150 if nx_flac_is_stream(mg, 2) != 0 { fails = fails + 1 } 151 152 // ---- T15 NEG: out-of-range orders are REFUSED ---- 153 let nb: *i64 = sys_mmap(64) as *i64 154 nb[0] = 1; nb[1] = 1; nb[2] = 1; nb[3] = 1; nb[4] = 1; nb[5] = 1 155 if nx_flac_restore_fixed(nb, 6, 5) != 0 { fails = fails + 1 } 156 if nx_flac_restore_lpc(nb, 6, 0, co, 1) != 0 { fails = fails + 1 } 157 158 if fails == 0 { 159 g_puts("GATE nx_flac verdict=GREEN pass=15/15 (zigzag sign + fold roundtrip; fixed predictors 0-4; order-1/2 restore; LPC identity; left/right/MID-SIDE odd-sum low-bit; block-size/rate/depth tables; CRC-8 0x07; Rice off real MSB bitstream; NEG magic/order refused)\n" as *u8) 160 sys_exit(0) 161 return 0 162 } 163 g_puts("GATE nx_flac verdict=RED\n" as *u8) 164 sys_exit(1) 165 return 1 166}