capsfeatures: For copying features it's not required to have no parent refcount
[platform/upstream/gstreamer.git] / gst / gstcapsfeatures.c
1 /* GStreamer
2  * Copyright (C) 2013 Collabora Ltd.
3  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:gstcapsfeatures
23  * @short_description: A set of features in caps 
24  * @see_also: #GstCaps
25  *
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.
31  *
32  * Empty #GstCapsFeatures are equivalent with the #GstCapsFeatures that only
33  * contain #GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY.
34  *
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".
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <string.h>
46 #include "gst_private.h"
47 #include "gstcapsfeatures.h"
48 #include <gst/gst.h>
49
50 GST_DEBUG_CATEGORY_STATIC (gst_caps_features_debug);
51 #define GST_CAT_DEFAULT gst_caps_features_debug
52
53 struct _GstCapsFeatures
54 {
55   GType type;
56   gint *parent_refcount;
57   GArray *array;
58 };
59
60 GType _gst_caps_features_type = 0;
61 GstCapsFeatures *_gst_caps_features_memory_system_memory = NULL;
62 static GQuark _gst_caps_feature_memory_system_memory = 0;
63
64 G_DEFINE_BOXED_TYPE (GstCapsFeatures, gst_caps_features,
65     gst_caps_features_copy, gst_caps_features_free);
66
67 #define IS_MUTABLE(features) \
68     (!features->parent_refcount || \
69      g_atomic_int_get (features->parent_refcount) == 1)
70
71 static void
72 gst_caps_features_transform_to_string (const GValue * src_value,
73     GValue * dest_value);
74
75 void
76 _priv_gst_caps_features_initialize (void)
77 {
78   _gst_caps_features_type = gst_caps_features_get_type ();
79   _gst_caps_feature_memory_system_memory =
80       g_quark_from_static_string (GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY);
81
82   g_value_register_transform_func (_gst_caps_features_type, G_TYPE_STRING,
83       gst_caps_features_transform_to_string);
84
85   _gst_caps_features_memory_system_memory =
86       gst_caps_features_new_id (_gst_caps_feature_memory_system_memory, 0);
87
88   GST_DEBUG_CATEGORY_INIT (gst_caps_features_debug, "caps-features", 0,
89       "GstCapsFeatures debug");
90 }
91
92 gboolean
93 gst_is_caps_features (gconstpointer obj)
94 {
95   const GstCapsFeatures *features = obj;
96
97   return (obj != NULL && features->type == _gst_caps_features_type);
98 }
99
100 static gboolean
101 gst_caps_feature_name_is_valid (const gchar * feature)
102 {
103 #ifndef G_DISABLE_CHECKS
104   while (TRUE) {
105     if (g_ascii_isalpha (*feature))
106       feature++;
107     else if (*feature == ':')
108       break;
109     else
110       return FALSE;
111   }
112
113   if (*feature != ':')
114     return FALSE;
115
116   feature++;
117   if (*feature == '\0' || !g_ascii_isalpha (*feature))
118     return FALSE;
119
120   while (TRUE) {
121     if (g_ascii_isalnum (*feature))
122       feature++;
123     else if (*feature == '\0')
124       break;
125     else
126       return FALSE;
127   }
128 #endif
129
130   return TRUE;
131 }
132
133 /**
134  * gst_caps_features_new_empty:
135  *
136  * Creates a new, empty #GstCapsFeatures.
137  *
138  * Free-function: gst_caps_features_free
139  *
140  * Returns: (transfer full): a new, empty #GstCapsFeatures
141  */
142 GstCapsFeatures *
143 gst_caps_features_new_empty (void)
144 {
145   GstCapsFeatures *features;
146
147   features = g_slice_new (GstCapsFeatures);
148   features->type = _gst_caps_features_type;
149   features->parent_refcount = NULL;
150   features->array = g_array_new (FALSE, FALSE, sizeof (GQuark));
151
152   GST_TRACE ("created caps features %p", features);
153
154   return features;
155 }
156
157 /**
158  * gst_caps_features_new:
159  * @feature1: name of first feature to set
160  * @...: additional features
161  *
162  * Creates a new #GstCapsFeatures with the given features.
163  * The last argument must be NULL.
164  *
165  * Free-function: gst_caps_features_free
166  *
167  * Returns: (transfer full): a new, empty #GstCapsFeatures
168  */
169 GstCapsFeatures *
170 gst_caps_features_new (const gchar * feature1, ...)
171 {
172   GstCapsFeatures *features;
173   va_list varargs;
174
175   g_return_val_if_fail (feature1 != NULL, NULL);
176
177   va_start (varargs, feature1);
178   features = gst_caps_features_new_valist (feature1, varargs);
179   va_end (varargs);
180
181   return features;
182 }
183
184 /**
185  * gst_caps_features_new_valist:
186  * @feature1: name of first feature to set
187  * @varargs: variable argument list
188  *
189  * Creates a new #GstCapsFeatures with the given features.
190  *
191  * Free-function: gst_caps_features_free
192  *
193  * Returns: (transfer full): a new, empty #GstCapsFeatures
194  */
195 GstCapsFeatures *
196 gst_caps_features_new_valist (const gchar * feature1, va_list varargs)
197 {
198   GstCapsFeatures *features;
199
200   g_return_val_if_fail (feature1 != NULL, NULL);
201
202   features = gst_caps_features_new_empty ();
203
204   while (feature1) {
205     gst_caps_features_add (features, feature1);
206     feature1 = va_arg (varargs, const gchar *);
207   }
208
209   return features;
210 }
211
212 /**
213  * gst_caps_features_new_id:
214  * @feature1: name of first feature to set
215  * @...: additional features
216  *
217  * Creates a new #GstCapsFeatures with the given features.
218  * The last argument must be 0.
219  *
220  * Free-function: gst_caps_features_free
221  *
222  * Returns: (transfer full): a new, empty #GstCapsFeatures
223  */
224 GstCapsFeatures *
225 gst_caps_features_new_id (GQuark feature1, ...)
226 {
227   GstCapsFeatures *features;
228   va_list varargs;
229
230   g_return_val_if_fail (feature1 != 0, NULL);
231
232   va_start (varargs, feature1);
233   features = gst_caps_features_new_id_valist (feature1, varargs);
234   va_end (varargs);
235
236   return features;
237 }
238
239 /**
240  * gst_caps_features_new_id_valist:
241  * @feature1: name of first feature to set
242  * @varargs: variable argument list
243  *
244  * Creates a new #GstCapsFeatures with the given features.
245  *
246  * Free-function: gst_caps_features_free
247  *
248  * Returns: (transfer full): a new, empty #GstCapsFeatures
249  */
250 GstCapsFeatures *
251 gst_caps_features_new_id_valist (GQuark feature1, va_list varargs)
252 {
253   GstCapsFeatures *features;
254
255   g_return_val_if_fail (feature1 != 0, NULL);
256
257   features = gst_caps_features_new_empty ();
258
259   while (feature1) {
260     gst_caps_features_add_id (features, feature1);
261     feature1 = va_arg (varargs, GQuark);
262   }
263
264   return features;
265 }
266
267 /**
268  * gst_caps_features_set_parent_refcount:
269  * @features: a #GstCapsFeatures
270  * @refcount: (in): a pointer to the parent's refcount
271  *
272  * Sets the parent_refcount field of #GstCapsFeatures. This field is used to
273  * determine whether a caps features is mutable or not. This function should only be
274  * called by code implementing parent objects of #GstCapsFeatures, as described in
275  * the MT Refcounting section of the design documents.
276  *
277  * Returns: %TRUE if the parent refcount could be set.
278  */
279 gboolean
280 gst_caps_features_set_parent_refcount (GstCapsFeatures * features,
281     gint * refcount)
282 {
283   g_return_val_if_fail (features != NULL, FALSE);
284
285   /* if we have a parent_refcount already, we can only clear
286    * if with a NULL refcount */
287   if (features->parent_refcount) {
288     if (refcount != NULL) {
289       g_return_val_if_fail (refcount == NULL, FALSE);
290       return FALSE;
291     }
292   } else {
293     if (refcount == NULL) {
294       g_return_val_if_fail (refcount != NULL, FALSE);
295       return FALSE;
296     }
297   }
298
299   features->parent_refcount = refcount;
300
301   return TRUE;
302 }
303
304 /**
305  * gst_caps_features_copy:
306  * @features: a #GstCapsFeatures to duplicate
307  *
308  * Duplicates a #GstCapsFeatures and all its values.
309  *
310  * Free-function: gst_caps_features_free
311  *
312  * Returns: (transfer full): a new #GstCapsFeatures.
313  */
314 GstCapsFeatures *
315 gst_caps_features_copy (const GstCapsFeatures * features)
316 {
317   GstCapsFeatures *copy;
318   guint i, n;
319
320   g_return_val_if_fail (features != NULL, NULL);
321
322   copy = gst_caps_features_new_empty ();
323   n = gst_caps_features_get_size (features);
324   for (i = 0; i < n; i++)
325     gst_caps_features_add_id (copy, gst_caps_features_get_nth_id (features, i));
326
327   return copy;
328 }
329
330 /**
331  * gst_caps_features_free:
332  * @features: (in) (transfer full): the #GstCapsFeatures to free
333  *
334  * Frees a #GstCapsFeatures and all its values. The caps features must not
335  * have a parent when this function is called.
336  */
337 void
338 gst_caps_features_free (GstCapsFeatures * features)
339 {
340   g_return_if_fail (features != NULL);
341   g_return_if_fail (features->parent_refcount == NULL);
342
343   g_array_free (features->array, TRUE);
344 #ifdef USE_POISONING
345   memset (features, 0xff, sizeof (GstCapsFeatures));
346 #endif
347   GST_TRACE ("free caps features %p", features);
348
349   g_slice_free (GstCapsFeatures, features);
350 }
351
352 /**
353  * gst_caps_features_to_string:
354  * @features: a #GstCapsFeatures
355  *
356  * Converts @features to a human-readable string representation.
357  *
358  * For debugging purposes its easier to do something like this:
359  * |[
360  * GST_LOG ("features is %" GST_PTR_FORMAT, features);
361  * ]|
362  * This prints the features in human readble form.
363  *
364  * Free-function: g_free
365  *
366  * Returns: (transfer full): a pointer to string allocated by g_malloc().
367  *     g_free() after usage.
368  */
369 gchar *
370 gst_caps_features_to_string (const GstCapsFeatures * features)
371 {
372   GString *s;
373
374   g_return_val_if_fail (features != NULL, NULL);
375
376   s = g_string_sized_new (FEATURES_ESTIMATED_STRING_LEN (features));
377
378   priv_gst_caps_features_append_to_gstring (features, s);
379
380   return g_string_free (s, FALSE);
381 }
382
383 void
384 priv_gst_caps_features_append_to_gstring (const GstCapsFeatures * features,
385     GString * s)
386 {
387   guint i, n;
388
389   g_return_if_fail (features != NULL);
390
391   n = features->array->len;
392   for (i = 0; i < n; i++) {
393     GQuark *quark = &g_array_index (features->array, GQuark, i);
394
395     g_string_append (s, g_quark_to_string (*quark));
396     if (i + 1 < n)
397       g_string_append (s, ", ");
398   }
399 }
400
401 /**
402  * gst_caps_features_from_string:
403  * @features: a string representation of a #GstCapsFeatures.
404  *
405  * Creates a #GstCapsFeatures from a string representation.
406  *
407  * Free-function: gst_caps_features_free
408  *
409  * Returns: (transfer full): a new #GstCapsFeatures or NULL when the string could
410  *     not be parsed. Free with gst_caps_features_free() after use.
411  */
412 GstCapsFeatures *
413 gst_caps_features_from_string (const gchar * features)
414 {
415   GstCapsFeatures *ret;
416   gboolean escape = FALSE;
417   const gchar *features_orig = features;
418   const gchar *feature;
419
420   ret = gst_caps_features_new_empty ();
421
422   if (!features || *features == '\0')
423     return ret;
424
425   /* Skip trailing spaces */
426   while (*features == ' ')
427     features++;
428
429   feature = features;
430   while (TRUE) {
431     gchar c = *features;
432
433     if (c == '\\') {
434       escape = TRUE;
435       features++;
436       continue;
437     } else if ((!escape && c == ',') || c == '\0') {
438       guint len = features - feature + 1;
439       gchar *tmp;
440       gchar *p;
441
442       if (len == 1) {
443         g_warning ("Failed deserialize caps features '%s'", features_orig);
444         gst_caps_features_free (ret);
445         return NULL;
446       }
447
448       tmp = g_malloc (len);
449       memcpy (tmp, feature, len - 1);
450       tmp[len - 1] = '\0';
451
452       p = tmp + len - 1;
453       while (*p == ' ') {
454         *p = '\0';
455         p--;
456       }
457
458       if (strstr (tmp, " ") != NULL || *tmp == '\0') {
459         g_free (tmp);
460         g_warning ("Failed deserialize caps features '%s'", features_orig);
461         gst_caps_features_free (ret);
462         return NULL;
463       }
464
465       gst_caps_features_add (ret, tmp);
466       g_free (tmp);
467
468       if (c == '\0')
469         break;
470
471       /* Skip to the next value */
472       features++;
473       while (*features == ' ')
474         features++;
475       feature = features;
476     } else {
477       escape = FALSE;
478       features++;
479     }
480   }
481
482   return ret;
483 }
484
485 /**
486  * gst_caps_features_get_size:
487  * @features: a #GstCapsFeatures.
488  *
489  * Returns the number of features in @features.
490  *
491  * Returns: The number of features in @features.
492  */
493 guint
494 gst_caps_features_get_size (const GstCapsFeatures * features)
495 {
496   g_return_val_if_fail (features != NULL, 0);
497
498   return features->array->len;
499 }
500
501 /**
502  * gst_caps_features_get_nth:
503  * @features: a #GstCapsFeatures.
504  * @i: index of the feature
505  *
506  * Returns the @i-th feature of @features.
507  *
508  * Returns: The @i-th feature of @features.
509  */
510 const gchar *
511 gst_caps_features_get_nth (const GstCapsFeatures * features, guint i)
512 {
513   const gchar *feature;
514   GQuark quark;
515
516   g_return_val_if_fail (features != NULL, NULL);
517
518   quark = gst_caps_features_get_nth_id (features, i);
519   if (!quark)
520     return NULL;
521
522   feature = g_quark_to_string (quark);
523   return feature;
524 }
525
526 /**
527  * gst_caps_features_get_nth_id:
528  * @features: a #GstCapsFeatures.
529  * @i: index of the feature
530  *
531  * Returns the @i-th feature of @features.
532  *
533  * Returns: The @i-th feature of @features.
534  */
535 GQuark
536 gst_caps_features_get_nth_id (const GstCapsFeatures * features, guint i)
537 {
538   GQuark *quark;
539
540   g_return_val_if_fail (features != NULL, 0);
541   g_return_val_if_fail (i < features->array->len, 0);
542
543   quark = &g_array_index (features->array, GQuark, i);
544
545   return *quark;
546 }
547
548 /**
549  * gst_caps_features_contains:
550  * @features: a #GstCapsFeatures.
551  * @feature: a feature
552  *
553  * Returns %TRUE if @features contains @feature.
554  *
555  * Returns: %TRUE if @features contains @feature.
556  */
557 gboolean
558 gst_caps_features_contains (const GstCapsFeatures * features,
559     const gchar * feature)
560 {
561   g_return_val_if_fail (features != NULL, FALSE);
562   g_return_val_if_fail (feature != NULL, FALSE);
563
564   return gst_caps_features_contains_id (features,
565       g_quark_from_string (feature));
566 }
567
568 /**
569  * gst_caps_features_contains_id:
570  * @features: a #GstCapsFeatures.
571  * @feature: a feature
572  *
573  * Returns %TRUE if @features contains @feature.
574  *
575  * Returns: %TRUE if @features contains @feature.
576  */
577 gboolean
578 gst_caps_features_contains_id (const GstCapsFeatures * features, GQuark feature)
579 {
580   guint i, n;
581
582   g_return_val_if_fail (features != NULL, FALSE);
583   g_return_val_if_fail (feature != 0, FALSE);
584
585   n = features->array->len;
586   if (n == 0)
587     return feature == _gst_caps_feature_memory_system_memory;
588
589   for (i = 0; i < n; i++) {
590     if (gst_caps_features_get_nth_id (features, i) == feature)
591       return TRUE;
592   }
593
594   return FALSE;
595 }
596
597 /**
598  * gst_caps_features_is_equal:
599  * @features1: a #GstCapsFeatures.
600  * @features2: a #GstCapsFeatures.
601  *
602  * Returns %TRUE if @features1 and @features2 are equal.
603  *
604  * Returns: %TRUE if @features1 and @features2 are equal.
605  */
606 gboolean
607 gst_caps_features_is_equal (const GstCapsFeatures * features1,
608     const GstCapsFeatures * features2)
609 {
610   guint i, n;
611
612   g_return_val_if_fail (features1 != NULL, FALSE);
613   g_return_val_if_fail (features2 != NULL, FALSE);
614
615   /* Check for the sysmem==empty case */
616   if (features1->array->len == 0 && features2->array->len == 0)
617     return TRUE;
618   if (features1->array->len == 0 && features2->array->len == 1
619       && gst_caps_features_contains_id (features2,
620           _gst_caps_feature_memory_system_memory))
621     return TRUE;
622   if (features2->array->len == 0 && features1->array->len == 1
623       && gst_caps_features_contains_id (features1,
624           _gst_caps_feature_memory_system_memory))
625     return TRUE;
626
627   if (features1->array->len != features2->array->len)
628     return FALSE;
629
630   n = features1->array->len;
631   for (i = 0; i < n; i++)
632     if (!gst_caps_features_contains_id (features2,
633             gst_caps_features_get_nth_id (features1, i)))
634       return FALSE;
635
636   return TRUE;
637 }
638
639 /**
640  * gst_caps_features_add:
641  * @features: a #GstCapsFeatures.
642  * @feature: a feature.
643  *
644  * Adds @feature to @features.
645  */
646 void
647 gst_caps_features_add (GstCapsFeatures * features, const gchar * feature)
648 {
649   g_return_if_fail (features != NULL);
650   g_return_if_fail (IS_MUTABLE (features));
651   g_return_if_fail (feature != NULL);
652
653   gst_caps_features_add_id (features, g_quark_from_string (feature));
654 }
655
656 /**
657  * gst_caps_features_add_id:
658  * @features: a #GstCapsFeatures.
659  * @feature: a feature.
660  *
661  * Adds @feature to @features.
662  */
663 void
664 gst_caps_features_add_id (GstCapsFeatures * features, GQuark feature)
665 {
666   g_return_if_fail (features != NULL);
667   g_return_if_fail (IS_MUTABLE (features));
668   g_return_if_fail (feature != 0);
669
670   if (!gst_caps_feature_name_is_valid (g_quark_to_string (feature))) {
671     g_warning ("Invalid caps feature name: %s", g_quark_to_string (feature));
672     return;
673   }
674
675   /* If features is empty it will contain sysmem, however
676    * we want to add it explicitely if it is tried to be
677    * added as first features
678    */
679   if (features->array->len > 0
680       && gst_caps_features_contains_id (features, feature))
681     return;
682
683   g_array_append_val (features->array, feature);
684 }
685
686 /**
687  * gst_caps_features_remove:
688  * @features: a #GstCapsFeatures.
689  * @feature: a feature.
690  *
691  * Removes @feature from @features.
692  */
693 void
694 gst_caps_features_remove (GstCapsFeatures * features, const gchar * feature)
695 {
696   g_return_if_fail (features != NULL);
697   g_return_if_fail (IS_MUTABLE (features));
698   g_return_if_fail (feature != NULL);
699
700   gst_caps_features_remove_id (features, g_quark_from_string (feature));
701 }
702
703 /**
704  * gst_caps_features_remove_id:
705  * @features: a #GstCapsFeatures.
706  * @feature: a feature.
707  *
708  * Removes @feature from @features.
709  */
710 void
711 gst_caps_features_remove_id (GstCapsFeatures * features, GQuark feature)
712 {
713   guint i, n;
714
715   g_return_if_fail (features != NULL);
716   g_return_if_fail (IS_MUTABLE (features));
717   g_return_if_fail (feature != 0);
718
719   n = features->array->len;
720   for (i = 0; i < n; i++) {
721     GQuark quark = gst_caps_features_get_nth_id (features, i);
722
723     if (quark == feature) {
724       g_array_remove_index_fast (features->array, i);
725       return;
726     }
727   }
728 }
729
730 static void
731 gst_caps_features_transform_to_string (const GValue * src_value,
732     GValue * dest_value)
733 {
734   g_return_if_fail (src_value != NULL);
735   g_return_if_fail (dest_value != NULL);
736
737   dest_value->data[0].v_pointer =
738       gst_caps_features_to_string (src_value->data[0].v_pointer);
739 }