Loading...
Searching...
No Matches
PlanarYUV.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
17{
18 int log2ChromaW{1};
19 int log2ChromaH{1};
20 int bytesPerSample{1};
22 const char* scale{"1.0"};
23 bool alpha{false};
24};
25
36{
37 // %1 = user filter, %2 = colour matrix, %3 = sample scale,
38 // %4 = chroma sample coordinate, %5 = alpha
39 static const constexpr auto frag = R"_(#version 450
40
41)_" SCORE_GFX_VIDEO_UNIFORMS R"_(
42
43layout(binding=3) uniform sampler2D y_tex;
44layout(binding=4) uniform sampler2D u_tex;
45layout(binding=5) uniform sampler2D v_tex;
46layout(binding=6) uniform sampler2D a_tex;
47
48layout(location = 0) in vec2 v_texcoord;
49layout(location = 0) out vec4 fragColor;
50
51%2
52
53vec4 processTexture(vec4 tex) {
54 vec4 processed = convert_to_rgb(tex);
55 { %1 }
56 return processed;
57}
58
59void main ()
60{
61 const float s = %3;
62 float y = s * texture(y_tex, score_tc(v_texcoord)).r;
63 float u = s * texture(u_tex, %4).r;
64 float v = s * texture(v_tex, %4).r;
65
66 vec4 rgb = processTexture(vec4(y, u, v, 1.));
67 fragColor = vec4(rgb.rgb, %5);
68})_";
69
71 : decoder{d}
72 , layout{l}
73 , filter{std::move(f)}
74 {
75 }
76
77 Video::ImageFormat& decoder;
78 PlanarYUVLayout layout;
79 QString filter;
80
81 QSize planeSize(int plane) const noexcept
82 {
83 if(plane == 1 || plane == 2)
84 return {
85 AV_CEIL_RSHIFT(decoder.width, layout.log2ChromaW),
86 AV_CEIL_RSHIFT(decoder.height, layout.log2ChromaH)};
87 return {decoder.width, decoder.height};
88 }
89
90 std::pair<QShader, QShader> init(RenderList& r) override
91 {
92 auto& rhi = *r.state.rhi;
93 const auto fmt = layout.bytesPerSample == 2 ? QRhiTexture::R16 : QRhiTexture::R8;
94
95 // The alpha binding is declared whether or not the format has a plane
96 // for it: a 1x1 texture stands in, never sampled.
97 for(int plane = 0; plane < 4; plane++)
98 {
99 const auto sz = plane == 3 && !layout.alpha ? QSize{1, 1} : planeSize(plane);
100 auto tex = rhi.newTexture(fmt, sz, 1, QRhiTexture::Flag{});
101 tex->create();
102
103 auto sampler = rhi.newSampler(
104 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
105 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
106 sampler->create();
107 samplers.push_back({sampler, tex});
108 }
109
110 // Chroma at full vertical resolution follows the luma through
111 // deinterlacing; vertically subsampled chroma is sampled as YUV420Decoder
112 // samples it.
113 const QString chromaCoord
114 = layout.log2ChromaH == 0 ? "score_tc(v_texcoord)" : "v_texcoord";
115 // Alpha is full range at 8 and 16 bits alike: both fill their lane.
116 const QString alpha
117 = layout.alpha ? "texture(a_tex, score_tc(v_texcoord)).r" : "1.0";
118
120 r.state, vertexShader(),
121 QString(frag)
122 .arg(filter)
123 .arg(colorMatrix(decoder))
124 .arg(layout.scale)
125 .arg(chromaCoord)
126 .arg(alpha));
127 }
128
129 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
130 {
131 const int planes = layout.alpha ? 4 : 3;
132 for(int plane = 0; plane < planes; plane++)
133 {
134 const auto sz = planeSize(plane);
135 QRhiTextureUploadEntry entry{
136 0, 0,
138 frame.data[plane], sz.width(), sz.height(), layout.bytesPerSample,
139 frame.linesize[plane])};
140 QRhiTextureUploadDescription desc{entry};
141 res.uploadTexture(samplers[plane].texture, desc);
142 }
143 }
144};
145
146}
Processes and renders a video frame on the GPU.
Definition GPUVideoDecoder.hpp:161
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:36
RenderState & state
RenderState corresponding to this RenderList.
Definition RenderList.hpp:299
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:1707
Definition VideoInterface.hpp:29
Decodes planar YUV of any subsampling, at 8 or 16 bits, with or without alpha.
Definition PlanarYUV.hpp:36
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition PlanarYUV.hpp:129
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition PlanarYUV.hpp:90
The shape of a planar YUV format: chroma subsampling, sample size, and an optional alpha plane.
Definition PlanarYUV.hpp:17
const char * scale
Brings a sample to the 8-bit-referenced colour matrices.
Definition PlanarYUV.hpp:22
int log2ChromaW
1 halves the chroma planes' width, 2 quarters it.
Definition PlanarYUV.hpp:18