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