Skip to main content

g729_sys/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2
3pub mod g729;
4
5/// One G.729 frame contains 80 16-bit PCM samples at 8 kHz.
6pub const FRAME_SAMPLES: usize = 80;
7/// Voice frame payload length in bytes (10 bytes = 80 bits).
8pub const VOICE_FRAME_BYTES: usize = 10;
9
10/// Encoder wrapper.
11pub struct Encoder {
12    inner: g729::encoder::Encoder,
13}
14
15impl Encoder {
16    /// Create a new G.729 encoder.
17    ///
18    /// `enable_vad` toggles Annex B (VAD/DTX).
19    pub fn new(enable_vad: bool) -> Self {
20        Self {
21            inner: g729::encoder::Encoder::new(enable_vad),
22        }
23    }
24
25    /// Encode one 80-sample frame into the caller-provided buffer.
26    ///
27    /// Returns the number of bytes written to `out` (always `<= VOICE_FRAME_BYTES`).
28    /// This method is always available, including in `no_std`.
29    pub fn encode_into(
30        &mut self,
31        input_80_samples: &[i16; FRAME_SAMPLES],
32        out: &mut [u8; VOICE_FRAME_BYTES],
33    ) -> u8 {
34        let mut len: u8 = 0;
35        self.inner.encode(input_80_samples, out, &mut len);
36        len
37    }
38
39    /// Encode one 80-sample frame into a fresh `Vec<u8>`.
40    ///
41    /// Only available with the `std` feature (enabled by default).
42    #[cfg(feature = "std")]
43    pub fn encode(&mut self, input_80_samples: &[i16; FRAME_SAMPLES]) -> Vec<u8> {
44        let mut out = [0u8; VOICE_FRAME_BYTES];
45        let len = self.encode_into(input_80_samples, &mut out);
46        out[..len as usize].to_vec()
47    }
48
49    pub fn rfc3389_payload(&mut self) -> [u8; 11] {
50        // Not implemented in Rust backend yet, return zeros or implement if needed
51        [0u8; 11]
52    }
53}
54
55/// Decoder wrapper.
56pub struct Decoder {
57    inner: g729::decoder::Decoder,
58}
59
60impl Default for Decoder {
61    fn default() -> Self {
62        Self::new()
63    }
64}
65
66impl Decoder {
67    /// Create a new G.729 decoder.
68    pub fn new() -> Self {
69        Self {
70            inner: g729::decoder::Decoder::new(),
71        }
72    }
73
74    pub fn decode(
75        &mut self,
76        payload: &[u8],
77        frame_erased: bool,
78        is_sid: bool,
79        rfc3389: bool,
80    ) -> [i16; FRAME_SAMPLES] {
81        let mut out = [0i16; FRAME_SAMPLES];
82        let len = payload.len() as u8;
83        let payload_opt = if len > 0 { Some(payload) } else { None };
84
85        self.inner.decode(
86            payload_opt,
87            len,
88            frame_erased as u8,
89            is_sid as u8,
90            rfc3389 as u8,
91            &mut out,
92        );
93        out
94    }
95}