HAP.hpp
1 #pragma once
2 #include <Gfx/Graph/decoders/GPUVideoDecoder.hpp>
3 
4 #include <hap/source/hap.h>
5 
6 #include <snappy.h>
7 
8 extern "C" {
9 #include <libavformat/avformat.h>
10 }
11 
12 namespace score::gfx
13 {
18 {
19  struct HAPSection
20  {
21  static HAPSection read(const uint8_t* bytes);
22 
23  uint32_t type{};
24  uint32_t size{};
25  const uint8_t* data{};
26  };
27 
28  void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override;
29  void setPixels_noEncoding(
30  QRhiResourceUpdateBatch& res, const uint8_t* data_start, std::size_t size);
31  void setPixels_snappy(
32  QRhiResourceUpdateBatch& res, const uint8_t* data_start, std::size_t size);
33 
34  static constexpr int buffer_size = 1024 * 1024 * 16;
35  std::unique_ptr<char[]> m_buffer = std::make_unique<char[]>(1024 * 1024 * 16);
36 };
37 
42 {
43  static inline const QString fragment = QStringLiteral(R"_(#version 450
44 
45 )_" SCORE_GFX_VIDEO_UNIFORMS R"_(
46 
47 layout(binding=3) uniform sampler2D y_tex;
48 
49 layout(location = 0) in vec2 v_texcoord;
50 layout(location = 0) out vec4 fragColor;
51 
52 vec4 processYCoCg(vec4 CoCgSY) {
53  const vec4 offsets = vec4(-0.50196078431373, -0.50196078431373, 0.0, 0.0);
54  CoCgSY += offsets;
55  float scale = ( CoCgSY.z * ( 255.0 / 8.0 ) ) + 1.0;
56 
57  float Co = CoCgSY.x / scale;
58  float Cg = CoCgSY.y / scale;
59  float Y = CoCgSY.w;
60 
61  vec4 rgba = vec4(Y + Co - Cg, Y + Cg, Y - Co - Cg, 1.0);
62  return rgba;
63 }
64 
65 vec4 processTexture(vec4 tex) {
66  vec4 processed = tex;
67  { %1 }
68  return processed;
69 }
70 
71 void main ()
72 {
73  fragColor = processTexture(texture(y_tex, v_texcoord));
74 })_");
75 
76  static inline const QString ycocg_filter
77  = QStringLiteral("processed = processYCoCg(processed);\n");
78 
79  HAPDefaultDecoder(QRhiTexture::Format fmt, Video::ImageFormat& d, QString f = "");
80  QRhiTexture::Format format;
81  Video::ImageFormat& decoder;
82  QString filter;
83 
84  std::pair<QShader, QShader> init(RenderList& r) override;
85 };
86 
91 {
92  static inline const QString fragment = QStringLiteral(R"_(#version 450
93 
94 )_" SCORE_GFX_VIDEO_UNIFORMS R"_(
95 
96 layout(binding=3) uniform sampler2D y_tex;
97 layout(binding=4) uniform sampler2D alpha_tex;
98 
99 layout(location = 0) in vec2 v_texcoord;
100 layout(location = 0) out vec4 fragColor;
101 
102 vec4 processYCoCg(vec4 CoCgSY, vec4 alpha) {
103  const vec4 offsets = vec4(-0.50196078431373, -0.50196078431373, 0.0, 0.0);
104  CoCgSY += offsets;
105  float scale = ( CoCgSY.z * ( 255.0 / 8.0 ) ) + 1.0;
106 
107  float Co = CoCgSY.x / scale;
108  float Cg = CoCgSY.y / scale;
109  float Y = CoCgSY.w;
110 
111  vec4 rgba = vec4(Y + Co - Cg, Y + Cg, Y - Co - Cg, alpha.r);
112  return rgba;
113 }
114 
115 vec4 processTexture(vec4 tex) {
116  vec4 processed = tex;
117  { %1 }
118  return processed;
119 }
120 
121 void main ()
122 {
123  vec4 ycocg = texture(y_tex, v_texcoord);
124  vec4 alpha = texture(alpha_tex, v_texcoord);
125  fragColor = processTexture(processYCoCg(ycocg, alpha));
126 })_");
127 
128  HAPMDecoder(Video::ImageFormat& d, QString f = "");
129  Video::ImageFormat& decoder;
130  QString filter;
131  std::pair<QShader, QShader> init(RenderList& r) override;
132 
133  void exec(RenderList&, QRhiResourceUpdateBatch& res, AVFrame& frame) override;
134 
135  static void setPixels(
136  QRhiResourceUpdateBatch& res, QRhiTexture* tex, const uint8_t* ycocg_start,
137  std::size_t ycocg_size);
138 
139  std::unique_ptr<char[]> m_alphaBuffer = std::make_unique<char[]>(1024 * 1024 * 16);
140 };
141 
142 }
Processes and renders a video frame on the GPU.
Definition: GPUVideoDecoder.hpp:43
List of nodes to be rendered to an output.
Definition: RenderList.hpp:19
Graphics rendering pipeline for ossia score.
Definition: PreviewWidget.hpp:12
Definition: VideoInterface.hpp:16
Base class for HAP ((c) Vidvox) decoding.
Definition: HAP.hpp:18
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition: HAP.cpp:32
Decodes HAP basic format.
Definition: HAP.hpp:42
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition: HAP.cpp:110
Decodes HAP-M (HAP + alpha channel)
Definition: HAP.hpp:91
std::pair< QShader, QShader > init(RenderList &r) override
Initialize a GPUVideoDecoder.
Definition: HAP.cpp:138
void exec(RenderList &, QRhiResourceUpdateBatch &res, AVFrame &frame) override
Decode and upload a video frame to the GPU.
Definition: HAP.cpp:172