CVPixelBuffer Reference

Derived from
Framework
System/Library/Frameworks/CoreVideo.framework
Companion guide
Declared in
CVPixelBuffer.h

Overview

A Core Video pixel buffer is an image buffer that holds pixels in main memory. Applications generating frames, compressing or decompressing video, or using Core Image can all make use of Core Video pixel buffers.

Functions

CVPixelBufferCreate

Creates a single pixel buffer for a given size and pixel format.

CVReturn CVPixelBufferCreate (
   CFAllocatorRef allocator,
   size_t width,
   size_t height,
   OSType pixelFormatType,
   CFDictionaryRef pixelBufferAttributes,
   CVPixelBufferRef *pixelBufferOut
);
Parameters
allocator

The allocator to use to create the pixel buffer. Pass NULL to specify the default allocator.

width

Width of the pixel buffer, in pixels.

height

Height of the pixel buffer, in pixels.

pixelFormatType

The pixel format identified by its respective four-character code (type OSType).

pixelBufferAttributes

A dictionary with additional attributes for a pixel buffer. This parameter is optional. See “Pixel Buffer Attribute Keys” for more details.

pixelBufferOut

On output, pixelBufferOut points to the newly created pixel buffer.

Return Value

A Core Video result code. See Core Video Constants Reference for possible values.

Discussion

This function allocates the necessary memory based on the pixel dimensions, format, and extended pixels described in the pixel buffer’s attributes.

Some of the parameters specified in this call override equivalent pixel buffer attributes. For example, if you define the kCVPixelBufferWidth and kCVPixelBufferHeight keys in the pixel buffer attributes parameter (pixelBufferAttributes), these values are overridden by the width and height parameters.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferCreateResolvedAttributesDictionary

Takes an array of CFDictionary objects describing various pixel buffer attributes and tries to resolve them into a single dictionary.

CVReturn CVPixelBufferCreateResolvedAttributesDictionary (
   CFAllocatorRef allocator,
   CFArrayRef attributes,
   CFDictionaryRef *resolvedDictionaryOut
);
Parameters
allocator

The allocator to use to create the pixel buffer. Pass NULL to specify the default allocator.

attributes

An array of Core Foundation dictionaries containing pixel buffer attribute key-value pairs.

resolvedDictionaryOut

On output, resolvedDictionaryOut points to the consolidated dictionary.

Return Value

A Core Video result code. See Core Video Constants Reference for possible values.

Discussion

This call is useful when you need to resolve requirements between several potential clients of a buffer.

If two or more dictionaries contain the same key but different values, errors may occur. For example, the width and height attributes must match, but if the bytes-per-row (rowBytes) attributes differ, the least common multiple is taken. Mismatches in pixel format allocators or callbacks also cause an error.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferCreateWithBytes

Creates a pixel buffer for a given size and pixel format containing data specified by a memory location.

CVReturn CVPixelBufferCreateWithBytes (
   CFAllocatorRef allocator,
   size_t width,
   size_t height,
   OSType pixelFormatType,
   void *baseAddress,
   size_t bytesPerRow,
   CVPixelBufferReleaseBytesCallback releaseCallback,
   void *releaseRefCon,
   CFDictionaryRef pixelBufferAttributes,
   CVPixelBufferRef *pixelBufferOut
);
Parameters
allocator

The allocator to use to create this buffer. Pass NULL to specify the default allocator.

width

The width of the pixel buffer, in pixels.

height

The height of the pixel buffer, in pixels.

pixelFormatType

The pixel format identified by its respective four character code (type OSType).

baseAddress

A pointer to the base address of the memory storing the pixels.

bytesPerRow

The row bytes of the pixel storage memory.

releaseCallback

The callback function to be called when the pixel buffer is destroyed. This callback allows the owner of the pixels to free the memory. See CVPixelBufferReleaseBytesCallback for more information.

releaseRefCon

The user data identifying the pixel buffer. This value is passed to your pixel buffer release callback.

pixelBufferAttributes

A Core Foundation dictionary with additional attributes for a a pixel buffer. This parameter is optional. See “Pixel Buffer Attribute Keys” for more details.

pixelBufferOut

On output, pixelBufferOut points to the newly created pixel buffer.

Return Value

A Core Video result code. See Core Video Constants Reference for possible values.

Discussion

Some of the parameters specified in this call override equivalent pixel buffer attributes. For example, if you define the kCVPixelBufferWidth and kCVPixelBufferHeight keys in the pixel buffer attributes parameter (pixelBufferAttributes), these values are overridden by the width and height parameters.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferCreateWithPlanarBytes

