gleffects: Merge laplacian filter into effects

https://bugzilla.gnome.org/show_bug.cgi?id=746209
This commit is contained in:
Michał Dębski 2015-03-22 11:22:52 +01:00 committed by Tim-Philipp Müller
parent befbf3733b
commit afdc3c8ee3
9 changed files with 149 additions and 287 deletions

View file

@ -30,6 +30,7 @@ libgstopengl_la_SOURCES = \
effects/gstgleffectglow.c \
effects/gstgleffectblur.c \
effects/gstgleffectsobel.c \
effects/gstgleffectlaplacian.c \
gstglcolorscale.c \
gstglmixer.c \
gstglvideomixer.c \
@ -58,7 +59,6 @@ noinst_HEADERS = \
# full opengl required
if USE_OPENGL
libgstopengl_la_SOURCES += \
gstglfilterlaplacian.c \
gstglfilterglass.c \
gstgldeinterlace.c \
gltestsrc.c \
@ -66,7 +66,6 @@ libgstopengl_la_SOURCES += \
gstglmosaic.c
noinst_HEADERS += \
gstglfilterlaplacian.h \
gstglfilterglass.h \
gstgldeinterlace.h \
gltestsrc.h \

View file

@ -0,0 +1,74 @@
/*
* GStreamer
* Copyright (C) 2008-2010 Filippo Argiolas <filippo.argiolas@gmail.com>
* Copyright (C) 2015 Michał Dębski <debski.mi.zd@gmail.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 "../gstgleffects.h"
static void
gst_gl_effects_laplacian_callback (gint width, gint height, guint texture,
gpointer data)
{
GstGLShader *shader = NULL;
GstGLEffects *effects = GST_GL_EFFECTS (data);
GstGLFilter *filter = GST_GL_FILTER (effects);
if (NULL != (shader = gst_gl_effects_get_fragment_shader (effects, "conv0",
conv9_fragment_source_gles2, conv9_fragment_source_opengl))) {
GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
static gfloat kernel[9] = { 0.0, -1.0, 0.0,
-1.0, 4.0, -1.0,
0.0, -1.0, 0.0
};
#if GST_GL_HAVE_OPENGL
if (USING_OPENGL (GST_GL_BASE_FILTER (filter)->context)) {
gl->MatrixMode (GL_PROJECTION);
gl->LoadIdentity ();
}
#endif
gst_gl_shader_use (shader);
gl->ActiveTexture (GL_TEXTURE0);
gl->Enable (GL_TEXTURE_2D);
gl->BindTexture (GL_TEXTURE_2D, texture);
gl->Disable (GL_TEXTURE_2D);
gst_gl_shader_set_uniform_1i (shader, "tex", 0);
gst_gl_shader_set_uniform_1f (shader, "height", height);
gst_gl_shader_set_uniform_1f (shader, "width", width);
gst_gl_shader_set_uniform_1fv (shader, "kernel", 9, kernel);
gst_gl_shader_set_uniform_1i (shader, "invert", effects->invert);
gst_gl_filter_draw_texture (filter, texture, width, height);
}
}
void
gst_gl_effects_laplacian (GstGLEffects * effects)
{
gst_gl_filter_render_to_target (GST_GL_FILTER (effects), TRUE,
effects->intexture, effects->outtexture,
gst_gl_effects_laplacian_callback, effects);
}

View file

@ -803,4 +803,66 @@ const gchar *difference_fragment_source =
"gl_FragColor = vec4 (step (0.12, length (savedcolor - currentcolor)));"
"}";
/* This filter is meant as a demo of gst-plugins-gl + glsl
capabilities. So I'm keeping this shader readable enough. If and
when this shader will be used in production be careful to hard code
kernel into the shader and remove unneeded zero multiplications in
the convolution */
const gchar *conv9_fragment_source_opengl =
"uniform sampler2D tex;"
"uniform float kernel[9];"
"uniform float width, height;"
"uniform bool invert;"
"void main () {"
" float w = 1.0 / width;"
" float h = 1.0 / height;"
" vec2 texturecoord[9];"
" texturecoord[4] = gl_TexCoord[0].st;" /* 0 0 */
" texturecoord[5] = texturecoord[4] + vec2(w, 0.0);" /* 1 0 */
" texturecoord[2] = texturecoord[5] - vec2(0.0, h);" /* 1 -1 */
" texturecoord[1] = texturecoord[2] - vec2(w, 0.0);" /* 0 -1 */
" texturecoord[0] = texturecoord[1] - vec2(w, 0.0);" /* -1 -1 */
" texturecoord[3] = texturecoord[0] + vec2(0.0, h);" /* -1 0 */
" texturecoord[6] = texturecoord[3] + vec2(0.0, h);" /* -1 1 */
" texturecoord[7] = texturecoord[6] + vec2(w, 0.0);" /* 0 1 */
" texturecoord[8] = texturecoord[7] + vec2(w, 0.0);" /* 1 1 */
" int i;"
" vec3 sum = vec3 (0.0);"
" for (i = 0; i < 9; i++) { "
" vec4 neighbor = texture2D (tex, texturecoord[i]);"
" sum += neighbor.xyz * kernel[i];"
" }"
" gl_FragColor = vec4 (abs(sum - vec3(float(invert))), 1.0);"
"}";
const gchar *conv9_fragment_source_gles2 =
"#ifdef GL_ES\n"
"precision mediump float;\n"
"#endif\n"
"varying vec2 v_texcoord;"
"uniform sampler2D tex;"
"uniform float kernel[9];"
"uniform float width, height;"
"void main () {"
" float w = 1.0 / width;"
" float h = 1.0 / height;"
" vec2 texturecoord[9];"
" texturecoord[4] = v_texcoord.xy;" /* 0 0 */
" texturecoord[5] = texturecoord[4] + vec2(w, 0.0);" /* 1 0 */
" texturecoord[2] = texturecoord[5] - vec2(0.0, h);" /* 1 -1 */
" texturecoord[1] = texturecoord[2] - vec2(w, 0.0);" /* 0 -1 */
" texturecoord[0] = texturecoord[1] - vec2(w, 0.0);" /* -1 -1 */
" texturecoord[3] = texturecoord[0] + vec2(0.0, h);" /* -1 0 */
" texturecoord[6] = texturecoord[3] + vec2(0.0, h);" /* -1 1 */
" texturecoord[7] = texturecoord[6] + vec2(w, 0.0);" /* 0 1 */
" texturecoord[8] = texturecoord[7] + vec2(w, 0.0);" /* 1 1 */
" int i;"
" vec3 sum = vec3 (0.0);"
" for (i = 0; i < 9; i++) { "
" vec4 neighbor = texture2D (tex, texturecoord[i]);"
" sum += neighbor.xyz * kernel[i];"
" }"
" gl_FragColor = vec4 (abs(sum - vec3(float(invert))), 1.0);"
"}";
/* *INDENT-ON* */

