Implement decode_simple_block
This commit is contained in:
parent
51156d9fde
commit
a00d43e124
4 changed files with 31 additions and 4 deletions
|
@ -4,4 +4,5 @@ version = "0.1.0"
|
||||||
authors = ["Tangent 128 <Tangent128@gmail.com>"]
|
authors = ["Tangent 128 <Tangent128@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
byteorder = "1"
|
||||||
futures = "^0.1.7"
|
futures = "^0.1.7"
|
||||||
|
|
|
@ -22,7 +22,7 @@ pub fn main() {
|
||||||
match element {
|
match element {
|
||||||
// suppress printing byte arrays
|
// suppress printing byte arrays
|
||||||
Tracks(slice) => println!("Tracks[{}]", slice.len()),
|
Tracks(slice) => println!("Tracks[{}]", slice.len()),
|
||||||
Cluster(slice) => println!("Cluster[{}]", slice.len()),
|
SimpleBlock{timecode, ..} => println!("SimpleBlock@{}", timecode),
|
||||||
other => println!("{:?}", other)
|
other => println!("{:?}", other)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
|
|
||||||
|
extern crate byteorder;
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
|
|
||||||
pub mod ebml;
|
pub mod ebml;
|
||||||
|
|
31
src/webm.rs
31
src/webm.rs
|
@ -1,3 +1,4 @@
|
||||||
|
use byteorder::{BigEndian, ByteOrder};
|
||||||
use ebml::*;
|
use ebml::*;
|
||||||
|
|
||||||
const SEGMENT_ID: u64 = 0x08538067;
|
const SEGMENT_ID: u64 = 0x08538067;
|
||||||
|
@ -21,7 +22,12 @@ pub enum WebmElement<'b> {
|
||||||
Tracks(&'b[u8]),
|
Tracks(&'b[u8]),
|
||||||
Cluster,
|
Cluster,
|
||||||
Timecode(u64),
|
Timecode(u64),
|
||||||
SimpleBlock(&'b[u8]),
|
SimpleBlock {
|
||||||
|
track: u64,
|
||||||
|
timecode: i16,
|
||||||
|
flags: u8,
|
||||||
|
data: &'b[u8]
|
||||||
|
},
|
||||||
Unknown(u64)
|
Unknown(u64)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,13 +53,32 @@ impl<'a> Schema<'a> for Webm {
|
||||||
CUES_ID => Ok(WebmElement::Cues),
|
CUES_ID => Ok(WebmElement::Cues),
|
||||||
TRACKS_ID => Ok(WebmElement::Tracks(bytes)),
|
TRACKS_ID => Ok(WebmElement::Tracks(bytes)),
|
||||||
CLUSTER_ID => Ok(WebmElement::Cluster),
|
CLUSTER_ID => Ok(WebmElement::Cluster),
|
||||||
TIMECODE_ID => Ok(WebmElement::Timecode(0)),
|
TIMECODE_ID => decode_uint(bytes).map(WebmElement::Timecode),
|
||||||
SIMPLE_BLOCK_ID => Ok(WebmElement::SimpleBlock(bytes)),
|
SIMPLE_BLOCK_ID => decode_simple_block(bytes),
|
||||||
_ => Ok(WebmElement::Unknown(element_id))
|
_ => Ok(WebmElement::Unknown(element_id))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn decode_simple_block(bytes: &[u8]) -> Result<WebmElement, Error> {
|
||||||
|
if let Ok(Some((Varint::Value(track), track_field_len))) = decode_varint(bytes) {
|
||||||
|
let header_len = track_field_len + 2 + 1;
|
||||||
|
if bytes.len() < header_len {
|
||||||
|
return Err(Error::CorruptPayload);
|
||||||
|
}
|
||||||
|
let timecode = BigEndian::read_i16(&bytes[track_field_len..]);
|
||||||
|
let flags = bytes[track_field_len + 2];
|
||||||
|
return Ok(WebmElement::SimpleBlock {
|
||||||
|
track: track,
|
||||||
|
timecode: timecode,
|
||||||
|
flags: flags,
|
||||||
|
data: &bytes[header_len..],
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
return Err(Error::CorruptPayload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tests::TEST_FILE;
|
use tests::TEST_FILE;
|
||||||
|
|
Loading…
Reference in a new issue