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