diff --git a/crates/ec-hw/src/picture.rs b/crates/ec-hw/src/picture.rs index d96a824f..0265cb9c 100644 --- a/crates/ec-hw/src/picture.rs +++ b/crates/ec-hw/src/picture.rs @@ -258,20 +258,16 @@ pub(crate) fn fill_i420_16( let bytes = if bit_depth == 8 { 1 } else { 2 }; check_plane("luma", y_plane.len(), y_pitch, h, w * bytes)?; check_plane("chroma", uv_plane.len(), uv_pitch, ch, cw * 2 * bytes)?; - let mut row_buf = vec![0u8; uv_pitch.max(y_pitch).max(w * 2)]; match bit_depth { 8 => { for row in 0..h { - let src = row * y_pitch; - row_buf[..w].copy_from_slice(&y_plane[src..src + w]); - for (dst, &sample) in out.y[row * w..(row + 1) * w].iter_mut().zip(&row_buf[..w]) { + let line = plane_row(y_plane, y_pitch, row, w); + for (dst, &sample) in out.y[row * w..(row + 1) * w].iter_mut().zip(line) { *dst = u16::from(sample) << 2; } } for row in 0..ch { - let src = row * uv_pitch; - let line = &mut row_buf[..cw * 2]; - line.copy_from_slice(&uv_plane[src..src + cw * 2]); + let line = plane_row(uv_plane, uv_pitch, row, cw * 2); for col in 0..cw { out.u[row * cw + col] = u16::from(line[col * 2]) << 2; out.v[row * cw + col] = u16::from(line[col * 2 + 1]) << 2; @@ -282,18 +278,14 @@ pub(crate) fn fill_i420_16( // P010 is 16-bit little endian with the sample in the top ten // bits; the low six are zero padding. for row in 0..h { - let src = row * y_pitch; - let line = &mut row_buf[..w * 2]; - line.copy_from_slice(&y_plane[src..src + w * 2]); + let line = plane_row(y_plane, y_pitch, row, w * 2); for col in 0..w { out.y[row * w + col] = u16::from_le_bytes([line[col * 2], line[col * 2 + 1]]) >> 6; } } for row in 0..ch { - let src = row * uv_pitch; - let line = &mut row_buf[..cw * 4]; - line.copy_from_slice(&uv_plane[src..src + cw * 4]); + let line = plane_row(uv_plane, uv_pitch, row, cw * 4); for col in 0..cw { out.u[row * cw + col] = u16::from_le_bytes([line[col * 4], line[col * 4 + 1]]) >> 6; @@ -347,17 +339,13 @@ pub(crate) fn fill_i420( let bytes = if bit_depth == 8 { 1 } else { 2 }; check_plane("luma", y_plane.len(), y_pitch, h, w * bytes)?; check_plane("chroma", uv_plane.len(), uv_pitch, ch, cw * 2 * bytes)?; - let mut row_buf = vec![0u8; uv_pitch.max(y_pitch).max(w * 2)]; match bit_depth { 8 => { for row in 0..h { - let src = row * y_pitch; - out.y[row * w..(row + 1) * w].copy_from_slice(&y_plane[src..src + w]); + out.y[row * w..(row + 1) * w].copy_from_slice(plane_row(y_plane, y_pitch, row, w)); } for row in 0..ch { - let src = row * uv_pitch; - let line = &mut row_buf[..cw * 2]; - line.copy_from_slice(&uv_plane[src..src + cw * 2]); + let line = plane_row(uv_plane, uv_pitch, row, cw * 2); for col in 0..cw { out.u[row * cw + col] = line[col * 2]; out.v[row * cw + col] = line[col * 2 + 1]; @@ -368,17 +356,13 @@ pub(crate) fn fill_i420( // P010 keeps the sample in the *high* bits of each u16, so the // 8-bit value is the high byte — a shift by 8, not by 2. for row in 0..h { - let src = row * y_pitch; - let line = &mut row_buf[..w * 2]; - line.copy_from_slice(&y_plane[src..src + w * 2]); + let line = plane_row(y_plane, y_pitch, row, w * 2); for col in 0..w { out.y[row * w + col] = line[col * 2 + 1]; } } for row in 0..ch { - let src = row * uv_pitch; - let line = &mut row_buf[..cw * 4]; - line.copy_from_slice(&uv_plane[src..src + cw * 4]); + let line = plane_row(uv_plane, uv_pitch, row, cw * 4); for col in 0..cw { out.u[row * cw + col] = line[col * 4 + 1]; out.v[row * cw + col] = line[col * 4 + 3]; @@ -389,6 +373,10 @@ pub(crate) fn fill_i420( Ok(()) } +fn plane_row(plane: &[u8], pitch: usize, row: usize, len: usize) -> &[u8] { + &plane[row * pitch..row * pitch + len] +} + /// Check that `rows` rows of `row_bytes` bytes at `pitch` fit in a plane of /// `len` bytes. A lost or half-initialised GPU context can hand back a short /// or odd mapping; this turns what would be a slice panic into an error.