gpt4 book ai didi

swift - 如何内存管理 CMSampleBuffer

转载 作者:可可西里 更新时间:2023-11-01 01:58:02 24 4
gpt4 key购买 nike

我通过以下方式从我的相机获取帧:

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard let imageBuffer: CVImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
}

来自苹果documentation ...

If you need to reference the CMSampleBuffer object outside of the scope of this method, you must CFRetain it and then CFRelease it when you are finished with it. 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.

在不显式设置 sampleBuffer = nil 的情况下保留对 CVImageBuffer 的引用是否可以?我问是因为最新版本的 Swift 自动内存管理 CF 数据结构,所以 CFRetainCFRelease 不可用。

此外,“对于未压缩的设备 native 捕获,内存块被尽可能少地复制,这种情况经常发生”背后的原因是什么。 ?为什么首先要复制一个内存块?

最佳答案

Is it okay to hold a reference to CVImageBuffer without explicitly setting sampleBuffer = nil?

如果您要保留对图像缓冲区的引用,那么保留对​​其“包含”CMSampleBuffer 的引用绝对不会有任何坏处。如果您保留对 CVImageBuffer 而不是 CMSampleBuffer 的引用,是否会完成“正确的事情”?也许吧。

Also, what is the reasoning behind "This is frequently the case for uncompressed device native capture where memory blocks are copied as little as possible." ? Why would a memory block be copied in the first place?

SO上有关于how to do a deep copy on an image CMSampleBuffer的问题,并且答案并不简单,因此无意中复制自己的内存块的可能性非常低。我认为本文档的目的是告诉您 AVCaptureVideoDataOutput 是高效的!如果您长时间卡在太多 CMSampleBuffer 上,这种效率(通过固定大小的帧池)可能会产生丢帧的令人惊讶的副作用,所以不要这样做。

但是这个警告有点多余,因为即使没有丢帧的问题,未压缩的视频 CMSampleBuffer 就其大小和频率而言已经是一个非常棘手的问题。您只需要引用几秒钟的值就可以用完千兆字节的 RAM,因此必须尽快处理它们,然后释放/清除对它们的任何引用。

关于swift - 如何内存管理 CMSampleBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49571001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com