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