-(void)YUVPixelBuffer2RGB:(CMSampleBufferRef)sampleBuffer
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer, 0);
int width = (int) CVPixelBufferGetWidth(imageBuffer);
int height = (int) CVPixelBufferGetHeight(imageBuffer);
uint8_t *yBuffer =(uint8_t *) CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);;
size_t yPitch = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0);
uint8_t *cbCrBuffer =(uint8_t *) CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 1);
size_t cbCrPitch = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 1);
if (yBuffer == NULL)
{
NSLog(@"yBuffer is NULL");
return ;
}
int bytesPerPixel = 4;
for(int y = 0; y < height; y++) {// height
uint8_t *rgbBufferLine = &rgbBuffer[y * width * bytesPerPixel];
uint8_t *yBufferLine = &yBuffer[y* yPitch];
uint8_t *cbCrBufferLine = &cbCrBuffer[(y >> 1) * cbCrPitch];
for(int x = 0; x < width; x++) {//width
int16_t y = yBufferLine[x];
int16_t cb = cbCrBufferLine[x & ~1] - 128;
int16_t cr = cbCrBufferLine[x | 1] - 128;
uint8_t *rgbOutput = &rgbBufferLine[x*bytesPerPixel];
int16_t r = (int16_t)roundf( y + cr * 1.4 );
int16_t g = (int16_t)roundf( y + cb * -0.343 + cr * -0.711 );
int16_t b = (int16_t)roundf( y + cb * 1.765);
rgbOutput[0] = 0xff;
rgbOutput[1] = clamp(r);
rgbOutput[2] = clamp(g);
rgbOutput[3] = clamp(b);
}
}
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
}
网友评论