From d51b2ae6eb340455e7fe1004b917e70b0904e77c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Nov 2026 00:56:37 +0300 Subject: [PATCH] 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) --- crates/ec-hw/examples/hw_decode_rgb.rs | 60 +++++++++++++++----------- crates/ec-hw/src/hw.rs | 18 ++++++++ crates/ec-hw/src/output.rs | 6 +-- crates/ec-hw/src/picture.rs | 31 +++++++------ 4 files changed, 75 insertions(+), 40 deletions(-) diff --git a/crates/ec-hw/examples/hw_decode_rgb.rs b/crates/ec-hw/examples/hw_decode_rgb.rs index b5533679..8c8e81bc 100644 --- a/crates/ec-hw/examples/hw_decode_rgb.rs +++ b/crates/ec-hw/examples/hw_decode_rgb.rs @@ -1,4 +1,4 @@ -//! 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. +//! 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. //! @@ -33,18 +33,18 @@ fn main() { }; let (_, reference, i420_time) = decode_i420(codec, &units); let expected_frames = reference.len(); - for max_width in [None, Some(640)] { - let order = if max_width.is_some() { - RgbOrder::Rgba - } else { - RgbOrder::Bgra - }; - let output = RgbOutput { order, max_width }; - let (backend, got, rgb_time) = decode_rgb(codec, &units, output); + 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() - .map(|f| { - let (w, h) = out_size(f.width, f.height, max_width); + .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, @@ -52,7 +52,7 @@ fn main() { &f.v, (f.width as usize, f.height as usize), (w as usize, h as usize), - order == RgbOrder::Bgra, + output.order == RgbOrder::Bgra, &coeffs, ); (data, w, h) @@ -69,7 +69,7 @@ fn main() { " (frame count differs from I420)" } ); - if max_width.is_none() { + if outputs == [native] { println!( "{name} ms/frame I420 {:.3} RGB {:.3}", ms_per_frame(i420_time, reference.len()), @@ -88,13 +88,17 @@ fn matrix_for(height: u32) -> YuvToRgb { } } -fn out_size(w: u32, h: u32, max_width: Option) -> (u32, u32) { - match max_width { - Some(max) if w > max => { - let even = 2 * ((u64::from(h) * u64::from(max) + u64::from(w)) / (2 * u64::from(w))); - (max, (even as u32).clamp(2, h)) - } - _ => (w, h), +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) } } @@ -141,20 +145,28 @@ fn decode_i420(codec: Codec, units: &[&[u8]]) -> (Backend, Vec, Duration) fn decode_rgb( codec: Codec, units: &[&[u8]], - output: RgbOutput, + outputs: &[RgbOutput], ) -> (Backend, Vec, Duration) { - let mut decoder = HwDecoder::open_rgb(codec, output).expect("open RGB decoder"); + 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"); - while let Some((_, picture)) = decoder.next_rgb().expect("next RGB") { + 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"); - while let Some((_, picture)) = decoder.next_rgb().expect("next RGB") { + 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()) diff --git a/crates/ec-hw/src/hw.rs b/crates/ec-hw/src/hw.rs index 8374e7b5..8e9e333e 100644 --- a/crates/ec-hw/src/hw.rs +++ b/crates/ec-hw/src/hw.rs @@ -1071,6 +1071,14 @@ pub struct RgbOutput { pub order: RgbOrder, /// Scale pictures wider than this down to it, keeping the aspect ratio. pub max_width: Option, + /// Scale pictures taller than this down to it, keeping the aspect ratio. + pub max_height: Option, +} + +impl RgbOutput { + pub(crate) fn bound(&self) -> (Option, Option) { + (self.max_width, self.max_height) + } } /// A hardware decoder on whichever backend opened. @@ -1222,6 +1230,16 @@ impl HwDecoder { Ok(self.next_rgb_into(&mut dst)?.map(|ts| (ts, dst))) } + /// Changes the RGB output of a decoder opened with [`HwDecoder::open_rgb`] from the next picture on. + pub fn set_rgb_output(&mut self, output: RgbOutput) -> Result<()> { + let rgb = self + .rgb + .as_mut() + .ok_or_else(|| Error::config("set_rgb_output on a decoder not opened with open_rgb"))?; + *rgb = output; + Ok(()) + } + /// [`HwDecoder::next_rgb`] into `dst`, reusing its allocation across pictures; returns the timestamp. pub fn next_rgb_into(&mut self, dst: &mut RgbPicture) -> Result> { let output = self diff --git a/crates/ec-hw/src/output.rs b/crates/ec-hw/src/output.rs index 594cd37c..26e17466 100644 --- a/crates/ec-hw/src/output.rs +++ b/crates/ec-hw/src/output.rs @@ -108,7 +108,7 @@ impl HwPicture { .unwrap_or_else(|| default_matrix(i420.height)); *dst = i420_to_rgb( &i420, - output.max_width, + output.bound(), output.order == RgbOrder::Bgra, matrix, ); @@ -340,10 +340,10 @@ impl VkPicture { "RGB of 10-bit output", )); } - *dst = i420_to_rgb(&self.i420()?, output.max_width, bgra, self.matrix); + *dst = i420_to_rgb(&self.i420()?, output.bound(), bgra, self.matrix); return Ok(()); } - let (w, h) = rgb_size(self.width, self.height, output.max_width); + let (w, h) = rgb_size(self.width, self.height, output.bound()); self.surfaces.rgb( self.slot.index, (0, 0, self.width, self.height), diff --git a/crates/ec-hw/src/picture.rs b/crates/ec-hw/src/picture.rs index 73f78ca3..d96a824f 100644 --- a/crates/ec-hw/src/picture.rs +++ b/crates/ec-hw/src/picture.rs @@ -122,14 +122,14 @@ pub(crate) fn yuv_to_rgb(matrix: Matrix) -> ec_dsp::rgb::YuvToRgb { } } -/// Packed RGB of an 8-bit I420 picture, scaled to `max_width`, converted on the CPU with the coefficients of `matrix`. +/// Packed RGB of an 8-bit I420 picture, scaled into `bound`, converted on the CPU with the coefficients of `matrix`. pub(crate) fn i420_to_rgb( frame: &I420, - max_width: Option, + bound: (Option, Option), bgra: bool, matrix: Matrix, ) -> RgbPicture { - let (w, h) = rgb_size(frame.width, frame.height, max_width); + let (w, h) = rgb_size(frame.width, frame.height, bound); let data = ec_dsp::rgb::i420_to_packed( &frame.y, &frame.u, @@ -142,16 +142,21 @@ pub(crate) fn i420_to_rgb( RgbPicture::new(data, w, h) } -/// Output size of an RGB picture of `width` x `height`: scaled to `max_width` keeping the aspect, height rounded to the nearest even. -pub(crate) fn rgb_size(width: u32, height: u32, max_width: Option) -> (u32, u32) { - match max_width { - Some(max) if max > 0 && width > max => { - let (w, h) = (u64::from(width), u64::from(height)); - let max = u64::from(max); - let even = 2 * ((h * max + w) / (2 * w)); - (max as u32, even.clamp(2, h) as u32) - } - _ => (width, height), +/// Output size of an RGB picture of `width` x `height` fit inside `(max_width, max_height)` keeping the aspect; a scaled height is even. +pub(crate) fn rgb_size(width: u32, height: u32, bound: (Option, Option)) -> (u32, u32) { + let (w, h) = (u64::from(width), u64::from(height)); + let limit = |max: Option, full: u64| max.filter(|&m| m > 0).map_or(full, u64::from).min(full); + let (mw, mh) = (limit(bound.0, w), limit(bound.1, h)); + if mw == w && mh == h { + return (width, height); + } + if mw * h <= mh * w { + let even = 2 * ((h * mw + w) / (2 * w)); + (mw as u32, even.clamp(2, h) as u32) + } else { + let even = (mh & !1).max(2); + let scaled = (w * even + h / 2) / h; + (scaled.clamp(1, w) as u32, even as u32) } }