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
qffmpeghwaccel_videotoolbox.mm
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtFFmpegMediaPluginImpl/private/qffmpeghwaccel_videotoolbox_p.h>
5
6#if !defined(Q_OS_DARWIN)
7#error "Configuration error"
8#endif
9
10#include <QtCore/qloggingcategory.h>
11
12#include <QtFFmpegMediaPluginImpl/private/qffmpegvideobuffer_p.h>
13
14#include <QtGui/qopenglcontext.h>
15#include <QtGui/rhi/qrhi.h>
16
17#include <QtMultimedia/private/qavfhelpers_p.h>
18#include <QtMultimedia/private/qvideotexturehelper_p.h>
19#include <QtMultimedia/qvideoframeformat.h>
20
21#include <CoreVideo/CVMetalTexture.h>
22#include <CoreVideo/CVMetalTextureCache.h>
23
24#ifdef Q_OS_MACOS
25#import <AppKit/AppKit.h>
26#endif
27#ifdef Q_OS_IOS
28#import <OpenGLES/EAGL.h>
29#endif
30#import <Metal/Metal.h>
31
33
34Q_STATIC_LOGGING_CATEGORY(qLcVideotoolbox, "qt.multimedia.ffmpeg.videotoolbox");
35
36namespace QFFmpeg
37{
38
39namespace {
40
41class VideoToolBoxTextureHandles : public QVideoFrameTexturesHandles
42{
43public:
44 ~VideoToolBoxTextureHandles();
45 quint64 textureHandle(QRhi &, int plane) override;
46
47 TextureConverterBackendPtr parentConverterBackend; // ensures the backend is deleted after the texture
48
49 QRhi *rhi = nullptr;
50 CVMetalTextureRef cvMetalTexture[3] = {};
51
52#if defined(Q_OS_MACOS)
53 CVOpenGLTextureRef cvOpenGLTexture;
54#elif defined(Q_OS_IOS)
55 CVOpenGLESTextureRef cvOpenGLESTexture;
56#endif
57
58 QAVFHelpers::QSharedCVPixelBuffer m_buffer;
59};
60}
61
64{
65 if (!rhi)
66 return;
67
68 if (rhi->backend() == QRhi::Metal) {
69 const auto *metal = static_cast<const QRhiMetalNativeHandles *>(rhi->nativeHandles());
70
71 // Create a Metal Core Video texture cache from the pixel buffer.
75 nil,
77 nil,
79 qWarning() << "Metal texture cache creation failed";
80 rhi = nullptr;
81 }
82 } else if (rhi->backend() == QRhi::OpenGLES2) {
83#if QT_CONFIG(opengl)
84#ifdef Q_OS_MACOS
85 const auto *gl = static_cast<const QRhiGles2NativeHandles *>(rhi->nativeHandles());
86
89
90 // Create an OpenGL CoreVideo texture cache from the pixel buffer.
93 nullptr,
94 reinterpret_cast<CGLContextObj>(nsGLContext.CGLContextObj),
96 nil,
98 qWarning() << "OpenGL texture cache creation failed";
99 rhi = nullptr;
100 }
101#endif
102#ifdef Q_OS_IOS
103 // Create an OpenGL CoreVideo texture cache from the pixel buffer.
106 nullptr,
108 nullptr,
110 qWarning() << "OpenGL texture cache creation failed";
111 rhi = nullptr;
112 }
113#endif
114#else
115 rhi = nullptr;
116#endif // QT_CONFIG(opengl)
117 }
118}
119
121{
123}
124
126{
127 cvMetalTextureCache = nullptr;
128#if defined(Q_OS_MACOS)
129 cvOpenGLTextureCache = nullptr;
130#elif defined(Q_OS_IOS)
131 cvOpenGLESTextureCache = nullptr;
132#endif
133}
134
136{
137 switch (f) {
138 default:
139 case QRhiTexture::UnknownFormat:
140 return MTLPixelFormatInvalid;
141 case QRhiTexture::RGBA8:
142 return MTLPixelFormatRGBA8Unorm;
143 case QRhiTexture::BGRA8:
144 return MTLPixelFormatBGRA8Unorm;
145 case QRhiTexture::R8:
146 case QRhiTexture::RED_OR_ALPHA8:
147 return MTLPixelFormatR8Unorm;
148 case QRhiTexture::RG8:
149 return MTLPixelFormatRG8Unorm;
150 case QRhiTexture::R16:
151 return MTLPixelFormatR16Unorm;
152 case QRhiTexture::RG16:
153 return MTLPixelFormatRG16Unorm;
154
155 case QRhiTexture::RGBA16F:
156 return MTLPixelFormatRGBA16Float;
157 case QRhiTexture::RGBA32F:
158 return MTLPixelFormatRGBA32Float;
159 case QRhiTexture::R16F:
160 return MTLPixelFormatR16Float;
161 case QRhiTexture::R32F:
162 return MTLPixelFormatR32Float;
163 }
164}
165
168 QVideoFrameTexturesHandlesUPtr /*oldHandles*/)
169{
170 if (!rhi)
171 return nullptr;
172
173 bool needsConversion = false;
175 if (needsConversion) {
176 // qDebug() << "XXXXXXXXXXXX pixel format needs conversion" << pixelFormat << HWAccel::format(frame);
177 return nullptr;
178 }
179
180 auto cvPixelBufferRef = reinterpret_cast<CVPixelBufferRef>(frame->data[3]);
182
189
192 // qDebug() << "XXXXX createTextureHandles" << pixelFormat << bufferPlanes << buffer;
193
194 if (rhi->backend() == QRhi::Metal) {
195 // First check that all planes have pixel-formats that we can handle,
196 // before we create any Metal textures.
197 for (int plane = 0; plane < bufferPlanes; ++plane) {
201 return nullptr;
202 }
203
204 for (int plane = 0; plane < bufferPlanes; ++plane) {
209
210 // Tested to be valid in prior loop.
213
214 // Create a CoreVideo pixel buffer backed Metal texture image from the texture cache.
219 nil,
221 width, height,
222 plane,
224
225 if (ret != kCVReturnSuccess)
226 qWarning() << "texture creation failed" << ret;
227// auto t = CVMetalTextureGetTexture(textureHandles->cvMetalTexture[plane]);
228// qDebug() << " metal texture for plane" << plane << "is" << quint64(textureHandles->cvMetalTexture[plane]) << width << height;
229// qDebug() << " " << t.iosurfacePlane << t.pixelFormat << t.width << t.height;
230 }
231 } else if (rhi->backend() == QRhi::OpenGLES2) {
232#if QT_CONFIG(opengl)
233#ifdef Q_OS_MACOS
235 // Create a CVPixelBuffer-backed OpenGL texture image from the texture cache.
240 nil,
242 if (cvret != kCVReturnSuccess) {
243 qCWarning(qLcVideotoolbox) << "OpenGL texture creation failed" << cvret;
244 return nullptr;
245 }
246
248#endif
249#ifdef Q_OS_IOS
251 // Create a CVPixelBuffer-backed OpenGL texture image from the texture cache.
256 nil,
258 GL_RGBA,
261 GL_RGBA,
263 0,
265 if (cvret != kCVReturnSuccess) {
266 qCWarning(qLcVideotoolbox) << "OpenGL ES texture creation failed" << cvret;
267 return nullptr;
268 }
269#endif
270#endif
271 }
272
273 return textureHandles;
274}
275
276VideoToolBoxTextureHandles::~VideoToolBoxTextureHandles()
277{
278}
279
280quint64 VideoToolBoxTextureHandles::textureHandle(QRhi &, int plane)
281{
282 if (rhi->backend() == QRhi::Metal)
283 return cvMetalTexture[plane] ? qint64(CVMetalTextureGetTexture(cvMetalTexture[plane])) : 0;
284#if QT_CONFIG(opengl)
285 Q_ASSERT(plane == 0);
286#ifdef Q_OS_MACOS
287 return CVOpenGLTextureGetName(cvOpenGLTexture);
288#endif
289#ifdef Q_OS_IOS
290 return CVOpenGLESTextureGetName(cvOpenGLESTexture);
291#endif
292#endif
293}
294
295}
296
297QT_END_NAMESPACE
static MTLPixelFormat rhiTextureFormatToMetalFormat(QRhiTexture::Format f)
std::conditional_t< QT_FFMPEG_AVIO_WRITE_CONST, const uint8_t *, uint8_t * > AvioWriteBufferType
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)