YUV420P12.hpp
1 #pragma once
2 #include <Gfx/Graph/decoders/GPUVideoDecoder.hpp>
3 extern "C" {
4 #include <libavformat/avformat.h>
5 }
6 
7 namespace score::gfx
8 {
9 
17 {
18  static const constexpr auto yuv420_filter = R"_(#version 450
19 
20 )_" SCORE_GFX_VIDEO_UNIFORMS R"_(
21 
22  layout(binding=3) uniform sampler2D y_tex;
23  layout(binding=4) uniform sampler2D u_tex;
24  layout(binding=5) uniform sampler2D v_tex;
25 
26  layout(location = 0) in vec2 v_texcoord;
27  layout(location = 0) out vec4 fragColor;
28 
29  const vec3 R_cf = vec3(1.164383, 0.000000, 1.596027);
30  const vec3 G_cf = vec3(1.164383, -0.391762, -0.812968);
31  const vec3 B_cf = vec3(1.164383, 2.017232, 0.000000);
32  const vec3 offset = vec3(-0.0625, -0.5, -0.5);
33 
34  void main ()
35  {
36  float y = 16. * texture(y_tex, v_texcoord).r;
37  float u = 16. * texture(u_tex, v_texcoord).r;
38  float v = 16. * texture(v_tex, v_texcoord).r;
39  vec3 yuv = vec3(y,u,v);
40  yuv += offset;
41  fragColor = vec4(0.0, 0.0, 0.0, 1.0);
42  fragColor.r = dot(yuv, R_cf);
43  fragColor.g = dot(yuv, G_cf);
44  fragColor.b = dot(yuv, B_cf);
45  })_";
46 
48  : decoder{d}
49  {
50  }
51 
52  Video::ImageFormat& decoder;
53 
54  std::pair<QShader, QShader> init(RenderList& r) override
55  {
56  auto& rhi = *r.state.rhi;
57  const auto w = decoder.width, h = decoder.height;
58  const auto fmt = QRhiTexture::R16;
59 
60  // Y
61  {
62  auto tex = rhi.newTexture(fmt, {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(fmt, {w / 2, h / 2}, 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(fmt, {w / 2, h / 2}, 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 
96  return score::gfx::makeShaders(r.state, vertexShader(), yuv420_filter);
97  }
98 
99  void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
100  {
101  setYPixels(res, frame.data[0], frame.linesize[0]);
102  setUPixels(res, frame.data[1], frame.linesize[1]);
103  setVPixels(res, frame.data[2], frame.linesize[2]);
104  }
105 
106  void
107  setYPixels(QRhiResourceUpdateBatch& res, uint8_t* pixels, int stride) const noexcept
108  {
109  const auto w = decoder.width, h = decoder.height;
110  auto y_tex = samplers[0].texture;
111 
112  QRhiTextureUploadEntry entry{0, 0, createTextureUpload(pixels, w, h, 2, stride)};
113  QRhiTextureUploadDescription desc{entry};
114 
115  res.uploadTexture(y_tex, desc);
116  }
117 
118  void
119  setUPixels(QRhiResourceUpdateBatch& res, uint8_t* pixels, int stride) const noexcept
120  {
121  const auto w = decoder.width / 2, h = decoder.height / 2;
122  auto u_tex = samplers[1].texture;
123 
124  QRhiTextureUploadEntry entry{0, 0, createTextureUpload(pixels, w, h, 2, stride)};
125  QRhiTextureUploadDescription desc{entry};
126 
127  res.uploadTexture(u_tex, desc);
128  }
129 
130  void
131  setVPixels(QRhiResourceUpdateBatch& res, uint8_t* pixels, int stride) const noexcept
132  {
133  const auto w = decoder.width / 2, h = decoder.height / 2;
134  auto v_tex = samplers[2].texture;
135 
136  QRhiTextureUploadEntry entry{0, 0, createTextureUpload(pixels, w, h, 2, stride)};
137  QRhiTextureUploadDescription desc{entry};
138  res.uploadTexture(v_tex, desc);
139  }
140 };
141 
142 }
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 YUV420 videos.
Definition: YUV420P12.hpp:17
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition: YUV420P12.hpp:99
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition: YUV420P12.hpp:54