Skip to main content

vsd_mp4/boxes/
tfhd.rs

1use crate::{ParsedBox, Result};
2
3/// Track Fragment Header Box (tfhd) - provides default parameters for a track fragment.
4///
5/// The track fragment header specifies the track ID and can define overrides for sample parameters
6/// such as duration, size, and data offset.
7#[derive(Debug, Clone)]
8pub struct TfhdBox {
9    /// An integer that uniquely identifies this track over the entire lifetime of this presentation.
10    pub track_id: u32,
11    /// If specified via flags, this overrides the default sample duration in the Track Extends Box for this fragment.
12    pub default_sample_duration: Option<u32>,
13    /// If specified via flags, this overrides the default sample size in the Track Extends Box for this fragment.
14    pub default_sample_size: Option<u32>,
15    /// If specified via flags, this indicates the base data offset.
16    pub base_data_offset: Option<u64>,
17}
18
19impl TfhdBox {
20    /// Parses a `tfhd` box from a `ParsedBox`.
21    pub fn new(box_: &mut ParsedBox) -> Result<Self> {
22        let reader = &mut box_.reader;
23        let flags = box_.flags.unwrap();
24
25        let mut default_sample_duration = None;
26        let mut default_sample_size = None;
27        let mut base_data_offset = None;
28
29        let track_id = reader.read_u32()?;
30
31        // Skip "base_data_offset" if present.
32        if (flags & 0x000001) != 0 {
33            base_data_offset = Some(reader.read_u64()?);
34        }
35
36        // Skip "sample_description_index" if present.
37        if (flags & 0x000002) != 0 {
38            reader.skip(4)?;
39        }
40
41        // Read "default_sample_duration" if present.
42        if (flags & 0x000008) != 0 {
43            default_sample_duration = Some(reader.read_u32()?);
44        }
45
46        // Read "default_sample_size" if present.
47        if (flags & 0x000010) != 0 {
48            default_sample_size = Some(reader.read_u32()?);
49        }
50
51        Ok(Self {
52            track_id,
53            default_sample_duration,
54            default_sample_size,
55            base_data_offset,
56        })
57    }
58}