use mediaway_common::Bytes;
pub(super) fn copy_nv12_from_planes(
data: &[u8],
width: u32,
height: u32,
y_pitch: u32,
y_offset: u32,
uv_pitch: u32,
uv_offset: u32,
) -> Bytes {
let width = width as usize;
let height = height as usize;
let y_pitch = y_pitch as usize;
let y_offset = y_offset as usize;
let uv_pitch = uv_pitch as usize;
let uv_offset = uv_offset as usize;
let uv_rows = height / 2;
let mut out = vec![0u8; width * height + width * uv_rows];
for row in 0..height {
let Some(src_start) = y_offset.checked_add(row * y_pitch) else {
break;
};
let src_end = src_start + width;
if src_end > data.len() {
break;
}
let dst_start = row * width;
out[dst_start..dst_start + width].copy_from_slice(&data[src_start..src_end]);
}
let y_plane_bytes = width * height;
for row in 0..uv_rows {
let Some(src_start) = uv_offset.checked_add(row * uv_pitch) else {
break;
};
let src_end = src_start + width;
if src_end > data.len() {
break;
}
let dst_start = y_plane_bytes + row * width;
out[dst_start..dst_start + width].copy_from_slice(&data[src_start..src_end]);
}
Bytes::from(out)
}
#[cfg(test)]
#[path = "nv12_tests.rs"]
mod tests;