hylarana_common/
macos.rs

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
use std::{
    fmt::Display,
    ptr::{null_mut, NonNull},
};

use core_foundation::kCFAllocatorDefault;
use core_media::{CMAudioFormatDescription, CMAudioFormatDescriptionGetStreamBasicDescription};
use core_metal::{MTLDevice as Objc2MTLDevice, MTLPixelFormat as Objc2MTLPixelFormat};
use core_video::{
    kCVPixelFormatType_32BGRA, kCVPixelFormatType_32RGBA,
    kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,
    kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, kCVPixelFormatType_420YpCbCr8Planar,
    kCVReturnSuccess, CVMetalTexture, CVMetalTextureCache, CVMetalTextureCacheCreate,
    CVMetalTextureCacheCreateTextureFromImage, CVMetalTextureCacheFlush, CVMetalTextureGetTexture,
    CVPixelBuffer, CVPixelBufferGetBaseAddressOfPlane, CVPixelBufferGetBytesPerRowOfPlane,
    CVPixelBufferGetHeight, CVPixelBufferGetPixelFormatType, CVPixelBufferGetWidth,
    CVPixelBufferLockBaseAddress, CVPixelBufferLockFlags, CVPixelBufferUnlockBaseAddress,
};

use objc2::{rc::Retained, runtime::ProtocolObject};

use crate::{frame::VideoFormat, Size};

pub use core_audo_types::AudioStreamBasicDescription;
pub use metal::{
    foreign_types::ForeignType, Device, MTLPixelFormat, MTLTexture, MTLTextureType, Texture,
    TextureRef,
};

pub type CVPixelBufferRef = *mut CVPixelBuffer;

#[derive(Debug)]
pub struct Error(i32);

impl std::error::Error for Error {}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "error code={}", self.0)
    }
}

impl From<i32> for Error {
    fn from(value: i32) -> Self {
        Self(value)
    }
}

#[allow(non_upper_case_globals)]
pub fn get_pixel_buffer_format(buffer: CVPixelBufferRef) -> VideoFormat {
    match unsafe { CVPixelBufferGetPixelFormatType(&*buffer) } {
        kCVPixelFormatType_32RGBA => VideoFormat::RGBA,
        kCVPixelFormatType_32BGRA => VideoFormat::BGRA,
        kCVPixelFormatType_420YpCbCr8Planar => VideoFormat::I420,
        kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
        | kCVPixelFormatType_420YpCbCr8BiPlanarFullRange => VideoFormat::NV12,
        format => unimplemented!("unsupports format = {:?}", format),
    }
}

pub fn get_pixel_buffer_size(buffer: CVPixelBufferRef) -> Size {
    Size {
        width: unsafe { CVPixelBufferGetWidth(&*buffer) } as u32,
        height: unsafe { CVPixelBufferGetHeight(&*buffer) } as u32,
    }
}

pub fn get_format_description_info<'a>(
    descr: *const CMAudioFormatDescription,
) -> Option<&'a AudioStreamBasicDescription> {
    let ptr = unsafe { CMAudioFormatDescriptionGetStreamBasicDescription(&*descr) };

    if ptr.is_null() {
        None
    } else {
        Some(unsafe { &*ptr })
    }
}

pub struct PixelMomeryBuffer<'a> {
    pub size: Size,
    pub format: VideoFormat,
    pub data: [&'a [u8]; 3],
    pub linesize: [usize; 3],
    buffer: CVPixelBufferRef,
}

impl<'a> PixelMomeryBuffer<'a> {
    pub fn as_ref(&self) -> CVPixelBufferRef {
        self.buffer
    }
}

impl<'a> From<(CVPixelBufferRef, VideoFormat, Size)> for PixelMomeryBuffer<'a> {
    fn from((buffer, format, size): (CVPixelBufferRef, VideoFormat, Size)) -> Self {
        unsafe {
            CVPixelBufferLockBaseAddress(&*buffer, CVPixelBufferLockFlags::ReadOnly);
        }

        let mut this = Self {
            size,
            format,
            buffer,
            data: [&[]; 3],
            linesize: [0; 3],
        };

        for i in 0..3 {
            this.linesize[i] = unsafe { CVPixelBufferGetBytesPerRowOfPlane(&*buffer, i) };
            this.data[i] = unsafe {
                std::slice::from_raw_parts(
                    CVPixelBufferGetBaseAddressOfPlane(&*buffer, i) as *const _,
                    this.linesize[i]
                        * if format == VideoFormat::I420 {
                            size.height / 2
                        } else {
                            size.height
                        } as usize,
                )
            };
        }

        this
    }
}

