forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

refusals.rs287 lines, 10.3 KiBHistoryRawDownload
1//! Out-of-scope HEVC streams are refused by name: `Error::Unsupported` whose `what` names the
2//! feature. Never a panic, never `Corrupt`, never pixels.
3//!
4//! The streams in `data/refusals/` are 64x64, four frames each, made with ffmpeg and libx265
5//! (`-pix_fmt` as in the file name, `-profile:v main444-8` for `main444_8.265`). The SCC, RExt,
6//! multi-layer and 3D cases are the 8-bit 4:2:0 control stream with one parameter set's
7//! extension flags set (the writer emits the flags, not the extension bodies, which the
8//! decoder never reads), so each case isolates one feature.
9//!
10//! Multi-layer policy: NAL units with `nuh_layer_id > 0` are skipped, so a multi-layer stream
11//! decodes its base layer. That is only refused when a base-layer SPS or PPS itself carries the
12//! multilayer extension flag.
13
14use ec_core::error::Error;
15use ec_h265::Decoder;
16use ec_h265::decode::DecodedPicture;
17use ec_h265_syntax::nal::{split_annex_b, write_annex_b};
18use ec_h265_syntax::ps::{Pps, PpsRangeExt, Sps, SpsRangeExt};
19
20const MAIN_8: &[u8] = include_bytes!("../data/refusals/main420_8.265");
21const MAIN_10: &[u8] = include_bytes!("../data/refusals/main10_420.265");
22const YUV444_8: &[u8] = include_bytes!("../data/refusals/yuv444_8.265");
23const MAIN444_8: &[u8] = include_bytes!("../data/refusals/main444_8.265");
24const YUV420_12: &[u8] = include_bytes!("../data/refusals/yuv420_12.265");
25const GRAY: &[u8] = include_bytes!("../data/refusals/gray_400.265");
26
27const NAL_SPS: u8 = 33;
28const NAL_PPS: u8 = 34;
29
30/// Decode a whole Annex-B stream and return the output pictures in order.
31fn decode_all(data: &[u8]) -> Result<Vec<DecodedPicture>, Error> {
32 let mut dec = Decoder::new();
33 dec.set_threads(1);
34 dec.decode_annex_b(data)?;
35 dec.flush()?;
36 let mut pictures = Vec::new();
37 while let Some(frame) = dec.next_frame() {
38 pictures.push((*frame.picture).clone());
39 }
40 Ok(pictures)
41}
42
43/// Assert that `data` is refused with `Error::Unsupported` and that the refused feature
44/// (the `what` field) contains `feature`.
45fn assert_refused(name: &str, data: &[u8], feature: &str) {
46 match decode_all(data) {
47 Err(Error::Unsupported { what, why }) => {
48 assert!(
49 what.contains(feature),
50 "{name}: refusal does not name {feature:?}: what = {what:?}, why = {why:?}"
51 );
52 }
53 Err(other) => {
54 panic!("{name}: expected Error::Unsupported naming {feature:?}, got {other:?}")
55 }
56 Ok(pictures) => panic!(
57 "{name}: expected a refusal naming {feature:?}, decoded {} pictures",
58 pictures.len()
59 ),
60 }
61}
62
63/// Rewrite every NAL unit of `data` with `edit` applied to each unit of type `nal_type`.
64/// `edit` receives the unit's RBSP and returns the replacement RBSP.
65fn rewrite_nals(data: &[u8], nal_type: u8, edit: impl Fn(&[u8]) -> Vec<u8>) -> Vec<u8> {
66 let mut out = Vec::new();
67 for nal in split_annex_b(data) {
68 let rbsp = nal.rbsp();
69 let rbsp = if nal.header.nal_type.code() == nal_type {
70 edit(&rbsp)
71 } else {
72 rbsp
73 };
74 write_annex_b(&mut out, nal.header, &rbsp, true);
75 }
76 out
77}
78
79/// The control stream with `flags` as its SPS extension flags (packed as `Sps::extension_flags`).
80fn with_sps_flags(data: &[u8], flags: u8) -> Vec<u8> {
81 rewrite_nals(data, NAL_SPS, |rbsp| {
82 let mut sps = Sps::parse(rbsp).expect("control SPS parses");
83 sps.extension_flags = flags;
84 sps.to_rbsp()
85 })
86}
87
88/// The control stream with `flags` as its PPS extension flags.
89fn with_pps_flags(data: &[u8], flags: u8) -> Vec<u8> {
90 rewrite_nals(data, NAL_PPS, |rbsp| {
91 let mut pps = Pps::parse(rbsp).expect("control PPS parses");
92 pps.extension_flags = flags;
93 pps.to_rbsp()
94 })
95}
96
97/// The control stream with an SPS range extension whose flags `edit` sets.
98fn with_sps_range(data: &[u8], edit: impl Fn(&mut SpsRangeExt)) -> Vec<u8> {
99 rewrite_nals(data, NAL_SPS, |rbsp| {
100 let mut sps = Sps::parse(rbsp).expect("control SPS parses");
101 sps.extension_flags = 0x80;
102 edit(&mut sps.range);
103 sps.to_rbsp()
104 })
105}
106
107/// The control stream with a PPS range extension whose fields `edit` sets.
108fn with_pps_range(data: &[u8], edit: impl Fn(&mut PpsRangeExt)) -> Vec<u8> {
109 rewrite_nals(data, NAL_PPS, |rbsp| {
110 let mut pps = Pps::parse(rbsp).expect("control PPS parses");
111 pps.extension_flags = 0x80;
112 edit(&mut pps.range);
113 pps.to_rbsp()
114 })
115}
116
117#[test]
118fn control_streams_decode() {
119 for (name, data) in [("main 8-bit", MAIN_8), ("main 10-bit", MAIN_10)] {
120 let pictures = decode_all(data).unwrap_or_else(|e| panic!("{name}: {e:?}"));
121 assert_eq!(pictures.len(), 4, "{name}: picture count");
122 }
123 // The re-written control streams (no extension flags) must still decode: this checks that
124 // the flag-setting rewrite used by the synthetic cases is faithful.
125 let rewritten = with_sps_flags(MAIN_8, 0);
126 assert_eq!(
127 decode_all(&rewritten).expect("rewritten SPS decodes").len(),
128 4
129 );
130 let rewritten = with_pps_flags(MAIN_8, 0);
131 assert_eq!(
132 decode_all(&rewritten).expect("rewritten PPS decodes").len(),
133 4
134 );
135}
136
137#[test]
138fn range_extension_formats_decode() {
139 // 4:4:4, 4:0:0 and 12-bit streams are decoded (see tests/it/rext_generated.rs for the
140 // bit-exact checks against ffmpeg); only the planes and depths are checked here.
141 for (name, data, idc, depth, planes) in [
142 ("yuv444p", YUV444_8, 3, 8, 3),
143 ("main444-8", MAIN444_8, 3, 8, 3),
144 ("gray", GRAY, 0, 8, 1),
145 ("yuv420p12le", YUV420_12, 1, 12, 3),
146 ] {
147 let pictures = decode_all(data).unwrap_or_else(|e| panic!("{name}: {e:?}"));
148 assert_eq!(pictures.len(), 4, "{name}: picture count");
149 for p in &pictures {
150 assert_eq!(p.chroma_format_idc, idc, "{name}: chroma_format_idc");
151 assert_eq!(p.bit_depth, depth, "{name}: bit depth");
152 assert_eq!(p.y.len(), p.width * p.height, "{name}: luma plane");
153 let (cw, ch) = p.chroma_dims();
154 let chroma = if planes == 1 { 0 } else { cw * ch };
155 assert_eq!(p.cb.len(), chroma, "{name}: Cb plane");
156 assert_eq!(p.cr.len(), chroma, "{name}: Cr plane");
157 }
158 }
159}
160
161#[test]
162fn sixteen_bit_is_refused_by_name() {
163 let data = rewrite_nals(MAIN_8, NAL_SPS, |rbsp| {
164 let mut sps = Sps::parse(rbsp).expect("control SPS parses");
165 sps.bit_depth_luma_minus8 = 8;
166 sps.bit_depth_chroma_minus8 = 8;
167 sps.to_rbsp()
168 });
169 assert_refused("16-bit", &data, "bit depth 16");
170}
171
172#[test]
173fn separate_colour_planes_are_refused_by_name() {
174 let data = rewrite_nals(MAIN_8, NAL_SPS, |rbsp| {
175 let mut sps = Sps::parse(rbsp).expect("control SPS parses");
176 sps.chroma_format_idc = 3;
177 sps.separate_colour_plane = true;
178 sps.to_rbsp()
179 });
180 assert_refused("separate planes", &data, "separate_colour_plane_flag");
181}
182
183#[test]
184fn sps_extended_precision_is_refused_by_name() {
185 let data = with_sps_range(MAIN_8, |r| r.extended_precision_processing = true);
186 assert_refused("SPS RExt", &data, "extended_precision_processing_flag");
187}
188
189#[test]
190fn sps_cabac_bypass_alignment_is_refused_by_name() {
191 let data = with_sps_range(MAIN_8, |r| r.cabac_bypass_alignment_enabled = true);
192 assert_refused("SPS RExt", &data, "cabac_bypass_alignment_enabled_flag");
193}
194
195#[test]
196fn pps_range_extension_tools_are_accepted() {
197 // Every PPS range extension tool is implemented, so the headers alone are not refused.
198 let data = with_pps_range(MAIN_8, |r| r.log2_sao_offset_scale_luma = 0);
199 decode_all(&data).expect("PPS range extension without tools decodes");
200}
201
202/// Each feature flag written into the control stream, the parameter set that carries it, the
203/// refusal text it must produce, and the old test name it was filed under.
204type FlagCase = (&'static str, fn(&[u8], u8) -> Vec<u8>, u8, &'static str);
205
206const FLAG_CASES: &[FlagCase] = &[
207 (
208 "sps_scc_extension_is_refused_by_name",
209 with_sps_flags,
210 0x10,
211 "SCC",
212 ),
213 (
214 "pps_scc_extension_is_refused_by_name",
215 with_pps_flags,
216 0x10,
217 "SCC",
218 ),
219 (
220 "sps_multilayer_extension_is_refused_by_name",
221 with_sps_flags,
222 0x40,
223 "multi-layer",
224 ),
225 (
226 "pps_multilayer_extension_is_refused_by_name",
227 with_pps_flags,
228 0x40,
229 "multi-layer",
230 ),
231 (
232 "sps_3d_extension_is_refused_by_name",
233 with_sps_flags,
234 0x20,
235 "3D",
236 ),
237 (
238 "pps_3d_extension_is_refused_by_name",
239 with_pps_flags,
240 0x20,
241 "3D",
242 ),
243];
244
245#[test]
246fn flag_extensions_are_refused_by_name() {
247 let mut failures = Vec::new();
248 for &(name, make, flags, feature) in FLAG_CASES {
249 let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
250 assert_refused(name, &make(MAIN_8, flags), feature)
251 }));
252 if outcome.is_err() {
253 failures.push(name);
254 }
255 }
256 assert!(failures.is_empty(), "failed cases: {failures:?}");
257}
258
259/// Layer-1 copies of every slice, and of the SPS with its multilayer flag set, are skipped, so
260/// the base layer decodes to exactly the pictures the plain stream decodes to.
261#[test]
262fn layer_one_nal_units_are_skipped_and_base_layer_decodes() {
263 let base = decode_all(MAIN_8).expect("base decodes");
264 let mut layered = Vec::new();
265 for nal in split_annex_b(MAIN_8) {
266 let rbsp = nal.rbsp();
267 write_annex_b(&mut layered, nal.header, &rbsp, true);
268 let code = nal.header.nal_type.code();
269 let mut header = nal.header;
270 header.layer_id = 1;
271 if code == NAL_SPS {
272 let mut sps = Sps::parse(&rbsp).expect("control SPS parses");
273 sps.extension_flags = 0x40;
274 write_annex_b(&mut layered, header, &sps.to_rbsp(), false);
275 } else if code <= 21 {
276 write_annex_b(&mut layered, header, &rbsp, false);
277 }
278 }
279 let pictures = decode_all(&layered).expect("layer-1 NAL units are skipped, not refused");
280 assert_eq!(pictures.len(), base.len(), "picture count");
281 for (got, want) in pictures.iter().zip(&base) {
282 assert_eq!(
283 (got.y.as_slice(), got.cb.as_slice(), got.cr.as_slice()),
284 (want.y.as_slice(), want.cb.as_slice(), want.cr.as_slice())
285 );
286 }
287}