mirror of
https://github.com/alfg/mp4-rust.git
synced 2024-06-02 13:39:54 +00:00
Add dinf box
This commit is contained in:
parent
8c2d539ef7
commit
8cc76140b6
223
src/mp4box/dinf.rs
Normal file
223
src/mp4box/dinf.rs
Normal file
|
@ -0,0 +1,223 @@
|
|||
use std::io::{Read, Seek, Write};
|
||||
|
||||
use crate::mp4box::*;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct DinfBox {
|
||||
dref: DrefBox,
|
||||
}
|
||||
|
||||
impl Mp4Box for DinfBox {
|
||||
fn box_type() -> BoxType {
|
||||
BoxType::DinfBox
|
||||
}
|
||||
|
||||
fn box_size(&self) -> u64 {
|
||||
HEADER_SIZE + self.dref.box_size()
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Read + Seek> ReadBox<&mut R> for DinfBox {
|
||||
fn read_box(reader: &mut R, size: u64) -> Result<Self> {
|
||||
let start = box_start(reader)?;
|
||||
|
||||
let mut dref = None;
|
||||
|
||||
let mut current = reader.seek(SeekFrom::Current(0))?;
|
||||
let end = start + size;
|
||||
while current < end {
|
||||
// Get box header.
|
||||
let header = BoxHeader::read(reader)?;
|
||||
let BoxHeader { name, size: s } = header;
|
||||
|
||||
match name {
|
||||
BoxType::DrefBox => {
|
||||
dref = Some(DrefBox::read_box(reader, s)?);
|
||||
}
|
||||
_ => {
|
||||
// XXX warn!()
|
||||
skip_box(reader, s)?;
|
||||
}
|
||||
}
|
||||
|
||||
current = reader.seek(SeekFrom::Current(0))?;
|
||||
}
|
||||
|
||||
if dref.is_none() {
|
||||
return Err(Error::BoxNotFound(BoxType::DrefBox));
|
||||
}
|
||||
|
||||
skip_bytes_to(reader, start + size)?;
|
||||
|
||||
Ok(DinfBox {
|
||||
dref: dref.unwrap(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Write> WriteBox<&mut W> for DinfBox {
|
||||
fn write_box(&self, writer: &mut W) -> Result<u64> {
|
||||
let size = self.box_size();
|
||||
BoxHeader::new(Self::box_type(), size).write(writer)?;
|
||||
self.dref.write_box(writer)?;
|
||||
Ok(size)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct DrefBox {
|
||||
pub version: u8,
|
||||
pub flags: u32,
|
||||
pub url: UrlBox,
|
||||
}
|
||||
|
||||
impl Mp4Box for DrefBox {
|
||||
fn box_type() -> BoxType {
|
||||
BoxType::DrefBox
|
||||
}
|
||||
|
||||
fn box_size(&self) -> u64 {
|
||||
HEADER_SIZE + HEADER_EXT_SIZE + 4 + self.url.box_size()
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Read + Seek> ReadBox<&mut R> for DrefBox {
|
||||
fn read_box(reader: &mut R, size: u64) -> Result<Self> {
|
||||
let start = box_start(reader)?;
|
||||
|
||||
let mut current = reader.seek(SeekFrom::Current(0))?;
|
||||
|
||||
let (version, flags) = read_box_header_ext(reader)?;
|
||||
let end = start + size;
|
||||
|
||||
let mut url = None;
|
||||
|
||||
let entry_count = reader.read_u32::<BigEndian>()?;
|
||||
for _i in 0..entry_count {
|
||||
if current >= end {
|
||||
break;
|
||||
}
|
||||
|
||||
// Get box header.
|
||||
let header = BoxHeader::read(reader)?;
|
||||
let BoxHeader { name, size: s } = header;
|
||||
|
||||
match name {
|
||||
BoxType::UrlBox => {
|
||||
url = Some(UrlBox::read_box(reader, s)?);
|
||||
}
|
||||
_ => {
|
||||
skip_box(reader, s)?;
|
||||
}
|
||||
}
|
||||
|
||||
current = reader.seek(SeekFrom::Current(0))?;
|
||||
}
|
||||
|
||||
if url.is_none() {
|
||||
return Err(Error::BoxNotFound(BoxType::UrlBox));
|
||||
}
|
||||
|
||||
skip_bytes_to(reader, start + size)?;
|
||||
|
||||
Ok(DrefBox {
|
||||
version,
|
||||
flags,
|
||||
url: url.unwrap(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Write> WriteBox<&mut W> for DrefBox {
|
||||
fn write_box(&self, writer: &mut W) -> Result<u64> {
|
||||
let size = self.box_size();
|
||||
BoxHeader::new(Self::box_type(), size).write(writer)?;
|
||||
|
||||
write_box_header_ext(writer, self.version, self.flags)?;
|
||||
|
||||
writer.write_u32::<BigEndian>(1)?;
|
||||
self.url.write_box(writer)?;
|
||||
|
||||
Ok(size)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct UrlBox {
|
||||
pub version: u8,
|
||||
pub flags: u32,
|
||||
pub location: String,
|
||||
}
|
||||
|
||||
impl Default for UrlBox {
|
||||
fn default() -> Self {
|
||||
UrlBox {
|
||||
version: 0,
|
||||
flags: 1,
|
||||
location: String::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mp4Box for UrlBox {
|
||||
fn box_type() -> BoxType {
|
||||
BoxType::UrlBox
|
||||
}
|
||||
|
||||
fn box_size(&self) -> u64 {
|
||||
let mut size = HEADER_SIZE + HEADER_EXT_SIZE;
|
||||
|
||||
if ! self.location.is_empty() {
|
||||
size += self.location.bytes().len() as u64 + 1;
|
||||
}
|
||||
|
||||
size
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Read + Seek> ReadBox<&mut R> for UrlBox {
|
||||
fn read_box(reader: &mut R, size: u64) -> Result<Self> {
|
||||
let start = box_start(reader)?;
|
||||
|
||||
let (version, flags) = read_box_header_ext(reader)?;
|
||||
|
||||
let location = if size - 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) => {
|
||||
assert_eq!(t.len(), buf_size as usize);
|
||||
t
|
||||
}
|
||||
_ => String::default(),
|
||||
}
|
||||
} else {
|
||||
String::default()
|
||||
};
|
||||
|
||||
skip_bytes_to(reader, start + size)?;
|
||||
|
||||
Ok(UrlBox {
|
||||
version,
|
||||
flags,
|
||||
location,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Write> WriteBox<&mut W> for UrlBox {
|
||||
fn write_box(&self, writer: &mut W) -> Result<u64> {
|
||||
let size = self.box_size();
|
||||
BoxHeader::new(Self::box_type(), size).write(writer)?;
|
||||
|
||||
write_box_header_ext(writer, self.version, self.flags)?;
|
||||
|
||||
if ! self.location.is_empty() {
|
||||
writer.write(self.location.as_bytes())?;
|
||||
writer.write_u8(0)?;
|
||||
}
|
||||
|
||||
Ok(size)
|
||||
}
|
||||
}
|
|
@ -1,12 +1,13 @@
|
|||
use std::io::{Read, Seek, SeekFrom, Write};
|
||||
|
||||
use crate::mp4box::*;
|
||||
use crate::mp4box::{smhd::SmhdBox, stbl::StblBox, vmhd::VmhdBox};
|
||||
use crate::mp4box::{dinf::DinfBox, smhd::SmhdBox, stbl::StblBox, vmhd::VmhdBox};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct MinfBox {
|
||||
pub vmhd: Option<VmhdBox>,
|
||||
pub smhd: Option<SmhdBox>,
|
||||
pub dinf: DinfBox,
|
||||
pub stbl: StblBox,
|
||||
}
|
||||
|
||||
|
@ -23,6 +24,7 @@ impl Mp4Box for MinfBox {
|
|||
if let Some(ref smhd) = self.smhd {
|
||||
size += smhd.box_size();
|
||||
}
|
||||
size += self.dinf.box_size();
|
||||
size += self.stbl.box_size();
|
||||
size
|
||||
}
|
||||
|
@ -32,6 +34,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for MinfBox {
|
|||
fn read_box(reader: &mut R, size: u64) -> Result<Self> {
|
||||
let start = box_start(reader)?;
|
||||
|
||||
let mut dinf = None;
|
||||
let mut vmhd = None;
|
||||
let mut smhd = None;
|
||||
let mut stbl = None;
|
||||
|
@ -51,8 +54,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for MinfBox {
|
|||
smhd = Some(SmhdBox::read_box(reader, s)?);
|
||||
}
|
||||
BoxType::DinfBox => {
|
||||
// XXX warn!()
|
||||
skip_box(reader, s)?;
|
||||
dinf = Some(DinfBox::read_box(reader, s)?);
|
||||
}
|
||||
BoxType::StblBox => {
|
||||
stbl = Some(StblBox::read_box(reader, s)?);
|
||||
|
@ -66,6 +68,9 @@ impl<R: Read + Seek> ReadBox<&mut R> for MinfBox {
|
|||
current = reader.seek(SeekFrom::Current(0))?;
|
||||
}
|
||||
|
||||
if dinf.is_none() {
|
||||
return Err(Error::BoxNotFound(BoxType::DinfBox));
|
||||
}
|
||||
if stbl.is_none() {
|
||||
return Err(Error::BoxNotFound(BoxType::StblBox));
|
||||
}
|
||||
|
@ -75,6 +80,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for MinfBox {
|
|||
Ok(MinfBox {
|
||||
vmhd,
|
||||
smhd,
|
||||
dinf: dinf.unwrap(),
|
||||
stbl: stbl.unwrap(),
|
||||
})
|
||||
}
|
||||
|
@ -91,6 +97,7 @@ impl<W: Write> WriteBox<&mut W> for MinfBox {
|
|||
if let Some(ref smhd) = self.smhd {
|
||||
smhd.write_box(writer)?;
|
||||
}
|
||||
self.dinf.write_box(writer)?;
|
||||
self.stbl.write_box(writer)?;
|
||||
|
||||
Ok(size)
|
||||
|
|
|
@ -14,6 +14,7 @@ use crate::*;
|
|||
pub(crate) mod avc1;
|
||||
pub(crate) mod co64;
|
||||
pub(crate) mod ctts;
|
||||
pub(crate) mod dinf;
|
||||
pub(crate) mod edts;
|
||||
pub(crate) mod elst;
|
||||
pub(crate) mod ftyp;
|
||||
|
@ -98,6 +99,8 @@ boxtype! {
|
|||
TrakBox => 0x7472616b,
|
||||
UdtaBox => 0x75647461,
|
||||
DinfBox => 0x64696e66,
|
||||
DrefBox => 0x64726566,
|
||||
UrlBox => 0x75726C20,
|
||||
SmhdBox => 0x736d6864,
|
||||
Avc1Box => 0x61766331,
|
||||
AvcCBox => 0x61766343,
|
||||
|
|
Loading…
Reference in a new issue