1 /* GStreamer
2 * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * SECTION:gstvalue
22 * @title: GstValue
23 * @short_description: GValue implementations specific
24 * to GStreamer
25 *
26 * GValue implementations specific to GStreamer.
27 *
28 * Note that operations on the same #GValue from multiple threads may lead to
29 * undefined behaviour.
30 */
31
32 /* Suppress warnings for GValueAraray */
33 #define GLIB_DISABLE_DEPRECATION_WARNINGS
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 #include <math.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43
44 #include "gst_private.h"
45 #include "glib-compat-private.h"
46 #include <gst/gst.h>
47 #include <gobject/gvaluecollector.h>
48 #include "gstutils.h"
49 #include "gstquark.h"
50
51 /* GstValueUnionFunc:
52 * @dest: a #GValue for the result
53 * @value1: a #GValue operand
54 * @value2: a #GValue operand
55 *
56 * Used by gst_value_union() to perform unification for a specific #GValue
57 * type. Register a new implementation with gst_value_register_union_func().
58 *
59 * Returns: %TRUE if a union was successful
60 */
61 typedef gboolean (*GstValueUnionFunc) (GValue * dest,
62 const GValue * value1, const GValue * value2);
63
64 /* GstValueIntersectFunc:
65 * @dest: (out caller-allocates): a #GValue for the result
66 * @value1: a #GValue operand
67 * @value2: a #GValue operand
68 *
69 * Used by gst_value_intersect() to perform intersection for a specific #GValue
70 * type. If the intersection is non-empty, the result is
71 * placed in @dest and %TRUE is returned. If the intersection is
72 * empty, @dest is unmodified and %FALSE is returned.
73 * Register a new implementation with gst_value_register_intersect_func().
74 *
75 * Returns: %TRUE if the values can intersect
76 */
77 typedef gboolean (*GstValueIntersectFunc) (GValue * dest,
78 const GValue * value1, const GValue * value2);
79
80 /* GstValueSubtractFunc:
81 * @dest: (out caller-allocates): a #GValue for the result
82 * @minuend: a #GValue operand
83 * @subtrahend: a #GValue operand
84 *
85 * Used by gst_value_subtract() to perform subtraction for a specific #GValue
86 * type. Register a new implementation with gst_value_register_subtract_func().
87 *
88 * Returns: %TRUE if the subtraction is not empty
89 */
90 typedef gboolean (*GstValueSubtractFunc) (GValue * dest,
91 const GValue * minuend, const GValue * subtrahend);
92
93 static void gst_value_register_union_func (GType type1,
94 GType type2, GstValueUnionFunc func);
95 static void gst_value_register_intersect_func (GType type1,
96 GType type2, GstValueIntersectFunc func);
97 static void gst_value_register_subtract_func (GType minuend_type,
98 GType subtrahend_type, GstValueSubtractFunc func);
99
100 static gboolean _priv_gst_value_parse_list (gchar * s, gchar ** after,
101 GValue * value, GType type, GParamSpec * pspec);
102 static gboolean _priv_gst_value_parse_array (gchar * s, gchar ** after,
103 GValue * value, GType type, GParamSpec * pspec);
104
105 typedef struct _GstValueUnionInfo GstValueUnionInfo;
106 struct _GstValueUnionInfo
107 {
108 GType type1;
109 GType type2;
110 GstValueUnionFunc func;
111 };
112
113 typedef struct _GstValueIntersectInfo GstValueIntersectInfo;
114 struct _GstValueIntersectInfo
115 {
116 GType type1;
117 GType type2;
118 GstValueIntersectFunc func;
119 };
120
121 typedef struct _GstValueSubtractInfo GstValueSubtractInfo;
122 struct _GstValueSubtractInfo
123 {
124 GType minuend;
125 GType subtrahend;
126 GstValueSubtractFunc func;
127 };
128
129 struct _GstFlagSetClass
130 {
131 GTypeClass parent;
132 GType flags_type; /* Type of the GFlags this flagset carries (can be 0) */
133 };
134
135 typedef struct _GstFlagSetClass GstFlagSetClass;
136
137 typedef struct _GstValueAbbreviation GstValueAbbreviation;
138
139 struct _GstValueAbbreviation
140 {
141 const gchar *type_name;
142 GType type;
143 };
144
145 /* Actual internal implementation of "GstValueList" and
146 * "GstValueArray" */
147 typedef struct _GstValueList GstValueList;
148
149 struct _GstValueList
150 {
151 /* These 2 fields must remain the same so that they match the public
152 * GArray structure (which was the former implementation) just in
153 * case someone calls `gst_value_peek_pointer` to access the
154 * array/list (such as in gststructure.c) */
155 GValue *fields;
156 guint len;
157
158 guint allocated;
159 GValue arr[1];
160 };
161
162 #define FUNDAMENTAL_TYPE_ID_MAX \
163 (G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT)
164 #define FUNDAMENTAL_TYPE_ID(type) \
165 ((type) >> G_TYPE_FUNDAMENTAL_SHIFT)
166
167 #define VALUE_LIST_ARRAY(v) ((GstValueList *) (v)->data[0].v_pointer)
168 #define VALUE_LIST_SIZE(v) (VALUE_LIST_ARRAY(v)->len)
169 #define VALUE_LIST_GET_VALUE(v, index) ((const GValue *) &(VALUE_LIST_ARRAY(v)->fields[index]))
170 #define VALUE_LIST_IS_USING_DYNAMIC_ARRAY(array) ((array)->fields != &(array)->arr[0])
171
172 static GArray *gst_value_table;
173 static GHashTable *gst_value_hash;
174 static GstValueTable *gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID_MAX + 1];
175 static GArray *gst_value_union_funcs;
176 static GArray *gst_value_intersect_funcs;
177 static GArray *gst_value_subtract_funcs;
178
179 /* Forward declarations */
180 static gchar *gst_value_serialize_fraction (const GValue * value);
181 static gint gst_value_compare_fraction (const GValue * value1,
182 const GValue * value2);
183
184 static GstValueCompareFunc gst_value_get_compare_func (const GValue * value1);
185
186 static gchar *gst_string_wrap (const gchar * s);
187 static gchar *gst_string_unwrap (const gchar * s);
188
189 static void gst_value_move (GValue * dest, GValue * src);
190 static void _gst_value_list_append_and_take_value (GValue * value,
191 GValue * append_value);
192 static void _gst_value_array_append_and_take_value (GValue * value,
193 GValue * append_value);
194
195 static inline GstValueTable *
gst_value_hash_lookup_type(GType type)196 gst_value_hash_lookup_type (GType type)
197 {
198 if (G_LIKELY (G_TYPE_IS_FUNDAMENTAL (type)))
199 return gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)];
200 else
201 return g_hash_table_lookup (gst_value_hash, (gpointer) type);
202 }
203
204 static void
gst_value_hash_add_type(GType type,const GstValueTable * table)205 gst_value_hash_add_type (GType type, const GstValueTable * table)
206 {
207 if (G_TYPE_IS_FUNDAMENTAL (type))
208 gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)] = (gpointer) table;
209
210 g_hash_table_insert (gst_value_hash, (gpointer) type, (gpointer) table);
211 }
212
213 /********
214 * list *
215 ********/
216
217 static void
resize_value_list(GstValueList * vlist)218 resize_value_list (GstValueList * vlist)
219 {
220 guint want_alloc;
221
222 if (G_UNLIKELY (vlist->allocated > (G_MAXUINT / 2)))
223 g_error ("Growing GstValueList would result in overflow");
224
225 want_alloc = MAX (GST_ROUND_UP_8 (vlist->len + 1), vlist->allocated * 2);
226
227 if (VALUE_LIST_IS_USING_DYNAMIC_ARRAY (vlist)) {
228 vlist->fields = g_renew (GValue, vlist->fields, want_alloc);
229 } else {
230 vlist->fields = g_new0 (GValue, want_alloc);
231 memcpy (vlist->fields, &vlist->arr[0], vlist->len * sizeof (GValue));
232 GST_CAT_LOG (GST_CAT_PERFORMANCE, "Exceeding pre-allocated array");
233 }
234 vlist->allocated = want_alloc;
235 }
236
237 /* Replacement for g_array_append_val */
238 static void
_gst_value_list_append_val(GstValueList * vlist,GValue * val)239 _gst_value_list_append_val (GstValueList * vlist, GValue * val)
240 {
241 /* resize if needed */
242 if (G_UNLIKELY (vlist->len == vlist->allocated))
243 resize_value_list (vlist);
244
245 /* Finally set value */
246 vlist->fields[vlist->len++] = *val;
247 }
248
249 /* Replacement for g_array_prepend_val */
250 static void
_gst_value_list_prepend_val(GstValueList * vlist,GValue * val)251 _gst_value_list_prepend_val (GstValueList * vlist, GValue * val)
252 {
253 /* resize if needed */
254 if (G_UNLIKELY (vlist->len == vlist->allocated))
255 resize_value_list (vlist);
256
257 /* Shift everything */
258 memmove (&vlist->fields[1], &vlist->fields[0],
259 (vlist->len) * sizeof (GValue));
260
261 vlist->fields[0] = *val;
262 vlist->len++;
263 }
264
265 static GstValueList *
_gst_value_list_new(guint prealloc)266 _gst_value_list_new (guint prealloc)
267 {
268 guint n_alloc;
269 GstValueList *res;
270
271 if (prealloc == 0)
272 prealloc = 1;
273
274 n_alloc = GST_ROUND_UP_8 (prealloc);
275 res = g_malloc0 (sizeof (GstValueList) + (n_alloc - 1) * sizeof (GValue));
276
277 res->len = 0;
278 res->allocated = n_alloc;
279 res->fields = &res->arr[0];
280
281 return res;
282 }
283
284 static void
_gst_value_list_init(GValue * value,guint prealloc)285 _gst_value_list_init (GValue * value, guint prealloc)
286 {
287 value->g_type = GST_TYPE_LIST;
288 memset (value->data, 0, sizeof (value->data));
289 value->data[0].v_pointer = _gst_value_list_new (prealloc);
290 }
291
292 /**
293 * gst_value_list_init:
294 * @value: A zero-filled (uninitialized) #GValue structure
295 * @prealloc: The number of entries to pre-allocate in the list
296 *
297 * Initializes and pre-allocates a #GValue of type #GST_TYPE_LIST.
298 *
299 * Returns: (transfer none): The #GValue structure that has been passed in
300 *
301 * Since: 1.18
302 */
303
304 GValue *
gst_value_list_init(GValue * value,guint prealloc)305 gst_value_list_init (GValue * value, guint prealloc)
306 {
307 g_return_val_if_fail (value != NULL, NULL);
308 g_return_val_if_fail (G_VALUE_TYPE (value) == 0, NULL);
309
310 _gst_value_list_init (value, prealloc);
311
312 return value;
313 }
314
315 static void
_gst_value_array_init(GValue * value,guint prealloc)316 _gst_value_array_init (GValue * value, guint prealloc)
317 {
318 value->g_type = GST_TYPE_ARRAY;
319 memset (value->data, 0, sizeof (value->data));
320 value->data[0].v_pointer = _gst_value_list_new (prealloc);
321 }
322
323 /**
324 * gst_value_array_init:
325 * @value: A zero-filled (uninitialized) #GValue structure
326 * @prealloc: The number of entries to pre-allocate in the array
327 *
328 * Initializes and pre-allocates a #GValue of type #GST_TYPE_ARRAY.
329 *
330 * Returns: (transfer none): The #GValue structure that has been passed in
331 *
332 * Since: 1.18
333 */
334
335 GValue *
gst_value_array_init(GValue * value,guint prealloc)336 gst_value_array_init (GValue * value, guint prealloc)
337 {
338 g_return_val_if_fail (value != NULL, NULL);
339 g_return_val_if_fail (G_VALUE_TYPE (value) == 0, NULL);
340
341 _gst_value_array_init (value, prealloc);
342
343 return value;
344 }
345
346 /* two helper functions to serialize/stringify any type of list
347 * regular lists are done with { }, arrays with < >
348 */
349 gchar *
_priv_gst_value_serialize_any_list(const GValue * value,const gchar * begin,const gchar * end,gboolean print_type)350 _priv_gst_value_serialize_any_list (const GValue * value, const gchar * begin,
351 const gchar * end, gboolean print_type)
352 {
353 guint i;
354 GstValueList *vlist = value->data[0].v_pointer;
355 GString *s;
356 GValue *v;
357 gchar *s_val;
358 guint alen = vlist->len;
359
360 /* estimate minimum string length to minimise re-allocs in GString */
361 s = g_string_sized_new (2 + (6 * alen) + 2);
362 g_string_append (s, begin);
363 for (i = 0; i < alen; i++) {
364 v = &vlist->fields[i];
365 s_val = gst_value_serialize (v);
366 if (s_val != NULL) {
367 if (print_type) {
368 g_string_append_c (s, '(');
369 g_string_append (s, _priv_gst_value_gtype_to_abbr (G_VALUE_TYPE (v)));
370 g_string_append_c (s, ')');
371 }
372 g_string_append (s, s_val);
373 g_free (s_val);
374 if (i < alen - 1) {
375 g_string_append_len (s, ", ", 2);
376 }
377 } else {
378 GST_WARNING ("Could not serialize list/array value of type '%s'",
379 G_VALUE_TYPE_NAME (v));
380 }
381 }
382 g_string_append (s, end);
383 return g_string_free (s, FALSE);
384 }
385
386 static void
gst_value_transform_any_list_string(const GValue * src_value,GValue * dest_value,const gchar * begin,const gchar * end)387 gst_value_transform_any_list_string (const GValue * src_value,
388 GValue * dest_value, const gchar * begin, const gchar * end)
389 {
390 GValue *list_value;
391 GstValueList *array;
392 GString *s;
393 guint i;
394 gchar *list_s;
395 guint alen;
396
397 array = src_value->data[0].v_pointer;
398 alen = array->len;
399
400 /* estimate minimum string length to minimise re-allocs in GString */
401 s = g_string_sized_new (2 + (10 * alen) + 2);
402 g_string_append (s, begin);
403 for (i = 0; i < alen; i++) {
404 list_value = &array->fields[i];
405
406 if (i != 0) {
407 g_string_append_len (s, ", ", 2);
408 }
409 list_s = g_strdup_value_contents (list_value);
410 g_string_append (s, list_s);
411 g_free (list_s);
412 }
413 g_string_append (s, end);
414
415 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
416 }
417
418 static gchar *
_gst_value_serialize_g_value_array(const GValue * value,const gchar * begin,const gchar * end)419 _gst_value_serialize_g_value_array (const GValue * value, const gchar * begin,
420 const gchar * end)
421 {
422 guint i;
423 GValueArray *array = value->data[0].v_pointer;
424 GString *s;
425 GValue *v;
426 gchar *s_val;
427 guint alen = 0;
428
429 if (array)
430 alen = array->n_values;
431
432 /* estimate minimum string length to minimise re-allocs in GString */
433 s = g_string_sized_new (2 + (6 * alen) + 2);
434 g_string_append (s, begin);
435 for (i = 0; i < alen; i++) {
436 v = g_value_array_get_nth (array, i);
437 s_val = gst_value_serialize (v);
438 if (s_val != NULL) {
439 g_string_append (s, s_val);
440 g_free (s_val);
441 if (i < alen - 1) {
442 g_string_append_len (s, ", ", 2);
443 }
444 } else {
445 GST_WARNING ("Could not serialize list/array value of type '%s'",
446 G_VALUE_TYPE_NAME (v));
447 }
448 }
449 g_string_append (s, end);
450 return g_string_free (s, FALSE);
451 }
452
453 static void
_gst_value_transform_g_value_array_string(const GValue * src_value,GValue * dest_value,const gchar * begin,const gchar * end)454 _gst_value_transform_g_value_array_string (const GValue * src_value,
455 GValue * dest_value, const gchar * begin, const gchar * end)
456 {
457 GValue *list_value;
458 GValueArray *array;
459 GString *s;
460 guint i;
461 gchar *list_s;
462 guint alen;
463
464 array = src_value->data[0].v_pointer;
465 alen = array->n_values;
466
467 /* estimate minimum string length to minimise re-allocs in GString */
468 s = g_string_sized_new (2 + (10 * alen) + 2);
469 g_string_append (s, begin);
470 for (i = 0; i < alen; i++) {
471 list_value = g_value_array_get_nth (array, i);
472
473 if (i != 0) {
474 g_string_append_len (s, ", ", 2);
475 }
476 list_s = g_strdup_value_contents (list_value);
477 g_string_append (s, list_s);
478 g_free (list_s);
479 }
480 g_string_append (s, end);
481
482 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
483 }
484
485 /*
486 * helper function to see if a type is fixed. Is used internally here and
487 * there. Do not export, since it doesn't work for types where the content
488 * decides the fixedness (e.g. GST_TYPE_ARRAY).
489 */
490 static gboolean
gst_type_is_fixed(GType type)491 gst_type_is_fixed (GType type)
492 {
493 /* the basic int, string, double types */
494 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
495 return TRUE;
496 }
497 /* our fundamental types that are certainly not fixed */
498 if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
499 type == GST_TYPE_INT64_RANGE ||
500 type == GST_TYPE_LIST || type == GST_TYPE_FRACTION_RANGE ||
501 type == GST_TYPE_STRUCTURE) {
502 return FALSE;
503 }
504 /* other (boxed) types that are fixed */
505 if (type == GST_TYPE_BUFFER) {
506 return TRUE;
507 }
508 /* heavy checks */
509 if (G_TYPE_IS_FUNDAMENTAL (type) || G_TYPE_FUNDAMENTAL (type) <=
510 G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
511 return TRUE;
512 }
513
514 return FALSE;
515 }
516
517 /* GValue functions usable for both regular lists and arrays */
518 static void
gst_value_init_list_or_array(GValue * value)519 gst_value_init_list_or_array (GValue * value)
520 {
521 value->data[0].v_pointer = _gst_value_list_new (0);
522 }
523
524 static GstValueList *
copy_gst_value_list(const GstValueList * src)525 copy_gst_value_list (const GstValueList * src)
526 {
527 GstValueList *dest;
528 guint i, len;
529
530 len = src->len;
531 dest = _gst_value_list_new (len);
532 dest->len = len;
533 for (i = 0; i < len; i++) {
534 gst_value_init_and_copy (&dest->fields[i], &src->fields[i]);
535 }
536
537 return dest;
538 }
539
540 static void
gst_value_copy_list_or_array(const GValue * src_value,GValue * dest_value)541 gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
542 {
543 dest_value->data[0].v_pointer =
544 copy_gst_value_list (VALUE_LIST_ARRAY (src_value));
545 }
546
547 static void
gst_value_free_list_or_array(GValue * value)548 gst_value_free_list_or_array (GValue * value)
549 {
550 guint i, len;
551 GstValueList *src = VALUE_LIST_ARRAY (value);
552 len = src->len;
553
554 if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
555 for (i = 0; i < len; i++) {
556 g_value_unset (&src->fields[i]);
557 }
558 if (VALUE_LIST_IS_USING_DYNAMIC_ARRAY (src)) {
559 g_free (src->fields);
560 }
561 g_free (src);
562 }
563 }
564
565 static gpointer
gst_value_list_or_array_peek_pointer(const GValue * value)566 gst_value_list_or_array_peek_pointer (const GValue * value)
567 {
568 return value->data[0].v_pointer;
569 }
570
571 static gchar *
gst_value_collect_list_or_array(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)572 gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
573 GTypeCValue * collect_values, guint collect_flags)
574 {
575 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
576 value->data[0].v_pointer = collect_values[0].v_pointer;
577 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
578 } else {
579 value->data[0].v_pointer =
580 copy_gst_value_list ((GstValueList *) collect_values[0].v_pointer);
581 }
582 return NULL;
583 }
584
585 static gchar *
gst_value_lcopy_list_or_array(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)586 gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
587 GTypeCValue * collect_values, guint collect_flags)
588 {
589 GstValueList **dest = collect_values[0].v_pointer;
590
591 g_return_val_if_fail (dest != NULL,
592 g_strdup_printf ("value location for `%s' passed as NULL",
593 G_VALUE_TYPE_NAME (value)));
594 g_return_val_if_fail (value->data[0].v_pointer != NULL,
595 g_strdup_printf ("invalid value given for `%s'",
596 G_VALUE_TYPE_NAME (value)));
597
598 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
599 *dest = (GstValueList *) value->data[0].v_pointer;
600 } else {
601 *dest = copy_gst_value_list (VALUE_LIST_ARRAY (value));
602 }
603 return NULL;
604 }
605
606 static gboolean
gst_value_list_or_array_get_basic_type(const GValue * value,GType * type)607 gst_value_list_or_array_get_basic_type (const GValue * value, GType * type)
608 {
609 if (G_UNLIKELY (value == NULL))
610 return FALSE;
611
612 if (GST_VALUE_HOLDS_LIST (value) || GST_VALUE_HOLDS_ARRAY (value)) {
613 if (VALUE_LIST_SIZE (value) == 0)
614 return FALSE;
615 return gst_value_list_or_array_get_basic_type (VALUE_LIST_GET_VALUE (value,
616 0), type);
617 }
618
619 *type = G_VALUE_TYPE (value);
620
621 return TRUE;
622 }
623
624 #define IS_RANGE_COMPAT(type1,type2,t1,t2) \
625 (((t1) == (type1) && (t2) == (type2)) || ((t2) == (type1) && (t1) == (type2)))
626
627 static gboolean
gst_value_list_or_array_are_compatible(const GValue * value1,const GValue * value2)628 gst_value_list_or_array_are_compatible (const GValue * value1,
629 const GValue * value2)
630 {
631 GType basic_type1, basic_type2;
632
633 /* empty or same type is OK */
634 if (!gst_value_list_or_array_get_basic_type (value1, &basic_type1) ||
635 !gst_value_list_or_array_get_basic_type (value2, &basic_type2) ||
636 basic_type1 == basic_type2)
637 return TRUE;
638
639 /* ranges are distinct types for each bound type... */
640 if (IS_RANGE_COMPAT (G_TYPE_INT, GST_TYPE_INT_RANGE, basic_type1,
641 basic_type2))
642 return TRUE;
643 if (IS_RANGE_COMPAT (G_TYPE_INT64, GST_TYPE_INT64_RANGE, basic_type1,
644 basic_type2))
645 return TRUE;
646 if (IS_RANGE_COMPAT (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE, basic_type1,
647 basic_type2))
648 return TRUE;
649 if (IS_RANGE_COMPAT (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE, basic_type1,
650 basic_type2))
651 return TRUE;
652
653 return FALSE;
654 }
655
656 static inline void
_gst_value_list_append_and_take_value(GValue * value,GValue * append_value)657 _gst_value_list_append_and_take_value (GValue * value, GValue * append_value)
658 {
659 _gst_value_list_append_val (VALUE_LIST_ARRAY (value), append_value);
660 memset (append_value, 0, sizeof (GValue));
661 }
662
663 /**
664 * gst_value_list_append_and_take_value:
665 * @value: a #GValue of type #GST_TYPE_LIST
666 * @append_value: (transfer full): the value to append
667 *
668 * Appends @append_value to the GstValueList in @value.
669 *
670 * Since: 1.2
671 */
672 void
gst_value_list_append_and_take_value(GValue * value,GValue * append_value)673 gst_value_list_append_and_take_value (GValue * value, GValue * append_value)
674 {
675 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
676 g_return_if_fail (G_IS_VALUE (append_value));
677 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
678 append_value));
679
680 _gst_value_list_append_and_take_value (value, append_value);
681 }
682
683 /**
684 * gst_value_list_append_value:
685 * @value: a #GValue of type #GST_TYPE_LIST
686 * @append_value: (transfer none): the value to append
687 *
688 * Appends @append_value to the GstValueList in @value.
689 */
690 void
gst_value_list_append_value(GValue * value,const GValue * append_value)691 gst_value_list_append_value (GValue * value, const GValue * append_value)
692 {
693 GValue val = { 0, };
694
695 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
696 g_return_if_fail (G_IS_VALUE (append_value));
697 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
698 append_value));
699
700 gst_value_init_and_copy (&val, append_value);
701 _gst_value_list_append_val (VALUE_LIST_ARRAY (value), &val);
702 }
703
704 /**
705 * gst_value_list_prepend_value:
706 * @value: a #GValue of type #GST_TYPE_LIST
707 * @prepend_value: the value to prepend
708 *
709 * Prepends @prepend_value to the GstValueList in @value.
710 */
711 void
gst_value_list_prepend_value(GValue * value,const GValue * prepend_value)712 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
713 {
714 GValue val = { 0, };
715
716 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
717 g_return_if_fail (G_IS_VALUE (prepend_value));
718 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
719 prepend_value));
720
721 gst_value_init_and_copy (&val, prepend_value);
722 _gst_value_list_prepend_val (VALUE_LIST_ARRAY (value), &val);
723 }
724
725 /**
726 * gst_value_list_concat:
727 * @dest: (out caller-allocates): an uninitialized #GValue to take the result
728 * @value1: a #GValue
729 * @value2: a #GValue
730 *
731 * Concatenates copies of @value1 and @value2 into a list. Values that are not
732 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
733 * @dest will be initialized to the type #GST_TYPE_LIST.
734 */
735 void
gst_value_list_concat(GValue * dest,const GValue * value1,const GValue * value2)736 gst_value_list_concat (GValue * dest, const GValue * value1,
737 const GValue * value2)
738 {
739 guint i, value1_length, value2_length;
740 GstValueList *vlist;
741
742 g_return_if_fail (dest != NULL);
743 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
744 g_return_if_fail (G_IS_VALUE (value1));
745 g_return_if_fail (G_IS_VALUE (value2));
746 g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
747
748 value1_length =
749 (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
750 value2_length =
751 (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
752
753 _gst_value_list_init (dest, value1_length + value2_length);
754 vlist = VALUE_LIST_ARRAY (dest);
755 vlist->len = value1_length + value2_length;
756
757 if (GST_VALUE_HOLDS_LIST (value1)) {
758 for (i = 0; i < value1_length; i++) {
759 gst_value_init_and_copy (&vlist->fields[i],
760 VALUE_LIST_GET_VALUE (value1, i));
761 }
762 } else {
763 gst_value_init_and_copy (&vlist->fields[0], value1);
764 }
765
766 if (GST_VALUE_HOLDS_LIST (value2)) {
767 for (i = 0; i < value2_length; i++) {
768 gst_value_init_and_copy (&vlist->fields[i + value1_length],
769 VALUE_LIST_GET_VALUE (value2, i));
770 }
771 } else {
772 gst_value_init_and_copy (&vlist->fields[value1_length], value2);
773 }
774 }
775
776 /* same as gst_value_list_concat() but takes ownership of GValues */
777 static void
gst_value_list_concat_and_take_values(GValue * dest,GValue * val1,GValue * val2)778 gst_value_list_concat_and_take_values (GValue * dest, GValue * val1,
779 GValue * val2)
780 {
781 guint i, val1_length, val2_length;
782 gboolean val1_is_list;
783 gboolean val2_is_list;
784 GstValueList *vlist;
785
786 g_assert (dest != NULL);
787 g_assert (G_VALUE_TYPE (dest) == 0);
788 g_assert (G_IS_VALUE (val1));
789 g_assert (G_IS_VALUE (val2));
790 g_assert (gst_value_list_or_array_are_compatible (val1, val2));
791
792 val1_is_list = GST_VALUE_HOLDS_LIST (val1);
793 val1_length = (val1_is_list ? VALUE_LIST_SIZE (val1) : 1);
794
795 val2_is_list = GST_VALUE_HOLDS_LIST (val2);
796 val2_length = (val2_is_list ? VALUE_LIST_SIZE (val2) : 1);
797
798 /* Overidding the default initialization to have a list of the target size */
799 _gst_value_list_init (dest, val1_length + val2_length);
800 vlist = VALUE_LIST_ARRAY (dest);
801 vlist->len = val1_length + val2_length;
802
803 if (val1_is_list) {
804 for (i = 0; i < val1_length; i++) {
805 vlist->fields[i] = *VALUE_LIST_GET_VALUE (val1, i);
806 }
807 VALUE_LIST_ARRAY (val1)->len = 0;
808 g_value_unset (val1);
809 } else {
810 vlist->fields[0] = *val1;
811 G_VALUE_TYPE (val1) = G_TYPE_INVALID;
812 }
813
814 if (val2_is_list) {
815 for (i = 0; i < val2_length; i++) {
816 const GValue *v2 = VALUE_LIST_GET_VALUE (val2, i);
817 vlist->fields[i + val1_length] = *v2;
818 }
819
820 VALUE_LIST_ARRAY (val2)->len = 0;
821 g_value_unset (val2);
822 } else {
823 vlist->fields[val1_length] = *val2;
824 G_VALUE_TYPE (val2) = G_TYPE_INVALID;
825 }
826 }
827
828 /**
829 * gst_value_list_merge:
830 * @dest: (out caller-allocates): an uninitialized #GValue to take the result
831 * @value1: a #GValue
832 * @value2: a #GValue
833 *
834 * Merges copies of @value1 and @value2. Values that are not
835 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
836 *
837 * The result will be put into @dest and will either be a list that will not
838 * contain any duplicates, or a non-list type (if @value1 and @value2
839 * were equal).
840 */
841 void
gst_value_list_merge(GValue * dest,const GValue * value1,const GValue * value2)842 gst_value_list_merge (GValue * dest, const GValue * value1,
843 const GValue * value2)
844 {
845 guint i, j, k, value1_length, value2_length, skipped;
846 const GValue *src;
847 gboolean skip;
848 GstValueList *vlist;
849
850 g_return_if_fail (dest != NULL);
851 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
852 g_return_if_fail (G_IS_VALUE (value1));
853 g_return_if_fail (G_IS_VALUE (value2));
854 g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
855
856 value1_length =
857 (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
858 value2_length =
859 (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
860
861 _gst_value_list_init (dest, value1_length + value2_length);
862 vlist = VALUE_LIST_ARRAY (dest);
863 vlist->len = value1_length + value2_length;
864
865 if (GST_VALUE_HOLDS_LIST (value1)) {
866 for (i = 0; i < value1_length; i++) {
867 gst_value_init_and_copy (&vlist->fields[i], VALUE_LIST_GET_VALUE (value1,
868 i));
869 }
870 } else {
871 gst_value_init_and_copy (&vlist->fields[0], value1);
872 }
873
874 j = value1_length;
875 skipped = 0;
876 if (GST_VALUE_HOLDS_LIST (value2)) {
877 for (i = 0; i < value2_length; i++) {
878 skip = FALSE;
879 src = VALUE_LIST_GET_VALUE (value2, i);
880 for (k = 0; k < value1_length; k++) {
881 if (gst_value_compare (&vlist->fields[k], src) == GST_VALUE_EQUAL) {
882 skip = TRUE;
883 skipped++;
884 break;
885 }
886 }
887 if (!skip) {
888 gst_value_init_and_copy (&vlist->fields[j], src);
889 j++;
890 }
891 }
892 } else {
893 skip = FALSE;
894 for (k = 0; k < value1_length; k++) {
895 if (gst_value_compare (&vlist->fields[k], value2) == GST_VALUE_EQUAL) {
896 skip = TRUE;
897 skipped++;
898 break;
899 }
900 }
901 if (!skip) {
902 gst_value_init_and_copy (&vlist->fields[j], value2);
903 }
904 }
905 if (skipped) {
906 guint new_size = value1_length + (value2_length - skipped);
907
908 if (new_size > 1) {
909 /* shrink list */
910 vlist->len = new_size;
911 } else {
912 GValue single_dest;
913
914 /* size is 1, take single value in list and make it new dest */
915 single_dest = vlist->fields[0];
916
917 /* clean up old value allocations: must set array size to 0, because
918 * allocated values are not inited meaning g_value_unset() will not
919 * work on them */
920 vlist->len = 0;
921 g_value_unset (dest);
922
923 /* the single value is our new result */
924 *dest = single_dest;
925 }
926 }
927 }
928
929 /**
930 * gst_value_list_get_size:
931 * @value: a #GValue of type #GST_TYPE_LIST
932 *
933 * Gets the number of values contained in @value.
934 *
935 * Returns: the number of values
936 */
937 guint
gst_value_list_get_size(const GValue * value)938 gst_value_list_get_size (const GValue * value)
939 {
940 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
941
942 return VALUE_LIST_SIZE (value);
943 }
944
945 /**
946 * gst_value_list_get_value:
947 * @value: a #GValue of type #GST_TYPE_LIST
948 * @index: index of value to get from the list
949 *
950 * Gets the value that is a member of the list contained in @value and
951 * has the index @index.
952 *
953 * Returns: (transfer none): the value at the given index
954 */
955 const GValue *
gst_value_list_get_value(const GValue * value,guint index)956 gst_value_list_get_value (const GValue * value, guint index)
957 {
958 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
959 g_return_val_if_fail (index < VALUE_LIST_SIZE (value), NULL);
960
961 return VALUE_LIST_GET_VALUE (value, index);
962 }
963
964 /**
965 * gst_value_array_append_value:
966 * @value: a #GValue of type #GST_TYPE_ARRAY
967 * @append_value: the value to append
968 *
969 * Appends @append_value to the GstValueArray in @value.
970 */
971 void
gst_value_array_append_value(GValue * value,const GValue * append_value)972 gst_value_array_append_value (GValue * value, const GValue * append_value)
973 {
974 GValue val = { 0, };
975
976 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
977 g_return_if_fail (G_IS_VALUE (append_value));
978 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
979 append_value));
980
981 gst_value_init_and_copy (&val, append_value);
982 _gst_value_list_append_val (VALUE_LIST_ARRAY (value), &val);
983 }
984
985 static inline void
_gst_value_array_append_and_take_value(GValue * value,GValue * append_value)986 _gst_value_array_append_and_take_value (GValue * value, GValue * append_value)
987 {
988 _gst_value_list_append_val (VALUE_LIST_ARRAY (value), append_value);
989 memset (append_value, 0, sizeof (GValue));
990 }
991
992 /**
993 * gst_value_array_append_and_take_value:
994 * @value: a #GValue of type #GST_TYPE_ARRAY
995 * @append_value: (transfer full): the value to append
996 *
997 * Appends @append_value to the GstValueArray in @value.
998 *
999 * Since: 1.2
1000 */
1001 void
gst_value_array_append_and_take_value(GValue * value,GValue * append_value)1002 gst_value_array_append_and_take_value (GValue * value, GValue * append_value)
1003 {
1004 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
1005 g_return_if_fail (G_IS_VALUE (append_value));
1006 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
1007 append_value));
1008
1009 _gst_value_array_append_and_take_value (value, append_value);
1010 }
1011
1012 /**
1013 * gst_value_array_prepend_value:
1014 * @value: a #GValue of type #GST_TYPE_ARRAY
1015 * @prepend_value: the value to prepend
1016 *
1017 * Prepends @prepend_value to the GstValueArray in @value.
1018 */
1019 void
gst_value_array_prepend_value(GValue * value,const GValue * prepend_value)1020 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
1021 {
1022 GValue val = { 0, };
1023
1024 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
1025 g_return_if_fail (G_IS_VALUE (prepend_value));
1026 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
1027 prepend_value));
1028
1029 gst_value_init_and_copy (&val, prepend_value);
1030 _gst_value_list_prepend_val (VALUE_LIST_ARRAY (value), &val);
1031 }
1032
1033 /**
1034 * gst_value_array_get_size:
1035 * @value: a #GValue of type #GST_TYPE_ARRAY
1036 *
1037 * Gets the number of values contained in @value.
1038 *
1039 * Returns: the number of values
1040 */
1041 guint
gst_value_array_get_size(const GValue * value)1042 gst_value_array_get_size (const GValue * value)
1043 {
1044 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
1045
1046 return VALUE_LIST_SIZE (value);
1047 }
1048
1049 /**
1050 * gst_value_array_get_value:
1051 * @value: a #GValue of type #GST_TYPE_ARRAY
1052 * @index: index of value to get from the array
1053 *
1054 * Gets the value that is a member of the array contained in @value and
1055 * has the index @index.
1056 *
1057 * Returns: (transfer none): the value at the given index
1058 */
1059 const GValue *
gst_value_array_get_value(const GValue * value,guint index)1060 gst_value_array_get_value (const GValue * value, guint index)
1061 {
1062 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
1063 g_return_val_if_fail (index < VALUE_LIST_SIZE (value), NULL);
1064
1065 return VALUE_LIST_GET_VALUE (value, index);
1066 }
1067
1068 static void
gst_value_transform_list_string(const GValue * src_value,GValue * dest_value)1069 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
1070 {
1071 gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
1072 }
1073
1074 static void
gst_value_transform_array_string(const GValue * src_value,GValue * dest_value)1075 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
1076 {
1077 gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
1078 }
1079
1080 static void
gst_value_transform_g_value_array_string(const GValue * src_value,GValue * dest_value)1081 gst_value_transform_g_value_array_string (const GValue * src_value,
1082 GValue * dest_value)
1083 {
1084 _gst_value_transform_g_value_array_string (src_value, dest_value, "< ", " >");
1085 }
1086
1087 static void
gst_value_transform_g_value_array_any_list(const GValue * src_value,GValue * dest_value)1088 gst_value_transform_g_value_array_any_list (const GValue * src_value,
1089 GValue * dest_value)
1090 {
1091 const GValueArray *varray;
1092 GstValueList *vlist;
1093 gint i;
1094
1095 varray = g_value_get_boxed (src_value);
1096
1097 /* GLib will unset the value, memset to 0 the data instead of doing a proper
1098 * reset. That's why we need to allocate the array here */
1099 vlist = dest_value->data[0].v_pointer =
1100 _gst_value_list_new (varray->n_values);
1101
1102 for (i = 0; i < varray->n_values; i++) {
1103 GValue val = G_VALUE_INIT;
1104 gst_value_init_and_copy (&val, &varray->values[i]);
1105 _gst_value_list_append_val (vlist, &val);
1106 }
1107 }
1108
1109 static void
gst_value_transform_any_list_g_value_array(const GValue * src_value,GValue * dest_value)1110 gst_value_transform_any_list_g_value_array (const GValue * src_value,
1111 GValue * dest_value)
1112 {
1113 GValueArray *varray;
1114 GstValueList *vlist;
1115 gint i;
1116
1117 vlist = VALUE_LIST_ARRAY (src_value);
1118 varray = g_value_array_new (vlist->len);
1119
1120 for (i = 0; i < vlist->len; i++)
1121 g_value_array_append (varray, &vlist->fields[i]);
1122
1123 g_value_take_boxed (dest_value, varray);
1124 }
1125
1126 /* Do an unordered compare of the contents of a list */
1127 static gint
gst_value_compare_value_list(const GValue * value1,const GValue * value2)1128 gst_value_compare_value_list (const GValue * value1, const GValue * value2)
1129 {
1130 guint i, j;
1131 GstValueList *vlist1 = VALUE_LIST_ARRAY (value1);
1132 GstValueList *vlist2 = VALUE_LIST_ARRAY (value2);
1133 GValue *v1;
1134 GValue *v2;
1135 gint len, to_remove;
1136 guint8 *removed;
1137 GstValueCompareFunc compare;
1138
1139 /* get length and do initial length check. */
1140 len = vlist1->len;
1141 if (len != vlist2->len)
1142 return GST_VALUE_UNORDERED;
1143
1144 /* Empty lists are equal */
1145 if (len == 0)
1146 return GST_VALUE_EQUAL;
1147
1148 /* We know lists are not empty. do sanity check on first values */
1149 if (G_VALUE_TYPE (&vlist1->fields[0]) != G_VALUE_TYPE (&vlist2->fields[0]))
1150 return GST_VALUE_UNORDERED;
1151
1152 /* Get the compare function */
1153 if (!(compare = gst_value_get_compare_func (&vlist1->fields[0])))
1154 return GST_VALUE_UNORDERED;
1155
1156 /* place to mark removed value indices of array2 */
1157 removed = g_newa (guint8, len);
1158 memset (removed, 0, len);
1159 to_remove = len;
1160
1161 /* loop over array1, all items should be in array2. When we find an
1162 * item in array2, remove it from array2 by marking it as removed */
1163 for (i = 0; i < len; i++) {
1164 v1 = &vlist1->fields[i];
1165
1166 for (j = 0; j < len; j++) {
1167 /* item is removed, we can skip it */
1168 if (removed[j])
1169 continue;
1170 v2 = &vlist2->fields[j];
1171 /* Note: compare function can be called directly since we know the types
1172 * are identical */
1173 if (compare (v1, v2) == GST_VALUE_EQUAL) {
1174 /* mark item as removed now that we found it in array2 and
1175 * decrement the number of remaining items in array2. */
1176 removed[j] = 1;
1177 to_remove--;
1178 break;
1179 }
1180 }
1181 /* item in array1 and not in array2, UNORDERED */
1182 if (j == len)
1183 return GST_VALUE_UNORDERED;
1184 }
1185 /* if not all items were removed, array2 contained something not in array1 */
1186 if (to_remove != 0)
1187 return GST_VALUE_UNORDERED;
1188
1189 /* arrays are equal */
1190 return GST_VALUE_EQUAL;
1191 }
1192
1193 /* Perform an ordered comparison of the contents of an array */
1194 static gint
gst_value_compare_value_array(const GValue * value1,const GValue * value2)1195 gst_value_compare_value_array (const GValue * value1, const GValue * value2)
1196 {
1197 guint i;
1198 GstValueList *vlist1 = VALUE_LIST_ARRAY (value1);
1199 GstValueList *vlist2 = VALUE_LIST_ARRAY (value2);
1200 guint len = vlist1->len;
1201 GValue *v1;
1202 GValue *v2;
1203
1204 if (len != vlist2->len)
1205 return GST_VALUE_UNORDERED;
1206
1207 for (i = 0; i < len; i++) {
1208 v1 = &vlist1->fields[i];
1209 v2 = &vlist2->fields[i];
1210 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
1211 return GST_VALUE_UNORDERED;
1212 }
1213
1214 return GST_VALUE_EQUAL;
1215 }
1216
1217 static gint
gst_value_compare_g_value_array(const GValue * value1,const GValue * value2)1218 gst_value_compare_g_value_array (const GValue * value1, const GValue * value2)
1219 {
1220 guint i;
1221 GValueArray *array1 = value1->data[0].v_pointer;
1222 GValueArray *array2 = value2->data[0].v_pointer;
1223 guint len = array1 ? array1->n_values : 0;
1224 GValue *v1;
1225 GValue *v2;
1226
1227 if (len != (array2 ? array2->n_values : 0))
1228 return GST_VALUE_UNORDERED;
1229
1230 for (i = 0; i < len; i++) {
1231 v1 = g_value_array_get_nth (array1, i);
1232 v2 = g_value_array_get_nth (array2, i);
1233 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
1234 return GST_VALUE_UNORDERED;
1235 }
1236
1237 return GST_VALUE_EQUAL;
1238 }
1239
1240 static gchar *
gst_value_serialize_value_list(const GValue * value)1241 gst_value_serialize_value_list (const GValue * value)
1242 {
1243 return _priv_gst_value_serialize_any_list (value, "{ ", " }", TRUE);
1244 }
1245
1246 static gboolean
gst_value_deserialize_value_list(GValue * dest,const gchar * s,GParamSpec * pspec)1247 gst_value_deserialize_value_list (GValue * dest, const gchar * s,
1248 GParamSpec * pspec)
1249 {
1250 gchar *s2 = (gchar *) s;
1251 return _priv_gst_value_parse_list (s2, &s2, dest, G_TYPE_INVALID, pspec);
1252 }
1253
1254 static gchar *
gst_value_serialize_value_array(const GValue * value)1255 gst_value_serialize_value_array (const GValue * value)
1256 {
1257 return _priv_gst_value_serialize_any_list (value, "< ", " >", TRUE);
1258 }
1259
1260 static gboolean
gst_value_deserialize_value_array(GValue * dest,const gchar * s,GParamSpec * pspec)1261 gst_value_deserialize_value_array (GValue * dest, const gchar * s,
1262 GParamSpec * pspec)
1263 {
1264 gchar *s2 = (gchar *) s;
1265 return _priv_gst_value_parse_array (s2, &s2, dest, G_TYPE_INVALID, pspec);
1266 }
1267
1268 static gchar *
gst_value_serialize_g_value_array(const GValue * value)1269 gst_value_serialize_g_value_array (const GValue * value)
1270 {
1271 return _gst_value_serialize_g_value_array (value, "< ", " >");
1272 }
1273
1274 static gboolean
gst_value_deserialize_g_value_array(GValue * dest,const gchar * s)1275 gst_value_deserialize_g_value_array (GValue * dest, const gchar * s)
1276 {
1277 g_warning ("gst_value_deserialize_g_value_array: unimplemented");
1278 return FALSE;
1279 }
1280
1281 /*************
1282 * int range *
1283 *
1284 * Values in the range are defined as any value greater or equal
1285 * to min*step, AND lesser or equal to max*step.
1286 * For step == 1, this falls back to the traditional range semantics.
1287 *
1288 * data[0] = (min << 32) | (max)
1289 * data[1] = step
1290 *
1291 *************/
1292
1293 #define INT_RANGE_MIN(v) ((gint) (((v)->data[0].v_uint64) >> 32))
1294 #define INT_RANGE_MAX(v) ((gint) (((v)->data[0].v_uint64) & 0xffffffff))
1295 #define INT_RANGE_STEP(v) ((v)->data[1].v_int)
1296
1297 static void
gst_value_init_int_range(GValue * value)1298 gst_value_init_int_range (GValue * value)
1299 {
1300 G_STATIC_ASSERT (sizeof (gint) <= 2 * sizeof (guint64));
1301
1302 value->data[0].v_uint64 = 0;
1303 value->data[1].v_int = 1;
1304 }
1305
1306 static void
gst_value_copy_int_range(const GValue * src_value,GValue * dest_value)1307 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
1308 {
1309 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
1310 dest_value->data[1].v_int = src_value->data[1].v_int;
1311 }
1312
1313 static gchar *
gst_value_collect_int_range(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)1314 gst_value_collect_int_range (GValue * value, guint n_collect_values,
1315 GTypeCValue * collect_values, guint collect_flags)
1316 {
1317 g_return_val_if_fail (n_collect_values == 2,
1318 g_strdup_printf ("not enough value locations for `%s' passed",
1319 G_VALUE_TYPE_NAME (value)));
1320 g_return_val_if_fail (collect_values[0].v_int < collect_values[1].v_int,
1321 g_strdup_printf ("range start is not smaller than end for `%s'",
1322 G_VALUE_TYPE_NAME (value)));
1323
1324 gst_value_set_int_range_step (value, collect_values[0].v_int,
1325 collect_values[1].v_int, 1);
1326
1327 return NULL;
1328 }
1329
1330 static gchar *
gst_value_lcopy_int_range(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)1331 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
1332 GTypeCValue * collect_values, guint collect_flags)
1333 {
1334 guint32 *int_range_start = collect_values[0].v_pointer;
1335 guint32 *int_range_end = collect_values[1].v_pointer;
1336
1337 g_return_val_if_fail (int_range_start != NULL,
1338 g_strdup_printf ("start value location for `%s' passed as NULL",
1339 G_VALUE_TYPE_NAME (value)));
1340 g_return_val_if_fail (int_range_end != NULL,
1341 g_strdup_printf ("end value location for `%s' passed as NULL",
1342 G_VALUE_TYPE_NAME (value)));
1343
1344 *int_range_start = INT_RANGE_MIN (value);
1345 *int_range_end = INT_RANGE_MAX (value);
1346
1347 return NULL;
1348 }
1349
1350 /**
1351 * gst_value_set_int_range_step:
1352 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1353 * @start: the start of the range
1354 * @end: the end of the range
1355 * @step: the step of the range
1356 *
1357 * Sets @value to the range specified by @start, @end and @step.
1358 */
1359 void
gst_value_set_int_range_step(GValue * value,gint start,gint end,gint step)1360 gst_value_set_int_range_step (GValue * value, gint start, gint end, gint step)
1361 {
1362 guint64 sstart, sstop;
1363
1364 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
1365 g_return_if_fail (start < end);
1366 g_return_if_fail (step > 0);
1367 g_return_if_fail (start % step == 0);
1368 g_return_if_fail (end % step == 0);
1369
1370 sstart = (guint) (start / step);
1371 sstop = (guint) (end / step);
1372 value->data[0].v_uint64 = (sstart << 32) | sstop;
1373 value->data[1].v_int = step;
1374 }
1375
1376 /**
1377 * gst_value_set_int_range:
1378 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1379 * @start: the start of the range
1380 * @end: the end of the range
1381 *
1382 * Sets @value to the range specified by @start and @end.
1383 */
1384 void
gst_value_set_int_range(GValue * value,gint start,gint end)1385 gst_value_set_int_range (GValue * value, gint start, gint end)
1386 {
1387 gst_value_set_int_range_step (value, start, end, 1);
1388 }
1389
1390 /**
1391 * gst_value_get_int_range_min:
1392 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1393 *
1394 * Gets the minimum of the range specified by @value.
1395 *
1396 * Returns: the minimum of the range
1397 */
1398 gint
gst_value_get_int_range_min(const GValue * value)1399 gst_value_get_int_range_min (const GValue * value)
1400 {
1401 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1402
1403 return INT_RANGE_MIN (value) * INT_RANGE_STEP (value);
1404 }
1405
1406 /**
1407 * gst_value_get_int_range_max:
1408 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1409 *
1410 * Gets the maximum of the range specified by @value.
1411 *
1412 * Returns: the maximum of the range
1413 */
1414 gint
gst_value_get_int_range_max(const GValue * value)1415 gst_value_get_int_range_max (const GValue * value)
1416 {
1417 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1418
1419 return INT_RANGE_MAX (value) * INT_RANGE_STEP (value);
1420 }
1421
1422 /**
1423 * gst_value_get_int_range_step:
1424 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1425 *
1426 * Gets the step of the range specified by @value.
1427 *
1428 * Returns: the step of the range
1429 */
1430 gint
gst_value_get_int_range_step(const GValue * value)1431 gst_value_get_int_range_step (const GValue * value)
1432 {
1433 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1434
1435 return INT_RANGE_STEP (value);
1436 }
1437
1438 static void
gst_value_transform_int_range_string(const GValue * src_value,GValue * dest_value)1439 gst_value_transform_int_range_string (const GValue * src_value,
1440 GValue * dest_value)
1441 {
1442 if (INT_RANGE_STEP (src_value) == 1)
1443 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
1444 INT_RANGE_MIN (src_value), INT_RANGE_MAX (src_value));
1445 else
1446 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d,%d]",
1447 INT_RANGE_MIN (src_value) * INT_RANGE_STEP (src_value),
1448 INT_RANGE_MAX (src_value) * INT_RANGE_STEP (src_value),
1449 INT_RANGE_STEP (src_value));
1450 }
1451
1452 static gint
gst_value_compare_int_range(const GValue * value1,const GValue * value2)1453 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
1454 {
1455 #if 0
1456 /* Compare the ranges. (Kept for clarity for the below comparision) */
1457 if (INT_RANGE_MIN (value1) != INT_RANGE_MIN (value2) ||
1458 INT_RANGE_MAX (value1) != INT_RANGE_MAX (value2))
1459 return GST_VALUE_UNORDERED;
1460 #else
1461 /* The MIN and MAX of the range are actually stored packed into one 64bit
1462 * value. We can therefore compare them directly */
1463 if (value1->data[0].v_uint64 != value2->data[0].v_uint64)
1464 return GST_VALUE_UNORDERED;
1465 #endif
1466
1467 /* The extents are equal */
1468 /* If there is only one value (min == max), we ignore the step for
1469 * comparison */
1470 if (INT_RANGE_MIN (value1) == INT_RANGE_MAX (value1))
1471 return GST_VALUE_EQUAL;
1472
1473 /* Else the ranges are only equal if their step is also equal */
1474 if (INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2))
1475 return GST_VALUE_EQUAL;
1476 return GST_VALUE_UNORDERED;
1477 }
1478
1479 static gchar *
gst_value_serialize_int_range(const GValue * value)1480 gst_value_serialize_int_range (const GValue * value)
1481 {
1482 if (INT_RANGE_STEP (value) == 1)
1483 return g_strdup_printf ("[ %d, %d ]", INT_RANGE_MIN (value),
1484 INT_RANGE_MAX (value));
1485 else
1486 return g_strdup_printf ("[ %d, %d, %d ]",
1487 INT_RANGE_MIN (value) * INT_RANGE_STEP (value),
1488 INT_RANGE_MAX (value) * INT_RANGE_STEP (value), INT_RANGE_STEP (value));
1489 }
1490
1491 static gboolean
gst_value_deserialize_int_range(GValue * dest,const gchar * s)1492 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
1493 {
1494 g_warning ("unimplemented");
1495 return FALSE;
1496 }
1497
1498 /***************
1499 * int64 range *
1500 *
1501 * Values in the range are defined as any value greater or equal
1502 * to min*step, AND lesser or equal to max*step.
1503 * For step == 1, this falls back to the traditional range semantics.
1504 ***************/
1505
1506 #define INT64_RANGE_MIN(v) (((gint64 *)((v)->data[0].v_pointer))[0])
1507 #define INT64_RANGE_MAX(v) (((gint64 *)((v)->data[0].v_pointer))[1])
1508 #define INT64_RANGE_STEP(v) (((gint64 *)((v)->data[0].v_pointer))[2])
1509
1510 static void
gst_value_init_int64_range(GValue * value)1511 gst_value_init_int64_range (GValue * value)
1512 {
1513 gint64 *vals = g_slice_alloc0 (3 * sizeof (gint64));
1514 value->data[0].v_pointer = vals;
1515 INT64_RANGE_MIN (value) = 0;
1516 INT64_RANGE_MAX (value) = 0;
1517 INT64_RANGE_STEP (value) = 1;
1518 }
1519
1520 static void
gst_value_free_int64_range(GValue * value)1521 gst_value_free_int64_range (GValue * value)
1522 {
1523 g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1524 g_slice_free1 (3 * sizeof (gint64), value->data[0].v_pointer);
1525 value->data[0].v_pointer = NULL;
1526 }
1527
1528 static void
gst_value_copy_int64_range(const GValue * src_value,GValue * dest_value)1529 gst_value_copy_int64_range (const GValue * src_value, GValue * dest_value)
1530 {
1531 gint64 *vals = (gint64 *) dest_value->data[0].v_pointer;
1532 gint64 *src_vals = (gint64 *) src_value->data[0].v_pointer;
1533
1534 if (vals == NULL) {
1535 gst_value_init_int64_range (dest_value);
1536 }
1537
1538 if (src_vals != NULL) {
1539 INT64_RANGE_MIN (dest_value) = INT64_RANGE_MIN (src_value);
1540 INT64_RANGE_MAX (dest_value) = INT64_RANGE_MAX (src_value);
1541 INT64_RANGE_STEP (dest_value) = INT64_RANGE_STEP (src_value);
1542 }
1543 }
1544
1545 static gchar *
gst_value_collect_int64_range(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)1546 gst_value_collect_int64_range (GValue * value, guint n_collect_values,
1547 GTypeCValue * collect_values, guint collect_flags)
1548 {
1549 gint64 *vals = value->data[0].v_pointer;
1550
1551 g_return_val_if_fail (n_collect_values == 2,
1552 g_strdup_printf ("not enough value locations for `%s' passed",
1553 G_VALUE_TYPE_NAME (value)));
1554
1555 g_return_val_if_fail (collect_values[0].v_int64 < collect_values[1].v_int64,
1556 g_strdup_printf ("range start is not smaller than end for `%s'",
1557 G_VALUE_TYPE_NAME (value)));
1558
1559 if (vals == NULL) {
1560 gst_value_init_int64_range (value);
1561 }
1562
1563 gst_value_set_int64_range_step (value, collect_values[0].v_int64,
1564 collect_values[1].v_int64, 1);
1565
1566 return NULL;
1567 }
1568
1569 static gchar *
gst_value_lcopy_int64_range(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)1570 gst_value_lcopy_int64_range (const GValue * value, guint n_collect_values,
1571 GTypeCValue * collect_values, guint collect_flags)
1572 {
1573 guint64 *int_range_start = collect_values[0].v_pointer;
1574 guint64 *int_range_end = collect_values[1].v_pointer;
1575 guint64 *int_range_step = collect_values[2].v_pointer;
1576 gint64 *vals = (gint64 *) value->data[0].v_pointer;
1577
1578 g_return_val_if_fail (int_range_start != NULL,
1579 g_strdup_printf ("start value location for `%s' passed as NULL",
1580 G_VALUE_TYPE_NAME (value)));
1581 g_return_val_if_fail (int_range_end != NULL,
1582 g_strdup_printf ("end value location for `%s' passed as NULL",
1583 G_VALUE_TYPE_NAME (value)));
1584 g_return_val_if_fail (int_range_step != NULL,
1585 g_strdup_printf ("step value location for `%s' passed as NULL",
1586 G_VALUE_TYPE_NAME (value)));
1587
1588 g_return_val_if_fail (vals != NULL,
1589 g_strdup_printf ("Uninitialised `%s' passed", G_VALUE_TYPE_NAME (value)));
1590
1591 *int_range_start = INT64_RANGE_MIN (value);
1592 *int_range_end = INT64_RANGE_MAX (value);
1593 *int_range_step = INT64_RANGE_STEP (value);
1594
1595 return NULL;
1596 }
1597
1598 /**
1599 * gst_value_set_int64_range_step:
1600 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1601 * @start: the start of the range
1602 * @end: the end of the range
1603 * @step: the step of the range
1604 *
1605 * Sets @value to the range specified by @start, @end and @step.
1606 */
1607 void
gst_value_set_int64_range_step(GValue * value,gint64 start,gint64 end,gint64 step)1608 gst_value_set_int64_range_step (GValue * value, gint64 start, gint64 end,
1609 gint64 step)
1610 {
1611 g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1612 g_return_if_fail (start < end);
1613 g_return_if_fail (step > 0);
1614 g_return_if_fail (start % step == 0);
1615 g_return_if_fail (end % step == 0);
1616
1617 INT64_RANGE_MIN (value) = start / step;
1618 INT64_RANGE_MAX (value) = end / step;
1619 INT64_RANGE_STEP (value) = step;
1620 }
1621
1622 /**
1623 * gst_value_set_int64_range:
1624 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1625 * @start: the start of the range
1626 * @end: the end of the range
1627 *
1628 * Sets @value to the range specified by @start and @end.
1629 */
1630 void
gst_value_set_int64_range(GValue * value,gint64 start,gint64 end)1631 gst_value_set_int64_range (GValue * value, gint64 start, gint64 end)
1632 {
1633 gst_value_set_int64_range_step (value, start, end, 1);
1634 }
1635
1636 /**
1637 * gst_value_get_int64_range_min:
1638 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1639 *
1640 * Gets the minimum of the range specified by @value.
1641 *
1642 * Returns: the minimum of the range
1643 */
1644 gint64
gst_value_get_int64_range_min(const GValue * value)1645 gst_value_get_int64_range_min (const GValue * value)
1646 {
1647 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1648
1649 return INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value);
1650 }
1651
1652 /**
1653 * gst_value_get_int64_range_max:
1654 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1655 *
1656 * Gets the maximum of the range specified by @value.
1657 *
1658 * Returns: the maximum of the range
1659 */
1660 gint64
gst_value_get_int64_range_max(const GValue * value)1661 gst_value_get_int64_range_max (const GValue * value)
1662 {
1663 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1664
1665 return INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value);
1666 }
1667
1668 /**
1669 * gst_value_get_int64_range_step:
1670 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1671 *
1672 * Gets the step of the range specified by @value.
1673 *
1674 * Returns: the step of the range
1675 */
1676 gint64
gst_value_get_int64_range_step(const GValue * value)1677 gst_value_get_int64_range_step (const GValue * value)
1678 {
1679 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1680
1681 return INT64_RANGE_STEP (value);
1682 }
1683
1684 static void
gst_value_transform_int64_range_string(const GValue * src_value,GValue * dest_value)1685 gst_value_transform_int64_range_string (const GValue * src_value,
1686 GValue * dest_value)
1687 {
1688 if (INT64_RANGE_STEP (src_value) == 1)
1689 dest_value->data[0].v_pointer =
1690 g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT "]",
1691 INT64_RANGE_MIN (src_value), INT64_RANGE_MAX (src_value));
1692 else
1693 dest_value->data[0].v_pointer =
1694 g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT
1695 ",%" G_GINT64_FORMAT "]",
1696 INT64_RANGE_MIN (src_value) * INT64_RANGE_STEP (src_value),
1697 INT64_RANGE_MAX (src_value) * INT64_RANGE_STEP (src_value),
1698 INT64_RANGE_STEP (src_value));
1699 }
1700
1701 static gint
gst_value_compare_int64_range(const GValue * value1,const GValue * value2)1702 gst_value_compare_int64_range (const GValue * value1, const GValue * value2)
1703 {
1704 /* Compare the ranges. */
1705 if (INT64_RANGE_MIN (value1) != INT64_RANGE_MIN (value2) ||
1706 INT64_RANGE_MAX (value1) != INT64_RANGE_MAX (value2))
1707 return GST_VALUE_UNORDERED;
1708
1709 /* The extents are equal */
1710 /* If there is only one value (min == max), we ignore the step for
1711 * comparison */
1712 if (INT64_RANGE_MIN (value1) == INT64_RANGE_MAX (value1))
1713 return GST_VALUE_EQUAL;
1714
1715 /* Else the ranges are only equal if their step is also equal */
1716 if (INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2))
1717 return GST_VALUE_EQUAL;
1718 return GST_VALUE_UNORDERED;
1719 }
1720
1721 static gchar *
gst_value_serialize_int64_range(const GValue * value)1722 gst_value_serialize_int64_range (const GValue * value)
1723 {
1724 if (INT64_RANGE_STEP (value) == 1)
1725 return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT " ]",
1726 INT64_RANGE_MIN (value), INT64_RANGE_MAX (value));
1727 else
1728 return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT ", %"
1729 G_GINT64_FORMAT " ]",
1730 INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value),
1731 INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value),
1732 INT64_RANGE_STEP (value));
1733 }
1734
1735 static gboolean
gst_value_deserialize_int64_range(GValue * dest,const gchar * s)1736 gst_value_deserialize_int64_range (GValue * dest, const gchar * s)
1737 {
1738 g_warning ("unimplemented");
1739 return FALSE;
1740 }
1741
1742 /****************
1743 * double range *
1744 ****************/
1745
1746 static void
gst_value_init_double_range(GValue * value)1747 gst_value_init_double_range (GValue * value)
1748 {
1749 value->data[0].v_double = 0;
1750 value->data[1].v_double = 0;
1751 }
1752
1753 static void
gst_value_copy_double_range(const GValue * src_value,GValue * dest_value)1754 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
1755 {
1756 dest_value->data[0].v_double = src_value->data[0].v_double;
1757 dest_value->data[1].v_double = src_value->data[1].v_double;
1758 }
1759
1760 static gchar *
gst_value_collect_double_range(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)1761 gst_value_collect_double_range (GValue * value, guint n_collect_values,
1762 GTypeCValue * collect_values, guint collect_flags)
1763 {
1764 g_return_val_if_fail (n_collect_values == 2,
1765 g_strdup_printf ("not enough value locations for `%s' passed",
1766 G_VALUE_TYPE_NAME (value)));
1767 g_return_val_if_fail (collect_values[0].v_double < collect_values[1].v_double,
1768 g_strdup_printf ("range start is not smaller than end for `%s'",
1769 G_VALUE_TYPE_NAME (value)));
1770
1771 value->data[0].v_double = collect_values[0].v_double;
1772 value->data[1].v_double = collect_values[1].v_double;
1773
1774 return NULL;
1775 }
1776
1777 static gchar *
gst_value_lcopy_double_range(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)1778 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
1779 GTypeCValue * collect_values, guint collect_flags)
1780 {
1781 gdouble *double_range_start = collect_values[0].v_pointer;
1782 gdouble *double_range_end = collect_values[1].v_pointer;
1783
1784 g_return_val_if_fail (double_range_start != NULL,
1785 g_strdup_printf ("start value location for `%s' passed as NULL",
1786 G_VALUE_TYPE_NAME (value)));
1787 g_return_val_if_fail (double_range_end != NULL,
1788 g_strdup_printf ("end value location for `%s' passed as NULL",
1789 G_VALUE_TYPE_NAME (value)));
1790
1791 *double_range_start = value->data[0].v_double;
1792 *double_range_end = value->data[1].v_double;
1793
1794 return NULL;
1795 }
1796
1797 /**
1798 * gst_value_set_double_range:
1799 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1800 * @start: the start of the range
1801 * @end: the end of the range
1802 *
1803 * Sets @value to the range specified by @start and @end.
1804 */
1805 void
gst_value_set_double_range(GValue * value,gdouble start,gdouble end)1806 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
1807 {
1808 g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
1809 g_return_if_fail (start < end);
1810
1811 value->data[0].v_double = start;
1812 value->data[1].v_double = end;
1813 }
1814
1815 /**
1816 * gst_value_get_double_range_min:
1817 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1818 *
1819 * Gets the minimum of the range specified by @value.
1820 *
1821 * Returns: the minimum of the range
1822 */
1823 gdouble
gst_value_get_double_range_min(const GValue * value)1824 gst_value_get_double_range_min (const GValue * value)
1825 {
1826 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1827
1828 return value->data[0].v_double;
1829 }
1830
1831 /**
1832 * gst_value_get_double_range_max:
1833 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1834 *
1835 * Gets the maximum of the range specified by @value.
1836 *
1837 * Returns: the maximum of the range
1838 */
1839 gdouble
gst_value_get_double_range_max(const GValue * value)1840 gst_value_get_double_range_max (const GValue * value)
1841 {
1842 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1843
1844 return value->data[1].v_double;
1845 }
1846
1847 static void
gst_value_transform_double_range_string(const GValue * src_value,GValue * dest_value)1848 gst_value_transform_double_range_string (const GValue * src_value,
1849 GValue * dest_value)
1850 {
1851 gchar s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
1852
1853 dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
1854 g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
1855 src_value->data[0].v_double),
1856 g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
1857 src_value->data[1].v_double));
1858 }
1859
1860 static gint
gst_value_compare_double_range(const GValue * value1,const GValue * value2)1861 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
1862 {
1863 if (value2->data[0].v_double == value1->data[0].v_double &&
1864 value2->data[1].v_double == value1->data[1].v_double)
1865 return GST_VALUE_EQUAL;
1866 return GST_VALUE_UNORDERED;
1867 }
1868
1869 static gchar *
gst_value_serialize_double_range(const GValue * value)1870 gst_value_serialize_double_range (const GValue * value)
1871 {
1872 gchar d1[G_ASCII_DTOSTR_BUF_SIZE];
1873 gchar d2[G_ASCII_DTOSTR_BUF_SIZE];
1874
1875 g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1876 g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
1877 return g_strdup_printf ("[ %s, %s ]", d1, d2);
1878 }
1879
1880 static gboolean
gst_value_deserialize_double_range(GValue * dest,const gchar * s)1881 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
1882 {
1883 g_warning ("unimplemented");
1884 return FALSE;
1885 }
1886
1887 /****************
1888 * fraction range *
1889 ****************/
1890
1891 static void
gst_value_init_fraction_range(GValue * value)1892 gst_value_init_fraction_range (GValue * value)
1893 {
1894 GValue *vals;
1895 GType ftype;
1896
1897 ftype = GST_TYPE_FRACTION;
1898
1899 value->data[0].v_pointer = vals = g_slice_alloc0 (2 * sizeof (GValue));
1900 g_value_init (&vals[0], ftype);
1901 g_value_init (&vals[1], ftype);
1902 }
1903
1904 static void
gst_value_free_fraction_range(GValue * value)1905 gst_value_free_fraction_range (GValue * value)
1906 {
1907 GValue *vals = (GValue *) value->data[0].v_pointer;
1908
1909 if (vals != NULL) {
1910 /* we know the two values contain fractions without internal allocs */
1911 /* g_value_unset (&vals[0]); */
1912 /* g_value_unset (&vals[1]); */
1913 g_slice_free1 (2 * sizeof (GValue), vals);
1914 value->data[0].v_pointer = NULL;
1915 }
1916 }
1917
1918 static void
gst_value_copy_fraction_range(const GValue * src_value,GValue * dest_value)1919 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
1920 {
1921 GValue *vals = (GValue *) dest_value->data[0].v_pointer;
1922 GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
1923
1924 if (vals == NULL) {
1925 gst_value_init_fraction_range (dest_value);
1926 vals = dest_value->data[0].v_pointer;
1927 }
1928 if (src_vals != NULL) {
1929 g_value_copy (&src_vals[0], &vals[0]);
1930 g_value_copy (&src_vals[1], &vals[1]);
1931 }
1932 }
1933
1934 static gchar *
gst_value_collect_fraction_range(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)1935 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1936 GTypeCValue * collect_values, guint collect_flags)
1937 {
1938 GValue *vals = (GValue *) value->data[0].v_pointer;
1939
1940 g_return_val_if_fail (n_collect_values == 4,
1941 g_strdup_printf ("not enough value locations for `%s' passed",
1942 G_VALUE_TYPE_NAME (value)));
1943 g_return_val_if_fail (collect_values[1].v_int != 0,
1944 g_strdup_printf ("passed '0' as first denominator for `%s'",
1945 G_VALUE_TYPE_NAME (value)));
1946 g_return_val_if_fail (collect_values[3].v_int != 0,
1947 g_strdup_printf ("passed '0' as second denominator for `%s'",
1948 G_VALUE_TYPE_NAME (value)));
1949 g_return_val_if_fail (gst_util_fraction_compare (collect_values[0].v_int,
1950 collect_values[1].v_int, collect_values[2].v_int,
1951 collect_values[3].v_int) < 0,
1952 g_strdup_printf ("range start is not smaller than end for `%s'",
1953 G_VALUE_TYPE_NAME (value)));
1954
1955 if (vals == NULL) {
1956 gst_value_init_fraction_range (value);
1957 vals = value->data[0].v_pointer;
1958 }
1959
1960 gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1961 collect_values[1].v_int);
1962 gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1963 collect_values[3].v_int);
1964
1965 return NULL;
1966 }
1967
1968 static gchar *
gst_value_lcopy_fraction_range(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)1969 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1970 GTypeCValue * collect_values, guint collect_flags)
1971 {
1972 gint i;
1973 gint *dest_values[4];
1974 GValue *vals = (GValue *) value->data[0].v_pointer;
1975
1976 g_return_val_if_fail (n_collect_values == 4,
1977 g_strdup_printf ("not enough value locations for `%s' passed",
1978 G_VALUE_TYPE_NAME (value)));
1979 g_return_val_if_fail (vals != NULL,
1980 g_strdup_printf ("Uninitialised `%s' passed", G_VALUE_TYPE_NAME (value)));
1981
1982 for (i = 0; i < 4; i++) {
1983 g_return_val_if_fail (collect_values[i].v_pointer != NULL,
1984 g_strdup_printf ("value location for `%s' passed as NULL",
1985 G_VALUE_TYPE_NAME (value)));
1986
1987 dest_values[i] = collect_values[i].v_pointer;
1988 }
1989
1990 dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1991 dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1992 dest_values[2][0] = gst_value_get_fraction_numerator (&vals[1]);
1993 dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1994 return NULL;
1995 }
1996
1997 /**
1998 * gst_value_set_fraction_range:
1999 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
2000 * @start: the start of the range (a GST_TYPE_FRACTION GValue)
2001 * @end: the end of the range (a GST_TYPE_FRACTION GValue)
2002 *
2003 * Sets @value to the range specified by @start and @end.
2004 */
2005 void
gst_value_set_fraction_range(GValue * value,const GValue * start,const GValue * end)2006 gst_value_set_fraction_range (GValue * value, const GValue * start,
2007 const GValue * end)
2008 {
2009 GValue *vals;
2010
2011 g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
2012 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (start));
2013 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (end));
2014 g_return_if_fail (gst_util_fraction_compare (start->data[0].v_int,
2015 start->data[1].v_int, end->data[0].v_int, end->data[1].v_int) < 0);
2016
2017 vals = (GValue *) value->data[0].v_pointer;
2018 if (vals == NULL) {
2019 gst_value_init_fraction_range (value);
2020 vals = value->data[0].v_pointer;
2021 }
2022 g_value_copy (start, &vals[0]);
2023 g_value_copy (end, &vals[1]);
2024 }
2025
2026 /**
2027 * gst_value_set_fraction_range_full:
2028 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
2029 * @numerator_start: the numerator start of the range
2030 * @denominator_start: the denominator start of the range
2031 * @numerator_end: the numerator end of the range
2032 * @denominator_end: the denominator end of the range
2033 *
2034 * Sets @value to the range specified by @numerator_start/@denominator_start
2035 * and @numerator_end/@denominator_end.
2036 */
2037 void
gst_value_set_fraction_range_full(GValue * value,gint numerator_start,gint denominator_start,gint numerator_end,gint denominator_end)2038 gst_value_set_fraction_range_full (GValue * value,
2039 gint numerator_start, gint denominator_start,
2040 gint numerator_end, gint denominator_end)
2041 {
2042 GValue start = { 0 };
2043 GValue end = { 0 };
2044
2045 g_return_if_fail (value != NULL);
2046 g_return_if_fail (denominator_start != 0);
2047 g_return_if_fail (denominator_end != 0);
2048 g_return_if_fail (gst_util_fraction_compare (numerator_start,
2049 denominator_start, numerator_end, denominator_end) < 0);
2050
2051 g_value_init (&start, GST_TYPE_FRACTION);
2052 g_value_init (&end, GST_TYPE_FRACTION);
2053
2054 gst_value_set_fraction (&start, numerator_start, denominator_start);
2055 gst_value_set_fraction (&end, numerator_end, denominator_end);
2056 gst_value_set_fraction_range (value, &start, &end);
2057
2058 /* we know the two values contain fractions without internal allocs */
2059 /* g_value_unset (&start); */
2060 /* g_value_unset (&end); */
2061 }
2062
2063 /* FIXME 2.0: Don't leak the internal representation of fraction
2064 * ranges but instead return the numerator and denominator
2065 * separately.
2066 * This would allow to store fraction ranges as
2067 * data[0] = (min_n << 32) | (min_d)
2068 * data[1] = (max_n << 32) | (max_d)
2069 * without requiring an additional allocation for each value.
2070 */
2071
2072 /**
2073 * gst_value_get_fraction_range_min:
2074 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
2075 *
2076 * Gets the minimum of the range specified by @value.
2077 *
2078 * Returns: (nullable): the minimum of the range
2079 */
2080 const GValue *
gst_value_get_fraction_range_min(const GValue * value)2081 gst_value_get_fraction_range_min (const GValue * value)
2082 {
2083 GValue *vals;
2084
2085 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
2086
2087 vals = (GValue *) value->data[0].v_pointer;
2088 if (vals != NULL) {
2089 return &vals[0];
2090 }
2091
2092 return NULL;
2093 }
2094
2095 /**
2096 * gst_value_get_fraction_range_max:
2097 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
2098 *
2099 * Gets the maximum of the range specified by @value.
2100 *
2101 * Returns: (nullable): the maximum of the range
2102 */
2103 const GValue *
gst_value_get_fraction_range_max(const GValue * value)2104 gst_value_get_fraction_range_max (const GValue * value)
2105 {
2106 GValue *vals;
2107
2108 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
2109
2110 vals = (GValue *) value->data[0].v_pointer;
2111 if (vals != NULL) {
2112 return &vals[1];
2113 }
2114
2115 return NULL;
2116 }
2117
2118 static gchar *
gst_value_serialize_fraction_range(const GValue * value)2119 gst_value_serialize_fraction_range (const GValue * value)
2120 {
2121 GValue *vals = (GValue *) value->data[0].v_pointer;
2122 gchar *retval;
2123
2124 if (vals == NULL) {
2125 retval = g_strdup ("[ 0/1, 0/1 ]");
2126 } else {
2127 gchar *start, *end;
2128
2129 start = gst_value_serialize_fraction (&vals[0]);
2130 end = gst_value_serialize_fraction (&vals[1]);
2131
2132 retval = g_strdup_printf ("[ %s, %s ]", start, end);
2133 g_free (start);
2134 g_free (end);
2135 }
2136
2137 return retval;
2138 }
2139
2140 static void
gst_value_transform_fraction_range_string(const GValue * src_value,GValue * dest_value)2141 gst_value_transform_fraction_range_string (const GValue * src_value,
2142 GValue * dest_value)
2143 {
2144 dest_value->data[0].v_pointer =
2145 gst_value_serialize_fraction_range (src_value);
2146 }
2147
2148 static gint
gst_value_compare_fraction_range(const GValue * value1,const GValue * value2)2149 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
2150 {
2151 GValue *vals1, *vals2;
2152
2153 if (value2->data[0].v_pointer == value1->data[0].v_pointer)
2154 return GST_VALUE_EQUAL; /* Only possible if both are NULL */
2155
2156 if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
2157 return GST_VALUE_UNORDERED;
2158
2159 vals1 = (GValue *) value1->data[0].v_pointer;
2160 vals2 = (GValue *) value2->data[0].v_pointer;
2161 if (gst_value_compare_fraction (&vals1[0], &vals2[0]) == GST_VALUE_EQUAL &&
2162 gst_value_compare_fraction (&vals1[1], &vals2[1]) == GST_VALUE_EQUAL)
2163 return GST_VALUE_EQUAL;
2164
2165 return GST_VALUE_UNORDERED;
2166 }
2167
2168 static gboolean
gst_value_deserialize_fraction_range(GValue * dest,const gchar * s)2169 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
2170 {
2171 g_warning ("unimplemented");
2172 return FALSE;
2173 }
2174
2175 /***********
2176 * GstCaps *
2177 ***********/
2178
2179 /**
2180 * gst_value_set_caps:
2181 * @value: a GValue initialized to GST_TYPE_CAPS
2182 * @caps: (transfer none): the caps to set the value to
2183 *
2184 * Sets the contents of @value to @caps. A reference to the
2185 * provided @caps will be taken by the @value.
2186 */
2187 void
gst_value_set_caps(GValue * value,const GstCaps * caps)2188 gst_value_set_caps (GValue * value, const GstCaps * caps)
2189 {
2190 g_return_if_fail (G_IS_VALUE (value));
2191 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
2192 g_return_if_fail (caps == NULL || GST_IS_CAPS (caps));
2193
2194 g_value_set_boxed (value, caps);
2195 }
2196
2197 /**
2198 * gst_value_get_caps:
2199 * @value: a GValue initialized to GST_TYPE_CAPS
2200 *
2201 * Gets the contents of @value. The reference count of the returned
2202 * #GstCaps will not be modified, therefore the caller must take one
2203 * before getting rid of the @value.
2204 *
2205 * Returns: (transfer none): the contents of @value
2206 */
2207 const GstCaps *
gst_value_get_caps(const GValue * value)2208 gst_value_get_caps (const GValue * value)
2209 {
2210 g_return_val_if_fail (G_IS_VALUE (value), NULL);
2211 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
2212
2213 return (GstCaps *) g_value_get_boxed (value);
2214 }
2215
2216 static gint
gst_value_compare_caps(const GValue * value1,const GValue * value2)2217 gst_value_compare_caps (const GValue * value1, const GValue * value2)
2218 {
2219 GstCaps *caps1 = GST_CAPS (gst_value_get_caps (value1));
2220 GstCaps *caps2 = GST_CAPS (gst_value_get_caps (value2));
2221
2222 if (caps1 == caps2)
2223 return GST_VALUE_EQUAL;
2224
2225 if (!caps1 || !caps2)
2226 return GST_VALUE_UNORDERED;
2227
2228 if (gst_caps_is_equal (caps1, caps2))
2229 return GST_VALUE_EQUAL;
2230 return GST_VALUE_UNORDERED;
2231 }
2232
2233 static gchar *
gst_value_serialize_caps(const GValue * value)2234 gst_value_serialize_caps (const GValue * value)
2235 {
2236 GstCaps *caps = g_value_get_boxed (value);
2237 return priv_gst_string_take_and_wrap (gst_caps_to_string (caps));
2238 }
2239
2240 static gboolean
gst_value_deserialize_caps(GValue * dest,const gchar * s)2241 gst_value_deserialize_caps (GValue * dest, const gchar * s)
2242 {
2243 GstCaps *caps;
2244
2245 if (*s != '"') {
2246 /* this can happen if caps are ANY, EMPTY, or only contains a single
2247 * empty structure */
2248 caps = gst_caps_from_string (s);
2249 } else {
2250 gchar *str = gst_string_unwrap (s);
2251
2252 if (G_UNLIKELY (!str))
2253 return FALSE;
2254
2255 caps = gst_caps_from_string (str);
2256 g_free (str);
2257 }
2258
2259 if (caps) {
2260 g_value_take_boxed (dest, caps);
2261 return TRUE;
2262 }
2263 return FALSE;
2264 }
2265
2266 /********************************************
2267 * Serialization/deserialization of GValues *
2268 ********************************************/
2269
2270 static GstValueAbbreviation *
_priv_gst_value_get_abbrs(gint * n_abbrs)2271 _priv_gst_value_get_abbrs (gint * n_abbrs)
2272 {
2273 static GstValueAbbreviation *abbrs = NULL;
2274 static gsize num = 0;
2275
2276 if (g_once_init_enter (&num)) {
2277 /* dynamically generate the array */
2278 gsize _num;
2279 GstValueAbbreviation dyn_abbrs[] = {
2280 {"int", G_TYPE_INT}
2281 ,
2282 {"i", G_TYPE_INT}
2283 ,
2284 {"uint", G_TYPE_UINT}
2285 ,
2286 {"u", G_TYPE_UINT}
2287 ,
2288 {"float", G_TYPE_FLOAT}
2289 ,
2290 {"f", G_TYPE_FLOAT}
2291 ,
2292 {"double", G_TYPE_DOUBLE}
2293 ,
2294 {"d", G_TYPE_DOUBLE}
2295 ,
2296 {"buffer", GST_TYPE_BUFFER}
2297 ,
2298 {"fraction", GST_TYPE_FRACTION}
2299 ,
2300 {"boolean", G_TYPE_BOOLEAN}
2301 ,
2302 {"bool", G_TYPE_BOOLEAN}
2303 ,
2304 {"b", G_TYPE_BOOLEAN}
2305 ,
2306 {"string", G_TYPE_STRING}
2307 ,
2308 {"str", G_TYPE_STRING}
2309 ,
2310 {"s", G_TYPE_STRING}
2311 ,
2312 {"structure", GST_TYPE_STRUCTURE}
2313 ,
2314 {"date", G_TYPE_DATE}
2315 ,
2316 {"datetime", GST_TYPE_DATE_TIME}
2317 ,
2318 {"bitmask", GST_TYPE_BITMASK}
2319 ,
2320 {"flagset", GST_TYPE_FLAG_SET}
2321 ,
2322 {"sample", GST_TYPE_SAMPLE}
2323 ,
2324 {"taglist", GST_TYPE_TAG_LIST}
2325 ,
2326 {"type", G_TYPE_GTYPE}
2327 ,
2328 {"array", GST_TYPE_ARRAY}
2329 ,
2330 {"list", GST_TYPE_LIST}
2331 };
2332 _num = G_N_ELEMENTS (dyn_abbrs);
2333 /* permanently allocate and copy the array now */
2334 abbrs = g_new0 (GstValueAbbreviation, _num);
2335 memcpy (abbrs, dyn_abbrs, sizeof (GstValueAbbreviation) * _num);
2336 g_once_init_leave (&num, _num);
2337 }
2338 *n_abbrs = num;
2339
2340 return abbrs;
2341 }
2342
2343 /* given a type_name that could be a type abbreviation or a registered GType,
2344 * return a matching GType */
2345 static GType
_priv_gst_value_gtype_from_abbr(const char * type_name)2346 _priv_gst_value_gtype_from_abbr (const char *type_name)
2347 {
2348 int i;
2349 GstValueAbbreviation *abbrs;
2350 gint n_abbrs;
2351 GType ret;
2352
2353 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
2354
2355 abbrs = _priv_gst_value_get_abbrs (&n_abbrs);
2356
2357 for (i = 0; i < n_abbrs; i++) {
2358 if (strcmp (type_name, abbrs[i].type_name) == 0) {
2359 return abbrs[i].type;
2360 }
2361 }
2362
2363 /* this is the fallback */
2364 ret = g_type_from_name (type_name);
2365 /* If not found, try it as a dynamic type */
2366 if (G_UNLIKELY (ret == 0))
2367 ret = gst_dynamic_type_factory_load (type_name);
2368 return ret;
2369
2370 }
2371
2372 const char *
_priv_gst_value_gtype_to_abbr(GType type)2373 _priv_gst_value_gtype_to_abbr (GType type)
2374 {
2375 int i;
2376 GstValueAbbreviation *abbrs;
2377 gint n_abbrs;
2378
2379 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
2380
2381 abbrs = _priv_gst_value_get_abbrs (&n_abbrs);
2382
2383 for (i = 0; i < n_abbrs; i++) {
2384 if (type == abbrs[i].type) {
2385 return abbrs[i].type_name;
2386 }
2387 }
2388
2389 return g_type_name (type);
2390 }
2391
2392 /*
2393 * _priv_gst_value_parse_string:
2394 * @s: string to parse
2395 * @end: out-pointer to char behind end of string
2396 * @next: out-pointer to start of unread data
2397 * @unescape: @TRUE if the substring is escaped.
2398 *
2399 * Find the end of a sub-string. If end == next, the string will not be
2400 * null-terminated. In all other cases it will be.
2401 *
2402 * Note: This function modifies the string in @s (if unescape == @TRUE).
2403 *
2404 * Returns: @TRUE if a sub-string was found and @FALSE if the string is not
2405 * terminated.
2406 */
2407 gboolean
_priv_gst_value_parse_string(gchar * s,gchar ** end,gchar ** next,gboolean unescape)2408 _priv_gst_value_parse_string (gchar * s, gchar ** end, gchar ** next,
2409 gboolean unescape)
2410 {
2411 gchar *w;
2412
2413 if (*s == 0)
2414 return FALSE;
2415
2416 if (*s != '"') {
2417 int ret = _priv_gst_value_parse_simple_string (s, end);
2418 *next = *end;
2419
2420 return ret;
2421 }
2422
2423 /* Find the closing quotes */
2424 if (unescape) {
2425 w = s;
2426 s++;
2427 while (*s != '"') {
2428 if (G_UNLIKELY (*s == 0))
2429 return FALSE;
2430 if (G_UNLIKELY (*s == '\\')) {
2431 s++;
2432 if (G_UNLIKELY (*s == 0))
2433 return FALSE;
2434 }
2435 *w = *s;
2436 w++;
2437 s++;
2438 }
2439 s++;
2440 } else {
2441 s++;
2442 while (*s != '"') {
2443 if (G_UNLIKELY (*s == 0))
2444 return FALSE;
2445 if (G_UNLIKELY (*s == '\\')) {
2446 s++;
2447 if (G_UNLIKELY (*s == 0))
2448 return FALSE;
2449 }
2450 s++;
2451 }
2452 s++;
2453 w = s;
2454 }
2455
2456 *end = w;
2457 *next = s;
2458
2459 return TRUE;
2460 }
2461
2462 static gboolean
_priv_gst_value_parse_range(gchar * s,gchar ** after,GValue * value,GType type)2463 _priv_gst_value_parse_range (gchar * s, gchar ** after, GValue * value,
2464 GType type)
2465 {
2466 GValue value1 = { 0 };
2467 GValue value2 = { 0 };
2468 GValue value3 = { 0 };
2469 GType range_type;
2470 gboolean ret, have_step = FALSE;
2471
2472 if (*s != '[')
2473 return FALSE;
2474 s++;
2475
2476 ret = _priv_gst_value_parse_value (s, &s, &value1, type, NULL);
2477 if (!ret)
2478 goto err;
2479
2480 while (g_ascii_isspace (*s))
2481 s++;
2482
2483 if (*s != ',')
2484 goto err;
2485 s++;
2486
2487 while (g_ascii_isspace (*s))
2488 s++;
2489
2490 ret = _priv_gst_value_parse_value (s, &s, &value2, type, NULL);
2491 if (!ret)
2492 goto err;
2493
2494 while (g_ascii_isspace (*s))
2495 s++;
2496
2497 /* optional step for int and int64 */
2498 if (G_VALUE_TYPE (&value1) == G_TYPE_INT
2499 || G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2500 if (*s == ',') {
2501 s++;
2502
2503 while (g_ascii_isspace (*s))
2504 s++;
2505
2506 ret = _priv_gst_value_parse_value (s, &s, &value3, type, NULL);
2507 if (!ret)
2508 goto err;
2509
2510 while (g_ascii_isspace (*s))
2511 s++;
2512
2513 have_step = TRUE;
2514 }
2515 }
2516
2517 if (*s != ']')
2518 goto err;
2519 s++;
2520
2521 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
2522 return FALSE;
2523 if (have_step && G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value3))
2524 return FALSE;
2525
2526 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
2527 range_type = GST_TYPE_DOUBLE_RANGE;
2528 g_value_init (value, range_type);
2529 gst_value_set_double_range (value,
2530 gst_g_value_get_double_unchecked (&value1),
2531 gst_g_value_get_double_unchecked (&value2));
2532 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
2533 range_type = GST_TYPE_INT_RANGE;
2534 g_value_init (value, range_type);
2535 if (have_step)
2536 gst_value_set_int_range_step (value,
2537 gst_g_value_get_int_unchecked (&value1),
2538 gst_g_value_get_int_unchecked (&value2),
2539 gst_g_value_get_int_unchecked (&value3));
2540 else
2541 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
2542 gst_g_value_get_int_unchecked (&value2));
2543 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2544 range_type = GST_TYPE_INT64_RANGE;
2545 g_value_init (value, range_type);
2546 if (have_step)
2547 gst_value_set_int64_range_step (value,
2548 gst_g_value_get_int64_unchecked (&value1),
2549 gst_g_value_get_int64_unchecked (&value2),
2550 gst_g_value_get_int64_unchecked (&value3));
2551 else
2552 gst_value_set_int64_range (value,
2553 gst_g_value_get_int64_unchecked (&value1),
2554 gst_g_value_get_int64_unchecked (&value2));
2555 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
2556 range_type = GST_TYPE_FRACTION_RANGE;
2557 g_value_init (value, range_type);
2558 gst_value_set_fraction_range (value, &value1, &value2);
2559 } else {
2560 goto err;
2561 }
2562
2563 *after = s;
2564 return TRUE;
2565
2566 err:
2567 g_value_unset (value);
2568 g_value_unset (&value1);
2569 g_value_unset (&value2);
2570 g_value_unset (&value3);
2571 return FALSE;
2572 }
2573
2574 static gboolean
_priv_gst_value_parse_any_list(gchar * s,gchar ** after,GValue * value,GType type,char begin,char end,GParamSpec * pspec)2575 _priv_gst_value_parse_any_list (gchar * s, gchar ** after, GValue * value,
2576 GType type, char begin, char end, GParamSpec * pspec)
2577 {
2578 GValue list_value = { 0 };
2579 gboolean ret;
2580 GstValueList *vlist = VALUE_LIST_ARRAY (value);
2581 GParamSpec *element_spec = NULL;
2582
2583 if (pspec)
2584 element_spec = GST_PARAM_SPEC_ARRAY_LIST (pspec)->element_spec;
2585
2586 if (*s != begin)
2587 return FALSE;
2588 s++;
2589
2590 while (g_ascii_isspace (*s))
2591 s++;
2592
2593 while (*s != end) {
2594 if (*s == ',') {
2595 s++;
2596 while (g_ascii_isspace (*s))
2597 s++;
2598
2599 if (*s == ',')
2600 return FALSE;
2601
2602 continue;
2603 }
2604
2605 memset (&list_value, 0, sizeof (list_value));
2606
2607 ret = _priv_gst_value_parse_value (s, &s, &list_value, type, element_spec);
2608 if (!ret)
2609 return FALSE;
2610
2611 _gst_value_list_append_val (vlist, &list_value);
2612
2613 while (g_ascii_isspace (*s))
2614 s++;
2615
2616 if (*s != ',' && *s != end)
2617 return FALSE;
2618 }
2619
2620 s++;
2621
2622 *after = s;
2623 return TRUE;
2624 }
2625
2626 static gboolean
_priv_gst_value_parse_list(gchar * s,gchar ** after,GValue * value,GType type,GParamSpec * pspec)2627 _priv_gst_value_parse_list (gchar * s, gchar ** after, GValue * value,
2628 GType type, GParamSpec * pspec)
2629 {
2630 return _priv_gst_value_parse_any_list (s, after, value, type, '{', '}',
2631 pspec);
2632 }
2633
2634 static gboolean
_priv_gst_value_parse_array(gchar * s,gchar ** after,GValue * value,GType type,GParamSpec * pspec)2635 _priv_gst_value_parse_array (gchar * s, gchar ** after, GValue * value,
2636 GType type, GParamSpec * pspec)
2637 {
2638 return _priv_gst_value_parse_any_list (s, after, value, type, '<', '>',
2639 pspec);
2640 }
2641
2642 gboolean
_priv_gst_value_parse_simple_string(gchar * str,gchar ** end)2643 _priv_gst_value_parse_simple_string (gchar * str, gchar ** end)
2644 {
2645 char *s = str;
2646
2647 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2648 s++;
2649 }
2650
2651 *end = s;
2652
2653 return (s != str);
2654 }
2655
2656 static gboolean
_priv_gst_value_parse_struct_or_caps(gchar * str,gchar ** after,GType type,GValue * value)2657 _priv_gst_value_parse_struct_or_caps (gchar * str, gchar ** after, GType type,
2658 GValue * value)
2659 {
2660 gint openers = 1;
2661 gboolean ret = FALSE;
2662 gchar *s = str, t, *start, *end, *next;
2663
2664 if (*s != '[')
2665 return FALSE;
2666
2667 s++;
2668 str = s;
2669 for (; *s; s++) {
2670 if (*s == ']')
2671 openers--;
2672 else if (*s == '[')
2673 openers++;
2674
2675 if (openers == 0) {
2676 *after = s + 1;
2677 break;
2678 }
2679 }
2680
2681 if (*after == NULL)
2682 return FALSE;
2683
2684 t = *s;
2685 *s = '\0';
2686 g_value_init (value, type);
2687 if (priv_gst_structure_parse_name (str, &start, &end, &next, TRUE))
2688 ret = gst_value_deserialize (value, str);
2689 if (G_UNLIKELY (!ret)) {
2690 *s = t;
2691 g_value_unset (value);
2692 }
2693
2694 return ret;
2695 }
2696
2697 static gboolean
_priv_gst_value_parse_range_struct_caps(gchar * s,gchar ** after,GValue * value,GType type)2698 _priv_gst_value_parse_range_struct_caps (gchar * s, gchar ** after,
2699 GValue * value, GType type)
2700 {
2701 gint i;
2702 gchar *tmp = s;
2703 gboolean ret = FALSE;
2704 GType try_types[] = {
2705 GST_TYPE_STRUCTURE,
2706 GST_TYPE_CAPS,
2707 };
2708
2709 if (type == GST_TYPE_CAPS || type == GST_TYPE_STRUCTURE)
2710 ret = _priv_gst_value_parse_struct_or_caps (tmp, &tmp, type, value);
2711
2712 if (ret)
2713 goto ok;
2714
2715 tmp = s;
2716 ret = _priv_gst_value_parse_range (tmp, &tmp, value, type);
2717 if (ret)
2718 goto ok;
2719
2720 if (type != G_TYPE_INVALID)
2721 return ret;
2722
2723 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2724 tmp = s;
2725 ret = _priv_gst_value_parse_struct_or_caps (tmp, &tmp, try_types[i], value);
2726 if (ret)
2727 goto ok;
2728 }
2729
2730 return ret;
2731
2732 ok:
2733 *after = tmp;
2734 return ret;
2735 }
2736
2737 gboolean
_priv_gst_value_parse_value(gchar * str,gchar ** after,GValue * value,GType default_type,GParamSpec * pspec)2738 _priv_gst_value_parse_value (gchar * str,
2739 gchar ** after, GValue * value, GType default_type, GParamSpec * pspec)
2740 {
2741 gchar *type_name;
2742 gchar *type_end;
2743 gchar *value_s;
2744 gchar *value_end;
2745 gchar *s;
2746 gchar c;
2747 int ret = 0;
2748 GType type = default_type;
2749
2750 s = str;
2751 while (g_ascii_isspace (*s))
2752 s++;
2753
2754 /* check if there's a (type_name) 'cast' */
2755 type_name = NULL;
2756
2757 if (*s == '(') {
2758 s++;
2759 while (g_ascii_isspace (*s))
2760 s++;
2761 type_name = s;
2762 if (G_UNLIKELY (!_priv_gst_value_parse_simple_string (s, &type_end)))
2763 return FALSE;
2764 s = type_end;
2765 while (g_ascii_isspace (*s))
2766 s++;
2767 if (G_UNLIKELY (*s != ')'))
2768 return FALSE;
2769 s++;
2770 while (g_ascii_isspace (*s))
2771 s++;
2772
2773 c = *type_end;
2774 *type_end = 0;
2775 type = _priv_gst_value_gtype_from_abbr (type_name);
2776 GST_DEBUG ("trying type name '%s'", type_name);
2777 *type_end = c;
2778
2779 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2780 GST_WARNING ("invalid type");
2781 return FALSE;
2782 }
2783 } else if (pspec) {
2784 type = G_PARAM_SPEC_VALUE_TYPE (pspec);
2785 }
2786
2787 while (g_ascii_isspace (*s))
2788 s++;
2789 if (*s == '[') {
2790 ret = _priv_gst_value_parse_range_struct_caps (s, &s, value, type);
2791 } else if (*s == '{') {
2792 g_value_init (value, GST_TYPE_LIST);
2793 ret = _priv_gst_value_parse_list (s, &s, value, type, pspec);
2794 } else if (*s == '<') {
2795 g_value_init (value, GST_TYPE_ARRAY);
2796 ret = _priv_gst_value_parse_array (s, &s, value, type, pspec);
2797 } else {
2798 value_s = s;
2799
2800 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2801 GType try_types[] =
2802 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, GST_TYPE_FLAG_SET,
2803 G_TYPE_BOOLEAN, G_TYPE_STRING
2804 };
2805 int i;
2806 int value_size;
2807 gboolean check_wrapped_non_string;
2808
2809 if (G_UNLIKELY (!_priv_gst_value_parse_string (s, &value_end, &s, FALSE)))
2810 return FALSE;
2811 /* Set NULL terminator for deserialization */
2812 value_size = value_end - value_s;
2813 value_s = g_strndup (value_s, value_end - value_s);
2814 /* Keep old broken behavior where "2" could be interpretted as an int */
2815 check_wrapped_non_string = value_s[0] == '"' &&
2816 strlen (value_s) >= 2 && value_end[-1] == '"';
2817
2818 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2819 g_value_init (value, try_types[i]);
2820 if (try_types[i] != G_TYPE_STRING && check_wrapped_non_string) {
2821 value_s[value_size - 1] = '\0';
2822 ret = gst_value_deserialize (value, value_s + 1);
2823 value_s[value_size - 1] = '"';
2824 if (ret) {
2825 const gchar *type_name = g_type_name (try_types[i]);
2826
2827 g_warning ("Received a structure string that contains "
2828 "'=%s'. Reading as a %s value, rather than a string "
2829 "value. This is undesired behaviour, and with GStreamer 1.22 "
2830 " onward, this will be interpreted as a string value instead "
2831 "because it is wrapped in '\"' quotes. If you want to "
2832 "guarantee this value is read as a string, before this "
2833 "change, use '=(string)%s' instead. If you want to read "
2834 "in a %s value, leave its value unquoted.",
2835 value_s, type_name, value_s, type_name);
2836 break;
2837 }
2838 } else {
2839 ret = gst_value_deserialize (value, value_s);
2840 if (ret)
2841 break;
2842 }
2843 g_value_unset (value);
2844 }
2845 } else {
2846 g_value_init (value, type);
2847
2848 if (G_UNLIKELY (!_priv_gst_value_parse_string (s, &value_end, &s, FALSE)))
2849 return FALSE;
2850 /* Set NULL terminator for deserialization */
2851 value_s = g_strndup (value_s, value_end - value_s);
2852
2853 ret = gst_value_deserialize_with_pspec (value, value_s, pspec);
2854 if (G_UNLIKELY (!ret))
2855 g_value_unset (value);
2856 }
2857 g_free (value_s);
2858 }
2859
2860 *after = s;
2861
2862 return ret;
2863 }
2864
2865 /**************
2866 * GstSegment *
2867 **************/
2868
2869 static gchar *
gst_value_serialize_segment_internal(const GValue * value,gboolean escape)2870 gst_value_serialize_segment_internal (const GValue * value, gboolean escape)
2871 {
2872 GstSegment *seg = g_value_get_boxed (value);
2873 gchar *t, *res;
2874 GstStructure *s;
2875
2876 s = gst_structure_new_id (GST_QUARK (SEGMENT),
2877 GST_QUARK (FLAGS), GST_TYPE_SEGMENT_FLAGS, seg->flags,
2878 GST_QUARK (RATE), G_TYPE_DOUBLE, seg->rate,
2879 GST_QUARK (APPLIED_RATE), G_TYPE_DOUBLE, seg->applied_rate,
2880 GST_QUARK (FORMAT), GST_TYPE_FORMAT, seg->format,
2881 GST_QUARK (BASE), G_TYPE_UINT64, seg->base,
2882 GST_QUARK (OFFSET), G_TYPE_UINT64, seg->offset,
2883 GST_QUARK (START), G_TYPE_UINT64, seg->start,
2884 GST_QUARK (STOP), G_TYPE_UINT64, seg->stop,
2885 GST_QUARK (TIME), G_TYPE_UINT64, seg->time,
2886 GST_QUARK (POSITION), G_TYPE_UINT64, seg->position,
2887 GST_QUARK (DURATION), G_TYPE_UINT64, seg->duration, NULL);
2888
2889 t = gst_structure_to_string (s);
2890 if (escape) {
2891 res = g_strdup_printf ("\"%s\"", t);
2892 g_free (t);
2893 } else {
2894 res = t;
2895 }
2896 gst_structure_free (s);
2897
2898 return res;
2899 }
2900
2901 static gchar *
gst_value_serialize_segment(const GValue * value)2902 gst_value_serialize_segment (const GValue * value)
2903 {
2904 return gst_value_serialize_segment_internal (value, TRUE);
2905 }
2906
2907 static gboolean
gst_value_deserialize_segment_internal(GValue * dest,const gchar * s,gboolean unescape)2908 gst_value_deserialize_segment_internal (GValue * dest, const gchar * s,
2909 gboolean unescape)
2910 {
2911 GstStructure *str;
2912 GstSegment seg;
2913 gboolean res;
2914 gsize len;
2915 gchar *t;
2916
2917 if (unescape) {
2918 len = strlen (s);
2919 if (G_UNLIKELY (*s != '"' || len < 2 || s[len - 1] != '"')) {
2920 /* "\"" is not an accepted string, so len must be at least 2 */
2921 GST_ERROR ("Failed deserializing segement: expected string to start and "
2922 "end with '\"'");
2923 return FALSE;
2924 }
2925 t = g_strdup (s + 1);
2926 t[len - 2] = '\0';
2927 /* removed trailing '"' */
2928 str = gst_structure_from_string (t, NULL);
2929 g_free (t);
2930 } else {
2931 str = gst_structure_from_string (s, NULL);
2932 }
2933 if (G_UNLIKELY (str == NULL))
2934 return FALSE;
2935
2936 res = gst_structure_id_get (str,
2937 GST_QUARK (FLAGS), GST_TYPE_SEGMENT_FLAGS, &seg.flags,
2938 GST_QUARK (RATE), G_TYPE_DOUBLE, &seg.rate,
2939 GST_QUARK (APPLIED_RATE), G_TYPE_DOUBLE, &seg.applied_rate,
2940 GST_QUARK (FORMAT), GST_TYPE_FORMAT, &seg.format,
2941 GST_QUARK (BASE), G_TYPE_UINT64, &seg.base,
2942 GST_QUARK (OFFSET), G_TYPE_UINT64, &seg.offset,
2943 GST_QUARK (START), G_TYPE_UINT64, &seg.start,
2944 GST_QUARK (STOP), G_TYPE_UINT64, &seg.stop,
2945 GST_QUARK (TIME), G_TYPE_UINT64, &seg.time,
2946 GST_QUARK (POSITION), G_TYPE_UINT64, &seg.position,
2947 GST_QUARK (DURATION), G_TYPE_UINT64, &seg.duration, NULL);
2948 gst_structure_free (str);
2949
2950 if (res)
2951 g_value_set_boxed (dest, &seg);
2952
2953 return res;
2954 }
2955
2956 static gboolean
gst_value_deserialize_segment(GValue * dest,const gchar * s)2957 gst_value_deserialize_segment (GValue * dest, const gchar * s)
2958 {
2959 return gst_value_deserialize_segment_internal (dest, s, TRUE);
2960 }
2961
2962 /****************
2963 * GstStructure *
2964 ****************/
2965
2966 /**
2967 * gst_value_set_structure:
2968 * @value: a GValue initialized to GST_TYPE_STRUCTURE
2969 * @structure: the structure to set the value to
2970 *
2971 * Sets the contents of @value to @structure.
2972 */
2973 void
gst_value_set_structure(GValue * value,const GstStructure * structure)2974 gst_value_set_structure (GValue * value, const GstStructure * structure)
2975 {
2976 g_return_if_fail (G_IS_VALUE (value));
2977 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
2978 g_return_if_fail (structure == NULL || GST_IS_STRUCTURE (structure));
2979
2980 g_value_set_boxed (value, structure);
2981 }
2982
2983 /**
2984 * gst_value_get_structure:
2985 * @value: a GValue initialized to GST_TYPE_STRUCTURE
2986 *
2987 * Gets the contents of @value.
2988 *
2989 * Returns: (transfer none): the contents of @value
2990 */
2991 const GstStructure *
gst_value_get_structure(const GValue * value)2992 gst_value_get_structure (const GValue * value)
2993 {
2994 g_return_val_if_fail (G_IS_VALUE (value), NULL);
2995 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
2996
2997 return (GstStructure *) g_value_get_boxed (value);
2998 }
2999
3000 static gchar *
gst_value_serialize_structure(const GValue * value)3001 gst_value_serialize_structure (const GValue * value)
3002 {
3003 GstStructure *structure = g_value_get_boxed (value);
3004
3005 return priv_gst_string_take_and_wrap (gst_structure_to_string (structure));
3006 /* string should always end up being wrapped, since a structure string
3007 * ends in a ';' character */
3008 }
3009
3010 static gboolean
gst_value_deserialize_structure(GValue * dest,const gchar * s)3011 gst_value_deserialize_structure (GValue * dest, const gchar * s)
3012 {
3013 GstStructure *structure;
3014
3015 if (*s != '"') {
3016 /* the output of gst_value_serialize_structure would never produce
3017 * such a string, but a user may pass to gst_structure_from_string
3018 * the string:
3019 * name, sub=(GstStructure)sub-name, val=(int)5;
3020 * and expect sub to be read as an *empty* structure with the name
3021 * sub-name. Similar to
3022 * name, caps=(GstCaps)video/x-raw, val=(int)5;
3023 * which gst_structure_to_string can produce. */
3024 structure = gst_structure_from_string (s, NULL);
3025 } else {
3026 gchar *str = gst_string_unwrap (s);
3027
3028 if (G_UNLIKELY (!str))
3029 return FALSE;
3030
3031 structure = gst_structure_from_string (str, NULL);
3032 g_free (str);
3033 }
3034
3035 if (G_LIKELY (structure)) {
3036 g_value_take_boxed (dest, structure);
3037 return TRUE;
3038 }
3039 return FALSE;
3040 }
3041
3042 static gboolean
gst_value_compare_structure(const GValue * value1,const GValue * value2)3043 gst_value_compare_structure (const GValue * value1, const GValue * value2)
3044 {
3045 GstStructure *structure1 = GST_STRUCTURE (g_value_get_boxed (value1));
3046 GstStructure *structure2 = GST_STRUCTURE (g_value_get_boxed (value2));
3047
3048 if (structure1 == structure2)
3049 return GST_VALUE_EQUAL;
3050
3051 if (!structure1 || !structure2)
3052 return GST_VALUE_UNORDERED;
3053
3054 if (gst_structure_is_equal (structure1, structure2))
3055 return GST_VALUE_EQUAL;
3056
3057 return GST_VALUE_UNORDERED;
3058 }
3059
3060 /*******************
3061 * GstCapsFeatures *
3062 *******************/
3063
3064 /**
3065 * gst_value_set_caps_features:
3066 * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
3067 * @features: the features to set the value to
3068 *
3069 * Sets the contents of @value to @features.
3070 */
3071 void
gst_value_set_caps_features(GValue * value,const GstCapsFeatures * features)3072 gst_value_set_caps_features (GValue * value, const GstCapsFeatures * features)
3073 {
3074 g_return_if_fail (G_IS_VALUE (value));
3075 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES);
3076 g_return_if_fail (features == NULL || GST_IS_CAPS_FEATURES (features));
3077
3078 g_value_set_boxed (value, features);
3079 }
3080
3081 /**
3082 * gst_value_get_caps_features:
3083 * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
3084 *
3085 * Gets the contents of @value.
3086 *
3087 * Returns: (transfer none): the contents of @value
3088 */
3089 const GstCapsFeatures *
gst_value_get_caps_features(const GValue * value)3090 gst_value_get_caps_features (const GValue * value)
3091 {
3092 g_return_val_if_fail (G_IS_VALUE (value), NULL);
3093 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES, NULL);
3094
3095 return (GstCapsFeatures *) g_value_get_boxed (value);
3096 }
3097
3098 static gchar *
gst_value_serialize_caps_features(const GValue * value)3099 gst_value_serialize_caps_features (const GValue * value)
3100 {
3101 GstCapsFeatures *features = g_value_get_boxed (value);
3102
3103 return priv_gst_string_take_and_wrap (gst_caps_features_to_string (features));
3104 }
3105
3106 static gboolean
gst_value_deserialize_caps_features(GValue * dest,const gchar * s)3107 gst_value_deserialize_caps_features (GValue * dest, const gchar * s)
3108 {
3109 GstCapsFeatures *features;
3110
3111 if (*s != '"') {
3112 /* This can happen if gst_caps_features_to_string only returns
3113 * ALL, NONE, or a single features name, which means it is not
3114 * actually wrapped by priv_gst_string_take_and_wrap */
3115 features = gst_caps_features_from_string (s);
3116 } else {
3117 gchar *str = gst_string_unwrap (s);
3118
3119 if (G_UNLIKELY (!str))
3120 return FALSE;
3121
3122 features = gst_caps_features_from_string (str);
3123 g_free (str);
3124 }
3125
3126 if (G_LIKELY (features)) {
3127 g_value_take_boxed (dest, features);
3128 return TRUE;
3129 }
3130 return FALSE;
3131 }
3132
3133 /**************
3134 * GstTagList *
3135 **************/
3136 static gint
gst_value_compare_tag_list(const GValue * value1,const GValue * value2)3137 gst_value_compare_tag_list (const GValue * value1, const GValue * value2)
3138 {
3139 GstTagList *taglist1 = GST_TAG_LIST (g_value_get_boxed (value1));
3140 GstTagList *taglist2 = GST_TAG_LIST (g_value_get_boxed (value2));
3141
3142 if (gst_tag_list_is_equal (taglist1, taglist2))
3143 return GST_VALUE_EQUAL;
3144 return GST_VALUE_UNORDERED;
3145 }
3146
3147 static gboolean
gst_value_deserialize_tag_list(GValue * dest,const gchar * s)3148 gst_value_deserialize_tag_list (GValue * dest, const gchar * s)
3149 {
3150 GstTagList *taglist;
3151
3152 if (*s != '"') {
3153 /* the output of gst_value_serialize_tag_list would never produce
3154 * such a string, but a user may pass to gst_structure_from_string
3155 * the string:
3156 * name, list=(GstTagList)taglist, val=(int)5;
3157 * and expect list to be read as an *empty* tag list. Similar to
3158 * name, caps=(GstCaps)video/x-raw, val=(int)5;
3159 * which gst_structure_to_string can produce. */
3160 taglist = gst_tag_list_new_from_string (s);
3161 } else {
3162 gchar *str = gst_string_unwrap (s);
3163
3164 if (G_UNLIKELY (!str))
3165 return FALSE;
3166
3167 taglist = gst_tag_list_new_from_string (str);
3168 g_free (str);
3169 }
3170
3171 if (G_LIKELY (taglist != NULL)) {
3172 g_value_take_boxed (dest, taglist);
3173 return TRUE;
3174 }
3175 return FALSE;
3176 }
3177
3178 static gchar *
gst_value_serialize_tag_list(const GValue * value)3179 gst_value_serialize_tag_list (const GValue * value)
3180 {
3181 GstTagList *taglist = g_value_get_boxed (value);
3182
3183 return priv_gst_string_take_and_wrap (gst_tag_list_to_string (taglist));
3184 /* string should always end up being wrapped, since a taglist (structure)
3185 * string ends in a ';' character */
3186 }
3187
3188
3189 /*************
3190 * GstBuffer *
3191 *************/
3192
3193 static gint
compare_buffer(GstBuffer * buf1,GstBuffer * buf2)3194 compare_buffer (GstBuffer * buf1, GstBuffer * buf2)
3195 {
3196 gsize size1, size2;
3197 GstMapInfo info1, info2;
3198 gint result, mret;
3199
3200 if (buf1 == buf2)
3201 return GST_VALUE_EQUAL;
3202
3203 size1 = gst_buffer_get_size (buf1);
3204 size2 = gst_buffer_get_size (buf2);
3205
3206 if (size1 != size2)
3207 return GST_VALUE_UNORDERED;
3208
3209 if (size1 == 0)
3210 return GST_VALUE_EQUAL;
3211
3212 if (!gst_buffer_map (buf1, &info1, GST_MAP_READ))
3213 return GST_VALUE_UNORDERED;
3214
3215 if (!gst_buffer_map (buf2, &info2, GST_MAP_READ)) {
3216 gst_buffer_unmap (buf1, &info1);
3217 return GST_VALUE_UNORDERED;
3218 }
3219
3220 mret = memcmp (info1.data, info2.data, info1.size);
3221 if (mret == 0)
3222 result = GST_VALUE_EQUAL;
3223 else if (mret < 0)
3224 result = GST_VALUE_LESS_THAN;
3225 else
3226 result = GST_VALUE_GREATER_THAN;
3227
3228 gst_buffer_unmap (buf1, &info1);
3229 gst_buffer_unmap (buf2, &info2);
3230
3231 return result;
3232 }
3233
3234 static gint
gst_value_compare_buffer(const GValue * value1,const GValue * value2)3235 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
3236 {
3237 GstBuffer *buf1 = gst_value_get_buffer (value1);
3238 GstBuffer *buf2 = gst_value_get_buffer (value2);
3239
3240 return compare_buffer (buf1, buf2);
3241 }
3242
3243 static gchar *
gst_value_serialize_buffer(const GValue * value)3244 gst_value_serialize_buffer (const GValue * value)
3245 {
3246 GstMapInfo info;
3247 guint8 *data;
3248 gint i;
3249 gchar *string;
3250 GstBuffer *buffer;
3251
3252 buffer = gst_value_get_buffer (value);
3253 if (buffer == NULL)
3254 return NULL;
3255
3256 if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
3257 return NULL;
3258
3259 data = info.data;
3260
3261 string = g_malloc (info.size * 2 + 1);
3262 for (i = 0; i < info.size; i++) {
3263 sprintf (string + i * 2, "%02x", data[i]);
3264 }
3265 string[info.size * 2] = 0;
3266
3267 gst_buffer_unmap (buffer, &info);
3268
3269 return string;
3270 }
3271
3272 static gboolean
gst_value_deserialize_buffer(GValue * dest,const gchar * s)3273 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
3274 {
3275 GstBuffer *buffer;
3276 gint len;
3277 gchar ts[3];
3278 GstMapInfo info;
3279 guint8 *data;
3280 gint i;
3281
3282 len = strlen (s);
3283 if (len & 1)
3284 goto wrong_length;
3285
3286 buffer = gst_buffer_new_allocate (NULL, len / 2, NULL);
3287 if (!gst_buffer_map (buffer, &info, GST_MAP_WRITE))
3288 goto map_failed;
3289 data = info.data;
3290
3291 for (i = 0; i < len / 2; i++) {
3292 if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
3293 goto wrong_char;
3294
3295 ts[0] = s[i * 2 + 0];
3296 ts[1] = s[i * 2 + 1];
3297 ts[2] = 0;
3298
3299 data[i] = (guint8) strtoul (ts, NULL, 16);
3300 }
3301 gst_buffer_unmap (buffer, &info);
3302
3303 gst_value_take_buffer (dest, buffer);
3304
3305 return TRUE;
3306
3307 /* ERRORS */
3308 wrong_length:
3309 {
3310 return FALSE;
3311 }
3312 map_failed:
3313 {
3314 return FALSE;
3315 }
3316 wrong_char:
3317 {
3318 gst_buffer_unref (buffer);
3319 gst_buffer_unmap (buffer, &info);
3320 return FALSE;
3321 }
3322 }
3323
3324 /*************
3325 * GstSample *
3326 *************/
3327
3328 /* This function is mostly used for comparing image/buffer tags in taglists */
3329 static gint
gst_value_compare_sample(const GValue * value1,const GValue * value2)3330 gst_value_compare_sample (const GValue * value1, const GValue * value2)
3331 {
3332 GstBuffer *buf1 = gst_sample_get_buffer (gst_value_get_sample (value1));
3333 GstBuffer *buf2 = gst_sample_get_buffer (gst_value_get_sample (value2));
3334
3335 /* FIXME: should we take into account anything else such as caps? */
3336 return compare_buffer (buf1, buf2);
3337 }
3338
3339 static gchar *
gst_value_serialize_sample(const GValue * value)3340 gst_value_serialize_sample (const GValue * value)
3341 {
3342 const GstStructure *info_structure;
3343 GstSegment *segment;
3344 GstBuffer *buffer;
3345 GstCaps *caps;
3346 GstSample *sample;
3347 GValue val = { 0, };
3348 gchar *info_str, *caps_str, *tmp;
3349 gchar *buf_str, *seg_str, *s;
3350
3351 sample = g_value_get_boxed (value);
3352
3353 buffer = gst_sample_get_buffer (sample);
3354 if (buffer) {
3355 g_value_init (&val, GST_TYPE_BUFFER);
3356 g_value_set_boxed (&val, buffer);
3357 buf_str = gst_value_serialize_buffer (&val);
3358 g_value_unset (&val);
3359 } else {
3360 buf_str = g_strdup ("None");
3361 }
3362
3363 caps = gst_sample_get_caps (sample);
3364 if (caps) {
3365 tmp = gst_caps_to_string (caps);
3366 caps_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
3367 g_strdelimit (caps_str, "=", '_');
3368 g_free (tmp);
3369 } else {
3370 caps_str = g_strdup ("None");
3371 }
3372
3373 segment = gst_sample_get_segment (sample);
3374 if (segment) {
3375 g_value_init (&val, GST_TYPE_SEGMENT);
3376 g_value_set_boxed (&val, segment);
3377 tmp = gst_value_serialize_segment_internal (&val, FALSE);
3378 seg_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
3379 g_strdelimit (seg_str, "=", '_');
3380 g_free (tmp);
3381 g_value_unset (&val);
3382 } else {
3383 seg_str = g_strdup ("None");
3384 }
3385
3386 info_structure = gst_sample_get_info (sample);
3387 if (info_structure) {
3388 tmp = gst_structure_to_string (info_structure);
3389 info_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
3390 g_strdelimit (info_str, "=", '_');
3391 g_free (tmp);
3392 } else {
3393 info_str = g_strdup ("None");
3394 }
3395
3396 s = g_strconcat (buf_str, ":", caps_str, ":", seg_str, ":", info_str, NULL);
3397 g_free (buf_str);
3398 g_free (caps_str);
3399 g_free (seg_str);
3400 g_free (info_str);
3401
3402 return s;
3403 }
3404
3405 static gboolean
gst_value_deserialize_sample(GValue * dest,const gchar * s)3406 gst_value_deserialize_sample (GValue * dest, const gchar * s)
3407 {
3408 GValue bval = G_VALUE_INIT, sval = G_VALUE_INIT;
3409 GstStructure *info;
3410 GstSample *sample;
3411 GstCaps *caps = NULL;
3412 gboolean ret = FALSE;
3413 gchar **fields;
3414 gsize outlen;
3415 gint len;
3416
3417 GST_TRACE ("deserialize '%s'", s);
3418
3419 fields = g_strsplit (s, ":", -1);
3420 len = g_strv_length (fields);
3421 if (len != 4)
3422 goto wrong_length;
3423
3424 g_value_init (&bval, GST_TYPE_BUFFER);
3425 g_value_init (&sval, GST_TYPE_SEGMENT);
3426
3427 if (!gst_value_deserialize_buffer (&bval, fields[0]))
3428 goto fail;
3429
3430 if (strcmp (fields[1], "None") != 0) {
3431 g_strdelimit (fields[1], "_", '=');
3432 g_base64_decode_inplace (fields[1], &outlen);
3433 GST_TRACE ("caps : %s", fields[1]);
3434 caps = gst_caps_from_string (fields[1]);
3435 if (caps == NULL)
3436 goto fail;
3437 }
3438
3439 if (strcmp (fields[2], "None") != 0) {
3440 g_strdelimit (fields[2], "_", '=');
3441 g_base64_decode_inplace (fields[2], &outlen);
3442 GST_TRACE ("segment : %s", fields[2]);
3443 if (!gst_value_deserialize_segment_internal (&sval, fields[2], FALSE))
3444 goto fail;
3445 }
3446
3447 if (strcmp (fields[3], "None") != 0) {
3448 g_strdelimit (fields[3], "_", '=');
3449 g_base64_decode_inplace (fields[3], &outlen);
3450 GST_TRACE ("info : %s", fields[3]);
3451 info = gst_structure_from_string (fields[3], NULL);
3452 if (info == NULL)
3453 goto fail;
3454 } else {
3455 info = NULL;
3456 }
3457
3458 sample = gst_sample_new (gst_value_get_buffer (&bval), caps,
3459 g_value_get_boxed (&sval), info);
3460
3461 g_value_take_boxed (dest, sample);
3462
3463 ret = TRUE;
3464
3465 fail:
3466 if (caps)
3467 gst_caps_unref (caps);
3468 g_value_unset (&bval);
3469 g_value_unset (&sval);
3470
3471 wrong_length:
3472
3473 g_strfreev (fields);
3474
3475 return ret;
3476 }
3477
3478 /***********
3479 * boolean *
3480 ***********/
3481
3482 static gint
gst_value_compare_boolean(const GValue * value1,const GValue * value2)3483 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
3484 {
3485 if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
3486 return GST_VALUE_EQUAL;
3487 return GST_VALUE_UNORDERED;
3488 }
3489
3490 static gchar *
gst_value_serialize_boolean(const GValue * value)3491 gst_value_serialize_boolean (const GValue * value)
3492 {
3493 if (value->data[0].v_int) {
3494 return g_strdup ("true");
3495 }
3496 return g_strdup ("false");
3497 }
3498
3499 static gboolean
gst_value_deserialize_boolean(GValue * dest,const gchar * s)3500 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
3501 {
3502 gboolean ret = FALSE;
3503
3504 if (g_ascii_strcasecmp (s, "true") == 0 ||
3505 g_ascii_strcasecmp (s, "yes") == 0 ||
3506 g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
3507 g_value_set_boolean (dest, TRUE);
3508 ret = TRUE;
3509 } else if (g_ascii_strcasecmp (s, "false") == 0 ||
3510 g_ascii_strcasecmp (s, "no") == 0 ||
3511 g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
3512 g_value_set_boolean (dest, FALSE);
3513 ret = TRUE;
3514 }
3515
3516 return ret;
3517 }
3518
3519 #define CREATE_SERIALIZATION_START(_type,_macro) \
3520 static gint \
3521 gst_value_compare_ ## _type \
3522 (const GValue * value1, const GValue * value2) \
3523 { \
3524 g ## _type val1 = g_value_get_ ## _type (value1); \
3525 g ## _type val2 = g_value_get_ ## _type (value2); \
3526 if (val1 > val2) \
3527 return GST_VALUE_GREATER_THAN; \
3528 if (val1 < val2) \
3529 return GST_VALUE_LESS_THAN; \
3530 return GST_VALUE_EQUAL; \
3531 } \
3532 \
3533 static gchar * \
3534 gst_value_serialize_ ## _type (const GValue * value) \
3535 { \
3536 GValue val = { 0, }; \
3537 g_value_init (&val, G_TYPE_STRING); \
3538 if (!g_value_transform (value, &val)) \
3539 g_assert_not_reached (); \
3540 /* NO_COPY_MADNESS!!! */ \
3541 return (char *) g_value_get_string (&val); \
3542 }
3543
3544 /* deserialize the given s into to as a gint64.
3545 * check if the result is actually storeable in the given size number of
3546 * bytes.
3547 */
3548 static gboolean
gst_value_deserialize_int_helper(gint64 * to,const gchar * s,gint64 min,gint64 max,gint size)3549 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
3550 gint64 min, gint64 max, gint size)
3551 {
3552 gboolean ret = FALSE;
3553 gchar *end;
3554 guint64 mask = ~0;
3555
3556 errno = 0;
3557 *to = g_ascii_strtoull (s, &end, 0);
3558 /* a range error is a definitive no-no */
3559 if (errno == ERANGE) {
3560 return FALSE;
3561 }
3562
3563 if (*end == 0) {
3564 ret = TRUE;
3565 } else {
3566 if (g_ascii_strcasecmp (s, "little_endian") == 0) {
3567 *to = G_LITTLE_ENDIAN;
3568 ret = TRUE;
3569 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
3570 *to = G_BIG_ENDIAN;
3571 ret = TRUE;
3572 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
3573 *to = G_BYTE_ORDER;
3574 ret = TRUE;
3575 } else if (g_ascii_strcasecmp (s, "min") == 0) {
3576 *to = min;
3577 ret = TRUE;
3578 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3579 *to = max;
3580 ret = TRUE;
3581 }
3582 }
3583 if (ret) {
3584 /* by definition, a gint64 fits into a gint64; so ignore those */
3585 if (size != sizeof (mask)) {
3586 if (*to >= 0) {
3587 /* for positive numbers, we create a mask of 1's outside of the range
3588 * and 0's inside the range. An and will thus keep only 1 bits
3589 * outside of the range */
3590 mask <<= (size * 8);
3591 if ((mask & *to) != 0) {
3592 ret = FALSE;
3593 }
3594 } else {
3595 /* for negative numbers, we do a 2's complement version */
3596 mask <<= ((size * 8) - 1);
3597 if ((mask & *to) != mask) {
3598 ret = FALSE;
3599 }
3600 }
3601 }
3602 }
3603 return ret;
3604 }
3605
3606 #define CREATE_SERIALIZATION(_type,_macro) \
3607 CREATE_SERIALIZATION_START(_type,_macro) \
3608 \
3609 static gboolean \
3610 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
3611 { \
3612 gint64 x; \
3613 \
3614 if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
3615 G_MAX ## _macro, sizeof (g ## _type))) { \
3616 g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
3617 return TRUE; \
3618 } else { \
3619 return FALSE; \
3620 } \
3621 }
3622
3623 #define CREATE_USERIALIZATION(_type,_macro) \
3624 CREATE_SERIALIZATION_START(_type,_macro) \
3625 \
3626 static gboolean \
3627 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
3628 { \
3629 gint64 x; \
3630 gchar *end; \
3631 gboolean ret = FALSE; \
3632 \
3633 errno = 0; \
3634 x = g_ascii_strtoull (s, &end, 0); \
3635 /* a range error is a definitive no-no */ \
3636 if (errno == ERANGE) { \
3637 return FALSE; \
3638 } \
3639 /* the cast ensures the range check later on makes sense */ \
3640 x = (g ## _type) x; \
3641 if (*end == 0) { \
3642 ret = TRUE; \
3643 } else { \
3644 if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
3645 x = G_LITTLE_ENDIAN; \
3646 ret = TRUE; \
3647 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
3648 x = G_BIG_ENDIAN; \
3649 ret = TRUE; \
3650 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
3651 x = G_BYTE_ORDER; \
3652 ret = TRUE; \
3653 } else if (g_ascii_strcasecmp (s, "min") == 0) { \
3654 x = 0; \
3655 ret = TRUE; \
3656 } else if (g_ascii_strcasecmp (s, "max") == 0) { \
3657 x = G_MAX ## _macro; \
3658 ret = TRUE; \
3659 } \
3660 } \
3661 if (ret) { \
3662 if (x > G_MAX ## _macro) { \
3663 ret = FALSE; \
3664 } else { \
3665 g_value_set_ ## _type (dest, x); \
3666 } \
3667 } \
3668 return ret; \
3669 }
3670
3671 CREATE_SERIALIZATION (int, INT);
3672 CREATE_SERIALIZATION (int64, INT64);
3673 CREATE_SERIALIZATION (long, LONG);
3674
3675 CREATE_USERIALIZATION (uint, UINT);
3676 CREATE_USERIALIZATION (uint64, UINT64);
3677 CREATE_USERIALIZATION (ulong, ULONG);
3678
3679 /* FIXME 2.0: remove this again, plugins shouldn't have uchar properties */
3680 #ifndef G_MAXUCHAR
3681 #define G_MAXUCHAR 255
3682 #endif
3683 CREATE_USERIALIZATION (uchar, UCHAR);
3684
3685 /**********
3686 * double *
3687 **********/
3688 static gint
gst_value_compare_double(const GValue * value1,const GValue * value2)3689 gst_value_compare_double (const GValue * value1, const GValue * value2)
3690 {
3691 if (value1->data[0].v_double > value2->data[0].v_double)
3692 return GST_VALUE_GREATER_THAN;
3693 if (value1->data[0].v_double < value2->data[0].v_double)
3694 return GST_VALUE_LESS_THAN;
3695 if (value1->data[0].v_double == value2->data[0].v_double)
3696 return GST_VALUE_EQUAL;
3697 return GST_VALUE_UNORDERED;
3698 }
3699
3700 static gchar *
gst_value_serialize_double(const GValue * value)3701 gst_value_serialize_double (const GValue * value)
3702 {
3703 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
3704
3705 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
3706 return g_strdup (d);
3707 }
3708
3709 static gboolean
gst_value_deserialize_double(GValue * dest,const gchar * s)3710 gst_value_deserialize_double (GValue * dest, const gchar * s)
3711 {
3712 gdouble x;
3713 gboolean ret = FALSE;
3714 gchar *end;
3715
3716 x = g_ascii_strtod (s, &end);
3717 if (*end == 0) {
3718 ret = TRUE;
3719 } else {
3720 if (g_ascii_strcasecmp (s, "min") == 0) {
3721 x = -G_MAXDOUBLE;
3722 ret = TRUE;
3723 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3724 x = G_MAXDOUBLE;
3725 ret = TRUE;
3726 }
3727 }
3728 if (ret) {
3729 g_value_set_double (dest, x);
3730 }
3731 return ret;
3732 }
3733
3734 /*********
3735 * float *
3736 *********/
3737
3738 static gint
gst_value_compare_float(const GValue * value1,const GValue * value2)3739 gst_value_compare_float (const GValue * value1, const GValue * value2)
3740 {
3741 if (value1->data[0].v_float > value2->data[0].v_float)
3742 return GST_VALUE_GREATER_THAN;
3743 if (value1->data[0].v_float < value2->data[0].v_float)
3744 return GST_VALUE_LESS_THAN;
3745 if (value1->data[0].v_float == value2->data[0].v_float)
3746 return GST_VALUE_EQUAL;
3747 return GST_VALUE_UNORDERED;
3748 }
3749
3750 static gchar *
gst_value_serialize_float(const GValue * value)3751 gst_value_serialize_float (const GValue * value)
3752 {
3753 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
3754
3755 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
3756 return g_strdup (d);
3757 }
3758
3759 static gboolean
gst_value_deserialize_float(GValue * dest,const gchar * s)3760 gst_value_deserialize_float (GValue * dest, const gchar * s)
3761 {
3762 gdouble x;
3763 gboolean ret = FALSE;
3764 gchar *end;
3765
3766 x = g_ascii_strtod (s, &end);
3767 if (*end == 0) {
3768 ret = TRUE;
3769 } else {
3770 if (g_ascii_strcasecmp (s, "min") == 0) {
3771 x = -G_MAXFLOAT;
3772 ret = TRUE;
3773 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3774 x = G_MAXFLOAT;
3775 ret = TRUE;
3776 }
3777 }
3778 if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
3779 ret = FALSE;
3780 if (ret) {
3781 g_value_set_float (dest, (float) x);
3782 }
3783 return ret;
3784 }
3785
3786 /**********
3787 * string *
3788 **********/
3789
3790 static gint
gst_value_compare_string(const GValue * value1,const GValue * value2)3791 gst_value_compare_string (const GValue * value1, const GValue * value2)
3792 {
3793 if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
3794 /* if only one is NULL, no match - otherwise both NULL == EQUAL */
3795 if (value1->data[0].v_pointer != value2->data[0].v_pointer)
3796 return GST_VALUE_UNORDERED;
3797 } else {
3798 gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
3799
3800 if (x < 0)
3801 return GST_VALUE_LESS_THAN;
3802 if (x > 0)
3803 return GST_VALUE_GREATER_THAN;
3804 }
3805
3806 return GST_VALUE_EQUAL;
3807 }
3808
3809 static gint
gst_string_measure_wrapping(const gchar * s)3810 gst_string_measure_wrapping (const gchar * s)
3811 {
3812 gint len;
3813 gboolean wrap = FALSE;
3814
3815 if (G_UNLIKELY (s == NULL))
3816 return -1;
3817
3818 /* Special case: the actual string NULL needs wrapping */
3819 if (G_UNLIKELY (strcmp (s, "NULL") == 0))
3820 return 4;
3821
3822 len = 0;
3823 while (*s) {
3824 if (GST_ASCII_IS_STRING (*s)) {
3825 len++;
3826 } else if (*s < 0x20 || *s >= 0x7f) {
3827 wrap = TRUE;
3828 len += 4;
3829 } else {
3830 wrap = TRUE;
3831 len += 2;
3832 }
3833 s++;
3834 }
3835
3836 /* Wrap the string if we found something that needs
3837 * wrapping, or the empty string (len == 0) */
3838 return (wrap || len == 0) ? len : -1;
3839 }
3840
3841 static gchar *
gst_string_wrap_inner(const gchar * s,gint len)3842 gst_string_wrap_inner (const gchar * s, gint len)
3843 {
3844 gchar *d, *e;
3845
3846 e = d = g_malloc (len + 3);
3847
3848 *e++ = '\"';
3849 while (*s) {
3850 if (GST_ASCII_IS_STRING (*s)) {
3851 *e++ = *s++;
3852 } else if (*s < 0x20 || *s >= 0x7f) {
3853 *e++ = '\\';
3854 *e++ = '0' + ((*(guchar *) s) >> 6);
3855 *e++ = '0' + (((*s) >> 3) & 0x7);
3856 *e++ = '0' + ((*s++) & 0x7);
3857 } else {
3858 *e++ = '\\';
3859 *e++ = *s++;
3860 }
3861 }
3862 *e++ = '\"';
3863 *e = 0;
3864
3865 g_assert (e - d <= len + 3);
3866 return d;
3867 }
3868
3869 /* Do string wrapping/escaping */
3870 static gchar *
gst_string_wrap(const gchar * s)3871 gst_string_wrap (const gchar * s)
3872 {
3873 gint len = gst_string_measure_wrapping (s);
3874
3875 if (G_LIKELY (len < 0))
3876 return g_strdup (s);
3877
3878 return gst_string_wrap_inner (s, len);
3879 }
3880
3881 /* Same as above, but take ownership of the string */
3882 gchar *
priv_gst_string_take_and_wrap(gchar * s)3883 priv_gst_string_take_and_wrap (gchar * s)
3884 {
3885 gchar *out;
3886 gint len = gst_string_measure_wrapping (s);
3887
3888 if (G_LIKELY (len < 0))
3889 return s;
3890
3891 out = gst_string_wrap_inner (s, len);
3892 g_free (s);
3893
3894 return out;
3895 }
3896
3897 /*
3898 * This function takes a string delimited with double quotes (")
3899 * and unescapes any \xxx octal numbers.
3900 *
3901 * If sequences of \y are found where y is not in the range of
3902 * 0->3, y is copied unescaped.
3903 *
3904 * If \xyy is found where x is an octal number but y is not, an
3905 * error is encountered and %NULL is returned.
3906 *
3907 * the input string must be \0 terminated.
3908 */
3909 static gchar *
gst_string_unwrap(const gchar * s)3910 gst_string_unwrap (const gchar * s)
3911 {
3912 gchar *ret;
3913 gchar *read, *write;
3914
3915 /* NULL string returns NULL */
3916 if (s == NULL)
3917 return NULL;
3918
3919 /* strings not starting with " are invalid */
3920 if (*s != '"')
3921 return NULL;
3922
3923 /* make copy of original string to hold the result. This
3924 * string will always be smaller than the original */
3925 ret = g_strdup (s);
3926 read = ret;
3927 write = ret;
3928
3929 /* need to move to the next position as we parsed the " */
3930 read++;
3931
3932 while (*read) {
3933 if (GST_ASCII_IS_STRING (*read)) {
3934 /* normal chars are just copied */
3935 *write++ = *read++;
3936 } else if (*read == '"') {
3937 /* quote marks end of string */
3938 break;
3939 } else if (*read == '\\') {
3940 /* got an escape char, move to next position to read a tripplet
3941 * of octal numbers */
3942 read++;
3943 /* is the next char a possible first octal number? */
3944 if (*read >= '0' && *read <= '3') {
3945 /* parse other 2 numbers, if one of them is not in the range of
3946 * an octal number, we error. We also catch the case where a zero
3947 * byte is found here. */
3948 if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
3949 goto beach;
3950
3951 /* now convert the octal number to a byte again. */
3952 *write++ = ((read[0] - '0') << 6) +
3953 ((read[1] - '0') << 3) + (read[2] - '0');
3954
3955 read += 3;
3956 } else {
3957 /* if we run into a \0 here, we definitely won't get a quote later */
3958 if (*read == 0)
3959 goto beach;
3960 /* else copy \X sequence */
3961 *write++ = *read++;
3962 }
3963 } else if (*read == '\0') {
3964 goto beach;
3965 } else {
3966 *write++ = *read++;
3967 }
3968 }
3969 /* if the string is not ending in " and zero terminated, we error */
3970 if (*read != '"' || read[1] != '\0')
3971 goto beach;
3972
3973 /* null terminate result string and return */
3974 *write = '\0';
3975 return ret;
3976
3977 beach:
3978 g_free (ret);
3979 return NULL;
3980 }
3981
3982 static gchar *
gst_value_serialize_string(const GValue * value)3983 gst_value_serialize_string (const GValue * value)
3984 {
3985 return gst_string_wrap (value->data[0].v_pointer);
3986 }
3987
3988 static gboolean
gst_value_deserialize_string(GValue * dest,const gchar * s)3989 gst_value_deserialize_string (GValue * dest, const gchar * s)
3990 {
3991 if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
3992 g_value_set_string (dest, NULL);
3993 return TRUE;
3994 } else if (G_LIKELY (*s != '"' || s[strlen (s) - 1] != '"')) {
3995 if (!g_utf8_validate (s, -1, NULL))
3996 return FALSE;
3997 g_value_set_string (dest, s);
3998 return TRUE;
3999 } else {
4000 /* strings delimited with double quotes should be unwrapped */
4001 gchar *str = gst_string_unwrap (s);
4002 if (G_UNLIKELY (!str))
4003 return FALSE;
4004 if (!g_utf8_validate (str, -1, NULL)) {
4005 g_free (str);
4006 return FALSE;
4007 }
4008 g_value_take_string (dest, str);
4009 }
4010
4011 return TRUE;
4012 }
4013
4014 /********
4015 * enum *
4016 ********/
4017
4018 static gint
gst_value_compare_enum(const GValue * value1,const GValue * value2)4019 gst_value_compare_enum (const GValue * value1, const GValue * value2)
4020 {
4021 GEnumValue *en1, *en2;
4022 GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
4023 GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
4024
4025 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
4026 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
4027 en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
4028 en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
4029 g_type_class_unref (klass1);
4030 g_type_class_unref (klass2);
4031 g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
4032 g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
4033 if (en1->value < en2->value)
4034 return GST_VALUE_LESS_THAN;
4035 if (en1->value > en2->value)
4036 return GST_VALUE_GREATER_THAN;
4037
4038 return GST_VALUE_EQUAL;
4039 }
4040
4041 static gchar *
gst_value_serialize_enum(const GValue * value)4042 gst_value_serialize_enum (const GValue * value)
4043 {
4044 GEnumValue *en;
4045 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
4046
4047 g_return_val_if_fail (klass, NULL);
4048 en = g_enum_get_value (klass, g_value_get_enum (value));
4049 g_type_class_unref (klass);
4050
4051 /* might be one of the custom formats registered later */
4052 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
4053 const GstFormatDefinition *format_def;
4054
4055 format_def = gst_format_get_details ((GstFormat) g_value_get_enum (value));
4056 g_return_val_if_fail (format_def != NULL, NULL);
4057 return g_strdup (format_def->description);
4058 }
4059
4060 g_return_val_if_fail (en, NULL);
4061 return g_strdup (en->value_nick);
4062 }
4063
4064 static gint
gst_value_deserialize_enum_iter_cmp(const GValue * format_def_value,const gchar * s)4065 gst_value_deserialize_enum_iter_cmp (const GValue * format_def_value,
4066 const gchar * s)
4067 {
4068 const GstFormatDefinition *format_def =
4069 g_value_get_pointer (format_def_value);
4070
4071 if (g_ascii_strcasecmp (s, format_def->nick) == 0)
4072 return 0;
4073
4074 return g_ascii_strcasecmp (s, format_def->description);
4075 }
4076
4077 static gboolean
gst_value_deserialize_enum(GValue * dest,const gchar * s)4078 gst_value_deserialize_enum (GValue * dest, const gchar * s)
4079 {
4080 GEnumValue *en;
4081 gchar *endptr = NULL;
4082 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
4083
4084 g_return_val_if_fail (klass, FALSE);
4085 if (!(en = g_enum_get_value_by_name (klass, s))) {
4086 if (!(en = g_enum_get_value_by_nick (klass, s))) {
4087 gint i = strtol (s, &endptr, 0);
4088
4089 if (endptr && *endptr == '\0') {
4090 en = g_enum_get_value (klass, i);
4091 }
4092 }
4093 }
4094 g_type_class_unref (klass);
4095
4096 /* might be one of the custom formats registered later */
4097 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
4098 GValue res = { 0, };
4099 const GstFormatDefinition *format_def;
4100 GstIterator *iter;
4101 gboolean found;
4102
4103 iter = gst_format_iterate_definitions ();
4104
4105 found = gst_iterator_find_custom (iter,
4106 (GCompareFunc) gst_value_deserialize_enum_iter_cmp, &res, (gpointer) s);
4107
4108 if (found) {
4109 format_def = g_value_get_pointer (&res);
4110 g_return_val_if_fail (format_def != NULL, FALSE);
4111 g_value_set_enum (dest, (gint) format_def->value);
4112 g_value_unset (&res);
4113 }
4114 gst_iterator_free (iter);
4115 return found;
4116 }
4117
4118 /* enum name/nick not found */
4119 if (en == NULL)
4120 return FALSE;
4121
4122 g_value_set_enum (dest, en->value);
4123 return TRUE;
4124 }
4125
4126 /********
4127 * flags *
4128 ********/
4129
4130 /* we just compare the value here */
4131 static gint
gst_value_compare_gflags(const GValue * value1,const GValue * value2)4132 gst_value_compare_gflags (const GValue * value1, const GValue * value2)
4133 {
4134 guint fl1, fl2;
4135 GFlagsClass *klass1 =
4136 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
4137 GFlagsClass *klass2 =
4138 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
4139
4140 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
4141 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
4142 fl1 = g_value_get_flags (value1);
4143 fl2 = g_value_get_flags (value2);
4144 g_type_class_unref (klass1);
4145 g_type_class_unref (klass2);
4146 if (fl1 < fl2)
4147 return GST_VALUE_LESS_THAN;
4148 if (fl1 > fl2)
4149 return GST_VALUE_GREATER_THAN;
4150
4151 return GST_VALUE_EQUAL;
4152 }
4153
4154 /* the different flags are serialized separated with a + */
4155 static gchar *
gst_value_serialize_gflags(const GValue * value)4156 gst_value_serialize_gflags (const GValue * value)
4157 {
4158 guint flags;
4159 GFlagsValue *fl;
4160 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
4161 gchar *result, *tmp;
4162 gboolean first = TRUE;
4163
4164 g_return_val_if_fail (klass, NULL);
4165
4166 flags = g_value_get_flags (value);
4167
4168 /* if no flags are set, try to serialize to the _NONE string */
4169 if (!flags) {
4170 fl = g_flags_get_first_value (klass, flags);
4171 if (fl)
4172 return g_strdup (fl->value_name);
4173 else
4174 return g_strdup ("0");
4175 }
4176
4177 /* some flags are set, so serialize one by one */
4178 result = g_strdup ("");
4179 while (flags) {
4180 fl = g_flags_get_first_value (klass, flags);
4181 if (fl != NULL) {
4182 tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
4183 g_free (result);
4184 result = tmp;
4185 first = FALSE;
4186
4187 /* clear flag */
4188 flags &= ~fl->value;
4189 }
4190 }
4191 g_type_class_unref (klass);
4192
4193 return result;
4194 }
4195
4196 static gboolean
gst_value_gflags_str_to_flags(GFlagsClass * klass,const gchar * s,guint * out_flags,guint * out_mask)4197 gst_value_gflags_str_to_flags (GFlagsClass * klass, const gchar * s,
4198 guint * out_flags, guint * out_mask)
4199 {
4200 GFlagsValue *fl;
4201 gchar delimiter;
4202 const gchar *pos = NULL;
4203 const gchar *next;
4204 gchar *cur_str, *endptr;
4205 guint flags = 0;
4206 guint mask = 0;
4207 guint val;
4208
4209 g_return_val_if_fail (klass, FALSE);
4210
4211 /* split into parts delimited with + or / and
4212 * compose the set of flags and mask. */
4213 pos = s;
4214
4215 if (*pos == '\0')
4216 goto done; /* Empty string, nothing to do */
4217
4218 /* As a special case if the first char isn't a delimiter, assume
4219 * it's a '+' - for GFlags strings, which don't start with a
4220 * delimiter, while GFlagSet always will */
4221 if (*pos == '/' || *pos == '+') {
4222 delimiter = *pos;
4223 pos++;
4224 } else {
4225 delimiter = '+';
4226 }
4227
4228 do {
4229 /* Find the next delimiter */
4230 next = pos;
4231 while (*next != '\0' && *next != '+' && *next != '/')
4232 next++;
4233 cur_str = g_strndup (pos, next - pos);
4234
4235 if ((fl = g_flags_get_value_by_name (klass, cur_str)))
4236 val = fl->value;
4237 else if ((fl = g_flags_get_value_by_nick (klass, cur_str)))
4238 val = fl->value;
4239 else {
4240 val = strtoul (cur_str, &endptr, 0);
4241 /* direct numeric value */
4242 if (endptr == NULL || *endptr != '\0') {
4243 g_free (cur_str);
4244 return FALSE; /* Invalid numeric or string we can't convert */
4245 }
4246 }
4247 g_free (cur_str);
4248
4249 if (val) {
4250 mask |= val;
4251 if (delimiter == '+')
4252 flags |= val;
4253 }
4254
4255 /* Advance to the next delimiter */
4256 pos = next;
4257 delimiter = *pos;
4258 pos++;
4259 } while (delimiter != '\0');
4260
4261 done:
4262 if (out_flags)
4263 *out_flags = flags;
4264 if (out_mask)
4265 *out_mask = mask;
4266
4267 return TRUE;
4268 }
4269
4270
4271 static gboolean
gst_value_deserialize_gflags(GValue * dest,const gchar * s)4272 gst_value_deserialize_gflags (GValue * dest, const gchar * s)
4273 {
4274 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
4275 gboolean res = FALSE;
4276 guint flags = 0;
4277
4278 if (gst_value_gflags_str_to_flags (klass, s, &flags, NULL)) {
4279 g_value_set_flags (dest, flags);
4280 res = TRUE;
4281 }
4282
4283 g_type_class_unref (klass);
4284
4285 return res;
4286 }
4287
4288 /*********
4289 * gtype *
4290 *********/
4291
4292 static gint
gst_value_compare_gtype(const GValue * value1,const GValue * value2)4293 gst_value_compare_gtype (const GValue * value1, const GValue * value2)
4294 {
4295 if (value1->data[0].v_pointer == value2->data[0].v_pointer)
4296 return GST_VALUE_EQUAL;
4297 return GST_VALUE_UNORDERED;
4298 }
4299
4300 static gchar *
gst_value_serialize_gtype(const GValue * value)4301 gst_value_serialize_gtype (const GValue * value)
4302 {
4303 return g_strdup (g_type_name (g_value_get_gtype (value)));
4304 }
4305
4306 static gboolean
gst_value_deserialize_gtype(GValue * dest,const gchar * s)4307 gst_value_deserialize_gtype (GValue * dest, const gchar * s)
4308 {
4309 GType t = g_type_from_name (s);
4310 gboolean ret = TRUE;
4311
4312 if (t == G_TYPE_INVALID)
4313 ret = FALSE;
4314 if (ret) {
4315 g_value_set_gtype (dest, t);
4316 }
4317 return ret;
4318 }
4319
4320 /****************
4321 * subset *
4322 ****************/
4323
4324 static gboolean
gst_value_is_subset_int_range_int_range(const GValue * value1,const GValue * value2)4325 gst_value_is_subset_int_range_int_range (const GValue * value1,
4326 const GValue * value2)
4327 {
4328 gint gcd;
4329
4330 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value1), FALSE);
4331 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value2), FALSE);
4332
4333 if (INT_RANGE_MIN (value1) * INT_RANGE_STEP (value1) <
4334 INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2))
4335 return FALSE;
4336 if (INT_RANGE_MAX (value1) * INT_RANGE_STEP (value1) >
4337 INT_RANGE_MAX (value2) * INT_RANGE_STEP (value2))
4338 return FALSE;
4339
4340 if (INT_RANGE_MIN (value2) == INT_RANGE_MAX (value2)) {
4341 if ((INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2)) %
4342 INT_RANGE_STEP (value1))
4343 return FALSE;
4344 return TRUE;
4345 }
4346
4347 gcd =
4348 gst_util_greatest_common_divisor (INT_RANGE_STEP (value1),
4349 INT_RANGE_STEP (value2));
4350 if (gcd != MIN (INT_RANGE_STEP (value1), INT_RANGE_STEP (value2)))
4351 return FALSE;
4352
4353 return TRUE;
4354 }
4355
4356 static gboolean
gst_value_is_subset_int64_range_int64_range(const GValue * value1,const GValue * value2)4357 gst_value_is_subset_int64_range_int64_range (const GValue * value1,
4358 const GValue * value2)
4359 {
4360 gint64 gcd;
4361
4362 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value1), FALSE);
4363 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value2), FALSE);
4364
4365 if (INT64_RANGE_MIN (value1) < INT64_RANGE_MIN (value2))
4366 return FALSE;
4367 if (INT64_RANGE_MAX (value1) > INT64_RANGE_MAX (value2))
4368 return FALSE;
4369
4370 if (INT64_RANGE_MIN (value2) == INT64_RANGE_MAX (value2)) {
4371 if ((INT64_RANGE_MIN (value2) * INT64_RANGE_STEP (value2)) %
4372 INT64_RANGE_STEP (value1))
4373 return FALSE;
4374 return TRUE;
4375 }
4376
4377 gcd =
4378 gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (value1),
4379 INT64_RANGE_STEP (value2));
4380 if (gcd != MIN (INT64_RANGE_STEP (value1), INT64_RANGE_STEP (value2)))
4381 return FALSE;
4382
4383 return TRUE;
4384 }
4385
4386 /* A flag set is a subset of another if the superset allows the
4387 * flags of the subset */
4388 static gboolean
gst_value_is_subset_flagset_flagset(const GValue * value1,const GValue * value2)4389 gst_value_is_subset_flagset_flagset (const GValue * value1,
4390 const GValue * value2)
4391 {
4392 guint f1, f2;
4393 guint m1, m2;
4394
4395 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (value1), FALSE);
4396 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (value2), FALSE);
4397
4398 f1 = value1->data[0].v_uint;
4399 f2 = value2->data[0].v_uint;
4400
4401 m1 = value1->data[1].v_uint;
4402 m2 = value2->data[1].v_uint;
4403
4404 /* Not a subset if masked bits of superset disagree */
4405 if ((f1 & m1) != (f2 & (m1 & m2)))
4406 return FALSE;
4407
4408 return TRUE;
4409 }
4410
4411 static gboolean
gst_value_is_subset_structure_structure(const GValue * value1,const GValue * value2)4412 gst_value_is_subset_structure_structure (const GValue * value1,
4413 const GValue * value2)
4414 {
4415 const GstStructure *s1, *s2;
4416
4417 g_return_val_if_fail (GST_VALUE_HOLDS_STRUCTURE (value1), FALSE);
4418 g_return_val_if_fail (GST_VALUE_HOLDS_STRUCTURE (value2), FALSE);
4419
4420 s1 = gst_value_get_structure (value1);
4421 s2 = gst_value_get_structure (value2);
4422
4423 return gst_structure_is_subset (s1, s2);
4424 }
4425
4426 static gboolean
gst_value_is_subset_list_list(const GValue * value1,const GValue * value2)4427 gst_value_is_subset_list_list (const GValue * value1, const GValue * value2)
4428 {
4429 GstValueList *vlist1 = VALUE_LIST_ARRAY (value1);
4430 GstValueList *vlist2 = VALUE_LIST_ARRAY (value2);
4431 gint it1, len1, it2, len2;
4432
4433 len1 = vlist1->len;
4434 len2 = vlist2->len;
4435
4436 /* A list can't be a subset of a smaller list */
4437 if (len1 > len2)
4438 return FALSE;
4439
4440 /* Check if all elements of the first list are within the 2nd list */
4441 for (it1 = 0; it1 < len1; it1++) {
4442 const GValue *child1 = &vlist1->fields[it1];
4443 gboolean seen = FALSE;
4444
4445 for (it2 = 0; it2 < len2; it2++) {
4446 const GValue *child2 = &vlist2->fields[it2];
4447 if (gst_value_compare (child1, child2) == GST_VALUE_EQUAL) {
4448 seen = TRUE;
4449 break;
4450 }
4451 }
4452 if (!seen)
4453 return FALSE;
4454 }
4455
4456 return TRUE;
4457 }
4458
4459 static gboolean
gst_value_is_subset_list(const GValue * value1,const GValue * value2)4460 gst_value_is_subset_list (const GValue * value1, const GValue * value2)
4461 {
4462 GstValueList *vlist2 = VALUE_LIST_ARRAY (value2);
4463 gint it2, len2;
4464
4465 len2 = vlist2->len;
4466
4467 /* Check whether value1 is within the list */
4468 for (it2 = 0; it2 < len2; it2++) {
4469 const GValue *child2 = &vlist2->fields[it2];
4470 if (gst_value_compare (value1, child2) == GST_VALUE_EQUAL) {
4471 return TRUE;
4472 }
4473 }
4474
4475 return FALSE;
4476 }
4477
4478 /**
4479 * gst_value_is_subset:
4480 * @value1: a #GValue
4481 * @value2: a #GValue
4482 *
4483 * Check that @value1 is a subset of @value2.
4484 *
4485 * Return: %TRUE is @value1 is a subset of @value2
4486 */
4487 gboolean
gst_value_is_subset(const GValue * value1,const GValue * value2)4488 gst_value_is_subset (const GValue * value1, const GValue * value2)
4489 {
4490 GType type1 = G_VALUE_TYPE (value1);
4491 GType type2 = G_VALUE_TYPE (value2);
4492
4493 /* special case for int/int64 ranges, since we cannot compute
4494 the difference for those when they have different steps,
4495 and it's actually a lot simpler to compute whether a range
4496 is a subset of another. */
4497 if (GST_VALUE_HOLDS_INT_RANGE (value1) && GST_VALUE_HOLDS_INT_RANGE (value2)) {
4498 return gst_value_is_subset_int_range_int_range (value1, value2);
4499 } else if (GST_VALUE_HOLDS_INT64_RANGE (value1)
4500 && GST_VALUE_HOLDS_INT64_RANGE (value2)) {
4501 return gst_value_is_subset_int64_range_int64_range (value1, value2);
4502 } else if (GST_VALUE_HOLDS_FLAG_SET (value1) &&
4503 GST_VALUE_HOLDS_FLAG_SET (value2)) {
4504 return gst_value_is_subset_flagset_flagset (value1, value2);
4505 } else if (GST_VALUE_HOLDS_STRUCTURE (value1)
4506 && GST_VALUE_HOLDS_STRUCTURE (value2)) {
4507 return gst_value_is_subset_structure_structure (value1, value2);
4508 } else if (type2 == GST_TYPE_LIST) {
4509 if (type1 == GST_TYPE_LIST)
4510 return gst_value_is_subset_list_list (value1, value2);
4511 return gst_value_is_subset_list (value1, value2);
4512 }
4513
4514 /*
4515 * 1 - [1,2] = empty
4516 * -> !subset
4517 *
4518 * [1,2] - 1 = 2
4519 * -> 1 - [1,2] = empty
4520 * -> subset
4521 *
4522 * [1,3] - [1,2] = 3
4523 * -> [1,2] - [1,3] = empty
4524 * -> subset
4525 *
4526 * {1,2} - {1,3} = 2
4527 * -> {1,3} - {1,2} = 3
4528 * -> !subset
4529 *
4530 * First caps subtraction needs to return a non-empty set, second
4531 * subtractions needs to give en empty set.
4532 * Both substractions are switched below, as it's faster that way.
4533 */
4534 if (!gst_value_subtract (NULL, value1, value2)) {
4535 if (gst_value_subtract (NULL, value2, value1)) {
4536 return TRUE;
4537 }
4538 }
4539 return FALSE;
4540 }
4541
4542 /*********
4543 * union *
4544 *********/
4545
4546 static gboolean
gst_value_union_int_int_range(GValue * dest,const GValue * src1,const GValue * src2)4547 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
4548 const GValue * src2)
4549 {
4550 gint v = src1->data[0].v_int;
4551
4552 /* check if it's already in the range */
4553 if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= v &&
4554 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= v &&
4555 v % INT_RANGE_STEP (src2) == 0) {
4556 if (dest)
4557 gst_value_init_and_copy (dest, src2);
4558 return TRUE;
4559 }
4560
4561 /* check if it extends the range */
4562 if (v == (INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2)) {
4563 if (dest) {
4564 guint64 new_min = INT_RANGE_MIN (src2) - 1;
4565 guint64 new_max = INT_RANGE_MAX (src2);
4566
4567 gst_value_init_and_copy (dest, src2);
4568 dest->data[0].v_uint64 = (new_min << 32) | (new_max);
4569 }
4570 return TRUE;
4571 }
4572 if (v == (INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2)) {
4573 if (dest) {
4574 guint64 new_min = INT_RANGE_MIN (src2);
4575 guint64 new_max = INT_RANGE_MAX (src2) + 1;
4576
4577 gst_value_init_and_copy (dest, src2);
4578 dest->data[0].v_uint64 = (new_min << 32) | (new_max);
4579 }
4580 return TRUE;
4581 }
4582
4583 return FALSE;
4584 }
4585
4586 static gboolean
gst_value_union_int_range_int_range(GValue * dest,const GValue * src1,const GValue * src2)4587 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
4588 const GValue * src2)
4589 {
4590 /* We can union in several special cases:
4591 1 - one is a subset of another
4592 2 - same step and not disjoint
4593 3 - different step, at least one with one value which matches a 'next' or 'previous'
4594 - anything else ?
4595 */
4596
4597 /* 1 - subset */
4598 if (gst_value_is_subset_int_range_int_range (src1, src2)) {
4599 if (dest)
4600 gst_value_init_and_copy (dest, src2);
4601 return TRUE;
4602 }
4603 if (gst_value_is_subset_int_range_int_range (src2, src1)) {
4604 if (dest)
4605 gst_value_init_and_copy (dest, src1);
4606 return TRUE;
4607 }
4608
4609 /* 2 - same step and not disjoint */
4610 if (INT_RANGE_STEP (src1) == INT_RANGE_STEP (src2)) {
4611 if ((INT_RANGE_MIN (src1) <= INT_RANGE_MAX (src2) + 1 &&
4612 INT_RANGE_MAX (src1) >= INT_RANGE_MIN (src2) - 1) ||
4613 (INT_RANGE_MIN (src2) <= INT_RANGE_MAX (src1) + 1 &&
4614 INT_RANGE_MAX (src2) >= INT_RANGE_MIN (src1) - 1)) {
4615 if (dest) {
4616 gint step = INT_RANGE_STEP (src1);
4617 gint min = step * MIN (INT_RANGE_MIN (src1), INT_RANGE_MIN (src2));
4618 gint max = step * MAX (INT_RANGE_MAX (src1), INT_RANGE_MAX (src2));
4619 g_value_init (dest, GST_TYPE_INT_RANGE);
4620 gst_value_set_int_range_step (dest, min, max, step);
4621 }
4622 return TRUE;
4623 }
4624 }
4625
4626 /* 3 - single value matches next or previous */
4627 if (INT_RANGE_STEP (src1) != INT_RANGE_STEP (src2)) {
4628 gint n1 = INT_RANGE_MAX (src1) - INT_RANGE_MIN (src1) + 1;
4629 gint n2 = INT_RANGE_MAX (src2) - INT_RANGE_MIN (src2) + 1;
4630 if (n1 == 1 || n2 == 1) {
4631 const GValue *range_value = NULL;
4632 gint scalar = 0;
4633 if (n1 == 1) {
4634 range_value = src2;
4635 scalar = INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1);
4636 } else if (n2 == 1) {
4637 range_value = src1;
4638 scalar = INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2);
4639 }
4640
4641 if (scalar ==
4642 (INT_RANGE_MIN (range_value) - 1) * INT_RANGE_STEP (range_value)) {
4643 if (dest) {
4644 guint64 new_min = (guint)
4645 ((INT_RANGE_MIN (range_value) -
4646 1) * INT_RANGE_STEP (range_value));
4647 guint64 new_max = (guint)
4648 (INT_RANGE_MAX (range_value) * INT_RANGE_STEP (range_value));
4649
4650 gst_value_init_and_copy (dest, range_value);
4651 dest->data[0].v_uint64 = (new_min << 32) | (new_max);
4652 }
4653 return TRUE;
4654 } else if (scalar ==
4655 (INT_RANGE_MAX (range_value) + 1) * INT_RANGE_STEP (range_value)) {
4656 if (dest) {
4657 guint64 new_min = (guint)
4658 (INT_RANGE_MIN (range_value) * INT_RANGE_STEP (range_value));
4659 guint64 new_max = (guint)
4660 ((INT_RANGE_MAX (range_value) +
4661 1) * INT_RANGE_STEP (range_value));
4662 gst_value_init_and_copy (dest, range_value);
4663 dest->data[0].v_uint64 = (new_min << 32) | (new_max);
4664 }
4665 return TRUE;
4666 }
4667 }
4668 }
4669
4670 /* If we get there, we did not find a way to make a union that can be
4671 represented with our simplistic model. */
4672 return FALSE;
4673 }
4674
4675 static gboolean
gst_value_union_flagset_flagset(GValue * dest,const GValue * src1,const GValue * src2)4676 gst_value_union_flagset_flagset (GValue * dest, const GValue * src1,
4677 const GValue * src2)
4678 {
4679 /* We can union 2 flag sets where they do not disagree on
4680 * required (masked) flag bits */
4681 guint64 f1, f2;
4682 guint64 m1, m2;
4683
4684 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (src1), FALSE);
4685 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (src2), FALSE);
4686
4687 f1 = src1->data[0].v_uint;
4688 f2 = src2->data[0].v_uint;
4689
4690 m1 = src1->data[1].v_uint;
4691 m2 = src2->data[1].v_uint;
4692
4693 /* Can't union if masked bits disagree */
4694 if ((f1 & (m1 & m2)) != (f2 & (m1 & m2)))
4695 return FALSE;
4696
4697 if (dest) {
4698 g_value_init (dest, GST_TYPE_FLAG_SET);
4699 /* Copy masked bits from src2 to src1 */
4700 f1 &= ~m2;
4701 f1 |= (f2 & m2);
4702 m1 |= m2;
4703 gst_value_set_flagset (dest, f1, m1);
4704 }
4705
4706 return TRUE;
4707 }
4708
4709 /* iterating over the result taking the union with the other structure's value */
4710 static gboolean
structure_field_union_into(GQuark field_id,GValue * val,gpointer user_data)4711 structure_field_union_into (GQuark field_id, GValue * val, gpointer user_data)
4712 {
4713 GstStructure *other = user_data;
4714 const GValue *other_value;
4715 GValue res_value = G_VALUE_INIT;
4716
4717 other_value = gst_structure_id_get_value (other, field_id);
4718 /* no value in the other struct, just keep this value */
4719 if (!other_value)
4720 return TRUE;
4721
4722 if (!gst_value_union (&res_value, val, other_value))
4723 return FALSE;
4724
4725 g_value_unset (val);
4726 gst_value_move (val, &res_value);
4727 return TRUE;
4728 }
4729
4730 /* iterating over the other source structure adding missing values */
4731 static gboolean
structure_field_union_from(GQuark field_id,const GValue * other_val,gpointer user_data)4732 structure_field_union_from (GQuark field_id, const GValue * other_val,
4733 gpointer user_data)
4734 {
4735 GstStructure *result = user_data;
4736 const GValue *result_value;
4737
4738 result_value = gst_structure_id_get_value (result, field_id);
4739 if (!result_value)
4740 gst_structure_id_set_value (result, field_id, other_val);
4741
4742 return TRUE;
4743 }
4744
4745 static gboolean
gst_value_union_structure_structure(GValue * dest,const GValue * src1,const GValue * src2)4746 gst_value_union_structure_structure (GValue * dest, const GValue * src1,
4747 const GValue * src2)
4748 {
4749 const GstStructure *s1, *s2;
4750 GstStructure *result;
4751 gboolean ret;
4752
4753 g_return_val_if_fail (GST_VALUE_HOLDS_STRUCTURE (src1), FALSE);
4754 g_return_val_if_fail (GST_VALUE_HOLDS_STRUCTURE (src2), FALSE);
4755
4756 s1 = gst_value_get_structure (src1);
4757 s2 = gst_value_get_structure (src2);
4758
4759 /* Can't join two structures with different names into a single structure */
4760 if (!gst_structure_has_name (s1, gst_structure_get_name (s2))) {
4761 gst_value_list_concat (dest, src1, src2);
4762 return TRUE;
4763 }
4764
4765 result = gst_structure_copy (s1);
4766 ret =
4767 gst_structure_map_in_place (result, structure_field_union_into,
4768 (gpointer) s2);
4769 if (!ret)
4770 goto out;
4771 ret =
4772 gst_structure_foreach (s2, structure_field_union_from, (gpointer) result);
4773
4774 if (ret) {
4775 g_value_init (dest, GST_TYPE_STRUCTURE);
4776 gst_value_set_structure (dest, result);
4777 }
4778
4779 out:
4780 gst_structure_free (result);
4781 return ret;
4782 }
4783
4784 /****************
4785 * intersection *
4786 ****************/
4787
4788 static gboolean
gst_value_intersect_int_int_range(GValue * dest,const GValue * src1,const GValue * src2)4789 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
4790 const GValue * src2)
4791 {
4792 if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= src1->data[0].v_int &&
4793 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= src1->data[0].v_int &&
4794 src1->data[0].v_int % INT_RANGE_STEP (src2) == 0) {
4795 if (dest)
4796 gst_value_init_and_copy (dest, src1);
4797 return TRUE;
4798 }
4799
4800 return FALSE;
4801 }
4802
4803 static gboolean
gst_value_intersect_int_range_int_range(GValue * dest,const GValue * src1,const GValue * src2)4804 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
4805 const GValue * src2)
4806 {
4807 gint min;
4808 gint max;
4809 gint step;
4810
4811 step =
4812 INT_RANGE_STEP (src1) /
4813 gst_util_greatest_common_divisor (INT_RANGE_STEP (src1),
4814 INT_RANGE_STEP (src2));
4815 if (G_MAXINT32 / INT_RANGE_STEP (src2) < step)
4816 return FALSE;
4817 step *= INT_RANGE_STEP (src2);
4818
4819 min =
4820 MAX (INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1),
4821 INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
4822 min = (min + step - 1) / step * step;
4823 max =
4824 MIN (INT_RANGE_MAX (src1) * INT_RANGE_STEP (src1),
4825 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
4826 max = max / step * step;
4827
4828 if (min < max) {
4829 if (dest) {
4830 g_value_init (dest, GST_TYPE_INT_RANGE);
4831 gst_value_set_int_range_step (dest, min, max, step);
4832 }
4833 return TRUE;
4834 }
4835 if (min == max) {
4836 if (dest) {
4837 g_value_init (dest, G_TYPE_INT);
4838 g_value_set_int (dest, min);
4839 }
4840 return TRUE;
4841 }
4842
4843 return FALSE;
4844 }
4845
4846 #define INT64_RANGE_MIN_VAL(v) (INT64_RANGE_MIN (v) * INT64_RANGE_STEP (v))
4847 #define INT64_RANGE_MAX_VAL(v) (INT64_RANGE_MAX (v) * INT64_RANGE_STEP (v))
4848
4849 static gboolean
gst_value_intersect_int64_int64_range(GValue * dest,const GValue * src1,const GValue * src2)4850 gst_value_intersect_int64_int64_range (GValue * dest, const GValue * src1,
4851 const GValue * src2)
4852 {
4853 if (INT64_RANGE_MIN_VAL (src2) <= src1->data[0].v_int64 &&
4854 INT64_RANGE_MAX_VAL (src2) >= src1->data[0].v_int64 &&
4855 src1->data[0].v_int64 % INT64_RANGE_STEP (src2) == 0) {
4856 if (dest)
4857 gst_value_init_and_copy (dest, src1);
4858 return TRUE;
4859 }
4860
4861 return FALSE;
4862 }
4863
4864 static gboolean
gst_value_intersect_int64_range_int64_range(GValue * dest,const GValue * src1,const GValue * src2)4865 gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
4866 const GValue * src2)
4867 {
4868 gint64 min;
4869 gint64 max;
4870 gint64 step;
4871
4872 step =
4873 INT64_RANGE_STEP (src1) /
4874 gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (src1),
4875 INT64_RANGE_STEP (src2));
4876 if (G_MAXINT64 / INT64_RANGE_STEP (src2) < step)
4877 return FALSE;
4878 step *= INT64_RANGE_STEP (src2);
4879
4880 min =
4881 MAX (INT64_RANGE_MIN (src1) * INT64_RANGE_STEP (src1),
4882 INT64_RANGE_MIN (src2) * INT64_RANGE_STEP (src2));
4883 min = (min + step - 1) / step * step;
4884 max =
4885 MIN (INT64_RANGE_MAX (src1) * INT64_RANGE_STEP (src1),
4886 INT64_RANGE_MAX (src2) * INT64_RANGE_STEP (src2));
4887 max = max / step * step;
4888
4889 if (min < max) {
4890 if (dest) {
4891 g_value_init (dest, GST_TYPE_INT64_RANGE);
4892 gst_value_set_int64_range_step (dest, min, max, step);
4893 }
4894 return TRUE;
4895 }
4896 if (min == max) {
4897 if (dest) {
4898 g_value_init (dest, G_TYPE_INT64);
4899 g_value_set_int64 (dest, min);
4900 }
4901 return TRUE;
4902 }
4903
4904 return FALSE;
4905 }
4906
4907 static gboolean
gst_value_intersect_double_double_range(GValue * dest,const GValue * src1,const GValue * src2)4908 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
4909 const GValue * src2)
4910 {
4911 if (src2->data[0].v_double <= src1->data[0].v_double &&
4912 src2->data[1].v_double >= src1->data[0].v_double) {
4913 if (dest)
4914 gst_value_init_and_copy (dest, src1);
4915 return TRUE;
4916 }
4917
4918 return FALSE;
4919 }
4920
4921 static gboolean
gst_value_intersect_double_range_double_range(GValue * dest,const GValue * src1,const GValue * src2)4922 gst_value_intersect_double_range_double_range (GValue * dest,
4923 const GValue * src1, const GValue * src2)
4924 {
4925 gdouble min;
4926 gdouble max;
4927
4928 min = MAX (src1->data[0].v_double, src2->data[0].v_double);
4929 max = MIN (src1->data[1].v_double, src2->data[1].v_double);
4930
4931 if (min < max) {
4932 if (dest) {
4933 g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
4934 gst_value_set_double_range (dest, min, max);
4935 }
4936 return TRUE;
4937 }
4938 if (min == max) {
4939 if (dest) {
4940 g_value_init (dest, G_TYPE_DOUBLE);
4941 g_value_set_int (dest, (int) min);
4942 }
4943 return TRUE;
4944 }
4945
4946 return FALSE;
4947 }
4948
4949 static gboolean
gst_value_intersect_list_list(GValue * dest,const GValue * value1,const GValue * value2)4950 gst_value_intersect_list_list (GValue * dest, const GValue * value1,
4951 const GValue * value2)
4952 {
4953 guint8 tmpfield[16]; /* Check up to 128 values */
4954 guint8 *bitfield;
4955 gboolean alloc_bitfield = FALSE;
4956 gboolean res = FALSE;
4957 GValue *tmp;
4958 GType type1, type2;
4959 guint it1, len1, start2, it2, len2, itar;
4960 GstValueList *vlist = NULL;
4961
4962 /* If they don't have the same basic type, all bets are off :) */
4963 if (!gst_value_list_or_array_get_basic_type (value1, &type1) ||
4964 !gst_value_list_or_array_get_basic_type (value2, &type2) ||
4965 type1 != type2)
4966 return FALSE;
4967
4968 len1 = VALUE_LIST_SIZE (value1);
4969 len2 = VALUE_LIST_SIZE (value2);
4970
4971 /* Fast-path with no dest (i.e. only interested in knowing whether
4972 * both lists intersected without wanting the result) */
4973 if (!dest) {
4974 for (it1 = 0; it1 < len1; it1++) {
4975 const GValue *item1 = VALUE_LIST_GET_VALUE (value1, it1);
4976 for (it2 = 0; it2 < len2; it2++) {
4977 const GValue *item2 = VALUE_LIST_GET_VALUE (value2, it2);
4978 if (gst_value_intersect (NULL, item1, item2)) {
4979 res = TRUE;
4980 goto beach;
4981 }
4982 }
4983 }
4984 goto beach;
4985 }
4986 #define is_visited(idx) (bitfield[(idx) >> 3] & (1 << ((idx) & 0x7)))
4987 #define mark_visited(idx) (bitfield[(idx) >> 3] |= (1 << ((idx) & 0x7)))
4988
4989 /* Bitfield to avoid double-visiting */
4990 if (G_UNLIKELY (len2 > 128)) {
4991 alloc_bitfield = TRUE;
4992 bitfield = g_malloc0 ((len2 / 8) + 1);
4993 GST_CAT_LOG (GST_CAT_PERFORMANCE,
4994 "Allocation for GstValueList with more than 128 members");
4995 } else {
4996 bitfield = &tmpfield[0];
4997 memset (bitfield, 0, 16);
4998 }
4999
5000
5001 /* When doing list<->list intersections, there is a greater
5002 * probability of ending up with a list than with a single value.
5003 * Furthermore, the biggest that list can be will the smallest list
5004 * (i.e. intersects fully).
5005 * Therefore we pre-allocate a GstValueList of that size. */
5006 vlist = _gst_value_list_new (MIN (len1, len2));
5007
5008 itar = 0;
5009 tmp = &vlist->fields[0];
5010 start2 = 0;
5011
5012 for (it1 = 0; it1 < len1; it1++) {
5013 const GValue *item1 = VALUE_LIST_GET_VALUE (value1, it1);
5014 for (it2 = start2; it2 < len2; it2++) {
5015 const GValue *item2;
5016 if (is_visited (it2))
5017 continue;
5018 item2 = VALUE_LIST_GET_VALUE (value2, it2);
5019
5020 if (gst_value_intersect (tmp, item1, item2)) {
5021 res = TRUE;
5022 mark_visited (it2);
5023 /* Increment our inner-loop starting point */
5024 if (it2 == start2)
5025 start2++;
5026
5027 /* Move our collection value */
5028 itar += 1;
5029 tmp = &vlist->fields[itar];
5030 vlist->len += 1;
5031
5032 /* We can stop iterating the second part now that we've matched */
5033 break;
5034 }
5035 }
5036 }
5037
5038 #undef is_visited
5039 #undef mark_visited
5040
5041 if (res) {
5042 /* If we end up with a single value in the list, just use that
5043 * value. Else use the list */
5044 if (vlist->len == 1) {
5045 gst_value_move (dest, &vlist->fields[0]);
5046 g_free (vlist);
5047 } else {
5048 dest->g_type = GST_TYPE_LIST;
5049 dest->data[0].v_pointer = vlist;
5050 }
5051 } else {
5052 g_free (vlist);
5053 }
5054
5055 beach:
5056 if (alloc_bitfield)
5057 g_free (bitfield);
5058 return res;
5059 }
5060
5061 static gboolean
gst_value_intersect_list(GValue * dest,const GValue * value1,const GValue * value2)5062 gst_value_intersect_list (GValue * dest, const GValue * value1,
5063 const GValue * value2)
5064 {
5065 guint i, size;
5066 GValue intersection = { 0, };
5067 gboolean ret = FALSE;
5068
5069 /* Use optimized list-list intersection */
5070 if (G_VALUE_TYPE (value2) == GST_TYPE_LIST) {
5071 return gst_value_intersect_list_list (dest, value1, value2);
5072 }
5073
5074 size = VALUE_LIST_SIZE (value1);
5075 for (i = 0; i < size; i++) {
5076 const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
5077
5078 /* quicker version when we don't need the resulting set */
5079 if (!dest) {
5080 if (gst_value_intersect (NULL, cur, value2)) {
5081 ret = TRUE;
5082 break;
5083 }
5084 continue;
5085 }
5086
5087 if (gst_value_intersect (&intersection, cur, value2)) {
5088 /* append value */
5089 if (!ret) {
5090 gst_value_move (dest, &intersection);
5091 ret = TRUE;
5092 } else if (GST_VALUE_HOLDS_LIST (dest)) {
5093 _gst_value_list_append_and_take_value (dest, &intersection);
5094 } else {
5095 GValue temp;
5096
5097 gst_value_move (&temp, dest);
5098 gst_value_list_merge (dest, &temp, &intersection);
5099 g_value_unset (&temp);
5100 g_value_unset (&intersection);
5101 }
5102 }
5103 }
5104
5105 return ret;
5106 }
5107
5108 static gboolean
gst_value_intersect_array(GValue * dest,const GValue * src1,const GValue * src2)5109 gst_value_intersect_array (GValue * dest, const GValue * src1,
5110 const GValue * src2)
5111 {
5112 guint size;
5113 guint n;
5114 GValue val = { 0 };
5115
5116 /* only works on similar-sized arrays */
5117 size = gst_value_array_get_size (src1);
5118 if (size != gst_value_array_get_size (src2))
5119 return FALSE;
5120
5121 /* quicker value when we don't need the resulting set */
5122 if (!dest) {
5123 for (n = 0; n < size; n++) {
5124 if (!gst_value_intersect (NULL, gst_value_array_get_value (src1, n),
5125 gst_value_array_get_value (src2, n))) {
5126 return FALSE;
5127 }
5128 }
5129 return TRUE;
5130 }
5131
5132 g_value_init (dest, GST_TYPE_ARRAY);
5133
5134 for (n = 0; n < size; n++) {
5135 if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
5136 gst_value_array_get_value (src2, n))) {
5137 g_value_unset (dest);
5138 return FALSE;
5139 }
5140 _gst_value_array_append_and_take_value (dest, &val);
5141 }
5142
5143 return TRUE;
5144 }
5145
5146 static gboolean
gst_value_intersect_fraction_fraction_range(GValue * dest,const GValue * src1,const GValue * src2)5147 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
5148 const GValue * src2)
5149 {
5150 gint res1, res2;
5151 GValue *vals;
5152
5153 vals = src2->data[0].v_pointer;
5154
5155 if (vals == NULL)
5156 return FALSE;
5157
5158 res1 = gst_value_compare_fraction (&vals[0], src1);
5159 res2 = gst_value_compare_fraction (&vals[1], src1);
5160
5161 if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
5162 (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
5163 if (dest)
5164 gst_value_init_and_copy (dest, src1);
5165 return TRUE;
5166 }
5167 return FALSE;
5168 }
5169
5170 static gboolean
gst_value_intersect_fraction_range_fraction_range(GValue * dest,const GValue * src1,const GValue * src2)5171 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
5172 const GValue * src1, const GValue * src2)
5173 {
5174 GValue *min;
5175 GValue *max;
5176 gint res;
5177 GValue *vals1, *vals2;
5178
5179 vals1 = src1->data[0].v_pointer;
5180 vals2 = src2->data[0].v_pointer;
5181 g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
5182
5183 /* min = MAX (src1.start, src2.start) */
5184 res = gst_value_compare_fraction (&vals1[0], &vals2[0]);
5185 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
5186 if (res == GST_VALUE_LESS_THAN)
5187 min = &vals2[0]; /* Take the max of the 2 */
5188 else
5189 min = &vals1[0];
5190
5191 /* max = MIN (src1.end, src2.end) */
5192 res = gst_value_compare_fraction (&vals1[1], &vals2[1]);
5193 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
5194 if (res == GST_VALUE_GREATER_THAN)
5195 max = &vals2[1]; /* Take the min of the 2 */
5196 else
5197 max = &vals1[1];
5198
5199 res = gst_value_compare_fraction (min, max);
5200 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
5201 if (res == GST_VALUE_LESS_THAN) {
5202 if (dest) {
5203 g_value_init (dest, GST_TYPE_FRACTION_RANGE);
5204 vals1 = dest->data[0].v_pointer;
5205 g_value_copy (min, &vals1[0]);
5206 g_value_copy (max, &vals1[1]);
5207 }
5208 return TRUE;
5209 }
5210 if (res == GST_VALUE_EQUAL) {
5211 if (dest)
5212 gst_value_init_and_copy (dest, min);
5213 return TRUE;
5214 }
5215
5216 return FALSE;
5217 }
5218
5219 /* Two flagsets intersect if the masked bits in both
5220 * flagsets are exactly equal */
5221 static gboolean
gst_value_intersect_flagset_flagset(GValue * dest,const GValue * src1,const GValue * src2)5222 gst_value_intersect_flagset_flagset (GValue * dest,
5223 const GValue * src1, const GValue * src2)
5224 {
5225 guint f1, f2;
5226 guint m1, m2;
5227 GType type1, type2, flagset_type;
5228
5229 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (src1), FALSE);
5230 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (src2), FALSE);
5231
5232 f1 = src1->data[0].v_uint;
5233 f2 = src2->data[0].v_uint;
5234
5235 m1 = src1->data[1].v_uint;
5236 m2 = src2->data[1].v_uint;
5237
5238 /* Don't intersect if masked bits disagree */
5239 if ((f1 & (m1 & m2)) != (f2 & (m1 & m2)))
5240 return FALSE;
5241
5242 /* Allow intersection with the generic FlagSet type, on one
5243 * side, but not 2 different subtypes - that makes no sense */
5244 type1 = G_VALUE_TYPE (src1);
5245 type2 = G_VALUE_TYPE (src2);
5246 flagset_type = GST_TYPE_FLAG_SET;
5247
5248 if (type1 != type2 && type1 != flagset_type && type2 != flagset_type)
5249 return FALSE;
5250
5251 if (dest) {
5252 GType dest_type;
5253
5254 /* Prefer an output type that matches a sub-type,
5255 * rather than the generic type */
5256 if (type1 != flagset_type)
5257 dest_type = type1;
5258 else
5259 dest_type = type2;
5260
5261 g_value_init (dest, dest_type);
5262
5263 /* The compatible set is all the bits from src1 that it
5264 * cares about and all the bits from src2 that it cares
5265 * about. */
5266 dest->data[0].v_uint = (f1 & m1) | (f2 & m2);
5267 dest->data[1].v_uint = m1 | m2;
5268 }
5269
5270 return TRUE;
5271 }
5272
5273 static gboolean
gst_value_intersect_structure_structure(GValue * dest,const GValue * src1,const GValue * src2)5274 gst_value_intersect_structure_structure (GValue * dest,
5275 const GValue * src1, const GValue * src2)
5276 {
5277 const GstStructure *s1, *s2;
5278 GstStructure *d1;
5279
5280 s1 = gst_value_get_structure (src1);
5281 s2 = gst_value_get_structure (src2);
5282
5283 d1 = gst_structure_intersect (s1, s2);
5284 if (!d1)
5285 return FALSE;
5286
5287 if (dest) {
5288 g_value_init (dest, GST_TYPE_STRUCTURE);
5289 gst_value_set_structure (dest, d1);
5290 }
5291
5292 gst_structure_free (d1);
5293 return TRUE;
5294 }
5295
5296 /***************
5297 * subtraction *
5298 ***************/
5299
5300 static gboolean
gst_value_subtract_int_int_range(GValue * dest,const GValue * minuend,const GValue * subtrahend)5301 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
5302 const GValue * subtrahend)
5303 {
5304 gint min = gst_value_get_int_range_min (subtrahend);
5305 gint max = gst_value_get_int_range_max (subtrahend);
5306 gint step = gst_value_get_int_range_step (subtrahend);
5307 gint val = g_value_get_int (minuend);
5308
5309 if (step == 0)
5310 return FALSE;
5311
5312 /* subtracting a range from an int only works if the int is not in the
5313 * range */
5314 if (val < min || val > max || val % step) {
5315 /* and the result is the int */
5316 if (dest)
5317 gst_value_init_and_copy (dest, minuend);
5318 return TRUE;
5319 }
5320 return FALSE;
5321 }
5322
5323 /* creates a new int range based on input values.
5324 */
5325 static gboolean
gst_value_create_new_range(GValue * dest,gint min1,gint max1,gint min2,gint max2,gint step)5326 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
5327 gint max2, gint step)
5328 {
5329 GValue v1 = { 0, };
5330 GValue v2 = { 0, };
5331 GValue *pv1, *pv2; /* yeah, hungarian! */
5332
5333 g_return_val_if_fail (step > 0, FALSE);
5334 g_return_val_if_fail (min1 % step == 0, FALSE);
5335 g_return_val_if_fail (max1 % step == 0, FALSE);
5336 g_return_val_if_fail (min2 % step == 0, FALSE);
5337 g_return_val_if_fail (max2 % step == 0, FALSE);
5338
5339 if (min1 <= max1 && min2 <= max2) {
5340 pv1 = &v1;
5341 pv2 = &v2;
5342 } else if (min1 <= max1) {
5343 pv1 = dest;
5344 pv2 = NULL;
5345 } else if (min2 <= max2) {
5346 pv1 = NULL;
5347 pv2 = dest;
5348 } else {
5349 return FALSE;
5350 }
5351
5352 if (!dest)
5353 return TRUE;
5354
5355 if (min1 < max1) {
5356 g_value_init (pv1, GST_TYPE_INT_RANGE);
5357 gst_value_set_int_range_step (pv1, min1, max1, step);
5358 } else if (min1 == max1) {
5359 g_value_init (pv1, G_TYPE_INT);
5360 g_value_set_int (pv1, min1);
5361 }
5362 if (min2 < max2) {
5363 g_value_init (pv2, GST_TYPE_INT_RANGE);
5364 gst_value_set_int_range_step (pv2, min2, max2, step);
5365 } else if (min2 == max2) {
5366 g_value_init (pv2, G_TYPE_INT);
5367 g_value_set_int (pv2, min2);
5368 }
5369
5370 if (min1 <= max1 && min2 <= max2) {
5371 gst_value_list_concat_and_take_values (dest, pv1, pv2);
5372 }
5373 return TRUE;
5374 }
5375
5376 static gboolean
gst_value_subtract_int_range_int(GValue * dest,const GValue * minuend,const GValue * subtrahend)5377 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
5378 const GValue * subtrahend)
5379 {
5380 gint min = gst_value_get_int_range_min (minuend);
5381 gint max = gst_value_get_int_range_max (minuend);
5382 gint step = gst_value_get_int_range_step (minuend);
5383 gint val = g_value_get_int (subtrahend);
5384
5385 g_return_val_if_fail (min < max, FALSE);
5386
5387 if (step == 0)
5388 return FALSE;
5389
5390 /* value is outside of the range, return range unchanged */
5391 if (val < min || val > max || val % step) {
5392 if (dest)
5393 gst_value_init_and_copy (dest, minuend);
5394 return TRUE;
5395 } else {
5396 /* max must be MAXINT too as val <= max */
5397 if (val >= G_MAXINT - step + 1) {
5398 max -= step;
5399 val -= step;
5400 }
5401 /* min must be MININT too as val >= max */
5402 if (val <= G_MININT + step - 1) {
5403 min += step;
5404 val += step;
5405 }
5406 if (dest)
5407 gst_value_create_new_range (dest, min, val - step, val + step, max, step);
5408 }
5409 return TRUE;
5410 }
5411
5412 static gboolean
gst_value_subtract_int_range_int_range(GValue * dest,const GValue * minuend,const GValue * subtrahend)5413 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
5414 const GValue * subtrahend)
5415 {
5416 gint min1 = gst_value_get_int_range_min (minuend);
5417 gint max1 = gst_value_get_int_range_max (minuend);
5418 gint step1 = gst_value_get_int_range_step (minuend);
5419 gint min2 = gst_value_get_int_range_min (subtrahend);
5420 gint max2 = gst_value_get_int_range_max (subtrahend);
5421 gint step2 = gst_value_get_int_range_step (subtrahend);
5422 gint step;
5423
5424 if (step1 != step2) {
5425 /* ENOIMPL */
5426 g_assert (FALSE);
5427 return FALSE;
5428 }
5429 step = step1;
5430
5431 if (step == 0)
5432 return FALSE;
5433
5434 if (max2 >= max1 && min2 <= min1) {
5435 return FALSE;
5436 } else if (max2 >= max1) {
5437 return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
5438 step, 0, step);
5439 } else if (min2 <= min1) {
5440 return gst_value_create_new_range (dest, MAX (max2 + step, min1), max1,
5441 step, 0, step);
5442 } else {
5443 return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
5444 MAX (max2 + step, min1), max1, step);
5445 }
5446 }
5447
5448 static gboolean
gst_value_subtract_int64_int64_range(GValue * dest,const GValue * minuend,const GValue * subtrahend)5449 gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
5450 const GValue * subtrahend)
5451 {
5452 gint64 min = gst_value_get_int64_range_min (subtrahend);
5453 gint64 max = gst_value_get_int64_range_max (subtrahend);
5454 gint64 step = gst_value_get_int64_range_step (subtrahend);
5455 gint64 val = g_value_get_int64 (minuend);
5456
5457 if (step == 0)
5458 return FALSE;
5459 /* subtracting a range from an int64 only works if the int64 is not in the
5460 * range */
5461 if (val < min || val > max || val % step) {
5462 /* and the result is the int64 */
5463 if (dest)
5464 gst_value_init_and_copy (dest, minuend);
5465 return TRUE;
5466 }
5467 return FALSE;
5468 }
5469
5470 /* creates a new int64 range based on input values.
5471 */
5472 static gboolean
gst_value_create_new_int64_range(GValue * dest,gint64 min1,gint64 max1,gint64 min2,gint64 max2,gint64 step)5473 gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
5474 gint64 min2, gint64 max2, gint64 step)
5475 {
5476 GValue v1 = { 0, };
5477 GValue v2 = { 0, };
5478 GValue *pv1, *pv2; /* yeah, hungarian! */
5479
5480 g_return_val_if_fail (step > 0, FALSE);
5481 g_return_val_if_fail (min1 % step == 0, FALSE);
5482 g_return_val_if_fail (max1 % step == 0, FALSE);
5483 g_return_val_if_fail (min2 % step == 0, FALSE);
5484 g_return_val_if_fail (max2 % step == 0, FALSE);
5485
5486 if (min1 <= max1 && min2 <= max2) {
5487 pv1 = &v1;
5488 pv2 = &v2;
5489 } else if (min1 <= max1) {
5490 pv1 = dest;
5491 pv2 = NULL;
5492 } else if (min2 <= max2) {
5493 pv1 = NULL;
5494 pv2 = dest;
5495 } else {
5496 return FALSE;
5497 }
5498
5499 if (!dest)
5500 return TRUE;
5501
5502 if (min1 < max1) {
5503 g_value_init (pv1, GST_TYPE_INT64_RANGE);
5504 gst_value_set_int64_range_step (pv1, min1, max1, step);
5505 } else if (min1 == max1) {
5506 g_value_init (pv1, G_TYPE_INT64);
5507 g_value_set_int64 (pv1, min1);
5508 }
5509 if (min2 < max2) {
5510 g_value_init (pv2, GST_TYPE_INT64_RANGE);
5511 gst_value_set_int64_range_step (pv2, min2, max2, step);
5512 } else if (min2 == max2) {
5513 g_value_init (pv2, G_TYPE_INT64);
5514 g_value_set_int64 (pv2, min2);
5515 }
5516
5517 if (min1 <= max1 && min2 <= max2) {
5518 gst_value_list_concat_and_take_values (dest, pv1, pv2);
5519 }
5520 return TRUE;
5521 }
5522
5523 static gboolean
gst_value_subtract_int64_range_int64(GValue * dest,const GValue * minuend,const GValue * subtrahend)5524 gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
5525 const GValue * subtrahend)
5526 {
5527 gint64 min = gst_value_get_int64_range_min (minuend);
5528 gint64 max = gst_value_get_int64_range_max (minuend);
5529 gint64 step = gst_value_get_int64_range_step (minuend);
5530 gint64 val = g_value_get_int64 (subtrahend);
5531
5532 g_return_val_if_fail (min < max, FALSE);
5533
5534 if (step == 0)
5535 return FALSE;
5536
5537 /* value is outside of the range, return range unchanged */
5538 if (val < min || val > max || val % step) {
5539 if (dest)
5540 gst_value_init_and_copy (dest, minuend);
5541 return TRUE;
5542 } else {
5543 /* max must be MAXINT64 too as val <= max */
5544 if (val >= G_MAXINT64 - step + 1) {
5545 max -= step;
5546 val -= step;
5547 }
5548 /* min must be MININT64 too as val >= max */
5549 if (val <= G_MININT64 + step - 1) {
5550 min += step;
5551 val += step;
5552 }
5553 if (dest)
5554 gst_value_create_new_int64_range (dest, min, val - step, val + step, max,
5555 step);
5556 }
5557 return TRUE;
5558 }
5559
5560 static gboolean
gst_value_subtract_int64_range_int64_range(GValue * dest,const GValue * minuend,const GValue * subtrahend)5561 gst_value_subtract_int64_range_int64_range (GValue * dest,
5562 const GValue * minuend, const GValue * subtrahend)
5563 {
5564 gint64 min1 = gst_value_get_int64_range_min (minuend);
5565 gint64 max1 = gst_value_get_int64_range_max (minuend);
5566 gint64 step1 = gst_value_get_int64_range_step (minuend);
5567 gint64 min2 = gst_value_get_int64_range_min (subtrahend);
5568 gint64 max2 = gst_value_get_int64_range_max (subtrahend);
5569 gint64 step2 = gst_value_get_int64_range_step (subtrahend);
5570 gint64 step;
5571
5572 if (step1 != step2) {
5573 /* ENOIMPL */
5574 g_assert (FALSE);
5575 return FALSE;
5576 }
5577
5578 if (step1 == 0)
5579 return FALSE;
5580
5581 step = step1;
5582
5583 if (max2 >= max1 && min2 <= min1) {
5584 return FALSE;
5585 } else if (max2 >= max1) {
5586 return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
5587 max1), step, 0, step);
5588 } else if (min2 <= min1) {
5589 return gst_value_create_new_int64_range (dest, MAX (max2 + step, min1),
5590 max1, step, 0, step);
5591 } else {
5592 return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
5593 max1), MAX (max2 + step, min1), max1, step);
5594 }
5595 }
5596
5597 static gboolean
gst_value_subtract_double_double_range(GValue * dest,const GValue * minuend,const GValue * subtrahend)5598 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
5599 const GValue * subtrahend)
5600 {
5601 gdouble min = gst_value_get_double_range_min (subtrahend);
5602 gdouble max = gst_value_get_double_range_max (subtrahend);
5603 gdouble val = g_value_get_double (minuend);
5604
5605 if (val < min || val > max) {
5606 if (dest)
5607 gst_value_init_and_copy (dest, minuend);
5608 return TRUE;
5609 }
5610 return FALSE;
5611 }
5612
5613 static gboolean
gst_value_subtract_double_range_double(GValue * dest,const GValue * minuend,const GValue * subtrahend)5614 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
5615 const GValue * subtrahend)
5616 {
5617 /* since we don't have open ranges, we cannot create a hole in
5618 * a double range. We return the original range */
5619 if (dest)
5620 gst_value_init_and_copy (dest, minuend);
5621 return TRUE;
5622 }
5623
5624 static gboolean
gst_value_subtract_double_range_double_range(GValue * dest,const GValue * minuend,const GValue * subtrahend)5625 gst_value_subtract_double_range_double_range (GValue * dest,
5626 const GValue * minuend, const GValue * subtrahend)
5627 {
5628 /* since we don't have open ranges, we have to approximate */
5629 /* done like with ints */
5630 gdouble min1 = gst_value_get_double_range_min (minuend);
5631 gdouble max2 = gst_value_get_double_range_max (minuend);
5632 gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
5633 gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
5634 GValue v1 = { 0, };
5635 GValue v2 = { 0, };
5636 GValue *pv1, *pv2; /* yeah, hungarian! */
5637
5638 if (min1 < max1 && min2 < max2) {
5639 pv1 = &v1;
5640 pv2 = &v2;
5641 } else if (min1 < max1) {
5642 pv1 = dest;
5643 pv2 = NULL;
5644 } else if (min2 < max2) {
5645 pv1 = NULL;
5646 pv2 = dest;
5647 } else {
5648 return FALSE;
5649 }
5650
5651 if (!dest)
5652 return TRUE;
5653
5654 if (min1 < max1) {
5655 g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
5656 gst_value_set_double_range (pv1, min1, max1);
5657 }
5658 if (min2 < max2) {
5659 g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
5660 gst_value_set_double_range (pv2, min2, max2);
5661 }
5662
5663 if (min1 < max1 && min2 < max2) {
5664 gst_value_list_concat_and_take_values (dest, pv1, pv2);
5665 }
5666 return TRUE;
5667 }
5668
5669 static gboolean
gst_value_subtract_from_list(GValue * dest,const GValue * minuend,const GValue * subtrahend)5670 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
5671 const GValue * subtrahend)
5672 {
5673 guint i, size;
5674 GValue subtraction = { 0, };
5675 gboolean ret = FALSE;
5676
5677 size = VALUE_LIST_SIZE (minuend);
5678 for (i = 0; i < size; i++) {
5679 const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
5680
5681 /* quicker version when we can discard the result */
5682 if (!dest) {
5683 if (gst_value_subtract (NULL, cur, subtrahend)) {
5684 ret = TRUE;
5685 break;
5686 }
5687 continue;
5688 }
5689
5690 if (gst_value_subtract (&subtraction, cur, subtrahend)) {
5691 if (!ret) {
5692 gst_value_move (dest, &subtraction);
5693 ret = TRUE;
5694 } else if (G_VALUE_TYPE (dest) == GST_TYPE_LIST
5695 && G_VALUE_TYPE (&subtraction) != GST_TYPE_LIST) {
5696 _gst_value_list_append_and_take_value (dest, &subtraction);
5697 } else {
5698 GValue temp;
5699
5700 gst_value_move (&temp, dest);
5701 gst_value_list_concat_and_take_values (dest, &temp, &subtraction);
5702 }
5703 }
5704 }
5705 return ret;
5706 }
5707
5708 static gboolean
gst_value_subtract_list(GValue * dest,const GValue * minuend,const GValue * subtrahend)5709 gst_value_subtract_list (GValue * dest, const GValue * minuend,
5710 const GValue * subtrahend)
5711 {
5712 guint i, size;
5713 GValue data[2] = { {0,}, {0,} };
5714 GValue *subtraction = &data[0], *result = &data[1];
5715
5716 gst_value_init_and_copy (result, minuend);
5717 size = VALUE_LIST_SIZE (subtrahend);
5718 for (i = 0; i < size; i++) {
5719 const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
5720
5721 if (gst_value_subtract (subtraction, result, cur)) {
5722 GValue *temp = result;
5723
5724 result = subtraction;
5725 subtraction = temp;
5726 g_value_unset (subtraction);
5727 } else {
5728 g_value_unset (result);
5729 return FALSE;
5730 }
5731 }
5732 if (dest) {
5733 gst_value_move (dest, result);
5734 } else {
5735 g_value_unset (result);
5736 }
5737 return TRUE;
5738 }
5739
5740 static gboolean
gst_value_subtract_fraction_fraction_range(GValue * dest,const GValue * minuend,const GValue * subtrahend)5741 gst_value_subtract_fraction_fraction_range (GValue * dest,
5742 const GValue * minuend, const GValue * subtrahend)
5743 {
5744 const GValue *min = gst_value_get_fraction_range_min (subtrahend);
5745 const GValue *max = gst_value_get_fraction_range_max (subtrahend);
5746
5747 /* subtracting a range from an fraction only works if the fraction
5748 * is not in the range */
5749 if (gst_value_compare_fraction (minuend, min) == GST_VALUE_LESS_THAN ||
5750 gst_value_compare_fraction (minuend, max) == GST_VALUE_GREATER_THAN) {
5751 /* and the result is the value */
5752 if (dest)
5753 gst_value_init_and_copy (dest, minuend);
5754 return TRUE;
5755 }
5756
5757 return FALSE;
5758 }
5759
5760 static gboolean
gst_value_subtract_fraction_range_fraction(GValue * dest,const GValue * minuend,const GValue * subtrahend)5761 gst_value_subtract_fraction_range_fraction (GValue * dest,
5762 const GValue * minuend, const GValue * subtrahend)
5763 {
5764 /* since we don't have open ranges, we cannot create a hole in
5765 * a range. We return the original range */
5766 if (dest)
5767 gst_value_init_and_copy (dest, minuend);
5768 return TRUE;
5769 }
5770
5771 static gboolean
gst_value_subtract_fraction_range_fraction_range(GValue * dest,const GValue * minuend,const GValue * subtrahend)5772 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
5773 const GValue * minuend, const GValue * subtrahend)
5774 {
5775 /* since we don't have open ranges, we have to approximate */
5776 /* done like with ints and doubles. Creates a list of 2 fraction ranges */
5777 const GValue *min1 = gst_value_get_fraction_range_min (minuend);
5778 const GValue *max2 = gst_value_get_fraction_range_max (minuend);
5779 const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
5780 const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
5781 gint cmp1, cmp2;
5782 GValue v1 = { 0, };
5783 GValue v2 = { 0, };
5784 GValue *pv1, *pv2; /* yeah, hungarian! */
5785
5786 g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
5787 g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
5788
5789 cmp1 = gst_value_compare_fraction (max2, max1);
5790 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
5791 if (cmp1 == GST_VALUE_LESS_THAN)
5792 max1 = max2;
5793 cmp1 = gst_value_compare_fraction (min1, min2);
5794 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
5795 if (cmp1 == GST_VALUE_GREATER_THAN)
5796 min2 = min1;
5797
5798 cmp1 = gst_value_compare_fraction (min1, max1);
5799 cmp2 = gst_value_compare_fraction (min2, max2);
5800
5801 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
5802 pv1 = &v1;
5803 pv2 = &v2;
5804 } else if (cmp1 == GST_VALUE_LESS_THAN) {
5805 pv1 = dest;
5806 pv2 = NULL;
5807 } else if (cmp2 == GST_VALUE_LESS_THAN) {
5808 pv1 = NULL;
5809 pv2 = dest;
5810 } else {
5811 return FALSE;
5812 }
5813
5814 if (!dest)
5815 return TRUE;
5816
5817 if (cmp1 == GST_VALUE_LESS_THAN) {
5818 g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
5819 gst_value_set_fraction_range (pv1, min1, max1);
5820 }
5821 if (cmp2 == GST_VALUE_LESS_THAN) {
5822 g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
5823 gst_value_set_fraction_range (pv2, min2, max2);
5824 }
5825
5826 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
5827 gst_value_list_concat_and_take_values (dest, pv1, pv2);
5828 }
5829 return TRUE;
5830 }
5831
5832 /**************
5833 * comparison *
5834 **************/
5835
5836 /*
5837 * gst_value_get_compare_func:
5838 * @value1: a value to get the compare function for
5839 *
5840 * Determines the compare function to be used with values of the same type as
5841 * @value1. The function can be given to gst_value_compare_with_func().
5842 *
5843 * Returns: A #GstValueCompareFunc value
5844 */
5845 static GstValueCompareFunc
gst_value_get_compare_func(const GValue * value1)5846 gst_value_get_compare_func (const GValue * value1)
5847 {
5848 GstValueTable *table, *best = NULL;
5849 guint i;
5850 GType type1;
5851
5852 type1 = G_VALUE_TYPE (value1);
5853
5854 /* this is a fast check */
5855 best = gst_value_hash_lookup_type (type1);
5856
5857 /* slower checks */
5858 if (G_UNLIKELY (!best || !best->compare)) {
5859 guint len = gst_value_table->len;
5860
5861 best = NULL;
5862 for (i = 0; i < len; i++) {
5863 table = &g_array_index (gst_value_table, GstValueTable, i);
5864 if (table->compare && g_type_is_a (type1, table->type)) {
5865 if (!best || g_type_is_a (table->type, best->type))
5866 best = table;
5867 }
5868 }
5869 }
5870 if (G_LIKELY (best))
5871 return best->compare;
5872
5873 return NULL;
5874 }
5875
5876 static inline gboolean
gst_value_can_compare_unchecked(const GValue * value1,const GValue * value2)5877 gst_value_can_compare_unchecked (const GValue * value1, const GValue * value2)
5878 {
5879 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
5880 return FALSE;
5881
5882 return gst_value_get_compare_func (value1) != NULL;
5883 }
5884
5885 /**
5886 * gst_value_can_compare:
5887 * @value1: a value to compare
5888 * @value2: another value to compare
5889 *
5890 * Determines if @value1 and @value2 can be compared.
5891 *
5892 * Returns: %TRUE if the values can be compared
5893 */
5894 gboolean
gst_value_can_compare(const GValue * value1,const GValue * value2)5895 gst_value_can_compare (const GValue * value1, const GValue * value2)
5896 {
5897 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
5898 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
5899
5900 return gst_value_can_compare_unchecked (value1, value2);
5901 }
5902
5903 static gboolean
gst_value_list_equals_range(const GValue * list,const GValue * value)5904 gst_value_list_equals_range (const GValue * list, const GValue * value)
5905 {
5906 const GValue *first;
5907 guint list_size, n;
5908
5909 /* TODO: compare against an empty list ? No type though... */
5910 list_size = VALUE_LIST_SIZE (list);
5911 if (list_size == 0)
5912 return FALSE;
5913
5914 /* compare the basic types - they have to match */
5915 first = VALUE_LIST_GET_VALUE (list, 0);
5916 #define CHECK_TYPES(type,prefix) \
5917 ((first) && G_VALUE_TYPE(first) == prefix##_TYPE_##type && GST_VALUE_HOLDS_##type##_RANGE (value))
5918 if (CHECK_TYPES (INT, G)) {
5919 const gint rmin = gst_value_get_int_range_min (value);
5920 const gint rmax = gst_value_get_int_range_max (value);
5921 const gint rstep = gst_value_get_int_range_step (value);
5922 if (rstep == 0)
5923 return FALSE;
5924 /* note: this will overflow for min 0 and max INT_MAX, but this
5925 would only be equal to a list of INT_MAX elements, which seems
5926 very unlikely */
5927 if (list_size != rmax / rstep - rmin / rstep + 1)
5928 return FALSE;
5929 for (n = 0; n < list_size; ++n) {
5930 gint v = g_value_get_int (VALUE_LIST_GET_VALUE (list, n));
5931 if (v < rmin || v > rmax || v % rstep) {
5932 return FALSE;
5933 }
5934 }
5935 return TRUE;
5936 } else if (CHECK_TYPES (INT64, G)) {
5937 const gint64 rmin = gst_value_get_int64_range_min (value);
5938 const gint64 rmax = gst_value_get_int64_range_max (value);
5939 const gint64 rstep = gst_value_get_int64_range_step (value);
5940 GST_DEBUG ("List/range of int64s");
5941 if (rstep == 0)
5942 return FALSE;
5943 if (list_size != rmax / rstep - rmin / rstep + 1)
5944 return FALSE;
5945 for (n = 0; n < list_size; ++n) {
5946 gint64 v = g_value_get_int64 (VALUE_LIST_GET_VALUE (list, n));
5947 if (v < rmin || v > rmax || v % rstep)
5948 return FALSE;
5949 }
5950 return TRUE;
5951 }
5952 #undef CHECK_TYPES
5953
5954 /* other combinations don't make sense for equality */
5955 return FALSE;
5956 }
5957
5958 /* "Pure" variant of gst_value_compare which is guaranteed to
5959 * not have list arguments and therefore does basic comparisons
5960 */
5961 static inline gint
_gst_value_compare_nolist(const GValue * value1,const GValue * value2)5962 _gst_value_compare_nolist (const GValue * value1, const GValue * value2)
5963 {
5964 GstValueCompareFunc compare;
5965
5966 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
5967 return GST_VALUE_UNORDERED;
5968
5969 compare = gst_value_get_compare_func (value1);
5970 if (compare) {
5971 return compare (value1, value2);
5972 }
5973
5974 g_critical ("unable to compare values of type %s\n",
5975 g_type_name (G_VALUE_TYPE (value1)));
5976 return GST_VALUE_UNORDERED;
5977 }
5978
5979 /**
5980 * gst_value_compare:
5981 * @value1: a value to compare
5982 * @value2: another value to compare
5983 *
5984 * Compares @value1 and @value2. If @value1 and @value2 cannot be
5985 * compared, the function returns GST_VALUE_UNORDERED. Otherwise,
5986 * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
5987 * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
5988 * If the values are equal, GST_VALUE_EQUAL is returned.
5989 *
5990 * Returns: comparison result
5991 */
5992 gint
gst_value_compare(const GValue * value1,const GValue * value2)5993 gst_value_compare (const GValue * value1, const GValue * value2)
5994 {
5995 gboolean value1_is_list;
5996 gboolean value2_is_list;
5997
5998 g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
5999 g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
6000
6001 value1_is_list = G_VALUE_TYPE (value1) == GST_TYPE_LIST;
6002 value2_is_list = G_VALUE_TYPE (value2) == GST_TYPE_LIST;
6003
6004 /* Special cases: lists and scalar values ("{ 1 }" and "1" are equal),
6005 as well as lists and ranges ("{ 1, 2 }" and "[ 1, 2 ]" are equal) */
6006 if (value1_is_list && !value2_is_list) {
6007 gint i, n, ret;
6008
6009 if (gst_value_list_equals_range (value1, value2)) {
6010 return GST_VALUE_EQUAL;
6011 }
6012
6013 n = gst_value_list_get_size (value1);
6014 if (n == 0)
6015 return GST_VALUE_UNORDERED;
6016
6017 for (i = 0; i < n; i++) {
6018 const GValue *elt;
6019
6020 elt = gst_value_list_get_value (value1, i);
6021 ret = gst_value_compare (elt, value2);
6022 if (ret != GST_VALUE_EQUAL && n == 1)
6023 return ret;
6024 else if (ret != GST_VALUE_EQUAL)
6025 return GST_VALUE_UNORDERED;
6026 }
6027
6028 return GST_VALUE_EQUAL;
6029 } else if (value2_is_list && !value1_is_list) {
6030 gint i, n, ret;
6031
6032 if (gst_value_list_equals_range (value2, value1)) {
6033 return GST_VALUE_EQUAL;
6034 }
6035
6036 n = gst_value_list_get_size (value2);
6037 if (n == 0)
6038 return GST_VALUE_UNORDERED;
6039
6040 for (i = 0; i < n; i++) {
6041 const GValue *elt;
6042
6043 elt = gst_value_list_get_value (value2, i);
6044 ret = gst_value_compare (elt, value1);
6045 if (ret != GST_VALUE_EQUAL && n == 1)
6046 return ret;
6047 else if (ret != GST_VALUE_EQUAL)
6048 return GST_VALUE_UNORDERED;
6049 }
6050
6051 return GST_VALUE_EQUAL;
6052 }
6053
6054 /* And now handle the generic case */
6055 return _gst_value_compare_nolist (value1, value2);
6056 }
6057
6058 /* union */
6059
6060 /**
6061 * gst_value_can_union:
6062 * @value1: a value to union
6063 * @value2: another value to union
6064 *
6065 * Determines if @value1 and @value2 can be non-trivially unioned.
6066 * Any two values can be trivially unioned by adding both of them
6067 * to a GstValueList. However, certain types have the possibility
6068 * to be unioned in a simpler way. For example, an integer range
6069 * and an integer can be unioned if the integer is a subset of the
6070 * integer range. If there is the possibility that two values can
6071 * be unioned, this function returns %TRUE.
6072 *
6073 * Returns: %TRUE if there is a function allowing the two values to
6074 * be unioned.
6075 */
6076 gboolean
gst_value_can_union(const GValue * value1,const GValue * value2)6077 gst_value_can_union (const GValue * value1, const GValue * value2)
6078 {
6079 GstValueUnionInfo *union_info;
6080 guint i, len;
6081
6082 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
6083 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
6084
6085 len = gst_value_union_funcs->len;
6086
6087 for (i = 0; i < len; i++) {
6088 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
6089 if (union_info->type1 == G_VALUE_TYPE (value1) &&
6090 union_info->type2 == G_VALUE_TYPE (value2))
6091 return TRUE;
6092 if (union_info->type1 == G_VALUE_TYPE (value2) &&
6093 union_info->type2 == G_VALUE_TYPE (value1))
6094 return TRUE;
6095 }
6096
6097 return FALSE;
6098 }
6099
6100 /**
6101 * gst_value_union:
6102 * @dest: (out caller-allocates): the destination value
6103 * @value1: a value to union
6104 * @value2: another value to union
6105 *
6106 * Creates a GValue corresponding to the union of @value1 and @value2.
6107 *
6108 * Returns: %TRUE if the union succeeded.
6109 */
6110 gboolean
gst_value_union(GValue * dest,const GValue * value1,const GValue * value2)6111 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
6112 {
6113 const GstValueUnionInfo *union_info;
6114 guint i, len;
6115 GType type1, type2;
6116
6117 g_return_val_if_fail (dest != NULL, FALSE);
6118 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
6119 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
6120 g_return_val_if_fail (gst_value_list_or_array_are_compatible (value1, value2),
6121 FALSE);
6122
6123 len = gst_value_union_funcs->len;
6124 type1 = G_VALUE_TYPE (value1);
6125 type2 = G_VALUE_TYPE (value2);
6126
6127 for (i = 0; i < len; i++) {
6128 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
6129 if (union_info->type1 == type1 && union_info->type2 == type2) {
6130 return union_info->func (dest, value1, value2);
6131 }
6132 if (union_info->type1 == type2 && union_info->type2 == type1) {
6133 return union_info->func (dest, value2, value1);
6134 }
6135 }
6136
6137 gst_value_list_concat (dest, value1, value2);
6138 return TRUE;
6139 }
6140
6141 /* gst_value_register_union_func: (skip)
6142 * @type1: a type to union
6143 * @type2: another type to union
6144 * @func: a function that implements creating a union between the two types
6145 *
6146 * Registers a union function that can create a union between #GValue items
6147 * of the type @type1 and @type2.
6148 *
6149 * Union functions should be registered at startup before any pipelines are
6150 * started, as gst_value_register_union_func() is not thread-safe and cannot
6151 * be used at the same time as gst_value_union() or gst_value_can_union().
6152 */
6153 static void
gst_value_register_union_func(GType type1,GType type2,GstValueUnionFunc func)6154 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
6155 {
6156 GstValueUnionInfo union_info;
6157
6158 union_info.type1 = type1;
6159 union_info.type2 = type2;
6160 union_info.func = func;
6161
6162 g_array_append_val (gst_value_union_funcs, union_info);
6163 }
6164
6165 /* intersection */
6166
6167 /**
6168 * gst_value_can_intersect:
6169 * @value1: a value to intersect
6170 * @value2: another value to intersect
6171 *
6172 * Determines if intersecting two values will produce a valid result.
6173 * Two values will produce a valid intersection if they have the same
6174 * type.
6175 *
6176 * Returns: %TRUE if the values can intersect
6177 */
6178 gboolean
gst_value_can_intersect(const GValue * value1,const GValue * value2)6179 gst_value_can_intersect (const GValue * value1, const GValue * value2)
6180 {
6181 GstValueIntersectInfo *intersect_info;
6182 guint i, len;
6183 GType type1, type2;
6184
6185 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
6186 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
6187
6188 type1 = G_VALUE_TYPE (value1);
6189 type2 = G_VALUE_TYPE (value2);
6190
6191 /* practically all GstValue types have a compare function (_can_compare=TRUE)
6192 * GstStructure and GstCaps have not, but are intersectable */
6193 if (type1 == type2)
6194 return TRUE;
6195
6196 /* special cases */
6197 if (type1 == GST_TYPE_LIST || type2 == GST_TYPE_LIST)
6198 return TRUE;
6199
6200 if (G_UNLIKELY (GST_VALUE_HOLDS_FLAG_SET (value1) &&
6201 GST_VALUE_HOLDS_FLAG_SET (value2))) {
6202 GType flagset_type;
6203
6204 flagset_type = GST_TYPE_FLAG_SET;
6205
6206 /* Allow intersection with the generic FlagSet type, on one
6207 * side, but not 2 different subtypes - that makes no sense */
6208 if (type1 == flagset_type || type2 == flagset_type)
6209 return TRUE;
6210 }
6211
6212 /* check registered intersect functions (only different gtype are checked at
6213 * this point) */
6214 len = gst_value_intersect_funcs->len;
6215 for (i = 0; i < len; i++) {
6216 intersect_info = &g_array_index (gst_value_intersect_funcs,
6217 GstValueIntersectInfo, i);
6218 if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
6219 (intersect_info->type1 == type2 && intersect_info->type2 == type1))
6220 return TRUE;
6221 }
6222
6223 return gst_value_can_compare_unchecked (value1, value2);
6224 }
6225
6226 /**
6227 * gst_value_intersect:
6228 * @dest: (out caller-allocates) (transfer full) (allow-none):
6229 * a uninitialized #GValue that will hold the calculated
6230 * intersection value. May be %NULL if the resulting set if not
6231 * needed.
6232 * @value1: a value to intersect
6233 * @value2: another value to intersect
6234 *
6235 * Calculates the intersection of two values. If the values have
6236 * a non-empty intersection, the value representing the intersection
6237 * is placed in @dest, unless %NULL. If the intersection is non-empty,
6238 * @dest is not modified.
6239 *
6240 * Returns: %TRUE if the intersection is non-empty
6241 */
6242 gboolean
gst_value_intersect(GValue * dest,const GValue * value1,const GValue * value2)6243 gst_value_intersect (GValue * dest, const GValue * value1,
6244 const GValue * value2)
6245 {
6246 GstValueIntersectInfo *intersect_info;
6247 guint i, len;
6248 GType type1, type2;
6249
6250 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
6251 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
6252
6253 type1 = G_VALUE_TYPE (value1);
6254 type2 = G_VALUE_TYPE (value2);
6255
6256 /* special cases first */
6257 if (type1 == GST_TYPE_LIST)
6258 return gst_value_intersect_list (dest, value1, value2);
6259 if (type2 == GST_TYPE_LIST)
6260 return gst_value_intersect_list (dest, value2, value1);
6261
6262 if (_gst_value_compare_nolist (value1, value2) == GST_VALUE_EQUAL) {
6263 if (dest)
6264 gst_value_init_and_copy (dest, value1);
6265 return TRUE;
6266 }
6267
6268 if (type1 == type2) {
6269 /* Equal type comparison */
6270 if (type1 == GST_TYPE_INT_RANGE)
6271 return gst_value_intersect_int_range_int_range (dest, value1, value2);
6272 if (type1 == GST_TYPE_INT64_RANGE)
6273 return gst_value_intersect_int64_range_int64_range (dest, value1, value2);
6274 if (type1 == GST_TYPE_DOUBLE_RANGE)
6275 return gst_value_intersect_double_range_double_range (dest, value1,
6276 value2);
6277 if (type1 == GST_TYPE_ARRAY)
6278 return gst_value_intersect_array (dest, value1, value2);
6279 if (type1 == GST_TYPE_FRACTION_RANGE)
6280 return gst_value_intersect_fraction_range_fraction_range (dest, value1,
6281 value2);
6282 if (type1 == GST_TYPE_FLAG_SET)
6283 return gst_value_intersect_flagset_flagset (dest, value1, value2);
6284 if (type1 == GST_TYPE_STRUCTURE)
6285 return gst_value_intersect_structure_structure (dest, value1, value2);
6286 } else {
6287 /* Different type comparison */
6288 len = gst_value_intersect_funcs->len;
6289 for (i = 0; i < len; i++) {
6290 intersect_info = &g_array_index (gst_value_intersect_funcs,
6291 GstValueIntersectInfo, i);
6292 if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
6293 return intersect_info->func (dest, value1, value2);
6294 }
6295 if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
6296 return intersect_info->func (dest, value2, value1);
6297 }
6298 }
6299 }
6300
6301 /* Failed to find a direct intersection, check if these are
6302 * GstFlagSet sub-types. */
6303 if (G_UNLIKELY (GST_VALUE_HOLDS_FLAG_SET (value1) &&
6304 GST_VALUE_HOLDS_FLAG_SET (value2))) {
6305 return gst_value_intersect_flagset_flagset (dest, value1, value2);
6306 }
6307
6308 return FALSE;
6309 }
6310
6311
6312
6313 /* gst_value_register_intersect_func: (skip)
6314 * @type1: the first type to intersect
6315 * @type2: the second type to intersect
6316 * @func: the intersection function
6317 *
6318 * Registers a function that is called to calculate the intersection
6319 * of the values having the types @type1 and @type2.
6320 *
6321 * Intersect functions should be registered at startup before any pipelines are
6322 * started, as gst_value_register_intersect_func() is not thread-safe and
6323 * cannot be used at the same time as gst_value_intersect() or
6324 * gst_value_can_intersect().
6325 */
6326 static void
gst_value_register_intersect_func(GType type1,GType type2,GstValueIntersectFunc func)6327 gst_value_register_intersect_func (GType type1, GType type2,
6328 GstValueIntersectFunc func)
6329 {
6330 GstValueIntersectInfo intersect_info;
6331
6332 intersect_info.type1 = type1;
6333 intersect_info.type2 = type2;
6334 intersect_info.func = func;
6335
6336 g_array_append_val (gst_value_intersect_funcs, intersect_info);
6337 }
6338
6339
6340 /* subtraction */
6341
6342 /**
6343 * gst_value_subtract:
6344 * @dest: (out caller-allocates) (allow-none): the destination value
6345 * for the result if the subtraction is not empty. May be %NULL,
6346 * in which case the resulting set will not be computed, which can
6347 * give a fair speedup.
6348 * @minuend: the value to subtract from
6349 * @subtrahend: the value to subtract
6350 *
6351 * Subtracts @subtrahend from @minuend and stores the result in @dest.
6352 * Note that this means subtraction as in sets, not as in mathematics.
6353 *
6354 * Returns: %TRUE if the subtraction is not empty
6355 */
6356 gboolean
gst_value_subtract(GValue * dest,const GValue * minuend,const GValue * subtrahend)6357 gst_value_subtract (GValue * dest, const GValue * minuend,
6358 const GValue * subtrahend)
6359 {
6360 GstValueSubtractInfo *info;
6361 guint i, len;
6362 GType mtype, stype;
6363
6364 g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
6365 g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
6366
6367 mtype = G_VALUE_TYPE (minuend);
6368 stype = G_VALUE_TYPE (subtrahend);
6369
6370 /* special cases first */
6371 if (mtype == GST_TYPE_LIST)
6372 return gst_value_subtract_from_list (dest, minuend, subtrahend);
6373 if (stype == GST_TYPE_LIST)
6374 return gst_value_subtract_list (dest, minuend, subtrahend);
6375
6376 len = gst_value_subtract_funcs->len;
6377 for (i = 0; i < len; i++) {
6378 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
6379 if (info->minuend == mtype && info->subtrahend == stype) {
6380 return info->func (dest, minuend, subtrahend);
6381 }
6382 }
6383
6384 if (_gst_value_compare_nolist (minuend, subtrahend) != GST_VALUE_EQUAL) {
6385 if (dest)
6386 gst_value_init_and_copy (dest, minuend);
6387 return TRUE;
6388 }
6389
6390 return FALSE;
6391 }
6392
6393 #if 0
6394 gboolean
6395 gst_value_subtract (GValue * dest, const GValue * minuend,
6396 const GValue * subtrahend)
6397 {
6398 gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
6399
6400 g_printerr ("\"%s\" - \"%s\" = \"%s\"\n", gst_value_serialize (minuend),
6401 gst_value_serialize (subtrahend),
6402 ret ? gst_value_serialize (dest) : "---");
6403 return ret;
6404 }
6405 #endif
6406
6407 /**
6408 * gst_value_can_subtract:
6409 * @minuend: the value to subtract from
6410 * @subtrahend: the value to subtract
6411 *
6412 * Checks if it's possible to subtract @subtrahend from @minuend.
6413 *
6414 * Returns: %TRUE if a subtraction is possible
6415 */
6416 gboolean
gst_value_can_subtract(const GValue * minuend,const GValue * subtrahend)6417 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
6418 {
6419 GstValueSubtractInfo *info;
6420 guint i, len;
6421 GType mtype, stype;
6422
6423 g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
6424 g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
6425
6426 mtype = G_VALUE_TYPE (minuend);
6427 stype = G_VALUE_TYPE (subtrahend);
6428
6429 /* special cases */
6430 if (mtype == GST_TYPE_LIST || stype == GST_TYPE_LIST)
6431 return TRUE;
6432 if (mtype == GST_TYPE_STRUCTURE || stype == GST_TYPE_STRUCTURE)
6433 return FALSE;
6434
6435 len = gst_value_subtract_funcs->len;
6436 for (i = 0; i < len; i++) {
6437 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
6438 if (info->minuend == mtype && info->subtrahend == stype)
6439 return TRUE;
6440 }
6441
6442 return gst_value_can_compare_unchecked (minuend, subtrahend);
6443 }
6444
6445 /* gst_value_register_subtract_func: (skip)
6446 * @minuend_type: type of the minuend
6447 * @subtrahend_type: type of the subtrahend
6448 * @func: function to use
6449 *
6450 * Registers @func as a function capable of subtracting the values of
6451 * @subtrahend_type from values of @minuend_type.
6452 *
6453 * Subtract functions should be registered at startup before any pipelines are
6454 * started, as gst_value_register_subtract_func() is not thread-safe and
6455 * cannot be used at the same time as gst_value_subtract().
6456 */
6457 static void
gst_value_register_subtract_func(GType minuend_type,GType subtrahend_type,GstValueSubtractFunc func)6458 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
6459 GstValueSubtractFunc func)
6460 {
6461 GstValueSubtractInfo info;
6462
6463 g_return_if_fail (!gst_type_is_fixed (minuend_type)
6464 || !gst_type_is_fixed (subtrahend_type));
6465
6466 info.minuend = minuend_type;
6467 info.subtrahend = subtrahend_type;
6468 info.func = func;
6469
6470 g_array_append_val (gst_value_subtract_funcs, info);
6471 }
6472
6473 /**
6474 * gst_value_register:
6475 * @table: structure containing functions to register
6476 *
6477 * Registers functions to perform calculations on #GValue items of a given
6478 * type. Each type can only be added once.
6479 */
6480 void
gst_value_register(const GstValueTable * table)6481 gst_value_register (const GstValueTable * table)
6482 {
6483 GstValueTable *found;
6484
6485 g_return_if_fail (table != NULL);
6486
6487 g_array_append_val (gst_value_table, *table);
6488
6489 found = gst_value_hash_lookup_type (table->type);
6490 if (found)
6491 g_warning ("adding type %s multiple times", g_type_name (table->type));
6492
6493 /* FIXME: we're not really doing the const justice, we assume the table is
6494 * static */
6495 gst_value_hash_add_type (table->type, table);
6496 }
6497
6498 /**
6499 * gst_value_init_and_copy:
6500 * @dest: (out caller-allocates): the target value
6501 * @src: the source value
6502 *
6503 * Initialises the target value to be of the same type as source and then copies
6504 * the contents from source to target.
6505 */
6506 void
gst_value_init_and_copy(GValue * dest,const GValue * src)6507 gst_value_init_and_copy (GValue * dest, const GValue * src)
6508 {
6509 GType type;
6510
6511 g_return_if_fail (G_IS_VALUE (src));
6512 g_return_if_fail (dest != NULL);
6513
6514 type = G_VALUE_TYPE (src);
6515 /* We need to shortcut GstValueList/GstValueArray copying because:
6516 * * g_value_init would end up allocating something
6517 * * which g_value_copy would then free and re-alloc.
6518 *
6519 * Instead directly call the copy */
6520 if (type == GST_TYPE_LIST || type == GST_TYPE_ARRAY) {
6521 dest->g_type = type;
6522 gst_value_copy_list_or_array (src, dest);
6523 return;
6524 }
6525
6526 g_value_init (dest, type);
6527 g_value_copy (src, dest);
6528 }
6529
6530 /* move src into dest and clear src */
6531 static void
gst_value_move(GValue * dest,GValue * src)6532 gst_value_move (GValue * dest, GValue * src)
6533 {
6534 g_assert (G_IS_VALUE (src));
6535 g_assert (dest != NULL);
6536
6537 *dest = *src;
6538 memset (src, 0, sizeof (GValue));
6539 }
6540
6541 /**
6542 * gst_value_serialize:
6543 * @value: a #GValue to serialize
6544 *
6545 * tries to transform the given @value into a string representation that allows
6546 * getting back this string later on using gst_value_deserialize().
6547 *
6548 * Free-function: g_free
6549 *
6550 * Returns: (transfer full) (nullable): the serialization for @value
6551 * or %NULL if none exists
6552 */
6553 gchar *
gst_value_serialize(const GValue * value)6554 gst_value_serialize (const GValue * value)
6555 {
6556 guint i, len;
6557 GValue s_val = { 0 };
6558 GstValueTable *table, *best;
6559 gchar *s;
6560 GType type;
6561
6562 g_return_val_if_fail (G_IS_VALUE (value), NULL);
6563
6564 type = G_VALUE_TYPE (value);
6565
6566 best = gst_value_hash_lookup_type (type);
6567
6568 if (G_UNLIKELY (!best || !best->serialize)) {
6569 len = gst_value_table->len;
6570 best = NULL;
6571 for (i = 0; i < len; i++) {
6572 table = &g_array_index (gst_value_table, GstValueTable, i);
6573 if (table->serialize && g_type_is_a (type, table->type)) {
6574 if (!best || g_type_is_a (table->type, best->type))
6575 best = table;
6576 }
6577 }
6578 }
6579 if (G_LIKELY (best))
6580 return best->serialize (value);
6581
6582 g_value_init (&s_val, G_TYPE_STRING);
6583 if (g_value_transform (value, &s_val)) {
6584 s = gst_string_wrap (g_value_get_string (&s_val));
6585 } else {
6586 s = NULL;
6587 }
6588 g_value_unset (&s_val);
6589
6590 return s;
6591 }
6592
6593 /**
6594 * gst_value_deserialize:
6595 * @dest: (out caller-allocates): #GValue to fill with contents of
6596 * deserialization
6597 * @src: string to deserialize
6598 *
6599 * Tries to deserialize a string into the type specified by the given GValue.
6600 * If the operation succeeds, %TRUE is returned, %FALSE otherwise.
6601 *
6602 * Returns: %TRUE on success
6603 */
6604 gboolean
gst_value_deserialize(GValue * dest,const gchar * src)6605 gst_value_deserialize (GValue * dest, const gchar * src)
6606 {
6607 GstValueTable *table, *best;
6608 guint i, len;
6609 GType type;
6610
6611 g_return_val_if_fail (src != NULL, FALSE);
6612 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
6613
6614 type = G_VALUE_TYPE (dest);
6615
6616 best = gst_value_hash_lookup_type (type);
6617 if (G_UNLIKELY (!best || (!best->deserialize
6618 && !best->deserialize_with_pspec))) {
6619 len = gst_value_table->len;
6620 best = NULL;
6621 for (i = 0; i < len; i++) {
6622 table = &g_array_index (gst_value_table, GstValueTable, i);
6623 if ((table->deserialize || table->deserialize_with_pspec) &&
6624 g_type_is_a (type, table->type)) {
6625 if (!best || g_type_is_a (table->type, best->type))
6626 best = table;
6627 }
6628 }
6629 }
6630 if (G_LIKELY (best)) {
6631 if (best->deserialize_with_pspec)
6632 return best->deserialize_with_pspec (dest, src, NULL);
6633 else
6634 return best->deserialize (dest, src);
6635 }
6636
6637 return FALSE;
6638 }
6639
6640 /**
6641 * gst_value_deserialize_with_pspec:
6642 * @dest: (out caller-allocates): #GValue to fill with contents of
6643 * deserialization
6644 * @src: string to deserialize
6645 * @pspec: (nullable): the #GParamSpec describing the expected value
6646 *
6647 * Tries to deserialize a string into the type specified by the given GValue.
6648 * @pspec may be used to guide the deserializing of nested members.
6649 * If the operation succeeds, %TRUE is returned, %FALSE otherwise.
6650 *
6651 * Returns: %TRUE on success
6652 * Since: 1.20
6653 */
6654 gboolean
gst_value_deserialize_with_pspec(GValue * dest,const gchar * src,GParamSpec * pspec)6655 gst_value_deserialize_with_pspec (GValue * dest, const gchar * src,
6656 GParamSpec * pspec)
6657 {
6658 GstValueTable *table, *best;
6659 guint i, len;
6660 GType type;
6661
6662 g_return_val_if_fail (src != NULL, FALSE);
6663 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
6664
6665 if (pspec)
6666 g_return_val_if_fail (G_VALUE_TYPE (dest) ==
6667 G_PARAM_SPEC_VALUE_TYPE (pspec), FALSE);
6668
6669 type = G_VALUE_TYPE (dest);
6670
6671 best = gst_value_hash_lookup_type (type);
6672 if (G_UNLIKELY (!best || (!best->deserialize
6673 && !best->deserialize_with_pspec))) {
6674 len = gst_value_table->len;
6675 best = NULL;
6676 for (i = 0; i < len; i++) {
6677 table = &g_array_index (gst_value_table, GstValueTable, i);
6678 if ((table->deserialize || table->deserialize_with_pspec) &&
6679 g_type_is_a (type, table->type)) {
6680 if (!best || g_type_is_a (table->type, best->type))
6681 best = table;
6682 }
6683 }
6684 }
6685 if (G_LIKELY (best)) {
6686 if (best->deserialize_with_pspec)
6687 return best->deserialize_with_pspec (dest, src, pspec);
6688 else
6689 return best->deserialize (dest, src);
6690 }
6691
6692 return FALSE;
6693 }
6694
6695 static gboolean
structure_field_is_fixed(GQuark field_id,const GValue * val,gpointer user_data)6696 structure_field_is_fixed (GQuark field_id, const GValue * val,
6697 gpointer user_data)
6698 {
6699 return gst_value_is_fixed (val);
6700 }
6701
6702 /**
6703 * gst_value_is_fixed:
6704 * @value: the #GValue to check
6705 *
6706 * Tests if the given GValue, if available in a GstStructure (or any other
6707 * container) contains a "fixed" (which means: one value) or an "unfixed"
6708 * (which means: multiple possible values, such as data lists or data
6709 * ranges) value.
6710 *
6711 * Returns: true if the value is "fixed".
6712 */
6713
6714 gboolean
gst_value_is_fixed(const GValue * value)6715 gst_value_is_fixed (const GValue * value)
6716 {
6717 GType type;
6718
6719 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
6720
6721 type = G_VALUE_TYPE (value);
6722
6723 /* the most common types are just basic plain glib types */
6724 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
6725 return TRUE;
6726 }
6727
6728 if (type == GST_TYPE_ARRAY) {
6729 gint size, n;
6730 const GValue *kid;
6731
6732 /* check recursively */
6733 size = gst_value_array_get_size (value);
6734 for (n = 0; n < size; n++) {
6735 kid = gst_value_array_get_value (value, n);
6736 if (!gst_value_is_fixed (kid))
6737 return FALSE;
6738 }
6739 return TRUE;
6740 } else if (GST_VALUE_HOLDS_FLAG_SET (value)) {
6741 /* Flagsets are only fixed if there are no 'don't care' bits */
6742 return (gst_value_get_flagset_mask (value) == GST_FLAG_SET_MASK_EXACT);
6743 } else if (GST_VALUE_HOLDS_STRUCTURE (value)) {
6744 return gst_structure_foreach (gst_value_get_structure (value),
6745 structure_field_is_fixed, NULL);
6746 }
6747 return gst_type_is_fixed (type);
6748 }
6749
6750 /**
6751 * gst_value_fixate:
6752 * @dest: the #GValue destination
6753 * @src: the #GValue to fixate
6754 *
6755 * Fixate @src into a new value @dest.
6756 * For ranges, the first element is taken. For lists and arrays, the
6757 * first item is fixated and returned.
6758 * If @src is already fixed, this function returns %FALSE.
6759 *
6760 * Returns: %TRUE if @dest contains a fixated version of @src.
6761 */
6762 gboolean
gst_value_fixate(GValue * dest,const GValue * src)6763 gst_value_fixate (GValue * dest, const GValue * src)
6764 {
6765 g_return_val_if_fail (G_IS_VALUE (src), FALSE);
6766 g_return_val_if_fail (dest != NULL, FALSE);
6767
6768 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
6769 g_value_init (dest, G_TYPE_INT);
6770 g_value_set_int (dest, gst_value_get_int_range_min (src));
6771 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
6772 g_value_init (dest, G_TYPE_DOUBLE);
6773 g_value_set_double (dest, gst_value_get_double_range_min (src));
6774 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
6775 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
6776 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
6777 GValue temp = { 0 };
6778
6779 /* list could be empty */
6780 if (gst_value_list_get_size (src) <= 0)
6781 return FALSE;
6782
6783 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
6784
6785 if (!gst_value_fixate (dest, &temp)) {
6786 gst_value_move (dest, &temp);
6787 } else {
6788 g_value_unset (&temp);
6789 }
6790 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
6791 gboolean res = FALSE;
6792 guint n, len;
6793
6794 len = gst_value_array_get_size (src);
6795 g_value_init (dest, GST_TYPE_ARRAY);
6796 for (n = 0; n < len; n++) {
6797 GValue kid = { 0 };
6798 const GValue *orig_kid = gst_value_array_get_value (src, n);
6799
6800 if (!gst_value_fixate (&kid, orig_kid))
6801 gst_value_init_and_copy (&kid, orig_kid);
6802 else
6803 res = TRUE;
6804 _gst_value_array_append_and_take_value (dest, &kid);
6805 }
6806
6807 if (!res)
6808 g_value_unset (dest);
6809
6810 return res;
6811 } else if (GST_VALUE_HOLDS_FLAG_SET (src)) {
6812 guint flags;
6813
6814 if (gst_value_get_flagset_mask (src) == GST_FLAG_SET_MASK_EXACT)
6815 return FALSE; /* Already fixed */
6816
6817 flags = gst_value_get_flagset_flags (src);
6818 g_value_init (dest, G_VALUE_TYPE (src));
6819 gst_value_set_flagset (dest, flags, GST_FLAG_SET_MASK_EXACT);
6820 return TRUE;
6821 } else if (GST_VALUE_HOLDS_STRUCTURE (src)) {
6822 const GstStructure *str = (GstStructure *) gst_value_get_structure (src);
6823 GstStructure *kid;
6824
6825 if (!str)
6826 return FALSE;
6827
6828 kid = gst_structure_copy (str);
6829 gst_structure_fixate (kid);
6830 g_value_init (dest, GST_TYPE_STRUCTURE);
6831 gst_value_set_structure (dest, kid);
6832 gst_structure_free (kid);
6833 return TRUE;
6834 } else {
6835 return FALSE;
6836 }
6837 return TRUE;
6838 }
6839
6840
6841 /************
6842 * fraction *
6843 ************/
6844
6845 /* helper functions */
6846 static void
gst_value_init_fraction(GValue * value)6847 gst_value_init_fraction (GValue * value)
6848 {
6849 value->data[0].v_int = 0;
6850 value->data[1].v_int = 1;
6851 }
6852
6853 static void
gst_value_copy_fraction(const GValue * src_value,GValue * dest_value)6854 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
6855 {
6856 dest_value->data[0].v_int = src_value->data[0].v_int;
6857 dest_value->data[1].v_int = src_value->data[1].v_int;
6858 }
6859
6860 static gchar *
gst_value_collect_fraction(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)6861 gst_value_collect_fraction (GValue * value, guint n_collect_values,
6862 GTypeCValue * collect_values, guint collect_flags)
6863 {
6864 g_return_val_if_fail (n_collect_values == 2,
6865 g_strdup_printf ("not enough value locations for `%s' passed",
6866 G_VALUE_TYPE_NAME (value)));
6867 g_return_val_if_fail (collect_values[1].v_int != 0,
6868 g_strdup_printf ("passed '0' as denominator for `%s'",
6869 G_VALUE_TYPE_NAME (value)));
6870 g_return_val_if_fail (collect_values[0].v_int >= -G_MAXINT,
6871 g_strdup_printf
6872 ("passed value smaller than -G_MAXINT as numerator for `%s'",
6873 G_VALUE_TYPE_NAME (value)));
6874 g_return_val_if_fail (collect_values[1].v_int >= -G_MAXINT,
6875 g_strdup_printf
6876 ("passed value smaller than -G_MAXINT as denominator for `%s'",
6877 G_VALUE_TYPE_NAME (value)));
6878
6879 gst_value_set_fraction (value,
6880 collect_values[0].v_int, collect_values[1].v_int);
6881
6882 return NULL;
6883 }
6884
6885 static gchar *
gst_value_lcopy_fraction(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)6886 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
6887 GTypeCValue * collect_values, guint collect_flags)
6888 {
6889 gint *numerator = collect_values[0].v_pointer;
6890 gint *denominator = collect_values[1].v_pointer;
6891
6892 g_return_val_if_fail (numerator != NULL,
6893 g_strdup_printf ("numerator for `%s' passed as NULL",
6894 G_VALUE_TYPE_NAME (value)));
6895 g_return_val_if_fail (denominator != NULL,
6896 g_strdup_printf ("denominator for `%s' passed as NULL",
6897 G_VALUE_TYPE_NAME (value)));
6898
6899 *numerator = value->data[0].v_int;
6900 *denominator = value->data[1].v_int;
6901
6902 return NULL;
6903 }
6904
6905 /**
6906 * gst_value_set_fraction:
6907 * @value: a GValue initialized to #GST_TYPE_FRACTION
6908 * @numerator: the numerator of the fraction
6909 * @denominator: the denominator of the fraction
6910 *
6911 * Sets @value to the fraction specified by @numerator over @denominator.
6912 * The fraction gets reduced to the smallest numerator and denominator,
6913 * and if necessary the sign is moved to the numerator.
6914 */
6915 void
gst_value_set_fraction(GValue * value,gint numerator,gint denominator)6916 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
6917 {
6918 gint gcd = 0;
6919
6920 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
6921 g_return_if_fail (denominator != 0);
6922 g_return_if_fail (denominator >= -G_MAXINT);
6923 g_return_if_fail (numerator >= -G_MAXINT);
6924
6925 /* normalize sign */
6926 if (denominator < 0) {
6927 numerator = -numerator;
6928 denominator = -denominator;
6929 }
6930
6931 /* check for reduction */
6932 gcd = gst_util_greatest_common_divisor (numerator, denominator);
6933 if (gcd) {
6934 numerator /= gcd;
6935 denominator /= gcd;
6936 }
6937
6938 g_assert (denominator > 0);
6939
6940 value->data[0].v_int = numerator;
6941 value->data[1].v_int = denominator;
6942 }
6943
6944 /**
6945 * gst_value_get_fraction_numerator:
6946 * @value: a GValue initialized to #GST_TYPE_FRACTION
6947 *
6948 * Gets the numerator of the fraction specified by @value.
6949 *
6950 * Returns: the numerator of the fraction.
6951 */
6952 gint
gst_value_get_fraction_numerator(const GValue * value)6953 gst_value_get_fraction_numerator (const GValue * value)
6954 {
6955 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
6956
6957 return value->data[0].v_int;
6958 }
6959
6960 /**
6961 * gst_value_get_fraction_denominator:
6962 * @value: a GValue initialized to #GST_TYPE_FRACTION
6963 *
6964 * Gets the denominator of the fraction specified by @value.
6965 *
6966 * Returns: the denominator of the fraction.
6967 */
6968 gint
gst_value_get_fraction_denominator(const GValue * value)6969 gst_value_get_fraction_denominator (const GValue * value)
6970 {
6971 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
6972
6973 return value->data[1].v_int;
6974 }
6975
6976 /**
6977 * gst_value_fraction_multiply:
6978 * @product: a GValue initialized to #GST_TYPE_FRACTION
6979 * @factor1: a GValue initialized to #GST_TYPE_FRACTION
6980 * @factor2: a GValue initialized to #GST_TYPE_FRACTION
6981 *
6982 * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
6983 * @product to the product of the two fractions.
6984 *
6985 * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
6986 */
6987 gboolean
gst_value_fraction_multiply(GValue * product,const GValue * factor1,const GValue * factor2)6988 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
6989 const GValue * factor2)
6990 {
6991 gint n1, n2, d1, d2;
6992 gint res_n, res_d;
6993
6994 g_return_val_if_fail (product != NULL, FALSE);
6995 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
6996 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
6997
6998 n1 = factor1->data[0].v_int;
6999 n2 = factor2->data[0].v_int;
7000 d1 = factor1->data[1].v_int;
7001 d2 = factor2->data[1].v_int;
7002
7003 if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
7004 return FALSE;
7005
7006 gst_value_set_fraction (product, res_n, res_d);
7007
7008 return TRUE;
7009 }
7010
7011 /**
7012 * gst_value_fraction_subtract:
7013 * @dest: a GValue initialized to #GST_TYPE_FRACTION
7014 * @minuend: a GValue initialized to #GST_TYPE_FRACTION
7015 * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
7016 *
7017 * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
7018 *
7019 * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
7020 */
7021 gboolean
gst_value_fraction_subtract(GValue * dest,const GValue * minuend,const GValue * subtrahend)7022 gst_value_fraction_subtract (GValue * dest,
7023 const GValue * minuend, const GValue * subtrahend)
7024 {
7025 gint n1, n2, d1, d2;
7026 gint res_n, res_d;
7027
7028 g_return_val_if_fail (dest != NULL, FALSE);
7029 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
7030 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
7031
7032 n1 = minuend->data[0].v_int;
7033 n2 = subtrahend->data[0].v_int;
7034 d1 = minuend->data[1].v_int;
7035 d2 = subtrahend->data[1].v_int;
7036
7037 if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
7038 return FALSE;
7039 gst_value_set_fraction (dest, res_n, res_d);
7040
7041 return TRUE;
7042 }
7043
7044 static gchar *
gst_value_serialize_fraction(const GValue * value)7045 gst_value_serialize_fraction (const GValue * value)
7046 {
7047 gint32 numerator = value->data[0].v_int;
7048 gint32 denominator = value->data[1].v_int;
7049 gboolean positive = TRUE;
7050
7051 /* get the sign and make components absolute */
7052 if (numerator < 0) {
7053 numerator = -numerator;
7054 positive = !positive;
7055 }
7056 if (denominator < 0) {
7057 denominator = -denominator;
7058 positive = !positive;
7059 }
7060
7061 return g_strdup_printf ("%s%d/%d",
7062 positive ? "" : "-", numerator, denominator);
7063 }
7064
7065 static gboolean
gst_value_deserialize_fraction(GValue * dest,const gchar * s)7066 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
7067 {
7068 gint num, den;
7069 gint num_chars;
7070
7071 if (G_UNLIKELY (s == NULL))
7072 return FALSE;
7073
7074 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
7075 return FALSE;
7076
7077 if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
7078 if (s[num_chars] != 0)
7079 return FALSE;
7080 if (den == 0)
7081 return FALSE;
7082
7083 gst_value_set_fraction (dest, num, den);
7084 return TRUE;
7085 } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
7086 gst_value_set_fraction (dest, 1, G_MAXINT);
7087 return TRUE;
7088 } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
7089 if (s[num_chars] != 0)
7090 return FALSE;
7091 gst_value_set_fraction (dest, num, 1);
7092 return TRUE;
7093 } else if (g_ascii_strcasecmp (s, "min") == 0) {
7094 gst_value_set_fraction (dest, -G_MAXINT, 1);
7095 return TRUE;
7096 } else if (g_ascii_strcasecmp (s, "max") == 0) {
7097 gst_value_set_fraction (dest, G_MAXINT, 1);
7098 return TRUE;
7099 }
7100
7101 return FALSE;
7102 }
7103
7104 static void
gst_value_transform_fraction_string(const GValue * src_value,GValue * dest_value)7105 gst_value_transform_fraction_string (const GValue * src_value,
7106 GValue * dest_value)
7107 {
7108 dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
7109 }
7110
7111 static void
gst_value_transform_string_fraction(const GValue * src_value,GValue * dest_value)7112 gst_value_transform_string_fraction (const GValue * src_value,
7113 GValue * dest_value)
7114 {
7115 if (!gst_value_deserialize_fraction (dest_value,
7116 src_value->data[0].v_pointer))
7117 /* If the deserialize fails, ensure we leave the fraction in a
7118 * valid, if incorrect, state */
7119 gst_value_set_fraction (dest_value, 0, 1);
7120 }
7121
7122 static void
gst_value_transform_double_fraction(const GValue * src_value,GValue * dest_value)7123 gst_value_transform_double_fraction (const GValue * src_value,
7124 GValue * dest_value)
7125 {
7126 gdouble src = g_value_get_double (src_value);
7127 gint n, d;
7128
7129 gst_util_double_to_fraction (src, &n, &d);
7130 gst_value_set_fraction (dest_value, n, d);
7131 }
7132
7133 static void
gst_value_transform_float_fraction(const GValue * src_value,GValue * dest_value)7134 gst_value_transform_float_fraction (const GValue * src_value,
7135 GValue * dest_value)
7136 {
7137 gfloat src = g_value_get_float (src_value);
7138 gint n, d;
7139
7140 gst_util_double_to_fraction (src, &n, &d);
7141 gst_value_set_fraction (dest_value, n, d);
7142 }
7143
7144 static void
gst_value_transform_fraction_double(const GValue * src_value,GValue * dest_value)7145 gst_value_transform_fraction_double (const GValue * src_value,
7146 GValue * dest_value)
7147 {
7148 dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
7149 ((double) src_value->data[1].v_int);
7150 }
7151
7152 static void
gst_value_transform_fraction_float(const GValue * src_value,GValue * dest_value)7153 gst_value_transform_fraction_float (const GValue * src_value,
7154 GValue * dest_value)
7155 {
7156 dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
7157 ((float) src_value->data[1].v_int);
7158 }
7159
7160 static gint
gst_value_compare_fraction(const GValue * value1,const GValue * value2)7161 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
7162 {
7163 gint n1, n2;
7164 gint d1, d2;
7165 gint ret;
7166
7167 n1 = value1->data[0].v_int;
7168 n2 = value2->data[0].v_int;
7169 d1 = value1->data[1].v_int;
7170 d2 = value2->data[1].v_int;
7171
7172 /* fractions are reduced when set, so we can quickly see if they're equal */
7173 if (n1 == n2 && d1 == d2)
7174 return GST_VALUE_EQUAL;
7175
7176 if (d1 == 0 && d2 == 0)
7177 return GST_VALUE_UNORDERED;
7178 else if (d1 == 0)
7179 return GST_VALUE_GREATER_THAN;
7180 else if (d2 == 0)
7181 return GST_VALUE_LESS_THAN;
7182
7183 ret = gst_util_fraction_compare (n1, d1, n2, d2);
7184 if (ret == -1)
7185 return GST_VALUE_LESS_THAN;
7186 else if (ret == 1)
7187 return GST_VALUE_GREATER_THAN;
7188
7189 /* Equality can't happen here because we check for that
7190 * first already */
7191 g_return_val_if_reached (GST_VALUE_UNORDERED);
7192 }
7193
7194 /*********
7195 * GDate *
7196 *********/
7197
7198 static gint
gst_value_compare_date(const GValue * value1,const GValue * value2)7199 gst_value_compare_date (const GValue * value1, const GValue * value2)
7200 {
7201 const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
7202 const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
7203 guint32 j1, j2;
7204
7205 if (date1 == date2)
7206 return GST_VALUE_EQUAL;
7207
7208 if ((date1 == NULL || !g_date_valid (date1))
7209 && (date2 != NULL && g_date_valid (date2))) {
7210 return GST_VALUE_LESS_THAN;
7211 }
7212
7213 if ((date2 == NULL || !g_date_valid (date2))
7214 && (date1 != NULL && g_date_valid (date1))) {
7215 return GST_VALUE_GREATER_THAN;
7216 }
7217
7218 if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
7219 || !g_date_valid (date2)) {
7220 return GST_VALUE_UNORDERED;
7221 }
7222
7223 j1 = g_date_get_julian (date1);
7224 j2 = g_date_get_julian (date2);
7225
7226 if (j1 == j2)
7227 return GST_VALUE_EQUAL;
7228 else if (j1 < j2)
7229 return GST_VALUE_LESS_THAN;
7230 else
7231 return GST_VALUE_GREATER_THAN;
7232 }
7233
7234 static gchar *
gst_value_serialize_date(const GValue * val)7235 gst_value_serialize_date (const GValue * val)
7236 {
7237 const GDate *date = (const GDate *) g_value_get_boxed (val);
7238
7239 if (date == NULL || !g_date_valid (date))
7240 return g_strdup ("9999-99-99");
7241
7242 return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
7243 g_date_get_month (date), g_date_get_day (date));
7244 }
7245
7246 static gboolean
gst_value_deserialize_date(GValue * dest,const gchar * s)7247 gst_value_deserialize_date (GValue * dest, const gchar * s)
7248 {
7249 guint year, month, day;
7250
7251 if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
7252 return FALSE;
7253
7254 if (!g_date_valid_dmy (day, month, year))
7255 return FALSE;
7256
7257 g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
7258 return TRUE;
7259 }
7260
7261 /*************
7262 * GstDateTime *
7263 *************/
7264
7265 static gint
gst_value_compare_date_time(const GValue * value1,const GValue * value2)7266 gst_value_compare_date_time (const GValue * value1, const GValue * value2)
7267 {
7268 const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
7269 const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
7270
7271 if (date1 == date2)
7272 return GST_VALUE_EQUAL;
7273
7274 if ((date1 == NULL) && (date2 != NULL)) {
7275 return GST_VALUE_LESS_THAN;
7276 }
7277 if ((date2 == NULL) && (date1 != NULL)) {
7278 return GST_VALUE_LESS_THAN;
7279 }
7280
7281 /* returns GST_VALUE_* */
7282 return __gst_date_time_compare (date1, date2);
7283 }
7284
7285 static gchar *
gst_value_serialize_date_time(const GValue * val)7286 gst_value_serialize_date_time (const GValue * val)
7287 {
7288 GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
7289
7290 if (date == NULL)
7291 return g_strdup ("null");
7292
7293 return __gst_date_time_serialize (date, TRUE);
7294 }
7295
7296 static gboolean
gst_value_deserialize_date_time(GValue * dest,const gchar * s)7297 gst_value_deserialize_date_time (GValue * dest, const gchar * s)
7298 {
7299 GstDateTime *datetime;
7300
7301 if (!s || strcmp (s, "null") == 0) {
7302 return FALSE;
7303 }
7304
7305 datetime = gst_date_time_new_from_iso8601_string (s);
7306 if (datetime != NULL) {
7307 g_value_take_boxed (dest, datetime);
7308 return TRUE;
7309 }
7310 GST_WARNING ("Failed to deserialize date time string '%s'", s);
7311 return FALSE;
7312 }
7313
7314 static void
gst_value_transform_date_string(const GValue * src_value,GValue * dest_value)7315 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
7316 {
7317 dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
7318 }
7319
7320 static void
gst_value_transform_string_date(const GValue * src_value,GValue * dest_value)7321 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
7322 {
7323 gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
7324 }
7325
7326
7327 /************
7328 * bitmask *
7329 ************/
7330
7331 /* helper functions */
7332 static void
gst_value_init_bitmask(GValue * value)7333 gst_value_init_bitmask (GValue * value)
7334 {
7335 value->data[0].v_uint64 = 0;
7336 }
7337
7338 static void
gst_value_copy_bitmask(const GValue * src_value,GValue * dest_value)7339 gst_value_copy_bitmask (const GValue * src_value, GValue * dest_value)
7340 {
7341 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
7342 }
7343
7344 static gchar *
gst_value_collect_bitmask(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)7345 gst_value_collect_bitmask (GValue * value, guint n_collect_values,
7346 GTypeCValue * collect_values, guint collect_flags)
7347 {
7348 g_return_val_if_fail (n_collect_values == 1,
7349 g_strdup_printf ("not enough value locations for `%s' passed",
7350 G_VALUE_TYPE_NAME (value)));
7351
7352 gst_value_set_bitmask (value, (guint64) collect_values[0].v_int64);
7353
7354 return NULL;
7355 }
7356
7357 static gchar *
gst_value_lcopy_bitmask(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)7358 gst_value_lcopy_bitmask (const GValue * value, guint n_collect_values,
7359 GTypeCValue * collect_values, guint collect_flags)
7360 {
7361 guint64 *bitmask = collect_values[0].v_pointer;
7362
7363 g_return_val_if_fail (bitmask != NULL,
7364 g_strdup_printf ("value for `%s' passed as NULL",
7365 G_VALUE_TYPE_NAME (value)));
7366
7367 *bitmask = value->data[0].v_uint64;
7368
7369 return NULL;
7370 }
7371
7372 /**
7373 * gst_value_set_bitmask:
7374 * @value: a GValue initialized to #GST_TYPE_BITMASK
7375 * @bitmask: the bitmask
7376 *
7377 * Sets @value to the bitmask specified by @bitmask.
7378 */
7379 void
gst_value_set_bitmask(GValue * value,guint64 bitmask)7380 gst_value_set_bitmask (GValue * value, guint64 bitmask)
7381 {
7382 g_return_if_fail (GST_VALUE_HOLDS_BITMASK (value));
7383
7384 value->data[0].v_uint64 = bitmask;
7385 }
7386
7387 /**
7388 * gst_value_get_bitmask:
7389 * @value: a GValue initialized to #GST_TYPE_BITMASK
7390 *
7391 * Gets the bitmask specified by @value.
7392 *
7393 * Returns: the bitmask.
7394 */
7395 guint64
gst_value_get_bitmask(const GValue * value)7396 gst_value_get_bitmask (const GValue * value)
7397 {
7398 g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (value), 0);
7399
7400 return value->data[0].v_uint64;
7401 }
7402
7403 static gchar *
gst_value_serialize_bitmask(const GValue * value)7404 gst_value_serialize_bitmask (const GValue * value)
7405 {
7406 guint64 bitmask = value->data[0].v_uint64;
7407
7408 return g_strdup_printf ("0x%016" G_GINT64_MODIFIER "x", bitmask);
7409 }
7410
7411 static gboolean
gst_value_deserialize_bitmask(GValue * dest,const gchar * s)7412 gst_value_deserialize_bitmask (GValue * dest, const gchar * s)
7413 {
7414 gchar *endptr = NULL;
7415 guint64 val;
7416
7417 if (G_UNLIKELY (s == NULL))
7418 return FALSE;
7419
7420 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_BITMASK (dest)))
7421 return FALSE;
7422
7423 errno = 0;
7424 val = g_ascii_strtoull (s, &endptr, 16);
7425 if (val == G_MAXUINT64 && (errno == ERANGE || errno == EINVAL))
7426 return FALSE;
7427 if (val == 0 && endptr == s)
7428 return FALSE;
7429
7430 gst_value_set_bitmask (dest, val);
7431
7432 return TRUE;
7433 }
7434
7435 static void
gst_value_transform_bitmask_string(const GValue * src_value,GValue * dest_value)7436 gst_value_transform_bitmask_string (const GValue * src_value,
7437 GValue * dest_value)
7438 {
7439 dest_value->data[0].v_pointer = gst_value_serialize_bitmask (src_value);
7440 }
7441
7442 static void
gst_value_transform_string_bitmask(const GValue * src_value,GValue * dest_value)7443 gst_value_transform_string_bitmask (const GValue * src_value,
7444 GValue * dest_value)
7445 {
7446 if (!gst_value_deserialize_bitmask (dest_value, src_value->data[0].v_pointer))
7447 gst_value_set_bitmask (dest_value, 0);
7448 }
7449
7450 static void
gst_value_transform_uint64_bitmask(const GValue * src_value,GValue * dest_value)7451 gst_value_transform_uint64_bitmask (const GValue * src_value,
7452 GValue * dest_value)
7453 {
7454 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
7455 }
7456
7457 static void
gst_value_transform_bitmask_uint64(const GValue * src_value,GValue * dest_value)7458 gst_value_transform_bitmask_uint64 (const GValue * src_value,
7459 GValue * dest_value)
7460 {
7461 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
7462 }
7463
7464 static gint
gst_value_compare_bitmask(const GValue * value1,const GValue * value2)7465 gst_value_compare_bitmask (const GValue * value1, const GValue * value2)
7466 {
7467 guint64 v1, v2;
7468
7469 v1 = value1->data[0].v_uint64;
7470 v2 = value2->data[0].v_uint64;
7471
7472 if (v1 == v2)
7473 return GST_VALUE_EQUAL;
7474
7475 return GST_VALUE_UNORDERED;
7476 }
7477
7478 /************
7479 * flagset *
7480 ************/
7481
7482 /* helper functions */
7483 static void
gst_value_init_flagset(GValue * value)7484 gst_value_init_flagset (GValue * value)
7485 {
7486 value->data[0].v_uint = 0;
7487 value->data[1].v_uint = 0;
7488 }
7489
7490 static void
gst_value_copy_flagset(const GValue * src_value,GValue * dest_value)7491 gst_value_copy_flagset (const GValue * src_value, GValue * dest_value)
7492 {
7493 dest_value->data[0].v_uint = src_value->data[0].v_uint;
7494 dest_value->data[1].v_uint = src_value->data[1].v_uint;
7495 }
7496
7497 static gchar *
gst_value_collect_flagset(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)7498 gst_value_collect_flagset (GValue * value, guint n_collect_values,
7499 GTypeCValue * collect_values, guint collect_flags)
7500 {
7501 g_return_val_if_fail (n_collect_values == 2,
7502 g_strdup_printf ("not enough value locations for `%s' passed",
7503 G_VALUE_TYPE_NAME (value)));
7504
7505 gst_value_set_flagset (value,
7506 (guint) collect_values[0].v_int, (guint) collect_values[1].v_int);
7507
7508 return NULL;
7509 }
7510
7511 static gchar *
gst_value_lcopy_flagset(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)7512 gst_value_lcopy_flagset (const GValue * value, guint n_collect_values,
7513 GTypeCValue * collect_values, guint collect_flags)
7514 {
7515 guint *flags = collect_values[0].v_pointer;
7516 guint *mask = collect_values[1].v_pointer;
7517
7518 *flags = value->data[0].v_uint;
7519 *mask = value->data[1].v_uint;
7520
7521 return NULL;
7522 }
7523
7524 /**
7525 * gst_value_set_flagset:
7526 * @value: a GValue initialized to %GST_TYPE_FLAG_SET
7527 * @flags: The value of the flags set or unset
7528 * @mask: The mask indicate which flags bits must match for comparisons
7529 *
7530 * Sets @value to the flags and mask values provided in @flags and @mask.
7531 * The @flags value indicates the values of flags, the @mask represents
7532 * which bits in the flag value have been set, and which are "don't care"
7533 *
7534 * Since: 1.6
7535 */
7536 void
gst_value_set_flagset(GValue * value,guint flags,guint mask)7537 gst_value_set_flagset (GValue * value, guint flags, guint mask)
7538 {
7539 g_return_if_fail (GST_VALUE_HOLDS_FLAG_SET (value));
7540
7541 /* Normalise and only keep flags mentioned in the mask */
7542 value->data[0].v_uint = flags & mask;
7543 value->data[1].v_uint = mask;
7544 }
7545
7546 /**
7547 * gst_value_get_flagset_flags:
7548 * @value: a GValue initialized to #GST_TYPE_FLAG_SET
7549 *
7550 * Retrieve the flags field of a GstFlagSet @value.
7551 *
7552 * Returns: the flags field of the flagset instance.
7553 *
7554 * Since: 1.6
7555 */
7556 guint
gst_value_get_flagset_flags(const GValue * value)7557 gst_value_get_flagset_flags (const GValue * value)
7558 {
7559 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (value), 0);
7560
7561 return value->data[0].v_uint;
7562 }
7563
7564 /**
7565 * gst_value_get_flagset_mask:
7566 * @value: a GValue initialized to #GST_TYPE_FLAG_SET
7567 *
7568 * Retrieve the mask field of a GstFlagSet @value.
7569 *
7570 * Returns: the mask field of the flagset instance.
7571 *
7572 * Since: 1.6
7573 */
7574 guint
gst_value_get_flagset_mask(const GValue * value)7575 gst_value_get_flagset_mask (const GValue * value)
7576 {
7577 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (value), 1);
7578
7579 return value->data[1].v_uint;
7580 }
7581
7582 static gchar *
gst_value_serialize_flagset(const GValue * value)7583 gst_value_serialize_flagset (const GValue * value)
7584 {
7585 guint flags = value->data[0].v_uint;
7586 guint mask = value->data[1].v_uint;
7587 GstFlagSetClass *set_klass =
7588 (GstFlagSetClass *) g_type_class_ref (G_VALUE_TYPE (value));
7589 gchar *result;
7590
7591 result = g_strdup_printf ("%x:%x", flags, mask);
7592
7593 /* If this flag set class has an associated GFlags GType, and some
7594 * bits in the mask, serialize the bits in human-readable form to
7595 * aid debugging */
7596 if (mask && set_klass->flags_type) {
7597 GFlagsClass *flags_klass =
7598 (GFlagsClass *) (g_type_class_ref (set_klass->flags_type));
7599 GFlagsValue *fl;
7600 gchar *tmp;
7601 gboolean first = TRUE;
7602
7603 g_return_val_if_fail (flags_klass, NULL);
7604
7605 /* some bits in the mask are set, so serialize one by one, according
7606 * to whether that bit is set or cleared in the flags value */
7607 while (mask) {
7608 fl = g_flags_get_first_value (flags_klass, mask);
7609 if (fl == NULL) {
7610 /* No more bits match in the flags mask - time to stop */
7611 mask = 0;
7612 break;
7613 }
7614
7615 tmp = g_strconcat (result,
7616 first ? ":" : "",
7617 (flags & fl->value) ? "+" : "/", fl->value_nick, NULL);
7618 g_free (result);
7619 result = tmp;
7620 first = FALSE;
7621
7622 /* clear flag */
7623 mask &= ~fl->value;
7624 }
7625 g_type_class_unref (flags_klass);
7626
7627 }
7628 g_type_class_unref (set_klass);
7629
7630 return result;
7631 }
7632
7633 static gboolean
is_valid_flags_string(const gchar * s)7634 is_valid_flags_string (const gchar * s)
7635 {
7636 /* We're looking to match +this/that+other-thing/not-this-thing type strings */
7637 return g_regex_match_simple ("^([\\+\\/][\\w\\d-]+)+$", s, G_REGEX_CASELESS,
7638 0);
7639 }
7640
7641 static gboolean
gst_value_deserialize_flagset(GValue * dest,const gchar * s)7642 gst_value_deserialize_flagset (GValue * dest, const gchar * s)
7643 {
7644 gboolean res = FALSE;
7645 guint flags, mask;
7646 gchar *cur, *next;
7647
7648 if (G_UNLIKELY (s == NULL))
7649 return FALSE;
7650
7651 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FLAG_SET (dest)))
7652 return FALSE;
7653
7654 /* Flagset strings look like %x:%x - hex flags : hex bitmask,
7655 * 32-bit each, or like a concatenated list of flag nicks,
7656 * with either '+' or '/' in front. The first form
7657 * may optionally be followed by ':' and a set of text flag descriptions
7658 * for easier debugging */
7659
7660 /* Try and interpret as hex form first, as it's the most efficient */
7661 /* Read the flags first */
7662 flags = strtoul (s, &next, 16);
7663 if (G_UNLIKELY ((flags == 0 && errno == EINVAL) || s == next))
7664 goto try_as_flags_string;
7665 /* Next char should be a colon */
7666 if (next[0] == ':')
7667 next++;
7668
7669 /* Read the mask */
7670 cur = next;
7671 mask = strtoul (cur, &next, 16);
7672 if (G_UNLIKELY ((mask == 0 && errno == EINVAL) || cur == next))
7673 goto try_as_flags_string;
7674
7675 /* Next char should be NULL terminator, or a ':'. If ':', we need the flag string after */
7676 if (G_UNLIKELY (next[0] == 0)) {
7677 res = TRUE;
7678 goto done;
7679 }
7680
7681 if (next[0] != ':')
7682 return FALSE;
7683
7684 s = next + 1;
7685
7686 if (g_str_equal (g_type_name (G_VALUE_TYPE (dest)), "GstFlagSet")) {
7687 /* If we're parsing a generic flag set, that can mean we're guessing
7688 * at the type in deserialising a GstStructure so at least check that
7689 * we have a valid-looking string, so we don't cause deserialisation of
7690 * other types of strings like 00:01:00:00 - https://bugzilla.gnome.org/show_bug.cgi?id=779755 */
7691 if (is_valid_flags_string (s)) {
7692 res = TRUE;
7693 goto done;
7694 }
7695 return FALSE;
7696 }
7697
7698 /* Otherwise, we already got a hex string for a valid non-generic flagset type */
7699 res = TRUE;
7700 goto done;
7701
7702 try_as_flags_string:
7703
7704 {
7705 const gchar *set_class = g_type_name (G_VALUE_TYPE (dest));
7706 GFlagsClass *flags_klass = NULL;
7707 const gchar *end;
7708
7709 if (g_str_equal (set_class, "GstFlagSet")) {
7710 /* There's no hope to parse the fields of generic flag set if we didn't already
7711 * catch a hex-string above */
7712 return FALSE;
7713 }
7714
7715 /* Flags class is the FlagSet class with 'Set' removed from the end */
7716 end = g_strrstr (set_class, "Set");
7717
7718 if (end != NULL) {
7719 gchar *class_name = g_strndup (set_class, end - set_class);
7720 GType flags_type = g_type_from_name (class_name);
7721 if (flags_type == 0) {
7722 GST_TRACE ("Looking for dynamic type %s", class_name);
7723 gst_dynamic_type_factory_load (class_name);
7724 }
7725
7726 if (flags_type != 0) {
7727 flags_klass = g_type_class_ref (flags_type);
7728 GST_TRACE ("Going to parse %s as %s", s, class_name);
7729 }
7730 g_free (class_name);
7731 }
7732
7733 if (flags_klass) {
7734 res = gst_value_gflags_str_to_flags (flags_klass, s, &flags, &mask);
7735 g_type_class_unref (flags_klass);
7736 }
7737 }
7738
7739 done:
7740 if (res)
7741 gst_value_set_flagset (dest, flags, mask);
7742 return res;
7743
7744 }
7745
7746 static void
gst_value_transform_flagset_string(const GValue * src_value,GValue * dest_value)7747 gst_value_transform_flagset_string (const GValue * src_value,
7748 GValue * dest_value)
7749 {
7750 dest_value->data[0].v_pointer = gst_value_serialize_flagset (src_value);
7751 }
7752
7753 static void
gst_value_transform_string_flagset(const GValue * src_value,GValue * dest_value)7754 gst_value_transform_string_flagset (const GValue * src_value,
7755 GValue * dest_value)
7756 {
7757 if (!gst_value_deserialize_flagset (dest_value, src_value->data[0].v_pointer)) {
7758 /* If the deserialize fails, ensure we leave the flags in a
7759 * valid, if incorrect, state */
7760 gst_value_set_flagset (dest_value, 0, 0);
7761 }
7762 }
7763
7764 static gint
gst_value_compare_flagset(const GValue * value1,const GValue * value2)7765 gst_value_compare_flagset (const GValue * value1, const GValue * value2)
7766 {
7767 guint v1, v2;
7768 guint m1, m2;
7769
7770 v1 = value1->data[0].v_uint;
7771 v2 = value2->data[0].v_uint;
7772
7773 m1 = value1->data[1].v_uint;
7774 m2 = value2->data[1].v_uint;
7775
7776 if (v1 == v2 && m1 == m2)
7777 return GST_VALUE_EQUAL;
7778
7779 return GST_VALUE_UNORDERED;
7780 }
7781
7782 /***********************
7783 * GstAllocationParams *
7784 ***********************/
7785 static gint
gst_value_compare_allocation_params(const GValue * value1,const GValue * value2)7786 gst_value_compare_allocation_params (const GValue * value1,
7787 const GValue * value2)
7788 {
7789 GstAllocationParams *v1, *v2;
7790
7791 v1 = value1->data[0].v_pointer;
7792 v2 = value2->data[0].v_pointer;
7793
7794 if (v1 == NULL && v1 == v2)
7795 return GST_VALUE_EQUAL;
7796
7797 if (v1 == NULL || v2 == NULL)
7798 return GST_VALUE_UNORDERED;
7799
7800 if (v1->flags == v2->flags && v1->align == v2->align &&
7801 v1->prefix == v2->prefix && v1->padding == v2->padding)
7802 return GST_VALUE_EQUAL;
7803
7804 return GST_VALUE_UNORDERED;
7805 }
7806
7807
7808 /************
7809 * GObject *
7810 ************/
7811
7812 static gint
gst_value_compare_object(const GValue * value1,const GValue * value2)7813 gst_value_compare_object (const GValue * value1, const GValue * value2)
7814 {
7815 gpointer v1, v2;
7816
7817 v1 = value1->data[0].v_pointer;
7818 v2 = value2->data[0].v_pointer;
7819
7820 if (v1 == v2)
7821 return GST_VALUE_EQUAL;
7822
7823 return GST_VALUE_UNORDERED;
7824 }
7825
7826 static void
gst_value_transform_object_string(const GValue * src_value,GValue * dest_value)7827 gst_value_transform_object_string (const GValue * src_value,
7828 GValue * dest_value)
7829 {
7830 GstObject *obj;
7831 gchar *str;
7832
7833 obj = g_value_get_object (src_value);
7834 if (obj) {
7835 str =
7836 g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
7837 GST_OBJECT_NAME (obj));
7838 } else {
7839 str = g_strdup ("NULL");
7840 }
7841
7842 dest_value->data[0].v_pointer = str;
7843 }
7844
7845 static GTypeInfo _info = {
7846 0, NULL, NULL, NULL, NULL, NULL, 0, 0, NULL, NULL,
7847 };
7848
7849 static GTypeFundamentalInfo _finfo = {
7850 0
7851 };
7852
7853 #define FUNC_VALUE_GET_TYPE_CLASSED(type, name, csize, flags) \
7854 GType _gst_ ## type ## _type = 0; \
7855 \
7856 GType gst_ ## type ## _get_type (void) \
7857 { \
7858 static GType gst_ ## type ## _type = 0; \
7859 \
7860 if (g_once_init_enter (&gst_ ## type ## _type)) { \
7861 GType _type; \
7862 _info.class_size = csize; \
7863 _finfo.type_flags = flags; \
7864 _info.value_table = & _gst_ ## type ## _value_table; \
7865 _type = g_type_register_fundamental ( \
7866 g_type_fundamental_next (), \
7867 name, &_info, &_finfo, 0); \
7868 _gst_ ## type ## _type = _type; \
7869 g_once_init_leave(&gst_ ## type ## _type, _type); \
7870 } \
7871 \
7872 return gst_ ## type ## _type; \
7873 }
7874
7875 #define FUNC_VALUE_GET_TYPE(type, name) \
7876 FUNC_VALUE_GET_TYPE_CLASSED(type, name, 0, 0)
7877
7878 static const GTypeValueTable _gst_int_range_value_table = {
7879 gst_value_init_int_range,
7880 NULL,
7881 gst_value_copy_int_range,
7882 NULL,
7883 (char *) "ii",
7884 gst_value_collect_int_range, (char *) "pp", gst_value_lcopy_int_range
7885 };
7886
7887 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
7888
7889 static const GTypeValueTable _gst_int64_range_value_table = {
7890 gst_value_init_int64_range,
7891 gst_value_free_int64_range,
7892 gst_value_copy_int64_range,
7893 NULL,
7894 (char *) "qq",
7895 gst_value_collect_int64_range,
7896 (char *) "pp", gst_value_lcopy_int64_range
7897 };
7898
7899 FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
7900
7901 static const GTypeValueTable _gst_double_range_value_table = {
7902 gst_value_init_double_range,
7903 NULL,
7904 gst_value_copy_double_range,
7905 NULL,
7906 (char *) "dd",
7907 gst_value_collect_double_range,
7908 (char *) "pp", gst_value_lcopy_double_range
7909 };
7910
7911 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
7912
7913 static const GTypeValueTable _gst_fraction_range_value_table = {
7914 gst_value_init_fraction_range,
7915 gst_value_free_fraction_range,
7916 gst_value_copy_fraction_range,
7917 NULL,
7918 (char *) "iiii",
7919 gst_value_collect_fraction_range,
7920 (char *) "pppp", gst_value_lcopy_fraction_range
7921 };
7922
7923 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
7924
7925 static const GTypeValueTable _gst_value_list_value_table = {
7926 gst_value_init_list_or_array,
7927 gst_value_free_list_or_array,
7928 gst_value_copy_list_or_array,
7929 gst_value_list_or_array_peek_pointer,
7930 (char *) "p",
7931 gst_value_collect_list_or_array,
7932 (char *) "p", gst_value_lcopy_list_or_array
7933 };
7934
7935 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
7936
7937 static const GTypeValueTable _gst_value_array_value_table = {
7938 gst_value_init_list_or_array,
7939 gst_value_free_list_or_array,
7940 gst_value_copy_list_or_array,
7941 gst_value_list_or_array_peek_pointer,
7942 (char *) "p",
7943 gst_value_collect_list_or_array,
7944 (char *) "p", gst_value_lcopy_list_or_array
7945 };
7946
7947 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
7948
7949 static const GTypeValueTable _gst_fraction_value_table = {
7950 gst_value_init_fraction,
7951 NULL,
7952 gst_value_copy_fraction,
7953 NULL,
7954 (char *) "ii",
7955 gst_value_collect_fraction, (char *) "pp", gst_value_lcopy_fraction
7956 };
7957
7958 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
7959
7960 static const GTypeValueTable _gst_bitmask_value_table = {
7961 gst_value_init_bitmask,
7962 NULL,
7963 gst_value_copy_bitmask,
7964 NULL,
7965 (char *) "q",
7966 gst_value_collect_bitmask, (char *) "p", gst_value_lcopy_bitmask
7967 };
7968
7969 FUNC_VALUE_GET_TYPE (bitmask, "GstBitmask");
7970
7971 static const GTypeValueTable _gst_flagset_value_table = {
7972 gst_value_init_flagset,
7973 NULL,
7974 gst_value_copy_flagset,
7975 NULL,
7976 (char *) "ii",
7977 gst_value_collect_flagset, (char *) "pp", gst_value_lcopy_flagset
7978 };
7979
7980 FUNC_VALUE_GET_TYPE_CLASSED (flagset, "GstFlagSet",
7981 sizeof (GstFlagSetClass), G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE);
7982
7983 GType
gst_g_thread_get_type(void)7984 gst_g_thread_get_type (void)
7985 {
7986 return G_TYPE_THREAD;
7987 }
7988
7989 #define SERIAL_VTABLE(t,c,s,d) { t, c, s, d, NULL }
7990 #define SERIAL_VTABLE_PSPEC(t,c,s,d) { t, c, s, NULL, d }
7991
7992 #define REGISTER_SERIALIZATION_CONST(_gtype, _type) \
7993 G_STMT_START { \
7994 static const GstValueTable gst_value = \
7995 SERIAL_VTABLE (_gtype, gst_value_compare_ ## _type, \
7996 gst_value_serialize_ ## _type, gst_value_deserialize_ ## _type); \
7997 gst_value_register (&gst_value); \
7998 } G_STMT_END
7999
8000 #define REGISTER_SERIALIZATION(_gtype, _type) \
8001 G_STMT_START { \
8002 static GstValueTable gst_value = \
8003 SERIAL_VTABLE (0, gst_value_compare_ ## _type, \
8004 gst_value_serialize_ ## _type, gst_value_deserialize_ ## _type); \
8005 gst_value.type = _gtype; \
8006 gst_value_register (&gst_value); \
8007 } G_STMT_END
8008
8009 #define REGISTER_SERIALIZATION_WITH_PSPEC(_gtype, _type) \
8010 G_STMT_START { \
8011 static GstValueTable gst_value = \
8012 SERIAL_VTABLE_PSPEC (0, gst_value_compare_ ## _type, \
8013 gst_value_serialize_ ## _type, gst_value_deserialize_ ## _type); \
8014 gst_value.type = _gtype; \
8015 gst_value_register (&gst_value); \
8016 } G_STMT_END
8017
8018 #define REGISTER_SERIALIZATION_NO_COMPARE(_gtype, _type) \
8019 G_STMT_START { \
8020 static GstValueTable gst_value = \
8021 SERIAL_VTABLE (0, NULL, \
8022 gst_value_serialize_ ## _type, gst_value_deserialize_ ## _type); \
8023 gst_value.type = _gtype; \
8024 gst_value_register (&gst_value); \
8025 } G_STMT_END
8026
8027 #define REGISTER_SERIALIZATION_COMPARE_ONLY(_gtype, _type) \
8028 G_STMT_START { \
8029 static GstValueTable gst_value = \
8030 SERIAL_VTABLE (0, gst_value_compare_ ## _type, \
8031 NULL, NULL); \
8032 gst_value.type = _gtype; \
8033 gst_value_register (&gst_value); \
8034 } G_STMT_END
8035
8036 /* These initial sizes are used for the tables
8037 * below, and save a couple of reallocs at startup */
8038
8039 static const gint GST_VALUE_TABLE_DEFAULT_SIZE = 40;
8040 static const gint GST_VALUE_UNION_TABLE_DEFAULT_SIZE = 8;
8041 static const gint GST_VALUE_INTERSECT_TABLE_DEFAULT_SIZE = 4;
8042 static const gint GST_VALUE_SUBTRACT_TABLE_DEFAULT_SIZE = 16;
8043
8044 void
_priv_gst_value_initialize(void)8045 _priv_gst_value_initialize (void)
8046 {
8047 gst_value_table =
8048 g_array_sized_new (FALSE, FALSE, sizeof (GstValueTable),
8049 GST_VALUE_TABLE_DEFAULT_SIZE);
8050 gst_value_hash = g_hash_table_new (NULL, NULL);
8051 gst_value_union_funcs = g_array_sized_new (FALSE, FALSE,
8052 sizeof (GstValueUnionInfo), GST_VALUE_UNION_TABLE_DEFAULT_SIZE);
8053 gst_value_intersect_funcs = g_array_sized_new (FALSE, FALSE,
8054 sizeof (GstValueIntersectInfo), GST_VALUE_INTERSECT_TABLE_DEFAULT_SIZE);
8055 gst_value_subtract_funcs = g_array_sized_new (FALSE, FALSE,
8056 sizeof (GstValueSubtractInfo), GST_VALUE_SUBTRACT_TABLE_DEFAULT_SIZE);
8057
8058 REGISTER_SERIALIZATION (gst_int_range_get_type (), int_range);
8059 REGISTER_SERIALIZATION (gst_int64_range_get_type (), int64_range);
8060 REGISTER_SERIALIZATION (gst_double_range_get_type (), double_range);
8061 REGISTER_SERIALIZATION (gst_fraction_range_get_type (), fraction_range);
8062 REGISTER_SERIALIZATION (g_value_array_get_type (), g_value_array);
8063 REGISTER_SERIALIZATION (gst_buffer_get_type (), buffer);
8064 REGISTER_SERIALIZATION (gst_sample_get_type (), sample);
8065 REGISTER_SERIALIZATION (gst_fraction_get_type (), fraction);
8066 REGISTER_SERIALIZATION (gst_caps_get_type (), caps);
8067 REGISTER_SERIALIZATION (gst_tag_list_get_type (), tag_list);
8068 REGISTER_SERIALIZATION (G_TYPE_DATE, date);
8069 REGISTER_SERIALIZATION (gst_date_time_get_type (), date_time);
8070 REGISTER_SERIALIZATION (gst_bitmask_get_type (), bitmask);
8071 REGISTER_SERIALIZATION (gst_structure_get_type (), structure);
8072 REGISTER_SERIALIZATION (gst_flagset_get_type (), flagset);
8073
8074 REGISTER_SERIALIZATION_NO_COMPARE (gst_segment_get_type (), segment);
8075 REGISTER_SERIALIZATION_NO_COMPARE (gst_caps_features_get_type (),
8076 caps_features);
8077
8078 REGISTER_SERIALIZATION_COMPARE_ONLY (gst_allocation_params_get_type (),
8079 allocation_params);
8080 REGISTER_SERIALIZATION_COMPARE_ONLY (G_TYPE_OBJECT, object);
8081
8082 REGISTER_SERIALIZATION_CONST (G_TYPE_DOUBLE, double);
8083 REGISTER_SERIALIZATION_CONST (G_TYPE_FLOAT, float);
8084
8085 REGISTER_SERIALIZATION_CONST (G_TYPE_STRING, string);
8086 REGISTER_SERIALIZATION_CONST (G_TYPE_BOOLEAN, boolean);
8087 REGISTER_SERIALIZATION_CONST (G_TYPE_ENUM, enum);
8088
8089 REGISTER_SERIALIZATION_CONST (G_TYPE_FLAGS, gflags);
8090
8091 REGISTER_SERIALIZATION_CONST (G_TYPE_INT, int);
8092
8093 REGISTER_SERIALIZATION_CONST (G_TYPE_INT64, int64);
8094 REGISTER_SERIALIZATION_CONST (G_TYPE_LONG, long);
8095
8096 REGISTER_SERIALIZATION_CONST (G_TYPE_UINT, uint);
8097 REGISTER_SERIALIZATION_CONST (G_TYPE_UINT64, uint64);
8098 REGISTER_SERIALIZATION_CONST (G_TYPE_ULONG, ulong);
8099
8100 REGISTER_SERIALIZATION_CONST (G_TYPE_UCHAR, uchar);
8101
8102 REGISTER_SERIALIZATION (G_TYPE_GTYPE, gtype);
8103
8104 REGISTER_SERIALIZATION_WITH_PSPEC (gst_value_list_get_type (), value_list);
8105 REGISTER_SERIALIZATION_WITH_PSPEC (gst_value_array_get_type (), value_array);
8106
8107 g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
8108 gst_value_transform_int_range_string);
8109 g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
8110 gst_value_transform_int64_range_string);
8111 g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
8112 gst_value_transform_double_range_string);
8113 g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
8114 gst_value_transform_fraction_range_string);
8115 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
8116 gst_value_transform_list_string);
8117 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_VALUE_ARRAY,
8118 gst_value_transform_any_list_g_value_array);
8119 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
8120 gst_value_transform_array_string);
8121 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_VALUE_ARRAY,
8122 gst_value_transform_any_list_g_value_array);
8123 g_value_register_transform_func (G_TYPE_VALUE_ARRAY, G_TYPE_STRING,
8124 gst_value_transform_g_value_array_string);
8125 g_value_register_transform_func (G_TYPE_VALUE_ARRAY, GST_TYPE_ARRAY,
8126 gst_value_transform_g_value_array_any_list);
8127 g_value_register_transform_func (G_TYPE_VALUE_ARRAY, GST_TYPE_LIST,
8128 gst_value_transform_g_value_array_any_list);
8129 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
8130 gst_value_transform_fraction_string);
8131 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
8132 gst_value_transform_string_fraction);
8133 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
8134 gst_value_transform_fraction_double);
8135 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
8136 gst_value_transform_fraction_float);
8137 g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
8138 gst_value_transform_double_fraction);
8139 g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
8140 gst_value_transform_float_fraction);
8141 g_value_register_transform_func (G_TYPE_DATE, G_TYPE_STRING,
8142 gst_value_transform_date_string);
8143 g_value_register_transform_func (G_TYPE_STRING, G_TYPE_DATE,
8144 gst_value_transform_string_date);
8145 g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
8146 gst_value_transform_object_string);
8147 g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_UINT64,
8148 gst_value_transform_bitmask_uint64);
8149 g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_STRING,
8150 gst_value_transform_bitmask_string);
8151 g_value_register_transform_func (G_TYPE_UINT64, GST_TYPE_BITMASK,
8152 gst_value_transform_uint64_bitmask);
8153 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_BITMASK,
8154 gst_value_transform_string_bitmask);
8155
8156 g_value_register_transform_func (GST_TYPE_FLAG_SET, G_TYPE_STRING,
8157 gst_value_transform_flagset_string);
8158 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FLAG_SET,
8159 gst_value_transform_string_flagset);
8160
8161 /* Only register intersection functions for *different* types.
8162 * Identical type intersection should be specified directly in
8163 * gst_value_intersect() */
8164 gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
8165 gst_value_intersect_int_int_range);
8166 gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
8167 gst_value_intersect_int64_int64_range);
8168 gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
8169 gst_value_intersect_double_double_range);
8170 gst_value_register_intersect_func (GST_TYPE_FRACTION,
8171 GST_TYPE_FRACTION_RANGE, gst_value_intersect_fraction_fraction_range);
8172
8173 gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
8174 gst_value_subtract_int_int_range);
8175 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
8176 gst_value_subtract_int_range_int);
8177 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
8178 gst_value_subtract_int_range_int_range);
8179 gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
8180 gst_value_subtract_int64_int64_range);
8181 gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
8182 gst_value_subtract_int64_range_int64);
8183 gst_value_register_subtract_func (GST_TYPE_INT64_RANGE,
8184 GST_TYPE_INT64_RANGE, gst_value_subtract_int64_range_int64_range);
8185 gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
8186 gst_value_subtract_double_double_range);
8187 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
8188 gst_value_subtract_double_range_double);
8189 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
8190 GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
8191 gst_value_register_subtract_func (GST_TYPE_FRACTION,
8192 GST_TYPE_FRACTION_RANGE, gst_value_subtract_fraction_fraction_range);
8193 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
8194 GST_TYPE_FRACTION, gst_value_subtract_fraction_range_fraction);
8195 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
8196 GST_TYPE_FRACTION_RANGE,
8197 gst_value_subtract_fraction_range_fraction_range);
8198
8199 {
8200 GType date_type = G_TYPE_DATE;
8201
8202 g_type_name (date_type);
8203 }
8204
8205 gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
8206 gst_value_union_int_int_range);
8207 gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
8208 gst_value_union_int_range_int_range);
8209 gst_value_register_union_func (GST_TYPE_FLAG_SET, GST_TYPE_FLAG_SET,
8210 gst_value_union_flagset_flagset);
8211 gst_value_register_union_func (GST_TYPE_STRUCTURE, GST_TYPE_STRUCTURE,
8212 gst_value_union_structure_structure);
8213
8214 #if GST_VERSION_NANO == 1
8215 /* If building from git master, check starting array sizes matched actual size
8216 * so we can keep the defines in sync and save a few reallocs on startup */
8217 if (gst_value_table->len > GST_VALUE_TABLE_DEFAULT_SIZE) {
8218 GST_ERROR ("Wrong initial gst_value_table size. "
8219 "Please set GST_VALUE_TABLE_DEFAULT_SIZE to %u in gstvalue.c",
8220 gst_value_table->len);
8221 }
8222 if (gst_value_union_funcs->len > GST_VALUE_UNION_TABLE_DEFAULT_SIZE) {
8223 GST_ERROR ("Wrong initial gst_value_union_funcs table size. "
8224 "Please set GST_VALUE_UNION_TABLE_DEFAULT_SIZE to %u in gstvalue.c",
8225 gst_value_union_funcs->len);
8226 }
8227 if (gst_value_intersect_funcs->len > GST_VALUE_INTERSECT_TABLE_DEFAULT_SIZE) {
8228 GST_ERROR ("Wrong initial gst_value_intersect_funcs table size. "
8229 "Please set GST_VALUE_INTERSECT_TABLE_DEFAULT_SIZE to %u in gstvalue.c",
8230 gst_value_intersect_funcs->len);
8231 }
8232 if (gst_value_subtract_funcs->len > GST_VALUE_SUBTRACT_TABLE_DEFAULT_SIZE) {
8233 GST_ERROR ("Wrong initial gst_value_subtract_funcs table size. "
8234 "Please set GST_VALUE_SUBTRACT_TABLE_DEFAULT_SIZE to %u in gstvalue.c",
8235 gst_value_subtract_funcs->len);
8236 }
8237 #endif
8238
8239 #if 0
8240 /* Implement these if needed */
8241 gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
8242 gst_value_union_fraction_fraction_range);
8243 gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
8244 GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);
8245 #endif
8246 }
8247
8248 static void
gst_flagset_class_init(gpointer g_class,gpointer class_data)8249 gst_flagset_class_init (gpointer g_class, gpointer class_data)
8250 {
8251 GstFlagSetClass *f_class = (GstFlagSetClass *) (g_class);
8252 f_class->flags_type = (GType) GPOINTER_TO_SIZE (class_data);
8253 }
8254
8255 /**
8256 * gst_flagset_register:
8257 * @flags_type: a #GType of a #G_TYPE_FLAGS type.
8258 *
8259 * Create a new sub-class of #GST_TYPE_FLAG_SET
8260 * which will pretty-print the human-readable flags
8261 * when serializing, for easier debugging.
8262 *
8263 * Since: 1.6
8264 */
8265 GType
gst_flagset_register(GType flags_type)8266 gst_flagset_register (GType flags_type)
8267 {
8268 GTypeInfo info = {
8269 sizeof (GstFlagSetClass),
8270 NULL, NULL,
8271 (GClassInitFunc) gst_flagset_class_init,
8272 NULL, GSIZE_TO_POINTER (flags_type), 0, 0, NULL, NULL
8273 };
8274 GType t;
8275 gchar *class_name;
8276
8277 g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), 0);
8278
8279 class_name = g_strdup_printf ("%sSet", g_type_name (flags_type));
8280
8281 t = g_type_register_static (GST_TYPE_FLAG_SET,
8282 g_intern_string (class_name), &info, 0);
8283 g_free (class_name);
8284
8285 return t;
8286 }
8287