lumen-engine-ffmpeg 0.2.2

FFmpeg integration for media decode, encode, muxing, and GPU interop in Lumen.
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
use std::ptr::NonNull;

use objc2::rc::Retained;
use objc2_core_foundation::{CFBoolean, CFDictionary, CFNumber, CFRetained, CFType};
use objc2_core_video::{
    CVMetalTexture, CVMetalTextureCache, CVMetalTextureGetTexture, CVPixelBuffer,
    CVPixelBufferGetHeight, CVPixelBufferGetHeightOfPlane, CVPixelBufferGetPixelFormatType,
    CVPixelBufferGetPlaneCount, CVPixelBufferGetWidth, CVPixelBufferGetWidthOfPlane,
    CVPixelBufferPool, kCVPixelBufferHeightKey, kCVPixelBufferIOSurfacePropertiesKey,
    kCVPixelBufferMetalCompatibilityKey, kCVPixelBufferPixelFormatTypeKey, kCVPixelBufferWidthKey,
    kCVPixelFormatType_32BGRA, kCVPixelFormatType_32RGBA, kCVReturnSuccess,
};

use crate::{FfmpegError, Result, gpu::GpuBackend};

pub type Objc2MetalTexture = objc2::runtime::ProtocolObject<dyn objc2_metal::MTLTexture>;
pub type Objc2MetalDevice = objc2::runtime::ProtocolObject<dyn objc2_metal::MTLDevice>;

pub struct MetalTextureCache {
    inner: CFRetained<CVMetalTextureCache>,
}

#[cfg(feature = "metal")]
impl std::fmt::Debug for MetalTextureCache {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("MetalTextureCache")
            .finish_non_exhaustive()
    }
}

#[cfg(feature = "metal")]
impl MetalTextureCache {
    pub fn create(device: &Objc2MetalDevice) -> Result<Self> {
        let mut cache = std::ptr::null_mut();
        let status = unsafe {
            CVMetalTextureCache::create(
                None,
                None,
                device,
                None,
                NonNull::new_unchecked(&mut cache),
            )
        };
        if status != kCVReturnSuccess {
            return Err(FfmpegError::new(
                "CVMetalTextureCacheCreate",
                format!("CoreVideo returned {status}"),
            )
            .with_backend(GpuBackend::Metal));
        }
        let cache = NonNull::new(cache).ok_or_else(|| {
            FfmpegError::new(
                "CVMetalTextureCacheCreate",
                "CoreVideo returned a null cache",
            )
            .with_backend(GpuBackend::Metal)
        })?;
        Ok(Self {
            inner: unsafe { CFRetained::from_raw(cache) },
        })
    }

    pub fn flush(&self) {
        self.inner.flush(0);
    }
}

#[cfg(feature = "metal")]
pub struct MetalPixelBufferPool {
    inner: CFRetained<CVPixelBufferPool>,
    width: u32,
    height: u32,
    pixel_format: u32,
}

#[cfg(feature = "metal")]
impl std::fmt::Debug for MetalPixelBufferPool {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("MetalPixelBufferPool")
            .field("width", &self.width)
            .field("height", &self.height)
            .field("pixel_format", &self.pixel_format)
            .finish_non_exhaustive()
    }
}

#[cfg(feature = "metal")]
impl MetalPixelBufferPool {
    pub fn rgba8(width: u32, height: u32) -> Result<Self> {
        Self::create(width, height, kCVPixelFormatType_32RGBA)
    }

    pub fn bgra8(width: u32, height: u32) -> Result<Self> {
        Self::create(width, height, kCVPixelFormatType_32BGRA)
    }

    pub fn create(width: u32, height: u32, pixel_format: u32) -> Result<Self> {
        if width == 0 || height == 0 {
            return Err(FfmpegError::new(
                "CVPixelBufferPoolCreate",
                "width and height must be greater than zero",
            )
            .with_backend(GpuBackend::Metal));
        }

        let width_value = CFNumber::new_i32(width.min(i32::MAX as u32) as i32);
        let height_value = CFNumber::new_i32(height.min(i32::MAX as u32) as i32);
        let pixel_format_value = CFNumber::new_i32(pixel_format as i32);
        let metal_compatible = CFBoolean::new(true);
        let io_surface_properties = CFDictionary::<CFType, CFType>::empty();
        let keys: [&CFType; 5] = unsafe {
            [
                kCVPixelBufferWidthKey.as_ref(),
                kCVPixelBufferHeightKey.as_ref(),
                kCVPixelBufferPixelFormatTypeKey.as_ref(),
                kCVPixelBufferMetalCompatibilityKey.as_ref(),
                kCVPixelBufferIOSurfacePropertiesKey.as_ref(),
            ]
        };
        let values: [&CFType; 5] = [
            width_value.as_ref(),
            height_value.as_ref(),
            pixel_format_value.as_ref(),
            metal_compatible.as_ref(),
            io_surface_properties.as_ref(),
        ];
        let attributes = CFDictionary::<CFType, CFType>::from_slices(&keys, &values);

        let mut pool: *mut CVPixelBufferPool = std::ptr::null_mut();
        let status = unsafe {
            CVPixelBufferPool::create(
                None,
                None,
                Some(attributes.as_ref()),
                NonNull::new_unchecked(&mut pool),
            )
        };
        if status != kCVReturnSuccess {
            return Err(FfmpegError::new(
                "CVPixelBufferPoolCreate",
                format!("CoreVideo returned {status}"),
            )
            .with_backend(GpuBackend::Metal));
        }
        let pool = NonNull::new(pool).ok_or_else(|| {
            FfmpegError::new("CVPixelBufferPoolCreate", "CoreVideo returned a null pool")
                .with_backend(GpuBackend::Metal)
        })?;
        Ok(Self {
            inner: unsafe { CFRetained::from_raw(pool) },
            width,
            height,
            pixel_format,
        })
    }

