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
84QFFmpeg::TextureConverter &QFFmpegVideoBuffer::ensureTextureConverter(QRhi &rhi)
85{
86 Q_ASSERT(m_hwFrame);
87
88 HwFrameContextData &frameContextData = HwFrameContextData::ensure(*m_hwFrame);
89 TextureConverter *converter = frameContextData.textureConverterMapper.get(&rhi);
90
91 if (!converter) {
92 bool added = false;
93 std::tie(converter, added) =
94 frameContextData.textureConverterMapper.tryMap(rhi, TextureConverter(rhi));
95 // no issues are expected if it's already added in another thread, however,it's worth to
96 // check it
97 Q_ASSERT(converter && added);
98 }
99
100 return *converter;
101}
102
104{
105 if (!m_hwFrame)
106 return nullptr;
107
108 HwFrameContextData &frameContextData = HwFrameContextData::ensure(*m_hwFrame);
109 return frameContextData.textureConverterMapper.findRhi(
110 [](QRhi &rhi) { return rhi.thread()->isCurrentThread(); });
111}
112
114{
115 return fromAvColorSpace(m_frame->colorspace);
116}
117
119{
120 return fromAvColorTransfer(m_frame->color_trc);
121}
122
124{
125 return fromAvColorRange(m_frame->color_range);
126}
127
129{
130 float maxNits = -1;
131 for (int i = 0; i < m_frame->nb_side_data; ++i) {
132 AVFrameSideData *sd = m_frame->side_data[i];
133 // TODO: Longer term we might want to also support HDR10+ dynamic metadata
134 if (sd->type == AV_FRAME_DATA_MASTERING_DISPLAY_METADATA) {
135 auto *data = reinterpret_cast<AVMasteringDisplayMetadata *>(sd->data);
136 // max_luminance is a rational in cd/m2 (nits) already, valid with has_luminance
137 if (!data->has_luminance)
138 continue;
139 auto maybeLum = QFFmpeg::toReal(data->max_luminance);
140 if (maybeLum)
141 maxNits = float(*maybeLum);
142 }
143 }
144 return maxNits;
145}
146
148{
149 if (!m_swFrame) {
150 Q_ASSERT(m_hwFrame && m_hwFrame->hw_frames_ctx);
151 m_swFrame = makeAVFrame();
152 /* retrieve data from GPU to CPU */
153 int ret = av_hwframe_transfer_data(m_swFrame.get(), m_hwFrame.get(), 0);
154 if (ret < 0) {
155 qWarning() << "Error transferring the data to system memory:" << ret;
156 return {};
157 }
159 }
160
161 m_mode = mode;
162
163 MapData mapData;
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);
168
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);
172 }
173
174 if ((mode & QVideoFrame::WriteOnly) != 0 && m_hwFrame)
175 m_hwFrame.reset();
176
177 return mapData;
178}
179
181{
182 // nothing to do here for SW buffers.
183 // Set NotMapped mode to ensure map/unmap/mapMode consisteny.
184 m_mode = QVideoFrame::NotMapped;
185}
186
187QVideoFrameTexturesUPtr QFFmpegVideoBuffer::mapTextures(QRhi &rhi, QVideoFrameTexturesUPtr& oldTextures)
188{
189 Q_ASSERT(rhi.thread()->isCurrentThread());
190
191 return createTexturesFromHwFrame(rhi, oldTextures);
192}
193
194QVideoFrameTexturesUPtr QFFmpegVideoBuffer::createTexturesFromHwFrame(QRhi &rhi, QVideoFrameTexturesUPtr& oldTextures) {
195
196 if (!m_hwFrame)
197 return {};
198
199 TextureConverter &converter = ensureTextureConverter(rhi);
200
201 if (!converter.init(*m_hwFrame))
202 return {};
203
204 const QVideoFrameTextures *oldTexturesRaw = oldTextures.get();
205 if (QVideoFrameTexturesUPtr newTextures = converter.createTextures(*m_hwFrame, oldTextures))
206 return newTextures;
207
208 Q_ASSERT(oldTextures.get() == oldTexturesRaw);
209
210 QVideoFrameTexturesHandlesUPtr oldTextureHandles =
211 oldTextures ? oldTextures->takeHandles() : nullptr;
212 QVideoFrameTexturesHandlesUPtr newTextureHandles =
213 converter.createTextureHandles(*m_hwFrame, std::move(oldTextureHandles));
214
215 if (newTextureHandles) {
216 QVideoFrameTexturesUPtr newTextures = QVideoTextureHelper::createTexturesFromHandles(
217 std::move(newTextureHandles), rhi, m_pixelFormat, frameSize(m_hwFrame));
218
219 return newTextures;
220 }
221
222 static thread_local int lastFormat = 0;
223 if (std::exchange(lastFormat, m_hwFrame->format) != m_hwFrame->format) // prevent logging spam
224 qWarning() << " failed to get textures for frame; format:" << m_hwFrame->format;
225
226 return {};
227}
228
230{
231 return m_pixelFormat;
232}
233
235{
236 return m_size;
237}
238
239QVideoFrameFormat::PixelFormat QFFmpegVideoBuffer::toQtPixelFormat(AVPixelFormat avPixelFormat, bool *needsConversion)
240{
241 if (needsConversion)
242 *needsConversion = false;
243
244 switch (avPixelFormat) {
245 default:
246 break;
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;
266
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;
285
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;
292 }
293
294 if (needsConversion)
295 *needsConversion = true;
296
297 const AVPixFmtDescriptor *descriptor = av_pix_fmt_desc_get(avPixelFormat);
298
299 if (descriptor->flags & AV_PIX_FMT_FLAG_RGB)
300 return QVideoFrameFormat::Format_RGBA8888;
301
302 if (descriptor->comp[0].depth > 8)
303 return QVideoFrameFormat::Format_P016;
304 return QVideoFrameFormat::Format_YUV420P;
305}
306
307AVPixelFormat QFFmpegVideoBuffer::toAVPixelFormat(QVideoFrameFormat::PixelFormat pixelFormat)
308{
309 switch (pixelFormat) {
310 default:
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:
321 // We're using the data from the converted QImage here, which is in BGRA.
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;
339 // to be added in 6.8:
340 // case QVideoFrameFormat::Format_RGBA8888_Premultiplied:
341 case QVideoFrameFormat::Format_RGBX8888:
342 return AV_PIX_FMT_RGB0;
343
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;
362
363 case QVideoFrameFormat::Format_P010:
364 return AV_PIX_FMT_P010;
365 case QVideoFrameFormat::Format_P016:
366 return AV_PIX_FMT_P016;
367
368 case QVideoFrameFormat::Format_SamplerExternalOES:
369 return AV_PIX_FMT_MEDIACODEC;
370 }
371}
372
373QT_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,...)