Skip to main content

codec_core/utils/
tables.rs

1//! Lookup table utilities for codec optimizations
2
3/// Pre-computed μ-law decoding table (8-bit μ-law to 16-bit linear)
4pub static MULAW_DECODE_TABLE: [i16; 256] = [
5    16004, 14980, 13956, 12932, 11908, 10884, 9860, 8836, 7812, 6788, 5764, 4740, 3716, 2692, 1668,
6    644, 8068, 7556, 7044, 6532, 6020, 5508, 4996, 4484, 3972, 3460, 2948, 2436, 1924, 1412, 900,
7    388, 4100, 3844, 3588, 3332, 3076, 2820, 2564, 2308, 2052, 1796, 1540, 1284, 1028, 772, 516,
8    260, 2116, 1988, 1860, 1732, 1604, 1476, 1348, 1220, 1092, 964, 836, 708, 580, 452, 324, 196,
9    1124, 1060, 996, 932, 868, 804, 740, 676, 612, 548, 484, 420, 356, 292, 228, 164, 628, 596,
10    564, 532, 500, 468, 436, 404, 372, 340, 308, 276, 244, 212, 180, 148, 380, 364, 348, 332, 316,
11    300, 284, 268, 252, 236, 220, 204, 188, 172, 156, 140, 252, 244, 236, 228, 220, 212, 204, 196,
12    188, 180, 172, 164, 156, 148, 140, 132, -16004, -14980, -13956, -12932, -11908, -10884, -9860,
13    -8836, -7812, -6788, -5764, -4740, -3716, -2692, -1668, -644, -8068, -7556, -7044, -6532,
14    -6020, -5508, -4996, -4484, -3972, -3460, -2948, -2436, -1924, -1412, -900, -388, -4100, -3844,
15    -3588, -3332, -3076, -2820, -2564, -2308, -2052, -1796, -1540, -1284, -1028, -772, -516, -260,
16    -2116, -1988, -1860, -1732, -1604, -1476, -1348, -1220, -1092, -964, -836, -708, -580, -452,
17    -324, -196, -1124, -1060, -996, -932, -868, -804, -740, -676, -612, -548, -484, -420, -356,
18    -292, -228, -164, -628, -596, -564, -532, -500, -468, -436, -404, -372, -340, -308, -276, -244,
19    -212, -180, -148, -380, -364, -348, -332, -316, -300, -284, -268, -252, -236, -220, -204, -188,
20    -172, -156, -140, -252, -244, -236, -228, -220, -212, -204, -196, -188, -180, -172, -164, -156,
21    -148, -140, -132,
22];
23
24/// Pre-computed A-law decoding table (8-bit A-law to 16-bit linear)
25pub static ALAW_DECODE_TABLE: [i16; 256] = [
26    15880, 14856, 13832, 12808, 11784, 10760, 9736, 8712, 7688, 6664, 5640, 4616, 3592, 2568, 1544,
27    520, 7944, 7432, 6920, 6408, 5896, 5384, 4872, 4360, 3848, 3336, 2824, 2312, 1800, 1288, 776,
28    264, 3976, 3720, 3464, 3208, 2952, 2696, 2440, 2184, 1928, 1672, 1416, 1160, 904, 648, 392,
29    136, 1992, 1864, 1736, 1608, 1480, 1352, 1224, 1096, 968, 840, 712, 584, 456, 328, 200, 72,
30    1000, 936, 872, 808, 744, 680, 616, 552, 488, 424, 360, 296, 232, 168, 104, 40, 504, 472, 440,
31    408, 376, 344, 312, 280, 248, 216, 184, 152, 120, 88, 56, 24, 256, 240, 224, 208, 192, 176,
32    160, 144, 128, 112, 96, 80, 64, 48, 32, 16, 248, 232, 216, 200, 184, 168, 152, 136, 120, 104,
33    88, 72, 56, 40, 24, 8, -15880, -14856, -13832, -12808, -11784, -10760, -9736, -8712, -7688,
34    -6664, -5640, -4616, -3592, -2568, -1544, -520, -7944, -7432, -6920, -6408, -5896, -5384,
35    -4872, -4360, -3848, -3336, -2824, -2312, -1800, -1288, -776, -264, -3976, -3720, -3464, -3208,
36    -2952, -2696, -2440, -2184, -1928, -1672, -1416, -1160, -904, -648, -392, -136, -1992, -1864,
37    -1736, -1608, -1480, -1352, -1224, -1096, -968, -840, -712, -584, -456, -328, -200, -72, -1000,
38    -936, -872, -808, -744, -680, -616, -552, -488, -424, -360, -296, -232, -168, -104, -40, -504,
39    -472, -440, -408, -376, -344, -312, -280, -248, -216, -184, -152, -120, -88, -56, -24, -256,
40    -240, -224, -208, -192, -176, -160, -144, -128, -112, -96, -80, -64, -48, -32, -16, -248, -232,
41    -216, -200, -184, -168, -152, -136, -120, -104, -88, -72, -56, -40, -24, -8,
42];
43
44/// Fast μ-law encoding using direct computation
45pub fn encode_mulaw_table(sample: i16) -> u8 {
46    crate::utils::simd::linear_to_mulaw_scalar(sample)
47}
48
49/// Fast μ-law decoding using lookup table
50pub fn decode_mulaw_table(encoded: u8) -> i16 {
51    MULAW_DECODE_TABLE[encoded as usize]
52}
53
54/// Fast A-law encoding using direct computation
55pub fn encode_alaw_table(sample: i16) -> u8 {
56    crate::utils::simd::linear_to_alaw_scalar(sample)
57}
58
59/// Fast A-law decoding using lookup table
60pub fn decode_alaw_table(encoded: u8) -> i16 {
61    ALAW_DECODE_TABLE[encoded as usize]
62}
63
64/// Batch μ-law encoding using lookup tables
65pub fn encode_mulaw_batch(samples: &[i16], output: &mut [u8]) {
66    for (i, &sample) in samples.iter().enumerate() {
67        output[i] = encode_mulaw_table(sample);
68    }
69}
70
71/// Batch μ-law decoding using lookup tables
72pub fn decode_mulaw_batch(encoded: &[u8], output: &mut [i16]) {
73    for (i, &byte) in encoded.iter().enumerate() {
74        output[i] = decode_mulaw_table(byte);
75    }
76}
77
78/// Batch A-law encoding using lookup tables
79pub fn encode_alaw_batch(samples: &[i16], output: &mut [u8]) {
80    for (i, &sample) in samples.iter().enumerate() {
81        output[i] = encode_alaw_table(sample);
82    }
83}
84
85/// Batch A-law decoding using lookup tables
86pub fn decode_alaw_batch(encoded: &[u8], output: &mut [i16]) {
87    for (i, &byte) in encoded.iter().enumerate() {
88        output[i] = decode_alaw_table(byte);
89    }
90}
91
92/// Initialize all lookup tables
93pub fn init_tables() {
94    // Static arrays are already initialized at compile time
95    tracing::debug!("Codec lookup tables already initialized (1KB total)");
96}
97
98/// Get memory usage of lookup tables
99pub fn get_table_memory_usage() -> usize {
100    // Only decode tables:
101    // μ-law: 256 * 2 = 512 bytes
102    // A-law: 256 * 2 = 512 bytes
103    // Total: 1024 bytes (1KB)
104    let mulaw_decode_size = std::mem::size_of::<[i16; 256]>();
105    let alaw_decode_size = std::mem::size_of::<[i16; 256]>();
106
107    mulaw_decode_size + alaw_decode_size
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113
114    #[test]
115    fn test_table_initialization() {
116        // Test that static arrays are available
117        assert_eq!(MULAW_DECODE_TABLE.len(), 256);
118        assert_eq!(ALAW_DECODE_TABLE.len(), 256);
119
120        // Test first and last values are reasonable
121        assert_ne!(MULAW_DECODE_TABLE[0], 0);
122        assert_ne!(MULAW_DECODE_TABLE[255], 0);
123        assert_ne!(ALAW_DECODE_TABLE[0], 0);
124        assert_ne!(ALAW_DECODE_TABLE[255], 0);
125    }
126
127    #[test]
128    fn test_table_vs_scalar() {
129        // Test decode tables only (they're small and fast)
130        let test_encoded = vec![0, 127, 128, 255];
131
132        for encoded in test_encoded {
133            // Test μ-law decode
134            let table_result = decode_mulaw_table(encoded);
135            let scalar_result = crate::utils::simd::mulaw_to_linear_scalar(encoded);
136            assert_eq!(
137                table_result, scalar_result,
138                "μ-law decode table mismatch for encoded {}",
139                encoded
140            );
141
142            // Test A-law decode
143            let table_result = decode_alaw_table(encoded);
144            let scalar_result = crate::utils::simd::alaw_to_linear_scalar(encoded);
145            assert_eq!(
146                table_result, scalar_result,
147                "A-law decode table mismatch for encoded {}",
148                encoded
149            );
150        }
151    }
152
153    #[test]
154    fn test_batch_operations() {
155        // Test decode batch operations only (they're fast)
156        let encoded = vec![0u8, 127, 128, 255];
157        let mut decoded = vec![0i16; encoded.len()];
158
159        // Test μ-law batch decode
160        decode_mulaw_batch(&encoded, &mut decoded);
161
162        // Verify we got some non-zero results
163        assert_ne!(decoded[1], 0);
164        assert_ne!(decoded[2], 0);
165        assert_ne!(decoded[3], 0);
166
167        // Test A-law batch decode
168        decode_alaw_batch(&encoded, &mut decoded);
169
170        // Verify we got some non-zero results
171        assert_ne!(decoded[1], 0);
172        assert_ne!(decoded[2], 0);
173        assert_ne!(decoded[3], 0);
174    }
175
176    #[test]
177    fn test_memory_usage() {
178        let usage = get_table_memory_usage();
179
180        // Expected: 2 * 256 * 2 bytes = 1024 bytes
181        assert_eq!(usage, 1024);
182    }
183
184    #[test]
185    fn test_edge_cases() {
186        // Test boundary values for decode operations only
187        let edge_cases = vec![0u8, 127, 128, 255];
188
189        for encoded in edge_cases {
190            // Decoders return `i16`; the value range is guaranteed by
191            // the type. We only need to assert they don't panic.
192            let _ = decode_mulaw_table(encoded);
193            let _ = decode_alaw_table(encoded);
194        }
195    }
196}