我需要创建 a 的副本,CVPixelBufferRef以便能够使用副本中的值以按位方式操作原始像素缓冲区。我似乎无法通过CVPixelBufferCreate, 或CVPixelBufferCreateWithBytes.
根据这个问题,它也可以用 memcpy() 来完成。但是,没有解释如何实现这一点,并且无论如何都需要调用哪些核心视频库。
作为这个答案的后续问题.我试图用金属中的内核函数替换在CPU上运行的for循环,以并行化计算并加快性能.
我的功能基本上是一个卷积.由于我反复接收输入数组值的新数据(数据来自a AVCaptureSession),因此使用newBufferWithBytesNoCopy:length:options:deallocator:是创建MTLBuffer对象的合理选项.这是相关代码:
id <MTLBuffer> dataBuffer = [device newBufferWithBytesNoCopy:dataVector length:sizeof(dataVector) options:MTLResourceStorageModeShared deallocator:nil];
id <MTLBuffer> filterBuffer = [device newBufferWithBytesNoCopy:filterVector length:sizeof(filterVector) options:MTLResourceStorageModeShared deallocator:nil];
id <MTLBuffer> outBuffer = [device newBufferWithBytesNoCopy:outVector length:sizeof(outVector) options:MTLResourceStorageModeShared deallocator:nil];
Run Code Online (Sandbox Code Playgroud)
运行时,我收到以下错误:
断言失败`newBufferWithBytesNoCopy:指针0x16fd0bd48不是4096字节对齐的.
现在,我没有分配任何内存,但是(出于测试目的)只是创建一个固定大小的空浮点数并用随机数填充它.所以我的主要问题是:
如何以正确的方式分配这些浮动数组,以满足以下要求
该值必须导致页面对齐的内存区域.
另外,还有一些问题:
MTLBuffer使用该newBufferWithBytesNoCopy方法创建它是否有意义,或者复制数据在性能方面不是真正的问题?(我的实际数据将包含每个视频帧约43'000个浮点值.)MTLResourceStorageModeShared正确的选择MTLResourceOptionsAPI参考说
返回的新MTLBuffer对象的存储分配与指针输入值相同.现有的内存分配必须由单个VM区域覆盖,通常使用vm_allocate或mmap进行分配.特别禁止malloc分配的内存.
这是否仅适用于输出缓冲器,还是应该与使用的所有对象的存储分配MTLBuffer不能与做malloc?
我正在使用iPhone相机捕捉实时视频并将像素缓冲区馈送到进行某些对象识别的网络.以下是相关代码:(我不会发布用于设置AVCaptureSession
等的代码,因为这是非常标准的.)
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
OSType sourcePixelFormat = CVPixelBufferGetPixelFormatType( pixelBuffer );
int doReverseChannels;
if ( kCVPixelFormatType_32ARGB == sourcePixelFormat ) {
doReverseChannels = 1;
} else if ( kCVPixelFormatType_32BGRA == sourcePixelFormat ) {
doReverseChannels = 0;
} else {
assert(false);
}
const int sourceRowBytes = (int)CVPixelBufferGetBytesPerRow( pixelBuffer );
const int width = (int)CVPixelBufferGetWidth( pixelBuffer );
const int fullHeight = (int)CVPixelBufferGetHeight( pixelBuffer );
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
unsigned char* sourceBaseAddr = CVPixelBufferGetBaseAddress( pixelBuffer …Run Code Online (Sandbox Code Playgroud)