6#include <private/qvideotexturehelper_p.h>
7#include <qpa/qplatformnativeinterface.h>
8#include <qguiapplication.h>
9#include <QtCore/qloggingcategory.h>
11#include <gst/video/video.h>
12#include <gst/video/video-frame.h>
13#include <gst/video/gstvideometa.h>
14#include <gst/pbutils/gstpluginsbaseversion.h>
16#include <common/qgstutils_p.h>
18#if QT_CONFIG(gstreamer_gl)
19# include <QtGui/rhi/qrhi.h>
20# include <QtGui/qopenglcontext.h>
21# include <QtGui/qopenglfunctions.h>
22# include <QtGui/qopengl.h>
24# include <gst/gl/gstglconfig.h>
25# include <gst/gl/gstglmemory.h>
26# include <gst/gl/gstglsyncmeta.h>
28# if QT_CONFIG(gstreamer_gl_egl)
30# include <EGL/eglext.h>
33# if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
34# include <gst/allocators/gstdmabuf.h>
40Q_STATIC_LOGGING_CATEGORY(qLcGstVideoBuffer,
"qt.multimedia.gstreamer.videobuffer");
43#define fourcc_code(a, b, c, d) ((uint32_t)(a) | ((uint32_t)(b) << 8
) |
44 ((uint32_t)(c) << 16
) | ((uint32_t)(d) << 24
))
71QGstVideoBuffer::
QGstVideoBuffer(QGstBufferHandle buffer,
const GstVideoInfo &info,
78 m_memoryFormat(memoryFormat),
83#if QT_CONFIG(gstreamer_gl_egl)
85 eglDisplay = sink->eglDisplay();
86 eglImageTargetTexture2D = sink->eglImageTargetTexture2D();
89 Q_UNUSED(m_memoryFormat);
91 Q_UNUSED(eglImageTargetTexture2D);
96 Q_ASSERT(m_mode == QVideoFrame::NotMapped);
101 const GstMapFlags flags = GstMapFlags(((mode & QVideoFrame::ReadOnly) ? GST_MAP_READ : 0)
102 | ((mode & QVideoFrame::WriteOnly) ? GST_MAP_WRITE : 0));
105 if (mode == QVideoFrame::NotMapped || m_mode != QVideoFrame::NotMapped)
108 if (m_videoInfo.finfo->n_planes == 0) {
109 if (gst_buffer_map(m_buffer.get(), &m_frame.map[0], flags)) {
110 mapData.planeCount = 1;
111 mapData.bytesPerLine[0] = -1;
112 mapData.dataSize[0] = m_frame.map[0].size;
113 mapData.data[0] =
static_cast<uchar *>(m_frame.map[0].data);
117 }
else if (gst_video_frame_map(&m_frame, &m_videoInfo, m_buffer.get(), flags)) {
118 mapData.planeCount = GST_VIDEO_FRAME_N_PLANES(&m_frame);
120 for (guint i = 0; i < GST_VIDEO_FRAME_N_PLANES(&m_frame); ++i) {
121 mapData.bytesPerLine[i] = GST_VIDEO_FRAME_PLANE_STRIDE(&m_frame, i);
122 mapData.data[i] =
static_cast<uchar *>(GST_VIDEO_FRAME_PLANE_DATA(&m_frame, i));
123 mapData.dataSize[i] = mapData.bytesPerLine[i]*GST_VIDEO_FRAME_COMP_HEIGHT(&m_frame, i);
133 if (m_mode != QVideoFrame::NotMapped) {
134 if (m_videoInfo.finfo->n_planes == 0)
135 gst_buffer_unmap(m_buffer.get(), &m_frame.map[0]);
137 gst_video_frame_unmap(&m_frame);
139 m_mode = QVideoFrame::NotMapped;
147#if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
150fourccFromVideoInfo(
const GstVideoInfo * info,
int plane,
bool singleEGLImage)
152 GstVideoFormat format = GST_VIDEO_INFO_FORMAT (info);
153#if G_BYTE_ORDER == G_LITTLE_ENDIAN
154 const gint argb_fourcc = DRM_FORMAT_ARGB8888;
155 const gint rgba_fourcc = DRM_FORMAT_ABGR8888;
156 const gint rgb_fourcc = DRM_FORMAT_BGR888;
157 const gint rg_fourcc = DRM_FORMAT_GR88;
159 const gint argb_fourcc = DRM_FORMAT_BGRA8888;
160 const gint rgba_fourcc = DRM_FORMAT_RGBA8888;
161 const gint rgb_fourcc = DRM_FORMAT_RGB888;
162 const gint rg_fourcc = DRM_FORMAT_RG88;
165 qCDebug(qLcGstVideoBuffer) <<
"Getting DRM fourcc for"
166 << gst_video_format_to_string(format)
170 case GST_VIDEO_FORMAT_RGB16:
171 case GST_VIDEO_FORMAT_BGR16:
172 return DRM_FORMAT_RGB565;
174 case GST_VIDEO_FORMAT_RGB:
175 case GST_VIDEO_FORMAT_BGR:
178 case GST_VIDEO_FORMAT_BGRx:
179 case GST_VIDEO_FORMAT_BGRA:
182 case GST_VIDEO_FORMAT_AYUV:
183 if (singleEGLImage)
return DRM_FORMAT_AYUV;
185 case GST_VIDEO_FORMAT_RGBx:
186 case GST_VIDEO_FORMAT_RGBA:
187 case GST_VIDEO_FORMAT_ARGB:
188 case GST_VIDEO_FORMAT_xRGB:
189 case GST_VIDEO_FORMAT_ABGR:
190 case GST_VIDEO_FORMAT_xBGR:
193 case GST_VIDEO_FORMAT_GRAY8:
194 return DRM_FORMAT_R8;
196 case GST_VIDEO_FORMAT_YUY2:
197 return DRM_FORMAT_YUYV;
199 case GST_VIDEO_FORMAT_UYVY:
200 return DRM_FORMAT_UYVY;
202 case GST_VIDEO_FORMAT_GRAY16_LE:
203 case GST_VIDEO_FORMAT_GRAY16_BE:
204 if (singleEGLImage)
return DRM_FORMAT_R16;
207 case GST_VIDEO_FORMAT_NV12:
208 if (singleEGLImage)
return DRM_FORMAT_NV12;
210 case GST_VIDEO_FORMAT_NV21:
211 if (singleEGLImage)
return DRM_FORMAT_NV21;
212 return plane == 0 ? DRM_FORMAT_R8 : rg_fourcc;
214 case GST_VIDEO_FORMAT_I420:
215 if (singleEGLImage)
return DRM_FORMAT_YUV420;
217 case GST_VIDEO_FORMAT_YV12:
218 if (singleEGLImage)
return DRM_FORMAT_YVU420;
220 case GST_VIDEO_FORMAT_Y41B:
221 if (singleEGLImage)
return DRM_FORMAT_YUV411;
223 case GST_VIDEO_FORMAT_Y42B:
224 if (singleEGLImage)
return DRM_FORMAT_YUV422;
226 case GST_VIDEO_FORMAT_Y444:
227 if (singleEGLImage)
return DRM_FORMAT_YUV444;
228 return DRM_FORMAT_R8;
230#if GST_CHECK_PLUGINS_BASE_VERSION(1
,16
,0
)
231 case GST_VIDEO_FORMAT_BGR10A2_LE:
232 return DRM_FORMAT_BGRA1010102;
235 case GST_VIDEO_FORMAT_P010_10LE:
236 case GST_VIDEO_FORMAT_P010_10BE:
237 if (singleEGLImage)
return DRM_FORMAT_P010;
238 return plane == 0 ? DRM_FORMAT_R16 : DRM_FORMAT_RG1616;
241 qWarning() <<
"Unsupported format for DMABuf:" << gst_video_format_to_string(format);
247#if QT_CONFIG(gstreamer_gl)
252 std::array<guint32, QVideoTextureHelper::TextureDescription::maxPlanes> names{};
255class QGstQVideoFrameTextures :
public QVideoFrameTextures
258 QGstQVideoFrameTextures(QRhi *rhi,
260 QVideoFrameFormat::PixelFormat format,
261 GlTextures &textures,
262 QGstCaps::MemoryFormat memoryFormat)
264 , m_glTextures(textures)
266 QRhiTexture::Flags textureFlags = {};
267 if (QVideoTextureHelper::forceGlTextureExternalOesIsSet()
268 && m_rhi && rhi->backend() == QRhi::OpenGLES2)
269 textureFlags = {QRhiTexture::ExternalOES};
271 bool isDmaBuf = memoryFormat == QGstCaps::DMABuf;
272 auto fallbackPolicy = isDmaBuf
273 ? QVideoTextureHelper::TextureDescription::FallbackPolicy::Disable
274 : QVideoTextureHelper::TextureDescription::FallbackPolicy::Enable;
276 auto desc = QVideoTextureHelper::textureDescription(format);
277 for (uint i = 0; i < textures.count; ++i) {
279 QSize planeSize = desc->rhiPlaneSize(size, i, isDmaBuf ?
nullptr : m_rhi);
280 QRhiTexture::Format format = desc->rhiTextureFormat(i, m_rhi, fallbackPolicy);
281 m_textures[i].reset(rhi->newTexture(format, planeSize, 1, textureFlags));
282 m_textures[i]->createFrom({textures.names[i], 0});
286 ~QGstQVideoFrameTextures() override
288 m_rhi->makeThreadLocalNativeContextCurrent();
289 auto ctx = QOpenGLContext::currentContext();
290 if (m_glTextures.owned && ctx)
291 ctx->functions()->glDeleteTextures(
int(m_glTextures.count), m_glTextures.names.data());
294 QRhiTexture *texture(uint plane)
const override
296 return plane < m_glTextures.count ? m_textures[plane].get() :
nullptr;
300 QRhi *m_rhi =
nullptr;
301 GlTextures m_glTextures;
302 std::unique_ptr<QRhiTexture> m_textures[QVideoTextureHelper::TextureDescription::maxPlanes];
305static GlTextures mapFromGlTexture(
const QGstBufferHandle &bufferHandle, GstVideoFrame &frame,
306 GstVideoInfo &videoInfo)
308 qCDebug(qLcGstVideoBuffer) <<
"mapFromGlTexture";
310 GstBuffer *buffer = bufferHandle.get();
311 auto *mem = GST_GL_BASE_MEMORY_CAST(gst_buffer_peek_memory(buffer, 0));
315 if (!gst_video_frame_map(&frame, &videoInfo, buffer, GstMapFlags(GST_MAP_READ|GST_MAP_GL))) {
316 qWarning() <<
"Could not map GL textures";
320 auto *sync_meta = gst_buffer_get_gl_sync_meta(buffer);
321 GstBuffer *sync_buffer =
nullptr;
323 sync_buffer = gst_buffer_new();
324 sync_meta = gst_buffer_add_gl_sync_meta(mem->context, sync_buffer);
326 gst_gl_sync_meta_set_sync_point (sync_meta, mem->context);
327 gst_gl_sync_meta_wait (sync_meta, mem->context);
329 gst_buffer_unref(sync_buffer);
332 textures.count = frame.info.finfo->n_planes;
334 for (uint i = 0; i < textures.count; ++i)
335 textures.names[i] = *(guint32 *)frame.data[i];
337 gst_video_frame_unmap(&frame);
342# if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
343static GlTextures mapFromDmaBuffer(QRhi *rhi,
const QGstBufferHandle &bufferHandle,
344 GstVideoFrame &frame, GstVideoInfo &videoInfo,
345 Qt::HANDLE eglDisplay, QFunctionPointer eglImageTargetTexture2D)
347 qCDebug(qLcGstVideoBuffer) <<
"mapFromDmaBuffer, glGetError returns" << Qt::hex << glGetError()
348 <<
", eglGetError() returns" << eglGetError();
350 GstBuffer *buffer = bufferHandle.get();
352 Q_ASSERT(gst_is_dmabuf_memory(gst_buffer_peek_memory(buffer, 0)));
353 Q_ASSERT(eglDisplay);
354 Q_ASSERT(eglImageTargetTexture2D);
355 Q_ASSERT(QGuiApplication::platformName() == QLatin1String(
"eglfs"));
358 auto *nativeHandles =
static_cast<
const QRhiGles2NativeHandles *>(rhi->nativeHandles());
359 auto glContext = nativeHandles->context;
361 qWarning() <<
"no GL context";
365 if (!gst_video_frame_map(&frame, &videoInfo, buffer, GstMapFlags(GST_MAP_READ))) {
366 qWarning() <<
"gst_video_frame_map failed, couldn't map DMA video frame";
370 constexpr int maxPlanes = 4;
371 const int nPlanes = GST_VIDEO_FRAME_N_PLANES(&frame);
372 const int nMemoryBlocks = gst_buffer_n_memory(buffer);
373 const bool externalOes =
374 QVideoTextureHelper::forceGlTextureExternalOesIsSet();
375 static const bool singleEGLImage =
376 externalOes || qEnvironmentVariableIsSet(
"QT_GSTREAMER_FORCE_SINGLE_EGLIMAGE");
378 qCDebug(qLcGstVideoBuffer) <<
"nPlanes:" << nPlanes
379 <<
"nMemoryBlocks:" << nMemoryBlocks
380 <<
"externalOes:" << externalOes
381 <<
"singleEGLImage:" << singleEGLImage;
382 Q_ASSERT(nPlanes >= 1
383 && nPlanes <= maxPlanes
384 && (nMemoryBlocks == 1 || nMemoryBlocks == nPlanes));
386 GlTextures textures = {};
387 textures.owned =
true;
388 textures.count = singleEGLImage ? 1 : nPlanes;
390 QOpenGLFunctions functions(glContext);
391 functions.glGenTextures(
int(textures.count), textures.names.data());
392 qCDebug(qLcGstVideoBuffer) <<
"called glGenTextures, glGetError returns"
393 << Qt::hex << glGetError();
395 std::array<
int, maxPlanes> fds{-1, -1, -1, -1};
396 for (
int i = 0; i < nMemoryBlocks && i < maxPlanes; ++i) {
397 fds[i] = gst_dmabuf_memory_get_fd(gst_buffer_peek_memory(buffer, i));
399 auto fdForPlane = [&](
int plane) {
400 if (plane < 0 || plane >= maxPlanes || plane >= nMemoryBlocks)
402 return (fds[plane] >= 0) ? fds[plane] : fds[0];
405 int nEGLImages = singleEGLImage ? 1 : nPlanes;
406 for (
int plane = 0; plane < nEGLImages; ++plane) {
407 constexpr int maxAttrCount = 31;
408 std::array<EGLAttrib, maxAttrCount> attr;
411 gint width = singleEGLImage ? GST_VIDEO_FRAME_WIDTH(&frame)
412 : GST_VIDEO_FRAME_COMP_WIDTH(&frame, plane);
413 gint height = singleEGLImage ? GST_VIDEO_FRAME_HEIGHT(&frame)
414 : GST_VIDEO_FRAME_COMP_HEIGHT(&frame, plane);
415 attr[i++] = EGL_WIDTH;
417 attr[i++] = EGL_HEIGHT;
419 attr[i++] = EGL_LINUX_DRM_FOURCC_EXT;
420 attr[i++] = fourccFromVideoInfo(&videoInfo, plane, singleEGLImage);
422 attr[i++] = EGL_DMA_BUF_PLANE0_FD_EXT;
423 attr[i++] = fdForPlane(plane);
424 attr[i++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
425 attr[i++] = (EGLAttrib)(GST_VIDEO_FRAME_PLANE_OFFSET(&frame, plane));
426 attr[i++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
427 attr[i++] = (EGLAttrib)(GST_VIDEO_FRAME_PLANE_STRIDE(&frame, plane));
429 if (singleEGLImage && nPlanes > 1) {
430 attr[i++] = EGL_DMA_BUF_PLANE1_FD_EXT;
431 attr[i++] = fdForPlane(1);
432 attr[i++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
433 attr[i++] = (EGLAttrib)(GST_VIDEO_FRAME_PLANE_OFFSET(&frame, 1));
434 attr[i++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
435 attr[i++] = (EGLAttrib)(GST_VIDEO_FRAME_PLANE_STRIDE(&frame, 1));
438 if (singleEGLImage && nPlanes > 2) {
439 attr[i++] = EGL_DMA_BUF_PLANE2_FD_EXT;
440 attr[i++] = fdForPlane(2);
441 attr[i++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
442 attr[i++] = (EGLAttrib)(GST_VIDEO_FRAME_PLANE_OFFSET(&frame, 2));
443 attr[i++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
444 attr[i++] = (EGLAttrib)(GST_VIDEO_FRAME_PLANE_STRIDE(&frame, 2));
447 if (singleEGLImage && nPlanes > 3) {
448 attr[i++] = EGL_DMA_BUF_PLANE3_FD_EXT;
449 attr[i++] = fdForPlane(3);
450 attr[i++] = EGL_DMA_BUF_PLANE3_OFFSET_EXT;
451 attr[i++] = (EGLAttrib)(GST_VIDEO_FRAME_PLANE_OFFSET(&frame, 3));
452 attr[i++] = EGL_DMA_BUF_PLANE3_PITCH_EXT;
453 attr[i++] = (EGLAttrib)(GST_VIDEO_FRAME_PLANE_STRIDE(&frame, 3));
456 attr[i++] = EGL_NONE;
457 Q_ASSERT(i <= maxAttrCount);
459 EGLImage image = eglCreateImage(eglDisplay,
461 EGL_LINUX_DMA_BUF_EXT,
464 if (image == EGL_NO_IMAGE_KHR) {
465 qWarning() <<
"could not create EGL image for plane" << plane
466 <<
", eglError"<< Qt::hex << eglGetError();
469 qCDebug(qLcGstVideoBuffer) <<
"called eglCreateImage, glGetError returns"
470 << Qt::hex << glGetError()
471 <<
", eglGetError() returns" << eglGetError();
473 #ifdef GL_OES_EGL_image_external
474 GLenum target = externalOes ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
476 GLenum target = GL_TEXTURE_2D;
478 functions.glBindTexture(target, textures.names[plane]);
480 auto EGLImageTargetTexture2D = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglImageTargetTexture2D;
481 EGLImageTargetTexture2D(target, image);
482 qCDebug(qLcGstVideoBuffer) <<
"called glEGLImageTargetTexture2DOES, glGetError returns"
483 << Qt::hex << glGetError()
484 <<
", eglGetError() returns" << eglGetError();
486 eglDestroyImage(eglDisplay, image);
489 gst_video_frame_unmap(&frame);
498#if QT_CONFIG(gstreamer_gl)
499# if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
500 static const bool isEglfsQPA = QGuiApplication::platformName() == QLatin1String(
"eglfs");
502 GlTextures textures = {};
503 if (m_memoryFormat == QGstCaps::GLTexture)
504 textures = mapFromGlTexture(m_buffer, m_frame, m_videoInfo);
506# if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
507 else if (m_memoryFormat == QGstCaps::DMABuf && eglDisplay && isEglfsQPA)
508 textures = mapFromDmaBuffer(&rhi, m_buffer, m_frame, m_videoInfo, eglDisplay,
509 eglImageTargetTexture2D);
512 if (textures.count > 0)
513 return std::make_unique<QGstQVideoFrameTextures>(&rhi, QSize{m_videoInfo.width, m_videoInfo.height},
514 m_frameFormat.pixelFormat(), textures, m_memoryFormat);
~QGstVideoBuffer() override
QGstVideoBuffer(QGstBufferHandle buffer, const GstVideoInfo &info, QGstreamerRelayVideoSink *sink, const QVideoFrameFormat &frameFormat, QGstCaps::MemoryFormat format)
bool isDmaBuf() const override
QVideoFrameTexturesUPtr mapTextures(QRhi &, QVideoFrameTexturesUPtr &) override
void unmap() override
Releases the memory mapped by the map() function.
MapData map(QVideoFrame::MapMode mode) override
Maps the planes of a video buffer to memory.
Combined button and popup list for selecting options.
#define fourcc_code(a, b, c, d)