1 /* GStreamer
2 *
3 * Copyright (C) 2014 Matthew Waters <ystreet00@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <gst/gl/gstglutils.c>
26 #undef GST_CAT_DEFAULT
27 #include <gst/check/gstcheck.h>
28
29 #define VEC4_FORMAT "10.4f %10.4f %10.4f %10.4f"
30 #define VEC4_ARGS(v) (v)[0], (v)[1], (v)[2], (v)[3]
31 #define EPSILON 0.0001f
32 #define FEQ(a,b) (fabs(a-b) < EPSILON)
33
34 static void
debug_matrix(const float * m)35 debug_matrix (const float *m)
36 {
37 int i;
38 for (i = 0; i < 4; i++) {
39 GST_DEBUG ("%" VEC4_FORMAT, VEC4_ARGS (&m[i * 4]));
40 }
41 }
42
GST_START_TEST(test_matrix_multiply)43 GST_START_TEST (test_matrix_multiply)
44 {
45 /* A * B == C */
46 const float A[] = {
47 1., 1., 2., 5.,
48 0., 3., 0., 1.,
49 2., 0., 3., 1.,
50 3., 2., 1., 0.,
51 };
52
53 const float B[] = {
54 3., 1., 0., 2.,
55 1., 0., 3., 2.,
56 0., 1., 2., 3.,
57 3., 2., 1., 0.,
58 };
59
60 const float C[] = {
61 19., 13., 12., 10.,
62 6., 2., 10., 6.,
63 9., 7., 7., 13.,
64 11., 4., 8., 13.,
65 };
66
67 float res[16];
68 int i;
69
70 gst_gl_multiply_matrix4 (A, B, res);
71 GST_DEBUG ("Matrix A:");
72 debug_matrix (A);
73 GST_DEBUG ("Matrix B:");
74 debug_matrix (B);
75 GST_DEBUG ("Matrix C:");
76 debug_matrix (C);
77 GST_DEBUG ("Multiplication Result == C == A * B:");
78 debug_matrix (res);
79
80 for (i = 0; i < G_N_ELEMENTS (res); i++) {
81 fail_unless (FEQ (res[i], C[i]), "value %f at index %u does not match "
82 "expected value %f", res[i], i, C[i]);
83 }
84 }
85
86 GST_END_TEST;
87
GST_START_TEST(test_matrix_ndc)88 GST_START_TEST (test_matrix_ndc)
89 {
90 GstBuffer *buffer = gst_buffer_new ();
91 GstVideoAffineTransformationMeta *aff_meta;
92 float res[16];
93 int i;
94
95 const float m[] = {
96 1., 0., 0., 0.,
97 0., 1., 0., 0.,
98 0., 0., 1., 0.,
99 0., 0., 0., 1.,
100 };
101
102 const float n[] = {
103 4., 6., 4., 9.,
104 1., 5., 8., 2.,
105 9., 3., 5., 8.,
106 3., 7., 9., 1.,
107 };
108
109 aff_meta = gst_buffer_add_video_affine_transformation_meta (buffer);
110
111 /* test default identity matrix */
112 gst_gl_get_affine_transformation_meta_as_ndc (aff_meta, res);
113 GST_DEBUG ("Default matrix in the affine meta:");
114 debug_matrix (res);
115
116 for (i = 0; i < G_N_ELEMENTS (res); i++) {
117 fail_unless (FEQ (res[i], m[i]), "value %f at index %u does not match "
118 "expected value %f", res[i], i, m[i]);
119 }
120
121 /* test setting and receiving the same values */
122 GST_DEBUG ("Set matrix on the affine transformation meta:");
123 debug_matrix (n);
124
125 gst_gl_set_affine_transformation_meta_from_ndc (aff_meta, n);
126 gst_gl_get_affine_transformation_meta_as_ndc (aff_meta, res);
127
128 GST_DEBUG ("Retrieve the matrix set on the affine meta:");
129 debug_matrix (res);
130
131 for (i = 0; i < G_N_ELEMENTS (res); i++) {
132 fail_unless (FEQ (res[i], n[i]), "value %f at index %u does not match "
133 "expected value %f", res[i], i, n[i]);
134 }
135
136 gst_buffer_unref (buffer);
137 }
138
139 GST_END_TEST;
140
141 static void
transpose_matrix4(float * m,float * res)142 transpose_matrix4 (float *m, float *res)
143 {
144 int i, j;
145
146 for (i = 0; i < 4; i++) {
147 for (j = 0; j < 4; j++) {
148 int idx = i + (j * 4);
149 int swapped_idx = j + (i * 4);
150
151 GST_ERROR ("swapping %i %f into %i", idx, m[idx], swapped_idx);
152
153 if (i == j)
154 fail_unless (idx == swapped_idx);
155
156 res[swapped_idx] = m[idx];
157 }
158 }
159 }
160
161 static float
dot4(float * v1,float * v2)162 dot4 (float *v1, float *v2)
163 {
164 GST_TRACE ("%.4f * %.4f + %.4f * %.4f + %.4f * %.4f + %.4f * %.4f",
165 v1[0], v2[0], v1[1], v2[1], v1[2], v2[2], v1[3], v2[3]);
166 return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2] + v1[3] * v2[3];
167 }
168
169 /* m * v */
170 static void
_matrix_mult_vertex4(float * m,float * v,float * res)171 _matrix_mult_vertex4 (float *m, float *v, float *res)
172 {
173 res[0] = dot4 (&m[0], v);
174 res[1] = dot4 (&m[4], v);
175 res[2] = dot4 (&m[8], v);
176 res[3] = dot4 (&m[12], v);
177 }
178
179 /* v * m */
180 /* Used because the default is for OpenGL to read matrices transposed on
181 * uploading */
182 static void
_vertex_mult_matrix4(float * m,float * v,float * res)183 _vertex_mult_matrix4 (float *m, float *v, float *res)
184 {
185 float tmp[16] = { 0., };
186
187 GST_TRACE ("original matrix");
188 debug_matrix (m);
189 transpose_matrix4 (m, tmp);
190 GST_TRACE ("transposed matrix");
191 debug_matrix (tmp);
192 _matrix_mult_vertex4 (tmp, v, res);
193 }
194
GST_START_TEST(test_matrix_vertex_identity)195 GST_START_TEST (test_matrix_vertex_identity)
196 {
197 float identity[] = {
198 1., 0., 0., 0.,
199 0., 1., 0., 0.,
200 0., 0., 1., 0.,
201 0., 0., 0., 1.,
202 };
203
204 float v[] = { 1., 1., 1., 1. };
205 float res[4] = { 0., };
206 int i;
207
208 _vertex_mult_matrix4 (identity, v, res);
209 GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v));
210 GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res));
211
212 for (i = 0; i < 4; i++) {
213 fail_unless (FEQ (res[i], v[i]), "value %f at index %u does not match "
214 "expected value %f", res[i], i, v[i]);
215 }
216 }
217
218 GST_END_TEST;
219
GST_START_TEST(test_matrix_vertex_scale)220 GST_START_TEST (test_matrix_vertex_scale)
221 {
222 float scale[] = {
223 1.5, 0., 0., 0.,
224 0., 2.5, 0., 0.,
225 0., 0., 3., 0.,
226 0., 0., 0., 1.,
227 };
228
229 float v[] = { 1., 1., 1., 1. };
230 float expected[] = { 1.5, 2.5, 3., 1. };
231 float res[4] = { 0., };
232 int i;
233
234 _vertex_mult_matrix4 (scale, v, res);
235 GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v));
236 GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res));
237
238 for (i = 0; i < 4; i++) {
239 fail_unless (FEQ (res[i], expected[i]),
240 "value %f at index %u does not match " "expected value %f", res[i], i,
241 expected[i]);
242 }
243 }
244
245 GST_END_TEST;
246
GST_START_TEST(test_matrix_vertex_translate)247 GST_START_TEST (test_matrix_vertex_translate)
248 {
249 float translate_1[] = {
250 1., 0., 0., 0.,
251 0., 1., 0., 0.,
252 0., 0., 1., 0.,
253 1., 2., 3., 1.,
254 };
255
256 float v[] = { 1., 1., 1., 1. };
257 float expected[] = { 2., 3., 4., 1. };
258 float res[4] = { 0., };
259 int i;
260
261 _vertex_mult_matrix4 (translate_1, v, res);
262
263 for (i = 0; i < 4; i++) {
264 fail_unless (FEQ (res[i], expected[i]),
265 "value %f at index %u does not match " "expected value %f", res[i], i,
266 expected[i]);
267 }
268 }
269
270 GST_END_TEST;
271
GST_START_TEST(test_matrix_vertex_y_invert)272 GST_START_TEST (test_matrix_vertex_y_invert)
273 {
274 GstBuffer *buffer = gst_buffer_new ();
275 GstVideoAffineTransformationMeta *aff_meta;
276
277 float y_invert[] = {
278 1., 0., 0., 0.,
279 0., -1., 0., 0.,
280 0., 0., 1., 0.,
281 0., 0., 0., 1.,
282 };
283
284 float v[] = { 1., 1., 1., 1. };
285 float expected[] = { 1., -1., 1., 1. };
286 float res[4] = { 0., };
287 int i;
288
289 /* The y_invert matrix but with a coordinate space of [0, 1]^3 instead
290 * of [-1, 1]^3 */
291
292 aff_meta = gst_buffer_add_video_affine_transformation_meta (buffer);
293
294 GST_DEBUG ("y-invert");
295 debug_matrix (y_invert);
296
297 _vertex_mult_matrix4 (y_invert, v, res);
298 GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v));
299 GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res));
300
301 for (i = 0; i < 4; i++) {
302 fail_unless (FEQ (res[i], expected[i]),
303 "value %f at index %u does not match " "expected value %f", res[i], i,
304 expected[i]);
305 }
306
307 /* now test the [0, 1]^3 matrix and update the test values acoordingly */
308 gst_gl_set_affine_transformation_meta_from_ndc (aff_meta, y_invert);
309 expected[1] = 0.;
310
311 GST_DEBUG ("y-invert from ndc [-1,1]^3 to [0,1]^3");
312 debug_matrix (aff_meta->matrix);
313
314 _vertex_mult_matrix4 (aff_meta->matrix, v, res);
315 GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v));
316 GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res));
317
318 for (i = 0; i < 4; i++) {
319 fail_unless (FEQ (res[i], expected[i]),
320 "value %f at index %u does not match " "expected value %f", res[i], i,
321 expected[i]);
322 }
323
324 /* test vec4(1,0,1,1) -> vec4(1,1,1,1) */
325 v[1] = 0.;
326 expected[1] = 1.;
327 _vertex_mult_matrix4 (aff_meta->matrix, v, res);
328 GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v));
329 GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res));
330
331 for (i = 0; i < 4; i++) {
332 fail_unless (FEQ (res[i], expected[i]),
333 "value %f at index %u does not match " "expected value %f", res[i], i,
334 expected[i]);
335 }
336
337 gst_buffer_unref (buffer);
338 }
339
340 GST_END_TEST;
341
342 static Suite *
gst_gl_matrix_suite(void)343 gst_gl_matrix_suite (void)
344 {
345 Suite *s = suite_create ("GstGLMatrix");
346 TCase *tc_chain = tcase_create ("matrix");
347
348 suite_add_tcase (s, tc_chain);
349 tcase_add_test (tc_chain, test_matrix_multiply);
350 tcase_add_test (tc_chain, test_matrix_ndc);
351 tcase_add_test (tc_chain, test_matrix_vertex_identity);
352 tcase_add_test (tc_chain, test_matrix_vertex_scale);
353 tcase_add_test (tc_chain, test_matrix_vertex_translate);
354 tcase_add_test (tc_chain, test_matrix_vertex_y_invert);
355
356 return s;
357 }
358
359 GST_CHECK_MAIN (gst_gl_matrix);
360