1
0
Fork 0
mirror of https://github.com/alfg/mp4-rust.git synced 2024-12-22 20:16:27 +00:00

Add ReadBox trait (#7)

This commit is contained in:
Ian Jun 2020-07-25 11:57:46 +09:00 committed by GitHub
parent e637fb761e
commit 85a65ea596
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 411 additions and 379 deletions

View file

@ -1,6 +1,5 @@
use std::fmt;
use std::io::{BufReader, SeekFrom, Seek, Read};
use std::fs::File;
use byteorder::{BigEndian, ReadBytesExt};
use std::char::{decode_utf16, REPLACEMENT_CHARACTER};
use crate::{Error, read_box_header, BoxHeader, HEADER_SIZE};
@ -151,6 +150,10 @@ impl fmt::Display for FourCC {
}
}
pub trait ReadBox<T>: Sized {
fn read_box(_: T, offset: u64, size: u32) -> Result<Self>;
}
#[derive(Debug, Default)]
pub struct FtypBox {
pub major_brand: FourCC,
@ -329,9 +332,10 @@ pub struct StsdBox {
pub flags: u32,
}
pub fn parse_ftyp_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<FtypBox> {
let major = f.read_u32::<BigEndian>().unwrap();
let minor = f.read_u32::<BigEndian>().unwrap();
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for FtypBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let major = reader.read_u32::<BigEndian>().unwrap();
let minor = reader.read_u32::<BigEndian>().unwrap();
if size % 4 != 0 {
return Err(Error::InvalidData("invalid ftyp size"));
}
@ -339,7 +343,7 @@ pub fn parse_ftyp_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Resul
let mut brands = Vec::new();
for _ in 0..brand_count {
let b = f.read_u32::<BigEndian>().unwrap();
let b = reader.read_u32::<BigEndian>().unwrap();
brands.push(From::from(b));
}
@ -348,24 +352,26 @@ pub fn parse_ftyp_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Resul
minor_version: minor,
compatible_brands: brands,
})
}
}
pub(crate) fn parse_moov_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<MoovBox> {
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for MoovBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let mut moov = MoovBox::new();
let mut start = 0u64;
while start < size as u64 {
// Get box header.
let header = read_box_header(f, start).unwrap();
let header = read_box_header(reader, start).unwrap();
let BoxHeader{ name, size: s, offset: _ } = header;
match name {
BoxType::MvhdBox => {
moov.mvhd = parse_mvhd_box(f, 0, s as u32).unwrap();
moov.mvhd = MvhdBox::read_box(reader, 0, s as u32).unwrap();
}
BoxType::TrakBox => {
let trak = parse_trak_box(f, 0, s as u32).unwrap();
let trak = TrakBox::read_box(reader, 0, s as u32).unwrap();
moov.traks.push(trak);
}
BoxType::UdtaBox => {
@ -375,22 +381,24 @@ pub(crate) fn parse_moov_box(f: &mut BufReader<File>, _offset: u64, size: u32) -
}
}
Ok(moov)
}
}
fn parse_mvhd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<MvhdBox> {
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for MvhdBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let current = reader.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let version = f.read_u8().unwrap();
let flags_a = f.read_u8().unwrap();
let flags_b = f.read_u8().unwrap();
let flags_c = f.read_u8().unwrap();
let version = reader.read_u8().unwrap();
let flags_a = reader.read_u8().unwrap();
let flags_b = reader.read_u8().unwrap();
let flags_c = reader.read_u8().unwrap();
let flags = u32::from(flags_a) << 16 | u32::from(flags_b) << 8 | u32::from(flags_c);
let creation_time = f.read_u32::<BigEndian>().unwrap();
let modification_time = f.read_u32::<BigEndian>().unwrap();
let timescale = f.read_u32::<BigEndian>().unwrap();
let duration = f.read_u32::<BigEndian>().unwrap();
let rate = f.read_u32::<BigEndian>().unwrap();
skip(f, current, size);
let creation_time = reader.read_u32::<BigEndian>().unwrap();
let modification_time = reader.read_u32::<BigEndian>().unwrap();
let timescale = reader.read_u32::<BigEndian>().unwrap();
let duration = reader.read_u32::<BigEndian>().unwrap();
let rate = reader.read_u32::<BigEndian>().unwrap();
skip(reader, current, size);
Ok(MvhdBox{
version,
@ -401,73 +409,76 @@ fn parse_mvhd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<Mv
duration,
rate,
})
}
}
fn parse_trak_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<TrakBox> {
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for TrakBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let current = reader.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let mut trak = TrakBox::new();
let start = 0u64;
while start < size as u64 {
// Get box header.
let header = read_box_header(f, start).unwrap();
let header = read_box_header(reader, start).unwrap();
let BoxHeader{ name, size: s, offset: _ } = header;
match name {
BoxType::TkhdBox => {
let tkhd = parse_tkhd_box(f, 0, s as u32).unwrap();
let tkhd = TkhdBox::read_box(reader, 0, s as u32).unwrap();
trak.tkhd = Some(tkhd);
}
BoxType::EdtsBox => {
let edts = parse_edts_box(f, 0, s as u32).unwrap();
let edts = EdtsBox::read_box(reader, 0, s as u32).unwrap();
trak.edts = Some(edts);
}
BoxType::MdiaBox => {
let mdia = parse_mdia_box(f, 0, s as u32).unwrap();
let mdia = MdiaBox::read_box(reader, 0, s as u32).unwrap();
trak.mdia = Some(mdia);
}
_ => break
}
}
skip(f, current, size);
skip(reader, current, size);
Ok(trak)
}
}
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for TkhdBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let current = reader.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
fn parse_tkhd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<TkhdBox> {
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let version = f.read_u8().unwrap();
let flags_a = f.read_u8().unwrap();
let flags_b = f.read_u8().unwrap();
let flags_c = f.read_u8().unwrap();
let version = reader.read_u8().unwrap();
let flags_a = reader.read_u8().unwrap();
let flags_b = reader.read_u8().unwrap();
let flags_c = reader.read_u8().unwrap();
let flags = u32::from(flags_a) << 16 | u32::from(flags_b) << 8 | u32::from(flags_c);
let creation_time = f.read_u32::<BigEndian>().unwrap();
let modification_time = f.read_u32::<BigEndian>().unwrap();
let track_id = f.read_u32::<BigEndian>().unwrap();
let duration = f.read_u64::<BigEndian>().unwrap();
f.read_u64::<BigEndian>().unwrap(); // skip.
let layer = f.read_u16::<BigEndian>().unwrap();
let alternate_group = f.read_u16::<BigEndian>().unwrap();
let volume = f.read_u16::<BigEndian>().unwrap() >> 8;
let creation_time = reader.read_u32::<BigEndian>().unwrap();
let modification_time = reader.read_u32::<BigEndian>().unwrap();
let track_id = reader.read_u32::<BigEndian>().unwrap();
let duration = reader.read_u64::<BigEndian>().unwrap();
reader.read_u64::<BigEndian>().unwrap(); // skip.
let layer = reader.read_u16::<BigEndian>().unwrap();
let alternate_group = reader.read_u16::<BigEndian>().unwrap();
let volume = reader.read_u16::<BigEndian>().unwrap() >> 8;
f.read_u8().unwrap(); // skip.
reader.read_u8().unwrap(); // skip.
let matrix = Matrix{
a: f.read_i32::<byteorder::LittleEndian>().unwrap(),
b: f.read_i32::<BigEndian>().unwrap(),
u: f.read_i32::<BigEndian>().unwrap(),
c: f.read_i32::<BigEndian>().unwrap(),
d: f.read_i32::<BigEndian>().unwrap(),
v: f.read_i32::<BigEndian>().unwrap(),
x: f.read_i32::<BigEndian>().unwrap(),
y: f.read_i32::<BigEndian>().unwrap(),
w: f.read_i32::<BigEndian>().unwrap(),
a: reader.read_i32::<byteorder::LittleEndian>().unwrap(),
b: reader.read_i32::<BigEndian>().unwrap(),
u: reader.read_i32::<BigEndian>().unwrap(),
c: reader.read_i32::<BigEndian>().unwrap(),
d: reader.read_i32::<BigEndian>().unwrap(),
v: reader.read_i32::<BigEndian>().unwrap(),
x: reader.read_i32::<BigEndian>().unwrap(),
y: reader.read_i32::<BigEndian>().unwrap(),
w: reader.read_i32::<BigEndian>().unwrap(),
};
let width = f.read_u32::<BigEndian>().unwrap() >> 8;
let height = f.read_u32::<BigEndian>().unwrap() >> 8;
skip(f, current, size);
let width = reader.read_u32::<BigEndian>().unwrap() >> 8;
let height = reader.read_u32::<BigEndian>().unwrap() >> 8;
skip(reader, current, size);
Ok(TkhdBox {
version,
@ -483,103 +494,111 @@ fn parse_tkhd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<Tk
width,
height,
})
}
}
fn parse_edts_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<EdtsBox> {
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for EdtsBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let current = reader.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let mut edts = EdtsBox::new();
let start = 0u64;
while start < size as u64 {
// Get box header.
let header = read_box_header(f, start).unwrap();
let header = read_box_header(reader, start).unwrap();
let BoxHeader{ name, size: s, offset: _ } = header;
match name {
BoxType::ElstBox => {
let elst = parse_elst_box(f, 0, s as u32).unwrap();
let elst = ElstBox::read_box(reader, 0, s as u32).unwrap();
edts.elst = Some(elst);
}
_ => break
}
}
skip(f, current, size);
skip(reader, current, size);
Ok(edts)
}
}
fn parse_elst_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<ElstBox> {
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for ElstBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let current = reader.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let version = f.read_u32::<BigEndian>().unwrap();
let entry_count = f.read_u32::<BigEndian>().unwrap();
let version = reader.read_u32::<BigEndian>().unwrap();
let entry_count = reader.read_u32::<BigEndian>().unwrap();
let mut entries = Vec::new();
for _i in 0..entry_count {
let entry = ElstEntry{
segment_duration: f.read_u32::<BigEndian>().unwrap(),
media_time: f.read_u32::<BigEndian>().unwrap(),
media_rate: f.read_u16::<BigEndian>().unwrap(),
media_rate_fraction: f.read_u16::<BigEndian>().unwrap(),
segment_duration: reader.read_u32::<BigEndian>().unwrap(),
media_time: reader.read_u32::<BigEndian>().unwrap(),
media_rate: reader.read_u16::<BigEndian>().unwrap(),
media_rate_fraction: reader.read_u16::<BigEndian>().unwrap(),
};
entries.push(entry);
}
skip(f, current, size);
skip(reader, current, size);
Ok(ElstBox {
version,
entry_count,
entries,
})
}
}
fn parse_mdia_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<MdiaBox> {
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for MdiaBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let current = reader.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let mut mdia = MdiaBox::new();
let start = 0u64;
while start < size as u64 {
// Get box header.
let header = read_box_header(f, start).unwrap();
let header = read_box_header(reader, start).unwrap();
let BoxHeader{ name, size: s, offset: _ } = header;
match name {
BoxType::MdhdBox => {
let mdhd = parse_mdhd_box(f, 0, s as u32).unwrap();
let mdhd = MdhdBox::read_box(reader, 0, s as u32).unwrap();
mdia.mdhd = Some(mdhd);
}
BoxType::HdlrBox => {
let hdlr = parse_hdlr_box(f, 0, s as u32).unwrap();
let hdlr = HdlrBox::read_box(reader, 0, s as u32).unwrap();
mdia.hdlr = Some(hdlr);
}
BoxType::MinfBox => {
let minf = parse_minf_box(f, 0, s as u32).unwrap();
let minf = MinfBox::read_box(reader, 0, s as u32).unwrap();
mdia.minf = Some(minf);
}
_ => break
}
}
skip(f, current, size);
skip(reader, current, size);
Ok(mdia)
}
}
fn parse_mdhd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<MdhdBox> {
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for MdhdBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let current = reader.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let version = f.read_u8().unwrap();
let flags_a = f.read_u8().unwrap();
let flags_b = f.read_u8().unwrap();
let flags_c = f.read_u8().unwrap();
let version = reader.read_u8().unwrap();
let flags_a = reader.read_u8().unwrap();
let flags_b = reader.read_u8().unwrap();
let flags_c = reader.read_u8().unwrap();
let flags = u32::from(flags_a) << 16 | u32::from(flags_b) << 8 | u32::from(flags_c);
let creation_time = f.read_u32::<BigEndian>().unwrap();
let modification_time = f.read_u32::<BigEndian>().unwrap();
let timescale = f.read_u32::<BigEndian>().unwrap();
let duration = f.read_u32::<BigEndian>().unwrap();
let language = f.read_u16::<BigEndian>().unwrap();
let creation_time = reader.read_u32::<BigEndian>().unwrap();
let modification_time = reader.read_u32::<BigEndian>().unwrap();
let timescale = reader.read_u32::<BigEndian>().unwrap();
let duration = reader.read_u32::<BigEndian>().unwrap();
let language = reader.read_u16::<BigEndian>().unwrap();
let language_string = get_language_string(language);
skip(f, current, size);
skip(reader, current, size);
Ok(MdhdBox {
version,
@ -591,6 +610,7 @@ fn parse_mdhd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<Md
language,
language_string,
})
}
}
fn get_language_string(language: u16) -> String {
@ -608,27 +628,28 @@ fn get_language_string(language: u16) -> String {
return lang_str;
}
fn parse_hdlr_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<HdlrBox> {
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for HdlrBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let current = reader.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let version = f.read_u8().unwrap();
let flags_a = f.read_u8().unwrap();
let flags_b = f.read_u8().unwrap();
let flags_c = f.read_u8().unwrap();
let version = reader.read_u8().unwrap();
let flags_a = reader.read_u8().unwrap();
let flags_b = reader.read_u8().unwrap();
let flags_c = reader.read_u8().unwrap();
let flags = u32::from(flags_a) << 16 | u32::from(flags_b) << 8 | u32::from(flags_c);
f.read_u32::<BigEndian>().unwrap(); // skip.
let handler = f.read_u32::<BigEndian>().unwrap();
reader.read_u32::<BigEndian>().unwrap(); // skip.
let handler = reader.read_u32::<BigEndian>().unwrap();
let n = f.seek(SeekFrom::Current(12)).unwrap(); // 12 bytes reserved.
let n = reader.seek(SeekFrom::Current(12)).unwrap(); // 12 bytes reserved.
let buf_size = (size as u64 - (n - current)) - HEADER_SIZE as u64;
let mut buf = vec![0u8; buf_size as usize];
f.read_exact(&mut buf).unwrap();
reader.read_exact(&mut buf).unwrap();
let handler_string = match String::from_utf8(buf) {
Ok(t) => t,
_ => String::from("null"),
};
skip(f, current, size);
skip(reader, current, size);
Ok(HdlrBox {
version,
@ -636,21 +657,23 @@ fn parse_hdlr_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<Hd
handler_type: From::from(handler),
name: handler_string,
})
}
}
fn parse_minf_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<MinfBox> {
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for MinfBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let current = reader.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let mut minf = MinfBox::new();
let mut start = 0u64;
while start < size as u64 {
// Get box header.
let header = read_box_header(f, start).unwrap();
let header = read_box_header(reader, start).unwrap();
let BoxHeader{ name, size: s, offset: _ } = header;
match name {
BoxType::VmhdBox => {
let vmhd = parse_vmhd_box(f, 0, s as u32).unwrap();
let vmhd = VmhdBox::read_box(reader, 0, s as u32).unwrap();
minf.vmhd = Some(vmhd);
}
BoxType::SmhdBox => {
@ -660,28 +683,30 @@ fn parse_minf_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<Mi
start = (s as u32 - HEADER_SIZE) as u64;
}
BoxType::StblBox => {
let stbl = parse_stbl_box(f, 0, s as u32).unwrap();
let stbl = StblBox::read_box(reader, 0, s as u32).unwrap();
minf.stbl = Some(stbl);
}
_ => break
}
}
skip(f, current, size);
skip(reader, current, size);
Ok(minf)
}
}
fn parse_vmhd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<VmhdBox> {
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for VmhdBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let current = reader.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let version = f.read_u8().unwrap();
let flags_a = f.read_u8().unwrap();
let flags_b = f.read_u8().unwrap();
let flags_c = f.read_u8().unwrap();
let version = reader.read_u8().unwrap();
let flags_a = reader.read_u8().unwrap();
let flags_b = reader.read_u8().unwrap();
let flags_c = reader.read_u8().unwrap();
let flags = u32::from(flags_a) << 16 | u32::from(flags_b) << 8 | u32::from(flags_c);
let graphics_mode = f.read_u16::<BigEndian>().unwrap();
let op_color = f.read_u16::<BigEndian>().unwrap();
skip(f, current, size);
let graphics_mode = reader.read_u16::<BigEndian>().unwrap();
let op_color = reader.read_u16::<BigEndian>().unwrap();
skip(reader, current, size);
Ok(VmhdBox {
version,
@ -689,51 +714,55 @@ fn parse_vmhd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<Vm
graphics_mode,
op_color,
})
}
}
fn parse_stbl_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<StblBox> {
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for StblBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let mut stbl = StblBox::new();
let start = 0u64;
while start < size as u64 {
// Get box header.
let header = read_box_header(f, start).unwrap();
let header = read_box_header(reader, start).unwrap();
let BoxHeader{ name, size: s, offset: _ } = header;
match name {
BoxType::StsdBox => {
let stsd = parse_stsd_box(f, 0, s as u32).unwrap();
let stsd = StsdBox::read_box(reader, 0, s as u32).unwrap();
stbl.stsd = Some(stsd);
}
BoxType::SttsBox => {
let stts = parse_stts_box(f, 0, s as u32).unwrap();
let stts = SttsBox::read_box(reader, 0, s as u32).unwrap();
stbl.stts = Some(stts);
}
_ => break
}
}
Ok(stbl)
}
}
fn parse_stts_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<SttsBox> {
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for SttsBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let current = reader.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let version = f.read_u8().unwrap();
let flags_a = f.read_u8().unwrap();
let flags_b = f.read_u8().unwrap();
let flags_c = f.read_u8().unwrap();
let version = reader.read_u8().unwrap();
let flags_a = reader.read_u8().unwrap();
let flags_b = reader.read_u8().unwrap();
let flags_c = reader.read_u8().unwrap();
let flags = u32::from(flags_a) << 16 | u32::from(flags_b) << 8 | u32::from(flags_c);
let entry_count = f.read_u32::<BigEndian>().unwrap();
let entry_count = reader.read_u32::<BigEndian>().unwrap();
let mut sample_counts = Vec::new();
let mut sample_deltas = Vec::new();
for _i in 0..entry_count {
let sc = f.read_u32::<BigEndian>().unwrap();
let sd = f.read_u32::<BigEndian>().unwrap();
let sc = reader.read_u32::<BigEndian>().unwrap();
let sd = reader.read_u32::<BigEndian>().unwrap();
sample_counts.push(sc);
sample_deltas.push(sd);
}
skip(f, current, size);
skip(reader, current, size);
Ok(SttsBox {
version,
@ -742,22 +771,24 @@ fn parse_stts_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<St
sample_counts,
sample_deltas,
})
}
}
fn parse_stsd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<StsdBox> {
let current = f.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for StsdBox {
fn read_box(reader: &mut BufReader<R>, _offset: u64, size: u32) -> Result<Self> {
let current = reader.seek(SeekFrom::Current(0)).unwrap(); // Current cursor position.
let version = f.read_u8().unwrap();
let flags_a = f.read_u8().unwrap();
let flags_b = f.read_u8().unwrap();
let flags_c = f.read_u8().unwrap();
let version = reader.read_u8().unwrap();
let flags_a = reader.read_u8().unwrap();
let flags_b = reader.read_u8().unwrap();
let flags_c = reader.read_u8().unwrap();
let flags = u32::from(flags_a) << 16 | u32::from(flags_b) << 8 | u32::from(flags_c);
f.read_u32::<BigEndian>().unwrap(); // skip.
reader.read_u32::<BigEndian>().unwrap(); // skip.
let mut start = 0u64;
while start < size as u64 {
// Get box header.
let header = read_box_header(f, start).unwrap();
let header = read_box_header(reader, start).unwrap();
let BoxHeader{ name, size: s, offset: _ } = header;
match name {
@ -770,16 +801,17 @@ fn parse_stsd_box(f: &mut BufReader<File>, _offset: u64, size: u32) -> Result<St
_ => break
}
}
skip(f, current, size);
skip(reader, current, size);
Ok(StsdBox {
version,
flags,
})
}
}
fn skip(f: &mut BufReader<File>, current: u64, size: u32) {
let after = f.seek(SeekFrom::Current(0)).unwrap();
fn skip<R: Read + Seek>(reader: &mut BufReader<R>, current: u64, size: u32) {
let after = reader.seek(SeekFrom::Current(0)).unwrap();
let remaining_bytes = (size as u64 - (after - current)) as i64;
f.seek(SeekFrom::Current(remaining_bytes - HEADER_SIZE as i64)).unwrap();
reader.seek(SeekFrom::Current(remaining_bytes - HEADER_SIZE as i64)).unwrap();
}

View file

@ -67,7 +67,7 @@ fn read_boxes(f: File) -> Result<BMFF> {
// Match and parse the atom boxes.
match name {
BoxType::FtypBox => {
let ftyp = parse_ftyp_box(&mut reader, 0, size as u32).unwrap();
let ftyp = FtypBox::read_box(&mut reader, 0, size as u32).unwrap();
bmff.ftyp = ftyp;
}
BoxType::FreeBox => {
@ -77,7 +77,7 @@ fn read_boxes(f: File) -> Result<BMFF> {
start = (size as u32 - HEADER_SIZE) as u64;
}
BoxType::MoovBox => {
let moov = parse_moov_box(&mut reader, 0, size as u32).unwrap();
let moov = MoovBox::read_box(&mut reader, 0, size as u32).unwrap();
bmff.moov = Some(moov);
}
BoxType::MoofBox => {
@ -97,7 +97,7 @@ fn read_boxes(f: File) -> Result<BMFF> {
Ok(bmff)
}
fn read_box_header(reader: &mut BufReader<File>, start: u64) -> Result<BoxHeader> {
fn read_box_header<R: Read + Seek>(reader: &mut BufReader<R>, start: u64) -> Result<BoxHeader> {
// Seek to offset.
let _r = reader.seek(SeekFrom::Current(start as i64));