mirror of
https://github.com/alfg/mp4-rust.git
synced 2024-06-11 09:29:21 +00:00
Add dinf box
This commit is contained in:
parent
098de52658
commit
c5a586ca7f
281
src/mp4box/dinf.rs
Normal file
281
src/mp4box/dinf.rs
Normal file
|
@ -0,0 +1,281 @@
|
|||
use std::io::{Read, Seek, Write};
|
||||
use serde::{Serialize};
|
||||
|
||||
use crate::mp4box::*;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default, Serialize)]
|
||||
pub struct DinfBox {
|
||||
dref: DrefBox,
|
||||
}
|
||||
|
||||
impl DinfBox {
|
||||
pub fn get_type(&self) -> BoxType {
|
||||
BoxType::DinfBox
|
||||
}
|
||||
|
||||
pub fn get_size(&self) -> u64 {
|
||||
HEADER_SIZE + self.dref.box_size()
|
||||
}
|
||||
}
|
||||
|
||||
impl Mp4Box for DinfBox {
|
||||
fn box_type(&self) -> BoxType {
|
||||
return self.get_type();
|
||||
}
|
||||
|
||||
fn box_size(&self) -> u64 {
|
||||
return self.get_size();
|
||||
}
|
||||
|
||||
fn to_json(&self) -> Result<String> {
|
||||
Ok(serde_json::to_string(&self).unwrap())
|
||||
}
|
||||
|
||||
fn summary(&self) -> Result<String> {
|
||||
let s = format!("");
|
||||
Ok(s)
|
||||
}
|
||||
}
|
||||
|
||||
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, Serialize)]
|
||||
pub struct DrefBox {
|
||||
pub version: u8,
|
||||
pub flags: u32,
|
||||
pub url: UrlBox,
|
||||
}
|
||||
|
||||
impl DrefBox {
|
||||
pub fn get_type(&self) -> BoxType {
|
||||
BoxType::DrefBox
|
||||
}
|
||||
|
||||
pub fn get_size(&self) -> u64 {
|
||||
HEADER_SIZE + HEADER_EXT_SIZE + 4 + self.url.box_size()
|
||||
}
|
||||
}
|
||||
|
||||
impl Mp4Box for DrefBox {
|
||||
fn box_type(&self) -> BoxType {
|
||||
return self.get_type();
|
||||
}
|
||||
|
||||
fn box_size(&self) -> u64 {
|
||||
return self.get_size();
|
||||
}
|
||||
|
||||
fn to_json(&self) -> Result<String> {
|
||||
Ok(serde_json::to_string(&self).unwrap())
|
||||
}
|
||||
|
||||
fn summary(&self) -> Result<String> {
|
||||
let s = format!("");
|
||||
Ok(s)
|
||||
}
|
||||
}
|
||||
|
||||
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, Serialize)]
|
||||
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 UrlBox {
|
||||
pub fn get_type(&self) -> BoxType {
|
||||
BoxType::UrlBox
|
||||
}
|
||||
|
||||
pub fn get_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 Mp4Box for UrlBox {
|
||||
fn box_type(&self) -> BoxType {
|
||||
return self.get_type();
|
||||
}
|
||||
|
||||
fn box_size(&self) -> u64 {
|
||||
return self.get_size();
|
||||
}
|
||||
|
||||
fn to_json(&self) -> Result<String> {
|
||||
Ok(serde_json::to_string(&self).unwrap())
|
||||
}
|
||||
|
||||
fn summary(&self) -> Result<String> {
|
||||
let s = format!("location={}", self.location);
|
||||
Ok(s)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@ use std::io::{Read, Seek, SeekFrom, Write};
|
|||
use serde::{Serialize};
|
||||
|
||||
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, Serialize)]
|
||||
pub struct MinfBox {
|
||||
|
@ -12,6 +12,7 @@ pub struct MinfBox {
|
|||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub smhd: Option<SmhdBox>,
|
||||
|
||||
pub dinf: DinfBox,
|
||||
pub stbl: StblBox,
|
||||
}
|
||||
|
||||
|
@ -28,6 +29,7 @@ impl MinfBox {
|
|||
if let Some(ref smhd) = self.smhd {
|
||||
size += smhd.box_size();
|
||||
}
|
||||
size += self.dinf.box_size();
|
||||
size += self.stbl.box_size();
|
||||
size
|
||||
}
|
||||
|
@ -58,6 +60,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for MinfBox {
|
|||
|
||||
let mut vmhd = None;
|
||||
let mut smhd = None;
|
||||
let mut dinf = None;
|
||||
let mut stbl = None;
|
||||
|
||||
let mut current = reader.seek(SeekFrom::Current(0))?;
|
||||
|
@ -75,8 +78,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)?);
|
||||
|
@ -90,6 +92,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));
|
||||
}
|
||||
|
@ -99,6 +104,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for MinfBox {
|
|||
Ok(MinfBox {
|
||||
vmhd,
|
||||
smhd,
|
||||
dinf: dinf.unwrap(),
|
||||
stbl: stbl.unwrap(),
|
||||
})
|
||||
}
|
||||
|
@ -115,6 +121,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)
|
||||
|
|
|
@ -18,21 +18,23 @@
|
|||
//! mdhd
|
||||
//! hdlr
|
||||
//! minf
|
||||
//! stbl
|
||||
//! stsd
|
||||
//! avc1
|
||||
//! hev1
|
||||
//! mp4a
|
||||
//! tx3g
|
||||
//! stts
|
||||
//! stsc
|
||||
//! stsz
|
||||
//! stss
|
||||
//! stco
|
||||
//! co64
|
||||
//! ctts
|
||||
//! smhd
|
||||
//! vmhd
|
||||
//! stbl
|
||||
//! stsd
|
||||
//! avc1
|
||||
//! hev1
|
||||
//! mp4a
|
||||
//! tx3g
|
||||
//! stts
|
||||
//! stsc
|
||||
//! stsz
|
||||
//! stss
|
||||
//! stco
|
||||
//! co64
|
||||
//! ctts
|
||||
//! dinf
|
||||
//! dref
|
||||
//! smhd
|
||||
//! vmhd
|
||||
//! edts
|
||||
//! elst
|
||||
//! mvex
|
||||
|
@ -56,6 +58,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;
|
||||
|
@ -158,6 +161,8 @@ boxtype! {
|
|||
TrunBox => 0x7472756E,
|
||||
UdtaBox => 0x75647461,
|
||||
DinfBox => 0x64696e66,
|
||||
DrefBox => 0x64726566,
|
||||
UrlBox => 0x75726C20,
|
||||
SmhdBox => 0x736d6864,
|
||||
Avc1Box => 0x61766331,
|
||||
AvcCBox => 0x61766343,
|
||||
|
|
Loading…
Reference in a new issue