View file

@ -41,6 +41,7 @@ extern const gchar *sep_sobel_hconv3_fragment_source_opengl;
extern const gchar *sep_sobel_vconv3_fragment_source_opengl;
extern const gchar *sep_sobel_length_fragment_source_opengl;
extern const gchar *multiply_fragment_source_opengl;
extern const gchar *conv9_fragment_source_opengl;
extern const gchar *mirror_fragment_source_gles2;
extern const gchar *squeeze_fragment_source_gles2;
@ -62,6 +63,7 @@ extern const gchar *sep_sobel_hconv3_fragment_source_gles2;
extern const gchar *sep_sobel_vconv3_fragment_source_gles2;
extern const gchar *sep_sobel_length_fragment_source_gles2;
extern const gchar *multiply_fragment_source_gles2;
extern const gchar *conv9_fragment_source_gles2;
extern const gchar *interpolate_fragment_source;
extern const gchar *texture_interp_fragment_source;

View file

@ -92,6 +92,7 @@ typedef enum
GST_GL_EFFECT_GLOW,
GST_GL_EFFECT_SOBEL,
GST_GL_EFFECT_BLUR,
GST_GL_EFFECT_LAPLACIAN,
GST_GL_N_EFFECTS
} GstGLEffectsEffect;
@ -119,6 +120,7 @@ gst_gl_effects_effect_get_type (void)
{GST_GL_EFFECT_GLOW, "Glow Lighting Effect", "glow"},
{GST_GL_EFFECT_SOBEL, "Sobel edge detection Effect", "sobel"},
{GST_GL_EFFECT_BLUR, "Blur with 9x9 separable convolution Effect", "blur"},
{GST_GL_EFFECT_LAPLACIAN, "Laplacian Convolution Demo Effect", "laplacian"},
{0, NULL, NULL}
};
@ -243,6 +245,12 @@ gst_gl_effects_set_effect (GstGLEffects * effects, gint effect_type)
GST_GL_API_GLES2 | GST_GL_API_OPENGL | GST_GL_API_OPENGL3;
effects->current_effect = effect_type;
break;
case GST_GL_EFFECT_LAPLACIAN:
effects->effect = (GstGLEffectProcessFunc) gst_gl_effects_laplacian;
filter_class->supported_gl_api =
GST_GL_API_GLES2 | GST_GL_API_OPENGL | GST_GL_API_OPENGL3;
effects->current_effect = effect_type;
break;
default:
g_assert_not_reached ();
}
@ -334,7 +342,7 @@ gst_gl_effects_class_init (GstGLEffectsClass * klass)
g_object_class_install_property (gobject_class,
PROP_INVERT,
g_param_spec_boolean ("invert",
"Invert the colours for sobel effect",
"Invert the colours for sobel and laplacian effect",
"Invert colors to get dark edges on bright background when using sobel effect",
FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

View file

@ -111,6 +111,7 @@ void gst_gl_effects_sin (GstGLEffects *effects);
void gst_gl_effects_glow (GstGLEffects *effects);
void gst_gl_effects_sobel (GstGLEffects *effects);
void gst_gl_effects_blur (GstGLEffects *effects);
void gst_gl_effects_laplacian (GstGLEffects *effects);
G_END_DECLS

View file

@ -1,225 +0,0 @@
/*
* GStreamer
* Copyright (C) 2008-2010 Filippo Argiolas <filippo.argiolas@gmail.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.
*/
/**
* SECTION:element-glfilterlaplacian
*
* Laplacian Convolution Demo Filter.
*
* <refsect2>
* <title>Examples</title>
* |[
* gst-launch videotestsrc ! glupload ! glfilterlaplacian ! glimagesink
* ]|
* FBO (Frame Buffer Object) and GLSL (OpenGL Shading Language) are required.
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstglfilterlaplacian.h"
#define GST_CAT_DEFAULT gst_gl_filter_laplacian_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
enum
{
PROP_0
};
#define DEBUG_INIT \
GST_DEBUG_CATEGORY_INIT (gst_gl_filter_laplacian_debug, "glfilterlaplacian", 0, "glfilterlaplacian element");
G_DEFINE_TYPE_WITH_CODE (GstGLFilterLaplacian, gst_gl_filter_laplacian,
GST_TYPE_GL_FILTER, DEBUG_INIT);
static void gst_gl_filter_laplacian_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_gl_filter_laplacian_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec);
static void gst_gl_filter_laplacian_reset (GstGLFilter * filter);
static gboolean gst_gl_filter_laplacian_init_shader (GstGLFilter * filter);
static gboolean gst_gl_filter_laplacian_filter_texture (GstGLFilter * filter,
guint in_tex, guint out_tex);
static void gst_gl_filter_laplacian_callback (gint width, gint height,
guint texture, gpointer stuff);
/* *INDENT-OFF* */
/* This filter is meant as a demo of gst-plugins-gl + glsl
capabilities. So I'm keeping this shader readable enough. If and
when this shader will be used in production be careful to hard code
kernel into the shader and remove unneeded zero multiplications in
the convolution */
static const gchar *convolution_fragment_source =
"uniform sampler2D tex;"
"uniform float kernel[9];"
"uniform float width, height;"
"void main () {"
" float w = 1.0 / width;"
" float h = 1.0 / height;"
" vec2 texturecoord[9];"
" texturecoord[4] = gl_TexCoord[0].st;" /* 0 0 */
" texturecoord[5] = texturecoord[4] + vec2(w, 0.0);" /* 1 0 */
" texturecoord[2] = texturecoord[5] - vec2(0.0, h);" /* 1 -1 */
" texturecoord[1] = texturecoord[2] - vec2(w, 0.0);" /* 0 -1 */
" texturecoord[0] = texturecoord[1] - vec2(w, 0.0);" /* -1 -1 */
" texturecoord[3] = texturecoord[0] + vec2(0.0, h);" /* -1 0 */
" texturecoord[6] = texturecoord[3] + vec2(0.0, h);" /* -1 1 */
" texturecoord[7] = texturecoord[6] + vec2(w, 0.0);" /* 0 1 */
" texturecoord[8] = texturecoord[7] + vec2(w, 0.0);" /* 1 1 */
" int i;"
" vec4 sum = vec4 (0.0);"
" for (i = 0; i < 9; i++) { "
" vec4 neighbor = texture2D(tex, texturecoord[i]);"
" sum += neighbor * kernel[i];"
" }"
" gl_FragColor = sum;"
"}";
/* *INDENT-ON* */
static void
gst_gl_filter_laplacian_class_init (GstGLFilterLaplacianClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *element_class;
gobject_class = (GObjectClass *) klass;
element_class = GST_ELEMENT_CLASS (klass);
gobject_class->set_property = gst_gl_filter_laplacian_set_property;
gobject_class->get_property = gst_gl_filter_laplacian_get_property;
gst_element_class_set_metadata (element_class,
"OpenGL laplacian filter", "Filter/Effect/Video",
"Laplacian Convolution Demo Filter",
"Filippo Argiolas <filippo.argiolas@gmail.com>");
GST_GL_FILTER_CLASS (klass)->filter_texture =
gst_gl_filter_laplacian_filter_texture;
GST_GL_FILTER_CLASS (klass)->onInitFBO = gst_gl_filter_laplacian_init_shader;
GST_GL_FILTER_CLASS (klass)->onReset = gst_gl_filter_laplacian_reset;
GST_GL_BASE_FILTER_CLASS (klass)->supported_gl_api = GST_GL_API_OPENGL;
}
static void
gst_gl_filter_laplacian_init (GstGLFilterLaplacian * filter)
{
filter->shader = NULL;
}
static void
gst_gl_filter_laplacian_reset (GstGLFilter * filter)
{
GstGLFilterLaplacian *laplacian_filter = GST_GL_FILTER_LAPLACIAN (filter);
//blocking call, wait the opengl thread has destroyed the shader
if (laplacian_filter->shader)
gst_gl_context_del_shader (GST_GL_BASE_FILTER (filter)->context,
laplacian_filter->shader);
laplacian_filter->shader = NULL;
}
static void
gst_gl_filter_laplacian_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
//GstGLFilterLaplacian *filter = GST_GL_FILTER_LAPLACIAN (object);
switch (prop_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_gl_filter_laplacian_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
//GstGLFilterLaplacian *filter = GST_GL_FILTER_LAPLACIAN (object);
switch (prop_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
gst_gl_filter_laplacian_init_shader (GstGLFilter * filter)
{
GstGLFilterLaplacian *laplacian_filter = GST_GL_FILTER_LAPLACIAN (filter);
//blocking call, wait the opengl thread has compiled the shader
return gst_gl_context_gen_shader (GST_GL_BASE_FILTER (filter)->context, 0,
convolution_fragment_source, &laplacian_filter->shader);
}
static gboolean
gst_gl_filter_laplacian_filter_texture (GstGLFilter * filter, guint in_tex,
guint out_tex)
{
gpointer laplacian_filter = GST_GL_FILTER_LAPLACIAN (filter);
//blocking call, use a FBO
gst_gl_filter_render_to_target (filter, TRUE, in_tex, out_tex,
gst_gl_filter_laplacian_callback, laplacian_filter);
return TRUE;
}
//opengl scene, params: input texture (not the output filter->texture)
static void
gst_gl_filter_laplacian_callback (gint width, gint height, guint texture,
gpointer stuff)
{
GstGLFilter *filter = GST_GL_FILTER (stuff);
GstGLFilterLaplacian *laplacian_filter = GST_GL_FILTER_LAPLACIAN (filter);
GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
gfloat kernel[9] = { 0.0, -1.0, 0.0,
-1.0, 4.0, -1.0,
0.0, -1.0, 0.0
};
gl->MatrixMode (GL_PROJECTION);
gl->LoadIdentity ();
gst_gl_shader_use (laplacian_filter->shader);
gl->ActiveTexture (GL_TEXTURE0);
gl->Enable (GL_TEXTURE_2D);
gl->BindTexture (GL_TEXTURE_2D, texture);
gst_gl_shader_set_uniform_1i (laplacian_filter->shader, "tex", 0);
gst_gl_shader_set_uniform_1fv (laplacian_filter->shader, "kernel", 9, kernel);
gst_gl_shader_set_uniform_1f (laplacian_filter->shader, "width",
(gfloat) width);
gst_gl_shader_set_uniform_1f (laplacian_filter->shader, "height",
(gfloat) height);
gst_gl_filter_draw_texture (filter, texture, width, height);
}

View file

@ -1,53 +0,0 @@
/*
* GStreamer
* Copyright (C) 2008 Filippo Argiolas <filippo.argiolas@gmail.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.
*/
#ifndef _GST_GL_FILTERLAPLACIAN_H_
#define _GST_GL_FILTERLAPLACIAN_H_
#include <gst/gl/gstglfilter.h>
G_BEGIN_DECLS
#define GST_TYPE_GL_FILTER_LAPLACIAN (gst_gl_filter_laplacian_get_type())
#define GST_GL_FILTER_LAPLACIAN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_GL_FILTER_LAPLACIAN,GstGLFilterLaplacian))
#define GST_IS_GL_FILTER_LAPLACIAN(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_GL_FILTER_LAPLACIAN))
#define GST_GL_FILTER_LAPLACIAN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_GL_FILTER_LAPLACIAN,GstGLFilterLaplacianClass))
#define GST_IS_GL_FILTER_LAPLACIAN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_GL_FILTER_LAPLACIAN))
#define GST_GL_FILTER_LAPLACIAN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_GL_FILTER_LAPLACIAN,GstGLFilterLaplacianClass))
typedef struct _GstGLFilterLaplacian GstGLFilterLaplacian;
typedef struct _GstGLFilterLaplacianClass GstGLFilterLaplacianClass;
struct _GstGLFilterLaplacian
{
GstGLFilter filter;
GstGLShader *shader;
};
struct _GstGLFilterLaplacianClass
{
GstGLFilterClass filter_class;
};
GType gst_gl_filter_laplacian_get_type (void);
G_END_DECLS
#endif /* _GST_GLFILTERLAPLACIAN_H_ */

View file

@ -69,7 +69,6 @@
#if GST_GL_HAVE_OPENGL
#include "gstgltestsrc.h"
#include "gstglfilterlaplacian.h"
#include "gstglfilterglass.h"
/* #include "gstglfilterreflectedscreen.h" */
#include "gstgldeinterlace.h"
@ -200,11 +199,6 @@ plugin_init (GstPlugin * plugin)
return FALSE;
}
if (!gst_element_register (plugin, "glfilterlaplacian",
GST_RANK_NONE, GST_TYPE_GL_FILTER_LAPLACIAN)) {
return FALSE;
}
if (!gst_element_register (plugin, "glfilterglass",
GST_RANK_NONE, GST_TYPE_GL_FILTER_GLASS)) {
return FALSE;