cameras 0.1.3

A cross-platform camera library for Rust, built with data-oriented design. Explicit format negotiation, push-based frame delivery, typed errors, and zero trait objects.
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! macOS VideoToolbox hardware H.264 / H.265 decoder.
//!
//! Builds a `CMFormatDescription` from the RTSP SDP extradata (AVCC for H.264,
//! HVCC for H.265), creates a `VTDecompressionSession`, and feeds NAL units
//! as `CMSampleBuffer`s. Decoded `CVPixelBuffer`s are pulled out of the async
//! output callback via an internal mutex-guarded queue and returned as NV12
//! [`Frame`]s (Y in `plane_primary`, UV in `plane_secondary`) matching the
//! Windows backend so the demo's GPU conversion path is identical on both.

use super::{VideoCodec, VideoDecoder};
use crate::error::Error;
use crate::types::{Frame, FrameQuality, PixelFormat};
use bytes::Bytes;
use objc2::rc::Retained;
use objc2_core_foundation::{CFDictionary, CFNumber, CFNumberType, CFRetained, CFString, CFType};
use objc2_core_media::{
    CMBlockBuffer, CMFormatDescription, CMSampleBuffer, CMSampleTimingInfo, CMTime, CMTimeFlags,
    CMVideoFormatDescriptionCreateFromH264ParameterSets,
    CMVideoFormatDescriptionCreateFromHEVCParameterSets, kCMTimeInvalid,
};
use objc2_core_video::{
    CVImageBuffer, CVPixelBuffer, CVPixelBufferGetBaseAddress, CVPixelBufferGetBaseAddressOfPlane,
    CVPixelBufferGetBytesPerRow, CVPixelBufferGetBytesPerRowOfPlane, CVPixelBufferGetHeight,
    CVPixelBufferGetHeightOfPlane, CVPixelBufferGetPlaneCount, CVPixelBufferGetWidth,
    CVPixelBufferLockBaseAddress, CVPixelBufferLockFlags, CVPixelBufferUnlockBaseAddress,
    kCVPixelBufferPixelFormatTypeKey,
};
use objc2_video_toolbox::{
    VTDecodeFrameFlags, VTDecodeInfoFlags, VTDecompressionOutputCallbackRecord,
    VTDecompressionSession,
};
use std::os::raw::c_void;
use std::ptr::NonNull;
use std::sync::{Arc, Mutex};
use std::time::Duration;

const NAL_LENGTH_SIZE: i32 = 4;

/// VideoToolbox decoder instance.
pub(crate) struct VideoToolboxDecoder {
    session: Retained<VTDecompressionSession>,
    format_desc: Retained<CMFormatDescription>,
    output: Arc<Mutex<OutputQueue>>,
}

unsafe impl Send for VideoToolboxDecoder {}

#[derive(Default)]
struct OutputQueue {
    frames: Vec<Frame>,
    error: Option<Error>,
}

fn build_destination_attributes() -> Option<CFRetained<CFDictionary>> {
    // NV12 biplanar video range: 'y420' -> kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
    let pixel_format: i32 = 0x34323076;
    let number = unsafe {
        CFNumber::new(
            None,
            CFNumberType::SInt32Type,
            &pixel_format as *const i32 as *const c_void,
        )
    }?;
    let number_ref: &CFType = (&*number).as_ref();
    let key_ref: &CFString = unsafe { kCVPixelBufferPixelFormatTypeKey };
    let typed: CFRetained<CFDictionary<CFString, CFType>> =
        CFDictionary::from_slices(&[key_ref], &[number_ref]);
    Some(unsafe { CFRetained::cast_unchecked(typed) })
}

fn catch_ns<R>(
    label: &'static str,
    closure: impl FnOnce() -> R + std::panic::UnwindSafe,
) -> Result<R, Error> {
    match objc2::exception::catch(closure) {
        Ok(value) => Ok(value),
        Err(None) => Err(Error::Backend {
            platform: "macos",
            message: format!("{label}: unknown NSException"),
        }),
        Err(Some(exception)) => Err(Error::Backend {
            platform: "macos",
            message: format!("{label}: {exception:?}"),
        }),
    }
}

