Skip to content

Instantly share code, notes, and snippets.

@bibhas
Forked from zintus/libyuv.cc
Created November 11, 2017 09:56
Show Gist options
  • Save bibhas/0235669e3f21cf04afa5dd1b3d654de7 to your computer and use it in GitHub Desktop.
Save bibhas/0235669e3f21cf04afa5dd1b3d654de7 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