forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB03dc6e9Tahinli3 months ago178 commits

vui.rs295 lines, 10.3 KiBHistoryRawDownload
1//! VUI parameters — the part of an SPS that says what the samples *mean*.
2
3use ec_core::bitio::{BitReader, BitWriter};
4use ec_core::color::ColorDescription;
5use ec_core::error::Result;
6
7/// `colour_primaries`, `transfer_characteristics`, `matrix_coeffs` (H.273 code
8/// points), carried verbatim.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
10pub struct ColourDescription {
11 /// `colour_primaries`: 1 = BT.709, 9 = BT.2020.
12 pub colour_primaries: u8,
13 /// `transfer_characteristics`: 1 = BT.709, 16 = PQ, 18 = HLG.
14 pub transfer_characteristics: u8,
15 /// `matrix_coeffs`: 1 = BT.709, 9 = BT.2020 non-constant luminance.
16 pub matrix_coeffs: u8,
17}
18
19impl ColourDescription {
20 /// The three code points a [`ColorDescription`] resolves to.
21 ///
22 /// This is the bridge that keeps the bitstream and the container from
23 /// drifting apart: both sides come from the same H.273 resolution in
24 /// [`ec_core::color`].
25 pub fn from_color(color: ColorDescription) -> ColourDescription {
26 let (primaries, transfer, matrix) = color.codes();
27 ColourDescription {
28 colour_primaries: primaries as u8,
29 transfer_characteristics: transfer as u8,
30 matrix_coeffs: matrix as u8,
31 }
32 }
33}
34
35/// `video_signal_type` and the colour description under it.
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub struct VideoSignalType {
38 /// The analogue system a picture came off; 5 ("unspecified") for anything
39 /// this family encodes, because none of it did.
40 pub video_format: u8,
41 /// False = limited range (16..235), true = full range.
42 pub video_full_range_flag: bool,
43 /// The H.273 code points, when stated.
44 pub colour_description: Option<ColourDescription>,
45}
46
47impl Default for VideoSignalType {
48 fn default() -> Self {
49 VideoSignalType {
50 video_format: 5,
51 video_full_range_flag: false,
52 colour_description: None,
53 }
54 }
55}
56
57/// The VUI fields this family writes and reads (E.2.1).
58///
59/// Everything absent here is written as "not present"; an HRD block in
60/// particular is deliberately never written, since nothing downstream reads one
61/// and a wrong one is worse than none.
62#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
63pub struct VuiParameters {
64 /// Sample aspect ratio as `aspect_ratio_idc`, or `Some((w, h))` for the
65 /// extended form.
66 pub sample_aspect_ratio: Option<(u16, u16)>,
67 /// What the samples mean.
68 pub video_signal_type: Option<VideoSignalType>,
69 /// `(num_units_in_tick, time_scale)`: a rational frame rate, exact.
70 pub timing: Option<(u32, u32)>,
71}
72
73impl VuiParameters {
74 /// Write `vui_parameters()`.
75 pub fn write(&self, w: &mut BitWriter) {
76 match self.sample_aspect_ratio {
77 Some((sw, sh)) => {
78 w.write_bit(true);
79 w.write_bits(255, 8); // EXTENDED_SAR
80 w.write_bits(u32::from(sw), 16);
81 w.write_bits(u32::from(sh), 16);
82 }
83 None => w.write_bit(false),
84 }
85 w.write_bit(false); // overscan_info_present_flag
86 match self.video_signal_type {
87 Some(vst) => {
88 w.write_bit(true);
89 w.write_bits(u32::from(vst.video_format), 3);
90 w.write_bit(vst.video_full_range_flag);
91 match vst.colour_description {
92 Some(cd) => {
93 w.write_bit(true);
94 w.write_bits(u32::from(cd.colour_primaries), 8);
95 w.write_bits(u32::from(cd.transfer_characteristics), 8);
96 w.write_bits(u32::from(cd.matrix_coeffs), 8);
97 }
98 None => w.write_bit(false),
99 }
100 }
101 None => w.write_bit(false),
102 }
103 w.write_bit(false); // chroma_loc_info_present_flag
104 w.write_bit(false); // neutral_chroma_indication_flag
105 w.write_bit(false); // field_seq_flag
106 w.write_bit(false); // frame_field_info_present_flag
107 w.write_bit(false); // default_display_window_flag
108 match self.timing {
109 Some((units, scale)) => {
110 w.write_bit(true);
111 w.write_bits(units, 32);
112 w.write_bits(scale, 32);
113 w.write_bit(false); // vui_poc_proportional_to_timing_flag
114 w.write_bit(false); // vui_hrd_parameters_present_flag
115 }
116 None => w.write_bit(false),
117 }
118 w.write_bit(false); // bitstream_restriction_flag
119 }
120
121 /// Parse `vui_parameters()`, skipping past the parts not modelled.
122 pub fn parse(r: &mut BitReader, max_sub_layers_minus1: u32) -> Result<VuiParameters> {
123 let mut vui = VuiParameters::default();
124 if r.read_bit()? {
125 let idc = r.read_bits(8)?;
126 if idc == 255 {
127 let sw = r.read_bits(16)? as u16;
128 let sh = r.read_bits(16)? as u16;
129 vui.sample_aspect_ratio = Some((sw, sh));
130 }
131 }
132 if r.read_bit()? {
133 r.read_bit()?; // overscan_appropriate_flag
134 }
135 if r.read_bit()? {
136 let video_format = r.read_bits(3)? as u8;
137 let video_full_range_flag = r.read_bit()?;
138 let colour_description = if r.read_bit()? {
139 Some(ColourDescription {
140 colour_primaries: r.read_bits(8)? as u8,
141 transfer_characteristics: r.read_bits(8)? as u8,
142 matrix_coeffs: r.read_bits(8)? as u8,
143 })
144 } else {
145 None
146 };
147 vui.video_signal_type = Some(VideoSignalType {
148 video_format,
149 video_full_range_flag,
150 colour_description,
151 });
152 }
153 if r.read_bit()? {
154 r.read_ue()?;
155 r.read_ue()?;
156 }
157 r.read_bit()?; // neutral_chroma_indication_flag
158 r.read_bit()?; // field_seq_flag
159 r.read_bit()?; // frame_field_info_present_flag
160 if r.read_bit()? {
161 for _ in 0..4 {
162 r.read_ue()?;
163 }
164 }
165 if r.read_bit()? {
166 let units = r.read_bits(32)?;
167 let scale = r.read_bits(32)?;
168 vui.timing = Some((units, scale));
169 if r.read_bit()? {
170 r.read_ue()?;
171 }
172 if r.read_bit()? {
173 skip_hrd_parameters(r, true, max_sub_layers_minus1)?;
174 }
175 }
176 if r.read_bit()? {
177 r.read_bit()?;
178 r.read_bit()?;
179 r.read_bit()?;
180 for _ in 0..5 {
181 r.read_ue()?;
182 }
183 }
184 Ok(vui)
185 }
186}
187
188/// Walk past an `hrd_parameters()` block (E.2.2) without modelling it.
189fn skip_hrd_parameters(
190 r: &mut BitReader,
191 common_inf_present: bool,
192 max_sub_layers_minus1: u32,
193) -> Result<()> {
194 let mut nal_hrd = false;
195 let mut vcl_hrd = false;
196 let mut sub_pic_hrd = false;
197 if common_inf_present {
198 nal_hrd = r.read_bit()?;
199 vcl_hrd = r.read_bit()?;
200 if nal_hrd || vcl_hrd {
201 sub_pic_hrd = r.read_bit()?;
202 if sub_pic_hrd {
203 r.read_bits(8)?; // tick_divisor_minus2
204 r.read_bits(5)?; // du_cpb_removal_delay_increment_length_minus1
205 r.read_bit()?; // sub_pic_cpb_params_in_pic_timing_sei_flag
206 r.read_bits(5)?; // dpb_output_delay_du_length_minus1
207 }
208 r.read_bits(4)?; // bit_rate_scale
209 r.read_bits(4)?; // cpb_size_scale
210 if sub_pic_hrd {
211 r.read_bits(4)?; // cpb_size_du_scale
212 }
213 r.read_bits(5)?; // initial_cpb_removal_delay_length_minus1
214 r.read_bits(5)?; // au_cpb_removal_delay_length_minus1
215 r.read_bits(5)?; // dpb_output_delay_length_minus1
216 }
217 }
218 for _ in 0..=max_sub_layers_minus1 {
219 let fixed_pic_rate_general = r.read_bit()?;
220 let mut fixed_pic_rate_within_cvs = fixed_pic_rate_general;
221 if !fixed_pic_rate_general {
222 fixed_pic_rate_within_cvs = r.read_bit()?;
223 }
224 let mut low_delay = false;
225 if fixed_pic_rate_within_cvs {
226 r.read_ue()?; // elemental_duration_in_tc_minus1
227 } else {
228 low_delay = r.read_bit()?;
229 }
230 let mut cpb_cnt = 1u32;
231 if !low_delay {
232 cpb_cnt = r.read_ue()? + 1;
233 }
234 for present in [nal_hrd, vcl_hrd] {
235 if !present {
236 continue;
237 }
238 for _ in 0..cpb_cnt {
239 r.read_ue()?; // bit_rate_value_minus1
240 r.read_ue()?; // cpb_size_value_minus1
241 if sub_pic_hrd {
242 r.read_ue()?; // cpb_size_du_value_minus1
243 r.read_ue()?; // bit_rate_du_value_minus1
244 }
245 r.read_bit()?; // cbr_flag
246 }
247 }
248 }
249 Ok(())
250}
251
252#[cfg(test)]
253mod tests {
254 use super::*;
255
256 #[test]
257 fn vui_round_trips() {
258 let vui = VuiParameters {
259 sample_aspect_ratio: Some((1, 1)),
260 video_signal_type: Some(VideoSignalType {
261 video_format: 5,
262 video_full_range_flag: false,
263 colour_description: Some(ColourDescription {
264 colour_primaries: 1,
265 transfer_characteristics: 1,
266 matrix_coeffs: 1,
267 }),
268 }),
269 timing: Some((1001, 60_000)),
270 };
271 let mut w = BitWriter::new();
272 vui.write(&mut w);
273 w.align_to_byte();
274 let bytes = w.into_bytes();
275 let mut r = BitReader::new(&bytes);
276 assert_eq!(VuiParameters::parse(&mut r, 0).unwrap(), vui);
277
278 // Every export this family writes declares itself SDR: 709 above 720
279 // lines and 601 below, which is the rule ec-core states, not a guess
280 // made here.
281 for height in [1080, 2160] {
282 let cd = ColourDescription::from_color(ColorDescription::output(height));
283 assert_eq!(
284 (
285 cd.colour_primaries,
286 cd.transfer_characteristics,
287 cd.matrix_coeffs
288 ),
289 (1, 1, 1)
290 );
291 }
292 let cd = ColourDescription::from_color(ColorDescription::output(480));
293 assert_eq!((cd.colour_primaries, cd.matrix_coeffs), (6, 6));
294 }
295}