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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#![crate_name = "simplemad_sys"]
extern crate libc;
use libc::{c_void, c_char, c_int, c_uint, c_ushort, c_long, uint16_t};
use std::fmt::{self, Result, Debug};
use std::ptr;
pub use libc::c_ulong;
#[link(name = "mad")]
extern {
pub fn mad_decoder_init(decoder: *mut MadDecoder,
message: *mut c_void,
input_cb: extern fn(message: *mut c_void,
stream: &MadStream) -> MadFlow,
header_cb: extern fn(message: *mut c_void,
header: &MadHeader) -> MadFlow,
filter_cb: extern fn(),
output_cb: extern fn(message: *mut c_void,
header: &MadHeader,
pcm: &MadPcm) -> MadFlow,
error_cb: extern fn(message: *mut c_void,
stream: &MadStream,
frame: *const c_void) -> MadFlow,
message_cb: extern fn());
pub fn mad_decoder_run(decoder: &mut MadDecoder, mode: MadDecoderMode) -> c_int;
pub fn mad_decoder_finish(decoder: &mut MadDecoder) -> c_int;
pub fn mad_stream_buffer(stream: &MadStream, buf_start: *const u8, buf_len: c_ulong);
pub fn mad_header_init(header: &mut MadHeader);
pub fn mad_stream_init(stream: &mut MadStream);
pub fn mad_frame_init(frame: &mut MadFrame);
pub fn mad_synth_init(synth: &mut MadSynth);
pub fn mad_stream_finish(stream: &mut MadStream);
pub fn mad_frame_finish(frame: &mut MadFrame);
pub fn mad_header_decode(header: &mut MadHeader, stream: &mut MadStream);
pub fn mad_frame_decode(frame: &mut MadFrame, stream: &mut MadStream);
pub fn mad_synth_frame(synth: &mut MadSynth, frame: &mut MadFrame);
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub enum MadFlow {
Continue = 0x0000,
Stop = 0x0010,
Break = 0x0011,
Ignore = 0x0020,
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(C)]
pub enum MadError {
None = 0x0000,
BufLen = 0x0001,
BufPtr = 0x0002,
NoMem = 0x0031,
LostSync = 0x0101,
BadLayer = 0x0102,
BadBitRate = 0x0103,
BadSampleRate = 0x0104,
BadEmphasis = 0x0105,
BadCRC = 0x0201,
BadBitAlloc = 0x0211,
BadScaleFactor = 0x0221,
BadMode = 0x0222,
BadFrameLen = 0x0231,
BadBigValues = 0x0232,
BadBlockType = 0x0233,
BadScFSI = 0x0234,
BadDataPtr = 0x0235,
BadPart3Len = 0x0236,
BadHuffTable = 0x0237,
BadHuffData = 0x0238,
BadStereo = 0x0239,
}
impl Default for MadError {
fn default() -> MadError {
MadError::None
}
}
#[repr(C)]
pub struct MadBitPtr {
pub byte: *mut c_char,
pub cache: uint16_t,
pub left: uint16_t,
}
impl Default for MadBitPtr {
fn default() -> MadBitPtr {
MadBitPtr {
byte: ptr::null::<c_char>() as *mut c_char,
cache: 0,
left: 0,
}
}
}
#[repr(C)]
pub struct MadStream {
pub buffer: *mut c_char,
pub buff_end: *mut c_char,
pub skip_len: c_ulong,
pub sync: c_int,
pub free_rate: c_ulong,
pub this_frame: *mut c_char,
pub next_frame: *mut c_char,
pub ptr: MadBitPtr,
pub anc_ptr: MadBitPtr,
pub anc_bitlen: c_uint,
pub buffer_mdlen: *mut c_char,
pub md_len: c_uint,
pub options: c_int,
pub error: MadError,
}
impl Default for MadStream {
fn default() -> MadStream {
MadStream {
buffer: ptr::null::<c_char>() as *mut c_char,
buff_end: ptr::null::<c_char>() as *mut c_char,
skip_len: 0,
sync: 0,
free_rate: 0,
this_frame: ptr::null::<c_char>() as *mut c_char,
next_frame: ptr::null::<c_char>() as *mut c_char,
ptr: Default::default(),
anc_ptr: Default::default(),
anc_bitlen: 0,
buffer_mdlen: ptr::null::<c_char>() as *mut c_char,
md_len: 0,
options: 0,
error: MadError::None,
}
}
}
#[repr(C)]
pub struct MadFrame {
pub header: MadHeader,
pub options: c_int,
pub sbsample: [[[i32; 32]; 36]; 2],
pub overlap: *mut i32,
}
impl Default for MadFrame {
fn default() -> MadFrame {
MadFrame {
header: Default::default(),
options: 0,
sbsample: [[[0; 32]; 36]; 2],
overlap: ptr::null::<i32>() as *mut i32,
}
}
}
#[derive(Clone)]
#[repr(C)]
pub struct MadSynth {
pub filter: [[[[[i32; 8]; 16]; 2]; 2]; 2],
pub phase: c_uint,
pub pcm: MadPcm,
}
impl Default for MadSynth {
fn default() -> MadSynth {
MadSynth {
filter: [[[[[0; 8]; 16]; 2]; 2]; 2],
phase: 0,
pcm: Default::default(),
}
}
}
impl fmt::Debug for MadSynth {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"MadSynth {{phase: {}, pcm: {:?} }}",
self.phase,
self.pcm)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(C)]
pub enum MadLayer {
LayerI = 1,
LayerII = 2,
LayerIII = 3,
}
impl Default for MadLayer {
fn default() -> MadLayer {
MadLayer::LayerI
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(C)]
pub enum MadMode {
SingleChannel = 0,
DualChannel = 1,
JointStereo = 2,
Stereo = 3,
}
impl Default for MadMode {
fn default() -> MadMode {
MadMode::SingleChannel
}
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub enum MadEmphasis {
None = 0,
Fifty15Us = 1,
CcittJ17 = 3,
Reserved = 2,
}
impl Default for MadEmphasis {
fn default() -> MadEmphasis {
MadEmphasis::None
}
}
#[derive(Default, Debug, Clone, Copy)]
#[repr(C)]
pub struct MadTimer {
pub seconds: c_long,
pub fraction: c_ulong,
}
#[derive(Default, Debug, Clone, Copy)]
#[repr(C)]
pub struct MadHeader {
pub layer: MadLayer,
pub mode: MadMode,
pub mode_extension: c_int,
pub emphasis: MadEmphasis,
pub bit_rate: c_ulong,
pub sample_rate: c_uint,
pub crc_check: c_ushort,
pub crc_target: c_ushort,
pub flags: c_int,
pub private_bits: c_int,
pub duration: MadTimer,
}
#[derive(Clone, Copy)]
#[repr(C)]
pub struct MadPcm {
pub sample_rate: c_uint,
pub channels: uint16_t,
pub length: uint16_t,
pub samples: [[i32; 1152]; 2],
}
impl Default for MadPcm {
fn default() -> MadPcm {
MadPcm {
sample_rate: 0,
channels: 0,
length: 0,
samples: [[0; 1152]; 2],
}
}
}
impl fmt::Debug for MadPcm {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"MadPcm {{sample_rate: {}, channels: {}, length: {}}}",
self.sample_rate,
self.channels,
self.length)
}
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub enum MadDecoderMode {
Sync = 0,
Async = 1,
}
impl Default for MadDecoderMode {
fn default() -> MadDecoderMode {
MadDecoderMode::Sync
}
}
#[derive(Default, Debug, Clone, Copy)]
#[repr(C)]
struct MadAsyncParameters {
pid: c_long,
ain: c_int,
aout: c_int,
}
#[repr(C)]
pub struct MadDecoder {
mode: MadDecoderMode,
options: c_int,
async: MadAsyncParameters,
sync: *const c_void,
cb_data: *const c_void,
input_func: *const c_void,
header_func: *const c_void,
filter_func: *const c_void,
output_func: *const c_void,
error_func: *const c_void,
message_func: *const c_void,
}
impl Default for MadDecoder {
fn default() -> MadDecoder {
MadDecoder {
mode: Default::default(),
options: 0,
async: Default::default(),
sync: ptr::null::<c_void>(),
cb_data: ptr::null::<c_void>(),
input_func: ptr::null::<c_void>(),
header_func: ptr::null::<c_void>(),
filter_func: ptr::null::<c_void>(),
output_func: ptr::null::<c_void>(),
error_func: ptr::null::<c_void>(),
message_func: ptr::null::<c_void>(),
}
}
}
#[cfg(test)]
mod test {
}