oxideav-videotoolbox 0.0.3

macOS VideoToolbox hardware decode/encode bridge for the oxideav framework — runtime-loaded via libloading, no compile-time framework dep
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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//! VTDecompressionSession-backed H.264 and HEVC decoders.
//!
//! # Design
//!
//! Each decoder holds a `VTDecompressionSessionRef` which is created
//! lazily the first time SPS+PPS (or VPS+SPS+PPS for HEVC) are seen.
//! Annex-B input is parsed to extract parameter sets, from which a
//! `CMVideoFormatDescription` is built. Subsequent NAL units are wrapped
//! in a `CMSampleBuffer` and handed to VideoToolbox.
//!
//! VT calls the registered callback with each decoded `CVPixelBuffer`
//! (NV12 / '420v'). The callback copies the two planes (Y + interleaved
//! UV) and converts to planar I420 so the rest of oxideav sees
//! `PixelFormat::Yuv420P` / `VideoFrame`.

use std::collections::VecDeque;
use std::ffi::c_void;
use std::sync::{Arc, Mutex};

use oxideav_core::{
    CodecId, CodecParameters, Error, Frame, Packet, Result, VideoFrame, VideoPlane,
};

use crate::sys::{
    self, CMSampleTimingInfo, CMTime, K_CV_PIXEL_FORMAT_420_YPCBCRi8_BI_PLANAR_VIDEO_RANGE,
    VTDecompressionOutputCallbackRecord, K_CV_PIXEL_BUFFER_LOCK_FLAGS_READ_ONLY,
    K_OS_STATUS_NO_ERROR,
};

// ─────────────────────────── NAL unit iterator ───────────────────────────────

fn annex_b_nals(buf: &[u8]) -> Vec<&[u8]> {
    let mut result = Vec::new();
    let mut pos = 0usize;
    let len = buf.len();

    while pos < len {
        // Find start code.
        let mut sc_len = 0usize;
        while pos + 3 <= len {
            if buf[pos] == 0 && buf[pos + 1] == 0 {
                if pos + 4 <= len && buf[pos + 2] == 0 && buf[pos + 3] == 1 {
                    sc_len = 4;
                    break;
                } else if buf[pos + 2] == 1 {
                    sc_len = 3;
                    break;
                }
            }
            pos += 1;
        }

        if sc_len == 0 {
            break;
        }
        pos += sc_len;
        let nal_start = pos;

        // Find end (next start code or end of buf).
        let mut nal_end = len;
        while pos + 3 <= len {
            if buf[pos] == 0 && buf[pos + 1] == 0 {
                if pos + 4 <= len && buf[pos + 2] == 0 && buf[pos + 3] == 1 {
                    nal_end = pos;
                    // Strip trailing zeros.
                    while nal_end > nal_start && buf[nal_end - 1] == 0 {
                        nal_end -= 1;
                    }
                    break;
                } else if buf[pos + 2] == 1 {
                    nal_end = pos;
                    while nal_end > nal_start && buf[nal_end - 1] == 0 {
                        nal_end -= 1;
                    }
                    break;
                }
            }
            pos += 1;
        }

        if nal_end > nal_start {
            result.push(&buf[nal_start..nal_end]);
        }
        // Don't advance pos past nal_end — the outer loop will rescan.
        pos = nal_end;
    }

    result
}

// ─────────────────────────── NAL type constants ───────────────────────────────

mod h264_nal {
    pub const SPS: u8 = 7;
    pub const PPS: u8 = 8;
}

mod hevc_nal {
    pub const VPS: u8 = 32;
    pub const SPS: u8 = 33;
    pub const PPS: u8 = 34;
}

// ─────────────────────────── Callback state ───────────────────────────────────

struct CallbackState {
    frames: VecDeque<VideoFrame>,
    error: Option<String>,
}

impl CallbackState {
    fn new() -> Arc<Mutex<Self>> {
        Arc::new(Mutex::new(Self {
            frames: VecDeque::new(),
            error: None,
        }))
    }
}

