Expose bit_depth on I420_16 and pin 10-bit readback
Browse files at this commit54b87cdc29c0d4f807b03291ff3ee42302e2f919Parent: b966d5e- feat
- expose bit_depth on I420_16 and pin 10-bit readback
2 changed files +95 -0
- crates/ec-hw/src/frame.rs +89 -0
- crates/ec-hw/src/lib.rs +6 -0
crates/ec-hw/src/frame.rsmodified+89 -0
| @@ -80,6 +80,8 @@ pub struct I420_16 { | ||
| 80 | 80 | pub width: u32, |
| 81 | 81 | /// Height in luma samples. |
| 82 | 82 | pub height: u32, |
| 83 | + /// Luma bit depth: always 10 for this format. | |
| 84 | + pub bit_depth: u8, | |
| 83 | 85 | } |
| 84 | 86 | |
| 85 | 87 | impl Frame { |
| @@ -214,6 +216,7 @@ impl Frame { | ||
| 214 | 216 | v: vec![0; cw * ch], |
| 215 | 217 | width: self.display_size.0, |
| 216 | 218 | height: self.display_size.1, |
| 219 | + bit_depth: 10, | |
| 217 | 220 | }; |
| 218 | 221 | |
| 219 | 222 | self.with_planes(|mapped| { |
| @@ -434,4 +437,90 @@ mod tests { | ||
| 434 | 437 | let back = nv12_to_i420(&y2, y_pitch, &uv2, uv_pitch, w, h); |
| 435 | 438 | assert_eq!(back, i420); |
| 436 | 439 | } |
| 440 | + | |
| 441 | + /// `to_i420_16` is the lossless 10-bit readback: samples that collapse | |
| 442 | + /// together under 8-bit truncation (`512 >> 2 == 513 >> 2 == 128`) must | |
| 443 | + /// survive distinctly. Skips when there is no VA device, the same gate the | |
| 444 | + /// live GPU tests use. | |
| 445 | + #[test] | |
| 446 | + fn p010_readback_preserves_ten_bits() { | |
| 447 | + let Ok(display) = ec_va::Display::open() else { | |
| 448 | + eprintln!("skipped: no VA display"); | |
| 449 | + return; | |
| 450 | + }; | |
| 451 | + let (w, h) = (4u32, 4u32); | |
| 452 | + let pool = | |
| 453 | + crate::pool::SurfacePool::new(&display, &ec_va::SurfaceSpec::p010(w, h), 1) | |
| 454 | + .expect("P010 surface pool"); | |
| 455 | + let surface = pool.acquire().expect("surface from pool"); | |
| 456 | + | |
| 457 | + // Synthetic P010: 10-bit samples packed into the high bits of each | |
| 458 | + // u16, exactly how the decoder's P010 surfaces carry them. 512 and 513 | |
| 459 | + // both truncate to 128 in 8-bit, so they prove the 16-bit path is not | |
| 460 | + // silently losing the low bits. | |
| 461 | + let y_samples = [512u16, 513, 1023, 5]; | |
| 462 | + let mut image = Image::create(&display, VA_FOURCC_P010, w, h).expect("P010 upload image"); | |
| 463 | + { | |
| 464 | + let mut mapped = image.map().expect("map upload image"); | |
| 465 | + let y_pitch = mapped.pitch(0).expect("luma pitch") as usize; | |
| 466 | + let uv_pitch = mapped.pitch(1).expect("chroma pitch") as usize; | |
| 467 | + if let Some(dst) = mapped.plane_mut(0) { | |
| 468 | + for row in 0..h as usize { | |
| 469 | + for col in 0..w as usize { | |
| 470 | + let sample = y_samples[(row * w as usize + col) % y_samples.len()] << 6; | |
| 471 | + let b = sample.to_le_bytes(); | |
| 472 | + let o = row * y_pitch + col * 2; | |
| 473 | + dst[o] = b[0]; | |
| 474 | + dst[o + 1] = b[1]; | |
| 475 | + } | |
| 476 | + } | |
| 477 | + } | |
| 478 | + if let Some(dst) = mapped.plane_mut(1) { | |
| 479 | + let (u, v) = (512u16 << 6, 1023u16 << 6); | |
| 480 | + let ub = u.to_le_bytes(); | |
| 481 | + let vb = v.to_le_bytes(); | |
| 482 | + for row in 0..h as usize / 2 { | |
| 483 | + for col in 0..w as usize / 2 { | |
| 484 | + let o = row * uv_pitch + col * 4; | |
| 485 | + dst[o] = ub[0]; | |
| 486 | + dst[o + 1] = ub[1]; | |
| 487 | + dst[o + 2] = vb[0]; | |
| 488 | + dst[o + 3] = vb[1]; | |
| 489 | + } | |
| 490 | + } | |
| 491 | + } | |
| 492 | + } | |
| 493 | + surface.write_from(&image, w, h).expect("P010 upload"); | |
| 494 | + | |
| 495 | + let frame = Frame::new( | |
| 496 | + surface, | |
| 497 | + Arc::new(Mutex::new(None)), | |
| 498 | + 0, | |
| 499 | + (w, h), | |
| 500 | + (w, h), | |
| 501 | + 10, | |
| 502 | + None, | |
| 503 | + ); | |
| 504 | + | |
| 505 | + let i16 = frame.to_i420_16().expect("to_i420_16"); | |
| 506 | + let i8 = frame.to_i420().expect("to_i420"); | |
| 507 | + | |
| 508 | + assert_eq!(i16.width, w); | |
| 509 | + assert_eq!(i16.height, h); | |
| 510 | + assert_eq!(i16.bit_depth, 10); | |
| 511 | + let want_y: Vec<u16> = (0..w as usize * h as usize) | |
| 512 | + .map(|i| y_samples[i % y_samples.len()]) | |
| 513 | + .collect(); | |
| 514 | + assert_eq!( | |
| 515 | + i16.y, want_y, | |
| 516 | + "16-bit luma must preserve the 10-bit samples" | |
| 517 | + ); | |
| 518 | + let want_y8: Vec<u8> = want_y.iter().map(|&s| (s >> 2) as u8).collect(); | |
| 519 | + assert_eq!(i8.y, want_y8, "8-bit luma must be the >> 2 truncation"); | |
| 520 | + | |
| 521 | + assert_eq!(i16.u, vec![512u16; 4]); | |
| 522 | + assert_eq!(i16.v, vec![1023u16; 4]); | |
| 523 | + assert_eq!(i8.u, vec![128u8; 4]); | |
| 524 | + assert_eq!(i8.v, vec![255u8; 4]); | |
| 525 | + } | |
| 437 | 526 | } |
crates/ec-hw/src/lib.rsmodified+6 -0
| @@ -18,6 +18,12 @@ | ||
| 18 | 18 | //! } |
| 19 | 19 | //! # Ok::<(), ec_hw::Error>(()) |
| 20 | 20 | //! ``` |
| 21 | +//! # Bit depth | |
| 22 | +//! | |
| 23 | +//! [`Frame::to_i420`] truncates a 10-bit (P010) surface to 8 bits, because a | |
| 24 | +//! byte per sample is what the software path speaks. A 10-bit consumer must | |
| 25 | +//! call [`Frame::to_i420_16`] instead — that is the lossless path that keeps | |
| 26 | +//! all ten bits. | |
| 21 | 27 | //! |
| 22 | 28 | //! # Shape |
| 23 | 29 | //! |