impl VideoDecoder for VideoToolboxDecoder {
    fn new(codec: VideoCodec, extradata: &[u8]) -> Result<Self, Error> {
        let format_desc = build_format_description(codec, extradata)?;
        let output = Arc::new(Mutex::new(OutputQueue::default()));

        let callback = VTDecompressionOutputCallbackRecord {
            decompressionOutputCallback: Some(output_callback),
            decompressionOutputRefCon: Arc::into_raw(Arc::clone(&output)) as *mut c_void,
        };

        let destination_attrs = build_destination_attributes();
        let destination_attrs_ref = destination_attrs.as_deref();

        let mut raw_session: *mut VTDecompressionSession = std::ptr::null_mut();
        let status = catch_ns(
            "VTDecompressionSession::create",
            std::panic::AssertUnwindSafe(|| unsafe {
                VTDecompressionSession::create(
                    None,
                    &format_desc,
                    None,
                    destination_attrs_ref,
                    &callback,
                    NonNull::from(&mut raw_session),
                )
            }),
        );
        let status = match status {
            Ok(status) => status,
            Err(error) => {
                unsafe {
                    drop(Arc::from_raw(
                        callback.decompressionOutputRefCon as *const Mutex<OutputQueue>,
                    ));
                }
                return Err(error);
            }
        };
        if status != 0 || raw_session.is_null() {
            unsafe {
                drop(Arc::from_raw(
                    callback.decompressionOutputRefCon as *const Mutex<OutputQueue>,
                ));
            }
            return Err(Error::Backend {
                platform: "macos",
                message: format!("VTDecompressionSessionCreate failed: {status}"),
            });
        }

        let session = unsafe { Retained::from_raw(raw_session) }.ok_or(Error::Backend {
            platform: "macos",
            message: "VTDecompressionSession returned null".into(),
        })?;

        Ok(Self {
            session,
            format_desc,
            output,
        })
    }

    fn decode(&mut self, nal: &[u8], timestamp: Duration) -> Result<Vec<Frame>, Error> {
        let block = create_block_buffer(nal)?;
        let sample = create_sample_buffer(&block, &self.format_desc, timestamp)?;
        let mut info_flags = VTDecodeInfoFlags::empty();
        let status = catch_ns(
            "VTDecompressionSession::decode_frame",
            std::panic::AssertUnwindSafe(|| unsafe {
                self.session.decode_frame(
                    &sample,
                    VTDecodeFrameFlags::empty(),
                    std::ptr::null_mut(),
                    &mut info_flags,
                )
            }),
        )?;
        if status != 0 {
            return Err(Error::Backend {
                platform: "macos",
                message: format!("VTDecompressionSessionDecodeFrame failed: {status}"),
            });
        }

        let _ = catch_ns(
            "VTDecompressionSession::wait_for_asynchronous_frames",
            std::panic::AssertUnwindSafe(|| unsafe { self.session.wait_for_asynchronous_frames() }),
        );

        let mut guard = self.output.lock().map_err(|_| Error::Backend {
            platform: "macos",
            message: "decoder output mutex poisoned".into(),
        })?;
        if let Some(error) = guard.error.take() {
            return Err(error);
        }
        Ok(std::mem::take(&mut guard.frames))
    }
}

impl Drop for VideoToolboxDecoder {
    fn drop(&mut self) {
        unsafe { self.session.invalidate() };
    }
}

