Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mp4"
version = "0.13.0"
version = "0.14.0"
authors = ["Alf <[email protected]>"]
edition = "2018"
description = "MP4 reader and writer library in Rust."
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ cargo add mp4
```
or add to your `Cargo.toml`:
```toml
mp4 = "0.13.0"
mp4 = "0.14.0"
```

#### Documentation
Expand Down
4 changes: 3 additions & 1 deletion examples/mp4dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ fn get_boxes(file: File) -> Result<Vec<Box>> {
if let Some(mehd) = &mvex.mehd {
boxes.push(build_box(mehd));
}
boxes.push(build_box(&mvex.trex));
for trex in mvex.trexs.iter() {
boxes.push(build_box(trex));
}
}

// trak.
Expand Down
55 changes: 31 additions & 24 deletions src/mp4box/avc1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,30 +101,37 @@ impl<R: Read + Seek> ReadBox<&mut R> for Avc1Box {
let depth = reader.read_u16::<BigEndian>()?;
reader.read_i16::<BigEndian>()?; // pre-defined

let header = BoxHeader::read(reader)?;
let BoxHeader { name, size: s } = header;
if s > size {
return Err(Error::InvalidData(
"avc1 box contains a box with a larger size than it",
));
}
if name == BoxType::AvcCBox {
let avcc = AvcCBox::read_box(reader, s)?;

skip_bytes_to(reader, start + size)?;

Ok(Avc1Box {
data_reference_index,
width,
height,
horizresolution,
vertresolution,
frame_count,
depth,
avcc,
})
} else {
Err(Error::InvalidData("avcc not found"))
let end = start + size;
loop {
let current = reader.stream_position()?;
if current >= end {
return Err(Error::InvalidData("avcc not found"));
}
let header = BoxHeader::read(reader)?;
let BoxHeader { name, size: s } = header;
if s > size {
return Err(Error::InvalidData(
"avc1 box contains a box with a larger size than it",
));
}
if name == BoxType::AvcCBox {
let avcc = AvcCBox::read_box(reader, s)?;

skip_bytes_to(reader, start + size)?;

return Ok(Avc1Box {
data_reference_index,
width,
height,
horizresolution,
vertresolution,
frame_count,
depth,
avcc,
});
} else {
skip_bytes_to(reader, current + s)?;
}
}
}
}
Expand Down
26 changes: 10 additions & 16 deletions src/mp4box/dinf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,22 +264,16 @@ impl<R: Read + Seek> ReadBox<&mut R> for UrlBox {

let (version, flags) = read_box_header_ext(reader)?;

let location = if size.saturating_sub(HEADER_SIZE + HEADER_EXT_SIZE) > 0 {
let buf_size = size - HEADER_SIZE - HEADER_EXT_SIZE - 1;
let mut buf = vec![0u8; buf_size as usize];
reader.read_exact(&mut buf)?;
match String::from_utf8(buf) {
Ok(t) => {
if t.len() != buf_size as usize {
return Err(Error::InvalidData("string too small"));
}
t
}
_ => String::default(),
}
} else {
String::default()
};
let buf_size = size
.checked_sub(HEADER_SIZE + HEADER_EXT_SIZE)
.ok_or(Error::InvalidData("url size too small"))?;

let mut buf = vec![0u8; buf_size as usize];
reader.read_exact(&mut buf)?;
if let Some(end) = buf.iter().position(|&b| b == b'\0') {
buf.truncate(end);
}
let location = String::from_utf8(buf).unwrap_or_default();

skip_bytes_to(reader, start + size)?;

Expand Down
65 changes: 54 additions & 11 deletions src/mp4box/hdlr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,15 @@ impl<R: Read + Seek> ReadBox<&mut R> for HdlrBox {
skip_bytes(reader, 12)?; // reserved

let buf_size = size
.checked_sub(HEADER_SIZE + HEADER_EXT_SIZE + 20 + 1)
.checked_sub(HEADER_SIZE + HEADER_EXT_SIZE + 20)
.ok_or(Error::InvalidData("hdlr size too small"))?;

let mut buf = vec![0u8; buf_size as usize];
reader.read_exact(&mut buf)?;

let handler_string = match String::from_utf8(buf) {
Ok(t) => {
if t.len() != buf_size as usize {
return Err(Error::InvalidData("string too small"));
}
t
}
_ => String::from("null"),
};
if let Some(end) = buf.iter().position(|&b| b == b'\0') {
buf.truncate(end);
}
let handler_string = String::from_utf8(buf).unwrap_or_default();

skip_bytes_to(reader, start + size)?;

Expand Down Expand Up @@ -127,4 +122,52 @@ mod tests {
let dst_box = HdlrBox::read_box(&mut reader, header.size).unwrap();
assert_eq!(src_box, dst_box);
}

#[test]
fn test_hdlr_empty() {
let src_box = HdlrBox {
version: 0,
flags: 0,
handler_type: str::parse::<FourCC>("vide").unwrap(),
name: String::new(),
};
let mut buf = Vec::new();
src_box.write_box(&mut buf).unwrap();
assert_eq!(buf.len(), src_box.box_size() as usize);

let mut reader = Cursor::new(&buf);
let header = BoxHeader::read(&mut reader).unwrap();
assert_eq!(header.name, BoxType::HdlrBox);
assert_eq!(src_box.box_size(), header.size);

let dst_box = HdlrBox::read_box(&mut reader, header.size).unwrap();
assert_eq!(src_box, dst_box);
}

#[test]
fn test_hdlr_extra() {
let real_src_box = HdlrBox {
version: 0,
flags: 0,
handler_type: str::parse::<FourCC>("vide").unwrap(),
name: String::from("Good"),
};
let src_box = HdlrBox {
version: 0,
flags: 0,
handler_type: str::parse::<FourCC>("vide").unwrap(),
name: String::from_utf8(b"Good\0Bad".to_vec()).unwrap(),
};
let mut buf = Vec::new();
src_box.write_box(&mut buf).unwrap();
assert_eq!(buf.len(), src_box.box_size() as usize);

let mut reader = Cursor::new(&buf);
let header = BoxHeader::read(&mut reader).unwrap();
assert_eq!(header.name, BoxType::HdlrBox);
assert_eq!(src_box.box_size(), header.size);

let dst_box = HdlrBox::read_box(&mut reader, header.size).unwrap();
assert_eq!(real_src_box, dst_box);
}
}
Loading