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 * @title: GstCapsFeatures
24 * @short_description: A set of features in caps
27 * #GstCapsFeatures can optionally be set on a #GstCaps to add requirements
28 * for additional features for a specific #GstStructure. Caps structures with
29 * the same name but with a non-equal set of caps features are not compatible.
30 * If a pad supports multiple sets of features it has to add multiple equal
31 * structures with different feature sets to the caps.
33 * Empty #GstCapsFeatures are equivalent with the #GstCapsFeatures that only
34 * contain #GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY. ANY #GstCapsFeatures as
35 * created by gst_caps_features_new_any() are equal to any other #GstCapsFeatures
36 * and can be used to specify that any #GstCapsFeatures would be supported, e.g.
37 * for elements that don't touch buffer memory. #GstCaps with ANY #GstCapsFeatures
38 * are considered non-fixed and during negotiation some #GstCapsFeatures have
41 * Examples for caps features would be the requirement of a specific #GstMemory
42 * types or the requirement of having a specific #GstMeta on the buffer. Features
43 * are given as a string of the format `memory:GstMemoryTypeName` or
44 * `meta:GstMetaAPIName`.
54 #include "gst_private.h"
55 #include "gstcapsfeatures.h"
58 GST_DEBUG_CATEGORY_STATIC (gst_caps_features_debug);
59 #define GST_CAT_DEFAULT gst_caps_features_debug
61 struct _GstCapsFeatures
64 gint *parent_refcount;
69 GType _gst_caps_features_type = 0;
70 static gint static_caps_features_parent_refcount = G_MAXINT;
71 GstCapsFeatures *_gst_caps_features_any = NULL;
72 GstCapsFeatures *_gst_caps_features_memory_system_memory = NULL;
73 static GQuark _gst_caps_feature_memory_system_memory = 0;
75 G_DEFINE_BOXED_TYPE (GstCapsFeatures, gst_caps_features,
76 gst_caps_features_copy, gst_caps_features_free);
78 #define IS_MUTABLE(features) \
79 (!features->parent_refcount || \
80 g_atomic_int_get (features->parent_refcount) == 1)
83 gst_caps_features_transform_to_string (const GValue * src_value,
87 _priv_gst_caps_features_initialize (void)
89 GST_DEBUG_CATEGORY_INIT (gst_caps_features_debug, "caps-features", 0,
90 "GstCapsFeatures debug");
92 _gst_caps_features_type = gst_caps_features_get_type ();
93 _gst_caps_feature_memory_system_memory =
94 g_quark_from_static_string (GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY);
96 g_value_register_transform_func (_gst_caps_features_type, G_TYPE_STRING,
97 gst_caps_features_transform_to_string);
99 _gst_caps_features_any = gst_caps_features_new_any ();
100 gst_caps_features_set_parent_refcount (_gst_caps_features_any,
101 &static_caps_features_parent_refcount);
102 _gst_caps_features_memory_system_memory =
103 gst_caps_features_new_id (_gst_caps_feature_memory_system_memory, 0);
104 gst_caps_features_set_parent_refcount
105 (_gst_caps_features_memory_system_memory,
106 &static_caps_features_parent_refcount);
110 _priv_gst_caps_features_cleanup (void)
112 gst_caps_features_set_parent_refcount (_gst_caps_features_any, NULL);
113 gst_caps_features_free (_gst_caps_features_any);
114 _gst_caps_features_any = NULL;
115 gst_caps_features_set_parent_refcount
116 (_gst_caps_features_memory_system_memory, NULL);
117 gst_caps_features_free (_gst_caps_features_memory_system_memory);
118 _gst_caps_features_memory_system_memory = NULL;
122 * gst_is_caps_features:
124 * Checks if @obj is a #GstCapsFeatures
126 * Returns: %TRUE if @obj is a #GstCapsFeatures %FALSE otherwise
129 gst_is_caps_features (gconstpointer obj)
131 const GstCapsFeatures *features = obj;
133 return (obj != NULL && features->type == _gst_caps_features_type);
137 gst_caps_feature_name_is_valid (const gchar * feature)
139 #ifndef G_DISABLE_CHECKS
141 if (g_ascii_isalpha (*feature))
143 else if (*feature == ':')
153 if (*feature == '\0' || !g_ascii_isalpha (*feature))
157 if (g_ascii_isalnum (*feature))
159 else if (*feature == '\0')
170 * gst_caps_features_new_empty:
172 * Creates a new, empty #GstCapsFeatures.
174 * Returns: (transfer full): a new, empty #GstCapsFeatures
179 gst_caps_features_new_empty (void)
181 GstCapsFeatures *features;
183 features = g_slice_new (GstCapsFeatures);
184 features->type = _gst_caps_features_type;
185 features->parent_refcount = NULL;
186 features->array = g_array_new (FALSE, FALSE, sizeof (GQuark));
187 features->is_any = FALSE;
189 GST_TRACE ("created caps features %p", features);
195 * gst_caps_features_new_any:
197 * Creates a new, ANY #GstCapsFeatures. This will be equal
198 * to any other #GstCapsFeatures but caps with these are
201 * Returns: (transfer full): a new, ANY #GstCapsFeatures
206 gst_caps_features_new_any (void)
208 GstCapsFeatures *features;
210 features = gst_caps_features_new_empty ();
211 features->is_any = TRUE;
217 * gst_caps_features_new_single:
218 * @feature: The feature
220 * Creates a new #GstCapsFeatures with a single feature.
222 * Returns: (transfer full): a new #GstCapsFeatures
227 gst_caps_features_new_single (const gchar * feature)
229 GstCapsFeatures *features;
230 g_return_val_if_fail (feature != NULL, NULL);
232 features = gst_caps_features_new_empty ();
233 gst_caps_features_add (features, feature);
238 * gst_caps_features_new:
239 * @feature1: name of first feature to set
240 * @...: additional features
242 * Creates a new #GstCapsFeatures with the given features.
243 * The last argument must be %NULL.
245 * Returns: (transfer full): a new, empty #GstCapsFeatures
250 gst_caps_features_new (const gchar * feature1, ...)
252 GstCapsFeatures *features;
255 g_return_val_if_fail (feature1 != NULL, NULL);
257 va_start (varargs, feature1);
258 features = gst_caps_features_new_valist (feature1, varargs);
265 * gst_caps_features_new_valist:
266 * @feature1: name of first feature to set
267 * @varargs: variable argument list
269 * Creates a new #GstCapsFeatures with the given features.
271 * Returns: (transfer full): a new, empty #GstCapsFeatures
276 gst_caps_features_new_valist (const gchar * feature1, va_list varargs)
278 GstCapsFeatures *features;
280 g_return_val_if_fail (feature1 != NULL, NULL);
282 features = gst_caps_features_new_empty ();
285 gst_caps_features_add (features, feature1);
286 feature1 = va_arg (varargs, const gchar *);
293 * gst_caps_features_new_id:
294 * @feature1: name of first feature to set
295 * @...: additional features
297 * Creates a new #GstCapsFeatures with the given features.
298 * The last argument must be 0.
300 * Returns: (transfer full): a new, empty #GstCapsFeatures
305 gst_caps_features_new_id (GQuark feature1, ...)
307 GstCapsFeatures *features;
310 g_return_val_if_fail (feature1 != 0, NULL);
312 va_start (varargs, feature1);
313 features = gst_caps_features_new_id_valist (feature1, varargs);
320 * gst_caps_features_new_id_valist:
321 * @feature1: name of first feature to set
322 * @varargs: variable argument list
324 * Creates a new #GstCapsFeatures with the given features.
326 * Returns: (transfer full): a new, empty #GstCapsFeatures
331 gst_caps_features_new_id_valist (GQuark feature1, va_list varargs)
333 GstCapsFeatures *features;
335 g_return_val_if_fail (feature1 != 0, NULL);
337 features = gst_caps_features_new_empty ();
340 gst_caps_features_add_id (features, feature1);
341 feature1 = va_arg (varargs, GQuark);
348 * gst_caps_features_set_parent_refcount:
349 * @features: a #GstCapsFeatures
350 * @refcount: (in): a pointer to the parent's refcount
352 * Sets the parent_refcount field of #GstCapsFeatures. This field is used to
353 * determine whether a caps features is mutable or not. This function should only be
354 * called by code implementing parent objects of #GstCapsFeatures, as described in
355 * [the MT refcounting design document](additional/design/MT-refcounting.md).
357 * Returns: %TRUE if the parent refcount could be set.
362 gst_caps_features_set_parent_refcount (GstCapsFeatures * features,
365 g_return_val_if_fail (features != NULL, FALSE);
367 /* if we have a parent_refcount already, we can only clear
368 * if with a NULL refcount */
369 if (features->parent_refcount) {
370 if (refcount != NULL) {
371 g_return_val_if_fail (refcount == NULL, FALSE);
375 if (refcount == NULL) {
376 g_return_val_if_fail (refcount != NULL, FALSE);
381 features->parent_refcount = refcount;
387 * gst_caps_features_copy:
388 * @features: a #GstCapsFeatures to duplicate
390 * Duplicates a #GstCapsFeatures and all its values.
392 * Returns: (transfer full): a new #GstCapsFeatures.
397 gst_caps_features_copy (const GstCapsFeatures * features)
399 GstCapsFeatures *copy;
402 g_return_val_if_fail (features != NULL, NULL);
404 copy = gst_caps_features_new_empty ();
405 n = gst_caps_features_get_size (features);
406 for (i = 0; i < n; i++)
407 gst_caps_features_add_id (copy, gst_caps_features_get_nth_id (features, i));
408 copy->is_any = features->is_any;
414 * gst_caps_features_free:
415 * @features: (in) (transfer full): the #GstCapsFeatures to free
417 * Frees a #GstCapsFeatures and all its values. The caps features must not
418 * have a parent when this function is called.
423 gst_caps_features_free (GstCapsFeatures * features)
425 g_return_if_fail (features != NULL);
426 g_return_if_fail (features->parent_refcount == NULL);
428 g_array_free (features->array, TRUE);
430 memset (features, 0xff, sizeof (GstCapsFeatures));
432 GST_TRACE ("free caps features %p", features);
434 g_slice_free (GstCapsFeatures, features);
438 * gst_caps_features_to_string:
439 * @features: a #GstCapsFeatures
441 * Converts @features to a human-readable string representation.
443 * For debugging purposes its easier to do something like this:
446 * GST_LOG ("features is %" GST_PTR_FORMAT, features);
449 * This prints the features in human readable form.
451 * Returns: (transfer full): a pointer to string allocated by g_malloc().
456 gst_caps_features_to_string (const GstCapsFeatures * features)
460 g_return_val_if_fail (features != NULL, NULL);
462 s = g_string_sized_new (FEATURES_ESTIMATED_STRING_LEN (features));
464 priv_gst_caps_features_append_to_gstring (features, s);
466 return g_string_free (s, FALSE);
470 priv_gst_caps_features_append_to_gstring (const GstCapsFeatures * features,
475 g_return_if_fail (features != NULL);
477 if (features->array->len == 0 && features->is_any) {
478 g_string_append (s, "ANY");
482 n = features->array->len;
483 for (i = 0; i < n; i++) {
484 GQuark *quark = &g_array_index (features->array, GQuark, i);
486 g_string_append (s, g_quark_to_string (*quark));
488 g_string_append (s, ", ");
493 * gst_caps_features_from_string:
494 * @features: a string representation of a #GstCapsFeatures.
496 * Creates a #GstCapsFeatures from a string representation.
498 * Returns: (transfer full) (nullable): a new #GstCapsFeatures or
499 * %NULL when the string could not be parsed.
504 gst_caps_features_from_string (const gchar * features)
506 GstCapsFeatures *ret;
507 gboolean escape = FALSE;
508 const gchar *features_orig = features;
509 const gchar *feature;
511 ret = gst_caps_features_new_empty ();
513 if (!features || *features == '\0')
516 if (strcmp (features, "ANY") == 0) {
521 /* Skip trailing spaces */
522 while (*features == ' ')
533 } else if ((!escape && c == ',') || c == '\0') {
534 guint len = features - feature + 1;
539 g_warning ("Failed deserialize caps features '%s'", features_orig);
540 gst_caps_features_free (ret);
544 tmp = g_malloc (len);
545 memcpy (tmp, feature, len - 1);
554 if (strstr (tmp, " ") != NULL || *tmp == '\0') {
556 g_warning ("Failed deserialize caps features '%s'", features_orig);
557 gst_caps_features_free (ret);
561 gst_caps_features_add (ret, tmp);
567 /* Skip to the next value */
569 while (*features == ' ')
582 * gst_caps_features_get_size:
583 * @features: a #GstCapsFeatures.
585 * Returns the number of features in @features.
587 * Returns: The number of features in @features.
592 gst_caps_features_get_size (const GstCapsFeatures * features)
594 g_return_val_if_fail (features != NULL, 0);
596 return features->array->len;
600 * gst_caps_features_get_nth:
601 * @features: a #GstCapsFeatures.
602 * @i: index of the feature
604 * Returns the @i-th feature of @features.
606 * Returns: (nullable): The @i-th feature of @features.
611 gst_caps_features_get_nth (const GstCapsFeatures * features, guint i)
613 const gchar *feature;
616 g_return_val_if_fail (features != NULL, NULL);
618 quark = gst_caps_features_get_nth_id (features, i);
622 feature = g_quark_to_string (quark);
627 * gst_caps_features_get_nth_id:
628 * @features: a #GstCapsFeatures.
629 * @i: index of the feature
631 * Returns the @i-th feature of @features.
633 * Returns: The @i-th feature of @features.
638 gst_caps_features_get_nth_id (const GstCapsFeatures * features, guint i)
642 g_return_val_if_fail (features != NULL, 0);
643 g_return_val_if_fail (i < features->array->len, 0);
645 quark = &g_array_index (features->array, GQuark, i);
651 * gst_caps_features_contains:
652 * @features: a #GstCapsFeatures.
653 * @feature: a feature
655 * Checks if @features contains @feature.
657 * Returns: %TRUE if @features contains @feature.
662 gst_caps_features_contains (const GstCapsFeatures * features,
663 const gchar * feature)
665 g_return_val_if_fail (features != NULL, FALSE);
666 g_return_val_if_fail (feature != NULL, FALSE);
668 return gst_caps_features_contains_id (features,
669 g_quark_from_string (feature));
673 * gst_caps_features_contains_id:
674 * @features: a #GstCapsFeatures.
675 * @feature: a feature
677 * Checks if @features contains @feature.
679 * Returns: %TRUE if @features contains @feature.
684 gst_caps_features_contains_id (const GstCapsFeatures * features, GQuark feature)
688 g_return_val_if_fail (features != NULL, FALSE);
689 g_return_val_if_fail (feature != 0, FALSE);
691 if (features->is_any)
694 n = features->array->len;
696 return feature == _gst_caps_feature_memory_system_memory;
698 for (i = 0; i < n; i++) {
699 if (gst_caps_features_get_nth_id (features, i) == feature)
707 * gst_caps_features_is_equal:
708 * @features1: a #GstCapsFeatures.
709 * @features2: a #GstCapsFeatures.
711 * Checks if @features1 and @features2 are equal.
713 * Returns: %TRUE if @features1 and @features2 are equal.
718 gst_caps_features_is_equal (const GstCapsFeatures * features1,
719 const GstCapsFeatures * features2)
723 g_return_val_if_fail (features1 != NULL, FALSE);
724 g_return_val_if_fail (features2 != NULL, FALSE);
726 if (features1->is_any || features2->is_any)
729 /* Check for the sysmem==empty case */
730 if (features1->array->len == 0 && features2->array->len == 0)
732 if (features1->array->len == 0 && features2->array->len == 1
733 && gst_caps_features_contains_id (features2,
734 _gst_caps_feature_memory_system_memory))
736 if (features2->array->len == 0 && features1->array->len == 1
737 && gst_caps_features_contains_id (features1,
738 _gst_caps_feature_memory_system_memory))
741 if (features1->array->len != features2->array->len)
744 n = features1->array->len;
745 for (i = 0; i < n; i++)
746 if (!gst_caps_features_contains_id (features2,
747 gst_caps_features_get_nth_id (features1, i)))
754 * gst_caps_features_is_any:
755 * @features: a #GstCapsFeatures.
757 * Checks if @features is %GST_CAPS_FEATURES_ANY.
759 * Returns: %TRUE if @features is %GST_CAPS_FEATURES_ANY.
764 gst_caps_features_is_any (const GstCapsFeatures * features)
766 g_return_val_if_fail (features != NULL, FALSE);
768 return features->is_any;
772 * gst_caps_features_add:
773 * @features: a #GstCapsFeatures.
774 * @feature: a feature.
776 * Adds @feature to @features.
781 gst_caps_features_add (GstCapsFeatures * features, const gchar * feature)
783 g_return_if_fail (features != NULL);
784 g_return_if_fail (IS_MUTABLE (features));
785 g_return_if_fail (feature != NULL);
786 g_return_if_fail (!features->is_any);
788 gst_caps_features_add_id (features, g_quark_from_string (feature));
792 * gst_caps_features_add_id:
793 * @features: a #GstCapsFeatures.
794 * @feature: a feature.
796 * Adds @feature to @features.
801 gst_caps_features_add_id (GstCapsFeatures * features, GQuark feature)
803 g_return_if_fail (features != NULL);
804 g_return_if_fail (IS_MUTABLE (features));
805 g_return_if_fail (feature != 0);
806 g_return_if_fail (!features->is_any);
808 if (!gst_caps_feature_name_is_valid (g_quark_to_string (feature))) {
809 g_warning ("Invalid caps feature name: %s", g_quark_to_string (feature));
813 /* If features is empty it will contain sysmem, however
814 * we want to add it explicitly if it is attempted to be
815 * added as first features
817 if (features->array->len > 0
818 && gst_caps_features_contains_id (features, feature))
821 g_array_append_val (features->array, feature);
825 * gst_caps_features_remove:
826 * @features: a #GstCapsFeatures.
827 * @feature: a feature.
829 * Removes @feature from @features.
834 gst_caps_features_remove (GstCapsFeatures * features, const gchar * feature)
836 g_return_if_fail (features != NULL);
837 g_return_if_fail (IS_MUTABLE (features));
838 g_return_if_fail (feature != NULL);
840 gst_caps_features_remove_id (features, g_quark_from_string (feature));
844 * gst_caps_features_remove_id:
845 * @features: a #GstCapsFeatures.
846 * @feature: a feature.
848 * Removes @feature from @features.
853 gst_caps_features_remove_id (GstCapsFeatures * features, GQuark feature)
857 g_return_if_fail (features != NULL);
858 g_return_if_fail (IS_MUTABLE (features));
859 g_return_if_fail (feature != 0);
861 n = features->array->len;
862 for (i = 0; i < n; i++) {
863 GQuark quark = gst_caps_features_get_nth_id (features, i);
865 if (quark == feature) {
866 g_array_remove_index_fast (features->array, i);
873 gst_caps_features_transform_to_string (const GValue * src_value,
876 g_return_if_fail (src_value != NULL);
877 g_return_if_fail (dest_value != NULL);
879 dest_value->data[0].v_pointer =
880 gst_caps_features_to_string (src_value->data[0].v_pointer);