5#include "private/qvideotexturehelper_p.h"
6#include "private/qmultimediautils_p.h"
11#include <libavutil/pixdesc.h>
12#include <libavutil/hdr_dynamic_metadata.h>
13#include <libavutil/mastering_display_metadata.h>
19 for (
int i = 0;
i < AV_NUM_DATA_POINTERS &&
frame.data[
i]; ++
i) {
33 { pixelAspectRatio.num, pixelAspectRatio.den }))
35 if (
frame->hw_frames_ctx) {
36 m_hwFrame = std::move(
frame);
41 m_swFrame = std::move(
frame);
53 const auto actualAVPixelFormat = AVPixelFormat(m_swFrame->format);
56 if (actualAVPixelFormat != targetAVPixelFormat ||
isFrameFlipped(*m_swFrame)
57 || m_size !=
QSize(m_swFrame->width, m_swFrame->height)) {
60 SwsContext *
c = sws_getContext(m_swFrame->width, m_swFrame->height, actualAVPixelFormat,
61 m_size.
width(), m_size.
height(), targetAVPixelFormat,
62 SWS_BICUBIC,
nullptr,
nullptr,
nullptr);
65 newFrame->width = m_size.
width();
66 newFrame->height = m_size.
height();
67 newFrame->format = targetAVPixelFormat;
68 av_frame_get_buffer(newFrame.get(), 0);
70 sws_scale(
c, m_swFrame->data, m_swFrame->linesize, 0, m_swFrame->height, newFrame->data, newFrame->linesize);
71 if (m_frame == m_swFrame.get())
72 m_frame = newFrame.get();
73 m_swFrame = std::move(newFrame);
80 m_textureConverter = converter;
81 m_textureConverter.
init(m_hwFrame.get());
87 switch (m_frame->colorspace) {
89 case AVCOL_SPC_UNSPECIFIED:
90 case AVCOL_SPC_RESERVED:
92 case AVCOL_SPC_SMPTE240M:
94 case AVCOL_SPC_SMPTE2085:
95 case AVCOL_SPC_CHROMA_DERIVED_NCL:
96 case AVCOL_SPC_CHROMA_DERIVED_CL:
101 case AVCOL_SPC_BT709:
103 case AVCOL_SPC_BT470BG:
104 case AVCOL_SPC_SMPTE170M:
106 case AVCOL_SPC_BT2020_NCL:
107 case AVCOL_SPC_BT2020_CL:
119 switch (m_frame->color_range) {
120 case AVCOL_RANGE_MPEG:
122 case AVCOL_RANGE_JPEG:
132 for (
int i = 0;
i < m_frame->nb_side_data; ++
i) {
133 AVFrameSideData *sd = m_frame->side_data[
i];
135 if (sd->type == AV_FRAME_DATA_MASTERING_DISPLAY_METADATA) {
136 auto *
data =
reinterpret_cast<AVMasteringDisplayMetadata *
>(sd->data);
139 maxNits = float(maybeLum.value());
153 Q_ASSERT(m_hwFrame && m_hwFrame->hw_frames_ctx);
156 int ret = av_hwframe_transfer_data(m_swFrame.get(), m_hwFrame.get(), 0);
158 qWarning() <<
"Error transferring the data to system memory:" <<
ret;
168 mapData.nPlanes = desc->nplanes;
173 mapData.bytesPerLine[
i] = m_swFrame->linesize[
i];
174 mapData.size[
i] =
mapData.bytesPerLine[
i]*desc->heightForPlane(m_swFrame->height,
i);
182 <<
"Mapping of FFmpeg video buffer with write mode when "
183 "textures have been created. Visual artifacts might "
184 "happen if the frame is still in the rendering pipeline";
205 if (m_textureConverter.
isNull()) {
206 m_textures =
nullptr;
210 m_textures.reset(m_textureConverter.
getTextures(m_hwFrame.get()));
212 static thread_local int lastFormat = 0;
213 if (std::exchange(lastFormat, m_hwFrame->format) != m_hwFrame->format)
214 qWarning() <<
" failed to get textures for frame; format:" << m_hwFrame->format;
221 return m_textures ? m_textures->textureHandle(
rhi, plane) : 0;
226 return m_pixelFormat;
237 *needsConversion =
false;
239 switch (avPixelFormat) {
242 case AV_PIX_FMT_NONE:
243 Q_ASSERT(!
"Invalid avPixelFormat!");
245 case AV_PIX_FMT_ARGB:
247 case AV_PIX_FMT_0RGB:
249 case AV_PIX_FMT_BGRA:
251 case AV_PIX_FMT_BGR0:
253 case AV_PIX_FMT_ABGR:
255 case AV_PIX_FMT_0BGR:
257 case AV_PIX_FMT_RGBA:
259 case AV_PIX_FMT_RGB0:
262 case AV_PIX_FMT_YUV422P:
264 case AV_PIX_FMT_YUV420P:
266 case AV_PIX_FMT_YUV420P10:
268 case AV_PIX_FMT_UYVY422:
270 case AV_PIX_FMT_YUYV422:
272 case AV_PIX_FMT_NV12:
274 case AV_PIX_FMT_NV21:
276 case AV_PIX_FMT_GRAY8:
278 case AV_PIX_FMT_GRAY16:
281 case AV_PIX_FMT_P010:
283 case AV_PIX_FMT_P016:
285 case AV_PIX_FMT_MEDIACODEC:
290 *needsConversion =
true;
292 const AVPixFmtDescriptor *descriptor = av_pix_fmt_desc_get(avPixelFormat);
294 if (descriptor->flags & AV_PIX_FMT_FLAG_RGB)
297 if (descriptor->comp[0].depth > 8)
314 return AV_PIX_FMT_NONE;
317 return AV_PIX_FMT_BGRA;
319 return AV_PIX_FMT_ARGB;
322 return AV_PIX_FMT_0RGB;
324 return AV_PIX_FMT_BGRA;
327 return AV_PIX_FMT_BGR0;
329 return AV_PIX_FMT_ABGR;
331 return AV_PIX_FMT_0BGR;
333 return AV_PIX_FMT_RGBA;
337 return AV_PIX_FMT_RGB0;
340 return AV_PIX_FMT_YUV422P;
342 return AV_PIX_FMT_YUV420P;
344 return AV_PIX_FMT_YUV420P10;
346 return AV_PIX_FMT_UYVY422;
348 return AV_PIX_FMT_YUYV422;
350 return AV_PIX_FMT_NV12;
352 return AV_PIX_FMT_NV21;
354 return AV_PIX_FMT_GRAY8;
356 return AV_PIX_FMT_GRAY16;
359 return AV_PIX_FMT_P010;
361 return AV_PIX_FMT_P016;
364 return AV_PIX_FMT_MEDIACODEC;
The QAbstractVideoBuffer class is an abstraction for video data. \inmodule QtMultimedia.
QRhi * rhi() const
Returns the QRhi instance.
QVideoFrame::HandleType m_type
QVideoFrameFormat::PixelFormat pixelFormat() const
QVideoFrameFormat::ColorSpace colorSpace() const
void unmap() override
Releases the memory mapped by the map() function.
MapData map(QVideoFrame::MapMode mode) override
Independently maps the planes of a video buffer to memory.
QVideoFrameFormat::ColorTransfer colorTransfer() const
QFFmpeg::AVFrameUPtr AVFrameUPtr
void setTextureConverter(const QFFmpeg::TextureConverter &converter)
virtual std::unique_ptr< QVideoFrameTextures > mapTextures(QRhi *) override
static AVPixelFormat toAVPixelFormat(QVideoFrameFormat::PixelFormat pixelFormat)
QVideoFrameFormat::ColorRange colorRange() const
static QVideoFrameFormat::PixelFormat toQtPixelFormat(AVPixelFormat avPixelFormat, bool *needsConversion=nullptr)
QFFmpegVideoBuffer(AVFrameUPtr frame, AVRational pixelAspectRatio={ 1, 1 })
~QFFmpegVideoBuffer() override
virtual quint64 textureHandle(QRhi *rhi, int plane) const override
Returns a texture handle to the data buffer.
QVideoFrame::MapMode mapMode() const override
static AVPixelFormat format(AVFrame *frame)
TextureSet * getTextures(AVFrame *frame)
void init(AVFrame *frame)
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
constexpr int height() const noexcept
Returns the height.
constexpr int width() const noexcept
Returns the width.
The QVideoFrame class represents a frame of video data.
MapMode
Enumerates how a video buffer's data is mapped to system memory.
AVFrameUPtr makeAVFrame()
QVideoFrameFormat::ColorTransfer fromAvColorTransfer(AVColorTransferCharacteristic colorTrc)
std::optional< qint64 > mul(qint64 a, AVRational b)
Combined button and popup list for selecting options.
const TextureDescription * textureDescription(QVideoFrameFormat::PixelFormat format)
static QDBusError::ErrorType get(const char *name)
static QT_BEGIN_NAMESPACE bool isFrameFlipped(const AVFrame &frame)
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
static QAbstractVideoBuffer::MapData mapData(const camera_frame_nv12_t &frame, unsigned char *baseAddress)
unsigned long long quint64