1use crate::{
2 AcquireSync, Descriptor, DuplicateError, OwnedAcquireSync, OwnedDescriptor, Rect,
3 ValidationError,
4};
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7#[cfg_attr(feature = "rustler", derive(rustler::NifStruct))]
8#[cfg_attr(feature = "rustler", module = "VideoInterop.Binary.Plane")]
9pub struct BinaryPlane {
10 pub offset: u64,
11 pub stride: u32,
12}
13
14#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct BinaryStorage {
16 pub data: Vec<u8>,
17 pub planes: Vec<BinaryPlane>,
18}
19
20#[derive(Clone, Debug)]
21#[non_exhaustive]
22pub enum Storage {
23 Binary(BinaryStorage),
24 DmaBuf(Descriptor),
25}
26
27#[derive(Debug)]
28#[non_exhaustive]
29pub enum OwnedStorage {
30 Binary(BinaryStorage),
31 DmaBuf(OwnedDescriptor),
32}
33
34#[derive(Clone, Debug)]
35pub struct FrameDescriptor {
36 pub coded_width: u32,
37 pub coded_height: u32,
38 pub visible_rect: Rect,
39 pub storage: Storage,
40 pub acquire_sync: AcquireSync,
41}
42
43#[derive(Debug)]
44pub struct OwnedFrame {
45 pub coded_width: u32,
46 pub coded_height: u32,
47 pub visible_rect: Rect,
48 pub storage: OwnedStorage,
49 pub acquire_sync: OwnedAcquireSync,
50}
51
52impl Storage {
53 pub fn validate(&self) -> Result<(), ValidationError> {
54 match self {
55 Self::Binary(storage) => storage.validate(),
56 Self::DmaBuf(descriptor) => descriptor.validate(),
57 }
58 }
59
60 pub fn duplicate_cloexec(&self) -> Result<OwnedStorage, DuplicateError> {
61 match self {
62 Self::Binary(storage) => Ok(OwnedStorage::Binary(storage.clone())),
63 Self::DmaBuf(descriptor) => descriptor.duplicate_cloexec().map(OwnedStorage::DmaBuf),
64 }
65 }
66}
67
68impl BinaryStorage {
69 pub fn validate(&self) -> Result<(), ValidationError> {
70 if self.planes.is_empty() {
71 return Err(ValidationError::EmptyBinaryPlanes);
72 }
73 if self.planes.len() != 1 {
74 return Err(ValidationError::UnsupportedBinaryPlaneCount(
75 self.planes.len(),
76 ));
77 }
78 let plane = self.planes[0];
79 if plane.stride == 0 {
80 return Err(ValidationError::ZeroBinaryStride);
81 }
82 if plane.offset >= self.data.len() as u64 {
83 return Err(ValidationError::BinaryOffsetOutOfBounds {
84 offset: plane.offset,
85 data_size: self.data.len() as u64,
86 });
87 }
88 Ok(())
89 }
90
91 fn validate_height(&self, coded_height: u32) -> Result<(), ValidationError> {
92 let plane = self.planes[0];
93 let last_row = plane
94 .offset
95 .checked_add(u64::from(plane.stride) * u64::from(coded_height.saturating_sub(1)))
96 .ok_or(ValidationError::BinaryLastRowOutOfBounds {
97 offset: u64::MAX,
98 data_size: self.data.len() as u64,
99 })?;
100 if last_row >= self.data.len() as u64 {
101 return Err(ValidationError::BinaryLastRowOutOfBounds {
102 offset: last_row,
103 data_size: self.data.len() as u64,
104 });
105 }
106 Ok(())
107 }
108}
109
110impl FrameDescriptor {
111 pub fn validate(&self) -> Result<(), ValidationError> {
112 self.visible_rect
113 .validate(self.coded_width, self.coded_height)?;
114 self.storage.validate()?;
115 if let Storage::Binary(storage) = &self.storage {
116 storage.validate_height(self.coded_height)?;
117 }
118
119 if matches!(self.storage, Storage::Binary(_))
120 && !matches!(self.acquire_sync, AcquireSync::Implicit)
121 {
122 return Err(ValidationError::BinaryStorageRequiresImplicitSync);
123 }
124
125 if let AcquireSync::SyncFile(sync_file) = &self.acquire_sync
126 && sync_file.acquire_fence_fd < 0
127 {
128 return Err(ValidationError::NegativeAcquireFence(
129 sync_file.acquire_fence_fd,
130 ));
131 }
132
133 Ok(())
134 }
135
136 pub fn duplicate_cloexec(&self) -> Result<OwnedFrame, DuplicateError> {
137 self.validate()?;
138 let storage = self.storage.duplicate_cloexec()?;
139 let acquire_sync = self.acquire_sync.duplicate_cloexec()?;
140
141 Ok(OwnedFrame {
142 coded_width: self.coded_width,
143 coded_height: self.coded_height,
144 visible_rect: self.visible_rect.clone(),
145 storage,
146 acquire_sync,
147 })
148 }
149}
150
151#[cfg(test)]
152mod tests {
153 use super::*;
154
155 #[test]
156 fn validates_binary_last_row_without_requiring_trailing_padding() {
157 let frame = FrameDescriptor {
158 coded_width: 1,
159 coded_height: 2,
160 visible_rect: Rect {
161 x: 0,
162 y: 0,
163 width: 1,
164 height: 2,
165 },
166 storage: Storage::Binary(BinaryStorage {
167 data: vec![0; 10],
168 planes: vec![BinaryPlane {
169 offset: 0,
170 stride: 9,
171 }],
172 }),
173 acquire_sync: AcquireSync::Implicit,
174 };
175
176 assert_eq!(frame.validate(), Ok(()));
177 }
178
179 #[test]
180 fn rejects_binary_storage_when_the_last_row_is_absent() {
181 let frame = FrameDescriptor {
182 coded_width: 1,
183 coded_height: 2,
184 visible_rect: Rect {
185 x: 0,
186 y: 0,
187 width: 1,
188 height: 2,
189 },
190 storage: Storage::Binary(BinaryStorage {
191 data: vec![0; 9],
192 planes: vec![BinaryPlane {
193 offset: 0,
194 stride: 9,
195 }],
196 }),
197 acquire_sync: AcquireSync::Implicit,
198 };
199
200 assert_eq!(
201 frame.validate(),
202 Err(ValidationError::BinaryLastRowOutOfBounds {
203 offset: 9,
204 data_size: 9
205 })
206 );
207 }
208}
209
210#[cfg(feature = "rustler")]
211mod rustler_impl {
212 use rustler::{Binary, Decoder, Encoder, Env, NewBinary, NifResult, Term};
213
214 use super::{BinaryPlane, BinaryStorage, Storage};
215 use crate::Descriptor;
216
217 #[derive(rustler::NifStruct)]
218 #[module = "VideoInterop.Binary"]
219 struct BinaryStorageNif<'a> {
220 data: Binary<'a>,
221 planes: Vec<BinaryPlane>,
222 }
223
224 impl<'a> Decoder<'a> for Storage {
225 fn decode(term: Term<'a>) -> NifResult<Self> {
226 if let Ok(descriptor) = term.decode::<Descriptor>() {
227 return Ok(Self::DmaBuf(descriptor));
228 }
229
230 term.decode::<BinaryStorageNif>().map(|storage| {
231 Self::Binary(BinaryStorage {
232 data: storage.data.as_slice().to_vec(),
233 planes: storage.planes,
234 })
235 })
236 }
237 }
238
239 impl Encoder for Storage {
240 fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
241 match self {
242 Self::Binary(storage) => {
243 let mut data = NewBinary::new(env, storage.data.len());
244 data.as_mut_slice().copy_from_slice(&storage.data);
245 BinaryStorageNif {
246 data: data.into(),
247 planes: storage.planes.clone(),
248 }
249 .encode(env)
250 }
251 Self::DmaBuf(descriptor) => descriptor.encode(env),
252 }
253 }
254 }
255}