unsafe extern "C-unwind" fn output_callback(
    refcon: *mut c_void,
    _source_frame_refcon: *mut c_void,
    status: i32,
    _info_flags: VTDecodeInfoFlags,
    image_buffer: *mut CVImageBuffer,
    _presentation_timestamp: CMTime,
    _presentation_duration: CMTime,
) {
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        if refcon.is_null() {
            return;
        }
        let output = unsafe { &*(refcon as *const Mutex<OutputQueue>) };
        let Ok(mut guard) = output.lock() else { return };
        if status != 0 || image_buffer.is_null() {
            if guard.error.is_none() {
                guard.error = Some(Error::Backend {
                    platform: "macos",
                    message: format!("VideoToolbox decode callback status: {status}"),
                });
            }
            return;
        }
        let pixel_buffer = unsafe { &*(image_buffer as *const CVPixelBuffer) };
        if let Some(frame) = pixel_buffer_to_frame(pixel_buffer) {
            guard.frames.push(frame);
        }
    }));
    let _ = result;
}

fn pixel_buffer_to_frame(pb: &CVPixelBuffer) -> Option<Frame> {
    unsafe {
        CVPixelBufferLockBaseAddress(pb, CVPixelBufferLockFlags::ReadOnly);
    }

    let width = CVPixelBufferGetWidth(pb) as u32;
    let height = CVPixelBufferGetHeight(pb) as u32;
    let plane_count = CVPixelBufferGetPlaneCount(pb);
    let result = if plane_count >= 2 {
        copy_biplanar_nv12(pb, width, height)
    } else {
        copy_single_plane_bgra(pb, width, height)
    };

    unsafe {
        CVPixelBufferUnlockBaseAddress(pb, CVPixelBufferLockFlags::ReadOnly);
    }
    result
}

fn copy_biplanar_nv12(pb: &CVPixelBuffer, width: u32, height: u32) -> Option<Frame> {
    let y_stride = CVPixelBufferGetBytesPerRowOfPlane(pb, 0);
    let y_rows = CVPixelBufferGetHeightOfPlane(pb, 0);
    let uv_stride = CVPixelBufferGetBytesPerRowOfPlane(pb, 1);
    let uv_rows = CVPixelBufferGetHeightOfPlane(pb, 1);
    let y_base = CVPixelBufferGetBaseAddressOfPlane(pb, 0);
    let uv_base = CVPixelBufferGetBaseAddressOfPlane(pb, 1);
    if y_base.is_null() || uv_base.is_null() || y_stride == 0 || y_stride != uv_stride {
        return None;
    }
    let y_len = y_stride.saturating_mul(y_rows);
    let uv_len = uv_stride.saturating_mul(uv_rows);
    if y_len == 0 || uv_len == 0 {
        return None;
    }
    let y_data = unsafe { std::slice::from_raw_parts(y_base as *const u8, y_len) }.to_vec();
    let uv_data = unsafe { std::slice::from_raw_parts(uv_base as *const u8, uv_len) }.to_vec();
    Some(Frame {
        width,
        height,
        stride: y_stride as u32,
        timestamp: Duration::ZERO,
        pixel_format: PixelFormat::Nv12,
        quality: FrameQuality::Intact,
        plane_primary: Bytes::from(y_data),
        plane_secondary: Bytes::from(uv_data),
    })
}

fn copy_single_plane_bgra(pb: &CVPixelBuffer, width: u32, height: u32) -> Option<Frame> {
    let stride = CVPixelBufferGetBytesPerRow(pb);
    let base = CVPixelBufferGetBaseAddress(pb);
    if base.is_null() || stride == 0 || height == 0 {
        return None;
    }
    let byte_count = stride.saturating_mul(height as usize);
    if byte_count == 0 {
        return None;
    }
    let data = unsafe { std::slice::from_raw_parts(base as *const u8, byte_count) }.to_vec();
    Some(Frame {
        width,
        height,
        stride: stride as u32,
        timestamp: Duration::ZERO,
        pixel_format: PixelFormat::Bgra8,
        quality: FrameQuality::Intact,
        plane_primary: Bytes::from(data),
        plane_secondary: Bytes::new(),
    })
}

