gstvalue.c 113 KB
Newer Older
David Schleef's avatar
David Schleef committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* GStreamer
 * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
19

20
21
22
23
/**
 * SECTION:gstvalue
 * @short_description: GValue implementations specific to GStreamer
 *
Wim Taymans's avatar
Wim Taymans committed
24
25
 * GValue implementations specific to GStreamer.
 *
Wim Taymans's avatar
Wim Taymans committed
26
27
28
29
 * Note that operations on the same GstValue (or GValue) from multiple
 * threads may lead to undefined behaviour. 
 *
 * Last reviewed on 2006-03-07 (0.10.4)
30
 */
David Schleef's avatar
David Schleef committed
31
32
33
34

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
35
#include <math.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <ctype.h>
David Schleef's avatar
David Schleef committed
39

40
#include "gst_private.h"
41
#include "glib-compat-private.h"
David Schleef's avatar
David Schleef committed
42
43
#include <gst/gst.h>
#include <gobject/gvaluecollector.h>
44
45

typedef struct _GstValueUnionInfo GstValueUnionInfo;
46
47
struct _GstValueUnionInfo
{
48
49
50
51
52
53
  GType type1;
  GType type2;
  GstValueUnionFunc func;
};

typedef struct _GstValueIntersectInfo GstValueIntersectInfo;
54
55
struct _GstValueIntersectInfo
{
56
57
58
59
  GType type1;
  GType type2;
  GstValueIntersectFunc func;
};
David Schleef's avatar
David Schleef committed
60

61
62
63
64
65
66
67
68
typedef struct _GstValueSubtractInfo GstValueSubtractInfo;
struct _GstValueSubtractInfo
{
  GType minuend;
  GType subtrahend;
  GstValueSubtractFunc func;
};

David Schleef's avatar
David Schleef committed
69
GType gst_type_double_range;
70
GType gst_type_fraction_range;
71
GType gst_type_list;
72
GType gst_type_array;
73
GType gst_type_fraction;
74
GType gst_type_date;
David Schleef's avatar
David Schleef committed
75

76
77
78
static GArray *gst_value_table;
static GArray *gst_value_union_funcs;
static GArray *gst_value_intersect_funcs;
79
static GArray *gst_value_subtract_funcs;
80

81
82
/* Forward declarations */
static gint gst_greatest_common_divisor (gint a, gint b);
83
static gchar *gst_value_serialize_fraction (const GValue * value);
84

85
86
87
88
static GstValueCompareFunc gst_value_get_compare_func (const GValue * value1);
static gint gst_value_compare_with_func (const GValue * value1,
    const GValue * value2, GstValueCompareFunc compare);

89
90
91
/********
 * list *
 ********/
92
93

/* two helper functions to serialize/stringify any type of list
94
 * regular lists are done with { }, arrays with < >
95
 */
96
97
98
static gchar *
gst_value_serialize_any_list (const GValue * value, const gchar * begin,
    const gchar * end)
99
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
100
  guint i;
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  GArray *array = value->data[0].v_pointer;
  GString *s;
  GValue *v;
  gchar *s_val;

  s = g_string_new (begin);
  for (i = 0; i < array->len; i++) {
    v = &g_array_index (array, GValue, i);
    s_val = gst_value_serialize (v);
    g_string_append (s, s_val);
    g_free (s_val);
    if (i < array->len - 1) {
      g_string_append (s, ", ");
    }
  }
  g_string_append (s, end);
  return g_string_free (s, FALSE);
}
Benjamin Otte's avatar
Benjamin Otte committed
119

120
121
static void
gst_value_transform_any_list_string (const GValue * src_value,
122
    GValue * dest_value, const gchar * begin, const gchar * end)
123
124
125
126
{
  GValue *list_value;
  GArray *array;
  GString *s;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
127
  guint i;
128
  gchar *list_s;
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

  array = src_value->data[0].v_pointer;

  s = g_string_new (begin);
  for (i = 0; i < array->len; i++) {
    list_value = &g_array_index (array, GValue, i);

    if (i != 0) {
      g_string_append (s, ", ");
    }
    list_s = g_strdup_value_contents (list_value);
    g_string_append (s, list_s);
    g_free (list_s);
  }
  g_string_append (s, end);

  dest_value->data[0].v_pointer = g_string_free (s, FALSE);
}

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * helper function to see if a type is fixed. Is used internally here and
 * there. Do not export, since it doesn't work for types where the content
 * decides the fixedness (e.g. GST_TYPE_ARRAY).
 */

