5#include "private/qvideotexturehelper_p.h"
6#include "private/qmultimediautils_p.h"
9#include <QtCore/qthread.h>
12#include <libavutil/pixdesc.h>
13#include <libavutil/hdr_dynamic_metadata.h>
14#include <libavutil/mastering_display_metadata.h>
19using namespace QFFmpeg;
24 for (
int i = 0; i < AV_NUM_DATA_POINTERS && frame.data[i]; ++i) {
25 if (frame.linesize[i] < 0)
38 if (frame->hw_frames_ctx) {
39 m_hwFrame = std::move(frame);
40 m_pixelFormat = toQtPixelFormat(HWAccel::format(m_hwFrame.get()));
44 m_swFrame = std::move(frame);
45 m_pixelFormat = toQtPixelFormat(AVPixelFormat(m_swFrame->format));
56 const auto actualAVPixelFormat = AVPixelFormat(m_swFrame->format);
57 const auto targetAVPixelFormat = toAVPixelFormat(m_pixelFormat);
59 const QSize actualSize(m_swFrame->width, m_swFrame->height);
60 if (actualAVPixelFormat != targetAVPixelFormat || isFrameFlipped(*m_swFrame)
61 || m_size != actualSize) {
62 Q_ASSERT(toQtPixelFormat(targetAVPixelFormat) == m_pixelFormat);
64 SwsContextUPtr scaleContext = createSwsContext(actualSize, actualAVPixelFormat, m_size,
65 targetAVPixelFormat, SWS_BICUBIC);
67 auto newFrame = makeAVFrame();
68 newFrame->width = m_size.width();
69 newFrame->height = m_size.height();
70 newFrame->format = targetAVPixelFormat;
71 const int status = av_frame_get_buffer(newFrame.get(), 0);
74 <<
"Failed to allocate frame buffer:" << AVError(status);
78 sws_scale(scaleContext.get(), m_swFrame->data, m_swFrame->linesize, 0, m_swFrame->height,
79 newFrame->data, newFrame->linesize);
80 if (m_frame == m_swFrame.get())
81 m_frame = newFrame.get();
82 m_swFrame = std::move(newFrame);
92 ensureTextureConverter(rhi);
95 m_type = m_hwFrame && TextureConverter::isBackendAvailable(*m_hwFrame, rhi)
96 ? QVideoFrame::RhiTextureHandle
97 : QVideoFrame::NoHandle;
104 HwFrameContextData &frameContextData = HwFrameContextData::ensure(*m_hwFrame);
105 TextureConverter *converter = frameContextData.textureConverterMapper.get(&rhi);
109 std::tie(converter, added) =
110 frameContextData.textureConverterMapper.tryMap(rhi, TextureConverter(rhi));
113 Q_ASSERT(converter && added);
124 HwFrameContextData &frameContextData = HwFrameContextData::ensure(*m_hwFrame);
125 return frameContextData.textureConverterMapper.findRhi(
126 [](QRhi &rhi) {
return rhi.thread()->isCurrentThread(); });
131 return fromAvColorSpace(m_frame->colorspace);
136 return fromAvColorTransfer(m_frame->color_trc);
141 return fromAvColorRange(m_frame->color_range);
147 for (
int i = 0; i < m_frame->nb_side_data; ++i) {
148 AVFrameSideData *sd = m_frame->side_data[i];
150 if (sd->type == AV_FRAME_DATA_MASTERING_DISPLAY_METADATA) {
151 auto *data =
reinterpret_cast<AVMasteringDisplayMetadata *>(sd->data);
152 auto maybeLum = QFFmpeg::mul(qreal(10'000.), data->max_luminance);
154 maxNits =
float(maybeLum.value());
163 Q_ASSERT(m_hwFrame && m_hwFrame->hw_frames_ctx);
164 m_swFrame = makeAVFrame();
166 int ret = av_hwframe_transfer_data(m_swFrame.get(), m_hwFrame.get(), 0);
168 qWarning() <<
"Error transferring the data to system memory:" << ret;
177 auto *desc = QVideoTextureHelper::textureDescription(pixelFormat());
178 mapData.planeCount = desc->nplanes;
179 for (
int i = 0; i < mapData.planeCount; ++i) {
180 Q_ASSERT(m_swFrame->linesize[i] >= 0);
182 mapData.data[i] = m_swFrame->data[i];
183 mapData.bytesPerLine[i] = m_swFrame->linesize[i];
184 mapData.dataSize[i] = mapData.bytesPerLine[i]*desc->heightForPlane(m_swFrame->height, i);
187 if ((mode & QVideoFrame::WriteOnly) != 0 && m_hwFrame) {
188 m_type = QVideoFrame::NoHandle;
199 m_mode = QVideoFrame::NotMapped;
202QVideoFrameTexturesUPtr
QFFmpegVideoBuffer::mapTextures(QRhi &rhi, QVideoFrameTexturesUPtr& oldTextures)
204 Q_ASSERT(rhi.thread()->isCurrentThread());
206 QVideoFrameTexturesUPtr result = createTexturesFromHwFrame(rhi, oldTextures);
209 m_type = result ? QVideoFrame::RhiTextureHandle : QVideoFrame::NoHandle;
213QVideoFrameTexturesUPtr
QFFmpegVideoBuffer::createTexturesFromHwFrame(QRhi &rhi, QVideoFrameTexturesUPtr& oldTextures) {
223 constexpr bool initTextureConverterForAnyRhi =
false;
225 TextureConverter *converter = initTextureConverterForAnyRhi
226 ? &ensureTextureConverter(rhi)
227 : HwFrameContextData::ensure(*m_hwFrame).textureConverterMapper.get(&rhi);
232 if (!converter->init(*m_hwFrame))
235 const QVideoFrameTextures *oldTexturesRaw = oldTextures.get();
236 if (QVideoFrameTexturesUPtr newTextures = converter->createTextures(*m_hwFrame, oldTextures))
239 Q_ASSERT(oldTextures.get() == oldTexturesRaw);
241 QVideoFrameTexturesHandlesUPtr oldTextureHandles =
242 oldTextures ? oldTextures->takeHandles() :
nullptr;
243 QVideoFrameTexturesHandlesUPtr newTextureHandles =
244 converter->createTextureHandles(*m_hwFrame,
std::move(oldTextureHandles));
246 if (newTextureHandles) {
247 QVideoFrameTexturesUPtr newTextures = QVideoTextureHelper::createTexturesFromHandles(
248 std::move(newTextureHandles), rhi, m_pixelFormat,
249 { m_hwFrame->width, m_hwFrame->height });
254 static thread_local int lastFormat = 0;
255 if (std::exchange(lastFormat, m_hwFrame->format) != m_hwFrame->format)
256 qWarning() <<
" failed to get textures for frame; format:" << m_hwFrame->format;
263 return m_pixelFormat;
271QVideoFrameFormat::PixelFormat
QFFmpegVideoBuffer::toQtPixelFormat(AVPixelFormat avPixelFormat,
bool *needsConversion)
274 *needsConversion =
false;
276 switch (avPixelFormat) {
279 case AV_PIX_FMT_NONE:
280 Q_ASSERT(!
"Invalid avPixelFormat!");
281 return QVideoFrameFormat::Format_Invalid;
282 case AV_PIX_FMT_ARGB:
283 return QVideoFrameFormat::Format_ARGB8888;
284 case AV_PIX_FMT_0RGB:
285 return QVideoFrameFormat::Format_XRGB8888;
286 case AV_PIX_FMT_BGRA:
287 return QVideoFrameFormat::Format_BGRA8888;
288 case AV_PIX_FMT_BGR0:
289 return QVideoFrameFormat::Format_BGRX8888;
290 case AV_PIX_FMT_ABGR:
291 return QVideoFrameFormat::Format_ABGR8888;
292 case AV_PIX_FMT_0BGR:
293 return QVideoFrameFormat::Format_XBGR8888;
294 case AV_PIX_FMT_RGBA:
295 return QVideoFrameFormat::Format_RGBA8888;
296 case AV_PIX_FMT_RGB0:
297 return QVideoFrameFormat::Format_RGBX8888;
299 case AV_PIX_FMT_YUV422P:
300 return QVideoFrameFormat::Format_YUV422P;
301 case AV_PIX_FMT_YUV420P:
302 return QVideoFrameFormat::Format_YUV420P;
303 case AV_PIX_FMT_YUV420P10:
304 return QVideoFrameFormat::Format_YUV420P10;
305 case AV_PIX_FMT_UYVY422:
306 return QVideoFrameFormat::Format_UYVY;
307 case AV_PIX_FMT_YUYV422:
308 return QVideoFrameFormat::Format_YUYV;
309 case AV_PIX_FMT_NV12:
310 return QVideoFrameFormat::Format_NV12;
311 case AV_PIX_FMT_NV21:
312 return QVideoFrameFormat::Format_NV21;
313 case AV_PIX_FMT_GRAY8:
314 return QVideoFrameFormat::Format_Y8;
315 case AV_PIX_FMT_GRAY16:
316 return QVideoFrameFormat::Format_Y16;
318 case AV_PIX_FMT_P010:
319 return QVideoFrameFormat::Format_P010;
320 case AV_PIX_FMT_P016:
321 return QVideoFrameFormat::Format_P016;
322 case AV_PIX_FMT_MEDIACODEC:
323 return QVideoFrameFormat::Format_SamplerExternalOES;
327 *needsConversion =
true;
329 const AVPixFmtDescriptor *descriptor = av_pix_fmt_desc_get(avPixelFormat);
331 if (descriptor->flags & AV_PIX_FMT_FLAG_RGB)
332 return QVideoFrameFormat::Format_RGBA8888;
334 if (descriptor->comp[0].depth > 8)
335 return QVideoFrameFormat::Format_P016;
336 return QVideoFrameFormat::Format_YUV420P;
341 switch (pixelFormat) {
343 case QVideoFrameFormat::Format_Invalid:
344 case QVideoFrameFormat::Format_AYUV:
345 case QVideoFrameFormat::Format_AYUV_Premultiplied:
346 case QVideoFrameFormat::Format_YV12:
347 case QVideoFrameFormat::Format_IMC1:
348 case QVideoFrameFormat::Format_IMC2:
349 case QVideoFrameFormat::Format_IMC3:
350 case QVideoFrameFormat::Format_IMC4:
351 return AV_PIX_FMT_NONE;
352 case QVideoFrameFormat::Format_Jpeg:
354 return AV_PIX_FMT_BGRA;
355 case QVideoFrameFormat::Format_ARGB8888:
356 return AV_PIX_FMT_ARGB;
357 case QVideoFrameFormat::Format_ARGB8888_Premultiplied:
358 case QVideoFrameFormat::Format_XRGB8888:
359 return AV_PIX_FMT_0RGB;
360 case QVideoFrameFormat::Format_BGRA8888:
361 return AV_PIX_FMT_BGRA;
362 case QVideoFrameFormat::Format_BGRA8888_Premultiplied:
363 case QVideoFrameFormat::Format_BGRX8888:
364 return AV_PIX_FMT_BGR0;
365 case QVideoFrameFormat::Format_ABGR8888:
366 return AV_PIX_FMT_ABGR;
367 case QVideoFrameFormat::Format_XBGR8888:
368 return AV_PIX_FMT_0BGR;
369 case QVideoFrameFormat::Format_RGBA8888:
370 return AV_PIX_FMT_RGBA;
373 case QVideoFrameFormat::Format_RGBX8888:
374 return AV_PIX_FMT_RGB0;
376 case QVideoFrameFormat::Format_YUV422P:
377 return AV_PIX_FMT_YUV422P;
378 case QVideoFrameFormat::Format_YUV420P:
379 return AV_PIX_FMT_YUV420P;
380 case QVideoFrameFormat::Format_YUV420P10:
381 return AV_PIX_FMT_YUV420P10;
382 case QVideoFrameFormat::Format_UYVY:
383 return AV_PIX_FMT_UYVY422;
384 case QVideoFrameFormat::Format_YUYV:
385 return AV_PIX_FMT_YUYV422;
386 case QVideoFrameFormat::Format_NV12:
387 return AV_PIX_FMT_NV12;
388 case QVideoFrameFormat::Format_NV21:
389 return AV_PIX_FMT_NV21;
390 case QVideoFrameFormat::Format_Y8:
391 return AV_PIX_FMT_GRAY8;
392 case QVideoFrameFormat::Format_Y16:
393 return AV_PIX_FMT_GRAY16;
395 case QVideoFrameFormat::Format_P010:
396 return AV_PIX_FMT_P010;
397 case QVideoFrameFormat::Format_P016:
398 return AV_PIX_FMT_P016;
400 case QVideoFrameFormat::Format_SamplerExternalOES:
401 return AV_PIX_FMT_MEDIACODEC;
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
Maps the planes of a video buffer to memory.
QVideoFrameFormat::ColorTransfer colorTransfer() const
QVideoFrameFormat::ColorRange colorRange() const
QFFmpegVideoBuffer(AVFrameUPtr frame, AVRational pixelAspectRatio={ 1, 1 })
~QFFmpegVideoBuffer() override
QRhi * associatedCurrentThreadRhi() const override
static bool isFrameFlipped(const AVFrame &frame)
#define qCWarning(category,...)
#define Q_STATIC_LOGGING_CATEGORY(name,...)