44208356539ec1f051b6f1d9e2f8eec9e300c597
[platform/upstream/gstreamer.git] / gst / gstcaps.h
1 /* GStreamer
2  * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifndef __GST_CAPS_H__
21 #define __GST_CAPS_H__
22
23 #include <gst/gstconfig.h>
24 #include <gst/gstminiobject.h>
25 #include <gst/gststructure.h>
26 #include <gst/glib-compat.h>
27
28 G_BEGIN_DECLS
29
30 GST_EXPORT GType _gst_caps_type;
31
32 #define GST_TYPE_CAPS             (_gst_caps_type)
33 #define GST_IS_CAPS(obj)          (GST_IS_MINI_OBJECT_TYPE((obj), GST_TYPE_CAPS))
34 #define GST_CAPS_CAST(obj)        ((GstCaps*)(obj))
35 #define GST_CAPS(obj)             (GST_CAPS_CAST(obj))
36
37 #define GST_TYPE_STATIC_CAPS      (gst_static_caps_get_type())
38
39 /**
40  * GstCapsFlags:
41  * @GST_CAPS_FLAG_ANY: Caps has no specific content, but can contain
42  *    anything.
43  *
44  * Extra flags for a caps.
45  */
46 typedef enum {
47   GST_CAPS_FLAG_ANY     = (GST_MINI_OBJECT_FLAG_LAST << 0)
48 } GstCapsFlags;
49
50 /**
51  * GstCapsIntersectMode:
52  * @GST_CAPS_INTERSECT_ZIG_ZAG  : Zig-zags over both caps.
53  * @GST_CAPS_INTERSECT_FIRST    : Keeps the first caps order.
54  *
55  * Modes of caps intersection
56  *
57  * @GST_CAPS_INTERSECT_ZIG_ZAG tries to preserve overall order of both caps
58  * by iterating on the caps' structures as the following matrix shows:
59  * |[
60  *          caps1
61  *       +-------------
62  *       | 1  2  4  7
63  * caps2 | 3  5  8 10
64  *       | 6  9 11 12
65  * ]|
66  * Used when there is no explicit precedence of one caps over the other. e.g.
67  * tee's sink pad getcaps function, it will probe its src pad peers' for their
68  * caps and intersect them with this mode.
69  *
70  * @GST_CAPS_INTERSECT_FIRST is useful when an element wants to preserve
71  * another element's caps priority order when intersecting with its own caps.
72  * Example: If caps1 is [A, B, C] and caps2 is [E, B, D, A], the result
73  * would be [A, B], maintaining the first caps priority on the intersection.
74  *
75  * Since: 0.10.33
76  */
77 typedef enum {
78   GST_CAPS_INTERSECT_ZIG_ZAG            =  0,
79   GST_CAPS_INTERSECT_FIRST              =  1
80 } GstCapsIntersectMode;
81
82 /**
83  * GST_CAPS_ANY:
84  *
85  * Means that the element/pad can output 'anything'. Useful for elements
86  * that output unknown media, such as filesrc. This macro returns a singleton and
87  * should not be unreffed.
88  */
89 #define GST_CAPS_ANY              _gst_caps_any
90 /**
91  * GST_CAPS_NONE:
92  *
93  * The opposite of %GST_CAPS_ANY: it means that the pad/element outputs an
94  * undefined media type that can not be detected. This macro returns a singleton
95  * and should not be unreffed.
96  */
97 #define GST_CAPS_NONE             _gst_caps_none
98
99 /**
100  * GST_STATIC_CAPS_ANY:
101  *
102  * Creates a new #GstCaps static caps that matches anything.
103  * This can be used in pad templates.
104  */
105 #define GST_STATIC_CAPS_ANY       GST_STATIC_CAPS("ANY")
106 /**
107  * GST_STATIC_CAPS_NONE:
108  *
109  * Creates a new #GstCaps static caps that matches nothing.
110  * This can be used in pad templates.
111  */
112 #define GST_STATIC_CAPS_NONE      GST_STATIC_CAPS("NONE")
113
114 /**
115  * GST_CAPS_IS_SIMPLE:
116  * @caps: the #GstCaps instance to check
117  *
118  * Convenience macro that checks if the number of structures in the given caps
119  * is exactly one.
120  */
121 #define GST_CAPS_IS_SIMPLE(caps) (gst_caps_get_size(caps) == 1)
122
123 /**
124  * GST_STATIC_CAPS:
125  * @string: the string describing the caps
126  *
127  * Creates a new #GstCaps static caps from an input string.
128  * This can be used in pad templates.
129  */
130 #define GST_STATIC_CAPS(string) \
131 { \
132   /* caps */ NULL, \
133   /* string */ string, \
134   GST_PADDING_INIT \
135 }
136
137 typedef struct _GstCaps GstCaps;
138 typedef struct _GstStaticCaps GstStaticCaps;
139
140 GST_EXPORT GstCaps * _gst_caps_any;
141 GST_EXPORT GstCaps * _gst_caps_none;
142 /**
143  * GST_CAPS_FLAGS:
144  * @caps: a #GstCaps.
145  *
146  * A flags word containing #GstCapsFlags flags set on this caps.
147  */
148 #define GST_CAPS_FLAGS(caps)                    GST_MINI_OBJECT_FLAGS(caps)
149
150 /* refcount */
151 /**
152  * GST_CAPS_REFCOUNT:
153  * @caps: a #GstCaps
154  *
155  * Get access to the reference count field of the caps
156  */
157 #define GST_CAPS_REFCOUNT(caps)                 GST_MINI_OBJECT_REFCOUNT(caps)
158 /**
159  * GST_CAPS_REFCOUNT_VALUE:
160  * @caps: a #GstCaps
161  *
162  * Get the reference count value of the caps.
163  */
164 #define GST_CAPS_REFCOUNT_VALUE(caps)           GST_MINI_OBJECT_REFCOUNT_VALUE(caps)
165
166 /**
167  * GST_CAPS_FLAG_IS_SET:
168  * @caps: a #GstCaps.
169  * @flag: the #GstCapsFlags to check.
170  *
171  * Gives the status of a specific flag on a caps.
172  */
173 #define GST_CAPS_FLAG_IS_SET(caps,flag)        GST_MINI_OBJECT_FLAG_IS_SET (caps, flag)
174 /**
175  * GST_CAPS_FLAG_SET:
176  * @caps: a #GstCaps.
177  * @flag: the #GstCapsFlags to set.
178  *
179  * Sets a caps flag on a caps.
180  */
181 #define GST_CAPS_FLAG_SET(caps,flag)           GST_MINI_OBJECT_FLAG_SET (caps, flag)
182 /**
183  * GST_CAPS_FLAG_UNSET:
184  * @caps: a #GstCaps.
185  * @flag: the #GstCapsFlags to clear.
186  *
187  * Clears a caps flag.
188  */
189 #define GST_CAPS_FLAG_UNSET(caps,flag)         GST_MINI_OBJECT_FLAG_UNSET (caps, flag)
190
191 /* refcounting */
192 /**
193  * gst_caps_ref:
194  * @caps: the #GstCaps to reference
195  *
196  * Add a reference to a #GstCaps object.
197  *
198  * From this point on, until the caller calls gst_caps_unref() or
199  * gst_caps_make_writable(), it is guaranteed that the caps object will not
200  * change. This means its structures won't change, etc. To use a #GstCaps
201  * object, you must always have a refcount on it -- either the one made
202  * implicitly by e.g. gst_caps_new_simple(), or via taking one explicitly with
203  * this function.
204  *
205  * Returns: the same #GstCaps object.
206  */
207 #ifdef _FOOL_GTK_DOC_
208 G_INLINE_FUNC GstCaps * gst_caps_ref (GstCaps * caps);
209 #endif
210
211 static inline GstCaps *
212 gst_caps_ref (GstCaps * caps)
213 {
214   return (GstCaps *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (caps));
215 }
216
217 /**
218  * gst_caps_unref:
219  * @caps: a #GstCaps.
220  *
221  * Unref a #GstCaps and and free all its structures and the
222  * structures' values when the refcount reaches 0.
223  */
224 #ifdef _FOOL_GTK_DOC_
225 G_INLINE_FUNC void gst_caps_unref (GstCaps * caps);
226 #endif
227
228 static inline void
229 gst_caps_unref (GstCaps * caps)
230 {
231   gst_mini_object_unref (GST_MINI_OBJECT_CAST (caps));
232 }
233
234 /* copy caps */
235 /**
236  * gst_caps_copy:
237  * @caps: a #GstCaps.
238  *
239  * Creates a new #GstCaps as a copy of the old @caps. The new caps will have a
240  * refcount of 1, owned by the caller. The structures are copied as well.
241  *
242  * Note that this function is the semantic equivalent of a gst_caps_ref()
243  * followed by a gst_caps_make_writable(). If you only want to hold on to a
244  * reference to the data, you should use gst_caps_ref().
245  *
246  * When you are finished with the caps, call gst_caps_unref() on it.
247  *
248  * Returns: the new #GstCaps
249  */
250 #ifdef _FOOL_GTK_DOC_
251 G_INLINE_FUNC GstCaps * gst_caps_copy (const GstCaps * caps);
252 #endif
253
254 static inline GstCaps *
255 gst_caps_copy (const GstCaps * caps)
256 {
257   return GST_CAPS (gst_mini_object_copy (GST_MINI_OBJECT_CAST (caps)));
258 }
259
260 /**
261  * gst_caps_is_writable:
262  * @caps: a #GstCaps
263  *
264  * Tests if you can safely modify @caps. It is only safe to modify caps when
265  * there is only one owner of the caps - ie, the refcount is 1.
266  */
267 #define         gst_caps_is_writable(caps)     gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (caps))
268
269 /**
270  * gst_caps_make_writable:
271  * @caps: (transfer full): a #GstCaps
272  *
273  * Returns a writable copy of @caps.
274  *
275  * If there is only one reference count on @caps, the caller must be the owner,
276  * and so this function will return the caps object unchanged. If on the other
277  * hand there is more than one reference on the object, a new caps object will
278  * be returned. The caller's reference on @caps will be removed, and instead the
279  * caller will own a reference to the returned object.
280  *
281  * In short, this function unrefs the caps in the argument and refs the caps
282  * that it returns. Don't access the argument after calling this function. See
283  * also: gst_caps_ref().
284  *
285  * Returns: (transfer full): a writable caps which may or may not be the
286  *     same as @caps
287  */
288 #define         gst_caps_make_writable(caps)   GST_CAPS_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (caps)))
289
290 /**
291  * gst_caps_replace:
292  * @old_caps: (inout) (transfer full): pointer to a pointer to a #GstCaps to be
293  *     replaced.
294  * @new_caps: (transfer none) (allow-none): pointer to a #GstCaps that will
295  *     replace the caps pointed to by @ocaps.
296  *
297  * Modifies a pointer to a #GstCaps to point to a different #GstCaps. The
298  * modification is done atomically (so this is useful for ensuring thread safety
299  * in some cases), and the reference counts are updated appropriately (the old
300  * caps is unreffed, the new is reffed).
301  *
302  * Either @ncaps or the #GstCaps pointed to by @ocaps may be NULL.
303  *
304  * Returns: TRUE if @new_caps was different from @old_caps
305  */
306 #ifdef _FOOL_GTK_DOC_
307 G_INLINE_FUNC gboolean gst_caps_replace (GstCaps **old_caps, GstCaps *new_caps);
308 #endif
309
310 static inline gboolean
311 gst_caps_replace (GstCaps **old_caps, GstCaps *new_caps)
312 {
313     return gst_mini_object_replace ((GstMiniObject **) old_caps, (GstMiniObject *) new_caps);
314 }
315
316 /**
317  * gst_caps_take:
318  * @old_caps: (inout) (transfer full): pointer to a pointer to a #GstCaps to be
319  *     replaced.
320  * @new_caps: (transfer full) (allow-none): pointer to a #GstCaps that will
321  *     replace the caps pointed to by @ocaps.
322  *
323  * Modifies a pointer to a #GstCaps to point to a different #GstCaps. This
324  * function is similar to gst_caps_replace() except that it takes ownership
325  * of @new_caps.
326  *
327  * Returns: TRUE if @new_caps was different from @old_caps
328  */
329 #ifdef _FOOL_GTK_DOC_
330 G_INLINE_FUNC gboolean gst_caps_take (GstCaps **old_caps, GstCaps *new_caps);
331 #endif
332
333 static inline gboolean
334 gst_caps_take (GstCaps **old_caps, GstCaps *new_caps)
335 {
336     return gst_mini_object_take ((GstMiniObject **) old_caps, (GstMiniObject *) new_caps);
337 }
338
339 /**
340  * GstCaps:
341  * @mini_object: the parent type
342  *
343  * Object describing media types.
344  */
345 struct _GstCaps {
346   GstMiniObject mini_object;
347 };
348
349 /**
350  * GstStaticCaps:
351  * @caps: the cached #GstCaps
352  * @string: a string describing a caps
353  *
354  * Datastructure to initialize #GstCaps from a string description usually
355  * used in conjunction with GST_STATIC_CAPS() and gst_static_caps_get() to
356  * instantiate a #GstCaps.
357  */
358 struct _GstStaticCaps {
359   /*< public >*/
360   GstCaps *caps;
361   const char *string;
362
363   /*< private >*/
364   gpointer _gst_reserved[GST_PADDING];
365 };
366
367 GType             gst_caps_get_type                (void);
368
369 GstCaps *         gst_caps_new_empty               (void);
370 GstCaps *         gst_caps_new_any                 (void);
371 GstCaps *         gst_caps_new_empty_simple        (const char    *media_type) G_GNUC_WARN_UNUSED_RESULT;
372 GstCaps *         gst_caps_new_simple              (const char    *media_type,
373                                                     const char    *fieldname,
374                                                     ...) G_GNUC_NULL_TERMINATED G_GNUC_WARN_UNUSED_RESULT;
375 GstCaps *         gst_caps_new_full                (GstStructure  *struct1,
376                                                     ...) G_GNUC_NULL_TERMINATED G_GNUC_WARN_UNUSED_RESULT;
377 GstCaps *         gst_caps_new_full_valist         (GstStructure  *structure,
378                                                     va_list        var_args) G_GNUC_WARN_UNUSED_RESULT;
379
380 GType             gst_static_caps_get_type         (void);
381 GstCaps *         gst_static_caps_get              (GstStaticCaps *static_caps);
382 void              gst_static_caps_cleanup          (GstStaticCaps *static_caps);
383
384 /* manipulation */
385 void              gst_caps_append                  (GstCaps       *caps1,
386                                                     GstCaps       *caps2);
387 void              gst_caps_append_structure        (GstCaps       *caps,
388                                                     GstStructure  *structure);
389 void              gst_caps_remove_structure        (GstCaps       *caps, guint idx);
390 GstCaps *         gst_caps_merge                   (GstCaps       *caps1,
391                                                     GstCaps       *caps2) G_GNUC_WARN_UNUSED_RESULT;
392 GstCaps *         gst_caps_merge_structure         (GstCaps       *caps,
393                                                     GstStructure  *structure) G_GNUC_WARN_UNUSED_RESULT;
394 guint             gst_caps_get_size                (const GstCaps *caps);
395 GstStructure *    gst_caps_get_structure           (const GstCaps *caps,
396                                                     guint          index);
397 GstStructure *    gst_caps_steal_structure         (GstCaps       *caps,
398                                                     guint          index) G_GNUC_WARN_UNUSED_RESULT;
399 GstCaps *         gst_caps_copy_nth                (const GstCaps *caps, guint nth) G_GNUC_WARN_UNUSED_RESULT;
400 GstCaps *         gst_caps_truncate                (GstCaps       *caps) G_GNUC_WARN_UNUSED_RESULT;
401 void              gst_caps_set_value               (GstCaps       *caps,
402                                                     const char    *field,
403                                                     const GValue  *value);
404 void              gst_caps_set_simple              (GstCaps       *caps,
405                                                     const char    *field, ...) G_GNUC_NULL_TERMINATED;
406 void              gst_caps_set_simple_valist       (GstCaps       *caps,
407                                                     const char    *field,
408                                                     va_list        varargs);
409
410 /* tests */
411 gboolean          gst_caps_is_any                  (const GstCaps *caps);
412 gboolean          gst_caps_is_empty                (const GstCaps *caps);
413 gboolean          gst_caps_is_fixed                (const GstCaps *caps);
414 gboolean          gst_caps_is_always_compatible    (const GstCaps *caps1,
415                                                     const GstCaps *caps2);
416 gboolean          gst_caps_is_subset               (const GstCaps *subset,
417                                                     const GstCaps *superset);
418 gboolean          gst_caps_is_subset_structure     (const GstCaps *caps,
419                                                     const GstStructure *structure);
420 gboolean          gst_caps_is_equal                (const GstCaps *caps1,
421                                                     const GstCaps *caps2);
422 gboolean          gst_caps_is_equal_fixed          (const GstCaps *caps1,
423                                                     const GstCaps *caps2);
424 gboolean          gst_caps_can_intersect           (const GstCaps * caps1,
425                                                     const GstCaps * caps2);
426 gboolean          gst_caps_is_strictly_equal       (const GstCaps *caps1,
427                                                     const GstCaps *caps2);
428
429
430 /* operations */
431 GstCaps *         gst_caps_intersect               (GstCaps *caps1,
432                                                     GstCaps *caps2) G_GNUC_WARN_UNUSED_RESULT;
433 GstCaps *         gst_caps_intersect_full          (GstCaps *caps1,
434                                                     GstCaps *caps2,
435                                                     GstCapsIntersectMode mode) G_GNUC_WARN_UNUSED_RESULT;
436 GstCaps *         gst_caps_subtract                (GstCaps *minuend,
437                                                     GstCaps *subtrahend) G_GNUC_WARN_UNUSED_RESULT;
438 GstCaps *         gst_caps_union                   (GstCaps *caps1,
439                                                     GstCaps *caps2) G_GNUC_WARN_UNUSED_RESULT;
440 GstCaps *         gst_caps_normalize               (GstCaps *caps) G_GNUC_WARN_UNUSED_RESULT;
441 gboolean          gst_caps_do_simplify             (GstCaps *caps);
442
443 GstCaps *         gst_caps_fixate                  (GstCaps *caps) G_GNUC_WARN_UNUSED_RESULT;
444
445 /* utility */
446 gchar *           gst_caps_to_string               (const GstCaps *caps) G_GNUC_MALLOC;
447 GstCaps *         gst_caps_from_string             (const gchar   *string) G_GNUC_WARN_UNUSED_RESULT;
448
449 G_END_DECLS
450
451 #endif /* __GST_CAPS_H__ */