ec-hw: HwDecoder::next_rgb_into / HwPicture::rgb_into write RGB into a caller buffer, reusing its allocation
Browse files at this commitc86cfc1a3fe48595d910b3309300b8740159c911Parent: 299aff68 changed files +61 -35
- crates/ec-hw/src/hw.rs +10 -1
- crates/ec-hw/src/output.rs +29 -19
- crates/ec-hw/src/picture.rs +1 -1
- shims/vk-video-ffi/src/av1_decode.rs +4 -3
- shims/vk-video-ffi/src/h264_decode.rs +4 -3
- shims/vk-video-ffi/src/h265_decode.rs +4 -3
- shims/vk-video-ffi/src/session.rs +5 -2
- shims/vk-video-ffi/src/vp9_decode.rs +4 -3
crates/ec-hw/src/hw.rsmodified+10 -1
| @@ -1171,12 +1171,21 @@ impl HwDecoder { | ||
| 1171 | 1171 | |
| 1172 | 1172 | /// The next picture in display order, as packed RGB with its timestamp. An error on a decoder not opened with [`HwDecoder::open_rgb`]. |
| 1173 | 1173 | 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>> { | |
| 1174 | 1180 | let output = self |
| 1175 | 1181 | .rgb |
| 1176 | 1182 | .ok_or_else(|| Error::config("next_rgb on a decoder not opened with open_rgb"))?; |
| 1177 | 1183 | match self.pull()? { |
| 1178 | 1184 | 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 | + } | |
| 1180 | 1189 | } |
| 1181 | 1190 | } |
| 1182 | 1191 |
crates/ec-hw/src/output.rsmodified+29 -19
| @@ -90,8 +90,15 @@ impl HwPicture { | ||
| 90 | 90 | |
| 91 | 91 | /// Packed RGB, scaled and byte-ordered as `output` asks. |
| 92 | 92 | 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<()> { | |
| 93 | 100 | match &self.source { |
| 94 | - Source::Vk(picture) => picture.rgb(output), | |
| 101 | + Source::Vk(picture) => picture.rgb(output, dst), | |
| 95 | 102 | #[cfg(target_os = "linux")] |
| 96 | 103 | Source::Va(frame) => { |
| 97 | 104 | let i420 = frame.to_i420()?; |
| @@ -99,12 +106,13 @@ impl HwPicture { | ||
| 99 | 106 | .colour |
| 100 | 107 | .map(|c| c.description.matrix) |
| 101 | 108 | .unwrap_or_else(|| default_matrix(i420.height)); |
| 102 | - Ok(i420_to_rgb( | |
| 109 | + *dst = i420_to_rgb( | |
| 103 | 110 | &i420, |
| 104 | 111 | output.max_width, |
| 105 | 112 | output.order == RgbOrder::Bgra, |
| 106 | 113 | matrix, |
| 107 | - )) | |
| 114 | + ); | |
| 115 | + Ok(()) | |
| 108 | 116 | } |
| 109 | 117 | } |
| 110 | 118 | } |
| @@ -194,21 +202,23 @@ impl Surfaces { | ||
| 194 | 202 | out: (u32, u32), |
| 195 | 203 | bgra: bool, |
| 196 | 204 | coeffs: [i32; 5], |
| 197 | - ) -> Result<Vec<u8>> { | |
| 198 | - Ok(match self { | |
| 205 | + dst: &mut Vec<u8>, | |
| 206 | + ) -> Result<()> { | |
| 207 | + match self { | |
| 199 | 208 | Surfaces::H264(s) => s |
| 200 | 209 | .borrow_mut() |
| 201 | - .read_output_rgb(slot, crop, out, bgra, coeffs)?, | |
| 210 | + .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?, | |
| 202 | 211 | Surfaces::H265(s) => s |
| 203 | 212 | .borrow_mut() |
| 204 | - .read_output_rgb(slot, crop, out, bgra, coeffs)?, | |
| 213 | + .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?, | |
| 205 | 214 | Surfaces::Av1(s) => s |
| 206 | 215 | .borrow_mut() |
| 207 | - .read_output_rgb(slot, crop, out, bgra, coeffs)?, | |
| 216 | + .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?, | |
| 208 | 217 | Surfaces::Vp9(s) => s |
| 209 | 218 | .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(()) | |
| 212 | 222 | } |
| 213 | 223 | |
| 214 | 224 | #[cfg(target_os = "linux")] |
| @@ -321,7 +331,7 @@ impl VkPicture { | ||
| 321 | 331 | }) |
| 322 | 332 | } |
| 323 | 333 | |
| 324 | - fn rgb(&self, output: &RgbOutput) -> Result<RgbPicture> { | |
| 334 | + fn rgb(&self, output: &RgbOutput, dst: &mut RgbPicture) -> Result<()> { | |
| 325 | 335 | let bgra = output.order == RgbOrder::Bgra; |
| 326 | 336 | if self.grain.is_some() { |
| 327 | 337 | if self.bit_depth != 8 { |
| @@ -330,22 +340,22 @@ impl VkPicture { | ||
| 330 | 340 | "RGB of 10-bit output", |
| 331 | 341 | )); |
| 332 | 342 | } |
| 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(()); | |
| 339 | 345 | } |
| 340 | 346 | let (w, h) = rgb_size(self.width, self.height, output.max_width); |
| 341 | - let data = self.surfaces.rgb( | |
| 347 | + self.surfaces.rgb( | |
| 342 | 348 | self.slot.index, |
| 343 | 349 | (0, 0, self.width, self.height), |
| 344 | 350 | (w, h), |
| 345 | 351 | bgra, |
| 346 | 352 | yuv_to_rgb(self.matrix).words(), |
| 353 | + &mut dst.data, | |
| 347 | 354 | )?; |
| 348 | - Ok(RgbPicture::new(data, w, h)) | |
| 355 | + dst.width = w; | |
| 356 | + dst.height = h; | |
| 357 | + dst.stride = w * 4; | |
| 358 | + Ok(()) | |
| 349 | 359 | } |
| 350 | 360 | |
| 351 | 361 | #[cfg(target_os = "linux")] |
crates/ec-hw/src/picture.rsmodified+1 -1
| @@ -35,7 +35,7 @@ pub struct I420 { | ||
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /// 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)] | |
| 39 | 39 | pub struct RgbPicture { |
| 40 | 40 | /// Pixels, `stride * height` bytes. |
| 41 | 41 | pub data: Vec<u8>, |
shims/vk-video-ffi/src/av1_decode.rsmodified+4 -3
| @@ -922,7 +922,7 @@ impl Av1DecodeSession { | ||
| 922 | 922 | self.dec.export(slot as usize, (width, height)) |
| 923 | 923 | } |
| 924 | 924 | |
| 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. | |
| 926 | 926 | pub fn read_output_rgb( |
| 927 | 927 | &mut self, |
| 928 | 928 | slot: u32, |
| @@ -930,8 +930,9 @@ impl Av1DecodeSession { | ||
| 930 | 930 | out: (u32, u32), |
| 931 | 931 | bgra: bool, |
| 932 | 932 | 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) | |
| 935 | 936 | } |
| 936 | 937 | } |
| 937 | 938 |
shims/vk-video-ffi/src/h264_decode.rsmodified+4 -3
| @@ -513,7 +513,7 @@ impl H264DecodeSession { | ||
| 513 | 513 | self.dec.export(slot as usize, (width, height)) |
| 514 | 514 | } |
| 515 | 515 | |
| 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. | |
| 517 | 517 | pub fn read_output_rgb( |
| 518 | 518 | &mut self, |
| 519 | 519 | slot: u32, |
| @@ -521,8 +521,9 @@ impl H264DecodeSession { | ||
| 521 | 521 | out: (u32, u32), |
| 522 | 522 | bgra: bool, |
| 523 | 523 | 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) | |
| 526 | 527 | } |
| 527 | 528 | } |
| 528 | 529 |
shims/vk-video-ffi/src/h265_decode.rsmodified+4 -3
| @@ -727,7 +727,7 @@ impl H265DecodeSession { | ||
| 727 | 727 | self.dec.export(slot as usize, (width, height)) |
| 728 | 728 | } |
| 729 | 729 | |
| 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. | |
| 731 | 731 | pub fn read_output_rgb( |
| 732 | 732 | &mut self, |
| 733 | 733 | slot: u32, |
| @@ -735,8 +735,9 @@ impl H265DecodeSession { | ||
| 735 | 735 | out: (u32, u32), |
| 736 | 736 | bgra: bool, |
| 737 | 737 | 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) | |
| 740 | 741 | } |
| 741 | 742 | } |
| 742 | 743 |
shims/vk-video-ffi/src/session.rsmodified+5 -2
| @@ -1874,7 +1874,8 @@ impl Decoder { | ||
| 1874 | 1874 | out: (u32, u32), |
| 1875 | 1875 | bgra: bool, |
| 1876 | 1876 | coeffs: [i32; 5], |
| 1877 | - ) -> Result<Vec<u8>, Error> { | |
| 1877 | + dst: &mut Vec<u8>, | |
| 1878 | + ) -> Result<(), Error> { | |
| 1878 | 1879 | self.check_window(crop)?; |
| 1879 | 1880 | let (x, y, width, height) = crop; |
| 1880 | 1881 | if out.0 == 0 || out.1 == 0 || out.0 > width || out.1 > height { |
| @@ -1931,7 +1932,9 @@ impl Decoder { | ||
| 1931 | 1932 | self.graphics.end()?; |
| 1932 | 1933 | self.graphics.submit()?; |
| 1933 | 1934 | 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(()) | |
| 1935 | 1938 | } |
| 1936 | 1939 | |
| 1937 | 1940 | /// 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 { | ||
| 457 | 457 | self.dec.export(slot as usize, (width, height)) |
| 458 | 458 | } |
| 459 | 459 | |
| 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. | |
| 461 | 461 | pub fn read_output_rgb( |
| 462 | 462 | &mut self, |
| 463 | 463 | slot: u32, |
| @@ -465,8 +465,9 @@ impl Vp9DecodeSession { | ||
| 465 | 465 | out: (u32, u32), |
| 466 | 466 | bgra: bool, |
| 467 | 467 | 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) | |
| 470 | 471 | } |
| 471 | 472 | } |
| 472 | 473 |