Skip to main content

core_video_sys/
pixel_buffer.rs

1use core_foundation_sys::{
2    array::CFArrayRef,
3    base::{Boolean, CFAllocatorRef, CFTypeID},
4    dictionary::CFDictionaryRef,
5    string::CFStringRef,
6};
7use libc::{c_void, size_t};
8
9use crate::{base::CVOptionFlags, image_buffer::CVImageBufferRef, return_::CVReturn, OSType};
10
11const fn as_u32_be(array: &[u8; 4]) -> u32 {
12    ((array[0] as u32) << 24)
13        + ((array[1] as u32) << 16)
14        + ((array[2] as u32) << 8)
15        + ((array[3] as u32) << 0)
16}
17
18pub type CVPixelBufferLockFlags = u64;
19pub type CVPixelBufferRef = CVImageBufferRef;
20
21pub const kCVPixelFormatType_1Monochrome: OSType = 0x00000001; /* 1 bit indexed */
22pub const kCVPixelFormatType_2Indexed: OSType = 0x00000002; /* 2 bit indexed */
23pub const kCVPixelFormatType_4Indexed: OSType = 0x00000004; /* 4 bit indexed */
24pub const kCVPixelFormatType_8Indexed: OSType = 0x00000008; /* 8 bit indexed */
25pub const kCVPixelFormatType_1IndexedGray_WhiteIsZero: OSType = 0x00000021; /* 1 bit indexed gray, white is zero */
26pub const kCVPixelFormatType_2IndexedGray_WhiteIsZero: OSType = 0x00000022; /* 2 bit indexed gray, white is zero */
27pub const kCVPixelFormatType_4IndexedGray_WhiteIsZero: OSType = 0x00000024; /* 4 bit indexed gray, white is zero */
28pub const kCVPixelFormatType_8IndexedGray_WhiteIsZero: OSType = 0x00000028; /* 8 bit indexed gray, white is zero */
29pub const kCVPixelFormatType_16BE555: OSType = 0x00000010; /* 16 bit BE RGB 555 */
30pub const kCVPixelFormatType_16LE555: OSType = as_u32_be(b"L555"); /* 16 bit LE RGB 555 */
31pub const kCVPixelFormatType_16LE5551: OSType = as_u32_be(b"5551"); /* 16 bit LE RGB 5551 */
32pub const kCVPixelFormatType_16BE565: OSType = as_u32_be(b"B565"); /* 16 bit BE RGB 565 */
33pub const kCVPixelFormatType_16LE565: OSType = as_u32_be(b"L565"); /* 16 bit LE RGB 565 */
34pub const kCVPixelFormatType_24RGB: OSType = 0x00000018; /* 24 bit RGB */
35pub const kCVPixelFormatType_24BGR: OSType = as_u32_be(b"24BG"); /* 24 bit BGR */
36pub const kCVPixelFormatType_32ARGB: OSType = 0x00000020; /* 32 bit ARGB */
37pub const kCVPixelFormatType_32BGRA: OSType = as_u32_be(b"BGRA"); /* 32 bit BGRA */
38pub const kCVPixelFormatType_32ABGR: OSType = as_u32_be(b"ABGR"); /* 32 bit ABGR */
39pub const kCVPixelFormatType_32RGBA: OSType = as_u32_be(b"RGBA"); /* 32 bit RGBA */
40pub const kCVPixelFormatType_64ARGB: OSType = as_u32_be(b"b64a"); /* 64 bit ARGB, 16-bit big-endian samples */
41pub const kCVPixelFormatType_48RGB: OSType = as_u32_be(b"b48r"); /* 48 bit RGB, 16-bit big-endian samples */
42pub const kCVPixelFormatType_32AlphaGray: OSType = as_u32_be(b"b32a"); /* 32 bit AlphaGray, 16-bit big-endian samples, black is zero */
43pub const kCVPixelFormatType_16Gray: OSType = as_u32_be(b"b16g"); /* 16 bit Grayscale, 16-bit big-endian samples, black is zero */
44pub const kCVPixelFormatType_30RGB: OSType = as_u32_be(b"R10k"); /* 30 bit RGB, 10-bit big-endian samples, 2 unused padding bits (at least significant end). */
45pub const kCVPixelFormatType_422YpCbCr8: OSType = as_u32_be(b"2vuy"); /* Component Y'CbCr 8-bit 4:2:2, ordered Cb Y'0 Cr Y'1 */
46pub const kCVPixelFormatType_4444YpCbCrA8: OSType = as_u32_be(b"v408"); /* Component Y'CbCrA 8-bit 4:4:4:4, ordered Cb Y' Cr A */
47pub const kCVPixelFormatType_4444YpCbCrA8R: OSType = as_u32_be(b"r408"); /* Component Y'CbCrA 8-bit 4:4:4:4, rendering format. full range alpha, zero biased YUV, ordered A Y' Cb Cr */
48pub const kCVPixelFormatType_4444AYpCbCr8: OSType = as_u32_be(b"y408"); /* Component Y'CbCrA 8-bit 4:4:4:4, ordered A Y' Cb Cr, full range alpha, video range Y'CbCr. */
49pub const kCVPixelFormatType_4444AYpCbCr16: OSType = as_u32_be(b"y416"); /* Component Y'CbCrA 16-bit 4:4:4:4, ordered A Y' Cb Cr, full range alpha, video range Y'CbCr, 16-bit little-endian samples. */
50pub const kCVPixelFormatType_444YpCbCr8: OSType = as_u32_be(b"v308"); /* Component Y'CbCr 8-bit 4:4:4 */
51pub const kCVPixelFormatType_422YpCbCr16: OSType = as_u32_be(b"v216"); /* Component Y'CbCr 10,12,14,16-bit 4:2:2 */
52pub const kCVPixelFormatType_422YpCbCr10: OSType = as_u32_be(b"v210"); /* Component Y'CbCr 10-bit 4:2:2 */
53pub const kCVPixelFormatType_444YpCbCr10: OSType = as_u32_be(b"v410"); /* Component Y'CbCr 10-bit 4:4:4 */
54pub const kCVPixelFormatType_420YpCbCr8Planar: OSType = as_u32_be(b"y420"); /* Planar Component Y'CbCr 8-bit 4:2:0.  baseAddr points to a big-endian CVPlanarPixelBufferInfo_YCbCrPlanar struct */
55pub const kCVPixelFormatType_420YpCbCr8PlanarFullRange: OSType = as_u32_be(b"f420"); /* Planar Component Y'CbCr 8-bit 4:2:0, full range.  baseAddr points to a big-endian CVPlanarPixelBufferInfo_YCbCrPlanar struct */
56pub const kCVPixelFormatType_422YpCbCr_4A_8BiPlanar: OSType = as_u32_be(b"a2vy"); /* First plane: Video-range Component Y'CbCr 8-bit 4:2:2, ordered Cb Y'0 Cr Y'1; second plane: alpha 8-bit 0-255 */
57pub const kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange: OSType = as_u32_be(b"420v"); /* Bi-Planar Component Y'CbCr 8-bit 4:2:0, video-range (luma=[16,235] chroma=[16,240]).  baseAddr points to a big-endian CVPlanarPixelBufferInfo_YCbCrBiPlanar struct */
58pub const kCVPixelFormatType_420YpCbCr8BiPlanarFullRange: OSType = as_u32_be(b"420f"); /* Bi-Planar Component Y'CbCr 8-bit 4:2:0, full-range (luma=[0,255] chroma=[1,255]).  baseAddr points to a big-endian CVPlanarPixelBufferInfo_YCbCrBiPlanar struct */
59pub const kCVPixelFormatType_422YpCbCr8_yuvs: OSType = as_u32_be(b"yuvs"); /* Component Y'CbCr 8-bit 4:2:2, ordered Y'0 Cb Y'1 Cr */
60pub const kCVPixelFormatType_422YpCbCr8FullRange: OSType = as_u32_be(b"yuvf"); /* Component Y'CbCr 8-bit 4:2:2, full range, ordered Y'0 Cb Y'1 Cr */
61pub const kCVPixelFormatType_OneComponent8: OSType = as_u32_be(b"L008"); /* 8 bit one component, black is zero */
62pub const kCVPixelFormatType_TwoComponent8: OSType = as_u32_be(b"2C08"); /* 8 bit two component, black is zero */
63pub const kCVPixelFormatType_30RGBLEPackedWideGamut: OSType = as_u32_be(b"w30r"); /* little-endian RGB101010, 2 MSB are zero, wide-gamut (384-895) */
64pub const kCVPixelFormatType_ARGB2101010LEPacked: OSType = as_u32_be(b"l10r"); /* little-endian ARGB2101010 full-range ARGB */
65pub const kCVPixelFormatType_OneComponent16Half: OSType = as_u32_be(b"L00h"); /* 16 bit one component IEEE half-precision float, 16-bit little-endian samples */
66pub const kCVPixelFormatType_OneComponent32Float: OSType = as_u32_be(b"L00f"); /* 32 bit one component IEEE float, 32-bit little-endian samples */
67pub const kCVPixelFormatType_TwoComponent16Half: OSType = as_u32_be(b"2C0h"); /* 16 bit two component IEEE half-precision float, 16-bit little-endian samples */
68pub const kCVPixelFormatType_TwoComponent32Float: OSType = as_u32_be(b"2C0f"); /* 32 bit two component IEEE float, 32-bit little-endian samples */
69pub const kCVPixelFormatType_64RGBAHalf: OSType = as_u32_be(b"RGhA"); /* 64 bit RGBA IEEE half-precision float, 16-bit little-endian samples */
70pub const kCVPixelFormatType_128RGBAFloat: OSType = as_u32_be(b"RGfA"); /* 128 bit RGBA IEEE float, 32-bit little-endian samples */
71pub const kCVPixelFormatType_14Bayer_GRBG: OSType = as_u32_be(b"grb4"); /* Bayer 14-bit Little-Endian, packed in 16-bits, ordered G R G R... alternating with B G B G... */
72pub const kCVPixelFormatType_14Bayer_RGGB: OSType = as_u32_be(b"rgg4"); /* Bayer 14-bit Little-Endian, packed in 16-bits, ordered R G R G... alternating with G B G B... */
73pub const kCVPixelFormatType_14Bayer_BGGR: OSType = as_u32_be(b"bgg4"); /* Bayer 14-bit Little-Endian, packed in 16-bits, ordered B G B G... alternating with G R G R... */
74pub const kCVPixelFormatType_14Bayer_GBRG: OSType = as_u32_be(b"gbr4"); /* Bayer 14-bit Little-Endian, packed in 16-bits, ordered G B G B... alternating with R G R G... */
75pub const kCVPixelFormatType_DisparityFloat16: OSType = as_u32_be(b"hdis"); /* IEEE754-2008 binary16 (half float), describing the normalized shift when comparing two images. Units are 1/meters: ( pixelShift / (pixelFocalLength * baselineInMeters) ) */
76pub const kCVPixelFormatType_DisparityFloat32: OSType = as_u32_be(b"fdis"); /* IEEE754-2008 binary32 float, describing the normalized shift when comparing two images. Units are 1/meters: ( pixelShift / (pixelFocalLength * baselineInMeters) ) */
77pub const kCVPixelFormatType_DepthFloat16: OSType = as_u32_be(b"hdep"); /* IEEE754-2008 binary16 (half float), describing the depth (distance to an object) in meters */
78pub const kCVPixelFormatType_DepthFloat32: OSType = as_u32_be(b"fdep"); /* IEEE754-2008 binary32 float, describing the depth (distance to an object) in meters */
79pub const kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange: OSType = as_u32_be(b"x420"); /* 2 plane YCbCr10 4:2:0, each 10 bits in the MSBs of 16bits, video-range (luma=[64,940] chroma=[64,960]) */
80pub const kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange: OSType = as_u32_be(b"x422"); /* 2 plane YCbCr10 4:2:2, each 10 bits in the MSBs of 16bits, video-range (luma=[64,940] chroma=[64,960]) */
81pub const kCVPixelFormatType_444YpCbCr10BiPlanarVideoRange: OSType = as_u32_be(b"x444"); /* 2 plane YCbCr10 4:4:4, each 10 bits in the MSBs of 16bits, video-range (luma=[64,940] chroma=[64,960]) */
82pub const kCVPixelFormatType_420YpCbCr10BiPlanarFullRange: OSType = as_u32_be(b"xf20"); /* 2 plane YCbCr10 4:2:0, each 10 bits in the MSBs of 16bits, full-range (Y range 0-1023) */
83pub const kCVPixelFormatType_422YpCbCr10BiPlanarFullRange: OSType = as_u32_be(b"xf22"); /* 2 plane YCbCr10 4:2:2, each 10 bits in the MSBs of 16bits, full-range (Y range 0-1023) */
84pub const kCVPixelFormatType_444YpCbCr10BiPlanarFullRange: OSType = as_u32_be(b"xf44"); /* 2 plane YCbCr10 4:4:4, each 10 bits in the MSBs of 16bits, full-range (Y range 0-1023) */
85
86pub const kCVPixelBufferLock_ReadOnly: CVPixelBufferLockFlags = 1;
87
88#[repr(C)]
89#[derive(Debug, Clone, Copy)]
90pub struct CVPlanarComponentInfo {
91    /// offset from main base address to base address of this plane, big-endian
92    pub offset: i32,
93    /// bytes per row of this plane, big-endian
94    pub rowBytes: u32,
95}
96
97#[repr(C)]
98#[derive(Debug, Clone, Copy)]
99pub struct CVPlanarPixelBufferInfo {
100    pub componentInfo: [CVPlanarComponentInfo; 1],
101}
102
103#[repr(C)]
104#[derive(Debug, Clone, Copy)]
105pub struct CVPlanarPixelBufferInfo_YCbCrPlanar {
106    pub componentInfoY: CVPlanarComponentInfo,
107    pub componentInfoCb: CVPlanarComponentInfo,
108    pub componentInfoCr: CVPlanarComponentInfo,
109}
110
111#[repr(C)]
112#[derive(Debug, Clone, Copy)]
113pub struct CVPlanarPixelBufferInfo_YCbCrBiPlanar {
114    pub componentInfoY: CVPlanarComponentInfo,
115    pub componentInfoCbCr: CVPlanarComponentInfo,
116}
117
118pub type CVPixelBufferReleaseBytesCallback =
119    extern "C" fn(releaseRefCon: *mut c_void, baseAddress: *const *const c_void);
120pub type CVPixelBufferReleasePlanarBytesCallback = extern "C" fn(
121    releaseRefCon: *mut c_void,
122    dataPtr: *const *const c_void,
123    dataSize: size_t,
124    numberOfPlanes: size_t,
125    planeAddresses: *const *const c_void,
126);
127
128extern "C" {
129    pub static kCVPixelBufferPixelFormatTypeKey: CFStringRef;
130    pub static kCVPixelBufferMemoryAllocatorKey: CFStringRef;
131    pub static kCVPixelBufferWidthKey: CFStringRef;
132    pub static kCVPixelBufferHeightKey: CFStringRef;
133    pub static kCVPixelBufferExtendedPixelsLeftKey: CFStringRef;
134    pub static kCVPixelBufferExtendedPixelsTopKey: CFStringRef;
135    pub static kCVPixelBufferExtendedPixelsRightKey: CFStringRef;
136    pub static kCVPixelBufferExtendedPixelsBottomKey: CFStringRef;
137    pub static kCVPixelBufferBytesPerRowAlignmentKey: CFStringRef;
138    pub static kCVPixelBufferCGBitmapContextCompatibilityKey: CFStringRef;
139    pub static kCVPixelBufferCGImageCompatibilityKey: CFStringRef;
140    pub static kCVPixelBufferOpenGLCompatibilityKey: CFStringRef;
141    pub static kCVPixelBufferPlaneAlignmentKey: CFStringRef;
142    pub static kCVPixelBufferIOSurfacePropertiesKey: CFStringRef;
143    pub static kCVPixelBufferOpenGLESCompatibilityKey: CFStringRef;
144    pub static kCVPixelBufferMetalCompatibilityKey: CFStringRef;
145    pub static kCVPixelBufferOpenGLTextureCacheCompatibilityKey: CFStringRef;
146    pub static kCVPixelBufferOpenGLESTextureCacheCompatibilityKey: CFStringRef;
147
148    pub fn CVBufferGetTypeID() -> CFTypeID;
149    pub fn CVPixelBufferRetain(texture: CVPixelBufferRef) -> CVPixelBufferRef;
150    pub fn CVPixelBufferRelease(texture: CVPixelBufferRef);
151    pub fn CVPixelBufferCreateResolvedAttributesDictionary(
152        allocator: CFAllocatorRef,
153        attributes: CFArrayRef,
154        resolvedDictionaryOut: *mut CFDictionaryRef,
155    ) -> CVReturn;
156    pub fn CVPixelBufferCreate(
157        allocator: CFAllocatorRef,
158        width: size_t,
159        height: size_t,
160        pixelFormatType: OSType,
161        pixelBufferAttributes: CFDictionaryRef,
162        pixelBufferOut: *mut CVPixelBufferRef,
163    ) -> CVReturn;
164    // pub fn CVPixelBufferCreateWithBytes(allocator: CFAllocatorRef,
165    //                                     width: size_t,
166    //                                     height: size_t,
167    //                                     pixelFormatType: OSType,
168    //                                     baseAddress: *const c_void,
169    //                                     bytesPerRow: size_t,
170    //                                     releaseCallback: CVPixelBufferReleaseBytesCallback,
171    //                                     releaseRefCon: *const c_void,
172    //                                     pixelBufferAttributes: CFDictionaryRef,
173    //                                     pixelBufferOut: *mut CVPixelBufferRef) -> CVReturn;
174    // pub fn CVPixelBufferCreateWithPlanarBytes(allocator: CFAllocatorRef,
175    //                                           width: size_t,
176    //                                           height: size_t,
177    //                                           pixelFormatType: OSType,
178    //                                           dataPtr: *const c_void,
179    //                                           dataSize: size_t,
180    //                                           numberOfPlanes: size_t,
181    //                                           planeBaseAddress: *const *const c_void,
182    //                                           ) -> CVReturn;
183
184    pub fn CVPixelBufferLockBaseAddress(
185        pixelBuffer: CVPixelBufferRef,
186        lockFlags: CVOptionFlags,
187    ) -> CVReturn;
188    pub fn CVPixelBufferUnlockBaseAddress(
189        pixelBuffer: CVPixelBufferRef,
190        unlockFlags: CVOptionFlags,
191    ) -> CVReturn;
192    pub fn CVPixelBufferGetWidth(pixelBuffer: CVPixelBufferRef) -> size_t;
193    pub fn CVPixelBufferGetHeight(pixelBuffer: CVPixelBufferRef) -> size_t;
194    pub fn CVPixelBufferGetPixelFormatType(pixelBuffer: CVPixelBufferRef) -> OSType;
195
196    pub fn CVPixelBufferGetBaseAddress(pixelBuffer: CVPixelBufferRef) -> *mut c_void;
197    pub fn CVPixelBufferGetBytesPerRow(pixelBuffer: CVPixelBufferRef) -> size_t;
198    pub fn CVPixelBufferIsPlanar(pixelBuffer: CVPixelBufferRef) -> Boolean;
199    pub fn CVPixelBufferGetPlaneCount(pixelBuffer: CVPixelBufferRef) -> size_t;
200    pub fn CVPixelBufferGetWidthOfPlane(
201        pixelBuffer: CVPixelBufferRef,
202        planeIndex: size_t,
203    ) -> size_t;
204    pub fn CVPixelBufferGetHeightOfPlane(
205        pixelBuffer: CVPixelBufferRef,
206        planeIndex: size_t,
207    ) -> size_t;
208    pub fn CVPixelBufferGetBaseAddressOfPlane(
209        pixelBuffer: CVPixelBufferRef,
210        planeIndex: size_t,
211    ) -> *mut c_void;
212    pub fn CVPixelBufferGetBytesPerRowOfPlane(
213        pixelBuffer: CVPixelBufferRef,
214        planeIndex: size_t,
215    ) -> size_t;
216
217    pub fn CVPixelBufferGetExtendedPixels(
218        pixelBuffer: CVPixelBufferRef,
219        extraColumnsOnLeft: *const size_t,
220        extraColumnsOnRight: *const size_t,
221        extraRowsOnTop: *const size_t,
222        extraRowsOnBottom: *const size_t,
223    );
224    pub fn CVPixelBufferFillExtendedPixels(pixelBuffer: CVPixelBufferRef) -> CVReturn;
225}