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__
28 #include <gst/gstconfig.h>
31 #include <gst/gstbin.h>
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);
39 guint64 gst_util_gdouble_to_guint64 (gdouble value);
40 gdouble gst_util_guint64_to_gdouble (guint64 value);
43 * gst_guint64_to_gdouble:
44 * @value: the #guint64 value to convert
46 * Convert @value to a gdouble.
48 * Returns: @value converted to a #gdouble.
52 * gst_gdouble_to_guint64:
53 * @value: the #gdouble value to convert
55 * Convert @value to a guint64.
57 * Returns: @value converted to a #guint64.
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)
63 #define gst_gdouble_to_guint64(value) ((guint64) (value))
64 #define gst_guint64_to_gdouble(value) ((gdouble) (value))
67 guint64 gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom);
69 guint64 gst_util_uint64_scale_int (guint64 val, gint num, gint denom);
71 void gst_print_pad_caps (GString *buf, gint indent, GstPad *pad);
72 void gst_print_element_args (GString *buf, gint indent, GstElement *element);
75 /* Macros for defining classes. Ideas taken from Bonobo, which took theirs
76 from Nautilus and GOB. */
79 * GST_BOILERPLATE_FULL:
80 * @type: the name of the type struct
81 * @type_as_function: the prefix for the functions
82 * @parent_type: the parent type struct name
83 * @parent_type_macro: the parent type macro
84 * @additional_initializations: function pointer in the form of
85 * void additional_initializations (GType type) that can be used for
86 * initializing interfaces and the like
88 * Define the boilerplate type stuff to reduce typos and code size. Defines
89 * the get_type method and the parent_class static variable.
93 * GST_BOILERPLATE_FULL (GstFdSink, gst_fdsink, GstElement, GST_TYPE_ELEMENT, _do_init);
97 #define GST_BOILERPLATE_FULL(type, type_as_function, parent_type, parent_type_macro, additional_initializations) \
99 static void type_as_function ## _base_init (gpointer g_class); \
100 static void type_as_function ## _class_init (type ## Class *g_class);\
101 static void type_as_function ## _init (type *object, \
102 type ## Class *g_class);\
103 static parent_type ## Class *parent_class = NULL; \
105 type_as_function ## _class_init_trampoline (gpointer g_class, \
108 parent_class = (parent_type ## Class *) \
109 g_type_class_peek_parent (g_class); \
110 type_as_function ## _class_init ((type ## Class *)g_class); \
113 GType type_as_function ## _get_type (void); \
116 type_as_function ## _get_type (void) \
118 static GType object_type = 0; \
119 if (G_UNLIKELY (object_type == 0)) { \
120 static const GTypeInfo object_info = { \
121 sizeof (type ## Class), \
122 type_as_function ## _base_init, \
123 NULL, /* base_finalize */ \
124 type_as_function ## _class_init_trampoline, \
125 NULL, /* class_finalize */ \
126 NULL, /* class_data */ \
128 0, /* n_preallocs */ \
129 (GInstanceInitFunc) type_as_function ## _init \
131 object_type = g_type_register_static (parent_type_macro, #type, \
132 &object_info, (GTypeFlags) 0); \
133 additional_initializations (object_type); \
135 return object_type; \
138 #define __GST_DO_NOTHING(type) /* NOP */
142 * @type: the name of the type struct
143 * @type_as_function: the prefix for the functions
144 * @parent_type: the parent type struct name
145 * @parent_type_macro: the parent type macro
147 * Define the boilerplate type stuff to reduce typos and code size. Defines
148 * the get_type method and the parent_class static variable.
152 * GST_BOILERPLATE (GstFdSink, gst_fdsink, GstElement, GST_TYPE_ELEMENT);
156 #define GST_BOILERPLATE(type,type_as_function,parent_type,parent_type_macro) \
157 GST_BOILERPLATE_FULL (type, type_as_function, parent_type, parent_type_macro, \
160 /* Like GST_BOILERPLATE, but makes the type 1) implement an interface, and 2)
161 * implement GstImplementsInterface for that type
163 * After this you will need to implement interface_as_function ## _supported
164 * and interface_as_function ## _interface_init
167 * GST_BOILERPLATE_WITH_INTERFACE:
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_as_macro: the parent type macro
172 * @interface_type: the name of the interface type struct
173 * @interface_type_as_macro: the interface type macro
174 * @interface_as_function: the interface function name prefix
176 * Like GST_BOILERPLATE, but makes the type 1) implement an interface, and 2)
177 * implement GstImplementsInterface for that type.
179 * After this you will need to implement interface_as_function ## _supported
180 * and interface_as_function ## _interface_init
182 #define GST_BOILERPLATE_WITH_INTERFACE(type, type_as_function, \
183 parent_type, parent_type_as_macro, interface_type, \
184 interface_type_as_macro, interface_as_function) \
186 static void interface_as_function ## _interface_init (interface_type ## Class *klass); \
187 static gboolean interface_as_function ## _supported (type *object, GType iface_type); \
190 type_as_function ## _implements_interface_init (GstImplementsInterfaceClass *klass) \
192 klass->supported = (gboolean (*)(GstImplementsInterface*, GType))interface_as_function ## _supported; \
196 type_as_function ## _init_interfaces (GType type) \
198 static const GInterfaceInfo implements_iface_info = { \
199 (GInterfaceInitFunc) type_as_function ## _implements_interface_init,\
203 static const GInterfaceInfo iface_info = { \
204 (GInterfaceInitFunc) interface_as_function ## _interface_init, \
209 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE, \
210 &implements_iface_info); \
211 g_type_add_interface_static (type, interface_type_as_macro, \
215 GST_BOILERPLATE_FULL (type, type_as_function, parent_type, \
216 parent_type_as_macro, type_as_function ## _init_interfaces)
220 * @parent_class_cast: the name of the class cast macro for the parent type
221 * @name: name of the function to call
222 * @args: arguments enclosed in '( )'
224 * Just call the parent handler. This assumes that there is a variable
225 * named parent_class that points to the (duh!) parent class. Note that
226 * this macro is not to be used with things that return something, use
227 * the _WITH_DEFAULT version for that
229 #define GST_CALL_PARENT(parent_class_cast, name, args) \
230 ((parent_class_cast(parent_class)->name != NULL) ? \
231 parent_class_cast(parent_class)->name args : (void) 0)
234 * GST_CALL_PARENT_WITH_DEFAULT:
235 * @parent_class_cast: the name of the class cast macro for the parent type
236 * @name: name of the function to call
237 * @args: arguments enclosed in '( )'
238 * @def_return: default result
240 * Same as GST_CALL_PARENT(), but in case there is no implementation, it
241 * evaluates to @def_return.
243 #define GST_CALL_PARENT_WITH_DEFAULT(parent_class_cast, name, args, def_return)\
244 ((parent_class_cast(parent_class)->name != NULL) ? \
245 parent_class_cast(parent_class)->name args : def_return)
247 /* Define possibly unaligned memory access method whether the type of
249 #if GST_HAVE_UNALIGNED_ACCESS
251 #define _GST_GET(__data, __size, __end) \
252 (GUINT##__size##_FROM_##__end (* ((guint##__size *) (__data))))
255 * GST_READ_UINT64_BE:
256 * @data: memory location
258 * Read a 64 bit unsigned integer value in big endian format from the memory buffer.
260 #define GST_READ_UINT64_BE(data) _GST_GET (data, 64, BE)
262 * GST_READ_UINT64_LE:
263 * @data: memory location
265 * Read a 64 bit unsigned integer value in little endian format from the memory buffer.
267 #define GST_READ_UINT64_LE(data) _GST_GET (data, 64, LE)
269 * GST_READ_UINT32_BE:
270 * @data: memory location
272 * Read a 32 bit unsigned integer value in big endian format from the memory buffer.
274 #define GST_READ_UINT32_BE(data) _GST_GET (data, 32, BE)
276 * GST_READ_UINT32_LE:
277 * @data: memory location
279 * Read a 32 bit unsigned integer value in little endian format from the memory buffer.
281 #define GST_READ_UINT32_LE(data) _GST_GET (data, 32, LE)
283 * GST_READ_UINT16_BE:
284 * @data: memory location
286 * Read a 16 bit unsigned integer value in big endian format from the memory buffer.
288 #define GST_READ_UINT16_BE(data) _GST_GET (data, 16, BE)
290 * GST_READ_UINT16_LE:
291 * @data: memory location
293 * Read a 16 bit unsigned integer value in little endian format from the memory buffer.
295 #define GST_READ_UINT16_LE(data) _GST_GET (data, 16, LE)
298 * @data: memory location
300 * Read an 8 bit unsigned integer value from the memory buffer.
302 #define GST_READ_UINT8(data) (* ((guint8 *) (data)))
304 #define _GST_PUT(__data, __size, __end, __num) \
305 ((* (guint##__size *) (__data)) = GUINT##__size##_TO_##__end (__num))
308 * GST_WRITE_UINT64_BE:
309 * @data: memory location
310 * @num: value to store
312 * Store a 64 bit unsigned integer value in big endian format into the memory buffer.
314 #define GST_WRITE_UINT64_BE(data, num) _GST_PUT(data, 64, BE, num)
316 * GST_WRITE_UINT64_LE:
317 * @data: memory location
318 * @num: value to store
320 * Store a 64 bit unsigned integer value in little endian format into the memory buffer.
322 #define GST_WRITE_UINT64_LE(data, num) _GST_PUT(data, 64, LE, num)
324 * GST_WRITE_UINT32_BE:
325 * @data: memory location
326 * @num: value to store
328 * Store a 32 bit unsigned integer value in big endian format into the memory buffer.
330 #define GST_WRITE_UINT32_BE(data, num) _GST_PUT(data, 32, BE, num)
332 * GST_WRITE_UINT32_LE:
333 * @data: memory location
334 * @num: value to store
336 * Store a 32 bit unsigned integer value in little endian format into the memory buffer.
338 #define GST_WRITE_UINT32_LE(data, num) _GST_PUT(data, 32, LE, num)
340 * GST_WRITE_UINT16_BE:
341 * @data: memory location
342 * @num: value to store
344 * Store a 16 bit unsigned integer value in big endian format into the memory buffer.
346 #define GST_WRITE_UINT16_BE(data, num) _GST_PUT(data, 16, BE, num)
348 * GST_WRITE_UINT16_LE:
349 * @data: memory location
350 * @num: value to store
352 * Store a 16 bit unsigned integer value in little endian format into the memory buffer.
354 #define GST_WRITE_UINT16_LE(data, num) _GST_PUT(data, 16, LE, num)
357 * @data: memory location
358 * @num: value to store
360 * Store an 8 bit unsigned integer value into the memory buffer.
362 #define GST_WRITE_UINT8(data, num) ((* (guint8 *) (data)) = (num))
364 #else /* GST_HAVE_UNALIGNED_ACCESS */
366 #define _GST_GET(__data, __idx, __size, __shift) \
367 (((guint##__size) (((guint8 *) (__data))[__idx])) << __shift)
369 #define GST_READ_UINT64_BE(data) (_GST_GET (data, 0, 64, 56) | \
370 _GST_GET (data, 1, 64, 48) | \
371 _GST_GET (data, 2, 64, 40) | \
372 _GST_GET (data, 3, 64, 32) | \
373 _GST_GET (data, 4, 64, 24) | \
374 _GST_GET (data, 5, 64, 16) | \
375 _GST_GET (data, 6, 64, 8) | \
376 _GST_GET (data, 7, 64, 0))
378 #define GST_READ_UINT64_LE(data) (_GST_GET (data, 7, 64, 56) | \
379 _GST_GET (data, 6, 64, 48) | \
380 _GST_GET (data, 5, 64, 40) | \
381 _GST_GET (data, 4, 64, 32) | \
382 _GST_GET (data, 3, 64, 24) | \
383 _GST_GET (data, 2, 64, 16) | \
384 _GST_GET (data, 1, 64, 8) | \
385 _GST_GET (data, 0, 64, 0))
387 #define GST_READ_UINT32_BE(data) (_GST_GET (data, 0, 32, 24) | \
388 _GST_GET (data, 1, 32, 16) | \
389 _GST_GET (data, 2, 32, 8) | \
390 _GST_GET (data, 3, 32, 0))
392 #define GST_READ_UINT32_LE(data) (_GST_GET (data, 3, 32, 24) | \
393 _GST_GET (data, 2, 32, 16) | \
394 _GST_GET (data, 1, 32, 8) | \
395 _GST_GET (data, 0, 32, 0))
397 #define GST_READ_UINT16_BE(data) (_GST_GET (data, 0, 16, 8) | \
398 _GST_GET (data, 1, 16, 0))
400 #define GST_READ_UINT16_LE(data) (_GST_GET (data, 1, 16, 8) | \
401 _GST_GET (data, 0, 16, 0))
403 #define GST_READ_UINT8(data) (_GST_GET (data, 0, 8, 0))
405 #define _GST_PUT(__data, __idx, __size, __shift, __num) \
406 (((guint8 *) (__data))[__idx] = (((guint##__size) __num) >> __shift) & 0xff)
408 #define GST_WRITE_UINT64_BE(data, num) do { \
409 _GST_PUT (data, 0, 64, 56, num); \
410 _GST_PUT (data, 1, 64, 48, num); \
411 _GST_PUT (data, 2, 64, 40, num); \
412 _GST_PUT (data, 3, 64, 32, num); \
413 _GST_PUT (data, 4, 64, 24, num); \
414 _GST_PUT (data, 5, 64, 16, num); \
415 _GST_PUT (data, 6, 64, 8, num); \
416 _GST_PUT (data, 7, 64, 0, num); \
419 #define GST_WRITE_UINT64_LE(data, num) do { \
420 _GST_PUT (data, 0, 64, 0, num); \
421 _GST_PUT (data, 1, 64, 8, num); \
422 _GST_PUT (data, 2, 64, 16, num); \
423 _GST_PUT (data, 3, 64, 24, num); \
424 _GST_PUT (data, 4, 64, 32, num); \
425 _GST_PUT (data, 5, 64, 40, num); \
426 _GST_PUT (data, 6, 64, 48, num); \
427 _GST_PUT (data, 7, 64, 56, num); \
430 #define GST_WRITE_UINT32_BE(data, num) do { \
431 _GST_PUT (data, 0, 32, 24, num); \
432 _GST_PUT (data, 1, 32, 16, num); \
433 _GST_PUT (data, 2, 32, 8, num); \
434 _GST_PUT (data, 3, 32, 0, num); \
437 #define GST_WRITE_UINT32_LE(data, num) do { \
438 _GST_PUT (data, 0, 32, 0, num); \
439 _GST_PUT (data, 1, 32, 8, num); \
440 _GST_PUT (data, 2, 32, 16, num); \
441 _GST_PUT (data, 3, 32, 24, num); \
444 #define GST_WRITE_UINT16_BE(data, num) do { \
445 _GST_PUT (data, 0, 16, 8, num); \
446 _GST_PUT (data, 1, 16, 0, num); \
449 #define GST_WRITE_UINT16_LE(data, num) do { \
450 _GST_PUT (data, 0, 16, 0, num); \
451 _GST_PUT (data, 1, 16, 8, num); \
454 #define GST_WRITE_UINT8(data, num) do { \
455 _GST_PUT (data, 0, 8, 0, num); \
458 #endif /* GST_HAVE_UNALIGNED_ACCESS */
461 /* Miscellaneous utility macros */
465 * @num: value round up
467 * Make number divideable by two without a rest.
469 #define GST_ROUND_UP_2(num) (((num)+1)&~1)
472 * @num: value round up
474 * Make number divideable by four without a rest.
476 #define GST_ROUND_UP_4(num) (((num)+3)&~3)
479 * @num: value round up
481 * Make number divideable by eight without a rest.
483 #define GST_ROUND_UP_8(num) (((num)+7)&~7)
486 * @num: value round up
488 * Make number divideable by 16 without a rest.
490 #define GST_ROUND_UP_16(num) (((num)+15)&~15)
493 * @num: value round up
495 * Make number divideable by 32 without a rest.
497 #define GST_ROUND_UP_32(num) (((num)+31)&~31)
500 * @num: value round up
502 * Make number divideable by 64 without a rest.
504 #define GST_ROUND_UP_64(num) (((num)+63)&~63)
506 void gst_object_default_error (GstObject * source,
507 GError * error, gchar * debug);
509 /* element functions */
510 void gst_element_create_all_pads (GstElement *element);
511 GstPad* gst_element_get_compatible_pad (GstElement *element, GstPad *pad,
512 const GstCaps *caps);
514 GstPadTemplate* gst_element_get_compatible_pad_template (GstElement *element, GstPadTemplate *compattempl);
516 G_CONST_RETURN gchar* gst_element_state_get_name (GstState state);
517 G_CONST_RETURN gchar * gst_element_state_change_return_get_name (GstStateChangeReturn state_ret);
519 gboolean gst_element_link (GstElement *src, GstElement *dest);
520 gboolean gst_element_link_many (GstElement *element_1,
521 GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
522 gboolean gst_element_link_filtered (GstElement * src,
525 void gst_element_unlink (GstElement *src, GstElement *dest);
526 void gst_element_unlink_many (GstElement *element_1,
527 GstElement *element_2, ...) G_GNUC_NULL_TERMINATED;
529 gboolean gst_element_link_pads (GstElement *src, const gchar *srcpadname,
530 GstElement *dest, const gchar *destpadname);
531 void gst_element_unlink_pads (GstElement *src, const gchar *srcpadname,
532 GstElement *dest, const gchar *destpadname);
534 gboolean gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
535 GstElement * dest, const gchar * destpadname,
538 gboolean gst_element_seek_simple (GstElement *element,
540 GstSeekFlags seek_flags,
543 /* util elementfactory functions */
544 gboolean gst_element_factory_can_src_caps(GstElementFactory *factory, const GstCaps *caps);
545 gboolean gst_element_factory_can_sink_caps(GstElementFactory *factory, const GstCaps *caps);
547 /* util query functions */
548 gboolean gst_element_query_position (GstElement *element, GstFormat *format,
550 gboolean gst_element_query_duration (GstElement *element, GstFormat *format,
552 gboolean gst_element_query_convert (GstElement *element, GstFormat src_format, gint64 src_val,
553 GstFormat *dest_format, gint64 *dest_val);
555 /* element class functions */
556 void gst_element_class_install_std_props (GstElementClass * klass,
557 const gchar * first_name, ...) G_GNUC_NULL_TERMINATED;
560 gboolean gst_pad_can_link (GstPad *srcpad, GstPad *sinkpad);
562 void gst_pad_use_fixed_caps (GstPad *pad);
563 GstCaps* gst_pad_get_fixed_caps_func (GstPad *pad);
564 GstCaps* gst_pad_proxy_getcaps (GstPad * pad);
565 gboolean gst_pad_proxy_setcaps (GstPad * pad, GstCaps * caps);
567 GstElement* gst_pad_get_parent_element (GstPad *pad);
569 /* util query functions */
570 gboolean gst_pad_query_position (GstPad *pad, GstFormat *format,
572 gboolean gst_pad_query_duration (GstPad *pad, GstFormat *format,
574 gboolean gst_pad_query_convert (GstPad *pad, GstFormat src_format, gint64 src_val,
575 GstFormat *dest_format, gint64 *dest_val);
577 gboolean gst_pad_query_peer_position (GstPad *pad, GstFormat *format,
579 gboolean gst_pad_query_peer_duration (GstPad *pad, GstFormat *format,
581 gboolean gst_pad_query_peer_convert (GstPad *pad, GstFormat src_format, gint64 src_val,
582 GstFormat *dest_format, gint64 *dest_val);
585 void gst_bin_add_many (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
586 void gst_bin_remove_many (GstBin *bin, GstElement *element_1, ...) G_GNUC_NULL_TERMINATED;
587 GstPad * gst_bin_find_unconnected_pad (GstBin *bin, GstPadDirection direction);
589 /* buffer functions */
590 GstBuffer * gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2);
591 GstBuffer * gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2);
592 void gst_buffer_stamp (GstBuffer * dest, const GstBuffer * src);
594 /* atomic functions */
595 void gst_atomic_int_set (gint * atomic_int, gint value);
598 gulong gst_pad_add_data_probe (GstPad * pad,
601 void gst_pad_remove_data_probe (GstPad * pad, guint handler_id);
602 gulong gst_pad_add_event_probe (GstPad * pad,
605 void gst_pad_remove_event_probe (GstPad * pad, guint handler_id);
606 gulong gst_pad_add_buffer_probe (GstPad * pad,
609 void gst_pad_remove_buffer_probe (GstPad * pad, guint handler_id);
611 /* tag emission utility functions */
612 void gst_element_found_tags_for_pad (GstElement * element,
615 void gst_element_found_tags (GstElement * element,
618 #ifndef GST_DISABLE_PARSE
619 /* parse utility functions */
620 GstElement * gst_parse_bin_from_description (const gchar * bin_description,
621 gboolean ghost_unconnected_pads,
623 #else /* GST_DISABLE_PARSE */
625 #if defined _GNUC_ && _GNUC_ >= 3
626 #pragma GCC poison gst_parse_bin_from_description
633 #endif /* __GST_UTILS_H__ */