axonml-vision 0.5.0

Computer vision utilities for the Axonml ML framework
Documentation
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! V4L2 Backend — Linux Camera Capture via Video4Linux2
//!
//! # File
//! `crates/axonml-vision/src/camera/v4l2.rs`
//!
//! # Author
//! Andrew Jewell Sr - AutomataNexus
//!
//! # Updated
//! March 8, 2026
//!
//! # Disclaimer
//! Use at own risk. This software is provided "as is", without warranty of any
//! kind, express or implied. The author and AutomataNexus shall not be held
//! liable for any damages arising from the use of this software.

use super::{CaptureBackend, CaptureConfig, CaptureError, FrameBuffer, PixelFormat};
use std::fs;
use std::os::unix::io::AsRawFd;

// =============================================================================
// V4L2 Constants
// =============================================================================

// V4L2 pixel format FourCC codes
const V4L2_PIX_FMT_YUYV: u32 = 0x5659_5559; // 'YUYV'
const V4L2_PIX_FMT_MJPEG: u32 = 0x4745_504D; // 'MJPG'

// V4L2 buffer types
const V4L2_BUF_TYPE_VIDEO_CAPTURE: u32 = 1;

// V4L2 memory types
const V4L2_MEMORY_MMAP: u32 = 1;

// V4L2 ioctl request codes (platform-dependent)
// These are Linux-specific, computed from _IOWR macros
const VIDIOC_QUERYCAP: libc::c_ulong = 0x8068_5600;
const VIDIOC_S_FMT: libc::c_ulong = 0xC0CC_5605;
const VIDIOC_REQBUFS: libc::c_ulong = 0xC014_5608;
const VIDIOC_QUERYBUF: libc::c_ulong = 0xC044_5609;
const VIDIOC_QBUF: libc::c_ulong = 0xC044_560F;
const VIDIOC_DQBUF: libc::c_ulong = 0xC044_5611;
const VIDIOC_STREAMON: libc::c_ulong = 0x4004_5612;
const VIDIOC_STREAMOFF: libc::c_ulong = 0x4004_5613;

const NUM_BUFFERS: u32 = 4;

// =============================================================================
// V4L2 Structures (C-compatible)
// =============================================================================

#[repr(C)]
struct V4l2Capability {
    driver: [u8; 16],
    card: [u8; 32],
    bus_info: [u8; 32],
    version: u32,
    capabilities: u32,
    device_caps: u32,
    reserved: [u32; 3],
}

#[repr(C)]
struct V4l2PixFormat {
    width: u32,
    height: u32,
    pixelformat: u32,
    field: u32,
    bytesperline: u32,
    sizeimage: u32,
    colorspace: u32,
    priv_: u32,
    flags: u32,
    ycbcr_enc: u32,
    quantization: u32,
    xfer_func: u32,
}

#[repr(C)]
struct V4l2Format {
    type_: u32,
    fmt: V4l2PixFormat,
    padding: [u8; 128], // Union padding
}

#[repr(C)]
struct V4l2RequestBuffers {
    count: u32,
    type_: u32,
    memory: u32,
    capabilities: u32,
    flags: u8,
    reserved: [u8; 3],
}

#[repr(C)]
struct V4l2Timeval {
    tv_sec: i64,
    tv_usec: i64,
}

#[repr(C)]
struct V4l2Buffer {
    index: u32,
    type_: u32,
    bytesused: u32,
    flags: u32,
    field: u32,
    timestamp: V4l2Timeval,
    timecode: [u8; 16],
    sequence: u32,
    memory: u32,
    offset: u32, // m.offset in union
    length: u32,
    reserved2: u32,
    request_fd: i32,
}

// =============================================================================
// Memory-Mapped Buffer
// =============================================================================

struct MmapBuffer {
    ptr: *mut u8,
    length: usize,
}

impl Drop for MmapBuffer {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                libc::munmap(self.ptr.cast::<libc::c_void>(), self.length);
            }
        }
    }
}

