forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

cu_syntax.rs139 lines, 5.2 KiBHistoryRawDownload
1//! CU syntax tests over the JCT-VC conformance streams: the P/B slice syntax with the
2//! zero-motion stand-in (the sweep, pixel-pipeline and motion-determinism tests were removed by
3//! testcut Phase 2).
4
5use ec_core::error::Error;
6use ec_h265::dec::Decoder;
7use std::path::{Path, PathBuf};
8
9/// The conformance streams: the first `.bin` or `.bit` bitstream of each directory
10/// under `fixtures/vectors/hevc-jctvc` (or `EC_H265_JCTVC_DIR`), sorted.
11fn streams() -> Vec<PathBuf> {
12 let root = match std::env::var("EC_H265_JCTVC_DIR") {
13 Ok(dir) => PathBuf::from(dir),
14 Err(_) => Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/vectors/hevc-jctvc"),
15 };
16 fn bitstreams_in(dir: &Path, depth: u32, out: &mut Vec<PathBuf>) {
17 let Ok(entries) = std::fs::read_dir(dir) else {
18 return;
19 };
20 let mut paths: Vec<PathBuf> = entries.filter_map(|e| e.ok().map(|e| e.path())).collect();
21 paths.sort();
22 for p in paths {
23 if p.is_dir() && depth > 0 {
24 bitstreams_in(&p, depth - 1, out);
25 } else if matches!(p.extension().and_then(|x| x.to_str()), Some("bin" | "bit")) {
26 out.push(p);
27 }
28 }
29 }
30 let Ok(dirs) = std::fs::read_dir(&root) else {
31 return Vec::new();
32 };
33 let mut dirs: Vec<PathBuf> = dirs.filter_map(|e| e.ok().map(|e| e.path())).collect();
34 dirs.sort();
35 dirs.into_iter()
36 .filter(|d| d.is_dir())
37 .filter_map(|d| {
38 let mut found = Vec::new();
39 bitstreams_in(&d, 2, &mut found);
40 found.into_iter().next()
41 })
42 .collect()
43}
44
45/// Split an Annex-B byte stream into NAL units (header included, start codes and
46/// trailing zero bytes removed).
47fn nal_units(data: &[u8]) -> Vec<&[u8]> {
48 let mut starts = Vec::new();
49 let mut i = 0;
50 while i + 3 <= data.len() {
51 if data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 1 {
52 starts.push(i + 3);
53 i += 3;
54 } else {
55 i += 1;
56 }
57 }
58 let mut out = Vec::new();
59 for (k, &s) in starts.iter().enumerate() {
60 let mut e = starts.get(k + 1).map_or(data.len(), |&next| next - 3);
61 while e > s && data[e - 1] == 0 {
62 e -= 1;
63 }
64 if e > s {
65 out.push(&data[s..e]);
66 }
67 }
68 out
69}
70
71/// Whether a NAL unit is a coded slice segment (types 0 to 9 and 16 to 21).
72fn is_slice(nal: &[u8]) -> bool {
73 let t = (nal[0] >> 1) & 0x3f;
74 t <= 9 || (16..=21).contains(&t)
75}
76
77/// The P/B slice syntax ([`ec_h265::dec::cu_syntax::parse_slice`] with the zero-motion
78/// stand-in, no DPB to derive motion from) over every P and B slice segment of every
79/// stream. Pixel decode is off so the decoder only keeps parameter sets. Refusals are
80/// fine; any other error is a failure.
81#[test]
82fn inter_placeholder_runs_over_pb_slices() {
83 use ec_core::bitio::BitReader;
84 use ec_h265_syntax::nal::{NalUnitType, unescape_rbsp};
85 use ec_h265_syntax::slice::{SliceHeader, SliceType};
86 let streams = streams();
87 if streams.is_empty() {
88 eprintln!("inter placeholder: no conformance streams found, skipping");
89 return;
90 }
91 let (mut pb_slices, mut decoded, mut refused) = (0usize, 0usize, 0usize);
92 for path in &streams {
93 let data = std::fs::read(path).expect("read bitstream");
94 let mut dec = Decoder::new();
95 dec.set_pixel_decode(false);
96 for nal in nal_units(&data) {
97 let code = (nal[0] >> 1) & 0x3f;
98 if is_slice(nal) {
99 let rbsp = unescape_rbsp(&nal[2..]);
100 let mut r = BitReader::new(&rbsp);
101 let _first = r.read_bit();
102 if (16..=21).contains(&code) {
103 let _no_output_of_prior_pics = r.read_bit();
104 }
105 let pps_id = r.read_ue().unwrap_or(u32::MAX);
106 if let Some(pps) = dec.pps(pps_id)
107 && let Some(sps) = dec.sps(pps.sps_id)
108 && let Ok((header, pos)) =
109 SliceHeader::parse(&rbsp, sps, pps, NalUnitType::from_code(code))
110 && header.slice_type != SliceType::I
111 && header.first_slice_segment_in_pic
112 {
113 pb_slices += 1;
114 let off = (pos.header_bits / 8) as usize;
115 let slice_data = rbsp.get(off..).unwrap_or(&[]);
116 let mut zero = ec_h265::dec::cu_syntax::ZeroMotion;
117 match ec_h265::dec::cu_syntax::parse_slice(
118 sps,
119 pps,
120 &header,
121 slice_data,
122 &mut zero,
123 &mut |_| {},
124 ) {
125 Ok(_) => decoded += 1,
126 Err(Error::Unsupported { .. }) => refused += 1,
127 Err(err) => {
128 panic!("{}: P/B placeholder failed: {err}", path.display())
129 }
130 }
131 }
132 }
133 let _ = dec.push_nal(nal);
134 }
135 }
136 println!(
137 "inter syntax: {decoded} P/B slices parsed, {refused} refused by name, of {pb_slices} first-slice P/B segments"
138 );
139}