1 /*
2  * GStreamer
3  * Copyright (C) 2009 Julien Isorce <julien.isorce@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 /**
22  * SECTION:element-glmosaic
23  * @title: glmosaic
24  *
25  * glmixer sub element. N gl sink pads to 1 source pad.
26  * N + 1 OpenGL contexts shared together.
27  * N <= 6 because the rendering is more a like a cube than a mosaic
28  * Each opengl input stream is rendered on a cube face
29  *
30  * ## Examples
31  * |[
32  * gst-launch-1.0 videotestsrc ! video/x-raw, format=YUY2 ! queue ! glmosaic name=m ! glimagesink \
33  *     videotestsrc pattern=12 ! video/x-raw, format=I420, framerate=5/1, width=100, height=200 ! queue ! m. \
34  *     videotestsrc ! video/x-raw, framerate=15/1, width=1500, height=1500 ! gleffects effect=3 ! queue ! m. \
35  *     videotestsrc ! gleffects effect=2 ! queue ! m.  \
36  *     videotestsrc ! glfiltercube ! queue ! m. \
37  *     videotestsrc ! gleffects effect=6 ! queue ! m.
38  * ]|
39  * FBO (Frame Buffer Object) is required.
40  *
41  */
42 
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46 
47 #include "gstglmosaic.h"
48 #include "gstglutils.h"
49 
50 #define GST_CAT_DEFAULT gst_gl_mosaic_debug
51 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
52 
53 enum
54 {
55   PROP_0,
56 };
57 
58 static void gst_gl_mosaic_child_proxy_init (gpointer g_iface,
59     gpointer iface_data);
60 
61 #define DEBUG_INIT \
62     GST_DEBUG_CATEGORY_INIT (gst_gl_mosaic_debug, "glmosaic", 0, "glmosaic element");
63 
64 G_DEFINE_TYPE_WITH_CODE (GstGLMosaic, gst_gl_mosaic, GST_TYPE_GL_MIXER,
65     G_IMPLEMENT_INTERFACE (GST_TYPE_CHILD_PROXY,
66         gst_gl_mosaic_child_proxy_init);
67     DEBUG_INIT);
68 
69 static void gst_gl_mosaic_set_property (GObject * object, guint prop_id,
70     const GValue * value, GParamSpec * pspec);
71 static void gst_gl_mosaic_get_property (GObject * object, guint prop_id,
72     GValue * value, GParamSpec * pspec);
73 
74 static GstPad *gst_gl_mosaic_request_new_pad (GstElement * element,
75     GstPadTemplate * temp, const gchar * req_name, const GstCaps * caps);
76 static void gst_gl_mosaic_release_pad (GstElement * element, GstPad * pad);
77 
78 static void gst_gl_mosaic_reset (GstGLMixer * mixer);
79 static gboolean gst_gl_mosaic_init_shader (GstGLMixer * mixer,
80     GstCaps * outcaps);
81 
82 static gboolean gst_gl_mosaic_process_textures (GstGLMixer * mixer,
83     GstGLMemory * out_tex);
84 static gboolean gst_gl_mosaic_callback (gpointer stuff);
85 
86 //vertex source
87 static const gchar *mosaic_v_src =
88     "uniform mat4 u_matrix;                                       \n"
89     "uniform float xrot_degree, yrot_degree, zrot_degree;         \n"
90     "attribute vec4 a_position;                                   \n"
91     "attribute vec2 a_texCoord;                                   \n"
92     "varying vec2 v_texCoord;                                     \n"
93     "void main()                                                  \n"
94     "{                                                            \n"
95     "   float PI = 3.14159265;                                    \n"
96     "   float xrot = xrot_degree*2.0*PI/360.0;                    \n"
97     "   float yrot = yrot_degree*2.0*PI/360.0;                    \n"
98     "   float zrot = zrot_degree*2.0*PI/360.0;                    \n"
99     "   mat4 matX = mat4 (                                        \n"
100     "            1.0,        0.0,        0.0, 0.0,                \n"
101     "            0.0,  cos(xrot),  sin(xrot), 0.0,                \n"
102     "            0.0, -sin(xrot),  cos(xrot), 0.0,                \n"
103     "            0.0,        0.0,        0.0, 1.0 );              \n"
104     "   mat4 matY = mat4 (                                        \n"
105     "      cos(yrot),        0.0, -sin(yrot), 0.0,                \n"
106     "            0.0,        1.0,        0.0, 0.0,                \n"
107     "      sin(yrot),        0.0,  cos(yrot), 0.0,                \n"
108     "            0.0,        0.0,       0.0,  1.0 );              \n"
109     "   mat4 matZ = mat4 (                                        \n"
110     "      cos(zrot),  sin(zrot),        0.0, 0.0,                \n"
111     "     -sin(zrot),  cos(zrot),        0.0, 0.0,                \n"
112     "            0.0,        0.0,        1.0, 0.0,                \n"
113     "            0.0,        0.0,        0.0, 1.0 );              \n"
114     "   gl_Position = u_matrix * matZ * matY * matX * a_position; \n"
115     "   v_texCoord = a_texCoord;                                  \n"
116     "}                                                            \n";
117 
118 //fragment source
119 static const gchar *mosaic_f_src =
120     "uniform sampler2D s_texture;                    \n"
121     "varying vec2 v_texCoord;                            \n"
122     "void main()                                         \n"
123     "{                                                   \n"
124     //"  gl_FragColor = vec4( 1.0, 0.5, 1.0, 1.0 );\n"
125     "  gl_FragColor = texture2D( s_texture, v_texCoord );\n"
126     "}                                                   \n";
127 
128 static void
gst_gl_mosaic_class_init(GstGLMosaicClass * klass)129 gst_gl_mosaic_class_init (GstGLMosaicClass * klass)
130 {
131   GObjectClass *gobject_class;
132   GstElementClass *element_class;
133 
134   gobject_class = (GObjectClass *) klass;
135   element_class = GST_ELEMENT_CLASS (klass);
136 
137   gobject_class->set_property = gst_gl_mosaic_set_property;
138   gobject_class->get_property = gst_gl_mosaic_get_property;
139 
140   element_class->request_new_pad =
141       GST_DEBUG_FUNCPTR (gst_gl_mosaic_request_new_pad);
142   element_class->release_pad = GST_DEBUG_FUNCPTR (gst_gl_mosaic_release_pad);
143 
144   gst_element_class_set_metadata (element_class, "OpenGL mosaic",
145       "Filter/Effect/Video", "OpenGL mosaic",
146       "Julien Isorce <julien.isorce@gmail.com>");
147 
148   GST_GL_MIXER_CLASS (klass)->set_caps = gst_gl_mosaic_init_shader;
149   GST_GL_MIXER_CLASS (klass)->reset = gst_gl_mosaic_reset;
150   GST_GL_MIXER_CLASS (klass)->process_textures = gst_gl_mosaic_process_textures;
151 
152   GST_GL_BASE_MIXER_CLASS (klass)->supported_gl_api = GST_GL_API_OPENGL;
153 }
154 
155 static void
gst_gl_mosaic_init(GstGLMosaic * mosaic)156 gst_gl_mosaic_init (GstGLMosaic * mosaic)
157 {
158   mosaic->shader = NULL;
159 }
160 
161 static void
gst_gl_mosaic_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)162 gst_gl_mosaic_set_property (GObject * object, guint prop_id,
163     const GValue * value, GParamSpec * pspec)
164 {
165   //GstGLMosaic *mixer = GST_GL_MOSAIC (object);
166 
167   switch (prop_id) {
168     default:
169       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
170       break;
171   }
172 }
173 
174 static void
gst_gl_mosaic_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)175 gst_gl_mosaic_get_property (GObject * object, guint prop_id,
176     GValue * value, GParamSpec * pspec)
177 {
178   //GstGLMosaic* mixer = GST_GL_MOSAIC (object);
179 
180   switch (prop_id) {
181     default:
182       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183       break;
184   }
185 }
186 
187 static GstPad *
gst_gl_mosaic_request_new_pad(GstElement * element,GstPadTemplate * templ,const gchar * req_name,const GstCaps * caps)188 gst_gl_mosaic_request_new_pad (GstElement * element, GstPadTemplate * templ,
189     const gchar * req_name, const GstCaps * caps)
190 {
191   GstPad *newpad;
192 
193   newpad = (GstPad *)
194       GST_ELEMENT_CLASS (gst_gl_mosaic_parent_class)->request_new_pad (element,
195       templ, req_name, caps);
196 
197   if (newpad == NULL)
198     goto could_not_create;
199 
200   gst_child_proxy_child_added (GST_CHILD_PROXY (element), G_OBJECT (newpad),
201       GST_OBJECT_NAME (newpad));
202 
203   return newpad;
204 
205 could_not_create:
206   {
207     GST_DEBUG_OBJECT (element, "could not create/add pad");
208     return NULL;
209   }
210 }
211 
212 static void
gst_gl_mosaic_release_pad(GstElement * element,GstPad * pad)213 gst_gl_mosaic_release_pad (GstElement * element, GstPad * pad)
214 {
215   GstGLMosaic *gl_mosaic = GST_GL_MOSAIC (element);
216 
217   GST_DEBUG_OBJECT (gl_mosaic, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
218 
219   gst_child_proxy_child_removed (GST_CHILD_PROXY (gl_mosaic), G_OBJECT (pad),
220       GST_OBJECT_NAME (pad));
221 
222   GST_ELEMENT_CLASS (gst_gl_mosaic_parent_class)->release_pad (element, pad);
223 }
224 
225 static void
gst_gl_mosaic_reset(GstGLMixer * mixer)226 gst_gl_mosaic_reset (GstGLMixer * mixer)
227 {
228   GstGLMosaic *mosaic = GST_GL_MOSAIC (mixer);
229 
230   //blocking call, wait the opengl thread has destroyed the shader
231   if (mosaic->shader)
232     gst_object_unref (mosaic->shader);
233   mosaic->shader = NULL;
234 
235   mosaic->xrot = 0.0;
236   mosaic->yrot = 0.0;
237   mosaic->zrot = 0.0;
238 }
239 
240 static gboolean
gst_gl_mosaic_init_shader(GstGLMixer * mixer,GstCaps * outcaps)241 gst_gl_mosaic_init_shader (GstGLMixer * mixer, GstCaps * outcaps)
242 {
243   GstGLMosaic *mosaic = GST_GL_MOSAIC (mixer);
244 
245   g_clear_object (&mosaic->shader);
246   //blocking call, wait the opengl thread has compiled the shader
247   return gst_gl_context_gen_shader (GST_GL_BASE_MIXER (mixer)->context,
248       mosaic_v_src, mosaic_f_src, &mosaic->shader);
249 }
250 
251 static void
_mosaic_render(GstGLContext * context,GstGLMosaic * mosaic)252 _mosaic_render (GstGLContext * context, GstGLMosaic * mosaic)
253 {
254   GstGLMixer *mixer = GST_GL_MIXER (mosaic);
255 
256   gst_gl_framebuffer_draw_to_texture (mixer->fbo, mosaic->out_tex,
257       gst_gl_mosaic_callback, mosaic);
258 }
259 
260 static gboolean
gst_gl_mosaic_process_textures(GstGLMixer * mix,GstGLMemory * out_tex)261 gst_gl_mosaic_process_textures (GstGLMixer * mix, GstGLMemory * out_tex)
262 {
263   GstGLMosaic *mosaic = GST_GL_MOSAIC (mix);
264   GstGLContext *context = GST_GL_BASE_MIXER (mix)->context;
265 
266   mosaic->out_tex = out_tex;
267 
268   gst_gl_context_thread_add (context, (GstGLContextThreadFunc) _mosaic_render,
269       mosaic);
270 
271   return TRUE;
272 }
273 
274 /* opengl scene, params: input texture (not the output mixer->texture) */
275 static gboolean
gst_gl_mosaic_callback(gpointer stuff)276 gst_gl_mosaic_callback (gpointer stuff)
277 {
278   GstGLMosaic *mosaic = GST_GL_MOSAIC (stuff);
279   GstGLMixer *mixer = GST_GL_MIXER (mosaic);
280   GstGLFuncs *gl = GST_GL_BASE_MIXER (mixer)->context->gl_vtable;
281   GList *walk;
282 
283   GLint attr_position_loc = 0;
284   GLint attr_texture_loc = 0;
285 
286   const GLfloat matrix[] = {
287     0.5f, 0.0f, 0.0f, 0.0f,
288     0.0f, 0.5f, 0.0f, 0.0f,
289     0.0f, 0.0f, 0.5f, 0.0f,
290     0.0f, 0.0f, 0.0f, 1.0f
291   };
292   const GLushort indices[] = {
293     0, 1, 2,
294     0, 2, 3
295   };
296 
297   guint count = 0;
298 
299   gst_gl_context_clear_shader (GST_GL_BASE_MIXER (mixer)->context);
300   gl->BindTexture (GL_TEXTURE_2D, 0);
301 
302   gl->Enable (GL_DEPTH_TEST);
303 
304   gl->ClearColor (0.0, 0.0, 0.0, 0.0);
305   gl->Clear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
306 
307   gst_gl_shader_use (mosaic->shader);
308 
309   attr_position_loc =
310       gst_gl_shader_get_attribute_location (mosaic->shader, "a_position");
311   attr_texture_loc =
312       gst_gl_shader_get_attribute_location (mosaic->shader, "a_texCoord");
313 
314   gst_gl_shader_set_uniform_1i (mosaic->shader, "s_texture", 0);
315   gst_gl_shader_set_uniform_1f (mosaic->shader, "xrot_degree", mosaic->xrot);
316   gst_gl_shader_set_uniform_1f (mosaic->shader, "yrot_degree", mosaic->yrot);
317   gst_gl_shader_set_uniform_1f (mosaic->shader, "zrot_degree", mosaic->zrot);
318   gst_gl_shader_set_uniform_matrix_4fv (mosaic->shader, "u_matrix", 1,
319       GL_FALSE, matrix);
320 
321   GST_OBJECT_LOCK (mosaic);
322   walk = GST_ELEMENT (mosaic)->sinkpads;
323   while (walk) {
324     GstGLMixerPad *pad = walk->data;
325     /* *INDENT-OFF* */
326     gfloat v_vertices[] = {
327       /* front face */
328        1.0f, 1.0f,-1.0f, 1.0f, 0.0f,
329        1.0f,-1.0f,-1.0f, 1.0f, 1.0f,
330       -1.0f,-1.0f,-1.0f, 0.0f, 1.0f,
331       -1.0f, 1.0f,-1.0f, 0.0f, 0.0f,
332       /* right face */
333        1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
334        1.0f,-1.0f, 1.0f, 0.0f, 0.0f,
335        1.0f,-1.0f,-1.0f, 0.0f, 1.0f,
336        1.0f, 1.0f,-1.0f, 1.0f, 1.0f,
337       /* left face */
338       -1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
339       -1.0f, 1.0f,-1.0f, 1.0f, 1.0f,
340       -1.0f,-1.0f,-1.0f, 0.0f, 1.0f,
341       -1.0f,-1.0f, 1.0f, 0.0f, 0.0f,
342       /* top face */
343        1.0f,-1.0f, 1.0f, 1.0f, 0.0f,
344       -1.0f,-1.0f, 1.0f, 0.0f, 0.0f,
345       -1.0f,-1.0f,-1.0f, 0.0f, 1.0f,
346        1.0f,-1.0f,-1.0f, 1.0f, 1.0f,
347       /* bottom face */
348        1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
349        1.0f, 1.0f,-1.0f, 1.0f, 1.0f,
350       -1.0f, 1.0f,-1.0f, 0.0f, 1.0f,
351       -1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
352       /* back face */
353        1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
354       -1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
355       -1.0f,-1.0f, 1.0f, 0.0f, 1.0f,
356        1.0f,-1.0f, 1.0f, 1.0f, 1.0f
357     };
358     /* *INDENT-ON* */
359     guint in_tex;
360     guint width, height;
361 
362     in_tex = pad->current_texture;
363     width = GST_VIDEO_INFO_WIDTH (&GST_VIDEO_AGGREGATOR_PAD (pad)->info);
364     height = GST_VIDEO_INFO_HEIGHT (&GST_VIDEO_AGGREGATOR_PAD (pad)->info);
365 
366     if (!in_tex || width <= 0 || height <= 0) {
367       GST_DEBUG ("skipping texture:%u pad:%p width:%u height %u",
368           in_tex, pad, width, height);
369       count++;
370       walk = g_list_next (walk);
371       continue;
372     }
373 
374     GST_TRACE ("processing texture:%u dimensions:%ux%u", in_tex, width, height);
375 
376     gl->VertexAttribPointer (attr_position_loc, 3, GL_FLOAT,
377         GL_FALSE, 5 * sizeof (GLfloat), &v_vertices[5 * 4 * count]);
378 
379     gl->VertexAttribPointer (attr_texture_loc, 2, GL_FLOAT,
380         GL_FALSE, 5 * sizeof (GLfloat), &v_vertices[5 * 4 * count + 3]);
381 
382     gl->EnableVertexAttribArray (attr_position_loc);
383     gl->EnableVertexAttribArray (attr_texture_loc);
384 
385     gl->ActiveTexture (GL_TEXTURE0);
386     gl->BindTexture (GL_TEXTURE_2D, in_tex);
387     gst_gl_shader_set_uniform_1i (mosaic->shader, "s_texture", 0);
388     gst_gl_shader_set_uniform_1f (mosaic->shader, "xrot_degree", mosaic->xrot);
389     gst_gl_shader_set_uniform_1f (mosaic->shader, "yrot_degree", mosaic->yrot);
390     gst_gl_shader_set_uniform_1f (mosaic->shader, "zrot_degree", mosaic->zrot);
391     gst_gl_shader_set_uniform_matrix_4fv (mosaic->shader, "u_matrix", 1,
392         GL_FALSE, matrix);
393 
394     gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
395 
396     ++count;
397 
398     walk = g_list_next (walk);
399   }
400   GST_OBJECT_UNLOCK (mosaic);
401 
402   gl->DisableVertexAttribArray (attr_position_loc);
403   gl->DisableVertexAttribArray (attr_texture_loc);
404 
405   gl->BindTexture (GL_TEXTURE_2D, 0);
406 
407   gl->Disable (GL_DEPTH_TEST);
408 
409   gst_gl_context_clear_shader (GST_GL_BASE_MIXER (mixer)->context);
410 
411   mosaic->xrot += 0.6f;
412   mosaic->yrot += 0.4f;
413   mosaic->zrot += 0.8f;
414 
415   return TRUE;
416 }
417 
418 /* GstChildProxy implementation */
419 static GObject *
gst_gl_mosaic_child_proxy_get_child_by_index(GstChildProxy * child_proxy,guint index)420 gst_gl_mosaic_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
421     guint index)
422 {
423   GstGLMosaic *gl_mosaic = GST_GL_MOSAIC (child_proxy);
424   GObject *obj = NULL;
425 
426   GST_OBJECT_LOCK (gl_mosaic);
427   obj = g_list_nth_data (GST_ELEMENT_CAST (gl_mosaic)->sinkpads, index);
428   if (obj)
429     gst_object_ref (obj);
430   GST_OBJECT_UNLOCK (gl_mosaic);
431 
432   return obj;
433 }
434 
435 static guint
gst_gl_mosaic_child_proxy_get_children_count(GstChildProxy * child_proxy)436 gst_gl_mosaic_child_proxy_get_children_count (GstChildProxy * child_proxy)
437 {
438   guint count = 0;
439   GstGLMosaic *gl_mosaic = GST_GL_MOSAIC (child_proxy);
440 
441   GST_OBJECT_LOCK (gl_mosaic);
442   count = GST_ELEMENT_CAST (gl_mosaic)->numsinkpads;
443   GST_OBJECT_UNLOCK (gl_mosaic);
444   GST_INFO_OBJECT (gl_mosaic, "Children Count: %d", count);
445 
446   return count;
447 }
448 
449 static void
gst_gl_mosaic_child_proxy_init(gpointer g_iface,gpointer iface_data)450 gst_gl_mosaic_child_proxy_init (gpointer g_iface, gpointer iface_data)
451 {
452   GstChildProxyInterface *iface = g_iface;
453 
454   iface->get_child_by_index = gst_gl_mosaic_child_proxy_get_child_by_index;
455   iface->get_children_count = gst_gl_mosaic_child_proxy_get_children_count;
456 }
457