Skip to main content

vsd_mp4/boxes/
mdhd.rs

1use crate::{ParsedBox, Result};
2
3/// Media Header Box (mdhd) - declares media-independent metadata and information.
4///
5/// The Media Header Box contains the overall information and media-independent metadata
6/// about the media within a track.
7#[derive(Debug, Clone)]
8pub struct MdhdBox {
9    /// The time-scale for this media. This is the number of time units that pass in one second.
10    pub timescale: u32,
11    /// The ISO 639-2/T 3-character language code for this media (e.g., "und", "eng").
12    pub language: String,
13}
14
15impl MdhdBox {
16    /// Parses a `mdhd` box from a `ParsedBox`.
17    ///
18    /// This method parses the creation time, modification time, timescale, duration,
19    /// and language from the box payload according to the box version (0 or 1).
20    pub fn new(box_: &mut ParsedBox) -> Result<Self> {
21        let reader = &mut box_.reader;
22        let version = box_.version.unwrap();
23
24        if version == 1 {
25            reader.skip(8)?;
26            reader.skip(8)?;
27        } else {
28            reader.skip(4)?;
29            reader.skip(4)?;
30        }
31
32        let timescale = reader.read_u32()?;
33
34        reader.skip(4)?;
35
36        let language = reader.read_u16()?;
37
38        let language_string = String::from_utf16(&[
39            (language >> 10) + 0x60,
40            ((language & 0x03c0) >> 5) + 0x60,
41            (language & 0x1f) + 0x60,
42        ])?;
43
44        Ok(Self {
45            timescale,
46            language: language_string,
47        })
48    }
49}