1
0
Fork 0
mirror of https://github.com/alfg/mp4-rust.git synced 2024-06-02 13:39:54 +00:00
mp4-rust/src/mp4box/mdia.rs
Ian Jun 3104a2d95b
Feature/mp4copy (#14)
* Add ReadBox trait

* Add boxtype macro

* Remove offset in BoxHeader

* Fix parsing error when box has largesize

* Remove duplicated codes reading version and flags

* Add avc1 box

* Add mp4a box

* Add mp4a box

* Add DecoderSpecificDescriptor in esds box

* Add necessary sub-boxes to stbl box

* Improve ReadBox::read_box()

* Add smhd box

* Refactor BoxHeader

* Refactor BMFF

* Refactor

* Add some functions to get offset and size of sample

* Add Mp4Reader::read_sample() that read media samples

* Move Mp4Reader to reader.rs

* Add mandatory check when reading boxes

Add some methods to Mp4Reader, TrackReader
Format codes

* Update mp4info

* Refactor common types

* Add FixedPointX types

* Add media configuration, profile, ...

* Add initial Mp4Writer

* Run cargo fmt

* Add Mp4Writer and examples/mp4copy

* Add test codes for Avc1Box and Mp4aBox

* Remove prefix "get_" from method names

* Rename atoms to mp4box

* Fix some bugs

Co-authored-by: Byungwan Jun <unipro.kr@gmail.com>
2020-08-04 16:56:59 -07:00

89 lines
2.4 KiB
Rust

use std::io::{Read, Seek, SeekFrom, Write};
use crate::mp4box::*;
use crate::mp4box::{hdlr::HdlrBox, mdhd::MdhdBox, minf::MinfBox};
#[derive(Debug, Clone, PartialEq, Default)]
pub struct MdiaBox {
pub mdhd: MdhdBox,
pub hdlr: HdlrBox,
pub minf: MinfBox,
}
impl Mp4Box for MdiaBox {
fn box_type() -> BoxType {
BoxType::MdiaBox
}
fn box_size(&self) -> u64 {
HEADER_SIZE + self.mdhd.box_size() + self.hdlr.box_size() + self.minf.box_size()
}
}
impl<R: Read + Seek> ReadBox<&mut R> for MdiaBox {
fn read_box(reader: &mut R, size: u64) -> Result<Self> {
let start = box_start(reader)?;
let mut mdhd = None;
let mut hdlr = None;
let mut minf = 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::MdhdBox => {
mdhd = Some(MdhdBox::read_box(reader, s)?);
}
BoxType::HdlrBox => {
hdlr = Some(HdlrBox::read_box(reader, s)?);
}
BoxType::MinfBox => {
minf = Some(MinfBox::read_box(reader, s)?);
}
_ => {
// XXX warn!()
skip_box(reader, s)?;
}
}
current = reader.seek(SeekFrom::Current(0))?;
}
if mdhd.is_none() {
return Err(Error::BoxNotFound(BoxType::MdhdBox));
}
if hdlr.is_none() {
return Err(Error::BoxNotFound(BoxType::HdlrBox));
}
if minf.is_none() {
return Err(Error::BoxNotFound(BoxType::MinfBox));
}
skip_read_to(reader, start + size)?;
Ok(MdiaBox {
mdhd: mdhd.unwrap(),
hdlr: hdlr.unwrap(),
minf: minf.unwrap(),
})
}
}
impl<W: Write> WriteBox<&mut W> for MdiaBox {
fn write_box(&self, writer: &mut W) -> Result<u64> {
let size = self.box_size();
BoxHeader::new(Self::box_type(), size).write(writer)?;
self.mdhd.write_box(writer)?;
self.hdlr.write_box(writer)?;
self.minf.write_box(writer)?;
Ok(size)
}
}