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