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 
68 static void
gst_smooth_class_init(GstSmoothClass * klass)69 gst_smooth_class_init (GstSmoothClass * klass)
70 {
71   GObjectClass *gobject_class = (GObjectClass *) klass;
72   GstElementClass *gstelement_class = (GstElementClass *) klass;
73   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
74 
75   gobject_class->set_property = gst_smooth_set_property;
76   gobject_class->get_property = gst_smooth_get_property;
77 
78   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ACTIVE,
79       g_param_spec_boolean ("active", "active", "process video", TRUE,
80           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
81   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TOLERANCE,
82       g_param_spec_int ("tolerance",
83           "tolerance", "contrast tolerance for smoothing", G_MININT,
84           G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
85   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FILTER_SIZE,
86       g_param_spec_int ("filter-size", "filter-size", "size of media filter",
87           G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
88   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LUMA_ONLY,
89       g_param_spec_boolean ("luma-only", "luma-only", "only filter luma part",
90           TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
91 
92   gst_element_class_add_static_pad_template (gstelement_class,
93       &gst_smooth_sink_template);
94   gst_element_class_add_static_pad_template (gstelement_class,
95       &gst_smooth_src_template);
96   gst_element_class_set_static_metadata (gstelement_class, "Smooth effect",
97       "Filter/Effect/Video", "Apply a smooth filter to an image",
98       "Wim Taymans <wim.taymans@chello.be>");
99 
100   vfilter_class->transform_frame =
101       GST_DEBUG_FUNCPTR (gst_smooth_transform_frame);
102   vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_smooth_set_info);
103 }
104 
105 static gboolean
gst_smooth_set_info(GstVideoFilter * filter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)106 gst_smooth_set_info (GstVideoFilter * filter, GstCaps * incaps,
107     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
108 {
109   GstSmooth *smooth;
110 
111   smooth = GST_SMOOTH (filter);
112 
113   smooth->width = GST_VIDEO_INFO_WIDTH (in_info);
114   smooth->height = GST_VIDEO_INFO_HEIGHT (in_info);
115 
116   return TRUE;
117 }
118 
119 static void
gst_smooth_init(GstSmooth * smooth)120 gst_smooth_init (GstSmooth * smooth)
121 {
122   smooth->active = TRUE;
123   smooth->tolerance = 8;
124   smooth->filtersize = 3;
125   smooth->luma_only = TRUE;
126 }
127 
128 static void
smooth_filter(guchar * dest,guchar * src,gint width,gint height,gint stride,gint dstride,gint tolerance,gint filtersize)129 smooth_filter (guchar * dest, guchar * src, gint width, gint height,
130     gint stride, gint dstride, gint tolerance, gint filtersize)
131 {
132   gint refval, aktval, upperval, lowerval, numvalues, sum;
133   gint x, y, fx, fy, fy1, fy2, fx1, fx2;
134   guchar *srcp = src, *destp = dest;
135 
136   fy1 = 0;
137   fy2 = MIN (filtersize + 1, height) * stride;
138 
139   for (y = 0; y < height; y++) {
140     if (y > (filtersize + 1))
141       fy1 += stride;
142     if (y < height - (filtersize + 1))
143       fy2 += stride;
144 
145     for (x = 0; x < width; x++) {
146       refval = *src;
147       upperval = refval + tolerance;
148       lowerval = refval - tolerance;
149 
150       numvalues = 1;
151       sum = refval;
152 
153       fx1 = MAX (x - filtersize, 0) + fy1;
154       fx2 = MIN (x + filtersize + 1, width) + fy1;
155 
156       for (fy = fy1; fy < fy2; fy += stride) {
157         for (fx = fx1; fx < fx2; fx++) {
158           aktval = srcp[fx];
159           if ((lowerval - aktval) * (upperval - aktval) < 0) {
160             numvalues++;
161             sum += aktval;
162           }
163         }                       /*for fx */
164         fx1 += stride;
165         fx2 += stride;
166       }                         /*for fy */
167 
168       src++;
169       *dest++ = sum / numvalues;
170     }
171 
172     src = srcp + stride * y;
173     dest = destp + dstride * y;
174   }
175 }
176 
177 static GstFlowReturn
gst_smooth_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)178 gst_smooth_transform_frame (GstVideoFilter * vfilter, GstVideoFrame * in_frame,
179     GstVideoFrame * out_frame)
180 {
181   GstSmooth *smooth;
182 
183   smooth = GST_SMOOTH (vfilter);
184 
185   if (!smooth->active) {
186     gst_video_frame_copy (out_frame, in_frame);
187     return GST_FLOW_OK;
188   }
189 
190   smooth_filter (GST_VIDEO_FRAME_COMP_DATA (out_frame, 0),
191       GST_VIDEO_FRAME_COMP_DATA (in_frame, 0),
192       GST_VIDEO_FRAME_COMP_WIDTH (in_frame, 0),
193       GST_VIDEO_FRAME_COMP_HEIGHT (in_frame, 0),
194       GST_VIDEO_FRAME_COMP_STRIDE (in_frame, 0),
195       GST_VIDEO_FRAME_COMP_STRIDE (out_frame, 0),
196       smooth->tolerance, smooth->filtersize);
197   if (!smooth->luma_only) {
198     smooth_filter (GST_VIDEO_FRAME_COMP_DATA (out_frame, 1),
199         GST_VIDEO_FRAME_COMP_DATA (in_frame, 1),
200         GST_VIDEO_FRAME_COMP_WIDTH (in_frame, 1),
201         GST_VIDEO_FRAME_COMP_HEIGHT (in_frame, 1),
202         GST_VIDEO_FRAME_COMP_STRIDE (in_frame, 1),
203         GST_VIDEO_FRAME_COMP_STRIDE (out_frame, 1),
204         smooth->tolerance, smooth->filtersize);
205     smooth_filter (GST_VIDEO_FRAME_COMP_DATA (out_frame, 2),
206         GST_VIDEO_FRAME_COMP_DATA (in_frame, 2),
207         GST_VIDEO_FRAME_COMP_WIDTH (in_frame, 2),
208         GST_VIDEO_FRAME_COMP_HEIGHT (in_frame, 2),
209         GST_VIDEO_FRAME_COMP_STRIDE (in_frame, 2),
210         GST_VIDEO_FRAME_COMP_STRIDE (out_frame, 2),
211         smooth->tolerance, smooth->filtersize);
212   } else {
213     gst_video_frame_copy_plane (out_frame, in_frame, 1);
214     gst_video_frame_copy_plane (out_frame, in_frame, 2);
215   }
216 
217   return GST_FLOW_OK;
218 }
219 
220 static void
gst_smooth_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)221 gst_smooth_set_property (GObject * object, guint prop_id, const GValue * value,
222     GParamSpec * pspec)
223 {
224   GstSmooth *smooth;
225 
226   g_return_if_fail (GST_IS_SMOOTH (object));
227   smooth = GST_SMOOTH (object);
228 
229   switch (prop_id) {
230     case PROP_ACTIVE:
231       smooth->active = g_value_get_boolean (value);
232       break;
233     case PROP_TOLERANCE:
234       smooth->tolerance = g_value_get_int (value);
235       break;
236     case PROP_FILTER_SIZE:
237       smooth->filtersize = g_value_get_int (value);
238       break;
239     case PROP_LUMA_ONLY:
240       smooth->luma_only = g_value_get_boolean (value);
241       break;
242     default:
243       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244       break;
245   }
246 }
247 
248 static void
gst_smooth_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)249 gst_smooth_get_property (GObject * object, guint prop_id, GValue * value,
250     GParamSpec * pspec)
251 {
252   GstSmooth *smooth;
253 
254   g_return_if_fail (GST_IS_SMOOTH (object));
255   smooth = GST_SMOOTH (object);
256 
257   switch (prop_id) {
258     case PROP_ACTIVE:
259       g_value_set_boolean (value, smooth->active);
260       break;
261     case PROP_TOLERANCE:
262       g_value_set_int (value, smooth->tolerance);
263       break;
264     case PROP_FILTER_SIZE:
265       g_value_set_int (value, smooth->filtersize);
266       break;
267     case PROP_LUMA_ONLY:
268       g_value_set_boolean (value, smooth->luma_only);
269       break;
270     default:
271       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272       break;
273   }
274 }
275 
276 
277 static gboolean
plugin_init(GstPlugin * plugin)278 plugin_init (GstPlugin * plugin)
279 {
280   return gst_element_register (plugin, "smooth",
281       GST_RANK_NONE, GST_TYPE_SMOOTH);
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