audio_codec/
pcma.rs

1use super::{Decoder, Encoder, PcmBuf, Sample};
2
3const SEG_SHIFT: i16 = 4;
4const QUANT_MASK: i16 = 0x0F;
5const SEG_END: [i16; 8] = [0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF];
6
7const fn search(val: i16, table: &[i16], size: usize) -> usize {
8    let mut i = 0;
9    while i < size {
10        if val <= table[i] {
11            return i;
12        }
13        i += 1;
14    }
15    size
16}
17
18const fn linear2alaw_algo(pcm_val: i16) -> u8 {
19    // Special case handling for small negative values [-8, -1]
20    if pcm_val < 0 && pcm_val >= -8 {
21        return 0xD5;
22    }
23
24    // Determine sign mask and prepare the positive sample value
25    let (mask, abs_val) = if pcm_val >= 0 {
26        (0xD5, pcm_val) // sign bit = 1
27    } else {
28        // Handle the edge case of -32768 (i16::MIN), which would overflow when negated
29        if pcm_val == i16::MIN {
30            (0x55, i16::MAX) // Use the maximum positive value
31        } else {
32            (0x55, -pcm_val - 8) // sign bit = 0
33        }
34    };
35
36    // Convert the scaled magnitude to segment number
37    let seg = search(abs_val, &SEG_END, 8);
38
39    // If out of range, return maximum value
40    if seg >= 8 {
41        return (0x7F ^ mask) as u8;
42    }
43
44    // Calculate the base value from segment
45    let aval = (seg as i16) << SEG_SHIFT;
46
47    // Combine the segment value with the quantization bits
48    let shift = if seg < 2 { 4 } else { seg as i16 + 3 };
49    let result = aval | ((abs_val >> shift) & QUANT_MASK);
50
51    // Apply the mask to set the sign bit
52    (result ^ mask) as u8
53}
54
55static ALAW_ENCODE_TABLE: [u8; 65536] = {
56    let mut table = [0; 65536];
57    let mut i = 0;
58    while i < 65536 {
59        let s = (i as i32 - 32768) as i16;
60        table[i] = linear2alaw_algo(s);
61        i += 1;
62    }
63    table
64};
65
66// A-law decode table (same as Go's alaw2lpcm)
67static ALAW_DECODE_TABLE: [i16; 256] = [
68    -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528,
69    -6272, -7040, -6784, -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368, -3776, -3648,
70    -4032, -3904, -3264, -3136, -3520, -3392, -22016, -20992, -24064, -23040, -17920, -16896,
71    -19968, -18944, -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136, -11008, -10496,
72    -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592, -16128, -15616, -13056, -12544,
73    -14080, -13568, -344, -328, -376, -360, -280, -264, -312, -296, -472, -456, -504, -488, -408,
74    -392, -440, -424, -88, -72, -120, -104, -24, -8, -56, -40, -216, -200, -248, -232, -152, -136,
75    -184, -168, -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952,
76    -1632, -1568, -1760, -1696, -688, -656, -752, -720, -560, -528, -624, -592, -944, -912, -1008,
77    -976, -816, -784, -880, -848, 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064,
78    7808, 6528, 6272, 7040, 6784, 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368, 3776, 3648, 4032,
79    3904, 3264, 3136, 3520, 3392, 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944, 30208,
80    29184, 32256, 31232, 26112, 25088, 28160, 27136, 11008, 10496, 12032, 11520, 8960, 8448, 9984,
81    9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568, 344, 328, 376, 360, 280, 264,
82    312, 296, 472, 456, 504, 488, 408, 392, 440, 424, 88, 72, 120, 104, 24, 8, 56, 40, 216, 200,
83    248, 232, 152, 136, 184, 168, 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184, 1888, 1824, 2016,
84    1952, 1632, 1568, 1760, 1696, 688, 656, 752, 720, 560, 528, 624, 592, 944, 912, 1008, 976, 816,
85    784, 880, 848,
86];
87
88/// Decoder for A-law (PCMA) format
89#[derive(Default)]
90pub struct PcmaDecoder {}
91
92impl PcmaDecoder {
93    /// Creates a new PcmaDecoder instance
94    pub fn new() -> Self {
95        Self {}
96    }
97}
98
99impl Decoder for PcmaDecoder {
100    fn decode(&mut self, samples: &[u8]) -> PcmBuf {
101        samples.iter().map(|&sample| decode_a_law(sample)).collect()
102    }
103
104    fn sample_rate(&self) -> u32 {
105        8000
106    }
107
108    fn channels(&self) -> u16 {
109        1
110    }
111}
112
113/// Decodes a single A-law encoded byte to a 16-bit PCM sample
114fn decode_a_law(a_law_sample: u8) -> i16 {
115    ALAW_DECODE_TABLE[a_law_sample as usize]
116}
117
118/// Encoder for A-law (PCMA) format
119#[derive(Default)]
120pub struct PcmaEncoder {}
121
122impl PcmaEncoder {
123    /// Creates a new PcmaEncoder instance
124    pub fn new() -> Self {
125        Self {}
126    }
127
128    /// Converts a linear 16-bit PCM sample to an A-law encoded byte using lookup table
129    fn linear2alaw(&self, sample: i16) -> u8 {
130        let index = (sample as i32 + 32768) as usize;
131        ALAW_ENCODE_TABLE[index]
132    }
133}
134
135impl Encoder for PcmaEncoder {
136    fn encode(&mut self, samples: &[Sample]) -> Vec<u8> {
137        samples
138            .iter()
139            .map(|&sample| self.linear2alaw(sample))
140            .collect()
141    }
142
143    fn sample_rate(&self) -> u32 {
144        8000
145    }
146
147    fn channels(&self) -> u16 {
148        1
149    }
150}