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
use std::ptr::null_mut;

use core_foundation::{
    base::{kCFAllocatorDefault, Boolean, CFAllocatorRef, OSStatus, TCFType},
    string::{CFString, CFStringEncoding, CFStringRef},
};
use libc::size_t;

use crate::{
    block_buffer::{CMBlockBuffer, CMBlockBufferRef},
    format_description::{
        CMAudioFormatDescription, CMAudioFormatDescriptionRef, CMClosedCaptionFormatDescriptionRef, CMMediaType, CMMetadataFormatDescriptionRef,
        CMTextFormatDescriptionRef, CMTimeCodeFormatDescriptionRef, CMVideoFormatDescription, CMVideoFormatDescriptionRef,
    },
};

pub const kCMFormatDescriptionBridgeError_InvalidParameter: OSStatus = -12712;
pub const kCMFormatDescriptionBridgeError_AllocationFailed: OSStatus = -12713;
pub const kCMFormatDescriptionBridgeError_InvalidSerializedSampleDescription: OSStatus = -12714;
pub const kCMFormatDescriptionBridgeError_InvalidFormatDescription: OSStatus = -12715;
pub const kCMFormatDescriptionBridgeError_IncompatibleFormatDescription: OSStatus = -12716;
pub const kCMFormatDescriptionBridgeError_UnsupportedSampleDescriptionFlavor: OSStatus = -12717;
pub const kCMFormatDescriptionBridgeError_InvalidSlice: OSStatus = -12719;

