pad: add gst_pad_get_stream_id() utility function
[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                                           _GST_PUT (data, 0, 64, 56, num); \
278                                           _GST_PUT (data, 1, 64, 48, num); \
279                                           _GST_PUT (data, 2, 64, 40, num); \
280                                           _GST_PUT (data, 3, 64, 32, num); \
281                                           _GST_PUT (data, 4, 64, 24, num); \
282                                           _GST_PUT (data, 5, 64, 16, num); \
283                                           _GST_PUT (data, 6, 64,  8, num); \
284                                           _GST_PUT (data, 7, 64,  0, num); \
285                                         } while (0)
286
287 /**
288  * GST_WRITE_UINT64_LE:
289  * @data: memory location
290  * @num: value to store
291  *
292  * Store a 64 bit unsigned integer value in little endian format into the memory buffer.
293  */
294 #define GST_WRITE_UINT64_LE(data, num)  do { \
295                                           _GST_PUT (data, 0, 64,  0, num); \
296                                           _GST_PUT (data, 1, 64,  8, num); \
297                                           _GST_PUT (data, 2, 64, 16, num); \
298                                           _GST_PUT (data, 3, 64, 24, num); \
299                                           _GST_PUT (data, 4, 64, 32, num); \
300                                           _GST_PUT (data, 5, 64, 40, num); \
301                                           _GST_PUT (data, 6, 64, 48, num); \
302                                           _GST_PUT (data, 7, 64, 56, num); \
303                                         } while (0)
304
305 /**
306  * GST_WRITE_UINT32_BE:
307  * @data: memory location
308  * @num: value to store
309  *
310  * Store a 32 bit unsigned integer value in big endian format into the memory buffer.
311  */
312 #define GST_WRITE_UINT32_BE(data, num)  do { \
313                                           _GST_PUT (data, 0, 32, 24, num); \
314                                           _GST_PUT (data, 1, 32, 16, num); \
315                                           _GST_PUT (data, 2, 32,  8, num); \
316                                           _GST_PUT (data, 3, 32,  0, num); \
317                                         } while (0)
318
319 /**
320  * GST_WRITE_UINT32_LE:
321  * @data: memory location
322  * @num: value to store
323  *
324  * Store a 32 bit unsigned integer value in little endian format into the memory buffer.
325  */
326 #define GST_WRITE_UINT32_LE(data, num)  do { \
327                                           _GST_PUT (data, 0, 32,  0, num); \
328                                           _GST_PUT (data, 1, 32,  8, num); \
329                                           _GST_PUT (data, 2, 32, 16, num); \
330                                           _GST_PUT (data, 3, 32, 24, num); \
331                                         } while (0)
332
333 /**
334  * GST_WRITE_UINT24_BE:
335  * @data: memory location
336  * @num: value to store
337  *
338  * Store a 24 bit unsigned integer value in big endian format into the memory buffer.
339  */
340 #define GST_WRITE_UINT24_BE(data, num)  do { \
341                                           _GST_PUT (data, 0, 32,  16, num); \
342                                           _GST_PUT (data, 1, 32,  8, num); \
343                                           _GST_PUT (data, 2, 32,  0, num); \
344                                         } while (0)
345
346 /**
347  * GST_WRITE_UINT24_LE:
348  * @data: memory location
349  * @num: value to store
350  *
351  * Store a 24 bit unsigned integer value in little endian format into the memory buffer.
352  */
353 #define GST_WRITE_UINT24_LE(data, num)  do { \
354                                           _GST_PUT (data, 0, 32,  0, num); \
355                                           _GST_PUT (data, 1, 32,  8, num); \
356                                           _GST_PUT (data, 2, 32,  16, num); \
357                                         } while (0)
358
359 /**
360  * GST_WRITE_UINT16_BE:
361  * @data: memory location
362  * @num: value to store
363  *
364  * Store a 16 bit unsigned integer value in big endian format into the memory buffer.
365  */
366 #define GST_WRITE_UINT16_BE(data, num)  do { \
367                                           _GST_PUT (data, 0, 16,  8, num); \
368                                           _GST_PUT (data, 1, 16,  0, num); \
369                                         } while (0)
370
371 /**
372  * GST_WRITE_UINT16_LE:
373  * @data: memory location
374  * @num: value to store
375  *
376  * Store a 16 bit unsigned integer value in little endian format into the memory buffer.
377  */
378 #define GST_WRITE_UINT16_LE(data, num)  do { \
379                                           _GST_PUT (data, 0, 16,  0, num); \
380                                           _GST_PUT (data, 1, 16,  8, num); \
381                                         } while (0)
382
383 /**
384  * GST_WRITE_UINT8:
385  * @data: memory location
386  * @num: value to store
387  *
388  * Store an 8 bit unsigned integer value into the memory buffer.
389  */
390 #define GST_WRITE_UINT8(data, num)      do { \
391                                           _GST_PUT (data, 0,  8,  0, num); \
392                                         } while (0)
393
394 /* Float endianness conversion macros */
395
396 /* FIXME: Remove this once we depend on a GLib version with this */
397 #ifndef GFLOAT_FROM_LE
398 /**
399  * GFLOAT_SWAP_LE_BE:
400  * @in: input value
401  *
402  * Swap byte order of a 32-bit floating point value (float).
403  *
404  * Returns: @in byte-swapped.
405  */
406 #ifdef _FOOL_GTK_DOC_
407 G_INLINE_FUNC gfloat GFLOAT_SWAP_LE_BE (gfloat in);
408 #endif
409
410 inline static gfloat
411 GFLOAT_SWAP_LE_BE(gfloat in)
412 {
413   union
414   {
415     guint32 i;
416     gfloat f;
417   } u;
418
419   u.f = in;
420   u.i = GUINT32_SWAP_LE_BE (u.i);
421   return u.f;
422 }
423
424 /**
425  * GDOUBLE_SWAP_LE_BE:
426  * @in: input value
427  *
428  * Swap byte order of a 64-bit floating point value (double).
429  *
430  * Returns: @in byte-swapped.
431  */
432 #ifdef _FOOL_GTK_DOC_
433 G_INLINE_FUNC gdouble GDOUBLE_SWAP_LE_BE (gdouble in);
434 #endif
435
436 inline static gdouble
437 GDOUBLE_SWAP_LE_BE(gdouble in)
438 {
439   union
440   {
441     guint64 i;
442     gdouble d;
443   } u;
444
445   u.d = in;
446   u.i = GUINT64_SWAP_LE_BE (u.i);
447   return u.d;
448 }
449
450 /**
451  * GDOUBLE_TO_LE:
452  * @val: value
453  *
454  * Convert 64-bit floating point value (double) from native byte order into
455  * little endian byte order.
456  */
457 /**
458  * GDOUBLE_TO_BE:
459  * @val: value
460  *
461  * Convert 64-bit floating point value (double) from native byte order into
462  * big endian byte order.
463  */
464 /**
465  * GDOUBLE_FROM_LE:
466  * @val: value
467  *
468  * Convert 64-bit floating point value (double) from little endian byte order
469  * into native byte order.
470  */
471 /**
472  * GDOUBLE_FROM_BE:
473  * @val: value
474  *
475  * Convert 64-bit floating point value (double) from big endian byte order
476  * into native byte order.
477  */
478
479 /**
480  * GFLOAT_TO_LE:
481  * @val: value
482  *
483  * Convert 32-bit floating point value (float) from native byte order into
484  * little endian byte order.
485  */
486 /**
487  * GFLOAT_TO_BE:
488  * @val: value
489  *
490  * Convert 32-bit floating point value (float) from native byte order into
491  * big endian byte order.
492  */
493 /**
494  * GFLOAT_FROM_LE:
495  * @val: value
496  *
497  * Convert 32-bit floating point value (float) from little endian byte order
498  * into native byte order.
499  */
500 /**
501  * GFLOAT_FROM_BE:
502  * @val: value
503  *
504  * Convert 32-bit floating point value (float) from big endian byte order
505  * into native byte order.
506  */
507
508 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
509 #define GFLOAT_TO_LE(val)    ((gfloat) (val))
510 #define GFLOAT_TO_BE(val)    (GFLOAT_SWAP_LE_BE (val))
511 #define GDOUBLE_TO_LE(val)   ((gdouble) (val))
512 #define GDOUBLE_TO_BE(val)   (GDOUBLE_SWAP_LE_BE (val))
513
514 #elif G_BYTE_ORDER == G_BIG_ENDIAN
515 #define GFLOAT_TO_LE(val)    (GFLOAT_SWAP_LE_BE (val))
516 #define GFLOAT_TO_BE(val)    ((gfloat) (val))
517 #define GDOUBLE_TO_LE(val)   (GDOUBLE_SWAP_LE_BE (val))
518 #define GDOUBLE_TO_BE(val)   ((gdouble) (val))
519
520 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
521 #error unknown ENDIAN type
522 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
523
524 #define GFLOAT_FROM_LE(val)  (GFLOAT_TO_LE (val))
525 #define GFLOAT_FROM_BE(val)  (GFLOAT_TO_BE (val))
526 #define GDOUBLE_FROM_LE(val) (GDOUBLE_TO_LE (val))
527 #define GDOUBLE_FROM_BE(val) (GDOUBLE_TO_BE (val))
528
529 #endif /* !defined(GFLOAT_FROM_LE) */
530
531 /**
532  * GST_READ_FLOAT_LE:
533  * @data: memory location
534  *
535  * Read a 32 bit float value in little endian format from the memory buffer.
536  *
537  * Returns: The floating point value read from @data
538  */
539 #ifdef _FOOL_GTK_DOC_
540 G_INLINE_FUNC gfloat GST_READ_FLOAT_LE (const guint8 *data);
541 #endif
542
543 inline static gfloat
544 GST_READ_FLOAT_LE(const guint8 *data)
545 {
546   union
547   {
548     guint32 i;
549     gfloat f;
550   } u;
551
552   u.i = GST_READ_UINT32_LE (data);
553   return u.f;
554 }
555
556 /**
557  * GST_READ_FLOAT_BE:
558  * @data: memory location
559  *
560  * Read a 32 bit float value in big endian format from the memory buffer.
561  *
562  * Returns: The floating point value read from @data
563  */
564 #ifdef _FOOL_GTK_DOC_
565 G_INLINE_FUNC gfloat GST_READ_FLOAT_BE (const guint8 *data);
566 #endif
567
568 inline static gfloat
569 GST_READ_FLOAT_BE(const guint8 *data)
570 {
571   union
572   {
573     guint32 i;
574     gfloat f;
575   } u;
576
577   u.i = GST_READ_UINT32_BE (data);
578   return u.f;
579 }
580
581 /**
582  * GST_READ_DOUBLE_LE:
583  * @data: memory location
584  *
585  * Read a 64 bit double value in little endian format from the memory buffer.
586  *
587  * Returns: The double-precision floating point value read from @data
588  */
589 #ifdef _FOOL_GTK_DOC_
590 G_INLINE_FUNC gdouble GST_READ_DOUBLE_LE (const guint8 *data);
591 #endif
592
593 inline static gdouble
594 GST_READ_DOUBLE_LE(const guint8 *data)
595 {
596   union
597   {
598     guint64 i;
599     gdouble d;
600   } u;
601
602   u.i = GST_READ_UINT64_LE (data);
603   return u.d;
604 }
605
606 /**
607  * GST_READ_DOUBLE_BE:
608  * @data: memory location
609  *
610  * Read a 64 bit double value in big endian format from the memory buffer.
611  *
612  * Returns: The double-precision floating point value read from @data
613  */
614 #ifdef _FOOL_GTK_DOC_
615 G_INLINE_FUNC gdouble GST_READ_DOUBLE_BE (const guint8 *data);
616 #endif
617
618 inline static gdouble
619 GST_READ_DOUBLE_BE(const guint8 *data)
620 {
621   union
622   {
623     guint64 i;
624     gdouble d;
625   } u;
626
627   u.i = GST_READ_UINT64_BE (data);
628   return u.d;
629 }
630
631 /**
632  * GST_WRITE_FLOAT_LE:
633  * @data: memory location
634  * @num: value to store
635  *
636  * Store a 32 bit float value in little endian format into the memory buffer.
637  */
638 #ifdef _FOOL_GTK_DOC_
639 G_INLINE_FUNC void GST_WRITE_FLOAT_LE (guint8 *data, gfloat num);
640 #endif
641
642 inline static void
643 GST_WRITE_FLOAT_LE(guint8 *data, gfloat num)
644 {
645   union
646   {
647     guint32 i;
648     gfloat f;
649   } u;
650
651   u.f = num;
652   GST_WRITE_UINT32_LE (data, u.i);
653 }
654
655 /**
656  * GST_WRITE_FLOAT_BE:
657  * @data: memory location
658  * @num: value to store
659  *
660  * Store a 32 bit float value in big endian format into the memory buffer.
661  */
662 #ifdef _FOOL_GTK_DOC_
663 G_INLINE_FUNC void GST_WRITE_FLOAT_BE (guint8 *data, gfloat num);
664 #endif
665
666 inline static void
667 GST_WRITE_FLOAT_BE(guint8 *data, gfloat num)
668 {
669   union
670   {
671     guint32 i;
672     gfloat f;
673   } u;
674
675   u.f = num;
676   GST_WRITE_UINT32_BE (data, u.i);
677 }
678
679 /**
680  * GST_WRITE_DOUBLE_LE:
681  * @data: memory location
682  * @num: value to store
683  *
684  * Store a 64 bit double value in little endian format into the memory buffer.
685  */
686 #ifdef _FOOL_GTK_DOC_
687 G_INLINE_FUNC void GST_WRITE_DOUBLE_LE (guint8 *data, gdouble num);
688 #endif
689
690 inline static void
691 GST_WRITE_DOUBLE_LE(guint8 *data, gdouble num)
692 {
693   union
694   {
695     guint64 i;
696     gdouble d;
697   } u;
698
699   u.d = num;
700   GST_WRITE_UINT64_LE (data, u.i);
701 }
702
703 /**
704  * GST_WRITE_DOUBLE_BE:
705  * @data: memory location
706  * @num: value to store
707  *
708  * Store a 64 bit double value in big endian format into the memory buffer.
709  */
710 #ifdef _FOOL_GTK_DOC_
711 G_INLINE_FUNC void GST_WRITE_DOUBLE_BE (guint8 *data, gdouble num);
712 #endif
713
714 inline static void
715 GST_WRITE_DOUBLE_BE(guint8 *data, gdouble num)
716 {
717   union
718   {
719     guint64 i;
720     gdouble d;
721   } u;
722
723   u.d = num;
724   GST_WRITE_UINT64_BE (data, u.i);
725 }
726
727 /* Miscellaneous utility macros */
728
729 /**
730  * GST_ROUND_UP_2:
731  * @num: integer value to round up
732  *
733  * Rounds an integer value up to the next multiple of 2.
734  */
735 #define GST_ROUND_UP_2(num)  (((num)+1)&~1)
736 /**
737  * GST_ROUND_UP_4:
738  * @num: integer value to round up
739  *
740  * Rounds an integer value up to the next multiple of 4.
741  */
742 #define GST_ROUND_UP_4(num)  (((num)+3)&~3)
743 /**
744  * GST_ROUND_UP_8:
745  * @num: integer value to round up
746  *
747  * Rounds an integer value up to the next multiple of 8.
748  */
749 #define GST_ROUND_UP_8(num)  (((num)+7)&~7)
750 /**
751  * GST_ROUND_UP_16:
752  * @num: integer value to round up
753  *
754  * Rounds an integer value up to the next multiple of 16.
755  */
756 #define GST_ROUND_UP_16(num) (((num)+15)&~15)
757 /**
758  * GST_ROUND_UP_32:
759  * @num: integer value to round up
760  *
761  * Rounds an integer value up to the next multiple of 32.
762  */
763 #define GST_ROUND_UP_32(num) (((num)+31)&~31)
764 /**
765  * GST_ROUND_UP_64:
766  * @num: integer value to round up
767  *
768  * Rounds an integer value up to the next multiple of 64.
769  */
770 #define GST_ROUND_UP_64(num) (((num)+63)&~63)
771
772 /**
773  * GST_ROUND_DOWN_2:
774  * @num: integer value to round down
775  *
776  * Rounds an integer value down to the next multiple of 2.
777  */
778 #define GST_ROUND_DOWN_2(num)  ((num)&(~1))
779 /**
780  * GST_ROUND_DOWN_4:
781  * @num: integer value to round down
782  *
783  * Rounds an integer value down to the next multiple of 4.
784  */
785 #define GST_ROUND_DOWN_4(num)  ((num)&(~3))
786 /**
787  * GST_ROUND_DOWN_8:
788  * @num: integer value to round down
789  *
790  * Rounds an integer value down to the next multiple of 8.
791  */
792 #define GST_ROUND_DOWN_8(num)  ((num)&(~7))
793 /**
794  * GST_ROUND_DOWN_16:
795  * @num: integer value to round down
796  *
797  * Rounds an integer value down to the next multiple of 16.
798  */
799 #define GST_ROUND_DOWN_16(num) ((num)&(~15))
800 /**
801  * GST_ROUND_DOWN_32:
802  * @num: integer value to round down
803  *
804  * Rounds an integer value down to the next multiple of 32.
805  */
806 #define GST_ROUND_DOWN_32(num) ((num)&(~31))
807 /**
808  * GST_ROUND_DOWN_64:
809  * @num: integer value to round down
810  *
811  * Rounds an integer value down to the next multiple of 64.
812  */
813 #define GST_ROUND_DOWN_64(num) ((num)&(~63))
814
815 void                    gst_object_default_error        (GstObject    * source,
816                                                          const GError * error,
817                                                          const gchar  * debug);
818
819 /* element functions */
820 void                    gst_element_create_all_pads     (GstElement *element);
821 GstPad*                 gst_element_get_compatible_pad  (GstElement *element, GstPad *pad,
822                                                          GstCaps *caps);
823
824 GstPadTemplate*         gst_element_get_compatible_pad_template (GstElement *element, GstPadTemplate *compattempl);
825
826 const gchar*            gst_element_state_get_name      (GstState state);
827 const gchar *           gst_element_state_change_return_get_name (GstStateChangeReturn state_ret);
828
829 gboolean                gst_element_link                (GstElement *src, GstElement *dest);
830 gboolean                gst_element_link_many           (GstElement *element_1,
831                                                          GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
832 gboolean                gst_element_link_filtered       (GstElement * src,
833                                                          GstElement * dest,
834                                                          GstCaps *filter);
835 void                    gst_element_unlink              (GstElement *src, GstElement *dest);
836 void                    gst_element_unlink_many         (GstElement *element_1,
837                                                          GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
838
839 gboolean                gst_element_link_pads           (GstElement *src, const gchar *srcpadname,
840                                                          GstElement *dest, const gchar *destpadname);
841 gboolean                gst_element_link_pads_full      (GstElement *src, const gchar *srcpadname,
842                                                          GstElement *dest, const gchar *destpadname,
843                                                          GstPadLinkCheck flags);
844 void                    gst_element_unlink_pads         (GstElement *src, const gchar *srcpadname,
845                                                          GstElement *dest, const gchar *destpadname);
846
847 gboolean                gst_element_link_pads_filtered  (GstElement * src, const gchar * srcpadname,
848                                                          GstElement * dest, const gchar * destpadname,
849                                                          GstCaps *filter);
850
851 gboolean                gst_element_seek_simple         (GstElement   *element,
852                                                          GstFormat     format,
853                                                          GstSeekFlags  seek_flags,
854                                                          gint64        seek_pos);
855
856 /* util elementfactory functions */
857 gboolean gst_element_factory_can_sink_all_caps (GstElementFactory *factory, const GstCaps *caps);
858 gboolean gst_element_factory_can_src_all_caps  (GstElementFactory *factory, const GstCaps *caps);
859 gboolean gst_element_factory_can_sink_any_caps (GstElementFactory *factory, const GstCaps *caps);
860 gboolean gst_element_factory_can_src_any_caps  (GstElementFactory *factory, const GstCaps *caps);
861
862 /* util query functions */
863 gboolean                gst_element_query_position      (GstElement *element, GstFormat format, gint64 *cur);
864 gboolean                gst_element_query_duration      (GstElement *element, GstFormat format, gint64 *duration);
865 gboolean                gst_element_query_convert       (GstElement *element, GstFormat src_format, gint64 src_val,
866                                                          GstFormat dest_format, gint64 *dest_val);
867
868 /* pad functions */
869 void                    gst_pad_use_fixed_caps          (GstPad *pad);
870 GstElement*             gst_pad_get_parent_element      (GstPad *pad);
871
872 /* util query functions */
873 gboolean                gst_pad_proxy_query_accept_caps (GstPad *pad, GstQuery *query);
874 gboolean                gst_pad_proxy_query_caps        (GstPad *pad, GstQuery *query);
875
876 gboolean                gst_pad_query_position          (GstPad *pad, GstFormat format, gint64 *cur);
877 gboolean                gst_pad_query_duration          (GstPad *pad, GstFormat format, gint64 *duration);
878 gboolean                gst_pad_query_convert           (GstPad *pad, GstFormat src_format, gint64 src_val,
879                                                          GstFormat dest_format, gint64 *dest_val);
880 GstCaps *               gst_pad_query_caps              (GstPad *pad, GstCaps *filter);
881 gboolean                gst_pad_query_accept_caps       (GstPad *pad, GstCaps *caps);
882
883
884 gboolean                gst_pad_peer_query_position     (GstPad *pad, GstFormat format, gint64 *cur);
885 gboolean                gst_pad_peer_query_duration     (GstPad *pad, GstFormat format, gint64 *duration);
886 gboolean                gst_pad_peer_query_convert      (GstPad *pad, GstFormat src_format, gint64 src_val,
887                                                          GstFormat dest_format, gint64 *dest_val);
888 GstCaps *               gst_pad_peer_query_caps         (GstPad * pad, GstCaps *filter);
889 gboolean                gst_pad_peer_query_accept_caps  (GstPad * pad, GstCaps *caps);
890
891 gchar *                 gst_pad_create_stream_id               (GstPad * pad, GstElement * parent, const gchar *stream_id);
892 gchar *                 gst_pad_create_stream_id_printf        (GstPad * pad, GstElement * parent, const gchar *stream_id, ...);
893 gchar *                 gst_pad_create_stream_id_printf_valist (GstPad * pad, GstElement * parent, const gchar *stream_id, va_list var_args);
894
895 gchar *                 gst_pad_get_stream_id           (GstPad * pad);
896
897 /* bin functions */
898 void                    gst_bin_add_many                (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
899 void                    gst_bin_remove_many             (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
900 GstPad *                gst_bin_find_unlinked_pad       (GstBin *bin, GstPadDirection direction);
901
902 /* parse utility functions */
903 GstElement *            gst_parse_bin_from_description      (const gchar     * bin_description,
904                                                              gboolean          ghost_unlinked_pads,
905                                                              GError         ** err);
906
907 GstElement *            gst_parse_bin_from_description_full (const gchar     * bin_description,
908                                                              gboolean          ghost_unlinked_pads,
909                                                              GstParseContext * context,
910                                                              GstParseFlags     flags,
911                                                              GError         ** err);
912
913 GstClockTime            gst_util_get_timestamp          (void);
914
915 /**
916  * GstSearchMode:
917  * @GST_SEARCH_MODE_EXACT : Only search for exact matches.
918  * @GST_SEARCH_MODE_BEFORE: Search for an exact match or the element just before.
919  * @GST_SEARCH_MODE_AFTER : Search for an exact match or the element just after.
920  *
921  * The different search modes.
922  */
923 typedef enum {
924   GST_SEARCH_MODE_EXACT = 0,
925   GST_SEARCH_MODE_BEFORE,
926   GST_SEARCH_MODE_AFTER
927 } GstSearchMode;
928
929 gpointer      gst_util_array_binary_search      (gpointer array, guint num_elements,
930                                                  gsize element_size, GCompareDataFunc search_func,
931                                                  GstSearchMode mode, gconstpointer search_data,
932                                                  gpointer user_data);
933
934 /* fraction operations */
935 gint          gst_util_greatest_common_divisor  (gint a, gint b);
936 gint64        gst_util_greatest_common_divisor_int64 (gint64 a, gint64 b);
937
938 void          gst_util_fraction_to_double       (gint src_n, gint src_d, gdouble *dest);
939 void          gst_util_double_to_fraction       (gdouble src, gint *dest_n, gint *dest_d);
940
941 gboolean      gst_util_fraction_multiply        (gint a_n, gint a_d, gint b_n, gint b_d,
942                                                  gint *res_n, gint *res_d);
943 gboolean      gst_util_fraction_add             (gint a_n, gint a_d, gint b_n, gint b_d,
944                                                  gint *res_n, gint *res_d);
945 gint          gst_util_fraction_compare         (gint a_n, gint a_d, gint b_n, gint b_d);
946
947
948 G_END_DECLS
949
950 #endif /* __GST_UTILS_H__ */