/
gstglutils.c
178 lines (150 loc) · 4.46 KB
/
gstglutils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* GStreamer
* Copyright (C) 2016 Matthew Waters <matthew@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gl/gstglfuncs.h>
#include "gstglutils.h"
struct _compile_shader
{
GstGLShader **shader;
const gchar *vertex_src;
const gchar *fragment_src;
};
static void
_compile_shader (GstGLContext * context, struct _compile_shader *data)
{
GstGLShader *shader;
GstGLSLStage *vert, *frag;
GError *error = NULL;
shader = gst_gl_shader_new (context);
if (data->vertex_src) {
vert = gst_glsl_stage_new_with_string (context, GL_VERTEX_SHADER,
GST_GLSL_VERSION_NONE,
GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY, data->vertex_src);
if (!gst_glsl_stage_compile (vert, &error)) {
GST_ERROR_OBJECT (vert, "%s", error->message);
gst_object_unref (vert);
gst_object_unref (shader);
return;
}
if (!gst_gl_shader_attach (shader, vert)) {
gst_object_unref (shader);
return;
}
}
if (data->fragment_src) {
frag = gst_glsl_stage_new_with_string (context, GL_FRAGMENT_SHADER,
GST_GLSL_VERSION_NONE,
GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY,
data->fragment_src);
if (!gst_glsl_stage_compile (frag, &error)) {
GST_ERROR_OBJECT (frag, "%s", error->message);
gst_object_unref (frag);
gst_object_unref (shader);
return;
}
if (!gst_gl_shader_attach (shader, frag)) {
gst_object_unref (shader);
return;
}
}
if (!gst_gl_shader_link (shader, &error)) {
GST_ERROR_OBJECT (shader, "%s", error->message);
g_error_free (error);
error = NULL;
gst_gl_context_clear_shader (context);
gst_object_unref (shader);
return;
}
*data->shader = shader;
}
/* Called by glfilter */
gboolean
gst_gl_context_gen_shader (GstGLContext * context, const gchar * vert_src,
const gchar * frag_src, GstGLShader ** shader)
{
struct _compile_shader data;
g_return_val_if_fail (frag_src != NULL || vert_src != NULL, FALSE);
g_return_val_if_fail (shader != NULL, FALSE);
data.shader = shader;
data.vertex_src = vert_src;
data.fragment_src = frag_src;
gst_gl_context_thread_add (context, (GstGLContextThreadFunc) _compile_shader,
&data);
return *shader != NULL;
}
static const gfloat identity_matrix[] = {
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
};
static const gfloat from_ndc_matrix[] = {
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0,
};
static const gfloat to_ndc_matrix[] = {
2.0, 0.0, 0.0, 0.0,
0.0, 2.0, 0.0, 0.0,
0.0, 0.0, 2.0, 0.0,
-1.0, -1.0, -1.0, 1.0,
};
void
gst_gl_multiply_matrix4 (const gfloat * a, const gfloat * b, gfloat * result)
{
int i, j, k;
gfloat tmp[16] = { 0.0f };
if (!a || !b || !result)
return;
for (i = 0; i < 4; i++) { /* column */
for (j = 0; j < 4; j++) { /* row */
for (k = 0; k < 4; k++) {
tmp[j + (i * 4)] += a[k + (i * 4)] * b[j + (k * 4)];
}
}
}
for (i = 0; i < 16; i++)
result[i] = tmp[i];
}
void gst_gl_get_affine_transformation_meta_as_ndc_ext
(GstVideoAffineTransformationMeta * meta, gfloat * matrix)
{
if (!meta) {
int i;
for (i = 0; i < 16; i++) {
matrix[i] = identity_matrix[i];
}
} else {
float tmp[16];
gst_gl_multiply_matrix4 (from_ndc_matrix, meta->matrix, tmp);
gst_gl_multiply_matrix4 (tmp, to_ndc_matrix, matrix);
}
}
void gst_gl_set_affine_transformation_meta_from_ndc_ext
(GstVideoAffineTransformationMeta * meta, const gfloat * matrix)
{
float tmp[16];
g_return_if_fail (meta != NULL);
gst_gl_multiply_matrix4 (to_ndc_matrix, matrix, tmp);
gst_gl_multiply_matrix4 (tmp, from_ndc_matrix, meta->matrix);
}