static gboolean
gst_type_is_fixed (GType type)
{
  if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
      type == GST_TYPE_LIST) {
    return FALSE;
  }
  if (G_TYPE_FUNDAMENTAL (type) <=
      G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
    return TRUE;
  }
  if (type == GST_TYPE_BUFFER || type == GST_TYPE_FOURCC
      || type == GST_TYPE_ARRAY || type == GST_TYPE_FRACTION) {
    return TRUE;
  }

  return FALSE;
}

/* GValue functions usable for both regular lists and arrays */
Benjamin Otte's avatar
Benjamin Otte committed
174
static void
175
gst_value_init_list_or_array (GValue * value)
Benjamin Otte's avatar
Benjamin Otte committed
176
{
177
  value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
Benjamin Otte's avatar
Benjamin Otte committed
178
179
180
}

static GArray *
181
copy_garray_of_gstvalue (const GArray * src)
Benjamin Otte's avatar
Benjamin Otte committed
182
183
{
  GArray *dest;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
184
  guint i;
185
186

  dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), src->len);
Benjamin Otte's avatar
Benjamin Otte committed
187
188
  g_array_set_size (dest, src->len);
  for (i = 0; i < src->len; i++) {
189
    gst_value_init_and_copy (&g_array_index (dest, GValue, i),
190
        &g_array_index (src, GValue, i));
Benjamin Otte's avatar
Benjamin Otte committed
191
192
193
194
195
196
  }

  return dest;
}

static void
197
gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
Benjamin Otte's avatar
Benjamin Otte committed
198
{
199
  dest_value->data[0].v_pointer =
200
      copy_garray_of_gstvalue ((GArray *) src_value->data[0].v_pointer);
Benjamin Otte's avatar
Benjamin Otte committed
201
202
203
}

static void
204
gst_value_free_list_or_array (GValue * value)
Benjamin Otte's avatar
Benjamin Otte committed
205
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
206
  guint i;
Benjamin Otte's avatar
Benjamin Otte committed
207
  GArray *src = (GArray *) value->data[0].v_pointer;
208

Benjamin Otte's avatar
Benjamin Otte committed
209
210
  if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
    for (i = 0; i < src->len; i++) {
211
      g_value_unset (&g_array_index (src, GValue, i));
Benjamin Otte's avatar
Benjamin Otte committed
212
213
214
215
216
217
    }
    g_array_free (src, TRUE);
  }
}

static gpointer
218
gst_value_list_or_array_peek_pointer (const GValue * value)
Benjamin Otte's avatar
Benjamin Otte committed
219
220
221
222
223
{
  return value->data[0].v_pointer;
}

static gchar *
224
gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
225
    GTypeCValue * collect_values, guint collect_flags)
Benjamin Otte's avatar
Benjamin Otte committed
226
227
228
229
230
{
  if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
    value->data[0].v_pointer = collect_values[0].v_pointer;
    value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
  } else {
231
    value->data[0].v_pointer =
232
        copy_garray_of_gstvalue ((GArray *) collect_values[0].v_pointer);
Benjamin Otte's avatar
Benjamin Otte committed
233
234
235
236
237
  }
  return NULL;
}

static gchar *
238
gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
239
    GTypeCValue * collect_values, guint collect_flags)
Benjamin Otte's avatar
Benjamin Otte committed
240
241
{
  GArray **dest = collect_values[0].v_pointer;
242

Benjamin Otte's avatar
Benjamin Otte committed
243
244
  if (!dest)
    return g_strdup_printf ("value location for `%s' passed as NULL",
245
        G_VALUE_TYPE_NAME (value));
246
  if (!value->data[0].v_pointer)
Benjamin Otte's avatar
Benjamin Otte committed
247
    return g_strdup_printf ("invalid value given for `%s'",
248
        G_VALUE_TYPE_NAME (value));
Benjamin Otte's avatar
Benjamin Otte committed
249
250
251
  if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
    *dest = (GArray *) value->data[0].v_pointer;
  } else {
252
    *dest = copy_garray_of_gstvalue ((GArray *) value->data[0].v_pointer);
Benjamin Otte's avatar
Benjamin Otte committed
253
254
255
256
  }
  return NULL;
}

