我想使用最少的资源以 60fps 的速度捕获视频,因为我需要每帧最多使用 16-20 毫秒,并且大部分时间将被帧上的大量计算占用。
我目前正在使用预设AVCaptureSessionPreset1280x720
,但我想以 640x480 进行计算,否则设备将跟不上。问题从这里开始:根据 Apple 允许开发人员执行的操作,我无法直接以 640x480@60fps 捕获,并且我当前的调整大小非常慢。
我正在使用 Metal 内核着色器调整图像大小,但 98% 的时间(在 Instruments 中看到)都花在这两行上:
[_inputTexture replaceRegion:region mipmapLevel:0 withBytes:inputImage.data bytesPerRow:inputImage.channels() * inputImage.cols];
...
[_outputTexture getBytes:outputImage.data bytesPerRow:inputImage.channels() * outputImage.cols fromRegion:outputRegion mipmapLevel:0];
基本上在内存加载/存储指令中。这部分让我感到困惑,因为理论上内存在 iPhone 上的 CPU 和 GPU 之间共享。
你认为 Metal 代码应该更快吗?我也有使用 Metal 的视频演示,它不会出汗(GPU 上约 1 毫秒,而调整大小需要约 20 毫秒)。
有没有更快的方法来调整图像大小?您对 Image I/O 的体验如何?
更新:
当我将工作组的大小增加到 22x22x1 时,图像大小调整的性能从 GPU 的 20 毫秒提高到了 8 毫秒。仍然不是我想要的,但更好。
更新 2:
我切换到CoreGraphics,它运行得足够快。见 this post .
最佳答案
我认为您已经从 Capture session 中创建了 UIImage,并且您必须将其转换为 Metal 纹理(MTLTexture)。但你不应该这样做。这是一个如何从 CaptureOutput 委托(delegate)获取 MTLTexture 的示例:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
id<MTLTexture> inTexture = nil;
size_t width = CVPixelBufferGetWidth(pixelBuffer);
size_t height = CVPixelBufferGetHeight(pixelBuffer);
MTLPixelFormat pixelFormat = MTLPixelFormatBGRA8Unorm;
CVMetalTextureRef metalTextureRef = NULL;
CVReturn status = CVMetalTextureCacheCreateTextureFromImage(NULL, _textureCache, pixelBuffer, NULL, pixelFormat, width, height, 0, &metalTextureRef);
if(status == kCVReturnSuccess) {
inTexture = CVMetalTextureGetTexture(metalTextureRef);
CFRelease(metalTextureRef);
}
CVPixelBufferUnlockBaseAddress(pixelBuffer,0);
}
关于ios - 以 640x480@60fps 快速拍摄视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29016732/