aboutsummaryrefslogtreecommitdiff
path: root/remuxer/mp4/src/lib.rs
blob: 2098e1b0868b0ba0a13810bd250809e15ef2dbb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/

mod boxes;
pub use boxes::*;

pub struct BoxW<'a>(&'a mut Vec<u8>);
impl<'a> BoxW<'a> {
    pub fn new(buffer: &'a mut Vec<u8>) -> Self {
        Self(buffer)
    }
    pub fn buffer_mut(&mut self) -> &mut Vec<u8> {
        self.0
    }
    pub fn write<T: WriteBox>(&mut self, b: T) {
        let start = self.0.len();
        self.0.extend(0u32.to_be_bytes());
        self.0.extend(T::BOXTYPE);

        if let Some(ver) = T::VERSION {
            self.0.push(ver);
            self.0.extend(&b.flags().to_be_bytes()[1..]);
        }
        b.write(self.0);
        let size = (self.0.len() - start) as u32;
        self.0[start..start + 4].copy_from_slice(&size.to_be_bytes());
    }
}

pub trait WriteBox {
    const BOXTYPE: [u8; 4];
    const VERSION: Option<u8> = None;
    fn write(&self, buf: &mut Vec<u8>);
    fn flags(&self) -> u32 {
        0
    }
}

impl<T: WriteBox> WriteBox for &T {
    const BOXTYPE: [u8; 4] = T::BOXTYPE;
    const VERSION: Option<u8> = T::VERSION;
    fn write(&self, buf: &mut Vec<u8>) {
        (**self).write(buf);
    }
    fn flags(&self) -> u32 {
        (**self).flags()
    }
}