use crate::grid::Mosaic;
use crate::hevc::{ChromaFormat, Frame};
#[derive(Debug, Clone, Copy)]
pub(crate) enum Source<'a> {
Frame(&'a Frame),
Mosaic(&'a Mosaic<'a>),
}
impl Source<'_> {
pub fn width(&self) -> u32 {
match self {
Source::Frame(f) => f.width,
Source::Mosaic(m) => m.width(),
}
}
pub fn height(&self) -> u32 {
match self {
Source::Frame(f) => f.height,
Source::Mosaic(m) => m.height(),
}
}
pub fn chroma(&self) -> ChromaFormat {
match self {
Source::Frame(f) => f.chroma,
Source::Mosaic(m) => m.chroma(),
}
}
pub fn bit_depth(&self) -> u8 {
match self {
Source::Frame(f) => f.bit_depth,
Source::Mosaic(m) => m.bit_depth(),
}
}
fn chroma_width(&self) -> usize {
self.chroma().chroma_size(self.width(), self.height()).0 as usize
}
pub fn stitch_len(&self) -> usize {
match self {
Source::Frame(_) => 0,
Source::Mosaic(_) => self.width() as usize + self.chroma_width(),
}
}
pub fn luma<'b>(&'b self, y: usize, buf: &'b mut [u16]) -> Option<&'b [u16]> {
match self {
Source::Frame(f) => {
let (w, stride) = (f.width as usize, f.y_stride as usize);
let start = y.checked_mul(stride)?;
f.y.get(start..start + w)
}
Source::Mosaic(m) => {
let row = buf.get_mut(..m.width() as usize)?;
m.luma_row(y, row)?;
Some(row)
}
}
}
pub fn chroma_row<'b>(&'b self, cr: bool, r: usize, buf: &'b mut [u16]) -> Option<&'b [u16]> {
let cw = self.chroma_width();
match self {
Source::Frame(f) => {
let start = r.checked_mul(f.c_stride as usize)?;
let plane = if cr { &f.cr } else { &f.cb };
plane.get(start..start + cw)
}
Source::Mosaic(m) => {
let row = buf.get_mut(..cw)?;
m.chroma_row(cr, r, row)?;
Some(row)
}
}
}
}