unsafe extern "C" fn decomp_callback(
    output_callback_ref_con: *mut c_void,
    _source_frame_ref_con: *mut c_void,
    status: i32,
    _info_flags: u32,
    image_buffer: sys::CVImageBufferRef,
) {
    let state_ptr = output_callback_ref_con as *const Mutex<CallbackState>;
    let state = unsafe { &*state_ptr };
    let mut guard = match state.lock() {
        Ok(g) => g,
        Err(_) => return,
    };

    if status != K_OS_STATUS_NO_ERROR {
        guard.error = Some(format!("VT decode callback OSStatus {status}"));
        return;
    }
    if image_buffer.is_null() {
        return;
    }

    let vt = match sys::vtable() {
        Ok(v) => v,
        Err(e) => {
            guard.error = Some(format!("vtable in callback: {e}"));
            return;
        }
    };

    let ret = unsafe { (vt.cv_pb_lock)(image_buffer, K_CV_PIXEL_BUFFER_LOCK_FLAGS_READ_ONLY) };
    if ret != 0 {
        guard.error = Some(format!("CVPixelBufferLockBaseAddress: {ret}"));
        return;
    }

    let width = unsafe { (vt.cv_pb_get_width)(image_buffer) };
    let height = unsafe { (vt.cv_pb_get_height)(image_buffer) };
    let chroma_w = width.div_ceil(2);
    let chroma_h = height.div_ceil(2);

    let y_ptr = unsafe { (vt.cv_pb_get_base_of_plane)(image_buffer, 0) } as *const u8;
    let y_stride = unsafe { (vt.cv_pb_get_bpr_of_plane)(image_buffer, 0) };
    let y_height = unsafe { (vt.cv_pb_get_height_of_plane)(image_buffer, 0) };
    let uv_ptr = unsafe { (vt.cv_pb_get_base_of_plane)(image_buffer, 1) } as *const u8;
    let uv_stride = unsafe { (vt.cv_pb_get_bpr_of_plane)(image_buffer, 1) };
    let uv_height = unsafe { (vt.cv_pb_get_height_of_plane)(image_buffer, 1) };

    let mut y_data = vec![0u8; width * height];
    let mut u_data = vec![0u8; chroma_w * chroma_h];
    let mut v_data = vec![0u8; chroma_w * chroma_h];

    if !y_ptr.is_null() {
        for row in 0..y_height.min(height) {
            let row_len = width.min(y_stride);
            let src = unsafe { std::slice::from_raw_parts(y_ptr.add(row * y_stride), row_len) };
            let dst = row * width;
            y_data[dst..dst + row_len].copy_from_slice(src);
        }
    }

    if !uv_ptr.is_null() {
        for row in 0..uv_height.min(chroma_h) {
            let row_len = (chroma_w * 2).min(uv_stride);
            let src = unsafe { std::slice::from_raw_parts(uv_ptr.add(row * uv_stride), row_len) };
            let dst = row * chroma_w;
            for col in 0..chroma_w {
                u_data[dst + col] = if col * 2 < row_len { src[col * 2] } else { 128 };
                v_data[dst + col] = if col * 2 + 1 < row_len {
                    src[col * 2 + 1]
                } else {
                    128
                };
            }
        }
    }

    unsafe { (vt.cv_pb_unlock)(image_buffer, 0) };

    guard.frames.push_back(VideoFrame {
        pts: None,
        planes: vec![
            VideoPlane {
                stride: width,
                data: y_data,
            },
            VideoPlane {
                stride: chroma_w,
                data: u_data,
            },
            VideoPlane {
                stride: chroma_w,
                data: v_data,
            },
        ],
    });
}

// ─────────────────────────── Session creation ─────────────────────────────────

/// Returns (session, retained_fmt_desc).
/// Caller must call `cf_release` on the returned `fmt_desc` when done — but the
/// session itself retains it, so the caller must retain it separately if they
/// want to keep using it after releasing.
fn create_vt_session(
    vt: &sys::Vtable,
    fmt_desc: sys::CMVideoFormatDescriptionRef,
    state: &Arc<Mutex<CallbackState>>,
) -> Result<sys::VTDecompressionSessionRef> {
    let pixel_fmt_val = K_CV_PIXEL_FORMAT_420_YPCBCRi8_BI_PLANAR_VIDEO_RANGE as i32;
    let pixel_fmt_num = unsafe { sys::cf_number_i32(vt, pixel_fmt_val) };
    let pf_key = unsafe { sys::cf_string(vt, "CVPixelBufferPixelFormatTypeKey") };

    let keys: [*const c_void; 1] = [pf_key as *const c_void];
    let vals: [*const c_void; 1] = [pixel_fmt_num as *const c_void];

    let dest_attrs = unsafe {
        (vt.cf_dict_create)(
            std::ptr::null_mut(),
            keys.as_ptr(),
            vals.as_ptr(),
            1,
            std::ptr::null(),
            std::ptr::null(),
        )
    };

    let state_raw = Arc::as_ptr(state) as *mut c_void;
    let record = VTDecompressionOutputCallbackRecord {
        decomp_output_callback: decomp_callback,
        decomp_output_ref_con: state_raw,
    };

    let mut session = std::ptr::null_mut();
    let status = unsafe {
        (vt.vt_decomp_create)(
            std::ptr::null_mut(),
            fmt_desc,
            std::ptr::null_mut(),
            dest_attrs,
            &record,
            &mut session,
        )
    };

    unsafe { (vt.cf_release)(dest_attrs) };
    unsafe { (vt.cf_release)(pixel_fmt_num) };
    unsafe { (vt.cf_release)(pf_key) };

    if status != K_OS_STATUS_NO_ERROR {
        Err(Error::other(format!(
            "VTDecompressionSessionCreate: OSStatus {status}"
        )))
    } else {
        Ok(session)
    }
}

