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