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
#![deny(unsafe_code)]
#![cfg_attr(test, deny(warnings))]
extern crate byteorder;
extern crate ogg;
macro_rules! try {
($expr:expr) => (match $expr {
$crate::std::result::Result::Ok(val) => val,
$crate::std::result::Result::Err(err) => {
panic!("Panic on Err turned on for debug reasons. Encountered Err: {:?}", err)
}
})
}
mod vorbis;
use std::io;
use ogg::{OggReadError, PacketReader};
pub use vorbis::Metadata as VorbisMetadata;
#[derive(Debug)]
pub enum OggFormat {
Vorbis(VorbisMetadata),
Opus,
Theora,
Speex,
}
#[derive(Debug)]
pub enum OggMetadataError {
UnrecognizedFormat,
ReadError(std::io::Error),
}
impl std::error::Error for OggMetadataError {
fn description(&self) -> &str {
use OggMetadataError::*;
match self {
&UnrecognizedFormat => "Unrecognized or invalid format",
&ReadError(_) => "I/O error",
}
}
fn cause(&self) -> Option<&std::error::Error> {
match self {
&OggMetadataError::ReadError(ref err) =>
Some(err as &std::error::Error),
_ => None
}
}
}
impl std::fmt::Display for OggMetadataError {
fn fmt(&self, fmt :&mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(fmt, "{}", std::error::Error::description(self))
}
}
impl From<std::io::Error> for OggMetadataError {
fn from(err :std::io::Error) -> OggMetadataError {
return OggMetadataError::ReadError(err);
}
}
impl From<OggReadError> for OggMetadataError {
fn from(err :OggReadError) -> OggMetadataError {
return match err {
OggReadError::ReadError(err) => OggMetadataError::ReadError(err),
_ => OggMetadataError::UnrecognizedFormat,
};
}
}
fn get_absgp_of_last_packet<'a, T :io::Read + io::Seek + 'a>(pck_rdr :&mut PacketReader<T>)
-> Result<u64, OggMetadataError> {
use std::io::SeekFrom;
let end_pos = try!(pck_rdr.seek_bytes(SeekFrom::End(0)));
let end_pos_to_seek = ::std::cmp::min(end_pos, 150 * 1024);
try!(pck_rdr.seek_bytes(SeekFrom::End(-(end_pos_to_seek as i64))));
let mut pck = try!(pck_rdr.read_packet());
while !pck.last_packet {
pck = try!(pck_rdr.read_packet());
}
return Ok(pck.absgp_page);
}
pub fn read_format<'a, T :io::Read + io::Seek + 'a>(rdr :&mut T)
-> Result<OggFormat, OggMetadataError> {
let mut pck_rdr = PacketReader::new(rdr);
let pck = try!(pck_rdr.read_packet());
let vorbis_magic = &[0x01, 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73];
let opus_magic = &[0x4f, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64];
let theora_magic = &[0x80, 0x74, 0x68, 0x65, 0x6f, 0x72, 0x61];
let speex_magic = &[0x53, 0x70, 0x65, 0x65, 0x78, 0x20, 0x20, 0x20];
if pck.data.len() < 1 {
try!(Err(OggMetadataError::UnrecognizedFormat));
}
use OggFormat::*;
let ret :OggFormat = match pck.data[0] {
0x01 if pck.data.starts_with(vorbis_magic) => {
let ident_hdr = try!(vorbis::read_header_ident(
&pck.data[vorbis_magic.len()..]));
let len = try!(get_absgp_of_last_packet(&mut pck_rdr));
Vorbis(VorbisMetadata {
channels : ident_hdr.channels,
sample_rate : ident_hdr.sample_rate,
length_in_samples : len,
})
},
0x4f if pck.data.starts_with(opus_magic) => Opus,
0x80 if pck.data.starts_with(theora_magic) => Theora,
0x53 if pck.data.starts_with(speex_magic) => Speex,
_ => try!(Err(OggMetadataError::UnrecognizedFormat)),
};
return Ok(ret);
}