我尝试了谷歌和stackoverflow但我似乎无法找到以 CVOpenGLESTexture 开头的函数的官方文档。我可以看到它们来自核心视频,我知道它们是在iOS 5上添加的,但搜索文档并没有给我任何东西。
我正在寻找有关参数的信息,他们做什么,如何使用它们等,就像在其他苹果框架中一样。
到目前为止,我所能做的就是命令点击它来查看信息,但这感觉非常奇怪。或者有没有办法添加它,以便它可以在xcode右侧的快速帮助中显示?
谢谢,对不起,如果这是一个愚蠢的问题。
PD:核心视频参考指南似乎也没有解释这些。
答案 0 :(得分:16)
不幸的是,确实没有关于这些新功能的任何文档。您现在要找到的最好的是CVOpenGLESTextureCache.h
头文件,您可以在其中看到函数参数的基本描述:
/*!
@function CVOpenGLESTextureCacheCreate
@abstract Creates a new Texture Cache.
@param allocator The CFAllocatorRef to use for allocating the cache. May be NULL.
@param cacheAttributes A CFDictionaryRef containing the attributes of the cache itself. May be NULL.
@param eaglContext The OpenGLES 2.0 context into which the texture objects will be created. OpenGLES 1.x contexts are not supported.
@param textureAttributes A CFDictionaryRef containing the attributes to be used for creating the CVOpenGLESTexture objects. May be NULL.
@param cacheOut The newly created texture cache will be placed here
@result Returns kCVReturnSuccess on success
*/
CV_EXPORT CVReturn CVOpenGLESTextureCacheCreate(
CFAllocatorRef allocator,
CFDictionaryRef cacheAttributes,
void *eaglContext,
CFDictionaryRef textureAttributes,
CVOpenGLESTextureCacheRef *cacheOut) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
更难的元素是属性字典,遗憾的是,您需要找到正确使用这些函数的示例。 Apple有GLCameraRipple和RosyWriter示例,展示了如何使用BGRA和YUV输入颜色格式的快速纹理上传路径。 Apple还在WWDC上提供了ChromaKey示例(可能仍然可以与视频一起访问),演示了如何使用这些纹理缓存从OpenGL ES纹理中提取信息。
我刚刚在我的GPUImage框架中获得了这种快速纹理上传(源代码可以在该链接中获得),所以我将列出我能够解析的内容。首先,我使用以下代码创建纹理缓存:
CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, (__bridge void *)[[GPUImageOpenGLESContext sharedImageProcessingOpenGLESContext] context], NULL, &coreVideoTextureCache);
if (err)
{
NSAssert(NO, @"Error at CVOpenGLESTextureCacheCreate %d");
}
其中提到的上下文是为OpenGL ES 2.0配置的EAGLContext。
我用它来保存视频内存中iOS设备相机的视频帧,我使用以下代码执行此操作:
CVPixelBufferLockBaseAddress(cameraFrame, 0);
CVOpenGLESTextureRef texture = NULL;
CVReturn err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, coreVideoTextureCache, cameraFrame, NULL, GL_TEXTURE_2D, GL_RGBA, bufferWidth, bufferHeight, GL_BGRA, GL_UNSIGNED_BYTE, 0, &texture);
if (!texture || err) {
NSLog(@"CVOpenGLESTextureCacheCreateTextureFromImage failed (error: %d)", err);
return;
}
outputTexture = CVOpenGLESTextureGetName(texture);
glBindTexture(GL_TEXTURE_2D, outputTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Do processing work on the texture data here
CVPixelBufferUnlockBaseAddress(cameraFrame, 0);
CVOpenGLESTextureCacheFlush(coreVideoTextureCache, 0);
CFRelease(texture);
outputTexture = 0;
这将从纹理缓存中创建一个新的CVOpenGLESTextureRef,表示OpenGL ES纹理。此纹理基于相机传入的CVImageBufferRef。然后从CVOpenGLESTextureRef中检索该纹理并为其设置适当的参数(这在我的处理中似乎是必要的)。最后,当我完成时,我会对纹理进行处理并清理。
这种快速上传过程对iOS设备产生了真正的影响。在iPhone 4S上上传和处理单个640x480视频帧的时间从9.0毫秒到1.8毫秒。
我也heard that this works in reverse,这可能允许在某些情况下替换glReadPixels()
,但我还没有尝试过。
答案 1 :(得分:2)