extern "C" {
    pub static kCMImageDescriptionFlavor_QuickTimeMovie: CFStringRef;
    pub static kCMImageDescriptionFlavor_ISOFamily: CFStringRef;
    pub static kCMImageDescriptionFlavor_3GPFamily: CFStringRef;
    pub static kCMImageDescriptionFlavor_ISOFamilyWithAppleExtensions: CFStringRef;

    pub fn CMVideoFormatDescriptionCreateFromBigEndianImageDescriptionData(
        allocator: CFAllocatorRef,
        imageDescriptionData: *const u8,
        size: size_t,
        stringEncoding: CFStringEncoding,
        flavor: CFStringRef,
        formatDescriptionOut: *mut CMVideoFormatDescriptionRef,
    ) -> OSStatus;
    pub fn CMVideoFormatDescriptionCreateFromBigEndianImageDescriptionBlockBuffer(
        allocator: CFAllocatorRef,
        imageDescriptionBlockBuffer: CMBlockBufferRef,
        stringEncoding: CFStringEncoding,
        flavor: CFStringRef,
        formatDescriptionOut: *mut CMVideoFormatDescriptionRef,
    ) -> OSStatus;
    pub fn CMVideoFormatDescriptionCopyAsBigEndianImageDescriptionBlockBuffer(
        allocator: CFAllocatorRef,
        videoFormatDescription: CMVideoFormatDescriptionRef,
        stringEncoding: CFStringEncoding,
        flavor: CFStringRef,
        blockBufferOut: *mut CMBlockBufferRef,
    ) -> OSStatus;

    cfg_if! {
        if #[cfg(target_endian = "little")] {
            pub fn CMSwapBigEndianImageDescriptionToHost(imageDescriptionData: *mut u8, imageDescriptionSize: size_t) -> OSStatus;
            pub fn CMSwapHostEndianImageDescriptionToBig(imageDescriptionData: *mut u8, imageDescriptionSize: size_t) -> OSStatus;
        }
    }

    pub static kCMSoundDescriptionFlavor_QuickTimeMovie: CFStringRef;
    pub static kCMSoundDescriptionFlavor_QuickTimeMovieV2: CFStringRef;
    pub static kCMSoundDescriptionFlavor_ISOFamily: CFStringRef;
    pub static kCMSoundDescriptionFlavor_3GPFamily: CFStringRef;

    pub fn CMAudioFormatDescriptionCreateFromBigEndianSoundDescriptionData(
        allocator: CFAllocatorRef,
        soundDescriptionData: *const u8,
        size: size_t,
        flavor: CFStringRef,
        formatDescriptionOut: *mut CMAudioFormatDescriptionRef,
    ) -> OSStatus;
    pub fn CMAudioFormatDescriptionCreateFromBigEndianSoundDescriptionBlockBuffer(
        allocator: CFAllocatorRef,
        soundDescriptionBlockBuffer: CMBlockBufferRef,
        flavor: CFStringRef,
        formatDescriptionOut: *mut CMAudioFormatDescriptionRef,
    ) -> OSStatus;
    pub fn CMAudioFormatDescriptionCopyAsBigEndianSoundDescriptionBlockBuffer(
        allocator: CFAllocatorRef,
        audioFormatDescription: CMAudioFormatDescriptionRef,
        flavor: CFStringRef,
        blockBufferOut: *mut CMBlockBufferRef,
    ) -> OSStatus;
    pub fn CMDoesBigEndianSoundDescriptionRequireLegacyCBRSampleTableLayout(
        soundDescriptionBlockBuffer: CMBlockBufferRef,
        flavor: CFStringRef,
    ) -> Boolean;

    cfg_if! {
        if #[cfg(target_endian = "little")] {
            pub fn CMSwapBigEndianSoundDescriptionToHost(soundDescriptionData: *mut u8, soundDescriptionSize: size_t) -> OSStatus;
            pub fn CMSwapHostEndianSoundDescriptionToBig(soundDescriptionData: *mut u8, soundDescriptionSize: size_t) -> OSStatus;
        }
    }

    pub fn CMTextFormatDescriptionCreateFromBigEndianTextDescriptionData(
        allocator: CFAllocatorRef,
        textDescriptionData: *const u8,
        size: size_t,
        flavor: CFStringRef,
        mediaType: CMMediaType,
        formatDescriptionOut: *mut CMTextFormatDescriptionRef,
    ) -> OSStatus;
    pub fn CMTextFormatDescriptionCreateFromBigEndianTextDescriptionBlockBuffer(
        allocator: CFAllocatorRef,
        textDescriptionBlockBuffer: CMBlockBufferRef,
        flavor: CFStringRef,
        mediaType: CMMediaType,
        formatDescriptionOut: *mut CMTextFormatDescriptionRef,
    ) -> OSStatus;
    pub fn CMTextFormatDescriptionCopyAsBigEndianTextDescriptionBlockBuffer(
        allocator: CFAllocatorRef,
        textFormatDescription: CMTextFormatDescriptionRef,
        flavor: CFStringRef,
        blockBufferOut: *mut CMBlockBufferRef,
    ) -> OSStatus;

    cfg_if! {
        if #[cfg(target_endian = "little")] {
            pub fn CMSwapBigEndianTextDescriptionToHost(textDescriptionData: *mut u8, textDescriptionSize: size_t) -> OSStatus;
            pub fn CMSwapHostEndianTextDescriptionToBig(textDescriptionData: *mut u8, textDescriptionSize: size_t) -> OSStatus;
        }
    }

    pub fn CMClosedCaptionFormatDescriptionCreateFromBigEndianClosedCaptionDescriptionData(
        allocator: CFAllocatorRef,
        closedCaptionDescriptionData: *const u8,
        size: size_t,
        flavor: CFStringRef,
        formatDescriptionOut: *mut CMClosedCaptionFormatDescriptionRef,
    ) -> OSStatus;
    pub fn CMClosedCaptionFormatDescriptionCreateFromBigEndianClosedCaptionDescriptionBlockBuffer(
        allocator: CFAllocatorRef,
        closedCaptionDescriptionBlockBuffer: CMBlockBufferRef,
        flavor: CFStringRef,
        formatDescriptionOut: *mut CMClosedCaptionFormatDescriptionRef,
    ) -> OSStatus;
    pub fn CMClosedCaptionFormatDescriptionCopyAsBigEndianClosedCaptionDescriptionBlockBuffer(
        allocator: CFAllocatorRef,
        closedCaptionFormatDescription: CMClosedCaptionFormatDescriptionRef,
        flavor: CFStringRef,
        blockBufferOut: *mut CMBlockBufferRef,
    ) -> OSStatus;

    cfg_if! {
        if #[cfg(target_endian = "little")] {
            pub fn CMSwapBigEndianClosedCaptionDescriptionToHost(closedCaptionDescriptionData: *mut u8, closedCaptionDescriptionSize: size_t) -> OSStatus;
            pub fn CMSwapHostEndianClosedCaptionDescriptionToBig(closedCaptionDescriptionData: *mut u8, closedCaptionDescriptionSize: size_t) -> OSStatus;
        }
    }

    pub fn CMTimeCodeFormatDescriptionCreateFromBigEndianTimeCodeDescriptionData(
        allocator: CFAllocatorRef,
        timeCodeDescriptionData: *const u8,
        size: size_t,
        flavor: CFStringRef,
        formatDescriptionOut: *mut CMTimeCodeFormatDescriptionRef,
    ) -> OSStatus;
    pub fn CMTimeCodeFormatDescriptionCreateFromBigEndianTimeCodeDescriptionBlockBuffer(
        allocator: CFAllocatorRef,
        timeCodeDescriptionBlockBuffer: CMBlockBufferRef,
        flavor: CFStringRef,
        formatDescriptionOut: *mut CMTimeCodeFormatDescriptionRef,
    ) -> OSStatus;
    pub fn CMTimeCodeFormatDescriptionCopyAsBigEndianTimeCodeDescriptionBlockBuffer(
        allocator: CFAllocatorRef,
        timeCodeFormatDescription: CMTimeCodeFormatDescriptionRef,
        flavor: CFStringRef,
        blockBufferOut: *mut CMBlockBufferRef,
    ) -> OSStatus;

    cfg_if! {
        if #[cfg(target_endian = "little")] {
            pub fn CMSwapBigEndianTimeCodeDescriptionToHost(timeCodeDescriptionData: *mut u8, timeCodeDescriptionSize: size_t) -> OSStatus;
            pub fn CMSwapHostEndianTimeCodeDescriptionToBig(timeCodeDescriptionData: *mut u8, timeCodeDescriptionSize: size_t) -> OSStatus;
        }
    }

    pub fn CMMetadataFormatDescriptionCreateFromBigEndianMetadataDescriptionData(
        allocator: CFAllocatorRef,
        metadataDescriptionData: *const u8,
        size: size_t,
        flavor: CFStringRef,
        formatDescriptionOut: *mut CMMetadataFormatDescriptionRef,
    ) -> OSStatus;
    pub fn CMMetadataFormatDescriptionCreateFromBigEndianMetadataDescriptionBlockBuffer(
        allocator: CFAllocatorRef,
        metadataDescriptionBlockBuffer: CMBlockBufferRef,
        flavor: CFStringRef,
        formatDescriptionOut: *mut CMMetadataFormatDescriptionRef,
    ) -> OSStatus;
    pub fn CMMetadataFormatDescriptionCopyAsBigEndianMetadataDescriptionBlockBuffer(
        allocator: CFAllocatorRef,
        metadataFormatDescription: CMMetadataFormatDescriptionRef,
        flavor: CFStringRef,
        blockBufferOut: *mut CMBlockBufferRef,
    ) -> OSStatus;

    cfg_if! {
        if #[cfg(target_endian = "little")] {
            pub fn CMSwapBigEndianMetadataDescriptionToHost(metadataDescriptionData: *mut u8, metadataDescriptionSize: size_t) -> OSStatus;
            pub fn CMSwapHostEndianMetadataDescriptionToBig(metadataDescriptionData: *mut u8, metadataDescriptionSize: size_t) -> OSStatus;
        }
    }
}

