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