forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust381.5 MiBcdccbc2Tahinli12 hours ago4263 commits

superframe.rs137 lines, 4.6 KiBHistoryRawDownload
1//! The superframe index (spec Annex B).
2//!
3//! A VP9 "chunk" — one sample in an MP4 or Matroska file, one IVF frame — may
4//! hold several coded frames, at most one of which is shown. That is how a
5//! hidden ALTREF travels: it is packed ahead of the frame that references it,
6//! and the frame that eventually displays it is a bodiless
7//! `show_existing_frame` header. The packing is described by an index appended
8//! *after* the frames, whose first and last bytes are the same marker byte, so
9//! it can be found by reading the chunk backwards.
10
11use ec_core::{Error, Result};
12
13/// Split a chunk into its coded frames.
14///
15/// A chunk without a superframe index is one frame, returned as a single slice
16/// borrowing the whole input — so callers never need to branch on "is this a
17/// superframe". Sizes that do not add up to the chunk length are
18/// [`Error::Corrupt`]: a decoder that trusted them would read another frame's
19/// bytes.
20pub fn split(chunk: &[u8]) -> Result<Vec<&[u8]>> {
21 let Some(&marker) = chunk.last() else {
22 return Err(Error::corrupt("VP9 superframe: empty chunk"));
23 };
24 if marker & 0xe0 != 0xc0 {
25 return Ok(vec![chunk]);
26 }
27 let bytes_per_size = ((marker >> 3) & 0x3) as usize + 1;
28 let frames = (marker & 0x7) as usize + 1;
29 let index_size = 2 + bytes_per_size * frames;
30 if chunk.len() < index_size {
31 return Ok(vec![chunk]);
32 }
33 let index = &chunk[chunk.len() - index_size..];
34 // The marker is repeated at both ends of the index; a byte that merely looks
35 // like a marker by accident almost never is.
36 if index[0] != marker {
37 return Ok(vec![chunk]);
38 }
39
40 let mut out = Vec::with_capacity(frames);
41 let mut offset = 0usize;
42 for i in 0..frames {
43 let mut size = 0usize;
44 for b in 0..bytes_per_size {
45 size |= (index[1 + i * bytes_per_size + b] as usize) << (8 * b);
46 }
47 let end = offset
48 .checked_add(size)
49 .filter(|&end| end <= chunk.len() - index_size)
50 .ok_or_else(|| {
51 Error::corrupt(format!(
52 "VP9 superframe: frame {i} of {frames} runs past the chunk ({size} bytes at {offset})"
53 ))
54 })?;
55 if end > offset {
56 out.push(&chunk[offset..end]);
57 }
58 offset = end;
59 }
60 if out.is_empty() {
61 return Err(Error::corrupt("VP9 superframe: index names no frame data"));
62 }
63 Ok(out)
64}
65
66/// True if `chunk` carries a superframe index, i.e. holds more than one frame.
67pub fn is_superframe(chunk: &[u8]) -> bool {
68 matches!(split(chunk), Ok(frames) if frames.len() > 1)
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 /// Build a chunk of `sizes` dummy frames plus a well-formed index.
76 fn pack(sizes: &[usize], bytes_per_size: usize) -> Vec<u8> {
77 let mut out = Vec::new();
78 for (i, &s) in sizes.iter().enumerate() {
79 out.extend(std::iter::repeat_n(i as u8, s));
80 }
81 let marker = 0xc0 | ((bytes_per_size as u8 - 1) << 3) | (sizes.len() as u8 - 1);
82 out.push(marker);
83 for &s in sizes {
84 for b in 0..bytes_per_size {
85 out.push((s >> (8 * b)) as u8);
86 }
87 }
88 out.push(marker);
89 out
90 }
91
92 #[test]
93 fn plain_chunk_is_one_frame() {
94 let frames = split(&[0x82, 0x49, 0x83, 0x42]).unwrap();
95 assert_eq!(frames.len(), 1);
96 assert_eq!(frames[0].len(), 4);
97 }
98
99 #[test]
100 fn splits_at_the_coded_sizes() {
101 let chunk = pack(&[3, 5, 300], 2);
102 let frames = split(&chunk).unwrap();
103 assert_eq!(
104 frames.iter().map(|f| f.len()).collect::<Vec<_>>(),
105 [3, 5, 300]
106 );
107 assert_eq!(frames[2][0], 2);
108 }
109
110 #[test]
111 fn zero_size_entry_is_skipped() {
112 let chunk = pack(&[3, 0, 5], 1);
113 let frames = split(&chunk).unwrap();
114 assert_eq!(frames.iter().map(|f| f.len()).collect::<Vec<_>>(), [3, 5]);
115 }
116
117 #[test]
118 fn oversized_entry_is_corrupt() {
119 let mut chunk = pack(&[3, 5], 1);
120 let last_size = chunk.len() - 2;
121 chunk[last_size] = 0xff; // second frame claims 255 bytes
122 assert!(matches!(split(&chunk), Err(Error::Corrupt { .. })));
123 }
124
125 #[test]
126 fn marker_without_matching_head_is_not_an_index() {
127 let mut chunk = pack(&[3, 5], 1);
128 let head = chunk.len() - (2 + 2); // 2 size bytes + 2 marker copies
129 chunk[head] = 0x00; // clobber the leading marker copy
130 assert_eq!(split(&chunk).unwrap().len(), 1);
131 }
132
133 #[test]
134 fn empty_chunk_is_corrupt() {
135 assert!(split(&[]).is_err());
136 }
137}