2002-03-20 21:45:04 +00:00
|
|
|
/* GStreamer
|
2001-12-22 23:26:33 +00:00
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
2011-11-21 07:20:14 +00:00
|
|
|
* <2001> Wim Taymans <wim.taymans@chello.be>
|
|
|
|
* <2011> Stefan Sauer <ensonic@user.sf.net>
|
2001-12-22 23:26:33 +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-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2001-12-22 23:26:33 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-29 19:46:13 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2001-12-22 23:26:33 +00:00
|
|
|
#include <string.h>
|
2004-07-27 21:51:30 +00:00
|
|
|
#include "gstsmooth.h"
|
2003-07-06 20:49:52 +00:00
|
|
|
#include <gst/video/video.h>
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2011-11-21 07:20:14 +00:00
|
|
|
/* Smooth args */
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
enum
|
|
|
|
{
|
2011-11-21 07:20:14 +00:00
|
|
|
PROP_0,
|
|
|
|
PROP_ACTIVE,
|
|
|
|
PROP_TOLERANCE,
|
|
|
|
PROP_FILTER_SIZE,
|
|
|
|
PROP_LUMA_ONLY
|
2001-12-22 23:26:33 +00:00
|
|
|
};
|
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate gst_smooth_src_template =
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2012-03-18 17:32:51 +00:00
|
|
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420")
|
2004-03-14 22:34:33 +00:00
|
|
|
)
|
|
|
|
);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate gst_smooth_sink_template =
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2012-03-18 17:32:51 +00:00
|
|
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420")
|
2004-03-14 22:34:33 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2012-03-18 17:32:51 +00:00
|
|
|
static gboolean gst_smooth_set_info (GstVideoFilter * filter, GstCaps * incaps,
|
|
|
|
GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info);
|
|
|
|
static GstFlowReturn gst_smooth_transform_frame (GstVideoFilter * vfilter,
|
|
|
|
GstVideoFrame * in_frame, GstVideoFrame * out_frame);
|
2004-03-14 22:34:33 +00:00
|
|
|
|
|
|
|
static void gst_smooth_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_smooth_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2012-03-18 17:32:51 +00:00
|
|
|
G_DEFINE_TYPE (GstSmooth, gst_smooth, GST_TYPE_VIDEO_FILTER);
|
2021-02-25 14:22:15 +00:00
|
|
|
GST_ELEMENT_REGISTER_DEFINE (smooth, "smooth", GST_RANK_NONE, GST_TYPE_SMOOTH);
|
2003-11-02 22:23:26 +00:00
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_smooth_class_init (GstSmoothClass * klass)
|
2001-12-22 23:26:33 +00:00
|
|
|
{
|
2011-11-21 07:20:14 +00:00
|
|
|
GObjectClass *gobject_class = (GObjectClass *) klass;
|
2012-03-18 17:32:51 +00:00
|
|
|
GstElementClass *gstelement_class = (GstElementClass *) klass;
|
|
|
|
GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
gobject_class->set_property = gst_smooth_set_property;
|
|
|
|
gobject_class->get_property = gst_smooth_get_property;
|
|
|
|
|
2011-11-21 07:20:14 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ACTIVE,
|
|
|
|
g_param_spec_boolean ("active", "active", "process video", TRUE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TOLERANCE,
|
|
|
|
g_param_spec_int ("tolerance",
|
|
|
|
"tolerance", "contrast tolerance for smoothing", G_MININT,
|
|
|
|
G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FILTER_SIZE,
|
|
|
|
g_param_spec_int ("filter-size", "filter-size", "size of media filter",
|
|
|
|
G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LUMA_ONLY,
|
|
|
|
g_param_spec_boolean ("luma-only", "luma-only", "only filter luma part",
|
|
|
|
TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (gstelement_class,
|
|
|
|
&gst_smooth_sink_template);
|
|
|
|
gst_element_class_add_static_pad_template (gstelement_class,
|
|
|
|
&gst_smooth_src_template);
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_class, "Smooth effect",
|
2016-03-04 06:50:26 +00:00
|
|
|
"Filter/Effect/Video", "Apply a smooth filter to an image",
|
2012-03-18 17:32:51 +00:00
|
|
|
"Wim Taymans <wim.taymans@chello.be>");
|
|
|
|
|
|
|
|
vfilter_class->transform_frame =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_smooth_transform_frame);
|
|
|
|
vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_smooth_set_info);
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
2011-11-21 07:20:14 +00:00
|
|
|
static gboolean
|
2012-03-18 17:32:51 +00:00
|
|
|
gst_smooth_set_info (GstVideoFilter * filter, GstCaps * incaps,
|
|
|
|
GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
|
2001-12-22 23:26:33 +00:00
|
|
|
{
|
2012-03-18 17:32:51 +00:00
|
|
|
GstSmooth *smooth;
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2012-03-18 17:32:51 +00:00
|
|
|
smooth = GST_SMOOTH (filter);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2012-03-18 17:32:51 +00:00
|
|
|
smooth->width = GST_VIDEO_INFO_WIDTH (in_info);
|
|
|
|
smooth->height = GST_VIDEO_INFO_HEIGHT (in_info);
|
2002-01-13 22:27:25 +00:00
|
|
|
|
2012-03-18 17:32:51 +00:00
|
|
|
return TRUE;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-18 17:32:51 +00:00
|
|
|
gst_smooth_init (GstSmooth * smooth)
|
2001-12-22 23:26:33 +00:00
|
|
|
{
|
|
|
|
smooth->active = TRUE;
|
|
|
|
smooth->tolerance = 8;
|
|
|
|
smooth->filtersize = 3;
|
2011-11-21 07:20:14 +00:00
|
|
|
smooth->luma_only = TRUE;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-21 07:20:14 +00:00
|
|
|
smooth_filter (guchar * dest, guchar * src, gint width, gint height,
|
2012-03-18 17:32:51 +00:00
|
|
|
gint stride, gint dstride, gint tolerance, gint filtersize)
|
2001-12-22 23:26:33 +00:00
|
|
|
{
|
2011-11-21 07:20:14 +00:00
|
|
|
gint refval, aktval, upperval, lowerval, numvalues, sum;
|
|
|
|
gint x, y, fx, fy, fy1, fy2, fx1, fx2;
|
2012-03-18 17:32:51 +00:00
|
|
|
guchar *srcp = src, *destp = dest;
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
fy1 = 0;
|
2012-03-18 17:32:51 +00:00
|
|
|
fy2 = MIN (filtersize + 1, height) * stride;
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
for (y = 0; y < height; y++) {
|
|
|
|
if (y > (filtersize + 1))
|
2012-03-18 17:32:51 +00:00
|
|
|
fy1 += stride;
|
2004-03-14 22:34:33 +00:00
|
|
|
if (y < height - (filtersize + 1))
|
2012-03-18 17:32:51 +00:00
|
|
|
fy2 += stride;
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
for (x = 0; x < width; x++) {
|
|
|
|
refval = *src;
|
|
|
|
upperval = refval + tolerance;
|
|
|
|
lowerval = refval - tolerance;
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
numvalues = 1;
|
2004-03-14 22:34:33 +00:00
|
|
|
sum = refval;
|
|
|
|
|
|
|
|
fx1 = MAX (x - filtersize, 0) + fy1;
|
|
|
|
fx2 = MIN (x + filtersize + 1, width) + fy1;
|
|
|
|
|
2012-03-18 17:32:51 +00:00
|
|
|
for (fy = fy1; fy < fy2; fy += stride) {
|
2004-03-15 19:32:27 +00:00
|
|
|
for (fx = fx1; fx < fx2; fx++) {
|
|
|
|
aktval = srcp[fx];
|
|
|
|
if ((lowerval - aktval) * (upperval - aktval) < 0) {
|
|
|
|
numvalues++;
|
|
|
|
sum += aktval;
|
|
|
|
}
|
|
|
|
} /*for fx */
|
2012-03-18 17:32:51 +00:00
|
|
|
fx1 += stride;
|
|
|
|
fx2 += stride;
|
2004-03-15 19:32:27 +00:00
|
|
|
} /*for fy */
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
src++;
|
2004-03-14 22:34:33 +00:00
|
|
|
*dest++ = sum / numvalues;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
2012-03-18 17:32:51 +00:00
|
|
|
|
|
|
|
src = srcp + stride * y;
|
|
|
|
dest = destp + dstride * y;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-21 07:20:14 +00:00
|
|
|
static GstFlowReturn
|
2012-03-18 17:32:51 +00:00
|
|
|
gst_smooth_transform_frame (GstVideoFilter * vfilter, GstVideoFrame * in_frame,
|
|
|
|
GstVideoFrame * out_frame)
|
2001-12-22 23:26:33 +00:00
|
|
|
{
|
|
|
|
GstSmooth *smooth;
|
|
|
|
|
2012-03-18 17:32:51 +00:00
|
|
|
smooth = GST_SMOOTH (vfilter);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
if (!smooth->active) {
|
2012-03-18 17:32:51 +00:00
|
|
|
gst_video_frame_copy (out_frame, in_frame);
|
2011-11-21 07:20:14 +00:00
|
|
|
return GST_FLOW_OK;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
2012-03-18 17:32:51 +00:00
|
|
|
smooth_filter (GST_VIDEO_FRAME_COMP_DATA (out_frame, 0),
|
|
|
|
GST_VIDEO_FRAME_COMP_DATA (in_frame, 0),
|
|
|
|
GST_VIDEO_FRAME_COMP_WIDTH (in_frame, 0),
|
|
|
|
GST_VIDEO_FRAME_COMP_HEIGHT (in_frame, 0),
|
|
|
|
GST_VIDEO_FRAME_COMP_STRIDE (in_frame, 0),
|
|
|
|
GST_VIDEO_FRAME_COMP_STRIDE (out_frame, 0),
|
2004-03-14 22:34:33 +00:00
|
|
|
smooth->tolerance, smooth->filtersize);
|
2011-11-21 07:20:14 +00:00
|
|
|
if (!smooth->luma_only) {
|
2012-03-18 17:32:51 +00:00
|
|
|
smooth_filter (GST_VIDEO_FRAME_COMP_DATA (out_frame, 1),
|
|
|
|
GST_VIDEO_FRAME_COMP_DATA (in_frame, 1),
|
|
|
|
GST_VIDEO_FRAME_COMP_WIDTH (in_frame, 1),
|
|
|
|
GST_VIDEO_FRAME_COMP_HEIGHT (in_frame, 1),
|
|
|
|
GST_VIDEO_FRAME_COMP_STRIDE (in_frame, 1),
|
|
|
|
GST_VIDEO_FRAME_COMP_STRIDE (out_frame, 1),
|
|
|
|
smooth->tolerance, smooth->filtersize);
|
|
|
|
smooth_filter (GST_VIDEO_FRAME_COMP_DATA (out_frame, 2),
|
|
|
|
GST_VIDEO_FRAME_COMP_DATA (in_frame, 2),
|
|
|
|
GST_VIDEO_FRAME_COMP_WIDTH (in_frame, 2),
|
|
|
|
GST_VIDEO_FRAME_COMP_HEIGHT (in_frame, 2),
|
|
|
|
GST_VIDEO_FRAME_COMP_STRIDE (in_frame, 2),
|
|
|
|
GST_VIDEO_FRAME_COMP_STRIDE (out_frame, 2),
|
|
|
|
smooth->tolerance, smooth->filtersize);
|
2004-03-14 22:34:33 +00:00
|
|
|
} else {
|
2012-03-18 17:32:51 +00:00
|
|
|
gst_video_frame_copy_plane (out_frame, in_frame, 1);
|
|
|
|
gst_video_frame_copy_plane (out_frame, in_frame, 2);
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
2011-11-21 07:20:14 +00:00
|
|
|
return GST_FLOW_OK;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_smooth_set_property (GObject * object, guint prop_id, const GValue * value,
|
|
|
|
GParamSpec * pspec)
|
2001-12-22 23:26:33 +00:00
|
|
|
{
|
|
|
|
GstSmooth *smooth;
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
g_return_if_fail (GST_IS_SMOOTH (object));
|
|
|
|
smooth = GST_SMOOTH (object);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
2011-11-21 07:20:14 +00:00
|
|
|
case PROP_ACTIVE:
|
2001-12-22 23:26:33 +00:00
|
|
|
smooth->active = g_value_get_boolean (value);
|
|
|
|
break;
|
2011-11-21 07:20:14 +00:00
|
|
|
case PROP_TOLERANCE:
|
2001-12-22 23:26:33 +00:00
|
|
|
smooth->tolerance = g_value_get_int (value);
|
|
|
|
break;
|
2011-11-21 07:20:14 +00:00
|
|
|
case PROP_FILTER_SIZE:
|
2001-12-22 23:26:33 +00:00
|
|
|
smooth->filtersize = g_value_get_int (value);
|
|
|
|
break;
|
2011-11-21 07:20:14 +00:00
|
|
|
case PROP_LUMA_ONLY:
|
|
|
|
smooth->luma_only = g_value_get_boolean (value);
|
2001-12-22 23:26:33 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-11-21 07:20:14 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2001-12-22 23:26:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_smooth_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
2001-12-22 23:26:33 +00:00
|
|
|
{
|
|
|
|
GstSmooth *smooth;
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
g_return_if_fail (GST_IS_SMOOTH (object));
|
|
|
|
smooth = GST_SMOOTH (object);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
2011-11-21 07:20:14 +00:00
|
|
|
case PROP_ACTIVE:
|
2001-12-22 23:26:33 +00:00
|
|
|
g_value_set_boolean (value, smooth->active);
|
|
|
|
break;
|
2011-11-21 07:20:14 +00:00
|
|
|
case PROP_TOLERANCE:
|
2001-12-22 23:26:33 +00:00
|
|
|
g_value_set_int (value, smooth->tolerance);
|
|
|
|
break;
|
2011-11-21 07:20:14 +00:00
|
|
|
case PROP_FILTER_SIZE:
|
2001-12-22 23:26:33 +00:00
|
|
|
g_value_set_int (value, smooth->filtersize);
|
|
|
|
break;
|
2011-11-21 07:20:14 +00:00
|
|
|
case PROP_LUMA_ONLY:
|
|
|
|
g_value_set_boolean (value, smooth->luma_only);
|
2001-12-22 23:26:33 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2004-03-14 22:34:33 +00:00
|
|
|
plugin_init (GstPlugin * plugin)
|
2001-12-22 23:26:33 +00:00
|
|
|
{
|
2021-02-25 14:22:15 +00:00
|
|
|
return GST_ELEMENT_REGISTER (smooth, plugin);
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
2012-04-05 16:02:56 +00:00
|
|
|
smooth,
|
2004-03-14 22:34:33 +00:00
|
|
|
"Apply a smooth filter to an image",
|
2006-04-01 10:09:11 +00:00
|
|
|
plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
|