//! Decodes streams through `HwDecoder::open_rgb` at native size, `max_width` 640, `max_height` 301 and switching between them per picture, and compares every picture with the CPU reference: the I420 decode of the same stream through `ec_dsp::rgb::i420_to_packed`. Times the decode loop of the RGB and I420 paths. //! //! Streams are assumed untagged, so the matrix is BT.709 from 720 lines up and BT.601 below. //! //! ```text //! ``` use std::fs; use std::time::{Duration, Instant}; use ec_dsp::rgb::{YuvToRgb, i420_to_packed}; use ec_hw::{Backend, Codec, HwDecoder, I420, RgbOrder, RgbOutput, RgbPicture}; const FILES: &[(&str, Codec)] = &[ ("bf1280.h264", Codec::H264), ("h265idr720.hevc", Codec::H265), ("av1aom720.ivf", Codec::Av1), ("ten.hevc", Codec::H265), ("vp9_640.ivf", Codec::Vp9), ("vp9_720.ivf", Codec::Vp9), ]; fn main() { for &(name, codec) in FILES { let data = fs::read(ec_testkit::fixture(&format!("bitstreams/{name}"))).expect("read stream"); let units = if matches!(codec, Codec::Av1 | Codec::Vp9) { ivf_units(&data) } else { vec![data.as_slice()] }; let (_, reference, i420_time) = decode_i420(codec, &units); let expected_frames = reference.len(); let native = RgbOutput { order: RgbOrder::Bgra, max_width: None, max_height: None }; let wide = RgbOutput { order: RgbOrder::Rgba, max_width: Some(640), max_height: None }; let tall = RgbOutput { order: RgbOrder::Rgba, max_width: None, max_height: Some(301) }; let cases: [&[RgbOutput]; 4] = [&[native], &[wide], &[tall], &[native, wide, tall]]; for outputs in cases { let (backend, got, rgb_time) = decode_rgb(codec, &units, outputs); let expected: Vec<(Vec, u32, u32)> = reference .iter() .enumerate() .map(|(i, f)| { let output = outputs[i % outputs.len()]; let (w, h) = out_size(f.width, f.height, output); let coeffs = matrix_for(f.height); let data = i420_to_packed( &f.y, &f.u, &f.v, (f.width as usize, f.height as usize), (w as usize, h as usize), output.order == RgbOrder::Bgra, &coeffs, ); (data, w, h) }) .collect(); let (w, h) = expected.first().map_or((0, 0), |e| (e.1, e.2)); let max_diff = max_diff(&got, &expected); println!( "{name} {backend:?} {w}x{h} frames {} max |diff| {max_diff}{}", got.len(), if got.len() == expected_frames { "" } else { " (frame count differs from I420)" } ); if outputs == [native] { println!( "{name} ms/frame I420 {:.3} RGB {:.3}", ms_per_frame(i420_time, reference.len()), ms_per_frame(rgb_time, got.len()) ); } } } } fn matrix_for(height: u32) -> YuvToRgb { if height >= 720 { ec_dsp::rgb::BT709_LIMITED } else { ec_dsp::rgb::BT601_LIMITED } } fn out_size(width: u32, height: u32, output: RgbOutput) -> (u32, u32) { let (w, h) = (u64::from(width), u64::from(height)); let mw = output.max_width.map_or(w, u64::from).min(w); let mh = output.max_height.map_or(h, u64::from).min(h); if mw == w && mh == h { (width, height) } else if mw * h <= mh * w { (mw as u32, (2 * ((h * mw + w) / (2 * w))).clamp(2, h) as u32) } else { let even = (mh & !1).max(2); (((w * even + h / 2) / h).clamp(1, w) as u32, even as u32) } } fn max_diff(got: &[RgbPicture], expected: &[(Vec, u32, u32)]) -> u32 { got.iter() .zip(expected) .map(|(g, (data, w, h))| { if g.width != *w || g.height != *h || g.stride != w * 4 || g.data.len() != data.len() { return 255; } g.data .iter() .zip(data) .map(|(a, b)| u32::from(a.abs_diff(*b))) .max() .unwrap_or(0) }) .max() .unwrap_or(0) } fn ms_per_frame(elapsed: Duration, frames: usize) -> f64 { elapsed.as_secs_f64() * 1000.0 / frames.max(1) as f64 } fn decode_i420(codec: Codec, units: &[&[u8]]) -> (Backend, Vec, Duration) { let mut decoder = HwDecoder::open(codec).expect("open decoder"); let backend = decoder.backend(); let mut out = Vec::new(); let start = Instant::now(); for (pts, unit) in units.iter().enumerate() { decoder.decode(unit, pts as i64).expect("decode"); while let Some((_, frame)) = decoder.next_frame().expect("next frame") { out.push(frame); } } decoder.flush().expect("flush"); while let Some((_, frame)) = decoder.next_frame().expect("next frame") { out.push(frame); } (backend, out, start.elapsed()) } fn decode_rgb( codec: Codec, units: &[&[u8]], outputs: &[RgbOutput], ) -> (Backend, Vec, Duration) { let mut decoder = HwDecoder::open_rgb(codec, outputs[0]).expect("open RGB decoder"); let backend = decoder.backend(); let mut out = Vec::new(); let start = Instant::now(); for (pts, unit) in units.iter().enumerate() { decoder.decode(unit, pts as i64).expect("decode"); loop { decoder.set_rgb_output(outputs[out.len() % outputs.len()]).expect("set output"); let Some((_, picture)) = decoder.next_rgb().expect("next RGB") else { break; }; out.push(picture); } } decoder.flush().expect("flush"); loop { decoder.set_rgb_output(outputs[out.len() % outputs.len()]).expect("set output"); let Some((_, picture)) = decoder.next_rgb().expect("next RGB") else { break; }; out.push(picture); } (backend, out, start.elapsed()) } fn ivf_units(ivf: &[u8]) -> Vec<&[u8]> { let mut pos = usize::from(u16::from_le_bytes([ivf[6], ivf[7]])); let mut units = Vec::new(); while pos + 12 <= ivf.len() { let len = u32::from_le_bytes(ivf[pos..pos + 4].try_into().expect("frame size")) as usize; units.push(&ivf[pos + 12..pos + 12 + len]); pos += 12 + len; } units }