6#include <QtMultimedia/private/qvideotexturehelper_p.h>
7#include <QtCore/qloggingcategory.h>
9#include <common/qgstutils_p.h>
11#include <gst/video/video.h>
12#include <gst/video/video-frame.h>
13#include <gst/pbutils/gstpluginsbaseversion.h>
16#if QT_CONFIG(gstreamer_gl)
17# include <QtCore/qscopeguard.h>
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>
33#if QT_CONFIG(gpu_vivante)
34# include <QtMultimedia/private/qvivantetextureimporter_p.h>
35# include <QtGui/rhi/qrhi.h>
36# include <gst/allocators/gstphysmemory.h>
42Q_STATIC_LOGGING_CATEGORY(qLcGstVideoBuffer,
"qt.multimedia.gstreamer.videobuffer")
44QGstVideoBuffer::QGstVideoBuffer(QGstBufferHandle buffer,
const QGstVideoInfo &videoInfo,
45 const QVideoFrameFormat &frameFormat)
46 : m_memoryFormat(buffer.isValid() ? qMemoryFormatFromGstBuffer(buffer.get())
47 : QGstCaps::CpuMemory),
48 m_frameFormat(frameFormat),
49 m_videoInfo(videoInfo),
50 m_buffer(std::move(buffer))
52 if constexpr (QT_CONFIG(linux_dmabuf))
53 Q_ASSERT(m_memoryFormat != QGstCaps::DMABuf
54 &&
"DMABuf buffers should use QGstDmaBufVideoBuffer");
59 Q_ASSERT(m_mode == QVideoFrame::NotMapped);
64 const GstMapFlags flags = GstMapFlags(((mode & QVideoFrame::ReadOnly) ? GST_MAP_READ : 0)
65 | ((mode & QVideoFrame::WriteOnly) ? GST_MAP_WRITE : 0));
68 if (mode == QVideoFrame::NotMapped || m_mode != QVideoFrame::NotMapped)
71 const GstVideoInfo &gstVideoInfo = m_videoInfo.gstVideoInfo;
72 if (!gstVideoInfo.finfo || gstVideoInfo.finfo->n_planes == 0) {
73 if (gst_buffer_map(m_buffer.get(), &m_frame.map[0], flags)) {
74 mapData.planeCount = 1;
75 mapData.bytesPerLine[0] = -1;
76 mapData.dataSize[0] = m_frame.map[0].size;
77 mapData.data[0] =
static_cast<uchar *>(m_frame.map[0].data);
81 }
else if (gst_video_frame_map(&m_frame, &gstVideoInfo, m_buffer.get(), flags)) {
82 mapData.planeCount = GST_VIDEO_FRAME_N_PLANES(&m_frame);
84 for (guint i = 0; i < GST_VIDEO_FRAME_N_PLANES(&m_frame); ++i) {
85 mapData.bytesPerLine[i] = GST_VIDEO_FRAME_PLANE_STRIDE(&m_frame, i);
86 mapData.data[i] =
static_cast<uchar *>(GST_VIDEO_FRAME_PLANE_DATA(&m_frame, i));
87 mapData.dataSize[i] = mapData.bytesPerLine[i]*GST_VIDEO_FRAME_COMP_HEIGHT(&m_frame, i);
97 if (m_mode != QVideoFrame::NotMapped) {
98 if (!m_videoInfo.gstVideoInfo.finfo || m_videoInfo.gstVideoInfo.finfo->n_planes == 0)
99 gst_buffer_unmap(m_buffer.get(), &m_frame.map[0]);
101 gst_video_frame_unmap(&m_frame);
103 m_mode = QVideoFrame::NotMapped;
106#if QT_CONFIG(gpu_vivante)
111std::optional<GLenum> qGstVivFormatTokenFromGstVideoFormat(GstVideoFormat format)
114 case GST_VIDEO_FORMAT_I420:
116 case GST_VIDEO_FORMAT_YV12:
118 case GST_VIDEO_FORMAT_NV12:
120 case GST_VIDEO_FORMAT_NV21:
122 case GST_VIDEO_FORMAT_YUY2:
124 case GST_VIDEO_FORMAT_UYVY:
126 case GST_VIDEO_FORMAT_BGRA:
127 case GST_VIDEO_FORMAT_BGRx:
139std::optional<QSize> qGstVivantePaddedFrameSize(GstBuffer *buffer,
const GstVideoInfo &videoInfo)
141 const GstVideoMeta *meta = gst_buffer_get_video_meta(buffer);
142 const int stride = meta ? meta->stride[0] : GST_VIDEO_INFO_PLANE_STRIDE(&videoInfo, 0);
143 const int pixelStride = GST_VIDEO_INFO_COMP_PSTRIDE(&videoInfo, 0);
144 const int width = GST_VIDEO_INFO_WIDTH(&videoInfo);
145 const int height = GST_VIDEO_INFO_HEIGHT(&videoInfo);
147 int paddedHeight = height;
148 if (GST_VIDEO_INFO_N_PLANES(&videoInfo) > 1 && stride > 0) {
149 const gsize lumaOffset = meta ? meta->offset[0] : GST_VIDEO_INFO_PLANE_OFFSET(&videoInfo, 0);
150 const gsize chromaOffset =
151 meta ? meta->offset[1] : GST_VIDEO_INFO_PLANE_OFFSET(&videoInfo, 1);
152 paddedHeight =
int((chromaOffset - lumaOffset) / stride);
154 const int paddedWidth = pixelStride > 0 ? stride / pixelStride : width;
156 if (paddedWidth < width || paddedHeight < height) {
157 qCWarning(qLcGstVideoBuffer) <<
"Unexpected Vivante buffer layout: stride" << stride
158 <<
"padded size" << paddedWidth <<
"x" << paddedHeight
159 <<
"for a" << width <<
"x" << height <<
"frame";
162 return QSize(paddedWidth, paddedHeight);
166#if QT_CONFIG(gstreamer_gl)
168class QGstQVideoFrameTextures :
public QVideoFrameTextures
171 QGstQVideoFrameTextures(QRhi *rhi, QSize size, QVideoFrameFormat::PixelFormat format,
172 std::vector<guint32> textures)
173 : m_rhi(rhi), m_glTextures(std::move(textures))
175 QRhiTexture::Flags textureFlags = {};
176 if (QVideoTextureHelper::forceGlTextureExternalOesIsSet() && m_rhi
177 && rhi->backend() == QRhi::OpenGLES2)
178 textureFlags = {QRhiTexture::ExternalOES};
180 auto desc = QVideoTextureHelper::textureDescription(format);
181 for (uint i = 0; i < textures.size(); ++i) {
182 QSize planeSize = desc->rhiPlaneSize(size, i, m_rhi);
183 QRhiTexture::Format format = desc->rhiTextureFormat(i, m_rhi);
184 m_textures[i].reset(rhi->newTexture(format, planeSize, 1, textureFlags));
185 m_textures[i]->createFrom({ textures[i], 0 });
189 QRhiTexture *texture(uint plane)
const override
191 return plane < m_glTextures.size() ? m_textures[plane].get() :
nullptr;
195 QRhi *m_rhi =
nullptr;
196 std::vector<guint32> m_glTextures;
197 std::unique_ptr<QRhiTexture> m_textures[QVideoTextureHelper::TextureDescription::maxPlanes];
200static std::vector<guint32> mapFromGlTexture(
const QGstBufferHandle &bufferHandle,
201 GstVideoFrame &frame, GstVideoInfo &videoInfo)
203 qCDebug(qLcGstVideoBuffer) <<
"Mapping textures from GL memory";
205 GstBuffer *buffer = bufferHandle.get();
206 auto *mem = GST_GL_BASE_MEMORY_CAST(gst_buffer_peek_memory(buffer, 0));
210 if (!gst_video_frame_map(&frame, &videoInfo, buffer, GstMapFlags(GST_MAP_READ|GST_MAP_GL))) {
211 qWarning() <<
"Could not map GL textures";
215 const auto unmapGuard = qScopeGuard([&] {
216 gst_video_frame_unmap(&frame);
221 auto *sync_meta = gst_buffer_get_gl_sync_meta(buffer);
222 QGstBufferHandle sync_buffer;
224 sync_buffer.reset(gst_buffer_new(), QGstBufferHandle::HasRef);
225 sync_meta = gst_buffer_add_gl_sync_meta(mem->context, sync_buffer.get());
227 gst_gl_sync_meta_set_sync_point(sync_meta, mem->context);
228 gst_gl_sync_meta_wait(sync_meta, mem->context);
231 std::vector<guint32> textures;
233 for (uint i = 0; i < GST_VIDEO_FRAME_N_PLANES(&frame); ++i)
234 textures.push_back(*(guint32 *)frame.data[i]);
241#if QT_CONFIG(gpu_vivante)
242QtMultimediaPrivate::QVivanteVideoBuffer *QGstVideoBuffer::vivanteVideoBuffer()
244 if (m_vivanteVideoBuffer)
245 return m_vivanteVideoBuffer.get();
247 GstBuffer *buffer = m_buffer.get();
248 GstMemory *mem = gst_buffer_peek_memory(buffer, 0);
249 if (!mem || !gst_is_phys_memory(mem)) {
250 qCWarning(qLcGstVideoBuffer) <<
"Vivante buffer is not phys-memory backed";
254 const GstVideoInfo &gstVideoInfo = m_videoInfo.gstVideoInfo;
255 const GstVideoFormat gstFormat = GST_VIDEO_INFO_FORMAT(&gstVideoInfo);
256 const std::optional<GLenum> vivFormatToken = qGstVivFormatTokenFromGstVideoFormat(gstFormat);
257 if (!vivFormatToken) {
258 qCDebug(qLcGstVideoBuffer) <<
"Unsupported format for Vivante direct-texture import:"
259 << gst_video_format_to_string(gstFormat)
260 <<
"; falling back to CPU path";
264 const auto physicalAddress = quint32(gst_phys_memory_get_phys_addr(mem));
265 if (!physicalAddress) {
266 qCWarning(qLcGstVideoBuffer) <<
"Vivante buffer has no physical address";
277 GstVideoFrame frame{};
278 ~MappedFrame() { gst_video_frame_unmap(&frame); }
280 auto mapped = std::make_shared<MappedFrame>();
281 if (!gst_video_frame_map(&mapped->frame, &gstVideoInfo, buffer, GST_MAP_READ)) {
282 qCWarning(qLcGstVideoBuffer) <<
"Failed to map Vivante phys-mem buffer";
285 const GstVideoFrame &frame = mapped->frame;
287 const std::optional<QSize> paddedSize = qGstVivantePaddedFrameSize(buffer, gstVideoInfo);
290 if (*paddedSize != QSize(GST_VIDEO_FRAME_WIDTH(&frame), GST_VIDEO_FRAME_HEIGHT(&frame))) {
291 qCDebug(qLcGstVideoBuffer) <<
"Vivante buffer is padded to" << *paddedSize <<
"for a"
292 << GST_VIDEO_FRAME_WIDTH(&frame) <<
"x"
293 << GST_VIDEO_FRAME_HEIGHT(&frame) <<
"frame";
296 void *logicalAddr = GST_VIDEO_FRAME_PLANE_DATA(&frame, 0);
297 const quint32 planePhysicalAddress =
298 physicalAddress + quint32(GST_VIDEO_FRAME_PLANE_OFFSET(&frame, 0));
300 m_vivanteVideoBuffer = std::make_unique<QtMultimediaPrivate::QVivanteVideoBuffer>(
301 m_frameFormat.pixelFormat(), *paddedSize, planePhysicalAddress,
302 logicalAddr, *vivFormatToken, std::move(mapped));
303 return m_vivanteVideoBuffer.get();
310 switch (m_memoryFormat) {
311#if QT_CONFIG(gpu_vivante)
312 case QGstCaps::VivantePhysMemory:
313 if (QtMultimediaPrivate::QVivanteVideoBuffer *vivante = vivanteVideoBuffer())
314 return vivante->mapTextures(rhi);
318#if QT_CONFIG(gstreamer_gl)
320 auto textures = mapFromGlTexture(m_buffer, m_frame, m_videoInfo.gstVideoInfo);
322 return std::make_unique<QGstQVideoFrameTextures>(
323 &rhi, QSize{ m_videoInfo.gstVideoInfo.width, m_videoInfo.gstVideoInfo.height },
324 m_frameFormat.pixelFormat(), std::move(textures));
~QGstVideoBuffer() 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.
QVideoFrameTexturesUPtr mapTextures(QRhi &) override
Combined button and popup list for selecting options.