257
258
/**
 * gst_value_list_append_value:
259
 * @value: a #GValue of type #GST_TYPE_LIST
260
 * @append_value: the value to append
261
 *
262
 * Appends @append_value to the GstValueList in @value.
263
 */
264
265
void
gst_value_list_append_value (GValue * value, const GValue * append_value)
Benjamin Otte's avatar
Benjamin Otte committed
266
{
267
268
  GValue val = { 0, };

269
  g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
Benjamin Otte's avatar
Benjamin Otte committed
270

271
272
  gst_value_init_and_copy (&val, append_value);
  g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
Benjamin Otte's avatar
Benjamin Otte committed
273
274
}

275
/**
276
277
278
 * gst_value_list_prepend_value:
 * @value: a #GValue of type #GST_TYPE_LIST
 * @prepend_value: the value to prepend
279
 *
280
 * Prepends @prepend_value to the GstValueList in @value.
281
 */
282
283
void
gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
Benjamin Otte's avatar
Benjamin Otte committed
284
{
285
  GValue val = { 0, };
Benjamin Otte's avatar
Benjamin Otte committed
286

287
  g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
Benjamin Otte's avatar
Benjamin Otte committed
288

289
290
  gst_value_init_and_copy (&val, prepend_value);
  g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
Benjamin Otte's avatar
Benjamin Otte committed
291
292
293
294
295
}

/**
 * gst_value_list_concat:
 * @dest: an uninitialized #GValue to take the result
296
 * @value1: a #GValue
Wim Taymans's avatar
Wim Taymans committed
297
 * @value2: a #GValue
Benjamin Otte's avatar
Benjamin Otte committed
298
 *
299
300
301
 * Concatenates copies of @value1 and @value2 into a list.  Values that are not
 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
 * @dest will be initialized to the type #GST_TYPE_LIST.
Benjamin Otte's avatar
Benjamin Otte committed
302
303
 */
void
304
305
gst_value_list_concat (GValue * dest, const GValue * value1,
    const GValue * value2)
Benjamin Otte's avatar
Benjamin Otte committed
306
307
308
309
310
311
312
313
{
  guint i, value1_length, value2_length;
  GArray *array;

  g_return_if_fail (dest != NULL);
  g_return_if_fail (G_VALUE_TYPE (dest) == 0);
  g_return_if_fail (G_IS_VALUE (value1));
  g_return_if_fail (G_IS_VALUE (value2));
314
315
316
317
318

  value1_length =
      (GST_VALUE_HOLDS_LIST (value1) ? gst_value_list_get_size (value1) : 1);
  value2_length =
      (GST_VALUE_HOLDS_LIST (value2) ? gst_value_list_get_size (value2) : 1);
319
  g_value_init (dest, GST_TYPE_LIST);
Benjamin Otte's avatar
Benjamin Otte committed
320
321
  array = (GArray *) dest->data[0].v_pointer;
  g_array_set_size (array, value1_length + value2_length);
322

Benjamin Otte's avatar
Benjamin Otte committed
323
324
  if (GST_VALUE_HOLDS_LIST (value1)) {
    for (i = 0; i < value1_length; i++) {
325
      gst_value_init_and_copy (&g_array_index (array, GValue, i),
326
          gst_value_list_get_value (value1, i));
Benjamin Otte's avatar
Benjamin Otte committed
327
328
    }
  } else {
329
    gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
Benjamin Otte's avatar
Benjamin Otte committed
330
  }
331

Benjamin Otte's avatar
Benjamin Otte committed
332
333
  if (GST_VALUE_HOLDS_LIST (value2)) {
    for (i = 0; i < value2_length; i++) {
334
      gst_value_init_and_copy (&g_array_index (array, GValue,
335
              i + value1_length), gst_value_list_get_value (value2, i));
Benjamin Otte's avatar
Benjamin Otte committed
336
337
    }
  } else {
338
    gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
339
        value2);
Benjamin Otte's avatar
Benjamin Otte committed
340
341
342
  }
}

343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**
 * gst_value_list_get_size:
 * @value: a #GValue of type #GST_TYPE_LIST
 *
 * Gets the number of values contained in @value.
 *
 * Returns: the number of values
 */
guint
gst_value_list_get_size (const GValue * value)
{
  g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);

  return ((GArray *) value->data[0].v_pointer)->len;
}

/**
 * gst_value_list_get_value:
 * @value: a #GValue of type #GST_TYPE_LIST
 * @index: index of value to get from the list
 *
 * Gets the value that is a member of the list contained in @value and
 * has the index @index.
 *
 * Returns: the value at the given index
 */
