another batch of connect->link fixes please let me know about issues and please refra...
[platform/upstream/gst-plugins-good.git] / gst / median / gstmedian.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <string.h>
21 #include <gstmedian.h>
22
23 /* elementfactory information */
24 static GstElementDetails median_details = {
25   "Median effect",
26   "Filter/Video",
27   "LGPL",
28   "Apply a median filter to an image",
29   VERSION,
30   "Wim Taymans <wim.taymans@chello.be>",
31   "(C) 2000",
32 };
33
34 GST_PAD_TEMPLATE_FACTORY (median_src_factory,
35   "src",
36   GST_PAD_SRC,
37   GST_PAD_ALWAYS,
38   GST_CAPS_NEW (
39    "median_src",
40    "video/raw",
41      "format",   GST_PROPS_FOURCC (GST_STR_FOURCC ("I420"))
42   )
43 )
44
45 GST_PAD_TEMPLATE_FACTORY (median_sink_factory,
46   "sink",
47   GST_PAD_SINK,
48   GST_PAD_ALWAYS,
49   GST_CAPS_NEW (
50    "median_src",
51    "video/raw",
52      "format",   GST_PROPS_FOURCC (GST_STR_FOURCC ("I420"))
53   )
54 )
55
56
57 /* Median signals and args */
58 enum {
59   /* FILL ME */
60   LAST_SIGNAL
61 };
62
63 enum {
64   ARG_0,
65   ARG_ACTIVE,
66   ARG_FILTERSIZE,
67   ARG_LUM_ONLY
68 };
69
70 static GType    gst_median_get_type     (void);
71 static void     gst_median_class_init   (GstMedianClass *klass);
72 static void     gst_median_init         (GstMedian *median);
73
74 static void     median_5                (unsigned char *src, unsigned char *dest, int height, int width);
75 static void     median_9                (unsigned char *src, unsigned char *dest, int height, int width);
76 static void     gst_median_chain        (GstPad *pad, GstBuffer *buf);
77
78 static void     gst_median_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
79 static void     gst_median_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
80
81 static GstElementClass *parent_class = NULL;
82 /*static guint gst_median_signals[LAST_SIGNAL] = { 0 }; */
83
84 GType
85 gst_median_get_type (void)
86 {
87   static GType median_type = 0;
88
89   if (!median_type) {
90     static const GTypeInfo median_info = {
91       sizeof(GstMedianClass),      NULL,      NULL,      (GClassInitFunc)gst_median_class_init,
92       NULL,
93       NULL,
94       sizeof(GstMedian),
95       0,
96       (GInstanceInitFunc)gst_median_init,
97     };
98     median_type = g_type_register_static(GST_TYPE_ELEMENT, "GstMedian", &median_info, 0);
99   }
100   return median_type;
101 }
102
103 static void
104 gst_median_class_init (GstMedianClass *klass)
105 {
106   GObjectClass *gobject_class;
107   GstElementClass *gstelement_class;
108
109   gobject_class = (GObjectClass*)klass;
110   gstelement_class = (GstElementClass*)klass;
111
112   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
113
114   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_ACTIVE,
115     g_param_spec_boolean("active","active","active",
116                          TRUE,G_PARAM_READWRITE)); /* CHECKME */
117   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FILTERSIZE,
118     g_param_spec_int("filtersize","filtersize","filtersize",
119                      G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
120   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LUM_ONLY,
121     g_param_spec_boolean("lum_only","lum_only","lum_only",
122                          TRUE,G_PARAM_READWRITE)); /* CHECKME */
123
124   gobject_class->set_property = gst_median_set_property;
125   gobject_class->get_property = gst_median_get_property;
126 }
127
128 static gboolean
129 gst_median_sinkconnect (GstPad *pad, GstCaps *caps)
130 {
131   GstMedian *filter;
132
133   filter = GST_MEDIAN (gst_pad_get_parent (pad));
134
135   if (!GST_CAPS_IS_FIXED (caps))
136     return GST_PAD_LINK_DELAYED;
137
138   gst_caps_get_int (caps, "width", &filter->width);
139   gst_caps_get_int (caps, "height", &filter->height);
140
141   /* forward to the next plugin */
142   return gst_pad_try_set_caps(filter->srcpad, gst_caps_copy_1(caps));
143 }
144
145 void gst_median_init (GstMedian *median)
146 {
147   median->sinkpad = gst_pad_new_from_template (
148                   GST_PAD_TEMPLATE_GET (median_sink_factory), "sink");
149   gst_pad_set_link_function (median->sinkpad, gst_median_sinkconnect);
150   gst_pad_set_chain_function (median->sinkpad, gst_median_chain);
151   gst_element_add_pad (GST_ELEMENT (median), median->sinkpad);
152
153   median->srcpad = gst_pad_new_from_template (
154                   GST_PAD_TEMPLATE_GET (median_src_factory), "src");
155   gst_element_add_pad (GST_ELEMENT (median), median->srcpad);
156
157   median->filtersize = 5;
158   median->lum_only = TRUE;
159   median->active = TRUE;
160 }
161
162 #define PIX_SORT(a,b) { if ((a)>(b)) PIX_SWAP((a),(b)); }
163 #define PIX_SWAP(a,b) { unsigned char temp=(a);(a)=(b);(b)=temp; }
164
165 static void
166 median_5 (unsigned char *src, unsigned char *dest, int width, int height)
167 {
168   int nLastRow;
169   int nLastCol;
170   unsigned char p[9];
171   int i, j, k;
172
173   nLastCol = width - 1;
174   nLastRow = height - 1;
175
176   /*copy the top and bottom rows into the result array */
177   for (i=0; i<width; i++) {
178     dest[i] = src[i];
179     dest[nLastRow * width + i] = src[nLastRow * width + i];
180   }
181   dest[i] = src[i];
182
183   nLastCol--;
184   nLastRow--;
185
186   /* process the interior pixels */
187   i = width + 1;
188   for (k=0; k < nLastRow; k++) {
189     for (j=0; j < nLastCol; j++, i++) {
190       p[0] = src[i-width];
191       p[1] = src[i-1];
192       p[2] = src[i];
193       p[3] = src[i+1];
194       p[4] = src[i+width];
195       PIX_SORT(p[0],p[1]) ; PIX_SORT(p[3],p[4]) ; PIX_SORT(p[0],p[3]) ;
196       PIX_SORT(p[1],p[4]) ; PIX_SORT(p[1],p[2]) ; PIX_SORT(p[2],p[3]) ;
197       PIX_SORT(p[1],p[2]) ;
198       dest[i] = p[2];
199     }
200     dest[i] = src[i];
201     i++;
202     dest[i] = src[i];
203     i++;
204   }
205   dest[i] = src[i];
206   i++;
207 }
208
209 static void
210 median_9 (unsigned char *src, unsigned char *dest, int width, int height)
211 {
212   int nLastRow;
213   int nLastCol;
214   unsigned char p[9];
215   int i, j, k;
216
217   nLastCol = width - 1;
218   nLastRow = height - 1;
219
220   /*copy the top and bottom rows into the result array */
221   for (i=0; i<width; i++) {
222     dest[i] = src[i];
223     dest[nLastRow * width + i] = src[nLastRow * width + i];
224   }
225   dest[i] = src[i];
226
227   nLastCol--;
228   nLastRow--;
229
230   /* process the interior pixels */
231   i = width + 1;
232   for (k=0; k < nLastRow; k++) {
233     for (j=0; j < nLastCol; j++, i++) {
234       p[0] = src[i-width-1];
235       p[1] = src[i-width];
236       p[2] = src[i-width+1];
237       p[3] = src[i-1];
238       p[4] = src[i];
239       p[5] = src[i+1];
240       p[6] = src[i+width-1];
241       p[7] = src[i+width];
242       p[8] = src[i+width+1];
243       PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ; 
244       PIX_SORT(p[0], p[1]) ; PIX_SORT(p[3], p[4]) ; PIX_SORT(p[6], p[7]) ; 
245       PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ; 
246       PIX_SORT(p[0], p[3]) ; PIX_SORT(p[5], p[8]) ; PIX_SORT(p[4], p[7]) ; 
247       PIX_SORT(p[3], p[6]) ; PIX_SORT(p[1], p[4]) ; PIX_SORT(p[2], p[5]) ; 
248       PIX_SORT(p[4], p[7]) ; PIX_SORT(p[4], p[2]) ; PIX_SORT(p[6], p[4]) ; 
249       PIX_SORT(p[4], p[2]) ;
250       dest[i] = p[4];
251     }
252     dest[i] = src[i];
253     i++;
254     dest[i] = src[i];
255     i++;
256   }
257   dest[i] = src[i];
258   i++;
259 }
260
261 static void
262 gst_median_chain (GstPad *pad, GstBuffer *buf)
263 {
264   GstMedian *median;
265   guchar *data;
266   gulong size;
267   GstBuffer *outbuf;
268 /*  GstMeta *meta; */
269   int lumsize, chromsize;
270
271   g_return_if_fail(pad != NULL);
272   g_return_if_fail(GST_IS_PAD(pad));
273   g_return_if_fail(buf != NULL);
274
275   median = GST_MEDIAN (GST_OBJECT_PARENT (pad));
276
277   if (!median->active) {
278     gst_pad_push(median->srcpad,buf);
279     return;
280   }
281
282   data = GST_BUFFER_DATA(buf);
283   size = GST_BUFFER_SIZE(buf);
284
285   GST_DEBUG (0,"median: have buffer of %d", GST_BUFFER_SIZE(buf));
286
287   outbuf = gst_buffer_new();
288   GST_BUFFER_DATA(outbuf) = g_malloc(GST_BUFFER_SIZE(buf));
289   GST_BUFFER_SIZE(outbuf) = GST_BUFFER_SIZE(buf);
290
291   lumsize = median->width * median->height;
292   chromsize = lumsize/4;
293
294   if (median->filtersize == 5) {
295     median_5(data, GST_BUFFER_DATA(outbuf), median->width, median->height);
296     if (!median->lum_only) {
297       median_5(data+lumsize, GST_BUFFER_DATA(outbuf)+lumsize, median->width/2, median->height/2);
298       median_5(data+lumsize+chromsize, GST_BUFFER_DATA(outbuf)+lumsize+chromsize, median->width/2, median->height/2);
299     }
300     else {
301       memcpy (GST_BUFFER_DATA (outbuf)+lumsize, data+lumsize, chromsize*2);
302     }
303   }
304   else {
305     median_9(data, GST_BUFFER_DATA(outbuf), median->width, median->height);
306     if (!median->lum_only) {
307       median_9(data+lumsize, GST_BUFFER_DATA(outbuf)+lumsize, median->width/2, median->height/2);
308       median_9(data+lumsize+chromsize, GST_BUFFER_DATA(outbuf)+lumsize+chromsize, median->width/2, median->height/2);
309     }
310     else {
311       memcpy (GST_BUFFER_DATA (outbuf)+lumsize, data+lumsize, chromsize*2);
312     }
313   }
314   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
315
316   gst_buffer_unref(buf);
317
318   gst_pad_push(median->srcpad,outbuf);
319 }
320
321 static void
322 gst_median_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
323 {
324   GstMedian *median;
325   gint argvalue;
326
327   /* it's not null if we got it, but it might not be ours */
328   g_return_if_fail(GST_IS_MEDIAN(object));
329   median = GST_MEDIAN(object);
330
331   switch (prop_id) {
332     case ARG_FILTERSIZE:
333       argvalue = g_value_get_int (value);
334       if (argvalue != 5 && argvalue != 9) {
335         g_warning ("median: invalid filtersize (%d), must be 5 or 9\n", argvalue);
336       }
337       else {
338         median->filtersize = argvalue;
339       }
340       break;
341     case ARG_ACTIVE:
342       median->active = g_value_get_boolean (value);
343       break;
344     case ARG_LUM_ONLY:
345       median->lum_only = g_value_get_boolean (value);
346       break;
347     default:
348       break;
349   }
350 }
351
352 static void
353 gst_median_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
354 {
355   GstMedian *median;
356
357   /* it's not null if we got it, but it might not be ours */
358   g_return_if_fail(GST_IS_MEDIAN(object));
359   median = GST_MEDIAN(object);
360
361   switch (prop_id) {
362     case ARG_FILTERSIZE:
363       g_value_set_int (value, median->filtersize);
364       break;
365     case ARG_ACTIVE:
366       g_value_set_boolean (value, median->active);
367       break;
368     case ARG_LUM_ONLY:
369       g_value_set_boolean (value, median->lum_only);
370       break;
371     default:
372       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373       break;
374   }
375 }
376
377
378 static gboolean
379 plugin_init (GModule *module, GstPlugin *plugin)
380 {
381   GstElementFactory *factory;
382
383   factory = gst_element_factory_new("median",GST_TYPE_MEDIAN,
384                                    &median_details);
385   g_return_val_if_fail(factory != NULL, FALSE);
386
387   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (median_sink_factory));
388   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (median_src_factory));
389
390   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
391
392   return TRUE;
393 }
394
395 GstPluginDesc plugin_desc = {
396   GST_VERSION_MAJOR,
397   GST_VERSION_MINOR,
398   "median",
399   plugin_init
400 };
401