Port gtk-doc comments to their equivalent markdown syntax
[platform/upstream/gstreamer.git] / libs / gst / controller / gstargbcontrolbinding.c
1 /* GStreamer
2  *
3  * Copyright (C) 2011 Stefan Sauer <ensonic@users.sf.net>
4  *
5  * gstargbcontrolbinding.c: Attachment for multiple control sources to gargb
6  *                            properties
7  *
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.
12  *
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.
17  *
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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 /**
24  * SECTION:gstargbcontrolbinding
25  * @title: GstARGBControlBinding
26  * @short_description: attachment for control sources to argb properties
27  *
28  * A value mapping object that attaches multiple control sources to a guint
29  * gobject properties representing a color. A control value of 0.0 will turn the
30  * color component off and a value of 1.0 will be the color level.
31  */
32
33 #include <glib-object.h>
34 #include <gst/gst.h>
35
36 #include "gstargbcontrolbinding.h"
37
38 #include <gst/math-compat.h>
39
40 #define GST_CAT_DEFAULT control_binding_debug
41 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
42
43 static GObject *gst_argb_control_binding_constructor (GType type,
44     guint n_construct_params, GObjectConstructParam * construct_params);
45 static void gst_argb_control_binding_set_property (GObject * object,
46     guint prop_id, const GValue * value, GParamSpec * pspec);
47 static void gst_argb_control_binding_get_property (GObject * object,
48     guint prop_id, GValue * value, GParamSpec * pspec);
49 static void gst_argb_control_binding_dispose (GObject * object);
50 static void gst_argb_control_binding_finalize (GObject * object);
51
52 static gboolean gst_argb_control_binding_sync_values (GstControlBinding * _self,
53     GstObject * object, GstClockTime timestamp, GstClockTime last_sync);
54 static GValue *gst_argb_control_binding_get_value (GstControlBinding * _self,
55     GstClockTime timestamp);
56 static gboolean gst_argb_control_binding_get_value_array (GstControlBinding *
57     _self, GstClockTime timestamp, GstClockTime interval, guint n_values,
58     gpointer values);
59 static gboolean gst_argb_control_binding_get_g_value_array (GstControlBinding *
60     _self, GstClockTime timestamp, GstClockTime interval, guint n_values,
61     GValue * values);
62
63 #define _do_init \
64   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstargbcontrolbinding", 0, \
65       "dynamic parameter control source attachment");
66
67 #define gst_argb_control_binding_parent_class parent_class
68 G_DEFINE_TYPE_WITH_CODE (GstARGBControlBinding, gst_argb_control_binding,
69     GST_TYPE_CONTROL_BINDING, _do_init);
70
71 enum
72 {
73   PROP_0,
74   PROP_CS_A,
75   PROP_CS_R,
76   PROP_CS_G,
77   PROP_CS_B,
78   PROP_LAST
79 };
80
81 static GParamSpec *properties[PROP_LAST];
82
83 /* vmethods */
84
85 static void
86 gst_argb_control_binding_class_init (GstARGBControlBindingClass * klass)
87 {
88   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89   GstControlBindingClass *control_binding_class =
90       GST_CONTROL_BINDING_CLASS (klass);
91
92   gobject_class->constructor = gst_argb_control_binding_constructor;
93   gobject_class->set_property = gst_argb_control_binding_set_property;
94   gobject_class->get_property = gst_argb_control_binding_get_property;
95   gobject_class->dispose = gst_argb_control_binding_dispose;
96   gobject_class->finalize = gst_argb_control_binding_finalize;
97
98   control_binding_class->sync_values = gst_argb_control_binding_sync_values;
99   control_binding_class->get_value = gst_argb_control_binding_get_value;
100   control_binding_class->get_value_array =
101       gst_argb_control_binding_get_value_array;
102   control_binding_class->get_g_value_array =
103       gst_argb_control_binding_get_g_value_array;
104
105   properties[PROP_CS_A] =
106       g_param_spec_object ("control-source-a", "ControlSource A",
107       "The control source for the alpha color component",
108       GST_TYPE_CONTROL_SOURCE,
109       G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
110
111   properties[PROP_CS_R] =
112       g_param_spec_object ("control-source-r", "ControlSource R",
113       "The control source for the red color component",
114       GST_TYPE_CONTROL_SOURCE,
115       G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
116
117   properties[PROP_CS_G] =
118       g_param_spec_object ("control-source-g", "ControlSource G",
119       "The control source for the green color component",
120       GST_TYPE_CONTROL_SOURCE,
121       G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
122
123   properties[PROP_CS_B] =
124       g_param_spec_object ("control-source-b", "ControlSource B",
125       "The control source for the blue color component",
126       GST_TYPE_CONTROL_SOURCE,
127       G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
128
129   g_object_class_install_properties (gobject_class, PROP_LAST, properties);
130 }
131
132 static void
133 gst_argb_control_binding_init (GstARGBControlBinding * self)
134 {
135 }
136
137 static GObject *
138 gst_argb_control_binding_constructor (GType type, guint n_construct_params,
139     GObjectConstructParam * construct_params)
140 {
141   GstARGBControlBinding *self;
142
143   self =
144       GST_ARGB_CONTROL_BINDING (G_OBJECT_CLASS (parent_class)->constructor
145       (type, n_construct_params, construct_params));
146
147   if (GST_CONTROL_BINDING_PSPEC (self)) {
148     if (!(G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (self)) ==
149             G_TYPE_UINT)) {
150       GST_WARNING ("can't bind to paramspec type '%s'",
151           G_PARAM_SPEC_TYPE_NAME (GST_CONTROL_BINDING_PSPEC (self)));
152       GST_CONTROL_BINDING_PSPEC (self) = NULL;
153     } else {
154       g_value_init (&self->cur_value, G_TYPE_UINT);
155     }
156   }
157   return (GObject *) self;
158 }
159
160 static void
161 gst_argb_control_binding_set_property (GObject * object, guint prop_id,
162     const GValue * value, GParamSpec * pspec)
163 {
164   GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (object);
165
166   switch (prop_id) {
167     case PROP_CS_A:
168       gst_object_replace ((GstObject **) & self->cs_a,
169           g_value_get_object (value));
170       break;
171     case PROP_CS_R:
172       gst_object_replace ((GstObject **) & self->cs_r,
173           g_value_get_object (value));
174       break;
175     case PROP_CS_G:
176       gst_object_replace ((GstObject **) & self->cs_g,
177           g_value_get_object (value));
178       break;
179     case PROP_CS_B:
180       gst_object_replace ((GstObject **) & self->cs_b,
181           g_value_get_object (value));
182       break;
183     default:
184       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185       break;
186   }
187 }
188
189 static void
190 gst_argb_control_binding_get_property (GObject * object, guint prop_id,
191     GValue * value, GParamSpec * pspec)
192 {
193   GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (object);
194
195   switch (prop_id) {
196     case PROP_CS_A:
197       g_value_set_object (value, self->cs_a);
198       break;
199     case PROP_CS_R:
200       g_value_set_object (value, self->cs_r);
201       break;
202     case PROP_CS_G:
203       g_value_set_object (value, self->cs_g);
204       break;
205     case PROP_CS_B:
206       g_value_set_object (value, self->cs_b);
207       break;
208     default:
209       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210       break;
211   }
212 }
213
214 static void
215 gst_argb_control_binding_dispose (GObject * object)
216 {
217   GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (object);
218
219   gst_object_replace ((GstObject **) & self->cs_a, NULL);
220   gst_object_replace ((GstObject **) & self->cs_r, NULL);
221   gst_object_replace ((GstObject **) & self->cs_g, NULL);
222   gst_object_replace ((GstObject **) & self->cs_b, NULL);
223
224   G_OBJECT_CLASS (parent_class)->dispose (object);
225 }
226
227 static void
228 gst_argb_control_binding_finalize (GObject * object)
229 {
230   GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (object);
231
232   g_value_unset (&self->cur_value);
233
234   G_OBJECT_CLASS (parent_class)->finalize (object);
235 }
236
237 static gboolean
238 gst_argb_control_binding_sync_values (GstControlBinding * _self,
239     GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
240 {
241   GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (_self);
242   gdouble src_val_a = 1.0, src_val_r = 0.0, src_val_g = 0.0, src_val_b = 0.0;
243   gboolean ret = TRUE;
244
245   g_return_val_if_fail (GST_IS_ARGB_CONTROL_BINDING (self), FALSE);
246   g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
247
248   GST_LOG_OBJECT (object, "property '%s' at ts=%" GST_TIME_FORMAT,
249       _self->name, GST_TIME_ARGS (timestamp));
250
251   if (self->cs_a)
252     ret &= gst_control_source_get_value (self->cs_a, timestamp, &src_val_a);
253   if (self->cs_r)
254     ret &= gst_control_source_get_value (self->cs_r, timestamp, &src_val_r);
255   if (self->cs_g)
256     ret &= gst_control_source_get_value (self->cs_g, timestamp, &src_val_g);
257   if (self->cs_b)
258     ret &= gst_control_source_get_value (self->cs_b, timestamp, &src_val_b);
259   if (G_LIKELY (ret)) {
260     guint src_val = (((guint) (CLAMP (src_val_a, 0.0, 1.0) * 255)) << 24) |
261         (((guint) (CLAMP (src_val_r, 0.0, 1.0) * 255)) << 16) |
262         (((guint) (CLAMP (src_val_g, 0.0, 1.0) * 255)) << 8) |
263         ((guint) (CLAMP (src_val_b, 0.0, 1.0) * 255));
264     GST_LOG_OBJECT (object, "  new value 0x%08x", src_val);
265     /* always set the value for first time, but then only if it changed
266      * this should limit g_object_notify invocations.
267      * FIXME: can we detect negative playback rates?
268      */
269     if ((timestamp < last_sync) || (src_val != self->last_value)) {
270       GValue *dst_val = &self->cur_value;
271
272       g_value_set_uint (dst_val, src_val);
273       /* we can make this faster
274        * http://bugzilla.gnome.org/show_bug.cgi?id=536939
275        */
276       g_object_set_property ((GObject *) object, _self->name, dst_val);
277       self->last_value = src_val;
278     }
279   } else {
280     GST_DEBUG_OBJECT (object, "no control value for param %s", _self->name);
281   }
282   return (ret);
283 }
284
285 static GValue *
286 gst_argb_control_binding_get_value (GstControlBinding * _self,
287     GstClockTime timestamp)
288 {
289   GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (_self);
290   GValue *dst_val = NULL;
291   gdouble src_val_a = 1.0, src_val_r = 0.0, src_val_g = 0.0, src_val_b = 0.0;
292   gboolean ret = TRUE;
293
294   g_return_val_if_fail (GST_IS_ARGB_CONTROL_BINDING (self), NULL);
295   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
296   g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
297
298   /* get current value via control source */
299   if (self->cs_a)
300     ret &= gst_control_source_get_value (self->cs_a, timestamp, &src_val_a);
301   if (self->cs_r)
302     ret &= gst_control_source_get_value (self->cs_r, timestamp, &src_val_r);
303   if (self->cs_g)
304     ret &= gst_control_source_get_value (self->cs_g, timestamp, &src_val_g);
305   if (self->cs_b)
306     ret &= gst_control_source_get_value (self->cs_b, timestamp, &src_val_b);
307   if (G_LIKELY (ret)) {
308     guint src_val = (((guint) (CLAMP (src_val_a, 0.0, 1.0) * 255)) << 24) |
309         (((guint) (CLAMP (src_val_r, 0.0, 1.0) * 255)) << 16) |
310         (((guint) (CLAMP (src_val_g, 0.0, 1.0) * 255)) << 8) |
311         ((guint) (CLAMP (src_val_b, 0.0, 1.0) * 255));
312     dst_val = g_new0 (GValue, 1);
313     g_value_init (dst_val, G_TYPE_UINT);
314     g_value_set_uint (dst_val, src_val);
315   } else {
316     GST_LOG ("no control value for property %s at ts %" GST_TIME_FORMAT,
317         _self->name, GST_TIME_ARGS (timestamp));
318   }
319
320   return dst_val;
321 }
322
323 static gboolean
324 gst_argb_control_binding_get_value_array (GstControlBinding * _self,
325     GstClockTime timestamp, GstClockTime interval, guint n_values,
326     gpointer values_)
327 {
328   GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (_self);
329   gint i;
330   gdouble *src_val_a = NULL, *src_val_r = NULL, *src_val_g = NULL, *src_val_b =
331       NULL;
332   guint *values = (guint *) values_;
333   gboolean ret = TRUE;
334
335   g_return_val_if_fail (GST_IS_ARGB_CONTROL_BINDING (self), FALSE);
336   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
337   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
338   g_return_val_if_fail (values, FALSE);
339   g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
340
341   if (self->cs_a) {
342     src_val_a = g_new0 (gdouble, n_values);
343     ret &= gst_control_source_get_value_array (self->cs_a, timestamp,
344         interval, n_values, src_val_a);
345   }
346   if (self->cs_r) {
347     src_val_r = g_new0 (gdouble, n_values);
348     ret &= gst_control_source_get_value_array (self->cs_r, timestamp,
349         interval, n_values, src_val_r);
350   }
351   if (self->cs_g) {
352     src_val_g = g_new0 (gdouble, n_values);
353     ret &= gst_control_source_get_value_array (self->cs_g, timestamp,
354         interval, n_values, src_val_g);
355   }
356   if (self->cs_b) {
357     src_val_b = g_new0 (gdouble, n_values);
358     ret &= gst_control_source_get_value_array (self->cs_b, timestamp,
359         interval, n_values, src_val_b);
360   }
361   if (G_LIKELY (ret)) {
362     for (i = 0; i < n_values; i++) {
363       gdouble a = 1.0, r = 0.0, g = 0.0, b = 0.0;
364       if (src_val_a && !isnan (src_val_a[i]))
365         a = src_val_a[i];
366       if (src_val_r && !isnan (src_val_r[i]))
367         r = src_val_r[i];
368       if (src_val_g && !isnan (src_val_g[i]))
369         g = src_val_g[i];
370       if (src_val_b && !isnan (src_val_b[i]))
371         b = src_val_b[i];
372       values[i] = (((guint) (CLAMP (a, 0.0, 1.0) * 255)) << 24) |
373           (((guint) (CLAMP (r, 0.0, 1.0) * 255)) << 16) |
374           (((guint) (CLAMP (g, 0.0, 1.0) * 255)) << 8) |
375           ((guint) (CLAMP (b, 0.0, 1.0) * 255));
376     }
377   } else {
378     GST_LOG ("failed to get control value for property %s at ts %"
379         GST_TIME_FORMAT, _self->name, GST_TIME_ARGS (timestamp));
380   }
381   g_free (src_val_a);
382   g_free (src_val_r);
383   g_free (src_val_g);
384   g_free (src_val_b);
385   return ret;
386 }
387
388 static gboolean
389 gst_argb_control_binding_get_g_value_array (GstControlBinding * _self,
390     GstClockTime timestamp, GstClockTime interval, guint n_values,
391     GValue * values)
392 {
393   GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (_self);
394   gint i;
395   gdouble *src_val_a = NULL, *src_val_r = NULL, *src_val_g = NULL, *src_val_b =
396       NULL;
397   guint src_val;
398   gboolean ret = TRUE;
399
400   g_return_val_if_fail (GST_IS_ARGB_CONTROL_BINDING (self), FALSE);
401   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
402   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
403   g_return_val_if_fail (values, FALSE);
404   g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
405
406   if (self->cs_a) {
407     src_val_a = g_new0 (gdouble, n_values);
408     ret &= gst_control_source_get_value_array (self->cs_a, timestamp,
409         interval, n_values, src_val_a);
410   }
411   if (self->cs_r) {
412     src_val_r = g_new0 (gdouble, n_values);
413     ret &= gst_control_source_get_value_array (self->cs_r, timestamp,
414         interval, n_values, src_val_r);
415   }
416   if (self->cs_g) {
417     src_val_g = g_new0 (gdouble, n_values);
418     ret &= gst_control_source_get_value_array (self->cs_g, timestamp,
419         interval, n_values, src_val_g);
420   }
421   if (self->cs_b) {
422     src_val_b = g_new0 (gdouble, n_values);
423     ret &= gst_control_source_get_value_array (self->cs_b, timestamp,
424         interval, n_values, src_val_b);
425   }
426   if (G_LIKELY (ret)) {
427     for (i = 0; i < n_values; i++) {
428       gdouble a = 1.0, r = 0.0, g = 0.0, b = 0.0;
429       if (src_val_a && !isnan (src_val_a[i]))
430         a = src_val_a[i];
431       if (src_val_r && !isnan (src_val_r[i]))
432         r = src_val_r[i];
433       if (src_val_g && !isnan (src_val_g[i]))
434         g = src_val_g[i];
435       if (src_val_b && !isnan (src_val_b[i]))
436         b = src_val_b[i];
437       src_val = (((guint) (CLAMP (a, 0.0, 1.0) * 255)) << 24) |
438           (((guint) (CLAMP (r, 0.0, 1.0) * 255)) << 16) |
439           (((guint) (CLAMP (g, 0.0, 1.0) * 255)) << 8) |
440           ((guint) (CLAMP (b, 0.0, 1.0) * 255));
441       g_value_init (&values[i], G_TYPE_UINT);
442       g_value_set_uint (&values[i], src_val);
443     }
444   } else {
445     GST_LOG ("failed to get control value for property %s at ts %"
446         GST_TIME_FORMAT, _self->name, GST_TIME_ARGS (timestamp));
447   }
448   g_free (src_val_a);
449   g_free (src_val_r);
450   g_free (src_val_g);
451   g_free (src_val_b);
452   return ret;
453 }
454
455 /* functions */
456
457 /**
458  * gst_argb_control_binding_new:
459  * @object: the object of the property
460  * @property_name: the property-name to attach the control source
461  * @cs_a: the control source for the alpha channel
462  * @cs_r: the control source for the red channel
463  * @cs_g: the control source for the green channel
464  * @cs_b: the control source for the blue channel
465  *
466  * Create a new control-binding that attaches the given #GstControlSource to the
467  * #GObject property.
468  *
469  * Returns: (transfer floating): the new #GstARGBControlBinding
470  */
471 GstControlBinding *
472 gst_argb_control_binding_new (GstObject * object, const gchar * property_name,
473     GstControlSource * cs_a, GstControlSource * cs_r, GstControlSource * cs_g,
474     GstControlSource * cs_b)
475 {
476   return (GstControlBinding *) g_object_new (GST_TYPE_ARGB_CONTROL_BINDING,
477       "object", object, "name", property_name,
478       "control-source-a", cs_a,
479       "control-source-r", cs_r,
480       "control-source-g", cs_g, "control-source-b", cs_b, NULL);
481 }
482
483 /* functions */