Loading...
Searching...
No Matches
DirectVideoNodeRenderer.hpp
1#pragma once
2#include <Gfx/Graph/NodeRenderer.hpp>
3#include <Gfx/Graph/VideoNode.hpp>
4#include <Gfx/Graph/VideoNodeRenderer.hpp>
5#include <Gfx/Graph/decoders/GPUVideoDecoder.hpp>
6#include <Video/VideoInterface.hpp>
7
8#include <vector>
9
10class QRhi;
11
12extern "C" {
13struct AVFormatContext;
14struct AVCodecContext;
15struct AVStream;
16struct AVFrame;
17struct AVPacket;
18struct AVBufferRef;
19struct SwsContext;
20#include <libavutil/pixfmt.h>
21#include <libavutil/rational.h>
22}
23
24namespace Video
25{
26class Rescale;
27}
28
29namespace score::gfx
30{
31class GPUVideoDecoder;
32struct PixelFormatInfo;
33
49class SCORE_PLUGIN_GFX_TEST_EXPORT DirectVideoNodeRenderer : public NodeRenderer
50{
51public:
53 const VideoNodeBase& node, const Video::VideoMetadata& metadata) noexcept;
55
56 DirectVideoNodeRenderer() = delete;
59 DirectVideoNodeRenderer& operator=(const DirectVideoNodeRenderer&) = delete;
61
62 TextureRenderTarget renderTargetForInput(const Port& input) override;
63
64 void init(RenderList& renderer, QRhiResourceUpdateBatch& res) override;
65 void runRenderPass(RenderList&, QRhiCommandBuffer& commands, Edge& edge) override;
66 void update(RenderList& renderer, QRhiResourceUpdateBatch& res, Edge* edge) override;
67 void runInitialPasses(
68 RenderList&, QRhiCommandBuffer& commands, QRhiResourceUpdateBatch*& res,
69 Edge& edge) override;
70 QRhiTexture* textureForOutput(const Port& output) override;
71 void release(RenderList& r) override;
72
73 void initState(RenderList& renderer, QRhiResourceUpdateBatch& res) override;
74 void releaseState(RenderList& renderer) override;
75 void addOutputPass(
76 RenderList& renderer, Edge& edge, QRhiResourceUpdateBatch& res) override;
77 void removeOutputPass(RenderList& renderer, Edge& edge) override;
78 bool hasOutputPassForEdge(Edge& edge) const override;
79
80private:
81 const VideoNodeBase& node() const noexcept
82 {
83 return static_cast<const VideoNodeBase&>(NodeRenderer::node);
84 }
85
86 bool openFile(score::gfx::GraphicsApi api, QRhi* rhi = nullptr);
87 void closeFile();
88 bool seekAndDecode(int64_t flicks);
91 bool holdsTime(int64_t flicks) const noexcept;
93 bool seekTo(int64_t pts);
95 int64_t ptsToFlicks(int64_t pts) const noexcept;
96 int64_t flicksToPts(int64_t flicks) const noexcept;
97 bool isSequentialRead(int64_t flicks) const;
98 bool readVideoPacket(AVPacket* into);
99 bool readNextPacketRaw();
100 bool peekNextPacketRaw();
102 bool readNext();
103 bool peekNext();
105 bool readNextPacketAVCodec();
108 bool decodeNextFrame(AVFrame* into, int64_t& ts, int64_t& duration);
110 bool peekNextFrame();
111 void dropPeekedFrame() noexcept;
112
113 void createGpuDecoder(QRhi& rhi);
114 score::gfx::PixelFormatInfo hwPixelFormatInfo() const;
115 std::unique_ptr<GPUVideoDecoder> tryCreateZeroCopyDecoder(QRhi& rhi);
116 void setupGpuDecoder(RenderList& r);
117 void createPipelines(RenderList& r);
118
119 // Hardware decoding
120 AVPixelFormat selectHardwareAcceleration(
121 score::gfx::GraphicsApi api, int codec_id) const;
122 bool setupHardwareDecoder(
123 const void* codec, AVPixelFormat hwPixFmt);
124 static enum AVPixelFormat negotiateHWFormat(
125 AVCodecContext* ctx, const enum AVPixelFormat* pix_fmts);
126 void transferHWFrame();
127
128 // Video file info
129 std::string m_filePath;
130 Video::ImageFormat m_frameFormat{};
131 double m_fps{};
132 double m_flicks_per_dts{};
133 double m_dts_per_flicks{};
134 AVRational m_timeBase{0, 1};
136 int64_t m_startPts{};
139 int64_t m_framePts{1};
142 bool m_noTimestamps{};
143 int64_t m_nextMissingPts{};
144 bool m_useAVCodec{true};
145
146 // Own LibAV context
147 AVFormatContext* m_formatContext{};
148 AVCodecContext* m_codecContext{};
149 const void* m_codec{}; // AVCodec*
150 AVStream* m_avstream{};
151 AVFrame* m_decodedFrame{};
152
153 // Hardware decode state
154 AVBufferRef* m_hwDeviceCtx{};
155 AVPixelFormat m_hwPixelFormat{AV_PIX_FMT_NONE};
156 AVPixelFormat m_hwSwFormat{AV_PIX_FMT_NONE};
157 std::vector<const char*> m_vkEnabledExtensions; // kept alive for FFmpeg
158 AVFrame* m_swTransferFrame{};
159 score::gfx::GraphicsApi m_rhiApi{};
160 QRhi* m_rhi{};
161 bool m_hwSwFormatChecked{false};
162 bool m_zeroCopyFailed{false};
163 bool m_sharedVulkanDevice{false}; // true if FFmpeg uses QRhi's VkDevice
164
165 // Render state
166 PassMap m_p;
167 MeshBuffers m_meshBuffer{};
168 QRhiBuffer* m_processUBO{};
169 QRhiBuffer* m_materialUBO{};
170
172
173 std::unique_ptr<GPUVideoDecoder> m_gpu;
174 QShader m_cachedVertexShader;
175 QShader m_cachedFragmentShader;
176 score::gfx::ScaleMode m_currentScaleMode{};
177 QSize m_scaleRenderSize{};
178
179 int64_t m_lastRequestedFlicks{-1};
180 int64_t m_lastDecodedDts{INT64_MIN};
181 int64_t m_lastDecodedDuration{};
182 AVFrame* m_peekedFrame{};
183 int64_t m_peekedDts{};
184 int64_t m_peekedDuration{};
185 bool m_hasPeekedFrame{};
186 AVPacket* m_peekedPacket{};
187 bool m_hasPeekedPacket{};
189 int64_t m_ptsLead{};
191 std::vector<std::pair<int64_t, int64_t>> m_keyframes;
192 int64_t m_packetNumber{};
193 bool m_recomputeScale{true};
194 VideoOwnTexture m_ownTexture;
195};
196
197}
Renderer for intra-only video codecs with instant seeking.
Definition DirectVideoNodeRenderer.hpp:50
Renderer for a given node.
Definition NodeRenderer.hpp:11
List of nodes to be rendered to an output.
Definition RenderList.hpp:36
Definition VideoNode.hpp:82
The decoded frame, converted to RGBA into a texture of the renderer's own.
Definition VideoNodeRenderer.hpp:59
Graphics rendering pipeline for ossia score.
Definition Filter/PreviewWidget.hpp:11
AVPixelFormat selectHardwareAcceleration(score::gfx::GraphicsApi api, AVCodecID codec_id, QRhi *rhi)
Picks the best HW accel for the given API/codec, using QRhi for vendor detection.
Definition HWAccelSetup.cpp:36
GraphicsApi
Available graphics APIs to use.
Definition RenderState.hpp:25
ScaleMode
How to resize a texture to adapt it to a viewport.
Definition Scale.hpp:10
Definition VideoInterface.hpp:29
Definition VideoInterface.hpp:73
Connection between two score::gfx::Port.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:236
Definition Mesh.hpp:95
Definition GPUVideoDecoder.hpp:100
Port of a score::gfx::Node.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:215
Useful abstraction for storing all the data related to a render target.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:307
UBO shared across all video objects.
Definition CommonUBOs.hpp:108