Loading...
Searching...
No Matches
P010.hpp
1#pragma once
2#include <Gfx/Graph/decoders/ColorSpace.hpp>
3#include <Gfx/Graph/decoders/GPUVideoDecoder.hpp>
4
5extern "C" {
6#include <libavformat/avformat.h>
7}
8
9namespace score::gfx
10{
11
23{
24 static const constexpr auto frag = R"_(#version 450
25
26)_" SCORE_GFX_VIDEO_UNIFORMS R"_(
27
28layout(binding=3) uniform sampler2D y_tex;
29layout(binding=4) uniform sampler2D uv_tex;
30
31layout(location = 0) in vec2 v_texcoord;
32layout(location = 0) out vec4 fragColor;
33
34%2
35
36vec4 processTexture(vec4 tex) {
37 vec4 processed = convert_to_rgb(tex);
38 { %1 }
39 return processed;
40}
41
42void main()
43{
44 // P010 stores 10-bit data in the high bits of 16-bit words,
45 // so R16 unorm already returns values normalized to [0, 1].
46 float y = texture(y_tex, v_texcoord).r;
47 float u = texture(uv_tex, v_texcoord).r;
48 float v = texture(uv_tex, v_texcoord).g;
49
50 fragColor = processTexture(vec4(y, u, v, 1.));
51})_";
52
53 Video::ImageFormat& decoder;
54
56 : decoder{d}
57 {
58 }
59
60 std::pair<QShader, QShader> init(RenderList& r) override
61 {
62 auto& rhi = *r.state.rhi;
63 const auto w = decoder.width, h = decoder.height;
64
65 // Y plane: R16 at full resolution
66 {
67 auto tex = rhi.newTexture(QRhiTexture::R16, {w, h}, 1, QRhiTexture::Flag{});
68 tex->create();
69
70 auto sampler = rhi.newSampler(
71 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
72 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
73 sampler->create();
74 samplers.push_back({sampler, tex});
75 }
76
77 // UV plane: RG16 (two 16-bit channels interleaved) at half resolution
78 {
79 auto tex
80 = rhi.newTexture(QRhiTexture::RG16, {w / 2, h / 2}, 1, QRhiTexture::Flag{});
81 tex->create();
82
83 auto sampler = rhi.newSampler(
84 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
85 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
86 sampler->create();
87 samplers.push_back({sampler, tex});
88 }
89
91 r.state, vertexShader(), QString(frag).arg("").arg(colorMatrix(decoder)));
92 }
93
94 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
95 {
96 setYPixels(res, frame.data[0], frame.linesize[0]);
97 setUVPixels(res, frame.data[1], frame.linesize[1]);
98 }
99
100 void
101 setYPixels(QRhiResourceUpdateBatch& res, uint8_t* pixels, int stride) const noexcept
102 {
103 const auto w = decoder.width, h = decoder.height;
104 auto y_tex = samplers[0].texture;
105
106 QRhiTextureUploadEntry entry{0, 0, createTextureUpload(pixels, w, h, 2, stride)};
107 QRhiTextureUploadDescription desc{entry};
108
109 res.uploadTexture(y_tex, desc);
110 }
111
112 void
113 setUVPixels(QRhiResourceUpdateBatch& res, uint8_t* pixels, int stride) const noexcept
114 {
115 const auto w = decoder.width / 2, h = decoder.height / 2;
116 auto uv_tex = samplers[1].texture;
117
118 QRhiTextureUploadEntry entry{0, 0, createTextureUpload(pixels, w, h, 4, stride)};
119 QRhiTextureUploadDescription desc{entry};
120
121 res.uploadTexture(uv_tex, desc);
122 }
123};
124
125}
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:94
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:394
Definition VideoInterface.hpp:18
Decodes P010 videos.
Definition P010.hpp:23
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition P010.hpp:60
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition P010.hpp:94