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.
2 //!2 //!
3 //! Streams are assumed untagged, so the matrix is BT.709 from 720 lines up and BT.601 below.3 //! Streams are assumed untagged, so the matrix is BT.709 from 720 lines up and BT.601 below.
4 //!4 //!
@@ -33,18 +33,18 @@ fn main() {
33 };33 };
34 let (_, reference, i420_time) = decode_i420(codec, &units);34 let (_, reference, i420_time) = decode_i420(codec, &units);
35 let expected_frames = reference.len();35 let expected_frames = reference.len();
36- for max_width in [None, Some(640)] {36+ let native = RgbOutput { order: RgbOrder::Bgra, max_width: None, max_height: None };
37- let order = if max_width.is_some() {37+ let wide = RgbOutput { order: RgbOrder::Rgba, max_width: Some(640), max_height: None };
38- RgbOrder::Rgba38+ let tall = RgbOutput { order: RgbOrder::Rgba, max_width: None, max_height: Some(301) };
39- } else {39+ let cases: [&[RgbOutput]; 4] = [&[native], &[wide], &[tall], &[native, wide, tall]];
40- RgbOrder::Bgra40+ for outputs in cases {
41- };41+ let (backend, got, rgb_time) = decode_rgb(codec, &units, outputs);
42- let output = RgbOutput { order, max_width };
43- let (backend, got, rgb_time) = decode_rgb(codec, &units, output);
44 let expected: Vec<(Vec<u8>, u32, u32)> = reference42 let expected: Vec<(Vec<u8>, u32, u32)> = reference
45 .iter()43 .iter()
46- .map(|f| {44+ .enumerate()
47- let (w, h) = out_size(f.width, f.height, max_width);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);48 let coeffs = matrix_for(f.height);
49 let data = i420_to_packed(49 let data = i420_to_packed(
50 &f.y,50 &f.y,
@@ -52,7 +52,7 @@ fn main() {
52 &f.v,52 &f.v,
53 (f.width as usize, f.height as usize),53 (f.width as usize, f.height as usize),
54 (w as usize, h as usize),54 (w as usize, h as usize),
55- order == RgbOrder::Bgra,55+ output.order == RgbOrder::Bgra,
56 &coeffs,56 &coeffs,
57 );57 );
58 (data, w, h)58 (data, w, h)
@@ -69,7 +69,7 @@ fn main() {
69 " (frame count differs from I420)"69 " (frame count differs from I420)"
70 }70 }
71 );71 );
72- if max_width.is_none() {72+ if outputs == [native] {
73 println!(73 println!(
74 "{name} ms/frame I420 {:.3} RGB {:.3}",74 "{name} ms/frame I420 {:.3} RGB {:.3}",
75 ms_per_frame(i420_time, reference.len()),75 ms_per_frame(i420_time, reference.len()),
@@ -88,13 +88,17 @@ fn matrix_for(height: u32) -> YuvToRgb {
88 }88 }
89 }89 }
90 90
91-fn out_size(w: u32, h: u32, max_width: Option<u32>) -> (u32, u32) {91+fn out_size(width: u32, height: u32, output: RgbOutput) -> (u32, u32) {
92- match max_width {92+ let (w, h) = (u64::from(width), u64::from(height));
93- Some(max) if w > max => {93+ let mw = output.max_width.map_or(w, u64::from).min(w);
94- let even = 2 * ((u64::from(h) * u64::from(max) + u64::from(w)) / (2 * u64::from(w)));94+ let mh = output.max_height.map_or(h, u64::from).min(h);
95- (max, (even as u32).clamp(2, h))95+ if mw == w && mh == h {
96- }96+ (width, height)
97- _ => (w, h),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)
98 }102 }
99 }103 }
100 104
@@ -141,20 +145,28 @@ fn decode_i420(codec: Codec, units: &[&[u8]]) -> (Backend, Vec<I420>, Duration)
141 fn decode_rgb(145 fn decode_rgb(
142 codec: Codec,146 codec: Codec,
143 units: &[&[u8]],147 units: &[&[u8]],
144- output: RgbOutput,148+ outputs: &[RgbOutput],
145 ) -> (Backend, Vec<RgbPicture>, Duration) {149 ) -> (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");
147 let backend = decoder.backend();151 let backend = decoder.backend();
148 let mut out = Vec::new();152 let mut out = Vec::new();
149 let start = Instant::now();153 let start = Instant::now();
150 for (pts, unit) in units.iter().enumerate() {154 for (pts, unit) in units.iter().enumerate() {
151 decoder.decode(unit, pts as i64).expect("decode");155 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+ };
153 out.push(picture);161 out.push(picture);
154 }162 }
155 }163 }
156 decoder.flush().expect("flush");164 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+ };
158 out.push(picture);170 out.push(picture);
159 }171 }
160 (backend, out, start.elapsed())172 (backend, out, start.elapsed())
crates/ec-hw/src/hw.rsmodified+18 -0
@@ -1071,6 +1071,14 @@ pub struct RgbOutput {
1071 pub order: RgbOrder,1071 pub order: RgbOrder,
1072 /// Scale pictures wider than this down to it, keeping the aspect ratio.1072 /// Scale pictures wider than this down to it, keeping the aspect ratio.
1073 pub max_width: Option<u32>,1073 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+ }
1074 }1082 }
1075 1083
1076 /// A hardware decoder on whichever backend opened.1084 /// A hardware decoder on whichever backend opened.
@@ -1222,6 +1230,16 @@ impl HwDecoder {
1222 Ok(self.next_rgb_into(&mut dst)?.map(|ts| (ts, dst)))1230 Ok(self.next_rgb_into(&mut dst)?.map(|ts| (ts, dst)))
1223 }1231 }
1224 1232
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+
1225 /// [`HwDecoder::next_rgb`] into `dst`, reusing its allocation across pictures; returns the timestamp.1243 /// [`HwDecoder::next_rgb`] into `dst`, reusing its allocation across pictures; returns the timestamp.
1226 pub fn next_rgb_into(&mut self, dst: &mut RgbPicture) -> Result<Option<i64>> {1244 pub fn next_rgb_into(&mut self, dst: &mut RgbPicture) -> Result<Option<i64>> {
1227 let output = self1245 let output = self
crates/ec-hw/src/output.rsmodified+3 -3
@@ -108,7 +108,7 @@ impl HwPicture {
108 .unwrap_or_else(|| default_matrix(i420.height));108 .unwrap_or_else(|| default_matrix(i420.height));
109 *dst = i420_to_rgb(109 *dst = i420_to_rgb(
110 &i420,110 &i420,
111- output.max_width,111+ output.bound(),
112 output.order == RgbOrder::Bgra,112 output.order == RgbOrder::Bgra,
113 matrix,113 matrix,
114 );114 );
@@ -340,10 +340,10 @@ impl VkPicture {
340 "RGB of 10-bit output",340 "RGB of 10-bit output",
341 ));341 ));
342 }342 }
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);
344 return Ok(());344 return Ok(());
345 }345 }
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());
347 self.surfaces.rgb(347 self.surfaces.rgb(
348 self.slot.index,348 self.slot.index,
349 (0, 0, self.width, self.height),349 (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 {
122 }122 }
123 }123 }
124 124
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`.
126 pub(crate) fn i420_to_rgb(126 pub(crate) fn i420_to_rgb(
127 frame: &I420,127 frame: &I420,
128- max_width: Option<u32>,128+ bound: (Option<u32>, Option<u32>),
129 bgra: bool,129 bgra: bool,
130 matrix: Matrix,130 matrix: Matrix,
131 ) -> RgbPicture {131 ) -> RgbPicture {
132- let (w, h) = rgb_size(frame.width, frame.height, max_width);132+ let (w, h) = rgb_size(frame.width, frame.height, bound);
133 let data = ec_dsp::rgb::i420_to_packed(133 let data = ec_dsp::rgb::i420_to_packed(
134 &frame.y,134 &frame.y,
135 &frame.u,135 &frame.u,
@@ -142,16 +142,21 @@ pub(crate) fn i420_to_rgb(
142 RgbPicture::new(data, w, h)142 RgbPicture::new(data, w, h)
143 }143 }
144 144
145-/// Output size of an RGB picture of `width` x `height`: scaled to `max_width` keeping the aspect, height rounded to the nearest even.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, max_width: Option<u32>) -> (u32, u32) {146+pub(crate) fn rgb_size(width: u32, height: u32, bound: (Option<u32>, Option<u32>)) -> (u32, u32) {
147- match max_width {147+ let (w, h) = (u64::from(width), u64::from(height));
148- Some(max) if max > 0 && width > max => {148+ let limit = |max: Option<u32>, full: u64| max.filter(|&m| m > 0).map_or(full, u64::from).min(full);
149- let (w, h) = (u64::from(width), u64::from(height));149+ let (mw, mh) = (limit(bound.0, w), limit(bound.1, h));
150- let max = u64::from(max);150+ if mw == w && mh == h {
151- let even = 2 * ((h * max + w) / (2 * w));151+ return (width, height);
152- (max as u32, even.clamp(2, h) as u32)152+ }
153- }153+ if mw * h <= mh * w {
154- _ => (width, height),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)
155 }160 }
156 }161 }
157 162