forge
Explore
Log in

Don't you dare go Hollow

ec-hw: HwDecoder::set_rgb_output changes the RGB output of an open_rgb decoder from the next picture on; RgbOutput::max_height bounds the height too (fit inside both, aspect kept, scaled height even)

Browse files at this commit

d51b2ae6eb340455e7fe1004b917e70b0904e77cAhmet Kaan GümüşParent: 7836ccd
crates/ec-hw/examples/hw_decode_rgb.rsmodified+36 -24
@@ -1,4 +1,4 @@
1-//! Decodes streams through `HwDecoder::open_rgb` at native size and at `max_width` 640, 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.
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.
22 //!
33 //! Streams are assumed untagged, so the matrix is BT.709 from 720 lines up and BT.601 below.
44 //!
@@ -33,18 +33,18 @@ fn main() {
3333 };
3434 let (_, reference, i420_time) = decode_i420(codec, &units);
3535 let expected_frames = reference.len();
36- for max_width in [None, Some(640)] {
37- let order = if max_width.is_some() {
38- RgbOrder::Rgba
39- } else {
40- RgbOrder::Bgra
41- };
42- let output = RgbOutput { order, max_width };
43- let (backend, got, rgb_time) = decode_rgb(codec, &units, output);
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);
4442 let expected: Vec<(Vec<u8>, u32, u32)> = reference
4543 .iter()
46- .map(|f| {
47- let (w, h) = out_size(f.width, f.height, max_width);
44+ .enumerate()
45+ .map(|(i, f)| {
46+ let output = outputs[i % outputs.len()];
47+ let (w, h) = out_size(f.width, f.height, output);
4848 let coeffs = matrix_for(f.height);
4949 let data = i420_to_packed(
5050 &f.y,
@@ -52,7 +52,7 @@ fn main() {
5252 &f.v,
5353 (f.width as usize, f.height as usize),
5454 (w as usize, h as usize),
55- order == RgbOrder::Bgra,
55+ output.order == RgbOrder::Bgra,
5656 &coeffs,
5757 );
5858 (data, w, h)
@@ -69,7 +69,7 @@ fn main() {
6969 " (frame count differs from I420)"
7070 }
7171 );
72- if max_width.is_none() {
72+ if outputs == [native] {
7373 println!(
7474 "{name} ms/frame I420 {:.3} RGB {:.3}",
7575 ms_per_frame(i420_time, reference.len()),
@@ -88,13 +88,17 @@ fn matrix_for(height: u32) -> YuvToRgb {
8888 }
8989 }
9090
91-fn out_size(w: u32, h: u32, max_width: Option<u32>) -> (u32, u32) {
92- match max_width {
93- Some(max) if w > max => {
94- let even = 2 * ((u64::from(h) * u64::from(max) + u64::from(w)) / (2 * u64::from(w)));
95- (max, (even as u32).clamp(2, h))
96- }
97- _ => (w, h),
91+fn 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)
98102 }
99103 }
100104
@@ -141,20 +145,28 @@ fn decode_i420(codec: Codec, units: &[&[u8]]) -> (Backend, Vec<I420>, Duration)
141145 fn decode_rgb(
142146 codec: Codec,
143147 units: &[&[u8]],
144- output: RgbOutput,
148+ outputs: &[RgbOutput],
145149 ) -> (Backend, Vec<RgbPicture>, Duration) {
146- let mut decoder = HwDecoder::open_rgb(codec, output).expect("open RGB decoder");
150+ let mut decoder = HwDecoder::open_rgb(codec, outputs[0]).expect("open RGB decoder");
147151 let backend = decoder.backend();
148152 let mut out = Vec::new();
149153 let start = Instant::now();
150154 for (pts, unit) in units.iter().enumerate() {
151155 decoder.decode(unit, pts as i64).expect("decode");
152- while let Some((_, picture)) = decoder.next_rgb().expect("next RGB") {
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+ };
153161 out.push(picture);
154162 }
155163 }
156164 decoder.flush().expect("flush");
157- while let Some((_, picture)) = decoder.next_rgb().expect("next RGB") {
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+ };
158170 out.push(picture);
159171 }
160172 (backend, out, start.elapsed())
crates/ec-hw/src/hw.rsmodified+18 -0
@@ -1071,6 +1071,14 @@ pub struct RgbOutput {
10711071 pub order: RgbOrder,
10721072 /// Scale pictures wider than this down to it, keeping the aspect ratio.
10731073 pub max_width: Option<u32>,
1074+ /// Scale pictures taller than this down to it, keeping the aspect ratio.
1075+ pub max_height: Option<u32>,
1076+}
1077+
1078+impl RgbOutput {
1079+ pub(crate) fn bound(&self) -> (Option<u32>, Option<u32>) {
1080+ (self.max_width, self.max_height)
1081+ }
10741082 }
10751083
10761084 /// A hardware decoder on whichever backend opened.
@@ -1222,6 +1230,16 @@ impl HwDecoder {
12221230 Ok(self.next_rgb_into(&mut dst)?.map(|ts| (ts, dst)))
12231231 }
12241232
1233+ /// Changes the RGB output of a decoder opened with [`HwDecoder::open_rgb`] from the next picture on.
1234+ pub fn set_rgb_output(&mut self, output: RgbOutput) -> Result<()> {
1235+ let rgb = self
1236+ .rgb
1237+ .as_mut()
1238+ .ok_or_else(|| Error::config("set_rgb_output on a decoder not opened with open_rgb"))?;
1239+ *rgb = output;
1240+ Ok(())
1241+ }
1242+
12251243 /// [`HwDecoder::next_rgb`] into `dst`, reusing its allocation across pictures; returns the timestamp.
12261244 pub fn next_rgb_into(&mut self, dst: &mut RgbPicture) -> Result<Option<i64>> {
12271245 let output = self
crates/ec-hw/src/output.rsmodified+3 -3
@@ -108,7 +108,7 @@ impl HwPicture {
108108 .unwrap_or_else(|| default_matrix(i420.height));
109109 *dst = i420_to_rgb(
110110 &i420,
111- output.max_width,
111+ output.bound(),
112112 output.order == RgbOrder::Bgra,
113113 matrix,
114114 );
@@ -340,10 +340,10 @@ impl VkPicture {
340340 "RGB of 10-bit output",
341341 ));
342342 }
343- *dst = i420_to_rgb(&self.i420()?, output.max_width, bgra, self.matrix);
343+ *dst = i420_to_rgb(&self.i420()?, output.bound(), bgra, self.matrix);
344344 return Ok(());
345345 }
346- let (w, h) = rgb_size(self.width, self.height, output.max_width);
346+ let (w, h) = rgb_size(self.width, self.height, output.bound());
347347 self.surfaces.rgb(
348348 self.slot.index,
349349 (0, 0, self.width, self.height),
crates/ec-hw/src/picture.rsmodified+18 -13
@@ -122,14 +122,14 @@ pub(crate) fn yuv_to_rgb(matrix: Matrix) -> ec_dsp::rgb::YuvToRgb {
122122 }
123123 }
124124
125-/// Packed RGB of an 8-bit I420 picture, scaled to `max_width`, converted on the CPU with the coefficients of `matrix`.
125+/// Packed RGB of an 8-bit I420 picture, scaled into `bound`, converted on the CPU with the coefficients of `matrix`.
126126 pub(crate) fn i420_to_rgb(
127127 frame: &I420,
128- max_width: Option<u32>,
128+ bound: (Option<u32>, Option<u32>),
129129 bgra: bool,
130130 matrix: Matrix,
131131 ) -> RgbPicture {
132- let (w, h) = rgb_size(frame.width, frame.height, max_width);
132+ let (w, h) = rgb_size(frame.width, frame.height, bound);
133133 let data = ec_dsp::rgb::i420_to_packed(
134134 &frame.y,
135135 &frame.u,
@@ -142,16 +142,21 @@ pub(crate) fn i420_to_rgb(
142142 RgbPicture::new(data, w, h)
143143 }
144144
145-/// Output size of an RGB picture of `width` x `height`: scaled to `max_width` keeping the aspect, height rounded to the nearest even.
146-pub(crate) fn rgb_size(width: u32, height: u32, max_width: Option<u32>) -> (u32, u32) {
147- match max_width {
148- Some(max) if max > 0 && width > max => {
149- let (w, h) = (u64::from(width), u64::from(height));
150- let max = u64::from(max);
151- let even = 2 * ((h * max + w) / (2 * w));
152- (max as u32, even.clamp(2, h) as u32)
153- }
154- _ => (width, height),
145+/// Output size of an RGB picture of `width` x `height` fit inside `(max_width, max_height)` keeping the aspect; a scaled height is even.
146+pub(crate) fn rgb_size(width: u32, height: u32, bound: (Option<u32>, Option<u32>)) -> (u32, u32) {
147+ let (w, h) = (u64::from(width), u64::from(height));
148+ let limit = |max: Option<u32>, full: u64| max.filter(|&m| m > 0).map_or(full, u64::from).min(full);
149+ let (mw, mh) = (limit(bound.0, w), limit(bound.1, h));
150+ if mw == w && mh == h {
151+ return (width, height);
152+ }
153+ if mw * h <= mh * w {
154+ let even = 2 * ((h * mw + w) / (2 * w));
155+ (mw as u32, even.clamp(2, h) as u32)
156+ } else {
157+ let even = (mh & !1).max(2);
158+ let scaled = (w * even + h / 2) / h;
159+ (scaled.clamp(1, w) as u32, even as u32)
155160 }
156161 }
157162