3 * Copyright (C) 2007,2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5 * gstinterpolationcontrolsource.c: Control source that provides several
6 * interpolation methods
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * SECTION:gstinterpolationcontrolsource
26 * @short_description: interpolation control source
28 * #GstInterpolationControlSource is a #GstControlSource, that interpolates values between user-given
29 * control points. It supports several interpolation modes and property types.
31 * To use #GstInterpolationControlSource get a new instance by calling
32 * gst_interpolation_control_source_new(), bind it to a #GParamSpec, select a interpolation mode with
33 * gst_interpolation_control_source_set_interpolation_mode() and set some control points by calling
34 * gst_interpolation_control_source_set().
36 * All functions are MT-safe.
40 #include <glib-object.h>
43 #include "gstcontrolsource.h"
44 #include "gstinterpolationcontrolsource.h"
45 #include "gstinterpolationcontrolsourceprivate.h"
47 static void gst_interpolation_control_source_init (GstInterpolationControlSource
50 gst_interpolation_control_source_class_init (GstInterpolationControlSourceClass
53 G_DEFINE_TYPE (GstInterpolationControlSource, gst_interpolation_control_source,
54 GST_TYPE_CONTROL_SOURCE);
56 static GObjectClass *parent_class = NULL;
59 * gst_control_point_free:
60 * @prop: the object to free
62 * Private method which frees all data allocated by a #GstControlPoint
66 gst_control_point_free (GstControlPoint * cp)
68 g_return_if_fail (cp);
70 g_value_unset (&cp->value);
71 g_slice_free (GstControlPoint, cp);
75 gst_interpolation_control_source_reset (GstInterpolationControlSource * self)
77 GstControlSource *csource = GST_CONTROL_SOURCE (self);
79 csource->get_value = NULL;
80 csource->get_value_array = NULL;
82 self->priv->type = self->priv->base = G_TYPE_INVALID;
84 if (G_IS_VALUE (&self->priv->default_value))
85 g_value_unset (&self->priv->default_value);
86 if (G_IS_VALUE (&self->priv->minimum_value))
87 g_value_unset (&self->priv->minimum_value);
88 if (G_IS_VALUE (&self->priv->maximum_value))
89 g_value_unset (&self->priv->maximum_value);
91 if (self->priv->values) {
92 g_sequence_free (self->priv->values);
93 self->priv->values = NULL;
96 self->priv->nvalues = 0;
97 self->priv->valid_cache = FALSE;
101 * gst_interpolation_control_source_new:
103 * This returns a new, unbound #GstInterpolationControlSource.
105 * Returns: a new, unbound #GstInterpolationControlSource.
107 GstInterpolationControlSource *
108 gst_interpolation_control_source_new (void)
110 return g_object_new (GST_TYPE_INTERPOLATION_CONTROL_SOURCE, NULL);
114 * gst_interpolation_control_source_set_interpolation_mode:
115 * @self: the #GstInterpolationControlSource object
116 * @mode: interpolation mode
118 * Sets the given interpolation mode.
120 * <note><para>User interpolation is not yet available and quadratic interpolation
121 * is deprecated and maps to cubic interpolation.</para></note>
123 * Returns: %TRUE if the interpolation mode could be set, %FALSE otherwise
126 gst_interpolation_control_source_set_interpolation_mode
127 (GstInterpolationControlSource * self, GstInterpolateMode mode) {
129 GstControlSource *csource = GST_CONTROL_SOURCE (self);
131 if (mode >= priv_gst_num_interpolation_methods
132 || priv_gst_interpolation_methods[mode] == NULL) {
133 GST_WARNING ("interpolation mode %d invalid or not implemented yet", mode);
137 if (mode == GST_INTERPOLATE_QUADRATIC) {
138 GST_WARNING ("Quadratic interpolation mode is deprecated, using cubic"
139 "interpolation mode");
142 if (mode == GST_INTERPOLATE_USER) {
143 GST_WARNING ("User interpolation mode is not implemented yet");
147 g_mutex_lock (self->lock);
148 switch (self->priv->base) {
150 csource->get_value = priv_gst_interpolation_methods[mode]->get_int;
151 csource->get_value_array =
152 priv_gst_interpolation_methods[mode]->get_int_value_array;
155 csource->get_value = priv_gst_interpolation_methods[mode]->get_uint;
156 csource->get_value_array =
157 priv_gst_interpolation_methods[mode]->get_uint_value_array;
161 csource->get_value = priv_gst_interpolation_methods[mode]->get_long;
162 csource->get_value_array =
163 priv_gst_interpolation_methods[mode]->get_long_value_array;
167 csource->get_value = priv_gst_interpolation_methods[mode]->get_ulong;
168 csource->get_value_array =
169 priv_gst_interpolation_methods[mode]->get_ulong_value_array;
173 csource->get_value = priv_gst_interpolation_methods[mode]->get_int64;
174 csource->get_value_array =
175 priv_gst_interpolation_methods[mode]->get_int64_value_array;
179 csource->get_value = priv_gst_interpolation_methods[mode]->get_uint64;
180 csource->get_value_array =
181 priv_gst_interpolation_methods[mode]->get_uint64_value_array;
185 csource->get_value = priv_gst_interpolation_methods[mode]->get_float;
186 csource->get_value_array =
187 priv_gst_interpolation_methods[mode]->get_float_value_array;
191 csource->get_value = priv_gst_interpolation_methods[mode]->get_double;
192 csource->get_value_array =
193 priv_gst_interpolation_methods[mode]->get_double_value_array;
196 case G_TYPE_BOOLEAN:{
197 csource->get_value = priv_gst_interpolation_methods[mode]->get_boolean;
198 csource->get_value_array =
199 priv_gst_interpolation_methods[mode]->get_boolean_value_array;
203 csource->get_value = priv_gst_interpolation_methods[mode]->get_enum;
204 csource->get_value_array =
205 priv_gst_interpolation_methods[mode]->get_enum_value_array;
209 csource->get_value = priv_gst_interpolation_methods[mode]->get_string;
210 csource->get_value_array =
211 priv_gst_interpolation_methods[mode]->get_string_value_array;
219 /* Incomplete implementation */
220 if (!ret || !csource->get_value || !csource->get_value_array) {
221 gst_interpolation_control_source_reset (self);
225 self->priv->valid_cache = FALSE;
226 self->priv->interpolation_mode = mode;
228 g_mutex_unlock (self->lock);
234 gst_interpolation_control_source_bind (GstControlSource * source,
238 GstInterpolationControlSource *self =
239 GST_INTERPOLATION_CONTROL_SOURCE (source);
242 /* get the fundamental base type */
243 self->priv->type = base = type = G_PARAM_SPEC_VALUE_TYPE (pspec);
244 while ((type = g_type_parent (type)))
247 self->priv->base = base;
249 type = self->priv->type;
251 if (!gst_interpolation_control_source_set_interpolation_mode (self,
252 self->priv->interpolation_mode))
257 GParamSpecInt *tpspec = G_PARAM_SPEC_INT (pspec);
259 g_value_init (&self->priv->default_value, type);
260 g_value_set_int (&self->priv->default_value, tpspec->default_value);
261 g_value_init (&self->priv->minimum_value, type);
262 g_value_set_int (&self->priv->minimum_value, tpspec->minimum);
263 g_value_init (&self->priv->maximum_value, type);
264 g_value_set_int (&self->priv->maximum_value, tpspec->maximum);
268 GParamSpecUInt *tpspec = G_PARAM_SPEC_UINT (pspec);
270 g_value_init (&self->priv->default_value, type);
271 g_value_set_uint (&self->priv->default_value, tpspec->default_value);
272 g_value_init (&self->priv->minimum_value, type);
273 g_value_set_uint (&self->priv->minimum_value, tpspec->minimum);
274 g_value_init (&self->priv->maximum_value, type);
275 g_value_set_uint (&self->priv->maximum_value, tpspec->maximum);
279 GParamSpecLong *tpspec = G_PARAM_SPEC_LONG (pspec);
281 g_value_init (&self->priv->default_value, type);
282 g_value_set_long (&self->priv->default_value, tpspec->default_value);
283 g_value_init (&self->priv->minimum_value, type);
284 g_value_set_long (&self->priv->minimum_value, tpspec->minimum);
285 g_value_init (&self->priv->maximum_value, type);
286 g_value_set_long (&self->priv->maximum_value, tpspec->maximum);
290 GParamSpecULong *tpspec = G_PARAM_SPEC_ULONG (pspec);
292 g_value_init (&self->priv->default_value, type);
293 g_value_set_ulong (&self->priv->default_value, tpspec->default_value);
294 g_value_init (&self->priv->minimum_value, type);
295 g_value_set_ulong (&self->priv->minimum_value, tpspec->minimum);
296 g_value_init (&self->priv->maximum_value, type);
297 g_value_set_ulong (&self->priv->maximum_value, tpspec->maximum);
301 GParamSpecInt64 *tpspec = G_PARAM_SPEC_INT64 (pspec);
303 g_value_init (&self->priv->default_value, type);
304 g_value_set_int64 (&self->priv->default_value, tpspec->default_value);
305 g_value_init (&self->priv->minimum_value, type);
306 g_value_set_int64 (&self->priv->minimum_value, tpspec->minimum);
307 g_value_init (&self->priv->maximum_value, type);
308 g_value_set_int64 (&self->priv->maximum_value, tpspec->maximum);
312 GParamSpecUInt64 *tpspec = G_PARAM_SPEC_UINT64 (pspec);
314 g_value_init (&self->priv->default_value, type);
315 g_value_set_uint64 (&self->priv->default_value, tpspec->default_value);
316 g_value_init (&self->priv->minimum_value, type);
317 g_value_set_uint64 (&self->priv->minimum_value, tpspec->minimum);
318 g_value_init (&self->priv->maximum_value, type);
319 g_value_set_uint64 (&self->priv->maximum_value, tpspec->maximum);
323 GParamSpecFloat *tpspec = G_PARAM_SPEC_FLOAT (pspec);
325 g_value_init (&self->priv->default_value, type);
326 g_value_set_float (&self->priv->default_value, tpspec->default_value);
327 g_value_init (&self->priv->minimum_value, type);
328 g_value_set_float (&self->priv->minimum_value, tpspec->minimum);
329 g_value_init (&self->priv->maximum_value, type);
330 g_value_set_float (&self->priv->maximum_value, tpspec->maximum);
334 GParamSpecDouble *tpspec = G_PARAM_SPEC_DOUBLE (pspec);
336 g_value_init (&self->priv->default_value, type);
337 g_value_set_double (&self->priv->default_value, tpspec->default_value);
338 g_value_init (&self->priv->minimum_value, type);
339 g_value_set_double (&self->priv->minimum_value, tpspec->minimum);
340 g_value_init (&self->priv->maximum_value, type);
341 g_value_set_double (&self->priv->maximum_value, tpspec->maximum);
344 case G_TYPE_BOOLEAN:{
345 GParamSpecBoolean *tpspec = G_PARAM_SPEC_BOOLEAN (pspec);
347 g_value_init (&self->priv->default_value, type);
348 g_value_set_boolean (&self->priv->default_value, tpspec->default_value);
352 GParamSpecEnum *tpspec = G_PARAM_SPEC_ENUM (pspec);
354 g_value_init (&self->priv->default_value, type);
355 g_value_set_enum (&self->priv->default_value, tpspec->default_value);
359 GParamSpecString *tpspec = G_PARAM_SPEC_STRING (pspec);
361 g_value_init (&self->priv->default_value, type);
362 g_value_set_string (&self->priv->default_value, tpspec->default_value);
366 GST_WARNING ("incomplete implementation for paramspec type '%s'",
367 G_PARAM_SPEC_TYPE_NAME (pspec));
373 self->priv->valid_cache = FALSE;
374 self->priv->nvalues = 0;
376 gst_interpolation_control_source_reset (self);
383 * gst_control_point_compare:
384 * @p1: a pointer to a #GstControlPoint
385 * @p2: a pointer to a #GstControlPoint
387 * Compare function for g_list operations that operates on two #GstControlPoint
391 gst_control_point_compare (gconstpointer p1, gconstpointer p2)
393 GstClockTime ct1 = ((GstControlPoint *) p1)->timestamp;
394 GstClockTime ct2 = ((GstControlPoint *) p2)->timestamp;
396 return ((ct1 < ct2) ? -1 : ((ct1 == ct2) ? 0 : 1));
400 * gst_control_point_find:
401 * @p1: a pointer to a #GstControlPoint
402 * @p2: a pointer to a #GstClockTime
404 * Compare function for g_list operations that operates on a #GstControlPoint and
408 gst_control_point_find (gconstpointer p1, gconstpointer p2)
410 GstClockTime ct1 = ((GstControlPoint *) p1)->timestamp;
411 GstClockTime ct2 = *(GstClockTime *) p2;
413 return ((ct1 < ct2) ? -1 : ((ct1 == ct2) ? 0 : 1));
416 static GstControlPoint *
417 _make_new_cp (GstInterpolationControlSource * self, GstClockTime timestamp,
422 /* create a new GstControlPoint */
423 cp = g_slice_new0 (GstControlPoint);
424 cp->timestamp = timestamp;
425 g_value_init (&cp->value, self->priv->type);
426 g_value_copy (value, &cp->value);
432 gst_interpolation_control_source_set_internal (GstInterpolationControlSource *
433 self, GstClockTime timestamp, GValue * value)
437 /* check if a control point for the timestamp already exists */
439 /* iter contains the iter right *after* timestamp */
440 if (self->priv->values) {
442 g_sequence_search (self->priv->values, ×tamp,
443 (GCompareDataFunc) gst_control_point_find, NULL);
445 GSequenceIter *prev = g_sequence_iter_prev (iter);
446 GstControlPoint *cp = g_sequence_get (prev);
448 /* If the timestamp is the same just update the control point value */
449 if (cp->timestamp == timestamp) {
450 /* update control point */
451 g_value_reset (&cp->value);
452 g_value_copy (value, &cp->value);
458 /* sort new cp into the prop->values list */
459 if (!self->priv->values)
461 g_sequence_new ((GDestroyNotify) gst_control_point_free);
463 g_sequence_insert_sorted (self->priv->values, _make_new_cp (self, timestamp,
464 value), (GCompareDataFunc) gst_control_point_compare, NULL);
465 self->priv->nvalues++;
468 self->priv->valid_cache = FALSE;
473 * gst_interpolation_control_source_set:
474 * @self: the #GstInterpolationControlSource object
475 * @timestamp: the time the control-change is scheduled for
476 * @value: the control-value
478 * Set the value of given controller-handled property at a certain time.
480 * Returns: FALSE if the values couldn't be set, TRUE otherwise.
483 gst_interpolation_control_source_set (GstInterpolationControlSource * self,
484 GstClockTime timestamp, GValue * value)
486 g_return_val_if_fail (GST_IS_INTERPOLATION_CONTROL_SOURCE (self), FALSE);
487 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
488 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
489 g_return_val_if_fail (G_VALUE_TYPE (value) == self->priv->type, FALSE);
491 g_mutex_lock (self->lock);
492 gst_interpolation_control_source_set_internal (self, timestamp, value);
493 g_mutex_unlock (self->lock);
499 * gst_interpolation_control_source_set_from_list:
500 * @self: the #GstInterpolationControlSource object
501 * @timedvalues: a list with #GstTimedValue items
503 * Sets multiple timed values at once.
505 * Returns: FALSE if the values couldn't be set, TRUE otherwise.
508 gst_interpolation_control_source_set_from_list (GstInterpolationControlSource *
509 self, GSList * timedvalues)
513 gboolean res = FALSE;
515 g_return_val_if_fail (GST_IS_INTERPOLATION_CONTROL_SOURCE (self), FALSE);
517 for (node = timedvalues; node; node = g_slist_next (node)) {
519 if (!GST_CLOCK_TIME_IS_VALID (tv->timestamp)) {
520 GST_WARNING ("GstTimedValued with invalid timestamp passed to %s",
522 } else if (!G_IS_VALUE (&tv->value)) {
523 GST_WARNING ("GstTimedValued with invalid value passed to %s",
525 } else if (G_VALUE_TYPE (&tv->value) != self->priv->type) {
526 GST_WARNING ("incompatible value type for property");
528 g_mutex_lock (self->lock);
529 gst_interpolation_control_source_set_internal (self, tv->timestamp,
531 g_mutex_unlock (self->lock);
539 * gst_interpolation_control_source_unset:
540 * @self: the #GstInterpolationControlSource object
541 * @timestamp: the time the control-change should be removed from
543 * Used to remove the value of given controller-handled property at a certain
546 * Returns: FALSE if the value couldn't be unset (i.e. not found, TRUE otherwise.
549 gst_interpolation_control_source_unset (GstInterpolationControlSource * self,
550 GstClockTime timestamp)
553 gboolean res = FALSE;
555 g_return_val_if_fail (GST_IS_INTERPOLATION_CONTROL_SOURCE (self), FALSE);
556 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
558 g_mutex_lock (self->lock);
559 /* check if a control point for the timestamp exists */
561 g_sequence_search (self->priv->values, ×tamp,
562 (GCompareDataFunc) gst_control_point_find, NULL))) {
565 /* Iter contains the iter right after timestamp, i.e.
566 * we need to get the previous one and check the timestamp
568 iter = g_sequence_iter_prev (iter);
569 cp = g_sequence_get (iter);
570 if (cp->timestamp == timestamp) {
571 g_sequence_remove (iter);
572 self->priv->nvalues--;
573 self->priv->valid_cache = FALSE;
577 g_mutex_unlock (self->lock);
583 * gst_interpolation_control_source_unset_all:
584 * @self: the #GstInterpolationControlSource object
586 * Used to remove all time-stamped values of given controller-handled property
590 gst_interpolation_control_source_unset_all (GstInterpolationControlSource *
593 g_return_if_fail (GST_IS_INTERPOLATION_CONTROL_SOURCE (self));
595 g_mutex_lock (self->lock);
596 /* free GstControlPoint structures */
597 if (self->priv->values) {
598 g_sequence_free (self->priv->values);
599 self->priv->values = NULL;
601 self->priv->nvalues = 0;
602 self->priv->valid_cache = FALSE;
604 g_mutex_unlock (self->lock);
608 _append_control_point (GstControlPoint * cp, GList ** l)
610 *l = g_list_prepend (*l, cp);
614 * gst_interpolation_control_source_get_all:
615 * @self: the #GstInterpolationControlSource to get the list from
617 * Returns a read-only copy of the list of #GstTimedValue for the given property.
618 * Free the list after done with it.
620 * Returns: a copy of the list, or %NULL if the property isn't handled by the controller
623 gst_interpolation_control_source_get_all (GstInterpolationControlSource * self)
627 g_return_val_if_fail (GST_IS_INTERPOLATION_CONTROL_SOURCE (self), NULL);
629 g_mutex_lock (self->lock);
630 if (self->priv->values)
631 g_sequence_foreach (self->priv->values, (GFunc) _append_control_point,
633 g_mutex_unlock (self->lock);
635 return g_list_reverse (res);
639 * gst_interpolation_control_source_get_count:
640 * @self: the #GstInterpolationControlSource to get the number of values from
642 * Returns the number of control points that are set.
644 * Returns: the number of control points that are set.
648 gst_interpolation_control_source_get_count (GstInterpolationControlSource *
651 g_return_val_if_fail (GST_IS_INTERPOLATION_CONTROL_SOURCE (self), 0);
652 return self->priv->nvalues;
656 gst_interpolation_control_source_init (GstInterpolationControlSource * self)
658 self->lock = g_mutex_new ();
660 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_INTERPOLATION_CONTROL_SOURCE,
661 GstInterpolationControlSourcePrivate);
662 self->priv->interpolation_mode = GST_INTERPOLATE_NONE;
666 gst_interpolation_control_source_finalize (GObject * obj)
668 GstInterpolationControlSource *self = GST_INTERPOLATION_CONTROL_SOURCE (obj);
670 g_mutex_lock (self->lock);
671 gst_interpolation_control_source_reset (self);
672 g_mutex_unlock (self->lock);
673 g_mutex_free (self->lock);
674 G_OBJECT_CLASS (parent_class)->finalize (obj);
678 gst_interpolation_control_source_dispose (GObject * obj)
680 G_OBJECT_CLASS (parent_class)->dispose (obj);
684 gst_interpolation_control_source_class_init (GstInterpolationControlSourceClass
687 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
688 GstControlSourceClass *csource_class = GST_CONTROL_SOURCE_CLASS (klass);
690 parent_class = g_type_class_peek_parent (klass);
691 g_type_class_add_private (klass,
692 sizeof (GstInterpolationControlSourcePrivate));
694 gobject_class->finalize = gst_interpolation_control_source_finalize;
695 gobject_class->dispose = gst_interpolation_control_source_dispose;
696 csource_class->bind = gst_interpolation_control_source_bind;