bin: Use the new group-id field of the stream-start message for stream-start message...
[platform/upstream/gstreamer.git] / gst / gstutils.h
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2002 Thomas Vander Stichele <thomas@apestaart.org>
5  *
6  * gstutils.h: Header for various utility functions
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24
25 #ifndef __GST_UTILS_H__
26 #define __GST_UTILS_H__
27
28 #include <glib.h>
29 #include <gst/gstconfig.h>
30 #include <gst/gstbin.h>
31 #include <gst/gstparse.h>
32
33 G_BEGIN_DECLS
34
35 void            gst_util_set_value_from_string  (GValue *value, const gchar *value_str);
36 void            gst_util_set_object_arg         (GObject *object, const gchar *name, const gchar *value);
37 void            gst_util_dump_mem               (const guchar *mem, guint size);
38
39 guint64         gst_util_gdouble_to_guint64     (gdouble value)  G_GNUC_CONST;
40 gdouble         gst_util_guint64_to_gdouble     (guint64 value)  G_GNUC_CONST;
41
42 /**
43  * gst_guint64_to_gdouble:
44  * @value: the #guint64 value to convert
45  *
46  * Convert @value to a gdouble.
47  *
48  * Returns: @value converted to a #gdouble.
49  */
50
51 /**
52  * gst_gdouble_to_guint64:
53  * @value: the #gdouble value to convert
54  *
55  * Convert @value to a guint64.
56  *
57  * Returns: @value converted to a #guint64.
58  */
59 #ifdef WIN32
60 #define         gst_gdouble_to_guint64(value)   gst_util_gdouble_to_guint64(value)
61 #define         gst_guint64_to_gdouble(value)   gst_util_guint64_to_gdouble(value)
62 #else
63 #define         gst_gdouble_to_guint64(value)   ((guint64) (value))
64 #define         gst_guint64_to_gdouble(value)   ((gdouble) (value))
65 #endif
66
67 guint64         gst_util_uint64_scale           (guint64 val, guint64 num, guint64 denom);
68 guint64         gst_util_uint64_scale_round     (guint64 val, guint64 num, guint64 denom);
69 guint64         gst_util_uint64_scale_ceil      (guint64 val, guint64 num, guint64 denom);
70
71 guint64         gst_util_uint64_scale_int       (guint64 val, gint num, gint denom);
72 guint64         gst_util_uint64_scale_int_round (guint64 val, gint num, gint denom);
73 guint64         gst_util_uint64_scale_int_ceil  (guint64 val, gint num, gint denom);
74
75 guint32         gst_util_seqnum_next            (void);
76 gint32          gst_util_seqnum_compare         (guint32 s1, guint32 s2);
77
78 guint           gst_util_group_id_next          (void);
79
80 /**
81  * GST_CALL_PARENT:
82  * @parent_class_cast: the name of the class cast macro for the parent type
83  * @name: name of the function to call
84  * @args: arguments enclosed in '( )'
85  *
86  * Just call the parent handler.  This assumes that there is a variable
87  * named parent_class that points to the (duh!) parent class.  Note that
88  * this macro is not to be used with things that return something, use
89  * the _WITH_DEFAULT version for that
90  */
91 #define GST_CALL_PARENT(parent_class_cast, name, args)                  \
92         ((parent_class_cast(parent_class)->name != NULL) ?              \
93          parent_class_cast(parent_class)->name args : (void) 0)
94
95 /**
96  * GST_CALL_PARENT_WITH_DEFAULT:
97  * @parent_class_cast: the name of the class cast macro for the parent type
98  * @name: name of the function to call
99  * @args: arguments enclosed in '( )'
100  * @def_return: default result
101  *
102  * Same as GST_CALL_PARENT(), but in case there is no implementation, it
103  * evaluates to @def_return.
104  */
105 #define GST_CALL_PARENT_WITH_DEFAULT(parent_class_cast, name, args, def_return)\
106         ((parent_class_cast(parent_class)->name != NULL) ?              \
107          parent_class_cast(parent_class)->name args : def_return)
108
109 /* Define PUT and GET functions for unaligned memory */
110 #define _GST_GET(__data, __idx, __size, __shift) \
111     (((guint##__size) (((const guint8 *) (__data))[__idx])) << (__shift))
112
113 #define _GST_PUT(__data, __idx, __size, __shift, __num) \
114     (((guint8 *) (__data))[__idx] = (((guint##__size) (__num)) >> (__shift)) & 0xff)
115
116 #if GST_HAVE_UNALIGNED_ACCESS
117 static inline guint16 __gst_fast_read16(const guint8 *v) {
118   return *(const guint16*)(v);
119 }
120 static inline guint32 __gst_fast_read32(const guint8 *v) {
121   return *(const guint32*)(v);
122 }
123 static inline guint64 __gst_fast_read64(const guint8 *v) {
124   return *(const guint64*)(v);
125 }
126 static inline guint16 __gst_fast_read_swap16(const guint8 *v) {
127   return GUINT16_SWAP_LE_BE(*(const guint16*)(v));
128 }
129 static inline guint32 __gst_fast_read_swap32(const guint8 *v) {
130   return GUINT32_SWAP_LE_BE(*(const guint32*)(v));
131 }
132 static inline guint64 __gst_fast_read_swap64(const guint8 *v) {
133   return GUINT64_SWAP_LE_BE(*(const guint64*)(v));
134 }
135 # define _GST_FAST_READ(s, d) __gst_fast_read##s((const guint8 *)(d))
136 # define _GST_FAST_READ_SWAP(s, d) __gst_fast_read_swap##s((const guint8 *)(d))
137 #endif
138
139
140 /**
141  * GST_READ_UINT64_BE:
142  * @data: memory location
143  *
144  * Read a 64 bit unsigned integer value in big endian format from the memory buffer.
145  */
146
147 /**
148  * GST_READ_UINT64_LE:
149  * @data: memory location
150  *
151  * Read a 64 bit unsigned integer value in little endian format from the memory buffer.
152  */
153 #if GST_HAVE_UNALIGNED_ACCESS
154 # if (G_BYTE_ORDER == G_BIG_ENDIAN)
155 #  define GST_READ_UINT64_BE(data)      _GST_FAST_READ (64, data)
156 #  define GST_READ_UINT64_LE(data)      _GST_FAST_READ_SWAP (64, data)
157 # else
158 #  define GST_READ_UINT64_BE(data)      _GST_FAST_READ_SWAP (64, data)
159 #  define GST_READ_UINT64_LE(data)      _GST_FAST_READ (64, data)
160 # endif
161 #else
162 #define _GST_READ_UINT64_BE(data)       (_GST_GET (data, 0, 64, 56) | \
163                                          _GST_GET (data, 1, 64, 48) | \
164                                          _GST_GET (data, 2, 64, 40) | \
165                                          _GST_GET (data, 3, 64, 32) | \
166                                          _GST_GET (data, 4, 64, 24) | \
167                                          _GST_GET (data, 5, 64, 16) | \
168                                          _GST_GET (data, 6, 64,  8) | \
169                                          _GST_GET (data, 7, 64,  0))
170
171 #define _GST_READ_UINT64_LE(data)       (_GST_GET (data, 7, 64, 56) | \
172                                          _GST_GET (data, 6, 64, 48) | \
173                                          _GST_GET (data, 5, 64, 40) | \
174                                          _GST_GET (data, 4, 64, 32) | \
175                                          _GST_GET (data, 3, 64, 24) | \
176                                          _GST_GET (data, 2, 64, 16) | \
177                                          _GST_GET (data, 1, 64,  8) | \
178                                          _GST_GET (data, 0, 64,  0))
179
180 #define GST_READ_UINT64_BE(data) __gst_slow_read64_be((const guint8 *)(data))
181 static inline guint64 __gst_slow_read64_be (const guint8 * data) {
182   return _GST_READ_UINT64_BE (data);
183 }
184 #define GST_READ_UINT64_LE(data) __gst_slow_read64_le((const guint8 *)(data))
185 static inline guint64 __gst_slow_read64_le (const guint8 * data) {
186   return _GST_READ_UINT64_LE (data);
187 }
188 #endif
189
190 /**
191  * GST_READ_UINT32_BE:
192  * @data: memory location
193  *
194  * Read a 32 bit unsigned integer value in big endian format from the memory buffer.
195  */
196
197 /**
198  * GST_READ_UINT32_LE:
199  * @data: memory location
200  *
201  * Read a 32 bit unsigned integer value in little endian format from the memory buffer.
202  */
203 #if GST_HAVE_UNALIGNED_ACCESS
204 # if (G_BYTE_ORDER == G_BIG_ENDIAN)
205 #  define GST_READ_UINT32_BE(data)      _GST_FAST_READ (32, data)
206 #  define GST_READ_UINT32_LE(data)      _GST_FAST_READ_SWAP (32, data)
207 # else
208 #  define GST_READ_UINT32_BE(data)      _GST_FAST_READ_SWAP (32, data)
209 #  define GST_READ_UINT32_LE(data)      _GST_FAST_READ (32, data)
210 # endif
211 #else
212 #define _GST_READ_UINT32_BE(data)       (_GST_GET (data, 0, 32, 24) | \
213                                          _GST_GET (data, 1, 32, 16) | \
214                                          _GST_GET (data, 2, 32,  8) | \
215                                          _GST_GET (data, 3, 32,  0))
216
217 #define _GST_READ_UINT32_LE(data)       (_GST_GET (data, 3, 32, 24) | \
218                                          _GST_GET (data, 2, 32, 16) | \
219                                          _GST_GET (data, 1, 32,  8) | \
220                                          _GST_GET (data, 0, 32,  0))
221
222 #define GST_READ_UINT32_BE(data) __gst_slow_read32_be((const guint8 *)(data))
223 static inline guint32 __gst_slow_read32_be (const guint8 * data) {
224   return _GST_READ_UINT32_BE (data);
225 }
226 #define GST_READ_UINT32_LE(data) __gst_slow_read32_le((const guint8 *)(data))
227 static inline guint32 __gst_slow_read32_le (const guint8 * data) {
228   return _GST_READ_UINT32_LE (data);
229 }
230 #endif
231
232 /**
233  * GST_READ_UINT24_BE:
234  * @data: memory location
235  *
236  * Read a 24 bit unsigned integer value in big endian format from the memory buffer.
237  */
238 #define _GST_READ_UINT24_BE(data)       (_GST_GET (data, 0, 32, 16) | \
239                                          _GST_GET (data, 1, 32,  8) | \
240                                          _GST_GET (data, 2, 32,  0))
241
242 #define GST_READ_UINT24_BE(data) __gst_slow_read24_be((const guint8 *)(data))
243 static inline guint32 __gst_slow_read24_be (const guint8 * data) {
244   return _GST_READ_UINT24_BE (data);
245 }
246
247 /**
248  * GST_READ_UINT24_LE:
249  * @data: memory location
250  *
251  * Read a 24 bit unsigned integer value in little endian format from the memory buffer.
252  */
253 #define _GST_READ_UINT24_LE(data)       (_GST_GET (data, 2, 32, 16) | \
254                                          _GST_GET (data, 1, 32,  8) | \
255                                          _GST_GET (data, 0, 32,  0))
256
257 #define GST_READ_UINT24_LE(data) __gst_slow_read24_le((const guint8 *)(data))
258 static inline guint32 __gst_slow_read24_le (const guint8 * data) {
259   return _GST_READ_UINT24_LE (data);
260 }
261
262 /**
263  * GST_READ_UINT16_BE:
264  * @data: memory location
265  *
266  * Read a 16 bit unsigned integer value in big endian format from the memory buffer.
267  */
268 /**
269  * GST_READ_UINT16_LE:
270  * @data: memory location
271  *
272  * Read a 16 bit unsigned integer value in little endian format from the memory buffer.
273  */
274 #if GST_HAVE_UNALIGNED_ACCESS
275 # if (G_BYTE_ORDER == G_BIG_ENDIAN)
276 #  define GST_READ_UINT16_BE(data)      _GST_FAST_READ (16, data)
277 #  define GST_READ_UINT16_LE(data)      _GST_FAST_READ_SWAP (16, data)
278 # else
279 #  define GST_READ_UINT16_BE(data)      _GST_FAST_READ_SWAP (16, data)
280 #  define GST_READ_UINT16_LE(data)      _GST_FAST_READ (16, data)
281 # endif
282 #else
283 #define _GST_READ_UINT16_BE(data)       (_GST_GET (data, 0, 16,  8) | \
284                                          _GST_GET (data, 1, 16,  0))
285
286 #define _GST_READ_UINT16_LE(data)       (_GST_GET (data, 1, 16,  8) | \
287                                          _GST_GET (data, 0, 16,  0))
288
289 #define GST_READ_UINT16_BE(data) __gst_slow_read16_be((const guint8 *)(data))
290 static inline guint16 __gst_slow_read16_be (const guint8 * data) {
291   return _GST_READ_UINT16_BE (data);
292 }
293 #define GST_READ_UINT16_LE(data) __gst_slow_read16_le((const guint8 *)(data))
294 static inline guint16 __gst_slow_read16_le (const guint8 * data) {
295   return _GST_READ_UINT16_LE (data);
296 }
297 #endif
298
299 /**
300  * GST_READ_UINT8:
301  * @data: memory location
302  *
303  * Read an 8 bit unsigned integer value from the memory buffer.
304  */
305 #define GST_READ_UINT8(data)            (_GST_GET (data, 0,  8,  0))
306
307 /**
308  * GST_WRITE_UINT64_BE:
309  * @data: memory location
310  * @num: value to store
311  *
312  * Store a 64 bit unsigned integer value in big endian format into the memory buffer.
313  */
314 #define GST_WRITE_UINT64_BE(data, num)  do { \
315                                           gpointer __put_data = data; \
316                                           _GST_PUT (__put_data, 0, 64, 56, num); \
317                                           _GST_PUT (__put_data, 1, 64, 48, num); \
318                                           _GST_PUT (__put_data, 2, 64, 40, num); \
319                                           _GST_PUT (__put_data, 3, 64, 32, num); \
320                                           _GST_PUT (__put_data, 4, 64, 24, num); \
321                                           _GST_PUT (__put_data, 5, 64, 16, num); \
322                                           _GST_PUT (__put_data, 6, 64,  8, num); \
323                                           _GST_PUT (__put_data, 7, 64,  0, num); \
324                                         } while (0)
325
326 /**
327  * GST_WRITE_UINT64_LE:
328  * @data: memory location
329  * @num: value to store
330  *
331  * Store a 64 bit unsigned integer value in little endian format into the memory buffer.
332  */
333 #define GST_WRITE_UINT64_LE(data, num)  do { \
334                                           gpointer __put_data = data; \
335                                           _GST_PUT (__put_data, 0, 64,  0, num); \
336                                           _GST_PUT (__put_data, 1, 64,  8, num); \
337                                           _GST_PUT (__put_data, 2, 64, 16, num); \
338                                           _GST_PUT (__put_data, 3, 64, 24, num); \
339                                           _GST_PUT (__put_data, 4, 64, 32, num); \
340                                           _GST_PUT (__put_data, 5, 64, 40, num); \
341                                           _GST_PUT (__put_data, 6, 64, 48, num); \
342                                           _GST_PUT (__put_data, 7, 64, 56, num); \
343                                         } while (0)
344
345 /**
346  * GST_WRITE_UINT32_BE:
347  * @data: memory location
348  * @num: value to store
349  *
350  * Store a 32 bit unsigned integer value in big endian format into the memory buffer.
351  */
352 #define GST_WRITE_UINT32_BE(data, num)  do { \
353                                           gpointer __put_data = data; \
354                                           _GST_PUT (__put_data, 0, 32, 24, num); \
355                                           _GST_PUT (__put_data, 1, 32, 16, num); \
356                                           _GST_PUT (__put_data, 2, 32,  8, num); \
357                                           _GST_PUT (__put_data, 3, 32,  0, num); \
358                                         } while (0)
359
360 /**
361  * GST_WRITE_UINT32_LE:
362  * @data: memory location
363  * @num: value to store
364  *
365  * Store a 32 bit unsigned integer value in little endian format into the memory buffer.
366  */
367 #define GST_WRITE_UINT32_LE(data, num)  do { \
368                                           gpointer __put_data = data; \
369                                           _GST_PUT (__put_data, 0, 32,  0, num); \
370                                           _GST_PUT (__put_data, 1, 32,  8, num); \
371                                           _GST_PUT (__put_data, 2, 32, 16, num); \
372                                           _GST_PUT (__put_data, 3, 32, 24, num); \
373                                         } while (0)
374
375 /**
376  * GST_WRITE_UINT24_BE:
377  * @data: memory location
378  * @num: value to store
379  *
380  * Store a 24 bit unsigned integer value in big endian format into the memory buffer.
381  */
382 #define GST_WRITE_UINT24_BE(data, num)  do { \
383                                           gpointer __put_data = data; \
384                                           _GST_PUT (__put_data, 0, 32,  16, num); \
385                                           _GST_PUT (__put_data, 1, 32,  8, num); \
386                                           _GST_PUT (__put_data, 2, 32,  0, num); \
387                                         } while (0)
388
389 /**
390  * GST_WRITE_UINT24_LE:
391  * @data: memory location
392  * @num: value to store
393  *
394  * Store a 24 bit unsigned integer value in little endian format into the memory buffer.
395  */
396 #define GST_WRITE_UINT24_LE(data, num)  do { \
397                                           gpointer __put_data = data; \
398                                           _GST_PUT (__put_data, 0, 32,  0, num); \
399                                           _GST_PUT (__put_data, 1, 32,  8, num); \
400                                           _GST_PUT (__put_data, 2, 32,  16, num); \
401                                         } while (0)
402
403 /**
404  * GST_WRITE_UINT16_BE:
405  * @data: memory location
406  * @num: value to store
407  *
408  * Store a 16 bit unsigned integer value in big endian format into the memory buffer.
409  */
410 #define GST_WRITE_UINT16_BE(data, num)  do { \
411                                           gpointer __put_data = data; \
412                                           _GST_PUT (__put_data, 0, 16,  8, num); \
413                                           _GST_PUT (__put_data, 1, 16,  0, num); \
414                                         } while (0)
415
416 /**
417  * GST_WRITE_UINT16_LE:
418  * @data: memory location
419  * @num: value to store
420  *
421  * Store a 16 bit unsigned integer value in little endian format into the memory buffer.
422  */
423 #define GST_WRITE_UINT16_LE(data, num)  do { \
424                                           gpointer __put_data = data; \
425                                           _GST_PUT (__put_data, 0, 16,  0, num); \
426                                           _GST_PUT (__put_data, 1, 16,  8, num); \
427                                         } while (0)
428
429 /**
430  * GST_WRITE_UINT8:
431  * @data: memory location
432  * @num: value to store
433  *
434  * Store an 8 bit unsigned integer value into the memory buffer.
435  */
436 #define GST_WRITE_UINT8(data, num)      do { \
437                                           _GST_PUT (data, 0,  8,  0, num); \
438                                         } while (0)
439
440 /* Float endianness conversion macros */
441
442 /* FIXME: Remove this once we depend on a GLib version with this */
443 #ifndef GFLOAT_FROM_LE
444 /**
445  * GFLOAT_SWAP_LE_BE:
446  * @in: input value
447  *
448  * Swap byte order of a 32-bit floating point value (float).
449  *
450  * Returns: @in byte-swapped.
451  */
452 #ifdef _FOOL_GTK_DOC_
453 G_INLINE_FUNC gfloat GFLOAT_SWAP_LE_BE (gfloat in);
454 #endif
455
456 inline static gfloat
457 GFLOAT_SWAP_LE_BE(gfloat in)
458 {
459   union
460   {
461     guint32 i;
462     gfloat f;
463   } u;
464
465   u.f = in;
466   u.i = GUINT32_SWAP_LE_BE (u.i);
467   return u.f;
468 }
469
470 /**
471  * GDOUBLE_SWAP_LE_BE:
472  * @in: input value
473  *
474  * Swap byte order of a 64-bit floating point value (double).
475  *
476  * Returns: @in byte-swapped.
477  */
478 #ifdef _FOOL_GTK_DOC_
479 G_INLINE_FUNC gdouble GDOUBLE_SWAP_LE_BE (gdouble in);
480 #endif
481
482 inline static gdouble
483 GDOUBLE_SWAP_LE_BE(gdouble in)
484 {
485   union
486   {
487     guint64 i;
488     gdouble d;
489   } u;
490
491   u.d = in;
492   u.i = GUINT64_SWAP_LE_BE (u.i);
493   return u.d;
494 }
495
496 /**
497  * GDOUBLE_TO_LE:
498  * @val: value
499  *
500  * Convert 64-bit floating point value (double) from native byte order into
501  * little endian byte order.
502  */
503 /**
504  * GDOUBLE_TO_BE:
505  * @val: value
506  *
507  * Convert 64-bit floating point value (double) from native byte order into
508  * big endian byte order.
509  */
510 /**
511  * GDOUBLE_FROM_LE:
512  * @val: value
513  *
514  * Convert 64-bit floating point value (double) from little endian byte order
515  * into native byte order.
516  */
517 /**
518  * GDOUBLE_FROM_BE:
519  * @val: value
520  *
521  * Convert 64-bit floating point value (double) from big endian byte order
522  * into native byte order.
523  */
524
525 /**
526  * GFLOAT_TO_LE:
527  * @val: value
528  *
529  * Convert 32-bit floating point value (float) from native byte order into
530  * little endian byte order.
531  */
532 /**
533  * GFLOAT_TO_BE:
534  * @val: value
535  *
536  * Convert 32-bit floating point value (float) from native byte order into
537  * big endian byte order.
538  */
539 /**
540  * GFLOAT_FROM_LE:
541  * @val: value
542  *
543  * Convert 32-bit floating point value (float) from little endian byte order
544  * into native byte order.
545  */
546 /**
547  * GFLOAT_FROM_BE:
548  * @val: value
549  *
550  * Convert 32-bit floating point value (float) from big endian byte order
551  * into native byte order.
552  */
553
554 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
555 #define GFLOAT_TO_LE(val)    ((gfloat) (val))
556 #define GFLOAT_TO_BE(val)    (GFLOAT_SWAP_LE_BE (val))
557 #define GDOUBLE_TO_LE(val)   ((gdouble) (val))
558 #define GDOUBLE_TO_BE(val)   (GDOUBLE_SWAP_LE_BE (val))
559
560 #elif G_BYTE_ORDER == G_BIG_ENDIAN
561 #define GFLOAT_TO_LE(val)    (GFLOAT_SWAP_LE_BE (val))
562 #define GFLOAT_TO_BE(val)    ((gfloat) (val))
563 #define GDOUBLE_TO_LE(val)   (GDOUBLE_SWAP_LE_BE (val))
564 #define GDOUBLE_TO_BE(val)   ((gdouble) (val))
565
566 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
567 #error unknown ENDIAN type
568 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
569
570 #define GFLOAT_FROM_LE(val)  (GFLOAT_TO_LE (val))
571 #define GFLOAT_FROM_BE(val)  (GFLOAT_TO_BE (val))
572 #define GDOUBLE_FROM_LE(val) (GDOUBLE_TO_LE (val))
573 #define GDOUBLE_FROM_BE(val) (GDOUBLE_TO_BE (val))
574
575 #endif /* !defined(GFLOAT_FROM_LE) */
576
577 /**
578  * GST_READ_FLOAT_LE:
579  * @data: memory location
580  *
581  * Read a 32 bit float value in little endian format from the memory buffer.
582  *
583  * Returns: The floating point value read from @data
584  */
585 #ifdef _FOOL_GTK_DOC_
586 G_INLINE_FUNC gfloat GST_READ_FLOAT_LE (const guint8 *data);
587 #endif
588
589 inline static gfloat
590 GST_READ_FLOAT_LE(const guint8 *data)
591 {
592   union
593   {
594     guint32 i;
595     gfloat f;
596   } u;
597
598   u.i = GST_READ_UINT32_LE (data);
599   return u.f;
600 }
601
602 /**
603  * GST_READ_FLOAT_BE:
604  * @data: memory location
605  *
606  * Read a 32 bit float value in big endian format from the memory buffer.
607  *
608  * Returns: The floating point value read from @data
609  */
610 #ifdef _FOOL_GTK_DOC_
611 G_INLINE_FUNC gfloat GST_READ_FLOAT_BE (const guint8 *data);
612 #endif
613
614 inline static gfloat
615 GST_READ_FLOAT_BE(const guint8 *data)
616 {
617   union
618   {
619     guint32 i;
620     gfloat f;
621   } u;
622
623   u.i = GST_READ_UINT32_BE (data);
624   return u.f;
625 }
626
627 /**
628  * GST_READ_DOUBLE_LE:
629  * @data: memory location
630  *
631  * Read a 64 bit double value in little endian format from the memory buffer.
632  *
633  * Returns: The double-precision floating point value read from @data
634  */
635 #ifdef _FOOL_GTK_DOC_
636 G_INLINE_FUNC gdouble GST_READ_DOUBLE_LE (const guint8 *data);
637 #endif
638
639 inline static gdouble
640 GST_READ_DOUBLE_LE(const guint8 *data)
641 {
642   union
643   {
644     guint64 i;
645     gdouble d;
646   } u;
647
648   u.i = GST_READ_UINT64_LE (data);
649   return u.d;
650 }
651
652 /**
653  * GST_READ_DOUBLE_BE:
654  * @data: memory location
655  *
656  * Read a 64 bit double value in big endian format from the memory buffer.
657  *
658  * Returns: The double-precision floating point value read from @data
659  */
660 #ifdef _FOOL_GTK_DOC_
661 G_INLINE_FUNC gdouble GST_READ_DOUBLE_BE (const guint8 *data);
662 #endif
663
664 inline static gdouble
665 GST_READ_DOUBLE_BE(const guint8 *data)
666 {
667   union
668   {
669     guint64 i;
670     gdouble d;
671   } u;
672
673   u.i = GST_READ_UINT64_BE (data);
674   return u.d;
675 }
676
677 /**
678  * GST_WRITE_FLOAT_LE:
679  * @data: memory location
680  * @num: value to store
681  *
682  * Store a 32 bit float value in little endian format into the memory buffer.
683  */
684 #ifdef _FOOL_GTK_DOC_
685 G_INLINE_FUNC void GST_WRITE_FLOAT_LE (guint8 *data, gfloat num);
686 #endif
687
688 inline static void
689 GST_WRITE_FLOAT_LE(guint8 *data, gfloat num)
690 {
691   union
692   {
693     guint32 i;
694     gfloat f;
695   } u;
696
697   u.f = num;
698   GST_WRITE_UINT32_LE (data, u.i);
699 }
700
701 /**
702  * GST_WRITE_FLOAT_BE:
703  * @data: memory location
704  * @num: value to store
705  *
706  * Store a 32 bit float value in big endian format into the memory buffer.
707  */
708 #ifdef _FOOL_GTK_DOC_
709 G_INLINE_FUNC void GST_WRITE_FLOAT_BE (guint8 *data, gfloat num);
710 #endif
711
712 inline static void
713 GST_WRITE_FLOAT_BE(guint8 *data, gfloat num)
714 {
715   union
716   {
717     guint32 i;
718     gfloat f;
719   } u;
720
721   u.f = num;
722   GST_WRITE_UINT32_BE (data, u.i);
723 }
724
725 /**
726  * GST_WRITE_DOUBLE_LE:
727  * @data: memory location
728  * @num: value to store
729  *
730  * Store a 64 bit double value in little endian format into the memory buffer.
731  */
732 #ifdef _FOOL_GTK_DOC_
733 G_INLINE_FUNC void GST_WRITE_DOUBLE_LE (guint8 *data, gdouble num);
734 #endif
735
736 inline static void
737 GST_WRITE_DOUBLE_LE(guint8 *data, gdouble num)
738 {
739   union
740   {
741     guint64 i;
742     gdouble d;
743   } u;
744
745   u.d = num;
746   GST_WRITE_UINT64_LE (data, u.i);
747 }
748
749 /**
750  * GST_WRITE_DOUBLE_BE:
751  * @data: memory location
752  * @num: value to store
753  *
754  * Store a 64 bit double value in big endian format into the memory buffer.
755  */
756 #ifdef _FOOL_GTK_DOC_
757 G_INLINE_FUNC void GST_WRITE_DOUBLE_BE (guint8 *data, gdouble num);
758 #endif
759
760 inline static void
761 GST_WRITE_DOUBLE_BE(guint8 *data, gdouble num)
762 {
763   union
764   {
765     guint64 i;
766     gdouble d;
767   } u;
768
769   u.d = num;
770   GST_WRITE_UINT64_BE (data, u.i);
771 }
772
773 /* Miscellaneous utility macros */
774
775 /**
776  * GST_ROUND_UP_2:
777  * @num: integer value to round up
778  *
779  * Rounds an integer value up to the next multiple of 2.
780  */
781 #define GST_ROUND_UP_2(num)  (((num)+1)&~1)
782 /**
783  * GST_ROUND_UP_4:
784  * @num: integer value to round up
785  *
786  * Rounds an integer value up to the next multiple of 4.
787  */
788 #define GST_ROUND_UP_4(num)  (((num)+3)&~3)
789 /**
790  * GST_ROUND_UP_8:
791  * @num: integer value to round up
792  *
793  * Rounds an integer value up to the next multiple of 8.
794  */
795 #define GST_ROUND_UP_8(num)  (((num)+7)&~7)
796 /**
797  * GST_ROUND_UP_16:
798  * @num: integer value to round up
799  *
800  * Rounds an integer value up to the next multiple of 16.
801  */
802 #define GST_ROUND_UP_16(num) (((num)+15)&~15)
803 /**
804  * GST_ROUND_UP_32:
805  * @num: integer value to round up
806  *
807  * Rounds an integer value up to the next multiple of 32.
808  */
809 #define GST_ROUND_UP_32(num) (((num)+31)&~31)
810 /**
811  * GST_ROUND_UP_64:
812  * @num: integer value to round up
813  *
814  * Rounds an integer value up to the next multiple of 64.
815  */
816 #define GST_ROUND_UP_64(num) (((num)+63)&~63)
817
818 /**
819  * GST_ROUND_DOWN_2:
820  * @num: integer value to round down
821  *
822  * Rounds an integer value down to the next multiple of 2.
823  */
824 #define GST_ROUND_DOWN_2(num)  ((num)&(~1))
825 /**
826  * GST_ROUND_DOWN_4:
827  * @num: integer value to round down
828  *
829  * Rounds an integer value down to the next multiple of 4.
830  */
831 #define GST_ROUND_DOWN_4(num)  ((num)&(~3))
832 /**
833  * GST_ROUND_DOWN_8:
834  * @num: integer value to round down
835  *
836  * Rounds an integer value down to the next multiple of 8.
837  */
838 #define GST_ROUND_DOWN_8(num)  ((num)&(~7))
839 /**
840  * GST_ROUND_DOWN_16:
841  * @num: integer value to round down
842  *
843  * Rounds an integer value down to the next multiple of 16.
844  */
845 #define GST_ROUND_DOWN_16(num) ((num)&(~15))
846 /**
847  * GST_ROUND_DOWN_32:
848  * @num: integer value to round down
849  *
850  * Rounds an integer value down to the next multiple of 32.
851  */
852 #define GST_ROUND_DOWN_32(num) ((num)&(~31))
853 /**
854  * GST_ROUND_DOWN_64:
855  * @num: integer value to round down
856  *
857  * Rounds an integer value down to the next multiple of 64.
858  */
859 #define GST_ROUND_DOWN_64(num) ((num)&(~63))
860
861 void                    gst_object_default_error        (GstObject    * source,
862                                                          const GError * error,
863                                                          const gchar  * debug);
864
865 /* element functions */
866 void                    gst_element_create_all_pads     (GstElement *element);
867 GstPad*                 gst_element_get_compatible_pad  (GstElement *element, GstPad *pad,
868                                                          GstCaps *caps);
869
870 GstPadTemplate*         gst_element_get_compatible_pad_template (GstElement *element, GstPadTemplate *compattempl);
871
872 const gchar*            gst_element_state_get_name      (GstState state);
873 const gchar *           gst_element_state_change_return_get_name (GstStateChangeReturn state_ret);
874
875 gboolean                gst_element_link                (GstElement *src, GstElement *dest);
876 gboolean                gst_element_link_many           (GstElement *element_1,
877                                                          GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
878 gboolean                gst_element_link_filtered       (GstElement * src,
879                                                          GstElement * dest,
880                                                          GstCaps *filter);
881 void                    gst_element_unlink              (GstElement *src, GstElement *dest);
882 void                    gst_element_unlink_many         (GstElement *element_1,
883                                                          GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
884
885 gboolean                gst_element_link_pads           (GstElement *src, const gchar *srcpadname,
886                                                          GstElement *dest, const gchar *destpadname);
887 gboolean                gst_element_link_pads_full      (GstElement *src, const gchar *srcpadname,
888                                                          GstElement *dest, const gchar *destpadname,
889                                                          GstPadLinkCheck flags);
890 void                    gst_element_unlink_pads         (GstElement *src, const gchar *srcpadname,
891                                                          GstElement *dest, const gchar *destpadname);
892
893 gboolean                gst_element_link_pads_filtered  (GstElement * src, const gchar * srcpadname,
894                                                          GstElement * dest, const gchar * destpadname,
895                                                          GstCaps *filter);
896
897 gboolean                gst_element_seek_simple         (GstElement   *element,
898                                                          GstFormat     format,
899                                                          GstSeekFlags  seek_flags,
900                                                          gint64        seek_pos);
901
902 /* util elementfactory functions */
903 gboolean gst_element_factory_can_sink_all_caps (GstElementFactory *factory, const GstCaps *caps);
904 gboolean gst_element_factory_can_src_all_caps  (GstElementFactory *factory, const GstCaps *caps);
905 gboolean gst_element_factory_can_sink_any_caps (GstElementFactory *factory, const GstCaps *caps);
906 gboolean gst_element_factory_can_src_any_caps  (GstElementFactory *factory, const GstCaps *caps);
907
908 /* util query functions */
909 gboolean                gst_element_query_position      (GstElement *element, GstFormat format, gint64 *cur);
910 gboolean                gst_element_query_duration      (GstElement *element, GstFormat format, gint64 *duration);
911 gboolean                gst_element_query_convert       (GstElement *element, GstFormat src_format, gint64 src_val,
912                                                          GstFormat dest_format, gint64 *dest_val);
913
914 /* pad functions */
915 void                    gst_pad_use_fixed_caps          (GstPad *pad);
916 GstElement*             gst_pad_get_parent_element      (GstPad *pad);
917
918 /* util query functions */
919 gboolean                gst_pad_proxy_query_accept_caps (GstPad *pad, GstQuery *query);
920 gboolean                gst_pad_proxy_query_caps        (GstPad *pad, GstQuery *query);
921
922 gboolean                gst_pad_query_position          (GstPad *pad, GstFormat format, gint64 *cur);
923 gboolean                gst_pad_query_duration          (GstPad *pad, GstFormat format, gint64 *duration);
924 gboolean                gst_pad_query_convert           (GstPad *pad, GstFormat src_format, gint64 src_val,
925                                                          GstFormat dest_format, gint64 *dest_val);
926 GstCaps *               gst_pad_query_caps              (GstPad *pad, GstCaps *filter);
927 gboolean                gst_pad_query_accept_caps       (GstPad *pad, GstCaps *caps);
928
929
930 gboolean                gst_pad_peer_query_position     (GstPad *pad, GstFormat format, gint64 *cur);
931 gboolean                gst_pad_peer_query_duration     (GstPad *pad, GstFormat format, gint64 *duration);
932 gboolean                gst_pad_peer_query_convert      (GstPad *pad, GstFormat src_format, gint64 src_val,
933                                                          GstFormat dest_format, gint64 *dest_val);
934 GstCaps *               gst_pad_peer_query_caps         (GstPad * pad, GstCaps *filter);
935 gboolean                gst_pad_peer_query_accept_caps  (GstPad * pad, GstCaps *caps);
936
937 gchar *                 gst_pad_create_stream_id               (GstPad * pad, GstElement * parent, const gchar *stream_id);
938 gchar *                 gst_pad_create_stream_id_printf        (GstPad * pad, GstElement * parent, const gchar *stream_id, ...);
939 gchar *                 gst_pad_create_stream_id_printf_valist (GstPad * pad, GstElement * parent, const gchar *stream_id, va_list var_args);
940
941 gchar *                 gst_pad_get_stream_id           (GstPad * pad);
942
943 /* bin functions */
944 void                    gst_bin_add_many                (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
945 void                    gst_bin_remove_many             (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
946 GstPad *                gst_bin_find_unlinked_pad       (GstBin *bin, GstPadDirection direction);
947
948 /* parse utility functions */
949 GstElement *            gst_parse_bin_from_description      (const gchar     * bin_description,
950                                                              gboolean          ghost_unlinked_pads,
951                                                              GError         ** err);
952
953 GstElement *            gst_parse_bin_from_description_full (const gchar     * bin_description,
954                                                              gboolean          ghost_unlinked_pads,
955                                                              GstParseContext * context,
956                                                              GstParseFlags     flags,
957                                                              GError         ** err);
958
959 GstClockTime            gst_util_get_timestamp          (void);
960
961 /**
962  * GstSearchMode:
963  * @GST_SEARCH_MODE_EXACT : Only search for exact matches.
964  * @GST_SEARCH_MODE_BEFORE: Search for an exact match or the element just before.
965  * @GST_SEARCH_MODE_AFTER : Search for an exact match or the element just after.
966  *
967  * The different search modes.
968  */
969 typedef enum {
970   GST_SEARCH_MODE_EXACT = 0,
971   GST_SEARCH_MODE_BEFORE,
972   GST_SEARCH_MODE_AFTER
973 } GstSearchMode;
974
975 gpointer      gst_util_array_binary_search      (gpointer array, guint num_elements,
976                                                  gsize element_size, GCompareDataFunc search_func,
977                                                  GstSearchMode mode, gconstpointer search_data,
978                                                  gpointer user_data);
979
980 /* fraction operations */
981 gint          gst_util_greatest_common_divisor  (gint a, gint b);
982 gint64        gst_util_greatest_common_divisor_int64 (gint64 a, gint64 b);
983
984 void          gst_util_fraction_to_double       (gint src_n, gint src_d, gdouble *dest);
985 void          gst_util_double_to_fraction       (gdouble src, gint *dest_n, gint *dest_d);
986
987 gboolean      gst_util_fraction_multiply        (gint a_n, gint a_d, gint b_n, gint b_d,
988                                                  gint *res_n, gint *res_d);
989 gboolean      gst_util_fraction_add             (gint a_n, gint a_d, gint b_n, gint b_d,
990                                                  gint *res_n, gint *res_d);
991 gint          gst_util_fraction_compare         (gint a_n, gint a_d, gint b_n, gint b_d);
992
993
994 G_END_DECLS
995
996 #endif /* __GST_UTILS_H__ */