scuffle_mp4/boxes/types/
clap.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/// Clean Aperture Box
11/// ISO/IEC 14496-12:2022(E) - 12.1.4.2
12pub struct Clap {
13    pub header: BoxHeader,
14    pub clean_aperture_width_n: u32,
15    pub clean_aperture_width_d: u32,
16    pub clean_aperture_height_n: u32,
17    pub clean_aperture_height_d: u32,
18    pub horiz_off_n: u32,
19    pub horiz_off_d: u32,
20    pub vert_off_n: u32,
21    pub vert_off_d: u32,
22}
23
24impl BoxType for Clap {
25    const NAME: [u8; 4] = *b"clap";
26
27    fn demux(header: BoxHeader, data: Bytes) -> io::Result<Self> {
28        let mut reader = io::Cursor::new(data);
29
30        let clean_aperture_width_n = reader.read_u32::<BigEndian>()?;
31        let clean_aperture_width_d = reader.read_u32::<BigEndian>()?;
32        let clean_aperture_height_n = reader.read_u32::<BigEndian>()?;
33        let clean_aperture_height_d = reader.read_u32::<BigEndian>()?;
34        let horiz_off_n = reader.read_u32::<BigEndian>()?;
35        let horiz_off_d = reader.read_u32::<BigEndian>()?;
36        let vert_off_n = reader.read_u32::<BigEndian>()?;
37        let vert_off_d = reader.read_u32::<BigEndian>()?;
38
39        Ok(Self {
40            header,
41
42            clean_aperture_width_n,
43            clean_aperture_width_d,
44            clean_aperture_height_n,
45            clean_aperture_height_d,
46            horiz_off_n,
47            horiz_off_d,
48            vert_off_n,
49            vert_off_d,
50        })
51    }
52
53    fn primitive_size(&self) -> u64 {
54        4 // clean_aperture_width_n
55        + 4 // clean_aperture_width_d
56        + 4 // clean_aperture_height_n
57        + 4 // clean_aperture_height_d
58        + 4 // horiz_off_n
59        + 4 // horiz_off_d
60        + 4 // vert_off_n
61        + 4 // vert_off_d
62    }
63
64    fn primitive_mux<T: io::Write>(&self, writer: &mut T) -> io::Result<()> {
65        writer.write_u32::<BigEndian>(self.clean_aperture_width_n)?;
66        writer.write_u32::<BigEndian>(self.clean_aperture_width_d)?;
67        writer.write_u32::<BigEndian>(self.clean_aperture_height_n)?;
68        writer.write_u32::<BigEndian>(self.clean_aperture_height_d)?;
69        writer.write_u32::<BigEndian>(self.horiz_off_n)?;
70        writer.write_u32::<BigEndian>(self.horiz_off_d)?;
71        writer.write_u32::<BigEndian>(self.vert_off_n)?;
72        writer.write_u32::<BigEndian>(self.vert_off_d)?;
73
74        Ok(())
75    }
76}