Loading...
Searching...
No Matches
PackedBitfieldYUV.hpp
Go to the documentation of this file.
1#pragma once
2
19#include <Gfx/Graph/decoders/ColorSpace.hpp>
20#include <Gfx/Graph/decoders/GPUVideoDecoder.hpp>
22
23namespace score::gfx
24{
25
29{
30 BitField y, u, v, a;
31};
32
34{
35 static const constexpr auto frag = R"_(#version 450
36
37)_" SCORE_GFX_VIDEO_UNIFORMS R"_(
38
39layout(binding=3) uniform sampler2D u_tex;
40
41layout(location = 0) in vec2 v_texcoord;
42layout(location = 0) out vec4 fragColor;
43
44%2
45
46vec4 processTexture(vec4 tex) {
47 vec4 processed = tex;
48 { %1 }
49 return processed;
50}
51
52void main()
53{
54 vec4 texel = texture(u_tex, v_texcoord);
55 uint w = (uint(texel.g * 255.0 + 0.5) << 8) | uint(texel.r * 255.0 + 0.5);
56 float y = %3;
57 float u = %4;
58 float v = %5;
59 vec4 rgb = convert_to_rgb(vec4(y, u, v, 1.0));
60 fragColor = processTexture(vec4(rgb.rgb, %6));
61}
62)_";
63
65 : decoder{d}
66 , layout{l}
67 {
68 }
69
70 Video::ImageFormat& decoder;
72
73 static QString field(const BitField& f)
74 {
75 if(f.width <= 0)
76 return "1.0";
77 const unsigned mask = (1u << f.width) - 1u;
78 return QString("float((w >> %1) & %2u) / %3.0")
79 .arg(f.offset)
80 .arg(mask)
81 .arg(mask);
82 }
83
84 std::pair<QShader, QShader> init(RenderList& r) override
85 {
86 auto& rhi = *r.state.rhi;
87 const auto w = decoder.width, h = decoder.height;
88
89 auto tex = rhi.newTexture(QRhiTexture::RG8, {w, h}, 1, QRhiTexture::Flag{});
90 tex->create();
91 auto sampler = rhi.newSampler(
92 QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
93 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
94 sampler->create();
95 samplers.push_back({sampler, tex});
96
98 r.state, vertexShader(),
99 QString(frag)
100 .arg("")
101 .arg(colorMatrix(decoder))
102 .arg(field(layout.y))
103 .arg(field(layout.u))
104 .arg(field(layout.v))
105 .arg(field(layout.a)));
106 }
107
108 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
109 {
110 setPixels(res, frame);
111 }
112
113 void setPixels(QRhiResourceUpdateBatch& res, AVFrame& frame) const noexcept
114 {
115 setYPixels(res, samplers[0].texture, frame.data[0], frame.linesize[0]);
116 }
117
118 void setYPixels(
119 QRhiResourceUpdateBatch& res, QRhiTexture* tex, uint8_t* pixels,
120 int stride) const noexcept
121 {
122 const auto w = decoder.width, h = decoder.height;
123 QRhiTextureUploadEntry entry{
124 0, 0, createTextureUpload(pixels, w, h, 2, stride)};
125 res.uploadTexture(tex, QRhiTextureUploadDescription{entry});
126 }
127};
128
129} // namespace score::gfx
Decoder for RGB packed into bit fields narrower than a byte.
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
One channel's placement inside the packed container.
Definition PackedBitfield.hpp:34
int width
Bit count; 0 means "channel absent, use 1.0".
Definition PackedBitfield.hpp:36
int offset
LSB position within the reassembled word.
Definition PackedBitfield.hpp:35
Definition PackedBitfieldYUV.hpp:34
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition PackedBitfieldYUV.hpp:84
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition PackedBitfieldYUV.hpp:108
Definition PackedBitfieldYUV.hpp:29