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"); }
48
49 Ok(())
50 }
51
52 fn decode(&mut self, input: &[u8], output: &mut [u8]) -> Result<(), &'static str> {
53 self.0.next_in = input.as_ptr();
54 self.0.avail_in = input.len();
55 self.0.next_out = output.as_mut_ptr();
56 self.0.avail_out = output.len();
57
58 let result = unsafe { aec_decode_init(&mut self.0) };
59 if result as u32 != AEC_OK {
60 return Err("aec_decode_init() failed");
61 }
62
63 let result = unsafe { aec_decode(&mut self.0, AEC_FLUSH as i32) };
64 if result as u32 != AEC_OK {
65 return Err("aec_decode() failed");
66 }
67
68 let result = unsafe { aec_decode_end(&mut self.0) };
69 if result as u32 != AEC_OK {
70 return Err("aec_decode_end() failed"); }
72
73 Ok(())
74 }
75 }
76
77 #[test]
78 fn aec_encoding_and_decoding_works() {
79 let input = [
80 [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0],
81 [3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0],
82 [7, 0, 0, 0, 8, 0, 0, 0, 9, 0, 0, 0, 10, 0, 0, 0],
83 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
84 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 [8, 8, 8, 8, 12, 12, 12, 12, 4, 4, 4, 4, 15, 15, 15, 15],
88 ];
89 let input = input.as_flattened();
90
91 let bits_per_sample = 4;
92 let block_size = 16;
93 let rsi = 128;
94 let flags = AEC_DATA_MSB;
95
96 let mut stream = Stream::new(bits_per_sample, block_size, rsi, flags);
97 let mut encoded = vec![0; 100];
98 let result = stream.encode(&input, &mut encoded);
99 assert!(result.is_ok());
100
101 let encoded = &encoded[..stream.total_out()];
102 let expected_encoded = [
103 0x1f, 0x63, 0x23, 0xc3, 0xc1, 0xe0, 0x7a, 0x1e, 0x1e, 0x1e, 0x0f, 0x80, 0x80, 0x01,
104 0xf1, 0x11, 0x19, 0x99, 0x88, 0x88, 0x9f, 0xff, 0xe0,
105 ];
106 assert_eq!(encoded, expected_encoded);
107
108 let mut stream = Stream::new(bits_per_sample, block_size, rsi, flags);
109 let mut decoded = vec![0; 100];
110 let result = stream.decode(&encoded, &mut decoded);
111 assert!(result.is_ok());
112
113 assert_eq!(stream.total_out(), decoded.len());
114 assert_eq!(&decoded[..stream.total_out()], &input[..stream.total_out()]);
115 }
116}