fn build_format_description(
    codec: VideoCodec,
    extradata: &[u8],
) -> Result<Retained<CMFormatDescription>, Error> {
    let (parameter_sets, sizes) = match codec {
        VideoCodec::H264 => parse_avcc(extradata)?,
        VideoCodec::H265 => parse_hvcc(extradata)?,
    };
    let pointers: Vec<NonNull<u8>> = parameter_sets
        .iter()
        .filter_map(|set| NonNull::new(set.as_ptr() as *mut u8))
        .collect();
    if pointers.len() != parameter_sets.len() {
        return Err(Error::Backend {
            platform: "macos",
            message: "parameter set had null pointer".into(),
        });
    }
    let pointers_nn =
        NonNull::new(pointers.as_ptr() as *mut NonNull<u8>).ok_or(Error::Backend {
            platform: "macos",
            message: "empty parameter set list".into(),
        })?;
    let sizes_nn = NonNull::new(sizes.as_ptr() as *mut usize).ok_or(Error::Backend {
        platform: "macos",
        message: "empty sizes list".into(),
    })?;

    let mut format_desc: *const CMFormatDescription = std::ptr::null();
    let status = catch_ns(
        "CMVideoFormatDescriptionCreate",
        std::panic::AssertUnwindSafe(|| unsafe {
            match codec {
                VideoCodec::H264 => CMVideoFormatDescriptionCreateFromH264ParameterSets(
                    None,
                    pointers.len(),
                    pointers_nn,
                    sizes_nn,
                    NAL_LENGTH_SIZE,
                    NonNull::from(&mut format_desc),
                ),
                VideoCodec::H265 => CMVideoFormatDescriptionCreateFromHEVCParameterSets(
                    None,
                    pointers.len(),
                    pointers_nn,
                    sizes_nn,
                    NAL_LENGTH_SIZE,
                    None,
                    NonNull::from(&mut format_desc),
                ),
            }
        }),
    )?;
    if status != 0 || format_desc.is_null() {
        return Err(Error::Backend {
            platform: "macos",
            message: format!("CMVideoFormatDescriptionCreate failed: {status}"),
        });
    }
    unsafe { Retained::from_raw(format_desc as *mut CMFormatDescription) }.ok_or(Error::Backend {
        platform: "macos",
        message: "format description returned null".into(),
    })
}

fn parse_avcc(data: &[u8]) -> Result<(Vec<Vec<u8>>, Vec<usize>), Error> {
    if data.len() < 7 {
        return Err(Error::Backend {
            platform: "macos",
            message: "AVCC record too short".into(),
        });
    }
    let mut parameter_sets = Vec::new();
    let mut sizes = Vec::new();
    let num_sps = (data[5] & 0x1F) as usize;
    let mut offset = 6;
    for _ in 0..num_sps {
        if offset + 2 > data.len() {
            break;
        }
        let len = u16::from_be_bytes([data[offset], data[offset + 1]]) as usize;
        offset += 2;
        if offset + len > data.len() {
            break;
        }
        parameter_sets.push(data[offset..offset + len].to_vec());
        sizes.push(len);
        offset += len;
    }
    if offset >= data.len() {
        return Err(Error::Backend {
            platform: "macos",
            message: "AVCC record missing PPS count".into(),
        });
    }
    let num_pps = data[offset] as usize;
    offset += 1;
    for _ in 0..num_pps {
        if offset + 2 > data.len() {
            break;
        }
        let len = u16::from_be_bytes([data[offset], data[offset + 1]]) as usize;
        offset += 2;
        if offset + len > data.len() {
            break;
        }
        parameter_sets.push(data[offset..offset + len].to_vec());
        sizes.push(len);
        offset += len;
    }
    if parameter_sets.is_empty() {
        return Err(Error::Backend {
            platform: "macos",
            message: "AVCC record produced no parameter sets".into(),
        });
    }
    Ok((parameter_sets, sizes))
}