const GValue *
gst_value_list_get_value (const GValue * value, guint index)
{
  g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
  g_return_val_if_fail (index < gst_value_list_get_size (value), NULL);

  return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
      GValue, index);
}

/**
 * gst_value_array_append_value:
 * @value: a #GValue of type #GST_TYPE_ARRAY
 * @append_value: the value to append
 *
 * Appends @append_value to the GstValueArray in @value.
 */
void
gst_value_array_append_value (GValue * value, const GValue * append_value)
{
  GValue val = { 0, };

  g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));

  gst_value_init_and_copy (&val, append_value);
  g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
}

/**
 * gst_value_array_prepend_value:
 * @value: a #GValue of type #GST_TYPE_ARRAY
 * @prepend_value: the value to prepend
 *
 * Prepends @prepend_value to the GstValueArray in @value.
 */
void
gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
{
  GValue val = { 0, };

  g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));

  gst_value_init_and_copy (&val, prepend_value);
  g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
}

/**
 * gst_value_array_get_size:
 * @value: a #GValue of type #GST_TYPE_ARRAY
 *
 * Gets the number of values contained in @value.
 *
 * Returns: the number of values
 */
guint
gst_value_array_get_size (const GValue * value)
{
  g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);

  return ((GArray *) value->data[0].v_pointer)->len;
}

/**
 * gst_value_array_get_value:
 * @value: a #GValue of type #GST_TYPE_ARRAY
 * @index: index of value to get from the array
 *
 * Gets the value that is a member of the array contained in @value and
 * has the index @index.
 *
 * Returns: the value at the given index
 */
const GValue *
gst_value_array_get_value (const GValue * value, guint index)
{
  g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
  g_return_val_if_fail (index < gst_value_array_get_size (value), NULL);

  return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
      GValue, index);
}

451
static void
452
gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
453
{
454
455
  gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
}
456

457
static void
458
gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
459
460
{
  gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
461
462
}

463
/* Do an unordered compare of the contents of a list */
464
static int
465
gst_value_compare_list (const GValue * value1, const GValue * value2)
466
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
467
  guint i, j;
468
469
470
471
  GArray *array1 = value1->data[0].v_pointer;
  GArray *array2 = value2->data[0].v_pointer;
  GValue *v1;
  GValue *v2;
472
473
  gint len, to_remove;
  guint8 *removed;
474
  GstValueCompareFunc compare;
475

476
477
478
  /* get length and do initial length check. */
  len = array1->len;
  if (len != array2->len)
479
    return GST_VALUE_UNORDERED;
480

481
482
483
484
485
486
487
488
  /* place to mark removed value indices of array2 */
  removed = g_newa (guint8, len);
  memset (removed, 0, len);
  to_remove = len;

  /* loop over array1, all items should be in array2. When we find an
   * item in array2, remove it from array2 by marking it as removed */
  for (i = 0; i < len; i++) {
489
    v1 = &g_array_index (array1, GValue, i);
490
491
492
493
494
495
496
497
498
499
500
501
502
    if ((compare = gst_value_get_compare_func (v1))) {
      for (j = 0; j < len; j++) {
        /* item is removed, we can skip it */
        if (removed[j])
          continue;
        v2 = &g_array_index (array2, GValue, j);
        if (gst_value_compare_with_func (v1, v2, compare) == GST_VALUE_EQUAL) {
          /* mark item as removed now that we found it in array2 and 
           * decrement the number of remaining items in array2. */
          removed[j] = 1;
          to_remove--;
          break;
        }
503
      }
504
505
506
507
      /* item in array1 and not in array2, UNORDERED */
      if (j == len)
        return GST_VALUE_UNORDERED;
    } else
508
509
      return GST_VALUE_UNORDERED;
  }
510
511
512
  /* if not all items were removed, array2 contained something not in array1 */
  if (to_remove != 0)
    return GST_VALUE_UNORDERED;
513

514
  /* arrays are equal */
515
516
517
  return GST_VALUE_EQUAL;
}

518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/* Perform an ordered comparison of the contents of an array */
static int
gst_value_compare_array (const GValue * value1, const GValue * value2)
{
  guint i;
  GArray *array1 = value1->data[0].v_pointer;
  GArray *array2 = value2->data[0].v_pointer;
  GValue *v1;
  GValue *v2;

  if (array1->len != array2->len)
    return GST_VALUE_UNORDERED;

  for (i = 0; i < array1->len; i++) {
    v1 = &g_array_index (array1, GValue, i);
    v2 = &g_array_index (array2, GValue, i);
    if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
      return GST_VALUE_UNORDERED;
  }

  return GST_VALUE_EQUAL;
}

