1

I try to convert 4:3 frame to 16:9 frame in the video capture pipeline. And converted frame would need further processing. Therefore I need to keep covered frame as CVImageBufferRef. I looked at this stack overflow thread, and borrow some idea from it iOS - Scale and crop CMSampleBufferRef/CVImageBufferRef

here is what I did:

int cropX0 = 0, cropY0 = 60, cropHeight = 360, cropWidth = 640, outWidth = 640, outHeight = 360;
//get CVPixel buffer from CMSampleBuffer
CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(cmSampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);

size_t startpos = cropY0 * bytesPerRow;
void* cropStartAddr = ((char*)baseAddress) + startpos;

CVPixelBufferRef cropPixelBuffer = NULL;
int status = CVPixelBufferCreateWithBytes(kCFAllocatorDefault,
                                          outWidth,
                                          outHeight,
                                          CVPixelBufferGetPixelFormatType(imageBuffer),
                                          cropStartAddr,
                                          bytesPerRow,
                                          NULL, 
                                          0,
                                          NULL, 
                                          &cropPixelBuffer);
if(status == 0){
    OSStatus result = 0;
}

But after this method. if we inspect the cropPixelBuffer from Xcode. it looks like a corrupt image.

enter image description here The original picture looks like this

enter image description here

I think it could be because of the color format is NV12 yuv pixel format Thanks for your help

Community
  • 1
  • 1
user454083
  • 1,337
  • 3
  • 16
  • 31

1 Answers1

1

If your image data is yuv, then you might to deal with multiple plane data (e.g. multiple rowbytes, multiple baseAddrs and multiple width,heights and multiple crops) in which case you would have to use

CVPixelBufferCreateWithPlanarBytes()

so what does CVPixelBufferGetPixelFormatType(imageBuffer) return?

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159