fn parse_hvcc(data: &[u8]) -> Result<(Vec<Vec<u8>>, Vec<usize>), Error> {
    if data.len() < 23 {
        return Err(Error::Backend {
            platform: "macos",
            message: "HVCC record too short".into(),
        });
    }
    let mut parameter_sets = Vec::new();
    let mut sizes = Vec::new();
    let num_arrays = data[22] as usize;
    let mut offset = 23;
    for _ in 0..num_arrays {
        if offset + 3 > data.len() {
            break;
        }
        let num_nalus = u16::from_be_bytes([data[offset + 1], data[offset + 2]]) as usize;
        offset += 3;
        for _ in 0..num_nalus {
            if offset + 2 > data.len() {
                break;
            }
            let len = u16::from_be_bytes([data[offset], data[offset + 1]]) as usize;
            offset += 2;
            if offset + len > data.len() {
                break;
            }
            parameter_sets.push(data[offset..offset + len].to_vec());
            sizes.push(len);
            offset += len;
        }
    }
    if parameter_sets.is_empty() {
        return Err(Error::Backend {
            platform: "macos",
            message: "HVCC record produced no parameter sets".into(),
        });
    }
    Ok((parameter_sets, sizes))
}

fn create_block_buffer(nal: &[u8]) -> Result<Retained<CMBlockBuffer>, Error> {
    let mut raw: *mut CMBlockBuffer = std::ptr::null_mut();
    let status = catch_ns(
        "CMBlockBuffer::create_with_memory_block",
        std::panic::AssertUnwindSafe(|| unsafe {
            CMBlockBuffer::create_with_memory_block(
                None,
                std::ptr::null_mut(),
                nal.len(),
                None,
                std::ptr::null(),
                0,
                nal.len(),
                1,
                NonNull::from(&mut raw),
            )
        }),
    )?;
    if status != 0 || raw.is_null() {
        return Err(Error::Backend {
            platform: "macos",
            message: format!("CMBlockBuffer::create_with_memory_block failed: {status}"),
        });
    }
    let buffer = unsafe { Retained::from_raw(raw) }.ok_or(Error::Backend {
        platform: "macos",
        message: "block buffer null".into(),
    })?;
    let source = NonNull::new(nal.as_ptr() as *mut c_void).ok_or(Error::Backend {
        platform: "macos",
        message: "empty NAL buffer".into(),
    })?;
    let status = catch_ns(
        "CMBlockBuffer::replace_data_bytes",
        std::panic::AssertUnwindSafe(|| unsafe {
            CMBlockBuffer::replace_data_bytes(source, &buffer, 0, nal.len())
        }),
    )?;
    if status != 0 {
        return Err(Error::Backend {
            platform: "macos",
            message: format!("CMBlockBuffer::replace_data_bytes failed: {status}"),
        });
    }
    Ok(buffer)
}

fn create_sample_buffer(
    block: &CMBlockBuffer,
    format: &CMFormatDescription,
    timestamp: Duration,
) -> Result<Retained<CMSampleBuffer>, Error> {
    let mut raw: *mut CMSampleBuffer = std::ptr::null_mut();
    let invalid = unsafe { kCMTimeInvalid };
    let timing = CMSampleTimingInfo {
        duration: invalid,
        presentationTimeStamp: duration_to_cmtime(timestamp),
        decodeTimeStamp: invalid,
    };
    let status = catch_ns(
        "CMSampleBuffer::create",
        std::panic::AssertUnwindSafe(|| unsafe {
            CMSampleBuffer::create(
                None,
                Some(block),
                true,
                None,
                std::ptr::null_mut(),
                Some(format),
                1,
                1,
                &timing,
                0,
                std::ptr::null(),
                NonNull::from(&mut raw),
            )
        }),
    )?;
    if status != 0 || raw.is_null() {
        return Err(Error::Backend {
            platform: "macos",
            message: format!("CMSampleBufferCreate failed: {status}"),
        });
    }
    unsafe { Retained::from_raw(raw) }.ok_or(Error::Backend {
        platform: "macos",
        message: "sample buffer null".into(),
    })
}

fn duration_to_cmtime(duration: Duration) -> CMTime {
    CMTime {
        value: duration.as_micros() as i64,
        timescale: 1_000_000,
        flags: CMTimeFlags::Valid,
        epoch: 0,
    }
}