const MAX_NAL_BYTES: usize = 32 * 1024 * 1024;
const MAX_DIMENSION: u32 = 8192;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AvcError {
InvalidData(&'static str),
Unsupported(&'static str),
UnsupportedProfile(u8),
TooLarge,
Incomplete,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SequenceParameters {
pub id: u32,
pub profile_idc: u8,
pub level_idc: u8,
pub width: u32,
pub height: u32,
pub crop_left: u32,
pub crop_top: u32,
pub chroma_format_idc: u32,
pub bit_depth_luma: u32,
pub bit_depth_chroma: u32,
pub scaling_matrices_present: bool,
pub width_mbs: u32,
pub frame_height_mbs: u32,
pub frame_mbs_only: bool,
pub direct_8x8_inference: bool,
pub frame_num_bits: usize,
pub max_num_ref_frames: u32,
pub pic_order_cnt_type: u32,
pub pic_order_cnt_lsb_bits: Option<usize>,
}
pub(super) fn type0_pic_order_count(
lsb: u32,
lsb_bits: usize,
previous_reference: Option<(i32, u32)>,
) -> Result<(i32, i32), AvcError> {
if !(4..=16).contains(&lsb_bits) {
return Err(AvcError::InvalidData("invalid POC LSB width"));
}
let maximum = 1u32 << lsb_bits;
if lsb >= maximum {
return Err(AvcError::InvalidData("POC LSB out of range"));
}
let (previous_msb, previous_lsb) = previous_reference.unwrap_or((0, 0));
if previous_lsb >= maximum {
return Err(AvcError::InvalidData("previous POC LSB out of range"));
}
let msb = if lsb < previous_lsb && previous_lsb - lsb >= maximum / 2 {
previous_msb.checked_add(maximum as i32)
} else if lsb > previous_lsb && lsb - previous_lsb > maximum / 2 {
previous_msb.checked_sub(maximum as i32)
} else {
Some(previous_msb)
}
.ok_or(AvcError::TooLarge)?;
let full = msb.checked_add(lsb as i32).ok_or(AvcError::TooLarge)?;
Ok((msb, full))
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PictureParameters2003 {
pub id: u32,
pub sequence_id: u32,
pub cabac: bool,
pub pic_order_present: bool,
pub slice_groups: u32,
pub slice_group_map_type: Option<u32>,
pub ref_idx_l0: u32,
pub ref_idx_l1: u32,
pub weighted_pred: bool,
pub weighted_bipred_idc: u32,
pub pic_init_qp: i32,
pub chroma_qp_index_offset: i32,
pub deblocking_filter_control_present: bool,
pub constrained_intra_pred: bool,
pub redundant_pic_cnt_present: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PictureParameters2005 {
pub core: PictureParameters2003,
pub transform_8x8: bool,
pub scaling_matrices_present: bool,
pub second_chroma_qp_index_offset: i32,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CabacIdrISlice {
pub first_mb: u32,
pub frame_num: u32,
pub pic_order_cnt_lsb: u32,
pub slice_qp: i32,
pub deblocking_disabled: bool,
pub alpha_offset: i32,
pub beta_offset: i32,
pub rbsp: Vec<u8>,
pub data_byte_offset: usize,
pub marking: Vec<MemoryManagement>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CabacInterSlice {
pub first_mb: u32,
pub slice_type: u32,
pub frame_num: u32,
pub pic_order_cnt_lsb: u32,
pub ref_idx_l0: u32,
pub ref_idx_l1: u32,
pub reorder_l0: Vec<RefPicReorder>,
pub reorder_l1: Vec<RefPicReorder>,
pub marking: Vec<MemoryManagement>,
pub direct_spatial_mv_pred: bool,
pub cabac_init_idc: u32,
pub slice_qp: i32,
pub deblocking_disabled: bool,
pub alpha_offset: i32,
pub beta_offset: i32,
pub weights: Option<PredictionWeightTable>,
pub rbsp: Vec<u8>,
pub data_byte_offset: usize,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RefPicReorder {
Subtract(u32),
Add(u32),
LongTerm(u32),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MemoryManagement {
ForgetShortTerm(u32),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PredictionWeight {
pub luma_weight: i32,
pub luma_offset: i32,
pub chroma_weight: [i32; 2],
pub chroma_offset: [i32; 2],
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PredictionWeightTable {
pub luma_denom: u32,
pub chroma_denom: u32,
pub list0: Vec<PredictionWeight>,
pub list1: Vec<PredictionWeight>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AvcConfig {
pub nal_length_size: usize,
pub sequence_parameters: Vec<SequenceParameters>,
pub picture_parameter_sets: Vec<Vec<u8>>,
}
impl AvcConfig {
pub fn parse(data: &[u8]) -> Result<Self, AvcError> {
if data.len() < 7 || data[0] != 1 || data[4] & 0xfc != 0xfc || data[5] & 0xe0 != 0xe0 {
return Err(AvcError::InvalidData("invalid avcC header"));
}
let nal_length_size = usize::from((data[4] & 3) + 1);
if nal_length_size == 3 {
return Err(AvcError::InvalidData("invalid NAL length size"));
}
let mut offset = 6;
let mut sequence_parameters = Vec::new();
for _ in 0..(data[5] & 31) {
let nal = take_nal(data, &mut offset)?;
let parameters = parse_sps(nal)?;
if parameters.profile_idc != data[1] || parameters.level_idc != data[3] {
return Err(AvcError::InvalidData("avcC and SPS disagree"));
}
sequence_parameters.push(parameters);
}
let count = *data.get(offset).ok_or(AvcError::Incomplete)?;
offset += 1;
let mut picture_parameter_sets = Vec::new();
for _ in 0..count {
let nal = take_nal(data, &mut offset)?;
if nal
.first()
.is_none_or(|byte| byte & 0x1f != 8 || byte & 0x80 != 0)
{
return Err(AvcError::InvalidData("invalid PPS NAL"));
}
picture_parameter_sets.push(nal.to_vec());
}
if sequence_parameters.is_empty() || picture_parameter_sets.is_empty() {
return Err(AvcError::InvalidData("missing parameter sets"));
}
Ok(Self {
nal_length_size,
sequence_parameters,
picture_parameter_sets,
})
}
}
fn take_nal<'a>(data: &'a [u8], offset: &mut usize) -> Result<&'a [u8], AvcError> {
let size = data.get(*offset..*offset + 2).ok_or(AvcError::Incomplete)?;
let size = u16::from_be_bytes([size[0], size[1]]) as usize;
*offset += 2;
if size == 0 {
return Err(AvcError::InvalidData("empty parameter set"));
}
let nal = data
.get(*offset..*offset + size)
.ok_or(AvcError::Incomplete)?;
*offset += size;
Ok(nal)
}
pub struct NalStream {
length_size: usize,
pending: Vec<u8>,
}
impl NalStream {
pub fn new(length_size: usize) -> Result<Self, AvcError> {
if !matches!(length_size, 1 | 2 | 4) {
return Err(AvcError::InvalidData("invalid NAL length size"));
}
Ok(Self {
length_size,
pending: Vec::new(),
})
}
pub fn push(&mut self, bytes: &[u8]) -> Result<Vec<Vec<u8>>, AvcError> {
let mut units = Vec::new();
for chunk in bytes.chunks(16 * 1024) {
self.pending.extend_from_slice(chunk);
let mut consumed = 0;
while self.pending.len() - consumed >= self.length_size {
let mut size = 0usize;
for byte in &self.pending[consumed..consumed + self.length_size] {
size = (size << 8) | usize::from(*byte);
}
if size == 0 || size > MAX_NAL_BYTES {
return Err(AvcError::TooLarge);
}
let end = consumed + self.length_size + size;
if end > self.pending.len() {
break;
}
let nal = &self.pending[consumed + self.length_size..end];
if nal[0] & 0x80 != 0 || nal[0] & 0x1f == 0 {
return Err(AvcError::InvalidData("invalid NAL header"));
}
units.push(nal.to_vec());
consumed = end;
}
self.pending.drain(..consumed);
if self.pending.len() > MAX_NAL_BYTES + self.length_size {
return Err(AvcError::TooLarge);
}
}
Ok(units)
}
pub fn finish(&self) -> Result<(), AvcError> {
if self.pending.is_empty() {
Ok(())
} else {
Err(AvcError::Incomplete)
}
}
}
struct Bits<'a> {
bytes: &'a [u8],
bit: usize,
}
impl Bits<'_> {
fn read(&mut self, count: usize) -> Result<u32, AvcError> {
if count > 32
|| self
.bit
.checked_add(count)
.is_none_or(|end| end > self.bytes.len() * 8)
{
return Err(AvcError::Incomplete);
}
let mut value = 0;
for _ in 0..count {
value = (value << 1) | u32::from((self.bytes[self.bit / 8] >> (7 - self.bit % 8)) & 1);
self.bit += 1;
}
Ok(value)
}
fn ue(&mut self) -> Result<u32, AvcError> {
let mut zeros = 0;
while self.read(1)? == 0 {
zeros += 1;
if zeros > 30 {
return Err(AvcError::InvalidData("Exp-Golomb value too large"));
}
}
Ok(((1u32 << zeros) - 1) + self.read(zeros)?)
}
fn se(&mut self) -> Result<i32, AvcError> {
let code = self.ue()?;
Ok(if code & 1 == 0 {
-(code as i32 / 2)
} else {
(code as i32 + 1) / 2
})
}
fn finish_rbsp(&mut self) -> Result<(), AvcError> {
if self.read(1)? != 1 {
return Err(AvcError::InvalidData("missing RBSP stop bit"));
}
while self.bit % 8 != 0 {
if self.read(1)? != 0 {
return Err(AvcError::InvalidData("invalid RBSP padding"));
}
}
if self.bit != self.bytes.len() * 8 {
return Err(AvcError::InvalidData("unexpected RBSP data"));
}
Ok(())
}
fn more_rbsp_data(&self) -> Result<bool, AvcError> {
let remaining = self.bytes.len() * 8 - self.bit;
if remaining == 0 {
return Err(AvcError::Incomplete);
}
if remaining > 8 {
return Ok(true);
}
let mut probe = Bits {
bytes: self.bytes,
bit: self.bit,
};
if probe.read(1)? != 1 {
return Ok(true);
}
for _ in 1..remaining {
if probe.read(1)? != 0 {
return Ok(true);
}
}
Ok(false)
}
}
fn rbsp_from_nal(nal: &[u8], expected_type: u8) -> Result<Vec<u8>, AvcError> {
if nal
.first()
.is_none_or(|byte| byte & 0x80 != 0 || byte & 0x1f != expected_type)
{
return Err(AvcError::InvalidData("invalid parameter-set NAL"));
}
let mut rbsp = Vec::with_capacity(nal.len().saturating_sub(1));
let mut zero_count = 0;
for &byte in &nal[1..] {
if zero_count == 2 && byte == 3 {
zero_count = 0;
continue;
}
rbsp.push(byte);
zero_count = if byte == 0 {
(zero_count + 1).min(2)
} else {
0
};
}
Ok(rbsp)
}
pub fn parse_pps_2003(nal: &[u8]) -> Result<PictureParameters2003, AvcError> {
let rbsp = rbsp_from_nal(nal, 8)?;
let mut bits = Bits {
bytes: &rbsp,
bit: 0,
};
let pps = parse_pps_fields(&mut bits)?;
bits.finish_rbsp()?;
Ok(pps)
}
pub fn parse_pps_2005(nal: &[u8]) -> Result<PictureParameters2005, AvcError> {
let rbsp = rbsp_from_nal(nal, 8)?;
let mut bits = Bits {
bytes: &rbsp,
bit: 0,
};
let core = parse_pps_fields(&mut bits)?;
let mut transform_8x8 = false;
let mut scaling_matrices_present = false;
let mut second_chroma_qp_index_offset = core.chroma_qp_index_offset;
if bits.more_rbsp_data()? {
transform_8x8 = bits.read(1)? != 0;
scaling_matrices_present = bits.read(1)? != 0;
if scaling_matrices_present {
for index in 0..(6 + if transform_8x8 { 2 } else { 0 }) {
if bits.read(1)? != 0 {
skip_scaling_list(&mut bits, if index < 6 { 16 } else { 64 })?;
}
}
}
second_chroma_qp_index_offset = bits.se()?;
if !(-12..=12).contains(&second_chroma_qp_index_offset) {
return Err(AvcError::InvalidData("chroma QP offset out of range"));
}
}
bits.finish_rbsp()?;
Ok(PictureParameters2005 {
core,
transform_8x8,
scaling_matrices_present,
second_chroma_qp_index_offset,
})
}
fn parse_pps_fields(bits: &mut Bits<'_>) -> Result<PictureParameters2003, AvcError> {
let id = bits.ue()?;
let sequence_id = bits.ue()?;
if id > 255 || sequence_id > 31 {
return Err(AvcError::InvalidData("parameter-set ID out of range"));
}
let cabac = bits.read(1)? != 0;
let pic_order_present = bits.read(1)? != 0;
let slice_groups = bits.ue()?.checked_add(1).ok_or(AvcError::TooLarge)?;
if slice_groups > 8 {
return Err(AvcError::TooLarge);
}
let slice_group_map_type = if slice_groups > 1 {
let map_type = bits.ue()?;
match map_type {
0 => {
for _ in 0..slice_groups {
bits.ue()?;
}
}
1 => {}
2 => {
for _ in 1..slice_groups {
bits.ue()?;
bits.ue()?;
}
}
3..=5 => {
bits.read(1)?;
bits.ue()?;
}
6 => {
let count = bits.ue()?.checked_add(1).ok_or(AvcError::TooLarge)?;
if count > 65_536 {
return Err(AvcError::TooLarge);
}
let width = (32 - (slice_groups - 1).leading_zeros()) as usize;
for _ in 0..count {
if bits.read(width)? >= slice_groups {
return Err(AvcError::InvalidData("slice group ID out of range"));
}
}
}
_ => return Err(AvcError::InvalidData("invalid slice group map type")),
}
Some(map_type)
} else {
None
};
let ref_idx_l0 = bits.ue()?.checked_add(1).ok_or(AvcError::TooLarge)?;
let ref_idx_l1 = bits.ue()?.checked_add(1).ok_or(AvcError::TooLarge)?;
if ref_idx_l0 > 32 || ref_idx_l1 > 32 {
return Err(AvcError::TooLarge);
}
let weighted_pred = bits.read(1)? != 0;
let weighted_bipred_idc = bits.read(2)?;
if weighted_bipred_idc == 3 {
return Err(AvcError::InvalidData("invalid weighted bipred mode"));
}
let pic_init_qp = 26 + bits.se()?;
bits.se()?; let chroma_qp_index_offset = bits.se()?;
if !(-12..=12).contains(&chroma_qp_index_offset) {
return Err(AvcError::InvalidData("chroma QP offset out of range"));
}
let deblocking_filter_control_present = bits.read(1)? != 0;
let constrained_intra_pred = bits.read(1)? != 0;
let redundant_pic_cnt_present = bits.read(1)? != 0;
Ok(PictureParameters2003 {
id,
sequence_id,
cabac,
pic_order_present,
slice_groups,
slice_group_map_type,
ref_idx_l0,
ref_idx_l1,
weighted_pred,
weighted_bipred_idc,
pic_init_qp,
chroma_qp_index_offset,
deblocking_filter_control_present,
constrained_intra_pred,
redundant_pic_cnt_present,
})
}
pub fn parse_cabac_idr_i_slice(
nal: &[u8],
sps: &SequenceParameters,
pps: &PictureParameters2003,
) -> Result<CabacIdrISlice, AvcError> {
if nal.first().is_none_or(|header| header & 0x1f != 5) {
return Err(AvcError::InvalidData("expected IDR I slice"));
}
parse_cabac_i_slice(nal, sps, pps)
}
pub fn parse_slice_type(nal: &[u8]) -> Result<u32, AvcError> {
let nal_type = nal.first().ok_or(AvcError::Incomplete)? & 0x1f;
let rbsp = rbsp_from_nal(nal, nal_type)?;
let mut bits = Bits {
bytes: &rbsp,
bit: 0,
};
bits.ue()?;
bits.ue()
}
pub fn parse_cabac_i_slice(
nal: &[u8],
sps: &SequenceParameters,
pps: &PictureParameters2003,
) -> Result<CabacIdrISlice, AvcError> {
let nal_type = nal.first().ok_or(AvcError::Incomplete)? & 0x1f;
if !matches!(nal_type, 1 | 5) {
return Err(AvcError::InvalidData("expected I slice"));
}
if !sps.frame_mbs_only || sps.pic_order_cnt_type != 0 || pps.slice_groups != 1 || !pps.cabac {
return Err(AvcError::Unsupported("CABAC IDR header shape"));
}
if pps.sequence_id != sps.id {
return Err(AvcError::InvalidData("PPS refers to another SPS"));
}
if nal_type == 5 && nal[0] & 0x60 == 0 {
return Err(AvcError::InvalidData("IDR is not a reference picture"));
}
let rbsp = rbsp_from_nal(nal, nal_type)?;
let mut bits = Bits {
bytes: &rbsp,
bit: 0,
};
let first_mb = bits.ue()?;
if first_mb >= sps.width_mbs * sps.frame_height_mbs {
return Err(AvcError::InvalidData("first macroblock out of bounds"));
}
if !matches!(bits.ue()?, 2 | 7) || bits.ue()? != pps.id {
return Err(AvcError::Unsupported("expected I slice for PPS"));
}
let frame_num = bits.read(sps.frame_num_bits)?;
if nal_type == 5 {
bits.ue()?; }
let pic_order_cnt_lsb = bits.read(
sps.pic_order_cnt_lsb_bits
.ok_or(AvcError::Unsupported("POC type"))?,
)?;
if pps.pic_order_present {
bits.se()?;
}
if pps.redundant_pic_cnt_present {
bits.ue()?;
}
let mut marking = Vec::new();
if nal_type == 5 {
bits.read(2)?; } else if nal[0] & 0x60 != 0 && bits.read(1)? != 0 {
for _ in 0..32 {
match bits.ue()? {
0 => break,
1 => marking.push(MemoryManagement::ForgetShortTerm(bits.ue()?)),
2..=6 => {
return Err(AvcError::Unsupported(
"adaptive reference marking operation",
));
}
_ => return Err(AvcError::InvalidData("invalid reference marking operation")),
}
}
if marking.len() == 32 {
return Err(AvcError::TooLarge);
}
}
let slice_qp = pps.pic_init_qp + bits.se()?;
if !(0..=51).contains(&slice_qp) {
return Err(AvcError::InvalidData("slice QP out of range"));
}
let mut deblocking_disabled = false;
let mut alpha_offset = 0;
let mut beta_offset = 0;
if pps.deblocking_filter_control_present {
let disable_idc = bits.ue()?;
if disable_idc > 2 {
return Err(AvcError::InvalidData("invalid deblocking mode"));
}
if disable_idc != 1 {
alpha_offset = bits.se()? * 2;
beta_offset = bits.se()? * 2;
}
deblocking_disabled = disable_idc == 1;
}
while bits.bit % 8 != 0 {
if bits.read(1)? != 1 {
return Err(AvcError::InvalidData("invalid CABAC alignment"));
}
}
let data_byte_offset = bits.bit / 8;
if data_byte_offset >= rbsp.len() {
return Err(AvcError::Incomplete);
}
Ok(CabacIdrISlice {
first_mb,
frame_num,
pic_order_cnt_lsb,
slice_qp,
deblocking_disabled,
alpha_offset,
beta_offset,
rbsp,
data_byte_offset,
marking,
})
}
fn parse_ref_pic_list_reordering(bits: &mut Bits<'_>) -> Result<Vec<RefPicReorder>, AvcError> {
if bits.read(1)? == 0 {
return Ok(Vec::new());
}
let mut changes = Vec::new();
for _ in 0..32 {
match bits.ue()? {
0 => changes.push(RefPicReorder::Subtract(bits.ue()?)),
1 => changes.push(RefPicReorder::Add(bits.ue()?)),
2 => changes.push(RefPicReorder::LongTerm(bits.ue()?)),
3 => return Ok(changes),
_ => {
return Err(AvcError::InvalidData(
"invalid reference picture reordering",
));
}
}
}
Err(AvcError::TooLarge)
}
fn parse_prediction_weight_table(
bits: &mut Bits<'_>,
is_b: bool,
ref_idx_l0: u32,
ref_idx_l1: u32,
) -> Result<PredictionWeightTable, AvcError> {
let luma_denom = bits.ue()?;
let chroma_denom = bits.ue()?;
if luma_denom > 7 || chroma_denom > 7 {
return Err(AvcError::InvalidData(
"prediction weight denominator out of range",
));
}
let read_list = |bits: &mut Bits<'_>, count: u32| -> Result<Vec<PredictionWeight>, AvcError> {
let mut list = Vec::with_capacity(count as usize);
for _ in 0..count {
let mut weight = PredictionWeight {
luma_weight: 1 << luma_denom,
luma_offset: 0,
chroma_weight: [1 << chroma_denom; 2],
chroma_offset: [0; 2],
};
if bits.read(1)? != 0 {
weight.luma_weight = bits.se()?;
weight.luma_offset = bits.se()?;
}
if bits.read(1)? != 0 {
for channel in 0..2 {
weight.chroma_weight[channel] = bits.se()?;
weight.chroma_offset[channel] = bits.se()?;
}
}
list.push(weight);
}
Ok(list)
};
let list0 = read_list(bits, ref_idx_l0)?;
let list1 = if is_b {
read_list(bits, ref_idx_l1)?
} else {
Vec::new()
};
Ok(PredictionWeightTable {
luma_denom,
chroma_denom,
list0,
list1,
})
}
pub fn parse_cabac_inter_slice(
nal: &[u8],
sps: &SequenceParameters,
pps: &PictureParameters2003,
) -> Result<CabacInterSlice, AvcError> {
if !sps.frame_mbs_only || sps.pic_order_cnt_type != 0 || pps.slice_groups != 1 || !pps.cabac {
return Err(AvcError::Unsupported("CABAC inter header shape"));
}
if pps.sequence_id != sps.id {
return Err(AvcError::InvalidData(
"inter picture PPS refers to another SPS",
));
}
let rbsp = rbsp_from_nal(nal, 1)?;
let mut bits = Bits {
bytes: &rbsp,
bit: 0,
};
let first_mb = bits.ue()?;
if first_mb >= sps.width_mbs * sps.frame_height_mbs {
return Err(AvcError::InvalidData("first macroblock out of bounds"));
}
let slice_type = bits.ue()?;
if !matches!(slice_type, 0 | 1 | 5 | 6) || bits.ue()? != pps.id {
return Err(AvcError::Unsupported("expected P or B slice for PPS"));
}
let is_b = slice_type % 5 == 1;
let frame_num = bits.read(sps.frame_num_bits)?;
let pic_order_cnt_lsb = bits.read(
sps.pic_order_cnt_lsb_bits
.ok_or(AvcError::Unsupported("POC type"))?,
)?;
if pps.pic_order_present {
bits.se()?;
}
if pps.redundant_pic_cnt_present {
bits.ue()?;
}
let direct_spatial_mv_pred = is_b && bits.read(1)? != 0;
let mut ref_idx_l0 = pps.ref_idx_l0;
let mut ref_idx_l1 = pps.ref_idx_l1;
if bits.read(1)? != 0 {
ref_idx_l0 = bits.ue()?.checked_add(1).ok_or(AvcError::TooLarge)?;
if is_b {
ref_idx_l1 = bits.ue()?.checked_add(1).ok_or(AvcError::TooLarge)?;
}
}
if ref_idx_l0 > 32 || ref_idx_l1 > 32 {
return Err(AvcError::TooLarge);
}
let reorder_l0 = parse_ref_pic_list_reordering(&mut bits)?;
let reorder_l1 = if is_b {
parse_ref_pic_list_reordering(&mut bits)?
} else {
Vec::new()
};
let weights = if (pps.weighted_pred && !is_b) || (pps.weighted_bipred_idc == 1 && is_b) {
Some(parse_prediction_weight_table(
&mut bits, is_b, ref_idx_l0, ref_idx_l1,
)?)
} else {
None
};
let mut marking = Vec::new();
if nal[0] & 0x60 != 0 && bits.read(1)? != 0 {
for _ in 0..32 {
match bits.ue()? {
0 => break,
1 => marking.push(MemoryManagement::ForgetShortTerm(bits.ue()?)),
2..=6 => {
return Err(AvcError::Unsupported(
"adaptive reference marking operation",
));
}
_ => return Err(AvcError::InvalidData("invalid reference marking operation")),
}
}
if marking.len() == 32 {
return Err(AvcError::TooLarge);
}
}
let cabac_init_idc = bits.ue()?;
if cabac_init_idc > 2 {
return Err(AvcError::InvalidData("CABAC initialization out of range"));
}
let slice_qp = pps.pic_init_qp + bits.se()?;
if !(0..=51).contains(&slice_qp) {
return Err(AvcError::InvalidData("slice QP out of range"));
}
let mut deblocking_disabled = false;
let mut alpha_offset = 0;
let mut beta_offset = 0;
if pps.deblocking_filter_control_present {
let disable_idc = bits.ue()?;
if disable_idc > 2 {
return Err(AvcError::InvalidData("invalid deblocking mode"));
}
if disable_idc != 1 {
alpha_offset = bits.se()? * 2;
beta_offset = bits.se()? * 2;
}
deblocking_disabled = disable_idc == 1;
}
while bits.bit % 8 != 0 {
if bits.read(1)? != 1 {
return Err(AvcError::InvalidData("invalid CABAC alignment"));
}
}
let data_byte_offset = bits.bit / 8;
if data_byte_offset >= rbsp.len() {
return Err(AvcError::Incomplete);
}
Ok(CabacInterSlice {
first_mb,
slice_type,
frame_num,
pic_order_cnt_lsb,
ref_idx_l0,
ref_idx_l1,
reorder_l0,
reorder_l1,
marking,
direct_spatial_mv_pred,
cabac_init_idc,
slice_qp,
deblocking_disabled,
alpha_offset,
beta_offset,
weights,
rbsp,
data_byte_offset,
})
}
fn skip_scaling_list(bits: &mut Bits<'_>, count: usize) -> Result<(), AvcError> {
let mut last = 8i32;
let mut next = 8i32;
for _ in 0..count {
if next != 0 {
next = (last + bits.se()? + 256).rem_euclid(256);
}
if next != 0 {
last = next;
}
}
Ok(())
}
pub fn parse_sps(nal: &[u8]) -> Result<SequenceParameters, AvcError> {
let rbsp = rbsp_from_nal(nal, 7)?;
let mut bits = Bits {
bytes: &rbsp,
bit: 0,
};
let profile_idc = bits.read(8)? as u8;
if !matches!(profile_idc, 66 | 77 | 88 | 100 | 110 | 122 | 144) {
return Err(AvcError::UnsupportedProfile(profile_idc));
}
let flags = bits.read(8)?;
let reserved_mask = if matches!(profile_idc, 66 | 77 | 88) {
0x1f
} else {
0x0f
};
if flags & reserved_mask != 0 {
return Err(AvcError::InvalidData("nonzero reserved SPS bits"));
}
let level_idc = bits.read(8)? as u8;
let id = bits.ue()?;
if id > 31 {
return Err(AvcError::InvalidData("SPS ID out of range"));
}
let mut chroma_format_idc = 1;
let mut bit_depth_luma = 8;
let mut bit_depth_chroma = 8;
let mut scaling_matrices_present = false;
if matches!(profile_idc, 100 | 110 | 122 | 144) {
chroma_format_idc = bits.ue()?;
if chroma_format_idc > 3 {
return Err(AvcError::InvalidData("invalid chroma format"));
}
if chroma_format_idc == 3 {
bits.read(1)?; }
bit_depth_luma = 8 + bits.ue()?;
bit_depth_chroma = 8 + bits.ue()?;
if bit_depth_luma > 14 || bit_depth_chroma > 14 {
return Err(AvcError::InvalidData("invalid bit depth"));
}
bits.read(1)?; scaling_matrices_present = bits.read(1)? != 0;
if scaling_matrices_present {
for index in 0..8 {
if bits.read(1)? != 0 {
skip_scaling_list(&mut bits, if index < 6 { 16 } else { 64 })?;
}
}
}
}
let frame_num_bits = bits.ue()?.checked_add(4).ok_or(AvcError::TooLarge)? as usize;
if frame_num_bits > 16 {
return Err(AvcError::InvalidData("frame number field too wide"));
}
let pic_order_cnt_type = bits.ue()?;
let pic_order_cnt_lsb_bits = match pic_order_cnt_type {
0 => {
let count = bits.ue()?.checked_add(4).ok_or(AvcError::TooLarge)? as usize;
if count > 16 {
return Err(AvcError::InvalidData("POC field too wide"));
}
Some(count)
}
1 => {
bits.read(1)?;
bits.se()?;
bits.se()?;
let count = bits.ue()?;
if count > 255 {
return Err(AvcError::TooLarge);
}
for _ in 0..count {
bits.se()?;
}
None
}
2 => None,
_ => return Err(AvcError::InvalidData("invalid picture order count type")),
};
let max_num_ref_frames = bits.ue()?;
if max_num_ref_frames > 16 {
return Err(AvcError::TooLarge);
}
bits.read(1)?; let width_mbs = bits.ue()?.checked_add(1).ok_or(AvcError::TooLarge)?;
let height_map_units = bits.ue()?.checked_add(1).ok_or(AvcError::TooLarge)?;
let frame_mbs_only = bits.read(1)? != 0;
if !frame_mbs_only {
bits.read(1)?;
}
let direct_8x8_inference = bits.read(1)? != 0;
let crop = if bits.read(1)? != 0 {
[bits.ue()?, bits.ue()?, bits.ue()?, bits.ue()?]
} else {
[0; 4]
};
let frame_height_units = if frame_mbs_only { 1u32 } else { 2 };
let (sub_x, sub_y) = match chroma_format_idc {
0 => (1, 1),
1 => (2, 2),
2 => (2, 1),
_ => (1, 1),
};
let width = width_mbs
.checked_mul(16)
.and_then(|n| n.checked_sub((crop[0] + crop[1]).checked_mul(sub_x)?))
.ok_or(AvcError::TooLarge)?;
let height = height_map_units
.checked_mul(16)
.and_then(|n| n.checked_mul(frame_height_units))
.and_then(|n| n.checked_sub((crop[2] + crop[3]).checked_mul(sub_y * frame_height_units)?))
.ok_or(AvcError::TooLarge)?;
if width == 0 || height == 0 || width > MAX_DIMENSION || height > MAX_DIMENSION {
return Err(AvcError::TooLarge);
}
Ok(SequenceParameters {
id,
profile_idc,
level_idc,
width,
height,
crop_left: crop[0].checked_mul(sub_x).ok_or(AvcError::TooLarge)?,
crop_top: crop[2].checked_mul(sub_y * frame_height_units).ok_or(AvcError::TooLarge)?,
chroma_format_idc,
bit_depth_luma,
bit_depth_chroma,
scaling_matrices_present,
width_mbs,
frame_height_mbs: height_map_units * frame_height_units,
frame_mbs_only,
direct_8x8_inference,
frame_num_bits,
max_num_ref_frames,
pic_order_cnt_type,
pic_order_cnt_lsb_bits,
})
}
fn intra_dc(plane: &[u8], stride: usize, x: usize, y: usize, size: usize) -> u8 {
let top = (y > 0).then(|| {
(0..size)
.map(|i| u32::from(plane[(y - 1) * stride + x + i]))
.sum::<u32>()
});
let left = (x > 0).then(|| {
(0..size)
.map(|i| u32::from(plane[(y + i) * stride + x - 1]))
.sum::<u32>()
});
match (top, left) {
(Some(top), Some(left)) => ((top + left + size as u32) / (2 * size as u32)) as u8,
(Some(sum), None) | (None, Some(sum)) => ((sum + (size / 2) as u32) / size as u32) as u8,
(None, None) => 128,
}
}
fn chroma_dc(plane: &[u8], stride: usize, x: usize, y: usize, quadrant: usize) -> u8 {
let qx = quadrant % 2;
let qy = quadrant / 2;
let top = (y > 0).then(|| {
(0..4)
.map(|i| u32::from(plane[(y - 1) * stride + x + qx * 4 + i]))
.sum::<u32>()
});
let left = (x > 0).then(|| {
(0..4)
.map(|i| u32::from(plane[(y + qy * 4 + i) * stride + x - 1]))
.sum::<u32>()
});
let (top, left) = match (qx, qy) {
(1, 0) => (top, None),
(0, 1) => (None, left),
_ => (top, left),
};
match (top, left) {
(Some(top), Some(left)) => ((top + left + 4) >> 3) as u8,
(Some(sum), None) | (None, Some(sum)) => ((sum + 2) >> 2) as u8,
(None, None) => 128,
}
}
pub fn decode_intra_2003(
nal: &[u8],
sps: &SequenceParameters,
pps: &PictureParameters2003,
) -> Result<super::VideoFrame, AvcError> {
if sps.profile_idc != 66
|| sps.chroma_format_idc != 1
|| sps.bit_depth_luma != 8
|| sps.bit_depth_chroma != 8
|| !sps.frame_mbs_only
|| sps.pic_order_cnt_type != 0
|| pps.cabac
|| pps.slice_groups != 1
|| pps.pic_order_present
|| pps.redundant_pic_cnt_present
|| pps.sequence_id != sps.id
|| sps.width != sps.width_mbs * 16
|| sps.height != sps.frame_height_mbs * 16
{
return Err(AvcError::Unsupported(
"I_PCM decoder requires uncropped progressive Baseline 4:2:0",
));
}
if nal
.first()
.is_none_or(|byte| byte & 0x1f != 5 || byte & 0x80 != 0 || byte & 0x60 == 0)
{
return Err(AvcError::InvalidData("expected reference IDR slice"));
}
let rbsp = rbsp_from_nal(nal, 5)?;
let mut bits = Bits {
bytes: &rbsp,
bit: 0,
};
let first_mb = bits.ue()?;
let slice_type = bits.ue()?;
let pps_id = bits.ue()?;
if first_mb != 0 || !matches!(slice_type, 2 | 7) || pps_id != pps.id {
return Err(AvcError::Unsupported("expected a complete I slice"));
}
bits.read(sps.frame_num_bits)?;
bits.ue()?; bits.read(
sps.pic_order_cnt_lsb_bits
.ok_or(AvcError::Unsupported("POC type"))?,
)?;
bits.read(2)?; bits.se()?; if pps.deblocking_filter_control_present {
if bits.ue()? != 1 {
bits.se()?;
bits.se()?;
}
}
let pixels = u64::from(sps.width) * u64::from(sps.height);
if pixels > 8 * 1024 * 1024 {
return Err(AvcError::TooLarge);
}
let luma_stride = sps.width as usize;
let chroma_stride = luma_stride / 2;
let mut luma = vec![0u8; pixels as usize];
let mut cb = vec![0u8; pixels as usize / 4];
let mut cr = vec![0u8; pixels as usize / 4];
let macroblocks = sps
.width_mbs
.checked_mul(sps.frame_height_mbs)
.ok_or(AvcError::TooLarge)?;
for mb in 0..macroblocks {
let mb_x = (mb % sps.width_mbs) as usize;
let mb_y = (mb / sps.width_mbs) as usize;
let x0 = mb_x * 16;
let y0 = mb_y * 16;
let cx0 = mb_x * 8;
let cy0 = mb_y * 8;
match bits.ue()? {
25 => {
while bits.bit % 8 != 0 {
if bits.read(1)? != 0 {
return Err(AvcError::InvalidData("invalid PCM alignment"));
}
}
for y in 0..16 {
for x in 0..16 {
luma[(y0 + y) * luma_stride + x0 + x] = bits.read(8)? as u8;
}
}
for plane in [&mut cb, &mut cr] {
for y in 0..8 {
for x in 0..8 {
plane[(cy0 + y) * chroma_stride + cx0 + x] = bits.read(8)? as u8;
}
}
}
}
3 => {
if bits.ue()? != 0 || bits.se()? != 0 || bits.read(1)? != 1 {
return Err(AvcError::Unsupported(
"Intra16x16 DC requires zero residual and chroma DC",
));
}
let dc = intra_dc(&luma, luma_stride, x0, y0, 16);
for y in 0..16 {
luma[(y0 + y) * luma_stride + x0..(y0 + y) * luma_stride + x0 + 16].fill(dc);
}
for plane in [&mut cb, &mut cr] {
for quadrant in 0..4 {
let value = chroma_dc(plane, chroma_stride, cx0, cy0, quadrant);
let qx = quadrant % 2;
let qy = quadrant / 2;
for y in 0..4 {
let start = (cy0 + qy * 4 + y) * chroma_stride + cx0 + qx * 4;
plane[start..start + 4].fill(value);
}
}
}
}
_ => return Err(AvcError::Unsupported("unsupported intra macroblock")),
}
}
bits.finish_rbsp()?;
Ok(frame_from_yuv420(sps, &luma, &cb, &cr))
}
pub(super) fn frame_from_yuv420(
sps: &SequenceParameters,
luma: &[u8],
cb: &[u8],
cr: &[u8],
) -> super::VideoFrame {
#[cfg(feature = "acceleration")]
let rgba = {
use accelerate::video::{LumaPolicy, Plane, YuvMatrix, yuv420_to_rgba};
let width = sps.width as usize;
let stride = (sps.width_mbs as usize * 16).max(width);
let y_offset = sps.crop_top as usize * stride + sps.crop_left as usize;
let uv_offset = sps.crop_top as usize / 2 * stride.div_ceil(2) + sps.crop_left as usize / 2;
yuv420_to_rgba(
width, sps.height as usize,
Plane { data: &luma[y_offset..], stride },
Plane { data: &cb[uv_offset..], stride: stride.div_ceil(2) },
Plane { data: &cr[uv_offset..], stride: stride.div_ceil(2) },
YuvMatrix::Bt601, LumaPolicy::Clamped,
).expect("decoded H.264 planes must cover the coded frame")
};
#[cfg(all(not(feature = "acceleration"), target_arch = "aarch64"))]
let rgba = unsafe { rgba_from_yuv420_neon::<true, true>(sps, luma, cb, cr) };
#[cfg(all(not(feature = "acceleration"), not(target_arch = "aarch64")))]
let rgba = rgba_from_yuv420_scalar(sps, luma, cb, cr);
super::VideoFrame {
presentation_size: None,
width: sps.width,
height: sps.height,
rgba: std::sync::Arc::new(rgba),
timestamp: 0.0,
}
}
#[cfg(any(test, all(not(feature = "acceleration"), not(target_arch = "aarch64"))))]
fn rgba_from_yuv420_scalar(sps: &SequenceParameters, luma: &[u8], cb: &[u8], cr: &[u8]) -> Vec<u8> {
let width = sps.width as usize;
let luma_stride = (sps.width_mbs as usize * 16).max(width);
let chroma_stride = luma_stride / 2;
let luma = &luma[sps.crop_top as usize * luma_stride + sps.crop_left as usize..];
let uv_offset = sps.crop_top as usize / 2 * chroma_stride + sps.crop_left as usize / 2;
let cb = &cb[uv_offset..];
let cr = &cr[uv_offset..];
let mut rgba = vec![0u8; width * sps.height as usize * 4];
for y in 0..sps.height as usize {
for x in 0..width {
let value = i32::from(luma[y * luma_stride + x]) - 16;
let uv = (y / 2) * chroma_stride + x / 2;
let u = i32::from(cb[uv]) - 128;
let v = i32::from(cr[uv]) - 128;
let c = value.max(0) * 298;
let index = (y * width + x) * 4;
rgba[index] = ((c + 409 * v + 128) >> 8).clamp(0, 255) as u8;
rgba[index + 1] = ((c - 100 * u - 208 * v + 128) >> 8).clamp(0, 255) as u8;
rgba[index + 2] = ((c + 516 * u + 128) >> 8).clamp(0, 255) as u8;
rgba[index + 3] = 255;
}
}
rgba
}
#[cfg(target_arch = "aarch64")]
#[cfg(any(test, not(feature = "acceleration")))]
#[target_feature(enable = "neon")]
unsafe fn rgba_from_yuv420_neon<const VECTOR_CHROMA_LOAD: bool, const PAIR_ROWS: bool>(
sps: &SequenceParameters,
luma: &[u8],
cb: &[u8],
cr: &[u8],
) -> Vec<u8> {
use std::arch::aarch64::*;
#[inline]
#[target_feature(enable = "neon")]
unsafe fn chroma_terms(u: int16x4_t, v: int16x4_t) -> (int32x4_t, int32x4_t, int32x4_t) {
let u = vsubq_s32(vmovl_s16(u), vdupq_n_s32(128));
let v = vsubq_s32(vmovl_s16(v), vdupq_n_s32(128));
(
vmlaq_n_s32(vdupq_n_s32(128), v, 409),
vsubq_s32(
vsubq_s32(vdupq_n_s32(128), vmulq_n_s32(u, 100)),
vmulq_n_s32(v, 208),
),
vmlaq_n_s32(vdupq_n_s32(128), u, 516),
)
}
#[inline]
#[target_feature(enable = "neon")]
unsafe fn convert_four(
y: int16x4_t,
terms: (int32x4_t, int32x4_t, int32x4_t),
) -> (uint16x4_t, uint16x4_t, uint16x4_t) {
let y = vmovl_s16(y);
let c = vmulq_n_s32(
vmaxq_s32(vsubq_s32(y, vdupq_n_s32(16)), vdupq_n_s32(0)),
298,
);
let red = vshrq_n_s32::<8>(vaddq_s32(c, terms.0));
let green = vshrq_n_s32::<8>(vaddq_s32(c, terms.1));
let blue = vshrq_n_s32::<8>(vaddq_s32(c, terms.2));
(vqmovun_s32(red), vqmovun_s32(green), vqmovun_s32(blue))
}
unsafe {
let width = sps.width as usize;
let height = sps.height as usize;
let stride = (sps.width_mbs as usize * 16).max(width);
let chroma_width = stride / 2;
let luma = &luma[sps.crop_top as usize * stride + sps.crop_left as usize..];
let uv_offset = sps.crop_top as usize / 2 * chroma_width + sps.crop_left as usize / 2;
let cb = &cb[uv_offset..];
let cr = &cr[uv_offset..];
let mut rgba = vec![0u8; width * height * 4];
for y in (0..height).step_by(if PAIR_ROWS { 2 } else { 1 }) {
let rows = if PAIR_ROWS { (height - y).min(2) } else { 1 };
let uv_row = y / 2 * chroma_width;
let mut x = 0;
while x + 8 <= width {
let chroma = uv_row + x / 2;
let (u8, v8) = if VECTOR_CHROMA_LOAD && x / 2 + 8 <= chroma_width {
let u = vld1_u8(cb.as_ptr().add(chroma));
let v = vld1_u8(cr.as_ptr().add(chroma));
(vzip1_u8(u, u), vzip1_u8(v, v))
} else {
let mut u = [0u8; 8];
let mut v = [0u8; 8];
for pair in 0..4 {
u[pair * 2..pair * 2 + 2].fill(cb[chroma + pair]);
v[pair * 2..pair * 2 + 2].fill(cr[chroma + pair]);
}
(vld1_u8(u.as_ptr()), vld1_u8(v.as_ptr()))
};
let u8 = vreinterpretq_s16_u16(vmovl_u8(u8));
let v8 = vreinterpretq_s16_u16(vmovl_u8(v8));
let low = chroma_terms(vget_low_s16(u8), vget_low_s16(v8));
let high = chroma_terms(vget_high_s16(u8), vget_high_s16(v8));
for row in y..y + rows {
let y8 = vreinterpretq_s16_u16(vmovl_u8(vld1_u8(
luma.as_ptr().add(row * stride + x),
)));
let (r0, g0, b0) = convert_four(vget_low_s16(y8), low);
let (r1, g1, b1) = convert_four(vget_high_s16(y8), high);
let red = vqmovn_u16(vcombine_u16(r0, r1));
let green = vqmovn_u16(vcombine_u16(g0, g1));
let blue = vqmovn_u16(vcombine_u16(b0, b1));
let channels = uint8x8x4_t(red, green, blue, vdup_n_u8(255));
vst4_u8(rgba.as_mut_ptr().add((row * width + x) * 4), channels);
}
x += 8;
}
while x < width {
let uv = uv_row + x / 2;
let u = i32::from(cb[uv]) - 128;
let v = i32::from(cr[uv]) - 128;
for row in y..y + rows {
let value = i32::from(luma[row * stride + x]) - 16;
let c = value.max(0) * 298;
let offset = (row * width + x) * 4;
rgba[offset] = ((c + 409 * v + 128) >> 8).clamp(0, 255) as u8;
rgba[offset + 1] = ((c - 100 * u - 208 * v + 128) >> 8).clamp(0, 255) as u8;
rgba[offset + 2] = ((c + 516 * u + 128) >> 8).clamp(0, 255) as u8;
rgba[offset + 3] = 255;
}
x += 1;
}
}
rgba
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rbsp_removes_only_emulation_prevention_bytes() {
assert_eq!(
rbsp_from_nal(&[0x41, 0, 0, 3, 0, 0, 3, 3, 0, 0, 3, 1], 1).unwrap(),
[0, 0, 0, 0, 3, 0, 0, 1]
);
}
#[cfg(target_arch = "aarch64")]
#[test]
fn neon_yuv_conversion_matches_scalar_for_full_and_tail_blocks() {
for width in [8, 16, 18, 24] {
let height = 6;
let sps = SequenceParameters {
id: 0,
profile_idc: 100,
level_idc: 31,
width,
height,
crop_left: 0,
crop_top: 0,
chroma_format_idc: 1,
bit_depth_luma: 8,
bit_depth_chroma: 8,
scaling_matrices_present: false,
width_mbs: width / 16,
frame_height_mbs: 1,
frame_mbs_only: true,
direct_8x8_inference: true,
frame_num_bits: 4,
max_num_ref_frames: 1,
pic_order_cnt_type: 0,
pic_order_cnt_lsb_bits: Some(4),
};
let pixels = width as usize * height as usize;
let luma: Vec<_> = (0..pixels).map(|i| ((i * 53 + 7) & 255) as u8).collect();
let cb: Vec<_> = (0..pixels / 4)
.map(|i| ((i * 37 + 3) & 255) as u8)
.collect();
let cr: Vec<_> = (0..pixels / 4)
.map(|i| ((i * 71 + 29) & 255) as u8)
.collect();
let expected = rgba_from_yuv420_scalar(&sps, &luma, &cb, &cr);
assert_eq!(frame_from_yuv420(&sps, &luma, &cb, &cr).rgba.as_slice(), expected.as_slice(), "production width {width}");
let actual = unsafe { rgba_from_yuv420_neon::<true, true>(&sps, &luma, &cb, &cr) };
assert_eq!(actual, expected, "width {width}");
let single = unsafe { rgba_from_yuv420_neon::<true, false>(&sps, &luma, &cb, &cr) };
assert_eq!(single, expected, "single row width {width}");
}
}
#[test]
fn production_yuv420_matches_clamped_scalar_for_odd_rows_and_tails() {
let mut sps = parse_sps(&high_720p_sps()).unwrap();
for width in [2_u32, 8, 16, 18, 24, 34] {
for height in [1_u32, 3, 6] {
sps.width = width;
sps.height = height;
sps.width_mbs = width.div_ceil(16);
let pixels = width as usize * height as usize;
let stride = sps.width_mbs as usize * 16;
let chroma = stride / 2 * (height as usize).div_ceil(2);
let y: Vec<_> = (0..stride * height as usize).map(|i| (i * 53 + 7) as u8).collect();
let u: Vec<_> = (0..chroma).map(|i| (i * 37 + 3) as u8).collect();
let v: Vec<_> = (0..chroma).map(|i| (i * 71 + 29) as u8).collect();
let expected = rgba_from_yuv420_scalar(&sps, &y, &u, &v);
assert_eq!(frame_from_yuv420(&sps, &y, &u, &v).rgba.as_slice(), expected.as_slice(), "{width}x{height}");
assert_eq!(expected.len(), pixels * 4);
}
}
}
#[test]
fn display_crop_preserves_coded_stride_and_reference_planes() {
let mut sps = parse_sps(&high_720p_sps()).unwrap();
sps.width_mbs = 2;
sps.frame_height_mbs = 1;
sps.width = 24;
sps.height = 10;
sps.crop_left = 2;
sps.crop_top = 2;
let y: Vec<_> = (0..32 * 16).map(|i| (i * 7 + 16) as u8).collect();
let u: Vec<_> = (0..16 * 8).map(|i| (i * 13 + 32) as u8).collect();
let v: Vec<_> = (0..16 * 8).map(|i| (i * 19 + 48) as u8).collect();
let original = (y.clone(), u.clone(), v.clone());
let mut compact = sps.clone();
compact.crop_left = 0;
compact.crop_top = 0;
compact.width_mbs = 1;
let yy: Vec<_> = (2..12).flat_map(|row| y[row * 32 + 2..row * 32 + 26].iter().copied()).collect();
let uu: Vec<_> = (1..6).flat_map(|row| u[row * 16 + 1..row * 16 + 13].iter().copied()).collect();
let vv: Vec<_> = (1..6).flat_map(|row| v[row * 16 + 1..row * 16 + 13].iter().copied()).collect();
let expected = rgba_from_yuv420_scalar(&compact, &yy, &uu, &vv);
assert_eq!(rgba_from_yuv420_scalar(&sps, &y, &u, &v), expected);
let frame = frame_from_yuv420(&sps, &y, &u, &v);
assert_eq!((frame.width, frame.height), (24, 10));
assert_eq!(*frame.rgba, expected);
#[cfg(target_arch = "aarch64")]
assert_eq!(unsafe { rgba_from_yuv420_neon::<true, true>(&sps, &y, &u, &v) }, expected);
assert_eq!((y, u, v), original);
}
#[cfg(target_arch = "aarch64")]
#[test]
#[cfg(feature = "acceleration")]
#[ignore = "release-mode shared/local YUV A/B benchmark"]
fn benchmark_shared_yuv_h264() {
assert!(!cfg!(debug_assertions), "run with --release");
let mut sps = SequenceParameters {
crop_left: 0, crop_top: 0,
id: 0, profile_idc: 100, level_idc: 31, width: 1920, height: 1080,
chroma_format_idc: 1, bit_depth_luma: 8, bit_depth_chroma: 8,
scaling_matrices_present: false, width_mbs: 120, frame_height_mbs: 68,
frame_mbs_only: true, direct_8x8_inference: true, frame_num_bits: 4,
max_num_ref_frames: 1, pic_order_cnt_type: 0, pic_order_cnt_lsb_bits: Some(4),
};
for (width, height) in [(1920, 1080), (1918, 1078), (1280, 720)] {
sps.width = width; sps.height = height;
sps.width_mbs = width.div_ceil(16);
let pixels = sps.width_mbs as usize * 16 * height as usize;
let y: Vec<_> = (0..pixels).map(|i| (i * 53 + 7) as u8).collect();
let u: Vec<_> = (0..pixels / 4).map(|i| (i * 37 + 3) as u8).collect();
let v: Vec<_> = (0..pixels / 4).map(|i| (i * 71 + 29) as u8).collect();
let shared = || frame_from_yuv420(&sps, &y, &u, &v).rgba;
let local = || unsafe { rgba_from_yuv420_neon::<true, true>(&sps, &y, &u, &v) };
assert_eq!(shared().as_slice(), local().as_slice());
assert_eq!(shared().as_slice(), rgba_from_yuv420_scalar(&sps, &y, &u, &v));
for _ in 0..4 { std::hint::black_box(shared()); std::hint::black_box(local()); }
let measure_shared = || {
let start = std::time::Instant::now();
for _ in 0..30 { std::hint::black_box(shared()); }
start.elapsed().as_nanos()
};
let measure_local = || {
let start = std::time::Instant::now();
for _ in 0..30 { std::hint::black_box(local()); }
start.elapsed().as_nanos()
};
let mut shared_times = Vec::new(); let mut local_times = Vec::new();
for trial in 0..8 {
let (s, l) = if trial % 2 == 0 { (measure_shared(), measure_local()) } else {
let l = measure_local(); (measure_shared(), l)
};
shared_times.push(s); local_times.push(l);
eprintln!("H264 {width}x{height} trial {trial}: shared={s}ns local={l}ns ratio={:.4}", s as f64 / l as f64);
}
shared_times.sort_unstable(); local_times.sort_unstable();
eprintln!("H264 {width}x{height} median ratio={:.4}", shared_times[4] as f64 / local_times[4] as f64);
}
}
#[cfg(target_arch = "aarch64")]
#[test]
#[ignore = "run explicitly when measuring the native 1280x720 conversion path"]
fn benchmark_neon_yuv_conversion() {
let sps = SequenceParameters {
crop_left: 0, crop_top: 0,
id: 0,
profile_idc: 100,
level_idc: 31,
width: 1280,
height: 720,
chroma_format_idc: 1,
bit_depth_luma: 8,
bit_depth_chroma: 8,
scaling_matrices_present: false,
width_mbs: 80,
frame_height_mbs: 45,
frame_mbs_only: true,
direct_8x8_inference: true,
frame_num_bits: 4,
max_num_ref_frames: 1,
pic_order_cnt_type: 0,
pic_order_cnt_lsb_bits: Some(4),
};
let pixels = sps.width as usize * sps.height as usize;
let luma: Vec<_> = (0..pixels).map(|i| ((i * 53 + 7) & 255) as u8).collect();
let cb: Vec<_> = (0..pixels / 4)
.map(|i| ((i * 37 + 3) & 255) as u8)
.collect();
let cr: Vec<_> = (0..pixels / 4)
.map(|i| ((i * 71 + 29) & 255) as u8)
.collect();
let runs = 40;
for pair_rows in [false, true, true, false] {
let start = std::time::Instant::now();
for _ in 0..runs {
let rgba = if pair_rows {
unsafe { rgba_from_yuv420_neon::<true, true>(&sps, &luma, &cb, &cr) }
} else {
unsafe { rgba_from_yuv420_neon::<true, false>(&sps, &luma, &cb, &cr) }
};
std::hint::black_box(rgba);
}
eprintln!(
"1280x720 x{runs}: paired rows={pair_rows}: {:?}",
start.elapsed()
);
}
}
#[test]
fn type_zero_picture_order_count_wraps_in_both_directions() {
assert_eq!(
type0_pic_order_count(2, 4, Some((0, 14))).unwrap(),
(16, 18)
);
assert_eq!(
type0_pic_order_count(14, 4, Some((16, 2))).unwrap(),
(0, 14)
);
assert_eq!(type0_pic_order_count(8, 4, Some((0, 0))).unwrap(), (0, 8));
assert_eq!(type0_pic_order_count(0, 4, Some((0, 8))).unwrap(), (16, 16));
assert!(type0_pic_order_count(16, 4, None).is_err());
}
struct BitWriter(Vec<bool>);
impl BitWriter {
fn bits(&mut self, value: u32, count: usize) {
for shift in (0..count).rev() {
self.0.push(value & (1 << shift) != 0);
}
}
fn ue(&mut self, value: u32) {
let code = value + 1;
let width = (32 - code.leading_zeros()) as usize;
self.bits(0, width - 1);
self.bits(code, width);
}
fn se(&mut self, value: i32) {
let code = if value <= 0 {
(-value as u32) * 2
} else {
value as u32 * 2 - 1
};
self.ue(code);
}
fn align_zero(&mut self) {
while !self.0.len().is_multiple_of(8) {
self.bits(0, 1);
}
}
fn bytes(&mut self, data: &[u8]) {
assert!(self.0.len().is_multiple_of(8));
for byte in data {
self.bits(u32::from(*byte), 8);
}
}
fn finish(mut self) -> Vec<u8> {
self.bits(1, 1);
while !self.0.len().is_multiple_of(8) {
self.bits(0, 1);
}
self.0
.chunks(8)
.map(|chunk| {
chunk
.iter()
.fold(0, |byte, bit| (byte << 1) | u8::from(*bit))
})
.collect()
}
}
fn high_720p_sps() -> Vec<u8> {
let mut bits = BitWriter(Vec::new());
bits.bits(100, 8); bits.bits(0, 8); bits.bits(31, 8); bits.ue(0); bits.ue(1); bits.ue(0); bits.ue(0); bits.bits(0, 2); bits.ue(0); bits.ue(0); bits.ue(0); bits.ue(1); bits.bits(0, 1); bits.ue(79); bits.ue(44); bits.bits(1, 1); bits.bits(1, 1); bits.bits(0, 1); bits.bits(0, 1); let mut nal = vec![0x67];
nal.extend(bits.finish());
nal
}
#[test]
fn parses_2005_high_profile_config() {
let sps = high_720p_sps();
let pps = [0x68, 0xce, 0x06, 0xe2];
let mut avcc = vec![1, 100, 0, 31, 0xff, 0xe1];
avcc.extend((sps.len() as u16).to_be_bytes());
avcc.extend(&sps);
avcc.push(1);
avcc.extend((pps.len() as u16).to_be_bytes());
avcc.extend(pps);
let config = AvcConfig::parse(&avcc).unwrap();
assert_eq!(config.nal_length_size, 4);
assert_eq!(config.sequence_parameters[0].width, 1280);
assert_eq!(config.sequence_parameters[0].height, 720);
assert_eq!(config.sequence_parameters[0].profile_idc, 100);
assert_eq!(config.picture_parameter_sets, vec![pps.to_vec()]);
avcc[3] = 30;
assert_eq!(
AvcConfig::parse(&avcc),
Err(AvcError::InvalidData("avcC and SPS disagree"))
);
}
#[test]
fn parses_original_2003_picture_parameter_set() {
let mut bits = BitWriter(Vec::new());
bits.ue(0); bits.ue(0); bits.bits(0, 2); bits.ue(0); bits.ue(0); bits.ue(0); bits.bits(0, 3); bits.se(0); bits.se(0); bits.se(0); bits.bits(1, 1); bits.bits(0, 2); let mut nal = vec![0x68];
nal.extend(bits.finish());
let pps = parse_pps_2003(&nal).unwrap();
assert_eq!((pps.id, pps.sequence_id, pps.pic_init_qp), (0, 0, 26));
assert!(!pps.cabac);
assert_eq!(pps.slice_groups, 1);
assert!(pps.deblocking_filter_control_present);
nal.push(0);
assert_eq!(
parse_pps_2003(&nal),
Err(AvcError::InvalidData("unexpected RBSP data"))
);
}
#[test]
fn parses_2003_disperse_slice_group_map_without_extra_fields() {
let mut bits = BitWriter(Vec::new());
bits.ue(0);
bits.ue(0);
bits.bits(0, 2);
bits.ue(1); bits.ue(1); bits.ue(0);
bits.ue(0);
bits.bits(0, 3);
bits.se(0);
bits.se(0);
bits.se(0);
bits.bits(1, 1);
bits.bits(0, 2);
let mut nal = vec![0x68];
nal.extend(bits.finish());
let pps = parse_pps_2003(&nal).unwrap();
assert_eq!((pps.slice_groups, pps.slice_group_map_type), (2, Some(1)));
}
#[test]
fn decodes_original_2003_pcm_and_intra_dc_idr_into_pixels() {
let mut sps_bits = BitWriter(Vec::new());
sps_bits.bits(66, 8); sps_bits.bits(0, 8); sps_bits.bits(10, 8); sps_bits.ue(0); sps_bits.ue(0); sps_bits.ue(0); sps_bits.ue(0); sps_bits.ue(1); sps_bits.bits(0, 1); sps_bits.ue(1); sps_bits.ue(0); sps_bits.bits(1, 1); sps_bits.bits(1, 1); sps_bits.bits(0, 2); let mut sps_nal = vec![0x67];
sps_nal.extend(sps_bits.finish());
let sps = parse_sps(&sps_nal).unwrap();
let mut pps_bits = BitWriter(Vec::new());
pps_bits.ue(0); pps_bits.ue(0); pps_bits.bits(0, 2); pps_bits.ue(0); pps_bits.ue(0); pps_bits.ue(0); pps_bits.bits(0, 3); pps_bits.se(0);
pps_bits.se(0);
pps_bits.se(0);
pps_bits.bits(1, 1); pps_bits.bits(0, 2);
let mut pps_nal = vec![0x68];
pps_nal.extend(pps_bits.finish());
let pps = parse_pps_2003(&pps_nal).unwrap();
let mut slice = BitWriter(Vec::new());
slice.ue(0); slice.ue(2); slice.ue(0); slice.bits(0, 4); slice.ue(0); slice.bits(0, 4); slice.bits(0, 2); slice.se(0); slice.ue(1); slice.ue(25); slice.align_zero();
let mut pcm = vec![235; 256];
pcm.extend([128; 128]);
slice.bytes(&pcm);
slice.ue(3); slice.ue(0); slice.se(0); slice.bits(1, 1); let mut slice_nal = vec![0x65];
slice_nal.extend(slice.finish());
let frame = decode_intra_2003(&slice_nal, &sps, &pps).unwrap();
assert_eq!((frame.width, frame.height), (32, 16));
assert_eq!(&frame.rgba[..4], &[255, 255, 255, 255]);
assert_eq!(&frame.rgba[frame.rgba.len() - 4..], &[255, 255, 255, 255]);
}
#[test]
fn parses_maroc_mp4_high_profile_configuration() {
let avcc = [
0x01, 0x64, 0x00, 0x20, 0xff, 0xe1, 0x00, 0x1d, 0x67, 0x64, 0x00, 0x20, 0xac, 0xd9,
0x40, 0x50, 0x05, 0xbb, 0xff, 0x04, 0x40, 0x04, 0x41, 0x10, 0x00, 0x00, 0x03, 0x00,
0x10, 0x00, 0x00, 0x06, 0x48, 0xf1, 0x83, 0x19, 0x60, 0x01, 0x00, 0x06, 0x68, 0xeb,
0xe3, 0xcb, 0x22, 0xc0, 0xfd, 0xf8, 0xf8, 0x00,
];
let config = AvcConfig::parse(&avcc).unwrap();
assert_eq!(config.nal_length_size, 4);
assert_eq!(config.sequence_parameters[0].profile_idc, 100);
assert_eq!(config.sequence_parameters[0].level_idc, 32);
assert!(config.sequence_parameters[0].direct_8x8_inference);
assert_eq!(
(
config.sequence_parameters[0].width,
config.sequence_parameters[0].height
),
(1280, 720)
);
}
#[test]
fn length_prefixed_nals_arrive_across_arbitrary_chunks() {
let bytes = [0, 0, 0, 2, 0x65, 0x99, 0, 0, 0, 3, 0x41, 0x12, 0x34];
for split in 1..bytes.len() {
let mut stream = NalStream::new(4).unwrap();
let mut units = stream.push(&bytes[..split]).unwrap();
units.extend(stream.push(&bytes[split..]).unwrap());
assert_eq!(units, vec![vec![0x65, 0x99], vec![0x41, 0x12, 0x34]]);
stream.finish().unwrap();
}
}
#[test]
fn rejects_truncated_and_unbounded_nals() {
let mut stream = NalStream::new(4).unwrap();
assert!(stream.push(&[0, 0, 0, 2, 0x65]).unwrap().is_empty());
assert_eq!(stream.finish(), Err(AvcError::Incomplete));
assert_eq!(stream.push(&[0x99]).unwrap(), vec![vec![0x65, 0x99]]);
stream.finish().unwrap();
assert_eq!(
NalStream::new(3).err(),
Some(AvcError::InvalidData("invalid NAL length size"))
);
assert_eq!(
NalStream::new(4).unwrap().push(&[0xff, 0xff, 0xff, 0xff]),
Err(AvcError::TooLarge)
);
assert_eq!(
parse_sps(&[0x67, 118]),
Err(AvcError::UnsupportedProfile(118))
);
}
}