Creates a single pixel buffer in planar format for a given size and pixel format containing data specified by a memory location.

CVReturn CVPixelBufferCreateWithPlanarBytes (
   CFAllocatorRef allocator,
   size_t width,
   size_t height,
   OSType pixelFormatType,
   void *dataPtr,
   size_t dataSize,
   size_t numberOfPlanes,
   void *planeBaseAddress[],
   size_t planeWidth[],
   size_t planeHeight[],
   size_t planeBytesPerRow[],
   CVPixelBufferReleasePlanarBytesCallback releaseCallback,
   void *releaseRefCon,
   CFDictionaryRef pixelBufferAttributes,
   CVPixelBufferRef *pixelBufferOut
);
Parameters
allocator

The allocator to use to create this buffer. Pass NULL to specify the default allocator.

width

The width of the pixel buffer, in pixels.

height

The height of the pixel buffer, in pixels.

pixelFormatType

The pixel format identified by its respective four-character code (type OSType).

dataPtr

A pointer to a plane descriptor block if applicable, or NULL if it is not.

dataSize

The size of the memory if the planes are contiguous, or NULL if it is not.

numberOfPlanes

The number of planes.

planeBaseAddress

The array of base addresses for the planes.

planeWidth

The array of plane widths.

planeHeight

The array of plane heights.

planeBytesPerRow

The array of plane bytes-per-row values.

releaseCallback

The callback function that gets called when the pixel buffer is destroyed. This callback allows the owner of the pixels to free the memory. See CVPixelBufferReleaseBytesCallback for more information.

releaseRefCon

A pointer to user data identifying the pixel buffer. This value is passed to your pixel buffer release callback.

pixelBufferAttributes

A dictionary with additional attributes for a a pixel buffer. This parameter is optional. See “Pixel Buffer Attribute Keys” for more details.

pixelBufferOut

On output, pixelBufferOut points to the newly created pixel buffer.

Return Value

A Core Video result code. See Core Video Constants Reference for possible values.

Discussion

Some of the parameters specified in this call override equivalent pixel buffer attributes. For example, if you define the kCVPixelBufferWidth and kCVPixelBufferHeight keys in the pixel buffer attributes parameter (pixelBufferAttributes), these values are overridden by the width and height parameters.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferFillExtendedPixels

Fills the extended pixels of the pixel buffer.

CVReturn CVPixelBufferFillExtendedPixels (
   CVPixelBufferRef pixelBuffer
);
Parameters
pixelBuffer

The pixel buffer whose extended pixels you want to fill.

Return Value

A Core Video result code. See Core Video Constants Reference for possible values.

Discussion

This function replicates edge pixels to fill the entire extended region of the image.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferGetBaseAddress

Returns the base address of the pixel buffer.

void * CVPixelBufferGetBaseAddress (
   CVPixelBufferRef pixelBuffer
);
Parameters
pixelBuffer

The pixel buffer whose base address you want to obtain.

Return Value

The base address of the pixels.

  • For chunky buffers, returns a pointer to the pixel at (0,0) in the buffer.

  • For planar buffers, returns a pointer to a PlanarComponentInfo structure (as defined by QuickTime in ImageCodec.h).

Discussion

Retrieving the base address for a pixel buffer requires that the buffer base address be locked via a successful call to CVPixelBufferLockBaseAddress.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferGetBaseAddressOfPlane

Returns the base address of the plane at the specified plane index.

void * CVPixelBufferGetBaseAddressOfPlane (
   CVPixelBufferRef pixelBuffer,
   size_t planeIndex
);
Parameters
pixelBuffer

The pixel buffer containing the plane whose base address you want to obtain.

planeIndex

The index of the plane.

Return Value

The base address of the plane, or NULL for nonplanar pixel buffers.

Discussion

Retrieving the base address for a pixel buffer requires that the buffer base address be locked by a successful call to CVPixelBufferLockBaseAddress.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferGetBytesPerRow

Returns the number of bytes per row of the pixel buffer.

size_t CVPixelBufferGetBytesPerRow (
   CVPixelBufferRef pixelBuffer
);
Parameters
pixelBuffer

The pixel buffer whose bytes-per-row value you want to obtain.

Return Value

The number of bytes per row of the image data. For planar buffers, this function returns a rowBytes value such that bytesPerRow * height covers the entire image, including all planes.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferGetBytesPerRowOfPlane

Returns the number of bytes per row for a plane at the specified index in the pixel buffer.

size_t CVPixelBufferGetBytesPerRowOfPlane (
   CVPixelBufferRef pixelBuffer,
   size_t planeIndex
);
Parameters
pixelBuffer

