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