Loading...
Searching...
No Matches
GPUVideoDecoder.hpp
1#pragma once
2
3#include <Gfx/Graph/Node.hpp>
4#include <Gfx/Graph/RenderList.hpp>
5#include <Gfx/Graph/RenderState.hpp>
6#include <Video/VideoInterface.hpp>
7
8extern "C" {
9#include <libavutil/pixdesc.h>
10}
11
12#define SCORE_GFX_VIDEO_UNIFORMS \
13"layout(std140, binding = 0) uniform renderer_t {\n" \
14" mat4 clipSpaceCorrMatrix;\n" \
15" vec2 renderSize;\n" \
16"} renderer;\n" \
17"\n" \
18"layout(std140, binding = 2) uniform material_t {\n" \
19" vec2 scale;\n" \
20" vec2 texSz;\n" \
21"} mat;\n"
22
23namespace score::gfx
24{
25
29{
30 int log2ChromaW{1};
31 int log2ChromaH{1};
32 int bitDepth{8};
33 int numPlanes{2};
34 bool hasAlpha{};
35
36 bool is10bit() const noexcept { return bitDepth > 8; }
37
39 static PixelFormatInfo fromAVPixelFormat(AVPixelFormat fmt)
40 {
41 PixelFormatInfo info;
42 const AVPixFmtDescriptor* desc = av_pix_fmt_desc_get(fmt);
43 if(desc)
44 {
45 info.log2ChromaW = desc->log2_chroma_w;
46 info.log2ChromaH = desc->log2_chroma_h;
47 info.bitDepth = desc->comp[0].depth;
48 info.numPlanes = av_pix_fmt_count_planes(fmt);
49 info.hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) != 0;
50 }
51 return info;
52 }
53
57 AVPixelFormat swFormat, AVPixelFormat codecparFormat,
58 int bitsPerRawSample)
59 {
60 PixelFormatInfo info;
61 // Prefer sw_format (from hw_frames_ctx) when available
62 AVPixelFormat fmt = (swFormat != AV_PIX_FMT_NONE) ? swFormat : codecparFormat;
63 if(fmt != AV_PIX_FMT_NONE)
64 info = fromAVPixelFormat(fmt);
65 // bits_per_raw_sample overrides depth when set (some containers report it)
66 if(bitsPerRawSample > info.bitDepth)
67 info.bitDepth = bitsPerRawSample;
68 return info;
69 }
70};
71
72// TODO the "model" nodes should have a first update step so that they
73// can share data across all renderers during a tick
74class VideoNode;
75
90{
91public:
93 virtual ~GPUVideoDecoder();
94
104 [[nodiscard]] virtual std::pair<QShader, QShader> init(RenderList& r) = 0;
105
109 virtual void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) = 0;
110
114 void release(RenderList&);
115
122 static QRhiTextureSubresourceUploadDescription
123 createTextureUpload(uint8_t* pixels, int w, int h, int bytesPerPixel, int stride);
124
125 static QString vertexShader(bool invertY = false) noexcept;
126
127 std::vector<Sampler> samplers;
128
132 bool hasFrame{};
133
137 bool failed{};
138};
139
144{
145 static const constexpr auto hashtag_no_filter = R"_(#version 450
146 void main ()
147 {
148 }
149 )_";
150
151 explicit EmptyDecoder() { }
152
153 std::pair<QShader, QShader> init(RenderList& r) override
154 {
155 return score::gfx::makeShaders(r.state, vertexShader(), hashtag_no_filter);
156 }
157
158 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override { }
159};
160}
Processes and renders a video frame on the GPU.
Definition GPUVideoDecoder.hpp:90
bool hasFrame
Definition GPUVideoDecoder.hpp:132
virtual void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame)=0
Decode and upload a video frame to the GPU.
virtual std::pair< QShader, QShader > init(RenderList &r)=0
Initialize a GPUVideoDecoder.
bool failed
Definition GPUVideoDecoder.hpp:137
static QRhiTextureSubresourceUploadDescription createTextureUpload(uint8_t *pixels, int w, int h, int bytesPerPixel, int stride)
Utility method to create a QRhiTextureSubresourceUploadDescription.
Definition GPUVideoDecoder.cpp:22
void release(RenderList &)
This method will release all the created samplers and textures.
Definition GPUVideoDecoder.cpp:10
List of nodes to be rendered to an output.
Definition RenderList.hpp:19
RenderState & state
RenderState corresponding to this RenderList.
Definition RenderList.hpp:94
Graphics rendering pipeline for ossia score.
Definition Filter/PreviewWidget.hpp:12
std::pair< QShader, QShader > makeShaders(const RenderState &v, QString vert, QString frag)
Get a pair of compiled vertex / fragment shaders from GLSL 4.5 sources.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:395
STL namespace.
Default decoder when we do not know what to render.
Definition GPUVideoDecoder.hpp:144
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition GPUVideoDecoder.hpp:158
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition GPUVideoDecoder.hpp:153
Definition GPUVideoDecoder.hpp:29
int log2ChromaW
Horizontal chroma subsampling: 0=4:4:4, 1=4:2:2/4:2:0.
Definition GPUVideoDecoder.hpp:30
static PixelFormatInfo fromAVPixelFormat(AVPixelFormat fmt)
Build from an AVPixelFormat.
Definition GPUVideoDecoder.hpp:39
int log2ChromaH
Vertical chroma subsampling: 0=4:4:4/4:2:2, 1=4:2:0.
Definition GPUVideoDecoder.hpp:31
int bitDepth
Per-component bit depth (8, 10, 12, 16)
Definition GPUVideoDecoder.hpp:32
bool hasAlpha
Format includes an alpha channel.
Definition GPUVideoDecoder.hpp:34
int numPlanes
Number of planes (2 for semi-planar, 3+ for planar)
Definition GPUVideoDecoder.hpp:33
static PixelFormatInfo fromCodecParameters(AVPixelFormat swFormat, AVPixelFormat codecparFormat, int bitsPerRawSample)
Definition GPUVideoDecoder.hpp:56
Stores a sampler and the texture currently associated with it.
Definition score-plugin-gfx/Gfx/Graph/Utils.hpp:26