impl<'a> From<CVPixelBufferRef> for PixelMomeryBuffer<'a> {
    fn from(buffer: CVPixelBufferRef) -> Self {
        Self::from((
            buffer,
            get_pixel_buffer_format(buffer),
            get_pixel_buffer_size(buffer),
        ))
    }
}

impl<'a> Drop for PixelMomeryBuffer<'a> {
    fn drop(&mut self) {
        unsafe {
            CVPixelBufferUnlockBaseAddress(&*self.buffer, CVPixelBufferLockFlags::ReadOnly);
        }
    }
}

#[derive(Clone, Copy)]
pub struct PixelBuffer {
    buffer: CVPixelBufferRef,
    pub format: VideoFormat,
    pub size: Size,
}

impl PixelBuffer {
    pub fn as_ref(&self) -> &CVPixelBuffer {
        unsafe { &*self.buffer }
    }

    pub fn as_raw(&self) -> CVPixelBufferRef {
        self.buffer
    }
}

impl From<CVPixelBufferRef> for PixelBuffer {
    fn from(buffer: CVPixelBufferRef) -> Self {
        Self::from((
            buffer,
            get_pixel_buffer_format(buffer),
            get_pixel_buffer_size(buffer),
        ))
    }
}

impl From<(CVPixelBufferRef, VideoFormat, Size)> for PixelBuffer {
    fn from((buffer, format, size): (CVPixelBufferRef, VideoFormat, Size)) -> Self {
        Self {
            buffer,
            format,
            size,
        }
    }
}

pub struct MetalTextureCache(Retained<CVMetalTextureCache>);

impl MetalTextureCache {
    pub fn new(device: Device) -> Result<Self, Error> {
        let device: Retained<ProtocolObject<dyn Objc2MTLDevice>> =
            unsafe { Retained::from_raw(device.into_ptr().cast()).unwrap() };

        let mut cache = null_mut();
        let code = unsafe {
            CVMetalTextureCacheCreate(
                kCFAllocatorDefault,
                None,
                device.as_ref(),
                None,
                NonNull::new(&mut cache).unwrap(),
            )
        };

        if code != kCVReturnSuccess || cache.is_null() {
            return Err(Error(code));
        }

        Ok(Self(unsafe { Retained::from_raw(cache).unwrap() }))
    }

    pub fn map(&self, buffer: PixelBuffer) -> Result<MetalTexture, Error> {
        let Size { width, height } = buffer.size;

        let mut texture = null_mut();
        let code = unsafe {
            CVMetalTextureCacheCreateTextureFromImage(
                kCFAllocatorDefault,
                &self.0,
                buffer.as_ref(),
                None,
                match buffer.format {
                    VideoFormat::BGRA => Objc2MTLPixelFormat::BGRA8Unorm,
                    VideoFormat::RGBA => Objc2MTLPixelFormat::RGBA8Unorm,
                    _ => unimplemented!("unsupports format = {:?}", buffer.format),
                },
                width as usize,
                height as usize,
                0,
                NonNull::new(&mut texture).unwrap(),
            )
        };

        if code != kCVReturnSuccess || texture.is_null() {
            return Err(Error(code));
        }

        Ok(MetalTexture(unsafe {
            Retained::from_raw(texture).unwrap()
        }))
    }

    pub fn flush(&self) {
        unsafe {
            CVMetalTextureCacheFlush(&self.0, 0);
        }
    }
}

pub struct MetalTexture(Retained<CVMetalTexture>);

impl MetalTexture {
    pub fn get_texture(&mut self) -> Result<Texture, Error> {
        if let Some(texture) = unsafe { CVMetalTextureGetTexture(&self.0) } {
            Ok(unsafe { Texture::from_ptr(Retained::into_raw(texture).cast()).to_owned() })
        } else {
            Err(Error(-1))
        }
    }
}