/// Submit AVCC-framed NAL units to a VT session.
/// `fmt_desc` must be the format description the session was created with.
fn submit_nal_units(
    vt: &sys::Vtable,
    session: sys::VTDecompressionSessionRef,
    fmt_desc: sys::CMVideoFormatDescriptionRef,
    nal_units: &[Vec<u8>],
    pts: Option<i64>,
    pts_counter: i64,
) -> Result<()> {
    if nal_units.is_empty() {
        return Ok(());
    }

    // Build AVCC payload (4-byte big-endian length prefix per NAL).
    let mut avcc: Vec<u8> = Vec::new();
    for nal in nal_units {
        let len = nal.len() as u32;
        avcc.extend_from_slice(&len.to_be_bytes());
        avcc.extend_from_slice(nal);
    }
    let avcc_len = avcc.len();

    // Create CMBlockBuffer with a copy of the data (custom allocator = kCFAllocatorNull
    // would work, but using a copied buffer is simpler and safe).
    let mut block_buf: sys::CMBlockBufferRef = std::ptr::null_mut();

    // Use CMBlockBufferCreateWithMemoryBlock. We pass a NULL memory block
    // which means VT allocates and copies the data itself.
    // To do a safe no-copy: we'd need to keep `avcc` alive until VT is done.
    // Instead, we allocate separately and let CF own it.
    //
    // kCFAllocatorDefault is NULL in the API.
    // kCFAllocatorNull = pointer to a global; we can't get it easily without headers.
    //
    // Simplest safe approach: allocate our own copy via raw malloc, pass it with
    // a NULL block allocator (which = default = malloc family), and CF will own it.
    // But we need CF to free it, which it won't if blockAllocator is NULL.
    //
    // Correct approach: pass the data pointer AND a non-null blockAllocator that
    // frees via `free()`. We'll approximate kCFAllocatorMalloc by using NULL
    // for the structure allocator and passing a malloc'd copy.

    // Allocate a copy using the system allocator that CF will free.
    let data_copy = unsafe {
        let ptr = libc_malloc(avcc_len);
        if ptr.is_null() {
            return Err(Error::other("malloc for CMBlockBuffer data failed"));
        }
        std::ptr::copy_nonoverlapping(avcc.as_ptr(), ptr as *mut u8, avcc_len);
        ptr
    };

    let status = unsafe {
        (vt.cm_block_create_with_mem)(
            std::ptr::null_mut(), // structure allocator = kCFAllocatorDefault
            data_copy,
            avcc_len,
            std::ptr::null_mut(), // block allocator = kCFAllocatorDefault = will free data_copy
            std::ptr::null(),     // custom block source
            0,                    // offset
            avcc_len,
            0, // flags
            &mut block_buf,
        )
    };

    if status != K_OS_STATUS_NO_ERROR {
        // Free our copy since CF won't.
        unsafe { libc_free(data_copy) };
        return Err(Error::other(format!(
            "CMBlockBufferCreateWithMemoryBlock: {status}"
        )));
    }

    let timing = CMSampleTimingInfo {
        duration: CMTime::make(1, 30),
        presentation_time_stamp: CMTime::make(pts.unwrap_or(pts_counter), 1_000_000),
        decode_time_stamp: CMTime::make(i64::MIN, 1),
    };

    let mut sample_buf: sys::CMSampleBufferRef = std::ptr::null_mut();
    let status = unsafe {
        (vt.cm_sample_create_ready)(
            std::ptr::null_mut(),
            block_buf,
            fmt_desc, // must match the session's format description
            1,        // num samples
            1,        // num timing entries
            &timing,
            1,
            &avcc_len,
            &mut sample_buf,
        )
    };

    unsafe { (vt.cf_release)(block_buf) };

    if status != K_OS_STATUS_NO_ERROR {
        return Err(Error::other(format!("CMSampleBufferCreateReady: {status}")));
    }

    let dec_status = unsafe {
        (vt.vt_decomp_decode)(
            session,
            sample_buf,
            0,
            std::ptr::null_mut(),
            std::ptr::null_mut(),
        )
    };

    unsafe { (vt.cf_release)(sample_buf) };

    if dec_status != K_OS_STATUS_NO_ERROR {
        return Err(Error::other(format!(
            "VTDecompressionSessionDecodeFrame: {dec_status}"
        )));
    }

    Ok(())
}

