Delay pad negotiation until the element is in READY or higher. this will gice the...
[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 void
147 gst_caps_debug (GstCaps *caps)
148 {
149   GST_DEBUG_ENTER ("caps debug");
150   while (caps) {
151     GST_DEBUG (GST_CAT_CAPS, "caps: %p %s %s\n", caps, caps->name, gst_caps_get_mime (caps));
152
153     if (caps->properties) {
154       gst_props_debug (caps->properties);
155     }
156     else {
157       GST_DEBUG (GST_CAT_CAPS, "no properties\n");
158     }
159
160     caps = caps->next;
161   }
162   GST_DEBUG_LEAVE ("caps debug");
163 }
164
165 /**
166  * gst_caps_unref:
167  * @caps: the caps to unref
168  *
169  * Decrease the refcount of this caps structure, 
170  * destroying it when the refcount is 0
171  *
172  * Returns: caps or NULL if the refcount reached 0
173  */
174 GstCaps*
175 gst_caps_unref (GstCaps *caps)
176 {
177   gboolean zero;
178   GstCaps **next;
179
180   if (caps == NULL)
181     return NULL;
182
183   g_return_val_if_fail (caps->refcount > 0, NULL);
184
185   GST_CAPS_LOCK (caps);
186   caps->refcount--;
187   zero = (caps->refcount == 0);
188   next = &caps->next;
189   GST_CAPS_UNLOCK (caps);
190
191   if (*next)
192     *next = gst_caps_unref (*next);
193
194   if (zero) {
195     gst_caps_destroy (caps);
196     caps = NULL;
197   }
198   return caps;
199 }
200
201 /**
202  * gst_caps_ref:
203  * @caps: the caps to ref
204  *
205  * Increase the refcount of this caps structure
206  *
207  * Returns: the caps with the refcount incremented
208  */
209 GstCaps*
210 gst_caps_ref (GstCaps *caps)
211 {
212   g_return_val_if_fail (caps != NULL, NULL);
213
214   GST_CAPS_LOCK (caps);
215   caps->refcount++;
216   GST_CAPS_UNLOCK (caps);
217
218   return caps;
219 }
220
221 /**
222  * gst_caps_copy_1:
223  * @caps: the caps to copy
224  *
225  * Copies the caps, not copying any chained caps.
226  *
227  * Returns: a copy of the GstCaps structure.
228  */
229 GstCaps*
230 gst_caps_copy_1 (GstCaps *caps)
231 {
232   GstCaps *newcaps;
233   
234   if (!caps)
235     return NULL;
236
237   newcaps = gst_caps_new_id (
238                   caps->name,
239                   caps->id,
240                   gst_props_copy (caps->properties));
241
242   return newcaps;
243 }
244
245 /**
246  * gst_caps_copy:
247  * @caps: the caps to copy
248  *
249  * Copies the caps.
250  *
251  * Returns: a copy of the GstCaps structure.
252  */
253 GstCaps*
254 gst_caps_copy (GstCaps *caps)
255 {
256   GstCaps *new = NULL, *walk = NULL;
257
258   while (caps) {
259     GstCaps *newcaps;
260
261     newcaps = gst_caps_copy_1 (caps);
262
263     if (new == NULL) {
264       new = walk = newcaps;
265     }
266     else {
267       walk = walk->next = newcaps;
268     }
269     caps = caps->next;
270   }
271
272   return new;
273 }
274
275 /**
276  * gst_caps_copy_on_write:
277  * @caps: the caps to copy
278  *
279  * Copies the caps if the refcount is greater than 1
280  *
281  * Returns: a pointer to a GstCaps strcuture that can
282  * be safely written to
283  */
284 GstCaps*
285 gst_caps_copy_on_write (GstCaps *caps)
286 {
287   GstCaps *new = caps;
288   gboolean needcopy;
289
290   g_return_val_if_fail (caps != NULL, NULL);
291
292   GST_CAPS_LOCK (caps);
293   needcopy = (caps->refcount > 1);
294   GST_CAPS_UNLOCK (caps);
295
296   if (needcopy) {
297     new = gst_caps_copy (caps);
298     gst_caps_unref (caps);
299   }
300
301   return new;
302 }
303
304 /**
305  * gst_caps_get_name:
306  * @caps: the caps to get the name from
307  *
308  * Get the name of a GstCaps structure.
309  *
310  * Returns: the name of the caps
311  */
312 const gchar*
313 gst_caps_get_name (GstCaps *caps)
314 {
315   g_return_val_if_fail (caps != NULL, NULL);
316
317   return (const gchar *)caps->name;
318 }
319
320 /**
321  * gst_caps_set_name:
322  * @caps: the caps to set the name to
323  * @name: the name to set
324  *
325  * Set the name of a caps.
326  */
327 void
328 gst_caps_set_name (GstCaps *caps, const gchar *name)
329 {
330   g_return_if_fail (caps != NULL);
331
332   if (caps->name)
333     g_free (caps->name);
334
335   caps->name = g_strdup (name);
336 }
337
338 /**
339  * gst_caps_get_mime:
340  * @caps: the caps to get the mime type from
341  *
342  * Get the mime type of the caps as a string.
343  *
344  * Returns: the mime type of the caps
345  */
346 const gchar*
347 gst_caps_get_mime (GstCaps *caps)
348 {
349   GstType *type;
350
351   g_return_val_if_fail (caps != NULL, NULL);
352
353   type = gst_type_find_by_id (caps->id);
354
355   if (type)
356     return type->mime;
357   else
358     return "unknown/unknown";
359 }
360
361 /**
362  * gst_caps_set_mime:
363  * @caps: the caps to set the mime type to
364  * @mime: the mime type to attach to the caps
365  *
366  * Set the mime type of the caps as a string.
367  */
368 void
369 gst_caps_set_mime (GstCaps *caps, const gchar *mime)
370 {
371   g_return_if_fail (caps != NULL);
372   g_return_if_fail (mime != NULL);
373
374   caps->id = get_type_for_mime (mime);
375 }
376
377 /**
378  * gst_caps_get_type_id:
379  * @caps: the caps to get the type id from
380  *
381  * Get the type id of the caps.
382  *
383  * Returns: the type id of the caps
384  */
385 guint16
386 gst_caps_get_type_id (GstCaps *caps)
387 {
388   g_return_val_if_fail (caps != NULL, 0);
389
390   return caps->id;
391 }
392
393 /**
394  * gst_caps_set_type_id:
395  * @caps: the caps to set the type id to
396  * @type_id: the type id to set
397  *
398  * Set the type id of the caps.
399  */
400 void
401 gst_caps_set_type_id (GstCaps *caps, guint16 type_id)
402 {
403   g_return_if_fail (caps != NULL);
404
405   caps->id = type_id;
406 }
407
408 /**
409  * gst_caps_set_props:
410  * @caps: the caps to attach the properties to
411  * @props: the properties to attach
412  *
413  * Set the properties to the given caps.
414  *
415  * Returns: the new caps structure
416  */
417 GstCaps*
418 gst_caps_set_props (GstCaps *caps, GstProps *props)
419 {
420   g_return_val_if_fail (caps != NULL, caps);
421   g_return_val_if_fail (props != NULL, caps);
422   g_return_val_if_fail (caps->properties == NULL, caps);
423
424   caps->properties = props;
425
426   return caps;
427 }
428
429 /**
430  * gst_caps_get_props:
431  * @caps: the caps to get the properties from
432  *
433  * Get the properties of the given caps.
434  *
435  * Returns: the properties of the caps
436  */
437 GstProps*
438 gst_caps_get_props (GstCaps *caps)
439 {
440   g_return_val_if_fail (caps != NULL, NULL);
441
442   return caps->properties;
443 }
444
445 /**
446  * gst_caps_chain:
447  * @caps: a capabilty
448  * @...: more capabilities
449  *
450  * chains the given capabilities
451  *
452  * Returns: the new capability
453  */
454 GstCaps*
455 gst_caps_chain (GstCaps *caps, ...)
456 {
457   GstCaps *orig = caps;
458   va_list var_args;
459
460   va_start (var_args, caps);
461
462   while (caps) {
463     GstCaps *toadd;
464     
465     toadd = va_arg (var_args, GstCaps*);
466     gst_caps_append (caps, toadd);
467     
468     caps = toadd;
469   }
470   va_end (var_args);
471   
472   return orig;
473 }
474
475 /**
476  * gst_caps_append:
477  * @caps: a capabilty
478  * @capstoadd: the capability to append
479  *
480  * Appends a capability to the existing capability.
481  *
482  * Returns: the new capability
483  */
484 GstCaps*
485 gst_caps_append (GstCaps *caps, GstCaps *capstoadd)
486 {
487   GstCaps *orig = caps;
488   
489   g_return_val_if_fail (caps != capstoadd, caps);
490
491   if (caps == NULL)
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)\n",
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\n");
571       return FALSE;
572     }
573   }
574   else {
575     /* assume it accepts everything */
576     GST_DEBUG (GST_CAT_CAPS,"no caps\n");
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\n");
596       return TRUE;
597     }
598     else {
599       GST_DEBUG (GST_CAT_CAPS,"no source but destination caps\n");
600       return FALSE;
601     }
602   }
603   else {
604     if (tocaps == NULL) {
605       GST_DEBUG (GST_CAT_CAPS,"source caps and no destination caps\n");
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)\n",
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\n");
669     return gst_caps_copy (caps2);
670   }
671   if (caps2 == NULL) {
672     GST_DEBUG (GST_CAT_CAPS, "second caps is NULL, return other caps\n");
673     return gst_caps_copy (caps1);
674   }
675
676   while (caps1) {
677     GstCaps *othercaps = caps2;
678
679     while (othercaps) {
680       GstCaps *intersection = gst_caps_intersect_func (caps1, othercaps);
681
682       if (intersection) {
683         if (!result) {
684           walk = result = intersection;
685         }
686         else {
687           walk = walk->next = intersection;
688         }
689       }
690       othercaps = othercaps->next;
691     }
692     caps1 =  caps1->next;
693   }
694
695   return result;
696 }
697
698 GstCaps*
699 gst_caps_normalize (GstCaps *caps)
700 {
701   GstCaps *result = NULL, *walk = caps;
702
703   if (caps == NULL)
704     return caps;
705
706   while (caps) {
707     GList *proplist;
708
709     proplist = gst_props_normalize (caps->properties);
710     if (proplist && g_list_next (proplist) == NULL) {
711       if (result == NULL)
712         walk = result = caps;
713       else {
714         walk = walk->next = caps;
715       }
716       goto next;
717     }
718
719     while (proplist) {
720       GstProps *props = (GstProps *) proplist->data;
721       GstCaps *newcaps = gst_caps_new_id (caps->name, caps->id, props);
722
723       if (result == NULL)
724         walk = result = newcaps;
725       else {
726         walk = walk->next = newcaps;
727       }
728       proplist = g_list_next (proplist);  
729     }
730 next:
731     caps = caps->next;
732   }
733   return result;
734 }
735
736 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
737 /**
738  * gst_caps_save_thyself:
739  * @caps: a capabilty to save
740  * @parent: the parent XML node pointer
741  *
742  * Save the capability into an XML representation.
743  *
744  * Returns: a new XML node pointer
745  */
746 xmlNodePtr
747 gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
748 {
749   xmlNodePtr subtree;
750   xmlNodePtr subsubtree;
751
752   while (caps) {
753     subtree = xmlNewChild (parent, NULL, "capscomp", NULL);
754
755     xmlNewChild (subtree, NULL, "name", caps->name);
756     xmlNewChild (subtree, NULL, "type", gst_type_find_by_id (caps->id)->mime);
757     if (caps->properties) {
758       subsubtree = xmlNewChild (subtree, NULL, "properties", NULL);
759
760       gst_props_save_thyself (caps->properties, subsubtree);
761     }
762
763     caps = caps->next;
764   }
765
766   return parent;
767 }
768
769 /**
770  * gst_caps_load_thyself:
771  * @parent: the parent XML node pointer
772  *
773  * Load a new caps from the XML representation.
774  *
775  * Returns: a new capability
776  */
777 GstCaps*
778 gst_caps_load_thyself (xmlNodePtr parent)
779 {
780   GstCaps *result = NULL;
781   xmlNodePtr field = parent->xmlChildrenNode;
782
783   while (field) {
784     if (!strcmp (field->name, "capscomp")) {
785       xmlNodePtr subfield = field->xmlChildrenNode;
786       GstCaps *caps;
787       gchar *content;
788
789       g_mutex_lock (_gst_caps_chunk_lock);
790       caps = g_mem_chunk_alloc0 (_gst_caps_chunk);
791       g_mutex_unlock (_gst_caps_chunk_lock);
792
793       caps->refcount = 1;
794       caps->lock = g_mutex_new ();
795       caps->next = NULL;
796       caps->fixed = TRUE;
797         
798       while (subfield) {
799         if (!strcmp (subfield->name, "name")) {
800           caps->name = xmlNodeGetContent (subfield);
801         }
802         if (!strcmp (subfield->name, "type")) {
803           content = xmlNodeGetContent (subfield);
804           caps->id = get_type_for_mime (content);
805           g_free (content);
806         }
807         else if (!strcmp (subfield->name, "properties")) {
808           caps->properties = gst_props_load_thyself (subfield);
809         }
810         
811         subfield = subfield->next;
812       }
813       result = gst_caps_append (result, caps);
814     }
815     field = field->next;
816   }
817
818   return result;
819 }
820
821 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */