6#include <QtMultimedia/private/qdmabufallocator_p.h>
7#include <QtMultimedia/private/qmultimedia_dmabuf_support_p.h>
8#include <QtMultimedia/private/qmultimedia_drm_support_p.h>
9#include <QtMultimedia/private/qvideotexturehelper_p.h>
13#if QT_CONFIG(linux_dmabuf)
17namespace QtMultimediaPrivate {
21bool dmaBufIsSemiPlanar(QVideoFrameFormat::PixelFormat format)
24 case QVideoFrameFormat::Format_NV12:
25 case QVideoFrameFormat::Format_NV21:
26 case QVideoFrameFormat::Format_P010:
27 case QVideoFrameFormat::Format_P016:
36std::optional<DmaBufLayout> dmaBufLayoutForFormat(QVideoFrameFormat::PixelFormat format,
37 QSize frameSize,
int bytesPerLine,
41 if (format == QVideoFrameFormat::Format_Jpeg)
44 const auto *desc = QVideoTextureHelper::textureDescription(format);
45 const auto drmFormats = dmaBufFourccFromPixelFormat(format);
46 if (!desc || drmFormats.empty() || bytesPerLine <= 0)
49 const bool supported = drmFormats.size() == 1 || dmaBufIsSemiPlanar(format);
55 for (qsizetype plane = 0; plane < drmFormats.size(); ++plane) {
56 const qsizetype size =
57 qsizetype(bytesPerLine) * desc->heightForPlane(frameSize.height(),
int(plane));
58 layout.push_back(DmaBufPlaneLayout{
60 uint32_t(bytesPerLine),
66 if (offset > qsizetype(sizeImage))
72DmaBufSource::DmaBufSource() =
default;
74DmaBufSource::DmaBufSource(std::shared_ptr<
const QDmaBuf> buffer, DmaBufLayout layout)
75 : m_buffer(std::move(buffer)), m_layout(std::move(layout))
78 Q_ASSERT(!m_layout.isEmpty() && m_layout[0].offset == 0);
79 Q_ASSERT(qsizetype(m_layout.back().offset) + m_layout.back().size <= m_buffer->size());
82std::optional<QAbstractVideoBuffer::MapData> DmaBufSource::mapReadOnly()
const
87 ::mmap(
nullptr, size_t(m_buffer->size()), PROT_READ, MAP_SHARED, m_buffer->fd(), 0);
88 if (mapped == MAP_FAILED)
91 dmaBufSyncRead(m_buffer->fd(), DmaBufCpuAccess::Begin);
93 QAbstractVideoBuffer::MapData mapData;
94 mapData.planeCount =
int(m_layout.size());
95 for (qsizetype plane = 0; plane < m_layout.size(); ++plane) {
96 const DmaBufPlaneLayout &p = m_layout[plane];
97 mapData.data[plane] =
static_cast<uchar *>(mapped) + p.offset;
98 mapData.bytesPerLine[plane] =
int(p.pitch);
99 mapData.dataSize[plane] =
int(p.size);
104void DmaBufSource::unmap(QAbstractVideoBuffer::MapData &mapData)
const
108 dmaBufSyncRead(m_buffer->fd(), DmaBufCpuAccess::End);
109 ::munmap(mapData.data[0], size_t(m_buffer->size()));