use std::rc::Rc;
use crate::{Error, Matrix, Result};
use super::VideoPixelFormat;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct PlaneExtent {
pub(crate) width: usize,
pub(crate) height: usize,
}
pub(crate) fn plane_count(format: VideoPixelFormat) -> usize {
match format {
VideoPixelFormat::Yuv420Planar8 | VideoPixelFormat::Yuv420Planar10 => 3,
VideoPixelFormat::Nv12 | VideoPixelFormat::P010 | VideoPixelFormat::P012 => 2,
}
}
pub(crate) fn plane_extents(
format: VideoPixelFormat,
width: usize,
height: usize,
) -> Result<Vec<PlaneExtent>> {
if width == 0 || height == 0 {
return Err(Error::invalid_argument(
"device-planar frames require a non-zero luma extent",
));
}
let chroma_width = width.div_ceil(2);
let chroma_height = height.div_ceil(2);
let extents = match format {
VideoPixelFormat::Yuv420Planar8 => vec![
PlaneExtent { width, height },
PlaneExtent {
width: chroma_width,
height: chroma_height,
},
PlaneExtent {
width: chroma_width,
height: chroma_height,
},
],
VideoPixelFormat::Nv12 | VideoPixelFormat::P010 => {
let sample_bytes = if format == VideoPixelFormat::P010 {
2
} else {
1
};
let row_bytes = |sites: usize, components: usize| {
sites
.checked_mul(components * sample_bytes)
.ok_or_else(|| Error::out_of_range("video plane row size overflows usize"))
};
vec![
PlaneExtent {
width: row_bytes(width, 1)?,
height,
},
PlaneExtent {
width: row_bytes(chroma_width, 2)?,
height: chroma_height,
},
]
}
VideoPixelFormat::Yuv420Planar10 | VideoPixelFormat::P012 => {
return Err(Error::missing_capability(
"device-planar video supports I420, NV12, and P010",
));
}
};
Ok(extents)
}
#[derive(Clone)]
pub(crate) struct DevicePlanar {
pub(crate) planes: Rc<Vec<Matrix>>,
pub(crate) format: VideoPixelFormat,
pub(crate) width: usize,
pub(crate) height: usize,
}