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 | /// The next picture in display order, as packed RGB with its timestamp. An error on a decoder not opened with [`HwDecoder::open_rgb`]. | 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 | pub fn next_rgb(&mut self) -> Result<Option<(i64, RgbPicture)>> { | 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 | let output = self | 1180 | let output = self |
| 1175 | .rgb | 1181 | .rgb |
| 1176 | .ok_or_else(|| Error::config("next_rgb on a decoder not opened with open_rgb"))?; | 1182 | .ok_or_else(|| Error::config("next_rgb on a decoder not opened with open_rgb"))?; |
| 1177 | match self.pull()? { | 1183 | match self.pull()? { |
| 1178 | None => Ok(None), | 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 | /// Packed RGB, scaled and byte-ordered as `output` asks. | 91 | /// Packed RGB, scaled and byte-ordered as `output` asks. |
| 92 | pub fn to_rgb(&self, output: &RgbOutput) -> Result<RgbPicture> { | 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 | match &self.source { | 100 | match &self.source { |
| 94 | - Source::Vk(picture) => picture.rgb(output), | 101 | + Source::Vk(picture) => picture.rgb(output, dst), |
| 95 | #[cfg(target_os = "linux")] | 102 | #[cfg(target_os = "linux")] |
| 96 | Source::Va(frame) => { | 103 | Source::Va(frame) => { |
| 97 | let i420 = frame.to_i420()?; | 104 | let i420 = frame.to_i420()?; |
| @@ -99,12 +106,13 @@ impl HwPicture { | |||
| 99 | .colour | 106 | .colour |
| 100 | .map(|c| c.description.matrix) | 107 | .map(|c| c.description.matrix) |
| 101 | .unwrap_or_else(|| default_matrix(i420.height)); | 108 | .unwrap_or_else(|| default_matrix(i420.height)); |
| 102 | - Ok(i420_to_rgb( | 109 | + *dst = i420_to_rgb( |
| 103 | &i420, | 110 | &i420, |
| 104 | output.max_width, | 111 | output.max_width, |
| 105 | output.order == RgbOrder::Bgra, | 112 | output.order == RgbOrder::Bgra, |
| 106 | matrix, | 113 | matrix, |
| 107 | - )) | 114 | + ); |
| 115 | + Ok(()) | ||
| 108 | } | 116 | } |
| 109 | } | 117 | } |
| 110 | } | 118 | } |
| @@ -194,21 +202,23 @@ impl Surfaces { | |||
| 194 | out: (u32, u32), | 202 | out: (u32, u32), |
| 195 | bgra: bool, | 203 | bgra: bool, |
| 196 | coeffs: [i32; 5], | 204 | coeffs: [i32; 5], |
| 197 | - ) -> Result<Vec<u8>> { | 205 | + dst: &mut Vec<u8>, |
| 198 | - Ok(match self { | 206 | + ) -> Result<()> { |
| 207 | + match self { | ||
| 199 | Surfaces::H264(s) => s | 208 | Surfaces::H264(s) => s |
| 200 | .borrow_mut() | 209 | .borrow_mut() |
| 201 | - .read_output_rgb(slot, crop, out, bgra, coeffs)?, | 210 | + .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?, |
| 202 | Surfaces::H265(s) => s | 211 | Surfaces::H265(s) => s |
| 203 | .borrow_mut() | 212 | .borrow_mut() |
| 204 | - .read_output_rgb(slot, crop, out, bgra, coeffs)?, | 213 | + .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?, |
| 205 | Surfaces::Av1(s) => s | 214 | Surfaces::Av1(s) => s |
| 206 | .borrow_mut() | 215 | .borrow_mut() |
| 207 | - .read_output_rgb(slot, crop, out, bgra, coeffs)?, | 216 | + .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?, |
| 208 | Surfaces::Vp9(s) => s | 217 | Surfaces::Vp9(s) => s |
| 209 | .borrow_mut() | 218 | .borrow_mut() |
| 210 | - .read_output_rgb(slot, crop, out, bgra, coeffs)?, | 219 | + .read_output_rgb(slot, crop, out, bgra, coeffs, dst)?, |
| 211 | - }) | 220 | + } |
| 221 | + Ok(()) | ||
| 212 | } | 222 | } |
| 213 | 223 | ||
| 214 | #[cfg(target_os = "linux")] | 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 | let bgra = output.order == RgbOrder::Bgra; | 335 | let bgra = output.order == RgbOrder::Bgra; |
| 326 | if self.grain.is_some() { | 336 | if self.grain.is_some() { |
| 327 | if self.bit_depth != 8 { | 337 | if self.bit_depth != 8 { |
| @@ -330,22 +340,22 @@ impl VkPicture { | |||
| 330 | "RGB of 10-bit output", | 340 | "RGB of 10-bit output", |
| 331 | )); | 341 | )); |
| 332 | } | 342 | } |
| 333 | - return Ok(i420_to_rgb( | 343 | + *dst = i420_to_rgb(&self.i420()?, output.max_width, bgra, self.matrix); |
| 334 | - &self.i420()?, | 344 | + return Ok(()); |
| 335 | - output.max_width, | ||
| 336 | - bgra, | ||
| 337 | - self.matrix, | ||
| 338 | - )); | ||
| 339 | } | 345 | } |
| 340 | let (w, h) = rgb_size(self.width, self.height, output.max_width); | 346 | let (w, h) = rgb_size(self.width, self.height, output.max_width); |
| 341 | - let data = self.surfaces.rgb( | 347 | + self.surfaces.rgb( |
| 342 | self.slot.index, | 348 | self.slot.index, |
| 343 | (0, 0, self.width, self.height), | 349 | (0, 0, self.width, self.height), |
| 344 | (w, h), | 350 | (w, h), |
| 345 | bgra, | 351 | bgra, |
| 346 | yuv_to_rgb(self.matrix).words(), | 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 | #[cfg(target_os = "linux")] | 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 | /// A packed 32-bit RGB picture, alpha 255, `stride` bytes per row. | 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 | pub struct RgbPicture { | 39 | pub struct RgbPicture { |
| 40 | /// Pixels, `stride * height` bytes. | 40 | /// Pixels, `stride * height` bytes. |
| 41 | pub data: Vec<u8>, | 41 | pub data: Vec<u8>, |
shims/vk-video-ffi/src/av1_decode.rsmodified+4 -3
| @@ -922,7 +922,7 @@ impl Av1DecodeSession { | |||
| 922 | self.dec.export(slot as usize, (width, height)) | 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 | pub fn read_output_rgb( | 926 | pub fn read_output_rgb( |
| 927 | &mut self, | 927 | &mut self, |
| 928 | slot: u32, | 928 | slot: u32, |
| @@ -930,8 +930,9 @@ impl Av1DecodeSession { | |||
| 930 | out: (u32, u32), | 930 | out: (u32, u32), |
| 931 | bgra: bool, | 931 | bgra: bool, |
| 932 | coeffs: [i32; 5], | 932 | coeffs: [i32; 5], |
| 933 | - ) -> Result<Vec<u8>, Error> { | 933 | + dst: &mut Vec<u8>, |
| 934 | - self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs) | 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 | self.dec.export(slot as usize, (width, height)) | 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 | pub fn read_output_rgb( | 517 | pub fn read_output_rgb( |
| 518 | &mut self, | 518 | &mut self, |
| 519 | slot: u32, | 519 | slot: u32, |
| @@ -521,8 +521,9 @@ impl H264DecodeSession { | |||
| 521 | out: (u32, u32), | 521 | out: (u32, u32), |
| 522 | bgra: bool, | 522 | bgra: bool, |
| 523 | coeffs: [i32; 5], | 523 | coeffs: [i32; 5], |
| 524 | - ) -> Result<Vec<u8>, Error> { | 524 | + dst: &mut Vec<u8>, |
| 525 | - self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs) | 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 | self.dec.export(slot as usize, (width, height)) | 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 | pub fn read_output_rgb( | 731 | pub fn read_output_rgb( |
| 732 | &mut self, | 732 | &mut self, |
| 733 | slot: u32, | 733 | slot: u32, |
| @@ -735,8 +735,9 @@ impl H265DecodeSession { | |||
| 735 | out: (u32, u32), | 735 | out: (u32, u32), |
| 736 | bgra: bool, | 736 | bgra: bool, |
| 737 | coeffs: [i32; 5], | 737 | coeffs: [i32; 5], |
| 738 | - ) -> Result<Vec<u8>, Error> { | 738 | + dst: &mut Vec<u8>, |
| 739 | - self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs) | 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 | out: (u32, u32), | 1874 | out: (u32, u32), |
| 1875 | bgra: bool, | 1875 | bgra: bool, |
| 1876 | coeffs: [i32; 5], | 1876 | coeffs: [i32; 5], |
| 1877 | - ) -> Result<Vec<u8>, Error> { | 1877 | + dst: &mut Vec<u8>, |
| 1878 | + ) -> Result<(), Error> { | ||
| 1878 | self.check_window(crop)?; | 1879 | self.check_window(crop)?; |
| 1879 | let (x, y, width, height) = crop; | 1880 | let (x, y, width, height) = crop; |
| 1880 | if out.0 == 0 || out.1 == 0 || out.0 > width || out.1 > height { | 1881 | if out.0 == 0 || out.1 == 0 || out.0 > width || out.1 > height { |
| @@ -1931,7 +1932,9 @@ impl Decoder { | |||
| 1931 | self.graphics.end()?; | 1932 | self.graphics.end()?; |
| 1932 | self.graphics.submit()?; | 1933 | self.graphics.submit()?; |
| 1933 | let len = out.0 as usize * out.1 as usize * 4; | 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 | /// 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. | 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 | self.dec.export(slot as usize, (width, height)) | 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 | pub fn read_output_rgb( | 461 | pub fn read_output_rgb( |
| 462 | &mut self, | 462 | &mut self, |
| 463 | slot: u32, | 463 | slot: u32, |
| @@ -465,8 +465,9 @@ impl Vp9DecodeSession { | |||
| 465 | out: (u32, u32), | 465 | out: (u32, u32), |
| 466 | bgra: bool, | 466 | bgra: bool, |
| 467 | coeffs: [i32; 5], | 467 | coeffs: [i32; 5], |
| 468 | - ) -> Result<Vec<u8>, Error> { | 468 | + dst: &mut Vec<u8>, |
| 469 | - self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs) | 469 | + ) -> Result<(), Error> { |
| 470 | + self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs, dst) | ||
| 470 | } | 471 | } |
| 471 | } | 472 | } |
| 472 | 473 | ||