forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

hw_caps.rs102 lines, 2.8 KiBHistoryRawDownload
1//! Lists hardware capabilities, then encodes a gradient with each codec and decodes the Vulkan streams back.
2//!
3//! ```text
4//! cargo run -p ec-hw --example hw_caps
5//! ```
6
7use ec_hw::{
8 Backend, Codec, EncCodec, EncoderConfig, FrameMetadata, HwDecoder, HwEncoder, I420,
9 RateControlMode, capabilities,
10};
11
12const FRAMES: u32 = 30;
13const WIDTH: u32 = 1280;
14const HEIGHT: u32 = 720;
15
16fn main() {
17 for cap in capabilities() {
18 println!(
19 "{:?} {:?} {:?} {:?} max {}x{}",
20 cap.backend, cap.direction, cap.codec, cap.profile, cap.max_width, cap.max_height
21 );
22 }
23 for codec in [EncCodec::H264, EncCodec::H265, EncCodec::Av1] {
24 if let Err(e) = run(codec) {
25 println!("{codec:?}: error: {e}");
26 }
27 }
28}
29
30fn run(codec: EncCodec) -> ec_hw::Result<()> {
31 let config = EncoderConfig {
32 bitrate: 2_000_000,
33 rate_control: RateControlMode::ConstantBitrate,
34 ..EncoderConfig::new(codec, WIDTH, HEIGHT)
35 };
36 let mut encoder = match HwEncoder::open(config, None) {
37 Ok(encoder) => encoder,
38 Err(e) => {
39 println!("{codec:?}: no encoder: {e}");
40 return Ok(());
41 }
42 };
43 let backend = encoder.backend();
44 let mut coded = Vec::with_capacity(FRAMES as usize);
45 for index in 0..FRAMES {
46 let meta = FrameMetadata {
47 timestamp: index as i64,
48 force_keyframe: index == 0,
49 };
50 coded.push(encoder.encode(&gray_gradient(index), meta)?);
51 }
52 let bytes: usize = coded.iter().map(|c| c.data.len()).sum();
53 println!("{codec:?} {backend:?} {FRAMES} frames {bytes} bytes");
54
55 if backend != Backend::Vk {
56 return Ok(());
57 }
58 let mut decoder = match HwDecoder::open(decode_codec(codec)) {
59 Ok(decoder) => decoder,
60 Err(e) => {
61 println!(" decode unavailable: {e}");
62 return Ok(());
63 }
64 };
65 let mut frames = 0;
66 for c in &coded {
67 decoder.decode(&c.data, c.timestamp)?;
68 while decoder.next_frame()?.is_some() {
69 frames += 1;
70 }
71 }
72 decoder.flush()?;
73 while decoder.next_frame()?.is_some() {
74 frames += 1;
75 }
76 println!(" decoded {frames} frames");
77 Ok(())
78}
79
80fn decode_codec(codec: EncCodec) -> Codec {
81 match codec {
82 EncCodec::H264 => Codec::H264,
83 EncCodec::H265 => Codec::H265,
84 EncCodec::Av1 => Codec::Av1,
85 }
86}
87
88fn gray_gradient(index: u32) -> I420 {
89 let (w, h) = (WIDTH as usize, HEIGHT as usize);
90 let shift = 4 * index as usize;
91 let y = (0..h)
92 .flat_map(|row| (0..w).map(move |col| ((col + row + shift) & 0xff) as u8))
93 .collect();
94 let chroma = w.div_ceil(2) * h.div_ceil(2);
95 I420 {
96 y,
97 u: vec![128; chroma],
98 v: vec![128; chroma],
99 width: WIDTH,
100 height: HEIGHT,
101 }
102}