1

我尝试在视频捕获管道中将 4:3 帧转换为 16:9 帧。转换后的帧需要进一步处理。因此我需要将覆盖框架保持为CVImageBufferRef. 我查看了这个堆栈溢出线程,并从中借鉴了一些想法 iOS - Scale and crop CMSampleBufferRef/CVImageBufferRef

这是我所做的:

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;
}

但是经过这个方法。如果我们从 Xcode 检查cropPixelBuffer。它看起来像一个损坏的图像。

在此处输入图像描述 原图是这样的

在此处输入图像描述

我想这可能是因为颜色格式是 NV12 yuv 像素格式 谢谢你的帮助

4

1 回答 1

1

如果您的图像数据是 yuv,那么您可能要处理多个平面数据(例如 multiple rowbytes, multiple baseAddrsand multiple widthheights 和 multiple crops),在这种情况下您将不得不使用

CVPixelBufferCreateWithPlanarBytes()

那么CVPixelBufferGetPixelFormatType(imageBuffer)返回什么?

于 2017-03-26T20:49:53.860 回答