forge
Explore
Log in

Don't you dare go Hollow

ec-hw: HwDecoder::next_rgb_into / HwPicture::rgb_into write RGB into a caller buffer, reusing its allocation

Browse files at this commit

c86cfc1a3fe48595d910b3309300b8740159c911Ahmet Kaan GümüşParent: 299aff6
crates/ec-hw/src/hw.rsmodified+10 -1
@@ -1171,12 +1171,21 @@ impl HwDecoder {
11711171
11721172 /// The next picture in display order, as packed RGB with its timestamp. An error on a decoder not opened with [`HwDecoder::open_rgb`].
11731173 pub fn next_rgb(&mut self) -> Result<Option<(i64, RgbPicture)>> {
1174+ let mut dst = RgbPicture::default();
1175+ Ok(self.next_rgb_into(&mut dst)?.map(|ts| (ts, dst)))
1176+ }
1177+
1178+ /// [`HwDecoder::next_rgb`] into `dst`, reusing its allocation across pictures; returns the timestamp.
1179+ pub fn next_rgb_into(&mut self, dst: &mut RgbPicture) -> Result<Option<i64>> {
11741180 let output = self
11751181 .rgb
11761182 .ok_or_else(|| Error::config("next_rgb on a decoder not opened with open_rgb"))?;
11771183 match self.pull()? {
11781184 None => Ok(None),
1179- Some(picture) => Ok(Some((picture.timestamp, picture.to_rgb(&output)?))),
1185+ Some(picture) => {
1186+ picture.rgb_into(&output, dst)?;
1187+ Ok(Some(picture.timestamp))
1188+ }
11801189 }
11811190 }
11821191
crates/ec-hw/src/output.rsmodified+29 -19
@@ -90,8 +90,15 @@ impl HwPicture {
9090
9191 /// Packed RGB, scaled and byte-ordered as `output` asks.
9292 pub fn to_rgb(&self, output: &RgbOutput) -> Result<RgbPicture> {
93+ let mut dst = RgbPicture::default();
94+ self.rgb_into(output, &mut dst)?;
95+ Ok(dst)
96+ }
97+
98+ /// [`HwPicture::to_rgb`] into `dst`, reusing its allocation.
99+ pub fn rgb_into(&self, output: &RgbOutput, dst: &mut RgbPicture) -> Result<()> {
93100 match &self.source {
94- Source::Vk(picture) => picture.rgb(output),
101+ Source::Vk(picture) => picture.rgb(output, dst),
95102 #[cfg(target_os = "linux")]
96103 Source::Va(frame) => {
97104 let i420 = frame.to_i420()?;
@@ -99,12 +106,13 @@ impl HwPicture {
99106 .colour
100107 .map(|c| c.description.matrix)
101108 .unwrap_or_else(|| default_matrix(i420.height));
102- Ok(i420_to_rgb(
109+ *dst = i420_to_rgb(
103110 &i420,
104111 output.max_width,
105112 output.order == RgbOrder::Bgra,
106113 matrix,
107- ))
114+ );
115+ Ok(())
108116 }
109117 }
110118 }
@@ -194,21 +202,23 @@ impl Surfaces {
194202 out: (u32, u32),
195203 bgra: bool,
196204 coeffs: [i32; 5],
197- ) -> Result<Vec<u8>> {
198- Ok(match self {
205+ dst: &mut Vec<u8>,
206+ ) -> Result<()> {
207+ match self {
199208 Surfaces::H264(s) => s
200209 .borrow_mut()
201- .read_output_rgb(slot, crop, out, bgra, coeffs)?,
210+ .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?,
202211 Surfaces::H265(s) => s
203212 .borrow_mut()
204- .read_output_rgb(slot, crop, out, bgra, coeffs)?,
213+ .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?,
205214 Surfaces::Av1(s) => s
206215 .borrow_mut()
207- .read_output_rgb(slot, crop, out, bgra, coeffs)?,
216+ .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?,
208217 Surfaces::Vp9(s) => s
209218 .borrow_mut()
210- .read_output_rgb(slot, crop, out, bgra, coeffs)?,
211- })
219+ .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?,
220+ }
221+ Ok(())
212222 }
213223
214224 #[cfg(target_os = "linux")]
@@ -321,7 +331,7 @@ impl VkPicture {
321331 })
322332 }
323333
324- fn rgb(&self, output: &RgbOutput) -> Result<RgbPicture> {
334+ fn rgb(&self, output: &RgbOutput, dst: &mut RgbPicture) -> Result<()> {
325335 let bgra = output.order == RgbOrder::Bgra;
326336 if self.grain.is_some() {
327337 if self.bit_depth != 8 {
@@ -330,22 +340,22 @@ impl VkPicture {
330340 "RGB of 10-bit output",
331341 ));
332342 }
333- return Ok(i420_to_rgb(
334- &self.i420()?,
335- output.max_width,
336- bgra,
337- self.matrix,
338- ));
343+ *dst = i420_to_rgb(&self.i420()?, output.max_width, bgra, self.matrix);
344+ return Ok(());
339345 }
340346 let (w, h) = rgb_size(self.width, self.height, output.max_width);
341- let data = self.surfaces.rgb(
347+ self.surfaces.rgb(
342348 self.slot.index,
343349 (0, 0, self.width, self.height),
344350 (w, h),
345351 bgra,
346352 yuv_to_rgb(self.matrix).words(),
353+ &mut dst.data,
347354 )?;
348- Ok(RgbPicture::new(data, w, h))
355+ dst.width = w;
356+ dst.height = h;
357+ dst.stride = w * 4;
358+ Ok(())
349359 }
350360
351361 #[cfg(target_os = "linux")]
crates/ec-hw/src/picture.rsmodified+1 -1
@@ -35,7 +35,7 @@ pub struct I420 {
3535 }
3636
3737 /// A packed 32-bit RGB picture, alpha 255, `stride` bytes per row.
38-#[derive(Debug, Clone, PartialEq, Eq)]
38+#[derive(Debug, Clone, Default, PartialEq, Eq)]
3939 pub struct RgbPicture {
4040 /// Pixels, `stride * height` bytes.
4141 pub data: Vec<u8>,
shims/vk-video-ffi/src/av1_decode.rsmodified+4 -3
@@ -922,7 +922,7 @@ impl Av1DecodeSession {
922922 self.dec.export(slot as usize, (width, height))
923923 }
924924
925- /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order.
925+ /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order. Written into `dst`, whose allocation is reused.
926926 pub fn read_output_rgb(
927927 &mut self,
928928 slot: u32,
@@ -930,8 +930,9 @@ impl Av1DecodeSession {
930930 out: (u32, u32),
931931 bgra: bool,
932932 coeffs: [i32; 5],
933- ) -> Result<Vec<u8>, Error> {
934- self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs)
933+ dst: &mut Vec<u8>,
934+ ) -> Result<(), Error> {
935+ self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs, dst)
935936 }
936937 }
937938
shims/vk-video-ffi/src/h264_decode.rsmodified+4 -3
@@ -513,7 +513,7 @@ impl H264DecodeSession {
513513 self.dec.export(slot as usize, (width, height))
514514 }
515515
516- /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order.
516+ /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order. Written into `dst`, whose allocation is reused.
517517 pub fn read_output_rgb(
518518 &mut self,
519519 slot: u32,
@@ -521,8 +521,9 @@ impl H264DecodeSession {
521521 out: (u32, u32),
522522 bgra: bool,
523523 coeffs: [i32; 5],
524- ) -> Result<Vec<u8>, Error> {
525- self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs)
524+ dst: &mut Vec<u8>,
525+ ) -> Result<(), Error> {
526+ self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs, dst)
526527 }
527528 }
528529
shims/vk-video-ffi/src/h265_decode.rsmodified+4 -3
@@ -727,7 +727,7 @@ impl H265DecodeSession {
727727 self.dec.export(slot as usize, (width, height))
728728 }
729729
730- /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order.
730+ /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order. Written into `dst`, whose allocation is reused.
731731 pub fn read_output_rgb(
732732 &mut self,
733733 slot: u32,
@@ -735,8 +735,9 @@ impl H265DecodeSession {
735735 out: (u32, u32),
736736 bgra: bool,
737737 coeffs: [i32; 5],
738- ) -> Result<Vec<u8>, Error> {
739- self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs)
738+ dst: &mut Vec<u8>,
739+ ) -> Result<(), Error> {
740+ self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs, dst)
740741 }
741742 }
742743
shims/vk-video-ffi/src/session.rsmodified+5 -2
@@ -1874,7 +1874,8 @@ impl Decoder {
18741874 out: (u32, u32),
18751875 bgra: bool,
18761876 coeffs: [i32; 5],
1877- ) -> Result<Vec<u8>, Error> {
1877+ dst: &mut Vec<u8>,
1878+ ) -> Result<(), Error> {
18781879 self.check_window(crop)?;
18791880 let (x, y, width, height) = crop;
18801881 if out.0 == 0 || out.1 == 0 || out.0 > width || out.1 > height {
@@ -1931,7 +1932,9 @@ impl Decoder {
19311932 self.graphics.end()?;
19321933 self.graphics.submit()?;
19331934 let len = out.0 as usize * out.1 as usize * 4;
1934- Ok(self.rgb_out.bytes(d, len)?.to_vec())
1935+ dst.clear();
1936+ dst.extend_from_slice(self.rgb_out.bytes(d, len)?);
1937+ Ok(())
19351938 }
19361939
19371940 /// The DMA-BUF of ring slot `slot`, `width` x `height` from its top-left: a second handle on the slot's memory, which the same slot returns every time.
shims/vk-video-ffi/src/vp9_decode.rsmodified+4 -3
@@ -457,7 +457,7 @@ impl Vp9DecodeSession {
457457 self.dec.export(slot as usize, (width, height))
458458 }
459459
460- /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order.
460+ /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order. Written into `dst`, whose allocation is reused.
461461 pub fn read_output_rgb(
462462 &mut self,
463463 slot: u32,
@@ -465,8 +465,9 @@ impl Vp9DecodeSession {
465465 out: (u32, u32),
466466 bgra: bool,
467467 coeffs: [i32; 5],
468- ) -> Result<Vec<u8>, Error> {
469- self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs)
468+ dst: &mut Vec<u8>,
469+ ) -> Result<(), Error> {
470+ self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs, dst)
470471 }
471472 }
472473