6#include <CoreVideo/CVMetalTexture.h>
7#include <CoreVideo/CVMetalTextureCache.h>
8#include <QtGui/qopenglcontext.h>
10#include <private/qvideotexturehelper_p.h>
13#import <AVFoundation/AVFoundation.h>
14#import <Metal/Metal.h>
25 CVPixelBufferRetain(m_buffer);
33 for (
int i = 0;
i < 3; ++
i)
34 if (cvMetalTexture[
i])
35 CFRelease(cvMetalTexture[
i]);
36#if defined(Q_OS_MACOS)
38 CVOpenGLTextureRelease(cvOpenGLTexture);
39#elif defined(Q_OS_IOS)
40 if (cvOpenGLESTexture)
41 CFRelease(cvOpenGLESTexture);
43 CVPixelBufferRelease(m_buffer);
52 ? kCVPixelBufferLock_ReadOnly
63 mapData.
data[0] =
static_cast<uchar*
>(CVPixelBufferGetBaseAddress(m_buffer));
64 mapData.
size[0] = CVPixelBufferGetDataSize(m_buffer);
73 mapData.
data[
i] =
static_cast<uchar*
>(CVPixelBufferGetBaseAddressOfPlane(m_buffer,
i));
83 ? kCVPixelBufferLock_ReadOnly
94 return MTLPixelFormatInvalid;
96 return MTLPixelFormatRGBA8Unorm;
98 return MTLPixelFormatBGRA8Unorm;
100 return MTLPixelFormatR8Unorm;
102 return MTLPixelFormatRG8Unorm;
104 return MTLPixelFormatR16Unorm;
106 return MTLPixelFormatRG16Unorm;
109 return MTLPixelFormatRGBA16Float;
111 return MTLPixelFormatRGBA32Float;
113 return MTLPixelFormatR16Float;
115 return MTLPixelFormatR32Float;
123 int bufferPlanes = CVPixelBufferGetPlaneCount(m_buffer);
125 if (plane > 0 && plane >= bufferPlanes)
130 if (!cvMetalTexture[plane]) {
131 size_t width = CVPixelBufferGetWidth(m_buffer);
132 size_t height = CVPixelBufferGetHeight(m_buffer);
138 auto ret = CVMetalTextureCacheCreateTextureFromImage(
140 sink->cvMetalTextureCache,
145 &cvMetalTexture[plane]);
147 if (
ret != kCVReturnSuccess)
156 return cvMetalTexture[plane] ?
quint64(CVMetalTextureGetTexture(cvMetalTexture[plane])) : 0;
160 CVOpenGLTextureCacheFlush(
sink->cvOpenGLTextureCache, 0);
162 const CVReturn cvret = CVOpenGLTextureCacheCreateTextureFromImage(
164 sink->cvOpenGLTextureCache,
168 if (cvret != kCVReturnSuccess)
169 qWarning() <<
"OpenGL texture creation failed" << cvret;
173 return CVOpenGLTextureGetName(cvOpenGLTexture);
176 CVOpenGLESTextureCacheFlush(
sink->cvOpenGLESTextureCache, 0);
178 const CVReturn cvret = CVOpenGLESTextureCacheCreateTextureFromImage(
180 sink->cvOpenGLESTextureCache,
185 CVPixelBufferGetWidth(m_buffer),
186 CVPixelBufferGetHeight(m_buffer),
191 if (cvret != kCVReturnSuccess)
192 qWarning() <<
"OpenGL ES texture creation failed" << cvret;
195 return CVOpenGLESTextureGetName(cvOpenGLESTexture);
static MTLPixelFormat rhiTextureFormatToMetalFormat(QRhiTexture::Format f)
void unmap()
Releases the memory mapped by the map() function.
MapData map(QVideoFrame::MapMode mode)
Independently maps the planes of a video buffer to memory.
AVFVideoBuffer(AVFVideoSinkInterface *sink, CVImageBufferRef buffer)
virtual quint64 textureHandle(int plane) const
Returns a texture handle to the data buffer.
The QAbstractVideoBuffer class is an abstraction for video data. \inmodule QtMultimedia.
Format
Specifies the texture format.
Implementation backend() const
The QVideoFrame class represents a frame of video data.
MapMode
Enumerates how a video buffer's data is mapped to system memory.
QVideoFrameFormat videoFormatForImageBuffer(CVImageBufferRef buffer, bool openGL=false)
const TextureDescription * textureDescription(QVideoFrameFormat::PixelFormat format)
GLint GLsizei GLsizei height
#define GL_TEXTURE_RECTANGLE
GLsizei GLenum GLboolean sink
static QAbstractVideoBuffer::MapData mapData(const camera_frame_nv12_t &frame, unsigned char *baseAddress)
unsigned long long quint64
QRhiTexture::Format textureFormat[maxPlanes]
int widthForPlane(int width, int plane) const
int heightForPlane(int height, int plane) const