impl CMAudioFormatDescription {
    pub fn from_big_endian_sound_description_data(sound_descriptionData: &[u8], flavor: &CFString) -> Result<CMAudioFormatDescription, OSStatus> {
        let mut format_description: CMAudioFormatDescriptionRef = null_mut();
        let status = unsafe {
            CMAudioFormatDescriptionCreateFromBigEndianSoundDescriptionData(
                kCFAllocatorDefault,
                sound_descriptionData.as_ptr(),
                sound_descriptionData.len(),
                flavor.as_concrete_TypeRef(),
                &mut format_description,
            )
        };
        if status == 0 {
            Ok(unsafe { CMAudioFormatDescription::wrap_under_create_rule(format_description) })
        } else {
            Err(status)
        }
    }

    pub fn from_big_endian_sound_description_block_buffer(
        sound_description_block_buffer: &CMBlockBuffer,
        flavor: &CFString,
    ) -> Result<CMAudioFormatDescription, OSStatus> {
        let mut format_description: CMAudioFormatDescriptionRef = null_mut();
        let status = unsafe {
            CMAudioFormatDescriptionCreateFromBigEndianSoundDescriptionBlockBuffer(
                kCFAllocatorDefault,
                sound_description_block_buffer.as_concrete_TypeRef(),
                flavor.as_concrete_TypeRef(),
                &mut format_description,
            )
        };
        if status == 0 {
            Ok(unsafe { CMAudioFormatDescription::wrap_under_create_rule(format_description) })
        } else {
            Err(status)
        }
    }

