forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

rext_generated.rs115 lines, 3.8 KiBHistoryRawDownload
1//! Synthetic range extension streams (4:0:0, 4:2:2 and 4:4:4 at 8 to 12 bits, transform skip,
2//! lossless, wavefronts and slices, made by `scripts/gen-hevc-rext-fixtures.sh`) against the MD5
3//! of ffmpeg's decode, stored beside each stream. No external tool is needed at test time.
4
5use std::path::Path;
6
7use ec_h265::Decoder;
8use ec_h265_syntax::md5::Md5;
9
10fn hex(d: [u8; 16]) -> String {
11 d.iter().map(|b| format!("{b:02x}")).collect()
12}
13
14/// The MD5 of all output pictures, cropped to the conformance window, as planar samples:
15/// one byte per sample at 8 bits, two (little endian) above.
16fn output_md5(data: &[u8]) -> Result<(String, usize), String> {
17 let mut dec = Decoder::new();
18 dec.decode_annex_b(data)
19 .map_err(|e| format!("decode: {e}"))?;
20 dec.flush().map_err(|e| format!("flush: {e}"))?;
21 let mut md5 = Md5::new();
22 let mut frames = 0;
23 while let Some(f) = dec.next_frame() {
24 let p = &*f.picture;
25 let (sx, sy) = p.chroma_shifts();
26 let (cl, cr, ct, cb) = (
27 f.window.left as usize,
28 f.window.right as usize,
29 f.window.top as usize,
30 f.window.bottom as usize,
31 );
32 let mono = p.chroma_format_idc == 0;
33 let (wx, wy) = if mono {
34 (1, 1)
35 } else {
36 (1usize << sx, 1usize << sy)
37 };
38 let (cw, _) = p.chroma_dims();
39 let planes: [(&Vec<u16>, usize, [usize; 4]); 3] = [
40 (
41 &p.y,
42 p.width,
43 [wx * cl, p.width - wx * cr, wy * ct, p.height - wy * cb],
44 ),
45 (
46 &p.cb,
47 cw,
48 [
49 cl,
50 cw.saturating_sub(cr),
51 ct,
52 p.chroma_dims().1.saturating_sub(cb),
53 ],
54 ),
55 (
56 &p.cr,
57 cw,
58 [
59 cl,
60 cw.saturating_sub(cr),
61 ct,
62 p.chroma_dims().1.saturating_sub(cb),
63 ],
64 ),
65 ];
66 for (c, (plane, stride, [x0, x1, y0, y1])) in planes.into_iter().enumerate() {
67 if plane.is_empty() {
68 continue;
69 }
70 let wide = p.bit_depth_of(c) > 8;
71 let mut bytes = Vec::new();
72 for y in y0..y1 {
73 for x in x0..x1 {
74 let v = plane[y * stride + x];
75 if wide {
76 bytes.extend_from_slice(&v.to_le_bytes());
77 } else {
78 bytes.push(v as u8);
79 }
80 }
81 }
82 md5.update(&bytes);
83 }
84 frames += 1;
85 }
86 Ok((hex(md5.finish()), frames))
87}
88
89#[test]
90fn generated_rext_streams_match_ffmpeg() {
91 let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/rext");
92 let mut names: Vec<_> = std::fs::read_dir(&dir)
93 .expect("tests/data/rext")
94 .flatten()
95 .map(|e| e.path())
96 .filter(|p| p.extension().is_some_and(|x| x == "265"))
97 .collect();
98 names.sort();
99 assert!(
100 names.len() >= 20,
101 "expected the generated streams in {dir:?}"
102 );
103 let mut bad = Vec::new();
104 for path in &names {
105 let data = std::fs::read(path).unwrap();
106 let want = std::fs::read_to_string(path.with_extension("md5")).unwrap();
107 let name = path.file_name().unwrap().to_string_lossy().into_owned();
108 match output_md5(&data) {
109 Ok((got, frames)) if frames > 0 && got == want.trim() => {}
110 Ok((_, frames)) => bad.push(format!("{name}: {frames} frames, md5 differs")),
111 Err(e) => bad.push(format!("{name}: {e}")),
112 }
113 }
114 assert!(bad.is_empty(), "streams that differ from ffmpeg: {bad:?}");
115}