mirror of
https://github.com/alfg/mp4-rust.git
synced 2024-06-02 13:39:54 +00:00
Add smhd box
This commit is contained in:
parent
948dc016d9
commit
4ce23997ad
|
@ -1,12 +1,17 @@
|
|||
use std::io::{BufReader, SeekFrom, Seek, Read, BufWriter, Write};
|
||||
|
||||
use crate::*;
|
||||
use crate::atoms::{vmhd::VmhdBox, stbl::StblBox};
|
||||
use crate::atoms::{
|
||||
vmhd::VmhdBox,
|
||||
smhd::SmhdBox,
|
||||
stbl::StblBox
|
||||
};
|
||||
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct MinfBox {
|
||||
pub vmhd: Option<VmhdBox>,
|
||||
pub smhd: Option<SmhdBox>,
|
||||
pub stbl: Option<StblBox>,
|
||||
}
|
||||
|
||||
|
@ -26,6 +31,9 @@ impl Mp4Box for MinfBox {
|
|||
if let Some(vmhd) = &self.vmhd {
|
||||
size += vmhd.box_size();
|
||||
}
|
||||
if let Some(smhd) = &self.smhd {
|
||||
size += smhd.box_size();
|
||||
}
|
||||
if let Some(stbl) = &self.stbl {
|
||||
size += stbl.box_size();
|
||||
}
|
||||
|
@ -51,7 +59,10 @@ impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for MinfBox {
|
|||
let vmhd = VmhdBox::read_box(reader, s)?;
|
||||
minf.vmhd = Some(vmhd);
|
||||
}
|
||||
BoxType::SmhdBox => {}
|
||||
BoxType::SmhdBox => {
|
||||
let smhd = SmhdBox::read_box(reader, s)?;
|
||||
minf.smhd = Some(smhd);
|
||||
}
|
||||
BoxType::DinfBox => {}
|
||||
BoxType::StblBox => {
|
||||
let stbl = StblBox::read_box(reader, s)?;
|
||||
|
@ -77,6 +88,9 @@ impl<W: Write> WriteBox<&mut BufWriter<W>> for MinfBox {
|
|||
if let Some(vmhd) = &self.vmhd {
|
||||
vmhd.write_box(writer)?;
|
||||
}
|
||||
if let Some(smhd) = &self.smhd {
|
||||
smhd.write_box(writer)?;
|
||||
}
|
||||
if let Some(stbl) = &self.stbl {
|
||||
stbl.write_box(writer)?;
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ mod mdhd;
|
|||
mod hdlr;
|
||||
mod minf;
|
||||
mod vmhd;
|
||||
mod smhd;
|
||||
mod stbl;
|
||||
mod stsd;
|
||||
mod stts;
|
||||
|
|
100
src/atoms/smhd.rs
Normal file
100
src/atoms/smhd.rs
Normal file
|
@ -0,0 +1,100 @@
|
|||
use std::io::{BufReader, Seek, Read, BufWriter, Write};
|
||||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
use num_rational::Ratio;
|
||||
|
||||
use crate::*;
|
||||
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct SmhdBox {
|
||||
pub version: u8,
|
||||
pub flags: u32,
|
||||
pub balance: Ratio<i16>,
|
||||
}
|
||||
|
||||
impl Default for SmhdBox {
|
||||
fn default() -> Self {
|
||||
SmhdBox {
|
||||
version: 0,
|
||||
flags: 0,
|
||||
balance: Ratio::new_raw(0, 0x100),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mp4Box for SmhdBox {
|
||||
fn box_type() -> BoxType {
|
||||
BoxType::SmhdBox
|
||||
}
|
||||
|
||||
fn box_size(&self) -> u64 {
|
||||
HEADER_SIZE + HEADER_EXT_SIZE + 4
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for SmhdBox {
|
||||
fn read_box(reader: &mut BufReader<R>, size: u64) -> Result<Self> {
|
||||
let start = get_box_start(reader)?;
|
||||
|
||||
let (version, flags) = read_box_header_ext(reader)?;
|
||||
|
||||
let balance_numer = reader.read_i16::<BigEndian>()?;
|
||||
let balance = Ratio::new_raw(balance_numer, 0x100);
|
||||
|
||||
skip_read_to(reader, start + size)?;
|
||||
|
||||
Ok(SmhdBox {
|
||||
version,
|
||||
flags,
|
||||
balance,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Write> WriteBox<&mut BufWriter<W>> for SmhdBox {
|
||||
fn write_box(&self, writer: &mut BufWriter<W>) -> Result<u64> {
|
||||
let size = self.box_size();
|
||||
BoxHeader::new(Self::box_type(), size).write_box(writer)?;
|
||||
|
||||
write_box_header_ext(writer, self.version, self.flags)?;
|
||||
|
||||
writer.write_i16::<BigEndian>(*self.balance.numer())?;
|
||||
writer.write_u16::<BigEndian>(0)?; // reserved
|
||||
|
||||
Ok(size)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::read_box_header;
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
fn test_smhd() {
|
||||
let src_box = SmhdBox {
|
||||
version: 0,
|
||||
flags: 0,
|
||||
balance: Ratio::new_raw(-0x100, 0x100),
|
||||
};
|
||||
let mut buf = Vec::new();
|
||||
{
|
||||
let mut writer = BufWriter::new(&mut buf);
|
||||
src_box.write_box(&mut writer).unwrap();
|
||||
}
|
||||
assert_eq!(buf.len(), src_box.box_size() as usize);
|
||||
|
||||
{
|
||||
let mut reader = BufReader::new(Cursor::new(&buf));
|
||||
let header = read_box_header(&mut reader).unwrap();
|
||||
assert_eq!(header.name, BoxType::SmhdBox);
|
||||
assert_eq!(src_box.box_size(), header.size);
|
||||
|
||||
let dst_box = SmhdBox::read_box(&mut reader, header.size).unwrap();
|
||||
|
||||
assert_eq!(src_box, dst_box);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue