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 | #ifndef QFFMPEGHWACCEL_P_H |
4 | #define QFFMPEGHWACCEL_P_H |
5 | |
6 | // |
7 | // W A R N I N G |
8 | // ------------- |
9 | // |
10 | // This file is not part of the Qt API. It exists purely as an |
11 | // implementation detail. This header file may change from version to |
12 | // version without notice, or even be removed. |
13 | // |
14 | // We mean it. |
15 | // |
16 | |
17 | #include <QtFFmpegMediaPluginImpl/private/qffmpeg_p.h> |
18 | #include <QtFFmpegMediaPluginImpl/private/qffmpegtextureconverter_p.h> |
19 | #include "qvideoframeformat.h" |
20 | #include <QtMultimedia/private/qhwvideobuffer_p.h> |
21 | #include <QtMultimedia/private/qrhivaluemapper_p.h> |
22 | |
23 | #include <qshareddata.h> |
24 | #include <memory> |
25 | #include <functional> |
26 | #include <mutex> |
27 | |
28 | QT_BEGIN_NAMESPACE |
29 | |
30 | class QRhi; |
31 | class QRhiTexture; |
32 | class QFFmpegVideoBuffer; |
33 | |
34 | namespace QFFmpeg { |
35 | |
36 | // used for the get_format callback for the decoder |
37 | AVPixelFormat getFormat(AVCodecContext *s, const AVPixelFormat *fmt); |
38 | |
39 | class HWAccel; |
40 | |
41 | class HWAccel; |
42 | using HWAccelUPtr = std::unique_ptr<HWAccel>; |
43 | |
44 | /** |
45 | * @brief The HwFrameContextData class contains custom belongings |
46 | * of hw frames context. |
47 | */ |
48 | struct HwFrameContextData |
49 | { |
50 | QRhiValueMapper<TextureConverter> textureConverterMapper; |
51 | |
52 | /** |
53 | * @brief gets or creates an instance of the class, associated with |
54 | * the frames context of the specified frame. Note, AVFrame |
55 | * holds shared ownership of the frames context, so consider this |
56 | * when designing HwFrameContextData's lifetime. |
57 | */ |
58 | static HwFrameContextData &ensure(AVFrame &hwFrame); |
59 | }; |
60 | |
61 | class HWAccel |
62 | { |
63 | AVBufferUPtr m_hwDeviceContext; |
64 | AVBufferUPtr m_hwFramesContext; |
65 | |
66 | mutable std::once_flag m_constraintsOnceFlag; |
67 | mutable AVHWFramesConstraintsUPtr m_constraints; |
68 | |
69 | public: |
70 | ~HWAccel(); |
71 | |
72 | static HWAccelUPtr create(AVHWDeviceType deviceType); |
73 | |
74 | static std::pair<std::optional<Codec>, HWAccelUPtr> findDecoderWithHwAccel(AVCodecID id); |
75 | |
76 | AVHWDeviceType deviceType() const; |
77 | |
78 | AVBufferRef *hwDeviceContextAsBuffer() const { return m_hwDeviceContext.get(); } |
79 | AVHWDeviceContext *hwDeviceContext() const; |
80 | AVPixelFormat hwFormat() const; |
81 | const AVHWFramesConstraints *constraints() const; |
82 | |
83 | bool matchesSizeContraints(QSize size) const; |
84 | |
85 | void createFramesContext(AVPixelFormat swFormat, const QSize &size); |
86 | AVBufferRef *hwFramesContextAsBuffer() const { return m_hwFramesContext.get(); } |
87 | AVHWFramesContext *hwFramesContext() const; |
88 | |
89 | static AVPixelFormat format(AVFrame *frame); |
90 | static const std::vector<AVHWDeviceType> &encodingDeviceTypes(); |
91 | |
92 | static const std::vector<AVHWDeviceType> &decodingDeviceTypes(); |
93 | |
94 | private: |
95 | HWAccel(AVBufferUPtr hwDeviceContext) : m_hwDeviceContext(std::move(hwDeviceContext)) { } |
96 | }; |
97 | |
98 | AVFrameUPtr copyFromHwPool(AVFrameUPtr frame); |
99 | |
100 | } // namespace QFFmpeg |
101 | |
102 | QT_END_NAMESPACE |
103 | |
104 | #endif |
105 | |