来自 iOS 相机的引用 CVPixelBuffer 的限制

Limit of referenced CVPixelBuffer coming from the iOS camera

我有一个应用程序,用于保存来自 iOS 相机的帧 (CVPixelBuffer)。我使用委托方法 -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer (CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection .

自从最近 iOS 更新后,如果前一次未释放,则不再有帧通过委托方法到达。现在 CVPixelBufferUnlockBaseAddressCVPixelBufferRelease 需要在新帧到来之前调用存储的帧。

知道这是为什么吗?我认为有一些硬件优化,但我找不到细节。

documentation for this delegate method 详细说明了这种情况:

To maintain optimal performance, some sample buffers directly reference pools of memory that may need to be reused by the device system and other capture inputs. This is frequently the case for uncompressed device native capture where memory blocks are copied as little as possible. If multiple sample buffers reference such pools of memory for too long, inputs will no longer be able to copy new samples into memory and those samples will be dropped.

If your application is causing samples to be dropped by retaining the provided CMSampleBufferRef objects for too long, but it needs access to the sample data for a long period of time, consider copying the data into a new buffer and then releasing the sample buffer (if it was previously retained) so that the memory it references can be reused.

这不足为奇。视频捕获系统不想花时间为每一帧分配新的内存。内存分配,尤其是大块的内存分配非常昂贵。