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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use std::io;
use byteorder::{BigEndian, ReadBytesExt};
use scuffle_bytes_util::{BitReader, range_check};
use crate::ProfileCompatibilityFlags;
/// Profile, tier and level.
///
/// `profile_tier_level(profilePresentFlag, maxNumSubLayersMinus1)`
///
/// - ISO/IEC 23008-2 - 7.3.3
/// - ISO/IEC 23008-2 - 7.4.4
#[derive(Debug, Clone, PartialEq)]
pub struct ProfileTierLevel {
/// `general_profile_space`, `general_tier_flag`, `general_profile_idc`, `general_profile_compatibility_flag[j]`,
/// `general_progressive_source_flag`, `general_interlaced_source_flag`, `general_non_packed_constraint_flag`,
/// `general_frame_only_constraint_flag`, `general_max_12bit_constraint_flag`, `general_max_10bit_constraint_flag`,
/// `general_max_8bit_constraint_flag`, `general_max_422chroma_constraint_flag`,
/// `general_max_420chroma_constraint_flag`, `general_max_monochrome_constraint_flag`,
/// `general_intra_constraint_flag`, `general_one_picture_only_constraint_flag`,
/// `general_lower_bit_rate_constraint_flag`, `general_max_14bit_constraint_flag`, `general_inbld_flag`
/// and `general_level_idc`.
pub general_profile: Profile,
/// `sub_layer_profile_space[i]`, `sub_layer_tier_flag[i]`,
/// `sub_layer_profile_idc[i]`,
/// `sub_layer_profile_compatibility_flag[i][j]`,
/// `sub_layer_progressive_source_flag[i]`,
/// `sub_layer_interlaced_source_flag[i]`,
/// `sub_layer_non_packed_constraint_flag[i]`,
/// `sub_layer_frame_only_constraint_flag[i]`,
/// `sub_layer_max_12bit_constraint_flag[i]`,
/// `sub_layer_max_10bit_constraint_flag[i]`,
/// `sub_layer_max_8bit_constraint_flag[i]`,
/// `sub_layer_max_422chroma_constraint_flag[i]`,
/// `sub_layer_max_420chroma_constraint_flag[i]`,
/// `sub_layer_max_monochrome_constraint_flag[i]`,
/// `sub_layer_intra_constraint_flag[i]`,
/// `sub_layer_one_picture_only_constraint_flag[i]`,
/// `sub_layer_lower_bit_rate_constraint_flag[i]`,
/// `sub_layer_max_14bit_constraint_flag[i]`,
/// `sub_layer_inbld_flag[i]`, and
/// `sub_layer_level_idc[i]`.
pub sub_layer_profiles: Vec<Profile>,
}
impl ProfileTierLevel {
pub(crate) fn parse<R: io::Read>(bit_reader: &mut BitReader<R>, max_num_sub_layers_minus_1: u8) -> io::Result<Self> {
// When parsing SPSs, the profile_present_flag is always true. (See 7.3.2.2.1)
// Since this decoder only supports SPS decoding, it is assumed to be true here.
let mut general_profile = Profile::parse(bit_reader, true)?;
// inbld_flag is inferred to be 0 when not present for the genral profile
general_profile.inbld_flag = Some(general_profile.inbld_flag.unwrap_or(false));
let mut sub_layer_profile_present_flags = Vec::with_capacity(max_num_sub_layers_minus_1 as usize);
let mut sub_layer_level_present_flags = Vec::with_capacity(max_num_sub_layers_minus_1 as usize);
for _ in 0..max_num_sub_layers_minus_1 {
sub_layer_profile_present_flags.push(bit_reader.read_bit()?); // sub_layer_profile_present_flag
sub_layer_level_present_flags.push(bit_reader.read_bit()?); // sub_layer_level_present_flag
}
// reserved_zero_2bits
if max_num_sub_layers_minus_1 > 0 && max_num_sub_layers_minus_1 < 8 {
bit_reader.read_bits(2 * (8 - max_num_sub_layers_minus_1))?;
}
let mut sub_layer_profiles = vec![None; max_num_sub_layers_minus_1 as usize];
let mut sub_layer_level_idcs = vec![None; max_num_sub_layers_minus_1 as usize];
for i in 0..max_num_sub_layers_minus_1 as usize {
if sub_layer_profile_present_flags[i] {
sub_layer_profiles[i] = Some(Profile::parse(bit_reader, sub_layer_level_present_flags[i])?);
}
if sub_layer_level_present_flags[i] {
sub_layer_level_idcs[i] = Some(bit_reader.read_u8()?);
}
}
let mut last_profile = general_profile.clone();
let mut sub_layer_profiles: Vec<_> = sub_layer_profiles
.into_iter()
.rev()
.map(|profile| match profile {
Some(profile) => {
let profile = profile.merge(&last_profile);
last_profile = profile.clone();
profile
}
None => last_profile.clone(),
})
.collect();
sub_layer_profiles.reverse(); // reverse back to original order
Ok(ProfileTierLevel {
general_profile,
sub_layer_profiles,
})
}
}
/// Profile part of the Profile, tier and level structure.
#[derive(Debug, Clone, PartialEq)]
pub struct Profile {
/// Decoders shall ignore the CVS when `general_profile_space` is not equal to 0.
pub profile_space: u8,
/// Specifies the tier context for the interpretation of `general_level_idc` as specified in ISO/IEC 23008-2 - Annex A.
pub tier_flag: bool,
/// When `general_profile_space` is equal to 0, indicates a profile to which the CVS
/// conforms as specified in ISO/IEC 23008-2 - Annex A.
pub profile_idc: u8,
/// `profile_compatibility_flag[j]` equal to `true`, when `general_profile_space` is equal to 0, indicates
/// that the CVS conforms to the profile indicated by `general_profile_idc` equal to `j`
/// as specified in ISO/IEC 23008-2 - Annex A.
pub profile_compatibility_flag: ProfileCompatibilityFlags,
/// - If `general_progressive_source_flag` is equal to `true` and
/// [`general_interlaced_source_flag`](Profile::interlaced_source_flag) is equal to `false`, the
/// source scan type of the pictures in the CVS should be interpreted as progressive only.
/// - Otherwise, if `general_progressive_source_flag` is equal to `false` and
/// [`general_interlaced_source_flag`](Profile::interlaced_source_flag) is equal to `true`, the
/// source scan type of the pictures in the CVS should be interpreted as interlaced only.
/// - Otherwise, if `general_progressive_source_flag` is equal to `false` and
/// [`general_interlaced_source_flag`](Profile::interlaced_source_flag) is equal to `false`, the
/// source scan type of the pictures in the CVS should be interpreted as unknown or
/// unspecified.
/// - Otherwise (`general_progressive_source_flag` is equal to `true` and
/// [`general_interlaced_source_flag`](Profile::interlaced_source_flag) is equal to `true`),
/// the source scan type of each picture in the CVS is indicated at the picture level using the syntax
/// element `source_scan_type` in a picture timing SEI message.
pub progressive_source_flag: bool,
/// See [`progressive_source_flag`](Profile::progressive_source_flag).
pub interlaced_source_flag: bool,
/// Equal to `true` specifies that there are no frame packing arrangement
/// SEI messages, segmented rectangular frame packing arrangement SEI messages, equirectangular
/// projection SEI messages, or cubemap projection SEI messages present in the CVS.
///
/// Equal to `false` indicates that there may or may not be one or more frame
/// packing arrangement SEI messages, segmented rectangular frame packing arrangement SEI messages,
/// equirectangular projection SEI messages, or cubemap projection SEI messages present in the CVS.
pub non_packed_constraint_flag: bool,
/// Equal to `true` specifies that `field_seq_flag` is equal to 0.
///
/// Equal to `false` indicates that `field_seq_flag` may or may not be equal to 0.
pub frame_only_constraint_flag: bool,
/// Any additional flags that may be present in the profile.
pub additional_flags: ProfileAdditionalFlags,
/// Equal to `true` specifies that the INBLD capability as specified in ISO/IEC 23008-2 - Annex F is required for
/// decoding of the layer to which the `profile_tier_level( )` syntax structure applies.
///
/// Equal to `false` specifies that the INBLD capability as specified in ISO/IEC 23008-2 - Annex F is not required for
/// decoding of the layer to which the profile_tier_level( ) syntax structure applies.
pub inbld_flag: Option<bool>,
/// Indicates a level to which the CVS conforms as specified in ISO/IEC 23008-2 - Annex A.
///
/// Always present for the general profile.
pub level_idc: Option<u8>,
}
impl Profile {
fn parse<R: io::Read>(bit_reader: &mut BitReader<R>, level_present: bool) -> io::Result<Self> {
let profile_space = bit_reader.read_bits(2)? as u8;
let tier_flag = bit_reader.read_bit()?;
let profile_idc = bit_reader.read_bits(5)? as u8;
let profile_compatibility_flag = ProfileCompatibilityFlags::from_bits_retain(bit_reader.read_u32::<BigEndian>()?);
let check_profile_idcs = |profiles: ProfileCompatibilityFlags| {
profiles.contains(ProfileCompatibilityFlags::from_bits_retain(1 << profile_idc))
|| profile_compatibility_flag.intersects(profiles)
};
let progressive_source_flag = bit_reader.read_bit()?;
let interlaced_source_flag = bit_reader.read_bit()?;
let non_packed_constraint_flag = bit_reader.read_bit()?;
let frame_only_constraint_flag = bit_reader.read_bit()?;
let additional_flags = if check_profile_idcs(
ProfileCompatibilityFlags::FormatRangeExtensionsProfile
| ProfileCompatibilityFlags::HighThroughputProfile
| ProfileCompatibilityFlags::Profile6
| ProfileCompatibilityFlags::Profile7
| ProfileCompatibilityFlags::Profile8
| ProfileCompatibilityFlags::ScreenContentCodingExtensionsProfile
| ProfileCompatibilityFlags::Profile10
| ProfileCompatibilityFlags::HighThroughputScreenContentCodingExtensionsProfile,
) {
let max_12bit_constraint_flag = bit_reader.read_bit()?;
let max_10bit_constraint_flag = bit_reader.read_bit()?;
let max_8bit_constraint_flag = bit_reader.read_bit()?;
let max_422chroma_constraint_flag = bit_reader.read_bit()?;
let max_420chroma_constraint_flag = bit_reader.read_bit()?;
let max_monochrome_constraint_flag = bit_reader.read_bit()?;
let intra_constraint_flag = bit_reader.read_bit()?;
let one_picture_only_constraint_flag = bit_reader.read_bit()?;
let lower_bit_rate_constraint_flag = bit_reader.read_bit()?;
let max_14bit_constraint_flag = if check_profile_idcs(
ProfileCompatibilityFlags::HighThroughputProfile
| ProfileCompatibilityFlags::ScreenContentCodingExtensionsProfile
| ProfileCompatibilityFlags::Profile10
| ProfileCompatibilityFlags::HighThroughputScreenContentCodingExtensionsProfile,
) {
let max_14bit_constraint_flag = bit_reader.read_bit()?;
bit_reader.read_bits(33)?;
Some(max_14bit_constraint_flag)
} else {
bit_reader.read_bits(34)?;
None
};
ProfileAdditionalFlags::Full {
max_12bit_constraint_flag,
max_10bit_constraint_flag,
max_8bit_constraint_flag,
max_422chroma_constraint_flag,
max_420chroma_constraint_flag,
max_monochrome_constraint_flag,
intra_constraint_flag,
one_picture_only_constraint_flag,
lower_bit_rate_constraint_flag,
max_14bit_constraint_flag,
}
} else if check_profile_idcs(ProfileCompatibilityFlags::Main10Profile) {
bit_reader.read_bits(7)?; // reserved_zero_7bits
let one_picture_only_constraint_flag = bit_reader.read_bit()?;
bit_reader.read_bits(35)?; // reserved_zero_35bits
ProfileAdditionalFlags::Main10Profile {
one_picture_only_constraint_flag,
}
} else {
bit_reader.read_bits(43)?; // reserved_zero_43bits
ProfileAdditionalFlags::None
};
let inbld_flag = if check_profile_idcs(
ProfileCompatibilityFlags::MainProfile
| ProfileCompatibilityFlags::Main10Profile
| ProfileCompatibilityFlags::MainStillPictureProfile
| ProfileCompatibilityFlags::FormatRangeExtensionsProfile
| ProfileCompatibilityFlags::HighThroughputProfile
| ProfileCompatibilityFlags::ScreenContentCodingExtensionsProfile
| ProfileCompatibilityFlags::HighThroughputScreenContentCodingExtensionsProfile,
) {
Some(bit_reader.read_bit()?)
} else {
bit_reader.read_bit()?; // reserved_zero_bit
None
};
let mut level_idc_value = None;
if level_present {
let level_idc = bit_reader.read_bits(8)? as u8;
range_check!(level_idc, 0, 254)?;
level_idc_value = Some(level_idc);
}
Ok(Profile {
profile_space,
tier_flag,
profile_idc,
profile_compatibility_flag,
progressive_source_flag,
interlaced_source_flag,
non_packed_constraint_flag,
frame_only_constraint_flag,
additional_flags,
inbld_flag,
level_idc: level_idc_value,
})
}
fn merge(self, defaults: &Self) -> Self {
Self {
additional_flags: self.additional_flags.merge(&defaults.additional_flags),
inbld_flag: self.inbld_flag.or(defaults.inbld_flag),
level_idc: self.level_idc.or(defaults.level_idc),
..self
}
}
}
/// Additional profile flags that can be present in the [profile](Profile).
#[derive(Debug, Clone, PartialEq)]
pub enum ProfileAdditionalFlags {
/// All additional flags are present.
Full {
/// Semantics specified in ISO/IEC 23008-2 - Annex A.
max_12bit_constraint_flag: bool,
/// Semantics specified in ISO/IEC 23008-2 - Annex A.
max_10bit_constraint_flag: bool,
/// Semantics specified in ISO/IEC 23008-2 - Annex A.
max_8bit_constraint_flag: bool,
/// Semantics specified in ISO/IEC 23008-2 - Annex A.
max_422chroma_constraint_flag: bool,
/// Semantics specified in ISO/IEC 23008-2 - Annex A.
max_420chroma_constraint_flag: bool,
/// Semantics specified in ISO/IEC 23008-2 - Annex A.
max_monochrome_constraint_flag: bool,
/// Semantics specified in ISO/IEC 23008-2 - Annex A.
intra_constraint_flag: bool,
/// Semantics specified in ISO/IEC 23008-2 - Annex A.
one_picture_only_constraint_flag: bool,
/// Semantics specified in ISO/IEC 23008-2 - Annex A.
lower_bit_rate_constraint_flag: bool,
/// Semantics specified in ISO/IEC 23008-2 - Annex A.
max_14bit_constraint_flag: Option<bool>,
},
/// Only the `one_picture_only_constraint_flag` is present because `profile_idc` is 2 or `general_profile_compatibility_flag[2]` is `true`.
Main10Profile {
/// Semantics specified in ISO/IEC 23008-2 - Annex A.
one_picture_only_constraint_flag: bool,
},
/// No additional flags are present.
None,
}
impl ProfileAdditionalFlags {
fn merge(self, defaults: &Self) -> Self {
match (&self, defaults) {
(Self::Full { .. }, _) => self,
(
Self::Main10Profile {
one_picture_only_constraint_flag,
},
Self::Full {
max_12bit_constraint_flag,
max_10bit_constraint_flag,
max_8bit_constraint_flag,
max_422chroma_constraint_flag,
max_420chroma_constraint_flag,
max_monochrome_constraint_flag,
intra_constraint_flag,
lower_bit_rate_constraint_flag,
max_14bit_constraint_flag,
..
},
) => Self::Full {
max_12bit_constraint_flag: *max_12bit_constraint_flag,
max_10bit_constraint_flag: *max_10bit_constraint_flag,
max_8bit_constraint_flag: *max_8bit_constraint_flag,
max_422chroma_constraint_flag: *max_422chroma_constraint_flag,
max_420chroma_constraint_flag: *max_420chroma_constraint_flag,
max_monochrome_constraint_flag: *max_monochrome_constraint_flag,
intra_constraint_flag: *intra_constraint_flag,
one_picture_only_constraint_flag: *one_picture_only_constraint_flag,
lower_bit_rate_constraint_flag: *lower_bit_rate_constraint_flag,
max_14bit_constraint_flag: *max_14bit_constraint_flag,
},
(Self::Main10Profile { .. }, _) => self,
(Self::None, _) => defaults.clone(),
}
}
}