541
static gchar *
542
gst_value_serialize_list (const GValue * value)
543
{
544
  return gst_value_serialize_any_list (value, "{ ", " }");
545
546
547
}

static gboolean
548
gst_value_deserialize_list (GValue * dest, const gchar * s)
549
{
550
  g_warning ("gst_value_deserialize_list: unimplemented");
551
552
553
  return FALSE;
}

554
static gchar *
555
gst_value_serialize_array (const GValue * value)
556
557
558
559
560
{
  return gst_value_serialize_any_list (value, "< ", " >");
}

static gboolean
561
gst_value_deserialize_array (GValue * dest, const gchar * s)
562
{
563
  g_warning ("gst_value_deserialize_array: unimplemented");
564
565
566
  return FALSE;
}

567
568
569
/**********
 * fourcc *
 **********/
570

571
572
static void
gst_value_init_fourcc (GValue * value)
David Schleef's avatar
David Schleef committed
573
{
David Schleef's avatar
David Schleef committed
574
  value->data[0].v_int = 0;
David Schleef's avatar
David Schleef committed
575
576
577
}

static void
578
gst_value_copy_fourcc (const GValue * src_value, GValue * dest_value)
David Schleef's avatar
David Schleef committed
579
{
David Schleef's avatar
David Schleef committed
580
  dest_value->data[0].v_int = src_value->data[0].v_int;
David Schleef's avatar
David Schleef committed
581
582
583
}

static gchar *
584
585
gst_value_collect_fourcc (GValue * value, guint n_collect_values,
    GTypeCValue * collect_values, guint collect_flags)
David Schleef's avatar
David Schleef committed
586
{
David Schleef's avatar
David Schleef committed
587
  value->data[0].v_int = collect_values[0].v_int;
David Schleef's avatar
David Schleef committed
588
589
590
591
592

  return NULL;
}

static gchar *
593
594
gst_value_lcopy_fourcc (const GValue * value, guint n_collect_values,
    GTypeCValue * collect_values, guint collect_flags)
David Schleef's avatar
David Schleef committed
595
596
597
598
599
{
  guint32 *fourcc_p = collect_values[0].v_pointer;

  if (!fourcc_p)
    return g_strdup_printf ("value location for `%s' passed as NULL",
600
        G_VALUE_TYPE_NAME (value));
David Schleef's avatar
David Schleef committed
601

David Schleef's avatar
David Schleef committed
602
  *fourcc_p = value->data[0].v_int;
David Schleef's avatar
David Schleef committed
603
604
605
606

  return NULL;
}

607
608
/**
 * gst_value_set_fourcc:
609
610
 * @value: a GValue initialized to #GST_TYPE_FOURCC
 * @fourcc: the #guint32 fourcc to set
611
 *
612
 * Sets @value to @fourcc.
613
 */
David Schleef's avatar
David Schleef committed
614
void
615
gst_value_set_fourcc (GValue * value, guint32 fourcc)
David Schleef's avatar
David Schleef committed
616
617
618
{
  g_return_if_fail (GST_VALUE_HOLDS_FOURCC (value));

David Schleef's avatar
David Schleef committed
619
  value->data[0].v_int = fourcc;
David Schleef's avatar
David Schleef committed
620
621
}

622
623
/**
 * gst_value_get_fourcc:
624
 * @value: a GValue initialized to #GST_TYPE_FOURCC
625
 *
626
 * Gets the #guint32 fourcc contained in @value.
627
 *
628
 * Returns: the #guint32 fourcc contained in @value.
629
 */
David Schleef's avatar
David Schleef committed
630
guint32
631
gst_value_get_fourcc (const GValue * value)
David Schleef's avatar
David Schleef committed
632
633
634
{
  g_return_val_if_fail (GST_VALUE_HOLDS_FOURCC (value), 0);

David Schleef's avatar
David Schleef committed
635
  return value->data[0].v_int;
David Schleef's avatar
David Schleef committed
636
637
}

638
static void
639
640
gst_value_transform_fourcc_string (const GValue * src_value,
    GValue * dest_value)
