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);
35 void gst_util_dump_mem (const guchar *mem, guint size);
37 void gst_print_pad_caps (GString *buf, gint indent, GstPad *pad);
38 void gst_print_element_args (GString *buf, gint indent, GstElement *element);
41 /* Macros for defining classes. Ideas taken from Bonobo, which took theirs
42 from Nautilus and GOB. */
44 /* Define the boilerplate type stuff to reduce typos and code size. Defines
45 the get_type method and the parent_class static variable.
46 void additional_initializations (GType type) is for initializing interfaces
47 and stuff like that */
49 #define GST_BOILERPLATE_FULL(type, type_as_function, parent_type, parent_type_macro, additional_initializations) \
51 static void type_as_function ## _base_init (gpointer g_class); \
52 static void type_as_function ## _class_init (type ## Class *g_class);\
53 static void type_as_function ## _init (type *object); \
54 static parent_type ## Class *parent_class = NULL; \
56 type_as_function ## _class_init_trampoline (gpointer g_class, \
59 parent_class = (parent_type ## Class *) \
60 g_type_class_peek_parent (g_class); \
61 type_as_function ## _class_init ((type ## Class *)g_class); \
65 type_as_function ## _get_type (void) \
67 static GType object_type = 0; \
68 if (object_type == 0) { \
69 static const GTypeInfo object_info = { \
70 sizeof (type ## Class), \
71 type_as_function ## _base_init, \
72 NULL, /* base_finalize */ \
73 type_as_function ## _class_init_trampoline, \
74 NULL, /* class_finalize */ \
75 NULL, /* class_data */ \
77 0, /* n_preallocs */ \
78 (GInstanceInitFunc) type_as_function ## _init \
80 object_type = g_type_register_static (parent_type_macro, #type, \
81 &object_info, (GTypeFlags) 0); \
82 additional_initializations (object_type); \
87 #define __GST_DO_NOTHING(type) /* NOP */
88 #define GST_BOILERPLATE(type,type_as_function,parent_type,parent_type_macro) \
89 GST_BOILERPLATE_FULL (type, type_as_function, parent_type, parent_type_macro, \
92 /* Like GST_BOILERPLATE, but makes the type 1) implement an interface, and 2)
93 * implement GstImplementsInterface for that type
95 * After this you will need to implement interface_as_function ## _supported
96 * and interface_as_function ## _interface_init
98 #define GST_BOILERPLATE_WITH_INTERFACE(type, type_as_function, parent_type, \
99 parent_type_as_macro, interface_type, interface_type_as_macro, \
100 interface_as_function) \
102 static void interface_as_function ## _interface_init (interface_type ## Class *klass); \
103 static gboolean interface_as_function ## _supported (type *object, GType iface_type); \
106 type_as_function ## _implements_interface_init (GstImplementsInterfaceClass *klass) \
108 klass->supported = (gpointer)interface_as_function ## _supported; \
112 type_as_function ## _init_interfaces (GType type) \
114 static const GInterfaceInfo implements_iface_info = { \
115 (GInterfaceInitFunc) type_as_function ## _implements_interface_init, \
119 static const GInterfaceInfo iface_info = { \
120 (GInterfaceInitFunc) interface_as_function ## _interface_init, \
125 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE, \
126 &implements_iface_info); \
127 g_type_add_interface_static (type, interface_type_as_macro, &iface_info); \
130 GST_BOILERPLATE_FULL (type, type_as_function, parent_type, \
131 parent_type_as_macro, type_as_function ## _init_interfaces)
133 /* Just call the parent handler. This assumes that there is a variable
134 * named parent_class that points to the (duh!) parent class. Note that
135 * this macro is not to be used with things that return something, use
136 * the _WITH_DEFAULT version for that */
137 #define GST_CALL_PARENT(parent_class_cast, name, args) \
138 ((parent_class_cast(parent_class)->name != NULL) ? \
139 parent_class_cast(parent_class)->name args : (void) 0)
141 /* Same as above, but in case there is no implementation, it evaluates
143 #define GST_CALL_PARENT_WITH_DEFAULT(parent_class_cast, name, args, def_return) \
144 ((parent_class_cast(parent_class)->name != NULL) ? \
145 parent_class_cast(parent_class)->name args : def_return)
147 /* Define possibly unaligned memory access method whether the type of
149 #if GST_HAVE_UNALIGNED_ACCESS
151 #define _GST_GET(__data, __size, __end) \
152 (GUINT##__size##_FROM_##__end (* ((guint##__size *) (__data))))
154 #define GST_READ_UINT64_BE(data) _GST_GET (data, 64, BE)
155 #define GST_READ_UINT64_LE(data) _GST_GET (data, 64, LE)
156 #define GST_READ_UINT32_BE(data) _GST_GET (data, 32, BE)
157 #define GST_READ_UINT32_LE(data) _GST_GET (data, 32, LE)
158 #define GST_READ_UINT16_BE(data) _GST_GET (data, 16, BE)
159 #define GST_READ_UINT16_LE(data) _GST_GET (data, 16, LE)
160 #define GST_READ_UINT8(data) (* ((guint8 *) (data)))
162 #define _GST_PUT(__data, __size, __end, __num) \
163 ((* (guint##__size *) (__data)) = GUINT##__size##_TO_##__end (__num))
165 #define GST_WRITE_UINT64_BE(data, num) _GST_PUT(data, 64, BE, num)
166 #define GST_WRITE_UINT64_LE(data, num) _GST_PUT(data, 64, LE, num)
167 #define GST_WRITE_UINT32_BE(data, num) _GST_PUT(data, 32, BE, num)
168 #define GST_WRITE_UINT32_LE(data, num) _GST_PUT(data, 32, LE, num)
169 #define GST_WRITE_UINT16_BE(data, num) _GST_PUT(data, 16, BE, num)
170 #define GST_WRITE_UINT16_LE(data, num) _GST_PUT(data, 16, LE, num)
171 #define GST_WRITE_UINT8(data, num) ((* (guint8 *) (data)) = (num))
173 #else /* GST_HAVE_UNALIGNED_ACCESS */
175 #define _GST_GET(__data, __idx, __size, __shift) \
176 (((guint##__size) (((guint8 *) (__data))[__idx])) << __shift)
178 #define GST_READ_UINT64_BE(data) (_GST_GET (data, 0, 64, 56) | \
179 _GST_GET (data, 1, 64, 48) | \
180 _GST_GET (data, 2, 64, 40) | \
181 _GST_GET (data, 3, 64, 32) | \
182 _GST_GET (data, 4, 64, 24) | \
183 _GST_GET (data, 5, 64, 16) | \
184 _GST_GET (data, 6, 64, 8) | \
185 _GST_GET (data, 7, 64, 0))
187 #define GST_READ_UINT64_LE(data) (_GST_GET (data, 7, 64, 56) | \
188 _GST_GET (data, 6, 64, 48) | \
189 _GST_GET (data, 5, 64, 40) | \
190 _GST_GET (data, 4, 64, 32) | \
191 _GST_GET (data, 3, 64, 24) | \
192 _GST_GET (data, 2, 64, 16) | \
193 _GST_GET (data, 1, 64, 8) | \
194 _GST_GET (data, 0, 64, 0))
196 #define GST_READ_UINT32_BE(data) (_GST_GET (data, 0, 32, 24) | \
197 _GST_GET (data, 1, 32, 16) | \
198 _GST_GET (data, 2, 32, 8) | \
199 _GST_GET (data, 3, 32, 0))
201 #define GST_READ_UINT32_LE(data) (_GST_GET (data, 3, 32, 24) | \
202 _GST_GET (data, 2, 32, 16) | \
203 _GST_GET (data, 1, 32, 8) | \
204 _GST_GET (data, 0, 32, 0))
206 #define GST_READ_UINT16_BE(data) (_GST_GET (data, 0, 16, 8) | \
207 _GST_GET (data, 1, 16, 0))
209 #define GST_READ_UINT16_LE(data) (_GST_GET (data, 1, 16, 8) | \
210 _GST_GET (data, 0, 16, 0))
212 #define GST_READ_UINT8(data) (_GST_GET (data, 0, 8, 0))
214 #define _GST_PUT(__data, __idx, __size, __shift, __num) \
215 (((guint8 *) (__data))[__idx] = (((guint##__size) __num) >> __shift) & 0xff)
217 #define GST_WRITE_UINT64_BE(data, num) do { \
218 _GST_PUT (data, 0, 64, 56, num); \
219 _GST_PUT (data, 1, 64, 48, num); \
220 _GST_PUT (data, 2, 64, 40, num); \
221 _GST_PUT (data, 3, 64, 32, num); \
222 _GST_PUT (data, 4, 64, 24, num); \
223 _GST_PUT (data, 5, 64, 16, num); \
224 _GST_PUT (data, 6, 64, 8, num); \
225 _GST_PUT (data, 7, 64, 0, num); \
228 #define GST_WRITE_UINT64_LE(data, num) do { \
229 _GST_PUT (data, 0, 64, 0, num); \
230 _GST_PUT (data, 1, 64, 8, num); \
231 _GST_PUT (data, 2, 64, 16, num); \
232 _GST_PUT (data, 3, 64, 24, num); \
233 _GST_PUT (data, 4, 64, 32, num); \
234 _GST_PUT (data, 5, 64, 40, num); \
235 _GST_PUT (data, 6, 64, 48, num); \
236 _GST_PUT (data, 7, 64, 56, num); \
239 #define GST_WRITE_UINT32_BE(data, num) do { \
240 _GST_PUT (data, 0, 32, 24, num); \
241 _GST_PUT (data, 1, 32, 16, num); \
242 _GST_PUT (data, 2, 32, 8, num); \
243 _GST_PUT (data, 3, 32, 0, num); \
246 #define GST_WRITE_UINT32_LE(data, num) do { \
247 _GST_PUT (data, 0, 32, 0, num); \
248 _GST_PUT (data, 1, 32, 8, num); \
249 _GST_PUT (data, 2, 32, 16, num); \
250 _GST_PUT (data, 3, 32, 24, num); \
253 #define GST_WRITE_UINT16_BE(data, num) do { \
254 _GST_PUT (data, 0, 16, 8, num); \
255 _GST_PUT (data, 1, 16, 0, num); \
258 #define GST_WRITE_UINT16_LE(data, num) do { \
259 _GST_PUT (data, 0, 16, 0, num); \
260 _GST_PUT (data, 1, 16, 8, num); \
263 #define GST_WRITE_UINT8(data, num) do { \
264 _GST_PUT (data, 0, 8, 0, num); \
267 #endif /* GST_HAVE_UNALIGNED_ACCESS */
270 /* Miscellaneous utility macros */
271 #define GST_ROUND_UP_2(num) (((num)+1)&~1)
272 #define GST_ROUND_UP_4(num) (((num)+3)&~3)
273 #define GST_ROUND_UP_8(num) (((num)+7)&~7)
274 #define GST_ROUND_UP_16(num) (((num)+15)&~15)
275 #define GST_ROUND_UP_32(num) (((num)+31)&~31)
276 #define GST_ROUND_UP_64(num) (((num)+63)&~63)
278 void gst_object_default_error (GstObject * source,
279 GError * error, gchar * debug);
282 /* element functions */
283 GstFlowReturn gst_element_abort_preroll (GstElement *element);
284 GstFlowReturn gst_element_finish_preroll (GstElement *element, GstPad *pad);
286 void gst_element_create_all_pads (GstElement *element);
287 GstPad* gst_element_get_compatible_pad (GstElement *element, GstPad *pad,
288 const GstCaps *caps);
290 GstPadTemplate* gst_element_get_compatible_pad_template (GstElement *element, GstPadTemplate *compattempl);
292 G_CONST_RETURN gchar* gst_element_state_get_name (GstElementState state);
294 gboolean gst_element_link (GstElement *src, GstElement *dest);
295 gboolean gst_element_link_many (GstElement *element_1,
296 GstElement *element_2, ...);
297 void gst_element_unlink (GstElement *src, GstElement *dest);
298 void gst_element_unlink_many (GstElement *element_1,
299 GstElement *element_2, ...);
301 gboolean gst_element_link_pads (GstElement *src, const gchar *srcpadname,
302 GstElement *dest, const gchar *destpadname);
303 void gst_element_unlink_pads (GstElement *src, const gchar *srcpadname,
304 GstElement *dest, const gchar *destpadname);
306 gboolean gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
307 GstElement * dest, const gchar * destpadname,
310 /* util elementfactory functions */
311 gboolean gst_element_factory_can_src_caps(GstElementFactory *factory, const GstCaps *caps);
312 gboolean gst_element_factory_can_sink_caps(GstElementFactory *factory, const GstCaps *caps);
314 /* util query functions */
315 gboolean gst_element_query_position (GstElement *element, GstFormat *format,
316 gint64 *cur, gint64 *end);
317 gboolean gst_element_query_convert (GstElement *element, GstFormat src_format, gint64 src_val,
318 GstFormat *dest_fmt, gint64 *dest_val);
320 /* element class functions */
321 void gst_element_class_install_std_props (GstElementClass * klass,
322 const gchar * first_name, ...);
325 gboolean gst_pad_can_link (GstPad *srcpad, GstPad *sinkpad);
327 void gst_pad_use_fixed_caps (GstPad *pad);
328 GstCaps* gst_pad_get_fixed_caps_func (GstPad *pad);
329 GstCaps* gst_pad_proxy_getcaps (GstPad * pad);
330 gboolean gst_pad_proxy_setcaps (GstPad * pad, GstCaps * caps);
332 GstElement* gst_pad_get_parent_element (GstPad *pad);
335 G_CONST_RETURN gchar* gst_flow_get_name (GstFlowReturn ret);
338 /* util query functions */
339 gboolean gst_pad_query_position (GstPad *pad, GstFormat *format,
340 gint64 *cur, gint64 *end);
341 gboolean gst_pad_query_convert (GstPad *pad, GstFormat src_format, gint64 src_val,
342 GstFormat *dest_format, gint64 *dest_val);
345 void gst_bin_add_many (GstBin *bin, GstElement *element_1, ...);
346 void gst_bin_remove_many (GstBin *bin, GstElement *element_1, ...);
347 void gst_bin_watch_for_state_change (GstBin *bin);
349 /* buffer functions */
350 GstBuffer * gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2);
351 GstBuffer * gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2);
352 void gst_buffer_stamp (GstBuffer * dest, const GstBuffer * src);
354 /* atomic functions */
355 void gst_atomic_int_set (gint * atomic_int, gint value);
358 gulong gst_pad_add_data_probe (GstPad * pad,
361 void gst_pad_remove_data_probe (GstPad * pad,
364 gulong gst_pad_add_event_probe (GstPad * pad,
367 void gst_pad_remove_event_probe (GstPad * pad,
370 gulong gst_pad_add_buffer_probe (GstPad * pad,
373 void gst_pad_remove_buffer_probe (GstPad * pad,
377 /* tag emission utility functions */
378 void gst_element_found_tags_for_pad (GstElement * element,
381 void gst_element_found_tags (GstElement * element,
386 #endif /* __GST_UTILS_H__ */