// Minimal libc shim — just malloc/free.
unsafe fn libc_malloc(size: usize) -> *mut c_void {
    extern "C" {
        fn malloc(size: usize) -> *mut c_void;
    }
    unsafe { malloc(size) }
}

unsafe fn libc_free(ptr: *mut c_void) {
    extern "C" {
        fn free(ptr: *mut c_void);
    }
    unsafe { free(ptr) }
}

// ─────────────────────────── H.264 decoder ────────────────────────────────────

pub struct H264VtDecoder {
    codec_id: CodecId,
    session: sys::VTDecompressionSessionRef,
    /// Retained format description — used to create CMSampleBuffers.
    fmt_desc: sys::CMVideoFormatDescriptionRef,
    state: Arc<Mutex<CallbackState>>,
    sps_list: Vec<Vec<u8>>,
    pps_list: Vec<Vec<u8>>,
    output_queue: VecDeque<VideoFrame>,
    pts_counter: i64,
    flushed: bool,
}

unsafe impl Send for H264VtDecoder {}

impl H264VtDecoder {
    pub fn make(params: &CodecParameters) -> Result<Box<dyn oxideav_core::Decoder>> {
        sys::vtable().map_err(|e| Error::unsupported(format!("videotoolbox: {e}")))?;
        let _ = params;
        Ok(Box::new(H264VtDecoder {
            codec_id: CodecId::new("h264"),
            session: std::ptr::null_mut(),
            fmt_desc: std::ptr::null_mut(),
            state: CallbackState::new(),
            sps_list: Vec::new(),
            pps_list: Vec::new(),
            output_queue: VecDeque::new(),
            pts_counter: 0,
            flushed: false,
        }))
    }

    fn ensure_session(&mut self) -> Result<()> {
        if !self.session.is_null() {
            return Ok(());
        }
        if self.sps_list.is_empty() || self.pps_list.is_empty() {
            return Ok(());
        }

        let vt = sys::vtable().map_err(|e| Error::unsupported(format!("videotoolbox: {e}")))?;

        let param_ptrs: Vec<*const u8> = self
            .sps_list
            .iter()
            .chain(self.pps_list.iter())
            .map(|v| v.as_ptr())
            .collect();
        let param_sizes: Vec<usize> = self
            .sps_list
            .iter()
            .chain(self.pps_list.iter())
            .map(|v| v.len())
            .collect();

        let mut fmt_desc: sys::CMVideoFormatDescriptionRef = std::ptr::null_mut();
        let st = unsafe {
            (vt.cm_fmt_from_h264_params)(
                std::ptr::null_mut(),
                param_ptrs.len(),
                param_ptrs.as_ptr(),
                param_sizes.as_ptr(),
                4,
                &mut fmt_desc,
            )
        };
        if st != K_OS_STATUS_NO_ERROR {
            return Err(Error::other(format!(
                "CMVideoFormatDescriptionCreateFromH264ParameterSets: {st}"
            )));
        }

        let session = create_vt_session(vt, fmt_desc, &self.state)?;

        // Retain fmt_desc for our own use (the session also retains it).
        unsafe { (vt.cf_retain)(fmt_desc) };

        self.session = session;
        self.fmt_desc = fmt_desc;

        // Release the creation reference — we hold our own retained copy.
        unsafe { (vt.cf_release)(fmt_desc) };

        Ok(())
    }

