use crate::bit_io::{BITS_PER_BYTE, BitReader, BitWriter, impl_h222_descriptor};
use crate::error::Result;
use alloc::vec::Vec;
use broadcast_core::reserved::ReservedOnes;
pub const TAG: u8 = 0x33;
pub const RESERVED_BEFORE_TEMPORAL_ID_BITS: u32 = 5;
pub const RESERVED_BEFORE_ES_REFERENCE_BITS: u32 = 2;
const AVC_COMPATIBLE_FLAGS_BITS: usize = 2;
const RESERVED_BEFORE_TEMPORAL_ID_READ_BITS: usize = RESERVED_BEFORE_TEMPORAL_ID_BITS as usize;
const RESERVED_BEFORE_ES_REFERENCE_READ_BITS: usize = RESERVED_BEFORE_ES_REFERENCE_BITS as usize;
const APPLICABLE_TEMPORAL_ID_BITS: usize = 3;
const ES_REFERENCE_BITS: usize = 6;
const CONSTRAINT_SET_FLAG_COUNT: usize = 6;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct MvcEsReference {
#[cfg_attr(feature = "serde", serde(skip))]
pub reserved: ReservedOnes<RESERVED_BEFORE_ES_REFERENCE_BITS>,
pub es_reference: u8,
}
impl MvcEsReference {
#[must_use]
pub const fn new(es_reference: u8) -> Self {
Self {
reserved: ReservedOnes::FILL,
es_reference,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct MvcOperationPointEntry {
#[cfg_attr(feature = "serde", serde(skip))]
pub reserved: ReservedOnes<RESERVED_BEFORE_TEMPORAL_ID_BITS>,
pub applicable_temporal_id: u8,
pub num_target_output_views: u8,
pub es_references: Vec<MvcEsReference>,
}
impl MvcOperationPointEntry {
#[must_use]
pub fn new(
applicable_temporal_id: u8,
num_target_output_views: u8,
es_references: Vec<MvcEsReference>,
) -> Self {
Self {
reserved: ReservedOnes::FILL,
applicable_temporal_id,
num_target_output_views,
es_references,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct MvcLevel {
pub level_idc: u8,
pub operation_points: Vec<MvcOperationPointEntry>,
}
impl MvcLevel {
#[must_use]
pub fn new(level_idc: u8, operation_points: Vec<MvcOperationPointEntry>) -> Self {
Self {
level_idc,
operation_points,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
#[non_exhaustive]
pub struct MvcOperationPointDescriptor {
pub profile_idc: u8,
pub constraint_set_flags: [bool; 6],
pub avc_compatible_flags: u8,
pub levels: Vec<MvcLevel>,
pub trailing: Vec<u8>,
}
impl MvcOperationPointDescriptor {
#[must_use]
pub fn new(
profile_idc: u8,
constraint_set_flags: [bool; 6],
avc_compatible_flags: u8,
levels: Vec<MvcLevel>,
) -> Self {
Self {
profile_idc,
constraint_set_flags,
avc_compatible_flags,
levels,
trailing: Vec::new(),
}
}
fn parse_body(body: &[u8]) -> Result<Self> {
let mut r = BitReader::new(body, "MvcOperationPointDescriptor");
let profile_idc = r.u8(BITS_PER_BYTE)?;
let mut constraint_set_flags = [false; CONSTRAINT_SET_FLAG_COUNT];
for f in &mut constraint_set_flags {
*f = r.flag()?;
}
let avc_compatible_flags = r.u8(AVC_COMPATIBLE_FLAGS_BITS)?;
let level_count = r.bits(BITS_PER_BYTE)?;
let mut levels = Vec::new();
for _ in 0..level_count {
let level_idc = r.u8(BITS_PER_BYTE)?;
let ops = r.bits(BITS_PER_BYTE)?;
let mut operation_points = Vec::new();
for _ in 0..ops {
let reserved = ReservedOnes::from_bits(u64::from(
r.u8(RESERVED_BEFORE_TEMPORAL_ID_READ_BITS)?,
));
let applicable_temporal_id = r.u8(APPLICABLE_TEMPORAL_ID_BITS)?;
let num_target_output_views = r.u8(BITS_PER_BYTE)?;
let es_count = r.bits(BITS_PER_BYTE)?;
let mut es_references = Vec::new();
for _ in 0..es_count {
es_references.push(MvcEsReference {
reserved: ReservedOnes::from_bits(u64::from(
r.u8(RESERVED_BEFORE_ES_REFERENCE_READ_BITS)?,
)),
es_reference: r.u8(ES_REFERENCE_BITS)?,
});
}
operation_points.push(MvcOperationPointEntry {
reserved,
applicable_temporal_id,
num_target_output_views,
es_references,
});
}
levels.push(MvcLevel {
level_idc,
operation_points,
});
}
Ok(Self {
profile_idc,
constraint_set_flags,
avc_compatible_flags,
levels,
trailing: r.rest()?.to_vec(),
})
}
fn write_body(&self) -> Result<Vec<u8>> {
let mut w = BitWriter::new();
w.put(u64::from(self.profile_idc), BITS_PER_BYTE, "profile_idc")?;
for f in &self.constraint_set_flags {
w.flag(*f, "constraint_set_flag")?;
}
w.put(
u64::from(self.avc_compatible_flags),
AVC_COMPATIBLE_FLAGS_BITS,
"AVC_compatible_flags",
)?;
w.put(self.levels.len() as u64, BITS_PER_BYTE, "level_count")?;
for l in &self.levels {
w.put(u64::from(l.level_idc), BITS_PER_BYTE, "level_idc")?;
w.put(
l.operation_points.len() as u64,
BITS_PER_BYTE,
"operation_points_count",
)?;
for op in &l.operation_points {
w.put(
op.reserved.get(),
RESERVED_BEFORE_TEMPORAL_ID_READ_BITS,
"reserved",
)?;
w.put(
u64::from(op.applicable_temporal_id),
APPLICABLE_TEMPORAL_ID_BITS,
"applicable_temporal_id",
)?;
w.put(
u64::from(op.num_target_output_views),
BITS_PER_BYTE,
"num_target_output_views",
)?;
w.put(op.es_references.len() as u64, BITS_PER_BYTE, "ES_count")?;
for e in &op.es_references {
w.put(
e.reserved.get(),
RESERVED_BEFORE_ES_REFERENCE_READ_BITS,
"reserved",
)?;
w.put(u64::from(e.es_reference), ES_REFERENCE_BITS, "ES_reference")?;
}
}
}
w.bytes(&self.trailing)?;
Ok(w.into_vec())
}
}
impl_h222_descriptor!(
MvcOperationPointDescriptor,
TAG,
"MvcOperationPointDescriptor",
"MVC_OPERATION_POINT"
);
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
use broadcast_core::{Parse, Serialize};
#[test]
fn spec_vector_round_trip() {
let b = [
TAG,
10,
118,
0b1010_0101,
1,
0x28,
1,
0b1111_1011,
2,
2,
0b11_000011,
0b11_000100,
];
let d = MvcOperationPointDescriptor::parse(&b).unwrap();
assert_eq!(d.profile_idc, 118);
assert_eq!(
d.constraint_set_flags,
[true, false, true, false, false, true]
);
assert_eq!(d.avc_compatible_flags, 1);
assert_eq!(
d.levels[0].operation_points[0].es_references[1].es_reference,
4
);
let mut out = vec![0u8; d.serialized_len()];
d.serialize_into(&mut out).unwrap();
assert_eq!(out, &b[..]);
}
#[test]
fn rejects_truncation_overflow_and_empty() {
assert!(matches!(
MvcOperationPointDescriptor::parse(&[TAG, 3, 118, 0, 1]),
Err(crate::Error::BufferTooShort(..))
));
let mut d = MvcOperationPointDescriptor::new(1, [false; 6], 0, vec![]);
let mut out = [0u8; 16];
assert!(d.serialize_into(&mut out).is_ok()); d.avc_compatible_flags = 4;
assert!(matches!(
d.serialize_into(&mut out),
Err(crate::Error::FieldOverflow(..))
));
}
#[test]
fn reserved_fields_cannot_hold_more_than_their_width() {
let es = MvcEsReference {
reserved: ReservedOnes::from_bits(0xFF),
es_reference: 5,
};
assert_eq!(es.reserved.get(), 0x03);
let op = MvcOperationPointEntry {
reserved: ReservedOnes::from_bits(0xFF),
applicable_temporal_id: 0,
num_target_output_views: 1,
es_references: vec![es],
};
assert_eq!(op.reserved.get(), 0x1F);
}
}