Loading...
Searching...
No Matches
P210.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
30{
31 static const constexpr auto frag = R"_(#version 450
32
33)_" SCORE_GFX_VIDEO_UNIFORMS R"_(
34
35layout(binding=3) uniform sampler2D y_tex;
36layout(binding=4) uniform sampler2D uv_tex;
37
38layout(location = 0) in vec2 v_texcoord;
39layout(location = 0) out vec4 fragColor;
40
41%2
42
43vec4 processTexture(vec4 tex) {
44 vec4 processed = convert_to_rgb(tex);
45 { %1 }
46 return processed;
47}
48
49void main()
50{
51 const float s = %3;
52 float y = s * texture(y_tex, score_tc(v_texcoord)).r;
53 float u = s * texture(uv_tex, score_tc(v_texcoord)).r;
54 float v = s * texture(uv_tex, score_tc(v_texcoord)).g;
55
56 fragColor = processTexture(vec4(y, u, v, 1.));
57})_";
58
59 Video::ImageFormat& decoder;
60
62 const char* scale{SCORE_GFX_MSB_ALIGNED_SCALE};
63
64 explicit P210Decoder(
65 Video::ImageFormat& d, const char* sampleScale = SCORE_GFX_MSB_ALIGNED_SCALE)
66 : decoder{d}
67 , scale{sampleScale}
68 {
69 }
70
71 std::pair<QShader, QShader> init(RenderList& r) override
72 {
73 auto& rhi = *r.state.rhi;
74 const auto w = decoder.width, h = decoder.height;
75
76 // Y plane: R16 at full resolution
77 {
78 auto tex = rhi.newTexture(QRhiTexture::R16, {w, 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
88 // UV plane: RG16 at half width, full height
89 {
90 auto tex
91 = rhi.newTexture(QRhiTexture::RG16, {w / 2, h}, 1, QRhiTexture::Flag{});
92 tex->create();
93
94 auto sampler = rhi.newSampler(
95 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
96 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
97 sampler->create();
98 samplers.push_back({sampler, tex});
99 }
100
102 r.state, vertexShader(),
103 QString(frag).arg("").arg(colorMatrix(decoder)).arg(scale));
104 }
105
106 void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override
107 {
108 // Both planes are full height in 4:2:2, so a field halves both of them.
109 {
110 const auto w = decoder.width;
111 const auto [rows, offset] = planeRows(decoder, frame, decoder.height);
112 auto sub = createTextureUpload(frame.data[0], w, rows, 2, frame.linesize[0]);
113 sub.setSourceSize({w, rows});
114 sub.setDestinationTopLeft({0, offset});
115 res.uploadTexture(samplers[0].texture, {QRhiTextureUploadEntry{0, 0, sub}});
116 }
117 {
118 const auto w = decoder.width / 2;
119 const auto [rows, offset] = planeRows(decoder, frame, decoder.height);
120 auto sub = createTextureUpload(frame.data[1], w, rows, 4, frame.linesize[1]);
121 sub.setSourceSize({w, rows});
122 sub.setDestinationTopLeft({0, offset});
123 res.uploadTexture(samplers[1].texture, {QRhiTextureUploadEntry{0, 0, sub}});
124 }
125 }
126};
127
128}
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 P210 and P216 semi-planar 4:2:2 videos.
Definition P210.hpp:30
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition P210.hpp:71
const char * scale
The MSB-aligned renormalization, which P210 and P216 share.
Definition P210.hpp:62
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition P210.hpp:106