Loading...
Searching...
No Matches
decoders/V210.hpp
1#pragma once
2#include <Gfx/Graph/decoders/ColorSpace.hpp>
3#include <Gfx/Graph/decoders/GPUVideoDecoder.hpp>
4extern "C" {
5#include <libavformat/avformat.h>
6}
7
8namespace score::gfx
9{
36{
37 // %1 = user filter, %2 = colorMatrix() (emits convert_to_rgb)
38 static const constexpr auto frag = R"_(#version 450
39
40)_" SCORE_GFX_VIDEO_UNIFORMS R"_(
41
42layout(binding=3) uniform sampler2D u_tex;
43
44layout(location = 0) in vec2 v_texcoord;
45layout(location = 0) out vec4 fragColor;
46
47%2
48
49vec4 processTexture(vec4 tex) {
50 vec4 processed = convert_to_rgb(tex);
51 { %1 }
52 return processed;
53}
54
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);
64}
65
66void main() {
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));
71
72 int groupIdx = outX / 6;
73 int pixInGroup = outX - groupIdx * 6;
74 int wordBaseX = groupIdx * 4; // 4 ULWords per 6-pixel group
75
76 uint y10 = 0u, cb10 = 0u, cr10 = 0u;
77
78 if (pixInGroup == 0) {
79 uint w0 = readWord(wordBaseX + 0, outY);
80 cb10 = w0 & 0x3FFu;
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
87 y10 = w1 & 0x3FFu;
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;
94 cr10 = w2 & 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;
105 y10 = w3 & 0x3FFu;
106 cr10 = (w3 >> 10u) & 0x3FFu;
107 } else { // 5
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
113 }
114
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);
122}
123)_";
124
126 : decoder{d}
127 {
128 }
129
130 Video::ImageFormat& decoder;
131
132 std::pair<QShader, QShader> init(RenderList& r) override
133 {
134 auto& rhi = *r.state.rhi;
135 const auto w = decoder.width, h = decoder.height;
136
137 // v210 input texture: 4 RGBA8 texels per 6-pixel group.
138 const int texW = (w / 6) * 4;
139 {
140 auto tex
141 = rhi.newTexture(QRhiTexture::RGBA8, {texW, h}, 1, QRhiTexture::Flag{});
142 tex->create();
143
144 // Nearest sampling: the shader uses texelFetch but the binding
145 // model still requires a sampler. Linear would smear bytes
146 // across v210 word boundaries — meaningless.
147 auto sampler = rhi.newSampler(
148 QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
149 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
150 sampler->create();
151
152 samplers.push_back({sampler, tex});
153 }
154
156 r.state, vertexShader(), QString(frag).arg("").arg(colorMatrix(decoder)));
157 }
158
159 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
160 {
161 // CPU-staging path: AVFrame.data[0] is raw v210 with row stride =
162 // frame.linesize[0] = ((width+47)/48)*128. The texture row is
163 // (width/6)*4 RGBA8 texels = width*16/6 bytes; for width % 48 == 0
164 // that exactly matches the AJA stride.
165 const int texW = (decoder.width / 6) * 4;
166 auto y_tex = samplers[0].texture;
167 QRhiTextureUploadEntry entry{
168 0, 0,
170 frame.data[0], texW, decoder.height, /*bpp=*/4, frame.linesize[0])};
171 QRhiTextureUploadDescription desc{entry};
172 res.uploadTexture(y_tex, desc);
173 }
174};
175
176} // namespace score::gfx
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
Definition VideoInterface.hpp:26
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