641
642
643
{
  guint32 fourcc = src_value->data[0].v_int;

644
645
646
647
648
  if (g_ascii_isprint ((fourcc >> 0) & 0xff) &&
      g_ascii_isprint ((fourcc >> 8) & 0xff) &&
      g_ascii_isprint ((fourcc >> 16) & 0xff) &&
      g_ascii_isprint ((fourcc >> 24) & 0xff)) {
    dest_value->data[0].v_pointer =
649
        g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
650
  } else {
651
    dest_value->data[0].v_pointer = g_strdup_printf ("0x%08x", fourcc);
652
653
654
  }
}

655
static gint
656
gst_value_compare_fourcc (const GValue * value1, const GValue * value2)
657
{
658
659
  if (value2->data[0].v_int == value1->data[0].v_int)
    return GST_VALUE_EQUAL;
660
661
662
  return GST_VALUE_UNORDERED;
}

663
static gchar *
664
gst_value_serialize_fourcc (const GValue * value)
665
{
666
667
  guint32 fourcc = value->data[0].v_int;

668
669
670
671
  if (g_ascii_isalnum ((fourcc >> 0) & 0xff) &&
      g_ascii_isalnum ((fourcc >> 8) & 0xff) &&
      g_ascii_isalnum ((fourcc >> 16) & 0xff) &&
      g_ascii_isalnum ((fourcc >> 24) & 0xff)) {
672
    return g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
673
  } else {
674
    return g_strdup_printf ("0x%08x", fourcc);
675
  }
676
677
678
}

static gboolean
679
gst_value_deserialize_fourcc (GValue * dest, const char *s)
680
{
681
682
683
684
  gboolean ret = FALSE;
  guint32 fourcc = 0;
  char *end;

685
686
  if (strlen (s) == 4) {
    fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
687
688
689
690
691
692
693
694
695
696
    ret = TRUE;
  } else if (g_ascii_isdigit (*s)) {
    fourcc = strtoul (s, &end, 0);
    if (*end == 0) {
      ret = TRUE;
    }
  }
  gst_value_set_fourcc (dest, fourcc);

  return ret;
697
698
}

699
700
701
/*************
 * int range *
 *************/
David Schleef's avatar
David Schleef committed
702

703
704
static void
gst_value_init_int_range (GValue * value)
David Schleef's avatar
David Schleef committed
705
{
David Schleef's avatar
David Schleef committed
706
707
  value->data[0].v_int = 0;
  value->data[1].v_int = 0;
David Schleef's avatar
David Schleef committed
708
709
710
}

static void
711
gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
David Schleef's avatar
David Schleef committed
712
{
David Schleef's avatar
David Schleef committed
713
714
  dest_value->data[0].v_int = src_value->data[0].v_int;
  dest_value->data[1].v_int = src_value->data[1].v_int;
David Schleef's avatar
David Schleef committed
715
716
717
}

static gchar *
718
719
gst_value_collect_int_range (GValue * value, guint n_collect_values,
    GTypeCValue * collect_values, guint collect_flags)
David Schleef's avatar
David Schleef committed
720
{
David Schleef's avatar
David Schleef committed
721
722
  value->data[0].v_int = collect_values[0].v_int;
  value->data[1].v_int = collect_values[1].v_int;
David Schleef's avatar
David Schleef committed
723
724
725
726
727

  return NULL;
}

static gchar *
728
729
gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
    GTypeCValue * collect_values, guint collect_flags)
David Schleef's avatar
David Schleef committed
730
{
David Schleef's avatar
David Schleef committed
731
732
  guint32 *int_range_start = collect_values[0].v_pointer;
  guint32 *int_range_end = collect_values[1].v_pointer;
David Schleef's avatar
David Schleef committed
733

David Schleef's avatar
David Schleef committed
734
735
  if (!int_range_start)
    return g_strdup_printf ("start value location for `%s' passed as NULL",
736
        G_VALUE_TYPE_NAME (value));
David Schleef's avatar
David Schleef committed
737
738
  if (!int_range_end)
    return g_strdup_printf ("end value location for `%s' passed as NULL",
739
        G_VALUE_TYPE_NAME (value));
David Schleef's avatar
David Schleef committed
740

David Schleef's avatar
David Schleef committed
741
742
  *int_range_start = value->data[0].v_int;
  *int_range_end = value->data[1].v_int;
David Schleef's avatar
David Schleef committed
743
744
745
746

  return NULL;
}

