scuffle_mp4/boxes/types/
mvex.rs

1use std::io;
2
3use bytes::{Buf, Bytes};
4
5use super::mehd::Mehd;
6use super::trex::Trex;
7use crate::boxes::DynBox;
8use crate::boxes::header::BoxHeader;
9use crate::boxes::traits::BoxType;
10
11#[derive(Debug, Clone, PartialEq)]
12/// Movie Extends Box
13/// ISO/IEC 14496-12:2022(E) - 8.8.1
14pub struct Mvex {
15    pub header: BoxHeader,
16    pub trex: Vec<Trex>,
17    pub mehd: Option<Mehd>,
18    pub unknown: Vec<DynBox>,
19}
20
21impl Mvex {
22    pub fn new(trex: Vec<Trex>, mehd: Option<Mehd>) -> Self {
23        Self {
24            header: BoxHeader::new(Self::NAME),
25            trex,
26            mehd,
27            unknown: Vec::new(),
28        }
29    }
30}
31
32impl BoxType for Mvex {
33    const NAME: [u8; 4] = *b"mvex";
34
35    fn demux(header: BoxHeader, data: Bytes) -> io::Result<Self> {
36        let mut trex = Vec::new();
37        let mut mehd = None;
38        let mut unknown = Vec::new();
39
40        let mut data = io::Cursor::new(data);
41        while data.has_remaining() {
42            let dyn_box = DynBox::demux(&mut data)?;
43
44            match dyn_box {
45                DynBox::Trex(b) => {
46                    trex.push(*b);
47                }
48                DynBox::Mehd(b) => {
49                    mehd = Some(*b);
50                }
51                _ => {
52                    unknown.push(dyn_box);
53                }
54            }
55        }
56
57        Ok(Self {
58            header,
59            trex,
60            mehd,
61            unknown,
62        })
63    }
64
65    fn primitive_size(&self) -> u64 {
66        self.trex.iter().map(|b| b.size()).sum::<u64>()
67            + self.mehd.iter().map(|b| b.size()).sum::<u64>()
68            + self.unknown.iter().map(|b| b.size()).sum::<u64>()
69    }
70
71    fn primitive_mux<T: io::Write>(&self, writer: &mut T) -> io::Result<()> {
72        for b in &self.trex {
73            b.mux(writer)?;
74        }
75
76        if let Some(b) = &self.mehd {
77            b.mux(writer)?;
78        }
79
80        for b in &self.unknown {
81            b.mux(writer)?;
82        }
83
84        Ok(())
85    }
86}