U16: fold hw_tune, hw_config, hw_ten, hw_rgb, hw_pipeline, hw_speed and hw_caps into one hw_smoke example (caps, CBR tune segments, 8-bit and 10-bit round trips); drop the ffprobe profile compare and timing
Browse files at this commit70cc0ea24e1e99de37cb12683f5fc895c1871396Parent: 502c34a8 changed files +164 -828
- crates/ec-hw/examples/hw_caps.rs +0 -102
- crates/ec-hw/examples/hw_config.rs +0 -144
- crates/ec-hw/examples/hw_pipeline.rs +0 -165
- crates/ec-hw/examples/hw_rgb.rs +0 -148
- crates/ec-hw/examples/hw_smoke.rs +164 -0
- crates/ec-hw/examples/hw_speed.rs +0 -26
- crates/ec-hw/examples/hw_ten.rs +0 -141
- crates/ec-hw/examples/hw_tune.rs +0 -102
crates/ec-hw/examples/hw_caps.rsdeleted+0 -102
| @@ -1,102 +0,0 @@ | |||
| 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 | - | ||
| 7 | -use ec_hw::{ | ||
| 8 | - Backend, Codec, EncCodec, EncoderConfig, FrameMetadata, HwDecoder, HwEncoder, I420, | ||
| 9 | - RateControlMode, capabilities, | ||
| 10 | -}; | ||
| 11 | - | ||
| 12 | -const FRAMES: u32 = 30; | ||
| 13 | -const WIDTH: u32 = 1280; | ||
| 14 | -const HEIGHT: u32 = 720; | ||
| 15 | - | ||
| 16 | -fn 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 | - | ||
| 30 | -fn 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 | - | ||
| 80 | -fn 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 | - | ||
| 88 | -fn 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 | -} | ||
crates/ec-hw/examples/hw_config.rsdeleted+0 -144
| @@ -1,144 +0,0 @@ | |||
| 1 | -use std::fs; | ||
| 2 | -use std::path::{Path, PathBuf}; | ||
| 3 | - | ||
| 4 | -use ec_hw::{ | ||
| 5 | - Backend, EncCodec, EncoderConfig, FrameMetadata, H264Profile, HwEncoder, I420, RateControlMode, | ||
| 6 | -}; | ||
| 7 | - | ||
| 8 | -const WIDTH: u32 = 640; | ||
| 9 | -const HEIGHT: u32 = 360; | ||
| 10 | -const FRAMES: usize = 30; | ||
| 11 | -const FPS: u32 = 30; | ||
| 12 | -const GOP: u32 = 15; | ||
| 13 | -const FORCE_AT: usize = 7; | ||
| 14 | -const BITRATE: u32 = 2_000_000; | ||
| 15 | -const FIELDS: [&str; 7] = [ | ||
| 16 | - "profile", | ||
| 17 | - "width", | ||
| 18 | - "height", | ||
| 19 | - "color_space", | ||
| 20 | - "color_transfer", | ||
| 21 | - "color_primaries", | ||
| 22 | - "color_range", | ||
| 23 | -]; | ||
| 24 | - | ||
| 25 | -fn config(profile: H264Profile) -> EncoderConfig { | ||
| 26 | - EncoderConfig { | ||
| 27 | - bitrate: BITRATE, | ||
| 28 | - framerate: (FPS, 1), | ||
| 29 | - gop_size: GOP, | ||
| 30 | - rate_control: RateControlMode::ConstantBitrate, | ||
| 31 | - ..EncoderConfig::new(EncCodec::H264, WIDTH, HEIGHT) | ||
| 32 | - } | ||
| 33 | - .h264_profile(profile) | ||
| 34 | - .colour(6, 6, 6, true) | ||
| 35 | -} | ||
| 36 | - | ||
| 37 | -fn frame(t: usize) -> I420 { | ||
| 38 | - let (w, h) = (WIDTH as usize, HEIGHT as usize); | ||
| 39 | - let y = (0..w * h) | ||
| 40 | - .map(|i| ((i % w + i / w + 3 * t) & 0xff) as u8) | ||
| 41 | - .collect(); | ||
| 42 | - let chroma = w / 2 * h / 2; | ||
| 43 | - I420 { | ||
| 44 | - y, | ||
| 45 | - u: vec![(96 + t % 64) as u8; chroma], | ||
| 46 | - v: vec![160; chroma], | ||
| 47 | - width: WIDTH, | ||
| 48 | - height: HEIGHT, | ||
| 49 | - } | ||
| 50 | -} | ||
| 51 | - | ||
| 52 | -fn encode(dir: &Path, name: &str, profile: H264Profile, backend: Backend) -> Option<PathBuf> { | ||
| 53 | - let mut encoder = match HwEncoder::open(config(profile), Some(backend)) { | ||
| 54 | - Ok(encoder) => encoder, | ||
| 55 | - Err(e) => { | ||
| 56 | - println!("{name}: {e}"); | ||
| 57 | - return None; | ||
| 58 | - } | ||
| 59 | - }; | ||
| 60 | - let mut stream = Vec::new(); | ||
| 61 | - for t in 0..FRAMES { | ||
| 62 | - let meta = FrameMetadata { | ||
| 63 | - timestamp: t as i64, | ||
| 64 | - force_keyframe: t == FORCE_AT, | ||
| 65 | - }; | ||
| 66 | - let coded = encoder.encode(&frame(t), meta).expect("encode frame"); | ||
| 67 | - stream.extend_from_slice(&coded.data); | ||
| 68 | - } | ||
| 69 | - let path = dir.join(format!("{name}.h264")); | ||
| 70 | - fs::write(&path, stream).expect("write stream"); | ||
| 71 | - Some(path) | ||
| 72 | -} | ||
| 73 | - | ||
| 74 | -fn probe(path: &Path) -> Vec<(String, String)> { | ||
| 75 | - let out = ec_testkit::ffprobe() | ||
| 76 | - .args(["-v", "error", "-select_streams", "v:0", "-show_entries"]) | ||
| 77 | - .arg(format!("stream={}", FIELDS.join(","))) | ||
| 78 | - .args(["-of", "default=nw=1"]) | ||
| 79 | - .arg(path) | ||
| 80 | - .output() | ||
| 81 | - .expect("run ffprobe"); | ||
| 82 | - String::from_utf8_lossy(&out.stdout) | ||
| 83 | - .lines() | ||
| 84 | - .filter_map(|l| l.split_once('=')) | ||
| 85 | - .map(|(k, v)| (k.to_string(), v.to_string())) | ||
| 86 | - .collect() | ||
| 87 | -} | ||
| 88 | - | ||
| 89 | -fn keyframes(path: &Path) -> Vec<usize> { | ||
| 90 | - let out = ec_testkit::ffprobe() | ||
| 91 | - .args([ | ||
| 92 | - "-v", | ||
| 93 | - "error", | ||
| 94 | - "-select_streams", | ||
| 95 | - "v:0", | ||
| 96 | - "-show_entries", | ||
| 97 | - "frame=key_frame", | ||
| 98 | - ]) | ||
| 99 | - .args(["-of", "csv=p=0"]) | ||
| 100 | - .arg(path) | ||
| 101 | - .output() | ||
| 102 | - .expect("run ffprobe"); | ||
| 103 | - String::from_utf8_lossy(&out.stdout) | ||
| 104 | - .lines() | ||
| 105 | - .enumerate() | ||
| 106 | - .filter(|(_, l)| l.trim() == "1") | ||
| 107 | - .map(|(i, _)| i) | ||
| 108 | - .collect() | ||
| 109 | -} | ||
| 110 | - | ||
| 111 | -fn value(info: &[(String, String)], key: &str) -> String { | ||
| 112 | - info.iter() | ||
| 113 | - .find(|(k, _)| k == key) | ||
| 114 | - .map_or("-".to_string(), |(_, v)| v.clone()) | ||
| 115 | -} | ||
| 116 | - | ||
| 117 | -fn main() { | ||
| 118 | - let dir = std::env::temp_dir(); | ||
| 119 | - for (label, profile) in [("main", H264Profile::Main), ("cb", H264Profile::ConstrainedBaseline)] { | ||
| 120 | - compare(&dir, label, profile); | ||
| 121 | - } | ||
| 122 | -} | ||
| 123 | - | ||
| 124 | -fn compare(dir: &Path, label: &str, profile: H264Profile) { | ||
| 125 | - let Some(vk) = encode(dir, &format!("vk_{label}"), profile, Backend::Vk) else { | ||
| 126 | - return; | ||
| 127 | - }; | ||
| 128 | - let Some(va) = encode(dir, &format!("va_{label}"), profile, Backend::Va) else { | ||
| 129 | - return; | ||
| 130 | - }; | ||
| 131 | - let (vk_info, va_info) = (probe(&vk), probe(&va)); | ||
| 132 | - let mut same = true; | ||
| 133 | - println!("{:<16}{:<16}{:<16}", "field", "vk", "va"); | ||
| 134 | - for key in FIELDS { | ||
| 135 | - let (a, b) = (value(&vk_info, key), value(&va_info, key)); | ||
| 136 | - same &= a == b; | ||
| 137 | - println!("{key:<16}{a:<16}{b:<16}"); | ||
| 138 | - } | ||
| 139 | - let (kv, ka) = (keyframes(&vk), keyframes(&va)); | ||
| 140 | - same &= kv == ka; | ||
| 141 | - let list = |k: &[usize]| k.iter().map(usize::to_string).collect::<Vec<_>>().join(","); | ||
| 142 | - println!("{:<16}{:<16}{:<16}", "keyframes", list(&kv), list(&ka)); | ||
| 143 | - println!("{label} match: {same}"); | ||
| 144 | -} | ||
crates/ec-hw/examples/hw_pipeline.rsdeleted+0 -165
| @@ -1,165 +0,0 @@ | |||
| 1 | -use std::time::Instant; | ||
| 2 | - | ||
| 3 | -use ec_hw::{ | ||
| 4 | - Backend, EncCodec, EncodeInput, EncoderConfig, FrameMetadata, HwEncoder, I420, I420_16, | ||
| 5 | - RateControlMode, | ||
| 6 | -}; | ||
| 7 | - | ||
| 8 | -const FRAMES: usize = 120; | ||
| 9 | -const PATTERN: usize = 6; | ||
| 10 | - | ||
| 11 | -struct Case { | ||
| 12 | - codec: EncCodec, | ||
| 13 | - width: u32, | ||
| 14 | - height: u32, | ||
| 15 | - ten: bool, | ||
| 16 | -} | ||
| 17 | - | ||
| 18 | -enum Source { | ||
| 19 | - Eight(Vec<I420>), | ||
| 20 | - Ten(Vec<I420_16>), | ||
| 21 | -} | ||
| 22 | - | ||
| 23 | -fn main() { | ||
| 24 | - let cases = [ | ||
| 25 | - Case { | ||
| 26 | - codec: EncCodec::H265, | ||
| 27 | - width: 3840, | ||
| 28 | - height: 2160, | ||
| 29 | - ten: true, | ||
| 30 | - }, | ||
| 31 | - Case { | ||
| 32 | - codec: EncCodec::H264, | ||
| 33 | - width: 1920, | ||
| 34 | - height: 1080, | ||
| 35 | - ten: false, | ||
| 36 | - }, | ||
| 37 | - Case { | ||
| 38 | - codec: EncCodec::H265, | ||
| 39 | - width: 1920, | ||
| 40 | - height: 1080, | ||
| 41 | - ten: false, | ||
| 42 | - }, | ||
| 43 | - Case { | ||
| 44 | - codec: EncCodec::Av1, | ||
| 45 | - width: 1920, | ||
| 46 | - height: 1080, | ||
| 47 | - ten: false, | ||
| 48 | - }, | ||
| 49 | - ]; | ||
| 50 | - for case in &cases { | ||
| 51 | - let source = if case.ten { | ||
| 52 | - Source::Ten(pattern_16(case)) | ||
| 53 | - } else { | ||
| 54 | - Source::Eight(pattern(case)) | ||
| 55 | - }; | ||
| 56 | - for backend in [Backend::Vk, Backend::Va] { | ||
| 57 | - if let Err(e) = run(case, &source, backend) { | ||
| 58 | - println!( | ||
| 59 | - "{:?} {}x{} {backend:?}: error: {e}", | ||
| 60 | - case.codec, case.width, case.height | ||
| 61 | - ); | ||
| 62 | - } | ||
| 63 | - } | ||
| 64 | - } | ||
| 65 | -} | ||
| 66 | - | ||
| 67 | -fn config(case: &Case) -> EncoderConfig { | ||
| 68 | - let base = EncoderConfig { | ||
| 69 | - rate_control: RateControlMode::ConstantQp { qp: 30 }, | ||
| 70 | - ..EncoderConfig::new(case.codec, case.width, case.height) | ||
| 71 | - }; | ||
| 72 | - if case.ten { | ||
| 73 | - base.ten_bit() | ||
| 74 | - } else { | ||
| 75 | - base | ||
| 76 | - } | ||
| 77 | -} | ||
| 78 | - | ||
| 79 | -fn meta(index: usize) -> FrameMetadata { | ||
| 80 | - FrameMetadata { | ||
| 81 | - timestamp: index as i64, | ||
| 82 | - force_keyframe: index == 0, | ||
| 83 | - } | ||
| 84 | -} | ||
| 85 | - | ||
| 86 | -fn run(case: &Case, source: &Source, backend: Backend) -> ec_hw::Result<()> { | ||
| 87 | - let mut encoder = HwEncoder::open(config(case), Some(backend))?; | ||
| 88 | - let start = Instant::now(); | ||
| 89 | - let mut sync = Vec::with_capacity(FRAMES); | ||
| 90 | - for index in 0..FRAMES { | ||
| 91 | - let coded = match source { | ||
| 92 | - Source::Eight(frames) => encoder.encode(&frames[index % PATTERN], meta(index))?, | ||
| 93 | - Source::Ten(frames) => encoder.encode_16(&frames[index % PATTERN], meta(index))?, | ||
| 94 | - }; | ||
| 95 | - sync.push(coded); | ||
| 96 | - } | ||
| 97 | - let sync_ms = start.elapsed().as_secs_f64() * 1000.0 / FRAMES as f64; | ||
| 98 | - | ||
| 99 | - let mut encoder = HwEncoder::open(config(case), Some(backend))?; | ||
| 100 | - let start = Instant::now(); | ||
| 101 | - let mut piped = Vec::with_capacity(FRAMES); | ||
| 102 | - for index in 0..FRAMES { | ||
| 103 | - let input = match source { | ||
| 104 | - Source::Eight(frames) => EncodeInput::I420(&frames[index % PATTERN]), | ||
| 105 | - Source::Ten(frames) => EncodeInput::I420_16(&frames[index % PATTERN]), | ||
| 106 | - }; | ||
| 107 | - piped.extend(encoder.submit(input, meta(index))?); | ||
| 108 | - } | ||
| 109 | - piped.extend(encoder.flush()?); | ||
| 110 | - let piped_ms = start.elapsed().as_secs_f64() * 1000.0 / FRAMES as f64; | ||
| 111 | - | ||
| 112 | - if matches!(backend, Backend::Vk) { | ||
| 113 | - assert_eq!(sync, piped, "{:?} sync and pipelined bitstreams differ", case.codec); | ||
| 114 | - } | ||
| 115 | - println!( | ||
| 116 | - "{:?} {}x{} {}-bit {backend:?}: sync {sync_ms:.2} ms/frame, pipelined {piped_ms:.2} ms/frame, {} frames", | ||
| 117 | - case.codec, | ||
| 118 | - case.width, | ||
| 119 | - case.height, | ||
| 120 | - if case.ten { 10 } else { 8 }, | ||
| 121 | - piped.len() | ||
| 122 | - ); | ||
| 123 | - Ok(()) | ||
| 124 | -} | ||
| 125 | - | ||
| 126 | -fn pattern(case: &Case) -> Vec<I420> { | ||
| 127 | - let (w, h) = (case.width as usize, case.height as usize); | ||
| 128 | - let (cw, ch) = (w / 2, h / 2); | ||
| 129 | - (0..PATTERN) | ||
| 130 | - .map(|k| I420 { | ||
| 131 | - y: (0..h) | ||
| 132 | - .flat_map(move |row| (0..w).map(move |x| ((x + row + 3 * k) & 0xff) as u8)) | ||
| 133 | - .collect(), | ||
| 134 | - u: (0..ch) | ||
| 135 | - .flat_map(move |row| (0..cw).map(move |x| ((x + 2 * row + 5 * k) & 0xff) as u8)) | ||
| 136 | - .collect(), | ||
| 137 | - v: (0..ch) | ||
| 138 | - .flat_map(move |row| (0..cw).map(move |x| ((2 * x + row + 7 * k) & 0xff) as u8)) | ||
| 139 | - .collect(), | ||
| 140 | - width: case.width, | ||
| 141 | - height: case.height, | ||
| 142 | - }) | ||
| 143 | - .collect() | ||
| 144 | -} | ||
| 145 | - | ||
| 146 | -fn pattern_16(case: &Case) -> Vec<I420_16> { | ||
| 147 | - let (w, h) = (case.width as usize, case.height as usize); | ||
| 148 | - let (cw, ch) = (w / 2, h / 2); | ||
| 149 | - (0..PATTERN) | ||
| 150 | - .map(|k| I420_16 { | ||
| 151 | - y: (0..h) | ||
| 152 | - .flat_map(move |row| (0..w).map(move |x| ((x + row + 3 * k) & 0x3ff) as u16)) | ||
| 153 | - .collect(), | ||
| 154 | - u: (0..ch) | ||
| 155 | - .flat_map(move |row| (0..cw).map(move |x| ((x + 2 * row + 5 * k) & 0x3ff) as u16)) | ||
| 156 | - .collect(), | ||
| 157 | - v: (0..ch) | ||
| 158 | - .flat_map(move |row| (0..cw).map(move |x| ((2 * x + row + 7 * k) & 0x3ff) as u16)) | ||
| 159 | - .collect(), | ||
| 160 | - width: case.width, | ||
| 161 | - height: case.height, | ||
| 162 | - bit_depth: 10, | ||
| 163 | - }) | ||
| 164 | - .collect() | ||
| 165 | -} | ||
crates/ec-hw/examples/hw_rgb.rsdeleted+0 -148
| @@ -1,148 +0,0 @@ | |||
| 1 | -use ec_hw::{ | ||
| 2 | - Backend, Codec, EncCodec, EncoderConfig, FrameMetadata, HwDecoder, HwEncoder, I420, | ||
| 3 | - RateControlMode, RgbFrame, RgbOrder, | ||
| 4 | -}; | ||
| 5 | - | ||
| 6 | -const FRAMES: u32 = 30; | ||
| 7 | - | ||
| 8 | -fn main() { | ||
| 9 | - for codec in [EncCodec::H264, EncCodec::H265, EncCodec::Av1] { | ||
| 10 | - if let Err(e) = run(codec, (1280, 720), (1280, 720)) { | ||
| 11 | - println!("{codec:?} 1280x720: error: {e}"); | ||
| 12 | - } | ||
| 13 | - } | ||
| 14 | - for target in [(1280, 720), (640, 360)] { | ||
| 15 | - if let Err(e) = run(EncCodec::H264, (1920, 1080), target) { | ||
| 16 | - println!("H264 1920x1080 -> {}x{}: error: {e}", target.0, target.1); | ||
| 17 | - } | ||
| 18 | - } | ||
| 19 | -} | ||
| 20 | - | ||
| 21 | -fn run(codec: EncCodec, source: (u32, u32), target: (u32, u32)) -> ec_hw::Result<()> { | ||
| 22 | - let config = EncoderConfig { | ||
| 23 | - bitrate: 4_000_000, | ||
| 24 | - rate_control: RateControlMode::ConstantBitrate, | ||
| 25 | - ..EncoderConfig::new(codec, target.0, target.1) | ||
| 26 | - }; | ||
| 27 | - let mut encoder = match HwEncoder::open(config, std::env::args().nth(1).map(|_| Backend::Va)) { | ||
| 28 | - Ok(encoder) => encoder, | ||
| 29 | - Err(e) => { | ||
| 30 | - println!("{codec:?}: no encoder: {e}"); | ||
| 31 | - return Ok(()); | ||
| 32 | - } | ||
| 33 | - }; | ||
| 34 | - let backend = encoder.backend(); | ||
| 35 | - let mut coded = Vec::with_capacity(FRAMES as usize); | ||
| 36 | - for index in 0..FRAMES { | ||
| 37 | - let pixels = bgra_gradient(index, source); | ||
| 38 | - let frame = RgbFrame { | ||
| 39 | - data: &pixels, | ||
| 40 | - width: source.0, | ||
| 41 | - height: source.1, | ||
| 42 | - stride: source.0 * 4, | ||
| 43 | - order: RgbOrder::Bgra, | ||
| 44 | - }; | ||
| 45 | - let meta = FrameMetadata { | ||
| 46 | - timestamp: index as i64, | ||
| 47 | - force_keyframe: index == 0, | ||
| 48 | - }; | ||
| 49 | - coded.push(encoder.encode_rgb(&frame, meta)?); | ||
| 50 | - } | ||
| 51 | - let bytes: usize = coded.iter().map(|c| c.data.len()).sum(); | ||
| 52 | - println!( | ||
| 53 | - "{codec:?} {backend:?} {}x{} -> {}x{} {FRAMES} frames {bytes} bytes", | ||
| 54 | - source.0, source.1, target.0, target.1 | ||
| 55 | - ); | ||
| 56 | - | ||
| 57 | - let mut decoder = match HwDecoder::open(decode_codec(codec)) { | ||
| 58 | - Ok(decoder) => decoder, | ||
| 59 | - Err(e) => { | ||
| 60 | - println!(" decode unavailable: {e}"); | ||
| 61 | - return Ok(()); | ||
| 62 | - } | ||
| 63 | - }; | ||
| 64 | - let mut decoded = Vec::new(); | ||
| 65 | - for c in &coded { | ||
| 66 | - decoder.decode(&c.data, c.timestamp)?; | ||
| 67 | - while let Some(frame) = decoder.next_frame()? { | ||
| 68 | - decoded.push(frame); | ||
| 69 | - } | ||
| 70 | - } | ||
| 71 | - decoder.flush()?; | ||
| 72 | - while let Some(frame) = decoder.next_frame()? { | ||
| 73 | - decoded.push(frame); | ||
| 74 | - } | ||
| 75 | - | ||
| 76 | - let total: f64 = decoded | ||
| 77 | - .iter() | ||
| 78 | - .map(|(ts, frame)| { | ||
| 79 | - mean_abs_luma(&frame.y, &luma_reference(*ts as u32, source, target)) | ||
| 80 | - }) | ||
| 81 | - .sum(); | ||
| 82 | - let mean = if decoded.is_empty() { | ||
| 83 | - 0.0 | ||
| 84 | - } else { | ||
| 85 | - total / decoded.len() as f64 | ||
| 86 | - }; | ||
| 87 | - println!( | ||
| 88 | - " decoded {} frames, mean |Y diff| {mean:.2}", | ||
| 89 | - decoded.len() | ||
| 90 | - ); | ||
| 91 | - Ok(()) | ||
| 92 | -} | ||
| 93 | - | ||
| 94 | -fn decode_codec(codec: EncCodec) -> Codec { | ||
| 95 | - match codec { | ||
| 96 | - EncCodec::H264 => Codec::H264, | ||
| 97 | - EncCodec::H265 => Codec::H265, | ||
| 98 | - EncCodec::Av1 => Codec::Av1, | ||
| 99 | - } | ||
| 100 | -} | ||
| 101 | - | ||
| 102 | -fn bgra_gradient(index: u32, (w, h): (u32, u32)) -> Vec<u8> { | ||
| 103 | - let shift = index as usize * 4; | ||
| 104 | - let mut out = Vec::with_capacity(w as usize * h as usize * 4); | ||
| 105 | - for y in 0..h as usize { | ||
| 106 | - for x in 0..w as usize { | ||
| 107 | - let r = (x + shift) & 0xff; | ||
| 108 | - let g = (y + 2 * shift) & 0xff; | ||
| 109 | - let b = ((x + y) / 2 + 3 * shift) & 0xff; | ||
| 110 | - out.extend_from_slice(&[b as u8, g as u8, r as u8, 0xff]); | ||
| 111 | - } | ||
| 112 | - } | ||
| 113 | - out | ||
| 114 | -} | ||
| 115 | - | ||
| 116 | -fn luma_reference(index: u32, source: (u32, u32), target: (u32, u32)) -> Vec<u8> { | ||
| 117 | - let (sw, sh) = (source.0 as usize, source.1 as usize); | ||
| 118 | - let (w, h) = (target.0 as usize, target.1 as usize); | ||
| 119 | - let mut scaled = vec![0; w * h * 4]; | ||
| 120 | - ec_dsp::rgb::rgb_box_downscale( | ||
| 121 | - &bgra_gradient(index, source), | ||
| 122 | - sw, | ||
| 123 | - sh, | ||
| 124 | - sw * 4, | ||
| 125 | - &mut scaled, | ||
| 126 | - w, | ||
| 127 | - h, | ||
| 128 | - ); | ||
| 129 | - let chroma = (w / 2) * (h / 2); | ||
| 130 | - let mut frame = I420 { | ||
| 131 | - y: vec![0; w * h], | ||
| 132 | - u: vec![0; chroma], | ||
| 133 | - v: vec![0; chroma], | ||
| 134 | - width: target.0, | ||
| 135 | - height: target.1, | ||
| 136 | - }; | ||
| 137 | - ec_dsp::rgb::bgra_to_i420_709(&scaled, w, h, &mut frame.y, &mut frame.u, &mut frame.v); | ||
| 138 | - frame.y | ||
| 139 | -} | ||
| 140 | - | ||
| 141 | -fn mean_abs_luma(decoded: &[u8], reference: &[u8]) -> f64 { | ||
| 142 | - let sum: u64 = decoded | ||
| 143 | - .iter() | ||
| 144 | - .zip(reference) | ||
| 145 | - .map(|(&a, &b)| u64::from(a.abs_diff(b))) | ||
| 146 | - .sum(); | ||
| 147 | - sum as f64 / reference.len() as f64 | ||
| 148 | -} | ||
crates/ec-hw/examples/hw_smoke.rsadded+164 -0
| @@ -0,0 +1,164 @@ | |||
| 1 | +use ec_hw::{ | ||
| 2 | + Backend, Codec, CodedFrame, EncCodec, EncoderConfig, FrameMetadata, HwDecoder, HwEncoder, HwPicture, I420_16, | ||
| 3 | + RateControlMode, RgbFrame, RgbOrder, Tunings, capabilities, | ||
| 4 | +}; | ||
| 5 | + | ||
| 6 | +const W: u32 = 1280; | ||
| 7 | +const H: u32 = 720; | ||
| 8 | +const FPS: u32 = 30; | ||
| 9 | +const SEGMENT: u32 = 30; | ||
| 10 | +const PEAK: f64 = 1023.0; | ||
| 11 | + | ||
| 12 | +fn main() { | ||
| 13 | + for cap in capabilities() { | ||
| 14 | + println!( | ||
| 15 | + "{:?} {:?} {:?} {:?} max {}x{}", | ||
| 16 | + cap.backend, cap.direction, cap.codec, cap.profile, cap.max_width, cap.max_height | ||
| 17 | + ); | ||
| 18 | + } | ||
| 19 | + for codec in [EncCodec::H264, EncCodec::H265, EncCodec::Av1] { | ||
| 20 | + if let Err(e) = eight_bit(codec) { | ||
| 21 | + println!("{codec:?}: error: {e}"); | ||
| 22 | + } | ||
| 23 | + } | ||
| 24 | + for codec in [EncCodec::H265, EncCodec::Av1] { | ||
| 25 | + for backend in [Backend::Vk, Backend::Va] { | ||
| 26 | + if let Err(e) = ten_bit(codec, backend) { | ||
| 27 | + println!("{codec:?} 10-bit {backend:?}: error: {e}"); | ||
| 28 | + } | ||
| 29 | + } | ||
| 30 | + } | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +fn eight_bit(codec: EncCodec) -> ec_hw::Result<()> { | ||
| 34 | + let config = EncoderConfig { | ||
| 35 | + bitrate: 4_000_000, | ||
| 36 | + framerate: (FPS, 1), | ||
| 37 | + gop_size: 1000, | ||
| 38 | + rate_control: RateControlMode::ConstantBitrate, | ||
| 39 | + ..EncoderConfig::new(codec, W, H) | ||
| 40 | + }; | ||
| 41 | + let mut encoder = HwEncoder::open(config, None)?; | ||
| 42 | + let mut coded = Vec::new(); | ||
| 43 | + for index in 0..3 * SEGMENT { | ||
| 44 | + if index == SEGMENT { | ||
| 45 | + encoder.tune(Tunings { bitrate: 1_000_000, framerate: (FPS, 1) })?; | ||
| 46 | + } | ||
| 47 | + if index == 2 * SEGMENT { | ||
| 48 | + encoder.tune(Tunings { bitrate: 3_000_000, framerate: (FPS, 1) })?; | ||
| 49 | + } | ||
| 50 | + let pixels = bgra_gradient(index); | ||
| 51 | + let frame = RgbFrame { data: &pixels, width: W, height: H, stride: W * 4, order: RgbOrder::Bgra }; | ||
| 52 | + let meta = FrameMetadata { timestamp: index as i64, force_keyframe: index == 0 }; | ||
| 53 | + coded.push(encoder.encode_rgb(&frame, meta)?); | ||
| 54 | + } | ||
| 55 | + let kbps: Vec<String> = coded | ||
| 56 | + .chunks(SEGMENT as usize) | ||
| 57 | + .map(|chunk| { | ||
| 58 | + let bytes: usize = chunk.iter().map(|c| c.data.len()).sum(); | ||
| 59 | + format!("{:.0}", bytes as f64 * 8.0 * FPS as f64 / SEGMENT as f64 / 1000.0) | ||
| 60 | + }) | ||
| 61 | + .collect(); | ||
| 62 | + let keyframes = coded.iter().filter(|c| c.is_keyframe).count(); | ||
| 63 | + println!("{codec:?} {:?} kb/s per segment: {} keyframes {keyframes}", encoder.backend(), kbps.join(" ")); | ||
| 64 | + let decoded = decode(codec, &coded)?; | ||
| 65 | + println!(" decoded {} of {}", decoded.len(), coded.len()); | ||
| 66 | + Ok(()) | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +fn ten_bit(codec: EncCodec, backend: Backend) -> ec_hw::Result<()> { | ||
| 70 | + let config = EncoderConfig { | ||
| 71 | + bitrate: 4_000_000, | ||
| 72 | + rate_control: RateControlMode::ConstantQp { qp: 30 }, | ||
| 73 | + ..EncoderConfig::new(codec, W, H).ten_bit() | ||
| 74 | + }; | ||
| 75 | + let mut encoder = HwEncoder::open(config, Some(backend))?; | ||
| 76 | + let mut coded = Vec::new(); | ||
| 77 | + let mut sources = Vec::new(); | ||
| 78 | + for index in 0..SEGMENT { | ||
| 79 | + let frame = pattern(index); | ||
| 80 | + let meta = FrameMetadata { timestamp: index as i64, force_keyframe: index == 0 }; | ||
| 81 | + coded.push(encoder.encode_16(&frame, meta)?); | ||
| 82 | + sources.push(frame); | ||
| 83 | + } | ||
| 84 | + let decoded = decode(codec, &coded)?; | ||
| 85 | + let mut sum = 0.0; | ||
| 86 | + for picture in &decoded { | ||
| 87 | + sum += psnr(&sources[picture.timestamp as usize], &picture.to_i420_16()?); | ||
| 88 | + } | ||
| 89 | + let psnr = sum / decoded.len().max(1) as f64; | ||
| 90 | + println!("{codec:?} 10-bit {backend:?}: decoded {} of {}, PSNR {psnr:.2} dB", decoded.len(), coded.len()); | ||
| 91 | + Ok(()) | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +fn decode(codec: EncCodec, coded: &[CodedFrame]) -> ec_hw::Result<Vec<HwPicture>> { | ||
| 95 | + let mut decoder = HwDecoder::open(decode_codec(codec))?; | ||
| 96 | + let mut pictures = Vec::new(); | ||
| 97 | + for c in coded { | ||
| 98 | + decoder.decode(&c.data, c.timestamp)?; | ||
| 99 | + while let Some(picture) = decoder.next_picture()? { | ||
| 100 | + pictures.push(picture); | ||
| 101 | + } | ||
| 102 | + } | ||
| 103 | + decoder.flush()?; | ||
| 104 | + while let Some(picture) = decoder.next_picture()? { | ||
| 105 | + pictures.push(picture); | ||
| 106 | + } | ||
| 107 | + Ok(pictures) | ||
| 108 | +} | ||
| 109 | + | ||
| 110 | +fn decode_codec(codec: EncCodec) -> Codec { | ||
| 111 | + match codec { | ||
| 112 | + EncCodec::H264 => Codec::H264, | ||
| 113 | + EncCodec::H265 => Codec::H265, | ||
| 114 | + EncCodec::Av1 => Codec::Av1, | ||
| 115 | + } | ||
| 116 | +} | ||
| 117 | + | ||
| 118 | +fn bgra_gradient(index: u32) -> Vec<u8> { | ||
| 119 | + let shift = index as usize * 4; | ||
| 120 | + let mut out = Vec::with_capacity(W as usize * H as usize * 4); | ||
| 121 | + for y in 0..H as usize { | ||
| 122 | + for x in 0..W as usize { | ||
| 123 | + let r = (x + shift) & 0xff; | ||
| 124 | + let g = (y + 2 * shift) & 0xff; | ||
| 125 | + let b = ((x + y) / 2 + 3 * shift) & 0xff; | ||
| 126 | + out.extend_from_slice(&[b as u8, g as u8, r as u8, 0xff]); | ||
| 127 | + } | ||
| 128 | + } | ||
| 129 | + out | ||
| 130 | +} | ||
| 131 | + | ||
| 132 | +fn pattern(index: u32) -> I420_16 { | ||
| 133 | + let t = index as usize; | ||
| 134 | + let (w, h) = (W as usize, H as usize); | ||
| 135 | + I420_16 { | ||
| 136 | + y: plane(w, h, |x, y| x * 3 + y * 5 + t * 16), | ||
| 137 | + u: plane(w / 2, h / 2, |x, y| x * 7 + y * 2 + t * 9), | ||
| 138 | + v: plane(w / 2, h / 2, |x, y| x * 2 + y * 11 + t * 5 + 512), | ||
| 139 | + width: W, | ||
| 140 | + height: H, | ||
| 141 | + bit_depth: 10, | ||
| 142 | + } | ||
| 143 | +} | ||
| 144 | + | ||
| 145 | +fn plane(w: usize, h: usize, value: impl Fn(usize, usize) -> usize) -> Vec<u16> { | ||
| 146 | + let mut out = Vec::with_capacity(w * h); | ||
| 147 | + for row in 0..h { | ||
| 148 | + for col in 0..w { | ||
| 149 | + out.push((value(col, row) & 0x3ff) as u16); | ||
| 150 | + } | ||
| 151 | + } | ||
| 152 | + out | ||
| 153 | +} | ||
| 154 | + | ||
| 155 | +fn psnr(a: &I420_16, b: &I420_16) -> f64 { | ||
| 156 | + let mut sse = 0f64; | ||
| 157 | + let mut n = 0usize; | ||
| 158 | + for (x, y) in a.y.iter().chain(&a.u).chain(&a.v).zip(b.y.iter().chain(&b.u).chain(&b.v)) { | ||
| 159 | + let d = f64::from(*x) - f64::from(*y); | ||
| 160 | + sse += d * d; | ||
| 161 | + n += 1; | ||
| 162 | + } | ||
| 163 | + 10.0 * (PEAK * PEAK * n as f64 / sse).log10() | ||
| 164 | +} | ||
crates/ec-hw/examples/hw_speed.rsdeleted+0 -26
| @@ -1,26 +0,0 @@ | |||
| 1 | -use ec_hw::{EncCodec, EncoderConfig, FrameMetadata, HwEncoder, RateControlMode, RgbFrame, RgbOrder}; | ||
| 2 | -use std::time::Instant; | ||
| 3 | - | ||
| 4 | -fn main() { | ||
| 5 | - let (w, h) = (1920u32, 1080u32); | ||
| 6 | - let frames: Vec<Vec<u8>> = (0..8) | ||
| 7 | - .map(|t| (0..w * h).flat_map(|i| [(i % w + t * 9) as u8, (i / w) as u8, t as u8 * 20, 255]).collect()) | ||
| 8 | - .collect(); | ||
| 9 | - for codec in [EncCodec::H264, EncCodec::H265, EncCodec::Av1] { | ||
| 10 | - for (ow, oh) in [(1920, 1080), (640, 360)] { | ||
| 11 | - let mut cfg = EncoderConfig::new(codec, ow, oh); | ||
| 12 | - cfg.bitrate = 8_000_000; | ||
| 13 | - cfg.framerate = (60, 1); | ||
| 14 | - cfg.rate_control = RateControlMode::ConstantBitrate; | ||
| 15 | - let Ok(mut enc) = HwEncoder::open(cfg, None) else { continue }; | ||
| 16 | - let n = 120; | ||
| 17 | - let start = Instant::now(); | ||
| 18 | - for i in 0..n { | ||
| 19 | - let frame = RgbFrame { data: &frames[i % 8], width: w, height: h, stride: w * 4, order: RgbOrder::Bgra }; | ||
| 20 | - enc.encode_rgb(&frame, FrameMetadata { timestamp: i as i64, force_keyframe: false }).unwrap(); | ||
| 21 | - } | ||
| 22 | - let ms = start.elapsed().as_secs_f64() * 1000.0 / n as f64; | ||
| 23 | - println!("{codec:?} {:?} 1080p->{ow}x{oh}: {ms:.2} ms/frame ({:.0} fps)", enc.backend(), 1000.0 / ms); | ||
| 24 | - } | ||
| 25 | - } | ||
| 26 | -} | ||
crates/ec-hw/examples/hw_ten.rsdeleted+0 -141
| @@ -1,141 +0,0 @@ | |||
| 1 | -use ec_hw::{ | ||
| 2 | - Backend, Codec, EncCodec, EncoderConfig, FrameMetadata, HwDecoder, HwEncoder, I420_16, | ||
| 3 | - RateControlMode, | ||
| 4 | -}; | ||
| 5 | - | ||
| 6 | -const WIDTH: u32 = 1280; | ||
| 7 | -const HEIGHT: u32 = 720; | ||
| 8 | -const FRAMES: u32 = 30; | ||
| 9 | -const PEAK: f64 = 1023.0; | ||
| 10 | - | ||
| 11 | -fn main() { | ||
| 12 | - let dir = std::env::temp_dir().display().to_string(); | ||
| 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 | - | ||
| 28 | -fn 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(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(picture) = decoder.next_picture()? { | ||
| 67 | - decoded.push((picture.timestamp, picture.to_i420_16()?)); | ||
| 68 | - } | ||
| 69 | - } | ||
| 70 | - decoder.flush()?; | ||
| 71 | - while let Some(picture) = decoder.next_picture()? { | ||
| 72 | - decoded.push((picture.timestamp, picture.to_i420_16()?)); | ||
| 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 | - | ||
| 96 | -fn 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 | - | ||
| 104 | -fn 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 | - | ||
| 117 | -fn 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 | - | ||
| 127 | -fn 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 | -} | ||
crates/ec-hw/examples/hw_tune.rsdeleted+0 -102
| @@ -1,102 +0,0 @@ | |||
| 1 | -use ec_hw::{ | ||
| 2 | - Codec, EncCodec, EncoderConfig, FrameMetadata, HwDecoder, HwEncoder, RateControlMode, RgbFrame, | ||
| 3 | - RgbOrder, Tunings, | ||
| 4 | -}; | ||
| 5 | - | ||
| 6 | -const W: u32 = 1280; | ||
| 7 | -const H: u32 = 720; | ||
| 8 | -const FPS: u32 = 30; | ||
| 9 | -const SEGMENT: u32 = 90; | ||
| 10 | - | ||
| 11 | -fn main() { | ||
| 12 | - for codec in [EncCodec::H264, EncCodec::H265, EncCodec::Av1] { | ||
| 13 | - if let Err(e) = run(codec) { | ||
| 14 | - println!("{codec:?}: error: {e}"); | ||
| 15 | - } | ||
| 16 | - } | ||
| 17 | -} | ||
| 18 | - | ||
| 19 | -fn run(codec: EncCodec) -> ec_hw::Result<()> { | ||
| 20 | - let config = EncoderConfig { | ||
| 21 | - bitrate: 4_000_000, | ||
| 22 | - rate_control: RateControlMode::ConstantBitrate, | ||
| 23 | - framerate: (FPS, 1), | ||
| 24 | - gop_size: 1000, | ||
| 25 | - ..EncoderConfig::new(codec, W, H) | ||
| 26 | - }; | ||
| 27 | - let mut encoder = HwEncoder::open(config, None)?; | ||
| 28 | - let mut coded = Vec::new(); | ||
| 29 | - for index in 0..3 * SEGMENT { | ||
| 30 | - if index == SEGMENT { | ||
| 31 | - encoder.tune(Tunings { | ||
| 32 | - bitrate: 1_000_000, | ||
| 33 | - framerate: (FPS, 1), | ||
| 34 | - })?; | ||
| 35 | - } | ||
| 36 | - if index == 2 * SEGMENT { | ||
| 37 | - encoder.tune(Tunings { | ||
| 38 | - bitrate: 3_000_000, | ||
| 39 | - framerate: (FPS, 1), | ||
| 40 | - })?; | ||
| 41 | - } | ||
| 42 | - let pixels = bgra_gradient(index); | ||
| 43 | - let frame = RgbFrame { | ||
| 44 | - data: &pixels, | ||
| 45 | - width: W, | ||
| 46 | - height: H, | ||
| 47 | - stride: W * 4, | ||
| 48 | - order: RgbOrder::Bgra, | ||
| 49 | - }; | ||
| 50 | - let meta = FrameMetadata { | ||
| 51 | - timestamp: index as i64, | ||
| 52 | - force_keyframe: index == 0, | ||
| 53 | - }; | ||
| 54 | - coded.push(encoder.encode_rgb(&frame, meta)?); | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - let mut line = format!("{codec:?} {:?} kb/s per segment:", encoder.backend()); | ||
| 58 | - for chunk in coded.chunks(SEGMENT as usize) { | ||
| 59 | - let bytes: usize = chunk.iter().map(|c| c.data.len()).sum(); | ||
| 60 | - let kbps = bytes as f64 * 8.0 * FPS as f64 / SEGMENT as f64 / 1000.0; | ||
| 61 | - line += &format!(" {kbps:.0}"); | ||
| 62 | - } | ||
| 63 | - let keyframes = coded.iter().filter(|c| c.is_keyframe).count(); | ||
| 64 | - println!("{line}, keyframes {keyframes}"); | ||
| 65 | - | ||
| 66 | - let mut decoder = HwDecoder::open(decode_codec(codec))?; | ||
| 67 | - let mut decoded = 0; | ||
| 68 | - for c in &coded { | ||
| 69 | - decoder.decode(&c.data, c.timestamp)?; | ||
| 70 | - while decoder.next_frame()?.is_some() { | ||
| 71 | - decoded += 1; | ||
| 72 | - } | ||
| 73 | - } | ||
| 74 | - decoder.flush()?; | ||
| 75 | - while decoder.next_frame()?.is_some() { | ||
| 76 | - decoded += 1; | ||
| 77 | - } | ||
| 78 | - println!(" decoded {decoded} of {}", coded.len()); | ||
| 79 | - Ok(()) | ||
| 80 | -} | ||
| 81 | - | ||
| 82 | -fn decode_codec(codec: EncCodec) -> Codec { | ||
| 83 | - match codec { | ||
| 84 | - EncCodec::H264 => Codec::H264, | ||
| 85 | - EncCodec::H265 => Codec::H265, | ||
| 86 | - EncCodec::Av1 => Codec::Av1, | ||
| 87 | - } | ||
| 88 | -} | ||
| 89 | - | ||
| 90 | -fn bgra_gradient(index: u32) -> Vec<u8> { | ||
| 91 | - let shift = index as usize * 4; | ||
| 92 | - let mut out = Vec::with_capacity(W as usize * H as usize * 4); | ||
| 93 | - for y in 0..H as usize { | ||
| 94 | - for x in 0..W as usize { | ||
| 95 | - let r = (x + shift) & 0xff; | ||
| 96 | - let g = (y + 2 * shift) & 0xff; | ||
| 97 | - let b = ((x + y) / 2 + 3 * shift) & 0xff; | ||
| 98 | - out.extend_from_slice(&[b as u8, g as u8, r as u8, 0xff]); | ||
| 99 | - } | ||
| 100 | - } | ||
| 101 | - out | ||
| 102 | -} | ||