Documentation updates
[platform/upstream/gstreamer.git] / gst / gstcaps.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstcaps.c: Element capabilities subsystem
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* #define GST_DEBUG_ENABLED */
24 #include "gst_private.h"
25
26 #include "gstcaps.h"
27 #include "gsttype.h"
28
29 static GMemChunk *_gst_caps_chunk;
30 static GMutex *_gst_caps_chunk_lock;
31
32 GType _gst_caps_type;
33
34 void
35 _gst_caps_initialize (void)
36 {
37   _gst_caps_chunk = g_mem_chunk_new ("GstCaps",
38                   sizeof (GstCaps), sizeof (GstCaps) * 256,
39                   G_ALLOC_AND_FREE);
40   _gst_caps_chunk_lock = g_mutex_new ();
41
42   _gst_caps_type = g_boxed_type_register_static ("GstCaps",
43                                        (GBoxedCopyFunc) gst_caps_ref,
44                                        (GBoxedFreeFunc) gst_caps_unref);
45
46 }
47
48 static guint16
49 get_type_for_mime (const gchar *mime)
50 {
51   guint16 typeid;
52
53   typeid = gst_type_find_by_mime (mime);
54   if (typeid == 0) {
55      GstTypeDefinition definition;
56      GstTypeFactory *factory;
57
58      definition.name = "capstype";
59      definition.mime = g_strdup (mime);
60      definition.exts = NULL;
61      definition.typefindfunc = NULL;
62
63      factory = gst_typefactory_new (&definition);
64
65      typeid = gst_type_register (factory);
66   }
67   return typeid;
68 }
69
70 /**
71  * gst_caps_new:
72  * @name: the name of this capability
73  * @mime: the mime type to attach to the capability
74  * @props: the properties to add to this capability
75  *
76  * Create a new capability with the given mime typei and properties.
77  *
78  * Returns: a new capability
79  */
80 GstCaps*
81 gst_caps_new (const gchar *name, const gchar *mime, GstProps *props)
82 {
83   g_return_val_if_fail (mime != NULL, NULL);
84
85   return gst_caps_new_id (name, get_type_for_mime (mime), props);
86 }
87
88 /**
89  * gst_caps_new_id:
90  * @name: the name of this capability
91  * @id: the id of the mime type 
92  * @props: the properties to add to this capability
93  *
94  * Create a new capability with the given mime typeid and properties.
95  *
96  * Returns: a new capability
97  */
98 GstCaps*
99 gst_caps_new_id (const gchar *name, const guint16 id, GstProps *props)
100 {
101   GstCaps *caps;
102
103   g_mutex_lock (_gst_caps_chunk_lock);
104   caps = g_mem_chunk_alloc (_gst_caps_chunk);
105   g_mutex_unlock (_gst_caps_chunk_lock);
106
107   caps->name = g_strdup (name);
108   caps->id = id;
109   caps->properties = props;
110   caps->next = NULL;
111   caps->refcount = 1;
112   if (props)
113     caps->fixed = props->fixed;
114   else
115     caps->fixed = TRUE;
116
117   return caps;
118 }
119
120 /**
121  * gst_caps_destroy:
122  * @caps: the caps to destroy
123  *
124  * Frees the memory used by this caps structure and all
125  * the chained caps and properties.
126  */
127 void
128 gst_caps_destroy (GstCaps *caps)
129 {
130   GstCaps *next;
131
132   if (caps == NULL)
133     return;
134   
135   next = caps->next;
136
137   gst_props_unref (caps->properties);
138   g_free (caps->name);
139   g_mutex_lock (_gst_caps_chunk_lock);
140   g_mem_chunk_free (_gst_caps_chunk, caps);
141   g_mutex_unlock (_gst_caps_chunk_lock);
142
143   if (next) 
144     gst_caps_unref (next);
145 }
146
147 /**
148  * gst_caps_debug:
149  * @caps: the caps to print out
150  * @label: a label to put on the printout, or NULL
151  *
152  * Print out the contents of the caps structure. Useful for debugging.
153  */
154 void
155 gst_caps_debug (GstCaps *caps, const gchar *label)
156 {
157   GST_DEBUG_ENTER ("caps debug: %s", label);
158   while (caps) {
159     GST_DEBUG (GST_CAT_CAPS, "caps: %p %s %s", caps, caps->name, gst_caps_get_mime (caps));
160
161     if (caps->properties) {
162       gst_props_debug (caps->properties);
163     }
164     else {
165       GST_DEBUG (GST_CAT_CAPS, "no properties");
166     }
167
168     caps = caps->next;
169   }
170   GST_DEBUG_LEAVE ("caps debug");
171 }
172
173 /**
174  * gst_caps_unref:
175  * @caps: the caps to unref
176  *
177  * Decrease the refcount of this caps structure, 
178  * destroying it when the refcount is 0
179  *
180  * Returns: caps or NULL if the refcount reached 0
181  */
182 GstCaps*
183 gst_caps_unref (GstCaps *caps)
184 {
185   gboolean zero;
186   GstCaps **next;
187
188   if (caps == NULL)
189     return NULL;
190
191   g_return_val_if_fail (caps->refcount > 0, NULL);
192
193   caps->refcount--;
194   zero = (caps->refcount == 0);
195   next = &caps->next;
196
197   if (*next)
198     *next = gst_caps_unref (*next);
199
200   if (zero) {
201     gst_caps_destroy (caps);
202     caps = NULL;
203   }
204   return caps;
205 }
206
207 /**
208  * gst_caps_ref:
209  * @caps: the caps to ref
210  *
211  * Increase the refcount of this caps structure
212  *
213  * Returns: the caps with the refcount incremented
214  */
215 GstCaps*
216 gst_caps_ref (GstCaps *caps)
217 {
218   g_return_val_if_fail (caps != NULL, NULL);
219
220   caps->refcount++;
221
222   return caps;
223 }
224
225 /**
226  * gst_caps_copy_1:
227  * @caps: the caps to copy
228  *
229  * Copies the caps, not copying any chained caps.
230  *
231  * Returns: a copy of the GstCaps structure.
232  */
233 GstCaps*
234 gst_caps_copy_1 (GstCaps *caps)
235 {
236   GstCaps *newcaps;
237   
238   if (!caps)
239     return NULL;
240
241   newcaps = gst_caps_new_id (
242                   caps->name,
243                   caps->id,
244                   gst_props_copy (caps->properties));
245
246   return newcaps;
247 }
248
249 /**
250  * gst_caps_copy:
251  * @caps: the caps to copy
252  *
253  * Copies the caps.
254  *
255  * Returns: a copy of the GstCaps structure.
256  */
257 GstCaps*
258 gst_caps_copy (GstCaps *caps)
259 {
260   GstCaps *new = NULL, *walk = NULL;
261
262   while (caps) {
263     GstCaps *newcaps;
264
265     newcaps = gst_caps_copy_1 (caps);
266
267     if (new == NULL) {
268       new = walk = newcaps;
269     }
270     else {
271       walk = walk->next = newcaps;
272     }
273     caps = caps->next;
274   }
275
276   return new;
277 }
278
279 /**
280  * gst_caps_copy_on_write:
281  * @caps: the caps to copy
282  *
283  * Copies the caps if the refcount is greater than 1
284  *
285  * Returns: a pointer to a GstCaps strcuture that can
286  * be safely written to
287  */
288 GstCaps*
289 gst_caps_copy_on_write (GstCaps *caps)
290 {
291   GstCaps *new = caps;
292   gboolean needcopy;
293
294   g_return_val_if_fail (caps != NULL, NULL);
295
296   needcopy = (caps->refcount > 1);
297
298   if (needcopy) {
299     new = gst_caps_copy (caps);
300     gst_caps_unref (caps);
301   }
302
303   return new;
304 }
305
306 /**
307  * gst_caps_get_name:
308  * @caps: the caps to get the name from
309  *
310  * Get the name of a GstCaps structure.
311  *
312  * Returns: the name of the caps
313  */
314 const gchar*
315 gst_caps_get_name (GstCaps *caps)
316 {
317   g_return_val_if_fail (caps != NULL, NULL);
318
319   return (const gchar *)caps->name;
320 }
321
322 /**
323  * gst_caps_set_name:
324  * @caps: the caps to set the name to
325  * @name: the name to set
326  *
327  * Set the name of a caps.
328  */
329 void
330 gst_caps_set_name (GstCaps *caps, const gchar *name)
331 {
332   g_return_if_fail (caps != NULL);
333
334   if (caps->name)
335     g_free (caps->name);
336
337   caps->name = g_strdup (name);
338 }
339
340 /**
341  * gst_caps_get_mime:
342  * @caps: the caps to get the mime type from
343  *
344  * Get the mime type of the caps as a string.
345  *
346  * Returns: the mime type of the caps
347  */
348 const gchar*
349 gst_caps_get_mime (GstCaps *caps)
350 {
351   GstType *type;
352
353   g_return_val_if_fail (caps != NULL, NULL);
354
355   type = gst_type_find_by_id (caps->id);
356
357   if (type)
358     return type->mime;
359   else
360     return "unknown/unknown";
361 }
362
363 /**
364  * gst_caps_set_mime:
365  * @caps: the caps to set the mime type to
366  * @mime: the mime type to attach to the caps
367  *
368  * Set the mime type of the caps as a string.
369  */
370 void
371 gst_caps_set_mime (GstCaps *caps, const gchar *mime)
372 {
373   g_return_if_fail (caps != NULL);
374   g_return_if_fail (mime != NULL);
375
376   caps->id = get_type_for_mime (mime);
377 }
378
379 /**
380  * gst_caps_get_type_id:
381  * @caps: the caps to get the type id from
382  *
383  * Get the type id of the caps.
384  *
385  * Returns: the type id of the caps
386  */
387 guint16
388 gst_caps_get_type_id (GstCaps *caps)
389 {
390   g_return_val_if_fail (caps != NULL, 0);
391
392   return caps->id;
393 }
394
395 /**
396  * gst_caps_set_type_id:
397  * @caps: the caps to set the type id to
398  * @type_id: the type id to set
399  *
400  * Set the type id of the caps.
401  */
402 void
403 gst_caps_set_type_id (GstCaps *caps, guint16 type_id)
404 {
405   g_return_if_fail (caps != NULL);
406
407   caps->id = type_id;
408 }
409
410 /**
411  * gst_caps_set_props:
412  * @caps: the caps to attach the properties to
413  * @props: the properties to attach
414  *
415  * Set the properties to the given caps.
416  *
417  * Returns: the new caps structure
418  */
419 GstCaps*
420 gst_caps_set_props (GstCaps *caps, GstProps *props)
421 {
422   g_return_val_if_fail (caps != NULL, caps);
423   g_return_val_if_fail (props != NULL, caps);
424   g_return_val_if_fail (caps->properties == NULL, caps);
425
426   caps->properties = props;
427
428   return caps;
429 }
430
431 /**
432  * gst_caps_get_props:
433  * @caps: the caps to get the properties from
434  *
435  * Get the properties of the given caps.
436  *
437  * Returns: the properties of the caps
438  */
439 GstProps*
440 gst_caps_get_props (GstCaps *caps)
441 {
442   g_return_val_if_fail (caps != NULL, NULL);
443
444   return caps->properties;
445 }
446
447 /**
448  * gst_caps_chain:
449  * @caps: a capabilty
450  * @...: more capabilities
451  *
452  * chains the given capabilities
453  *
454  * Returns: the new capability
455  */
456 GstCaps*
457 gst_caps_chain (GstCaps *caps, ...)
458 {
459   GstCaps *orig = caps;
460   va_list var_args;
461
462   va_start (var_args, caps);
463
464   while (caps) {
465     GstCaps *toadd;
466     
467     toadd = va_arg (var_args, GstCaps*);
468     gst_caps_append (caps, toadd);
469     
470     caps = toadd;
471   }
472   va_end (var_args);
473   
474   return orig;
475 }
476
477 /**
478  * gst_caps_append:
479  * @caps: a capabilty
480  * @capstoadd: the capability to append
481  *
482  * Appends a capability to the existing capability.
483  *
484  * Returns: the new capability
485  */
486 GstCaps*
487 gst_caps_append (GstCaps *caps, GstCaps *capstoadd)
488 {
489   GstCaps *orig = caps;
490   
491   if (caps == NULL || caps == capstoadd)
492     return capstoadd;
493   
494   while (caps->next) {
495     caps = caps->next;
496   }
497   caps->next = capstoadd;
498
499   return orig;
500 }
501
502 /**
503  * gst_caps_prepend:
504  * @caps: a capabilty
505  * @capstoadd: a capabilty to prepend
506  *
507  * prepend the capability to the list of capabilities
508  *
509  * Returns: the new capability
510  */
511 GstCaps*
512 gst_caps_prepend (GstCaps *caps, GstCaps *capstoadd)
513 {
514   GstCaps *orig = capstoadd;
515   
516   if (capstoadd == NULL)
517     return caps;
518
519   g_return_val_if_fail (caps != capstoadd, caps);
520
521   while (capstoadd->next) {
522     capstoadd = capstoadd->next;
523   }
524   capstoadd->next = caps;
525
526   return orig;
527 }
528
529 /**
530  * gst_caps_get_by_name:
531  * @caps: a capabilty
532  * @name: the name of the capability to get
533  *
534  * Get the capability with the given name from this
535  * chain of capabilities.
536  *
537  * Returns: the first capability in the chain with the 
538  * given name
539  */
540 GstCaps*
541 gst_caps_get_by_name (GstCaps *caps, const gchar *name)
542 {
543   g_return_val_if_fail (caps != NULL, NULL);
544   g_return_val_if_fail (name != NULL, NULL);
545    
546   while (caps) {
547     if (!strcmp (caps->name, name)) 
548       return caps;
549     caps = caps->next;
550   }
551
552   return NULL;
553 }
554                                                                                                                    
555 static gboolean
556 gst_caps_check_compatibility_func (GstCaps *fromcaps, GstCaps *tocaps)
557 {
558   if (fromcaps->id != tocaps->id) {
559     GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)",
560                gst_type_find_by_id (fromcaps->id)->mime, 
561                gst_type_find_by_id (tocaps->id)->mime);
562     return FALSE;
563   }
564
565   if (tocaps->properties) {
566     if (fromcaps->properties) {
567       return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
568     }
569     else {
570       GST_DEBUG (GST_CAT_CAPS,"no source caps");
571       return FALSE;
572     }
573   }
574   else {
575     /* assume it accepts everything */
576     GST_DEBUG (GST_CAT_CAPS,"no caps");
577     return TRUE;
578   }
579 }
580
581 /**
582  * gst_caps_check_compatibility:
583  * @fromcaps: a capabilty
584  * @tocaps: a capabilty
585  *
586  * Checks whether two capabilities are compatible.
587  *
588  * Returns: TRUE if compatible, FALSE otherwise
589  */
590 gboolean
591 gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
592 {
593   if (fromcaps == NULL) {
594     if (tocaps == NULL) {
595       GST_DEBUG (GST_CAT_CAPS,"no caps");
596       return TRUE;
597     }
598     else {
599       GST_DEBUG (GST_CAT_CAPS,"no source but destination caps");
600       return FALSE;
601     }
602   }
603   else {
604     if (tocaps == NULL) {
605       GST_DEBUG (GST_CAT_CAPS,"source caps and no destination caps");
606       return TRUE;
607     }
608   }
609
610   while (fromcaps) {
611     GstCaps *destcaps = tocaps;
612
613     while (destcaps) {
614       if (gst_caps_check_compatibility_func (fromcaps, destcaps))
615         return TRUE;
616
617       destcaps =  destcaps->next;
618     }
619     fromcaps =  fromcaps->next;
620   }
621   return FALSE;
622 }
623
624 static GstCaps*
625 gst_caps_intersect_func (GstCaps *caps1, GstCaps *caps2)
626 {
627   GstCaps *result = NULL;
628   GstProps *props;
629
630   if (caps1->id != caps2->id) {
631     GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)",
632                gst_type_find_by_id (caps1->id)->mime, 
633                gst_type_find_by_id (caps2->id)->mime);
634     return NULL;
635   }
636
637   if (caps1->properties == NULL) {
638     return gst_caps_ref (caps2);
639   }
640   if (caps2->properties == NULL) {
641     return gst_caps_ref (caps1);
642   }
643   
644   props = gst_props_intersect (caps1->properties, caps2->properties);
645   if (props) {
646     result = gst_caps_new_id ("intersect", caps1->id, props);
647   }
648
649   return result;
650 }
651
652 /**
653  * gst_caps_intersect:
654  * @caps1: a capabilty
655  * @caps2: a capabilty
656  *
657  * Make the intersection between two caps.
658  *
659  * Returns: The intersection of the two caps or NULL if the intersection
660  * is empty.
661  */
662 GstCaps*
663 gst_caps_intersect (GstCaps *caps1, GstCaps *caps2)
664 {
665   GstCaps *result = NULL, *walk = NULL;
666
667   if (caps1 == NULL) {
668     GST_DEBUG (GST_CAT_CAPS, "first caps is NULL, return other caps");
669     return gst_caps_copy (caps2);
670   }
671   if (caps2 == NULL) {
672     GST_DEBUG (GST_CAT_CAPS, "second caps is NULL, return other caps");
673     return gst_caps_copy (caps1);
674   }
675
676   while (caps1) {
677     GstCaps *othercaps = caps2;
678
679     while (othercaps) {
680       GstCaps *intersection;
681       
682       intersection = gst_caps_intersect_func (caps1, othercaps);
683
684       if (intersection) {
685         if (!result) {
686           walk = result = intersection;
687         }
688         else {
689           walk = walk->next = intersection;
690         }
691       }
692       othercaps = othercaps->next;
693     }
694     caps1 =  caps1->next;
695   }
696
697   return result;
698 }
699
700 /**
701  * gst_caps_normalize:
702  * @caps: a capabilty
703  *
704  * Make the normalisation of the caps. This will return a new caps
705  * that is equivalent to the input caps with the exception that all
706  * lists are unrolled. This function is useful when you want to iterate
707  * the caps.
708  *
709  * Returns: The normalisation of the caps.
710  */
711 GstCaps*
712 gst_caps_normalize (GstCaps *caps)
713 {
714   GstCaps *result = NULL, *walk = caps;
715
716   if (caps == NULL)
717     return caps;
718
719   while (caps) {
720     GList *proplist;
721
722     proplist = gst_props_normalize (caps->properties);
723     if (proplist && g_list_next (proplist) == NULL) {
724       if (result == NULL)
725         walk = result = caps;
726       else {
727         walk = walk->next = caps;
728       }
729       goto next;
730     }
731
732     while (proplist) {
733       GstProps *props = (GstProps *) proplist->data;
734       GstCaps *newcaps = gst_caps_new_id (caps->name, caps->id, props);
735
736       if (result == NULL)
737         walk = result = newcaps;
738       else {
739         walk = walk->next = newcaps;
740       }
741       proplist = g_list_next (proplist);  
742     }
743 next:
744     caps = caps->next;
745   }
746   return result;
747 }
748
749 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
750 /**
751  * gst_caps_save_thyself:
752  * @caps: a capabilty to save
753  * @parent: the parent XML node pointer
754  *
755  * Save the capability into an XML representation.
756  *
757  * Returns: a new XML node pointer
758  */
759 xmlNodePtr
760 gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
761 {
762   xmlNodePtr subtree;
763   xmlNodePtr subsubtree;
764
765   while (caps) {
766     subtree = xmlNewChild (parent, NULL, "capscomp", NULL);
767
768     xmlNewChild (subtree, NULL, "name", caps->name);
769     xmlNewChild (subtree, NULL, "type", gst_type_find_by_id (caps->id)->mime);
770     if (caps->properties) {
771       subsubtree = xmlNewChild (subtree, NULL, "properties", NULL);
772
773       gst_props_save_thyself (caps->properties, subsubtree);
774     }
775
776     caps = caps->next;
777   }
778
779   return parent;
780 }
781
782 /**
783  * gst_caps_load_thyself:
784  * @parent: the parent XML node pointer
785  *
786  * Load a new caps from the XML representation.
787  *
788  * Returns: a new capability
789  */
790 GstCaps*
791 gst_caps_load_thyself (xmlNodePtr parent)
792 {
793   GstCaps *result = NULL;
794   xmlNodePtr field = parent->xmlChildrenNode;
795
796   while (field) {
797     if (!strcmp (field->name, "capscomp")) {
798       xmlNodePtr subfield = field->xmlChildrenNode;
799       GstCaps *caps;
800       gchar *content;
801
802       g_mutex_lock (_gst_caps_chunk_lock);
803       caps = g_mem_chunk_alloc0 (_gst_caps_chunk);
804       g_mutex_unlock (_gst_caps_chunk_lock);
805
806       caps->refcount = 1;
807       caps->next = NULL;
808       caps->fixed = TRUE;
809         
810       while (subfield) {
811         if (!strcmp (subfield->name, "name")) {
812           caps->name = xmlNodeGetContent (subfield);
813         }
814         if (!strcmp (subfield->name, "type")) {
815           content = xmlNodeGetContent (subfield);
816           caps->id = get_type_for_mime (content);
817           g_free (content);
818         }
819         else if (!strcmp (subfield->name, "properties")) {
820           caps->properties = gst_props_load_thyself (subfield);
821         }
822         
823         subfield = subfield->next;
824       }
825       result = gst_caps_append (result, caps);
826     }
827     field = field->next;
828   }
829
830   return result;
831 }
832
833 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */