voice_engine/media/codecs/
pcma.rs

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