crates/format/video/src/mp4.rs 4.4 KiB 113 lines Raw
1 //! The boxes of an mp4 that carry a clip's header.2 //!3 //! A client that only reads the header has to parse nothing else. The writer4 //! emits one `ftyp` and one `moov` with one `mvhd`; a codec fills the tracks.5 6 use crate::VideoError;7 use crate::clip::Clip;8 9 /// The length field and the type field that every box starts with.10 const BOX_HEADER_LEN: usize = 8;11 12 /// The brand that the empty mp4 writes first.13 const BRAND: &[u8; 4] = b"isom";14 15 /// The compatible brands of the empty mp4. `mp41` is what a simple player16 /// accepts when it has no track to read.17 const COMPATIBLE: [&[u8; 4]; 2] = [b"isom", b"mp41"];18 19 /// The box types that this module writes.20 const FTYP: &[u8; 4] = b"ftyp";21 const MOOV: &[u8; 4] = b"moov";22 const MVHD: &[u8; 4] = b"mvhd";23 24 /// The version of `mvhd` that carries 64-bit times. A clip can be longer than25 /// the 32-bit seconds field of version 0.26 const MVHD_VERSION: u8 = 1;27 28 /// A full box has a version byte and three flag bytes before its payload.29 const FULL_BOX_HEADER_LEN: usize = 4;30 31 /// The minor version of an `ftyp`. This writer has no minor brands.32 const FTYP_MINOR: u32 = 0;33 34 /// 1.0 in 16.16 fixed point, the rate of a normal track.35 const RATE_ONE: u32 = 1 << 16;36 37 /// 1.0 in 8.8 fixed point, the volume of a normal track.38 const VOLUME_ONE: u16 = 1 << 8;39 40 /// The identity matrix in 16.16, as `mvhd` writes it.41 const MATRIX: [u32; 9] = [0x0001_0000, 0, 0, 0, 0x0001_0000, 0, 0, 0, 0x4000_0000];42 43 /// The `pre_defined` array and the next track id together. Both stay zero: the44 /// header has no track to point at.45 const RESERVED_WORDS: usize = 7;46 47 /// One ISO BMFF box: a big-endian length that includes the header, a four48 /// character type, and the payload.49 pub fn box_of(kind: &[u8; 4], payload: &[u8]) -> Vec<u8> {50     let len = payload.len() + BOX_HEADER_LEN;51     // The caller owns the payload. An mp4 box cannot carry more than u32 bytes,52     // so a payload beyond that is already outside the container.53     let len = u32::try_from(len).unwrap_or(u32::MAX);54     let mut out = Vec::with_capacity(BOX_HEADER_LEN + payload.len());55     out.extend_from_slice(&len.to_be_bytes());56     out.extend_from_slice(kind);57     out.extend_from_slice(payload);58     out59 }60 61 /// The `ftyp` box for an mp4 with one brand and one compatible brand.62 pub fn ftyp(major: &[u8; 4], compatible: &[&[u8; 4]]) -> Vec<u8> {63     let mut payload = Vec::with_capacity(2 * major.len() + compatible.len() * major.len());64     payload.extend_from_slice(major);65     payload.extend_from_slice(&FTYP_MINOR.to_be_bytes());66     for brand in compatible {67         payload.extend_from_slice(*brand);68     }69     box_of(FTYP, &payload)70 }71 72 /// A whole mp4 with no track: `ftyp` then `moov` with an empty `mvhd` at the73 /// timebase of `clip`. Enough for a client to read the header and the duration.74 pub fn empty(clip: &Clip) -> Result<Vec<u8>, VideoError> {75     let mvhd = box_of(MVHD, &mvhd(clip)?);76     let moov = box_of(MOOV, &mvhd);77     let mut out = ftyp(BRAND, &COMPATIBLE);78     out.extend_from_slice(&moov);79     Ok(out)80 }81 82 /// The payload of the `mvhd` that [`empty`] wraps.83 fn mvhd(clip: &Clip) -> Result<Vec<u8>, VideoError> {84     if clip.fps.num == 0 {85         return Err(VideoError::Division);86     }87     let timescale = u32::try_from(clip.fps.num).map_err(|_| VideoError::TooLarge)?;88     // `mvhd.duration` is in `timescale` units, and the timescale is the rate's89     // numerator. One frame is `den` ticks, so the whole clip is `frames * den`90     // ticks; dividing by the numerator would report a fraction of one frame.91     let duration = u128::from(clip.frames).saturating_mul(u128::from(clip.fps.den));92     let duration = u64::try_from(duration).map_err(|_| VideoError::TooLarge)?;93 94     let mut out = Vec::new();95     out.push(MVHD_VERSION);96     out.extend_from_slice(&[0; FULL_BOX_HEADER_LEN - 1]);97     out.extend_from_slice(&0u64.to_be_bytes());98     out.extend_from_slice(&0u64.to_be_bytes());99     out.extend_from_slice(&timescale.to_be_bytes());100     out.extend_from_slice(&duration.to_be_bytes());101     out.extend_from_slice(&RATE_ONE.to_be_bytes());102     out.extend_from_slice(&VOLUME_ONE.to_be_bytes());103     out.extend_from_slice(&0u16.to_be_bytes());104     out.extend_from_slice(&0u32.to_be_bytes());105     out.extend_from_slice(&0u32.to_be_bytes());106     for entry in MATRIX {107         out.extend_from_slice(&entry.to_be_bytes());108     }109     for _ in 0..RESERVED_WORDS {110         out.extend_from_slice(&0u32.to_be_bytes());111     }112     Ok(out)113 }