747
748
/**
 * gst_value_set_int_range:
749
750
751
 * @value: a GValue initialized to GST_TYPE_INT_RANGE
 * @start: the start of the range
 * @end: the end of the range
752
 *
753
 * Sets @value to the range specified by @start and @end.
754
 */
David Schleef's avatar
David Schleef committed
755
void
756
gst_value_set_int_range (GValue * value, gint start, gint end)
David Schleef's avatar
David Schleef committed
757
{
Benjamin Otte's avatar
Benjamin Otte committed
758
  g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
759
  g_return_if_fail (start < end);
David Schleef's avatar
David Schleef committed
760

David Schleef's avatar
David Schleef committed
761
762
  value->data[0].v_int = start;
  value->data[1].v_int = end;
David Schleef's avatar
David Schleef committed
763
764
}

765
766
/**
 * gst_value_get_int_range_min:
767
 * @value: a GValue initialized to GST_TYPE_INT_RANGE
768
 *
769
770
 * Gets the minimum of the range specified by @value.
 *
771
 * Returns: the minimum of the range
772
 */
773
gint
774
gst_value_get_int_range_min (const GValue * value)
David Schleef's avatar
David Schleef committed
775
{
Benjamin Otte's avatar
Benjamin Otte committed
776
  g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
David Schleef's avatar
David Schleef committed
777

David Schleef's avatar
David Schleef committed
778
  return value->data[0].v_int;
David Schleef's avatar
David Schleef committed
779
780
}

781
782
/**
 * gst_value_get_int_range_max:
783
784
785
 * @value: a GValue initialized to GST_TYPE_INT_RANGE
 *
 * Gets the maximum of the range specified by @value.
786
 *
787
 * Returns: the maxumum of the range
788
 */
789
gint
790
gst_value_get_int_range_max (const GValue * value)
David Schleef's avatar
David Schleef committed
791
{
Benjamin Otte's avatar
Benjamin Otte committed
792
  g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
David Schleef's avatar
David Schleef committed
793

David Schleef's avatar
David Schleef committed
794
  return value->data[1].v_int;
David Schleef's avatar
David Schleef committed
795
796
}

797
static void
798
799
gst_value_transform_int_range_string (const GValue * src_value,
    GValue * dest_value)
800
{
801
802
  dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
      (int) src_value->data[0].v_int, (int) src_value->data[1].v_int);
803
804
}

805
static gint
806
gst_value_compare_int_range (const GValue * value1, const GValue * value2)
807
808
{
  if (value2->data[0].v_int == value1->data[0].v_int &&
809
      value2->data[1].v_int == value1->data[1].v_int)
810
    return GST_VALUE_EQUAL;
811
812
813
  return GST_VALUE_UNORDERED;
}

814
static gchar *
815
gst_value_serialize_int_range (const GValue * value)
816
{
817
818
  return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
      value->data[1].v_int);
819
820
821
}

static gboolean
822
gst_value_deserialize_int_range (GValue * dest, const gchar * s)
823
{
824
  g_warning ("unimplemented");
825
826
827
  return FALSE;
}

828
829
830
/****************
 * double range *
 ****************/
Benjamin Otte's avatar
Benjamin Otte committed
831

832
833
static void
gst_value_init_double_range (GValue * value)
Benjamin Otte's avatar
Benjamin Otte committed
834
835
836
837
838
839
{
  value->data[0].v_double = 0;
  value->data[1].v_double = 0;
}

static void
840
gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
Benjamin Otte's avatar
Benjamin Otte committed
841
842
843
844
845
846
{
  dest_value->data[0].v_double = src_value->data[0].v_double;
  dest_value->data[1].v_double = src_value->data[1].v_double;
}

static gchar *
847
848
gst_value_collect_double_range (GValue * value, guint n_collect_values,
    GTypeCValue * collect_values, guint collect_flags)
Benjamin Otte's avatar
Benjamin Otte committed
849
850
851
852
853
854
855
856
{
  value->data[0].v_double = collect_values[0].v_double;
  value->data[1].v_double = collect_values[1].v_double;

  return NULL;
}

static gchar *
857
858
gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
    GTypeCValue * collect_values, guint collect_flags)
