Debian Codegraph @

libs/gst/controller/gstargbcontrolbinding.c

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