forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

h265_encode.rs55 lines, 1.7 KiBHistoryRawDownload
1use ec_vk::{Gpu, H265Encoder, H265EncoderConfig, RateControl};
2
3const W: u32 = 1280;
4const H: u32 = 720;
5const FRAMES: u32 = 60;
6
7fn frame(n: u32) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
8 let y = (0..H)
9 .flat_map(|row| (0..W).map(move |col| ((col + row + 4 * n) % 256) as u8))
10 .collect();
11 let u = (0..H / 2)
12 .flat_map(|_| (0..W / 2).map(move |col| ((col * 2 + n) % 256) as u8))
13 .collect();
14 let v = (0..H / 2)
15 .flat_map(|row| (0..W / 2).map(move |_| ((row * 2 + 2 * n) % 256) as u8))
16 .collect();
17 (y, u, v)
18}
19
20fn main() {
21 let path = std::env::args()
22 .nth(1)
23 .expect("usage: h265_encode <out.hevc> [cbr_bitrate]");
24 let gpu = Gpu::open().expect("open Vulkan Video device");
25 let cfg = H265EncoderConfig {
26 width: W,
27 height: H,
28 fps: (30, 1),
29 level: 186,
30 gop_size: 30,
31 rate: match std::env::args().nth(2) {
32 Some(b) => RateControl::Cbr {
33 bitrate: b.parse().expect("bitrate"),
34 buffer_ms: 1000,
35 },
36 None => RateControl::ConstantQp { qp: 26 },
37 },
38 bit_depth: 8,
39 colour: None,
40 quality: 0,
41 };
42 let mut encoder = H265Encoder::new(&gpu, cfg).expect("create encoder");
43 let mut out = Vec::new();
44 for n in 0..FRAMES {
45 let (y, u, v) = frame(n);
46 let coded = encoder.encode(&y, &u, &v, false).expect("encode frame");
47 println!(
48 "frame {n}: {} bytes keyframe {}",
49 coded.data.len(),
50 coded.keyframe
51 );
52 out.extend_from_slice(&coded.data);
53 }
54 std::fs::write(&path, out).expect("write output");
55}