Skip to main content

moq_mux/codec/vp8/
mod.rs

1//! VP8.
2//!
3//! Parses the VP8 uncompressed frame header (RFC 6386 §9.1, §19.1) to detect
4//! key frames and read the encoded dimensions, and provides an [`Import`] that
5//! publishes a raw VP8 bitstream (one frame per buffer) to a moq broadcast.
6//!
7//! VP8 carries no out-of-band configuration record, so `vpcc` synthesizes the
8//! informational `vpcC` box the fMP4 exporter needs.
9
10mod import;
11
12pub use import::*;
13
14/// VP8 parsing errors.
15#[derive(Debug, Clone, thiserror::Error)]
16#[non_exhaustive]
17pub enum Error {
18	/// The frame is shorter than its required header.
19	#[error("VP8 frame too short for tag")]
20	FrameTooShort,
21
22	/// The keyframe is shorter than its required header.
23	#[error("VP8 key frame too short for header")]
24	KeyframeHeaderTooShort,
25
26	/// The keyframe header has an invalid start code.
27	#[error("VP8 key frame start code mismatch")]
28	StartCodeMismatch,
29
30	/// The frame contains no bytes.
31	#[error("empty VP8 frame")]
32	EmptyFrame,
33}
34
35/// A Result type alias for VP8 parsing.
36pub type Result<T> = std::result::Result<T, Error>;
37
38/// Fields parsed from a VP8 frame tag (RFC 6386 §9.1) plus, for key frames, the
39/// key-frame header (§19.1).
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub(crate) struct FrameHeader {
42	/// True for a key frame (frame_type bit clear), false for an interframe.
43	pub keyframe: bool,
44	/// Encoded `(width, height)`, present only on key frames.
45	pub dimensions: Option<(u16, u16)>,
46}
47
48impl FrameHeader {
49	/// Parse the leading bytes of a VP8 frame.
50	///
51	/// Reads the 3-byte frame tag, and on a key frame the 7-byte header that
52	/// follows (start code + dimensions). Interframes carry neither a start code
53	/// nor dimensions.
54	pub fn parse(data: &[u8]) -> Result<Self> {
55		if data.len() < 3 {
56			return Err(Error::FrameTooShort);
57		}
58
59		// 24-bit little-endian frame tag. Bit 0 is frame_type: 0 = key frame.
60		let tag = u32::from(data[0]) | (u32::from(data[1]) << 8) | (u32::from(data[2]) << 16);
61		let keyframe = tag & 0x1 == 0;
62
63		if !keyframe {
64			return Ok(Self {
65				keyframe: false,
66				dimensions: None,
67			});
68		}
69
70		if data.len() < 10 {
71			return Err(Error::KeyframeHeaderTooShort);
72		}
73		if !(data[3] == 0x9d && data[4] == 0x01 && data[5] == 0x2a) {
74			return Err(Error::StartCodeMismatch);
75		}
76
77		// 14-bit dimensions; the top 2 bits of each field are the scaling factor.
78		let width = (u16::from(data[6]) | (u16::from(data[7]) << 8)) & 0x3fff;
79		let height = (u16::from(data[8]) | (u16::from(data[9]) << 8)) & 0x3fff;
80
81		Ok(Self {
82			keyframe: true,
83			dimensions: Some((width, height)),
84		})
85	}
86}
87
88/// Build the informational `vpcC` configuration record for VP8.
89///
90/// VP8 is always 8-bit 4:2:0 with no out-of-band parameters, so the box carries
91/// fixed placeholders. Profile 0 / level 0 are the standard "unspecified"
92/// values. See https://www.webmproject.org/vp9/mp4/. The colour fields are
93/// unspecified (2) rather than mp4-atom's BT.709 default.
94pub(crate) fn vpcc() -> mp4_atom::VpcC {
95	mp4_atom::VpcC {
96		profile: 0,
97		level: 0,
98		bit_depth: 8,
99		color_primaries: 2,
100		transfer_characteristics: 2,
101		matrix_coefficients: 2,
102		..Default::default()
103	}
104}
105
106#[cfg(test)]
107mod tests {
108	use super::*;
109
110	#[test]
111	fn parses_keyframe_dimensions() {
112		// Key frame tag (bit 0 = 0) + start code + 320x240.
113		let frame = [0x10, 0x00, 0x00, 0x9d, 0x01, 0x2a, 0x40, 0x01, 0xf0, 0x00];
114		let header = FrameHeader::parse(&frame).expect("parse key frame");
115		assert!(header.keyframe);
116		assert_eq!(header.dimensions, Some((320, 240)));
117	}
118
119	#[test]
120	fn parses_interframe() {
121		// Interframe tag (bit 0 = 1); no start code or dimensions follow.
122		let frame = [0x31, 0x00, 0x00];
123		let header = FrameHeader::parse(&frame).expect("parse interframe");
124		assert!(!header.keyframe);
125		assert_eq!(header.dimensions, None);
126	}
127
128	#[test]
129	fn rejects_bad_start_code() {
130		let frame = [0x10, 0x00, 0x00, 0xde, 0xad, 0xbe, 0x40, 0x01, 0xf0, 0x00];
131		assert!(FrameHeader::parse(&frame).is_err());
132	}
133
134	#[test]
135	fn rejects_short_frame() {
136		assert!(FrameHeader::parse(&[0x10, 0x00]).is_err());
137	}
138}