The pixel buffer containing the plane.

planeIndex

The index of the plane whose bytes-per-row value you want to obtain.

Return Value

The number of row bytes of the plane, or NULL for nonplanar pixel buffers.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferGetDataSize

Returns the data size for contiguous planes of the pixel buffer.

size_t CVPixelBufferGetDataSize (
   CVPixelBufferRef pixelBuffer
);
Parameters
pixelBuffer

The pixel buffer whose data size you want to obtain.

Return Value

The data size as specified in the call to CVPixelBufferCreateWithPlanarBytes.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferGetExtendedPixels

Returns the amount of extended pixel padding in the pixel buffer.

void CVPixelBufferGetExtendedPixels (
   CVPixelBufferRef pixelBuffer,
   size_t *extraColumnsOnLeft,
   size_t *extraColumnsOnRight,
   size_t *extraRowsOnTop,
   size_t *extraRowsOnBottom
);
Parameters
pixelBuffer

The pixel buffer whose extended pixel size you want to obtain.

extraColumnsOnLeft

On output, the pixel row padding to the left. May be NULL.

extraColumnsOnRight

On output, the pixel row padding to the right. May be NULL.

extraRowsOnTop

On output, the pixel row padding to the top. May be NULL.

extraRowsOnBottom

On output, the pixel row padding to the bottom. May be NULL.

Discussion

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferGetHeight

Returns the height of the pixel buffer.

size_t CVPixelBufferGetHeight (
   CVPixelBufferRef pixelBuffer
);
Parameters
pixelBuffer

The pixel buffer whose height you want to obtain.

Return Value

The buffer height, in pixels.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferGetHeightOfPlane

Returns the height of the plane at planeIndex in the pixel buffer.

size_t CVPixelBufferGetHeightOfPlane (
   CVPixelBufferRef pixelBuffer,
   size_t planeIndex
);
Parameters
pixelBuffer

The pixel buffer whose plane height you want to obtain.

planeIndex

The index of the plane.

Return Value

The height of the buffer, in pixels, or 0 for nonplanar pixel buffers.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferGetPixelFormatType

Returns the pixel format type of the pixel buffer.

OSType CVPixelBufferGetPixelFormatType (
   CVPixelBufferRef pixelBuffer
);
Parameters
pixelBuffer

The pixel buffer whose format type you want to obtain.

Return Value

A four-character code OSType identifier for the pixel format.

Discussion

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferGetPlaneCount

Returns number of planes of the pixel buffer.

size_t CVPixelBufferGetPlaneCount (
   CVPixelBufferRef pixelBuffer
);
Parameters
pixelBuffer

The pixel buffer whose plane count you want to obtain.

Return Value

The number of planes. Returns 0 for nonplanar pixel buffers.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferGetTypeID

Returns the Core Foundation ID of the pixel buffer type.

CFTypeID CVPixelBufferGetTypeID (
   void
);
Return Value

The Core Foundation ID for this type.

Availability
  • Available in OS X v10.4 and later.
Related Sample Code
Declared In
CVPixelBuffer.h

CVPixelBufferGetWidth

Returns the width of the pixel buffer.

size_t CVPixelBufferGetWidth (
   CVPixelBufferRef pixelBuffer
);
Parameters
pixelBuffer

The pixel buffer whose width you want to obtain.

Return Value

The width of the buffer, in pixels.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferGetWidthOfPlane

Returns the width of the plane at a given index in the pixel buffer.

size_t CVPixelBufferGetWidthOfPlane (
   CVPixelBufferRef pixelBuffer,
   size_t planeIndex
);
Parameters
pixelBuffer

The pixel buffer whose plane width you want to obtain.

planeIndex

The plane index that contains the plane’s width value.

Return Value

The width of the plane, in pixels, or 0 for nonplanar pixel buffers.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferIsPlanar

Determines whether the pixel buffer is planar.

Boolean CVPixelBufferIsPlanar (
   CVPixelBufferRef pixelBuffer
);
Parameters
pixelBuffer

The pixel buffer to check.

Return Value

Returns true if the pixel buffer was created using CVPixelBufferCreateWithPlanarBytes; otherwise, false.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferLockBaseAddress

Locks the base address of the pixel buffer.

CVReturn CVPixelBufferLockBaseAddress (
   CVPixelBufferRef pixelBuffer,
   CVOptionFlags lockFlags
);
Parameters
pixelBuffer

The pixel buffer whose base address you want to lock.

lockFlags

No options currently defined; pass 0.

Return Value

A Core Video result code. See Core Video Constants Reference for possible values.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferRelease

