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