    pub fn create_frame(&self, pts: Option<i64>) -> Result<MetalPixelBufferFrame> {
        let mut pixel_buffer: *mut CVPixelBuffer = std::ptr::null_mut();
        let status = unsafe {
            CVPixelBufferPool::create_pixel_buffer(
                None,
                &self.inner,
                NonNull::new_unchecked(&mut pixel_buffer),
            )
        };
        if status != kCVReturnSuccess {
            return Err(FfmpegError::new(
                "CVPixelBufferPoolCreatePixelBuffer",
                format!("CoreVideo returned {status}"),
            )
            .with_backend(GpuBackend::Metal));
        }
        let pixel_buffer = NonNull::new(pixel_buffer).ok_or_else(|| {
            FfmpegError::new(
                "CVPixelBufferPoolCreatePixelBuffer",
                "CoreVideo returned a null pixel buffer",
            )
            .with_backend(GpuBackend::Metal)
        })?;
        Ok(unsafe { MetalPixelBufferFrame::from_retained_pixel_buffer(pixel_buffer, pts) })
    }

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

    pub fn pixel_format(&self) -> u32 {
        self.pixel_format
    }
}

#[cfg(feature = "metal")]
pub struct MetalPixelBufferFrame {
    pixel_buffer: CFRetained<CVPixelBuffer>,
    width: u32,
    height: u32,
    pixel_format: u32,
    pts: Option<i64>,
}

#[cfg(feature = "metal")]
impl std::fmt::Debug for MetalPixelBufferFrame {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("MetalPixelBufferFrame")
            .field("width", &self.width)
            .field("height", &self.height)
            .field("pixel_format", &self.pixel_format)
            .field("pts", &self.pts)
            .finish_non_exhaustive()
    }
}

#[cfg(feature = "metal")]
impl MetalPixelBufferFrame {
    /// Retains a Core Video pixel buffer for encoding.
    ///
    /// # Safety
    ///
    /// `pixel_buffer` must be a valid `CVPixelBuffer` pointer.
    pub unsafe fn retain_pixel_buffer(
        pixel_buffer: NonNull<CVPixelBuffer>,
        pts: Option<i64>,
    ) -> Self {
        let pixel_buffer = unsafe { CFRetained::retain(pixel_buffer) };
        Self::from_pixel_buffer(pixel_buffer, pts)
    }

    unsafe fn from_retained_pixel_buffer(
        pixel_buffer: NonNull<CVPixelBuffer>,
        pts: Option<i64>,
    ) -> Self {
        let pixel_buffer = unsafe { CFRetained::from_raw(pixel_buffer) };
        Self::from_pixel_buffer(pixel_buffer, pts)
    }

    fn from_pixel_buffer(pixel_buffer: CFRetained<CVPixelBuffer>, pts: Option<i64>) -> Self {
        let width = CVPixelBufferGetWidth(&pixel_buffer).min(u32::MAX as usize) as u32;
        let height = CVPixelBufferGetHeight(&pixel_buffer).min(u32::MAX as usize) as u32;
        let pixel_format = CVPixelBufferGetPixelFormatType(&pixel_buffer);
        Self {
            pixel_buffer,
            width,
            height,
            pixel_format,
            pts,
        }
    }

    pub fn pixel_buffer(&self) -> &CVPixelBuffer {
        &self.pixel_buffer
    }

    pub fn pixel_buffer_ptr(&self) -> NonNull<CVPixelBuffer> {
        CFRetained::as_ptr(&self.pixel_buffer)
    }

    pub fn pixel_format(&self) -> u32 {
        self.pixel_format
    }

    pub fn plane_count(&self) -> usize {
        CVPixelBufferGetPlaneCount(&self.pixel_buffer)
    }

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

    pub fn pts(&self) -> Option<i64> {
        self.pts
    }

