2 * Copyright (C) 2009,2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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.
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.
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.
26 #include "gstfrei0r.h"
27 #include "gstfrei0rfilter.h"
29 GST_DEBUG_CATEGORY_EXTERN (frei0r_debug);
30 #define GST_CAT_DEFAULT frei0r_debug
34 f0r_plugin_info_t info;
35 GstFrei0rFuncTable ftable;
36 } GstFrei0rFilterClassData;
39 gst_frei0r_filter_set_caps (GstBaseTransform * trans, GstCaps * incaps,
42 GstFrei0rFilter *self = GST_FREI0R_FILTER (trans);
43 GstFrei0rFilterClass *klass = GST_FREI0R_FILTER_GET_CLASS (trans);
45 gboolean destroy_f0r_instance = FALSE;
47 gst_video_info_init (&info);
48 if (!gst_video_info_from_caps (&info, incaps))
51 if (self->width != info.width || self->height != info.height)
52 destroy_f0r_instance = TRUE;
54 self->width = info.width;
55 self->height = info.height;
57 if (self->f0r_instance && destroy_f0r_instance) {
58 klass->ftable->destruct (self->f0r_instance);
59 self->f0r_instance = NULL;
66 gst_frei0r_filter_stop (GstBaseTransform * trans)
68 GstFrei0rFilter *self = GST_FREI0R_FILTER (trans);
69 GstFrei0rFilterClass *klass = GST_FREI0R_FILTER_GET_CLASS (trans);
71 if (self->f0r_instance) {
72 klass->ftable->destruct (self->f0r_instance);
73 self->f0r_instance = NULL;
76 self->width = self->height = 0;
82 gst_frei0r_filter_before_transform (GstBaseTransform * trans,
85 GstClockTime timestamp;
86 GstFrei0rFilter *self = GST_FREI0R_FILTER (trans);
88 timestamp = GST_BUFFER_TIMESTAMP (buffer);
90 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
92 GST_DEBUG_OBJECT (self, "sync to %" GST_TIME_FORMAT,
93 GST_TIME_ARGS (timestamp));
95 if (GST_CLOCK_TIME_IS_VALID (timestamp))
96 gst_object_sync_values (GST_OBJECT (self), timestamp);
100 gst_frei0r_filter_transform (GstBaseTransform * trans, GstBuffer * inbuf,
103 GstFrei0rFilter *self = GST_FREI0R_FILTER (trans);
104 GstFrei0rFilterClass *klass = GST_FREI0R_FILTER_GET_CLASS (trans);
106 GstMapInfo inmap, outmap;
108 if (G_UNLIKELY (self->width <= 0 || self->height <= 0))
109 return GST_FLOW_NOT_NEGOTIATED;
111 if (G_UNLIKELY (!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;
119 time = ((gdouble) GST_BUFFER_TIMESTAMP (inbuf)) / GST_SECOND;
121 GST_OBJECT_LOCK (self);
123 gst_buffer_map (inbuf, &inmap, GST_MAP_READ);
124 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
126 if (klass->ftable->update2)
127 klass->ftable->update2 (self->f0r_instance, time,
128 (const guint32 *) inmap.data, NULL, NULL, (guint32 *) outmap.data);
130 klass->ftable->update (self->f0r_instance, time,
131 (const guint32 *) inmap.data, (guint32 *) outmap.data);
133 gst_buffer_unmap (outbuf, &outmap);
134 gst_buffer_unmap (inbuf, &inmap);
136 GST_OBJECT_UNLOCK (self);
142 gst_frei0r_filter_finalize (GObject * object)
144 GstFrei0rFilter *self = GST_FREI0R_FILTER (object);
145 GstFrei0rFilterClass *klass = GST_FREI0R_FILTER_GET_CLASS (object);
147 if (self->f0r_instance) {
148 klass->ftable->destruct (self->f0r_instance);
149 self->f0r_instance = NULL;
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;
157 G_OBJECT_CLASS (g_type_class_peek_parent (klass))->finalize (object);
161 gst_frei0r_filter_get_property (GObject * object, guint prop_id, GValue * value,
164 GstFrei0rFilter *self = GST_FREI0R_FILTER (object);
165 GstFrei0rFilterClass *klass = GST_FREI0R_FILTER_GET_CLASS (object);
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,
171 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172 GST_OBJECT_UNLOCK (self);
176 gst_frei0r_filter_set_property (GObject * object, guint prop_id,
177 const GValue * value, GParamSpec * pspec)
179 GstFrei0rFilter *self = GST_FREI0R_FILTER (object);
180 GstFrei0rFilterClass *klass = GST_FREI0R_FILTER_GET_CLASS (object);
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,
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187 GST_OBJECT_UNLOCK (self);
191 gst_frei0r_filter_class_init (GstFrei0rFilterClass * klass,
192 GstFrei0rFilterClassData * class_data)
194 GObjectClass *gobject_class = (GObjectClass *) klass;
195 GstElementClass *gstelement_class = (GstElementClass *) klass;
196 GstBaseTransformClass *gsttrans_class = (GstBaseTransformClass *) klass;
197 GstPadTemplate *templ;
202 klass->ftable = &class_data->ftable;
203 klass->info = &class_data->info;
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;
209 klass->n_properties = klass->info->num_params;
210 klass->properties = g_new0 (GstFrei0rProperty, klass->n_properties);
212 gst_frei0r_klass_install_properties (gobject_class, klass->ftable,
213 klass->properties, klass->n_properties);
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')
222 gst_element_class_set_metadata (gstelement_class, class_data->info.name,
223 "Filter/Effect/Video", desc, author);
226 caps = gst_frei0r_caps_from_color_model (class_data->info.color_model);
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);
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);
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);
245 gst_frei0r_filter_init (GstFrei0rFilter * self, GstFrei0rFilterClass * klass)
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));
253 GstFrei0rPluginRegisterReturn
254 gst_frei0r_filter_register (GstPlugin * plugin, const gchar * vendor,
255 const f0r_plugin_info_t * info, const GstFrei0rFuncTable * ftable)
257 GTypeInfo typeinfo = {
258 sizeof (GstFrei0rFilterClass),
261 (GClassInitFunc) gst_frei0r_filter_class_init,
264 sizeof (GstFrei0rFilter),
266 (GInstanceInitFunc) gst_frei0r_filter_init
269 gchar *type_name, *tmp;
270 GstFrei0rFilterClassData *class_data;
271 GstFrei0rPluginRegisterReturn ret = GST_FREI0R_PLUGIN_REGISTER_RETURN_FAILED;
274 tmp = g_strdup_printf ("frei0r-filter-%s-%s", vendor, info->name);
276 tmp = g_strdup_printf ("frei0r-filter-%s", info->name);
277 type_name = g_ascii_strdown (tmp, -1);
279 g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-+", '-');
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;
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;
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;