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 (gconstpointer obj)
124 const GstCapsFeatures *features = obj;
126 return (obj != NULL && features->type == _gst_caps_features_type);
130 gst_caps_feature_name_is_valid (const gchar * feature)
132 #ifndef G_DISABLE_CHECKS
134 if (g_ascii_isalpha (*feature))
136 else if (*feature == ':')
146 if (*feature == '\0' || !g_ascii_isalpha (*feature))
150 if (g_ascii_isalnum (*feature))
152 else if (*feature == '\0')
163 * gst_caps_features_new_empty:
165 * Creates a new, empty #GstCapsFeatures.
167 * Free-function: gst_caps_features_free
169 * Returns: (transfer full): a new, empty #GstCapsFeatures
174 gst_caps_features_new_empty (void)
176 GstCapsFeatures *features;
178 features = g_slice_new (GstCapsFeatures);
179 features->type = _gst_caps_features_type;
180 features->parent_refcount = NULL;
181 features->array = g_array_new (FALSE, FALSE, sizeof (GQuark));
182 features->is_any = FALSE;
184 GST_TRACE ("created caps features %p", features);
190 * gst_caps_features_new_any:
192 * Creates a new, ANY #GstCapsFeatures. This will be equal
193 * to any other #GstCapsFeatures but caps with these are
196 * Free-function: gst_caps_features_free
198 * Returns: (transfer full): a new, ANY #GstCapsFeatures
203 gst_caps_features_new_any (void)
205 GstCapsFeatures *features;
207 features = gst_caps_features_new_empty ();
208 features->is_any = TRUE;
214 * gst_caps_features_new:
215 * @feature1: name of first feature to set
216 * @...: additional features
218 * Creates a new #GstCapsFeatures with the given features.
219 * The last argument must be %NULL.
221 * Free-function: gst_caps_features_free
223 * Returns: (transfer full): a new, empty #GstCapsFeatures
228 gst_caps_features_new (const gchar * feature1, ...)
230 GstCapsFeatures *features;
233 g_return_val_if_fail (feature1 != NULL, NULL);
235 va_start (varargs, feature1);
236 features = gst_caps_features_new_valist (feature1, varargs);
243 * gst_caps_features_new_valist:
244 * @feature1: name of first feature to set
245 * @varargs: variable argument list
247 * Creates a new #GstCapsFeatures with the given features.
249 * Free-function: gst_caps_features_free
251 * Returns: (transfer full): a new, empty #GstCapsFeatures
256 gst_caps_features_new_valist (const gchar * feature1, va_list varargs)
258 GstCapsFeatures *features;
260 g_return_val_if_fail (feature1 != NULL, NULL);
262 features = gst_caps_features_new_empty ();
265 gst_caps_features_add (features, feature1);
266 feature1 = va_arg (varargs, const gchar *);
273 * gst_caps_features_new_id:
274 * @feature1: name of first feature to set
275 * @...: additional features
277 * Creates a new #GstCapsFeatures with the given features.
278 * The last argument must be 0.
280 * Free-function: gst_caps_features_free
282 * Returns: (transfer full): a new, empty #GstCapsFeatures
287 gst_caps_features_new_id (GQuark feature1, ...)
289 GstCapsFeatures *features;
292 g_return_val_if_fail (feature1 != 0, NULL);
294 va_start (varargs, feature1);
295 features = gst_caps_features_new_id_valist (feature1, varargs);
302 * gst_caps_features_new_id_valist:
303 * @feature1: name of first feature to set
304 * @varargs: variable argument list
306 * Creates a new #GstCapsFeatures with the given features.
308 * Free-function: gst_caps_features_free
310 * Returns: (transfer full): a new, empty #GstCapsFeatures
315 gst_caps_features_new_id_valist (GQuark feature1, va_list varargs)
317 GstCapsFeatures *features;
319 g_return_val_if_fail (feature1 != 0, NULL);
321 features = gst_caps_features_new_empty ();
324 gst_caps_features_add_id (features, feature1);
325 feature1 = va_arg (varargs, GQuark);
332 * gst_caps_features_set_parent_refcount:
333 * @features: a #GstCapsFeatures
334 * @refcount: (in): a pointer to the parent's refcount
336 * Sets the parent_refcount field of #GstCapsFeatures. This field is used to
337 * determine whether a caps features is mutable or not. This function should only be
338 * called by code implementing parent objects of #GstCapsFeatures, as described in
339 * the MT Refcounting section of the design documents.
341 * Returns: %TRUE if the parent refcount could be set.
346 gst_caps_features_set_parent_refcount (GstCapsFeatures * features,
349 g_return_val_if_fail (features != NULL, FALSE);
351 /* if we have a parent_refcount already, we can only clear
352 * if with a NULL refcount */
353 if (features->parent_refcount) {
354 if (refcount != NULL) {
355 g_return_val_if_fail (refcount == NULL, FALSE);
359 if (refcount == NULL) {
360 g_return_val_if_fail (refcount != NULL, FALSE);
365 features->parent_refcount = refcount;
371 * gst_caps_features_copy:
372 * @features: a #GstCapsFeatures to duplicate
374 * Duplicates a #GstCapsFeatures and all its values.
376 * Free-function: gst_caps_features_free
378 * Returns: (transfer full): a new #GstCapsFeatures.
383 gst_caps_features_copy (const GstCapsFeatures * features)
385 GstCapsFeatures *copy;
388 g_return_val_if_fail (features != NULL, NULL);
390 copy = gst_caps_features_new_empty ();
391 n = gst_caps_features_get_size (features);
392 for (i = 0; i < n; i++)
393 gst_caps_features_add_id (copy, gst_caps_features_get_nth_id (features, i));
394 copy->is_any = features->is_any;
400 * gst_caps_features_free:
401 * @features: (in) (transfer full): the #GstCapsFeatures to free
403 * Frees a #GstCapsFeatures and all its values. The caps features must not
404 * have a parent when this function is called.
409 gst_caps_features_free (GstCapsFeatures * features)
411 g_return_if_fail (features != NULL);
412 g_return_if_fail (features->parent_refcount == NULL);
414 g_array_free (features->array, TRUE);
416 memset (features, 0xff, sizeof (GstCapsFeatures));
418 GST_TRACE ("free caps features %p", features);
420 g_slice_free (GstCapsFeatures, features);
424 * gst_caps_features_to_string:
425 * @features: a #GstCapsFeatures
427 * Converts @features to a human-readable string representation.
429 * For debugging purposes its easier to do something like this:
430 * |[<!-- language="C" -->
431 * GST_LOG ("features is %" GST_PTR_FORMAT, features);
433 * This prints the features in human readable form.
435 * Free-function: g_free
437 * Returns: (transfer full): a pointer to string allocated by g_malloc().
438 * g_free() after usage.
443 gst_caps_features_to_string (const GstCapsFeatures * features)
447 g_return_val_if_fail (features != NULL, NULL);
449 s = g_string_sized_new (FEATURES_ESTIMATED_STRING_LEN (features));
451 priv_gst_caps_features_append_to_gstring (features, s);
453 return g_string_free (s, FALSE);
457 priv_gst_caps_features_append_to_gstring (const GstCapsFeatures * features,
462 g_return_if_fail (features != NULL);
464 if (features->array->len == 0 && features->is_any) {
465 g_string_append (s, "ANY");
469 n = features->array->len;
470 for (i = 0; i < n; i++) {
471 GQuark *quark = &g_array_index (features->array, GQuark, i);
473 g_string_append (s, g_quark_to_string (*quark));
475 g_string_append (s, ", ");
480 * gst_caps_features_from_string:
481 * @features: a string representation of a #GstCapsFeatures.
483 * Creates a #GstCapsFeatures from a string representation.
485 * Free-function: gst_caps_features_free
487 * Returns: (transfer full) (nullable): a new #GstCapsFeatures or
488 * %NULL when the string could not be parsed. Free with
489 * gst_caps_features_free() after use.
494 gst_caps_features_from_string (const gchar * features)
496 GstCapsFeatures *ret;
497 gboolean escape = FALSE;
498 const gchar *features_orig = features;
499 const gchar *feature;
501 ret = gst_caps_features_new_empty ();
503 if (!features || *features == '\0')
506 if (strcmp (features, "ANY") == 0) {
511 /* Skip trailing spaces */
512 while (*features == ' ')
523 } else if ((!escape && c == ',') || c == '\0') {
524 guint len = features - feature + 1;
529 g_warning ("Failed deserialize caps features '%s'", features_orig);
530 gst_caps_features_free (ret);
534 tmp = g_malloc (len);
535 memcpy (tmp, feature, len - 1);
544 if (strstr (tmp, " ") != NULL || *tmp == '\0') {
546 g_warning ("Failed deserialize caps features '%s'", features_orig);
547 gst_caps_features_free (ret);
551 gst_caps_features_add (ret, tmp);
557 /* Skip to the next value */
559 while (*features == ' ')
572 * gst_caps_features_get_size:
573 * @features: a #GstCapsFeatures.
575 * Returns the number of features in @features.
577 * Returns: The number of features in @features.
582 gst_caps_features_get_size (const GstCapsFeatures * features)
584 g_return_val_if_fail (features != NULL, 0);
586 return features->array->len;
590 * gst_caps_features_get_nth:
591 * @features: a #GstCapsFeatures.
592 * @i: index of the feature
594 * Returns the @i-th feature of @features.
596 * Returns: (nullable): The @i-th feature of @features.
601 gst_caps_features_get_nth (const GstCapsFeatures * features, guint i)
603 const gchar *feature;
606 g_return_val_if_fail (features != NULL, NULL);
608 quark = gst_caps_features_get_nth_id (features, i);
612 feature = g_quark_to_string (quark);
617 * gst_caps_features_get_nth_id:
618 * @features: a #GstCapsFeatures.
619 * @i: index of the feature
621 * Returns the @i-th feature of @features.
623 * Returns: The @i-th feature of @features.
628 gst_caps_features_get_nth_id (const GstCapsFeatures * features, guint i)
632 g_return_val_if_fail (features != NULL, 0);
633 g_return_val_if_fail (i < features->array->len, 0);
635 quark = &g_array_index (features->array, GQuark, i);
641 * gst_caps_features_contains:
642 * @features: a #GstCapsFeatures.
643 * @feature: a feature
645 * Check if @features contains @feature.
647 * Returns: %TRUE if @features contains @feature.
652 gst_caps_features_contains (const GstCapsFeatures * features,
653 const gchar * feature)
655 g_return_val_if_fail (features != NULL, FALSE);
656 g_return_val_if_fail (feature != NULL, FALSE);
658 return gst_caps_features_contains_id (features,
659 g_quark_from_string (feature));
663 * gst_caps_features_contains_id:
664 * @features: a #GstCapsFeatures.
665 * @feature: a feature
667 * Check if @features contains @feature.
669 * Returns: %TRUE if @features contains @feature.
674 gst_caps_features_contains_id (const GstCapsFeatures * features, GQuark feature)
678 g_return_val_if_fail (features != NULL, FALSE);
679 g_return_val_if_fail (feature != 0, FALSE);
681 if (features->is_any)
684 n = features->array->len;
686 return feature == _gst_caps_feature_memory_system_memory;
688 for (i = 0; i < n; i++) {
689 if (gst_caps_features_get_nth_id (features, i) == feature)
697 * gst_caps_features_is_equal:
698 * @features1: a #GstCapsFeatures.
699 * @features2: a #GstCapsFeatures.
701 * Check if @features1 and @features2 are equal.
703 * Returns: %TRUE if @features1 and @features2 are equal.
708 gst_caps_features_is_equal (const GstCapsFeatures * features1,
709 const GstCapsFeatures * features2)
713 g_return_val_if_fail (features1 != NULL, FALSE);
714 g_return_val_if_fail (features2 != NULL, FALSE);
716 if (features1->is_any || features2->is_any)
719 /* Check for the sysmem==empty case */
720 if (features1->array->len == 0 && features2->array->len == 0)
722 if (features1->array->len == 0 && features2->array->len == 1
723 && gst_caps_features_contains_id (features2,
724 _gst_caps_feature_memory_system_memory))
726 if (features2->array->len == 0 && features1->array->len == 1
727 && gst_caps_features_contains_id (features1,
728 _gst_caps_feature_memory_system_memory))
731 if (features1->array->len != features2->array->len)
734 n = features1->array->len;
735 for (i = 0; i < n; i++)
736 if (!gst_caps_features_contains_id (features2,
737 gst_caps_features_get_nth_id (features1, i)))
744 * gst_caps_features_is_any:
745 * @features: a #GstCapsFeatures.
747 * Check if @features is %GST_CAPS_FEATURES_ANY.
749 * Returns: %TRUE if @features is %GST_CAPS_FEATURES_ANY.
754 gst_caps_features_is_any (const GstCapsFeatures * features)
756 g_return_val_if_fail (features != NULL, FALSE);
758 return features->is_any;
762 * gst_caps_features_add:
763 * @features: a #GstCapsFeatures.
764 * @feature: a feature.
766 * Adds @feature to @features.
771 gst_caps_features_add (GstCapsFeatures * features, const gchar * feature)
773 g_return_if_fail (features != NULL);
774 g_return_if_fail (IS_MUTABLE (features));
775 g_return_if_fail (feature != NULL);
776 g_return_if_fail (!features->is_any);
778 gst_caps_features_add_id (features, g_quark_from_string (feature));
782 * gst_caps_features_add_id:
783 * @features: a #GstCapsFeatures.
784 * @feature: a feature.
786 * Adds @feature to @features.
791 gst_caps_features_add_id (GstCapsFeatures * features, GQuark feature)
793 g_return_if_fail (features != NULL);
794 g_return_if_fail (IS_MUTABLE (features));
795 g_return_if_fail (feature != 0);
796 g_return_if_fail (!features->is_any);
798 if (!gst_caps_feature_name_is_valid (g_quark_to_string (feature))) {
799 g_warning ("Invalid caps feature name: %s", g_quark_to_string (feature));
803 /* If features is empty it will contain sysmem, however
804 * we want to add it explicitly if it is attempted to be
805 * added as first features
807 if (features->array->len > 0
808 && gst_caps_features_contains_id (features, feature))
811 g_array_append_val (features->array, feature);
815 * gst_caps_features_remove:
816 * @features: a #GstCapsFeatures.
817 * @feature: a feature.
819 * Removes @feature from @features.
824 gst_caps_features_remove (GstCapsFeatures * features, const gchar * feature)
826 g_return_if_fail (features != NULL);
827 g_return_if_fail (IS_MUTABLE (features));
828 g_return_if_fail (feature != NULL);
830 gst_caps_features_remove_id (features, g_quark_from_string (feature));
834 * gst_caps_features_remove_id:
835 * @features: a #GstCapsFeatures.
836 * @feature: a feature.
838 * Removes @feature from @features.
843 gst_caps_features_remove_id (GstCapsFeatures * features, GQuark feature)
847 g_return_if_fail (features != NULL);
848 g_return_if_fail (IS_MUTABLE (features));
849 g_return_if_fail (feature != 0);
851 n = features->array->len;
852 for (i = 0; i < n; i++) {
853 GQuark quark = gst_caps_features_get_nth_id (features, i);
855 if (quark == feature) {
856 g_array_remove_index_fast (features->array, i);
863 gst_caps_features_transform_to_string (const GValue * src_value,
866 g_return_if_fail (src_value != NULL);
867 g_return_if_fail (dest_value != NULL);
869 dest_value->data[0].v_pointer =
870 gst_caps_features_to_string (src_value->data[0].v_pointer);