    pub fn create_texture(
        &self,
        cache: &MetalTextureCache,
        pixel_format: objc2_metal::MTLPixelFormat,
        plane_index: usize,
    ) -> Result<Retained<Objc2MetalTexture>> {
        create_metal_texture_from_pixel_buffer(
            &self.pixel_buffer,
            self.width,
            self.height,
            self.plane_count(),
            cache,
            pixel_format,
            plane_index,
        )
    }
}

#[cfg(feature = "metal")]
pub struct MetalDecodedFrame {
    pixel_buffer: CFRetained<CVPixelBuffer>,
    width: u32,
    height: u32,
    pixel_format: u32,
    pts: Option<i64>,
}

// The retained CoreVideo pixel buffer owns the decoded image storage. Consumers create Metal
// textures from it on the active render thread before use.
#[cfg(feature = "metal")]
unsafe impl Send for MetalDecodedFrame {}
#[cfg(feature = "metal")]
unsafe impl Sync for MetalDecodedFrame {}

#[cfg(feature = "metal")]
impl std::fmt::Debug for MetalDecodedFrame {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("MetalDecodedFrame")
            .field("width", &self.width)
            .field("height", &self.height)
            .field("pixel_format", &self.pixel_format)
            .field("pts", &self.pts)
            .finish_non_exhaustive()
    }
}

#[cfg(feature = "metal")]
impl MetalDecodedFrame {
    pub(crate) unsafe fn retain_from_video_toolbox_frame(
        pixel_buffer: NonNull<CVPixelBuffer>,
        pts: Option<i64>,
    ) -> Self {
        let pixel_buffer = unsafe { CFRetained::retain(pixel_buffer) };
        let width = CVPixelBufferGetWidth(&pixel_buffer).min(u32::MAX as usize) as u32;
        let height = CVPixelBufferGetHeight(&pixel_buffer).min(u32::MAX as usize) as u32;
        let pixel_format = CVPixelBufferGetPixelFormatType(&pixel_buffer);
        Self {
            pixel_buffer,
            width,
            height,
            pixel_format,
            pts,
        }
    }

    pub fn pixel_buffer(&self) -> &CVPixelBuffer {
        &self.pixel_buffer
    }

    pub fn pixel_format(&self) -> u32 {
        self.pixel_format
    }

    pub fn plane_count(&self) -> usize {
        CVPixelBufferGetPlaneCount(&self.pixel_buffer)
    }

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

    pub fn pts(&self) -> Option<i64> {
        self.pts
    }

    pub fn create_texture(
        &self,
        cache: &MetalTextureCache,
        pixel_format: objc2_metal::MTLPixelFormat,
        plane_index: usize,
    ) -> Result<Retained<Objc2MetalTexture>> {
        create_metal_texture_from_pixel_buffer(
            &self.pixel_buffer,
            self.width,
            self.height,
            self.plane_count(),
            cache,
            pixel_format,
            plane_index,
        )
    }
}

#[cfg(feature = "metal")]
fn create_metal_texture_from_pixel_buffer(
    pixel_buffer: &CVPixelBuffer,
    width: u32,
    height: u32,
    plane_count: usize,
    cache: &MetalTextureCache,
    pixel_format: objc2_metal::MTLPixelFormat,
    plane_index: usize,
) -> Result<Retained<Objc2MetalTexture>> {
    let (width, height) = if plane_count == 0 {
        (width as usize, height as usize)
    } else {
        (
            CVPixelBufferGetWidthOfPlane(pixel_buffer, plane_index),
            CVPixelBufferGetHeightOfPlane(pixel_buffer, plane_index),
        )
    };
    let mut cv_texture: *mut CVMetalTexture = std::ptr::null_mut();
    let status = unsafe {
        CVMetalTextureCache::create_texture_from_image(
            None,
            &cache.inner,
            pixel_buffer,
            None,
            pixel_format,
            width,
            height,
            plane_index,
            NonNull::new_unchecked(&mut cv_texture),
        )
    };
    if status != kCVReturnSuccess {
        return Err(FfmpegError::new(
            "CVMetalTextureCacheCreateTextureFromImage",
            format!("CoreVideo returned {status}"),
        )
        .with_backend(GpuBackend::Metal));
    }
    let cv_texture = NonNull::new(cv_texture).ok_or_else(|| {
        FfmpegError::new(
            "CVMetalTextureCacheCreateTextureFromImage",
            "CoreVideo returned a null texture",
        )
        .with_backend(GpuBackend::Metal)
    })?;
    let cv_texture = unsafe { CFRetained::from_raw(cv_texture) };
    CVMetalTextureGetTexture(&cv_texture).ok_or_else(|| {
        FfmpegError::new(
            "CVMetalTextureGetTexture",
            "CoreVideo returned a null MTLTexture",
        )
        .with_backend(GpuBackend::Metal)
    })
}