Skip to main content

mp4_atom/moov/trak/mdia/minf/
hmhd.rs

1use crate::*;
2
3#[derive(Debug, Clone, PartialEq, Eq, Default)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5pub struct Hmhd {
6    pub max_pdu_size: u16,
7    pub avg_pdu_size: u16,
8    pub max_bitrate: u32,
9    pub avg_bitrate: u32,
10}
11
12impl AtomExt for Hmhd {
13    type Ext = ();
14
15    const KIND_EXT: FourCC = FourCC::new(b"hmhd");
16
17    fn decode_body_ext<B: Buf>(buf: &mut B, _ext: ()) -> Result<Self> {
18        let max_pdu_size = u16::decode(buf)?;
19        let avg_pdu_size = u16::decode(buf)?;
20        let max_bitrate = u32::decode(buf)?;
21        let avg_bitrate = u32::decode(buf)?;
22        u32::decode(buf)?; // reserved
23        Ok(Hmhd {
24            max_pdu_size,
25            avg_pdu_size,
26            max_bitrate,
27            avg_bitrate,
28        })
29    }
30
31    fn encode_body_ext<B: BufMut>(&self, buf: &mut B) -> Result<()> {
32        self.max_pdu_size.encode(buf)?;
33        self.avg_pdu_size.encode(buf)?;
34        self.max_bitrate.encode(buf)?;
35        self.avg_bitrate.encode(buf)?;
36        0u32.encode(buf)?; // reserved
37        Ok(())
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_hmhd() {
47        let hmhd = Hmhd {
48            max_pdu_size: 400,
49            avg_pdu_size: 115,
50            max_bitrate: 6700,
51            avg_bitrate: 4123,
52        };
53        let mut buf = Vec::new();
54        hmhd.encode(&mut buf).unwrap();
55        assert_eq!(
56            buf,
57            vec![
58                0x00, 0x00, 0x00, 0x1c, 0x68, 0x6d, 0x68, 0x64, 0x00, 0x00, 0x00, 0x00, 0x01, 0x90,
59                0x00, 0x73, 0x00, 0x00, 0x1a, 0x2c, 0x00, 0x00, 0x10, 0x1b, 0x00, 0x00, 0x00, 0x00
60            ]
61        );
62
63        let mut buf = buf.as_ref();
64        let decoded = Hmhd::decode(&mut buf).unwrap();
65        assert_eq!(decoded, hmhd);
66    }
67}