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.
35 * Examples for caps features would be the requirement of a specific #GstMemory
36 * types or the requirement of having a specific #GstMeta on the buffer. Features
37 * are given as a string of the format "memory:GstMemoryTypeName" or
38 * "meta:GstMetaAPIName".
48 #include "gst_private.h"
49 #include "gstcapsfeatures.h"
52 GST_DEBUG_CATEGORY_STATIC (gst_caps_features_debug);
53 #define GST_CAT_DEFAULT gst_caps_features_debug
55 struct _GstCapsFeatures
58 gint *parent_refcount;
63 GType _gst_caps_features_type = 0;
64 GstCapsFeatures *_gst_caps_features_any = NULL;
65 GstCapsFeatures *_gst_caps_features_memory_system_memory = NULL;
66 static GQuark _gst_caps_feature_memory_system_memory = 0;
68 G_DEFINE_BOXED_TYPE (GstCapsFeatures, gst_caps_features,
69 gst_caps_features_copy, gst_caps_features_free);
71 #define IS_MUTABLE(features) \
72 (!features->parent_refcount || \
73 g_atomic_int_get (features->parent_refcount) == 1)
76 gst_caps_features_transform_to_string (const GValue * src_value,
80 _priv_gst_caps_features_initialize (void)
82 GST_DEBUG_CATEGORY_INIT (gst_caps_features_debug, "caps-features", 0,
83 "GstCapsFeatures debug");
85 _gst_caps_features_type = gst_caps_features_get_type ();
86 _gst_caps_feature_memory_system_memory =
87 g_quark_from_static_string (GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY);
89 g_value_register_transform_func (_gst_caps_features_type, G_TYPE_STRING,
90 gst_caps_features_transform_to_string);
92 _gst_caps_features_any = gst_caps_features_new_any ();
93 _gst_caps_features_memory_system_memory =
94 gst_caps_features_new_id (_gst_caps_feature_memory_system_memory, 0);
98 gst_is_caps_features (gconstpointer obj)
100 const GstCapsFeatures *features = obj;
102 return (obj != NULL && features->type == _gst_caps_features_type);
106 gst_caps_feature_name_is_valid (const gchar * feature)
108 #ifndef G_DISABLE_CHECKS
110 if (g_ascii_isalpha (*feature))
112 else if (*feature == ':')
122 if (*feature == '\0' || !g_ascii_isalpha (*feature))
126 if (g_ascii_isalnum (*feature))
128 else if (*feature == '\0')
139 * gst_caps_features_new_empty:
141 * Creates a new, empty #GstCapsFeatures.
143 * Free-function: gst_caps_features_free
145 * Returns: (transfer full): a new, empty #GstCapsFeatures
150 gst_caps_features_new_empty (void)
152 GstCapsFeatures *features;
154 features = g_slice_new (GstCapsFeatures);
155 features->type = _gst_caps_features_type;
156 features->parent_refcount = NULL;
157 features->array = g_array_new (FALSE, FALSE, sizeof (GQuark));
158 features->is_any = FALSE;
160 GST_TRACE ("created caps features %p", features);
166 * gst_caps_features_new_any:
168 * Creates a new, ANY #GstCapsFeatures. This will be equal
169 * to any other #GstCapsFeatures but caps with these are
172 * Free-function: gst_caps_features_free
174 * Returns: (transfer full): a new, ANY #GstCapsFeatures
179 gst_caps_features_new_any (void)
181 GstCapsFeatures *features;
183 features = gst_caps_features_new_empty ();
184 features->is_any = TRUE;
190 * gst_caps_features_new:
191 * @feature1: name of first feature to set
192 * @...: additional features
194 * Creates a new #GstCapsFeatures with the given features.
195 * The last argument must be NULL.
197 * Free-function: gst_caps_features_free
199 * Returns: (transfer full): a new, empty #GstCapsFeatures
204 gst_caps_features_new (const gchar * feature1, ...)
206 GstCapsFeatures *features;
209 g_return_val_if_fail (feature1 != NULL, NULL);
211 va_start (varargs, feature1);
212 features = gst_caps_features_new_valist (feature1, varargs);
219 * gst_caps_features_new_valist:
220 * @feature1: name of first feature to set
221 * @varargs: variable argument list
223 * Creates a new #GstCapsFeatures with the given features.
225 * Free-function: gst_caps_features_free
227 * Returns: (transfer full): a new, empty #GstCapsFeatures
232 gst_caps_features_new_valist (const gchar * feature1, va_list varargs)
234 GstCapsFeatures *features;
236 g_return_val_if_fail (feature1 != NULL, NULL);
238 features = gst_caps_features_new_empty ();
241 gst_caps_features_add (features, feature1);
242 feature1 = va_arg (varargs, const gchar *);
249 * gst_caps_features_new_id:
250 * @feature1: name of first feature to set
251 * @...: additional features
253 * Creates a new #GstCapsFeatures with the given features.
254 * The last argument must be 0.
256 * Free-function: gst_caps_features_free
258 * Returns: (transfer full): a new, empty #GstCapsFeatures
263 gst_caps_features_new_id (GQuark feature1, ...)
265 GstCapsFeatures *features;
268 g_return_val_if_fail (feature1 != 0, NULL);
270 va_start (varargs, feature1);
271 features = gst_caps_features_new_id_valist (feature1, varargs);
278 * gst_caps_features_new_id_valist:
279 * @feature1: name of first feature to set
280 * @varargs: variable argument list
282 * Creates a new #GstCapsFeatures with the given features.
284 * Free-function: gst_caps_features_free
286 * Returns: (transfer full): a new, empty #GstCapsFeatures
291 gst_caps_features_new_id_valist (GQuark feature1, va_list varargs)
293 GstCapsFeatures *features;
295 g_return_val_if_fail (feature1 != 0, NULL);
297 features = gst_caps_features_new_empty ();
300 gst_caps_features_add_id (features, feature1);
301 feature1 = va_arg (varargs, GQuark);
308 * gst_caps_features_set_parent_refcount:
309 * @features: a #GstCapsFeatures
310 * @refcount: (in): a pointer to the parent's refcount
312 * Sets the parent_refcount field of #GstCapsFeatures. This field is used to
313 * determine whether a caps features is mutable or not. This function should only be
314 * called by code implementing parent objects of #GstCapsFeatures, as described in
315 * the MT Refcounting section of the design documents.
317 * Returns: %TRUE if the parent refcount could be set.
322 gst_caps_features_set_parent_refcount (GstCapsFeatures * features,
325 g_return_val_if_fail (features != NULL, FALSE);
327 /* if we have a parent_refcount already, we can only clear
328 * if with a NULL refcount */
329 if (features->parent_refcount) {
330 if (refcount != NULL) {
331 g_return_val_if_fail (refcount == NULL, FALSE);
335 if (refcount == NULL) {
336 g_return_val_if_fail (refcount != NULL, FALSE);
341 features->parent_refcount = refcount;
347 * gst_caps_features_copy:
348 * @features: a #GstCapsFeatures to duplicate
350 * Duplicates a #GstCapsFeatures and all its values.
352 * Free-function: gst_caps_features_free
354 * Returns: (transfer full): a new #GstCapsFeatures.
359 gst_caps_features_copy (const GstCapsFeatures * features)
361 GstCapsFeatures *copy;
364 g_return_val_if_fail (features != NULL, NULL);
366 copy = gst_caps_features_new_empty ();
367 n = gst_caps_features_get_size (features);
368 for (i = 0; i < n; i++)
369 gst_caps_features_add_id (copy, gst_caps_features_get_nth_id (features, i));
370 copy->is_any = features->is_any;
376 * gst_caps_features_free:
377 * @features: (in) (transfer full): the #GstCapsFeatures to free
379 * Frees a #GstCapsFeatures and all its values. The caps features must not
380 * have a parent when this function is called.
385 gst_caps_features_free (GstCapsFeatures * features)
387 g_return_if_fail (features != NULL);
388 g_return_if_fail (features->parent_refcount == NULL);
390 g_array_free (features->array, TRUE);
392 memset (features, 0xff, sizeof (GstCapsFeatures));
394 GST_TRACE ("free caps features %p", features);
396 g_slice_free (GstCapsFeatures, features);
400 * gst_caps_features_to_string:
401 * @features: a #GstCapsFeatures
403 * Converts @features to a human-readable string representation.
405 * For debugging purposes its easier to do something like this:
407 * GST_LOG ("features is %" GST_PTR_FORMAT, features);
409 * This prints the features in human readble form.
411 * Free-function: g_free
413 * Returns: (transfer full): a pointer to string allocated by g_malloc().
414 * g_free() after usage.
419 gst_caps_features_to_string (const GstCapsFeatures * features)
423 g_return_val_if_fail (features != NULL, NULL);
425 s = g_string_sized_new (FEATURES_ESTIMATED_STRING_LEN (features));
427 priv_gst_caps_features_append_to_gstring (features, s);
429 return g_string_free (s, FALSE);
433 priv_gst_caps_features_append_to_gstring (const GstCapsFeatures * features,
438 g_return_if_fail (features != NULL);
440 if (features->array->len == 0 && features->is_any) {
441 g_string_append (s, "ANY");
445 n = features->array->len;
446 for (i = 0; i < n; i++) {
447 GQuark *quark = &g_array_index (features->array, GQuark, i);
449 g_string_append (s, g_quark_to_string (*quark));
451 g_string_append (s, ", ");
456 * gst_caps_features_from_string:
457 * @features: a string representation of a #GstCapsFeatures.
459 * Creates a #GstCapsFeatures from a string representation.
461 * Free-function: gst_caps_features_free
463 * Returns: (transfer full): a new #GstCapsFeatures or NULL when the string could
464 * not be parsed. Free with gst_caps_features_free() after use.
469 gst_caps_features_from_string (const gchar * features)
471 GstCapsFeatures *ret;
472 gboolean escape = FALSE;
473 const gchar *features_orig = features;
474 const gchar *feature;
476 ret = gst_caps_features_new_empty ();
478 if (!features || *features == '\0')
481 if (strcmp (features, "ANY") == 0) {
486 /* Skip trailing spaces */
487 while (*features == ' ')
498 } else if ((!escape && c == ',') || c == '\0') {
499 guint len = features - feature + 1;
504 g_warning ("Failed deserialize caps features '%s'", features_orig);
505 gst_caps_features_free (ret);
509 tmp = g_malloc (len);
510 memcpy (tmp, feature, len - 1);
519 if (strstr (tmp, " ") != NULL || *tmp == '\0') {
521 g_warning ("Failed deserialize caps features '%s'", features_orig);
522 gst_caps_features_free (ret);
526 gst_caps_features_add (ret, tmp);
532 /* Skip to the next value */
534 while (*features == ' ')
547 * gst_caps_features_get_size:
548 * @features: a #GstCapsFeatures.
550 * Returns the number of features in @features.
552 * Returns: The number of features in @features.
557 gst_caps_features_get_size (const GstCapsFeatures * features)
559 g_return_val_if_fail (features != NULL, 0);
561 return features->array->len;
565 * gst_caps_features_get_nth:
566 * @features: a #GstCapsFeatures.
567 * @i: index of the feature
569 * Returns the @i-th feature of @features.
571 * Returns: The @i-th feature of @features.
576 gst_caps_features_get_nth (const GstCapsFeatures * features, guint i)
578 const gchar *feature;
581 g_return_val_if_fail (features != NULL, NULL);
583 quark = gst_caps_features_get_nth_id (features, i);
587 feature = g_quark_to_string (quark);
592 * gst_caps_features_get_nth_id:
593 * @features: a #GstCapsFeatures.
594 * @i: index of the feature
596 * Returns the @i-th feature of @features.
598 * Returns: The @i-th feature of @features.
603 gst_caps_features_get_nth_id (const GstCapsFeatures * features, guint i)
607 g_return_val_if_fail (features != NULL, 0);
608 g_return_val_if_fail (i < features->array->len, 0);
610 quark = &g_array_index (features->array, GQuark, i);
616 * gst_caps_features_contains:
617 * @features: a #GstCapsFeatures.
618 * @feature: a feature
620 * Check if @features contains @feature.
622 * Returns: %TRUE if @features contains @feature.
627 gst_caps_features_contains (const GstCapsFeatures * features,
628 const gchar * feature)
630 g_return_val_if_fail (features != NULL, FALSE);
631 g_return_val_if_fail (feature != NULL, FALSE);
633 return gst_caps_features_contains_id (features,
634 g_quark_from_string (feature));
638 * gst_caps_features_contains_id:
639 * @features: a #GstCapsFeatures.
640 * @feature: a feature
642 * Check if @features contains @feature.
644 * Returns: %TRUE if @features contains @feature.
649 gst_caps_features_contains_id (const GstCapsFeatures * features, GQuark feature)
653 g_return_val_if_fail (features != NULL, FALSE);
654 g_return_val_if_fail (feature != 0, FALSE);
656 if (features->is_any)
659 n = features->array->len;
661 return feature == _gst_caps_feature_memory_system_memory;
663 for (i = 0; i < n; i++) {
664 if (gst_caps_features_get_nth_id (features, i) == feature)
672 * gst_caps_features_is_equal:
673 * @features1: a #GstCapsFeatures.
674 * @features2: a #GstCapsFeatures.
676 * Check if @features1 and @features2 are equal.
678 * Returns: %TRUE if @features1 and @features2 are equal.
683 gst_caps_features_is_equal (const GstCapsFeatures * features1,
684 const GstCapsFeatures * features2)
688 g_return_val_if_fail (features1 != NULL, FALSE);
689 g_return_val_if_fail (features2 != NULL, FALSE);
691 if (features1->is_any || features2->is_any)
694 /* Check for the sysmem==empty case */
695 if (features1->array->len == 0 && features2->array->len == 0)
697 if (features1->array->len == 0 && features2->array->len == 1
698 && gst_caps_features_contains_id (features2,
699 _gst_caps_feature_memory_system_memory))
701 if (features2->array->len == 0 && features1->array->len == 1
702 && gst_caps_features_contains_id (features1,
703 _gst_caps_feature_memory_system_memory))
706 if (features1->array->len != features2->array->len)
709 n = features1->array->len;
710 for (i = 0; i < n; i++)
711 if (!gst_caps_features_contains_id (features2,
712 gst_caps_features_get_nth_id (features1, i)))
719 * gst_caps_features_is_any:
720 * @features: a #GstCapsFeatures.
722 * Check if @features is %GST_CAPS_FEATURES_ANY.
724 * Returns: %TRUE if @features is %GST_CAPS_FEATURES_ANY.
729 gst_caps_features_is_any (const GstCapsFeatures * features)
731 g_return_val_if_fail (features != NULL, FALSE);
733 return features->is_any;
737 * gst_caps_features_add:
738 * @features: a #GstCapsFeatures.
739 * @feature: a feature.
741 * Adds @feature to @features.
746 gst_caps_features_add (GstCapsFeatures * features, const gchar * feature)
748 g_return_if_fail (features != NULL);
749 g_return_if_fail (IS_MUTABLE (features));
750 g_return_if_fail (feature != NULL);
751 g_return_if_fail (!features->is_any);
753 gst_caps_features_add_id (features, g_quark_from_string (feature));
757 * gst_caps_features_add_id:
758 * @features: a #GstCapsFeatures.
759 * @feature: a feature.
761 * Adds @feature to @features.
766 gst_caps_features_add_id (GstCapsFeatures * features, GQuark feature)
768 g_return_if_fail (features != NULL);
769 g_return_if_fail (IS_MUTABLE (features));
770 g_return_if_fail (feature != 0);
771 g_return_if_fail (!features->is_any);
773 if (!gst_caps_feature_name_is_valid (g_quark_to_string (feature))) {
774 g_warning ("Invalid caps feature name: %s", g_quark_to_string (feature));
778 /* If features is empty it will contain sysmem, however
779 * we want to add it explicitely if it is tried to be
780 * added as first features
782 if (features->array->len > 0
783 && gst_caps_features_contains_id (features, feature))
786 g_array_append_val (features->array, feature);
790 * gst_caps_features_remove:
791 * @features: a #GstCapsFeatures.
792 * @feature: a feature.
794 * Removes @feature from @features.
799 gst_caps_features_remove (GstCapsFeatures * features, const gchar * feature)
801 g_return_if_fail (features != NULL);
802 g_return_if_fail (IS_MUTABLE (features));
803 g_return_if_fail (feature != NULL);
805 gst_caps_features_remove_id (features, g_quark_from_string (feature));
809 * gst_caps_features_remove_id:
810 * @features: a #GstCapsFeatures.
811 * @feature: a feature.
813 * Removes @feature from @features.
818 gst_caps_features_remove_id (GstCapsFeatures * features, GQuark feature)
822 g_return_if_fail (features != NULL);
823 g_return_if_fail (IS_MUTABLE (features));
824 g_return_if_fail (feature != 0);
826 n = features->array->len;
827 for (i = 0; i < n; i++) {
828 GQuark quark = gst_caps_features_get_nth_id (features, i);
830 if (quark == feature) {
831 g_array_remove_index_fast (features->array, i);
838 gst_caps_features_transform_to_string (const GValue * src_value,
841 g_return_if_fail (src_value != NULL);
842 g_return_if_fail (dest_value != NULL);
844 dest_value->data[0].v_pointer =
845 gst_caps_features_to_string (src_value->data[0].v_pointer);