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