summaryrefslogtreecommitdiff
path: root/libs/gst/controller/gstinterpolationcontrolsource.c
blob: 037bc7528cd1ca7beae69b38c3c94b7158211df4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* GStreamer
 *
 * Copyright (C) 2007,2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
 *
 * gstinterpolationcontrolsource.c: Control source that provides several
 *                                  interpolation methods
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:gstinterpolationcontrolsource
 * @short_description: interpolation control source
 *
 * #GstInterpolationControlSource is a #GstControlSource, that interpolates values between user-given
 * control points. It supports several interpolation modes and property types.
 *
 * To use #GstInterpolationControlSource get a new instance by calling
 * gst_interpolation_control_source_new(), bind it to a #GParamSpec, select a interpolation mode with
 * gst_interpolation_control_source_set_interpolation_mode() and set some control points by calling
 * gst_timed_value_control_source_set().
 *
 * All functions are MT-safe.
 *
 */

#include <glib-object.h>
#include <gst/gst.h>

#include "gstinterpolationcontrolsource.h"
#include "gstinterpolationcontrolsourceprivate.h"
#include "gst/glib-compat-private.h"

#define GST_CAT_DEFAULT controller_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);

#define _do_init \
  GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "interpolation control source", 0, \
    "timeline value interpolating control source")

G_DEFINE_TYPE_WITH_CODE (GstInterpolationControlSource,
    gst_interpolation_control_source, GST_TYPE_TIMED_VALUE_CONTROL_SOURCE,
    _do_init);

struct _GstInterpolationControlSourcePrivate
{
  GstInterpolateMode interpolation_mode;
};

/**
 * gst_interpolation_control_source_new:
 *
 * This returns a new, unbound #GstInterpolationControlSource.
 *
 * Returns: a new, unbound #GstInterpolationControlSource.
 */
GstInterpolationControlSource *
gst_interpolation_control_source_new (void)
{
  return g_object_newv (GST_TYPE_INTERPOLATION_CONTROL_SOURCE, 0, NULL);
}

/**
 * gst_interpolation_control_source_set_interpolation_mode:
 * @self: the #GstInterpolationControlSource object
 * @mode: interpolation mode
 *
 * Sets the given interpolation mode.
 *
 * <note><para>User interpolation is not yet available and quadratic interpolation
 * is deprecated and maps to cubic interpolation.</para></note>
 *
 * Returns: %TRUE if the interpolation mode could be set, %FALSE otherwise
 */
/* *INDENT-OFF* */
gboolean
gst_interpolation_control_source_set_interpolation_mode (
    GstInterpolationControlSource * self, GstInterpolateMode mode)
/* *INDENT-ON* */
{
  gboolean ret = TRUE;
  GstControlSource *csource = GST_CONTROL_SOURCE (self);

  if (mode >= priv_gst_num_interpolation_methods
      || priv_gst_interpolation_methods[mode] == NULL) {
    GST_WARNING ("interpolation mode %d invalid or not implemented yet", mode);
    return FALSE;
  }

  if (mode == GST_INTERPOLATE_QUADRATIC) {
    GST_WARNING ("Quadratic interpolation mode is deprecated, using cubic"
        "interpolation mode");
  }

  GST_TIMED_VALUE_CONTROL_SOURCE_LOCK (self);
  switch (gst_timed_value_control_source_get_base_value_type (
          (GstTimedValueControlSource *) self)) {
    case G_TYPE_INT:
      csource->get_value = priv_gst_interpolation_methods[mode]->get_int;
      csource->get_value_array =
          priv_gst_interpolation_methods[mode]->get_int_value_array;
      break;
    case G_TYPE_UINT:
      csource->get_value = priv_gst_interpolation_methods[mode]->get_uint;
      csource->get_value_array =
          priv_gst_interpolation_methods[mode]->get_uint_value_array;
      break;
    case G_TYPE_LONG:
      csource->get_value = priv_gst_interpolation_methods[mode]->get_long;
      csource->get_value_array =
          priv_gst_interpolation_methods[mode]->get_long_value_array;
      break;
    case G_TYPE_ULONG:
      csource->get_value = priv_gst_interpolation_methods[mode]->get_ulong;
      csource->get_value_array =
          priv_gst_interpolation_methods[mode]->get_ulong_value_array;
      break;
    case G_TYPE_INT64:
      csource->get_value = priv_gst_interpolation_methods[mode]->get_int64;
      csource->get_value_array =
          priv_gst_interpolation_methods[mode]->get_int64_value_array;
      break;
    case G_TYPE_UINT64:
      csource->get_value = priv_gst_interpolation_methods[mode]->get_uint64;
      csource->get_value_array =
          priv_gst_interpolation_methods[mode]->get_uint64_value_array;
      break;
    case G_TYPE_FLOAT:
      csource->get_value = priv_gst_interpolation_methods[mode]->get_float;
      csource->get_value_array =
          priv_gst_interpolation_methods[mode]->get_float_value_array;
      break;
    case G_TYPE_DOUBLE:
      csource->get_value = priv_gst_interpolation_methods[mode]->get_double;
      csource->get_value_array =
          priv_gst_interpolation_methods[mode]->get_double_value_array;
      break;
    case G_TYPE_BOOLEAN:
      csource->get_value = priv_gst_interpolation_methods[mode]->get_boolean;
      csource->get_value_array =
          priv_gst_interpolation_methods[mode]->get_boolean_value_array;
      break;
    case G_TYPE_ENUM:
      csource->get_value = priv_gst_interpolation_methods[mode]->get_enum;
      csource->get_value_array =
          priv_gst_interpolation_methods[mode]->get_enum_value_array;
      break;
    case G_TYPE_STRING:
      csource->get_value = priv_gst_interpolation_methods[mode]->get_string;
      csource->get_value_array =
          priv_gst_interpolation_methods[mode]->get_string_value_array;
      break;
    default:
      ret = FALSE;
      break;
  }

  /* Incomplete implementation */
  if (!csource->get_value || !csource->get_value_array) {
    ret = FALSE;
  }
  gst_timed_value_control_invalidate_cache ((GstTimedValueControlSource *)
      csource);
  self->priv->interpolation_mode = mode;

  GST_TIMED_VALUE_CONTROL_SOURCE_UNLOCK (self);

  return ret;
}

static gboolean
gst_interpolation_control_source_bind (GstControlSource * csource,
    GParamSpec * pspec)
{
  if (GST_CONTROL_SOURCE_CLASS
      (gst_interpolation_control_source_parent_class)->bind (csource, pspec)) {
    GstInterpolationControlSource *self =
        GST_INTERPOLATION_CONTROL_SOURCE (csource);

    if (gst_interpolation_control_source_set_interpolation_mode (self,
            self->priv->interpolation_mode))
      return TRUE;
  }
  return FALSE;
}

static void
gst_interpolation_control_source_init (GstInterpolationControlSource * self)
{
  self->priv =
      G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_INTERPOLATION_CONTROL_SOURCE,
      GstInterpolationControlSourcePrivate);
  self->priv->interpolation_mode = GST_INTERPOLATE_NONE;
}

static void
gst_interpolation_control_source_class_init (GstInterpolationControlSourceClass
    * klass)
{
  GstControlSourceClass *csource_class = GST_CONTROL_SOURCE_CLASS (klass);

  g_type_class_add_private (klass,
      sizeof (GstInterpolationControlSourcePrivate));

  csource_class->bind = gst_interpolation_control_source_bind;
}