mirror of
https://github.com/alfg/mp4-rust.git
synced 2025-04-24 04:34:11 +00:00
Merge af3b69e6a4
into 35560e94f5
This commit is contained in:
commit
c896f64a68
4 changed files with 94 additions and 3 deletions
|
@ -46,6 +46,7 @@
|
|||
//! mehd
|
||||
//! trex
|
||||
//! emsg
|
||||
//! uuid
|
||||
//! moof
|
||||
//! mfhd
|
||||
//! traf
|
||||
|
@ -102,6 +103,7 @@ pub(crate) mod trex;
|
|||
pub(crate) mod trun;
|
||||
pub(crate) mod tx3g;
|
||||
pub(crate) mod udta;
|
||||
pub(crate) mod uuid;
|
||||
pub(crate) mod vmhd;
|
||||
pub(crate) mod vp09;
|
||||
pub(crate) mod vpcc;
|
||||
|
@ -146,6 +148,7 @@ pub use trex::TrexBox;
|
|||
pub use trun::TrunBox;
|
||||
pub use tx3g::Tx3gBox;
|
||||
pub use udta::UdtaBox;
|
||||
pub use uuid::UuidBox;
|
||||
pub use vmhd::VmhdBox;
|
||||
pub use vp09::Vp09Box;
|
||||
pub use vpcc::VpccBox;
|
||||
|
@ -238,7 +241,8 @@ boxtype! {
|
|||
CovrBox => 0x636f7672,
|
||||
DescBox => 0x64657363,
|
||||
WideBox => 0x77696465,
|
||||
WaveBox => 0x77617665
|
||||
WaveBox => 0x77617665,
|
||||
UuidBox => 0x75756964
|
||||
}
|
||||
|
||||
pub trait Mp4Box: Sized {
|
||||
|
|
74
src/mp4box/uuid.rs
Normal file
74
src/mp4box/uuid.rs
Normal file
|
@ -0,0 +1,74 @@
|
|||
use serde::Serialize;
|
||||
use std::io::{Read, Seek, Write};
|
||||
|
||||
use crate::mp4box::*;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
|
||||
pub struct UuidBox {
|
||||
pub extended_type: [u8; 16],
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl UuidBox {
|
||||
pub fn get_type(&self) -> BoxType {
|
||||
BoxType::UuidBox
|
||||
}
|
||||
|
||||
pub fn get_size(&self) -> u64 {
|
||||
HEADER_SIZE + 16 + self.data.len() as u64
|
||||
}
|
||||
}
|
||||
|
||||
impl Mp4Box for UuidBox {
|
||||
fn box_type(&self) -> BoxType {
|
||||
self.get_type()
|
||||
}
|
||||
|
||||
fn box_size(&self) -> u64 {
|
||||
self.get_size()
|
||||
}
|
||||
|
||||
fn to_json(&self) -> Result<String> {
|
||||
Ok(serde_json::to_string(&self).unwrap())
|
||||
}
|
||||
|
||||
fn summary(&self) -> Result<String> {
|
||||
let s = format!("extended_type: {:02x?}", self.extended_type);
|
||||
Ok(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Read + Seek> ReadBox<&mut R> for UuidBox {
|
||||
fn read_box(reader: &mut R, size: u64) -> Result<Self> {
|
||||
let start = box_start(reader)?;
|
||||
|
||||
let mut extended_type = [0; 16];
|
||||
reader.read_exact(&mut extended_type)?;
|
||||
|
||||
let data_size = (start + size)
|
||||
.checked_sub(reader.stream_position()?)
|
||||
.ok_or(Error::InvalidData("uuid size too small"))?;
|
||||
let mut data = vec![0; data_size as usize];
|
||||
reader.read_exact(&mut data)?;
|
||||
|
||||
skip_bytes_to(reader, start + size)?;
|
||||
|
||||
Ok(UuidBox {
|
||||
extended_type,
|
||||
data,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Write> WriteBox<&mut W> for UuidBox {
|
||||
fn write_box(&self, writer: &mut W) -> Result<u64> {
|
||||
let size = self.box_size();
|
||||
|
||||
BoxHeader::new(self.box_type(), size).write(writer)?;
|
||||
|
||||
writer.write_all(&self.extended_type)?;
|
||||
writer.write_all(&self.data)?;
|
||||
|
||||
Ok(size)
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@ pub struct Mp4Reader<R> {
|
|||
pub moov: MoovBox,
|
||||
pub moofs: Vec<MoofBox>,
|
||||
pub emsgs: Vec<EmsgBox>,
|
||||
pub uuids: Vec<UuidBox>,
|
||||
|
||||
tracks: HashMap<u32, Mp4Track>,
|
||||
size: u64,
|
||||
|
@ -26,6 +27,7 @@ impl<R: Read + Seek> Mp4Reader<R> {
|
|||
let mut moofs = Vec::new();
|
||||
let mut moof_offsets = Vec::new();
|
||||
let mut emsgs = Vec::new();
|
||||
let mut uuids = Vec::new();
|
||||
|
||||
let mut current = start;
|
||||
while current < size {
|
||||
|
@ -67,6 +69,10 @@ impl<R: Read + Seek> Mp4Reader<R> {
|
|||
let emsg = EmsgBox::read_box(&mut reader, s)?;
|
||||
emsgs.push(emsg);
|
||||
}
|
||||
BoxType::UuidBox => {
|
||||
let uuid = UuidBox::read_box(&mut reader, s)?;
|
||||
uuids.push(uuid);
|
||||
}
|
||||
_ => {
|
||||
// XXX warn!()
|
||||
skip_box(&mut reader, s)?;
|
||||
|
@ -124,6 +130,7 @@ impl<R: Read + Seek> Mp4Reader<R> {
|
|||
moov: moov.unwrap(),
|
||||
moofs,
|
||||
emsgs,
|
||||
uuids,
|
||||
size,
|
||||
tracks,
|
||||
})
|
||||
|
@ -138,6 +145,7 @@ impl<R: Read + Seek> Mp4Reader<R> {
|
|||
|
||||
let mut moofs = Vec::new();
|
||||
let mut moof_offsets = Vec::new();
|
||||
let mut uuids = Vec::new();
|
||||
|
||||
let mut current = start;
|
||||
while current < size {
|
||||
|
@ -166,6 +174,10 @@ impl<R: Read + Seek> Mp4Reader<R> {
|
|||
moofs.push(moof);
|
||||
moof_offsets.push(moof_offset);
|
||||
}
|
||||
BoxType::UuidBox => {
|
||||
let uuid = UuidBox::read_box(&mut reader, s)?;
|
||||
uuids.push(uuid);
|
||||
}
|
||||
_ => {
|
||||
// XXX warn!()
|
||||
skip_box(&mut reader, s)?;
|
||||
|
@ -210,6 +222,7 @@ impl<R: Read + Seek> Mp4Reader<R> {
|
|||
moov: self.moov.clone(),
|
||||
moofs,
|
||||
emsgs: Vec::new(),
|
||||
uuids,
|
||||
tracks,
|
||||
size,
|
||||
})
|
||||
|
|
|
@ -261,7 +261,7 @@ impl Mp4Track {
|
|||
|
||||
pub fn sequence_parameter_set(&self) -> Result<&[u8]> {
|
||||
if let Some(ref avc1) = self.trak.mdia.minf.stbl.stsd.avc1 {
|
||||
match avc1.avcc.sequence_parameter_sets.get(0) {
|
||||
match avc1.avcc.sequence_parameter_sets.first() {
|
||||
Some(nal) => Ok(nal.bytes.as_ref()),
|
||||
None => Err(Error::EntryInStblNotFound(
|
||||
self.track_id(),
|
||||
|
@ -276,7 +276,7 @@ impl Mp4Track {
|
|||
|
||||
pub fn picture_parameter_set(&self) -> Result<&[u8]> {
|
||||
if let Some(ref avc1) = self.trak.mdia.minf.stbl.stsd.avc1 {
|
||||
match avc1.avcc.picture_parameter_sets.get(0) {
|
||||
match avc1.avcc.picture_parameter_sets.first() {
|
||||
Some(nal) => Ok(nal.bytes.as_ref()),
|
||||
None => Err(Error::EntryInStblNotFound(
|
||||
self.track_id(),
|
||||
|
|
Loading…
Reference in a new issue