use crate::data::Data;
use crate::decoder::{Decoder, Settings};
use crate::error::TealdustError;
use crate::getbits::GetBits;
use crate::headers::{ContentLightLevel, ObuType, PixelLayout};
use crate::levels::ObuMetaType;
use crate::picture::Picture;
use crate::{ColorPrimaries, MatrixCoefficients, TransferCharacteristics};
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
pub const DEFAULT_AVIF_MAX_ITEMS: u32 = 1024;
pub const DEFAULT_AVIF_MAX_IPCO_PROPS: usize = 256;
pub const DEFAULT_AVIF_MAX_IPMA_ENTRIES: u32 = 4096;
pub const DEFAULT_AVIF_MAX_EXTENTS_PER_ITEM: u16 = 32;
pub const DEFAULT_AVIF_MAX_OBU_BYTES: usize = 64 * 1024 * 1024; pub const DEFAULT_AVIF_MAX_IMAGE_DIMENSION: u32 = 65536;
pub const DEFAULT_AVIF_MAX_ILOC_ITEMS: u32 = 1024;
pub const DEFAULT_AVIF_MAX_COMPAT_BRANDS: usize = 64;
pub const DEFAULT_AVIF_MAX_AUXC_URN_LEN: usize = 512;
pub const DEFAULT_AVIF_MAX_IREF_REFS: u16 = 256;
#[derive(Debug, Clone)]
pub struct AvifSettings {
pub decoder_settings: Settings,
pub max_obu_bytes: usize,
pub max_image_dimension: u32,
pub max_items: u32,
pub max_ipco_props: usize,
pub max_ipma_entries: u32,
pub max_extents_per_item: u16,
pub max_iloc_items: u32,
pub max_compat_brands: usize,
pub max_auxc_urn_len: usize,
pub max_iref_refs: u16,
}
impl Default for AvifSettings {
fn default() -> Self {
let mut decoder_settings = Settings::default();
decoder_settings.n_threads = std::thread::available_parallelism()
.map(|n| n.get() as u32)
.unwrap_or(1);
decoder_settings.run_decode = true;
Self {
decoder_settings,
max_obu_bytes: DEFAULT_AVIF_MAX_OBU_BYTES,
max_image_dimension: DEFAULT_AVIF_MAX_IMAGE_DIMENSION,
max_items: DEFAULT_AVIF_MAX_ITEMS,
max_ipco_props: DEFAULT_AVIF_MAX_IPCO_PROPS,
max_ipma_entries: DEFAULT_AVIF_MAX_IPMA_ENTRIES,
max_extents_per_item: DEFAULT_AVIF_MAX_EXTENTS_PER_ITEM,
max_iloc_items: DEFAULT_AVIF_MAX_ILOC_ITEMS,
max_compat_brands: DEFAULT_AVIF_MAX_COMPAT_BRANDS,
max_auxc_urn_len: DEFAULT_AVIF_MAX_AUXC_URN_LEN,
max_iref_refs: DEFAULT_AVIF_MAX_IREF_REFS,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AvifError {
TooShort,
NotAvif,
MissingBox(&'static str),
InvalidBox,
UnsupportedCodec,
GridNotSupported,
ExtentOutOfBounds,
DecodeError(TealdustError),
InvalidCodecConfig,
LimitExceeded,
OutOfMemory,
}
impl fmt::Display for AvifError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TooShort => write!(f, "file too short"),
Self::NotAvif => write!(f, "not an AVIF file (missing avif/avis brand)"),
Self::MissingBox(b) => write!(f, "required box '{b}' not found"),
Self::InvalidBox => write!(f, "malformed ISOBMFF box"),
Self::UnsupportedCodec => write!(f, "unsupported item codec type"),
Self::GridNotSupported => write!(f, "grid/sequence AVIF items not yet supported"),
Self::ExtentOutOfBounds => write!(f, "iloc extent references bytes outside the file"),
Self::DecodeError(e) => write!(f, "AV2 decode error: {e}"),
Self::InvalidCodecConfig => write!(f, "invalid av2C/av1C codec config box"),
Self::LimitExceeded => write!(f, "parser limit exceeded"),
Self::OutOfMemory => write!(f, "out of memory"),
}
}
}
impl std::error::Error for AvifError {}
impl From<TealdustError> for AvifError {
fn from(e: TealdustError) -> Self {
Self::DecodeError(e)
}
}
type Result<T> = std::result::Result<T, AvifError>;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ColorInfo {
pub color_primaries: ColorPrimaries,
pub transfer_characteristics: TransferCharacteristics,
pub matrix_coefficients: MatrixCoefficients,
pub full_range: bool,
}
pub const ALPHA_AUXILIARY_URN: &str = "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha";
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AuxiliaryType {
Alpha,
Depth,
Other(String),
}
impl AuxiliaryType {
fn from_urn_bytes(bytes: &[u8]) -> Self {
let s = bytes
.iter()
.position(|&b| b == 0)
.map_or(bytes, |i| &bytes[..i]);
match s {
b"urn:mpeg:mpegB:cicp:systems:auxiliary:alpha" => Self::Alpha,
b"urn:mpeg:mpegB:cicp:systems:auxiliary:depth" => Self::Depth,
_ => Self::Other(String::from_utf8_lossy(s).into_owned()),
}
}
#[inline]
pub fn is_alpha(&self) -> bool {
*self == Self::Alpha
}
}
#[derive(Debug, Clone)]
pub struct CodecConfig {
pub config_obus: Vec<u8>,
pub high_bitdepth: bool,
pub twelve_bit: bool,
pub monochrome: bool,
pub chroma_subsampling_x: u8,
pub chroma_subsampling_y: u8,
pub chroma_sample_position: u8,
}
impl CodecConfig {
pub fn pixel_layout(&self) -> PixelLayout {
if self.monochrome {
PixelLayout::I400
} else if self.chroma_subsampling_x != 0 && self.chroma_subsampling_y != 0 {
PixelLayout::I420
} else if self.chroma_subsampling_x != 0 {
PixelLayout::I422
} else {
PixelLayout::I444
}
}
pub fn bits_per_component(&self) -> u8 {
if self.twelve_bit {
12
} else if self.high_bitdepth {
10
} else {
8
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct SpatialExtents {
pub width: u32,
pub height: u32,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct PixelAspectRatio {
pub h_spacing: u32,
pub v_spacing: u32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Orientation {
#[default]
Normal,
FlipH,
Rotate180,
FlipV,
Transpose,
Rotate90,
Transverse,
Rotate270,
}
impl Orientation {
pub fn from_exif(v: u16) -> Self {
match v {
2 => Orientation::FlipH,
3 => Orientation::Rotate180,
4 => Orientation::FlipV,
5 => Orientation::Transpose,
6 => Orientation::Rotate90,
7 => Orientation::Transverse,
8 => Orientation::Rotate270,
_ => Orientation::Normal,
}
}
pub fn is_identity(self) -> bool {
self.irot_steps() == 0 && self.imir_axis().is_none()
}
pub(crate) fn imir_axis(self) -> Option<bool> {
match self {
Orientation::FlipH | Orientation::Transpose | Orientation::Transverse => Some(false),
Orientation::FlipV => Some(true),
_ => None,
}
}
pub(crate) fn irot_steps(self) -> u8 {
match self {
Orientation::Normal | Orientation::FlipH | Orientation::FlipV => 0,
Orientation::Rotate180 => 2,
Orientation::Rotate90 => 3,
Orientation::Rotate270 => 1,
Orientation::Transpose => 3,
Orientation::Transverse => 1,
}
}
pub(crate) fn from_irot_imir(steps: u8, axis: Option<bool>) -> Self {
match (steps & 3, axis) {
(0, None) => Orientation::Normal,
(2, None) => Orientation::Rotate180,
(3, None) => Orientation::Rotate90,
(1, None) => Orientation::Rotate270,
(0, Some(false)) => Orientation::FlipH,
(0, Some(true)) => Orientation::FlipV,
(3, Some(false)) => Orientation::Transpose,
(1, Some(false)) => Orientation::Transverse,
_ => Orientation::Normal,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Rational {
pub numerator: i32,
pub denominator: i32,
}
impl Rational {
#[inline]
pub fn to_f64(self) -> f64 {
self.numerator as f64 / self.denominator as f64
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CleanAperture {
pub width: Rational,
pub height: Rational,
pub horiz_off: Rational,
pub vert_off: Rational,
}
impl CleanAperture {
pub fn to_crop_rect(self, coded_width: u32, coded_height: u32) -> Option<(u32, u32, u32, u32)> {
let cw = coded_width as f64;
let ch = coded_height as f64;
let clean_w = self.width.to_f64();
let clean_h = self.height.to_f64();
let h_off = self.horiz_off.to_f64();
let v_off = self.vert_off.to_f64();
if clean_w <= 0.0 || clean_h <= 0.0 {
return None;
}
let x_centre = (cw - 1.0) / 2.0 + h_off;
let y_centre = (ch - 1.0) / 2.0 + v_off;
let left = x_centre - (clean_w - 1.0) / 2.0;
let top = y_centre - (clean_h - 1.0) / 2.0;
let x = left.round() as i64;
let y = top.round() as i64;
let w = clean_w.round() as i64;
let h = clean_h.round() as i64;
if x < 0 || y < 0 || w <= 0 || h <= 0 {
return None;
}
if x + w > coded_width as i64 {
return None;
}
if y + h > coded_height as i64 {
return None;
}
Some((x as u32, y as u32, w as u32, h as u32))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ItemType {
Av01,
Av02,
Grid,
Thumb,
Prof,
Other([u8; 4]),
}
impl ItemType {
fn from_fourcc(b: [u8; 4]) -> Self {
match &b {
b"av01" => Self::Av01,
b"av02" => Self::Av02,
b"grid" => Self::Grid,
b"thmb" => Self::Thumb,
b"prof" => Self::Prof,
_ => Self::Other(b),
}
}
pub fn is_image(&self) -> bool {
matches!(self, Self::Av02)
}
}
#[derive(Debug, Clone)]
pub struct AvifItem {
pub item_id: u16,
pub item_type: ItemType,
pub extents: Option<SpatialExtents>,
pub codec_config: Option<CodecConfig>,
pub color_info: Option<ColorInfo>,
pub pixel_aspect_ratio: Option<PixelAspectRatio>,
pub iloc_extents: Vec<(u64, u64)>,
pub auxiliary_type: Option<AuxiliaryType>,
pub premultiplied_alpha: bool,
pub orientation: Option<Orientation>,
pub clean_aperture: Option<CleanAperture>,
pub icc_profile: Option<Vec<u8>>,
}
impl AvifItem {
fn new(item_id: u16, item_type: ItemType) -> Self {
Self {
item_id,
item_type,
extents: None,
codec_config: None,
color_info: None,
pixel_aspect_ratio: None,
iloc_extents: Vec::new(),
auxiliary_type: None,
premultiplied_alpha: false,
orientation: None,
clean_aperture: None,
icc_profile: None,
}
}
}
#[derive(Debug, Clone)]
pub struct AvifImageInfo {
pub width: u32,
pub height: u32,
pub pixel_layout: PixelLayout,
pub bits_per_component: u8,
pub color_info: Option<ColorInfo>,
pub content_light_level: Option<ContentLightLevel>,
pub pixel_aspect_ratio: Option<PixelAspectRatio>,
pub item_type: ItemType,
pub has_alpha: bool,
pub premultiplied_alpha: bool,
pub orientation: Option<Orientation>,
pub clean_aperture: Option<CleanAperture>,
pub icc_profile: Option<Vec<u8>>,
}
#[derive(Debug)]
pub struct AvifContainer {
pub items: HashMap<u16, AvifItem>,
pub primary_item_id: u16,
pub brand: [u8; 4],
pub alpha_item_id: Option<u16>,
}
impl AvifContainer {
pub fn primary_item(&self) -> Option<&AvifItem> {
self.items.get(&self.primary_item_id)
}
pub fn alpha_item(&self) -> Option<&AvifItem> {
self.alpha_item_id.and_then(|id| self.items.get(&id))
}
}
#[derive(Debug)]
struct Reader<'a> {
data: &'a [u8],
pos: usize,
}
impl<'a> Reader<'a> {
fn new(data: &'a [u8]) -> Self {
Self { data, pos: 0 }
}
#[inline]
fn remaining(&self) -> usize {
self.data.len().saturating_sub(self.pos)
}
#[inline]
fn is_empty(&self) -> bool {
self.pos >= self.data.len()
}
fn read_u8(&mut self) -> Result<u8> {
let v = self
.data
.get(self.pos)
.copied()
.ok_or(AvifError::TooShort)?;
self.pos += 1;
Ok(v)
}
fn read_u16_be(&mut self) -> Result<u16> {
let b = self.read_bytes::<2>()?;
Ok(u16::from_be_bytes(b))
}
fn read_u32_be(&mut self) -> Result<u32> {
let b = self.read_bytes::<4>()?;
Ok(u32::from_be_bytes(b))
}
fn read_u64_be(&mut self) -> Result<u64> {
let b = self.read_bytes::<8>()?;
Ok(u64::from_be_bytes(b))
}
fn read_bytes<const N: usize>(&mut self) -> Result<[u8; N]> {
let end = self.pos.checked_add(N).ok_or(AvifError::TooShort)?;
if end > self.data.len() {
return Err(AvifError::TooShort);
}
let mut buf = [0u8; N];
buf.copy_from_slice(&self.data[self.pos..end]);
self.pos = end;
Ok(buf)
}
fn read_slice(&mut self, n: usize) -> Result<&'a [u8]> {
let end = self.pos.checked_add(n).ok_or(AvifError::TooShort)?;
if end > self.data.len() {
return Err(AvifError::TooShort);
}
let sl = &self.data[self.pos..end];
self.pos = end;
Ok(sl)
}
fn sub_reader(&mut self, len: usize) -> Result<Reader<'a>> {
let end = self.pos.checked_add(len).ok_or(AvifError::TooShort)?;
if end > self.data.len() {
return Err(AvifError::TooShort);
}
let sl = &self.data[self.pos..end];
self.pos = end;
Ok(Reader { data: sl, pos: 0 })
}
}
#[derive(Debug, Clone, Copy)]
struct BoxHeader {
fourcc: [u8; 4],
#[allow(dead_code)]
header_size: u64,
}
fn read_box_header<'a>(r: &mut Reader<'a>) -> Result<(BoxHeader, Reader<'a>)> {
if r.remaining() < 8 {
return Err(AvifError::TooShort);
}
let size_field = r.read_u32_be()?;
let fourcc = r.read_bytes::<4>()?;
let (total_size, header_size): (u64, u64) = match size_field {
0 => return Err(AvifError::InvalidBox),
1 => {
let ext = r.read_u64_be()?;
if ext < 16 {
return Err(AvifError::InvalidBox);
}
(ext, 16)
}
n if n < 8 => return Err(AvifError::InvalidBox),
n => (n as u64, 8),
};
let payload_size = total_size
.checked_sub(header_size)
.ok_or(AvifError::InvalidBox)?;
let payload_size_usize = usize::try_from(payload_size).map_err(|_| AvifError::InvalidBox)?;
let payload = r.sub_reader(payload_size_usize)?;
Ok((
BoxHeader {
fourcc,
header_size,
},
payload,
))
}
fn read_fullbox_header(r: &mut Reader<'_>) -> Result<(u8, u32)> {
let version = r.read_u8()?;
let b = r.read_bytes::<3>()?;
let flags = ((b[0] as u32) << 16) | ((b[1] as u32) << 8) | (b[2] as u32);
Ok((version, flags))
}
pub struct AvifParser;
impl AvifParser {
pub fn parse(data: &[u8]) -> Result<AvifContainer> {
Self::parse_with_settings(data, &AvifSettings::default())
}
pub fn parse_with_settings(data: &[u8], settings: &AvifSettings) -> Result<AvifContainer> {
let mut r = Reader::new(data);
let mut brand = [0u8; 4];
let mut found_ftyp = false;
let mut primary_item_id: u16 = 0;
let mut items: HashMap<u16, AvifItem> = HashMap::new();
let mut iloc_raw: Option<Vec<u8>> = None;
let mut ipco_props: Vec<([u8; 4], Vec<u8>)> = Vec::new();
let mut ipma_raw: Option<Vec<u8>> = None;
let mut iref_raw: Option<Vec<u8>> = None;
let mut seen_meta = false;
while !r.is_empty() {
let (hdr, mut payload) = match read_box_header(&mut r) {
Ok(v) => v,
Err(AvifError::TooShort) => break,
Err(e) => return Err(e),
};
match &hdr.fourcc {
b"ftyp" => {
if !found_ftyp {
brand = payload.read_bytes::<4>()?;
let _minor_version = payload.read_u32_be()?;
let mut avif_brand = is_avif_brand(&brand);
let mut brand_count = 0usize;
while payload.remaining() >= 4
&& (settings.max_compat_brands == 0
|| brand_count < settings.max_compat_brands)
{
let compat = payload.read_bytes::<4>()?;
if is_avif_brand(&compat) {
avif_brand = true;
}
brand_count += 1;
}
if !avif_brand {
return Err(AvifError::NotAvif);
}
found_ftyp = true;
}
}
b"meta" if !seen_meta => {
seen_meta = true;
let (_ver, _flags) = read_fullbox_header(&mut payload)?;
Self::parse_meta(
&mut payload,
&mut primary_item_id,
&mut items,
&mut iloc_raw,
&mut ipco_props,
&mut ipma_raw,
&mut iref_raw,
settings,
)?;
}
_ => {}
}
}
if !found_ftyp {
return Err(AvifError::NotAvif);
}
if let Some(iloc_bytes) = iloc_raw {
Self::apply_iloc(&iloc_bytes, &mut items, data.len() as u64, settings)?;
}
if !ipco_props.is_empty() {
if let Some(ipma_bytes) = ipma_raw {
Self::apply_ipma(&ipma_bytes, &ipco_props, &mut items, settings)?;
}
}
let alpha_item_id = if let Some(iref_bytes) = iref_raw {
Self::resolve_alpha_from_iref(&iref_bytes, primary_item_id, &mut items, settings)?
} else {
None
};
Ok(AvifContainer {
items,
primary_item_id,
brand,
alpha_item_id,
})
}
fn parse_meta(
r: &mut Reader<'_>,
primary_item_id: &mut u16,
items: &mut HashMap<u16, AvifItem>,
iloc_raw: &mut Option<Vec<u8>>,
ipco_props: &mut Vec<([u8; 4], Vec<u8>)>,
ipma_raw: &mut Option<Vec<u8>>,
iref_raw: &mut Option<Vec<u8>>,
settings: &AvifSettings,
) -> Result<()> {
let mut seen_pitm = false;
let mut seen_iinf = false;
let mut seen_iloc = false;
let mut seen_iprp = false;
let mut seen_iref = false;
while !r.is_empty() {
let (hdr, mut payload) = match read_box_header(r) {
Ok(v) => v,
Err(AvifError::TooShort) => break,
Err(e) => return Err(e),
};
match &hdr.fourcc {
b"pitm" if !seen_pitm => {
seen_pitm = true;
let (ver, _flags) = read_fullbox_header(&mut payload)?;
*primary_item_id = if ver == 0 {
payload.read_u16_be()?
} else {
let v = payload.read_u32_be()?;
u16::try_from(v).unwrap_or(0)
};
}
b"iinf" if !seen_iinf => {
seen_iinf = true;
let (ver, _flags) = read_fullbox_header(&mut payload)?;
let raw_count: u32 = if ver == 0 {
payload.read_u16_be()? as u32
} else {
payload.read_u32_be()?
};
if settings.max_items != 0 && raw_count > settings.max_items {
return Err(AvifError::LimitExceeded);
}
let count = raw_count;
for _ in 0..count {
let (_, mut ie_payload) = read_box_header(&mut payload)?;
let (iv, _) = read_fullbox_header(&mut ie_payload)?;
let item_id: u16 = if iv < 3 {
ie_payload.read_u16_be()?
} else {
let v = ie_payload.read_u32_be()?;
u16::try_from(v).unwrap_or(0)
};
let _ = ie_payload.read_u16_be()?;
let item_type_bytes = if iv >= 1 {
ie_payload.read_bytes::<4>()?
} else {
*b"\0\0\0\0"
};
let item_type = ItemType::from_fourcc(item_type_bytes);
items
.entry(item_id)
.or_insert_with(|| AvifItem::new(item_id, item_type));
}
}
b"iloc" if !seen_iloc => {
seen_iloc = true;
let raw = payload.read_slice(payload.remaining())?;
*iloc_raw = Some(raw.to_vec());
}
b"iprp" if !seen_iprp => {
seen_iprp = true;
Self::parse_iprp(&mut payload, ipco_props, ipma_raw, settings)?;
}
b"iref" if !seen_iref => {
seen_iref = true;
let raw = payload.read_slice(payload.remaining())?;
*iref_raw = Some(raw.to_vec());
}
_ => {}
}
}
Ok(())
}
fn parse_iprp(
r: &mut Reader<'_>,
ipco_props: &mut Vec<([u8; 4], Vec<u8>)>,
ipma_raw: &mut Option<Vec<u8>>,
settings: &AvifSettings,
) -> Result<()> {
let mut seen_ipco = false;
let mut seen_ipma = false;
while !r.is_empty() {
let (hdr, mut payload) = match read_box_header(r) {
Ok(v) => v,
Err(AvifError::TooShort) => break,
Err(e) => return Err(e),
};
match &hdr.fourcc {
b"ipco" if !seen_ipco => {
seen_ipco = true;
while !payload.is_empty() {
let (phdr, mut ppayload) = match read_box_header(&mut payload) {
Ok(v) => v,
Err(AvifError::TooShort) => break,
Err(e) => return Err(e),
};
if settings.max_ipco_props != 0
&& ipco_props.len() >= settings.max_ipco_props
{
return Err(AvifError::LimitExceeded);
}
let raw = ppayload.read_slice(ppayload.remaining())?;
ipco_props.push((phdr.fourcc, raw.to_vec()));
}
}
b"ipma" if !seen_ipma => {
seen_ipma = true;
let raw = payload.read_slice(payload.remaining())?;
*ipma_raw = Some(raw.to_vec());
}
_ => {}
}
}
Ok(())
}
fn apply_iloc(
iloc_bytes: &[u8],
items: &mut HashMap<u16, AvifItem>,
file_len: u64,
settings: &AvifSettings,
) -> Result<()> {
let mut r = Reader::new(iloc_bytes);
let (ver, _flags) = read_fullbox_header(&mut r)?;
let byte = r.read_u8()?;
let byte2 = r.read_u8()?;
let offset_size = (byte >> 4) & 0x0F;
let length_size = byte & 0x0F;
let base_offset_size = (byte2 >> 4) & 0x0F;
let index_size = if ver >= 1 { byte2 & 0x0F } else { 0 };
for &sz in &[offset_size, length_size, base_offset_size, index_size] {
if !matches!(sz, 0 | 1 | 2 | 4 | 8) {
return Err(AvifError::InvalidBox);
}
}
let raw_item_count: u32 = if ver < 2 {
r.read_u16_be()? as u32
} else {
r.read_u32_be()?
};
if settings.max_iloc_items != 0 && raw_item_count > settings.max_iloc_items {
return Err(AvifError::LimitExceeded);
}
for _ in 0..raw_item_count {
let item_id: u16 = if ver < 2 {
r.read_u16_be()?
} else {
let v = r.read_u32_be()?;
u16::try_from(v).map_err(|_| AvifError::InvalidBox)?
};
if ver >= 1 {
let cm = r.read_u16_be()?;
if cm & 0x0F != 0 {
let _ = r.read_u16_be()?; let _ = read_sized_int(&mut r, base_offset_size)?; let ec = r.read_u16_be()?;
if settings.max_extents_per_item != 0 && ec > settings.max_extents_per_item {
return Err(AvifError::LimitExceeded);
}
for _ in 0..ec {
if index_size > 0 {
let _ = read_sized_int(&mut r, index_size)?;
}
let _ = read_sized_int(&mut r, offset_size)?;
let _ = read_sized_int(&mut r, length_size)?;
}
continue;
}
}
let _ = r.read_u16_be()?; let base_offset = read_sized_int(&mut r, base_offset_size)?;
let extent_count = r.read_u16_be()?;
if settings.max_extents_per_item != 0 && extent_count > settings.max_extents_per_item {
return Err(AvifError::LimitExceeded);
}
let mut extents: Vec<(u64, u64)> = Vec::new();
extents
.try_reserve_exact(extent_count as usize)
.map_err(|_| AvifError::OutOfMemory)?;
for _ in 0..extent_count {
if index_size > 0 {
let _ = read_sized_int(&mut r, index_size)?;
}
let ext_offset = read_sized_int(&mut r, offset_size)?;
let ext_length = read_sized_int(&mut r, length_size)?;
let abs_offset = base_offset
.checked_add(ext_offset)
.ok_or(AvifError::InvalidBox)?;
let end = abs_offset
.checked_add(ext_length)
.ok_or(AvifError::InvalidBox)?;
if end > file_len {
return Err(AvifError::ExtentOutOfBounds);
}
extents.push((abs_offset, ext_length));
}
if let Some(item) = items.get_mut(&item_id) {
item.iloc_extents = extents;
}
}
Ok(())
}
fn apply_ipma(
ipma_bytes: &[u8],
ipco_props: &[([u8; 4], Vec<u8>)],
items: &mut HashMap<u16, AvifItem>,
settings: &AvifSettings,
) -> Result<()> {
let mut r = Reader::new(ipma_bytes);
let (ver, flags) = read_fullbox_header(&mut r)?;
let entry_count = r.read_u32_be()?;
if settings.max_ipma_entries != 0 && entry_count > settings.max_ipma_entries {
return Err(AvifError::LimitExceeded);
}
for _ in 0..entry_count {
let item_id: u16 = if ver < 1 {
r.read_u16_be()?
} else {
let v = r.read_u32_be()?;
u16::try_from(v).unwrap_or(0)
};
let assoc_count = r.read_u8()?;
for _ in 0..assoc_count {
let prop_idx: u16 = if flags & 1 != 0 {
let word = r.read_u16_be()?;
word & 0x7FFF
} else {
let byte = r.read_u8()?;
(byte & 0x7F) as u16
};
if prop_idx == 0 {
continue; }
let idx = (prop_idx as usize).wrapping_sub(1);
if idx >= ipco_props.len() {
continue;
}
let (fourcc, raw_prop) = &ipco_props[idx];
if let Some(item) = items.get_mut(&item_id) {
Self::apply_property(item, fourcc, raw_prop, settings)?;
}
}
}
Ok(())
}
fn apply_property(
item: &mut AvifItem,
fourcc: &[u8; 4],
raw: &[u8],
settings: &AvifSettings,
) -> Result<()> {
match fourcc {
b"ispe" => {
if raw.len() < 12 {
return Err(AvifError::InvalidBox);
}
let width = u32::from_be_bytes([raw[4], raw[5], raw[6], raw[7]]);
let height = u32::from_be_bytes([raw[8], raw[9], raw[10], raw[11]]);
if width == 0 || height == 0 {
return Err(AvifError::InvalidBox);
}
if settings.max_image_dimension != 0
&& (width > settings.max_image_dimension
|| height > settings.max_image_dimension)
{
return Err(AvifError::LimitExceeded);
}
item.extents = Some(SpatialExtents { width, height });
}
b"av2C" | b"av1C" => {
if raw.len() < 4 {
return Err(AvifError::InvalidCodecConfig);
}
if raw[0] & 0x80 == 0 {
return Err(AvifError::InvalidCodecConfig);
}
let b2 = raw[2];
let b3 = raw[3];
let high_bitdepth = (b2 >> 6) & 1 != 0;
let twelve_bit = (b2 >> 5) & 1 != 0;
let monochrome = (b2 >> 4) & 1 != 0;
let chroma_subsampling_x = (b2 >> 3) & 1;
let chroma_subsampling_y = (b2 >> 2) & 1;
let chroma_sample_position = b2 & 0x3;
let ipd_present = (b3 >> 4) & 1 != 0;
let config_obu_start = if ipd_present { 5 } else { 4 };
let config_obus = raw.get(config_obu_start..).unwrap_or(&[]).to_vec();
item.codec_config = Some(CodecConfig {
config_obus,
high_bitdepth,
twelve_bit,
monochrome,
chroma_subsampling_x,
chroma_subsampling_y,
chroma_sample_position,
});
}
b"colr" => {
if raw.len() < 4 {
return Err(AvifError::InvalidBox);
}
match &raw[..4] {
b"nclx" => {
if raw.len() < 11 {
return Err(AvifError::InvalidBox);
}
let cp = u16::from_be_bytes([raw[4], raw[5]]);
let tc = u16::from_be_bytes([raw[6], raw[7]]);
let mc = u16::from_be_bytes([raw[8], raw[9]]);
let full_range = (raw[10] >> 7) & 1;
item.color_info = Some(ColorInfo {
color_primaries: ColorPrimaries::from(cp),
transfer_characteristics: TransferCharacteristics::from(tc),
matrix_coefficients: MatrixCoefficients::from(mc),
full_range: full_range != 0,
});
}
b"rICC" | b"prof" => {
let profile_bytes = raw[4..].to_vec();
item.icc_profile = Some(profile_bytes);
}
_ => {
}
}
}
b"pasp" => {
if raw.len() < 8 {
return Err(AvifError::InvalidBox);
}
let h = u32::from_be_bytes([raw[0], raw[1], raw[2], raw[3]]);
let v = u32::from_be_bytes([raw[4], raw[5], raw[6], raw[7]]);
if h == 0 || v == 0 {
return Err(AvifError::InvalidBox);
}
item.pixel_aspect_ratio = Some(PixelAspectRatio {
h_spacing: h,
v_spacing: v,
});
}
b"auxC" => {
if raw.len() < 5 {
return Err(AvifError::InvalidBox);
}
let urn_bytes = &raw[4..];
if settings.max_auxc_urn_len != 0 && urn_bytes.len() > settings.max_auxc_urn_len {
return Err(AvifError::LimitExceeded);
}
item.auxiliary_type = Some(AuxiliaryType::from_urn_bytes(urn_bytes));
}
b"irot" => {
if raw.is_empty() {
return Err(AvifError::InvalidBox);
}
let steps = raw[0] & 0x03;
let axis = item.orientation.and_then(|o| o.imir_axis());
item.orientation = Some(Orientation::from_irot_imir(steps, axis));
}
b"imir" => {
if raw.is_empty() {
return Err(AvifError::InvalidBox);
}
let axis = Some(raw[0] & 0x01 != 0);
let steps = item.orientation.map_or(0, |o| o.irot_steps());
item.orientation = Some(Orientation::from_irot_imir(steps, axis));
}
b"clap" => {
if raw.len() < 32 {
return Err(AvifError::InvalidBox);
}
let read_u32 = |off: usize| -> i32 {
i32::from_be_bytes([raw[off], raw[off + 1], raw[off + 2], raw[off + 3]])
};
let width_n = read_u32(0);
let width_d = read_u32(4);
let height_n = read_u32(8);
let height_d = read_u32(12);
let hoff_n = read_u32(16);
let hoff_d = read_u32(20);
let voff_n = read_u32(24);
let voff_d = read_u32(28);
if width_d == 0 || height_d == 0 || hoff_d == 0 || voff_d == 0 {
return Err(AvifError::InvalidBox);
}
if width_n <= 0 || height_n <= 0 {
return Err(AvifError::InvalidBox);
}
item.clean_aperture = Some(CleanAperture {
width: Rational {
numerator: width_n,
denominator: width_d,
},
height: Rational {
numerator: height_n,
denominator: height_d,
},
horiz_off: Rational {
numerator: hoff_n,
denominator: hoff_d,
},
vert_off: Rational {
numerator: voff_n,
denominator: voff_d,
},
});
}
_ => {}
}
Ok(())
}
fn resolve_alpha_from_iref(
iref_bytes: &[u8],
primary_item_id: u16,
items: &mut HashMap<u16, AvifItem>,
settings: &AvifSettings,
) -> Result<Option<u16>> {
let mut r = Reader::new(iref_bytes);
let (ver, _flags) = read_fullbox_header(&mut r)?;
let mut alpha_item_id: Option<u16> = None;
while !r.is_empty() {
let (hdr, mut payload) = match read_box_header(&mut r) {
Ok(v) => v,
Err(AvifError::TooShort) => break,
Err(e) => return Err(e),
};
let ref_type = &hdr.fourcc;
let is_auxl = ref_type == b"auxl";
let is_prem = ref_type == b"prem";
if !is_auxl && !is_prem {
continue;
}
let from_item_id: u16 = if ver == 0 {
payload.read_u16_be()?
} else {
let v = payload.read_u32_be()?;
u16::try_from(v).unwrap_or(0)
};
let ref_count = payload.read_u16_be()?;
if settings.max_iref_refs != 0 && ref_count > settings.max_iref_refs {
return Err(AvifError::LimitExceeded);
}
for _ in 0..ref_count {
let to_item_id: u16 = if ver == 0 {
payload.read_u16_be()?
} else {
let v = payload.read_u32_be()?;
u16::try_from(v).unwrap_or(0)
};
if is_auxl && to_item_id == primary_item_id {
if let Some(item) = items.get(&from_item_id) {
if item.auxiliary_type.as_ref().is_some_and(|a| a.is_alpha()) {
alpha_item_id = Some(from_item_id);
}
}
}
if is_prem && from_item_id == primary_item_id {
if let Some(item) = items.get_mut(&to_item_id) {
item.premultiplied_alpha = true;
}
}
}
}
Ok(alpha_item_id)
}
}
fn read_sized_int(r: &mut Reader<'_>, size: u8) -> Result<u64> {
match size {
0 => Ok(0),
1 => Ok(r.read_u8()? as u64),
2 => Ok(r.read_u16_be()? as u64),
4 => Ok(r.read_u32_be()? as u64),
8 => Ok(r.read_u64_be()?),
_ => Err(AvifError::InvalidBox),
}
}
#[inline]
fn is_avif_brand(b: &[u8; 4]) -> bool {
matches!(b, b"avif" | b"avis" | b"av02")
}
#[derive(Debug, Clone)]
pub struct AlphaPlane {
pub data: Vec<u8>,
pub stride: usize,
pub bits_per_component: u8,
}
pub struct AvifImage {
pub width: u32,
pub height: u32,
pub pixel_layout: PixelLayout,
pub bits_per_component: u8,
pub color_info: Option<ColorInfo>,
pub content_light_level: Option<ContentLightLevel>,
pub pixel_aspect_ratio: Option<PixelAspectRatio>,
pub planes: [Vec<u8>; 3],
pub strides: [usize; 2],
pub alpha: Option<AlphaPlane>,
pub premultiplied_alpha: bool,
pub orientation: Orientation,
pub clean_aperture: Option<CleanAperture>,
pub icc_profile: Option<Vec<u8>>,
pub picture: Arc<Picture>,
}
impl fmt::Debug for AvifImage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AvifImage")
.field("width", &self.width)
.field("height", &self.height)
.field("pixel_layout", &self.pixel_layout)
.field("bits_per_component", &self.bits_per_component)
.field("color_info", &self.color_info)
.field("content_light_level", &self.content_light_level)
.field("has_alpha", &self.alpha.is_some())
.field("premultiplied_alpha", &self.premultiplied_alpha)
.field("orientation", &self.orientation)
.field("clean_aperture", &self.clean_aperture)
.field("has_icc_profile", &self.icc_profile.is_some())
.finish_non_exhaustive()
}
}
pub struct AvifDecoder<'a> {
file: &'a [u8],
container: AvifContainer,
settings: AvifSettings,
}
impl<'a> AvifDecoder<'a> {
pub fn new(data: &'a [u8]) -> Result<Self> {
Self::with_settings(data, AvifSettings::default())
}
pub fn with_settings(data: &'a [u8], settings: AvifSettings) -> Result<Self> {
let container = AvifParser::parse_with_settings(data, &settings)?;
Ok(Self {
file: data,
container,
settings,
})
}
pub fn image_info(&self) -> Result<AvifImageInfo> {
let item = self
.container
.primary_item()
.ok_or(AvifError::MissingBox("pitm"))?;
let extents = item.extents.ok_or(AvifError::MissingBox("ispe"))?;
let cfg = item
.codec_config
.as_ref()
.ok_or(AvifError::MissingBox("av2C"))?;
let has_alpha = self.container.alpha_item_id.is_some();
let premultiplied_alpha = self
.container
.alpha_item()
.is_some_and(|a| a.premultiplied_alpha);
Ok(AvifImageInfo {
width: extents.width,
height: extents.height,
pixel_layout: cfg.pixel_layout(),
bits_per_component: cfg.bits_per_component(),
color_info: item.color_info,
content_light_level: self.item_content_light_level(item)?,
pixel_aspect_ratio: item.pixel_aspect_ratio,
item_type: item.item_type,
has_alpha,
premultiplied_alpha,
orientation: item.orientation,
clean_aperture: item.clean_aperture,
icc_profile: item.icc_profile.clone(),
})
}
pub fn decode(&mut self) -> Result<AvifImage> {
let item = self
.container
.primary_item()
.ok_or(AvifError::MissingBox("pitm"))?;
if item.item_type == ItemType::Grid {
return Err(AvifError::GridNotSupported);
}
if !item.item_type.is_image() {
return Err(AvifError::UnsupportedCodec);
}
let extents = item.extents.ok_or(AvifError::MissingBox("ispe"))?;
let cfg = item
.codec_config
.as_ref()
.ok_or(AvifError::MissingBox("av2C"))?;
let alpha_item_id = self.container.alpha_item_id;
let premultiplied_alpha = alpha_item_id
.and_then(|id| self.container.items.get(&id))
.is_some_and(|a| a.premultiplied_alpha);
let primary_obu = self.assemble_obu(item)?;
if primary_obu.is_empty() {
return Err(AvifError::MissingBox("mdat"));
}
let picture = self.run_decoder(primary_obu)?;
let layout = picture.p.layout;
let bpc = u8::try_from(picture.p.bpc).map_err(|_| AvifError::InvalidCodecConfig)?;
let bps: usize = picture.bytes_per_sample();
if cfg.pixel_layout() != layout || cfg.bits_per_component() != bpc {
return Err(AvifError::InvalidCodecConfig);
}
let pic_w = picture.p.w as usize;
let pic_h = picture.p.h as usize;
let y_stride = picture.stride[0].unsigned_abs();
let uv_stride = picture.stride[1].unsigned_abs();
let uv_h = match layout {
PixelLayout::I420 => pic_h.div_ceil(2),
PixelLayout::I400 => 0,
_ => pic_h,
};
let uv_w = match layout {
PixelLayout::I420 | PixelLayout::I422 => pic_w.div_ceil(2),
PixelLayout::I400 => 0,
_ => pic_w,
};
let plane_y = copy_plane(picture.plane_bytes(0), y_stride, pic_h, pic_w, bps)?;
let plane_u = copy_plane(picture.plane_bytes(1), uv_stride, uv_h, uv_w, bps)?;
let plane_v = copy_plane(picture.plane_bytes(2), uv_stride, uv_h, uv_w, bps)?;
let out_y_stride = pic_w.checked_mul(bps).ok_or(AvifError::InvalidBox)?;
let out_uv_stride = uv_w.checked_mul(bps).ok_or(AvifError::InvalidBox)?;
let content_light_level = picture.content_light_level;
let picture_arc = Arc::new(picture);
let alpha: Option<AlphaPlane> = if let Some(aid) = alpha_item_id {
let alpha_item = self
.container
.items
.get(&aid)
.ok_or(AvifError::MissingBox("alpha item"))?;
if !alpha_item.item_type.is_image() {
return Err(AvifError::UnsupportedCodec);
}
let alpha_extents = alpha_item
.extents
.ok_or(AvifError::MissingBox("alpha ispe"))?;
if alpha_extents.width != extents.width || alpha_extents.height != extents.height {
return Err(AvifError::InvalidBox);
}
let alpha_cfg = alpha_item
.codec_config
.as_ref()
.ok_or(AvifError::MissingBox("alpha av2C"))?;
let alpha_obu = self.assemble_obu(alpha_item)?;
if alpha_obu.is_empty() {
return Err(AvifError::MissingBox("alpha mdat"));
}
let alpha_picture = self.run_decoder(alpha_obu)?;
if alpha_picture.p.w as u32 != extents.width
|| alpha_picture.p.h as u32 != extents.height
{
return Err(AvifError::InvalidBox);
}
let alpha_bpc =
u8::try_from(alpha_picture.p.bpc).map_err(|_| AvifError::InvalidCodecConfig)?;
let alpha_bps = alpha_picture.bytes_per_sample();
if alpha_cfg.pixel_layout() != PixelLayout::I400
|| alpha_picture.p.layout != PixelLayout::I400
|| alpha_cfg.bits_per_component() != alpha_bpc
{
return Err(AvifError::InvalidCodecConfig);
}
let a_w = alpha_picture.p.w as usize;
let a_h = alpha_picture.p.h as usize;
let a_stride = alpha_picture.stride[0].unsigned_abs();
let data = copy_plane(alpha_picture.plane_bytes(0), a_stride, a_h, a_w, alpha_bps)?;
let alpha_out_stride = a_w.checked_mul(alpha_bps).ok_or(AvifError::InvalidBox)?;
Some(AlphaPlane {
data,
stride: alpha_out_stride,
bits_per_component: alpha_bpc,
})
} else {
None
};
Ok(AvifImage {
width: extents.width,
height: extents.height,
pixel_layout: layout,
bits_per_component: bpc,
color_info: self.container.primary_item().and_then(|i| i.color_info),
content_light_level,
pixel_aspect_ratio: self
.container
.primary_item()
.and_then(|i| i.pixel_aspect_ratio),
planes: [plane_y, plane_u, plane_v],
strides: [out_y_stride, out_uv_stride],
alpha,
premultiplied_alpha,
orientation: self
.container
.primary_item()
.and_then(|i| i.orientation)
.unwrap_or(Orientation::Normal),
clean_aperture: self.container.primary_item().and_then(|i| i.clean_aperture),
icc_profile: self
.container
.primary_item()
.and_then(|i| i.icc_profile.clone()),
picture: picture_arc,
})
}
pub fn content_light_level(&self) -> Result<Option<ContentLightLevel>> {
let item = self
.container
.primary_item()
.ok_or(AvifError::MissingBox("pitm"))?;
self.item_content_light_level(item)
}
pub fn container(&self) -> &AvifContainer {
&self.container
}
pub fn settings(&self) -> &AvifSettings {
&self.settings
}
fn item_content_light_level(&self, item: &AvifItem) -> Result<Option<ContentLightLevel>> {
let obu_data = self.assemble_obu(item)?;
scan_content_light_level_from_obus(&obu_data)
}
fn assemble_obu(&self, item: &AvifItem) -> Result<Vec<u8>> {
let cfg = item
.codec_config
.as_ref()
.ok_or(AvifError::MissingBox("av2C"))?;
let config_len = cfg.config_obus.len();
let sample_len: u64 = item
.iloc_extents
.iter()
.try_fold(0u64, |acc, &(_, len)| acc.checked_add(len))
.ok_or(AvifError::InvalidBox)?;
let total_obu_len: u64 = (config_len as u64)
.checked_add(sample_len)
.ok_or(AvifError::InvalidBox)?;
if self.settings.max_obu_bytes != 0 && total_obu_len > self.settings.max_obu_bytes as u64 {
return Err(AvifError::LimitExceeded);
}
let mut obu_data: Vec<u8> = Vec::new();
obu_data
.try_reserve_exact(total_obu_len as usize)
.map_err(|_| AvifError::OutOfMemory)?;
if !cfg.config_obus.is_empty() {
obu_data.extend_from_slice(&cfg.config_obus);
}
for &(abs_offset, ext_len) in &item.iloc_extents {
let start = usize::try_from(abs_offset).map_err(|_| AvifError::ExtentOutOfBounds)?;
let len = usize::try_from(ext_len).map_err(|_| AvifError::ExtentOutOfBounds)?;
let end = start.checked_add(len).ok_or(AvifError::ExtentOutOfBounds)?;
let slice = self
.file
.get(start..end)
.ok_or(AvifError::ExtentOutOfBounds)?;
obu_data.extend_from_slice(slice);
}
Ok(obu_data)
}
fn run_decoder(&self, obu_data: Vec<u8>) -> Result<Picture> {
let mut settings = self.settings.decoder_settings.clone();
settings.run_decode = true;
let mut decoder = Decoder::open(&settings).map_err(AvifError::DecodeError)?;
decoder
.send_data(Some(Data::wrap(obu_data)))
.map_err(AvifError::DecodeError)?;
let _ = decoder.send_data(None);
match decoder.get_picture() {
Ok(p) => Ok(p),
Err(TealdustError::Again) => {
let _ = decoder.send_data(None);
match decoder.get_picture() {
Ok(p) => Ok(p),
Err(e) => Err(AvifError::DecodeError(e)),
}
}
Err(e) => Err(AvifError::DecodeError(e)),
}
}
}
fn scan_content_light_level_from_obus(obu_data: &[u8]) -> Result<Option<ContentLightLevel>> {
let mut pos = 0usize;
let mut content_light_level = None;
while pos < obu_data.len() {
let (obu_len, leb_len) = read_leb128_usize(&obu_data[pos..])?;
pos = pos.checked_add(leb_len).ok_or(AvifError::InvalidBox)?;
let end = pos.checked_add(obu_len).ok_or(AvifError::InvalidBox)?;
let body = obu_data.get(pos..end).ok_or(AvifError::InvalidBox)?;
pos = end;
if body.is_empty() {
continue;
}
let mut gb = GetBits::new(body);
let has_extension = gb.get_bit() != 0;
let obu_type_raw = gb.get_bits(5);
let _temporal_layer_id = gb.get_bits(2);
if has_extension {
let _multilayer_id = gb.get_bits(3);
let _extension_layer_id = gb.get_bits(5);
}
if gb.has_error() {
return Err(AvifError::InvalidCodecConfig);
}
if obu_type_raw == ObuType::Metadata as u32 {
let meta_type = gb.get_uleb128();
if gb.has_error() {
return Err(AvifError::InvalidCodecConfig);
}
if meta_type == ObuMetaType::HdrCll as u32 {
let cll = crate::obu::parse_cll(&mut gb);
if gb.has_error() {
return Err(AvifError::InvalidCodecConfig);
}
content_light_level = Some(cll);
}
}
}
Ok(content_light_level)
}
fn read_leb128_usize(data: &[u8]) -> Result<(usize, usize)> {
let mut value: u64 = 0;
let mut shift = 0u32;
for (i, &byte) in data.iter().take(8).enumerate() {
value |= ((byte & 0x7f) as u64) << shift;
if byte & 0x80 == 0 {
let len = i + 1;
let value = usize::try_from(value).map_err(|_| AvifError::LimitExceeded)?;
return Ok((value, len));
}
shift += 7;
}
Err(AvifError::InvalidBox)
}
fn copy_plane(
plane: Option<&[u8]>,
stride: usize,
h: usize,
w: usize,
bps: usize,
) -> Result<Vec<u8>> {
let Some(plane) = plane else {
return Ok(Vec::new());
};
if h == 0 || w == 0 {
return Ok(Vec::new());
}
let w_bytes = w.checked_mul(bps).ok_or(AvifError::InvalidBox)?;
let total = h.checked_mul(w_bytes).ok_or(AvifError::InvalidBox)?;
let mut out = Vec::new();
out.try_reserve_exact(total)
.map_err(|_| AvifError::OutOfMemory)?;
for row in 0..h {
let row_offset = row.checked_mul(stride).ok_or(AvifError::InvalidBox)?;
let row_end = row_offset
.checked_add(w_bytes)
.ok_or(AvifError::InvalidBox)?;
let row_slice = plane
.get(row_offset..row_end)
.ok_or(AvifError::InvalidBox)?;
out.extend_from_slice(row_slice);
}
Ok(out)
}
pub fn probe_avif(data: &[u8]) -> bool {
if data.len() < 12 {
return false;
}
if &data[4..8] != b"ftyp" {
return false;
}
let brand = [data[8], data[9], data[10], data[11]];
is_avif_brand(&brand)
}