forge
Explore
Log in

Don't you dare go Hollow

S2: picture.rs plane fills read rows straight from the mapped plane through one plane_row helper; the row_buf copy is gone

Browse files at this commit

a78494532a2f35549588ab2f37da76da5281d037Ahmet Kaan GümüşParent: 8c693a2
crates/ec-hw/src/picture.rsmodified+13 -25
@@ -258,20 +258,16 @@ pub(crate) fn fill_i420_16(
258258 let bytes = if bit_depth == 8 { 1 } else { 2 };
259259 check_plane("luma", y_plane.len(), y_pitch, h, w * bytes)?;
260260 check_plane("chroma", uv_plane.len(), uv_pitch, ch, cw * 2 * bytes)?;
261- let mut row_buf = vec![0u8; uv_pitch.max(y_pitch).max(w * 2)];
262261 match bit_depth {
263262 8 => {
264263 for row in 0..h {
265- let src = row * y_pitch;
266- row_buf[..w].copy_from_slice(&y_plane[src..src + w]);
267- for (dst, &sample) in out.y[row * w..(row + 1) * w].iter_mut().zip(&row_buf[..w]) {
264+ let line = plane_row(y_plane, y_pitch, row, w);
265+ for (dst, &sample) in out.y[row * w..(row + 1) * w].iter_mut().zip(line) {
268266 *dst = u16::from(sample) << 2;
269267 }
270268 }
271269 for row in 0..ch {
272- let src = row * uv_pitch;
273- let line = &mut row_buf[..cw * 2];
274- line.copy_from_slice(&uv_plane[src..src + cw * 2]);
270+ let line = plane_row(uv_plane, uv_pitch, row, cw * 2);
275271 for col in 0..cw {
276272 out.u[row * cw + col] = u16::from(line[col * 2]) << 2;
277273 out.v[row * cw + col] = u16::from(line[col * 2 + 1]) << 2;
@@ -282,18 +278,14 @@ pub(crate) fn fill_i420_16(
282278 // P010 is 16-bit little endian with the sample in the top ten
283279 // bits; the low six are zero padding.
284280 for row in 0..h {
285- let src = row * y_pitch;
286- let line = &mut row_buf[..w * 2];
287- line.copy_from_slice(&y_plane[src..src + w * 2]);
281+ let line = plane_row(y_plane, y_pitch, row, w * 2);
288282 for col in 0..w {
289283 out.y[row * w + col] =
290284 u16::from_le_bytes([line[col * 2], line[col * 2 + 1]]) >> 6;
291285 }
292286 }
293287 for row in 0..ch {
294- let src = row * uv_pitch;
295- let line = &mut row_buf[..cw * 4];
296- line.copy_from_slice(&uv_plane[src..src + cw * 4]);
288+ let line = plane_row(uv_plane, uv_pitch, row, cw * 4);
297289 for col in 0..cw {
298290 out.u[row * cw + col] =
299291 u16::from_le_bytes([line[col * 4], line[col * 4 + 1]]) >> 6;
@@ -347,17 +339,13 @@ pub(crate) fn fill_i420(
347339 let bytes = if bit_depth == 8 { 1 } else { 2 };
348340 check_plane("luma", y_plane.len(), y_pitch, h, w * bytes)?;
349341 check_plane("chroma", uv_plane.len(), uv_pitch, ch, cw * 2 * bytes)?;
350- let mut row_buf = vec![0u8; uv_pitch.max(y_pitch).max(w * 2)];
351342 match bit_depth {
352343 8 => {
353344 for row in 0..h {
354- let src = row * y_pitch;
355- out.y[row * w..(row + 1) * w].copy_from_slice(&y_plane[src..src + w]);
345+ out.y[row * w..(row + 1) * w].copy_from_slice(plane_row(y_plane, y_pitch, row, w));
356346 }
357347 for row in 0..ch {
358- let src = row * uv_pitch;
359- let line = &mut row_buf[..cw * 2];
360- line.copy_from_slice(&uv_plane[src..src + cw * 2]);
348+ let line = plane_row(uv_plane, uv_pitch, row, cw * 2);
361349 for col in 0..cw {
362350 out.u[row * cw + col] = line[col * 2];
363351 out.v[row * cw + col] = line[col * 2 + 1];
@@ -368,17 +356,13 @@ pub(crate) fn fill_i420(
368356 // P010 keeps the sample in the *high* bits of each u16, so the
369357 // 8-bit value is the high byte — a shift by 8, not by 2.
370358 for row in 0..h {
371- let src = row * y_pitch;
372- let line = &mut row_buf[..w * 2];
373- line.copy_from_slice(&y_plane[src..src + w * 2]);
359+ let line = plane_row(y_plane, y_pitch, row, w * 2);
374360 for col in 0..w {
375361 out.y[row * w + col] = line[col * 2 + 1];
376362 }
377363 }
378364 for row in 0..ch {
379- let src = row * uv_pitch;
380- let line = &mut row_buf[..cw * 4];
381- line.copy_from_slice(&uv_plane[src..src + cw * 4]);
365+ let line = plane_row(uv_plane, uv_pitch, row, cw * 4);
382366 for col in 0..cw {
383367 out.u[row * cw + col] = line[col * 4 + 1];
384368 out.v[row * cw + col] = line[col * 4 + 3];
@@ -389,6 +373,10 @@ pub(crate) fn fill_i420(
389373 Ok(())
390374 }
391375
376+fn plane_row(plane: &[u8], pitch: usize, row: usize, len: usize) -> &[u8] {
377+ &plane[row * pitch..row * pitch + len]
378+}
379+
392380 /// Check that `rows` rows of `row_bytes` bytes at `pitch` fit in a plane of
393381 /// `len` bytes. A lost or half-initialised GPU context can hand back a short
394382 /// or odd mapping; this turns what would be a slice panic into an error.