// =============================================================================
// V4L2 Backend
// =============================================================================

/// V4L2 camera capture backend for Linux.
///
/// Uses memory-mapped I/O for zero-copy frame capture from USB cameras
/// and CSI cameras (Raspberry Pi).
///
/// # Example
/// ```ignore
/// use axonml_vision::camera::{V4L2Backend, CaptureBackend, CaptureConfig, PixelFormat};
///
/// let mut cam = V4L2Backend::new("/dev/video0");
/// cam.open(&CaptureConfig {
///     width: 640,
///     height: 480,
///     format: PixelFormat::Yuyv,
///     fps: 30,
/// }).unwrap();
///
/// let frame = cam.grab_frame().unwrap();
/// cam.close();
/// ```
pub struct V4L2Backend {
    device_path: String,
    file: Option<fs::File>,
    buffers: Vec<MmapBuffer>,
    width: u32,
    height: u32,
    format: PixelFormat,
    streaming: bool,
}

impl V4L2Backend {
    /// Create a V4L2 backend for the specified device.
    ///
    /// Common paths: `/dev/video0`, `/dev/video1`.
    pub fn new(device_path: &str) -> Self {
        Self {
            device_path: device_path.to_string(),
            file: None,
            buffers: Vec::new(),
            width: 0,
            height: 0,
            format: PixelFormat::Yuyv,
            streaming: false,
        }
    }

    /// Open the default camera device (`/dev/video0`).
    pub fn default_device() -> Self {
        Self::new("/dev/video0")
    }

    fn fd(&self) -> Result<i32, CaptureError> {
        self.file
            .as_ref()
            .map(|f| f.as_raw_fd())
            .ok_or(CaptureError::NotOpen)
    }

    fn ioctl(&self, request: libc::c_ulong, arg: *mut libc::c_void) -> Result<(), CaptureError> {
        let fd = self.fd()?;
        let ret = unsafe { libc::ioctl(fd, request, arg) };
        if ret < 0 {
            Err(CaptureError::CaptureError(format!(
                "ioctl 0x{:X} failed: {}",
                request,
                std::io::Error::last_os_error()
            )))
        } else {
            Ok(())
        }
    }

    fn set_format(&mut self, config: &CaptureConfig) -> Result<(), CaptureError> {
        let pixfmt = match config.format {
            PixelFormat::Yuyv => V4L2_PIX_FMT_YUYV,
            PixelFormat::Mjpeg => V4L2_PIX_FMT_MJPEG,
            _ => V4L2_PIX_FMT_YUYV,
        };

        let mut fmt: V4l2Format = unsafe { std::mem::zeroed() };
        fmt.type_ = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        fmt.fmt.width = config.width;
        fmt.fmt.height = config.height;
        fmt.fmt.pixelformat = pixfmt;

        self.ioctl(
            VIDIOC_S_FMT,
            std::ptr::addr_of_mut!(fmt).cast::<libc::c_void>(),
        )?;

        self.width = fmt.fmt.width;
        self.height = fmt.fmt.height;
        self.format = config.format;

        Ok(())
    }

    fn request_buffers(&mut self) -> Result<(), CaptureError> {
        let mut req: V4l2RequestBuffers = unsafe { std::mem::zeroed() };
        req.count = NUM_BUFFERS;
        req.type_ = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        req.memory = V4L2_MEMORY_MMAP;

        self.ioctl(
            VIDIOC_REQBUFS,
            std::ptr::addr_of_mut!(req).cast::<libc::c_void>(),
        )?;

        let fd = self.fd()?;

        for i in 0..req.count {
            let mut buf: V4l2Buffer = unsafe { std::mem::zeroed() };
            buf.index = i;
            buf.type_ = V4L2_BUF_TYPE_VIDEO_CAPTURE;
            buf.memory = V4L2_MEMORY_MMAP;

            self.ioctl(
                VIDIOC_QUERYBUF,
                std::ptr::addr_of_mut!(buf).cast::<libc::c_void>(),
            )?;

            let ptr = unsafe {
                libc::mmap(
                    std::ptr::null_mut(),
                    buf.length as usize,
                    libc::PROT_READ | libc::PROT_WRITE,
                    libc::MAP_SHARED,
                    fd,
                    buf.offset as libc::off_t,
                )
            };

            if ptr == libc::MAP_FAILED {
                return Err(CaptureError::CaptureError("mmap failed".into()));
            }

            self.buffers.push(MmapBuffer {
                ptr: ptr.cast::<u8>(),
                length: buf.length as usize,
            });

            // Queue the buffer
            self.ioctl(
                VIDIOC_QBUF,
                std::ptr::addr_of_mut!(buf).cast::<libc::c_void>(),
            )?;
        }

        Ok(())
    }