    fn pull_frames(&mut self) {
        if let Ok(mut g) = self.state.lock() {
            while let Some(f) = g.frames.pop_front() {
                self.output_queue.push_back(f);
            }
        }
    }
}

impl Drop for H264VtDecoder {
    fn drop(&mut self) {
        if let Ok(vt) = sys::vtable() {
            if !self.session.is_null() {
                unsafe { (vt.vt_decomp_invalidate)(self.session) };
            }
            if !self.fmt_desc.is_null() {
                unsafe { (vt.cf_release)(self.fmt_desc) };
            }
        }
    }
}

impl oxideav_core::Decoder for H264VtDecoder {
    fn codec_id(&self) -> &CodecId {
        &self.codec_id
    }

    fn send_packet(&mut self, packet: &Packet) -> Result<()> {
        self.flushed = false;

        if let Some(e) = self
            .state
            .lock()
            .ok()
            .and_then(|g| g.error.as_ref().map(|s| Error::other(s.clone())))
        {
            return Err(e);
        }

        let mut vcl_nals: Vec<Vec<u8>> = Vec::new();
        let mut got_params = false;

        for nal in annex_b_nals(&packet.data) {
            if nal.is_empty() {
                continue;
            }
            let nal_type = nal[0] & 0x1F;
            match nal_type {
                h264_nal::SPS => {
                    self.sps_list.clear();
                    self.sps_list.push(nal.to_vec());
                    got_params = true;
                }
                h264_nal::PPS => {
                    self.pps_list.clear();
                    self.pps_list.push(nal.to_vec());
                    got_params = true;
                }
                _ => vcl_nals.push(nal.to_vec()),
            }
        }

        if got_params {
            self.ensure_session()?;
        }

        if !vcl_nals.is_empty() && !self.session.is_null() {
            let vt = sys::vtable().map_err(|e| Error::unsupported(format!("videotoolbox: {e}")))?;
            let pts = packet.pts;
            let ctr = self.pts_counter;
            submit_nal_units(vt, self.session, self.fmt_desc, &vcl_nals, pts, ctr)?;
            self.pts_counter += 1;
            unsafe { (vt.vt_decomp_finish)(self.session) };
        }

        self.pull_frames();
        Ok(())
    }

    fn receive_frame(&mut self) -> Result<Frame> {
        if let Some(f) = self.output_queue.pop_front() {
            return Ok(Frame::Video(f));
        }
        Err(if self.flushed {
            Error::Eof
        } else {
            Error::NeedMore
        })
    }

    fn flush(&mut self) -> Result<()> {
        if !self.session.is_null() {
            if let Ok(vt) = sys::vtable() {
                unsafe { (vt.vt_decomp_finish)(self.session) };
            }
        }
        self.pull_frames();
        self.flushed = true;
        Ok(())
    }
}

// ─────────────────────────── HEVC decoder ─────────────────────────────────────

pub struct HevcVtDecoder {
    codec_id: CodecId,
    session: sys::VTDecompressionSessionRef,
    fmt_desc: sys::CMVideoFormatDescriptionRef,
    state: Arc<Mutex<CallbackState>>,
    vps_list: Vec<Vec<u8>>,
    sps_list: Vec<Vec<u8>>,
    pps_list: Vec<Vec<u8>>,
    output_queue: VecDeque<VideoFrame>,
    pts_counter: i64,
    flushed: bool,
}

unsafe impl Send for HevcVtDecoder {}

impl HevcVtDecoder {
    pub fn make(params: &CodecParameters) -> Result<Box<dyn oxideav_core::Decoder>> {
        sys::vtable().map_err(|e| Error::unsupported(format!("videotoolbox: {e}")))?;
        let _ = params;
        Ok(Box::new(HevcVtDecoder {
            codec_id: CodecId::new("hevc"),
            session: std::ptr::null_mut(),
            fmt_desc: std::ptr::null_mut(),
            state: CallbackState::new(),
            vps_list: Vec::new(),
            sps_list: Vec::new(),
            pps_list: Vec::new(),
            output_queue: VecDeque::new(),
            pts_counter: 0,
            flushed: false,
        }))
    }