    pub fn copy_as_big_endian_sound_description_block_buffer(&self, flavor: &CFString) -> Result<CMBlockBuffer, OSStatus> {
        let mut block_buffer: CMBlockBufferRef = null_mut();
        let status = unsafe {
            CMAudioFormatDescriptionCopyAsBigEndianSoundDescriptionBlockBuffer(
                kCFAllocatorDefault,
                self.as_concrete_TypeRef(),
                flavor.as_concrete_TypeRef(),
                &mut block_buffer,
            )
        };
        if status == 0 {
            Ok(unsafe { CMBlockBuffer::wrap_under_create_rule(block_buffer) })
        } else {
            Err(status)
        }
    }
}

impl CMBlockBuffer {
    pub fn does_big_endian_sound_description_require_legacy_cbr_sample_table_layout(&self, flavor: &CFString) -> bool {
        unsafe { CMDoesBigEndianSoundDescriptionRequireLegacyCBRSampleTableLayout(self.as_concrete_TypeRef(), flavor.as_concrete_TypeRef()) != 0 }
    }
}

impl CMVideoFormatDescription {
    pub fn from_big_endian_image_description_data(
        image_description_data: &[u8],
        string_encoding: CFStringEncoding,
        flavor: &CFString,
    ) -> Result<CMVideoFormatDescription, OSStatus> {
        let mut format_description: CMVideoFormatDescriptionRef = null_mut();
        let status = unsafe {
            CMVideoFormatDescriptionCreateFromBigEndianImageDescriptionData(
                kCFAllocatorDefault,
                image_description_data.as_ptr(),
                image_description_data.len(),
                string_encoding,
                flavor.as_concrete_TypeRef(),
                &mut format_description,
            )
        };
        if status == 0 {
            Ok(unsafe { CMVideoFormatDescription::wrap_under_create_rule(format_description) })
        } else {
            Err(status)
        }
    }

    pub fn from_big_endian_image_description_block_buffer(
        image_description_block_buffer: &CMBlockBuffer,
        string_encoding: CFStringEncoding,
        flavor: &CFString,
    ) -> Result<CMVideoFormatDescription, OSStatus> {
        let mut format_description: CMVideoFormatDescriptionRef = null_mut();
        let status = unsafe {
            CMVideoFormatDescriptionCreateFromBigEndianImageDescriptionBlockBuffer(
                kCFAllocatorDefault,
                image_description_block_buffer.as_concrete_TypeRef(),
                string_encoding,
                flavor.as_concrete_TypeRef(),
                &mut format_description,
            )
        };
        if status == 0 {
            Ok(unsafe { CMVideoFormatDescription::wrap_under_create_rule(format_description) })
        } else {
            Err(status)
        }
    }

    pub fn copy_as_big_endian_image_description_block_buffer(
        &self,
        string_encoding: CFStringEncoding,
        flavor: &CFString,
    ) -> Result<CMBlockBuffer, OSStatus> {
        let mut block_buffer: CMBlockBufferRef = null_mut();
        let status = unsafe {
            CMVideoFormatDescriptionCopyAsBigEndianImageDescriptionBlockBuffer(
                kCFAllocatorDefault,
                self.as_concrete_TypeRef(),
                string_encoding,
                flavor.as_concrete_TypeRef(),
                &mut block_buffer,
            )
        };
        if status == 0 {
            Ok(unsafe { CMBlockBuffer::wrap_under_create_rule(block_buffer) })
        } else {
            Err(status)
        }
    }
}