Loading...
Searching...
No Matches
YUYV422.hpp
1#pragma once
2#include <Gfx/Graph/decoders/ColorSpace.hpp>
3#include <Gfx/Graph/decoders/GPUVideoDecoder.hpp>
4extern "C" {
5#include <libavformat/avformat.h>
6}
7
8namespace score::gfx
9{
16{
17 static const constexpr auto frag = R"_(#version 450
18
19)_" SCORE_GFX_VIDEO_UNIFORMS R"_(
20
21layout(binding=3) uniform sampler2D u_tex;
22
23layout(location = 0) in vec2 v_texcoord;
24layout(location = 0) out vec4 fragColor;
25
26%2
27
28vec4 processTexture(vec4 tex) {
29 vec4 processed = convert_to_rgb(tex);
30 { %1 }
31 return processed;
32}
33
34void main() {
35 // YUYV packs Y0 U Y1 V into RGBA as x=Y0 y=U z=Y1 w=V
36 // Input texture is half the width of the output (one RGBA texel = 2 pixels)
37 float colIndex = floor(v_texcoord.x * mat.texSz.x);
38 float oddCol = mod(colIndex, 2.0);
39
40 // Offset by half an input pixel to sample the correct texel center
41 vec2 dxInput = 0.5 * vec2(1.0 / mat.texSz.x, 0.0);
42
43 float oddY = texture(u_tex, v_texcoord - dxInput).z;
44 float evenY = texture(u_tex, v_texcoord + dxInput).x;
45 float y = mix(evenY, oddY, oddCol);
46
47 vec2 uv = texture(u_tex, v_texcoord).yw;
48
49 fragColor = processTexture(vec4(y, uv.x, uv.y, 1.));
50}
51)_";
52
54 : decoder{d}
55 {
56 }
57
58 Video::ImageFormat& decoder;
59 std::pair<QShader, QShader> init(RenderList& r) override
60 {
61 auto& rhi = *r.state.rhi;
62
63 const auto w = decoder.width, h = decoder.height;
64 // Y
65 {
66 auto tex = rhi.newTexture(QRhiTexture::RGBA8, {w / 2, 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
77 r.state, vertexShader(), QString(frag).arg("").arg(colorMatrix(decoder)));
78 }
79
80 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
81 {
82 setYPixels(res, frame.data[0], frame.linesize[0]);
83 }
84
85 void
86 setYPixels(QRhiResourceUpdateBatch& res, uint8_t* pixels, int stride) const noexcept
87 {
88 const auto w = decoder.width, h = decoder.height;
89 auto y_tex = samplers[0].texture;
90
91 // Texture is RGBA8 at {w/2, h}: 4 bytes per texel = one YUYV macropixel
92 QRhiTextureUploadEntry entry{0, 0, createTextureUpload(pixels, w / 2, h, 4, stride)};
93
94 QRhiTextureUploadDescription desc{entry};
95 res.uploadTexture(y_tex, desc);
96 }
97};
98
105{
106 static const constexpr auto frag = R"_(#version 450
107
108)_" SCORE_GFX_VIDEO_UNIFORMS R"_(
109
110layout(binding=3) uniform sampler2D u_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 // UYVY packs U0 Y0 V0 Y1 into RGBA as x=U0 y=Y0 z=V0 w=Y1
125 // Input texture is half the width of the output (one RGBA texel = 2 pixels)
126 float colIndex = floor(v_texcoord.x * mat.texSz.x);
127 float oddCol = mod(colIndex, 2.0);
128
129 // Offset by half an input pixel to sample the correct texel center
130 vec2 dxInput = 0.5 * vec2(1.0 / mat.texSz.x, 0.0);
131
132 float oddY = texture(u_tex, v_texcoord - dxInput).w;
133 float evenY = texture(u_tex, v_texcoord + dxInput).y;
134 float y = mix(evenY, oddY, oddCol);
135
136 vec2 uv = texture(u_tex, v_texcoord).xz;
137
138 fragColor = processTexture(vec4(y, uv.x, uv.y, 1.));
139}
140)_";
141
143 : decoder{d}
144 {
145 }
146
147 Video::ImageFormat& decoder;
148 std::pair<QShader, QShader> init(RenderList& r) override
149 {
150 auto& rhi = *r.state.rhi;
151
152 const auto w = decoder.width, h = decoder.height;
153 // Y
154 {
155 auto tex = rhi.newTexture(QRhiTexture::RGBA8, {w / 2, h}, 1, QRhiTexture::Flag{});
156 tex->create();
157
158 auto sampler = rhi.newSampler(
159 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
160 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
161 sampler->create();
162 samplers.push_back({sampler, tex});
163 }
164
166 r.state, vertexShader(), QString(frag).arg("").arg(colorMatrix(decoder)));
167 }
168
169 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
170 {
171 auto y_tex = samplers[0].texture;
172
173 auto pixels = frame.data[0];
174 auto stride = frame.linesize[0];
175 // Texture is RGBA8 at {w/2, h}: 4 bytes per texel = one UYVY macropixel
176 QRhiTextureUploadEntry entry{
177 0, 0, createTextureUpload(pixels, frame.width / 2, frame.height, 4, stride)};
178
179 QRhiTextureUploadDescription desc{entry};
180 res.uploadTexture(y_tex, desc);
181 }
182};
183
184}
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 UYVY422 video, mostly used for NDI.
Definition YUYV422.hpp:105
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition YUYV422.hpp:169
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition YUYV422.hpp:148
Decodes YUYV422 video.
Definition YUYV422.hpp:16
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition YUYV422.hpp:59
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition YUYV422.hpp:80