1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
use leonida_xma2_decoder::*;
use std::ptr;
use xma::bits::BitWriter;
use xma::Xma2Stream;
const PACKET_BYTES: usize = 2048;
fn synthetic_silent_packet(channels: usize) -> [u8; PACKET_BYTES] {
assert!(channels == 1 || channels == 2);
let mut frame = BitWriter::default();
frame.put(1, 1); // Fixed-size tile header.
frame.put(1, 0); // One full-length subframe.
if channels == 2 {
frame.put(1, 0); // No optional stereo decorrelation matrix.
}
frame.put(8, 0); // No dynamic range adjustment.
frame.put(1, 0); // No trim metadata.
frame.put(1, 0); // No fill data.
frame.put(1, 0); // No corrupt-subframe marker.
if channels == 2 {
frame.put(1, 0); // No stereo transform error.
frame.put(1, 1); // Two-channel group without decorrelation.
frame.put(1, 0);
}
for _ in 0..channels {
frame.put(1, 0); // No transmitted coefficients: silence.
}
frame.put(1, 0); // Align to the next frame boundary.
frame.put(1, 0); // This packet contains one frame.
let frame_bits = 15 + frame.bits;
let mut packet_writer = BitWriter::default();
packet_writer.put(6, 1);
packet_writer.put(15, frame_bits as u32);
packet_writer.put(3, 1);
packet_writer.put(8, 0);
packet_writer.put(15, frame_bits as u32);
packet_writer.copy_from(&frame.buf, 0, frame.bits);
let mut packet = [0u8; PACKET_BYTES];
packet[..packet_writer.buf.len()].copy_from_slice(&packet_writer.buf);
packet
}
fn make_decoder(rate: u32, channels: u32) -> *mut LeonidaXma2Decoder {
let mut decoder = ptr::null_mut();
let result = unsafe { leonida_xma2_decoder_create(rate, channels, &mut decoder) };
assert_eq!(result, LEONIDA_XMA2_OK);
assert!(!decoder.is_null());
decoder
}
#[test]
fn hardware_mode_keeps_all_samples_of_the_first_stereo_frame() {
let mut packet = synthetic_silent_packet(2);
packet[..4].copy_from_slice(&[4, 0, 1, 0]); // One frame, no continuation from a previous packet.
let mut decoder = ptr::null_mut();
let mut block = ptr::null_mut();
unsafe {
assert_eq!(
leonida_xma2_decoder_create_hardware(48_000, 2, &mut decoder),
LEONIDA_XMA2_OK
);
assert_eq!(
leonida_xma2_decode_buffer(decoder, packet.as_ptr(), packet.len(), &mut block),
LEONIDA_XMA2_OK
);
let count = leonida_xma2_pcm_sample_count(block);
leonida_xma2_pcm_destroy(block);
leonida_xma2_decoder_destroy(decoder);
assert_eq!(count, 512 * 2);
}
}
#[test]
fn creation_rejects_invalid_outputs_and_formats() {
assert_eq!(
unsafe { leonida_xma2_decoder_create(48_000, 2, ptr::null_mut()) },
LEONIDA_XMA2_INVALID_ARGUMENT
);
let mut decoder = ptr::dangling_mut::<LeonidaXma2Decoder>();
assert_eq!(
unsafe { leonida_xma2_decoder_create(48_000, 3, &mut decoder) },
LEONIDA_XMA2_INVALID_FORMAT
);
assert!(decoder.is_null());
assert_eq!(
unsafe { leonida_xma2_decoder_create(0, 1, &mut decoder) },
LEONIDA_XMA2_INVALID_FORMAT
);
assert!(decoder.is_null());
}
#[test]
fn synthetic_silent_packet_returns_owned_pcm_block_after_decoder_destruction() {
let decoder = make_decoder(48_000, 2);
let packet = synthetic_silent_packet(2);
let mut input = [0u8; PACKET_BYTES * 3];
input[PACKET_BYTES..PACKET_BYTES * 2].copy_from_slice(&packet);
input[PACKET_BYTES * 2..].copy_from_slice(&packet);
let mut block = ptr::null_mut();
assert_eq!(
unsafe { leonida_xma2_decode_buffer(decoder, input.as_ptr(), input.len(), &mut block,) },
LEONIDA_XMA2_OK
);
assert!(!block.is_null());
let samples = unsafe { leonida_xma2_pcm_sample_count(block) };
assert_eq!(samples % 2, 0);
assert!(samples > 0);
let pcm = unsafe { leonida_xma2_pcm_data(block) };
assert!(!pcm.is_null());
let decoded = unsafe { std::slice::from_raw_parts(pcm, samples) };
assert!(decoded.iter().all(|sample| *sample == 0));
unsafe { leonida_xma2_decoder_destroy(decoder) };
assert_eq!(unsafe { leonida_xma2_pcm_sample_count(block) }, samples);
assert_eq!(
unsafe { std::slice::from_raw_parts(leonida_xma2_pcm_data(block), samples) },
decoded
);
unsafe { leonida_xma2_pcm_destroy(block) };
}
#[test]
fn synthetic_silent_mono_stream_decodes_to_silent_pcm() {
let decoder = make_decoder(44_100, 1);
let packet = synthetic_silent_packet(1);
let mut input = [0u8; PACKET_BYTES * 3];
input[PACKET_BYTES..PACKET_BYTES * 2].copy_from_slice(&packet);
input[PACKET_BYTES * 2..].copy_from_slice(&packet);
let mut block = ptr::null_mut();
assert_eq!(
unsafe { leonida_xma2_decode_buffer(decoder, input.as_ptr(), input.len(), &mut block) },
LEONIDA_XMA2_OK
);
assert!(!block.is_null());
let samples = unsafe { leonida_xma2_pcm_sample_count(block) };
assert!(samples > 0);
let pcm = unsafe { leonida_xma2_pcm_data(block) };
assert!(!pcm.is_null());
assert!(unsafe { std::slice::from_raw_parts(pcm, samples) }
.iter()
.all(|sample| *sample == 0));
unsafe {
leonida_xma2_pcm_destroy(block);
leonida_xma2_decoder_destroy(decoder);
}
}
#[test]
fn decode_rejects_nulls_and_non_packet_aligned_buffers() {
let decoder = make_decoder(44_100, 1);
let packet = [0u8; PACKET_BYTES];
let mut block = ptr::dangling_mut::<LeonidaXma2PcmBlock>();
assert_eq!(
unsafe { leonida_xma2_decode_buffer(decoder, packet.as_ptr(), 0, &mut block) },
LEONIDA_XMA2_BAD_PACKET_SIZE
);
assert!(block.is_null());
assert_eq!(
unsafe {
leonida_xma2_decode_buffer(decoder, packet.as_ptr(), PACKET_BYTES - 1, &mut block)
},
LEONIDA_XMA2_BAD_PACKET_SIZE
);
assert!(block.is_null());
assert_eq!(
unsafe { leonida_xma2_decode_buffer(decoder, ptr::null(), PACKET_BYTES, &mut block) },
LEONIDA_XMA2_INVALID_ARGUMENT
);
assert!(block.is_null());
unsafe { leonida_xma2_decoder_destroy(decoder) };
}
#[test]
fn deterministic_synthetic_packets_do_not_unwind_across_ffi() {
let decoder = make_decoder(48_000, 2);
for seed in 1..17u32 {
let mut state = seed.wrapping_mul(2_654_435_761).wrapping_add(1);
let mut packet = [0u8; PACKET_BYTES];
for byte in &mut packet {
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
*byte = state as u8;
}
let mut block = ptr::null_mut();
let result = unsafe {
leonida_xma2_decode_buffer(decoder, packet.as_ptr(), packet.len(), &mut block)
};
assert!(
result == LEONIDA_XMA2_OK
|| result == LEONIDA_XMA2_DECODER_FAILURE
|| result == LEONIDA_XMA2_PANIC,
"unexpected status {result}"
);
if !block.is_null() {
unsafe { leonida_xma2_pcm_destroy(block) };
}
}
unsafe { leonida_xma2_decoder_destroy(decoder) };
}
#[test]
fn multiple_packet_buffer_matches_serial_decoder_calls() {
let decoder = make_decoder(48_000, 2);
let packet = synthetic_silent_packet(2);
let mut input = [0u8; PACKET_BYTES * 3];
input[PACKET_BYTES..PACKET_BYTES * 2].copy_from_slice(&packet);
input[PACKET_BYTES * 2..].copy_from_slice(&packet);
let mut reference = Xma2Stream::new(48_000, 2).unwrap();
let mut expected = Vec::new();
for packet in input.as_chunks::<PACKET_BYTES>().0 {
reference.decode_packet(packet, &mut expected);
}
let mut block = ptr::null_mut();
assert_eq!(
unsafe { leonida_xma2_decode_buffer(decoder, input.as_ptr(), input.len(), &mut block) },
LEONIDA_XMA2_OK
);
assert!(!block.is_null());
let samples = unsafe { leonida_xma2_pcm_sample_count(block) };
assert!(samples > 0);
assert_eq!(samples, expected.len());
assert_eq!(samples % 2, 0);
let pcm = unsafe { leonida_xma2_pcm_data(block) };
let actual = unsafe { std::slice::from_raw_parts(pcm, samples) };
assert_eq!(actual, expected.as_slice());
unsafe {
leonida_xma2_pcm_destroy(block);
leonida_xma2_decoder_destroy(decoder);
}
}
|