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