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
#![doc(html_root_url = "http://www.rust-ci.org/tari/vorbisfile.rs/doc/libvorbisfile/")]
#![deny(dead_code, missing_docs)]
#![feature(unsafe_destructor)]
#![allow(unstable)]
extern crate libc;
use libc::{c_void, c_int, c_long, size_t, c_char};
use std::error::Error;
use std::ffi::c_str_to_bytes;
use std::fmt;
use std::mem;
use std::str;
use std::ptr;
use std::raw;
use std::slice::from_raw_mut_buf;
#[allow(dead_code, non_snake_case)]
mod ffi;
pub type OVResult<T> = Result<T, OVError>;
#[derive(Show, Clone)]
pub enum OVError {
EndOfStream,
StreamInterrupted,
ReadError,
InternalFault,
NotImplemented,
InvalidArgument,
NotVorbis,
InvalidHeader,
UnsupportedVersion,
CorruptLink,
NotSeekable,
}
impl Error for OVError {
fn description(&self) -> &str {
match *self {
OVError::EndOfStream => "End of stream",
OVError::StreamInterrupted => "Stream interrupted",
OVError::ReadError => "Read error",
OVError::InternalFault => "Internal library fault",
OVError::NotImplemented => "Feature not implemented",
OVError::InvalidArgument => "Invalid argument",
OVError::NotVorbis => "Not a Vorbis stream",
OVError::InvalidHeader => "Invalid Vorbis header",
OVError::UnsupportedVersion => "Bitstream format revision not supported",
OVError::CorruptLink => "Vorbis link is corrupt",
OVError::NotSeekable => "Not seekable",
}
}
}
impl fmt::Display for OVError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str(self.description())
}
}
impl Copy for OVError { }
impl OVError {
fn from_native(code: c_int) -> OVError {
match code {
ffi::OV_HOLE => OVError::StreamInterrupted,
ffi::OV_EREAD => OVError::ReadError,
ffi::OV_EFAULT => OVError::InternalFault,
ffi::OV_EIMPL => OVError::NotImplemented,
ffi::OV_EINVAL => OVError::InvalidArgument,
ffi::OV_ENOTVORBIS => OVError::NotVorbis,
ffi::OV_EBADHEADER => OVError::InvalidHeader,
ffi::OV_EVERSION => OVError::UnsupportedVersion,
ffi::OV_EBADLINK => OVError::CorruptLink,
ffi::OV_ENOSEEK => OVError::NotSeekable,
x => panic!("Unexpected OVError code: {}", x)
}
}
}
pub struct VorbisFile<R> {
src: R,
decoder: ffi::OggVorbis_File,
channels: Vec<raw::Slice<f32>>,
}
pub struct Comments<'a> {
pub vendor: &'a str,
pub comments: Vec<&'a str>
}
#[allow(unused_variables)]
extern "C" fn seek(datasource: *mut c_void, offset: i64, whence: c_int) -> c_int {
-1
}
#[allow(unused_variables)]
extern "C" fn close(datasource: *mut c_void) -> c_int {
0
}
#[allow(unused_variables)]
extern "C" fn tell(datasource: *mut c_void) -> c_long {
-1
}
impl<R: Reader> VorbisFile<R> {
fn callback_setup(&mut self) {
let ds = self as *mut _ as *mut c_void;
self.decoder.datasource = ds;
}
pub fn new(src: R) -> OVResult<VorbisFile<R>> {
let mut vf = VorbisFile {
src: src,
decoder: unsafe { mem::uninitialized() },
channels: Vec::new()
};
let callbacks = ffi::ov_callbacks {
read: VorbisFile::<R>::read,
seek: seek,
tell: tell,
close: close,
};
let status = unsafe {
ffi::ov_open_callbacks(&mut vf.src as *mut _ as *mut c_void,
&mut vf.decoder as *mut _,
ptr::null_mut(), 0, callbacks)
};
match status {
0 => Ok(vf),
f => {
unsafe {
mem::forget(vf);
}
Err(OVError::from_native(f))
}
}
}
pub fn comment<'a>(&'a mut self, link: isize) -> Option<Comments<'a>> {
let cm = unsafe {
match ffi::ov_comment(&mut self.decoder, link as c_int).as_ref() {
Some(r) => r,
None => return None
}
};
unsafe fn make_str<'a>(data: *const u8, len: usize) -> Option<&'a str> {
let slice = raw::Slice {
data: data,
len: len
};
str::from_utf8(mem::transmute(slice)).ok()
}
Some(Comments {
vendor: unsafe {
let vendor_raw: &'a *const c_char = mem::copy_lifetime(self, &(cm.vendor as *const _));
match str::from_utf8(c_str_to_bytes(vendor_raw)) {
Ok(x) => x,
Err(_) => "<INVALID>"
}
},
comments: unsafe {
let mut v = Vec::with_capacity(cm.comments as usize);
for i in range(0, (*cm).comments) {
let len = *cm.comment_lengths.offset(i as isize);
match make_str(*cm.user_comments.offset(i as isize) as *const _,
len as usize) {
Some(s) => {
v.push(s);
}
None => {
}
}
}
v
}
})
}
pub fn decode<'a>(&'a mut self) -> OVResult<&'a mut [&'a mut [f32]]> {
let max_samples = 4096;
self.callback_setup();
let mut sample_buffer: *mut *mut f32 = unsafe {
mem::uninitialized()
};
let mut bitstream_idx: c_int = unsafe {
mem::uninitialized()
};
let n_samples = unsafe {
match ffi::ov_read_float(&mut self.decoder, &mut sample_buffer,
max_samples, &mut bitstream_idx) {
0 => {
return Err(OVError::EndOfStream);
}
x if x < 0 => {
return Err(OVError::from_native(x as c_int));
}
x => x
}
};
let n_channels = unsafe {
(*ffi::ov_info(&mut self.decoder, bitstream_idx)).channels
};
self.channels.truncate(0);
for i in range(0, n_channels) {
unsafe {
let channel_buffer = *sample_buffer.offset(i as isize);
let channel_slice = raw::Slice::<f32> {
data: channel_buffer as *const f32,
len: n_samples as usize
};
self.channels.push(channel_slice);
};
}
Ok(unsafe {
mem::transmute(self.channels.as_slice())
})
}
extern "C" fn read(buffer: *mut c_void, size: size_t, nmemb: size_t,
datasource: *mut c_void) -> size_t {
let vf: &mut VorbisFile<R> = unsafe { mem::transmute(datasource) };
let ptr = buffer as *mut u8;
for i in range(0, nmemb) {
let more = unsafe {
let bufp = ptr.offset(i as isize);
let buf = from_raw_mut_buf(&bufp, size as usize);
match vf.src.read_at_least(size as usize, buf) {
Ok(_) => true,
Err(_) => false
}
};
if !more {
return i;
}
}
return nmemb;
}
}
#[unsafe_destructor]
impl<R: Reader> Drop for VorbisFile<R> {
fn drop(&mut self) {
self.callback_setup();
unsafe {
ffi::ov_clear(&mut self.decoder);
}
}
}