forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

hw_ten.rs141 lines, 4.1 KiBHistoryRawDownload
1use ec_hw::{
2 Backend, Codec, EncCodec, EncoderConfig, FrameMetadata, HwDecoder, HwEncoder, I420_16,
3 RateControlMode,
4};
5
6const WIDTH: u32 = 1280;
7const HEIGHT: u32 = 720;
8const FRAMES: u32 = 30;
9const PEAK: f64 = 1023.0;
10
11fn main() {
12 let dir = std::env::args().nth(1).unwrap_or_else(|| ".".into());
13 let rates = [
14 ("cqp", RateControlMode::ConstantQp { qp: 30 }),
15 ("cbr", RateControlMode::ConstantBitrate),
16 ];
17 for codec in [EncCodec::H265, EncCodec::Av1] {
18 for (name, rate) in rates {
19 for backend in [Backend::Vk, Backend::Va] {
20 if let Err(e) = run(&dir, codec, name, rate, backend) {
21 println!("{codec:?} {name} {backend:?}: error: {e}");
22 }
23 }
24 }
25 }
26}
27
28fn run(
29 dir: &str,
30 codec: EncCodec,
31 name: &str,
32 rate: RateControlMode,
33 backend: Backend,
34) -> ec_hw::Result<()> {
35 let config = EncoderConfig {
36 bitrate: 4_000_000,
37 rate_control: rate,
38 ..EncoderConfig::new(codec, WIDTH, HEIGHT).ten_bit()
39 };
40 let mut encoder = match HwEncoder::open(config, Some(backend)) {
41 Ok(encoder) => encoder,
42 Err(e) => {
43 println!("{codec:?} {name} {backend:?}: no encoder: {e}");
44 return Ok(());
45 }
46 };
47 let mut coded = Vec::with_capacity(FRAMES as usize);
48 let mut sources = Vec::with_capacity(FRAMES as usize);
49 for index in 0..FRAMES {
50 let frame = pattern(index);
51 let meta = FrameMetadata {
52 timestamp: index as i64,
53 force_keyframe: index == 0,
54 };
55 coded.push(encoder.encode_16(&frame, meta)?);
56 sources.push(frame);
57 }
58 let stream: Vec<u8> = coded.iter().flat_map(|c| c.data.iter().copied()).collect();
59 let stem = format!("{dir}/ten_{codec:?}_{name}_{backend:?}");
60 std::fs::write(format!("{stem}.bin"), &stream).expect("write bitstream");
61
62 let mut decoder = HwDecoder::open_16(decode_codec(codec))?;
63 let mut decoded = Vec::new();
64 for c in &coded {
65 decoder.decode(&c.data, c.timestamp)?;
66 while let Some(frame) = decoder.next_frame_16()? {
67 decoded.push(frame);
68 }
69 }
70 decoder.flush()?;
71 while let Some(frame) = decoder.next_frame_16()? {
72 decoded.push(frame);
73 }
74
75 let mut yuv = Vec::new();
76 for (_, frame) in &decoded {
77 for sample in frame.y.iter().chain(&frame.u).chain(&frame.v) {
78 yuv.extend_from_slice(&sample.to_le_bytes());
79 }
80 }
81 std::fs::write(format!("{stem}_ours.yuv"), &yuv).expect("write decoded yuv");
82
83 let psnr: f64 = decoded
84 .iter()
85 .map(|(ts, frame)| psnr(&sources[*ts as usize], frame))
86 .sum::<f64>()
87 / decoded.len().max(1) as f64;
88 println!(
89 "{codec:?} {name} {backend:?}: {} bytes, decoded {} frames, PSNR {psnr:.2} dB",
90 stream.len(),
91 decoded.len()
92 );
93 Ok(())
94}
95
96fn decode_codec(codec: EncCodec) -> Codec {
97 match codec {
98 EncCodec::H264 => Codec::H264,
99 EncCodec::H265 => Codec::H265,
100 EncCodec::Av1 => Codec::Av1,
101 }
102}
103
104fn pattern(index: u32) -> I420_16 {
105 let t = index as usize;
106 let (w, h) = (WIDTH as usize, HEIGHT as usize);
107 I420_16 {
108 y: plane(w, h, |x, y| x * 3 + y * 5 + t * 16),
109 u: plane(w / 2, h / 2, |x, y| x * 7 + y * 2 + t * 9),
110 v: plane(w / 2, h / 2, |x, y| x * 2 + y * 11 + t * 5 + 512),
111 width: WIDTH,
112 height: HEIGHT,
113 bit_depth: 10,
114 }
115}
116
117fn plane(w: usize, h: usize, value: impl Fn(usize, usize) -> usize) -> Vec<u16> {
118 let mut out = Vec::with_capacity(w * h);
119 for row in 0..h {
120 for col in 0..w {
121 out.push((value(col, row) & 0x3ff) as u16);
122 }
123 }
124 out
125}
126
127fn psnr(a: &I420_16, b: &I420_16) -> f64 {
128 let mut sse = 0f64;
129 let mut n = 0usize;
130 for (x, y) in
131 a.y.iter()
132 .chain(&a.u)
133 .chain(&a.v)
134 .zip(b.y.iter().chain(&b.u).chain(&b.v))
135 {
136 let d = f64::from(*x) - f64::from(*y);
137 sse += d * d;
138 n += 1;
139 }
140 10.0 * (PEAK * PEAK * n as f64 / sse).log10()
141}