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