use alloc::vec::Vec;
use crate::boxes::BoxHeader;
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Ispe {
pub width: u32,
pub height: u32,
}
pub fn parse_ispe(b: &BoxHeader<'_>) -> Result<Ispe> {
let (_v, _f, mut r) = b.full_box("ispe")?;
let width = r.u32("ispe width")?;
let height = r.u32("ispe height")?;
if width == 0 || height == 0 {
return Err(Error::Malformed("ispe declares a zero dimension"));
}
Ok(Ispe { width, height })
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pixi {
pub bits_per_channel: Vec<u8>,
}
pub fn parse_pixi(b: &BoxHeader<'_>) -> Result<Pixi> {
let (_v, _f, mut r) = b.full_box("pixi")?;
let n = r.u8("pixi channel count")?;
if usize::from(n) > r.remaining() {
return Err(Error::Malformed(
"pixi declares more channels than it holds",
));
}
let mut bits_per_channel = Vec::with_capacity(usize::from(n));
for _ in 0..n {
bits_per_channel.push(r.u8("pixi channel depth")?);
}
Ok(Pixi { bits_per_channel })
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Pasp {
pub h_spacing: u32,
pub v_spacing: u32,
}
pub fn parse_pasp(b: &BoxHeader<'_>) -> Result<Pasp> {
let mut r = b.reader();
Ok(Pasp {
h_spacing: r.u32("pasp hSpacing")?,
v_spacing: r.u32("pasp vSpacing")?,
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Rotation {
#[default]
None,
Ccw90,
Ccw180,
Ccw270,
}
impl Rotation {
pub const fn degrees(self) -> u16 {
match self {
Rotation::None => 0,
Rotation::Ccw90 => 90,
Rotation::Ccw180 => 180,
Rotation::Ccw270 => 270,
}
}
pub const fn swaps_axes(self) -> bool {
matches!(self, Rotation::Ccw90 | Rotation::Ccw270)
}
}
pub fn parse_irot(b: &BoxHeader<'_>) -> Result<Rotation> {
let mut r = b.reader();
Ok(match r.u8("irot angle")? & 0x03 {
0 => Rotation::None,
1 => Rotation::Ccw90,
2 => Rotation::Ccw180,
_ => Rotation::Ccw270,
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mirror {
LeftRight,
TopBottom,
}
pub fn parse_imir(b: &BoxHeader<'_>) -> Result<Mirror> {
let mut r = b.reader();
Ok(if r.u8("imir axis")? & 0x01 == 0 {
Mirror::LeftRight
} else {
Mirror::TopBottom
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Clap {
pub width: (u32, u32),
pub height: (u32, u32),
pub horiz_off: (i32, u32),
pub vert_off: (i32, u32),
}
pub fn parse_clap(b: &BoxHeader<'_>) -> Result<Clap> {
let mut r = b.reader();
let wn = r.u32("clap width numerator")?;
let wd = r.u32("clap width denominator")?;
let hn = r.u32("clap height numerator")?;
let hd = r.u32("clap height denominator")?;
let hon = r.u32("clap horizontal offset numerator")? as i32;
let hod = r.u32("clap horizontal offset denominator")?;
let von = r.u32("clap vertical offset numerator")? as i32;
let vod = r.u32("clap vertical offset denominator")?;
if wd == 0 || hd == 0 || hod == 0 || vod == 0 {
return Err(Error::Malformed("clap has a zero denominator"));
}
Ok(Clap {
width: (wn, wd),
height: (hn, hd),
horiz_off: (hon, hod),
vert_off: (von, vod),
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AuxKind {
Alpha,
Depth,
Other,
}
#[derive(Debug, Clone, Copy)]
pub struct AuxC<'a> {
pub urn: &'a str,
pub kind: AuxKind,
}
pub fn parse_auxc<'a>(b: &BoxHeader<'a>) -> Result<AuxC<'a>> {
let (_v, _f, mut r) = b.full_box("auxC")?;
let urn = r.cstr("auxC aux type")?;
let kind = if urn.ends_with("auxiliary:alpha") || urn == "urn:mpeg:hevc:2015:auxid:1" {
AuxKind::Alpha
} else if urn.ends_with("auxiliary:depth") || urn == "urn:mpeg:hevc:2015:auxid:2" {
AuxKind::Depth
} else {
AuxKind::Other
};
Ok(AuxC { urn, kind })
}