1.
2.
opengles
gpuimage
3.
toyuv
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sample);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
// 获取CVImageBufferRef中的y数据
UInt8 *pY = (UInt8 *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
// 获取CMVImageBufferRef中的uv数据
UInt8 *pUV = (UInt8 *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1);
size_t width = CVPixelBufferGetWidth(pixelBuffer);
size_t height = CVPixelBufferGetHeight(pixelBuffer);
size_t pYBytes = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0);
size_t pUVBytes = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 1);
UInt8 *pYUV420P = (UInt8 *)malloc(width * height * 3 / 2);
UInt8 *pU = pYUV420P + (width * height);
UInt8 *pV = pU + (width * height / 4);
for(int i = 0; i < height; i++) {
memcpy(pYUV420P + i * width, pY + i * pYBytes, width);
}
for(int j = 0; j < height / 2; j++) {
for(int i = 0; i < width / 2; i++) {
*(pU++) = pUV[i<<1];
*(pV++) = pUV[(i<<1) + 1];
}
pUV += pUVBytes;
}
free(pYUV420P);
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
topixelBuffer
- (CVPixelBufferRef) copyDataFromBuffer:(const unsigned char*)buffer toYUVPixelBufferWithWidth:(size_t)width Height:(size_t)height
{
uint8_t* nv12 = buffer;
NSDictionary *pixelAttributes = @{(NSString*)kCVPixelBufferIOSurfacePropertiesKey:@{}};
CVPixelBufferRef pixelBuffer = NULL;
CVReturn result = CVPixelBufferCreate(kCFAllocatorDefault,
width,
height,
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,
(__bridge CFDictionaryRef)(pixelAttributes),
&pixelBuffer);
size_t pYBytes = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0);
size_t pUVBytes = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 1);
CVPixelBufferLockBaseAddress(pixelBuffer,0);
unsigned char *yDestPlane = (unsigned char *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
unsigned char *y_ch0 = nv12;
unsigned char *y_ch1 = nv12 + width * height;
// memcpy(yDestPlane, y_ch0, width * height);
for(int i = 0; i < height; i++) {
memcpy(yDestPlane + i * pYBytes, y_ch0 + i * width, width);
}
unsigned char *uvDestPlane = (unsigned char *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1);
// memcpy(uvDestPlane, y_ch1, width * height/2);
UInt8 *pU = nv12 + (width * height);
UInt8 *pV = pU + (width * height / 4);
for(int j = 0; j < height / 2; j++) {
for(int i = 0; i < width / 2; i++) {
uvDestPlane[i<<1] = *(pU++) ;
uvDestPlane[(i<<1) + 1] = *(pV++);
}
uvDestPlane += pUVBytes;
}
// if (nv12) {
// free(nv12);
// }
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
if (result != kCVReturnSuccess) {
NSLog(@"Unable to create cvpixelbuffer %d", result);
}
return pixelBuffer;
}