Skip to content

Instantly share code, notes, and snippets.

@zintus
Last active November 11, 2017 09:56
Show Gist options
  • Save zintus/edcec1f6b0ccb1574d7dc318a1a1e564 to your computer and use it in GitHub Desktop.
Save zintus/edcec1f6b0ccb1574d7dc318a1a1e564 to your computer and use it in GitHub Desktop.
Update CVImageBufferRef with pixels in capture format with bgra pixels using libyuv
// Function to update image buffer in NV12 format with BGRA pixel buffer
// Assumptions:
// bgra buffer is 32bpp
// bgra buffer width and height equal to imageBuffer
// bgra buffer stride is equal to imageBuffer width
int UpdateSampleBufferWithBGRAPixels(CVImageBufferRef imageBuffer, void *bgraPixels) {
const OSType pixelFormat = CVPixelBufferGetPixelFormatType(imageBuffer);
assert(pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange || pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange);
const size_t width = CVPixelBufferGetWidth(imageBuffer);
const size_t height = CVPixelBufferGetHeight(imageBuffer);
CVPixelBufferLockBaseAddress(imageBuffer, NULL);
const int retval = ARGBToNV12(bgraPixels, 4 * width,
CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0), CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0),
CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 1), CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 1),
width, height);
CVPixelBufferUnlockBaseAddress(imageBuffer, NULL);
return retval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment