videotoolbox 0.21.0

Safe Rust bindings for Apple's VideoToolbox framework — hardware H.264/HEVC/ProRes encode and decode on macOS
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
//! Async API for `videotoolbox`.
//!
//! Provides executor-agnostic wrappers around the crate's callback-shaped APIs.
//! Enable with the `async` cargo feature.
//!
//! ## Available types
//!
//! | Type | Wrapped surface |
//! |------|-----------------|
//! | [`AsyncCompressionSession`] | single-frame `VTCompressionSession` completion |
//! | [`AsyncDecompressionSession`] | single-frame `VTDecompressionSession` completion |
//! | [`AsyncRawProcessingSession`] | `VTRAWProcessingSession` async processing plus parameter-change stream subscription |
//! | [`RawProcessingParameterChangeStream`] | `VTRAWProcessingSessionSetParameterChangedHandler` |
//!
//! ## Tier-2 deferrals
//!
//! The following callback-shaped APIs are intentionally **not** exposed as
//! async streams here:
//!
//! * [`crate::DecompressionSession::set_multi_image_callback`] — the audited C
//!   signature requires a **non-null** callback and does not provide a clear /
//!   unsubscribe entry point, so an RAII stream wrapper would have to install a
//!   hidden no-op callback on drop.
//! * [`crate::FrameSilo::sample_buffers`] — synchronous collection helper.
//! * [`crate::MultiPassStorage::close`] and time-range configuration APIs —
//!   synchronous control surfaces.
//!
//! ## Example
//!
//! ```rust,no_run
//! use apple_cf::cm::CMTime;
//! use apple_cf::cv::CVPixelBuffer;
//! use videotoolbox::{Codec, CompressionSession, async_api::AsyncCompressionSession};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! pollster::block_on(async {
//!     let session = CompressionSession::builder(64, 64, Codec::H264)
//!         .with_real_time(true)
//!         .with_average_bit_rate(500_000)
//!         .with_expected_frame_rate(30.0)
//!         .with_max_keyframe_interval(1)
//!         .build()?;
//!     let frame = CVPixelBuffer::create(64, 64, u32::from_be_bytes(*b"BGRA"))
//!         .unwrap_or_else(|status| panic!("CVPixelBuffer::create failed: {status}"));
//!     let sample_buffer = AsyncCompressionSession::new(&session)
//!         .encode_frame(frame, CMTime::new(0, 30), CMTime::INVALID, None)
//!         .await?;
//!     assert!(sample_buffer.is_valid());
//!     Ok::<_, videotoolbox::VTError>(())
//! })?;
//! # Ok(())
//! # }
//! ```

#![cfg(feature = "async")]

use core::future::Future;

use apple_cf::cm::CMSampleBuffer;
use apple_cf::cv::CVImageBuffer;

#[cfg(any(feature = "compression", feature = "frame_processor"))]
use apple_cf::cv::CVPixelBuffer;
#[cfg(feature = "compression")]
use apple_cf::{cf::CFDictionary, cm::CMTime};
#[cfg(feature = "frame_processor")]
use doom_fish_utils::{
    panic_safe::catch_user_panic,
    stream::{BoundedAsyncStream, NextItem},
};

#[cfg(feature = "compression")]
use crate::compression::CompressionSession;
#[cfg(feature = "frame_processor")]
use crate::raw_processing::{RawProcessingParameter, RawProcessingSession};
use crate::{DecompressionSession, VTError};

/// Async accessor for [`CompressionSession`].
///
/// Wraps the encoder's one-shot frame completion callback as a future that can
/// be awaited on any executor.
#[cfg(feature = "compression")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "async", feature = "compression"))))]
pub struct AsyncCompressionSession<'a> {
    session: &'a CompressionSession,
}

#[cfg(feature = "compression")]
impl core::fmt::Debug for AsyncCompressionSession<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("AsyncCompressionSession")
            .finish_non_exhaustive()
    }
}

#[cfg(feature = "compression")]
impl<'a> AsyncCompressionSession<'a> {
    /// Wrap a borrowed [`CompressionSession`].
    #[must_use]
    pub const fn new(session: &'a CompressionSession) -> Self {
        Self { session }
    }

    /// Submit `image_buffer` for encoding and await the encoded sample buffer.
    ///
    /// # Errors
    ///
    /// Returns [`VTError::EncodeFailed`] if the frame submission is rejected or
    /// [`VTError::EncoderCallback`] if the encoder callback reports a failure or
    /// drops the frame without a `CMSampleBuffer`.
    #[must_use = "futures do nothing unless awaited"]
    #[allow(clippy::future_not_send)]
    pub fn encode_frame(
        &self,
        image_buffer: CVPixelBuffer,
        presentation_timestamp: CMTime,
        duration: CMTime,
        frame_properties: Option<CFDictionary>,
    ) -> impl Future<Output = Result<CMSampleBuffer, VTError>> + '_ {
        self.session.encode_frame_async(
            image_buffer,
            presentation_timestamp,
            duration,
            frame_properties,
        )
    }
}

