2008-08-18 18:25:25 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2012-11-08 11:53:56 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2008-08-18 18:25:25 +00:00
|
|
|
*/
|
|
|
|
|
2009-01-15 17:39:48 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-gldifferencematte.
|
2017-03-08 18:01:13 +00:00
|
|
|
* @title: gldifferencematte.
|
2009-01-15 17:39:48 +00:00
|
|
|
*
|
|
|
|
* Saves a background frame and replace it with a pixbuf.
|
|
|
|
*
|
2017-03-08 18:01:13 +00:00
|
|
|
* ## Examples
|
2009-01-15 17:39:48 +00:00
|
|
|
* |[
|
2015-12-14 02:09:46 +00:00
|
|
|
* gst-launch-1.0 videotestsrc ! glupload ! gldifferencemate location=backgroundimagefile ! glimagesink
|
2009-01-15 17:39:48 +00:00
|
|
|
* ]|
|
|
|
|
* FBO (Frame Buffer Object) and GLSL (OpenGL Shading Language) are required.
|
2017-03-08 18:01:13 +00:00
|
|
|
*
|
2009-01-15 17:39:48 +00:00
|
|
|
*/
|
|
|
|
|
2008-08-18 18:25:25 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2009-02-23 11:35:48 +00:00
|
|
|
#include <stdlib.h>
|
2009-01-23 01:04:23 +00:00
|
|
|
#include <png.h>
|
2013-07-11 09:03:31 +00:00
|
|
|
|
2017-07-07 15:15:12 +00:00
|
|
|
#include <gst/gl/gstglfuncs.h>
|
|
|
|
|
2009-07-29 23:36:30 +00:00
|
|
|
#include "gstgldifferencematte.h"
|
2013-07-11 09:03:31 +00:00
|
|
|
#include "effects/gstgleffectssources.h"
|
2008-08-18 18:25:25 +00:00
|
|
|
|
2010-03-09 00:24:06 +00:00
|
|
|
#if PNG_LIBPNG_VER >= 10400
|
|
|
|
#define int_p_NULL NULL
|
|
|
|
#define png_infopp_NULL NULL
|
|
|
|
#endif
|
|
|
|
|
2008-08-18 18:25:25 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_gl_differencematte_debug
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
|
|
|
|
|
2012-05-30 03:46:21 +00:00
|
|
|
#define DEBUG_INIT \
|
2008-08-18 18:25:25 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_gl_differencematte_debug, "gldifferencematte", 0, "gldifferencematte element");
|
|
|
|
|
2016-11-07 15:21:20 +00:00
|
|
|
#define gst_gl_differencematte_parent_class parent_class
|
2012-05-30 03:46:21 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstGLDifferenceMatte, gst_gl_differencematte,
|
2009-02-11 06:39:14 +00:00
|
|
|
GST_TYPE_GL_FILTER, DEBUG_INIT);
|
2008-08-18 18:25:25 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
static void gst_gl_differencematte_set_property (GObject * object,
|
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_gl_differencematte_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec);
|
2008-08-18 18:25:25 +00:00
|
|
|
|
2012-09-20 13:03:55 +00:00
|
|
|
static gboolean gst_gl_differencematte_filter_texture (GstGLFilter * filter,
|
2016-07-11 14:30:22 +00:00
|
|
|
GstGLMemory * in_tex, GstGLMemory * out_tex);
|
2008-08-18 18:25:25 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
static gboolean gst_gl_differencematte_loader (GstGLFilter * filter);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2008-08-18 18:25:25 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_LOCATION,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* init resources that need a gl context */
|
2016-11-07 15:21:20 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_differencematte_gl_start (GstGLBaseFilter * base_filter)
|
2008-08-18 18:25:25 +00:00
|
|
|
{
|
2016-11-07 15:21:20 +00:00
|
|
|
GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (base_filter);
|
|
|
|
GstGLFilter *filter = GST_GL_FILTER (base_filter);
|
|
|
|
GstGLContext *context = base_filter->context;
|
2016-07-11 14:30:22 +00:00
|
|
|
GstGLBaseMemoryAllocator *tex_alloc;
|
|
|
|
GstGLAllocationParams *params;
|
2015-09-04 06:02:32 +00:00
|
|
|
GError *error = NULL;
|
2008-08-18 18:25:25 +00:00
|
|
|
gint i;
|
|
|
|
|
2016-11-07 15:21:20 +00:00
|
|
|
if (!GST_GL_BASE_FILTER_CLASS (parent_class)->gl_start (base_filter))
|
|
|
|
return FALSE;
|
|
|
|
|
2016-07-11 14:30:22 +00:00
|
|
|
tex_alloc = (GstGLBaseMemoryAllocator *)
|
|
|
|
gst_gl_memory_allocator_get_default (context);
|
|
|
|
params =
|
|
|
|
(GstGLAllocationParams *) gst_gl_video_allocation_params_new (context,
|
2017-03-13 03:28:47 +00:00
|
|
|
NULL, &filter->out_info, 0, NULL, GST_GL_TEXTURE_TARGET_2D, GST_GL_RGBA);
|
2016-07-11 14:30:22 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
differencematte->midtexture[i] =
|
|
|
|
(GstGLMemory *) gst_gl_base_memory_alloc (tex_alloc, params);
|
|
|
|
gst_gl_allocation_params_free (params);
|
|
|
|
gst_object_unref (tex_alloc);
|
2016-03-17 12:50:00 +00:00
|
|
|
|
|
|
|
if (!(differencematte->identity_shader =
|
|
|
|
gst_gl_shader_new_default (context, &error))) {
|
|
|
|
GST_ELEMENT_ERROR (differencematte, RESOURCE, NOT_FOUND, ("%s",
|
|
|
|
"Failed to compile identity shader"), ("%s", error->message));
|
2016-11-07 15:21:20 +00:00
|
|
|
return FALSE;
|
2008-08-18 19:40:26 +00:00
|
|
|
}
|
2008-08-18 18:25:25 +00:00
|
|
|
|
2015-09-04 06:02:32 +00:00
|
|
|
if (!(differencematte->shader[0] =
|
|
|
|
gst_gl_shader_new_link_with_stages (context, &error,
|
|
|
|
gst_glsl_stage_new_default_vertex (context),
|
|
|
|
gst_glsl_stage_new_with_string (context, GL_FRAGMENT_SHADER,
|
2016-03-17 12:50:00 +00:00
|
|
|
GST_GLSL_VERSION_NONE,
|
|
|
|
GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY,
|
2015-09-04 06:02:32 +00:00
|
|
|
difference_fragment_source), NULL))) {
|
2015-08-10 14:38:32 +00:00
|
|
|
GST_ELEMENT_ERROR (differencematte, RESOURCE, NOT_FOUND, ("%s",
|
2016-03-17 12:50:00 +00:00
|
|
|
"Failed to compile difference shader"), ("%s", error->message));
|
2016-11-07 15:21:20 +00:00
|
|
|
return FALSE;
|
2011-11-24 15:02:32 +00:00
|
|
|
}
|
2009-02-11 06:39:14 +00:00
|
|
|
|
2015-09-04 06:02:32 +00:00
|
|
|
if (!(differencematte->shader[1] =
|
|
|
|
gst_gl_shader_new_link_with_stages (context, &error,
|
|
|
|
gst_glsl_stage_new_default_vertex (context),
|
|
|
|
gst_glsl_stage_new_with_string (context, GL_FRAGMENT_SHADER,
|
2016-03-17 12:50:00 +00:00
|
|
|
GST_GLSL_VERSION_NONE,
|
|
|
|
GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY,
|
2015-09-04 06:02:32 +00:00
|
|
|
hconv7_fragment_source_gles2), NULL))) {
|
2015-08-10 14:38:32 +00:00
|
|
|
GST_ELEMENT_ERROR (differencematte, RESOURCE, NOT_FOUND, ("%s",
|
2016-03-17 12:50:00 +00:00
|
|
|
"Failed to compile convolution shader"), ("%s", error->message));
|
2016-11-07 15:21:20 +00:00
|
|
|
return FALSE;
|
2011-11-24 15:02:32 +00:00
|
|
|
}
|
2009-02-11 06:39:14 +00:00
|
|
|
|
2015-09-04 06:02:32 +00:00
|
|
|
if (!(differencematte->shader[2] =
|
|
|
|
gst_gl_shader_new_link_with_stages (context, &error,
|
|
|
|
gst_glsl_stage_new_default_vertex (context),
|
|
|
|
gst_glsl_stage_new_with_string (context, GL_FRAGMENT_SHADER,
|
2016-03-17 12:50:00 +00:00
|
|
|
GST_GLSL_VERSION_NONE,
|
|
|
|
GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY,
|
2015-09-04 06:02:32 +00:00
|
|
|
vconv7_fragment_source_gles2), NULL))) {
|
2015-08-10 14:38:32 +00:00
|
|
|
GST_ELEMENT_ERROR (differencematte, RESOURCE, NOT_FOUND, ("%s",
|
2016-03-17 12:50:00 +00:00
|
|
|
"Failed to compile convolution shader"), ("%s", error->message));
|
2016-11-07 15:21:20 +00:00
|
|
|
return FALSE;
|
2011-11-24 15:02:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 06:02:32 +00:00
|
|
|
if (!(differencematte->shader[3] =
|
|
|
|
gst_gl_shader_new_link_with_stages (context, &error,
|
|
|
|
gst_glsl_stage_new_default_vertex (context),
|
|
|
|
gst_glsl_stage_new_with_string (context, GL_FRAGMENT_SHADER,
|
2016-03-17 12:50:00 +00:00
|
|
|
GST_GLSL_VERSION_NONE,
|
|
|
|
GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY,
|
2015-09-04 06:02:32 +00:00
|
|
|
texture_interp_fragment_source), NULL))) {
|
2015-08-10 14:38:32 +00:00
|
|
|
GST_ELEMENT_ERROR (differencematte, RESOURCE, NOT_FOUND, ("%s",
|
2016-03-17 12:50:00 +00:00
|
|
|
"Failed to compile interpolation shader"), ("%s", error->message));
|
2016-11-07 15:21:20 +00:00
|
|
|
return FALSE;
|
2011-11-24 15:02:32 +00:00
|
|
|
}
|
2015-09-04 06:02:32 +00:00
|
|
|
|
|
|
|
/* FIXME: this should really be per shader */
|
|
|
|
filter->draw_attr_position_loc =
|
2016-03-17 12:50:00 +00:00
|
|
|
gst_gl_shader_get_attribute_location (differencematte->shader[2],
|
2015-09-04 06:02:32 +00:00
|
|
|
"a_position");
|
|
|
|
filter->draw_attr_texture_loc =
|
2016-03-17 12:50:00 +00:00
|
|
|
gst_gl_shader_get_attribute_location (differencematte->shader[2],
|
2015-09-04 06:02:32 +00:00
|
|
|
"a_texcoord");
|
2016-11-07 15:21:20 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2008-08-18 18:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* free resources that need a gl context */
|
|
|
|
static void
|
2016-11-07 15:21:20 +00:00
|
|
|
gst_gl_differencematte_gl_stop (GstGLBaseFilter * base_filter)
|
2008-08-18 18:25:25 +00:00
|
|
|
{
|
2016-11-07 15:21:20 +00:00
|
|
|
GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (base_filter);
|
2008-08-18 18:25:25 +00:00
|
|
|
gint i;
|
2009-02-11 06:39:14 +00:00
|
|
|
|
2016-07-11 14:30:22 +00:00
|
|
|
if (differencematte->savedbgtexture) {
|
|
|
|
gst_memory_unref (GST_MEMORY_CAST (differencematte->savedbgtexture));
|
|
|
|
differencematte->savedbgtexture = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (differencematte->newbgtexture) {
|
|
|
|
gst_memory_unref (GST_MEMORY_CAST (differencematte->newbgtexture));
|
|
|
|
differencematte->newbgtexture = NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2016-03-17 12:50:00 +00:00
|
|
|
if (differencematte->identity_shader) {
|
|
|
|
gst_object_unref (differencematte->identity_shader);
|
|
|
|
differencematte->identity_shader = NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-20 06:55:41 +00:00
|
|
|
if (differencematte->shader[i]) {
|
2013-06-13 04:36:41 +00:00
|
|
|
gst_object_unref (differencematte->shader[i]);
|
2012-08-20 06:55:41 +00:00
|
|
|
differencematte->shader[i] = NULL;
|
|
|
|
}
|
2016-07-11 14:30:22 +00:00
|
|
|
|
2012-08-20 06:55:41 +00:00
|
|
|
if (differencematte->midtexture[i]) {
|
2016-07-11 14:30:22 +00:00
|
|
|
gst_memory_unref (GST_MEMORY_CAST (differencematte->midtexture[i]));
|
|
|
|
differencematte->midtexture[i] = NULL;
|
2012-08-20 06:55:41 +00:00
|
|
|
}
|
2008-08-18 19:40:26 +00:00
|
|
|
}
|
2008-08-19 07:48:14 +00:00
|
|
|
differencematte->location = NULL;
|
|
|
|
differencematte->pixbuf = NULL;
|
|
|
|
differencematte->bg_has_changed = FALSE;
|
2016-11-07 15:21:20 +00:00
|
|
|
|
|
|
|
GST_GL_BASE_FILTER_CLASS (parent_class)->gl_stop (base_filter);
|
2008-08-18 18:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_differencematte_class_init (GstGLDifferenceMatteClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2012-05-30 03:46:21 +00:00
|
|
|
GstElementClass *element_class;
|
2008-08-18 18:25:25 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
2012-05-30 03:46:21 +00:00
|
|
|
element_class = GST_ELEMENT_CLASS (klass);
|
2017-07-06 11:09:50 +00:00
|
|
|
|
|
|
|
gst_gl_filter_add_rgba_pad_templates (GST_GL_FILTER_CLASS (klass));
|
|
|
|
|
2008-08-18 18:25:25 +00:00
|
|
|
gobject_class->set_property = gst_gl_differencematte_set_property;
|
|
|
|
gobject_class->get_property = gst_gl_differencematte_get_property;
|
|
|
|
|
2016-11-07 15:21:20 +00:00
|
|
|
GST_GL_BASE_FILTER_CLASS (klass)->gl_start = gst_gl_differencematte_gl_start;
|
|
|
|
GST_GL_BASE_FILTER_CLASS (klass)->gl_stop = gst_gl_differencematte_gl_stop;
|
|
|
|
|
2012-09-20 13:03:55 +00:00
|
|
|
GST_GL_FILTER_CLASS (klass)->filter_texture =
|
|
|
|
gst_gl_differencematte_filter_texture;
|
2008-08-18 18:25:25 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
2009-02-11 06:39:14 +00:00
|
|
|
PROP_LOCATION,
|
|
|
|
g_param_spec_string ("location",
|
|
|
|
"Background image location",
|
2010-10-19 15:00:33 +00:00
|
|
|
"Background image location", NULL,
|
|
|
|
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 DifferenceMatte", "Filter/Effect/Video",
|
2012-05-30 03:46:21 +00:00
|
|
|
"Saves a background frame and replace it with a pixbuf",
|
|
|
|
"Filippo Argiolas <filippo.argiolas@gmail.com>");
|
2014-11-27 10:05:45 +00:00
|
|
|
|
2015-08-10 14:38:32 +00:00
|
|
|
GST_GL_BASE_FILTER_CLASS (klass)->supported_gl_api =
|
|
|
|
GST_GL_API_OPENGL | GST_GL_API_OPENGL3 | GST_GL_API_GLES2;
|
2008-08-18 18:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-05-30 03:46:21 +00:00
|
|
|
gst_gl_differencematte_init (GstGLDifferenceMatte * differencematte)
|
2008-08-18 18:25:25 +00:00
|
|
|
{
|
|
|
|
differencematte->shader[0] = NULL;
|
|
|
|
differencematte->shader[1] = NULL;
|
|
|
|
differencematte->shader[2] = NULL;
|
|
|
|
differencematte->shader[3] = NULL;
|
|
|
|
differencematte->location = NULL;
|
|
|
|
differencematte->pixbuf = NULL;
|
|
|
|
differencematte->savedbgtexture = 0;
|
|
|
|
differencematte->newbgtexture = 0;
|
|
|
|
differencematte->bg_has_changed = FALSE;
|
2010-04-23 18:06:48 +00:00
|
|
|
|
2010-04-29 18:49:02 +00:00
|
|
|
fill_gaussian_kernel (differencematte->kernel, 7, 30.0);
|
2008-08-18 18:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_differencematte_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
2009-02-11 06:39:14 +00:00
|
|
|
GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (object);
|
2008-08-18 18:25:25 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
2009-02-11 06:39:14 +00:00
|
|
|
case PROP_LOCATION:
|
2015-11-17 23:23:17 +00:00
|
|
|
g_free (differencematte->location);
|
2009-02-11 06:39:14 +00:00
|
|
|
differencematte->bg_has_changed = TRUE;
|
|
|
|
differencematte->location = g_value_dup_string (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
2008-08-18 18:25:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_differencematte_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2009-02-11 06:39:14 +00:00
|
|
|
case PROP_LOCATION:
|
|
|
|
g_value_set_string (value, differencematte->location);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
2008-08-18 18:25:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
static void
|
2016-07-11 14:30:22 +00:00
|
|
|
init_pixbuf_texture (GstGLDifferenceMatte * differencematte)
|
2008-08-18 19:40:26 +00:00
|
|
|
{
|
2016-07-11 14:30:22 +00:00
|
|
|
GstGLContext *context = GST_GL_BASE_FILTER (differencematte)->context;
|
|
|
|
GstGLFilter *filter = GST_GL_FILTER (differencematte);
|
|
|
|
GstGLBaseMemoryAllocator *tex_alloc;
|
|
|
|
GstGLAllocationParams *params;
|
|
|
|
GstVideoInfo v_info;
|
|
|
|
|
|
|
|
tex_alloc = (GstGLBaseMemoryAllocator *)
|
|
|
|
gst_gl_memory_allocator_get_default (context);
|
|
|
|
gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA,
|
|
|
|
differencematte->pbuf_width, differencematte->pbuf_height);
|
|
|
|
params =
|
|
|
|
(GstGLAllocationParams *) gst_gl_video_allocation_params_new (context,
|
2017-03-13 03:28:47 +00:00
|
|
|
NULL, &v_info, 0, NULL, GST_GL_TEXTURE_TARGET_2D, GST_GL_RGBA);
|
2016-07-11 14:30:22 +00:00
|
|
|
|
|
|
|
differencematte->newbgtexture =
|
|
|
|
(GstGLMemory *) gst_gl_base_memory_alloc (tex_alloc, params);
|
|
|
|
gst_gl_allocation_params_free (params);
|
|
|
|
|
|
|
|
if (differencematte->savedbgtexture == NULL) {
|
|
|
|
params =
|
|
|
|
(GstGLAllocationParams *) gst_gl_video_allocation_params_new (context,
|
|
|
|
NULL, &filter->out_info, 0, NULL, GST_GL_TEXTURE_TARGET_2D,
|
2017-03-13 03:28:47 +00:00
|
|
|
GST_GL_RGBA);
|
2016-07-11 14:30:22 +00:00
|
|
|
|
|
|
|
differencematte->savedbgtexture =
|
|
|
|
(GstGLMemory *) gst_gl_base_memory_alloc (tex_alloc, params);
|
|
|
|
gst_gl_allocation_params_free (params);
|
2008-08-18 19:40:26 +00:00
|
|
|
}
|
2016-07-11 14:30:22 +00:00
|
|
|
|
|
|
|
gst_object_unref (tex_alloc);
|
2008-08-18 19:40:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 03:39:54 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_differencematte_diff (GstGLFilter * filter, GstGLMemory * in_tex,
|
2009-02-11 06:39:14 +00:00
|
|
|
gpointer stuff)
|
2008-08-18 18:25:25 +00:00
|
|
|
{
|
2016-07-15 03:39:54 +00:00
|
|
|
GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter);
|
|
|
|
const GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
|
2009-02-11 06:39:14 +00:00
|
|
|
|
2008-08-18 18:25:25 +00:00
|
|
|
gst_gl_shader_use (differencematte->shader[0]);
|
|
|
|
|
2013-01-17 12:17:14 +00:00
|
|
|
gl->ActiveTexture (GL_TEXTURE0);
|
2016-07-15 03:39:54 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_2D, gst_gl_memory_get_texture_id (in_tex));
|
2009-02-11 06:39:14 +00:00
|
|
|
|
2008-08-18 18:25:25 +00:00
|
|
|
gst_gl_shader_set_uniform_1i (differencematte->shader[0], "current", 0);
|
|
|
|
|
2013-01-17 12:17:14 +00:00
|
|
|
gl->ActiveTexture (GL_TEXTURE1);
|
2016-07-15 03:39:54 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_2D,
|
|
|
|
gst_gl_memory_get_texture_id (differencematte->savedbgtexture));
|
2008-08-18 18:25:25 +00:00
|
|
|
|
2008-08-19 06:50:14 +00:00
|
|
|
gst_gl_shader_set_uniform_1i (differencematte->shader[0], "saved", 1);
|
2008-08-18 18:25:25 +00:00
|
|
|
|
2016-07-11 12:44:16 +00:00
|
|
|
gst_gl_filter_draw_fullscreen_quad (filter);
|
2016-07-15 03:39:54 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2008-08-18 18:25:25 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 03:39:54 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_differencematte_hblur (GstGLFilter * filter, GstGLMemory * in_tex,
|
2009-02-11 06:39:14 +00:00
|
|
|
gpointer stuff)
|
2008-08-18 18:25:25 +00:00
|
|
|
{
|
2016-07-15 03:39:54 +00:00
|
|
|
GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter);
|
|
|
|
const GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
|
2009-02-11 06:39:14 +00:00
|
|
|
|
2008-08-18 19:40:26 +00:00
|
|
|
gst_gl_shader_use (differencematte->shader[1]);
|
|
|
|
|
2013-01-17 12:17:14 +00:00
|
|
|
gl->ActiveTexture (GL_TEXTURE0);
|
2016-07-15 03:39:54 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_2D, gst_gl_memory_get_texture_id (in_tex));
|
2009-02-11 06:39:14 +00:00
|
|
|
|
2008-08-18 19:40:26 +00:00
|
|
|
gst_gl_shader_set_uniform_1i (differencematte->shader[1], "tex", 0);
|
|
|
|
|
2010-04-29 18:49:02 +00:00
|
|
|
gst_gl_shader_set_uniform_1fv (differencematte->shader[1], "kernel", 7,
|
2010-04-23 18:06:48 +00:00
|
|
|
differencematte->kernel);
|
2016-03-17 12:50:00 +00:00
|
|
|
gst_gl_shader_set_uniform_1f (differencematte->shader[1], "gauss_width",
|
2016-07-15 03:39:54 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&filter->out_info));
|
2008-08-18 19:40:26 +00:00
|
|
|
|
2016-07-11 12:44:16 +00:00
|
|
|
gst_gl_filter_draw_fullscreen_quad (filter);
|
2016-07-15 03:39:54 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2008-08-18 19:40:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 03:39:54 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_differencematte_vblur (GstGLFilter * filter, GstGLMemory * in_tex,
|
2009-02-11 06:39:14 +00:00
|
|
|
gpointer stuff)
|
2008-08-18 19:40:26 +00:00
|
|
|
{
|
2016-07-15 03:39:54 +00:00
|
|
|
GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter);
|
|
|
|
const GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
|
2009-02-11 06:39:14 +00:00
|
|
|
|
2008-08-18 19:40:26 +00:00
|
|
|
gst_gl_shader_use (differencematte->shader[2]);
|
|
|
|
|
2013-01-17 12:17:14 +00:00
|
|
|
gl->ActiveTexture (GL_TEXTURE0);
|
2016-07-15 03:39:54 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_2D, gst_gl_memory_get_texture_id (in_tex));
|
2008-08-18 19:40:26 +00:00
|
|
|
|
|
|
|
gst_gl_shader_set_uniform_1i (differencematte->shader[2], "tex", 0);
|
|
|
|
|
2010-04-29 18:49:02 +00:00
|
|
|
gst_gl_shader_set_uniform_1fv (differencematte->shader[2], "kernel", 7,
|
2010-04-23 18:06:48 +00:00
|
|
|
differencematte->kernel);
|
2016-03-17 12:50:00 +00:00
|
|
|
gst_gl_shader_set_uniform_1f (differencematte->shader[2], "gauss_height",
|
2016-07-15 03:39:54 +00:00
|
|
|
GST_VIDEO_INFO_HEIGHT (&filter->out_info));
|
2009-02-11 06:39:14 +00:00
|
|
|
|
2016-07-11 12:44:16 +00:00
|
|
|
gst_gl_filter_draw_fullscreen_quad (filter);
|
2016-07-15 03:39:54 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2008-08-18 19:40:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 03:39:54 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_differencematte_interp (GstGLFilter * filter, GstGLMemory * in_tex,
|
2009-02-11 06:39:14 +00:00
|
|
|
gpointer stuff)
|
2008-08-18 19:40:26 +00:00
|
|
|
{
|
2016-07-15 03:39:54 +00:00
|
|
|
GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter);
|
|
|
|
const GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
|
2009-02-11 06:39:14 +00:00
|
|
|
|
2008-08-18 19:40:26 +00:00
|
|
|
gst_gl_shader_use (differencematte->shader[3]);
|
|
|
|
|
2013-01-17 12:17:14 +00:00
|
|
|
gl->ActiveTexture (GL_TEXTURE0);
|
2016-07-15 03:39:54 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_2D, gst_gl_memory_get_texture_id (in_tex));
|
2009-02-11 06:39:14 +00:00
|
|
|
|
2008-08-19 06:50:14 +00:00
|
|
|
gst_gl_shader_set_uniform_1i (differencematte->shader[3], "blend", 0);
|
2008-08-18 19:40:26 +00:00
|
|
|
|
2013-01-17 12:17:14 +00:00
|
|
|
gl->ActiveTexture (GL_TEXTURE1);
|
2016-07-11 14:30:22 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_2D, differencematte->newbgtexture->tex_id);
|
2008-08-18 18:25:25 +00:00
|
|
|
|
2008-08-19 06:50:14 +00:00
|
|
|
gst_gl_shader_set_uniform_1i (differencematte->shader[3], "base", 1);
|
2008-08-18 19:40:26 +00:00
|
|
|
|
2013-01-17 12:17:14 +00:00
|
|
|
gl->ActiveTexture (GL_TEXTURE2);
|
2016-07-11 14:30:22 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_2D, differencematte->midtexture[2]->tex_id);
|
2008-08-18 19:40:26 +00:00
|
|
|
|
|
|
|
gst_gl_shader_set_uniform_1i (differencematte->shader[3], "alpha", 2);
|
|
|
|
|
2016-07-11 12:44:16 +00:00
|
|
|
gst_gl_filter_draw_fullscreen_quad (filter);
|
2008-08-18 18:25:25 +00:00
|
|
|
|
2016-07-15 03:39:54 +00:00
|
|
|
return TRUE;
|
2008-08-19 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2008-08-18 18:25:25 +00:00
|
|
|
static gboolean
|
2016-07-11 14:30:22 +00:00
|
|
|
gst_gl_differencematte_filter_texture (GstGLFilter * filter,
|
|
|
|
GstGLMemory * in_tex, GstGLMemory * out_tex)
|
2008-08-18 18:25:25 +00:00
|
|
|
{
|
2009-02-11 06:39:14 +00:00
|
|
|
GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter);
|
2008-08-18 18:25:25 +00:00
|
|
|
|
2012-09-20 13:03:55 +00:00
|
|
|
differencematte->intexture = in_tex;
|
2008-08-18 19:40:26 +00:00
|
|
|
|
2008-08-18 18:25:25 +00:00
|
|
|
if (differencematte->bg_has_changed && (differencematte->location != NULL)) {
|
2009-01-23 01:04:23 +00:00
|
|
|
|
|
|
|
if (!gst_gl_differencematte_loader (filter))
|
|
|
|
differencematte->pixbuf = NULL;
|
|
|
|
|
2016-07-11 14:30:22 +00:00
|
|
|
init_pixbuf_texture (differencematte);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
|
|
|
/* save current frame, needed to calculate difference between
|
|
|
|
* this frame and next ones */
|
2016-07-15 03:39:54 +00:00
|
|
|
gst_gl_filter_render_to_target_with_shader (filter, in_tex,
|
|
|
|
differencematte->savedbgtexture, differencematte->identity_shader);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
|
|
|
if (differencematte->pixbuf) {
|
|
|
|
free (differencematte->pixbuf);
|
|
|
|
differencematte->pixbuf = NULL;
|
2008-08-18 18:25:25 +00:00
|
|
|
}
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2008-08-18 18:25:25 +00:00
|
|
|
differencematte->bg_has_changed = FALSE;
|
|
|
|
}
|
|
|
|
|
2016-07-11 14:30:22 +00:00
|
|
|
if (differencematte->savedbgtexture != NULL) {
|
2016-07-15 03:39:54 +00:00
|
|
|
gst_gl_filter_render_to_target (filter, in_tex,
|
|
|
|
differencematte->midtexture[0], gst_gl_differencematte_diff, NULL);
|
|
|
|
gst_gl_filter_render_to_target (filter, differencematte->midtexture[0],
|
|
|
|
differencematte->midtexture[1], gst_gl_differencematte_hblur, NULL);
|
|
|
|
gst_gl_filter_render_to_target (filter, differencematte->midtexture[1],
|
|
|
|
differencematte->midtexture[2], gst_gl_differencematte_vblur, NULL);
|
|
|
|
gst_gl_filter_render_to_target (filter, in_tex, out_tex,
|
|
|
|
gst_gl_differencematte_interp, NULL);
|
2012-09-20 13:03:55 +00:00
|
|
|
} else {
|
2016-07-15 03:39:54 +00:00
|
|
|
gst_gl_filter_render_to_target_with_shader (filter, in_tex, out_tex,
|
|
|
|
differencematte->identity_shader);
|
2008-08-19 06:50:14 +00:00
|
|
|
}
|
2009-02-11 06:39:14 +00:00
|
|
|
|
2008-08-18 18:25:25 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
static void
|
|
|
|
user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
|
2009-01-23 01:04:23 +00:00
|
|
|
{
|
2009-02-11 06:39:14 +00:00
|
|
|
g_warning ("%s\n", warning_msg);
|
2009-01-23 01:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define LOAD_ERROR(msg) { GST_WARNING ("unable to load %s: %s", differencematte->location, msg); return FALSE; }
|
|
|
|
|
|
|
|
static gboolean
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_differencematte_loader (GstGLFilter * filter)
|
2009-01-23 01:04:23 +00:00
|
|
|
{
|
2009-02-11 06:39:14 +00:00
|
|
|
GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
|
|
|
png_structp png_ptr;
|
|
|
|
png_infop info_ptr;
|
|
|
|
guint sig_read = 0;
|
|
|
|
png_uint_32 width = 0;
|
|
|
|
png_uint_32 height = 0;
|
|
|
|
gint bit_depth = 0;
|
|
|
|
gint color_type = 0;
|
|
|
|
gint interlace_type = 0;
|
|
|
|
png_FILE_p fp = NULL;
|
|
|
|
guint y = 0;
|
|
|
|
guchar **rows = NULL;
|
2009-10-28 09:41:53 +00:00
|
|
|
gint filler;
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2015-03-13 10:25:35 +00:00
|
|
|
if (!GST_GL_BASE_FILTER (filter)->context)
|
2009-01-23 01:04:23 +00:00
|
|
|
return TRUE;
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
if ((fp = fopen (differencematte->location, "rb")) == NULL)
|
2009-01-23 01:04:23 +00:00
|
|
|
LOAD_ERROR ("file not found");
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
if (png_ptr == NULL) {
|
|
|
|
fclose (fp);
|
2009-01-23 01:04:23 +00:00
|
|
|
LOAD_ERROR ("failed to initialize the png_struct");
|
|
|
|
}
|
|
|
|
|
|
|
|
png_set_error_fn (png_ptr, NULL, NULL, user_warning_fn);
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
info_ptr = png_create_info_struct (png_ptr);
|
|
|
|
if (info_ptr == NULL) {
|
|
|
|
fclose (fp);
|
|
|
|
png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL);
|
2009-01-23 01:04:23 +00:00
|
|
|
LOAD_ERROR ("failed to initialize the memory for image information");
|
|
|
|
}
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_init_io (png_ptr, fp);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_set_sig_bytes (png_ptr, sig_read);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_read_info (png_ptr, info_ptr);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
|
|
|
|
&interlace_type, int_p_NULL, int_p_NULL);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2009-10-28 09:41:53 +00:00
|
|
|
if (color_type == PNG_COLOR_TYPE_RGB) {
|
|
|
|
filler = 0xff;
|
|
|
|
png_set_filler (png_ptr, filler, PNG_FILLER_AFTER);
|
|
|
|
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
|
|
|
|
}
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
if (color_type != PNG_COLOR_TYPE_RGB_ALPHA) {
|
|
|
|
fclose (fp);
|
|
|
|
png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL);
|
2009-01-23 01:04:23 +00:00
|
|
|
LOAD_ERROR ("color type is not rgb");
|
|
|
|
}
|
|
|
|
|
2009-10-28 09:41:53 +00:00
|
|
|
differencematte->pbuf_width = width;
|
|
|
|
differencematte->pbuf_height = height;
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2010-04-23 18:06:48 +00:00
|
|
|
differencematte->pixbuf =
|
|
|
|
(guchar *) malloc (sizeof (guchar) * width * height * 4);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
rows = (guchar **) malloc (sizeof (guchar *) * height);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
for (y = 0; y < height; ++y)
|
|
|
|
rows[y] = (guchar *) (differencematte->pixbuf + y * width * 4);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_read_image (png_ptr, rows);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
free (rows);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_read_end (png_ptr, info_ptr);
|
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, png_infopp_NULL);
|
|
|
|
fclose (fp);
|
2009-01-23 01:04:23 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|