Loading...
Searching...
No Matches
YUV420.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;
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{
41 float y = texture(y_tex, v_texcoord).r;
42 float u = texture(%3_tex, v_texcoord).r;
43 float v = texture(%4_tex, v_texcoord).r;
44
45 fragColor = processTexture(vec4(y,u,v, 1.));
46})_";
47
50 explicit YUV420Decoder(Video::ImageFormat& d, bool swapPlanes = false)
51 : decoder{d}
52 , yv12{swapPlanes}
53 {
54 }
55
56 Video::ImageFormat& decoder;
57 bool yv12{};
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 const auto fmt = QRhiTexture::R8;
64
65 // Y
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
77 // U
78 {
79 auto tex = rhi.newTexture(fmt, {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
90 {
91 auto tex = rhi.newTexture(fmt, {w / 2, h / 2}, 1, QRhiTexture::Flag{});
92 tex->create();
93
94 auto sampler = rhi.newSampler(
95 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
96 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
97 sampler->create();
98 samplers.push_back({sampler, tex});
99 }
100
102 r.state, vertexShader(),
103 QString(frag)
104 .arg("")
105 .arg(colorMatrix(decoder))
106 .arg(yv12 ? "v" : "u")
107 .arg(yv12 ? "u" : "v"));
108 }
109
110 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
111 {
112 setYPixels(res, frame.data[0], frame.linesize[0]);
113 setUPixels(res, frame.data[1], frame.linesize[1]);
114 setVPixels(res, frame.data[2], frame.linesize[2]);
115 }
116
117 void
118 setYPixels(QRhiResourceUpdateBatch& res, uint8_t* pixels, int stride) const noexcept
119 {
120 const auto w = decoder.width, h = decoder.height;
121 auto y_tex = samplers[0].texture;
122
123 QRhiTextureUploadEntry entry{0, 0, createTextureUpload(pixels, w, h, 1, stride)};
124 QRhiTextureUploadDescription desc{entry};
125
126 res.uploadTexture(y_tex, desc);
127 }
128
129 void
130 setUPixels(QRhiResourceUpdateBatch& res, uint8_t* pixels, int stride) const noexcept
131 {
132 const auto w = decoder.width / 2, h = decoder.height / 2;
133 auto u_tex = samplers[1].texture;
134
135 QRhiTextureUploadEntry entry{0, 0, createTextureUpload(pixels, w, h, 1, stride)};
136 QRhiTextureUploadDescription desc{entry};
137
138 res.uploadTexture(u_tex, desc);
139 }
140
141 void
142 setVPixels(QRhiResourceUpdateBatch& res, uint8_t* pixels, int stride) const noexcept
143 {
144 const auto w = decoder.width / 2, h = decoder.height / 2;
145 auto v_tex = samplers[2].texture;
146
147 QRhiTextureUploadEntry entry{0, 0, createTextureUpload(pixels, w, h, 1, stride)};
148 QRhiTextureUploadDescription desc{entry};
149 res.uploadTexture(v_tex, desc);
150 }
151};
152
153}
Processes and renders a video frame on the GPU.
Definition GPUVideoDecoder.hpp:90
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:30
RenderState & state
RenderState corresponding to this RenderList.
Definition RenderList.hpp:169
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:1238
Definition VideoInterface.hpp:26
Decodes YUV420 videos.
Definition YUV420.hpp:19
YUV420Decoder(Video::ImageFormat &d, bool swapPlanes=false)
Definition YUV420.hpp:50
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition YUV420.hpp:110
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition YUV420.hpp:59