1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use std::{marker::PhantomData, ptr::NonNull};

use libcamera_sys::*;
use num_enum::{IntoPrimitive, TryFromPrimitive};

use crate::utils::Immutable;

#[derive(Debug, Clone, Copy, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum FrameMetadataStatus {
    Success = libcamera_frame_metadata_status::LIBCAMERA_FRAME_METADATA_STATUS_SUCCESS,
    Error = libcamera_frame_metadata_status::LIBCAMERA_FRAME_METADATA_STATUS_ERROR,
    Cancelled = libcamera_frame_metadata_status::LIBCAMERA_FRAME_METADATA_STATUS_CANCELLED,
}

pub type FrameMetadataPlane = libcamera_frame_metadata_plane_t;

pub struct FrameMetadataPlanes {
    pub(crate) ptr: NonNull<libcamera_frame_metadata_planes_t>,
}

impl FrameMetadataPlanes {
    pub(crate) unsafe fn from_ptr(ptr: NonNull<libcamera_frame_metadata_planes_t>) -> Self {
        Self { ptr }
    }

    /// Number of planes within framebuffer metadata.
    ///
    /// Should be consistent with other planes within framebuffer.
    pub fn len(&self) -> usize {
        unsafe { libcamera_frame_metadata_planes_size(self.ptr.as_ptr()) as _ }
    }

    /// Returns `true` if there are no planes.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns framebuffer plane metadata at a given index.
    ///
    /// Return None if given index is out of range of available planes.
    pub fn get(&self, index: usize) -> Option<FrameMetadataPlane> {
        if index >= self.len() {
            None
        } else {
            Some(unsafe { libcamera_frame_metadata_planes_at(self.ptr.as_ptr(), index as _).read() })
        }
    }
}

impl core::fmt::Debug for FrameMetadataPlanes {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut list = f.debug_list();
        for plane in self.into_iter() {
            list.entry(&plane);
        }
        list.finish()
    }
}

impl Drop for FrameMetadataPlanes {
    fn drop(&mut self) {
        unsafe { libcamera_frame_metadata_planes_destroy(self.ptr.as_ptr()) }
    }
}

impl<'d> IntoIterator for &'d FrameMetadataPlanes {
    type Item = FrameMetadataPlane;

    type IntoIter = FrameMetadataPlanesIterator<'d>;

    fn into_iter(self) -> Self::IntoIter {
        FrameMetadataPlanesIterator { planes: self, index: 0 }
    }
}

pub struct FrameMetadataPlanesIterator<'d> {
    planes: &'d FrameMetadataPlanes,
    index: usize,
}

impl<'d> Iterator for FrameMetadataPlanesIterator<'d> {
    type Item = FrameMetadataPlane;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(plane) = self.planes.get(self.index) {
            self.index += 1;
            Some(plane)
        } else {
            None
        }
    }
}

pub struct FrameMetadataRef<'d> {
    pub(crate) ptr: NonNull<libcamera_frame_metadata_t>,
    _phantom: PhantomData<&'d ()>,
}

impl<'d> FrameMetadataRef<'d> {
    pub(crate) unsafe fn from_ptr(ptr: NonNull<libcamera_frame_metadata_t>) -> Self {
        Self {
            ptr,
            _phantom: Default::default(),
        }
    }

    pub fn status(&self) -> FrameMetadataStatus {
        FrameMetadataStatus::try_from(unsafe { libcamera_frame_metadata_status(self.ptr.as_ptr()) }).unwrap()
    }

    pub fn sequence(&self) -> u32 {
        unsafe { libcamera_frame_metadata_sequence(self.ptr.as_ptr()) }
    }

    pub fn timestamp(&self) -> u64 {
        unsafe { libcamera_frame_metadata_timestamp(self.ptr.as_ptr()) }
    }

    pub fn planes(&self) -> FrameMetadataPlanes {
        unsafe {
            FrameMetadataPlanes::from_ptr(NonNull::new(libcamera_frame_metadata_planes(self.ptr.as_ptr())).unwrap())
        }
    }
}

impl<'d> core::fmt::Debug for FrameMetadataRef<'d> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FrameMetadataRef")
            .field("status", &self.status())
            .field("sequence", &self.sequence())
            .field("timestamp", &self.timestamp())
            .field("planes", &self.planes())
            .finish()
    }
}

pub struct FrameBufferPlaneRef<'d> {
    pub(crate) ptr: NonNull<libcamera_framebuffer_plane_t>,
    _phantom: PhantomData<&'d ()>,
}

impl<'d> FrameBufferPlaneRef<'d> {
    pub(crate) unsafe fn from_ptr(ptr: NonNull<libcamera_framebuffer_plane_t>) -> Self {
        Self {
            ptr,
            _phantom: Default::default(),
        }
    }

