2008-09-23 08:37:58 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
2010-04-29 06:27:29 +00:00
|
|
|
* Copyright (C) 2008-2010 Filippo Argiolas <filippo.argiolas@gmail.com>
|
2008-09-23 08:37:58 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-11-08 11:53:56 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2008-09-23 08:37:58 +00:00
|
|
|
*/
|
|
|
|
|
2009-01-15 17:39:48 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-glfiltersobel.
|
|
|
|
*
|
|
|
|
* Sobel Edge Detection.
|
|
|
|
*
|
|
|
|
* <refsect2>
|
|
|
|
* <title>Examples</title>
|
|
|
|
* |[
|
|
|
|
* gst-launch videotestsrc ! glupload ! glfiltersobel ! glimagesink
|
|
|
|
* ]|
|
|
|
|
* FBO (Frame Buffer Object) and GLSL (OpenGL Shading Language) are required.
|
|
|
|
* </refsect2>
|
|
|
|
*/
|
|
|
|
|
2008-09-23 08:37:58 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2009-07-29 23:36:30 +00:00
|
|
|
#include "gstglfiltersobel.h"
|
2013-07-11 09:03:31 +00:00
|
|
|
#include "effects/gstgleffectssources.h"
|
2008-09-23 08:37:58 +00:00
|
|
|
|
2008-10-15 13:49:12 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2009-07-29 23:36:30 +00:00
|
|
|
PROP_INVERT
|
2008-10-15 13:49:12 +00:00
|
|
|
};
|
|
|
|
|
2008-09-23 08:37:58 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_gl_filtersobel_debug
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
|
|
|
|
|
2012-05-30 03:46:21 +00:00
|
|
|
#define DEBUG_INIT \
|
2008-09-23 08:37:58 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_gl_filtersobel_debug, "glfiltersobel", 0, "glfiltersobel element");
|
|
|
|
|
2012-05-30 03:46:21 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstGLFilterSobel, gst_gl_filtersobel,
|
2009-02-11 06:39:14 +00:00
|
|
|
GST_TYPE_GL_FILTER, DEBUG_INIT);
|
2008-09-23 08:37:58 +00:00
|
|
|
|
|
|
|
static void gst_gl_filtersobel_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_gl_filtersobel_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
2009-02-11 06:39:14 +00:00
|
|
|
static void gst_gl_filter_filtersobel_reset (GstGLFilter * filter);
|
2008-09-23 08:37:58 +00:00
|
|
|
|
2011-11-24 15:02:32 +00:00
|
|
|
static gboolean gst_gl_filtersobel_init_shader (GstGLFilter * filter);
|
2012-09-20 13:03:55 +00:00
|
|
|
static gboolean gst_gl_filtersobel_filter_texture (GstGLFilter * filter,
|
|
|
|
guint in_tex, guint out_tex);
|
2010-04-27 17:38:33 +00:00
|
|
|
|
|
|
|
static void gst_gl_filtersobel_length (gint width, gint height, guint texture,
|
2010-04-27 07:08:22 +00:00
|
|
|
gpointer stuff);
|
2008-09-23 08:37:58 +00:00
|
|
|
|
|
|
|
static void
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_filtersobel_init_resources (GstGLFilter * filter)
|
2008-09-23 08:37:58 +00:00
|
|
|
{
|
|
|
|
GstGLFilterSobel *filtersobel = GST_GL_FILTERSOBEL (filter);
|
2015-03-13 10:25:35 +00:00
|
|
|
GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
|
2010-04-27 07:08:22 +00:00
|
|
|
int i;
|
|
|
|
|
2010-04-27 17:38:33 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2013-02-01 07:20:04 +00:00
|
|
|
gl->GenTextures (1, &filtersobel->midtexture[i]);
|
2013-11-15 07:28:49 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_2D, filtersobel->midtexture[i]);
|
|
|
|
gl->TexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8,
|
2012-09-20 13:03:55 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&filter->out_info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&filter->out_info),
|
|
|
|
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
2013-11-15 07:28:49 +00:00
|
|
|
gl->TexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
gl->TexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
gl->TexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
gl->TexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2010-04-27 07:08:22 +00:00
|
|
|
}
|
2008-09-23 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_filtersobel_reset_resources (GstGLFilter * filter)
|
2008-09-23 08:37:58 +00:00
|
|
|
{
|
|
|
|
GstGLFilterSobel *filtersobel = GST_GL_FILTERSOBEL (filter);
|
2015-03-13 10:25:35 +00:00
|
|
|
GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
|
2010-04-27 07:08:22 +00:00
|
|
|
int i;
|
2008-09-23 08:37:58 +00:00
|
|
|
|
2010-04-27 17:38:33 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2013-02-01 07:20:04 +00:00
|
|
|
gl->DeleteTextures (1, &filtersobel->midtexture[i]);
|
2010-04-27 07:08:22 +00:00
|
|
|
}
|
2008-09-23 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_filtersobel_class_init (GstGLFilterSobelClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2012-05-30 03:46:21 +00:00
|
|
|
GstElementClass *element_class;
|
2008-09-23 08:37:58 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
2012-05-30 03:46:21 +00:00
|
|
|
element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
2008-09-23 08:37:58 +00:00
|
|
|
gobject_class->set_property = gst_gl_filtersobel_set_property;
|
|
|
|
gobject_class->get_property = gst_gl_filtersobel_get_property;
|
|
|
|
|
2012-09-20 13:03:55 +00:00
|
|
|
GST_GL_FILTER_CLASS (klass)->filter_texture =
|
|
|
|
gst_gl_filtersobel_filter_texture;
|
2009-02-11 06:39:14 +00:00
|
|
|
GST_GL_FILTER_CLASS (klass)->display_init_cb =
|
|
|
|
gst_gl_filtersobel_init_resources;
|
|
|
|
GST_GL_FILTER_CLASS (klass)->display_reset_cb =
|
|
|
|
gst_gl_filtersobel_reset_resources;
|
2008-09-23 08:37:58 +00:00
|
|
|
GST_GL_FILTER_CLASS (klass)->onInitFBO = gst_gl_filtersobel_init_shader;
|
|
|
|
GST_GL_FILTER_CLASS (klass)->onReset = gst_gl_filter_filtersobel_reset;
|
2008-10-15 13:49:12 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_INVERT,
|
|
|
|
g_param_spec_boolean ("invert",
|
|
|
|
"Invert the colors",
|
|
|
|
"Invert colors to get dark edges on bright background",
|
2010-10-19 15:00:33 +00:00
|
|
|
FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2012-05-30 03:46:21 +00:00
|
|
|
|
2012-11-21 02:34:15 +00:00
|
|
|
gst_element_class_set_metadata (element_class,
|
2013-07-17 23:41:05 +00:00
|
|
|
"Gstreamer OpenGL Sobel", "Filter/Effect/Video", "Sobel edge detection",
|
2012-05-30 03:46:21 +00:00
|
|
|
"Filippo Argiolas <filippo.argiolas@gmail.com>");
|
2014-11-27 10:05:45 +00:00
|
|
|
|
2015-03-13 10:25:35 +00:00
|
|
|
GST_GL_BASE_FILTER_CLASS (klass)->supported_gl_api = GST_GL_API_OPENGL;
|
2008-09-23 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-05-30 03:46:21 +00:00
|
|
|
gst_gl_filtersobel_init (GstGLFilterSobel * filtersobel)
|
2008-09-23 08:37:58 +00:00
|
|
|
{
|
2010-04-27 07:08:22 +00:00
|
|
|
int i;
|
|
|
|
filtersobel->hconv = NULL;
|
|
|
|
filtersobel->vconv = NULL;
|
2008-10-15 13:49:12 +00:00
|
|
|
filtersobel->invert = FALSE;
|
2010-04-27 17:38:33 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2010-04-27 07:08:22 +00:00
|
|
|
filtersobel->midtexture[i] = 0;
|
|
|
|
}
|
2008-09-23 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_filter_filtersobel_reset (GstGLFilter * filter)
|
2008-09-23 08:37:58 +00:00
|
|
|
{
|
2009-02-11 06:39:14 +00:00
|
|
|
GstGLFilterSobel *filtersobel = GST_GL_FILTERSOBEL (filter);
|
2008-09-23 08:37:58 +00:00
|
|
|
|
|
|
|
//blocking call, wait the opengl thread has destroyed the shader
|
2013-09-24 06:37:11 +00:00
|
|
|
if (filtersobel->desat)
|
2015-03-13 10:25:35 +00:00
|
|
|
gst_gl_context_del_shader (GST_GL_BASE_FILTER (filter)->context,
|
|
|
|
filtersobel->desat);
|
2013-09-24 06:37:11 +00:00
|
|
|
filtersobel->desat = NULL;
|
|
|
|
|
|
|
|
if (filtersobel->hconv)
|
2015-03-13 10:25:35 +00:00
|
|
|
gst_gl_context_del_shader (GST_GL_BASE_FILTER (filter)->context,
|
|
|
|
filtersobel->hconv);
|
2013-09-24 06:37:11 +00:00
|
|
|
filtersobel->hconv = NULL;
|
|
|
|
|
|
|
|
if (filtersobel->vconv)
|
2015-03-13 10:25:35 +00:00
|
|
|
gst_gl_context_del_shader (GST_GL_BASE_FILTER (filter)->context,
|
|
|
|
filtersobel->vconv);
|
2013-09-24 06:37:11 +00:00
|
|
|
filtersobel->vconv = NULL;
|
|
|
|
|
|
|
|
if (filtersobel->len)
|
2015-03-13 10:25:35 +00:00
|
|
|
gst_gl_context_del_shader (GST_GL_BASE_FILTER (filter)->context,
|
|
|
|
filtersobel->len);
|
2013-09-24 06:37:11 +00:00
|
|
|
filtersobel->len = NULL;
|
2008-09-23 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_filtersobel_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
2008-10-15 13:49:12 +00:00
|
|
|
GstGLFilterSobel *filtersobel = GST_GL_FILTERSOBEL (object);
|
2008-09-23 08:37:58 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
2009-02-11 06:39:14 +00:00
|
|
|
case PROP_INVERT:
|
|
|
|
filtersobel->invert = g_value_get_boolean (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
2008-09-23 08:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_filtersobel_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
2008-10-15 13:49:12 +00:00
|
|
|
GstGLFilterSobel *filtersobel = GST_GL_FILTERSOBEL (object);
|
2008-09-23 08:37:58 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
2009-02-11 06:39:14 +00:00
|
|
|
case PROP_INVERT:
|
|
|
|
g_value_set_boolean (value, filtersobel->invert);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
2008-09-23 08:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-24 15:02:32 +00:00
|
|
|
static gboolean
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_filtersobel_init_shader (GstGLFilter * filter)
|
2008-09-23 08:37:58 +00:00
|
|
|
{
|
2009-02-11 06:39:14 +00:00
|
|
|
GstGLFilterSobel *filtersobel = GST_GL_FILTERSOBEL (filter);
|
2011-11-24 15:02:32 +00:00
|
|
|
gboolean ret = TRUE;
|
2008-09-23 08:37:58 +00:00
|
|
|
|
|
|
|
//blocking call, wait the opengl thread has compiled the shader
|
2011-11-24 15:02:32 +00:00
|
|
|
ret =
|
2015-03-13 10:25:35 +00:00
|
|
|
gst_gl_context_gen_shader (GST_GL_BASE_FILTER (filter)->context, 0,
|
|
|
|
desaturate_fragment_source, &filtersobel->desat);
|
2011-11-24 15:02:32 +00:00
|
|
|
ret &=
|
2015-03-13 10:25:35 +00:00
|
|
|
gst_gl_context_gen_shader (GST_GL_BASE_FILTER (filter)->context, 0,
|
2010-04-27 17:38:33 +00:00
|
|
|
sep_sobel_hconv3_fragment_source, &filtersobel->hconv);
|
2011-11-24 15:02:32 +00:00
|
|
|
ret &=
|
2015-03-13 10:25:35 +00:00
|
|
|
gst_gl_context_gen_shader (GST_GL_BASE_FILTER (filter)->context, 0,
|
2010-04-27 17:38:33 +00:00
|
|
|
sep_sobel_vconv3_fragment_source, &filtersobel->vconv);
|
2011-11-24 15:02:32 +00:00
|
|
|
ret &=
|
2015-03-13 10:25:35 +00:00
|
|
|
gst_gl_context_gen_shader (GST_GL_BASE_FILTER (filter)->context, 0,
|
2010-04-27 17:38:33 +00:00
|
|
|
sep_sobel_length_fragment_source, &filtersobel->len);
|
2011-11-24 15:02:32 +00:00
|
|
|
|
|
|
|
return ret;
|
2008-09-23 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-09-20 13:03:55 +00:00
|
|
|
gst_gl_filtersobel_filter_texture (GstGLFilter * filter, guint in_tex,
|
|
|
|
guint out_tex)
|
2008-09-23 08:37:58 +00:00
|
|
|
{
|
2009-02-11 06:39:14 +00:00
|
|
|
GstGLFilterSobel *filtersobel = GST_GL_FILTERSOBEL (filter);
|
2008-09-23 08:37:58 +00:00
|
|
|
|
2012-09-25 08:06:14 +00:00
|
|
|
gst_gl_filter_render_to_target_with_shader (filter, TRUE, in_tex,
|
2010-04-29 06:27:29 +00:00
|
|
|
filtersobel->midtexture[0], filtersobel->desat);
|
2012-09-25 08:06:14 +00:00
|
|
|
gst_gl_filter_render_to_target_with_shader (filter, FALSE,
|
2010-04-29 06:27:29 +00:00
|
|
|
filtersobel->midtexture[0], filtersobel->midtexture[1],
|
|
|
|
filtersobel->hconv);
|
2012-09-25 08:06:14 +00:00
|
|
|
gst_gl_filter_render_to_target_with_shader (filter, FALSE,
|
2010-04-29 06:27:29 +00:00
|
|
|
filtersobel->midtexture[1], filtersobel->midtexture[0],
|
|
|
|
filtersobel->vconv);
|
2012-09-25 08:06:14 +00:00
|
|
|
gst_gl_filter_render_to_target (filter, FALSE, filtersobel->midtexture[0],
|
2012-09-20 13:03:55 +00:00
|
|
|
out_tex, gst_gl_filtersobel_length, filtersobel);
|
2012-07-15 14:03:50 +00:00
|
|
|
|
2008-09-23 08:37:58 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-04-27 07:08:22 +00:00
|
|
|
static void
|
2010-04-27 17:38:33 +00:00
|
|
|
gst_gl_filtersobel_length (gint width, gint height, guint texture,
|
2010-04-27 07:08:22 +00:00
|
|
|
gpointer stuff)
|
|
|
|
{
|
2010-04-29 06:27:29 +00:00
|
|
|
GstGLFilter *filter = GST_GL_FILTER (stuff);
|
2015-03-13 10:25:35 +00:00
|
|
|
GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
|
2010-04-29 06:27:29 +00:00
|
|
|
GstGLFilterSobel *filtersobel = GST_GL_FILTERSOBEL (filter);
|
2010-04-27 07:08:22 +00:00
|
|
|
|
|
|
|
glMatrixMode (GL_PROJECTION);
|
|
|
|
glLoadIdentity ();
|
|
|
|
|
|
|
|
gst_gl_shader_use (filtersobel->len);
|
|
|
|
|
2013-02-01 07:20:04 +00:00
|
|
|
gl->ActiveTexture (GL_TEXTURE1);
|
2013-11-15 07:28:49 +00:00
|
|
|
gl->Enable (GL_TEXTURE_2D);
|
|
|
|
gl->BindTexture (GL_TEXTURE_2D, texture);
|
|
|
|
gl->Disable (GL_TEXTURE_2D);
|
2008-09-23 08:37:58 +00:00
|
|
|
|
2010-04-27 17:38:33 +00:00
|
|
|
gst_gl_shader_set_uniform_1i (filtersobel->len, "tex", 1);
|
|
|
|
gst_gl_shader_set_uniform_1i (filtersobel->len, "invert",
|
|
|
|
filtersobel->invert);
|
2008-10-15 13:49:12 +00:00
|
|
|
|
2012-09-25 08:06:14 +00:00
|
|
|
gst_gl_filter_draw_texture (filter, texture, width, height);
|
2008-09-23 08:37:58 +00:00
|
|
|
}
|