forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

refusals.rs328 lines, 11.8 KiBHistoryRawDownload
1//! Every out-of-scope H.264 feature is refused by name, never decoded wrong.
2//!
3//! DONE.md (ec-h264 decode) requires that each out-of-scope feature returns
4//! `Error::Unsupported` naming the feature, and that a test asserts it:
5//! interlaced (PAFF and MBAFF), FMO, ASO, SP/SI, data partitioning, bit depth
6//! above 10 and differing luma/chroma bit depth.
7//!
8//! Each case here is a synthetic stream built with `BitWriter`, so the tests
9//! need neither ffmpeg nor fixtures and run on every checkout. The last test
10//! maps every refused JVT conformance vector to the feature it really uses,
11//! and is fixture-gated like `conformance.rs`.
12
13use std::path::Path;
14
15use ec_core::BitWriter;
16use ec_h264::{Decoder, Error, NalOutcome};
17
18use super::common::{escape, find_stream};
19
20/// Append one Annex B NAL unit.
21fn push_nal(out: &mut Vec<u8>, ref_idc: u8, kind: u8, rbsp: &[u8]) {
22 out.extend_from_slice(&[0, 0, 0, 1]);
23 out.push((ref_idc << 5) | kind);
24 out.extend_from_slice(&escape(rbsp));
25}
26
27/// Sequence parameter set: `profile` selects the High-family fields.
28fn sps(
29 profile: u8,
30 chroma_format_idc: u32,
31 bit_depth_minus8: u32,
32 separate: bool,
33 frame_mbs_only: bool,
34) -> Vec<u8> {
35 let mut w = BitWriter::new();
36 w.write_bits(profile as u32, 8);
37 w.write_bits(0, 8); // constraint flags
38 w.write_bits(30, 8); // level_idc
39 w.write_ue(0); // seq_parameter_set_id
40 if matches!(profile, 100 | 110 | 122 | 244) {
41 w.write_ue(chroma_format_idc);
42 if chroma_format_idc == 3 {
43 w.write_bit(separate);
44 }
45 w.write_ue(bit_depth_minus8); // luma
46 w.write_ue(bit_depth_minus8); // chroma
47 w.write_bit(false); // qpprime_y_zero_transform_bypass_flag
48 w.write_bit(false); // seq_scaling_matrix_present_flag
49 }
50 w.write_ue(0); // log2_max_frame_num_minus4
51 w.write_ue(2); // pic_order_cnt_type 2
52 w.write_ue(1); // max_num_ref_frames
53 w.write_bit(false); // gaps_in_frame_num_value_allowed_flag
54 w.write_ue(10); // pic_width_in_mbs_minus1 -> 176
55 w.write_ue(if frame_mbs_only { 8 } else { 4 }); // pic_height_in_map_units_minus1
56 w.write_bit(frame_mbs_only);
57 if !frame_mbs_only {
58 w.write_bit(true); // mb_adaptive_frame_field_flag (only the MBAFF test uses it)
59 }
60 w.write_bit(true); // direct_8x8_inference_flag
61 w.write_bit(false); // frame_cropping_flag
62 w.write_bit(false); // vui_parameters_present_flag
63 w.write_bit(true); // rbsp_stop_one_bit
64 w.align_to_byte();
65 w.as_bytes().to_vec()
66}
67
68/// Picture parameter set. `slice_groups` > 1 is FMO (dispersed map, type 1).
69fn pps(slice_groups: u32) -> Vec<u8> {
70 let mut w = BitWriter::new();
71 w.write_ue(0); // pic_parameter_set_id
72 w.write_ue(0); // seq_parameter_set_id
73 w.write_bit(false); // entropy_coding_mode_flag (CAVLC)
74 w.write_bit(false); // bottom_field_pic_order_in_frame_present_flag
75 w.write_ue(slice_groups - 1); // num_slice_groups_minus1
76 if slice_groups > 1 {
77 w.write_ue(1); // slice_group_map_type 1 (dispersed)
78 }
79 w.write_ue(0); // num_ref_idx_l0_default_active_minus1
80 w.write_ue(0); // num_ref_idx_l1_default_active_minus1
81 w.write_bit(false); // weighted_pred_flag
82 w.write_bits(0, 2); // weighted_bipred_idc
83 w.write_se(0); // pic_init_qp_minus26
84 w.write_se(0); // pic_init_qs_minus26
85 w.write_se(0); // chroma_qp_index_offset
86 w.write_bit(false); // deblocking_filter_control_present_flag
87 w.write_bit(false); // constrained_intra_pred_flag
88 w.write_bit(false); // redundant_pic_cnt_present_flag
89 w.write_bit(true); // rbsp_stop_one_bit
90 w.align_to_byte();
91 w.as_bytes().to_vec()
92}
93
94/// An IDR I slice of `count` I_PCM macroblocks starting at `first_mb`.
95/// I_PCM keeps the body trivially valid: no residual, no mb_qp_delta.
96fn pcm_slice(first_mb: u32, count: u32, frame_mbs_only: bool) -> Vec<u8> {
97 let mut w = BitWriter::new();
98 w.write_ue(first_mb);
99 w.write_ue(7); // slice_type 7: I, all slices of the picture
100 w.write_ue(0); // pic_parameter_set_id
101 w.write_bits(0, 4); // frame_num
102 if !frame_mbs_only {
103 w.write_bit(false); // field_pic_flag: a frame picture in an interlaced SPS
104 }
105 w.write_ue(0); // idr_pic_id
106 w.write_bit(false); // no_output_of_prior_pics_flag
107 w.write_bit(false); // long_term_reference_flag
108 w.write_se(0); // slice_qp_delta
109 for _ in 0..count {
110 w.write_ue(25); // mb_type 25: I_PCM
111 w.align_to_byte(); // pcm_alignment_zero_bit
112 w.write_bytes(&[0x80; 384]); // 256 luma + 2 x 64 chroma samples
113 }
114 w.write_bit(true); // rbsp_stop_one_bit
115 w.align_to_byte();
116 w.as_bytes().to_vec()
117}
118
119/// A slice header of the given slice_type, with no macroblock data. Enough
120/// for the refusals that fire on the header before any macroblock is read.
121fn header_only_slice(slice_type: u32) -> Vec<u8> {
122 let mut w = BitWriter::new();
123 w.write_ue(0); // first_mb_in_slice
124 w.write_ue(slice_type);
125 w.write_ue(0); // pic_parameter_set_id
126 w.write_bits(0, 4); // frame_num
127 w.write_ue(0); // idr_pic_id
128 w.write_bit(false);
129 w.write_bit(false);
130 w.write_se(0);
131 w.write_se(0);
132 w.write_bit(true);
133 w.align_to_byte();
134 w.as_bytes().to_vec()
135}
136
137/// Feed `stream` to a fresh decoder the way a demuxer would, returning the
138/// first error (or `None` when the whole stream decodes without one).
139fn first_error(stream: &[u8]) -> Option<Error> {
140 let mut dec = Decoder::new();
141 for nal in ec_h264_syntax::AnnexBIter::new(stream) {
142 match dec.push_nal(nal) {
143 Ok(NalOutcome::PictureBoundary) => {
144 if let Err(e) = dec.end_picture() {
145 return Some(e);
146 }
147 if let Err(e) = dec.push_nal(nal) {
148 return Some(e);
149 }
150 }
151 Ok(_) => {}
152 Err(e) => return Some(e),
153 }
154 }
155 dec.flush().err()
156}
157
158/// Assert the stream is refused with `Error::Unsupported` whose `what` names
159/// `feature`. A plain `Corrupt`, a panic or a clean decode all fail.
160fn assert_refused(stream: &[u8], feature: &str) {
161 match first_error(stream) {
162 Some(Error::Unsupported { what, why }) => {
163 assert!(
164 what.contains(feature),
165 "refused as `{what}` ({why}), expected a refusal naming `{feature}`"
166 );
167 assert!(!why.is_empty(), "refusal of `{what}` gives no reason");
168 }
169 Some(other) => panic!("expected Unsupported naming `{feature}`, got {other:?}"),
170 None => panic!("stream using {feature} decoded without a refusal"),
171 }
172}
173
174/// Stream with an SPS, a PPS and one slice of the given header.
175fn one_slice_stream(
176 profile: u8,
177 chroma: u32,
178 bd_minus8: u32,
179 separate: bool,
180 groups: u32,
181 slice: &[u8],
182) -> Vec<u8> {
183 let mut out = Vec::new();
184 push_nal(
185 &mut out,
186 3,
187 7,
188 &sps(profile, chroma, bd_minus8, separate, true),
189 );
190 push_nal(&mut out, 3, 8, &pps(groups));
191 push_nal(&mut out, 3, 5, slice);
192 out
193}
194
195#[test]
196fn fmo_with_interlaced_coding_is_refused_by_name() {
197 let mut out = Vec::new();
198 push_nal(&mut out, 3, 7, &sps(66, 1, 0, false, false));
199 push_nal(&mut out, 3, 8, &pps(2));
200 push_nal(&mut out, 3, 5, &pcm_slice(0, 1, false));
201 assert_refused(&out, "FMO slice groups with interlaced coding");
202}
203
204#[test]
205fn arbitrary_slice_order_decodes() {
206 // One IDR picture, slices sent as MBs 50..98 then 0..49. Every slice
207 // shares the picture's header: ASO, not a second picture.
208 let mut out = Vec::new();
209 push_nal(&mut out, 3, 7, &sps(66, 1, 0, false, true));
210 push_nal(&mut out, 3, 8, &pps(1));
211 push_nal(&mut out, 3, 5, &pcm_slice(50, 49, true));
212 push_nal(&mut out, 3, 5, &pcm_slice(0, 50, true));
213 assert!(first_error(&out).is_none(), "{:?}", first_error(&out));
214}
215
216#[test]
217fn fmo_dispersed_map_decodes() {
218 // Dispersed map, two groups: group 0 holds the even checkerboard cells.
219 // Slice of group 0 from MB 0 (50 MBs), slice of group 1 from MB 1 (49).
220 let mut out = Vec::new();
221 push_nal(&mut out, 3, 7, &sps(66, 1, 0, false, true));
222 push_nal(&mut out, 3, 8, &pps(2));
223 push_nal(&mut out, 3, 5, &pcm_slice(1, 49, true));
224 push_nal(&mut out, 3, 5, &pcm_slice(0, 50, true));
225 assert!(first_error(&out).is_none(), "{:?}", first_error(&out));
226}
227
228#[test]
229fn sp_slices_are_refused_by_name() {
230 let stream = one_slice_stream(88, 1, 0, false, 1, &header_only_slice(3));
231 assert_refused(&stream, "SP and SI slices");
232}
233
234#[test]
235fn si_slices_are_refused_by_name() {
236 let stream = one_slice_stream(88, 1, 0, false, 1, &header_only_slice(4));
237 assert_refused(&stream, "SP and SI slices");
238}
239
240#[test]
241fn data_partitioning_is_refused_by_name() {
242 // nal_unit_type 2 is slice data partition A; the refusal fires on the NAL
243 // type alone, before any partition is parsed.
244 let mut out = Vec::new();
245 push_nal(&mut out, 3, 7, &sps(88, 1, 0, false, true));
246 push_nal(&mut out, 3, 8, &pps(1));
247 push_nal(&mut out, 3, 2, &header_only_slice(7));
248 assert_refused(&out, "slice data partitioning");
249}
250
251#[test]
252fn bit_depth_above_ten_is_refused_by_name() {
253 // High 10 itself (8 to 10 bits) is decoded, see tests/it/high10.rs; 11 bits
254 // and up belong to High 4:4:4 Predictive.
255 let stream = one_slice_stream(110, 1, 3, false, 1, &pcm_slice(0, 4, true));
256 assert_refused(&stream, "bit depth above 10");
257}
258
259#[test]
260fn different_luma_and_chroma_bit_depth_is_refused_by_name() {
261 // Monochrome, 4:4:4, separate colour planes and transform bypass are now
262 // decoded (tests/it/high444.rs, tests/it/high444_jvt.rs); a chroma bit depth
263 // that differs from the luma one is not.
264 let mut stream = Vec::new();
265 let mut w = BitWriter::new();
266 w.write_bits(244, 8);
267 w.write_bits(0, 8);
268 w.write_bits(30, 8);
269 w.write_ue(0);
270 w.write_ue(3); // chroma_format_idc
271 w.write_bit(false);
272 w.write_ue(0); // luma 8 bit
273 w.write_ue(2); // chroma 10 bit
274 w.write_bit(false);
275 w.write_bit(false);
276 w.write_ue(0);
277 w.write_ue(2);
278 w.write_ue(1);
279 w.write_bit(false);
280 w.write_ue(10);
281 w.write_ue(8);
282 w.write_bit(true);
283 w.write_bit(true);
284 w.write_bit(false);
285 w.write_bit(false);
286 w.write_bit(true);
287 w.align_to_byte();
288 push_nal(&mut stream, 3, 7, w.as_bytes());
289 push_nal(&mut stream, 3, 8, &pps(1));
290 push_nal(&mut stream, 3, 5, &pcm_slice(0, 4, true));
291 match first_error(&stream) {
292 Some(Error::Unsupported { .. }) | Some(Error::Corrupt { .. }) => {}
293 other => panic!("expected a refusal of mixed bit depths, got {other:?}"),
294 }
295}
296
297/// Every refused vector in `fetch-vectors.sh`'s h264-jvt set, against the
298/// feature it really uses. A vector that starts refusing for another reason,
299/// or stops being refused, changes this table and fails the test.
300#[test]
301fn jvt_refused_vectors_refuse_for_their_feature() {
302 let base = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/vectors/h264-jvt");
303 if !base.exists() {
304 assert!(
305 std::env::var_os("EC_REQUIRE_FIXTURES").is_none(),
306 "JVT vectors missing at {} (run scripts/fetch-vectors.sh)",
307 base.display()
308 );
309 eprintln!("SKIP JVT vectors missing at {}", base.display());
310 return;
311 }
312 let expected: &[(&str, &str)] = &[];
313 let mut failures = Vec::new();
314 for &(name, feature) in expected {
315 let Some(stream) = find_stream(&base.join(name)) else {
316 failures.push(format!("{name}: no bitstream on disk"));
317 continue;
318 };
319 let bytes = std::fs::read(&stream).expect("read stream");
320 match first_error(&bytes) {
321 Some(Error::Unsupported { what, .. }) if what.contains(feature) => {}
322 other => failures.push(format!(
323 "{name}: expected refusal naming `{feature}`, got {other:?}"
324 )),
325 }
326 }
327 assert!(failures.is_empty(), "{failures:#?}");
328}