#[cfg(feature = "compression")]
impl<'a> From<&'a CompressionSession> for AsyncCompressionSession<'a> {
    fn from(session: &'a CompressionSession) -> Self {
        Self::new(session)
    }
}

/// Async accessor for [`DecompressionSession`].
///
/// Wraps the decoder's one-shot frame completion callback as a future that can
/// be awaited on any executor.
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
pub struct AsyncDecompressionSession<'a> {
    session: &'a DecompressionSession,
}

impl core::fmt::Debug for AsyncDecompressionSession<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("AsyncDecompressionSession")
            .finish_non_exhaustive()
    }
}

impl<'a> AsyncDecompressionSession<'a> {
    /// Wrap a borrowed [`DecompressionSession`].
    #[must_use]
    pub const fn new(session: &'a DecompressionSession) -> Self {
        Self { session }
    }

    /// Submit `sample_buffer` for decoding and await the decoded image buffer.
    ///
    /// # Errors
    ///
    /// Returns [`VTError::UnexpectedSampleCount`] unless `sample_buffer`
    /// contains exactly one sample. Returns [`VTError::EncoderCallback`] if the
    /// decoder rejects the frame, reports an asynchronous failure, or completes
    /// without an image buffer.
    #[must_use = "futures do nothing unless awaited"]
    #[allow(clippy::future_not_send)]
    pub fn decode_frame(
        &self,
        sample_buffer: CMSampleBuffer,
        frame_flags: crate::ffi::VTDecodeFrameFlags,
    ) -> impl Future<Output = Result<CVImageBuffer, VTError>> + '_ {
        self.session.decode_frame_async(sample_buffer, frame_flags)
    }
}

impl<'a> From<&'a DecompressionSession> for AsyncDecompressionSession<'a> {
    fn from(session: &'a DecompressionSession) -> Self {
        Self::new(session)
    }
}

/// Async accessor for [`RawProcessingSession`].
#[cfg(feature = "frame_processor")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "async", feature = "frame_processor"))))]
pub struct AsyncRawProcessingSession<'a> {
    session: &'a RawProcessingSession,
}

#[cfg(feature = "frame_processor")]
impl core::fmt::Debug for AsyncRawProcessingSession<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("AsyncRawProcessingSession")
            .finish_non_exhaustive()
    }
}

#[cfg(feature = "frame_processor")]
impl<'a> AsyncRawProcessingSession<'a> {
    /// Wrap a borrowed [`RawProcessingSession`].
    #[must_use]
    pub const fn new(session: &'a RawProcessingSession) -> Self {
        Self { session }
    }

    /// Submit `input_pixel_buffer` for RAW processing and await the processed output.
    #[must_use = "futures do nothing unless awaited"]
    #[allow(clippy::future_not_send)]
    pub fn process_frame(
        &self,
        input_pixel_buffer: CVPixelBuffer,
    ) -> impl Future<Output = Result<CVPixelBuffer, VTError>> + '_ {
        self.session.process_frame_async(input_pixel_buffer)
    }

    /// Subscribe to RAW-parameter changes as a bounded async stream.
    ///
    /// Installing this stream replaces any existing parameter-change handler on
    /// the underlying session for the lifetime of the returned guard.
    ///
    /// # Errors
    ///
    /// Returns [`VTError::ApiFailed`] when `VideoToolbox` rejects the handler
    /// install (for example on runtimes where the callback API is unavailable).
    pub fn parameter_changes(
        &self,
        capacity: usize,
    ) -> Result<RawProcessingParameterChangeStream<'a>, VTError> {
        RawProcessingParameterChangeStream::subscribe(self.session, capacity)
    }
}

#[cfg(feature = "frame_processor")]
impl<'a> From<&'a RawProcessingSession> for AsyncRawProcessingSession<'a> {
    fn from(session: &'a RawProcessingSession) -> Self {
        Self::new(session)
    }
}

/// Bounded async stream of updated RAW-processing parameter snapshots.
#[cfg(feature = "frame_processor")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "async", feature = "frame_processor"))))]
pub struct RawProcessingParameterChangeStream<'a> {
    inner: BoundedAsyncStream<Vec<RawProcessingParameter>>,
    session: &'a RawProcessingSession,
}

