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>
6 * gstutils.h: Header for various utility functions
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.
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.
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.
25 #ifndef __GST_UTILS_H__
26 #define __GST_UTILS_H__
29 #include <gst/gstbin.h>
33 void gst_util_set_value_from_string (GValue *value, const gchar *value_str);
34 void gst_util_set_object_arg (GObject *object, const gchar *name, const gchar *value);
35 void gst_util_dump_mem (const guchar *mem, guint size);
37 guint64 gst_util_gdouble_to_guint64 (gdouble value);
38 gdouble gst_util_guint64_to_gdouble (guint64 value);
41 * gst_guint64_to_gdouble:
42 * @value: the #guint64 value to convert
44 * Convert @value to a gdouble.
46 * Returns: @value converted to a #gdouble.
50 * gst_gdouble_to_guint64:
51 * @value: the #gdouble value to convert
53 * Convert @value to a guint64.
55 * Returns: @value converted to a #guint64.
58 #define gst_gdouble_to_guint64(value) gst_util_gdouble_to_guint64(value)
59 #define gst_guint64_to_gdouble(value) gst_util_guint64_to_gdouble(value)
61 #define gst_gdouble_to_guint64(value) ((guint64) (value))
62 #define gst_guint64_to_gdouble(value) ((gdouble) (value))
65 guint64 gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom);
67 guint64 gst_util_uint64_scale_int (guint64 val, gint num, gint denom);
69 void gst_print_pad_caps (GString *buf, gint indent, GstPad *pad);
70 void gst_print_element_args (GString *buf, gint indent, GstElement *element);
73 GType gst_type_register_static_full (GType parent_type,
74 const gchar *type_name,
76 GBaseInitFunc base_init,
77 GBaseFinalizeFunc base_finalize,
78 GClassInitFunc class_init,
79 GClassFinalizeFunc class_finalize,
80 gconstpointer class_data,
83 GInstanceInitFunc instance_init,
84 const GTypeValueTable *value_table,
88 /* Macros for defining classes. Ideas taken from Bonobo, which took theirs
89 from Nautilus and GOB. */
92 * GST_BOILERPLATE_FULL:
93 * @type: the name of the type struct
94 * @type_as_function: the prefix for the functions
95 * @parent_type: the parent type struct name
96 * @parent_type_macro: the parent type macro
97 * @additional_initializations: function pointer in the form of
98 * void additional_initializations (GType type) that can be used for
99 * initializing interfaces and the like
101 * Define the boilerplate type stuff to reduce typos and code size. Defines
102 * the get_type method and the parent_class static variable.
106 * GST_BOILERPLATE_FULL (GstFdSink, gst_fdsink, GstElement, GST_TYPE_ELEMENT, _do_init);
110 #define GST_BOILERPLATE_FULL(type, type_as_function, parent_type, parent_type_macro, additional_initializations) \
112 static void type_as_function ## _base_init (gpointer g_class); \
113 static void type_as_function ## _class_init (type ## Class *g_class);\
114 static void type_as_function ## _init (type *object, \
115 type ## Class *g_class);\
116 static parent_type ## Class *parent_class = NULL; \
118 type_as_function ## _class_init_trampoline (gpointer g_class, \
121 parent_class = (parent_type ## Class *) \
122 g_type_class_peek_parent (g_class); \
123 type_as_function ## _class_init ((type ## Class *)g_class); \
126 GType type_as_function ## _get_type (void); \
129 type_as_function ## _get_type (void) \
131 static GType object_type = 0; \
132 if (G_UNLIKELY (object_type == 0)) { \
133 object_type = gst_type_register_static_full (parent_type_macro, #type, \
134 sizeof (type ## Class), \
135 type_as_function ## _base_init, \
136 NULL, /* base_finalize */ \
137 type_as_function ## _class_init_trampoline, \
138 NULL, /* class_finalize */ \
139 NULL, /* class_data */ \
141 0, /* n_preallocs */ \
142 (GInstanceInitFunc) type_as_function ## _init, \
145 additional_initializations (object_type); \
147 return object_type; \
150 #define __GST_DO_NOTHING(type) /* NOP */
154 * @type: the name of the type struct
155 * @type_as_function: the prefix for the functions
156 * @parent_type: the parent type struct name
157 * @parent_type_macro: the parent type macro
159 * Define the boilerplate type stuff to reduce typos and code size. Defines
160 * the get_type method and the parent_class static variable.
164 * GST_BOILERPLATE (GstFdSink, gst_fdsink, GstElement, GST_TYPE_ELEMENT);
168 #define GST_BOILERPLATE(type,type_as_function,parent_type,parent_type_macro) \
169 GST_BOILERPLATE_FULL (type, type_as_function, parent_type, parent_type_macro, \
172 /* Like GST_BOILERPLATE, but makes the type 1) implement an interface, and 2)
173 * implement GstImplementsInterface for that type
175 * After this you will need to implement interface_as_function ## _supported
176 * and interface_as_function ## _interface_init
179 * GST_BOILERPLATE_WITH_INTERFACE:
180 * @type: the name of the type struct
181 * @type_as_function: the prefix for the functions
182 * @parent_type: the parent type struct name
183 * @parent_type_as_macro: the parent type macro
184 * @interface_type: the name of the interface type struct
185 * @interface_type_as_macro: the interface type macro
186 * @interface_as_function: the interface function name prefix
188 * Like GST_BOILERPLATE, but makes the type 1) implement an interface, and 2)
189 * implement GstImplementsInterface for that type.
191 * After this you will need to implement interface_as_function ## _supported
192 * and interface_as_function ## _interface_init
194 #define GST_BOILERPLATE_WITH_INTERFACE(type, type_as_function, \
195 parent_type, parent_type_as_macro, interface_type, \
196 interface_type_as_macro, interface_as_function) \
198 static void interface_as_function ## _interface_init (interface_type ## Class *klass); \
199 static gboolean interface_as_function ## _supported (type *object, GType iface_type); \
202 type_as_function ## _implements_interface_init (GstImplementsInterfaceClass *klass) \
204 klass->supported = (gboolean (*)(GstImplementsInterface*, GType))interface_as_function ## _supported; \
208 type_as_function ## _init_interfaces (GType type) \
210 static const GInterfaceInfo implements_iface_info = { \
211 (GInterfaceInitFunc) type_as_function ## _implements_interface_init,\
215 static const GInterfaceInfo iface_info = { \
216 (GInterfaceInitFunc) interface_as_function ## _interface_init, \
221 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE, \
222 &implements_iface_info); \
223 g_type_add_interface_static (type, interface_type_as_macro, \
227 GST_BOILERPLATE_FULL (type, type_as_function, parent_type, \
228 parent_type_as_macro, type_as_function ## _init_interfaces)
232 * @parent_class_cast: the name of the class cast macro for the parent type
233 * @name: name of the function to call
234 * @args: arguments enclosed in '( )'
236 * Just call the parent handler. This assumes that there is a variable
237 * named parent_class that points to the (duh!) parent class. Note that
238 * this macro is not to be used with things that return something, use
239 * the _WITH_DEFAULT version for that
241 #define GST_CALL_PARENT(parent_class_cast, name, args) \
242 ((parent_class_cast(parent_class)->name != NULL) ? \
243 parent_class_cast(parent_class)->name args : (void) 0)
246 * GST_CALL_PARENT_WITH_DEFAULT:
247 * @parent_class_cast: the name of the class cast macro for the parent type
248 * @name: name of the function to call
249 * @args: arguments enclosed in '( )'
250 * @def_return: default result
252 * Same as GST_CALL_PARENT(), but in case there is no implementation, it
253 * evaluates to @def_return.
255 #define GST_CALL_PARENT_WITH_DEFAULT(parent_class_cast, name, args, def_return)\
256 ((parent_class_cast(parent_class)->name != NULL) ? \
257 parent_class_cast(parent_class)->name args : def_return)
259 /* Define possibly unaligned memory access method whether the type of
261 #if GST_HAVE_UNALIGNED_ACCESS
263 #define _GST_GET(__data, __size, __end) \
264 (GUINT##__size##_FROM_##__end (* ((guint##__size *) (__data))))
267 * GST_READ_UINT64_BE:
268 * @data: memory location
270 * Read a 64 bit unsigned integer value in big endian format from the memory buffer.
272 #define GST_READ_UINT64_BE(data) _GST_GET (data, 64, BE)
274 * GST_READ_UINT64_LE:
275 * @data: memory location
277 * Read a 64 bit unsigned integer value in little endian format from the memory buffer.
279 #define GST_READ_UINT64_LE(data) _GST_GET (data, 64, LE)
281 * GST_READ_UINT32_BE:
282 * @data: memory location
284 * Read a 32 bit unsigned integer value in big endian format from the memory buffer.
286 #define GST_READ_UINT32_BE(data) _GST_GET (data, 32, BE)
288 * GST_READ_UINT32_LE:
289 * @data: memory location
291 * Read a 32 bit unsigned integer value in little endian format from the memory buffer.
293 #define GST_READ_UINT32_LE(data) _GST_GET (data, 32, LE)
295 * GST_READ_UINT16_BE:
296 * @data: memory location
298 * Read a 16 bit unsigned integer value in big endian format from the memory buffer.
300 #define GST_READ_UINT16_BE(data) _GST_GET (data, 16, BE)
302 * GST_READ_UINT16_LE:
303 * @data: memory location
305 * Read a 16 bit unsigned integer value in little endian format from the memory buffer.
307 #define GST_READ_UINT16_LE(data) _GST_GET (data, 16, LE)
310 * @data: memory location
312 * Read an 8 bit unsigned integer value from the memory buffer.
314 #define GST_READ_UINT8(data) (* ((guint8 *) (data)))
316 #define _GST_PUT(__data, __size, __end, __num) \
317 ((* (guint##__size *) (__data)) = GUINT##__size##_TO_##__end (__num))
320 * GST_WRITE_UINT64_BE:
321 * @data: memory location
322 * @num: value to store
324 * Store a 64 bit unsigned integer value in big endian format into the memory buffer.
326 #define GST_WRITE_UINT64_BE(data, num) _GST_PUT(data, 64, BE, num)
328 * GST_WRITE_UINT64_LE:
329 * @data: memory location
330 * @num: value to store
332 * Store a 64 bit unsigned integer value in little endian format into the memory buffer.
334 #define GST_WRITE_UINT64_LE(data, num) _GST_PUT(data, 64, LE, num)
336 * GST_WRITE_UINT32_BE:
337 * @data: memory location
338 * @num: value to store
340 * Store a 32 bit unsigned integer value in big endian format into the memory buffer.
342 #define GST_WRITE_UINT32_BE(data, num) _GST_PUT(data, 32, BE, num)
344 * GST_WRITE_UINT32_LE:
345 * @data: memory location
346 * @num: value to store
348 * Store a 32 bit unsigned integer value in little endian format into the memory buffer.
350 #define GST_WRITE_UINT32_LE(data, num) _GST_PUT(data, 32, LE, num)
352 * GST_WRITE_UINT16_BE:
353 * @data: memory location
354 * @num: value to store
356 * Store a 16 bit unsigned integer value in big endian format into the memory buffer.
358 #define GST_WRITE_UINT16_BE(data, num) _GST_PUT(data, 16, BE, num)
360 * GST_WRITE_UINT16_LE:
361 * @data: memory location
362 * @num: value to store
364 * Store a 16 bit unsigned integer value in little endian format into the memory buffer.
366 #define GST_WRITE_UINT16_LE(data, num) _GST_PUT(data, 16, LE, num)
369 * @data: memory location
370 * @num: value to store
372 * Store an 8 bit unsigned integer value into the memory buffer.
374 #define GST_WRITE_UINT8(data, num) ((* (guint8 *) (data)) = (num))
376 #else /* GST_HAVE_UNALIGNED_ACCESS */
378 #define _GST_GET(__data, __idx, __size, __shift) \
379 (((guint##__size) (((guint8 *) (__data))[__idx])) << __shift)
381 #define GST_READ_UINT64_BE(data) (_GST_GET (data, 0, 64, 56) | \
382 _GST_GET (data, 1, 64, 48) | \
383 _GST_GET (data, 2, 64, 40) | \
384 _GST_GET (data, 3, 64, 32) | \
385 _GST_GET (data, 4, 64, 24) | \
386 _GST_GET (data, 5, 64, 16) | \
387 _GST_GET (data, 6, 64, 8) | \
388 _GST_GET (data, 7, 64, 0))
390 #define GST_READ_UINT64_LE(data) (_GST_GET (data, 7, 64, 56) | \
391 _GST_GET (data, 6, 64, 48) | \
392 _GST_GET (data, 5, 64, 40) | \
393 _GST_GET (data, 4, 64, 32) | \
394 _GST_GET (data, 3, 64, 24) | \
395 _GST_GET (data, 2, 64, 16) | \
396 _GST_GET (data, 1, 64, 8) | \
397 _GST_GET (data, 0, 64, 0))
399 #define GST_READ_UINT32_BE(data) (_GST_GET (data, 0, 32, 24) | \
400 _GST_GET (data, 1, 32, 16) | \
401 _GST_GET (data, 2, 32, 8) | \
402 _GST_GET (data, 3, 32, 0))
404 #define GST_READ_UINT32_LE(data) (_GST_GET (data, 3, 32, 24) | \
405 _GST_GET (data, 2, 32, 16) | \
406 _GST_GET (data, 1, 32, 8) | \
407 _GST_GET (data, 0, 32, 0))
409 #define GST_READ_UINT16_BE(data) (_GST_GET (data, 0, 16, 8) | \
410 _GST_GET (data, 1, 16, 0))
412 #define GST_READ_UINT16_LE(data) (_GST_GET (data, 1, 16, 8) | \
413 _GST_GET (data, 0, 16, 0))
415 #define GST_READ_UINT8(data) (_GST_GET (data, 0, 8, 0))
417 #define _GST_PUT(__data, __idx, __size, __shift, __num) \
418 (((guint8 *) (__data))[__idx] = (((guint##__size) __num) >> __shift) & 0xff)
420 #define GST_WRITE_UINT64_BE(data, num) do { \
421 _GST_PUT (data, 0, 64, 56, num); \
422 _GST_PUT (data, 1, 64, 48, num); \
423 _GST_PUT (data, 2, 64, 40, num); \
424 _GST_PUT (data, 3, 64, 32, num); \
425 _GST_PUT (data, 4, 64, 24, num); \
426 _GST_PUT (data, 5, 64, 16, num); \
427 _GST_PUT (data, 6, 64, 8, num); \
428 _GST_PUT (data, 7, 64, 0, num); \
431 #define GST_WRITE_UINT64_LE(data, num) do { \
432 _GST_PUT (data, 0, 64, 0, num); \
433 _GST_PUT (data, 1, 64, 8, num); \
434 _GST_PUT (data, 2, 64, 16, num); \
435 _GST_PUT (data, 3, 64, 24, num); \
436 _GST_PUT (data, 4, 64, 32, num); \
437 _GST_PUT (data, 5, 64, 40, num); \
438 _GST_PUT (data, 6, 64, 48, num); \
439 _GST_PUT (data, 7, 64, 56, num); \
442 #define GST_WRITE_UINT32_BE(data, num) do { \
443 _GST_PUT (data, 0, 32, 24, num); \
444 _GST_PUT (data, 1, 32, 16, num); \
445 _GST_PUT (data, 2, 32, 8, num); \
446 _GST_PUT (data, 3, 32, 0, num); \
449 #define GST_WRITE_UINT32_LE(data, num) do { \
450 _GST_PUT (data, 0, 32, 0, num); \
451 _GST_PUT (data, 1, 32, 8, num); \
452 _GST_PUT (data, 2, 32, 16, num); \
453 _GST_PUT (data, 3, 32, 24, num); \
456 #define GST_WRITE_UINT16_BE(data, num) do { \
457 _GST_PUT (data, 0, 16, 8, num); \
458 _GST_PUT (data, 1, 16, 0, num); \
461 #define GST_WRITE_UINT16_LE(data, num) do { \
462 _GST_PUT (data, 0, 16, 0, num); \
463 _GST_PUT (data, 1, 16, 8, num); \
466 #define GST_WRITE_UINT8(data, num) do { \
467 _GST_PUT (data, 0, 8, 0, num); \
470 #endif /* GST_HAVE_UNALIGNED_ACCESS */
473 /* Miscellaneous utility macros */
477 * @num: integer value to round up
479 * Rounds an integer value up to the next multiple of 2.
481 #define GST_ROUND_UP_2(num) (((num)+1)&~1)
484 * @num: integer value to round up
486 * Rounds an integer value up to the next multiple of 4.
488 #define GST_ROUND_UP_4(num) (((num)+3)&~3)
491 * @num: integer value to round up
493 * Rounds an integer value up to the next multiple of 8.
495 #define GST_ROUND_UP_8(num) (((num)+7)&~7)
498 * @num: integer value to round up
500 * Rounds an integer value up to the next multiple of 16.
502 #define GST_ROUND_UP_16(num) (((num)+15)&~15)
505 * @num: integer value to round up
507 * Rounds an integer value up to the next multiple of 32.
509 #define GST_ROUND_UP_32(num) (((num)+31)&~31)
512 * @num: integer value to round up
514 * Rounds an integer value up to the next multiple of 64.
516 #define GST_ROUND_UP_64(num) (((num)+63)&~63)
520 * @num: integer value to round down
522 * Rounds an integer value down to the next multiple of 2.
526 #define GST_ROUND_DOWN_2(num) ((num)&(~1))
529 * @num: integer value to round down
531 * Rounds an integer value down to the next multiple of 4.
535 #define GST_ROUND_DOWN_4(num) ((num)&(~3))
538 * @num: integer value to round down
540 * Rounds an integer value down to the next multiple of 8.
544 #define GST_ROUND_DOWN_8(num) ((num)&(~7))
547 * @num: integer value to round down
549 * Rounds an integer value down to the next multiple of 16.
553 #define GST_ROUND_DOWN_16(num) ((num)&(~15))
556 * @num: integer value to round down
558 * Rounds an integer value down to the next multiple of 32.
562 #define GST_ROUND_DOWN_32(num) ((num)&(~31))
565 * @num: integer value to round down
567 * Rounds an integer value down to the next multiple of 64.
571 #define GST_ROUND_DOWN_64(num) ((num)&(~63))
573 void gst_object_default_error (GstObject * source,
574 GError * error, gchar * debug);
576 /* element functions */
577 void gst_element_create_all_pads (GstElement *element);
578 GstPad* gst_element_get_compatible_pad (GstElement *element, GstPad *pad,
579 const GstCaps *caps);
581 GstPadTemplate* gst_element_get_compatible_pad_template (GstElement *element, GstPadTemplate *compattempl);
583 G_CONST_RETURN gchar* gst_element_state_get_name (GstState state);
584 G_CONST_RETURN gchar * gst_element_state_change_return_get_name (GstStateChangeReturn state_ret);
586 gboolean gst_element_link (GstElement *src, GstElement *dest);
587 gboolean gst_element_link_many (GstElement *element_1,
588 GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
589 gboolean gst_element_link_filtered (GstElement * src,
592 void gst_element_unlink (GstElement *src, GstElement *dest);
593 void gst_element_unlink_many (GstElement *element_1,
594 GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
596 gboolean gst_element_link_pads (GstElement *src, const gchar *srcpadname,
597 GstElement *dest, const gchar *destpadname);
598 void gst_element_unlink_pads (GstElement *src, const gchar *srcpadname,
599 GstElement *dest, const gchar *destpadname);
601 gboolean gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
602 GstElement * dest, const gchar * destpadname,
605 gboolean gst_element_seek_simple (GstElement *element,
607 GstSeekFlags seek_flags,
610 /* util elementfactory functions */
611 gboolean gst_element_factory_can_src_caps(GstElementFactory *factory, const GstCaps *caps);
612 gboolean gst_element_factory_can_sink_caps(GstElementFactory *factory, const GstCaps *caps);
614 /* util query functions */
615 gboolean gst_element_query_position (GstElement *element, GstFormat *format,
617 gboolean gst_element_query_duration (GstElement *element, GstFormat *format,
619 gboolean gst_element_query_convert (GstElement *element, GstFormat src_format, gint64 src_val,
620 GstFormat *dest_format, gint64 *dest_val);
622 /* element class functions */
623 void gst_element_class_install_std_props (GstElementClass * klass,
624 const gchar * first_name, ...) G_GNUC_NULL_TERMINATED;
627 gboolean gst_pad_can_link (GstPad *srcpad, GstPad *sinkpad);
629 void gst_pad_use_fixed_caps (GstPad *pad);
630 GstCaps* gst_pad_get_fixed_caps_func (GstPad *pad);
631 GstCaps* gst_pad_proxy_getcaps (GstPad * pad);
632 gboolean gst_pad_proxy_setcaps (GstPad * pad, GstCaps * caps);
634 GstElement* gst_pad_get_parent_element (GstPad *pad);
636 /* util query functions */
637 gboolean gst_pad_query_position (GstPad *pad, GstFormat *format,
639 gboolean gst_pad_query_duration (GstPad *pad, GstFormat *format,
641 gboolean gst_pad_query_convert (GstPad *pad, GstFormat src_format, gint64 src_val,
642 GstFormat *dest_format, gint64 *dest_val);
644 gboolean gst_pad_query_peer_position (GstPad *pad, GstFormat *format,
646 gboolean gst_pad_query_peer_duration (GstPad *pad, GstFormat *format,
648 gboolean gst_pad_query_peer_convert (GstPad *pad, GstFormat src_format, gint64 src_val,
649 GstFormat *dest_format, gint64 *dest_val);
652 void gst_bin_add_many (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
653 void gst_bin_remove_many (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
654 GstPad * gst_bin_find_unconnected_pad (GstBin *bin, GstPadDirection direction);
656 /* buffer functions */
657 GstBuffer * gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2);
658 GstBuffer * gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2);
659 #ifndef GST_DISABLE_DEPRECATED
660 void gst_buffer_stamp (GstBuffer * dest, const GstBuffer * src);
661 #endif /* GST_DISABLE_DEPRECATED */
663 /* atomic functions */
664 #ifndef GST_DISABLE_DEPRECATED
665 void gst_atomic_int_set (gint * atomic_int, gint value);
669 gulong gst_pad_add_data_probe (GstPad * pad,
673 gulong gst_pad_add_data_probe_full (GstPad * pad,
676 GDestroyNotify notify);
678 void gst_pad_remove_data_probe (GstPad * pad, guint handler_id);
680 gulong gst_pad_add_event_probe (GstPad * pad,
684 gulong gst_pad_add_event_probe_full (GstPad * pad,
687 GDestroyNotify notify);
689 void gst_pad_remove_event_probe (GstPad * pad, guint handler_id);
691 gulong gst_pad_add_buffer_probe (GstPad * pad,
695 gulong gst_pad_add_buffer_probe_full (GstPad * pad,
698 GDestroyNotify notify);
700 void gst_pad_remove_buffer_probe (GstPad * pad, guint handler_id);
702 /* tag emission utility functions */
703 void gst_element_found_tags_for_pad (GstElement * element,
706 void gst_element_found_tags (GstElement * element,
709 /* parse utility functions */
710 GstElement * gst_parse_bin_from_description (const gchar * bin_description,
711 gboolean ghost_unconnected_pads,
714 GstClockTime gst_util_get_timestamp (void);
718 #endif /* __GST_UTILS_H__ */