Benjamin Otte's avatar
Benjamin Otte committed
859
{
David Schleef's avatar
David Schleef committed
860
861
  gdouble *double_range_start = collect_values[0].v_pointer;
  gdouble *double_range_end = collect_values[1].v_pointer;
Benjamin Otte's avatar
Benjamin Otte committed
862

David Schleef's avatar
David Schleef committed
863
864
  if (!double_range_start)
    return g_strdup_printf ("start value location for `%s' passed as NULL",
865
        G_VALUE_TYPE_NAME (value));
David Schleef's avatar
David Schleef committed
866
867
  if (!double_range_end)
    return g_strdup_printf ("end value location for `%s' passed as NULL",
868
        G_VALUE_TYPE_NAME (value));
Benjamin Otte's avatar
Benjamin Otte committed
869

David Schleef's avatar
David Schleef committed
870
871
  *double_range_start = value->data[0].v_double;
  *double_range_end = value->data[1].v_double;
Benjamin Otte's avatar
Benjamin Otte committed
872
873
874
875

  return NULL;
}

876
877
/**
 * gst_value_set_double_range:
878
879
880
 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
 * @start: the start of the range
 * @end: the end of the range
881
 *
882
 * Sets @value to the range specified by @start and @end.
883
 */
Benjamin Otte's avatar
Benjamin Otte committed
884
void
885
gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
Benjamin Otte's avatar
Benjamin Otte committed
886
887
888
889
890
891
892
{
  g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));

  value->data[0].v_double = start;
  value->data[1].v_double = end;
}

893
894
/**
 * gst_value_get_double_range_min:
895
 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
896
 *
897
898
899
 * Gets the minimum of the range specified by @value.
 *
 * Returns: the minumum of the range
900
 */
901
gdouble
902
gst_value_get_double_range_min (const GValue * value)
Benjamin Otte's avatar
Benjamin Otte committed
903
904
905
906
907
908
{
  g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);

  return value->data[0].v_double;
}

909
910
/**
 * gst_value_get_double_range_max:
911
912
913
 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
 *
 * Gets the maximum of the range specified by @value.
914
 *
915
 * Returns: the maxumum of the range
916
 */
917
gdouble
918
gst_value_get_double_range_max (const GValue * value)
Benjamin Otte's avatar
Benjamin Otte committed
919
920
921
922
923
924
{
  g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);

  return value->data[1].v_double;
}

925
static void
926
927
gst_value_transform_double_range_string (const GValue * src_value,
    GValue * dest_value)
928
{
929
  char s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
930

931
  dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
932
      g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
933
          src_value->data[0].v_double),
934
      g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
935
          src_value->data[1].v_double));
936
937
}

938
static gint
939
gst_value_compare_double_range (const GValue * value1, const GValue * value2)
940
941
942
943
944
945
946
{
  if (value2->data[0].v_double == value1->data[0].v_double &&
      value2->data[0].v_double == value1->data[0].v_double)
    return GST_VALUE_EQUAL;
  return GST_VALUE_UNORDERED;
}

947
static gchar *
948
gst_value_serialize_double_range (const GValue * value)
949
{
950
951
  char d1[G_ASCII_DTOSTR_BUF_SIZE];
  char d2[G_ASCII_DTOSTR_BUF_SIZE];
952
953
954

  g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
  g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
955
  return g_strdup_printf ("[ %s, %s ]", d1, d2);
956
957
958
}

static gboolean
959
gst_value_deserialize_double_range (GValue * dest, const gchar * s)
960
{
961
  g_warning ("unimplemented");
962
963
964
  return FALSE;
}

965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************
 * fraction range *
 ****************/

static void
gst_value_init_fraction_range (GValue * value)
{
  GValue *vals;

  value->data[0].v_pointer = vals = g_new0 (GValue, 2);
  g_value_init (&vals[0], GST_TYPE_FRACTION);
  g_value_init (&vals[1], GST_TYPE_FRACTION);
}

static void
gst_value_free_fraction_range (GValue * value)
{
  GValue *vals = (GValue *) value->data[0].v_pointer;

  if (vals != NULL) {
    g_value_unset (&vals[0]);
    g_value_unset (&vals[1]);
    g_free (vals);
    value->data[0].v_pointer = NULL;
  }
}

static void
gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
{
  GValue *vals = (GValue *) dest_value->data[0].v_pointer;
  GValue *src_vals = (GValue *) src_value->data[0].v_pointer;

  if (vals == NULL) {
    dest_value->data[0].v_pointer = vals = g_new0 (GValue, 2);
    g_return_if_fail (vals != NULL);
For faster browsing, not all history is shown. View entire blame