Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qffmpegvideobuffer.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include "private/qvideotexturehelper_p.h"
6#include "private/qmultimediautils_p.h"
9#include <QtCore/qthread.h>
10
11extern "C" {
12#include <libavutil/pixdesc.h>
13#include <libavutil/hdr_dynamic_metadata.h>
14#include <libavutil/mastering_display_metadata.h>
15}
16
17QT_BEGIN_NAMESPACE
18
19using namespace QFFmpeg;
20
21Q_STATIC_LOGGING_CATEGORY(qLcFFmpegVideoBuffer, "qt.multimedia.ffmpeg.videobuffer");
22
23static bool isFrameFlipped(const AVFrame& frame) {
24 for (int i = 0; i < AV_NUM_DATA_POINTERS && frame.data[i]; ++i) {
25 if (frame.linesize[i] < 0)
26 return true;
27 }
28
29 return false;
30}
31
32QFFmpegVideoBuffer::QFFmpegVideoBuffer(AVFrameUPtr frame, AVRational pixelAspectRatio)
33 : m_frame(frame.get()),
35{
36 if (frame->hw_frames_ctx) {
37 m_hwFrame = std::move(frame);
38 m_pixelFormat = toQtPixelFormat(HWAccel::format(m_hwFrame.get()));
39 return;
40 }
41
42 m_swFrame = std::move(frame);
43 m_pixelFormat = toQtPixelFormat(AVPixelFormat(m_swFrame->format));
44
46}
47
49
51{
52 Q_ASSERT(m_swFrame);
53
54 const auto actualAVPixelFormat = AVPixelFormat(m_swFrame->format);
55 const auto targetAVPixelFormat = toAVPixelFormat(m_pixelFormat);
56
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);
61 // convert the format into something we can handle
62 SwsContextUPtr scaleContext = createSwsContext(actualSize, actualAVPixelFormat, m_size,
63 targetAVPixelFormat, SWS_BICUBIC);
64
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);
70 if (status < 0) {
71 qCWarning(qLcFFmpegVideoBuffer)
72 << "Failed to allocate frame buffer:" << AVError(status);
73 return;
74 }
75
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);
81 }
82}
83
84void QFFmpegVideoBuffer::initTextureConverter(QRhi &rhi)
85{
86 if (!m_hwFrame)
87 return;
88
89 // don't use the result reference here
90 ensureTextureConverter(rhi);
91}
92
93QFFmpeg::TextureConverter &QFFmpegVideoBuffer::ensureTextureConverter(QRhi &rhi)
94{
95 Q_ASSERT(m_hwFrame);
96
97 HwFrameContextData &frameContextData = HwFrameContextData::ensure(*m_hwFrame);
98 TextureConverter *converter = frameContextData.textureConverterMapper.get(&rhi);
99
100 if (!converter) {
101 bool added = false;
102 std::tie(converter, added) =
103 frameContextData.textureConverterMapper.tryMap(rhi, TextureConverter(rhi));
104 // no issues are expected if it's already added in another thread, however,it's worth to
105 // check it
106 Q_ASSERT(converter && added);
107 }
108
109 return *converter;
110}
111
113{
114 if (!m_hwFrame)
115 return nullptr;
116
117 HwFrameContextData &frameContextData = HwFrameContextData::ensure(*m_hwFrame);
118 return frameContextData.textureConverterMapper.findRhi(
119 [](QRhi &rhi) { return rhi.thread()->isCurrentThread(); });
120}
121
123{
124 return fromAvColorSpace(m_frame->colorspace);
125}
126
128{
129 return fromAvColorTransfer(m_frame->color_trc);
130}
131
133{
134 return fromAvColorRange(m_frame->color_range);
135}
136
138{
139 float maxNits = -1;
140 for (int i = 0; i < m_frame->nb_side_data; ++i) {
141 AVFrameSideData *sd = m_frame->side_data[i];
142 // TODO: Longer term we might want to also support HDR10+ dynamic metadata
143 if (sd->type == AV_FRAME_DATA_MASTERING_DISPLAY_METADATA) {
144 auto *data = reinterpret_cast<AVMasteringDisplayMetadata *>(sd->data);
145 // max_luminance is a rational in cd/m2 (nits) already, valid with has_luminance
146 if (!data->has_luminance)
147 continue;
148 auto maybeLum = QFFmpeg::toReal(data->max_luminance);
149 if (maybeLum)
150 maxNits = float(*maybeLum);
151 }
152 }
153 return maxNits;
154}
155
157{
158 if (!m_swFrame) {
159 Q_ASSERT(m_hwFrame && m_hwFrame->hw_frames_ctx);
160 m_swFrame = makeAVFrame();
161 /* retrieve data from GPU to CPU */
162 int ret = av_hwframe_transfer_data(m_swFrame.get(), m_hwFrame.get(), 0);
163 if (ret < 0) {
164 qWarning() << "Error transferring the data to system memory:" << ret;
165 return {};
166 }
168 }
169
170 m_mode = mode;
171
172 MapData mapData;
173 auto *desc = QVideoTextureHelper::textureDescription(pixelFormat());
174 mapData.planeCount = desc->nplanes;
175 for (int i = 0; i < mapData.planeCount; ++i) {
176 Q_ASSERT(m_swFrame->linesize[i] >= 0);
177
178 mapData.data[i] = m_swFrame->data[i];
179 mapData.bytesPerLine[i] = m_swFrame->linesize[i];
180 mapData.dataSize[i] = mapData.bytesPerLine[i]*desc->heightForPlane(m_swFrame->height, i);
181 }
182
183 if ((mode & QVideoFrame::WriteOnly) != 0 && m_hwFrame)
184 m_hwFrame.reset();
185
186 return mapData;
187}
188
190{
191 // nothing to do here for SW buffers.
192 // Set NotMapped mode to ensure map/unmap/mapMode consisteny.
193 m_mode = QVideoFrame::NotMapped;
194}
195
196QVideoFrameTexturesUPtr QFFmpegVideoBuffer::mapTextures(QRhi &rhi, QVideoFrameTexturesUPtr& oldTextures)
197{
198 Q_ASSERT(rhi.thread()->isCurrentThread());
199
200 return createTexturesFromHwFrame(rhi, oldTextures);
201}
202
203QVideoFrameTexturesUPtr QFFmpegVideoBuffer::createTexturesFromHwFrame(QRhi &rhi, QVideoFrameTexturesUPtr& oldTextures) {
204
205 if (!m_hwFrame)
206 return {};
207
208 // QTBUG-132200:
209 // We aim to set initTextureConverterForAnyRhi=true for as much platforms as we can,
210 // and remove the check after all platforms work fine on CI. If the flag is enabled,
211 // QVideoFrame::toImage can work faster, and we can test hw texture conversion on CI.
212 // Currently, enabling the flag fails some CI platforms.
213 constexpr bool initTextureConverterForAnyRhi = false;
214
215 TextureConverter *converter = initTextureConverterForAnyRhi
216 ? &ensureTextureConverter(rhi)
217 : HwFrameContextData::ensure(*m_hwFrame).textureConverterMapper.get(&rhi);
218
219 if (!converter)
220 return {};
221
222 if (!converter->init(*m_hwFrame))
223 return {};
224
225 const QVideoFrameTextures *oldTexturesRaw = oldTextures.get();
226 if (QVideoFrameTexturesUPtr newTextures = converter->createTextures(*m_hwFrame, oldTextures))
227 return newTextures;
228
229 Q_ASSERT(oldTextures.get() == oldTexturesRaw);
230
231 QVideoFrameTexturesHandlesUPtr oldTextureHandles =
232 oldTextures ? oldTextures->takeHandles() : nullptr;
233 QVideoFrameTexturesHandlesUPtr newTextureHandles =
234 converter->createTextureHandles(*m_hwFrame, std::move(oldTextureHandles));
235
236 if (newTextureHandles) {
237 QVideoFrameTexturesUPtr newTextures = QVideoTextureHelper::createTexturesFromHandles(
238 std::move(newTextureHandles), rhi, m_pixelFormat, frameSize(m_hwFrame));
239
240 return newTextures;
241 }
242
243 static thread_local int lastFormat = 0;
244 if (std::exchange(lastFormat, m_hwFrame->format) != m_hwFrame->format) // prevent logging spam
245 qWarning() << " failed to get textures for frame; format:" << m_hwFrame->format;
246
247 return {};
248}
249
251{
252 return m_pixelFormat;
253}
254
256{
257 return m_size;
258}
259
260QVideoFrameFormat::PixelFormat QFFmpegVideoBuffer::toQtPixelFormat(AVPixelFormat avPixelFormat, bool *needsConversion)
261{
262 if (needsConversion)
263 *needsConversion = false;
264
265 switch (avPixelFormat) {
266 default:
267 break;
268 case AV_PIX_FMT_NONE:
269 Q_ASSERT(!"Invalid avPixelFormat!");
270 return QVideoFrameFormat::Format_Invalid;
271 case AV_PIX_FMT_ARGB:
272 return QVideoFrameFormat::Format_ARGB8888;
273 case AV_PIX_FMT_0RGB:
274 return QVideoFrameFormat::Format_XRGB8888;
275 case AV_PIX_FMT_BGRA:
276 return QVideoFrameFormat::Format_BGRA8888;
277 case AV_PIX_FMT_BGR0:
278 return QVideoFrameFormat::Format_BGRX8888;
279 case AV_PIX_FMT_ABGR:
280 return QVideoFrameFormat::Format_ABGR8888;
281 case AV_PIX_FMT_0BGR:
282 return QVideoFrameFormat::Format_XBGR8888;
283 case AV_PIX_FMT_RGBA:
284 return QVideoFrameFormat::Format_RGBA8888;
285 case AV_PIX_FMT_RGB0:
286 return QVideoFrameFormat::Format_RGBX8888;
287
288 case AV_PIX_FMT_YUV422P:
289 return QVideoFrameFormat::Format_YUV422P;
290 case AV_PIX_FMT_YUV420P:
291 return QVideoFrameFormat::Format_YUV420P;
292 case AV_PIX_FMT_YUV420P10:
293 return QVideoFrameFormat::Format_YUV420P10;
294 case AV_PIX_FMT_UYVY422:
295 return QVideoFrameFormat::Format_UYVY;
296 case AV_PIX_FMT_YUYV422:
297 return QVideoFrameFormat::Format_YUYV;
298 case AV_PIX_FMT_NV12:
299 return QVideoFrameFormat::Format_NV12;
300 case AV_PIX_FMT_NV21:
301 return QVideoFrameFormat::Format_NV21;
302 case AV_PIX_FMT_GRAY8:
303 return QVideoFrameFormat::Format_Y8;
304 case AV_PIX_FMT_GRAY16:
305 return QVideoFrameFormat::Format_Y16;
306
307 case AV_PIX_FMT_P010:
308 return QVideoFrameFormat::Format_P010;
309 case AV_PIX_FMT_P016:
310 return QVideoFrameFormat::Format_P016;
311 case AV_PIX_FMT_MEDIACODEC:
312 return QVideoFrameFormat::Format_SamplerExternalOES;
313 }
314
315 if (needsConversion)
316 *needsConversion = true;
317
318 const AVPixFmtDescriptor *descriptor = av_pix_fmt_desc_get(avPixelFormat);
319
320 if (descriptor->flags & AV_PIX_FMT_FLAG_RGB)
321 return QVideoFrameFormat::Format_RGBA8888;
322
323 if (descriptor->comp[0].depth > 8)
324 return QVideoFrameFormat::Format_P016;
325 return QVideoFrameFormat::Format_YUV420P;
326}
327
328AVPixelFormat QFFmpegVideoBuffer::toAVPixelFormat(QVideoFrameFormat::PixelFormat pixelFormat)
329{
330 switch (pixelFormat) {
331 default:
332 case QVideoFrameFormat::Format_Invalid:
333 case QVideoFrameFormat::Format_AYUV:
334 case QVideoFrameFormat::Format_AYUV_Premultiplied:
335 case QVideoFrameFormat::Format_YV12:
336 case QVideoFrameFormat::Format_IMC1:
337 case QVideoFrameFormat::Format_IMC2:
338 case QVideoFrameFormat::Format_IMC3:
339 case QVideoFrameFormat::Format_IMC4:
340 return AV_PIX_FMT_NONE;
341 case QVideoFrameFormat::Format_Jpeg:
342 // We're using the data from the converted QImage here, which is in BGRA.
343 return AV_PIX_FMT_BGRA;
344 case QVideoFrameFormat::Format_ARGB8888:
345 return AV_PIX_FMT_ARGB;
346 case QVideoFrameFormat::Format_ARGB8888_Premultiplied:
347 case QVideoFrameFormat::Format_XRGB8888:
348 return AV_PIX_FMT_0RGB;
349 case QVideoFrameFormat::Format_BGRA8888:
350 return AV_PIX_FMT_BGRA;
351 case QVideoFrameFormat::Format_BGRA8888_Premultiplied:
352 case QVideoFrameFormat::Format_BGRX8888:
353 return AV_PIX_FMT_BGR0;
354 case QVideoFrameFormat::Format_ABGR8888:
355 return AV_PIX_FMT_ABGR;
356 case QVideoFrameFormat::Format_XBGR8888:
357 return AV_PIX_FMT_0BGR;
358 case QVideoFrameFormat::Format_RGBA8888:
359 return AV_PIX_FMT_RGBA;
360 // to be added in 6.8:
361 // case QVideoFrameFormat::Format_RGBA8888_Premultiplied:
362 case QVideoFrameFormat::Format_RGBX8888:
363 return AV_PIX_FMT_RGB0;
364
365 case QVideoFrameFormat::Format_YUV422P:
366 return AV_PIX_FMT_YUV422P;
367 case QVideoFrameFormat::Format_YUV420P:
368 return AV_PIX_FMT_YUV420P;
369 case QVideoFrameFormat::Format_YUV420P10:
370 return AV_PIX_FMT_YUV420P10;
371 case QVideoFrameFormat::Format_UYVY:
372 return AV_PIX_FMT_UYVY422;
373 case QVideoFrameFormat::Format_YUYV:
374 return AV_PIX_FMT_YUYV422;
375 case QVideoFrameFormat::Format_NV12:
376 return AV_PIX_FMT_NV12;
377 case QVideoFrameFormat::Format_NV21:
378 return AV_PIX_FMT_NV21;
379 case QVideoFrameFormat::Format_Y8:
380 return AV_PIX_FMT_GRAY8;
381 case QVideoFrameFormat::Format_Y16:
382 return AV_PIX_FMT_GRAY16;
383
384 case QVideoFrameFormat::Format_P010:
385 return AV_PIX_FMT_P010;
386 case QVideoFrameFormat::Format_P016:
387 return AV_PIX_FMT_P016;
388
389 case QVideoFrameFormat::Format_SamplerExternalOES:
390 return AV_PIX_FMT_MEDIACODEC;
391 }
392}
393
394QT_END_NAMESPACE
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,...)