forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

decode_frame.rs128 lines, 3.6 KiBHistoryRawDownload
1#![no_main]
2
3use ec_vp9::decode::Decoder;
4use libfuzzer_sys::fuzz_target;
5
6/// Harness caps (see lanes/fuzz1hvideo.report.md): a key frame with luma above
7/// 128x128 is not fed. Inter frames inherit the size of the key frame before them,
8/// so a part that starts with an oversized key frame ends the input.
9const MAX_DIM: u32 = 128;
10
11fuzz_target!(|data: &[u8]| {
12 if oversized_frame(data) {
13 return;
14 }
15 // The whole input as one frame or superframe, on a fresh decoder.
16 let _ = Decoder::new().decode(data);
17
18 // The same bytes cut at 2 or 3 points and decoded in order, so that the
19 // inter-frame state carried between calls is exercised too.
20 if let Some(parts) = split(data) {
21 let mut dec = Decoder::new();
22 for part in parts {
23 if oversized_frame(part) {
24 break;
25 }
26 let _ = dec.decode(part);
27 }
28 }
29});
30
31fn oversized_frame(frame: &[u8]) -> bool {
32 matches!(frame_dims(frame), Some((w, h)) if w > MAX_DIM || h > MAX_DIM)
33}
34
35/// Frame size of a key frame or an intra-only frame from its uncompressed header (VP9 spec 6.2),
36/// or `None` for inter frames and headers too short to read. Both carry their own frame size.
37fn frame_dims(frame: &[u8]) -> Option<(u32, u32)> {
38 let mut r = Bits { data: frame, pos: 0 };
39 if r.bits(2)? != 2 {
40 return None;
41 }
42 let profile = (r.bits(1)? << 1) | r.bits(1)?;
43 if profile == 3 {
44 r.bits(1)?;
45 }
46 if r.bits(1)? == 1 {
47 return None; // show_existing_frame
48 }
49 let key = r.bits(1)? == 0;
50 let show_frame = r.bits(1)?;
51 let error_resilient = r.bits(1)?;
52 let intra_only = if key {
53 false
54 } else {
55 let intra_only = if show_frame == 0 { r.bits(1)? == 1 } else { false };
56 if error_resilient == 0 {
57 r.bits(2)?; // reset_frame_context
58 }
59 intra_only
60 };
61 if !key && !intra_only {
62 return None;
63 }
64 if r.bits(24)? != 0x49_83_42 {
65 return None;
66 }
67 if key || profile > 0 {
68 color_config(&mut r, profile)?;
69 }
70 if !key {
71 r.bits(8)?; // refresh_frame_flags
72 }
73 let width = r.bits(16)? + 1;
74 let height = r.bits(16)? + 1;
75 Some((width, height))
76}
77
78fn color_config(r: &mut Bits, profile: u32) -> Option<()> {
79 if profile >= 2 {
80 r.bits(1)?; // ten_or_twelve_bit
81 }
82 let color_space = r.bits(3)?;
83 let odd_profile = profile == 1 || profile == 3;
84 if color_space != 7 {
85 r.bits(1)?; // color_range
86 if odd_profile {
87 r.bits(3)?; // subsampling_x, subsampling_y, reserved_zero
88 }
89 } else if odd_profile {
90 r.bits(1)?; // reserved_zero
91 }
92 Some(())
93}
94
95struct Bits<'a> {
96 data: &'a [u8],
97 pos: usize,
98}
99
100impl Bits<'_> {
101 fn bits(&mut self, n: u32) -> Option<u32> {
102 let mut value = 0;
103 for _ in 0..n {
104 let byte = *self.data.get(self.pos >> 3)?;
105 value = (value << 1) | u32::from((byte >> (7 - (self.pos & 7))) & 1);
106 self.pos += 1;
107 }
108 Some(value)
109 }
110}
111
112/// Cut `data` into 2 or 3 non-empty parts. The first byte picks the number of
113/// parts and the cut points; the rest is the material that gets cut.
114fn split(data: &[u8]) -> Option<Vec<&[u8]>> {
115 let (&ctl, body) = data.split_first()?;
116 let n = body.len();
117 if n < 2 {
118 return None;
119 }
120 if ctl & 1 == 1 && n >= 3 {
121 let a = 1 + ((ctl as usize) >> 1) % (n - 2);
122 let b = a + 1 + (body[0] as usize) % (n - a - 1);
123 Some(vec![&body[..a], &body[a..b], &body[b..]])
124 } else {
125 let a = 1 + ((ctl as usize) >> 1) % (n - 1);
126 Some(vec![&body[..a], &body[a..]])
127 }
128}