    /// File descriptor to the framebuffer plane data.
    ///
    /// Multiple planes may point to the same file descriptor at different offsets.
    pub fn fd(&self) -> i32 {
        unsafe { libcamera_framebuffer_plane_fd(self.ptr.as_ptr()) }
    }

    /// Offset of data within the file descriptor.
    pub fn offset(&self) -> Option<usize> {
        if unsafe { libcamera_framebuffer_plane_offset_valid(self.ptr.as_ptr()) } {
            Some(unsafe { libcamera_framebuffer_plane_offset(self.ptr.as_ptr()) as _ })
        } else {
            None
        }
    }

    /// Data length of the plane in bytes
    pub fn len(&self) -> usize {
        unsafe { libcamera_framebuffer_plane_length(self.ptr.as_ptr()) as _ }
    }

    /// Returns `true` if plane has no data
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<'d> core::fmt::Debug for FrameBufferPlaneRef<'d> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FrameBufferPlaneRef")
            .field("fd", &self.fd())
            .field("offset", &self.offset())
            .field("len", &self.len())
            .finish()
    }
}

pub struct FrameBufferPlanesRef<'d> {
    pub(crate) ptr: NonNull<libcamera_framebuffer_planes_t>,
    _phantom: PhantomData<&'d ()>,
}

impl<'d> FrameBufferPlanesRef<'d> {
    pub(crate) unsafe fn from_ptr(ptr: NonNull<libcamera_framebuffer_planes_t>) -> Self {
        Self {
            ptr,
            _phantom: Default::default(),
        }
    }

    /// Number of planes within framebuffer
    pub fn len(&self) -> usize {
        unsafe { libcamera_framebuffer_planes_size(self.ptr.as_ptr()) as _ }
    }

    /// Returns `true` if framebuffer has no planes
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns framebuffer plane at a given index
    pub fn get(&self, index: usize) -> Option<Immutable<FrameBufferPlaneRef<'_>>> {
        if index >= self.len() {
            None
        } else {
            Some(Immutable(unsafe {
                FrameBufferPlaneRef::from_ptr(
                    NonNull::new(libcamera_framebuffer_planes_at(self.ptr.as_ptr(), index as _)).unwrap(),
                )
            }))
        }
    }
}

impl<'d> core::fmt::Debug for FrameBufferPlanesRef<'d> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut list = f.debug_list();
        for plane in self.into_iter() {
            list.entry(&plane);
        }
        list.finish()
    }
}

impl<'d> IntoIterator for &'d FrameBufferPlanesRef<'d> {
    type Item = Immutable<FrameBufferPlaneRef<'d>>;

    type IntoIter = FrameBufferPlanesRefIterator<'d>;

    fn into_iter(self) -> Self::IntoIter {
        FrameBufferPlanesRefIterator { planes: self, index: 0 }
    }
}

pub struct FrameBufferPlanesRefIterator<'d> {
    planes: &'d FrameBufferPlanesRef<'d>,
    index: usize,
}

impl<'d> Iterator for FrameBufferPlanesRefIterator<'d> {
    type Item = Immutable<FrameBufferPlaneRef<'d>>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(plane) = self.planes.get(self.index) {
            self.index += 1;
            Some(plane)
        } else {
            None
        }
    }
}

pub trait AsFrameBuffer: Send {
    /// Returns raw framebuffer used by libcamera.
    ///
    /// It is expected that metadata status field is initialized with u32::MAX on a new buffer, which indicates that
    /// metadata is not yet available. This "hackfix" prevents read of uninitialized data in [Self::metadata()].
    ///
    /// # Safety
    ///
    /// This function must return a valid instance of `libcamera::FrameBuffer`.
    unsafe fn ptr(&self) -> NonNull<libcamera_framebuffer_t>;

    /// Returns framebuffer metadata information.
    ///
    /// Only available after associated [Request](crate::request::Request) has completed.
    fn metadata(&self) -> Option<Immutable<FrameMetadataRef<'_>>> {
        let ptr = NonNull::new(unsafe { libcamera_framebuffer_metadata(self.ptr().as_ptr()) }.cast_mut()).unwrap();
        if unsafe { libcamera_frame_metadata_status(ptr.as_ptr()) } != u32::MAX {
            Some(unsafe { Immutable(FrameMetadataRef::from_ptr(ptr)) })
        } else {
            None
        }
    }

    /// Provides access to framebuffer data by exposing file descriptors, offsets and lengths of the planes.
    fn planes(&self) -> Immutable<FrameBufferPlanesRef<'_>> {
        unsafe {
            Immutable(FrameBufferPlanesRef::from_ptr(
                NonNull::new(libcamera_framebuffer_planes(self.ptr().as_ptr()).cast_mut()).unwrap(),
            ))
        }
    }
}