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