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 /* Macros for defining classes. Ideas taken from Bonobo, which took theirs
74 from Nautilus and GOB. */
77 * GST_BOILERPLATE_FULL:
78 * @type: the name of the type struct
79 * @type_as_function: the prefix for the functions
80 * @parent_type: the parent type struct name
81 * @parent_type_macro: the parent type macro
82 * @additional_initializations: function pointer in the form of
83 * void additional_initializations (GType type) that can be used for
84 * initializing interfaces and the like
86 * Define the boilerplate type stuff to reduce typos and code size. Defines
87 * the get_type method and the parent_class static variable.
91 * GST_BOILERPLATE_FULL (GstFdSink, gst_fdsink, GstElement, GST_TYPE_ELEMENT, _do_init);
95 #define GST_BOILERPLATE_FULL(type, type_as_function, parent_type, parent_type_macro, additional_initializations) \
97 static void type_as_function ## _base_init (gpointer g_class); \
98 static void type_as_function ## _class_init (type ## Class *g_class);\
99 static void type_as_function ## _init (type *object, \
100 type ## Class *g_class);\
101 static parent_type ## Class *parent_class = NULL; \
103 type_as_function ## _class_init_trampoline (gpointer g_class, \
106 parent_class = (parent_type ## Class *) \
107 g_type_class_peek_parent (g_class); \
108 type_as_function ## _class_init ((type ## Class *)g_class); \
111 GType type_as_function ## _get_type (void); \
114 type_as_function ## _get_type (void) \
116 static GType object_type = 0; \
117 if (G_UNLIKELY (object_type == 0)) { \
118 static const GTypeInfo object_info = { \
119 sizeof (type ## Class), \
120 type_as_function ## _base_init, \
121 NULL, /* base_finalize */ \
122 type_as_function ## _class_init_trampoline, \
123 NULL, /* class_finalize */ \
124 NULL, /* class_data */ \
126 0, /* n_preallocs */ \
127 (GInstanceInitFunc) type_as_function ## _init \
129 object_type = g_type_register_static (parent_type_macro, #type, \
130 &object_info, (GTypeFlags) 0); \
131 additional_initializations (object_type); \
133 return object_type; \
136 #define __GST_DO_NOTHING(type) /* NOP */
140 * @type: the name of the type struct
141 * @type_as_function: the prefix for the functions
142 * @parent_type: the parent type struct name
143 * @parent_type_macro: the parent type macro
145 * Define the boilerplate type stuff to reduce typos and code size. Defines
146 * the get_type method and the parent_class static variable.
150 * GST_BOILERPLATE (GstFdSink, gst_fdsink, GstElement, GST_TYPE_ELEMENT);
154 #define GST_BOILERPLATE(type,type_as_function,parent_type,parent_type_macro) \
155 GST_BOILERPLATE_FULL (type, type_as_function, parent_type, parent_type_macro, \
158 /* Like GST_BOILERPLATE, but makes the type 1) implement an interface, and 2)
159 * implement GstImplementsInterface for that type
161 * After this you will need to implement interface_as_function ## _supported
162 * and interface_as_function ## _interface_init
165 * GST_BOILERPLATE_WITH_INTERFACE:
166 * @type: the name of the type struct
167 * @type_as_function: the prefix for the functions
168 * @parent_type: the parent type struct name
169 * @parent_type_as_macro: the parent type macro
170 * @interface_type: the name of the interface type struct
171 * @interface_type_as_macro: the interface type macro
172 * @interface_as_function: the interface function name prefix
174 * Like GST_BOILERPLATE, but makes the type 1) implement an interface, and 2)
175 * implement GstImplementsInterface for that type.
177 * After this you will need to implement interface_as_function ## _supported
178 * and interface_as_function ## _interface_init
180 #define GST_BOILERPLATE_WITH_INTERFACE(type, type_as_function, \
181 parent_type, parent_type_as_macro, interface_type, \
182 interface_type_as_macro, interface_as_function) \
184 static void interface_as_function ## _interface_init (interface_type ## Class *klass); \
185 static gboolean interface_as_function ## _supported (type *object, GType iface_type); \
188 type_as_function ## _implements_interface_init (GstImplementsInterfaceClass *klass) \
190 klass->supported = (gboolean (*)(GstImplementsInterface*, GType))interface_as_function ## _supported; \
194 type_as_function ## _init_interfaces (GType type) \
196 static const GInterfaceInfo implements_iface_info = { \
197 (GInterfaceInitFunc) type_as_function ## _implements_interface_init,\
201 static const GInterfaceInfo iface_info = { \
202 (GInterfaceInitFunc) interface_as_function ## _interface_init, \
207 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE, \
208 &implements_iface_info); \
209 g_type_add_interface_static (type, interface_type_as_macro, \
213 GST_BOILERPLATE_FULL (type, type_as_function, parent_type, \
214 parent_type_as_macro, type_as_function ## _init_interfaces)
218 * @parent_class_cast: the name of the class cast macro for the parent type
219 * @name: name of the function to call
220 * @args: arguments enclosed in '( )'
222 * Just call the parent handler. This assumes that there is a variable
223 * named parent_class that points to the (duh!) parent class. Note that
224 * this macro is not to be used with things that return something, use
225 * the _WITH_DEFAULT version for that
227 #define GST_CALL_PARENT(parent_class_cast, name, args) \
228 ((parent_class_cast(parent_class)->name != NULL) ? \
229 parent_class_cast(parent_class)->name args : (void) 0)
232 * GST_CALL_PARENT_WITH_DEFAULT:
233 * @parent_class_cast: the name of the class cast macro for the parent type
234 * @name: name of the function to call
235 * @args: arguments enclosed in '( )'
236 * @def_return: default result
238 * Same as GST_CALL_PARENT(), but in case there is no implementation, it
239 * evaluates to @def_return.
241 #define GST_CALL_PARENT_WITH_DEFAULT(parent_class_cast, name, args, def_return)\
242 ((parent_class_cast(parent_class)->name != NULL) ? \
243 parent_class_cast(parent_class)->name args : def_return)
245 /* Define possibly unaligned memory access method whether the type of
247 #if GST_HAVE_UNALIGNED_ACCESS
249 #define _GST_GET(__data, __size, __end) \
250 (GUINT##__size##_FROM_##__end (* ((guint##__size *) (__data))))
253 * GST_READ_UINT64_BE:
254 * @data: memory location
256 * Read a 64 bit unsigned integer value in big endian format from the memory buffer.
258 #define GST_READ_UINT64_BE(data) _GST_GET (data, 64, BE)
260 * GST_READ_UINT64_LE:
261 * @data: memory location
263 * Read a 64 bit unsigned integer value in little endian format from the memory buffer.
265 #define GST_READ_UINT64_LE(data) _GST_GET (data, 64, LE)
267 * GST_READ_UINT32_BE:
268 * @data: memory location
270 * Read a 32 bit unsigned integer value in big endian format from the memory buffer.
272 #define GST_READ_UINT32_BE(data) _GST_GET (data, 32, BE)
274 * GST_READ_UINT32_LE:
275 * @data: memory location
277 * Read a 32 bit unsigned integer value in little endian format from the memory buffer.
279 #define GST_READ_UINT32_LE(data) _GST_GET (data, 32, LE)
281 * GST_READ_UINT16_BE:
282 * @data: memory location
284 * Read a 16 bit unsigned integer value in big endian format from the memory buffer.
286 #define GST_READ_UINT16_BE(data) _GST_GET (data, 16, BE)
288 * GST_READ_UINT16_LE:
289 * @data: memory location
291 * Read a 16 bit unsigned integer value in little endian format from the memory buffer.
293 #define GST_READ_UINT16_LE(data) _GST_GET (data, 16, LE)
296 * @data: memory location
298 * Read an 8 bit unsigned integer value from the memory buffer.
300 #define GST_READ_UINT8(data) (* ((guint8 *) (data)))
302 #define _GST_PUT(__data, __size, __end, __num) \
303 ((* (guint##__size *) (__data)) = GUINT##__size##_TO_##__end (__num))
306 * GST_WRITE_UINT64_BE:
307 * @data: memory location
308 * @num: value to store
310 * Store a 64 bit unsigned integer value in big endian format into the memory buffer.
312 #define GST_WRITE_UINT64_BE(data, num) _GST_PUT(data, 64, BE, num)
314 * GST_WRITE_UINT64_LE:
315 * @data: memory location
316 * @num: value to store
318 * Store a 64 bit unsigned integer value in little endian format into the memory buffer.
320 #define GST_WRITE_UINT64_LE(data, num) _GST_PUT(data, 64, LE, num)
322 * GST_WRITE_UINT32_BE:
323 * @data: memory location
324 * @num: value to store
326 * Store a 32 bit unsigned integer value in big endian format into the memory buffer.
328 #define GST_WRITE_UINT32_BE(data, num) _GST_PUT(data, 32, BE, num)
330 * GST_WRITE_UINT32_LE:
331 * @data: memory location
332 * @num: value to store
334 * Store a 32 bit unsigned integer value in little endian format into the memory buffer.
336 #define GST_WRITE_UINT32_LE(data, num) _GST_PUT(data, 32, LE, num)
338 * GST_WRITE_UINT16_BE:
339 * @data: memory location
340 * @num: value to store
342 * Store a 16 bit unsigned integer value in big endian format into the memory buffer.
344 #define GST_WRITE_UINT16_BE(data, num) _GST_PUT(data, 16, BE, num)
346 * GST_WRITE_UINT16_LE:
347 * @data: memory location
348 * @num: value to store
350 * Store a 16 bit unsigned integer value in little endian format into the memory buffer.
352 #define GST_WRITE_UINT16_LE(data, num) _GST_PUT(data, 16, LE, num)
355 * @data: memory location
356 * @num: value to store
358 * Store an 8 bit unsigned integer value into the memory buffer.
360 #define GST_WRITE_UINT8(data, num) ((* (guint8 *) (data)) = (num))
362 #else /* GST_HAVE_UNALIGNED_ACCESS */
364 #define _GST_GET(__data, __idx, __size, __shift) \
365 (((guint##__size) (((guint8 *) (__data))[__idx])) << __shift)
367 #define GST_READ_UINT64_BE(data) (_GST_GET (data, 0, 64, 56) | \
368 _GST_GET (data, 1, 64, 48) | \
369 _GST_GET (data, 2, 64, 40) | \
370 _GST_GET (data, 3, 64, 32) | \
371 _GST_GET (data, 4, 64, 24) | \
372 _GST_GET (data, 5, 64, 16) | \
373 _GST_GET (data, 6, 64, 8) | \
374 _GST_GET (data, 7, 64, 0))
376 #define GST_READ_UINT64_LE(data) (_GST_GET (data, 7, 64, 56) | \
377 _GST_GET (data, 6, 64, 48) | \
378 _GST_GET (data, 5, 64, 40) | \
379 _GST_GET (data, 4, 64, 32) | \
380 _GST_GET (data, 3, 64, 24) | \
381 _GST_GET (data, 2, 64, 16) | \
382 _GST_GET (data, 1, 64, 8) | \
383 _GST_GET (data, 0, 64, 0))
385 #define GST_READ_UINT32_BE(data) (_GST_GET (data, 0, 32, 24) | \
386 _GST_GET (data, 1, 32, 16) | \
387 _GST_GET (data, 2, 32, 8) | \
388 _GST_GET (data, 3, 32, 0))
390 #define GST_READ_UINT32_LE(data) (_GST_GET (data, 3, 32, 24) | \
391 _GST_GET (data, 2, 32, 16) | \
392 _GST_GET (data, 1, 32, 8) | \
393 _GST_GET (data, 0, 32, 0))
395 #define GST_READ_UINT16_BE(data) (_GST_GET (data, 0, 16, 8) | \
396 _GST_GET (data, 1, 16, 0))
398 #define GST_READ_UINT16_LE(data) (_GST_GET (data, 1, 16, 8) | \
399 _GST_GET (data, 0, 16, 0))
401 #define GST_READ_UINT8(data) (_GST_GET (data, 0, 8, 0))
403 #define _GST_PUT(__data, __idx, __size, __shift, __num) \
404 (((guint8 *) (__data))[__idx] = (((guint##__size) __num) >> __shift) & 0xff)
406 #define GST_WRITE_UINT64_BE(data, num) do { \
407 _GST_PUT (data, 0, 64, 56, num); \
408 _GST_PUT (data, 1, 64, 48, num); \
409 _GST_PUT (data, 2, 64, 40, num); \
410 _GST_PUT (data, 3, 64, 32, num); \
411 _GST_PUT (data, 4, 64, 24, num); \
412 _GST_PUT (data, 5, 64, 16, num); \
413 _GST_PUT (data, 6, 64, 8, num); \
414 _GST_PUT (data, 7, 64, 0, num); \
417 #define GST_WRITE_UINT64_LE(data, num) do { \
418 _GST_PUT (data, 0, 64, 0, num); \
419 _GST_PUT (data, 1, 64, 8, num); \
420 _GST_PUT (data, 2, 64, 16, num); \
421 _GST_PUT (data, 3, 64, 24, num); \
422 _GST_PUT (data, 4, 64, 32, num); \
423 _GST_PUT (data, 5, 64, 40, num); \
424 _GST_PUT (data, 6, 64, 48, num); \
425 _GST_PUT (data, 7, 64, 56, num); \
428 #define GST_WRITE_UINT32_BE(data, num) do { \
429 _GST_PUT (data, 0, 32, 24, num); \
430 _GST_PUT (data, 1, 32, 16, num); \
431 _GST_PUT (data, 2, 32, 8, num); \
432 _GST_PUT (data, 3, 32, 0, num); \
435 #define GST_WRITE_UINT32_LE(data, num) do { \
436 _GST_PUT (data, 0, 32, 0, num); \
437 _GST_PUT (data, 1, 32, 8, num); \
438 _GST_PUT (data, 2, 32, 16, num); \
439 _GST_PUT (data, 3, 32, 24, num); \
442 #define GST_WRITE_UINT16_BE(data, num) do { \
443 _GST_PUT (data, 0, 16, 8, num); \
444 _GST_PUT (data, 1, 16, 0, num); \
447 #define GST_WRITE_UINT16_LE(data, num) do { \
448 _GST_PUT (data, 0, 16, 0, num); \
449 _GST_PUT (data, 1, 16, 8, num); \
452 #define GST_WRITE_UINT8(data, num) do { \
453 _GST_PUT (data, 0, 8, 0, num); \
456 #endif /* GST_HAVE_UNALIGNED_ACCESS */
459 /* Miscellaneous utility macros */
463 * @num: integer value to round up
465 * Make number divideable by two without a rest.
467 #define GST_ROUND_UP_2(num) (((num)+1)&~1)
470 * @num: integer value to round up
472 * Make number divideable by four without a rest.
474 #define GST_ROUND_UP_4(num) (((num)+3)&~3)
477 * @num: integer value to round up
479 * Make number divideable by eight without a rest.
481 #define GST_ROUND_UP_8(num) (((num)+7)&~7)
484 * @num: integer value to round up
486 * Make number divideable by 16 without a rest.
488 #define GST_ROUND_UP_16(num) (((num)+15)&~15)
491 * @num: integer value to round up
493 * Make number divideable by 32 without a rest.
495 #define GST_ROUND_UP_32(num) (((num)+31)&~31)
498 * @num: integer value to round up
500 * Make number divideable by 64 without a rest.
502 #define GST_ROUND_UP_64(num) (((num)+63)&~63)
506 * @num: integer value to round down
508 * Make number divisible by two without a rest by rounding it down
512 #define GST_ROUND_DOWN_2(num) ((num)&(~1))
515 * @num: integer value to round down
517 * Make number divisible by four without a rest by rounding it down
521 #define GST_ROUND_DOWN_4(num) ((num)&(~3))
524 * @num: integer value to round down
526 * Make number divisible by eight without a rest by rounding it down
530 #define GST_ROUND_DOWN_8(num) ((num)&(~7))
533 * @num: integer value to round down
535 * Make number divisible by 16 without a rest by rounding it down
539 #define GST_ROUND_DOWN_16(num) ((num)&(~15))
542 * @num: integer value to round down
544 * Make number divisible by 32 without a rest by rounding it down
548 #define GST_ROUND_DOWN_32(num) ((num)&(~31))
551 * @num: integer value to round down
553 * Make number divisible by 64 without a rest by rounding it down
557 #define GST_ROUND_DOWN_64(num) ((num)&(~63))
559 void gst_object_default_error (GstObject * source,
560 GError * error, gchar * debug);
562 /* element functions */
563 void gst_element_create_all_pads (GstElement *element);
564 GstPad* gst_element_get_compatible_pad (GstElement *element, GstPad *pad,
565 const GstCaps *caps);
567 GstPadTemplate* gst_element_get_compatible_pad_template (GstElement *element, GstPadTemplate *compattempl);
569 G_CONST_RETURN gchar* gst_element_state_get_name (GstState state);
570 G_CONST_RETURN gchar * gst_element_state_change_return_get_name (GstStateChangeReturn state_ret);
572 gboolean gst_element_link (GstElement *src, GstElement *dest);
573 gboolean gst_element_link_many (GstElement *element_1,
574 GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
575 gboolean gst_element_link_filtered (GstElement * src,
578 void gst_element_unlink (GstElement *src, GstElement *dest);
579 void gst_element_unlink_many (GstElement *element_1,
580 GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
582 gboolean gst_element_link_pads (GstElement *src, const gchar *srcpadname,
583 GstElement *dest, const gchar *destpadname);
584 void gst_element_unlink_pads (GstElement *src, const gchar *srcpadname,
585 GstElement *dest, const gchar *destpadname);
587 gboolean gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
588 GstElement * dest, const gchar * destpadname,
591 gboolean gst_element_seek_simple (GstElement *element,
593 GstSeekFlags seek_flags,
596 /* util elementfactory functions */
597 gboolean gst_element_factory_can_src_caps(GstElementFactory *factory, const GstCaps *caps);
598 gboolean gst_element_factory_can_sink_caps(GstElementFactory *factory, const GstCaps *caps);
600 /* util query functions */
601 gboolean gst_element_query_position (GstElement *element, GstFormat *format,
603 gboolean gst_element_query_duration (GstElement *element, GstFormat *format,
605 gboolean gst_element_query_convert (GstElement *element, GstFormat src_format, gint64 src_val,
606 GstFormat *dest_format, gint64 *dest_val);
608 /* element class functions */
609 void gst_element_class_install_std_props (GstElementClass * klass,
610 const gchar * first_name, ...) G_GNUC_NULL_TERMINATED;
613 gboolean gst_pad_can_link (GstPad *srcpad, GstPad *sinkpad);
615 void gst_pad_use_fixed_caps (GstPad *pad);
616 GstCaps* gst_pad_get_fixed_caps_func (GstPad *pad);
617 GstCaps* gst_pad_proxy_getcaps (GstPad * pad);
618 gboolean gst_pad_proxy_setcaps (GstPad * pad, GstCaps * caps);
620 GstElement* gst_pad_get_parent_element (GstPad *pad);
622 /* util query functions */
623 gboolean gst_pad_query_position (GstPad *pad, GstFormat *format,
625 gboolean gst_pad_query_duration (GstPad *pad, GstFormat *format,
627 gboolean gst_pad_query_convert (GstPad *pad, GstFormat src_format, gint64 src_val,
628 GstFormat *dest_format, gint64 *dest_val);
630 gboolean gst_pad_query_peer_position (GstPad *pad, GstFormat *format,
632 gboolean gst_pad_query_peer_duration (GstPad *pad, GstFormat *format,
634 gboolean gst_pad_query_peer_convert (GstPad *pad, GstFormat src_format, gint64 src_val,
635 GstFormat *dest_format, gint64 *dest_val);
638 void gst_bin_add_many (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
639 void gst_bin_remove_many (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
640 GstPad * gst_bin_find_unconnected_pad (GstBin *bin, GstPadDirection direction);
642 /* buffer functions */
643 GstBuffer * gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2);
644 GstBuffer * gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2);
645 void gst_buffer_stamp (GstBuffer * dest, const GstBuffer * src);
647 /* atomic functions */
648 void gst_atomic_int_set (gint * atomic_int, gint value);
651 gulong gst_pad_add_data_probe (GstPad * pad,
654 void gst_pad_remove_data_probe (GstPad * pad, guint handler_id);
655 gulong gst_pad_add_event_probe (GstPad * pad,
658 void gst_pad_remove_event_probe (GstPad * pad, guint handler_id);
659 gulong gst_pad_add_buffer_probe (GstPad * pad,
662 void gst_pad_remove_buffer_probe (GstPad * pad, guint handler_id);
664 /* tag emission utility functions */
665 void gst_element_found_tags_for_pad (GstElement * element,
668 void gst_element_found_tags (GstElement * element,
671 /* parse utility functions */
672 GstElement * gst_parse_bin_from_description (const gchar * bin_description,
673 gboolean ghost_unconnected_pads,
678 #endif /* __GST_UTILS_H__ */