    fn start_streaming(&mut self) -> Result<(), CaptureError> {
        let mut type_ = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        self.ioctl(
            VIDIOC_STREAMON,
            std::ptr::addr_of_mut!(type_).cast::<libc::c_void>(),
        )?;
        self.streaming = true;
        Ok(())
    }

    fn stop_streaming(&mut self) {
        if self.streaming {
            let mut type_ = V4L2_BUF_TYPE_VIDEO_CAPTURE;
            let _ = self.ioctl(
                VIDIOC_STREAMOFF,
                std::ptr::addr_of_mut!(type_).cast::<libc::c_void>(),
            );
            self.streaming = false;
        }
    }
}

impl CaptureBackend for V4L2Backend {
    fn open(&mut self, config: &CaptureConfig) -> Result<(), CaptureError> {
        use std::fs::OpenOptions;

        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .open(&self.device_path)
            .map_err(|e| CaptureError::DeviceNotFound(format!("{}: {}", self.device_path, e)))?;

        self.file = Some(file);

        // Query capabilities
        let mut cap: V4l2Capability = unsafe { std::mem::zeroed() };
        self.ioctl(
            VIDIOC_QUERYCAP,
            std::ptr::addr_of_mut!(cap).cast::<libc::c_void>(),
        )?;

        self.set_format(config)?;
        self.request_buffers()?;
        self.start_streaming()?;

        Ok(())
    }

    fn grab_frame(&mut self) -> Result<FrameBuffer, CaptureError> {
        if !self.streaming {
            return Err(CaptureError::NotOpen);
        }

        // Dequeue a buffer
        let mut buf: V4l2Buffer = unsafe { std::mem::zeroed() };
        buf.type_ = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        buf.memory = V4L2_MEMORY_MMAP;

        self.ioctl(
            VIDIOC_DQBUF,
            std::ptr::addr_of_mut!(buf).cast::<libc::c_void>(),
        )?;

        let idx = buf.index as usize;
        let mmap = &self.buffers[idx];

        // Copy frame data
        let data = unsafe { std::slice::from_raw_parts(mmap.ptr, buf.bytesused as usize) };
        let frame_data = data.to_vec();

        let timestamp_us =
            (buf.timestamp.tv_sec as u64) * 1_000_000 + (buf.timestamp.tv_usec as u64);

        // Re-queue the buffer
        self.ioctl(
            VIDIOC_QBUF,
            std::ptr::addr_of_mut!(buf).cast::<libc::c_void>(),
        )?;

        Ok(FrameBuffer {
            data: frame_data,
            width: self.width,
            height: self.height,
            format: self.format,
            timestamp_us,
        })
    }

    fn is_open(&self) -> bool {
        self.streaming
    }

    fn close(&mut self) {
        self.stop_streaming();
        self.buffers.clear();
        self.file = None;
    }

    fn resolution(&self) -> (u32, u32) {
        (self.width, self.height)
    }
}

impl Drop for V4L2Backend {
    fn drop(&mut self) {
        self.close();
    }
}

// Note: V4L2 tests require actual camera hardware and are not included
// in the unit test suite. Integration testing is done on target devices.