Loading...
Searching...
No Matches
UYVA.hpp
1#pragma once
2#include <Gfx/Graph/decoders/GPUVideoDecoder.hpp>
3
4namespace score::gfx
5{
6
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 a_tex;
27
28layout(location = 0) in vec2 v_texcoord;
29layout(location = 0) out vec4 fragColor;
30
31%2
32
33vec4 processTexture(vec4 tex) {
34 vec4 processed = convert_to_rgb(tex);
35 { %1 }
36 return processed;
37}
38
39void main() {
40 // UYVY packs U0 Y0 V0 Y1 into RGBA as x=U0 y=Y0 z=V0 w=Y1, so the texture
41 // is half the width of the picture and each texel is two pixels.
42 float colIndex = floor(v_texcoord.x * mat.texSz.x);
43 float oddCol = mod(colIndex, 2.0);
44
45 vec2 tc = score_tc(v_texcoord);
46 vec2 dxInput = 0.5 * vec2(1.0 / mat.texSz.x, 0.0);
47
48 float oddY = texture(y_tex, tc - dxInput).w;
49 float evenY = texture(y_tex, tc + dxInput).y;
50 float y = mix(evenY, oddY, oddCol);
51 vec2 uv = texture(y_tex, tc).xz;
52
53 // The alpha plane is full resolution, so it samples at the picture
54 // coordinate rather than the half-width one.
55 float a = texture(a_tex, tc).r;
56
57 vec4 rgb = processTexture(vec4(y, uv.x, uv.y, 1.));
58 fragColor = vec4(rgb.rgb, a);
59}
60)_";
61
63 : decoder{d}
64 {
65 }
66
67 Video::ImageFormat& decoder;
68
69 std::pair<QShader, QShader> init(RenderList& r) override
70 {
71 auto& rhi = *r.state.rhi;
72 const auto w = decoder.width, h = decoder.height;
73
74 auto mkSampler = [&] {
75 auto sampler = rhi.newSampler(
76 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
77 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
78 sampler->create();
79 return sampler;
80 };
81
82 { // UYVY, RGBA8 at half width
83 auto tex = rhi.newTexture(QRhiTexture::RGBA8, {w / 2, h}, 1, QRhiTexture::Flag{});
84 tex->create();
85 samplers.push_back({mkSampler(), tex});
86 }
87 { // alpha, one byte per pixel
88 auto tex = rhi.newTexture(QRhiTexture::R8, {w, h}, 1, QRhiTexture::Flag{});
89 tex->create();
90 samplers.push_back({mkSampler(), tex});
91 }
92
94 r.state, vertexShader(), QString(frag).arg("").arg(colorMatrix(decoder)));
95 }
96
97 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
98 {
99 {
100 const auto [rows, offset] = planeRows(decoder, frame, decoder.height);
101 auto sub = createTextureUpload(
102 frame.data[0], decoder.width / 2, rows, 4, frame.linesize[0]);
103 sub.setDestinationTopLeft({0, offset});
104 res.uploadTexture(samplers[0].texture, QRhiTextureUploadDescription{{0, 0, sub}});
105 }
106 {
107 const auto [rows, offset] = planeRows(decoder, frame, decoder.height);
108 auto sub = createTextureUpload(
109 frame.data[1], decoder.width, rows, 1, frame.linesize[1]);
110 sub.setDestinationTopLeft({0, offset});
111 res.uploadTexture(samplers[1].texture, QRhiTextureUploadDescription{{0, 0, sub}});
112 }
113 }
114};
115
116}
Processes and renders a video frame on the GPU.
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
List of nodes to be rendered to an output.
Definition RenderList.hpp:33
RenderState & state
RenderState corresponding to this RenderList.
Definition RenderList.hpp:259
Graphics rendering pipeline for ossia score.
Definition Filter/PreviewWidget.hpp:11
std::pair< QShader, QShader > makeShaders(const RenderState &v, QString vert, QString frag, int multiViewCount)
Get a pair of compiled vertex / fragment shaders from GLSL 4.5 sources.
Definition score-plugin-gfx/Gfx/Graph/Utils.cpp:1303
Definition VideoInterface.hpp:27
NDI UYVA: a UYVY 4:2:2 plane, then a full-resolution alpha plane.
Definition UYVA.hpp:20
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition UYVA.hpp:69
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition UYVA.hpp:97