From c86cfc1a3fe48595d910b3309300b8740159c911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 26 Nov 2026 10:45:08 +0300 Subject: [PATCH] ec-hw: HwDecoder::next_rgb_into / HwPicture::rgb_into write RGB into a caller buffer, reusing its allocation --- crates/ec-hw/src/hw.rs | 11 +++++- crates/ec-hw/src/output.rs | 48 ++++++++++++++++----------- crates/ec-hw/src/picture.rs | 2 +- shims/vk-video-ffi/src/av1_decode.rs | 7 ++-- shims/vk-video-ffi/src/h264_decode.rs | 7 ++-- shims/vk-video-ffi/src/h265_decode.rs | 7 ++-- shims/vk-video-ffi/src/session.rs | 7 ++-- shims/vk-video-ffi/src/vp9_decode.rs | 7 ++-- 8 files changed, 61 insertions(+), 35 deletions(-) diff --git a/crates/ec-hw/src/hw.rs b/crates/ec-hw/src/hw.rs index fcc7926d..c81875de 100644 --- a/crates/ec-hw/src/hw.rs +++ b/crates/ec-hw/src/hw.rs @@ -1171,12 +1171,21 @@ impl HwDecoder { /// The next picture in display order, as packed RGB with its timestamp. An error on a decoder not opened with [`HwDecoder::open_rgb`]. pub fn next_rgb(&mut self) -> Result> { + let mut dst = RgbPicture::default(); + Ok(self.next_rgb_into(&mut dst)?.map(|ts| (ts, dst))) + } + + /// [`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 .rgb .ok_or_else(|| Error::config("next_rgb on a decoder not opened with open_rgb"))?; match self.pull()? { None => Ok(None), - Some(picture) => Ok(Some((picture.timestamp, picture.to_rgb(&output)?))), + Some(picture) => { + picture.rgb_into(&output, dst)?; + Ok(Some(picture.timestamp)) + } } } diff --git a/crates/ec-hw/src/output.rs b/crates/ec-hw/src/output.rs index 6f49c7ff..594cd37c 100644 --- a/crates/ec-hw/src/output.rs +++ b/crates/ec-hw/src/output.rs @@ -90,8 +90,15 @@ impl HwPicture { /// Packed RGB, scaled and byte-ordered as `output` asks. pub fn to_rgb(&self, output: &RgbOutput) -> Result { + let mut dst = RgbPicture::default(); + self.rgb_into(output, &mut dst)?; + Ok(dst) + } + + /// [`HwPicture::to_rgb`] into `dst`, reusing its allocation. + pub fn rgb_into(&self, output: &RgbOutput, dst: &mut RgbPicture) -> Result<()> { match &self.source { - Source::Vk(picture) => picture.rgb(output), + Source::Vk(picture) => picture.rgb(output, dst), #[cfg(target_os = "linux")] Source::Va(frame) => { let i420 = frame.to_i420()?; @@ -99,12 +106,13 @@ impl HwPicture { .colour .map(|c| c.description.matrix) .unwrap_or_else(|| default_matrix(i420.height)); - Ok(i420_to_rgb( + *dst = i420_to_rgb( &i420, output.max_width, output.order == RgbOrder::Bgra, matrix, - )) + ); + Ok(()) } } } @@ -194,21 +202,23 @@ impl Surfaces { out: (u32, u32), bgra: bool, coeffs: [i32; 5], - ) -> Result> { - Ok(match self { + dst: &mut Vec, + ) -> Result<()> { + match self { Surfaces::H264(s) => s .borrow_mut() - .read_output_rgb(slot, crop, out, bgra, coeffs)?, + .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?, Surfaces::H265(s) => s .borrow_mut() - .read_output_rgb(slot, crop, out, bgra, coeffs)?, + .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?, Surfaces::Av1(s) => s .borrow_mut() - .read_output_rgb(slot, crop, out, bgra, coeffs)?, + .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?, Surfaces::Vp9(s) => s .borrow_mut() - .read_output_rgb(slot, crop, out, bgra, coeffs)?, - }) + .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?, + } + Ok(()) } #[cfg(target_os = "linux")] @@ -321,7 +331,7 @@ impl VkPicture { }) } - fn rgb(&self, output: &RgbOutput) -> Result { + fn rgb(&self, output: &RgbOutput, dst: &mut RgbPicture) -> Result<()> { let bgra = output.order == RgbOrder::Bgra; if self.grain.is_some() { if self.bit_depth != 8 { @@ -330,22 +340,22 @@ impl VkPicture { "RGB of 10-bit output", )); } - return Ok(i420_to_rgb( - &self.i420()?, - output.max_width, - bgra, - self.matrix, - )); + *dst = i420_to_rgb(&self.i420()?, output.max_width, bgra, self.matrix); + return Ok(()); } let (w, h) = rgb_size(self.width, self.height, output.max_width); - let data = self.surfaces.rgb( + self.surfaces.rgb( self.slot.index, (0, 0, self.width, self.height), (w, h), bgra, yuv_to_rgb(self.matrix).words(), + &mut dst.data, )?; - Ok(RgbPicture::new(data, w, h)) + dst.width = w; + dst.height = h; + dst.stride = w * 4; + Ok(()) } #[cfg(target_os = "linux")] diff --git a/crates/ec-hw/src/picture.rs b/crates/ec-hw/src/picture.rs index c3ceffc4..73f78ca3 100644 --- a/crates/ec-hw/src/picture.rs +++ b/crates/ec-hw/src/picture.rs @@ -35,7 +35,7 @@ pub struct I420 { } /// A packed 32-bit RGB picture, alpha 255, `stride` bytes per row. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct RgbPicture { /// Pixels, `stride * height` bytes. pub data: Vec, diff --git a/shims/vk-video-ffi/src/av1_decode.rs b/shims/vk-video-ffi/src/av1_decode.rs index 30b97e92..ab171f0b 100644 --- a/shims/vk-video-ffi/src/av1_decode.rs +++ b/shims/vk-video-ffi/src/av1_decode.rs @@ -922,7 +922,7 @@ impl Av1DecodeSession { self.dec.export(slot as usize, (width, height)) } - /// 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. + /// 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. pub fn read_output_rgb( &mut self, slot: u32, @@ -930,8 +930,9 @@ impl Av1DecodeSession { out: (u32, u32), bgra: bool, coeffs: [i32; 5], - ) -> Result, Error> { - self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs) + dst: &mut Vec, + ) -> Result<(), Error> { + self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs, dst) } } diff --git a/shims/vk-video-ffi/src/h264_decode.rs b/shims/vk-video-ffi/src/h264_decode.rs index fc0cd9fe..0d432aa6 100644 --- a/shims/vk-video-ffi/src/h264_decode.rs +++ b/shims/vk-video-ffi/src/h264_decode.rs @@ -513,7 +513,7 @@ impl H264DecodeSession { self.dec.export(slot as usize, (width, height)) } - /// 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. + /// 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. pub fn read_output_rgb( &mut self, slot: u32, @@ -521,8 +521,9 @@ impl H264DecodeSession { out: (u32, u32), bgra: bool, coeffs: [i32; 5], - ) -> Result, Error> { - self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs) + dst: &mut Vec, + ) -> Result<(), Error> { + self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs, dst) } } diff --git a/shims/vk-video-ffi/src/h265_decode.rs b/shims/vk-video-ffi/src/h265_decode.rs index 78b75d56..a637defd 100644 --- a/shims/vk-video-ffi/src/h265_decode.rs +++ b/shims/vk-video-ffi/src/h265_decode.rs @@ -727,7 +727,7 @@ impl H265DecodeSession { self.dec.export(slot as usize, (width, height)) } - /// 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. + /// 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. pub fn read_output_rgb( &mut self, slot: u32, @@ -735,8 +735,9 @@ impl H265DecodeSession { out: (u32, u32), bgra: bool, coeffs: [i32; 5], - ) -> Result, Error> { - self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs) + dst: &mut Vec, + ) -> Result<(), Error> { + self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs, dst) } } diff --git a/shims/vk-video-ffi/src/session.rs b/shims/vk-video-ffi/src/session.rs index e95c24b2..930b7c49 100644 --- a/shims/vk-video-ffi/src/session.rs +++ b/shims/vk-video-ffi/src/session.rs @@ -1874,7 +1874,8 @@ impl Decoder { out: (u32, u32), bgra: bool, coeffs: [i32; 5], - ) -> Result, Error> { + dst: &mut Vec, + ) -> Result<(), Error> { self.check_window(crop)?; let (x, y, width, height) = crop; if out.0 == 0 || out.1 == 0 || out.0 > width || out.1 > height { @@ -1931,7 +1932,9 @@ impl Decoder { self.graphics.end()?; self.graphics.submit()?; let len = out.0 as usize * out.1 as usize * 4; - Ok(self.rgb_out.bytes(d, len)?.to_vec()) + dst.clear(); + dst.extend_from_slice(self.rgb_out.bytes(d, len)?); + Ok(()) } /// 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. diff --git a/shims/vk-video-ffi/src/vp9_decode.rs b/shims/vk-video-ffi/src/vp9_decode.rs index d8f81b95..49309f09 100644 --- a/shims/vk-video-ffi/src/vp9_decode.rs +++ b/shims/vk-video-ffi/src/vp9_decode.rs @@ -457,7 +457,7 @@ impl Vp9DecodeSession { self.dec.export(slot as usize, (width, height)) } - /// 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. + /// 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. pub fn read_output_rgb( &mut self, slot: u32, @@ -465,8 +465,9 @@ impl Vp9DecodeSession { out: (u32, u32), bgra: bool, coeffs: [i32; 5], - ) -> Result, Error> { - self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs) + dst: &mut Vec, + ) -> Result<(), Error> { + self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs, dst) } }