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
qffmpegtextureconverter_p.h
Go to the documentation of this file.
1// Copyright (C) 2024 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
4#ifndef QFFMPEGTEXTURECONVERTER_P_H
5#define QFFMPEGTEXTURECONVERTER_P_H
6
7#include <QtFFmpegMediaPluginImpl/private/qffmpegdefs_p.h>
8#include <QtMultimedia/private/qhwvideobuffer_p.h>
9
10#include <memory>
11
12//
13// W A R N I N G
14// -------------
15//
16// This file is not part of the Qt API. It exists purely as an
17// implementation detail. This header file may change from version to
18// version without notice, or even be removed.
19//
20// We mean it.
21//
22
23QT_BEGIN_NAMESPACE
24
25class QRhi;
26
27namespace QFFmpeg {
28
29/**
30 * @brief The base class for platform-specific implementations of TextureConverter
31 * One of two virtual methods, createTextures and createTextureHandles, must be overridden.
32 * If the implementation operates with QRhiTexture under the hood, overriding of
33 * createTextures is prefferable, otherwise expose texture handles of native textures
34 * by createTextureHandles.
35 */
37{
38public:
39 TextureConverterBackend(QRhi *rhi) : rhi(rhi) { }
40
42
43 virtual QVideoFrameTexturesUPtr createTextures(AVFrame *hwFrame,
44 QVideoFrameTexturesUPtr & /*oldTextures*/)
45 {
46 return createTextures(hwFrame);
47 }
48 virtual QVideoFrameTexturesUPtr createTextures(AVFrame * /*hwFrame*/) { return nullptr; }
49
51 createTextureHandles(AVFrame *hwFrame, QVideoFrameTexturesHandlesUPtr /*oldHandles*/)
52 {
53 return createTextureHandles(hwFrame);
54 }
55
57 {
58 return nullptr;
59 }
60
61 /**
62 * @brief Points to the matching QRhi.
63 * If the constructor, createTextures, or createTextureHandles get failed without
64 * chances for recovery, it may set the pointer to nullptr, which will invalidate
65 * the parent TextureConverter, and textures creation won't be invoked anymore.
66 */
67 QRhi *rhi = nullptr;
68};
70
71/**
72 * @brief The TextureConverter class implements conversion of AVFrame hw textures to
73 * textures for rendering by the specified QRhi. Any instance of TextureConverter
74 * matches the pair of FFmpeg hw frames context + QRhi.
75 */
77{
78public:
79 /**
80 * @brief Construct uninitialized texture converter for the specified QRhi
81 */
82 TextureConverter(QRhi &rhi);
83
84 /**
85 * @brief Initializes the instance of the texture converter for the frame context
86 * associated with the specified frame. The method tries to initialize
87 * the conversion backend during the first call with the specified frame format.
88 * If frame format is not changed, the method does nothing even if the first
89 * attempt failed.
90 * @return Whether the instance has been initialized.
91 */
92 bool init(AVFrame &hwFrame);
93
94 /**
95 * @brief Creates video frame textures basing on the current hw frame and the previous textures
96 * from the texture pool. We should strive to reuse oldTextures if we can do so.
97 * If the method returns null, try createTextureHandles.
98 */
99 QVideoFrameTexturesUPtr createTextures(AVFrame &hwFrame, QVideoFrameTexturesUPtr &oldTextures);
100
101 /**
102 * @brief Creates video frame texture handles basing on the current hw frame and the previous
103 * texture handles from the pool. We should strive to reuse oldHandles if we can do so.
104 */
106 QVideoFrameTexturesHandlesUPtr oldHandles);
107
108 /**
109 * @brief Indicates whether the texture converter is not initialized or the initialization
110 * failed. If hw texture conversion is disabled, it always true.
111 */
112 bool isNull() const { return !m_backend || !m_backend->rhi; }
113
114 /**
115 * @brief Applies platform-specific hw texture conversion presets for a decoder.
116 * The function is supposed to be invoked for the get_format callback.
117 */
118 static void applyDecoderPreset(AVPixelFormat format, AVCodecContext &codecContext);
119
120 /**
121 * @brief Indicates whether hw texture conversion is enabled for the application.
122 */
123 static bool hwTextureConversionEnabled();
124
125 /**
126 * @brief Indicates whether the matching textute converter backend can be created.
127 * If isBackendAvailable returns false, instances cannot be initialized with
128 * the specified frame. If it returns true, init will attempt to create backend,
129 * but it may fail if something goes wrong in the backend.
130 */
131 static bool isBackendAvailable(AVFrame &hwFrame, QRhi &rhi);
132
133private:
134 void updateBackend(AVFrame &hwFrame);
135
136private:
137 QRhi &m_rhi;
138 AVPixelFormat m_format = AV_PIX_FMT_NONE;
140};
141
142} // namespace QFFmpeg
143
144QT_END_NAMESPACE
145
146#endif // QFFMPEGTEXTURECONVERTER_P_H
The base class for platform-specific implementations of TextureConverter One of two virtual methods,...
virtual QVideoFrameTexturesHandlesUPtr createTextureHandles(AVFrame *)
virtual QVideoFrameTexturesHandlesUPtr createTextureHandles(AVFrame *hwFrame, QVideoFrameTexturesHandlesUPtr)
virtual QVideoFrameTexturesUPtr createTextures(AVFrame *hwFrame, QVideoFrameTexturesUPtr &)
virtual QVideoFrameTexturesUPtr createTextures(AVFrame *)
QRhi * rhi
Points to the matching QRhi. If the constructor, createTextures, or createTextureHandles get failed w...
The TextureConverter class implements conversion of AVFrame hw textures to textures for rendering by ...
QVideoFrameTexturesHandlesUPtr createTextureHandles(AVFrame &hwFrame, QVideoFrameTexturesHandlesUPtr oldHandles)
Creates video frame texture handles basing on the current hw frame and the previous texture handles f...
TextureConverter(QRhi &rhi)
Construct uninitialized texture converter for the specified QRhi.
bool isNull() const
Indicates whether the texture converter is not initialized or the initialization failed....
static bool isBackendAvailable(AVFrame &hwFrame, QRhi &rhi)
Indicates whether the matching textute converter backend can be created. If isBackendAvailable return...
static void applyDecoderPreset(AVPixelFormat format, AVCodecContext &codecContext)
Applies platform-specific hw texture conversion presets for a decoder. The function is supposed to be...
bool init(AVFrame &hwFrame)
Initializes the instance of the texture converter for the frame context associated with the specified...
QVideoFrameTexturesUPtr createTextures(AVFrame &hwFrame, QVideoFrameTexturesUPtr &oldTextures)
Creates video frame textures basing on the current hw frame and the previous textures from the textur...
static bool hwTextureConversionEnabled()
Indicates whether hw texture conversion is enabled for the application.
std::shared_ptr< TextureConverterBackend > TextureConverterBackendPtr