Skip to main content

mp4/mp4box/
vpcc.rs

1use crate::mp4box::*;
2use crate::Mp4Box;
3use serde::Serialize;
4
5#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
6pub struct VpccBox {
7    pub version: u8,
8    pub flags: u32,
9    pub profile: u8,
10    pub level: u8,
11    pub bit_depth: u8,
12    pub chroma_subsampling: u8,
13    pub video_full_range_flag: bool,
14    pub color_primaries: u8,
15    pub transfer_characteristics: u8,
16    pub matrix_coefficients: u8,
17    pub codec_initialization_data_size: u16,
18}
19
20impl VpccBox {
21    pub const DEFAULT_VERSION: u8 = 1;
22    pub const DEFAULT_BIT_DEPTH: u8 = 8;
23}
24
25impl Mp4Box for VpccBox {
26    fn box_type(&self) -> BoxType {
27        BoxType::VpccBox
28    }
29
30    fn box_size(&self) -> u64 {
31        HEADER_SIZE + HEADER_EXT_SIZE + 8
32    }
33
34    fn to_json(&self) -> Result<String> {
35        Ok(serde_json::to_string(&self).unwrap())
36    }
37
38    fn summary(&self) -> Result<String> {
39        Ok(format!("{self:?}"))
40    }
41}
42
43impl<R: Read + Seek> ReadBox<&mut R> for VpccBox {
44    fn read_box(reader: &mut R, size: u64) -> Result<Self> {
45        let start = box_start(reader)?;
46        let (version, flags) = read_box_header_ext(reader)?;
47
48        let profile: u8 = reader.read_u8()?;
49        let level: u8 = reader.read_u8()?;
50        let (bit_depth, chroma_subsampling, video_full_range_flag) = {
51            let b = reader.read_u8()?;
52            (b >> 4, b << 4 >> 5, b & 0x01 == 1)
53        };
54        let transfer_characteristics: u8 = reader.read_u8()?;
55        let matrix_coefficients: u8 = reader.read_u8()?;
56        let codec_initialization_data_size: u16 = reader.read_u16::<BigEndian>()?;
57
58        skip_bytes_to(reader, start + size)?;
59
60        Ok(Self {
61            version,
62            flags,
63            profile,
64            level,
65            bit_depth,
66            chroma_subsampling,
67            video_full_range_flag,
68            color_primaries: 0,
69            transfer_characteristics,
70            matrix_coefficients,
71            codec_initialization_data_size,
72        })
73    }
74}
75
76impl<W: Write> WriteBox<&mut W> for VpccBox {
77    fn write_box(&self, writer: &mut W) -> Result<u64> {
78        let size = self.box_size();
79        BoxHeader::new(self.box_type(), size).write(writer)?;
80
81        write_box_header_ext(writer, self.version, self.flags)?;
82
83        writer.write_u8(self.profile)?;
84        writer.write_u8(self.level)?;
85        writer.write_u8(
86            (self.bit_depth << 4)
87                | (self.chroma_subsampling << 1)
88                | (self.video_full_range_flag as u8),
89        )?;
90        writer.write_u8(self.color_primaries)?;
91        writer.write_u8(self.transfer_characteristics)?;
92        writer.write_u8(self.matrix_coefficients)?;
93        writer.write_u16::<BigEndian>(self.codec_initialization_data_size)?;
94
95        Ok(size)
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102    use crate::mp4box::BoxHeader;
103    use std::io::Cursor;
104
105    #[test]
106    fn test_vpcc() {
107        let src_box = VpccBox {
108            version: VpccBox::DEFAULT_VERSION,
109            flags: 0,
110            profile: 0,
111            level: 0x1F,
112            bit_depth: VpccBox::DEFAULT_BIT_DEPTH,
113            chroma_subsampling: 0,
114            video_full_range_flag: false,
115            color_primaries: 0,
116            transfer_characteristics: 0,
117            matrix_coefficients: 0,
118            codec_initialization_data_size: 0,
119        };
120        let mut buf = Vec::new();
121        src_box.write_box(&mut buf).unwrap();
122        assert_eq!(buf.len(), src_box.box_size() as usize);
123
124        let mut reader = Cursor::new(&buf);
125        let header = BoxHeader::read(&mut reader).unwrap();
126        assert_eq!(header.name, BoxType::VpccBox);
127        assert_eq!(src_box.box_size(), header.size);
128
129        let dst_box = VpccBox::read_box(&mut reader, header.size).unwrap();
130        assert_eq!(src_box, dst_box);
131    }
132}