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 commita78494532a2f35549588ab2f37da76da5281d037Parent: 8c693a21 changed file +13 -25
- crates/ec-hw/src/picture.rs +13 -25
crates/ec-hw/src/picture.rsmodified+13 -25
| @@ -258,20 +258,16 @@ pub(crate) fn fill_i420_16( | ||
| 258 | 258 | let bytes = if bit_depth == 8 { 1 } else { 2 }; |
| 259 | 259 | check_plane("luma", y_plane.len(), y_pitch, h, w * bytes)?; |
| 260 | 260 | 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)]; | |
| 262 | 261 | match bit_depth { |
| 263 | 262 | 8 => { |
| 264 | 263 | 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) { | |
| 268 | 266 | *dst = u16::from(sample) << 2; |
| 269 | 267 | } |
| 270 | 268 | } |
| 271 | 269 | 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); | |
| 275 | 271 | for col in 0..cw { |
| 276 | 272 | out.u[row * cw + col] = u16::from(line[col * 2]) << 2; |
| 277 | 273 | out.v[row * cw + col] = u16::from(line[col * 2 + 1]) << 2; |
| @@ -282,18 +278,14 @@ pub(crate) fn fill_i420_16( | ||
| 282 | 278 | // P010 is 16-bit little endian with the sample in the top ten |
| 283 | 279 | // bits; the low six are zero padding. |
| 284 | 280 | 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); | |
| 288 | 282 | for col in 0..w { |
| 289 | 283 | out.y[row * w + col] = |
| 290 | 284 | u16::from_le_bytes([line[col * 2], line[col * 2 + 1]]) >> 6; |
| 291 | 285 | } |
| 292 | 286 | } |
| 293 | 287 | 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); | |
| 297 | 289 | for col in 0..cw { |
| 298 | 290 | out.u[row * cw + col] = |
| 299 | 291 | u16::from_le_bytes([line[col * 4], line[col * 4 + 1]]) >> 6; |
| @@ -347,17 +339,13 @@ pub(crate) fn fill_i420( | ||
| 347 | 339 | let bytes = if bit_depth == 8 { 1 } else { 2 }; |
| 348 | 340 | check_plane("luma", y_plane.len(), y_pitch, h, w * bytes)?; |
| 349 | 341 | 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)]; | |
| 351 | 342 | match bit_depth { |
| 352 | 343 | 8 => { |
| 353 | 344 | 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)); | |
| 356 | 346 | } |
| 357 | 347 | 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); | |
| 361 | 349 | for col in 0..cw { |
| 362 | 350 | out.u[row * cw + col] = line[col * 2]; |
| 363 | 351 | out.v[row * cw + col] = line[col * 2 + 1]; |
| @@ -368,17 +356,13 @@ pub(crate) fn fill_i420( | ||
| 368 | 356 | // P010 keeps the sample in the *high* bits of each u16, so the |
| 369 | 357 | // 8-bit value is the high byte — a shift by 8, not by 2. |
| 370 | 358 | 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); | |
| 374 | 360 | for col in 0..w { |
| 375 | 361 | out.y[row * w + col] = line[col * 2 + 1]; |
| 376 | 362 | } |
| 377 | 363 | } |
| 378 | 364 | 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); | |
| 382 | 366 | for col in 0..cw { |
| 383 | 367 | out.u[row * cw + col] = line[col * 4 + 1]; |
| 384 | 368 | out.v[row * cw + col] = line[col * 4 + 3]; |
| @@ -389,6 +373,10 @@ pub(crate) fn fill_i420( | ||
| 389 | 373 | Ok(()) |
| 390 | 374 | } |
| 391 | 375 | |
| 376 | +fn plane_row(plane: &[u8], pitch: usize, row: usize, len: usize) -> &[u8] { | |
| 377 | + &plane[row * pitch..row * pitch + len] | |
| 378 | +} | |
| 379 | + | |
| 392 | 380 | /// Check that `rows` rows of `row_bytes` bytes at `pitch` fit in a plane of |
| 393 | 381 | /// `len` bytes. A lost or half-initialised GPU context can hand back a short |
| 394 | 382 | /// or odd mapping; this turns what would be a slice panic into an error. |