Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst / frei0r / gstfrei0rfilter.c
1 /* GStreamer
2  * Copyright (C) 2009,2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include "gstfrei0r.h"
27 #include "gstfrei0rfilter.h"
28
29 GST_DEBUG_CATEGORY_EXTERN (frei0r_debug);
30 #define GST_CAT_DEFAULT frei0r_debug
31
32 typedef struct
33 {
34   f0r_plugin_info_t info;
35   GstFrei0rFuncTable ftable;
36 } GstFrei0rFilterClassData;
37
38 static gboolean
39 gst_frei0r_filter_set_caps (GstBaseTransform * trans, GstCaps * incaps,
40     GstCaps * outcaps)
41 {
42   GstFrei0rFilter *self = GST_FREI0R_FILTER (trans);
43   GstFrei0rFilterClass *klass = GST_FREI0R_FILTER_GET_CLASS (trans);
44   GstVideoInfo info;
45   gboolean destroy_f0r_instance = FALSE;
46
47   gst_video_info_init (&info);
48   if (!gst_video_info_from_caps (&info, incaps))
49     return FALSE;
50
51   if (self->width != info.width || self->height != info.height)
52     destroy_f0r_instance = TRUE;
53
54   self->width = info.width;
55   self->height = info.height;
56
57   if (self->f0r_instance && destroy_f0r_instance) {
58     klass->ftable->destruct (self->f0r_instance);
59     self->f0r_instance = NULL;
60   }
61
62   return TRUE;
63 }
64
65 static gboolean
66 gst_frei0r_filter_stop (GstBaseTransform * trans)
67 {
68   GstFrei0rFilter *self = GST_FREI0R_FILTER (trans);
69   GstFrei0rFilterClass *klass = GST_FREI0R_FILTER_GET_CLASS (trans);
70
71   if (self->f0r_instance) {
72     klass->ftable->destruct (self->f0r_instance);
73     self->f0r_instance = NULL;
74   }
75
76   self->width = self->height = 0;
77
78   return TRUE;
79 }
80
81 static void
82 gst_frei0r_filter_before_transform (GstBaseTransform * trans,
83     GstBuffer * buffer)
84 {
85   GstClockTime timestamp;
86   GstFrei0rFilter *self = GST_FREI0R_FILTER (trans);
87
88   timestamp = GST_BUFFER_TIMESTAMP (buffer);
89   timestamp =
90       gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
91
92   GST_DEBUG_OBJECT (self, "sync to %" GST_TIME_FORMAT,
93       GST_TIME_ARGS (timestamp));
94
95   if (GST_CLOCK_TIME_IS_VALID (timestamp))
96     gst_object_sync_values (GST_OBJECT (self), timestamp);
97 }
98
99 static GstFlowReturn
100 gst_frei0r_filter_transform (GstBaseTransform * trans, GstBuffer * inbuf,
101     GstBuffer * outbuf)
102 {
103   GstFrei0rFilter *self = GST_FREI0R_FILTER (trans);
104   GstFrei0rFilterClass *klass = GST_FREI0R_FILTER_GET_CLASS (trans);
105   gdouble time;
106   GstMapInfo inmap, outmap;
107
108   if (G_UNLIKELY (self->width <= 0 || self->height <= 0))
109     return GST_FLOW_NOT_NEGOTIATED;
110
111   if (G_UNLIKELY (!self->f0r_instance)) {
112     self->f0r_instance =
113         gst_frei0r_instance_construct (klass->ftable, klass->properties,
114         klass->n_properties, self->property_cache, self->width, self->height);
115     if (G_UNLIKELY (!self->f0r_instance))
116       return GST_FLOW_ERROR;
117   }
118
119   time = ((gdouble) GST_BUFFER_TIMESTAMP (inbuf)) / GST_SECOND;
120
121   GST_OBJECT_LOCK (self);
122
123   gst_buffer_map (inbuf, &inmap, GST_MAP_READ);
124   gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
125
126   if (klass->ftable->update2)
127     klass->ftable->update2 (self->f0r_instance, time,
128         (const guint32 *) inmap.data, NULL, NULL, (guint32 *) outmap.data);
129   else
130     klass->ftable->update (self->f0r_instance, time,
131         (const guint32 *) inmap.data, (guint32 *) outmap.data);
132
133   gst_buffer_unmap (outbuf, &outmap);
134   gst_buffer_unmap (inbuf, &inmap);
135
136   GST_OBJECT_UNLOCK (self);
137
138   return GST_FLOW_OK;
139 }
140
141 static void
142 gst_frei0r_filter_finalize (GObject * object)
143 {
144   GstFrei0rFilter *self = GST_FREI0R_FILTER (object);
145   GstFrei0rFilterClass *klass = GST_FREI0R_FILTER_GET_CLASS (object);
146
147   if (self->f0r_instance) {
148     klass->ftable->destruct (self->f0r_instance);
149     self->f0r_instance = NULL;
150   }
151
152   if (self->property_cache)
153     gst_frei0r_property_cache_free (klass->properties, self->property_cache,
154         klass->n_properties);
155   self->property_cache = NULL;
156
157   G_OBJECT_CLASS (g_type_class_peek_parent (klass))->finalize (object);
158 }
159
160 static void
161 gst_frei0r_filter_get_property (GObject * object, guint prop_id, GValue * value,
162     GParamSpec * pspec)
163 {
164   GstFrei0rFilter *self = GST_FREI0R_FILTER (object);
165   GstFrei0rFilterClass *klass = GST_FREI0R_FILTER_GET_CLASS (object);
166
167   GST_OBJECT_LOCK (self);
168   if (!gst_frei0r_get_property (self->f0r_instance, klass->ftable,
169           klass->properties, klass->n_properties, self->property_cache, prop_id,
170           value))
171     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172   GST_OBJECT_UNLOCK (self);
173 }
174
175 static void
176 gst_frei0r_filter_set_property (GObject * object, guint prop_id,
177     const GValue * value, GParamSpec * pspec)
178 {
179   GstFrei0rFilter *self = GST_FREI0R_FILTER (object);
180   GstFrei0rFilterClass *klass = GST_FREI0R_FILTER_GET_CLASS (object);
181
182   GST_OBJECT_LOCK (self);
183   if (!gst_frei0r_set_property (self->f0r_instance, klass->ftable,
184           klass->properties, klass->n_properties, self->property_cache, prop_id,
185           value))
186     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187   GST_OBJECT_UNLOCK (self);
188 }
189
190 static void
191 gst_frei0r_filter_class_init (GstFrei0rFilterClass * klass,
192     GstFrei0rFilterClassData * class_data)
193 {
194   GObjectClass *gobject_class = (GObjectClass *) klass;
195   GstElementClass *gstelement_class = (GstElementClass *) klass;
196   GstBaseTransformClass *gsttrans_class = (GstBaseTransformClass *) klass;
197   GstPadTemplate *templ;
198   const gchar *desc;
199   GstCaps *caps;
200   gchar *author;
201
202   klass->ftable = &class_data->ftable;
203   klass->info = &class_data->info;
204
205   gobject_class->finalize = gst_frei0r_filter_finalize;
206   gobject_class->set_property = gst_frei0r_filter_set_property;
207   gobject_class->get_property = gst_frei0r_filter_get_property;
208
209   klass->n_properties = klass->info->num_params;
210   klass->properties = g_new0 (GstFrei0rProperty, klass->n_properties);
211
212   gst_frei0r_klass_install_properties (gobject_class, klass->ftable,
213       klass->properties, klass->n_properties);
214
215   author =
216       g_strdup_printf
217       ("Sebastian Dröge <sebastian.droege@collabora.co.uk>, %s",
218       class_data->info.author);
219   desc = class_data->info.explanation;
220   if (desc == NULL || *desc == '\0')
221     desc = "No details";
222   gst_element_class_set_metadata (gstelement_class, class_data->info.name,
223       "Filter/Effect/Video", desc, author);
224   g_free (author);
225
226   caps = gst_frei0r_caps_from_color_model (class_data->info.color_model);
227
228   templ =
229       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
230       gst_caps_ref (caps));
231   gst_element_class_add_pad_template (gstelement_class, templ);
232
233   templ = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
234   gst_element_class_add_pad_template (gstelement_class, templ);
235   gst_caps_unref (caps);
236
237   gsttrans_class->set_caps = GST_DEBUG_FUNCPTR (gst_frei0r_filter_set_caps);
238   gsttrans_class->stop = GST_DEBUG_FUNCPTR (gst_frei0r_filter_stop);
239   gsttrans_class->transform = GST_DEBUG_FUNCPTR (gst_frei0r_filter_transform);
240   gsttrans_class->before_transform =
241       GST_DEBUG_FUNCPTR (gst_frei0r_filter_before_transform);
242 }
243
244 static void
245 gst_frei0r_filter_init (GstFrei0rFilter * self, GstFrei0rFilterClass * klass)
246 {
247   self->property_cache =
248       gst_frei0r_property_cache_init (klass->properties, klass->n_properties);
249   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (self));
250   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (self));
251 }
252
253 GstFrei0rPluginRegisterReturn
254 gst_frei0r_filter_register (GstPlugin * plugin, const gchar * vendor,
255     const f0r_plugin_info_t * info, const GstFrei0rFuncTable * ftable)
256 {
257   GTypeInfo typeinfo = {
258     sizeof (GstFrei0rFilterClass),
259     NULL,
260     NULL,
261     (GClassInitFunc) gst_frei0r_filter_class_init,
262     NULL,
263     NULL,
264     sizeof (GstFrei0rFilter),
265     0,
266     (GInstanceInitFunc) gst_frei0r_filter_init
267   };
268   GType type;
269   gchar *type_name, *tmp;
270   GstFrei0rFilterClassData *class_data;
271   GstFrei0rPluginRegisterReturn ret = GST_FREI0R_PLUGIN_REGISTER_RETURN_FAILED;
272
273   if (vendor)
274     tmp = g_strdup_printf ("frei0r-filter-%s-%s", vendor, info->name);
275   else
276     tmp = g_strdup_printf ("frei0r-filter-%s", info->name);
277   type_name = g_ascii_strdown (tmp, -1);
278   g_free (tmp);
279   g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-+", '-');
280
281   if (g_type_from_name (type_name)) {
282     GST_DEBUG ("Type '%s' already exists", type_name);
283     return GST_FREI0R_PLUGIN_REGISTER_RETURN_ALREADY_REGISTERED;
284   }
285
286   class_data = g_new0 (GstFrei0rFilterClassData, 1);
287   memcpy (&class_data->info, info, sizeof (f0r_plugin_info_t));
288   memcpy (&class_data->ftable, ftable, sizeof (GstFrei0rFuncTable));
289   typeinfo.class_data = class_data;
290
291   type =
292       g_type_register_static (GST_TYPE_VIDEO_FILTER, type_name, &typeinfo, 0);
293   if (gst_element_register (plugin, type_name, GST_RANK_NONE, type))
294     ret = GST_FREI0R_PLUGIN_REGISTER_RETURN_OK;
295
296   g_free (type_name);
297   return ret;
298 }