2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * <2001> Wim Taymans <wim.taymans@chello.be>
4 * <2011> Stefan Sauer <ensonic@user.sf.net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #include "gstsmooth.h"
27 #include <gst/video/video.h>
40 static GstStaticPadTemplate gst_smooth_src_template =
41 GST_STATIC_PAD_TEMPLATE ("src",
44 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420")
48 static GstStaticPadTemplate gst_smooth_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
52 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420")
57 static gboolean gst_smooth_set_caps (GstBaseTransform * btrans,
58 GstCaps * incaps, GstCaps * outcaps);
60 static GstFlowReturn gst_smooth_transform (GstBaseTransform * btrans,
61 GstBuffer * inbuf, GstBuffer * outbuf);
62 static void smooth_filter (unsigned char *dest, unsigned char *src,
63 int width, int height, int tolerance, int filtersize);
65 static void gst_smooth_set_property (GObject * object, guint prop_id,
66 const GValue * value, GParamSpec * pspec);
67 static void gst_smooth_get_property (GObject * object, guint prop_id,
68 GValue * value, GParamSpec * pspec);
70 GST_BOILERPLATE (GstSmooth, gst_smooth, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
73 gst_smooth_base_init (gpointer g_class)
75 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
77 gst_element_class_add_static_pad_template (element_class,
78 &gst_smooth_sink_template);
79 gst_element_class_add_static_pad_template (element_class,
80 &gst_smooth_src_template);
81 gst_element_class_set_details_simple (element_class, "Smooth effect",
82 "Filter/Effect/Video",
83 "Apply a smooth filter to an image",
84 "Wim Taymans <wim.taymans@chello.be>");
88 gst_smooth_class_init (GstSmoothClass * klass)
90 GObjectClass *gobject_class = (GObjectClass *) klass;
91 GstBaseTransformClass *btrans_class = (GstBaseTransformClass *) klass;
93 gobject_class->set_property = gst_smooth_set_property;
94 gobject_class->get_property = gst_smooth_get_property;
96 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ACTIVE,
97 g_param_spec_boolean ("active", "active", "process video", TRUE,
98 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
99 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TOLERANCE,
100 g_param_spec_int ("tolerance",
101 "tolerance", "contrast tolerance for smoothing", G_MININT,
102 G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
103 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FILTER_SIZE,
104 g_param_spec_int ("filter-size", "filter-size", "size of media filter",
105 G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
106 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LUMA_ONLY,
107 g_param_spec_boolean ("luma-only", "luma-only", "only filter luma part",
108 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
110 btrans_class->transform = GST_DEBUG_FUNCPTR (gst_smooth_transform);
111 btrans_class->set_caps = GST_DEBUG_FUNCPTR (gst_smooth_set_caps);
115 gst_smooth_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
119 GstStructure *structure;
122 filter = GST_SMOOTH (btrans);
124 structure = gst_caps_get_structure (incaps, 0);
125 ret = gst_structure_get_int (structure, "width", &filter->width);
126 ret &= gst_structure_get_int (structure, "height", &filter->height);
132 gst_smooth_init (GstSmooth * smooth, GstSmoothClass * klass)
134 smooth->active = TRUE;
135 smooth->tolerance = 8;
136 smooth->filtersize = 3;
137 smooth->luma_only = TRUE;
141 smooth_filter (guchar * dest, guchar * src, gint width, gint height,
142 gint tolerance, gint filtersize)
144 gint refval, aktval, upperval, lowerval, numvalues, sum;
145 gint x, y, fx, fy, fy1, fy2, fx1, fx2;
149 fy2 = MIN (filtersize + 1, height) * width;
151 for (y = 0; y < height; y++) {
152 if (y > (filtersize + 1))
154 if (y < height - (filtersize + 1))
157 for (x = 0; x < width; x++) {
159 upperval = refval + tolerance;
160 lowerval = refval - tolerance;
165 fx1 = MAX (x - filtersize, 0) + fy1;
166 fx2 = MIN (x + filtersize + 1, width) + fy1;
168 for (fy = fy1; fy < fy2; fy += width) {
169 for (fx = fx1; fx < fx2; fx++) {
171 if ((lowerval - aktval) * (upperval - aktval) < 0) {
181 *dest++ = sum / numvalues;
187 gst_smooth_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
191 guint8 *idata, *odata;
192 guint size, lumsize, chromsize;
194 smooth = GST_SMOOTH (btrans);
195 idata = GST_BUFFER_DATA (inbuf);
196 odata = GST_BUFFER_DATA (outbuf);
197 size = GST_BUFFER_SIZE (inbuf);
199 if (!smooth->active) {
200 memcpy (odata, idata, size);
204 GST_DEBUG_OBJECT (smooth, "smooth: have buffer of %d", size);
206 lumsize = smooth->width * smooth->height;
207 chromsize = lumsize / 4;
209 smooth_filter (odata, idata, smooth->width, smooth->height,
210 smooth->tolerance, smooth->filtersize);
211 if (!smooth->luma_only) {
212 smooth_filter (odata + lumsize, idata + lumsize,
213 smooth->width / 2, smooth->height / 2, smooth->tolerance,
214 smooth->filtersize / 2);
215 smooth_filter (odata + lumsize + chromsize,
216 idata + lumsize + chromsize, smooth->width / 2, smooth->height / 2,
217 smooth->tolerance, smooth->filtersize / 2);
219 memcpy (odata + lumsize, idata + lumsize, chromsize * 2);
226 gst_smooth_set_property (GObject * object, guint prop_id, const GValue * value,
231 g_return_if_fail (GST_IS_SMOOTH (object));
232 smooth = GST_SMOOTH (object);
236 smooth->active = g_value_get_boolean (value);
239 smooth->tolerance = g_value_get_int (value);
241 case PROP_FILTER_SIZE:
242 smooth->filtersize = g_value_get_int (value);
245 smooth->luma_only = g_value_get_boolean (value);
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254 gst_smooth_get_property (GObject * object, guint prop_id, GValue * value,
259 g_return_if_fail (GST_IS_SMOOTH (object));
260 smooth = GST_SMOOTH (object);
264 g_value_set_boolean (value, smooth->active);
267 g_value_set_int (value, smooth->tolerance);
269 case PROP_FILTER_SIZE:
270 g_value_set_int (value, smooth->filtersize);
273 g_value_set_boolean (value, smooth->luma_only);
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
283 plugin_init (GstPlugin * plugin)
285 return gst_element_register (plugin, "smooth",
286 GST_RANK_NONE, GST_TYPE_SMOOTH);
289 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
292 "Apply a smooth filter to an image",
293 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)