1/* GStreamer
2 * Copyright (C) 2003 David A. Schleef <[email protected]>
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
29G_BEGIN_DECLS
30
31GST_API 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 */
47typedef 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 */
76typedef 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
136typedef struct _GstCaps GstCaps;
137typedef struct _GstStaticCaps GstStaticCaps;
138
139GST_API GstCaps * _gst_caps_any;
140
141GST_API 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 */
207static inline GstCaps *
208gst_caps_ref (GstCaps * caps)
209{
210 return (GstCaps *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (caps));
211}
212
213/**
214 * gst_caps_unref:
215 * @caps: a #GstCaps.
216 *
217 * Unref a #GstCaps and and free all its structures and the
218 * structures' values when the refcount reaches 0.
219 */
220static inline void
221gst_caps_unref (GstCaps * caps)
222{
223 gst_mini_object_unref (GST_MINI_OBJECT_CAST (caps));
224}
225
226/**
227 * gst_clear_caps: (skip)
228 * @caps_ptr: a pointer to a #GstCaps reference
229 *
230 * Clears a reference to a #GstCaps.
231 *
232 * @caps_ptr must not be %NULL.
233 *
234 * If the reference is %NULL then this function does nothing. Otherwise, the
235 * reference count of the caps is decreased and the pointer is set to %NULL.
236 *
237 * Since: 1.16
238 */
239static inline void
240gst_clear_caps (GstCaps ** caps_ptr)
241{
242 gst_clear_mini_object ((GstMiniObject **) caps_ptr);
243}
244
245/* copy caps */
246GST_API
247GstCaps * gst_caps_copy (const GstCaps * caps);
248
249#define gst_caps_copy(caps) GST_CAPS (gst_mini_object_copy (GST_MINI_OBJECT_CAST (caps)))
250
251/**
252 * gst_caps_is_writable:
253 * @caps: a #GstCaps
254 *
255 * Tests if you can safely modify @caps. It is only safe to modify caps when
256 * there is only one owner of the caps - ie, the object is writable.
257 */
258#define gst_caps_is_writable(caps) gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (caps))
259
260/**
261 * gst_caps_make_writable:
262 * @caps: (transfer full): a #GstCaps
263 *
264 * Returns a writable copy of @caps.
265 *
266 * If there is only one reference count on @caps, the caller must be the owner,
267 * and so this function will return the caps object unchanged. If on the other
268 * hand there is more than one reference on the object, a new caps object will
269 * be returned. The caller's reference on @caps will be removed, and instead the
270 * caller will own a reference to the returned object.
271 *
272 * In short, this function unrefs the caps in the argument and refs the caps
273 * that it returns. Don't access the argument after calling this function. See
274 * also: gst_caps_ref().
275 *
276 * Returns: (transfer full): a writable caps which may or may not be the
277 * same as @caps
278 */
279#define gst_caps_make_writable(caps) GST_CAPS_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (caps)))
280
281/**
282 * gst_caps_replace:
283 * @old_caps: (inout) (transfer full) (nullable): pointer to a pointer
284 * to a #GstCaps to be replaced.
285 * @new_caps: (transfer none) (allow-none): pointer to a #GstCaps that will
286 * replace the caps pointed to by @old_caps.
287 *
288 * Modifies a pointer to a #GstCaps to point to a different #GstCaps. The
289 * modification is done atomically (so this is useful for ensuring thread safety
290 * in some cases), and the reference counts are updated appropriately (the old
291 * caps is unreffed, the new is reffed).
292 *
293 * Either @new_caps or the #GstCaps pointed to by @old_caps may be %NULL.
294 *
295 * Returns: %TRUE if @new_caps was different from @old_caps
296 */
297static inline gboolean
298gst_caps_replace (GstCaps **old_caps, GstCaps *new_caps)
299{
300 return gst_mini_object_replace ((GstMiniObject **) old_caps, (GstMiniObject *) new_caps);
301}
302
303/**
304 * gst_caps_take:
305 * @old_caps: (inout) (transfer full): pointer to a pointer to a #GstCaps to be
306 * replaced.
307 * @new_caps: (transfer full) (allow-none): pointer to a #GstCaps that will
308 * replace the caps pointed to by @old_caps.
309 *
310 * Modifies a pointer to a #GstCaps to point to a different #GstCaps. This
311 * function is similar to gst_caps_replace() except that it takes ownership
312 * of @new_caps.
313 *
314 * Returns: %TRUE if @new_caps was different from @old_caps
315 */
316static inline gboolean
317gst_caps_take (GstCaps **old_caps, GstCaps *new_caps)
318{
319 return gst_mini_object_take ((GstMiniObject **) old_caps, (GstMiniObject *) new_caps);
320}
321
322/**
323 * GstCaps:
324 * @mini_object: the parent type
325 *
326 * Object describing media types.
327 */
328struct _GstCaps {
329 GstMiniObject mini_object;
330};
331
332/**
333 * GstStaticCaps:
334 * @caps: the cached #GstCaps
335 * @string: a string describing a caps
336 *
337 * Datastructure to initialize #GstCaps from a string description usually
338 * used in conjunction with GST_STATIC_CAPS() and gst_static_caps_get() to
339 * instantiate a #GstCaps.
340 */
341struct _GstStaticCaps {
342 /*< public >*/
343 GstCaps *caps;
344 const char *string;
345
346 /*< private >*/
347 gpointer _gst_reserved[GST_PADDING];
348};
349
350/**
351 * GstCapsForeachFunc:
352 * @features: the #GstCapsFeatures
353 * @structure: the #GstStructure
354 * @user_data: user data
355 *
356 * A function that will be called in gst_caps_foreach(). The function may
357 * not modify @features or @structure.
358 *
359 * Returns: %TRUE if the foreach operation should continue, %FALSE if
360 * the foreach operation should stop with %FALSE.
361 *
362 * Since: 1.6
363 */
364typedef gboolean (*GstCapsForeachFunc) (GstCapsFeatures *features,
365 GstStructure *structure,
366 gpointer user_data);
367
368/**
369 * GstCapsMapFunc:
370 * @features: the #GstCapsFeatures
371 * @structure: the #GstStructure
372 * @user_data: user data
373 *
374 * A function that will be called in gst_caps_map_in_place(). The function
375 * may modify @features and @structure.
376 *
377 * Returns: %TRUE if the map operation should continue, %FALSE if
378 * the map operation should stop with %FALSE.
379 */
380typedef gboolean (*GstCapsMapFunc) (GstCapsFeatures *features,
381 GstStructure *structure,
382 gpointer user_data);
383
384/**
385 * GstCapsFilterMapFunc:
386 * @features: the #GstCapsFeatures
387 * @structure: the #GstStructure
388 * @user_data: user data
389 *
390 * A function that will be called in gst_caps_filter_and_map_in_place().
391 * The function may modify @features and @structure, and both will be
392 * removed from the caps if %FALSE is returned.
393 *
394 * Returns: %TRUE if the features and structure should be preserved,
395 * %FALSE if it should be removed.
396 */
397typedef gboolean (*GstCapsFilterMapFunc) (GstCapsFeatures *features,
398 GstStructure *structure,
399 gpointer user_data);
400
401
402GST_API
403GType gst_caps_get_type (void);
404
405GST_API
406GstCaps * gst_caps_new_empty (void);
407
408GST_API
409GstCaps * gst_caps_new_any (void);
410
411GST_API
412GstCaps * gst_caps_new_empty_simple (const char *media_type) G_GNUC_WARN_UNUSED_RESULT;
413
414GST_API
415GstCaps * gst_caps_new_simple (const char *media_type,
416 const char *fieldname,
417 ...) G_GNUC_NULL_TERMINATED G_GNUC_WARN_UNUSED_RESULT;
418GST_API
419GstCaps * gst_caps_new_full (GstStructure *struct1,
420 ...) G_GNUC_NULL_TERMINATED G_GNUC_WARN_UNUSED_RESULT;
421GST_API
422GstCaps * gst_caps_new_full_valist (GstStructure *structure,
423 va_list var_args) G_GNUC_WARN_UNUSED_RESULT;
424GST_API
425GType gst_static_caps_get_type (void);
426
427GST_API
428GstCaps * gst_static_caps_get (GstStaticCaps *static_caps);
429
430GST_API
431void gst_static_caps_cleanup (GstStaticCaps *static_caps);
432
433/* manipulation */
434
435GST_API
436void gst_caps_append (GstCaps *caps1,
437 GstCaps *caps2);
438GST_API
439void gst_caps_append_structure (GstCaps *caps,
440 GstStructure *structure);
441GST_API
442void gst_caps_append_structure_full (GstCaps *caps,
443 GstStructure *structure,
444 GstCapsFeatures *features);
445GST_API
446void gst_caps_remove_structure (GstCaps *caps, guint idx);
447
448GST_API
449GstCaps * gst_caps_merge (GstCaps *caps1,
450 GstCaps *caps2) G_GNUC_WARN_UNUSED_RESULT;
451GST_API
452GstCaps * gst_caps_merge_structure (GstCaps *caps,
453 GstStructure *structure) G_GNUC_WARN_UNUSED_RESULT;
454GST_API
455GstCaps * gst_caps_merge_structure_full (GstCaps *caps,
456 GstStructure *structure,
457 GstCapsFeatures *features) G_GNUC_WARN_UNUSED_RESULT;
458
459GST_API
460guint gst_caps_get_size (const GstCaps *caps);
461
462GST_API
463GstStructure * gst_caps_get_structure (const GstCaps *caps,
464 guint index);
465GST_API
466GstStructure * gst_caps_steal_structure (GstCaps *caps,
467 guint index) G_GNUC_WARN_UNUSED_RESULT;
468GST_API
469void gst_caps_set_features (GstCaps *caps,
470 guint index,
471 GstCapsFeatures * features);
472GST_API
473void gst_caps_set_features_simple (GstCaps *caps,
474 GstCapsFeatures * features);
475
476GST_API
477GstCapsFeatures * gst_caps_get_features (const GstCaps *caps,
478 guint index);
479GST_API
480GstCaps * gst_caps_copy_nth (const GstCaps *caps, guint nth) G_GNUC_WARN_UNUSED_RESULT;
481
482GST_API
483GstCaps * gst_caps_truncate (GstCaps *caps) G_GNUC_WARN_UNUSED_RESULT;
484
485GST_API
486void gst_caps_set_value (GstCaps *caps,
487 const char *field,
488 const GValue *value);
489GST_API
490void gst_caps_set_simple (GstCaps *caps,
491 const char *field, ...) G_GNUC_NULL_TERMINATED;
492GST_API
493void gst_caps_set_simple_valist (GstCaps *caps,
494 const char *field,
495 va_list varargs);
496GST_API
497gboolean gst_caps_foreach (const GstCaps *caps,
498 GstCapsForeachFunc func,
499 gpointer user_data);
500GST_API
501gboolean gst_caps_map_in_place (GstCaps *caps,
502 GstCapsMapFunc func,
503 gpointer user_data);
504GST_API
505void gst_caps_filter_and_map_in_place (GstCaps *caps,
506 GstCapsFilterMapFunc func,
507 gpointer user_data);
508
509/* tests */
510
511GST_API
512gboolean gst_caps_is_any (const GstCaps *caps);
513
514GST_API
515gboolean gst_caps_is_empty (const GstCaps *caps);
516
517GST_API
518gboolean gst_caps_is_fixed (const GstCaps *caps);
519
520GST_API
521gboolean gst_caps_is_always_compatible (const GstCaps *caps1,
522 const GstCaps *caps2);
523GST_API
524gboolean gst_caps_is_subset (const GstCaps *subset,
525 const GstCaps *superset);
526GST_API
527gboolean gst_caps_is_subset_structure (const GstCaps *caps,
528 const GstStructure *structure);
529GST_API
530gboolean gst_caps_is_subset_structure_full (const GstCaps *caps,
531 const GstStructure *structure,
532 const GstCapsFeatures *features);
533GST_API
534gboolean gst_caps_is_equal (const GstCaps *caps1,
535 const GstCaps *caps2);
536GST_API
537gboolean gst_caps_is_equal_fixed (const GstCaps *caps1,
538 const GstCaps *caps2);
539GST_API
540gboolean gst_caps_can_intersect (const GstCaps * caps1,
541 const GstCaps * caps2);
542GST_API
543gboolean gst_caps_is_strictly_equal (const GstCaps *caps1,
544 const GstCaps *caps2);
545
546
547/* operations */
548
549GST_API
550GstCaps * gst_caps_intersect (GstCaps *caps1,
551 GstCaps *caps2) G_GNUC_WARN_UNUSED_RESULT;
552GST_API
553GstCaps * gst_caps_intersect_full (GstCaps *caps1,
554 GstCaps *caps2,
555 GstCapsIntersectMode mode) G_GNUC_WARN_UNUSED_RESULT;
556GST_API
557GstCaps * gst_caps_subtract (GstCaps *minuend,
558 GstCaps *subtrahend) G_GNUC_WARN_UNUSED_RESULT;
559GST_API
560GstCaps * gst_caps_normalize (GstCaps *caps) G_GNUC_WARN_UNUSED_RESULT;
561
562GST_API
563GstCaps * gst_caps_simplify (GstCaps *caps) G_GNUC_WARN_UNUSED_RESULT;
564
565GST_API
566GstCaps * gst_caps_fixate (GstCaps *caps) G_GNUC_WARN_UNUSED_RESULT;
567
568/* utility */
569
570GST_API
571gchar * gst_caps_to_string (const GstCaps *caps) G_GNUC_MALLOC;
572
573GST_API
574GstCaps * gst_caps_from_string (const gchar *string) G_GNUC_WARN_UNUSED_RESULT;
575
576#ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
577G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstCaps, gst_caps_unref)
578#endif
579
580G_END_DECLS
581
582#endif /* __GST_CAPS_H__ */
583