Loading...
Searching...
No Matches
YUVA420.hpp
1#pragma once
2#include <Gfx/Graph/decoders/ColorSpace.hpp>
3#include <Gfx/Graph/decoders/GPUVideoDecoder.hpp>
4
5extern "C" {
6#include <libavformat/avformat.h>
7}
8
9namespace score::gfx
10{
11
20{
21 static const constexpr auto frag = R"_(#version 450
22
23)_" SCORE_GFX_VIDEO_UNIFORMS R"_(
24
25layout(binding=3) uniform sampler2D y_tex;
26layout(binding=4) uniform sampler2D u_tex;
27layout(binding=5) uniform sampler2D v_tex;
28layout(binding=6) uniform sampler2D a_tex;
29
30layout(location = 0) in vec2 v_texcoord;
31layout(location = 0) out vec4 fragColor;
32
33%2
34
35vec4 processTexture(vec4 tex) {
36 vec4 processed = convert_to_rgb(tex);
37 { %1 }
38 return processed;
39}
40
41void main ()
42{
43 float y = texture(y_tex, v_texcoord).r;
44 float u = texture(u_tex, v_texcoord).r;
45 float v = texture(v_tex, v_texcoord).r;
46 float a = texture(a_tex, v_texcoord).r;
47
48 vec4 rgb = processTexture(vec4(y,u,v, 1.));
49 fragColor = vec4(rgb.rgb, a);
50})_";
51
53 : decoder{d}
54 {
55 }
56
57 Video::ImageFormat& decoder;
58
59 std::pair<QShader, QShader> init(RenderList& r) override
60 {
61 auto& rhi = *r.state.rhi;
62 const auto w = decoder.width, h = decoder.height;
63
64 // Y plane (full res)
65 {
66 auto tex = rhi.newTexture(QRhiTexture::R8, {w, h}, 1, QRhiTexture::Flag{});
67 tex->create();
68
69 auto sampler = rhi.newSampler(
70 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
71 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
72 sampler->create();
73 samplers.push_back({sampler, tex});
74 }
75
76 // U plane (half res)
77 {
78 auto tex
79 = rhi.newTexture(QRhiTexture::R8, {w / 2, h / 2}, 1, QRhiTexture::Flag{});
80 tex->create();
81
82 auto sampler = rhi.newSampler(
83 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
84 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
85 sampler->create();
86 samplers.push_back({sampler, tex});
87 }
88
89 // V plane (half res)
90 {
91 auto tex
92 = rhi.newTexture(QRhiTexture::R8, {w / 2, h / 2}, 1, QRhiTexture::Flag{});
93 tex->create();
94
95 auto sampler = rhi.newSampler(
96 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
97 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
98 sampler->create();
99 samplers.push_back({sampler, tex});
100 }
101
102 // A plane (full res)
103 {
104 auto tex = rhi.newTexture(QRhiTexture::R8, {w, h}, 1, QRhiTexture::Flag{});
105 tex->create();
106
107 auto sampler = rhi.newSampler(
108 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
109 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
110 sampler->create();
111 samplers.push_back({sampler, tex});
112 }
113
115 r.state, vertexShader(), QString(frag).arg("").arg(colorMatrix(decoder)));
116 }
117
118 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
119 {
120 const auto w = decoder.width, h = decoder.height;
121
122 // Y
123 {
124 QRhiTextureUploadEntry entry{
125 0, 0, createTextureUpload(frame.data[0], w, h, 1, frame.linesize[0])};
126 QRhiTextureUploadDescription desc{entry};
127 res.uploadTexture(samplers[0].texture, desc);
128 }
129 // U
130 {
131 QRhiTextureUploadEntry entry{
132 0, 0,
133 createTextureUpload(frame.data[1], w / 2, h / 2, 1, frame.linesize[1])};
134 QRhiTextureUploadDescription desc{entry};
135 res.uploadTexture(samplers[1].texture, desc);
136 }
137 // V
138 {
139 QRhiTextureUploadEntry entry{
140 0, 0,
141 createTextureUpload(frame.data[2], w / 2, h / 2, 1, frame.linesize[2])};
142 QRhiTextureUploadDescription desc{entry};
143 res.uploadTexture(samplers[2].texture, desc);
144 }
145 // A
146 {
147 QRhiTextureUploadEntry entry{
148 0, 0, createTextureUpload(frame.data[3], w, h, 1, frame.linesize[3])};
149 QRhiTextureUploadDescription desc{entry};
150 res.uploadTexture(samplers[3].texture, desc);
151 }
152 }
153};
154
155}
Processes and renders a video frame on the GPU.
Definition GPUVideoDecoder.hpp:43
static QRhiTextureSubresourceUploadDescription createTextureUpload(uint8_t *pixels, int w, int h, int bytesPerPixel, int stride)
Utility method to create a QRhiTextureSubresourceUploadDescription.
Definition GPUVideoDecoder.cpp:22
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:394
Definition VideoInterface.hpp:18
Decodes YUVA420P videos.
Definition YUVA420.hpp:20
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition YUVA420.hpp:118
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition YUVA420.hpp:59