mp4_atom/
ftyp.rs

1use crate::*;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5pub struct Ftyp {
6    pub major_brand: FourCC,
7    pub minor_version: u32,
8    pub compatible_brands: Vec<FourCC>,
9}
10
11impl Atom for Ftyp {
12    const KIND: FourCC = FourCC::new(b"ftyp");
13
14    fn decode_body<B: Buf>(buf: &mut B) -> Result<Self> {
15        Ok(Ftyp {
16            major_brand: FourCC::decode(buf)?,
17            minor_version: u32::decode(buf)?,
18            compatible_brands: Vec::<FourCC>::decode(buf)?,
19        })
20    }
21
22    fn encode_body<B: BufMut>(&self, buf: &mut B) -> Result<()> {
23        self.major_brand.encode(buf)?;
24        self.minor_version.encode(buf)?;
25        self.compatible_brands.encode(buf)?;
26        Ok(())
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn test_ftyp() {
36        let decoded = Ftyp {
37            major_brand: b"isom".into(),
38            minor_version: 0,
39            compatible_brands: vec![
40                b"isom".into(),
41                b"isom".into(),
42                b"iso2".into(),
43                b"avc1".into(),
44                b"mp41".into(),
45            ],
46        };
47
48        let mut buf = Vec::new();
49        decoded.encode(&mut buf).expect("failed to encode");
50
51        let result = Ftyp::decode(&mut buf.as_slice()).expect("failed to decode");
52        assert_eq!(decoded, result);
53    }
54}