Loading...
Searching...
No Matches
YUVA444.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
19{
20 static const constexpr auto frag = R"_(#version 450
21
22)_" SCORE_GFX_VIDEO_UNIFORMS R"_(
23
24layout(binding=3) uniform sampler2D y_tex;
25layout(binding=4) uniform sampler2D u_tex;
26layout(binding=5) uniform sampler2D v_tex;
27layout(binding=6) uniform sampler2D a_tex;
28
29layout(location = 0) in vec2 v_texcoord;
30layout(location = 0) out vec4 fragColor;
31
32%2
33
34vec4 processTexture(vec4 tex) {
35 vec4 processed = convert_to_rgb(tex);
36 { %1 }
37 return processed;
38}
39
40void main ()
41{
42 float y = texture(y_tex, v_texcoord).r;
43 float u = texture(u_tex, v_texcoord).r;
44 float v = texture(v_tex, v_texcoord).r;
45 float a = texture(a_tex, v_texcoord).r;
46
47 vec4 rgb = processTexture(vec4(y,u,v, 1.));
48 fragColor = vec4(rgb.rgb, a);
49})_";
50
52 : decoder{d}
53 {
54 }
55
56 Video::ImageFormat& decoder;
57
58 std::pair<QShader, QShader> init(RenderList& r) override
59 {
60 auto& rhi = *r.state.rhi;
61 const auto w = decoder.width, h = decoder.height;
62 const auto fmt = QRhiTexture::R8;
63
64 // Y, U, V, A - all at full resolution
65 for(int i = 0; i < 4; i++)
66 {
67 auto tex = rhi.newTexture(fmt, {w, h}, 1, QRhiTexture::Flag{});
68 tex->create();
69
70 auto sampler = rhi.newSampler(
71 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
72 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
73 sampler->create();
74 samplers.push_back({sampler, tex});
75 }
76
78 r.state, vertexShader(), QString(frag).arg("").arg(colorMatrix(decoder)));
79 }
80
81 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
82 {
83 const auto w = decoder.width, h = decoder.height;
84 for(int i = 0; i < 4; i++)
85 {
86 QRhiTextureUploadEntry entry{
87 0, 0, createTextureUpload(frame.data[i], w, h, 1, frame.linesize[i])};
88 QRhiTextureUploadDescription desc{entry};
89 res.uploadTexture(samplers[i].texture, desc);
90 }
91 }
92};
93
102{
103 static const constexpr auto frag = R"_(#version 450
104
105)_" SCORE_GFX_VIDEO_UNIFORMS R"_(
106
107layout(binding=3) uniform sampler2D y_tex;
108layout(binding=4) uniform sampler2D u_tex;
109layout(binding=5) uniform sampler2D v_tex;
110layout(binding=6) uniform sampler2D a_tex;
111
112layout(location = 0) in vec2 v_texcoord;
113layout(location = 0) out vec4 fragColor;
114
115%2
116
117vec4 processTexture(vec4 tex) {
118 vec4 processed = convert_to_rgb(tex);
119 { %1 }
120 return processed;
121}
122
123void main ()
124{
125 float y = 64. * texture(y_tex, v_texcoord).r;
126 float u = 64. * texture(u_tex, v_texcoord).r;
127 float v = 64. * texture(v_tex, v_texcoord).r;
128 float a = 64. * texture(a_tex, v_texcoord).r;
129
130 vec4 rgb = processTexture(vec4(y,u,v, 1.));
131 fragColor = vec4(rgb.rgb, a);
132})_";
133
135 : decoder{d}
136 {
137 }
138
139 Video::ImageFormat& decoder;
140
141 std::pair<QShader, QShader> init(RenderList& r) override
142 {
143 auto& rhi = *r.state.rhi;
144 const auto w = decoder.width, h = decoder.height;
145 const auto fmt = QRhiTexture::R16;
146
147 // Y, U, V, A - all at full resolution
148 for(int i = 0; i < 4; i++)
149 {
150 auto tex = rhi.newTexture(fmt, {w, h}, 1, QRhiTexture::Flag{});
151 tex->create();
152
153 auto sampler = rhi.newSampler(
154 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
155 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
156 sampler->create();
157 samplers.push_back({sampler, tex});
158 }
159
161 r.state, vertexShader(), QString(frag).arg("").arg(colorMatrix(decoder)));
162 }
163
164 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
165 {
166 const auto w = decoder.width, h = decoder.height;
167 for(int i = 0; i < 4; i++)
168 {
169 QRhiTextureUploadEntry entry{
170 0, 0, createTextureUpload(frame.data[i], w, h, 2, frame.linesize[i])};
171 QRhiTextureUploadDescription desc{entry};
172 res.uploadTexture(samplers[i].texture, desc);
173 }
174 }
175};
176
177}
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 YUVA444P videos.
Definition YUVA444.hpp:19
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition YUVA444.hpp:58
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition YUVA444.hpp:81
Decodes YUVA444P10 videos.
Definition YUVA444.hpp:102
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition YUVA444.hpp:141
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition YUVA444.hpp:164