gstutils: Fix build with clang -Werror=cast-align
[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 #ifdef _FOOL_GTK_DOC_
455 G_INLINE_FUNC gfloat GFLOAT_SWAP_LE_BE (gfloat in);
456 #endif
457
458 inline static gfloat
459 GFLOAT_SWAP_LE_BE(gfloat in)
460 {
461   union
462   {
463     guint32 i;
464     gfloat f;
465   } u;
466
467   u.f = in;
468   u.i = GUINT32_SWAP_LE_BE (u.i);
469   return u.f;
470 }
471
472 /**
473  * GDOUBLE_SWAP_LE_BE:
474  * @in: input value
475  *
476  * Swap byte order of a 64-bit floating point value (double).
477  *
478  * Returns: @in byte-swapped.
479  */
480 #ifdef _FOOL_GTK_DOC_
481 G_INLINE_FUNC gdouble GDOUBLE_SWAP_LE_BE (gdouble in);
482 #endif
483
484 inline static gdouble
485 GDOUBLE_SWAP_LE_BE(gdouble in)
486 {
487   union
488   {
489     guint64 i;
490     gdouble d;
491   } u;
492
493   u.d = in;
494   u.i = GUINT64_SWAP_LE_BE (u.i);
495   return u.d;
496 }
497
498 /**
499  * GDOUBLE_TO_LE:
500  * @val: value
501  *
502  * Convert 64-bit floating point value (double) from native byte order into
503  * little endian byte order.
504  */
505 /**
506  * GDOUBLE_TO_BE:
507  * @val: value
508  *
509  * Convert 64-bit floating point value (double) from native byte order into
510  * big endian byte order.
511  */
512 /**
513  * GDOUBLE_FROM_LE:
514  * @val: value
515  *
516  * Convert 64-bit floating point value (double) from little endian byte order
517  * into native byte order.
518  */
519 /**
520  * GDOUBLE_FROM_BE:
521  * @val: value
522  *
523  * Convert 64-bit floating point value (double) from big endian byte order
524  * into native byte order.
525  */
526
527 /**
528  * GFLOAT_TO_LE:
529  * @val: value
530  *
531  * Convert 32-bit floating point value (float) from native byte order into
532  * little endian byte order.
533  */
534 /**
535  * GFLOAT_TO_BE:
536  * @val: value
537  *
538  * Convert 32-bit floating point value (float) from native byte order into
539  * big endian byte order.
540  */
541 /**
542  * GFLOAT_FROM_LE:
543  * @val: value
544  *
545  * Convert 32-bit floating point value (float) from little endian byte order
546  * into native byte order.
547  */
548 /**
549  * GFLOAT_FROM_BE:
550  * @val: value
551  *
552  * Convert 32-bit floating point value (float) from big endian byte order
553  * into native byte order.
554  */
555
556 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
557 #define GFLOAT_TO_LE(val)    ((gfloat) (val))
558 #define GFLOAT_TO_BE(val)    (GFLOAT_SWAP_LE_BE (val))
559 #define GDOUBLE_TO_LE(val)   ((gdouble) (val))
560 #define GDOUBLE_TO_BE(val)   (GDOUBLE_SWAP_LE_BE (val))
561
562 #elif G_BYTE_ORDER == G_BIG_ENDIAN
563 #define GFLOAT_TO_LE(val)    (GFLOAT_SWAP_LE_BE (val))
564 #define GFLOAT_TO_BE(val)    ((gfloat) (val))
565 #define GDOUBLE_TO_LE(val)   (GDOUBLE_SWAP_LE_BE (val))
566 #define GDOUBLE_TO_BE(val)   ((gdouble) (val))
567
568 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
569 #error unknown ENDIAN type
570 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
571
572 #define GFLOAT_FROM_LE(val)  (GFLOAT_TO_LE (val))
573 #define GFLOAT_FROM_BE(val)  (GFLOAT_TO_BE (val))
574 #define GDOUBLE_FROM_LE(val) (GDOUBLE_TO_LE (val))
575 #define GDOUBLE_FROM_BE(val) (GDOUBLE_TO_BE (val))
576
577 #endif /* !defined(GFLOAT_FROM_LE) */
578
579 /**
580  * GST_READ_FLOAT_LE:
581  * @data: memory location
582  *
583  * Read a 32 bit float value in little endian format from the memory buffer.
584  *
585  * Returns: The floating point value read from @data
586  */
587 #ifdef _FOOL_GTK_DOC_
588 G_INLINE_FUNC gfloat GST_READ_FLOAT_LE (const guint8 *data);
589 #endif
590
591 inline static gfloat
592 GST_READ_FLOAT_LE(const guint8 *data)
593 {
594   union
595   {
596     guint32 i;
597     gfloat f;
598   } u;
599
600   u.i = GST_READ_UINT32_LE (data);
601   return u.f;
602 }
603
604 /**
605  * GST_READ_FLOAT_BE:
606  * @data: memory location
607  *
608  * Read a 32 bit float value in big endian format from the memory buffer.
609  *
610  * Returns: The floating point value read from @data
611  */
612 #ifdef _FOOL_GTK_DOC_
613 G_INLINE_FUNC gfloat GST_READ_FLOAT_BE (const guint8 *data);
614 #endif
615
616 inline static gfloat
617 GST_READ_FLOAT_BE(const guint8 *data)
618 {
619   union
620   {
621     guint32 i;
622     gfloat f;
623   } u;
624
625   u.i = GST_READ_UINT32_BE (data);
626   return u.f;
627 }
628
629 /**
630  * GST_READ_DOUBLE_LE:
631  * @data: memory location
632  *
633  * Read a 64 bit double value in little endian format from the memory buffer.
634  *
635  * Returns: The double-precision floating point value read from @data
636  */
637 #ifdef _FOOL_GTK_DOC_
638 G_INLINE_FUNC gdouble GST_READ_DOUBLE_LE (const guint8 *data);
639 #endif
640
641 inline static gdouble
642 GST_READ_DOUBLE_LE(const guint8 *data)
643 {
644   union
645   {
646     guint64 i;
647     gdouble d;
648   } u;
649
650   u.i = GST_READ_UINT64_LE (data);
651   return u.d;
652 }
653
654 /**
655  * GST_READ_DOUBLE_BE:
656  * @data: memory location
657  *
658  * Read a 64 bit double value in big endian format from the memory buffer.
659  *
660  * Returns: The double-precision floating point value read from @data
661  */
662 #ifdef _FOOL_GTK_DOC_
663 G_INLINE_FUNC gdouble GST_READ_DOUBLE_BE (const guint8 *data);
664 #endif
665
666 inline static gdouble
667 GST_READ_DOUBLE_BE(const guint8 *data)
668 {
669   union
670   {
671     guint64 i;
672     gdouble d;
673   } u;
674
675   u.i = GST_READ_UINT64_BE (data);
676   return u.d;
677 }
678
679 /**
680  * GST_WRITE_FLOAT_LE:
681  * @data: memory location
682  * @num: value to store
683  *
684  * Store a 32 bit float value in little endian format into the memory buffer.
685  */
686 #ifdef _FOOL_GTK_DOC_
687 G_INLINE_FUNC void GST_WRITE_FLOAT_LE (guint8 *data, gfloat num);
688 #endif
689
690 inline static void
691 GST_WRITE_FLOAT_LE(guint8 *data, gfloat num)
692 {
693   union
694   {
695     guint32 i;
696     gfloat f;
697   } u;
698
699   u.f = num;
700   GST_WRITE_UINT32_LE (data, u.i);
701 }
702
703 /**
704  * GST_WRITE_FLOAT_BE:
705  * @data: memory location
706  * @num: value to store
707  *
708  * Store a 32 bit float value in big endian format into the memory buffer.
709  */
710 #ifdef _FOOL_GTK_DOC_
711 G_INLINE_FUNC void GST_WRITE_FLOAT_BE (guint8 *data, gfloat num);
712 #endif
713
714 inline static void
715 GST_WRITE_FLOAT_BE(guint8 *data, gfloat num)
716 {
717   union
718   {
719     guint32 i;
720     gfloat f;
721   } u;
722
723   u.f = num;
724   GST_WRITE_UINT32_BE (data, u.i);
725 }
726
727 /**
728  * GST_WRITE_DOUBLE_LE:
729  * @data: memory location
730  * @num: value to store
731  *
732  * Store a 64 bit double value in little endian format into the memory buffer.
733  */
734 #ifdef _FOOL_GTK_DOC_
735 G_INLINE_FUNC void GST_WRITE_DOUBLE_LE (guint8 *data, gdouble num);
736 #endif
737
738 inline static void
739 GST_WRITE_DOUBLE_LE(guint8 *data, gdouble num)
740 {
741   union
742   {
743     guint64 i;
744     gdouble d;
745   } u;
746
747   u.d = num;
748   GST_WRITE_UINT64_LE (data, u.i);
749 }
750
751 /**
752  * GST_WRITE_DOUBLE_BE:
753  * @data: memory location
754  * @num: value to store
755  *
756  * Store a 64 bit double value in big endian format into the memory buffer.
757  */
758 #ifdef _FOOL_GTK_DOC_
759 G_INLINE_FUNC void GST_WRITE_DOUBLE_BE (guint8 *data, gdouble num);
760 #endif
761
762 inline static void
763 GST_WRITE_DOUBLE_BE(guint8 *data, gdouble num)
764 {
765   union
766   {
767     guint64 i;
768     gdouble d;
769   } u;
770
771   u.d = num;
772   GST_WRITE_UINT64_BE (data, u.i);
773 }
774
775 /* Miscellaneous utility macros */
776
777 /**
778  * GST_ROUND_UP_2:
779  * @num: integer value to round up
780  *
781  * Rounds an integer value up to the next multiple of 2.
782  */
783 #define GST_ROUND_UP_2(num)  (((num)+1)&~1)
784 /**
785  * GST_ROUND_UP_4:
786  * @num: integer value to round up
787  *
788  * Rounds an integer value up to the next multiple of 4.
789  */
790 #define GST_ROUND_UP_4(num)  (((num)+3)&~3)
791 /**
792  * GST_ROUND_UP_8:
793  * @num: integer value to round up
794  *
795  * Rounds an integer value up to the next multiple of 8.
796  */
797 #define GST_ROUND_UP_8(num)  (((num)+7)&~7)
798 /**
799  * GST_ROUND_UP_16:
800  * @num: integer value to round up
801  *
802  * Rounds an integer value up to the next multiple of 16.
803  */
804 #define GST_ROUND_UP_16(num) (((num)+15)&~15)
805 /**
806  * GST_ROUND_UP_32:
807  * @num: integer value to round up
808  *
809  * Rounds an integer value up to the next multiple of 32.
810  */
811 #define GST_ROUND_UP_32(num) (((num)+31)&~31)
812 /**
813  * GST_ROUND_UP_64:
814  * @num: integer value to round up
815  *
816  * Rounds an integer value up to the next multiple of 64.
817  */
818 #define GST_ROUND_UP_64(num) (((num)+63)&~63)
819 /**
820  * GST_ROUND_UP_128:
821  * @num: integer value to round up
822  *
823  * Rounds an integer value up to the next multiple of 128.
824  * Since: 1.4
825  */
826 #define GST_ROUND_UP_128(num) (((num)+127)&~127)
827 /**
828  * GST_ROUND_UP_N:
829  * @num: integrer value to round up
830  * @align: a power of two to round up to
831  *
832  * Rounds an integer value up to the next multiple of @align. @align MUST be a
833  * power of two.
834  */
835 #define GST_ROUND_UP_N(num,align) ((((num) + ((align) - 1)) & ~((align) - 1)))
836
837
838 /**
839  * GST_ROUND_DOWN_2:
840  * @num: integer value to round down
841  *
842  * Rounds an integer value down to the next multiple of 2.
843  */
844 #define GST_ROUND_DOWN_2(num)  ((num)&(~1))
845 /**
846  * GST_ROUND_DOWN_4:
847  * @num: integer value to round down
848  *
849  * Rounds an integer value down to the next multiple of 4.
850  */
851 #define GST_ROUND_DOWN_4(num)  ((num)&(~3))
852 /**
853  * GST_ROUND_DOWN_8:
854  * @num: integer value to round down
855  *
856  * Rounds an integer value down to the next multiple of 8.
857  */
858 #define GST_ROUND_DOWN_8(num)  ((num)&(~7))
859 /**
860  * GST_ROUND_DOWN_16:
861  * @num: integer value to round down
862  *
863  * Rounds an integer value down to the next multiple of 16.
864  */
865 #define GST_ROUND_DOWN_16(num) ((num)&(~15))
866 /**
867  * GST_ROUND_DOWN_32:
868  * @num: integer value to round down
869  *
870  * Rounds an integer value down to the next multiple of 32.
871  */
872 #define GST_ROUND_DOWN_32(num) ((num)&(~31))
873 /**
874  * GST_ROUND_DOWN_64:
875  * @num: integer value to round down
876  *
877  * Rounds an integer value down to the next multiple of 64.
878  */
879 #define GST_ROUND_DOWN_64(num) ((num)&(~63))
880 /**
881  * GST_ROUND_DOWN_128:
882  * @num: integer value to round down
883  *
884  * Rounds an integer value down to the next multiple of 128.
885  * Since: 1.4
886  */
887 #define GST_ROUND_DOWN_128(num) ((num)&(~127))
888 /**
889  * GST_ROUND_DOWN_N:
890  * @num: integrer value to round down
891  * @align: a power of two to round down to
892  *
893  * Rounds an integer value down to the next multiple of @align. @align MUST be a
894  * power of two.
895  */
896 #define GST_ROUND_DOWN_N(num,align) (((num) & ~((align) - 1)))
897
898
899 void                    gst_object_default_error        (GstObject    * source,
900                                                          const GError * error,
901                                                          const gchar  * debug);
902
903 /* element functions */
904 void                    gst_element_create_all_pads     (GstElement *element);
905 GstPad*                 gst_element_get_compatible_pad  (GstElement *element, GstPad *pad,
906                                                          GstCaps *caps);
907
908 GstPadTemplate*         gst_element_get_compatible_pad_template (GstElement *element, GstPadTemplate *compattempl);
909
910 const gchar*            gst_element_state_get_name      (GstState state);
911 const gchar *           gst_element_state_change_return_get_name (GstStateChangeReturn state_ret);
912
913 gboolean                gst_element_link                (GstElement *src, GstElement *dest);
914 gboolean                gst_element_link_many           (GstElement *element_1,
915                                                          GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
916 gboolean                gst_element_link_filtered       (GstElement * src,
917                                                          GstElement * dest,
918                                                          GstCaps *filter);
919 void                    gst_element_unlink              (GstElement *src, GstElement *dest);
920 void                    gst_element_unlink_many         (GstElement *element_1,
921                                                          GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
922
923 gboolean                gst_element_link_pads           (GstElement *src, const gchar *srcpadname,
924                                                          GstElement *dest, const gchar *destpadname);
925 gboolean                gst_element_link_pads_full      (GstElement *src, const gchar *srcpadname,
926                                                          GstElement *dest, const gchar *destpadname,
927                                                          GstPadLinkCheck flags);
928 void                    gst_element_unlink_pads         (GstElement *src, const gchar *srcpadname,
929                                                          GstElement *dest, const gchar *destpadname);
930
931 gboolean                gst_element_link_pads_filtered  (GstElement * src, const gchar * srcpadname,
932                                                          GstElement * dest, const gchar * destpadname,
933                                                          GstCaps *filter);
934
935 gboolean                gst_element_seek_simple         (GstElement   *element,
936                                                          GstFormat     format,
937                                                          GstSeekFlags  seek_flags,
938                                                          gint64        seek_pos);
939
940 /* util elementfactory functions */
941 gboolean gst_element_factory_can_sink_all_caps (GstElementFactory *factory, const GstCaps *caps);
942 gboolean gst_element_factory_can_src_all_caps  (GstElementFactory *factory, const GstCaps *caps);
943 gboolean gst_element_factory_can_sink_any_caps (GstElementFactory *factory, const GstCaps *caps);
944 gboolean gst_element_factory_can_src_any_caps  (GstElementFactory *factory, const GstCaps *caps);
945
946 /* util query functions */
947 gboolean                gst_element_query_position      (GstElement *element, GstFormat format, gint64 *cur);
948 gboolean                gst_element_query_duration      (GstElement *element, GstFormat format, gint64 *duration);
949 gboolean                gst_element_query_convert       (GstElement *element, GstFormat src_format, gint64 src_val,
950                                                          GstFormat dest_format, gint64 *dest_val);
951
952 /* pad functions */
953 void                    gst_pad_use_fixed_caps          (GstPad *pad);
954 GstElement*             gst_pad_get_parent_element      (GstPad *pad);
955
956 /* util query functions */
957 gboolean                gst_pad_proxy_query_accept_caps (GstPad *pad, GstQuery *query);
958 gboolean                gst_pad_proxy_query_caps        (GstPad *pad, GstQuery *query);
959
960 gboolean                gst_pad_query_position          (GstPad *pad, GstFormat format, gint64 *cur);
961 gboolean                gst_pad_query_duration          (GstPad *pad, GstFormat format, gint64 *duration);
962 gboolean                gst_pad_query_convert           (GstPad *pad, GstFormat src_format, gint64 src_val,
963                                                          GstFormat dest_format, gint64 *dest_val);
964 GstCaps *               gst_pad_query_caps              (GstPad *pad, GstCaps *filter);
965 gboolean                gst_pad_query_accept_caps       (GstPad *pad, GstCaps *caps);
966
967
968 gboolean                gst_pad_peer_query_position     (GstPad *pad, GstFormat format, gint64 *cur);
969 gboolean                gst_pad_peer_query_duration     (GstPad *pad, GstFormat format, gint64 *duration);
970 gboolean                gst_pad_peer_query_convert      (GstPad *pad, GstFormat src_format, gint64 src_val,
971                                                          GstFormat dest_format, gint64 *dest_val);
972 GstCaps *               gst_pad_peer_query_caps         (GstPad * pad, GstCaps *filter);
973 gboolean                gst_pad_peer_query_accept_caps  (GstPad * pad, GstCaps *caps);
974
975 gchar *                 gst_pad_create_stream_id               (GstPad * pad, GstElement * parent, const gchar *stream_id) G_GNUC_MALLOC;
976 gchar *                 gst_pad_create_stream_id_printf        (GstPad * pad, GstElement * parent, const gchar *stream_id, ...) G_GNUC_PRINTF (3, 4) G_GNUC_MALLOC;
977 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;
978
979 gchar *                 gst_pad_get_stream_id           (GstPad * pad);
980
981 /* bin functions */
982 void                    gst_bin_add_many                (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
983 void                    gst_bin_remove_many             (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
984 GstPad *                gst_bin_find_unlinked_pad       (GstBin *bin, GstPadDirection direction);
985
986 gboolean                gst_bin_sync_children_states    (GstBin *bin);
987
988 /* parse utility functions */
989 GstElement *            gst_parse_bin_from_description      (const gchar     * bin_description,
990                                                              gboolean          ghost_unlinked_pads,
991                                                              GError         ** err);
992
993 GstElement *            gst_parse_bin_from_description_full (const gchar     * bin_description,
994                                                              gboolean          ghost_unlinked_pads,
995                                                              GstParseContext * context,
996                                                              GstParseFlags     flags,
997                                                              GError         ** err);
998
999 GstClockTime            gst_util_get_timestamp          (void);
1000
1001 /**
1002  * GstSearchMode:
1003  * @GST_SEARCH_MODE_EXACT : Only search for exact matches.
1004  * @GST_SEARCH_MODE_BEFORE: Search for an exact match or the element just before.
1005  * @GST_SEARCH_MODE_AFTER : Search for an exact match or the element just after.
1006  *
1007  * The different search modes.
1008  */
1009 typedef enum {
1010   GST_SEARCH_MODE_EXACT = 0,
1011   GST_SEARCH_MODE_BEFORE,
1012   GST_SEARCH_MODE_AFTER
1013 } GstSearchMode;
1014
1015 gpointer      gst_util_array_binary_search      (gpointer array, guint num_elements,
1016                                                  gsize element_size, GCompareDataFunc search_func,
1017                                                  GstSearchMode mode, gconstpointer search_data,
1018                                                  gpointer user_data);
1019
1020 /* fraction operations */
1021 gint          gst_util_greatest_common_divisor  (gint a, gint b);
1022 gint64        gst_util_greatest_common_divisor_int64 (gint64 a, gint64 b);
1023
1024 void          gst_util_fraction_to_double       (gint src_n, gint src_d, gdouble *dest);
1025 void          gst_util_double_to_fraction       (gdouble src, gint *dest_n, gint *dest_d);
1026
1027 gboolean      gst_util_fraction_multiply        (gint a_n, gint a_d, gint b_n, gint b_d,
1028                                                  gint *res_n, gint *res_d);
1029 gboolean      gst_util_fraction_add             (gint a_n, gint a_d, gint b_n, gint b_d,
1030                                                  gint *res_n, gint *res_d);
1031 gint          gst_util_fraction_compare         (gint a_n, gint a_d, gint b_n, gint b_d);
1032
1033
1034 G_END_DECLS
1035
1036 #endif /* __GST_UTILS_H__ */