isobmff/boxes/
protected_streams.rs

1use std::io;
2
3use scuffle_bytes_util::zero_copy::{Deserialize, DeserializeSeed, Serialize};
4
5use super::StereoVideoBox;
6use crate::{BoxHeader, FullBoxHeader, IsoBox, UnknownBox, Utf8String};
7
8/// Protection scheme information box
9///
10/// ISO/IEC 14496-12 - 8.12.2
11#[derive(IsoBox, Debug, PartialEq, Eq)]
12#[iso_box(box_type = b"sinf", crate_path = crate)]
13pub struct ProtectionSchemeInfoBox<'a> {
14    /// The contained [`OriginalFormatBox`]. (mandatory)
15    #[iso_box(nested_box)]
16    pub orginal_format: OriginalFormatBox,
17    /// The contained [`SchemeTypeBox`]. (optional)
18    #[iso_box(nested_box(collect))]
19    pub scheme_type: Option<SchemeTypeBox>,
20    /// The contained [`SchemeInformationBox`]. (optional)
21    #[iso_box(nested_box(collect))]
22    pub info: Option<SchemeInformationBox<'a>>,
23}
24
25/// Original format box
26///
27/// ISO/IEC 14496-12 - 8.12.3
28#[derive(IsoBox, Debug, PartialEq, Eq)]
29#[iso_box(box_type = b"frma", crate_path = crate)]
30pub struct OriginalFormatBox {
31    /// The four character code of the original un-transformed sample entry
32    /// (e.g. 'mp4v' if the stream contains protected or restricted MPEG-4 visual material).
33    pub data_format: [u8; 4],
34}
35
36/// Scheme type box
37///
38/// ISO/IEC 14496-12 - 8.12.6
39#[derive(IsoBox, Debug, PartialEq, Eq)]
40#[iso_box(box_type = b"schm", skip_impl(deserialize_seed, serialize), crate_path = crate)]
41pub struct SchemeTypeBox {
42    /// The full box header.
43    pub full_header: FullBoxHeader,
44    /// The code defining the protection or restriction scheme, normally expressed as a four character code.
45    pub scheme_type: [u8; 4],
46    /// The version of the scheme (used to create the content).
47    pub scheme_version: u32,
48    /// An absolute URI allowing for the option of directing the user to a web-page if they do not
49    /// have the scheme installed on their system.
50    pub scheme_uri: Option<Utf8String>,
51}
52
53impl<'a> DeserializeSeed<'a, BoxHeader> for SchemeTypeBox {
54    fn deserialize_seed<R>(mut reader: R, _seed: BoxHeader) -> std::io::Result<Self>
55    where
56        R: scuffle_bytes_util::zero_copy::ZeroCopyReader<'a>,
57    {
58        let full_header = FullBoxHeader::deserialize(&mut reader)?;
59
60        let scheme_type = <[u8; 4]>::deserialize(&mut reader)?;
61        let scheme_version = u32::deserialize(&mut reader)?;
62        let scheme_uri = if (*full_header.flags & 0x000001) != 0 {
63            Some(Utf8String::deserialize(&mut reader)?)
64        } else {
65            None
66        };
67
68        Ok(Self {
69            full_header,
70            scheme_type,
71            scheme_version,
72            scheme_uri,
73        })
74    }
75}
76
77impl Serialize for SchemeTypeBox {
78    fn serialize<W>(&self, mut writer: W) -> io::Result<()>
79    where
80        W: std::io::Write,
81    {
82        self.serialize_box_header(&mut writer)?;
83        self.full_header.serialize(&mut writer)?;
84
85        self.scheme_type.serialize(&mut writer)?;
86        self.scheme_version.serialize(&mut writer)?;
87
88        if (*self.full_header.flags & 0x000001) != 0 {
89            self.scheme_uri
90                .as_ref()
91                .ok_or(io::Error::new(io::ErrorKind::InvalidData, "scheme_uri is required"))?
92                .serialize(&mut writer)?;
93        }
94
95        Ok(())
96    }
97}
98
99/// Scheme information box
100///
101/// ISO/IEC 14496-12 - 8.12.7
102#[derive(IsoBox, Debug, PartialEq, Eq)]
103#[iso_box(box_type = b"schi", crate_path = crate)]
104pub struct SchemeInformationBox<'a> {
105    /// The contained [`StereoVideoBox`]. (optional)
106    #[iso_box(nested_box(collect))]
107    pub stvi: Option<StereoVideoBox<'a>>,
108    /// A list of unknown boxes that were not recognized during deserialization.
109    #[iso_box(nested_box(collect_unknown))]
110    pub boxes: Vec<UnknownBox<'a>>,
111}
112
113/// Scheme information box
114///
115/// ISO/IEC 14496-12 - 8.12.7
116#[derive(IsoBox, Debug, PartialEq, Eq)]
117#[iso_box(box_type = b"scrb", crate_path = crate)]
118pub struct ScrambleSchemeInfoBox<'a> {
119    /// The contained [`OriginalFormatBox`]. (mandatory)
120    #[iso_box(nested_box)]
121    pub scheme_type_box: SchemeTypeBox,
122    /// The contained [`SchemeInformationBox`]. (optional)
123    #[iso_box(nested_box(collect))]
124    pub info: Option<SchemeInformationBox<'a>>,
125}