scuffle_mp4/boxes/types/
pasp.rs

1use std::io;
2
3use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
4use bytes::Bytes;
5
6use crate::boxes::header::BoxHeader;
7use crate::boxes::traits::BoxType;
8
9#[derive(Debug, Clone, PartialEq)]
10/// Pixel Aspect Ratio Box
11/// ISO/IEC 14496-12:2022(E) - 12.1.4.2
12pub struct Pasp {
13    pub header: BoxHeader,
14    pub h_spacing: u32,
15    pub v_spacing: u32,
16}
17
18impl Default for Pasp {
19    fn default() -> Self {
20        Self::new()
21    }
22}
23
24impl Pasp {
25    pub fn new() -> Self {
26        Self {
27            header: BoxHeader::new(Self::NAME),
28            h_spacing: 1,
29            v_spacing: 1,
30        }
31    }
32}
33
34impl BoxType for Pasp {
35    const NAME: [u8; 4] = *b"pasp";
36
37    fn demux(header: BoxHeader, data: Bytes) -> io::Result<Self> {
38        let mut reader = io::Cursor::new(data);
39
40        let h_spacing = reader.read_u32::<BigEndian>()?;
41        let v_spacing = reader.read_u32::<BigEndian>()?;
42
43        Ok(Self {
44            header,
45
46            h_spacing,
47            v_spacing,
48        })
49    }
50
51    fn primitive_size(&self) -> u64 {
52        4 // h_spacing
53        + 4 // v_spacing
54    }
55
56    fn primitive_mux<T: io::Write>(&self, writer: &mut T) -> io::Result<()> {
57        writer.write_u32::<BigEndian>(self.h_spacing)?;
58        writer.write_u32::<BigEndian>(self.v_spacing)?;
59
60        Ok(())
61    }
62}