2 * Copyright (C) 2013 Collabora Ltd.
3 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:gstcapsfeatures
23 * @short_description: A set of features in caps
26 * #GstCapsFeatures can optionally be set on a #GstCaps to add requirements
27 * for additional features for a specific #GstStructure. Caps structures with
28 * the same name but with a non-equal set of caps features are not compatible.
29 * If a pad supports multiple sets of features it has to add multiple equal
30 * structures with different feature sets to the caps.
32 * Empty #GstCapsFeatures are equivalent with the #GstCapsFeatures that only
33 * contain #GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY. ANY #GstCapsFeatures as
34 * created by gst_caps_features_new_any() are equal to any other #GstCapsFeatures
35 * and can be used to specify that any #GstCapsFeatures would be supported, e.g.
36 * for elements that don't touch buffer memory. #GstCaps with ANY #GstCapsFeatures
37 * are considered non-fixed and during negotiation some #GstCapsFeatures have
40 * Examples for caps features would be the requirement of a specific #GstMemory
41 * types or the requirement of having a specific #GstMeta on the buffer. Features
42 * are given as a string of the format "memory:GstMemoryTypeName" or
43 * "meta:GstMetaAPIName".
53 #include "gst_private.h"
54 #include "gstcapsfeatures.h"
57 GST_DEBUG_CATEGORY_STATIC (gst_caps_features_debug);
58 #define GST_CAT_DEFAULT gst_caps_features_debug
60 struct _GstCapsFeatures
63 gint *parent_refcount;
68 GType _gst_caps_features_type = 0;
69 static gint static_caps_features_parent_refcount = G_MAXINT;
70 GstCapsFeatures *_gst_caps_features_any = NULL;
71 GstCapsFeatures *_gst_caps_features_memory_system_memory = NULL;
72 static GQuark _gst_caps_feature_memory_system_memory = 0;
74 G_DEFINE_BOXED_TYPE (GstCapsFeatures, gst_caps_features,
75 gst_caps_features_copy, gst_caps_features_free);
77 #define IS_MUTABLE(features) \
78 (!features->parent_refcount || \
79 g_atomic_int_get (features->parent_refcount) == 1)
82 gst_caps_features_transform_to_string (const GValue * src_value,
86 _priv_gst_caps_features_initialize (void)
88 GST_DEBUG_CATEGORY_INIT (gst_caps_features_debug, "caps-features", 0,
89 "GstCapsFeatures debug");
91 _gst_caps_features_type = gst_caps_features_get_type ();
92 _gst_caps_feature_memory_system_memory =
93 g_quark_from_static_string (GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY);
95 g_value_register_transform_func (_gst_caps_features_type, G_TYPE_STRING,
96 gst_caps_features_transform_to_string);
98 _gst_caps_features_any = gst_caps_features_new_any ();
99 gst_caps_features_set_parent_refcount (_gst_caps_features_any,
100 &static_caps_features_parent_refcount);
101 _gst_caps_features_memory_system_memory =
102 gst_caps_features_new_id (_gst_caps_feature_memory_system_memory, 0);
103 gst_caps_features_set_parent_refcount
104 (_gst_caps_features_memory_system_memory,
105 &static_caps_features_parent_refcount);
109 _priv_gst_caps_features_cleanup (void)
111 gst_caps_features_set_parent_refcount (_gst_caps_features_any, NULL);
112 gst_caps_features_free (_gst_caps_features_any);
113 _gst_caps_features_any = NULL;
114 gst_caps_features_set_parent_refcount
115 (_gst_caps_features_memory_system_memory, NULL);
116 gst_caps_features_free (_gst_caps_features_memory_system_memory);
117 _gst_caps_features_memory_system_memory = NULL;
121 gst_is_caps_features (gconstpointer obj)
123 const GstCapsFeatures *features = obj;
125 return (obj != NULL && features->type == _gst_caps_features_type);
129 gst_caps_feature_name_is_valid (const gchar * feature)
131 #ifndef G_DISABLE_CHECKS
133 if (g_ascii_isalpha (*feature))
135 else if (*feature == ':')
145 if (*feature == '\0' || !g_ascii_isalpha (*feature))
149 if (g_ascii_isalnum (*feature))
151 else if (*feature == '\0')
162 * gst_caps_features_new_empty:
164 * Creates a new, empty #GstCapsFeatures.
166 * Free-function: gst_caps_features_free
168 * Returns: (transfer full): a new, empty #GstCapsFeatures
173 gst_caps_features_new_empty (void)
175 GstCapsFeatures *features;
177 features = g_slice_new (GstCapsFeatures);
178 features->type = _gst_caps_features_type;
179 features->parent_refcount = NULL;
180 features->array = g_array_new (FALSE, FALSE, sizeof (GQuark));
181 features->is_any = FALSE;
183 GST_TRACE ("created caps features %p", features);
189 * gst_caps_features_new_any:
191 * Creates a new, ANY #GstCapsFeatures. This will be equal
192 * to any other #GstCapsFeatures but caps with these are
195 * Free-function: gst_caps_features_free
197 * Returns: (transfer full): a new, ANY #GstCapsFeatures
202 gst_caps_features_new_any (void)
204 GstCapsFeatures *features;
206 features = gst_caps_features_new_empty ();
207 features->is_any = TRUE;
213 * gst_caps_features_new:
214 * @feature1: name of first feature to set
215 * @...: additional features
217 * Creates a new #GstCapsFeatures with the given features.
218 * The last argument must be %NULL.
220 * Free-function: gst_caps_features_free
222 * Returns: (transfer full): a new, empty #GstCapsFeatures
227 gst_caps_features_new (const gchar * feature1, ...)
229 GstCapsFeatures *features;
232 g_return_val_if_fail (feature1 != NULL, NULL);
234 va_start (varargs, feature1);
235 features = gst_caps_features_new_valist (feature1, varargs);
242 * gst_caps_features_new_valist:
243 * @feature1: name of first feature to set
244 * @varargs: variable argument list
246 * Creates a new #GstCapsFeatures with the given features.
248 * Free-function: gst_caps_features_free
250 * Returns: (transfer full): a new, empty #GstCapsFeatures
255 gst_caps_features_new_valist (const gchar * feature1, va_list varargs)
257 GstCapsFeatures *features;
259 g_return_val_if_fail (feature1 != NULL, NULL);
261 features = gst_caps_features_new_empty ();
264 gst_caps_features_add (features, feature1);
265 feature1 = va_arg (varargs, const gchar *);
272 * gst_caps_features_new_id:
273 * @feature1: name of first feature to set
274 * @...: additional features
276 * Creates a new #GstCapsFeatures with the given features.
277 * The last argument must be 0.
279 * Free-function: gst_caps_features_free
281 * Returns: (transfer full): a new, empty #GstCapsFeatures
286 gst_caps_features_new_id (GQuark feature1, ...)
288 GstCapsFeatures *features;
291 g_return_val_if_fail (feature1 != 0, NULL);
293 va_start (varargs, feature1);
294 features = gst_caps_features_new_id_valist (feature1, varargs);
301 * gst_caps_features_new_id_valist:
302 * @feature1: name of first feature to set
303 * @varargs: variable argument list
305 * Creates a new #GstCapsFeatures with the given features.
307 * Free-function: gst_caps_features_free
309 * Returns: (transfer full): a new, empty #GstCapsFeatures
314 gst_caps_features_new_id_valist (GQuark feature1, va_list varargs)
316 GstCapsFeatures *features;
318 g_return_val_if_fail (feature1 != 0, NULL);
320 features = gst_caps_features_new_empty ();
323 gst_caps_features_add_id (features, feature1);
324 feature1 = va_arg (varargs, GQuark);
331 * gst_caps_features_set_parent_refcount:
332 * @features: a #GstCapsFeatures
333 * @refcount: (in): a pointer to the parent's refcount
335 * Sets the parent_refcount field of #GstCapsFeatures. This field is used to
336 * determine whether a caps features is mutable or not. This function should only be
337 * called by code implementing parent objects of #GstCapsFeatures, as described in
338 * the MT Refcounting section of the design documents.
340 * Returns: %TRUE if the parent refcount could be set.
345 gst_caps_features_set_parent_refcount (GstCapsFeatures * features,
348 g_return_val_if_fail (features != NULL, FALSE);
350 /* if we have a parent_refcount already, we can only clear
351 * if with a NULL refcount */
352 if (features->parent_refcount) {
353 if (refcount != NULL) {
354 g_return_val_if_fail (refcount == NULL, FALSE);
358 if (refcount == NULL) {
359 g_return_val_if_fail (refcount != NULL, FALSE);
364 features->parent_refcount = refcount;
370 * gst_caps_features_copy:
371 * @features: a #GstCapsFeatures to duplicate
373 * Duplicates a #GstCapsFeatures and all its values.
375 * Free-function: gst_caps_features_free
377 * Returns: (transfer full): a new #GstCapsFeatures.
382 gst_caps_features_copy (const GstCapsFeatures * features)
384 GstCapsFeatures *copy;
387 g_return_val_if_fail (features != NULL, NULL);
389 copy = gst_caps_features_new_empty ();
390 n = gst_caps_features_get_size (features);
391 for (i = 0; i < n; i++)
392 gst_caps_features_add_id (copy, gst_caps_features_get_nth_id (features, i));
393 copy->is_any = features->is_any;
399 * gst_caps_features_free:
400 * @features: (in) (transfer full): the #GstCapsFeatures to free
402 * Frees a #GstCapsFeatures and all its values. The caps features must not
403 * have a parent when this function is called.
408 gst_caps_features_free (GstCapsFeatures * features)
410 g_return_if_fail (features != NULL);
411 g_return_if_fail (features->parent_refcount == NULL);
413 g_array_free (features->array, TRUE);
415 memset (features, 0xff, sizeof (GstCapsFeatures));
417 GST_TRACE ("free caps features %p", features);
419 g_slice_free (GstCapsFeatures, features);
423 * gst_caps_features_to_string:
424 * @features: a #GstCapsFeatures
426 * Converts @features to a human-readable string representation.
428 * For debugging purposes its easier to do something like this:
429 * |[<!-- language="C" -->
430 * GST_LOG ("features is %" GST_PTR_FORMAT, features);
432 * This prints the features in human readable form.
434 * Free-function: g_free
436 * Returns: (transfer full): a pointer to string allocated by g_malloc().
437 * g_free() after usage.
442 gst_caps_features_to_string (const GstCapsFeatures * features)
446 g_return_val_if_fail (features != NULL, NULL);
448 s = g_string_sized_new (FEATURES_ESTIMATED_STRING_LEN (features));
450 priv_gst_caps_features_append_to_gstring (features, s);
452 return g_string_free (s, FALSE);
456 priv_gst_caps_features_append_to_gstring (const GstCapsFeatures * features,
461 g_return_if_fail (features != NULL);
463 if (features->array->len == 0 && features->is_any) {
464 g_string_append (s, "ANY");
468 n = features->array->len;
469 for (i = 0; i < n; i++) {
470 GQuark *quark = &g_array_index (features->array, GQuark, i);
472 g_string_append (s, g_quark_to_string (*quark));
474 g_string_append (s, ", ");
479 * gst_caps_features_from_string:
480 * @features: a string representation of a #GstCapsFeatures.
482 * Creates a #GstCapsFeatures from a string representation.
484 * Free-function: gst_caps_features_free
486 * Returns: (transfer full) (nullable): a new #GstCapsFeatures or
487 * %NULL when the string could not be parsed. Free with
488 * gst_caps_features_free() after use.
493 gst_caps_features_from_string (const gchar * features)
495 GstCapsFeatures *ret;
496 gboolean escape = FALSE;
497 const gchar *features_orig = features;
498 const gchar *feature;
500 ret = gst_caps_features_new_empty ();
502 if (!features || *features == '\0')
505 if (strcmp (features, "ANY") == 0) {
510 /* Skip trailing spaces */
511 while (*features == ' ')
522 } else if ((!escape && c == ',') || c == '\0') {
523 guint len = features - feature + 1;
528 g_warning ("Failed deserialize caps features '%s'", features_orig);
529 gst_caps_features_free (ret);
533 tmp = g_malloc (len);
534 memcpy (tmp, feature, len - 1);
543 if (strstr (tmp, " ") != NULL || *tmp == '\0') {
545 g_warning ("Failed deserialize caps features '%s'", features_orig);
546 gst_caps_features_free (ret);
550 gst_caps_features_add (ret, tmp);
556 /* Skip to the next value */
558 while (*features == ' ')
571 * gst_caps_features_get_size:
572 * @features: a #GstCapsFeatures.
574 * Returns the number of features in @features.
576 * Returns: The number of features in @features.
581 gst_caps_features_get_size (const GstCapsFeatures * features)
583 g_return_val_if_fail (features != NULL, 0);
585 return features->array->len;
589 * gst_caps_features_get_nth:
590 * @features: a #GstCapsFeatures.
591 * @i: index of the feature
593 * Returns the @i-th feature of @features.
595 * Returns: The @i-th feature of @features.
600 gst_caps_features_get_nth (const GstCapsFeatures * features, guint i)
602 const gchar *feature;
605 g_return_val_if_fail (features != NULL, NULL);
607 quark = gst_caps_features_get_nth_id (features, i);
611 feature = g_quark_to_string (quark);
616 * gst_caps_features_get_nth_id:
617 * @features: a #GstCapsFeatures.
618 * @i: index of the feature
620 * Returns the @i-th feature of @features.
622 * Returns: The @i-th feature of @features.
627 gst_caps_features_get_nth_id (const GstCapsFeatures * features, guint i)
631 g_return_val_if_fail (features != NULL, 0);
632 g_return_val_if_fail (i < features->array->len, 0);
634 quark = &g_array_index (features->array, GQuark, i);
640 * gst_caps_features_contains:
641 * @features: a #GstCapsFeatures.
642 * @feature: a feature
644 * Check if @features contains @feature.
646 * Returns: %TRUE if @features contains @feature.
651 gst_caps_features_contains (const GstCapsFeatures * features,
652 const gchar * feature)
654 g_return_val_if_fail (features != NULL, FALSE);
655 g_return_val_if_fail (feature != NULL, FALSE);
657 return gst_caps_features_contains_id (features,
658 g_quark_from_string (feature));
662 * gst_caps_features_contains_id:
663 * @features: a #GstCapsFeatures.
664 * @feature: a feature
666 * Check if @features contains @feature.
668 * Returns: %TRUE if @features contains @feature.
673 gst_caps_features_contains_id (const GstCapsFeatures * features, GQuark feature)
677 g_return_val_if_fail (features != NULL, FALSE);
678 g_return_val_if_fail (feature != 0, FALSE);
680 if (features->is_any)
683 n = features->array->len;
685 return feature == _gst_caps_feature_memory_system_memory;
687 for (i = 0; i < n; i++) {
688 if (gst_caps_features_get_nth_id (features, i) == feature)
696 * gst_caps_features_is_equal:
697 * @features1: a #GstCapsFeatures.
698 * @features2: a #GstCapsFeatures.
700 * Check if @features1 and @features2 are equal.
702 * Returns: %TRUE if @features1 and @features2 are equal.
707 gst_caps_features_is_equal (const GstCapsFeatures * features1,
708 const GstCapsFeatures * features2)
712 g_return_val_if_fail (features1 != NULL, FALSE);
713 g_return_val_if_fail (features2 != NULL, FALSE);
715 if (features1->is_any || features2->is_any)
718 /* Check for the sysmem==empty case */
719 if (features1->array->len == 0 && features2->array->len == 0)
721 if (features1->array->len == 0 && features2->array->len == 1
722 && gst_caps_features_contains_id (features2,
723 _gst_caps_feature_memory_system_memory))
725 if (features2->array->len == 0 && features1->array->len == 1
726 && gst_caps_features_contains_id (features1,
727 _gst_caps_feature_memory_system_memory))
730 if (features1->array->len != features2->array->len)
733 n = features1->array->len;
734 for (i = 0; i < n; i++)
735 if (!gst_caps_features_contains_id (features2,
736 gst_caps_features_get_nth_id (features1, i)))
743 * gst_caps_features_is_any:
744 * @features: a #GstCapsFeatures.
746 * Check if @features is %GST_CAPS_FEATURES_ANY.
748 * Returns: %TRUE if @features is %GST_CAPS_FEATURES_ANY.
753 gst_caps_features_is_any (const GstCapsFeatures * features)
755 g_return_val_if_fail (features != NULL, FALSE);
757 return features->is_any;
761 * gst_caps_features_add:
762 * @features: a #GstCapsFeatures.
763 * @feature: a feature.
765 * Adds @feature to @features.
770 gst_caps_features_add (GstCapsFeatures * features, const gchar * feature)
772 g_return_if_fail (features != NULL);
773 g_return_if_fail (IS_MUTABLE (features));
774 g_return_if_fail (feature != NULL);
775 g_return_if_fail (!features->is_any);
777 gst_caps_features_add_id (features, g_quark_from_string (feature));
781 * gst_caps_features_add_id:
782 * @features: a #GstCapsFeatures.
783 * @feature: a feature.
785 * Adds @feature to @features.
790 gst_caps_features_add_id (GstCapsFeatures * features, GQuark feature)
792 g_return_if_fail (features != NULL);
793 g_return_if_fail (IS_MUTABLE (features));
794 g_return_if_fail (feature != 0);
795 g_return_if_fail (!features->is_any);
797 if (!gst_caps_feature_name_is_valid (g_quark_to_string (feature))) {
798 g_warning ("Invalid caps feature name: %s", g_quark_to_string (feature));
802 /* If features is empty it will contain sysmem, however
803 * we want to add it explicitely if it is tried to be
804 * added as first features
806 if (features->array->len > 0
807 && gst_caps_features_contains_id (features, feature))
810 g_array_append_val (features->array, feature);
814 * gst_caps_features_remove:
815 * @features: a #GstCapsFeatures.
816 * @feature: a feature.
818 * Removes @feature from @features.
823 gst_caps_features_remove (GstCapsFeatures * features, const gchar * feature)
825 g_return_if_fail (features != NULL);
826 g_return_if_fail (IS_MUTABLE (features));
827 g_return_if_fail (feature != NULL);
829 gst_caps_features_remove_id (features, g_quark_from_string (feature));
833 * gst_caps_features_remove_id:
834 * @features: a #GstCapsFeatures.
835 * @feature: a feature.
837 * Removes @feature from @features.
842 gst_caps_features_remove_id (GstCapsFeatures * features, GQuark feature)
846 g_return_if_fail (features != NULL);
847 g_return_if_fail (IS_MUTABLE (features));
848 g_return_if_fail (feature != 0);
850 n = features->array->len;
851 for (i = 0; i < n; i++) {
852 GQuark quark = gst_caps_features_get_nth_id (features, i);
854 if (quark == feature) {
855 g_array_remove_index_fast (features->array, i);
862 gst_caps_features_transform_to_string (const GValue * src_value,
865 g_return_if_fail (src_value != NULL);
866 g_return_if_fail (dest_value != NULL);
868 dest_value->data[0].v_pointer =
869 gst_caps_features_to_string (src_value->data[0].v_pointer);