1
0
Fork 0
mirror of https://github.com/alfg/mp4-rust.git synced 2024-06-11 01:19:21 +00:00
mp4-rust/src/mp4box/tkhd.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

276 lines
8.2 KiB
Rust

use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use std::io::{Read, Seek, Write};
use crate::mp4box::*;
#[derive(Debug, Clone, PartialEq)]
pub struct TkhdBox {
pub version: u8,
pub flags: u32,
pub creation_time: u64,
pub modification_time: u64,
pub track_id: u32,
pub duration: u64,
pub layer: u16,
pub alternate_group: u16,
pub volume: FixedPointU8,
pub matrix: Matrix,
pub width: FixedPointU16,
pub height: FixedPointU16,
}
impl Default for TkhdBox {
fn default() -> Self {
TkhdBox {
version: 0,
flags: 0,
creation_time: 0,
modification_time: 0,
track_id: 0,
duration: 0,
layer: 0,
alternate_group: 0,
volume: FixedPointU8::new(1),
matrix: Matrix::default(),
width: FixedPointU16::new(0),
height: FixedPointU16::new(0),
}
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Matrix {
pub a: i32,
pub b: i32,
pub u: i32,
pub c: i32,
pub d: i32,
pub v: i32,
pub x: i32,
pub y: i32,
pub w: i32,
}
impl TkhdBox {
pub fn set_width(&mut self, width: u16) {
self.width = FixedPointU16::new(width);
}
pub fn set_height(&mut self, height: u16) {
self.height = FixedPointU16::new(height);
}
}
impl Mp4Box for TkhdBox {
fn box_type() -> BoxType {
BoxType::TkhdBox
}
fn box_size(&self) -> u64 {
let mut size = HEADER_SIZE + HEADER_EXT_SIZE;
if self.version == 1 {
size += 32;
} else {
assert_eq!(self.version, 0);
size += 20;
}
size += 60;
size
}
}
impl<R: Read + Seek> ReadBox<&mut R> for TkhdBox {
fn read_box(reader: &mut R, size: u64) -> Result<Self> {
let start = box_start(reader)?;
let (version, flags) = read_box_header_ext(reader)?;
let (creation_time, modification_time, track_id, _, duration) = if version == 1 {
(
reader.read_u64::<BigEndian>()?,
reader.read_u64::<BigEndian>()?,
reader.read_u32::<BigEndian>()?,
reader.read_u32::<BigEndian>()?,
reader.read_u64::<BigEndian>()?,
)
} else {
assert_eq!(version, 0);
(
reader.read_u32::<BigEndian>()? as u64,
reader.read_u32::<BigEndian>()? as u64,
reader.read_u32::<BigEndian>()?,
reader.read_u32::<BigEndian>()?,
reader.read_u32::<BigEndian>()? as u64,
)
};
reader.read_u64::<BigEndian>()?; // reserved
let layer = reader.read_u16::<BigEndian>()?;
let alternate_group = reader.read_u16::<BigEndian>()?;
let volume = FixedPointU8::new_raw(reader.read_u16::<BigEndian>()?);
reader.read_u16::<BigEndian>()?; // reserved
let matrix = Matrix {
a: reader.read_i32::<byteorder::LittleEndian>()?,
b: reader.read_i32::<BigEndian>()?,
u: reader.read_i32::<BigEndian>()?,
c: reader.read_i32::<BigEndian>()?,
d: reader.read_i32::<BigEndian>()?,
v: reader.read_i32::<BigEndian>()?,
x: reader.read_i32::<BigEndian>()?,
y: reader.read_i32::<BigEndian>()?,
w: reader.read_i32::<BigEndian>()?,
};
let width = FixedPointU16::new_raw(reader.read_u32::<BigEndian>()?);
let height = FixedPointU16::new_raw(reader.read_u32::<BigEndian>()?);
skip_read_to(reader, start + size)?;
Ok(TkhdBox {
version,
flags,
creation_time,
modification_time,
track_id,
duration,
layer,
alternate_group,
volume,
matrix,
width,
height,
})
}
}
impl<W: Write> WriteBox<&mut W> for TkhdBox {
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.version == 1 {
writer.write_u64::<BigEndian>(self.creation_time)?;
writer.write_u64::<BigEndian>(self.modification_time)?;
writer.write_u32::<BigEndian>(self.track_id)?;
writer.write_u32::<BigEndian>(0)?; // reserved
writer.write_u64::<BigEndian>(self.duration)?;
} else {
assert_eq!(self.version, 0);
writer.write_u32::<BigEndian>(self.creation_time as u32)?;
writer.write_u32::<BigEndian>(self.modification_time as u32)?;
writer.write_u32::<BigEndian>(self.track_id)?;
writer.write_u32::<BigEndian>(0)?; // reserved
writer.write_u32::<BigEndian>(self.duration as u32)?;
}
writer.write_u64::<BigEndian>(0)?; // reserved
writer.write_u16::<BigEndian>(self.layer)?;
writer.write_u16::<BigEndian>(self.alternate_group)?;
writer.write_u16::<BigEndian>(self.volume.raw_value())?;
writer.write_u16::<BigEndian>(0)?; // reserved
writer.write_i32::<byteorder::LittleEndian>(self.matrix.a)?;
writer.write_i32::<BigEndian>(self.matrix.b)?;
writer.write_i32::<BigEndian>(self.matrix.u)?;
writer.write_i32::<BigEndian>(self.matrix.c)?;
writer.write_i32::<BigEndian>(self.matrix.d)?;
writer.write_i32::<BigEndian>(self.matrix.v)?;
writer.write_i32::<BigEndian>(self.matrix.x)?;
writer.write_i32::<BigEndian>(self.matrix.y)?;
writer.write_i32::<BigEndian>(self.matrix.w)?;
writer.write_u32::<BigEndian>(self.width.raw_value())?;
writer.write_u32::<BigEndian>(self.height.raw_value())?;
Ok(size)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mp4box::BoxHeader;
use std::io::Cursor;
#[test]
fn test_tkhd32() {
let src_box = TkhdBox {
version: 0,
flags: 0,
creation_time: 100,
modification_time: 200,
track_id: 1,
duration: 634634,
layer: 0,
alternate_group: 0,
volume: FixedPointU8::new(1),
matrix: Matrix {
a: 0x00010000,
b: 0,
u: 0,
c: 0,
d: 0x00010000,
v: 0,
x: 0,
y: 0,
w: 0x40000000,
},
width: FixedPointU16::new(512),
height: FixedPointU16::new(288),
};
let mut buf = Vec::new();
src_box.write_box(&mut buf).unwrap();
assert_eq!(buf.len(), src_box.box_size() as usize);
let mut reader = Cursor::new(&buf);
let header = BoxHeader::read(&mut reader).unwrap();
assert_eq!(header.name, BoxType::TkhdBox);
assert_eq!(src_box.box_size(), header.size);
let dst_box = TkhdBox::read_box(&mut reader, header.size).unwrap();
assert_eq!(src_box, dst_box);
}
#[test]
fn test_tkhd64() {
let src_box = TkhdBox {
version: 1,
flags: 0,
creation_time: 100,
modification_time: 200,
track_id: 1,
duration: 634634,
layer: 0,
alternate_group: 0,
volume: FixedPointU8::new(1),
matrix: Matrix {
a: 0x00010000,
b: 0,
u: 0,
c: 0,
d: 0x00010000,
v: 0,
x: 0,
y: 0,
w: 0x40000000,
},
width: FixedPointU16::new(512),
height: FixedPointU16::new(288),
};
let mut buf = Vec::new();
src_box.write_box(&mut buf).unwrap();
assert_eq!(buf.len(), src_box.box_size() as usize);
let mut reader = Cursor::new(&buf);
let header = BoxHeader::read(&mut reader).unwrap();
assert_eq!(header.name, BoxType::TkhdBox);
assert_eq!(src_box.box_size(), header.size);
let dst_box = TkhdBox::read_box(&mut reader, header.size).unwrap();
assert_eq!(src_box, dst_box);
}
}