Loading...
Searching...
No Matches
NV24.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
22{
23 static const constexpr auto frag_prologue = R"_(#version 450
24
25)_" SCORE_GFX_VIDEO_UNIFORMS R"_(
26
27layout(binding=3) uniform sampler2D y_tex;
28layout(binding=4) uniform sampler2D uv_tex;
29
30layout(location = 0) in vec2 v_texcoord;
31layout(location = 0) out vec4 fragColor;
32
33%2
34
35vec4 processTexture(vec4 tex) {
36 vec4 processed = convert_to_rgb(tex);
37 { %1 }
38 return processed;
39}
40
41void main()
42{
43 float y = texture(y_tex, v_texcoord).r;
44 float u = texture(uv_tex, v_texcoord).r;
45 float v = texture(uv_tex, v_texcoord).g;
46)_";
47
48 static const constexpr auto frag_epilogue = R"_(
49 fragColor = processTexture(vec4(yuv, 1.));
50})_";
51
52 Video::ImageFormat& decoder;
53 bool nv42{};
54
55 NV24Decoder(Video::ImageFormat& d, bool inverted)
56 : decoder{d}
57 , nv42{inverted}
58 {
59 }
60
61 std::pair<QShader, QShader> init(RenderList& r) override
62 {
63 auto& rhi = *r.state.rhi;
64 const auto w = decoder.width, h = decoder.height;
65
66 // Y plane: R8 at full resolution
67 {
68 auto tex = rhi.newTexture(QRhiTexture::R8, {w, h}, 1, QRhiTexture::Flag{});
69 tex->create();
70
71 auto sampler = rhi.newSampler(
72 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
73 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
74 sampler->create();
75 samplers.push_back({sampler, tex});
76 }
77
78 // UV plane: RG8 at full resolution
79 {
80 auto tex = rhi.newTexture(QRhiTexture::RG8, {w, h}, 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
90 QString frag = frag_prologue;
91 if(nv42)
92 frag += " vec3 yuv = vec3(y, v, u);\n";
93 else
94 frag += " vec3 yuv = vec3(y, u, v);\n";
95 frag += frag_epilogue;
96
98 r.state, vertexShader(), QString(frag).arg("").arg(colorMatrix(decoder)));
99 }
100
101 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
102 {
103 {
104 const auto w = decoder.width, h = decoder.height;
105 QRhiTextureUploadEntry entry{
106 0, 0, createTextureUpload(frame.data[0], w, h, 1, frame.linesize[0])};
107 res.uploadTexture(samplers[0].texture, {entry});
108 }
109 {
110 const auto w = decoder.width, h = decoder.height;
111 QRhiTextureUploadEntry entry{
112 0, 0, createTextureUpload(frame.data[1], w, h, 2, frame.linesize[1])};
113 res.uploadTexture(samplers[1].texture, {entry});
114 }
115 }
116};
117
118}
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 NV24 / NV42 semi-planar 4:4:4 8-bit videos.
Definition NV24.hpp:22
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition NV24.hpp:101
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition NV24.hpp:61