Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
avfvideobuffer.mm
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtMultimedia/private/qavfhelpers_p.h>
7#include <QtMultimedia/private/qvideotexturehelper_p.h>
8#include <QtGui/qopenglcontext.h>
9#include <QtGui/rhi/qrhi.h>
10#include <QtCore/qloggingcategory.h>
11
12#include <CoreVideo/CVMetalTexture.h>
13#include <CoreVideo/CVMetalTextureCache.h>
14
15#import <AVFoundation/AVFoundation.h>
16#import <Metal/Metal.h>
17
18QT_USE_NAMESPACE
19
20Q_STATIC_LOGGING_CATEGORY(qLcVideoBuffer, "qt.multimedia.darwin.videobuffer")
21
22AVFVideoBuffer::AVFVideoBuffer(AVFVideoSinkInterface *sink, QCFType<CVImageBufferRef> buffer)
23 : QHwVideoBuffer(sink->rhi() ? QVideoFrame::RhiTextureHandle : QVideoFrame::NoHandle),
24#if QT_CONFIG(opengl)
25 sink(sink),
26#endif
27 m_buffer(std::move(buffer))
28{
29 const bool rhiIsOpenGL = sink && sink->rhi() && sink->rhi()->backend() == QRhi::OpenGLES2;
30 m_format = QAVFHelpers::videoFormatForImageBuffer(m_buffer, rhiIsOpenGL);
31
32 if (sink && sink->rhi() && sink->rhi()->backend() == QRhi::Metal)
33 metalCache = sink->cvMetalTextureCache;
34}
35
37{
38 Q_ASSERT(m_mode == QVideoFrame::NotMapped);
39}
40
41AVFVideoBuffer::MapData AVFVideoBuffer::map(QVideoFrame::MapMode mode)
42{
43 MapData mapData;
44
45 if (m_mode == QVideoFrame::NotMapped) {
46 CVPixelBufferLockBaseAddress(m_buffer, mode == QVideoFrame::ReadOnly
47 ? kCVPixelBufferLock_ReadOnly
48 : 0);
49 m_mode = mode;
50 }
51
52 mapData.planeCount = CVPixelBufferGetPlaneCount(m_buffer);
53 Q_ASSERT(mapData.planeCount <= 3);
54
55 if (!mapData.planeCount) {
56 // single plane
57 mapData.bytesPerLine[0] = CVPixelBufferGetBytesPerRow(m_buffer);
58 mapData.data[0] = static_cast<uchar*>(CVPixelBufferGetBaseAddress(m_buffer));
59 mapData.dataSize[0] = CVPixelBufferGetDataSize(m_buffer);
60 mapData.planeCount = mapData.data[0] ? 1 : 0;
61 return mapData;
62 }
63
64 // For a bi-planar or tri-planar format we have to set the parameters correctly:
65 for (int i = 0; i < mapData.planeCount; ++i) {
66 mapData.bytesPerLine[i] = CVPixelBufferGetBytesPerRowOfPlane(m_buffer, i);
67 mapData.dataSize[i] = mapData.bytesPerLine[i]*CVPixelBufferGetHeightOfPlane(m_buffer, i);
68 mapData.data[i] = static_cast<uchar*>(CVPixelBufferGetBaseAddressOfPlane(m_buffer, i));
69 }
70
71 return mapData;
72}
73
75{
76 if (m_mode != QVideoFrame::NotMapped) {
77 CVPixelBufferUnlockBaseAddress(m_buffer, m_mode == QVideoFrame::ReadOnly
78 ? kCVPixelBufferLock_ReadOnly
79 : 0);
80 m_mode = QVideoFrame::NotMapped;
81 }
82}
83
84static MTLPixelFormat rhiTextureFormatToMetalFormat(QRhiTexture::Format f)
85{
86 switch (f) {
87 default:
88 case QRhiTexture::UnknownFormat:
89 return MTLPixelFormatInvalid;
90 case QRhiTexture::RGBA8:
91 return MTLPixelFormatRGBA8Unorm;
92 case QRhiTexture::BGRA8:
93 return MTLPixelFormatBGRA8Unorm;
94 case QRhiTexture::R8:
95 case QRhiTexture::RED_OR_ALPHA8:
96 return MTLPixelFormatR8Unorm;
97 case QRhiTexture::RG8:
98 return MTLPixelFormatRG8Unorm;
99 case QRhiTexture::R16:
100 return MTLPixelFormatR16Unorm;
101 case QRhiTexture::RG16:
102 return MTLPixelFormatRG16Unorm;
103 case QRhiTexture::RGBA16F:
104 return MTLPixelFormatRGBA16Float;
105 case QRhiTexture::RGBA32F:
106 return MTLPixelFormatRGBA32Float;
107 case QRhiTexture::R16F:
108 return MTLPixelFormatR16Float;
109 case QRhiTexture::R32F:
110 return MTLPixelFormatR32Float;
111 }
112}
113
114
116{
117 auto *textureDescription = QVideoTextureHelper::textureDescription(m_format.pixelFormat());
118 int bufferPlanes = CVPixelBufferGetPlaneCount(m_buffer);
119 if (plane > 0 && plane >= bufferPlanes)
120 return 0;
121
122 if (rhi.backend() == QRhi::Metal) {
123 if (!cvMetalTexture[plane]) {
124 size_t width = CVPixelBufferGetWidth(m_buffer);
125 size_t height = CVPixelBufferGetHeight(m_buffer);
126 QSize planeSize = textureDescription->rhiPlaneSize(QSize(width, height), plane, &rhi);
127
128 if (!metalCache) {
129 qWarning("cannot create texture, Metal texture cache was released?");
130 return {};
131 }
132
133 // Create a CoreVideo pixel buffer backed Metal texture image from the texture cache.
134 const auto pixelFormat = rhiTextureFormatToMetalFormat(textureDescription->rhiTextureFormat(plane, &rhi));
135 if (pixelFormat != MTLPixelFormatInvalid) {
136 // Passing invalid pixel format makes Metal API validation
137 // to crash (and also makes no sense at all).
138 auto ret = CVMetalTextureCacheCreateTextureFromImage(
139 kCFAllocatorDefault,
140 metalCache,
141 m_buffer, nil,
142 pixelFormat,
143 planeSize.width(), planeSize.height(),
144 plane,
145 &cvMetalTexture[plane]);
146
147 if (ret != kCVReturnSuccess)
148 qCWarning(qLcVideoBuffer) << "texture creation failed" << ret;
149 } else {
150 qCWarning(qLcVideoBuffer) << "requested invalid pixel format:"
151 << textureDescription->rhiTextureFormat(plane, &rhi);
152 }
153 }
154
155 return cvMetalTexture[plane] ? quint64(CVMetalTextureGetTexture(cvMetalTexture[plane])) : 0;
156 } else if (rhi.backend() == QRhi::OpenGLES2) {
157#if QT_CONFIG(opengl)
158#ifdef Q_OS_MACOS
159 CVOpenGLTextureCacheFlush(sink->cvOpenGLTextureCache, 0);
160 // Create a CVPixelBuffer-backed OpenGL texture image from the texture cache.
161 const CVReturn cvret = CVOpenGLTextureCacheCreateTextureFromImage(
162 kCFAllocatorDefault,
163 sink->cvOpenGLTextureCache,
164 m_buffer,
165 nil,
166 &cvOpenGLTexture);
167 if (cvret != kCVReturnSuccess)
168 qCWarning(qLcVideoBuffer) << "OpenGL texture creation failed" << cvret;
169
170 Q_ASSERT(CVOpenGLTextureGetTarget(cvOpenGLTexture) == GL_TEXTURE_RECTANGLE);
171 // Get an OpenGL texture name from the CVPixelBuffer-backed OpenGL texture image.
172 return CVOpenGLTextureGetName(cvOpenGLTexture);
173#endif
174#ifdef Q_OS_IOS
175 CVOpenGLESTextureCacheFlush(sink->cvOpenGLESTextureCache, 0);
176 // Create a CVPixelBuffer-backed OpenGL texture image from the texture cache.
177 const CVReturn cvret = CVOpenGLESTextureCacheCreateTextureFromImage(
178 kCFAllocatorDefault,
179 sink->cvOpenGLESTextureCache,
180 m_buffer,
181 nil,
182 GL_TEXTURE_2D,
183 GL_RGBA,
184 CVPixelBufferGetWidth(m_buffer),
185 CVPixelBufferGetHeight(m_buffer),
186 GL_RGBA,
187 GL_UNSIGNED_BYTE,
188 0,
189 &cvOpenGLESTexture);
190 if (cvret != kCVReturnSuccess)
191 qCWarning(qLcVideoBuffer) << "OpenGL ES texture creation failed" << cvret;
192
193 // Get an OpenGL texture name from the CVPixelBuffer-backed OpenGL texture image.
194 return CVOpenGLESTextureGetName(cvOpenGLESTexture);
195#endif
196#endif
197 }
198 return 0;
199}
static MTLPixelFormat rhiTextureFormatToMetalFormat(QRhiTexture::Format f)
MapData map(QVideoFrame::MapMode mode) override
Maps the planes of a video buffer to memory.
~AVFVideoBuffer() override
quint64 textureHandle(QRhi &, int plane) override
void unmap() override
Releases the memory mapped by the map() function.