ccap-rs 1.6.0

Rust bindings for ccap — high-performance, cross-platform webcam/camera capture with hardware-accelerated pixel format conversion (DirectShow/AVFoundation/V4L2), including common RGB/YUV workflows and video file input/playback support
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
/**
 * @file ccap_convert_apple.cpp
 * @author wysaid (this@wysaid.org)
 * @date 2025-05
 *
 */

#if __APPLE__

#include "ccap_convert_apple.h"

#include "ccap_utils.h"

#include <Accelerate/Accelerate.h>
#include <cassert>

namespace ccap {
template <int inputChannels, int outputChannels, bool swapRB>
void colorShuffle_apple(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height) {
    static_assert((inputChannels == 3 || inputChannels == 4) && (outputChannels == 3 || outputChannels == 4),
                  "inputChannels and outputChannels must be 3 or 4");

    assert(width > 0 && height != 0); // width must be positive, height can be negative for vertical flip

    vImage_Buffer dstBuffer;
    std::shared_ptr<ccap::Allocator> sharedAllocator;

    if (height > 0) {
        dstBuffer = { dst, (uint32_t)height, (uint32_t)width, (size_t)dstStride };
    } else {
        height = -height; // Negative height means vertical flip
        sharedAllocator = ccap::getSharedAllocator();
        assert(sharedAllocator != nullptr);
        if (dstStride * height > sharedAllocator->size()) {
            // Resize the shared allocator to accommodate the new size
            sharedAllocator->resize(dstStride * height);
        }
        dstBuffer = { sharedAllocator->data(), (uint32_t)height, (uint32_t)width, (size_t)dstStride };
    }
    vImage_Buffer srcBuffer = { (void*)src, (uint32_t)height, (uint32_t)width, (size_t)srcStride };

    if constexpr (inputChannels == outputChannels) {
        /// Conversion with the same number of channels, only RGB <-> BGR, RGBA <-> BGRA, swapRB must be true.

        static_assert(swapRB, "swapRB must be true when inputChannels == outputChannels");

        // RGBA8888 <-> BGRA8888: Channel reordering required
        // vImagePermuteChannels_ARGB8888 needs 4 indices to implement arbitrary channel arrangement
        // RGBA8888: [R, G, B, A], BGRA8888: [B, G, R, A]
        constexpr uint8_t permuteMap[4] = { 2, 1, 0, 3 };

        if constexpr (inputChannels == 4) { // RGBA -> BGRA
            vImagePermuteChannels_ARGB8888(&srcBuffer, &dstBuffer, permuteMap, kvImageNoFlags);
        } else {
            vImagePermuteChannels_RGB888(&srcBuffer, &dstBuffer, permuteMap, kvImageNoFlags);
        }
    } else { // Different number of channels, only 4 channels <-> 3 channels

        if constexpr (inputChannels == 4) { // 4 channels -> 3 channels
            if constexpr (swapRB) {         // Possible cases: RGBA->BGR, BGRA->RGB
                vImageConvert_RGBA8888toBGR888(&srcBuffer, &dstBuffer, kvImageNoFlags);
            } else { // Possible cases: RGBA->RGB, BGRA->BGR
                vImageConvert_RGBA8888toRGB888(&srcBuffer, &dstBuffer, kvImageNoFlags);
            }
        } else {                    /// 3 channels -> 4 channels
            if constexpr (swapRB) { // Possible cases: BGR->RGBA, RGB->BGRA
                vImageConvert_RGB888toBGRA8888(&srcBuffer, nullptr, 0xff, &dstBuffer, false, kvImageNoFlags);
            } else { // Possible cases: BGR->BGRA, RGB->RGBA
                vImageConvert_RGB888toRGBA8888(&srcBuffer, nullptr, 0xff, &dstBuffer, false, kvImageNoFlags);
            }
        }
    }

    if (sharedAllocator) { // Perform vertical flip and write the final result to dst. Even with two-step operation, it's still much faster than regular CPU operations.
        verticalFlip_apple((const uint8_t*)dstBuffer.data, dstStride, dst, dstStride, height);
    }
}

template void colorShuffle_apple<4, 4, true>(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height);

template void colorShuffle_apple<4, 3, true>(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height);

template void colorShuffle_apple<4, 3, false>(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height);

template void colorShuffle_apple<3, 4, true>(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height);

template void colorShuffle_apple<3, 4, false>(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height);

template void colorShuffle_apple<3, 3, true>(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height);

void verticalFlip_apple(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int height) {
    assert(src != nullptr && dst != nullptr);
    assert(srcStride > 0 && dstStride > 0 && height > 0); // width and height must be positive
    vImage_Buffer srcBuffer = { (void*)src, (vImagePixelCount)height, (vImagePixelCount)srcStride, (size_t)srcStride };
    vImage_Buffer dstBuffer = { dst, (vImagePixelCount)height, (vImagePixelCount)dstStride, (size_t)dstStride };
    vImageVerticalReflect_Planar8(&srcBuffer, &dstBuffer, kvImageNoFlags);
}

////////////////// NV12 to BGRA8888 //////////////////////

// void checkAlpha(const uint8_t* rgbaSrc, int srcStride, int width, int height, int alphaOffset = 3) {
//     assert(rgbaSrc != nullptr);
//     assert(width > 0 && height > 0); // width and height must be positive
//     for (int y = 0; y < height; ++y) {
//         const uint8_t* row = rgbaSrc + y * srcStride;
//         for (int x = 0; x < width; ++x) {
//             if (row[x * 4 + alphaOffset] != 0xff) { // Check alpha channel
//                 // Print current pixel coordinates and RGB values
//                 auto r = row[x * 4 + 0];
//                 auto g = row[x * 4 + 1];
//                 auto b = row[x * 4 + 2];
//                 auto a = row[x * 4 + 3];
//                 CCAP_LOG_E("Alpha channel (%d) is not 255 at pixel (%d, %d, %d): (%d, %d, %d, %d)", (int)row[x * 4 + alphaOffset], x, y, alphaOffset, r, g, b, a);
//                 assert(false && "Alpha channel is not 255, this is an error in the conversion process.");
//             }
//         }
//     }
// }

void nv12ToArgb32_apple_imp(const uint8_t* srcY, int srcYStride,
                            const uint8_t* srcUV, int srcUVStride,
                            uint8_t* dst, int dstStride,
                            int width, int height,
                            const vImage_YpCbCrToARGBMatrix* matrix,
                            const vImage_YpCbCrPixelRange* range,
                            const uint8_t permuteMap[4]) {
    assert(width > 0 && height != 0); // Internal implementation
    // Construct vImage_Buffer
    vImage_Buffer dstBuffer;
    std::shared_ptr<ccap::Allocator> sharedAllocator = ccap::getSharedAllocator();
    assert(sharedAllocator != nullptr);
    auto realHeight = std::abs(height);

    if (dstStride * realHeight > sharedAllocator->size()) {
        // Resize the shared allocator to accommodate the new size
        sharedAllocator->resize(dstStride * realHeight);
    }
    if (height < 0) {
        dstBuffer = { sharedAllocator->data(), (uint32_t)realHeight, (uint32_t)width, (size_t)dstStride };
    } else {
        dstBuffer = { dst, (uint32_t)realHeight, (uint32_t)width, (size_t)dstStride };
    }

    vImage_Buffer yBuffer = { (void*)srcY, (vImagePixelCount)realHeight, (vImagePixelCount)width, (size_t)srcYStride };
    vImage_Buffer uvBuffer = { (void*)srcUV, (vImagePixelCount)(realHeight / 2), (vImagePixelCount)(width / 2), (size_t)srcUVStride };

    // Generate conversion descriptor
    vImage_YpCbCrToARGB info;
    vImage_Error err = vImageConvert_YpCbCrToARGB_GenerateConversion(matrix, range, &info,
                                                                     kvImage420Yp8_CbCr8, // NV12 format
                                                                     kvImageARGB8888,     // Output format
                                                                     kvImageNoFlags);
    if (err != kvImageNoError) {
        CCAP_LOG_E("vImageConvert_YpCbCrToARGB_GenerateConversion failed: %zu", err);
        return;
    }

    // Execute NV12 to BGRA8888 conversion
    err = vImageConvert_420Yp8_CbCr8ToARGB8888(&yBuffer, &uvBuffer, &dstBuffer, &info,
                                               permuteMap, // No premultiplied alpha
                                               255,        // Alpha channel fully opaque
                                               kvImageNoFlags);

    if (err != kvImageNoError) {
        CCAP_LOG_E("vImageConvert_420Yp8_CbCr8ToARGB8888 failed: %zu", err);
        return;
    }

    if (height < 0) { // Complete flip and pixel reordering, write final result to dst. Even with two-step operation, it's still much faster than regular CPU operations.
        verticalFlip_apple((const uint8_t*)dstBuffer.data, dstStride, dst, dstStride, realHeight);
    }
}

void nv12ToRgbColor_apple(const uint8_t* srcY, int srcYStride,
                          const uint8_t* srcUV, int srcUVStride,
                          uint8_t* dst, int dstStride,
                          int width, int height,
                          ConvertFlag flag, ccap::PixelFormat targetPixelFormat) {
    // @refitem <https://developer.apple.com/documentation/accelerate/vimage_ypcbcrpixelrange?language=objc>

    // Video Range (unclamped)
    constexpr vImage_YpCbCrPixelRange videoRange = { 16,  // Yp_bias
                                                     128, // CbCr_bias
                                                     235, // YpRangeMax
                                                     240, // CbCrRangeMax
                                                     255, // YpMax
                                                     0,   // YpMin
                                                     255, // CbCrMax
                                                     1 }; // CbCrMin

    // Full Range
    constexpr vImage_YpCbCrPixelRange fullRange = { 0,   // Yp_bias
                                                    128, // CbCr_bias
                                                    255, // YpRangeMax
                                                    255, // CbCrRangeMax
                                                    255, // YpMax
                                                    1,   // YpMin
                                                    255, // CbCrMax
                                                    0 }; // CbCrMin

    const auto* range = (flag & ConvertFlag::FullRange) ? &fullRange : &videoRange;

    // Select color space matrix (typically NV12 uses BT.601, video range)
    const vImage_YpCbCrToARGBMatrix* matrix = (flag & ConvertFlag::BT601) ? kvImage_YpCbCrToARGBMatrix_ITU_R_601_4 : kvImage_YpCbCrToARGBMatrix_ITU_R_709_2;

    auto allocator = ccap::getSharedAllocator();
    auto realHeight = std::abs(height);
    bool hasAlpha = ccap::pixelFormatInclude(targetPixelFormat, ccap::kPixelFormatAlphaColorBit);
    auto argbStride = (4 * width + 31) & ~31; // 32-byte alignment

    if (!hasAlpha && allocator->size() < argbStride * realHeight) {
        allocator->resize(argbStride * realHeight); // Ensure allocator has sufficient space
    }

    uint8_t* argbData = hasAlpha ? dst : allocator->data(); // If no alpha channel, use shared allocator data

    uint8_t permuteMap[4]; /// Convert from ARGB to other formats.
    if (ccap::pixelFormatInclude(targetPixelFormat, ccap::kPixelFormatBGRBit)) {
        // BGR or BGRA
        permuteMap[0] = 3; // B
        permuteMap[1] = 2; // G
        permuteMap[2] = 1; // R
        permuteMap[3] = 0; // A (if exists)
    } else {
        // RGB or RGBA
        permuteMap[0] = 1; // R
        permuteMap[1] = 2; // G
        permuteMap[2] = 3; // B
        permuteMap[3] = 0; // A (if exists)
    }

    nv12ToArgb32_apple_imp(srcY, srcYStride, srcUV, srcUVStride, argbData, argbStride, width, height, matrix, range, permuteMap);

    if (!hasAlpha) {
        vImage_Buffer srcBuffer = { argbData, (vImagePixelCount)realHeight, (vImagePixelCount)width, (size_t)argbStride };
        vImage_Buffer dstBuffer = { dst, (vImagePixelCount)realHeight, (vImagePixelCount)width, (size_t)dstStride };
        vImageConvert_RGBA8888toRGB888(&srcBuffer, &dstBuffer, kvImageNoFlags);
    }
}

void nv12ToBgra32_apple(const uint8_t* srcY, int srcYStride,
                        const uint8_t* srcUV, int srcUVStride,
                        uint8_t* dst, int dstStride,
                        int width, int height, ConvertFlag flag) {
    nv12ToRgbColor_apple(srcY, srcYStride, srcUV, srcUVStride,
                         dst, dstStride, width, height, flag, ccap::PixelFormat::BGRA32);
}

void nv12ToRgba32_apple(const uint8_t* srcY, int srcYStride,
                        const uint8_t* srcUV, int srcUVStride,
                        uint8_t* dst, int dstStride,
                        int width, int height, ConvertFlag flag) {
    nv12ToRgbColor_apple(srcY, srcYStride, srcUV, srcUVStride,
                         dst, dstStride, width, height, flag, ccap::PixelFormat::RGBA32);
}

void nv12ToBgr24_apple(const uint8_t* srcY, int srcYStride,
                       const uint8_t* srcUV, int srcUVStride,
                       uint8_t* dst, int dstStride,
                       int width, int height, ConvertFlag flag) {
    nv12ToRgbColor_apple(srcY, srcYStride, srcUV, srcUVStride,
                         dst, dstStride, width, height, flag, ccap::PixelFormat::BGR24);
}

void nv12ToRgb24_apple(const uint8_t* srcY, int srcYStride,
                       const uint8_t* srcUV, int srcUVStride,
                       uint8_t* dst, int dstStride,
                       int width, int height, ConvertFlag flag) {
    nv12ToRgbColor_apple(srcY, srcYStride, srcUV, srcUVStride,
                         dst, dstStride, width, height, flag, ccap::PixelFormat::RGB24);
}

//////////////////////  I420 to BGRA8888 //////////////////////

void i420ToBgra32_apple_imp(const uint8_t* srcY, int srcYStride,
                            const uint8_t* srcU, int srcUStride,
                            const uint8_t* srcV, int srcVStride,
                            uint8_t* dst, int dstStride,
                            int width, int height,
                            const vImage_YpCbCrToARGBMatrix* matrix,
                            const vImage_YpCbCrPixelRange* range,
                            const uint8_t permuteMap[4]) {
    assert(width > 0 && height != 0); // Internal implementation
    // Construct vImage_Buffer
    vImage_Buffer dstBuffer;
    std::shared_ptr<ccap::Allocator> sharedAllocator = ccap::getSharedAllocator();
    assert(sharedAllocator != nullptr);
    auto realHeight = std::abs(height);

    if (dstStride * realHeight > sharedAllocator->size()) {
        // Resize the shared allocator to accommodate the new size
        sharedAllocator->resize(dstStride * realHeight);
    }

    if (height < 0) {
        dstBuffer = { sharedAllocator->data(), (uint32_t)realHeight, (uint32_t)width, (size_t)dstStride };
    } else {
        dstBuffer = { dst, (uint32_t)realHeight, (uint32_t)width, (size_t)dstStride };
    }

    vImage_Buffer yBuffer = { (void*)srcY, (vImagePixelCount)realHeight, (vImagePixelCount)width, (size_t)srcYStride };
    vImage_Buffer uBuffer = { (void*)srcU, (vImagePixelCount)(realHeight / 2), (vImagePixelCount)(width / 2), (size_t)srcUStride };
    vImage_Buffer vBuffer = { (void*)srcV, (vImagePixelCount)(realHeight / 2), (vImagePixelCount)(width / 2), (size_t)srcVStride };

    vImage_YpCbCrToARGB info;
    vImage_Error err = vImageConvert_YpCbCrToARGB_GenerateConversion(matrix, range, &info,
                                                                     kvImage420Yp8_Cb8_Cr8, // I420 format
                                                                     kvImageARGB8888,       // Output format
                                                                     kvImageNoFlags);
    if (err != kvImageNoError) {
        CCAP_LOG_E("vImageConvert_YpCbCrToARGB_GenerateConversion failed: %zu", err);
        return;
    }

    err = vImageConvert_420Yp8_Cb8_Cr8ToARGB8888(&yBuffer, &uBuffer, &vBuffer, &dstBuffer, &info, permuteMap, 255, kvImageNoFlags);

    if (err != kvImageNoError) {
        CCAP_LOG_E("vImageConvert_420Yp8_Cb8_Cr8ToARGB8888 failed: %zu", err);
        return;
    }

    if (height < 0) { // Complete flip and pixel reordering, write final result to dst. Even with two-step operation, it's still much faster than regular CPU operations.
        verticalFlip_apple((const uint8_t*)dstBuffer.data, dstStride, dst, dstStride, realHeight);
    }
}

void i420ToRgbColor_apple(const uint8_t* srcY, int srcYStride,
                          const uint8_t* srcU, int srcUStride,
                          const uint8_t* srcV, int srcVStride,
                          uint8_t* dst, int dstStride,
                          int width, int height,
                          ConvertFlag flag, ccap::PixelFormat targetPixelFormat) {
    // Video Range (unclamped)
    constexpr vImage_YpCbCrPixelRange videoRange = { 16,  // Yp_bias
                                                     128, // CbCr_bias
                                                     235, // YpRangeMax
                                                     240, // CbCrRangeMax
                                                     255, // YpMax
                                                     0,   // YpMin
                                                     255, // CbCrMax
                                                     1 }; // CbCrMin
    // Full Range
    constexpr vImage_YpCbCrPixelRange fullRange = { 0,   // Yp_bias
                                                    128, // CbCr_bias
                                                    255, // YpRangeMax
                                                    255, // CbCrRangeMax
                                                    255, // YpMax
                                                    1,   // YpMin
                                                    255, // CbCrMax
                                                    0 }; // CbCrMin

    const auto* range = (flag & ConvertFlag::FullRange) ? &fullRange : &videoRange;
    const vImage_YpCbCrToARGBMatrix* matrix = (flag & ConvertFlag::BT601) ? kvImage_YpCbCrToARGBMatrix_ITU_R_601_4 : kvImage_YpCbCrToARGBMatrix_ITU_R_709_2;

    auto allocator = ccap::getSharedAllocator();
    auto realHeight = std::abs(height);
    bool hasAlpha = ccap::pixelFormatInclude(targetPixelFormat, ccap::kPixelFormatAlphaColorBit);
    auto argbStride = (4 * width + 31) & ~31; // 32-byte alignment

    if (!hasAlpha && allocator->size() < argbStride * realHeight) {
        allocator->resize(argbStride * realHeight); // Ensure allocator has sufficient space
    }

    uint8_t* argbData = hasAlpha ? dst : allocator->data(); // If no alpha channel, use shared allocator data

    uint8_t permuteMap[4]; /// Convert from ARGB to other formats.
    if (ccap::pixelFormatInclude(targetPixelFormat, ccap::kPixelFormatBGRBit)) {
        // BGR or BGRA
        permuteMap[0] = 3; // B
        permuteMap[1] = 2; // G
        permuteMap[2] = 1; // R
        permuteMap[3] = 0; // A (if exists)
    } else {
        // RGB or RGBA
        permuteMap[0] = 1; // R
        permuteMap[1] = 2; // G
        permuteMap[2] = 3; // B
        permuteMap[3] = 0; // A (if exists)
    }

    i420ToBgra32_apple_imp(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, argbData, argbStride, width, height, matrix, range, permuteMap);

    if (!hasAlpha) {
        vImage_Buffer srcBuffer = { argbData, (vImagePixelCount)realHeight, (vImagePixelCount)width, (size_t)argbStride };
        vImage_Buffer dstBuffer = { dst, (vImagePixelCount)realHeight, (vImagePixelCount)width, (size_t)dstStride };
        vImageConvert_RGBA8888toRGB888(&srcBuffer, &dstBuffer, kvImageNoFlags);
    }
}

void i420ToBgra32_apple(const uint8_t* srcY, int srcYStride,
                        const uint8_t* srcU, int srcUStride,
                        const uint8_t* srcV, int srcVStride,
                        uint8_t* dst, int dstStride,
                        int width, int height, ConvertFlag flag) {
    i420ToRgbColor_apple(srcY, srcYStride, srcU, srcUStride,
                         srcV, srcVStride, dst, dstStride,
                         width, height, flag, ccap::PixelFormat::BGRA32);
}

void i420ToRgba32_apple(const uint8_t* srcY, int srcYStride,
                        const uint8_t* srcU, int srcUStride,
                        const uint8_t* srcV, int srcVStride,
                        uint8_t* dst, int dstStride,
                        int width, int height, ConvertFlag flag) {
    i420ToRgbColor_apple(srcY, srcYStride, srcU, srcUStride,
                         srcV, srcVStride, dst, dstStride,
                         width, height, flag, ccap::PixelFormat::RGBA32);
}

void i420ToBgr24_apple(const uint8_t* srcY, int srcYStride,
                       const uint8_t* srcU, int srcUStride,
                       const uint8_t* srcV, int srcVStride,
                       uint8_t* dst, int dstStride,
                       int width, int height, ConvertFlag flag) {
    i420ToRgbColor_apple(srcY, srcYStride, srcU, srcUStride,
                         srcV, srcVStride, dst, dstStride,
                         width, height, flag, ccap::PixelFormat::BGR24);
}

void i420ToRgb24_apple(const uint8_t* srcY, int srcYStride,
                       const uint8_t* srcU, int srcUStride,
                       const uint8_t* srcV, int srcVStride,
                       uint8_t* dst, int dstStride,
                       int width, int height, ConvertFlag flag) {
    i420ToRgbColor_apple(srcY, srcYStride, srcU, srcUStride,
                         srcV, srcVStride, dst, dstStride,
                         width, height, flag, ccap::PixelFormat::RGB24);
}

} // namespace ccap

#endif