2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstutils.h: Header for various utility functions
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 #ifndef __GST_UTILS_H__
25 #define __GST_UTILS_H__
28 #include <gst/gstbin.h>
32 void gst_util_set_value_from_string (GValue *value, const gchar *value_str);
33 void gst_util_set_object_arg (GObject *object, const gchar *name, const gchar *value);
34 void gst_util_dump_mem (const guchar *mem, guint size);
36 guint64 gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom);
38 void gst_print_pad_caps (GString *buf, gint indent, GstPad *pad);
39 void gst_print_element_args (GString *buf, gint indent, GstElement *element);
42 /* Macros for defining classes. Ideas taken from Bonobo, which took theirs
43 from Nautilus and GOB. */
46 * GST_BOILERPLATE_FULL:
47 * @type: the name of the type struct
48 * @type_as_function: the prefix for the functions
49 * @parent_type: the parent type struct name
50 * @parent_type_macro: the parent type macro
51 * @additional_initializations: function pointer in the form of
52 * void additional_initializations (GType type) that can be used for
53 * initializing interfaces and the like
55 * Define the boilerplate type stuff to reduce typos and code size. Defines
56 * the get_type method and the parent_class static variable.
60 * GST_BOILERPLATE_FULL (GstFdSink, gst_fdsink, GstElement, GST_TYPE_ELEMENT, _do_init);
64 #define GST_BOILERPLATE_FULL(type, type_as_function, parent_type, parent_type_macro, additional_initializations) \
66 static void type_as_function ## _base_init (gpointer g_class); \
67 static void type_as_function ## _class_init (type ## Class *g_class);\
68 static void type_as_function ## _init (type *object, \
69 type ## Class *g_class);\
70 static parent_type ## Class *parent_class = NULL; \
72 type_as_function ## _class_init_trampoline (gpointer g_class, \
75 parent_class = (parent_type ## Class *) \
76 g_type_class_peek_parent (g_class); \
77 type_as_function ## _class_init ((type ## Class *)g_class); \
81 type_as_function ## _get_type (void) \
83 static GType object_type = 0; \
84 if (object_type == 0) { \
85 static const GTypeInfo object_info = { \
86 sizeof (type ## Class), \
87 type_as_function ## _base_init, \
88 NULL, /* base_finalize */ \
89 type_as_function ## _class_init_trampoline, \
90 NULL, /* class_finalize */ \
91 NULL, /* class_data */ \
93 0, /* n_preallocs */ \
94 (GInstanceInitFunc) type_as_function ## _init \
96 object_type = g_type_register_static (parent_type_macro, #type, \
97 &object_info, (GTypeFlags) 0); \
98 additional_initializations (object_type); \
100 return object_type; \
103 #define __GST_DO_NOTHING(type) /* NOP */
107 * @type: the name of the type struct
108 * @type_as_function: the prefix for the functions
109 * @parent_type: the parent type struct name
110 * @parent_type_macro: the parent type macro
112 * Define the boilerplate type stuff to reduce typos and code size. Defines
113 * the get_type method and the parent_class static variable.
117 * GST_BOILERPLATE (GstFdSink, gst_fdsink, GstElement, GST_TYPE_ELEMENT);
121 #define GST_BOILERPLATE(type,type_as_function,parent_type,parent_type_macro) \
122 GST_BOILERPLATE_FULL (type, type_as_function, parent_type, parent_type_macro, \
125 /* Like GST_BOILERPLATE, but makes the type 1) implement an interface, and 2)
126 * implement GstImplementsInterface for that type
128 * After this you will need to implement interface_as_function ## _supported
129 * and interface_as_function ## _interface_init
132 * GST_BOILERPLATE_WITH_INTERFACE:
133 * @type: the name of the type struct
134 * @type_as_function: the prefix for the functions
135 * @parent_type: the parent type struct name
136 * @parent_type_as_macro: the parent type macro
137 * @interface_type: the name of the interface type struct
138 * @interface_type_as_macro: the interface type macro
139 * @interface_as_function: the interface function name prefix
141 * Like GST_BOILERPLATE, but makes the type 1) implement an interface, and 2)
142 * implement GstImplementsInterface for that type.
144 * After this you will need to implement interface_as_function ## _supported
145 * and interface_as_function ## _interface_init
147 #define GST_BOILERPLATE_WITH_INTERFACE(type, type_as_function, \
148 parent_type, parent_type_as_macro, interface_type, \
149 interface_type_as_macro, interface_as_function) \
151 static void interface_as_function ## _interface_init (interface_type ## Class *klass); \
152 static gboolean interface_as_function ## _supported (type *object, GType iface_type); \
155 type_as_function ## _implements_interface_init (GstImplementsInterfaceClass *klass) \
157 klass->supported = (gpointer)interface_as_function ## _supported; \
161 type_as_function ## _init_interfaces (GType type) \
163 static const GInterfaceInfo implements_iface_info = { \
164 (GInterfaceInitFunc) type_as_function ## _implements_interface_init,\
168 static const GInterfaceInfo iface_info = { \
169 (GInterfaceInitFunc) interface_as_function ## _interface_init, \
174 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE, \
175 &implements_iface_info); \
176 g_type_add_interface_static (type, interface_type_as_macro, \
180 GST_BOILERPLATE_FULL (type, type_as_function, parent_type, \
181 parent_type_as_macro, type_as_function ## _init_interfaces)
183 /* Just call the parent handler. This assumes that there is a variable
184 * named parent_class that points to the (duh!) parent class. Note that
185 * this macro is not to be used with things that return something, use
186 * the _WITH_DEFAULT version for that */
187 #define GST_CALL_PARENT(parent_class_cast, name, args) \
188 ((parent_class_cast(parent_class)->name != NULL) ? \
189 parent_class_cast(parent_class)->name args : (void) 0)
191 /* Same as above, but in case there is no implementation, it evaluates
193 #define GST_CALL_PARENT_WITH_DEFAULT(parent_class_cast, name, args, \
195 ((parent_class_cast(parent_class)->name != NULL) ? \
196 parent_class_cast(parent_class)->name args : def_return)
198 /* Define possibly unaligned memory access method whether the type of
200 #if GST_HAVE_UNALIGNED_ACCESS
202 #define _GST_GET(__data, __size, __end) \
203 (GUINT##__size##_FROM_##__end (* ((guint##__size *) (__data))))
206 * GST_READ_UINT64_BE:
207 * @data: memory location
209 * Read a 64 bit unsigned integer value in big endian format from the memory buffer.
211 #define GST_READ_UINT64_BE(data) _GST_GET (data, 64, BE)
213 * GST_READ_UINT64_LE:
214 * @data: memory location
216 * Read a 64 bit unsigned integer value in little endian format from the memory buffer.
218 #define GST_READ_UINT64_LE(data) _GST_GET (data, 64, LE)
220 * GST_READ_UINT32_BE:
221 * @data: memory location
223 * Read a 32 bit unsigned integer value in big endian format from the memory buffer.
225 #define GST_READ_UINT32_BE(data) _GST_GET (data, 32, BE)
227 * GST_READ_UINT32_LE:
228 * @data: memory location
230 * Read a 32 bit unsigned integer value in little endian format from the memory buffer.
232 #define GST_READ_UINT32_LE(data) _GST_GET (data, 32, LE)
234 * GST_READ_UINT16_BE:
235 * @data: memory location
237 * Read a 16 bit unsigned integer value in big endian format from the memory buffer.
239 #define GST_READ_UINT16_BE(data) _GST_GET (data, 16, BE)
241 * GST_READ_UINT16_LE:
242 * @data: memory location
244 * Read a 16 bit unsigned integer value in little endian format from the memory buffer.
246 #define GST_READ_UINT16_LE(data) _GST_GET (data, 16, LE)
249 * @data: memory location
251 * Read an 8 bit unsigned integer value from the memory buffer.
253 #define GST_READ_UINT8(data) (* ((guint8 *) (data)))
255 #define _GST_PUT(__data, __size, __end, __num) \
256 ((* (guint##__size *) (__data)) = GUINT##__size##_TO_##__end (__num))
259 * GST_WRITE_UINT64_BE:
260 * @data: memory location
261 * @num: value to store
263 * Store a 64 bit unsigned integer value in big endian format into the memory buffer.
265 #define GST_WRITE_UINT64_BE(data, num) _GST_PUT(data, 64, BE, num)
267 * GST_WRITE_UINT64_LE:
268 * @data: memory location
269 * @num: value to store
271 * Store a 64 bit unsigned integer value in little endian format into the memory buffer.
273 #define GST_WRITE_UINT64_LE(data, num) _GST_PUT(data, 64, LE, num)
275 * GST_WRITE_UINT32_BE:
276 * @data: memory location
277 * @num: value to store
279 * Store a 32 bit unsigned integer value in big endian format into the memory buffer.
281 #define GST_WRITE_UINT32_BE(data, num) _GST_PUT(data, 32, BE, num)
283 * GST_WRITE_UINT32_LE:
284 * @data: memory location
285 * @num: value to store
287 * Store a 32 bit unsigned integer value in little endian format into the memory buffer.
289 #define GST_WRITE_UINT32_LE(data, num) _GST_PUT(data, 32, LE, num)
291 * GST_WRITE_UINT16_BE:
292 * @data: memory location
293 * @num: value to store
295 * Store a 16 bit unsigned integer value in big endian format into the memory buffer.
297 #define GST_WRITE_UINT16_BE(data, num) _GST_PUT(data, 16, BE, num)
299 * GST_WRITE_UINT16_LE:
300 * @data: memory location
301 * @num: value to store
303 * Store a 16 bit unsigned integer value in little endian format into the memory buffer.
305 #define GST_WRITE_UINT16_LE(data, num) _GST_PUT(data, 16, LE, num)
308 * @data: memory location
309 * @num: value to store
311 * Store an 8 bit unsigned integer value into the memory buffer.
313 #define GST_WRITE_UINT8(data, num) ((* (guint8 *) (data)) = (num))
315 #else /* GST_HAVE_UNALIGNED_ACCESS */
317 #define _GST_GET(__data, __idx, __size, __shift) \
318 (((guint##__size) (((guint8 *) (__data))[__idx])) << __shift)
320 #define GST_READ_UINT64_BE(data) (_GST_GET (data, 0, 64, 56) | \
321 _GST_GET (data, 1, 64, 48) | \
322 _GST_GET (data, 2, 64, 40) | \
323 _GST_GET (data, 3, 64, 32) | \
324 _GST_GET (data, 4, 64, 24) | \
325 _GST_GET (data, 5, 64, 16) | \
326 _GST_GET (data, 6, 64, 8) | \
327 _GST_GET (data, 7, 64, 0))
329 #define GST_READ_UINT64_LE(data) (_GST_GET (data, 7, 64, 56) | \
330 _GST_GET (data, 6, 64, 48) | \
331 _GST_GET (data, 5, 64, 40) | \
332 _GST_GET (data, 4, 64, 32) | \
333 _GST_GET (data, 3, 64, 24) | \
334 _GST_GET (data, 2, 64, 16) | \
335 _GST_GET (data, 1, 64, 8) | \
336 _GST_GET (data, 0, 64, 0))
338 #define GST_READ_UINT32_BE(data) (_GST_GET (data, 0, 32, 24) | \
339 _GST_GET (data, 1, 32, 16) | \
340 _GST_GET (data, 2, 32, 8) | \
341 _GST_GET (data, 3, 32, 0))
343 #define GST_READ_UINT32_LE(data) (_GST_GET (data, 3, 32, 24) | \
344 _GST_GET (data, 2, 32, 16) | \
345 _GST_GET (data, 1, 32, 8) | \
346 _GST_GET (data, 0, 32, 0))
348 #define GST_READ_UINT16_BE(data) (_GST_GET (data, 0, 16, 8) | \
349 _GST_GET (data, 1, 16, 0))
351 #define GST_READ_UINT16_LE(data) (_GST_GET (data, 1, 16, 8) | \
352 _GST_GET (data, 0, 16, 0))
354 #define GST_READ_UINT8(data) (_GST_GET (data, 0, 8, 0))
356 #define _GST_PUT(__data, __idx, __size, __shift, __num) \
357 (((guint8 *) (__data))[__idx] = (((guint##__size) __num) >> __shift) & 0xff)
359 #define GST_WRITE_UINT64_BE(data, num) do { \
360 _GST_PUT (data, 0, 64, 56, num); \
361 _GST_PUT (data, 1, 64, 48, num); \
362 _GST_PUT (data, 2, 64, 40, num); \
363 _GST_PUT (data, 3, 64, 32, num); \
364 _GST_PUT (data, 4, 64, 24, num); \
365 _GST_PUT (data, 5, 64, 16, num); \
366 _GST_PUT (data, 6, 64, 8, num); \
367 _GST_PUT (data, 7, 64, 0, num); \
370 #define GST_WRITE_UINT64_LE(data, num) do { \
371 _GST_PUT (data, 0, 64, 0, num); \
372 _GST_PUT (data, 1, 64, 8, num); \
373 _GST_PUT (data, 2, 64, 16, num); \
374 _GST_PUT (data, 3, 64, 24, num); \
375 _GST_PUT (data, 4, 64, 32, num); \
376 _GST_PUT (data, 5, 64, 40, num); \
377 _GST_PUT (data, 6, 64, 48, num); \
378 _GST_PUT (data, 7, 64, 56, num); \
381 #define GST_WRITE_UINT32_BE(data, num) do { \
382 _GST_PUT (data, 0, 32, 24, num); \
383 _GST_PUT (data, 1, 32, 16, num); \
384 _GST_PUT (data, 2, 32, 8, num); \
385 _GST_PUT (data, 3, 32, 0, num); \
388 #define GST_WRITE_UINT32_LE(data, num) do { \
389 _GST_PUT (data, 0, 32, 0, num); \
390 _GST_PUT (data, 1, 32, 8, num); \
391 _GST_PUT (data, 2, 32, 16, num); \
392 _GST_PUT (data, 3, 32, 24, num); \
395 #define GST_WRITE_UINT16_BE(data, num) do { \
396 _GST_PUT (data, 0, 16, 8, num); \
397 _GST_PUT (data, 1, 16, 0, num); \
400 #define GST_WRITE_UINT16_LE(data, num) do { \
401 _GST_PUT (data, 0, 16, 0, num); \
402 _GST_PUT (data, 1, 16, 8, num); \
405 #define GST_WRITE_UINT8(data, num) do { \
406 _GST_PUT (data, 0, 8, 0, num); \
409 #endif /* GST_HAVE_UNALIGNED_ACCESS */
412 /* Miscellaneous utility macros */
416 * @num: value round up
418 * Make number divideable by two without a rest.
420 #define GST_ROUND_UP_2(num) (((num)+1)&~1)
423 * @num: value round up
425 * Make number divideable by four without a rest.
427 #define GST_ROUND_UP_4(num) (((num)+3)&~3)
430 * @num: value round up
432 * Make number divideable by eight without a rest.
434 #define GST_ROUND_UP_8(num) (((num)+7)&~7)
437 * @num: value round up
439 * Make number divideable by 16 without a rest.
441 #define GST_ROUND_UP_16(num) (((num)+15)&~15)
444 * @num: value round up
446 * Make number divideable by 32 without a rest.
448 #define GST_ROUND_UP_32(num) (((num)+31)&~31)
451 * @num: value round up
453 * Make number divideable by 64 without a rest.
455 #define GST_ROUND_UP_64(num) (((num)+63)&~63)
457 void gst_object_default_error (GstObject * source,
458 GError * error, gchar * debug);
461 #ifndef g_static_rec_cond_wait
462 #define GST_COMPILE_STATIC_REC_COND_WAIT
463 void g_static_rec_cond_wait (GCond *cond,
464 GStaticRecMutex *mutex);
465 gboolean g_static_rec_cond_timed_wait (GCond *cond,
466 GStaticRecMutex *mutex,
470 /* element functions */
471 GstFlowReturn gst_element_abort_preroll (GstElement *element);
472 GstFlowReturn gst_element_finish_preroll (GstElement *element, GstPad *pad);
474 void gst_element_create_all_pads (GstElement *element);
475 GstPad* gst_element_get_compatible_pad (GstElement *element, GstPad *pad,
476 const GstCaps *caps);
478 GstPadTemplate* gst_element_get_compatible_pad_template (GstElement *element, GstPadTemplate *compattempl);
480 G_CONST_RETURN gchar* gst_element_state_get_name (GstState state);
482 gboolean gst_element_link (GstElement *src, GstElement *dest);
483 gboolean gst_element_link_many (GstElement *element_1,
484 GstElement *element_2, ...);
485 gboolean gst_element_link_filtered (GstElement * src,
488 void gst_element_unlink (GstElement *src, GstElement *dest);
489 void gst_element_unlink_many (GstElement *element_1,
490 GstElement *element_2, ...);
492 gboolean gst_element_link_pads (GstElement *src, const gchar *srcpadname,
493 GstElement *dest, const gchar *destpadname);
494 void gst_element_unlink_pads (GstElement *src, const gchar *srcpadname,
495 GstElement *dest, const gchar *destpadname);
497 gboolean gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
498 GstElement * dest, const gchar * destpadname,
500 GstStateChangeReturn gst_element_set_state_async (GstElement * element, GstState state);
502 /* util elementfactory functions */
503 gboolean gst_element_factory_can_src_caps(GstElementFactory *factory, const GstCaps *caps);
504 gboolean gst_element_factory_can_sink_caps(GstElementFactory *factory, const GstCaps *caps);
506 /* util query functions */
507 gboolean gst_element_query_position (GstElement *element, GstFormat *format,
508 gint64 *cur, gint64 *end);
509 gboolean gst_element_query_convert (GstElement *element, GstFormat src_format, gint64 src_val,
510 GstFormat *dest_fmt, gint64 *dest_val);
512 /* element class functions */
513 void gst_element_class_install_std_props (GstElementClass * klass,
514 const gchar * first_name, ...);
517 gboolean gst_pad_can_link (GstPad *srcpad, GstPad *sinkpad);
519 void gst_pad_use_fixed_caps (GstPad *pad);
520 GstCaps* gst_pad_get_fixed_caps_func (GstPad *pad);
521 GstCaps* gst_pad_proxy_getcaps (GstPad * pad);
522 gboolean gst_pad_proxy_setcaps (GstPad * pad, GstCaps * caps);
524 GstElement* gst_pad_get_parent_element (GstPad *pad);
527 G_CONST_RETURN gchar* gst_flow_get_name (GstFlowReturn ret);
530 /* util query functions */
531 gboolean gst_pad_query_position (GstPad *pad, GstFormat *format,
532 gint64 *cur, gint64 *end);
533 gboolean gst_pad_query_convert (GstPad *pad, GstFormat src_format, gint64 src_val,
534 GstFormat *dest_format, gint64 *dest_val);
537 void gst_bin_add_many (GstBin *bin, GstElement *element_1, ...);
538 void gst_bin_remove_many (GstBin *bin, GstElement *element_1, ...);
539 void gst_bin_watch_for_state_change (GstBin *bin);
541 /* buffer functions */
542 GstBuffer * gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2);
543 GstBuffer * gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2);
544 void gst_buffer_stamp (GstBuffer * dest, const GstBuffer * src);
546 /* atomic functions */
547 void gst_atomic_int_set (gint * atomic_int, gint value);
550 gulong gst_pad_add_data_probe (GstPad * pad,
553 void gst_pad_remove_data_probe (GstPad * pad, guint handler_id);
554 gulong gst_pad_add_event_probe (GstPad * pad,
557 void gst_pad_remove_event_probe (GstPad * pad, guint handler_id);
558 gulong gst_pad_add_buffer_probe (GstPad * pad,
561 void gst_pad_remove_buffer_probe (GstPad * pad, guint handler_id);
563 /* tag emission utility functions */
564 void gst_element_found_tags_for_pad (GstElement * element,
567 void gst_element_found_tags (GstElement * element,
572 #endif /* __GST_UTILS_H__ */