2#include <Gfx/Graph/decoders/ColorSpace.hpp>
3#include <Gfx/Graph/decoders/GPUVideoDecoder.hpp>
5#include <libavformat/avformat.h>
38 static const constexpr auto frag = R
"_(#version 450
40)_" SCORE_GFX_VIDEO_UNIFORMS R"_(
42layout(binding=3) uniform sampler2D u_tex;
44layout(location = 0) in vec2 v_texcoord;
45layout(location = 0) out vec4 fragColor;
49vec4 processTexture(vec4 tex) {
50 vec4 processed = convert_to_rgb(tex);
55uint readWord(int wx, int y) {
56 // Sample the RGBA8 texel that holds one v210 ULWord, repack the four
57 // bytes into a little-endian uint32 we can bit-extract from.
58 vec4 t = texelFetch(u_tex, ivec2(wx, y), 0);
59 uint b0 = uint(t.r * 255.0 + 0.5);
60 uint b1 = uint(t.g * 255.0 + 0.5);
61 uint b2 = uint(t.b * 255.0 + 0.5);
62 uint b3 = uint(t.a * 255.0 + 0.5);
63 return b0 | (b1 << 8u) | (b2 << 16u) | (b3 << 24u);
67 // Output coordinates are in source-pixel space. mat.texSz is the
68 // logical (full-width) frame size that downstream nodes see.
69 int outX = int(floor(v_texcoord.x * mat.texSz.x));
70 int outY = int(floor(v_texcoord.y * mat.texSz.y));
72 int groupIdx = outX / 6;
73 int pixInGroup = outX - groupIdx * 6;
74 int wordBaseX = groupIdx * 4; // 4 ULWords per 6-pixel group
76 uint y10 = 0u, cb10 = 0u, cr10 = 0u;
78 if (pixInGroup == 0) {
79 uint w0 = readWord(wordBaseX + 0, outY);
81 y10 = (w0 >> 10u) & 0x3FFu;
82 cr10 = (w0 >> 20u) & 0x3FFu;
83 } else if (pixInGroup == 1) {
84 uint w0 = readWord(wordBaseX + 0, outY);
85 uint w1 = readWord(wordBaseX + 1, outY);
86 cb10 = w0 & 0x3FFu; // shared with pixel 0
88 cr10 = (w0 >> 20u) & 0x3FFu; // shared with pixel 0
89 } else if (pixInGroup == 2) {
90 uint w1 = readWord(wordBaseX + 1, outY);
91 uint w2 = readWord(wordBaseX + 2, outY);
92 cb10 = (w1 >> 10u) & 0x3FFu;
93 y10 = (w1 >> 20u) & 0x3FFu;
95 } else if (pixInGroup == 3) {
96 uint w1 = readWord(wordBaseX + 1, outY);
97 uint w2 = readWord(wordBaseX + 2, outY);
98 cb10 = (w1 >> 10u) & 0x3FFu; // shared with pixel 2
99 y10 = (w2 >> 10u) & 0x3FFu;
100 cr10 = w2 & 0x3FFu; // shared with pixel 2
101 } else if (pixInGroup == 4) {
102 uint w2 = readWord(wordBaseX + 2, outY);
103 uint w3 = readWord(wordBaseX + 3, outY);
104 cb10 = (w2 >> 20u) & 0x3FFu;
106 cr10 = (w3 >> 10u) & 0x3FFu;
108 uint w2 = readWord(wordBaseX + 2, outY);
109 uint w3 = readWord(wordBaseX + 3, outY);
110 cb10 = (w2 >> 20u) & 0x3FFu; // shared with pixel 4
111 y10 = (w3 >> 20u) & 0x3FFu;
112 cr10 = (w3 >> 10u) & 0x3FFu; // shared with pixel 4
115 // 10-bit limited-range YCbCr (Y in [64,940], Cb/Cr in [64,960]) divided
116 // by 1023 yields the same [0,1]-normalized signal the 8-bit limited
117 // range matrices in ColorSpace.hpp expect, modulo a tiny scale factor
118 // that's well below visible quantization. The matrix handles the
119 // [16/255, 235/255] -> [0, 1] expansion internally.
120 vec4 yuv = vec4(float(y10), float(cb10), float(cr10), 1023.0) / 1023.0;
121 fragColor = processTexture(yuv);
134 auto& rhi = *r.
state.rhi;
135 const auto w = decoder.width, h = decoder.height;
138 const int texW = (w / 6) * 4;
141 = rhi.newTexture(QRhiTexture::RGBA8, {texW, h}, 1, QRhiTexture::Flag{});
147 auto sampler = rhi.newSampler(
148 QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
149 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
152 samplers.push_back({sampler, tex});
156 r.
state, vertexShader(), QString(frag).arg(
"").arg(colorMatrix(decoder)));
165 const int texW = (decoder.width / 6) * 4;
166 auto y_tex = samplers[0].texture;
167 QRhiTextureUploadEntry entry{
170 frame.data[0], texW, decoder.height, 4, frame.linesize[0])};
171 QRhiTextureUploadDescription desc{entry};
172 res.uploadTexture(y_tex, desc);
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:19
RenderState & state
RenderState corresponding to this RenderList.
Definition RenderList.hpp:102
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:647
Decodes 10-bit 4:2:2 YCbCr packed in v210 (SMPTE ST 2110-20 / SDI native).
Definition decoders/V210.hpp:36
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition decoders/V210.hpp:159
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition decoders/V210.hpp:132