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)
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 = frameSize(m_swFrame);
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 const int status = av_frame_get_buffer(newFrame.get(), 0);
72 <<
"Failed to allocate frame buffer:" << AVError(status);
76 sws_scale(scaleContext.get(), m_swFrame->data, m_swFrame->linesize, 0, m_swFrame->height,
77 newFrame->data, newFrame->linesize);
78 if (m_frame == m_swFrame.get())
79 m_frame = newFrame.get();
80 m_swFrame = std::move(newFrame);
88 HwFrameContextData &frameContextData = HwFrameContextData::ensure(*m_hwFrame);
89 TextureConverter *converter = frameContextData.textureConverterMapper.get(&rhi);
93 std::tie(converter, added) =
94 frameContextData.textureConverterMapper.tryMap(rhi, TextureConverter(rhi));
97 Q_ASSERT(converter && added);
108 HwFrameContextData &frameContextData = HwFrameContextData::ensure(*m_hwFrame);
109 return frameContextData.textureConverterMapper.findRhi(
110 [](QRhi &rhi) {
return rhi.thread()->isCurrentThread(); });
115 return fromAvColorSpace(m_frame->colorspace);
120 return fromAvColorTransfer(m_frame->color_trc);
125 return fromAvColorRange(m_frame->color_range);
131 for (
int i = 0; i < m_frame->nb_side_data; ++i) {
132 AVFrameSideData *sd = m_frame->side_data[i];
134 if (sd->type == AV_FRAME_DATA_MASTERING_DISPLAY_METADATA) {
135 auto *data =
reinterpret_cast<AVMasteringDisplayMetadata *>(sd->data);
137 if (!data->has_luminance)
139 auto maybeLum = QFFmpeg::toReal(data->max_luminance);
141 maxNits =
float(*maybeLum);
150 Q_ASSERT(m_hwFrame && m_hwFrame->hw_frames_ctx);
151 m_swFrame = makeAVFrame();
153 int ret = av_hwframe_transfer_data(m_swFrame.get(), m_hwFrame.get(), 0);
155 qWarning() <<
"Error transferring the data to system memory:" << ret;
164 auto *desc = QVideoTextureHelper::textureDescription(pixelFormat());
165 mapData.planeCount = desc->nplanes;
166 for (
int i = 0; i < mapData.planeCount; ++i) {
167 Q_ASSERT(m_swFrame->linesize[i] >= 0);
169 mapData.data[i] = m_swFrame->data[i];
170 mapData.bytesPerLine[i] = m_swFrame->linesize[i];
171 mapData.dataSize[i] = mapData.bytesPerLine[i]*desc->heightForPlane(m_swFrame->height, i);
174 if ((mode & QVideoFrame::WriteOnly) != 0 && m_hwFrame)
184 m_mode = QVideoFrame::NotMapped;
187QVideoFrameTexturesUPtr
QFFmpegVideoBuffer::mapTextures(QRhi &rhi, QVideoFrameTexturesUPtr& oldTextures)
189 Q_ASSERT(rhi.thread()->isCurrentThread());
191 return createTexturesFromHwFrame(rhi, oldTextures);
194QVideoFrameTexturesUPtr
QFFmpegVideoBuffer::createTexturesFromHwFrame(QRhi &rhi, QVideoFrameTexturesUPtr& oldTextures) {
199 TextureConverter &converter = ensureTextureConverter(rhi);
201 if (!converter.init(*m_hwFrame))
204 const QVideoFrameTextures *oldTexturesRaw = oldTextures.get();
205 if (QVideoFrameTexturesUPtr newTextures = converter.createTextures(*m_hwFrame, oldTextures))
208 Q_ASSERT(oldTextures.get() == oldTexturesRaw);
210 QVideoFrameTexturesHandlesUPtr oldTextureHandles =
211 oldTextures ? oldTextures->takeHandles() :
nullptr;
212 QVideoFrameTexturesHandlesUPtr newTextureHandles =
213 converter.createTextureHandles(*m_hwFrame,
std::move(oldTextureHandles));
215 if (newTextureHandles) {
216 QVideoFrameTexturesUPtr newTextures = QVideoTextureHelper::createTexturesFromHandles(
217 std::move(newTextureHandles), rhi, m_pixelFormat, frameSize(m_hwFrame));
222 static thread_local int lastFormat = 0;
223 if (std::exchange(lastFormat, m_hwFrame->format) != m_hwFrame->format)
224 qWarning() <<
" failed to get textures for frame; format:" << m_hwFrame->format;
231 return m_pixelFormat;
239QVideoFrameFormat::PixelFormat
QFFmpegVideoBuffer::toQtPixelFormat(AVPixelFormat avPixelFormat,
bool *needsConversion)
242 *needsConversion =
false;
244 switch (avPixelFormat) {
247 case AV_PIX_FMT_NONE:
248 Q_ASSERT(!
"Invalid avPixelFormat!");
249 return QVideoFrameFormat::Format_Invalid;
250 case AV_PIX_FMT_ARGB:
251 return QVideoFrameFormat::Format_ARGB8888;
252 case AV_PIX_FMT_0RGB:
253 return QVideoFrameFormat::Format_XRGB8888;
254 case AV_PIX_FMT_BGRA:
255 return QVideoFrameFormat::Format_BGRA8888;
256 case AV_PIX_FMT_BGR0:
257 return QVideoFrameFormat::Format_BGRX8888;
258 case AV_PIX_FMT_ABGR:
259 return QVideoFrameFormat::Format_ABGR8888;
260 case AV_PIX_FMT_0BGR:
261 return QVideoFrameFormat::Format_XBGR8888;
262 case AV_PIX_FMT_RGBA:
263 return QVideoFrameFormat::Format_RGBA8888;
264 case AV_PIX_FMT_RGB0:
265 return QVideoFrameFormat::Format_RGBX8888;
267 case AV_PIX_FMT_YUV422P:
268 return QVideoFrameFormat::Format_YUV422P;
269 case AV_PIX_FMT_YUV420P:
270 return QVideoFrameFormat::Format_YUV420P;
271 case AV_PIX_FMT_YUV420P10:
272 return QVideoFrameFormat::Format_YUV420P10;
273 case AV_PIX_FMT_UYVY422:
274 return QVideoFrameFormat::Format_UYVY;
275 case AV_PIX_FMT_YUYV422:
276 return QVideoFrameFormat::Format_YUYV;
277 case AV_PIX_FMT_NV12:
278 return QVideoFrameFormat::Format_NV12;
279 case AV_PIX_FMT_NV21:
280 return QVideoFrameFormat::Format_NV21;
281 case AV_PIX_FMT_GRAY8:
282 return QVideoFrameFormat::Format_Y8;
283 case AV_PIX_FMT_GRAY16:
284 return QVideoFrameFormat::Format_Y16;
286 case AV_PIX_FMT_P010:
287 return QVideoFrameFormat::Format_P010;
288 case AV_PIX_FMT_P016:
289 return QVideoFrameFormat::Format_P016;
290 case AV_PIX_FMT_MEDIACODEC:
291 return QVideoFrameFormat::Format_SamplerExternalOES;
295 *needsConversion =
true;
297 const AVPixFmtDescriptor *descriptor = av_pix_fmt_desc_get(avPixelFormat);
299 if (descriptor->flags & AV_PIX_FMT_FLAG_RGB)
300 return QVideoFrameFormat::Format_RGBA8888;
302 if (descriptor->comp[0].depth > 8)
303 return QVideoFrameFormat::Format_P016;
304 return QVideoFrameFormat::Format_YUV420P;
309 switch (pixelFormat) {
311 case QVideoFrameFormat::Format_Invalid:
312 case QVideoFrameFormat::Format_AYUV:
313 case QVideoFrameFormat::Format_AYUV_Premultiplied:
314 case QVideoFrameFormat::Format_YV12:
315 case QVideoFrameFormat::Format_IMC1:
316 case QVideoFrameFormat::Format_IMC2:
317 case QVideoFrameFormat::Format_IMC3:
318 case QVideoFrameFormat::Format_IMC4:
319 return AV_PIX_FMT_NONE;
320 case QVideoFrameFormat::Format_Jpeg:
322 return AV_PIX_FMT_BGRA;
323 case QVideoFrameFormat::Format_ARGB8888:
324 return AV_PIX_FMT_ARGB;
325 case QVideoFrameFormat::Format_ARGB8888_Premultiplied:
326 case QVideoFrameFormat::Format_XRGB8888:
327 return AV_PIX_FMT_0RGB;
328 case QVideoFrameFormat::Format_BGRA8888:
329 return AV_PIX_FMT_BGRA;
330 case QVideoFrameFormat::Format_BGRA8888_Premultiplied:
331 case QVideoFrameFormat::Format_BGRX8888:
332 return AV_PIX_FMT_BGR0;
333 case QVideoFrameFormat::Format_ABGR8888:
334 return AV_PIX_FMT_ABGR;
335 case QVideoFrameFormat::Format_XBGR8888:
336 return AV_PIX_FMT_0BGR;
337 case QVideoFrameFormat::Format_RGBA8888:
338 return AV_PIX_FMT_RGBA;
341 case QVideoFrameFormat::Format_RGBX8888:
342 return AV_PIX_FMT_RGB0;
344 case QVideoFrameFormat::Format_YUV422P:
345 return AV_PIX_FMT_YUV422P;
346 case QVideoFrameFormat::Format_YUV420P:
347 return AV_PIX_FMT_YUV420P;
348 case QVideoFrameFormat::Format_YUV420P10:
349 return AV_PIX_FMT_YUV420P10;
350 case QVideoFrameFormat::Format_UYVY:
351 return AV_PIX_FMT_UYVY422;
352 case QVideoFrameFormat::Format_YUYV:
353 return AV_PIX_FMT_YUYV422;
354 case QVideoFrameFormat::Format_NV12:
355 return AV_PIX_FMT_NV12;
356 case QVideoFrameFormat::Format_NV21:
357 return AV_PIX_FMT_NV21;
358 case QVideoFrameFormat::Format_Y8:
359 return AV_PIX_FMT_GRAY8;
360 case QVideoFrameFormat::Format_Y16:
361 return AV_PIX_FMT_GRAY16;
363 case QVideoFrameFormat::Format_P010:
364 return AV_PIX_FMT_P010;
365 case QVideoFrameFormat::Format_P016:
366 return AV_PIX_FMT_P016;
368 case QVideoFrameFormat::Format_SamplerExternalOES:
369 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,...)