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