89dba5d08cf90c7bd8f7d68aca0e9fc053187354
[platform/upstream/gstreamer.git] / gst / gstutils.h
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2002 Thomas Vander Stichele <thomas@apestaart.org>
5  *
6  * gstutils.h: Header for various utility functions
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24
25 #ifndef __GST_UTILS_H__
26 #define __GST_UTILS_H__
27
28 #include <glib.h>
29 #include <gst/gstbin.h>
30 #include <gst/gstparse.h>
31
32 G_BEGIN_DECLS
33
34 void            gst_util_set_value_from_string  (GValue *value, const gchar *value_str);
35 void            gst_util_set_object_arg         (GObject *object, const gchar *name, const gchar *value);
36 void            gst_util_dump_mem               (const guchar *mem, guint size);
37
38 guint64         gst_util_gdouble_to_guint64     (gdouble value)  G_GNUC_PURE;
39 gdouble         gst_util_guint64_to_gdouble     (guint64 value)  G_GNUC_PURE;
40
41 /**
42  * gst_guint64_to_gdouble:
43  * @value: the #guint64 value to convert
44  *
45  * Convert @value to a gdouble.
46  *
47  * Returns: @value converted to a #gdouble.
48  */
49
50 /**
51  * gst_gdouble_to_guint64:
52  * @value: the #gdouble value to convert
53  *
54  * Convert @value to a guint64.
55  *
56  * Returns: @value converted to a #guint64.
57  */
58 #ifdef WIN32
59 #define         gst_gdouble_to_guint64(value)   gst_util_gdouble_to_guint64(value)
60 #define         gst_guint64_to_gdouble(value)   gst_util_guint64_to_gdouble(value)
61 #else
62 #define         gst_gdouble_to_guint64(value)   ((guint64) (value))
63 #define         gst_guint64_to_gdouble(value)   ((gdouble) (value))
64 #endif
65
66 guint64         gst_util_uint64_scale           (guint64 val, guint64 num, guint64 denom) G_GNUC_PURE;
67 guint64         gst_util_uint64_scale_round     (guint64 val, guint64 num, guint64 denom) G_GNUC_PURE;
68 guint64         gst_util_uint64_scale_ceil      (guint64 val, guint64 num, guint64 denom) G_GNUC_PURE;
69
70 guint64         gst_util_uint64_scale_int       (guint64 val, gint num, gint denom) G_GNUC_PURE;
71 guint64         gst_util_uint64_scale_int_round (guint64 val, gint num, gint denom) G_GNUC_PURE;
72 guint64         gst_util_uint64_scale_int_ceil  (guint64 val, gint num, gint denom) G_GNUC_PURE;
73
74 guint32         gst_util_seqnum_next            (void);
75 gint32          gst_util_seqnum_compare         (guint32 s1, guint32 s2);
76
77 void            gst_print_pad_caps              (GString *buf, gint indent, GstPad *pad);
78 void            gst_print_element_args          (GString *buf, gint indent, GstElement *element);
79
80
81 GType gst_type_register_static_full (GType parent_type,
82                                            const gchar       *type_name,
83                                guint              class_size,
84                                GBaseInitFunc      base_init,
85                                GBaseFinalizeFunc  base_finalize,
86                                GClassInitFunc     class_init,
87                                GClassFinalizeFunc class_finalize,
88                                gconstpointer      class_data,
89                                guint              instance_size,
90                                guint16            n_preallocs,
91                                GInstanceInitFunc  instance_init,
92                                const GTypeValueTable *value_table,
93                                GTypeFlags        flags);
94
95
96 /* Macros for defining classes.  Ideas taken from Bonobo, which took theirs
97    from Nautilus and GOB. */
98
99 /**
100  * GST_BOILERPLATE_FULL:
101  * @type: the name of the type struct
102  * @type_as_function: the prefix for the functions
103  * @parent_type: the parent type struct name
104  * @parent_type_macro: the parent type macro
105  * @additional_initializations: function pointer in the form of
106  * void additional_initializations (GType type) that can be used for
107  * initializing interfaces and the like
108  *
109  * Define the boilerplate type stuff to reduce typos and code size.  Defines
110  * the get_type method and the parent_class static variable.
111  *
112  * <informalexample>
113  * <programlisting>
114  *   GST_BOILERPLATE_FULL (GstFdSink, gst_fdsink, GstElement, GST_TYPE_ELEMENT, _do_init);
115  * </programlisting>
116  * </informalexample>
117  */
118 #define GST_BOILERPLATE_FULL(type, type_as_function, parent_type, parent_type_macro, additional_initializations)        \
119                                                                         \
120 static void type_as_function ## _base_init     (gpointer      g_class); \
121 static void type_as_function ## _class_init    (type ## Class *g_class);\
122 static void type_as_function ## _init          (type          *object,  \
123                                                 type ## Class *g_class);\
124 static parent_type ## Class *parent_class = NULL;                       \
125 static void                                                             \
126 type_as_function ## _class_init_trampoline (gpointer g_class,           \
127                                             gpointer data)              \
128 {                                                                       \
129   parent_class = (parent_type ## Class *)                               \
130       g_type_class_peek_parent (g_class);                               \
131   type_as_function ## _class_init ((type ## Class *)g_class);           \
132 }                                                                       \
133                                                                         \
134 GType type_as_function ## _get_type (void);                             \
135                                                                         \
136 GType                                                                   \
137 type_as_function ## _get_type (void)                                    \
138 {                                                                       \
139   /* The typedef for GType may be gulong or gsize, depending on the     \
140    * system and whether the compiler is c++ or not. The g_once_init_*   \
141    * functions always take a gsize * though ... */                      \
142   static volatile gsize gonce_data = 0;                                 \
143   if (g_once_init_enter (&gonce_data)) {                                \
144     GType _type;                                                        \
145     _type = gst_type_register_static_full (parent_type_macro,           \
146         g_intern_static_string (#type),                                 \
147         sizeof (type ## Class),                                         \
148         type_as_function ## _base_init,                                 \
149         NULL,             /* base_finalize */                           \
150         type_as_function ## _class_init_trampoline,                     \
151         NULL,             /* class_finalize */                          \
152         NULL,               /* class_data */                            \
153         sizeof (type),                                                  \
154         0,                  /* n_preallocs */                           \
155         (GInstanceInitFunc) type_as_function ## _init,                  \
156         NULL,                                                           \
157         (GTypeFlags) 0);                                                \
158     additional_initializations (_type);                                 \
159     g_once_init_leave (&gonce_data, (gsize) _type);                     \
160   }                                                                     \
161   return (GType) gonce_data;                                            \
162 }
163
164 #define __GST_DO_NOTHING(type)  /* NOP */
165
166 /**
167  * GST_BOILERPLATE:
168  * @type: the name of the type struct
169  * @type_as_function: the prefix for the functions
170  * @parent_type: the parent type struct name
171  * @parent_type_macro: the parent type macro
172  *
173  * Define the boilerplate type stuff to reduce typos and code size.  Defines
174  * the get_type method and the parent_class static variable.
175  *
176  * <informalexample>
177  * <programlisting>
178  *   GST_BOILERPLATE (GstFdSink, gst_fdsink, GstElement, GST_TYPE_ELEMENT);
179  * </programlisting>
180  * </informalexample>
181  */
182 #define GST_BOILERPLATE(type,type_as_function,parent_type,parent_type_macro)    \
183   GST_BOILERPLATE_FULL (type, type_as_function, parent_type, parent_type_macro, \
184       __GST_DO_NOTHING)
185
186 /* Like GST_BOILERPLATE, but makes the type 1) implement an interface, and 2)
187  * implement GstImplementsInterface for that type
188  *
189  * After this you will need to implement interface_as_function ## _supported
190  * and interface_as_function ## _interface_init
191  */
192 /**
193  * GST_BOILERPLATE_WITH_INTERFACE:
194  * @type: the name of the type struct
195  * @type_as_function: the prefix for the functions
196  * @parent_type: the parent type struct name
197  * @parent_type_as_macro: the parent type macro
198  * @interface_type: the name of the interface type struct
199  * @interface_type_as_macro: the interface type macro
200  * @interface_as_function: the interface function name prefix
201  *
202  * Like GST_BOILERPLATE, but makes the type 1) implement an interface, and 2)
203  * implement GstImplementsInterface for that type.
204  *
205  * After this you will need to implement interface_as_function ## _supported
206  * and interface_as_function ## _interface_init
207  */
208 #define GST_BOILERPLATE_WITH_INTERFACE(type, type_as_function,          \
209     parent_type, parent_type_as_macro, interface_type,                  \
210     interface_type_as_macro, interface_as_function)                     \
211                                                                         \
212 static void interface_as_function ## _interface_init (interface_type ## Class *klass);  \
213 static gboolean interface_as_function ## _supported (type *object, GType iface_type);   \
214                                                                         \
215 static void                                                             \
216 type_as_function ## _implements_interface_init (GstImplementsInterfaceClass *klass)     \
217 {                                                                       \
218   klass->supported = (gboolean (*)(GstImplementsInterface*, GType))interface_as_function ## _supported;     \
219 }                                                                       \
220                                                                         \
221 static void                                                             \
222 type_as_function ## _init_interfaces (GType type)                       \
223 {                                                                       \
224   static const GInterfaceInfo implements_iface_info = {                 \
225     (GInterfaceInitFunc) type_as_function ## _implements_interface_init,\
226     NULL,                                                               \
227     NULL,                                                               \
228   };                                                                    \
229   static const GInterfaceInfo iface_info = {                            \
230     (GInterfaceInitFunc) interface_as_function ## _interface_init,      \
231     NULL,                                                               \
232     NULL,                                                               \
233   };                                                                    \
234                                                                         \
235   g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,     \
236       &implements_iface_info);                                          \
237   g_type_add_interface_static (type, interface_type_as_macro,           \
238       &iface_info);                                                     \
239 }                                                                       \
240                                                                         \
241 GST_BOILERPLATE_FULL (type, type_as_function, parent_type,              \
242     parent_type_as_macro, type_as_function ## _init_interfaces)
243
244 /**
245  * GST_CALL_PARENT:
246  * @parent_class_cast: the name of the class cast macro for the parent type
247  * @name: name of the function to call
248  * @args: arguments enclosed in '( )'
249  *
250  * Just call the parent handler.  This assumes that there is a variable
251  * named parent_class that points to the (duh!) parent class.  Note that
252  * this macro is not to be used with things that return something, use
253  * the _WITH_DEFAULT version for that
254  */
255 #define GST_CALL_PARENT(parent_class_cast, name, args)                  \
256         ((parent_class_cast(parent_class)->name != NULL) ?              \
257          parent_class_cast(parent_class)->name args : (void) 0)
258
259 /**
260  * GST_CALL_PARENT_WITH_DEFAULT:
261  * @parent_class_cast: the name of the class cast macro for the parent type
262  * @name: name of the function to call
263  * @args: arguments enclosed in '( )'
264  * @def_return: default result
265  *
266  * Same as GST_CALL_PARENT(), but in case there is no implementation, it
267  * evaluates to @def_return.
268  */
269 #define GST_CALL_PARENT_WITH_DEFAULT(parent_class_cast, name, args, def_return)\
270         ((parent_class_cast(parent_class)->name != NULL) ?              \
271          parent_class_cast(parent_class)->name args : def_return)
272
273 /* Define PUT and GET functions for unaligned memory */
274 #define _GST_GET(__data, __idx, __size, __shift) \
275     (((guint##__size) (((guint8 *) (__data))[__idx])) << (__shift))
276
277 #define _GST_PUT(__data, __idx, __size, __shift, __num) \
278     (((guint8 *) (__data))[__idx] = (((guint##__size) (__num)) >> (__shift)) & 0xff)
279
280 /**
281  * GST_READ_UINT64_BE:
282  * @data: memory location
283  *
284  * Read a 64 bit unsigned integer value in big endian format from the memory buffer.
285  */
286 #define GST_READ_UINT64_BE(data)        (_GST_GET (data, 0, 64, 56) | \
287                                          _GST_GET (data, 1, 64, 48) | \
288                                          _GST_GET (data, 2, 64, 40) | \
289                                          _GST_GET (data, 3, 64, 32) | \
290                                          _GST_GET (data, 4, 64, 24) | \
291                                          _GST_GET (data, 5, 64, 16) | \
292                                          _GST_GET (data, 6, 64,  8) | \
293                                          _GST_GET (data, 7, 64,  0))
294
295 /**
296  * GST_READ_UINT64_LE:
297  * @data: memory location
298  *
299  * Read a 64 bit unsigned integer value in little endian format from the memory buffer.
300  */
301 #define GST_READ_UINT64_LE(data)        (_GST_GET (data, 7, 64, 56) | \
302                                          _GST_GET (data, 6, 64, 48) | \
303                                          _GST_GET (data, 5, 64, 40) | \
304                                          _GST_GET (data, 4, 64, 32) | \
305                                          _GST_GET (data, 3, 64, 24) | \
306                                          _GST_GET (data, 2, 64, 16) | \
307                                          _GST_GET (data, 1, 64,  8) | \
308                                          _GST_GET (data, 0, 64,  0))
309
310 /**
311  * GST_READ_UINT32_BE:
312  * @data: memory location
313  *
314  * Read a 32 bit unsigned integer value in big endian format from the memory buffer.
315  */
316 #define GST_READ_UINT32_BE(data)        (_GST_GET (data, 0, 32, 24) | \
317                                          _GST_GET (data, 1, 32, 16) | \
318                                          _GST_GET (data, 2, 32,  8) | \
319                                          _GST_GET (data, 3, 32,  0))
320
321 /**
322  * GST_READ_UINT32_LE:
323  * @data: memory location
324  *
325  * Read a 32 bit unsigned integer value in little endian format from the memory buffer.
326  */
327 #define GST_READ_UINT32_LE(data)        (_GST_GET (data, 3, 32, 24) | \
328                                          _GST_GET (data, 2, 32, 16) | \
329                                          _GST_GET (data, 1, 32,  8) | \
330                                          _GST_GET (data, 0, 32,  0))
331
332 /**
333  * GST_READ_UINT24_BE:
334  * @data: memory location
335  *
336  * Read a 24 bit unsigned integer value in big endian format from the memory buffer.
337  *
338  * Since: 0.10.22
339  */
340 #define GST_READ_UINT24_BE(data)        (_GST_GET (data, 0, 32, 16) | \
341                                          _GST_GET (data, 1, 32,  8) | \
342                                          _GST_GET (data, 2, 32,  0))
343
344 /**
345  * GST_READ_UINT24_LE:
346  * @data: memory location
347  *
348  * Read a 24 bit unsigned integer value in little endian format from the memory buffer.
349  *
350  * Since: 0.10.22
351  */
352 #define GST_READ_UINT24_LE(data)        (_GST_GET (data, 2, 32, 16) | \
353                                          _GST_GET (data, 1, 32,  8) | \
354                                          _GST_GET (data, 0, 32,  0))
355
356 /**
357  * GST_READ_UINT16_BE:
358  * @data: memory location
359  *
360  * Read a 16 bit unsigned integer value in big endian format from the memory buffer.
361  */
362 #define GST_READ_UINT16_BE(data)        (_GST_GET (data, 0, 16,  8) | \
363                                          _GST_GET (data, 1, 16,  0))
364
365 /**
366  * GST_READ_UINT16_LE:
367  * @data: memory location
368  *
369  * Read a 16 bit unsigned integer value in little endian format from the memory buffer.
370  */
371 #define GST_READ_UINT16_LE(data)        (_GST_GET (data, 1, 16,  8) | \
372                                          _GST_GET (data, 0, 16,  0))
373
374 /**
375  * GST_READ_UINT8:
376  * @data: memory location
377  *
378  * Read an 8 bit unsigned integer value from the memory buffer.
379  */
380 #define GST_READ_UINT8(data)            (_GST_GET (data, 0,  8,  0))
381
382 /**
383  * GST_WRITE_UINT64_BE:
384  * @data: memory location
385  * @num: value to store
386  *
387  * Store a 64 bit unsigned integer value in big endian format into the memory buffer.
388  */
389 #define GST_WRITE_UINT64_BE(data, num)  do { \
390                                           _GST_PUT (data, 0, 64, 56, num); \
391                                           _GST_PUT (data, 1, 64, 48, num); \
392                                           _GST_PUT (data, 2, 64, 40, num); \
393                                           _GST_PUT (data, 3, 64, 32, num); \
394                                           _GST_PUT (data, 4, 64, 24, num); \
395                                           _GST_PUT (data, 5, 64, 16, num); \
396                                           _GST_PUT (data, 6, 64,  8, num); \
397                                           _GST_PUT (data, 7, 64,  0, num); \
398                                         } while (0)
399
400 /**
401  * GST_WRITE_UINT64_LE:
402  * @data: memory location
403  * @num: value to store
404  *
405  * Store a 64 bit unsigned integer value in little endian format into the memory buffer.
406  */
407 #define GST_WRITE_UINT64_LE(data, num)  do { \
408                                           _GST_PUT (data, 0, 64,  0, num); \
409                                           _GST_PUT (data, 1, 64,  8, num); \
410                                           _GST_PUT (data, 2, 64, 16, num); \
411                                           _GST_PUT (data, 3, 64, 24, num); \
412                                           _GST_PUT (data, 4, 64, 32, num); \
413                                           _GST_PUT (data, 5, 64, 40, num); \
414                                           _GST_PUT (data, 6, 64, 48, num); \
415                                           _GST_PUT (data, 7, 64, 56, num); \
416                                         } while (0)
417
418 /**
419  * GST_WRITE_UINT32_BE:
420  * @data: memory location
421  * @num: value to store
422  *
423  * Store a 32 bit unsigned integer value in big endian format into the memory buffer.
424  */
425 #define GST_WRITE_UINT32_BE(data, num)  do { \
426                                           _GST_PUT (data, 0, 32, 24, num); \
427                                           _GST_PUT (data, 1, 32, 16, num); \
428                                           _GST_PUT (data, 2, 32,  8, num); \
429                                           _GST_PUT (data, 3, 32,  0, num); \
430                                         } while (0)
431
432 /**
433  * GST_WRITE_UINT32_LE:
434  * @data: memory location
435  * @num: value to store
436  *
437  * Store a 32 bit unsigned integer value in little endian format into the memory buffer.
438  */
439 #define GST_WRITE_UINT32_LE(data, num)  do { \
440                                           _GST_PUT (data, 0, 32,  0, num); \
441                                           _GST_PUT (data, 1, 32,  8, num); \
442                                           _GST_PUT (data, 2, 32, 16, num); \
443                                           _GST_PUT (data, 3, 32, 24, num); \
444                                         } while (0)
445
446 /**
447  * GST_WRITE_UINT24_BE:
448  * @data: memory location
449  * @num: value to store
450  *
451  * Store a 24 bit unsigned integer value in big endian format into the memory buffer.
452  *
453  * Since: 0.10.22
454  */
455 #define GST_WRITE_UINT24_BE(data, num)  do { \
456                                           _GST_PUT (data, 0, 32,  16, num); \
457                                           _GST_PUT (data, 1, 32,  8, num); \
458                                           _GST_PUT (data, 2, 32,  0, num); \
459                                         } while (0)
460
461 /**
462  * GST_WRITE_UINT24_LE:
463  * @data: memory location
464  * @num: value to store
465  *
466  * Store a 24 bit unsigned integer value in little endian format into the memory buffer.
467  *
468  * Since: 0.10.22
469  */
470 #define GST_WRITE_UINT24_LE(data, num)  do { \
471                                           _GST_PUT (data, 0, 32,  0, num); \
472                                           _GST_PUT (data, 1, 32,  8, num); \
473                                           _GST_PUT (data, 2, 32,  16, num); \
474                                         } while (0)
475
476 /**
477  * GST_WRITE_UINT16_BE:
478  * @data: memory location
479  * @num: value to store
480  *
481  * Store a 16 bit unsigned integer value in big endian format into the memory buffer.
482  */
483 #define GST_WRITE_UINT16_BE(data, num)  do { \
484                                           _GST_PUT (data, 0, 16,  8, num); \
485                                           _GST_PUT (data, 1, 16,  0, num); \
486                                         } while (0)
487
488 /**
489  * GST_WRITE_UINT16_LE:
490  * @data: memory location
491  * @num: value to store
492  *
493  * Store a 16 bit unsigned integer value in little endian format into the memory buffer.
494  */
495 #define GST_WRITE_UINT16_LE(data, num)  do { \
496                                           _GST_PUT (data, 0, 16,  0, num); \
497                                           _GST_PUT (data, 1, 16,  8, num); \
498                                         } while (0)
499
500 /**
501  * GST_WRITE_UINT8:
502  * @data: memory location
503  * @num: value to store
504  *
505  * Store an 8 bit unsigned integer value into the memory buffer.
506  */
507 #define GST_WRITE_UINT8(data, num)      do { \
508                                           _GST_PUT (data, 0,  8,  0, num); \
509                                         } while (0)
510
511 /* Float endianess conversion macros */
512
513 /* FIXME: Remove this once we depend on a GLib version with this */
514 #ifndef GFLOAT_FROM_LE
515 /**
516  * GFLOAT_SWAP_LE_BE:
517  * @in: input value
518  *
519  * Swap byte order of a 32-bit floating point value (float).
520  *
521  * Returns: @in byte-swapped.
522  *
523  * Since: 0.10.22
524  *
525  */
526 #ifdef _FOOL_GTK_DOC_
527 G_INLINE_FUNC gfloat GFLOAT_SWAP_LE_BE (gfloat in);
528 #endif
529
530 inline static gfloat
531 GFLOAT_SWAP_LE_BE(gfloat in)
532 {
533   union
534   {
535     guint32 i;
536     gfloat f;
537   } u;
538
539   u.f = in;
540   u.i = GUINT32_SWAP_LE_BE (u.i);
541   return u.f;
542 }
543
544 /**
545  * GDOUBLE_SWAP_LE_BE:
546  * @in: input value
547  *
548  * Swap byte order of a 64-bit floating point value (double).
549  *
550  * Returns: @in byte-swapped.
551  *
552  * Since: 0.10.22
553  *
554  */
555 #ifdef _FOOL_GTK_DOC_
556 G_INLINE_FUNC gdouble GDOUBLE_SWAP_LE_BE (gdouble in);
557 #endif
558
559 inline static gdouble
560 GDOUBLE_SWAP_LE_BE(gdouble in)
561 {
562   union
563   {
564     guint64 i;
565     gdouble d;
566   } u;
567
568   u.d = in;
569   u.i = GUINT64_SWAP_LE_BE (u.i);
570   return u.d;
571 }
572
573 /**
574  * GDOUBLE_TO_LE:
575  * @val: value
576  *
577  * Convert 64-bit floating point value (double) from native byte order into
578  * little endian byte order.
579  *
580  * Since: 0.10.22
581  *
582  */
583 /**
584  * GDOUBLE_TO_BE:
585  * @val: value
586  *
587  * Convert 64-bit floating point value (double) from native byte order into
588  * big endian byte order.
589  *
590  * Since: 0.10.22
591  *
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  * Since: 0.10.22
601  *
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  * Since: 0.10.22
611  *
612  */
613
614 /**
615  * GFLOAT_TO_LE:
616  * @val: value
617  *
618  * Convert 32-bit floating point value (float) from native byte order into
619  * little endian byte order.
620  *
621  * Since: 0.10.22
622  *
623  */
624 /**
625  * GFLOAT_TO_BE:
626  * @val: value
627  *
628  * Convert 32-bit floating point value (float) from native byte order into
629  * big endian byte order.
630  *
631  * Since: 0.10.22
632  *
633  */
634 /**
635  * GFLOAT_FROM_LE:
636  * @val: value
637  *
638  * Convert 32-bit floating point value (float) from little endian byte order
639  * into native byte order.
640  *
641  * Since: 0.10.22
642  *
643  */
644 /**
645  * GFLOAT_FROM_BE:
646  * @val: value
647  *
648  * Convert 32-bit floating point value (float) from big endian byte order
649  * into native byte order.
650  *
651  * Since: 0.10.22
652  *
653  */
654
655 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
656 #define GFLOAT_TO_LE(val)    ((gfloat) (val))
657 #define GFLOAT_TO_BE(val)    (GFLOAT_SWAP_LE_BE (val))
658 #define GDOUBLE_TO_LE(val)   ((gdouble) (val))
659 #define GDOUBLE_TO_BE(val)   (GDOUBLE_SWAP_LE_BE (val))
660
661 #elif G_BYTE_ORDER == G_BIG_ENDIAN
662 #define GFLOAT_TO_LE(val)    (GFLOAT_SWAP_LE_BE (val))
663 #define GFLOAT_TO_BE(val)    ((gfloat) (val))
664 #define GDOUBLE_TO_LE(val)   (GDOUBLE_SWAP_LE_BE (val))
665 #define GDOUBLE_TO_BE(val)   ((gdouble) (val))
666
667 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
668 #error unknown ENDIAN type
669 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
670
671 #define GFLOAT_FROM_LE(val)  (GFLOAT_TO_LE (val))
672 #define GFLOAT_FROM_BE(val)  (GFLOAT_TO_BE (val))
673 #define GDOUBLE_FROM_LE(val) (GDOUBLE_TO_LE (val))
674 #define GDOUBLE_FROM_BE(val) (GDOUBLE_TO_BE (val))
675
676 #endif /* !defined(GFLOAT_FROM_LE) */
677
678 /**
679  * GST_READ_FLOAT_LE:
680  * @data: memory location
681  *
682  * Read a 32 bit float value in little endian format from the memory buffer.
683  *
684  * Returns: The floating point value read from @data
685  *
686  * Since: 0.10.22
687  *
688  */
689 #ifdef _FOOL_GTK_DOC_
690 G_INLINE_FUNC gfloat GST_READ_FLOAT_LE (const guint8 *data);
691 #endif
692
693 inline static gfloat
694 GST_READ_FLOAT_LE(const guint8 *data)
695 {
696   union
697   {
698     guint32 i;
699     gfloat f;
700   } u;
701
702   u.i = GST_READ_UINT32_LE (data);
703   return u.f;
704 }
705
706 /**
707  * GST_READ_FLOAT_BE:
708  * @data: memory location
709  *
710  * Read a 32 bit float value in big endian format from the memory buffer.
711  *
712  * Returns: The floating point value read from @data
713  *
714  * Since: 0.10.22
715  *
716  */
717 #ifdef _FOOL_GTK_DOC_
718 G_INLINE_FUNC gfloat GST_READ_FLOAT_BE (const guint8 *data);
719 #endif
720
721 inline static gfloat
722 GST_READ_FLOAT_BE(const guint8 *data)
723 {
724   union
725   {
726     guint32 i;
727     gfloat f;
728   } u;
729
730   u.i = GST_READ_UINT32_BE (data);
731   return u.f;
732 }
733
734 /**
735  * GST_READ_DOUBLE_LE:
736  * @data: memory location
737  *
738  * Read a 64 bit double value in little endian format from the memory buffer.
739  *
740  * Returns: The double-precision floating point value read from @data
741  *
742  * Since: 0.10.22
743  *
744  */
745 #ifdef _FOOL_GTK_DOC_
746 G_INLINE_FUNC gdouble GST_READ_DOUBLE_LE (const guint8 *data);
747 #endif
748
749 inline static gdouble
750 GST_READ_DOUBLE_LE(const guint8 *data)
751 {
752   union
753   {
754     guint64 i;
755     gdouble d;
756   } u;
757
758   u.i = GST_READ_UINT64_LE (data);
759   return u.d;
760 }
761
762 /**
763  * GST_READ_DOUBLE_BE:
764  * @data: memory location
765  *
766  * Read a 64 bit double value in big endian format from the memory buffer.
767  *
768  * Returns: The double-precision floating point value read from @data
769  *
770  * Since: 0.10.22
771  *
772  */
773 #ifdef _FOOL_GTK_DOC_
774 G_INLINE_FUNC gdouble GST_READ_DOUBLE_BE (const guint8 *data);
775 #endif
776
777 inline static gdouble
778 GST_READ_DOUBLE_BE(const guint8 *data)
779 {
780   union
781   {
782     guint64 i;
783     gdouble d;
784   } u;
785
786   u.i = GST_READ_UINT64_BE (data);
787   return u.d;
788 }
789
790 /**
791  * GST_WRITE_FLOAT_LE:
792  * @data: memory location
793  * @num: value to store
794  *
795  * Store a 32 bit float value in little endian format into the memory buffer.
796  *
797  * Since: 0.10.22
798  *
799  */
800 #ifdef _FOOL_GTK_DOC_
801 G_INLINE_FUNC void GST_WRITE_FLOAT_LE (guint8 *data, gfloat num);
802 #endif
803
804 inline static void
805 GST_WRITE_FLOAT_LE(guint8 *data, gfloat num)
806 {
807   union
808   {
809     guint32 i;
810     gfloat f;
811   } u;
812
813   u.f = num;
814   GST_WRITE_UINT32_LE (data, u.i);
815 }
816
817 /**
818  * GST_WRITE_FLOAT_BE:
819  * @data: memory location
820  * @num: value to store
821  *
822  * Store a 32 bit float value in big endian format into the memory buffer.
823  *
824  * Since: 0.10.22
825  *
826  */
827 #ifdef _FOOL_GTK_DOC_
828 G_INLINE_FUNC void GST_WRITE_FLOAT_BE (guint8 *data, gfloat num);
829 #endif
830
831 inline static void
832 GST_WRITE_FLOAT_BE(guint8 *data, gfloat num)
833 {
834   union
835   {
836     guint32 i;
837     gfloat f;
838   } u;
839
840   u.f = num;
841   GST_WRITE_UINT32_BE (data, u.i);
842 }
843
844 /**
845  * GST_WRITE_DOUBLE_LE:
846  * @data: memory location
847  * @num: value to store
848  *
849  * Store a 64 bit double value in little endian format into the memory buffer.
850  *
851  * Since: 0.10.22
852  *
853  */
854 #ifdef _FOOL_GTK_DOC_
855 G_INLINE_FUNC void GST_WRITE_DOUBLE_LE (guint8 *data, gdouble num);
856 #endif
857
858 inline static void
859 GST_WRITE_DOUBLE_LE(guint8 *data, gdouble num)
860 {
861   union
862   {
863     guint64 i;
864     gdouble d;
865   } u;
866
867   u.d = num;
868   GST_WRITE_UINT64_LE (data, u.i);
869 }
870
871 /**
872  * GST_WRITE_DOUBLE_BE:
873  * @data: memory location
874  * @num: value to store
875  *
876  * Store a 64 bit double value in big endian format into the memory buffer.
877  *
878  * Since: 0.10.22
879  *
880  */
881 #ifdef _FOOL_GTK_DOC_
882 G_INLINE_FUNC void GST_WRITE_DOUBLE_BE (guint8 *data, gdouble num);
883 #endif
884
885 inline static void
886 GST_WRITE_DOUBLE_BE(guint8 *data, gdouble num)
887 {
888   union
889   {
890     guint64 i;
891     gdouble d;
892   } u;
893
894   u.d = num;
895   GST_WRITE_UINT64_BE (data, u.i);
896 }
897
898 /* Miscellaneous utility macros */
899
900 /**
901  * GST_ROUND_UP_2:
902  * @num: integer value to round up
903  *
904  * Rounds an integer value up to the next multiple of 2.
905  */
906 #define GST_ROUND_UP_2(num)  (((num)+1)&~1)
907 /**
908  * GST_ROUND_UP_4:
909  * @num: integer value to round up
910  *
911  * Rounds an integer value up to the next multiple of 4.
912  */
913 #define GST_ROUND_UP_4(num)  (((num)+3)&~3)
914 /**
915  * GST_ROUND_UP_8:
916  * @num: integer value to round up
917  *
918  * Rounds an integer value up to the next multiple of 8.
919  */
920 #define GST_ROUND_UP_8(num)  (((num)+7)&~7)
921 /**
922  * GST_ROUND_UP_16:
923  * @num: integer value to round up
924  *
925  * Rounds an integer value up to the next multiple of 16.
926  */
927 #define GST_ROUND_UP_16(num) (((num)+15)&~15)
928 /**
929  * GST_ROUND_UP_32:
930  * @num: integer value to round up
931  *
932  * Rounds an integer value up to the next multiple of 32.
933  */
934 #define GST_ROUND_UP_32(num) (((num)+31)&~31)
935 /**
936  * GST_ROUND_UP_64:
937  * @num: integer value to round up
938  *
939  * Rounds an integer value up to the next multiple of 64.
940  */
941 #define GST_ROUND_UP_64(num) (((num)+63)&~63)
942
943 /**
944  * GST_ROUND_DOWN_2:
945  * @num: integer value to round down
946  *
947  * Rounds an integer value down to the next multiple of 2.
948  *
949  * Since: 0.10.12
950  */
951 #define GST_ROUND_DOWN_2(num)  ((num)&(~1))
952 /**
953  * GST_ROUND_DOWN_4:
954  * @num: integer value to round down
955  *
956  * Rounds an integer value down to the next multiple of 4.
957  *
958  * Since: 0.10.12
959  */
960 #define GST_ROUND_DOWN_4(num)  ((num)&(~3))
961 /**
962  * GST_ROUND_DOWN_8:
963  * @num: integer value to round down
964  *
965  * Rounds an integer value down to the next multiple of 8.
966  *
967  * Since: 0.10.12
968  */
969 #define GST_ROUND_DOWN_8(num)  ((num)&(~7))
970 /**
971  * GST_ROUND_DOWN_16:
972  * @num: integer value to round down
973  *
974  * Rounds an integer value down to the next multiple of 16.
975  *
976  * Since: 0.10.12
977  */
978 #define GST_ROUND_DOWN_16(num) ((num)&(~15))
979 /**
980  * GST_ROUND_DOWN_32:
981  * @num: integer value to round down
982  *
983  * Rounds an integer value down to the next multiple of 32.
984  *
985  * Since: 0.10.12
986  */
987 #define GST_ROUND_DOWN_32(num) ((num)&(~31))
988 /**
989  * GST_ROUND_DOWN_64:
990  * @num: integer value to round down
991  *
992  * Rounds an integer value down to the next multiple of 64.
993  *
994  * Since: 0.10.12
995  */
996 #define GST_ROUND_DOWN_64(num) ((num)&(~63))
997
998 void                    gst_object_default_error        (GstObject * source,
999                                                          GError * error, gchar * debug);
1000
1001 /* element functions */
1002 void                    gst_element_create_all_pads     (GstElement *element);
1003 GstPad*                 gst_element_get_compatible_pad  (GstElement *element, GstPad *pad,
1004                                                          const GstCaps *caps);
1005
1006 GstPadTemplate*         gst_element_get_compatible_pad_template (GstElement *element, GstPadTemplate *compattempl);
1007
1008 G_CONST_RETURN gchar*   gst_element_state_get_name      (GstState state);
1009 G_CONST_RETURN gchar *  gst_element_state_change_return_get_name (GstStateChangeReturn state_ret);
1010
1011 gboolean                gst_element_link                (GstElement *src, GstElement *dest);
1012 gboolean                gst_element_link_many           (GstElement *element_1,
1013                                                          GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
1014 gboolean                gst_element_link_filtered       (GstElement * src,
1015                                                          GstElement * dest,
1016                                                          GstCaps *filter);
1017 void                    gst_element_unlink              (GstElement *src, GstElement *dest);
1018 void                    gst_element_unlink_many         (GstElement *element_1,
1019                                                          GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
1020
1021 gboolean                gst_element_link_pads           (GstElement *src, const gchar *srcpadname,
1022                                                          GstElement *dest, const gchar *destpadname);
1023 void                    gst_element_unlink_pads         (GstElement *src, const gchar *srcpadname,
1024                                                          GstElement *dest, const gchar *destpadname);
1025
1026 gboolean                gst_element_link_pads_filtered  (GstElement * src, const gchar * srcpadname,
1027                                                          GstElement * dest, const gchar * destpadname,
1028                                                          GstCaps *filter);
1029
1030 gboolean                gst_element_seek_simple         (GstElement   *element,
1031                                                          GstFormat     format,
1032                                                          GstSeekFlags  seek_flags,
1033                                                          gint64        seek_pos);
1034
1035 /* util elementfactory functions */
1036 gboolean                gst_element_factory_can_src_caps(GstElementFactory *factory, const GstCaps *caps);
1037 gboolean                gst_element_factory_can_sink_caps(GstElementFactory *factory, const GstCaps *caps);
1038
1039 /* util query functions */
1040 gboolean                gst_element_query_position      (GstElement *element, GstFormat *format,
1041                                                          gint64 *cur);
1042 gboolean                gst_element_query_duration      (GstElement *element, GstFormat *format,
1043                                                          gint64 *duration);
1044 gboolean                gst_element_query_convert       (GstElement *element, GstFormat src_format, gint64 src_val,
1045                                                          GstFormat *dest_format, gint64 *dest_val);
1046
1047 /* element class functions */
1048 void                    gst_element_class_install_std_props (GstElementClass * klass,
1049                                                          const gchar * first_name, ...) G_GNUC_NULL_TERMINATED;
1050
1051 /* pad functions */
1052 void                    gst_pad_use_fixed_caps          (GstPad *pad);
1053 GstCaps*                gst_pad_get_fixed_caps_func     (GstPad *pad);
1054 GstCaps*                gst_pad_proxy_getcaps           (GstPad * pad);
1055 gboolean                gst_pad_proxy_setcaps           (GstPad * pad, GstCaps * caps);
1056
1057 GstElement*             gst_pad_get_parent_element      (GstPad *pad);
1058
1059 /* util query functions */
1060 gboolean                gst_pad_query_position          (GstPad *pad, GstFormat *format,
1061                                                          gint64 *cur);
1062 gboolean                gst_pad_query_duration          (GstPad *pad, GstFormat *format,
1063                                                          gint64 *duration);
1064 gboolean                gst_pad_query_convert           (GstPad *pad, GstFormat src_format, gint64 src_val,
1065                                                          GstFormat *dest_format, gint64 *dest_val);
1066
1067 gboolean                gst_pad_query_peer_position     (GstPad *pad, GstFormat *format,
1068                                                          gint64 *cur);
1069 gboolean                gst_pad_query_peer_duration     (GstPad *pad, GstFormat *format,
1070                                                          gint64 *duration);
1071 gboolean                gst_pad_query_peer_convert      (GstPad *pad, GstFormat src_format, gint64 src_val,
1072                                                          GstFormat *dest_format, gint64 *dest_val);
1073
1074 /* bin functions */
1075 void                    gst_bin_add_many                (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
1076 void                    gst_bin_remove_many             (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
1077 GstPad *                gst_bin_find_unlinked_pad       (GstBin *bin, GstPadDirection direction);
1078 #ifndef GST_DISABLE_DEPRECATED
1079 GstPad *                gst_bin_find_unconnected_pad    (GstBin *bin, GstPadDirection direction);
1080 #endif
1081
1082 /* buffer functions */
1083 GstBuffer *             gst_buffer_merge                (GstBuffer * buf1, GstBuffer * buf2);
1084 GstBuffer *             gst_buffer_join                 (GstBuffer * buf1, GstBuffer * buf2);
1085 #ifndef GST_DISABLE_DEPRECATED
1086 void                    gst_buffer_stamp                (GstBuffer * dest, const GstBuffer * src);
1087 #endif /* GST_DISABLE_DEPRECATED */
1088
1089 /* atomic functions */
1090 #ifndef GST_DISABLE_DEPRECATED
1091 void                    gst_atomic_int_set              (gint * atomic_int, gint value);
1092 #endif
1093
1094 /* probes */
1095 gulong                  gst_pad_add_data_probe          (GstPad   * pad,
1096                                                          GCallback  handler,
1097                                                          gpointer   data);
1098
1099 gulong                  gst_pad_add_data_probe_full     (GstPad       * pad,
1100                                                          GCallback      handler,
1101                                                          gpointer       data,
1102                                                          GDestroyNotify notify);
1103
1104 void                    gst_pad_remove_data_probe       (GstPad * pad, guint handler_id);
1105
1106 gulong                  gst_pad_add_event_probe         (GstPad   * pad,
1107                                                          GCallback  handler,
1108                                                          gpointer   data);
1109
1110 gulong                  gst_pad_add_event_probe_full    (GstPad       * pad,
1111                                                          GCallback      handler,
1112                                                          gpointer       data,
1113                                                          GDestroyNotify notify);
1114
1115 void                    gst_pad_remove_event_probe      (GstPad * pad, guint handler_id);
1116
1117 gulong                  gst_pad_add_buffer_probe        (GstPad   * pad,
1118                                                          GCallback  handler,
1119                                                          gpointer   data);
1120
1121 gulong                  gst_pad_add_buffer_probe_full   (GstPad       * pad,
1122                                                          GCallback      handler,
1123                                                          gpointer       data,
1124                                                          GDestroyNotify notify);
1125
1126 void                    gst_pad_remove_buffer_probe     (GstPad * pad, guint handler_id);
1127
1128 /* tag emission utility functions */
1129 void                    gst_element_found_tags_for_pad  (GstElement * element,
1130                                                          GstPad * pad,
1131                                                          GstTagList * list);
1132 void                    gst_element_found_tags          (GstElement * element,
1133                                                          GstTagList * list);
1134
1135 /* parse utility functions */
1136 GstElement *            gst_parse_bin_from_description      (const gchar     * bin_description,
1137                                                              gboolean          ghost_unlinked_pads,
1138                                                              GError         ** err);
1139
1140 GstElement *            gst_parse_bin_from_description_full (const gchar     * bin_description,
1141                                                              gboolean          ghost_unlinked_pads,
1142                                                              GstParseContext * context,
1143                                                              GstParseFlags     flags,
1144                                                              GError         ** err);
1145
1146 GstClockTime            gst_util_get_timestamp          (void);
1147
1148 /**
1149  * GstSearchMode:
1150  * @GST_SEARCH_MODE_EXACT : Only search for exact matches.
1151  * @GST_SEARCH_MODE_BEFORE: Search for an exact match or the element just before.
1152  * @GST_SEARCH_MODE_AFTER : Search for an exact match or the element just after.
1153  *
1154  * The different search modes.
1155  *
1156  * Since: 0.10.23
1157  */
1158 typedef enum {
1159   GST_SEARCH_MODE_EXACT = 0,
1160   GST_SEARCH_MODE_BEFORE,
1161   GST_SEARCH_MODE_AFTER
1162 } GstSearchMode;
1163
1164 gpointer                gst_util_array_binary_search      (gpointer array, guint num_elements,
1165                                                            gsize element_size, GCompareDataFunc search_func,
1166                                                            GstSearchMode mode, gconstpointer search_data,
1167                                                            gpointer user_data);
1168
1169 gint gst_util_greatest_common_divisor (gint a, gint b);
1170 void gst_util_fraction_to_double (gint src_n, gint src_d, gdouble *dest);
1171 void gst_util_double_to_fraction (gdouble src, gint *dest_n, gint *dest_d);
1172 gboolean gst_util_fraction_multiply (gint a_n, gint a_d, gint b_n, gint b_d, gint *res_n, gint *res_d);
1173 gboolean gst_util_fraction_add (gint a_n, gint a_d, gint b_n, gint b_d, gint *res_n, gint *res_d);
1174
1175
1176 /* sink message event
1177  *
1178  * FIXME: This should be in gstevent.h but can't because
1179  * it needs GstMessage and this would introduce circular
1180  * header includes. And forward declarations of typedefs
1181  * are unfortunately not possible. The implementation of
1182  * these functions is in gstevent.c.
1183  */
1184 GstEvent*       gst_event_new_sink_message      (struct _GstMessage *msg);
1185 void            gst_event_parse_sink_message    (GstEvent *event, struct _GstMessage **msg);
1186
1187
1188 G_END_DECLS
1189
1190 #endif /* __GST_UTILS_H__ */