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_vaapi.cpp
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
5
6#include <QtFFmpegMediaPluginImpl/private/qffmpegvideobuffer_p.h>
7
8#if QT_CONFIG(linux_dmabuf_egl)
9# include <QtMultimedia/private/qdmabuftextureimporter_p.h>
10#endif
11#if QT_CONFIG(linux_dmabuf_vulkan)
12# include <QtMultimedia/private/qdmabufvulkanimporter_p.h>
13#endif
14#include <QtMultimedia/private/qmultimedia_drm_support_p.h>
15#include <QtMultimedia/private/qmultimedia_va_support_p.h>
16#include <QtMultimedia/private/qvideotexturehelper_p.h>
17
18#include <QtCore/qloggingcategory.h>
19
20extern "C" {
21#include <libavutil/hwcontext_vaapi.h>
22}
23
24#include <va/va.h>
25#include <va/va_drmcommon.h>
26
27#include <unistd.h>
28
29static_assert(QT_CONFIG(vaapi));
30
32
33Q_STATIC_LOGGING_CATEGORY(qLHWAccelVAAPI, "qt.multimedia.ffmpeg.hwaccelvaapi");
34
35namespace QFFmpeg {
36
37namespace {
38
39constexpr bool VAExportUseLayers = false;
40
41// Backend-agnostic export-and-import of an AV_PIX_FMT_VAAPI frame's VA surface, parameterized by
42// the dma-buf import context/function so the actual GL/Vulkan interop stays in the caller.
43template <typename Context, typename ImportFunc>
44QVideoFrameTexturesHandlesUPtr
45createVaapiTextureHandles(AVFrame *frame, TextureConverterBackend &converter,
46 const Context &context, ImportFunc &&importPlanes)
47{
48 QRhi &rhi = *converter.rhi;
49
50 using namespace QtMultimediaPrivate;
51 using VAStatus = QtMultimediaPrivate::VAStatus;
52
53 if (frame->format != AV_PIX_FMT_VAAPI || !frame->hw_frames_ctx)
54 return nullptr;
55
56 auto *ctx = avFrameDeviceContext(frame);
57 if (!ctx)
58 return nullptr;
59
60 auto *vaCtx = (AVVAAPIDeviceContext *)ctx->hwctx;
61 auto vaDisplay = vaCtx->display;
62 if (!vaDisplay) {
63 qCDebug(qLHWAccelVAAPI) << " no VADisplay, disabling";
64 return nullptr;
65 }
66
67 VASurfaceID vaSurface = (uintptr_t)frame->data[3];
68
69 VADRMPRIMESurfaceDescriptor prime = {};
70 const auto exportStatus =
71 vaExportSurfaceHandle(vaDisplay, vaSurface, VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2,
72 VA_EXPORT_SURFACE_READ_ONLY
73 | (VAExportUseLayers ? VA_EXPORT_SURFACE_SEPARATE_LAYERS
74 : VA_EXPORT_SURFACE_COMPOSED_LAYERS),
75 &prime);
76 if (exportStatus != VA_STATUS_SUCCESS) {
77 qWarning() << "vaExportSurfaceHandle failed:" << VAStatus(exportStatus);
78 return nullptr;
79 }
80
81 // Make sure all fd's in 'prime' are closed when we return from this function, i.e. after
82 // importPlanes() below has dup()'d the ones it needs.
83 QScopeGuard closeObjectsGuard([&prime]() {
84 for (uint32_t i = 0; i < prime.num_objects; ++i)
85 close(prime.objects[i].fd);
86 });
87
88 // ### Check that prime.fourcc is what we expect
89 vaSyncSurface(vaDisplay, vaSurface);
90
91 AVPixelFormat fmt = HWAccel::format(frame);
92 bool needsConversion;
93 auto qtFormat = QFFmpegVideoBuffer::toQtPixelFormat(fmt, &needsConversion);
94 QSpan<const DRMFormat> drm_formats = dmaBufFourccFromPixelFormat(qtFormat);
95 if (drm_formats.empty() || needsConversion) {
96 qWarning() << "can't use DMA transfer for pixel format" << fmt << qtFormat;
97 return nullptr;
98 }
99
100 auto *desc = QVideoTextureHelper::textureDescription(qtFormat);
101 int nPlanes = int(drm_formats.size());
102 Q_ASSERT(nPlanes == desc->nplanes);
103 nPlanes = desc->nplanes;
104
105 std::array<DmaBufPlane, 4> planes;
106 for (int i = 0; i < nPlanes; ++i) {
107 const int layer = VAExportUseLayers ? i : 0;
108 const int plane = VAExportUseLayers ? 0 : i;
109
110 if constexpr (VAExportUseLayers) {
111 if (prime.layers[i].drm_format != quint32(drm_formats[i])) {
112 qWarning() << "expected DRM format check failed expected" << Qt::hex
113 << drm_formats[i] << "got" << prime.layers[i].drm_format;
114 }
115 }
116
117 planes[i].fd = prime.objects[prime.layers[layer].object_index[plane]].fd;
118 planes[i].offset = prime.layers[layer].offset[plane];
119 planes[i].pitch = prime.layers[layer].pitch[plane];
120 planes[i].drmFormat = drm_formats[i];
121 planes[i].modifier = DRMModifier{
122 prime.objects[prime.layers[layer].object_index[plane]].drm_format_modifier
123 };
124 }
125
126 auto textureHandles = importPlanes(rhi, context, QSpan(planes.data(), nPlanes), qtFormat,
127 frameSize(*frame), converter.shared_from_this());
128 if (!textureHandles) {
129 if (textureHandles.error() == FailureSeverity::unrecoverable) {
130 qWarning() << "VAAPI driver:" << vaQueryVendorString(vaDisplay);
131 converter.rhi = nullptr; // Disabling texture conversion here to fix QTBUG-112312
132 }
133 return nullptr;
134 }
135 return std::move(*textureHandles);
136}
137
138#if QT_CONFIG(linux_dmabuf_egl)
139class VAAPIGLTextureConverter final : public TextureConverterBackend
140{
141public:
142 static std::shared_ptr<VAAPIGLTextureConverter> create(QRhi *rhi)
143 {
144 auto converter = std::shared_ptr<VAAPIGLTextureConverter>(new VAAPIGLTextureConverter(rhi));
145 if (!converter->m_context.isValid())
146 return nullptr;
147 return converter;
148 }
149
150 QVideoFrameTexturesHandlesUPtr
151 createTextureHandles(AVFrame *frame, QVideoFrameTexturesHandlesUPtr /*oldHandles*/) override
152 {
153 return createVaapiTextureHandles(frame, *this, m_context,
154 [](QRhi &rhi,
155 const QtMultimediaPrivate::DmaBufEglContext &eglContext,
156 QSpan<const QtMultimediaPrivate::DmaBufPlane> planes,
157 QVideoFrameFormat::PixelFormat qtFormat, QSize size,
158 std::shared_ptr<void> parentKeepAlive) {
159 return importDmaBufTextures(rhi, eglContext, planes, qtFormat, size,
160 std::move(parentKeepAlive));
161 });
162 }
163
164private:
165 explicit VAAPIGLTextureConverter(QRhi *rhi) : TextureConverterBackend(rhi), m_context(rhi) { }
166
167 QtMultimediaPrivate::DmaBufEglContext m_context;
168};
169#endif // QT_CONFIG(linux_dmabuf_egl)
170
171#if QT_CONFIG(linux_dmabuf_vulkan)
172class VAAPIVulkanTextureConverter final : public TextureConverterBackend
173{
174public:
175 static std::shared_ptr<VAAPIVulkanTextureConverter> create(QRhi *rhi)
176 {
177 auto converter =
178 std::shared_ptr<VAAPIVulkanTextureConverter>(new VAAPIVulkanTextureConverter(rhi));
179 if (!converter->m_context.isValid())
180 return nullptr;
181
182 return converter;
183 }
184
185 QVideoFrameTexturesHandlesUPtr
186 createTextureHandles(AVFrame *frame, QVideoFrameTexturesHandlesUPtr /*oldHandles*/) override
187 {
188 return createVaapiTextureHandles(frame, *this, m_context,
189 QtMultimediaPrivate::importDmaBufTexturesVulkan);
190 }
191
192private:
193 explicit VAAPIVulkanTextureConverter(QRhi *rhi) : TextureConverterBackend(rhi), m_context(rhi)
194 {
195 }
196
197 QtMultimediaPrivate::DmaBufVulkanContext m_context;
198};
199#endif // QT_CONFIG(linux_dmabuf_vulkan)
200
201} // namespace
202
203std::shared_ptr<TextureConverterBackend> VAAPITextureConverterFactory::create(QRhi *rhi)
204{
205 if (rhi) {
206 switch (rhi->backend()) {
207#if QT_CONFIG(linux_dmabuf_egl)
208 case QRhi::OpenGLES2:
209 return VAAPIGLTextureConverter::create(rhi);
210#endif
211#if QT_CONFIG(linux_dmabuf_vulkan)
212 case QRhi::Vulkan:
213 return VAAPIVulkanTextureConverter::create(rhi);
214#endif
215 default:
216 break;
217 }
218 }
219 return nullptr;
220}
221
222} // namespace QFFmpeg
223
224QT_END_NAMESPACE
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)