forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

hw_decode_rgb.rs184 lines, 6.6 KiBHistoryRawDownload
1//! 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.
2//!
3//! Streams are assumed untagged, so the matrix is BT.709 from 720 lines up and BT.601 below.
4//!
5//! ```text
6//! cargo run --release -p ec-hw --example hw_decode_rgb -- <scratch dir>
7//! ```
8
9use std::fs;
10use std::path::PathBuf;
11use std::time::{Duration, Instant};
12
13use ec_dsp::rgb::{YuvToRgb, i420_to_packed};
14use ec_hw::{Backend, Codec, HwDecoder, I420, RgbOrder, RgbOutput, RgbPicture};
15
16const FILES: &[(&str, Codec)] = &[
17 ("bf1280.h264", Codec::H264),
18 ("h265idr720.hevc", Codec::H265),
19 ("av1aom720.ivf", Codec::Av1),
20 ("ten.hevc", Codec::H265),
21 ("vp9_640.ivf", Codec::Vp9),
22 ("vp9_720.ivf", Codec::Vp9),
23];
24
25fn main() {
26 let dir = PathBuf::from(std::env::args().nth(1).expect("scratch dir argument"));
27 for &(name, codec) in FILES {
28 let data = fs::read(dir.join(name)).expect("read stream");
29 let units = if matches!(codec, Codec::Av1 | Codec::Vp9) {
30 ivf_units(&data)
31 } else {
32 vec![data.as_slice()]
33 };
34 let (_, reference, i420_time) = decode_i420(codec, &units);
35 let expected_frames = reference.len();
36 let native = RgbOutput { order: RgbOrder::Bgra, max_width: None, max_height: None };
37 let wide = RgbOutput { order: RgbOrder::Rgba, max_width: Some(640), max_height: None };
38 let tall = RgbOutput { order: RgbOrder::Rgba, max_width: None, max_height: Some(301) };
39 let cases: [&[RgbOutput]; 4] = [&[native], &[wide], &[tall], &[native, wide, tall]];
40 for outputs in cases {
41 let (backend, got, rgb_time) = decode_rgb(codec, &units, outputs);
42 let expected: Vec<(Vec<u8>, u32, u32)> = reference
43 .iter()
44 .enumerate()
45 .map(|(i, f)| {
46 let output = outputs[i % outputs.len()];
47 let (w, h) = out_size(f.width, f.height, output);
48 let coeffs = matrix_for(f.height);
49 let data = i420_to_packed(
50 &f.y,
51 &f.u,
52 &f.v,
53 (f.width as usize, f.height as usize),
54 (w as usize, h as usize),
55 output.order == RgbOrder::Bgra,
56 &coeffs,
57 );
58 (data, w, h)
59 })
60 .collect();
61 let (w, h) = expected.first().map_or((0, 0), |e| (e.1, e.2));
62 let max_diff = max_diff(&got, &expected);
63 println!(
64 "{name} {backend:?} {w}x{h} frames {} max |diff| {max_diff}{}",
65 got.len(),
66 if got.len() == expected_frames {
67 ""
68 } else {
69 " (frame count differs from I420)"
70 }
71 );
72 if outputs == [native] {
73 println!(
74 "{name} ms/frame I420 {:.3} RGB {:.3}",
75 ms_per_frame(i420_time, reference.len()),
76 ms_per_frame(rgb_time, got.len())
77 );
78 }
79 }
80 }
81}
82
83fn matrix_for(height: u32) -> YuvToRgb {
84 if height >= 720 {
85 ec_dsp::rgb::BT709_LIMITED
86 } else {
87 ec_dsp::rgb::BT601_LIMITED
88 }
89}
90
91fn out_size(width: u32, height: u32, output: RgbOutput) -> (u32, u32) {
92 let (w, h) = (u64::from(width), u64::from(height));
93 let mw = output.max_width.map_or(w, u64::from).min(w);
94 let mh = output.max_height.map_or(h, u64::from).min(h);
95 if mw == w && mh == h {
96 (width, height)
97 } else if mw * h <= mh * w {
98 (mw as u32, (2 * ((h * mw + w) / (2 * w))).clamp(2, h) as u32)
99 } else {
100 let even = (mh & !1).max(2);
101 (((w * even + h / 2) / h).clamp(1, w) as u32, even as u32)
102 }
103}
104
105fn max_diff(got: &[RgbPicture], expected: &[(Vec<u8>, u32, u32)]) -> u32 {
106 got.iter()
107 .zip(expected)
108 .map(|(g, (data, w, h))| {
109 if g.width != *w || g.height != *h || g.stride != w * 4 || g.data.len() != data.len() {
110 return 255;
111 }
112 g.data
113 .iter()
114 .zip(data)
115 .map(|(a, b)| u32::from(a.abs_diff(*b)))
116 .max()
117 .unwrap_or(0)
118 })
119 .max()
120 .unwrap_or(0)
121}
122
123fn ms_per_frame(elapsed: Duration, frames: usize) -> f64 {
124 elapsed.as_secs_f64() * 1000.0 / frames.max(1) as f64
125}
126
127fn decode_i420(codec: Codec, units: &[&[u8]]) -> (Backend, Vec<I420>, Duration) {
128 let mut decoder = HwDecoder::open(codec).expect("open decoder");
129 let backend = decoder.backend();
130 let mut out = Vec::new();
131 let start = Instant::now();
132 for (pts, unit) in units.iter().enumerate() {
133 decoder.decode(unit, pts as i64).expect("decode");
134 while let Some((_, frame)) = decoder.next_frame().expect("next frame") {
135 out.push(frame);
136 }
137 }
138 decoder.flush().expect("flush");
139 while let Some((_, frame)) = decoder.next_frame().expect("next frame") {
140 out.push(frame);
141 }
142 (backend, out, start.elapsed())
143}
144
145fn decode_rgb(
146 codec: Codec,
147 units: &[&[u8]],
148 outputs: &[RgbOutput],
149) -> (Backend, Vec<RgbPicture>, Duration) {
150 let mut decoder = HwDecoder::open_rgb(codec, outputs[0]).expect("open RGB decoder");
151 let backend = decoder.backend();
152 let mut out = Vec::new();
153 let start = Instant::now();
154 for (pts, unit) in units.iter().enumerate() {
155 decoder.decode(unit, pts as i64).expect("decode");
156 loop {
157 decoder.set_rgb_output(outputs[out.len() % outputs.len()]).expect("set output");
158 let Some((_, picture)) = decoder.next_rgb().expect("next RGB") else {
159 break;
160 };
161 out.push(picture);
162 }
163 }
164 decoder.flush().expect("flush");
165 loop {
166 decoder.set_rgb_output(outputs[out.len() % outputs.len()]).expect("set output");
167 let Some((_, picture)) = decoder.next_rgb().expect("next RGB") else {
168 break;
169 };
170 out.push(picture);
171 }
172 (backend, out, start.elapsed())
173}
174
175fn ivf_units(ivf: &[u8]) -> Vec<&[u8]> {
176 let mut pos = usize::from(u16::from_le_bytes([ivf[6], ivf[7]]));
177 let mut units = Vec::new();
178 while pos + 12 <= ivf.len() {
179 let len = u32::from_le_bytes(ivf[pos..pos + 4].try_into().expect("frame size")) as usize;
180 units.push(&ivf[pos + 12..pos + 12 + len]);
181 pos += 12 + len;
182 }
183 units
184}