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 "qffmpeghwaccel_vaapi_p.h" |
5 | |
6 | #if !QT_CONFIG(vaapi) |
7 | #error "Configuration error" |
8 | #endif |
9 | |
10 | #include <va/va.h> |
11 | |
12 | #include <qvideoframeformat.h> |
13 | #include "qffmpegvideobuffer_p.h" |
14 | #include "private/qvideotexturehelper_p.h" |
15 | |
16 | #include <rhi/qrhi.h> |
17 | |
18 | #include <qguiapplication.h> |
19 | #include <qpa/qplatformnativeinterface.h> |
20 | |
21 | #include <qopenglfunctions.h> |
22 | |
23 | //#define VA_EXPORT_USE_LAYERS |
24 | |
25 | #if __has_include("drm/drm_fourcc.h") |
26 | #include <drm/drm_fourcc.h> |
27 | #elif __has_include("libdrm/drm_fourcc.h") |
28 | #include <libdrm/drm_fourcc.h> |
29 | #else |
30 | // keep things building without drm_fourcc.h |
31 | #define fourcc_code(a, b, c, d) ((uint32_t)(a) | ((uint32_t)(b) << 8) | \ |
32 | ((uint32_t)(c) << 16) | ((uint32_t)(d) << 24)) |
33 | |
34 | #define DRM_FORMAT_RGBA8888 fourcc_code('R', 'A', '2', '4') /* [31:0] R:G:B:A 8:8:8:8 little endian */ |
35 | #define DRM_FORMAT_RGB888 fourcc_code('R', 'G', '2', '4') /* [23:0] R:G:B little endian */ |
36 | #define DRM_FORMAT_RG88 fourcc_code('R', 'G', '8', '8') /* [15:0] R:G 8:8 little endian */ |
37 | #define DRM_FORMAT_ABGR8888 fourcc_code('A', 'B', '2', '4') /* [31:0] A:B:G:R 8:8:8:8 little endian */ |
38 | #define DRM_FORMAT_BGR888 fourcc_code('B', 'G', '2', '4') /* [23:0] B:G:R little endian */ |
39 | #define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */ |
40 | #define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */ |
41 | #define DRM_FORMAT_R16 fourcc_code('R', '1', '6', ' ') /* [15:0] R little endian */ |
42 | #define DRM_FORMAT_RGB565 fourcc_code('R', 'G', '1', '6') /* [15:0] R:G:B 5:6:5 little endian */ |
43 | #define DRM_FORMAT_RG1616 fourcc_code('R', 'G', '3', '2') /* [31:0] R:G 16:16 little endian */ |
44 | #define DRM_FORMAT_GR1616 fourcc_code('G', 'R', '3', '2') /* [31:0] G:R 16:16 little endian */ |
45 | #define DRM_FORMAT_BGRA1010102 fourcc_code('B', 'A', '3', '0') /* [31:0] B:G:R:A 10:10:10:2 little endian */ |
46 | #endif |
47 | |
48 | extern "C" { |
49 | #include <libavutil/hwcontext_vaapi.h> |
50 | } |
51 | |
52 | #include <va/va_drm.h> |
53 | #include <va/va_drmcommon.h> |
54 | |
55 | #include <EGL/egl.h> |
56 | #include <EGL/eglext.h> |
57 | |
58 | #include <unistd.h> |
59 | |
60 | #include <qloggingcategory.h> |
61 | |
62 | QT_BEGIN_NAMESPACE |
63 | |
64 | static Q_LOGGING_CATEGORY(qLHWAccelVAAPI, "qt.multimedia.ffmpeg.hwaccelvaapi" ); |
65 | |
66 | namespace QFFmpeg { |
67 | |
68 | static const quint32 *fourccFromPixelFormat(const QVideoFrameFormat::PixelFormat format) |
69 | { |
70 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN |
71 | const quint32 rgba_fourcc = DRM_FORMAT_ABGR8888; |
72 | const quint32 rg_fourcc = DRM_FORMAT_GR88; |
73 | const quint32 rg16_fourcc = DRM_FORMAT_GR1616; |
74 | #else |
75 | const quint32 rgba_fourcc = DRM_FORMAT_RGBA8888; |
76 | const quint32 rg_fourcc = DRM_FORMAT_RG88; |
77 | const quint32 rg16_fourcc = DRM_FORMAT_RG1616; |
78 | #endif |
79 | |
80 | // qCDebug(qLHWAccelVAAPI) << "Getting DRM fourcc for pixel format" << format; |
81 | |
82 | switch (format) { |
83 | case QVideoFrameFormat::Format_Invalid: |
84 | case QVideoFrameFormat::Format_IMC1: |
85 | case QVideoFrameFormat::Format_IMC2: |
86 | case QVideoFrameFormat::Format_IMC3: |
87 | case QVideoFrameFormat::Format_IMC4: |
88 | case QVideoFrameFormat::Format_SamplerExternalOES: |
89 | case QVideoFrameFormat::Format_Jpeg: |
90 | case QVideoFrameFormat::Format_SamplerRect: |
91 | return nullptr; |
92 | |
93 | case QVideoFrameFormat::Format_ARGB8888: |
94 | case QVideoFrameFormat::Format_ARGB8888_Premultiplied: |
95 | case QVideoFrameFormat::Format_XRGB8888: |
96 | case QVideoFrameFormat::Format_BGRA8888: |
97 | case QVideoFrameFormat::Format_BGRA8888_Premultiplied: |
98 | case QVideoFrameFormat::Format_BGRX8888: |
99 | case QVideoFrameFormat::Format_ABGR8888: |
100 | case QVideoFrameFormat::Format_XBGR8888: |
101 | case QVideoFrameFormat::Format_RGBA8888: |
102 | case QVideoFrameFormat::Format_RGBX8888: |
103 | case QVideoFrameFormat::Format_AYUV: |
104 | case QVideoFrameFormat::Format_AYUV_Premultiplied: |
105 | case QVideoFrameFormat::Format_UYVY: |
106 | case QVideoFrameFormat::Format_YUYV: |
107 | { |
108 | static constexpr quint32 format[] = { rgba_fourcc, 0, 0, 0 }; |
109 | return format; |
110 | } |
111 | |
112 | case QVideoFrameFormat::Format_Y8: |
113 | { |
114 | static constexpr quint32 format[] = { DRM_FORMAT_R8, 0, 0, 0 }; |
115 | return format; |
116 | } |
117 | case QVideoFrameFormat::Format_Y16: |
118 | { |
119 | static constexpr quint32 format[] = { DRM_FORMAT_R16, 0, 0, 0 }; |
120 | return format; |
121 | } |
122 | |
123 | case QVideoFrameFormat::Format_YUV420P: |
124 | case QVideoFrameFormat::Format_YUV422P: |
125 | case QVideoFrameFormat::Format_YV12: |
126 | { |
127 | static constexpr quint32 format[] = { DRM_FORMAT_R8, DRM_FORMAT_R8, DRM_FORMAT_R8, 0 }; |
128 | return format; |
129 | } |
130 | case QVideoFrameFormat::Format_YUV420P10: |
131 | { |
132 | static constexpr quint32 format[] = { DRM_FORMAT_R16, DRM_FORMAT_R16, DRM_FORMAT_R16, 0 }; |
133 | return format; |
134 | } |
135 | |
136 | case QVideoFrameFormat::Format_NV12: |
137 | case QVideoFrameFormat::Format_NV21: |
138 | { |
139 | static constexpr quint32 format[] = { DRM_FORMAT_R8, rg_fourcc, 0, 0 }; |
140 | return format; |
141 | } |
142 | |
143 | case QVideoFrameFormat::Format_P010: |
144 | case QVideoFrameFormat::Format_P016: |
145 | { |
146 | static constexpr quint32 format[] = { DRM_FORMAT_R16, rg16_fourcc, 0, 0 }; |
147 | return format; |
148 | } |
149 | } |
150 | return nullptr; |
151 | } |
152 | |
153 | class VAAPITextureSet : public TextureSet |
154 | { |
155 | public: |
156 | ~VAAPITextureSet(); |
157 | qint64 textureHandle(QRhi *, int plane) override { |
158 | return textures[plane]; |
159 | } |
160 | |
161 | QRhi *rhi = nullptr; |
162 | QOpenGLContext *glContext = nullptr; |
163 | int nPlanes = 0; |
164 | GLuint textures[4] = {}; |
165 | }; |
166 | |
167 | |
168 | VAAPITextureConverter::VAAPITextureConverter(QRhi *rhi) |
169 | : TextureConverterBackend(nullptr) |
170 | { |
171 | qCDebug(qLHWAccelVAAPI) << ">>>> Creating VAAPI HW accelerator" ; |
172 | |
173 | if (!rhi || rhi->backend() != QRhi::OpenGLES2) { |
174 | qWarning() << "VAAPITextureConverter: No rhi or non openGL based RHI" ; |
175 | this->rhi = nullptr; |
176 | return; |
177 | } |
178 | |
179 | auto *nativeHandles = static_cast<const QRhiGles2NativeHandles *>(rhi->nativeHandles()); |
180 | glContext = nativeHandles->context; |
181 | if (!glContext) { |
182 | qCDebug(qLHWAccelVAAPI) << " no GL context, disabling" ; |
183 | return; |
184 | } |
185 | const QString platform = QGuiApplication::platformName(); |
186 | QPlatformNativeInterface *pni = QGuiApplication::platformNativeInterface(); |
187 | eglDisplay = pni->nativeResourceForIntegration(QByteArrayLiteral("egldisplay" )); |
188 | qCDebug(qLHWAccelVAAPI) << " platform is" << platform << eglDisplay; |
189 | |
190 | if (!eglDisplay) { |
191 | qCDebug(qLHWAccelVAAPI) << " no egl display, disabling" ; |
192 | return; |
193 | } |
194 | eglImageTargetTexture2D = eglGetProcAddress(procname: "glEGLImageTargetTexture2DOES" ); |
195 | if (!eglDisplay) { |
196 | qCDebug(qLHWAccelVAAPI) << " no eglImageTargetTexture2D, disabling" ; |
197 | return; |
198 | } |
199 | |
200 | // everything ok, indicate that we can do zero copy |
201 | this->rhi = rhi; |
202 | } |
203 | |
204 | VAAPITextureConverter::~VAAPITextureConverter() |
205 | { |
206 | } |
207 | |
208 | //#define VA_EXPORT_USE_LAYERS |
209 | TextureSet *VAAPITextureConverter::getTextures(AVFrame *frame) |
210 | { |
211 | // qCDebug(qLHWAccelVAAPI) << "VAAPIAccel::getTextures"; |
212 | if (frame->format != AV_PIX_FMT_VAAPI || !eglDisplay) { |
213 | qCDebug(qLHWAccelVAAPI) << "format/egl error" << frame->format << eglDisplay; |
214 | return nullptr; |
215 | } |
216 | |
217 | if (!frame->hw_frames_ctx) |
218 | return nullptr; |
219 | |
220 | auto *ctx = avFrameDeviceContext(frame); |
221 | if (!ctx) |
222 | return nullptr; |
223 | |
224 | auto *vaCtx = (AVVAAPIDeviceContext *)ctx->hwctx; |
225 | auto vaDisplay = vaCtx->display; |
226 | if (!vaDisplay) { |
227 | qCDebug(qLHWAccelVAAPI) << " no VADisplay, disabling" ; |
228 | return nullptr; |
229 | } |
230 | |
231 | VASurfaceID vaSurface = (uintptr_t)frame->data[3]; |
232 | |
233 | VADRMPRIMESurfaceDescriptor prime = {}; |
234 | if (vaExportSurfaceHandle(dpy: vaDisplay, surface_id: vaSurface, |
235 | VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2, |
236 | VA_EXPORT_SURFACE_READ_ONLY | |
237 | #ifdef VA_EXPORT_USE_LAYERS |
238 | VA_EXPORT_SURFACE_SEPARATE_LAYERS, |
239 | #else |
240 | VA_EXPORT_SURFACE_COMPOSED_LAYERS, |
241 | #endif |
242 | descriptor: &prime) != VA_STATUS_SUCCESS) |
243 | { |
244 | qWarning() << "vaExportSurfaceHandle failed" ; |
245 | return nullptr; |
246 | } |
247 | |
248 | // Make sure all fd's in 'prime' are closed when we return from this function |
249 | QScopeGuard closeObjectsGuard([&prime]() { |
250 | for (uint32_t i = 0; i < prime.num_objects; ++i) |
251 | close(fd: prime.objects[i].fd); |
252 | }); |
253 | |
254 | // ### Check that prime.fourcc is what we expect |
255 | vaSyncSurface(dpy: vaDisplay, render_target: vaSurface); |
256 | |
257 | // qCDebug(qLHWAccelVAAPI) << "VAAPIAccel: vaSufraceDesc: width/height" << prime.width << prime.height << "num objects" |
258 | // << prime.num_objects << "num layers" << prime.num_layers; |
259 | |
260 | QOpenGLFunctions functions(glContext); |
261 | |
262 | AVPixelFormat fmt = HWAccel::format(frame); |
263 | bool needsConversion; |
264 | auto qtFormat = QFFmpegVideoBuffer::toQtPixelFormat(avPixelFormat: fmt, needsConversion: &needsConversion); |
265 | auto *drm_formats = fourccFromPixelFormat(format: qtFormat); |
266 | if (!drm_formats || needsConversion) { |
267 | qWarning() << "can't use DMA transfer for pixel format" << fmt << qtFormat; |
268 | return nullptr; |
269 | } |
270 | |
271 | auto *desc = QVideoTextureHelper::textureDescription(format: qtFormat); |
272 | int nPlanes = 0; |
273 | for (; nPlanes < 5; ++nPlanes) { |
274 | if (drm_formats[nPlanes] == 0) |
275 | break; |
276 | } |
277 | Q_ASSERT(nPlanes == desc->nplanes); |
278 | nPlanes = desc->nplanes; |
279 | // qCDebug(qLHWAccelVAAPI) << "VAAPIAccel: nPlanes" << nPlanes; |
280 | |
281 | rhi->makeThreadLocalNativeContextCurrent(); |
282 | |
283 | EGLImage images[4]; |
284 | GLuint glTextures[4] = {}; |
285 | functions.glGenTextures(n: nPlanes, textures: glTextures); |
286 | for (int i = 0; i < nPlanes; ++i) { |
287 | #ifdef VA_EXPORT_USE_LAYERS |
288 | #define LAYER i |
289 | #define PLANE 0 |
290 | if (prime.layers[i].drm_format != drm_formats[i]) { |
291 | qWarning() << "expected DRM format check failed expected" |
292 | << Qt::hex << drm_formats[i] << "got" << prime.layers[i].drm_format; |
293 | } |
294 | #else |
295 | #define LAYER 0 |
296 | #define PLANE i |
297 | #endif |
298 | |
299 | EGLAttrib img_attr[] = { |
300 | EGL_LINUX_DRM_FOURCC_EXT, (EGLint)drm_formats[i], |
301 | EGL_WIDTH, desc->widthForPlane(width: frame->width, plane: i), |
302 | EGL_HEIGHT, desc->heightForPlane(height: frame->height, plane: i), |
303 | EGL_DMA_BUF_PLANE0_FD_EXT, prime.objects[prime.layers[LAYER].object_index[PLANE]].fd, |
304 | EGL_DMA_BUF_PLANE0_OFFSET_EXT, (EGLint)prime.layers[LAYER].offset[PLANE], |
305 | EGL_DMA_BUF_PLANE0_PITCH_EXT, (EGLint)prime.layers[LAYER].pitch[PLANE], |
306 | EGL_NONE |
307 | }; |
308 | images[i] = eglCreateImage(dpy: eglDisplay, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, buffer: nullptr, attrib_list: img_attr); |
309 | if (!images[i]) { |
310 | const GLenum error = eglGetError(); |
311 | if (error == EGL_BAD_MATCH) { |
312 | qWarning() << "eglCreateImage failed for plane" << i << "with error code EGL_BAD_MATCH, " |
313 | "disabling hardware acceleration. This could indicate an EGL implementation issue." |
314 | "\nVAAPI driver: " << vaQueryVendorString(dpy: vaDisplay) |
315 | << "\nEGL vendor:" << eglQueryString(dpy: eglDisplay, EGL_VENDOR); |
316 | this->rhi = nullptr; // Disabling texture conversion here to fix QTBUG-112312 |
317 | return nullptr; |
318 | } |
319 | if (error) { |
320 | qWarning() << "eglCreateImage failed for plane" << i << "with error code" << error; |
321 | return nullptr; |
322 | } |
323 | } |
324 | functions.glActiveTexture(GL_TEXTURE0 + i); |
325 | functions.glBindTexture(GL_TEXTURE_2D, texture: glTextures[i]); |
326 | |
327 | PFNGLEGLIMAGETARGETTEXTURE2DOESPROC eglImageTargetTexture2D = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)this->eglImageTargetTexture2D; |
328 | eglImageTargetTexture2D(GL_TEXTURE_2D, images[i]); |
329 | GLenum error = glGetError(); |
330 | if (error) |
331 | qWarning() << "eglImageTargetTexture2D failed with error code" << error; |
332 | } |
333 | |
334 | for (int i = 0; i < nPlanes; ++i) { |
335 | functions.glActiveTexture(GL_TEXTURE0 + i); |
336 | functions.glBindTexture(GL_TEXTURE_2D, texture: 0); |
337 | eglDestroyImage(dpy: eglDisplay, image: images[i]); |
338 | } |
339 | |
340 | VAAPITextureSet *textureSet = new VAAPITextureSet; |
341 | textureSet->nPlanes = nPlanes; |
342 | textureSet->rhi = rhi; |
343 | textureSet->glContext = glContext; |
344 | |
345 | for (int i = 0; i < 4; ++i) |
346 | textureSet->textures[i] = glTextures[i]; |
347 | // qCDebug(qLHWAccelVAAPI) << "VAAPIAccel: got textures" << textures[0] << textures[1] << textures[2] << textures[3]; |
348 | |
349 | return textureSet; |
350 | } |
351 | |
352 | VAAPITextureSet::~VAAPITextureSet() |
353 | { |
354 | if (rhi) { |
355 | rhi->makeThreadLocalNativeContextCurrent(); |
356 | QOpenGLFunctions functions(glContext); |
357 | functions.glDeleteTextures(n: nPlanes, textures); |
358 | } |
359 | } |
360 | |
361 | } |
362 | |
363 | QT_END_NAMESPACE |
364 | |