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;
22 for (
int i = 0; i < AV_NUM_DATA_POINTERS && frame.data[i]; ++i) {
23 if (frame.linesize[i] < 0)
36 if (frame->hw_frames_ctx) {
37 m_hwFrame = std::move(frame);
38 m_pixelFormat = toQtPixelFormat(HWAccel::format(m_hwFrame.get()));
42 m_swFrame = std::move(frame);
43 m_pixelFormat = toQtPixelFormat(AVPixelFormat(m_swFrame->format));
54 const auto actualAVPixelFormat = AVPixelFormat(m_swFrame->format);
55 const auto targetAVPixelFormat = toAVPixelFormat(m_pixelFormat);
57 const QSize actualSize(m_swFrame->width, m_swFrame->height);
58 if (actualAVPixelFormat != targetAVPixelFormat || isFrameFlipped(*m_swFrame)
59 || m_size != actualSize) {
60 Q_ASSERT(toQtPixelFormat(targetAVPixelFormat) == m_pixelFormat);
62 SwsContextUPtr scaleContext = createSwsContext(actualSize, actualAVPixelFormat, m_size,
63 targetAVPixelFormat, SWS_BICUBIC);
65 auto newFrame = makeAVFrame();
66 newFrame->width = m_size.width();
67 newFrame->height = m_size.height();
68 newFrame->format = targetAVPixelFormat;
69 av_frame_get_buffer(newFrame.get(), 0);
71 sws_scale(scaleContext.get(), m_swFrame->data, m_swFrame->linesize, 0, m_swFrame->height,
72 newFrame->data, newFrame->linesize);
73 if (m_frame == m_swFrame.get())
74 m_frame = newFrame.get();
75 m_swFrame = std::move(newFrame);
85 ensureTextureConverter(rhi);
88 m_type = m_hwFrame && TextureConverter::isBackendAvailable(*m_hwFrame, rhi)
89 ? QVideoFrame::RhiTextureHandle
90 : QVideoFrame::NoHandle;
97 HwFrameContextData &frameContextData = HwFrameContextData::ensure(*m_hwFrame);
98 TextureConverter *converter = frameContextData.textureConverterMapper.get(&rhi);
102 std::tie(converter, added) =
103 frameContextData.textureConverterMapper.tryMap(rhi, TextureConverter(rhi));
106 Q_ASSERT(converter && added);
117 HwFrameContextData &frameContextData = HwFrameContextData::ensure(*m_hwFrame);
118 return frameContextData.textureConverterMapper.findRhi(
119 [](QRhi &rhi) {
return rhi.thread()->isCurrentThread(); });
124 return fromAvColorSpace(m_frame->colorspace);
129 return fromAvColorTransfer(m_frame->color_trc);
134 return fromAvColorRange(m_frame->color_range);
140 for (
int i = 0; i < m_frame->nb_side_data; ++i) {
141 AVFrameSideData *sd = m_frame->side_data[i];
143 if (sd->type == AV_FRAME_DATA_MASTERING_DISPLAY_METADATA) {
144 auto *data =
reinterpret_cast<AVMasteringDisplayMetadata *>(sd->data);
145 auto maybeLum = QFFmpeg::mul(qreal(10'000.), data->max_luminance);
147 maxNits =
float(maybeLum.value());
156 Q_ASSERT(m_hwFrame && m_hwFrame->hw_frames_ctx);
157 m_swFrame = makeAVFrame();
159 int ret = av_hwframe_transfer_data(m_swFrame.get(), m_hwFrame.get(), 0);
161 qWarning() <<
"Error transferring the data to system memory:" << ret;
170 auto *desc = QVideoTextureHelper::textureDescription(pixelFormat());
171 mapData.planeCount = desc->nplanes;
172 for (
int i = 0; i < mapData.planeCount; ++i) {
173 Q_ASSERT(m_swFrame->linesize[i] >= 0);
175 mapData.data[i] = m_swFrame->data[i];
176 mapData.bytesPerLine[i] = m_swFrame->linesize[i];
177 mapData.dataSize[i] = mapData.bytesPerLine[i]*desc->heightForPlane(m_swFrame->height, i);
180 if ((mode & QVideoFrame::WriteOnly) != 0 && m_hwFrame) {
181 m_type = QVideoFrame::NoHandle;
192 m_mode = QVideoFrame::NotMapped;
195QVideoFrameTexturesUPtr
QFFmpegVideoBuffer::mapTextures(QRhi &rhi, QVideoFrameTexturesUPtr& oldTextures)
197 Q_ASSERT(rhi.thread()->isCurrentThread());
199 QVideoFrameTexturesUPtr result = createTexturesFromHwFrame(rhi, oldTextures);
202 m_type = result ? QVideoFrame::RhiTextureHandle : QVideoFrame::NoHandle;
206QVideoFrameTexturesUPtr
QFFmpegVideoBuffer::createTexturesFromHwFrame(QRhi &rhi, QVideoFrameTexturesUPtr& oldTextures) {
216 constexpr bool initTextureConverterForAnyRhi =
false;
218 TextureConverter *converter = initTextureConverterForAnyRhi
219 ? &ensureTextureConverter(rhi)
220 : HwFrameContextData::ensure(*m_hwFrame).textureConverterMapper.get(&rhi);
225 if (!converter->init(*m_hwFrame))
228 const QVideoFrameTextures *oldTexturesRaw = oldTextures.get();
229 if (QVideoFrameTexturesUPtr newTextures = converter->createTextures(*m_hwFrame, oldTextures))
232 Q_ASSERT(oldTextures.get() == oldTexturesRaw);
234 QVideoFrameTexturesHandlesUPtr oldTextureHandles =
235 oldTextures ? oldTextures->takeHandles() :
nullptr;
236 QVideoFrameTexturesHandlesUPtr newTextureHandles =
237 converter->createTextureHandles(*m_hwFrame, std::move(oldTextureHandles));
239 if (newTextureHandles) {
240 QVideoFrameTexturesUPtr newTextures = QVideoTextureHelper::createTexturesFromHandles(
241 std::move(newTextureHandles), rhi, m_pixelFormat,
242 { m_hwFrame->width, m_hwFrame->height });
247 static thread_local int lastFormat = 0;
248 if (std::exchange(lastFormat, m_hwFrame->format) != m_hwFrame->format)
249 qWarning() <<
" failed to get textures for frame; format:" << m_hwFrame->format;
256 return m_pixelFormat;
264QVideoFrameFormat::PixelFormat
QFFmpegVideoBuffer::toQtPixelFormat(AVPixelFormat avPixelFormat,
bool *needsConversion)
267 *needsConversion =
false;
269 switch (avPixelFormat) {
272 case AV_PIX_FMT_NONE:
273 Q_ASSERT(!
"Invalid avPixelFormat!");
274 return QVideoFrameFormat::Format_Invalid;
275 case AV_PIX_FMT_ARGB:
276 return QVideoFrameFormat::Format_ARGB8888;
277 case AV_PIX_FMT_0RGB:
278 return QVideoFrameFormat::Format_XRGB8888;
279 case AV_PIX_FMT_BGRA:
280 return QVideoFrameFormat::Format_BGRA8888;
281 case AV_PIX_FMT_BGR0:
282 return QVideoFrameFormat::Format_BGRX8888;
283 case AV_PIX_FMT_ABGR:
284 return QVideoFrameFormat::Format_ABGR8888;
285 case AV_PIX_FMT_0BGR:
286 return QVideoFrameFormat::Format_XBGR8888;
287 case AV_PIX_FMT_RGBA:
288 return QVideoFrameFormat::Format_RGBA8888;
289 case AV_PIX_FMT_RGB0:
290 return QVideoFrameFormat::Format_RGBX8888;
292 case AV_PIX_FMT_YUV422P:
293 return QVideoFrameFormat::Format_YUV422P;
294 case AV_PIX_FMT_YUV420P:
295 return QVideoFrameFormat::Format_YUV420P;
296 case AV_PIX_FMT_YUV420P10:
297 return QVideoFrameFormat::Format_YUV420P10;
298 case AV_PIX_FMT_UYVY422:
299 return QVideoFrameFormat::Format_UYVY;
300 case AV_PIX_FMT_YUYV422:
301 return QVideoFrameFormat::Format_YUYV;
302 case AV_PIX_FMT_NV12:
303 return QVideoFrameFormat::Format_NV12;
304 case AV_PIX_FMT_NV21:
305 return QVideoFrameFormat::Format_NV21;
306 case AV_PIX_FMT_GRAY8:
307 return QVideoFrameFormat::Format_Y8;
308 case AV_PIX_FMT_GRAY16:
309 return QVideoFrameFormat::Format_Y16;
311 case AV_PIX_FMT_P010:
312 return QVideoFrameFormat::Format_P010;
313 case AV_PIX_FMT_P016:
314 return QVideoFrameFormat::Format_P016;
315 case AV_PIX_FMT_MEDIACODEC:
316 return QVideoFrameFormat::Format_SamplerExternalOES;
320 *needsConversion =
true;
322 const AVPixFmtDescriptor *descriptor = av_pix_fmt_desc_get(avPixelFormat);
324 if (descriptor->flags & AV_PIX_FMT_FLAG_RGB)
325 return QVideoFrameFormat::Format_RGBA8888;
327 if (descriptor->comp[0].depth > 8)
328 return QVideoFrameFormat::Format_P016;
329 return QVideoFrameFormat::Format_YUV420P;
334 switch (pixelFormat) {
336 case QVideoFrameFormat::Format_Invalid:
337 case QVideoFrameFormat::Format_AYUV:
338 case QVideoFrameFormat::Format_AYUV_Premultiplied:
339 case QVideoFrameFormat::Format_YV12:
340 case QVideoFrameFormat::Format_IMC1:
341 case QVideoFrameFormat::Format_IMC2:
342 case QVideoFrameFormat::Format_IMC3:
343 case QVideoFrameFormat::Format_IMC4:
344 return AV_PIX_FMT_NONE;
345 case QVideoFrameFormat::Format_Jpeg:
347 return AV_PIX_FMT_BGRA;
348 case QVideoFrameFormat::Format_ARGB8888:
349 return AV_PIX_FMT_ARGB;
350 case QVideoFrameFormat::Format_ARGB8888_Premultiplied:
351 case QVideoFrameFormat::Format_XRGB8888:
352 return AV_PIX_FMT_0RGB;
353 case QVideoFrameFormat::Format_BGRA8888:
354 return AV_PIX_FMT_BGRA;
355 case QVideoFrameFormat::Format_BGRA8888_Premultiplied:
356 case QVideoFrameFormat::Format_BGRX8888:
357 return AV_PIX_FMT_BGR0;
358 case QVideoFrameFormat::Format_ABGR8888:
359 return AV_PIX_FMT_ABGR;
360 case QVideoFrameFormat::Format_XBGR8888:
361 return AV_PIX_FMT_0BGR;
362 case QVideoFrameFormat::Format_RGBA8888:
363 return AV_PIX_FMT_RGBA;
366 case QVideoFrameFormat::Format_RGBX8888:
367 return AV_PIX_FMT_RGB0;
369 case QVideoFrameFormat::Format_YUV422P:
370 return AV_PIX_FMT_YUV422P;
371 case QVideoFrameFormat::Format_YUV420P:
372 return AV_PIX_FMT_YUV420P;
373 case QVideoFrameFormat::Format_YUV420P10:
374 return AV_PIX_FMT_YUV420P10;
375 case QVideoFrameFormat::Format_UYVY:
376 return AV_PIX_FMT_UYVY422;
377 case QVideoFrameFormat::Format_YUYV:
378 return AV_PIX_FMT_YUYV422;
379 case QVideoFrameFormat::Format_NV12:
380 return AV_PIX_FMT_NV12;
381 case QVideoFrameFormat::Format_NV21:
382 return AV_PIX_FMT_NV21;
383 case QVideoFrameFormat::Format_Y8:
384 return AV_PIX_FMT_GRAY8;
385 case QVideoFrameFormat::Format_Y16:
386 return AV_PIX_FMT_GRAY16;
388 case QVideoFrameFormat::Format_P010:
389 return AV_PIX_FMT_P010;
390 case QVideoFrameFormat::Format_P016:
391 return AV_PIX_FMT_P016;
393 case QVideoFrameFormat::Format_SamplerExternalOES:
394 return AV_PIX_FMT_MEDIACODEC;
QVideoFrameFormat::PixelFormat pixelFormat() const
QVideoFrameFormat::ColorSpace colorSpace() const
QRhi * rhi() const 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.
QVideoFrameFormat::ColorTransfer colorTransfer() const
QVideoFrameFormat::ColorRange colorRange() const
QFFmpegVideoBuffer(AVFrameUPtr frame, AVRational pixelAspectRatio={ 1, 1 })
~QFFmpegVideoBuffer() override
static bool isFrameFlipped(const AVFrame &frame)