1use anyhow::{Context, Result, ensure};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct VapInfo {
8 pub width: u32,
9 pub height: u32,
10 pub video_width: u32,
11 pub video_height: u32,
12 pub frames: u32,
13 pub fps: f64,
14 pub rgb_frame: [u32; 4],
15 pub alpha_frame: [u32; 4],
16 pub dynamic_sources: usize,
17}
18
19#[derive(Deserialize)]
20struct Config {
21 info: Info,
22 #[serde(default)]
23 src: Vec<serde_json::Value>,
24}
25#[derive(Deserialize)]
26struct Info {
27 v: u32,
28 w: u32,
29 h: u32,
30 f: u32,
31 fps: f64,
32 #[serde(rename = "videoW")]
33 video_w: u32,
34 #[serde(rename = "videoH")]
35 video_h: u32,
36 #[serde(rename = "rgbFrame")]
37 rgb: [u32; 4],
38 #[serde(rename = "aFrame")]
39 alpha: [u32; 4],
40}
41
42pub(crate) fn inspect(bytes: &[u8]) -> Result<Option<VapInfo>> {
43 if bytes.get(4..8) != Some(b"ftyp") {
44 return Ok(None);
45 }
46 let mut at = 0_usize;
47 let mut boxes = 0;
48 let mut found = None;
49 while at < bytes.len() {
50 boxes += 1;
51 ensure!(boxes <= 10_000, "mp4_box_count_limit");
52 let header = bytes.get(at..at + 8).context("mp4_truncated_box")?;
53 let length = u32::from_be_bytes(header[..4].try_into()?) as u64;
54 let (length, header_len) = match length {
55 0 => ((bytes.len() - at) as u64, 8),
56 1 => (
57 u64::from_be_bytes(
58 bytes
59 .get(at + 8..at + 16)
60 .context("mp4_truncated_large_box")?
61 .try_into()?,
62 ),
63 16,
64 ),
65 _ => (length, 8),
66 };
67 ensure!(
68 length >= header_len as u64 && length <= (bytes.len() - at) as u64,
69 "mp4_invalid_box_size"
70 );
71 let end = at + length as usize;
72 if &header[4..] == b"vapc" {
73 ensure!(found.is_none(), "vap_duplicate_config");
74 ensure!(length <= 1024 * 1024, "vap_config_size_limit");
75 let config: Config = serde_json::from_slice(&bytes[at + header_len..end])
76 .context("vap_invalid_config")?;
77 let i = config.info;
78 ensure!(
79 i.v == 2
80 && i.w > 0
81 && i.h > 0
82 && i.video_w > 0
83 && i.video_h > 0
84 && i.video_w <= 8192
85 && i.video_h <= 8192
86 && u64::from(i.w) * u64::from(i.h) <= 16_777_216
87 && i.f > 0
88 && i.f <= 1_000_000
89 && i.fps.is_finite()
90 && (1.0..=120.0).contains(&i.fps),
91 "vap_unsupported_canvas_or_timing"
92 );
93 for rect in [i.rgb, i.alpha] {
94 ensure!(
95 rect[2] > 0
96 && rect[3] > 0
97 && rect[0].checked_add(rect[2]).is_some_and(|x| x <= i.video_w)
98 && rect[1].checked_add(rect[3]).is_some_and(|y| y <= i.video_h),
99 "vap_region_out_of_bounds"
100 );
101 }
102 found = Some(VapInfo {
103 width: i.w,
104 height: i.h,
105 video_width: i.video_w,
106 video_height: i.video_h,
107 frames: i.f,
108 fps: i.fps,
109 rgb_frame: i.rgb,
110 alpha_frame: i.alpha,
111 dynamic_sources: config.src.len(),
112 });
113 }
114 at = end;
115 }
116 Ok(found)
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122 fn mp4(config: serde_json::Value) -> Vec<u8> {
123 let mut bytes = vec![0, 0, 0, 16];
124 bytes.extend(b"ftypisom0000");
125 let json = serde_json::to_vec(&config).unwrap();
126 bytes.extend(((json.len() + 8) as u32).to_be_bytes());
127 bytes.extend(b"vapc");
128 bytes.extend(json);
129 bytes
130 }
131 fn config() -> serde_json::Value {
132 serde_json::json!({"info":{"v":2,"w":100,"h":80,"f":30,"fps":25,"videoW":150,"videoH":80,"rgbFrame":[0,0,100,80],"aFrame":[100,0,50,40]},"src":[{"srcId":"avatar"}]})
133 }
134 #[test]
135 fn detects_vap_and_preserves_regions_and_dynamic_content_count() {
136 let info = inspect(&mp4(config())).unwrap().unwrap();
137 assert_eq!((info.width, info.height, info.frames), (100, 80, 30));
138 assert_eq!(info.alpha_frame, [100, 0, 50, 40]);
139 assert_eq!(info.dynamic_sources, 1);
140 assert!(inspect(b"ordinary data").unwrap().is_none());
141 }
142 #[test]
143 fn rejects_invalid_regions_timing_and_container_sizes() {
144 let mut invalid = config();
145 invalid["info"]["aFrame"] = serde_json::json!([149, 0, 50, 40]);
146 assert!(inspect(&mp4(invalid)).is_err());
147 let mut invalid = config();
148 invalid["info"]["fps"] = serde_json::json!(0);
149 assert!(inspect(&mp4(invalid)).is_err());
150 let mut bytes = mp4(config());
151 bytes[16..20].copy_from_slice(&u32::MAX.to_be_bytes());
152 assert!(inspect(&bytes).is_err());
153 let mut bytes = mp4(config());
154 bytes.extend_from_slice(&mp4(config())[16..]);
155 assert!(inspect(&bytes).is_err());
156 }
157}