core_video/
pixel_format_description.rs

1use core_foundation::{
2    array::{CFArray, CFArrayRef},
3    base::{kCFAllocatorDefault, Boolean, CFAllocatorRef, CFIndex, CFType, TCFType},
4    dictionary::{CFDictionary, CFDictionaryRef},
5    number::CFNumber,
6    string::{CFString, CFStringRef},
7};
8use libc::c_void;
9
10use crate::{pixel_buffer::CVPixelBufferRef, OSType};
11
12pub type CVFillExtendedPixelsCallBack = extern "C" fn(pixelBuffer: CVPixelBufferRef, refCon: *mut c_void) -> Boolean;
13
14#[repr(C)]
15pub struct CVFillExtendedPixelsCallBackData {
16    pub version: CFIndex,
17    pub fillCallBack: CVFillExtendedPixelsCallBack,
18    pub refCon: *mut c_void,
19}
20
21extern "C" {
22    pub static kCVPixelFormatName: CFStringRef;
23    pub static kCVPixelFormatConstant: CFStringRef;
24    pub static kCVPixelFormatCodecType: CFStringRef;
25    pub static kCVPixelFormatFourCC: CFStringRef;
26    pub static kCVPixelFormatContainsAlpha: CFStringRef;
27    pub static kCVPixelFormatContainsYCbCr: CFStringRef;
28    pub static kCVPixelFormatContainsRGB: CFStringRef;
29    pub static kCVPixelFormatComponentRange: CFStringRef;
30    pub static kCVPixelFormatComponentRange_VideoRange: CFStringRef;
31    pub static kCVPixelFormatComponentRange_FullRange: CFStringRef;
32    pub static kCVPixelFormatComponentRange_WideRange: CFStringRef;
33    pub static kCVPixelFormatPlanes: CFStringRef;
34    pub static kCVPixelFormatBlockWidth: CFStringRef;
35    pub static kCVPixelFormatBlockHeight: CFStringRef;
36    pub static kCVPixelFormatBitsPerBlock: CFStringRef;
37    pub static kCVPixelFormatBlockHorizontalAlignment: CFStringRef;
38    pub static kCVPixelFormatBlockVerticalAlignment: CFStringRef;
39    pub static kCVPixelFormatBlackBlock: CFStringRef;
40    pub static kCVPixelFormatHorizontalSubsampling: CFStringRef;
41    pub static kCVPixelFormatVerticalSubsampling: CFStringRef;
42    pub static kCVPixelFormatOpenGLFormat: CFStringRef;
43    pub static kCVPixelFormatOpenGLType: CFStringRef;
44    pub static kCVPixelFormatOpenGLInternalFormat: CFStringRef;
45    pub static kCVPixelFormatCGBitmapInfo: CFStringRef;
46    pub static kCVPixelFormatQDCompatibility: CFStringRef;
47    pub static kCVPixelFormatCGBitmapContextCompatibility: CFStringRef;
48    pub static kCVPixelFormatCGImageCompatibility: CFStringRef;
49    pub static kCVPixelFormatOpenGLCompatibility: CFStringRef;
50    #[cfg(target_os = "ios")]
51    pub static kCVPixelFormatOpenGLESCompatibility: CFStringRef;
52    pub static kCVPixelFormatFillExtendedPixelsCallback: CFStringRef;
53
54    pub fn CVPixelFormatDescriptionCreateWithPixelFormatType(allocator: CFAllocatorRef, pixelFormat: OSType) -> CFDictionaryRef;
55    pub fn CVPixelFormatDescriptionArrayCreateWithAllPixelFormatTypes(allocator: CFAllocatorRef) -> CFArrayRef;
56    pub fn CVPixelFormatDescriptionRegisterDescriptionWithPixelFormatType(description: CFDictionaryRef, pixelFormat: OSType);
57    pub fn CVIsCompressedPixelFormatAvailable(pixelFormat: OSType) -> Boolean;
58}
59
60pub enum CVPixelFormatDescriptionKeys {
61    Name,
62    Constant,
63    CodecType,
64    FourCC,
65    ContainsAlpha,
66    ContainsYCbCr,
67    ContainsRGB,
68    ComponentRange,
69    ComponentRange_VideoRange,
70    ComponentRange_FullRange,
71    ComponentRange_WideRange,
72    Planes,
73    BlockWidth,
74    BlockHeight,
75    BitsPerBlock,
76    BlockHorizontalAlignment,
77    BlockVerticalAlignment,
78    BlackBlock,
79    HorizontalSubsampling,
80    VerticalSubsampling,
81    OpenGLFormat,
82    OpenGLType,
83    OpenGLInternalFormat,
84    CGBitmapInfo,
85    QDCompatibility,
86    CGBitmapContextCompatibility,
87    CGImageCompatibility,
88    OpenGLCompatibility,
89    #[cfg(target_os = "ios")]
90    OpenGLESCompatibility,
91    FillExtendedPixelsCallback,
92}
93
94impl From<CVPixelFormatDescriptionKeys> for CFStringRef {
95    fn from(key: CVPixelFormatDescriptionKeys) -> Self {
96        unsafe {
97            match key {
98                CVPixelFormatDescriptionKeys::Name => kCVPixelFormatName,
99                CVPixelFormatDescriptionKeys::Constant => kCVPixelFormatConstant,
100                CVPixelFormatDescriptionKeys::CodecType => kCVPixelFormatCodecType,
101                CVPixelFormatDescriptionKeys::FourCC => kCVPixelFormatFourCC,
102                CVPixelFormatDescriptionKeys::ContainsAlpha => kCVPixelFormatContainsAlpha,
103                CVPixelFormatDescriptionKeys::ContainsYCbCr => kCVPixelFormatContainsYCbCr,
104                CVPixelFormatDescriptionKeys::ContainsRGB => kCVPixelFormatContainsRGB,
105                CVPixelFormatDescriptionKeys::ComponentRange => kCVPixelFormatComponentRange,
106                CVPixelFormatDescriptionKeys::ComponentRange_VideoRange => kCVPixelFormatComponentRange_VideoRange,
107                CVPixelFormatDescriptionKeys::ComponentRange_FullRange => kCVPixelFormatComponentRange_FullRange,
108                CVPixelFormatDescriptionKeys::ComponentRange_WideRange => kCVPixelFormatComponentRange_WideRange,
109                CVPixelFormatDescriptionKeys::Planes => kCVPixelFormatPlanes,
110                CVPixelFormatDescriptionKeys::BlockWidth => kCVPixelFormatBlockWidth,
111                CVPixelFormatDescriptionKeys::BlockHeight => kCVPixelFormatBlockHeight,
112                CVPixelFormatDescriptionKeys::BitsPerBlock => kCVPixelFormatBitsPerBlock,
113                CVPixelFormatDescriptionKeys::BlockHorizontalAlignment => kCVPixelFormatBlockHorizontalAlignment,
114                CVPixelFormatDescriptionKeys::BlockVerticalAlignment => kCVPixelFormatBlockVerticalAlignment,
115                CVPixelFormatDescriptionKeys::BlackBlock => kCVPixelFormatBlackBlock,
116                CVPixelFormatDescriptionKeys::HorizontalSubsampling => kCVPixelFormatHorizontalSubsampling,
117                CVPixelFormatDescriptionKeys::VerticalSubsampling => kCVPixelFormatVerticalSubsampling,
118                CVPixelFormatDescriptionKeys::OpenGLFormat => kCVPixelFormatOpenGLFormat,
119                CVPixelFormatDescriptionKeys::OpenGLType => kCVPixelFormatOpenGLType,
120                CVPixelFormatDescriptionKeys::OpenGLInternalFormat => kCVPixelFormatOpenGLInternalFormat,
121                CVPixelFormatDescriptionKeys::CGBitmapInfo => kCVPixelFormatCGBitmapInfo,
122                CVPixelFormatDescriptionKeys::QDCompatibility => kCVPixelFormatQDCompatibility,
123                CVPixelFormatDescriptionKeys::CGBitmapContextCompatibility => kCVPixelFormatCGBitmapContextCompatibility,
124                CVPixelFormatDescriptionKeys::CGImageCompatibility => kCVPixelFormatCGImageCompatibility,
125                CVPixelFormatDescriptionKeys::OpenGLCompatibility => kCVPixelFormatOpenGLCompatibility,
126                #[cfg(target_os = "ios")]
127                CVPixelFormatDescriptionKeys::OpenGLESCompatibility => kCVPixelFormatOpenGLESCompatibility,
128                CVPixelFormatDescriptionKeys::FillExtendedPixelsCallback => kCVPixelFormatFillExtendedPixelsCallback,
129            }
130        }
131    }
132}
133
134impl From<CVPixelFormatDescriptionKeys> for CFString {
135    fn from(key: CVPixelFormatDescriptionKeys) -> Self {
136        unsafe { CFString::wrap_under_get_rule(CFStringRef::from(key)) }
137    }
138}
139
140pub fn pixel_format_description_create_with_pixel_format_type(pixel_format: OSType) -> Result<CFDictionary<CFString, CFType>, ()> {
141    unsafe {
142        let description = CVPixelFormatDescriptionCreateWithPixelFormatType(kCFAllocatorDefault, pixel_format);
143        if description.is_null() {
144            Err(())
145        } else {
146            Ok(TCFType::wrap_under_create_rule(description))
147        }
148    }
149}
150
151pub fn pixel_format_description_array_create_with_all_pixel_format_types() -> Result<CFArray<CFNumber>, ()> {
152    unsafe {
153        let array = CVPixelFormatDescriptionArrayCreateWithAllPixelFormatTypes(kCFAllocatorDefault);
154        if array.is_null() {
155            Err(())
156        } else {
157            Ok(TCFType::wrap_under_create_rule(array))
158        }
159    }
160}
161
162pub fn is_compressed_pixel_format_available(pixel_format: OSType) -> bool {
163    unsafe { CVIsCompressedPixelFormatAvailable(pixel_format) != 0 }
164}