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