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