Releases a pixel buffer.

void CVPixelBufferRelease (
   CVPixelBufferRef texture
);
Parameters
buffer

The pixel buffer that you want to release.

Discussion

This function is equivalent to CFRelease, but is NULL safe.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferRetain

Retains a pixel buffer.

CVPixelBufferRef CVPixelBufferRetain (
   CVPixelBufferRef texture
);
Parameters
buffer

The pixel buffer that you want to retain.

Return Value

For convenience, the same pixel buffer you want to retain.

Discussion

This function is equivalent to CFRetain, but is NULL safe.

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferUnlockBaseAddress

Unlocks the base address of the pixel buffer.

CVReturn CVPixelBufferUnlockBaseAddress (
   CVPixelBufferRef pixelBuffer,
   CVOptionFlags unlockFlags
);
Parameters
pixelBuffer

The pixel buffer whose base address you want to unlock.

unlockFlags

No options currently defined; pass 0.

Return Value

A Core Video result code. See Core Video Constants Reference for possible values.

Discussion

Availability
  • Available in OS X v10.4 and later.
Declared In
CVPixelBuffer.h

Callbacks

CVPixelBufferReleaseBytesCallback

Defines a pointer to a pixel buffer release callback function, which is called when a pixel buffer created by CVPixelBufferCreateWithBytes is released.

typedef void (*CVPixelBufferReleaseBytesCallback)(
   void *releaseRefCon,
   const void *baseAddress
   );

You would declare a pixel buffer release callback named MyPixelBufferReleaseCallback like this:

void MyPixelBufferReleaseCallback(
   void *releaseRefCon,
   const void *baseAddress
   );

Parameters
releaseRefCon

A pointer to application-defined data. This pointer is the same as that passed in the releaseRefCon parameter of CVPixelBufferCreateWithBytes.

baseAddress

A pointer to the base address of the memory holding the pixels. This pointer is the same as that passed in the baseAddress parameter of CVPixelBufferCreateWithBytes.

Discussion

You use this callback to release the pixels and perform any other cleanup when a pixel buffer is released.

Availability
  • Available in OS X v10.3 and later.
Declared In
CVPixelBuffer.h

CVPixelBufferReleasePlanarBytesCallback

Defines a pointer to a pixel buffer release callback function, which is called when a pixel buffer created by CVPixelBufferCreateWithPlanarBytes is released.

typedef void (*CVPixelBufferReleasePlanarBytesCallback)(
   void *releaseRefCon,
   const void *dataPtr,
   size_t dataSize,
   size_t numberOfPlanes,
   const void *planeAddresses[]
   );

You would declare a callback named MyPixelBufferReleasePlanarBytes like this:

void MyPixelBufferReleasePlanarBytes)(
   void *releaseRefCon,
   const void *dataPtr,
   size_t dataSize,
   size_t numberOfPlanes,
   const void *planeAddresses[]
   );

Parameters
releaseRefCon

A pointer to application-defined data. This pointer is the same as that passed in the releaseRefCon parameter of CVPixelBufferCreateWithPlanarBytes.

dataPtr

A pointer to a plane descriptor block. This is the same pointer you passed to CVPixelBufferCreateWithPlanarBytes in the dataPtr parameter.

dataSize

The size value you passed to CVPixelBufferCreateWithPlanarBytes in the dataSize parameter.

numberOfPlanes

The number of planes value you passed to CVPixelBufferCreateWithPlanarBytes in the numberOfPlanes parameter.

planeAddresses

A pointer to the base plane address you passed to CVPixelBufferCreateWithPlanarBytes in the basePlaneAddress parameter.

Discussion

You use this callback to release the pixels and perform any other cleanup when a pixel buffer is released.

Availability
  • Available in OS X v10.3 and later.
Declared In
CVPixelBuffer.h

Data Types

CVPixelBufferRef

A reference to a Core Video pixel buffer object.

typedef CVImageBufferRef CVPixelBufferRef;
Discussion

The pixel buffer stores an image in main memory.

Availability
  • Available in OS X v10.3 and later.
Declared In
CVPixelBuffer.h

Constants

Pixel Buffer Attribute Keys

The attributes associated with a pixel buffer.