    fn ensure_session(&mut self) -> Result<()> {
        if !self.session.is_null() {
            return Ok(());
        }
        if self.sps_list.is_empty() || self.pps_list.is_empty() {
            return Ok(());
        }

        let vt = sys::vtable().map_err(|e| Error::unsupported(format!("videotoolbox: {e}")))?;

        let param_ptrs: Vec<*const u8> = self
            .vps_list
            .iter()
            .chain(self.sps_list.iter())
            .chain(self.pps_list.iter())
            .map(|v| v.as_ptr())
            .collect();
        let param_sizes: Vec<usize> = self
            .vps_list
            .iter()
            .chain(self.sps_list.iter())
            .chain(self.pps_list.iter())
            .map(|v| v.len())
            .collect();

        let mut fmt_desc: sys::CMVideoFormatDescriptionRef = std::ptr::null_mut();
        let st = unsafe {
            (vt.cm_fmt_from_hevc_params)(
                std::ptr::null_mut(),
                param_ptrs.len(),
                param_ptrs.as_ptr(),
                param_sizes.as_ptr(),
                4,
                std::ptr::null_mut(),
                &mut fmt_desc,
            )
        };
        if st != K_OS_STATUS_NO_ERROR {
            return Err(Error::other(format!(
                "CMVideoFormatDescriptionCreateFromHEVCParameterSets: {st}"
            )));
        }

        let session = create_vt_session(vt, fmt_desc, &self.state)?;

        unsafe { (vt.cf_retain)(fmt_desc) };
        self.session = session;
        self.fmt_desc = fmt_desc;
        unsafe { (vt.cf_release)(fmt_desc) };

        Ok(())
    }

    fn pull_frames(&mut self) {
        if let Ok(mut g) = self.state.lock() {
            while let Some(f) = g.frames.pop_front() {
                self.output_queue.push_back(f);
            }
        }
    }
}

impl Drop for HevcVtDecoder {
    fn drop(&mut self) {
        if let Ok(vt) = sys::vtable() {
            if !self.session.is_null() {
                unsafe { (vt.vt_decomp_invalidate)(self.session) };
            }
            if !self.fmt_desc.is_null() {
                unsafe { (vt.cf_release)(self.fmt_desc) };
            }
        }
    }
}

impl oxideav_core::Decoder for HevcVtDecoder {
    fn codec_id(&self) -> &CodecId {
        &self.codec_id
    }

    fn send_packet(&mut self, packet: &Packet) -> Result<()> {
        self.flushed = false;

        if let Some(e) = self
            .state
            .lock()
            .ok()
            .and_then(|g| g.error.as_ref().map(|s| Error::other(s.clone())))
        {
            return Err(e);
        }

        let mut vcl_nals: Vec<Vec<u8>> = Vec::new();
        let mut got_params = false;

        for nal in annex_b_nals(&packet.data) {
            if nal.len() < 2 {
                continue;
            }
            let nal_type = (nal[0] >> 1) & 0x3F;
            match nal_type {
                hevc_nal::VPS => {
                    self.vps_list.clear();
                    self.vps_list.push(nal.to_vec());
                    got_params = true;
                }
                hevc_nal::SPS => {
                    self.sps_list.clear();
                    self.sps_list.push(nal.to_vec());
                    got_params = true;
                }
                hevc_nal::PPS => {
                    self.pps_list.clear();
                    self.pps_list.push(nal.to_vec());
                    got_params = true;
                }
                _ => vcl_nals.push(nal.to_vec()),
            }
        }

        if got_params {
            self.ensure_session()?;
        }

        if !vcl_nals.is_empty() && !self.session.is_null() {
            let vt = sys::vtable().map_err(|e| Error::unsupported(format!("videotoolbox: {e}")))?;
            let pts = packet.pts;
            let ctr = self.pts_counter;
            submit_nal_units(vt, self.session, self.fmt_desc, &vcl_nals, pts, ctr)?;
            self.pts_counter += 1;
            unsafe { (vt.vt_decomp_finish)(self.session) };
        }

        self.pull_frames();
        Ok(())
    }

    fn receive_frame(&mut self) -> Result<Frame> {
        if let Some(f) = self.output_queue.pop_front() {
            return Ok(Frame::Video(f));
        }
        Err(if self.flushed {
            Error::Eof
        } else {
            Error::NeedMore
        })
    }

    fn flush(&mut self) -> Result<()> {
        if !self.session.is_null() {
            if let Ok(vt) = sys::vtable() {
                unsafe { (vt.vt_decomp_finish)(self.session) };
            }
        }
        self.pull_frames();
        self.flushed = true;
        Ok(())
    }
}