gst: mark symbols explicitly for export with GST_EXPORT
[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 GST_EXPORT
36 void            gst_util_set_value_from_string  (GValue *value, const gchar *value_str);
37
38 GST_EXPORT
39 void            gst_util_set_object_arg         (GObject *object, const gchar *name, const gchar *value);
40
41 GST_EXPORT
42 gboolean        gst_util_set_object_array       (GObject * object, const gchar * name,
43                                                  const GValueArray * array);
44 GST_EXPORT
45 gboolean        gst_util_get_object_array       (GObject * object, const gchar * name,
46                                                  GValueArray ** array);
47 GST_EXPORT
48 void            gst_util_dump_mem               (const guchar *mem, guint size);
49
50 GST_EXPORT
51 guint64         gst_util_gdouble_to_guint64     (gdouble value)  G_GNUC_CONST;
52
53 GST_EXPORT
54 gdouble         gst_util_guint64_to_gdouble     (guint64 value)  G_GNUC_CONST;
55
56 /**
57  * gst_guint64_to_gdouble:
58  * @value: the #guint64 value to convert
59  *
60  * Convert @value to a gdouble.
61  *
62  * Returns: @value converted to a #gdouble.
63  */
64
65 /**
66  * gst_gdouble_to_guint64:
67  * @value: the #gdouble value to convert
68  *
69  * Convert @value to a guint64.
70  *
71  * Returns: @value converted to a #guint64.
72  */
73 #ifdef WIN32
74 #define         gst_gdouble_to_guint64(value)   gst_util_gdouble_to_guint64(value)
75 #define         gst_guint64_to_gdouble(value)   gst_util_guint64_to_gdouble(value)
76 #else
77 #define         gst_gdouble_to_guint64(value)   ((guint64) (value))
78 #define         gst_guint64_to_gdouble(value)   ((gdouble) (value))
79 #endif
80
81 GST_EXPORT
82 guint64         gst_util_uint64_scale           (guint64 val, guint64 num, guint64 denom);
83
84 GST_EXPORT
85 guint64         gst_util_uint64_scale_round     (guint64 val, guint64 num, guint64 denom);
86
87 GST_EXPORT
88 guint64         gst_util_uint64_scale_ceil      (guint64 val, guint64 num, guint64 denom);
89
90 GST_EXPORT
91 guint64         gst_util_uint64_scale_int       (guint64 val, gint num, gint denom);
92
93 GST_EXPORT
94 guint64         gst_util_uint64_scale_int_round (guint64 val, gint num, gint denom);
95
96 GST_EXPORT
97 guint64         gst_util_uint64_scale_int_ceil  (guint64 val, gint num, gint denom);
98
99 GST_EXPORT
100 guint32         gst_util_seqnum_next            (void);
101
102 GST_EXPORT
103 gint32          gst_util_seqnum_compare         (guint32 s1, guint32 s2);
104
105 GST_EXPORT
106 guint           gst_util_group_id_next          (void);
107
108 /**
109  * GST_CALL_PARENT:
110  * @parent_class_cast: the name of the class cast macro for the parent type
111  * @name: name of the function to call
112  * @args: arguments enclosed in '( )'
113  *
114  * Just call the parent handler.  This assumes that there is a variable
115  * named parent_class that points to the (duh!) parent class.  Note that
116  * this macro is not to be used with things that return something, use
117  * the _WITH_DEFAULT version for that
118  */
119 #define GST_CALL_PARENT(parent_class_cast, name, args)                  \
120         ((parent_class_cast(parent_class)->name != NULL) ?              \
121          parent_class_cast(parent_class)->name args : (void) 0)
122
123 /**
124  * GST_CALL_PARENT_WITH_DEFAULT:
125  * @parent_class_cast: the name of the class cast macro for the parent type
126  * @name: name of the function to call
127  * @args: arguments enclosed in '( )'
128  * @def_return: default result
129  *
130  * Same as GST_CALL_PARENT(), but in case there is no implementation, it
131  * evaluates to @def_return.
132  */
133 #define GST_CALL_PARENT_WITH_DEFAULT(parent_class_cast, name, args, def_return)\
134         ((parent_class_cast(parent_class)->name != NULL) ?              \
135          parent_class_cast(parent_class)->name args : def_return)
136
137 /* Define PUT and GET functions for unaligned memory */
138 #define _GST_GET(__data, __idx, __size, __shift) \
139     (((guint##__size) (((const guint8 *) (__data))[__idx])) << (__shift))
140
141 #define _GST_PUT(__data, __idx, __size, __shift, __num) \
142     (((guint8 *) (__data))[__idx] = (((guint##__size) (__num)) >> (__shift)) & 0xff)
143
144 #ifndef __GTK_DOC_IGNORE__
145 #if GST_HAVE_UNALIGNED_ACCESS
146 static inline guint16 __gst_fast_read16(const guint8 *v) {
147   return *(const guint16*)(const void*)(v);
148 }
149 static inline guint32 __gst_fast_read32(const guint8 *v) {
150   return *(const guint32*)(const void*)(v);
151 }
152 static inline guint64 __gst_fast_read64(const guint8 *v) {
153   return *(const guint64*)(const void*)(v);
154 }
155 static inline guint16 __gst_fast_read_swap16(const guint8 *v) {
156   return GUINT16_SWAP_LE_BE(*(const guint16*)(const void*)(v));
157 }
158 static inline guint32 __gst_fast_read_swap32(const guint8 *v) {
159   return GUINT32_SWAP_LE_BE(*(const guint32*)(const void*)(v));
160 }
161 static inline guint64 __gst_fast_read_swap64(const guint8 *v) {
162   return GUINT64_SWAP_LE_BE(*(const guint64*)(const void*)(v));
163 }
164 # define _GST_FAST_READ(s, d) __gst_fast_read##s((const guint8 *)(d))
165 # define _GST_FAST_READ_SWAP(s, d) __gst_fast_read_swap##s((const guint8 *)(d))
166
167 static inline void __gst_fast_write16 (guint8 *p, guint16 v) {
168   *(guint16*)(void*)(p) = v;
169 }
170 static inline void __gst_fast_write32 (guint8 *p, guint32 v) {
171   *(guint32*)(void*)(p) = v;
172 }
173 static inline void __gst_fast_write64 (guint8 *p, guint64 v) {
174   *(guint64*)(void*)(p) = v;
175 }
176 static inline void __gst_fast_write_swap16 (guint8 *p, guint16 v) {
177   *(guint16*)(void*)(p) = GUINT16_SWAP_LE_BE (v);
178 }
179 static inline void __gst_fast_write_swap32 (guint8 *p, guint32 v) {
180   *(guint32*)(void*)(p) = GUINT32_SWAP_LE_BE (v);
181 }
182 static inline void __gst_fast_write_swap64 (guint8 *p, guint64 v) {
183   *(guint64*)(void*)(p) = GUINT64_SWAP_LE_BE (v);
184 }
185 # define _GST_FAST_WRITE(s, d, v) __gst_fast_write##s((guint8 *)(d), (v))
186 # define _GST_FAST_WRITE_SWAP(s, d, v) __gst_fast_write_swap##s((guint8 *)(d), (v))
187 #endif
188 #endif
189
190
191 /**
192  * GST_READ_UINT64_BE:
193  * @data: memory location
194  *
195  * Read a 64 bit unsigned integer value in big endian format from the memory buffer.
196  */
197
198 /**
199  * GST_READ_UINT64_LE:
200  * @data: memory location
201  *
202  * Read a 64 bit unsigned integer value in little endian format from the memory buffer.
203  */
204 #if GST_HAVE_UNALIGNED_ACCESS
205 # if (G_BYTE_ORDER == G_BIG_ENDIAN)
206 #  define GST_READ_UINT64_BE(data)      _GST_FAST_READ (64, data)
207 #  define GST_READ_UINT64_LE(data)      _GST_FAST_READ_SWAP (64, data)
208 # else
209 #  define GST_READ_UINT64_BE(data)      _GST_FAST_READ_SWAP (64, data)
210 #  define GST_READ_UINT64_LE(data)      _GST_FAST_READ (64, data)
211 # endif
212 #else
213 #define _GST_READ_UINT64_BE(data)       (_GST_GET (data, 0, 64, 56) | \
214                                          _GST_GET (data, 1, 64, 48) | \
215                                          _GST_GET (data, 2, 64, 40) | \
216                                          _GST_GET (data, 3, 64, 32) | \
217                                          _GST_GET (data, 4, 64, 24) | \
218                                          _GST_GET (data, 5, 64, 16) | \
219                                          _GST_GET (data, 6, 64,  8) | \
220                                          _GST_GET (data, 7, 64,  0))
221
222 #define _GST_READ_UINT64_LE(data)       (_GST_GET (data, 7, 64, 56) | \
223                                          _GST_GET (data, 6, 64, 48) | \
224                                          _GST_GET (data, 5, 64, 40) | \
225                                          _GST_GET (data, 4, 64, 32) | \
226                                          _GST_GET (data, 3, 64, 24) | \
227                                          _GST_GET (data, 2, 64, 16) | \
228                                          _GST_GET (data, 1, 64,  8) | \
229                                          _GST_GET (data, 0, 64,  0))
230
231 #define GST_READ_UINT64_BE(data) __gst_slow_read64_be((const guint8 *)(data))
232 static inline guint64 __gst_slow_read64_be (const guint8 * data) {
233   return _GST_READ_UINT64_BE (data);
234 }
235 #define GST_READ_UINT64_LE(data) __gst_slow_read64_le((const guint8 *)(data))
236 static inline guint64 __gst_slow_read64_le (const guint8 * data) {
237   return _GST_READ_UINT64_LE (data);
238 }
239 #endif
240
241 /**
242  * GST_READ_UINT32_BE:
243  * @data: memory location
244  *
245  * Read a 32 bit unsigned integer value in big endian format from the memory buffer.
246  */
247
248 /**
249  * GST_READ_UINT32_LE:
250  * @data: memory location
251  *
252  * Read a 32 bit unsigned integer value in little endian format from the memory buffer.
253  */
254 #if GST_HAVE_UNALIGNED_ACCESS
255 # if (G_BYTE_ORDER == G_BIG_ENDIAN)
256 #  define GST_READ_UINT32_BE(data)      _GST_FAST_READ (32, data)
257 #  define GST_READ_UINT32_LE(data)      _GST_FAST_READ_SWAP (32, data)
258 # else
259 #  define GST_READ_UINT32_BE(data)      _GST_FAST_READ_SWAP (32, data)
260 #  define GST_READ_UINT32_LE(data)      _GST_FAST_READ (32, data)
261 # endif
262 #else
263 #define _GST_READ_UINT32_BE(data)       (_GST_GET (data, 0, 32, 24) | \
264                                          _GST_GET (data, 1, 32, 16) | \
265                                          _GST_GET (data, 2, 32,  8) | \
266                                          _GST_GET (data, 3, 32,  0))
267
268 #define _GST_READ_UINT32_LE(data)       (_GST_GET (data, 3, 32, 24) | \
269                                          _GST_GET (data, 2, 32, 16) | \
270                                          _GST_GET (data, 1, 32,  8) | \
271                                          _GST_GET (data, 0, 32,  0))
272
273 #define GST_READ_UINT32_BE(data) __gst_slow_read32_be((const guint8 *)(data))
274 static inline guint32 __gst_slow_read32_be (const guint8 * data) {
275   return _GST_READ_UINT32_BE (data);
276 }
277 #define GST_READ_UINT32_LE(data) __gst_slow_read32_le((const guint8 *)(data))
278 static inline guint32 __gst_slow_read32_le (const guint8 * data) {
279   return _GST_READ_UINT32_LE (data);
280 }
281 #endif
282
283 /**
284  * GST_READ_UINT24_BE:
285  * @data: memory location
286  *
287  * Read a 24 bit unsigned integer value in big endian format from the memory buffer.
288  */
289 #define _GST_READ_UINT24_BE(data)       (_GST_GET (data, 0, 32, 16) | \
290                                          _GST_GET (data, 1, 32,  8) | \
291                                          _GST_GET (data, 2, 32,  0))
292
293 #define GST_READ_UINT24_BE(data) __gst_slow_read24_be((const guint8 *)(data))
294 static inline guint32 __gst_slow_read24_be (const guint8 * data) {
295   return _GST_READ_UINT24_BE (data);
296 }
297
298 /**
299  * GST_READ_UINT24_LE:
300  * @data: memory location
301  *
302  * Read a 24 bit unsigned integer value in little endian format from the memory buffer.
303  */
304 #define _GST_READ_UINT24_LE(data)       (_GST_GET (data, 2, 32, 16) | \
305                                          _GST_GET (data, 1, 32,  8) | \
306                                          _GST_GET (data, 0, 32,  0))
307
308 #define GST_READ_UINT24_LE(data) __gst_slow_read24_le((const guint8 *)(data))
309 static inline guint32 __gst_slow_read24_le (const guint8 * data) {
310   return _GST_READ_UINT24_LE (data);
311 }
312
313 /**
314  * GST_READ_UINT16_BE:
315  * @data: memory location
316  *
317  * Read a 16 bit unsigned integer value in big endian format from the memory buffer.
318  */
319 /**
320  * GST_READ_UINT16_LE:
321  * @data: memory location
322  *
323  * Read a 16 bit unsigned integer value in little endian format from the memory buffer.
324  */
325 #if GST_HAVE_UNALIGNED_ACCESS
326 # if (G_BYTE_ORDER == G_BIG_ENDIAN)
327 #  define GST_READ_UINT16_BE(data)      _GST_FAST_READ (16, data)
328 #  define GST_READ_UINT16_LE(data)      _GST_FAST_READ_SWAP (16, data)
329 # else
330 #  define GST_READ_UINT16_BE(data)      _GST_FAST_READ_SWAP (16, data)
331 #  define GST_READ_UINT16_LE(data)      _GST_FAST_READ (16, data)
332 # endif
333 #else
334 #define _GST_READ_UINT16_BE(data)       (_GST_GET (data, 0, 16,  8) | \
335                                          _GST_GET (data, 1, 16,  0))
336
337 #define _GST_READ_UINT16_LE(data)       (_GST_GET (data, 1, 16,  8) | \
338                                          _GST_GET (data, 0, 16,  0))
339
340 #define GST_READ_UINT16_BE(data) __gst_slow_read16_be((const guint8 *)(data))
341 static inline guint16 __gst_slow_read16_be (const guint8 * data) {
342   return _GST_READ_UINT16_BE (data);
343 }
344 #define GST_READ_UINT16_LE(data) __gst_slow_read16_le((const guint8 *)(data))
345 static inline guint16 __gst_slow_read16_le (const guint8 * data) {
346   return _GST_READ_UINT16_LE (data);
347 }
348 #endif
349
350 /**
351  * GST_READ_UINT8:
352  * @data: memory location
353  *
354  * Read an 8 bit unsigned integer value from the memory buffer.
355  */
356 #define GST_READ_UINT8(data)            (_GST_GET (data, 0,  8,  0))
357
358 /**
359  * GST_WRITE_UINT64_BE:
360  * @data: memory location
361  * @val: value to store
362  *
363  * Store a 64 bit unsigned integer value in big endian format into the memory buffer.
364  */
365 /**
366  * GST_WRITE_UINT64_LE:
367  * @data: memory location
368  * @val: value to store
369  *
370  * Store a 64 bit unsigned integer value in little endian format into the memory buffer.
371  */
372 #if GST_HAVE_UNALIGNED_ACCESS
373 # if (G_BYTE_ORDER == G_BIG_ENDIAN)
374 #  define GST_WRITE_UINT64_BE(data,val) _GST_FAST_WRITE(64,data,val)
375 #  define GST_WRITE_UINT64_LE(data,val) _GST_FAST_WRITE_SWAP(64,data,val)
376 # else
377 #  define GST_WRITE_UINT64_BE(data,val) _GST_FAST_WRITE_SWAP(64,data,val)
378 #  define GST_WRITE_UINT64_LE(data,val) _GST_FAST_WRITE(64,data,val)
379 # endif
380 #else
381 #define GST_WRITE_UINT64_BE(data,val)   do { \
382                                           gpointer __put_data = data; \
383                                           guint64 __put_val = val; \
384                                           _GST_PUT (__put_data, 0, 64, 56, __put_val); \
385                                           _GST_PUT (__put_data, 1, 64, 48, __put_val); \
386                                           _GST_PUT (__put_data, 2, 64, 40, __put_val); \
387                                           _GST_PUT (__put_data, 3, 64, 32, __put_val); \
388                                           _GST_PUT (__put_data, 4, 64, 24, __put_val); \
389                                           _GST_PUT (__put_data, 5, 64, 16, __put_val); \
390                                           _GST_PUT (__put_data, 6, 64,  8, __put_val); \
391                                           _GST_PUT (__put_data, 7, 64,  0, __put_val); \
392                                         } while (0)
393
394 #define GST_WRITE_UINT64_LE(data,val)   do { \
395                                           gpointer __put_data = data; \
396                                           guint64 __put_val = val; \
397                                           _GST_PUT (__put_data, 0, 64,  0, __put_val); \
398                                           _GST_PUT (__put_data, 1, 64,  8, __put_val); \
399                                           _GST_PUT (__put_data, 2, 64, 16, __put_val); \
400                                           _GST_PUT (__put_data, 3, 64, 24, __put_val); \
401                                           _GST_PUT (__put_data, 4, 64, 32, __put_val); \
402                                           _GST_PUT (__put_data, 5, 64, 40, __put_val); \
403                                           _GST_PUT (__put_data, 6, 64, 48, __put_val); \
404                                           _GST_PUT (__put_data, 7, 64, 56, __put_val); \
405                                         } while (0)
406 #endif /* !GST_HAVE_UNALIGNED_ACCESS */
407
408 /**
409  * GST_WRITE_UINT32_BE:
410  * @data: memory location
411  * @val: value to store
412  *
413  * Store a 32 bit unsigned integer value in big endian format into the memory buffer.
414  */
415 /**
416  * GST_WRITE_UINT32_LE:
417  * @data: memory location
418  * @val: value to store
419  *
420  * Store a 32 bit unsigned integer value in little endian format into the memory buffer.
421  */
422 #if GST_HAVE_UNALIGNED_ACCESS
423 # if (G_BYTE_ORDER == G_BIG_ENDIAN)
424 #  define GST_WRITE_UINT32_BE(data,val) _GST_FAST_WRITE(32,data,val)
425 #  define GST_WRITE_UINT32_LE(data,val) _GST_FAST_WRITE_SWAP(32,data,val)
426 # else
427 #  define GST_WRITE_UINT32_BE(data,val) _GST_FAST_WRITE_SWAP(32,data,val)
428 #  define GST_WRITE_UINT32_LE(data,val) _GST_FAST_WRITE(32,data,val)
429 # endif
430 #else
431 #define GST_WRITE_UINT32_BE(data,val)   do { \
432                                           gpointer __put_data = data; \
433                                           guint32 __put_val = val; \
434                                           _GST_PUT (__put_data, 0, 32, 24, __put_val); \
435                                           _GST_PUT (__put_data, 1, 32, 16, __put_val); \
436                                           _GST_PUT (__put_data, 2, 32,  8, __put_val); \
437                                           _GST_PUT (__put_data, 3, 32,  0, __put_val); \
438                                         } while (0)
439
440 #define GST_WRITE_UINT32_LE(data,val)   do { \
441                                           gpointer __put_data = data; \
442                                           guint32 __put_val = val; \
443                                           _GST_PUT (__put_data, 0, 32,  0, __put_val); \
444                                           _GST_PUT (__put_data, 1, 32,  8, __put_val); \
445                                           _GST_PUT (__put_data, 2, 32, 16, __put_val); \
446                                           _GST_PUT (__put_data, 3, 32, 24, __put_val); \
447                                         } while (0)
448 #endif /* !GST_HAVE_UNALIGNED_ACCESS */
449
450 /**
451  * GST_WRITE_UINT24_BE:
452  * @data: memory location
453  * @num: value to store
454  *
455  * Store a 24 bit unsigned integer value in big endian format into the memory buffer.
456  */
457 #define GST_WRITE_UINT24_BE(data, num)  do { \
458                                           gpointer __put_data = data; \
459                                           guint32 __put_val = num; \
460                                           _GST_PUT (__put_data, 0, 32,  16, __put_val); \
461                                           _GST_PUT (__put_data, 1, 32,  8, __put_val); \
462                                           _GST_PUT (__put_data, 2, 32,  0, __put_val); \
463                                         } while (0)
464
465 /**
466  * GST_WRITE_UINT24_LE:
467  * @data: memory location
468  * @num: value to store
469  *
470  * Store a 24 bit unsigned integer value in little endian format into the memory buffer.
471  */
472 #define GST_WRITE_UINT24_LE(data, num)  do { \
473                                           gpointer __put_data = data; \
474                                           guint32 __put_val = num; \
475                                           _GST_PUT (__put_data, 0, 32,  0, __put_val); \
476                                           _GST_PUT (__put_data, 1, 32,  8, __put_val); \
477                                           _GST_PUT (__put_data, 2, 32,  16, __put_val); \
478                                         } while (0)
479
480 /**
481  * GST_WRITE_UINT16_BE:
482  * @data: memory location
483  * @val: value to store
484  *
485  * Store a 16 bit unsigned integer value in big endian format into the memory buffer.
486  */
487 /**
488  * GST_WRITE_UINT16_LE:
489  * @data: memory location
490  * @val: value to store
491  *
492  * Store a 16 bit unsigned integer value in little endian format into the memory buffer.
493  */
494 #if GST_HAVE_UNALIGNED_ACCESS
495 # if (G_BYTE_ORDER == G_BIG_ENDIAN)
496 #  define GST_WRITE_UINT16_BE(data,val) _GST_FAST_WRITE(16,data,val)
497 #  define GST_WRITE_UINT16_LE(data,val) _GST_FAST_WRITE_SWAP(16,data,val)
498 # else
499 #  define GST_WRITE_UINT16_BE(data,val) _GST_FAST_WRITE_SWAP(16,data,val)
500 #  define GST_WRITE_UINT16_LE(data,val) _GST_FAST_WRITE(16,data,val)
501 # endif
502 #else
503 #define GST_WRITE_UINT16_BE(data,val)   do { \
504                                           gpointer __put_data = data; \
505                                           guint16 __put_val = val; \
506                                           _GST_PUT (__put_data, 0, 16,  8, __put_val); \
507                                           _GST_PUT (__put_data, 1, 16,  0, __put_val); \
508                                         } while (0)
509
510 #define GST_WRITE_UINT16_LE(data,val)   do { \
511                                           gpointer __put_data = data; \
512                                           guint16 __put_val = val; \
513                                           _GST_PUT (__put_data, 0, 16,  0, __put_val); \
514                                           _GST_PUT (__put_data, 1, 16,  8, __put_val); \
515                                         } while (0)
516 #endif /* !GST_HAVE_UNALIGNED_ACCESS */
517
518 /**
519  * GST_WRITE_UINT8:
520  * @data: memory location
521  * @num: value to store
522  *
523  * Store an 8 bit unsigned integer value into the memory buffer.
524  */
525 #define GST_WRITE_UINT8(data, num)      do { \
526                                           _GST_PUT (data, 0,  8,  0, num); \
527                                         } while (0)
528
529 /* Float endianness conversion macros */
530
531 /* FIXME: Remove this once we depend on a GLib version with this */
532 #ifndef GFLOAT_FROM_LE
533 /**
534  * GFLOAT_SWAP_LE_BE:
535  * @in: input value
536  *
537  * Swap byte order of a 32-bit floating point value (float).
538  *
539  * Returns: @in byte-swapped.
540  */
541 static inline gfloat
542 GFLOAT_SWAP_LE_BE(gfloat in)
543 {
544   union
545   {
546     guint32 i;
547     gfloat f;
548   } u;
549
550   u.f = in;
551   u.i = GUINT32_SWAP_LE_BE (u.i);
552   return u.f;
553 }
554
555 /**
556  * GDOUBLE_SWAP_LE_BE:
557  * @in: input value
558  *
559  * Swap byte order of a 64-bit floating point value (double).
560  *
561  * Returns: @in byte-swapped.
562  */
563 static inline gdouble
564 GDOUBLE_SWAP_LE_BE(gdouble in)
565 {
566   union
567   {
568     guint64 i;
569     gdouble d;
570   } u;
571
572   u.d = in;
573   u.i = GUINT64_SWAP_LE_BE (u.i);
574   return u.d;
575 }
576
577 /**
578  * GDOUBLE_TO_LE:
579  * @val: value
580  *
581  * Convert 64-bit floating point value (double) from native byte order into
582  * little endian byte order.
583  */
584 /**
585  * GDOUBLE_TO_BE:
586  * @val: value
587  *
588  * Convert 64-bit floating point value (double) from native byte order into
589  * big endian byte order.
590  */
591 /**
592  * GDOUBLE_FROM_LE:
593  * @val: value
594  *
595  * Convert 64-bit floating point value (double) from little endian byte order
596  * into native byte order.
597  */
598 /**
599  * GDOUBLE_FROM_BE:
600  * @val: value
601  *
602  * Convert 64-bit floating point value (double) from big endian byte order
603  * into native byte order.
604  */
605
606 /**
607  * GFLOAT_TO_LE:
608  * @val: value
609  *
610  * Convert 32-bit floating point value (float) from native byte order into
611  * little endian byte order.
612  */
613 /**
614  * GFLOAT_TO_BE:
615  * @val: value
616  *
617  * Convert 32-bit floating point value (float) from native byte order into
618  * big endian byte order.
619  */
620 /**
621  * GFLOAT_FROM_LE:
622  * @val: value
623  *
624  * Convert 32-bit floating point value (float) from little endian byte order
625  * into native byte order.
626  */
627 /**
628  * GFLOAT_FROM_BE:
629  * @val: value
630  *
631  * Convert 32-bit floating point value (float) from big endian byte order
632  * into native byte order.
633  */
634
635 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
636 #define GFLOAT_TO_LE(val)    ((gfloat) (val))
637 #define GFLOAT_TO_BE(val)    (GFLOAT_SWAP_LE_BE (val))
638 #define GDOUBLE_TO_LE(val)   ((gdouble) (val))
639 #define GDOUBLE_TO_BE(val)   (GDOUBLE_SWAP_LE_BE (val))
640
641 #elif G_BYTE_ORDER == G_BIG_ENDIAN
642 #define GFLOAT_TO_LE(val)    (GFLOAT_SWAP_LE_BE (val))
643 #define GFLOAT_TO_BE(val)    ((gfloat) (val))
644 #define GDOUBLE_TO_LE(val)   (GDOUBLE_SWAP_LE_BE (val))
645 #define GDOUBLE_TO_BE(val)   ((gdouble) (val))
646
647 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
648 #error unknown ENDIAN type
649 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
650
651 #define GFLOAT_FROM_LE(val)  (GFLOAT_TO_LE (val))
652 #define GFLOAT_FROM_BE(val)  (GFLOAT_TO_BE (val))
653 #define GDOUBLE_FROM_LE(val) (GDOUBLE_TO_LE (val))
654 #define GDOUBLE_FROM_BE(val) (GDOUBLE_TO_BE (val))
655
656 #endif /* !defined(GFLOAT_FROM_LE) */
657
658 /**
659  * GST_READ_FLOAT_LE:
660  * @data: memory location
661  *
662  * Read a 32 bit float value in little endian format from the memory buffer.
663  *
664  * Returns: The floating point value read from @data
665  */
666 static inline gfloat
667 GST_READ_FLOAT_LE(const guint8 *data)
668 {
669   union
670   {
671     guint32 i;
672     gfloat f;
673   } u;
674
675   u.i = GST_READ_UINT32_LE (data);
676   return u.f;
677 }
678
679 /**
680  * GST_READ_FLOAT_BE:
681  * @data: memory location
682  *
683  * Read a 32 bit float value in big endian format from the memory buffer.
684  *
685  * Returns: The floating point value read from @data
686  */
687 static inline gfloat
688 GST_READ_FLOAT_BE(const guint8 *data)
689 {
690   union
691   {
692     guint32 i;
693     gfloat f;
694   } u;
695
696   u.i = GST_READ_UINT32_BE (data);
697   return u.f;
698 }
699
700 /**
701  * GST_READ_DOUBLE_LE:
702  * @data: memory location
703  *
704  * Read a 64 bit double value in little endian format from the memory buffer.
705  *
706  * Returns: The double-precision floating point value read from @data
707  */
708 static inline gdouble
709 GST_READ_DOUBLE_LE(const guint8 *data)
710 {
711   union
712   {
713     guint64 i;
714     gdouble d;
715   } u;
716
717   u.i = GST_READ_UINT64_LE (data);
718   return u.d;
719 }
720
721 /**
722  * GST_READ_DOUBLE_BE:
723  * @data: memory location
724  *
725  * Read a 64 bit double value in big endian format from the memory buffer.
726  *
727  * Returns: The double-precision floating point value read from @data
728  */
729 static inline gdouble
730 GST_READ_DOUBLE_BE(const guint8 *data)
731 {
732   union
733   {
734     guint64 i;
735     gdouble d;
736   } u;
737
738   u.i = GST_READ_UINT64_BE (data);
739   return u.d;
740 }
741
742 /**
743  * GST_WRITE_FLOAT_LE:
744  * @data: memory location
745  * @num: value to store
746  *
747  * Store a 32 bit float value in little endian format into the memory buffer.
748  */
749 static inline void
750 GST_WRITE_FLOAT_LE(guint8 *data, gfloat num)
751 {
752   union
753   {
754     guint32 i;
755     gfloat f;
756   } u;
757
758   u.f = num;
759   GST_WRITE_UINT32_LE (data, u.i);
760 }
761
762 /**
763  * GST_WRITE_FLOAT_BE:
764  * @data: memory location
765  * @num: value to store
766  *
767  * Store a 32 bit float value in big endian format into the memory buffer.
768  */
769 static inline void
770 GST_WRITE_FLOAT_BE(guint8 *data, gfloat num)
771 {
772   union
773   {
774     guint32 i;
775     gfloat f;
776   } u;
777
778   u.f = num;
779   GST_WRITE_UINT32_BE (data, u.i);
780 }
781
782 /**
783  * GST_WRITE_DOUBLE_LE:
784  * @data: memory location
785  * @num: value to store
786  *
787  * Store a 64 bit double value in little endian format into the memory buffer.
788  */
789 static inline void
790 GST_WRITE_DOUBLE_LE(guint8 *data, gdouble num)
791 {
792   union
793   {
794     guint64 i;
795     gdouble d;
796   } u;
797
798   u.d = num;
799   GST_WRITE_UINT64_LE (data, u.i);
800 }
801
802 /**
803  * GST_WRITE_DOUBLE_BE:
804  * @data: memory location
805  * @num: value to store
806  *
807  * Store a 64 bit double value in big endian format into the memory buffer.
808  */
809 static inline void
810 GST_WRITE_DOUBLE_BE(guint8 *data, gdouble num)
811 {
812   union
813   {
814     guint64 i;
815     gdouble d;
816   } u;
817
818   u.d = num;
819   GST_WRITE_UINT64_BE (data, u.i);
820 }
821
822 /* Miscellaneous utility macros */
823
824 /**
825  * GST_ROUND_UP_2:
826  * @num: integer value to round up
827  *
828  * Rounds an integer value up to the next multiple of 2.
829  */
830 #define GST_ROUND_UP_2(num)  (((num)+1)&~1)
831 /**
832  * GST_ROUND_UP_4:
833  * @num: integer value to round up
834  *
835  * Rounds an integer value up to the next multiple of 4.
836  */
837 #define GST_ROUND_UP_4(num)  (((num)+3)&~3)
838 /**
839  * GST_ROUND_UP_8:
840  * @num: integer value to round up
841  *
842  * Rounds an integer value up to the next multiple of 8.
843  */
844 #define GST_ROUND_UP_8(num)  (((num)+7)&~7)
845 /**
846  * GST_ROUND_UP_16:
847  * @num: integer value to round up
848  *
849  * Rounds an integer value up to the next multiple of 16.
850  */
851 #define GST_ROUND_UP_16(num) (((num)+15)&~15)
852 /**
853  * GST_ROUND_UP_32:
854  * @num: integer value to round up
855  *
856  * Rounds an integer value up to the next multiple of 32.
857  */
858 #define GST_ROUND_UP_32(num) (((num)+31)&~31)
859 /**
860  * GST_ROUND_UP_64:
861  * @num: integer value to round up
862  *
863  * Rounds an integer value up to the next multiple of 64.
864  */
865 #define GST_ROUND_UP_64(num) (((num)+63)&~63)
866 /**
867  * GST_ROUND_UP_128:
868  * @num: integer value to round up
869  *
870  * Rounds an integer value up to the next multiple of 128.
871  * Since: 1.4
872  */
873 #define GST_ROUND_UP_128(num) (((num)+127)&~127)
874 /**
875  * GST_ROUND_UP_N:
876  * @num: integrer value to round up
877  * @align: a power of two to round up to
878  *
879  * Rounds an integer value up to the next multiple of @align. @align MUST be a
880  * power of two.
881  */
882 #define GST_ROUND_UP_N(num,align) ((((num) + ((align) - 1)) & ~((align) - 1)))
883
884
885 /**
886  * GST_ROUND_DOWN_2:
887  * @num: integer value to round down
888  *
889  * Rounds an integer value down to the next multiple of 2.
890  */
891 #define GST_ROUND_DOWN_2(num)  ((num)&(~1))
892 /**
893  * GST_ROUND_DOWN_4:
894  * @num: integer value to round down
895  *
896  * Rounds an integer value down to the next multiple of 4.
897  */
898 #define GST_ROUND_DOWN_4(num)  ((num)&(~3))
899 /**
900  * GST_ROUND_DOWN_8:
901  * @num: integer value to round down
902  *
903  * Rounds an integer value down to the next multiple of 8.
904  */
905 #define GST_ROUND_DOWN_8(num)  ((num)&(~7))
906 /**
907  * GST_ROUND_DOWN_16:
908  * @num: integer value to round down
909  *
910  * Rounds an integer value down to the next multiple of 16.
911  */
912 #define GST_ROUND_DOWN_16(num) ((num)&(~15))
913 /**
914  * GST_ROUND_DOWN_32:
915  * @num: integer value to round down
916  *
917  * Rounds an integer value down to the next multiple of 32.
918  */
919 #define GST_ROUND_DOWN_32(num) ((num)&(~31))
920 /**
921  * GST_ROUND_DOWN_64:
922  * @num: integer value to round down
923  *
924  * Rounds an integer value down to the next multiple of 64.
925  */
926 #define GST_ROUND_DOWN_64(num) ((num)&(~63))
927 /**
928  * GST_ROUND_DOWN_128:
929  * @num: integer value to round down
930  *
931  * Rounds an integer value down to the next multiple of 128.
932  * Since: 1.4
933  */
934 #define GST_ROUND_DOWN_128(num) ((num)&(~127))
935 /**
936  * GST_ROUND_DOWN_N:
937  * @num: integrer value to round down
938  * @align: a power of two to round down to
939  *
940  * Rounds an integer value down to the next multiple of @align. @align MUST be a
941  * power of two.
942  */
943 #define GST_ROUND_DOWN_N(num,align) (((num) & ~((align) - 1)))
944
945
946 GST_EXPORT
947 void                    gst_object_default_error        (GstObject    * source,
948                                                          const GError * error,
949                                                          const gchar  * debug);
950
951 /* element functions */
952
953 GST_EXPORT
954 void                    gst_element_create_all_pads     (GstElement *element);
955
956 GST_EXPORT
957 GstPad*                 gst_element_get_compatible_pad  (GstElement *element, GstPad *pad,
958                                                          GstCaps *caps);
959 GST_EXPORT
960 GstPadTemplate*         gst_element_get_compatible_pad_template (GstElement *element, GstPadTemplate *compattempl);
961
962 GST_EXPORT
963 const gchar*            gst_element_state_get_name      (GstState state);
964
965 GST_EXPORT
966 const gchar *           gst_element_state_change_return_get_name (GstStateChangeReturn state_ret);
967
968 GST_EXPORT
969 gboolean                gst_element_link                (GstElement *src, GstElement *dest);
970
971 GST_EXPORT
972 gboolean                gst_element_link_many           (GstElement *element_1,
973                                                          GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
974 GST_EXPORT
975 gboolean                gst_element_link_filtered       (GstElement * src,
976                                                          GstElement * dest,
977                                                          GstCaps *filter);
978 GST_EXPORT
979 void                    gst_element_unlink              (GstElement *src, GstElement *dest);
980
981 GST_EXPORT
982 void                    gst_element_unlink_many         (GstElement *element_1,
983                                                          GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
984 GST_EXPORT
985 gboolean                gst_element_link_pads           (GstElement *src, const gchar *srcpadname,
986                                                          GstElement *dest, const gchar *destpadname);
987 GST_EXPORT
988 gboolean                gst_element_link_pads_full      (GstElement *src, const gchar *srcpadname,
989                                                          GstElement *dest, const gchar *destpadname,
990                                                          GstPadLinkCheck flags);
991 GST_EXPORT
992 void                    gst_element_unlink_pads         (GstElement *src, const gchar *srcpadname,
993                                                          GstElement *dest, const gchar *destpadname);
994 GST_EXPORT
995 gboolean                gst_element_link_pads_filtered  (GstElement * src, const gchar * srcpadname,
996                                                          GstElement * dest, const gchar * destpadname,
997                                                          GstCaps *filter);
998 GST_EXPORT
999 gboolean                gst_element_seek_simple         (GstElement   *element,
1000                                                          GstFormat     format,
1001                                                          GstSeekFlags  seek_flags,
1002                                                          gint64        seek_pos);
1003
1004 /* util elementfactory functions */
1005
1006 GST_EXPORT
1007 gboolean gst_element_factory_can_sink_all_caps (GstElementFactory *factory, const GstCaps *caps);
1008
1009 GST_EXPORT
1010 gboolean gst_element_factory_can_src_all_caps  (GstElementFactory *factory, const GstCaps *caps);
1011
1012 GST_EXPORT
1013 gboolean gst_element_factory_can_sink_any_caps (GstElementFactory *factory, const GstCaps *caps);
1014
1015 GST_EXPORT
1016 gboolean gst_element_factory_can_src_any_caps  (GstElementFactory *factory, const GstCaps *caps);
1017
1018 /* util query functions */
1019
1020 GST_EXPORT
1021 gboolean                gst_element_query_position      (GstElement *element, GstFormat format, gint64 *cur);
1022
1023 GST_EXPORT
1024 gboolean                gst_element_query_duration      (GstElement *element, GstFormat format, gint64 *duration);
1025
1026 GST_EXPORT
1027 gboolean                gst_element_query_convert       (GstElement *element, GstFormat src_format, gint64 src_val,
1028                                                          GstFormat dest_format, gint64 *dest_val);
1029
1030 /* pad functions */
1031
1032 GST_EXPORT
1033 void                    gst_pad_use_fixed_caps          (GstPad *pad);
1034
1035 GST_EXPORT
1036 GstElement*             gst_pad_get_parent_element      (GstPad *pad);
1037
1038 /* util query functions */
1039
1040 GST_EXPORT
1041 gboolean                gst_pad_proxy_query_accept_caps (GstPad *pad, GstQuery *query);
1042
1043 GST_EXPORT
1044 gboolean                gst_pad_proxy_query_caps        (GstPad *pad, GstQuery *query);
1045
1046 GST_EXPORT
1047 gboolean                gst_pad_query_position          (GstPad *pad, GstFormat format, gint64 *cur);
1048
1049 GST_EXPORT
1050 gboolean                gst_pad_query_duration          (GstPad *pad, GstFormat format, gint64 *duration);
1051
1052 GST_EXPORT
1053 gboolean                gst_pad_query_convert           (GstPad *pad, GstFormat src_format, gint64 src_val,
1054                                                          GstFormat dest_format, gint64 *dest_val);
1055 GST_EXPORT
1056 GstCaps *               gst_pad_query_caps              (GstPad *pad, GstCaps *filter);
1057
1058 GST_EXPORT
1059 gboolean                gst_pad_query_accept_caps       (GstPad *pad, GstCaps *caps);
1060
1061 GST_EXPORT
1062 gboolean                gst_pad_link_maybe_ghosting      (GstPad            *src,
1063                                                           GstPad            *sink);
1064 GST_EXPORT
1065 gboolean                gst_pad_link_maybe_ghosting_full (GstPad            *src,
1066                                                           GstPad            *sink,
1067                                                           GstPadLinkCheck   flags);
1068 GST_EXPORT
1069 gboolean                gst_pad_peer_query_position     (GstPad *pad, GstFormat format, gint64 *cur);
1070
1071 GST_EXPORT
1072 gboolean                gst_pad_peer_query_duration     (GstPad *pad, GstFormat format, gint64 *duration);
1073
1074 GST_EXPORT
1075 gboolean                gst_pad_peer_query_convert      (GstPad *pad, GstFormat src_format, gint64 src_val,
1076                                                          GstFormat dest_format, gint64 *dest_val);
1077 GST_EXPORT
1078 GstCaps *               gst_pad_peer_query_caps         (GstPad * pad, GstCaps *filter);
1079
1080 GST_EXPORT
1081 gboolean                gst_pad_peer_query_accept_caps  (GstPad * pad, GstCaps *caps);
1082
1083 GST_EXPORT
1084 gchar *                 gst_pad_create_stream_id               (GstPad * pad, GstElement * parent, const gchar *stream_id) G_GNUC_MALLOC;
1085
1086 GST_EXPORT
1087 gchar *                 gst_pad_create_stream_id_printf        (GstPad * pad, GstElement * parent, const gchar *stream_id, ...) G_GNUC_PRINTF (3, 4) G_GNUC_MALLOC;
1088
1089 GST_EXPORT
1090 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;
1091
1092 GST_EXPORT
1093 gchar *                 gst_pad_get_stream_id           (GstPad * pad);
1094
1095 GST_EXPORT
1096 GstStream *             gst_pad_get_stream              (GstPad * pad);
1097
1098 /* bin functions */
1099
1100 GST_EXPORT
1101 void                    gst_bin_add_many                (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
1102
1103 GST_EXPORT
1104 void                    gst_bin_remove_many             (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
1105
1106 GST_EXPORT
1107 GstPad *                gst_bin_find_unlinked_pad       (GstBin *bin, GstPadDirection direction);
1108
1109 GST_EXPORT
1110 gboolean                gst_bin_sync_children_states    (GstBin *bin);
1111
1112 /* parse utility functions */
1113
1114 GST_EXPORT
1115 GstElement *            gst_parse_bin_from_description      (const gchar     * bin_description,
1116                                                              gboolean          ghost_unlinked_pads,
1117                                                              GError         ** err);
1118 GST_EXPORT
1119 GstElement *            gst_parse_bin_from_description_full (const gchar     * bin_description,
1120                                                              gboolean          ghost_unlinked_pads,
1121                                                              GstParseContext * context,
1122                                                              GstParseFlags     flags,
1123                                                              GError         ** err);
1124 GST_EXPORT
1125 GstClockTime            gst_util_get_timestamp          (void);
1126
1127 /**
1128  * GstSearchMode:
1129  * @GST_SEARCH_MODE_EXACT : Only search for exact matches.
1130  * @GST_SEARCH_MODE_BEFORE: Search for an exact match or the element just before.
1131  * @GST_SEARCH_MODE_AFTER : Search for an exact match or the element just after.
1132  *
1133  * The different search modes.
1134  */
1135 typedef enum {
1136   GST_SEARCH_MODE_EXACT = 0,
1137   GST_SEARCH_MODE_BEFORE,
1138   GST_SEARCH_MODE_AFTER
1139 } GstSearchMode;
1140
1141 GST_EXPORT
1142 gpointer      gst_util_array_binary_search      (gpointer array, guint num_elements,
1143                                                  gsize element_size, GCompareDataFunc search_func,
1144                                                  GstSearchMode mode, gconstpointer search_data,
1145                                                  gpointer user_data);
1146
1147 /* fraction operations */
1148
1149 GST_EXPORT
1150 gint          gst_util_greatest_common_divisor  (gint a, gint b);
1151
1152 GST_EXPORT
1153 gint64        gst_util_greatest_common_divisor_int64 (gint64 a, gint64 b);
1154
1155 GST_EXPORT
1156 void          gst_util_fraction_to_double       (gint src_n, gint src_d, gdouble *dest);
1157
1158 GST_EXPORT
1159 void          gst_util_double_to_fraction       (gdouble src, gint *dest_n, gint *dest_d);
1160
1161 GST_EXPORT
1162 gboolean      gst_util_fraction_multiply        (gint a_n, gint a_d, gint b_n, gint b_d,
1163                                                  gint *res_n, gint *res_d);
1164 GST_EXPORT
1165 gboolean      gst_util_fraction_add             (gint a_n, gint a_d, gint b_n, gint b_d,
1166                                                  gint *res_n, gint *res_d);
1167 GST_EXPORT
1168 gint          gst_util_fraction_compare         (gint a_n, gint a_d, gint b_n, gint b_d);
1169
1170 GST_EXPORT
1171 gboolean      gst_calculate_linear_regression   (const GstClockTime * xy,
1172                                                  GstClockTime * temp, guint n,
1173                                                  GstClockTime * m_num, GstClockTime * m_denom,
1174                                                  GstClockTime * b, GstClockTime * xbase,
1175                                                  gdouble * r_squared);
1176
1177
1178 G_END_DECLS
1179
1180 #endif /* __GST_UTILS_H__ */