const CFStringRef kCVPixelBufferPixelFormatTypeKey;
   const CFStringRef kCVPixelBufferMemoryAllocatorKey;
   const CFStringRef kCVPixelBufferWidthKey;
   const CFStringRef kCVPixelBufferHeightKey;
   const CFStringRef kCVPixelBufferExtendedPixelsLeftKey;
   const CFStringRef kCVPixelBufferExtendedPixelsTopKey;
   const CFStringRef kCVPixelBufferExtendedPixelsRightKey;
   const CFStringRef kCVPixelBufferExtendedPixelsBottomKey;
   const CFStringRef kCVPixelBufferBytesPerRowAlignmentKey;
   const CFStringRef kCVPixelBufferCGBitmapContextCompatibilityKey;
   const CFStringRef kCVPixelBufferCGImageCompatibilityKey;
   const CFStringRef kCVPixelBufferOpenGLCompatibilityKey;
   const CFStringRef kCVPixelBufferIOSurfacePropertiesKey;
   const CFStringRef kCVPixelBufferPlaneAlignmentKey;
Constants
kCVPixelBufferPixelFormatTypeKey

The pixel format for this buffer (type CFNumber, or type CFArray containing an array of CFNumber types (actually type OSType)). For a listing of common pixel formats, see the QuickTime Ice Floe Dispatch 20.

Available in OS X v10.4 and later.

Declared in CVPixelBuffer.h.

kCVPixelBufferMemoryAllocatorKey

The allocator used with this buffer (type CFAllocatorRef).

Available in OS X v10.4 and later.

Declared in CVPixelBuffer.h.

kCVPixelBufferWidthKey

The width of the pixel buffer (type CFNumber).

Available in OS X v10.4 and later.

Declared in CVPixelBuffer.h.

kCVPixelBufferHeightKey

The height of the pixel buffer (type CFNumber).

Available in OS X v10.4 and later.

Declared in CVPixelBuffer.h.

kCVPixelBufferExtendedPixelsLeftKey

The number of pixels padding the left of the image (type CFNumber).

Available in OS X v10.4 and later.

Declared in CVPixelBuffer.h.

kCVPixelBufferExtendedPixelsTopKey

The number of pixels padding the top of the image (type CFNumber).

Available in OS X v10.4 and later.

Declared in CVPixelBuffer.h.

kCVPixelBufferExtendedPixelsRightKey

The number of pixels padding the right of the image (type CFNumber).

Available in OS X v10.4 and later.

Declared in CVPixelBuffer.h.

kCVPixelBufferExtendedPixelsBottomKey

The number of pixels padding the bottom of the image (type CFNumber).

Available in OS X v10.4 and later.

Declared in CVPixelBuffer.h.

kCVPixelBufferBytesPerRowAlignmentKey

Indicates the number of bytes per row in the pixel buffer (type CFNumber).

Available in OS X v10.4 and later.

Declared in CVPixelBuffer.h.

kCVPixelBufferCGBitmapContextCompatibilityKey

Indicates whether the pixel buffer is compatible with Core Graphics bitmap contexts (type CFBoolean).

Available in OS X v10.4 and later.

Declared in CVPixelBuffer.h.

kCVPixelBufferCGImageCompatibilityKey

Indicates whether the pixel buffer is compatible with CGImage types (type CFBoolean).

Available in OS X v10.4 and later.

Declared in CVPixelBuffer.h.

kCVPixelBufferOpenGLCompatibilityKey

Indicates whether the pixel buffer is compatible with OpenGL contexts (type CFBoolean).

Available in OS X v10.4 and later.

Declared in CVPixelBuffer.h.

kCVPixelBufferIOSurfacePropertiesKey

Indicates optional properties in the IOSurface framework (type CFDictionary). An empty dictionary indicates default values.

Presence of this key requests allocation via the IOSurface framework.

Available in OS X v10.6 and later.

Declared in CVPixelBuffer.h.

kCVPixelBufferPlaneAlignmentKey

Specifies the alignment of the planes within the buffer. Planes will start on a byte number which is a multiple of this value. (type CFNumber).

Available in OS X v10.5 and later.

Declared in CVPixelBuffer.h.

Discussion

You specify these keys in a Core Foundation dictionary when calling functions such as CVPixelBufferCreate.

Pixel Buffer Locking Flags

The flags to pass to CVPixelBufferLockBaseAddress and CVPixelBufferUnlockBaseAddress.

enum CVPixelBufferLockFlags {
   kCVPixelBufferLock_ReadOnly = 0x00000001,
};
Constants
kCVPixelBufferLock_ReadOnly

A read-only buffer.

If you are not going to modify the data while you hold the lock, you should set this flag to avoid potentially invalidating any existing caches of the buffer contents. This flag should be passed to both the lock and the unlock functions. Non symmetrical usage of this flag will result in undefined behavior.

Available in OS X v10.6 and later.

Declared in CVPixelBuffer.h.

Discussion

You should set these attributes when adding attachments to a CVBuffer object.


Did this document help you? Yes It's good, but... Not helpful...