forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

frame.rs111 lines, 3.8 KiBHistoryRawDownload
1//! Frame-level structures and the raw (uncompressed) frame tag
2//! (RFC 6386 §9.1).
3
4use ec_core::{Error, Result};
5
6/// Key frame or interframe — the 1-bit frame type from the frame tag.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum FrameType {
9 /// Key frame (`frame_type` bit 0).
10 Key,
11 /// Interframe (`frame_type` bit 1); predicted from earlier frames.
12 Inter,
13}
14
15/// The 3-byte frame tag every frame starts with (RFC 6386 §9.1 item 1-4):
16/// 1 bit type, 3 bits version, 1 bit show_frame, 19 bits first-partition
17/// size — all read out of one little-endian 24-bit word.
18#[derive(Debug, Clone, Copy)]
19pub struct FrameTag {
20 /// Key or inter frame.
21 pub frame_type: FrameType,
22 /// 3-bit version (reconstruction-filter / loop-filter profile; only
23 /// bits that matter historically — the header's own loop filter fields
24 /// govern decoding).
25 pub version: u8,
26 /// Whether the decoded frame is for display.
27 pub show_frame: bool,
28 /// Size in bytes of the first (header/mode) data partition.
29 pub first_part_size: u32,
30}
31
32/// Bytes occupied by the 3-byte frame tag.
33pub const FRAME_TAG_SZ: usize = 3;
34
35/// Bytes occupied by the key frame's start code + size fields (§9.1).
36pub const KEYFRAME_HEADER_SZ: usize = 7;
37
38impl FrameTag {
39 /// Parse the tag from the first 3 bytes of a frame.
40 pub fn parse(data: &[u8]) -> Result<FrameTag> {
41 if data.len() < FRAME_TAG_SZ {
42 return Err(Error::corrupt("VP8 frame shorter than its 3-byte tag"));
43 }
44 let tag = u32::from(data[0]) | (u32::from(data[1]) << 8) | (u32::from(data[2]) << 16);
45 Ok(FrameTag {
46 frame_type: if tag & 1 == 0 {
47 FrameType::Key
48 } else {
49 FrameType::Inter
50 },
51 version: ((tag >> 1) & 7) as u8,
52 show_frame: (tag >> 4) & 1 == 1,
53 first_part_size: tag >> 5,
54 })
55 }
56}
57
58/// Key-frame dimensions and scaling flags (RFC 6386 §9.1).
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub struct KeyFrameDims {
61 /// Encoded (pre-scaling) width in pixels, 14 bits.
62 pub width: u16,
63 /// Encoded height in pixels, 14 bits.
64 pub height: u16,
65 /// 2-bit horizontal scale (0 = none, 1 = 5/4, 2 = 5/3, 3 = 2).
66 pub h_scale: u8,
67 /// 2-bit vertical scale.
68 pub v_scale: u8,
69}
70
71/// Parse the key-frame start code (`9d 01 2a`) and dimensions that follow
72/// the frame tag. `data` starts at the frame tag.
73pub fn parse_keyframe_dims(data: &[u8]) -> Result<KeyFrameDims> {
74 let dims_off = FRAME_TAG_SZ;
75 if data.len() < dims_off + KEYFRAME_HEADER_SZ {
76 return Err(Error::corrupt("VP8 key frame header truncated"));
77 }
78 let c = &data[dims_off..];
79 if c[0] != 0x9d || c[1] != 0x01 || c[2] != 0x2a {
80 return Err(Error::corrupt(format!(
81 "VP8 key frame start code 9d 01 2a not found (got {:02x} {:02x} {:02x})",
82 c[0], c[1], c[2]
83 )));
84 }
85 let w = u16::from_le_bytes([c[3], c[4]]);
86 let h = u16::from_le_bytes([c[5], c[6]]);
87 // A zero-size frame has no macroblocks; it cannot be reconstructed or
88 // used as a reference (libvpx rejects it the same way).
89 if w & 0x3fff == 0 || h & 0x3fff == 0 {
90 return Err(Error::corrupt(format!(
91 "VP8 key frame has a zero dimension ({}x{})",
92 w & 0x3fff,
93 h & 0x3fff
94 )));
95 }
96 Ok(KeyFrameDims {
97 width: w & 0x3fff,
98 height: h & 0x3fff,
99 h_scale: (w >> 14) as u8,
100 v_scale: (h >> 14) as u8,
101 })
102}
103
104/// Number of 16×16 macroblock columns/rows covering `width × height`
105/// pixels (the encoded frame is padded up to whole macroblocks).
106pub fn mb_geometry(width: u16, height: u16) -> (usize, usize) {
107 (
108 usize::from(width).div_ceil(16),
109 usize::from(height).div_ceil(16),
110 )
111}