Skip to main content

mp4_edit/atom/container/
minf.rs

1use crate::{
2    atom::{
3        atom_ref::{AtomRef, AtomRefMut},
4        smhd::SMHD,
5        GmhdAtomRef, GmhdAtomRefMut, StblAtomRef, StblAtomRefMut, DINF, GMHD, STBL,
6    },
7    Atom, FourCC,
8};
9
10pub const MINF: FourCC = FourCC::new(b"minf");
11
12#[derive(Debug, Clone, Copy)]
13pub struct MinfAtomRef<'a>(pub(crate) AtomRef<'a>);
14
15impl<'a> MinfAtomRef<'a> {
16    pub fn children(&self) -> impl Iterator<Item = &'a Atom> {
17        self.0.children()
18    }
19
20    pub fn header(&self) -> GmhdAtomRef<'a> {
21        let atom = self.0.find_child(GMHD);
22        GmhdAtomRef(AtomRef(atom))
23    }
24
25    /// Finds the STBL atom
26    pub fn sample_table(&self) -> StblAtomRef<'a> {
27        let atom = self.0.find_child(STBL);
28        StblAtomRef(AtomRef(atom))
29    }
30}
31
32#[derive(Debug)]
33pub struct MinfAtomRefMut<'a>(pub(crate) AtomRefMut<'a>);
34
35impl<'a> MinfAtomRefMut<'a> {
36    pub fn as_ref(&self) -> MinfAtomRef<'_> {
37        MinfAtomRef(self.0.as_ref())
38    }
39
40    pub fn into_ref(self) -> MinfAtomRef<'a> {
41        MinfAtomRef(self.0.into_ref())
42    }
43
44    pub fn into_children(self) -> impl Iterator<Item = &'a mut Atom> {
45        self.0.into_children()
46    }
47
48    /// Finds or inserts the GMHD atom
49    pub fn header(&mut self) -> GmhdAtomRefMut<'_> {
50        GmhdAtomRefMut(self.0.find_or_insert_child(GMHD).insert_index(0).call())
51    }
52
53    /// Finds or inserts the STBL atom
54    pub fn sample_table(&mut self) -> StblAtomRefMut<'_> {
55        StblAtomRefMut(
56            self.0
57                .find_or_insert_child(STBL)
58                .insert_after(vec![DINF, SMHD])
59                .call(),
60        )
61    }
62
63    /// Finds the STBL atom
64    pub fn into_sample_table(self) -> Option<StblAtomRefMut<'a>> {
65        let atom = self.0.into_child(STBL)?;
66        Some(StblAtomRefMut(AtomRefMut(atom)))
67    }
68}