http://bezstellar.blog.163.com/blog/static/174868780201123035346108/
CGImageRef =>
CVPixelBufferRef
- (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
nil];
CVPixelBufferRef pxbuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, frameSize.width,
frameSize.height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options,
&pxbuffer);
NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
NSParameterAssert(pxdata != NULL);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pxdata, frameSize.width,
frameSize.height, 8, 4*frameSize.width, rgbColorSpace,
kCGImageAlphaNoneSkipFirst);
NSParameterAssert(context);
CGContextConcatCTM(context, frameTransform);
CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
CGImageGetHeight(image)), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
return pxbuffer;
}
CMSampleBufferRef => CVImageBufferRef [CMSampleBufferGetImageBuffer]
CVPixelBuffer(CVImageBuffer) =>
CMSampleBuffer
CVImageBufferRef cvimgRef = CMSampleBufferGetImageBuffer(sampleBuffer); CVPixelBufferLockBaseAddress(cvimgRef,0); uint8_t *buf=(uint8_t *)CVPixelBufferGetBaseAddress(cvimgRef); int width = 480; int height = 360; int bitmapBytesPerRow = width*4; int bitmapByteCount = bitmapBytesPerRow*height; CVPixelBufferRef pixelBufRef = NULL; CMSampleBufferRef newSampleBuffer = NULL; CMSampleTimingInfo timimgInfo = kCMTimingInfoInvalid; CMSampleBufferGetSampleTimingInfo(sampleBuffer, 0, &timimgInfo); OSStatus result = 0; OSType pixFmt = CVPixelBufferGetPixelFormatType(cvimgRef); CVPixelBufferCreateWithBytes(kCFAllocatorDefault, width, height, pixFmt, buf, bitmapBytesPerRow, NULL, NULL, NULL, &pixelBufRef); CMVideoFormatDescriptionRef videoInfo = NULL; result = CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixelBufRef, &videoInfo); CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBufRef, true, NULL, NULL, videoInfo, &timimgInfo, &newSampleBuffer);