mp4_atom/moov/trak/mdia/minf/stbl/stsd/
visual.rs

1use crate::coding::{Decode, Encode};
2use crate::{Buf, BufMut, Compressor, FixedPoint, Result};
3
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct Visual {
7    pub data_reference_index: u16,
8    pub width: u16,
9    pub height: u16,
10    pub horizresolution: FixedPoint<u16>,
11    pub vertresolution: FixedPoint<u16>,
12    pub frame_count: u16,
13    pub compressor: Compressor,
14    pub depth: u16,
15}
16
17impl Default for Visual {
18    fn default() -> Self {
19        Self {
20            data_reference_index: 0,
21            width: 0,
22            height: 0,
23            horizresolution: 0x48.into(),
24            vertresolution: 0x48.into(),
25            frame_count: 1,
26            compressor: Default::default(),
27            depth: 0x0018,
28        }
29    }
30}
31
32impl Encode for Visual {
33    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
34        0u32.encode(buf)?; // reserved
35        0u16.encode(buf)?; // reserved
36        self.data_reference_index.encode(buf)?;
37
38        0u32.encode(buf)?; // pre-defined, reserved
39        0u64.encode(buf)?; // pre-defined
40        0u32.encode(buf)?; // pre-defined
41        self.width.encode(buf)?;
42        self.height.encode(buf)?;
43        self.horizresolution.encode(buf)?;
44        self.vertresolution.encode(buf)?;
45        0u32.encode(buf)?; // reserved
46        self.frame_count.encode(buf)?;
47        self.compressor.encode(buf)?;
48        self.depth.encode(buf)?;
49        (-1i16).encode(buf)?; // pre-defined
50
51        Ok(())
52    }
53}
54impl Decode for Visual {
55    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
56        /*
57        class VisualSampleEntry(codingname) extends SampleEntry (codingname){
58            unsigned int(16) pre_defined = 0;
59            const unsigned int(16) reserved = 0;
60            unsigned int(32)[3]  pre_defined = 0;
61            unsigned int(16)  width;
62            unsigned int(16)  height;
63            template unsigned int(32)  horizresolution = 0x00480000; // 72 dpi
64            template unsigned int(32)  vertresolution  = 0x00480000; // 72 dpi
65            const unsigned int(32)  reserved = 0;
66            template unsigned int(16)  frame_count = 1;
67            string[32]  compressorname;
68            template unsigned int(16)  depth = 0x0018;
69            int(16)  pre_defined = -1;
70            // other boxes from derived specifications
71            CleanApertureBox     clap;    // optional
72            PixelAspectRatioBox  pasp;    // optional
73        }
74
75         */
76
77        // SampleEntry
78        <[u8; 6]>::decode(buf)?;
79        let data_reference_index = u16::decode(buf)?;
80
81        // VisualSampleEntry
82        // 16 bytes of garb at the front
83        <[u8; 16]>::decode(buf)?;
84        let width = u16::decode(buf)?;
85        let height = u16::decode(buf)?;
86        let horizresolution = FixedPoint::decode(buf)?;
87        let vertresolution = FixedPoint::decode(buf)?;
88        u32::decode(buf)?; // reserved
89        let frame_count = u16::decode(buf)?;
90        let compressor = Compressor::decode(buf)?;
91        let depth = u16::decode(buf)?;
92        i16::decode(buf)?; // pre-defined
93
94        Ok(Self {
95            data_reference_index,
96            width,
97            height,
98            horizresolution,
99            vertresolution,
100            frame_count,
101            compressor,
102            depth,
103        })
104    }
105}