| 1 | use ec_vk::{Gpu, H264Encoder, H264EncoderConfig, H264Profile, RateControl}; |
| 2 | |
| 3 | const W: u32 = 1280; |
| 4 | const H: u32 = 720; |
| 5 | const FRAMES: u32 = 60; |
| 6 | |
| 7 | fn 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 | |
| 20 | fn main() { |
| 21 | let path = std::env::args() |
| 22 | .nth(1) |
| 23 | .expect("usage: h264_encode <out.h264>"); |
| 24 | let gpu = Gpu::open().expect("open Vulkan Video device"); |
| 25 | let cfg = H264EncoderConfig { |
| 26 | width: W, |
| 27 | height: H, |
| 28 | fps: (30, 1), |
| 29 | level: 62, |
| 30 | gop_size: 30, |
| 31 | profile: H264Profile::High, |
| 32 | colour: None, |
| 33 | quality: 0, |
| 34 | rate: match std::env::args().nth(2) { |
| 35 | Some(b) => RateControl::Cbr { |
| 36 | bitrate: b.parse().expect("bitrate"), |
| 37 | buffer_ms: 1000, |
| 38 | }, |
| 39 | None => RateControl::ConstantQp { qp: 26 }, |
| 40 | }, |
| 41 | }; |
| 42 | let mut encoder = H264Encoder::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 | } |