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