scuffle_mp4/boxes/types/
hev1.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::io;

use bytes::{Buf, Bytes};

use super::btrt::Btrt;
use super::hvcc::HvcC;
use super::stsd::{SampleEntry, VisualSampleEntry};
use crate::boxes::header::BoxHeader;
use crate::boxes::traits::BoxType;
use crate::boxes::DynBox;
use crate::codec::VideoCodec;

#[derive(Debug, Clone, PartialEq)]
/// HEVC (H.265) Codec Box
/// ISO/IEC 14496-15:2022 - 8.4
pub struct Hev1 {
    pub header: BoxHeader,
    pub visual_sample_entry: SampleEntry<VisualSampleEntry>,
    pub hvcc: HvcC,
    pub btrt: Option<Btrt>,
    pub unknown: Vec<DynBox>,
}

impl Hev1 {
    pub fn new(visual_sample_entry: SampleEntry<VisualSampleEntry>, hvcc: HvcC, btrt: Option<Btrt>) -> Self {
        Self {
            header: BoxHeader::new(Self::NAME),
            visual_sample_entry,
            hvcc,
            btrt,
            unknown: Vec::new(),
        }
    }

    pub fn codec(&self) -> io::Result<VideoCodec> {
        Ok(VideoCodec::Hevc {
            constraint_indicator: self.hvcc.hevc_config.general_constraint_indicator_flags,
            level: self.hvcc.hevc_config.general_level_idc,
            profile: self.hvcc.hevc_config.general_profile_idc,
            profile_compatibility: self.hvcc.hevc_config.general_profile_compatibility_flags,
            tier: self.hvcc.hevc_config.general_tier_flag,
            general_profile_space: self.hvcc.hevc_config.general_profile_space,
        })
    }
}

impl BoxType for Hev1 {
    const NAME: [u8; 4] = *b"hev1";

    fn demux(header: BoxHeader, data: Bytes) -> io::Result<Self> {
        let mut reader = io::Cursor::new(data);

        let mut visual_sample_entry = SampleEntry::<VisualSampleEntry>::demux(&mut reader)?;

        let mut hvcc = None;
        let mut btrt = None;
        let mut unknown = Vec::new();

        while reader.has_remaining() {
            let dyn_box = DynBox::demux(&mut reader)?;
            match dyn_box {
                DynBox::HvcC(b) => {
                    hvcc = Some(*b);
                }
                DynBox::Btrt(b) => {
                    btrt = Some(*b);
                }
                DynBox::Clap(b) => {
                    visual_sample_entry.extension.clap = Some(*b);
                }
                DynBox::Pasp(b) => {
                    visual_sample_entry.extension.pasp = Some(*b);
                }
                DynBox::Colr(b) => {
                    visual_sample_entry.extension.colr = Some(*b);
                }
                _ => {
                    unknown.push(dyn_box);
                }
            }
        }

        let hvcc = hvcc.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "trak box is missing tkhd box"))?;

        Ok(Self {
            header,
            visual_sample_entry,
            hvcc,
            btrt,
            unknown,
        })
    }

    fn primitive_size(&self) -> u64 {
        self.visual_sample_entry.size()
            + self.hvcc.size()
            + self.btrt.as_ref().map(|b| b.size()).unwrap_or(0)
            + self.unknown.iter().map(|b| b.size()).sum::<u64>()
    }

    fn primitive_mux<T: io::Write>(&self, writer: &mut T) -> io::Result<()> {
        self.visual_sample_entry.mux(writer)?;
        self.hvcc.mux(writer)?;
        if let Some(btrt) = &self.btrt {
            btrt.mux(writer)?;
        }
        for unknown in &self.unknown {
            unknown.mux(writer)?;
        }
        Ok(())
    }
}