Skip to content

computer-graphics-tools/core-video-tools

Repository files navigation

CoreVideoTools

Platform Compatibility Swift Version

Overview

CoreVideoTools offers a more idiomatic Swift interface to CoreVideo functionality, making it easier and safer to work with CVPixelBuffers, IOSurfaces, and related CoreVideo concepts in Swift code.

Please see the package's documentation for more detailed usage instructions.

CVPixelBuffer

There are a lot Swift wrappers over vanilla CVPixelBuffer C-style API:

Swifty API:

let width = pixelBuffer.width
let height = pixelBuffer.height
let format = pixelBuffer.cvPixelFormat
let bytesPerRow = pixelBuffer.bytesPerRow

Convenience Init:

let pixelBuffer = try CVPixelBuffer.create(
    width: 1920,
    height: 1080,
    cvPixelFormat: .type_32BGRA
)

Check out more examples in the Working With CVPixelBuffer.

IOSurface

Convenience Init:

let surface = try IOSurface.create(
    width: 1920,
    height: 1080,
    cvPixelFormat: .type_32BGRA,
    bytesPerElement: 4,
    bytesPerRow: 1920 * 4
)

For more detail, please checkout Working With IOSurface.

CVPixelFormat

While debuging Core Video objects, you need to understand what pixel format is used in them. To do this using vanilla API you are forced to find a matching OSType value, because OSType if basically a number. This project uses CVPixelFormat enum istead of vanilla OSType public vars which is much more handy, and you can easily get a description of a current pixel format.

let cvPixelFormat: CVPixelFormat = cvPixelBuffer.cvPixelFormat
let description = cvPixelFormat.description

CVReturn Result & CVError

There are some functions in Core Video that return a code which helps if the operation succeeded. This project aims to simplify this error checking. CVReturn Result and CVError types are used to wrap vanilla API with thowable functions.

Vanilla API:

let returnCode = CVPixelBufferLockBaseAddress(cvPixelBuffer, lockFlags)
guard returnCode == kCVReturnSuccess else { /* handle the error ... */ }

Swifty API:

try cvPixelBuffer.lockBaseAddress(lockFlags: lockFlags)

License

MetalTools is licensed under MIT license.