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