ios - 以 640x480@60fps 快速拍摄视频

标签 ios video-capture metal

我想使用最少的资源以 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/

相关文章:

ios - 核心剧情: How do I set the x-axis go through zero and at the bottom and y-axis to zero and left?

java - 自动视频快照

ios - 如何将纹理传递给顶点着色器? (iOS 和 Metal )(IOAF 代码 5)

ios - OpenGL ES 到 Metal - 性能瓶颈差异

ios - 如何设置约束,以便 View 的宽度为 0(如果 View 不能至少达到某个宽度)

iphone - iOS 通用框架 (i386) 中的重复符号

ios - cocos3d:如何将阴影附加到 pod 对象

android - 忽略 EXTRA_OUTPUT 困境的视频捕获

opengl - 在视频中捕获 openGL 图形

ios - 在 MTLBuffer 的一部分上运行计算内核?