use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FrameHeaderError {
InputTooShort,
InvalidStartCode {
got: [u8; 3],
},
}
impl fmt::Display for FrameHeaderError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FrameHeaderError::InputTooShort => {
write!(f, "vp8 frame header: input shorter than required")
}
FrameHeaderError::InvalidStartCode { got } => {
write!(
f,
"vp8 frame header: invalid key-frame start code: got [{:#04x}, {:#04x}, {:#04x}], want [0x9d, 0x01, 0x2a]",
got[0], got[1], got[2]
)
}
}
}
}
impl std::error::Error for FrameHeaderError {}
pub const KEY_FRAME_START_CODE: [u8; 3] = [0x9d, 0x01, 0x2a];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReconstructionFilter {
Bicubic,
Bilinear,
None,
Reserved,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoopFilterPolicy {
Normal,
Simple,
None,
Reserved,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScaleCode {
None,
FiveFourths,
FiveThirds,
Double,
}
impl ScaleCode {
fn from_code(code: u16) -> Self {
match code & 0b11 {
0 => ScaleCode::None,
1 => ScaleCode::FiveFourths,
2 => ScaleCode::FiveThirds,
_ => ScaleCode::Double,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Vp8FrameHeader {
pub key_frame: bool,
pub version: u8,
pub reconstruction_filter: ReconstructionFilter,
pub loop_filter_policy: LoopFilterPolicy,
pub show_frame: bool,
pub first_partition_size: u32,
pub width: Option<u16>,
pub height: Option<u16>,
pub horizontal_scale: Option<ScaleCode>,
pub vertical_scale: Option<ScaleCode>,
pub header_bytes_consumed: usize,
}
impl Vp8FrameHeader {
#[allow(clippy::too_many_arguments)]
pub fn new(
key_frame: bool,
version: u8,
reconstruction_filter: ReconstructionFilter,
loop_filter_policy: LoopFilterPolicy,
show_frame: bool,
first_partition_size: u32,
width: Option<u16>,
height: Option<u16>,
horizontal_scale: Option<ScaleCode>,
vertical_scale: Option<ScaleCode>,
header_bytes_consumed: usize,
) -> Self {
Self {
key_frame,
version,
reconstruction_filter,
loop_filter_policy,
show_frame,
first_partition_size,
width,
height,
horizontal_scale,
vertical_scale,
header_bytes_consumed,
}
}
#[must_use]
pub fn with_key_frame(mut self, v: bool) -> Self {
self.key_frame = v;
self
}
#[must_use]
pub fn with_version(mut self, v: u8) -> Self {
self.version = v;
self
}
#[must_use]
pub fn with_reconstruction_filter(mut self, v: ReconstructionFilter) -> Self {
self.reconstruction_filter = v;
self
}
#[must_use]
pub fn with_loop_filter_policy(mut self, v: LoopFilterPolicy) -> Self {
self.loop_filter_policy = v;
self
}
#[must_use]
pub fn with_show_frame(mut self, v: bool) -> Self {
self.show_frame = v;
self
}
#[must_use]
pub fn with_first_partition_size(mut self, v: u32) -> Self {
self.first_partition_size = v;
self
}
#[must_use]
pub fn with_width(mut self, v: Option<u16>) -> Self {
self.width = v;
self
}
#[must_use]
pub fn with_height(mut self, v: Option<u16>) -> Self {
self.height = v;
self
}
#[must_use]
pub fn with_horizontal_scale(mut self, v: Option<ScaleCode>) -> Self {
self.horizontal_scale = v;
self
}
#[must_use]
pub fn with_vertical_scale(mut self, v: Option<ScaleCode>) -> Self {
self.vertical_scale = v;
self
}
#[must_use]
pub fn with_header_bytes_consumed(mut self, v: usize) -> Self {
self.header_bytes_consumed = v;
self
}
}
impl Vp8FrameHeader {
pub fn parse(bytes: &[u8]) -> Result<Self, FrameHeaderError> {
if bytes.len() < 3 {
return Err(FrameHeaderError::InputTooShort);
}
let tmp = (bytes[0] as u32) | ((bytes[1] as u32) << 8) | ((bytes[2] as u32) << 16);
let key_frame = (tmp & 0x1) == 0;
let version = ((tmp >> 1) & 0x7) as u8;
let show_frame = ((tmp >> 4) & 0x1) != 0;
let first_partition_size = (tmp >> 5) & 0x7_FFFF;
let (reconstruction_filter, loop_filter_policy) = decode_version(version);
if !key_frame {
return Ok(Vp8FrameHeader {
key_frame,
version,
reconstruction_filter,
loop_filter_policy,
show_frame,
first_partition_size,
width: None,
height: None,
horizontal_scale: None,
vertical_scale: None,
header_bytes_consumed: 3,
});
}
if bytes.len() < 10 {
return Err(FrameHeaderError::InputTooShort);
}
let start_code = [bytes[3], bytes[4], bytes[5]];
if start_code != KEY_FRAME_START_CODE {
return Err(FrameHeaderError::InvalidStartCode { got: start_code });
}
let h_word = (bytes[6] as u16) | ((bytes[7] as u16) << 8);
let v_word = (bytes[8] as u16) | ((bytes[9] as u16) << 8);
let width = h_word & 0x3FFF;
let horizontal_scale = ScaleCode::from_code(h_word >> 14);
let height = v_word & 0x3FFF;
let vertical_scale = ScaleCode::from_code(v_word >> 14);
Ok(Vp8FrameHeader {
key_frame,
version,
reconstruction_filter,
loop_filter_policy,
show_frame,
first_partition_size,
width: Some(width),
height: Some(height),
horizontal_scale: Some(horizontal_scale),
vertical_scale: Some(vertical_scale),
header_bytes_consumed: 10,
})
}
}
fn decode_version(version: u8) -> (ReconstructionFilter, LoopFilterPolicy) {
match version {
0 => (ReconstructionFilter::Bicubic, LoopFilterPolicy::Normal),
1 => (ReconstructionFilter::Bilinear, LoopFilterPolicy::Simple),
2 => (ReconstructionFilter::Bilinear, LoopFilterPolicy::None),
3 => (ReconstructionFilter::None, LoopFilterPolicy::None),
_ => (ReconstructionFilter::Reserved, LoopFilterPolicy::Reserved),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_short_input_below_three_bytes() {
assert_eq!(
Vp8FrameHeader::parse(&[]).unwrap_err(),
FrameHeaderError::InputTooShort
);
assert_eq!(
Vp8FrameHeader::parse(&[0x00, 0x00]).unwrap_err(),
FrameHeaderError::InputTooShort
);
}
#[test]
fn rejects_key_frame_with_short_input_below_ten_bytes() {
let buf = [0x00, 0x00, 0x00, 0x9d, 0x01];
assert_eq!(
Vp8FrameHeader::parse(&buf).unwrap_err(),
FrameHeaderError::InputTooShort
);
}
#[test]
fn rejects_key_frame_with_wrong_start_code() {
let buf = [0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0x10, 0x00, 0x10, 0x00];
assert_eq!(
Vp8FrameHeader::parse(&buf).unwrap_err(),
FrameHeaderError::InvalidStartCode {
got: [0xde, 0xad, 0xbe]
}
);
}
#[test]
fn interframe_consumes_three_bytes_and_omits_dimensions() {
let first_part_size: u32 = 0x1_2345;
let tmp: u32 = 0x1 | (0x5 << 1) | (0x1 << 4) | (first_part_size << 5);
let bytes = [
(tmp & 0xff) as u8,
((tmp >> 8) & 0xff) as u8,
((tmp >> 16) & 0xff) as u8,
0xFF, 0xFF,
];
let hdr = Vp8FrameHeader::parse(&bytes).unwrap();
assert!(!hdr.key_frame);
assert_eq!(hdr.version, 5);
assert!(hdr.show_frame);
assert_eq!(hdr.first_partition_size, first_part_size);
assert_eq!(hdr.header_bytes_consumed, 3);
assert_eq!(hdr.width, None);
assert_eq!(hdr.height, None);
assert_eq!(hdr.horizontal_scale, None);
assert_eq!(hdr.vertical_scale, None);
assert_eq!(hdr.reconstruction_filter, ReconstructionFilter::Reserved);
assert_eq!(hdr.loop_filter_policy, LoopFilterPolicy::Reserved);
}
#[test]
fn version_table_maps_all_four_defined_codes() {
let cases: &[(u8, ReconstructionFilter, LoopFilterPolicy)] = &[
(0, ReconstructionFilter::Bicubic, LoopFilterPolicy::Normal),
(1, ReconstructionFilter::Bilinear, LoopFilterPolicy::Simple),
(2, ReconstructionFilter::Bilinear, LoopFilterPolicy::None),
(3, ReconstructionFilter::None, LoopFilterPolicy::None),
];
for &(v, want_filter, want_policy) in cases {
let tmp: u32 = 0x1 | ((v as u32) << 1);
let bytes = [
(tmp & 0xff) as u8,
((tmp >> 8) & 0xff) as u8,
((tmp >> 16) & 0xff) as u8,
];
let hdr = Vp8FrameHeader::parse(&bytes).unwrap();
assert_eq!(hdr.version, v);
assert_eq!(hdr.reconstruction_filter, want_filter, "version={v}");
assert_eq!(hdr.loop_filter_policy, want_policy, "version={v}");
}
}
#[test]
fn scale_code_decodes_all_four_values() {
assert_eq!(ScaleCode::from_code(0), ScaleCode::None);
assert_eq!(ScaleCode::from_code(1), ScaleCode::FiveFourths);
assert_eq!(ScaleCode::from_code(2), ScaleCode::FiveThirds);
assert_eq!(ScaleCode::from_code(3), ScaleCode::Double);
}
#[test]
fn key_frame_parses_real_tiny_fixture_bytes() {
let bytes = [0xf0, 0x02, 0x00, 0x9d, 0x01, 0x2a, 0x10, 0x00, 0x10, 0x00];
let hdr = Vp8FrameHeader::parse(&bytes).unwrap();
assert!(hdr.key_frame);
assert_eq!(hdr.version, 0);
assert!(hdr.show_frame);
assert_eq!(hdr.first_partition_size, 23);
assert_eq!(hdr.width, Some(16));
assert_eq!(hdr.height, Some(16));
assert_eq!(hdr.horizontal_scale, Some(ScaleCode::None));
assert_eq!(hdr.vertical_scale, Some(ScaleCode::None));
assert_eq!(hdr.header_bytes_consumed, 10);
assert_eq!(hdr.reconstruction_filter, ReconstructionFilter::Bicubic);
assert_eq!(hdr.loop_filter_policy, LoopFilterPolicy::Normal);
}
#[test]
fn key_frame_max_width_height_round_trip() {
let bytes = [
0x00, 0x00, 0x00, 0x9d, 0x01, 0x2a, 0xFF, 0xFF, 0xFF, 0xBF, ];
let hdr = Vp8FrameHeader::parse(&bytes).unwrap();
assert!(hdr.key_frame);
assert_eq!(hdr.width, Some(0x3FFF));
assert_eq!(hdr.height, Some(0x3FFF));
assert_eq!(hdr.horizontal_scale, Some(ScaleCode::Double));
assert_eq!(hdr.vertical_scale, Some(ScaleCode::FiveThirds));
}
#[test]
fn first_partition_size_uses_full_nineteen_bits() {
let fps: u32 = 0x7_FFFF;
let tmp: u32 = fps << 5;
let bytes = [
(tmp & 0xff) as u8,
((tmp >> 8) & 0xff) as u8,
((tmp >> 16) & 0xff) as u8,
0x9d,
0x01,
0x2a,
0x00,
0x00,
0x00,
0x00,
];
let hdr = Vp8FrameHeader::parse(&bytes).unwrap();
assert_eq!(hdr.first_partition_size, 0x7_FFFF);
assert!(hdr.first_partition_size < (1 << 19));
}
}