libaec_sys/
lib.rs

1#![allow(non_camel_case_types, unused)]
2include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
3
4#[cfg(test)]
5mod tests {
6    use super::*;
7
8    struct Stream(aec_stream);
9
10    impl Stream {
11        fn new(bits_per_sample: u32, block_size: u32, rsi: u32, flags: u32) -> Self {
12            let mut raw: aec_stream = unsafe { std::mem::zeroed() };
13            raw.bits_per_sample = bits_per_sample;
14            raw.block_size = block_size;
15            raw.rsi = rsi;
16            raw.flags = flags;
17            Self(raw)
18        }
19
20        fn total_in(&self) -> usize {
21            self.0.total_in
22        }
23
24        fn total_out(&self) -> usize {
25            self.0.total_out
26        }
27
28        fn encode(&mut self, input: &[u8], output: &mut [u8]) -> Result<(), &'static str> {
29            self.0.next_in = input.as_ptr();
30            self.0.avail_in = input.len();
31            self.0.next_out = output.as_mut_ptr();
32            self.0.avail_out = output.len();
33
34            let result = unsafe { aec_encode_init(&mut self.0) };
35            if result as u32 != AEC_OK {
36                return Err("aec_encode_init() failed");
37            }
38
39            let result = unsafe { aec_encode(&mut self.0, AEC_FLUSH as i32) };
40            if result as u32 != AEC_OK {
41                return Err("aec_encode() failed");
42            }
43
44            let result = unsafe { aec_encode_end(&mut self.0) };
45            if result as u32 != AEC_OK {
46                return Err("aec_encode_end() failed"); // FIXME: support
47                                                       // incomplete processing
48            }
49
50            Ok(())
51        }
52
53        fn decode(&mut self, input: &[u8], output: &mut [u8]) -> Result<(), &'static str> {
54            self.0.next_in = input.as_ptr();
55            self.0.avail_in = input.len();
56            self.0.next_out = output.as_mut_ptr();
57            self.0.avail_out = output.len();
58
59            let result = unsafe { aec_decode_init(&mut self.0) };
60            if result as u32 != AEC_OK {
61                return Err("aec_decode_init() failed");
62            }
63
64            let result = unsafe { aec_decode(&mut self.0, AEC_FLUSH as i32) };
65            if result as u32 != AEC_OK {
66                return Err("aec_decode() failed");
67            }
68
69            let result = unsafe { aec_decode_end(&mut self.0) };
70            if result as u32 != AEC_OK {
71                return Err("aec_decode_end() failed"); // FIXME: support
72                                                       // incomplete processing
73            }
74
75            Ok(())
76        }
77    }
78
79    #[test]
80    fn aec_encoding_and_decoding_works() {
81        let input = [
82            [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0],
83            [3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0],
84            [7, 0, 0, 0, 8, 0, 0, 0, 9, 0, 0, 0, 10, 0, 0, 0],
85            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
86            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
87            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
88            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
89            [8, 8, 8, 8, 12, 12, 12, 12, 4, 4, 4, 4, 15, 15, 15, 15],
90        ];
91        let input = input.as_flattened();
92
93        let bits_per_sample = 4;
94        let block_size = 16;
95        let rsi = 128;
96        let flags = AEC_DATA_MSB;
97
98        let mut stream = Stream::new(bits_per_sample, block_size, rsi, flags);
99        let mut encoded = vec![0; 100];
100        let result = stream.encode(&input, &mut encoded);
101        assert!(result.is_ok());
102
103        let encoded = &encoded[..stream.total_out()];
104        let expected_encoded = [
105            0x1f, 0x63, 0x23, 0xc3, 0xc1, 0xe0, 0x7a, 0x1e, 0x1e, 0x1e, 0x0f, 0x80, 0x80, 0x01,
106            0xf1, 0x11, 0x19, 0x99, 0x88, 0x88, 0x9f, 0xff, 0xe0,
107        ];
108        assert_eq!(encoded, expected_encoded);
109
110        let mut stream = Stream::new(bits_per_sample, block_size, rsi, flags);
111        let mut decoded = vec![0; 100];
112        let result = stream.decode(&encoded, &mut decoded);
113        assert!(result.is_ok());
114
115        assert_eq!(stream.total_out(), decoded.len());
116        assert_eq!(&decoded[..stream.total_out()], &input[..stream.total_out()]);
117    }
118}