2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <gstmedian.h>
25 #include <gst/video/video.h>
27 /* elementfactory information */
28 static GstElementDetails median_details = {
30 "Filter/Effect/Video",
31 "Apply a median filter to an image",
32 "Wim Taymans <wim.taymans@chello.be>"
35 static GstStaticPadTemplate median_src_factory =
36 GST_STATIC_PAD_TEMPLATE (
40 GST_STATIC_CAPS (GST_VIDEO_YUV_PAD_TEMPLATE_CAPS ("I420"))
43 static GstStaticPadTemplate median_sink_factory =
44 GST_STATIC_PAD_TEMPLATE (
48 GST_STATIC_CAPS (GST_VIDEO_YUV_PAD_TEMPLATE_CAPS ("I420"))
52 /* Median signals and args */
65 static GType gst_median_get_type (void);
66 static void gst_median_class_init (GstMedianClass *klass);
67 static void gst_median_base_init (GstMedianClass *klass);
68 static void gst_median_init (GstMedian *median);
70 static void median_5 (unsigned char *src, unsigned char *dest, int height, int width);
71 static void median_9 (unsigned char *src, unsigned char *dest, int height, int width);
72 static void gst_median_chain (GstPad *pad, GstData *_data);
74 static void gst_median_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
75 static void gst_median_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
77 static GstElementClass *parent_class = NULL;
78 /*static guint gst_median_signals[LAST_SIGNAL] = { 0 }; */
81 gst_median_get_type (void)
83 static GType median_type = 0;
86 static const GTypeInfo median_info = {
87 sizeof(GstMedianClass),
88 (GBaseInitFunc)gst_median_base_init,
90 (GClassInitFunc)gst_median_class_init,
95 (GInstanceInitFunc)gst_median_init,
97 median_type = g_type_register_static(GST_TYPE_ELEMENT, "GstMedian", &median_info, 0);
103 gst_median_base_init (GstMedianClass *klass)
105 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
107 gst_element_class_add_pad_template (element_class,
108 gst_static_pad_template_get (&median_sink_factory));
109 gst_element_class_add_pad_template (element_class,
110 gst_static_pad_template_get (&median_src_factory));
111 gst_element_class_set_details (element_class, &median_details);
115 gst_median_class_init (GstMedianClass *klass)
117 GObjectClass *gobject_class;
118 GstElementClass *gstelement_class;
120 gobject_class = (GObjectClass*)klass;
121 gstelement_class = (GstElementClass*)klass;
123 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
125 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_ACTIVE,
126 g_param_spec_boolean("active","active","active",
127 TRUE,G_PARAM_READWRITE)); /* CHECKME */
128 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FILTERSIZE,
129 g_param_spec_int("filtersize","filtersize","filtersize",
130 G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
131 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LUM_ONLY,
132 g_param_spec_boolean("lum_only","lum_only","lum_only",
133 TRUE,G_PARAM_READWRITE)); /* CHECKME */
135 gobject_class->set_property = gst_median_set_property;
136 gobject_class->get_property = gst_median_get_property;
140 gst_median_link (GstPad *pad, const GstCaps *caps)
142 GstMedian *filter = GST_MEDIAN (gst_pad_get_parent (pad));
143 GstPad *otherpad = (pad == filter->srcpad) ? filter->sinkpad : filter->srcpad;
144 GstStructure *structure = gst_caps_get_structure (caps, 0);
146 GstPadLinkReturn ret;
148 gst_structure_get_int (structure, "width", &w);
149 gst_structure_get_int (structure, "height", &h);
151 ret = gst_pad_try_set_caps (otherpad, caps);
152 if (GST_PAD_LINK_SUCCESSFUL (ret)) {
160 void gst_median_init (GstMedian *median)
162 median->sinkpad = gst_pad_new_from_template (
163 gst_static_pad_template_get (&median_sink_factory), "sink");
164 gst_pad_set_getcaps_function (median->sinkpad, gst_pad_proxy_getcaps);
165 gst_pad_set_link_function (median->sinkpad, gst_median_link);
166 gst_pad_set_chain_function (median->sinkpad, gst_median_chain);
167 gst_element_add_pad (GST_ELEMENT (median), median->sinkpad);
169 median->srcpad = gst_pad_new_from_template (
170 gst_static_pad_template_get (&median_src_factory), "src");
171 gst_pad_set_getcaps_function (median->srcpad, gst_pad_proxy_getcaps);
172 gst_pad_set_link_function (median->sinkpad, gst_median_link);
173 gst_element_add_pad (GST_ELEMENT (median), median->srcpad);
175 median->filtersize = 5;
176 median->lum_only = TRUE;
177 median->active = TRUE;
180 #define PIX_SORT(a,b) { if ((a)>(b)) PIX_SWAP((a),(b)); }
181 #define PIX_SWAP(a,b) { unsigned char temp=(a);(a)=(b);(b)=temp; }
184 median_5 (unsigned char *src, unsigned char *dest, int width, int height)
191 nLastCol = width - 1;
192 nLastRow = height - 1;
194 /*copy the top and bottom rows into the result array */
195 for (i=0; i<width; i++) {
197 dest[nLastRow * width + i] = src[nLastRow * width + i];
204 /* process the interior pixels */
206 for (k=0; k < nLastRow; k++) {
207 for (j=0; j < nLastCol; j++, i++) {
213 PIX_SORT(p[0],p[1]) ; PIX_SORT(p[3],p[4]) ; PIX_SORT(p[0],p[3]) ;
214 PIX_SORT(p[1],p[4]) ; PIX_SORT(p[1],p[2]) ; PIX_SORT(p[2],p[3]) ;
215 PIX_SORT(p[1],p[2]) ;
228 median_9 (unsigned char *src, unsigned char *dest, int width, int height)
235 nLastCol = width - 1;
236 nLastRow = height - 1;
238 /*copy the top and bottom rows into the result array */
239 for (i=0; i<width; i++) {
241 dest[nLastRow * width + i] = src[nLastRow * width + i];
248 /* process the interior pixels */
250 for (k=0; k < nLastRow; k++) {
251 for (j=0; j < nLastCol; j++, i++) {
252 p[0] = src[i-width-1];
254 p[2] = src[i-width+1];
258 p[6] = src[i+width-1];
260 p[8] = src[i+width+1];
261 PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
262 PIX_SORT(p[0], p[1]) ; PIX_SORT(p[3], p[4]) ; PIX_SORT(p[6], p[7]) ;
263 PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
264 PIX_SORT(p[0], p[3]) ; PIX_SORT(p[5], p[8]) ; PIX_SORT(p[4], p[7]) ;
265 PIX_SORT(p[3], p[6]) ; PIX_SORT(p[1], p[4]) ; PIX_SORT(p[2], p[5]) ;
266 PIX_SORT(p[4], p[7]) ; PIX_SORT(p[4], p[2]) ; PIX_SORT(p[6], p[4]) ;
267 PIX_SORT(p[4], p[2]) ;
280 gst_median_chain (GstPad *pad, GstData *_data)
282 GstBuffer *buf = GST_BUFFER (_data);
288 int lumsize, chromsize;
290 g_return_if_fail(pad != NULL);
291 g_return_if_fail(GST_IS_PAD(pad));
292 g_return_if_fail(buf != NULL);
294 median = GST_MEDIAN (GST_OBJECT_PARENT (pad));
296 if (!median->active) {
297 gst_pad_push(median->srcpad,GST_DATA (buf));
301 data = GST_BUFFER_DATA(buf);
302 size = GST_BUFFER_SIZE(buf);
304 GST_DEBUG ("median: have buffer of %d", GST_BUFFER_SIZE(buf));
306 outbuf = gst_buffer_new();
307 GST_BUFFER_DATA(outbuf) = g_malloc(GST_BUFFER_SIZE(buf));
308 GST_BUFFER_SIZE(outbuf) = GST_BUFFER_SIZE(buf);
310 lumsize = median->width * median->height;
311 chromsize = lumsize/4;
313 if (median->filtersize == 5) {
314 median_5(data, GST_BUFFER_DATA(outbuf), median->width, median->height);
315 if (!median->lum_only) {
316 median_5(data+lumsize, GST_BUFFER_DATA(outbuf)+lumsize, median->width/2, median->height/2);
317 median_5(data+lumsize+chromsize, GST_BUFFER_DATA(outbuf)+lumsize+chromsize, median->width/2, median->height/2);
320 memcpy (GST_BUFFER_DATA (outbuf)+lumsize, data+lumsize, chromsize*2);
324 median_9(data, GST_BUFFER_DATA(outbuf), median->width, median->height);
325 if (!median->lum_only) {
326 median_9(data+lumsize, GST_BUFFER_DATA(outbuf)+lumsize, median->width/2, median->height/2);
327 median_9(data+lumsize+chromsize, GST_BUFFER_DATA(outbuf)+lumsize+chromsize, median->width/2, median->height/2);
330 memcpy (GST_BUFFER_DATA (outbuf)+lumsize, data+lumsize, chromsize*2);
333 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
335 gst_buffer_unref(buf);
337 gst_pad_push(median->srcpad,GST_DATA (outbuf));
341 gst_median_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
346 /* it's not null if we got it, but it might not be ours */
347 g_return_if_fail(GST_IS_MEDIAN(object));
348 median = GST_MEDIAN(object);
352 argvalue = g_value_get_int (value);
353 if (argvalue != 5 && argvalue != 9) {
354 g_warning ("median: invalid filtersize (%d), must be 5 or 9\n", argvalue);
357 median->filtersize = argvalue;
361 median->active = g_value_get_boolean (value);
364 median->lum_only = g_value_get_boolean (value);
372 gst_median_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
376 /* it's not null if we got it, but it might not be ours */
377 g_return_if_fail(GST_IS_MEDIAN(object));
378 median = GST_MEDIAN(object);
382 g_value_set_int (value, median->filtersize);
385 g_value_set_boolean (value, median->active);
388 g_value_set_boolean (value, median->lum_only);
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
398 plugin_init (GstPlugin *plugin)
400 return gst_element_register (plugin, "median",
401 GST_RANK_NONE, GST_TYPE_MEDIAN);
408 "Video median filter",