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