#[cfg(feature = "frame_processor")]
impl RawProcessingParameterChangeStream<'_> {
    fn subscribe(
        session: &RawProcessingSession,
        capacity: usize,
    ) -> Result<RawProcessingParameterChangeStream<'_>, VTError> {
        let (stream, sender) = BoundedAsyncStream::new(capacity);
        session.set_parameter_changed_handler(move |parameters| {
            catch_user_panic(
                "videotoolbox::async_api::raw_processing_parameter_changes",
                || {
                    sender.push(parameters);
                },
            );
        })?;

        Ok(RawProcessingParameterChangeStream {
            inner: stream,
            session,
        })
    }

    /// Await the next buffered parameter-change notification.
    #[must_use]
    pub const fn next(&self) -> NextItem<'_, Vec<RawProcessingParameter>> {
        self.inner.next()
    }

    /// Try to pop the next buffered parameter-change notification without waiting.
    #[must_use]
    pub fn try_next(&self) -> Option<Vec<RawProcessingParameter>> {
        self.inner.try_next()
    }

    /// Number of buffered parameter-change notifications.
    #[must_use]
    pub fn buffered_count(&self) -> usize {
        self.inner.buffered_count()
    }
}

#[cfg(feature = "frame_processor")]
impl Drop for RawProcessingParameterChangeStream<'_> {
    fn drop(&mut self) {
        let _ = self.session.clear_parameter_changed_handler();
    }
}

#[cfg(feature = "frame_processor")]
impl core::fmt::Debug for RawProcessingParameterChangeStream<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("RawProcessingParameterChangeStream")
            .field("buffered_count", &self.buffered_count())
            .finish_non_exhaustive()
    }
}

#[cfg(all(test, feature = "frame_processor"))]
mod tests {
    use apple_cf::{cm::CMFormatDescription, cv::CVPixelBuffer};

    use super::AsyncRawProcessingSession;
    use crate::{copy_raw_processor_extension_properties, RawProcessingSession, VTError};

    const BGRA: u32 = u32::from_be_bytes(*b"BGRA");

    #[test]
    fn async_raw_processing_process_frame_matches_existing_async_method_when_supported(
    ) -> Result<(), VTError> {
        pollster::block_on(async {
            let input = make_test_pixel_buffer(16, 16);
            let Ok(format) = make_video_format_description(&input) else {
                return Ok(());
            };

            if copy_raw_processor_extension_properties(&format).is_err() {
                return Ok(());
            }

            let session = match RawProcessingSession::new(&format) {
                Ok(session) => session,
                Err(VTError::SessionCreateFailed(_)) => return Ok(()),
                Err(error) => return Err(error),
            };

            let processed = match AsyncRawProcessingSession::new(&session)
                .process_frame(input)
                .await
            {
                Ok(processed) => processed,
                Err(VTError::EncodeFailed(_)) => return Ok(()),
                Err(error) => return Err(error),
            };

            assert_eq!(processed.width(), 16);
            assert_eq!(processed.height(), 16);
            Ok(())
        })
    }

    #[test]
    fn async_raw_processing_parameter_changes_constructs_stream_or_reports_runtime_gap(
    ) -> Result<(), VTError> {
        let input = make_test_pixel_buffer(16, 16);
        let Ok(format) = make_video_format_description(&input) else {
            return Ok(());
        };

        if copy_raw_processor_extension_properties(&format).is_err() {
            return Ok(());
        }

        let session = match RawProcessingSession::new(&format) {
            Ok(session) => session,
            Err(VTError::SessionCreateFailed(_)) => return Ok(()),
            Err(error) => return Err(error),
        };

        let result = match AsyncRawProcessingSession::new(&session).parameter_changes(4) {
            Ok(stream) => {
                assert_eq!(stream.buffered_count(), 0);
                assert!(stream.try_next().is_none());
                Ok(())
            }
            Err(VTError::ApiFailed { api, status }) => {
                assert_eq!(api, "VTRAWProcessingSessionSetParameterChangedHandler");
                assert_ne!(status, 0);
                Ok(())
            }
            Err(error) => Err(error),
        };
        result
    }

    fn make_test_pixel_buffer(width: usize, height: usize) -> CVPixelBuffer {
        CVPixelBuffer::create(width, height, BGRA)
            .unwrap_or_else(|status| panic!("CVPixelBuffer::create failed: {status}"))
    }

    fn make_video_format_description(
        pixel_buffer: &CVPixelBuffer,
    ) -> Result<CMFormatDescription, i32> {
        let mut description: apple_cf::raw::CMVideoFormatDescriptionRef = core::ptr::null();
        let status = unsafe {
            apple_cf::raw::CMVideoFormatDescriptionCreateForImageBuffer(
                apple_cf::raw::kCFAllocatorDefault,
                pixel_buffer.as_ptr().cast(),
                &raw mut description,
            )
        };
        if status == 0 && !description.is_null() {
            unsafe { CMFormatDescription::from_raw(description.cast_mut().cast()) }.ok_or(status)
        } else {
            Err(status)
        }
    }
}