scuffle_h265/
config.rs

1use std::io::{
2    Read, Write, {self},
3};
4
5use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
6use bytes::Bytes;
7use scuffle_bytes_util::{BitReader, BitWriter};
8
9use crate::{ConstantFrameRate, NALUnitType, NumTemporalLayers, ParallelismType, ProfileCompatibilityFlags};
10
11/// HEVC Decoder Configuration Record.
12///
13/// ISO/IEC 14496-15 - 8.3.2.1
14#[derive(Debug, Clone, PartialEq)]
15pub struct HEVCDecoderConfigurationRecord {
16    /// Matches the [`general_profile_space`](crate::Profile::profile_space) field as defined in ISO/IEC 23008-2.
17    pub general_profile_space: u8,
18    /// Matches the [`general_tier_flag`](crate::Profile::tier_flag) field as defined in ISO/IEC 23008-2.
19    pub general_tier_flag: bool,
20    /// Matches the [`general_profile_idc`](crate::Profile::profile_idc) field as defined in ISO/IEC 23008-2.
21    pub general_profile_idc: u8,
22    /// Matches the [`general_profile_compatibility_flag`](crate::Profile::profile_compatibility_flag) field as defined in ISO/IEC 23008-2.
23    pub general_profile_compatibility_flags: ProfileCompatibilityFlags,
24    /// This is stored as a 48-bit (6 bytes) unsigned integer.
25    /// Therefore only the first 48 bits of this value are used.
26    pub general_constraint_indicator_flags: u64,
27    /// Matches the [`general_level_idc`](crate::Profile::level_idc) field as defined in ISO/IEC 23008-2.
28    pub general_level_idc: u8,
29    /// Matches the [`min_spatial_segmentation_idc`](crate::BitStreamRestriction::min_spatial_segmentation_idc) field as defined in ISO/IEC 23008-2.
30    pub min_spatial_segmentation_idc: u16,
31    /// See [`ParallelismType`] for more info.
32    pub parallelism_type: ParallelismType,
33    /// Matches the [`chroma_format_idc`](crate::SpsRbsp::chroma_format_idc) field as defined in ISO/IEC 23008-2.
34    pub chroma_format_idc: u8,
35    /// Matches the [`bit_depth_luma_minus8`](crate::SpsRbsp::bit_depth_luma_minus8) field as defined in ISO/IEC 23008-2.
36    pub bit_depth_luma_minus8: u8,
37    /// Matches the [`bit_depth_chroma_minus8`](crate::SpsRbsp::bit_depth_chroma_minus8) field as defined in ISO/IEC 23008-2.
38    pub bit_depth_chroma_minus8: u8,
39    /// Gives the average frame rate in units of frames/(256 seconds), for the stream to
40    /// which this configuration record applies.
41    ///
42    /// Value 0 indicates an unspecified average frame rate.
43    pub avg_frame_rate: u16,
44    /// See [`ConstantFrameRate`] for more info.
45    pub constant_frame_rate: ConstantFrameRate,
46    /// This is the count of tepmoral layers or sub-layers as defined in ISO/IEC 23008-2.
47    pub num_temporal_layers: NumTemporalLayers,
48    /// Equal to `true` indicates that all SPSs that are activated when the stream to which
49    /// this configuration record applies is decoded have
50    /// [`sps_temporal_id_nesting_flag`](crate::SpsRbsp::sps_temporal_id_nesting_flag) as defined in
51    /// ISO/IEC 23008-2 equal to `true` and temporal sub-layer up-switching to any higher temporal layer
52    /// can be performed at any sample.
53    ///
54    /// Value `false` indicates that the conditions above are not or may not be met.
55    pub temporal_id_nested: bool,
56    /// This value plus 1 indicates the length in bytes of the `NALUnitLength` field in an
57    /// HEVC video sample in the stream to which this configuration record applies.
58    ///
59    /// For example, a size of one byte is indicated with a value of 0.
60    /// The value of this field is one of 0, 1, or 3
61    /// corresponding to a length encoded with 1, 2, or 4 bytes, respectively.
62    pub length_size_minus_one: u8,
63    /// [`NaluArray`]s in that are part of this configuration record.
64    pub arrays: Vec<NaluArray>,
65}
66
67/// Nalu Array Structure
68///
69/// ISO/IEC 14496-15 - 8.3.2.1
70#[derive(Debug, Clone, PartialEq)]
71pub struct NaluArray {
72    /// When equal to `true` indicates that all NAL units of the given type are in the
73    /// following array and none are in the stream; when equal to `false` indicates that additional NAL units
74    /// of the indicated type may be in the stream; the default and permitted values are constrained by
75    /// the sample entry name.
76    pub array_completeness: bool,
77    /// Indicates the type of the NAL units in the following array (which shall be all of
78    /// that type); it takes a value as defined in ISO/IEC 23008-2; it is restricted to take one of the
79    /// values indicating a VPS, SPS, PPS, prefix SEI, or suffix SEI NAL unit.
80    pub nal_unit_type: NALUnitType,
81    /// The raw byte stream of NAL units.
82    ///
83    /// You might want to use [`SpsNALUnit::parse`](crate::SpsNALUnit::parse)
84    /// to parse an SPS NAL unit.
85    pub nalus: Vec<Bytes>,
86}
87
88impl HEVCDecoderConfigurationRecord {
89    /// Demuxes an [`HEVCDecoderConfigurationRecord`] from a byte stream.
90    ///
91    /// Returns a demuxed [`HEVCDecoderConfigurationRecord`].
92    pub fn demux(data: impl io::Read) -> io::Result<Self> {
93        let mut bit_reader = BitReader::new(data);
94
95        // This demuxer only supports version 1
96        let configuration_version = bit_reader.read_u8()?;
97        if configuration_version != 1 {
98            return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid configuration version"));
99        }
100
101        let general_profile_space = bit_reader.read_bits(2)? as u8;
102        let general_tier_flag = bit_reader.read_bit()?;
103        let general_profile_idc = bit_reader.read_bits(5)? as u8;
104        let general_profile_compatibility_flags =
105            ProfileCompatibilityFlags::from_bits_retain(bit_reader.read_u32::<BigEndian>()?);
106        let general_constraint_indicator_flags = bit_reader.read_u48::<BigEndian>()?;
107        let general_level_idc = bit_reader.read_u8()?;
108
109        bit_reader.read_bits(4)?; // reserved_4bits
110        let min_spatial_segmentation_idc = bit_reader.read_bits(12)? as u16;
111
112        bit_reader.read_bits(6)?; // reserved_6bits
113        let parallelism_type = bit_reader.read_bits(2)? as u8;
114
115        bit_reader.read_bits(6)?; // reserved_6bits
116        let chroma_format_idc = bit_reader.read_bits(2)? as u8;
117
118        bit_reader.read_bits(5)?; // reserved_5bits
119        let bit_depth_luma_minus8 = bit_reader.read_bits(3)? as u8;
120
121        bit_reader.read_bits(5)?; // reserved_5bits
122        let bit_depth_chroma_minus8 = bit_reader.read_bits(3)? as u8;
123
124        let avg_frame_rate = bit_reader.read_u16::<BigEndian>()?;
125        let constant_frame_rate = bit_reader.read_bits(2)? as u8;
126        let num_temporal_layers = bit_reader.read_bits(3)? as u8;
127        let temporal_id_nested = bit_reader.read_bit()?;
128        let length_size_minus_one = bit_reader.read_bits(2)? as u8;
129
130        if length_size_minus_one == 2 {
131            return Err(io::Error::new(
132                io::ErrorKind::InvalidData,
133                "length_size_minus_one must be 0, 1, or 3",
134            ));
135        }
136
137        let num_of_arrays = bit_reader.read_u8()?;
138
139        let mut arrays = Vec::with_capacity(num_of_arrays as usize);
140
141        for _ in 0..num_of_arrays {
142            let array_completeness = bit_reader.read_bit()?;
143            bit_reader.read_bits(1)?; // reserved
144
145            let nal_unit_type = bit_reader.read_bits(6)? as u8;
146            let nal_unit_type = NALUnitType::from(nal_unit_type);
147            if nal_unit_type != NALUnitType::VpsNut
148                && nal_unit_type != NALUnitType::SpsNut
149                && nal_unit_type != NALUnitType::PpsNut
150                && nal_unit_type != NALUnitType::PrefixSeiNut
151                && nal_unit_type != NALUnitType::SuffixSeiNut
152            {
153                return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid nal_unit_type"));
154            }
155
156            let num_nalus = bit_reader.read_u16::<BigEndian>()?;
157            let mut nalus = Vec::with_capacity(num_nalus as usize);
158            for _ in 0..num_nalus {
159                let nal_unit_length = bit_reader.read_u16::<BigEndian>()?;
160                let mut data = vec![0; nal_unit_length as usize];
161                bit_reader.read_exact(&mut data)?;
162                nalus.push(data.into());
163            }
164
165            arrays.push(NaluArray {
166                array_completeness,
167                nal_unit_type,
168                nalus,
169            });
170        }
171
172        Ok(HEVCDecoderConfigurationRecord {
173            general_profile_space,
174            general_tier_flag,
175            general_profile_idc,
176            general_profile_compatibility_flags,
177            general_constraint_indicator_flags,
178            general_level_idc,
179            min_spatial_segmentation_idc,
180            parallelism_type: ParallelismType(parallelism_type),
181            chroma_format_idc,
182            bit_depth_luma_minus8,
183            bit_depth_chroma_minus8,
184            avg_frame_rate,
185            constant_frame_rate: ConstantFrameRate(constant_frame_rate),
186            num_temporal_layers: NumTemporalLayers(num_temporal_layers),
187            temporal_id_nested,
188            length_size_minus_one,
189            arrays,
190        })
191    }
192
193    /// Returns the total byte size of the [`HEVCDecoderConfigurationRecord`].
194    pub fn size(&self) -> u64 {
195        1 // configuration_version
196        + 1 // general_profile_space, general_tier_flag, general_profile_idc
197        + 4 // general_profile_compatibility_flags
198        + 6 // general_constraint_indicator_flags
199        + 1 // general_level_idc
200        + 2 // reserved_4bits, min_spatial_segmentation_idc
201        + 1 // reserved_6bits, parallelism_type
202        + 1 // reserved_6bits, chroma_format_idc
203        + 1 // reserved_5bits, bit_depth_luma_minus8
204        + 1 // reserved_5bits, bit_depth_chroma_minus8
205        + 2 // avg_frame_rate
206        + 1 // constant_frame_rate, num_temporal_layers, temporal_id_nested, length_size_minus_one
207        + 1 // num_of_arrays
208        + self.arrays.iter().map(|array| {
209            1 // array_completeness, reserved, nal_unit_type
210            + 2 // num_nalus
211            + array.nalus.iter().map(|nalu| {
212                2 // nal_unit_length
213                + nalu.len() as u64 // nal_unit
214            }).sum::<u64>()
215        }).sum::<u64>()
216    }
217
218    /// Muxes the [`HEVCDecoderConfigurationRecord`] into a byte stream.
219    ///
220    /// Returns a muxed byte stream.
221    pub fn mux<T: io::Write>(&self, writer: &mut T) -> io::Result<()> {
222        let mut bit_writer = BitWriter::new(writer);
223
224        // This muxer only supports version 1
225        bit_writer.write_u8(1)?; // configuration_version
226        bit_writer.write_bits(self.general_profile_space as u64, 2)?;
227        bit_writer.write_bit(self.general_tier_flag)?;
228        bit_writer.write_bits(self.general_profile_idc as u64, 5)?;
229        bit_writer.write_u32::<BigEndian>(self.general_profile_compatibility_flags.bits())?;
230        bit_writer.write_u48::<BigEndian>(self.general_constraint_indicator_flags)?;
231        bit_writer.write_u8(self.general_level_idc)?;
232
233        bit_writer.write_bits(0b1111, 4)?; // reserved_4bits
234        bit_writer.write_bits(self.min_spatial_segmentation_idc as u64, 12)?;
235
236        bit_writer.write_bits(0b111111, 6)?; // reserved_6bits
237        bit_writer.write_bits(self.parallelism_type.0 as u64, 2)?;
238
239        bit_writer.write_bits(0b111111, 6)?; // reserved_6bits
240        bit_writer.write_bits(self.chroma_format_idc as u64, 2)?;
241
242        bit_writer.write_bits(0b11111, 5)?; // reserved_5bits
243        bit_writer.write_bits(self.bit_depth_luma_minus8 as u64, 3)?;
244
245        bit_writer.write_bits(0b11111, 5)?; // reserved_5bits
246        bit_writer.write_bits(self.bit_depth_chroma_minus8 as u64, 3)?;
247
248        bit_writer.write_u16::<BigEndian>(self.avg_frame_rate)?;
249        bit_writer.write_bits(self.constant_frame_rate.0 as u64, 2)?;
250
251        bit_writer.write_bits(self.num_temporal_layers.0 as u64, 3)?;
252        bit_writer.write_bit(self.temporal_id_nested)?;
253        bit_writer.write_bits(self.length_size_minus_one as u64, 2)?;
254
255        bit_writer.write_u8(self.arrays.len() as u8)?;
256        for array in &self.arrays {
257            bit_writer.write_bit(array.array_completeness)?;
258            bit_writer.write_bits(0b0, 1)?; // reserved
259            bit_writer.write_bits(u8::from(array.nal_unit_type) as u64, 6)?;
260
261            bit_writer.write_u16::<BigEndian>(array.nalus.len() as u16)?;
262
263            for nalu in &array.nalus {
264                bit_writer.write_u16::<BigEndian>(nalu.len() as u16)?;
265                bit_writer.write_all(nalu)?;
266            }
267        }
268
269        bit_writer.finish()?;
270
271        Ok(())
272    }
273}
274
275#[cfg(test)]
276#[cfg_attr(all(test, coverage_nightly), coverage(off))]
277mod tests {
278    use std::io;
279
280    use bytes::Bytes;
281
282    use crate::{
283        ConstantFrameRate, HEVCDecoderConfigurationRecord, NALUnitType, NumTemporalLayers, ParallelismType,
284        ProfileCompatibilityFlags, SpsNALUnit,
285    };
286
287    #[test]
288    fn test_config_demux() {
289        // h265 config
290        let data = Bytes::from(b"\x01\x01@\0\0\0\x90\0\0\0\0\0\x99\xf0\0\xfc\xfd\xf8\xf8\0\0\x0f\x03 \0\x01\0\x18@\x01\x0c\x01\xff\xff\x01@\0\0\x03\0\x90\0\0\x03\0\0\x03\0\x99\x95@\x90!\0\x01\0=B\x01\x01\x01@\0\0\x03\0\x90\0\0\x03\0\0\x03\0\x99\xa0\x01@ \x05\xa1e\x95R\x90\x84d_\xf8\xc0Z\x80\x80\x80\x82\0\0\x03\0\x02\0\0\x03\x01 \xc0\x0b\xbc\xa2\0\x02bX\0\x011-\x08\"\0\x01\0\x07D\x01\xc0\x93|\x0c\xc9".to_vec());
291
292        let config = HEVCDecoderConfigurationRecord::demux(&mut io::Cursor::new(data)).unwrap();
293
294        assert_eq!(config.general_profile_space, 0);
295        assert!(!config.general_tier_flag);
296        assert_eq!(config.general_profile_idc, 1);
297        assert_eq!(
298            config.general_profile_compatibility_flags,
299            ProfileCompatibilityFlags::MainProfile
300        );
301        assert_eq!(config.general_constraint_indicator_flags, (1 << 47) | (1 << 44)); // 1. bit and 4. bit
302        assert_eq!(config.general_level_idc, 153);
303        assert_eq!(config.min_spatial_segmentation_idc, 0);
304        assert_eq!(config.parallelism_type, ParallelismType::MixedOrUnknown);
305        assert_eq!(config.chroma_format_idc, 1);
306        assert_eq!(config.bit_depth_luma_minus8, 0);
307        assert_eq!(config.bit_depth_chroma_minus8, 0);
308        assert_eq!(config.avg_frame_rate, 0);
309        assert_eq!(config.constant_frame_rate, ConstantFrameRate::Unknown);
310        assert_eq!(config.num_temporal_layers, NumTemporalLayers::NotScalable);
311        assert!(config.temporal_id_nested);
312        assert_eq!(config.length_size_minus_one, 3);
313        assert_eq!(config.arrays.len(), 3);
314
315        let vps = &config.arrays[0];
316        assert!(!vps.array_completeness);
317        assert_eq!(vps.nal_unit_type, NALUnitType::VpsNut);
318        assert_eq!(vps.nalus.len(), 1);
319
320        let sps = &config.arrays[1];
321        assert!(!sps.array_completeness);
322        assert_eq!(sps.nal_unit_type, NALUnitType::SpsNut);
323        assert_eq!(sps.nalus.len(), 1);
324        let sps = SpsNALUnit::parse(io::Cursor::new(sps.nalus[0].clone())).unwrap();
325        insta::assert_debug_snapshot!(sps);
326
327        let pps = &config.arrays[2];
328        assert!(!pps.array_completeness);
329        assert_eq!(pps.nal_unit_type, NALUnitType::PpsNut);
330        assert_eq!(pps.nalus.len(), 1);
331    }
332
333    #[test]
334    fn test_config_mux() {
335        let data = Bytes::from(b"\x01\x01@\0\0\0\x90\0\0\0\0\0\x99\xf0\0\xfc\xfd\xf8\xf8\0\0\x0f\x03 \0\x01\0\x18@\x01\x0c\x01\xff\xff\x01@\0\0\x03\0\x90\0\0\x03\0\0\x03\0\x99\x95@\x90!\0\x01\0=B\x01\x01\x01@\0\0\x03\0\x90\0\0\x03\0\0\x03\0\x99\xa0\x01@ \x05\xa1e\x95R\x90\x84d_\xf8\xc0Z\x80\x80\x80\x82\0\0\x03\0\x02\0\0\x03\x01 \xc0\x0b\xbc\xa2\0\x02bX\0\x011-\x08\"\0\x01\0\x07D\x01\xc0\x93|\x0c\xc9".to_vec());
336
337        let config = HEVCDecoderConfigurationRecord::demux(&mut io::Cursor::new(data.clone())).unwrap();
338
339        assert_eq!(config.size(), data.len() as u64);
340
341        let mut buf = Vec::new();
342        config.mux(&mut buf).unwrap();
343
344        assert_eq!(buf, data.to_vec());
345    }
346}