1 /* GStreamer
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>
5 *
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.
10 *
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.
15 *
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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <string.h>
26 #include "gstsmooth.h"
27 #include <gst/video/video.h>
28
29 /* Smooth args */
30
31 enum
32 {
33 PROP_0,
34 PROP_ACTIVE,
35 PROP_TOLERANCE,
36 PROP_FILTER_SIZE,
37 PROP_LUMA_ONLY
38 };
39
40 static GstStaticPadTemplate gst_smooth_src_template =
41 GST_STATIC_PAD_TEMPLATE ("src",
42 GST_PAD_SRC,
43 GST_PAD_ALWAYS,
44 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420")
45 )
46 );
47
48 static GstStaticPadTemplate gst_smooth_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
50 GST_PAD_SINK,
51 GST_PAD_ALWAYS,
52 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420")
53 )
54 );
55
56 static gboolean gst_smooth_set_info (GstVideoFilter * filter, GstCaps * incaps,
57 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info);
58 static GstFlowReturn gst_smooth_transform_frame (GstVideoFilter * vfilter,
59 GstVideoFrame * in_frame, GstVideoFrame * out_frame);
60
61 static void gst_smooth_set_property (GObject * object, guint prop_id,
62 const GValue * value, GParamSpec * pspec);
63 static void gst_smooth_get_property (GObject * object, guint prop_id,
64 GValue * value, GParamSpec * pspec);
65
66 G_DEFINE_TYPE (GstSmooth, gst_smooth, GST_TYPE_VIDEO_FILTER);
67 GST_ELEMENT_REGISTER_DEFINE (smooth, "smooth", GST_RANK_NONE, GST_TYPE_SMOOTH);
68
69 static void
gst_smooth_class_init(GstSmoothClass * klass)70 gst_smooth_class_init (GstSmoothClass * klass)
71 {
72 GObjectClass *gobject_class = (GObjectClass *) klass;
73 GstElementClass *gstelement_class = (GstElementClass *) klass;
74 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
75
76 gobject_class->set_property = gst_smooth_set_property;
77 gobject_class->get_property = gst_smooth_get_property;
78
79 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ACTIVE,
80 g_param_spec_boolean ("active", "active", "process video", TRUE,
81 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
82 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TOLERANCE,
83 g_param_spec_int ("tolerance",
84 "tolerance", "contrast tolerance for smoothing", G_MININT,
85 G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
86 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FILTER_SIZE,
87 g_param_spec_int ("filter-size", "filter-size", "size of media filter",
88 G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
89 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LUMA_ONLY,
90 g_param_spec_boolean ("luma-only", "luma-only", "only filter luma part",
91 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
92
93 gst_element_class_add_static_pad_template (gstelement_class,
94 &gst_smooth_sink_template);
95 gst_element_class_add_static_pad_template (gstelement_class,
96 &gst_smooth_src_template);
97 gst_element_class_set_static_metadata (gstelement_class, "Smooth effect",
98 "Filter/Effect/Video", "Apply a smooth filter to an image",
99 "Wim Taymans <wim.taymans@chello.be>");
100
101 vfilter_class->transform_frame =
102 GST_DEBUG_FUNCPTR (gst_smooth_transform_frame);
103 vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_smooth_set_info);
104 }
105
106 static gboolean
gst_smooth_set_info(GstVideoFilter * filter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)107 gst_smooth_set_info (GstVideoFilter * filter, GstCaps * incaps,
108 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
109 {
110 GstSmooth *smooth;
111
112 smooth = GST_SMOOTH (filter);
113
114 smooth->width = GST_VIDEO_INFO_WIDTH (in_info);
115 smooth->height = GST_VIDEO_INFO_HEIGHT (in_info);
116
117 return TRUE;
118 }
119
120 static void
gst_smooth_init(GstSmooth * smooth)121 gst_smooth_init (GstSmooth * smooth)
122 {
123 smooth->active = TRUE;
124 smooth->tolerance = 8;
125 smooth->filtersize = 3;
126 smooth->luma_only = TRUE;
127 }
128
129 static void
smooth_filter(guchar * dest,guchar * src,gint width,gint height,gint stride,gint dstride,gint tolerance,gint filtersize)130 smooth_filter (guchar * dest, guchar * src, gint width, gint height,
131 gint stride, gint dstride, gint tolerance, gint filtersize)
132 {
133 gint refval, aktval, upperval, lowerval, numvalues, sum;
134 gint x, y, fx, fy, fy1, fy2, fx1, fx2;
135 guchar *srcp = src, *destp = dest;
136
137 fy1 = 0;
138 fy2 = MIN (filtersize + 1, height) * stride;
139
140 for (y = 0; y < height; y++) {
141 if (y > (filtersize + 1))
142 fy1 += stride;
143 if (y < height - (filtersize + 1))
144 fy2 += stride;
145
146 for (x = 0; x < width; x++) {
147 refval = *src;
148 upperval = refval + tolerance;
149 lowerval = refval - tolerance;
150
151 numvalues = 1;
152 sum = refval;
153
154 fx1 = MAX (x - filtersize, 0) + fy1;
155 fx2 = MIN (x + filtersize + 1, width) + fy1;
156
157 for (fy = fy1; fy < fy2; fy += stride) {
158 for (fx = fx1; fx < fx2; fx++) {
159 aktval = srcp[fx];
160 if ((lowerval - aktval) * (upperval - aktval) < 0) {
161 numvalues++;
162 sum += aktval;
163 }
164 } /*for fx */
165 fx1 += stride;
166 fx2 += stride;
167 } /*for fy */
168
169 src++;
170 *dest++ = sum / numvalues;
171 }
172
173 src = srcp + stride * y;
174 dest = destp + dstride * y;
175 }
176 }
177
178 static GstFlowReturn
gst_smooth_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)179 gst_smooth_transform_frame (GstVideoFilter * vfilter, GstVideoFrame * in_frame,
180 GstVideoFrame * out_frame)
181 {
182 GstSmooth *smooth;
183
184 smooth = GST_SMOOTH (vfilter);
185
186 if (!smooth->active) {
187 gst_video_frame_copy (out_frame, in_frame);
188 return GST_FLOW_OK;
189 }
190
191 smooth_filter (GST_VIDEO_FRAME_COMP_DATA (out_frame, 0),
192 GST_VIDEO_FRAME_COMP_DATA (in_frame, 0),
193 GST_VIDEO_FRAME_COMP_WIDTH (in_frame, 0),
194 GST_VIDEO_FRAME_COMP_HEIGHT (in_frame, 0),
195 GST_VIDEO_FRAME_COMP_STRIDE (in_frame, 0),
196 GST_VIDEO_FRAME_COMP_STRIDE (out_frame, 0),
197 smooth->tolerance, smooth->filtersize);
198 if (!smooth->luma_only) {
199 smooth_filter (GST_VIDEO_FRAME_COMP_DATA (out_frame, 1),
200 GST_VIDEO_FRAME_COMP_DATA (in_frame, 1),
201 GST_VIDEO_FRAME_COMP_WIDTH (in_frame, 1),
202 GST_VIDEO_FRAME_COMP_HEIGHT (in_frame, 1),
203 GST_VIDEO_FRAME_COMP_STRIDE (in_frame, 1),
204 GST_VIDEO_FRAME_COMP_STRIDE (out_frame, 1),
205 smooth->tolerance, smooth->filtersize);
206 smooth_filter (GST_VIDEO_FRAME_COMP_DATA (out_frame, 2),
207 GST_VIDEO_FRAME_COMP_DATA (in_frame, 2),
208 GST_VIDEO_FRAME_COMP_WIDTH (in_frame, 2),
209 GST_VIDEO_FRAME_COMP_HEIGHT (in_frame, 2),
210 GST_VIDEO_FRAME_COMP_STRIDE (in_frame, 2),
211 GST_VIDEO_FRAME_COMP_STRIDE (out_frame, 2),
212 smooth->tolerance, smooth->filtersize);
213 } else {
214 gst_video_frame_copy_plane (out_frame, in_frame, 1);
215 gst_video_frame_copy_plane (out_frame, in_frame, 2);
216 }
217
218 return GST_FLOW_OK;
219 }
220
221 static void
gst_smooth_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)222 gst_smooth_set_property (GObject * object, guint prop_id, const GValue * value,
223 GParamSpec * pspec)
224 {
225 GstSmooth *smooth;
226
227 g_return_if_fail (GST_IS_SMOOTH (object));
228 smooth = GST_SMOOTH (object);
229
230 switch (prop_id) {
231 case PROP_ACTIVE:
232 smooth->active = g_value_get_boolean (value);
233 break;
234 case PROP_TOLERANCE:
235 smooth->tolerance = g_value_get_int (value);
236 break;
237 case PROP_FILTER_SIZE:
238 smooth->filtersize = g_value_get_int (value);
239 break;
240 case PROP_LUMA_ONLY:
241 smooth->luma_only = g_value_get_boolean (value);
242 break;
243 default:
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245 break;
246 }
247 }
248
249 static void
gst_smooth_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)250 gst_smooth_get_property (GObject * object, guint prop_id, GValue * value,
251 GParamSpec * pspec)
252 {
253 GstSmooth *smooth;
254
255 g_return_if_fail (GST_IS_SMOOTH (object));
256 smooth = GST_SMOOTH (object);
257
258 switch (prop_id) {
259 case PROP_ACTIVE:
260 g_value_set_boolean (value, smooth->active);
261 break;
262 case PROP_TOLERANCE:
263 g_value_set_int (value, smooth->tolerance);
264 break;
265 case PROP_FILTER_SIZE:
266 g_value_set_int (value, smooth->filtersize);
267 break;
268 case PROP_LUMA_ONLY:
269 g_value_set_boolean (value, smooth->luma_only);
270 break;
271 default:
272 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273 break;
274 }
275 }
276
277
278 static gboolean
plugin_init(GstPlugin * plugin)279 plugin_init (GstPlugin * plugin)
280 {
281 return GST_ELEMENT_REGISTER (smooth, plugin);
282 }
283
284 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
285 GST_VERSION_MINOR,
286 smooth,
287 "Apply a smooth filter to an image",
288 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
289