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 gst_is_caps_features (gconstpointer obj)
111 const GstCapsFeatures *features = obj;
113 return (obj != NULL && features->type == _gst_caps_features_type);
117 gst_caps_feature_name_is_valid (const gchar * feature)
119 #ifndef G_DISABLE_CHECKS
121 if (g_ascii_isalpha (*feature))
123 else if (*feature == ':')
133 if (*feature == '\0' || !g_ascii_isalpha (*feature))
137 if (g_ascii_isalnum (*feature))
139 else if (*feature == '\0')
150 * gst_caps_features_new_empty:
152 * Creates a new, empty #GstCapsFeatures.
154 * Free-function: gst_caps_features_free
156 * Returns: (transfer full): a new, empty #GstCapsFeatures
161 gst_caps_features_new_empty (void)
163 GstCapsFeatures *features;
165 features = g_slice_new (GstCapsFeatures);
166 features->type = _gst_caps_features_type;
167 features->parent_refcount = NULL;
168 features->array = g_array_new (FALSE, FALSE, sizeof (GQuark));
169 features->is_any = FALSE;
171 GST_TRACE ("created caps features %p", features);
177 * gst_caps_features_new_any:
179 * Creates a new, ANY #GstCapsFeatures. This will be equal
180 * to any other #GstCapsFeatures but caps with these are
183 * Free-function: gst_caps_features_free
185 * Returns: (transfer full): a new, ANY #GstCapsFeatures
190 gst_caps_features_new_any (void)
192 GstCapsFeatures *features;
194 features = gst_caps_features_new_empty ();
195 features->is_any = TRUE;
201 * gst_caps_features_new:
202 * @feature1: name of first feature to set
203 * @...: additional features
205 * Creates a new #GstCapsFeatures with the given features.
206 * The last argument must be %NULL.
208 * Free-function: gst_caps_features_free
210 * Returns: (transfer full): a new, empty #GstCapsFeatures
215 gst_caps_features_new (const gchar * feature1, ...)
217 GstCapsFeatures *features;
220 g_return_val_if_fail (feature1 != NULL, NULL);
222 va_start (varargs, feature1);
223 features = gst_caps_features_new_valist (feature1, varargs);
230 * gst_caps_features_new_valist:
231 * @feature1: name of first feature to set
232 * @varargs: variable argument list
234 * Creates a new #GstCapsFeatures with the given features.
236 * Free-function: gst_caps_features_free
238 * Returns: (transfer full): a new, empty #GstCapsFeatures
243 gst_caps_features_new_valist (const gchar * feature1, va_list varargs)
245 GstCapsFeatures *features;
247 g_return_val_if_fail (feature1 != NULL, NULL);
249 features = gst_caps_features_new_empty ();
252 gst_caps_features_add (features, feature1);
253 feature1 = va_arg (varargs, const gchar *);
260 * gst_caps_features_new_id:
261 * @feature1: name of first feature to set
262 * @...: additional features
264 * Creates a new #GstCapsFeatures with the given features.
265 * The last argument must be 0.
267 * Free-function: gst_caps_features_free
269 * Returns: (transfer full): a new, empty #GstCapsFeatures
274 gst_caps_features_new_id (GQuark feature1, ...)
276 GstCapsFeatures *features;
279 g_return_val_if_fail (feature1 != 0, NULL);
281 va_start (varargs, feature1);
282 features = gst_caps_features_new_id_valist (feature1, varargs);
289 * gst_caps_features_new_id_valist:
290 * @feature1: name of first feature to set
291 * @varargs: variable argument list
293 * Creates a new #GstCapsFeatures with the given features.
295 * Free-function: gst_caps_features_free
297 * Returns: (transfer full): a new, empty #GstCapsFeatures
302 gst_caps_features_new_id_valist (GQuark feature1, va_list varargs)
304 GstCapsFeatures *features;
306 g_return_val_if_fail (feature1 != 0, NULL);
308 features = gst_caps_features_new_empty ();
311 gst_caps_features_add_id (features, feature1);
312 feature1 = va_arg (varargs, GQuark);
319 * gst_caps_features_set_parent_refcount:
320 * @features: a #GstCapsFeatures
321 * @refcount: (in): a pointer to the parent's refcount
323 * Sets the parent_refcount field of #GstCapsFeatures. This field is used to
324 * determine whether a caps features is mutable or not. This function should only be
325 * called by code implementing parent objects of #GstCapsFeatures, as described in
326 * the MT Refcounting section of the design documents.
328 * Returns: %TRUE if the parent refcount could be set.
333 gst_caps_features_set_parent_refcount (GstCapsFeatures * features,
336 g_return_val_if_fail (features != NULL, FALSE);
338 /* if we have a parent_refcount already, we can only clear
339 * if with a NULL refcount */
340 if (features->parent_refcount) {
341 if (refcount != NULL) {
342 g_return_val_if_fail (refcount == NULL, FALSE);
346 if (refcount == NULL) {
347 g_return_val_if_fail (refcount != NULL, FALSE);
352 features->parent_refcount = refcount;
358 * gst_caps_features_copy:
359 * @features: a #GstCapsFeatures to duplicate
361 * Duplicates a #GstCapsFeatures and all its values.
363 * Free-function: gst_caps_features_free
365 * Returns: (transfer full): a new #GstCapsFeatures.
370 gst_caps_features_copy (const GstCapsFeatures * features)
372 GstCapsFeatures *copy;
375 g_return_val_if_fail (features != NULL, NULL);
377 copy = gst_caps_features_new_empty ();
378 n = gst_caps_features_get_size (features);
379 for (i = 0; i < n; i++)
380 gst_caps_features_add_id (copy, gst_caps_features_get_nth_id (features, i));
381 copy->is_any = features->is_any;
387 * gst_caps_features_free:
388 * @features: (in) (transfer full): the #GstCapsFeatures to free
390 * Frees a #GstCapsFeatures and all its values. The caps features must not
391 * have a parent when this function is called.
396 gst_caps_features_free (GstCapsFeatures * features)
398 g_return_if_fail (features != NULL);
399 g_return_if_fail (features->parent_refcount == NULL);
401 g_array_free (features->array, TRUE);
403 memset (features, 0xff, sizeof (GstCapsFeatures));
405 GST_TRACE ("free caps features %p", features);
407 g_slice_free (GstCapsFeatures, features);
411 * gst_caps_features_to_string:
412 * @features: a #GstCapsFeatures
414 * Converts @features to a human-readable string representation.
416 * For debugging purposes its easier to do something like this:
418 * GST_LOG ("features is %" GST_PTR_FORMAT, features);
420 * This prints the features in human readable form.
422 * Free-function: g_free
424 * Returns: (transfer full): a pointer to string allocated by g_malloc().
425 * g_free() after usage.
430 gst_caps_features_to_string (const GstCapsFeatures * features)
434 g_return_val_if_fail (features != NULL, NULL);
436 s = g_string_sized_new (FEATURES_ESTIMATED_STRING_LEN (features));
438 priv_gst_caps_features_append_to_gstring (features, s);
440 return g_string_free (s, FALSE);
444 priv_gst_caps_features_append_to_gstring (const GstCapsFeatures * features,
449 g_return_if_fail (features != NULL);
451 if (features->array->len == 0 && features->is_any) {
452 g_string_append (s, "ANY");
456 n = features->array->len;
457 for (i = 0; i < n; i++) {
458 GQuark *quark = &g_array_index (features->array, GQuark, i);
460 g_string_append (s, g_quark_to_string (*quark));
462 g_string_append (s, ", ");
467 * gst_caps_features_from_string:
468 * @features: a string representation of a #GstCapsFeatures.
470 * Creates a #GstCapsFeatures from a string representation.
472 * Free-function: gst_caps_features_free
474 * Returns: (transfer full) (nullable): a new #GstCapsFeatures or
475 * %NULL when the string could not be parsed. Free with
476 * gst_caps_features_free() after use.
481 gst_caps_features_from_string (const gchar * features)
483 GstCapsFeatures *ret;
484 gboolean escape = FALSE;
485 const gchar *features_orig = features;
486 const gchar *feature;
488 ret = gst_caps_features_new_empty ();
490 if (!features || *features == '\0')
493 if (strcmp (features, "ANY") == 0) {
498 /* Skip trailing spaces */
499 while (*features == ' ')
510 } else if ((!escape && c == ',') || c == '\0') {
511 guint len = features - feature + 1;
516 g_warning ("Failed deserialize caps features '%s'", features_orig);
517 gst_caps_features_free (ret);
521 tmp = g_malloc (len);
522 memcpy (tmp, feature, len - 1);
531 if (strstr (tmp, " ") != NULL || *tmp == '\0') {
533 g_warning ("Failed deserialize caps features '%s'", features_orig);
534 gst_caps_features_free (ret);
538 gst_caps_features_add (ret, tmp);
544 /* Skip to the next value */
546 while (*features == ' ')
559 * gst_caps_features_get_size:
560 * @features: a #GstCapsFeatures.
562 * Returns the number of features in @features.
564 * Returns: The number of features in @features.
569 gst_caps_features_get_size (const GstCapsFeatures * features)
571 g_return_val_if_fail (features != NULL, 0);
573 return features->array->len;
577 * gst_caps_features_get_nth:
578 * @features: a #GstCapsFeatures.
579 * @i: index of the feature
581 * Returns the @i-th feature of @features.
583 * Returns: The @i-th feature of @features.
588 gst_caps_features_get_nth (const GstCapsFeatures * features, guint i)
590 const gchar *feature;
593 g_return_val_if_fail (features != NULL, NULL);
595 quark = gst_caps_features_get_nth_id (features, i);
599 feature = g_quark_to_string (quark);
604 * gst_caps_features_get_nth_id:
605 * @features: a #GstCapsFeatures.
606 * @i: index of the feature
608 * Returns the @i-th feature of @features.
610 * Returns: The @i-th feature of @features.
615 gst_caps_features_get_nth_id (const GstCapsFeatures * features, guint i)
619 g_return_val_if_fail (features != NULL, 0);
620 g_return_val_if_fail (i < features->array->len, 0);
622 quark = &g_array_index (features->array, GQuark, i);
628 * gst_caps_features_contains:
629 * @features: a #GstCapsFeatures.
630 * @feature: a feature
632 * Check if @features contains @feature.
634 * Returns: %TRUE if @features contains @feature.
639 gst_caps_features_contains (const GstCapsFeatures * features,
640 const gchar * feature)
642 g_return_val_if_fail (features != NULL, FALSE);
643 g_return_val_if_fail (feature != NULL, FALSE);
645 return gst_caps_features_contains_id (features,
646 g_quark_from_string (feature));
650 * gst_caps_features_contains_id:
651 * @features: a #GstCapsFeatures.
652 * @feature: a feature
654 * Check if @features contains @feature.
656 * Returns: %TRUE if @features contains @feature.
661 gst_caps_features_contains_id (const GstCapsFeatures * features, GQuark feature)
665 g_return_val_if_fail (features != NULL, FALSE);
666 g_return_val_if_fail (feature != 0, FALSE);
668 if (features->is_any)
671 n = features->array->len;
673 return feature == _gst_caps_feature_memory_system_memory;
675 for (i = 0; i < n; i++) {
676 if (gst_caps_features_get_nth_id (features, i) == feature)
684 * gst_caps_features_is_equal:
685 * @features1: a #GstCapsFeatures.
686 * @features2: a #GstCapsFeatures.
688 * Check if @features1 and @features2 are equal.
690 * Returns: %TRUE if @features1 and @features2 are equal.
695 gst_caps_features_is_equal (const GstCapsFeatures * features1,
696 const GstCapsFeatures * features2)
700 g_return_val_if_fail (features1 != NULL, FALSE);
701 g_return_val_if_fail (features2 != NULL, FALSE);
703 if (features1->is_any || features2->is_any)
706 /* Check for the sysmem==empty case */
707 if (features1->array->len == 0 && features2->array->len == 0)
709 if (features1->array->len == 0 && features2->array->len == 1
710 && gst_caps_features_contains_id (features2,
711 _gst_caps_feature_memory_system_memory))
713 if (features2->array->len == 0 && features1->array->len == 1
714 && gst_caps_features_contains_id (features1,
715 _gst_caps_feature_memory_system_memory))
718 if (features1->array->len != features2->array->len)
721 n = features1->array->len;
722 for (i = 0; i < n; i++)
723 if (!gst_caps_features_contains_id (features2,
724 gst_caps_features_get_nth_id (features1, i)))
731 * gst_caps_features_is_any:
732 * @features: a #GstCapsFeatures.
734 * Check if @features is %GST_CAPS_FEATURES_ANY.
736 * Returns: %TRUE if @features is %GST_CAPS_FEATURES_ANY.
741 gst_caps_features_is_any (const GstCapsFeatures * features)
743 g_return_val_if_fail (features != NULL, FALSE);
745 return features->is_any;
749 * gst_caps_features_add:
750 * @features: a #GstCapsFeatures.
751 * @feature: a feature.
753 * Adds @feature to @features.
758 gst_caps_features_add (GstCapsFeatures * features, const gchar * feature)
760 g_return_if_fail (features != NULL);
761 g_return_if_fail (IS_MUTABLE (features));
762 g_return_if_fail (feature != NULL);
763 g_return_if_fail (!features->is_any);
765 gst_caps_features_add_id (features, g_quark_from_string (feature));
769 * gst_caps_features_add_id:
770 * @features: a #GstCapsFeatures.
771 * @feature: a feature.
773 * Adds @feature to @features.
778 gst_caps_features_add_id (GstCapsFeatures * features, GQuark feature)
780 g_return_if_fail (features != NULL);
781 g_return_if_fail (IS_MUTABLE (features));
782 g_return_if_fail (feature != 0);
783 g_return_if_fail (!features->is_any);
785 if (!gst_caps_feature_name_is_valid (g_quark_to_string (feature))) {
786 g_warning ("Invalid caps feature name: %s", g_quark_to_string (feature));
790 /* If features is empty it will contain sysmem, however
791 * we want to add it explicitely if it is tried to be
792 * added as first features
794 if (features->array->len > 0
795 && gst_caps_features_contains_id (features, feature))
798 g_array_append_val (features->array, feature);
802 * gst_caps_features_remove:
803 * @features: a #GstCapsFeatures.
804 * @feature: a feature.
806 * Removes @feature from @features.
811 gst_caps_features_remove (GstCapsFeatures * features, const gchar * feature)
813 g_return_if_fail (features != NULL);
814 g_return_if_fail (IS_MUTABLE (features));
815 g_return_if_fail (feature != NULL);
817 gst_caps_features_remove_id (features, g_quark_from_string (feature));
821 * gst_caps_features_remove_id:
822 * @features: a #GstCapsFeatures.
823 * @feature: a feature.
825 * Removes @feature from @features.
830 gst_caps_features_remove_id (GstCapsFeatures * features, GQuark feature)
834 g_return_if_fail (features != NULL);
835 g_return_if_fail (IS_MUTABLE (features));
836 g_return_if_fail (feature != 0);
838 n = features->array->len;
839 for (i = 0; i < n; i++) {
840 GQuark quark = gst_caps_features_get_nth_id (features, i);
842 if (quark == feature) {
843 g_array_remove_index_fast (features->array, i);
850 gst_caps_features_transform_to_string (const GValue * src_value,
853 g_return_if_fail (src_value != NULL);
854 g_return_if_fail (dest_value != NULL);
856 dest_value->data[0].v_pointer =
857 gst_caps_features_to_string (src_value->data[0].v_pointer);