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